Home | History | Annotate | Line # | Download | only in netinet
sctp_output.c revision 1.1.2.5
      1  1.1.2.5  skrll /*	$NetBSD: sctp_output.c,v 1.1.2.5 2016/07/09 20:25:22 skrll Exp $ */
      2  1.1.2.2  skrll /*	$KAME: sctp_output.c,v 1.48 2005/06/16 18:29:24 jinmei Exp $	*/
      3  1.1.2.2  skrll 
      4  1.1.2.2  skrll /*
      5  1.1.2.2  skrll  * Copyright (C) 2002, 2003, 2004 Cisco Systems Inc,
      6  1.1.2.2  skrll  * All rights reserved.
      7  1.1.2.2  skrll  *
      8  1.1.2.2  skrll  * Redistribution and use in source and binary forms, with or without
      9  1.1.2.2  skrll  * modification, are permitted provided that the following conditions
     10  1.1.2.2  skrll  * are met:
     11  1.1.2.2  skrll  * 1. Redistributions of source code must retain the above copyright
     12  1.1.2.2  skrll  *    notice, this list of conditions and the following disclaimer.
     13  1.1.2.2  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1.2.2  skrll  *    notice, this list of conditions and the following disclaimer in the
     15  1.1.2.2  skrll  *    documentation and/or other materials provided with the distribution.
     16  1.1.2.2  skrll  * 3. Neither the name of the project nor the names of its contributors
     17  1.1.2.2  skrll  *    may be used to endorse or promote products derived from this software
     18  1.1.2.2  skrll  *    without specific prior written permission.
     19  1.1.2.2  skrll  *
     20  1.1.2.2  skrll  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  1.1.2.2  skrll  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  1.1.2.2  skrll  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  1.1.2.2  skrll  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  1.1.2.2  skrll  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  1.1.2.2  skrll  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  1.1.2.2  skrll  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  1.1.2.2  skrll  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  1.1.2.2  skrll  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  1.1.2.2  skrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  1.1.2.2  skrll  * SUCH DAMAGE.
     31  1.1.2.2  skrll  */
     32  1.1.2.2  skrll #include <sys/cdefs.h>
     33  1.1.2.5  skrll __KERNEL_RCSID(0, "$NetBSD: sctp_output.c,v 1.1.2.5 2016/07/09 20:25:22 skrll Exp $");
     34  1.1.2.2  skrll 
     35  1.1.2.2  skrll #ifdef _KERNEL_OPT
     36  1.1.2.2  skrll #include "opt_ipsec.h"
     37  1.1.2.2  skrll #include "opt_inet.h"
     38  1.1.2.2  skrll #include "opt_sctp.h"
     39  1.1.2.2  skrll #endif /* _KERNEL_OPT */
     40  1.1.2.2  skrll 
     41  1.1.2.2  skrll #include <sys/param.h>
     42  1.1.2.2  skrll #include <sys/systm.h>
     43  1.1.2.2  skrll #include <sys/malloc.h>
     44  1.1.2.2  skrll #include <sys/mbuf.h>
     45  1.1.2.2  skrll #include <sys/domain.h>
     46  1.1.2.2  skrll #include <sys/protosw.h>
     47  1.1.2.2  skrll #include <sys/socket.h>
     48  1.1.2.2  skrll #include <sys/socketvar.h>
     49  1.1.2.2  skrll #include <sys/proc.h>
     50  1.1.2.2  skrll #include <sys/kernel.h>
     51  1.1.2.2  skrll #include <sys/sysctl.h>
     52  1.1.2.2  skrll #include <sys/resourcevar.h>
     53  1.1.2.2  skrll #include <sys/uio.h>
     54  1.1.2.2  skrll #ifdef INET6
     55  1.1.2.2  skrll #include <sys/domain.h>
     56  1.1.2.2  skrll #endif
     57  1.1.2.2  skrll 
     58  1.1.2.2  skrll #include <machine/limits.h>
     59  1.1.2.2  skrll #include <machine/cpu.h>
     60  1.1.2.2  skrll 
     61  1.1.2.2  skrll #include <net/if.h>
     62  1.1.2.2  skrll #include <net/if_types.h>
     63  1.1.2.2  skrll 
     64  1.1.2.2  skrll #include <net/route.h>
     65  1.1.2.2  skrll 
     66  1.1.2.2  skrll #include <netinet/in.h>
     67  1.1.2.2  skrll #include <netinet/in_systm.h>
     68  1.1.2.2  skrll #include <netinet/ip.h>
     69  1.1.2.2  skrll #include <netinet/in_pcb.h>
     70  1.1.2.2  skrll #include <netinet/in_var.h>
     71  1.1.2.2  skrll #include <netinet/ip_var.h>
     72  1.1.2.2  skrll 
     73  1.1.2.2  skrll #ifdef INET6
     74  1.1.2.2  skrll #include <netinet/ip6.h>
     75  1.1.2.2  skrll #include <netinet6/ip6_var.h>
     76  1.1.2.2  skrll #include <netinet6/scope6_var.h>
     77  1.1.2.2  skrll #include <netinet6/nd6.h>
     78  1.1.2.2  skrll 
     79  1.1.2.2  skrll #include <netinet6/in6_pcb.h>
     80  1.1.2.2  skrll 
     81  1.1.2.2  skrll #include <netinet/icmp6.h>
     82  1.1.2.2  skrll 
     83  1.1.2.2  skrll #endif /* INET6 */
     84  1.1.2.2  skrll 
     85  1.1.2.2  skrll #include <net/net_osdep.h>
     86  1.1.2.2  skrll 
     87  1.1.2.2  skrll #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__)
     88  1.1.2.2  skrll #ifndef in6pcb
     89  1.1.2.2  skrll #define in6pcb		inpcb
     90  1.1.2.2  skrll #endif
     91  1.1.2.2  skrll #endif
     92  1.1.2.2  skrll 
     93  1.1.2.2  skrll #include <netinet/sctp_pcb.h>
     94  1.1.2.2  skrll 
     95  1.1.2.2  skrll #ifdef IPSEC
     96  1.1.2.4  skrll #include <netipsec/ipsec.h>
     97  1.1.2.4  skrll #include <netipsec/key.h>
     98  1.1.2.2  skrll #endif /* IPSEC */
     99  1.1.2.2  skrll 
    100  1.1.2.2  skrll #include <netinet/sctp_var.h>
    101  1.1.2.2  skrll #include <netinet/sctp_header.h>
    102  1.1.2.2  skrll #include <netinet/sctputil.h>
    103  1.1.2.2  skrll #include <netinet/sctp_pcb.h>
    104  1.1.2.2  skrll #include <netinet/sctp_output.h>
    105  1.1.2.2  skrll #include <netinet/sctp_uio.h>
    106  1.1.2.2  skrll #include <netinet/sctputil.h>
    107  1.1.2.2  skrll #include <netinet/sctp_hashdriver.h>
    108  1.1.2.2  skrll #include <netinet/sctp_timer.h>
    109  1.1.2.2  skrll #include <netinet/sctp_asconf.h>
    110  1.1.2.2  skrll #include <netinet/sctp_indata.h>
    111  1.1.2.2  skrll 
    112  1.1.2.2  skrll #ifdef SCTP_DEBUG
    113  1.1.2.2  skrll extern uint32_t sctp_debug_on;
    114  1.1.2.2  skrll #endif
    115  1.1.2.2  skrll 
    116  1.1.2.2  skrll extern int sctp_peer_chunk_oh;
    117  1.1.2.2  skrll 
    118  1.1.2.2  skrll static int
    119  1.1.2.2  skrll sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
    120  1.1.2.2  skrll {
    121  1.1.2.2  skrll 	struct cmsghdr cmh;
    122  1.1.2.2  skrll 	int tlen, at;
    123  1.1.2.2  skrll 
    124  1.1.2.2  skrll 	tlen = control->m_len;
    125  1.1.2.2  skrll 	at = 0;
    126  1.1.2.2  skrll 	/*
    127  1.1.2.2  skrll 	 * Independent of how many mbufs, find the c_type inside the control
    128  1.1.2.2  skrll 	 * structure and copy out the data.
    129  1.1.2.2  skrll 	 */
    130  1.1.2.2  skrll 	while (at < tlen) {
    131  1.1.2.2  skrll 		if ((tlen-at) < (int)CMSG_ALIGN(sizeof(cmh))) {
    132  1.1.2.2  skrll 			/* not enough room for one more we are done. */
    133  1.1.2.2  skrll 			return (0);
    134  1.1.2.2  skrll 		}
    135  1.1.2.2  skrll 		m_copydata(control, at, sizeof(cmh), (void *)&cmh);
    136  1.1.2.2  skrll 		if ((cmh.cmsg_len + at) > tlen) {
    137  1.1.2.2  skrll 			/*
    138  1.1.2.2  skrll 			 * this is real messed up since there is not enough
    139  1.1.2.2  skrll 			 * data here to cover the cmsg header. We are done.
    140  1.1.2.2  skrll 			 */
    141  1.1.2.2  skrll 			return (0);
    142  1.1.2.2  skrll 		}
    143  1.1.2.2  skrll 		if ((cmh.cmsg_level == IPPROTO_SCTP) &&
    144  1.1.2.2  skrll 		    (c_type == cmh.cmsg_type)) {
    145  1.1.2.2  skrll 			/* found the one we want, copy it out */
    146  1.1.2.2  skrll 			at += CMSG_ALIGN(sizeof(struct cmsghdr));
    147  1.1.2.2  skrll 			if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
    148  1.1.2.2  skrll 				/*
    149  1.1.2.2  skrll 				 * space of cmsg_len after header not
    150  1.1.2.2  skrll 				 * big enough
    151  1.1.2.2  skrll 				 */
    152  1.1.2.2  skrll 				return (0);
    153  1.1.2.2  skrll 			}
    154  1.1.2.2  skrll 			m_copydata(control, at, cpsize, data);
    155  1.1.2.2  skrll 			return (1);
    156  1.1.2.2  skrll 		 } else {
    157  1.1.2.2  skrll 			at += CMSG_ALIGN(cmh.cmsg_len);
    158  1.1.2.2  skrll 			if (cmh.cmsg_len == 0) {
    159  1.1.2.2  skrll 				break;
    160  1.1.2.2  skrll 			}
    161  1.1.2.2  skrll 		}
    162  1.1.2.2  skrll 	}
    163  1.1.2.2  skrll 	/* not found */
    164  1.1.2.2  skrll 	return (0);
    165  1.1.2.2  skrll }
    166  1.1.2.2  skrll 
    167  1.1.2.2  skrll static struct mbuf *
    168  1.1.2.2  skrll sctp_add_addr_to_mbuf(struct mbuf *m, struct ifaddr *ifa)
    169  1.1.2.2  skrll {
    170  1.1.2.2  skrll 	struct sctp_paramhdr *parmh;
    171  1.1.2.2  skrll 	struct mbuf *mret;
    172  1.1.2.2  skrll 	int len;
    173  1.1.2.2  skrll 	if (ifa->ifa_addr->sa_family == AF_INET) {
    174  1.1.2.2  skrll 		len = sizeof(struct sctp_ipv4addr_param);
    175  1.1.2.2  skrll 	} else if (ifa->ifa_addr->sa_family == AF_INET6) {
    176  1.1.2.2  skrll 		len = sizeof(struct sctp_ipv6addr_param);
    177  1.1.2.2  skrll 	} else {
    178  1.1.2.2  skrll 		/* unknown type */
    179  1.1.2.2  skrll 		return (m);
    180  1.1.2.2  skrll 	}
    181  1.1.2.2  skrll 
    182  1.1.2.2  skrll 	if (M_TRAILINGSPACE(m) >= len) {
    183  1.1.2.2  skrll 		/* easy side we just drop it on the end */
    184  1.1.2.2  skrll 		parmh = (struct sctp_paramhdr *)(m->m_data + m->m_len);
    185  1.1.2.2  skrll 		mret = m;
    186  1.1.2.2  skrll 	} else {
    187  1.1.2.2  skrll 		/* Need more space */
    188  1.1.2.2  skrll 		mret = m;
    189  1.1.2.2  skrll 		while (mret->m_next != NULL) {
    190  1.1.2.2  skrll 			mret = mret->m_next;
    191  1.1.2.2  skrll 		}
    192  1.1.2.2  skrll 		MGET(mret->m_next, M_DONTWAIT, MT_DATA);
    193  1.1.2.2  skrll 		if (mret->m_next == NULL) {
    194  1.1.2.2  skrll 			/* We are hosed, can't add more addresses */
    195  1.1.2.2  skrll 			return (m);
    196  1.1.2.2  skrll 		}
    197  1.1.2.2  skrll 		mret = mret->m_next;
    198  1.1.2.2  skrll 		parmh = mtod(mret, struct sctp_paramhdr *);
    199  1.1.2.2  skrll 	}
    200  1.1.2.2  skrll 	/* now add the parameter */
    201  1.1.2.2  skrll 	if (ifa->ifa_addr->sa_family == AF_INET) {
    202  1.1.2.2  skrll 		struct sctp_ipv4addr_param *ipv4p;
    203  1.1.2.2  skrll 		struct sockaddr_in *sin;
    204  1.1.2.2  skrll 		sin = (struct sockaddr_in *)ifa->ifa_addr;
    205  1.1.2.2  skrll 		ipv4p = (struct sctp_ipv4addr_param *)parmh;
    206  1.1.2.2  skrll 		parmh->param_type = htons(SCTP_IPV4_ADDRESS);
    207  1.1.2.2  skrll 		parmh->param_length = htons(len);
    208  1.1.2.2  skrll 		ipv4p->addr = sin->sin_addr.s_addr;
    209  1.1.2.2  skrll 		mret->m_len += len;
    210  1.1.2.2  skrll 	} else if (ifa->ifa_addr->sa_family == AF_INET6) {
    211  1.1.2.2  skrll 		struct sctp_ipv6addr_param *ipv6p;
    212  1.1.2.2  skrll 		struct sockaddr_in6 *sin6;
    213  1.1.2.2  skrll 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
    214  1.1.2.2  skrll 		ipv6p = (struct sctp_ipv6addr_param *)parmh;
    215  1.1.2.2  skrll 		parmh->param_type = htons(SCTP_IPV6_ADDRESS);
    216  1.1.2.2  skrll 		parmh->param_length = htons(len);
    217  1.1.2.2  skrll 		memcpy(ipv6p->addr, &sin6->sin6_addr,
    218  1.1.2.2  skrll 		    sizeof(ipv6p->addr));
    219  1.1.2.2  skrll 		/* clear embedded scope in the address */
    220  1.1.2.2  skrll 		in6_clearscope((struct in6_addr *)ipv6p->addr);
    221  1.1.2.2  skrll 		mret->m_len += len;
    222  1.1.2.2  skrll 	} else {
    223  1.1.2.2  skrll 		return (m);
    224  1.1.2.2  skrll 	}
    225  1.1.2.2  skrll 	return (mret);
    226  1.1.2.2  skrll }
    227  1.1.2.2  skrll 
    228  1.1.2.2  skrll 
    229  1.1.2.2  skrll 
    230  1.1.2.2  skrll static struct mbuf *
    231  1.1.2.2  skrll sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
    232  1.1.2.2  skrll     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in)
    233  1.1.2.2  skrll {
    234  1.1.2.2  skrll 	struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
    235  1.1.2.2  skrll 	struct sctp_state_cookie *stc;
    236  1.1.2.2  skrll 	struct sctp_paramhdr *ph;
    237  1.1.2.2  skrll 	uint8_t *signature;
    238  1.1.2.2  skrll 	int sig_offset;
    239  1.1.2.2  skrll 	uint16_t cookie_sz;
    240  1.1.2.2  skrll 
    241  1.1.2.2  skrll 	mret = NULL;
    242  1.1.2.2  skrll 
    243  1.1.2.2  skrll 	MGET(mret, M_DONTWAIT, MT_DATA);
    244  1.1.2.2  skrll 	if (mret == NULL) {
    245  1.1.2.2  skrll 		return (NULL);
    246  1.1.2.2  skrll 	}
    247  1.1.2.2  skrll 	copy_init = sctp_m_copym(init, init_offset, M_COPYALL, M_DONTWAIT);
    248  1.1.2.2  skrll 	if (copy_init == NULL) {
    249  1.1.2.2  skrll 		sctp_m_freem(mret);
    250  1.1.2.2  skrll 		return (NULL);
    251  1.1.2.2  skrll 	}
    252  1.1.2.2  skrll 	copy_initack = sctp_m_copym(initack, initack_offset, M_COPYALL,
    253  1.1.2.2  skrll 	    M_DONTWAIT);
    254  1.1.2.2  skrll 	if (copy_initack == NULL) {
    255  1.1.2.2  skrll 		sctp_m_freem(mret);
    256  1.1.2.2  skrll 		sctp_m_freem(copy_init);
    257  1.1.2.2  skrll 		return (NULL);
    258  1.1.2.2  skrll 	}
    259  1.1.2.2  skrll 	/* easy side we just drop it on the end */
    260  1.1.2.2  skrll 	ph = mtod(mret, struct sctp_paramhdr *);
    261  1.1.2.2  skrll 	mret->m_len = sizeof(struct sctp_state_cookie) +
    262  1.1.2.2  skrll 	    sizeof(struct sctp_paramhdr);
    263  1.1.2.2  skrll 	stc = (struct sctp_state_cookie *)((vaddr_t)ph +
    264  1.1.2.2  skrll 	    sizeof(struct sctp_paramhdr));
    265  1.1.2.2  skrll 	ph->param_type = htons(SCTP_STATE_COOKIE);
    266  1.1.2.2  skrll 	ph->param_length = 0;	/* fill in at the end */
    267  1.1.2.2  skrll 	/* Fill in the stc cookie data */
    268  1.1.2.2  skrll 	*stc = *stc_in;
    269  1.1.2.2  skrll 
    270  1.1.2.2  skrll 	/* tack the INIT and then the INIT-ACK onto the chain */
    271  1.1.2.2  skrll 	cookie_sz = 0;
    272  1.1.2.2  skrll 	m_at = mret;
    273  1.1.2.2  skrll 	for (m_at = mret; m_at; m_at = m_at->m_next) {
    274  1.1.2.2  skrll 		cookie_sz += m_at->m_len;
    275  1.1.2.2  skrll 		if (m_at->m_next == NULL) {
    276  1.1.2.2  skrll 			m_at->m_next = copy_init;
    277  1.1.2.2  skrll 			break;
    278  1.1.2.2  skrll 		}
    279  1.1.2.2  skrll 	}
    280  1.1.2.2  skrll 
    281  1.1.2.2  skrll 	for (m_at = copy_init; m_at; m_at = m_at->m_next) {
    282  1.1.2.2  skrll 		cookie_sz += m_at->m_len;
    283  1.1.2.2  skrll 		if (m_at->m_next == NULL) {
    284  1.1.2.2  skrll 			m_at->m_next = copy_initack;
    285  1.1.2.2  skrll 			break;
    286  1.1.2.2  skrll 		}
    287  1.1.2.2  skrll 	}
    288  1.1.2.2  skrll 
    289  1.1.2.2  skrll 	for (m_at = copy_initack; m_at; m_at = m_at->m_next) {
    290  1.1.2.2  skrll 		cookie_sz += m_at->m_len;
    291  1.1.2.2  skrll 		if (m_at->m_next == NULL) {
    292  1.1.2.2  skrll 			break;
    293  1.1.2.2  skrll 		}
    294  1.1.2.2  skrll 	}
    295  1.1.2.2  skrll 	MGET(sig, M_DONTWAIT, MT_DATA);
    296  1.1.2.2  skrll 	if (sig == NULL) {
    297  1.1.2.2  skrll 		/* no space */
    298  1.1.2.2  skrll 		sctp_m_freem(mret);
    299  1.1.2.2  skrll 		sctp_m_freem(copy_init);
    300  1.1.2.2  skrll 		sctp_m_freem(copy_initack);
    301  1.1.2.2  skrll 		return (NULL);
    302  1.1.2.2  skrll 	}
    303  1.1.2.2  skrll 	sig->m_len = 0;
    304  1.1.2.2  skrll 	m_at->m_next = sig;
    305  1.1.2.2  skrll 	sig_offset = 0;
    306  1.1.2.2  skrll 	signature = (uint8_t *)(mtod(sig, vaddr_t) + sig_offset);
    307  1.1.2.2  skrll 	/* Time to sign the cookie */
    308  1.1.2.2  skrll 	sctp_hash_digest_m((char *)inp->sctp_ep.secret_key[
    309  1.1.2.2  skrll 	    (int)(inp->sctp_ep.current_secret_number)],
    310  1.1.2.2  skrll 	    SCTP_SECRET_SIZE, mret, sizeof(struct sctp_paramhdr),
    311  1.1.2.2  skrll 	    (uint8_t *)signature);
    312  1.1.2.2  skrll 	sig->m_len += SCTP_SIGNATURE_SIZE;
    313  1.1.2.2  skrll 	cookie_sz += SCTP_SIGNATURE_SIZE;
    314  1.1.2.2  skrll 
    315  1.1.2.2  skrll 	ph->param_length = htons(cookie_sz);
    316  1.1.2.2  skrll 	return (mret);
    317  1.1.2.2  skrll }
    318  1.1.2.2  skrll 
    319  1.1.2.2  skrll 
    320  1.1.2.2  skrll static struct sockaddr_in *
    321  1.1.2.2  skrll 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  1.1.2.2  skrll {
    323  1.1.2.2  skrll 	struct sockaddr_in *sin;
    324  1.1.2.2  skrll 	/*
    325  1.1.2.2  skrll 	 * Here we determine if its a prefered address. A
    326  1.1.2.2  skrll 	 * prefered address means it is the same scope or
    327  1.1.2.2  skrll 	 * higher scope then the destination.
    328  1.1.2.2  skrll 	 *  L = loopback, P = private, G = global
    329  1.1.2.2  skrll 	 * -----------------------------------------
    330  1.1.2.2  skrll 	 *  src    |      dest     |    result
    331  1.1.2.2  skrll 	 *-----------------------------------------
    332  1.1.2.2  skrll 	 *   L     |       L       |    yes
    333  1.1.2.2  skrll 	 *-----------------------------------------
    334  1.1.2.2  skrll 	 *   P     |       L       |    yes
    335  1.1.2.2  skrll 	 *-----------------------------------------
    336  1.1.2.2  skrll 	 *   G     |       L       |    yes
    337  1.1.2.2  skrll 	 *-----------------------------------------
    338  1.1.2.2  skrll 	 *   L     |       P       |    no
    339  1.1.2.2  skrll 	 *-----------------------------------------
    340  1.1.2.2  skrll 	 *   P     |       P       |    yes
    341  1.1.2.2  skrll 	 *-----------------------------------------
    342  1.1.2.2  skrll 	 *   G     |       P       |    no
    343  1.1.2.2  skrll 	 *-----------------------------------------
    344  1.1.2.2  skrll 	 *   L     |       G       |    no
    345  1.1.2.2  skrll 	 *-----------------------------------------
    346  1.1.2.2  skrll 	 *   P     |       G       |    no
    347  1.1.2.2  skrll 	 *-----------------------------------------
    348  1.1.2.2  skrll 	 *   G     |       G       |    yes
    349  1.1.2.2  skrll 	 *-----------------------------------------
    350  1.1.2.2  skrll 	 */
    351  1.1.2.2  skrll 
    352  1.1.2.2  skrll 	if (ifa->ifa_addr->sa_family != AF_INET) {
    353  1.1.2.2  skrll 		/* forget non-v4 */
    354  1.1.2.2  skrll 		return (NULL);
    355  1.1.2.2  skrll 	}
    356  1.1.2.2  skrll 	/* Ok the address may be ok */
    357  1.1.2.2  skrll 	sin = (struct sockaddr_in *)ifa->ifa_addr;
    358  1.1.2.2  skrll 	if (sin->sin_addr.s_addr == 0) {
    359  1.1.2.2  skrll 		return (NULL);
    360  1.1.2.2  skrll 	}
    361  1.1.2.2  skrll 	*sin_local = *sin_loop = 0;
    362  1.1.2.2  skrll 	if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
    363  1.1.2.2  skrll 	    (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
    364  1.1.2.2  skrll 		*sin_loop = 1;
    365  1.1.2.2  skrll 		*sin_local = 1;
    366  1.1.2.2  skrll 	}
    367  1.1.2.2  skrll 	if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
    368  1.1.2.2  skrll 		*sin_local = 1;
    369  1.1.2.2  skrll 	}
    370  1.1.2.2  skrll 	if (!loopscope && *sin_loop) {
    371  1.1.2.2  skrll 		/* Its a loopback address and we don't have loop scope */
    372  1.1.2.2  skrll 		return (NULL);
    373  1.1.2.2  skrll 	}
    374  1.1.2.2  skrll 	if (!ipv4_scope && *sin_local) {
    375  1.1.2.2  skrll 		/* Its a private address, and we don't have private address scope */
    376  1.1.2.2  skrll 		return (NULL);
    377  1.1.2.2  skrll 	}
    378  1.1.2.2  skrll 	if (((ipv4_scope == 0) && (loopscope == 0)) && (*sin_local)) {
    379  1.1.2.2  skrll 		/* its a global src and a private dest */
    380  1.1.2.2  skrll 		return (NULL);
    381  1.1.2.2  skrll 	}
    382  1.1.2.2  skrll 	/* its a prefered address */
    383  1.1.2.2  skrll 	return (sin);
    384  1.1.2.2  skrll }
    385  1.1.2.2  skrll 
    386  1.1.2.2  skrll static struct sockaddr_in *
    387  1.1.2.2  skrll 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  1.1.2.2  skrll {
    389  1.1.2.2  skrll 	struct sockaddr_in *sin;
    390  1.1.2.2  skrll 	/*
    391  1.1.2.2  skrll 	 * Here we determine if its a acceptable address. A
    392  1.1.2.2  skrll 	 * acceptable address means it is the same scope or
    393  1.1.2.2  skrll 	 * higher scope but we can allow for NAT which means
    394  1.1.2.2  skrll 	 * its ok to have a global dest and a private src.
    395  1.1.2.2  skrll 	 *
    396  1.1.2.2  skrll 	 *  L = loopback, P = private, G = global
    397  1.1.2.2  skrll 	 * -----------------------------------------
    398  1.1.2.2  skrll 	 *  src    |      dest     |    result
    399  1.1.2.2  skrll 	 *-----------------------------------------
    400  1.1.2.2  skrll 	 *   L     |       L       |    yes
    401  1.1.2.2  skrll 	 *-----------------------------------------
    402  1.1.2.2  skrll 	 *   P     |       L       |    yes
    403  1.1.2.2  skrll 	 *-----------------------------------------
    404  1.1.2.2  skrll 	 *   G     |       L       |    yes
    405  1.1.2.2  skrll 	 *-----------------------------------------
    406  1.1.2.2  skrll 	 *   L     |       P       |    no
    407  1.1.2.2  skrll 	 *-----------------------------------------
    408  1.1.2.2  skrll 	 *   P     |       P       |    yes
    409  1.1.2.2  skrll 	 *-----------------------------------------
    410  1.1.2.2  skrll 	 *   G     |       P       |    yes - probably this won't work.
    411  1.1.2.2  skrll 	 *-----------------------------------------
    412  1.1.2.2  skrll 	 *   L     |       G       |    no
    413  1.1.2.2  skrll 	 *-----------------------------------------
    414  1.1.2.2  skrll 	 *   P     |       G       |    yes
    415  1.1.2.2  skrll 	 *-----------------------------------------
    416  1.1.2.2  skrll 	 *   G     |       G       |    yes
    417  1.1.2.2  skrll 	 *-----------------------------------------
    418  1.1.2.2  skrll 	 */
    419  1.1.2.2  skrll 
    420  1.1.2.2  skrll 	if (ifa->ifa_addr->sa_family != AF_INET) {
    421  1.1.2.2  skrll 		/* forget non-v4 */
    422  1.1.2.2  skrll 		return (NULL);
    423  1.1.2.2  skrll 	}
    424  1.1.2.2  skrll 	/* Ok the address may be ok */
    425  1.1.2.2  skrll 	sin = (struct sockaddr_in *)ifa->ifa_addr;
    426  1.1.2.2  skrll 	if (sin->sin_addr.s_addr == 0) {
    427  1.1.2.2  skrll 		return (NULL);
    428  1.1.2.2  skrll 	}
    429  1.1.2.2  skrll 	*sin_local = *sin_loop = 0;
    430  1.1.2.2  skrll 	if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
    431  1.1.2.2  skrll 	    (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
    432  1.1.2.2  skrll 		*sin_loop = 1;
    433  1.1.2.2  skrll 		*sin_local = 1;
    434  1.1.2.2  skrll 	}
    435  1.1.2.2  skrll 	if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
    436  1.1.2.2  skrll 		*sin_local = 1;
    437  1.1.2.2  skrll 	}
    438  1.1.2.2  skrll 	if (!loopscope && *sin_loop) {
    439  1.1.2.2  skrll 		/* Its a loopback address and we don't have loop scope */
    440  1.1.2.2  skrll 		return (NULL);
    441  1.1.2.2  skrll 	}
    442  1.1.2.2  skrll 	/* its an acceptable address */
    443  1.1.2.2  skrll 	return (sin);
    444  1.1.2.2  skrll }
    445  1.1.2.2  skrll 
    446  1.1.2.2  skrll /*
    447  1.1.2.2  skrll  * This treats the address list on the ep as a restricted list
    448  1.1.2.2  skrll  * (negative list). If a the passed address is listed, then
    449  1.1.2.2  skrll  * the address is NOT allowed on the association.
    450  1.1.2.2  skrll  */
    451  1.1.2.2  skrll int
    452  1.1.2.2  skrll sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sockaddr *addr)
    453  1.1.2.2  skrll {
    454  1.1.2.2  skrll 	struct sctp_laddr *laddr;
    455  1.1.2.2  skrll #ifdef SCTP_DEBUG
    456  1.1.2.2  skrll 	int cnt=0;
    457  1.1.2.2  skrll #endif
    458  1.1.2.2  skrll 	if (stcb == NULL) {
    459  1.1.2.2  skrll 		/* There are no restrictions, no TCB :-) */
    460  1.1.2.2  skrll 		return (0);
    461  1.1.2.2  skrll 	}
    462  1.1.2.2  skrll #ifdef SCTP_DEBUG
    463  1.1.2.2  skrll 	LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
    464  1.1.2.2  skrll 		cnt++;
    465  1.1.2.2  skrll 	}
    466  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
    467  1.1.2.2  skrll 		printf("There are %d addresses on the restricted list\n", cnt);
    468  1.1.2.2  skrll 	}
    469  1.1.2.2  skrll 	cnt = 0;
    470  1.1.2.2  skrll #endif
    471  1.1.2.2  skrll 	LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
    472  1.1.2.2  skrll 		if (laddr->ifa == NULL) {
    473  1.1.2.2  skrll #ifdef SCTP_DEBUG
    474  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
    475  1.1.2.2  skrll 				printf("Help I have fallen and I can't get up!\n");
    476  1.1.2.2  skrll 			}
    477  1.1.2.2  skrll #endif
    478  1.1.2.2  skrll 			continue;
    479  1.1.2.2  skrll 		}
    480  1.1.2.2  skrll #ifdef SCTP_DEBUG
    481  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
    482  1.1.2.2  skrll 			cnt++;
    483  1.1.2.2  skrll 			printf("Restricted address[%d]:", cnt);
    484  1.1.2.2  skrll 			sctp_print_address(laddr->ifa->ifa_addr);
    485  1.1.2.2  skrll 		}
    486  1.1.2.2  skrll #endif
    487  1.1.2.2  skrll 		if (sctp_cmpaddr(addr, laddr->ifa->ifa_addr) == 1) {
    488  1.1.2.2  skrll 			/* Yes it is on the list */
    489  1.1.2.2  skrll 			return (1);
    490  1.1.2.2  skrll 		}
    491  1.1.2.2  skrll 	}
    492  1.1.2.2  skrll 	return (0);
    493  1.1.2.2  skrll }
    494  1.1.2.2  skrll 
    495  1.1.2.2  skrll static int
    496  1.1.2.2  skrll sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
    497  1.1.2.2  skrll {
    498  1.1.2.2  skrll 	struct sctp_laddr *laddr;
    499  1.1.2.2  skrll 
    500  1.1.2.2  skrll 	if (ifa == NULL)
    501  1.1.2.2  skrll 		return (0);
    502  1.1.2.2  skrll 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
    503  1.1.2.2  skrll 		if (laddr->ifa == NULL) {
    504  1.1.2.2  skrll #ifdef SCTP_DEBUG
    505  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
    506  1.1.2.2  skrll 				printf("Help I have fallen and I can't get up!\n");
    507  1.1.2.2  skrll 			}
    508  1.1.2.2  skrll #endif
    509  1.1.2.2  skrll 			continue;
    510  1.1.2.2  skrll 		}
    511  1.1.2.2  skrll 		if (laddr->ifa->ifa_addr == NULL)
    512  1.1.2.2  skrll 			continue;
    513  1.1.2.2  skrll 		if (laddr->ifa == ifa)
    514  1.1.2.2  skrll 			/* same pointer */
    515  1.1.2.2  skrll 			return (1);
    516  1.1.2.2  skrll 		if (laddr->ifa->ifa_addr->sa_family != ifa->ifa_addr->sa_family) {
    517  1.1.2.2  skrll 			/* skip non compatible address comparison */
    518  1.1.2.2  skrll 			continue;
    519  1.1.2.2  skrll 		}
    520  1.1.2.2  skrll 		if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
    521  1.1.2.2  skrll 			/* Yes it is restricted */
    522  1.1.2.2  skrll 			return (1);
    523  1.1.2.2  skrll 		}
    524  1.1.2.2  skrll 	}
    525  1.1.2.2  skrll 	return (0);
    526  1.1.2.2  skrll }
    527  1.1.2.2  skrll 
    528  1.1.2.2  skrll 
    529  1.1.2.2  skrll 
    530  1.1.2.2  skrll static struct in_addr
    531  1.1.2.2  skrll sctp_choose_v4_boundspecific_inp(struct sctp_inpcb *inp,
    532  1.1.2.2  skrll 				 struct rtentry *rt,
    533  1.1.2.2  skrll 				 uint8_t ipv4_scope,
    534  1.1.2.2  skrll 				 uint8_t loopscope)
    535  1.1.2.2  skrll {
    536  1.1.2.2  skrll 	struct in_addr ans;
    537  1.1.2.2  skrll 	struct sctp_laddr *laddr;
    538  1.1.2.2  skrll 	struct sockaddr_in *sin;
    539  1.1.2.2  skrll 	struct ifnet *ifn;
    540  1.1.2.2  skrll 	struct ifaddr *ifa;
    541  1.1.2.2  skrll 	uint8_t sin_loop, sin_local;
    542  1.1.2.2  skrll 
    543  1.1.2.2  skrll 	/* first question, is the ifn we will emit on
    544  1.1.2.2  skrll 	 * in our list, if so, we want that one.
    545  1.1.2.2  skrll 	 */
    546  1.1.2.2  skrll 	ifn = rt->rt_ifp;
    547  1.1.2.2  skrll 	if (ifn) {
    548  1.1.2.2  skrll 		/* is a prefered one on the interface we route out? */
    549  1.1.2.5  skrll 		IFADDR_READER_FOREACH(ifa, ifn) {
    550  1.1.2.2  skrll 			sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    551  1.1.2.2  skrll 			if (sin == NULL)
    552  1.1.2.2  skrll 				continue;
    553  1.1.2.2  skrll 			if (sctp_is_addr_in_ep(inp, ifa)) {
    554  1.1.2.2  skrll 				return (sin->sin_addr);
    555  1.1.2.2  skrll 			}
    556  1.1.2.2  skrll 		}
    557  1.1.2.2  skrll 		/* is an acceptable one on the interface we route out? */
    558  1.1.2.5  skrll 		IFADDR_READER_FOREACH(ifa, ifn) {
    559  1.1.2.2  skrll 			sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    560  1.1.2.2  skrll 			if (sin == NULL)
    561  1.1.2.2  skrll 				continue;
    562  1.1.2.2  skrll 			if (sctp_is_addr_in_ep(inp, ifa)) {
    563  1.1.2.2  skrll 				return (sin->sin_addr);
    564  1.1.2.2  skrll 			}
    565  1.1.2.2  skrll 		}
    566  1.1.2.2  skrll 	}
    567  1.1.2.2  skrll 	/* ok, what about a prefered address in the inp */
    568  1.1.2.2  skrll 	for (laddr = LIST_FIRST(&inp->sctp_addr_list);
    569  1.1.2.2  skrll 	     laddr && (laddr != inp->next_addr_touse);
    570  1.1.2.2  skrll 	     laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
    571  1.1.2.2  skrll 		if (laddr->ifa == NULL) {
    572  1.1.2.2  skrll 			/* address has been removed */
    573  1.1.2.2  skrll 			continue;
    574  1.1.2.2  skrll 		}
    575  1.1.2.2  skrll 		sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    576  1.1.2.2  skrll 		if (sin == NULL)
    577  1.1.2.2  skrll 			continue;
    578  1.1.2.2  skrll 		return (sin->sin_addr);
    579  1.1.2.2  skrll 
    580  1.1.2.2  skrll 	}
    581  1.1.2.2  skrll 	/* ok, what about an acceptable address in the inp */
    582  1.1.2.2  skrll 	for (laddr = LIST_FIRST(&inp->sctp_addr_list);
    583  1.1.2.2  skrll 	     laddr && (laddr != inp->next_addr_touse);
    584  1.1.2.2  skrll 	     laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
    585  1.1.2.2  skrll 		if (laddr->ifa == NULL) {
    586  1.1.2.2  skrll 			/* address has been removed */
    587  1.1.2.2  skrll 			continue;
    588  1.1.2.2  skrll 		}
    589  1.1.2.2  skrll 		sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    590  1.1.2.2  skrll 		if (sin == NULL)
    591  1.1.2.2  skrll 			continue;
    592  1.1.2.2  skrll 		return (sin->sin_addr);
    593  1.1.2.2  skrll 
    594  1.1.2.2  skrll 	}
    595  1.1.2.2  skrll 
    596  1.1.2.2  skrll 	/* no address bound can be a source for the destination we are in trouble */
    597  1.1.2.2  skrll #ifdef SCTP_DEBUG
    598  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
    599  1.1.2.2  skrll 		printf("Src address selection for EP, no acceptable src address found for address\n");
    600  1.1.2.2  skrll 	}
    601  1.1.2.2  skrll #endif
    602  1.1.2.2  skrll 	memset(&ans, 0, sizeof(ans));
    603  1.1.2.2  skrll 	return (ans);
    604  1.1.2.2  skrll }
    605  1.1.2.2  skrll 
    606  1.1.2.2  skrll 
    607  1.1.2.2  skrll 
    608  1.1.2.2  skrll static struct in_addr
    609  1.1.2.2  skrll sctp_choose_v4_boundspecific_stcb(struct sctp_inpcb *inp,
    610  1.1.2.2  skrll 				  struct sctp_tcb *stcb,
    611  1.1.2.2  skrll 				  struct sctp_nets *net,
    612  1.1.2.2  skrll 				  struct rtentry *rt,
    613  1.1.2.2  skrll  			          uint8_t ipv4_scope,
    614  1.1.2.2  skrll 				  uint8_t loopscope,
    615  1.1.2.2  skrll 				  int non_asoc_addr_ok)
    616  1.1.2.2  skrll {
    617  1.1.2.2  skrll 	/*
    618  1.1.2.2  skrll 	 * Here we have two cases, bound all asconf
    619  1.1.2.2  skrll 	 * allowed. bound all asconf not allowed.
    620  1.1.2.2  skrll 	 *
    621  1.1.2.2  skrll 	 */
    622  1.1.2.2  skrll 	struct sctp_laddr *laddr, *starting_point;
    623  1.1.2.2  skrll 	struct in_addr ans;
    624  1.1.2.2  skrll 	struct ifnet *ifn;
    625  1.1.2.2  skrll 	struct ifaddr *ifa;
    626  1.1.2.2  skrll 	uint8_t sin_loop, sin_local, start_at_beginning=0;
    627  1.1.2.2  skrll 	struct sockaddr_in *sin;
    628  1.1.2.2  skrll 
    629  1.1.2.2  skrll 	/* first question, is the ifn we will emit on
    630  1.1.2.2  skrll 	 * in our list, if so, we want that one.
    631  1.1.2.2  skrll 	 */
    632  1.1.2.2  skrll 	ifn = rt->rt_ifp;
    633  1.1.2.2  skrll 
    634  1.1.2.2  skrll  	if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
    635  1.1.2.2  skrll 		/*
    636  1.1.2.2  skrll 		 * Here we use the list of addresses on the endpoint. Then
    637  1.1.2.2  skrll 		 * the addresses listed on the "restricted" list is just that,
    638  1.1.2.2  skrll 		 * address that have not been added and can't be used (unless
    639  1.1.2.2  skrll 		 * the non_asoc_addr_ok is set).
    640  1.1.2.2  skrll 		 */
    641  1.1.2.2  skrll #ifdef SCTP_DEBUG
    642  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
    643  1.1.2.2  skrll 			printf("Have a STCB - asconf allowed, not bound all have a netgative list\n");
    644  1.1.2.2  skrll 		}
    645  1.1.2.2  skrll #endif
    646  1.1.2.2  skrll 		/* first question, is the ifn we will emit on
    647  1.1.2.2  skrll 		 * in our list, if so, we want that one.
    648  1.1.2.2  skrll 		 */
    649  1.1.2.2  skrll 		if (ifn) {
    650  1.1.2.2  skrll 			/* first try for an prefered address on the ep */
    651  1.1.2.5  skrll 			IFADDR_READER_FOREACH(ifa, ifn) {
    652  1.1.2.2  skrll 				if (sctp_is_addr_in_ep(inp, ifa)) {
    653  1.1.2.2  skrll 					sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    654  1.1.2.2  skrll 					if (sin == NULL)
    655  1.1.2.2  skrll 						continue;
    656  1.1.2.2  skrll 					if ((non_asoc_addr_ok == 0) &&
    657  1.1.2.2  skrll 					    (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
    658  1.1.2.2  skrll 						/* on the no-no list */
    659  1.1.2.2  skrll 						continue;
    660  1.1.2.2  skrll 					}
    661  1.1.2.2  skrll 					return (sin->sin_addr);
    662  1.1.2.2  skrll 				}
    663  1.1.2.2  skrll 			}
    664  1.1.2.2  skrll 			/* next try for an acceptable address on the ep */
    665  1.1.2.5  skrll 			IFADDR_READER_FOREACH(ifa, ifn) {
    666  1.1.2.2  skrll 				if (sctp_is_addr_in_ep(inp, ifa)) {
    667  1.1.2.2  skrll 					sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    668  1.1.2.2  skrll 					if (sin == NULL)
    669  1.1.2.2  skrll 						continue;
    670  1.1.2.2  skrll 					if ((non_asoc_addr_ok == 0) &&
    671  1.1.2.2  skrll 					    (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
    672  1.1.2.2  skrll 						/* on the no-no list */
    673  1.1.2.2  skrll 						continue;
    674  1.1.2.2  skrll 					}
    675  1.1.2.2  skrll 					return (sin->sin_addr);
    676  1.1.2.2  skrll 				}
    677  1.1.2.2  skrll 			}
    678  1.1.2.2  skrll 
    679  1.1.2.2  skrll 		}
    680  1.1.2.2  skrll 		/* if we can't find one like that then we must
    681  1.1.2.2  skrll 		 * look at all addresses bound to pick one at
    682  1.1.2.2  skrll 		 * first prefereable then secondly acceptable.
    683  1.1.2.2  skrll 		 */
    684  1.1.2.2  skrll 		starting_point = stcb->asoc.last_used_address;
    685  1.1.2.2  skrll 	sctpv4_from_the_top:
    686  1.1.2.2  skrll 		if (stcb->asoc.last_used_address == NULL) {
    687  1.1.2.2  skrll 			start_at_beginning=1;
    688  1.1.2.2  skrll 			stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
    689  1.1.2.2  skrll 		}
    690  1.1.2.2  skrll 		/* search beginning with the last used address */
    691  1.1.2.2  skrll 		for (laddr = stcb->asoc.last_used_address; laddr;
    692  1.1.2.2  skrll 		     laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
    693  1.1.2.2  skrll 			if (laddr->ifa == NULL) {
    694  1.1.2.2  skrll 				/* address has been removed */
    695  1.1.2.2  skrll 				continue;
    696  1.1.2.2  skrll 			}
    697  1.1.2.2  skrll 			sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    698  1.1.2.2  skrll 			if (sin == NULL)
    699  1.1.2.2  skrll 				continue;
    700  1.1.2.2  skrll 			if ((non_asoc_addr_ok == 0) &&
    701  1.1.2.2  skrll 			    (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
    702  1.1.2.2  skrll 				/* on the no-no list */
    703  1.1.2.2  skrll 				continue;
    704  1.1.2.2  skrll 			}
    705  1.1.2.2  skrll 			return (sin->sin_addr);
    706  1.1.2.2  skrll 
    707  1.1.2.2  skrll 		}
    708  1.1.2.2  skrll 		if (start_at_beginning == 0) {
    709  1.1.2.2  skrll 			stcb->asoc.last_used_address = NULL;
    710  1.1.2.2  skrll 			goto sctpv4_from_the_top;
    711  1.1.2.2  skrll 		}
    712  1.1.2.2  skrll 		/* now try for any higher scope than the destination */
    713  1.1.2.2  skrll 		stcb->asoc.last_used_address = starting_point;
    714  1.1.2.2  skrll 		start_at_beginning = 0;
    715  1.1.2.2  skrll 	sctpv4_from_the_top2:
    716  1.1.2.2  skrll 		if (stcb->asoc.last_used_address == NULL) {
    717  1.1.2.2  skrll 			start_at_beginning=1;
    718  1.1.2.2  skrll 			stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
    719  1.1.2.2  skrll 		}
    720  1.1.2.2  skrll 		/* search beginning with the last used address */
    721  1.1.2.2  skrll 		for (laddr = stcb->asoc.last_used_address; laddr;
    722  1.1.2.2  skrll 		     laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
    723  1.1.2.2  skrll 			if (laddr->ifa == NULL) {
    724  1.1.2.2  skrll 				/* address has been removed */
    725  1.1.2.2  skrll 				continue;
    726  1.1.2.2  skrll 			}
    727  1.1.2.2  skrll 			sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    728  1.1.2.2  skrll 			if (sin == NULL)
    729  1.1.2.2  skrll 				continue;
    730  1.1.2.2  skrll 			if ((non_asoc_addr_ok == 0) &&
    731  1.1.2.2  skrll 			    (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
    732  1.1.2.2  skrll 				/* on the no-no list */
    733  1.1.2.2  skrll 				continue;
    734  1.1.2.2  skrll 			}
    735  1.1.2.2  skrll 			return (sin->sin_addr);
    736  1.1.2.2  skrll 		}
    737  1.1.2.2  skrll 		if (start_at_beginning == 0) {
    738  1.1.2.2  skrll 			stcb->asoc.last_used_address = NULL;
    739  1.1.2.2  skrll 			goto sctpv4_from_the_top2;
    740  1.1.2.2  skrll 		}
    741  1.1.2.2  skrll 	} else {
    742  1.1.2.2  skrll 		/*
    743  1.1.2.2  skrll 		 * Here we have an address list on the association, thats the
    744  1.1.2.2  skrll 		 * only valid source addresses that we can use.
    745  1.1.2.2  skrll 		 */
    746  1.1.2.2  skrll #ifdef SCTP_DEBUG
    747  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
    748  1.1.2.2  skrll 			printf("Have a STCB - no asconf allowed, not bound all have a postive list\n");
    749  1.1.2.2  skrll 		}
    750  1.1.2.2  skrll #endif
    751  1.1.2.2  skrll 		/* First look at all addresses for one that is on
    752  1.1.2.2  skrll 		 * the interface we route out
    753  1.1.2.2  skrll 		 */
    754  1.1.2.2  skrll 		LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
    755  1.1.2.2  skrll 			     sctp_nxt_addr) {
    756  1.1.2.2  skrll 			if (laddr->ifa == NULL) {
    757  1.1.2.2  skrll 				/* address has been removed */
    758  1.1.2.2  skrll 				continue;
    759  1.1.2.2  skrll 			}
    760  1.1.2.2  skrll 			sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    761  1.1.2.2  skrll 			if (sin == NULL)
    762  1.1.2.2  skrll 				continue;
    763  1.1.2.2  skrll 			/* first question, is laddr->ifa an address associated with the emit interface */
    764  1.1.2.2  skrll 			if (ifn) {
    765  1.1.2.5  skrll 				IFADDR_READER_FOREACH(ifa, ifn) {
    766  1.1.2.2  skrll 					if (laddr->ifa == ifa) {
    767  1.1.2.2  skrll 						sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
    768  1.1.2.2  skrll 						return (sin->sin_addr);
    769  1.1.2.2  skrll 					}
    770  1.1.2.2  skrll 					if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
    771  1.1.2.2  skrll 						sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
    772  1.1.2.2  skrll 						return (sin->sin_addr);
    773  1.1.2.2  skrll 					}
    774  1.1.2.2  skrll 				}
    775  1.1.2.2  skrll 			}
    776  1.1.2.2  skrll 		}
    777  1.1.2.2  skrll 		/* what about an acceptable one on the interface? */
    778  1.1.2.2  skrll 		LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
    779  1.1.2.2  skrll 			     sctp_nxt_addr) {
    780  1.1.2.2  skrll 			if (laddr->ifa == NULL) {
    781  1.1.2.2  skrll 				/* address has been removed */
    782  1.1.2.2  skrll 				continue;
    783  1.1.2.2  skrll 			}
    784  1.1.2.2  skrll 			sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    785  1.1.2.2  skrll 			if (sin == NULL)
    786  1.1.2.2  skrll 				continue;
    787  1.1.2.2  skrll 			/* first question, is laddr->ifa an address associated with the emit interface */
    788  1.1.2.2  skrll 			if (ifn) {
    789  1.1.2.5  skrll 				IFADDR_READER_FOREACH(ifa, ifn) {
    790  1.1.2.2  skrll 					if (laddr->ifa == ifa) {
    791  1.1.2.2  skrll 						sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
    792  1.1.2.2  skrll 						return (sin->sin_addr);
    793  1.1.2.2  skrll 					}
    794  1.1.2.2  skrll 					if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
    795  1.1.2.2  skrll 						sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
    796  1.1.2.2  skrll 						return (sin->sin_addr);
    797  1.1.2.2  skrll 					}
    798  1.1.2.2  skrll 				}
    799  1.1.2.2  skrll 			}
    800  1.1.2.2  skrll 		}
    801  1.1.2.2  skrll 		/* ok, next one that is preferable in general */
    802  1.1.2.2  skrll 		LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
    803  1.1.2.2  skrll 			     sctp_nxt_addr) {
    804  1.1.2.2  skrll 			if (laddr->ifa == NULL) {
    805  1.1.2.2  skrll 				/* address has been removed */
    806  1.1.2.2  skrll 				continue;
    807  1.1.2.2  skrll 			}
    808  1.1.2.2  skrll 			sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    809  1.1.2.2  skrll 			if (sin == NULL)
    810  1.1.2.2  skrll 				continue;
    811  1.1.2.2  skrll 			return (sin->sin_addr);
    812  1.1.2.2  skrll 		}
    813  1.1.2.2  skrll 
    814  1.1.2.2  skrll 		/* last, what about one that is acceptable */
    815  1.1.2.2  skrll 		LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
    816  1.1.2.2  skrll 			     sctp_nxt_addr) {
    817  1.1.2.2  skrll 			if (laddr->ifa == NULL) {
    818  1.1.2.2  skrll 				/* address has been removed */
    819  1.1.2.2  skrll 				continue;
    820  1.1.2.2  skrll 			}
    821  1.1.2.2  skrll 			sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    822  1.1.2.2  skrll 			if (sin == NULL)
    823  1.1.2.2  skrll 				continue;
    824  1.1.2.2  skrll 			return (sin->sin_addr);
    825  1.1.2.2  skrll 		}
    826  1.1.2.2  skrll 	}
    827  1.1.2.2  skrll 	memset(&ans, 0, sizeof(ans));
    828  1.1.2.2  skrll 	return (ans);
    829  1.1.2.2  skrll }
    830  1.1.2.2  skrll 
    831  1.1.2.2  skrll static struct sockaddr_in *
    832  1.1.2.2  skrll sctp_select_v4_nth_prefered_addr_from_ifn_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok,
    833  1.1.2.2  skrll 						    uint8_t loopscope, uint8_t ipv4_scope, int cur_addr_num)
    834  1.1.2.2  skrll {
    835  1.1.2.2  skrll 	struct ifaddr *ifa;
    836  1.1.2.2  skrll 	struct sockaddr_in *sin;
    837  1.1.2.2  skrll 	uint8_t sin_loop, sin_local;
    838  1.1.2.2  skrll 	int num_eligible_addr = 0;
    839  1.1.2.5  skrll 	IFADDR_READER_FOREACH(ifa, ifn) {
    840  1.1.2.2  skrll 		sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    841  1.1.2.2  skrll 		if (sin == NULL)
    842  1.1.2.2  skrll 			continue;
    843  1.1.2.2  skrll 		if (stcb) {
    844  1.1.2.2  skrll 			if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
    845  1.1.2.2  skrll 				/* It is restricted for some reason.. probably
    846  1.1.2.2  skrll 				 * not yet added.
    847  1.1.2.2  skrll 				 */
    848  1.1.2.2  skrll 				continue;
    849  1.1.2.2  skrll 			}
    850  1.1.2.2  skrll 		}
    851  1.1.2.2  skrll 		if (cur_addr_num == num_eligible_addr) {
    852  1.1.2.2  skrll 			return (sin);
    853  1.1.2.2  skrll 		}
    854  1.1.2.2  skrll 	}
    855  1.1.2.2  skrll 	return (NULL);
    856  1.1.2.2  skrll }
    857  1.1.2.2  skrll 
    858  1.1.2.2  skrll 
    859  1.1.2.2  skrll static int
    860  1.1.2.2  skrll sctp_count_v4_num_prefered_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok,
    861  1.1.2.2  skrll 				     uint8_t loopscope, uint8_t ipv4_scope, uint8_t *sin_loop, uint8_t *sin_local)
    862  1.1.2.2  skrll {
    863  1.1.2.2  skrll 	struct ifaddr *ifa;
    864  1.1.2.2  skrll 	struct sockaddr_in *sin;
    865  1.1.2.2  skrll 	int num_eligible_addr = 0;
    866  1.1.2.2  skrll 
    867  1.1.2.5  skrll 	IFADDR_READER_FOREACH(ifa, ifn) {
    868  1.1.2.2  skrll 		sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, sin_loop, sin_local);
    869  1.1.2.2  skrll 		if (sin == NULL)
    870  1.1.2.2  skrll 			continue;
    871  1.1.2.2  skrll 		if (stcb) {
    872  1.1.2.2  skrll 			if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
    873  1.1.2.2  skrll 				/* It is restricted for some reason.. probably
    874  1.1.2.2  skrll 				 * not yet added.
    875  1.1.2.2  skrll 				 */
    876  1.1.2.2  skrll 				continue;
    877  1.1.2.2  skrll 			}
    878  1.1.2.2  skrll 		}
    879  1.1.2.2  skrll 		num_eligible_addr++;
    880  1.1.2.2  skrll 	}
    881  1.1.2.2  skrll 	return (num_eligible_addr);
    882  1.1.2.2  skrll 
    883  1.1.2.2  skrll }
    884  1.1.2.2  skrll 
    885  1.1.2.2  skrll static struct in_addr
    886  1.1.2.2  skrll sctp_choose_v4_boundall(struct sctp_inpcb *inp,
    887  1.1.2.2  skrll 			struct sctp_tcb *stcb,
    888  1.1.2.2  skrll 			struct sctp_nets *net,
    889  1.1.2.2  skrll 			struct rtentry *rt,
    890  1.1.2.2  skrll 			uint8_t ipv4_scope,
    891  1.1.2.2  skrll 			uint8_t loopscope,
    892  1.1.2.2  skrll 			int non_asoc_addr_ok)
    893  1.1.2.2  skrll {
    894  1.1.2.2  skrll 	int cur_addr_num=0, num_prefered=0;
    895  1.1.2.2  skrll 	uint8_t sin_loop, sin_local;
    896  1.1.2.2  skrll 	struct ifnet *ifn;
    897  1.1.2.2  skrll 	struct sockaddr_in *sin;
    898  1.1.2.2  skrll 	struct in_addr ans;
    899  1.1.2.2  skrll 	struct ifaddr *ifa;
    900  1.1.2.4  skrll 	int s;
    901  1.1.2.2  skrll 	/*
    902  1.1.2.2  skrll 	 * For v4 we can use (in boundall) any address in the association. If
    903  1.1.2.2  skrll 	 * non_asoc_addr_ok is set we can use any address (at least in theory).
    904  1.1.2.2  skrll 	 * So we look for prefered addresses first. If we find one, we use it.
    905  1.1.2.2  skrll 	 * Otherwise we next try to get an address on the interface, which we
    906  1.1.2.2  skrll 	 * should be able to do (unless non_asoc_addr_ok is false and we are
    907  1.1.2.2  skrll 	 * routed out that way). In these cases where we can't use the address
    908  1.1.2.2  skrll 	 * of the interface we go through all the ifn's looking for an address
    909  1.1.2.2  skrll 	 * we can use and fill that in. Punting means we send back address
    910  1.1.2.2  skrll 	 * 0, which will probably cause problems actually since then IP will
    911  1.1.2.2  skrll 	 * fill in the address of the route ifn, which means we probably already
    912  1.1.2.2  skrll 	 * rejected it.. i.e. here comes an abort :-<.
    913  1.1.2.2  skrll 	 */
    914  1.1.2.2  skrll 	ifn = rt->rt_ifp;
    915  1.1.2.2  skrll 	if (net) {
    916  1.1.2.2  skrll 		cur_addr_num = net->indx_of_eligible_next_to_use;
    917  1.1.2.2  skrll 	}
    918  1.1.2.2  skrll 	if (ifn == NULL) {
    919  1.1.2.2  skrll  		goto bound_all_v4_plan_c;
    920  1.1.2.2  skrll 	}
    921  1.1.2.2  skrll 	num_prefered = sctp_count_v4_num_prefered_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, ipv4_scope, &sin_loop, &sin_local);
    922  1.1.2.2  skrll #ifdef SCTP_DEBUG
    923  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
    924  1.1.2.2  skrll 		printf("Found %d prefered source addresses\n", num_prefered);
    925  1.1.2.2  skrll 	}
    926  1.1.2.2  skrll #endif
    927  1.1.2.2  skrll 	if (num_prefered == 0) {
    928  1.1.2.2  skrll 		/* no eligible addresses, we must use some other
    929  1.1.2.2  skrll 		 * interface address if we can find one.
    930  1.1.2.2  skrll 		 */
    931  1.1.2.2  skrll  		goto bound_all_v4_plan_b;
    932  1.1.2.2  skrll 	}
    933  1.1.2.2  skrll 	/* Ok we have num_eligible_addr set with how many we can use,
    934  1.1.2.2  skrll 	 * this may vary from call to call due to addresses being deprecated etc..
    935  1.1.2.2  skrll 	 */
    936  1.1.2.2  skrll 	if (cur_addr_num >= num_prefered) {
    937  1.1.2.2  skrll 		cur_addr_num = 0;
    938  1.1.2.2  skrll 	}
    939  1.1.2.2  skrll 	/* select the nth address from the list (where cur_addr_num is the nth) and
    940  1.1.2.2  skrll 	 * 0 is the first one, 1 is the second one etc...
    941  1.1.2.2  skrll 	 */
    942  1.1.2.2  skrll #ifdef SCTP_DEBUG
    943  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
    944  1.1.2.2  skrll 		printf("cur_addr_num:%d\n", cur_addr_num);
    945  1.1.2.2  skrll 	}
    946  1.1.2.2  skrll #endif
    947  1.1.2.2  skrll 	sin = sctp_select_v4_nth_prefered_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
    948  1.1.2.2  skrll 								   ipv4_scope, cur_addr_num);
    949  1.1.2.2  skrll 
    950  1.1.2.2  skrll 	/* if sin is NULL something changed??, plan_a now */
    951  1.1.2.2  skrll 	if (sin) {
    952  1.1.2.2  skrll 		return (sin->sin_addr);
    953  1.1.2.2  skrll 	}
    954  1.1.2.2  skrll 
    955  1.1.2.2  skrll 	/*
    956  1.1.2.2  skrll 	 * plan_b: Look at the interface that we emit on
    957  1.1.2.2  skrll 	 *         and see if we can find an acceptable address.
    958  1.1.2.2  skrll 	 */
    959  1.1.2.2  skrll  bound_all_v4_plan_b:
    960  1.1.2.5  skrll 	IFADDR_READER_FOREACH(ifa, ifn) {
    961  1.1.2.2  skrll 		sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    962  1.1.2.2  skrll 		if (sin == NULL)
    963  1.1.2.2  skrll 			continue;
    964  1.1.2.2  skrll 		if (stcb) {
    965  1.1.2.2  skrll 			if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
    966  1.1.2.2  skrll 				/* It is restricted for some reason.. probably
    967  1.1.2.2  skrll 				 * not yet added.
    968  1.1.2.2  skrll 				 */
    969  1.1.2.2  skrll 				continue;
    970  1.1.2.2  skrll 			}
    971  1.1.2.2  skrll 		}
    972  1.1.2.2  skrll 		return (sin->sin_addr);
    973  1.1.2.2  skrll 	}
    974  1.1.2.2  skrll 	/*
    975  1.1.2.2  skrll 	 * plan_c: Look at all interfaces and find a prefered
    976  1.1.2.2  skrll 	 *         address. If we reache here we are in trouble I think.
    977  1.1.2.2  skrll 	 */
    978  1.1.2.2  skrll  bound_all_v4_plan_c:
    979  1.1.2.4  skrll 	s = pserialize_read_enter();
    980  1.1.2.4  skrll 	IFNET_READER_FOREACH(ifn) {
    981  1.1.2.3  skrll 		if (ifn == inp->next_ifn_touse)
    982  1.1.2.3  skrll 			break;
    983  1.1.2.2  skrll 		if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
    984  1.1.2.2  skrll 			/* wrong base scope */
    985  1.1.2.2  skrll 			continue;
    986  1.1.2.2  skrll 		}
    987  1.1.2.2  skrll 		if (ifn == rt->rt_ifp)
    988  1.1.2.2  skrll 			/* already looked at this guy */
    989  1.1.2.2  skrll 			continue;
    990  1.1.2.2  skrll 		num_prefered = sctp_count_v4_num_prefered_boundall (ifn, stcb, non_asoc_addr_ok,
    991  1.1.2.2  skrll 								    loopscope, ipv4_scope, &sin_loop, &sin_local);
    992  1.1.2.2  skrll #ifdef SCTP_DEBUG
    993  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
    994  1.1.2.2  skrll 			printf("Found ifn:%p %d prefered source addresses\n", ifn, num_prefered);
    995  1.1.2.2  skrll 		}
    996  1.1.2.2  skrll #endif
    997  1.1.2.2  skrll 		if (num_prefered == 0) {
    998  1.1.2.2  skrll 			/*
    999  1.1.2.2  skrll 			 * None on this interface.
   1000  1.1.2.2  skrll 			 */
   1001  1.1.2.2  skrll 			continue;
   1002  1.1.2.2  skrll 		}
   1003  1.1.2.2  skrll 		/* Ok we have num_eligible_addr set with how many we can use,
   1004  1.1.2.2  skrll 		 * this may vary from call to call due to addresses being deprecated etc..
   1005  1.1.2.2  skrll 		 */
   1006  1.1.2.2  skrll 		if (cur_addr_num >= num_prefered) {
   1007  1.1.2.2  skrll 			cur_addr_num = 0;
   1008  1.1.2.2  skrll 		}
   1009  1.1.2.2  skrll 		sin = sctp_select_v4_nth_prefered_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
   1010  1.1.2.2  skrll 									  ipv4_scope, cur_addr_num);
   1011  1.1.2.2  skrll 		if (sin == NULL)
   1012  1.1.2.2  skrll 			continue;
   1013  1.1.2.4  skrll 		pserialize_read_exit(s);
   1014  1.1.2.2  skrll 		return (sin->sin_addr);
   1015  1.1.2.2  skrll 
   1016  1.1.2.2  skrll 	}
   1017  1.1.2.4  skrll 	pserialize_read_exit(s);
   1018  1.1.2.2  skrll 
   1019  1.1.2.2  skrll 	/*
   1020  1.1.2.2  skrll 	 * plan_d: We are in deep trouble. No prefered address on
   1021  1.1.2.2  skrll 	 *         any interface. And the emit interface does not
   1022  1.1.2.2  skrll 	 *         even have an acceptable address. Take anything
   1023  1.1.2.2  skrll 	 *         we can get! If this does not work we are
   1024  1.1.2.2  skrll 	 *         probably going to emit a packet that will
   1025  1.1.2.2  skrll 	 *         illicit an ABORT, falling through.
   1026  1.1.2.2  skrll 	 */
   1027  1.1.2.2  skrll 
   1028  1.1.2.4  skrll 	s = pserialize_read_enter();
   1029  1.1.2.4  skrll 	IFNET_READER_FOREACH(ifn) {
   1030  1.1.2.3  skrll 		if (ifn == inp->next_ifn_touse)
   1031  1.1.2.3  skrll 			break;
   1032  1.1.2.2  skrll 		if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
   1033  1.1.2.2  skrll 			/* wrong base scope */
   1034  1.1.2.2  skrll 			continue;
   1035  1.1.2.2  skrll 		}
   1036  1.1.2.2  skrll 		if (ifn == rt->rt_ifp)
   1037  1.1.2.2  skrll 			/* already looked at this guy */
   1038  1.1.2.2  skrll 			continue;
   1039  1.1.2.2  skrll 
   1040  1.1.2.5  skrll 		IFADDR_READER_FOREACH(ifa, ifn) {
   1041  1.1.2.2  skrll 			sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
   1042  1.1.2.2  skrll 			if (sin == NULL)
   1043  1.1.2.2  skrll 				continue;
   1044  1.1.2.2  skrll 			if (stcb) {
   1045  1.1.2.2  skrll 				if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
   1046  1.1.2.2  skrll 					/* It is restricted for some reason.. probably
   1047  1.1.2.2  skrll 					 * not yet added.
   1048  1.1.2.2  skrll 					 */
   1049  1.1.2.2  skrll 					continue;
   1050  1.1.2.2  skrll 				}
   1051  1.1.2.2  skrll 			}
   1052  1.1.2.4  skrll 			pserialize_read_exit(s);
   1053  1.1.2.2  skrll 			return (sin->sin_addr);
   1054  1.1.2.2  skrll 		}
   1055  1.1.2.2  skrll 	}
   1056  1.1.2.4  skrll 	pserialize_read_exit(s);
   1057  1.1.2.2  skrll 	/*
   1058  1.1.2.2  skrll 	 * Ok we can find NO address to source from that is
   1059  1.1.2.2  skrll 	 * not on our negative list. It is either the special
   1060  1.1.2.2  skrll 	 * ASCONF case where we are sourceing from a intf that
   1061  1.1.2.2  skrll 	 * has been ifconfig'd to a different address (i.e.
   1062  1.1.2.2  skrll 	 * it holds a ADD/DEL/SET-PRIM and the proper lookup
   1063  1.1.2.2  skrll 	 * address. OR we are hosed, and this baby is going
   1064  1.1.2.2  skrll 	 * to abort the association.
   1065  1.1.2.2  skrll 	 */
   1066  1.1.2.2  skrll 	if (non_asoc_addr_ok) {
   1067  1.1.2.2  skrll 		return (((struct sockaddr_in *)(rt->rt_ifa->ifa_addr))->sin_addr);
   1068  1.1.2.2  skrll 	} else {
   1069  1.1.2.2  skrll 		memset(&ans, 0, sizeof(ans));
   1070  1.1.2.2  skrll 		return (ans);
   1071  1.1.2.2  skrll 	}
   1072  1.1.2.2  skrll }
   1073  1.1.2.2  skrll 
   1074  1.1.2.2  skrll 
   1075  1.1.2.2  skrll 
   1076  1.1.2.2  skrll /* tcb may be NULL */
   1077  1.1.2.2  skrll struct in_addr
   1078  1.1.2.2  skrll sctp_ipv4_source_address_selection(struct sctp_inpcb *inp,
   1079  1.1.2.2  skrll     struct sctp_tcb *stcb, struct route *ro, struct sctp_nets *net,
   1080  1.1.2.2  skrll     int non_asoc_addr_ok)
   1081  1.1.2.2  skrll {
   1082  1.1.2.2  skrll 	struct in_addr ans;
   1083  1.1.2.2  skrll 	const struct sockaddr_in *to;
   1084  1.1.2.2  skrll 	struct rtentry *rt;
   1085  1.1.2.2  skrll 	uint8_t ipv4_scope, loopscope;
   1086  1.1.2.2  skrll 	/*
   1087  1.1.2.2  skrll 	 * Rules:
   1088  1.1.2.2  skrll 	 * - Find the route if needed, cache if I can.
   1089  1.1.2.2  skrll 	 * - Look at interface address in route, Is it
   1090  1.1.2.2  skrll 	 *   in the bound list. If so we have the best source.
   1091  1.1.2.2  skrll 	 * - If not we must rotate amongst the addresses.
   1092  1.1.2.2  skrll 	 *
   1093  1.1.2.2  skrll 	 * Cavets and issues
   1094  1.1.2.2  skrll 	 *
   1095  1.1.2.2  skrll 	 * Do we need to pay attention to scope. We can have
   1096  1.1.2.2  skrll 	 * a private address or a global address we are sourcing
   1097  1.1.2.2  skrll 	 * or sending to. So if we draw it out
   1098  1.1.2.2  skrll 	 *      source     *      dest   *  result
   1099  1.1.2.2  skrll 	 *  ------------------------------------------
   1100  1.1.2.2  skrll 	 *  a   Private    *     Global  *  NAT?
   1101  1.1.2.2  skrll 	 *  ------------------------------------------
   1102  1.1.2.2  skrll 	 *  b   Private    *     Private *  No problem
   1103  1.1.2.2  skrll 	 *  ------------------------------------------
   1104  1.1.2.2  skrll 	 *  c   Global     *     Private *  Huh, How will this work?
   1105  1.1.2.2  skrll 	 *  ------------------------------------------
   1106  1.1.2.2  skrll 	 *  d   Global     *     Global  *  No Problem
   1107  1.1.2.2  skrll 	 *  ------------------------------------------
   1108  1.1.2.2  skrll 	 *
   1109  1.1.2.2  skrll 	 * And then we add to that what happens if there are multiple
   1110  1.1.2.2  skrll 	 * addresses assigned to an interface. Remember the ifa on a
   1111  1.1.2.2  skrll 	 * ifn is a linked list of addresses. So one interface can
   1112  1.1.2.2  skrll 	 * have more than one IPv4 address. What happens if we
   1113  1.1.2.2  skrll 	 * have both a private and a global address? Do we then
   1114  1.1.2.2  skrll 	 * use context of destination to sort out which one is
   1115  1.1.2.2  skrll 	 * best? And what about NAT's sending P->G may get you
   1116  1.1.2.2  skrll 	 * a NAT translation, or should you select the G thats
   1117  1.1.2.2  skrll 	 * on the interface in preference.
   1118  1.1.2.2  skrll 	 *
   1119  1.1.2.2  skrll 	 * Decisions:
   1120  1.1.2.2  skrll 	 *
   1121  1.1.2.2  skrll 	 *  - count the number of addresses on the interface.
   1122  1.1.2.2  skrll 	 *  - if its one, no problem except case <c>. For <a>
   1123  1.1.2.2  skrll 	 *    we will assume a NAT out there.
   1124  1.1.2.2  skrll 	 *  - if there are more than one, then we need to worry
   1125  1.1.2.2  skrll 	 *    about scope P or G. We should prefer G -> G and
   1126  1.1.2.2  skrll 	 *    P -> P if possible. Then as a secondary fall back
   1127  1.1.2.2  skrll 	 *    to mixed types G->P being a last ditch one.
   1128  1.1.2.2  skrll 	 *  - The above all works for bound all, but bound
   1129  1.1.2.2  skrll 	 *    specific we need to use the same concept but instead
   1130  1.1.2.2  skrll 	 *    only consider the bound addresses. If the bound set
   1131  1.1.2.2  skrll 	 *    is NOT assigned to the interface then we must use
   1132  1.1.2.2  skrll 	 *    rotation amongst them.
   1133  1.1.2.2  skrll 	 *
   1134  1.1.2.2  skrll 	 * Notes: For v4, we can always punt and let ip_output
   1135  1.1.2.2  skrll 	 * decide by sending back a source of 0.0.0.0
   1136  1.1.2.2  skrll 	 */
   1137  1.1.2.2  skrll 
   1138  1.1.2.2  skrll 	/*
   1139  1.1.2.2  skrll 	 * Need a route to cache.
   1140  1.1.2.2  skrll 	 *
   1141  1.1.2.2  skrll 	 */
   1142  1.1.2.2  skrll 	rt = rtcache_validate(ro);
   1143  1.1.2.2  skrll 	if (rt == NULL) {
   1144  1.1.2.2  skrll 		/* No route to host .. punt */
   1145  1.1.2.2  skrll 		memset(&ans, 0, sizeof(ans));
   1146  1.1.2.2  skrll 		return (ans);
   1147  1.1.2.2  skrll 	} else {
   1148  1.1.2.2  skrll 		to = satocsin(rtcache_getdst(ro));
   1149  1.1.2.2  skrll 	}
   1150  1.1.2.2  skrll 	/* Setup our scopes */
   1151  1.1.2.2  skrll 	if (stcb) {
   1152  1.1.2.2  skrll 		ipv4_scope = stcb->asoc.ipv4_local_scope;
   1153  1.1.2.2  skrll 		loopscope = stcb->asoc.loopback_scope;
   1154  1.1.2.2  skrll 	} else {
   1155  1.1.2.2  skrll 		/* Scope based on outbound address */
   1156  1.1.2.2  skrll 		if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
   1157  1.1.2.2  skrll 			ipv4_scope = 1;
   1158  1.1.2.2  skrll 			loopscope = 0;
   1159  1.1.2.2  skrll 		} else if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
   1160  1.1.2.2  skrll 			ipv4_scope = 1;
   1161  1.1.2.2  skrll 			loopscope = 1;
   1162  1.1.2.2  skrll 		} else {
   1163  1.1.2.2  skrll 			ipv4_scope = 0;
   1164  1.1.2.2  skrll 			loopscope = 0;
   1165  1.1.2.2  skrll 		}
   1166  1.1.2.2  skrll 	}
   1167  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1168  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1169  1.1.2.2  skrll 		printf("Scope setup loop:%d ipv4_scope:%d\n",
   1170  1.1.2.2  skrll 		       loopscope, ipv4_scope);
   1171  1.1.2.2  skrll 	}
   1172  1.1.2.2  skrll #endif
   1173  1.1.2.2  skrll 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   1174  1.1.2.2  skrll 		/*
   1175  1.1.2.2  skrll 		 * When bound to all if the address list is set
   1176  1.1.2.2  skrll 		 * it is a negative list. Addresses being added
   1177  1.1.2.2  skrll 		 * by asconf.
   1178  1.1.2.2  skrll 		 */
   1179  1.1.2.2  skrll 		return (sctp_choose_v4_boundall(inp, stcb, net, rt,
   1180  1.1.2.2  skrll 		    ipv4_scope, loopscope, non_asoc_addr_ok));
   1181  1.1.2.2  skrll         }
   1182  1.1.2.2  skrll 	/*
   1183  1.1.2.2  skrll  	 * Three possiblities here:
   1184  1.1.2.2  skrll 	 *
   1185  1.1.2.2  skrll 	 * a) stcb is NULL, which means we operate only from
   1186  1.1.2.2  skrll 	 *    the list of addresses (ifa's) bound to the assoc and
   1187  1.1.2.2  skrll 	 *    we care not about the list.
   1188  1.1.2.2  skrll 	 * b) stcb is NOT-NULL, which means we have an assoc structure and
   1189  1.1.2.2  skrll 	 *    auto-asconf is on. This means that the list of addresses is
   1190  1.1.2.2  skrll          *    a NOT list. We use the list from the inp, but any listed address
   1191  1.1.2.2  skrll 	 *    in our list is NOT yet added. However if the non_asoc_addr_ok is
   1192  1.1.2.2  skrll 	 *    set we CAN use an address NOT available (i.e. being added). Its
   1193  1.1.2.2  skrll 	 *    a negative list.
   1194  1.1.2.2  skrll 	 * c) stcb is NOT-NULL, which means we have an assoc structure and
   1195  1.1.2.2  skrll 	 *    auto-asconf is off. This means that the list of addresses is
   1196  1.1.2.2  skrll          *    the ONLY addresses I can use.. its positive.
   1197  1.1.2.2  skrll 	 *
   1198  1.1.2.2  skrll 	 *    Note we collapse b & c into the same function just like in
   1199  1.1.2.2  skrll 	 *    the v6 address selection.
   1200  1.1.2.2  skrll 	 */
   1201  1.1.2.2  skrll 	if (stcb) {
   1202  1.1.2.2  skrll 		return (sctp_choose_v4_boundspecific_stcb(inp, stcb, net,
   1203  1.1.2.2  skrll 		    rt, ipv4_scope, loopscope, non_asoc_addr_ok));
   1204  1.1.2.2  skrll 	} else {
   1205  1.1.2.2  skrll 		return (sctp_choose_v4_boundspecific_inp(inp, rt,
   1206  1.1.2.2  skrll 		    ipv4_scope, loopscope));
   1207  1.1.2.2  skrll 	}
   1208  1.1.2.2  skrll 	/* this should not be reached */
   1209  1.1.2.2  skrll 	memset(&ans, 0, sizeof(ans));
   1210  1.1.2.2  skrll 	return (ans);
   1211  1.1.2.2  skrll }
   1212  1.1.2.2  skrll 
   1213  1.1.2.2  skrll 
   1214  1.1.2.2  skrll 
   1215  1.1.2.2  skrll static struct sockaddr_in6 *
   1216  1.1.2.2  skrll sctp_is_v6_ifa_addr_acceptable (struct ifaddr *ifa, int loopscope, int loc_scope, int *sin_loop, int *sin_local)
   1217  1.1.2.2  skrll {
   1218  1.1.2.2  skrll 	struct in6_ifaddr *ifa6;
   1219  1.1.2.2  skrll 	struct sockaddr_in6 *sin6;
   1220  1.1.2.2  skrll 
   1221  1.1.2.2  skrll 	if (ifa->ifa_addr->sa_family != AF_INET6) {
   1222  1.1.2.2  skrll 		/* forget non-v6 */
   1223  1.1.2.2  skrll 		return (NULL);
   1224  1.1.2.2  skrll 	}
   1225  1.1.2.2  skrll 	ifa6 = (struct in6_ifaddr *)ifa;
   1226  1.1.2.2  skrll 	/* ok to use deprecated addresses? */
   1227  1.1.2.2  skrll 	if (!ip6_use_deprecated) {
   1228  1.1.2.2  skrll 		if (IFA6_IS_DEPRECATED(ifa6)) {
   1229  1.1.2.2  skrll 			/* can't use this type */
   1230  1.1.2.2  skrll 			return (NULL);
   1231  1.1.2.2  skrll 		}
   1232  1.1.2.2  skrll 	}
   1233  1.1.2.2  skrll 	/* are we ok, with the current state of this address? */
   1234  1.1.2.2  skrll 	if (ifa6->ia6_flags &
   1235  1.1.2.2  skrll 	    (IN6_IFF_DETACHED | IN6_IFF_NOTREADY | IN6_IFF_ANYCAST)) {
   1236  1.1.2.2  skrll 		/* Can't use these types */
   1237  1.1.2.2  skrll 		return (NULL);
   1238  1.1.2.2  skrll 	}
   1239  1.1.2.2  skrll 	/* Ok the address may be ok */
   1240  1.1.2.2  skrll 	sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
   1241  1.1.2.2  skrll 	*sin_local = *sin_loop = 0;
   1242  1.1.2.2  skrll 	if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
   1243  1.1.2.2  skrll 	    (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
   1244  1.1.2.2  skrll 		*sin_loop = 1;
   1245  1.1.2.2  skrll 	}
   1246  1.1.2.2  skrll 	if (!loopscope && *sin_loop) {
   1247  1.1.2.2  skrll 		/* Its a loopback address and we don't have loop scope */
   1248  1.1.2.2  skrll 		return (NULL);
   1249  1.1.2.2  skrll 	}
   1250  1.1.2.2  skrll 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   1251  1.1.2.2  skrll 		/* we skip unspecifed addresses */
   1252  1.1.2.2  skrll 		return (NULL);
   1253  1.1.2.2  skrll 	}
   1254  1.1.2.2  skrll 
   1255  1.1.2.2  skrll 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
   1256  1.1.2.2  skrll 		*sin_local = 1;
   1257  1.1.2.2  skrll 	}
   1258  1.1.2.2  skrll 	if (!loc_scope && *sin_local) {
   1259  1.1.2.2  skrll 		/* Its a link local address, and we don't have link local scope */
   1260  1.1.2.2  skrll 		return (NULL);
   1261  1.1.2.2  skrll 	}
   1262  1.1.2.2  skrll 	return (sin6);
   1263  1.1.2.2  skrll }
   1264  1.1.2.2  skrll 
   1265  1.1.2.2  skrll 
   1266  1.1.2.2  skrll static struct sockaddr_in6 *
   1267  1.1.2.2  skrll sctp_choose_v6_boundspecific_stcb(struct sctp_inpcb *inp,
   1268  1.1.2.2  skrll 				  struct sctp_tcb *stcb,
   1269  1.1.2.2  skrll 				  struct sctp_nets *net,
   1270  1.1.2.2  skrll 				  struct rtentry *rt,
   1271  1.1.2.2  skrll  			          uint8_t loc_scope,
   1272  1.1.2.2  skrll 				  uint8_t loopscope,
   1273  1.1.2.2  skrll 				  int non_asoc_addr_ok)
   1274  1.1.2.2  skrll {
   1275  1.1.2.2  skrll 	/*
   1276  1.1.2.2  skrll 	 *   Each endpoint has a list of local addresses associated
   1277  1.1.2.2  skrll 	 *   with it. The address list is either a "negative list" i.e.
   1278  1.1.2.2  skrll 	 *   those addresses that are NOT allowed to be used as a source OR
   1279  1.1.2.2  skrll 	 *   a "postive list" i.e. those addresses that CAN be used.
   1280  1.1.2.2  skrll 	 *
   1281  1.1.2.2  skrll 	 *   Its a negative list if asconf is allowed. What we do
   1282  1.1.2.2  skrll 	 *   in this case is use the ep address list BUT we have
   1283  1.1.2.2  skrll 	 *   to cross check it against the negative list.
   1284  1.1.2.2  skrll 	 *
   1285  1.1.2.2  skrll 	 *   In the case where NO asconf is allowed, we have just
   1286  1.1.2.2  skrll 	 *   a straight association level list that we must use to
   1287  1.1.2.2  skrll 	 *   find a source address.
   1288  1.1.2.2  skrll 	 */
   1289  1.1.2.2  skrll 	struct sctp_laddr *laddr, *starting_point;
   1290  1.1.2.2  skrll 	struct sockaddr_in6 *sin6;
   1291  1.1.2.2  skrll 	int sin_loop, sin_local;
   1292  1.1.2.2  skrll 	int start_at_beginning=0;
   1293  1.1.2.2  skrll 	struct ifnet *ifn;
   1294  1.1.2.2  skrll 	struct ifaddr *ifa;
   1295  1.1.2.2  skrll 
   1296  1.1.2.2  skrll 	ifn = rt->rt_ifp;
   1297  1.1.2.2  skrll 	if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
   1298  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1299  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1300  1.1.2.2  skrll 			printf("Have a STCB - asconf allowed, not bound all have a netgative list\n");
   1301  1.1.2.2  skrll 		}
   1302  1.1.2.2  skrll #endif
   1303  1.1.2.2  skrll 		/* first question, is the ifn we will emit on
   1304  1.1.2.2  skrll 		 * in our list, if so, we want that one.
   1305  1.1.2.2  skrll 		 */
   1306  1.1.2.2  skrll 		if (ifn) {
   1307  1.1.2.5  skrll 			IFADDR_READER_FOREACH(ifa, ifn) {
   1308  1.1.2.2  skrll 				if (sctp_is_addr_in_ep(inp, ifa)) {
   1309  1.1.2.2  skrll 					sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1310  1.1.2.2  skrll 					if (sin6 == NULL)
   1311  1.1.2.2  skrll 						continue;
   1312  1.1.2.2  skrll 					if ((non_asoc_addr_ok == 0) &&
   1313  1.1.2.2  skrll 					    (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
   1314  1.1.2.2  skrll 						/* on the no-no list */
   1315  1.1.2.2  skrll 						continue;
   1316  1.1.2.2  skrll 					}
   1317  1.1.2.2  skrll 					return (sin6);
   1318  1.1.2.2  skrll 				}
   1319  1.1.2.2  skrll 			}
   1320  1.1.2.2  skrll 		}
   1321  1.1.2.2  skrll 		starting_point = stcb->asoc.last_used_address;
   1322  1.1.2.2  skrll 		/* First try for matching scope */
   1323  1.1.2.2  skrll 	sctp_from_the_top:
   1324  1.1.2.2  skrll 		if (stcb->asoc.last_used_address == NULL) {
   1325  1.1.2.2  skrll 			start_at_beginning=1;
   1326  1.1.2.2  skrll 			stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
   1327  1.1.2.2  skrll 		}
   1328  1.1.2.2  skrll 		/* search beginning with the last used address */
   1329  1.1.2.2  skrll 		for (laddr = stcb->asoc.last_used_address; laddr;
   1330  1.1.2.2  skrll 		     laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
   1331  1.1.2.2  skrll 			if (laddr->ifa == NULL) {
   1332  1.1.2.2  skrll 				/* address has been removed */
   1333  1.1.2.2  skrll 				continue;
   1334  1.1.2.2  skrll 			}
   1335  1.1.2.2  skrll 			sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1336  1.1.2.2  skrll 			if (sin6 == NULL)
   1337  1.1.2.2  skrll 				continue;
   1338  1.1.2.2  skrll 			if ((non_asoc_addr_ok == 0) && (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
   1339  1.1.2.2  skrll 				/* on the no-no list */
   1340  1.1.2.2  skrll 				continue;
   1341  1.1.2.2  skrll 			}
   1342  1.1.2.2  skrll 			/* is it of matching scope ? */
   1343  1.1.2.2  skrll 			if ((loopscope == 0) &&
   1344  1.1.2.2  skrll 			    (loc_scope == 0) &&
   1345  1.1.2.2  skrll 			    (sin_loop == 0) &&
   1346  1.1.2.2  skrll 			    (sin_local == 0)) {
   1347  1.1.2.2  skrll 				/* all of global scope we are ok with it */
   1348  1.1.2.2  skrll 				return (sin6);
   1349  1.1.2.2  skrll 			}
   1350  1.1.2.2  skrll 			if (loopscope && sin_loop)
   1351  1.1.2.2  skrll 				/* both on the loopback, thats ok */
   1352  1.1.2.2  skrll 				return (sin6);
   1353  1.1.2.2  skrll 			if (loc_scope && sin_local)
   1354  1.1.2.2  skrll 				/* both local scope */
   1355  1.1.2.2  skrll 				return (sin6);
   1356  1.1.2.2  skrll 
   1357  1.1.2.2  skrll 		}
   1358  1.1.2.2  skrll 		if (start_at_beginning == 0) {
   1359  1.1.2.2  skrll 			stcb->asoc.last_used_address = NULL;
   1360  1.1.2.2  skrll 			goto sctp_from_the_top;
   1361  1.1.2.2  skrll 		}
   1362  1.1.2.2  skrll 		/* now try for any higher scope than the destination */
   1363  1.1.2.2  skrll 		stcb->asoc.last_used_address = starting_point;
   1364  1.1.2.2  skrll 		start_at_beginning = 0;
   1365  1.1.2.2  skrll 	sctp_from_the_top2:
   1366  1.1.2.2  skrll 		if (stcb->asoc.last_used_address == NULL) {
   1367  1.1.2.2  skrll 			start_at_beginning=1;
   1368  1.1.2.2  skrll 			stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
   1369  1.1.2.2  skrll 		}
   1370  1.1.2.2  skrll 		/* search beginning with the last used address */
   1371  1.1.2.2  skrll 		for (laddr = stcb->asoc.last_used_address; laddr;
   1372  1.1.2.2  skrll 		     laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
   1373  1.1.2.2  skrll 			if (laddr->ifa == NULL) {
   1374  1.1.2.2  skrll 				/* address has been removed */
   1375  1.1.2.2  skrll 				continue;
   1376  1.1.2.2  skrll 			}
   1377  1.1.2.2  skrll 			sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1378  1.1.2.2  skrll 			if (sin6 == NULL)
   1379  1.1.2.2  skrll 				continue;
   1380  1.1.2.2  skrll 			if ((non_asoc_addr_ok == 0) && (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
   1381  1.1.2.2  skrll 				/* on the no-no list */
   1382  1.1.2.2  skrll 				continue;
   1383  1.1.2.2  skrll 			}
   1384  1.1.2.2  skrll 			return (sin6);
   1385  1.1.2.2  skrll 		}
   1386  1.1.2.2  skrll 		if (start_at_beginning == 0) {
   1387  1.1.2.2  skrll 			stcb->asoc.last_used_address = NULL;
   1388  1.1.2.2  skrll 			goto sctp_from_the_top2;
   1389  1.1.2.2  skrll 		}
   1390  1.1.2.2  skrll 	} else {
   1391  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1392  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1393  1.1.2.2  skrll 			printf("Have a STCB - no asconf allowed, not bound all have a postive list\n");
   1394  1.1.2.2  skrll 		}
   1395  1.1.2.2  skrll #endif
   1396  1.1.2.2  skrll 		/* First try for interface output match */
   1397  1.1.2.2  skrll 		LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
   1398  1.1.2.2  skrll 			     sctp_nxt_addr) {
   1399  1.1.2.2  skrll 			if (laddr->ifa == NULL) {
   1400  1.1.2.2  skrll 				/* address has been removed */
   1401  1.1.2.2  skrll 				continue;
   1402  1.1.2.2  skrll 			}
   1403  1.1.2.2  skrll 			sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1404  1.1.2.2  skrll 			if (sin6 == NULL)
   1405  1.1.2.2  skrll 				continue;
   1406  1.1.2.2  skrll 			/* first question, is laddr->ifa an address associated with the emit interface */
   1407  1.1.2.2  skrll 			if (ifn) {
   1408  1.1.2.5  skrll 				IFADDR_READER_FOREACH(ifa, ifn) {
   1409  1.1.2.2  skrll 					if (laddr->ifa == ifa) {
   1410  1.1.2.2  skrll 						sin6 = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
   1411  1.1.2.2  skrll 						return (sin6);
   1412  1.1.2.2  skrll 					}
   1413  1.1.2.2  skrll 					if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
   1414  1.1.2.2  skrll 						sin6 = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
   1415  1.1.2.2  skrll 						return (sin6);
   1416  1.1.2.2  skrll 					}
   1417  1.1.2.2  skrll 				}
   1418  1.1.2.2  skrll 			}
   1419  1.1.2.2  skrll 		}
   1420  1.1.2.2  skrll 		/* Next try for matching scope */
   1421  1.1.2.2  skrll 		LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
   1422  1.1.2.2  skrll 			     sctp_nxt_addr) {
   1423  1.1.2.2  skrll 			if (laddr->ifa == NULL) {
   1424  1.1.2.2  skrll 				/* address has been removed */
   1425  1.1.2.2  skrll 				continue;
   1426  1.1.2.2  skrll 			}
   1427  1.1.2.2  skrll 			sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1428  1.1.2.2  skrll 			if (sin6 == NULL)
   1429  1.1.2.2  skrll 				continue;
   1430  1.1.2.2  skrll 
   1431  1.1.2.2  skrll 			if ((loopscope == 0) &&
   1432  1.1.2.2  skrll 			    (loc_scope == 0) &&
   1433  1.1.2.2  skrll 			    (sin_loop == 0) &&
   1434  1.1.2.2  skrll 			    (sin_local == 0)) {
   1435  1.1.2.2  skrll 				/* all of global scope we are ok with it */
   1436  1.1.2.2  skrll 				return (sin6);
   1437  1.1.2.2  skrll 			}
   1438  1.1.2.2  skrll 			if (loopscope && sin_loop)
   1439  1.1.2.2  skrll 				/* both on the loopback, thats ok */
   1440  1.1.2.2  skrll 				return (sin6);
   1441  1.1.2.2  skrll 			if (loc_scope && sin_local)
   1442  1.1.2.2  skrll 				/* both local scope */
   1443  1.1.2.2  skrll 				return (sin6);
   1444  1.1.2.2  skrll 		}
   1445  1.1.2.2  skrll 		/* ok, now try for a higher scope in the source address */
   1446  1.1.2.2  skrll 		/* First try for matching scope */
   1447  1.1.2.2  skrll 		LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
   1448  1.1.2.2  skrll 			     sctp_nxt_addr) {
   1449  1.1.2.2  skrll 			if (laddr->ifa == NULL) {
   1450  1.1.2.2  skrll 				/* address has been removed */
   1451  1.1.2.2  skrll 				continue;
   1452  1.1.2.2  skrll 			}
   1453  1.1.2.2  skrll 			sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1454  1.1.2.2  skrll 			if (sin6 == NULL)
   1455  1.1.2.2  skrll 				continue;
   1456  1.1.2.2  skrll 			return (sin6);
   1457  1.1.2.2  skrll 		}
   1458  1.1.2.2  skrll 	}
   1459  1.1.2.2  skrll 	return (NULL);
   1460  1.1.2.2  skrll }
   1461  1.1.2.2  skrll 
   1462  1.1.2.2  skrll static struct sockaddr_in6 *
   1463  1.1.2.2  skrll sctp_choose_v6_boundspecific_inp(struct sctp_inpcb *inp,
   1464  1.1.2.2  skrll 				 struct rtentry *rt,
   1465  1.1.2.2  skrll 				 uint8_t loc_scope,
   1466  1.1.2.2  skrll 				 uint8_t loopscope)
   1467  1.1.2.2  skrll {
   1468  1.1.2.2  skrll 	/*
   1469  1.1.2.2  skrll 	 * Here we are bound specific and have only
   1470  1.1.2.2  skrll 	 * an inp. We must find an address that is bound
   1471  1.1.2.2  skrll 	 * that we can give out as a src address. We
   1472  1.1.2.2  skrll 	 * prefer two addresses of same scope if we can
   1473  1.1.2.2  skrll 	 * find them that way.
   1474  1.1.2.2  skrll 	 */
   1475  1.1.2.2  skrll 	struct sctp_laddr *laddr;
   1476  1.1.2.2  skrll 	struct sockaddr_in6 *sin6;
   1477  1.1.2.2  skrll 	struct ifnet *ifn;
   1478  1.1.2.2  skrll 	struct ifaddr *ifa;
   1479  1.1.2.2  skrll 	int sin_loop, sin_local;
   1480  1.1.2.2  skrll 
   1481  1.1.2.2  skrll 	/* first question, is the ifn we will emit on
   1482  1.1.2.2  skrll 	 * in our list, if so, we want that one.
   1483  1.1.2.2  skrll 	 */
   1484  1.1.2.2  skrll 
   1485  1.1.2.2  skrll 	ifn = rt->rt_ifp;
   1486  1.1.2.2  skrll 	if (ifn) {
   1487  1.1.2.5  skrll 		IFADDR_READER_FOREACH(ifa, ifn) {
   1488  1.1.2.2  skrll 			sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1489  1.1.2.2  skrll 			if (sin6 == NULL)
   1490  1.1.2.2  skrll 				continue;
   1491  1.1.2.2  skrll 			if (sctp_is_addr_in_ep(inp, ifa)) {
   1492  1.1.2.2  skrll 				return (sin6);
   1493  1.1.2.2  skrll 			}
   1494  1.1.2.2  skrll 		}
   1495  1.1.2.2  skrll 	}
   1496  1.1.2.2  skrll 	for (laddr = LIST_FIRST(&inp->sctp_addr_list);
   1497  1.1.2.2  skrll 	     laddr && (laddr != inp->next_addr_touse);
   1498  1.1.2.2  skrll 	     laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
   1499  1.1.2.2  skrll 		if (laddr->ifa == NULL) {
   1500  1.1.2.2  skrll 			/* address has been removed */
   1501  1.1.2.2  skrll 			continue;
   1502  1.1.2.2  skrll 		}
   1503  1.1.2.2  skrll 		sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1504  1.1.2.2  skrll 		if (sin6 == NULL)
   1505  1.1.2.2  skrll 			continue;
   1506  1.1.2.2  skrll 
   1507  1.1.2.2  skrll 		if ((loopscope == 0) &&
   1508  1.1.2.2  skrll 		    (loc_scope == 0) &&
   1509  1.1.2.2  skrll 		    (sin_loop == 0) &&
   1510  1.1.2.2  skrll 		    (sin_local == 0)) {
   1511  1.1.2.2  skrll 			/* all of global scope we are ok with it */
   1512  1.1.2.2  skrll 			return (sin6);
   1513  1.1.2.2  skrll 		}
   1514  1.1.2.2  skrll 		if (loopscope && sin_loop)
   1515  1.1.2.2  skrll 			/* both on the loopback, thats ok */
   1516  1.1.2.2  skrll 			return (sin6);
   1517  1.1.2.2  skrll 		if (loc_scope && sin_local)
   1518  1.1.2.2  skrll 			/* both local scope */
   1519  1.1.2.2  skrll 			return (sin6);
   1520  1.1.2.2  skrll 
   1521  1.1.2.2  skrll 	}
   1522  1.1.2.2  skrll 	/* if we reach here, we could not find two addresses
   1523  1.1.2.2  skrll 	 * of the same scope to give out. Lets look for any higher level
   1524  1.1.2.2  skrll 	 * scope for a source address.
   1525  1.1.2.2  skrll 	 */
   1526  1.1.2.2  skrll 	for (laddr = LIST_FIRST(&inp->sctp_addr_list);
   1527  1.1.2.2  skrll 	     laddr && (laddr != inp->next_addr_touse);
   1528  1.1.2.2  skrll 	     laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
   1529  1.1.2.2  skrll 		if (laddr->ifa == NULL) {
   1530  1.1.2.2  skrll 			/* address has been removed */
   1531  1.1.2.2  skrll 			continue;
   1532  1.1.2.2  skrll 		}
   1533  1.1.2.2  skrll 		sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1534  1.1.2.2  skrll 		if (sin6 == NULL)
   1535  1.1.2.2  skrll 			continue;
   1536  1.1.2.2  skrll 		return (sin6);
   1537  1.1.2.2  skrll 	}
   1538  1.1.2.2  skrll 	/* no address bound can be a source for the destination */
   1539  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1540  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1541  1.1.2.2  skrll 		printf("Src address selection for EP, no acceptable src address found for address\n");
   1542  1.1.2.2  skrll 	}
   1543  1.1.2.2  skrll #endif
   1544  1.1.2.2  skrll 	return (NULL);
   1545  1.1.2.2  skrll }
   1546  1.1.2.2  skrll 
   1547  1.1.2.2  skrll 
   1548  1.1.2.2  skrll static struct sockaddr_in6 *
   1549  1.1.2.2  skrll sctp_select_v6_nth_addr_from_ifn_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok, uint8_t loopscope,
   1550  1.1.2.2  skrll 					   uint8_t loc_scope, int cur_addr_num, int match_scope)
   1551  1.1.2.2  skrll {
   1552  1.1.2.2  skrll 	struct ifaddr *ifa;
   1553  1.1.2.2  skrll 	struct sockaddr_in6 *sin6;
   1554  1.1.2.2  skrll 	int sin_loop, sin_local;
   1555  1.1.2.2  skrll 	int num_eligible_addr = 0;
   1556  1.1.2.2  skrll 
   1557  1.1.2.5  skrll 	IFADDR_READER_FOREACH(ifa, ifn) {
   1558  1.1.2.2  skrll 		sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1559  1.1.2.2  skrll 		if (sin6 == NULL)
   1560  1.1.2.2  skrll 			continue;
   1561  1.1.2.2  skrll 		if (stcb) {
   1562  1.1.2.2  skrll 			if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6)) {
   1563  1.1.2.2  skrll 				/* It is restricted for some reason.. probably
   1564  1.1.2.2  skrll 				 * not yet added.
   1565  1.1.2.2  skrll 				 */
   1566  1.1.2.2  skrll 				continue;
   1567  1.1.2.2  skrll 			}
   1568  1.1.2.2  skrll 		}
   1569  1.1.2.2  skrll 		if (match_scope) {
   1570  1.1.2.2  skrll 			/* Here we are asked to match scope if possible */
   1571  1.1.2.2  skrll 			if (loopscope && sin_loop)
   1572  1.1.2.2  skrll 				/* src and destination are loopback scope */
   1573  1.1.2.2  skrll 				return (sin6);
   1574  1.1.2.2  skrll 			if (loc_scope && sin_local)
   1575  1.1.2.2  skrll 				/* src and destination are local scope */
   1576  1.1.2.2  skrll 				return (sin6);
   1577  1.1.2.2  skrll 			if ((loopscope == 0) &&
   1578  1.1.2.2  skrll 			    (loc_scope == 0)  &&
   1579  1.1.2.2  skrll 			    (sin_loop == 0) &&
   1580  1.1.2.2  skrll 			    (sin_local == 0)) {
   1581  1.1.2.2  skrll 				/* src and destination are global scope */
   1582  1.1.2.2  skrll 				return (sin6);
   1583  1.1.2.2  skrll 			}
   1584  1.1.2.2  skrll 			continue;
   1585  1.1.2.2  skrll 		}
   1586  1.1.2.2  skrll 		if (num_eligible_addr == cur_addr_num) {
   1587  1.1.2.2  skrll 			/* this is it */
   1588  1.1.2.2  skrll 			return (sin6);
   1589  1.1.2.2  skrll 		}
   1590  1.1.2.2  skrll 		num_eligible_addr++;
   1591  1.1.2.2  skrll 	}
   1592  1.1.2.2  skrll 	return (NULL);
   1593  1.1.2.2  skrll }
   1594  1.1.2.2  skrll 
   1595  1.1.2.2  skrll 
   1596  1.1.2.2  skrll static int
   1597  1.1.2.2  skrll sctp_count_v6_num_eligible_boundall (struct ifnet *ifn, struct sctp_tcb *stcb,
   1598  1.1.2.2  skrll 				     int non_asoc_addr_ok, uint8_t loopscope, uint8_t loc_scope)
   1599  1.1.2.2  skrll {
   1600  1.1.2.2  skrll 	struct ifaddr *ifa;
   1601  1.1.2.2  skrll 	struct sockaddr_in6 *sin6;
   1602  1.1.2.2  skrll 	int num_eligible_addr = 0;
   1603  1.1.2.2  skrll 	int sin_loop, sin_local;
   1604  1.1.2.2  skrll 
   1605  1.1.2.5  skrll 	IFADDR_READER_FOREACH(ifa, ifn) {
   1606  1.1.2.2  skrll 		sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1607  1.1.2.2  skrll 		if (sin6 == NULL)
   1608  1.1.2.2  skrll 			continue;
   1609  1.1.2.2  skrll 		if (stcb) {
   1610  1.1.2.2  skrll 			if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6)) {
   1611  1.1.2.2  skrll 				/* It is restricted for some reason.. probably
   1612  1.1.2.2  skrll 				 * not yet added.
   1613  1.1.2.2  skrll 				 */
   1614  1.1.2.2  skrll 				continue;
   1615  1.1.2.2  skrll 			}
   1616  1.1.2.2  skrll 		}
   1617  1.1.2.2  skrll 		num_eligible_addr++;
   1618  1.1.2.2  skrll 	}
   1619  1.1.2.2  skrll 	return (num_eligible_addr);
   1620  1.1.2.2  skrll }
   1621  1.1.2.2  skrll 
   1622  1.1.2.2  skrll 
   1623  1.1.2.2  skrll static struct sockaddr_in6 *
   1624  1.1.2.2  skrll sctp_choose_v6_boundall(struct sctp_inpcb *inp,
   1625  1.1.2.2  skrll 			struct sctp_tcb *stcb,
   1626  1.1.2.2  skrll 			struct sctp_nets *net,
   1627  1.1.2.2  skrll 			struct rtentry *rt,
   1628  1.1.2.2  skrll 			uint8_t loc_scope,
   1629  1.1.2.2  skrll 			uint8_t loopscope,
   1630  1.1.2.2  skrll 			int non_asoc_addr_ok)
   1631  1.1.2.2  skrll {
   1632  1.1.2.2  skrll 	/* Ok, we are bound all SO any address
   1633  1.1.2.2  skrll 	 * is ok to use as long as it is NOT in the negative
   1634  1.1.2.2  skrll 	 * list.
   1635  1.1.2.2  skrll 	 */
   1636  1.1.2.2  skrll 	int num_eligible_addr;
   1637  1.1.2.2  skrll 	int cur_addr_num=0;
   1638  1.1.2.2  skrll 	int started_at_beginning=0;
   1639  1.1.2.2  skrll 	int match_scope_prefered;
   1640  1.1.2.2  skrll 	/* first question is, how many eligible addresses are
   1641  1.1.2.2  skrll 	 * there for the destination ifn that we are using that
   1642  1.1.2.2  skrll 	 * are within the proper scope?
   1643  1.1.2.2  skrll 	 */
   1644  1.1.2.2  skrll 	struct ifnet *ifn;
   1645  1.1.2.2  skrll 	struct sockaddr_in6 *sin6;
   1646  1.1.2.4  skrll 	int s;
   1647  1.1.2.2  skrll 
   1648  1.1.2.2  skrll 	ifn = rt->rt_ifp;
   1649  1.1.2.2  skrll 	if (net) {
   1650  1.1.2.2  skrll 		cur_addr_num = net->indx_of_eligible_next_to_use;
   1651  1.1.2.2  skrll 	}
   1652  1.1.2.2  skrll 	if (cur_addr_num == 0) {
   1653  1.1.2.2  skrll 		match_scope_prefered = 1;
   1654  1.1.2.2  skrll 	} else {
   1655  1.1.2.2  skrll 		match_scope_prefered = 0;
   1656  1.1.2.2  skrll 	}
   1657  1.1.2.2  skrll 	num_eligible_addr = sctp_count_v6_num_eligible_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope);
   1658  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1659  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1660  1.1.2.2  skrll 		printf("Found %d eligible source addresses\n", num_eligible_addr);
   1661  1.1.2.2  skrll 	}
   1662  1.1.2.2  skrll #endif
   1663  1.1.2.2  skrll 	if (num_eligible_addr == 0) {
   1664  1.1.2.2  skrll 		/* no eligible addresses, we must use some other
   1665  1.1.2.2  skrll 		 * interface address if we can find one.
   1666  1.1.2.2  skrll 		 */
   1667  1.1.2.2  skrll  		goto bound_all_v6_plan_b;
   1668  1.1.2.2  skrll 	}
   1669  1.1.2.2  skrll 	/* Ok we have num_eligible_addr set with how many we can use,
   1670  1.1.2.2  skrll 	 * this may vary from call to call due to addresses being deprecated etc..
   1671  1.1.2.2  skrll 	 */
   1672  1.1.2.2  skrll 	if (cur_addr_num >= num_eligible_addr) {
   1673  1.1.2.2  skrll 		cur_addr_num = 0;
   1674  1.1.2.2  skrll 	}
   1675  1.1.2.2  skrll 	/* select the nth address from the list (where cur_addr_num is the nth) and
   1676  1.1.2.2  skrll 	 * 0 is the first one, 1 is the second one etc...
   1677  1.1.2.2  skrll 	 */
   1678  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1679  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1680  1.1.2.2  skrll 		printf("cur_addr_num:%d match_scope_prefered:%d select it\n",
   1681  1.1.2.2  skrll 		       cur_addr_num, match_scope_prefered);
   1682  1.1.2.2  skrll 	}
   1683  1.1.2.2  skrll #endif
   1684  1.1.2.2  skrll 	sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
   1685  1.1.2.2  skrll 							  loc_scope, cur_addr_num, match_scope_prefered);
   1686  1.1.2.2  skrll 	if (match_scope_prefered && (sin6 == NULL)) {
   1687  1.1.2.2  skrll 		/* retry without the preference for matching scope */
   1688  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1689  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1690  1.1.2.2  skrll 		printf("retry with no match_scope_prefered\n");
   1691  1.1.2.2  skrll 	}
   1692  1.1.2.2  skrll #endif
   1693  1.1.2.2  skrll 		sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
   1694  1.1.2.2  skrll 								  loc_scope, cur_addr_num, 0);
   1695  1.1.2.2  skrll 	}
   1696  1.1.2.2  skrll 	if (sin6) {
   1697  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1698  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1699  1.1.2.2  skrll 			printf("Selected address %d ifn:%p for the route\n", cur_addr_num, ifn);
   1700  1.1.2.2  skrll 		}
   1701  1.1.2.2  skrll #endif
   1702  1.1.2.2  skrll 		if (net) {
   1703  1.1.2.2  skrll 			/* store so we get the next one */
   1704  1.1.2.2  skrll 			if (cur_addr_num < 255)
   1705  1.1.2.2  skrll 				net->indx_of_eligible_next_to_use = cur_addr_num + 1;
   1706  1.1.2.2  skrll 			else
   1707  1.1.2.2  skrll 				net->indx_of_eligible_next_to_use = 0;
   1708  1.1.2.2  skrll 		}
   1709  1.1.2.2  skrll 		return (sin6);
   1710  1.1.2.2  skrll 	}
   1711  1.1.2.2  skrll 	num_eligible_addr = 0;
   1712  1.1.2.2  skrll  bound_all_v6_plan_b:
   1713  1.1.2.2  skrll 	/* ok, if we reach here we either fell through
   1714  1.1.2.2  skrll 	 * due to something changing during an interupt (unlikely)
   1715  1.1.2.2  skrll 	 * or we have NO eligible source addresses for the ifn
   1716  1.1.2.2  skrll 	 * of the route (most likely). We must look at all the other
   1717  1.1.2.2  skrll 	 * interfaces EXCEPT rt->rt_ifp and do the same game.
   1718  1.1.2.2  skrll 	 */
   1719  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1720  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1721  1.1.2.2  skrll 		printf("bound-all Plan B\n");
   1722  1.1.2.2  skrll 	}
   1723  1.1.2.2  skrll #endif
   1724  1.1.2.2  skrll 	if (inp->next_ifn_touse == NULL) {
   1725  1.1.2.2  skrll 		started_at_beginning=1;
   1726  1.1.2.4  skrll 		inp->next_ifn_touse = IFNET_READER_FIRST();
   1727  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1728  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1729  1.1.2.2  skrll 			printf("Start at first IFN:%p\n", inp->next_ifn_touse);
   1730  1.1.2.2  skrll 		}
   1731  1.1.2.2  skrll #endif
   1732  1.1.2.2  skrll 	} else {
   1733  1.1.2.4  skrll 		inp->next_ifn_touse = IFNET_READER_NEXT(inp->next_ifn_touse);
   1734  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1735  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1736  1.1.2.2  skrll 			printf("Resume at IFN:%p\n", inp->next_ifn_touse);
   1737  1.1.2.2  skrll 		}
   1738  1.1.2.2  skrll #endif
   1739  1.1.2.2  skrll 		if (inp->next_ifn_touse == NULL) {
   1740  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1741  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1742  1.1.2.2  skrll 				printf("IFN Resets\n");
   1743  1.1.2.2  skrll 			}
   1744  1.1.2.2  skrll #endif
   1745  1.1.2.2  skrll 			started_at_beginning=1;
   1746  1.1.2.4  skrll 			inp->next_ifn_touse = IFNET_READER_FIRST();
   1747  1.1.2.2  skrll 		}
   1748  1.1.2.2  skrll 	}
   1749  1.1.2.4  skrll 
   1750  1.1.2.4  skrll 	s = pserialize_read_enter();
   1751  1.1.2.4  skrll 	IFNET_READER_FOREACH(ifn) {
   1752  1.1.2.2  skrll 		if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
   1753  1.1.2.2  skrll 			/* wrong base scope */
   1754  1.1.2.2  skrll 			continue;
   1755  1.1.2.2  skrll 		}
   1756  1.1.2.2  skrll 		if (loc_scope && (ifn->if_index != loc_scope)) {
   1757  1.1.2.2  skrll 			/* by definition the scope (from to->sin6_scopeid)
   1758  1.1.2.2  skrll 			 * must match that of the interface. If not then
   1759  1.1.2.2  skrll 			 * we could pick a wrong scope for the address.
   1760  1.1.2.2  skrll 			 * Ususally we don't hit plan-b since the route
   1761  1.1.2.2  skrll 			 * handles this. However we can hit plan-b when
   1762  1.1.2.2  skrll 			 * we send to local-host so the route is the
   1763  1.1.2.2  skrll 			 * loopback interface, but the destination is a
   1764  1.1.2.2  skrll 			 * link local.
   1765  1.1.2.2  skrll 			 */
   1766  1.1.2.2  skrll 			continue;
   1767  1.1.2.2  skrll 		}
   1768  1.1.2.2  skrll 		if (ifn == rt->rt_ifp) {
   1769  1.1.2.2  skrll 			/* already looked at this guy */
   1770  1.1.2.2  skrll 			continue;
   1771  1.1.2.2  skrll 		}
   1772  1.1.2.2  skrll 		/* Address rotation will only work when we are not
   1773  1.1.2.2  skrll 		 * rotating sourced interfaces and are using the interface
   1774  1.1.2.2  skrll 		 * of the route. We would need to have a per interface index
   1775  1.1.2.2  skrll 		 * in order to do proper rotation.
   1776  1.1.2.2  skrll 		 */
   1777  1.1.2.2  skrll 		num_eligible_addr = sctp_count_v6_num_eligible_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope);
   1778  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1779  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1780  1.1.2.2  skrll 			printf("IFN:%p has %d eligible\n", ifn, num_eligible_addr);
   1781  1.1.2.2  skrll 		}
   1782  1.1.2.2  skrll #endif
   1783  1.1.2.2  skrll 		if (num_eligible_addr == 0) {
   1784  1.1.2.2  skrll 			/* none we can use */
   1785  1.1.2.2  skrll 			continue;
   1786  1.1.2.2  skrll 		}
   1787  1.1.2.2  skrll 		/* Ok we have num_eligible_addr set with how many we can use,
   1788  1.1.2.2  skrll 		 * this may vary from call to call due to addresses being deprecated etc..
   1789  1.1.2.2  skrll 		 */
   1790  1.1.2.2  skrll 		inp->next_ifn_touse = ifn;
   1791  1.1.2.2  skrll 
   1792  1.1.2.2  skrll 		/* select the first one we can find with perference for matching scope.
   1793  1.1.2.2  skrll 		 */
   1794  1.1.2.2  skrll 		sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope, 0, 1);
   1795  1.1.2.2  skrll 		if (sin6 == NULL) {
   1796  1.1.2.2  skrll 			/* can't find one with matching scope how about a source with higher
   1797  1.1.2.2  skrll 			 * scope
   1798  1.1.2.2  skrll 			 */
   1799  1.1.2.2  skrll  			sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope, 0, 0);
   1800  1.1.2.2  skrll 			if (sin6 == NULL)
   1801  1.1.2.2  skrll 				/* Hmm, can't find one in the interface now */
   1802  1.1.2.2  skrll 				continue;
   1803  1.1.2.2  skrll 		}
   1804  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1805  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1806  1.1.2.2  skrll 			printf("Selected the %d'th address of ifn:%p\n",
   1807  1.1.2.2  skrll 			       cur_addr_num, ifn);
   1808  1.1.2.2  skrll 		}
   1809  1.1.2.2  skrll #endif
   1810  1.1.2.4  skrll 		pserialize_read_exit(s);
   1811  1.1.2.2  skrll 		return (sin6);
   1812  1.1.2.2  skrll 	}
   1813  1.1.2.4  skrll 	pserialize_read_exit(s);
   1814  1.1.2.4  skrll 
   1815  1.1.2.2  skrll 	if (started_at_beginning == 0) {
   1816  1.1.2.2  skrll 		/* we have not been through all of them yet, force
   1817  1.1.2.2  skrll 		 * us to go through them all.
   1818  1.1.2.2  skrll 		 */
   1819  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1820  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1821  1.1.2.2  skrll 			printf("Force a recycle\n");
   1822  1.1.2.2  skrll 		}
   1823  1.1.2.2  skrll #endif
   1824  1.1.2.2  skrll 		inp->next_ifn_touse = NULL;
   1825  1.1.2.2  skrll 		goto bound_all_v6_plan_b;
   1826  1.1.2.2  skrll 	}
   1827  1.1.2.2  skrll 	return (NULL);
   1828  1.1.2.2  skrll 
   1829  1.1.2.2  skrll }
   1830  1.1.2.2  skrll 
   1831  1.1.2.2  skrll /* stcb and net may be NULL */
   1832  1.1.2.2  skrll struct in6_addr
   1833  1.1.2.2  skrll sctp_ipv6_source_address_selection(struct sctp_inpcb *inp,
   1834  1.1.2.2  skrll     struct sctp_tcb *stcb, struct route *ro, struct sctp_nets *net,
   1835  1.1.2.2  skrll     int non_asoc_addr_ok)
   1836  1.1.2.2  skrll {
   1837  1.1.2.2  skrll 	struct in6_addr ans;
   1838  1.1.2.2  skrll 	struct sockaddr_in6 *rt_addr;
   1839  1.1.2.2  skrll 	uint8_t loc_scope, loopscope;
   1840  1.1.2.2  skrll 	struct sockaddr_in6 to;
   1841  1.1.2.2  skrll 	struct rtentry *rt;
   1842  1.1.2.2  skrll 
   1843  1.1.2.2  skrll 	/*
   1844  1.1.2.2  skrll 	 * This routine is tricky standard v6 src address
   1845  1.1.2.2  skrll 	 * selection cannot take into account what we have
   1846  1.1.2.2  skrll 	 * bound etc, so we can't use it.
   1847  1.1.2.2  skrll 	 *
   1848  1.1.2.2  skrll 	 * Instead here is what we must do:
   1849  1.1.2.2  skrll 	 * 1) Make sure we have a route, if we
   1850  1.1.2.2  skrll 	 *    don't have a route we can never reach the peer.
   1851  1.1.2.2  skrll 	 * 2) Once we have a route, determine the scope of the
   1852  1.1.2.2  skrll 	 *     route. Link local, loopback or global.
   1853  1.1.2.2  skrll 	 * 3) Next we divide into three types. Either we
   1854  1.1.2.2  skrll 	 *    are bound all.. which means we want to use
   1855  1.1.2.2  skrll 	 *    one of the addresses of the interface we are
   1856  1.1.2.2  skrll 	 *    going out. <or>
   1857  1.1.2.2  skrll 	 * 4a) We have not stcb, which means we are using the
   1858  1.1.2.2  skrll 	 *    specific addresses bound on an inp, in this
   1859  1.1.2.2  skrll 	 *    case we are similar to the stcb case (4b below)
   1860  1.1.2.2  skrll 	 *    accept the list is always a positive list.<or>
   1861  1.1.2.2  skrll 	 * 4b) We are bound specific with a stcb, which means we have a
   1862  1.1.2.2  skrll 	 *    list of bound addresses and we must see if the
   1863  1.1.2.2  skrll 	 *    ifn of the route is actually one of the bound addresses.
   1864  1.1.2.2  skrll 	 *    If not, then we must rotate addresses amongst properly
   1865  1.1.2.2  skrll 	 *    scoped bound addresses, if so we use the address
   1866  1.1.2.2  skrll 	 *    of the interface.
   1867  1.1.2.2  skrll 	 * 5) Always, no matter which path we take through the above
   1868  1.1.2.2  skrll 	 *    we must be sure the source address we use is allowed to
   1869  1.1.2.2  skrll 	 *    be used. I.e.  IN6_IFF_DETACHED, IN6_IFF_NOTREADY, and IN6_IFF_ANYCAST
   1870  1.1.2.2  skrll 	 *    addresses cannot be used.
   1871  1.1.2.2  skrll 	 * 6) Addresses that are deprecated MAY be used
   1872  1.1.2.2  skrll 	 * 		if (!ip6_use_deprecated) {
   1873  1.1.2.2  skrll 	 *                    if (IFA6_IS_DEPRECATED(ifa6)) {
   1874  1.1.2.2  skrll 	 *	                  skip the address
   1875  1.1.2.2  skrll   	 *	              }
   1876  1.1.2.2  skrll 	 *	        }
   1877  1.1.2.2  skrll  	 */
   1878  1.1.2.2  skrll 
   1879  1.1.2.2  skrll 	/*** 1> determine route, if not already done */
   1880  1.1.2.2  skrll 	rt = rtcache_validate(ro);
   1881  1.1.2.2  skrll 	if (rt == NULL) {
   1882  1.1.2.2  skrll 		/*
   1883  1.1.2.2  skrll 		 * Need a route to cache.
   1884  1.1.2.2  skrll 		 */
   1885  1.1.2.2  skrll 		int scope_save;
   1886  1.1.2.2  skrll 
   1887  1.1.2.2  skrll 		memcpy(&to, rtcache_getdst(ro), sizeof(struct sockaddr));
   1888  1.1.2.2  skrll 		scope_save = to.sin6_scope_id;
   1889  1.1.2.2  skrll 		to.sin6_scope_id = 0;
   1890  1.1.2.2  skrll 
   1891  1.1.2.2  skrll 		rt = rtcache_lookup(ro, (struct sockaddr *)&to);
   1892  1.1.2.2  skrll 		to.sin6_scope_id = scope_save;
   1893  1.1.2.2  skrll 	}
   1894  1.1.2.2  skrll 	if (rt == NULL) {
   1895  1.1.2.2  skrll 		/*
   1896  1.1.2.2  skrll 		 * no route to host. this packet is going no-where.
   1897  1.1.2.2  skrll 		 * We probably should make sure we arrange to send back
   1898  1.1.2.2  skrll 		 * an error.
   1899  1.1.2.2  skrll 		 */
   1900  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1901  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1902  1.1.2.2  skrll 			printf("No route to host, this packet cannot be sent!\n");
   1903  1.1.2.2  skrll 		}
   1904  1.1.2.2  skrll #endif
   1905  1.1.2.2  skrll 		memset(&ans, 0, sizeof(ans));
   1906  1.1.2.2  skrll 		return (ans);
   1907  1.1.2.2  skrll 	}
   1908  1.1.2.2  skrll 
   1909  1.1.2.2  skrll 	/*** 2a> determine scope for outbound address/route */
   1910  1.1.2.2  skrll 	loc_scope = loopscope = 0;
   1911  1.1.2.2  skrll 	/*
   1912  1.1.2.2  skrll 	 * We base our scope on the outbound packet scope and route,
   1913  1.1.2.2  skrll 	 * NOT the TCB (if there is one). This way in local scope we will only
   1914  1.1.2.2  skrll 	 * use a local scope src address when we send to a local address.
   1915  1.1.2.2  skrll 	 */
   1916  1.1.2.2  skrll 
   1917  1.1.2.2  skrll 	if (IN6_IS_ADDR_LOOPBACK(&to.sin6_addr)) {
   1918  1.1.2.2  skrll 		/* If the route goes to the loopback address OR
   1919  1.1.2.2  skrll 		 * the address is a loopback address, we are loopback
   1920  1.1.2.2  skrll 		 * scope.
   1921  1.1.2.2  skrll 		 */
   1922  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1923  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1924  1.1.2.2  skrll 			printf("Loopback scope is set\n");
   1925  1.1.2.2  skrll 		}
   1926  1.1.2.2  skrll #endif
   1927  1.1.2.2  skrll 		loc_scope = 0;
   1928  1.1.2.2  skrll 		loopscope = 1;
   1929  1.1.2.2  skrll 		if (net != NULL) {
   1930  1.1.2.2  skrll 			/* mark it as local */
   1931  1.1.2.2  skrll 			net->addr_is_local = 1;
   1932  1.1.2.2  skrll 		}
   1933  1.1.2.2  skrll 
   1934  1.1.2.2  skrll 	} else if (IN6_IS_ADDR_LINKLOCAL(&to.sin6_addr)) {
   1935  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1936  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1937  1.1.2.2  skrll 			printf("Link local scope is set, id:%d\n", to.sin6_scope_id);
   1938  1.1.2.2  skrll 		}
   1939  1.1.2.2  skrll #endif
   1940  1.1.2.2  skrll 		if (to.sin6_scope_id)
   1941  1.1.2.2  skrll 			loc_scope = to.sin6_scope_id;
   1942  1.1.2.2  skrll 		else {
   1943  1.1.2.2  skrll 			loc_scope = 1;
   1944  1.1.2.2  skrll 		}
   1945  1.1.2.2  skrll 		loopscope = 0;
   1946  1.1.2.2  skrll 	} else {
   1947  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1948  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1949  1.1.2.2  skrll 			printf("Global scope is set\n");
   1950  1.1.2.2  skrll 		}
   1951  1.1.2.2  skrll #endif
   1952  1.1.2.2  skrll 	}
   1953  1.1.2.2  skrll 
   1954  1.1.2.2  skrll 	/* now, depending on which way we are bound we call the appropriate
   1955  1.1.2.2  skrll 	 * routine to do steps 3-6
   1956  1.1.2.2  skrll 	 */
   1957  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1958  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1959  1.1.2.2  skrll 		printf("Destination address:");
   1960  1.1.2.2  skrll 		sctp_print_address((struct sockaddr *)&to);
   1961  1.1.2.2  skrll 	}
   1962  1.1.2.2  skrll #endif
   1963  1.1.2.2  skrll 
   1964  1.1.2.2  skrll 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   1965  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1966  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1967  1.1.2.2  skrll 			printf("Calling bound-all src addr selection for v6\n");
   1968  1.1.2.2  skrll 		}
   1969  1.1.2.2  skrll #endif
   1970  1.1.2.2  skrll 		rt_addr = sctp_choose_v6_boundall(inp, stcb, net, rt, loc_scope, loopscope, non_asoc_addr_ok);
   1971  1.1.2.2  skrll 	} else {
   1972  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1973  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1974  1.1.2.2  skrll 			printf("Calling bound-specific src addr selection for v6\n");
   1975  1.1.2.2  skrll 		}
   1976  1.1.2.2  skrll #endif
   1977  1.1.2.2  skrll 		if (stcb)
   1978  1.1.2.2  skrll 			rt_addr = sctp_choose_v6_boundspecific_stcb(inp, stcb, net, rt, loc_scope, loopscope,  non_asoc_addr_ok);
   1979  1.1.2.2  skrll 		else
   1980  1.1.2.2  skrll 			/* we can't have a non-asoc address since we have no association */
   1981  1.1.2.2  skrll 			rt_addr = sctp_choose_v6_boundspecific_inp(inp,  rt, loc_scope, loopscope);
   1982  1.1.2.2  skrll 	}
   1983  1.1.2.2  skrll 	if (rt_addr == NULL) {
   1984  1.1.2.2  skrll 		/* no suitable address? */
   1985  1.1.2.2  skrll 		struct in6_addr in6;
   1986  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1987  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1988  1.1.2.2  skrll 			printf("V6 packet will reach dead-end no suitable src address\n");
   1989  1.1.2.2  skrll 		}
   1990  1.1.2.2  skrll #endif
   1991  1.1.2.2  skrll 		memset(&in6, 0, sizeof(in6));
   1992  1.1.2.2  skrll 		return (in6);
   1993  1.1.2.2  skrll 	}
   1994  1.1.2.2  skrll #ifdef SCTP_DEBUG
   1995  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1996  1.1.2.2  skrll 		printf("Source address selected is:");
   1997  1.1.2.2  skrll 		sctp_print_address((struct sockaddr *)rt_addr);
   1998  1.1.2.2  skrll 	}
   1999  1.1.2.2  skrll #endif
   2000  1.1.2.2  skrll 	return (rt_addr->sin6_addr);
   2001  1.1.2.2  skrll }
   2002  1.1.2.2  skrll 
   2003  1.1.2.2  skrll static uint8_t
   2004  1.1.2.2  skrll sctp_get_ect(struct sctp_tcb *stcb,
   2005  1.1.2.2  skrll 	     struct sctp_tmit_chunk *chk)
   2006  1.1.2.2  skrll {
   2007  1.1.2.2  skrll 	uint8_t this_random;
   2008  1.1.2.2  skrll 
   2009  1.1.2.2  skrll 	/* Huh? */
   2010  1.1.2.2  skrll 	if (sctp_ecn == 0)
   2011  1.1.2.2  skrll 		return (0);
   2012  1.1.2.2  skrll 
   2013  1.1.2.2  skrll 	if (sctp_ecn_nonce == 0)
   2014  1.1.2.2  skrll 		/* no nonce, always return ECT0 */
   2015  1.1.2.2  skrll 		return (SCTP_ECT0_BIT);
   2016  1.1.2.2  skrll 
   2017  1.1.2.2  skrll 	if (stcb->asoc.peer_supports_ecn_nonce == 0) {
   2018  1.1.2.2  skrll 		/* Peer does NOT support it, so we send a ECT0 only */
   2019  1.1.2.2  skrll  		return (SCTP_ECT0_BIT);
   2020  1.1.2.2  skrll 	}
   2021  1.1.2.2  skrll 
   2022  1.1.2.2  skrll 	if (chk == NULL)
   2023  1.1.2.2  skrll 	   return (SCTP_ECT0_BIT);
   2024  1.1.2.2  skrll 
   2025  1.1.2.2  skrll 	if (((stcb->asoc.hb_random_idx == 3) &&
   2026  1.1.2.2  skrll 	     (stcb->asoc.hb_ect_randombit > 7)) ||
   2027  1.1.2.2  skrll 	     (stcb->asoc.hb_random_idx > 3)) {
   2028  1.1.2.2  skrll 		uint32_t rndval;
   2029  1.1.2.2  skrll 		rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
   2030  1.1.2.2  skrll 		memcpy(stcb->asoc.hb_random_values, &rndval,
   2031  1.1.2.2  skrll 		       sizeof(stcb->asoc.hb_random_values));
   2032  1.1.2.2  skrll 		this_random = stcb->asoc.hb_random_values[0];
   2033  1.1.2.2  skrll 		stcb->asoc.hb_random_idx = 0;
   2034  1.1.2.2  skrll 		stcb->asoc.hb_ect_randombit = 0;
   2035  1.1.2.2  skrll 	} else {
   2036  1.1.2.2  skrll 		if (stcb->asoc.hb_ect_randombit > 7) {
   2037  1.1.2.2  skrll 		  stcb->asoc.hb_ect_randombit = 0;
   2038  1.1.2.2  skrll 		  stcb->asoc.hb_random_idx++;
   2039  1.1.2.2  skrll 		}
   2040  1.1.2.2  skrll 		this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
   2041  1.1.2.2  skrll 	}
   2042  1.1.2.2  skrll 	if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
   2043  1.1.2.2  skrll 		if (chk != NULL)
   2044  1.1.2.2  skrll 			/* ECN Nonce stuff */
   2045  1.1.2.2  skrll 			chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
   2046  1.1.2.2  skrll 		stcb->asoc.hb_ect_randombit++;
   2047  1.1.2.2  skrll 		return (SCTP_ECT1_BIT);
   2048  1.1.2.2  skrll 	} else {
   2049  1.1.2.2  skrll 		stcb->asoc.hb_ect_randombit++;
   2050  1.1.2.2  skrll 		return (SCTP_ECT0_BIT);
   2051  1.1.2.2  skrll 	}
   2052  1.1.2.2  skrll }
   2053  1.1.2.2  skrll 
   2054  1.1.2.2  skrll extern int sctp_no_csum_on_loopback;
   2055  1.1.2.2  skrll 
   2056  1.1.2.2  skrll static int
   2057  1.1.2.2  skrll sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
   2058  1.1.2.2  skrll 			   struct sctp_tcb *stcb,    /* may be NULL */
   2059  1.1.2.2  skrll 			   struct sctp_nets *net,
   2060  1.1.2.2  skrll 			   const struct sockaddr *to,
   2061  1.1.2.2  skrll 			   struct mbuf *m,
   2062  1.1.2.2  skrll 			   int nofragment_flag,
   2063  1.1.2.2  skrll 			   int ecn_ok,
   2064  1.1.2.2  skrll 			   struct sctp_tmit_chunk *chk,
   2065  1.1.2.2  skrll 			   int out_of_asoc_ok)
   2066  1.1.2.2  skrll 	/* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
   2067  1.1.2.2  skrll {
   2068  1.1.2.2  skrll 	/*
   2069  1.1.2.2  skrll 	 * Given a mbuf chain (via m_next) that holds a packet header
   2070  1.1.2.2  skrll 	 * WITH a SCTPHDR but no IP header, endpoint inp and sa structure.
   2071  1.1.2.2  skrll 	 * - calculate SCTP checksum and fill in
   2072  1.1.2.2  skrll 	 * - prepend a IP address header
   2073  1.1.2.2  skrll 	 * - if boundall use INADDR_ANY
   2074  1.1.2.2  skrll 	 * - if boundspecific do source address selection
   2075  1.1.2.2  skrll 	 * - set fragmentation option for ipV4
   2076  1.1.2.2  skrll 	 * - On return from IP output, check/adjust mtu size
   2077  1.1.2.2  skrll 	 * - of output interface and smallest_mtu size as well.
   2078  1.1.2.2  skrll 	 */
   2079  1.1.2.2  skrll 	struct sctphdr *sctphdr;
   2080  1.1.2.2  skrll 	int o_flgs;
   2081  1.1.2.2  skrll 	uint32_t csum;
   2082  1.1.2.2  skrll 	int ret;
   2083  1.1.2.2  skrll 	unsigned int have_mtu;
   2084  1.1.2.2  skrll 	struct route *ro;
   2085  1.1.2.2  skrll 	struct rtentry *rt;
   2086  1.1.2.2  skrll 
   2087  1.1.2.2  skrll 	if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
   2088  1.1.2.2  skrll 		sctp_m_freem(m);
   2089  1.1.2.2  skrll 		return (EFAULT);
   2090  1.1.2.2  skrll 	}
   2091  1.1.2.2  skrll 	if ((m->m_flags & M_PKTHDR) == 0) {
   2092  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2093  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   2094  1.1.2.2  skrll 			printf("Software error: sctp_lowlevel_chunk_output() called with non pkthdr!\n");
   2095  1.1.2.2  skrll 		}
   2096  1.1.2.2  skrll #endif
   2097  1.1.2.2  skrll 		sctp_m_freem(m);
   2098  1.1.2.2  skrll 		return (EFAULT);
   2099  1.1.2.2  skrll 	}
   2100  1.1.2.2  skrll 	/* Calculate the csum and fill in the length of the packet */
   2101  1.1.2.2  skrll 	sctphdr = mtod(m, struct sctphdr *);
   2102  1.1.2.2  skrll 	have_mtu = 0;
   2103  1.1.2.2  skrll 	if (sctp_no_csum_on_loopback &&
   2104  1.1.2.2  skrll 	     (stcb) &&
   2105  1.1.2.2  skrll 	     (stcb->asoc.loopback_scope)) {
   2106  1.1.2.2  skrll 		sctphdr->checksum = 0;
   2107  1.1.2.2  skrll 		m->m_pkthdr.len = sctp_calculate_len(m);
   2108  1.1.2.2  skrll 	} else {
   2109  1.1.2.2  skrll 		sctphdr->checksum = 0;
   2110  1.1.2.2  skrll 		csum = sctp_calculate_sum(m, &m->m_pkthdr.len, 0);
   2111  1.1.2.2  skrll 		sctphdr->checksum = csum;
   2112  1.1.2.2  skrll 	}
   2113  1.1.2.2  skrll 	if (to->sa_family == AF_INET) {
   2114  1.1.2.2  skrll 		struct ip *ip;
   2115  1.1.2.2  skrll 		static struct route iproute;
   2116  1.1.2.2  skrll 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
   2117  1.1.2.2  skrll 		if (m == NULL) {
   2118  1.1.2.2  skrll 			/* failed to prepend data, give up */
   2119  1.1.2.2  skrll 			return (ENOMEM);
   2120  1.1.2.2  skrll 		}
   2121  1.1.2.2  skrll 		ip = mtod(m, struct ip *);
   2122  1.1.2.2  skrll 		ip->ip_v = IPVERSION;
   2123  1.1.2.2  skrll 		ip->ip_hl = (sizeof(struct ip) >> 2);
   2124  1.1.2.2  skrll 		if (nofragment_flag) {
   2125  1.1.2.2  skrll 			ip->ip_off = htons(IP_DF);
   2126  1.1.2.2  skrll 		} else
   2127  1.1.2.2  skrll 			ip->ip_off = 0;
   2128  1.1.2.2  skrll 
   2129  1.1.2.2  skrll 		ip->ip_id = htons(ip_newid(NULL));
   2130  1.1.2.2  skrll 		ip->ip_ttl = inp->inp_ip_ttl;
   2131  1.1.2.2  skrll 		ip->ip_len = htons(m->m_pkthdr.len);
   2132  1.1.2.2  skrll 		if (stcb) {
   2133  1.1.2.2  skrll 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
   2134  1.1.2.2  skrll 				/* Enable ECN */
   2135  1.1.2.2  skrll 				ip->ip_tos = (u_char)((inp->ip_inp.inp.inp_ip.ip_tos & 0x000000fc) |
   2136  1.1.2.2  skrll 						      sctp_get_ect(stcb, chk));
   2137  1.1.2.2  skrll 			} else {
   2138  1.1.2.2  skrll 				/* No ECN */
   2139  1.1.2.2  skrll 				ip->ip_tos = inp->ip_inp.inp.inp_ip.ip_tos;
   2140  1.1.2.2  skrll 			}
   2141  1.1.2.2  skrll 		} else {
   2142  1.1.2.2  skrll 			/* no association at all */
   2143  1.1.2.2  skrll 			ip->ip_tos = inp->inp_ip_tos;
   2144  1.1.2.2  skrll 		}
   2145  1.1.2.2  skrll 		ip->ip_p = IPPROTO_SCTP;
   2146  1.1.2.2  skrll 		ip->ip_sum = 0;
   2147  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2148  1.1.2.2  skrll 		printf("chunk_output: net %p\n", net);
   2149  1.1.2.2  skrll #endif
   2150  1.1.2.2  skrll 		if (net == NULL) {
   2151  1.1.2.2  skrll 			ro = &iproute;
   2152  1.1.2.2  skrll 			memset(&iproute, 0, sizeof(iproute));
   2153  1.1.2.2  skrll 			rtcache_lookup(ro, to);
   2154  1.1.2.2  skrll 		} else {
   2155  1.1.2.2  skrll 			ro = (struct route *)&net->ro;
   2156  1.1.2.2  skrll 		}
   2157  1.1.2.2  skrll 		/* Now the address selection part */
   2158  1.1.2.2  skrll 		ip->ip_dst.s_addr = satocsin(to)->sin_addr.s_addr;
   2159  1.1.2.2  skrll 
   2160  1.1.2.2  skrll 		/* call the routine to select the src address */
   2161  1.1.2.2  skrll 		if (net) {
   2162  1.1.2.2  skrll 			if (net->src_addr_selected == 0) {
   2163  1.1.2.2  skrll 				/* Cache the source address */
   2164  1.1.2.2  skrll 				((struct sockaddr_in *)&net->_s_addr)->sin_addr = sctp_ipv4_source_address_selection(inp,
   2165  1.1.2.2  skrll 				    stcb,
   2166  1.1.2.2  skrll 				    ro, net, out_of_asoc_ok);
   2167  1.1.2.2  skrll 				if (rtcache_validate(ro)) {
   2168  1.1.2.2  skrll 					net->src_addr_selected = 1;
   2169  1.1.2.2  skrll 				}
   2170  1.1.2.2  skrll 			}
   2171  1.1.2.2  skrll 			ip->ip_src = ((struct sockaddr_in *)&net->_s_addr)->sin_addr;
   2172  1.1.2.2  skrll 		} else {
   2173  1.1.2.2  skrll 			ip->ip_src = sctp_ipv4_source_address_selection(inp,
   2174  1.1.2.2  skrll 			    stcb, ro, net, out_of_asoc_ok);
   2175  1.1.2.2  skrll 		}
   2176  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2177  1.1.2.2  skrll 		printf("src addr %x\n", ip->ip_src.s_addr);
   2178  1.1.2.2  skrll #endif
   2179  1.1.2.2  skrll 		/*
   2180  1.1.2.2  skrll 		 * If source address selection fails and we find no route then
   2181  1.1.2.2  skrll 		 * the ip_ouput should fail as well with a NO_ROUTE_TO_HOST
   2182  1.1.2.2  skrll 		 * type error. We probably should catch that somewhere and
   2183  1.1.2.2  skrll 		 * abort the association right away (assuming this is an INIT
   2184  1.1.2.2  skrll 		 * being sent).
   2185  1.1.2.2  skrll 		 */
   2186  1.1.2.2  skrll 		rt = rtcache_validate(ro);
   2187  1.1.2.2  skrll 		if ((rt == NULL)) {
   2188  1.1.2.2  skrll 			/*
   2189  1.1.2.2  skrll 			 * src addr selection failed to find a route (or valid
   2190  1.1.2.2  skrll 			 * source addr), so we can't get there from here!
   2191  1.1.2.2  skrll 			 */
   2192  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2193  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   2194  1.1.2.2  skrll 				printf("low_level_output: dropped v4 packet- no valid source addr\n");
   2195  1.1.2.2  skrll 				printf("Destination was %x\n", (u_int)(ntohl(ip->ip_dst.s_addr)));
   2196  1.1.2.2  skrll 			}
   2197  1.1.2.2  skrll #endif /* SCTP_DEBUG */
   2198  1.1.2.2  skrll 			if (net) {
   2199  1.1.2.2  skrll 				if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb)
   2200  1.1.2.2  skrll 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
   2201  1.1.2.2  skrll 							stcb,
   2202  1.1.2.2  skrll 							SCTP_FAILED_THRESHOLD,
   2203  1.1.2.2  skrll 							(void *)net);
   2204  1.1.2.2  skrll 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
   2205  1.1.2.2  skrll 				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
   2206  1.1.2.2  skrll 				if (stcb) {
   2207  1.1.2.2  skrll 					if (net == stcb->asoc.primary_destination) {
   2208  1.1.2.2  skrll 						/* need a new primary */
   2209  1.1.2.2  skrll 						struct sctp_nets *alt;
   2210  1.1.2.2  skrll 						alt = sctp_find_alternate_net(stcb, net);
   2211  1.1.2.2  skrll 						if (alt != net) {
   2212  1.1.2.2  skrll 							if (sctp_set_primary_addr(stcb,
   2213  1.1.2.2  skrll 									      (struct sockaddr *)NULL,
   2214  1.1.2.2  skrll 										 alt) == 0) {
   2215  1.1.2.2  skrll 								net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
   2216  1.1.2.2  skrll 								net->src_addr_selected = 0;
   2217  1.1.2.2  skrll 							}
   2218  1.1.2.2  skrll 						}
   2219  1.1.2.2  skrll 					}
   2220  1.1.2.2  skrll 				}
   2221  1.1.2.2  skrll 			}
   2222  1.1.2.2  skrll 			sctp_m_freem(m);
   2223  1.1.2.2  skrll 			return (EHOSTUNREACH);
   2224  1.1.2.2  skrll 		} else {
   2225  1.1.2.2  skrll 			have_mtu = rt->rt_ifp->if_mtu;
   2226  1.1.2.2  skrll 		}
   2227  1.1.2.2  skrll 
   2228  1.1.2.2  skrll 		o_flgs = (IP_RAWOUTPUT | (inp->sctp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)));
   2229  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2230  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   2231  1.1.2.2  skrll 			printf("Calling ipv4 output routine from low level src addr:%x\n",
   2232  1.1.2.2  skrll 			       (u_int)(ntohl(ip->ip_src.s_addr)));
   2233  1.1.2.2  skrll 			printf("Destination is %x\n", (u_int)(ntohl(ip->ip_dst.s_addr)));
   2234  1.1.2.2  skrll 			printf("RTP route is %p through\n", rt);
   2235  1.1.2.2  skrll 			printf("length %d\n", ip->ip_len);
   2236  1.1.2.2  skrll 		}
   2237  1.1.2.2  skrll #endif
   2238  1.1.2.2  skrll 		if ((have_mtu) && (net) && (have_mtu > net->mtu)) {
   2239  1.1.2.2  skrll 			rt->rt_ifp->if_mtu = net->mtu;
   2240  1.1.2.2  skrll 		}
   2241  1.1.2.2  skrll 		ret = ip_output(m, inp->ip_inp.inp.inp_options,
   2242  1.1.2.2  skrll 				ro, o_flgs, inp->ip_inp.inp.inp_moptions,
   2243  1.1.2.2  skrll                                 (struct socket *)inp->sctp_socket);
   2244  1.1.2.2  skrll 		if ((rt) && (have_mtu) && (net) && (have_mtu > net->mtu)) {
   2245  1.1.2.2  skrll 			rt->rt_ifp->if_mtu = have_mtu;
   2246  1.1.2.2  skrll 		}
   2247  1.1.2.2  skrll 		sctp_pegs[SCTP_DATAGRAMS_SENT]++;
   2248  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2249  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   2250  1.1.2.2  skrll 			printf("Ip output returns %d\n", ret);
   2251  1.1.2.2  skrll 		}
   2252  1.1.2.2  skrll #endif
   2253  1.1.2.2  skrll 		if (net == NULL) {
   2254  1.1.2.2  skrll 		} else {
   2255  1.1.2.2  skrll 			/* PMTU check versus smallest asoc MTU goes here */
   2256  1.1.2.2  skrll 			if (rt != NULL) {
   2257  1.1.2.2  skrll 				if (rt->rt_rmx.rmx_mtu &&
   2258  1.1.2.2  skrll 				    (stcb->asoc.smallest_mtu > rt->rt_rmx.rmx_mtu)) {
   2259  1.1.2.2  skrll 					sctp_mtu_size_reset(inp, &stcb->asoc,
   2260  1.1.2.2  skrll 					    rt->rt_rmx.rmx_mtu);
   2261  1.1.2.2  skrll 				}
   2262  1.1.2.2  skrll 			} else {
   2263  1.1.2.2  skrll 				/* route was freed */
   2264  1.1.2.2  skrll 				net->src_addr_selected = 0;
   2265  1.1.2.2  skrll 			}
   2266  1.1.2.2  skrll 		}
   2267  1.1.2.2  skrll 		return (ret);
   2268  1.1.2.2  skrll 	}
   2269  1.1.2.2  skrll #ifdef INET6
   2270  1.1.2.2  skrll 	else if (to->sa_family == AF_INET6) {
   2271  1.1.2.2  skrll 		struct ip6_hdr *ip6h;
   2272  1.1.2.2  skrll 		static struct route ip6route;
   2273  1.1.2.2  skrll 		struct ifnet *ifp;
   2274  1.1.2.2  skrll 		u_char flowTop;
   2275  1.1.2.2  skrll 		uint16_t flowBottom;
   2276  1.1.2.2  skrll 		u_char tosBottom, tosTop;
   2277  1.1.2.2  skrll 		struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
   2278  1.1.2.2  skrll 		int prev_scope=0;
   2279  1.1.2.2  skrll 		u_short prev_port=0;
   2280  1.1.2.2  skrll 
   2281  1.1.2.2  skrll 		M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
   2282  1.1.2.2  skrll 		if (m == NULL) {
   2283  1.1.2.2  skrll 			/* failed to prepend data, give up */
   2284  1.1.2.2  skrll 			return (ENOMEM);
   2285  1.1.2.2  skrll 		}
   2286  1.1.2.2  skrll 		ip6h = mtod(m, struct ip6_hdr *);
   2287  1.1.2.2  skrll 
   2288  1.1.2.2  skrll 		/*
   2289  1.1.2.2  skrll 		 * We assume here that inp_flow is in host byte order within
   2290  1.1.2.2  skrll 		 * the TCB!
   2291  1.1.2.2  skrll 		 */
   2292  1.1.2.2  skrll 		flowBottom = ((struct in6pcb *)inp)->in6p_flowinfo & 0x0000ffff;
   2293  1.1.2.2  skrll 		flowTop = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x000f0000) >> 16);
   2294  1.1.2.2  skrll 
   2295  1.1.2.2  skrll 		tosTop = (((((struct in6pcb *)inp)->in6p_flowinfo & 0xf0) >> 4) | IPV6_VERSION);
   2296  1.1.2.2  skrll 
   2297  1.1.2.2  skrll 		/* protect *sin6 from overwrite */
   2298  1.1.2.2  skrll 		memcpy(&tmp, to, sizeof(struct sockaddr_in6));
   2299  1.1.2.2  skrll 		sin6 = &tmp;
   2300  1.1.2.2  skrll 
   2301  1.1.2.2  skrll 		/* KAME hack: embed scopeid */
   2302  1.1.2.2  skrll #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__)
   2303  1.1.2.2  skrll 		if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0)
   2304  1.1.2.2  skrll #else
   2305  1.1.2.2  skrll 		/*
   2306  1.1.2.2  skrll 		 * XXX: appropriate scope zone must be provided or otherwise
   2307  1.1.2.2  skrll 		 * ip6_use_defzone must be 1.
   2308  1.1.2.2  skrll 		 */
   2309  1.1.2.2  skrll 		if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
   2310  1.1.2.2  skrll #endif
   2311  1.1.2.2  skrll 			return (EINVAL);
   2312  1.1.2.2  skrll 		if (net == NULL) {
   2313  1.1.2.2  skrll 			memset(&ip6route, 0, sizeof(ip6route));
   2314  1.1.2.2  skrll 			ro = (struct route *)&ip6route;
   2315  1.1.2.2  skrll 			rtcache_lookup(ro, (struct sockaddr *) sin6);
   2316  1.1.2.2  skrll 		} else {
   2317  1.1.2.2  skrll 			ro = (struct route *)&net->ro;
   2318  1.1.2.2  skrll 		}
   2319  1.1.2.2  skrll 		if (stcb != NULL) {
   2320  1.1.2.2  skrll 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
   2321  1.1.2.2  skrll 				/* Enable ECN */
   2322  1.1.2.2  skrll 				tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
   2323  1.1.2.2  skrll 			} else {
   2324  1.1.2.2  skrll 				/* No ECN */
   2325  1.1.2.2  skrll 				tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
   2326  1.1.2.2  skrll 			}
   2327  1.1.2.2  skrll 		} else {
   2328  1.1.2.2  skrll 			/* we could get no asoc if it is a O-O-T-B packet */
   2329  1.1.2.2  skrll 			tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
   2330  1.1.2.2  skrll 		}
   2331  1.1.2.2  skrll 		ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom|flowTop) << 16) | flowBottom));
   2332  1.1.2.2  skrll 		ip6h->ip6_nxt = IPPROTO_SCTP;
   2333  1.1.2.2  skrll 		ip6h->ip6_plen = m->m_pkthdr.len;
   2334  1.1.2.2  skrll 		ip6h->ip6_dst = sin6->sin6_addr;
   2335  1.1.2.2  skrll 
   2336  1.1.2.2  skrll 		/*
   2337  1.1.2.2  skrll 		 * Add SRC address selection here:
   2338  1.1.2.2  skrll 		 * we can only reuse to a limited degree the kame src-addr-sel,
   2339  1.1.2.2  skrll 		 * since we can try their selection but it may not be bound.
   2340  1.1.2.2  skrll 		 */
   2341  1.1.2.2  skrll 		memset(&lsa6_tmp, 0, sizeof(lsa6_tmp));
   2342  1.1.2.2  skrll 		lsa6_tmp.sin6_family = AF_INET6;
   2343  1.1.2.2  skrll 		lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
   2344  1.1.2.2  skrll 		lsa6 = &lsa6_tmp;
   2345  1.1.2.2  skrll 		rt = rtcache_validate(ro);
   2346  1.1.2.2  skrll 		if (net) {
   2347  1.1.2.2  skrll 			if (net->src_addr_selected == 0) {
   2348  1.1.2.2  skrll 				/* Cache the source address */
   2349  1.1.2.2  skrll 				((struct sockaddr_in6 *)&net->_s_addr)->sin6_addr = sctp_ipv6_source_address_selection(inp,
   2350  1.1.2.2  skrll 				    stcb, ro, net, out_of_asoc_ok);
   2351  1.1.2.2  skrll 
   2352  1.1.2.2  skrll 				if (rt != NULL) {
   2353  1.1.2.2  skrll 					net->src_addr_selected = 1;
   2354  1.1.2.2  skrll 				}
   2355  1.1.2.2  skrll 			}
   2356  1.1.2.2  skrll 			lsa6->sin6_addr = ((struct sockaddr_in6 *)&net->_s_addr)->sin6_addr;
   2357  1.1.2.2  skrll 		} else {
   2358  1.1.2.2  skrll 			lsa6->sin6_addr = sctp_ipv6_source_address_selection(
   2359  1.1.2.2  skrll 			    inp, stcb, ro, net, out_of_asoc_ok);
   2360  1.1.2.2  skrll 		}
   2361  1.1.2.2  skrll 		lsa6->sin6_port = inp->sctp_lport;
   2362  1.1.2.2  skrll 
   2363  1.1.2.2  skrll 		if ((rt ==  NULL)) {
   2364  1.1.2.2  skrll 			/*
   2365  1.1.2.2  skrll 			 * src addr selection failed to find a route (or valid
   2366  1.1.2.2  skrll 			 * source addr), so we can't get there from here!
   2367  1.1.2.2  skrll 			 */
   2368  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2369  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   2370  1.1.2.2  skrll 				printf("low_level_output: dropped v6 pkt- no valid source addr\n");
   2371  1.1.2.2  skrll 			}
   2372  1.1.2.2  skrll #endif
   2373  1.1.2.2  skrll 			sctp_m_freem(m);
   2374  1.1.2.2  skrll 			if (net) {
   2375  1.1.2.2  skrll 				if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb)
   2376  1.1.2.2  skrll 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
   2377  1.1.2.2  skrll 							stcb,
   2378  1.1.2.2  skrll 							SCTP_FAILED_THRESHOLD,
   2379  1.1.2.2  skrll 							(void *)net);
   2380  1.1.2.2  skrll 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
   2381  1.1.2.2  skrll 				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
   2382  1.1.2.2  skrll 				if (stcb) {
   2383  1.1.2.2  skrll 					if (net == stcb->asoc.primary_destination) {
   2384  1.1.2.2  skrll 						/* need a new primary */
   2385  1.1.2.2  skrll 						struct sctp_nets *alt;
   2386  1.1.2.2  skrll 						alt = sctp_find_alternate_net(stcb, net);
   2387  1.1.2.2  skrll 						if (alt != net) {
   2388  1.1.2.2  skrll 							if (sctp_set_primary_addr(stcb,
   2389  1.1.2.2  skrll 									      (struct sockaddr *)NULL,
   2390  1.1.2.2  skrll 										 alt) == 0) {
   2391  1.1.2.2  skrll 								net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
   2392  1.1.2.2  skrll 								net->src_addr_selected = 0;
   2393  1.1.2.2  skrll 							}
   2394  1.1.2.2  skrll 						}
   2395  1.1.2.2  skrll 					}
   2396  1.1.2.2  skrll 				}
   2397  1.1.2.2  skrll 			}
   2398  1.1.2.2  skrll 			return (EHOSTUNREACH);
   2399  1.1.2.2  skrll 		}
   2400  1.1.2.2  skrll 
   2401  1.1.2.2  skrll 		ip6h->ip6_src = lsa6->sin6_addr;
   2402  1.1.2.2  skrll 
   2403  1.1.2.2  skrll 		/*
   2404  1.1.2.2  skrll 		 * We set the hop limit now since there is a good chance that
   2405  1.1.2.2  skrll 		 * our ro pointer is now filled
   2406  1.1.2.2  skrll 		 */
   2407  1.1.2.2  skrll 		ip6h->ip6_hlim = in6_selecthlim((struct in6pcb *)&inp->ip_inp.inp,
   2408  1.1.2.2  skrll 						(ro ?
   2409  1.1.2.2  skrll 						 (rt ? (rt->rt_ifp) : (NULL)) :
   2410  1.1.2.2  skrll 						 (NULL)));
   2411  1.1.2.2  skrll 		o_flgs = 0;
   2412  1.1.2.2  skrll 		ifp = rt->rt_ifp;
   2413  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2414  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   2415  1.1.2.2  skrll 			/* Copy to be sure something bad is not happening */
   2416  1.1.2.2  skrll 			sin6->sin6_addr = ip6h->ip6_dst;
   2417  1.1.2.2  skrll 			lsa6->sin6_addr = ip6h->ip6_src;
   2418  1.1.2.2  skrll 
   2419  1.1.2.2  skrll 			printf("Calling ipv6 output routine from low level\n");
   2420  1.1.2.2  skrll 			printf("src: ");
   2421  1.1.2.2  skrll 			sctp_print_address((struct sockaddr *)lsa6);
   2422  1.1.2.2  skrll 			printf("dst: ");
   2423  1.1.2.2  skrll 			sctp_print_address((struct sockaddr *)sin6);
   2424  1.1.2.2  skrll 		}
   2425  1.1.2.2  skrll #endif /* SCTP_DEBUG */
   2426  1.1.2.2  skrll 		if (net) {
   2427  1.1.2.2  skrll 			sin6 = (struct sockaddr_in6 *)&net->ro.ro_sa;
   2428  1.1.2.2  skrll 			/* preserve the port and scope for link local send */
   2429  1.1.2.2  skrll 			prev_scope = sin6->sin6_scope_id;
   2430  1.1.2.2  skrll 			prev_port = sin6->sin6_port;
   2431  1.1.2.2  skrll 		}
   2432  1.1.2.2  skrll 		ret = ip6_output(m, ((struct in6pcb *)inp)->in6p_outputopts,
   2433  1.1.2.2  skrll 				 ro,
   2434  1.1.2.2  skrll 				 o_flgs,
   2435  1.1.2.2  skrll 				 ((struct in6pcb *)inp)->in6p_moptions,
   2436  1.1.2.2  skrll 				 (struct socket *)inp->sctp_socket,
   2437  1.1.2.2  skrll 				 &ifp);
   2438  1.1.2.2  skrll 		if (net) {
   2439  1.1.2.2  skrll 			/* for link local this must be done */
   2440  1.1.2.2  skrll 			sin6->sin6_scope_id = prev_scope;
   2441  1.1.2.2  skrll 			sin6->sin6_port = prev_port;
   2442  1.1.2.2  skrll 		}
   2443  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2444  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   2445  1.1.2.2  skrll 			printf("return from send is %d\n", ret);
   2446  1.1.2.2  skrll 		}
   2447  1.1.2.2  skrll #endif /* SCTP_DEBUG_OUTPUT */
   2448  1.1.2.2  skrll 		sctp_pegs[SCTP_DATAGRAMS_SENT]++;
   2449  1.1.2.2  skrll 		if (net) {
   2450  1.1.2.2  skrll 			/* PMTU check versus smallest asoc MTU goes here */
   2451  1.1.2.2  skrll 			rt = rtcache_validate(ro);
   2452  1.1.2.2  skrll 			if (rt == NULL) {
   2453  1.1.2.2  skrll 				/* Route was freed */
   2454  1.1.2.2  skrll 				net->src_addr_selected = 0;
   2455  1.1.2.2  skrll 			}
   2456  1.1.2.2  skrll 			if (rt != NULL) {
   2457  1.1.2.2  skrll 				if (rt->rt_rmx.rmx_mtu &&
   2458  1.1.2.2  skrll 				    (stcb->asoc.smallest_mtu > rt->rt_rmx.rmx_mtu)) {
   2459  1.1.2.2  skrll 					sctp_mtu_size_reset(inp,
   2460  1.1.2.2  skrll 							    &stcb->asoc,
   2461  1.1.2.2  skrll 							    rt->rt_rmx.rmx_mtu);
   2462  1.1.2.2  skrll 				}
   2463  1.1.2.2  skrll 			} else if (ifp) {
   2464  1.1.2.2  skrll 				if (ND_IFINFO(ifp)->linkmtu &&
   2465  1.1.2.2  skrll 				    (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
   2466  1.1.2.2  skrll 					sctp_mtu_size_reset(inp,
   2467  1.1.2.2  skrll 							    &stcb->asoc,
   2468  1.1.2.2  skrll 							    ND_IFINFO(ifp)->linkmtu);
   2469  1.1.2.2  skrll 				}
   2470  1.1.2.2  skrll 			}
   2471  1.1.2.2  skrll 		}
   2472  1.1.2.2  skrll 		return (ret);
   2473  1.1.2.2  skrll 	}
   2474  1.1.2.2  skrll #endif
   2475  1.1.2.2  skrll 	else {
   2476  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2477  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   2478  1.1.2.2  skrll 			printf("Unknown protocol (TSNH) type %d\n", ((const struct sockaddr *)to)->sa_family);
   2479  1.1.2.2  skrll 		}
   2480  1.1.2.2  skrll #endif
   2481  1.1.2.2  skrll 		sctp_m_freem(m);
   2482  1.1.2.2  skrll 		return (EFAULT);
   2483  1.1.2.2  skrll 	}
   2484  1.1.2.2  skrll }
   2485  1.1.2.2  skrll 
   2486  1.1.2.2  skrll static
   2487  1.1.2.2  skrll int sctp_is_address_in_scope(struct ifaddr *ifa,
   2488  1.1.2.2  skrll  			     int ipv4_addr_legal,
   2489  1.1.2.2  skrll 			     int ipv6_addr_legal,
   2490  1.1.2.2  skrll 			     int loopback_scope,
   2491  1.1.2.2  skrll 			     int ipv4_local_scope,
   2492  1.1.2.2  skrll 			     int local_scope,
   2493  1.1.2.2  skrll 			     int site_scope)
   2494  1.1.2.2  skrll {
   2495  1.1.2.2  skrll 	if ((loopback_scope == 0) &&
   2496  1.1.2.2  skrll 	    (ifa->ifa_ifp) &&
   2497  1.1.2.2  skrll 	    (ifa->ifa_ifp->if_type == IFT_LOOP)) {
   2498  1.1.2.2  skrll 		/* skip loopback if not in scope *
   2499  1.1.2.2  skrll 		 */
   2500  1.1.2.2  skrll 		return (0);
   2501  1.1.2.2  skrll 	}
   2502  1.1.2.2  skrll 	if ((ifa->ifa_addr->sa_family == AF_INET) && ipv4_addr_legal) {
   2503  1.1.2.2  skrll 		struct sockaddr_in *sin;
   2504  1.1.2.2  skrll 		sin = (struct sockaddr_in *)ifa->ifa_addr;
   2505  1.1.2.2  skrll 		if (sin->sin_addr.s_addr == 0) {
   2506  1.1.2.2  skrll 			/* not in scope , unspecified */
   2507  1.1.2.2  skrll 			return (0);
   2508  1.1.2.2  skrll 		}
   2509  1.1.2.2  skrll 		if ((ipv4_local_scope == 0) &&
   2510  1.1.2.2  skrll 		    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
   2511  1.1.2.2  skrll 			/* private address not in scope */
   2512  1.1.2.2  skrll 			return (0);
   2513  1.1.2.2  skrll 		}
   2514  1.1.2.2  skrll 	} else if ((ifa->ifa_addr->sa_family == AF_INET6) && ipv6_addr_legal) {
   2515  1.1.2.2  skrll 		struct sockaddr_in6 *sin6;
   2516  1.1.2.2  skrll 		struct in6_ifaddr *ifa6;
   2517  1.1.2.2  skrll 
   2518  1.1.2.2  skrll 		ifa6 = (struct in6_ifaddr *)ifa;
   2519  1.1.2.2  skrll 		/* ok to use deprecated addresses? */
   2520  1.1.2.2  skrll 		if (!ip6_use_deprecated) {
   2521  1.1.2.2  skrll 			if (ifa6->ia6_flags &
   2522  1.1.2.2  skrll 			    IN6_IFF_DEPRECATED) {
   2523  1.1.2.2  skrll 				return (0);
   2524  1.1.2.2  skrll 			}
   2525  1.1.2.2  skrll 		}
   2526  1.1.2.2  skrll 		if (ifa6->ia6_flags &
   2527  1.1.2.2  skrll 		    (IN6_IFF_DETACHED |
   2528  1.1.2.2  skrll 		     IN6_IFF_ANYCAST |
   2529  1.1.2.2  skrll 		     IN6_IFF_NOTREADY)) {
   2530  1.1.2.2  skrll 			return (0);
   2531  1.1.2.2  skrll 		}
   2532  1.1.2.2  skrll 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
   2533  1.1.2.2  skrll 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   2534  1.1.2.2  skrll 			/* skip unspecifed addresses */
   2535  1.1.2.2  skrll 			return (0);
   2536  1.1.2.2  skrll 		}
   2537  1.1.2.2  skrll 		if (/*(local_scope == 0) && */
   2538  1.1.2.2  skrll 		    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
   2539  1.1.2.2  skrll 			return (0);
   2540  1.1.2.2  skrll 		}
   2541  1.1.2.2  skrll 		if ((site_scope == 0) &&
   2542  1.1.2.2  skrll 		    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
   2543  1.1.2.2  skrll 			return (0);
   2544  1.1.2.2  skrll 		}
   2545  1.1.2.2  skrll 	} else {
   2546  1.1.2.2  skrll 		return (0);
   2547  1.1.2.2  skrll 	}
   2548  1.1.2.2  skrll 	return (1);
   2549  1.1.2.2  skrll }
   2550  1.1.2.2  skrll 
   2551  1.1.2.2  skrll 
   2552  1.1.2.2  skrll void
   2553  1.1.2.2  skrll sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
   2554  1.1.2.2  skrll {
   2555  1.1.2.2  skrll 	struct mbuf *m, *m_at, *m_last;
   2556  1.1.2.2  skrll 	struct sctp_nets *net;
   2557  1.1.2.2  skrll 	struct sctp_init_msg *initm;
   2558  1.1.2.2  skrll 	struct sctp_supported_addr_param *sup_addr;
   2559  1.1.2.2  skrll 	struct sctp_ecn_supported_param *ecn;
   2560  1.1.2.2  skrll 	struct sctp_prsctp_supported_param *prsctp;
   2561  1.1.2.2  skrll 	struct sctp_ecn_nonce_supported_param *ecn_nonce;
   2562  1.1.2.2  skrll 	struct sctp_supported_chunk_types_param *pr_supported;
   2563  1.1.2.2  skrll 	int cnt_inits_to=0;
   2564  1.1.2.2  skrll 	int padval, ret;
   2565  1.1.2.2  skrll 
   2566  1.1.2.2  skrll 	/* INIT's always go to the primary (and usually ONLY address) */
   2567  1.1.2.2  skrll 	m_last = NULL;
   2568  1.1.2.2  skrll 	net = stcb->asoc.primary_destination;
   2569  1.1.2.2  skrll 	if (net == NULL) {
   2570  1.1.2.2  skrll 		net = TAILQ_FIRST(&stcb->asoc.nets);
   2571  1.1.2.2  skrll 		if (net == NULL) {
   2572  1.1.2.2  skrll 			/* TSNH */
   2573  1.1.2.2  skrll 			return;
   2574  1.1.2.2  skrll 		}
   2575  1.1.2.2  skrll 		/* we confirm any address we send an INIT to */
   2576  1.1.2.2  skrll 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
   2577  1.1.2.2  skrll 		sctp_set_primary_addr(stcb, NULL, net);
   2578  1.1.2.2  skrll 	} else {
   2579  1.1.2.2  skrll 		/* we confirm any address we send an INIT to */
   2580  1.1.2.2  skrll 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
   2581  1.1.2.2  skrll 	}
   2582  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2583  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   2584  1.1.2.2  skrll 		printf("Sending INIT to ");
   2585  1.1.2.2  skrll 		sctp_print_address (rtcache_getdst(&net->ro));
   2586  1.1.2.2  skrll 	}
   2587  1.1.2.2  skrll #endif
   2588  1.1.2.2  skrll 	if (rtcache_getdst(&net->ro)->sa_family == AF_INET6) {
   2589  1.1.2.2  skrll 		/* special hook, if we are sending to link local
   2590  1.1.2.2  skrll 		 * it will not show up in our private address count.
   2591  1.1.2.2  skrll 		 */
   2592  1.1.2.2  skrll 		if (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *) rtcache_getdst(&net->ro)->sa_data))
   2593  1.1.2.2  skrll 			cnt_inits_to = 1;
   2594  1.1.2.2  skrll 	}
   2595  1.1.2.2  skrll 	if (callout_pending(&net->rxt_timer.timer)) {
   2596  1.1.2.2  skrll 		/* This case should not happen */
   2597  1.1.2.2  skrll 		return;
   2598  1.1.2.2  skrll 	}
   2599  1.1.2.2  skrll 	/* start the INIT timer */
   2600  1.1.2.2  skrll 	if (sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net)) {
   2601  1.1.2.2  skrll 		/* we are hosed since I can't start the INIT timer? */
   2602  1.1.2.2  skrll 		return;
   2603  1.1.2.2  skrll 	}
   2604  1.1.2.2  skrll 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
   2605  1.1.2.2  skrll 	if (m == NULL) {
   2606  1.1.2.2  skrll 		/* No memory, INIT timer will re-attempt. */
   2607  1.1.2.2  skrll 		return;
   2608  1.1.2.2  skrll 	}
   2609  1.1.2.2  skrll 	/* make it into a M_EXT */
   2610  1.1.2.2  skrll 	MCLGET(m, M_DONTWAIT);
   2611  1.1.2.2  skrll 	if ((m->m_flags & M_EXT) != M_EXT) {
   2612  1.1.2.2  skrll 		/* Failed to get cluster buffer */
   2613  1.1.2.2  skrll 		sctp_m_freem(m);
   2614  1.1.2.2  skrll 		return;
   2615  1.1.2.2  skrll 	}
   2616  1.1.2.2  skrll 	m->m_data += SCTP_MIN_OVERHEAD;
   2617  1.1.2.2  skrll 	m->m_len = sizeof(struct sctp_init_msg);
   2618  1.1.2.2  skrll 	/* Now lets put the SCTP header in place */
   2619  1.1.2.2  skrll 	initm = mtod(m, struct sctp_init_msg *);
   2620  1.1.2.2  skrll 	initm->sh.src_port = inp->sctp_lport;
   2621  1.1.2.2  skrll 	initm->sh.dest_port = stcb->rport;
   2622  1.1.2.2  skrll 	initm->sh.v_tag = 0;
   2623  1.1.2.2  skrll 	initm->sh.checksum = 0;	/* calculate later */
   2624  1.1.2.2  skrll 	/* now the chunk header */
   2625  1.1.2.2  skrll 	initm->msg.ch.chunk_type = SCTP_INITIATION;
   2626  1.1.2.2  skrll 	initm->msg.ch.chunk_flags = 0;
   2627  1.1.2.2  skrll 	/* fill in later from mbuf we build */
   2628  1.1.2.2  skrll 	initm->msg.ch.chunk_length = 0;
   2629  1.1.2.2  skrll 	/* place in my tag */
   2630  1.1.2.2  skrll 	initm->msg.init.initiate_tag = htonl(stcb->asoc.my_vtag);
   2631  1.1.2.2  skrll 	/* set up some of the credits. */
   2632  1.1.2.2  skrll 	initm->msg.init.a_rwnd = htonl(max(inp->sctp_socket->so_rcv.sb_hiwat,
   2633  1.1.2.2  skrll 	    SCTP_MINIMAL_RWND));
   2634  1.1.2.2  skrll 
   2635  1.1.2.2  skrll 	initm->msg.init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
   2636  1.1.2.2  skrll 	initm->msg.init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
   2637  1.1.2.2  skrll 	initm->msg.init.initial_tsn = htonl(stcb->asoc.init_seq_number);
   2638  1.1.2.2  skrll 	/* now the address restriction */
   2639  1.1.2.2  skrll 	sup_addr = (struct sctp_supported_addr_param *)((vaddr_t)initm +
   2640  1.1.2.2  skrll 	    sizeof(*initm));
   2641  1.1.2.2  skrll 	sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
   2642  1.1.2.2  skrll 	/* we support 2 types IPv6/IPv4 */
   2643  1.1.2.2  skrll 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) +
   2644  1.1.2.2  skrll 					  sizeof(uint16_t));
   2645  1.1.2.2  skrll 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
   2646  1.1.2.2  skrll 	sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
   2647  1.1.2.2  skrll 	m->m_len += sizeof(*sup_addr) + sizeof(uint16_t);
   2648  1.1.2.2  skrll 
   2649  1.1.2.2  skrll /*	if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT) {*/
   2650  1.1.2.2  skrll 	if (inp->sctp_ep.adaption_layer_indicator) {
   2651  1.1.2.2  skrll 		struct sctp_adaption_layer_indication *ali;
   2652  1.1.2.2  skrll 		ali = (struct sctp_adaption_layer_indication *)(
   2653  1.1.2.2  skrll 		    (vaddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
   2654  1.1.2.2  skrll 		ali->ph.param_type = htons(SCTP_ULP_ADAPTION);
   2655  1.1.2.2  skrll 		ali->ph.param_length = htons(sizeof(*ali));
   2656  1.1.2.2  skrll 		ali->indication = ntohl(inp->sctp_ep.adaption_layer_indicator);
   2657  1.1.2.2  skrll 		m->m_len += sizeof(*ali);
   2658  1.1.2.2  skrll 		ecn = (struct sctp_ecn_supported_param *)((vaddr_t)ali +
   2659  1.1.2.2  skrll 		    sizeof(*ali));
   2660  1.1.2.2  skrll 	} else {
   2661  1.1.2.2  skrll 		ecn = (struct sctp_ecn_supported_param *)((vaddr_t)sup_addr +
   2662  1.1.2.2  skrll 		    sizeof(*sup_addr) + sizeof(uint16_t));
   2663  1.1.2.2  skrll 	}
   2664  1.1.2.2  skrll 
   2665  1.1.2.2  skrll 	/* now any cookie time extensions */
   2666  1.1.2.2  skrll 	if (stcb->asoc.cookie_preserve_req) {
   2667  1.1.2.2  skrll 		struct sctp_cookie_perserve_param *cookie_preserve;
   2668  1.1.2.2  skrll 		cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
   2669  1.1.2.2  skrll 		cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
   2670  1.1.2.2  skrll 		cookie_preserve->ph.param_length = htons(
   2671  1.1.2.2  skrll 		    sizeof(*cookie_preserve));
   2672  1.1.2.2  skrll 		cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
   2673  1.1.2.2  skrll 		m->m_len += sizeof(*cookie_preserve);
   2674  1.1.2.2  skrll 		ecn = (struct sctp_ecn_supported_param *)(
   2675  1.1.2.2  skrll 		    (vaddr_t)cookie_preserve + sizeof(*cookie_preserve));
   2676  1.1.2.2  skrll 		stcb->asoc.cookie_preserve_req = 0;
   2677  1.1.2.2  skrll 	}
   2678  1.1.2.2  skrll 
   2679  1.1.2.2  skrll 	/* ECN parameter */
   2680  1.1.2.2  skrll 	if (sctp_ecn == 1) {
   2681  1.1.2.2  skrll 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
   2682  1.1.2.2  skrll 		ecn->ph.param_length = htons(sizeof(*ecn));
   2683  1.1.2.2  skrll 		m->m_len += sizeof(*ecn);
   2684  1.1.2.2  skrll 		prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn +
   2685  1.1.2.2  skrll 		    sizeof(*ecn));
   2686  1.1.2.2  skrll 	} else {
   2687  1.1.2.2  skrll 		prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn);
   2688  1.1.2.2  skrll 	}
   2689  1.1.2.2  skrll 	/* And now tell the peer we do pr-sctp */
   2690  1.1.2.2  skrll 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
   2691  1.1.2.2  skrll 	prsctp->ph.param_length = htons(sizeof(*prsctp));
   2692  1.1.2.2  skrll 	m->m_len += sizeof(*prsctp);
   2693  1.1.2.2  skrll 
   2694  1.1.2.2  skrll 
   2695  1.1.2.2  skrll 	/* And now tell the peer we do all the extensions */
   2696  1.1.2.2  skrll 	pr_supported = (struct sctp_supported_chunk_types_param *)((vaddr_t)prsctp +
   2697  1.1.2.2  skrll 	   sizeof(*prsctp));
   2698  1.1.2.2  skrll 
   2699  1.1.2.2  skrll 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
   2700  1.1.2.2  skrll 	pr_supported->ph.param_length = htons(sizeof(*pr_supported) + SCTP_EXT_COUNT);
   2701  1.1.2.2  skrll 	pr_supported->chunk_types[0] = SCTP_ASCONF;
   2702  1.1.2.2  skrll 	pr_supported->chunk_types[1] = SCTP_ASCONF_ACK;
   2703  1.1.2.2  skrll 	pr_supported->chunk_types[2] = SCTP_FORWARD_CUM_TSN;
   2704  1.1.2.2  skrll 	pr_supported->chunk_types[3] = SCTP_PACKET_DROPPED;
   2705  1.1.2.2  skrll 	pr_supported->chunk_types[4] = SCTP_STREAM_RESET;
   2706  1.1.2.2  skrll 	pr_supported->chunk_types[5] = 0; /* pad */
   2707  1.1.2.2  skrll 	pr_supported->chunk_types[6] = 0; /* pad */
   2708  1.1.2.2  skrll 	pr_supported->chunk_types[7] = 0; /* pad */
   2709  1.1.2.2  skrll 
   2710  1.1.2.2  skrll 	m->m_len += (sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
   2711  1.1.2.2  skrll 	/* ECN nonce: And now tell the peer we support ECN nonce */
   2712  1.1.2.2  skrll 
   2713  1.1.2.2  skrll 	if (sctp_ecn_nonce) {
   2714  1.1.2.2  skrll 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)((vaddr_t)pr_supported +
   2715  1.1.2.2  skrll 		    sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
   2716  1.1.2.2  skrll 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
   2717  1.1.2.2  skrll 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
   2718  1.1.2.2  skrll 		m->m_len += sizeof(*ecn_nonce);
   2719  1.1.2.2  skrll 	}
   2720  1.1.2.2  skrll 
   2721  1.1.2.2  skrll 	m_at = m;
   2722  1.1.2.2  skrll 	/* now the addresses */
   2723  1.1.2.2  skrll 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   2724  1.1.2.2  skrll 		struct ifnet *ifn;
   2725  1.1.2.2  skrll 		struct ifaddr *ifa;
   2726  1.1.2.2  skrll 		int cnt;
   2727  1.1.2.4  skrll 		int s;
   2728  1.1.2.2  skrll 
   2729  1.1.2.2  skrll 		cnt = cnt_inits_to;
   2730  1.1.2.4  skrll 		s = pserialize_read_enter();
   2731  1.1.2.4  skrll 		IFNET_READER_FOREACH(ifn) {
   2732  1.1.2.2  skrll 			if ((stcb->asoc.loopback_scope == 0) &&
   2733  1.1.2.2  skrll 			    (ifn->if_type == IFT_LOOP)) {
   2734  1.1.2.2  skrll 				/*
   2735  1.1.2.2  skrll 				 * Skip loopback devices if loopback_scope
   2736  1.1.2.2  skrll 				 * not set
   2737  1.1.2.2  skrll 				 */
   2738  1.1.2.2  skrll 				continue;
   2739  1.1.2.2  skrll 			}
   2740  1.1.2.5  skrll 			IFADDR_READER_FOREACH(ifa, ifn) {
   2741  1.1.2.2  skrll 				if (sctp_is_address_in_scope(ifa,
   2742  1.1.2.2  skrll 				    stcb->asoc.ipv4_addr_legal,
   2743  1.1.2.2  skrll 				    stcb->asoc.ipv6_addr_legal,
   2744  1.1.2.2  skrll 				    stcb->asoc.loopback_scope,
   2745  1.1.2.2  skrll 				    stcb->asoc.ipv4_local_scope,
   2746  1.1.2.2  skrll 				    stcb->asoc.local_scope,
   2747  1.1.2.2  skrll 				    stcb->asoc.site_scope) == 0) {
   2748  1.1.2.2  skrll 					continue;
   2749  1.1.2.2  skrll 				}
   2750  1.1.2.2  skrll 				cnt++;
   2751  1.1.2.2  skrll 			}
   2752  1.1.2.2  skrll 		}
   2753  1.1.2.4  skrll 		pserialize_read_exit(s);
   2754  1.1.2.4  skrll 
   2755  1.1.2.2  skrll 		if (cnt > 1) {
   2756  1.1.2.4  skrll 			s = pserialize_read_enter();
   2757  1.1.2.4  skrll 			IFNET_READER_FOREACH(ifn) {
   2758  1.1.2.2  skrll 				if ((stcb->asoc.loopback_scope == 0) &&
   2759  1.1.2.2  skrll 				    (ifn->if_type == IFT_LOOP)) {
   2760  1.1.2.2  skrll 					/*
   2761  1.1.2.2  skrll 					 * Skip loopback devices if loopback_scope
   2762  1.1.2.2  skrll 					 * not set
   2763  1.1.2.2  skrll 					 */
   2764  1.1.2.2  skrll 					continue;
   2765  1.1.2.2  skrll 				}
   2766  1.1.2.5  skrll 				IFADDR_READER_FOREACH(ifa, ifn) {
   2767  1.1.2.2  skrll 					if (sctp_is_address_in_scope(ifa,
   2768  1.1.2.2  skrll 					    stcb->asoc.ipv4_addr_legal,
   2769  1.1.2.2  skrll 					    stcb->asoc.ipv6_addr_legal,
   2770  1.1.2.2  skrll 					    stcb->asoc.loopback_scope,
   2771  1.1.2.2  skrll 					    stcb->asoc.ipv4_local_scope,
   2772  1.1.2.2  skrll 					    stcb->asoc.local_scope,
   2773  1.1.2.2  skrll 					    stcb->asoc.site_scope) == 0) {
   2774  1.1.2.2  skrll 						continue;
   2775  1.1.2.2  skrll 					}
   2776  1.1.2.2  skrll 					m_at = sctp_add_addr_to_mbuf(m_at, ifa);
   2777  1.1.2.2  skrll 				}
   2778  1.1.2.2  skrll 			}
   2779  1.1.2.4  skrll 			pserialize_read_exit(s);
   2780  1.1.2.2  skrll 		}
   2781  1.1.2.2  skrll 	} else {
   2782  1.1.2.2  skrll 		struct sctp_laddr *laddr;
   2783  1.1.2.2  skrll 		int cnt;
   2784  1.1.2.2  skrll 		cnt = cnt_inits_to;
   2785  1.1.2.2  skrll 		/* First, how many ? */
   2786  1.1.2.2  skrll 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   2787  1.1.2.2  skrll 			if (laddr->ifa == NULL) {
   2788  1.1.2.2  skrll 				continue;
   2789  1.1.2.2  skrll 			}
   2790  1.1.2.2  skrll 			if (laddr->ifa->ifa_addr == NULL)
   2791  1.1.2.2  skrll 				continue;
   2792  1.1.2.2  skrll 			if (sctp_is_address_in_scope(laddr->ifa,
   2793  1.1.2.2  skrll 			    stcb->asoc.ipv4_addr_legal,
   2794  1.1.2.2  skrll 			    stcb->asoc.ipv6_addr_legal,
   2795  1.1.2.2  skrll 			    stcb->asoc.loopback_scope,
   2796  1.1.2.2  skrll 			    stcb->asoc.ipv4_local_scope,
   2797  1.1.2.2  skrll 			    stcb->asoc.local_scope,
   2798  1.1.2.2  skrll 			    stcb->asoc.site_scope) == 0) {
   2799  1.1.2.2  skrll 				continue;
   2800  1.1.2.2  skrll 			}
   2801  1.1.2.2  skrll 			cnt++;
   2802  1.1.2.2  skrll 		}
   2803  1.1.2.2  skrll 		/* To get through a NAT we only list addresses if
   2804  1.1.2.2  skrll 		 * we have more than one. That way if you just
   2805  1.1.2.2  skrll 		 * bind a single address we let the source of the init
   2806  1.1.2.2  skrll 		 * dictate our address.
   2807  1.1.2.2  skrll 		 */
   2808  1.1.2.2  skrll 		if (cnt > 1) {
   2809  1.1.2.2  skrll 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   2810  1.1.2.2  skrll 				if (laddr->ifa == NULL) {
   2811  1.1.2.2  skrll 					continue;
   2812  1.1.2.2  skrll 				}
   2813  1.1.2.2  skrll 				if (laddr->ifa->ifa_addr == NULL) {
   2814  1.1.2.2  skrll 					continue;
   2815  1.1.2.2  skrll 				}
   2816  1.1.2.2  skrll 
   2817  1.1.2.2  skrll 				if (sctp_is_address_in_scope(laddr->ifa,
   2818  1.1.2.2  skrll 				    stcb->asoc.ipv4_addr_legal,
   2819  1.1.2.2  skrll 				    stcb->asoc.ipv6_addr_legal,
   2820  1.1.2.2  skrll 				    stcb->asoc.loopback_scope,
   2821  1.1.2.2  skrll 				    stcb->asoc.ipv4_local_scope,
   2822  1.1.2.2  skrll 				    stcb->asoc.local_scope,
   2823  1.1.2.2  skrll 				    stcb->asoc.site_scope) == 0) {
   2824  1.1.2.2  skrll 					continue;
   2825  1.1.2.2  skrll 				}
   2826  1.1.2.2  skrll 				m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
   2827  1.1.2.2  skrll 			}
   2828  1.1.2.2  skrll 		}
   2829  1.1.2.2  skrll 	}
   2830  1.1.2.2  skrll 	/* calulate the size and update pkt header and chunk header */
   2831  1.1.2.2  skrll 	m->m_pkthdr.len = 0;
   2832  1.1.2.2  skrll 	for (m_at = m; m_at; m_at = m_at->m_next) {
   2833  1.1.2.2  skrll 		if (m_at->m_next == NULL)
   2834  1.1.2.2  skrll 			m_last = m_at;
   2835  1.1.2.2  skrll 		m->m_pkthdr.len += m_at->m_len;
   2836  1.1.2.2  skrll 	}
   2837  1.1.2.2  skrll 	initm->msg.ch.chunk_length = htons((m->m_pkthdr.len -
   2838  1.1.2.2  skrll 	    sizeof(struct sctphdr)));
   2839  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2840  1.1.2.2  skrll 	printf("chunk_length %d\n", ntohs(initm->msg.ch.chunk_length));
   2841  1.1.2.2  skrll #endif
   2842  1.1.2.2  skrll 	/* We pass 0 here to NOT set IP_DF if its IPv4, we
   2843  1.1.2.2  skrll 	 * ignore the return here since the timer will drive
   2844  1.1.2.2  skrll 	 * a retranmission.
   2845  1.1.2.2  skrll 	 */
   2846  1.1.2.2  skrll 
   2847  1.1.2.2  skrll 	/* I don't expect this to execute but we will be safe here */
   2848  1.1.2.2  skrll 	padval = m->m_pkthdr.len % 4;
   2849  1.1.2.2  skrll 	if ((padval) && (m_last)) {
   2850  1.1.2.2  skrll 		/* The compiler worries that m_last may not be
   2851  1.1.2.2  skrll 		 * set even though I think it is impossible :->
   2852  1.1.2.2  skrll 		 * however we add m_last here just in case.
   2853  1.1.2.2  skrll 		 */
   2854  1.1.2.2  skrll 		ret = sctp_add_pad_tombuf(m_last, (4-padval));
   2855  1.1.2.2  skrll 		if (ret) {
   2856  1.1.2.2  skrll 			/* Houston we have a problem, no space */
   2857  1.1.2.2  skrll 			sctp_m_freem(m);
   2858  1.1.2.2  skrll 			return;
   2859  1.1.2.2  skrll 		}
   2860  1.1.2.2  skrll 		m->m_pkthdr.len += padval;
   2861  1.1.2.2  skrll 	}
   2862  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2863  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   2864  1.1.2.2  skrll 		printf("Calling lowlevel output stcb:%p net:%p\n",
   2865  1.1.2.2  skrll 		       stcb, net);
   2866  1.1.2.2  skrll 	}
   2867  1.1.2.2  skrll #endif
   2868  1.1.2.2  skrll 	ret = sctp_lowlevel_chunk_output(inp, stcb, net,
   2869  1.1.2.2  skrll 		  rtcache_getdst(&net->ro), m, 0, 0, NULL, 0);
   2870  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2871  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   2872  1.1.2.2  skrll 		printf("Low level output returns %d\n", ret);
   2873  1.1.2.2  skrll 	}
   2874  1.1.2.2  skrll #endif
   2875  1.1.2.2  skrll 	sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
   2876  1.1.2.2  skrll 	SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
   2877  1.1.2.2  skrll }
   2878  1.1.2.2  skrll 
   2879  1.1.2.2  skrll struct mbuf *
   2880  1.1.2.2  skrll sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
   2881  1.1.2.2  skrll     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp)
   2882  1.1.2.2  skrll {
   2883  1.1.2.2  skrll 	/* Given a mbuf containing an INIT or INIT-ACK
   2884  1.1.2.2  skrll 	 * with the param_offset being equal to the
   2885  1.1.2.2  skrll 	 * beginning of the params i.e. (iphlen + sizeof(struct sctp_init_msg)
   2886  1.1.2.2  skrll 	 * parse through the parameters to the end of the mbuf verifying
   2887  1.1.2.2  skrll 	 * that all parameters are known.
   2888  1.1.2.2  skrll 	 *
   2889  1.1.2.2  skrll 	 * For unknown parameters build and return a mbuf with
   2890  1.1.2.2  skrll 	 * UNRECOGNIZED_PARAMETER errors. If the flags indicate
   2891  1.1.2.2  skrll 	 * to stop processing this chunk stop, and set *abort_processing
   2892  1.1.2.2  skrll 	 * to 1.
   2893  1.1.2.2  skrll 	 *
   2894  1.1.2.2  skrll 	 * By having param_offset be pre-set to where parameters begin
   2895  1.1.2.2  skrll 	 * it is hoped that this routine may be reused in the future
   2896  1.1.2.2  skrll 	 * by new features.
   2897  1.1.2.2  skrll 	 */
   2898  1.1.2.2  skrll 	struct sctp_paramhdr *phdr, params;
   2899  1.1.2.2  skrll 
   2900  1.1.2.2  skrll 	struct mbuf *mat, *op_err;
   2901  1.1.2.2  skrll 	char tempbuf[2048];
   2902  1.1.2.2  skrll 	int at, limit, pad_needed;
   2903  1.1.2.2  skrll 	uint16_t ptype, plen;
   2904  1.1.2.2  skrll 	int err_at;
   2905  1.1.2.2  skrll 
   2906  1.1.2.2  skrll 	*abort_processing = 0;
   2907  1.1.2.2  skrll 	mat = in_initpkt;
   2908  1.1.2.2  skrll 	err_at = 0;
   2909  1.1.2.2  skrll 	limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
   2910  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2911  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   2912  1.1.2.2  skrll 		printf("Limit is %d bytes\n", limit);
   2913  1.1.2.2  skrll 	}
   2914  1.1.2.2  skrll #endif
   2915  1.1.2.2  skrll 	at = param_offset;
   2916  1.1.2.2  skrll 	op_err = NULL;
   2917  1.1.2.2  skrll 
   2918  1.1.2.2  skrll 	phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
   2919  1.1.2.2  skrll 	while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
   2920  1.1.2.2  skrll 		ptype = ntohs(phdr->param_type);
   2921  1.1.2.2  skrll 		plen = ntohs(phdr->param_length);
   2922  1.1.2.2  skrll 		limit -= SCTP_SIZE32(plen);
   2923  1.1.2.2  skrll 		if (plen < sizeof(struct sctp_paramhdr)) {
   2924  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2925  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   2926  1.1.2.2  skrll 			printf("sctp_output.c:Impossible length in parameter < %d\n", plen);
   2927  1.1.2.2  skrll 	}
   2928  1.1.2.2  skrll #endif
   2929  1.1.2.2  skrll 			*abort_processing = 1;
   2930  1.1.2.2  skrll 			break;
   2931  1.1.2.2  skrll 		}
   2932  1.1.2.2  skrll 		/* All parameters for all chunks that we
   2933  1.1.2.2  skrll 		 * know/understand are listed here. We process
   2934  1.1.2.2  skrll 		 * them other places and make appropriate
   2935  1.1.2.2  skrll 		 * stop actions per the upper bits. However
   2936  1.1.2.2  skrll 		 * this is the generic routine processor's can
   2937  1.1.2.2  skrll 		 * call to get back an operr.. to either incorporate (init-ack)
   2938  1.1.2.2  skrll 		 * or send.
   2939  1.1.2.2  skrll 		 */
   2940  1.1.2.2  skrll 		if ((ptype == SCTP_HEARTBEAT_INFO) ||
   2941  1.1.2.2  skrll 		    (ptype == SCTP_IPV4_ADDRESS) ||
   2942  1.1.2.2  skrll 		    (ptype == SCTP_IPV6_ADDRESS) ||
   2943  1.1.2.2  skrll 		    (ptype == SCTP_STATE_COOKIE) ||
   2944  1.1.2.2  skrll 		    (ptype == SCTP_UNRECOG_PARAM) ||
   2945  1.1.2.2  skrll 		    (ptype == SCTP_COOKIE_PRESERVE) ||
   2946  1.1.2.2  skrll 		    (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
   2947  1.1.2.2  skrll 		    (ptype == SCTP_PRSCTP_SUPPORTED) ||
   2948  1.1.2.2  skrll 		    (ptype == SCTP_ADD_IP_ADDRESS) ||
   2949  1.1.2.2  skrll 		    (ptype == SCTP_DEL_IP_ADDRESS) ||
   2950  1.1.2.2  skrll 		    (ptype == SCTP_ECN_CAPABLE) ||
   2951  1.1.2.2  skrll 		    (ptype == SCTP_ULP_ADAPTION) ||
   2952  1.1.2.2  skrll 		    (ptype == SCTP_ERROR_CAUSE_IND) ||
   2953  1.1.2.2  skrll 		    (ptype == SCTP_SET_PRIM_ADDR) ||
   2954  1.1.2.2  skrll 		    (ptype == SCTP_SUCCESS_REPORT) ||
   2955  1.1.2.2  skrll 		    (ptype == SCTP_ULP_ADAPTION) ||
   2956  1.1.2.2  skrll 		    (ptype == SCTP_SUPPORTED_CHUNK_EXT) ||
   2957  1.1.2.2  skrll 		    (ptype == SCTP_ECN_NONCE_SUPPORTED)
   2958  1.1.2.2  skrll 			) {
   2959  1.1.2.2  skrll 			/* no skip it */
   2960  1.1.2.2  skrll 			at += SCTP_SIZE32(plen);
   2961  1.1.2.2  skrll 		} else if (ptype == SCTP_HOSTNAME_ADDRESS) {
   2962  1.1.2.2  skrll 			/* We can NOT handle HOST NAME addresses!! */
   2963  1.1.2.2  skrll #ifdef SCTP_DEBUG
   2964  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   2965  1.1.2.2  skrll 		printf("Can't handle hostname addresses.. abort processing\n");
   2966  1.1.2.2  skrll 	}
   2967  1.1.2.2  skrll #endif
   2968  1.1.2.2  skrll 			*abort_processing = 1;
   2969  1.1.2.2  skrll 			if (op_err == NULL) {
   2970  1.1.2.2  skrll 				/* Ok need to try to get a mbuf */
   2971  1.1.2.2  skrll 				MGETHDR(op_err, M_DONTWAIT, MT_DATA);
   2972  1.1.2.2  skrll 				if (op_err) {
   2973  1.1.2.2  skrll 					op_err->m_len = 0;
   2974  1.1.2.2  skrll 					op_err->m_pkthdr.len = 0;
   2975  1.1.2.2  skrll 					/* pre-reserve space for ip and sctp header  and chunk hdr*/
   2976  1.1.2.2  skrll 					op_err->m_data += sizeof(struct ip6_hdr);
   2977  1.1.2.2  skrll 					op_err->m_data += sizeof(struct sctphdr);
   2978  1.1.2.2  skrll 					op_err->m_data += sizeof(struct sctp_chunkhdr);
   2979  1.1.2.2  skrll 				}
   2980  1.1.2.2  skrll 			}
   2981  1.1.2.2  skrll 			if (op_err) {
   2982  1.1.2.2  skrll 				/* If we have space */
   2983  1.1.2.2  skrll 				struct sctp_paramhdr s;
   2984  1.1.2.2  skrll 				if (err_at % 4) {
   2985  1.1.2.2  skrll 					u_int32_t cpthis=0;
   2986  1.1.2.2  skrll 					pad_needed = 4 - (err_at % 4);
   2987  1.1.2.2  skrll 					m_copyback(op_err, err_at, pad_needed, (void *)&cpthis);
   2988  1.1.2.2  skrll 					err_at += pad_needed;
   2989  1.1.2.2  skrll 				}
   2990  1.1.2.2  skrll 				s.param_type = htons(SCTP_CAUSE_UNRESOLV_ADDR);
   2991  1.1.2.2  skrll 				s.param_length = htons(sizeof(s) + plen);
   2992  1.1.2.2  skrll 				m_copyback(op_err, err_at, sizeof(s), (void *)&s);
   2993  1.1.2.2  skrll 				err_at += sizeof(s);
   2994  1.1.2.2  skrll 				phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, plen);
   2995  1.1.2.2  skrll 				if (phdr == NULL) {
   2996  1.1.2.2  skrll 					sctp_m_freem(op_err);
   2997  1.1.2.2  skrll 					/* we are out of memory but we
   2998  1.1.2.2  skrll 					 * still need to have a look at what to
   2999  1.1.2.2  skrll 					 * do (the system is in trouble though).
   3000  1.1.2.2  skrll 					 */
   3001  1.1.2.2  skrll 					return (NULL);
   3002  1.1.2.2  skrll 				}
   3003  1.1.2.2  skrll 				m_copyback(op_err, err_at, plen, (void *)phdr);
   3004  1.1.2.2  skrll 				err_at += plen;
   3005  1.1.2.2  skrll 			}
   3006  1.1.2.2  skrll 			return (op_err);
   3007  1.1.2.2  skrll 		} else {
   3008  1.1.2.2  skrll 			/* we do not recognize the parameter
   3009  1.1.2.2  skrll 			 * figure out what we do.
   3010  1.1.2.2  skrll 			 */
   3011  1.1.2.2  skrll #ifdef SCTP_DEBUG
   3012  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   3013  1.1.2.2  skrll 				printf("Got parameter type %x - unknown\n",
   3014  1.1.2.2  skrll 				       (u_int)ptype);
   3015  1.1.2.2  skrll 			}
   3016  1.1.2.2  skrll #endif
   3017  1.1.2.2  skrll 			if ((ptype & 0x4000) == 0x4000) {
   3018  1.1.2.2  skrll 				/* Report bit is set?? */
   3019  1.1.2.2  skrll #ifdef SCTP_DEBUG
   3020  1.1.2.2  skrll 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   3021  1.1.2.2  skrll 					printf("Report bit is set\n");
   3022  1.1.2.2  skrll 				}
   3023  1.1.2.2  skrll #endif
   3024  1.1.2.2  skrll 				if (op_err == NULL) {
   3025  1.1.2.2  skrll 					/* Ok need to try to get an mbuf */
   3026  1.1.2.2  skrll 					MGETHDR(op_err, M_DONTWAIT, MT_DATA);
   3027  1.1.2.2  skrll 					if (op_err) {
   3028  1.1.2.2  skrll 						op_err->m_len = 0;
   3029  1.1.2.2  skrll 						op_err->m_pkthdr.len = 0;
   3030  1.1.2.2  skrll 						op_err->m_data += sizeof(struct ip6_hdr);
   3031  1.1.2.2  skrll 						op_err->m_data += sizeof(struct sctphdr);
   3032  1.1.2.2  skrll 						op_err->m_data += sizeof(struct sctp_chunkhdr);
   3033  1.1.2.2  skrll 					}
   3034  1.1.2.2  skrll 				}
   3035  1.1.2.2  skrll 				if (op_err) {
   3036  1.1.2.2  skrll 					/* If we have space */
   3037  1.1.2.2  skrll 					struct sctp_paramhdr s;
   3038  1.1.2.2  skrll 					if (err_at % 4) {
   3039  1.1.2.2  skrll 						u_int32_t cpthis=0;
   3040  1.1.2.2  skrll 						pad_needed = 4 - (err_at % 4);
   3041  1.1.2.2  skrll 						m_copyback(op_err, err_at, pad_needed, (void *)&cpthis);
   3042  1.1.2.2  skrll 						err_at += pad_needed;
   3043  1.1.2.2  skrll  					}
   3044  1.1.2.2  skrll 					s.param_type = htons(SCTP_UNRECOG_PARAM);
   3045  1.1.2.2  skrll 					s.param_length = htons(sizeof(s) + plen);
   3046  1.1.2.2  skrll 					m_copyback(op_err, err_at, sizeof(s), (void *)&s);
   3047  1.1.2.2  skrll 					err_at += sizeof(s);
   3048  1.1.2.2  skrll 					if (plen > sizeof(tempbuf)) {
   3049  1.1.2.2  skrll 						plen = sizeof(tempbuf);
   3050  1.1.2.2  skrll 					}
   3051  1.1.2.2  skrll 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, plen);
   3052  1.1.2.2  skrll 					if (phdr == NULL) {
   3053  1.1.2.2  skrll 						sctp_m_freem(op_err);
   3054  1.1.2.2  skrll 						/* we are out of memory but we
   3055  1.1.2.2  skrll 						 * still need to have a look at what to
   3056  1.1.2.2  skrll 						 * do (the system is in trouble though).
   3057  1.1.2.2  skrll 						 */
   3058  1.1.2.2  skrll 						goto more_processing;
   3059  1.1.2.2  skrll 					}
   3060  1.1.2.2  skrll 					m_copyback(op_err, err_at, plen, (void *)phdr);
   3061  1.1.2.2  skrll 					err_at += plen;
   3062  1.1.2.2  skrll 				}
   3063  1.1.2.2  skrll 			}
   3064  1.1.2.2  skrll 		more_processing:
   3065  1.1.2.2  skrll 			if ((ptype & 0x8000) == 0x0000) {
   3066  1.1.2.2  skrll #ifdef SCTP_DEBUG
   3067  1.1.2.2  skrll 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   3068  1.1.2.2  skrll 					printf("Abort bit is now setting1\n");
   3069  1.1.2.2  skrll 				}
   3070  1.1.2.2  skrll #endif
   3071  1.1.2.2  skrll 				return (op_err);
   3072  1.1.2.2  skrll 			} else {
   3073  1.1.2.2  skrll 				/* skip this chunk and continue processing */
   3074  1.1.2.2  skrll 				at += SCTP_SIZE32(plen);
   3075  1.1.2.2  skrll 			}
   3076  1.1.2.2  skrll 
   3077  1.1.2.2  skrll 		}
   3078  1.1.2.2  skrll 		phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
   3079  1.1.2.2  skrll 	}
   3080  1.1.2.2  skrll 	return (op_err);
   3081  1.1.2.2  skrll }
   3082  1.1.2.2  skrll 
   3083  1.1.2.2  skrll static int
   3084  1.1.2.2  skrll sctp_are_there_new_addresses(struct sctp_association *asoc,
   3085  1.1.2.2  skrll     struct mbuf *in_initpkt, int iphlen, int offset)
   3086  1.1.2.2  skrll {
   3087  1.1.2.2  skrll 	/*
   3088  1.1.2.2  skrll 	 * Given a INIT packet, look through the packet to verify that
   3089  1.1.2.2  skrll 	 * there are NO new addresses. As we go through the parameters
   3090  1.1.2.2  skrll 	 * add reports of any un-understood parameters that require an
   3091  1.1.2.2  skrll 	 * error.  Also we must return (1) to drop the packet if we see
   3092  1.1.2.2  skrll 	 * a un-understood parameter that tells us to drop the chunk.
   3093  1.1.2.2  skrll 	 */
   3094  1.1.2.2  skrll 	struct sockaddr_in sin4, *sa4;
   3095  1.1.2.2  skrll 	struct sockaddr_in6 sin6, *sa6;
   3096  1.1.2.2  skrll 	struct sockaddr *sa_touse;
   3097  1.1.2.2  skrll 	struct sockaddr *sa;
   3098  1.1.2.2  skrll 	struct sctp_paramhdr *phdr, params;
   3099  1.1.2.2  skrll 	struct ip *iph;
   3100  1.1.2.2  skrll 	struct mbuf *mat;
   3101  1.1.2.2  skrll 	uint16_t ptype, plen;
   3102  1.1.2.2  skrll 	uint8_t fnd;
   3103  1.1.2.2  skrll 	struct sctp_nets *net;
   3104  1.1.2.2  skrll 
   3105  1.1.2.2  skrll 	memset(&sin4, 0, sizeof(sin4));
   3106  1.1.2.2  skrll 	memset(&sin6, 0, sizeof(sin6));
   3107  1.1.2.2  skrll 	sin4.sin_family = AF_INET;
   3108  1.1.2.2  skrll 	sin4.sin_len = sizeof(sin4);
   3109  1.1.2.2  skrll 	sin6.sin6_family = AF_INET6;
   3110  1.1.2.2  skrll 	sin6.sin6_len = sizeof(sin6);
   3111  1.1.2.2  skrll 
   3112  1.1.2.2  skrll 	sa_touse = NULL;
   3113  1.1.2.2  skrll 	/* First what about the src address of the pkt ? */
   3114  1.1.2.2  skrll 	iph = mtod(in_initpkt, struct ip *);
   3115  1.1.2.2  skrll 	if (iph->ip_v == IPVERSION) {
   3116  1.1.2.2  skrll 		/* source addr is IPv4 */
   3117  1.1.2.2  skrll 		sin4.sin_addr = iph->ip_src;
   3118  1.1.2.2  skrll 		sa_touse = (struct sockaddr *)&sin4;
   3119  1.1.2.2  skrll 	} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
   3120  1.1.2.2  skrll 		/* source addr is IPv6 */
   3121  1.1.2.2  skrll 		struct ip6_hdr *ip6h;
   3122  1.1.2.2  skrll 		ip6h = mtod(in_initpkt, struct ip6_hdr *);
   3123  1.1.2.2  skrll 		sin6.sin6_addr = ip6h->ip6_src;
   3124  1.1.2.2  skrll 		sa_touse = (struct sockaddr *)&sin6;
   3125  1.1.2.2  skrll 	} else {
   3126  1.1.2.2  skrll 		return (1);
   3127  1.1.2.2  skrll 	}
   3128  1.1.2.2  skrll 
   3129  1.1.2.2  skrll 	fnd = 0;
   3130  1.1.2.2  skrll 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   3131  1.1.2.2  skrll 		sa = (struct sockaddr *)&net->ro.ro_sa;
   3132  1.1.2.2  skrll 		if (sa->sa_family == sa_touse->sa_family) {
   3133  1.1.2.2  skrll 			if (sa->sa_family == AF_INET) {
   3134  1.1.2.2  skrll 			    sa4 = (struct sockaddr_in *)sa;
   3135  1.1.2.2  skrll 				if (sa4->sin_addr.s_addr ==
   3136  1.1.2.2  skrll 				    sin4.sin_addr.s_addr) {
   3137  1.1.2.2  skrll 					fnd = 1;
   3138  1.1.2.2  skrll 					break;
   3139  1.1.2.2  skrll 				}
   3140  1.1.2.2  skrll 			} else if (sa->sa_family == AF_INET6) {
   3141  1.1.2.2  skrll 				sa6 = (struct sockaddr_in6 *)sa;
   3142  1.1.2.2  skrll 				if (SCTP6_ARE_ADDR_EQUAL(&sa6->sin6_addr,
   3143  1.1.2.2  skrll 				    &sin6.sin6_addr)) {
   3144  1.1.2.2  skrll 					fnd = 1;
   3145  1.1.2.2  skrll 					break;
   3146  1.1.2.2  skrll 				}
   3147  1.1.2.2  skrll 			}
   3148  1.1.2.2  skrll 		}
   3149  1.1.2.2  skrll 	}
   3150  1.1.2.2  skrll 	if (fnd == 0) {
   3151  1.1.2.2  skrll 		/* New address added! no need to look futher. */
   3152  1.1.2.2  skrll 		return (1);
   3153  1.1.2.2  skrll 	}
   3154  1.1.2.2  skrll 	/* Ok so far lets munge through the rest of the packet */
   3155  1.1.2.2  skrll 	mat = in_initpkt;
   3156  1.1.2.2  skrll 	sa_touse = NULL;
   3157  1.1.2.2  skrll 	offset += sizeof(struct sctp_init_chunk);
   3158  1.1.2.2  skrll 	phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
   3159  1.1.2.2  skrll 	while (phdr) {
   3160  1.1.2.2  skrll 		ptype = ntohs(phdr->param_type);
   3161  1.1.2.2  skrll 		plen = ntohs(phdr->param_length);
   3162  1.1.2.2  skrll 		if (ptype == SCTP_IPV4_ADDRESS) {
   3163  1.1.2.2  skrll 			struct sctp_ipv4addr_param *p4, p4_buf;
   3164  1.1.2.2  skrll 
   3165  1.1.2.2  skrll 			phdr = sctp_get_next_param(mat, offset,
   3166  1.1.2.2  skrll 			    (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
   3167  1.1.2.2  skrll 			if (plen != sizeof(struct sctp_ipv4addr_param) ||
   3168  1.1.2.2  skrll 			    phdr == NULL) {
   3169  1.1.2.2  skrll                                 return (1);
   3170  1.1.2.2  skrll                         }
   3171  1.1.2.2  skrll 			p4 = (struct sctp_ipv4addr_param *)phdr;
   3172  1.1.2.2  skrll 			sin4.sin_addr.s_addr = p4->addr;
   3173  1.1.2.2  skrll 			sa_touse = (struct sockaddr *)&sin4;
   3174  1.1.2.2  skrll 		} else if (ptype == SCTP_IPV6_ADDRESS) {
   3175  1.1.2.2  skrll 			struct sctp_ipv6addr_param *p6, p6_buf;
   3176  1.1.2.2  skrll 
   3177  1.1.2.2  skrll 			phdr = sctp_get_next_param(mat, offset,
   3178  1.1.2.2  skrll 			    (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
   3179  1.1.2.2  skrll 			if (plen != sizeof(struct sctp_ipv6addr_param) ||
   3180  1.1.2.2  skrll 			    phdr == NULL) {
   3181  1.1.2.2  skrll                                 return (1);
   3182  1.1.2.2  skrll                         }
   3183  1.1.2.2  skrll 			p6 = (struct sctp_ipv6addr_param *)phdr;
   3184  1.1.2.2  skrll 			memcpy((void *)&sin6.sin6_addr, p6->addr,
   3185  1.1.2.2  skrll 			    sizeof(p6->addr));
   3186  1.1.2.2  skrll 			sa_touse = (struct sockaddr *)&sin4;
   3187  1.1.2.2  skrll 		}
   3188  1.1.2.2  skrll 
   3189  1.1.2.2  skrll 		if (sa_touse) {
   3190  1.1.2.2  skrll 			/* ok, sa_touse points to one to check */
   3191  1.1.2.2  skrll 			fnd = 0;
   3192  1.1.2.2  skrll 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   3193  1.1.2.2  skrll 				sa = (struct sockaddr *)&net->ro.ro_sa;
   3194  1.1.2.2  skrll 				if (sa->sa_family != sa_touse->sa_family) {
   3195  1.1.2.2  skrll 					continue;
   3196  1.1.2.2  skrll 				}
   3197  1.1.2.2  skrll 				if (sa->sa_family == AF_INET) {
   3198  1.1.2.2  skrll 					sa4 = (struct sockaddr_in *)sa;
   3199  1.1.2.2  skrll 					if (sa4->sin_addr.s_addr ==
   3200  1.1.2.2  skrll 					    sin4.sin_addr.s_addr) {
   3201  1.1.2.2  skrll 						fnd = 1;
   3202  1.1.2.2  skrll 						break;
   3203  1.1.2.2  skrll 					}
   3204  1.1.2.2  skrll 				} else if (sa->sa_family == AF_INET6) {
   3205  1.1.2.2  skrll 					sa6 = (struct sockaddr_in6 *)sa;
   3206  1.1.2.2  skrll 					if (SCTP6_ARE_ADDR_EQUAL(
   3207  1.1.2.2  skrll 					    &sa6->sin6_addr, &sin6.sin6_addr)) {
   3208  1.1.2.2  skrll 						fnd = 1;
   3209  1.1.2.2  skrll 						break;
   3210  1.1.2.2  skrll 					}
   3211  1.1.2.2  skrll 				}
   3212  1.1.2.2  skrll 			}
   3213  1.1.2.2  skrll 			if (!fnd) {
   3214  1.1.2.2  skrll 				/* New addr added! no need to look further */
   3215  1.1.2.2  skrll 				return (1);
   3216  1.1.2.2  skrll 			}
   3217  1.1.2.2  skrll 		}
   3218  1.1.2.2  skrll 		offset += SCTP_SIZE32(plen);
   3219  1.1.2.2  skrll 		phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
   3220  1.1.2.2  skrll 	}
   3221  1.1.2.2  skrll 	return (0);
   3222  1.1.2.2  skrll }
   3223  1.1.2.2  skrll 
   3224  1.1.2.2  skrll /*
   3225  1.1.2.2  skrll  * Given a MBUF chain that was sent into us containing an
   3226  1.1.2.2  skrll  * INIT. Build a INIT-ACK with COOKIE and send back.
   3227  1.1.2.2  skrll  * We assume that the in_initpkt has done a pullup to
   3228  1.1.2.2  skrll  * include IPv6/4header, SCTP header and initial part of
   3229  1.1.2.2  skrll  * INIT message (i.e. the struct sctp_init_msg).
   3230  1.1.2.2  skrll  */
   3231  1.1.2.2  skrll void
   3232  1.1.2.2  skrll sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
   3233  1.1.2.2  skrll     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
   3234  1.1.2.2  skrll     struct sctp_init_chunk *init_chk)
   3235  1.1.2.2  skrll {
   3236  1.1.2.2  skrll 	struct sctp_association *asoc;
   3237  1.1.2.2  skrll 	struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *m_last;
   3238  1.1.2.2  skrll 	struct sctp_init_msg *initackm_out;
   3239  1.1.2.2  skrll 	struct sctp_ecn_supported_param *ecn;
   3240  1.1.2.2  skrll 	struct sctp_prsctp_supported_param *prsctp;
   3241  1.1.2.2  skrll 	struct sctp_ecn_nonce_supported_param *ecn_nonce;
   3242  1.1.2.2  skrll 	struct sctp_supported_chunk_types_param *pr_supported;
   3243  1.1.2.2  skrll 	struct sockaddr_storage store;
   3244  1.1.2.2  skrll 	struct sockaddr_in *sin;
   3245  1.1.2.2  skrll 	struct sockaddr_in6 *sin6;
   3246  1.1.2.2  skrll 	struct route *ro;
   3247  1.1.2.2  skrll 	struct ip *iph;
   3248  1.1.2.2  skrll 	struct ip6_hdr *ip6;
   3249  1.1.2.2  skrll 	const struct sockaddr *to;
   3250  1.1.2.2  skrll 	struct sctp_state_cookie stc;
   3251  1.1.2.2  skrll 	struct sctp_nets *net=NULL;
   3252  1.1.2.2  skrll 	int cnt_inits_to=0;
   3253  1.1.2.2  skrll 	uint16_t his_limit, i_want;
   3254  1.1.2.2  skrll 	int abort_flag, padval, sz_of;
   3255  1.1.2.2  skrll 
   3256  1.1.2.2  skrll 	if (stcb) {
   3257  1.1.2.2  skrll 		asoc = &stcb->asoc;
   3258  1.1.2.2  skrll 	} else {
   3259  1.1.2.2  skrll 		asoc = NULL;
   3260  1.1.2.2  skrll 	}
   3261  1.1.2.2  skrll 	m_last = NULL;
   3262  1.1.2.2  skrll 	if ((asoc != NULL) &&
   3263  1.1.2.2  skrll 	    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
   3264  1.1.2.2  skrll 	    (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
   3265  1.1.2.2  skrll 		/* new addresses, out of here in non-cookie-wait states */
   3266  1.1.2.2  skrll 		/*
   3267  1.1.2.2  skrll 		 * Send a ABORT, we don't add the new address error clause though
   3268  1.1.2.2  skrll 		 * we even set the T bit and copy in the 0 tag.. this looks no
   3269  1.1.2.2  skrll 		 * different than if no listner was present.
   3270  1.1.2.2  skrll 		 */
   3271  1.1.2.2  skrll 		sctp_send_abort(init_pkt, iphlen, sh, 0, NULL);
   3272  1.1.2.2  skrll 		return;
   3273  1.1.2.2  skrll 	}
   3274  1.1.2.2  skrll 	abort_flag = 0;
   3275  1.1.2.2  skrll 	op_err = sctp_arethere_unrecognized_parameters(init_pkt,
   3276  1.1.2.2  skrll 	    (offset+sizeof(struct sctp_init_chunk)),
   3277  1.1.2.2  skrll 	    &abort_flag, (struct sctp_chunkhdr *)init_chk);
   3278  1.1.2.2  skrll 	if (abort_flag) {
   3279  1.1.2.2  skrll 		sctp_send_abort(init_pkt, iphlen, sh, init_chk->init.initiate_tag, op_err);
   3280  1.1.2.2  skrll 		return;
   3281  1.1.2.2  skrll 	}
   3282  1.1.2.2  skrll 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
   3283  1.1.2.2  skrll 	if (m == NULL) {
   3284  1.1.2.2  skrll 		/* No memory, INIT timer will re-attempt. */
   3285  1.1.2.2  skrll 		if (op_err)
   3286  1.1.2.2  skrll 			sctp_m_freem(op_err);
   3287  1.1.2.2  skrll 		return;
   3288  1.1.2.2  skrll 	}
   3289  1.1.2.2  skrll 	MCLGET(m, M_DONTWAIT);
   3290  1.1.2.2  skrll 	if ((m->m_flags & M_EXT) != M_EXT) {
   3291  1.1.2.2  skrll 		/* Failed to get cluster buffer */
   3292  1.1.2.2  skrll 		if (op_err)
   3293  1.1.2.2  skrll 			sctp_m_freem(op_err);
   3294  1.1.2.2  skrll 		sctp_m_freem(m);
   3295  1.1.2.2  skrll 		return;
   3296  1.1.2.2  skrll 	}
   3297  1.1.2.2  skrll 	m->m_data += SCTP_MIN_OVERHEAD;
   3298  1.1.2.5  skrll 	m_reset_rcvif(m);
   3299  1.1.2.2  skrll 	m->m_len = sizeof(struct sctp_init_msg);
   3300  1.1.2.2  skrll 
   3301  1.1.2.2  skrll 	/* the time I built cookie */
   3302  1.1.2.2  skrll 	SCTP_GETTIME_TIMEVAL(&stc.time_entered);
   3303  1.1.2.2  skrll 
   3304  1.1.2.2  skrll 	/* populate any tie tags */
   3305  1.1.2.2  skrll 	if (asoc != NULL) {
   3306  1.1.2.2  skrll 		/* unlock before tag selections */
   3307  1.1.2.2  skrll 		SCTP_TCB_UNLOCK(stcb);
   3308  1.1.2.2  skrll 		if (asoc->my_vtag_nonce == 0)
   3309  1.1.2.2  skrll 			asoc->my_vtag_nonce = sctp_select_a_tag(inp);
   3310  1.1.2.2  skrll 		stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
   3311  1.1.2.2  skrll 
   3312  1.1.2.2  skrll 		if (asoc->peer_vtag_nonce == 0)
   3313  1.1.2.2  skrll 			asoc->peer_vtag_nonce = sctp_select_a_tag(inp);
   3314  1.1.2.2  skrll 		stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
   3315  1.1.2.2  skrll 
   3316  1.1.2.2  skrll 		stc.cookie_life = asoc->cookie_life;
   3317  1.1.2.2  skrll 		net = asoc->primary_destination;
   3318  1.1.2.2  skrll 		/* now we must relock */
   3319  1.1.2.2  skrll 		SCTP_INP_RLOCK(inp);
   3320  1.1.2.2  skrll 		/* we may be in trouble here if the inp got freed
   3321  1.1.2.2  skrll 		 * most likely this set of tests will protect
   3322  1.1.2.2  skrll 		 * us but there is a chance not.
   3323  1.1.2.2  skrll 		 */
   3324  1.1.2.2  skrll 		if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
   3325  1.1.2.2  skrll 			if (op_err)
   3326  1.1.2.2  skrll 				sctp_m_freem(op_err);
   3327  1.1.2.2  skrll 			sctp_m_freem(m);
   3328  1.1.2.2  skrll 			sctp_send_abort(init_pkt, iphlen, sh, 0, NULL);
   3329  1.1.2.2  skrll 			return;
   3330  1.1.2.2  skrll 		}
   3331  1.1.2.2  skrll 		SCTP_TCB_LOCK(stcb);
   3332  1.1.2.2  skrll 		SCTP_INP_RUNLOCK(stcb->sctp_ep);
   3333  1.1.2.2  skrll 	} else {
   3334  1.1.2.2  skrll 		stc.tie_tag_my_vtag = 0;
   3335  1.1.2.2  skrll 		stc.tie_tag_peer_vtag = 0;
   3336  1.1.2.2  skrll 		/* life I will award this cookie */
   3337  1.1.2.2  skrll 		stc.cookie_life = inp->sctp_ep.def_cookie_life;
   3338  1.1.2.2  skrll 	}
   3339  1.1.2.2  skrll 
   3340  1.1.2.2  skrll 	/* copy in the ports for later check */
   3341  1.1.2.2  skrll 	stc.myport = sh->dest_port;
   3342  1.1.2.2  skrll 	stc.peerport = sh->src_port;
   3343  1.1.2.2  skrll 
   3344  1.1.2.2  skrll 	/*
   3345  1.1.2.2  skrll 	 * If we wanted to honor cookie life extentions, we would add
   3346  1.1.2.2  skrll 	 * to stc.cookie_life. For now we should NOT honor any extension
   3347  1.1.2.2  skrll 	 */
   3348  1.1.2.2  skrll 	stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
   3349  1.1.2.2  skrll 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   3350  1.1.2.2  skrll 		struct inpcb *in_inp;
   3351  1.1.2.2  skrll 		/* Its a V6 socket */
   3352  1.1.2.2  skrll 		in_inp = (struct inpcb *)inp;
   3353  1.1.2.2  skrll 		stc.ipv6_addr_legal = 1;
   3354  1.1.2.2  skrll 		/* Now look at the binding flag to see if V4 will be legal */
   3355  1.1.2.2  skrll 		if (
   3356  1.1.2.2  skrll #if defined(__FreeBSD__) || defined(__APPLE__)
   3357  1.1.2.2  skrll 		    (in_inp->inp_flags & IN6P_IPV6_V6ONLY)
   3358  1.1.2.2  skrll #elif defined(__OpenBSD__)
   3359  1.1.2.2  skrll 		    (0)	/* For openbsd we do dual bind only */
   3360  1.1.2.2  skrll #else
   3361  1.1.2.2  skrll 		    (((struct in6pcb *)in_inp)->in6p_flags & IN6P_IPV6_V6ONLY)
   3362  1.1.2.2  skrll #endif
   3363  1.1.2.2  skrll 		    == 0) {
   3364  1.1.2.2  skrll 			stc.ipv4_addr_legal = 1;
   3365  1.1.2.2  skrll 		} else {
   3366  1.1.2.2  skrll 			/* V4 addresses are NOT legal on the association */
   3367  1.1.2.2  skrll 			stc.ipv4_addr_legal = 0;
   3368  1.1.2.2  skrll 		}
   3369  1.1.2.2  skrll 	} else {
   3370  1.1.2.2  skrll 		/* Its a V4 socket, no - V6 */
   3371  1.1.2.2  skrll 		stc.ipv4_addr_legal = 1;
   3372  1.1.2.2  skrll 		stc.ipv6_addr_legal = 0;
   3373  1.1.2.2  skrll 	}
   3374  1.1.2.2  skrll 
   3375  1.1.2.2  skrll #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
   3376  1.1.2.2  skrll 	stc.ipv4_scope = 1;
   3377  1.1.2.2  skrll #else
   3378  1.1.2.2  skrll 	stc.ipv4_scope = 0;
   3379  1.1.2.2  skrll #endif
   3380  1.1.2.2  skrll 	/* now for scope setup */
   3381  1.1.2.2  skrll 	memset((void *)&store, 0, sizeof(store));
   3382  1.1.2.2  skrll 	sin = (struct sockaddr_in *)&store;
   3383  1.1.2.2  skrll 	sin6 = (struct sockaddr_in6 *)&store;
   3384  1.1.2.2  skrll 	if (net == NULL) {
   3385  1.1.2.2  skrll 		to = (struct sockaddr *)&store;
   3386  1.1.2.2  skrll 		iph = mtod(init_pkt, struct ip *);
   3387  1.1.2.2  skrll 		if (iph->ip_v == IPVERSION) {
   3388  1.1.2.2  skrll 			struct in_addr addr;
   3389  1.1.2.2  skrll 			static struct route iproute;
   3390  1.1.2.2  skrll 
   3391  1.1.2.2  skrll 			sin->sin_family = AF_INET;
   3392  1.1.2.2  skrll 			sin->sin_len = sizeof(struct sockaddr_in);
   3393  1.1.2.2  skrll 			sin->sin_port = sh->src_port;
   3394  1.1.2.2  skrll 			sin->sin_addr = iph->ip_src;
   3395  1.1.2.2  skrll 			/* lookup address */
   3396  1.1.2.2  skrll 			stc.address[0] = sin->sin_addr.s_addr;
   3397  1.1.2.2  skrll 			stc.address[1] = 0;
   3398  1.1.2.2  skrll 			stc.address[2] = 0;
   3399  1.1.2.2  skrll 			stc.address[3] = 0;
   3400  1.1.2.2  skrll 			stc.addr_type = SCTP_IPV4_ADDRESS;
   3401  1.1.2.2  skrll 			/* local from address */
   3402  1.1.2.2  skrll 			memset(&iproute, 0, sizeof(iproute));
   3403  1.1.2.2  skrll 			ro = &iproute;
   3404  1.1.2.2  skrll 
   3405  1.1.2.2  skrll 			rtcache_lookup(ro, (struct sockaddr *) sin);
   3406  1.1.2.2  skrll 			addr = sctp_ipv4_source_address_selection(inp, NULL,
   3407  1.1.2.2  skrll 			    ro, NULL, 0);
   3408  1.1.2.2  skrll 			stc.laddress[0] = addr.s_addr;
   3409  1.1.2.2  skrll 			stc.laddress[1] = 0;
   3410  1.1.2.2  skrll 			stc.laddress[2] = 0;
   3411  1.1.2.2  skrll 			stc.laddress[3] = 0;
   3412  1.1.2.2  skrll 			stc.laddr_type = SCTP_IPV4_ADDRESS;
   3413  1.1.2.2  skrll 			/* scope_id is only for v6 */
   3414  1.1.2.2  skrll 			stc.scope_id = 0;
   3415  1.1.2.2  skrll #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
   3416  1.1.2.2  skrll 			if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
   3417  1.1.2.2  skrll 				stc.ipv4_scope = 1;
   3418  1.1.2.2  skrll 			}
   3419  1.1.2.2  skrll #else
   3420  1.1.2.2  skrll 			stc.ipv4_scope = 1;
   3421  1.1.2.2  skrll #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
   3422  1.1.2.2  skrll 			/* Must use the address in this case */
   3423  1.1.2.2  skrll 			if (sctp_is_address_on_local_host((struct sockaddr *)sin)) {
   3424  1.1.2.2  skrll 				stc.loopback_scope = 1;
   3425  1.1.2.2  skrll 				stc.ipv4_scope = 1;
   3426  1.1.2.2  skrll 				stc.site_scope = 1;
   3427  1.1.2.2  skrll 				stc.local_scope = 1;
   3428  1.1.2.2  skrll 			}
   3429  1.1.2.2  skrll 		} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
   3430  1.1.2.2  skrll 			struct in6_addr addr;
   3431  1.1.2.2  skrll                         static struct route iproute6;
   3432  1.1.2.2  skrll 			ip6 = mtod(init_pkt, struct ip6_hdr *);
   3433  1.1.2.2  skrll 			sin6->sin6_family = AF_INET6;
   3434  1.1.2.2  skrll 			sin6->sin6_len = sizeof(struct sockaddr_in6);
   3435  1.1.2.2  skrll 			sin6->sin6_port = sh->src_port;
   3436  1.1.2.2  skrll 			sin6->sin6_addr = ip6->ip6_src;
   3437  1.1.2.2  skrll 			/* lookup address */
   3438  1.1.2.2  skrll 			memcpy(&stc.address, &sin6->sin6_addr,
   3439  1.1.2.2  skrll 			    sizeof(struct in6_addr));
   3440  1.1.2.2  skrll 			sin6->sin6_scope_id = 0;
   3441  1.1.2.2  skrll 			stc.addr_type = SCTP_IPV6_ADDRESS;
   3442  1.1.2.2  skrll 			stc.scope_id = 0;
   3443  1.1.2.2  skrll 			if (sctp_is_address_on_local_host((struct sockaddr *)sin6)) {
   3444  1.1.2.2  skrll 				stc.loopback_scope = 1;
   3445  1.1.2.2  skrll 				stc.local_scope = 1;
   3446  1.1.2.2  skrll 				stc.site_scope = 1;
   3447  1.1.2.2  skrll 				stc.ipv4_scope = 1;
   3448  1.1.2.2  skrll 			} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
   3449  1.1.2.2  skrll 				/*
   3450  1.1.2.2  skrll 				 * If the new destination is a LINK_LOCAL
   3451  1.1.2.2  skrll 				 * we must have common both site and local
   3452  1.1.2.2  skrll 				 * scope. Don't set local scope though since
   3453  1.1.2.2  skrll 				 * we must depend on the source to be added
   3454  1.1.2.2  skrll 				 * implicitly. We cannot assure just because
   3455  1.1.2.2  skrll 				 * we share one link that all links are common.
   3456  1.1.2.2  skrll 				 *
   3457  1.1.2.2  skrll 				 * XXX: never treat link-local case explicitly.
   3458  1.1.2.2  skrll 				 * Use general routines defined in scope6.c.
   3459  1.1.2.2  skrll 				 * (jinmei@kame)
   3460  1.1.2.2  skrll 				 */
   3461  1.1.2.2  skrll 				stc.local_scope = 0;
   3462  1.1.2.2  skrll 				stc.site_scope = 1;
   3463  1.1.2.2  skrll  				stc.ipv4_scope = 1;
   3464  1.1.2.2  skrll 				/* we start counting for the private
   3465  1.1.2.2  skrll 				 * address stuff at 1. since the link
   3466  1.1.2.2  skrll 				 * local we source from won't show
   3467  1.1.2.2  skrll 				 * up in our scoped count.
   3468  1.1.2.2  skrll 				 */
   3469  1.1.2.2  skrll 				cnt_inits_to=1;
   3470  1.1.2.2  skrll 				/* pull out the scope_id from incoming pkt */
   3471  1.1.2.2  skrll #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__)
   3472  1.1.2.2  skrll 				(void)in6_recoverscope(sin6, &in6_src,
   3473  1.1.2.5  skrll 				    m_get_rcvif_NOMPSAFE(init_pkt));
   3474  1.1.2.2  skrll 				in6_embedscope(&sin6->sin6_addr, sin6, NULL,
   3475  1.1.2.2  skrll 				    NULL);
   3476  1.1.2.2  skrll #else
   3477  1.1.2.2  skrll 				(void)sa6_recoverscope(sin6);
   3478  1.1.2.2  skrll #endif
   3479  1.1.2.2  skrll 				stc.scope_id = sin6->sin6_scope_id;
   3480  1.1.2.2  skrll 
   3481  1.1.2.2  skrll 			} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
   3482  1.1.2.2  skrll 				/*
   3483  1.1.2.2  skrll 				 * If the new destination is SITE_LOCAL
   3484  1.1.2.2  skrll 				 * then we must have site scope in common.
   3485  1.1.2.2  skrll 				 */
   3486  1.1.2.2  skrll 				stc.site_scope = 1;
   3487  1.1.2.2  skrll 			}
   3488  1.1.2.2  skrll 			/* local from address */
   3489  1.1.2.2  skrll 			memset(&iproute6, 0, sizeof(iproute6));
   3490  1.1.2.2  skrll 			ro = (struct route *)&iproute6;
   3491  1.1.2.2  skrll 			rtcache_lookup(ro, (struct sockaddr *) sin6);
   3492  1.1.2.2  skrll 			addr = sctp_ipv6_source_address_selection(inp, NULL,
   3493  1.1.2.2  skrll 			    ro, NULL, 0);
   3494  1.1.2.2  skrll 			memcpy(&stc.laddress, &addr, sizeof(struct in6_addr));
   3495  1.1.2.2  skrll 			stc.laddr_type = SCTP_IPV6_ADDRESS;
   3496  1.1.2.2  skrll 		}
   3497  1.1.2.2  skrll 	} else {
   3498  1.1.2.2  skrll 		/* set the scope per the existing tcb */
   3499  1.1.2.2  skrll 		struct sctp_nets *lnet;
   3500  1.1.2.2  skrll 
   3501  1.1.2.2  skrll 		stc.loopback_scope = asoc->loopback_scope;
   3502  1.1.2.2  skrll 		stc.ipv4_scope = asoc->ipv4_local_scope;
   3503  1.1.2.2  skrll 		stc.site_scope = asoc->site_scope;
   3504  1.1.2.2  skrll 		stc.local_scope = asoc->local_scope;
   3505  1.1.2.2  skrll 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
   3506  1.1.2.2  skrll 			if (rtcache_getdst(&lnet->ro)->sa_family == AF_INET6) {
   3507  1.1.2.2  skrll 				if (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *) rtcache_getdst(&lnet->ro)->sa_data)) {
   3508  1.1.2.2  skrll 					/* if we have a LL address, start counting
   3509  1.1.2.2  skrll 					 * at 1.
   3510  1.1.2.2  skrll 					 */
   3511  1.1.2.2  skrll  					cnt_inits_to = 1;
   3512  1.1.2.2  skrll 				}
   3513  1.1.2.2  skrll 			}
   3514  1.1.2.2  skrll 		}
   3515  1.1.2.2  skrll 
   3516  1.1.2.2  skrll 		/* use the net pointer */
   3517  1.1.2.2  skrll 		to = rtcache_getdst(&net->ro);
   3518  1.1.2.2  skrll 		if (to->sa_family == AF_INET) {
   3519  1.1.2.2  skrll 			memcpy(&stc.address[0], to, sizeof(struct in_addr));
   3520  1.1.2.2  skrll 			stc.address[1] = 0;
   3521  1.1.2.2  skrll 			stc.address[2] = 0;
   3522  1.1.2.2  skrll 			stc.address[3] = 0;
   3523  1.1.2.2  skrll 			stc.addr_type = SCTP_IPV4_ADDRESS;
   3524  1.1.2.2  skrll 			if (net->src_addr_selected == 0) {
   3525  1.1.2.2  skrll 				/* strange case here, the INIT
   3526  1.1.2.2  skrll 				 * should have did the selection.
   3527  1.1.2.2  skrll 				 */
   3528  1.1.2.2  skrll 				net->_s_addr.sin.sin_addr =
   3529  1.1.2.2  skrll 				    sctp_ipv4_source_address_selection(inp,
   3530  1.1.2.2  skrll 				    stcb, &net->ro, net, 0);
   3531  1.1.2.2  skrll 				net->src_addr_selected = 1;
   3532  1.1.2.2  skrll 
   3533  1.1.2.2  skrll 			}
   3534  1.1.2.2  skrll 
   3535  1.1.2.2  skrll 			stc.laddress[0] = net->_s_addr.sin.sin_addr.s_addr;
   3536  1.1.2.2  skrll 			stc.laddress[1] = 0;
   3537  1.1.2.2  skrll 			stc.laddress[2] = 0;
   3538  1.1.2.2  skrll 			stc.laddress[3] = 0;
   3539  1.1.2.2  skrll 			stc.laddr_type = SCTP_IPV4_ADDRESS;
   3540  1.1.2.2  skrll 		} else if (to->sa_family == AF_INET6) {
   3541  1.1.2.2  skrll 			memcpy(&stc.address, &to->sa_data,
   3542  1.1.2.2  skrll 			    sizeof(struct in6_addr));
   3543  1.1.2.2  skrll 			stc.addr_type = SCTP_IPV6_ADDRESS;
   3544  1.1.2.2  skrll 			if (net->src_addr_selected == 0) {
   3545  1.1.2.2  skrll 				/* strange case here, the INIT
   3546  1.1.2.2  skrll 				 * should have did the selection.
   3547  1.1.2.2  skrll 				 */
   3548  1.1.2.2  skrll 				net->_s_addr.sin6.sin6_addr =
   3549  1.1.2.2  skrll 				    sctp_ipv6_source_address_selection(inp,
   3550  1.1.2.2  skrll 				    stcb, &net->ro, net, 0);
   3551  1.1.2.2  skrll 				net->src_addr_selected = 1;
   3552  1.1.2.2  skrll 			}
   3553  1.1.2.2  skrll 			memcpy(&stc.laddress, &net->_s_addr.sin6.sin6_addr,
   3554  1.1.2.2  skrll 			    sizeof(struct in6_addr));
   3555  1.1.2.2  skrll 			stc.laddr_type = SCTP_IPV6_ADDRESS;
   3556  1.1.2.2  skrll 		}
   3557  1.1.2.2  skrll 	}
   3558  1.1.2.2  skrll 	/* Now lets put the SCTP header in place */
   3559  1.1.2.2  skrll 	initackm_out = mtod(m, struct sctp_init_msg *);
   3560  1.1.2.2  skrll 	initackm_out->sh.src_port = inp->sctp_lport;
   3561  1.1.2.2  skrll 	initackm_out->sh.dest_port = sh->src_port;
   3562  1.1.2.2  skrll 	initackm_out->sh.v_tag = init_chk->init.initiate_tag;
   3563  1.1.2.2  skrll 	/* Save it off for quick ref */
   3564  1.1.2.2  skrll 	stc.peers_vtag = init_chk->init.initiate_tag;
   3565  1.1.2.2  skrll 	initackm_out->sh.checksum = 0;	/* calculate later */
   3566  1.1.2.2  skrll 	/* who are we */
   3567  1.1.2.2  skrll 	strncpy(stc.identification, SCTP_VERSION_STRING,
   3568  1.1.2.2  skrll 	   min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
   3569  1.1.2.2  skrll 	/* now the chunk header */
   3570  1.1.2.2  skrll 	initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
   3571  1.1.2.2  skrll 	initackm_out->msg.ch.chunk_flags = 0;
   3572  1.1.2.2  skrll 	/* fill in later from mbuf we build */
   3573  1.1.2.2  skrll 	initackm_out->msg.ch.chunk_length = 0;
   3574  1.1.2.2  skrll 	/* place in my tag */
   3575  1.1.2.2  skrll 	if ((asoc != NULL) &&
   3576  1.1.2.2  skrll 	    ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
   3577  1.1.2.2  skrll 	     (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
   3578  1.1.2.2  skrll 		/* re-use the v-tags and init-seq here */
   3579  1.1.2.2  skrll 		initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
   3580  1.1.2.2  skrll 		initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
   3581  1.1.2.2  skrll 	} else {
   3582  1.1.2.2  skrll 		initackm_out->msg.init.initiate_tag = htonl(sctp_select_a_tag(inp));
   3583  1.1.2.2  skrll 		/* get a TSN to use too */
   3584  1.1.2.2  skrll 		initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
   3585  1.1.2.2  skrll 	}
   3586  1.1.2.2  skrll 	/* save away my tag to */
   3587  1.1.2.2  skrll 	stc.my_vtag = initackm_out->msg.init.initiate_tag;
   3588  1.1.2.2  skrll 
   3589  1.1.2.2  skrll 	/* set up some of the credits. */
   3590  1.1.2.2  skrll 	initackm_out->msg.init.a_rwnd = htonl(max(inp->sctp_socket->so_rcv.sb_hiwat, SCTP_MINIMAL_RWND));
   3591  1.1.2.2  skrll 	/* set what I want */
   3592  1.1.2.2  skrll 	his_limit = ntohs(init_chk->init.num_inbound_streams);
   3593  1.1.2.2  skrll 	/* choose what I want */
   3594  1.1.2.2  skrll 	if (asoc != NULL) {
   3595  1.1.2.2  skrll 		if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
   3596  1.1.2.2  skrll 			i_want = asoc->streamoutcnt;
   3597  1.1.2.2  skrll 		} else {
   3598  1.1.2.2  skrll 			i_want = inp->sctp_ep.pre_open_stream_count;
   3599  1.1.2.2  skrll 		}
   3600  1.1.2.2  skrll 	} else {
   3601  1.1.2.2  skrll 		i_want = inp->sctp_ep.pre_open_stream_count;
   3602  1.1.2.2  skrll 	}
   3603  1.1.2.2  skrll 	if (his_limit < i_want) {
   3604  1.1.2.2  skrll 		/* I Want more :< */
   3605  1.1.2.2  skrll 		initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
   3606  1.1.2.2  skrll 	} else {
   3607  1.1.2.2  skrll 		/* I can have what I want :> */
   3608  1.1.2.2  skrll 		initackm_out->msg.init.num_outbound_streams = htons(i_want);
   3609  1.1.2.2  skrll 	}
   3610  1.1.2.2  skrll 	/* tell him his limt. */
   3611  1.1.2.2  skrll 	initackm_out->msg.init.num_inbound_streams =
   3612  1.1.2.2  skrll 	    htons(inp->sctp_ep.max_open_streams_intome);
   3613  1.1.2.2  skrll 	/* setup the ECN pointer */
   3614  1.1.2.2  skrll 
   3615  1.1.2.2  skrll /*	if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT) {*/
   3616  1.1.2.2  skrll 	if (inp->sctp_ep.adaption_layer_indicator) {
   3617  1.1.2.2  skrll 		struct sctp_adaption_layer_indication *ali;
   3618  1.1.2.2  skrll 		ali = (struct sctp_adaption_layer_indication *)(
   3619  1.1.2.2  skrll 		    (vaddr_t)initackm_out + sizeof(*initackm_out));
   3620  1.1.2.2  skrll 		ali->ph.param_type = htons(SCTP_ULP_ADAPTION);
   3621  1.1.2.2  skrll 		ali->ph.param_length = htons(sizeof(*ali));
   3622  1.1.2.2  skrll 		ali->indication = ntohl(inp->sctp_ep.adaption_layer_indicator);
   3623  1.1.2.2  skrll 		m->m_len += sizeof(*ali);
   3624  1.1.2.2  skrll 		ecn = (struct sctp_ecn_supported_param *)((vaddr_t)ali +
   3625  1.1.2.2  skrll 		    sizeof(*ali));
   3626  1.1.2.2  skrll 	} else {
   3627  1.1.2.2  skrll 		ecn = (struct sctp_ecn_supported_param*)(
   3628  1.1.2.2  skrll 		    (vaddr_t)initackm_out + sizeof(*initackm_out));
   3629  1.1.2.2  skrll 	}
   3630  1.1.2.2  skrll 
   3631  1.1.2.2  skrll 	/* ECN parameter */
   3632  1.1.2.2  skrll 	if (sctp_ecn == 1) {
   3633  1.1.2.2  skrll 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
   3634  1.1.2.2  skrll 		ecn->ph.param_length = htons(sizeof(*ecn));
   3635  1.1.2.2  skrll 		m->m_len += sizeof(*ecn);
   3636  1.1.2.2  skrll 
   3637  1.1.2.2  skrll 		prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn +
   3638  1.1.2.2  skrll 		    sizeof(*ecn));
   3639  1.1.2.2  skrll 	} else {
   3640  1.1.2.2  skrll 		prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn);
   3641  1.1.2.2  skrll 	}
   3642  1.1.2.2  skrll 	/* And now tell the peer we do  pr-sctp */
   3643  1.1.2.2  skrll 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
   3644  1.1.2.2  skrll 	prsctp->ph.param_length = htons(sizeof(*prsctp));
   3645  1.1.2.2  skrll 	m->m_len += sizeof(*prsctp);
   3646  1.1.2.2  skrll 
   3647  1.1.2.2  skrll 
   3648  1.1.2.2  skrll 	/* And now tell the peer we do all the extensions */
   3649  1.1.2.2  skrll 	pr_supported = (struct sctp_supported_chunk_types_param *)((vaddr_t)prsctp +
   3650  1.1.2.2  skrll 	   sizeof(*prsctp));
   3651  1.1.2.2  skrll 
   3652  1.1.2.2  skrll 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
   3653  1.1.2.2  skrll 	pr_supported->ph.param_length = htons(sizeof(*pr_supported) + SCTP_EXT_COUNT);
   3654  1.1.2.2  skrll 	pr_supported->chunk_types[0] = SCTP_ASCONF;
   3655  1.1.2.2  skrll 	pr_supported->chunk_types[1] = SCTP_ASCONF_ACK;
   3656  1.1.2.2  skrll 	pr_supported->chunk_types[2] = SCTP_FORWARD_CUM_TSN;
   3657  1.1.2.2  skrll 	pr_supported->chunk_types[3] = SCTP_PACKET_DROPPED;
   3658  1.1.2.2  skrll 	pr_supported->chunk_types[4] = SCTP_STREAM_RESET;
   3659  1.1.2.2  skrll 	pr_supported->chunk_types[5] = 0; /* pad */
   3660  1.1.2.2  skrll 	pr_supported->chunk_types[6] = 0; /* pad */
   3661  1.1.2.2  skrll 	pr_supported->chunk_types[7] = 0; /* pad */
   3662  1.1.2.2  skrll 
   3663  1.1.2.2  skrll 	m->m_len += (sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
   3664  1.1.2.2  skrll 	if (sctp_ecn_nonce) {
   3665  1.1.2.2  skrll 		/* ECN nonce: And now tell the peer we support ECN nonce */
   3666  1.1.2.2  skrll 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)((vaddr_t)pr_supported +
   3667  1.1.2.2  skrll 		     sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
   3668  1.1.2.2  skrll 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
   3669  1.1.2.2  skrll 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
   3670  1.1.2.2  skrll 		m->m_len += sizeof(*ecn_nonce);
   3671  1.1.2.2  skrll 	}
   3672  1.1.2.2  skrll 
   3673  1.1.2.2  skrll 	m_at = m;
   3674  1.1.2.2  skrll 	/* now the addresses */
   3675  1.1.2.2  skrll 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   3676  1.1.2.2  skrll 		struct ifnet *ifn;
   3677  1.1.2.2  skrll 		struct ifaddr *ifa;
   3678  1.1.2.2  skrll 		int cnt = cnt_inits_to;
   3679  1.1.2.4  skrll 		int s;
   3680  1.1.2.2  skrll 
   3681  1.1.2.4  skrll 		s = pserialize_read_enter();
   3682  1.1.2.4  skrll 		IFNET_READER_FOREACH(ifn) {
   3683  1.1.2.2  skrll 			if ((stc.loopback_scope == 0) &&
   3684  1.1.2.2  skrll 			    (ifn->if_type == IFT_LOOP)) {
   3685  1.1.2.2  skrll 				/*
   3686  1.1.2.2  skrll 				 * Skip loopback devices if loopback_scope
   3687  1.1.2.2  skrll 				 * not set
   3688  1.1.2.2  skrll 				 */
   3689  1.1.2.2  skrll 				continue;
   3690  1.1.2.2  skrll 			}
   3691  1.1.2.5  skrll 			IFADDR_READER_FOREACH(ifa, ifn) {
   3692  1.1.2.2  skrll 				if (sctp_is_address_in_scope(ifa,
   3693  1.1.2.2  skrll 				    stc.ipv4_addr_legal, stc.ipv6_addr_legal,
   3694  1.1.2.2  skrll 				    stc.loopback_scope, stc.ipv4_scope,
   3695  1.1.2.2  skrll 				    stc.local_scope, stc.site_scope) == 0) {
   3696  1.1.2.2  skrll 					continue;
   3697  1.1.2.2  skrll 				}
   3698  1.1.2.2  skrll 				cnt++;
   3699  1.1.2.2  skrll 			}
   3700  1.1.2.2  skrll 		}
   3701  1.1.2.4  skrll 		pserialize_read_exit(s);
   3702  1.1.2.4  skrll 
   3703  1.1.2.2  skrll 		if (cnt > 1) {
   3704  1.1.2.4  skrll 			s = pserialize_read_enter();
   3705  1.1.2.4  skrll 			IFNET_READER_FOREACH(ifn) {
   3706  1.1.2.2  skrll 				if ((stc.loopback_scope == 0) &&
   3707  1.1.2.2  skrll 				    (ifn->if_type == IFT_LOOP)) {
   3708  1.1.2.2  skrll 					/*
   3709  1.1.2.2  skrll 					 * Skip loopback devices if
   3710  1.1.2.2  skrll 					 * loopback_scope not set
   3711  1.1.2.2  skrll 					 */
   3712  1.1.2.2  skrll 					continue;
   3713  1.1.2.2  skrll 				}
   3714  1.1.2.5  skrll 				IFADDR_READER_FOREACH(ifa, ifn) {
   3715  1.1.2.2  skrll 					if (sctp_is_address_in_scope(ifa,
   3716  1.1.2.2  skrll 					    stc.ipv4_addr_legal,
   3717  1.1.2.2  skrll 					    stc.ipv6_addr_legal,
   3718  1.1.2.2  skrll 					    stc.loopback_scope, stc.ipv4_scope,
   3719  1.1.2.2  skrll 					    stc.local_scope, stc.site_scope) == 0) {
   3720  1.1.2.2  skrll 						continue;
   3721  1.1.2.2  skrll 					}
   3722  1.1.2.2  skrll 					m_at = sctp_add_addr_to_mbuf(m_at, ifa);
   3723  1.1.2.2  skrll 				}
   3724  1.1.2.2  skrll 			}
   3725  1.1.2.4  skrll 			pserialize_read_exit(s);
   3726  1.1.2.2  skrll 		}
   3727  1.1.2.2  skrll 	} else {
   3728  1.1.2.2  skrll 		struct sctp_laddr *laddr;
   3729  1.1.2.2  skrll 		int cnt;
   3730  1.1.2.2  skrll 		cnt = cnt_inits_to;
   3731  1.1.2.2  skrll 		/* First, how many ? */
   3732  1.1.2.2  skrll 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   3733  1.1.2.2  skrll 			if (laddr->ifa == NULL) {
   3734  1.1.2.2  skrll 				continue;
   3735  1.1.2.2  skrll 			}
   3736  1.1.2.2  skrll 			if (laddr->ifa->ifa_addr == NULL)
   3737  1.1.2.2  skrll 				continue;
   3738  1.1.2.2  skrll 			if (sctp_is_address_in_scope(laddr->ifa,
   3739  1.1.2.2  skrll 			    stc.ipv4_addr_legal, stc.ipv6_addr_legal,
   3740  1.1.2.2  skrll 			    stc.loopback_scope, stc.ipv4_scope,
   3741  1.1.2.2  skrll 			    stc.local_scope, stc.site_scope) == 0) {
   3742  1.1.2.2  skrll 				continue;
   3743  1.1.2.2  skrll 			}
   3744  1.1.2.2  skrll 			cnt++;
   3745  1.1.2.2  skrll 		}
   3746  1.1.2.2  skrll 		/* If we bind a single address only we won't list
   3747  1.1.2.2  skrll 		 * any. This way you can get through a NAT
   3748  1.1.2.2  skrll 		 */
   3749  1.1.2.2  skrll 		if (cnt > 1) {
   3750  1.1.2.2  skrll 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   3751  1.1.2.2  skrll 				if (laddr->ifa == NULL) {
   3752  1.1.2.2  skrll #ifdef SCTP_DEBUG
   3753  1.1.2.2  skrll 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   3754  1.1.2.2  skrll 						printf("Help I have fallen and I can't get up!\n");
   3755  1.1.2.2  skrll 					}
   3756  1.1.2.2  skrll #endif
   3757  1.1.2.2  skrll 					continue;
   3758  1.1.2.2  skrll 				}
   3759  1.1.2.2  skrll 				if (laddr->ifa->ifa_addr == NULL)
   3760  1.1.2.2  skrll 					continue;
   3761  1.1.2.2  skrll 				if (sctp_is_address_in_scope(laddr->ifa,
   3762  1.1.2.2  skrll 				    stc.ipv4_addr_legal, stc.ipv6_addr_legal,
   3763  1.1.2.2  skrll 				    stc.loopback_scope, stc.ipv4_scope,
   3764  1.1.2.2  skrll 				    stc.local_scope, stc.site_scope) == 0) {
   3765  1.1.2.2  skrll 					continue;
   3766  1.1.2.2  skrll 				}
   3767  1.1.2.2  skrll 				m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
   3768  1.1.2.2  skrll 			}
   3769  1.1.2.2  skrll 		}
   3770  1.1.2.2  skrll 	}
   3771  1.1.2.2  skrll 
   3772  1.1.2.2  skrll 	/* tack on the operational error if present */
   3773  1.1.2.2  skrll 	if (op_err) {
   3774  1.1.2.2  skrll 		if (op_err->m_pkthdr.len % 4) {
   3775  1.1.2.2  skrll 			/* must add a pad to the param */
   3776  1.1.2.2  skrll 			u_int32_t cpthis=0;
   3777  1.1.2.2  skrll 			int padlen;
   3778  1.1.2.2  skrll 			padlen = 4 - (op_err->m_pkthdr.len % 4);
   3779  1.1.2.2  skrll 			m_copyback(op_err, op_err->m_pkthdr.len, padlen, (void *)&cpthis);
   3780  1.1.2.2  skrll 		}
   3781  1.1.2.2  skrll 		while (m_at->m_next != NULL) {
   3782  1.1.2.2  skrll 			m_at = m_at->m_next;
   3783  1.1.2.2  skrll 		}
   3784  1.1.2.2  skrll 		m_at->m_next = op_err;
   3785  1.1.2.2  skrll 		while (m_at->m_next != NULL) {
   3786  1.1.2.2  skrll 			m_at = m_at->m_next;
   3787  1.1.2.2  skrll 		}
   3788  1.1.2.2  skrll 	}
   3789  1.1.2.2  skrll 	/* Get total size of init packet */
   3790  1.1.2.2  skrll 	sz_of = SCTP_SIZE32(ntohs(init_chk->ch.chunk_length));
   3791  1.1.2.2  skrll 	/* pre-calulate the size and update pkt header and chunk header */
   3792  1.1.2.2  skrll 	m->m_pkthdr.len = 0;
   3793  1.1.2.2  skrll 	for (m_tmp = m; m_tmp; m_tmp = m_tmp->m_next) {
   3794  1.1.2.2  skrll 		m->m_pkthdr.len += m_tmp->m_len;
   3795  1.1.2.2  skrll 		if (m_tmp->m_next == NULL) {
   3796  1.1.2.2  skrll 			/* m_tmp should now point to last one */
   3797  1.1.2.2  skrll 			break;
   3798  1.1.2.2  skrll 		}
   3799  1.1.2.2  skrll 	}
   3800  1.1.2.2  skrll 	/*
   3801  1.1.2.2  skrll 	 * Figure now the size of the cookie. We know the size of the
   3802  1.1.2.2  skrll 	 * INIT-ACK. The Cookie is going to be the size of INIT, INIT-ACK,
   3803  1.1.2.2  skrll 	 * COOKIE-STRUCTURE and SIGNATURE.
   3804  1.1.2.2  skrll 	 */
   3805  1.1.2.2  skrll 
   3806  1.1.2.2  skrll 	/*
   3807  1.1.2.2  skrll 	 * take our earlier INIT calc and add in the sz we just calculated
   3808  1.1.2.2  skrll 	 * minus the size of the sctphdr (its not included in chunk size
   3809  1.1.2.2  skrll 	 */
   3810  1.1.2.2  skrll 
   3811  1.1.2.2  skrll 	/* add once for the INIT-ACK */
   3812  1.1.2.2  skrll 	sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
   3813  1.1.2.2  skrll 
   3814  1.1.2.2  skrll 	/* add a second time for the INIT-ACK in the cookie */
   3815  1.1.2.2  skrll 	sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
   3816  1.1.2.2  skrll 
   3817  1.1.2.2  skrll 	/* Now add the cookie header and cookie message struct */
   3818  1.1.2.2  skrll 	sz_of += sizeof(struct sctp_state_cookie_param);
   3819  1.1.2.2  skrll 	/* ...and add the size of our signature */
   3820  1.1.2.2  skrll 	sz_of += SCTP_SIGNATURE_SIZE;
   3821  1.1.2.2  skrll 	initackm_out->msg.ch.chunk_length = htons(sz_of);
   3822  1.1.2.2  skrll 
   3823  1.1.2.2  skrll 	/* Now we must build a cookie */
   3824  1.1.2.2  skrll 	m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
   3825  1.1.2.2  skrll 	    sizeof(struct sctphdr), &stc);
   3826  1.1.2.2  skrll 	if (m_cookie == NULL) {
   3827  1.1.2.2  skrll 		/* memory problem */
   3828  1.1.2.2  skrll 		sctp_m_freem(m);
   3829  1.1.2.2  skrll 		return;
   3830  1.1.2.2  skrll 	}
   3831  1.1.2.2  skrll 	/* Now append the cookie to the end and update the space/size */
   3832  1.1.2.2  skrll 	m_tmp->m_next = m_cookie;
   3833  1.1.2.2  skrll 
   3834  1.1.2.2  skrll 	/*
   3835  1.1.2.2  skrll 	 * We pass 0 here to NOT set IP_DF if its IPv4, we ignore the
   3836  1.1.2.2  skrll 	 * return here since the timer will drive a retranmission.
   3837  1.1.2.2  skrll 	 */
   3838  1.1.2.2  skrll 	padval = m->m_pkthdr.len % 4;
   3839  1.1.2.2  skrll 	if ((padval) && (m_last)) {
   3840  1.1.2.2  skrll 		/* see my previous comments on m_last */
   3841  1.1.2.2  skrll 		int ret;
   3842  1.1.2.2  skrll 		ret = sctp_add_pad_tombuf(m_last, (4-padval));
   3843  1.1.2.2  skrll 		if (ret) {
   3844  1.1.2.2  skrll 			/* Houston we have a problem, no space */
   3845  1.1.2.2  skrll 			sctp_m_freem(m);
   3846  1.1.2.2  skrll 			return;
   3847  1.1.2.2  skrll 		}
   3848  1.1.2.2  skrll 		m->m_pkthdr.len += padval;
   3849  1.1.2.2  skrll 	}
   3850  1.1.2.2  skrll 	sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, 0, NULL, 0);
   3851  1.1.2.2  skrll }
   3852  1.1.2.2  skrll 
   3853  1.1.2.2  skrll 
   3854  1.1.2.2  skrll static void
   3855  1.1.2.2  skrll sctp_insert_on_wheel(struct sctp_association *asoc,
   3856  1.1.2.2  skrll 		     struct sctp_stream_out *strq)
   3857  1.1.2.2  skrll {
   3858  1.1.2.2  skrll 	struct sctp_stream_out *stre, *strn;
   3859  1.1.2.2  skrll 	stre = TAILQ_FIRST(&asoc->out_wheel);
   3860  1.1.2.2  skrll 	if (stre == NULL) {
   3861  1.1.2.2  skrll 		/* only one on wheel */
   3862  1.1.2.2  skrll 		TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
   3863  1.1.2.2  skrll 		return;
   3864  1.1.2.2  skrll 	}
   3865  1.1.2.2  skrll 	for (; stre; stre = strn) {
   3866  1.1.2.2  skrll 		strn = TAILQ_NEXT(stre, next_spoke);
   3867  1.1.2.2  skrll 		if (stre->stream_no > strq->stream_no) {
   3868  1.1.2.2  skrll 			TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
   3869  1.1.2.2  skrll 			return;
   3870  1.1.2.2  skrll 		} else if (stre->stream_no == strq->stream_no) {
   3871  1.1.2.2  skrll 			/* huh, should not happen */
   3872  1.1.2.2  skrll 			return;
   3873  1.1.2.2  skrll 		} else if (strn == NULL) {
   3874  1.1.2.2  skrll 			/* next one is null */
   3875  1.1.2.2  skrll 			TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
   3876  1.1.2.2  skrll 					   next_spoke);
   3877  1.1.2.2  skrll 		}
   3878  1.1.2.2  skrll 	}
   3879  1.1.2.2  skrll }
   3880  1.1.2.2  skrll 
   3881  1.1.2.2  skrll static void
   3882  1.1.2.2  skrll sctp_remove_from_wheel(struct sctp_association *asoc,
   3883  1.1.2.2  skrll 		       struct sctp_stream_out *strq)
   3884  1.1.2.2  skrll {
   3885  1.1.2.2  skrll 	/* take off and then setup so we know it is not on the wheel */
   3886  1.1.2.2  skrll 	TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
   3887  1.1.2.2  skrll 	strq->next_spoke.tqe_next = NULL;
   3888  1.1.2.2  skrll 	strq->next_spoke.tqe_prev = NULL;
   3889  1.1.2.2  skrll }
   3890  1.1.2.2  skrll 
   3891  1.1.2.2  skrll 
   3892  1.1.2.2  skrll static void
   3893  1.1.2.2  skrll sctp_prune_prsctp(struct sctp_tcb *stcb,
   3894  1.1.2.2  skrll 		  struct sctp_association *asoc,
   3895  1.1.2.2  skrll 		  struct sctp_sndrcvinfo *srcv,
   3896  1.1.2.2  skrll 		  int dataout
   3897  1.1.2.2  skrll 	)
   3898  1.1.2.2  skrll {
   3899  1.1.2.2  skrll 	int freed_spc=0;
   3900  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk, *nchk;
   3901  1.1.2.2  skrll 	if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
   3902  1.1.2.2  skrll 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
   3903  1.1.2.2  skrll 			/*
   3904  1.1.2.2  skrll 			 * Look for chunks marked with the PR_SCTP
   3905  1.1.2.2  skrll 			 * flag AND the buffer space flag. If the one
   3906  1.1.2.2  skrll 			 * being sent is equal or greater priority then
   3907  1.1.2.2  skrll 			 * purge the old one and free some space.
   3908  1.1.2.2  skrll 			 */
   3909  1.1.2.2  skrll 			if ((chk->flags & (SCTP_PR_SCTP_ENABLED |
   3910  1.1.2.2  skrll 					   SCTP_PR_SCTP_BUFFER)) ==
   3911  1.1.2.2  skrll 			    (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) {
   3912  1.1.2.2  skrll 				/*
   3913  1.1.2.2  skrll 				 * This one is PR-SCTP AND buffer space
   3914  1.1.2.2  skrll 				 * limited type
   3915  1.1.2.2  skrll 				 */
   3916  1.1.2.2  skrll 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
   3917  1.1.2.2  skrll 					/* Lower numbers equates to
   3918  1.1.2.2  skrll 					 * higher priority so if the
   3919  1.1.2.2  skrll 					 * one we are looking at has a
   3920  1.1.2.2  skrll 					 * larger or equal priority we
   3921  1.1.2.2  skrll 					 * want to drop the data and
   3922  1.1.2.2  skrll 					 * NOT retransmit it.
   3923  1.1.2.2  skrll 					 */
   3924  1.1.2.2  skrll 					if (chk->data) {
   3925  1.1.2.2  skrll 						/* We release the
   3926  1.1.2.2  skrll 						 * book_size if the
   3927  1.1.2.2  skrll 						 * mbuf is here
   3928  1.1.2.2  skrll 						 */
   3929  1.1.2.2  skrll 						int ret_spc;
   3930  1.1.2.2  skrll 						int cause;
   3931  1.1.2.2  skrll 						if (chk->sent > SCTP_DATAGRAM_UNSENT)
   3932  1.1.2.2  skrll 							cause = SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_SENT;
   3933  1.1.2.2  skrll 						else
   3934  1.1.2.2  skrll 							cause = SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_UNSENT;
   3935  1.1.2.2  skrll 						ret_spc  = sctp_release_pr_sctp_chunk(stcb, chk,
   3936  1.1.2.2  skrll 										      cause,
   3937  1.1.2.2  skrll 										      &asoc->sent_queue);
   3938  1.1.2.2  skrll 						freed_spc += ret_spc;
   3939  1.1.2.2  skrll 						if (freed_spc >= dataout) {
   3940  1.1.2.2  skrll 							return;
   3941  1.1.2.2  skrll 						}
   3942  1.1.2.2  skrll 					} /* if chunk was present */
   3943  1.1.2.2  skrll 				} /* if of sufficent priority */
   3944  1.1.2.2  skrll 			} /* if chunk has enabled */
   3945  1.1.2.2  skrll 		} /* tailqforeach */
   3946  1.1.2.2  skrll 
   3947  1.1.2.2  skrll 		chk = TAILQ_FIRST(&asoc->send_queue);
   3948  1.1.2.2  skrll 		while (chk) {
   3949  1.1.2.2  skrll 			nchk = TAILQ_NEXT(chk, sctp_next);
   3950  1.1.2.2  skrll 			/* Here we must move to the sent queue and mark */
   3951  1.1.2.2  skrll 			if ((chk->flags & (SCTP_PR_SCTP_ENABLED |
   3952  1.1.2.2  skrll 					   SCTP_PR_SCTP_BUFFER)) ==
   3953  1.1.2.2  skrll 			    (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) {
   3954  1.1.2.2  skrll 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
   3955  1.1.2.2  skrll 					if (chk->data) {
   3956  1.1.2.2  skrll 						/* We release the
   3957  1.1.2.2  skrll 						 * book_size if the
   3958  1.1.2.2  skrll 						 * mbuf is here
   3959  1.1.2.2  skrll 						 */
   3960  1.1.2.2  skrll 						int ret_spc;
   3961  1.1.2.2  skrll 						ret_spc  = sctp_release_pr_sctp_chunk(stcb, chk,
   3962  1.1.2.2  skrll 						    SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_UNSENT,
   3963  1.1.2.2  skrll 						    &asoc->send_queue);
   3964  1.1.2.2  skrll 
   3965  1.1.2.2  skrll 						freed_spc += ret_spc;
   3966  1.1.2.2  skrll 						if (freed_spc >= dataout) {
   3967  1.1.2.2  skrll 							return;
   3968  1.1.2.2  skrll 						}
   3969  1.1.2.2  skrll 					} /* end if chk->data */
   3970  1.1.2.2  skrll 				} /* end if right class */
   3971  1.1.2.2  skrll 			} /* end if chk pr-sctp */
   3972  1.1.2.2  skrll 			chk = nchk;
   3973  1.1.2.2  skrll 		} /* end while (chk) */
   3974  1.1.2.2  skrll 	} /* if enabled in asoc */
   3975  1.1.2.2  skrll }
   3976  1.1.2.2  skrll 
   3977  1.1.2.2  skrll static void
   3978  1.1.2.2  skrll sctp_prepare_chunk(struct sctp_tmit_chunk *template,
   3979  1.1.2.2  skrll 		   struct sctp_tcb *stcb,
   3980  1.1.2.2  skrll 		   struct sctp_sndrcvinfo *srcv,
   3981  1.1.2.2  skrll 		   struct sctp_stream_out *strq,
   3982  1.1.2.2  skrll 		   struct sctp_nets *net)
   3983  1.1.2.2  skrll {
   3984  1.1.2.2  skrll 	memset(template, 0, sizeof(struct sctp_tmit_chunk));
   3985  1.1.2.2  skrll 	template->sent = SCTP_DATAGRAM_UNSENT;
   3986  1.1.2.2  skrll 	if ((stcb->asoc.peer_supports_prsctp) &&
   3987  1.1.2.2  skrll 	    (srcv->sinfo_flags & (MSG_PR_SCTP_TTL|MSG_PR_SCTP_BUF)) &&
   3988  1.1.2.2  skrll 	    (srcv->sinfo_timetolive > 0)
   3989  1.1.2.2  skrll 		) {
   3990  1.1.2.2  skrll 		/* If:
   3991  1.1.2.2  skrll 		 *  Peer supports PR-SCTP
   3992  1.1.2.2  skrll 		 *  The flags is set against this send for PR-SCTP
   3993  1.1.2.2  skrll 		 *  And timetolive is a postive value, zero is reserved
   3994  1.1.2.2  skrll 		 *     to mean a reliable send for both buffer/time
   3995  1.1.2.2  skrll 		 *     related one.
   3996  1.1.2.2  skrll 		 */
   3997  1.1.2.2  skrll 		if (srcv->sinfo_flags & MSG_PR_SCTP_BUF) {
   3998  1.1.2.2  skrll 			/*
   3999  1.1.2.2  skrll 			 * Time to live is a priority stored in tv_sec
   4000  1.1.2.2  skrll 			 * when doing the buffer drop thing.
   4001  1.1.2.2  skrll 			 */
   4002  1.1.2.2  skrll 			template->rec.data.timetodrop.tv_sec = srcv->sinfo_timetolive;
   4003  1.1.2.2  skrll 		} else {
   4004  1.1.2.2  skrll 			struct timeval tv;
   4005  1.1.2.2  skrll 
   4006  1.1.2.2  skrll 			SCTP_GETTIME_TIMEVAL(&template->rec.data.timetodrop);
   4007  1.1.2.2  skrll 			tv.tv_sec = srcv->sinfo_timetolive / 1000;
   4008  1.1.2.2  skrll 			tv.tv_usec = (srcv->sinfo_timetolive * 1000) % 1000000;
   4009  1.1.2.2  skrll #ifndef __FreeBSD__
   4010  1.1.2.2  skrll 			timeradd(&template->rec.data.timetodrop, &tv,
   4011  1.1.2.2  skrll 			    &template->rec.data.timetodrop);
   4012  1.1.2.2  skrll #else
   4013  1.1.2.2  skrll 			timevaladd(&template->rec.data.timetodrop, &tv);
   4014  1.1.2.2  skrll #endif
   4015  1.1.2.2  skrll 		}
   4016  1.1.2.2  skrll 	}
   4017  1.1.2.2  skrll 	if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
   4018  1.1.2.2  skrll 		template->rec.data.stream_seq = strq->next_sequence_sent;
   4019  1.1.2.2  skrll 	} else {
   4020  1.1.2.2  skrll 		template->rec.data.stream_seq = 0;
   4021  1.1.2.2  skrll 	}
   4022  1.1.2.2  skrll 	template->rec.data.TSN_seq = 0;	/* not yet assigned */
   4023  1.1.2.2  skrll 
   4024  1.1.2.2  skrll 	template->rec.data.stream_number = srcv->sinfo_stream;
   4025  1.1.2.2  skrll 	template->rec.data.payloadtype = srcv->sinfo_ppid;
   4026  1.1.2.2  skrll 	template->rec.data.context = srcv->sinfo_context;
   4027  1.1.2.2  skrll 	template->rec.data.doing_fast_retransmit = 0;
   4028  1.1.2.2  skrll 	template->rec.data.ect_nonce = 0;   /* ECN Nonce */
   4029  1.1.2.2  skrll 
   4030  1.1.2.2  skrll 	if (srcv->sinfo_flags & MSG_ADDR_OVER) {
   4031  1.1.2.2  skrll 		template->whoTo = net;
   4032  1.1.2.2  skrll 	} else {
   4033  1.1.2.2  skrll 		if (stcb->asoc.primary_destination)
   4034  1.1.2.2  skrll 			template->whoTo = stcb->asoc.primary_destination;
   4035  1.1.2.2  skrll 		else {
   4036  1.1.2.2  skrll 			/* TSNH */
   4037  1.1.2.2  skrll 			template->whoTo = net;
   4038  1.1.2.2  skrll 		}
   4039  1.1.2.2  skrll 	}
   4040  1.1.2.2  skrll 	/* the actual chunk flags */
   4041  1.1.2.2  skrll 	if (srcv->sinfo_flags & MSG_UNORDERED) {
   4042  1.1.2.2  skrll 		template->rec.data.rcv_flags = SCTP_DATA_UNORDERED;
   4043  1.1.2.2  skrll 	} else {
   4044  1.1.2.2  skrll 		template->rec.data.rcv_flags = 0;
   4045  1.1.2.2  skrll 	}
   4046  1.1.2.2  skrll 	/* no flags yet, FRAGMENT_OK goes here */
   4047  1.1.2.2  skrll 	template->flags = 0;
   4048  1.1.2.2  skrll 	/* PR sctp flags */
   4049  1.1.2.2  skrll 	if (stcb->asoc.peer_supports_prsctp) {
   4050  1.1.2.2  skrll 		if (srcv->sinfo_timetolive > 0) {
   4051  1.1.2.2  skrll 			/*
   4052  1.1.2.2  skrll 			 * We only set the flag if timetolive (or
   4053  1.1.2.2  skrll 			 * priority) was set to a positive number.
   4054  1.1.2.2  skrll 			 * Zero is reserved specifically to be
   4055  1.1.2.2  skrll 			 * EXCLUDED and sent reliable.
   4056  1.1.2.2  skrll 			 */
   4057  1.1.2.2  skrll 			if (srcv->sinfo_flags & MSG_PR_SCTP_TTL) {
   4058  1.1.2.2  skrll 				template->flags |= SCTP_PR_SCTP_ENABLED;
   4059  1.1.2.2  skrll 			}
   4060  1.1.2.2  skrll 			if (srcv->sinfo_flags & MSG_PR_SCTP_BUF) {
   4061  1.1.2.2  skrll 				template->flags |= SCTP_PR_SCTP_BUFFER;
   4062  1.1.2.2  skrll 			}
   4063  1.1.2.2  skrll 		}
   4064  1.1.2.2  skrll 	}
   4065  1.1.2.2  skrll 	template->asoc = &stcb->asoc;
   4066  1.1.2.2  skrll }
   4067  1.1.2.2  skrll 
   4068  1.1.2.2  skrll 
   4069  1.1.2.2  skrll int
   4070  1.1.2.2  skrll sctp_get_frag_point(struct sctp_tcb *stcb,
   4071  1.1.2.2  skrll 		    struct sctp_association *asoc)
   4072  1.1.2.2  skrll {
   4073  1.1.2.2  skrll 	int siz, ovh;
   4074  1.1.2.2  skrll 
   4075  1.1.2.2  skrll 	/* For endpoints that have both 6 and 4 addresses
   4076  1.1.2.2  skrll 	 * we must reserver room for the 6 ip header, for
   4077  1.1.2.2  skrll 	 * those that are only dealing with V4 we use
   4078  1.1.2.2  skrll 	 * a larger frag point.
   4079  1.1.2.2  skrll 	 */
   4080  1.1.2.2  skrll  	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   4081  1.1.2.2  skrll 		ovh = SCTP_MED_OVERHEAD;
   4082  1.1.2.2  skrll 	} else {
   4083  1.1.2.2  skrll 		ovh = SCTP_MED_V4_OVERHEAD;
   4084  1.1.2.2  skrll 	}
   4085  1.1.2.2  skrll 
   4086  1.1.2.2  skrll 	if (stcb->sctp_ep->sctp_frag_point > asoc->smallest_mtu)
   4087  1.1.2.2  skrll 		siz = asoc->smallest_mtu - ovh;
   4088  1.1.2.2  skrll 	else
   4089  1.1.2.2  skrll 		siz = (stcb->sctp_ep->sctp_frag_point - ovh);
   4090  1.1.2.2  skrll /*
   4091  1.1.2.2  skrll   if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) { */
   4092  1.1.2.2  skrll 		/* A data chunk MUST fit in a cluster */
   4093  1.1.2.2  skrll /*		siz = (MCLBYTES - sizeof(struct sctp_data_chunk));*/
   4094  1.1.2.2  skrll /*	}*/
   4095  1.1.2.2  skrll 
   4096  1.1.2.2  skrll 	if (siz % 4) {
   4097  1.1.2.2  skrll 		/* make it an even word boundary please */
   4098  1.1.2.2  skrll 		siz -= (siz % 4);
   4099  1.1.2.2  skrll 	}
   4100  1.1.2.2  skrll 	return (siz);
   4101  1.1.2.2  skrll }
   4102  1.1.2.2  skrll extern unsigned int sctp_max_chunks_on_queue;
   4103  1.1.2.2  skrll 
   4104  1.1.2.2  skrll #define   SBLOCKWAIT(f)   (((f)&MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
   4105  1.1.2.2  skrll 
   4106  1.1.2.2  skrll static int
   4107  1.1.2.2  skrll sctp_msg_append(struct sctp_tcb *stcb,
   4108  1.1.2.2  skrll 		struct sctp_nets *net,
   4109  1.1.2.2  skrll 		struct mbuf *m,
   4110  1.1.2.2  skrll 		struct sctp_sndrcvinfo *srcv,
   4111  1.1.2.2  skrll 		int flags)
   4112  1.1.2.2  skrll {
   4113  1.1.2.2  skrll 	struct socket *so;
   4114  1.1.2.2  skrll 	struct sctp_association *asoc;
   4115  1.1.2.2  skrll 	struct sctp_stream_out *strq;
   4116  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   4117  1.1.2.2  skrll 	struct sctpchunk_listhead tmp;
   4118  1.1.2.2  skrll 	struct sctp_tmit_chunk template;
   4119  1.1.2.2  skrll 	struct mbuf *n, *mnext;
   4120  1.1.2.2  skrll 	struct mbuf *mm;
   4121  1.1.2.2  skrll 	unsigned int dataout, siz;
   4122  1.1.2.2  skrll 	int mbcnt = 0;
   4123  1.1.2.2  skrll 	int mbcnt_e = 0;
   4124  1.1.2.2  skrll 	int error = 0;
   4125  1.1.2.2  skrll 
   4126  1.1.2.2  skrll 	if ((stcb == NULL) || (net == NULL) || (m == NULL) || (srcv == NULL)) {
   4127  1.1.2.2  skrll 		/* Software fault, you blew it on the call */
   4128  1.1.2.2  skrll #ifdef SCTP_DEBUG
   4129  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   4130  1.1.2.2  skrll 			printf("software error in sctp_msg_append:1\n");
   4131  1.1.2.2  skrll 			printf("stcb:%p net:%p m:%p srcv:%p\n",
   4132  1.1.2.2  skrll 			       stcb, net, m, srcv);
   4133  1.1.2.2  skrll 		}
   4134  1.1.2.2  skrll #endif
   4135  1.1.2.2  skrll 		if (m)
   4136  1.1.2.2  skrll 			sctp_m_freem(m);
   4137  1.1.2.2  skrll 		return (EFAULT);
   4138  1.1.2.2  skrll 	}
   4139  1.1.2.2  skrll 	so = stcb->sctp_socket;
   4140  1.1.2.2  skrll 	asoc = &stcb->asoc;
   4141  1.1.2.2  skrll 	if (srcv->sinfo_flags & MSG_ABORT) {
   4142  1.1.2.2  skrll 		if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
   4143  1.1.2.2  skrll 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
   4144  1.1.2.2  skrll 			/* It has to be up before we abort */
   4145  1.1.2.2  skrll 			/* how big is the user initiated abort? */
   4146  1.1.2.2  skrll 			if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.len)) {
   4147  1.1.2.2  skrll 				dataout = m->m_pkthdr.len;
   4148  1.1.2.2  skrll 			} else {
   4149  1.1.2.2  skrll 				/* we must count */
   4150  1.1.2.2  skrll 				dataout = 0;
   4151  1.1.2.2  skrll 				for (n = m; n; n = n->m_next) {
   4152  1.1.2.2  skrll 					dataout += n->m_len;
   4153  1.1.2.2  skrll 				}
   4154  1.1.2.2  skrll 			}
   4155  1.1.2.2  skrll 			M_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
   4156  1.1.2.2  skrll 			if (m) {
   4157  1.1.2.2  skrll 				struct sctp_paramhdr *ph;
   4158  1.1.2.2  skrll 				m->m_len = sizeof(struct sctp_paramhdr) + dataout;
   4159  1.1.2.2  skrll 				ph = mtod(m, struct sctp_paramhdr *);
   4160  1.1.2.2  skrll 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
   4161  1.1.2.2  skrll 				ph->param_length = htons(m->m_len);
   4162  1.1.2.2  skrll 			}
   4163  1.1.2.2  skrll 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, m);
   4164  1.1.2.2  skrll 			m = NULL;
   4165  1.1.2.2  skrll 		} else {
   4166  1.1.2.2  skrll 			/* Only free if we don't send an abort */
   4167  1.1.2.2  skrll 			;
   4168  1.1.2.2  skrll 		}
   4169  1.1.2.2  skrll 		goto out;
   4170  1.1.2.2  skrll 	}
   4171  1.1.2.2  skrll 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
   4172  1.1.2.2  skrll 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
   4173  1.1.2.2  skrll 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
   4174  1.1.2.2  skrll 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
   4175  1.1.2.2  skrll 		/* got data while shutting down */
   4176  1.1.2.2  skrll 		error = ECONNRESET;
   4177  1.1.2.2  skrll 		goto out;
   4178  1.1.2.2  skrll 	}
   4179  1.1.2.2  skrll 
   4180  1.1.2.2  skrll 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
   4181  1.1.2.2  skrll 		/* Invalid stream number */
   4182  1.1.2.2  skrll 		error = EINVAL;
   4183  1.1.2.2  skrll 		goto out;
   4184  1.1.2.2  skrll 	}
   4185  1.1.2.2  skrll 	if (asoc->strmout == NULL) {
   4186  1.1.2.2  skrll 		/* huh? software error */
   4187  1.1.2.2  skrll #ifdef SCTP_DEBUG
   4188  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   4189  1.1.2.2  skrll 			printf("software error in sctp_msg_append:2\n");
   4190  1.1.2.2  skrll 		}
   4191  1.1.2.2  skrll #endif
   4192  1.1.2.2  skrll 		error = EFAULT;
   4193  1.1.2.2  skrll 		goto out;
   4194  1.1.2.2  skrll 	}
   4195  1.1.2.2  skrll 	strq = &asoc->strmout[srcv->sinfo_stream];
   4196  1.1.2.2  skrll 	/* how big is it ? */
   4197  1.1.2.2  skrll 	if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.len)) {
   4198  1.1.2.2  skrll 		dataout = m->m_pkthdr.len;
   4199  1.1.2.2  skrll 	} else {
   4200  1.1.2.2  skrll 		/* we must count */
   4201  1.1.2.2  skrll 		dataout = 0;
   4202  1.1.2.2  skrll 		for (n = m; n; n = n->m_next) {
   4203  1.1.2.2  skrll 			dataout += n->m_len;
   4204  1.1.2.2  skrll 		}
   4205  1.1.2.2  skrll 	}
   4206  1.1.2.2  skrll #ifdef SCTP_DEBUG
   4207  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   4208  1.1.2.2  skrll 		printf("Attempt to send out %d bytes\n",
   4209  1.1.2.2  skrll 		       dataout);
   4210  1.1.2.2  skrll 	}
   4211  1.1.2.2  skrll #endif
   4212  1.1.2.2  skrll 
   4213  1.1.2.2  skrll 	/* lock the socket buf */
   4214  1.1.2.2  skrll 	error = sblock(&so->so_snd, SBLOCKWAIT(flags));
   4215  1.1.2.2  skrll 	if (error)
   4216  1.1.2.2  skrll 		goto out_locked;
   4217  1.1.2.2  skrll 
   4218  1.1.2.2  skrll 	if (dataout > so->so_snd.sb_hiwat) {
   4219  1.1.2.2  skrll 		/* It will NEVER fit */
   4220  1.1.2.2  skrll 		error = EMSGSIZE;
   4221  1.1.2.2  skrll 		goto release;
   4222  1.1.2.2  skrll 	}
   4223  1.1.2.2  skrll 	if ((srcv->sinfo_flags & MSG_EOF) &&
   4224  1.1.2.2  skrll 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
   4225  1.1.2.2  skrll 	    (dataout == 0)
   4226  1.1.2.2  skrll 		) {
   4227  1.1.2.2  skrll 		goto zap_by_it_all;
   4228  1.1.2.2  skrll 	}
   4229  1.1.2.2  skrll 	if ((so->so_snd.sb_hiwat <
   4230  1.1.2.2  skrll 	     (dataout + asoc->total_output_queue_size)) ||
   4231  1.1.2.2  skrll 	    (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
   4232  1.1.2.2  skrll 	    (asoc->total_output_mbuf_queue_size >
   4233  1.1.2.2  skrll 	     so->so_snd.sb_mbmax)
   4234  1.1.2.2  skrll 		) {
   4235  1.1.2.2  skrll 		/* XXX Buffer space hunt for data to skip */
   4236  1.1.2.2  skrll 		if (asoc->peer_supports_prsctp) {
   4237  1.1.2.2  skrll 			sctp_prune_prsctp(stcb, asoc, srcv, dataout);
   4238  1.1.2.2  skrll 		}
   4239  1.1.2.2  skrll 		while ((so->so_snd.sb_hiwat <
   4240  1.1.2.2  skrll 		    (dataout + asoc->total_output_queue_size)) ||
   4241  1.1.2.2  skrll 		    (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
   4242  1.1.2.2  skrll 		    (asoc->total_output_mbuf_queue_size >
   4243  1.1.2.2  skrll 		    so->so_snd.sb_mbmax)) {
   4244  1.1.2.2  skrll 			struct sctp_inpcb *inp;
   4245  1.1.2.2  skrll 			/* Now did we free up enough room? */
   4246  1.1.2.2  skrll 			if (so->so_state & SS_NBIO) {
   4247  1.1.2.2  skrll 				/* Non-blocking io in place */
   4248  1.1.2.2  skrll 				error = EWOULDBLOCK;
   4249  1.1.2.2  skrll 				goto release;
   4250  1.1.2.2  skrll 			}
   4251  1.1.2.2  skrll 			/*
   4252  1.1.2.2  skrll 			 * We store off a pointer to the endpoint.
   4253  1.1.2.2  skrll 			 * Since on return from this we must check to
   4254  1.1.2.2  skrll 			 * see if an so_error is set. If so we may have
   4255  1.1.2.2  skrll 			 * been reset and our stcb destroyed. Returning
   4256  1.1.2.2  skrll 			 * an error will cause the correct error return
   4257  1.1.2.2  skrll 			 * through and fix this all.
   4258  1.1.2.2  skrll 			 */
   4259  1.1.2.2  skrll 			inp = stcb->sctp_ep;
   4260  1.1.2.2  skrll 			/*
   4261  1.1.2.2  skrll 			 * Not sure how else to do this since
   4262  1.1.2.2  skrll 			 * the level we suspended at is not
   4263  1.1.2.2  skrll 			 * known deep down where we are. I will
   4264  1.1.2.2  skrll 			 * drop to spl0() so that others can
   4265  1.1.2.2  skrll 			 * get in.
   4266  1.1.2.2  skrll 			 */
   4267  1.1.2.2  skrll 
   4268  1.1.2.2  skrll 			inp->sctp_tcb_at_block = (void *)stcb;
   4269  1.1.2.2  skrll 			inp->error_on_block = 0;
   4270  1.1.2.2  skrll 			sbunlock(&so->so_snd);
   4271  1.1.2.2  skrll 			error = sbwait(&so->so_snd);
   4272  1.1.2.2  skrll 			/*
   4273  1.1.2.2  skrll 			 * XXX: This is ugly but I have
   4274  1.1.2.2  skrll 			 * recreated most of what goes on to
   4275  1.1.2.2  skrll 			 * block in the sb. UGHH
   4276  1.1.2.2  skrll 			 * May want to add the bit about being
   4277  1.1.2.2  skrll 			 * no longer connected.. but this then
   4278  1.1.2.2  skrll 			 * further dooms the UDP model NOT to
   4279  1.1.2.2  skrll 			 * allow this.
   4280  1.1.2.2  skrll 			 */
   4281  1.1.2.2  skrll 			inp->sctp_tcb_at_block = 0;
   4282  1.1.2.2  skrll 			if (inp->error_on_block)
   4283  1.1.2.2  skrll 				error = inp->error_on_block;
   4284  1.1.2.2  skrll 			if (so->so_error)
   4285  1.1.2.2  skrll 				error = so->so_error;
   4286  1.1.2.2  skrll 			if (error) {
   4287  1.1.2.2  skrll 				goto out_locked;
   4288  1.1.2.2  skrll 			}
   4289  1.1.2.2  skrll 			error = sblock(&so->so_snd, M_WAITOK);
   4290  1.1.2.2  skrll 			if (error)
   4291  1.1.2.2  skrll 				goto out_locked;
   4292  1.1.2.2  skrll 			/* Otherwise we cycle back and recheck
   4293  1.1.2.2  skrll 			 * the space
   4294  1.1.2.2  skrll 			 */
   4295  1.1.2.2  skrll #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
   4296  1.1.2.2  skrll 			if (so->so_rcv.sb_state & SBS_CANTSENDMORE) {
   4297  1.1.2.2  skrll #else
   4298  1.1.2.2  skrll 			if (so->so_state & SS_CANTSENDMORE) {
   4299  1.1.2.2  skrll #endif
   4300  1.1.2.2  skrll 				error = EPIPE;
   4301  1.1.2.2  skrll 				goto release;
   4302  1.1.2.2  skrll 			}
   4303  1.1.2.2  skrll 			if (so->so_error) {
   4304  1.1.2.2  skrll 				error = so->so_error;
   4305  1.1.2.2  skrll 				goto release;
   4306  1.1.2.2  skrll 			}
   4307  1.1.2.2  skrll 		}
   4308  1.1.2.2  skrll 	}
   4309  1.1.2.2  skrll 	/* If we have a packet header fix it if it was broke */
   4310  1.1.2.2  skrll 	if (m->m_flags & M_PKTHDR) {
   4311  1.1.2.2  skrll 		m->m_pkthdr.len = dataout;
   4312  1.1.2.2  skrll 	}
   4313  1.1.2.2  skrll 	/* use the smallest one, user set value or
   4314  1.1.2.2  skrll 	 * smallest mtu of the asoc
   4315  1.1.2.2  skrll 	 */
   4316  1.1.2.2  skrll 	siz = sctp_get_frag_point(stcb, asoc);
   4317  1.1.2.2  skrll 	if ((dataout) && (dataout <= siz)) {
   4318  1.1.2.2  skrll 		/* Fast path */
   4319  1.1.2.2  skrll 		chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   4320  1.1.2.2  skrll 		if (chk == NULL) {
   4321  1.1.2.2  skrll 			error = ENOMEM;
   4322  1.1.2.2  skrll 			goto release;
   4323  1.1.2.2  skrll 		}
   4324  1.1.2.2  skrll 		sctp_prepare_chunk(chk, stcb, srcv, strq, net);
   4325  1.1.2.2  skrll 		chk->whoTo->ref_count++;
   4326  1.1.2.2  skrll 		chk->rec.data.rcv_flags |= SCTP_DATA_NOT_FRAG;
   4327  1.1.2.2  skrll 
   4328  1.1.2.2  skrll 		/* no flags yet, FRAGMENT_OK goes here */
   4329  1.1.2.2  skrll 		sctppcbinfo.ipi_count_chunk++;
   4330  1.1.2.2  skrll 		sctppcbinfo.ipi_gencnt_chunk++;
   4331  1.1.2.2  skrll 		asoc->chunks_on_out_queue++;
   4332  1.1.2.2  skrll 		chk->data = m;
   4333  1.1.2.2  skrll 		m = NULL;
   4334  1.1.2.2  skrll 		/* Total in the MSIZE */
   4335  1.1.2.2  skrll 		for (mm = chk->data; mm; mm = mm->m_next) {
   4336  1.1.2.2  skrll 			mbcnt += MSIZE;
   4337  1.1.2.2  skrll 			if (mm->m_flags & M_EXT) {
   4338  1.1.2.2  skrll 				mbcnt += chk->data->m_ext.ext_size;
   4339  1.1.2.2  skrll 			}
   4340  1.1.2.2  skrll 		}
   4341  1.1.2.2  skrll 		/* fix up the send_size if it is not present */
   4342  1.1.2.2  skrll 		chk->send_size = dataout;
   4343  1.1.2.2  skrll 		chk->book_size = chk->send_size;
   4344  1.1.2.2  skrll 		chk->mbcnt = mbcnt;
   4345  1.1.2.2  skrll 		/* ok, we are commited */
   4346  1.1.2.2  skrll 		if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
   4347  1.1.2.2  skrll 			/* bump the ssn if we are unordered. */
   4348  1.1.2.2  skrll 			strq->next_sequence_sent++;
   4349  1.1.2.2  skrll 		}
   4350  1.1.2.2  skrll 		chk->data->m_nextpkt = 0;
   4351  1.1.2.2  skrll 		asoc->stream_queue_cnt++;
   4352  1.1.2.2  skrll 		TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
   4353  1.1.2.2  skrll 		/* now check if this stream is on the wheel */
   4354  1.1.2.2  skrll 		if ((strq->next_spoke.tqe_next == NULL) &&
   4355  1.1.2.2  skrll 		    (strq->next_spoke.tqe_prev == NULL)) {
   4356  1.1.2.2  skrll 			/* Insert it on the wheel since it is not
   4357  1.1.2.2  skrll 			 * on it currently
   4358  1.1.2.2  skrll 			 */
   4359  1.1.2.2  skrll 			sctp_insert_on_wheel(asoc, strq);
   4360  1.1.2.2  skrll 		}
   4361  1.1.2.2  skrll 	} else if ((dataout) && (dataout > siz)) {
   4362  1.1.2.2  skrll 		/* Slow path */
   4363  1.1.2.2  skrll 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT) &&
   4364  1.1.2.2  skrll 		    (dataout > siz)) {
   4365  1.1.2.2  skrll 			error = EMSGSIZE;
   4366  1.1.2.2  skrll 			goto release;
   4367  1.1.2.2  skrll 		}
   4368  1.1.2.2  skrll 		/* setup the template */
   4369  1.1.2.2  skrll 		sctp_prepare_chunk(&template, stcb, srcv, strq, net);
   4370  1.1.2.2  skrll 
   4371  1.1.2.2  skrll 		n = m;
   4372  1.1.2.2  skrll 		while (dataout > siz) {
   4373  1.1.2.2  skrll 			/*
   4374  1.1.2.2  skrll 			 * We can wait since this is called from the user
   4375  1.1.2.2  skrll 			 * send side
   4376  1.1.2.2  skrll 			 */
   4377  1.1.2.2  skrll 			n->m_nextpkt = m_split(n, siz, M_WAIT);
   4378  1.1.2.2  skrll 			if (n->m_nextpkt == NULL) {
   4379  1.1.2.2  skrll 				error = EFAULT;
   4380  1.1.2.2  skrll 				goto release;
   4381  1.1.2.2  skrll 			}
   4382  1.1.2.2  skrll 			dataout -= siz;
   4383  1.1.2.2  skrll 			n = n->m_nextpkt;
   4384  1.1.2.2  skrll 		}
   4385  1.1.2.2  skrll 		/*
   4386  1.1.2.2  skrll 		 * ok, now we have a chain on m where m->m_nextpkt points to
   4387  1.1.2.2  skrll 		 * the next chunk and m/m->m_next chain is the piece to send.
   4388  1.1.2.2  skrll 		 * We must go through the chains and thread them on to
   4389  1.1.2.2  skrll 		 * sctp_tmit_chunk chains and place them all on the stream
   4390  1.1.2.2  skrll 		 * queue, breaking the m->m_nextpkt pointers as we go.
   4391  1.1.2.2  skrll 		 */
   4392  1.1.2.2  skrll 		n = m;
   4393  1.1.2.2  skrll 		TAILQ_INIT(&tmp);
   4394  1.1.2.2  skrll 		while (n) {
   4395  1.1.2.2  skrll 			/*
   4396  1.1.2.2  skrll 			 * first go through and allocate a sctp_tmit chunk
   4397  1.1.2.2  skrll 			 * for each chunk piece
   4398  1.1.2.2  skrll 			 */
   4399  1.1.2.2  skrll 			chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   4400  1.1.2.2  skrll 			if (chk == NULL) {
   4401  1.1.2.2  skrll 				/*
   4402  1.1.2.2  skrll 				 * ok we must spin through and dump anything
   4403  1.1.2.2  skrll 				 * we have allocated and then jump to the
   4404  1.1.2.2  skrll 				 * no_membad
   4405  1.1.2.2  skrll 				 */
   4406  1.1.2.2  skrll 				chk = TAILQ_FIRST(&tmp);
   4407  1.1.2.2  skrll 				while (chk) {
   4408  1.1.2.2  skrll 					TAILQ_REMOVE(&tmp, chk, sctp_next);
   4409  1.1.2.2  skrll 					SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   4410  1.1.2.2  skrll 					sctppcbinfo.ipi_count_chunk--;
   4411  1.1.2.2  skrll 					asoc->chunks_on_out_queue--;
   4412  1.1.2.2  skrll 					if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   4413  1.1.2.2  skrll 						panic("Chunk count is negative");
   4414  1.1.2.2  skrll 					}
   4415  1.1.2.2  skrll 					sctppcbinfo.ipi_gencnt_chunk++;
   4416  1.1.2.2  skrll 					chk = TAILQ_FIRST(&tmp);
   4417  1.1.2.2  skrll 				}
   4418  1.1.2.2  skrll 				error = ENOMEM;
   4419  1.1.2.2  skrll 				goto release;
   4420  1.1.2.2  skrll 			}
   4421  1.1.2.2  skrll 			sctppcbinfo.ipi_count_chunk++;
   4422  1.1.2.2  skrll 			asoc->chunks_on_out_queue++;
   4423  1.1.2.2  skrll 
   4424  1.1.2.2  skrll 			sctppcbinfo.ipi_gencnt_chunk++;
   4425  1.1.2.2  skrll 			*chk = template;
   4426  1.1.2.2  skrll 			chk->whoTo->ref_count++;
   4427  1.1.2.2  skrll 			chk->data = n;
   4428  1.1.2.2  skrll 			/* Total in the MSIZE */
   4429  1.1.2.2  skrll 			mbcnt_e = 0;
   4430  1.1.2.2  skrll 			for (mm = chk->data; mm; mm = mm->m_next) {
   4431  1.1.2.2  skrll 				mbcnt_e += MSIZE;
   4432  1.1.2.2  skrll 				if (mm->m_flags & M_EXT) {
   4433  1.1.2.2  skrll 					mbcnt_e += chk->data->m_ext.ext_size;
   4434  1.1.2.2  skrll 				}
   4435  1.1.2.2  skrll 			}
   4436  1.1.2.2  skrll 			/* now fix the chk->send_size */
   4437  1.1.2.2  skrll 			if (chk->data->m_flags & M_PKTHDR) {
   4438  1.1.2.2  skrll 				chk->send_size = chk->data->m_pkthdr.len;
   4439  1.1.2.2  skrll 			} else {
   4440  1.1.2.2  skrll 				struct mbuf *nn;
   4441  1.1.2.2  skrll 				chk->send_size = 0;
   4442  1.1.2.2  skrll 				for (nn = chk->data; nn; nn = nn->m_next) {
   4443  1.1.2.2  skrll 					chk->send_size += nn->m_len;
   4444  1.1.2.2  skrll 				}
   4445  1.1.2.2  skrll 			}
   4446  1.1.2.2  skrll 			chk->book_size = chk->send_size;
   4447  1.1.2.2  skrll 			chk->mbcnt = mbcnt_e;
   4448  1.1.2.2  skrll 			mbcnt += mbcnt_e;
   4449  1.1.2.2  skrll 			if (chk->flags & SCTP_PR_SCTP_BUFFER) {
   4450  1.1.2.2  skrll 				asoc->sent_queue_cnt_removeable++;
   4451  1.1.2.2  skrll 			}
   4452  1.1.2.2  skrll 			n = n->m_nextpkt;
   4453  1.1.2.2  skrll 			TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
   4454  1.1.2.2  skrll 		}
   4455  1.1.2.2  skrll 		m = NULL;
   4456  1.1.2.2  skrll 		/* now that we have enough space for all de-couple the
   4457  1.1.2.2  skrll 		 * chain of mbufs by going through our temp array
   4458  1.1.2.2  skrll 		 * and breaking the pointers.
   4459  1.1.2.2  skrll 		 */
   4460  1.1.2.2  skrll 		/* ok, we are commited */
   4461  1.1.2.2  skrll 		if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
   4462  1.1.2.2  skrll 			/* bump the ssn if we are unordered. */
   4463  1.1.2.2  skrll 			strq->next_sequence_sent++;
   4464  1.1.2.2  skrll 		}
   4465  1.1.2.2  skrll 		/* Mark the first/last flags. This will
   4466  1.1.2.2  skrll 		 * result int a 3 for a single item on the list
   4467  1.1.2.2  skrll 		 */
   4468  1.1.2.2  skrll 		chk = TAILQ_FIRST(&tmp);
   4469  1.1.2.2  skrll 		chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
   4470  1.1.2.2  skrll 		chk = TAILQ_LAST(&tmp, sctpchunk_listhead);
   4471  1.1.2.2  skrll 		chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
   4472  1.1.2.2  skrll 		/* now break any chains on the queue and
   4473  1.1.2.2  skrll 		 * move it to the streams actual queue.
   4474  1.1.2.2  skrll 		 */
   4475  1.1.2.2  skrll 		chk = TAILQ_FIRST(&tmp);
   4476  1.1.2.2  skrll 		while (chk) {
   4477  1.1.2.2  skrll 			chk->data->m_nextpkt = 0;
   4478  1.1.2.2  skrll 			TAILQ_REMOVE(&tmp, chk, sctp_next);
   4479  1.1.2.2  skrll 			asoc->stream_queue_cnt++;
   4480  1.1.2.2  skrll 			TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
   4481  1.1.2.2  skrll 			chk = TAILQ_FIRST(&tmp);
   4482  1.1.2.2  skrll 		}
   4483  1.1.2.2  skrll 		/* now check if this stream is on the wheel */
   4484  1.1.2.2  skrll 		if ((strq->next_spoke.tqe_next == NULL) &&
   4485  1.1.2.2  skrll 		    (strq->next_spoke.tqe_prev == NULL)) {
   4486  1.1.2.2  skrll 			/* Insert it on the wheel since it is not
   4487  1.1.2.2  skrll 			 * on it currently
   4488  1.1.2.2  skrll 			 */
   4489  1.1.2.2  skrll 			sctp_insert_on_wheel(asoc, strq);
   4490  1.1.2.2  skrll 		}
   4491  1.1.2.2  skrll 	}
   4492  1.1.2.2  skrll 	/* has a SHUTDOWN been (also) requested by the user on this asoc? */
   4493  1.1.2.2  skrll zap_by_it_all:
   4494  1.1.2.2  skrll 
   4495  1.1.2.2  skrll 	if ((srcv->sinfo_flags & MSG_EOF) &&
   4496  1.1.2.2  skrll 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
   4497  1.1.2.2  skrll 
   4498  1.1.2.2  skrll 		int some_on_streamwheel = 0;
   4499  1.1.2.2  skrll 
   4500  1.1.2.2  skrll 		if (!TAILQ_EMPTY(&asoc->out_wheel)) {
   4501  1.1.2.2  skrll 			/* Check to see if some data queued */
   4502  1.1.2.2  skrll 			struct sctp_stream_out *outs;
   4503  1.1.2.2  skrll 			TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
   4504  1.1.2.2  skrll 				if (!TAILQ_EMPTY(&outs->outqueue)) {
   4505  1.1.2.2  skrll 					some_on_streamwheel = 1;
   4506  1.1.2.2  skrll 					break;
   4507  1.1.2.2  skrll 				}
   4508  1.1.2.2  skrll 			}
   4509  1.1.2.2  skrll 		}
   4510  1.1.2.2  skrll 
   4511  1.1.2.2  skrll 		if (TAILQ_EMPTY(&asoc->send_queue) &&
   4512  1.1.2.2  skrll 		    TAILQ_EMPTY(&asoc->sent_queue) &&
   4513  1.1.2.2  skrll 		    (some_on_streamwheel == 0)) {
   4514  1.1.2.2  skrll 			/* there is nothing queued to send, so I'm done... */
   4515  1.1.2.2  skrll 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
   4516  1.1.2.2  skrll 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
   4517  1.1.2.2  skrll 				/* only send SHUTDOWN the first time through */
   4518  1.1.2.2  skrll #ifdef SCTP_DEBUG
   4519  1.1.2.2  skrll 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   4520  1.1.2.2  skrll 					printf("%s:%d sends a shutdown\n",
   4521  1.1.2.2  skrll 					       __FILE__,
   4522  1.1.2.2  skrll 					       __LINE__
   4523  1.1.2.2  skrll 						);
   4524  1.1.2.2  skrll 				}
   4525  1.1.2.2  skrll #endif
   4526  1.1.2.2  skrll 				sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
   4527  1.1.2.2  skrll 				asoc->state = SCTP_STATE_SHUTDOWN_SENT;
   4528  1.1.2.2  skrll 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
   4529  1.1.2.2  skrll 						 asoc->primary_destination);
   4530  1.1.2.2  skrll 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
   4531  1.1.2.2  skrll 						 asoc->primary_destination);
   4532  1.1.2.2  skrll 			}
   4533  1.1.2.2  skrll 		} else {
   4534  1.1.2.2  skrll 			/*
   4535  1.1.2.2  skrll 			 * we still got (or just got) data to send, so set
   4536  1.1.2.2  skrll 			 * SHUTDOWN_PENDING
   4537  1.1.2.2  skrll 			 */
   4538  1.1.2.2  skrll 			/*
   4539  1.1.2.2  skrll 			 * XXX sockets draft says that MSG_EOF should be sent
   4540  1.1.2.2  skrll 			 * with no data.  currently, we will allow user data
   4541  1.1.2.2  skrll 			 * to be sent first and move to SHUTDOWN-PENDING
   4542  1.1.2.2  skrll 			 */
   4543  1.1.2.2  skrll 			asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
   4544  1.1.2.2  skrll 		}
   4545  1.1.2.2  skrll 	}
   4546  1.1.2.2  skrll #ifdef SCTP_MBCNT_LOGGING
   4547  1.1.2.2  skrll 	sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
   4548  1.1.2.2  skrll 		       asoc->total_output_queue_size,
   4549  1.1.2.2  skrll 		       dataout,
   4550  1.1.2.2  skrll 		       asoc->total_output_mbuf_queue_size,
   4551  1.1.2.2  skrll 		       mbcnt);
   4552  1.1.2.2  skrll #endif
   4553  1.1.2.2  skrll 	asoc->total_output_queue_size += dataout;
   4554  1.1.2.2  skrll 	asoc->total_output_mbuf_queue_size += mbcnt;
   4555  1.1.2.2  skrll 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
   4556  1.1.2.2  skrll 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
   4557  1.1.2.2  skrll 		so->so_snd.sb_cc += dataout;
   4558  1.1.2.2  skrll 		so->so_snd.sb_mbcnt += mbcnt;
   4559  1.1.2.2  skrll 	}
   4560  1.1.2.2  skrll 
   4561  1.1.2.2  skrll #ifdef SCTP_DEBUG
   4562  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   4563  1.1.2.2  skrll 		printf("++total out:%d total_mbuf_out:%d\n",
   4564  1.1.2.2  skrll 		       (int)asoc->total_output_queue_size,
   4565  1.1.2.2  skrll 		       (int)asoc->total_output_mbuf_queue_size);
   4566  1.1.2.2  skrll 	}
   4567  1.1.2.2  skrll #endif
   4568  1.1.2.2  skrll 
   4569  1.1.2.2  skrll release:
   4570  1.1.2.2  skrll 	sbunlock(&so->so_snd);
   4571  1.1.2.2  skrll out_locked:
   4572  1.1.2.2  skrll 
   4573  1.1.2.2  skrll out:
   4574  1.1.2.2  skrll 	if (m && m->m_nextpkt) {
   4575  1.1.2.2  skrll 		n = m;
   4576  1.1.2.2  skrll 		while (n) {
   4577  1.1.2.2  skrll 			mnext = n->m_nextpkt;
   4578  1.1.2.2  skrll 			n->m_nextpkt = NULL;
   4579  1.1.2.2  skrll 			sctp_m_freem(n);
   4580  1.1.2.2  skrll 			n = mnext;
   4581  1.1.2.2  skrll 		}
   4582  1.1.2.2  skrll 	} else if (m)
   4583  1.1.2.2  skrll 		sctp_m_freem(m);
   4584  1.1.2.2  skrll 
   4585  1.1.2.2  skrll 	return (error);
   4586  1.1.2.2  skrll }
   4587  1.1.2.2  skrll 
   4588  1.1.2.2  skrll static struct mbuf *
   4589  1.1.2.2  skrll sctp_copy_mbufchain(struct mbuf *clonechain,
   4590  1.1.2.2  skrll 		    struct mbuf *outchain)
   4591  1.1.2.2  skrll {
   4592  1.1.2.2  skrll 	struct mbuf *appendchain;
   4593  1.1.2.2  skrll #if defined(__FreeBSD__) || defined(__NetBSD__)
   4594  1.1.2.2  skrll 	/* Supposedly m_copypacket is an optimization, use it if we can */
   4595  1.1.2.2  skrll 	if (clonechain->m_flags & M_PKTHDR) {
   4596  1.1.2.2  skrll 		appendchain = m_copypacket(clonechain, M_DONTWAIT);
   4597  1.1.2.2  skrll 		sctp_pegs[SCTP_CACHED_SRC]++;
   4598  1.1.2.2  skrll 	} else
   4599  1.1.2.2  skrll 		appendchain = m_copy(clonechain, 0, M_COPYALL);
   4600  1.1.2.2  skrll #elif defined(__APPLE__)
   4601  1.1.2.2  skrll 	appendchain = sctp_m_copym(clonechain, 0, M_COPYALL, M_DONTWAIT);
   4602  1.1.2.2  skrll #else
   4603  1.1.2.2  skrll 	appendchain = m_copy(clonechain, 0, M_COPYALL);
   4604  1.1.2.2  skrll #endif
   4605  1.1.2.2  skrll 
   4606  1.1.2.2  skrll 	if (appendchain == NULL) {
   4607  1.1.2.2  skrll 		/* error */
   4608  1.1.2.2  skrll 		if (outchain)
   4609  1.1.2.2  skrll 			sctp_m_freem(outchain);
   4610  1.1.2.2  skrll 		return (NULL);
   4611  1.1.2.2  skrll 	}
   4612  1.1.2.2  skrll 	if (outchain) {
   4613  1.1.2.2  skrll 		/* tack on to the end */
   4614  1.1.2.2  skrll 		struct mbuf *m;
   4615  1.1.2.2  skrll 		m = outchain;
   4616  1.1.2.2  skrll 		while (m) {
   4617  1.1.2.2  skrll 			if (m->m_next == NULL) {
   4618  1.1.2.2  skrll 				m->m_next = appendchain;
   4619  1.1.2.2  skrll 				break;
   4620  1.1.2.2  skrll 			}
   4621  1.1.2.2  skrll 			m = m->m_next;
   4622  1.1.2.2  skrll 		}
   4623  1.1.2.2  skrll 		if (outchain->m_flags & M_PKTHDR) {
   4624  1.1.2.2  skrll 			int append_tot;
   4625  1.1.2.2  skrll 			struct mbuf *t;
   4626  1.1.2.2  skrll 			t = appendchain;
   4627  1.1.2.2  skrll 			append_tot = 0;
   4628  1.1.2.2  skrll 			while (t) {
   4629  1.1.2.2  skrll 				append_tot += t->m_len;
   4630  1.1.2.2  skrll 				t = t->m_next;
   4631  1.1.2.2  skrll 			}
   4632  1.1.2.2  skrll 			outchain->m_pkthdr.len += append_tot;
   4633  1.1.2.2  skrll 		}
   4634  1.1.2.2  skrll 		return (outchain);
   4635  1.1.2.2  skrll 	} else {
   4636  1.1.2.2  skrll 		return (appendchain);
   4637  1.1.2.2  skrll 	}
   4638  1.1.2.2  skrll }
   4639  1.1.2.2  skrll 
   4640  1.1.2.2  skrll static void
   4641  1.1.2.2  skrll sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr, u_int32_t val)
   4642  1.1.2.2  skrll {
   4643  1.1.2.2  skrll 	struct sctp_copy_all *ca;
   4644  1.1.2.2  skrll 	struct mbuf *m;
   4645  1.1.2.2  skrll 	int turned_on_nonblock=0, ret;
   4646  1.1.2.2  skrll 
   4647  1.1.2.2  skrll 	ca = (struct sctp_copy_all *)ptr;
   4648  1.1.2.2  skrll 	if (ca->m == NULL) {
   4649  1.1.2.2  skrll 		return;
   4650  1.1.2.2  skrll 	}
   4651  1.1.2.2  skrll 	if (ca->inp != inp) {
   4652  1.1.2.2  skrll 		/* TSNH */
   4653  1.1.2.2  skrll 		return;
   4654  1.1.2.2  skrll 	}
   4655  1.1.2.2  skrll 	m = sctp_copy_mbufchain(ca->m, NULL);
   4656  1.1.2.2  skrll 	if (m == NULL) {
   4657  1.1.2.2  skrll 		/* can't copy so we are done */
   4658  1.1.2.2  skrll 		ca->cnt_failed++;
   4659  1.1.2.2  skrll 		return;
   4660  1.1.2.2  skrll 	}
   4661  1.1.2.2  skrll 	if ((stcb->sctp_socket->so_state & SS_NBIO) == 0) {
   4662  1.1.2.2  skrll 		/* we have to do this non-blocking */
   4663  1.1.2.2  skrll 		turned_on_nonblock = 1;
   4664  1.1.2.2  skrll 		stcb->sctp_socket->so_state |= SS_NBIO;
   4665  1.1.2.2  skrll 	}
   4666  1.1.2.2  skrll 	ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m, &ca->sndrcv, 0);
   4667  1.1.2.2  skrll 	if (turned_on_nonblock) {
   4668  1.1.2.2  skrll 		/* we turned on non-blocking so turn it off */
   4669  1.1.2.2  skrll 		stcb->sctp_socket->so_state &= ~SS_NBIO;
   4670  1.1.2.2  skrll 	}
   4671  1.1.2.2  skrll 	if (ret) {
   4672  1.1.2.2  skrll 		ca->cnt_failed++;
   4673  1.1.2.2  skrll 	} else {
   4674  1.1.2.2  skrll 		ca->cnt_sent++;
   4675  1.1.2.2  skrll 	}
   4676  1.1.2.2  skrll }
   4677  1.1.2.2  skrll 
   4678  1.1.2.2  skrll static void
   4679  1.1.2.2  skrll sctp_sendall_completes(void *ptr, u_int32_t val)
   4680  1.1.2.2  skrll {
   4681  1.1.2.2  skrll 	struct sctp_copy_all *ca;
   4682  1.1.2.2  skrll 	ca = (struct sctp_copy_all *)ptr;
   4683  1.1.2.2  skrll 	/* Do a notify here?
   4684  1.1.2.2  skrll 	 * Kacheong suggests that the notify
   4685  1.1.2.2  skrll 	 * be done at the send time.. so you would
   4686  1.1.2.2  skrll 	 * push up a notification if any send failed.
   4687  1.1.2.2  skrll 	 * Don't know if this is feasable since the
   4688  1.1.2.2  skrll 	 * only failures we have is "memory" related and
   4689  1.1.2.2  skrll 	 * if you cannot get an mbuf to send the data
   4690  1.1.2.2  skrll 	 * you surely can't get an mbuf to send up
   4691  1.1.2.2  skrll 	 * to notify the user you can't send the data :->
   4692  1.1.2.2  skrll 	 */
   4693  1.1.2.2  skrll 
   4694  1.1.2.2  skrll 	/* now free everything */
   4695  1.1.2.2  skrll 	m_freem(ca->m);
   4696  1.1.2.2  skrll 	free(ca, M_PCB);
   4697  1.1.2.2  skrll }
   4698  1.1.2.2  skrll 
   4699  1.1.2.2  skrll 
   4700  1.1.2.2  skrll #define	MC_ALIGN(m, len) do {						\
   4701  1.1.2.2  skrll 	(m)->m_data += (MCLBYTES - (len)) & ~(sizeof(long) - 1);		\
   4702  1.1.2.2  skrll } while (0)
   4703  1.1.2.2  skrll 
   4704  1.1.2.2  skrll 
   4705  1.1.2.2  skrll 
   4706  1.1.2.2  skrll static struct mbuf *
   4707  1.1.2.2  skrll sctp_copy_out_all(struct uio *uio, int len)
   4708  1.1.2.2  skrll {
   4709  1.1.2.2  skrll 	struct mbuf *ret, *at;
   4710  1.1.2.2  skrll 	int left, willcpy, cancpy, error;
   4711  1.1.2.2  skrll 
   4712  1.1.2.2  skrll 	MGETHDR(ret, M_WAIT, MT_HEADER);
   4713  1.1.2.2  skrll 	if (ret == NULL) {
   4714  1.1.2.2  skrll 		/* TSNH */
   4715  1.1.2.2  skrll 		return (NULL);
   4716  1.1.2.2  skrll 	}
   4717  1.1.2.2  skrll 	left = len;
   4718  1.1.2.2  skrll 	ret->m_len = 0;
   4719  1.1.2.2  skrll 	ret->m_pkthdr.len = len;
   4720  1.1.2.2  skrll 	MCLGET(ret, M_WAIT);
   4721  1.1.2.2  skrll 	if (ret == NULL) {
   4722  1.1.2.2  skrll 		return (NULL);
   4723  1.1.2.2  skrll 	}
   4724  1.1.2.2  skrll 	if ((ret->m_flags & M_EXT) == 0) {
   4725  1.1.2.2  skrll 		m_freem (ret);
   4726  1.1.2.2  skrll 		return (NULL);
   4727  1.1.2.2  skrll 	}
   4728  1.1.2.2  skrll 	cancpy = M_TRAILINGSPACE(ret);
   4729  1.1.2.2  skrll 	willcpy = min(cancpy, left);
   4730  1.1.2.2  skrll 	at = ret;
   4731  1.1.2.2  skrll 	while (left > 0) {
   4732  1.1.2.2  skrll 		/* Align data to the end */
   4733  1.1.2.2  skrll 		MC_ALIGN(at, willcpy);
   4734  1.1.2.2  skrll 		error = uiomove(mtod(at, void *), willcpy, uio);
   4735  1.1.2.2  skrll 		if (error) {
   4736  1.1.2.2  skrll 		err_out_now:
   4737  1.1.2.2  skrll 			m_freem(ret);
   4738  1.1.2.2  skrll 			return (NULL);
   4739  1.1.2.2  skrll 		}
   4740  1.1.2.2  skrll 		at->m_len = willcpy;
   4741  1.1.2.2  skrll 		at->m_nextpkt = at->m_next = 0;
   4742  1.1.2.2  skrll 		left -= willcpy;
   4743  1.1.2.2  skrll 		if (left > 0) {
   4744  1.1.2.2  skrll 			MGET(at->m_next, M_WAIT, MT_DATA);
   4745  1.1.2.2  skrll 			if (at->m_next == NULL) {
   4746  1.1.2.2  skrll 				goto err_out_now;
   4747  1.1.2.2  skrll 			}
   4748  1.1.2.2  skrll 			at = at->m_next;
   4749  1.1.2.2  skrll 			at->m_len = 0;
   4750  1.1.2.2  skrll 			MCLGET(at, M_WAIT);
   4751  1.1.2.2  skrll 			if (at == NULL) {
   4752  1.1.2.2  skrll 				goto err_out_now;
   4753  1.1.2.2  skrll 			}
   4754  1.1.2.2  skrll 			if ((at->m_flags & M_EXT) == 0) {
   4755  1.1.2.2  skrll 				goto err_out_now;
   4756  1.1.2.2  skrll 			}
   4757  1.1.2.2  skrll 			cancpy = M_TRAILINGSPACE(at);
   4758  1.1.2.2  skrll 			willcpy = min(cancpy, left);
   4759  1.1.2.2  skrll 		}
   4760  1.1.2.2  skrll 	}
   4761  1.1.2.2  skrll 	return (ret);
   4762  1.1.2.2  skrll }
   4763  1.1.2.2  skrll 
   4764  1.1.2.2  skrll static int
   4765  1.1.2.2  skrll sctp_sendall (struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m, struct sctp_sndrcvinfo *srcv)
   4766  1.1.2.2  skrll {
   4767  1.1.2.2  skrll 	int ret;
   4768  1.1.2.2  skrll 	struct sctp_copy_all *ca;
   4769  1.1.2.2  skrll 	ca = malloc(sizeof(struct sctp_copy_all), M_PCB, M_WAIT);
   4770  1.1.2.2  skrll 	if (ca == NULL) {
   4771  1.1.2.2  skrll 		m_freem(m);
   4772  1.1.2.2  skrll 		return (ENOMEM);
   4773  1.1.2.2  skrll 	}
   4774  1.1.2.2  skrll 	memset (ca, 0, sizeof(struct sctp_copy_all));
   4775  1.1.2.2  skrll 
   4776  1.1.2.2  skrll 	ca->inp = inp;
   4777  1.1.2.2  skrll 	ca->sndrcv = *srcv;
   4778  1.1.2.2  skrll 	/* take off the sendall flag, it would
   4779  1.1.2.2  skrll 	 * be bad if we failed to do this  :-0
   4780  1.1.2.2  skrll 	 */
   4781  1.1.2.2  skrll  	ca->sndrcv.sinfo_flags &= ~MSG_SENDALL;
   4782  1.1.2.2  skrll 
   4783  1.1.2.2  skrll 	/* get length and mbuf chain */
   4784  1.1.2.2  skrll 	if (uio) {
   4785  1.1.2.2  skrll 		ca->sndlen = uio->uio_resid;
   4786  1.1.2.2  skrll 		ca->m = sctp_copy_out_all(uio, ca->sndlen);
   4787  1.1.2.2  skrll 		if (ca->m == NULL) {
   4788  1.1.2.2  skrll 			free(ca, M_PCB);
   4789  1.1.2.2  skrll 			return (ENOMEM);
   4790  1.1.2.2  skrll 		}
   4791  1.1.2.2  skrll 	} else {
   4792  1.1.2.2  skrll 		if ((m->m_flags & M_PKTHDR) == 0) {
   4793  1.1.2.2  skrll 			ca->sndlen = 0;
   4794  1.1.2.2  skrll 			while(m) {
   4795  1.1.2.2  skrll 				ca->sndlen += m->m_len;
   4796  1.1.2.2  skrll 				m = m->m_next;
   4797  1.1.2.2  skrll 			}
   4798  1.1.2.2  skrll 		} else {
   4799  1.1.2.2  skrll 			ca->sndlen = m->m_pkthdr.len;
   4800  1.1.2.2  skrll 		}
   4801  1.1.2.2  skrll 		ca->m = m;
   4802  1.1.2.2  skrll 	}
   4803  1.1.2.2  skrll 
   4804  1.1.2.2  skrll 	ret = sctp_initiate_iterator(sctp_sendall_iterator, SCTP_PCB_ANY_FLAGS, SCTP_ASOC_ANY_STATE,
   4805  1.1.2.2  skrll 				     (void *)ca, 0, sctp_sendall_completes, inp);
   4806  1.1.2.2  skrll 	if (ret) {
   4807  1.1.2.2  skrll #ifdef SCTP_DEBUG
   4808  1.1.2.2  skrll 		printf("Failed to initate iterator to takeover associations\n");
   4809  1.1.2.2  skrll #endif
   4810  1.1.2.2  skrll 		free(ca, M_PCB);
   4811  1.1.2.2  skrll 		return (EFAULT);
   4812  1.1.2.2  skrll 
   4813  1.1.2.2  skrll 	}
   4814  1.1.2.2  skrll 	return (0);
   4815  1.1.2.2  skrll }
   4816  1.1.2.2  skrll 
   4817  1.1.2.2  skrll 
   4818  1.1.2.2  skrll void
   4819  1.1.2.2  skrll sctp_toss_old_cookies(struct sctp_association *asoc)
   4820  1.1.2.2  skrll {
   4821  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk, *nchk;
   4822  1.1.2.2  skrll 	chk = TAILQ_FIRST(&asoc->control_send_queue);
   4823  1.1.2.2  skrll 	while (chk) {
   4824  1.1.2.2  skrll 		nchk = TAILQ_NEXT(chk, sctp_next);
   4825  1.1.2.2  skrll 		if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
   4826  1.1.2.2  skrll 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
   4827  1.1.2.2  skrll 			if (chk->data) {
   4828  1.1.2.2  skrll 				sctp_m_freem(chk->data);
   4829  1.1.2.2  skrll 				chk->data = NULL;
   4830  1.1.2.2  skrll 			}
   4831  1.1.2.2  skrll 			asoc->ctrl_queue_cnt--;
   4832  1.1.2.2  skrll 			if (chk->whoTo)
   4833  1.1.2.2  skrll 				sctp_free_remote_addr(chk->whoTo);
   4834  1.1.2.2  skrll 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   4835  1.1.2.2  skrll 			sctppcbinfo.ipi_count_chunk--;
   4836  1.1.2.2  skrll 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   4837  1.1.2.2  skrll 				panic("Chunk count is negative");
   4838  1.1.2.2  skrll 			}
   4839  1.1.2.2  skrll 			sctppcbinfo.ipi_gencnt_chunk++;
   4840  1.1.2.2  skrll 		}
   4841  1.1.2.2  skrll 		chk = nchk;
   4842  1.1.2.2  skrll 	}
   4843  1.1.2.2  skrll }
   4844  1.1.2.2  skrll 
   4845  1.1.2.2  skrll void
   4846  1.1.2.2  skrll sctp_toss_old_asconf(struct sctp_tcb *stcb)
   4847  1.1.2.2  skrll {
   4848  1.1.2.2  skrll 	struct sctp_association *asoc;
   4849  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk, *chk_tmp;
   4850  1.1.2.2  skrll 
   4851  1.1.2.2  skrll 	asoc = &stcb->asoc;
   4852  1.1.2.2  skrll 	for (chk = TAILQ_FIRST(&asoc->control_send_queue); chk != NULL;
   4853  1.1.2.2  skrll 	     chk = chk_tmp) {
   4854  1.1.2.2  skrll 		/* get next chk */
   4855  1.1.2.2  skrll 		chk_tmp = TAILQ_NEXT(chk, sctp_next);
   4856  1.1.2.2  skrll 		/* find SCTP_ASCONF chunk in queue (only one ever in queue) */
   4857  1.1.2.2  skrll 		if (chk->rec.chunk_id == SCTP_ASCONF) {
   4858  1.1.2.2  skrll 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
   4859  1.1.2.2  skrll 			if (chk->data) {
   4860  1.1.2.2  skrll 				sctp_m_freem(chk->data);
   4861  1.1.2.2  skrll 				chk->data = NULL;
   4862  1.1.2.2  skrll 			}
   4863  1.1.2.2  skrll 			asoc->ctrl_queue_cnt--;
   4864  1.1.2.2  skrll 			if (chk->whoTo)
   4865  1.1.2.2  skrll 				sctp_free_remote_addr(chk->whoTo);
   4866  1.1.2.2  skrll 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   4867  1.1.2.2  skrll 			sctppcbinfo.ipi_count_chunk--;
   4868  1.1.2.2  skrll 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   4869  1.1.2.2  skrll 				panic("Chunk count is negative");
   4870  1.1.2.2  skrll 			}
   4871  1.1.2.2  skrll 			sctppcbinfo.ipi_gencnt_chunk++;
   4872  1.1.2.2  skrll 		}
   4873  1.1.2.2  skrll 	}
   4874  1.1.2.2  skrll }
   4875  1.1.2.2  skrll 
   4876  1.1.2.2  skrll 
   4877  1.1.2.2  skrll static void
   4878  1.1.2.2  skrll sctp_clean_up_datalist(struct sctp_tcb *stcb,
   4879  1.1.2.2  skrll 		       struct sctp_association *asoc,
   4880  1.1.2.2  skrll 		       struct sctp_tmit_chunk **data_list,
   4881  1.1.2.2  skrll 		       int bundle_at,
   4882  1.1.2.2  skrll 		       struct sctp_nets *net)
   4883  1.1.2.2  skrll {
   4884  1.1.2.2  skrll 	int i;
   4885  1.1.2.2  skrll 	for (i = 0; i < bundle_at; i++) {
   4886  1.1.2.2  skrll 		/* off of the send queue */
   4887  1.1.2.2  skrll 		if (i) {
   4888  1.1.2.2  skrll 			/* Any chunk NOT 0 you zap the time
   4889  1.1.2.2  skrll 			 * chunk 0 gets zapped or set based on
   4890  1.1.2.2  skrll 			 * if a RTO measurment is needed.
   4891  1.1.2.2  skrll 			 */
   4892  1.1.2.2  skrll 			data_list[i]->do_rtt = 0;
   4893  1.1.2.2  skrll 		}
   4894  1.1.2.2  skrll 		/* record time */
   4895  1.1.2.2  skrll 		data_list[i]->sent_rcv_time = net->last_sent_time;
   4896  1.1.2.2  skrll 		TAILQ_REMOVE(&asoc->send_queue,
   4897  1.1.2.2  skrll 			     data_list[i],
   4898  1.1.2.2  skrll 			     sctp_next);
   4899  1.1.2.2  skrll 		/* on to the sent queue */
   4900  1.1.2.2  skrll 		TAILQ_INSERT_TAIL(&asoc->sent_queue,
   4901  1.1.2.2  skrll 				  data_list[i],
   4902  1.1.2.2  skrll 				  sctp_next);
   4903  1.1.2.2  skrll 		/* This does not lower until the cum-ack passes it */
   4904  1.1.2.2  skrll 		asoc->sent_queue_cnt++;
   4905  1.1.2.2  skrll 		asoc->send_queue_cnt--;
   4906  1.1.2.2  skrll 		if ((asoc->peers_rwnd <= 0) &&
   4907  1.1.2.2  skrll 		    (asoc->total_flight == 0) &&
   4908  1.1.2.2  skrll 		    (bundle_at == 1)) {
   4909  1.1.2.2  skrll 			/* Mark the chunk as being a window probe */
   4910  1.1.2.2  skrll #ifdef SCTP_DEBUG
   4911  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   4912  1.1.2.2  skrll 				printf("WINDOW PROBE SET\n");
   4913  1.1.2.2  skrll 			}
   4914  1.1.2.2  skrll #endif
   4915  1.1.2.2  skrll 			sctp_pegs[SCTP_WINDOW_PROBES]++;
   4916  1.1.2.2  skrll 			data_list[i]->rec.data.state_flags |= SCTP_WINDOW_PROBE;
   4917  1.1.2.2  skrll 		} else {
   4918  1.1.2.2  skrll 			data_list[i]->rec.data.state_flags &= ~SCTP_WINDOW_PROBE;
   4919  1.1.2.2  skrll 		}
   4920  1.1.2.2  skrll #ifdef SCTP_AUDITING_ENABLED
   4921  1.1.2.2  skrll 		sctp_audit_log(0xC2, 3);
   4922  1.1.2.2  skrll #endif
   4923  1.1.2.2  skrll 		data_list[i]->sent = SCTP_DATAGRAM_SENT;
   4924  1.1.2.2  skrll 		data_list[i]->snd_count = 1;
   4925  1.1.2.2  skrll 		net->flight_size += data_list[i]->book_size;
   4926  1.1.2.2  skrll 		asoc->total_flight += data_list[i]->book_size;
   4927  1.1.2.2  skrll 		asoc->total_flight_count++;
   4928  1.1.2.2  skrll #ifdef SCTP_LOG_RWND
   4929  1.1.2.2  skrll 		sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
   4930  1.1.2.2  skrll 			      asoc->peers_rwnd , data_list[i]->send_size, sctp_peer_chunk_oh);
   4931  1.1.2.2  skrll #endif
   4932  1.1.2.2  skrll 		asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
   4933  1.1.2.2  skrll 						    (u_int32_t)(data_list[i]->send_size + sctp_peer_chunk_oh));
   4934  1.1.2.2  skrll 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
   4935  1.1.2.2  skrll 			/* SWS sender side engages */
   4936  1.1.2.2  skrll 			asoc->peers_rwnd = 0;
   4937  1.1.2.2  skrll 		}
   4938  1.1.2.2  skrll 	}
   4939  1.1.2.2  skrll }
   4940  1.1.2.2  skrll 
   4941  1.1.2.2  skrll static void
   4942  1.1.2.2  skrll sctp_clean_up_ctl(struct sctp_association *asoc)
   4943  1.1.2.2  skrll {
   4944  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk, *nchk;
   4945  1.1.2.2  skrll 	for (chk = TAILQ_FIRST(&asoc->control_send_queue);
   4946  1.1.2.2  skrll 	    chk; chk = nchk) {
   4947  1.1.2.2  skrll 		nchk = TAILQ_NEXT(chk, sctp_next);
   4948  1.1.2.2  skrll 		if ((chk->rec.chunk_id == SCTP_SELECTIVE_ACK) ||
   4949  1.1.2.2  skrll 		    (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST) ||
   4950  1.1.2.2  skrll 		    (chk->rec.chunk_id == SCTP_HEARTBEAT_ACK) ||
   4951  1.1.2.2  skrll 		    (chk->rec.chunk_id == SCTP_SHUTDOWN) ||
   4952  1.1.2.2  skrll 		    (chk->rec.chunk_id == SCTP_SHUTDOWN_ACK) ||
   4953  1.1.2.2  skrll 		    (chk->rec.chunk_id == SCTP_OPERATION_ERROR) ||
   4954  1.1.2.2  skrll 		    (chk->rec.chunk_id == SCTP_PACKET_DROPPED) ||
   4955  1.1.2.2  skrll 		    (chk->rec.chunk_id == SCTP_COOKIE_ACK) ||
   4956  1.1.2.2  skrll 		    (chk->rec.chunk_id == SCTP_ECN_CWR) ||
   4957  1.1.2.2  skrll 		    (chk->rec.chunk_id == SCTP_ASCONF_ACK)) {
   4958  1.1.2.2  skrll 			/* Stray chunks must be cleaned up */
   4959  1.1.2.2  skrll 		clean_up_anyway:
   4960  1.1.2.2  skrll 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
   4961  1.1.2.2  skrll 			if (chk->data) {
   4962  1.1.2.2  skrll 				sctp_m_freem(chk->data);
   4963  1.1.2.2  skrll 				chk->data = NULL;
   4964  1.1.2.2  skrll 			}
   4965  1.1.2.2  skrll 			asoc->ctrl_queue_cnt--;
   4966  1.1.2.2  skrll 			sctp_free_remote_addr(chk->whoTo);
   4967  1.1.2.2  skrll 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   4968  1.1.2.2  skrll 			sctppcbinfo.ipi_count_chunk--;
   4969  1.1.2.2  skrll 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   4970  1.1.2.2  skrll 				panic("Chunk count is negative");
   4971  1.1.2.2  skrll 			}
   4972  1.1.2.2  skrll 			sctppcbinfo.ipi_gencnt_chunk++;
   4973  1.1.2.2  skrll 		} else if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
   4974  1.1.2.2  skrll 			struct sctp_stream_reset_req *strreq;
   4975  1.1.2.2  skrll 			/* special handling, we must look into the param */
   4976  1.1.2.2  skrll 			strreq = mtod(chk->data, struct sctp_stream_reset_req *);
   4977  1.1.2.2  skrll 			if (strreq->sr_req.ph.param_type == ntohs(SCTP_STR_RESET_RESPONSE)) {
   4978  1.1.2.2  skrll 				goto clean_up_anyway;
   4979  1.1.2.2  skrll 			}
   4980  1.1.2.2  skrll 		}
   4981  1.1.2.2  skrll 	}
   4982  1.1.2.2  skrll }
   4983  1.1.2.2  skrll 
   4984  1.1.2.2  skrll static int
   4985  1.1.2.2  skrll sctp_move_to_outqueue(struct sctp_tcb *stcb,
   4986  1.1.2.2  skrll 		      struct sctp_stream_out *strq)
   4987  1.1.2.2  skrll {
   4988  1.1.2.2  skrll 	/* Move from the stream to the send_queue keeping track of the total */
   4989  1.1.2.2  skrll 	struct sctp_association *asoc;
   4990  1.1.2.2  skrll 	int tot_moved = 0;
   4991  1.1.2.2  skrll 	int failed = 0;
   4992  1.1.2.2  skrll 	int padval;
   4993  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk, *nchk;
   4994  1.1.2.2  skrll 	struct sctp_data_chunk *dchkh;
   4995  1.1.2.2  skrll 	struct sctpchunk_listhead tmp;
   4996  1.1.2.2  skrll 	struct mbuf *orig;
   4997  1.1.2.2  skrll 
   4998  1.1.2.2  skrll 	asoc = &stcb->asoc;
   4999  1.1.2.2  skrll 	TAILQ_INIT(&tmp);
   5000  1.1.2.2  skrll 	chk = TAILQ_FIRST(&strq->outqueue);
   5001  1.1.2.2  skrll 	while (chk) {
   5002  1.1.2.2  skrll 		nchk = TAILQ_NEXT(chk, sctp_next);
   5003  1.1.2.2  skrll 		/* now put in the chunk header */
   5004  1.1.2.2  skrll 		orig = chk->data;
   5005  1.1.2.2  skrll 		M_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
   5006  1.1.2.2  skrll 		if (chk->data == NULL) {
   5007  1.1.2.2  skrll 			/* HELP */
   5008  1.1.2.2  skrll 			failed++;
   5009  1.1.2.2  skrll 			break;
   5010  1.1.2.2  skrll 		}
   5011  1.1.2.2  skrll 		if (orig != chk->data) {
   5012  1.1.2.2  skrll 			/* A new mbuf was added, account for it */
   5013  1.1.2.2  skrll 			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
   5014  1.1.2.2  skrll 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
   5015  1.1.2.2  skrll 				stcb->sctp_socket->so_snd.sb_mbcnt += MSIZE;
   5016  1.1.2.2  skrll 			}
   5017  1.1.2.2  skrll #ifdef SCTP_MBCNT_LOGGING
   5018  1.1.2.2  skrll 			sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
   5019  1.1.2.2  skrll 				       asoc->total_output_queue_size,
   5020  1.1.2.2  skrll 				       0,
   5021  1.1.2.2  skrll 				       asoc->total_output_mbuf_queue_size,
   5022  1.1.2.2  skrll 				       MSIZE);
   5023  1.1.2.2  skrll #endif
   5024  1.1.2.2  skrll 			stcb->asoc.total_output_mbuf_queue_size += MSIZE;
   5025  1.1.2.2  skrll 			chk->mbcnt += MSIZE;
   5026  1.1.2.2  skrll 		}
   5027  1.1.2.2  skrll 		chk->send_size += sizeof(struct sctp_data_chunk);
   5028  1.1.2.2  skrll 		/* This should NOT have to do anything, but
   5029  1.1.2.2  skrll 		 * I would rather be cautious
   5030  1.1.2.2  skrll 		 */
   5031  1.1.2.2  skrll 		if (!failed && ((size_t)chk->data->m_len < sizeof(struct sctp_data_chunk))) {
   5032  1.1.2.2  skrll 			m_pullup(chk->data, sizeof(struct sctp_data_chunk));
   5033  1.1.2.2  skrll 			if (chk->data == NULL) {
   5034  1.1.2.2  skrll 				failed++;
   5035  1.1.2.2  skrll 				break;
   5036  1.1.2.2  skrll 			}
   5037  1.1.2.2  skrll 		}
   5038  1.1.2.2  skrll 		dchkh = mtod(chk->data, struct sctp_data_chunk *);
   5039  1.1.2.2  skrll 		dchkh->ch.chunk_length = htons(chk->send_size);
   5040  1.1.2.2  skrll 		/* Chunks must be padded to even word boundary */
   5041  1.1.2.2  skrll 		padval = chk->send_size % 4;
   5042  1.1.2.2  skrll 		if (padval) {
   5043  1.1.2.2  skrll 			/* For fragmented messages this should not
   5044  1.1.2.2  skrll 			 * run except possibly on the last chunk
   5045  1.1.2.2  skrll 			 */
   5046  1.1.2.2  skrll 			if (sctp_pad_lastmbuf(chk->data, (4 - padval))) {
   5047  1.1.2.2  skrll 				/* we are in big big trouble no mbufs :< */
   5048  1.1.2.2  skrll 				failed++;
   5049  1.1.2.2  skrll 				break;
   5050  1.1.2.2  skrll 			}
   5051  1.1.2.2  skrll 			chk->send_size += (4 - padval);
   5052  1.1.2.2  skrll 		}
   5053  1.1.2.2  skrll 		/* pull from stream queue */
   5054  1.1.2.2  skrll 		TAILQ_REMOVE(&strq->outqueue, chk, sctp_next);
   5055  1.1.2.2  skrll 		asoc->stream_queue_cnt--;
   5056  1.1.2.2  skrll 		TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
   5057  1.1.2.2  skrll 		/* add it in to the size of moved chunks */
   5058  1.1.2.2  skrll 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
   5059  1.1.2.2  skrll 			/* we pull only one message */
   5060  1.1.2.2  skrll 			break;
   5061  1.1.2.2  skrll 		}
   5062  1.1.2.2  skrll 		chk = nchk;
   5063  1.1.2.2  skrll 	}
   5064  1.1.2.2  skrll 	if (failed) {
   5065  1.1.2.2  skrll 		/* Gak, we just lost the user message */
   5066  1.1.2.2  skrll 		chk = TAILQ_FIRST(&tmp);
   5067  1.1.2.2  skrll 		while (chk) {
   5068  1.1.2.2  skrll 			nchk = TAILQ_NEXT(chk, sctp_next);
   5069  1.1.2.2  skrll 			TAILQ_REMOVE(&tmp, chk, sctp_next);
   5070  1.1.2.2  skrll 
   5071  1.1.2.2  skrll 			sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
   5072  1.1.2.2  skrll 					(SCTP_NOTIFY_DATAGRAM_UNSENT|SCTP_INTERNAL_ERROR),
   5073  1.1.2.2  skrll 					chk);
   5074  1.1.2.2  skrll 
   5075  1.1.2.2  skrll 			if (chk->data) {
   5076  1.1.2.2  skrll 				sctp_m_freem(chk->data);
   5077  1.1.2.2  skrll 				chk->data = NULL;
   5078  1.1.2.2  skrll 			}
   5079  1.1.2.2  skrll 			if (chk->whoTo) {
   5080  1.1.2.2  skrll 				sctp_free_remote_addr(chk->whoTo);
   5081  1.1.2.2  skrll 				chk->whoTo = NULL;
   5082  1.1.2.2  skrll 			}
   5083  1.1.2.2  skrll 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   5084  1.1.2.2  skrll 			sctppcbinfo.ipi_count_chunk--;
   5085  1.1.2.2  skrll 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   5086  1.1.2.2  skrll 				panic("Chunk count is negative");
   5087  1.1.2.2  skrll 			}
   5088  1.1.2.2  skrll 			sctppcbinfo.ipi_gencnt_chunk++;
   5089  1.1.2.2  skrll 			chk = nchk;
   5090  1.1.2.2  skrll 		}
   5091  1.1.2.2  skrll 		return (0);
   5092  1.1.2.2  skrll 	}
   5093  1.1.2.2  skrll 	/* now pull them off of temp wheel */
   5094  1.1.2.2  skrll 	chk = TAILQ_FIRST(&tmp);
   5095  1.1.2.2  skrll 	while (chk) {
   5096  1.1.2.2  skrll 		nchk = TAILQ_NEXT(chk, sctp_next);
   5097  1.1.2.2  skrll 		/* insert on send_queue */
   5098  1.1.2.2  skrll 		TAILQ_REMOVE(&tmp, chk, sctp_next);
   5099  1.1.2.2  skrll 		TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
   5100  1.1.2.2  skrll 		asoc->send_queue_cnt++;
   5101  1.1.2.2  skrll 		/* assign TSN */
   5102  1.1.2.2  skrll 		chk->rec.data.TSN_seq = asoc->sending_seq++;
   5103  1.1.2.2  skrll 
   5104  1.1.2.2  skrll 		dchkh = mtod(chk->data, struct sctp_data_chunk *);
   5105  1.1.2.2  skrll 		/* Put the rest of the things in place now. Size
   5106  1.1.2.2  skrll 		 * was done earlier in previous loop prior to
   5107  1.1.2.2  skrll 		 * padding.
   5108  1.1.2.2  skrll 		 */
   5109  1.1.2.2  skrll 		dchkh->ch.chunk_type = SCTP_DATA;
   5110  1.1.2.2  skrll 		dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
   5111  1.1.2.2  skrll 		dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
   5112  1.1.2.2  skrll 		dchkh->dp.stream_id = htons(strq->stream_no);
   5113  1.1.2.2  skrll 		dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
   5114  1.1.2.2  skrll 		dchkh->dp.protocol_id = chk->rec.data.payloadtype;
   5115  1.1.2.2  skrll 		/* total count moved */
   5116  1.1.2.2  skrll 		tot_moved += chk->send_size;
   5117  1.1.2.2  skrll 		chk = nchk;
   5118  1.1.2.2  skrll 	}
   5119  1.1.2.2  skrll 	return (tot_moved);
   5120  1.1.2.2  skrll }
   5121  1.1.2.2  skrll 
   5122  1.1.2.2  skrll static void
   5123  1.1.2.2  skrll sctp_fill_outqueue(struct sctp_tcb *stcb,
   5124  1.1.2.2  skrll 		   struct sctp_nets *net)
   5125  1.1.2.2  skrll {
   5126  1.1.2.2  skrll 	struct sctp_association *asoc;
   5127  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   5128  1.1.2.2  skrll 	struct sctp_stream_out *strq, *strqn;
   5129  1.1.2.2  skrll 	int mtu_fromwheel, goal_mtu;
   5130  1.1.2.2  skrll 	unsigned int moved, seenend, cnt_mvd=0;
   5131  1.1.2.2  skrll 
   5132  1.1.2.2  skrll 	asoc = &stcb->asoc;
   5133  1.1.2.2  skrll 	/* Attempt to move at least 1 MTU's worth
   5134  1.1.2.2  skrll 	 * onto the wheel for each destination address
   5135  1.1.2.2  skrll 	 */
   5136  1.1.2.2  skrll 	goal_mtu = net->cwnd - net->flight_size;
   5137  1.1.2.2  skrll 	if ((unsigned int)goal_mtu < net->mtu) {
   5138  1.1.2.2  skrll 		goal_mtu = net->mtu;
   5139  1.1.2.2  skrll 	}
   5140  1.1.2.2  skrll 	if (sctp_pegs[SCTP_MOVED_MTU] < (unsigned int)goal_mtu) {
   5141  1.1.2.2  skrll 		sctp_pegs[SCTP_MOVED_MTU] = goal_mtu;
   5142  1.1.2.2  skrll 	}
   5143  1.1.2.2  skrll 	seenend = moved = mtu_fromwheel = 0;
   5144  1.1.2.2  skrll 	if (asoc->last_out_stream == NULL) {
   5145  1.1.2.2  skrll 		strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
   5146  1.1.2.2  skrll 		if (asoc->last_out_stream == NULL) {
   5147  1.1.2.2  skrll 			/* huh nothing on the wheel, TSNH */
   5148  1.1.2.2  skrll 			return;
   5149  1.1.2.2  skrll 		}
   5150  1.1.2.2  skrll 		goto done_it;
   5151  1.1.2.2  skrll 	}
   5152  1.1.2.2  skrll 	strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
   5153  1.1.2.2  skrll  done_it:
   5154  1.1.2.2  skrll 	if (strq == NULL) {
   5155  1.1.2.2  skrll 		asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
   5156  1.1.2.2  skrll 	}
   5157  1.1.2.2  skrll 	while (mtu_fromwheel < goal_mtu) {
   5158  1.1.2.2  skrll 		if (strq == NULL) {
   5159  1.1.2.2  skrll 			if (seenend == 0) {
   5160  1.1.2.2  skrll 				seenend = 1;
   5161  1.1.2.2  skrll 				strq = TAILQ_FIRST(&asoc->out_wheel);
   5162  1.1.2.2  skrll 			} else if ((moved == 0) && (seenend)) {
   5163  1.1.2.2  skrll 				/* none left on the wheel */
   5164  1.1.2.2  skrll 				sctp_pegs[SCTP_MOVED_NLEF]++;
   5165  1.1.2.2  skrll 				return;
   5166  1.1.2.2  skrll 			} else if (moved) {
   5167  1.1.2.2  skrll 				/*
   5168  1.1.2.2  skrll 				 * clear the flags and rotate back through
   5169  1.1.2.2  skrll 				 * again
   5170  1.1.2.2  skrll 				 */
   5171  1.1.2.2  skrll 				moved = 0;
   5172  1.1.2.2  skrll 				seenend = 0;
   5173  1.1.2.2  skrll 				strq = TAILQ_FIRST(&asoc->out_wheel);
   5174  1.1.2.2  skrll 			}
   5175  1.1.2.2  skrll 			if (strq == NULL)
   5176  1.1.2.2  skrll 				break;
   5177  1.1.2.2  skrll 			continue;
   5178  1.1.2.2  skrll 		}
   5179  1.1.2.2  skrll 		strqn = TAILQ_NEXT(strq, next_spoke);
   5180  1.1.2.2  skrll 		if ((chk = TAILQ_FIRST(&strq->outqueue)) == NULL) {
   5181  1.1.2.2  skrll 			/* none left on this queue, prune a spoke?  */
   5182  1.1.2.2  skrll 			sctp_remove_from_wheel(asoc, strq);
   5183  1.1.2.2  skrll 			if (strq == asoc->last_out_stream) {
   5184  1.1.2.2  skrll 			    /* the last one we used went off the wheel */
   5185  1.1.2.2  skrll 			    asoc->last_out_stream = NULL;
   5186  1.1.2.2  skrll 			}
   5187  1.1.2.2  skrll 			strq = strqn;
   5188  1.1.2.2  skrll 			continue;
   5189  1.1.2.2  skrll 		}
   5190  1.1.2.2  skrll 		if (chk->whoTo != net) {
   5191  1.1.2.2  skrll 			/* Skip this stream, first one on stream
   5192  1.1.2.2  skrll 			 * does not head to our current destination.
   5193  1.1.2.2  skrll 			 */
   5194  1.1.2.2  skrll 			strq = strqn;
   5195  1.1.2.2  skrll 			continue;
   5196  1.1.2.2  skrll 		}
   5197  1.1.2.2  skrll 		mtu_fromwheel += sctp_move_to_outqueue(stcb, strq);
   5198  1.1.2.2  skrll 		cnt_mvd++;
   5199  1.1.2.2  skrll 		moved++;
   5200  1.1.2.2  skrll 		asoc->last_out_stream = strq;
   5201  1.1.2.2  skrll 		strq = strqn;
   5202  1.1.2.2  skrll 	}
   5203  1.1.2.2  skrll 	sctp_pegs[SCTP_MOVED_MAX]++;
   5204  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5205  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5206  1.1.2.2  skrll 		printf("Ok we moved %d chunks to send queue\n",
   5207  1.1.2.2  skrll 		       moved);
   5208  1.1.2.2  skrll 	}
   5209  1.1.2.2  skrll #endif
   5210  1.1.2.2  skrll 	if (sctp_pegs[SCTP_MOVED_QMAX] < cnt_mvd) {
   5211  1.1.2.2  skrll 		sctp_pegs[SCTP_MOVED_QMAX] = cnt_mvd;
   5212  1.1.2.2  skrll 	}
   5213  1.1.2.2  skrll }
   5214  1.1.2.2  skrll 
   5215  1.1.2.2  skrll void
   5216  1.1.2.2  skrll sctp_fix_ecn_echo(struct sctp_association *asoc)
   5217  1.1.2.2  skrll {
   5218  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   5219  1.1.2.2  skrll 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
   5220  1.1.2.2  skrll 		if (chk->rec.chunk_id == SCTP_ECN_ECHO) {
   5221  1.1.2.2  skrll 			chk->sent = SCTP_DATAGRAM_UNSENT;
   5222  1.1.2.2  skrll 		}
   5223  1.1.2.2  skrll 	}
   5224  1.1.2.2  skrll }
   5225  1.1.2.2  skrll 
   5226  1.1.2.2  skrll static void
   5227  1.1.2.2  skrll sctp_move_to_an_alt(struct sctp_tcb *stcb,
   5228  1.1.2.2  skrll 		    struct sctp_association *asoc,
   5229  1.1.2.2  skrll 		    struct sctp_nets *net)
   5230  1.1.2.2  skrll {
   5231  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   5232  1.1.2.2  skrll 	struct sctp_nets *a_net;
   5233  1.1.2.2  skrll 	a_net = sctp_find_alternate_net(stcb, net);
   5234  1.1.2.2  skrll 	if ((a_net != net) &&
   5235  1.1.2.2  skrll 	    ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
   5236  1.1.2.2  skrll 		/*
   5237  1.1.2.2  skrll 		 * We only proceed if a valid alternate is found that is
   5238  1.1.2.2  skrll 		 * not this one and is reachable. Here we must move all
   5239  1.1.2.2  skrll 		 * chunks queued in the send queue off of the destination
   5240  1.1.2.2  skrll 		 * address to our alternate.
   5241  1.1.2.2  skrll 		 */
   5242  1.1.2.2  skrll 		TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
   5243  1.1.2.2  skrll 			if (chk->whoTo == net) {
   5244  1.1.2.2  skrll 				/* Move the chunk to our alternate */
   5245  1.1.2.2  skrll 				sctp_free_remote_addr(chk->whoTo);
   5246  1.1.2.2  skrll 				chk->whoTo = a_net;
   5247  1.1.2.2  skrll 				a_net->ref_count++;
   5248  1.1.2.2  skrll 			}
   5249  1.1.2.2  skrll 		}
   5250  1.1.2.2  skrll 	}
   5251  1.1.2.2  skrll }
   5252  1.1.2.2  skrll 
   5253  1.1.2.2  skrll static int sctp_from_user_send=0;
   5254  1.1.2.2  skrll 
   5255  1.1.2.2  skrll static int
   5256  1.1.2.2  skrll sctp_med_chunk_output(struct sctp_inpcb *inp,
   5257  1.1.2.2  skrll 		      struct sctp_tcb *stcb,
   5258  1.1.2.2  skrll 		      struct sctp_association *asoc,
   5259  1.1.2.2  skrll 		      int *num_out,
   5260  1.1.2.2  skrll 		      int *reason_code,
   5261  1.1.2.2  skrll 		      int control_only, int *cwnd_full, int from_where,
   5262  1.1.2.2  skrll 		      struct timeval *now, int *now_filled)
   5263  1.1.2.2  skrll {
   5264  1.1.2.2  skrll 	/*
   5265  1.1.2.2  skrll 	 * Ok this is the generic chunk service queue.
   5266  1.1.2.2  skrll 	 * we must do the following:
   5267  1.1.2.2  skrll 	 *  - Service the stream queue that is next, moving any message
   5268  1.1.2.2  skrll 	 *    (note I must get a complete message i.e. FIRST/MIDDLE and
   5269  1.1.2.2  skrll 	 *    LAST to the out queue in one pass) and assigning TSN's
   5270  1.1.2.2  skrll 	 *  - Check to see if the cwnd/rwnd allows any output, if so we
   5271  1.1.2.2  skrll 	 *    go ahead and fomulate and send the low level chunks. Making
   5272  1.1.2.2  skrll 	 *    sure to combine any control in the control chunk queue also.
   5273  1.1.2.2  skrll 	 */
   5274  1.1.2.2  skrll 	struct sctp_nets *net;
   5275  1.1.2.2  skrll 	struct mbuf *outchain;
   5276  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk, *nchk;
   5277  1.1.2.2  skrll 	struct sctphdr *shdr;
   5278  1.1.2.2  skrll 	/* temp arrays for unlinking */
   5279  1.1.2.2  skrll 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
   5280  1.1.2.2  skrll 	int no_fragmentflg, error;
   5281  1.1.2.2  skrll 	int one_chunk, hbflag;
   5282  1.1.2.2  skrll 	int asconf, cookie, no_out_cnt;
   5283  1.1.2.2  skrll 	int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind;
   5284  1.1.2.2  skrll         unsigned int mtu, r_mtu, omtu;
   5285  1.1.2.2  skrll 	*num_out = 0;
   5286  1.1.2.2  skrll 	cwnd_full_ind = 0;
   5287  1.1.2.2  skrll 	ctl_cnt = no_out_cnt = asconf = cookie = 0;
   5288  1.1.2.2  skrll 	/*
   5289  1.1.2.2  skrll 	 * First lets prime the pump. For each destination, if there
   5290  1.1.2.2  skrll 	 * is room in the flight size, attempt to pull an MTU's worth
   5291  1.1.2.2  skrll 	 * out of the stream queues into the general send_queue
   5292  1.1.2.2  skrll 	 */
   5293  1.1.2.2  skrll #ifdef SCTP_AUDITING_ENABLED
   5294  1.1.2.2  skrll 	sctp_audit_log(0xC2, 2);
   5295  1.1.2.2  skrll #endif
   5296  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5297  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5298  1.1.2.2  skrll 		printf("***********************\n");
   5299  1.1.2.2  skrll 	}
   5300  1.1.2.2  skrll #endif
   5301  1.1.2.2  skrll 	hbflag = 0;
   5302  1.1.2.2  skrll 	if (control_only)
   5303  1.1.2.2  skrll 		no_data_chunks = 1;
   5304  1.1.2.2  skrll 	else
   5305  1.1.2.2  skrll 		no_data_chunks = 0;
   5306  1.1.2.2  skrll 
   5307  1.1.2.2  skrll 	/* Nothing to possible to send? */
   5308  1.1.2.2  skrll 	if (TAILQ_EMPTY(&asoc->control_send_queue) &&
   5309  1.1.2.2  skrll 	    TAILQ_EMPTY(&asoc->send_queue) &&
   5310  1.1.2.2  skrll 	    TAILQ_EMPTY(&asoc->out_wheel)) {
   5311  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5312  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5313  1.1.2.2  skrll 			printf("All wheels empty\n");
   5314  1.1.2.2  skrll 		}
   5315  1.1.2.2  skrll #endif
   5316  1.1.2.2  skrll 		return (0);
   5317  1.1.2.2  skrll 	}
   5318  1.1.2.2  skrll 	if (asoc->peers_rwnd <= 0) {
   5319  1.1.2.2  skrll 		/* No room in peers rwnd */
   5320  1.1.2.2  skrll 		*cwnd_full = 1;
   5321  1.1.2.2  skrll 		*reason_code = 1;
   5322  1.1.2.2  skrll 		if (asoc->total_flight > 0) {
   5323  1.1.2.2  skrll 			/* we are allowed one chunk in flight */
   5324  1.1.2.2  skrll 			no_data_chunks = 1;
   5325  1.1.2.2  skrll 			sctp_pegs[SCTP_RWND_BLOCKED]++;
   5326  1.1.2.2  skrll 		}
   5327  1.1.2.2  skrll 	}
   5328  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5329  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5330  1.1.2.2  skrll 		printf("Ok we have done the fillup no_data_chunk=%d tf=%d prw:%d\n",
   5331  1.1.2.2  skrll 		       (int)no_data_chunks,
   5332  1.1.2.2  skrll 		       (int)asoc->total_flight, (int)asoc->peers_rwnd);
   5333  1.1.2.2  skrll 	}
   5334  1.1.2.2  skrll #endif
   5335  1.1.2.2  skrll 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   5336  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5337  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5338  1.1.2.2  skrll 			printf("net:%p fs:%d  cwnd:%d\n",
   5339  1.1.2.2  skrll 			       net, net->flight_size, net->cwnd);
   5340  1.1.2.2  skrll 		}
   5341  1.1.2.2  skrll #endif
   5342  1.1.2.2  skrll 		if (net->flight_size >= net->cwnd) {
   5343  1.1.2.2  skrll 			/* skip this network, no room */
   5344  1.1.2.2  skrll 			cwnd_full_ind++;
   5345  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5346  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5347  1.1.2.2  skrll 				printf("Ok skip fillup->fs:%d > cwnd:%d\n",
   5348  1.1.2.2  skrll 				       net->flight_size,
   5349  1.1.2.2  skrll 				       net->cwnd);
   5350  1.1.2.2  skrll 			}
   5351  1.1.2.2  skrll #endif
   5352  1.1.2.2  skrll 			sctp_pegs[SCTP_CWND_NOFILL]++;
   5353  1.1.2.2  skrll 			continue;
   5354  1.1.2.2  skrll 		}
   5355  1.1.2.2  skrll 		/*
   5356  1.1.2.2  skrll 		 * spin through the stream queues moving one message and
   5357  1.1.2.2  skrll 		 * assign TSN's as appropriate.
   5358  1.1.2.2  skrll 		 */
   5359  1.1.2.2  skrll 		sctp_fill_outqueue(stcb, net);
   5360  1.1.2.2  skrll 	}
   5361  1.1.2.2  skrll 	*cwnd_full = cwnd_full_ind;
   5362  1.1.2.2  skrll 	/* now service each destination and send out what we can for it */
   5363  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5364  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5365  1.1.2.2  skrll 		int chk_cnt = 0;
   5366  1.1.2.2  skrll 		TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
   5367  1.1.2.2  skrll 			chk_cnt++;
   5368  1.1.2.2  skrll 		}
   5369  1.1.2.2  skrll 		printf("We have %d chunks on the send_queue\n", chk_cnt);
   5370  1.1.2.2  skrll 		chk_cnt = 0;
   5371  1.1.2.2  skrll 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
   5372  1.1.2.2  skrll 			chk_cnt++;
   5373  1.1.2.2  skrll 		}
   5374  1.1.2.2  skrll 		printf("We have %d chunks on the sent_queue\n", chk_cnt);
   5375  1.1.2.2  skrll 		TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
   5376  1.1.2.2  skrll 			chk_cnt++;
   5377  1.1.2.2  skrll 		}
   5378  1.1.2.2  skrll 		printf("We have %d chunks on the control_queue\n", chk_cnt);
   5379  1.1.2.2  skrll 	}
   5380  1.1.2.2  skrll #endif
   5381  1.1.2.2  skrll 	/* If we have data to send, and DSACK is running, stop it
   5382  1.1.2.2  skrll 	 * and build a SACK to dump on to bundle with output. This
   5383  1.1.2.2  skrll 	 * actually MAY make it so the bundling does not occur if
   5384  1.1.2.2  skrll 	 * the SACK is big but I think this is ok because basic SACK
   5385  1.1.2.2  skrll 	 * space is pre-reserved in our fragmentation size choice.
   5386  1.1.2.2  skrll 	 */
   5387  1.1.2.2  skrll 	if ((TAILQ_FIRST(&asoc->send_queue) != NULL) &&
   5388  1.1.2.2  skrll 	    (no_data_chunks == 0)) {
   5389  1.1.2.2  skrll 		/* We will be sending something */
   5390  1.1.2.2  skrll 		if (callout_pending(&stcb->asoc.dack_timer.timer)) {
   5391  1.1.2.2  skrll 			/* Yep a callout is pending */
   5392  1.1.2.2  skrll 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
   5393  1.1.2.2  skrll 					stcb->sctp_ep,
   5394  1.1.2.2  skrll 					stcb, NULL);
   5395  1.1.2.2  skrll 			sctp_send_sack(stcb);
   5396  1.1.2.2  skrll 		}
   5397  1.1.2.2  skrll 	}
   5398  1.1.2.2  skrll 	/* Nothing to send? */
   5399  1.1.2.2  skrll 	if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
   5400  1.1.2.2  skrll 	    (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
   5401  1.1.2.2  skrll 		return (0);
   5402  1.1.2.2  skrll 	}
   5403  1.1.2.2  skrll 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   5404  1.1.2.2  skrll 		/* how much can we send? */
   5405  1.1.2.2  skrll 		if (net->ref_count < 2) {
   5406  1.1.2.2  skrll 			/* Ref-count of 1 so we cannot have data or control
   5407  1.1.2.2  skrll 			 * queued to this address. Skip it.
   5408  1.1.2.2  skrll 			 */
   5409  1.1.2.2  skrll  			continue;
   5410  1.1.2.2  skrll 		}
   5411  1.1.2.2  skrll 		ctl_cnt = bundle_at = 0;
   5412  1.1.2.2  skrll 		outchain = NULL;
   5413  1.1.2.2  skrll 		no_fragmentflg = 1;
   5414  1.1.2.2  skrll 		one_chunk = 0;
   5415  1.1.2.2  skrll 
   5416  1.1.2.2  skrll 		if (rtcache_validate(&net->ro)) {
   5417  1.1.2.2  skrll 			/* if we have a route and an ifp
   5418  1.1.2.2  skrll 			 * check to see if we have room to
   5419  1.1.2.2  skrll 			 * send to this guy
   5420  1.1.2.2  skrll 			 */
   5421  1.1.2.2  skrll 			struct ifnet *ifp;
   5422  1.1.2.2  skrll 			ifp = net->ro._ro_rt->rt_ifp;
   5423  1.1.2.2  skrll 			if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
   5424  1.1.2.2  skrll 				sctp_pegs[SCTP_IFP_QUEUE_FULL]++;
   5425  1.1.2.2  skrll #ifdef SCTP_LOG_MAXBURST
   5426  1.1.2.2  skrll 				sctp_log_maxburst(net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
   5427  1.1.2.2  skrll   #endif
   5428  1.1.2.2  skrll 				continue;
   5429  1.1.2.2  skrll 			}
   5430  1.1.2.2  skrll 		}
   5431  1.1.2.2  skrll 		if (((struct sockaddr *)&net->ro.ro_sa)->sa_family == AF_INET) {
   5432  1.1.2.2  skrll 			mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
   5433  1.1.2.2  skrll 		} else {
   5434  1.1.2.2  skrll 			mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
   5435  1.1.2.2  skrll 		}
   5436  1.1.2.2  skrll 		if (mtu > asoc->peers_rwnd) {
   5437  1.1.2.2  skrll 			if (asoc->total_flight > 0) {
   5438  1.1.2.2  skrll 				/* We have a packet in flight somewhere */
   5439  1.1.2.2  skrll 				r_mtu = asoc->peers_rwnd;
   5440  1.1.2.2  skrll 			} else {
   5441  1.1.2.2  skrll 				/* We are always allowed to send one MTU out */
   5442  1.1.2.2  skrll 				one_chunk = 1;
   5443  1.1.2.2  skrll 				r_mtu = mtu;
   5444  1.1.2.2  skrll 			}
   5445  1.1.2.2  skrll 		} else {
   5446  1.1.2.2  skrll 			r_mtu = mtu;
   5447  1.1.2.2  skrll 		}
   5448  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5449  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5450  1.1.2.2  skrll 			printf("Ok r_mtu is %d mtu is %d for this net:%p one_chunk:%d\n",
   5451  1.1.2.2  skrll 			       r_mtu, mtu, net, one_chunk);
   5452  1.1.2.2  skrll 		}
   5453  1.1.2.2  skrll #endif
   5454  1.1.2.2  skrll 		/************************/
   5455  1.1.2.2  skrll 		/* Control transmission */
   5456  1.1.2.2  skrll 		/************************/
   5457  1.1.2.2  skrll 		/* Now first lets go through the control queue */
   5458  1.1.2.2  skrll 		for (chk = TAILQ_FIRST(&asoc->control_send_queue);
   5459  1.1.2.2  skrll 		     chk; chk = nchk) {
   5460  1.1.2.2  skrll 			nchk = TAILQ_NEXT(chk, sctp_next);
   5461  1.1.2.2  skrll 			if (chk->whoTo != net) {
   5462  1.1.2.2  skrll 				/*
   5463  1.1.2.2  skrll 				 * No, not sent to the network we are
   5464  1.1.2.2  skrll 				 * looking at
   5465  1.1.2.2  skrll 				 */
   5466  1.1.2.2  skrll 				continue;
   5467  1.1.2.2  skrll 			}
   5468  1.1.2.2  skrll 			if (chk->data == NULL) {
   5469  1.1.2.2  skrll 				continue;
   5470  1.1.2.2  skrll 			}
   5471  1.1.2.2  skrll 			if ((chk->data->m_flags & M_PKTHDR) == 0) {
   5472  1.1.2.2  skrll 				/*
   5473  1.1.2.2  skrll 				 * NOTE: the chk queue MUST have the PKTHDR
   5474  1.1.2.2  skrll 				 * flag set on it with a total in the
   5475  1.1.2.2  skrll 				 * m_pkthdr.len field!! else the chunk will
   5476  1.1.2.2  skrll 				 * ALWAYS be skipped
   5477  1.1.2.2  skrll 				 */
   5478  1.1.2.2  skrll 				continue;
   5479  1.1.2.2  skrll 			}
   5480  1.1.2.2  skrll 			if (chk->sent != SCTP_DATAGRAM_UNSENT) {
   5481  1.1.2.2  skrll 				/*
   5482  1.1.2.2  skrll 				 * It must be unsent. Cookies and ASCONF's
   5483  1.1.2.2  skrll 				 * hang around but there timers will force
   5484  1.1.2.2  skrll 				 * when marked for resend.
   5485  1.1.2.2  skrll 				 */
   5486  1.1.2.2  skrll 				continue;
   5487  1.1.2.2  skrll 			}
   5488  1.1.2.2  skrll 			/* Here we do NOT factor the r_mtu */
   5489  1.1.2.2  skrll 			if ((chk->data->m_pkthdr.len < (int)mtu) ||
   5490  1.1.2.2  skrll 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
   5491  1.1.2.2  skrll 				/*
   5492  1.1.2.2  skrll 				 * We probably should glom the mbuf chain from
   5493  1.1.2.2  skrll 				 * the chk->data for control but the problem
   5494  1.1.2.2  skrll 				 * is it becomes yet one more level of
   5495  1.1.2.2  skrll 				 * tracking to do if for some reason output
   5496  1.1.2.2  skrll 				 * fails. Then I have got to reconstruct the
   5497  1.1.2.2  skrll 				 * merged control chain.. el yucko.. for now
   5498  1.1.2.2  skrll 				 * we take the easy way and do the copy
   5499  1.1.2.2  skrll 				 */
   5500  1.1.2.2  skrll 				outchain = sctp_copy_mbufchain(chk->data,
   5501  1.1.2.2  skrll 							       outchain);
   5502  1.1.2.2  skrll 				if (outchain == NULL) {
   5503  1.1.2.2  skrll 					return (ENOMEM);
   5504  1.1.2.2  skrll 				}
   5505  1.1.2.2  skrll 				/* update our MTU size */
   5506  1.1.2.2  skrll 				if (mtu > chk->data->m_pkthdr.len)
   5507  1.1.2.2  skrll 					mtu -= chk->data->m_pkthdr.len;
   5508  1.1.2.2  skrll 				else
   5509  1.1.2.2  skrll 					mtu = 0;
   5510  1.1.2.2  skrll 				/* Do clear IP_DF ? */
   5511  1.1.2.2  skrll 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
   5512  1.1.2.2  skrll 					no_fragmentflg = 0;
   5513  1.1.2.2  skrll 				}
   5514  1.1.2.2  skrll 				/* Mark things to be removed, if needed */
   5515  1.1.2.2  skrll 				if ((chk->rec.chunk_id == SCTP_SELECTIVE_ACK) ||
   5516  1.1.2.2  skrll 				    (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST) ||
   5517  1.1.2.2  skrll 				    (chk->rec.chunk_id == SCTP_HEARTBEAT_ACK) ||
   5518  1.1.2.2  skrll 				    (chk->rec.chunk_id == SCTP_SHUTDOWN) ||
   5519  1.1.2.2  skrll 				    (chk->rec.chunk_id == SCTP_SHUTDOWN_ACK) ||
   5520  1.1.2.2  skrll 				    (chk->rec.chunk_id == SCTP_OPERATION_ERROR) ||
   5521  1.1.2.2  skrll 				    (chk->rec.chunk_id == SCTP_COOKIE_ACK) ||
   5522  1.1.2.2  skrll 				    (chk->rec.chunk_id == SCTP_ECN_CWR) ||
   5523  1.1.2.2  skrll 				    (chk->rec.chunk_id == SCTP_PACKET_DROPPED) ||
   5524  1.1.2.2  skrll 				    (chk->rec.chunk_id == SCTP_ASCONF_ACK)) {
   5525  1.1.2.2  skrll 
   5526  1.1.2.2  skrll 					if (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST)
   5527  1.1.2.2  skrll 						hbflag = 1;
   5528  1.1.2.2  skrll 					/* remove these chunks at the end */
   5529  1.1.2.2  skrll 					if (chk->rec.chunk_id == SCTP_SELECTIVE_ACK) {
   5530  1.1.2.2  skrll 						/* turn off the timer */
   5531  1.1.2.2  skrll 						if (callout_pending(&stcb->asoc.dack_timer.timer)) {
   5532  1.1.2.2  skrll 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
   5533  1.1.2.2  skrll 									inp, stcb, net);
   5534  1.1.2.2  skrll 						}
   5535  1.1.2.2  skrll 					}
   5536  1.1.2.2  skrll 					ctl_cnt++;
   5537  1.1.2.2  skrll 				} else {
   5538  1.1.2.2  skrll 					/*
   5539  1.1.2.2  skrll 					 * Other chunks, since they have
   5540  1.1.2.2  skrll 					 * timers running (i.e. COOKIE or
   5541  1.1.2.2  skrll 					 * ASCONF) we just "trust" that it
   5542  1.1.2.2  skrll 					 * gets sent or retransmitted.
   5543  1.1.2.2  skrll 					 */
   5544  1.1.2.2  skrll 					ctl_cnt++;
   5545  1.1.2.2  skrll 					if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
   5546  1.1.2.2  skrll 						cookie = 1;
   5547  1.1.2.2  skrll 						no_out_cnt = 1;
   5548  1.1.2.2  skrll 					} else if (chk->rec.chunk_id == SCTP_ASCONF) {
   5549  1.1.2.2  skrll 						/*
   5550  1.1.2.2  skrll 						 * set hb flag since we can use
   5551  1.1.2.2  skrll 						 * these for RTO
   5552  1.1.2.2  skrll 						 */
   5553  1.1.2.2  skrll 						hbflag = 1;
   5554  1.1.2.2  skrll 						asconf = 1;
   5555  1.1.2.2  skrll 					}
   5556  1.1.2.2  skrll 					chk->sent = SCTP_DATAGRAM_SENT;
   5557  1.1.2.2  skrll 					chk->snd_count++;
   5558  1.1.2.2  skrll 				}
   5559  1.1.2.2  skrll 				if (mtu == 0) {
   5560  1.1.2.2  skrll 					/*
   5561  1.1.2.2  skrll 					 * Ok we are out of room but we can
   5562  1.1.2.2  skrll 					 * output without effecting the flight
   5563  1.1.2.2  skrll 					 * size since this little guy is a
   5564  1.1.2.2  skrll 					 * control only packet.
   5565  1.1.2.2  skrll 					 */
   5566  1.1.2.2  skrll 					if (asconf) {
   5567  1.1.2.2  skrll 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
   5568  1.1.2.2  skrll 						asconf = 0;
   5569  1.1.2.2  skrll 					}
   5570  1.1.2.2  skrll 					if (cookie) {
   5571  1.1.2.2  skrll 						sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
   5572  1.1.2.2  skrll 						cookie = 0;
   5573  1.1.2.2  skrll 					}
   5574  1.1.2.2  skrll 					if (outchain->m_len == 0) {
   5575  1.1.2.2  skrll 						/*
   5576  1.1.2.2  skrll 						 * Special case for when you
   5577  1.1.2.2  skrll 						 * get a 0 len mbuf at the
   5578  1.1.2.2  skrll 						 * head due to the lack of a
   5579  1.1.2.2  skrll 						 * MHDR at the beginning.
   5580  1.1.2.2  skrll 						 */
   5581  1.1.2.2  skrll 						outchain->m_len = sizeof(struct sctphdr);
   5582  1.1.2.2  skrll 					} else {
   5583  1.1.2.2  skrll 						M_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
   5584  1.1.2.2  skrll 						if (outchain == NULL) {
   5585  1.1.2.2  skrll 							/* no memory */
   5586  1.1.2.2  skrll 							error = ENOBUFS;
   5587  1.1.2.2  skrll 							goto error_out_again;
   5588  1.1.2.2  skrll 						}
   5589  1.1.2.2  skrll 					}
   5590  1.1.2.2  skrll 					shdr = mtod(outchain, struct sctphdr *);
   5591  1.1.2.2  skrll 					shdr->src_port = inp->sctp_lport;
   5592  1.1.2.2  skrll 					shdr->dest_port = stcb->rport;
   5593  1.1.2.2  skrll 					shdr->v_tag = htonl(stcb->asoc.peer_vtag);
   5594  1.1.2.2  skrll 					shdr->checksum = 0;
   5595  1.1.2.2  skrll 
   5596  1.1.2.2  skrll 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
   5597  1.1.2.2  skrll 										rtcache_getdst(&net->ro),
   5598  1.1.2.2  skrll 										outchain,
   5599  1.1.2.2  skrll 										no_fragmentflg, 0, NULL, asconf))) {
   5600  1.1.2.2  skrll 						if (error == ENOBUFS) {
   5601  1.1.2.2  skrll 							asoc->ifp_had_enobuf = 1;
   5602  1.1.2.2  skrll 						}
   5603  1.1.2.2  skrll 						sctp_pegs[SCTP_DATA_OUT_ERR]++;
   5604  1.1.2.2  skrll 						if (from_where == 0) {
   5605  1.1.2.2  skrll 							sctp_pegs[SCTP_ERROUT_FRM_USR]++;
   5606  1.1.2.2  skrll 						}
   5607  1.1.2.2  skrll 					error_out_again:
   5608  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5609  1.1.2.2  skrll 						if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   5610  1.1.2.2  skrll 							printf("Gak got ctrl error %d\n", error);
   5611  1.1.2.2  skrll 						}
   5612  1.1.2.2  skrll #endif
   5613  1.1.2.2  skrll 						/* error, could not output */
   5614  1.1.2.2  skrll 						if (hbflag) {
   5615  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5616  1.1.2.2  skrll 							if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5617  1.1.2.2  skrll 								printf("Update HB anyway\n");
   5618  1.1.2.2  skrll 							}
   5619  1.1.2.2  skrll #endif
   5620  1.1.2.2  skrll 							if (*now_filled == 0) {
   5621  1.1.2.2  skrll 								SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
   5622  1.1.2.2  skrll 								*now_filled = 1;
   5623  1.1.2.2  skrll 								*now = net->last_sent_time;
   5624  1.1.2.2  skrll 							} else {
   5625  1.1.2.2  skrll 								net->last_sent_time = *now;
   5626  1.1.2.2  skrll 							}
   5627  1.1.2.2  skrll 							hbflag = 0;
   5628  1.1.2.2  skrll 						}
   5629  1.1.2.2  skrll 						if (error == EHOSTUNREACH) {
   5630  1.1.2.2  skrll 							/*
   5631  1.1.2.2  skrll 							 * Destination went
   5632  1.1.2.2  skrll 							 * unreachable during
   5633  1.1.2.2  skrll 							 * this send
   5634  1.1.2.2  skrll 							 */
   5635  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5636  1.1.2.2  skrll 							if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5637  1.1.2.2  skrll 								printf("Moving data to an alterante\n");
   5638  1.1.2.2  skrll 							}
   5639  1.1.2.2  skrll #endif
   5640  1.1.2.2  skrll 							sctp_move_to_an_alt(stcb, asoc, net);
   5641  1.1.2.2  skrll 						}
   5642  1.1.2.2  skrll 						sctp_clean_up_ctl (asoc);
   5643  1.1.2.2  skrll 						return (error);
   5644  1.1.2.2  skrll 					} else
   5645  1.1.2.2  skrll 						asoc->ifp_had_enobuf = 0;
   5646  1.1.2.2  skrll 					/* Only HB or ASCONF advances time */
   5647  1.1.2.2  skrll 					if (hbflag) {
   5648  1.1.2.2  skrll 						if (*now_filled == 0) {
   5649  1.1.2.2  skrll 							SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
   5650  1.1.2.2  skrll 							*now_filled = 1;
   5651  1.1.2.2  skrll 							*now = net->last_sent_time;
   5652  1.1.2.2  skrll 						} else {
   5653  1.1.2.2  skrll 							net->last_sent_time = *now;
   5654  1.1.2.2  skrll 						}
   5655  1.1.2.2  skrll 						hbflag = 0;
   5656  1.1.2.2  skrll 					}
   5657  1.1.2.2  skrll 					/*
   5658  1.1.2.2  skrll 					 * increase the number we sent, if a
   5659  1.1.2.2  skrll 					 * cookie is sent we don't tell them
   5660  1.1.2.2  skrll 					 * any was sent out.
   5661  1.1.2.2  skrll 					 */
   5662  1.1.2.2  skrll 					if (!no_out_cnt)
   5663  1.1.2.2  skrll 						*num_out +=  ctl_cnt;
   5664  1.1.2.2  skrll 					/* recalc a clean slate and setup */
   5665  1.1.2.2  skrll 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   5666  1.1.2.2  skrll 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
   5667  1.1.2.2  skrll 					} else {
   5668  1.1.2.2  skrll 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
   5669  1.1.2.2  skrll 					}
   5670  1.1.2.2  skrll 					no_fragmentflg = 1;
   5671  1.1.2.2  skrll 				}
   5672  1.1.2.2  skrll 			}
   5673  1.1.2.2  skrll 		}
   5674  1.1.2.2  skrll 		/*********************/
   5675  1.1.2.2  skrll 		/* Data transmission */
   5676  1.1.2.2  skrll 		/*********************/
   5677  1.1.2.2  skrll 		/* now lets add any data within the MTU constraints */
   5678  1.1.2.2  skrll 		if (((struct sockaddr *)&net->ro.ro_sa)->sa_family == AF_INET) {
   5679  1.1.2.2  skrll 			omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
   5680  1.1.2.2  skrll 		} else {
   5681  1.1.2.2  skrll 			omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
   5682  1.1.2.2  skrll 		}
   5683  1.1.2.2  skrll 
   5684  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5685  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5686  1.1.2.2  skrll 			printf("Now to data transmission\n");
   5687  1.1.2.2  skrll 		}
   5688  1.1.2.2  skrll #endif
   5689  1.1.2.2  skrll 
   5690  1.1.2.2  skrll 		if (((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) ||
   5691  1.1.2.2  skrll 		    (cookie)) {
   5692  1.1.2.2  skrll 			for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
   5693  1.1.2.2  skrll 				if (no_data_chunks) {
   5694  1.1.2.2  skrll 					/* let only control go out */
   5695  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5696  1.1.2.2  skrll 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5697  1.1.2.2  skrll 						printf("Either nothing to send or we are full\n");
   5698  1.1.2.2  skrll 					}
   5699  1.1.2.2  skrll #endif
   5700  1.1.2.2  skrll 					break;
   5701  1.1.2.2  skrll 				}
   5702  1.1.2.2  skrll 				if (net->flight_size >= net->cwnd) {
   5703  1.1.2.2  skrll 					/* skip this net, no room for data */
   5704  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5705  1.1.2.2  skrll 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5706  1.1.2.2  skrll 						printf("fs:%d > cwnd:%d\n",
   5707  1.1.2.2  skrll 						       net->flight_size, net->cwnd);
   5708  1.1.2.2  skrll 					}
   5709  1.1.2.2  skrll #endif
   5710  1.1.2.2  skrll 					sctp_pegs[SCTP_CWND_BLOCKED]++;
   5711  1.1.2.2  skrll 					*reason_code = 2;
   5712  1.1.2.2  skrll 					break;
   5713  1.1.2.2  skrll 				}
   5714  1.1.2.2  skrll 				nchk = TAILQ_NEXT(chk, sctp_next);
   5715  1.1.2.2  skrll 				if (chk->whoTo != net) {
   5716  1.1.2.2  skrll 					/* No, not sent to this net */
   5717  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5718  1.1.2.2  skrll 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5719  1.1.2.2  skrll 						printf("chk->whoTo:%p not %p\n",
   5720  1.1.2.2  skrll 						       chk->whoTo, net);
   5721  1.1.2.2  skrll 
   5722  1.1.2.2  skrll 					}
   5723  1.1.2.2  skrll #endif
   5724  1.1.2.2  skrll 					continue;
   5725  1.1.2.2  skrll 				}
   5726  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5727  1.1.2.2  skrll 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5728  1.1.2.2  skrll 					printf("Can we pick up a chunk?\n");
   5729  1.1.2.2  skrll 				}
   5730  1.1.2.2  skrll #endif
   5731  1.1.2.2  skrll 				if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
   5732  1.1.2.2  skrll 					/* strange, we have a chunk that is to bit
   5733  1.1.2.2  skrll 					 * for its destination and yet no fragment ok flag.
   5734  1.1.2.2  skrll 					 * Something went wrong when the PMTU changed...we did
   5735  1.1.2.2  skrll 					 * not mark this chunk for some reason?? I will
   5736  1.1.2.2  skrll 					 * fix it here by letting IP fragment it for now and
   5737  1.1.2.2  skrll 					 * printing a warning. This really should not happen ...
   5738  1.1.2.2  skrll 					 */
   5739  1.1.2.2  skrll /*#ifdef SCTP_DEBUG*/
   5740  1.1.2.2  skrll 					printf("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
   5741  1.1.2.2  skrll 					       chk->send_size, mtu);
   5742  1.1.2.2  skrll /*#endif*/
   5743  1.1.2.2  skrll 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
   5744  1.1.2.2  skrll 				}
   5745  1.1.2.2  skrll 
   5746  1.1.2.2  skrll 				if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
   5747  1.1.2.2  skrll 				    ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
   5748  1.1.2.2  skrll 					/* ok we will add this one */
   5749  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5750  1.1.2.2  skrll 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5751  1.1.2.2  skrll 						printf("Picking up the chunk\n");
   5752  1.1.2.2  skrll 					}
   5753  1.1.2.2  skrll #endif
   5754  1.1.2.2  skrll 					outchain = sctp_copy_mbufchain(chk->data, outchain);
   5755  1.1.2.2  skrll 					if (outchain == NULL) {
   5756  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5757  1.1.2.2  skrll 						if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5758  1.1.2.2  skrll 							printf("Gakk no memory\n");
   5759  1.1.2.2  skrll 						}
   5760  1.1.2.2  skrll #endif
   5761  1.1.2.2  skrll 						if (!callout_pending(&net->rxt_timer.timer)) {
   5762  1.1.2.2  skrll 							sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
   5763  1.1.2.2  skrll 						}
   5764  1.1.2.2  skrll 						return (ENOMEM);
   5765  1.1.2.2  skrll 					}
   5766  1.1.2.2  skrll 					/* upate our MTU size */
   5767  1.1.2.2  skrll 					/* Do clear IP_DF ? */
   5768  1.1.2.2  skrll 					if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
   5769  1.1.2.2  skrll 						no_fragmentflg = 0;
   5770  1.1.2.2  skrll 					}
   5771  1.1.2.2  skrll 					mtu -= chk->send_size;
   5772  1.1.2.2  skrll 					r_mtu -= chk->send_size;
   5773  1.1.2.2  skrll 					data_list[bundle_at++] = chk;
   5774  1.1.2.2  skrll 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
   5775  1.1.2.2  skrll 						mtu = 0;
   5776  1.1.2.2  skrll 						break;
   5777  1.1.2.2  skrll 					}
   5778  1.1.2.2  skrll 					if (mtu <= 0) {
   5779  1.1.2.2  skrll 						mtu = 0;
   5780  1.1.2.2  skrll 						break;
   5781  1.1.2.2  skrll 					}
   5782  1.1.2.2  skrll 					if ((r_mtu <= 0) || one_chunk) {
   5783  1.1.2.2  skrll 						r_mtu = 0;
   5784  1.1.2.2  skrll 						break;
   5785  1.1.2.2  skrll 					}
   5786  1.1.2.2  skrll 				} else {
   5787  1.1.2.2  skrll 					/*
   5788  1.1.2.2  skrll 					 * Must be sent in order of the TSN's
   5789  1.1.2.2  skrll 					 * (on a network)
   5790  1.1.2.2  skrll 					 */
   5791  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5792  1.1.2.2  skrll 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5793  1.1.2.2  skrll 						printf("ok no more chk:%d > mtu:%d || < r_mtu:%d\n",
   5794  1.1.2.2  skrll 						       chk->send_size, mtu, r_mtu);
   5795  1.1.2.2  skrll 					}
   5796  1.1.2.2  skrll #endif
   5797  1.1.2.2  skrll 
   5798  1.1.2.2  skrll 					break;
   5799  1.1.2.2  skrll 				}
   5800  1.1.2.2  skrll 			}/* for () */
   5801  1.1.2.2  skrll 		} /* if asoc.state OPEN */
   5802  1.1.2.2  skrll 		/* Is there something to send for this destination? */
   5803  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5804  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5805  1.1.2.2  skrll 			printf("ok now is chain assembled? %p\n",
   5806  1.1.2.2  skrll 			       outchain);
   5807  1.1.2.2  skrll 		}
   5808  1.1.2.2  skrll #endif
   5809  1.1.2.2  skrll 
   5810  1.1.2.2  skrll 		if (outchain) {
   5811  1.1.2.2  skrll 			/* We may need to start a control timer or two */
   5812  1.1.2.2  skrll 			if (asconf) {
   5813  1.1.2.2  skrll 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
   5814  1.1.2.2  skrll 				asconf = 0;
   5815  1.1.2.2  skrll 			}
   5816  1.1.2.2  skrll 			if (cookie) {
   5817  1.1.2.2  skrll 				sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
   5818  1.1.2.2  skrll 				cookie = 0;
   5819  1.1.2.2  skrll 			}
   5820  1.1.2.2  skrll 			/* must start a send timer if data is being sent */
   5821  1.1.2.2  skrll 			if (bundle_at && (!callout_pending(&net->rxt_timer.timer))) {
   5822  1.1.2.2  skrll 				/* no timer running on this destination
   5823  1.1.2.2  skrll 				 * restart it.
   5824  1.1.2.2  skrll 				 */
   5825  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5826  1.1.2.2  skrll 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5827  1.1.2.2  skrll 					printf("ok lets start a send timer .. we will transmit %p\n",
   5828  1.1.2.2  skrll 					       outchain);
   5829  1.1.2.2  skrll 				}
   5830  1.1.2.2  skrll #endif
   5831  1.1.2.2  skrll 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
   5832  1.1.2.2  skrll 			}
   5833  1.1.2.2  skrll 			/* Now send it, if there is anything to send :> */
   5834  1.1.2.2  skrll 			if ((outchain->m_flags & M_PKTHDR) == 0) {
   5835  1.1.2.2  skrll 				struct mbuf *t;
   5836  1.1.2.2  skrll 
   5837  1.1.2.2  skrll 				MGETHDR(t, M_DONTWAIT, MT_HEADER);
   5838  1.1.2.2  skrll 				if (t == NULL) {
   5839  1.1.2.2  skrll 					sctp_m_freem(outchain);
   5840  1.1.2.2  skrll 					return (ENOMEM);
   5841  1.1.2.2  skrll 				}
   5842  1.1.2.2  skrll 				t->m_next = outchain;
   5843  1.1.2.2  skrll 				t->m_pkthdr.len = 0;
   5844  1.1.2.5  skrll 				m_reset_rcvif(t);
   5845  1.1.2.2  skrll 				t->m_len = 0;
   5846  1.1.2.2  skrll 
   5847  1.1.2.2  skrll 				outchain = t;
   5848  1.1.2.2  skrll 				while (t) {
   5849  1.1.2.2  skrll 					outchain->m_pkthdr.len += t->m_len;
   5850  1.1.2.2  skrll 					t = t->m_next;
   5851  1.1.2.2  skrll 				}
   5852  1.1.2.2  skrll 			}
   5853  1.1.2.2  skrll 			if (outchain->m_len == 0) {
   5854  1.1.2.2  skrll 				/* Special case for when you get a 0 len
   5855  1.1.2.2  skrll 				 * mbuf at the head due to the lack
   5856  1.1.2.2  skrll 				 * of a MHDR at the beginning.
   5857  1.1.2.2  skrll 				 */
   5858  1.1.2.2  skrll 				MH_ALIGN(outchain, sizeof(struct sctphdr));
   5859  1.1.2.2  skrll 				outchain->m_len = sizeof(struct sctphdr);
   5860  1.1.2.2  skrll 			} else {
   5861  1.1.2.2  skrll 				M_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
   5862  1.1.2.2  skrll 				if (outchain == NULL) {
   5863  1.1.2.2  skrll 					/* out of mbufs */
   5864  1.1.2.2  skrll 					error = ENOBUFS;
   5865  1.1.2.2  skrll 					goto errored_send;
   5866  1.1.2.2  skrll 				}
   5867  1.1.2.2  skrll 			}
   5868  1.1.2.2  skrll 			shdr = mtod(outchain, struct sctphdr *);
   5869  1.1.2.2  skrll 			shdr->src_port = inp->sctp_lport;
   5870  1.1.2.2  skrll 			shdr->dest_port = stcb->rport;
   5871  1.1.2.2  skrll 			shdr->v_tag = htonl(stcb->asoc.peer_vtag);
   5872  1.1.2.2  skrll 			shdr->checksum = 0;
   5873  1.1.2.2  skrll 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
   5874  1.1.2.2  skrll 								rtcache_getdst(&net->ro),
   5875  1.1.2.2  skrll 								outchain,
   5876  1.1.2.2  skrll 								no_fragmentflg, bundle_at, data_list[0], asconf))) {
   5877  1.1.2.2  skrll 				/* error, we could not output */
   5878  1.1.2.2  skrll 				if (error == ENOBUFS) {
   5879  1.1.2.2  skrll 					asoc->ifp_had_enobuf = 1;
   5880  1.1.2.2  skrll 				}
   5881  1.1.2.2  skrll 				sctp_pegs[SCTP_DATA_OUT_ERR]++;
   5882  1.1.2.2  skrll 				if (from_where == 0) {
   5883  1.1.2.2  skrll 					sctp_pegs[SCTP_ERROUT_FRM_USR]++;
   5884  1.1.2.2  skrll 				}
   5885  1.1.2.2  skrll 
   5886  1.1.2.2  skrll 			errored_send:
   5887  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5888  1.1.2.2  skrll 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5889  1.1.2.2  skrll 					printf("Gak send error %d\n", error);
   5890  1.1.2.2  skrll 				}
   5891  1.1.2.2  skrll #endif
   5892  1.1.2.2  skrll 				if (hbflag) {
   5893  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5894  1.1.2.2  skrll 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5895  1.1.2.2  skrll 						printf("Update HB time anyway\n");
   5896  1.1.2.2  skrll 					}
   5897  1.1.2.2  skrll #endif
   5898  1.1.2.2  skrll 					if (*now_filled == 0) {
   5899  1.1.2.2  skrll 						SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
   5900  1.1.2.2  skrll 						*now_filled = 1;
   5901  1.1.2.2  skrll 						*now = net->last_sent_time;
   5902  1.1.2.2  skrll 					} else {
   5903  1.1.2.2  skrll 						net->last_sent_time = *now;
   5904  1.1.2.2  skrll 					}
   5905  1.1.2.2  skrll 					hbflag = 0;
   5906  1.1.2.2  skrll 				}
   5907  1.1.2.2  skrll 				if (error == EHOSTUNREACH) {
   5908  1.1.2.2  skrll 					/*
   5909  1.1.2.2  skrll 					 * Destination went unreachable during
   5910  1.1.2.2  skrll 					 * this send
   5911  1.1.2.2  skrll 					 */
   5912  1.1.2.2  skrll #ifdef SCTP_DEBUG
   5913  1.1.2.2  skrll 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5914  1.1.2.2  skrll 						printf("Calling the movement routine\n");
   5915  1.1.2.2  skrll 					}
   5916  1.1.2.2  skrll #endif
   5917  1.1.2.2  skrll 					sctp_move_to_an_alt(stcb, asoc, net);
   5918  1.1.2.2  skrll 				}
   5919  1.1.2.2  skrll 				sctp_clean_up_ctl (asoc);
   5920  1.1.2.2  skrll 				return (error);
   5921  1.1.2.2  skrll 			} else {
   5922  1.1.2.2  skrll 				asoc->ifp_had_enobuf = 0;
   5923  1.1.2.2  skrll 			}
   5924  1.1.2.2  skrll 			if (bundle_at || hbflag) {
   5925  1.1.2.2  skrll 				/* For data/asconf and hb set time */
   5926  1.1.2.2  skrll 				if (*now_filled == 0) {
   5927  1.1.2.2  skrll 					SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
   5928  1.1.2.2  skrll 					*now_filled = 1;
   5929  1.1.2.2  skrll 					*now = net->last_sent_time;
   5930  1.1.2.2  skrll 				} else {
   5931  1.1.2.2  skrll 					net->last_sent_time = *now;
   5932  1.1.2.2  skrll 				}
   5933  1.1.2.2  skrll 			}
   5934  1.1.2.2  skrll 
   5935  1.1.2.2  skrll 			if (!no_out_cnt) {
   5936  1.1.2.2  skrll 				*num_out += (ctl_cnt + bundle_at);
   5937  1.1.2.2  skrll 			}
   5938  1.1.2.2  skrll 			if (bundle_at) {
   5939  1.1.2.2  skrll 				if (!net->rto_pending) {
   5940  1.1.2.2  skrll 					/* setup for a RTO measurement */
   5941  1.1.2.2  skrll 					net->rto_pending = 1;
   5942  1.1.2.2  skrll 					data_list[0]->do_rtt = 1;
   5943  1.1.2.2  skrll 				} else {
   5944  1.1.2.2  skrll 					data_list[0]->do_rtt = 0;
   5945  1.1.2.2  skrll 				}
   5946  1.1.2.2  skrll 				sctp_pegs[SCTP_PEG_TSNS_SENT] += bundle_at;
   5947  1.1.2.2  skrll 				sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
   5948  1.1.2.2  skrll 			}
   5949  1.1.2.2  skrll 			if (one_chunk) {
   5950  1.1.2.2  skrll 				break;
   5951  1.1.2.2  skrll 			}
   5952  1.1.2.2  skrll 		}
   5953  1.1.2.2  skrll 	}
   5954  1.1.2.2  skrll 	/* At the end there should be no NON timed
   5955  1.1.2.2  skrll 	 * chunks hanging on this queue.
   5956  1.1.2.2  skrll 	 */
   5957  1.1.2.2  skrll 	if ((*num_out == 0) && (*reason_code == 0)) {
   5958  1.1.2.2  skrll 		*reason_code = 3;
   5959  1.1.2.2  skrll 	}
   5960  1.1.2.2  skrll 	sctp_clean_up_ctl (asoc);
   5961  1.1.2.2  skrll 	return (0);
   5962  1.1.2.2  skrll }
   5963  1.1.2.2  skrll 
   5964  1.1.2.2  skrll void
   5965  1.1.2.2  skrll sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
   5966  1.1.2.2  skrll {
   5967  1.1.2.2  skrll 	/* Prepend a OPERATIONAL_ERROR chunk header
   5968  1.1.2.2  skrll 	 * and put on the end of the control chunk queue.
   5969  1.1.2.2  skrll 	 */
   5970  1.1.2.2  skrll 	/* Sender had better have gotten a MGETHDR or else
   5971  1.1.2.2  skrll 	 * the control chunk will be forever skipped
   5972  1.1.2.2  skrll 	 */
   5973  1.1.2.2  skrll 	struct sctp_chunkhdr *hdr;
   5974  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   5975  1.1.2.2  skrll 	struct mbuf *mat;
   5976  1.1.2.2  skrll 
   5977  1.1.2.2  skrll 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   5978  1.1.2.2  skrll 	if (chk == NULL) {
   5979  1.1.2.2  skrll 		/* no memory */
   5980  1.1.2.2  skrll 		sctp_m_freem(op_err);
   5981  1.1.2.2  skrll 		return;
   5982  1.1.2.2  skrll 	}
   5983  1.1.2.2  skrll 	sctppcbinfo.ipi_count_chunk++;
   5984  1.1.2.2  skrll 	sctppcbinfo.ipi_gencnt_chunk++;
   5985  1.1.2.2  skrll 	M_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
   5986  1.1.2.2  skrll 	if (op_err == NULL) {
   5987  1.1.2.2  skrll 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   5988  1.1.2.2  skrll 		sctppcbinfo.ipi_count_chunk--;
   5989  1.1.2.2  skrll 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   5990  1.1.2.2  skrll 			panic("Chunk count is negative");
   5991  1.1.2.2  skrll 		}
   5992  1.1.2.2  skrll 		sctppcbinfo.ipi_gencnt_chunk++;
   5993  1.1.2.2  skrll 		return;
   5994  1.1.2.2  skrll 	}
   5995  1.1.2.2  skrll 	chk->send_size = 0;
   5996  1.1.2.2  skrll 	mat = op_err;
   5997  1.1.2.2  skrll 	while (mat != NULL) {
   5998  1.1.2.2  skrll 		chk->send_size += mat->m_len;
   5999  1.1.2.2  skrll 		mat = mat->m_next;
   6000  1.1.2.2  skrll 	}
   6001  1.1.2.2  skrll 	chk->rec.chunk_id = SCTP_OPERATION_ERROR;
   6002  1.1.2.2  skrll 	chk->sent = SCTP_DATAGRAM_UNSENT;
   6003  1.1.2.2  skrll 	chk->snd_count = 0;
   6004  1.1.2.2  skrll 	chk->flags = 0;
   6005  1.1.2.2  skrll 	chk->asoc = &stcb->asoc;
   6006  1.1.2.2  skrll 	chk->data = op_err;
   6007  1.1.2.2  skrll 	chk->whoTo = chk->asoc->primary_destination;
   6008  1.1.2.2  skrll 	chk->whoTo->ref_count++;
   6009  1.1.2.2  skrll 	hdr = mtod(op_err, struct sctp_chunkhdr *);
   6010  1.1.2.2  skrll 	hdr->chunk_type = SCTP_OPERATION_ERROR;
   6011  1.1.2.2  skrll 	hdr->chunk_flags = 0;
   6012  1.1.2.2  skrll 	hdr->chunk_length = htons(chk->send_size);
   6013  1.1.2.2  skrll 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
   6014  1.1.2.2  skrll 			  chk,
   6015  1.1.2.2  skrll 			  sctp_next);
   6016  1.1.2.2  skrll 	chk->asoc->ctrl_queue_cnt++;
   6017  1.1.2.2  skrll }
   6018  1.1.2.2  skrll 
   6019  1.1.2.2  skrll int
   6020  1.1.2.2  skrll sctp_send_cookie_echo(struct mbuf *m,
   6021  1.1.2.2  skrll 		      int offset,
   6022  1.1.2.2  skrll 		      struct sctp_tcb *stcb,
   6023  1.1.2.2  skrll 		      struct sctp_nets *net)
   6024  1.1.2.2  skrll {
   6025  1.1.2.2  skrll 	/*
   6026  1.1.2.2  skrll 	 * pull out the cookie and put it at the front of the control
   6027  1.1.2.2  skrll 	 * chunk queue.
   6028  1.1.2.2  skrll 	 */
   6029  1.1.2.2  skrll 	int at;
   6030  1.1.2.2  skrll 	struct mbuf *cookie, *mat;
   6031  1.1.2.2  skrll 	struct sctp_paramhdr parm, *phdr;
   6032  1.1.2.2  skrll 	struct sctp_chunkhdr *hdr;
   6033  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   6034  1.1.2.2  skrll 	uint16_t ptype, plen;
   6035  1.1.2.2  skrll 	/* First find the cookie in the param area */
   6036  1.1.2.2  skrll 	cookie = NULL;
   6037  1.1.2.2  skrll 	at = offset + sizeof(struct sctp_init_chunk);
   6038  1.1.2.2  skrll 
   6039  1.1.2.2  skrll 	do {
   6040  1.1.2.2  skrll 		phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
   6041  1.1.2.2  skrll 		if (phdr == NULL) {
   6042  1.1.2.2  skrll 			return (-3);
   6043  1.1.2.2  skrll 		}
   6044  1.1.2.2  skrll 		ptype = ntohs(phdr->param_type);
   6045  1.1.2.2  skrll 		plen = ntohs(phdr->param_length);
   6046  1.1.2.2  skrll 		if (ptype == SCTP_STATE_COOKIE) {
   6047  1.1.2.2  skrll 			int pad;
   6048  1.1.2.2  skrll 			/* found the cookie */
   6049  1.1.2.2  skrll 			if ((pad = (plen % 4))) {
   6050  1.1.2.2  skrll 				plen += 4 - pad;
   6051  1.1.2.2  skrll 			}
   6052  1.1.2.2  skrll 			cookie = sctp_m_copym(m, at, plen, M_DONTWAIT);
   6053  1.1.2.2  skrll 			if (cookie == NULL) {
   6054  1.1.2.2  skrll 				/* No memory */
   6055  1.1.2.2  skrll 				return (-2);
   6056  1.1.2.2  skrll 			}
   6057  1.1.2.2  skrll 			break;
   6058  1.1.2.2  skrll 		}
   6059  1.1.2.2  skrll 		at += SCTP_SIZE32(plen);
   6060  1.1.2.2  skrll 	} while (phdr);
   6061  1.1.2.2  skrll 	if (cookie == NULL) {
   6062  1.1.2.2  skrll 		/* Did not find the cookie */
   6063  1.1.2.2  skrll 		return (-3);
   6064  1.1.2.2  skrll 	}
   6065  1.1.2.2  skrll 	/* ok, we got the cookie lets change it into a cookie echo chunk */
   6066  1.1.2.2  skrll 
   6067  1.1.2.2  skrll 	/* first the change from param to cookie */
   6068  1.1.2.2  skrll 	hdr = mtod(cookie, struct sctp_chunkhdr *);
   6069  1.1.2.2  skrll 	hdr->chunk_type = SCTP_COOKIE_ECHO;
   6070  1.1.2.2  skrll 	hdr->chunk_flags = 0;
   6071  1.1.2.2  skrll 	/* now we MUST have a PKTHDR on it */
   6072  1.1.2.2  skrll 	if ((cookie->m_flags & M_PKTHDR) != M_PKTHDR) {
   6073  1.1.2.2  skrll 		/* we hope this happens rarely */
   6074  1.1.2.2  skrll 		MGETHDR(mat, M_DONTWAIT, MT_HEADER);
   6075  1.1.2.2  skrll 		if (mat == NULL) {
   6076  1.1.2.2  skrll 			sctp_m_freem(cookie);
   6077  1.1.2.2  skrll 			return (-4);
   6078  1.1.2.2  skrll 		}
   6079  1.1.2.2  skrll 		mat->m_len = 0;
   6080  1.1.2.5  skrll 		m_reset_rcvif(mat);
   6081  1.1.2.2  skrll 		mat->m_next = cookie;
   6082  1.1.2.2  skrll 		cookie = mat;
   6083  1.1.2.2  skrll 	}
   6084  1.1.2.2  skrll 	cookie->m_pkthdr.len = plen;
   6085  1.1.2.2  skrll 	/* get the chunk stuff now and place it in the FRONT of the queue */
   6086  1.1.2.2  skrll 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   6087  1.1.2.2  skrll 	if (chk == NULL) {
   6088  1.1.2.2  skrll 		/* no memory */
   6089  1.1.2.2  skrll 		sctp_m_freem(cookie);
   6090  1.1.2.2  skrll 		return (-5);
   6091  1.1.2.2  skrll 	}
   6092  1.1.2.2  skrll 	sctppcbinfo.ipi_count_chunk++;
   6093  1.1.2.2  skrll 	sctppcbinfo.ipi_gencnt_chunk++;
   6094  1.1.2.2  skrll 	chk->send_size = cookie->m_pkthdr.len;
   6095  1.1.2.2  skrll 	chk->rec.chunk_id = SCTP_COOKIE_ECHO;
   6096  1.1.2.2  skrll 	chk->sent = SCTP_DATAGRAM_UNSENT;
   6097  1.1.2.2  skrll 	chk->snd_count = 0;
   6098  1.1.2.2  skrll 	chk->flags = 0;
   6099  1.1.2.2  skrll 	chk->asoc = &stcb->asoc;
   6100  1.1.2.2  skrll 	chk->data = cookie;
   6101  1.1.2.2  skrll 	chk->whoTo = chk->asoc->primary_destination;
   6102  1.1.2.2  skrll 	chk->whoTo->ref_count++;
   6103  1.1.2.2  skrll 	TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
   6104  1.1.2.2  skrll 	chk->asoc->ctrl_queue_cnt++;
   6105  1.1.2.2  skrll 	return (0);
   6106  1.1.2.2  skrll }
   6107  1.1.2.2  skrll 
   6108  1.1.2.2  skrll void
   6109  1.1.2.2  skrll sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
   6110  1.1.2.2  skrll 			struct mbuf *m,
   6111  1.1.2.2  skrll 			int offset,
   6112  1.1.2.2  skrll 			int chk_length,
   6113  1.1.2.2  skrll 			struct sctp_nets *net)
   6114  1.1.2.2  skrll {
   6115  1.1.2.2  skrll 	/* take a HB request and make it into a
   6116  1.1.2.2  skrll 	 * HB ack and send it.
   6117  1.1.2.2  skrll 	 */
   6118  1.1.2.2  skrll 	struct mbuf *outchain;
   6119  1.1.2.2  skrll 	struct sctp_chunkhdr *chdr;
   6120  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   6121  1.1.2.2  skrll 
   6122  1.1.2.2  skrll 
   6123  1.1.2.2  skrll 	if (net == NULL)
   6124  1.1.2.2  skrll 		/* must have a net pointer */
   6125  1.1.2.2  skrll 		return;
   6126  1.1.2.2  skrll 
   6127  1.1.2.2  skrll 	outchain = sctp_m_copym(m, offset, chk_length, M_DONTWAIT);
   6128  1.1.2.2  skrll 	if (outchain == NULL) {
   6129  1.1.2.2  skrll 		/* gak out of memory */
   6130  1.1.2.2  skrll 		return;
   6131  1.1.2.2  skrll 	}
   6132  1.1.2.2  skrll 	chdr = mtod(outchain, struct sctp_chunkhdr *);
   6133  1.1.2.2  skrll 	chdr->chunk_type = SCTP_HEARTBEAT_ACK;
   6134  1.1.2.2  skrll 	chdr->chunk_flags = 0;
   6135  1.1.2.2  skrll 	if ((outchain->m_flags & M_PKTHDR) != M_PKTHDR) {
   6136  1.1.2.2  skrll 		/* should not happen but we are cautious. */
   6137  1.1.2.2  skrll 		struct mbuf *tmp;
   6138  1.1.2.2  skrll 		MGETHDR(tmp, M_DONTWAIT, MT_HEADER);
   6139  1.1.2.2  skrll 		if (tmp == NULL) {
   6140  1.1.2.2  skrll 			return;
   6141  1.1.2.2  skrll 		}
   6142  1.1.2.2  skrll 		tmp->m_len = 0;
   6143  1.1.2.5  skrll 		m_reset_rcvif(tmp);
   6144  1.1.2.2  skrll 		tmp->m_next = outchain;
   6145  1.1.2.2  skrll 		outchain = tmp;
   6146  1.1.2.2  skrll 	}
   6147  1.1.2.2  skrll 	outchain->m_pkthdr.len = chk_length;
   6148  1.1.2.2  skrll 	if (chk_length % 4) {
   6149  1.1.2.2  skrll 		/* need pad */
   6150  1.1.2.2  skrll 		u_int32_t cpthis=0;
   6151  1.1.2.2  skrll 		int padlen;
   6152  1.1.2.2  skrll 		padlen = 4 - (outchain->m_pkthdr.len % 4);
   6153  1.1.2.2  skrll 		m_copyback(outchain, outchain->m_pkthdr.len, padlen, (void *)&cpthis);
   6154  1.1.2.2  skrll 	}
   6155  1.1.2.2  skrll 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   6156  1.1.2.2  skrll 	if (chk == NULL) {
   6157  1.1.2.2  skrll 		/* no memory */
   6158  1.1.2.2  skrll 		sctp_m_freem(outchain);
   6159  1.1.2.2  skrll 		return ;
   6160  1.1.2.2  skrll 	}
   6161  1.1.2.2  skrll 	sctppcbinfo.ipi_count_chunk++;
   6162  1.1.2.2  skrll 	sctppcbinfo.ipi_gencnt_chunk++;
   6163  1.1.2.2  skrll 
   6164  1.1.2.2  skrll 	chk->send_size = chk_length;
   6165  1.1.2.2  skrll 	chk->rec.chunk_id = SCTP_HEARTBEAT_ACK;
   6166  1.1.2.2  skrll 	chk->sent = SCTP_DATAGRAM_UNSENT;
   6167  1.1.2.2  skrll 	chk->snd_count = 0;
   6168  1.1.2.2  skrll 	chk->flags = 0;
   6169  1.1.2.2  skrll 	chk->asoc = &stcb->asoc;
   6170  1.1.2.2  skrll 	chk->data = outchain;
   6171  1.1.2.2  skrll 	chk->whoTo = net;
   6172  1.1.2.2  skrll 	chk->whoTo->ref_count++;
   6173  1.1.2.2  skrll 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
   6174  1.1.2.2  skrll 	chk->asoc->ctrl_queue_cnt++;
   6175  1.1.2.2  skrll }
   6176  1.1.2.2  skrll 
   6177  1.1.2.2  skrll int
   6178  1.1.2.2  skrll sctp_send_cookie_ack(struct sctp_tcb *stcb) {
   6179  1.1.2.2  skrll 	/* formulate and queue a cookie-ack back to sender */
   6180  1.1.2.2  skrll 	struct mbuf *cookie_ack;
   6181  1.1.2.2  skrll 	struct sctp_chunkhdr *hdr;
   6182  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   6183  1.1.2.2  skrll 
   6184  1.1.2.2  skrll 	cookie_ack = NULL;
   6185  1.1.2.2  skrll 	MGETHDR(cookie_ack, M_DONTWAIT, MT_HEADER);
   6186  1.1.2.2  skrll 	if (cookie_ack == NULL) {
   6187  1.1.2.2  skrll 		/* no mbuf's */
   6188  1.1.2.2  skrll 		return (-1);
   6189  1.1.2.2  skrll 	}
   6190  1.1.2.2  skrll  	cookie_ack->m_data += SCTP_MIN_OVERHEAD;
   6191  1.1.2.2  skrll 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   6192  1.1.2.2  skrll 	if (chk == NULL) {
   6193  1.1.2.2  skrll 		/* no memory */
   6194  1.1.2.2  skrll 		sctp_m_freem(cookie_ack);
   6195  1.1.2.2  skrll 		return (-1);
   6196  1.1.2.2  skrll 	}
   6197  1.1.2.2  skrll 	sctppcbinfo.ipi_count_chunk++;
   6198  1.1.2.2  skrll 	sctppcbinfo.ipi_gencnt_chunk++;
   6199  1.1.2.2  skrll 
   6200  1.1.2.2  skrll 	chk->send_size = sizeof(struct sctp_chunkhdr);
   6201  1.1.2.2  skrll 	chk->rec.chunk_id = SCTP_COOKIE_ACK;
   6202  1.1.2.2  skrll 	chk->sent = SCTP_DATAGRAM_UNSENT;
   6203  1.1.2.2  skrll 	chk->snd_count = 0;
   6204  1.1.2.2  skrll 	chk->flags = 0;
   6205  1.1.2.2  skrll 	chk->asoc = &stcb->asoc;
   6206  1.1.2.2  skrll 	chk->data = cookie_ack;
   6207  1.1.2.2  skrll 	if (chk->asoc->last_control_chunk_from != NULL) {
   6208  1.1.2.2  skrll 		chk->whoTo = chk->asoc->last_control_chunk_from;
   6209  1.1.2.2  skrll 	} else {
   6210  1.1.2.2  skrll 		chk->whoTo = chk->asoc->primary_destination;
   6211  1.1.2.2  skrll 	}
   6212  1.1.2.2  skrll 	chk->whoTo->ref_count++;
   6213  1.1.2.2  skrll 	hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
   6214  1.1.2.2  skrll 	hdr->chunk_type = SCTP_COOKIE_ACK;
   6215  1.1.2.2  skrll 	hdr->chunk_flags = 0;
   6216  1.1.2.2  skrll 	hdr->chunk_length = htons(chk->send_size);
   6217  1.1.2.2  skrll 	cookie_ack->m_pkthdr.len = cookie_ack->m_len = chk->send_size;
   6218  1.1.2.5  skrll 	m_reset_rcvif(cookie_ack);
   6219  1.1.2.2  skrll 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
   6220  1.1.2.2  skrll 	chk->asoc->ctrl_queue_cnt++;
   6221  1.1.2.2  skrll 	return (0);
   6222  1.1.2.2  skrll }
   6223  1.1.2.2  skrll 
   6224  1.1.2.2  skrll 
   6225  1.1.2.2  skrll int
   6226  1.1.2.2  skrll sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
   6227  1.1.2.2  skrll {
   6228  1.1.2.2  skrll 	/* formulate and queue a SHUTDOWN-ACK back to the sender */
   6229  1.1.2.2  skrll 	struct mbuf *m_shutdown_ack;
   6230  1.1.2.2  skrll 	struct sctp_shutdown_ack_chunk *ack_cp;
   6231  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   6232  1.1.2.2  skrll 
   6233  1.1.2.2  skrll 	m_shutdown_ack = NULL;
   6234  1.1.2.2  skrll 	MGETHDR(m_shutdown_ack, M_DONTWAIT, MT_HEADER);
   6235  1.1.2.2  skrll 	if (m_shutdown_ack == NULL) {
   6236  1.1.2.2  skrll 		/* no mbuf's */
   6237  1.1.2.2  skrll 		return (-1);
   6238  1.1.2.2  skrll 	}
   6239  1.1.2.2  skrll 	m_shutdown_ack->m_data += SCTP_MIN_OVERHEAD;
   6240  1.1.2.2  skrll 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   6241  1.1.2.2  skrll 	if (chk == NULL) {
   6242  1.1.2.2  skrll 		/* no memory */
   6243  1.1.2.2  skrll 		sctp_m_freem(m_shutdown_ack);
   6244  1.1.2.2  skrll 		return (-1);
   6245  1.1.2.2  skrll 	}
   6246  1.1.2.2  skrll 	sctppcbinfo.ipi_count_chunk++;
   6247  1.1.2.2  skrll 	sctppcbinfo.ipi_gencnt_chunk++;
   6248  1.1.2.2  skrll 
   6249  1.1.2.2  skrll 	chk->send_size = sizeof(struct sctp_chunkhdr);
   6250  1.1.2.2  skrll 	chk->rec.chunk_id = SCTP_SHUTDOWN_ACK;
   6251  1.1.2.2  skrll 	chk->sent = SCTP_DATAGRAM_UNSENT;
   6252  1.1.2.2  skrll 	chk->snd_count = 0;
   6253  1.1.2.2  skrll 	chk->flags = 0;
   6254  1.1.2.2  skrll 	chk->asoc = &stcb->asoc;
   6255  1.1.2.2  skrll 	chk->data = m_shutdown_ack;
   6256  1.1.2.2  skrll 	chk->whoTo = net;
   6257  1.1.2.2  skrll 	net->ref_count++;
   6258  1.1.2.2  skrll 
   6259  1.1.2.2  skrll 	ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
   6260  1.1.2.2  skrll 	ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
   6261  1.1.2.2  skrll 	ack_cp->ch.chunk_flags = 0;
   6262  1.1.2.2  skrll 	ack_cp->ch.chunk_length = htons(chk->send_size);
   6263  1.1.2.2  skrll 	m_shutdown_ack->m_pkthdr.len = m_shutdown_ack->m_len = chk->send_size;
   6264  1.1.2.5  skrll 	m_reset_rcvif(m_shutdown_ack);
   6265  1.1.2.2  skrll 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
   6266  1.1.2.2  skrll 	chk->asoc->ctrl_queue_cnt++;
   6267  1.1.2.2  skrll 	return (0);
   6268  1.1.2.2  skrll }
   6269  1.1.2.2  skrll 
   6270  1.1.2.2  skrll int
   6271  1.1.2.2  skrll sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
   6272  1.1.2.2  skrll {
   6273  1.1.2.2  skrll 	/* formulate and queue a SHUTDOWN to the sender */
   6274  1.1.2.2  skrll 	struct mbuf *m_shutdown;
   6275  1.1.2.2  skrll 	struct sctp_shutdown_chunk *shutdown_cp;
   6276  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   6277  1.1.2.2  skrll 
   6278  1.1.2.2  skrll 	m_shutdown = NULL;
   6279  1.1.2.2  skrll 	MGETHDR(m_shutdown, M_DONTWAIT, MT_HEADER);
   6280  1.1.2.2  skrll 	if (m_shutdown == NULL) {
   6281  1.1.2.2  skrll 		/* no mbuf's */
   6282  1.1.2.2  skrll 		return (-1);
   6283  1.1.2.2  skrll 	}
   6284  1.1.2.2  skrll 	m_shutdown->m_data += SCTP_MIN_OVERHEAD;
   6285  1.1.2.2  skrll 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   6286  1.1.2.2  skrll 	if (chk == NULL) {
   6287  1.1.2.2  skrll 		/* no memory */
   6288  1.1.2.2  skrll 		sctp_m_freem(m_shutdown);
   6289  1.1.2.2  skrll 		return (-1);
   6290  1.1.2.2  skrll 	}
   6291  1.1.2.2  skrll 	sctppcbinfo.ipi_count_chunk++;
   6292  1.1.2.2  skrll 	sctppcbinfo.ipi_gencnt_chunk++;
   6293  1.1.2.2  skrll 
   6294  1.1.2.2  skrll 	chk->send_size = sizeof(struct sctp_shutdown_chunk);
   6295  1.1.2.2  skrll 	chk->rec.chunk_id = SCTP_SHUTDOWN;
   6296  1.1.2.2  skrll 	chk->sent = SCTP_DATAGRAM_UNSENT;
   6297  1.1.2.2  skrll 	chk->snd_count = 0;
   6298  1.1.2.2  skrll 	chk->flags = 0;
   6299  1.1.2.2  skrll 	chk->asoc = &stcb->asoc;
   6300  1.1.2.2  skrll 	chk->data = m_shutdown;
   6301  1.1.2.2  skrll 	chk->whoTo = net;
   6302  1.1.2.2  skrll 	net->ref_count++;
   6303  1.1.2.2  skrll 
   6304  1.1.2.2  skrll 	shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
   6305  1.1.2.2  skrll 	shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
   6306  1.1.2.2  skrll 	shutdown_cp->ch.chunk_flags = 0;
   6307  1.1.2.2  skrll 	shutdown_cp->ch.chunk_length = htons(chk->send_size);
   6308  1.1.2.2  skrll 	shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
   6309  1.1.2.2  skrll 	m_shutdown->m_pkthdr.len = m_shutdown->m_len = chk->send_size;
   6310  1.1.2.5  skrll 	m_reset_rcvif(m_shutdown);
   6311  1.1.2.2  skrll 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
   6312  1.1.2.2  skrll 	chk->asoc->ctrl_queue_cnt++;
   6313  1.1.2.2  skrll 
   6314  1.1.2.2  skrll 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
   6315  1.1.2.2  skrll 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
   6316  1.1.2.2  skrll 		stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
   6317  1.1.2.2  skrll 		soisdisconnecting(stcb->sctp_ep->sctp_socket);
   6318  1.1.2.2  skrll 	}
   6319  1.1.2.2  skrll 	return (0);
   6320  1.1.2.2  skrll }
   6321  1.1.2.2  skrll 
   6322  1.1.2.2  skrll int
   6323  1.1.2.2  skrll sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net)
   6324  1.1.2.2  skrll {
   6325  1.1.2.2  skrll 	/*
   6326  1.1.2.2  skrll 	 * formulate and queue an ASCONF to the peer
   6327  1.1.2.2  skrll 	 * ASCONF parameters should be queued on the assoc queue
   6328  1.1.2.2  skrll 	 */
   6329  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   6330  1.1.2.2  skrll 	struct mbuf *m_asconf;
   6331  1.1.2.2  skrll 
   6332  1.1.2.2  skrll 	/* compose an ASCONF chunk, maximum length is PMTU */
   6333  1.1.2.2  skrll 	m_asconf = sctp_compose_asconf(stcb);
   6334  1.1.2.2  skrll 	if (m_asconf == NULL) {
   6335  1.1.2.2  skrll 		return (-1);
   6336  1.1.2.2  skrll 	}
   6337  1.1.2.2  skrll 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   6338  1.1.2.2  skrll 	if (chk == NULL) {
   6339  1.1.2.2  skrll 		/* no memory */
   6340  1.1.2.2  skrll 		sctp_m_freem(m_asconf);
   6341  1.1.2.2  skrll 		return (-1);
   6342  1.1.2.2  skrll 	}
   6343  1.1.2.2  skrll 	sctppcbinfo.ipi_count_chunk++;
   6344  1.1.2.2  skrll 	sctppcbinfo.ipi_gencnt_chunk++;
   6345  1.1.2.2  skrll 
   6346  1.1.2.2  skrll 	chk->data = m_asconf;
   6347  1.1.2.2  skrll 	chk->send_size = m_asconf->m_pkthdr.len;
   6348  1.1.2.2  skrll 	chk->rec.chunk_id = SCTP_ASCONF;
   6349  1.1.2.2  skrll 	chk->sent = SCTP_DATAGRAM_UNSENT;
   6350  1.1.2.2  skrll 	chk->snd_count = 0;
   6351  1.1.2.2  skrll 	chk->flags = 0;
   6352  1.1.2.2  skrll 	chk->asoc = &stcb->asoc;
   6353  1.1.2.2  skrll 	chk->whoTo = chk->asoc->primary_destination;
   6354  1.1.2.2  skrll 	chk->whoTo->ref_count++;
   6355  1.1.2.2  skrll 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
   6356  1.1.2.2  skrll 	chk->asoc->ctrl_queue_cnt++;
   6357  1.1.2.2  skrll 	return (0);
   6358  1.1.2.2  skrll }
   6359  1.1.2.2  skrll 
   6360  1.1.2.2  skrll int
   6361  1.1.2.2  skrll sctp_send_asconf_ack(struct sctp_tcb *stcb, uint32_t retrans)
   6362  1.1.2.2  skrll {
   6363  1.1.2.2  skrll 	/*
   6364  1.1.2.2  skrll 	 * formulate and queue a asconf-ack back to sender
   6365  1.1.2.2  skrll 	 * the asconf-ack must be stored in the tcb
   6366  1.1.2.2  skrll 	 */
   6367  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   6368  1.1.2.2  skrll 	struct mbuf *m_ack;
   6369  1.1.2.2  skrll 
   6370  1.1.2.2  skrll 	/* is there a asconf-ack mbuf chain to send? */
   6371  1.1.2.2  skrll 	if (stcb->asoc.last_asconf_ack_sent == NULL) {
   6372  1.1.2.2  skrll 		return (-1);
   6373  1.1.2.2  skrll 	}
   6374  1.1.2.2  skrll 
   6375  1.1.2.2  skrll 	/* copy the asconf_ack */
   6376  1.1.2.2  skrll #if defined(__FreeBSD__) || defined(__NetBSD__)
   6377  1.1.2.2  skrll 	/* Supposedly the m_copypacket is a optimzation,
   6378  1.1.2.2  skrll 	 * use it if we can.
   6379  1.1.2.2  skrll 	 */
   6380  1.1.2.2  skrll 	if (stcb->asoc.last_asconf_ack_sent->m_flags & M_PKTHDR) {
   6381  1.1.2.2  skrll 		m_ack = m_copypacket(stcb->asoc.last_asconf_ack_sent, M_DONTWAIT);
   6382  1.1.2.2  skrll 		sctp_pegs[SCTP_CACHED_SRC]++;
   6383  1.1.2.2  skrll 	} else
   6384  1.1.2.2  skrll 		m_ack = m_copy(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL);
   6385  1.1.2.2  skrll #else
   6386  1.1.2.2  skrll 		m_ack = m_copy(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL);
   6387  1.1.2.2  skrll #endif
   6388  1.1.2.2  skrll 	if (m_ack == NULL) {
   6389  1.1.2.2  skrll 		/* couldn't copy it */
   6390  1.1.2.2  skrll 
   6391  1.1.2.2  skrll 		return (-1);
   6392  1.1.2.2  skrll 	}
   6393  1.1.2.2  skrll 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   6394  1.1.2.2  skrll 	if (chk == NULL) {
   6395  1.1.2.2  skrll 		/* no memory */
   6396  1.1.2.2  skrll 		if (m_ack)
   6397  1.1.2.2  skrll 			sctp_m_freem(m_ack);
   6398  1.1.2.2  skrll 		return (-1);
   6399  1.1.2.2  skrll 	}
   6400  1.1.2.2  skrll 	sctppcbinfo.ipi_count_chunk++;
   6401  1.1.2.2  skrll 	sctppcbinfo.ipi_gencnt_chunk++;
   6402  1.1.2.2  skrll 
   6403  1.1.2.2  skrll 	/* figure out where it goes to */
   6404  1.1.2.2  skrll 	if (retrans) {
   6405  1.1.2.2  skrll 		/* we're doing a retransmission */
   6406  1.1.2.2  skrll 		if (stcb->asoc.used_alt_asconfack > 2) {
   6407  1.1.2.2  skrll 			/* tried alternate nets already, go back */
   6408  1.1.2.2  skrll 			chk->whoTo = NULL;
   6409  1.1.2.2  skrll 		} else {
   6410  1.1.2.2  skrll 			/* need to try and alternate net */
   6411  1.1.2.2  skrll 			chk->whoTo = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from);
   6412  1.1.2.2  skrll 			stcb->asoc.used_alt_asconfack++;
   6413  1.1.2.2  skrll 		}
   6414  1.1.2.2  skrll 		if (chk->whoTo == NULL) {
   6415  1.1.2.2  skrll 			/* no alternate */
   6416  1.1.2.2  skrll 			if (stcb->asoc.last_control_chunk_from == NULL)
   6417  1.1.2.2  skrll 				chk->whoTo = stcb->asoc.primary_destination;
   6418  1.1.2.2  skrll 			else
   6419  1.1.2.2  skrll 				chk->whoTo = stcb->asoc.last_control_chunk_from;
   6420  1.1.2.2  skrll 			stcb->asoc.used_alt_asconfack = 0;
   6421  1.1.2.2  skrll 		}
   6422  1.1.2.2  skrll 	} else {
   6423  1.1.2.2  skrll 		/* normal case */
   6424  1.1.2.2  skrll 		if (stcb->asoc.last_control_chunk_from == NULL)
   6425  1.1.2.2  skrll 			chk->whoTo = stcb->asoc.primary_destination;
   6426  1.1.2.2  skrll 		else
   6427  1.1.2.2  skrll 			chk->whoTo = stcb->asoc.last_control_chunk_from;
   6428  1.1.2.2  skrll 		stcb->asoc.used_alt_asconfack = 0;
   6429  1.1.2.2  skrll 	}
   6430  1.1.2.2  skrll 	chk->data = m_ack;
   6431  1.1.2.2  skrll 	chk->send_size = m_ack->m_pkthdr.len;
   6432  1.1.2.2  skrll 	chk->rec.chunk_id = SCTP_ASCONF_ACK;
   6433  1.1.2.2  skrll 	chk->sent = SCTP_DATAGRAM_UNSENT;
   6434  1.1.2.2  skrll 	chk->snd_count = 0;
   6435  1.1.2.2  skrll 	chk->flags = 0;
   6436  1.1.2.2  skrll 	chk->asoc = &stcb->asoc;
   6437  1.1.2.2  skrll 	chk->whoTo->ref_count++;
   6438  1.1.2.2  skrll 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
   6439  1.1.2.2  skrll 	chk->asoc->ctrl_queue_cnt++;
   6440  1.1.2.2  skrll 	return (0);
   6441  1.1.2.2  skrll }
   6442  1.1.2.2  skrll 
   6443  1.1.2.2  skrll 
   6444  1.1.2.2  skrll static int
   6445  1.1.2.2  skrll sctp_chunk_retransmission(struct sctp_inpcb *inp,
   6446  1.1.2.2  skrll 			  struct sctp_tcb *stcb,
   6447  1.1.2.2  skrll 			  struct sctp_association *asoc,
   6448  1.1.2.2  skrll 			  int *cnt_out, struct timeval *now, int *now_filled)
   6449  1.1.2.2  skrll {
   6450  1.1.2.2  skrll 	/*
   6451  1.1.2.2  skrll 	 * send out one MTU of retransmission.
   6452  1.1.2.2  skrll 	 * If fast_retransmit is happening we ignore the cwnd.
   6453  1.1.2.2  skrll 	 * Otherwise we obey the cwnd and rwnd.
   6454  1.1.2.2  skrll 	 * For a Cookie or Asconf in the control chunk queue we retransmit
   6455  1.1.2.2  skrll 	 * them by themselves.
   6456  1.1.2.2  skrll 	 *
   6457  1.1.2.2  skrll 	 * For data chunks we will pick out the lowest TSN's in the
   6458  1.1.2.2  skrll 	 * sent_queue marked for resend and bundle them all together
   6459  1.1.2.2  skrll 	 * (up to a MTU of destination). The address to send to should
   6460  1.1.2.2  skrll 	 * have been selected/changed where the retransmission was
   6461  1.1.2.2  skrll 	 * marked (i.e. in FR or t3-timeout routines).
   6462  1.1.2.2  skrll 	 */
   6463  1.1.2.2  skrll 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
   6464  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk, *fwd;
   6465  1.1.2.2  skrll 	struct mbuf *m;
   6466  1.1.2.2  skrll 	struct sctphdr *shdr;
   6467  1.1.2.2  skrll 	int asconf;
   6468  1.1.2.2  skrll 	struct sctp_nets *net;
   6469  1.1.2.2  skrll 	int no_fragmentflg, bundle_at, cnt_thru;
   6470  1.1.2.2  skrll 	unsigned int mtu;
   6471  1.1.2.2  skrll 	int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
   6472  1.1.2.2  skrll 
   6473  1.1.2.2  skrll 	tmr_started = ctl_cnt = bundle_at =  error = 0;
   6474  1.1.2.2  skrll 	no_fragmentflg = 1;
   6475  1.1.2.2  skrll 	asconf = 0;
   6476  1.1.2.2  skrll 	fwd_tsn = 0;
   6477  1.1.2.2  skrll 	*cnt_out = 0;
   6478  1.1.2.2  skrll 	fwd = NULL;
   6479  1.1.2.2  skrll 	m = NULL;
   6480  1.1.2.2  skrll #ifdef SCTP_AUDITING_ENABLED
   6481  1.1.2.2  skrll 	sctp_audit_log(0xC3, 1);
   6482  1.1.2.2  skrll #endif
   6483  1.1.2.2  skrll 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
   6484  1.1.2.2  skrll #ifdef SCTP_DEBUG
   6485  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   6486  1.1.2.2  skrll 			printf("SCTP hits empty queue with cnt set to %d?\n",
   6487  1.1.2.2  skrll 			       asoc->sent_queue_retran_cnt);
   6488  1.1.2.2  skrll 		}
   6489  1.1.2.2  skrll #endif
   6490  1.1.2.2  skrll 		asoc->sent_queue_cnt = 0;
   6491  1.1.2.2  skrll 		asoc->sent_queue_cnt_removeable = 0;
   6492  1.1.2.2  skrll 	}
   6493  1.1.2.2  skrll 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
   6494  1.1.2.2  skrll 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
   6495  1.1.2.2  skrll 			/* we only worry about things marked for resend */
   6496  1.1.2.2  skrll 			continue;
   6497  1.1.2.2  skrll 		}
   6498  1.1.2.2  skrll 		if ((chk->rec.chunk_id == SCTP_COOKIE_ECHO) ||
   6499  1.1.2.2  skrll 		    (chk->rec.chunk_id == SCTP_ASCONF) ||
   6500  1.1.2.2  skrll 		    (chk->rec.chunk_id == SCTP_STREAM_RESET) ||
   6501  1.1.2.2  skrll 		    (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN)) {
   6502  1.1.2.2  skrll 			if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
   6503  1.1.2.2  skrll 				/* For stream reset we only retran the request
   6504  1.1.2.2  skrll 				 * not the response.
   6505  1.1.2.2  skrll 				 */
   6506  1.1.2.2  skrll 				struct sctp_stream_reset_req *strreq;
   6507  1.1.2.2  skrll 				strreq = mtod(chk->data, struct sctp_stream_reset_req *);
   6508  1.1.2.2  skrll 				if (strreq->sr_req.ph.param_type != ntohs(SCTP_STR_RESET_REQUEST)) {
   6509  1.1.2.2  skrll 					continue;
   6510  1.1.2.2  skrll 				}
   6511  1.1.2.2  skrll 			}
   6512  1.1.2.2  skrll 			ctl_cnt++;
   6513  1.1.2.2  skrll 			if (chk->rec.chunk_id == SCTP_ASCONF) {
   6514  1.1.2.2  skrll 				no_fragmentflg = 1;
   6515  1.1.2.2  skrll 				asconf = 1;
   6516  1.1.2.2  skrll 			}
   6517  1.1.2.2  skrll 			if (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN) {
   6518  1.1.2.2  skrll 				fwd_tsn = 1;
   6519  1.1.2.2  skrll 				fwd = chk;
   6520  1.1.2.2  skrll 			}
   6521  1.1.2.2  skrll 			m = sctp_copy_mbufchain(chk->data, m);
   6522  1.1.2.2  skrll 			break;
   6523  1.1.2.2  skrll 		}
   6524  1.1.2.2  skrll 	}
   6525  1.1.2.2  skrll 	one_chunk = 0;
   6526  1.1.2.2  skrll 	cnt_thru = 0;
   6527  1.1.2.2  skrll 	/* do we have control chunks to retransmit? */
   6528  1.1.2.2  skrll 	if (m != NULL) {
   6529  1.1.2.2  skrll 		/* Start a timer no matter if we suceed or fail */
   6530  1.1.2.2  skrll 		if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
   6531  1.1.2.2  skrll 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
   6532  1.1.2.2  skrll 		} else if (chk->rec.chunk_id == SCTP_ASCONF)
   6533  1.1.2.2  skrll 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
   6534  1.1.2.2  skrll 
   6535  1.1.2.2  skrll 		if (m->m_len == 0) {
   6536  1.1.2.2  skrll 			/* Special case for when you get a 0 len
   6537  1.1.2.2  skrll 			 * mbuf at the head due to the lack
   6538  1.1.2.2  skrll 			 * of a MHDR at the beginning.
   6539  1.1.2.2  skrll 			 */
   6540  1.1.2.2  skrll 			m->m_len = sizeof(struct sctphdr);
   6541  1.1.2.2  skrll 		} else {
   6542  1.1.2.2  skrll 			M_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
   6543  1.1.2.2  skrll 			if (m == NULL) {
   6544  1.1.2.2  skrll 				return (ENOBUFS);
   6545  1.1.2.2  skrll 			}
   6546  1.1.2.2  skrll 		}
   6547  1.1.2.2  skrll 		shdr = mtod(m, struct sctphdr *);
   6548  1.1.2.2  skrll 		shdr->src_port = inp->sctp_lport;
   6549  1.1.2.2  skrll 		shdr->dest_port = stcb->rport;
   6550  1.1.2.2  skrll 		shdr->v_tag = htonl(stcb->asoc.peer_vtag);
   6551  1.1.2.2  skrll 		shdr->checksum = 0;
   6552  1.1.2.2  skrll 		chk->snd_count++;		/* update our count */
   6553  1.1.2.2  skrll 
   6554  1.1.2.2  skrll 		if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
   6555  1.1.2.2  skrll 		    rtcache_getdst(&chk->whoTo->ro), m,
   6556  1.1.2.2  skrll 		    no_fragmentflg, 0, NULL, asconf))) {
   6557  1.1.2.2  skrll 			sctp_pegs[SCTP_DATA_OUT_ERR]++;
   6558  1.1.2.2  skrll 			return (error);
   6559  1.1.2.2  skrll 		}
   6560  1.1.2.2  skrll 		/*
   6561  1.1.2.2  skrll 		 *We don't want to mark the net->sent time here since this
   6562  1.1.2.2  skrll 		 * we use this for HB and retrans cannot measure RTT
   6563  1.1.2.2  skrll 		 */
   6564  1.1.2.2  skrll 		/*    SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time);*/
   6565  1.1.2.2  skrll 		*cnt_out += 1;
   6566  1.1.2.2  skrll 		chk->sent = SCTP_DATAGRAM_SENT;
   6567  1.1.2.2  skrll 		sctp_ucount_decr(asoc->sent_queue_retran_cnt);
   6568  1.1.2.2  skrll 		if (fwd_tsn == 0) {
   6569  1.1.2.2  skrll 			return (0);
   6570  1.1.2.2  skrll 		} else {
   6571  1.1.2.2  skrll 			/* Clean up the fwd-tsn list */
   6572  1.1.2.2  skrll 			sctp_clean_up_ctl (asoc);
   6573  1.1.2.2  skrll 			return (0);
   6574  1.1.2.2  skrll 		}
   6575  1.1.2.2  skrll 	}
   6576  1.1.2.2  skrll 	/* Ok, it is just data retransmission we need to do or
   6577  1.1.2.2  skrll 	 * that and a fwd-tsn with it all.
   6578  1.1.2.2  skrll 	 */
   6579  1.1.2.2  skrll 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
   6580  1.1.2.2  skrll 		return (-1);
   6581  1.1.2.2  skrll 	}
   6582  1.1.2.2  skrll #ifdef SCTP_DEBUG
   6583  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   6584  1.1.2.2  skrll 		printf("Normal chunk retransmission cnt:%d\n",
   6585  1.1.2.2  skrll 		       asoc->sent_queue_retran_cnt);
   6586  1.1.2.2  skrll 	}
   6587  1.1.2.2  skrll #endif
   6588  1.1.2.2  skrll 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
   6589  1.1.2.2  skrll 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
   6590  1.1.2.2  skrll 		/* not yet open, resend the cookie and that is it */
   6591  1.1.2.2  skrll 		return (1);
   6592  1.1.2.2  skrll 	}
   6593  1.1.2.2  skrll 
   6594  1.1.2.2  skrll 
   6595  1.1.2.2  skrll #ifdef SCTP_AUDITING_ENABLED
   6596  1.1.2.2  skrll 	sctp_auditing(20, inp, stcb, NULL);
   6597  1.1.2.2  skrll #endif
   6598  1.1.2.2  skrll 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
   6599  1.1.2.2  skrll 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
   6600  1.1.2.2  skrll 			/* No, not sent to this net or not ready for rtx */
   6601  1.1.2.2  skrll 			continue;
   6602  1.1.2.2  skrll 
   6603  1.1.2.2  skrll 		}
   6604  1.1.2.2  skrll 		/* pick up the net */
   6605  1.1.2.2  skrll 		net = chk->whoTo;
   6606  1.1.2.2  skrll 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   6607  1.1.2.2  skrll 			mtu = (net->mtu - SCTP_MIN_OVERHEAD);
   6608  1.1.2.2  skrll 		} else {
   6609  1.1.2.2  skrll 			mtu = net->mtu- SCTP_MIN_V4_OVERHEAD;
   6610  1.1.2.2  skrll 		}
   6611  1.1.2.2  skrll 
   6612  1.1.2.2  skrll 		if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
   6613  1.1.2.2  skrll 			/* No room in peers rwnd */
   6614  1.1.2.2  skrll 			uint32_t tsn;
   6615  1.1.2.2  skrll 			tsn = asoc->last_acked_seq + 1;
   6616  1.1.2.2  skrll 			if (tsn == chk->rec.data.TSN_seq) {
   6617  1.1.2.2  skrll 				/* we make a special exception for this case.
   6618  1.1.2.2  skrll 				 * The peer has no rwnd but is missing the
   6619  1.1.2.2  skrll 				 * lowest chunk.. which is probably what is
   6620  1.1.2.2  skrll 				 * holding up the rwnd.
   6621  1.1.2.2  skrll 				 */
   6622  1.1.2.2  skrll 				goto one_chunk_around;
   6623  1.1.2.2  skrll 			}
   6624  1.1.2.2  skrll #ifdef SCTP_DEBUG
   6625  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   6626  1.1.2.2  skrll 				printf("blocked-peers_rwnd:%d tf:%d\n",
   6627  1.1.2.2  skrll 				       (int)asoc->peers_rwnd,
   6628  1.1.2.2  skrll 				       (int)asoc->total_flight);
   6629  1.1.2.2  skrll 			}
   6630  1.1.2.2  skrll #endif
   6631  1.1.2.2  skrll 			sctp_pegs[SCTP_RWND_BLOCKED]++;
   6632  1.1.2.2  skrll 			return (1);
   6633  1.1.2.2  skrll 		}
   6634  1.1.2.2  skrll 	one_chunk_around:
   6635  1.1.2.2  skrll 		if (asoc->peers_rwnd < mtu) {
   6636  1.1.2.2  skrll 			one_chunk = 1;
   6637  1.1.2.2  skrll 		}
   6638  1.1.2.2  skrll #ifdef SCTP_AUDITING_ENABLED
   6639  1.1.2.2  skrll 		sctp_audit_log(0xC3, 2);
   6640  1.1.2.2  skrll #endif
   6641  1.1.2.2  skrll 		bundle_at = 0;
   6642  1.1.2.2  skrll 		m = NULL;
   6643  1.1.2.2  skrll 		net->fast_retran_ip = 0;
   6644  1.1.2.2  skrll 		if (chk->rec.data.doing_fast_retransmit == 0) {
   6645  1.1.2.2  skrll 			/* if no FR in progress skip destination that
   6646  1.1.2.2  skrll 			 * have flight_size > cwnd.
   6647  1.1.2.2  skrll 			 */
   6648  1.1.2.2  skrll 			if (net->flight_size >= net->cwnd) {
   6649  1.1.2.2  skrll 				sctp_pegs[SCTP_CWND_BLOCKED]++;
   6650  1.1.2.2  skrll 				continue;
   6651  1.1.2.2  skrll 			}
   6652  1.1.2.2  skrll 		} else {
   6653  1.1.2.2  skrll 			/* Mark the destination net to have FR recovery
   6654  1.1.2.2  skrll 			 * limits put on it.
   6655  1.1.2.2  skrll 			 */
   6656  1.1.2.2  skrll 			net->fast_retran_ip = 1;
   6657  1.1.2.2  skrll 		}
   6658  1.1.2.2  skrll 
   6659  1.1.2.2  skrll 		if ((chk->send_size <= mtu) || (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
   6660  1.1.2.2  skrll 			/* ok we will add this one */
   6661  1.1.2.2  skrll 			m = sctp_copy_mbufchain(chk->data, m);
   6662  1.1.2.2  skrll 			if (m == NULL) {
   6663  1.1.2.2  skrll 				return (ENOMEM);
   6664  1.1.2.2  skrll 			}
   6665  1.1.2.2  skrll 			/* upate our MTU size */
   6666  1.1.2.2  skrll 			/* Do clear IP_DF ? */
   6667  1.1.2.2  skrll 			if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
   6668  1.1.2.2  skrll 				no_fragmentflg = 0;
   6669  1.1.2.2  skrll 			}
   6670  1.1.2.2  skrll 			mtu -= chk->send_size;
   6671  1.1.2.2  skrll 			data_list[bundle_at++] = chk;
   6672  1.1.2.2  skrll 			if (one_chunk && (asoc->total_flight <= 0)) {
   6673  1.1.2.2  skrll 				sctp_pegs[SCTP_WINDOW_PROBES]++;
   6674  1.1.2.2  skrll 				chk->rec.data.state_flags |= SCTP_WINDOW_PROBE;
   6675  1.1.2.2  skrll 			}
   6676  1.1.2.2  skrll 		}
   6677  1.1.2.2  skrll 		if (one_chunk == 0) {
   6678  1.1.2.2  skrll 			/* now are there anymore forward from chk to pick up?*/
   6679  1.1.2.2  skrll 			fwd = TAILQ_NEXT(chk, sctp_next);
   6680  1.1.2.2  skrll 			while (fwd) {
   6681  1.1.2.2  skrll 				if (fwd->sent != SCTP_DATAGRAM_RESEND) {
   6682  1.1.2.2  skrll 					/* Nope, not for retran */
   6683  1.1.2.2  skrll 					fwd = TAILQ_NEXT(fwd, sctp_next);
   6684  1.1.2.2  skrll 					continue;
   6685  1.1.2.2  skrll 				}
   6686  1.1.2.2  skrll 				if (fwd->whoTo != net) {
   6687  1.1.2.2  skrll 					/* Nope, not the net in question */
   6688  1.1.2.2  skrll 					fwd = TAILQ_NEXT(fwd, sctp_next);
   6689  1.1.2.2  skrll 					continue;
   6690  1.1.2.2  skrll 				}
   6691  1.1.2.2  skrll 				if (fwd->send_size <= mtu) {
   6692  1.1.2.2  skrll 					m = sctp_copy_mbufchain(fwd->data, m);
   6693  1.1.2.2  skrll 					if (m == NULL) {
   6694  1.1.2.2  skrll 						return (ENOMEM);
   6695  1.1.2.2  skrll 					}
   6696  1.1.2.2  skrll 					/* upate our MTU size */
   6697  1.1.2.2  skrll 					/* Do clear IP_DF ? */
   6698  1.1.2.2  skrll 					if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
   6699  1.1.2.2  skrll 						no_fragmentflg = 0;
   6700  1.1.2.2  skrll 					}
   6701  1.1.2.2  skrll 					mtu -= fwd->send_size;
   6702  1.1.2.2  skrll 					data_list[bundle_at++] = fwd;
   6703  1.1.2.2  skrll 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
   6704  1.1.2.2  skrll 						break;
   6705  1.1.2.2  skrll 					}
   6706  1.1.2.2  skrll 					fwd = TAILQ_NEXT(fwd, sctp_next);
   6707  1.1.2.2  skrll 				} else {
   6708  1.1.2.2  skrll 					/* can't fit so we are done */
   6709  1.1.2.2  skrll 					break;
   6710  1.1.2.2  skrll 				}
   6711  1.1.2.2  skrll 			}
   6712  1.1.2.2  skrll 		}
   6713  1.1.2.2  skrll 		/* Is there something to send for this destination? */
   6714  1.1.2.2  skrll 		if (m) {
   6715  1.1.2.2  skrll 			/* No matter if we fail/or suceed we should
   6716  1.1.2.2  skrll 			 * start a timer. A failure is like a lost
   6717  1.1.2.2  skrll 			 * IP packet :-)
   6718  1.1.2.2  skrll 			 */
   6719  1.1.2.2  skrll 			if (!callout_pending(&net->rxt_timer.timer)) {
   6720  1.1.2.2  skrll 				/* no timer running on this destination
   6721  1.1.2.2  skrll 				 * restart it.
   6722  1.1.2.2  skrll 				 */
   6723  1.1.2.2  skrll 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
   6724  1.1.2.2  skrll 				tmr_started = 1;
   6725  1.1.2.2  skrll 			}
   6726  1.1.2.2  skrll 			if (m->m_len == 0) {
   6727  1.1.2.2  skrll 				/* Special case for when you get a 0 len
   6728  1.1.2.2  skrll 				 * mbuf at the head due to the lack
   6729  1.1.2.2  skrll 				 * of a MHDR at the beginning.
   6730  1.1.2.2  skrll 				 */
   6731  1.1.2.2  skrll 				m->m_len = sizeof(struct sctphdr);
   6732  1.1.2.2  skrll 			} else {
   6733  1.1.2.2  skrll 				M_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
   6734  1.1.2.2  skrll 				if (m == NULL) {
   6735  1.1.2.2  skrll 					return (ENOBUFS);
   6736  1.1.2.2  skrll 				}
   6737  1.1.2.2  skrll 			}
   6738  1.1.2.2  skrll 			shdr = mtod(m, struct sctphdr *);
   6739  1.1.2.2  skrll 			shdr->src_port = inp->sctp_lport;
   6740  1.1.2.2  skrll 			shdr->dest_port = stcb->rport;
   6741  1.1.2.2  skrll 			shdr->v_tag = htonl(stcb->asoc.peer_vtag);
   6742  1.1.2.2  skrll 			shdr->checksum = 0;
   6743  1.1.2.2  skrll 
   6744  1.1.2.2  skrll 			/* Now lets send it, if there is anything to send :> */
   6745  1.1.2.2  skrll 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
   6746  1.1.2.2  skrll 							       rtcache_getdst(&net->ro),
   6747  1.1.2.2  skrll 							       m,
   6748  1.1.2.2  skrll 							       no_fragmentflg, 0, NULL, asconf))) {
   6749  1.1.2.2  skrll 				/* error, we could not output */
   6750  1.1.2.2  skrll 				sctp_pegs[SCTP_DATA_OUT_ERR]++;
   6751  1.1.2.2  skrll 				return (error);
   6752  1.1.2.2  skrll 			}
   6753  1.1.2.2  skrll 			/* For HB's */
   6754  1.1.2.2  skrll 			/*
   6755  1.1.2.2  skrll 			 * We don't want to mark the net->sent time here since
   6756  1.1.2.2  skrll 			 * this we use this for HB and retrans cannot measure
   6757  1.1.2.2  skrll 			 * RTT
   6758  1.1.2.2  skrll 			 */
   6759  1.1.2.2  skrll 			/*      SCTP_GETTIME_TIMEVAL(&net->last_sent_time);*/
   6760  1.1.2.2  skrll 
   6761  1.1.2.2  skrll 			/* For auto-close */
   6762  1.1.2.2  skrll 			cnt_thru++;
   6763  1.1.2.2  skrll 			if (*now_filled == 0) {
   6764  1.1.2.2  skrll 				SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
   6765  1.1.2.2  skrll 				*now = asoc->time_last_sent;
   6766  1.1.2.2  skrll 				*now_filled = 1;
   6767  1.1.2.2  skrll 			} else {
   6768  1.1.2.2  skrll 				asoc->time_last_sent = *now;
   6769  1.1.2.2  skrll 			}
   6770  1.1.2.2  skrll 			*cnt_out += bundle_at;
   6771  1.1.2.2  skrll #ifdef SCTP_AUDITING_ENABLED
   6772  1.1.2.2  skrll 			sctp_audit_log(0xC4, bundle_at);
   6773  1.1.2.2  skrll #endif
   6774  1.1.2.2  skrll 			for (i = 0; i < bundle_at; i++) {
   6775  1.1.2.2  skrll 				sctp_pegs[SCTP_RETRANTSN_SENT]++;
   6776  1.1.2.2  skrll 				data_list[i]->sent = SCTP_DATAGRAM_SENT;
   6777  1.1.2.2  skrll 				data_list[i]->snd_count++;
   6778  1.1.2.2  skrll 				sctp_ucount_decr(asoc->sent_queue_retran_cnt);
   6779  1.1.2.2  skrll 				/* record the time */
   6780  1.1.2.2  skrll 				data_list[i]->sent_rcv_time = asoc->time_last_sent;
   6781  1.1.2.2  skrll 				net->flight_size += data_list[i]->book_size;
   6782  1.1.2.2  skrll 				asoc->total_flight += data_list[i]->book_size;
   6783  1.1.2.2  skrll 				asoc->total_flight_count++;
   6784  1.1.2.2  skrll 
   6785  1.1.2.2  skrll #ifdef SCTP_LOG_RWND
   6786  1.1.2.2  skrll 				sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
   6787  1.1.2.2  skrll 					      asoc->peers_rwnd , data_list[i]->send_size, sctp_peer_chunk_oh);
   6788  1.1.2.2  skrll #endif
   6789  1.1.2.2  skrll 				asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
   6790  1.1.2.2  skrll 								    (u_int32_t)(data_list[i]->send_size + sctp_peer_chunk_oh));
   6791  1.1.2.2  skrll 				if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
   6792  1.1.2.2  skrll 					/* SWS sender side engages */
   6793  1.1.2.2  skrll 					asoc->peers_rwnd = 0;
   6794  1.1.2.2  skrll 				}
   6795  1.1.2.2  skrll 
   6796  1.1.2.2  skrll 				if ((i == 0) &&
   6797  1.1.2.2  skrll 				    (data_list[i]->rec.data.doing_fast_retransmit)) {
   6798  1.1.2.2  skrll 					sctp_pegs[SCTP_FAST_RETRAN]++;
   6799  1.1.2.2  skrll 					if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
   6800  1.1.2.2  skrll 					    (tmr_started == 0)) {
   6801  1.1.2.2  skrll 						/*
   6802  1.1.2.2  skrll 						 * ok we just fast-retrans'd
   6803  1.1.2.2  skrll 						 * the lowest TSN, i.e the
   6804  1.1.2.2  skrll 						 * first on the list. In this
   6805  1.1.2.2  skrll 						 * case we want to give some
   6806  1.1.2.2  skrll 						 * more time to get a SACK
   6807  1.1.2.2  skrll 						 * back without a t3-expiring.
   6808  1.1.2.2  skrll 						 */
   6809  1.1.2.2  skrll 						sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
   6810  1.1.2.2  skrll 						sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
   6811  1.1.2.2  skrll 					}
   6812  1.1.2.2  skrll 				}
   6813  1.1.2.2  skrll 			}
   6814  1.1.2.2  skrll #ifdef SCTP_AUDITING_ENABLED
   6815  1.1.2.2  skrll 			sctp_auditing(21, inp, stcb, NULL);
   6816  1.1.2.2  skrll #endif
   6817  1.1.2.2  skrll 		} else {
   6818  1.1.2.2  skrll 			/* None will fit */
   6819  1.1.2.2  skrll 			return (1);
   6820  1.1.2.2  skrll 		}
   6821  1.1.2.2  skrll 		if (asoc->sent_queue_retran_cnt <= 0) {
   6822  1.1.2.2  skrll 			/* all done we have no more to retran */
   6823  1.1.2.2  skrll 			asoc->sent_queue_retran_cnt = 0;
   6824  1.1.2.2  skrll 			break;
   6825  1.1.2.2  skrll 		}
   6826  1.1.2.2  skrll 		if (one_chunk) {
   6827  1.1.2.2  skrll 			/* No more room in rwnd */
   6828  1.1.2.2  skrll 			return (1);
   6829  1.1.2.2  skrll 		}
   6830  1.1.2.2  skrll 		/* stop the for loop here. we sent out a packet */
   6831  1.1.2.2  skrll 		break;
   6832  1.1.2.2  skrll 	}
   6833  1.1.2.2  skrll 	return (0);
   6834  1.1.2.2  skrll }
   6835  1.1.2.2  skrll 
   6836  1.1.2.2  skrll 
   6837  1.1.2.2  skrll static int
   6838  1.1.2.2  skrll sctp_timer_validation(struct sctp_inpcb *inp,
   6839  1.1.2.2  skrll 		      struct sctp_tcb *stcb,
   6840  1.1.2.2  skrll 		      struct sctp_association *asoc,
   6841  1.1.2.2  skrll 		      int ret)
   6842  1.1.2.2  skrll {
   6843  1.1.2.2  skrll 	struct sctp_nets *net;
   6844  1.1.2.2  skrll 	/* Validate that a timer is running somewhere */
   6845  1.1.2.2  skrll 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   6846  1.1.2.2  skrll 		if (callout_pending(&net->rxt_timer.timer)) {
   6847  1.1.2.2  skrll 			/* Here is a timer */
   6848  1.1.2.2  skrll 			return (ret);
   6849  1.1.2.2  skrll 		}
   6850  1.1.2.2  skrll 	}
   6851  1.1.2.2  skrll 	/* Gak, we did not have a timer somewhere */
   6852  1.1.2.2  skrll #ifdef SCTP_DEBUG
   6853  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   6854  1.1.2.2  skrll 		printf("Deadlock avoided starting timer on a dest at retran\n");
   6855  1.1.2.2  skrll 	}
   6856  1.1.2.2  skrll #endif
   6857  1.1.2.2  skrll 	sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
   6858  1.1.2.2  skrll 	return (ret);
   6859  1.1.2.2  skrll }
   6860  1.1.2.2  skrll 
   6861  1.1.2.2  skrll int
   6862  1.1.2.2  skrll sctp_chunk_output(struct sctp_inpcb *inp,
   6863  1.1.2.2  skrll 		  struct sctp_tcb *stcb,
   6864  1.1.2.2  skrll 		  int from_where)
   6865  1.1.2.2  skrll {
   6866  1.1.2.2  skrll 	/* Ok this is the generic chunk service queue.
   6867  1.1.2.2  skrll 	 * we must do the following:
   6868  1.1.2.2  skrll 	 *  - See if there are retransmits pending, if so we
   6869  1.1.2.2  skrll 	 *   	must do these first and return.
   6870  1.1.2.2  skrll 	 *  - Service the stream queue that is next,
   6871  1.1.2.2  skrll 	 *    moving any message (note I must get a complete
   6872  1.1.2.2  skrll 	 *    message i.e. FIRST/MIDDLE and LAST to the out
   6873  1.1.2.2  skrll 	 *    queue in one pass) and assigning TSN's
   6874  1.1.2.2  skrll 	 *  - Check to see if the cwnd/rwnd allows any output, if
   6875  1.1.2.2  skrll 	 *	so we go ahead and fomulate and send the low level
   6876  1.1.2.2  skrll 	 *    chunks. Making sure to combine any control in the
   6877  1.1.2.2  skrll 	 *    control chunk queue also.
   6878  1.1.2.2  skrll 	 */
   6879  1.1.2.2  skrll 	struct sctp_association *asoc;
   6880  1.1.2.2  skrll 	struct sctp_nets *net;
   6881  1.1.2.2  skrll 	int error, num_out, tot_out, ret, reason_code, burst_cnt, burst_limit;
   6882  1.1.2.2  skrll 	struct timeval now;
   6883  1.1.2.2  skrll 	int now_filled=0;
   6884  1.1.2.2  skrll 	int cwnd_full=0;
   6885  1.1.2.2  skrll 	asoc = &stcb->asoc;
   6886  1.1.2.2  skrll 	tot_out = 0;
   6887  1.1.2.2  skrll 	num_out = 0;
   6888  1.1.2.2  skrll 	reason_code = 0;
   6889  1.1.2.2  skrll 	sctp_pegs[SCTP_CALLS_TO_CO]++;
   6890  1.1.2.2  skrll #ifdef SCTP_DEBUG
   6891  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   6892  1.1.2.2  skrll 		printf("in co - retran count:%d\n", asoc->sent_queue_retran_cnt);
   6893  1.1.2.2  skrll 	}
   6894  1.1.2.2  skrll #endif
   6895  1.1.2.2  skrll 	while (asoc->sent_queue_retran_cnt) {
   6896  1.1.2.2  skrll 		/* Ok, it is retransmission time only, we send out only ONE
   6897  1.1.2.2  skrll 		 * packet with a single call off to the retran code.
   6898  1.1.2.2  skrll 		 */
   6899  1.1.2.2  skrll 		ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled);
   6900  1.1.2.2  skrll 		if (ret > 0) {
   6901  1.1.2.2  skrll 			/* Can't send anymore */
   6902  1.1.2.2  skrll #ifdef SCTP_DEBUG
   6903  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   6904  1.1.2.2  skrll 				printf("retransmission ret:%d -- full\n", ret);
   6905  1.1.2.2  skrll 			}
   6906  1.1.2.2  skrll #endif
   6907  1.1.2.2  skrll 			/*
   6908  1.1.2.2  skrll 			 * now lets push out control by calling med-level
   6909  1.1.2.2  skrll 			 * output once. this assures that we WILL send HB's
   6910  1.1.2.2  skrll 			 * if queued too.
   6911  1.1.2.2  skrll 			 */
   6912  1.1.2.2  skrll 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
   6913  1.1.2.2  skrll 						    &cwnd_full, from_where,
   6914  1.1.2.2  skrll 						    &now, &now_filled);
   6915  1.1.2.2  skrll #ifdef SCTP_DEBUG
   6916  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   6917  1.1.2.2  skrll 				printf("Control send outputs:%d@full\n", num_out);
   6918  1.1.2.2  skrll 			}
   6919  1.1.2.2  skrll #endif
   6920  1.1.2.2  skrll #ifdef SCTP_AUDITING_ENABLED
   6921  1.1.2.2  skrll 			sctp_auditing(8, inp, stcb, NULL);
   6922  1.1.2.2  skrll #endif
   6923  1.1.2.2  skrll 			return (sctp_timer_validation(inp, stcb, asoc, ret));
   6924  1.1.2.2  skrll 		}
   6925  1.1.2.2  skrll 		if (ret < 0) {
   6926  1.1.2.2  skrll 			/*
   6927  1.1.2.2  skrll 			 * The count was off.. retran is not happening so do
   6928  1.1.2.2  skrll 			 * the normal retransmission.
   6929  1.1.2.2  skrll 			 */
   6930  1.1.2.2  skrll #ifdef SCTP_DEBUG
   6931  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   6932  1.1.2.2  skrll 				printf("Done with retrans, none left fill up window\n");
   6933  1.1.2.2  skrll 			}
   6934  1.1.2.2  skrll #endif
   6935  1.1.2.2  skrll #ifdef SCTP_AUDITING_ENABLED
   6936  1.1.2.2  skrll 			sctp_auditing(9, inp, stcb, NULL);
   6937  1.1.2.2  skrll #endif
   6938  1.1.2.2  skrll 			break;
   6939  1.1.2.2  skrll 		}
   6940  1.1.2.2  skrll 		if (from_where == 1) {
   6941  1.1.2.2  skrll 			/* Only one transmission allowed out of a timeout */
   6942  1.1.2.2  skrll #ifdef SCTP_DEBUG
   6943  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   6944  1.1.2.2  skrll 				printf("Only one packet allowed out\n");
   6945  1.1.2.2  skrll 			}
   6946  1.1.2.2  skrll #endif
   6947  1.1.2.2  skrll #ifdef SCTP_AUDITING_ENABLED
   6948  1.1.2.2  skrll 			sctp_auditing(10, inp, stcb, NULL);
   6949  1.1.2.2  skrll #endif
   6950  1.1.2.2  skrll 			/* Push out any control */
   6951  1.1.2.2  skrll 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
   6952  1.1.2.2  skrll 						    &now, &now_filled);
   6953  1.1.2.2  skrll 			return (ret);
   6954  1.1.2.2  skrll 		}
   6955  1.1.2.2  skrll 		if ((num_out == 0) && (ret == 0)) {
   6956  1.1.2.2  skrll 			/* No more retrans to send */
   6957  1.1.2.2  skrll 			break;
   6958  1.1.2.2  skrll 		}
   6959  1.1.2.2  skrll 	}
   6960  1.1.2.2  skrll #ifdef SCTP_AUDITING_ENABLED
   6961  1.1.2.2  skrll 	sctp_auditing(12, inp, stcb, NULL);
   6962  1.1.2.2  skrll #endif
   6963  1.1.2.2  skrll 	/* Check for bad destinations, if they exist move chunks around. */
   6964  1.1.2.2  skrll 	burst_limit = asoc->max_burst;
   6965  1.1.2.2  skrll 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   6966  1.1.2.2  skrll 		if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
   6967  1.1.2.2  skrll 		    SCTP_ADDR_NOT_REACHABLE) {
   6968  1.1.2.2  skrll 			/*
   6969  1.1.2.2  skrll 			 * if possible move things off of this address
   6970  1.1.2.2  skrll 			 * we still may send below due to the dormant state
   6971  1.1.2.2  skrll 			 * but we try to find an alternate address to send
   6972  1.1.2.2  skrll 			 * to and if we have one we move all queued data on
   6973  1.1.2.2  skrll 			 * the out wheel to this alternate address.
   6974  1.1.2.2  skrll 			 */
   6975  1.1.2.2  skrll 			sctp_move_to_an_alt(stcb, asoc, net);
   6976  1.1.2.2  skrll 		} else {
   6977  1.1.2.2  skrll 			/*
   6978  1.1.2.2  skrll 			if ((asoc->sat_network) || (net->addr_is_local)) {
   6979  1.1.2.2  skrll 				burst_limit = asoc->max_burst * SCTP_SAT_NETWORK_BURST_INCR;
   6980  1.1.2.2  skrll 			}
   6981  1.1.2.2  skrll 			*/
   6982  1.1.2.2  skrll #ifdef SCTP_DEBUG
   6983  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   6984  1.1.2.2  skrll 				printf("examined net:%p burst limit:%d\n", net, asoc->max_burst);
   6985  1.1.2.2  skrll 			}
   6986  1.1.2.2  skrll #endif
   6987  1.1.2.2  skrll 
   6988  1.1.2.2  skrll #ifdef SCTP_USE_ALLMAN_BURST
   6989  1.1.2.2  skrll 			if ((net->flight_size+(burst_limit*net->mtu)) < net->cwnd) {
   6990  1.1.2.2  skrll 				if (net->ssthresh < net->cwnd)
   6991  1.1.2.2  skrll 					net->ssthresh = net->cwnd;
   6992  1.1.2.2  skrll 				net->cwnd = (net->flight_size+(burst_limit*net->mtu));
   6993  1.1.2.2  skrll #ifdef SCTP_LOG_MAXBURST
   6994  1.1.2.2  skrll 				sctp_log_maxburst(net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
   6995  1.1.2.2  skrll #endif
   6996  1.1.2.2  skrll 				sctp_pegs[SCTP_MAX_BURST_APL]++;
   6997  1.1.2.2  skrll 			}
   6998  1.1.2.2  skrll 			net->fast_retran_ip = 0;
   6999  1.1.2.2  skrll #endif
   7000  1.1.2.2  skrll 		}
   7001  1.1.2.2  skrll 
   7002  1.1.2.2  skrll 	}
   7003  1.1.2.2  skrll 	/* Fill up what we can to the destination */
   7004  1.1.2.2  skrll 	burst_cnt = 0;
   7005  1.1.2.2  skrll 	cwnd_full = 0;
   7006  1.1.2.2  skrll 	do {
   7007  1.1.2.2  skrll #ifdef SCTP_DEBUG
   7008  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   7009  1.1.2.2  skrll 			printf("Burst count:%d - call m-c-o\n", burst_cnt);
   7010  1.1.2.2  skrll 		}
   7011  1.1.2.2  skrll #endif
   7012  1.1.2.2  skrll 		error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
   7013  1.1.2.2  skrll 					      &reason_code, 0,  &cwnd_full, from_where,
   7014  1.1.2.2  skrll 					      &now, &now_filled);
   7015  1.1.2.2  skrll 		if (error) {
   7016  1.1.2.2  skrll #ifdef SCTP_DEBUG
   7017  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   7018  1.1.2.2  skrll 				printf("Error %d was returned from med-c-op\n", error);
   7019  1.1.2.2  skrll 			}
   7020  1.1.2.2  skrll #endif
   7021  1.1.2.2  skrll #ifdef SCTP_LOG_MAXBURST
   7022  1.1.2.2  skrll 			sctp_log_maxburst(asoc->primary_destination, error , burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
   7023  1.1.2.2  skrll #endif
   7024  1.1.2.2  skrll 			break;
   7025  1.1.2.2  skrll 		}
   7026  1.1.2.2  skrll #ifdef SCTP_DEBUG
   7027  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   7028  1.1.2.2  skrll 			printf("m-c-o put out %d\n", num_out);
   7029  1.1.2.2  skrll 		}
   7030  1.1.2.2  skrll #endif
   7031  1.1.2.2  skrll 		tot_out += num_out;
   7032  1.1.2.2  skrll 		burst_cnt++;
   7033  1.1.2.2  skrll 	} while (num_out
   7034  1.1.2.2  skrll #ifndef SCTP_USE_ALLMAN_BURST
   7035  1.1.2.2  skrll 		 &&  (burst_cnt < burst_limit)
   7036  1.1.2.2  skrll #endif
   7037  1.1.2.2  skrll 		);
   7038  1.1.2.2  skrll #ifndef SCTP_USE_ALLMAN_BURST
   7039  1.1.2.2  skrll 	if (burst_cnt >= burst_limit) {
   7040  1.1.2.2  skrll 		sctp_pegs[SCTP_MAX_BURST_APL]++;
   7041  1.1.2.2  skrll  		asoc->burst_limit_applied = 1;
   7042  1.1.2.2  skrll #ifdef SCTP_LOG_MAXBURST
   7043  1.1.2.2  skrll 		sctp_log_maxburst(asoc->primary_destination, 0 , burst_cnt, SCTP_MAX_BURST_APPLIED);
   7044  1.1.2.2  skrll #endif
   7045  1.1.2.2  skrll  	} else {
   7046  1.1.2.2  skrll 		asoc->burst_limit_applied = 0;
   7047  1.1.2.2  skrll  	}
   7048  1.1.2.2  skrll #endif
   7049  1.1.2.2  skrll 
   7050  1.1.2.2  skrll #ifdef SCTP_DEBUG
   7051  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   7052  1.1.2.2  skrll 		printf("Ok, we have put out %d chunks\n", tot_out);
   7053  1.1.2.2  skrll 	}
   7054  1.1.2.2  skrll #endif
   7055  1.1.2.2  skrll 	if (tot_out == 0) {
   7056  1.1.2.2  skrll 		sctp_pegs[SCTP_CO_NODATASNT]++;
   7057  1.1.2.2  skrll 		if (asoc->stream_queue_cnt > 0) {
   7058  1.1.2.2  skrll 			sctp_pegs[SCTP_SOS_NOSNT]++;
   7059  1.1.2.2  skrll 		} else {
   7060  1.1.2.2  skrll 			sctp_pegs[SCTP_NOS_NOSNT]++;
   7061  1.1.2.2  skrll 		}
   7062  1.1.2.2  skrll 		if (asoc->send_queue_cnt > 0) {
   7063  1.1.2.2  skrll 			sctp_pegs[SCTP_SOSE_NOSNT]++;
   7064  1.1.2.2  skrll 		} else {
   7065  1.1.2.2  skrll 			sctp_pegs[SCTP_NOSE_NOSNT]++;
   7066  1.1.2.2  skrll 		}
   7067  1.1.2.2  skrll 	}
   7068  1.1.2.2  skrll 	/* Now we need to clean up the control chunk chain if
   7069  1.1.2.2  skrll 	 * a ECNE is on it. It must be marked as UNSENT again
   7070  1.1.2.2  skrll 	 * so next call will continue to send it until
   7071  1.1.2.2  skrll 	 * such time that we get a CWR, to remove it.
   7072  1.1.2.2  skrll 	 */
   7073  1.1.2.2  skrll 	sctp_fix_ecn_echo(asoc);
   7074  1.1.2.2  skrll 	return (error);
   7075  1.1.2.2  skrll }
   7076  1.1.2.2  skrll 
   7077  1.1.2.2  skrll 
   7078  1.1.2.2  skrll int
   7079  1.1.2.2  skrll sctp_output(struct sctp_inpcb *inp, struct mbuf *m,
   7080  1.1.2.2  skrll      struct sockaddr *addr, struct mbuf *control, struct lwp *l, int flags)
   7081  1.1.2.2  skrll {
   7082  1.1.2.2  skrll 	struct sctp_inpcb *t_inp;
   7083  1.1.2.2  skrll  	struct sctp_tcb *stcb;
   7084  1.1.2.2  skrll 	struct sctp_nets *net;
   7085  1.1.2.2  skrll 	struct sctp_association *asoc;
   7086  1.1.2.2  skrll 	int create_lock_applied = 0;
   7087  1.1.2.2  skrll 	int queue_only, error = 0;
   7088  1.1.2.2  skrll 	struct sctp_sndrcvinfo srcv;
   7089  1.1.2.2  skrll 	int un_sent = 0;
   7090  1.1.2.2  skrll 	int use_rcvinfo = 0;
   7091  1.1.2.2  skrll 	t_inp = inp;
   7092  1.1.2.2  skrll 	/*  struct route ro;*/
   7093  1.1.2.2  skrll 
   7094  1.1.2.2  skrll 	queue_only = 0;
   7095  1.1.2.2  skrll 	stcb = NULL;
   7096  1.1.2.2  skrll 	asoc = NULL;
   7097  1.1.2.2  skrll 	net = NULL;
   7098  1.1.2.2  skrll 
   7099  1.1.2.2  skrll #ifdef SCTP_DEBUG
   7100  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   7101  1.1.2.2  skrll 		printf("USR Send BEGINS\n");
   7102  1.1.2.2  skrll 	}
   7103  1.1.2.2  skrll #endif
   7104  1.1.2.2  skrll 
   7105  1.1.2.2  skrll 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
   7106  1.1.2.2  skrll 	    (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
   7107  1.1.2.2  skrll 		/* The listner can NOT send */
   7108  1.1.2.2  skrll 		if (control) {
   7109  1.1.2.2  skrll 			sctppcbinfo.mbuf_track--;
   7110  1.1.2.2  skrll 			sctp_m_freem(control);
   7111  1.1.2.2  skrll 			control = NULL;
   7112  1.1.2.2  skrll 		}
   7113  1.1.2.2  skrll 		sctp_m_freem(m);
   7114  1.1.2.2  skrll 		return (EFAULT);
   7115  1.1.2.2  skrll 	}
   7116  1.1.2.2  skrll 	/* Can't allow a V6 address on a non-v6 socket */
   7117  1.1.2.2  skrll 	if (addr) {
   7118  1.1.2.2  skrll 		SCTP_ASOC_CREATE_LOCK(inp);
   7119  1.1.2.2  skrll 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
   7120  1.1.2.2  skrll 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
   7121  1.1.2.2  skrll 			/* Should I really unlock ? */
   7122  1.1.2.2  skrll 			SCTP_ASOC_CREATE_UNLOCK(inp);
   7123  1.1.2.2  skrll 			if (control) {
   7124  1.1.2.2  skrll 				sctppcbinfo.mbuf_track--;
   7125  1.1.2.2  skrll 				sctp_m_freem(control);
   7126  1.1.2.2  skrll 				control = NULL;
   7127  1.1.2.2  skrll 			}
   7128  1.1.2.2  skrll 			sctp_m_freem(m);
   7129  1.1.2.2  skrll 			return (EFAULT);
   7130  1.1.2.2  skrll 		}
   7131  1.1.2.2  skrll 		create_lock_applied = 1;
   7132  1.1.2.2  skrll 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
   7133  1.1.2.2  skrll 		    (addr->sa_family == AF_INET6)) {
   7134  1.1.2.2  skrll 			SCTP_ASOC_CREATE_UNLOCK(inp);
   7135  1.1.2.2  skrll 			if (control) {
   7136  1.1.2.2  skrll 				sctppcbinfo.mbuf_track--;
   7137  1.1.2.2  skrll 				sctp_m_freem(control);
   7138  1.1.2.2  skrll 				control = NULL;
   7139  1.1.2.2  skrll 			}
   7140  1.1.2.2  skrll 			sctp_m_freem(m);
   7141  1.1.2.2  skrll 			return (EINVAL);
   7142  1.1.2.2  skrll 		}
   7143  1.1.2.2  skrll 	}
   7144  1.1.2.2  skrll 	if (control) {
   7145  1.1.2.2  skrll 		sctppcbinfo.mbuf_track++;
   7146  1.1.2.2  skrll 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
   7147  1.1.2.2  skrll 				   sizeof(srcv))) {
   7148  1.1.2.2  skrll 			if (srcv.sinfo_flags & MSG_SENDALL) {
   7149  1.1.2.2  skrll 				/* its a sendall */
   7150  1.1.2.2  skrll 				sctppcbinfo.mbuf_track--;
   7151  1.1.2.2  skrll 				sctp_m_freem(control);
   7152  1.1.2.2  skrll 				if (create_lock_applied) {
   7153  1.1.2.2  skrll 					SCTP_ASOC_CREATE_UNLOCK(inp);
   7154  1.1.2.2  skrll 					create_lock_applied = 0;
   7155  1.1.2.2  skrll 				}
   7156  1.1.2.2  skrll 				return (sctp_sendall(inp, NULL, m, &srcv));
   7157  1.1.2.2  skrll 			}
   7158  1.1.2.2  skrll 			if (srcv.sinfo_assoc_id) {
   7159  1.1.2.2  skrll 				if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   7160  1.1.2.2  skrll 					SCTP_INP_RLOCK(inp);
   7161  1.1.2.2  skrll 					stcb = LIST_FIRST(&inp->sctp_asoc_list);
   7162  1.1.2.2  skrll 					if (stcb) {
   7163  1.1.2.2  skrll 						SCTP_TCB_LOCK(stcb);
   7164  1.1.2.2  skrll 					}
   7165  1.1.2.2  skrll 					SCTP_INP_RUNLOCK(inp);
   7166  1.1.2.2  skrll 
   7167  1.1.2.2  skrll 					if (stcb == NULL) {
   7168  1.1.2.2  skrll 						if (create_lock_applied) {
   7169  1.1.2.2  skrll 							SCTP_ASOC_CREATE_UNLOCK(inp);
   7170  1.1.2.2  skrll 							create_lock_applied = 0;
   7171  1.1.2.2  skrll 						}
   7172  1.1.2.2  skrll 						sctppcbinfo.mbuf_track--;
   7173  1.1.2.2  skrll 						sctp_m_freem(control);
   7174  1.1.2.2  skrll 						sctp_m_freem(m);
   7175  1.1.2.2  skrll 						return (ENOTCONN);
   7176  1.1.2.2  skrll 					}
   7177  1.1.2.2  skrll 					net = stcb->asoc.primary_destination;
   7178  1.1.2.2  skrll 				} else {
   7179  1.1.2.2  skrll 					stcb = sctp_findassociation_ep_asocid(inp, srcv.sinfo_assoc_id);
   7180  1.1.2.2  skrll 				}
   7181  1.1.2.2  skrll 				/*
   7182  1.1.2.2  skrll 				 * Question: Should I error here if the
   7183  1.1.2.2  skrll 
   7184  1.1.2.2  skrll 				 * assoc_id is no longer valid?
   7185  1.1.2.2  skrll 				 * i.e. I can't find it?
   7186  1.1.2.2  skrll 				 */
   7187  1.1.2.2  skrll 				if ((stcb) &&
   7188  1.1.2.2  skrll 				    (addr != NULL)) {
   7189  1.1.2.2  skrll 					/* Must locate the net structure */
   7190  1.1.2.2  skrll 					if (addr)
   7191  1.1.2.2  skrll 						net = sctp_findnet(stcb, addr);
   7192  1.1.2.2  skrll 				}
   7193  1.1.2.2  skrll 				if (net == NULL)
   7194  1.1.2.2  skrll 					net = stcb->asoc.primary_destination;
   7195  1.1.2.2  skrll 			}
   7196  1.1.2.2  skrll 			use_rcvinfo = 1;
   7197  1.1.2.2  skrll 		}
   7198  1.1.2.2  skrll 	}
   7199  1.1.2.2  skrll 	if (stcb == NULL) {
   7200  1.1.2.2  skrll 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   7201  1.1.2.2  skrll 			SCTP_INP_RLOCK(inp);
   7202  1.1.2.2  skrll 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   7203  1.1.2.2  skrll 			if (stcb) {
   7204  1.1.2.2  skrll 				SCTP_TCB_LOCK(stcb);
   7205  1.1.2.2  skrll 			}
   7206  1.1.2.2  skrll 			SCTP_INP_RUNLOCK(inp);
   7207  1.1.2.2  skrll 			if (stcb == NULL) {
   7208  1.1.2.2  skrll 				if (create_lock_applied) {
   7209  1.1.2.2  skrll 					SCTP_ASOC_CREATE_UNLOCK(inp);
   7210  1.1.2.2  skrll 					create_lock_applied = 0;
   7211  1.1.2.2  skrll 				}
   7212  1.1.2.2  skrll 				if (control) {
   7213  1.1.2.2  skrll 					sctppcbinfo.mbuf_track--;
   7214  1.1.2.2  skrll 					sctp_m_freem(control);
   7215  1.1.2.2  skrll 					control = NULL;
   7216  1.1.2.2  skrll 				}
   7217  1.1.2.2  skrll 				sctp_m_freem(m);
   7218  1.1.2.2  skrll 				return (ENOTCONN);
   7219  1.1.2.2  skrll 			}
   7220  1.1.2.2  skrll 			if (addr == NULL) {
   7221  1.1.2.2  skrll 				net = stcb->asoc.primary_destination;
   7222  1.1.2.2  skrll 			} else {
   7223  1.1.2.2  skrll 				net = sctp_findnet(stcb, addr);
   7224  1.1.2.2  skrll 				if (net == NULL) {
   7225  1.1.2.2  skrll 					net = stcb->asoc.primary_destination;
   7226  1.1.2.2  skrll 				}
   7227  1.1.2.2  skrll 			}
   7228  1.1.2.2  skrll 		} else {
   7229  1.1.2.2  skrll 			if (addr != NULL) {
   7230  1.1.2.2  skrll 				SCTP_INP_WLOCK(inp);
   7231  1.1.2.2  skrll 				SCTP_INP_INCR_REF(inp);
   7232  1.1.2.2  skrll 				SCTP_INP_WUNLOCK(inp);
   7233  1.1.2.2  skrll 				stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
   7234  1.1.2.2  skrll 				if (stcb == NULL) {
   7235  1.1.2.2  skrll 					SCTP_INP_WLOCK(inp);
   7236  1.1.2.2  skrll 					SCTP_INP_DECR_REF(inp);
   7237  1.1.2.2  skrll 					SCTP_INP_WUNLOCK(inp);
   7238  1.1.2.2  skrll 				}
   7239  1.1.2.2  skrll 			}
   7240  1.1.2.2  skrll 		}
   7241  1.1.2.2  skrll 	}
   7242  1.1.2.2  skrll 	if ((stcb == NULL) &&
   7243  1.1.2.2  skrll 	    (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
   7244  1.1.2.2  skrll 		if (control) {
   7245  1.1.2.2  skrll 			sctppcbinfo.mbuf_track--;
   7246  1.1.2.2  skrll 			sctp_m_freem(control);
   7247  1.1.2.2  skrll 			control = NULL;
   7248  1.1.2.2  skrll 		}
   7249  1.1.2.2  skrll 		if (create_lock_applied) {
   7250  1.1.2.2  skrll 			SCTP_ASOC_CREATE_UNLOCK(inp);
   7251  1.1.2.2  skrll 			create_lock_applied = 0;
   7252  1.1.2.2  skrll 		}
   7253  1.1.2.2  skrll 		sctp_m_freem(m);
   7254  1.1.2.2  skrll 		return (ENOTCONN);
   7255  1.1.2.2  skrll 	} else if ((stcb == NULL) &&
   7256  1.1.2.2  skrll 		   (addr == NULL)) {
   7257  1.1.2.2  skrll 		if (control) {
   7258  1.1.2.2  skrll 			sctppcbinfo.mbuf_track--;
   7259  1.1.2.2  skrll 			sctp_m_freem(control);
   7260  1.1.2.2  skrll 			control = NULL;
   7261  1.1.2.2  skrll 		}
   7262  1.1.2.2  skrll 		if (create_lock_applied) {
   7263  1.1.2.2  skrll 			SCTP_ASOC_CREATE_UNLOCK(inp);
   7264  1.1.2.2  skrll 			create_lock_applied = 0;
   7265  1.1.2.2  skrll 		}
   7266  1.1.2.2  skrll 		sctp_m_freem(m);
   7267  1.1.2.2  skrll 		return (ENOENT);
   7268  1.1.2.2  skrll 	} else if (stcb == NULL) {
   7269  1.1.2.2  skrll 		/* UDP mode, we must go ahead and start the INIT process */
   7270  1.1.2.2  skrll 		if ((use_rcvinfo) && (srcv.sinfo_flags & MSG_ABORT)) {
   7271  1.1.2.2  skrll 			/* Strange user to do this */
   7272  1.1.2.2  skrll 			if (control) {
   7273  1.1.2.2  skrll 				sctppcbinfo.mbuf_track--;
   7274  1.1.2.2  skrll 				sctp_m_freem(control);
   7275  1.1.2.2  skrll 				control = NULL;
   7276  1.1.2.2  skrll 			}
   7277  1.1.2.2  skrll 			if (create_lock_applied) {
   7278  1.1.2.2  skrll 				SCTP_ASOC_CREATE_UNLOCK(inp);
   7279  1.1.2.2  skrll 				create_lock_applied = 0;
   7280  1.1.2.2  skrll 			}
   7281  1.1.2.2  skrll 			sctp_m_freem(m);
   7282  1.1.2.2  skrll 			return (ENOENT);
   7283  1.1.2.2  skrll 		}
   7284  1.1.2.2  skrll 		stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
   7285  1.1.2.2  skrll 		if (stcb == NULL) {
   7286  1.1.2.2  skrll 			if (control) {
   7287  1.1.2.2  skrll 				sctppcbinfo.mbuf_track--;
   7288  1.1.2.2  skrll 				sctp_m_freem(control);
   7289  1.1.2.2  skrll 				control = NULL;
   7290  1.1.2.2  skrll 			}
   7291  1.1.2.2  skrll 			if (create_lock_applied) {
   7292  1.1.2.2  skrll 				SCTP_ASOC_CREATE_UNLOCK(inp);
   7293  1.1.2.2  skrll 				create_lock_applied = 0;
   7294  1.1.2.2  skrll 			}
   7295  1.1.2.2  skrll 			sctp_m_freem(m);
   7296  1.1.2.2  skrll 			return (error);
   7297  1.1.2.2  skrll 		}
   7298  1.1.2.2  skrll 		if (create_lock_applied) {
   7299  1.1.2.2  skrll 			SCTP_ASOC_CREATE_UNLOCK(inp);
   7300  1.1.2.2  skrll 			create_lock_applied = 0;
   7301  1.1.2.2  skrll 		} else {
   7302  1.1.2.2  skrll 			printf("Huh-1, create lock should have been applied!\n");
   7303  1.1.2.2  skrll 		}
   7304  1.1.2.2  skrll 		queue_only = 1;
   7305  1.1.2.2  skrll 		asoc = &stcb->asoc;
   7306  1.1.2.2  skrll 		asoc->state = SCTP_STATE_COOKIE_WAIT;
   7307  1.1.2.2  skrll 		SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
   7308  1.1.2.2  skrll 		if (control) {
   7309  1.1.2.2  skrll 			/* see if a init structure exists in cmsg headers */
   7310  1.1.2.2  skrll 			struct sctp_initmsg initm;
   7311  1.1.2.2  skrll 			int i;
   7312  1.1.2.2  skrll 			if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
   7313  1.1.2.2  skrll 					   sizeof(initm))) {
   7314  1.1.2.2  skrll 				/* we have an INIT override of the default */
   7315  1.1.2.2  skrll 				if (initm.sinit_max_attempts)
   7316  1.1.2.2  skrll 					asoc->max_init_times = initm.sinit_max_attempts;
   7317  1.1.2.2  skrll 				if (initm.sinit_num_ostreams)
   7318  1.1.2.2  skrll 					asoc->pre_open_streams = initm.sinit_num_ostreams;
   7319  1.1.2.2  skrll 				if (initm.sinit_max_instreams)
   7320  1.1.2.2  skrll 					asoc->max_inbound_streams = initm.sinit_max_instreams;
   7321  1.1.2.2  skrll 				if (initm.sinit_max_init_timeo)
   7322  1.1.2.2  skrll 					asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
   7323  1.1.2.2  skrll 			}
   7324  1.1.2.2  skrll 			if (asoc->streamoutcnt < asoc->pre_open_streams) {
   7325  1.1.2.2  skrll 				/* Default is NOT correct */
   7326  1.1.2.2  skrll #ifdef SCTP_DEBUG
   7327  1.1.2.2  skrll 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   7328  1.1.2.2  skrll 					printf("Ok, defout:%d pre_open:%d\n",
   7329  1.1.2.2  skrll 					       asoc->streamoutcnt, asoc->pre_open_streams);
   7330  1.1.2.2  skrll 				}
   7331  1.1.2.2  skrll #endif
   7332  1.1.2.2  skrll 				free(asoc->strmout, M_PCB);
   7333  1.1.2.2  skrll 				asoc->strmout = NULL;
   7334  1.1.2.2  skrll 				asoc->streamoutcnt = asoc->pre_open_streams;
   7335  1.1.2.2  skrll 				asoc->strmout = malloc(asoc->streamoutcnt *
   7336  1.1.2.2  skrll 				       sizeof(struct sctp_stream_out), M_PCB,
   7337  1.1.2.2  skrll 				       M_WAIT);
   7338  1.1.2.2  skrll 				for (i = 0; i < asoc->streamoutcnt; i++) {
   7339  1.1.2.2  skrll 					/*
   7340  1.1.2.2  skrll 					 * inbound side must be set to 0xffff,
   7341  1.1.2.2  skrll 					 * also NOTE when we get the INIT-ACK
   7342  1.1.2.2  skrll 					 * back (for INIT sender) we MUST
   7343  1.1.2.2  skrll 					 * reduce the count (streamoutcnt) but
   7344  1.1.2.2  skrll 					 * first check if we sent to any of the
   7345  1.1.2.2  skrll 					 * upper streams that were dropped (if
   7346  1.1.2.2  skrll 					 * some were). Those that were dropped
   7347  1.1.2.2  skrll 					 * must be notified to the upper layer
   7348  1.1.2.2  skrll 					 * as failed to send.
   7349  1.1.2.2  skrll 					 */
   7350  1.1.2.2  skrll 					asoc->strmout[i].next_sequence_sent = 0x0;
   7351  1.1.2.2  skrll 					TAILQ_INIT(&asoc->strmout[i].outqueue);
   7352  1.1.2.2  skrll 					asoc->strmout[i].stream_no = i;
   7353  1.1.2.2  skrll 					asoc->strmout[i].next_spoke.tqe_next = 0;
   7354  1.1.2.2  skrll 					asoc->strmout[i].next_spoke.tqe_prev = 0;
   7355  1.1.2.2  skrll 				}
   7356  1.1.2.2  skrll 			}
   7357  1.1.2.2  skrll 		}
   7358  1.1.2.2  skrll 		sctp_send_initiate(inp, stcb);
   7359  1.1.2.2  skrll 		/*
   7360  1.1.2.2  skrll 		 * we may want to dig in after this call and adjust the MTU
   7361  1.1.2.2  skrll 		 * value. It defaulted to 1500 (constant) but the ro structure
   7362  1.1.2.2  skrll 		 * may now have an update and thus we may need to change it
   7363  1.1.2.2  skrll 		 * BEFORE we append the message.
   7364  1.1.2.2  skrll 		 */
   7365  1.1.2.2  skrll 		net = stcb->asoc.primary_destination;
   7366  1.1.2.2  skrll 	} else {
   7367  1.1.2.2  skrll 		if (create_lock_applied) {
   7368  1.1.2.2  skrll 			SCTP_ASOC_CREATE_UNLOCK(inp);
   7369  1.1.2.2  skrll 			create_lock_applied = 0;
   7370  1.1.2.2  skrll 		}
   7371  1.1.2.2  skrll 		asoc = &stcb->asoc;
   7372  1.1.2.2  skrll 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
   7373  1.1.2.2  skrll 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
   7374  1.1.2.2  skrll 			queue_only = 1;
   7375  1.1.2.2  skrll 		}
   7376  1.1.2.2  skrll 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
   7377  1.1.2.2  skrll 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
   7378  1.1.2.2  skrll 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
   7379  1.1.2.2  skrll 		    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
   7380  1.1.2.2  skrll 			if (control) {
   7381  1.1.2.2  skrll 				sctppcbinfo.mbuf_track--;
   7382  1.1.2.2  skrll 				sctp_m_freem(control);
   7383  1.1.2.2  skrll 				control = NULL;
   7384  1.1.2.2  skrll 			}
   7385  1.1.2.2  skrll 			if ((use_rcvinfo) &&
   7386  1.1.2.2  skrll 			    (srcv.sinfo_flags & MSG_ABORT)) {
   7387  1.1.2.2  skrll 				sctp_msg_append(stcb, net, m, &srcv, flags);
   7388  1.1.2.2  skrll 				error = 0;
   7389  1.1.2.2  skrll 			} else {
   7390  1.1.2.2  skrll 				if (m)
   7391  1.1.2.2  skrll 					sctp_m_freem(m);
   7392  1.1.2.2  skrll 				error = ECONNRESET;
   7393  1.1.2.2  skrll 			}
   7394  1.1.2.2  skrll 			SCTP_TCB_UNLOCK(stcb);
   7395  1.1.2.2  skrll 			return (error);
   7396  1.1.2.2  skrll 		}
   7397  1.1.2.2  skrll 	}
   7398  1.1.2.2  skrll 	if (create_lock_applied) {
   7399  1.1.2.2  skrll 		/* we should never hit here with the create lock applied
   7400  1.1.2.2  skrll 		 *
   7401  1.1.2.2  skrll 		 */
   7402  1.1.2.2  skrll 		SCTP_ASOC_CREATE_UNLOCK(inp);
   7403  1.1.2.2  skrll 		create_lock_applied = 0;
   7404  1.1.2.2  skrll 	}
   7405  1.1.2.2  skrll 
   7406  1.1.2.2  skrll 
   7407  1.1.2.2  skrll 	if (use_rcvinfo == 0) {
   7408  1.1.2.2  skrll 		srcv = stcb->asoc.def_send;
   7409  1.1.2.2  skrll 	}
   7410  1.1.2.2  skrll #ifdef SCTP_DEBUG
   7411  1.1.2.2  skrll 	else {
   7412  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT5) {
   7413  1.1.2.2  skrll 			printf("stream:%d\n", srcv.sinfo_stream);
   7414  1.1.2.2  skrll 			printf("flags:%x\n", (u_int)srcv.sinfo_flags);
   7415  1.1.2.2  skrll 			printf("ppid:%d\n", srcv.sinfo_ppid);
   7416  1.1.2.2  skrll 			printf("context:%d\n", srcv.sinfo_context);
   7417  1.1.2.2  skrll 		}
   7418  1.1.2.2  skrll 	}
   7419  1.1.2.2  skrll #endif
   7420  1.1.2.2  skrll 	if (control) {
   7421  1.1.2.2  skrll 		sctppcbinfo.mbuf_track--;
   7422  1.1.2.2  skrll 		sctp_m_freem(control);
   7423  1.1.2.2  skrll 		control = NULL;
   7424  1.1.2.2  skrll 	}
   7425  1.1.2.2  skrll 	if (net && ((srcv.sinfo_flags & MSG_ADDR_OVER))) {
   7426  1.1.2.2  skrll 		/* we take the override or the unconfirmed */
   7427  1.1.2.2  skrll 		;
   7428  1.1.2.2  skrll 	} else {
   7429  1.1.2.2  skrll 		net = stcb->asoc.primary_destination;
   7430  1.1.2.2  skrll 	}
   7431  1.1.2.2  skrll 	if ((error = sctp_msg_append(stcb, net, m, &srcv, flags))) {
   7432  1.1.2.2  skrll 		SCTP_TCB_UNLOCK(stcb);
   7433  1.1.2.2  skrll 		return (error);
   7434  1.1.2.2  skrll 	}
   7435  1.1.2.2  skrll 	if (net->flight_size > net->cwnd) {
   7436  1.1.2.2  skrll 		sctp_pegs[SCTP_SENDTO_FULL_CWND]++;
   7437  1.1.2.2  skrll 		queue_only = 1;
   7438  1.1.2.2  skrll  	} else if (asoc->ifp_had_enobuf) {
   7439  1.1.2.2  skrll 		sctp_pegs[SCTP_QUEONLY_BURSTLMT]++;
   7440  1.1.2.2  skrll 	 	queue_only = 1;
   7441  1.1.2.2  skrll  	} else {
   7442  1.1.2.2  skrll 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
   7443  1.1.2.2  skrll 			   ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)) +
   7444  1.1.2.2  skrll 			   SCTP_MED_OVERHEAD);
   7445  1.1.2.2  skrll 
   7446  1.1.2.2  skrll 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY) == 0) &&
   7447  1.1.2.2  skrll 		    (stcb->asoc.total_flight > 0) &&
   7448  1.1.2.2  skrll 		    (un_sent < (int)stcb->asoc.smallest_mtu)
   7449  1.1.2.2  skrll 			) {
   7450  1.1.2.2  skrll 
   7451  1.1.2.2  skrll 			/* Ok, Nagle is set on and we have
   7452  1.1.2.2  skrll 			 * data outstanding. Don't send anything
   7453  1.1.2.2  skrll 			 * and let the SACK drive out the data.
   7454  1.1.2.2  skrll 			 */
   7455  1.1.2.2  skrll 			sctp_pegs[SCTP_NAGLE_NOQ]++;
   7456  1.1.2.2  skrll 			queue_only = 1;
   7457  1.1.2.2  skrll 		} else {
   7458  1.1.2.2  skrll 			sctp_pegs[SCTP_NAGLE_OFF]++;
   7459  1.1.2.2  skrll 		}
   7460  1.1.2.2  skrll 	}
   7461  1.1.2.2  skrll 	if ((queue_only == 0) && stcb->asoc.peers_rwnd) {
   7462  1.1.2.2  skrll 		/* we can attempt to send too.*/
   7463  1.1.2.2  skrll #ifdef SCTP_DEBUG
   7464  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   7465  1.1.2.2  skrll 			printf("USR Send calls sctp_chunk_output\n");
   7466  1.1.2.2  skrll 		}
   7467  1.1.2.2  skrll #endif
   7468  1.1.2.2  skrll #ifdef SCTP_AUDITING_ENABLED
   7469  1.1.2.2  skrll 		sctp_audit_log(0xC0, 1);
   7470  1.1.2.2  skrll 		sctp_auditing(6, inp, stcb, net);
   7471  1.1.2.2  skrll #endif
   7472  1.1.2.2  skrll 		sctp_pegs[SCTP_OUTPUT_FRM_SND]++;
   7473  1.1.2.2  skrll 		sctp_chunk_output(inp, stcb, 0);
   7474  1.1.2.2  skrll #ifdef SCTP_AUDITING_ENABLED
   7475  1.1.2.2  skrll 		sctp_audit_log(0xC0, 2);
   7476  1.1.2.2  skrll 		sctp_auditing(7, inp, stcb, net);
   7477  1.1.2.2  skrll #endif
   7478  1.1.2.2  skrll 
   7479  1.1.2.2  skrll 	}
   7480  1.1.2.2  skrll #ifdef SCTP_DEBUG
   7481  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   7482  1.1.2.2  skrll 		printf("USR Send complete qo:%d prw:%d\n", queue_only, stcb->asoc.peers_rwnd);
   7483  1.1.2.2  skrll 	}
   7484  1.1.2.2  skrll #endif
   7485  1.1.2.2  skrll 	SCTP_TCB_UNLOCK(stcb);
   7486  1.1.2.2  skrll 	return (0);
   7487  1.1.2.2  skrll }
   7488  1.1.2.2  skrll 
   7489  1.1.2.2  skrll void
   7490  1.1.2.2  skrll send_forward_tsn(struct sctp_tcb *stcb,
   7491  1.1.2.2  skrll 		 struct sctp_association *asoc)
   7492  1.1.2.2  skrll {
   7493  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   7494  1.1.2.2  skrll 	struct sctp_forward_tsn_chunk *fwdtsn;
   7495  1.1.2.2  skrll 
   7496  1.1.2.2  skrll 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
   7497  1.1.2.2  skrll 		if (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN) {
   7498  1.1.2.2  skrll 			/* mark it to unsent */
   7499  1.1.2.2  skrll 			chk->sent = SCTP_DATAGRAM_UNSENT;
   7500  1.1.2.2  skrll 			chk->snd_count = 0;
   7501  1.1.2.2  skrll 			/* Do we correct its output location? */
   7502  1.1.2.2  skrll 			if (chk->whoTo != asoc->primary_destination) {
   7503  1.1.2.2  skrll 				sctp_free_remote_addr(chk->whoTo);
   7504  1.1.2.2  skrll 				chk->whoTo = asoc->primary_destination;
   7505  1.1.2.2  skrll 				chk->whoTo->ref_count++;
   7506  1.1.2.2  skrll 			}
   7507  1.1.2.2  skrll 			goto sctp_fill_in_rest;
   7508  1.1.2.2  skrll 		}
   7509  1.1.2.2  skrll 	}
   7510  1.1.2.2  skrll 	/* Ok if we reach here we must build one */
   7511  1.1.2.2  skrll 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   7512  1.1.2.2  skrll 	if (chk == NULL) {
   7513  1.1.2.2  skrll 		return;
   7514  1.1.2.2  skrll 	}
   7515  1.1.2.2  skrll 	sctppcbinfo.ipi_count_chunk++;
   7516  1.1.2.2  skrll 	sctppcbinfo.ipi_gencnt_chunk++;
   7517  1.1.2.2  skrll 	chk->rec.chunk_id = SCTP_FORWARD_CUM_TSN;
   7518  1.1.2.2  skrll 	chk->asoc = asoc;
   7519  1.1.2.2  skrll 	MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
   7520  1.1.2.2  skrll 	if (chk->data == NULL) {
   7521  1.1.2.2  skrll 		chk->whoTo->ref_count--;
   7522  1.1.2.2  skrll 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   7523  1.1.2.2  skrll 		sctppcbinfo.ipi_count_chunk--;
   7524  1.1.2.2  skrll 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   7525  1.1.2.2  skrll 			panic("Chunk count is negative");
   7526  1.1.2.2  skrll 		}
   7527  1.1.2.2  skrll 		sctppcbinfo.ipi_gencnt_chunk++;
   7528  1.1.2.2  skrll 		return;
   7529  1.1.2.2  skrll 	}
   7530  1.1.2.2  skrll 	chk->data->m_data += SCTP_MIN_OVERHEAD;
   7531  1.1.2.2  skrll 	chk->sent = SCTP_DATAGRAM_UNSENT;
   7532  1.1.2.2  skrll 	chk->snd_count = 0;
   7533  1.1.2.2  skrll 	chk->whoTo = asoc->primary_destination;
   7534  1.1.2.2  skrll 	chk->whoTo->ref_count++;
   7535  1.1.2.2  skrll 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
   7536  1.1.2.2  skrll 	asoc->ctrl_queue_cnt++;
   7537  1.1.2.2  skrll  sctp_fill_in_rest:
   7538  1.1.2.2  skrll 	/* Here we go through and fill out the part that
   7539  1.1.2.2  skrll 	 * deals with stream/seq of the ones we skip.
   7540  1.1.2.2  skrll 	 */
   7541  1.1.2.2  skrll 	chk->data->m_pkthdr.len = chk->data->m_len = 0;
   7542  1.1.2.2  skrll 	{
   7543  1.1.2.2  skrll 		struct sctp_tmit_chunk *at, *tp1, *last;
   7544  1.1.2.2  skrll 		struct sctp_strseq *strseq;
   7545  1.1.2.2  skrll 		unsigned int cnt_of_space, i, ovh;
   7546  1.1.2.2  skrll 		unsigned int space_needed;
   7547  1.1.2.2  skrll 		unsigned int cnt_of_skipped = 0;
   7548  1.1.2.2  skrll 		TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
   7549  1.1.2.2  skrll 			if (at->sent != SCTP_FORWARD_TSN_SKIP) {
   7550  1.1.2.2  skrll 				/* no more to look at */
   7551  1.1.2.2  skrll 				break;
   7552  1.1.2.2  skrll 			}
   7553  1.1.2.2  skrll 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
   7554  1.1.2.2  skrll 				/* We don't report these */
   7555  1.1.2.2  skrll 				continue;
   7556  1.1.2.2  skrll 			}
   7557  1.1.2.2  skrll 			cnt_of_skipped++;
   7558  1.1.2.2  skrll 		}
   7559  1.1.2.2  skrll 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
   7560  1.1.2.2  skrll 				(cnt_of_skipped * sizeof(struct sctp_strseq)));
   7561  1.1.2.2  skrll 		if ((M_TRAILINGSPACE(chk->data) < (int)space_needed) &&
   7562  1.1.2.2  skrll 		    ((chk->data->m_flags & M_EXT) == 0)) {
   7563  1.1.2.2  skrll 			/* Need a M_EXT, get one and move
   7564  1.1.2.2  skrll 			 * fwdtsn to data area.
   7565  1.1.2.2  skrll 			 */
   7566  1.1.2.2  skrll 			MCLGET(chk->data, M_DONTWAIT);
   7567  1.1.2.2  skrll 		}
   7568  1.1.2.2  skrll 		cnt_of_space = M_TRAILINGSPACE(chk->data);
   7569  1.1.2.2  skrll 
   7570  1.1.2.2  skrll 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   7571  1.1.2.2  skrll 			ovh = SCTP_MIN_OVERHEAD;
   7572  1.1.2.2  skrll 		} else {
   7573  1.1.2.2  skrll 			ovh = SCTP_MIN_V4_OVERHEAD;
   7574  1.1.2.2  skrll 		}
   7575  1.1.2.2  skrll 		if (cnt_of_space > (asoc->smallest_mtu-ovh)) {
   7576  1.1.2.2  skrll 			/* trim to a mtu size */
   7577  1.1.2.2  skrll 			cnt_of_space = asoc->smallest_mtu - ovh;
   7578  1.1.2.2  skrll 		}
   7579  1.1.2.2  skrll 		if (cnt_of_space < space_needed) {
   7580  1.1.2.2  skrll 			/* ok we must trim down the chunk by lowering
   7581  1.1.2.2  skrll 			 * the advance peer ack point.
   7582  1.1.2.2  skrll 			 */
   7583  1.1.2.2  skrll 			cnt_of_skipped = (cnt_of_space-
   7584  1.1.2.2  skrll 					  ((sizeof(struct sctp_forward_tsn_chunk))/
   7585  1.1.2.2  skrll  					    sizeof(struct sctp_strseq)));
   7586  1.1.2.2  skrll 			/* Go through and find the TSN that
   7587  1.1.2.2  skrll 			 * will be the one we report.
   7588  1.1.2.2  skrll 			 */
   7589  1.1.2.2  skrll 			at = TAILQ_FIRST(&asoc->sent_queue);
   7590  1.1.2.2  skrll 			for (i = 0; i < cnt_of_skipped; i++) {
   7591  1.1.2.2  skrll 				tp1 = TAILQ_NEXT(at, sctp_next);
   7592  1.1.2.2  skrll 				at = tp1;
   7593  1.1.2.2  skrll 			}
   7594  1.1.2.2  skrll 			last = at;
   7595  1.1.2.2  skrll 			/* last now points to last one I can report, update peer ack point */
   7596  1.1.2.2  skrll 			asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
   7597  1.1.2.2  skrll 			space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
   7598  1.1.2.2  skrll 		}
   7599  1.1.2.2  skrll 		chk->send_size = space_needed;
   7600  1.1.2.2  skrll 		/* Setup the chunk */
   7601  1.1.2.2  skrll 		fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
   7602  1.1.2.2  skrll 		fwdtsn->ch.chunk_length = htons(chk->send_size);
   7603  1.1.2.2  skrll 		fwdtsn->ch.chunk_flags = 0;
   7604  1.1.2.2  skrll 		fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
   7605  1.1.2.2  skrll 		fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
   7606  1.1.2.2  skrll 		chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
   7607  1.1.2.2  skrll 				  (cnt_of_skipped * sizeof(struct sctp_strseq)));
   7608  1.1.2.2  skrll 		chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
   7609  1.1.2.2  skrll 		fwdtsn++;
   7610  1.1.2.2  skrll 		/* Move pointer to after the fwdtsn and transfer to
   7611  1.1.2.2  skrll 		 * the strseq pointer.
   7612  1.1.2.2  skrll 		 */
   7613  1.1.2.2  skrll 		strseq = (struct sctp_strseq *)fwdtsn;
   7614  1.1.2.2  skrll 		/*
   7615  1.1.2.2  skrll 		 * Now populate the strseq list. This is done blindly
   7616  1.1.2.2  skrll 		 * without pulling out duplicate stream info. This is
   7617  1.1.2.2  skrll 		 * inefficent but won't harm the process since the peer
   7618  1.1.2.2  skrll 		 * will look at these in sequence and will thus release
   7619  1.1.2.2  skrll 		 * anything. It could mean we exceed the PMTU and chop
   7620  1.1.2.2  skrll 		 * off some that we could have included.. but this is
   7621  1.1.2.2  skrll 		 * unlikely (aka 1432/4 would mean 300+ stream seq's would
   7622  1.1.2.2  skrll 		 * have to be reported in one FWD-TSN. With a bit of work
   7623  1.1.2.2  skrll 		 * we can later FIX this to optimize and pull out duplcates..
   7624  1.1.2.2  skrll 		 * but it does add more overhead. So for now... not!
   7625  1.1.2.2  skrll 		 */
   7626  1.1.2.2  skrll 		at = TAILQ_FIRST(&asoc->sent_queue);
   7627  1.1.2.2  skrll 		for (i = 0; i < cnt_of_skipped; i++) {
   7628  1.1.2.2  skrll 			tp1 = TAILQ_NEXT(at, sctp_next);
   7629  1.1.2.2  skrll 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
   7630  1.1.2.2  skrll 				/* We don't report these */
   7631  1.1.2.2  skrll 				i--;
   7632  1.1.2.2  skrll 				at = tp1;
   7633  1.1.2.2  skrll 				continue;
   7634  1.1.2.2  skrll 			}
   7635  1.1.2.2  skrll 			strseq->stream = ntohs(at->rec.data.stream_number);
   7636  1.1.2.2  skrll 			strseq->sequence = ntohs(at->rec.data.stream_seq);
   7637  1.1.2.2  skrll 			strseq++;
   7638  1.1.2.2  skrll 			at = tp1;
   7639  1.1.2.2  skrll 		}
   7640  1.1.2.2  skrll 	}
   7641  1.1.2.2  skrll 	return;
   7642  1.1.2.2  skrll 
   7643  1.1.2.2  skrll }
   7644  1.1.2.2  skrll 
   7645  1.1.2.2  skrll void
   7646  1.1.2.2  skrll sctp_send_sack(struct sctp_tcb *stcb)
   7647  1.1.2.2  skrll {
   7648  1.1.2.2  skrll 	/*
   7649  1.1.2.2  skrll 	 * Queue up a SACK in the control queue. We must first check to
   7650  1.1.2.2  skrll 	 * see if a SACK is somehow on the control queue. If so, we will
   7651  1.1.2.2  skrll 	 * take and and remove the old one.
   7652  1.1.2.2  skrll 	 */
   7653  1.1.2.2  skrll 	struct sctp_association *asoc;
   7654  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk, *a_chk;
   7655  1.1.2.2  skrll 	struct sctp_sack_chunk *sack;
   7656  1.1.2.2  skrll 	struct sctp_gap_ack_block *gap_descriptor;
   7657  1.1.2.2  skrll 	uint32_t *dup;
   7658  1.1.2.2  skrll 	int start;
   7659  1.1.2.2  skrll 	unsigned int i, maxi, seeing_ones, m_size;
   7660  1.1.2.2  skrll 	unsigned int num_gap_blocks, space;
   7661  1.1.2.2  skrll 
   7662  1.1.2.2  skrll 	start = maxi = 0;
   7663  1.1.2.2  skrll 	seeing_ones = 1;
   7664  1.1.2.2  skrll 	a_chk = NULL;
   7665  1.1.2.2  skrll 	asoc = &stcb->asoc;
   7666  1.1.2.2  skrll 	if (asoc->last_data_chunk_from == NULL) {
   7667  1.1.2.2  skrll 		/* Hmm we never received anything */
   7668  1.1.2.2  skrll 		return;
   7669  1.1.2.2  skrll 	}
   7670  1.1.2.2  skrll 	sctp_set_rwnd(stcb, asoc);
   7671  1.1.2.2  skrll 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
   7672  1.1.2.2  skrll 		if (chk->rec.chunk_id == SCTP_SELECTIVE_ACK) {
   7673  1.1.2.2  skrll 			/* Hmm, found a sack already on queue, remove it */
   7674  1.1.2.2  skrll 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
   7675  1.1.2.2  skrll 			asoc->ctrl_queue_cnt++;
   7676  1.1.2.2  skrll 			a_chk = chk;
   7677  1.1.2.2  skrll 			if (a_chk->data)
   7678  1.1.2.2  skrll 				sctp_m_freem(a_chk->data);
   7679  1.1.2.2  skrll 			a_chk->data = NULL;
   7680  1.1.2.2  skrll 			sctp_free_remote_addr(a_chk->whoTo);
   7681  1.1.2.2  skrll 			a_chk->whoTo = NULL;
   7682  1.1.2.2  skrll 			break;
   7683  1.1.2.2  skrll 		}
   7684  1.1.2.2  skrll 	}
   7685  1.1.2.2  skrll 	if (a_chk == NULL) {
   7686  1.1.2.2  skrll 		a_chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   7687  1.1.2.2  skrll 		if (a_chk == NULL) {
   7688  1.1.2.2  skrll 			/* No memory so we drop the idea, and set a timer */
   7689  1.1.2.2  skrll 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
   7690  1.1.2.2  skrll 					stcb->sctp_ep, stcb, NULL);
   7691  1.1.2.2  skrll 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
   7692  1.1.2.2  skrll 					 stcb->sctp_ep, stcb, NULL);
   7693  1.1.2.2  skrll 			return;
   7694  1.1.2.2  skrll 		}
   7695  1.1.2.2  skrll 		sctppcbinfo.ipi_count_chunk++;
   7696  1.1.2.2  skrll 		sctppcbinfo.ipi_gencnt_chunk++;
   7697  1.1.2.2  skrll 		a_chk->rec.chunk_id = SCTP_SELECTIVE_ACK;
   7698  1.1.2.2  skrll 	}
   7699  1.1.2.2  skrll 	a_chk->asoc = asoc;
   7700  1.1.2.2  skrll 	a_chk->snd_count = 0;
   7701  1.1.2.2  skrll 	a_chk->send_size = 0;	/* fill in later */
   7702  1.1.2.2  skrll 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
   7703  1.1.2.2  skrll 	m_size = (asoc->mapping_array_size << 3);
   7704  1.1.2.2  skrll 
   7705  1.1.2.2  skrll 	if ((asoc->numduptsns) ||
   7706  1.1.2.2  skrll 	    (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
   7707  1.1.2.2  skrll 		) {
   7708  1.1.2.2  skrll 		/* Ok, we have some duplicates or the destination for the
   7709  1.1.2.2  skrll 		 * sack is unreachable, lets see if we can select an alternate
   7710  1.1.2.2  skrll 		 * than asoc->last_data_chunk_from
   7711  1.1.2.2  skrll 		 */
   7712  1.1.2.2  skrll 		if ((!(asoc->last_data_chunk_from->dest_state &
   7713  1.1.2.2  skrll 		      SCTP_ADDR_NOT_REACHABLE)) &&
   7714  1.1.2.2  skrll 		    (asoc->used_alt_onsack > 2)) {
   7715  1.1.2.2  skrll 			/* We used an alt last time, don't this time */
   7716  1.1.2.2  skrll 			a_chk->whoTo = NULL;
   7717  1.1.2.2  skrll 		} else {
   7718  1.1.2.2  skrll 			asoc->used_alt_onsack++;
   7719  1.1.2.2  skrll 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from);
   7720  1.1.2.2  skrll 		}
   7721  1.1.2.2  skrll 		if (a_chk->whoTo == NULL) {
   7722  1.1.2.2  skrll 			/* Nope, no alternate */
   7723  1.1.2.2  skrll 			a_chk->whoTo = asoc->last_data_chunk_from;
   7724  1.1.2.2  skrll 			asoc->used_alt_onsack = 0;
   7725  1.1.2.2  skrll 		}
   7726  1.1.2.2  skrll 	} else {
   7727  1.1.2.2  skrll 		/* No duplicates so we use the last
   7728  1.1.2.2  skrll 		 * place we received data from.
   7729  1.1.2.2  skrll 		 */
   7730  1.1.2.2  skrll #ifdef SCTP_DEBUG
   7731  1.1.2.2  skrll 		if (asoc->last_data_chunk_from == NULL) {
   7732  1.1.2.2  skrll 			printf("Huh, last_data_chunk_from is null when we want to sack??\n");
   7733  1.1.2.2  skrll 		}
   7734  1.1.2.2  skrll #endif
   7735  1.1.2.2  skrll 		asoc->used_alt_onsack = 0;
   7736  1.1.2.2  skrll 		a_chk->whoTo = asoc->last_data_chunk_from;
   7737  1.1.2.2  skrll 	}
   7738  1.1.2.2  skrll 	if (a_chk->whoTo)
   7739  1.1.2.2  skrll 		a_chk->whoTo->ref_count++;
   7740  1.1.2.2  skrll 
   7741  1.1.2.2  skrll 	/* Ok now lets formulate a MBUF with our sack */
   7742  1.1.2.2  skrll 	MGETHDR(a_chk->data, M_DONTWAIT, MT_DATA);
   7743  1.1.2.2  skrll 	if ((a_chk->data == NULL) ||
   7744  1.1.2.2  skrll 	    (a_chk->whoTo == NULL)) {
   7745  1.1.2.2  skrll 		/* rats, no mbuf memory */
   7746  1.1.2.2  skrll 		if (a_chk->data) {
   7747  1.1.2.2  skrll 			/* was a problem with the destination */
   7748  1.1.2.2  skrll 			sctp_m_freem(a_chk->data);
   7749  1.1.2.2  skrll 			a_chk->data = NULL;
   7750  1.1.2.2  skrll 		}
   7751  1.1.2.2  skrll 		a_chk->whoTo->ref_count--;
   7752  1.1.2.2  skrll 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, a_chk);
   7753  1.1.2.2  skrll 		sctppcbinfo.ipi_count_chunk--;
   7754  1.1.2.2  skrll 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   7755  1.1.2.2  skrll 			panic("Chunk count is negative");
   7756  1.1.2.2  skrll 		}
   7757  1.1.2.2  skrll 		sctppcbinfo.ipi_gencnt_chunk++;
   7758  1.1.2.2  skrll 		sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
   7759  1.1.2.2  skrll 				stcb->sctp_ep, stcb, NULL);
   7760  1.1.2.2  skrll 		sctp_timer_start(SCTP_TIMER_TYPE_RECV,
   7761  1.1.2.2  skrll 				 stcb->sctp_ep, stcb, NULL);
   7762  1.1.2.2  skrll 		return;
   7763  1.1.2.2  skrll 	}
   7764  1.1.2.2  skrll 	/* First count the number of gap ack blocks we need */
   7765  1.1.2.2  skrll 	if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
   7766  1.1.2.2  skrll 		/* We know if there are none above the cum-ack we
   7767  1.1.2.2  skrll 		 * have everything with NO gaps
   7768  1.1.2.2  skrll 		 */
   7769  1.1.2.2  skrll 		num_gap_blocks = 0;
   7770  1.1.2.2  skrll 	} else {
   7771  1.1.2.2  skrll 		/* Ok we must count how many gaps we
   7772  1.1.2.2  skrll 		 * have.
   7773  1.1.2.2  skrll 		 */
   7774  1.1.2.2  skrll 		num_gap_blocks = 0;
   7775  1.1.2.2  skrll 		if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
   7776  1.1.2.2  skrll 			maxi = (asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn);
   7777  1.1.2.2  skrll 		} else {
   7778  1.1.2.2  skrll 			maxi = (asoc->highest_tsn_inside_map  + (MAX_TSN - asoc->mapping_array_base_tsn) + 1);
   7779  1.1.2.2  skrll 		}
   7780  1.1.2.2  skrll 		if (maxi > m_size) {
   7781  1.1.2.2  skrll 			/* impossible but who knows, someone is playing with us  :> */
   7782  1.1.2.2  skrll #ifdef SCTP_DEBUG
   7783  1.1.2.2  skrll 			printf("GAK maxi:%d  > m_size:%d came out higher than allowed htsn:%u base:%u cumack:%u\n",
   7784  1.1.2.2  skrll 			       maxi,
   7785  1.1.2.2  skrll 			       m_size,
   7786  1.1.2.2  skrll 			       asoc->highest_tsn_inside_map,
   7787  1.1.2.2  skrll 			       asoc->mapping_array_base_tsn,
   7788  1.1.2.2  skrll 			       asoc->cumulative_tsn
   7789  1.1.2.2  skrll 			       );
   7790  1.1.2.2  skrll #endif
   7791  1.1.2.2  skrll 			num_gap_blocks = 0;
   7792  1.1.2.2  skrll 			goto no_gaps_now;
   7793  1.1.2.2  skrll 		}
   7794  1.1.2.2  skrll 		if (asoc->cumulative_tsn >= asoc->mapping_array_base_tsn) {
   7795  1.1.2.2  skrll 			start = (asoc->cumulative_tsn - asoc->mapping_array_base_tsn);
   7796  1.1.2.2  skrll 		} else {
   7797  1.1.2.2  skrll 			/* Set it so we start at 0 */
   7798  1.1.2.2  skrll 			start = -1;
   7799  1.1.2.2  skrll 		}
   7800  1.1.2.2  skrll 		/* Ok move start up one to look at the NEXT past the cum-ack */
   7801  1.1.2.2  skrll 		start++;
   7802  1.1.2.2  skrll 		for (i = start; i <= maxi; i++) {
   7803  1.1.2.2  skrll 			if (seeing_ones) {
   7804  1.1.2.2  skrll 				/* while seeing ones I must
   7805  1.1.2.2  skrll 				 * transition back to 0 before
   7806  1.1.2.2  skrll 				 * finding the next gap and
   7807  1.1.2.2  skrll 				 * counting the segment.
   7808  1.1.2.2  skrll 				 */
   7809  1.1.2.2  skrll 				if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) == 0) {
   7810  1.1.2.2  skrll 					seeing_ones = 0;
   7811  1.1.2.2  skrll 				}
   7812  1.1.2.2  skrll 			} else {
   7813  1.1.2.2  skrll 				if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
   7814  1.1.2.2  skrll 					seeing_ones = 1;
   7815  1.1.2.2  skrll 					num_gap_blocks++;
   7816  1.1.2.2  skrll 				}
   7817  1.1.2.2  skrll 			}
   7818  1.1.2.2  skrll 		}
   7819  1.1.2.2  skrll 	no_gaps_now:
   7820  1.1.2.2  skrll 		if (num_gap_blocks == 0) {
   7821  1.1.2.2  skrll 			/*
   7822  1.1.2.2  skrll 			 * Traveled all of the bits and NO one,
   7823  1.1.2.2  skrll 			 * must have reneged
   7824  1.1.2.2  skrll 			 */
   7825  1.1.2.2  skrll 			if (compare_with_wrap(asoc->cumulative_tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
   7826  1.1.2.2  skrll 			   asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
   7827  1.1.2.2  skrll #ifdef SCTP_MAP_LOGGING
   7828  1.1.2.2  skrll 			   sctp_log_map(0, 4, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
   7829  1.1.2.2  skrll #endif
   7830  1.1.2.2  skrll 			}
   7831  1.1.2.2  skrll 		}
   7832  1.1.2.2  skrll 	}
   7833  1.1.2.2  skrll 
   7834  1.1.2.2  skrll 	/* Now calculate the space needed */
   7835  1.1.2.2  skrll 	space = (sizeof(struct sctp_sack_chunk) +
   7836  1.1.2.2  skrll 		 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
   7837  1.1.2.2  skrll 		 (asoc->numduptsns * sizeof(int32_t))
   7838  1.1.2.2  skrll 		);
   7839  1.1.2.2  skrll 	if (space > (asoc->smallest_mtu-SCTP_MAX_OVERHEAD)) {
   7840  1.1.2.2  skrll 		/* Reduce the size of the sack to fit */
   7841  1.1.2.2  skrll 		int calc, fit;
   7842  1.1.2.2  skrll 		calc = (asoc->smallest_mtu - SCTP_MAX_OVERHEAD);
   7843  1.1.2.2  skrll 		calc -= sizeof(struct sctp_gap_ack_block);
   7844  1.1.2.2  skrll 		fit = calc/sizeof(struct sctp_gap_ack_block);
   7845  1.1.2.2  skrll 		if (fit > (int)num_gap_blocks) {
   7846  1.1.2.2  skrll 			/* discard some dups */
   7847  1.1.2.2  skrll 			asoc->numduptsns = (fit - num_gap_blocks);
   7848  1.1.2.2  skrll 		} else {
   7849  1.1.2.2  skrll 			/* discard all dups and some gaps */
   7850  1.1.2.2  skrll 			num_gap_blocks = fit;
   7851  1.1.2.2  skrll 			asoc->numduptsns = 0;
   7852  1.1.2.2  skrll 		}
   7853  1.1.2.2  skrll 		/* recalc space */
   7854  1.1.2.2  skrll 		space = (sizeof(struct sctp_sack_chunk) +
   7855  1.1.2.2  skrll 			 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
   7856  1.1.2.2  skrll 			 (asoc->numduptsns * sizeof(int32_t))
   7857  1.1.2.2  skrll 			);
   7858  1.1.2.2  skrll 
   7859  1.1.2.2  skrll 	}
   7860  1.1.2.2  skrll 
   7861  1.1.2.2  skrll 	if ((space+SCTP_MIN_OVERHEAD) > MHLEN) {
   7862  1.1.2.2  skrll 		/* We need a cluster */
   7863  1.1.2.2  skrll 		MCLGET(a_chk->data, M_DONTWAIT);
   7864  1.1.2.2  skrll 		if ((a_chk->data->m_flags & M_EXT) != M_EXT) {
   7865  1.1.2.2  skrll 			/* can't get a cluster
   7866  1.1.2.2  skrll 			 * give up and try later.
   7867  1.1.2.2  skrll 			 */
   7868  1.1.2.2  skrll 			if (a_chk->data)
   7869  1.1.2.2  skrll 				sctp_m_freem(a_chk->data);
   7870  1.1.2.2  skrll 			a_chk->data = NULL;
   7871  1.1.2.2  skrll 			a_chk->whoTo->ref_count--;
   7872  1.1.2.2  skrll 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, a_chk);
   7873  1.1.2.2  skrll 			sctppcbinfo.ipi_count_chunk--;
   7874  1.1.2.2  skrll 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   7875  1.1.2.2  skrll 				panic("Chunk count is negative");
   7876  1.1.2.2  skrll 			}
   7877  1.1.2.2  skrll 			sctppcbinfo.ipi_gencnt_chunk++;
   7878  1.1.2.2  skrll 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
   7879  1.1.2.2  skrll 					stcb->sctp_ep, stcb, NULL);
   7880  1.1.2.2  skrll 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
   7881  1.1.2.2  skrll 					 stcb->sctp_ep, stcb, NULL);
   7882  1.1.2.2  skrll 			return;
   7883  1.1.2.2  skrll 		}
   7884  1.1.2.2  skrll 	}
   7885  1.1.2.2  skrll 
   7886  1.1.2.2  skrll 	/* ok, lets go through and fill it in */
   7887  1.1.2.2  skrll 	a_chk->data->m_data += SCTP_MIN_OVERHEAD;
   7888  1.1.2.2  skrll 	sack = mtod(a_chk->data, struct sctp_sack_chunk *);
   7889  1.1.2.2  skrll 	sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
   7890  1.1.2.2  skrll 	sack->ch.chunk_flags = asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM;
   7891  1.1.2.2  skrll 	sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
   7892  1.1.2.2  skrll 	sack->sack.a_rwnd = htonl(asoc->my_rwnd);
   7893  1.1.2.2  skrll 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
   7894  1.1.2.2  skrll 	sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
   7895  1.1.2.2  skrll 	sack->sack.num_dup_tsns = htons(asoc->numduptsns);
   7896  1.1.2.2  skrll 
   7897  1.1.2.2  skrll 	a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
   7898  1.1.2.2  skrll 			    (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
   7899  1.1.2.2  skrll 			    (asoc->numduptsns * sizeof(int32_t)));
   7900  1.1.2.2  skrll 	a_chk->data->m_pkthdr.len = a_chk->data->m_len = a_chk->send_size;
   7901  1.1.2.2  skrll 	sack->ch.chunk_length = htons(a_chk->send_size);
   7902  1.1.2.2  skrll 
   7903  1.1.2.2  skrll 	gap_descriptor = (struct sctp_gap_ack_block *)((vaddr_t)sack + sizeof(struct sctp_sack_chunk));
   7904  1.1.2.2  skrll 	seeing_ones = 0;
   7905  1.1.2.2  skrll 	for (i = start; i <= maxi; i++) {
   7906  1.1.2.2  skrll 		if (num_gap_blocks == 0) {
   7907  1.1.2.2  skrll 			break;
   7908  1.1.2.2  skrll 		}
   7909  1.1.2.2  skrll 		if (seeing_ones) {
   7910  1.1.2.2  skrll 			/* while seeing Ones I must
   7911  1.1.2.2  skrll 			 * transition back to 0 before
   7912  1.1.2.2  skrll 			 * finding the next gap
   7913  1.1.2.2  skrll 			 */
   7914  1.1.2.2  skrll 			if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) == 0) {
   7915  1.1.2.2  skrll 				gap_descriptor->end = htons(((uint16_t)(i-start)));
   7916  1.1.2.2  skrll 				gap_descriptor++;
   7917  1.1.2.2  skrll 				seeing_ones = 0;
   7918  1.1.2.2  skrll 				num_gap_blocks--;
   7919  1.1.2.2  skrll 			}
   7920  1.1.2.2  skrll 		} else {
   7921  1.1.2.2  skrll 			if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
   7922  1.1.2.2  skrll 				gap_descriptor->start = htons(((uint16_t)(i+1-start)));
   7923  1.1.2.2  skrll 				/* advance struct to next pointer */
   7924  1.1.2.2  skrll 				seeing_ones = 1;
   7925  1.1.2.2  skrll 			}
   7926  1.1.2.2  skrll 		}
   7927  1.1.2.2  skrll 	}
   7928  1.1.2.2  skrll 	if (num_gap_blocks) {
   7929  1.1.2.2  skrll 		/* special case where the array is all 1's
   7930  1.1.2.2  skrll 		 * to the end of the array.
   7931  1.1.2.2  skrll 		 */
   7932  1.1.2.2  skrll 		gap_descriptor->end = htons(((uint16_t)((i-start))));
   7933  1.1.2.2  skrll 		gap_descriptor++;
   7934  1.1.2.2  skrll 	}
   7935  1.1.2.2  skrll 	/* now we must add any dups we are going to report. */
   7936  1.1.2.2  skrll 	if (asoc->numduptsns) {
   7937  1.1.2.2  skrll 		dup = (uint32_t *)gap_descriptor;
   7938  1.1.2.2  skrll 		for (i = 0; i < asoc->numduptsns; i++) {
   7939  1.1.2.2  skrll 			*dup = htonl(asoc->dup_tsns[i]);
   7940  1.1.2.2  skrll 			dup++;
   7941  1.1.2.2  skrll 		}
   7942  1.1.2.2  skrll 		asoc->numduptsns = 0;
   7943  1.1.2.2  skrll 	}
   7944  1.1.2.2  skrll 	/* now that the chunk is prepared queue it to the control
   7945  1.1.2.2  skrll 	 * chunk queue.
   7946  1.1.2.2  skrll 	 */
   7947  1.1.2.2  skrll 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
   7948  1.1.2.2  skrll 	asoc->ctrl_queue_cnt++;
   7949  1.1.2.2  skrll 	sctp_pegs[SCTP_PEG_SACKS_SENT]++;
   7950  1.1.2.2  skrll 	return;
   7951  1.1.2.2  skrll }
   7952  1.1.2.2  skrll 
   7953  1.1.2.2  skrll void
   7954  1.1.2.2  skrll sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr)
   7955  1.1.2.2  skrll {
   7956  1.1.2.2  skrll 	struct mbuf *m_abort;
   7957  1.1.2.2  skrll 	struct sctp_abort_msg *abort_m;
   7958  1.1.2.2  skrll 	int sz;
   7959  1.1.2.2  skrll 	abort_m = NULL;
   7960  1.1.2.2  skrll 	MGETHDR(m_abort, M_DONTWAIT, MT_HEADER);
   7961  1.1.2.2  skrll 	if (m_abort == NULL) {
   7962  1.1.2.2  skrll 		/* no mbuf's */
   7963  1.1.2.2  skrll 		return;
   7964  1.1.2.2  skrll 	}
   7965  1.1.2.2  skrll 	m_abort->m_data += SCTP_MIN_OVERHEAD;
   7966  1.1.2.2  skrll 	abort_m = mtod(m_abort, struct sctp_abort_msg *);
   7967  1.1.2.2  skrll 	m_abort->m_len = sizeof(struct sctp_abort_msg);
   7968  1.1.2.2  skrll 	m_abort->m_next = operr;
   7969  1.1.2.2  skrll 	sz = 0;
   7970  1.1.2.2  skrll 	if (operr) {
   7971  1.1.2.2  skrll 		struct mbuf *n;
   7972  1.1.2.2  skrll 		n = operr;
   7973  1.1.2.2  skrll 		while (n) {
   7974  1.1.2.2  skrll 			sz += n->m_len;
   7975  1.1.2.2  skrll 			n = n->m_next;
   7976  1.1.2.2  skrll 		}
   7977  1.1.2.2  skrll 	}
   7978  1.1.2.2  skrll 	abort_m->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
   7979  1.1.2.2  skrll 	abort_m->msg.ch.chunk_flags = 0;
   7980  1.1.2.2  skrll 	abort_m->msg.ch.chunk_length = htons(sizeof(struct sctp_abort_chunk) +
   7981  1.1.2.2  skrll 					     sz);
   7982  1.1.2.2  skrll 	abort_m->sh.src_port = stcb->sctp_ep->sctp_lport;
   7983  1.1.2.2  skrll 	abort_m->sh.dest_port = stcb->rport;
   7984  1.1.2.2  skrll 	abort_m->sh.v_tag = htonl(stcb->asoc.peer_vtag);
   7985  1.1.2.2  skrll 	abort_m->sh.checksum = 0;
   7986  1.1.2.2  skrll 	m_abort->m_pkthdr.len = m_abort->m_len + sz;
   7987  1.1.2.5  skrll 	m_reset_rcvif(m_abort);
   7988  1.1.2.2  skrll 	sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
   7989  1.1.2.2  skrll 	    stcb->asoc.primary_destination,
   7990  1.1.2.2  skrll 	    rtcache_getdst(&stcb->asoc.primary_destination->ro),
   7991  1.1.2.2  skrll 	    m_abort, 1, 0, NULL, 0);
   7992  1.1.2.2  skrll }
   7993  1.1.2.2  skrll 
   7994  1.1.2.2  skrll int
   7995  1.1.2.2  skrll sctp_send_shutdown_complete(struct sctp_tcb *stcb,
   7996  1.1.2.2  skrll 			    struct sctp_nets *net)
   7997  1.1.2.2  skrll 
   7998  1.1.2.2  skrll {
   7999  1.1.2.2  skrll 	/* formulate and SEND a SHUTDOWN-COMPLETE */
   8000  1.1.2.2  skrll 	struct mbuf *m_shutdown_comp;
   8001  1.1.2.2  skrll 	struct sctp_shutdown_complete_msg *comp_cp;
   8002  1.1.2.2  skrll 
   8003  1.1.2.2  skrll 	m_shutdown_comp = NULL;
   8004  1.1.2.2  skrll 	MGETHDR(m_shutdown_comp, M_DONTWAIT, MT_HEADER);
   8005  1.1.2.2  skrll 	if (m_shutdown_comp == NULL) {
   8006  1.1.2.2  skrll 		/* no mbuf's */
   8007  1.1.2.2  skrll 		return (-1);
   8008  1.1.2.2  skrll 	}
   8009  1.1.2.2  skrll 	m_shutdown_comp->m_data += sizeof(struct ip6_hdr);
   8010  1.1.2.2  skrll 	comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
   8011  1.1.2.2  skrll 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
   8012  1.1.2.2  skrll 	comp_cp->shut_cmp.ch.chunk_flags = 0;
   8013  1.1.2.2  skrll 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
   8014  1.1.2.2  skrll 	comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
   8015  1.1.2.2  skrll 	comp_cp->sh.dest_port = stcb->rport;
   8016  1.1.2.2  skrll 	comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
   8017  1.1.2.2  skrll 	comp_cp->sh.checksum = 0;
   8018  1.1.2.2  skrll 
   8019  1.1.2.2  skrll 	m_shutdown_comp->m_pkthdr.len = m_shutdown_comp->m_len = sizeof(struct sctp_shutdown_complete_msg);
   8020  1.1.2.5  skrll 	m_reset_rcvif(m_shutdown_comp);
   8021  1.1.2.2  skrll 	sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
   8022  1.1.2.2  skrll 	    rtcache_getdst(&net->ro), m_shutdown_comp,
   8023  1.1.2.2  skrll 	    1, 0, NULL, 0);
   8024  1.1.2.2  skrll 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
   8025  1.1.2.2  skrll 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
   8026  1.1.2.2  skrll 		stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
   8027  1.1.2.2  skrll 		stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
   8028  1.1.2.2  skrll 		soisdisconnected(stcb->sctp_ep->sctp_socket);
   8029  1.1.2.2  skrll 	}
   8030  1.1.2.2  skrll 	return (0);
   8031  1.1.2.2  skrll }
   8032  1.1.2.2  skrll 
   8033  1.1.2.2  skrll int
   8034  1.1.2.2  skrll sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh)
   8035  1.1.2.2  skrll {
   8036  1.1.2.2  skrll 	/* formulate and SEND a SHUTDOWN-COMPLETE */
   8037  1.1.2.2  skrll 	struct mbuf *mout;
   8038  1.1.2.2  skrll 	struct ip *iph, *iph_out;
   8039  1.1.2.2  skrll 	struct ip6_hdr *ip6, *ip6_out;
   8040  1.1.2.2  skrll 	int offset_out;
   8041  1.1.2.2  skrll 	struct sctp_shutdown_complete_msg *comp_cp;
   8042  1.1.2.2  skrll 
   8043  1.1.2.2  skrll 	MGETHDR(mout, M_DONTWAIT, MT_HEADER);
   8044  1.1.2.2  skrll 	if (mout == NULL) {
   8045  1.1.2.2  skrll 		/* no mbuf's */
   8046  1.1.2.2  skrll 		return (-1);
   8047  1.1.2.2  skrll 	}
   8048  1.1.2.2  skrll 	iph = mtod(m, struct ip *);
   8049  1.1.2.2  skrll 	iph_out = NULL;
   8050  1.1.2.2  skrll 	ip6_out = NULL;
   8051  1.1.2.2  skrll 	offset_out = 0;
   8052  1.1.2.2  skrll 	if (iph->ip_v == IPVERSION) {
   8053  1.1.2.2  skrll 		mout->m_len = sizeof(struct ip) +
   8054  1.1.2.2  skrll 		    sizeof(struct sctp_shutdown_complete_msg);
   8055  1.1.2.2  skrll 		mout->m_next = NULL;
   8056  1.1.2.2  skrll 		iph_out = mtod(mout, struct ip *);
   8057  1.1.2.2  skrll 
   8058  1.1.2.2  skrll 		/* Fill in the IP header for the ABORT */
   8059  1.1.2.2  skrll 		iph_out->ip_v = IPVERSION;
   8060  1.1.2.2  skrll 		iph_out->ip_hl = (sizeof(struct ip)/4);
   8061  1.1.2.2  skrll 		iph_out->ip_tos = (u_char)0;
   8062  1.1.2.2  skrll 		iph_out->ip_id = 0;
   8063  1.1.2.2  skrll 		iph_out->ip_off = 0;
   8064  1.1.2.2  skrll 		iph_out->ip_ttl = MAXTTL;
   8065  1.1.2.2  skrll 		iph_out->ip_p = IPPROTO_SCTP;
   8066  1.1.2.2  skrll 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
   8067  1.1.2.2  skrll 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
   8068  1.1.2.2  skrll 
   8069  1.1.2.2  skrll 		/* let IP layer calculate this */
   8070  1.1.2.2  skrll 		iph_out->ip_sum = 0;
   8071  1.1.2.2  skrll 		offset_out += sizeof(*iph_out);
   8072  1.1.2.2  skrll 		comp_cp = (struct sctp_shutdown_complete_msg *)(
   8073  1.1.2.2  skrll 		    (vaddr_t)iph_out + offset_out);
   8074  1.1.2.2  skrll 	} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
   8075  1.1.2.2  skrll 		ip6 = (struct ip6_hdr *)iph;
   8076  1.1.2.2  skrll 		mout->m_len = sizeof(struct ip6_hdr) +
   8077  1.1.2.2  skrll 		    sizeof(struct sctp_shutdown_complete_msg);
   8078  1.1.2.2  skrll 		mout->m_next = NULL;
   8079  1.1.2.2  skrll 		ip6_out = mtod(mout, struct ip6_hdr *);
   8080  1.1.2.2  skrll 
   8081  1.1.2.2  skrll 		/* Fill in the IPv6 header for the ABORT */
   8082  1.1.2.2  skrll 		ip6_out->ip6_flow = ip6->ip6_flow;
   8083  1.1.2.2  skrll 		ip6_out->ip6_hlim = ip6_defhlim;
   8084  1.1.2.2  skrll 		ip6_out->ip6_nxt = IPPROTO_SCTP;
   8085  1.1.2.2  skrll 		ip6_out->ip6_src = ip6->ip6_dst;
   8086  1.1.2.2  skrll 		ip6_out->ip6_dst = ip6->ip6_src;
   8087  1.1.2.2  skrll  		ip6_out->ip6_plen = mout->m_len;
   8088  1.1.2.2  skrll 		offset_out += sizeof(*ip6_out);
   8089  1.1.2.2  skrll 		comp_cp = (struct sctp_shutdown_complete_msg *)(
   8090  1.1.2.2  skrll 		    (vaddr_t)ip6_out + offset_out);
   8091  1.1.2.2  skrll 	} else {
   8092  1.1.2.2  skrll 		/* Currently not supported. */
   8093  1.1.2.2  skrll 		return (-1);
   8094  1.1.2.2  skrll 	}
   8095  1.1.2.2  skrll 
   8096  1.1.2.2  skrll 	/* Now copy in and fill in the ABORT tags etc. */
   8097  1.1.2.2  skrll 	comp_cp->sh.src_port = sh->dest_port;
   8098  1.1.2.2  skrll 	comp_cp->sh.dest_port = sh->src_port;
   8099  1.1.2.2  skrll 	comp_cp->sh.checksum = 0;
   8100  1.1.2.2  skrll 	comp_cp->sh.v_tag = sh->v_tag;
   8101  1.1.2.2  skrll 	comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
   8102  1.1.2.2  skrll 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
   8103  1.1.2.2  skrll 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
   8104  1.1.2.2  skrll 
   8105  1.1.2.2  skrll 	mout->m_pkthdr.len = mout->m_len;
   8106  1.1.2.2  skrll 	/* add checksum */
   8107  1.1.2.5  skrll 	if ((sctp_no_csum_on_loopback) && m_get_rcvif_NOMPSAFE(m) != NULL &&
   8108  1.1.2.5  skrll 	    m_get_rcvif_NOMPSAFE(m)->if_type == IFT_LOOP) {
   8109  1.1.2.2  skrll 		comp_cp->sh.checksum =  0;
   8110  1.1.2.2  skrll 	} else {
   8111  1.1.2.2  skrll 		comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
   8112  1.1.2.2  skrll 	}
   8113  1.1.2.2  skrll 
   8114  1.1.2.2  skrll 	/* zap the rcvif, it should be null */
   8115  1.1.2.5  skrll 	m_reset_rcvif(mout);
   8116  1.1.2.2  skrll 	/* zap the stack pointer to the route */
   8117  1.1.2.2  skrll 	if (iph_out != NULL) {
   8118  1.1.2.2  skrll 		struct route ro;
   8119  1.1.2.2  skrll 
   8120  1.1.2.2  skrll 		memset(&ro, 0, sizeof ro);
   8121  1.1.2.2  skrll #ifdef SCTP_DEBUG
   8122  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   8123  1.1.2.2  skrll 			printf("sctp_shutdown_complete2 calling ip_output:\n");
   8124  1.1.2.2  skrll 			sctp_print_address_pkt(iph_out, &comp_cp->sh);
   8125  1.1.2.2  skrll 		}
   8126  1.1.2.2  skrll #endif
   8127  1.1.2.2  skrll 		/* set IPv4 length */
   8128  1.1.2.2  skrll 		iph_out->ip_len = htons(mout->m_pkthdr.len);
   8129  1.1.2.2  skrll 		/* out it goes */
   8130  1.1.2.2  skrll 		ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL, NULL);
   8131  1.1.2.2  skrll 	} else if (ip6_out != NULL) {
   8132  1.1.2.2  skrll 		struct route ro;
   8133  1.1.2.2  skrll 
   8134  1.1.2.2  skrll 		memset(&ro, 0, sizeof(ro));
   8135  1.1.2.2  skrll #ifdef SCTP_DEBUG
   8136  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   8137  1.1.2.2  skrll 			printf("sctp_shutdown_complete2 calling ip6_output:\n");
   8138  1.1.2.2  skrll 			sctp_print_address_pkt((struct ip *)ip6_out,
   8139  1.1.2.2  skrll 			    &comp_cp->sh);
   8140  1.1.2.2  skrll 		}
   8141  1.1.2.2  skrll #endif
   8142  1.1.2.2  skrll 		ip6_output(mout, NULL, &ro, 0, NULL, NULL, NULL);
   8143  1.1.2.2  skrll 	}
   8144  1.1.2.2  skrll 	sctp_pegs[SCTP_DATAGRAMS_SENT]++;
   8145  1.1.2.2  skrll 	return (0);
   8146  1.1.2.2  skrll }
   8147  1.1.2.2  skrll 
   8148  1.1.2.2  skrll static struct sctp_nets *
   8149  1.1.2.2  skrll sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
   8150  1.1.2.2  skrll {
   8151  1.1.2.2  skrll 	struct sctp_nets *net, *hnet;
   8152  1.1.2.2  skrll 	int ms_goneby, highest_ms, state_overide=0;
   8153  1.1.2.2  skrll 
   8154  1.1.2.2  skrll 	SCTP_GETTIME_TIMEVAL(now);
   8155  1.1.2.2  skrll 	highest_ms = 0;
   8156  1.1.2.2  skrll 	hnet = NULL;
   8157  1.1.2.2  skrll 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   8158  1.1.2.2  skrll 		if (
   8159  1.1.2.2  skrll 			((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
   8160  1.1.2.2  skrll 			(net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
   8161  1.1.2.2  skrll 			) {
   8162  1.1.2.2  skrll 			/* Skip this guy from consideration if HB is off AND its confirmed*/
   8163  1.1.2.2  skrll #ifdef SCTP_DEBUG
   8164  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   8165  1.1.2.2  skrll 				printf("Skipping net:%p state:%d nohb/out-of-scope\n",
   8166  1.1.2.2  skrll 				       net, net->dest_state);
   8167  1.1.2.2  skrll 			}
   8168  1.1.2.2  skrll #endif
   8169  1.1.2.2  skrll 			continue;
   8170  1.1.2.2  skrll 		}
   8171  1.1.2.2  skrll 		if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro.ro_sa) == 0) {
   8172  1.1.2.2  skrll 			/* skip this dest net from consideration */
   8173  1.1.2.2  skrll #ifdef SCTP_DEBUG
   8174  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   8175  1.1.2.2  skrll 				printf("Skipping net:%p reachable NOT\n",
   8176  1.1.2.2  skrll 				       net);
   8177  1.1.2.2  skrll 			}
   8178  1.1.2.2  skrll #endif
   8179  1.1.2.2  skrll 			continue;
   8180  1.1.2.2  skrll 		}
   8181  1.1.2.2  skrll 		if (net->last_sent_time.tv_sec) {
   8182  1.1.2.2  skrll 			/* Sent to so we subtract */
   8183  1.1.2.2  skrll 			ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
   8184  1.1.2.2  skrll 		} else
   8185  1.1.2.2  skrll 			/* Never been sent to */
   8186  1.1.2.2  skrll 			ms_goneby = 0x7fffffff;
   8187  1.1.2.2  skrll #ifdef SCTP_DEBUG
   8188  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   8189  1.1.2.2  skrll 			printf("net:%p ms_goneby:%d\n",
   8190  1.1.2.2  skrll 			       net, ms_goneby);
   8191  1.1.2.2  skrll 		}
   8192  1.1.2.2  skrll #endif
   8193  1.1.2.2  skrll 		/* When the address state is unconfirmed but still considered reachable, we
   8194  1.1.2.2  skrll 		 * HB at a higher rate. Once it goes confirmed OR reaches the "unreachable"
   8195  1.1.2.2  skrll 		 * state, thenw we cut it back to HB at a more normal pace.
   8196  1.1.2.2  skrll 		 */
   8197  1.1.2.2  skrll 		if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED|SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
   8198  1.1.2.2  skrll 			state_overide = 1;
   8199  1.1.2.2  skrll 		} else {
   8200  1.1.2.2  skrll 			state_overide = 0;
   8201  1.1.2.2  skrll 		}
   8202  1.1.2.2  skrll 
   8203  1.1.2.2  skrll 		if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
   8204  1.1.2.2  skrll 		    (ms_goneby > highest_ms)) {
   8205  1.1.2.2  skrll 			highest_ms = ms_goneby;
   8206  1.1.2.2  skrll 			hnet = net;
   8207  1.1.2.2  skrll #ifdef SCTP_DEBUG
   8208  1.1.2.2  skrll 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   8209  1.1.2.2  skrll 				printf("net:%p is the new high\n",
   8210  1.1.2.2  skrll 				       net);
   8211  1.1.2.2  skrll 			}
   8212  1.1.2.2  skrll #endif
   8213  1.1.2.2  skrll 		}
   8214  1.1.2.2  skrll 	}
   8215  1.1.2.2  skrll 	if (hnet &&
   8216  1.1.2.2  skrll 	   ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED|SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
   8217  1.1.2.2  skrll 		state_overide = 1;
   8218  1.1.2.2  skrll 	} else {
   8219  1.1.2.2  skrll 		state_overide = 0;
   8220  1.1.2.2  skrll 	}
   8221  1.1.2.2  skrll 
   8222  1.1.2.2  skrll 	if (highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
   8223  1.1.2.2  skrll 		/* Found the one with longest delay bounds
   8224  1.1.2.2  skrll 		 * OR it is unconfirmed and still not marked
   8225  1.1.2.2  skrll 		 * unreachable.
   8226  1.1.2.2  skrll 		 */
   8227  1.1.2.2  skrll #ifdef SCTP_DEBUG
   8228  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   8229  1.1.2.2  skrll 			printf("net:%p is the hb winner -",
   8230  1.1.2.2  skrll 				hnet);
   8231  1.1.2.2  skrll 			if (hnet)
   8232  1.1.2.2  skrll 				sctp_print_address((struct sockaddr *)&hnet->ro.ro_sa);
   8233  1.1.2.2  skrll 			else
   8234  1.1.2.2  skrll 				printf(" none\n");
   8235  1.1.2.2  skrll 		}
   8236  1.1.2.2  skrll #endif
   8237  1.1.2.2  skrll 		/* update the timer now */
   8238  1.1.2.2  skrll 		hnet->last_sent_time = *now;
   8239  1.1.2.2  skrll 		return (hnet);
   8240  1.1.2.2  skrll 	}
   8241  1.1.2.2  skrll 	/* Nothing to HB */
   8242  1.1.2.2  skrll 	return (NULL);
   8243  1.1.2.2  skrll }
   8244  1.1.2.2  skrll 
   8245  1.1.2.2  skrll int
   8246  1.1.2.2  skrll sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
   8247  1.1.2.2  skrll {
   8248  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   8249  1.1.2.2  skrll 	struct sctp_nets *net;
   8250  1.1.2.2  skrll 	struct sctp_heartbeat_chunk *hb;
   8251  1.1.2.2  skrll 	struct timeval now;
   8252  1.1.2.2  skrll 	struct sockaddr_in *sin;
   8253  1.1.2.2  skrll 	struct sockaddr_in6 *sin6;
   8254  1.1.2.2  skrll 
   8255  1.1.2.2  skrll 	if (user_req == 0) {
   8256  1.1.2.2  skrll 		net = sctp_select_hb_destination(stcb, &now);
   8257  1.1.2.2  skrll 		if (net == NULL) {
   8258  1.1.2.2  skrll 			/* All our busy none to send to, just
   8259  1.1.2.2  skrll 			 * start the timer again.
   8260  1.1.2.2  skrll 			 */
   8261  1.1.2.2  skrll 			if (stcb->asoc.state == 0) {
   8262  1.1.2.2  skrll 				return (0);
   8263  1.1.2.2  skrll 			}
   8264  1.1.2.2  skrll 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
   8265  1.1.2.2  skrll 					 stcb->sctp_ep,
   8266  1.1.2.2  skrll 					 stcb,
   8267  1.1.2.2  skrll 					 net);
   8268  1.1.2.2  skrll 			return (0);
   8269  1.1.2.2  skrll 		}
   8270  1.1.2.2  skrll #ifndef SCTP_USE_ALLMAN_BURST
   8271  1.1.2.2  skrll 		else {
   8272  1.1.2.2  skrll 			/* found one idle.. decay cwnd on this one
   8273  1.1.2.2  skrll 			 * by 1/2 if none outstanding.
   8274  1.1.2.2  skrll 			 */
   8275  1.1.2.2  skrll 
   8276  1.1.2.2  skrll 			if (net->flight_size == 0) {
   8277  1.1.2.2  skrll 				net->cwnd /= 2;
   8278  1.1.2.2  skrll 				if (net->addr_is_local) {
   8279  1.1.2.2  skrll 					if (net->cwnd < (net->mtu *4)) {
   8280  1.1.2.2  skrll 						net->cwnd = net->mtu * 4;
   8281  1.1.2.2  skrll 					}
   8282  1.1.2.2  skrll 				} else {
   8283  1.1.2.2  skrll 					if (net->cwnd < (net->mtu * 2)) {
   8284  1.1.2.2  skrll 						net->cwnd = net->mtu * 2;
   8285  1.1.2.2  skrll 					}
   8286  1.1.2.2  skrll 				}
   8287  1.1.2.2  skrll 
   8288  1.1.2.2  skrll 			}
   8289  1.1.2.2  skrll 
   8290  1.1.2.2  skrll 		}
   8291  1.1.2.2  skrll #endif
   8292  1.1.2.2  skrll 	} else {
   8293  1.1.2.2  skrll 		net = u_net;
   8294  1.1.2.2  skrll 		if (net == NULL) {
   8295  1.1.2.2  skrll 			return (0);
   8296  1.1.2.2  skrll 		}
   8297  1.1.2.2  skrll 		SCTP_GETTIME_TIMEVAL(&now);
   8298  1.1.2.2  skrll 	}
   8299  1.1.2.2  skrll 	sin = (struct sockaddr_in *)&net->ro.ro_sa;
   8300  1.1.2.2  skrll 	if (sin->sin_family != AF_INET) {
   8301  1.1.2.2  skrll 		if (sin->sin_family != AF_INET6) {
   8302  1.1.2.2  skrll 			/* huh */
   8303  1.1.2.2  skrll 			return (0);
   8304  1.1.2.2  skrll 		}
   8305  1.1.2.2  skrll 	}
   8306  1.1.2.2  skrll 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   8307  1.1.2.2  skrll 	if (chk == NULL) {
   8308  1.1.2.2  skrll #ifdef SCTP_DEBUG
   8309  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   8310  1.1.2.2  skrll 			printf("Gak, can't get a chunk for hb\n");
   8311  1.1.2.2  skrll 		}
   8312  1.1.2.2  skrll #endif
   8313  1.1.2.2  skrll 		return (0);
   8314  1.1.2.2  skrll 	}
   8315  1.1.2.2  skrll 	sctppcbinfo.ipi_gencnt_chunk++;
   8316  1.1.2.2  skrll 	sctppcbinfo.ipi_count_chunk++;
   8317  1.1.2.2  skrll 	chk->rec.chunk_id = SCTP_HEARTBEAT_REQUEST;
   8318  1.1.2.2  skrll 	chk->asoc = &stcb->asoc;
   8319  1.1.2.2  skrll 	chk->send_size = sizeof(struct sctp_heartbeat_chunk);
   8320  1.1.2.2  skrll 	MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
   8321  1.1.2.2  skrll 	if (chk->data == NULL) {
   8322  1.1.2.2  skrll 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   8323  1.1.2.2  skrll 		sctppcbinfo.ipi_count_chunk--;
   8324  1.1.2.2  skrll 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   8325  1.1.2.2  skrll 			panic("Chunk count is negative");
   8326  1.1.2.2  skrll 		}
   8327  1.1.2.2  skrll 		sctppcbinfo.ipi_gencnt_chunk++;
   8328  1.1.2.2  skrll 		return (0);
   8329  1.1.2.2  skrll 	}
   8330  1.1.2.2  skrll 	chk->data->m_data += SCTP_MIN_OVERHEAD;
   8331  1.1.2.2  skrll 	chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
   8332  1.1.2.2  skrll 	chk->sent = SCTP_DATAGRAM_UNSENT;
   8333  1.1.2.2  skrll 	chk->snd_count = 0;
   8334  1.1.2.2  skrll 	chk->whoTo = net;
   8335  1.1.2.2  skrll 	chk->whoTo->ref_count++;
   8336  1.1.2.2  skrll 	/* Now we have a mbuf that we can fill in with the details */
   8337  1.1.2.2  skrll 	hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
   8338  1.1.2.2  skrll 
   8339  1.1.2.2  skrll 	/* fill out chunk header */
   8340  1.1.2.2  skrll 	hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
   8341  1.1.2.2  skrll 	hb->ch.chunk_flags = 0;
   8342  1.1.2.2  skrll 	hb->ch.chunk_length = htons(chk->send_size);
   8343  1.1.2.2  skrll 	/* Fill out hb parameter */
   8344  1.1.2.2  skrll 	hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
   8345  1.1.2.2  skrll 	hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
   8346  1.1.2.2  skrll 	hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
   8347  1.1.2.2  skrll 	hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
   8348  1.1.2.2  skrll 	/* Did our user request this one, put it in */
   8349  1.1.2.2  skrll 	hb->heartbeat.hb_info.user_req = user_req;
   8350  1.1.2.2  skrll 	hb->heartbeat.hb_info.addr_family = sin->sin_family;
   8351  1.1.2.2  skrll 	hb->heartbeat.hb_info.addr_len = sin->sin_len;
   8352  1.1.2.2  skrll 	if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
   8353  1.1.2.2  skrll 		/* we only take from the entropy pool if the address is
   8354  1.1.2.2  skrll 		 * not confirmed.
   8355  1.1.2.2  skrll 		 */
   8356  1.1.2.2  skrll  		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
   8357  1.1.2.2  skrll  		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
   8358  1.1.2.2  skrll 	} else {
   8359  1.1.2.2  skrll 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
   8360  1.1.2.2  skrll 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
   8361  1.1.2.2  skrll 	}
   8362  1.1.2.2  skrll 	if (sin->sin_family == AF_INET) {
   8363  1.1.2.2  skrll 		memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
   8364  1.1.2.2  skrll 	} else if (sin->sin_family == AF_INET6) {
   8365  1.1.2.2  skrll 		/* We leave the scope the way it is in our lookup table. */
   8366  1.1.2.2  skrll 		sin6 = (struct sockaddr_in6 *)&net->ro.ro_sa;
   8367  1.1.2.2  skrll 		memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
   8368  1.1.2.2  skrll 	} else {
   8369  1.1.2.2  skrll 		/* huh compiler bug */
   8370  1.1.2.2  skrll #ifdef SCTP_DEBUG
   8371  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   8372  1.1.2.2  skrll 			printf("Compiler bug bleeds a mbuf and a chunk\n");
   8373  1.1.2.2  skrll 		}
   8374  1.1.2.2  skrll #endif
   8375  1.1.2.2  skrll 		return (0);
   8376  1.1.2.2  skrll 	}
   8377  1.1.2.2  skrll 	/* ok we have a destination that needs a beat */
   8378  1.1.2.2  skrll 	/* lets do the theshold management Qiaobing style */
   8379  1.1.2.2  skrll 	if (user_req == 0) {
   8380  1.1.2.2  skrll 		if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
   8381  1.1.2.2  skrll 					      stcb->asoc.max_send_times)) {
   8382  1.1.2.2  skrll 			/* we have lost the association, in a way this
   8383  1.1.2.2  skrll 			 * is quite bad since we really are one less time
   8384  1.1.2.2  skrll 			 * since we really did not send yet. This is the
   8385  1.1.2.2  skrll 			 * down side to the Q's style as defined in the RFC
   8386  1.1.2.2  skrll 			 * and not my alternate style defined in the RFC.
   8387  1.1.2.2  skrll 			 */
   8388  1.1.2.2  skrll 			if (chk->data != NULL) {
   8389  1.1.2.2  skrll 				sctp_m_freem(chk->data);
   8390  1.1.2.2  skrll 				chk->data = NULL;
   8391  1.1.2.2  skrll 			}
   8392  1.1.2.2  skrll 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   8393  1.1.2.2  skrll 			sctppcbinfo.ipi_count_chunk--;
   8394  1.1.2.2  skrll 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   8395  1.1.2.2  skrll 				panic("Chunk count is negative");
   8396  1.1.2.2  skrll 			}
   8397  1.1.2.2  skrll 			sctppcbinfo.ipi_gencnt_chunk++;
   8398  1.1.2.2  skrll 			return (-1);
   8399  1.1.2.2  skrll 		}
   8400  1.1.2.2  skrll 	}
   8401  1.1.2.2  skrll 	net->hb_responded = 0;
   8402  1.1.2.2  skrll #ifdef SCTP_DEBUG
   8403  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   8404  1.1.2.2  skrll 		printf("Inserting chunk for HB\n");
   8405  1.1.2.2  skrll 	}
   8406  1.1.2.2  skrll #endif
   8407  1.1.2.2  skrll 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
   8408  1.1.2.2  skrll 	stcb->asoc.ctrl_queue_cnt++;
   8409  1.1.2.2  skrll 	sctp_pegs[SCTP_HB_SENT]++;
   8410  1.1.2.2  skrll 	/*
   8411  1.1.2.2  skrll 	 * Call directly med level routine to put out the chunk. It will
   8412  1.1.2.2  skrll 	 * always tumble out control chunks aka HB but it may even tumble
   8413  1.1.2.2  skrll 	 * out data too.
   8414  1.1.2.2  skrll 	 */
   8415  1.1.2.2  skrll 	if (user_req == 0) {
   8416  1.1.2.2  skrll 		/* Ok now lets start the HB timer if it is NOT a user req */
   8417  1.1.2.2  skrll 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
   8418  1.1.2.2  skrll 				 stcb, net);
   8419  1.1.2.2  skrll 	}
   8420  1.1.2.2  skrll 	return (1);
   8421  1.1.2.2  skrll }
   8422  1.1.2.2  skrll 
   8423  1.1.2.2  skrll void
   8424  1.1.2.2  skrll sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
   8425  1.1.2.2  skrll 		   uint32_t high_tsn)
   8426  1.1.2.2  skrll {
   8427  1.1.2.2  skrll 	struct sctp_association *asoc;
   8428  1.1.2.2  skrll 	struct sctp_ecne_chunk *ecne;
   8429  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   8430  1.1.2.2  skrll 	asoc = &stcb->asoc;
   8431  1.1.2.2  skrll 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
   8432  1.1.2.2  skrll 		if (chk->rec.chunk_id == SCTP_ECN_ECHO) {
   8433  1.1.2.2  skrll 			/* found a previous ECN_ECHO update it if needed */
   8434  1.1.2.2  skrll 			ecne = mtod(chk->data, struct sctp_ecne_chunk *);
   8435  1.1.2.2  skrll 			ecne->tsn = htonl(high_tsn);
   8436  1.1.2.2  skrll 			return;
   8437  1.1.2.2  skrll 		}
   8438  1.1.2.2  skrll 	}
   8439  1.1.2.2  skrll 	/* nope could not find one to update so we must build one */
   8440  1.1.2.2  skrll 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   8441  1.1.2.2  skrll 	if (chk == NULL) {
   8442  1.1.2.2  skrll 		return;
   8443  1.1.2.2  skrll 	}
   8444  1.1.2.2  skrll 	sctp_pegs[SCTP_ECNE_SENT]++;
   8445  1.1.2.2  skrll 	sctppcbinfo.ipi_count_chunk++;
   8446  1.1.2.2  skrll 	sctppcbinfo.ipi_gencnt_chunk++;
   8447  1.1.2.2  skrll 	chk->rec.chunk_id = SCTP_ECN_ECHO;
   8448  1.1.2.2  skrll 	chk->asoc = &stcb->asoc;
   8449  1.1.2.2  skrll 	chk->send_size = sizeof(struct sctp_ecne_chunk);
   8450  1.1.2.2  skrll 	MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
   8451  1.1.2.2  skrll 	if (chk->data == NULL) {
   8452  1.1.2.2  skrll 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   8453  1.1.2.2  skrll 		sctppcbinfo.ipi_count_chunk--;
   8454  1.1.2.2  skrll 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   8455  1.1.2.2  skrll 			panic("Chunk count is negative");
   8456  1.1.2.2  skrll 		}
   8457  1.1.2.2  skrll 		sctppcbinfo.ipi_gencnt_chunk++;
   8458  1.1.2.2  skrll 		return;
   8459  1.1.2.2  skrll 	}
   8460  1.1.2.2  skrll 	chk->data->m_data += SCTP_MIN_OVERHEAD;
   8461  1.1.2.2  skrll 	chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
   8462  1.1.2.2  skrll 	chk->sent = SCTP_DATAGRAM_UNSENT;
   8463  1.1.2.2  skrll 	chk->snd_count = 0;
   8464  1.1.2.2  skrll 	chk->whoTo = net;
   8465  1.1.2.2  skrll 	chk->whoTo->ref_count++;
   8466  1.1.2.2  skrll 	ecne = mtod(chk->data, struct sctp_ecne_chunk *);
   8467  1.1.2.2  skrll 	ecne->ch.chunk_type = SCTP_ECN_ECHO;
   8468  1.1.2.2  skrll 	ecne->ch.chunk_flags = 0;
   8469  1.1.2.2  skrll 	ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
   8470  1.1.2.2  skrll 	ecne->tsn = htonl(high_tsn);
   8471  1.1.2.2  skrll 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
   8472  1.1.2.2  skrll 	asoc->ctrl_queue_cnt++;
   8473  1.1.2.2  skrll }
   8474  1.1.2.2  skrll 
   8475  1.1.2.2  skrll void
   8476  1.1.2.2  skrll sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
   8477  1.1.2.2  skrll 			 struct mbuf *m, int iphlen, int bad_crc)
   8478  1.1.2.2  skrll {
   8479  1.1.2.2  skrll 	struct sctp_association *asoc;
   8480  1.1.2.2  skrll 	struct sctp_pktdrop_chunk *drp;
   8481  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   8482  1.1.2.2  skrll 	uint8_t *datap;
   8483  1.1.2.2  skrll 	int len;
   8484  1.1.2.2  skrll 	unsigned int small_one;
   8485  1.1.2.2  skrll 	struct ip *iph;
   8486  1.1.2.2  skrll 
   8487  1.1.2.2  skrll 	long spc;
   8488  1.1.2.2  skrll 	asoc = &stcb->asoc;
   8489  1.1.2.2  skrll 	if (asoc->peer_supports_pktdrop == 0) {
   8490  1.1.2.2  skrll 		/* peer must declare support before I
   8491  1.1.2.2  skrll 		 * send one.
   8492  1.1.2.2  skrll 		 */
   8493  1.1.2.2  skrll 		return;
   8494  1.1.2.2  skrll 	}
   8495  1.1.2.2  skrll 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   8496  1.1.2.2  skrll 	if (chk == NULL) {
   8497  1.1.2.2  skrll 		return;
   8498  1.1.2.2  skrll 	}
   8499  1.1.2.2  skrll 	sctppcbinfo.ipi_count_chunk++;
   8500  1.1.2.2  skrll 	sctppcbinfo.ipi_gencnt_chunk++;
   8501  1.1.2.2  skrll 
   8502  1.1.2.2  skrll 	iph = mtod(m, struct ip *);
   8503  1.1.2.2  skrll 	if (iph == NULL) {
   8504  1.1.2.2  skrll 		return;
   8505  1.1.2.2  skrll 	}
   8506  1.1.2.2  skrll 	if (iph->ip_v == IPVERSION) {
   8507  1.1.2.2  skrll 		/* IPv4 */
   8508  1.1.2.2  skrll #if defined(__FreeBSD__)
   8509  1.1.2.2  skrll 		len = chk->send_size = iph->ip_len;
   8510  1.1.2.2  skrll #else
   8511  1.1.2.2  skrll 		len = chk->send_size = (iph->ip_len - iphlen);
   8512  1.1.2.2  skrll #endif
   8513  1.1.2.2  skrll 	} else {
   8514  1.1.2.2  skrll 		struct ip6_hdr *ip6h;
   8515  1.1.2.2  skrll 		/* IPv6 */
   8516  1.1.2.2  skrll 		ip6h = mtod(m, struct ip6_hdr *);
   8517  1.1.2.2  skrll 		len = chk->send_size = htons(ip6h->ip6_plen);
   8518  1.1.2.2  skrll 	}
   8519  1.1.2.2  skrll 	if ((len+iphlen) > m->m_pkthdr.len) {
   8520  1.1.2.2  skrll 		/* huh */
   8521  1.1.2.2  skrll 		chk->send_size = len = m->m_pkthdr.len - iphlen;
   8522  1.1.2.2  skrll 	}
   8523  1.1.2.2  skrll 	chk->asoc = &stcb->asoc;
   8524  1.1.2.2  skrll 	MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
   8525  1.1.2.2  skrll 	if (chk->data == NULL) {
   8526  1.1.2.2  skrll 	jump_out:
   8527  1.1.2.2  skrll 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   8528  1.1.2.2  skrll 		sctppcbinfo.ipi_count_chunk--;
   8529  1.1.2.2  skrll 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   8530  1.1.2.2  skrll 			panic("Chunk count is negative");
   8531  1.1.2.2  skrll 		}
   8532  1.1.2.2  skrll 		sctppcbinfo.ipi_gencnt_chunk++;
   8533  1.1.2.2  skrll 		return;
   8534  1.1.2.2  skrll 	}
   8535  1.1.2.2  skrll 	if ((chk->send_size+sizeof(struct sctp_pktdrop_chunk)+SCTP_MIN_OVERHEAD) > MHLEN) {
   8536  1.1.2.2  skrll 		MCLGET(chk->data, M_DONTWAIT);
   8537  1.1.2.2  skrll 		if ((chk->data->m_flags & M_EXT) == 0) {
   8538  1.1.2.2  skrll 			/* Give up */
   8539  1.1.2.2  skrll 			sctp_m_freem(chk->data);
   8540  1.1.2.2  skrll 			chk->data = NULL;
   8541  1.1.2.2  skrll 			goto jump_out;
   8542  1.1.2.2  skrll 		}
   8543  1.1.2.2  skrll 	}
   8544  1.1.2.2  skrll 	chk->data->m_data += SCTP_MIN_OVERHEAD;
   8545  1.1.2.2  skrll 	drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
   8546  1.1.2.2  skrll 	if (drp == NULL) {
   8547  1.1.2.2  skrll 		sctp_m_freem(chk->data);
   8548  1.1.2.2  skrll 		chk->data = NULL;
   8549  1.1.2.2  skrll 		goto jump_out;
   8550  1.1.2.2  skrll 	}
   8551  1.1.2.2  skrll 	small_one = asoc->smallest_mtu;
   8552  1.1.2.2  skrll 	if (small_one > MCLBYTES) {
   8553  1.1.2.2  skrll 		/* Only one cluster worth of data MAX */
   8554  1.1.2.2  skrll 		small_one = MCLBYTES;
   8555  1.1.2.2  skrll 	}
   8556  1.1.2.2  skrll 	chk->book_size = (chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
   8557  1.1.2.2  skrll 			  sizeof(struct sctphdr) + SCTP_MED_OVERHEAD);
   8558  1.1.2.2  skrll 	if (chk->book_size > small_one) {
   8559  1.1.2.2  skrll 		drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
   8560  1.1.2.2  skrll 		drp->trunc_len = htons(chk->send_size);
   8561  1.1.2.2  skrll 		chk->send_size = small_one - (SCTP_MED_OVERHEAD +
   8562  1.1.2.2  skrll 					     sizeof(struct sctp_pktdrop_chunk) +
   8563  1.1.2.2  skrll 					     sizeof(struct sctphdr));
   8564  1.1.2.2  skrll 		len = chk->send_size;
   8565  1.1.2.2  skrll 	} else {
   8566  1.1.2.2  skrll 		/* no truncation needed */
   8567  1.1.2.2  skrll 		drp->ch.chunk_flags = 0;
   8568  1.1.2.2  skrll 		drp->trunc_len = htons(0);
   8569  1.1.2.2  skrll 	}
   8570  1.1.2.2  skrll 	if (bad_crc) {
   8571  1.1.2.2  skrll 		drp->ch.chunk_flags |= SCTP_BADCRC;
   8572  1.1.2.2  skrll 	}
   8573  1.1.2.2  skrll 	chk->send_size += sizeof(struct sctp_pktdrop_chunk);
   8574  1.1.2.2  skrll 	chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
   8575  1.1.2.2  skrll 	chk->sent = SCTP_DATAGRAM_UNSENT;
   8576  1.1.2.2  skrll 	chk->snd_count = 0;
   8577  1.1.2.2  skrll 	if (net) {
   8578  1.1.2.2  skrll 		/* we should hit here */
   8579  1.1.2.2  skrll 		chk->whoTo = net;
   8580  1.1.2.2  skrll 	} else {
   8581  1.1.2.2  skrll 		chk->whoTo = asoc->primary_destination;
   8582  1.1.2.2  skrll 	}
   8583  1.1.2.2  skrll 	chk->whoTo->ref_count++;
   8584  1.1.2.2  skrll 	chk->rec.chunk_id = SCTP_PACKET_DROPPED;
   8585  1.1.2.2  skrll 	drp->ch.chunk_type = SCTP_PACKET_DROPPED;
   8586  1.1.2.2  skrll 	drp->ch.chunk_length = htons(chk->send_size);
   8587  1.1.2.2  skrll 	spc = stcb->sctp_socket->so_rcv.sb_hiwat;
   8588  1.1.2.2  skrll 	if (spc < 0) {
   8589  1.1.2.2  skrll 		spc = 0;
   8590  1.1.2.2  skrll 	}
   8591  1.1.2.2  skrll 	drp->bottle_bw = htonl(spc);
   8592  1.1.2.2  skrll 	drp->current_onq = htonl(asoc->size_on_delivery_queue +
   8593  1.1.2.2  skrll 				 asoc->size_on_reasm_queue +
   8594  1.1.2.2  skrll 				 asoc->size_on_all_streams +
   8595  1.1.2.2  skrll 				 asoc->my_rwnd_control_len +
   8596  1.1.2.2  skrll 		                 stcb->sctp_socket->so_rcv.sb_cc);
   8597  1.1.2.2  skrll 	drp->reserved = 0;
   8598  1.1.2.2  skrll 	datap = drp->data;
   8599  1.1.2.2  skrll         m_copydata(m, iphlen, len, datap);
   8600  1.1.2.2  skrll 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
   8601  1.1.2.2  skrll 	asoc->ctrl_queue_cnt++;
   8602  1.1.2.2  skrll }
   8603  1.1.2.2  skrll 
   8604  1.1.2.2  skrll void
   8605  1.1.2.2  skrll sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
   8606  1.1.2.2  skrll {
   8607  1.1.2.2  skrll 	struct sctp_association *asoc;
   8608  1.1.2.2  skrll 	struct sctp_cwr_chunk *cwr;
   8609  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   8610  1.1.2.2  skrll 
   8611  1.1.2.2  skrll 	asoc = &stcb->asoc;
   8612  1.1.2.2  skrll 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
   8613  1.1.2.2  skrll 		if (chk->rec.chunk_id == SCTP_ECN_CWR) {
   8614  1.1.2.2  skrll 			/* found a previous ECN_CWR update it if needed */
   8615  1.1.2.2  skrll 			cwr = mtod(chk->data, struct sctp_cwr_chunk *);
   8616  1.1.2.2  skrll 			if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
   8617  1.1.2.2  skrll 					      MAX_TSN)) {
   8618  1.1.2.2  skrll 				cwr->tsn = htonl(high_tsn);
   8619  1.1.2.2  skrll 			}
   8620  1.1.2.2  skrll 			return;
   8621  1.1.2.2  skrll 		}
   8622  1.1.2.2  skrll 	}
   8623  1.1.2.2  skrll 	/* nope could not find one to update so we must build one */
   8624  1.1.2.2  skrll 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   8625  1.1.2.2  skrll 	if (chk == NULL) {
   8626  1.1.2.2  skrll 		return;
   8627  1.1.2.2  skrll 	}
   8628  1.1.2.2  skrll 	sctppcbinfo.ipi_count_chunk++;
   8629  1.1.2.2  skrll 	sctppcbinfo.ipi_gencnt_chunk++;
   8630  1.1.2.2  skrll 	chk->rec.chunk_id = SCTP_ECN_CWR;
   8631  1.1.2.2  skrll 	chk->asoc = &stcb->asoc;
   8632  1.1.2.2  skrll 	chk->send_size = sizeof(struct sctp_cwr_chunk);
   8633  1.1.2.2  skrll 	MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
   8634  1.1.2.2  skrll 	if (chk->data == NULL) {
   8635  1.1.2.2  skrll 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   8636  1.1.2.2  skrll 		sctppcbinfo.ipi_count_chunk--;
   8637  1.1.2.2  skrll 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   8638  1.1.2.2  skrll 			panic("Chunk count is negative");
   8639  1.1.2.2  skrll 		}
   8640  1.1.2.2  skrll 		sctppcbinfo.ipi_gencnt_chunk++;
   8641  1.1.2.2  skrll 		return;
   8642  1.1.2.2  skrll 	}
   8643  1.1.2.2  skrll 	chk->data->m_data += SCTP_MIN_OVERHEAD;
   8644  1.1.2.2  skrll 	chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
   8645  1.1.2.2  skrll 	chk->sent = SCTP_DATAGRAM_UNSENT;
   8646  1.1.2.2  skrll 	chk->snd_count = 0;
   8647  1.1.2.2  skrll 	chk->whoTo = net;
   8648  1.1.2.2  skrll 	chk->whoTo->ref_count++;
   8649  1.1.2.2  skrll 	cwr = mtod(chk->data, struct sctp_cwr_chunk *);
   8650  1.1.2.2  skrll 	cwr->ch.chunk_type = SCTP_ECN_CWR;
   8651  1.1.2.2  skrll 	cwr->ch.chunk_flags = 0;
   8652  1.1.2.2  skrll 	cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
   8653  1.1.2.2  skrll 	cwr->tsn = htonl(high_tsn);
   8654  1.1.2.2  skrll 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
   8655  1.1.2.2  skrll 	asoc->ctrl_queue_cnt++;
   8656  1.1.2.2  skrll }
   8657  1.1.2.2  skrll static void
   8658  1.1.2.2  skrll sctp_reset_the_streams(struct sctp_tcb *stcb,
   8659  1.1.2.2  skrll      struct sctp_stream_reset_request *req, int number_entries, uint16_t *list)
   8660  1.1.2.2  skrll {
   8661  1.1.2.2  skrll 	int i;
   8662  1.1.2.2  skrll 
   8663  1.1.2.2  skrll 	if (req->reset_flags & SCTP_RESET_ALL) {
   8664  1.1.2.2  skrll 		for (i=0; i<stcb->asoc.streamoutcnt; i++) {
   8665  1.1.2.2  skrll 			stcb->asoc.strmout[i].next_sequence_sent = 0;
   8666  1.1.2.2  skrll 		}
   8667  1.1.2.2  skrll 	} else if (number_entries) {
   8668  1.1.2.2  skrll 		for (i=0; i<number_entries; i++) {
   8669  1.1.2.2  skrll 			if (list[i] >= stcb->asoc.streamoutcnt) {
   8670  1.1.2.2  skrll 				/* no such stream */
   8671  1.1.2.2  skrll 				continue;
   8672  1.1.2.2  skrll 			}
   8673  1.1.2.2  skrll 			stcb->asoc.strmout[(list[i])].next_sequence_sent = 0;
   8674  1.1.2.2  skrll 		}
   8675  1.1.2.2  skrll 	}
   8676  1.1.2.2  skrll 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list);
   8677  1.1.2.2  skrll }
   8678  1.1.2.2  skrll 
   8679  1.1.2.2  skrll void
   8680  1.1.2.2  skrll sctp_send_str_reset_ack(struct sctp_tcb *stcb,
   8681  1.1.2.2  skrll      struct sctp_stream_reset_request *req)
   8682  1.1.2.2  skrll {
   8683  1.1.2.2  skrll 	struct sctp_association *asoc;
   8684  1.1.2.2  skrll 	struct sctp_stream_reset_resp *strack;
   8685  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   8686  1.1.2.2  skrll 	uint32_t seq;
   8687  1.1.2.2  skrll 	int number_entries, i;
   8688  1.1.2.2  skrll 	uint8_t two_way=0, not_peer=0;
   8689  1.1.2.2  skrll 	uint16_t *list=NULL;
   8690  1.1.2.2  skrll 
   8691  1.1.2.2  skrll 	asoc = &stcb->asoc;
   8692  1.1.2.2  skrll 	if (req->reset_flags & SCTP_RESET_ALL)
   8693  1.1.2.2  skrll 		number_entries = 0;
   8694  1.1.2.2  skrll 	else
   8695  1.1.2.2  skrll 		number_entries = (ntohs(req->ph.param_length) - sizeof(struct sctp_stream_reset_request)) / sizeof(uint16_t);
   8696  1.1.2.2  skrll 
   8697  1.1.2.2  skrll 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   8698  1.1.2.2  skrll 	if (chk == NULL) {
   8699  1.1.2.2  skrll 		return;
   8700  1.1.2.2  skrll 	}
   8701  1.1.2.2  skrll 	sctppcbinfo.ipi_count_chunk++;
   8702  1.1.2.2  skrll 	sctppcbinfo.ipi_gencnt_chunk++;
   8703  1.1.2.2  skrll 	chk->rec.chunk_id = SCTP_STREAM_RESET;
   8704  1.1.2.2  skrll 	chk->asoc = &stcb->asoc;
   8705  1.1.2.2  skrll 	chk->send_size = sizeof(struct sctp_stream_reset_resp) + (number_entries * sizeof(uint16_t));
   8706  1.1.2.2  skrll 	MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
   8707  1.1.2.2  skrll 	if (chk->data == NULL) {
   8708  1.1.2.2  skrll 	strresp_jump_out:
   8709  1.1.2.2  skrll 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   8710  1.1.2.2  skrll 		sctppcbinfo.ipi_count_chunk--;
   8711  1.1.2.2  skrll 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   8712  1.1.2.2  skrll 			panic("Chunk count is negative");
   8713  1.1.2.2  skrll 		}
   8714  1.1.2.2  skrll 		sctppcbinfo.ipi_gencnt_chunk++;
   8715  1.1.2.2  skrll 		return;
   8716  1.1.2.2  skrll 	}
   8717  1.1.2.2  skrll 	chk->data->m_data += SCTP_MIN_OVERHEAD;
   8718  1.1.2.2  skrll 	chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
   8719  1.1.2.2  skrll 	if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
   8720  1.1.2.2  skrll 		MCLGET(chk->data, M_DONTWAIT);
   8721  1.1.2.2  skrll 		if ((chk->data->m_flags & M_EXT) == 0) {
   8722  1.1.2.2  skrll 			/* Give up */
   8723  1.1.2.2  skrll 			sctp_m_freem(chk->data);
   8724  1.1.2.2  skrll 			chk->data = NULL;
   8725  1.1.2.2  skrll 			goto strresp_jump_out;
   8726  1.1.2.2  skrll 		}
   8727  1.1.2.2  skrll 		chk->data->m_data += SCTP_MIN_OVERHEAD;
   8728  1.1.2.2  skrll 	}
   8729  1.1.2.2  skrll 	if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
   8730  1.1.2.2  skrll 		/* can't do it, no room */
   8731  1.1.2.2  skrll 		/* Give up */
   8732  1.1.2.2  skrll 		sctp_m_freem(chk->data);
   8733  1.1.2.2  skrll 		chk->data = NULL;
   8734  1.1.2.2  skrll 		goto strresp_jump_out;
   8735  1.1.2.2  skrll 
   8736  1.1.2.2  skrll 	}
   8737  1.1.2.2  skrll 	chk->sent = SCTP_DATAGRAM_UNSENT;
   8738  1.1.2.2  skrll 	chk->snd_count = 0;
   8739  1.1.2.2  skrll 	chk->whoTo = asoc->primary_destination;
   8740  1.1.2.2  skrll 	chk->whoTo->ref_count++;
   8741  1.1.2.2  skrll 	strack = mtod(chk->data, struct sctp_stream_reset_resp *);
   8742  1.1.2.2  skrll 
   8743  1.1.2.2  skrll 	strack->ch.chunk_type = SCTP_STREAM_RESET;
   8744  1.1.2.2  skrll 	strack->ch.chunk_flags = 0;
   8745  1.1.2.2  skrll 	strack->ch.chunk_length = htons(chk->send_size);
   8746  1.1.2.2  skrll 
   8747  1.1.2.2  skrll 	memset(strack->sr_resp.reset_pad, 0, sizeof(strack->sr_resp.reset_pad));
   8748  1.1.2.2  skrll 
   8749  1.1.2.2  skrll 	strack->sr_resp.ph.param_type = ntohs(SCTP_STR_RESET_RESPONSE);
   8750  1.1.2.2  skrll 	strack->sr_resp.ph.param_length = htons((chk->send_size - sizeof(struct sctp_chunkhdr)));
   8751  1.1.2.2  skrll 
   8752  1.1.2.2  skrll 
   8753  1.1.2.2  skrll 
   8754  1.1.2.2  skrll 	if (chk->send_size % 4) {
   8755  1.1.2.2  skrll 		/* need a padding for the end */
   8756  1.1.2.2  skrll 		int pad;
   8757  1.1.2.2  skrll 		uint8_t *end;
   8758  1.1.2.2  skrll 		end = (uint8_t *)((vaddr_t)strack + chk->send_size);
   8759  1.1.2.2  skrll 		pad = chk->send_size % 4;
   8760  1.1.2.2  skrll 		for (i = 0; i < pad; i++) {
   8761  1.1.2.2  skrll 			end[i] = 0;
   8762  1.1.2.2  skrll 		}
   8763  1.1.2.2  skrll 		chk->send_size += pad;
   8764  1.1.2.2  skrll 	}
   8765  1.1.2.2  skrll 
   8766  1.1.2.2  skrll         /* actual response */
   8767  1.1.2.2  skrll 	if (req->reset_flags & SCTP_RESET_YOUR) {
   8768  1.1.2.2  skrll 		strack->sr_resp.reset_flags = SCTP_RESET_PERFORMED;
   8769  1.1.2.2  skrll 	} else {
   8770  1.1.2.2  skrll 		strack->sr_resp.reset_flags = 0;
   8771  1.1.2.2  skrll 	}
   8772  1.1.2.2  skrll 
   8773  1.1.2.2  skrll 	/* copied from reset request */
   8774  1.1.2.2  skrll 	strack->sr_resp.reset_req_seq_resp = req->reset_req_seq;
   8775  1.1.2.2  skrll 	seq = ntohl(req->reset_req_seq);
   8776  1.1.2.2  skrll 
   8777  1.1.2.2  skrll 	list = req->list_of_streams;
   8778  1.1.2.2  skrll 	/* copy the un-converted network byte order streams */
   8779  1.1.2.2  skrll 	for (i=0; i<number_entries; i++) {
   8780  1.1.2.2  skrll 		strack->sr_resp.list_of_streams[i] = list[i];
   8781  1.1.2.2  skrll 	}
   8782  1.1.2.2  skrll 	if (asoc->str_reset_seq_in == seq) {
   8783  1.1.2.2  skrll 		/* is it the next expected? */
   8784  1.1.2.2  skrll 		asoc->str_reset_seq_in++;
   8785  1.1.2.2  skrll 		strack->sr_resp.reset_at_tsn = htonl(asoc->sending_seq);
   8786  1.1.2.2  skrll 		asoc->str_reset_sending_seq = asoc->sending_seq;
   8787  1.1.2.2  skrll 		if (number_entries) {
   8788  1.1.2.2  skrll 			uint16_t temp;
   8789  1.1.2.2  skrll 			/* convert them to host byte order */
   8790  1.1.2.2  skrll 			for (i=0 ; i<number_entries; i++) {
   8791  1.1.2.2  skrll 				temp = ntohs(list[i]);
   8792  1.1.2.2  skrll 				list[i] = temp;
   8793  1.1.2.2  skrll 			}
   8794  1.1.2.2  skrll 		}
   8795  1.1.2.2  skrll 		if (req->reset_flags & SCTP_RESET_YOUR) {
   8796  1.1.2.2  skrll 			/* reset my outbound streams */
   8797  1.1.2.2  skrll 			sctp_reset_the_streams(stcb, req , number_entries, list);
   8798  1.1.2.2  skrll 		}
   8799  1.1.2.2  skrll 		if (req->reset_flags & SCTP_RECIPRICAL) {
   8800  1.1.2.2  skrll 			/* reset peer too */
   8801  1.1.2.2  skrll 			sctp_send_str_reset_req(stcb, number_entries, list, two_way, not_peer);
   8802  1.1.2.2  skrll 		}
   8803  1.1.2.2  skrll 
   8804  1.1.2.2  skrll 	} else {
   8805  1.1.2.2  skrll 		/* no its a retran so I must just ack and do nothing */
   8806  1.1.2.2  skrll 		strack->sr_resp.reset_at_tsn = htonl(asoc->str_reset_sending_seq);
   8807  1.1.2.2  skrll 	}
   8808  1.1.2.2  skrll 	strack->sr_resp.cumulative_tsn = htonl(asoc->cumulative_tsn);
   8809  1.1.2.2  skrll 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
   8810  1.1.2.2  skrll 			  chk,
   8811  1.1.2.2  skrll 			  sctp_next);
   8812  1.1.2.2  skrll 	asoc->ctrl_queue_cnt++;
   8813  1.1.2.2  skrll }
   8814  1.1.2.2  skrll 
   8815  1.1.2.2  skrll 
   8816  1.1.2.2  skrll void
   8817  1.1.2.2  skrll sctp_send_str_reset_req(struct sctp_tcb *stcb,
   8818  1.1.2.2  skrll      int number_entrys, uint16_t *list, uint8_t two_way, uint8_t not_peer)
   8819  1.1.2.2  skrll {
   8820  1.1.2.2  skrll 	/* Send a stream reset request. The number_entrys may be 0 and list NULL
   8821  1.1.2.2  skrll 	 * if the request is to reset all streams. If two_way is true then we
   8822  1.1.2.2  skrll 	 * not only request a RESET of the received streams but we also
   8823  1.1.2.2  skrll 	 * request the peer to send a reset req to us too.
   8824  1.1.2.2  skrll 	 * Flag combinations in table:
   8825  1.1.2.2  skrll 	 *
   8826  1.1.2.2  skrll 	 *       two_way | not_peer  | = | Flags
   8827  1.1.2.2  skrll 	 *       ------------------------------
   8828  1.1.2.2  skrll 	 *         0     |    0      | = | SCTP_RESET_YOUR (just the peer)
   8829  1.1.2.2  skrll 	 *         1     |    0      | = | SCTP_RESET_YOUR | SCTP_RECIPRICAL (both sides)
   8830  1.1.2.2  skrll 	 *         0     |    1      | = | Not a Valid Request (not anyone)
   8831  1.1.2.2  skrll 	 *         1     |    1      | = | SCTP_RESET_RECIPRICAL (Just local host)
   8832  1.1.2.2  skrll 	 */
   8833  1.1.2.2  skrll 	struct sctp_association *asoc;
   8834  1.1.2.2  skrll 	struct sctp_stream_reset_req *strreq;
   8835  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   8836  1.1.2.2  skrll 
   8837  1.1.2.2  skrll 
   8838  1.1.2.2  skrll 	asoc = &stcb->asoc;
   8839  1.1.2.2  skrll 	if (asoc->stream_reset_outstanding) {
   8840  1.1.2.2  skrll 		/* Already one pending, must get ACK back
   8841  1.1.2.2  skrll 		 * to clear the flag.
   8842  1.1.2.2  skrll 		 */
   8843  1.1.2.2  skrll 		return;
   8844  1.1.2.2  skrll 	}
   8845  1.1.2.2  skrll 
   8846  1.1.2.2  skrll 	if ((two_way == 0) && (not_peer == 1)) {
   8847  1.1.2.2  skrll 		/* not a valid request */
   8848  1.1.2.2  skrll 		return;
   8849  1.1.2.2  skrll 	}
   8850  1.1.2.2  skrll 
   8851  1.1.2.2  skrll 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   8852  1.1.2.2  skrll 	if (chk == NULL) {
   8853  1.1.2.2  skrll 		return;
   8854  1.1.2.2  skrll 	}
   8855  1.1.2.2  skrll 	sctppcbinfo.ipi_count_chunk++;
   8856  1.1.2.2  skrll 	sctppcbinfo.ipi_gencnt_chunk++;
   8857  1.1.2.2  skrll 	chk->rec.chunk_id = SCTP_STREAM_RESET;
   8858  1.1.2.2  skrll 	chk->asoc = &stcb->asoc;
   8859  1.1.2.2  skrll 	chk->send_size = sizeof(struct sctp_stream_reset_req) + (number_entrys * sizeof(uint16_t));
   8860  1.1.2.2  skrll 	MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
   8861  1.1.2.2  skrll 	if (chk->data == NULL) {
   8862  1.1.2.2  skrll 	strreq_jump_out:
   8863  1.1.2.2  skrll 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   8864  1.1.2.2  skrll 		sctppcbinfo.ipi_count_chunk--;
   8865  1.1.2.2  skrll 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   8866  1.1.2.2  skrll 			panic("Chunk count is negative");
   8867  1.1.2.2  skrll 		}
   8868  1.1.2.2  skrll 		sctppcbinfo.ipi_gencnt_chunk++;
   8869  1.1.2.2  skrll 		return;
   8870  1.1.2.2  skrll 	}
   8871  1.1.2.2  skrll 	chk->data->m_data += SCTP_MIN_OVERHEAD;
   8872  1.1.2.2  skrll 	chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
   8873  1.1.2.2  skrll 	if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
   8874  1.1.2.2  skrll 		MCLGET(chk->data, M_DONTWAIT);
   8875  1.1.2.2  skrll 		if ((chk->data->m_flags & M_EXT) == 0) {
   8876  1.1.2.2  skrll 			/* Give up */
   8877  1.1.2.2  skrll 			sctp_m_freem(chk->data);
   8878  1.1.2.2  skrll 			chk->data = NULL;
   8879  1.1.2.2  skrll 			goto strreq_jump_out;
   8880  1.1.2.2  skrll 		}
   8881  1.1.2.2  skrll 		chk->data->m_data += SCTP_MIN_OVERHEAD;
   8882  1.1.2.2  skrll 	}
   8883  1.1.2.2  skrll 	if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
   8884  1.1.2.2  skrll 		/* can't do it, no room */
   8885  1.1.2.2  skrll 		/* Give up */
   8886  1.1.2.2  skrll 		sctp_m_freem(chk->data);
   8887  1.1.2.2  skrll 		chk->data = NULL;
   8888  1.1.2.2  skrll 		goto strreq_jump_out;
   8889  1.1.2.2  skrll 	}
   8890  1.1.2.2  skrll 	chk->sent = SCTP_DATAGRAM_UNSENT;
   8891  1.1.2.2  skrll 	chk->snd_count = 0;
   8892  1.1.2.2  skrll 	chk->whoTo = asoc->primary_destination;
   8893  1.1.2.2  skrll 	chk->whoTo->ref_count++;
   8894  1.1.2.2  skrll 
   8895  1.1.2.2  skrll 	strreq = mtod(chk->data, struct sctp_stream_reset_req *);
   8896  1.1.2.2  skrll 	strreq->ch.chunk_type = SCTP_STREAM_RESET;
   8897  1.1.2.2  skrll 	strreq->ch.chunk_flags = 0;
   8898  1.1.2.2  skrll 	strreq->ch.chunk_length = htons(chk->send_size);
   8899  1.1.2.2  skrll 
   8900  1.1.2.2  skrll 	strreq->sr_req.ph.param_type = ntohs(SCTP_STR_RESET_REQUEST);
   8901  1.1.2.2  skrll 	strreq->sr_req.ph.param_length = htons((chk->send_size - sizeof(struct sctp_chunkhdr)));
   8902  1.1.2.2  skrll 
   8903  1.1.2.2  skrll 	if (chk->send_size % 4) {
   8904  1.1.2.2  skrll 		/* need a padding for the end */
   8905  1.1.2.2  skrll 		int pad, i;
   8906  1.1.2.2  skrll 		uint8_t *end;
   8907  1.1.2.2  skrll 		end = (uint8_t *)((vaddr_t)strreq + chk->send_size);
   8908  1.1.2.2  skrll 		pad = chk->send_size % 4;
   8909  1.1.2.2  skrll 		for (i=0; i<pad; i++) {
   8910  1.1.2.2  skrll 			end[i] = 0;
   8911  1.1.2.2  skrll 		}
   8912  1.1.2.2  skrll 		chk->send_size += pad;
   8913  1.1.2.2  skrll 	}
   8914  1.1.2.2  skrll 
   8915  1.1.2.2  skrll 	strreq->sr_req.reset_flags = 0;
   8916  1.1.2.2  skrll 	if (number_entrys == 0) {
   8917  1.1.2.2  skrll 		strreq->sr_req.reset_flags |= SCTP_RESET_ALL;
   8918  1.1.2.2  skrll 	}
   8919  1.1.2.2  skrll 	if (two_way == 0) {
   8920  1.1.2.2  skrll 		strreq->sr_req.reset_flags |= SCTP_RESET_YOUR;
   8921  1.1.2.2  skrll 	} else {
   8922  1.1.2.2  skrll 		if (not_peer == 0) {
   8923  1.1.2.2  skrll 			strreq->sr_req.reset_flags |= SCTP_RECIPRICAL | SCTP_RESET_YOUR;
   8924  1.1.2.2  skrll 		} else {
   8925  1.1.2.2  skrll 			strreq->sr_req.reset_flags |= SCTP_RECIPRICAL;
   8926  1.1.2.2  skrll 		}
   8927  1.1.2.2  skrll 	}
   8928  1.1.2.2  skrll 	memset(strreq->sr_req.reset_pad, 0, sizeof(strreq->sr_req.reset_pad));
   8929  1.1.2.2  skrll 	strreq->sr_req.reset_req_seq = htonl(asoc->str_reset_seq_out);
   8930  1.1.2.2  skrll 	if (number_entrys) {
   8931  1.1.2.2  skrll 		/* populate the specific entry's */
   8932  1.1.2.2  skrll 		int i;
   8933  1.1.2.2  skrll 		for (i=0; i < number_entrys; i++) {
   8934  1.1.2.2  skrll 			strreq->sr_req.list_of_streams[i] = htons(list[i]);
   8935  1.1.2.2  skrll 		}
   8936  1.1.2.2  skrll 	}
   8937  1.1.2.2  skrll 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
   8938  1.1.2.2  skrll 			  chk,
   8939  1.1.2.2  skrll 			  sctp_next);
   8940  1.1.2.2  skrll 	asoc->ctrl_queue_cnt++;
   8941  1.1.2.2  skrll 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
   8942  1.1.2.2  skrll 	asoc->stream_reset_outstanding = 1;
   8943  1.1.2.2  skrll }
   8944  1.1.2.2  skrll 
   8945  1.1.2.2  skrll void
   8946  1.1.2.2  skrll sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
   8947  1.1.2.2  skrll     struct mbuf *err_cause)
   8948  1.1.2.2  skrll {
   8949  1.1.2.2  skrll 	/*
   8950  1.1.2.2  skrll 	 * Formulate the abort message, and send it back down.
   8951  1.1.2.2  skrll 	 */
   8952  1.1.2.2  skrll 	struct mbuf *mout;
   8953  1.1.2.2  skrll 	struct sctp_abort_msg *abm;
   8954  1.1.2.2  skrll 	struct ip *iph, *iph_out;
   8955  1.1.2.2  skrll 	struct ip6_hdr *ip6, *ip6_out;
   8956  1.1.2.2  skrll 	int iphlen_out;
   8957  1.1.2.2  skrll 
   8958  1.1.2.2  skrll 	/* don't respond to ABORT with ABORT */
   8959  1.1.2.2  skrll 	if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
   8960  1.1.2.2  skrll 		if (err_cause)
   8961  1.1.2.2  skrll 			sctp_m_freem(err_cause);
   8962  1.1.2.2  skrll 		return;
   8963  1.1.2.2  skrll 	}
   8964  1.1.2.2  skrll 	MGETHDR(mout, M_DONTWAIT, MT_HEADER);
   8965  1.1.2.2  skrll 	if (mout == NULL) {
   8966  1.1.2.2  skrll 		if (err_cause)
   8967  1.1.2.2  skrll 			sctp_m_freem(err_cause);
   8968  1.1.2.2  skrll 		return;
   8969  1.1.2.2  skrll 	}
   8970  1.1.2.2  skrll 	iph = mtod(m, struct ip *);
   8971  1.1.2.2  skrll 	iph_out = NULL;
   8972  1.1.2.2  skrll 	ip6_out = NULL;
   8973  1.1.2.2  skrll 	if (iph->ip_v == IPVERSION) {
   8974  1.1.2.2  skrll 		iph_out = mtod(mout, struct ip *);
   8975  1.1.2.2  skrll 		mout->m_len = sizeof(*iph_out) + sizeof(*abm);
   8976  1.1.2.2  skrll 		mout->m_next = err_cause;
   8977  1.1.2.2  skrll 
   8978  1.1.2.2  skrll 		/* Fill in the IP header for the ABORT */
   8979  1.1.2.2  skrll 		iph_out->ip_v = IPVERSION;
   8980  1.1.2.2  skrll 		iph_out->ip_hl = (sizeof(struct ip) / 4);
   8981  1.1.2.2  skrll 		iph_out->ip_tos = (u_char)0;
   8982  1.1.2.2  skrll 		iph_out->ip_id = 0;
   8983  1.1.2.2  skrll 		iph_out->ip_off = 0;
   8984  1.1.2.2  skrll 		iph_out->ip_ttl = MAXTTL;
   8985  1.1.2.2  skrll 		iph_out->ip_p = IPPROTO_SCTP;
   8986  1.1.2.2  skrll 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
   8987  1.1.2.2  skrll 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
   8988  1.1.2.2  skrll 		/* let IP layer calculate this */
   8989  1.1.2.2  skrll 		iph_out->ip_sum = 0;
   8990  1.1.2.2  skrll 
   8991  1.1.2.2  skrll 		iphlen_out = sizeof(*iph_out);
   8992  1.1.2.2  skrll 		abm = (struct sctp_abort_msg *)((vaddr_t)iph_out + iphlen_out);
   8993  1.1.2.2  skrll 	} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
   8994  1.1.2.2  skrll 		ip6 = (struct ip6_hdr *)iph;
   8995  1.1.2.2  skrll 		ip6_out = mtod(mout, struct ip6_hdr *);
   8996  1.1.2.2  skrll 		mout->m_len = sizeof(*ip6_out) + sizeof(*abm);
   8997  1.1.2.2  skrll 		mout->m_next = err_cause;
   8998  1.1.2.2  skrll 
   8999  1.1.2.2  skrll 		/* Fill in the IP6 header for the ABORT */
   9000  1.1.2.2  skrll 		ip6_out->ip6_flow = ip6->ip6_flow;
   9001  1.1.2.2  skrll 		ip6_out->ip6_hlim = ip6_defhlim;
   9002  1.1.2.2  skrll 		ip6_out->ip6_nxt = IPPROTO_SCTP;
   9003  1.1.2.2  skrll 		ip6_out->ip6_src = ip6->ip6_dst;
   9004  1.1.2.2  skrll 		ip6_out->ip6_dst = ip6->ip6_src;
   9005  1.1.2.2  skrll 
   9006  1.1.2.2  skrll 		iphlen_out = sizeof(*ip6_out);
   9007  1.1.2.2  skrll 		abm = (struct sctp_abort_msg *)((vaddr_t)ip6_out + iphlen_out);
   9008  1.1.2.2  skrll 	} else {
   9009  1.1.2.2  skrll 		/* Currently not supported */
   9010  1.1.2.2  skrll 		return;
   9011  1.1.2.2  skrll 	}
   9012  1.1.2.2  skrll 
   9013  1.1.2.2  skrll 	abm->sh.src_port = sh->dest_port;
   9014  1.1.2.2  skrll 	abm->sh.dest_port = sh->src_port;
   9015  1.1.2.2  skrll 	abm->sh.checksum = 0;
   9016  1.1.2.2  skrll 	if (vtag == 0) {
   9017  1.1.2.2  skrll 		abm->sh.v_tag = sh->v_tag;
   9018  1.1.2.2  skrll 		abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
   9019  1.1.2.2  skrll 	} else {
   9020  1.1.2.2  skrll 		abm->sh.v_tag = htonl(vtag);
   9021  1.1.2.2  skrll 		abm->msg.ch.chunk_flags = 0;
   9022  1.1.2.2  skrll 	}
   9023  1.1.2.2  skrll 	abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
   9024  1.1.2.2  skrll 
   9025  1.1.2.2  skrll 	if (err_cause) {
   9026  1.1.2.2  skrll 		struct mbuf *m_tmp = err_cause;
   9027  1.1.2.2  skrll 		int err_len = 0;
   9028  1.1.2.2  skrll 		/* get length of the err_cause chain */
   9029  1.1.2.2  skrll 		while (m_tmp != NULL) {
   9030  1.1.2.2  skrll 			err_len += m_tmp->m_len;
   9031  1.1.2.2  skrll 			m_tmp = m_tmp->m_next;
   9032  1.1.2.2  skrll 		}
   9033  1.1.2.2  skrll 		mout->m_pkthdr.len = mout->m_len + err_len;
   9034  1.1.2.2  skrll 		if (err_len % 4) {
   9035  1.1.2.2  skrll 			/* need pad at end of chunk */
   9036  1.1.2.2  skrll 			u_int32_t cpthis=0;
   9037  1.1.2.2  skrll 			int padlen;
   9038  1.1.2.2  skrll 			padlen = 4 - (mout->m_pkthdr.len % 4);
   9039  1.1.2.2  skrll 			m_copyback(mout, mout->m_pkthdr.len, padlen, (void *)&cpthis);
   9040  1.1.2.2  skrll 		}
   9041  1.1.2.2  skrll 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
   9042  1.1.2.2  skrll 	} else {
   9043  1.1.2.2  skrll 		mout->m_pkthdr.len = mout->m_len;
   9044  1.1.2.2  skrll 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
   9045  1.1.2.2  skrll 	}
   9046  1.1.2.2  skrll 
   9047  1.1.2.2  skrll 	/* add checksum */
   9048  1.1.2.5  skrll 	if ((sctp_no_csum_on_loopback) && m_get_rcvif_NOMPSAFE(m) != NULL &&
   9049  1.1.2.5  skrll 	    m_get_rcvif_NOMPSAFE(m)->if_type == IFT_LOOP) {
   9050  1.1.2.2  skrll 		abm->sh.checksum =  0;
   9051  1.1.2.2  skrll 	} else {
   9052  1.1.2.2  skrll 		abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
   9053  1.1.2.2  skrll 	}
   9054  1.1.2.2  skrll 
   9055  1.1.2.2  skrll 	/* zap the rcvif, it should be null */
   9056  1.1.2.5  skrll 	m_reset_rcvif(mout);
   9057  1.1.2.2  skrll 	if (iph_out != NULL) {
   9058  1.1.2.2  skrll 		struct route ro;
   9059  1.1.2.2  skrll 
   9060  1.1.2.2  skrll 		/* zap the stack pointer to the route */
   9061  1.1.2.2  skrll 		memset(&ro, 0, sizeof ro);
   9062  1.1.2.2  skrll #ifdef SCTP_DEBUG
   9063  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   9064  1.1.2.2  skrll                         printf("sctp_send_abort calling ip_output:\n");
   9065  1.1.2.2  skrll 			sctp_print_address_pkt(iph_out, &abm->sh);
   9066  1.1.2.2  skrll                 }
   9067  1.1.2.2  skrll #endif
   9068  1.1.2.2  skrll 		/* set IPv4 length */
   9069  1.1.2.2  skrll 		iph_out->ip_len = htons(mout->m_pkthdr.len);
   9070  1.1.2.2  skrll 		/* out it goes */
   9071  1.1.2.2  skrll 		(void)ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL, NULL);
   9072  1.1.2.2  skrll 	} else if (ip6_out != NULL) {
   9073  1.1.2.2  skrll 		struct route ro;
   9074  1.1.2.2  skrll 
   9075  1.1.2.2  skrll 		/* zap the stack pointer to the route */
   9076  1.1.2.2  skrll 		memset(&ro, 0, sizeof(ro));
   9077  1.1.2.2  skrll #ifdef SCTP_DEBUG
   9078  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   9079  1.1.2.2  skrll                         printf("sctp_send_abort calling ip6_output:\n");
   9080  1.1.2.2  skrll 			sctp_print_address_pkt((struct ip *)ip6_out, &abm->sh);
   9081  1.1.2.2  skrll                 }
   9082  1.1.2.2  skrll #endif
   9083  1.1.2.2  skrll 		ip6_output(mout, NULL, &ro, 0, NULL, NULL, NULL);
   9084  1.1.2.2  skrll 	}
   9085  1.1.2.2  skrll         sctp_pegs[SCTP_DATAGRAMS_SENT]++;
   9086  1.1.2.2  skrll }
   9087  1.1.2.2  skrll 
   9088  1.1.2.2  skrll void
   9089  1.1.2.2  skrll sctp_send_operr_to(struct mbuf *m, int iphlen,
   9090  1.1.2.2  skrll 		   struct mbuf *scm,
   9091  1.1.2.2  skrll 		   uint32_t vtag)
   9092  1.1.2.2  skrll {
   9093  1.1.2.2  skrll 	struct sctphdr *ihdr;
   9094  1.1.2.2  skrll 	struct sctphdr *ohdr;
   9095  1.1.2.2  skrll 	struct sctp_chunkhdr *ophdr;
   9096  1.1.2.2  skrll 
   9097  1.1.2.2  skrll 	struct ip *iph;
   9098  1.1.2.2  skrll #ifdef SCTP_DEBUG
   9099  1.1.2.2  skrll 	struct sockaddr_in6 lsa6, fsa6;
   9100  1.1.2.2  skrll #endif
   9101  1.1.2.2  skrll 	uint32_t val;
   9102  1.1.2.2  skrll 	iph = mtod(m, struct ip *);
   9103  1.1.2.2  skrll 	ihdr = (struct sctphdr *)((vaddr_t)iph + iphlen);
   9104  1.1.2.2  skrll 	if (!(scm->m_flags & M_PKTHDR)) {
   9105  1.1.2.2  skrll 		/* must be a pkthdr */
   9106  1.1.2.2  skrll 		printf("Huh, not a packet header in send_operr\n");
   9107  1.1.2.2  skrll 		m_freem(scm);
   9108  1.1.2.2  skrll 		return;
   9109  1.1.2.2  skrll 	}
   9110  1.1.2.2  skrll 	M_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), M_DONTWAIT);
   9111  1.1.2.2  skrll 	if (scm == NULL) {
   9112  1.1.2.2  skrll 		/* can't send because we can't add a mbuf */
   9113  1.1.2.2  skrll 		return;
   9114  1.1.2.2  skrll 	}
   9115  1.1.2.2  skrll 	ohdr = mtod(scm, struct sctphdr *);
   9116  1.1.2.2  skrll 	ohdr->src_port = ihdr->dest_port;
   9117  1.1.2.2  skrll 	ohdr->dest_port = ihdr->src_port;
   9118  1.1.2.2  skrll 	ohdr->v_tag = vtag;
   9119  1.1.2.2  skrll 	ohdr->checksum = 0;
   9120  1.1.2.2  skrll 	ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
   9121  1.1.2.2  skrll 	ophdr->chunk_type = SCTP_OPERATION_ERROR;
   9122  1.1.2.2  skrll 	ophdr->chunk_flags = 0;
   9123  1.1.2.2  skrll 	ophdr->chunk_length = htons(scm->m_pkthdr.len - sizeof(struct sctphdr));
   9124  1.1.2.2  skrll 	if (scm->m_pkthdr.len % 4) {
   9125  1.1.2.2  skrll 		/* need padding */
   9126  1.1.2.2  skrll 		u_int32_t cpthis=0;
   9127  1.1.2.2  skrll 		int padlen;
   9128  1.1.2.2  skrll 		padlen = 4 - (scm->m_pkthdr.len % 4);
   9129  1.1.2.2  skrll 		m_copyback(scm, scm->m_pkthdr.len, padlen, (void *)&cpthis);
   9130  1.1.2.2  skrll 	}
   9131  1.1.2.5  skrll 	if ((sctp_no_csum_on_loopback) && m_get_rcvif_NOMPSAFE(m) != NULL &&
   9132  1.1.2.5  skrll 	    m_get_rcvif_NOMPSAFE(m)->if_type == IFT_LOOP) {
   9133  1.1.2.2  skrll 		val = 0;
   9134  1.1.2.2  skrll 	} else {
   9135  1.1.2.2  skrll 		val = sctp_calculate_sum(scm, NULL, 0);
   9136  1.1.2.2  skrll 	}
   9137  1.1.2.2  skrll 	ohdr->checksum = val;
   9138  1.1.2.2  skrll 	if (iph->ip_v == IPVERSION) {
   9139  1.1.2.2  skrll 		/* V4 */
   9140  1.1.2.2  skrll 		struct ip *out;
   9141  1.1.2.2  skrll 		struct route ro;
   9142  1.1.2.2  skrll 		M_PREPEND(scm, sizeof(struct ip), M_DONTWAIT);
   9143  1.1.2.2  skrll 		if (scm == NULL)
   9144  1.1.2.2  skrll 			return;
   9145  1.1.2.2  skrll 		memset(&ro, 0, sizeof ro);
   9146  1.1.2.2  skrll 		out = mtod(scm, struct ip *);
   9147  1.1.2.2  skrll 		out->ip_v = iph->ip_v;
   9148  1.1.2.2  skrll 		out->ip_hl = (sizeof(struct ip)/4);
   9149  1.1.2.2  skrll 		out->ip_tos = iph->ip_tos;
   9150  1.1.2.2  skrll 		out->ip_id = iph->ip_id;
   9151  1.1.2.2  skrll 		out->ip_off = 0;
   9152  1.1.2.2  skrll 		out->ip_ttl = MAXTTL;
   9153  1.1.2.2  skrll 		out->ip_p = IPPROTO_SCTP;
   9154  1.1.2.2  skrll 		out->ip_sum = 0;
   9155  1.1.2.2  skrll 		out->ip_src = iph->ip_dst;
   9156  1.1.2.2  skrll 		out->ip_dst = iph->ip_src;
   9157  1.1.2.2  skrll 		out->ip_len = htons(scm->m_pkthdr.len);
   9158  1.1.2.2  skrll 		ip_output(scm, 0, &ro, IP_RAWOUTPUT, NULL, NULL);
   9159  1.1.2.2  skrll 		sctp_pegs[SCTP_DATAGRAMS_SENT]++;
   9160  1.1.2.2  skrll 	} else {
   9161  1.1.2.2  skrll 		/* V6 */
   9162  1.1.2.2  skrll 		struct route ro;
   9163  1.1.2.2  skrll 		struct ip6_hdr *out6, *in6;
   9164  1.1.2.2  skrll 
   9165  1.1.2.2  skrll 		M_PREPEND(scm, sizeof(struct ip6_hdr), M_DONTWAIT);
   9166  1.1.2.2  skrll 		if (scm == NULL)
   9167  1.1.2.2  skrll 			return;
   9168  1.1.2.2  skrll 		memset(&ro, 0, sizeof ro);
   9169  1.1.2.2  skrll 		in6 = mtod(m, struct ip6_hdr *);
   9170  1.1.2.2  skrll 		out6 = mtod(scm, struct ip6_hdr *);
   9171  1.1.2.2  skrll 		out6->ip6_flow = in6->ip6_flow;
   9172  1.1.2.2  skrll 		out6->ip6_hlim = ip6_defhlim;
   9173  1.1.2.2  skrll 		out6->ip6_nxt = IPPROTO_SCTP;
   9174  1.1.2.2  skrll 		out6->ip6_src = in6->ip6_dst;
   9175  1.1.2.2  skrll 		out6->ip6_dst = in6->ip6_src;
   9176  1.1.2.2  skrll 
   9177  1.1.2.2  skrll #ifdef SCTP_DEBUG
   9178  1.1.2.2  skrll 		memset(&lsa6, 0, sizeof(lsa6));
   9179  1.1.2.2  skrll 		lsa6.sin6_len = sizeof(lsa6);
   9180  1.1.2.2  skrll 		lsa6.sin6_family = AF_INET6;
   9181  1.1.2.2  skrll 		lsa6.sin6_addr = out6->ip6_src;
   9182  1.1.2.2  skrll 		memset(&fsa6, 0, sizeof(fsa6));
   9183  1.1.2.2  skrll 		fsa6.sin6_len = sizeof(fsa6);
   9184  1.1.2.2  skrll 		fsa6.sin6_family = AF_INET6;
   9185  1.1.2.2  skrll 		fsa6.sin6_addr = out6->ip6_dst;
   9186  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   9187  1.1.2.2  skrll 			printf("sctp_operr_to calling ipv6 output:\n");
   9188  1.1.2.2  skrll 			printf("src: ");
   9189  1.1.2.2  skrll 			sctp_print_address((struct sockaddr *)&lsa6);
   9190  1.1.2.2  skrll 			printf("dst ");
   9191  1.1.2.2  skrll 			sctp_print_address((struct sockaddr *)&fsa6);
   9192  1.1.2.2  skrll 		}
   9193  1.1.2.2  skrll #endif /* SCTP_DEBUG */
   9194  1.1.2.2  skrll 		ip6_output(scm, NULL, &ro, 0, NULL, NULL, NULL);
   9195  1.1.2.2  skrll 		sctp_pegs[SCTP_DATAGRAMS_SENT]++;
   9196  1.1.2.2  skrll 	}
   9197  1.1.2.2  skrll }
   9198  1.1.2.2  skrll 
   9199  1.1.2.2  skrll static int
   9200  1.1.2.2  skrll sctp_copy_one(struct mbuf *m, struct uio *uio, int cpsz, int resv_upfront, int *mbcnt)
   9201  1.1.2.2  skrll {
   9202  1.1.2.2  skrll 	int left, cancpy, willcpy, error;
   9203  1.1.2.2  skrll 	left = cpsz;
   9204  1.1.2.2  skrll 
   9205  1.1.2.2  skrll 	if (m == NULL) {
   9206  1.1.2.2  skrll 		/* TSNH */
   9207  1.1.2.2  skrll 		*mbcnt = 0;
   9208  1.1.2.2  skrll 		return (ENOMEM);
   9209  1.1.2.2  skrll 	}
   9210  1.1.2.2  skrll 	m->m_len = 0;
   9211  1.1.2.2  skrll 	if ((left+resv_upfront) > (int)MHLEN) {
   9212  1.1.2.2  skrll 		MCLGET(m, M_WAIT);
   9213  1.1.2.2  skrll 		if (m == NULL) {
   9214  1.1.2.2  skrll 			*mbcnt = 0;
   9215  1.1.2.2  skrll 			return (ENOMEM);
   9216  1.1.2.2  skrll 		}
   9217  1.1.2.2  skrll 		if ((m->m_flags & M_EXT) == 0) {
   9218  1.1.2.2  skrll 			*mbcnt = 0;
   9219  1.1.2.2  skrll 			return (ENOMEM);
   9220  1.1.2.2  skrll 		}
   9221  1.1.2.2  skrll 		*mbcnt += m->m_ext.ext_size;
   9222  1.1.2.2  skrll 	}
   9223  1.1.2.2  skrll 	*mbcnt += MSIZE;
   9224  1.1.2.2  skrll 	cancpy = M_TRAILINGSPACE(m);
   9225  1.1.2.2  skrll 	willcpy = min(cancpy, left);
   9226  1.1.2.2  skrll 	if ((willcpy + resv_upfront) > cancpy) {
   9227  1.1.2.2  skrll 		willcpy -= resv_upfront;
   9228  1.1.2.2  skrll 	}
   9229  1.1.2.2  skrll 	while (left > 0) {
   9230  1.1.2.2  skrll 		/* Align data to the end */
   9231  1.1.2.2  skrll 		if ((m->m_flags & M_EXT) == 0) {
   9232  1.1.2.2  skrll 			if (m->m_flags & M_PKTHDR) {
   9233  1.1.2.2  skrll 				MH_ALIGN(m, willcpy);
   9234  1.1.2.2  skrll 			} else {
   9235  1.1.2.2  skrll 				M_ALIGN(m, willcpy);
   9236  1.1.2.2  skrll 			}
   9237  1.1.2.2  skrll 		} else {
   9238  1.1.2.2  skrll 			MC_ALIGN(m, willcpy);
   9239  1.1.2.2  skrll 		}
   9240  1.1.2.2  skrll 		error = uiomove(mtod(m, void *), willcpy, uio);
   9241  1.1.2.2  skrll 		if (error) {
   9242  1.1.2.2  skrll 			return (error);
   9243  1.1.2.2  skrll 		}
   9244  1.1.2.2  skrll 		m->m_len = willcpy;
   9245  1.1.2.2  skrll 		m->m_nextpkt = 0;
   9246  1.1.2.2  skrll 		left -= willcpy;
   9247  1.1.2.2  skrll 		if (left > 0) {
   9248  1.1.2.2  skrll 			MGET(m->m_next, M_WAIT, MT_DATA);
   9249  1.1.2.2  skrll 			if (m->m_next == NULL) {
   9250  1.1.2.2  skrll 				*mbcnt = 0;
   9251  1.1.2.2  skrll 				return (ENOMEM);
   9252  1.1.2.2  skrll 			}
   9253  1.1.2.2  skrll 			m = m->m_next;
   9254  1.1.2.2  skrll 			m->m_len = 0;
   9255  1.1.2.2  skrll 			*mbcnt += MSIZE;
   9256  1.1.2.2  skrll 			if (left > (int)MHLEN) {
   9257  1.1.2.2  skrll 				MCLGET(m, M_WAIT);
   9258  1.1.2.2  skrll 				if (m == NULL) {
   9259  1.1.2.2  skrll 					*mbcnt = 0;
   9260  1.1.2.2  skrll 					return (ENOMEM);
   9261  1.1.2.2  skrll 				}
   9262  1.1.2.2  skrll 				if ((m->m_flags & M_EXT) == 0) {
   9263  1.1.2.2  skrll 					*mbcnt = 0;
   9264  1.1.2.2  skrll 					return (ENOMEM);
   9265  1.1.2.2  skrll 				}
   9266  1.1.2.2  skrll 				*mbcnt += m->m_ext.ext_size;
   9267  1.1.2.2  skrll 			}
   9268  1.1.2.2  skrll 			cancpy = M_TRAILINGSPACE(m);
   9269  1.1.2.2  skrll 			willcpy = min(cancpy, left);
   9270  1.1.2.2  skrll 		}
   9271  1.1.2.2  skrll 	}
   9272  1.1.2.2  skrll 	return (0);
   9273  1.1.2.2  skrll }
   9274  1.1.2.2  skrll 
   9275  1.1.2.2  skrll static int
   9276  1.1.2.2  skrll sctp_copy_it_in(struct sctp_inpcb *inp,
   9277  1.1.2.2  skrll 		struct sctp_tcb *stcb,
   9278  1.1.2.2  skrll 		struct sctp_association *asoc,
   9279  1.1.2.2  skrll 		struct sctp_nets *net,
   9280  1.1.2.2  skrll 		struct sctp_sndrcvinfo *srcv,
   9281  1.1.2.2  skrll 		struct uio *uio,
   9282  1.1.2.2  skrll 		int flags)
   9283  1.1.2.2  skrll {
   9284  1.1.2.2  skrll 	/* This routine must be very careful in
   9285  1.1.2.2  skrll 	 * its work. Protocol processing is
   9286  1.1.2.2  skrll 	 * up and running so care must be taken to
   9287  1.1.2.2  skrll 	 * spl...() when you need to do something
   9288  1.1.2.2  skrll 	 * that may effect the stcb/asoc. The sb is
   9289  1.1.2.2  skrll 	 * locked however. When data is copied the
   9290  1.1.2.2  skrll 	 * protocol processing should be enabled since
   9291  1.1.2.2  skrll 	 * this is a slower operation...
   9292  1.1.2.2  skrll 	 */
   9293  1.1.2.2  skrll 	struct socket *so;
   9294  1.1.2.2  skrll 	int error = 0;
   9295  1.1.2.2  skrll 	int frag_size, mbcnt = 0, mbcnt_e = 0;
   9296  1.1.2.2  skrll 	unsigned int sndlen;
   9297  1.1.2.2  skrll 	unsigned int tot_demand;
   9298  1.1.2.2  skrll 	int tot_out, dataout;
   9299  1.1.2.2  skrll 	struct sctp_tmit_chunk *chk;
   9300  1.1.2.2  skrll 	struct mbuf *mm;
   9301  1.1.2.2  skrll 	struct sctp_stream_out *strq;
   9302  1.1.2.2  skrll 	uint32_t my_vtag;
   9303  1.1.2.2  skrll 	int resv_in_first;
   9304  1.1.2.2  skrll 
   9305  1.1.2.2  skrll 	so = stcb->sctp_socket;
   9306  1.1.2.2  skrll 	solock(so);
   9307  1.1.2.2  skrll 	chk = NULL;
   9308  1.1.2.2  skrll 	mm = NULL;
   9309  1.1.2.2  skrll 
   9310  1.1.2.2  skrll 	sndlen = uio->uio_resid;
   9311  1.1.2.2  skrll 	/* lock the socket buf */
   9312  1.1.2.2  skrll 	error = sblock(&so->so_snd, SBLOCKWAIT(flags));
   9313  1.1.2.2  skrll 	if (error)
   9314  1.1.2.2  skrll 		goto out_locked;
   9315  1.1.2.2  skrll 
   9316  1.1.2.2  skrll #ifdef SCTP_DEBUG
   9317  1.1.2.2  skrll 	printf("sctp_copy_it_in: %d\n", sndlen);
   9318  1.1.2.2  skrll #endif
   9319  1.1.2.2  skrll 	/* will it ever fit ? */
   9320  1.1.2.2  skrll 	if (sndlen > so->so_snd.sb_hiwat) {
   9321  1.1.2.2  skrll 		/* It will NEVER fit */
   9322  1.1.2.2  skrll 		error = EMSGSIZE;
   9323  1.1.2.2  skrll 		goto release;
   9324  1.1.2.2  skrll 	}
   9325  1.1.2.2  skrll 	/* Do I need to block? */
   9326  1.1.2.2  skrll 	if ((so->so_snd.sb_hiwat <
   9327  1.1.2.2  skrll 	    (sndlen + asoc->total_output_queue_size)) ||
   9328  1.1.2.2  skrll 	    (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
   9329  1.1.2.2  skrll 	    (asoc->total_output_mbuf_queue_size >
   9330  1.1.2.2  skrll 	    so->so_snd.sb_mbmax)
   9331  1.1.2.2  skrll 	) {
   9332  1.1.2.2  skrll 		/* prune any prsctp bufs out */
   9333  1.1.2.2  skrll 		if (asoc->peer_supports_prsctp) {
   9334  1.1.2.2  skrll 			sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
   9335  1.1.2.2  skrll 		}
   9336  1.1.2.2  skrll 		/*
   9337  1.1.2.2  skrll 		 * We store off a pointer to the endpoint.
   9338  1.1.2.2  skrll 		 * Since on return from this we must check to
   9339  1.1.2.2  skrll 		 * see if an so_error is set. If so we may have
   9340  1.1.2.2  skrll 		 * been reset and our stcb destroyed. Returning
   9341  1.1.2.2  skrll 		 * an error will flow back to the user...
   9342  1.1.2.2  skrll 		 */
   9343  1.1.2.2  skrll 		while ((so->so_snd.sb_hiwat <
   9344  1.1.2.2  skrll 		    (sndlen + asoc->total_output_queue_size)) ||
   9345  1.1.2.2  skrll 		    (asoc->chunks_on_out_queue >
   9346  1.1.2.2  skrll 		    sctp_max_chunks_on_queue) ||
   9347  1.1.2.2  skrll 		    (asoc->total_output_mbuf_queue_size >
   9348  1.1.2.2  skrll 		    so->so_snd.sb_mbmax)
   9349  1.1.2.2  skrll 		) {
   9350  1.1.2.2  skrll 			if ((so->so_state & SS_NBIO)
   9351  1.1.2.2  skrll #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
   9352  1.1.2.2  skrll 			    || (flags & MSG_NBIO)
   9353  1.1.2.2  skrll #endif
   9354  1.1.2.2  skrll 				) {
   9355  1.1.2.2  skrll 				/* Non-blocking io in place */
   9356  1.1.2.2  skrll 				error = EWOULDBLOCK;
   9357  1.1.2.2  skrll 				goto release;
   9358  1.1.2.2  skrll 			}
   9359  1.1.2.2  skrll 			inp->sctp_tcb_at_block = (void *)stcb;
   9360  1.1.2.2  skrll 			inp->error_on_block = 0;
   9361  1.1.2.2  skrll #ifdef SCTP_BLK_LOGGING
   9362  1.1.2.2  skrll 			sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
   9363  1.1.2.2  skrll 			    so, asoc);
   9364  1.1.2.2  skrll #endif
   9365  1.1.2.2  skrll 			sbunlock(&so->so_snd);
   9366  1.1.2.2  skrll 			SCTP_TCB_UNLOCK(stcb);
   9367  1.1.2.2  skrll 			error = sbwait(&so->so_snd);
   9368  1.1.2.2  skrll 			SCTP_INP_RLOCK(inp);
   9369  1.1.2.2  skrll 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
   9370  1.1.2.2  skrll 			    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
   9371  1.1.2.2  skrll 				/* Should I really unlock ? */
   9372  1.1.2.2  skrll 				SCTP_INP_RUNLOCK(inp);
   9373  1.1.2.2  skrll 				error = EFAULT;
   9374  1.1.2.2  skrll 				goto out_locked;
   9375  1.1.2.2  skrll 			}
   9376  1.1.2.2  skrll 			SCTP_TCB_LOCK(stcb);
   9377  1.1.2.2  skrll 			SCTP_INP_RUNLOCK(inp);
   9378  1.1.2.2  skrll 
   9379  1.1.2.2  skrll 			inp->sctp_tcb_at_block = 0;
   9380  1.1.2.2  skrll #ifdef SCTP_BLK_LOGGING
   9381  1.1.2.2  skrll 			sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
   9382  1.1.2.2  skrll 			    so, asoc);
   9383  1.1.2.2  skrll #endif
   9384  1.1.2.2  skrll 			if (inp->error_on_block) {
   9385  1.1.2.2  skrll 				/*
   9386  1.1.2.2  skrll 				 * if our asoc was killed, the free code
   9387  1.1.2.2  skrll 				 * (in sctp_pcb.c) will save a error in
   9388  1.1.2.2  skrll 				 * here for us
   9389  1.1.2.2  skrll 				 */
   9390  1.1.2.2  skrll  				error = inp->error_on_block;
   9391  1.1.2.2  skrll 				goto out_locked;
   9392  1.1.2.2  skrll 			}
   9393  1.1.2.2  skrll 			if (error) {
   9394  1.1.2.2  skrll 				goto out_locked;
   9395  1.1.2.2  skrll 			}
   9396  1.1.2.2  skrll 			/* did we encounter a socket error? */
   9397  1.1.2.2  skrll 			if (so->so_error) {
   9398  1.1.2.2  skrll 				error = so->so_error;
   9399  1.1.2.2  skrll 				goto out_locked;
   9400  1.1.2.2  skrll 			}
   9401  1.1.2.2  skrll 			error = sblock(&so->so_snd, M_WAITOK);
   9402  1.1.2.2  skrll 			if (error) {
   9403  1.1.2.2  skrll 				/* Can't aquire the lock */
   9404  1.1.2.2  skrll 				goto out_locked;
   9405  1.1.2.2  skrll 			}
   9406  1.1.2.2  skrll #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
   9407  1.1.2.2  skrll 			if (so->so_rcv.sb_state & SBS_CANTSENDMORE) {
   9408  1.1.2.2  skrll #else
   9409  1.1.2.2  skrll 			if (so->so_state & SS_CANTSENDMORE) {
   9410  1.1.2.2  skrll #endif
   9411  1.1.2.2  skrll 				/* The socket is now set not to sendmore.. its gone */
   9412  1.1.2.2  skrll 				error = EPIPE;
   9413  1.1.2.2  skrll 				goto release;
   9414  1.1.2.2  skrll 			}
   9415  1.1.2.2  skrll 			if (so->so_error) {
   9416  1.1.2.2  skrll 				error = so->so_error;
   9417  1.1.2.2  skrll 				goto release;
   9418  1.1.2.2  skrll 			}
   9419  1.1.2.2  skrll 			if (asoc->peer_supports_prsctp) {
   9420  1.1.2.2  skrll 				sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
   9421  1.1.2.2  skrll 			}
   9422  1.1.2.2  skrll 		}
   9423  1.1.2.2  skrll 	}
   9424  1.1.2.2  skrll 	dataout = tot_out = uio->uio_resid;
   9425  1.1.2.2  skrll  	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   9426  1.1.2.2  skrll 		resv_in_first = SCTP_MED_OVERHEAD;
   9427  1.1.2.2  skrll 	} else {
   9428  1.1.2.2  skrll 		resv_in_first = SCTP_MED_V4_OVERHEAD;
   9429  1.1.2.2  skrll 	}
   9430  1.1.2.2  skrll 
   9431  1.1.2.2  skrll 	/* Are we aborting? */
   9432  1.1.2.2  skrll 	if (srcv->sinfo_flags & MSG_ABORT) {
   9433  1.1.2.2  skrll 		if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
   9434  1.1.2.2  skrll 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
   9435  1.1.2.2  skrll 			/* It has to be up before we abort */
   9436  1.1.2.2  skrll 			/* how big is the user initiated abort? */
   9437  1.1.2.2  skrll 
   9438  1.1.2.2  skrll 			/* I wonder about doing a MGET without a splnet set.
   9439  1.1.2.2  skrll 			 * it is done that way in the sosend code so I guess
   9440  1.1.2.2  skrll 			 * it is ok :-0
   9441  1.1.2.2  skrll 			 */
   9442  1.1.2.2  skrll  			MGETHDR(mm, M_WAIT, MT_DATA);
   9443  1.1.2.2  skrll 			if (mm) {
   9444  1.1.2.2  skrll 				struct sctp_paramhdr *ph;
   9445  1.1.2.2  skrll 
   9446  1.1.2.2  skrll 				tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
   9447  1.1.2.2  skrll 				if (tot_demand > MHLEN) {
   9448  1.1.2.2  skrll 					if (tot_demand > MCLBYTES) {
   9449  1.1.2.2  skrll 						/* truncate user data */
   9450  1.1.2.2  skrll 						tot_demand = MCLBYTES;
   9451  1.1.2.2  skrll 						tot_out = tot_demand - sizeof(struct sctp_paramhdr);
   9452  1.1.2.2  skrll 					}
   9453  1.1.2.2  skrll 					MCLGET(mm, M_WAIT);
   9454  1.1.2.2  skrll 					if ((mm->m_flags & M_EXT) == 0) {
   9455  1.1.2.2  skrll 						/* truncate further */
   9456  1.1.2.2  skrll 						tot_demand = MHLEN;
   9457  1.1.2.2  skrll 						tot_out = tot_demand - sizeof(struct sctp_paramhdr);
   9458  1.1.2.2  skrll 					}
   9459  1.1.2.2  skrll 				}
   9460  1.1.2.2  skrll 				/* now move forward the data pointer */
   9461  1.1.2.2  skrll 				ph = mtod(mm, struct sctp_paramhdr *);
   9462  1.1.2.2  skrll 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
   9463  1.1.2.2  skrll 				ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
   9464  1.1.2.2  skrll 				ph++;
   9465  1.1.2.2  skrll 				mm->m_pkthdr.len = tot_out + sizeof(struct sctp_paramhdr);
   9466  1.1.2.2  skrll 				mm->m_len = mm->m_pkthdr.len;
   9467  1.1.2.2  skrll 				error = uiomove((void *)ph, (int)tot_out, uio);
   9468  1.1.2.2  skrll 				if (error) {
   9469  1.1.2.2  skrll 					/*
   9470  1.1.2.2  skrll 					 * Here if we can't get his data we
   9471  1.1.2.2  skrll 					 * still abort we just don't get to
   9472  1.1.2.2  skrll 					 * send the users note :-0
   9473  1.1.2.2  skrll 					 */
   9474  1.1.2.2  skrll 					sctp_m_freem(mm);
   9475  1.1.2.2  skrll 					mm = NULL;
   9476  1.1.2.2  skrll 				}
   9477  1.1.2.2  skrll 			}
   9478  1.1.2.2  skrll 			sbunlock(&so->so_snd);
   9479  1.1.2.2  skrll 			sctp_abort_an_association(stcb->sctp_ep, stcb,
   9480  1.1.2.2  skrll 						  SCTP_RESPONSE_TO_USER_REQ,
   9481  1.1.2.2  skrll 						  mm);
   9482  1.1.2.2  skrll 			mm = NULL;
   9483  1.1.2.2  skrll 			goto out_locked;
   9484  1.1.2.2  skrll 		}
   9485  1.1.2.2  skrll 		goto release;
   9486  1.1.2.2  skrll 	}
   9487  1.1.2.2  skrll 
   9488  1.1.2.2  skrll 	/* Now can we send this? */
   9489  1.1.2.2  skrll 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
   9490  1.1.2.2  skrll 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
   9491  1.1.2.2  skrll 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
   9492  1.1.2.2  skrll 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
   9493  1.1.2.2  skrll 		/* got data while shutting down */
   9494  1.1.2.2  skrll 		error = ECONNRESET;
   9495  1.1.2.2  skrll 		goto release;
   9496  1.1.2.2  skrll  	}
   9497  1.1.2.2  skrll  	/* Is the stream no. valid? */
   9498  1.1.2.2  skrll 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
   9499  1.1.2.2  skrll  		/* Invalid stream number */
   9500  1.1.2.2  skrll 		error = EINVAL;
   9501  1.1.2.2  skrll 		goto release;
   9502  1.1.2.2  skrll  	}
   9503  1.1.2.2  skrll 	if (asoc->strmout == NULL) {
   9504  1.1.2.2  skrll 		/* huh? software error */
   9505  1.1.2.2  skrll #ifdef SCTP_DEBUG
   9506  1.1.2.2  skrll  		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   9507  1.1.2.2  skrll  			printf("software error in sctp_copy_it_in\n");
   9508  1.1.2.2  skrll  		}
   9509  1.1.2.2  skrll #endif
   9510  1.1.2.2  skrll 		error = EFAULT;
   9511  1.1.2.2  skrll 		goto release;
   9512  1.1.2.2  skrll 	}
   9513  1.1.2.2  skrll 	if ((srcv->sinfo_flags & MSG_EOF) &&
   9514  1.1.2.2  skrll 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
   9515  1.1.2.2  skrll 	    (tot_out == 0)) {
   9516  1.1.2.2  skrll 		sounlock(so);
   9517  1.1.2.2  skrll 		goto zap_by_it_now;
   9518  1.1.2.2  skrll 	}
   9519  1.1.2.2  skrll  	if (tot_out == 0) {
   9520  1.1.2.2  skrll  		/* not allowed */
   9521  1.1.2.2  skrll  		error = EMSGSIZE;
   9522  1.1.2.2  skrll 		goto release;
   9523  1.1.2.2  skrll  	}
   9524  1.1.2.2  skrll 	/* save off the tag */
   9525  1.1.2.2  skrll 	my_vtag = asoc->my_vtag;
   9526  1.1.2.2  skrll 	strq = &asoc->strmout[srcv->sinfo_stream];
   9527  1.1.2.2  skrll 	/* First lets figure out the "chunking" point */
   9528  1.1.2.2  skrll 	frag_size = sctp_get_frag_point(stcb, asoc);
   9529  1.1.2.2  skrll 
   9530  1.1.2.2  skrll 	/* two choices here, it all fits in one chunk or
   9531  1.1.2.2  skrll 	 * we need multiple chunks.
   9532  1.1.2.2  skrll 	 */
   9533  1.1.2.2  skrll 	sounlock(so);
   9534  1.1.2.2  skrll 	if (tot_out <= frag_size) {
   9535  1.1.2.2  skrll 		/* no need to setup a template */
   9536  1.1.2.2  skrll 		chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   9537  1.1.2.2  skrll 		if (chk == NULL) {
   9538  1.1.2.2  skrll 			error = ENOMEM;
   9539  1.1.2.2  skrll 			goto release;
   9540  1.1.2.2  skrll 		}
   9541  1.1.2.2  skrll 		sctppcbinfo.ipi_count_chunk++;
   9542  1.1.2.2  skrll 		sctppcbinfo.ipi_gencnt_chunk++;
   9543  1.1.2.2  skrll 		asoc->chunks_on_out_queue++;
   9544  1.1.2.2  skrll 		MGETHDR(mm, M_WAIT, MT_DATA);
   9545  1.1.2.2  skrll 		if (mm == NULL) {
   9546  1.1.2.2  skrll 			error = ENOMEM;
   9547  1.1.2.2  skrll 			goto clean_up;
   9548  1.1.2.2  skrll 		}
   9549  1.1.2.2  skrll 		error = sctp_copy_one(mm, uio, tot_out, resv_in_first, &mbcnt_e);
   9550  1.1.2.2  skrll 		if (error)
   9551  1.1.2.2  skrll 			goto clean_up;
   9552  1.1.2.2  skrll 		sctp_prepare_chunk(chk, stcb, srcv, strq, net);
   9553  1.1.2.2  skrll 		chk->mbcnt = mbcnt_e;
   9554  1.1.2.2  skrll 		mbcnt += mbcnt_e;
   9555  1.1.2.2  skrll 		mbcnt_e = 0;
   9556  1.1.2.2  skrll 		mm->m_pkthdr.len = tot_out;
   9557  1.1.2.2  skrll 		chk->data = mm;
   9558  1.1.2.2  skrll 		mm = NULL;
   9559  1.1.2.2  skrll 
   9560  1.1.2.2  skrll 		/* the actual chunk flags */
   9561  1.1.2.2  skrll 		chk->rec.data.rcv_flags |= SCTP_DATA_NOT_FRAG;
   9562  1.1.2.2  skrll 		chk->whoTo->ref_count++;
   9563  1.1.2.2  skrll 
   9564  1.1.2.2  skrll 		/* fix up the send_size if it is not present */
   9565  1.1.2.2  skrll 		chk->send_size = tot_out;
   9566  1.1.2.2  skrll 		chk->book_size = chk->send_size;
   9567  1.1.2.2  skrll 		/* ok, we are commited */
   9568  1.1.2.2  skrll 		if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
   9569  1.1.2.2  skrll 			/* bump the ssn if we are unordered. */
   9570  1.1.2.2  skrll 			strq->next_sequence_sent++;
   9571  1.1.2.2  skrll 		}
   9572  1.1.2.2  skrll 		if (chk->flags & SCTP_PR_SCTP_BUFFER) {
   9573  1.1.2.2  skrll 			asoc->sent_queue_cnt_removeable++;
   9574  1.1.2.2  skrll 		}
   9575  1.1.2.2  skrll 		solock(so);
   9576  1.1.2.2  skrll 		if ((asoc->state == 0) ||
   9577  1.1.2.2  skrll 		    (my_vtag != asoc->my_vtag) ||
   9578  1.1.2.2  skrll 		    (so != inp->sctp_socket) ||
   9579  1.1.2.2  skrll 		    (inp->sctp_socket == 0)) {
   9580  1.1.2.2  skrll 			/* connection was aborted */
   9581  1.1.2.2  skrll 			sounlock(so);
   9582  1.1.2.2  skrll 			error = ECONNRESET;
   9583  1.1.2.2  skrll 			goto clean_up;
   9584  1.1.2.2  skrll 		}
   9585  1.1.2.2  skrll 		asoc->stream_queue_cnt++;
   9586  1.1.2.2  skrll 		TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
   9587  1.1.2.2  skrll 		/* now check if this stream is on the wheel */
   9588  1.1.2.2  skrll 		if ((strq->next_spoke.tqe_next == NULL) &&
   9589  1.1.2.2  skrll 		    (strq->next_spoke.tqe_prev == NULL)) {
   9590  1.1.2.2  skrll 			/* Insert it on the wheel since it is not
   9591  1.1.2.2  skrll 			 * on it currently
   9592  1.1.2.2  skrll 			 */
   9593  1.1.2.2  skrll 			sctp_insert_on_wheel(asoc, strq);
   9594  1.1.2.2  skrll 		}
   9595  1.1.2.2  skrll 		sounlock(so);
   9596  1.1.2.2  skrll clean_up:
   9597  1.1.2.2  skrll 		if (error) {
   9598  1.1.2.2  skrll 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   9599  1.1.2.2  skrll 			sctppcbinfo.ipi_count_chunk--;
   9600  1.1.2.2  skrll 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   9601  1.1.2.2  skrll 				panic("Chunk count is negative");
   9602  1.1.2.2  skrll 			}
   9603  1.1.2.2  skrll 			goto release;
   9604  1.1.2.2  skrll 		}
   9605  1.1.2.2  skrll 	} else {
   9606  1.1.2.2  skrll 		/* we need to setup a template */
   9607  1.1.2.2  skrll 		struct sctp_tmit_chunk template;
   9608  1.1.2.2  skrll 		struct sctpchunk_listhead tmp;
   9609  1.1.2.2  skrll 
   9610  1.1.2.2  skrll 		/* setup the template */
   9611  1.1.2.2  skrll 		sctp_prepare_chunk(&template, stcb, srcv, strq, net);
   9612  1.1.2.2  skrll 
   9613  1.1.2.2  skrll 		/* Prepare the temp list */
   9614  1.1.2.2  skrll 		TAILQ_INIT(&tmp);
   9615  1.1.2.2  skrll 
   9616  1.1.2.2  skrll 		/* Template is complete, now time for the work */
   9617  1.1.2.2  skrll 		while (tot_out > 0) {
   9618  1.1.2.2  skrll 			/* Get a chunk */
   9619  1.1.2.2  skrll  			chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   9620  1.1.2.2  skrll 			if (chk == NULL) {
   9621  1.1.2.2  skrll 				/*
   9622  1.1.2.2  skrll 				 * ok we must spin through and dump anything
   9623  1.1.2.2  skrll 				 * we have allocated and then jump to the
   9624  1.1.2.2  skrll 				 * no_membad
   9625  1.1.2.2  skrll 				 */
   9626  1.1.2.2  skrll 				error = ENOMEM;
   9627  1.1.2.2  skrll 			}
   9628  1.1.2.2  skrll 			sctppcbinfo.ipi_count_chunk++;
   9629  1.1.2.2  skrll 			asoc->chunks_on_out_queue++;
   9630  1.1.2.2  skrll 
   9631  1.1.2.2  skrll 			sctppcbinfo.ipi_gencnt_chunk++;
   9632  1.1.2.2  skrll 			*chk = template;
   9633  1.1.2.2  skrll 			chk->whoTo->ref_count++;
   9634  1.1.2.2  skrll 			MGETHDR(chk->data, M_WAIT, MT_DATA);
   9635  1.1.2.2  skrll 			if (chk->data == NULL) {
   9636  1.1.2.2  skrll 				error = ENOMEM;
   9637  1.1.2.2  skrll 				goto temp_clean_up;
   9638  1.1.2.2  skrll 			}
   9639  1.1.2.2  skrll 			tot_demand = min(tot_out, frag_size);
   9640  1.1.2.2  skrll 			error = sctp_copy_one(chk->data, uio, tot_demand , resv_in_first, &mbcnt_e);
   9641  1.1.2.2  skrll 			if (error)
   9642  1.1.2.2  skrll 				goto temp_clean_up;
   9643  1.1.2.2  skrll 			/* now fix the chk->send_size */
   9644  1.1.2.2  skrll 			chk->mbcnt = mbcnt_e;
   9645  1.1.2.2  skrll 			mbcnt += mbcnt_e;
   9646  1.1.2.2  skrll 			mbcnt_e = 0;
   9647  1.1.2.2  skrll 			chk->send_size = tot_demand;
   9648  1.1.2.2  skrll 			chk->data->m_pkthdr.len = tot_demand;
   9649  1.1.2.2  skrll 			chk->book_size = chk->send_size;
   9650  1.1.2.2  skrll 			if (chk->flags & SCTP_PR_SCTP_BUFFER) {
   9651  1.1.2.2  skrll 				asoc->sent_queue_cnt_removeable++;
   9652  1.1.2.2  skrll 			}
   9653  1.1.2.2  skrll 			TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
   9654  1.1.2.2  skrll 			tot_out -= tot_demand;
   9655  1.1.2.2  skrll 		}
   9656  1.1.2.2  skrll 		/* Now the tmp list holds all chunks and data */
   9657  1.1.2.2  skrll 		if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
   9658  1.1.2.2  skrll 			/* bump the ssn if we are unordered. */
   9659  1.1.2.2  skrll 			strq->next_sequence_sent++;
   9660  1.1.2.2  skrll 		}
   9661  1.1.2.2  skrll 		/* Mark the first/last flags. This will
   9662  1.1.2.2  skrll 		 * result int a 3 for a single item on the list
   9663  1.1.2.2  skrll 		 */
   9664  1.1.2.2  skrll 		chk = TAILQ_FIRST(&tmp);
   9665  1.1.2.2  skrll 		chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
   9666  1.1.2.2  skrll 		chk = TAILQ_LAST(&tmp, sctpchunk_listhead);
   9667  1.1.2.2  skrll 		chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
   9668  1.1.2.2  skrll 
   9669  1.1.2.2  skrll 		/* now move it to the streams actual queue */
   9670  1.1.2.2  skrll 		/* first stop protocol processing */
   9671  1.1.2.2  skrll 		mutex_enter(softnet_lock);
   9672  1.1.2.2  skrll 		if ((asoc->state == 0) ||
   9673  1.1.2.2  skrll 		    (my_vtag != asoc->my_vtag) ||
   9674  1.1.2.2  skrll 		    (so != inp->sctp_socket) ||
   9675  1.1.2.2  skrll 		    (inp->sctp_socket == 0)) {
   9676  1.1.2.2  skrll 			/* connection was aborted */
   9677  1.1.2.2  skrll 			mutex_exit(softnet_lock);
   9678  1.1.2.2  skrll 			error = ECONNRESET;
   9679  1.1.2.2  skrll 			goto temp_clean_up;
   9680  1.1.2.2  skrll 		}
   9681  1.1.2.2  skrll 		chk = TAILQ_FIRST(&tmp);
   9682  1.1.2.2  skrll 		while (chk) {
   9683  1.1.2.2  skrll 			chk->data->m_nextpkt = 0;
   9684  1.1.2.2  skrll 			TAILQ_REMOVE(&tmp, chk, sctp_next);
   9685  1.1.2.2  skrll 			asoc->stream_queue_cnt++;
   9686  1.1.2.2  skrll 			TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
   9687  1.1.2.2  skrll 			chk = TAILQ_FIRST(&tmp);
   9688  1.1.2.2  skrll 		}
   9689  1.1.2.2  skrll 		/* now check if this stream is on the wheel */
   9690  1.1.2.2  skrll 		if ((strq->next_spoke.tqe_next == NULL) &&
   9691  1.1.2.2  skrll 		    (strq->next_spoke.tqe_prev == NULL)) {
   9692  1.1.2.2  skrll 			/* Insert it on the wheel since it is not
   9693  1.1.2.2  skrll 			 * on it currently
   9694  1.1.2.2  skrll 			 */
   9695  1.1.2.2  skrll 			sctp_insert_on_wheel(asoc, strq);
   9696  1.1.2.2  skrll 		}
   9697  1.1.2.2  skrll 		/* Ok now we can allow pping */
   9698  1.1.2.2  skrll 		mutex_exit(softnet_lock);
   9699  1.1.2.2  skrll temp_clean_up:
   9700  1.1.2.2  skrll 		if (error) {
   9701  1.1.2.2  skrll 			chk = TAILQ_FIRST(&tmp);
   9702  1.1.2.2  skrll 			while (chk) {
   9703  1.1.2.2  skrll 				if (chk->data) {
   9704  1.1.2.2  skrll 					sctp_m_freem(chk->data);
   9705  1.1.2.2  skrll 					chk->data = NULL;
   9706  1.1.2.2  skrll 				}
   9707  1.1.2.2  skrll 				TAILQ_REMOVE(&tmp, chk, sctp_next);
   9708  1.1.2.2  skrll 				SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   9709  1.1.2.2  skrll 				sctppcbinfo.ipi_count_chunk--;
   9710  1.1.2.2  skrll 				asoc->chunks_on_out_queue--;
   9711  1.1.2.2  skrll 				if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   9712  1.1.2.2  skrll 					panic("Chunk count is negative");
   9713  1.1.2.2  skrll 				}
   9714  1.1.2.2  skrll 				sctppcbinfo.ipi_gencnt_chunk++;
   9715  1.1.2.2  skrll 				chk = TAILQ_FIRST(&tmp);
   9716  1.1.2.2  skrll 			}
   9717  1.1.2.2  skrll 			goto release;
   9718  1.1.2.2  skrll 		}
   9719  1.1.2.2  skrll 	}
   9720  1.1.2.2  skrll zap_by_it_now:
   9721  1.1.2.2  skrll #ifdef SCTP_MBCNT_LOGGING
   9722  1.1.2.2  skrll 	sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
   9723  1.1.2.2  skrll 		       asoc->total_output_queue_size,
   9724  1.1.2.2  skrll 		       dataout,
   9725  1.1.2.2  skrll 		       asoc->total_output_mbuf_queue_size,
   9726  1.1.2.2  skrll 		       mbcnt);
   9727  1.1.2.2  skrll #endif
   9728  1.1.2.2  skrll 	solock(so);
   9729  1.1.2.2  skrll 	asoc->total_output_queue_size += dataout;
   9730  1.1.2.2  skrll 	asoc->total_output_mbuf_queue_size += mbcnt;
   9731  1.1.2.2  skrll 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
   9732  1.1.2.2  skrll 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
   9733  1.1.2.2  skrll 		so->so_snd.sb_cc += dataout;
   9734  1.1.2.2  skrll 		so->so_snd.sb_mbcnt += mbcnt;
   9735  1.1.2.2  skrll 	}
   9736  1.1.2.2  skrll 	if ((srcv->sinfo_flags & MSG_EOF) &&
   9737  1.1.2.2  skrll 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
   9738  1.1.2.2  skrll 		) {
   9739  1.1.2.2  skrll 		int some_on_streamwheel = 0;
   9740  1.1.2.2  skrll 		error = 0;
   9741  1.1.2.2  skrll 		if (!TAILQ_EMPTY(&asoc->out_wheel)) {
   9742  1.1.2.2  skrll 			/* Check to see if some data queued */
   9743  1.1.2.2  skrll 			struct sctp_stream_out *outs;
   9744  1.1.2.2  skrll 			TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
   9745  1.1.2.2  skrll 				if (!TAILQ_EMPTY(&outs->outqueue)) {
   9746  1.1.2.2  skrll 					some_on_streamwheel = 1;
   9747  1.1.2.2  skrll 					break;
   9748  1.1.2.2  skrll 				}
   9749  1.1.2.2  skrll 			}
   9750  1.1.2.2  skrll 		}
   9751  1.1.2.2  skrll 		if (TAILQ_EMPTY(&asoc->send_queue) &&
   9752  1.1.2.2  skrll 		    TAILQ_EMPTY(&asoc->sent_queue) &&
   9753  1.1.2.2  skrll 		    (some_on_streamwheel == 0)) {
   9754  1.1.2.2  skrll 			/* there is nothing queued to send, so I'm done... */
   9755  1.1.2.2  skrll 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
   9756  1.1.2.2  skrll 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
   9757  1.1.2.2  skrll 				/* only send SHUTDOWN the first time through */
   9758  1.1.2.2  skrll #ifdef SCTP_DEBUG
   9759  1.1.2.2  skrll 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   9760  1.1.2.2  skrll 					printf("%s:%d sends a shutdown\n",
   9761  1.1.2.2  skrll 					       __FILE__,
   9762  1.1.2.2  skrll 					       __LINE__
   9763  1.1.2.2  skrll 						);
   9764  1.1.2.2  skrll 				}
   9765  1.1.2.2  skrll #endif
   9766  1.1.2.2  skrll 				sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
   9767  1.1.2.2  skrll 				asoc->state = SCTP_STATE_SHUTDOWN_SENT;
   9768  1.1.2.2  skrll 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
   9769  1.1.2.2  skrll 						 asoc->primary_destination);
   9770  1.1.2.2  skrll 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
   9771  1.1.2.2  skrll 						 asoc->primary_destination);
   9772  1.1.2.2  skrll 			}
   9773  1.1.2.2  skrll 		} else {
   9774  1.1.2.2  skrll 			/*
   9775  1.1.2.2  skrll 			 * we still got (or just got) data to send, so set
   9776  1.1.2.2  skrll 			 * SHUTDOWN_PENDING
   9777  1.1.2.2  skrll 			 */
   9778  1.1.2.2  skrll 			/*
   9779  1.1.2.2  skrll 			 * XXX sockets draft says that MSG_EOF should be sent
   9780  1.1.2.2  skrll 			 * with no data.  currently, we will allow user data
   9781  1.1.2.2  skrll 			 * to be sent first and move to SHUTDOWN-PENDING
   9782  1.1.2.2  skrll 			 */
   9783  1.1.2.2  skrll 			asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
   9784  1.1.2.2  skrll 		}
   9785  1.1.2.2  skrll 	}
   9786  1.1.2.2  skrll #ifdef SCTP_DEBUG
   9787  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   9788  1.1.2.2  skrll 		printf("++total out:%d total_mbuf_out:%d\n",
   9789  1.1.2.2  skrll 		       (int)asoc->total_output_queue_size,
   9790  1.1.2.2  skrll 		       (int)asoc->total_output_mbuf_queue_size);
   9791  1.1.2.2  skrll 	}
   9792  1.1.2.2  skrll #endif
   9793  1.1.2.2  skrll 
   9794  1.1.2.2  skrll release:
   9795  1.1.2.2  skrll 	sbunlock(&so->so_snd);
   9796  1.1.2.2  skrll out_locked:
   9797  1.1.2.2  skrll 	sounlock(so);
   9798  1.1.2.2  skrll 
   9799  1.1.2.2  skrll 	if (mm)
   9800  1.1.2.2  skrll 		sctp_m_freem(mm);
   9801  1.1.2.2  skrll 	return (error);
   9802  1.1.2.2  skrll }
   9803  1.1.2.2  skrll 
   9804  1.1.2.2  skrll 
   9805  1.1.2.2  skrll int
   9806  1.1.2.2  skrll sctp_sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
   9807  1.1.2.2  skrll 	    struct mbuf *top, struct mbuf *control, int flags, struct lwp *p)
   9808  1.1.2.2  skrll {
   9809  1.1.2.2  skrll 	int error, use_rcvinfo;
   9810  1.1.2.2  skrll 	int queue_only = 0, queue_only_for_init=0;
   9811  1.1.2.2  skrll 	int un_sent = 0;
   9812  1.1.2.2  skrll 	int now_filled=0;
   9813  1.1.2.2  skrll 	struct sctp_inpcb *inp;
   9814  1.1.2.2  skrll  	struct sctp_tcb *stcb=NULL;
   9815  1.1.2.2  skrll 	struct sctp_sndrcvinfo srcv;
   9816  1.1.2.2  skrll 	struct timeval now;
   9817  1.1.2.2  skrll 	struct sctp_nets *net;
   9818  1.1.2.2  skrll 	struct sctp_association *asoc;
   9819  1.1.2.2  skrll 	struct sctp_inpcb *t_inp;
   9820  1.1.2.2  skrll 	int create_lock_applied = 0;
   9821  1.1.2.2  skrll 
   9822  1.1.2.2  skrll 	error = use_rcvinfo = 0;
   9823  1.1.2.2  skrll 	net = NULL;
   9824  1.1.2.2  skrll 	stcb = NULL;
   9825  1.1.2.2  skrll 	asoc = NULL;
   9826  1.1.2.2  skrll 	t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
   9827  1.1.2.2  skrll 
   9828  1.1.2.2  skrll 	solock(so);
   9829  1.1.2.2  skrll 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
   9830  1.1.2.2  skrll 	    (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
   9831  1.1.2.2  skrll 		/* The listner can NOT send */
   9832  1.1.2.2  skrll 		error = EFAULT;
   9833  1.1.2.2  skrll 		sounlock(so);
   9834  1.1.2.2  skrll 		goto out;
   9835  1.1.2.2  skrll 	}
   9836  1.1.2.2  skrll 	if (addr) {
   9837  1.1.2.2  skrll 		SCTP_ASOC_CREATE_LOCK(inp);
   9838  1.1.2.2  skrll 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
   9839  1.1.2.2  skrll 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
   9840  1.1.2.2  skrll 			/* Should I really unlock ? */
   9841  1.1.2.2  skrll 			error = EFAULT;
   9842  1.1.2.2  skrll 			sounlock(so);
   9843  1.1.2.2  skrll 			goto out;
   9844  1.1.2.2  skrll 
   9845  1.1.2.2  skrll 		}
   9846  1.1.2.2  skrll 		create_lock_applied = 1;
   9847  1.1.2.2  skrll 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
   9848  1.1.2.2  skrll 		    (addr->sa_family == AF_INET6)) {
   9849  1.1.2.2  skrll 			error = EINVAL;
   9850  1.1.2.2  skrll 			sounlock(so);
   9851  1.1.2.2  skrll 			goto out;
   9852  1.1.2.2  skrll 		}
   9853  1.1.2.2  skrll 	}
   9854  1.1.2.2  skrll 	/* now we must find the assoc */
   9855  1.1.2.2  skrll 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   9856  1.1.2.2  skrll 		SCTP_INP_RLOCK(inp);
   9857  1.1.2.2  skrll 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
   9858  1.1.2.2  skrll 		if (stcb == NULL) {
   9859  1.1.2.2  skrll 			SCTP_INP_RUNLOCK(inp);
   9860  1.1.2.2  skrll 			error = ENOTCONN;
   9861  1.1.2.2  skrll 			sounlock(so);
   9862  1.1.2.2  skrll 			goto out;
   9863  1.1.2.2  skrll 		}
   9864  1.1.2.2  skrll 		SCTP_TCB_LOCK(stcb);
   9865  1.1.2.2  skrll 		SCTP_INP_RUNLOCK(inp);
   9866  1.1.2.2  skrll 		net = stcb->asoc.primary_destination;
   9867  1.1.2.2  skrll 	}
   9868  1.1.2.2  skrll #ifdef SCTP_DEBUG
   9869  1.1.2.2  skrll 	printf("sctp_sosend: get control\n");
   9870  1.1.2.2  skrll #endif
   9871  1.1.2.2  skrll 	/* get control */
   9872  1.1.2.2  skrll 	if (control) {
   9873  1.1.2.2  skrll 		/* process cmsg snd/rcv info (maybe a assoc-id) */
   9874  1.1.2.2  skrll 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
   9875  1.1.2.2  skrll 				   sizeof(srcv))) {
   9876  1.1.2.2  skrll 			/* got one */
   9877  1.1.2.2  skrll 			if (srcv.sinfo_flags & MSG_SENDALL) {
   9878  1.1.2.2  skrll 				/* its a sendall */
   9879  1.1.2.2  skrll 				sctppcbinfo.mbuf_track--;
   9880  1.1.2.2  skrll 				sctp_m_freem(control);
   9881  1.1.2.2  skrll 
   9882  1.1.2.2  skrll 				if (create_lock_applied) {
   9883  1.1.2.2  skrll 					SCTP_ASOC_CREATE_UNLOCK(inp);
   9884  1.1.2.2  skrll 					create_lock_applied = 0;
   9885  1.1.2.2  skrll 				}
   9886  1.1.2.2  skrll 				return (sctp_sendall(inp, uio, top, &srcv));
   9887  1.1.2.2  skrll 			}
   9888  1.1.2.2  skrll 			use_rcvinfo = 1;
   9889  1.1.2.2  skrll 		}
   9890  1.1.2.2  skrll 	}
   9891  1.1.2.2  skrll #ifdef SCTP_DEBUG
   9892  1.1.2.2  skrll 	printf("sctp_sosend: doing lookup\n");
   9893  1.1.2.2  skrll #endif
   9894  1.1.2.2  skrll 	if (stcb == NULL) {
   9895  1.1.2.2  skrll 		/* Need to do a lookup */
   9896  1.1.2.2  skrll 		if (use_rcvinfo && srcv.sinfo_assoc_id) {
   9897  1.1.2.2  skrll 			stcb = sctp_findassociation_ep_asocid(inp, srcv.sinfo_assoc_id);
   9898  1.1.2.2  skrll 			/*
   9899  1.1.2.2  skrll 			 * Question: Should I error here if the assoc_id is
   9900  1.1.2.2  skrll 			 * no longer valid? i.e. I can't find it?
   9901  1.1.2.2  skrll 			 */
   9902  1.1.2.2  skrll 			if ((stcb) &&
   9903  1.1.2.2  skrll 			    (addr != NULL)) {
   9904  1.1.2.2  skrll 				/* Must locate the net structure */
   9905  1.1.2.2  skrll 				net = sctp_findnet(stcb, addr);
   9906  1.1.2.2  skrll 			}
   9907  1.1.2.2  skrll 		}
   9908  1.1.2.2  skrll 		if (stcb == NULL) {
   9909  1.1.2.2  skrll 			if (addr != NULL) {
   9910  1.1.2.2  skrll 				/* Since we did not use findep we must
   9911  1.1.2.2  skrll 				 * increment it, and if we don't find a
   9912  1.1.2.2  skrll 				 * tcb decrement it.
   9913  1.1.2.2  skrll 				 */
   9914  1.1.2.2  skrll 				SCTP_INP_WLOCK(inp);
   9915  1.1.2.2  skrll 				SCTP_INP_INCR_REF(inp);
   9916  1.1.2.2  skrll 				SCTP_INP_WUNLOCK(inp);
   9917  1.1.2.2  skrll 				stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
   9918  1.1.2.2  skrll 				if (stcb == NULL) {
   9919  1.1.2.2  skrll 					SCTP_INP_WLOCK(inp);
   9920  1.1.2.2  skrll 					SCTP_INP_DECR_REF(inp);
   9921  1.1.2.2  skrll 					SCTP_INP_WUNLOCK(inp);
   9922  1.1.2.2  skrll 				}
   9923  1.1.2.2  skrll 			}
   9924  1.1.2.2  skrll 		}
   9925  1.1.2.2  skrll 	}
   9926  1.1.2.2  skrll 	if ((stcb == NULL) &&
   9927  1.1.2.2  skrll 	    (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
   9928  1.1.2.2  skrll 		error = ENOTCONN;
   9929  1.1.2.2  skrll 		sounlock(so);
   9930  1.1.2.2  skrll 		goto out;
   9931  1.1.2.2  skrll 	} else if ((stcb == NULL) && (addr == NULL)) {
   9932  1.1.2.2  skrll 		error = ENOENT;
   9933  1.1.2.2  skrll 		sounlock(so);
   9934  1.1.2.2  skrll 		goto out;
   9935  1.1.2.2  skrll 	} else if (stcb == NULL) {
   9936  1.1.2.2  skrll 		/* UDP style, we must go ahead and start the INIT process */
   9937  1.1.2.2  skrll 		if ((use_rcvinfo) &&
   9938  1.1.2.2  skrll 		    (srcv.sinfo_flags & MSG_ABORT)) {
   9939  1.1.2.2  skrll 			/* User asks to abort a non-existant asoc */
   9940  1.1.2.2  skrll 			error = ENOENT;
   9941  1.1.2.2  skrll 			sounlock(so);
   9942  1.1.2.2  skrll 			goto out;
   9943  1.1.2.2  skrll 		}
   9944  1.1.2.2  skrll 		/* get an asoc/stcb struct */
   9945  1.1.2.2  skrll 		stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
   9946  1.1.2.2  skrll 		if (stcb == NULL) {
   9947  1.1.2.2  skrll 			/* Error is setup for us in the call */
   9948  1.1.2.2  skrll 			sounlock(so);
   9949  1.1.2.2  skrll 			goto out;
   9950  1.1.2.2  skrll 		}
   9951  1.1.2.2  skrll 		if (create_lock_applied) {
   9952  1.1.2.2  skrll 			SCTP_ASOC_CREATE_UNLOCK(inp);
   9953  1.1.2.2  skrll 			create_lock_applied = 0;
   9954  1.1.2.2  skrll 		} else {
   9955  1.1.2.2  skrll 			printf("Huh-3? create lock should have been on??\n");
   9956  1.1.2.2  skrll 		}
   9957  1.1.2.2  skrll 		/* Turn on queue only flag to prevent data from being sent */
   9958  1.1.2.2  skrll  		queue_only = 1;
   9959  1.1.2.2  skrll 		asoc = &stcb->asoc;
   9960  1.1.2.2  skrll 		asoc->state = SCTP_STATE_COOKIE_WAIT;
   9961  1.1.2.2  skrll 		SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
   9962  1.1.2.2  skrll 		if (control) {
   9963  1.1.2.2  skrll 			/* see if a init structure exists in cmsg headers */
   9964  1.1.2.2  skrll 			struct sctp_initmsg initm;
   9965  1.1.2.2  skrll 			int i;
   9966  1.1.2.2  skrll 			if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control, sizeof(initm))) {
   9967  1.1.2.2  skrll 				/* we have an INIT override of the default */
   9968  1.1.2.2  skrll 				if (initm.sinit_max_attempts)
   9969  1.1.2.2  skrll 					asoc->max_init_times = initm.sinit_max_attempts;
   9970  1.1.2.2  skrll 				if (initm.sinit_num_ostreams)
   9971  1.1.2.2  skrll 					asoc->pre_open_streams = initm.sinit_num_ostreams;
   9972  1.1.2.2  skrll 				if (initm.sinit_max_instreams)
   9973  1.1.2.2  skrll 					asoc->max_inbound_streams = initm.sinit_max_instreams;
   9974  1.1.2.2  skrll 				if (initm.sinit_max_init_timeo)
   9975  1.1.2.2  skrll 					asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
   9976  1.1.2.2  skrll 				if (asoc->streamoutcnt < asoc->pre_open_streams) {
   9977  1.1.2.2  skrll 					/* Default is NOT correct */
   9978  1.1.2.2  skrll #ifdef SCTP_DEBUG
   9979  1.1.2.2  skrll 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   9980  1.1.2.2  skrll 						printf("Ok, defout:%d pre_open:%d\n",
   9981  1.1.2.2  skrll 						       asoc->streamoutcnt, asoc->pre_open_streams);
   9982  1.1.2.2  skrll 					}
   9983  1.1.2.2  skrll #endif
   9984  1.1.2.2  skrll 					free(asoc->strmout, M_PCB);
   9985  1.1.2.2  skrll 					asoc->strmout = NULL;
   9986  1.1.2.2  skrll 					asoc->streamoutcnt = asoc->pre_open_streams;
   9987  1.1.2.2  skrll 
   9988  1.1.2.2  skrll 					/* What happesn if this fails? .. we panic ...*/
   9989  1.1.2.2  skrll 					asoc->strmout = malloc(
   9990  1.1.2.2  skrll 					       asoc->streamoutcnt *
   9991  1.1.2.2  skrll 					       sizeof(struct sctp_stream_out),
   9992  1.1.2.2  skrll 					       M_PCB, M_WAIT);
   9993  1.1.2.2  skrll 					for (i = 0; i < asoc->streamoutcnt; i++) {
   9994  1.1.2.2  skrll 						/*
   9995  1.1.2.2  skrll 						 * inbound side must be set to 0xffff,
   9996  1.1.2.2  skrll 						 * also NOTE when we get the INIT-ACK
   9997  1.1.2.2  skrll 						 * back (for INIT sender) we MUST
   9998  1.1.2.2  skrll 						 * reduce the count (streamoutcnt) but
   9999  1.1.2.2  skrll 						 * first check if we sent to any of the
   10000  1.1.2.2  skrll 						 * upper streams that were dropped (if
   10001  1.1.2.2  skrll 						 * some were). Those that were dropped
   10002  1.1.2.2  skrll 						 * must be notified to the upper layer
   10003  1.1.2.2  skrll 						 * as failed to send.
   10004  1.1.2.2  skrll 						 */
   10005  1.1.2.2  skrll 						asoc->strmout[i].next_sequence_sent = 0x0;
   10006  1.1.2.2  skrll 						TAILQ_INIT(&asoc->strmout[i].outqueue);
   10007  1.1.2.2  skrll 						asoc->strmout[i].stream_no = i;
   10008  1.1.2.2  skrll 						asoc->strmout[i].next_spoke.tqe_next = 0;
   10009  1.1.2.2  skrll 						asoc->strmout[i].next_spoke.tqe_prev = 0;
   10010  1.1.2.2  skrll 					}
   10011  1.1.2.2  skrll 				}
   10012  1.1.2.2  skrll 			}
   10013  1.1.2.2  skrll 
   10014  1.1.2.2  skrll 		}
   10015  1.1.2.2  skrll 		/* out with the INIT */
   10016  1.1.2.2  skrll 		queue_only_for_init = 1;
   10017  1.1.2.2  skrll 		sctp_send_initiate(inp, stcb);
   10018  1.1.2.2  skrll 		/*
   10019  1.1.2.2  skrll 		 * we may want to dig in after this call and adjust the MTU
   10020  1.1.2.2  skrll 		 * value. It defaulted to 1500 (constant) but the ro structure
   10021  1.1.2.2  skrll 		 * may now have an update and thus we may need to change it
   10022  1.1.2.2  skrll 		 * BEFORE we append the message.
   10023  1.1.2.2  skrll 		 */
   10024  1.1.2.2  skrll 		net = stcb->asoc.primary_destination;
   10025  1.1.2.2  skrll 		asoc = &stcb->asoc;
   10026  1.1.2.2  skrll 	} else {
   10027  1.1.2.2  skrll 		asoc = &stcb->asoc;
   10028  1.1.2.2  skrll 	}
   10029  1.1.2.2  skrll 	if (create_lock_applied) {
   10030  1.1.2.2  skrll 		SCTP_ASOC_CREATE_UNLOCK(inp);
   10031  1.1.2.2  skrll 		create_lock_applied = 0;
   10032  1.1.2.2  skrll 	}
   10033  1.1.2.2  skrll 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
   10034  1.1.2.2  skrll 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
   10035  1.1.2.2  skrll 		queue_only = 1;
   10036  1.1.2.2  skrll 	}
   10037  1.1.2.2  skrll 	if (use_rcvinfo == 0) {
   10038  1.1.2.2  skrll 		/* Grab the default stuff from the asoc */
   10039  1.1.2.2  skrll 		srcv = stcb->asoc.def_send;
   10040  1.1.2.2  skrll 	}
   10041  1.1.2.2  skrll 	/* we are now done with all control */
   10042  1.1.2.2  skrll 	if (control) {
   10043  1.1.2.2  skrll 		sctp_m_freem(control);
   10044  1.1.2.2  skrll 		control = NULL;
   10045  1.1.2.2  skrll 	}
   10046  1.1.2.2  skrll 
   10047  1.1.2.2  skrll 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
   10048  1.1.2.2  skrll 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
   10049  1.1.2.2  skrll 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
   10050  1.1.2.2  skrll 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
   10051  1.1.2.2  skrll 		if ((use_rcvinfo) &&
   10052  1.1.2.2  skrll 		    (srcv.sinfo_flags & MSG_ABORT)) {
   10053  1.1.2.2  skrll 			;
   10054  1.1.2.2  skrll 		} else {
   10055  1.1.2.2  skrll 			error = ECONNRESET;
   10056  1.1.2.2  skrll 			sounlock(so);
   10057  1.1.2.2  skrll 			goto out;
   10058  1.1.2.2  skrll 		}
   10059  1.1.2.2  skrll 	}
   10060  1.1.2.2  skrll 	/* Ok, we will attempt a msgsnd :> */
   10061  1.1.2.2  skrll #if 0	/* XXX */
   10062  1.1.2.2  skrll 	if (p)
   10063  1.1.2.2  skrll 		p->p_stats->p_ru.ru_msgsnd++;
   10064  1.1.2.2  skrll #endif
   10065  1.1.2.2  skrll 
   10066  1.1.2.2  skrll 	if (stcb) {
   10067  1.1.2.2  skrll 		if (net && ((srcv.sinfo_flags & MSG_ADDR_OVER))) {
   10068  1.1.2.2  skrll 			/* we take the override or the unconfirmed */
   10069  1.1.2.2  skrll 			;
   10070  1.1.2.2  skrll 		} else {
   10071  1.1.2.2  skrll 			net = stcb->asoc.primary_destination;
   10072  1.1.2.2  skrll 		}
   10073  1.1.2.2  skrll 	}
   10074  1.1.2.2  skrll 
   10075  1.1.2.2  skrll #ifdef SCTP_DEBUG
   10076  1.1.2.2  skrll 	printf("sctp_sosend: before copying in %p\n", top);
   10077  1.1.2.2  skrll #endif
   10078  1.1.2.2  skrll 	if (top == NULL) {
   10079  1.1.2.2  skrll 		/* Must copy it all in from user land. The
   10080  1.1.2.2  skrll 		 * socket buf is locked but we don't suspend
   10081  1.1.2.2  skrll 		 * protocol processing until we are ready to
   10082  1.1.2.2  skrll 		 * send/queue it.
   10083  1.1.2.2  skrll 		 */
   10084  1.1.2.2  skrll 		sounlock(so);
   10085  1.1.2.2  skrll #ifdef SCTP_DEBUG
   10086  1.1.2.2  skrll 		printf("sctp_sosend: before cii\n");
   10087  1.1.2.2  skrll #endif
   10088  1.1.2.2  skrll 		error = sctp_copy_it_in(inp, stcb, asoc, net, &srcv, uio, flags);
   10089  1.1.2.2  skrll #ifdef SCTP_DEBUG
   10090  1.1.2.2  skrll 		printf("sctp_sosend: after cii\n");
   10091  1.1.2.2  skrll #endif
   10092  1.1.2.2  skrll 		if (error)
   10093  1.1.2.2  skrll 			goto out;
   10094  1.1.2.2  skrll 	} else {
   10095  1.1.2.2  skrll 		/* Here we must either pull in the user data to chunk
   10096  1.1.2.2  skrll 		 * buffers, or use top to do a msg_append.
   10097  1.1.2.2  skrll 		 */
   10098  1.1.2.2  skrll  		error = sctp_msg_append(stcb, net, top, &srcv, flags);
   10099  1.1.2.2  skrll 		sounlock(so);
   10100  1.1.2.2  skrll 		if (error)
   10101  1.1.2.2  skrll 			goto out;
   10102  1.1.2.2  skrll 		/* zap the top since it is now being used */
   10103  1.1.2.2  skrll 		top = 0;
   10104  1.1.2.2  skrll 	}
   10105  1.1.2.2  skrll #ifdef SCTP_DEBUG
   10106  1.1.2.2  skrll 	printf("sctp_sosend: after copying in\n");
   10107  1.1.2.2  skrll #endif
   10108  1.1.2.2  skrll 	if (net->flight_size > net->cwnd) {
   10109  1.1.2.2  skrll 		sctp_pegs[SCTP_SENDTO_FULL_CWND]++;
   10110  1.1.2.2  skrll 		queue_only = 1;
   10111  1.1.2.2  skrll 
   10112  1.1.2.2  skrll 	} else if (asoc->ifp_had_enobuf) {
   10113  1.1.2.2  skrll 		sctp_pegs[SCTP_QUEONLY_BURSTLMT]++;
   10114  1.1.2.2  skrll 		queue_only = 1;
   10115  1.1.2.2  skrll  	} else {
   10116  1.1.2.2  skrll 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
   10117  1.1.2.2  skrll 			   ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)) +
   10118  1.1.2.2  skrll 			   SCTP_MED_OVERHEAD);
   10119  1.1.2.2  skrll 
   10120  1.1.2.2  skrll 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY) == 0) &&
   10121  1.1.2.2  skrll 		    (stcb->asoc.total_flight > 0) &&
   10122  1.1.2.2  skrll 		    (un_sent < (int)stcb->asoc.smallest_mtu)) {
   10123  1.1.2.2  skrll 
   10124  1.1.2.2  skrll 			/* Ok, Nagle is set on and we have data outstanding. Don't
   10125  1.1.2.2  skrll 			 * send anything and let SACKs drive out the data unless we
   10126  1.1.2.2  skrll 			 * have a "full" segment to send.
   10127  1.1.2.2  skrll 			 */
   10128  1.1.2.2  skrll 			sctp_pegs[SCTP_NAGLE_NOQ]++;
   10129  1.1.2.2  skrll 			queue_only = 1;
   10130  1.1.2.2  skrll 		} else {
   10131  1.1.2.2  skrll 			sctp_pegs[SCTP_NAGLE_OFF]++;
   10132  1.1.2.2  skrll 		}
   10133  1.1.2.2  skrll 	}
   10134  1.1.2.2  skrll 	if (queue_only_for_init) {
   10135  1.1.2.2  skrll 		/* It is possible to have a turn around of the
   10136  1.1.2.2  skrll 		 * INIT/INIT-ACK/COOKIE before I have a chance to
   10137  1.1.2.2  skrll 		 * copy in the data. In such a case I DO want to
   10138  1.1.2.2  skrll 		 * send it out by reversing the queue only flag.
   10139  1.1.2.2  skrll 		 */
   10140  1.1.2.2  skrll 		if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) ||
   10141  1.1.2.2  skrll 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
   10142  1.1.2.2  skrll 			/* yep, reverse it */
   10143  1.1.2.2  skrll 			queue_only = 0;
   10144  1.1.2.2  skrll 		}
   10145  1.1.2.2  skrll  	}
   10146  1.1.2.2  skrll 
   10147  1.1.2.2  skrll #ifdef SCTP_DEBUG
   10148  1.1.2.2  skrll 	printf("sctp_sosend: before sending chunk\n");
   10149  1.1.2.2  skrll #endif
   10150  1.1.2.2  skrll 	if ((queue_only == 0) && (stcb->asoc.peers_rwnd  && un_sent)) {
   10151  1.1.2.2  skrll 		/* we can attempt to send too.*/
   10152  1.1.2.2  skrll #ifdef SCTP_DEBUG
   10153  1.1.2.2  skrll 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   10154  1.1.2.2  skrll 			printf("USR Send calls sctp_chunk_output\n");
   10155  1.1.2.2  skrll 		}
   10156  1.1.2.2  skrll #endif
   10157  1.1.2.2  skrll 		solock(so);
   10158  1.1.2.2  skrll 		sctp_pegs[SCTP_OUTPUT_FRM_SND]++;
   10159  1.1.2.2  skrll 		sctp_chunk_output(inp, stcb, 0);
   10160  1.1.2.2  skrll 		sounlock(so);
   10161  1.1.2.2  skrll 	} else if ((queue_only == 0) &&
   10162  1.1.2.2  skrll 		   (stcb->asoc.peers_rwnd == 0) &&
   10163  1.1.2.2  skrll 		   (stcb->asoc.total_flight == 0)) {
   10164  1.1.2.2  skrll 		/* We get to have a probe outstanding */
   10165  1.1.2.2  skrll 		solock(so);
   10166  1.1.2.2  skrll 		sctp_from_user_send = 1;
   10167  1.1.2.2  skrll 		sctp_chunk_output(inp, stcb, 0);
   10168  1.1.2.2  skrll 		sctp_from_user_send = 0;
   10169  1.1.2.2  skrll 		sounlock(so);
   10170  1.1.2.2  skrll 
   10171  1.1.2.2  skrll 	} else if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
   10172  1.1.2.2  skrll 		int num_out, reason, cwnd_full;
   10173  1.1.2.2  skrll 		/* Here we do control only */
   10174  1.1.2.2  skrll 		solock(so);
   10175  1.1.2.2  skrll 		sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
   10176  1.1.2.2  skrll 				      &reason, 1, &cwnd_full, 1, &now, &now_filled);
   10177  1.1.2.2  skrll 		sounlock(so);
   10178  1.1.2.2  skrll 	}
   10179  1.1.2.2  skrll #ifdef SCTP_DEBUG
   10180  1.1.2.2  skrll 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   10181  1.1.2.2  skrll 		printf("USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d \n",
   10182  1.1.2.2  skrll 		       queue_only, stcb->asoc.peers_rwnd, un_sent,
   10183  1.1.2.2  skrll 		       stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
   10184  1.1.2.2  skrll 		       stcb->asoc.total_output_queue_size);
   10185  1.1.2.2  skrll 	}
   10186  1.1.2.2  skrll #endif
   10187  1.1.2.2  skrll  out:
   10188  1.1.2.2  skrll 	if (create_lock_applied) {
   10189  1.1.2.2  skrll 		SCTP_ASOC_CREATE_UNLOCK(inp);
   10190  1.1.2.2  skrll 		create_lock_applied = 0;
   10191  1.1.2.2  skrll 	}
   10192  1.1.2.2  skrll 	if (stcb) {
   10193  1.1.2.2  skrll 		SCTP_TCB_UNLOCK(stcb);
   10194  1.1.2.2  skrll 	}
   10195  1.1.2.2  skrll 	if (top)
   10196  1.1.2.2  skrll 		sctp_m_freem(top);
   10197  1.1.2.2  skrll 	if (control)
   10198  1.1.2.2  skrll 		sctp_m_freem(control);
   10199  1.1.2.2  skrll 	return (error);
   10200  1.1.2.2  skrll }
   10201