Home | History | Annotate | Line # | Download | only in netinet
sctp_indata.c revision 1.5.2.2
      1  1.5.2.2  pgoyette /*	$NetBSD: sctp_indata.c,v 1.5.2.2 2018/09/06 06:56:44 pgoyette Exp $ */
      2      1.1       rjs /*	$KAME: sctp_indata.c,v 1.36 2005/03/06 16:04:17 itojun Exp $	*/
      3      1.1       rjs 
      4      1.1       rjs /*
      5      1.1       rjs  * Copyright (C) 2002, 2003, 2004 Cisco Systems Inc,
      6      1.1       rjs  * All rights reserved.
      7      1.1       rjs  *
      8      1.1       rjs  * Redistribution and use in source and binary forms, with or without
      9      1.1       rjs  * modification, are permitted provided that the following conditions
     10      1.1       rjs  * are met:
     11      1.1       rjs  * 1. Redistributions of source code must retain the above copyright
     12      1.1       rjs  *    notice, this list of conditions and the following disclaimer.
     13      1.1       rjs  * 2. Redistributions in binary form must reproduce the above copyright
     14      1.1       rjs  *    notice, this list of conditions and the following disclaimer in the
     15      1.1       rjs  *    documentation and/or other materials provided with the distribution.
     16      1.1       rjs  * 3. Neither the name of the project nor the names of its contributors
     17      1.1       rjs  *    may be used to endorse or promote products derived from this software
     18      1.1       rjs  *    without specific prior written permission.
     19      1.1       rjs  *
     20      1.1       rjs  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21      1.1       rjs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22      1.1       rjs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23      1.1       rjs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24      1.1       rjs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25      1.1       rjs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26      1.1       rjs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27      1.1       rjs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28      1.1       rjs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29      1.1       rjs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30      1.1       rjs  * SUCH DAMAGE.
     31      1.1       rjs  */
     32      1.1       rjs 
     33      1.1       rjs #include <sys/cdefs.h>
     34  1.5.2.2  pgoyette __KERNEL_RCSID(0, "$NetBSD: sctp_indata.c,v 1.5.2.2 2018/09/06 06:56:44 pgoyette Exp $");
     35      1.1       rjs 
     36      1.1       rjs #ifdef _KERNEL_OPT
     37      1.1       rjs #include "opt_ipsec.h"
     38      1.1       rjs #include "opt_inet.h"
     39      1.1       rjs #include "opt_sctp.h"
     40      1.1       rjs #endif /* _KERNEL_OPT */
     41      1.1       rjs 
     42      1.1       rjs #include <sys/param.h>
     43      1.1       rjs #include <sys/systm.h>
     44      1.1       rjs #include <sys/mbuf.h>
     45      1.1       rjs #include <sys/malloc.h>
     46      1.1       rjs #include <sys/socket.h>
     47      1.1       rjs #include <sys/socketvar.h>
     48      1.1       rjs #include <sys/sysctl.h>
     49      1.1       rjs 
     50      1.1       rjs #include <net/if.h>
     51      1.1       rjs #include <net/route.h>
     52      1.1       rjs 
     53      1.1       rjs 
     54      1.1       rjs #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
     55      1.1       rjs #include <sys/limits.h>
     56      1.1       rjs #else
     57      1.1       rjs #include <machine/limits.h>
     58      1.1       rjs #endif
     59      1.1       rjs #include <machine/cpu.h>
     60      1.1       rjs 
     61      1.1       rjs #include <netinet/in.h>
     62      1.1       rjs #include <netinet/in_systm.h>
     63      1.1       rjs #include <netinet/ip.h>
     64      1.1       rjs #ifdef INET6
     65      1.1       rjs #include <netinet/ip6.h>
     66      1.1       rjs #endif /* INET6 */
     67      1.1       rjs #include <netinet/in_pcb.h>
     68      1.1       rjs #include <netinet/in_var.h>
     69      1.1       rjs #include <netinet/ip_var.h>
     70      1.1       rjs #ifdef INET6
     71      1.1       rjs #include <netinet6/ip6_var.h>
     72      1.1       rjs #endif /* INET6 */
     73      1.1       rjs #include <netinet/ip_icmp.h>
     74      1.1       rjs #include <netinet/icmp_var.h>
     75      1.1       rjs #include <netinet/sctp_var.h>
     76      1.1       rjs #include <netinet/sctp_pcb.h>
     77      1.1       rjs #include <netinet/sctp_header.h>
     78      1.1       rjs #include <netinet/sctputil.h>
     79      1.1       rjs #include <netinet/sctp_output.h>
     80      1.1       rjs #include <netinet/sctp_input.h>
     81      1.1       rjs #include <netinet/sctp_hashdriver.h>
     82      1.1       rjs #include <netinet/sctp_indata.h>
     83      1.1       rjs #include <netinet/sctp_uio.h>
     84      1.1       rjs #include <netinet/sctp_timer.h>
     85      1.1       rjs #ifdef IPSEC
     86      1.4       rjs #include <netipsec/ipsec.h>
     87      1.4       rjs #include <netipsec/key.h>
     88      1.1       rjs #endif /*IPSEC*/
     89      1.1       rjs 
     90      1.1       rjs #ifdef SCTP_DEBUG
     91      1.1       rjs extern u_int32_t sctp_debug_on;
     92      1.1       rjs #endif
     93      1.1       rjs 
     94      1.1       rjs /*
     95      1.1       rjs  * NOTES: On the outbound side of things I need to check the sack timer to
     96      1.1       rjs  * see if I should generate a sack into the chunk queue (if I have data to
     97      1.1       rjs  * send that is and will be sending it .. for bundling.
     98      1.1       rjs  *
     99      1.1       rjs  * The callback in sctp_usrreq.c will get called when the socket is read
    100      1.1       rjs  * from. This will cause sctp_service_queues() to get called on the top
    101      1.1       rjs  * entry in the list.
    102      1.1       rjs  */
    103      1.1       rjs 
    104      1.1       rjs extern int sctp_strict_sacks;
    105      1.1       rjs 
    106      1.1       rjs void
    107      1.1       rjs sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
    108      1.1       rjs {
    109      1.1       rjs 	u_int32_t calc, calc_w_oh;
    110      1.1       rjs 
    111      1.1       rjs #ifdef SCTP_DEBUG
    112      1.1       rjs 	if (sctp_debug_on & SCTP_DEBUG_INDATA4) {
    113      1.1       rjs 		printf("cc:%lu hiwat:%lu lowat:%lu mbcnt:%lu mbmax:%lu\n",
    114      1.1       rjs 		       (u_long)stcb->sctp_socket->so_rcv.sb_cc,
    115      1.1       rjs 		       (u_long)stcb->sctp_socket->so_rcv.sb_hiwat,
    116      1.1       rjs 		       (u_long)stcb->sctp_socket->so_rcv.sb_lowat,
    117      1.1       rjs 		       (u_long)stcb->sctp_socket->so_rcv.sb_mbcnt,
    118      1.1       rjs 		       (u_long)stcb->sctp_socket->so_rcv.sb_mbmax);
    119      1.1       rjs 		printf("Setting rwnd to: sb:%ld - (del:%d + reasm:%d str:%d)\n",
    120      1.1       rjs 		       sctp_sbspace(&stcb->sctp_socket->so_rcv),
    121      1.1       rjs 		       asoc->size_on_delivery_queue,
    122      1.1       rjs 		       asoc->size_on_reasm_queue,
    123      1.1       rjs 		       asoc->size_on_all_streams);
    124      1.1       rjs 	}
    125      1.1       rjs #endif
    126      1.1       rjs 	if (stcb->sctp_socket->so_rcv.sb_cc == 0 &&
    127      1.1       rjs 	    asoc->size_on_delivery_queue == 0 &&
    128      1.1       rjs 	    asoc->size_on_reasm_queue == 0 &&
    129      1.1       rjs 	    asoc->size_on_all_streams == 0) {
    130      1.1       rjs 		/* Full rwnd granted */
    131  1.5.2.2  pgoyette 		asoc->my_rwnd = uimax(stcb->sctp_socket->so_rcv.sb_hiwat,
    132      1.1       rjs 				    SCTP_MINIMAL_RWND);
    133      1.1       rjs 		return;
    134      1.1       rjs 	}
    135      1.1       rjs 	/* get actual space */
    136      1.1       rjs 	calc = (u_int32_t)sctp_sbspace(&stcb->sctp_socket->so_rcv);
    137      1.1       rjs 
    138      1.1       rjs 	/* take out what has NOT been put on socket queue and
    139      1.1       rjs 	 * we yet hold for putting up.
    140      1.1       rjs 	 */
    141      1.1       rjs 	calc = sctp_sbspace_sub(calc, (u_int32_t)asoc->size_on_delivery_queue);
    142      1.1       rjs 	calc = sctp_sbspace_sub(calc, (u_int32_t)asoc->size_on_reasm_queue);
    143      1.1       rjs 	calc = sctp_sbspace_sub(calc, (u_int32_t)asoc->size_on_all_streams);
    144      1.1       rjs 
    145      1.1       rjs 	/* what is the overhead of all these rwnd's */
    146      1.1       rjs 	calc_w_oh = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
    147      1.1       rjs 
    148      1.1       rjs 	asoc->my_rwnd = calc;
    149      1.1       rjs 	if (calc_w_oh == 0) {
    150      1.1       rjs 		/* If our overhead is greater than the advertised
    151      1.1       rjs 		 * rwnd, we clamp the rwnd to 1. This lets us
    152      1.1       rjs 		 * still accept inbound segments, but hopefully will
    153      1.1       rjs 		 * shut the sender down when he finally gets the message.
    154      1.1       rjs 		 */
    155      1.1       rjs  		asoc->my_rwnd = 1;
    156      1.1       rjs 	} else {
    157      1.1       rjs 		/* SWS threshold */
    158      1.1       rjs 		if (asoc->my_rwnd &&
    159      1.1       rjs 		    (asoc->my_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_receiver)) {
    160      1.1       rjs 			/* SWS engaged, tell peer none left */
    161      1.1       rjs 			asoc->my_rwnd = 1;
    162      1.1       rjs #ifdef SCTP_DEBUG
    163      1.1       rjs 			if (sctp_debug_on & SCTP_DEBUG_INDATA4) {
    164      1.1       rjs 				printf(" - SWS zeros\n");
    165      1.1       rjs 			}
    166      1.1       rjs 		} else {
    167      1.1       rjs 			if (sctp_debug_on & SCTP_DEBUG_INDATA4) {
    168      1.1       rjs 				printf("\n");
    169      1.1       rjs 			}
    170      1.1       rjs #endif
    171      1.1       rjs 		}
    172      1.1       rjs 	}
    173      1.1       rjs }
    174      1.1       rjs 
    175      1.1       rjs /*
    176      1.1       rjs  * Take a chk structure and build it into an mbuf. Hmm should we change things
    177      1.1       rjs  * so that instead we store the data side in a chunk?
    178      1.1       rjs  */
    179      1.1       rjs static struct mbuf *
    180      1.1       rjs sctp_build_ctl_nchunk(struct sctp_tcb *stcb, uint32_t tsn, uint32_t ppid,
    181      1.1       rjs     uint32_t context, uint16_t stream_no, uint16_t stream_seq, uint8_t flags)
    182      1.1       rjs {
    183      1.1       rjs 	struct sctp_sndrcvinfo *outinfo;
    184      1.1       rjs 	struct cmsghdr *cmh;
    185      1.1       rjs 	struct mbuf *ret;
    186      1.1       rjs 
    187      1.1       rjs 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT) == 0) {
    188      1.1       rjs 		/* user does not want the sndrcv ctl */
    189      1.1       rjs 		return (NULL);
    190      1.1       rjs 	}
    191      1.1       rjs 
    192      1.1       rjs 	MGETHDR(ret, M_DONTWAIT, MT_CONTROL);
    193      1.1       rjs 	if (ret == NULL) {
    194      1.1       rjs 		/* No space */
    195      1.1       rjs 		return (ret);
    196      1.1       rjs 	}
    197      1.1       rjs 	/* We need a CMSG header followed by the struct  */
    198      1.1       rjs 	cmh = mtod(ret, struct cmsghdr *);
    199      1.1       rjs 	outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
    200      1.1       rjs 	cmh->cmsg_level = IPPROTO_SCTP;
    201      1.1       rjs 	cmh->cmsg_type = SCTP_SNDRCV;
    202      1.1       rjs 	cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
    203      1.1       rjs 	outinfo->sinfo_stream = stream_no;
    204      1.1       rjs 	outinfo->sinfo_ssn = stream_seq;
    205      1.1       rjs 	if (flags & SCTP_DATA_UNORDERED) {
    206      1.5       rjs 		outinfo->sinfo_flags = SCTP_UNORDERED;
    207      1.1       rjs 	} else {
    208      1.1       rjs 		outinfo->sinfo_flags = 0;
    209      1.1       rjs 	}
    210      1.1       rjs 	outinfo->sinfo_ppid = ppid;
    211      1.1       rjs 	outinfo->sinfo_context = context;
    212      1.1       rjs 	outinfo->sinfo_assoc_id = sctp_get_associd(stcb);
    213      1.1       rjs 	outinfo->sinfo_tsn = tsn;
    214      1.1       rjs 	outinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
    215      1.1       rjs 	ret->m_len = cmh->cmsg_len;
    216      1.1       rjs 	ret->m_pkthdr.len = ret->m_len;
    217      1.1       rjs 	/*
    218      1.1       rjs 	 * We track how many control len's have gone upon the sb
    219      1.1       rjs 	 * and do not count these in the rwnd calculation.
    220      1.1       rjs 	 */
    221      1.1       rjs 	stcb->asoc.my_rwnd_control_len +=
    222      1.1       rjs 	    CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
    223      1.1       rjs 
    224      1.1       rjs 	return (ret);
    225      1.1       rjs }
    226      1.1       rjs 
    227      1.1       rjs /*
    228      1.1       rjs  * Take a chk structure and build it into an mbuf.  Should we change things
    229      1.1       rjs  * so that instead we store the data side in a chunk?
    230      1.1       rjs  */
    231      1.1       rjs static
    232      1.1       rjs struct mbuf *
    233      1.1       rjs sctp_build_ctl(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk)
    234      1.1       rjs {
    235      1.1       rjs 	struct sctp_sndrcvinfo *outinfo;
    236      1.1       rjs 	struct cmsghdr *cmh;
    237      1.1       rjs 	struct mbuf *ret;
    238      1.1       rjs 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT) == 0) {
    239      1.1       rjs 		/* user does not want the sndrcv ctl */
    240      1.1       rjs 		return (NULL);
    241      1.1       rjs 	}
    242      1.1       rjs 	MGET(ret, M_DONTWAIT, MT_CONTROL);
    243      1.1       rjs 	if (ret == NULL) {
    244      1.1       rjs 		/* No space */
    245      1.1       rjs 		return (ret);
    246      1.1       rjs 	}
    247      1.1       rjs 
    248      1.1       rjs 	/* We need a CMSG header followed by the struct  */
    249      1.1       rjs 	cmh = mtod(ret, struct cmsghdr *);
    250      1.1       rjs 	outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
    251      1.1       rjs 	cmh->cmsg_level = IPPROTO_SCTP;
    252      1.1       rjs 	cmh->cmsg_type = SCTP_SNDRCV;
    253      1.1       rjs 	cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
    254      1.1       rjs 	outinfo->sinfo_stream = chk->rec.data.stream_number;
    255      1.1       rjs 	outinfo->sinfo_ssn = chk->rec.data.stream_seq;
    256      1.1       rjs 	if (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
    257      1.5       rjs 		outinfo->sinfo_flags = SCTP_UNORDERED;
    258      1.1       rjs 	} else {
    259      1.1       rjs 		outinfo->sinfo_flags = 0;
    260      1.1       rjs 	}
    261      1.1       rjs 	outinfo->sinfo_ppid = chk->rec.data.payloadtype;
    262      1.1       rjs 	outinfo->sinfo_context = chk->rec.data.context;
    263      1.1       rjs 	outinfo->sinfo_assoc_id = sctp_get_associd(stcb);
    264      1.1       rjs 	outinfo->sinfo_tsn = chk->rec.data.TSN_seq;
    265      1.1       rjs 	outinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
    266      1.1       rjs 	ret->m_len = cmh->cmsg_len;
    267      1.1       rjs 	stcb->asoc.my_rwnd_control_len +=
    268      1.1       rjs 	    CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
    269      1.1       rjs 
    270      1.1       rjs 	return (ret);
    271      1.1       rjs }
    272      1.1       rjs 
    273      1.1       rjs int
    274      1.1       rjs sctp_deliver_data(struct sctp_tcb *stcb, struct sctp_association *asoc,
    275      1.1       rjs     struct sctp_tmit_chunk *chk, int hold_locks)
    276      1.1       rjs {
    277      1.1       rjs 	struct mbuf *control, *m;
    278      1.1       rjs 	int free_it;
    279      1.1       rjs 	struct sockaddr_in6 sin6;
    280      1.1       rjs 	const struct sockaddr *to;
    281      1.1       rjs 
    282      1.1       rjs #ifdef SCTP_DEBUG
    283      1.1       rjs 	if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
    284      1.1       rjs 		printf("I am now in Deliver data! (%p)\n", chk);
    285      1.1       rjs 	}
    286      1.1       rjs #endif
    287      1.1       rjs 	/* get a write lock on the inp if not already */
    288      1.1       rjs 	if (hold_locks == 0) {
    289      1.1       rjs 		SCTP_TCB_UNLOCK(stcb);
    290      1.1       rjs 		SCTP_INP_WLOCK(stcb->sctp_ep);
    291      1.1       rjs 		SCTP_TCB_LOCK(stcb);
    292      1.1       rjs 	}
    293      1.1       rjs 	free_it = 0;
    294      1.1       rjs 	/* We always add it to the queue */
    295      1.1       rjs 	if (stcb && (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
    296      1.1       rjs 		/* socket above is long gone */
    297      1.1       rjs #ifdef SCTP_DEBUG
    298      1.1       rjs 		if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
    299      1.1       rjs 			printf("gone is gone!\n");
    300      1.1       rjs 		}
    301      1.1       rjs #endif
    302      1.1       rjs 		if (chk != NULL) {
    303      1.1       rjs 			if (chk->data)
    304      1.1       rjs 				sctp_m_freem(chk->data);
    305      1.1       rjs 			chk->data = NULL;
    306      1.1       rjs 			sctp_free_remote_addr(chk->whoTo);
    307      1.1       rjs 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
    308      1.1       rjs 			sctppcbinfo.ipi_count_chunk--;
    309      1.1       rjs 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
    310      1.1       rjs 				panic("Chunk count is negative");
    311      1.1       rjs 			}
    312      1.1       rjs 			sctppcbinfo.ipi_gencnt_chunk++;
    313      1.1       rjs 		}
    314      1.1       rjs 		TAILQ_FOREACH(chk, &asoc->delivery_queue, sctp_next) {
    315      1.1       rjs 			asoc->size_on_delivery_queue -= chk->send_size;
    316      1.1       rjs 			asoc->cnt_on_delivery_queue--;
    317      1.1       rjs 			/*
    318      1.1       rjs 			 * Lose the data pointer, since its in the socket buffer
    319      1.1       rjs 			 */
    320      1.1       rjs 			if (chk->data)
    321      1.1       rjs 				sctp_m_freem(chk->data);
    322      1.1       rjs 			chk->data = NULL;
    323      1.1       rjs 			/* Now free the address and data */
    324      1.1       rjs 			sctp_free_remote_addr(chk->whoTo);
    325      1.1       rjs 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
    326      1.1       rjs 			sctppcbinfo.ipi_count_chunk--;
    327      1.1       rjs 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
    328      1.1       rjs 				panic("Chunk count is negative");
    329      1.1       rjs 			}
    330      1.1       rjs 			sctppcbinfo.ipi_gencnt_chunk++;
    331      1.1       rjs 		}
    332      1.1       rjs 		if (hold_locks == 0) {
    333      1.1       rjs 			SCTP_INP_WUNLOCK(stcb->sctp_ep);
    334      1.1       rjs 		}
    335      1.1       rjs 		return (0);
    336      1.1       rjs 	}
    337      1.1       rjs 	if (chk != NULL) {
    338      1.1       rjs 		TAILQ_INSERT_TAIL(&asoc->delivery_queue, chk, sctp_next);
    339      1.1       rjs 		asoc->size_on_delivery_queue += chk->send_size;
    340      1.1       rjs 		asoc->cnt_on_delivery_queue++;
    341      1.1       rjs 	}
    342      1.1       rjs 	if (asoc->fragmented_delivery_inprogress) {
    343      1.1       rjs 		/*
    344      1.1       rjs 		 * oh oh, fragmented delivery in progress
    345      1.1       rjs 		 * return out of here.
    346      1.1       rjs 		 */
    347      1.1       rjs #ifdef SCTP_DEBUG
    348      1.1       rjs 		if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
    349      1.1       rjs 			printf("Fragmented delivery in progress?\n");
    350      1.1       rjs 		}
    351      1.1       rjs #endif
    352      1.1       rjs 		if (hold_locks == 0) {
    353      1.1       rjs 			SCTP_INP_WUNLOCK(stcb->sctp_ep);
    354      1.1       rjs 		}
    355      1.1       rjs 		return (0);
    356      1.1       rjs 	}
    357      1.1       rjs 	/* Now grab the first one  */
    358      1.1       rjs 	chk = TAILQ_FIRST(&asoc->delivery_queue);
    359      1.1       rjs 	if (chk == NULL) {
    360      1.1       rjs 		/* Nothing in queue */
    361      1.1       rjs #ifdef SCTP_DEBUG
    362      1.1       rjs 		if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
    363      1.1       rjs 			printf("Nothing in queue?\n");
    364      1.1       rjs 		}
    365      1.1       rjs #endif
    366      1.1       rjs 		asoc->size_on_delivery_queue = 0;
    367      1.1       rjs 		asoc->cnt_on_delivery_queue = 0;
    368      1.1       rjs 		if (hold_locks == 0) {
    369      1.1       rjs 			SCTP_INP_WUNLOCK(stcb->sctp_ep);
    370      1.1       rjs 		}
    371      1.1       rjs 		return (0);
    372      1.1       rjs 	}
    373      1.1       rjs 
    374      1.1       rjs 	if (stcb->sctp_socket->so_rcv.sb_cc >= stcb->sctp_socket->so_rcv.sb_hiwat) {
    375      1.1       rjs 		/* Boy, there really is NO room */
    376      1.1       rjs 		if (hold_locks == 0) {
    377      1.1       rjs 			SCTP_INP_WUNLOCK(stcb->sctp_ep);
    378      1.1       rjs 		}
    379      1.1       rjs 		return (0);
    380      1.1       rjs 	}
    381      1.1       rjs #ifdef SCTP_DEBUG
    382      1.1       rjs 	if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
    383      1.1       rjs 		printf("Now to the delivery with chk(%p)!\n", chk);
    384      1.1       rjs 	}
    385      1.1       rjs #endif
    386      1.1       rjs 	/* XXX need to append PKTHDR to the socket buffer first */
    387      1.1       rjs 	if ((chk->data->m_flags & M_PKTHDR) == 0) {
    388      1.1       rjs 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    389      1.1       rjs 		if (m == NULL) {
    390      1.1       rjs 			/* no room! */
    391      1.1       rjs 			if (hold_locks == 0) {
    392      1.1       rjs 				SCTP_INP_WUNLOCK(stcb->sctp_ep);
    393      1.1       rjs 			}
    394      1.1       rjs 			return (0);
    395      1.1       rjs 		}
    396      1.1       rjs 		m->m_pkthdr.len = chk->send_size;
    397      1.1       rjs 		m->m_len = 0;
    398      1.1       rjs 		m->m_next = chk->data;
    399      1.1       rjs 		chk->data = m;
    400      1.1       rjs 	}
    401      1.1       rjs 	if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
    402      1.1       rjs 		if (chk->data->m_next == NULL) {
    403      1.1       rjs 			/* hopefully we hit here most of the time */
    404      1.1       rjs 			chk->data->m_flags |= M_EOR;
    405      1.1       rjs 		} else {
    406      1.1       rjs 			/* Add the flag to the LAST mbuf in the chain */
    407      1.1       rjs 			m = chk->data;
    408      1.1       rjs 			while (m->m_next != NULL) {
    409      1.1       rjs 				m = m->m_next;
    410      1.1       rjs 			}
    411      1.1       rjs 			m->m_flags |= M_EOR;
    412      1.1       rjs 		}
    413      1.1       rjs 	}
    414      1.1       rjs 
    415      1.1       rjs 	if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
    416      1.1       rjs 		struct sockaddr_in6 lsa6;
    417      1.1       rjs 
    418      1.1       rjs 		control = sctp_build_ctl(stcb, chk);
    419      1.1       rjs 		to = rtcache_getdst(&chk->whoTo->ro);
    420      1.1       rjs 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
    421      1.1       rjs 		    to->sa_family == AF_INET) {
    422      1.1       rjs 			const struct sockaddr_in *sin;
    423      1.1       rjs 
    424      1.1       rjs 			sin = (const struct sockaddr_in *)to;
    425      1.3       rtr 			in6_sin_2_v4mapsin6(sin, &sin6);
    426      1.1       rjs 			to = (struct sockaddr *)&sin6;
    427      1.1       rjs 		}
    428      1.1       rjs 		/* check and strip embedded scope junk */
    429      1.1       rjs 		to = (const struct sockaddr *)sctp_recover_scope((const struct sockaddr_in6 *)to,
    430      1.1       rjs 		    &lsa6);
    431      1.1       rjs 		if (((const struct sockaddr_in *)to)->sin_port == 0) {
    432      1.1       rjs 			printf("Huh a, port is %d not net:%p %d?\n",
    433      1.1       rjs 			       ((const struct sockaddr_in *)to)->sin_port,
    434      1.1       rjs 			       chk->whoTo,
    435      1.1       rjs 			       (int)(ntohs(stcb->rport)));
    436      1.1       rjs 			/*((struct sockaddr_in *)to)->sin_port = stcb->rport;*/
    437      1.1       rjs 			/* XXX */
    438      1.1       rjs 		}
    439      1.1       rjs 		if (sctp_sbspace(&stcb->sctp_socket->so_rcv) < (long)chk->send_size) {
    440      1.1       rjs 			/* Gak not enough room */
    441      1.1       rjs 			if (control) {
    442      1.1       rjs 				sctp_m_freem(control);
    443      1.1       rjs 				stcb->asoc.my_rwnd_control_len -=
    444      1.1       rjs 				    CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
    445      1.1       rjs 			}
    446      1.1       rjs 			goto skip;
    447      1.1       rjs 		}
    448      1.1       rjs 		if (!sbappendaddr_nocheck(&stcb->sctp_socket->so_rcv,
    449      1.1       rjs 		    to, chk->data, control, stcb->asoc.my_vtag,
    450      1.1       rjs 		    stcb->sctp_ep)) {
    451      1.1       rjs 			/* Gak not enough room */
    452      1.1       rjs 			if (control) {
    453      1.1       rjs 				sctp_m_freem(control);
    454      1.1       rjs 				stcb->asoc.my_rwnd_control_len -=
    455      1.1       rjs 				    CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
    456      1.1       rjs 			}
    457      1.1       rjs 		} else {
    458      1.1       rjs 			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
    459      1.1       rjs 				if (sctp_add_to_socket_q(stcb->sctp_ep, stcb)) {
    460      1.1       rjs 					stcb->asoc.my_rwnd_control_len +=
    461      1.1       rjs 						sizeof(struct mbuf);
    462      1.1       rjs 				}
    463      1.1       rjs 			} else {
    464      1.1       rjs 				stcb->asoc.my_rwnd_control_len += sizeof(struct mbuf);
    465      1.1       rjs 			}
    466      1.1       rjs 			free_it = 1;
    467      1.1       rjs 		}
    468      1.1       rjs 	} else {
    469      1.1       rjs 		/* append to a already started message. */
    470      1.1       rjs 		if (sctp_sbspace(&stcb->sctp_socket->so_rcv) >=
    471      1.1       rjs 		    (long)chk->send_size) {
    472      1.1       rjs 			sbappend(&stcb->sctp_socket->so_rcv, chk->data);
    473      1.1       rjs 			free_it = 1;
    474      1.1       rjs 		}
    475      1.1       rjs 	}
    476      1.1       rjs  skip:
    477      1.1       rjs 	if (hold_locks == 0) {
    478      1.1       rjs 		SCTP_INP_WUNLOCK(stcb->sctp_ep);
    479      1.1       rjs 	}
    480      1.1       rjs 	/* free up the one we inserted */
    481      1.1       rjs 	if (free_it) {
    482      1.1       rjs 		/* Pull it off the queue */
    483      1.1       rjs #ifdef SCTP_DEBUG
    484      1.1       rjs 		if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
    485      1.1       rjs 			printf("Free_it true, doing tickle wakeup\n");
    486      1.1       rjs 		}
    487      1.1       rjs #endif
    488      1.1       rjs 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
    489      1.1       rjs 		TAILQ_REMOVE(&asoc->delivery_queue, chk, sctp_next);
    490      1.1       rjs 		asoc->size_on_delivery_queue -= chk->send_size;
    491      1.1       rjs 		asoc->cnt_on_delivery_queue--;
    492      1.1       rjs 		/* Lose the data pointer, since its in the socket buffer */
    493      1.1       rjs 		chk->data = NULL;
    494      1.1       rjs 		/* Now free the address and data */
    495      1.1       rjs 		sctp_free_remote_addr(chk->whoTo);
    496      1.1       rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
    497      1.1       rjs 		sctppcbinfo.ipi_count_chunk--;
    498      1.1       rjs 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
    499      1.1       rjs 			panic("Chunk count is negative");
    500      1.1       rjs 		}
    501      1.1       rjs 		sctppcbinfo.ipi_gencnt_chunk++;
    502      1.1       rjs 	}
    503      1.1       rjs 	return (free_it);
    504      1.1       rjs }
    505      1.1       rjs 
    506      1.1       rjs /*
    507      1.1       rjs  * We are delivering currently from the reassembly queue. We must continue to
    508      1.1       rjs  * deliver until we either:
    509      1.1       rjs  * 1) run out of space.
    510      1.1       rjs  * 2) run out of sequential TSN's
    511      1.1       rjs  * 3) hit the SCTP_DATA_LAST_FRAG flag.
    512      1.1       rjs  */
    513      1.1       rjs static void
    514      1.1       rjs sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc, int hold_locks)
    515      1.1       rjs {
    516      1.1       rjs 	const struct sockaddr *to;
    517      1.1       rjs 	struct sockaddr_in6 sin6;
    518      1.1       rjs 	struct sctp_tmit_chunk *chk, *at;
    519      1.1       rjs 	struct mbuf *control, *m;
    520      1.1       rjs 	u_int16_t nxt_todel;
    521      1.1       rjs 	u_int16_t stream_no;
    522      1.1       rjs 	int cntDel;
    523      1.1       rjs 	cntDel = stream_no = 0;
    524      1.1       rjs 	if (hold_locks == 0) {
    525      1.1       rjs 		/*
    526      1.1       rjs 		 * you always have the TCB lock, we need
    527      1.1       rjs 		 * to have the inp write lock as well.
    528      1.1       rjs 		 */
    529      1.1       rjs 		SCTP_TCB_UNLOCK(stcb);
    530      1.1       rjs 		SCTP_INP_WLOCK(stcb->sctp_ep);
    531      1.1       rjs 		SCTP_TCB_LOCK(stcb);
    532      1.1       rjs 	}
    533      1.1       rjs 	if (stcb && (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
    534      1.1       rjs 		/* socket above is long gone */
    535      1.1       rjs 		asoc->fragmented_delivery_inprogress = 0;
    536      1.1       rjs 		TAILQ_FOREACH(chk, &asoc->reasmqueue, sctp_next) {
    537      1.1       rjs 			asoc->size_on_delivery_queue -= chk->send_size;
    538      1.1       rjs 			asoc->cnt_on_delivery_queue--;
    539      1.1       rjs 			/*
    540      1.1       rjs 			 * Lose the data pointer, since its in the socket buffer
    541      1.1       rjs 			 */
    542      1.1       rjs 			if (chk->data)
    543      1.1       rjs 				sctp_m_freem(chk->data);
    544      1.1       rjs 			chk->data = NULL;
    545      1.1       rjs 			/* Now free the address and data */
    546      1.1       rjs 			sctp_free_remote_addr(chk->whoTo);
    547      1.1       rjs 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
    548      1.1       rjs 			sctppcbinfo.ipi_count_chunk--;
    549      1.1       rjs 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
    550      1.1       rjs 				panic("Chunk count is negative");
    551      1.1       rjs 			}
    552      1.1       rjs 			sctppcbinfo.ipi_gencnt_chunk++;
    553      1.1       rjs 		}
    554      1.1       rjs 		if (hold_locks == 0) {
    555      1.1       rjs 			SCTP_INP_WUNLOCK(stcb->sctp_ep);
    556      1.1       rjs 		}
    557      1.1       rjs 		return;
    558      1.1       rjs 	}
    559      1.1       rjs 	do {
    560      1.1       rjs 		if (stcb->sctp_socket->so_rcv.sb_cc >=
    561      1.1       rjs 		    stcb->sctp_socket->so_rcv.sb_hiwat) {
    562      1.1       rjs 			if (cntDel) {
    563      1.1       rjs 				sctp_sorwakeup(stcb->sctp_ep,
    564      1.1       rjs 					       stcb->sctp_socket);
    565      1.1       rjs 			}
    566      1.1       rjs 			if (hold_locks == 0) {
    567      1.1       rjs 				SCTP_INP_WUNLOCK(stcb->sctp_ep);
    568      1.1       rjs 			}
    569      1.1       rjs 			return;
    570      1.1       rjs 		}
    571      1.1       rjs 		chk = TAILQ_FIRST(&asoc->reasmqueue);
    572      1.1       rjs 		if (chk == NULL) {
    573      1.1       rjs 			if (cntDel) {
    574      1.1       rjs 				sctp_sorwakeup(stcb->sctp_ep,
    575      1.1       rjs 					       stcb->sctp_socket);
    576      1.1       rjs 			}
    577      1.1       rjs 			if (hold_locks == 0) {
    578      1.1       rjs 				SCTP_INP_WUNLOCK(stcb->sctp_ep);
    579      1.1       rjs 			}
    580      1.1       rjs 			return;
    581      1.1       rjs 		}
    582      1.1       rjs 		if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
    583      1.1       rjs 			/* Can't deliver more :< */
    584      1.1       rjs 			if (cntDel) {
    585      1.1       rjs 				sctp_sorwakeup(stcb->sctp_ep,
    586      1.1       rjs 					       stcb->sctp_socket);
    587      1.1       rjs 			}
    588      1.1       rjs 			if (hold_locks == 0) {
    589      1.1       rjs 				SCTP_INP_WUNLOCK(stcb->sctp_ep);
    590      1.1       rjs 			}
    591      1.1       rjs 			return;
    592      1.1       rjs 		}
    593      1.1       rjs 		stream_no = chk->rec.data.stream_number;
    594      1.1       rjs 		nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
    595      1.1       rjs 		if (nxt_todel != chk->rec.data.stream_seq &&
    596      1.1       rjs 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
    597      1.1       rjs 			/*
    598      1.1       rjs 			 * Not the next sequence to deliver in its stream OR
    599      1.1       rjs 			 * unordered
    600      1.1       rjs 			 */
    601      1.1       rjs 			if (cntDel) {
    602      1.1       rjs 				sctp_sorwakeup(stcb->sctp_ep,
    603      1.1       rjs 					       stcb->sctp_socket);
    604      1.1       rjs 			}
    605      1.1       rjs 			if (hold_locks == 0) {
    606      1.1       rjs 				SCTP_INP_WUNLOCK(stcb->sctp_ep);
    607      1.1       rjs 			}
    608      1.1       rjs 			return;
    609      1.1       rjs 		}
    610      1.1       rjs 
    611      1.1       rjs 		if ((chk->data->m_flags & M_PKTHDR) == 0) {
    612      1.1       rjs 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    613      1.1       rjs 			if (m == NULL) {
    614      1.1       rjs 				/* no room! */
    615      1.1       rjs 				if (hold_locks == 0) {
    616      1.1       rjs 					SCTP_INP_WUNLOCK(stcb->sctp_ep);
    617      1.1       rjs 				}
    618      1.1       rjs 				return;
    619      1.1       rjs 			}
    620      1.1       rjs 			m->m_pkthdr.len = chk->send_size;
    621      1.1       rjs 			m->m_len = 0;
    622      1.1       rjs 			m->m_next = chk->data;
    623      1.1       rjs 			chk->data = m;
    624      1.1       rjs 		}
    625      1.1       rjs 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
    626      1.1       rjs 			if (chk->data->m_next == NULL) {
    627      1.1       rjs 				/* hopefully we hit here most of the time */
    628      1.1       rjs 				chk->data->m_flags |= M_EOR;
    629      1.1       rjs 			} else {
    630      1.1       rjs 				/* Add the flag to the LAST mbuf in the chain */
    631      1.1       rjs 				m = chk->data;
    632      1.1       rjs 				while (m->m_next != NULL) {
    633      1.1       rjs 					m = m->m_next;
    634      1.1       rjs 				}
    635      1.1       rjs 				m->m_flags |= M_EOR;
    636      1.1       rjs 			}
    637      1.1       rjs 		}
    638      1.1       rjs 		if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
    639      1.1       rjs 			struct sockaddr_in6 lsa6;
    640      1.1       rjs 
    641      1.1       rjs 			control = sctp_build_ctl(stcb, chk);
    642      1.1       rjs 			to = rtcache_getdst(&chk->whoTo->ro);
    643      1.1       rjs 			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
    644      1.1       rjs 			    to->sa_family == AF_INET) {
    645      1.1       rjs 				const struct sockaddr_in *sin;
    646      1.1       rjs 
    647      1.1       rjs 				sin = satocsin(to);
    648      1.3       rtr 				in6_sin_2_v4mapsin6(sin, &sin6);
    649      1.1       rjs 				to = (struct sockaddr *)&sin6;
    650      1.1       rjs 			}
    651      1.1       rjs 			/* check and strip embedded scope junk */
    652      1.1       rjs 			to = (const struct sockaddr *)sctp_recover_scope((const struct sockaddr_in6 *)to,
    653      1.1       rjs 								   &lsa6);
    654      1.1       rjs 			if (((const struct sockaddr_in *)to)->sin_port == 0) {
    655      1.1       rjs 				printf("Huh b, port is %d not net:%p %d?\n",
    656      1.1       rjs 				       ((const struct sockaddr_in *)to)->sin_port,
    657      1.1       rjs 				       chk->whoTo,
    658      1.1       rjs 				       (int)(ntohs(stcb->rport)));
    659      1.1       rjs 				/*((struct sockaddr_in *)to)->sin_port = stcb->rport;*/
    660      1.1       rjs 				/* XXX */
    661      1.1       rjs 			}
    662      1.1       rjs 			if (sctp_sbspace(&stcb->sctp_socket->so_rcv) <
    663      1.1       rjs 			    (long)chk->send_size) {
    664      1.1       rjs 				if (control) {
    665      1.1       rjs 					sctp_m_freem(control);
    666      1.1       rjs 					stcb->asoc.my_rwnd_control_len -=
    667      1.1       rjs 						CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
    668      1.1       rjs 				}
    669      1.1       rjs 				sctp_sorwakeup(stcb->sctp_ep,
    670      1.1       rjs 					       stcb->sctp_socket);
    671      1.1       rjs 				if (hold_locks == 0) {
    672      1.1       rjs 					SCTP_INP_WUNLOCK(stcb->sctp_ep);
    673      1.1       rjs 				}
    674      1.1       rjs 				return;
    675      1.1       rjs 			}
    676      1.1       rjs 			if (!sbappendaddr_nocheck(&stcb->sctp_socket->so_rcv,
    677      1.1       rjs 						  to, chk->data, control, stcb->asoc.my_vtag,
    678      1.1       rjs 						  stcb->sctp_ep)) {
    679      1.1       rjs 				/* Gak not enough room */
    680      1.1       rjs 				if (control) {
    681      1.1       rjs 					sctp_m_freem(control);
    682      1.1       rjs 					stcb->asoc.my_rwnd_control_len -=
    683      1.1       rjs 						CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
    684      1.1       rjs 				}
    685      1.1       rjs 				sctp_sorwakeup(stcb->sctp_ep,
    686      1.1       rjs 					       stcb->sctp_socket);
    687      1.1       rjs 				if (hold_locks == 0) {
    688      1.1       rjs 					SCTP_INP_WUNLOCK(stcb->sctp_ep);
    689      1.1       rjs 				}
    690      1.1       rjs 				return;
    691      1.1       rjs 			}
    692      1.1       rjs 			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
    693      1.1       rjs 				if (sctp_add_to_socket_q(stcb->sctp_ep, stcb)) {
    694      1.1       rjs 					stcb->asoc.my_rwnd_control_len +=
    695      1.1       rjs 						sizeof(struct mbuf);
    696      1.1       rjs 				}
    697      1.1       rjs 			} else {
    698      1.1       rjs 				stcb->asoc.my_rwnd_control_len += sizeof(struct mbuf);
    699      1.1       rjs 			}
    700      1.1       rjs 			cntDel++;
    701      1.1       rjs 		} else {
    702      1.1       rjs 			if (sctp_sbspace(&stcb->sctp_socket->so_rcv) >=
    703      1.1       rjs 			    (long)chk->send_size) {
    704      1.1       rjs 				sbappend(&stcb->sctp_socket->so_rcv, chk->data);
    705      1.1       rjs 				cntDel++;
    706      1.1       rjs 			} else {
    707      1.1       rjs 				/* out of space in the sb */
    708      1.1       rjs 				sctp_sorwakeup(stcb->sctp_ep,
    709      1.1       rjs 					       stcb->sctp_socket);
    710      1.1       rjs 				if (hold_locks == 0) {
    711      1.1       rjs 					SCTP_INP_WUNLOCK(stcb->sctp_ep);
    712      1.1       rjs 				}
    713      1.1       rjs 				return;
    714      1.1       rjs 			}
    715      1.1       rjs 		}
    716      1.1       rjs 		/* pull it we did it */
    717      1.1       rjs 		TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
    718      1.1       rjs 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
    719      1.1       rjs 			asoc->fragmented_delivery_inprogress = 0;
    720      1.1       rjs 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
    721      1.1       rjs 				asoc->strmin[stream_no].last_sequence_delivered++;
    722      1.1       rjs 			}
    723      1.1       rjs 		}
    724      1.1       rjs 		asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
    725      1.1       rjs 		asoc->size_on_reasm_queue -= chk->send_size;
    726      1.1       rjs 		asoc->cnt_on_reasm_queue--;
    727      1.1       rjs 		/* free up the chk */
    728      1.1       rjs 		sctp_free_remote_addr(chk->whoTo);
    729      1.1       rjs 		chk->data = NULL;
    730      1.1       rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
    731      1.1       rjs 		sctppcbinfo.ipi_count_chunk--;
    732      1.1       rjs 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
    733      1.1       rjs 			panic("Chunk count is negative");
    734      1.1       rjs 		}
    735      1.1       rjs 		sctppcbinfo.ipi_gencnt_chunk++;
    736      1.1       rjs 		if (asoc->fragmented_delivery_inprogress == 0) {
    737      1.1       rjs 			/*
    738      1.1       rjs 			 * Now lets see if we can deliver the next one on the
    739      1.1       rjs 			 * stream
    740      1.1       rjs 			 */
    741      1.1       rjs 			/*u_int16_t nxt_todel;*/
    742      1.1       rjs 			struct sctp_stream_in *strm;
    743      1.1       rjs 
    744      1.1       rjs 			strm = &asoc->strmin[stream_no];
    745      1.1       rjs 			nxt_todel = strm->last_sequence_delivered + 1;
    746      1.1       rjs 			chk = TAILQ_FIRST(&strm->inqueue);
    747      1.1       rjs 			if (chk && (nxt_todel == chk->rec.data.stream_seq)) {
    748      1.1       rjs 				while (chk != NULL) {
    749      1.1       rjs 					/* all delivered */
    750      1.1       rjs 					if (nxt_todel ==
    751      1.1       rjs 					    chk->rec.data.stream_seq) {
    752      1.1       rjs 						at = TAILQ_NEXT(chk, sctp_next);
    753      1.1       rjs 						TAILQ_REMOVE(&strm->inqueue,
    754      1.1       rjs 							     chk, sctp_next);
    755      1.1       rjs 						asoc->size_on_all_streams -=
    756      1.1       rjs 							chk->send_size;
    757      1.1       rjs 						asoc->cnt_on_all_streams--;
    758      1.1       rjs 						strm->last_sequence_delivered++;
    759      1.1       rjs 						/*
    760      1.1       rjs 						 * We ignore the return of
    761      1.1       rjs 						 * deliver_data here since we
    762      1.1       rjs 						 * always can hold the chunk on
    763      1.1       rjs 						 * the d-queue. And we have a
    764      1.1       rjs 						 * finite number that can be
    765      1.1       rjs 						 * delivered from the strq.
    766      1.1       rjs 						 */
    767      1.1       rjs 						sctp_deliver_data(stcb, asoc, chk, 1);
    768      1.1       rjs 						chk = at;
    769      1.1       rjs 					} else {
    770      1.1       rjs 						break;
    771      1.1       rjs 					}
    772      1.1       rjs 					nxt_todel =
    773      1.1       rjs 						strm->last_sequence_delivered + 1;
    774      1.1       rjs 				}
    775      1.1       rjs 			}
    776      1.1       rjs 			if (!TAILQ_EMPTY(&asoc->delivery_queue)) {
    777      1.1       rjs 				/* Here if deliver_data fails, we must break */
    778      1.1       rjs 				if (sctp_deliver_data(stcb, asoc, NULL, 1) == 0)
    779      1.1       rjs 					break;
    780      1.1       rjs 			}
    781      1.1       rjs 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
    782      1.1       rjs 			if (hold_locks == 0) {
    783      1.1       rjs 				SCTP_INP_WUNLOCK(stcb->sctp_ep);
    784      1.1       rjs 			}
    785      1.1       rjs 			return;
    786      1.1       rjs 		}
    787      1.1       rjs 		chk = TAILQ_FIRST(&asoc->reasmqueue);
    788      1.1       rjs 	} while (chk);
    789      1.1       rjs 	if (cntDel) {
    790      1.1       rjs 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
    791      1.1       rjs 	}
    792      1.1       rjs 	if (hold_locks == 0) {
    793      1.1       rjs 		SCTP_INP_WUNLOCK(stcb->sctp_ep);
    794      1.1       rjs 	}
    795      1.1       rjs }
    796      1.1       rjs 
    797      1.1       rjs /*
    798      1.1       rjs  * Queue the chunk either right into the socket buffer if it is the next one
    799      1.1       rjs  * to go OR put it in the correct place in the delivery queue.  If we do
    800      1.1       rjs  * append to the so_buf, keep doing so until we are out of order.
    801      1.1       rjs  * One big question still remains, what to do when the socket buffer is FULL??
    802      1.1       rjs  */
    803      1.1       rjs static void
    804      1.1       rjs sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
    805      1.1       rjs     struct sctp_tmit_chunk *chk, int *abort_flag)
    806      1.1       rjs {
    807      1.1       rjs 	struct sctp_stream_in *strm;
    808      1.1       rjs 	struct sctp_tmit_chunk *at;
    809      1.1       rjs 	int queue_needed;
    810      1.1       rjs 	u_int16_t nxt_todel;
    811      1.1       rjs 	struct mbuf *oper;
    812      1.1       rjs 
    813      1.1       rjs /*** FIX FIX FIX ???
    814      1.1       rjs  * Need to add code to deal with 16 bit seq wrap
    815      1.1       rjs  * without a TSN wrap for ordered delivery (maybe).
    816      1.1       rjs  * FIX FIX FIX ???
    817      1.1       rjs  */
    818      1.1       rjs 	queue_needed = 1;
    819      1.1       rjs 	asoc->size_on_all_streams += chk->send_size;
    820      1.1       rjs 	asoc->cnt_on_all_streams++;
    821      1.1       rjs 	strm = &asoc->strmin[chk->rec.data.stream_number];
    822      1.1       rjs 	nxt_todel = strm->last_sequence_delivered + 1;
    823      1.1       rjs #ifdef SCTP_STR_LOGGING
    824      1.1       rjs 	sctp_log_strm_del(chk, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
    825      1.1       rjs #endif
    826      1.1       rjs #ifdef SCTP_DEBUG
    827      1.1       rjs 	if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
    828      1.1       rjs 		printf("queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
    829      1.1       rjs 		    (u_int)chk->rec.data.stream_seq,
    830      1.1       rjs 		    (u_int)strm->last_sequence_delivered, (u_int)nxt_todel);
    831      1.1       rjs 	}
    832      1.1       rjs #endif
    833      1.1       rjs 	if (compare_with_wrap(strm->last_sequence_delivered,
    834      1.1       rjs 	    chk->rec.data.stream_seq, MAX_SEQ) ||
    835      1.1       rjs 	    (strm->last_sequence_delivered == chk->rec.data.stream_seq)) {
    836      1.1       rjs 		/* The incoming sseq is behind where we last delivered? */
    837      1.1       rjs #ifdef SCTP_DEBUG
    838      1.1       rjs 		if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
    839      1.1       rjs 			printf("Duplicate S-SEQ:%d delivered:%d from peer, Abort  association\n",
    840      1.1       rjs 			    chk->rec.data.stream_seq,
    841      1.1       rjs 			    strm->last_sequence_delivered);
    842      1.1       rjs 		}
    843      1.1       rjs #endif
    844      1.1       rjs 		/*
    845      1.1       rjs 		 * throw it in the stream so it gets cleaned up in
    846      1.1       rjs 		 * association destruction
    847      1.1       rjs 		 */
    848      1.1       rjs 		TAILQ_INSERT_HEAD(&strm->inqueue, chk, sctp_next);
    849      1.1       rjs 		MGET(oper, M_DONTWAIT, MT_DATA);
    850      1.1       rjs 		if (oper) {
    851      1.1       rjs 			struct sctp_paramhdr *ph;
    852      1.1       rjs 			u_int32_t *ippp;
    853      1.1       rjs 
    854      1.1       rjs 			oper->m_len = sizeof(struct sctp_paramhdr) +
    855      1.1       rjs 			    sizeof(*ippp);
    856      1.1       rjs 			ph = mtod(oper, struct sctp_paramhdr *);
    857      1.1       rjs 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
    858      1.1       rjs 			ph->param_length = htons(oper->m_len);
    859      1.1       rjs 			ippp = (u_int32_t *)(ph + 1);
    860      1.1       rjs 			*ippp = htonl(0x00000001);
    861      1.1       rjs 		}
    862      1.1       rjs 		sctp_abort_an_association(stcb->sctp_ep, stcb,
    863      1.1       rjs 		    SCTP_PEER_FAULTY, oper);
    864      1.1       rjs 
    865      1.1       rjs 		*abort_flag = 1;
    866      1.1       rjs 		return;
    867      1.1       rjs 
    868      1.1       rjs 	}
    869      1.1       rjs 	if (nxt_todel == chk->rec.data.stream_seq) {
    870      1.1       rjs 		/* can be delivered right away */
    871      1.1       rjs #ifdef SCTP_DEBUG
    872      1.1       rjs 		if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
    873      1.1       rjs 			printf("It's NEXT!\n");
    874      1.1       rjs 		}
    875      1.1       rjs #endif
    876      1.1       rjs #ifdef SCTP_STR_LOGGING
    877      1.1       rjs 		sctp_log_strm_del(chk, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
    878      1.1       rjs #endif
    879      1.1       rjs 		queue_needed = 0;
    880      1.1       rjs 		asoc->size_on_all_streams -= chk->send_size;
    881      1.1       rjs 		asoc->cnt_on_all_streams--;
    882      1.1       rjs 		strm->last_sequence_delivered++;
    883      1.1       rjs 		sctp_deliver_data(stcb, asoc, chk, 0);
    884      1.1       rjs 		chk = TAILQ_FIRST(&strm->inqueue);
    885      1.1       rjs 		while (chk != NULL) {
    886      1.1       rjs 			/* all delivered */
    887      1.1       rjs 			nxt_todel = strm->last_sequence_delivered + 1;
    888      1.1       rjs 			if (nxt_todel == chk->rec.data.stream_seq) {
    889      1.1       rjs 				at = TAILQ_NEXT(chk, sctp_next);
    890      1.1       rjs 				TAILQ_REMOVE(&strm->inqueue, chk, sctp_next);
    891      1.1       rjs 				asoc->size_on_all_streams -= chk->send_size;
    892      1.1       rjs 				asoc->cnt_on_all_streams--;
    893      1.1       rjs 				strm->last_sequence_delivered++;
    894      1.1       rjs 				/*
    895      1.1       rjs 				 * We ignore the return of deliver_data here
    896      1.1       rjs 				 * since we always can hold the chunk on the
    897      1.1       rjs 				 * d-queue. And we have a finite number that
    898      1.1       rjs 				 * can be delivered from the strq.
    899      1.1       rjs 				 */
    900      1.1       rjs #ifdef SCTP_STR_LOGGING
    901      1.1       rjs 				sctp_log_strm_del(chk, NULL,
    902      1.1       rjs 				    SCTP_STR_LOG_FROM_IMMED_DEL);
    903      1.1       rjs #endif
    904      1.1       rjs 				sctp_deliver_data(stcb, asoc, chk, 0);
    905      1.1       rjs 				chk = at;
    906      1.1       rjs 				continue;
    907      1.1       rjs 			}
    908      1.1       rjs 			break;
    909      1.1       rjs 		}
    910      1.1       rjs 	}
    911      1.1       rjs 	if (queue_needed) {
    912      1.1       rjs 		/*
    913      1.1       rjs 		 * Ok, we did not deliver this guy, find
    914      1.1       rjs 		 * the correct place to put it on the queue.
    915      1.1       rjs 		 */
    916      1.1       rjs #ifdef SCTP_DEBUG
    917      1.1       rjs 		if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
    918      1.1       rjs 			printf("Queue Needed!\n");
    919      1.1       rjs 		}
    920      1.1       rjs #endif
    921      1.1       rjs 		if (TAILQ_EMPTY(&strm->inqueue)) {
    922      1.1       rjs 			/* Empty queue */
    923      1.1       rjs #ifdef SCTP_STR_LOGGING
    924      1.1       rjs 			sctp_log_strm_del(chk, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
    925      1.1       rjs #endif
    926      1.1       rjs 			TAILQ_INSERT_HEAD(&strm->inqueue, chk, sctp_next);
    927      1.1       rjs 		} else {
    928      1.1       rjs 			TAILQ_FOREACH(at, &strm->inqueue, sctp_next) {
    929      1.1       rjs 				if (compare_with_wrap(at->rec.data.stream_seq,
    930      1.1       rjs 				    chk->rec.data.stream_seq, MAX_SEQ)) {
    931      1.1       rjs 					/*
    932      1.1       rjs 					 * one in queue is bigger than the new
    933      1.1       rjs 					 * one, insert before this one
    934      1.1       rjs 					 */
    935      1.1       rjs #ifdef SCTP_STR_LOGGING
    936      1.1       rjs 					sctp_log_strm_del(chk, at,
    937      1.1       rjs 					    SCTP_STR_LOG_FROM_INSERT_MD);
    938      1.1       rjs #endif
    939      1.1       rjs 					TAILQ_INSERT_BEFORE(at, chk, sctp_next);
    940      1.1       rjs 					break;
    941      1.1       rjs 				} else if (at->rec.data.stream_seq ==
    942      1.1       rjs 				    chk->rec.data.stream_seq) {
    943      1.1       rjs 					/*
    944      1.1       rjs 					 * Gak, He sent me a duplicate str seq
    945      1.1       rjs 					 * number
    946      1.1       rjs 					 */
    947      1.1       rjs 					/*
    948      1.1       rjs 					 * foo bar, I guess I will just free
    949      1.1       rjs 					 * this new guy, should we abort too?
    950      1.1       rjs 					 * FIX ME MAYBE? Or it COULD be that
    951      1.1       rjs 					 * the SSN's have wrapped. Maybe I
    952      1.1       rjs 					 * should compare to TSN somehow...
    953      1.1       rjs 					 * sigh for now just blow away the
    954      1.1       rjs 					 * chunk!
    955      1.1       rjs 					 */
    956      1.1       rjs 
    957      1.1       rjs 					if (chk->data)
    958      1.1       rjs 						sctp_m_freem(chk->data);
    959      1.1       rjs 					chk->data = NULL;
    960      1.1       rjs 					asoc->size_on_all_streams -= chk->send_size;
    961      1.1       rjs 					asoc->cnt_on_all_streams--;
    962      1.1       rjs 					sctp_pegs[SCTP_DUP_SSN_RCVD]++;
    963      1.1       rjs 					sctp_free_remote_addr(chk->whoTo);
    964      1.1       rjs 					SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
    965      1.1       rjs 					sctppcbinfo.ipi_count_chunk--;
    966      1.1       rjs 					if ((int)sctppcbinfo.ipi_count_chunk <
    967      1.1       rjs 					    0) {
    968      1.1       rjs 						panic("Chunk count is negative");
    969      1.1       rjs 					}
    970      1.1       rjs 					sctppcbinfo.ipi_gencnt_chunk++;
    971      1.1       rjs 					return;
    972      1.1       rjs 				} else {
    973      1.1       rjs 					if (TAILQ_NEXT(at, sctp_next) == NULL) {
    974      1.1       rjs 						/*
    975      1.1       rjs 						 * We are at the end, insert it
    976      1.1       rjs 						 * after this one
    977      1.1       rjs 						 */
    978      1.1       rjs #ifdef SCTP_STR_LOGGING
    979      1.1       rjs 						sctp_log_strm_del(chk, at,
    980      1.1       rjs 						    SCTP_STR_LOG_FROM_INSERT_TL);
    981      1.1       rjs #endif
    982      1.1       rjs 						TAILQ_INSERT_AFTER(&strm->inqueue,
    983      1.1       rjs 						    at, chk, sctp_next);
    984      1.1       rjs 						break;
    985      1.1       rjs 					}
    986      1.1       rjs 				}
    987      1.1       rjs 			}
    988      1.1       rjs 		}
    989      1.1       rjs 	} else {
    990      1.1       rjs 		/* We delivered some chunks, wake them up */
    991      1.1       rjs 
    992      1.1       rjs #ifdef SCTP_DEBUG
    993      1.1       rjs 		if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
    994      1.1       rjs 			printf("Doing WAKEUP!\n");
    995      1.1       rjs 		}
    996      1.1       rjs #endif
    997      1.1       rjs 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
    998      1.1       rjs 	}
    999      1.1       rjs }
   1000      1.1       rjs 
   1001      1.1       rjs /*
   1002      1.1       rjs  * Returns two things: You get the total size of the deliverable parts of the
   1003      1.1       rjs  * first fragmented message on the reassembly queue. And you get a 1 back if
   1004      1.1       rjs  * all of the message is ready or a 0 back if the message is still incomplete
   1005      1.1       rjs  */
   1006      1.1       rjs static int
   1007      1.1       rjs sctp_is_all_msg_on_reasm(struct sctp_association *asoc, int *t_size)
   1008      1.1       rjs {
   1009      1.1       rjs 	struct sctp_tmit_chunk *chk;
   1010      1.1       rjs 	u_int32_t tsn;
   1011      1.1       rjs 
   1012      1.1       rjs 	*t_size = 0;
   1013      1.1       rjs 	chk = TAILQ_FIRST(&asoc->reasmqueue);
   1014      1.1       rjs 	if (chk == NULL) {
   1015      1.1       rjs 		/* nothing on the queue */
   1016      1.1       rjs 		return (0);
   1017      1.1       rjs 	}
   1018      1.1       rjs 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
   1019      1.1       rjs 		/* Not a first on the queue */
   1020      1.1       rjs 		return (0);
   1021      1.1       rjs 	}
   1022      1.1       rjs 	tsn = chk->rec.data.TSN_seq;
   1023      1.1       rjs 	while (chk) {
   1024      1.1       rjs 		if (tsn != chk->rec.data.TSN_seq) {
   1025      1.1       rjs 			return (0);
   1026      1.1       rjs 		}
   1027      1.1       rjs 		*t_size += chk->send_size;
   1028      1.1       rjs 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
   1029      1.1       rjs 			return (1);
   1030      1.1       rjs 		}
   1031      1.1       rjs 		tsn++;
   1032      1.1       rjs 		chk = TAILQ_NEXT(chk, sctp_next);
   1033      1.1       rjs 	}
   1034      1.1       rjs 	return (0);
   1035      1.1       rjs }
   1036      1.1       rjs 
   1037      1.1       rjs /*
   1038      1.1       rjs  * Dump onto the re-assembly queue, in its proper place. After dumping on
   1039      1.1       rjs  * the queue, see if anthing can be delivered. If so pull it off (or as much
   1040      1.1       rjs  * as we can. If we run out of space then we must dump what we can and set
   1041      1.1       rjs  * the appropriate flag to say we queued what we could.
   1042      1.1       rjs  */
   1043      1.1       rjs static void
   1044      1.1       rjs sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
   1045      1.1       rjs     struct sctp_tmit_chunk *chk, int *abort_flag)
   1046      1.1       rjs {
   1047      1.1       rjs 	struct mbuf *oper;
   1048      1.1       rjs 	u_int16_t nxt_todel;
   1049      1.1       rjs 	u_int32_t cum_ackp1, prev_tsn, post_tsn;
   1050      1.1       rjs 	int tsize;
   1051      1.1       rjs 	struct sctp_tmit_chunk *at, *prev, *next;
   1052      1.1       rjs 
   1053      1.1       rjs 	prev = next = NULL;
   1054      1.1       rjs 	cum_ackp1 = asoc->tsn_last_delivered + 1;
   1055      1.1       rjs 
   1056      1.1       rjs 	if (TAILQ_EMPTY(&asoc->reasmqueue)) {
   1057      1.1       rjs 		/* This is the first one on the queue */
   1058      1.1       rjs 		TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
   1059      1.1       rjs 		/*
   1060      1.1       rjs 		 * we do not check for delivery of anything when
   1061      1.1       rjs 		 * only one fragment is here
   1062      1.1       rjs 		 */
   1063      1.1       rjs 		asoc->size_on_reasm_queue = chk->send_size;
   1064      1.1       rjs 		asoc->cnt_on_reasm_queue++;
   1065      1.1       rjs 		if (chk->rec.data.TSN_seq == cum_ackp1) {
   1066      1.1       rjs 			if (asoc->fragmented_delivery_inprogress == 0  &&
   1067      1.1       rjs 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
   1068      1.1       rjs 			    SCTP_DATA_FIRST_FRAG) {
   1069      1.1       rjs 				/*
   1070      1.1       rjs 				 * An empty queue, no delivery inprogress, we
   1071      1.1       rjs 				 * hit the next one and it does NOT have a
   1072      1.1       rjs 				 * FIRST fragment mark.
   1073      1.1       rjs 				 */
   1074      1.1       rjs #ifdef SCTP_DEBUG
   1075      1.1       rjs 				if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   1076      1.1       rjs 					printf("Gak, Evil plot, its not first, no fragmented delivery in progress\n");
   1077      1.1       rjs 				}
   1078      1.1       rjs #endif
   1079      1.1       rjs 				MGET(oper, M_DONTWAIT, MT_DATA);
   1080      1.1       rjs 				if (oper) {
   1081      1.1       rjs 					struct sctp_paramhdr *ph;
   1082      1.1       rjs 					u_int32_t *ippp;
   1083      1.1       rjs 
   1084      1.1       rjs 					oper->m_len =
   1085      1.1       rjs 					    sizeof(struct sctp_paramhdr) +
   1086      1.1       rjs 					    sizeof(*ippp);
   1087      1.1       rjs 					ph = mtod(oper, struct sctp_paramhdr *);
   1088      1.1       rjs 					ph->param_type =
   1089      1.1       rjs 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   1090      1.1       rjs 					ph->param_length = htons(oper->m_len);
   1091      1.1       rjs 					ippp = (u_int32_t *)(ph + 1);
   1092      1.1       rjs 					*ippp = htonl(0x10000001);
   1093      1.1       rjs 				}
   1094      1.1       rjs 				sctp_abort_an_association(stcb->sctp_ep, stcb,
   1095      1.1       rjs 				    SCTP_PEER_FAULTY, oper);
   1096      1.1       rjs 				*abort_flag = 1;
   1097      1.1       rjs 			} else if (asoc->fragmented_delivery_inprogress &&
   1098      1.1       rjs 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
   1099      1.1       rjs 				/*
   1100      1.1       rjs 				 * We are doing a partial delivery and the NEXT
   1101      1.1       rjs 				 * chunk MUST be either the LAST or MIDDLE
   1102      1.1       rjs 				 * fragment NOT a FIRST
   1103      1.1       rjs 				 */
   1104      1.1       rjs #ifdef SCTP_DEBUG
   1105      1.1       rjs 				if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   1106      1.1       rjs 					printf("Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
   1107      1.1       rjs 				}
   1108      1.1       rjs #endif
   1109      1.1       rjs 				MGET(oper, M_DONTWAIT, MT_DATA);
   1110      1.1       rjs 				if (oper) {
   1111      1.1       rjs 					struct sctp_paramhdr *ph;
   1112      1.1       rjs 					u_int32_t *ippp;
   1113      1.1       rjs 
   1114      1.1       rjs 					oper->m_len =
   1115      1.1       rjs 					    sizeof(struct sctp_paramhdr) +
   1116      1.1       rjs 					    sizeof(*ippp);
   1117      1.1       rjs 					ph = mtod(oper, struct sctp_paramhdr *);
   1118      1.1       rjs 					ph->param_type =
   1119      1.1       rjs 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   1120      1.1       rjs 					ph->param_length = htons(oper->m_len);
   1121      1.1       rjs 					ippp = (u_int32_t *)(ph + 1);
   1122      1.1       rjs 					*ippp = htonl(0x10000002);
   1123      1.1       rjs 				}
   1124      1.1       rjs 				sctp_abort_an_association(stcb->sctp_ep, stcb,
   1125      1.1       rjs 				    SCTP_PEER_FAULTY, oper);
   1126      1.1       rjs 				*abort_flag = 1;
   1127      1.1       rjs 			} else if (asoc->fragmented_delivery_inprogress) {
   1128      1.1       rjs 				/* Here we are ok with a MIDDLE or LAST piece */
   1129      1.1       rjs 				if (chk->rec.data.stream_number !=
   1130      1.1       rjs 				    asoc->str_of_pdapi) {
   1131      1.1       rjs 					/* Got to be the right STR No */
   1132      1.1       rjs #ifdef SCTP_DEBUG
   1133      1.1       rjs 					if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   1134      1.1       rjs 						printf("Gak, Evil plot, it IS not same stream number %d vs %d\n",
   1135      1.1       rjs 						    chk->rec.data.stream_number,
   1136      1.1       rjs 						    asoc->str_of_pdapi);
   1137      1.1       rjs 					}
   1138      1.1       rjs #endif
   1139      1.1       rjs 					MGET(oper, M_DONTWAIT, MT_DATA);
   1140      1.1       rjs 					if (oper) {
   1141      1.1       rjs 						struct sctp_paramhdr *ph;
   1142      1.1       rjs 						u_int32_t *ippp;
   1143      1.1       rjs 						oper->m_len =
   1144      1.1       rjs 						    sizeof(struct sctp_paramhdr) +
   1145      1.1       rjs 						    sizeof(*ippp);
   1146      1.1       rjs 						ph = mtod(oper,
   1147      1.1       rjs 						    struct sctp_paramhdr *);
   1148      1.1       rjs 						ph->param_type =
   1149      1.1       rjs 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   1150      1.1       rjs 						ph->param_length =
   1151      1.1       rjs 						    htons(oper->m_len);
   1152      1.1       rjs 						ippp = (u_int32_t *)(ph + 1);
   1153      1.1       rjs 						*ippp = htonl(0x10000003);
   1154      1.1       rjs 					}
   1155      1.1       rjs 					sctp_abort_an_association(stcb->sctp_ep,
   1156      1.1       rjs 					    stcb, SCTP_PEER_FAULTY, oper);
   1157      1.1       rjs 					*abort_flag = 1;
   1158      1.1       rjs 				} else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
   1159      1.1       rjs 				    SCTP_DATA_UNORDERED &&
   1160      1.1       rjs 				    chk->rec.data.stream_seq !=
   1161      1.1       rjs 				    asoc->ssn_of_pdapi) {
   1162      1.1       rjs 					/* Got to be the right STR Seq */
   1163      1.1       rjs #ifdef SCTP_DEBUG
   1164      1.1       rjs 					if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   1165      1.1       rjs 						printf("Gak, Evil plot, it IS not same stream seq %d vs %d\n",
   1166      1.1       rjs 						    chk->rec.data.stream_seq,
   1167      1.1       rjs 						    asoc->ssn_of_pdapi);
   1168      1.1       rjs 					}
   1169      1.1       rjs #endif
   1170      1.1       rjs 					MGET(oper, M_DONTWAIT, MT_DATA);
   1171      1.1       rjs 					if (oper) {
   1172      1.1       rjs 						struct sctp_paramhdr *ph;
   1173      1.1       rjs 						u_int32_t *ippp;
   1174      1.1       rjs 						oper->m_len =
   1175      1.1       rjs 						    sizeof(struct sctp_paramhdr) +
   1176      1.1       rjs 						    sizeof(*ippp);
   1177      1.1       rjs 						ph = mtod(oper,
   1178      1.1       rjs 						    struct sctp_paramhdr *);
   1179      1.1       rjs 						ph->param_type =
   1180      1.1       rjs 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   1181      1.1       rjs 						ph->param_length =
   1182      1.1       rjs 						    htons(oper->m_len);
   1183      1.1       rjs 						ippp = (u_int32_t *)(ph + 1);
   1184      1.1       rjs 						*ippp = htonl(0x10000004);
   1185      1.1       rjs 					}
   1186      1.1       rjs 					sctp_abort_an_association(stcb->sctp_ep,
   1187      1.1       rjs 					    stcb, SCTP_PEER_FAULTY, oper);
   1188      1.1       rjs 					*abort_flag = 1;
   1189      1.1       rjs 				}
   1190      1.1       rjs 			}
   1191      1.1       rjs 		}
   1192      1.1       rjs 		return;
   1193      1.1       rjs 	}
   1194      1.1       rjs 	/* Find its place */
   1195      1.1       rjs 	at = TAILQ_FIRST(&asoc->reasmqueue);
   1196      1.1       rjs 
   1197      1.1       rjs 	/* Grab the top flags */
   1198      1.1       rjs 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
   1199      1.1       rjs 		if (compare_with_wrap(at->rec.data.TSN_seq,
   1200      1.1       rjs 		    chk->rec.data.TSN_seq, MAX_TSN)) {
   1201      1.1       rjs 			/*
   1202      1.1       rjs 			 * one in queue is bigger than the new one, insert
   1203      1.1       rjs 			 * before this one
   1204      1.1       rjs 			 */
   1205      1.1       rjs 			/* A check */
   1206      1.1       rjs 			asoc->size_on_reasm_queue += chk->send_size;
   1207      1.1       rjs 			asoc->cnt_on_reasm_queue++;
   1208      1.1       rjs 			next = at;
   1209      1.1       rjs 			TAILQ_INSERT_BEFORE(at, chk, sctp_next);
   1210      1.1       rjs 			break;
   1211      1.1       rjs 		} else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
   1212      1.1       rjs 			/* Gak, He sent me a duplicate str seq number */
   1213      1.1       rjs 			/*
   1214      1.1       rjs 			 * foo bar, I guess I will just free this new guy,
   1215      1.1       rjs 			 * should we abort too? FIX ME MAYBE? Or it COULD be
   1216      1.1       rjs 			 * that the SSN's have wrapped. Maybe I should compare
   1217      1.1       rjs 			 * to TSN somehow... sigh for now just blow away the
   1218      1.1       rjs 			 * chunk!
   1219      1.1       rjs 			 */
   1220      1.1       rjs 			if (chk->data)
   1221      1.1       rjs 				sctp_m_freem(chk->data);
   1222      1.1       rjs 			chk->data = NULL;
   1223      1.1       rjs 			sctp_free_remote_addr(chk->whoTo);
   1224      1.1       rjs 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   1225      1.1       rjs 			sctppcbinfo.ipi_count_chunk--;
   1226      1.1       rjs 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   1227      1.1       rjs 				panic("Chunk count is negative");
   1228      1.1       rjs 			}
   1229      1.1       rjs 			sctppcbinfo.ipi_gencnt_chunk++;
   1230      1.1       rjs 			return;
   1231      1.1       rjs 		} else {
   1232      1.1       rjs 			prev = at;
   1233      1.1       rjs 			if (TAILQ_NEXT(at, sctp_next) == NULL) {
   1234      1.1       rjs 				/*
   1235      1.1       rjs 				 * We are at the end, insert it after this one
   1236      1.1       rjs 				 */
   1237      1.1       rjs 				/* check it first */
   1238      1.1       rjs 				asoc->size_on_reasm_queue += chk->send_size;
   1239      1.1       rjs 				asoc->cnt_on_reasm_queue++;
   1240      1.1       rjs 				TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
   1241      1.1       rjs 				break;
   1242      1.1       rjs 			}
   1243      1.1       rjs 		}
   1244      1.1       rjs 	}
   1245      1.1       rjs 	/* Now the audits */
   1246      1.1       rjs 	if (prev) {
   1247      1.1       rjs 		prev_tsn = chk->rec.data.TSN_seq - 1;
   1248      1.1       rjs 		if (prev_tsn == prev->rec.data.TSN_seq) {
   1249      1.1       rjs 			/*
   1250      1.1       rjs 			 * Ok the one I am dropping onto the end
   1251      1.1       rjs 			 * is the NEXT. A bit of valdiation here.
   1252      1.1       rjs 			 */
   1253      1.1       rjs 			if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
   1254      1.1       rjs 			    SCTP_DATA_FIRST_FRAG ||
   1255      1.1       rjs 			    (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
   1256      1.1       rjs 			    SCTP_DATA_MIDDLE_FRAG) {
   1257      1.1       rjs 				/*
   1258      1.1       rjs 				 * Insert chk MUST be a MIDDLE or LAST fragment
   1259      1.1       rjs 				 */
   1260      1.1       rjs 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
   1261      1.1       rjs 				    SCTP_DATA_FIRST_FRAG) {
   1262      1.1       rjs #ifdef SCTP_DEBUG
   1263      1.1       rjs 					if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   1264      1.1       rjs 						printf("Prev check - It can be a midlle or last but not a first\n");
   1265      1.1       rjs 						printf("Gak, Evil plot, it's a FIRST!\n");
   1266      1.1       rjs 					}
   1267      1.1       rjs #endif
   1268      1.1       rjs 					MGET(oper, M_DONTWAIT, MT_DATA);
   1269      1.1       rjs 					if (oper) {
   1270      1.1       rjs 						struct sctp_paramhdr *ph;
   1271      1.1       rjs 						u_int32_t *ippp;
   1272      1.1       rjs 
   1273      1.1       rjs 						oper->m_len =
   1274      1.1       rjs 						    sizeof(struct sctp_paramhdr) +
   1275      1.1       rjs 						    sizeof(*ippp);
   1276      1.1       rjs 						ph = mtod(oper,
   1277      1.1       rjs 						    struct sctp_paramhdr *);
   1278      1.1       rjs 						ph->param_type =
   1279      1.1       rjs 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   1280      1.1       rjs 						ph->param_length =
   1281      1.1       rjs 						    htons(oper->m_len);
   1282      1.1       rjs 
   1283      1.1       rjs 						ippp = (u_int32_t *)(ph + 1);
   1284      1.1       rjs 						*ippp = htonl(0x10000005);
   1285      1.1       rjs 					}
   1286      1.1       rjs 					sctp_abort_an_association(stcb->sctp_ep,
   1287      1.1       rjs 					    stcb, SCTP_PEER_FAULTY, oper);
   1288      1.1       rjs 					*abort_flag = 1;
   1289      1.1       rjs 					return;
   1290      1.1       rjs 				}
   1291      1.1       rjs 				if (chk->rec.data.stream_number !=
   1292      1.1       rjs 				    prev->rec.data.stream_number) {
   1293      1.1       rjs 					/*
   1294      1.1       rjs 					 * Huh, need the correct STR here, they
   1295      1.1       rjs 					 * must be the same.
   1296      1.1       rjs 					 */
   1297      1.1       rjs #ifdef SCTP_DEBUG
   1298      1.1       rjs 					if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   1299      1.1       rjs 						printf("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
   1300      1.1       rjs 						    chk->rec.data.stream_number,
   1301      1.1       rjs 						    prev->rec.data.stream_number);
   1302      1.1       rjs 					}
   1303      1.1       rjs #endif
   1304      1.1       rjs 					MGET(oper, M_DONTWAIT, MT_DATA);
   1305      1.1       rjs 					if (oper) {
   1306      1.1       rjs 						struct sctp_paramhdr *ph;
   1307      1.1       rjs 						u_int32_t *ippp;
   1308      1.1       rjs 
   1309      1.1       rjs 						oper->m_len =
   1310      1.1       rjs 						    sizeof(struct sctp_paramhdr) +
   1311      1.1       rjs 						    sizeof(*ippp);
   1312      1.1       rjs 						ph = mtod(oper,
   1313      1.1       rjs 						    struct sctp_paramhdr *);
   1314      1.1       rjs 						ph->param_type =
   1315      1.1       rjs 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   1316      1.1       rjs 						ph->param_length =
   1317      1.1       rjs 						    htons(oper->m_len);
   1318      1.1       rjs 						ippp = (u_int32_t *)(ph + 1);
   1319      1.1       rjs 						*ippp = htonl(0x10000006);
   1320      1.1       rjs 					}
   1321      1.1       rjs 
   1322      1.1       rjs 					sctp_abort_an_association(stcb->sctp_ep,
   1323      1.1       rjs 					    stcb, SCTP_PEER_FAULTY, oper);
   1324      1.1       rjs 
   1325      1.1       rjs 					*abort_flag = 1;
   1326      1.1       rjs 					return;
   1327      1.1       rjs 				}
   1328      1.1       rjs 				if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
   1329      1.1       rjs 				    chk->rec.data.stream_seq !=
   1330      1.1       rjs 				    prev->rec.data.stream_seq) {
   1331      1.1       rjs 					/*
   1332      1.1       rjs 					 * Huh, need the correct STR here, they
   1333      1.1       rjs 					 * must be the same.
   1334      1.1       rjs 					 */
   1335      1.1       rjs #ifdef SCTP_DEBUG
   1336      1.1       rjs 					if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   1337      1.1       rjs 						printf("Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
   1338      1.1       rjs 						    chk->rec.data.stream_seq,
   1339      1.1       rjs 						    prev->rec.data.stream_seq);
   1340      1.1       rjs 					}
   1341      1.1       rjs #endif
   1342      1.1       rjs 					MGET(oper, M_DONTWAIT, MT_DATA);
   1343      1.1       rjs 					if (oper) {
   1344      1.1       rjs 						struct sctp_paramhdr *ph;
   1345      1.1       rjs 						u_int32_t *ippp;
   1346      1.1       rjs 
   1347      1.1       rjs 						oper->m_len =
   1348      1.1       rjs 						    sizeof(struct sctp_paramhdr) +
   1349      1.1       rjs 						    sizeof(*ippp);
   1350      1.1       rjs 						ph = mtod(oper,
   1351      1.1       rjs 						    struct sctp_paramhdr *);
   1352      1.1       rjs 						ph->param_type =
   1353      1.1       rjs 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   1354      1.1       rjs 						ph->param_length =
   1355      1.1       rjs 						    htons(oper->m_len);
   1356      1.1       rjs 						ippp = (u_int32_t *)(ph + 1);
   1357      1.1       rjs 						*ippp = htonl(0x10000007);
   1358      1.1       rjs 					}
   1359      1.1       rjs 
   1360      1.1       rjs 					sctp_abort_an_association(stcb->sctp_ep,
   1361      1.1       rjs 					    stcb, SCTP_PEER_FAULTY, oper);
   1362      1.1       rjs 
   1363      1.1       rjs 					*abort_flag = 1;
   1364      1.1       rjs 					return;
   1365      1.1       rjs 				}
   1366      1.1       rjs 			} else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
   1367      1.1       rjs 			    SCTP_DATA_LAST_FRAG) {
   1368      1.1       rjs 				/* Insert chk MUST be a FIRST */
   1369      1.1       rjs 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
   1370      1.1       rjs 				    SCTP_DATA_FIRST_FRAG) {
   1371      1.1       rjs #ifdef SCTP_DEBUG
   1372      1.1       rjs 					if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   1373      1.1       rjs 						printf("Prev check - Gak, evil plot, its not FIRST and it must be!\n");
   1374      1.1       rjs 					}
   1375      1.1       rjs #endif
   1376      1.1       rjs 					MGET(oper, M_DONTWAIT, MT_DATA);
   1377      1.1       rjs 					if (oper) {
   1378      1.1       rjs 						struct sctp_paramhdr *ph;
   1379      1.1       rjs 						u_int32_t *ippp;
   1380      1.1       rjs 
   1381      1.1       rjs 						oper->m_len =
   1382      1.1       rjs 						    sizeof(struct sctp_paramhdr) +
   1383      1.1       rjs 						    sizeof(*ippp);
   1384      1.1       rjs 						ph = mtod(oper,
   1385      1.1       rjs 						    struct sctp_paramhdr *);
   1386      1.1       rjs 						ph->param_type =
   1387      1.1       rjs 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   1388      1.1       rjs 						ph->param_length =
   1389      1.1       rjs 						    htons(oper->m_len);
   1390      1.1       rjs 						ippp = (u_int32_t *)(ph + 1);
   1391      1.1       rjs 						*ippp = htonl(0x10000008);
   1392      1.1       rjs 					}
   1393      1.1       rjs 
   1394      1.1       rjs 					sctp_abort_an_association(stcb->sctp_ep,
   1395      1.1       rjs 					    stcb, SCTP_PEER_FAULTY, oper);
   1396      1.1       rjs 
   1397      1.1       rjs 					*abort_flag = 1;
   1398      1.1       rjs 					return;
   1399      1.1       rjs 				}
   1400      1.1       rjs 			}
   1401      1.1       rjs 		}
   1402      1.1       rjs 	}
   1403      1.1       rjs 
   1404      1.1       rjs 	if (next) {
   1405      1.1       rjs 		post_tsn = chk->rec.data.TSN_seq + 1;
   1406      1.1       rjs 		if (post_tsn == next->rec.data.TSN_seq) {
   1407      1.1       rjs 			/*
   1408      1.1       rjs 			 * Ok the one I am inserting ahead of
   1409      1.1       rjs 			 * is my NEXT one. A bit of valdiation here.
   1410      1.1       rjs 			 */
   1411      1.1       rjs 			if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
   1412      1.1       rjs 				/* Insert chk MUST be a last fragment */
   1413      1.1       rjs 				if ((chk->rec.data.rcv_flags&SCTP_DATA_FRAG_MASK)
   1414      1.1       rjs 				   != SCTP_DATA_LAST_FRAG) {
   1415      1.1       rjs #ifdef SCTP_DEBUG
   1416      1.1       rjs 					if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   1417      1.1       rjs 						printf("Next chk - Next is FIRST, we must be LAST\n");
   1418      1.1       rjs 						printf("Gak, Evil plot, its not a last!\n");
   1419      1.1       rjs 					}
   1420      1.1       rjs #endif
   1421      1.1       rjs 					MGET(oper, M_DONTWAIT, MT_DATA);
   1422      1.1       rjs 					if (oper) {
   1423      1.1       rjs 						struct sctp_paramhdr *ph;
   1424      1.1       rjs 						u_int32_t *ippp;
   1425      1.1       rjs 
   1426      1.1       rjs 						oper->m_len =
   1427      1.1       rjs 						    sizeof(struct sctp_paramhdr) +
   1428      1.1       rjs 						    sizeof(*ippp);
   1429      1.1       rjs 						ph = mtod(oper,
   1430      1.1       rjs 						    struct sctp_paramhdr *);
   1431      1.1       rjs 						ph->param_type =
   1432      1.1       rjs 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   1433      1.1       rjs 						ph->param_length =
   1434      1.1       rjs 						    htons(oper->m_len);
   1435      1.1       rjs 						ippp = (u_int32_t *)(ph + 1);
   1436      1.1       rjs 						*ippp = htonl(0x10000009);
   1437      1.1       rjs 					}
   1438      1.1       rjs 
   1439      1.1       rjs 					sctp_abort_an_association(stcb->sctp_ep,
   1440      1.1       rjs 					    stcb, SCTP_PEER_FAULTY, oper);
   1441      1.1       rjs 
   1442      1.1       rjs 					*abort_flag = 1;
   1443      1.1       rjs 					return;
   1444      1.1       rjs 				}
   1445      1.1       rjs 			} else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
   1446      1.1       rjs 			    SCTP_DATA_MIDDLE_FRAG ||
   1447      1.1       rjs 			    (next->rec.data.rcv_flags&SCTP_DATA_FRAG_MASK) ==
   1448      1.1       rjs 			    SCTP_DATA_LAST_FRAG) {
   1449      1.1       rjs 				/* Insert chk CAN be MIDDLE or FIRST NOT LAST */
   1450      1.1       rjs 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
   1451      1.1       rjs 				    SCTP_DATA_LAST_FRAG) {
   1452      1.1       rjs #ifdef SCTP_DEBUG
   1453      1.1       rjs 					if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   1454      1.1       rjs 						printf("Next chk - Next is a MIDDLE/LAST\n");
   1455      1.1       rjs 						printf("Gak, Evil plot, new prev chunk is a LAST\n");
   1456      1.1       rjs 					}
   1457      1.1       rjs #endif
   1458      1.1       rjs 					MGET(oper, M_DONTWAIT, MT_DATA);
   1459      1.1       rjs 					if (oper) {
   1460      1.1       rjs 						struct sctp_paramhdr *ph;
   1461      1.1       rjs 						u_int32_t *ippp;
   1462      1.1       rjs 
   1463      1.1       rjs 						oper->m_len =
   1464      1.1       rjs 						    sizeof(struct sctp_paramhdr) +
   1465      1.1       rjs 						    sizeof(*ippp);
   1466      1.1       rjs 						ph = mtod(oper,
   1467      1.1       rjs 						    struct sctp_paramhdr *);
   1468      1.1       rjs 						ph->param_type =
   1469      1.1       rjs 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   1470      1.1       rjs 						ph->param_length =
   1471      1.1       rjs 						    htons(oper->m_len);
   1472      1.1       rjs 						ippp = (u_int32_t *)(ph + 1);
   1473      1.1       rjs 						*ippp = htonl(0x1000000a);
   1474      1.1       rjs 					}
   1475      1.1       rjs 					sctp_abort_an_association(stcb->sctp_ep,
   1476      1.1       rjs 					    stcb, SCTP_PEER_FAULTY, oper);
   1477      1.1       rjs 
   1478      1.1       rjs 					*abort_flag = 1;
   1479      1.1       rjs 					return;
   1480      1.1       rjs 				}
   1481      1.1       rjs 				if (chk->rec.data.stream_number !=
   1482      1.1       rjs 				    next->rec.data.stream_number) {
   1483      1.1       rjs 					/*
   1484      1.1       rjs 					 * Huh, need the correct STR here, they
   1485      1.1       rjs 					 * must be the same.
   1486      1.1       rjs 					 */
   1487      1.1       rjs #ifdef SCTP_DEBUG
   1488      1.1       rjs 					if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   1489      1.1       rjs 						printf("Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
   1490      1.1       rjs 						    chk->rec.data.stream_number,
   1491      1.1       rjs 						    next->rec.data.stream_number);
   1492      1.1       rjs 					}
   1493      1.1       rjs #endif
   1494      1.1       rjs 					MGET(oper, M_DONTWAIT, MT_DATA);
   1495      1.1       rjs 					if (oper) {
   1496      1.1       rjs 						struct sctp_paramhdr *ph;
   1497      1.1       rjs 						u_int32_t *ippp;
   1498      1.1       rjs 
   1499      1.1       rjs 						oper->m_len =
   1500      1.1       rjs 						    sizeof(struct sctp_paramhdr) +
   1501      1.1       rjs 						    sizeof(*ippp);
   1502      1.1       rjs 						ph = mtod(oper,
   1503      1.1       rjs 						    struct sctp_paramhdr *);
   1504      1.1       rjs 						ph->param_type =
   1505      1.1       rjs 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   1506      1.1       rjs 						ph->param_length =
   1507      1.1       rjs 						    htons(oper->m_len);
   1508      1.1       rjs 						ippp = (u_int32_t *)(ph + 1);
   1509      1.1       rjs 						*ippp = htonl(0x1000000b);
   1510      1.1       rjs 					}
   1511      1.1       rjs 
   1512      1.1       rjs 					sctp_abort_an_association(stcb->sctp_ep,
   1513      1.1       rjs 					    stcb, SCTP_PEER_FAULTY, oper);
   1514      1.1       rjs 
   1515      1.1       rjs 					*abort_flag = 1;
   1516      1.1       rjs 					return;
   1517      1.1       rjs 				}
   1518      1.1       rjs 				if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
   1519      1.1       rjs 				    chk->rec.data.stream_seq !=
   1520      1.1       rjs 				    next->rec.data.stream_seq) {
   1521      1.1       rjs 					/*
   1522      1.1       rjs 					 * Huh, need the correct STR here, they
   1523      1.1       rjs 					 * must be the same.
   1524      1.1       rjs 					 */
   1525      1.1       rjs #ifdef SCTP_DEBUG
   1526      1.1       rjs 					if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   1527      1.1       rjs 						printf("Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
   1528      1.1       rjs 						    chk->rec.data.stream_seq,
   1529      1.1       rjs 						    next->rec.data.stream_seq);
   1530      1.1       rjs 					}
   1531      1.1       rjs #endif
   1532      1.1       rjs 					MGET(oper, M_DONTWAIT, MT_DATA);
   1533      1.1       rjs 					if (oper) {
   1534      1.1       rjs 						struct sctp_paramhdr *ph;
   1535      1.1       rjs 						u_int32_t *ippp;
   1536      1.1       rjs 
   1537      1.1       rjs 						oper->m_len =
   1538      1.1       rjs 						    sizeof(struct sctp_paramhdr) +
   1539      1.1       rjs 						    sizeof(*ippp);
   1540      1.1       rjs 						ph = mtod(oper,
   1541      1.1       rjs 						    struct sctp_paramhdr *);
   1542      1.1       rjs 						ph->param_type =
   1543      1.1       rjs 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   1544      1.1       rjs 						ph->param_length =
   1545      1.1       rjs 						    htons(oper->m_len);
   1546      1.1       rjs 						ippp = (u_int32_t *)(ph + 1);
   1547      1.1       rjs 						*ippp = htonl(0x1000000c);
   1548      1.1       rjs 					}
   1549      1.1       rjs 
   1550      1.1       rjs 					sctp_abort_an_association(stcb->sctp_ep,
   1551      1.1       rjs 					    stcb, SCTP_PEER_FAULTY, oper);
   1552      1.1       rjs 
   1553      1.1       rjs 					*abort_flag = 1;
   1554      1.1       rjs 					return;
   1555      1.1       rjs 
   1556      1.1       rjs 				}
   1557      1.1       rjs 			}
   1558      1.1       rjs 		}
   1559      1.1       rjs 	}
   1560      1.1       rjs 	/*
   1561      1.1       rjs 	 * now that we have all in there place we must check a number of
   1562      1.1       rjs 	 * things to see if we can send data to the ULP.
   1563      1.1       rjs 	 */
   1564      1.1       rjs 	/* we need to do some delivery, if we can */
   1565      1.1       rjs 	chk = TAILQ_FIRST(&asoc->reasmqueue);
   1566      1.1       rjs 	if (chk == NULL) {
   1567      1.1       rjs 		/* Huh? */
   1568      1.1       rjs 		asoc->size_on_reasm_queue = 0;
   1569      1.1       rjs 		asoc->cnt_on_reasm_queue = 0;
   1570      1.1       rjs 		return;
   1571      1.1       rjs 	}
   1572      1.1       rjs 	if (asoc->fragmented_delivery_inprogress == 0) {
   1573      1.1       rjs 		nxt_todel =
   1574      1.1       rjs 		    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
   1575      1.1       rjs 		if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
   1576      1.1       rjs 		    (nxt_todel == chk->rec.data.stream_seq ||
   1577      1.1       rjs 		     (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
   1578      1.1       rjs 			/*
   1579      1.1       rjs 			 * Yep the first one is here and its
   1580      1.1       rjs 			 * ok to deliver but should we?
   1581      1.1       rjs 			 */
   1582      1.1       rjs 			if (TAILQ_EMPTY(&asoc->delivery_queue) &&
   1583      1.1       rjs 			    (sctp_is_all_msg_on_reasm(asoc, &tsize) ||
   1584      1.1       rjs 			     (asoc->size_on_reasm_queue >=
   1585      1.1       rjs 			      (stcb->sctp_socket->so_rcv.sb_hiwat >> 2) &&
   1586      1.1       rjs 			      tsize))) {
   1587      1.1       rjs 				/*
   1588      1.1       rjs 				 * Yes, we setup to
   1589      1.1       rjs 				 * start reception, by backing down the TSN
   1590      1.1       rjs 				 * just in case we can't deliver. If we
   1591      1.1       rjs 				 */
   1592      1.1       rjs 				asoc->fragmented_delivery_inprogress = 1;
   1593      1.1       rjs 				asoc->tsn_last_delivered =
   1594      1.1       rjs 				    chk->rec.data.TSN_seq - 1;
   1595      1.1       rjs 				asoc->str_of_pdapi =
   1596      1.1       rjs 				    chk->rec.data.stream_number;
   1597      1.1       rjs 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
   1598      1.1       rjs 				asoc->fragment_flags = chk->rec.data.rcv_flags;
   1599      1.1       rjs 				sctp_service_reassembly(stcb, asoc, 0);
   1600      1.1       rjs 			}
   1601      1.1       rjs 		}
   1602      1.1       rjs 	} else {
   1603      1.1       rjs 		sctp_service_reassembly(stcb, asoc, 0);
   1604      1.1       rjs 	}
   1605      1.1       rjs }
   1606      1.1       rjs 
   1607      1.1       rjs /*
   1608      1.1       rjs  * This is an unfortunate routine. It checks to make sure a evil guy is not
   1609      1.1       rjs  * stuffing us full of bad packet fragments. A broken peer could also do this
   1610      1.1       rjs  * but this is doubtful. It is to bad I must worry about evil crackers sigh
   1611      1.1       rjs  * :< more cycles.
   1612      1.1       rjs  */
   1613      1.1       rjs static int
   1614      1.1       rjs sctp_does_chk_belong_to_reasm(struct sctp_association *asoc,
   1615      1.1       rjs     struct sctp_tmit_chunk *chk)
   1616      1.1       rjs {
   1617      1.1       rjs 	struct sctp_tmit_chunk *at;
   1618      1.1       rjs 	u_int32_t tsn_est;
   1619      1.1       rjs 
   1620      1.1       rjs 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
   1621      1.1       rjs 		if (compare_with_wrap(chk->rec.data.TSN_seq,
   1622      1.1       rjs 		    at->rec.data.TSN_seq, MAX_TSN)) {
   1623      1.1       rjs 			/* is it one bigger? */
   1624      1.1       rjs 			tsn_est = at->rec.data.TSN_seq + 1;
   1625      1.1       rjs 			if (tsn_est == chk->rec.data.TSN_seq) {
   1626      1.1       rjs 				/* yep. It better be a last then*/
   1627      1.1       rjs 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
   1628      1.1       rjs 				    SCTP_DATA_LAST_FRAG) {
   1629      1.1       rjs 					/*
   1630      1.1       rjs 					 * Ok this guy belongs next to a guy
   1631      1.1       rjs 					 * that is NOT last, it should be a
   1632      1.1       rjs 					 * middle/last, not a complete chunk.
   1633      1.1       rjs 					 */
   1634      1.1       rjs 					return (1);
   1635      1.1       rjs 				} else {
   1636      1.1       rjs 					/*
   1637      1.1       rjs 					 * This guy is ok since its a LAST and
   1638      1.1       rjs 					 * the new chunk is a fully self-
   1639      1.1       rjs 					 * contained one.
   1640      1.1       rjs 					 */
   1641      1.1       rjs 					return (0);
   1642      1.1       rjs 				}
   1643      1.1       rjs 			}
   1644      1.1       rjs 		} else if (chk->rec.data.TSN_seq == at->rec.data.TSN_seq) {
   1645      1.1       rjs 			/* Software error since I have a dup? */
   1646      1.1       rjs 			return (1);
   1647      1.1       rjs 		} else {
   1648      1.1       rjs 			/*
   1649      1.1       rjs 			 * Ok, 'at' is larger than new chunk but does it
   1650      1.1       rjs 			 * need to be right before it.
   1651      1.1       rjs 			 */
   1652      1.1       rjs 			tsn_est = chk->rec.data.TSN_seq + 1;
   1653      1.1       rjs 			if (tsn_est == at->rec.data.TSN_seq) {
   1654      1.1       rjs 				/* Yep, It better be a first */
   1655      1.1       rjs 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
   1656      1.1       rjs 				    SCTP_DATA_FIRST_FRAG) {
   1657      1.1       rjs 					return (1);
   1658      1.1       rjs 				} else {
   1659      1.1       rjs 					return (0);
   1660      1.1       rjs 				}
   1661      1.1       rjs 			}
   1662      1.1       rjs 		}
   1663      1.1       rjs 	}
   1664      1.1       rjs 	return (0);
   1665      1.1       rjs }
   1666      1.1       rjs 
   1667      1.1       rjs extern unsigned int sctp_max_chunks_on_queue;
   1668      1.1       rjs static int
   1669      1.1       rjs sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
   1670      1.1       rjs     struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
   1671      1.1       rjs     struct sctp_nets *net, u_int32_t *high_tsn, int *abort_flag,
   1672      1.1       rjs     int *break_flag, int last_chunk)
   1673      1.1       rjs {
   1674      1.1       rjs 	/* Process a data chunk */
   1675      1.1       rjs 	/*  struct sctp_tmit_chunk *chk;*/
   1676      1.1       rjs 	struct sctp_tmit_chunk *chk;
   1677      1.1       rjs 	u_int32_t tsn, gap;
   1678      1.1       rjs 	struct mbuf *dmbuf;
   1679      1.1       rjs 	int the_len;
   1680      1.1       rjs 	u_int16_t strmno, strmseq;
   1681      1.1       rjs 	struct mbuf *oper;
   1682      1.1       rjs 
   1683      1.1       rjs 	chk = NULL;
   1684      1.1       rjs 	tsn = ntohl(ch->dp.tsn);
   1685      1.1       rjs #ifdef SCTP_MAP_LOGGING
   1686      1.1       rjs 	sctp_log_map(0, tsn, asoc->cumulative_tsn, SCTP_MAP_PREPARE_SLIDE);
   1687      1.1       rjs #endif
   1688      1.1       rjs 	if (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN) ||
   1689      1.1       rjs 	    asoc->cumulative_tsn == tsn) {
   1690      1.1       rjs 		/* It is a duplicate */
   1691      1.1       rjs 		sctp_pegs[SCTP_DUPTSN_RECVD]++;
   1692      1.1       rjs 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
   1693      1.1       rjs 			/* Record a dup for the next outbound sack */
   1694      1.1       rjs 			asoc->dup_tsns[asoc->numduptsns] = tsn;
   1695      1.1       rjs 			asoc->numduptsns++;
   1696      1.1       rjs 		}
   1697      1.1       rjs 		return (0);
   1698      1.1       rjs 	}
   1699      1.1       rjs 	/* Calculate the number of TSN's between the base and this TSN */
   1700      1.1       rjs 	if (tsn >= asoc->mapping_array_base_tsn) {
   1701      1.1       rjs 		gap  = tsn - asoc->mapping_array_base_tsn;
   1702      1.1       rjs 	} else {
   1703      1.1       rjs 		gap = (MAX_TSN - asoc->mapping_array_base_tsn) + tsn + 1;
   1704      1.1       rjs 	}
   1705      1.1       rjs 	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
   1706      1.1       rjs 		/* Can't hold the bit in the mapping at max array, toss it */
   1707      1.1       rjs 		return (0);
   1708      1.1       rjs 	}
   1709      1.1       rjs 	if (gap >= (uint32_t)(asoc->mapping_array_size << 3)) {
   1710      1.1       rjs 		if (sctp_expand_mapping_array(asoc)) {
   1711      1.1       rjs 			/* Can't expand, drop it */
   1712      1.1       rjs 			return (0);
   1713      1.1       rjs 		}
   1714      1.1       rjs 	}
   1715      1.1       rjs 	if (compare_with_wrap(tsn, *high_tsn, MAX_TSN)) {
   1716      1.1       rjs 		*high_tsn = tsn;
   1717      1.1       rjs 	}
   1718      1.1       rjs 	/* See if we have received this one already */
   1719      1.1       rjs 	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
   1720      1.1       rjs 		sctp_pegs[SCTP_DUPTSN_RECVD]++;
   1721      1.1       rjs 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
   1722      1.1       rjs 			/* Record a dup for the next outbound sack */
   1723      1.1       rjs 			asoc->dup_tsns[asoc->numduptsns] = tsn;
   1724      1.1       rjs 			asoc->numduptsns++;
   1725      1.1       rjs 		}
   1726      1.1       rjs 		if (!callout_pending(&asoc->dack_timer.timer)) {
   1727      1.1       rjs 			/*
   1728      1.1       rjs 			 * By starting the timer we assure that we
   1729      1.1       rjs 			 * WILL sack at the end of the packet
   1730      1.1       rjs 			 * when sctp_sack_check gets called.
   1731      1.1       rjs 			 */
   1732      1.1       rjs 			sctp_timer_start(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep,
   1733      1.1       rjs 			    stcb, NULL);
   1734      1.1       rjs 		}
   1735      1.1       rjs 		return (0);
   1736      1.1       rjs 	}
   1737      1.1       rjs 	/*
   1738      1.1       rjs 	 * Check to see about the GONE flag, duplicates would cause
   1739      1.1       rjs 	 * a sack to be sent up above
   1740      1.1       rjs 	 */
   1741      1.1       rjs 	if (stcb && (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
   1742      1.1       rjs 		/*
   1743      1.1       rjs 		 * wait a minute, this guy is gone, there is no
   1744      1.1       rjs 		 * longer a receiver. Send peer an ABORT!
   1745      1.1       rjs 		 */
   1746      1.1       rjs 		struct mbuf *op_err;
   1747      1.1       rjs 		op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
   1748      1.1       rjs 		sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err);
   1749      1.1       rjs 		*abort_flag = 1;
   1750      1.1       rjs 		return (0);
   1751      1.1       rjs 	}
   1752      1.1       rjs 	/*
   1753      1.1       rjs 	 * Now before going further we see if there is room. If NOT then
   1754      1.1       rjs 	 * we MAY let one through only IF this TSN is the one we are
   1755      1.1       rjs 	 * waiting for on a partial delivery API.
   1756      1.1       rjs 	 */
   1757      1.1       rjs 
   1758      1.1       rjs 	/* now do the tests */
   1759      1.1       rjs 	if (((asoc->cnt_on_all_streams +
   1760      1.1       rjs 	 asoc->cnt_on_delivery_queue +
   1761      1.1       rjs 	 asoc->cnt_on_reasm_queue +
   1762      1.1       rjs 	  asoc->cnt_msg_on_sb) > sctp_max_chunks_on_queue) ||
   1763      1.1       rjs 	   (((int)asoc->my_rwnd) <= 0)) {
   1764      1.1       rjs 		/*
   1765      1.1       rjs 		 * When we have NO room in the rwnd we check
   1766      1.1       rjs 		 * to make sure the reader is doing its job...
   1767      1.1       rjs 		 */
   1768      1.1       rjs 		if (stcb->sctp_socket->so_rcv.sb_cc) {
   1769      1.1       rjs 			/* some to read, wake-up */
   1770      1.1       rjs 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
   1771      1.1       rjs 		}
   1772      1.1       rjs 		/* now is it in the mapping array of what we have accepted? */
   1773      1.1       rjs 		if (compare_with_wrap(tsn,
   1774      1.1       rjs 		    asoc->highest_tsn_inside_map, MAX_TSN)) {
   1775      1.1       rjs 
   1776      1.1       rjs 			/* Nope not in the valid range dump it */
   1777      1.1       rjs #ifdef SCTP_DEBUG
   1778      1.1       rjs 			if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   1779      1.1       rjs 				printf("My rwnd overrun1:tsn:%lx rwnd %lu sbspace:%ld delq:%d!\n",
   1780      1.1       rjs 				    (u_long)tsn, (u_long)asoc->my_rwnd,
   1781      1.1       rjs 				    sctp_sbspace(&stcb->sctp_socket->so_rcv),
   1782      1.1       rjs 				    stcb->asoc.cnt_on_delivery_queue);
   1783      1.1       rjs 			}
   1784      1.1       rjs #endif
   1785      1.1       rjs 			sctp_set_rwnd(stcb, asoc);
   1786      1.1       rjs 			if ((asoc->cnt_on_all_streams +
   1787      1.1       rjs 			    asoc->cnt_on_delivery_queue +
   1788      1.1       rjs 			    asoc->cnt_on_reasm_queue +
   1789      1.1       rjs 			    asoc->cnt_msg_on_sb) > sctp_max_chunks_on_queue) {
   1790      1.1       rjs 				sctp_pegs[SCTP_MSGC_DROP]++;
   1791      1.1       rjs 			} else {
   1792      1.1       rjs 				sctp_pegs[SCTP_RWND_DROPS]++;
   1793      1.1       rjs 			}
   1794      1.1       rjs 			*break_flag = 1;
   1795      1.1       rjs 			return (0);
   1796      1.1       rjs 		}
   1797      1.1       rjs 	}
   1798      1.1       rjs 	strmno = ntohs(ch->dp.stream_id);
   1799      1.1       rjs 	if (strmno >= asoc->streamincnt) {
   1800      1.1       rjs 		struct sctp_paramhdr *phdr;
   1801      1.1       rjs 		struct mbuf *mb;
   1802      1.1       rjs 
   1803      1.1       rjs 		MGETHDR(mb, M_DONTWAIT, MT_DATA);
   1804      1.1       rjs 		if (mb != NULL) {
   1805      1.1       rjs 			/* add some space up front so prepend will work well */
   1806      1.1       rjs 			mb->m_data += sizeof(struct sctp_chunkhdr);
   1807      1.1       rjs 			phdr = mtod(mb, struct sctp_paramhdr *);
   1808      1.1       rjs 			/*
   1809      1.1       rjs 			 * Error causes are just param's and this one has
   1810      1.1       rjs 			 * two back to back phdr, one with the error type
   1811      1.1       rjs 			 * and size, the other with the streamid and a rsvd
   1812      1.1       rjs 		 	 */
   1813      1.1       rjs 			mb->m_pkthdr.len = mb->m_len =
   1814      1.1       rjs 			    (sizeof(struct sctp_paramhdr) * 2);
   1815      1.1       rjs 			phdr->param_type = htons(SCTP_CAUSE_INV_STRM);
   1816      1.1       rjs 			phdr->param_length =
   1817      1.1       rjs 			    htons(sizeof(struct sctp_paramhdr) * 2);
   1818      1.1       rjs 			phdr++;
   1819      1.1       rjs 			/* We insert the stream in the type field */
   1820      1.1       rjs 			phdr->param_type = ch->dp.stream_id;
   1821      1.1       rjs 			/* And set the length to 0 for the rsvd field */
   1822      1.1       rjs 			phdr->param_length = 0;
   1823      1.1       rjs 			sctp_queue_op_err(stcb, mb);
   1824      1.1       rjs 		}
   1825      1.1       rjs 		sctp_pegs[SCTP_BAD_STRMNO]++;
   1826      1.1       rjs 		return (0);
   1827      1.1       rjs 	}
   1828      1.1       rjs 	/*
   1829      1.1       rjs 	 * Before we continue lets validate that we are not
   1830      1.1       rjs 	 * being fooled by an evil attacker. We can only
   1831      1.1       rjs 	 * have 4k chunks based on our TSN spread allowed
   1832      1.1       rjs 	 * by the mapping array 512 * 8 bits, so there is
   1833      1.1       rjs 	 * no way our stream sequence numbers could have wrapped.
   1834      1.1       rjs 	 * We of course only validate the FIRST fragment so the
   1835      1.1       rjs 	 * bit must be set.
   1836      1.1       rjs 	 */
   1837      1.1       rjs 	strmseq = ntohs(ch->dp.stream_sequence);
   1838      1.1       rjs 	if ((ch->ch.chunk_flags & SCTP_DATA_FIRST_FRAG) &&
   1839      1.1       rjs 	    (ch->ch.chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
   1840      1.1       rjs 	    (compare_with_wrap(asoc->strmin[strmno].last_sequence_delivered,
   1841      1.1       rjs 	     strmseq, MAX_SEQ) ||
   1842      1.1       rjs 	     asoc->strmin[strmno].last_sequence_delivered == strmseq)) {
   1843      1.1       rjs 		/* The incoming sseq is behind where we last delivered? */
   1844      1.1       rjs #ifdef SCTP_DEBUG
   1845      1.1       rjs 		if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   1846      1.1       rjs 			printf("EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
   1847      1.1       rjs 			    strmseq,
   1848      1.1       rjs 			    asoc->strmin[strmno].last_sequence_delivered);
   1849      1.1       rjs 		}
   1850      1.1       rjs #endif
   1851      1.1       rjs 		/*
   1852      1.1       rjs 		 * throw it in the stream so it gets cleaned up in
   1853      1.1       rjs 		 * association destruction
   1854      1.1       rjs 		 */
   1855      1.1       rjs 		MGET(oper, M_DONTWAIT, MT_DATA);
   1856      1.1       rjs 		if (oper) {
   1857      1.1       rjs 			struct sctp_paramhdr *ph;
   1858      1.1       rjs 			u_int32_t *ippp;
   1859      1.1       rjs 
   1860      1.1       rjs 			oper->m_len = sizeof(struct sctp_paramhdr) +
   1861      1.1       rjs 			    sizeof(*ippp);
   1862      1.1       rjs 			ph = mtod(oper, struct sctp_paramhdr *);
   1863      1.1       rjs 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   1864      1.1       rjs 			ph->param_length = htons(oper->m_len);
   1865      1.1       rjs 			ippp = (u_int32_t *)(ph + 1);
   1866      1.1       rjs 			*ippp = htonl(0x20000001);
   1867      1.1       rjs 		}
   1868      1.1       rjs 		sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY,
   1869      1.1       rjs 		    oper);
   1870      1.1       rjs 		sctp_pegs[SCTP_BAD_SSN_WRAP]++;
   1871      1.1       rjs 		*abort_flag = 1;
   1872      1.1       rjs 		return (0);
   1873      1.1       rjs 	}
   1874      1.1       rjs 
   1875      1.1       rjs 	the_len = (chk_length-sizeof(struct sctp_data_chunk));
   1876      1.1       rjs 	if (last_chunk == 0) {
   1877      1.1       rjs 		dmbuf = sctp_m_copym(*m,
   1878      1.1       rjs 		    (offset + sizeof(struct sctp_data_chunk)),
   1879      1.1       rjs 		    the_len, M_DONTWAIT);
   1880      1.1       rjs 	} else {
   1881      1.1       rjs 		/* We can steal the last chunk */
   1882      1.1       rjs 		dmbuf = *m;
   1883      1.1       rjs 		/* lop off the top part */
   1884      1.1       rjs 		m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
   1885      1.1       rjs 		if (dmbuf->m_pkthdr.len > the_len) {
   1886      1.1       rjs 			/* Trim the end round bytes off  too */
   1887      1.1       rjs 			m_adj(dmbuf, -(dmbuf->m_pkthdr.len-the_len));
   1888      1.1       rjs 		}
   1889      1.1       rjs 		sctp_pegs[SCTP_NO_COPY_IN]++;
   1890      1.1       rjs 	}
   1891      1.1       rjs 	if (dmbuf == NULL) {
   1892      1.1       rjs 		sctp_pegs[SCTP_DROP_NOMEMORY]++;
   1893      1.1       rjs 		return (0);
   1894      1.1       rjs 	}
   1895      1.1       rjs 	if ((ch->ch.chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
   1896      1.1       rjs 	    asoc->fragmented_delivery_inprogress == 0 &&
   1897      1.1       rjs 	    TAILQ_EMPTY(&asoc->delivery_queue) &&
   1898      1.1       rjs 	    ((ch->ch.chunk_flags & SCTP_DATA_UNORDERED) ||
   1899      1.1       rjs 	     ((asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
   1900      1.1       rjs 	      TAILQ_EMPTY(&asoc->strmin[strmno].inqueue))) &&
   1901      1.1       rjs 	    ((long)(stcb->sctp_socket->so_rcv.sb_hiwat -
   1902      1.1       rjs 	            stcb->sctp_socket->so_rcv.sb_cc) >= (long)the_len)) {
   1903      1.1       rjs 		/* Candidate for express delivery */
   1904      1.1       rjs 		/*
   1905      1.1       rjs 		 * Its not fragmented,
   1906      1.1       rjs 		 * No PD-API is up,
   1907      1.1       rjs 		 * Nothing in the delivery queue,
   1908      1.1       rjs 		 * Its un-ordered OR ordered and the next to deliver AND
   1909      1.1       rjs 		 * nothing else is stuck on the stream queue,
   1910      1.1       rjs 		 * And there is room for it in the socket buffer.
   1911      1.1       rjs 		 * Lets just stuff it up the buffer....
   1912      1.1       rjs 		 */
   1913      1.1       rjs 
   1914      1.1       rjs 		struct mbuf *control, *mmm;
   1915      1.1       rjs 		struct sockaddr_in6 sin6;
   1916      1.1       rjs 		struct sockaddr_in6 lsa6;
   1917      1.1       rjs 		const struct sockaddr *to;
   1918      1.1       rjs 
   1919      1.1       rjs 		/* It would be nice to avoid this copy if we could :< */
   1920      1.1       rjs 		control = sctp_build_ctl_nchunk(stcb, tsn,
   1921      1.1       rjs 		    ch->dp.protocol_id, 0, strmno, strmseq,
   1922      1.1       rjs 		    ch->ch.chunk_flags);
   1923      1.1       rjs 		/* XXX need to append PKTHDR to the socket buffer first */
   1924      1.1       rjs 
   1925      1.1       rjs 		if ((dmbuf->m_flags & M_PKTHDR) == 0) {
   1926      1.1       rjs 			struct mbuf *tmp;
   1927      1.1       rjs 			MGETHDR(tmp, M_DONTWAIT, MT_DATA);
   1928      1.1       rjs 			if (tmp == NULL) {
   1929      1.1       rjs 
   1930      1.1       rjs 				/* no room! */
   1931      1.1       rjs 				if (control) {
   1932      1.1       rjs 					sctp_m_freem(control);
   1933      1.1       rjs 					stcb->asoc.my_rwnd_control_len -=
   1934      1.1       rjs 					    CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
   1935      1.1       rjs 				}
   1936      1.1       rjs 
   1937      1.1       rjs 				goto failed_express_del;
   1938      1.1       rjs 			}
   1939      1.1       rjs 			tmp->m_pkthdr.len = the_len;
   1940      1.1       rjs 			tmp->m_len = 0;
   1941      1.1       rjs 			tmp->m_next = dmbuf;
   1942      1.1       rjs 			dmbuf = tmp;
   1943      1.1       rjs 		}
   1944      1.1       rjs 		to = rtcache_getdst(&net->ro);
   1945      1.1       rjs 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
   1946      1.1       rjs 		    to->sa_family == AF_INET) {
   1947      1.1       rjs 			const struct sockaddr_in *sin;
   1948      1.1       rjs 
   1949      1.1       rjs 			sin = satocsin(to);
   1950      1.3       rtr 			in6_sin_2_v4mapsin6(sin, &sin6);
   1951      1.1       rjs 			to = (struct sockaddr *)&sin6;
   1952      1.1       rjs 		}
   1953      1.1       rjs 
   1954      1.1       rjs 		/* check and strip embedded scope junk */
   1955      1.1       rjs 		to = (const struct sockaddr *)sctp_recover_scope((const struct sockaddr_in6 *)to,
   1956      1.1       rjs 		    &lsa6);
   1957      1.1       rjs 		if (((const struct sockaddr_in *)to)->sin_port == 0) {
   1958      1.1       rjs 			printf("Huh c, port is %d not net:%p %d?\n",
   1959      1.1       rjs 			       ((const struct sockaddr_in *)to)->sin_port,
   1960      1.1       rjs 			       net,
   1961      1.1       rjs 			       (int)(ntohs(stcb->rport)));
   1962      1.1       rjs 			/*((struct sockaddr_in *)to)->sin_port = stcb->rport;*/
   1963      1.1       rjs 			/* XXX */
   1964      1.1       rjs 		}
   1965      1.1       rjs 
   1966      1.1       rjs 		mmm = dmbuf;
   1967      1.1       rjs 		/* Mark the EOR */
   1968      1.1       rjs 		while (mmm->m_next != NULL) {
   1969      1.1       rjs 			mmm = mmm->m_next;
   1970      1.1       rjs 		}
   1971      1.1       rjs 		mmm->m_flags |= M_EOR;
   1972      1.1       rjs 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
   1973      1.1       rjs 			/* we have a new high score */
   1974      1.1       rjs 			asoc->highest_tsn_inside_map = tsn;
   1975      1.1       rjs #ifdef SCTP_MAP_LOGGING
   1976      1.1       rjs 			sctp_log_map(0, 1, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
   1977      1.1       rjs #endif
   1978      1.1       rjs 		}
   1979      1.1       rjs 		SCTP_TCB_UNLOCK(stcb);
   1980      1.1       rjs 		SCTP_INP_WLOCK(stcb->sctp_ep);
   1981      1.1       rjs 		SCTP_TCB_LOCK(stcb);
   1982      1.1       rjs 		if (!sbappendaddr_nocheck(&stcb->sctp_socket->so_rcv, to, dmbuf,
   1983      1.1       rjs 		    control, stcb->asoc.my_vtag, stcb->sctp_ep)) {
   1984      1.1       rjs 			if (control) {
   1985      1.1       rjs 				sctp_m_freem(control);
   1986      1.1       rjs 				stcb->asoc.my_rwnd_control_len -=
   1987      1.1       rjs 				    CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
   1988      1.1       rjs 			}
   1989      1.1       rjs 			sctp_m_freem(dmbuf);
   1990      1.1       rjs 			goto failed_express_del;
   1991      1.1       rjs 		}
   1992      1.1       rjs 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
   1993      1.1       rjs 			if (sctp_add_to_socket_q(stcb->sctp_ep, stcb)) {
   1994      1.1       rjs 				stcb->asoc.my_rwnd_control_len += sizeof(struct mbuf);
   1995      1.1       rjs 			}
   1996      1.1       rjs 		} else {
   1997      1.1       rjs 			stcb->asoc.my_rwnd_control_len += sizeof(struct mbuf);
   1998      1.1       rjs 		}
   1999      1.1       rjs 		SCTP_INP_WUNLOCK(stcb->sctp_ep);
   2000      1.1       rjs 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
   2001      1.1       rjs 		if ((ch->ch.chunk_flags & SCTP_DATA_UNORDERED) == 0) {
   2002      1.1       rjs 
   2003      1.1       rjs 			/* for ordered, bump what we delivered */
   2004      1.1       rjs 			asoc->strmin[strmno].last_sequence_delivered++;
   2005      1.1       rjs 		}
   2006      1.1       rjs 		sctp_pegs[SCTP_EXPRESS_ROUTE]++;
   2007      1.1       rjs #ifdef SCTP_STR_LOGGING
   2008      1.1       rjs 		sctp_log_strm_del_alt(tsn, strmseq,
   2009      1.1       rjs 		    SCTP_STR_LOG_FROM_EXPRS_DEL);
   2010      1.1       rjs #endif
   2011      1.1       rjs #ifdef SCTP_DEBUG
   2012      1.1       rjs 		if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   2013      1.1       rjs 			printf("Express Delivery succeeds\n");
   2014      1.1       rjs 		}
   2015      1.1       rjs #endif
   2016      1.1       rjs 		goto finish_express_del;
   2017      1.1       rjs 	}
   2018      1.1       rjs 
   2019      1.1       rjs  failed_express_del:
   2020      1.1       rjs 	/* If we reach here this is a new chunk */
   2021      1.1       rjs 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   2022      1.1       rjs 	if (chk == NULL) {
   2023      1.1       rjs 		/* No memory so we drop the chunk */
   2024      1.1       rjs 		sctp_pegs[SCTP_DROP_NOMEMORY]++;
   2025      1.1       rjs 		if (last_chunk == 0) {
   2026      1.1       rjs 			/* we copied it, free the copy */
   2027      1.1       rjs 			sctp_m_freem(dmbuf);
   2028      1.1       rjs 		}
   2029      1.1       rjs 		return (0);
   2030      1.1       rjs 	}
   2031      1.1       rjs 	sctppcbinfo.ipi_count_chunk++;
   2032      1.1       rjs 	sctppcbinfo.ipi_gencnt_chunk++;
   2033      1.1       rjs 	chk->rec.data.TSN_seq = tsn;
   2034      1.1       rjs 	chk->rec.data.stream_seq = strmseq;
   2035      1.1       rjs 	chk->rec.data.stream_number = strmno;
   2036      1.1       rjs 	chk->rec.data.payloadtype = ch->dp.protocol_id;
   2037      1.1       rjs 	chk->rec.data.context = 0;
   2038      1.1       rjs 	chk->rec.data.doing_fast_retransmit = 0;
   2039      1.1       rjs 	chk->rec.data.rcv_flags = ch->ch.chunk_flags;
   2040      1.1       rjs 	chk->asoc = asoc;
   2041      1.1       rjs 	chk->send_size = the_len;
   2042      1.1       rjs 	chk->whoTo = net;
   2043      1.1       rjs 	net->ref_count++;
   2044      1.1       rjs 	chk->data = dmbuf;
   2045      1.1       rjs 
   2046      1.1       rjs 
   2047      1.1       rjs 	/* Mark it as received */
   2048      1.1       rjs 	/* Now queue it where it belongs */
   2049      1.1       rjs 	if ((chk->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
   2050      1.1       rjs 	    SCTP_DATA_NOT_FRAG) {
   2051      1.1       rjs 		/* First a sanity check */
   2052      1.1       rjs 		if (asoc->fragmented_delivery_inprogress) {
   2053      1.1       rjs 			/*
   2054      1.1       rjs 			 * Ok, we have a fragmented delivery in progress
   2055      1.1       rjs 			 * if this chunk is next to deliver OR belongs in
   2056      1.1       rjs 			 * our view to the reassembly, the peer is evil
   2057      1.1       rjs 			 * or broken.
   2058      1.1       rjs 			 */
   2059      1.1       rjs 			u_int32_t estimate_tsn;
   2060      1.1       rjs 			estimate_tsn = asoc->tsn_last_delivered + 1;
   2061      1.1       rjs 			if (TAILQ_EMPTY(&asoc->reasmqueue) &&
   2062      1.1       rjs 			    (estimate_tsn == chk->rec.data.TSN_seq)) {
   2063      1.1       rjs 				/* Evil/Broke peer */
   2064      1.1       rjs 				MGET(oper, M_DONTWAIT, MT_DATA);
   2065      1.1       rjs 				if (oper) {
   2066      1.1       rjs 					struct sctp_paramhdr *ph;
   2067      1.1       rjs 					u_int32_t *ippp;
   2068      1.1       rjs 
   2069      1.1       rjs 					oper->m_len =
   2070      1.1       rjs 					    sizeof(struct sctp_paramhdr) +
   2071      1.1       rjs 					    sizeof(*ippp);
   2072      1.1       rjs 					ph = mtod(oper, struct sctp_paramhdr *);
   2073      1.1       rjs 					ph->param_type =
   2074      1.1       rjs 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   2075      1.1       rjs 					ph->param_length = htons(oper->m_len);
   2076      1.1       rjs 					ippp = (u_int32_t *)(ph + 1);
   2077      1.1       rjs 					*ippp = htonl(0x20000002);
   2078      1.1       rjs 				}
   2079      1.1       rjs 				sctp_abort_an_association(stcb->sctp_ep, stcb,
   2080      1.1       rjs 				    SCTP_PEER_FAULTY, oper);
   2081      1.1       rjs 
   2082      1.1       rjs 				*abort_flag = 1;
   2083      1.1       rjs 				sctp_pegs[SCTP_DROP_FRAG]++;
   2084      1.1       rjs 				return (0);
   2085      1.1       rjs 			} else {
   2086      1.1       rjs 				if (sctp_does_chk_belong_to_reasm(asoc, chk)) {
   2087      1.1       rjs 					MGET(oper, M_DONTWAIT, MT_DATA);
   2088      1.1       rjs 					if (oper) {
   2089      1.1       rjs 						struct sctp_paramhdr *ph;
   2090      1.1       rjs 						u_int32_t *ippp;
   2091      1.1       rjs 
   2092      1.1       rjs 						oper->m_len =
   2093      1.1       rjs 						    sizeof(struct sctp_paramhdr) +
   2094      1.1       rjs 						    sizeof(*ippp);
   2095      1.1       rjs 						ph = mtod(oper,
   2096      1.1       rjs 						    struct sctp_paramhdr *);
   2097      1.1       rjs 						ph->param_type =
   2098      1.1       rjs 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   2099      1.1       rjs 						ph->param_length =
   2100      1.1       rjs 						    htons(oper->m_len);
   2101      1.1       rjs 						ippp = (u_int32_t *)(ph + 1);
   2102      1.1       rjs 						*ippp = htonl(0x20000003);
   2103      1.1       rjs 					}
   2104      1.1       rjs 					sctp_abort_an_association(stcb->sctp_ep,
   2105      1.1       rjs 					    stcb, SCTP_PEER_FAULTY, oper);
   2106      1.1       rjs 
   2107      1.1       rjs 					*abort_flag = 1;
   2108      1.1       rjs 					sctp_pegs[SCTP_DROP_FRAG]++;
   2109      1.1       rjs 					return (0);
   2110      1.1       rjs 				}
   2111      1.1       rjs 			}
   2112      1.1       rjs 		} else {
   2113      1.1       rjs 			if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
   2114      1.1       rjs 				/*
   2115      1.1       rjs 				 * Reassembly queue is NOT empty
   2116      1.1       rjs 				 * validate that this chk does not need to
   2117      1.1       rjs 				 * be in reasembly queue. If it does then
   2118      1.1       rjs 				 * our peer is broken or evil.
   2119      1.1       rjs 				 */
   2120      1.1       rjs 				if (sctp_does_chk_belong_to_reasm(asoc, chk)) {
   2121      1.1       rjs 					MGET(oper, M_DONTWAIT, MT_DATA);
   2122      1.1       rjs 					if (oper) {
   2123      1.1       rjs 						struct sctp_paramhdr *ph;
   2124      1.1       rjs 						u_int32_t *ippp;
   2125      1.1       rjs 
   2126      1.1       rjs 						oper->m_len =
   2127      1.1       rjs 						    sizeof(struct sctp_paramhdr) +
   2128      1.1       rjs 						    sizeof(*ippp);
   2129      1.1       rjs 						ph = mtod(oper,
   2130      1.1       rjs 						    struct sctp_paramhdr *);
   2131      1.1       rjs 						ph->param_type =
   2132      1.1       rjs 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   2133      1.1       rjs 						ph->param_length =
   2134      1.1       rjs 						    htons(oper->m_len);
   2135      1.1       rjs 						ippp = (u_int32_t *)(ph + 1);
   2136      1.1       rjs 						*ippp = htonl(0x20000004);
   2137      1.1       rjs 					}
   2138      1.1       rjs 					sctp_abort_an_association(stcb->sctp_ep,
   2139      1.1       rjs 					    stcb, SCTP_PEER_FAULTY, oper);
   2140      1.1       rjs 
   2141      1.1       rjs 					*abort_flag = 1;
   2142      1.1       rjs 					sctp_pegs[SCTP_DROP_FRAG]++;
   2143      1.1       rjs 					return (0);
   2144      1.1       rjs 				}
   2145      1.1       rjs 			}
   2146      1.1       rjs 		}
   2147      1.1       rjs 		if (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
   2148      1.1       rjs 			/* queue directly into socket buffer */
   2149      1.1       rjs 			sctp_deliver_data(stcb, asoc, chk, 0);
   2150      1.1       rjs 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
   2151      1.1       rjs 		} else {
   2152      1.1       rjs 			/* Special check for when streams are resetting.
   2153      1.1       rjs 			 * We could be more smart about this and check the
   2154      1.1       rjs 			 * actual stream to see if it is not being reset.. that
   2155      1.1       rjs 			 * way we would not create a HOLB when amongst streams
   2156      1.1       rjs 			 * being reset and those not being reset.
   2157      1.1       rjs 			 *
   2158      1.1       rjs 			 * We take complete messages that have a stream reset
   2159      1.1       rjs 			 * intervening (aka the TSN is after where our cum-ack needs
   2160      1.1       rjs 			 * to be) off and put them on a pending_reply_queue. The
   2161      1.1       rjs 			 * reassembly ones we do not have to worry about since
   2162      1.1       rjs 			 * they are all sorted and proceessed by TSN order. It
   2163      1.1       rjs 			 * is only the singletons I must worry about.
   2164      1.1       rjs 			 */
   2165      1.1       rjs 			if ((asoc->pending_reply) &&
   2166      1.1       rjs 			   ((compare_with_wrap(tsn, ntohl(asoc->pending_reply->reset_at_tsn), MAX_TSN)) ||
   2167      1.1       rjs 			    (tsn == ntohl(asoc->pending_reply->reset_at_tsn)))
   2168      1.1       rjs 				) {
   2169      1.1       rjs 				/* yep its past where we need to reset... go ahead and
   2170      1.1       rjs 				 * queue it.
   2171      1.1       rjs 				 */
   2172      1.1       rjs 				TAILQ_INSERT_TAIL(&asoc->pending_reply_queue , chk, sctp_next);
   2173      1.1       rjs 			}  else {
   2174      1.1       rjs 				sctp_queue_data_to_stream(stcb, asoc, chk, abort_flag);
   2175      1.1       rjs 			}
   2176      1.1       rjs 		}
   2177      1.1       rjs 	} else {
   2178      1.1       rjs 		/* Into the re-assembly queue */
   2179      1.1       rjs 		sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
   2180      1.1       rjs 		if (*abort_flag) {
   2181      1.1       rjs 			sctp_pegs[SCTP_DROP_FRAG]++;
   2182      1.1       rjs 			return (0);
   2183      1.1       rjs 		}
   2184      1.1       rjs 	}
   2185      1.1       rjs 	if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
   2186      1.1       rjs 		/* we have a new high score */
   2187      1.1       rjs 		asoc->highest_tsn_inside_map = tsn;
   2188      1.1       rjs #ifdef SCTP_MAP_LOGGING
   2189      1.1       rjs 		sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
   2190      1.1       rjs #endif
   2191      1.1       rjs 	}
   2192      1.1       rjs  finish_express_del:
   2193      1.1       rjs 	if (last_chunk) {
   2194      1.1       rjs 		*m = NULL;
   2195      1.1       rjs 	}
   2196      1.1       rjs 	sctp_pegs[SCTP_PEG_TSNS_RCVD]++;
   2197      1.1       rjs 	/* Set it present please */
   2198      1.1       rjs #ifdef SCTP_STR_LOGGING
   2199      1.1       rjs 	sctp_log_strm_del_alt(tsn, strmseq, SCTP_STR_LOG_FROM_MARK_TSN);
   2200      1.1       rjs #endif
   2201      1.1       rjs #ifdef SCTP_MAP_LOGGING
   2202      1.1       rjs 	sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
   2203      1.1       rjs 		     asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
   2204      1.1       rjs #endif
   2205      1.1       rjs 	SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
   2206      1.1       rjs 	return (1);
   2207      1.1       rjs }
   2208      1.1       rjs 
   2209      1.1       rjs void
   2210      1.1       rjs sctp_sack_check(struct sctp_tcb *stcb, int ok_to_sack, int was_a_gap, int *abort_flag)
   2211      1.1       rjs {
   2212      1.1       rjs 	/*
   2213      1.1       rjs 	 * Now we also need to check the mapping array in a couple of ways.
   2214      1.1       rjs 	 * 1) Did we move the cum-ack point?
   2215      1.1       rjs 	 */
   2216      1.1       rjs 	struct sctp_association *asoc;
   2217      1.1       rjs 	int i, at;
   2218      1.1       rjs 	int m_size, all_ones;
   2219      1.1       rjs 	int slide_from, slide_end, lgap, distance;
   2220      1.1       rjs #ifdef SCTP_MAP_LOGGING
   2221      1.1       rjs 	uint32_t old_cumack, old_base, old_highest;
   2222      1.1       rjs 	unsigned char aux_array[64];
   2223      1.1       rjs #endif
   2224      1.1       rjs 
   2225      1.1       rjs 	asoc = &stcb->asoc;
   2226      1.1       rjs 	at = 0;
   2227      1.1       rjs 
   2228      1.1       rjs #ifdef SCTP_MAP_LOGGING
   2229      1.1       rjs 	old_cumack = asoc->cumulative_tsn;
   2230      1.1       rjs 	old_base = asoc->mapping_array_base_tsn;
   2231      1.1       rjs 	old_highest = asoc->highest_tsn_inside_map;
   2232      1.1       rjs 	if (asoc->mapping_array_size < 64)
   2233      1.1       rjs 		memcpy(aux_array, asoc->mapping_array,
   2234      1.1       rjs 		    asoc->mapping_array_size);
   2235      1.1       rjs 	else
   2236      1.1       rjs 		memcpy(aux_array, asoc->mapping_array, 64);
   2237      1.1       rjs #endif
   2238      1.1       rjs 
   2239      1.1       rjs 	/*
   2240      1.1       rjs 	 * We could probably improve this a small bit by calculating the
   2241      1.1       rjs 	 * offset of the current cum-ack as the starting point.
   2242      1.1       rjs 	 */
   2243      1.1       rjs 	all_ones = 1;
   2244      1.1       rjs 	m_size = stcb->asoc.mapping_array_size << 3;
   2245      1.1       rjs 	for (i = 0; i < m_size; i++) {
   2246      1.1       rjs 		if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
   2247      1.1       rjs 			/*
   2248      1.1       rjs 			 * Ok we found the first place that we are
   2249      1.1       rjs 			 * missing a TSN.
   2250      1.1       rjs 			 */
   2251      1.1       rjs 			at = i;
   2252      1.1       rjs 			all_ones = 0;
   2253      1.1       rjs 			asoc->cumulative_tsn = asoc->mapping_array_base_tsn +
   2254      1.1       rjs 			    (i - 1);
   2255      1.1       rjs 			break;
   2256      1.1       rjs 		}
   2257      1.1       rjs 	}
   2258      1.1       rjs 	if (compare_with_wrap(asoc->cumulative_tsn,
   2259      1.1       rjs 			      asoc->highest_tsn_inside_map,
   2260      1.1       rjs 			      MAX_TSN)) {
   2261      1.1       rjs 		panic("huh, cumack greater than high-tsn in map");
   2262      1.1       rjs 	}
   2263      1.1       rjs 	if (all_ones ||
   2264      1.1       rjs 	    (asoc->cumulative_tsn == asoc->highest_tsn_inside_map && at >= 8)) {
   2265      1.1       rjs 		/* The complete array was completed by a single FR */
   2266      1.1       rjs 		/* higest becomes the cum-ack */
   2267      1.1       rjs 		int clr;
   2268      1.1       rjs 		asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
   2269      1.1       rjs 		/* clear the array */
   2270      1.1       rjs 		if (all_ones)
   2271      1.1       rjs 			clr = asoc->mapping_array_size;
   2272      1.1       rjs 		else {
   2273      1.1       rjs 			clr = (at >> 3) + 1;
   2274      1.1       rjs 			/*
   2275      1.1       rjs 			 * this should be the allones case
   2276      1.1       rjs 			 * but just in case :>
   2277      1.1       rjs 			 */
   2278      1.1       rjs 			if (clr > asoc->mapping_array_size)
   2279      1.1       rjs 				clr = asoc->mapping_array_size;
   2280      1.1       rjs 		}
   2281      1.1       rjs 		memset(asoc->mapping_array, 0, clr);
   2282      1.1       rjs 		/* base becomes one ahead of the cum-ack */
   2283      1.1       rjs 		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
   2284      1.1       rjs #ifdef SCTP_MAP_LOGGING
   2285      1.1       rjs 		sctp_log_map(old_base, old_cumack, old_highest,
   2286      1.1       rjs 		    SCTP_MAP_PREPARE_SLIDE);
   2287      1.1       rjs 		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
   2288      1.1       rjs 		    asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_CLEARED);
   2289      1.1       rjs #endif
   2290      1.1       rjs 	} else if (at >= 8) {
   2291      1.1       rjs 		/* we can slide the mapping array down */
   2292      1.1       rjs 		/* Calculate the new byte postion we can move down */
   2293      1.1       rjs 		slide_from = at >> 3;
   2294      1.1       rjs 		/* now calculate the ceiling of the move using our highest TSN value */
   2295      1.1       rjs 		if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
   2296      1.1       rjs 			lgap = asoc->highest_tsn_inside_map -
   2297      1.1       rjs 			    asoc->mapping_array_base_tsn;
   2298      1.1       rjs 		} else {
   2299      1.1       rjs 			lgap = (MAX_TSN - asoc->mapping_array_base_tsn) +
   2300      1.1       rjs 			    asoc->highest_tsn_inside_map + 1;
   2301      1.1       rjs 		}
   2302      1.1       rjs 		slide_end = lgap >> 3;
   2303      1.1       rjs 		if (slide_end < slide_from) {
   2304      1.1       rjs 			panic("impossible slide");
   2305      1.1       rjs 		}
   2306      1.1       rjs 		distance = (slide_end-slide_from) + 1;
   2307      1.1       rjs #ifdef SCTP_MAP_LOGGING
   2308      1.1       rjs 		sctp_log_map(old_base, old_cumack, old_highest,
   2309      1.1       rjs 		    SCTP_MAP_PREPARE_SLIDE);
   2310      1.1       rjs 		sctp_log_map((uint32_t)slide_from, (uint32_t)slide_end,
   2311      1.1       rjs 		    (uint32_t)lgap, SCTP_MAP_SLIDE_FROM);
   2312      1.1       rjs #endif
   2313      1.1       rjs 		if (distance + slide_from > asoc->mapping_array_size ||
   2314      1.1       rjs 		    distance < 0) {
   2315      1.1       rjs #ifdef SCTP_DEBUG
   2316      1.1       rjs 			if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   2317      1.1       rjs 				printf("Ugh bad addition.. you can't hrumpp!\n");
   2318      1.1       rjs 			}
   2319      1.1       rjs #endif
   2320      1.1       rjs 			/*
   2321      1.1       rjs 			 * Here we do NOT slide forward the array so that
   2322      1.1       rjs 			 * hopefully when more data comes in to fill it up
   2323      1.1       rjs 			 * we will be able to slide it forward. Really
   2324      1.1       rjs 			 * I don't think this should happen :-0
   2325      1.1       rjs 			 */
   2326      1.1       rjs 
   2327      1.1       rjs #ifdef SCTP_MAP_LOGGING
   2328      1.1       rjs 			sctp_log_map((uint32_t)distance, (uint32_t)slide_from,
   2329      1.1       rjs 			    (uint32_t)asoc->mapping_array_size,
   2330      1.1       rjs 			    SCTP_MAP_SLIDE_NONE);
   2331      1.1       rjs #endif
   2332      1.1       rjs 		} else {
   2333      1.1       rjs 			int ii;
   2334      1.1       rjs 			for (ii = 0; ii < distance; ii++) {
   2335      1.1       rjs 				asoc->mapping_array[ii] =
   2336      1.1       rjs 				    asoc->mapping_array[slide_from + ii];
   2337      1.1       rjs 			}
   2338      1.1       rjs 			for (ii = distance;ii <= slide_end; ii++) {
   2339      1.1       rjs 				asoc->mapping_array[ii] = 0;
   2340      1.1       rjs 			}
   2341      1.1       rjs 			asoc->mapping_array_base_tsn += (slide_from << 3);
   2342      1.1       rjs #ifdef SCTP_MAP_LOGGING
   2343      1.1       rjs 			sctp_log_map(asoc->mapping_array_base_tsn,
   2344      1.1       rjs 			    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
   2345      1.1       rjs 			    SCTP_MAP_SLIDE_RESULT);
   2346      1.1       rjs #endif
   2347      1.1       rjs 		}
   2348      1.1       rjs 	}
   2349      1.1       rjs 
   2350      1.1       rjs         /* check the special flag for stream resets */
   2351      1.1       rjs 	if ((asoc->pending_reply) &&
   2352      1.1       rjs 	   ((compare_with_wrap((asoc->cumulative_tsn+1), ntohl(asoc->pending_reply->reset_at_tsn), MAX_TSN)) ||
   2353      1.1       rjs 	    ((asoc->cumulative_tsn+1) ==  ntohl(asoc->pending_reply->reset_at_tsn)))
   2354      1.1       rjs 		) {
   2355      1.1       rjs 		/* we have finished working through the backlogged TSN's now
   2356      1.1       rjs 		 * time to reset streams.
   2357      1.1       rjs 		 * 1: call reset function.
   2358      1.1       rjs 		 * 2: free pending_reply space
   2359      1.1       rjs 		 * 3: distribute any chunks in pending_reply_queue.
   2360      1.1       rjs 		 */
   2361      1.1       rjs 		struct sctp_tmit_chunk *chk;
   2362      1.1       rjs 		sctp_handle_stream_reset_response(stcb, asoc->pending_reply);
   2363      1.1       rjs 		free(asoc->pending_reply, M_PCB);
   2364      1.1       rjs 		asoc->pending_reply = NULL;
   2365      1.1       rjs 		chk = TAILQ_FIRST(&asoc->pending_reply_queue);
   2366      1.1       rjs 		while (chk) {
   2367      1.1       rjs 			TAILQ_REMOVE(&asoc->pending_reply_queue, chk, sctp_next);
   2368      1.1       rjs 			sctp_queue_data_to_stream(stcb, asoc, chk, abort_flag);
   2369      1.1       rjs 			if (*abort_flag) {
   2370      1.1       rjs 				return;
   2371      1.1       rjs 			}
   2372      1.1       rjs 			chk = TAILQ_FIRST(&asoc->pending_reply_queue);
   2373      1.1       rjs 		}
   2374      1.1       rjs 	}
   2375      1.1       rjs 	/*
   2376      1.1       rjs 	 * Now we need to see if we need to queue a sack or just start
   2377      1.1       rjs 	 * the timer (if allowed).
   2378      1.1       rjs 	 */
   2379      1.1       rjs 	if (ok_to_sack) {
   2380      1.1       rjs 		if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
   2381      1.1       rjs 			/*
   2382      1.1       rjs 			 * Ok special case, in SHUTDOWN-SENT case.
   2383      1.1       rjs 			 * here we maker sure SACK timer is off and
   2384      1.1       rjs 			 * instead send a SHUTDOWN and a SACK
   2385      1.1       rjs 			 */
   2386      1.1       rjs 			if (callout_pending(&stcb->asoc.dack_timer.timer)) {
   2387      1.1       rjs 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
   2388      1.1       rjs 				    stcb->sctp_ep, stcb, NULL);
   2389      1.1       rjs 			}
   2390      1.1       rjs #ifdef SCTP_DEBUG
   2391      1.1       rjs 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   2392      1.1       rjs 				printf("%s:%d sends a shutdown\n",
   2393      1.1       rjs 				       __FILE__,
   2394      1.1       rjs 				       __LINE__
   2395      1.1       rjs 				       );
   2396      1.1       rjs 			}
   2397      1.1       rjs #endif
   2398      1.1       rjs 			sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
   2399      1.1       rjs 			sctp_send_sack(stcb);
   2400      1.1       rjs 		} else {
   2401      1.1       rjs 			int is_a_gap;
   2402      1.1       rjs 			/* is there a gap now ? */
   2403      1.1       rjs 			is_a_gap = compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
   2404      1.1       rjs 			    stcb->asoc.cumulative_tsn, MAX_TSN);
   2405      1.1       rjs 			if ((stcb->asoc.first_ack_sent == 0) ||	/* First time we send a sack */
   2406      1.1       rjs 			    ((was_a_gap) && (is_a_gap == 0)) || /* was a gap, but no longer is one */
   2407      1.1       rjs 			    (stcb->asoc.numduptsns) ||		/* we have dup's */
   2408      1.1       rjs 			    (is_a_gap) ||			/* is still a gap */
   2409      1.1       rjs 			    (callout_pending(&stcb->asoc.dack_timer.timer)) /* timer was up . second packet */
   2410      1.1       rjs 				) {
   2411      1.1       rjs 				/*
   2412      1.1       rjs 			 	 * Ok we must build a SACK since the timer
   2413      1.1       rjs 				 * is pending, we got our first packet OR
   2414      1.1       rjs 				 * there are gaps or duplicates.
   2415      1.1       rjs 				 */
   2416      1.1       rjs 				stcb->asoc.first_ack_sent = 1;
   2417      1.1       rjs 				sctp_send_sack(stcb);
   2418      1.1       rjs 				/* The sending will stop the timer */
   2419      1.1       rjs 			} else {
   2420      1.1       rjs 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
   2421      1.1       rjs 				    stcb->sctp_ep, stcb, NULL);
   2422      1.1       rjs 			}
   2423      1.1       rjs 		}
   2424      1.1       rjs 	}
   2425      1.1       rjs }
   2426      1.1       rjs 
   2427      1.1       rjs void
   2428      1.1       rjs sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc, int hold_locks)
   2429      1.1       rjs {
   2430      1.1       rjs 	struct sctp_tmit_chunk *chk;
   2431      1.1       rjs 	int tsize, cntDel;
   2432      1.1       rjs 	u_int16_t nxt_todel;
   2433      1.1       rjs 
   2434      1.1       rjs 	cntDel = 0;
   2435      1.1       rjs 	if (asoc->fragmented_delivery_inprogress) {
   2436      1.1       rjs 		sctp_service_reassembly(stcb, asoc, hold_locks);
   2437      1.1       rjs 	}
   2438      1.1       rjs 	/* Can we proceed further, i.e. the PD-API is complete */
   2439      1.1       rjs 	if (asoc->fragmented_delivery_inprogress) {
   2440      1.1       rjs 		/* no */
   2441      1.1       rjs 		return;
   2442      1.1       rjs 	}
   2443      1.1       rjs 
   2444      1.1       rjs 	/*
   2445      1.1       rjs 	 * Yes, reassembly delivery no longer in progress see if we
   2446      1.1       rjs 	 * have some on the sb hold queue.
   2447      1.1       rjs 	 */
   2448      1.1       rjs 	do {
   2449      1.1       rjs 		if (stcb->sctp_socket->so_rcv.sb_cc >= stcb->sctp_socket->so_rcv.sb_hiwat) {
   2450      1.1       rjs 			if (cntDel == 0)
   2451      1.1       rjs 				sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
   2452      1.1       rjs 			break;
   2453      1.1       rjs 		}
   2454      1.1       rjs 		/* If deliver_data says no we must stop */
   2455      1.1       rjs 		if (sctp_deliver_data(stcb, asoc, (struct sctp_tmit_chunk *)NULL, hold_locks) == 0)
   2456      1.1       rjs 			break;
   2457      1.1       rjs 		cntDel++;
   2458      1.1       rjs 		chk = TAILQ_FIRST(&asoc->delivery_queue);
   2459      1.1       rjs 	} while (chk);
   2460      1.1       rjs 	if (cntDel) {
   2461      1.1       rjs 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
   2462      1.1       rjs 	}
   2463      1.1       rjs 	/*
   2464      1.1       rjs 	 * Now is there some other chunk I can deliver
   2465      1.1       rjs 	 * from the reassembly queue.
   2466      1.1       rjs 	 */
   2467      1.1       rjs 	chk = TAILQ_FIRST(&asoc->reasmqueue);
   2468      1.1       rjs 	if (chk == NULL) {
   2469      1.1       rjs 		asoc->size_on_reasm_queue = 0;
   2470      1.1       rjs 		asoc->cnt_on_reasm_queue = 0;
   2471      1.1       rjs 		return;
   2472      1.1       rjs 	}
   2473      1.1       rjs 	nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
   2474      1.1       rjs 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
   2475      1.1       rjs 	    ((nxt_todel == chk->rec.data.stream_seq) ||
   2476      1.1       rjs 	     (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
   2477      1.1       rjs 		/*
   2478      1.1       rjs 		 * Yep the first one is here. We setup to
   2479      1.1       rjs 		 * start reception, by backing down the TSN
   2480      1.1       rjs 		 * just in case we can't deliver.
   2481      1.1       rjs 		 */
   2482      1.1       rjs 
   2483      1.1       rjs 		/*
   2484      1.1       rjs 		 * Before we start though either all of the
   2485      1.1       rjs 		 * message should be here or 1/4 the socket buffer
   2486      1.1       rjs 		 * max or nothing on the delivery queue and something
   2487      1.1       rjs 		 * can be delivered.
   2488      1.1       rjs 		 */
   2489      1.1       rjs 		if (TAILQ_EMPTY(&asoc->delivery_queue) &&
   2490      1.1       rjs 		    (sctp_is_all_msg_on_reasm(asoc, &tsize) ||
   2491      1.1       rjs 		     (asoc->size_on_reasm_queue >=
   2492      1.1       rjs 		      (stcb->sctp_socket->so_rcv.sb_hiwat >> 2) && tsize))) {
   2493      1.1       rjs 			asoc->fragmented_delivery_inprogress = 1;
   2494      1.1       rjs 			asoc->tsn_last_delivered = chk->rec.data.TSN_seq-1;
   2495      1.1       rjs 			asoc->str_of_pdapi = chk->rec.data.stream_number;
   2496      1.1       rjs 			asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
   2497      1.1       rjs 			asoc->fragment_flags = chk->rec.data.rcv_flags;
   2498      1.1       rjs 			sctp_service_reassembly(stcb, asoc, hold_locks);
   2499      1.1       rjs 		}
   2500      1.1       rjs 	}
   2501      1.1       rjs }
   2502      1.1       rjs 
   2503      1.1       rjs int
   2504      1.1       rjs sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
   2505      1.1       rjs     struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
   2506      1.1       rjs     struct sctp_nets *net, u_int32_t *high_tsn)
   2507      1.1       rjs {
   2508      1.1       rjs 	struct sctp_data_chunk *ch, chunk_buf;
   2509      1.1       rjs 	struct sctp_association *asoc;
   2510      1.1       rjs 	int num_chunks = 0;	/* number of control chunks processed */
   2511      1.1       rjs 	int chk_length, break_flag, last_chunk;
   2512      1.1       rjs 	int abort_flag = 0, was_a_gap = 0;
   2513      1.1       rjs 	struct mbuf *m;
   2514      1.1       rjs 
   2515      1.1       rjs 	/* set the rwnd */
   2516      1.1       rjs 	sctp_set_rwnd(stcb, &stcb->asoc);
   2517      1.1       rjs 
   2518      1.1       rjs 	m = *mm;
   2519      1.1       rjs 	asoc = &stcb->asoc;
   2520      1.1       rjs 	if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
   2521      1.1       rjs 	    stcb->asoc.cumulative_tsn, MAX_TSN)) {
   2522      1.1       rjs 		/* there was a gap before this data was processed */
   2523      1.1       rjs 		was_a_gap = 1;
   2524      1.1       rjs 	}
   2525      1.1       rjs 	/*
   2526      1.1       rjs 	 * setup where we got the last DATA packet from for
   2527      1.1       rjs 	 * any SACK that may need to go out. Don't bump
   2528      1.1       rjs 	 * the net. This is done ONLY when a chunk
   2529      1.1       rjs 	 * is assigned.
   2530      1.1       rjs 	 */
   2531      1.1       rjs 	asoc->last_data_chunk_from = net;
   2532      1.1       rjs 
   2533      1.1       rjs 	/*
   2534      1.1       rjs 	 * Now before we proceed we must figure out if this
   2535      1.1       rjs 	 * is a wasted cluster... i.e. it is a small packet
   2536      1.1       rjs 	 * sent in and yet the driver underneath allocated a
   2537      1.1       rjs 	 * full cluster for it. If so we must copy it to a
   2538      1.1       rjs 	 * smaller mbuf and free up the cluster mbuf. This
   2539      1.1       rjs 	 * will help with cluster starvation.
   2540      1.1       rjs 	 */
   2541      1.1       rjs 	if (m->m_len < (long)MHLEN && m->m_next == NULL) {
   2542      1.1       rjs 		/* we only handle mbufs that are singletons.. not chains */
   2543      1.1       rjs 		MGET(m, M_DONTWAIT, MT_DATA);
   2544      1.1       rjs 		if (m) {
   2545      1.1       rjs 			/* ok lets see if we can copy the data up */
   2546      1.1       rjs 			vaddr_t *from, *to;
   2547      1.1       rjs 
   2548      1.1       rjs 			if ((*mm)->m_flags & M_PKTHDR) {
   2549      1.1       rjs 				/* got to copy the header first */
   2550      1.1       rjs #ifdef __APPLE__
   2551      1.1       rjs 				M_COPY_PKTHDR(m, (*mm));
   2552      1.1       rjs #else
   2553      1.1       rjs 				M_MOVE_PKTHDR(m, (*mm));
   2554      1.1       rjs #endif
   2555      1.1       rjs 			}
   2556      1.1       rjs 			/* get the pointers and copy */
   2557      1.1       rjs 			to = mtod(m, vaddr_t *);
   2558      1.1       rjs 			from = mtod((*mm), vaddr_t *);
   2559      1.1       rjs 			memcpy(to, from, (*mm)->m_len);
   2560      1.1       rjs 			/* copy the length and free up the old */
   2561      1.1       rjs 			m->m_len = (*mm)->m_len;
   2562      1.1       rjs 			sctp_m_freem(*mm);
   2563      1.1       rjs 			/* sucess, back copy */
   2564      1.1       rjs 			*mm = m;
   2565      1.1       rjs 		} else {
   2566      1.1       rjs 			/* We are in trouble in the mbuf world .. yikes */
   2567      1.1       rjs 			m = *mm;
   2568      1.1       rjs 		}
   2569      1.1       rjs 	}
   2570      1.1       rjs 	/* get pointer to the first chunk header */
   2571      1.1       rjs 	ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
   2572      1.1       rjs 	    sizeof(chunk_buf), (u_int8_t *)&chunk_buf);
   2573      1.1       rjs 	if (ch == NULL) {
   2574      1.1       rjs 		printf(" ... its short\n");
   2575      1.1       rjs 		return (1);
   2576      1.1       rjs 	}
   2577      1.1       rjs 	/*
   2578      1.1       rjs 	 * process all DATA chunks...
   2579      1.1       rjs 	 */
   2580      1.1       rjs 
   2581      1.1       rjs #ifdef SCTP_DEBUG
   2582      1.1       rjs 	if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
   2583      1.1       rjs 		printf("In process data off:%d length:%d iphlen:%d ch->type:%d\n",
   2584      1.1       rjs 		    *offset, length, iphlen, (int)ch->ch.chunk_type);
   2585      1.1       rjs 	}
   2586      1.1       rjs #endif
   2587      1.1       rjs 
   2588      1.1       rjs 	*high_tsn = asoc->cumulative_tsn;
   2589      1.1       rjs 	break_flag = 0;
   2590      1.1       rjs 	while (ch->ch.chunk_type == SCTP_DATA) {
   2591      1.1       rjs 		/* validate chunk length */
   2592      1.1       rjs 		chk_length = ntohs(ch->ch.chunk_length);
   2593      1.1       rjs 		if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1 ||
   2594      1.1       rjs 		    length - *offset < chk_length) {
   2595      1.1       rjs 			/*
   2596      1.1       rjs 			 * Need to send an abort since we had a invalid
   2597      1.1       rjs 			 * data chunk.
   2598      1.1       rjs 			 */
   2599      1.1       rjs 			struct mbuf *op_err;
   2600      1.1       rjs 			MGET(op_err, M_DONTWAIT, MT_DATA);
   2601      1.1       rjs 			if (op_err) {
   2602      1.1       rjs 				struct sctp_paramhdr *ph;
   2603      1.1       rjs 				u_int32_t *ippp;
   2604      1.1       rjs 
   2605      1.1       rjs 				op_err->m_len = sizeof(struct sctp_paramhdr) +
   2606      1.1       rjs 				    sizeof(*ippp);
   2607      1.1       rjs 				ph = mtod(op_err, struct sctp_paramhdr *);
   2608      1.1       rjs 				ph->param_type =
   2609      1.1       rjs 				    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   2610      1.1       rjs 				ph->param_length = htons(op_err->m_len);
   2611      1.1       rjs 				ippp = (u_int32_t *)(ph + 1);
   2612      1.1       rjs 				*ippp = htonl(0x30000001);
   2613      1.1       rjs 			}
   2614      1.1       rjs 			sctp_abort_association(inp, stcb, m, iphlen, sh,
   2615      1.1       rjs 			    op_err);
   2616      1.1       rjs 			return (2);
   2617      1.1       rjs 		}
   2618      1.1       rjs #ifdef SCTP_DEBUG
   2619      1.1       rjs 		if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
   2620      1.1       rjs 			printf("A chunk of len:%d to process (tot:%d)\n",
   2621      1.1       rjs 			    chk_length, length - *offset);
   2622      1.1       rjs 		}
   2623      1.1       rjs #endif
   2624      1.1       rjs 
   2625      1.1       rjs #ifdef SCTP_AUDITING_ENABLED
   2626      1.1       rjs 		sctp_audit_log(0xB1, 0);
   2627      1.1       rjs #endif
   2628      1.1       rjs 		if (SCTP_SIZE32(chk_length) == *offset - length) {
   2629      1.1       rjs 			last_chunk = 1;
   2630      1.1       rjs 		} else {
   2631      1.1       rjs 			last_chunk = 0;
   2632      1.1       rjs 		}
   2633      1.1       rjs 		if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
   2634      1.1       rjs 		    chk_length, net, high_tsn, &abort_flag, &break_flag,
   2635      1.1       rjs 		    last_chunk)) {
   2636      1.1       rjs 			num_chunks++;
   2637      1.1       rjs #ifdef SCTP_DEBUG
   2638      1.1       rjs 			if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
   2639      1.1       rjs 				printf("Now incr num_chunks to %d\n",
   2640      1.1       rjs 				    num_chunks);
   2641      1.1       rjs 			}
   2642      1.1       rjs #endif
   2643      1.1       rjs 		}
   2644      1.1       rjs 		if (abort_flag)
   2645      1.1       rjs 			return (2);
   2646      1.1       rjs 
   2647      1.1       rjs 		if (break_flag) {
   2648      1.1       rjs 			/*
   2649      1.1       rjs 			 * Set because of out of rwnd space and no drop rep
   2650      1.1       rjs 			 * space left.
   2651      1.1       rjs 			 */
   2652      1.1       rjs 			break;
   2653      1.1       rjs 		}
   2654      1.1       rjs 
   2655      1.1       rjs 		*offset += SCTP_SIZE32(chk_length);
   2656      1.1       rjs 		if (*offset >= length) {
   2657      1.1       rjs 			/* no more data left in the mbuf chain */
   2658      1.1       rjs 			break;
   2659      1.1       rjs 		}
   2660      1.1       rjs 		ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
   2661      1.1       rjs 		    sizeof(chunk_buf), (u_int8_t *)&chunk_buf);
   2662      1.1       rjs 		if (ch == NULL) {
   2663      1.1       rjs 			*offset = length;
   2664      1.1       rjs 			break;
   2665      1.1       rjs 		}
   2666      1.1       rjs 	} /* while */
   2667      1.1       rjs 	if (break_flag) {
   2668      1.1       rjs 		/*
   2669      1.1       rjs 		 * we need to report rwnd overrun drops.
   2670      1.1       rjs 		 */
   2671      1.1       rjs 		sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
   2672      1.1       rjs 	}
   2673      1.1       rjs 	if (num_chunks) {
   2674      1.1       rjs 		/*
   2675      1.1       rjs 		 * Did we get data, if so update the time for
   2676      1.1       rjs 		 * auto-close and give peer credit for being
   2677      1.1       rjs 		 * alive.
   2678      1.1       rjs 		 */
   2679      1.1       rjs 		sctp_pegs[SCTP_DATA_DG_RECV]++;
   2680      1.1       rjs 		stcb->asoc.overall_error_count = 0;
   2681      1.1       rjs 		SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
   2682      1.1       rjs 	}
   2683      1.1       rjs 	/* now service all of the reassm queue and delivery queue */
   2684      1.1       rjs 	sctp_service_queues(stcb, asoc, 0);
   2685      1.1       rjs 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
   2686      1.1       rjs 		/*
   2687      1.1       rjs 		 * Assure that we ack right away by making
   2688      1.1       rjs 		 * sure that a d-ack timer is running. So the
   2689      1.1       rjs 		 * sack_check will send a sack.
   2690      1.1       rjs 		 */
   2691      1.1       rjs 		sctp_timer_start(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb,
   2692      1.1       rjs 		    net);
   2693      1.1       rjs 	}
   2694      1.1       rjs 	/* Start a sack timer or QUEUE a SACK for sending */
   2695      1.1       rjs 	sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
   2696      1.1       rjs 	if (abort_flag)
   2697      1.1       rjs 		return (2);
   2698      1.1       rjs 
   2699      1.1       rjs 	return (0);
   2700      1.1       rjs }
   2701      1.1       rjs 
   2702      1.1       rjs static void
   2703      1.1       rjs sctp_handle_segments(struct sctp_tcb *stcb, struct sctp_association *asoc,
   2704      1.1       rjs     struct sctp_sack_chunk *ch, u_long last_tsn, u_long *biggest_tsn_acked,
   2705      1.1       rjs     u_long *biggest_newly_acked_tsn, int num_seg, int *ecn_seg_sums)
   2706      1.1       rjs {
   2707      1.1       rjs 	/************************************************/
   2708      1.1       rjs 	/* process fragments and update sendqueue        */
   2709      1.1       rjs 	/************************************************/
   2710      1.1       rjs 	struct sctp_sack *sack;
   2711      1.1       rjs 	struct sctp_gap_ack_block *frag;
   2712      1.1       rjs 	struct sctp_tmit_chunk *tp1;
   2713      1.1       rjs 	int i;
   2714      1.1       rjs 	unsigned int j;
   2715      1.1       rjs #ifdef SCTP_FR_LOGGING
   2716      1.1       rjs 	int num_frs=0;
   2717      1.1       rjs #endif
   2718      1.1       rjs 	uint16_t frag_strt, frag_end, primary_flag_set;
   2719      1.1       rjs 	u_long last_frag_high;
   2720      1.1       rjs 
   2721      1.1       rjs 	if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
   2722      1.1       rjs 		primary_flag_set = 1;
   2723      1.1       rjs 	} else {
   2724      1.1       rjs 		primary_flag_set = 0;
   2725      1.1       rjs 	}
   2726      1.1       rjs 
   2727      1.1       rjs 	sack = &ch->sack;
   2728      1.1       rjs 	frag = (struct sctp_gap_ack_block *)((vaddr_t)sack +
   2729      1.1       rjs 	    sizeof(struct sctp_sack));
   2730      1.1       rjs 	tp1 = NULL;
   2731      1.1       rjs 	last_frag_high = 0;
   2732      1.1       rjs 	for (i = 0; i < num_seg; i++) {
   2733      1.1       rjs 		frag_strt = ntohs(frag->start);
   2734      1.1       rjs 		frag_end = ntohs(frag->end);
   2735      1.1       rjs 		/* some sanity checks on the fargment offsets */
   2736      1.1       rjs 		if (frag_strt > frag_end) {
   2737      1.1       rjs 			/* this one is malformed, skip */
   2738      1.1       rjs 			frag++;
   2739      1.1       rjs 			continue;
   2740      1.1       rjs 		}
   2741      1.1       rjs 		if (compare_with_wrap((frag_end+last_tsn), *biggest_tsn_acked,
   2742      1.1       rjs 		    MAX_TSN))
   2743      1.1       rjs 			*biggest_tsn_acked = frag_end+last_tsn;
   2744      1.1       rjs 
   2745      1.1       rjs 		/* mark acked dgs and find out the highestTSN being acked */
   2746      1.1       rjs 		if (tp1 == NULL) {
   2747      1.1       rjs 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
   2748      1.1       rjs 
   2749      1.1       rjs 			/* save the locations of the last frags */
   2750      1.1       rjs 			last_frag_high = frag_end + last_tsn;
   2751      1.1       rjs 		} else {
   2752      1.1       rjs 			/*
   2753      1.1       rjs 			 * now lets see if we need to reset the queue
   2754      1.1       rjs 			 * due to a out-of-order SACK fragment
   2755      1.1       rjs 			 */
   2756      1.1       rjs 			if (compare_with_wrap(frag_strt+last_tsn,
   2757      1.1       rjs 			    last_frag_high, MAX_TSN)) {
   2758      1.1       rjs 				/*
   2759      1.1       rjs 				 * if the new frag starts after the last TSN
   2760      1.1       rjs 				 * frag covered, we are ok
   2761      1.1       rjs 				 * and this one is beyond the last one
   2762      1.1       rjs 				 */
   2763      1.1       rjs 				;
   2764      1.1       rjs 			} else {
   2765      1.1       rjs 				/*
   2766      1.1       rjs 				 * ok, they have reset us, so we need to reset
   2767      1.1       rjs 				 * the queue this will cause extra hunting but
   2768      1.1       rjs 				 * hey, they chose the performance
   2769      1.1       rjs 				 * hit when they failed to order there gaps..
   2770      1.1       rjs 				 */
   2771      1.1       rjs 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
   2772      1.1       rjs 			}
   2773      1.1       rjs 			last_frag_high = frag_end + last_tsn;
   2774      1.1       rjs 		}
   2775      1.1       rjs 		for (j = frag_strt + last_tsn; j <= frag_end + last_tsn; j++) {
   2776      1.1       rjs 			while (tp1) {
   2777      1.1       rjs #ifdef SCTP_FR_LOGGING
   2778      1.1       rjs 				if (tp1->rec.data.doing_fast_retransmit)
   2779      1.1       rjs 					num_frs++;
   2780      1.1       rjs #endif
   2781      1.1       rjs 
   2782      1.1       rjs 				if (tp1->rec.data.TSN_seq == j) {
   2783      1.1       rjs 					if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
   2784      1.1       rjs 						/* must be held until cum-ack passes */
   2785      1.1       rjs 						/* ECN Nonce: Add the nonce value to the sender's nonce sum */
   2786      1.1       rjs 						if (tp1->sent < SCTP_DATAGRAM_ACKED) {
   2787      1.1       rjs 							/*
   2788      1.1       rjs 							 * If it is less than
   2789      1.1       rjs 							 * ACKED, it is now
   2790      1.1       rjs 							 * no-longer in flight.
   2791      1.1       rjs 							 * Higher values may
   2792      1.1       rjs 							 * already be set via
   2793      1.1       rjs 							 * previous Gap Ack
   2794      1.1       rjs 							 * Blocks...
   2795      1.1       rjs 							 * i.e. ACKED or MARKED.
   2796      1.1       rjs 							 */
   2797      1.1       rjs 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
   2798      1.1       rjs 							    *biggest_newly_acked_tsn,
   2799      1.1       rjs 							    MAX_TSN)) {
   2800      1.1       rjs 								*biggest_newly_acked_tsn =
   2801      1.1       rjs 								    tp1->rec.data.TSN_seq;
   2802      1.1       rjs 							}
   2803      1.1       rjs 							sctp_flight_size_decrease(tp1);
   2804      1.1       rjs 
   2805      1.1       rjs 							sctp_total_flight_decrease(stcb, tp1);
   2806      1.1       rjs 
   2807      1.1       rjs 							if (tp1->snd_count < 2) {
   2808      1.1       rjs 								/* True non-retransmited chunk */
   2809      1.1       rjs 								tp1->whoTo->net_ack2 +=
   2810      1.1       rjs 								    tp1->send_size;
   2811      1.1       rjs 
   2812      1.1       rjs 								/* update RTO too? */
   2813      1.1       rjs 								if (tp1->do_rtt) {
   2814      1.1       rjs 									tp1->whoTo->RTO =
   2815      1.1       rjs 									    sctp_calculate_rto(stcb,
   2816      1.1       rjs 									    asoc,
   2817      1.1       rjs 									    tp1->whoTo,
   2818      1.1       rjs 									    &tp1->sent_rcv_time);
   2819      1.1       rjs 									tp1->whoTo->rto_pending = 0;
   2820      1.1       rjs 									tp1->do_rtt = 0;
   2821      1.1       rjs 								}
   2822      1.1       rjs 							}
   2823      1.1       rjs 						}
   2824      1.1       rjs 						if (tp1->sent <= SCTP_DATAGRAM_RESEND &&
   2825      1.1       rjs 						    tp1->sent != SCTP_DATAGRAM_UNSENT &&
   2826      1.1       rjs 						    compare_with_wrap(tp1->rec.data.TSN_seq,
   2827      1.1       rjs 						    asoc->this_sack_highest_gap,
   2828      1.1       rjs 						    MAX_TSN)) {
   2829      1.1       rjs 							asoc->this_sack_highest_gap =
   2830      1.1       rjs 							    tp1->rec.data.TSN_seq;
   2831      1.1       rjs 							if (primary_flag_set) {
   2832      1.1       rjs 								tp1->whoTo->cacc_saw_newack = 1;
   2833      1.1       rjs 							}
   2834      1.1       rjs 						}
   2835      1.1       rjs 						if (tp1->sent == SCTP_DATAGRAM_RESEND) {
   2836      1.1       rjs #ifdef SCTP_DEBUG
   2837      1.1       rjs 							if (sctp_debug_on &
   2838      1.1       rjs 							    SCTP_DEBUG_INDATA3) {
   2839      1.1       rjs 								printf("Hmm. one that is in RESEND that is now ACKED\n");
   2840      1.1       rjs 							}
   2841      1.1       rjs #endif
   2842      1.1       rjs 							sctp_ucount_decr(asoc->sent_queue_retran_cnt);
   2843      1.1       rjs #ifdef SCTP_AUDITING_ENABLED
   2844      1.1       rjs 							sctp_audit_log(0xB2,
   2845      1.1       rjs 							    (asoc->sent_queue_retran_cnt & 0x000000ff));
   2846      1.1       rjs #endif
   2847      1.1       rjs 
   2848      1.1       rjs 						}
   2849      1.1       rjs 						(*ecn_seg_sums) += tp1->rec.data.ect_nonce;
   2850      1.1       rjs 						(*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
   2851      1.1       rjs 						tp1->sent = SCTP_DATAGRAM_MARKED;
   2852      1.1       rjs 					}
   2853      1.1       rjs 					break;
   2854      1.1       rjs 				} /* if (tp1->TSN_seq == j) */
   2855      1.1       rjs 				if (compare_with_wrap(tp1->rec.data.TSN_seq, j,
   2856      1.1       rjs 				    MAX_TSN))
   2857      1.1       rjs 					break;
   2858      1.1       rjs 				tp1 = TAILQ_NEXT(tp1, sctp_next);
   2859      1.1       rjs 			}/* end while (tp1) */
   2860      1.1       rjs 		}  /* end for (j = fragStart */
   2861      1.1       rjs 		frag++; /* next one */
   2862      1.1       rjs 	}
   2863      1.1       rjs #ifdef SCTP_FR_LOGGING
   2864      1.1       rjs 	if (num_frs)
   2865      1.1       rjs 		sctp_log_fr(*biggest_tsn_acked, *biggest_newly_acked_tsn,
   2866      1.1       rjs 		    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
   2867      1.1       rjs #endif
   2868      1.1       rjs }
   2869      1.1       rjs 
   2870      1.1       rjs static void
   2871      1.1       rjs sctp_check_for_revoked(struct sctp_association *asoc, u_long cum_ack,
   2872      1.1       rjs     u_long biggest_tsn_acked)
   2873      1.1       rjs {
   2874      1.1       rjs 	struct sctp_tmit_chunk *tp1;
   2875      1.1       rjs 	int tot_revoked=0;
   2876      1.1       rjs 
   2877      1.1       rjs 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
   2878      1.1       rjs 	while (tp1) {
   2879      1.1       rjs 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
   2880      1.1       rjs 		    MAX_TSN)) {
   2881      1.1       rjs 			/*
   2882      1.1       rjs 			 * ok this guy is either ACK or MARKED. If it is ACKED
   2883      1.1       rjs 			 * it has been previously acked but not this time i.e.
   2884      1.1       rjs 			 * revoked.  If it is MARKED it was ACK'ed again.
   2885      1.1       rjs 			 */
   2886      1.1       rjs 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
   2887      1.1       rjs 				/* it has been revoked */
   2888      1.1       rjs 				/*
   2889      1.1       rjs 				 * We do NOT add back to flight size here since
   2890      1.1       rjs 				 * it is really NOT in flight. Resend (when/if
   2891      1.1       rjs 				 * it occurs will add to flight size
   2892      1.1       rjs 				 */
   2893      1.1       rjs 				tp1->sent = SCTP_DATAGRAM_SENT;
   2894      1.1       rjs 				tot_revoked++;
   2895      1.1       rjs 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
   2896      1.1       rjs 				/* it has been re-acked in this SACK */
   2897      1.1       rjs 				tp1->sent = SCTP_DATAGRAM_ACKED;
   2898      1.1       rjs 			}
   2899      1.1       rjs 		}
   2900      1.1       rjs 		if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
   2901      1.1       rjs 		    MAX_TSN)) {
   2902      1.1       rjs 			/* above the sack */
   2903      1.1       rjs 			break;
   2904      1.1       rjs 		}
   2905      1.1       rjs 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
   2906      1.1       rjs 			break;
   2907      1.1       rjs 		tp1 = TAILQ_NEXT(tp1, sctp_next);
   2908      1.1       rjs 	}
   2909      1.1       rjs 	if (tot_revoked > 0) {
   2910      1.1       rjs 		/* Setup the ecn nonce re-sync point. We
   2911      1.1       rjs 		 * do this since once data is revoked
   2912      1.1       rjs 		 * we begin to retransmit things, which
   2913      1.1       rjs 		 * do NOT have the ECN bits set. This means
   2914      1.1       rjs 		 * we are now out of sync and must wait until
   2915      1.1       rjs 		 * we get back in sync with the peer to
   2916      1.1       rjs 		 * check ECN bits.
   2917      1.1       rjs 		 */
   2918      1.1       rjs 		tp1 = TAILQ_FIRST(&asoc->send_queue);
   2919      1.1       rjs 		if (tp1 == NULL) {
   2920      1.1       rjs 			asoc->nonce_resync_tsn = asoc->sending_seq;
   2921      1.1       rjs 		} else {
   2922      1.1       rjs 			asoc->nonce_resync_tsn = tp1->rec.data.TSN_seq;
   2923      1.1       rjs 		}
   2924      1.1       rjs 		asoc->nonce_wait_for_ecne = 0;
   2925      1.1       rjs 		asoc->nonce_sum_check = 0;
   2926      1.1       rjs 	}
   2927      1.1       rjs 
   2928      1.1       rjs }
   2929      1.1       rjs 
   2930      1.1       rjs extern int sctp_peer_chunk_oh;
   2931      1.1       rjs 
   2932      1.1       rjs static void
   2933      1.1       rjs sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
   2934      1.1       rjs     u_long biggest_tsn_acked, int strike_enabled,
   2935      1.1       rjs     u_long biggest_tsn_newly_acked, int accum_moved)
   2936      1.1       rjs {
   2937      1.1       rjs 	struct sctp_tmit_chunk *tp1;
   2938      1.1       rjs 	int strike_flag=0;
   2939      1.1       rjs 	struct timeval now;
   2940      1.1       rjs 	int tot_retrans=0;
   2941      1.1       rjs 	u_int32_t sending_seq;
   2942      1.1       rjs 	int primary_switch_active = 0;
   2943      1.1       rjs 	int double_switch_active = 0;
   2944      1.1       rjs 
   2945      1.1       rjs 	/* select the sending_seq, this is
   2946      1.1       rjs 	 * either the next thing ready to
   2947      1.1       rjs 	 * be sent but not transmitted, OR,
   2948      1.1       rjs 	 * the next seq we assign.
   2949      1.1       rjs 	 */
   2950      1.1       rjs 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
   2951      1.1       rjs 	if (tp1 == NULL) {
   2952      1.1       rjs 		sending_seq = asoc->sending_seq;
   2953      1.1       rjs 	} else {
   2954      1.1       rjs 		sending_seq = tp1->rec.data.TSN_seq;
   2955      1.1       rjs 	}
   2956      1.1       rjs 
   2957      1.1       rjs 	if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
   2958      1.1       rjs 		primary_switch_active = 1;
   2959      1.1       rjs 	}
   2960      1.1       rjs 	if (asoc->primary_destination->dest_state & SCTP_ADDR_DOUBLE_SWITCH) {
   2961      1.1       rjs 		double_switch_active = 1;
   2962      1.1       rjs 	}
   2963      1.1       rjs 	if (stcb->asoc.peer_supports_prsctp ) {
   2964      1.1       rjs 		SCTP_GETTIME_TIMEVAL(&now);
   2965      1.1       rjs 	}
   2966      1.1       rjs 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
   2967      1.1       rjs 	while (tp1) {
   2968      1.1       rjs 		strike_flag=0;
   2969      1.1       rjs 		if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
   2970      1.1       rjs 		    MAX_TSN) ||
   2971      1.1       rjs 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
   2972      1.1       rjs 			/* done */
   2973      1.1       rjs 			break;
   2974      1.1       rjs 		}
   2975      1.1       rjs 		if ((tp1->flags & (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) ==
   2976      1.1       rjs 		    SCTP_PR_SCTP_ENABLED &&
   2977      1.1       rjs 		    tp1->sent < SCTP_DATAGRAM_ACKED) {
   2978      1.1       rjs 			/* Is it expired? */
   2979      1.1       rjs #ifndef __FreeBSD__
   2980      1.1       rjs 			if (timercmp(&now, &tp1->rec.data.timetodrop, >))
   2981      1.1       rjs #else
   2982      1.1       rjs 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
   2983      1.1       rjs #endif
   2984      1.1       rjs 			{
   2985      1.1       rjs 				/* Yes so drop it */
   2986      1.1       rjs 				if (tp1->data != NULL) {
   2987      1.1       rjs 					sctp_release_pr_sctp_chunk(stcb, tp1,
   2988      1.1       rjs 					    (SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_SENT),
   2989      1.1       rjs 					    &asoc->sent_queue);
   2990      1.1       rjs 				}
   2991      1.1       rjs 				tp1 = TAILQ_NEXT(tp1, sctp_next);
   2992      1.1       rjs 				continue;
   2993      1.1       rjs 			}
   2994      1.1       rjs 		}
   2995      1.1       rjs 
   2996      1.1       rjs 		if (compare_with_wrap(tp1->rec.data.TSN_seq,
   2997      1.1       rjs 		    asoc->this_sack_highest_gap, MAX_TSN)) {
   2998      1.1       rjs 			/* we are beyond the tsn in the sack  */
   2999      1.1       rjs  			break;
   3000      1.1       rjs 		}
   3001      1.1       rjs 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
   3002      1.1       rjs 			/* either a RESEND, ACKED, or MARKED */
   3003      1.1       rjs 			/* skip */
   3004      1.1       rjs 			tp1 = TAILQ_NEXT(tp1, sctp_next);
   3005      1.1       rjs 			continue;
   3006      1.1       rjs 		}
   3007      1.1       rjs 		if (primary_switch_active && (strike_enabled == 0)) {
   3008      1.1       rjs 			if (tp1->whoTo != asoc->primary_destination) {
   3009      1.1       rjs 				/*
   3010      1.1       rjs 				 * We can only strike things on the primary if
   3011      1.1       rjs 				 * the strike_enabled flag is clear
   3012      1.1       rjs 				 */
   3013      1.1       rjs 				tp1 = TAILQ_NEXT(tp1, sctp_next);
   3014      1.1       rjs 				continue;
   3015      1.1       rjs 			}
   3016      1.1       rjs 		} else if (primary_switch_active) {
   3017      1.1       rjs 			if (tp1->whoTo->cacc_saw_newack == 0) {
   3018      1.1       rjs 				/*
   3019      1.1       rjs 				 * Only one was received but it was NOT
   3020      1.1       rjs 				 * this one.
   3021      1.1       rjs 				 */
   3022      1.1       rjs 				tp1 = TAILQ_NEXT(tp1, sctp_next);
   3023      1.1       rjs 				continue;
   3024      1.1       rjs 			}
   3025      1.1       rjs 		}
   3026      1.1       rjs 		if (double_switch_active &&
   3027      1.1       rjs 		    (compare_with_wrap(asoc->primary_destination->next_tsn_at_change,
   3028      1.1       rjs 		    tp1->rec.data.TSN_seq, MAX_TSN))) {
   3029      1.1       rjs 			/*
   3030      1.1       rjs 			 * With a double switch we do NOT mark unless we
   3031      1.1       rjs 			 * are beyond the switch point.
   3032      1.1       rjs 			 */
   3033      1.1       rjs 			tp1 = TAILQ_NEXT(tp1, sctp_next);
   3034      1.1       rjs 			continue;
   3035      1.1       rjs 		}
   3036      1.1       rjs 		/*
   3037      1.1       rjs 		 * Here we check to see if we were have already done a FR
   3038      1.1       rjs 		 * and if so we see if the biggest TSN we saw in the sack is
   3039      1.1       rjs 		 * smaller than the recovery point. If so we don't strike the
   3040      1.1       rjs 		 * tsn... otherwise we CAN strike the TSN.
   3041      1.1       rjs 		 */
   3042      1.1       rjs 		if (accum_moved && asoc->fast_retran_loss_recovery) {
   3043      1.1       rjs 		 	/*
   3044      1.1       rjs 		 	 * Strike the TSN if in fast-recovery and
   3045      1.1       rjs 			 * cum-ack moved.
   3046      1.1       rjs 			 */
   3047      1.1       rjs 			tp1->sent++;
   3048      1.1       rjs 		} else if (tp1->rec.data.doing_fast_retransmit) {
   3049      1.1       rjs 			/*
   3050      1.1       rjs 			 * For those that have done a FR we must
   3051      1.1       rjs 			 * take special consideration if we strike. I.e
   3052      1.1       rjs 			 * the biggest_newly_acked must be higher
   3053      1.1       rjs 			 * than the sending_seq at the time we did
   3054      1.1       rjs 			 * the FR.
   3055      1.1       rjs 			 */
   3056      1.1       rjs #ifdef SCTP_FR_TO_ALTERNATE
   3057      1.1       rjs 			/*
   3058      1.1       rjs 			 * If FR's go to new networks, then we
   3059      1.1       rjs 			 * must only do this for singly homed asoc's. However
   3060      1.1       rjs 			 * if the FR's go to the same network (Armando's work)
   3061      1.1       rjs 			 * then its ok to FR multiple times.
   3062      1.1       rjs 			 */
   3063      1.1       rjs 			if (asoc->numnets < 2)
   3064      1.1       rjs #else
   3065      1.1       rjs 			if (1)
   3066      1.1       rjs #endif
   3067      1.1       rjs 			{
   3068      1.1       rjs 				if ((compare_with_wrap(biggest_tsn_newly_acked,
   3069      1.1       rjs 				    tp1->rec.data.fast_retran_tsn, MAX_TSN)) ||
   3070      1.1       rjs 				    (biggest_tsn_newly_acked ==
   3071      1.1       rjs 				     tp1->rec.data.fast_retran_tsn)) {
   3072      1.1       rjs 					/*
   3073      1.1       rjs 					 * Strike the TSN, since this ack is
   3074      1.1       rjs 					 * beyond where things were when we did
   3075      1.1       rjs 					 * a FR.
   3076      1.1       rjs 					 */
   3077      1.1       rjs #ifdef SCTP_FR_LOGGING
   3078      1.1       rjs 					sctp_log_fr(biggest_tsn_newly_acked,
   3079      1.1       rjs 					    tp1->rec.data.TSN_seq,
   3080      1.1       rjs 					    tp1->rec.data.fast_retran_tsn,
   3081      1.1       rjs 					    SCTP_FR_LOG_STRIKE_CHUNK);
   3082      1.1       rjs #endif
   3083      1.1       rjs 					tp1->sent++;
   3084      1.1       rjs 					strike_flag=1;
   3085      1.1       rjs 				}
   3086      1.1       rjs 			}
   3087      1.1       rjs  		} else if (compare_with_wrap(tp1->rec.data.TSN_seq,
   3088      1.1       rjs  		    biggest_tsn_newly_acked, MAX_TSN)) {
   3089      1.1       rjs 			/*
   3090      1.1       rjs 			 * We don't strike these:
   3091      1.1       rjs 			 * This is the  HTNA algorithm i.e. we don't strike
   3092      1.1       rjs 			 * If our TSN is larger than the Highest TSN Newly
   3093      1.1       rjs 			 * Acked.
   3094      1.1       rjs 			 */
   3095      1.1       rjs 			;
   3096      1.1       rjs 	 	} else {
   3097      1.1       rjs 		 	/* Strike the TSN */
   3098      1.1       rjs 			tp1->sent++;
   3099      1.1       rjs 		}
   3100      1.1       rjs 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
   3101      1.1       rjs 			/* Increment the count to resend */
   3102      1.1       rjs 			struct sctp_nets *alt;
   3103      1.1       rjs 
   3104      1.1       rjs #ifdef SCTP_FR_LOGGING
   3105      1.1       rjs 			sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
   3106      1.1       rjs 			    0, SCTP_FR_MARKED);
   3107      1.1       rjs #endif
   3108      1.1       rjs 			if (strike_flag) {
   3109      1.1       rjs 				/* This is a subsequent FR */
   3110      1.1       rjs 				sctp_pegs[SCTP_DUP_FR]++;
   3111      1.1       rjs 			}
   3112      1.1       rjs 			asoc->sent_queue_retran_cnt++;
   3113      1.1       rjs #ifdef SCTP_FR_TO_ALTERNATE
   3114      1.1       rjs 			/* Can we find an alternate? */
   3115      1.1       rjs 			alt = sctp_find_alternate_net(stcb, tp1->whoTo);
   3116      1.1       rjs #else
   3117      1.1       rjs 			/*
   3118      1.1       rjs 			 * default behavior is to NOT retransmit FR's
   3119      1.1       rjs 			 * to an alternate. Armando Caro's paper details
   3120      1.1       rjs 			 * why.
   3121      1.1       rjs 			 */
   3122      1.1       rjs 			alt = tp1->whoTo;
   3123      1.1       rjs #endif
   3124      1.1       rjs 			tp1->rec.data.doing_fast_retransmit = 1;
   3125      1.1       rjs 			tot_retrans++;
   3126      1.1       rjs 			/* mark the sending seq for possible subsequent FR's */
   3127      1.1       rjs 			if (TAILQ_EMPTY(&asoc->send_queue)) {
   3128      1.1       rjs 				/*
   3129      1.1       rjs 				 * If the queue of send is empty then its the
   3130      1.1       rjs 				 * next sequence number that will be assigned so
   3131      1.1       rjs 				 * we subtract one from this to get the one we
   3132      1.1       rjs 				 * last sent.
   3133      1.1       rjs 				 */
   3134      1.1       rjs  				tp1->rec.data.fast_retran_tsn = sending_seq - 1;
   3135      1.1       rjs 			} else {
   3136      1.1       rjs 				/*
   3137      1.1       rjs 			 	 * If there are chunks on the send queue
   3138      1.1       rjs 				 * (unsent data that has made it from the
   3139      1.1       rjs 				 * stream queues but not out the door, we take
   3140      1.1       rjs 				 * the first one (which will have the lowest
   3141      1.1       rjs 				 * TSN) and subtract one to get the one we last
   3142      1.1       rjs 				 * sent.
   3143      1.1       rjs 				 */
   3144      1.1       rjs 				struct sctp_tmit_chunk *ttt;
   3145      1.1       rjs 				ttt = TAILQ_FIRST(&asoc->send_queue);
   3146      1.1       rjs 				tp1->rec.data.fast_retran_tsn =
   3147      1.1       rjs 				    ttt->rec.data.TSN_seq - 1;
   3148      1.1       rjs 			}
   3149      1.1       rjs 			if (tp1->do_rtt) {
   3150      1.1       rjs 				/*
   3151      1.1       rjs 				 * this guy had a RTO calculation pending on it,
   3152      1.1       rjs 				 * cancel it
   3153      1.1       rjs 				 */
   3154      1.1       rjs 				tp1->whoTo->rto_pending = 0;
   3155      1.1       rjs 				tp1->do_rtt = 0;
   3156      1.1       rjs 			}
   3157      1.1       rjs 			/* fix counts and things */
   3158      1.1       rjs 
   3159      1.1       rjs 			tp1->whoTo->net_ack++;
   3160      1.1       rjs 			sctp_flight_size_decrease(tp1);
   3161      1.1       rjs #ifdef SCTP_LOG_RWND
   3162      1.1       rjs 			sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
   3163      1.1       rjs 				      asoc->peers_rwnd , tp1->send_size, sctp_peer_chunk_oh);
   3164      1.1       rjs #endif
   3165      1.1       rjs 			/* add back to the rwnd */
   3166      1.1       rjs 			asoc->peers_rwnd += (tp1->send_size + sctp_peer_chunk_oh);
   3167      1.1       rjs 
   3168      1.1       rjs 			/* remove from the total flight */
   3169      1.1       rjs 			sctp_total_flight_decrease(stcb, tp1);
   3170      1.1       rjs 			if (alt != tp1->whoTo) {
   3171      1.1       rjs 				/* yes, there is an alternate. */
   3172      1.1       rjs 				sctp_free_remote_addr(tp1->whoTo);
   3173      1.1       rjs 				tp1->whoTo = alt;
   3174      1.1       rjs 				alt->ref_count++;
   3175      1.1       rjs 			}
   3176      1.1       rjs 		}
   3177      1.1       rjs 		tp1 = TAILQ_NEXT(tp1, sctp_next);
   3178      1.1       rjs 	} /* while (tp1) */
   3179      1.1       rjs 
   3180      1.1       rjs 	if (tot_retrans > 0) {
   3181      1.1       rjs 		/* Setup the ecn nonce re-sync point. We
   3182      1.1       rjs 		 * do this since once we go to FR something
   3183      1.1       rjs 		 * we introduce a Karn's rule scenario and
   3184      1.1       rjs 		 * won't know the totals for the ECN bits.
   3185      1.1       rjs 		 */
   3186      1.1       rjs 		asoc->nonce_resync_tsn = sending_seq;
   3187      1.1       rjs 		asoc->nonce_wait_for_ecne = 0;
   3188      1.1       rjs 		asoc->nonce_sum_check = 0;
   3189      1.1       rjs 	}
   3190      1.1       rjs 
   3191      1.1       rjs }
   3192      1.1       rjs 
   3193      1.1       rjs struct sctp_tmit_chunk *
   3194      1.1       rjs sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
   3195      1.1       rjs     struct sctp_association *asoc)
   3196      1.1       rjs {
   3197      1.1       rjs 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv=NULL;
   3198      1.1       rjs 	struct timeval now;
   3199      1.1       rjs 	int now_filled=0;
   3200      1.1       rjs 
   3201      1.1       rjs 	if (asoc->peer_supports_prsctp == 0) {
   3202      1.1       rjs 		return (NULL);
   3203      1.1       rjs 	}
   3204      1.1       rjs 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
   3205      1.1       rjs 	while (tp1) {
   3206      1.1       rjs 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
   3207      1.1       rjs 		    tp1->sent != SCTP_DATAGRAM_RESEND) {
   3208      1.1       rjs 			/* no chance to advance, out of here */
   3209      1.1       rjs 			break;
   3210      1.1       rjs 		}
   3211      1.1       rjs 		if ((tp1->flags & SCTP_PR_SCTP_ENABLED) == 0) {
   3212      1.1       rjs 			/*
   3213      1.1       rjs 			 * We can't fwd-tsn past any that are reliable
   3214      1.1       rjs 			 * aka retransmitted until the asoc fails.
   3215      1.1       rjs 			 */
   3216      1.1       rjs 			break;
   3217      1.1       rjs 		}
   3218      1.1       rjs 		if (!now_filled) {
   3219      1.1       rjs 			SCTP_GETTIME_TIMEVAL(&now);
   3220      1.1       rjs 			now_filled = 1;
   3221      1.1       rjs 		}
   3222      1.1       rjs 		tp2 = TAILQ_NEXT(tp1, sctp_next);
   3223      1.1       rjs 		/*
   3224      1.1       rjs 		 * now we got a chunk which is marked for another
   3225      1.1       rjs 		 * retransmission to a PR-stream but has run
   3226      1.1       rjs 		 * out its chances already maybe OR has been
   3227      1.1       rjs 		 * marked to skip now. Can we skip it if its a
   3228      1.1       rjs 		 * resend?
   3229      1.1       rjs 		 */
   3230      1.1       rjs 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
   3231      1.1       rjs 		    (tp1->flags & SCTP_PR_SCTP_BUFFER) == 0) {
   3232      1.1       rjs 			/*
   3233      1.1       rjs 			 * Now is this one marked for resend and its time
   3234      1.1       rjs 			 * is now up?
   3235      1.1       rjs 			 */
   3236      1.1       rjs #ifndef __FreeBSD__
   3237      1.1       rjs 			if (timercmp(&now, &tp1->rec.data.timetodrop, >))
   3238      1.1       rjs #else
   3239      1.1       rjs 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
   3240      1.1       rjs #endif
   3241      1.1       rjs 			{
   3242      1.1       rjs 				/* Yes so drop it */
   3243      1.1       rjs 				if (tp1->data) {
   3244      1.1       rjs 					sctp_release_pr_sctp_chunk(stcb, tp1,
   3245      1.1       rjs 					    (SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_SENT),
   3246      1.1       rjs 					    &asoc->sent_queue);
   3247      1.1       rjs 				}
   3248      1.1       rjs 			} else {
   3249      1.1       rjs 				/*
   3250      1.1       rjs 				 * No, we are done when hit one for resend whos
   3251      1.1       rjs 				 * time as not expired.
   3252      1.1       rjs 				 */
   3253      1.1       rjs 				break;
   3254      1.1       rjs 			}
   3255      1.1       rjs 		}
   3256      1.1       rjs 		/*
   3257      1.1       rjs 		 * Ok now if this chunk is marked to drop it
   3258      1.1       rjs 		 * we can clean up the chunk, advance our peer ack point
   3259      1.1       rjs 		 * and we can check the next chunk.
   3260      1.1       rjs 		 */
   3261      1.1       rjs 		if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
   3262      1.1       rjs 			/* advance PeerAckPoint goes forward */
   3263      1.1       rjs 			asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
   3264      1.1       rjs 			a_adv = tp1;
   3265      1.1       rjs 			/*
   3266      1.1       rjs 			 * we don't want to de-queue it here. Just wait for the
   3267      1.1       rjs 			 * next peer SACK to come with a new cumTSN and then
   3268      1.1       rjs 			 * the chunk will be droped in the normal fashion.
   3269      1.1       rjs 			 */
   3270      1.1       rjs 			if (tp1->data) {
   3271      1.1       rjs 				sctp_free_bufspace(stcb, asoc, tp1);
   3272      1.1       rjs #ifdef SCTP_DEBUG
   3273      1.1       rjs 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   3274      1.1       rjs 					printf("--total out:%lu total_mbuf_out:%lu\n",
   3275      1.1       rjs 					    (u_long)asoc->total_output_queue_size,
   3276      1.1       rjs 					    (u_long)asoc->total_output_mbuf_queue_size);
   3277      1.1       rjs 				}
   3278      1.1       rjs #endif
   3279      1.1       rjs 				/*
   3280      1.1       rjs 				 * Maybe there should be another notification
   3281      1.1       rjs 				 * type
   3282      1.1       rjs 				 */
   3283      1.1       rjs 				sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
   3284      1.1       rjs 				    (SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_SENT),
   3285      1.1       rjs 				    tp1);
   3286      1.1       rjs 				sctp_m_freem(tp1->data);
   3287      1.1       rjs 				tp1->data = NULL;
   3288      1.1       rjs 				sctp_sowwakeup(stcb->sctp_ep,
   3289      1.1       rjs 				    stcb->sctp_socket);
   3290      1.1       rjs 			}
   3291      1.1       rjs 		} else {
   3292      1.1       rjs 			/* If it is still in RESEND we can advance no further */
   3293      1.1       rjs 			break;
   3294      1.1       rjs 		}
   3295      1.1       rjs 		/*
   3296      1.1       rjs 		 * If we hit here we just dumped tp1, move to next
   3297      1.1       rjs 		 * tsn on sent queue.
   3298      1.1       rjs 		 */
   3299      1.1       rjs 		tp1 = tp2;
   3300      1.1       rjs 	}
   3301      1.1       rjs 	return (a_adv);
   3302      1.1       rjs }
   3303      1.1       rjs 
   3304      1.1       rjs #ifdef SCTP_HIGH_SPEED
   3305      1.1       rjs struct sctp_hs_raise_drop {
   3306      1.1       rjs 	int32_t cwnd;
   3307      1.1       rjs 	int32_t increase;
   3308      1.1       rjs 	int32_t drop_percent;
   3309      1.1       rjs };
   3310      1.1       rjs 
   3311      1.1       rjs #define SCTP_HS_TABLE_SIZE 73
   3312      1.1       rjs 
   3313      1.1       rjs struct sctp_hs_raise_drop sctp_cwnd_adjust[SCTP_HS_TABLE_SIZE] = {
   3314      1.1       rjs 	{38,1,50},	/* 0   */
   3315      1.1       rjs 	{118,2,44},	/* 1   */
   3316      1.1       rjs 	{221,3,41},	/* 2   */
   3317      1.1       rjs 	{347,4,38},	/* 3   */
   3318      1.1       rjs 	{495,5,37},	/* 4   */
   3319      1.1       rjs 	{663,6,35},	/* 5   */
   3320      1.1       rjs 	{851,7,34},	/* 6   */
   3321      1.1       rjs 	{1058,8,33},	/* 7   */
   3322      1.1       rjs 	{1284,9,32},	/* 8   */
   3323      1.1       rjs 	{1529,10,31},	/* 9   */
   3324      1.1       rjs 	{1793,11,30},	/* 10  */
   3325      1.1       rjs 	{2076,12,29},	/* 11  */
   3326      1.1       rjs 	{2378,13,28},	/* 12  */
   3327      1.1       rjs 	{2699,14,28},	/* 13  */
   3328      1.1       rjs 	{3039,15,27},	/* 14  */
   3329      1.1       rjs 	{3399,16,27},	/* 15  */
   3330      1.1       rjs 	{3778,17,26},	/* 16  */
   3331      1.1       rjs 	{4177,18,26},	/* 17  */
   3332      1.1       rjs 	{4596,19,25},	/* 18  */
   3333      1.1       rjs 	{5036,20,25},	/* 19  */
   3334      1.1       rjs 	{5497,21,24},	/* 20  */
   3335      1.1       rjs 	{5979,22,24},	/* 21  */
   3336      1.1       rjs 	{6483,23,23},	/* 22  */
   3337      1.1       rjs 	{7009,24,23},	/* 23  */
   3338      1.1       rjs 	{7558,25,22},	/* 24  */
   3339      1.1       rjs 	{8130,26,22},	/* 25  */
   3340      1.1       rjs 	{8726,27,22},	/* 26  */
   3341      1.1       rjs 	{9346,28,21},	/* 27  */
   3342      1.1       rjs 	{9991,29,21},	/* 28  */
   3343      1.1       rjs 	{10661,30,21},  /* 29  */
   3344      1.1       rjs 	{11358,31,20},  /* 30  */
   3345      1.1       rjs 	{12082,32,20},  /* 31  */
   3346      1.1       rjs 	{12834,33,20},  /* 32  */
   3347      1.1       rjs 	{13614,34,19},  /* 33  */
   3348      1.1       rjs 	{14424,35,19},  /* 34  */
   3349      1.1       rjs 	{15265,36,19},  /* 35  */
   3350      1.1       rjs 	{16137,37,19},  /* 36  */
   3351      1.1       rjs 	{17042,38,18},  /* 37  */
   3352      1.1       rjs 	{17981,39,18},  /* 38  */
   3353      1.1       rjs 	{18955,40,18},  /* 39  */
   3354      1.1       rjs 	{19965,41,17},  /* 40  */
   3355      1.1       rjs 	{21013,42,17},  /* 41  */
   3356      1.1       rjs 	{22101,43,17},  /* 42  */
   3357      1.1       rjs 	{23230,44,17},  /* 43  */
   3358      1.1       rjs 	{24402,45,16},  /* 44  */
   3359      1.1       rjs 	{25618,46,16},  /* 45  */
   3360      1.1       rjs 	{26881,47,16},  /* 46  */
   3361      1.1       rjs 	{28193,48,16},  /* 47  */
   3362      1.1       rjs 	{29557,49,15},  /* 48  */
   3363      1.1       rjs 	{30975,50,15},  /* 49  */
   3364      1.1       rjs 	{32450,51,15},  /* 50  */
   3365      1.1       rjs 	{33986,52,15},  /* 51  */
   3366      1.1       rjs 	{35586,53,14},  /* 52  */
   3367      1.1       rjs 	{37253,54,14},  /* 53  */
   3368      1.1       rjs 	{38992,55,14},  /* 54  */
   3369      1.1       rjs 	{40808,56,14},  /* 55  */
   3370      1.1       rjs 	{42707,57,13},  /* 56  */
   3371      1.1       rjs 	{44694,58,13},  /* 57  */
   3372      1.1       rjs 	{46776,59,13},  /* 58  */
   3373      1.1       rjs 	{48961,60,13},  /* 59  */
   3374      1.1       rjs 	{51258,61,13},  /* 60  */
   3375      1.1       rjs 	{53677,62,12},  /* 61  */
   3376      1.1       rjs 	{56230,63,12},  /* 62  */
   3377      1.1       rjs 	{58932,64,12},  /* 63  */
   3378      1.1       rjs 	{61799,65,12},  /* 64  */
   3379      1.1       rjs 	{64851,66,11},  /* 65  */
   3380      1.1       rjs 	{68113,67,11},  /* 66  */
   3381      1.1       rjs 	{71617,68,11},  /* 67  */
   3382      1.1       rjs 	{75401,69,10},  /* 68  */
   3383      1.1       rjs 	{79517,70,10},  /* 69  */
   3384      1.1       rjs 	{84035,71,10},  /* 70  */
   3385      1.1       rjs 	{89053,72,10},  /* 71  */
   3386      1.1       rjs 	{94717,73,9}    /* 72  */
   3387      1.1       rjs };
   3388      1.1       rjs 
   3389      1.1       rjs static void
   3390      1.1       rjs sctp_hs_cwnd_increase(struct sctp_nets *net)
   3391      1.1       rjs {
   3392      1.1       rjs  	int cur_val, i, indx, incr;
   3393      1.1       rjs 
   3394      1.1       rjs 	cur_val = net->cwnd >> 10;
   3395      1.1       rjs 	indx = SCTP_HS_TABLE_SIZE - 1;
   3396      1.1       rjs 
   3397      1.1       rjs 	if (cur_val < sctp_cwnd_adjust[0].cwnd) {
   3398      1.1       rjs 		/* normal mode */
   3399      1.1       rjs 		if (net->net_ack > net->mtu) {
   3400      1.1       rjs 			net->cwnd += net->mtu;
   3401      1.1       rjs #ifdef SCTP_CWND_LOGGING
   3402      1.1       rjs 			sctp_log_cwnd(net, net->mtu, SCTP_CWND_LOG_FROM_SS);
   3403      1.1       rjs #endif
   3404      1.1       rjs 		} else {
   3405      1.1       rjs 			net->cwnd += net->net_ack;
   3406      1.1       rjs #ifdef SCTP_CWND_LOGGING
   3407      1.1       rjs 			sctp_log_cwnd(net, net->net_ack, SCTP_CWND_LOG_FROM_SS);
   3408      1.1       rjs #endif
   3409      1.1       rjs 		}
   3410      1.1       rjs 	} else {
   3411      1.1       rjs 		for (i=net->last_hs_used; i<SCTP_HS_TABLE_SIZE; i++) {
   3412      1.1       rjs 			if (cur_val < sctp_cwnd_adjust[i].cwnd) {
   3413      1.1       rjs 				indx = i;
   3414      1.1       rjs 				break;
   3415      1.1       rjs 			}
   3416      1.1       rjs 		}
   3417      1.1       rjs 		net->last_hs_used = indx;
   3418      1.1       rjs 		incr = ((sctp_cwnd_adjust[indx].increase) << 10);
   3419      1.1       rjs 		net->cwnd += incr;
   3420      1.1       rjs #ifdef SCTP_CWND_LOGGING
   3421      1.1       rjs 		sctp_log_cwnd(net, incr, SCTP_CWND_LOG_FROM_SS);
   3422      1.1       rjs #endif
   3423      1.1       rjs 	}
   3424      1.1       rjs }
   3425      1.1       rjs 
   3426      1.1       rjs static void
   3427      1.1       rjs sctp_hs_cwnd_decrease(struct sctp_nets *net)
   3428      1.1       rjs {
   3429      1.1       rjs  	int cur_val, i, indx;
   3430      1.1       rjs #ifdef SCTP_CWND_LOGGING
   3431      1.1       rjs 	int old_cwnd = net->cwnd;
   3432      1.1       rjs #endif
   3433      1.1       rjs 
   3434      1.1       rjs 	cur_val = net->cwnd >> 10;
   3435      1.1       rjs 	indx = net->last_hs_used;
   3436      1.1       rjs 	if (cur_val < sctp_cwnd_adjust[0].cwnd) {
   3437      1.1       rjs 		/* normal mode */
   3438      1.1       rjs 		net->ssthresh = net->cwnd / 2;
   3439      1.1       rjs 		if (net->ssthresh < (net->mtu*2)) {
   3440      1.1       rjs 			net->ssthresh = 2 * net->mtu;
   3441      1.1       rjs 		}
   3442      1.1       rjs 		net->cwnd = net->ssthresh;
   3443      1.1       rjs #ifdef SCTP_CWND_LOGGING
   3444      1.1       rjs 		sctp_log_cwnd(net, (net->cwnd-old_cwnd), SCTP_CWND_LOG_FROM_FR);
   3445      1.1       rjs #endif
   3446      1.1       rjs 	} else {
   3447      1.1       rjs 		/* drop by the proper amount */
   3448      1.1       rjs 		net->ssthresh = net->cwnd - (int)((net->cwnd / 100) *
   3449      1.1       rjs 		    sctp_cwnd_adjust[net->last_hs_used].drop_percent);
   3450      1.1       rjs 		net->cwnd = net->ssthresh;
   3451      1.1       rjs 		/* now where are we */
   3452      1.1       rjs 		indx = net->last_hs_used;
   3453      1.1       rjs 		cur_val = net->cwnd >> 10;
   3454      1.1       rjs 		/* reset where we are in the table */
   3455      1.1       rjs 		if (cur_val < sctp_cwnd_adjust[0].cwnd) {
   3456      1.1       rjs 			/* feel out of hs */
   3457      1.1       rjs 			net->last_hs_used = 0;
   3458      1.1       rjs 		} else {
   3459      1.1       rjs 			for (i = indx; i >= 1; i--) {
   3460      1.1       rjs 				if (cur_val > sctp_cwnd_adjust[i - 1].cwnd) {
   3461      1.1       rjs 					break;
   3462      1.1       rjs 				}
   3463      1.1       rjs 			}
   3464      1.1       rjs 			net->last_hs_used = indx;
   3465      1.1       rjs 		}
   3466      1.1       rjs 	}
   3467      1.1       rjs }
   3468      1.1       rjs #endif
   3469      1.1       rjs 
   3470      1.1       rjs void
   3471      1.1       rjs sctp_handle_sack(struct sctp_sack_chunk *ch, struct sctp_tcb *stcb,
   3472      1.1       rjs     struct sctp_nets *net_from, int *abort_now)
   3473      1.1       rjs {
   3474      1.1       rjs 	struct sctp_association *asoc;
   3475      1.1       rjs 	struct sctp_sack *sack;
   3476      1.1       rjs 	struct sctp_tmit_chunk *tp1, *tp2;
   3477      1.1       rjs 	u_long cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked;
   3478      1.1       rjs 	uint16_t num_seg;
   3479      1.1       rjs 	unsigned int sack_length;
   3480      1.1       rjs 	uint32_t send_s;
   3481      1.1       rjs 	int some_on_streamwheel;
   3482      1.1       rjs 	int strike_enabled = 0, cnt_of_cacc = 0;
   3483      1.1       rjs 	int accum_moved = 0;
   3484      1.1       rjs 	int marking_allowed = 1;
   3485      1.1       rjs 	int will_exit_fast_recovery=0;
   3486      1.1       rjs 	u_int32_t a_rwnd;
   3487      1.1       rjs 	struct sctp_nets *net = NULL;
   3488      1.1       rjs 	int nonce_sum_flag, ecn_seg_sums=0;
   3489      1.1       rjs 	asoc = &stcb->asoc;
   3490      1.1       rjs 
   3491      1.1       rjs 	/*
   3492      1.1       rjs 	 * Handle the incoming sack on data I have been sending.
   3493      1.1       rjs 	 */
   3494      1.1       rjs 
   3495      1.1       rjs 	/*
   3496      1.1       rjs 	 * we take any chance we can to service our queues since we
   3497      1.1       rjs 	 * cannot get awoken when the socket is read from :<
   3498      1.1       rjs 	 */
   3499      1.1       rjs 	asoc->overall_error_count = 0;
   3500      1.1       rjs 
   3501      1.1       rjs 	if (asoc->sent_queue_retran_cnt) {
   3502      1.1       rjs #ifdef SCTP_DEBUG
   3503      1.1       rjs 		if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   3504      1.1       rjs 			printf("Handling SACK for asoc:%p retran:%d\n",
   3505      1.1       rjs 			       asoc, asoc->sent_queue_retran_cnt);
   3506      1.1       rjs 		}
   3507      1.1       rjs #endif
   3508      1.1       rjs 	}
   3509      1.1       rjs 
   3510      1.1       rjs 	sctp_service_queues(stcb, asoc, 0);
   3511      1.1       rjs 
   3512      1.1       rjs 	/*
   3513      1.1       rjs 	 * Now perform the actual SACK handling:
   3514      1.1       rjs 	 * 1) Verify that it is not an old sack, if so discard.
   3515      1.1       rjs 	 * 2) If there is nothing left in the send queue (cum-ack is equal
   3516      1.1       rjs 	 *    to last acked) then you have a duplicate too, update any rwnd
   3517      1.1       rjs 	 *    change and verify no timers are running. then return.
   3518      1.1       rjs 	 * 3) Process any new consequtive data i.e. cum-ack moved
   3519      1.1       rjs 	 *    process these first and note that it moved.
   3520      1.1       rjs 	 * 4) Process any sack blocks.
   3521      1.1       rjs 	 * 5) Drop any acked from the queue.
   3522      1.1       rjs 	 * 6) Check for any revoked blocks and mark.
   3523      1.1       rjs 	 * 7) Update the cwnd.
   3524      1.1       rjs 	 * 8) Nothing left, sync up flightsizes and things, stop all timers
   3525      1.1       rjs 	 *    and also check for shutdown_pending state. If so then go ahead
   3526      1.1       rjs 	 *    and send off the shutdown. If in shutdown recv, send off the
   3527      1.1       rjs 	 *    shutdown-ack and start that timer, Ret.
   3528      1.1       rjs 	 * 9) Strike any non-acked things and do FR procedure if needed being
   3529      1.1       rjs 	 *    sure to set the FR flag.
   3530      1.1       rjs 	 * 10) Do pr-sctp procedures.
   3531      1.1       rjs 	 * 11) Apply any FR penalties.
   3532      1.1       rjs 	 * 12) Assure we will SACK if in shutdown_recv state.
   3533      1.1       rjs 	 */
   3534      1.1       rjs 
   3535      1.1       rjs 	sack_length = ntohs(ch->ch.chunk_length);
   3536      1.1       rjs 	if (sack_length < sizeof(struct sctp_sack_chunk)) {
   3537      1.1       rjs #ifdef SCTP_DEBUG
   3538      1.1       rjs 		if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   3539      1.1       rjs 			printf("Bad size on sack chunk .. to small\n");
   3540      1.1       rjs 		}
   3541      1.1       rjs #endif
   3542      1.1       rjs 		return;
   3543      1.1       rjs 	}
   3544      1.1       rjs 	/* ECN Nonce */
   3545      1.1       rjs 	nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
   3546      1.1       rjs 	sack = &ch->sack;
   3547      1.1       rjs 	cum_ack = last_tsn = ntohl(sack->cum_tsn_ack);
   3548      1.1       rjs 	num_seg = ntohs(sack->num_gap_ack_blks);
   3549      1.1       rjs 
   3550      1.1       rjs 	/* reality check */
   3551      1.1       rjs 	if (TAILQ_EMPTY(&asoc->send_queue)) {
   3552      1.1       rjs 		send_s = asoc->sending_seq;
   3553      1.1       rjs 	} else {
   3554      1.1       rjs 		tp1 = TAILQ_FIRST(&asoc->send_queue);
   3555      1.1       rjs 		send_s = tp1->rec.data.TSN_seq;
   3556      1.1       rjs 	}
   3557      1.1       rjs 
   3558      1.1       rjs 	if (sctp_strict_sacks) {
   3559      1.1       rjs 		if (cum_ack == send_s ||
   3560      1.1       rjs 		    compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
   3561      1.1       rjs 			struct mbuf *oper;
   3562      1.1       rjs 			/*
   3563      1.1       rjs 			 * no way, we have not even sent this TSN out yet.
   3564      1.1       rjs 			 * Peer is hopelessly messed up with us.
   3565      1.1       rjs 			 */
   3566      1.1       rjs 		hopeless_peer:
   3567      1.1       rjs 			*abort_now = 1;
   3568      1.1       rjs 			/* XXX */
   3569      1.1       rjs 			MGET(oper, M_DONTWAIT, MT_DATA);
   3570      1.1       rjs 			if (oper) {
   3571      1.1       rjs 				struct sctp_paramhdr *ph;
   3572      1.1       rjs 				u_int32_t *ippp;
   3573      1.1       rjs 
   3574      1.1       rjs 				oper->m_len = sizeof(struct sctp_paramhdr) +
   3575      1.1       rjs 					sizeof(*ippp);
   3576      1.1       rjs 				ph = mtod(oper, struct sctp_paramhdr *);
   3577      1.1       rjs 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
   3578      1.1       rjs 				ph->param_length = htons(oper->m_len);
   3579      1.1       rjs 				ippp = (u_int32_t *)(ph + 1);
   3580      1.1       rjs 				*ippp = htonl(0x30000002);
   3581      1.1       rjs 			}
   3582      1.1       rjs 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper);
   3583      1.1       rjs 			return;
   3584      1.1       rjs 		}
   3585      1.1       rjs 	}
   3586      1.1       rjs 	/* update the Rwnd of the peer */
   3587      1.1       rjs 	a_rwnd = (u_int32_t)ntohl(sack->a_rwnd);
   3588      1.1       rjs 	if (asoc->sent_queue_retran_cnt) {
   3589      1.1       rjs #ifdef SCTP_DEBUG
   3590      1.1       rjs 		if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   3591      1.1       rjs 			printf("cum_ack:%lx num_seg:%u last_acked_seq:%x\n",
   3592      1.1       rjs 			       cum_ack, (u_int)num_seg, asoc->last_acked_seq);
   3593      1.1       rjs 		}
   3594      1.1       rjs #endif
   3595      1.1       rjs 	}
   3596      1.1       rjs 	if (compare_with_wrap(asoc->t3timeout_highest_marked, cum_ack, MAX_TSN)) {
   3597      1.1       rjs 		/* we are not allowed to mark for FR */
   3598      1.1       rjs 		marking_allowed = 0;
   3599      1.1       rjs 	}
   3600      1.1       rjs 	/**********************/
   3601      1.1       rjs 	/* 1) check the range */
   3602      1.1       rjs 	/**********************/
   3603      1.1       rjs 	if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
   3604      1.1       rjs 		/* acking something behind */
   3605      1.1       rjs 		if (asoc->sent_queue_retran_cnt) {
   3606      1.1       rjs #ifdef SCTP_DEBUG
   3607      1.1       rjs 			if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   3608      1.1       rjs 				printf("The cum-ack is behind us\n");
   3609      1.1       rjs 			}
   3610      1.1       rjs #endif
   3611      1.1       rjs 		}
   3612      1.1       rjs 		return;
   3613      1.1       rjs 	}
   3614      1.1       rjs 
   3615      1.1       rjs 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
   3616      1.1       rjs 		/* nothing left on sendqueue.. consider done */
   3617      1.1       rjs #ifdef SCTP_LOG_RWND
   3618      1.1       rjs 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
   3619      1.1       rjs 				  asoc->peers_rwnd, 0, 0, a_rwnd);
   3620      1.1       rjs #endif
   3621      1.1       rjs 		asoc->peers_rwnd = a_rwnd;
   3622      1.1       rjs 		if (asoc->sent_queue_retran_cnt) {
   3623      1.1       rjs #ifdef SCTP_DEBUG
   3624      1.1       rjs 			if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   3625      1.1       rjs 				printf("Huh? retran set but none on queue\n");
   3626      1.1       rjs 			}
   3627      1.1       rjs #endif
   3628      1.1       rjs 			asoc->sent_queue_retran_cnt = 0;
   3629      1.1       rjs 		}
   3630      1.1       rjs 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
   3631      1.1       rjs 			/* SWS sender side engages */
   3632      1.1       rjs 			asoc->peers_rwnd = 0;
   3633      1.1       rjs 		}
   3634      1.1       rjs 		/* stop any timers */
   3635      1.1       rjs 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   3636      1.1       rjs 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
   3637      1.1       rjs 					stcb, net);
   3638      1.1       rjs 			net->partial_bytes_acked = 0;
   3639      1.1       rjs 			net->flight_size = 0;
   3640      1.1       rjs 		}
   3641      1.1       rjs 		asoc->total_flight = 0;
   3642      1.1       rjs 		asoc->total_flight_count = 0;
   3643      1.1       rjs 		return;
   3644      1.1       rjs 	}
   3645      1.1       rjs 	/*
   3646      1.1       rjs 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
   3647      1.1       rjs 	 * things. The total byte count acked is tracked in netAckSz AND
   3648      1.1       rjs 	 * netAck2 is used to track the total bytes acked that are un-
   3649      1.1       rjs 	 * amibguious and were never retransmitted. We track these on a
   3650      1.1       rjs 	 * per destination address basis.
   3651      1.1       rjs 	 */
   3652      1.1       rjs 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   3653      1.1       rjs 		net->prev_cwnd = net->cwnd;
   3654      1.1       rjs 		net->net_ack = 0;
   3655      1.1       rjs 		net->net_ack2 = 0;
   3656      1.1       rjs 	}
   3657      1.1       rjs 	/* process the new consecutive TSN first */
   3658      1.1       rjs 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
   3659      1.1       rjs 	while (tp1) {
   3660      1.1       rjs 		if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
   3661      1.1       rjs 				      MAX_TSN) ||
   3662      1.1       rjs 		    last_tsn == tp1->rec.data.TSN_seq) {
   3663      1.1       rjs 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
   3664      1.1       rjs 				/* ECN Nonce: Add the nonce to the sender's nonce sum */
   3665      1.1       rjs 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
   3666      1.1       rjs 				accum_moved = 1;
   3667      1.1       rjs 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
   3668      1.1       rjs 					/*
   3669      1.1       rjs 					 * If it is less than ACKED, it is now
   3670      1.1       rjs 					 * no-longer in flight. Higher values
   3671      1.1       rjs 					 * may occur during marking
   3672      1.1       rjs 					 */
   3673      1.1       rjs 					if ((tp1->whoTo->dest_state &
   3674      1.1       rjs 					     SCTP_ADDR_UNCONFIRMED) &&
   3675      1.1       rjs 					    (tp1->snd_count < 2) ) {
   3676      1.1       rjs 						/*
   3677      1.1       rjs 						 * If there was no retran and
   3678      1.1       rjs 						 * the address is un-confirmed
   3679      1.1       rjs 						 * and we sent there and are
   3680      1.1       rjs 						 * now sacked.. its confirmed,
   3681      1.1       rjs 						 * mark it so.
   3682      1.1       rjs 						 */
   3683      1.1       rjs 						tp1->whoTo->dest_state &=
   3684      1.1       rjs 							~SCTP_ADDR_UNCONFIRMED;
   3685      1.1       rjs 					}
   3686      1.1       rjs 					sctp_flight_size_decrease(tp1);
   3687      1.1       rjs 					sctp_total_flight_decrease(stcb, tp1);
   3688      1.1       rjs 					tp1->whoTo->net_ack += tp1->send_size;
   3689      1.1       rjs 					if (tp1->snd_count < 2) {
   3690      1.1       rjs 						/* True non-retransmited chunk */
   3691      1.1       rjs 						tp1->whoTo->net_ack2 +=
   3692      1.1       rjs 							tp1->send_size;
   3693      1.1       rjs 						/* update RTO too? */
   3694      1.1       rjs 						if (tp1->do_rtt) {
   3695      1.1       rjs 							tp1->whoTo->RTO =
   3696      1.1       rjs 								sctp_calculate_rto(stcb,
   3697      1.1       rjs 										   asoc, tp1->whoTo,
   3698      1.1       rjs 										   &tp1->sent_rcv_time);
   3699      1.1       rjs 							tp1->whoTo->rto_pending = 0;
   3700      1.1       rjs 							tp1->do_rtt = 0;
   3701      1.1       rjs 						}
   3702      1.1       rjs 					}
   3703      1.1       rjs 				}
   3704      1.1       rjs 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
   3705      1.1       rjs #ifdef SCTP_DEBUG
   3706      1.1       rjs 					if (sctp_debug_on & SCTP_DEBUG_INDATA3) {
   3707      1.1       rjs 						printf("Hmm. one that is in RESEND that is now ACKED\n");
   3708      1.1       rjs 					}
   3709      1.1       rjs #endif
   3710      1.1       rjs 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
   3711      1.1       rjs #ifdef SCTP_AUDITING_ENABLED
   3712      1.1       rjs 					sctp_audit_log(0xB3,
   3713      1.1       rjs 						       (asoc->sent_queue_retran_cnt & 0x000000ff));
   3714      1.1       rjs #endif
   3715      1.1       rjs 
   3716      1.1       rjs 				}
   3717      1.1       rjs 				tp1->sent = SCTP_DATAGRAM_ACKED;
   3718      1.1       rjs 			}
   3719      1.1       rjs 		} else {
   3720      1.1       rjs 			break;
   3721      1.1       rjs 		}
   3722      1.1       rjs 		tp1 = TAILQ_NEXT(tp1, sctp_next);
   3723      1.1       rjs 	}
   3724      1.1       rjs 	/*******************************************/
   3725      1.1       rjs 	/* cancel ALL T3-send timer if accum moved */
   3726      1.1       rjs 	/*******************************************/
   3727      1.1       rjs 	if (accum_moved) {
   3728      1.1       rjs 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   3729      1.1       rjs 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
   3730      1.1       rjs 					stcb, net);
   3731      1.1       rjs 		}
   3732      1.1       rjs 	}
   3733      1.1       rjs 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
   3734      1.1       rjs 	/* always set this up to cum-ack */
   3735      1.1       rjs 	asoc->this_sack_highest_gap = last_tsn;
   3736      1.1       rjs 
   3737      1.2  christos 	if (num_seg * sizeof(struct sctp_gap_ack_block) + sizeof(struct sctp_sack_chunk) > sack_length) {
   3738      1.1       rjs 		/* skip corrupt segments */
   3739      1.1       rjs 		strike_enabled = 0;
   3740      1.1       rjs 		goto skip_segments;
   3741      1.1       rjs 	}
   3742      1.1       rjs 
   3743      1.1       rjs 	if (num_seg > 0) {
   3744      1.1       rjs 		if (asoc->primary_destination->dest_state &
   3745      1.1       rjs 		    SCTP_ADDR_SWITCH_PRIMARY) {
   3746      1.1       rjs 			/* clear the nets CACC flags */
   3747      1.1       rjs 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   3748      1.1       rjs 				net->cacc_saw_newack = 0;
   3749      1.1       rjs 			}
   3750      1.1       rjs 		}
   3751      1.1       rjs 		/*
   3752      1.1       rjs 		 * thisSackHighestGap will increase while handling NEW segments
   3753      1.1       rjs 		 */
   3754      1.1       rjs 
   3755      1.1       rjs 		sctp_handle_segments(stcb, asoc, ch, last_tsn,
   3756      1.1       rjs 		    &biggest_tsn_acked, &biggest_tsn_newly_acked,
   3757      1.1       rjs 		    num_seg, &ecn_seg_sums);
   3758      1.1       rjs 
   3759      1.1       rjs 		if (sctp_strict_sacks) {
   3760      1.1       rjs 			/* validate the biggest_tsn_acked in the gap acks
   3761      1.1       rjs 			 * if strict adherence is wanted.
   3762      1.1       rjs 			 */
   3763      1.1       rjs 			if ((biggest_tsn_acked == send_s) ||
   3764      1.1       rjs 			    (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
   3765      1.1       rjs 				/*
   3766      1.1       rjs 				 * peer is either confused or we are under
   3767      1.1       rjs 				 * attack. We must abort.
   3768      1.1       rjs 				 */
   3769      1.1       rjs 				goto hopeless_peer;
   3770      1.1       rjs 			}
   3771      1.1       rjs 		}
   3772      1.1       rjs 
   3773      1.1       rjs 		if (asoc->primary_destination->dest_state &
   3774      1.1       rjs 		    SCTP_ADDR_SWITCH_PRIMARY) {
   3775      1.1       rjs 			/* clear the nets CACC flags */
   3776      1.1       rjs 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   3777      1.1       rjs 				if (net->cacc_saw_newack) {
   3778      1.1       rjs 					cnt_of_cacc++;
   3779      1.1       rjs 				}
   3780      1.1       rjs 			}
   3781      1.1       rjs 		}
   3782      1.1       rjs 
   3783      1.1       rjs 	}
   3784      1.1       rjs 
   3785      1.1       rjs 	if (cnt_of_cacc < 2) {
   3786      1.1       rjs 		strike_enabled = 1;
   3787      1.1       rjs 	} else {
   3788      1.1       rjs 		strike_enabled = 0;
   3789      1.1       rjs 	}
   3790      1.1       rjs  skip_segments:
   3791      1.1       rjs 	/********************************************/
   3792      1.1       rjs 	/* drop the acked chunks from the sendqueue */
   3793      1.1       rjs 	/********************************************/
   3794      1.1       rjs 	asoc->last_acked_seq = cum_ack;
   3795      1.1       rjs 	if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
   3796      1.1       rjs 		if ((cum_ack == asoc->primary_destination->next_tsn_at_change) ||
   3797      1.1       rjs 		    (compare_with_wrap(cum_ack,
   3798      1.1       rjs 				       asoc->primary_destination->next_tsn_at_change, MAX_TSN))) {
   3799      1.1       rjs 			struct sctp_nets *lnet;
   3800      1.1       rjs 			/* Turn off the switch flag for ALL addresses */
   3801      1.1       rjs 			TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
   3802      1.1       rjs 				asoc->primary_destination->dest_state &=
   3803      1.1       rjs 					~(SCTP_ADDR_SWITCH_PRIMARY|SCTP_ADDR_DOUBLE_SWITCH);
   3804      1.1       rjs 			}
   3805      1.1       rjs 		}
   3806      1.1       rjs 	}
   3807      1.1       rjs 	/* Drag along the t3 timeout point so we don't have a problem at wrap */
   3808      1.1       rjs 	if (marking_allowed) {
   3809      1.1       rjs 		asoc->t3timeout_highest_marked = cum_ack;
   3810      1.1       rjs 	}
   3811      1.1       rjs 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
   3812      1.1       rjs 	do {
   3813      1.1       rjs 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
   3814      1.1       rjs 				      MAX_TSN)) {
   3815      1.1       rjs 			break;
   3816      1.1       rjs 		}
   3817      1.1       rjs 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
   3818      1.1       rjs 			/* no more sent on list */
   3819      1.1       rjs 			break;
   3820      1.1       rjs 		}
   3821      1.1       rjs 		tp2 = TAILQ_NEXT(tp1, sctp_next);
   3822      1.1       rjs 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
   3823      1.1       rjs 		if (tp1->data) {
   3824      1.1       rjs 			sctp_free_bufspace(stcb, asoc, tp1);
   3825      1.1       rjs #ifdef SCTP_DEBUG
   3826      1.1       rjs 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   3827      1.1       rjs 				printf("--total out:%lu total_mbuf_out:%lu\n",
   3828      1.1       rjs 				       (u_long)asoc->total_output_queue_size,
   3829      1.1       rjs 				       (u_long)asoc->total_output_mbuf_queue_size);
   3830      1.1       rjs 			}
   3831      1.1       rjs #endif
   3832      1.1       rjs 
   3833      1.1       rjs 			sctp_m_freem(tp1->data);
   3834      1.1       rjs 			if (tp1->flags & SCTP_PR_SCTP_BUFFER) {
   3835      1.1       rjs 				asoc->sent_queue_cnt_removeable--;
   3836      1.1       rjs 			}
   3837      1.1       rjs 
   3838      1.1       rjs 		}
   3839      1.1       rjs 		tp1->data = NULL;
   3840      1.1       rjs 		asoc->sent_queue_cnt--;
   3841      1.1       rjs 		sctp_free_remote_addr(tp1->whoTo);
   3842      1.1       rjs 		sctppcbinfo.ipi_count_chunk--;
   3843      1.1       rjs 		asoc->chunks_on_out_queue--;
   3844      1.1       rjs 
   3845      1.1       rjs 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   3846      1.1       rjs 			panic("Chunk count is going negative");
   3847      1.1       rjs 		}
   3848      1.1       rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, tp1);
   3849      1.1       rjs 		sctppcbinfo.ipi_gencnt_chunk++;
   3850      1.1       rjs 		sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
   3851      1.1       rjs 		tp1 = tp2;
   3852      1.1       rjs 	} while (tp1 != NULL);
   3853      1.1       rjs 
   3854      1.1       rjs 
   3855      1.1       rjs 	if (asoc->fast_retran_loss_recovery && accum_moved) {
   3856      1.1       rjs 		if (compare_with_wrap(asoc->last_acked_seq,
   3857      1.1       rjs 				      asoc->fast_recovery_tsn, MAX_TSN) ||
   3858      1.1       rjs 		    asoc->last_acked_seq == asoc->fast_recovery_tsn) {
   3859      1.1       rjs 			/* Setup so we will exit RFC2582 fast recovery */
   3860      1.1       rjs 			will_exit_fast_recovery = 1;
   3861      1.1       rjs 		}
   3862      1.1       rjs 	}
   3863      1.1       rjs 
   3864      1.1       rjs 	/* Check for revoked fragments if we hand
   3865      1.1       rjs 	 * fragments in a previous segment. If we
   3866      1.1       rjs 	 * had no previous fragments we cannot have
   3867      1.1       rjs 	 * a revoke issue.
   3868      1.1       rjs 	 */
   3869      1.1       rjs 	if (asoc->saw_sack_with_frags)
   3870      1.1       rjs 		sctp_check_for_revoked(asoc, cum_ack, biggest_tsn_acked);
   3871      1.1       rjs 
   3872      1.1       rjs 	if (num_seg)
   3873      1.1       rjs 		asoc->saw_sack_with_frags = 1;
   3874      1.1       rjs 	else
   3875      1.1       rjs 		asoc->saw_sack_with_frags = 0;
   3876      1.1       rjs 
   3877      1.1       rjs 	/******************************/
   3878      1.1       rjs 	/* update cwnd                */
   3879      1.1       rjs 	/******************************/
   3880      1.1       rjs 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   3881      1.1       rjs 		/* if nothing was acked on this destination skip it */
   3882      1.1       rjs 		if (net->net_ack == 0)
   3883      1.1       rjs 			continue;
   3884      1.1       rjs 
   3885      1.1       rjs 		if (net->net_ack2 > 0) {
   3886      1.1       rjs 			/*
   3887      1.1       rjs 			 * Karn's rule applies to clearing error count,
   3888      1.1       rjs 			 * this is optional.
   3889      1.1       rjs 			 */
   3890      1.1       rjs 			net->error_count = 0;
   3891      1.1       rjs 			if ((net->dest_state&SCTP_ADDR_NOT_REACHABLE) ==
   3892      1.1       rjs 			    SCTP_ADDR_NOT_REACHABLE) {
   3893      1.1       rjs 				/* addr came good */
   3894      1.1       rjs 				net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE;
   3895      1.1       rjs 				net->dest_state |= SCTP_ADDR_REACHABLE;
   3896      1.1       rjs 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
   3897      1.1       rjs 						SCTP_RECEIVED_SACK, (void *)net);
   3898      1.1       rjs 				/* now was it the primary? if so restore */
   3899      1.1       rjs 				if (net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
   3900      1.1       rjs 					sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net);
   3901      1.1       rjs 				}
   3902      1.1       rjs 			}
   3903      1.1       rjs 		}
   3904      1.1       rjs 
   3905      1.1       rjs 		if (asoc->fast_retran_loss_recovery &&
   3906      1.1       rjs 		    will_exit_fast_recovery == 0) {
   3907      1.1       rjs 			/* If we are in loss recovery we skip any cwnd update */
   3908      1.1       rjs 			sctp_pegs[SCTP_CWND_SKIP]++;
   3909      1.1       rjs 			goto skip_cwnd_update;
   3910      1.1       rjs 		}
   3911      1.1       rjs 		if (accum_moved) {
   3912      1.1       rjs 			/* If the cumulative ack moved we can proceed */
   3913      1.1       rjs 			if (net->cwnd <= net->ssthresh) {
   3914      1.1       rjs 				/* We are in slow start */
   3915      1.1       rjs 				if (net->flight_size + net->net_ack >=
   3916      1.1       rjs 				    net->cwnd ) {
   3917      1.1       rjs #ifdef SCTP_HIGH_SPEED
   3918      1.1       rjs 					sctp_hs_cwnd_increase(net);
   3919      1.1       rjs #else
   3920      1.1       rjs 					if (net->net_ack > net->mtu) {
   3921      1.1       rjs 						net->cwnd += net->mtu;
   3922      1.1       rjs #ifdef SCTP_CWND_LOGGING
   3923      1.1       rjs 						sctp_log_cwnd(net, net->mtu,
   3924      1.1       rjs 							      SCTP_CWND_LOG_FROM_SS);
   3925      1.1       rjs #endif
   3926      1.1       rjs 
   3927      1.1       rjs 					} else {
   3928      1.1       rjs 						net->cwnd += net->net_ack;
   3929      1.1       rjs #ifdef SCTP_CWND_LOGGING
   3930      1.1       rjs 						sctp_log_cwnd(net, net->net_ack,
   3931      1.1       rjs 							      SCTP_CWND_LOG_FROM_SS);
   3932      1.1       rjs #endif
   3933      1.1       rjs 
   3934      1.1       rjs 					}
   3935      1.1       rjs #endif
   3936      1.1       rjs 					sctp_pegs[SCTP_CWND_SS]++;
   3937      1.1       rjs 				} else {
   3938      1.1       rjs 					unsigned int dif;
   3939      1.1       rjs 					sctp_pegs[SCTP_CWND_NOUSE_SS]++;
   3940      1.1       rjs 					dif = net->cwnd - (net->flight_size +
   3941      1.1       rjs 							   net->net_ack);
   3942      1.1       rjs #ifdef SCTP_CWND_LOGGING
   3943      1.1       rjs /*					sctp_log_cwnd(net, net->net_ack,
   3944      1.1       rjs 					SCTP_CWND_LOG_NOADV_SS);*/
   3945      1.1       rjs #endif
   3946      1.1       rjs 					if (dif > sctp_pegs[SCTP_CWND_DIFF_SA]) {
   3947      1.1       rjs 						sctp_pegs[SCTP_CWND_DIFF_SA] =
   3948      1.1       rjs 							dif;
   3949      1.1       rjs 						sctp_pegs[SCTP_OQS_AT_SS] =
   3950      1.1       rjs 							asoc->total_output_queue_size;
   3951      1.1       rjs 						sctp_pegs[SCTP_SQQ_AT_SS] =
   3952      1.1       rjs 							asoc->sent_queue_cnt;
   3953      1.1       rjs 						sctp_pegs[SCTP_SQC_AT_SS] =
   3954      1.1       rjs 							asoc->send_queue_cnt;
   3955      1.1       rjs 					}
   3956      1.1       rjs 				}
   3957      1.1       rjs 			} else {
   3958      1.1       rjs 				/* We are in congestion avoidance */
   3959      1.1       rjs 				if (net->flight_size + net->net_ack >=
   3960      1.1       rjs 				    net->cwnd) {
   3961      1.1       rjs 					/*
   3962      1.1       rjs 					 * add to pba only if we had a cwnd's
   3963      1.1       rjs 					 * worth (or so) in flight OR the
   3964      1.1       rjs 					 * burst limit was applied.
   3965      1.1       rjs 					 */
   3966      1.1       rjs 					net->partial_bytes_acked +=
   3967      1.1       rjs 						net->net_ack;
   3968      1.1       rjs 
   3969      1.1       rjs 					/*
   3970      1.1       rjs 					 * Do we need to increase
   3971      1.1       rjs 					 * (if pba is > cwnd)?
   3972      1.1       rjs 					 */
   3973      1.1       rjs 					if (net->partial_bytes_acked >=
   3974      1.1       rjs 					    net->cwnd) {
   3975      1.1       rjs 						if (net->cwnd <
   3976      1.1       rjs 						    net->partial_bytes_acked) {
   3977      1.1       rjs 							net->partial_bytes_acked -=
   3978      1.1       rjs 								net->cwnd;
   3979      1.1       rjs 						} else {
   3980      1.1       rjs 							net->partial_bytes_acked =
   3981      1.1       rjs 								0;
   3982      1.1       rjs 						}
   3983      1.1       rjs 						net->cwnd += net->mtu;
   3984      1.1       rjs #ifdef SCTP_CWND_LOGGING
   3985      1.1       rjs 						sctp_log_cwnd(net, net->mtu,
   3986      1.1       rjs 							      SCTP_CWND_LOG_FROM_CA);
   3987      1.1       rjs #endif
   3988      1.1       rjs 						sctp_pegs[SCTP_CWND_CA]++;
   3989      1.1       rjs 					}
   3990      1.1       rjs 				} else {
   3991      1.1       rjs 					unsigned int dif;
   3992      1.1       rjs 					sctp_pegs[SCTP_CWND_NOUSE_CA]++;
   3993      1.1       rjs #ifdef SCTP_CWND_LOGGING
   3994      1.1       rjs /*					sctp_log_cwnd(net, net->net_ack,
   3995      1.1       rjs 					SCTP_CWND_LOG_NOADV_CA);
   3996      1.1       rjs */
   3997      1.1       rjs #endif
   3998      1.1       rjs 					dif = net->cwnd - (net->flight_size +
   3999      1.1       rjs 							   net->net_ack);
   4000      1.1       rjs 					if (dif > sctp_pegs[SCTP_CWND_DIFF_CA]) {
   4001      1.1       rjs 						sctp_pegs[SCTP_CWND_DIFF_CA] =
   4002      1.1       rjs 							dif;
   4003      1.1       rjs 						sctp_pegs[SCTP_OQS_AT_CA] =
   4004      1.1       rjs 							asoc->total_output_queue_size;
   4005      1.1       rjs 						sctp_pegs[SCTP_SQQ_AT_CA] =
   4006      1.1       rjs 							asoc->sent_queue_cnt;
   4007      1.1       rjs 						sctp_pegs[SCTP_SQC_AT_CA] =
   4008      1.1       rjs 							asoc->send_queue_cnt;
   4009      1.1       rjs 
   4010      1.1       rjs 					}
   4011      1.1       rjs 
   4012      1.1       rjs 				}
   4013      1.1       rjs 			}
   4014      1.1       rjs 		} else {
   4015      1.1       rjs 			sctp_pegs[SCTP_CWND_NOCUM]++;
   4016      1.1       rjs 		}
   4017      1.1       rjs 	skip_cwnd_update:
   4018      1.1       rjs 		/*
   4019      1.1       rjs 		 * NOW, according to Karn's rule do we need to restore the
   4020      1.1       rjs 		 * RTO timer back? Check our net_ack2. If not set then we
   4021      1.1       rjs 		 * have a ambiguity.. i.e. all data ack'd was sent to more
   4022      1.1       rjs 		 * than one place.
   4023      1.1       rjs 		 */
   4024      1.1       rjs 
   4025      1.1       rjs 		if (net->net_ack2) {
   4026      1.1       rjs 			/* restore any doubled timers */
   4027      1.1       rjs 			net->RTO = ((net->lastsa >> 2) + net->lastsv) >> 1;
   4028      1.1       rjs 			if (net->RTO < stcb->asoc.minrto) {
   4029      1.1       rjs 				net->RTO = stcb->asoc.minrto;
   4030      1.1       rjs 			}
   4031      1.1       rjs 			if (net->RTO > stcb->asoc.maxrto) {
   4032      1.1       rjs 				net->RTO = stcb->asoc.maxrto;
   4033      1.1       rjs 			}
   4034      1.1       rjs 		}
   4035      1.1       rjs 		if (net->cwnd > sctp_pegs[SCTP_MAX_CWND]) {
   4036      1.1       rjs 			sctp_pegs[SCTP_MAX_CWND] = net->cwnd;
   4037      1.1       rjs 		}
   4038      1.1       rjs 	}
   4039      1.1       rjs 	/**********************************/
   4040      1.1       rjs 	/* Now what about shutdown issues */
   4041      1.1       rjs 	/**********************************/
   4042      1.1       rjs 	some_on_streamwheel = 0;
   4043      1.1       rjs 	if (!TAILQ_EMPTY(&asoc->out_wheel)) {
   4044      1.1       rjs 		/* Check to see if some data queued */
   4045      1.1       rjs 		struct sctp_stream_out *outs;
   4046      1.1       rjs 		TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
   4047      1.1       rjs 			if (!TAILQ_EMPTY(&outs->outqueue)) {
   4048      1.1       rjs 				some_on_streamwheel = 1;
   4049      1.1       rjs 				break;
   4050      1.1       rjs 			}
   4051      1.1       rjs 		}
   4052      1.1       rjs 	}
   4053      1.1       rjs 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue) &&
   4054      1.1       rjs 	    some_on_streamwheel == 0) {
   4055      1.1       rjs 		/* nothing left on sendqueue.. consider done */
   4056      1.1       rjs 		/* stop all timers */
   4057      1.1       rjs #ifdef SCTP_LOG_RWND
   4058      1.1       rjs 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
   4059      1.1       rjs 				  asoc->peers_rwnd, 0, 0,  a_rwnd);
   4060      1.1       rjs #endif
   4061      1.1       rjs 		asoc->peers_rwnd = a_rwnd;
   4062      1.1       rjs 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
   4063      1.1       rjs 			/* SWS sender side engages */
   4064      1.1       rjs 			asoc->peers_rwnd = 0;
   4065      1.1       rjs 		}
   4066      1.1       rjs 		/* stop any timers */
   4067      1.1       rjs 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   4068      1.1       rjs 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
   4069      1.1       rjs 					stcb, net);
   4070      1.1       rjs 			net->flight_size = 0;
   4071      1.1       rjs 			net->partial_bytes_acked = 0;
   4072      1.1       rjs 		}
   4073      1.1       rjs 		asoc->total_flight = 0;
   4074      1.1       rjs 		asoc->total_flight_count = 0;
   4075      1.1       rjs 		/* clean up */
   4076      1.1       rjs 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
   4077      1.1       rjs 			asoc->state = SCTP_STATE_SHUTDOWN_SENT;
   4078      1.1       rjs #ifdef SCTP_DEBUG
   4079      1.1       rjs 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   4080      1.1       rjs 				printf("%s:%d sends a shutdown\n",
   4081      1.1       rjs 				       __FILE__,
   4082      1.1       rjs 				       __LINE__
   4083      1.1       rjs 				       );
   4084      1.1       rjs 			}
   4085      1.1       rjs #endif
   4086      1.1       rjs 			sctp_send_shutdown(stcb,
   4087      1.1       rjs 					   stcb->asoc.primary_destination);
   4088      1.1       rjs 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
   4089      1.1       rjs 					 stcb->sctp_ep, stcb, asoc->primary_destination);
   4090      1.1       rjs 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
   4091      1.1       rjs 					 stcb->sctp_ep, stcb, asoc->primary_destination);
   4092      1.1       rjs 		} else if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) {
   4093      1.1       rjs 			asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
   4094      1.1       rjs 
   4095      1.1       rjs 			sctp_send_shutdown_ack(stcb,
   4096      1.1       rjs 					       stcb->asoc.primary_destination);
   4097      1.1       rjs 
   4098      1.1       rjs 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
   4099      1.1       rjs 					 stcb->sctp_ep, stcb, asoc->primary_destination);
   4100      1.1       rjs 		}
   4101      1.1       rjs 		return;
   4102      1.1       rjs 	}
   4103      1.1       rjs 	/*
   4104      1.1       rjs 	 * Now here we are going to recycle net_ack for a different
   4105      1.1       rjs 	 * use... HEADS UP.
   4106      1.1       rjs 	 */
   4107      1.1       rjs 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   4108      1.1       rjs 		net->net_ack = 0;
   4109      1.1       rjs 	}
   4110      1.1       rjs 	if ((num_seg > 0) && marking_allowed) {
   4111      1.1       rjs 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
   4112      1.1       rjs 					   strike_enabled, biggest_tsn_newly_acked, accum_moved);
   4113      1.1       rjs 	}
   4114      1.1       rjs 
   4115      1.1       rjs 	/*********************************************/
   4116      1.1       rjs 	/* Here we perform PR-SCTP procedures        */
   4117      1.1       rjs 	/* (section 4.2)                             */
   4118      1.1       rjs 	/*********************************************/
   4119      1.1       rjs 	/* C1. update advancedPeerAckPoint */
   4120      1.1       rjs 	if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
   4121      1.1       rjs 		asoc->advanced_peer_ack_point = cum_ack;
   4122      1.1       rjs 	}
   4123      1.1       rjs 	/* C2. try to further move advancedPeerAckPoint ahead */
   4124      1.1       rjs 	if (asoc->peer_supports_prsctp) {
   4125      1.1       rjs 		struct sctp_tmit_chunk *lchk;
   4126      1.1       rjs 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
   4127      1.1       rjs 		/* C3. See if we need to send a Fwd-TSN */
   4128      1.1       rjs 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
   4129      1.1       rjs 				      MAX_TSN)) {
   4130      1.1       rjs 			/*
   4131      1.1       rjs 			 * ISSUE with ECN, see FWD-TSN processing for notes
   4132      1.1       rjs 			 * on issues that will occur when the ECN NONCE stuff
   4133      1.1       rjs 			 * is put into SCTP for cross checking.
   4134      1.1       rjs 			 */
   4135      1.1       rjs 			send_forward_tsn(stcb, asoc);
   4136      1.1       rjs 
   4137      1.1       rjs 			/* ECN Nonce: Disable Nonce Sum check when FWD TSN is sent and store resync tsn*/
   4138      1.1       rjs 			asoc->nonce_sum_check = 0;
   4139      1.1       rjs 			asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
   4140      1.1       rjs 			if (lchk) {
   4141      1.1       rjs 				/* Assure a timer is up */
   4142      1.1       rjs 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
   4143      1.1       rjs 						 stcb->sctp_ep, stcb, lchk->whoTo);
   4144      1.1       rjs 			}
   4145      1.1       rjs 		}
   4146      1.1       rjs 	}
   4147      1.1       rjs 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   4148      1.1       rjs 		if (asoc->fast_retran_loss_recovery == 0) {
   4149      1.1       rjs 			/* out of a RFC2582 Fast recovery window? */
   4150      1.1       rjs 			if (net->net_ack > 0) {
   4151      1.1       rjs 				/*
   4152      1.1       rjs 				 * per section 7.2.3, are there
   4153      1.1       rjs 				 * any destinations that had a fast
   4154      1.1       rjs 				 * retransmit to them. If so what we
   4155      1.1       rjs 				 * need to do is adjust ssthresh and
   4156      1.1       rjs 				 * cwnd.
   4157      1.1       rjs 				 */
   4158      1.1       rjs 				struct sctp_tmit_chunk *lchk;
   4159      1.1       rjs #ifdef  SCTP_HIGH_SPEED
   4160      1.1       rjs 				sctp_hs_cwnd_decrease(net);
   4161      1.1       rjs #else
   4162      1.1       rjs #ifdef SCTP_CWND_LOGGING
   4163      1.1       rjs 				int old_cwnd = net->cwnd;
   4164      1.1       rjs #endif
   4165      1.1       rjs 				net->ssthresh = net->cwnd / 2;
   4166      1.1       rjs 				if (net->ssthresh < (net->mtu*2)) {
   4167      1.1       rjs 					net->ssthresh = 2 * net->mtu;
   4168      1.1       rjs 				}
   4169      1.1       rjs 				net->cwnd = net->ssthresh;
   4170      1.1       rjs #ifdef SCTP_CWND_LOGGING
   4171      1.1       rjs 				sctp_log_cwnd(net, (net->cwnd-old_cwnd),
   4172      1.1       rjs 					      SCTP_CWND_LOG_FROM_FR);
   4173      1.1       rjs #endif
   4174      1.1       rjs #endif
   4175      1.1       rjs 
   4176      1.1       rjs 				lchk = TAILQ_FIRST(&asoc->send_queue);
   4177      1.1       rjs 
   4178      1.1       rjs 				net->partial_bytes_acked = 0;
   4179      1.1       rjs 				/* Turn on fast recovery window */
   4180      1.1       rjs 				asoc->fast_retran_loss_recovery = 1;
   4181      1.1       rjs 				if (lchk == NULL) {
   4182      1.1       rjs 					/* Mark end of the window */
   4183      1.1       rjs 					asoc->fast_recovery_tsn = asoc->sending_seq - 1;
   4184      1.1       rjs 				} else {
   4185      1.1       rjs 					asoc->fast_recovery_tsn = lchk->rec.data.TSN_seq - 1;
   4186      1.1       rjs 				}
   4187      1.1       rjs 
   4188      1.1       rjs 
   4189      1.1       rjs 				/* Disable Nonce Sum Checking and store the resync tsn*/
   4190      1.1       rjs 				asoc->nonce_sum_check = 0;
   4191      1.1       rjs 				asoc->nonce_resync_tsn = asoc->fast_recovery_tsn + 1;
   4192      1.1       rjs 
   4193      1.1       rjs 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND,
   4194      1.1       rjs 						stcb->sctp_ep, stcb, net);
   4195      1.1       rjs 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
   4196      1.1       rjs 						 stcb->sctp_ep, stcb, net);
   4197      1.1       rjs 			}
   4198      1.1       rjs 		} else if (net->net_ack > 0) {
   4199      1.1       rjs 			/*
   4200      1.1       rjs 			 * Mark a peg that we WOULD have done a cwnd reduction
   4201      1.1       rjs 			 * but RFC2582 prevented this action.
   4202      1.1       rjs 			 */
   4203      1.1       rjs 			sctp_pegs[SCTP_FR_INAWINDOW]++;
   4204      1.1       rjs 		}
   4205      1.1       rjs 	}
   4206      1.1       rjs 
   4207      1.1       rjs 
   4208      1.1       rjs 	/******************************************************************
   4209      1.1       rjs 	 *  Here we do the stuff with ECN Nonce checking.
   4210      1.1       rjs 	 *  We basically check to see if the nonce sum flag was incorrect
   4211      1.1       rjs 	 *  or if resynchronization needs to be done. Also if we catch a
   4212      1.1       rjs 	 *  misbehaving receiver we give him the kick.
   4213      1.1       rjs 	 ******************************************************************/
   4214      1.1       rjs 
   4215      1.1       rjs 	if (asoc->ecn_nonce_allowed) {
   4216      1.1       rjs 		if (asoc->nonce_sum_check) {
   4217      1.1       rjs 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
   4218      1.1       rjs 				if (asoc->nonce_wait_for_ecne == 0) {
   4219      1.1       rjs 					struct sctp_tmit_chunk *lchk;
   4220      1.1       rjs 					lchk = TAILQ_FIRST(&asoc->send_queue);
   4221      1.1       rjs 					asoc->nonce_wait_for_ecne = 1;
   4222      1.1       rjs 					if (lchk) {
   4223      1.1       rjs 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
   4224      1.1       rjs 					} else {
   4225      1.1       rjs 						asoc->nonce_wait_tsn = asoc->sending_seq;
   4226      1.1       rjs 					}
   4227      1.1       rjs 				} else {
   4228      1.1       rjs 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
   4229      1.1       rjs 					   (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
   4230      1.1       rjs 						/* Misbehaving peer. We need to react to this guy */
   4231      1.1       rjs 						printf("Mis-behaving peer detected\n");
   4232      1.1       rjs 						asoc->ecn_allowed = 0;
   4233      1.1       rjs 						asoc->ecn_nonce_allowed = 0;
   4234      1.1       rjs 					}
   4235      1.1       rjs 				}
   4236      1.1       rjs 			}
   4237      1.1       rjs 		} else {
   4238      1.1       rjs 			/* See if Resynchronization Possible */
   4239      1.1       rjs 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
   4240      1.1       rjs 				asoc->nonce_sum_check = 1;
   4241      1.1       rjs 				/* now we must calculate what the base
   4242      1.1       rjs 				 * is. We do this based on two things, we know
   4243      1.1       rjs 				 * the total's for all the segments gap-acked
   4244      1.1       rjs 				 * in the SACK, its stored in ecn_seg_sums.
   4245      1.1       rjs 				 * We also know the SACK's nonce sum, its
   4246      1.1       rjs 				 * in nonce_sum_flag. So we can build a truth
   4247      1.1       rjs 				 * table to back-calculate the new value of asoc->nonce_sum_expect_base:
   4248      1.1       rjs                                  *
   4249      1.1       rjs                                  *   SACK-flag-Value         Seg-Sums              Base
   4250      1.1       rjs 				 *         0                    0                   0
   4251      1.1       rjs 				 *         1                    0                   1
   4252      1.1       rjs 				 *         0                    1                   1
   4253      1.1       rjs 				 *         1                    1                   0
   4254      1.1       rjs 				 */
   4255      1.1       rjs 				asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
   4256      1.1       rjs 			}
   4257      1.1       rjs 		}
   4258      1.1       rjs 	}
   4259      1.1       rjs 	/* Now are we exiting loss recovery ? */
   4260      1.1       rjs 	if (will_exit_fast_recovery) {
   4261      1.1       rjs 		/* Ok, we must exit fast recovery */
   4262      1.1       rjs 		asoc->fast_retran_loss_recovery = 0;
   4263      1.1       rjs 	}
   4264      1.1       rjs 	if ((asoc->sat_t3_loss_recovery) &&
   4265      1.1       rjs 	    ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
   4266      1.1       rjs 				MAX_TSN) ||
   4267      1.1       rjs 	      (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
   4268      1.1       rjs 		/* end satellite t3 loss recovery */
   4269      1.1       rjs 		asoc->sat_t3_loss_recovery = 0;
   4270      1.1       rjs 	}
   4271      1.1       rjs 	/* Adjust and set the new rwnd value */
   4272      1.1       rjs #ifdef SCTP_LOG_RWND
   4273      1.1       rjs 	sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
   4274      1.1       rjs 			  asoc->peers_rwnd,  asoc->total_flight, (asoc->sent_queue_cnt * sctp_peer_chunk_oh), a_rwnd);
   4275      1.1       rjs #endif
   4276      1.1       rjs 
   4277      1.1       rjs 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
   4278      1.1       rjs 					    (u_int32_t)(asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
   4279      1.1       rjs 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
   4280      1.1       rjs 		/* SWS sender side engages */
   4281      1.1       rjs 		asoc->peers_rwnd = 0;
   4282      1.1       rjs 	}
   4283      1.1       rjs 	/*
   4284      1.1       rjs 	 * Now we must setup so we have a timer up for anyone with
   4285      1.1       rjs 	 * outstanding data.
   4286      1.1       rjs 	 */
   4287      1.1       rjs 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   4288      1.1       rjs 		struct sctp_tmit_chunk *chk;
   4289      1.1       rjs 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
   4290      1.1       rjs 			if (chk->whoTo == net &&
   4291      1.1       rjs 			    (chk->sent < SCTP_DATAGRAM_ACKED ||
   4292      1.1       rjs 			     chk->sent == SCTP_FORWARD_TSN_SKIP)) {
   4293      1.1       rjs 				/*
   4294      1.1       rjs 				 * Not ack'ed and still outstanding to this
   4295      1.1       rjs 				 * destination or marked and must be
   4296      1.1       rjs 				 * sacked after fwd-tsn sent.
   4297      1.1       rjs 				 */
   4298      1.1       rjs 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
   4299      1.1       rjs 						 stcb->sctp_ep, stcb, net);
   4300      1.1       rjs 				break;
   4301      1.1       rjs 			}
   4302      1.1       rjs 		}
   4303      1.1       rjs 	}
   4304      1.1       rjs }
   4305      1.1       rjs 
   4306      1.1       rjs void
   4307      1.1       rjs sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
   4308      1.1       rjs     struct sctp_nets *netp, int *abort_flag)
   4309      1.1       rjs {
   4310      1.1       rjs 	/* Mutate a shutdown into a SACK */
   4311      1.1       rjs 	struct sctp_sack_chunk sack;
   4312      1.1       rjs 
   4313      1.1       rjs 	/* Copy cum-ack */
   4314      1.1       rjs 	sack.sack.cum_tsn_ack = cp->cumulative_tsn_ack;
   4315      1.1       rjs 	/* Arrange so a_rwnd does NOT change */
   4316      1.1       rjs 	sack.ch.chunk_type = SCTP_SELECTIVE_ACK;
   4317      1.1       rjs 	sack.ch.chunk_flags = 0;
   4318      1.1       rjs 	sack.ch.chunk_length = ntohs(sizeof(struct sctp_sack_chunk));
   4319      1.1       rjs 	sack.sack.a_rwnd =
   4320      1.1       rjs 	    htonl(stcb->asoc.peers_rwnd + stcb->asoc.total_flight);
   4321      1.1       rjs 	/*
   4322      1.1       rjs 	 * no gaps in this one. This may cause a temporal view to reneging,
   4323      1.1       rjs 	 * but hopefully the second chunk is a true SACK in the packet and
   4324      1.1       rjs 	 * will correct this view. One will come soon after no matter what
   4325      1.1       rjs 	 * to fix this.
   4326      1.1       rjs 	 */
   4327      1.1       rjs 	sack.sack.num_gap_ack_blks = 0;
   4328      1.1       rjs 	sack.sack.num_dup_tsns = 0;
   4329      1.1       rjs 	/* Now call the SACK processor */
   4330      1.1       rjs 	sctp_handle_sack(&sack, stcb, netp, abort_flag);
   4331      1.1       rjs }
   4332      1.1       rjs 
   4333      1.1       rjs static void
   4334      1.1       rjs sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
   4335      1.1       rjs     struct sctp_stream_in *strmin)
   4336      1.1       rjs {
   4337      1.1       rjs 	struct sctp_tmit_chunk *chk, *nchk;
   4338      1.1       rjs 	struct sctp_association *asoc;
   4339      1.1       rjs 	int tt;
   4340      1.1       rjs 
   4341      1.1       rjs 	asoc = &stcb->asoc;
   4342      1.1       rjs 	tt = strmin->last_sequence_delivered;
   4343      1.1       rjs 	/*
   4344      1.1       rjs 	 * First deliver anything prior to and including the stream no that
   4345      1.1       rjs 	 * came in
   4346      1.1       rjs 	 */
   4347      1.1       rjs 	chk = TAILQ_FIRST(&strmin->inqueue);
   4348      1.1       rjs 	while (chk) {
   4349      1.1       rjs 		nchk = TAILQ_NEXT(chk, sctp_next);
   4350      1.1       rjs 		if (compare_with_wrap(tt, chk->rec.data.stream_seq, MAX_SEQ) ||
   4351      1.1       rjs 		    (tt == chk->rec.data.stream_seq)) {
   4352      1.1       rjs 			/* this is deliverable now */
   4353      1.1       rjs 			TAILQ_REMOVE(&strmin->inqueue, chk, sctp_next);
   4354      1.1       rjs 			/* subtract pending on streams */
   4355      1.1       rjs 			asoc->size_on_all_streams -= chk->send_size;
   4356      1.1       rjs 			asoc->cnt_on_all_streams--;
   4357      1.1       rjs 			/* deliver it to at least the delivery-q */
   4358      1.1       rjs 			sctp_deliver_data(stcb, &stcb->asoc, chk, 0);
   4359      1.1       rjs 		} else {
   4360      1.1       rjs 			/* no more delivery now. */
   4361      1.1       rjs 			break;
   4362      1.1       rjs 		}
   4363      1.1       rjs 		chk = nchk;
   4364      1.1       rjs 	}
   4365      1.1       rjs 	/*
   4366      1.1       rjs 	 * now we must deliver things in queue the normal way  if any
   4367      1.1       rjs 	 * are now ready.
   4368      1.1       rjs 	 */
   4369      1.1       rjs 	tt = strmin->last_sequence_delivered + 1;
   4370      1.1       rjs 	chk = TAILQ_FIRST(&strmin->inqueue);
   4371      1.1       rjs 	while (chk) {
   4372      1.1       rjs 		nchk = TAILQ_NEXT(chk, sctp_next);
   4373      1.1       rjs 		if (tt == chk->rec.data.stream_seq) {
   4374      1.1       rjs 			/* this is deliverable now */
   4375      1.1       rjs 			TAILQ_REMOVE(&strmin->inqueue, chk, sctp_next);
   4376      1.1       rjs 			/* subtract pending on streams */
   4377      1.1       rjs 			asoc->size_on_all_streams -= chk->send_size;
   4378      1.1       rjs 			asoc->cnt_on_all_streams--;
   4379      1.1       rjs 			/* deliver it to at least the delivery-q */
   4380      1.1       rjs 			strmin->last_sequence_delivered =
   4381      1.1       rjs 			    chk->rec.data.stream_seq;
   4382      1.1       rjs 			sctp_deliver_data(stcb, &stcb->asoc, chk, 0);
   4383      1.1       rjs 			tt = strmin->last_sequence_delivered + 1;
   4384      1.1       rjs 		} else {
   4385      1.1       rjs 			break;
   4386      1.1       rjs 		}
   4387      1.1       rjs 		chk = nchk;
   4388      1.1       rjs 	}
   4389      1.1       rjs 
   4390      1.1       rjs }
   4391      1.1       rjs 
   4392      1.1       rjs void
   4393      1.1       rjs sctp_handle_forward_tsn(struct sctp_tcb *stcb,
   4394      1.1       rjs     struct sctp_forward_tsn_chunk *fwd, int *abort_flag)
   4395      1.1       rjs {
   4396      1.1       rjs 	/*
   4397      1.1       rjs 	 * ISSUES that MUST be fixed for ECN! When we are the
   4398      1.1       rjs 	 * sender of the forward TSN, when the SACK comes back
   4399      1.1       rjs 	 * that acknowledges the FWD-TSN we must reset the
   4400      1.1       rjs 	 * NONCE sum to match correctly. This will get quite
   4401      1.1       rjs 	 * tricky since we may have sent more data interveneing and
   4402      1.1       rjs 	 * must carefully account for what the SACK says on the
   4403      1.1       rjs 	 * nonce and any gaps that are reported. This work
   4404      1.1       rjs 	 * will NOT be done here, but I note it here since
   4405      1.1       rjs 	 * it is really related to PR-SCTP and FWD-TSN's
   4406      1.1       rjs 	 */
   4407      1.1       rjs 
   4408      1.1       rjs 	/* The pr-sctp fwd tsn */
   4409      1.1       rjs 	/*
   4410      1.1       rjs 	 * here we will perform all the data receiver side steps for
   4411      1.1       rjs 	 * processing FwdTSN, as required in by pr-sctp draft:
   4412      1.1       rjs 	 *
   4413      1.1       rjs 	 * Assume we get FwdTSN(x):
   4414      1.1       rjs 	 *
   4415      1.1       rjs 	 * 1) update local cumTSN to x
   4416      1.1       rjs 	 * 2) try to further advance cumTSN to x + others we have
   4417      1.1       rjs 	 * 3) examine and update re-ordering queue on pr-in-streams
   4418      1.1       rjs 	 * 4) clean up re-assembly queue
   4419      1.1       rjs 	 * 5) Send a sack to report where we are.
   4420      1.1       rjs 	 */
   4421      1.1       rjs 	struct sctp_strseq *stseq;
   4422      1.1       rjs 	struct sctp_association *asoc;
   4423      1.1       rjs 	u_int32_t new_cum_tsn, gap, back_out_htsn;
   4424      1.1       rjs 	unsigned int i, cnt_gone, fwd_sz, cumack_set_flag, m_size;
   4425      1.1       rjs 	struct sctp_stream_in *strm;
   4426      1.1       rjs 	struct sctp_tmit_chunk *chk, *at;
   4427      1.1       rjs 
   4428      1.1       rjs 	cumack_set_flag = 0;
   4429      1.1       rjs 	asoc = &stcb->asoc;
   4430      1.1       rjs 	cnt_gone = 0;
   4431      1.1       rjs 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
   4432      1.1       rjs #ifdef SCTP_DEBUG
   4433      1.1       rjs 		if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   4434      1.1       rjs 			printf("Bad size too small/big fwd-tsn\n");
   4435      1.1       rjs 		}
   4436      1.1       rjs #endif
   4437      1.1       rjs 		return;
   4438      1.1       rjs 	}
   4439      1.1       rjs 	m_size = (stcb->asoc.mapping_array_size << 3);
   4440      1.1       rjs 	/*************************************************************/
   4441      1.1       rjs 	/* 1. Here we update local cumTSN and shift the bitmap array */
   4442      1.1       rjs 	/*************************************************************/
   4443      1.1       rjs 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
   4444      1.1       rjs 
   4445      1.1       rjs 	if (compare_with_wrap(asoc->cumulative_tsn, new_cum_tsn, MAX_TSN) ||
   4446      1.1       rjs 	    asoc->cumulative_tsn == new_cum_tsn) {
   4447      1.1       rjs 		/* Already got there ... */
   4448      1.1       rjs 		return;
   4449      1.1       rjs 	}
   4450      1.1       rjs 
   4451      1.1       rjs 	back_out_htsn = asoc->highest_tsn_inside_map;
   4452      1.1       rjs 	if (compare_with_wrap(new_cum_tsn, asoc->highest_tsn_inside_map,
   4453      1.1       rjs 	    MAX_TSN)) {
   4454      1.1       rjs 		asoc->highest_tsn_inside_map = new_cum_tsn;
   4455      1.1       rjs #ifdef SCTP_MAP_LOGGING
   4456      1.1       rjs 			sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
   4457      1.1       rjs #endif
   4458      1.1       rjs 	}
   4459      1.1       rjs 	/*
   4460      1.1       rjs 	 * now we know the new TSN is more advanced, let's find the
   4461      1.1       rjs 	 * actual gap
   4462      1.1       rjs 	 */
   4463      1.1       rjs 	if ((compare_with_wrap(new_cum_tsn, asoc->mapping_array_base_tsn,
   4464      1.1       rjs 			       MAX_TSN)) ||
   4465      1.1       rjs 	     (new_cum_tsn == asoc->mapping_array_base_tsn)) {
   4466      1.1       rjs 		gap = new_cum_tsn - asoc->mapping_array_base_tsn;
   4467      1.1       rjs 	} else {
   4468      1.1       rjs 		/* try to prevent underflow here */
   4469      1.1       rjs 		gap = new_cum_tsn + (MAX_TSN - asoc->mapping_array_base_tsn) + 1;
   4470      1.1       rjs 	}
   4471      1.1       rjs 
   4472      1.1       rjs 	if (gap >= m_size) {
   4473      1.1       rjs 		asoc->highest_tsn_inside_map = back_out_htsn;
   4474      1.1       rjs 		if ((long)gap > sctp_sbspace(&stcb->sctp_socket->so_rcv)) {
   4475      1.1       rjs 			/*
   4476      1.1       rjs 			 * out of range (of single byte chunks in the rwnd I
   4477      1.1       rjs 			 * give out)
   4478      1.1       rjs 			 * too questionable. better to drop it silently
   4479      1.1       rjs 			 */
   4480      1.1       rjs 			return;
   4481      1.1       rjs 		}
   4482      1.1       rjs 		if (asoc->highest_tsn_inside_map >
   4483      1.1       rjs 		    asoc->mapping_array_base_tsn) {
   4484      1.1       rjs 			gap = asoc->highest_tsn_inside_map -
   4485      1.1       rjs 			    asoc->mapping_array_base_tsn;
   4486      1.1       rjs 		} else {
   4487      1.1       rjs 			gap = asoc->highest_tsn_inside_map +
   4488      1.1       rjs 			    (MAX_TSN - asoc->mapping_array_base_tsn) + 1;
   4489      1.1       rjs 		}
   4490      1.1       rjs 		cumack_set_flag = 1;
   4491      1.1       rjs 	}
   4492      1.1       rjs 	for (i = 0; i <= gap; i++) {
   4493      1.1       rjs 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, i);
   4494      1.1       rjs 	}
   4495      1.1       rjs 	/*
   4496      1.1       rjs 	 * Now after marking all, slide thing forward but no
   4497      1.1       rjs 	 * sack please.
   4498      1.1       rjs 	 */
   4499      1.1       rjs 	sctp_sack_check(stcb, 0, 0, abort_flag);
   4500      1.1       rjs 	if (*abort_flag)
   4501      1.1       rjs 		return;
   4502      1.1       rjs 
   4503      1.1       rjs 	if (cumack_set_flag) {
   4504      1.1       rjs 		/*
   4505      1.1       rjs 		 * fwd-tsn went outside my gap array - not a
   4506      1.1       rjs 		 * common occurance. Do the same thing we
   4507      1.1       rjs 		 * do when a cookie-echo arrives.
   4508      1.1       rjs 		 */
   4509      1.1       rjs 		asoc->highest_tsn_inside_map =  new_cum_tsn - 1;
   4510      1.1       rjs 		asoc->mapping_array_base_tsn = new_cum_tsn;
   4511      1.1       rjs 		asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
   4512      1.1       rjs #ifdef SCTP_MAP_LOGGING
   4513      1.1       rjs 		sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
   4514      1.1       rjs #endif
   4515      1.1       rjs 		asoc->last_echo_tsn = asoc->highest_tsn_inside_map;
   4516      1.1       rjs 	}
   4517      1.1       rjs 	/*************************************************************/
   4518      1.1       rjs 	/* 2. Clear up re-assembly queue                             */
   4519      1.1       rjs 	/*************************************************************/
   4520      1.1       rjs 
   4521      1.1       rjs 	/*
   4522      1.1       rjs 	 * First service it if pd-api is up, just in case we can
   4523      1.1       rjs 	 * progress it forward
   4524      1.1       rjs 	 */
   4525      1.1       rjs 	if (asoc->fragmented_delivery_inprogress) {
   4526      1.1       rjs 		sctp_service_reassembly(stcb, asoc, 0);
   4527      1.1       rjs 	}
   4528      1.1       rjs 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
   4529      1.1       rjs 		/* For each one on here see if we need to toss it */
   4530      1.1       rjs 		/*
   4531      1.1       rjs 		 * For now large messages held on the reasmqueue that are
   4532      1.1       rjs 		 * complete will be tossed too. We could in theory do more
   4533      1.1       rjs 		 * work to spin through and stop after dumping one msg
   4534      1.1       rjs 		 * aka seeing the start of a new msg at the head, and call
   4535      1.1       rjs 		 * the delivery function... to see if it can be delivered...
   4536      1.1       rjs 		 * But for now we just dump everything on the queue.
   4537      1.1       rjs 		 */
   4538      1.1       rjs 		chk = TAILQ_FIRST(&asoc->reasmqueue);
   4539      1.1       rjs 		while (chk) {
   4540      1.1       rjs 			at = TAILQ_NEXT(chk, sctp_next);
   4541      1.1       rjs 			if (compare_with_wrap(asoc->cumulative_tsn,
   4542      1.1       rjs 			    chk->rec.data.TSN_seq, MAX_TSN) ||
   4543      1.1       rjs 			    asoc->cumulative_tsn == chk->rec.data.TSN_seq) {
   4544      1.1       rjs 				/* It needs to be tossed */
   4545      1.1       rjs 				TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
   4546      1.1       rjs 				if (compare_with_wrap(chk->rec.data.TSN_seq,
   4547      1.1       rjs 				    asoc->tsn_last_delivered, MAX_TSN)) {
   4548      1.1       rjs 					asoc->tsn_last_delivered =
   4549      1.1       rjs 					    chk->rec.data.TSN_seq;
   4550      1.1       rjs 					asoc->str_of_pdapi =
   4551      1.1       rjs 					    chk->rec.data.stream_number;
   4552      1.1       rjs 					asoc->ssn_of_pdapi =
   4553      1.1       rjs 					    chk->rec.data.stream_seq;
   4554      1.1       rjs 					asoc->fragment_flags =
   4555      1.1       rjs 					    chk->rec.data.rcv_flags;
   4556      1.1       rjs 				}
   4557      1.1       rjs 				asoc->size_on_reasm_queue -= chk->send_size;
   4558      1.1       rjs 				asoc->cnt_on_reasm_queue--;
   4559      1.1       rjs 				cnt_gone++;
   4560      1.1       rjs 
   4561      1.1       rjs 				/* Clear up any stream problem */
   4562      1.1       rjs 				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
   4563      1.1       rjs 				    SCTP_DATA_UNORDERED &&
   4564      1.1       rjs 				    (compare_with_wrap(chk->rec.data.stream_seq,
   4565      1.1       rjs 				    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
   4566      1.1       rjs 				    MAX_SEQ))) {
   4567      1.1       rjs 					/*
   4568      1.1       rjs 					 * We must dump forward this streams
   4569      1.1       rjs 					 * sequence number if the chunk is not
   4570      1.1       rjs 					 * unordered that is being skipped.
   4571      1.1       rjs 					 * There is a chance that if the peer
   4572      1.1       rjs 					 * does not include the last fragment
   4573      1.1       rjs 					 * in its FWD-TSN we WILL have a problem
   4574      1.1       rjs 					 * here since you would have a partial
   4575      1.1       rjs 					 * chunk in queue that may not be
   4576      1.1       rjs 					 * deliverable.
   4577      1.1       rjs 					 * Also if a Partial delivery API as
   4578      1.1       rjs 					 * started the user may get a partial
   4579      1.1       rjs 					 * chunk. The next read returning a new
   4580      1.1       rjs 					 * chunk... really ugly but I see no way
   4581      1.1       rjs 					 * around it! Maybe a notify??
   4582      1.1       rjs 					 */
   4583      1.1       rjs 					asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
   4584      1.1       rjs 					    chk->rec.data.stream_seq;
   4585      1.1       rjs 				}
   4586      1.1       rjs 				if (chk->data) {
   4587      1.1       rjs 					sctp_m_freem(chk->data);
   4588      1.1       rjs 					chk->data = NULL;
   4589      1.1       rjs 				}
   4590      1.1       rjs 				sctp_free_remote_addr(chk->whoTo);
   4591      1.1       rjs 				SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   4592      1.1       rjs 				sctppcbinfo.ipi_count_chunk--;
   4593      1.1       rjs 				if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   4594      1.1       rjs 					panic("Chunk count is negative");
   4595      1.1       rjs 				}
   4596      1.1       rjs 				sctppcbinfo.ipi_gencnt_chunk++;
   4597      1.1       rjs 			} else {
   4598      1.1       rjs 				/*
   4599      1.1       rjs 				 * Ok we have gone beyond the end of the
   4600      1.1       rjs 				 * fwd-tsn's mark. Some checks...
   4601      1.1       rjs 				 */
   4602      1.1       rjs 				if ((asoc->fragmented_delivery_inprogress) &&
   4603      1.1       rjs 				    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
   4604      1.1       rjs 					/* Special case PD-API is up and what we fwd-tsn'
   4605      1.1       rjs 					 * over includes one that had the LAST_FRAG. We
   4606      1.1       rjs 					 * no longer need to do the PD-API.
   4607      1.1       rjs 					 */
   4608      1.1       rjs 					asoc->fragmented_delivery_inprogress = 0;
   4609      1.1       rjs 					sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
   4610      1.1       rjs 					    stcb, SCTP_PARTIAL_DELIVERY_ABORTED, (void *)NULL);
   4611      1.1       rjs 
   4612      1.1       rjs 				}
   4613      1.1       rjs 				break;
   4614      1.1       rjs 			}
   4615      1.1       rjs 			chk = at;
   4616      1.1       rjs 		}
   4617      1.1       rjs 	}
   4618      1.1       rjs 	if (asoc->fragmented_delivery_inprogress) {
   4619      1.1       rjs 		/*
   4620      1.1       rjs 		 * Ok we removed cnt_gone chunks in the PD-API queue that
   4621      1.1       rjs 		 * were being delivered. So now we must turn off the
   4622      1.1       rjs 		 * flag.
   4623      1.1       rjs 		 */
   4624      1.1       rjs 		sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
   4625      1.1       rjs 		    stcb, SCTP_PARTIAL_DELIVERY_ABORTED, (void *)NULL);
   4626      1.1       rjs 		asoc->fragmented_delivery_inprogress = 0;
   4627      1.1       rjs 	}
   4628      1.1       rjs 	/*************************************************************/
   4629      1.1       rjs 	/* 3. Update the PR-stream re-ordering queues                */
   4630      1.1       rjs 	/*************************************************************/
   4631      1.1       rjs 	stseq = (struct sctp_strseq *)((vaddr_t)fwd + sizeof(*fwd));
   4632      1.1       rjs 	fwd_sz -= sizeof(*fwd);
   4633      1.1       rjs 	{
   4634      1.1       rjs 		/* New method. */
   4635      1.1       rjs 		int num_str;
   4636      1.1       rjs 		num_str = fwd_sz/sizeof(struct sctp_strseq);
   4637      1.1       rjs #ifdef SCTP_DEBUG
   4638      1.1       rjs 		if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   4639      1.1       rjs 			printf("Using NEW method, %d strseq's reported in FWD-TSN\n",
   4640      1.1       rjs 			    num_str);
   4641      1.1       rjs 		}
   4642      1.1       rjs #endif
   4643      1.1       rjs 		for (i = 0; i < num_str; i++) {
   4644      1.1       rjs 			u_int16_t st;
   4645      1.1       rjs #if 0
   4646      1.1       rjs 			unsigned char *xx;
   4647      1.1       rjs 			/* Convert */
   4648      1.1       rjs 			xx = (unsigned char *)&stseq[i];
   4649      1.1       rjs #endif
   4650      1.1       rjs 			st = ntohs(stseq[i].stream);
   4651      1.1       rjs 			stseq[i].stream = st;
   4652      1.1       rjs 			st = ntohs(stseq[i].sequence);
   4653      1.1       rjs 			stseq[i].sequence = st;
   4654      1.1       rjs 			/* now process */
   4655      1.1       rjs 			if (stseq[i].stream > asoc->streamincnt) {
   4656      1.1       rjs #ifdef SCTP_DEBUG
   4657      1.1       rjs 				if (sctp_debug_on & SCTP_DEBUG_INDATA1) {
   4658      1.1       rjs 					printf("Bogus stream number %d "
   4659      1.1       rjs 					    "streamincnt is %d\n",
   4660      1.1       rjs 					    stseq[i].stream, asoc->streamincnt);
   4661      1.1       rjs 				}
   4662      1.1       rjs #endif
   4663      1.1       rjs 				/*
   4664      1.1       rjs 				 * It is arguable if we should continue. Since
   4665      1.1       rjs 				 * the peer sent bogus stream info we may be in
   4666      1.1       rjs 				 * deep trouble..
   4667      1.1       rjs  				 * a return may be a better choice?
   4668      1.1       rjs  				 */
   4669      1.1       rjs  				continue;
   4670      1.1       rjs  			}
   4671      1.1       rjs 			strm = &asoc->strmin[stseq[i].stream];
   4672      1.1       rjs 			if (compare_with_wrap(stseq[i].sequence,
   4673      1.1       rjs 			    strm->last_sequence_delivered, MAX_SEQ)) {
   4674      1.1       rjs 				/* Update the sequence number */
   4675      1.1       rjs 				strm->last_sequence_delivered =
   4676      1.1       rjs 				    stseq[i].sequence;
   4677      1.1       rjs 			}
   4678      1.1       rjs 			/* now kick the stream the new way */
   4679      1.1       rjs 			sctp_kick_prsctp_reorder_queue(stcb, strm);
   4680      1.1       rjs 		}
   4681      1.1       rjs 	}
   4682      1.1       rjs }
   4683