]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/sctp_indata.c
This commit was generated by cvs2svn to compensate for changes in r170964,
[FreeBSD/FreeBSD.git] / sys / netinet / sctp_indata.c
1 /*-
2  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 /* $KAME: sctp_indata.c,v 1.36 2005/03/06 16:04:17 itojun Exp $  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_var.h>
38 #include <netinet/sctp_sysctl.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctputil.h>
42 #include <netinet/sctp_output.h>
43 #include <netinet/sctp_input.h>
44 #include <netinet/sctp_indata.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctp_timer.h>
47
48
49 /*
50  * NOTES: On the outbound side of things I need to check the sack timer to
51  * see if I should generate a sack into the chunk queue (if I have data to
52  * send that is and will be sending it .. for bundling.
53  *
54  * The callback in sctp_usrreq.c will get called when the socket is read from.
55  * This will cause sctp_service_queues() to get called on the top entry in
56  * the list.
57  */
58
59 void
60 sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
61 {
62         uint32_t calc, calc_w_oh;
63
64         /*
65          * This is really set wrong with respect to a 1-2-m socket. Since
66          * the sb_cc is the count that everyone as put up. When we re-write
67          * sctp_soreceive then we will fix this so that ONLY this
68          * associations data is taken into account.
69          */
70         if (stcb->sctp_socket == NULL)
71                 return;
72
73         if (stcb->asoc.sb_cc == 0 &&
74             asoc->size_on_reasm_queue == 0 &&
75             asoc->size_on_all_streams == 0) {
76                 /* Full rwnd granted */
77                 asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
78                     SCTP_MINIMAL_RWND);
79                 return;
80         }
81         /* get actual space */
82         calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
83
84         /*
85          * take out what has NOT been put on socket queue and we yet hold
86          * for putting up.
87          */
88         calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_reasm_queue);
89         calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_all_streams);
90
91         if (calc == 0) {
92                 /* out of space */
93                 asoc->my_rwnd = 0;
94                 return;
95         }
96         /* what is the overhead of all these rwnd's */
97         calc_w_oh = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
98         asoc->my_rwnd = calc;
99         if (calc_w_oh == 0) {
100                 /*
101                  * If our overhead is greater than the advertised rwnd, we
102                  * clamp the rwnd to 1. This lets us still accept inbound
103                  * segments, but hopefully will shut the sender down when he
104                  * finally gets the message.
105                  */
106                 asoc->my_rwnd = 1;
107         } else {
108                 /* SWS threshold */
109                 if (asoc->my_rwnd &&
110                     (asoc->my_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_receiver)) {
111                         /* SWS engaged, tell peer none left */
112                         asoc->my_rwnd = 1;
113                 }
114         }
115 }
116
117 /* Calculate what the rwnd would be */
118
119 uint32_t
120 sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
121 {
122         uint32_t calc = 0, calc_w_oh;
123
124         /*
125          * This is really set wrong with respect to a 1-2-m socket. Since
126          * the sb_cc is the count that everyone as put up. When we re-write
127          * sctp_soreceive then we will fix this so that ONLY this
128          * associations data is taken into account.
129          */
130         if (stcb->sctp_socket == NULL)
131                 return (calc);
132
133         if (stcb->asoc.sb_cc == 0 &&
134             asoc->size_on_reasm_queue == 0 &&
135             asoc->size_on_all_streams == 0) {
136                 /* Full rwnd granted */
137                 calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
138                     SCTP_MINIMAL_RWND);
139                 return (calc);
140         }
141         /* get actual space */
142         calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
143
144         /*
145          * take out what has NOT been put on socket queue and we yet hold
146          * for putting up.
147          */
148         calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_reasm_queue);
149         calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_all_streams);
150
151         if (calc == 0) {
152                 /* out of space */
153                 return (calc);
154         }
155         /* what is the overhead of all these rwnd's */
156         calc_w_oh = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
157         if (calc_w_oh == 0) {
158                 /*
159                  * If our overhead is greater than the advertised rwnd, we
160                  * clamp the rwnd to 1. This lets us still accept inbound
161                  * segments, but hopefully will shut the sender down when he
162                  * finally gets the message.
163                  */
164                 calc = 1;
165         } else {
166                 /* SWS threshold */
167                 if (calc &&
168                     (calc < stcb->sctp_ep->sctp_ep.sctp_sws_receiver)) {
169                         /* SWS engaged, tell peer none left */
170                         calc = 1;
171                 }
172         }
173         return (calc);
174 }
175
176
177
178 /*
179  * Build out our readq entry based on the incoming packet.
180  */
181 struct sctp_queued_to_read *
182 sctp_build_readq_entry(struct sctp_tcb *stcb,
183     struct sctp_nets *net,
184     uint32_t tsn, uint32_t ppid,
185     uint32_t context, uint16_t stream_no,
186     uint16_t stream_seq, uint8_t flags,
187     struct mbuf *dm)
188 {
189         struct sctp_queued_to_read *read_queue_e = NULL;
190
191         sctp_alloc_a_readq(stcb, read_queue_e);
192         if (read_queue_e == NULL) {
193                 goto failed_build;
194         }
195         read_queue_e->sinfo_stream = stream_no;
196         read_queue_e->sinfo_ssn = stream_seq;
197         read_queue_e->sinfo_flags = (flags << 8);
198         read_queue_e->sinfo_ppid = ppid;
199         read_queue_e->sinfo_context = stcb->asoc.context;
200         read_queue_e->sinfo_timetolive = 0;
201         read_queue_e->sinfo_tsn = tsn;
202         read_queue_e->sinfo_cumtsn = tsn;
203         read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
204         read_queue_e->whoFrom = net;
205         read_queue_e->length = 0;
206         atomic_add_int(&net->ref_count, 1);
207         read_queue_e->data = dm;
208         read_queue_e->spec_flags = 0;
209         read_queue_e->tail_mbuf = NULL;
210         read_queue_e->aux_data = NULL;
211         read_queue_e->stcb = stcb;
212         read_queue_e->port_from = stcb->rport;
213         read_queue_e->do_not_ref_stcb = 0;
214         read_queue_e->end_added = 0;
215         read_queue_e->some_taken = 0;
216         read_queue_e->pdapi_aborted = 0;
217 failed_build:
218         return (read_queue_e);
219 }
220
221
222 /*
223  * Build out our readq entry based on the incoming packet.
224  */
225 static struct sctp_queued_to_read *
226 sctp_build_readq_entry_chk(struct sctp_tcb *stcb,
227     struct sctp_tmit_chunk *chk)
228 {
229         struct sctp_queued_to_read *read_queue_e = NULL;
230
231         sctp_alloc_a_readq(stcb, read_queue_e);
232         if (read_queue_e == NULL) {
233                 goto failed_build;
234         }
235         read_queue_e->sinfo_stream = chk->rec.data.stream_number;
236         read_queue_e->sinfo_ssn = chk->rec.data.stream_seq;
237         read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8);
238         read_queue_e->sinfo_ppid = chk->rec.data.payloadtype;
239         read_queue_e->sinfo_context = stcb->asoc.context;
240         read_queue_e->sinfo_timetolive = 0;
241         read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq;
242         read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq;
243         read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
244         read_queue_e->whoFrom = chk->whoTo;
245         read_queue_e->aux_data = NULL;
246         read_queue_e->length = 0;
247         atomic_add_int(&chk->whoTo->ref_count, 1);
248         read_queue_e->data = chk->data;
249         read_queue_e->tail_mbuf = NULL;
250         read_queue_e->stcb = stcb;
251         read_queue_e->port_from = stcb->rport;
252         read_queue_e->spec_flags = 0;
253         read_queue_e->do_not_ref_stcb = 0;
254         read_queue_e->end_added = 0;
255         read_queue_e->some_taken = 0;
256         read_queue_e->pdapi_aborted = 0;
257 failed_build:
258         return (read_queue_e);
259 }
260
261
262 struct mbuf *
263 sctp_build_ctl_nchunk(struct sctp_inpcb *inp,
264     struct sctp_sndrcvinfo *sinfo)
265 {
266         struct sctp_sndrcvinfo *outinfo;
267         struct cmsghdr *cmh;
268         struct mbuf *ret;
269         int len;
270         int use_extended = 0;
271
272         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
273                 /* user does not want the sndrcv ctl */
274                 return (NULL);
275         }
276         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
277                 use_extended = 1;
278                 len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
279         } else {
280                 len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
281         }
282
283
284         ret = sctp_get_mbuf_for_msg(len,
285             0, M_DONTWAIT, 1, MT_DATA);
286
287         if (ret == NULL) {
288                 /* No space */
289                 return (ret);
290         }
291         /* We need a CMSG header followed by the struct  */
292         cmh = mtod(ret, struct cmsghdr *);
293         outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
294         cmh->cmsg_level = IPPROTO_SCTP;
295         if (use_extended) {
296                 cmh->cmsg_type = SCTP_EXTRCV;
297                 cmh->cmsg_len = len;
298                 memcpy(outinfo, sinfo, len);
299         } else {
300                 cmh->cmsg_type = SCTP_SNDRCV;
301                 cmh->cmsg_len = len;
302                 *outinfo = *sinfo;
303         }
304         SCTP_BUF_LEN(ret) = cmh->cmsg_len;
305         return (ret);
306 }
307
308
309 char *
310 sctp_build_ctl_cchunk(struct sctp_inpcb *inp,
311     int *control_len,
312     struct sctp_sndrcvinfo *sinfo)
313 {
314         struct sctp_sndrcvinfo *outinfo;
315         struct cmsghdr *cmh;
316         char *buf;
317         int len;
318         int use_extended = 0;
319
320         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
321                 /* user does not want the sndrcv ctl */
322                 return (NULL);
323         }
324         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
325                 use_extended = 1;
326                 len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
327         } else {
328                 len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
329         }
330         SCTP_MALLOC(buf, char *, len, SCTP_M_CMSG);
331         if (buf == NULL) {
332                 /* No space */
333                 return (buf);
334         }
335         /* We need a CMSG header followed by the struct  */
336         cmh = (struct cmsghdr *)buf;
337         outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
338         cmh->cmsg_level = IPPROTO_SCTP;
339         if (use_extended) {
340                 cmh->cmsg_type = SCTP_EXTRCV;
341                 cmh->cmsg_len = len;
342                 memcpy(outinfo, sinfo, len);
343         } else {
344                 cmh->cmsg_type = SCTP_SNDRCV;
345                 cmh->cmsg_len = len;
346                 *outinfo = *sinfo;
347         }
348         *control_len = len;
349         return (buf);
350 }
351
352
353 /*
354  * We are delivering currently from the reassembly queue. We must continue to
355  * deliver until we either: 1) run out of space. 2) run out of sequential
356  * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
357  */
358 static void
359 sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
360 {
361         struct sctp_tmit_chunk *chk;
362         uint16_t nxt_todel;
363         uint16_t stream_no;
364         int end = 0;
365         int cntDel;
366         struct sctp_queued_to_read *control, *ctl, *ctlat;
367
368         if (stcb == NULL)
369                 return;
370
371         cntDel = stream_no = 0;
372         if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
373             (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
374                 /* socket above is long gone */
375                 asoc->fragmented_delivery_inprogress = 0;
376                 chk = TAILQ_FIRST(&asoc->reasmqueue);
377                 while (chk) {
378                         TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
379                         asoc->size_on_reasm_queue -= chk->send_size;
380                         sctp_ucount_decr(asoc->cnt_on_reasm_queue);
381                         /*
382                          * Lose the data pointer, since its in the socket
383                          * buffer
384                          */
385                         if (chk->data) {
386                                 sctp_m_freem(chk->data);
387                                 chk->data = NULL;
388                         }
389                         /* Now free the address and data */
390                         sctp_free_remote_addr(chk->whoTo);
391                         sctp_free_a_chunk(stcb, chk);
392                         /* sa_ignore FREED_MEMORY */
393                         chk = TAILQ_FIRST(&asoc->reasmqueue);
394                 }
395                 return;
396         }
397         SCTP_TCB_LOCK_ASSERT(stcb);
398         do {
399                 chk = TAILQ_FIRST(&asoc->reasmqueue);
400                 if (chk == NULL) {
401                         return;
402                 }
403                 if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
404                         /* Can't deliver more :< */
405                         return;
406                 }
407                 stream_no = chk->rec.data.stream_number;
408                 nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
409                 if (nxt_todel != chk->rec.data.stream_seq &&
410                     (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
411                         /*
412                          * Not the next sequence to deliver in its stream OR
413                          * unordered
414                          */
415                         return;
416                 }
417                 if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
418
419                         control = sctp_build_readq_entry_chk(stcb, chk);
420                         if (control == NULL) {
421                                 /* out of memory? */
422                                 return;
423                         }
424                         /* save it off for our future deliveries */
425                         stcb->asoc.control_pdapi = control;
426                         if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
427                                 end = 1;
428                         else
429                                 end = 0;
430                         sctp_add_to_readq(stcb->sctp_ep,
431                             stcb, control, &stcb->sctp_socket->so_rcv, end);
432                         cntDel++;
433                 } else {
434                         if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
435                                 end = 1;
436                         else
437                                 end = 0;
438                         if (sctp_append_to_readq(stcb->sctp_ep, stcb,
439                             stcb->asoc.control_pdapi,
440                             chk->data, end, chk->rec.data.TSN_seq,
441                             &stcb->sctp_socket->so_rcv)) {
442                                 /*
443                                  * something is very wrong, either
444                                  * control_pdapi is NULL, or the tail_mbuf
445                                  * is corrupt, or there is a EOM already on
446                                  * the mbuf chain.
447                                  */
448                                 if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
449                                         panic("This should not happen control_pdapi NULL?");
450                                 }
451                                 /* if we did not panic, it was a EOM */
452                                 panic("Bad chunking ??");
453                                 return;
454                         }
455                         cntDel++;
456                 }
457                 /* pull it we did it */
458                 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
459                 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
460                         asoc->fragmented_delivery_inprogress = 0;
461                         if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
462                                 asoc->strmin[stream_no].last_sequence_delivered++;
463                         }
464                         if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
465                                 SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
466                         }
467                 } else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
468                         /*
469                          * turn the flag back on since we just  delivered
470                          * yet another one.
471                          */
472                         asoc->fragmented_delivery_inprogress = 1;
473                 }
474                 asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
475                 asoc->last_flags_delivered = chk->rec.data.rcv_flags;
476                 asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
477                 asoc->last_strm_no_delivered = chk->rec.data.stream_number;
478
479                 asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
480                 asoc->size_on_reasm_queue -= chk->send_size;
481                 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
482                 /* free up the chk */
483                 chk->data = NULL;
484                 sctp_free_remote_addr(chk->whoTo);
485                 sctp_free_a_chunk(stcb, chk);
486
487                 if (asoc->fragmented_delivery_inprogress == 0) {
488                         /*
489                          * Now lets see if we can deliver the next one on
490                          * the stream
491                          */
492                         struct sctp_stream_in *strm;
493
494                         strm = &asoc->strmin[stream_no];
495                         nxt_todel = strm->last_sequence_delivered + 1;
496                         ctl = TAILQ_FIRST(&strm->inqueue);
497                         if (ctl && (nxt_todel == ctl->sinfo_ssn)) {
498                                 while (ctl != NULL) {
499                                         /* Deliver more if we can. */
500                                         if (nxt_todel == ctl->sinfo_ssn) {
501                                                 ctlat = TAILQ_NEXT(ctl, next);
502                                                 TAILQ_REMOVE(&strm->inqueue, ctl, next);
503                                                 asoc->size_on_all_streams -= ctl->length;
504                                                 sctp_ucount_decr(asoc->cnt_on_all_streams);
505                                                 strm->last_sequence_delivered++;
506                                                 sctp_add_to_readq(stcb->sctp_ep, stcb,
507                                                     ctl,
508                                                     &stcb->sctp_socket->so_rcv, 1);
509                                                 ctl = ctlat;
510                                         } else {
511                                                 break;
512                                         }
513                                         nxt_todel = strm->last_sequence_delivered + 1;
514                                 }
515                         }
516                         break;
517                 }
518                 /* sa_ignore FREED_MEMORY */
519                 chk = TAILQ_FIRST(&asoc->reasmqueue);
520         } while (chk);
521 }
522
523 /*
524  * Queue the chunk either right into the socket buffer if it is the next one
525  * to go OR put it in the correct place in the delivery queue.  If we do
526  * append to the so_buf, keep doing so until we are out of order. One big
527  * question still remains, what to do when the socket buffer is FULL??
528  */
529 static void
530 sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
531     struct sctp_queued_to_read *control, int *abort_flag)
532 {
533         /*
534          * FIX-ME maybe? What happens when the ssn wraps? If we are getting
535          * all the data in one stream this could happen quite rapidly. One
536          * could use the TSN to keep track of things, but this scheme breaks
537          * down in the other type of stream useage that could occur. Send a
538          * single msg to stream 0, send 4Billion messages to stream 1, now
539          * send a message to stream 0. You have a situation where the TSN
540          * has wrapped but not in the stream. Is this worth worrying about
541          * or should we just change our queue sort at the bottom to be by
542          * TSN.
543          * 
544          * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
545          * with TSN 1? If the peer is doing some sort of funky TSN/SSN
546          * assignment this could happen... and I don't see how this would be
547          * a violation. So for now I am undecided an will leave the sort by
548          * SSN alone. Maybe a hybred approach is the answer
549          * 
550          */
551         struct sctp_stream_in *strm;
552         struct sctp_queued_to_read *at;
553         int queue_needed;
554         uint16_t nxt_todel;
555         struct mbuf *oper;
556
557         queue_needed = 1;
558         asoc->size_on_all_streams += control->length;
559         sctp_ucount_incr(asoc->cnt_on_all_streams);
560         strm = &asoc->strmin[control->sinfo_stream];
561         nxt_todel = strm->last_sequence_delivered + 1;
562         if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
563                 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
564         }
565         SCTPDBG(SCTP_DEBUG_INDATA1,
566             "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
567             (uint32_t) control->sinfo_stream,
568             (uint32_t) strm->last_sequence_delivered,
569             (uint32_t) nxt_todel);
570         if (compare_with_wrap(strm->last_sequence_delivered,
571             control->sinfo_ssn, MAX_SEQ) ||
572             (strm->last_sequence_delivered == control->sinfo_ssn)) {
573                 /* The incoming sseq is behind where we last delivered? */
574                 SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort  association\n",
575                     control->sinfo_ssn, strm->last_sequence_delivered);
576                 /*
577                  * throw it in the stream so it gets cleaned up in
578                  * association destruction
579                  */
580                 TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
581                 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
582                     0, M_DONTWAIT, 1, MT_DATA);
583                 if (oper) {
584                         struct sctp_paramhdr *ph;
585                         uint32_t *ippp;
586
587                         SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
588                             (sizeof(uint32_t) * 3);
589                         ph = mtod(oper, struct sctp_paramhdr *);
590                         ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
591                         ph->param_length = htons(SCTP_BUF_LEN(oper));
592                         ippp = (uint32_t *) (ph + 1);
593                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_1);
594                         ippp++;
595                         *ippp = control->sinfo_tsn;
596                         ippp++;
597                         *ippp = ((control->sinfo_stream << 16) | control->sinfo_ssn);
598                 }
599                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
600                 sctp_abort_an_association(stcb->sctp_ep, stcb,
601                     SCTP_PEER_FAULTY, oper);
602
603                 *abort_flag = 1;
604                 return;
605
606         }
607         if (nxt_todel == control->sinfo_ssn) {
608                 /* can be delivered right away? */
609                 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
610                         sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
611                 }
612                 queue_needed = 0;
613                 asoc->size_on_all_streams -= control->length;
614                 sctp_ucount_decr(asoc->cnt_on_all_streams);
615                 strm->last_sequence_delivered++;
616                 sctp_add_to_readq(stcb->sctp_ep, stcb,
617                     control,
618                     &stcb->sctp_socket->so_rcv, 1);
619                 control = TAILQ_FIRST(&strm->inqueue);
620                 while (control != NULL) {
621                         /* all delivered */
622                         nxt_todel = strm->last_sequence_delivered + 1;
623                         if (nxt_todel == control->sinfo_ssn) {
624                                 at = TAILQ_NEXT(control, next);
625                                 TAILQ_REMOVE(&strm->inqueue, control, next);
626                                 asoc->size_on_all_streams -= control->length;
627                                 sctp_ucount_decr(asoc->cnt_on_all_streams);
628                                 strm->last_sequence_delivered++;
629                                 /*
630                                  * We ignore the return of deliver_data here
631                                  * since we always can hold the chunk on the
632                                  * d-queue. And we have a finite number that
633                                  * can be delivered from the strq.
634                                  */
635                                 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
636                                         sctp_log_strm_del(control, NULL,
637                                             SCTP_STR_LOG_FROM_IMMED_DEL);
638                                 }
639                                 sctp_add_to_readq(stcb->sctp_ep, stcb,
640                                     control,
641                                     &stcb->sctp_socket->so_rcv, 1);
642                                 control = at;
643                                 continue;
644                         }
645                         break;
646                 }
647         }
648         if (queue_needed) {
649                 /*
650                  * Ok, we did not deliver this guy, find the correct place
651                  * to put it on the queue.
652                  */
653                 if (TAILQ_EMPTY(&strm->inqueue)) {
654                         /* Empty queue */
655                         if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
656                                 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
657                         }
658                         TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
659                 } else {
660                         TAILQ_FOREACH(at, &strm->inqueue, next) {
661                                 if (compare_with_wrap(at->sinfo_ssn,
662                                     control->sinfo_ssn, MAX_SEQ)) {
663                                         /*
664                                          * one in queue is bigger than the
665                                          * new one, insert before this one
666                                          */
667                                         if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
668                                                 sctp_log_strm_del(control, at,
669                                                     SCTP_STR_LOG_FROM_INSERT_MD);
670                                         }
671                                         TAILQ_INSERT_BEFORE(at, control, next);
672                                         break;
673                                 } else if (at->sinfo_ssn == control->sinfo_ssn) {
674                                         /*
675                                          * Gak, He sent me a duplicate str
676                                          * seq number
677                                          */
678                                         /*
679                                          * foo bar, I guess I will just free
680                                          * this new guy, should we abort
681                                          * too? FIX ME MAYBE? Or it COULD be
682                                          * that the SSN's have wrapped.
683                                          * Maybe I should compare to TSN
684                                          * somehow... sigh for now just blow
685                                          * away the chunk!
686                                          */
687
688                                         if (control->data)
689                                                 sctp_m_freem(control->data);
690                                         control->data = NULL;
691                                         asoc->size_on_all_streams -= control->length;
692                                         sctp_ucount_decr(asoc->cnt_on_all_streams);
693                                         sctp_free_remote_addr(control->whoFrom);
694                                         sctp_free_a_readq(stcb, control);
695                                         return;
696                                 } else {
697                                         if (TAILQ_NEXT(at, next) == NULL) {
698                                                 /*
699                                                  * We are at the end, insert
700                                                  * it after this one
701                                                  */
702                                                 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
703                                                         sctp_log_strm_del(control, at,
704                                                             SCTP_STR_LOG_FROM_INSERT_TL);
705                                                 }
706                                                 TAILQ_INSERT_AFTER(&strm->inqueue,
707                                                     at, control, next);
708                                                 break;
709                                         }
710                                 }
711                         }
712                 }
713         }
714 }
715
716 /*
717  * Returns two things: You get the total size of the deliverable parts of the
718  * first fragmented message on the reassembly queue. And you get a 1 back if
719  * all of the message is ready or a 0 back if the message is still incomplete
720  */
721 static int
722 sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
723 {
724         struct sctp_tmit_chunk *chk;
725         uint32_t tsn;
726
727         *t_size = 0;
728         chk = TAILQ_FIRST(&asoc->reasmqueue);
729         if (chk == NULL) {
730                 /* nothing on the queue */
731                 return (0);
732         }
733         if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
734                 /* Not a first on the queue */
735                 return (0);
736         }
737         tsn = chk->rec.data.TSN_seq;
738         while (chk) {
739                 if (tsn != chk->rec.data.TSN_seq) {
740                         return (0);
741                 }
742                 *t_size += chk->send_size;
743                 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
744                         return (1);
745                 }
746                 tsn++;
747                 chk = TAILQ_NEXT(chk, sctp_next);
748         }
749         return (0);
750 }
751
752 static void
753 sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
754 {
755         struct sctp_tmit_chunk *chk;
756         uint16_t nxt_todel;
757         uint32_t tsize;
758
759 doit_again:
760         chk = TAILQ_FIRST(&asoc->reasmqueue);
761         if (chk == NULL) {
762                 /* Huh? */
763                 asoc->size_on_reasm_queue = 0;
764                 asoc->cnt_on_reasm_queue = 0;
765                 return;
766         }
767         if (asoc->fragmented_delivery_inprogress == 0) {
768                 nxt_todel =
769                     asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
770                 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
771                     (nxt_todel == chk->rec.data.stream_seq ||
772                     (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
773                         /*
774                          * Yep the first one is here and its ok to deliver
775                          * but should we?
776                          */
777                         if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
778                             (tsize > stcb->sctp_ep->partial_delivery_point))) {
779
780                                 /*
781                                  * Yes, we setup to start reception, by
782                                  * backing down the TSN just in case we
783                                  * can't deliver. If we
784                                  */
785                                 asoc->fragmented_delivery_inprogress = 1;
786                                 asoc->tsn_last_delivered =
787                                     chk->rec.data.TSN_seq - 1;
788                                 asoc->str_of_pdapi =
789                                     chk->rec.data.stream_number;
790                                 asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
791                                 asoc->pdapi_ppid = chk->rec.data.payloadtype;
792                                 asoc->fragment_flags = chk->rec.data.rcv_flags;
793                                 sctp_service_reassembly(stcb, asoc);
794                         }
795                 }
796         } else {
797                 /*
798                  * Service re-assembly will deliver stream data queued at
799                  * the end of fragmented delivery.. but it wont know to go
800                  * back and call itself again... we do that here with the
801                  * got doit_again
802                  */
803                 sctp_service_reassembly(stcb, asoc);
804                 if (asoc->fragmented_delivery_inprogress == 0) {
805                         /*
806                          * finished our Fragmented delivery, could be more
807                          * waiting?
808                          */
809                         goto doit_again;
810                 }
811         }
812 }
813
814 /*
815  * Dump onto the re-assembly queue, in its proper place. After dumping on the
816  * queue, see if anthing can be delivered. If so pull it off (or as much as
817  * we can. If we run out of space then we must dump what we can and set the
818  * appropriate flag to say we queued what we could.
819  */
820 static void
821 sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
822     struct sctp_tmit_chunk *chk, int *abort_flag)
823 {
824         struct mbuf *oper;
825         uint32_t cum_ackp1, last_tsn, prev_tsn, post_tsn;
826         u_char last_flags;
827         struct sctp_tmit_chunk *at, *prev, *next;
828
829         prev = next = NULL;
830         cum_ackp1 = asoc->tsn_last_delivered + 1;
831         if (TAILQ_EMPTY(&asoc->reasmqueue)) {
832                 /* This is the first one on the queue */
833                 TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
834                 /*
835                  * we do not check for delivery of anything when only one
836                  * fragment is here
837                  */
838                 asoc->size_on_reasm_queue = chk->send_size;
839                 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
840                 if (chk->rec.data.TSN_seq == cum_ackp1) {
841                         if (asoc->fragmented_delivery_inprogress == 0 &&
842                             (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
843                             SCTP_DATA_FIRST_FRAG) {
844                                 /*
845                                  * An empty queue, no delivery inprogress,
846                                  * we hit the next one and it does NOT have
847                                  * a FIRST fragment mark.
848                                  */
849                                 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
850                                 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
851                                     0, M_DONTWAIT, 1, MT_DATA);
852
853                                 if (oper) {
854                                         struct sctp_paramhdr *ph;
855                                         uint32_t *ippp;
856
857                                         SCTP_BUF_LEN(oper) =
858                                             sizeof(struct sctp_paramhdr) +
859                                             (sizeof(uint32_t) * 3);
860                                         ph = mtod(oper, struct sctp_paramhdr *);
861                                         ph->param_type =
862                                             htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
863                                         ph->param_length = htons(SCTP_BUF_LEN(oper));
864                                         ippp = (uint32_t *) (ph + 1);
865                                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_2);
866                                         ippp++;
867                                         *ippp = chk->rec.data.TSN_seq;
868                                         ippp++;
869                                         *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
870
871                                 }
872                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
873                                 sctp_abort_an_association(stcb->sctp_ep, stcb,
874                                     SCTP_PEER_FAULTY, oper);
875                                 *abort_flag = 1;
876                         } else if (asoc->fragmented_delivery_inprogress &&
877                             (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
878                                 /*
879                                  * We are doing a partial delivery and the
880                                  * NEXT chunk MUST be either the LAST or
881                                  * MIDDLE fragment NOT a FIRST
882                                  */
883                                 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
884                                 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
885                                     0, M_DONTWAIT, 1, MT_DATA);
886                                 if (oper) {
887                                         struct sctp_paramhdr *ph;
888                                         uint32_t *ippp;
889
890                                         SCTP_BUF_LEN(oper) =
891                                             sizeof(struct sctp_paramhdr) +
892                                             (3 * sizeof(uint32_t));
893                                         ph = mtod(oper, struct sctp_paramhdr *);
894                                         ph->param_type =
895                                             htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
896                                         ph->param_length = htons(SCTP_BUF_LEN(oper));
897                                         ippp = (uint32_t *) (ph + 1);
898                                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_3);
899                                         ippp++;
900                                         *ippp = chk->rec.data.TSN_seq;
901                                         ippp++;
902                                         *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
903                                 }
904                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
905                                 sctp_abort_an_association(stcb->sctp_ep, stcb,
906                                     SCTP_PEER_FAULTY, oper);
907                                 *abort_flag = 1;
908                         } else if (asoc->fragmented_delivery_inprogress) {
909                                 /*
910                                  * Here we are ok with a MIDDLE or LAST
911                                  * piece
912                                  */
913                                 if (chk->rec.data.stream_number !=
914                                     asoc->str_of_pdapi) {
915                                         /* Got to be the right STR No */
916                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
917                                             chk->rec.data.stream_number,
918                                             asoc->str_of_pdapi);
919                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
920                                             0, M_DONTWAIT, 1, MT_DATA);
921                                         if (oper) {
922                                                 struct sctp_paramhdr *ph;
923                                                 uint32_t *ippp;
924
925                                                 SCTP_BUF_LEN(oper) =
926                                                     sizeof(struct sctp_paramhdr) +
927                                                     (sizeof(uint32_t) * 3);
928                                                 ph = mtod(oper,
929                                                     struct sctp_paramhdr *);
930                                                 ph->param_type =
931                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
932                                                 ph->param_length =
933                                                     htons(SCTP_BUF_LEN(oper));
934                                                 ippp = (uint32_t *) (ph + 1);
935                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
936                                                 ippp++;
937                                                 *ippp = chk->rec.data.TSN_seq;
938                                                 ippp++;
939                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
940                                         }
941                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
942                                         sctp_abort_an_association(stcb->sctp_ep,
943                                             stcb, SCTP_PEER_FAULTY, oper);
944                                         *abort_flag = 1;
945                                 } else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
946                                             SCTP_DATA_UNORDERED &&
947                                             chk->rec.data.stream_seq !=
948                                     asoc->ssn_of_pdapi) {
949                                         /* Got to be the right STR Seq */
950                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
951                                             chk->rec.data.stream_seq,
952                                             asoc->ssn_of_pdapi);
953                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
954                                             0, M_DONTWAIT, 1, MT_DATA);
955                                         if (oper) {
956                                                 struct sctp_paramhdr *ph;
957                                                 uint32_t *ippp;
958
959                                                 SCTP_BUF_LEN(oper) =
960                                                     sizeof(struct sctp_paramhdr) +
961                                                     (3 * sizeof(uint32_t));
962                                                 ph = mtod(oper,
963                                                     struct sctp_paramhdr *);
964                                                 ph->param_type =
965                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
966                                                 ph->param_length =
967                                                     htons(SCTP_BUF_LEN(oper));
968                                                 ippp = (uint32_t *) (ph + 1);
969                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
970                                                 ippp++;
971                                                 *ippp = chk->rec.data.TSN_seq;
972                                                 ippp++;
973                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
974
975                                         }
976                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
977                                         sctp_abort_an_association(stcb->sctp_ep,
978                                             stcb, SCTP_PEER_FAULTY, oper);
979                                         *abort_flag = 1;
980                                 }
981                         }
982                 }
983                 return;
984         }
985         /* Find its place */
986         TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
987                 if (compare_with_wrap(at->rec.data.TSN_seq,
988                     chk->rec.data.TSN_seq, MAX_TSN)) {
989                         /*
990                          * one in queue is bigger than the new one, insert
991                          * before this one
992                          */
993                         /* A check */
994                         asoc->size_on_reasm_queue += chk->send_size;
995                         sctp_ucount_incr(asoc->cnt_on_reasm_queue);
996                         next = at;
997                         TAILQ_INSERT_BEFORE(at, chk, sctp_next);
998                         break;
999                 } else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
1000                         /* Gak, He sent me a duplicate str seq number */
1001                         /*
1002                          * foo bar, I guess I will just free this new guy,
1003                          * should we abort too? FIX ME MAYBE? Or it COULD be
1004                          * that the SSN's have wrapped. Maybe I should
1005                          * compare to TSN somehow... sigh for now just blow
1006                          * away the chunk!
1007                          */
1008                         if (chk->data) {
1009                                 sctp_m_freem(chk->data);
1010                                 chk->data = NULL;
1011                         }
1012                         sctp_free_remote_addr(chk->whoTo);
1013                         sctp_free_a_chunk(stcb, chk);
1014                         return;
1015                 } else {
1016                         last_flags = at->rec.data.rcv_flags;
1017                         last_tsn = at->rec.data.TSN_seq;
1018                         prev = at;
1019                         if (TAILQ_NEXT(at, sctp_next) == NULL) {
1020                                 /*
1021                                  * We are at the end, insert it after this
1022                                  * one
1023                                  */
1024                                 /* check it first */
1025                                 asoc->size_on_reasm_queue += chk->send_size;
1026                                 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1027                                 TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
1028                                 break;
1029                         }
1030                 }
1031         }
1032         /* Now the audits */
1033         if (prev) {
1034                 prev_tsn = chk->rec.data.TSN_seq - 1;
1035                 if (prev_tsn == prev->rec.data.TSN_seq) {
1036                         /*
1037                          * Ok the one I am dropping onto the end is the
1038                          * NEXT. A bit of valdiation here.
1039                          */
1040                         if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1041                             SCTP_DATA_FIRST_FRAG ||
1042                             (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1043                             SCTP_DATA_MIDDLE_FRAG) {
1044                                 /*
1045                                  * Insert chk MUST be a MIDDLE or LAST
1046                                  * fragment
1047                                  */
1048                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1049                                     SCTP_DATA_FIRST_FRAG) {
1050                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
1051                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
1052                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1053                                             0, M_DONTWAIT, 1, MT_DATA);
1054                                         if (oper) {
1055                                                 struct sctp_paramhdr *ph;
1056                                                 uint32_t *ippp;
1057
1058                                                 SCTP_BUF_LEN(oper) =
1059                                                     sizeof(struct sctp_paramhdr) +
1060                                                     (3 * sizeof(uint32_t));
1061                                                 ph = mtod(oper,
1062                                                     struct sctp_paramhdr *);
1063                                                 ph->param_type =
1064                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1065                                                 ph->param_length =
1066                                                     htons(SCTP_BUF_LEN(oper));
1067                                                 ippp = (uint32_t *) (ph + 1);
1068                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1069                                                 ippp++;
1070                                                 *ippp = chk->rec.data.TSN_seq;
1071                                                 ippp++;
1072                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1073
1074                                         }
1075                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
1076                                         sctp_abort_an_association(stcb->sctp_ep,
1077                                             stcb, SCTP_PEER_FAULTY, oper);
1078                                         *abort_flag = 1;
1079                                         return;
1080                                 }
1081                                 if (chk->rec.data.stream_number !=
1082                                     prev->rec.data.stream_number) {
1083                                         /*
1084                                          * Huh, need the correct STR here,
1085                                          * they must be the same.
1086                                          */
1087                                         SCTP_PRINTF("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1088                                             chk->rec.data.stream_number,
1089                                             prev->rec.data.stream_number);
1090                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1091                                             0, M_DONTWAIT, 1, MT_DATA);
1092                                         if (oper) {
1093                                                 struct sctp_paramhdr *ph;
1094                                                 uint32_t *ippp;
1095
1096                                                 SCTP_BUF_LEN(oper) =
1097                                                     sizeof(struct sctp_paramhdr) +
1098                                                     (3 * sizeof(uint32_t));
1099                                                 ph = mtod(oper,
1100                                                     struct sctp_paramhdr *);
1101                                                 ph->param_type =
1102                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1103                                                 ph->param_length =
1104                                                     htons(SCTP_BUF_LEN(oper));
1105                                                 ippp = (uint32_t *) (ph + 1);
1106                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1107                                                 ippp++;
1108                                                 *ippp = chk->rec.data.TSN_seq;
1109                                                 ippp++;
1110                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1111                                         }
1112                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1113                                         sctp_abort_an_association(stcb->sctp_ep,
1114                                             stcb, SCTP_PEER_FAULTY, oper);
1115
1116                                         *abort_flag = 1;
1117                                         return;
1118                                 }
1119                                 if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1120                                     chk->rec.data.stream_seq !=
1121                                     prev->rec.data.stream_seq) {
1122                                         /*
1123                                          * Huh, need the correct STR here,
1124                                          * they must be the same.
1125                                          */
1126                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1127                                             chk->rec.data.stream_seq,
1128                                             prev->rec.data.stream_seq);
1129                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1130                                             0, M_DONTWAIT, 1, MT_DATA);
1131                                         if (oper) {
1132                                                 struct sctp_paramhdr *ph;
1133                                                 uint32_t *ippp;
1134
1135                                                 SCTP_BUF_LEN(oper) =
1136                                                     sizeof(struct sctp_paramhdr) +
1137                                                     (3 * sizeof(uint32_t));
1138                                                 ph = mtod(oper,
1139                                                     struct sctp_paramhdr *);
1140                                                 ph->param_type =
1141                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1142                                                 ph->param_length =
1143                                                     htons(SCTP_BUF_LEN(oper));
1144                                                 ippp = (uint32_t *) (ph + 1);
1145                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1146                                                 ippp++;
1147                                                 *ippp = chk->rec.data.TSN_seq;
1148                                                 ippp++;
1149                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1150                                         }
1151                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1152                                         sctp_abort_an_association(stcb->sctp_ep,
1153                                             stcb, SCTP_PEER_FAULTY, oper);
1154
1155                                         *abort_flag = 1;
1156                                         return;
1157                                 }
1158                         } else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1159                             SCTP_DATA_LAST_FRAG) {
1160                                 /* Insert chk MUST be a FIRST */
1161                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1162                                     SCTP_DATA_FIRST_FRAG) {
1163                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1164                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1165                                             0, M_DONTWAIT, 1, MT_DATA);
1166                                         if (oper) {
1167                                                 struct sctp_paramhdr *ph;
1168                                                 uint32_t *ippp;
1169
1170                                                 SCTP_BUF_LEN(oper) =
1171                                                     sizeof(struct sctp_paramhdr) +
1172                                                     (3 * sizeof(uint32_t));
1173                                                 ph = mtod(oper,
1174                                                     struct sctp_paramhdr *);
1175                                                 ph->param_type =
1176                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1177                                                 ph->param_length =
1178                                                     htons(SCTP_BUF_LEN(oper));
1179                                                 ippp = (uint32_t *) (ph + 1);
1180                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1181                                                 ippp++;
1182                                                 *ippp = chk->rec.data.TSN_seq;
1183                                                 ippp++;
1184                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1185
1186                                         }
1187                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1188                                         sctp_abort_an_association(stcb->sctp_ep,
1189                                             stcb, SCTP_PEER_FAULTY, oper);
1190
1191                                         *abort_flag = 1;
1192                                         return;
1193                                 }
1194                         }
1195                 }
1196         }
1197         if (next) {
1198                 post_tsn = chk->rec.data.TSN_seq + 1;
1199                 if (post_tsn == next->rec.data.TSN_seq) {
1200                         /*
1201                          * Ok the one I am inserting ahead of is my NEXT
1202                          * one. A bit of valdiation here.
1203                          */
1204                         if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1205                                 /* Insert chk MUST be a last fragment */
1206                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1207                                     != SCTP_DATA_LAST_FRAG) {
1208                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1209                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1210                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1211                                             0, M_DONTWAIT, 1, MT_DATA);
1212                                         if (oper) {
1213                                                 struct sctp_paramhdr *ph;
1214                                                 uint32_t *ippp;
1215
1216                                                 SCTP_BUF_LEN(oper) =
1217                                                     sizeof(struct sctp_paramhdr) +
1218                                                     (3 * sizeof(uint32_t));
1219                                                 ph = mtod(oper,
1220                                                     struct sctp_paramhdr *);
1221                                                 ph->param_type =
1222                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1223                                                 ph->param_length =
1224                                                     htons(SCTP_BUF_LEN(oper));
1225                                                 ippp = (uint32_t *) (ph + 1);
1226                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1227                                                 ippp++;
1228                                                 *ippp = chk->rec.data.TSN_seq;
1229                                                 ippp++;
1230                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1231                                         }
1232                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1233                                         sctp_abort_an_association(stcb->sctp_ep,
1234                                             stcb, SCTP_PEER_FAULTY, oper);
1235
1236                                         *abort_flag = 1;
1237                                         return;
1238                                 }
1239                         } else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1240                                     SCTP_DATA_MIDDLE_FRAG ||
1241                                     (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1242                             SCTP_DATA_LAST_FRAG) {
1243                                 /*
1244                                  * Insert chk CAN be MIDDLE or FIRST NOT
1245                                  * LAST
1246                                  */
1247                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1248                                     SCTP_DATA_LAST_FRAG) {
1249                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1250                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1251                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1252                                             0, M_DONTWAIT, 1, MT_DATA);
1253                                         if (oper) {
1254                                                 struct sctp_paramhdr *ph;
1255                                                 uint32_t *ippp;
1256
1257                                                 SCTP_BUF_LEN(oper) =
1258                                                     sizeof(struct sctp_paramhdr) +
1259                                                     (3 * sizeof(uint32_t));
1260                                                 ph = mtod(oper,
1261                                                     struct sctp_paramhdr *);
1262                                                 ph->param_type =
1263                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1264                                                 ph->param_length =
1265                                                     htons(SCTP_BUF_LEN(oper));
1266                                                 ippp = (uint32_t *) (ph + 1);
1267                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1268                                                 ippp++;
1269                                                 *ippp = chk->rec.data.TSN_seq;
1270                                                 ippp++;
1271                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1272
1273                                         }
1274                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1275                                         sctp_abort_an_association(stcb->sctp_ep,
1276                                             stcb, SCTP_PEER_FAULTY, oper);
1277
1278                                         *abort_flag = 1;
1279                                         return;
1280                                 }
1281                                 if (chk->rec.data.stream_number !=
1282                                     next->rec.data.stream_number) {
1283                                         /*
1284                                          * Huh, need the correct STR here,
1285                                          * they must be the same.
1286                                          */
1287                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1288                                             chk->rec.data.stream_number,
1289                                             next->rec.data.stream_number);
1290                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1291                                             0, M_DONTWAIT, 1, MT_DATA);
1292                                         if (oper) {
1293                                                 struct sctp_paramhdr *ph;
1294                                                 uint32_t *ippp;
1295
1296                                                 SCTP_BUF_LEN(oper) =
1297                                                     sizeof(struct sctp_paramhdr) +
1298                                                     (3 * sizeof(uint32_t));
1299                                                 ph = mtod(oper,
1300                                                     struct sctp_paramhdr *);
1301                                                 ph->param_type =
1302                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1303                                                 ph->param_length =
1304                                                     htons(SCTP_BUF_LEN(oper));
1305                                                 ippp = (uint32_t *) (ph + 1);
1306                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1307                                                 ippp++;
1308                                                 *ippp = chk->rec.data.TSN_seq;
1309                                                 ippp++;
1310                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1311
1312                                         }
1313                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1314                                         sctp_abort_an_association(stcb->sctp_ep,
1315                                             stcb, SCTP_PEER_FAULTY, oper);
1316
1317                                         *abort_flag = 1;
1318                                         return;
1319                                 }
1320                                 if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1321                                     chk->rec.data.stream_seq !=
1322                                     next->rec.data.stream_seq) {
1323                                         /*
1324                                          * Huh, need the correct STR here,
1325                                          * they must be the same.
1326                                          */
1327                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1328                                             chk->rec.data.stream_seq,
1329                                             next->rec.data.stream_seq);
1330                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1331                                             0, M_DONTWAIT, 1, MT_DATA);
1332                                         if (oper) {
1333                                                 struct sctp_paramhdr *ph;
1334                                                 uint32_t *ippp;
1335
1336                                                 SCTP_BUF_LEN(oper) =
1337                                                     sizeof(struct sctp_paramhdr) +
1338                                                     (3 * sizeof(uint32_t));
1339                                                 ph = mtod(oper,
1340                                                     struct sctp_paramhdr *);
1341                                                 ph->param_type =
1342                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1343                                                 ph->param_length =
1344                                                     htons(SCTP_BUF_LEN(oper));
1345                                                 ippp = (uint32_t *) (ph + 1);
1346                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1347                                                 ippp++;
1348                                                 *ippp = chk->rec.data.TSN_seq;
1349                                                 ippp++;
1350                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1351                                         }
1352                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1353                                         sctp_abort_an_association(stcb->sctp_ep,
1354                                             stcb, SCTP_PEER_FAULTY, oper);
1355
1356                                         *abort_flag = 1;
1357                                         return;
1358
1359                                 }
1360                         }
1361                 }
1362         }
1363         /* Do we need to do some delivery? check */
1364         sctp_deliver_reasm_check(stcb, asoc);
1365 }
1366
1367 /*
1368  * This is an unfortunate routine. It checks to make sure a evil guy is not
1369  * stuffing us full of bad packet fragments. A broken peer could also do this
1370  * but this is doubtful. It is to bad I must worry about evil crackers sigh
1371  * :< more cycles.
1372  */
1373 static int
1374 sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1375     uint32_t TSN_seq)
1376 {
1377         struct sctp_tmit_chunk *at;
1378         uint32_t tsn_est;
1379
1380         TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1381                 if (compare_with_wrap(TSN_seq,
1382                     at->rec.data.TSN_seq, MAX_TSN)) {
1383                         /* is it one bigger? */
1384                         tsn_est = at->rec.data.TSN_seq + 1;
1385                         if (tsn_est == TSN_seq) {
1386                                 /* yep. It better be a last then */
1387                                 if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1388                                     SCTP_DATA_LAST_FRAG) {
1389                                         /*
1390                                          * Ok this guy belongs next to a guy
1391                                          * that is NOT last, it should be a
1392                                          * middle/last, not a complete
1393                                          * chunk.
1394                                          */
1395                                         return (1);
1396                                 } else {
1397                                         /*
1398                                          * This guy is ok since its a LAST
1399                                          * and the new chunk is a fully
1400                                          * self- contained one.
1401                                          */
1402                                         return (0);
1403                                 }
1404                         }
1405                 } else if (TSN_seq == at->rec.data.TSN_seq) {
1406                         /* Software error since I have a dup? */
1407                         return (1);
1408                 } else {
1409                         /*
1410                          * Ok, 'at' is larger than new chunk but does it
1411                          * need to be right before it.
1412                          */
1413                         tsn_est = TSN_seq + 1;
1414                         if (tsn_est == at->rec.data.TSN_seq) {
1415                                 /* Yep, It better be a first */
1416                                 if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1417                                     SCTP_DATA_FIRST_FRAG) {
1418                                         return (1);
1419                                 } else {
1420                                         return (0);
1421                                 }
1422                         }
1423                 }
1424         }
1425         return (0);
1426 }
1427
1428
1429 static int
1430 sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1431     struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1432     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1433     int *break_flag, int last_chunk)
1434 {
1435         /* Process a data chunk */
1436         /* struct sctp_tmit_chunk *chk; */
1437         struct sctp_tmit_chunk *chk;
1438         uint32_t tsn, gap;
1439         struct mbuf *dmbuf;
1440         int indx, the_len;
1441         int need_reasm_check = 0;
1442         uint16_t strmno, strmseq;
1443         struct mbuf *oper;
1444         struct sctp_queued_to_read *control;
1445         int ordered;
1446         uint32_t protocol_id;
1447         uint8_t chunk_flags;
1448         struct sctp_stream_reset_list *liste;
1449
1450         chk = NULL;
1451         tsn = ntohl(ch->dp.tsn);
1452         chunk_flags = ch->ch.chunk_flags;
1453         protocol_id = ch->dp.protocol_id;
1454         ordered = ((ch->ch.chunk_flags & SCTP_DATA_UNORDERED) == 0);
1455         if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
1456                 sctp_log_map(0, tsn, asoc->cumulative_tsn, SCTP_MAP_PREPARE_SLIDE);
1457         }
1458         if (stcb == NULL) {
1459                 return (0);
1460         }
1461         SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
1462         if (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN) ||
1463             asoc->cumulative_tsn == tsn) {
1464                 /* It is a duplicate */
1465                 SCTP_STAT_INCR(sctps_recvdupdata);
1466                 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1467                         /* Record a dup for the next outbound sack */
1468                         asoc->dup_tsns[asoc->numduptsns] = tsn;
1469                         asoc->numduptsns++;
1470                 }
1471                 return (0);
1472         }
1473         /* Calculate the number of TSN's between the base and this TSN */
1474         if (tsn >= asoc->mapping_array_base_tsn) {
1475                 gap = tsn - asoc->mapping_array_base_tsn;
1476         } else {
1477                 gap = (MAX_TSN - asoc->mapping_array_base_tsn) + tsn + 1;
1478         }
1479         if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1480                 /* Can't hold the bit in the mapping at max array, toss it */
1481                 return (0);
1482         }
1483         if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1484                 SCTP_TCB_LOCK_ASSERT(stcb);
1485                 if (sctp_expand_mapping_array(asoc, gap)) {
1486                         /* Can't expand, drop it */
1487                         return (0);
1488                 }
1489         }
1490         if (compare_with_wrap(tsn, *high_tsn, MAX_TSN)) {
1491                 *high_tsn = tsn;
1492         }
1493         /* See if we have received this one already */
1494         if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
1495                 SCTP_STAT_INCR(sctps_recvdupdata);
1496                 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1497                         /* Record a dup for the next outbound sack */
1498                         asoc->dup_tsns[asoc->numduptsns] = tsn;
1499                         asoc->numduptsns++;
1500                 }
1501                 asoc->send_sack = 1;
1502                 return (0);
1503         }
1504         /*
1505          * Check to see about the GONE flag, duplicates would cause a sack
1506          * to be sent up above
1507          */
1508         if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1509             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1510             (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1511             ) {
1512                 /*
1513                  * wait a minute, this guy is gone, there is no longer a
1514                  * receiver. Send peer an ABORT!
1515                  */
1516                 struct mbuf *op_err;
1517
1518                 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1519                 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err);
1520                 *abort_flag = 1;
1521                 return (0);
1522         }
1523         /*
1524          * Now before going further we see if there is room. If NOT then we
1525          * MAY let one through only IF this TSN is the one we are waiting
1526          * for on a partial delivery API.
1527          */
1528
1529         /* now do the tests */
1530         if (((asoc->cnt_on_all_streams +
1531             asoc->cnt_on_reasm_queue +
1532             asoc->cnt_msg_on_sb) > sctp_max_chunks_on_queue) ||
1533             (((int)asoc->my_rwnd) <= 0)) {
1534                 /*
1535                  * When we have NO room in the rwnd we check to make sure
1536                  * the reader is doing its job...
1537                  */
1538                 if (stcb->sctp_socket->so_rcv.sb_cc) {
1539                         /* some to read, wake-up */
1540                         sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1541                 }
1542                 /* now is it in the mapping array of what we have accepted? */
1543                 if (compare_with_wrap(tsn,
1544                     asoc->highest_tsn_inside_map, MAX_TSN)) {
1545
1546                         /* Nope not in the valid range dump it */
1547                         SCTPDBG(SCTP_DEBUG_INDATA1, "My rwnd overrun1:tsn:%lx rwnd %lu sbspace:%ld\n",
1548                             (u_long)tsn, (u_long)asoc->my_rwnd,
1549                             sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv));
1550                         sctp_set_rwnd(stcb, asoc);
1551                         if ((asoc->cnt_on_all_streams +
1552                             asoc->cnt_on_reasm_queue +
1553                             asoc->cnt_msg_on_sb) > sctp_max_chunks_on_queue) {
1554                                 SCTP_STAT_INCR(sctps_datadropchklmt);
1555                         } else {
1556                                 SCTP_STAT_INCR(sctps_datadroprwnd);
1557                         }
1558                         indx = *break_flag;
1559                         *break_flag = 1;
1560                         return (0);
1561                 }
1562         }
1563         strmno = ntohs(ch->dp.stream_id);
1564         if (strmno >= asoc->streamincnt) {
1565                 struct sctp_paramhdr *phdr;
1566                 struct mbuf *mb;
1567
1568                 mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1569                     0, M_DONTWAIT, 1, MT_DATA);
1570                 if (mb != NULL) {
1571                         /* add some space up front so prepend will work well */
1572                         SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1573                         phdr = mtod(mb, struct sctp_paramhdr *);
1574                         /*
1575                          * Error causes are just param's and this one has
1576                          * two back to back phdr, one with the error type
1577                          * and size, the other with the streamid and a rsvd
1578                          */
1579                         SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1580                         phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1581                         phdr->param_length =
1582                             htons(sizeof(struct sctp_paramhdr) * 2);
1583                         phdr++;
1584                         /* We insert the stream in the type field */
1585                         phdr->param_type = ch->dp.stream_id;
1586                         /* And set the length to 0 for the rsvd field */
1587                         phdr->param_length = 0;
1588                         sctp_queue_op_err(stcb, mb);
1589                 }
1590                 SCTP_STAT_INCR(sctps_badsid);
1591                 SCTP_TCB_LOCK_ASSERT(stcb);
1592                 SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
1593                 if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1594                         /* we have a new high score */
1595                         asoc->highest_tsn_inside_map = tsn;
1596                         if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
1597                                 sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
1598                         }
1599                 }
1600                 if (tsn == (asoc->cumulative_tsn + 1)) {
1601                         /* Update cum-ack */
1602                         asoc->cumulative_tsn = tsn;
1603                 }
1604                 return (0);
1605         }
1606         /*
1607          * Before we continue lets validate that we are not being fooled by
1608          * an evil attacker. We can only have 4k chunks based on our TSN
1609          * spread allowed by the mapping array 512 * 8 bits, so there is no
1610          * way our stream sequence numbers could have wrapped. We of course
1611          * only validate the FIRST fragment so the bit must be set.
1612          */
1613         strmseq = ntohs(ch->dp.stream_sequence);
1614 #ifdef SCTP_ASOCLOG_OF_TSNS
1615         asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1616         asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1617         asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1618         asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1619         asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
1620         asoc->tsn_in_at++;
1621         if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
1622                 asoc->tsn_in_at = 0;
1623                 asoc->tsn_in_wrapped = 1;
1624         }
1625 #endif
1626         if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1627             (TAILQ_EMPTY(&asoc->resetHead)) &&
1628             (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
1629             (compare_with_wrap(asoc->strmin[strmno].last_sequence_delivered,
1630             strmseq, MAX_SEQ) ||
1631             asoc->strmin[strmno].last_sequence_delivered == strmseq)) {
1632                 /* The incoming sseq is behind where we last delivered? */
1633                 SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1634                     strmseq, asoc->strmin[strmno].last_sequence_delivered);
1635                 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1636                     0, M_DONTWAIT, 1, MT_DATA);
1637                 if (oper) {
1638                         struct sctp_paramhdr *ph;
1639                         uint32_t *ippp;
1640
1641                         SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1642                             (3 * sizeof(uint32_t));
1643                         ph = mtod(oper, struct sctp_paramhdr *);
1644                         ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1645                         ph->param_length = htons(SCTP_BUF_LEN(oper));
1646                         ippp = (uint32_t *) (ph + 1);
1647                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1648                         ippp++;
1649                         *ippp = tsn;
1650                         ippp++;
1651                         *ippp = ((strmno << 16) | strmseq);
1652
1653                 }
1654                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1655                 sctp_abort_an_association(stcb->sctp_ep, stcb,
1656                     SCTP_PEER_FAULTY, oper);
1657                 *abort_flag = 1;
1658                 return (0);
1659         }
1660         /************************************
1661          * From here down we may find ch-> invalid
1662          * so its a good idea NOT to use it.
1663          *************************************/
1664
1665         the_len = (chk_length - sizeof(struct sctp_data_chunk));
1666         if (last_chunk == 0) {
1667                 dmbuf = SCTP_M_COPYM(*m,
1668                     (offset + sizeof(struct sctp_data_chunk)),
1669                     the_len, M_DONTWAIT);
1670 #ifdef SCTP_MBUF_LOGGING
1671                 if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) {
1672                         struct mbuf *mat;
1673
1674                         mat = dmbuf;
1675                         while (mat) {
1676                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
1677                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1678                                 }
1679                                 mat = SCTP_BUF_NEXT(mat);
1680                         }
1681                 }
1682 #endif
1683         } else {
1684                 /* We can steal the last chunk */
1685                 int l_len;
1686
1687                 dmbuf = *m;
1688                 /* lop off the top part */
1689                 m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1690                 if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1691                         l_len = SCTP_BUF_LEN(dmbuf);
1692                 } else {
1693                         /*
1694                          * need to count up the size hopefully does not hit
1695                          * this to often :-0
1696                          */
1697                         struct mbuf *lat;
1698
1699                         l_len = 0;
1700                         lat = dmbuf;
1701                         while (lat) {
1702                                 l_len += SCTP_BUF_LEN(lat);
1703                                 lat = SCTP_BUF_NEXT(lat);
1704                         }
1705                 }
1706                 if (l_len > the_len) {
1707                         /* Trim the end round bytes off  too */
1708                         m_adj(dmbuf, -(l_len - the_len));
1709                 }
1710         }
1711         if (dmbuf == NULL) {
1712                 SCTP_STAT_INCR(sctps_nomem);
1713                 return (0);
1714         }
1715         if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1716             asoc->fragmented_delivery_inprogress == 0 &&
1717             TAILQ_EMPTY(&asoc->resetHead) &&
1718             ((ordered == 0) ||
1719             ((asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1720             TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1721                 /* Candidate for express delivery */
1722                 /*
1723                  * Its not fragmented, No PD-API is up, Nothing in the
1724                  * delivery queue, Its un-ordered OR ordered and the next to
1725                  * deliver AND nothing else is stuck on the stream queue,
1726                  * And there is room for it in the socket buffer. Lets just
1727                  * stuff it up the buffer....
1728                  */
1729
1730                 /* It would be nice to avoid this copy if we could :< */
1731                 sctp_alloc_a_readq(stcb, control);
1732                 sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1733                     protocol_id,
1734                     stcb->asoc.context,
1735                     strmno, strmseq,
1736                     chunk_flags,
1737                     dmbuf);
1738                 if (control == NULL) {
1739                         goto failed_express_del;
1740                 }
1741                 sctp_add_to_readq(stcb->sctp_ep, stcb, control, &stcb->sctp_socket->so_rcv, 1);
1742                 if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1743                         /* for ordered, bump what we delivered */
1744                         asoc->strmin[strmno].last_sequence_delivered++;
1745                 }
1746                 SCTP_STAT_INCR(sctps_recvexpress);
1747                 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
1748                         sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1749                             SCTP_STR_LOG_FROM_EXPRS_DEL);
1750                 }
1751                 control = NULL;
1752                 goto finish_express_del;
1753         }
1754 failed_express_del:
1755         /* If we reach here this is a new chunk */
1756         chk = NULL;
1757         control = NULL;
1758         /* Express for fragmented delivery? */
1759         if ((asoc->fragmented_delivery_inprogress) &&
1760             (stcb->asoc.control_pdapi) &&
1761             (asoc->str_of_pdapi == strmno) &&
1762             (asoc->ssn_of_pdapi == strmseq)
1763             ) {
1764                 control = stcb->asoc.control_pdapi;
1765                 if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1766                         /* Can't be another first? */
1767                         goto failed_pdapi_express_del;
1768                 }
1769                 if (tsn == (control->sinfo_tsn + 1)) {
1770                         /* Yep, we can add it on */
1771                         int end = 0;
1772                         uint32_t cumack;
1773
1774                         if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1775                                 end = 1;
1776                         }
1777                         cumack = asoc->cumulative_tsn;
1778                         if ((cumack + 1) == tsn)
1779                                 cumack = tsn;
1780
1781                         if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1782                             tsn,
1783                             &stcb->sctp_socket->so_rcv)) {
1784                                 SCTP_PRINTF("Append fails end:%d\n", end);
1785                                 goto failed_pdapi_express_del;
1786                         }
1787                         SCTP_STAT_INCR(sctps_recvexpressm);
1788                         control->sinfo_tsn = tsn;
1789                         asoc->tsn_last_delivered = tsn;
1790                         asoc->fragment_flags = chunk_flags;
1791                         asoc->tsn_of_pdapi_last_delivered = tsn;
1792                         asoc->last_flags_delivered = chunk_flags;
1793                         asoc->last_strm_seq_delivered = strmseq;
1794                         asoc->last_strm_no_delivered = strmno;
1795                         if (end) {
1796                                 /* clean up the flags and such */
1797                                 asoc->fragmented_delivery_inprogress = 0;
1798                                 if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1799                                         asoc->strmin[strmno].last_sequence_delivered++;
1800                                 }
1801                                 stcb->asoc.control_pdapi = NULL;
1802                                 if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
1803                                         /*
1804                                          * There could be another message
1805                                          * ready
1806                                          */
1807                                         need_reasm_check = 1;
1808                                 }
1809                         }
1810                         control = NULL;
1811                         goto finish_express_del;
1812                 }
1813         }
1814 failed_pdapi_express_del:
1815         control = NULL;
1816         if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1817                 sctp_alloc_a_chunk(stcb, chk);
1818                 if (chk == NULL) {
1819                         /* No memory so we drop the chunk */
1820                         SCTP_STAT_INCR(sctps_nomem);
1821                         if (last_chunk == 0) {
1822                                 /* we copied it, free the copy */
1823                                 sctp_m_freem(dmbuf);
1824                         }
1825                         return (0);
1826                 }
1827                 chk->rec.data.TSN_seq = tsn;
1828                 chk->no_fr_allowed = 0;
1829                 chk->rec.data.stream_seq = strmseq;
1830                 chk->rec.data.stream_number = strmno;
1831                 chk->rec.data.payloadtype = protocol_id;
1832                 chk->rec.data.context = stcb->asoc.context;
1833                 chk->rec.data.doing_fast_retransmit = 0;
1834                 chk->rec.data.rcv_flags = chunk_flags;
1835                 chk->asoc = asoc;
1836                 chk->send_size = the_len;
1837                 chk->whoTo = net;
1838                 atomic_add_int(&net->ref_count, 1);
1839                 chk->data = dmbuf;
1840         } else {
1841                 sctp_alloc_a_readq(stcb, control);
1842                 sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1843                     protocol_id,
1844                     stcb->asoc.context,
1845                     strmno, strmseq,
1846                     chunk_flags,
1847                     dmbuf);
1848                 if (control == NULL) {
1849                         /* No memory so we drop the chunk */
1850                         SCTP_STAT_INCR(sctps_nomem);
1851                         if (last_chunk == 0) {
1852                                 /* we copied it, free the copy */
1853                                 sctp_m_freem(dmbuf);
1854                         }
1855                         return (0);
1856                 }
1857                 control->length = the_len;
1858         }
1859
1860         /* Mark it as received */
1861         /* Now queue it where it belongs */
1862         if (control != NULL) {
1863                 /* First a sanity check */
1864                 if (asoc->fragmented_delivery_inprogress) {
1865                         /*
1866                          * Ok, we have a fragmented delivery in progress if
1867                          * this chunk is next to deliver OR belongs in our
1868                          * view to the reassembly, the peer is evil or
1869                          * broken.
1870                          */
1871                         uint32_t estimate_tsn;
1872
1873                         estimate_tsn = asoc->tsn_last_delivered + 1;
1874                         if (TAILQ_EMPTY(&asoc->reasmqueue) &&
1875                             (estimate_tsn == control->sinfo_tsn)) {
1876                                 /* Evil/Broke peer */
1877                                 sctp_m_freem(control->data);
1878                                 control->data = NULL;
1879                                 sctp_free_remote_addr(control->whoFrom);
1880                                 sctp_free_a_readq(stcb, control);
1881                                 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1882                                     0, M_DONTWAIT, 1, MT_DATA);
1883                                 if (oper) {
1884                                         struct sctp_paramhdr *ph;
1885                                         uint32_t *ippp;
1886
1887                                         SCTP_BUF_LEN(oper) =
1888                                             sizeof(struct sctp_paramhdr) +
1889                                             (3 * sizeof(uint32_t));
1890                                         ph = mtod(oper, struct sctp_paramhdr *);
1891                                         ph->param_type =
1892                                             htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1893                                         ph->param_length = htons(SCTP_BUF_LEN(oper));
1894                                         ippp = (uint32_t *) (ph + 1);
1895                                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
1896                                         ippp++;
1897                                         *ippp = tsn;
1898                                         ippp++;
1899                                         *ippp = ((strmno << 16) | strmseq);
1900                                 }
1901                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
1902                                 sctp_abort_an_association(stcb->sctp_ep, stcb,
1903                                     SCTP_PEER_FAULTY, oper);
1904
1905                                 *abort_flag = 1;
1906                                 return (0);
1907                         } else {
1908                                 if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1909                                         sctp_m_freem(control->data);
1910                                         control->data = NULL;
1911                                         sctp_free_remote_addr(control->whoFrom);
1912                                         sctp_free_a_readq(stcb, control);
1913
1914                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1915                                             0, M_DONTWAIT, 1, MT_DATA);
1916                                         if (oper) {
1917                                                 struct sctp_paramhdr *ph;
1918                                                 uint32_t *ippp;
1919
1920                                                 SCTP_BUF_LEN(oper) =
1921                                                     sizeof(struct sctp_paramhdr) +
1922                                                     (3 * sizeof(uint32_t));
1923                                                 ph = mtod(oper,
1924                                                     struct sctp_paramhdr *);
1925                                                 ph->param_type =
1926                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1927                                                 ph->param_length =
1928                                                     htons(SCTP_BUF_LEN(oper));
1929                                                 ippp = (uint32_t *) (ph + 1);
1930                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
1931                                                 ippp++;
1932                                                 *ippp = tsn;
1933                                                 ippp++;
1934                                                 *ippp = ((strmno << 16) | strmseq);
1935                                         }
1936                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1937                                         sctp_abort_an_association(stcb->sctp_ep,
1938                                             stcb, SCTP_PEER_FAULTY, oper);
1939
1940                                         *abort_flag = 1;
1941                                         return (0);
1942                                 }
1943                         }
1944                 } else {
1945                         /* No PDAPI running */
1946                         if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
1947                                 /*
1948                                  * Reassembly queue is NOT empty validate
1949                                  * that this tsn does not need to be in
1950                                  * reasembly queue. If it does then our peer
1951                                  * is broken or evil.
1952                                  */
1953                                 if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1954                                         sctp_m_freem(control->data);
1955                                         control->data = NULL;
1956                                         sctp_free_remote_addr(control->whoFrom);
1957                                         sctp_free_a_readq(stcb, control);
1958                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1959                                             0, M_DONTWAIT, 1, MT_DATA);
1960                                         if (oper) {
1961                                                 struct sctp_paramhdr *ph;
1962                                                 uint32_t *ippp;
1963
1964                                                 SCTP_BUF_LEN(oper) =
1965                                                     sizeof(struct sctp_paramhdr) +
1966                                                     (3 * sizeof(uint32_t));
1967                                                 ph = mtod(oper,
1968                                                     struct sctp_paramhdr *);
1969                                                 ph->param_type =
1970                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1971                                                 ph->param_length =
1972                                                     htons(SCTP_BUF_LEN(oper));
1973                                                 ippp = (uint32_t *) (ph + 1);
1974                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
1975                                                 ippp++;
1976                                                 *ippp = tsn;
1977                                                 ippp++;
1978                                                 *ippp = ((strmno << 16) | strmseq);
1979                                         }
1980                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
1981                                         sctp_abort_an_association(stcb->sctp_ep,
1982                                             stcb, SCTP_PEER_FAULTY, oper);
1983
1984                                         *abort_flag = 1;
1985                                         return (0);
1986                                 }
1987                         }
1988                 }
1989                 /* ok, if we reach here we have passed the sanity checks */
1990                 if (chunk_flags & SCTP_DATA_UNORDERED) {
1991                         /* queue directly into socket buffer */
1992                         sctp_add_to_readq(stcb->sctp_ep, stcb,
1993                             control,
1994                             &stcb->sctp_socket->so_rcv, 1);
1995                 } else {
1996                         /*
1997                          * Special check for when streams are resetting. We
1998                          * could be more smart about this and check the
1999                          * actual stream to see if it is not being reset..
2000                          * that way we would not create a HOLB when amongst
2001                          * streams being reset and those not being reset.
2002                          * 
2003                          * We take complete messages that have a stream reset
2004                          * intervening (aka the TSN is after where our
2005                          * cum-ack needs to be) off and put them on a
2006                          * pending_reply_queue. The reassembly ones we do
2007                          * not have to worry about since they are all sorted
2008                          * and proceessed by TSN order. It is only the
2009                          * singletons I must worry about.
2010                          */
2011                         if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2012                             ((compare_with_wrap(tsn, liste->tsn, MAX_TSN)))
2013                             ) {
2014                                 /*
2015                                  * yep its past where we need to reset... go
2016                                  * ahead and queue it.
2017                                  */
2018                                 if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2019                                         /* first one on */
2020                                         TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2021                                 } else {
2022                                         struct sctp_queued_to_read *ctlOn;
2023                                         unsigned char inserted = 0;
2024
2025                                         ctlOn = TAILQ_FIRST(&asoc->pending_reply_queue);
2026                                         while (ctlOn) {
2027                                                 if (compare_with_wrap(control->sinfo_tsn,
2028                                                     ctlOn->sinfo_tsn, MAX_TSN)) {
2029                                                         ctlOn = TAILQ_NEXT(ctlOn, next);
2030                                                 } else {
2031                                                         /* found it */
2032                                                         TAILQ_INSERT_BEFORE(ctlOn, control, next);
2033                                                         inserted = 1;
2034                                                         break;
2035                                                 }
2036                                         }
2037                                         if (inserted == 0) {
2038                                                 /*
2039                                                  * must be put at end, use
2040                                                  * prevP (all setup from
2041                                                  * loop) to setup nextP.
2042                                                  */
2043                                                 TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2044                                         }
2045                                 }
2046                         } else {
2047                                 sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2048                                 if (*abort_flag) {
2049                                         return (0);
2050                                 }
2051                         }
2052                 }
2053         } else {
2054                 /* Into the re-assembly queue */
2055                 sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2056                 if (*abort_flag) {
2057                         /*
2058                          * the assoc is now gone and chk was put onto the
2059                          * reasm queue, which has all been freed.
2060                          */
2061                         *m = NULL;
2062                         return (0);
2063                 }
2064         }
2065 finish_express_del:
2066         if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
2067                 /* we have a new high score */
2068                 asoc->highest_tsn_inside_map = tsn;
2069                 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2070                         sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2071                 }
2072         }
2073         if (tsn == (asoc->cumulative_tsn + 1)) {
2074                 /* Update cum-ack */
2075                 asoc->cumulative_tsn = tsn;
2076         }
2077         if (last_chunk) {
2078                 *m = NULL;
2079         }
2080         if (ordered) {
2081                 SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2082         } else {
2083                 SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2084         }
2085         SCTP_STAT_INCR(sctps_recvdata);
2086         /* Set it present please */
2087         if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
2088                 sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
2089         }
2090         if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2091                 sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2092                     asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
2093         }
2094         SCTP_TCB_LOCK_ASSERT(stcb);
2095         SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
2096         /* check the special flag for stream resets */
2097         if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2098             ((compare_with_wrap(asoc->cumulative_tsn, liste->tsn, MAX_TSN)) ||
2099             (asoc->cumulative_tsn == liste->tsn))
2100             ) {
2101                 /*
2102                  * we have finished working through the backlogged TSN's now
2103                  * time to reset streams. 1: call reset function. 2: free
2104                  * pending_reply space 3: distribute any chunks in
2105                  * pending_reply_queue.
2106                  */
2107                 struct sctp_queued_to_read *ctl;
2108
2109                 sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
2110                 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2111                 SCTP_FREE(liste, SCTP_M_STRESET);
2112                 /* sa_ignore FREED_MEMORY */
2113                 liste = TAILQ_FIRST(&asoc->resetHead);
2114                 ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2115                 if (ctl && (liste == NULL)) {
2116                         /* All can be removed */
2117                         while (ctl) {
2118                                 TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2119                                 sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2120                                 if (*abort_flag) {
2121                                         return (0);
2122                                 }
2123                                 ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2124                         }
2125                 } else if (ctl) {
2126                         /* more than one in queue */
2127                         while (!compare_with_wrap(ctl->sinfo_tsn, liste->tsn, MAX_TSN)) {
2128                                 /*
2129                                  * if ctl->sinfo_tsn is <= liste->tsn we can
2130                                  * process it which is the NOT of
2131                                  * ctl->sinfo_tsn > liste->tsn
2132                                  */
2133                                 TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2134                                 sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2135                                 if (*abort_flag) {
2136                                         return (0);
2137                                 }
2138                                 ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2139                         }
2140                 }
2141                 /*
2142                  * Now service re-assembly to pick up anything that has been
2143                  * held on reassembly queue?
2144                  */
2145                 sctp_deliver_reasm_check(stcb, asoc);
2146                 need_reasm_check = 0;
2147         }
2148         if (need_reasm_check) {
2149                 /* Another one waits ? */
2150                 sctp_deliver_reasm_check(stcb, asoc);
2151         }
2152         return (1);
2153 }
2154
2155 int8_t sctp_map_lookup_tab[256] = {
2156         -1, 0, -1, 1, -1, 0, -1, 2,
2157         -1, 0, -1, 1, -1, 0, -1, 3,
2158         -1, 0, -1, 1, -1, 0, -1, 2,
2159         -1, 0, -1, 1, -1, 0, -1, 4,
2160         -1, 0, -1, 1, -1, 0, -1, 2,
2161         -1, 0, -1, 1, -1, 0, -1, 3,
2162         -1, 0, -1, 1, -1, 0, -1, 2,
2163         -1, 0, -1, 1, -1, 0, -1, 5,
2164         -1, 0, -1, 1, -1, 0, -1, 2,
2165         -1, 0, -1, 1, -1, 0, -1, 3,
2166         -1, 0, -1, 1, -1, 0, -1, 2,
2167         -1, 0, -1, 1, -1, 0, -1, 4,
2168         -1, 0, -1, 1, -1, 0, -1, 2,
2169         -1, 0, -1, 1, -1, 0, -1, 3,
2170         -1, 0, -1, 1, -1, 0, -1, 2,
2171         -1, 0, -1, 1, -1, 0, -1, 6,
2172         -1, 0, -1, 1, -1, 0, -1, 2,
2173         -1, 0, -1, 1, -1, 0, -1, 3,
2174         -1, 0, -1, 1, -1, 0, -1, 2,
2175         -1, 0, -1, 1, -1, 0, -1, 4,
2176         -1, 0, -1, 1, -1, 0, -1, 2,
2177         -1, 0, -1, 1, -1, 0, -1, 3,
2178         -1, 0, -1, 1, -1, 0, -1, 2,
2179         -1, 0, -1, 1, -1, 0, -1, 5,
2180         -1, 0, -1, 1, -1, 0, -1, 2,
2181         -1, 0, -1, 1, -1, 0, -1, 3,
2182         -1, 0, -1, 1, -1, 0, -1, 2,
2183         -1, 0, -1, 1, -1, 0, -1, 4,
2184         -1, 0, -1, 1, -1, 0, -1, 2,
2185         -1, 0, -1, 1, -1, 0, -1, 3,
2186         -1, 0, -1, 1, -1, 0, -1, 2,
2187         -1, 0, -1, 1, -1, 0, -1, 7,
2188 };
2189
2190
2191 void
2192 sctp_sack_check(struct sctp_tcb *stcb, int ok_to_sack, int was_a_gap, int *abort_flag)
2193 {
2194         /*
2195          * Now we also need to check the mapping array in a couple of ways.
2196          * 1) Did we move the cum-ack point?
2197          */
2198         struct sctp_association *asoc;
2199         int i, at;
2200         int all_ones, last_all_ones = 0;
2201         int slide_from, slide_end, lgap, distance;
2202         uint32_t old_cumack, old_base, old_highest;
2203         unsigned char aux_array[64];
2204
2205
2206         asoc = &stcb->asoc;
2207         at = 0;
2208
2209         old_cumack = asoc->cumulative_tsn;
2210         old_base = asoc->mapping_array_base_tsn;
2211         old_highest = asoc->highest_tsn_inside_map;
2212         if (asoc->mapping_array_size < 64)
2213                 memcpy(aux_array, asoc->mapping_array,
2214                     asoc->mapping_array_size);
2215         else
2216                 memcpy(aux_array, asoc->mapping_array, 64);
2217
2218         /*
2219          * We could probably improve this a small bit by calculating the
2220          * offset of the current cum-ack as the starting point.
2221          */
2222         all_ones = 1;
2223         at = 0;
2224         for (i = 0; i < stcb->asoc.mapping_array_size; i++) {
2225
2226                 if (asoc->mapping_array[i] == 0xff) {
2227                         at += 8;
2228                         last_all_ones = 1;
2229                 } else {
2230                         /* there is a 0 bit */
2231                         all_ones = 0;
2232                         at += sctp_map_lookup_tab[asoc->mapping_array[i]];
2233                         last_all_ones = 0;
2234                         break;
2235                 }
2236         }
2237         asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - last_all_ones);
2238         /* at is one off, since in the table a embedded -1 is present */
2239         at++;
2240
2241         if (compare_with_wrap(asoc->cumulative_tsn,
2242             asoc->highest_tsn_inside_map,
2243             MAX_TSN)) {
2244 #ifdef INVARIANTS
2245                 panic("huh, cumack greater than high-tsn in map");
2246 #else
2247                 SCTP_PRINTF("huh, cumack greater than high-tsn in map - should panic?\n");
2248                 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2249 #endif
2250         }
2251         if (all_ones ||
2252             (asoc->cumulative_tsn == asoc->highest_tsn_inside_map && at >= 8)) {
2253                 /* The complete array was completed by a single FR */
2254                 /* higest becomes the cum-ack */
2255                 int clr;
2256
2257                 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
2258                 /* clear the array */
2259                 if (all_ones)
2260                         clr = asoc->mapping_array_size;
2261                 else {
2262                         clr = (at >> 3) + 1;
2263                         /*
2264                          * this should be the allones case but just in case
2265                          * :>
2266                          */
2267                         if (clr > asoc->mapping_array_size)
2268                                 clr = asoc->mapping_array_size;
2269                 }
2270                 memset(asoc->mapping_array, 0, clr);
2271                 /* base becomes one ahead of the cum-ack */
2272                 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2273                 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2274                         sctp_log_map(old_base, old_cumack, old_highest,
2275                             SCTP_MAP_PREPARE_SLIDE);
2276                         sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2277                             asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_CLEARED);
2278                 }
2279         } else if (at >= 8) {
2280                 /* we can slide the mapping array down */
2281                 /* Calculate the new byte postion we can move down */
2282                 slide_from = at >> 3;
2283                 /*
2284                  * now calculate the ceiling of the move using our highest
2285                  * TSN value
2286                  */
2287                 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
2288                         lgap = asoc->highest_tsn_inside_map -
2289                             asoc->mapping_array_base_tsn;
2290                 } else {
2291                         lgap = (MAX_TSN - asoc->mapping_array_base_tsn) +
2292                             asoc->highest_tsn_inside_map + 1;
2293                 }
2294                 slide_end = lgap >> 3;
2295                 if (slide_end < slide_from) {
2296                         panic("impossible slide");
2297                 }
2298                 distance = (slide_end - slide_from) + 1;
2299                 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2300                         sctp_log_map(old_base, old_cumack, old_highest,
2301                             SCTP_MAP_PREPARE_SLIDE);
2302                         sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2303                             (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
2304                 }
2305                 if (distance + slide_from > asoc->mapping_array_size ||
2306                     distance < 0) {
2307                         /*
2308                          * Here we do NOT slide forward the array so that
2309                          * hopefully when more data comes in to fill it up
2310                          * we will be able to slide it forward. Really I
2311                          * don't think this should happen :-0
2312                          */
2313
2314                         if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2315                                 sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2316                                     (uint32_t) asoc->mapping_array_size,
2317                                     SCTP_MAP_SLIDE_NONE);
2318                         }
2319                 } else {
2320                         int ii;
2321
2322                         for (ii = 0; ii < distance; ii++) {
2323                                 asoc->mapping_array[ii] =
2324                                     asoc->mapping_array[slide_from + ii];
2325                         }
2326                         for (ii = distance; ii <= slide_end; ii++) {
2327                                 asoc->mapping_array[ii] = 0;
2328                         }
2329                         asoc->mapping_array_base_tsn += (slide_from << 3);
2330                         if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2331                                 sctp_log_map(asoc->mapping_array_base_tsn,
2332                                     asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2333                                     SCTP_MAP_SLIDE_RESULT);
2334                         }
2335                 }
2336         }
2337         /*
2338          * Now we need to see if we need to queue a sack or just start the
2339          * timer (if allowed).
2340          */
2341         if (ok_to_sack) {
2342                 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2343                         /*
2344                          * Ok special case, in SHUTDOWN-SENT case. here we
2345                          * maker sure SACK timer is off and instead send a
2346                          * SHUTDOWN and a SACK
2347                          */
2348                         if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2349                                 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2350                                     stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2351                         }
2352                         sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
2353                         sctp_send_sack(stcb);
2354                 } else {
2355                         int is_a_gap;
2356
2357                         /* is there a gap now ? */
2358                         is_a_gap = compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2359                             stcb->asoc.cumulative_tsn, MAX_TSN);
2360
2361                         /*
2362                          * CMT DAC algorithm: increase number of packets
2363                          * received since last ack
2364                          */
2365                         stcb->asoc.cmt_dac_pkts_rcvd++;
2366
2367                         if ((stcb->asoc.send_sack == 1) ||      /* We need to send a
2368                                                                  * SACK */
2369                             ((was_a_gap) && (is_a_gap == 0)) || /* was a gap, but no
2370                                                                  * longer is one */
2371                             (stcb->asoc.numduptsns) ||  /* we have dup's */
2372                             (is_a_gap) ||       /* is still a gap */
2373                             (stcb->asoc.delayed_ack == 0) ||    /* Delayed sack disabled */
2374                             (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) /* hit limit of pkts */
2375                             ) {
2376
2377                                 if ((sctp_cmt_on_off) && (sctp_cmt_use_dac) &&
2378                                     (stcb->asoc.send_sack == 0) &&
2379                                     (stcb->asoc.numduptsns == 0) &&
2380                                     (stcb->asoc.delayed_ack) &&
2381                                     (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2382
2383                                         /*
2384                                          * CMT DAC algorithm: With CMT,
2385                                          * delay acks even in the face of
2386                                          * 
2387                                          * reordering. Therefore, if acks that
2388                                          * do not have to be sent because of
2389                                          * the above reasons, will be
2390                                          * delayed. That is, acks that would
2391                                          * have been sent due to gap reports
2392                                          * will be delayed with DAC. Start
2393                                          * the delayed ack timer.
2394                                          */
2395                                         sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2396                                             stcb->sctp_ep, stcb, NULL);
2397                                 } else {
2398                                         /*
2399                                          * Ok we must build a SACK since the
2400                                          * timer is pending, we got our
2401                                          * first packet OR there are gaps or
2402                                          * duplicates.
2403                                          */
2404                                         (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2405                                         sctp_send_sack(stcb);
2406                                 }
2407                         } else {
2408                                 if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2409                                         sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2410                                             stcb->sctp_ep, stcb, NULL);
2411                                 }
2412                         }
2413                 }
2414         }
2415 }
2416
2417 void
2418 sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2419 {
2420         struct sctp_tmit_chunk *chk;
2421         uint32_t tsize;
2422         uint16_t nxt_todel;
2423
2424         if (asoc->fragmented_delivery_inprogress) {
2425                 sctp_service_reassembly(stcb, asoc);
2426         }
2427         /* Can we proceed further, i.e. the PD-API is complete */
2428         if (asoc->fragmented_delivery_inprogress) {
2429                 /* no */
2430                 return;
2431         }
2432         /*
2433          * Now is there some other chunk I can deliver from the reassembly
2434          * queue.
2435          */
2436 doit_again:
2437         chk = TAILQ_FIRST(&asoc->reasmqueue);
2438         if (chk == NULL) {
2439                 asoc->size_on_reasm_queue = 0;
2440                 asoc->cnt_on_reasm_queue = 0;
2441                 return;
2442         }
2443         nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2444         if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2445             ((nxt_todel == chk->rec.data.stream_seq) ||
2446             (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2447                 /*
2448                  * Yep the first one is here. We setup to start reception,
2449                  * by backing down the TSN just in case we can't deliver.
2450                  */
2451
2452                 /*
2453                  * Before we start though either all of the message should
2454                  * be here or 1/4 the socket buffer max or nothing on the
2455                  * delivery queue and something can be delivered.
2456                  */
2457                 if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
2458                     (tsize > stcb->sctp_ep->partial_delivery_point))) {
2459                         asoc->fragmented_delivery_inprogress = 1;
2460                         asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2461                         asoc->str_of_pdapi = chk->rec.data.stream_number;
2462                         asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2463                         asoc->pdapi_ppid = chk->rec.data.payloadtype;
2464                         asoc->fragment_flags = chk->rec.data.rcv_flags;
2465                         sctp_service_reassembly(stcb, asoc);
2466                         if (asoc->fragmented_delivery_inprogress == 0) {
2467                                 goto doit_again;
2468                         }
2469                 }
2470         }
2471 }
2472
2473 int
2474 sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2475     struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2476     struct sctp_nets *net, uint32_t * high_tsn)
2477 {
2478         struct sctp_data_chunk *ch, chunk_buf;
2479         struct sctp_association *asoc;
2480         int num_chunks = 0;     /* number of control chunks processed */
2481         int stop_proc = 0;
2482         int chk_length, break_flag, last_chunk;
2483         int abort_flag = 0, was_a_gap = 0;
2484         struct mbuf *m;
2485
2486         /* set the rwnd */
2487         sctp_set_rwnd(stcb, &stcb->asoc);
2488
2489         m = *mm;
2490         SCTP_TCB_LOCK_ASSERT(stcb);
2491         asoc = &stcb->asoc;
2492         if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
2493             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
2494             (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
2495                 /*
2496                  * wait a minute, this guy is gone, there is no longer a
2497                  * receiver. Send peer an ABORT!
2498                  */
2499                 struct mbuf *op_err;
2500
2501                 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2502                 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err);
2503                 return (2);
2504         }
2505         if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2506             stcb->asoc.cumulative_tsn, MAX_TSN)) {
2507                 /* there was a gap before this data was processed */
2508                 was_a_gap = 1;
2509         }
2510         /*
2511          * setup where we got the last DATA packet from for any SACK that
2512          * may need to go out. Don't bump the net. This is done ONLY when a
2513          * chunk is assigned.
2514          */
2515         asoc->last_data_chunk_from = net;
2516
2517         /*-
2518          * Now before we proceed we must figure out if this is a wasted
2519          * cluster... i.e. it is a small packet sent in and yet the driver
2520          * underneath allocated a full cluster for it. If so we must copy it
2521          * to a smaller mbuf and free up the cluster mbuf. This will help
2522          * with cluster starvation. Note for __Panda__ we don't do this
2523          * since it has clusters all the way down to 64 bytes.
2524          */
2525         if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2526                 /* we only handle mbufs that are singletons.. not chains */
2527                 m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2528                 if (m) {
2529                         /* ok lets see if we can copy the data up */
2530                         caddr_t *from, *to;
2531
2532                         /* get the pointers and copy */
2533                         to = mtod(m, caddr_t *);
2534                         from = mtod((*mm), caddr_t *);
2535                         memcpy(to, from, SCTP_BUF_LEN((*mm)));
2536                         /* copy the length and free up the old */
2537                         SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2538                         sctp_m_freem(*mm);
2539                         /* sucess, back copy */
2540                         *mm = m;
2541                 } else {
2542                         /* We are in trouble in the mbuf world .. yikes */
2543                         m = *mm;
2544                 }
2545         }
2546         /* get pointer to the first chunk header */
2547         ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2548             sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2549         if (ch == NULL) {
2550                 return (1);
2551         }
2552         /*
2553          * process all DATA chunks...
2554          */
2555         *high_tsn = asoc->cumulative_tsn;
2556         break_flag = 0;
2557         asoc->data_pkts_seen++;
2558         while (stop_proc == 0) {
2559                 /* validate chunk length */
2560                 chk_length = ntohs(ch->ch.chunk_length);
2561                 if (length - *offset < chk_length) {
2562                         /* all done, mutulated chunk */
2563                         stop_proc = 1;
2564                         break;
2565                 }
2566                 if (ch->ch.chunk_type == SCTP_DATA) {
2567                         if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2568                                 /*
2569                                  * Need to send an abort since we had a
2570                                  * invalid data chunk.
2571                                  */
2572                                 struct mbuf *op_err;
2573
2574                                 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2575                                     0, M_DONTWAIT, 1, MT_DATA);
2576
2577                                 if (op_err) {
2578                                         struct sctp_paramhdr *ph;
2579                                         uint32_t *ippp;
2580
2581                                         SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2582                                             (2 * sizeof(uint32_t));
2583                                         ph = mtod(op_err, struct sctp_paramhdr *);
2584                                         ph->param_type =
2585                                             htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2586                                         ph->param_length = htons(SCTP_BUF_LEN(op_err));
2587                                         ippp = (uint32_t *) (ph + 1);
2588                                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2589                                         ippp++;
2590                                         *ippp = asoc->cumulative_tsn;
2591
2592                                 }
2593                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2594                                 sctp_abort_association(inp, stcb, m, iphlen, sh,
2595                                     op_err, 0);
2596                                 return (2);
2597                         }
2598 #ifdef SCTP_AUDITING_ENABLED
2599                         sctp_audit_log(0xB1, 0);
2600 #endif
2601                         if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2602                                 last_chunk = 1;
2603                         } else {
2604                                 last_chunk = 0;
2605                         }
2606                         if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2607                             chk_length, net, high_tsn, &abort_flag, &break_flag,
2608                             last_chunk)) {
2609                                 num_chunks++;
2610                         }
2611                         if (abort_flag)
2612                                 return (2);
2613
2614                         if (break_flag) {
2615                                 /*
2616                                  * Set because of out of rwnd space and no
2617                                  * drop rep space left.
2618                                  */
2619                                 stop_proc = 1;
2620                                 break;
2621                         }
2622                 } else {
2623                         /* not a data chunk in the data region */
2624                         switch (ch->ch.chunk_type) {
2625                         case SCTP_INITIATION:
2626                         case SCTP_INITIATION_ACK:
2627                         case SCTP_SELECTIVE_ACK:
2628                         case SCTP_HEARTBEAT_REQUEST:
2629                         case SCTP_HEARTBEAT_ACK:
2630                         case SCTP_ABORT_ASSOCIATION:
2631                         case SCTP_SHUTDOWN:
2632                         case SCTP_SHUTDOWN_ACK:
2633                         case SCTP_OPERATION_ERROR:
2634                         case SCTP_COOKIE_ECHO:
2635                         case SCTP_COOKIE_ACK:
2636                         case SCTP_ECN_ECHO:
2637                         case SCTP_ECN_CWR:
2638                         case SCTP_SHUTDOWN_COMPLETE:
2639                         case SCTP_AUTHENTICATION:
2640                         case SCTP_ASCONF_ACK:
2641                         case SCTP_PACKET_DROPPED:
2642                         case SCTP_STREAM_RESET:
2643                         case SCTP_FORWARD_CUM_TSN:
2644                         case SCTP_ASCONF:
2645                                 /*
2646                                  * Now, what do we do with KNOWN chunks that
2647                                  * are NOT in the right place?
2648                                  * 
2649                                  * For now, I do nothing but ignore them. We
2650                                  * may later want to add sysctl stuff to
2651                                  * switch out and do either an ABORT() or
2652                                  * possibly process them.
2653                                  */
2654                                 if (sctp_strict_data_order) {
2655                                         struct mbuf *op_err;
2656
2657                                         op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
2658                                         sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0);
2659                                         return (2);
2660                                 }
2661                                 break;
2662                         default:
2663                                 /* unknown chunk type, use bit rules */
2664                                 if (ch->ch.chunk_type & 0x40) {
2665                                         /* Add a error report to the queue */
2666                                         struct mbuf *merr;
2667                                         struct sctp_paramhdr *phd;
2668
2669                                         merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
2670                                         if (merr) {
2671                                                 phd = mtod(merr, struct sctp_paramhdr *);
2672                                                 /*
2673                                                  * We cheat and use param
2674                                                  * type since we did not
2675                                                  * bother to define a error
2676                                                  * cause struct. They are
2677                                                  * the same basic format
2678                                                  * with different names.
2679                                                  */
2680                                                 phd->param_type =
2681                                                     htons(SCTP_CAUSE_UNRECOG_CHUNK);
2682                                                 phd->param_length =
2683                                                     htons(chk_length + sizeof(*phd));
2684                                                 SCTP_BUF_LEN(merr) = sizeof(*phd);
2685                                                 SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset,
2686                                                     SCTP_SIZE32(chk_length),
2687                                                     M_DONTWAIT);
2688                                                 if (SCTP_BUF_NEXT(merr)) {
2689                                                         sctp_queue_op_err(stcb, merr);
2690                                                 } else {
2691                                                         sctp_m_freem(merr);
2692                                                 }
2693                                         }
2694                                 }
2695                                 if ((ch->ch.chunk_type & 0x80) == 0) {
2696                                         /* discard the rest of this packet */
2697                                         stop_proc = 1;
2698                                 }       /* else skip this bad chunk and
2699                                          * continue... */
2700                                 break;
2701                         };      /* switch of chunk type */
2702                 }
2703                 *offset += SCTP_SIZE32(chk_length);
2704                 if ((*offset >= length) || stop_proc) {
2705                         /* no more data left in the mbuf chain */
2706                         stop_proc = 1;
2707                         continue;
2708                 }
2709                 ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2710                     sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2711                 if (ch == NULL) {
2712                         *offset = length;
2713                         stop_proc = 1;
2714                         break;
2715
2716                 }
2717         }                       /* while */
2718         if (break_flag) {
2719                 /*
2720                  * we need to report rwnd overrun drops.
2721                  */
2722                 sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
2723         }
2724         if (num_chunks) {
2725                 /*
2726                  * Did we get data, if so update the time for auto-close and
2727                  * give peer credit for being alive.
2728                  */
2729                 SCTP_STAT_INCR(sctps_recvpktwithdata);
2730                 stcb->asoc.overall_error_count = 0;
2731                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2732         }
2733         /* now service all of the reassm queue if needed */
2734         if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
2735                 sctp_service_queues(stcb, asoc);
2736
2737         if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2738                 /* Assure that we ack right away */
2739                 stcb->asoc.send_sack = 1;
2740         }
2741         /* Start a sack timer or QUEUE a SACK for sending */
2742         if ((stcb->asoc.cumulative_tsn == stcb->asoc.highest_tsn_inside_map) &&
2743             (stcb->asoc.mapping_array[0] != 0xff)) {
2744                 if ((stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) ||
2745                     (stcb->asoc.delayed_ack == 0) ||
2746                     (stcb->asoc.send_sack == 1)) {
2747                         if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2748                                 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2749                         }
2750                         sctp_send_sack(stcb);
2751                 } else {
2752                         if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2753                                 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2754                                     stcb->sctp_ep, stcb, NULL);
2755                         }
2756                 }
2757         } else {
2758                 sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
2759         }
2760         if (abort_flag)
2761                 return (2);
2762
2763         return (0);
2764 }
2765
2766 static void
2767 sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
2768     struct sctp_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
2769     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
2770     int num_seg, int *ecn_seg_sums)
2771 {
2772         /************************************************/
2773         /* process fragments and update sendqueue        */
2774         /************************************************/
2775         struct sctp_sack *sack;
2776         struct sctp_gap_ack_block *frag, block;
2777         struct sctp_tmit_chunk *tp1;
2778         int i;
2779         unsigned int j;
2780         int num_frs = 0;
2781
2782         uint16_t frag_strt, frag_end, primary_flag_set;
2783         u_long last_frag_high;
2784
2785         /*
2786          * @@@ JRI : TODO: This flag is not used anywhere .. remove?
2787          */
2788         if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
2789                 primary_flag_set = 1;
2790         } else {
2791                 primary_flag_set = 0;
2792         }
2793         sack = &ch->sack;
2794
2795         frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
2796             sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
2797         *offset += sizeof(block);
2798         if (frag == NULL) {
2799                 return;
2800         }
2801         tp1 = NULL;
2802         last_frag_high = 0;
2803         for (i = 0; i < num_seg; i++) {
2804                 frag_strt = ntohs(frag->start);
2805                 frag_end = ntohs(frag->end);
2806                 /* some sanity checks on the fargment offsets */
2807                 if (frag_strt > frag_end) {
2808                         /* this one is malformed, skip */
2809                         frag++;
2810                         continue;
2811                 }
2812                 if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
2813                     MAX_TSN))
2814                         *biggest_tsn_acked = frag_end + last_tsn;
2815
2816                 /* mark acked dgs and find out the highestTSN being acked */
2817                 if (tp1 == NULL) {
2818                         tp1 = TAILQ_FIRST(&asoc->sent_queue);
2819
2820                         /* save the locations of the last frags */
2821                         last_frag_high = frag_end + last_tsn;
2822                 } else {
2823                         /*
2824                          * now lets see if we need to reset the queue due to
2825                          * a out-of-order SACK fragment
2826                          */
2827                         if (compare_with_wrap(frag_strt + last_tsn,
2828                             last_frag_high, MAX_TSN)) {
2829                                 /*
2830                                  * if the new frag starts after the last TSN
2831                                  * frag covered, we are ok and this one is
2832                                  * beyond the last one
2833                                  */
2834                                 ;
2835                         } else {
2836                                 /*
2837                                  * ok, they have reset us, so we need to
2838                                  * reset the queue this will cause extra
2839                                  * hunting but hey, they chose the
2840                                  * performance hit when they failed to order
2841                                  * there gaps..
2842                                  */
2843                                 tp1 = TAILQ_FIRST(&asoc->sent_queue);
2844                         }
2845                         last_frag_high = frag_end + last_tsn;
2846                 }
2847                 for (j = frag_strt + last_tsn; j <= frag_end + last_tsn; j++) {
2848                         while (tp1) {
2849                                 if (tp1->rec.data.doing_fast_retransmit)
2850                                         num_frs++;
2851
2852                                 /*
2853                                  * CMT: CUCv2 algorithm. For each TSN being
2854                                  * processed from the sent queue, track the
2855                                  * next expected pseudo-cumack, or
2856                                  * rtx_pseudo_cumack, if required. Separate
2857                                  * cumack trackers for first transmissions,
2858                                  * and retransmissions.
2859                                  */
2860                                 if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
2861                                     (tp1->snd_count == 1)) {
2862                                         tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
2863                                         tp1->whoTo->find_pseudo_cumack = 0;
2864                                 }
2865                                 if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
2866                                     (tp1->snd_count > 1)) {
2867                                         tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
2868                                         tp1->whoTo->find_rtx_pseudo_cumack = 0;
2869                                 }
2870                                 if (tp1->rec.data.TSN_seq == j) {
2871                                         if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
2872                                                 /*
2873                                                  * must be held until
2874                                                  * cum-ack passes
2875                                                  */
2876                                                 /*
2877                                                  * ECN Nonce: Add the nonce
2878                                                  * value to the sender's
2879                                                  * nonce sum
2880                                                  */
2881                                                 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
2882                                                         /*-
2883                                                          * If it is less than RESEND, it is
2884                                                          * now no-longer in flight.
2885                                                          * Higher values may already be set
2886                                                          * via previous Gap Ack Blocks...
2887                                                          * i.e. ACKED or RESEND.
2888                                                          */
2889                                                         if (compare_with_wrap(tp1->rec.data.TSN_seq,
2890                                                             *biggest_newly_acked_tsn, MAX_TSN)) {
2891                                                                 *biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
2892                                                         }
2893                                                         /*
2894                                                          * CMT: SFR algo
2895                                                          * (and HTNA) - set
2896                                                          * saw_newack to 1
2897                                                          * for dest being
2898                                                          * newly acked.
2899                                                          * update
2900                                                          * this_sack_highest_
2901                                                          * newack if
2902                                                          * appropriate.
2903                                                          */
2904                                                         if (tp1->rec.data.chunk_was_revoked == 0)
2905                                                                 tp1->whoTo->saw_newack = 1;
2906
2907                                                         if (compare_with_wrap(tp1->rec.data.TSN_seq,
2908                                                             tp1->whoTo->this_sack_highest_newack,
2909                                                             MAX_TSN)) {
2910                                                                 tp1->whoTo->this_sack_highest_newack =
2911                                                                     tp1->rec.data.TSN_seq;
2912                                                         }
2913                                                         /*
2914                                                          * CMT DAC algo:
2915                                                          * also update
2916                                                          * this_sack_lowest_n
2917                                                          * ewack
2918                                                          */
2919                                                         if (*this_sack_lowest_newack == 0) {
2920                                                                 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
2921                                                                         sctp_log_sack(*this_sack_lowest_newack,
2922                                                                             last_tsn,
2923                                                                             tp1->rec.data.TSN_seq,
2924                                                                             0,
2925                                                                             0,
2926                                                                             SCTP_LOG_TSN_ACKED);
2927                                                                 }
2928                                                                 *this_sack_lowest_newack = tp1->rec.data.TSN_seq;
2929                                                         }
2930                                                         /*
2931                                                          * CMT: CUCv2
2932                                                          * algorithm. If
2933                                                          * (rtx-)pseudo-cumac
2934                                                          * k for corresp
2935                                                          * dest is being
2936                                                          * acked, then we
2937                                                          * have a new
2938                                                          * (rtx-)pseudo-cumac
2939                                                          * k. Set
2940                                                          * new_(rtx_)pseudo_c
2941                                                          * umack to TRUE so
2942                                                          * that the cwnd for
2943                                                          * this dest can be
2944                                                          * updated. Also
2945                                                          * trigger search
2946                                                          * for the next
2947                                                          * expected
2948                                                          * (rtx-)pseudo-cumac
2949                                                          * k. Separate
2950                                                          * pseudo_cumack
2951                                                          * trackers for
2952                                                          * first
2953                                                          * transmissions and
2954                                                          * retransmissions.
2955                                                          */
2956                                                         if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
2957                                                                 if (tp1->rec.data.chunk_was_revoked == 0) {
2958                                                                         tp1->whoTo->new_pseudo_cumack = 1;
2959                                                                 }
2960                                                                 tp1->whoTo->find_pseudo_cumack = 1;
2961                                                         }
2962                                                         if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
2963                                                                 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
2964                                                         }
2965                                                         if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
2966                                                                 if (tp1->rec.data.chunk_was_revoked == 0) {
2967                                                                         tp1->whoTo->new_pseudo_cumack = 1;
2968                                                                 }
2969                                                                 tp1->whoTo->find_rtx_pseudo_cumack = 1;
2970                                                         }
2971                                                         if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
2972                                                                 sctp_log_sack(*biggest_newly_acked_tsn,
2973                                                                     last_tsn,
2974                                                                     tp1->rec.data.TSN_seq,
2975                                                                     frag_strt,
2976                                                                     frag_end,
2977                                                                     SCTP_LOG_TSN_ACKED);
2978                                                         }
2979                                                         if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
2980                                                                 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
2981                                                                     tp1->whoTo->flight_size,
2982                                                                     tp1->book_size,
2983                                                                     (uintptr_t) tp1->whoTo,
2984                                                                     tp1->rec.data.TSN_seq);
2985                                                         }
2986                                                         sctp_flight_size_decrease(tp1);
2987                                                         sctp_total_flight_decrease(stcb, tp1);
2988
2989                                                         tp1->whoTo->net_ack += tp1->send_size;
2990                                                         if (tp1->snd_count < 2) {
2991                                                                 /*
2992                                                                  * True
2993                                                                  * non-retran
2994                                                                  * smited
2995                                                                  * chunk */
2996                                                                 tp1->whoTo->net_ack2 += tp1->send_size;
2997
2998                                                                 /*
2999                                                                  * update RTO
3000                                                                  * too ? */
3001                                                                 if (tp1->do_rtt) {
3002                                                                         tp1->whoTo->RTO =
3003                                                                             sctp_calculate_rto(stcb,
3004                                                                             asoc,
3005                                                                             tp1->whoTo,
3006                                                                             &tp1->sent_rcv_time);
3007                                                                         tp1->do_rtt = 0;
3008                                                                 }
3009                                                         }
3010                                                 }
3011                                                 if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
3012                                                         (*ecn_seg_sums) += tp1->rec.data.ect_nonce;
3013                                                         (*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
3014                                                         if (compare_with_wrap(tp1->rec.data.TSN_seq,
3015                                                             asoc->this_sack_highest_gap,
3016                                                             MAX_TSN)) {
3017                                                                 asoc->this_sack_highest_gap =
3018                                                                     tp1->rec.data.TSN_seq;
3019                                                         }
3020                                                         if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3021                                                                 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3022 #ifdef SCTP_AUDITING_ENABLED
3023                                                                 sctp_audit_log(0xB2,
3024                                                                     (asoc->sent_queue_retran_cnt & 0x000000ff));
3025 #endif
3026                                                         }
3027                                                 }
3028                                                 /*
3029                                                  * All chunks NOT UNSENT
3030                                                  * fall through here and are
3031                                                  * marked
3032                                                  */
3033                                                 tp1->sent = SCTP_DATAGRAM_MARKED;
3034                                                 if (tp1->rec.data.chunk_was_revoked) {
3035                                                         /* deflate the cwnd */
3036                                                         tp1->whoTo->cwnd -= tp1->book_size;
3037                                                         tp1->rec.data.chunk_was_revoked = 0;
3038                                                 }
3039                                         }
3040                                         break;
3041                                 }       /* if (tp1->TSN_seq == j) */
3042                                 if (compare_with_wrap(tp1->rec.data.TSN_seq, j,
3043                                     MAX_TSN))
3044                                         break;
3045
3046                                 tp1 = TAILQ_NEXT(tp1, sctp_next);
3047                         }       /* end while (tp1) */
3048                 }               /* end for (j = fragStart */
3049                 frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3050                     sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3051                 *offset += sizeof(block);
3052                 if (frag == NULL) {
3053                         break;
3054                 }
3055         }
3056         if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3057                 if (num_frs)
3058                         sctp_log_fr(*biggest_tsn_acked,
3059                             *biggest_newly_acked_tsn,
3060                             last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
3061         }
3062 }
3063
3064 static void
3065 sctp_check_for_revoked(struct sctp_tcb *stcb,
3066     struct sctp_association *asoc, uint32_t cumack,
3067     u_long biggest_tsn_acked)
3068 {
3069         struct sctp_tmit_chunk *tp1;
3070         int tot_revoked = 0;
3071
3072         tp1 = TAILQ_FIRST(&asoc->sent_queue);
3073         while (tp1) {
3074                 if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
3075                     MAX_TSN)) {
3076                         /*
3077                          * ok this guy is either ACK or MARKED. If it is
3078                          * ACKED it has been previously acked but not this
3079                          * time i.e. revoked.  If it is MARKED it was ACK'ed
3080                          * again.
3081                          */
3082                         if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3083                             MAX_TSN))
3084                                 break;
3085
3086
3087                         if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3088                                 /* it has been revoked */
3089                                 tp1->sent = SCTP_DATAGRAM_SENT;
3090                                 tp1->rec.data.chunk_was_revoked = 1;
3091                                 /*
3092                                  * We must add this stuff back in to assure
3093                                  * timers and such get started.
3094                                  */
3095                                 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3096                                         sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3097                                             tp1->whoTo->flight_size,
3098                                             tp1->book_size,
3099                                             (uintptr_t) tp1->whoTo,
3100                                             tp1->rec.data.TSN_seq);
3101                                 }
3102                                 sctp_flight_size_increase(tp1);
3103                                 sctp_total_flight_increase(stcb, tp1);
3104                                 /*
3105                                  * We inflate the cwnd to compensate for our
3106                                  * artificial inflation of the flight_size.
3107                                  */
3108                                 tp1->whoTo->cwnd += tp1->book_size;
3109                                 tot_revoked++;
3110                                 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
3111                                         sctp_log_sack(asoc->last_acked_seq,
3112                                             cumack,
3113                                             tp1->rec.data.TSN_seq,
3114                                             0,
3115                                             0,
3116                                             SCTP_LOG_TSN_REVOKED);
3117                                 }
3118                         } else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3119                                 /* it has been re-acked in this SACK */
3120                                 tp1->sent = SCTP_DATAGRAM_ACKED;
3121                         }
3122                 }
3123                 if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3124                         break;
3125                 tp1 = TAILQ_NEXT(tp1, sctp_next);
3126         }
3127         if (tot_revoked > 0) {
3128                 /*
3129                  * Setup the ecn nonce re-sync point. We do this since once
3130                  * data is revoked we begin to retransmit things, which do
3131                  * NOT have the ECN bits set. This means we are now out of
3132                  * sync and must wait until we get back in sync with the
3133                  * peer to check ECN bits.
3134                  */
3135                 tp1 = TAILQ_FIRST(&asoc->send_queue);
3136                 if (tp1 == NULL) {
3137                         asoc->nonce_resync_tsn = asoc->sending_seq;
3138                 } else {
3139                         asoc->nonce_resync_tsn = tp1->rec.data.TSN_seq;
3140                 }
3141                 asoc->nonce_wait_for_ecne = 0;
3142                 asoc->nonce_sum_check = 0;
3143         }
3144 }
3145
3146 static void
3147 sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
3148     u_long biggest_tsn_acked, u_long biggest_tsn_newly_acked, u_long this_sack_lowest_newack, int accum_moved)
3149 {
3150         struct sctp_tmit_chunk *tp1;
3151         int strike_flag = 0;
3152         struct timeval now;
3153         int tot_retrans = 0;
3154         uint32_t sending_seq;
3155         struct sctp_nets *net;
3156         int num_dests_sacked = 0;
3157
3158         /*
3159          * select the sending_seq, this is either the next thing ready to be
3160          * sent but not transmitted, OR, the next seq we assign.
3161          */
3162         tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3163         if (tp1 == NULL) {
3164                 sending_seq = asoc->sending_seq;
3165         } else {
3166                 sending_seq = tp1->rec.data.TSN_seq;
3167         }
3168
3169         /* CMT DAC algo: finding out if SACK is a mixed SACK */
3170         if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3171                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3172                         if (net->saw_newack)
3173                                 num_dests_sacked++;
3174                 }
3175         }
3176         if (stcb->asoc.peer_supports_prsctp) {
3177                 (void)SCTP_GETTIME_TIMEVAL(&now);
3178         }
3179         tp1 = TAILQ_FIRST(&asoc->sent_queue);
3180         while (tp1) {
3181                 strike_flag = 0;
3182                 if (tp1->no_fr_allowed) {
3183                         /* this one had a timeout or something */
3184                         tp1 = TAILQ_NEXT(tp1, sctp_next);
3185                         continue;
3186                 }
3187                 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3188                         if (tp1->sent < SCTP_DATAGRAM_RESEND)
3189                                 sctp_log_fr(biggest_tsn_newly_acked,
3190                                     tp1->rec.data.TSN_seq,
3191                                     tp1->sent,
3192                                     SCTP_FR_LOG_CHECK_STRIKE);
3193                 }
3194                 if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3195                     MAX_TSN) ||
3196                     tp1->sent == SCTP_DATAGRAM_UNSENT) {
3197                         /* done */
3198                         break;
3199                 }
3200                 if (stcb->asoc.peer_supports_prsctp) {
3201                         if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3202                                 /* Is it expired? */
3203                                 if (
3204                                     (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
3205                                     ) {
3206                                         /* Yes so drop it */
3207                                         if (tp1->data != NULL) {
3208                                                 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3209                                                     (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3210                                                     &asoc->sent_queue);
3211                                         }
3212                                         tp1 = TAILQ_NEXT(tp1, sctp_next);
3213                                         continue;
3214                                 }
3215                         }
3216                         if ((PR_SCTP_RTX_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3217                                 /* Has it been retransmitted tv_sec times? */
3218                                 if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3219                                         /* Yes, so drop it */
3220                                         if (tp1->data != NULL) {
3221                                                 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3222                                                     (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3223                                                     &asoc->sent_queue);
3224                                         }
3225                                         tp1 = TAILQ_NEXT(tp1, sctp_next);
3226                                         continue;
3227                                 }
3228                         }
3229                 }
3230                 if (compare_with_wrap(tp1->rec.data.TSN_seq,
3231                     asoc->this_sack_highest_gap, MAX_TSN)) {
3232                         /* we are beyond the tsn in the sack  */
3233                         break;
3234                 }
3235                 if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3236                         /* either a RESEND, ACKED, or MARKED */
3237                         /* skip */
3238                         tp1 = TAILQ_NEXT(tp1, sctp_next);
3239                         continue;
3240                 }
3241                 /*
3242                  * CMT : SFR algo (covers part of DAC and HTNA as well)
3243                  */
3244                 if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3245                         /*
3246                          * No new acks were receieved for data sent to this
3247                          * dest. Therefore, according to the SFR algo for
3248                          * CMT, no data sent to this dest can be marked for
3249                          * FR using this SACK.
3250                          */
3251                         tp1 = TAILQ_NEXT(tp1, sctp_next);
3252                         continue;
3253                 } else if (tp1->whoTo && compare_with_wrap(tp1->rec.data.TSN_seq,
3254                     tp1->whoTo->this_sack_highest_newack, MAX_TSN)) {
3255                         /*
3256                          * CMT: New acks were receieved for data sent to
3257                          * this dest. But no new acks were seen for data
3258                          * sent after tp1. Therefore, according to the SFR
3259                          * algo for CMT, tp1 cannot be marked for FR using
3260                          * this SACK. This step covers part of the DAC algo
3261                          * and the HTNA algo as well.
3262                          */
3263                         tp1 = TAILQ_NEXT(tp1, sctp_next);
3264                         continue;
3265                 }
3266                 /*
3267                  * Here we check to see if we were have already done a FR
3268                  * and if so we see if the biggest TSN we saw in the sack is
3269                  * smaller than the recovery point. If so we don't strike
3270                  * the tsn... otherwise we CAN strike the TSN.
3271                  */
3272                 /*
3273                  * @@@ JRI: Check for CMT if (accum_moved &&
3274                  * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
3275                  * 0)) {
3276                  */
3277                 if (accum_moved && asoc->fast_retran_loss_recovery) {
3278                         /*
3279                          * Strike the TSN if in fast-recovery and cum-ack
3280                          * moved.
3281                          */
3282                         if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3283                                 sctp_log_fr(biggest_tsn_newly_acked,
3284                                     tp1->rec.data.TSN_seq,
3285                                     tp1->sent,
3286                                     SCTP_FR_LOG_STRIKE_CHUNK);
3287                         }
3288                         if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3289                                 tp1->sent++;
3290                         }
3291                         if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3292                                 /*
3293                                  * CMT DAC algorithm: If SACK flag is set to
3294                                  * 0, then lowest_newack test will not pass
3295                                  * because it would have been set to the
3296                                  * cumack earlier. If not already to be
3297                                  * rtx'd, If not a mixed sack and if tp1 is
3298                                  * not between two sacked TSNs, then mark by
3299                                  * one more. NOTE that we are marking by one
3300                                  * additional time since the SACK DAC flag
3301                                  * indicates that two packets have been
3302                                  * received after this missing TSN.
3303                                  */
3304                                 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3305                                     compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3306                                         if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3307                                                 sctp_log_fr(16 + num_dests_sacked,
3308                                                     tp1->rec.data.TSN_seq,
3309                                                     tp1->sent,
3310                                                     SCTP_FR_LOG_STRIKE_CHUNK);
3311                                         }
3312                                         tp1->sent++;
3313                                 }
3314                         }
3315                 } else if (tp1->rec.data.doing_fast_retransmit) {
3316                         /*
3317                          * For those that have done a FR we must take
3318                          * special consideration if we strike. I.e the
3319                          * biggest_newly_acked must be higher than the
3320                          * sending_seq at the time we did the FR.
3321                          */
3322                         if (
3323 #ifdef SCTP_FR_TO_ALTERNATE
3324                         /*
3325                          * If FR's go to new networks, then we must only do
3326                          * this for singly homed asoc's. However if the FR's
3327                          * go to the same network (Armando's work) then its
3328                          * ok to FR multiple times.
3329                          */
3330                             (asoc->numnets < 2)
3331 #else
3332                             (1)
3333 #endif
3334                             ) {
3335
3336                                 if ((compare_with_wrap(biggest_tsn_newly_acked,
3337                                     tp1->rec.data.fast_retran_tsn, MAX_TSN)) ||
3338                                     (biggest_tsn_newly_acked ==
3339                                     tp1->rec.data.fast_retran_tsn)) {
3340                                         /*
3341                                          * Strike the TSN, since this ack is
3342                                          * beyond where things were when we
3343                                          * did a FR.
3344                                          */
3345                                         if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3346                                                 sctp_log_fr(biggest_tsn_newly_acked,
3347                                                     tp1->rec.data.TSN_seq,
3348                                                     tp1->sent,
3349                                                     SCTP_FR_LOG_STRIKE_CHUNK);
3350                                         }
3351                                         if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3352                                                 tp1->sent++;
3353                                         }
3354                                         strike_flag = 1;
3355                                         if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3356                                                 /*
3357                                                  * CMT DAC algorithm: If
3358                                                  * SACK flag is set to 0,
3359                                                  * then lowest_newack test
3360                                                  * will not pass because it
3361                                                  * would have been set to
3362                                                  * the cumack earlier. If
3363                                                  * not already to be rtx'd,
3364                                                  * If not a mixed sack and
3365                                                  * if tp1 is not between two
3366                                                  * sacked TSNs, then mark by
3367                                                  * one more. NOTE that we
3368                                                  * are marking by one
3369                                                  * additional time since the
3370                                                  * SACK DAC flag indicates
3371                                                  * that two packets have
3372                                                  * been received after this
3373                                                  * missing TSN.
3374                                                  */
3375                                                 if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
3376                                                     (num_dests_sacked == 1) &&
3377                                                     compare_with_wrap(this_sack_lowest_newack,
3378                                                     tp1->rec.data.TSN_seq, MAX_TSN)) {
3379                                                         if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3380                                                                 sctp_log_fr(32 + num_dests_sacked,
3381                                                                     tp1->rec.data.TSN_seq,
3382                                                                     tp1->sent,
3383                                                                     SCTP_FR_LOG_STRIKE_CHUNK);
3384                                                         }
3385                                                         if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3386                                                                 tp1->sent++;
3387
3388                                                         }
3389                                                 }
3390                                         }
3391                                 }
3392                         }
3393                         /*
3394                          * JRI: TODO: remove code for HTNA algo. CMT's SFR
3395                          * algo covers HTNA.
3396                          */
3397                 } else if (compare_with_wrap(tp1->rec.data.TSN_seq,
3398                     biggest_tsn_newly_acked, MAX_TSN)) {
3399                         /*
3400                          * We don't strike these: This is the  HTNA
3401                          * algorithm i.e. we don't strike If our TSN is
3402                          * larger than the Highest TSN Newly Acked.
3403                          */
3404                         ;
3405                 } else {
3406                         /* Strike the TSN */
3407                         if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3408                                 sctp_log_fr(biggest_tsn_newly_acked,
3409                                     tp1->rec.data.TSN_seq,
3410                                     tp1->sent,
3411                                     SCTP_FR_LOG_STRIKE_CHUNK);
3412                         }
3413                         if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3414                                 tp1->sent++;
3415                         }
3416                         if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3417                                 /*
3418                                  * CMT DAC algorithm: If SACK flag is set to
3419                                  * 0, then lowest_newack test will not pass
3420                                  * because it would have been set to the
3421                                  * cumack earlier. If not already to be
3422                                  * rtx'd, If not a mixed sack and if tp1 is
3423                                  * not between two sacked TSNs, then mark by
3424                                  * one more. NOTE that we are marking by one
3425                                  * additional time since the SACK DAC flag
3426                                  * indicates that two packets have been
3427                                  * received after this missing TSN.
3428                                  */
3429                                 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3430                                     compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3431                                         if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3432                                                 sctp_log_fr(48 + num_dests_sacked,
3433                                                     tp1->rec.data.TSN_seq,
3434                                                     tp1->sent,
3435                                                     SCTP_FR_LOG_STRIKE_CHUNK);
3436                                         }
3437                                         tp1->sent++;
3438                                 }
3439                         }
3440                 }
3441                 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3442                         /* Increment the count to resend */
3443                         struct sctp_nets *alt;
3444
3445                         /* printf("OK, we are now ready to FR this guy\n"); */
3446                         if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3447                                 sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3448                                     0, SCTP_FR_MARKED);
3449                         }
3450                         if (strike_flag) {
3451                                 /* This is a subsequent FR */
3452                                 SCTP_STAT_INCR(sctps_sendmultfastretrans);
3453                         }
3454                         sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3455                         if (sctp_cmt_on_off) {
3456                                 /*
3457                                  * CMT: Using RTX_SSTHRESH policy for CMT.
3458                                  * If CMT is being used, then pick dest with
3459                                  * largest ssthresh for any retransmission.
3460                                  */
3461                                 tp1->no_fr_allowed = 1;
3462                                 alt = tp1->whoTo;
3463                                 /* sa_ignore NO_NULL_CHK */
3464                                 alt = sctp_find_alternate_net(stcb, alt, 1);
3465                                 if (alt == NULL) {
3466                                         alt = tp1->whoTo;
3467                                 }
3468                                 /*
3469                                  * CUCv2: If a different dest is picked for
3470                                  * the retransmission, then new
3471                                  * (rtx-)pseudo_cumack needs to be tracked
3472                                  * for orig dest. Let CUCv2 track new (rtx-)
3473                                  * pseudo-cumack always.
3474                                  */
3475                                 if (tp1->whoTo) {
3476                                         tp1->whoTo->find_pseudo_cumack = 1;
3477                                         tp1->whoTo->find_rtx_pseudo_cumack = 1;
3478                                 }
3479                         } else {/* CMT is OFF */
3480
3481 #ifdef SCTP_FR_TO_ALTERNATE
3482                                 /* Can we find an alternate? */
3483                                 alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3484 #else
3485                                 /*
3486                                  * default behavior is to NOT retransmit
3487                                  * FR's to an alternate. Armando Caro's
3488                                  * paper details why.
3489                                  */
3490                                 alt = tp1->whoTo;
3491 #endif
3492                         }
3493
3494                         tp1->rec.data.doing_fast_retransmit = 1;
3495                         tot_retrans++;
3496                         /* mark the sending seq for possible subsequent FR's */
3497                         /*
3498                          * printf("Marking TSN for FR new value %x\n",
3499                          * (uint32_t)tpi->rec.data.TSN_seq);
3500                          */
3501                         if (TAILQ_EMPTY(&asoc->send_queue)) {
3502                                 /*
3503                                  * If the queue of send is empty then its
3504                                  * the next sequence number that will be
3505                                  * assigned so we subtract one from this to
3506                                  * get the one we last sent.
3507                                  */
3508                                 tp1->rec.data.fast_retran_tsn = sending_seq;
3509                         } else {
3510                                 /*
3511                                  * If there are chunks on the send queue
3512                                  * (unsent data that has made it from the
3513                                  * stream queues but not out the door, we
3514                                  * take the first one (which will have the
3515                                  * lowest TSN) and subtract one to get the
3516                                  * one we last sent.
3517                                  */
3518                                 struct sctp_tmit_chunk *ttt;
3519
3520                                 ttt = TAILQ_FIRST(&asoc->send_queue);
3521                                 tp1->rec.data.fast_retran_tsn =
3522                                     ttt->rec.data.TSN_seq;
3523                         }
3524
3525                         if (tp1->do_rtt) {
3526                                 /*
3527                                  * this guy had a RTO calculation pending on
3528                                  * it, cancel it
3529                                  */
3530                                 tp1->do_rtt = 0;
3531                         }
3532                         /* fix counts and things */
3533                         if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3534                                 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3535                                     (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3536                                     tp1->book_size,
3537                                     (uintptr_t) tp1->whoTo,
3538                                     tp1->rec.data.TSN_seq);
3539                         }
3540                         if (tp1->whoTo) {
3541                                 tp1->whoTo->net_ack++;
3542                                 sctp_flight_size_decrease(tp1);
3543                         }
3544                         if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
3545                                 sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3546                                     asoc->peers_rwnd, tp1->send_size, sctp_peer_chunk_oh);
3547                         }
3548                         /* add back to the rwnd */
3549                         asoc->peers_rwnd += (tp1->send_size + sctp_peer_chunk_oh);
3550
3551                         /* remove from the total flight */
3552                         sctp_total_flight_decrease(stcb, tp1);
3553                         if (alt != tp1->whoTo) {
3554                                 /* yes, there is an alternate. */
3555                                 sctp_free_remote_addr(tp1->whoTo);
3556                                 /* sa_ignore FREED_MEMORY */
3557                                 tp1->whoTo = alt;
3558                                 atomic_add_int(&alt->ref_count, 1);
3559                         }
3560                 }
3561                 tp1 = TAILQ_NEXT(tp1, sctp_next);
3562         }                       /* while (tp1) */
3563
3564         if (tot_retrans > 0) {
3565                 /*
3566                  * Setup the ecn nonce re-sync point. We do this since once
3567                  * we go to FR something we introduce a Karn's rule scenario
3568                  * and won't know the totals for the ECN bits.
3569                  */
3570                 asoc->nonce_resync_tsn = sending_seq;
3571                 asoc->nonce_wait_for_ecne = 0;
3572                 asoc->nonce_sum_check = 0;
3573         }
3574 }
3575
3576 struct sctp_tmit_chunk *
3577 sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3578     struct sctp_association *asoc)
3579 {
3580         struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3581         struct timeval now;
3582         int now_filled = 0;
3583
3584         if (asoc->peer_supports_prsctp == 0) {
3585                 return (NULL);
3586         }
3587         tp1 = TAILQ_FIRST(&asoc->sent_queue);
3588         while (tp1) {
3589                 if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
3590                     tp1->sent != SCTP_DATAGRAM_RESEND) {
3591                         /* no chance to advance, out of here */
3592                         break;
3593                 }
3594                 if (!PR_SCTP_ENABLED(tp1->flags)) {
3595                         /*
3596                          * We can't fwd-tsn past any that are reliable aka
3597                          * retransmitted until the asoc fails.
3598                          */
3599                         break;
3600                 }
3601                 if (!now_filled) {
3602                         (void)SCTP_GETTIME_TIMEVAL(&now);
3603                         now_filled = 1;
3604                 }
3605                 tp2 = TAILQ_NEXT(tp1, sctp_next);
3606                 /*
3607                  * now we got a chunk which is marked for another
3608                  * retransmission to a PR-stream but has run out its chances
3609                  * already maybe OR has been marked to skip now. Can we skip
3610                  * it if its a resend?
3611                  */
3612                 if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3613                     (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3614                         /*
3615                          * Now is this one marked for resend and its time is
3616                          * now up?
3617                          */
3618                         if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3619                                 /* Yes so drop it */
3620                                 if (tp1->data) {
3621                                         (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3622                                             (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3623                                             &asoc->sent_queue);
3624                                 }
3625                         } else {
3626                                 /*
3627                                  * No, we are done when hit one for resend
3628                                  * whos time as not expired.
3629                                  */
3630                                 break;
3631                         }
3632                 }
3633                 /*
3634                  * Ok now if this chunk is marked to drop it we can clean up
3635                  * the chunk, advance our peer ack point and we can check
3636                  * the next chunk.
3637                  */
3638                 if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3639                         /* advance PeerAckPoint goes forward */
3640                         asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3641                         a_adv = tp1;
3642                         /*
3643                          * we don't want to de-queue it here. Just wait for
3644                          * the next peer SACK to come with a new cumTSN and
3645                          * then the chunk will be droped in the normal
3646                          * fashion.
3647                          */
3648                         if (tp1->data) {
3649                                 sctp_free_bufspace(stcb, asoc, tp1, 1);
3650                                 /*
3651                                  * Maybe there should be another
3652                                  * notification type
3653                                  */
3654                                 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3655                                     (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3656                                     tp1);
3657                                 sctp_m_freem(tp1->data);
3658                                 tp1->data = NULL;
3659                                 if (stcb->sctp_socket) {
3660                                         sctp_sowwakeup(stcb->sctp_ep,
3661                                             stcb->sctp_socket);
3662                                         if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
3663                                                 sctp_wakeup_log(stcb, tp1->rec.data.TSN_seq, 1, SCTP_WAKESND_FROM_FWDTSN);
3664                                         }
3665                                 }
3666                         }
3667                 } else {
3668                         /*
3669                          * If it is still in RESEND we can advance no
3670                          * further
3671                          */
3672                         break;
3673                 }
3674                 /*
3675                  * If we hit here we just dumped tp1, move to next tsn on
3676                  * sent queue.
3677                  */
3678                 tp1 = tp2;
3679         }
3680         return (a_adv);
3681 }
3682
3683 #ifdef SCTP_HIGH_SPEED
3684 struct sctp_hs_raise_drop {
3685         int32_t cwnd;
3686         int32_t increase;
3687         int32_t drop_percent;
3688 };
3689
3690 #define SCTP_HS_TABLE_SIZE 73
3691
3692 struct sctp_hs_raise_drop sctp_cwnd_adjust[SCTP_HS_TABLE_SIZE] = {
3693         {38, 1, 50},            /* 0   */
3694         {118, 2, 44},           /* 1   */
3695         {221, 3, 41},           /* 2   */
3696         {347, 4, 38},           /* 3   */
3697         {495, 5, 37},           /* 4   */
3698         {663, 6, 35},           /* 5   */
3699         {851, 7, 34},           /* 6   */
3700         {1058, 8, 33},          /* 7   */
3701         {1284, 9, 32},          /* 8   */
3702         {1529, 10, 31},         /* 9   */
3703         {1793, 11, 30},         /* 10  */
3704         {2076, 12, 29},         /* 11  */
3705         {2378, 13, 28},         /* 12  */
3706         {2699, 14, 28},         /* 13  */
3707         {3039, 15, 27},         /* 14  */
3708         {3399, 16, 27},         /* 15  */
3709         {3778, 17, 26},         /* 16  */
3710         {4177, 18, 26},         /* 17  */
3711         {4596, 19, 25},         /* 18  */
3712         {5036, 20, 25},         /* 19  */
3713         {5497, 21, 24},         /* 20  */
3714         {5979, 22, 24},         /* 21  */
3715         {6483, 23, 23},         /* 22  */
3716         {7009, 24, 23},         /* 23  */
3717         {7558, 25, 22},         /* 24  */
3718         {8130, 26, 22},         /* 25  */
3719         {8726, 27, 22},         /* 26  */
3720         {9346, 28, 21},         /* 27  */
3721         {9991, 29, 21},         /* 28  */
3722         {10661, 30, 21},        /* 29  */
3723         {11358, 31, 20},        /* 30  */
3724         {12082, 32, 20},        /* 31  */
3725         {12834, 33, 20},        /* 32  */
3726         {13614, 34, 19},        /* 33  */
3727         {14424, 35, 19},        /* 34  */
3728         {15265, 36, 19},        /* 35  */
3729         {16137, 37, 19},        /* 36  */
3730         {17042, 38, 18},        /* 37  */
3731         {17981, 39, 18},        /* 38  */
3732         {18955, 40, 18},        /* 39  */
3733         {19965, 41, 17},        /* 40  */
3734         {21013, 42, 17},        /* 41  */
3735         {22101, 43, 17},        /* 42  */
3736         {23230, 44, 17},        /* 43  */
3737         {24402, 45, 16},        /* 44  */
3738         {25618, 46, 16},        /* 45  */
3739         {26881, 47, 16},        /* 46  */
3740         {28193, 48, 16},        /* 47  */
3741         {29557, 49, 15},        /* 48  */
3742         {30975, 50, 15},        /* 49  */
3743         {32450, 51, 15},        /* 50  */
3744         {33986, 52, 15},        /* 51  */
3745         {35586, 53, 14},        /* 52  */
3746         {37253, 54, 14},        /* 53  */
3747         {38992, 55, 14},        /* 54  */
3748         {40808, 56, 14},        /* 55  */
3749         {42707, 57, 13},        /* 56  */
3750         {44694, 58, 13},        /* 57  */
3751         {46776, 59, 13},        /* 58  */
3752         {48961, 60, 13},        /* 59  */
3753         {51258, 61, 13},        /* 60  */
3754         {53677, 62, 12},        /* 61  */
3755         {56230, 63, 12},        /* 62  */
3756         {58932, 64, 12},        /* 63  */
3757         {61799, 65, 12},        /* 64  */
3758         {64851, 66, 11},        /* 65  */
3759         {68113, 67, 11},        /* 66  */
3760         {71617, 68, 11},        /* 67  */
3761         {75401, 69, 10},        /* 68  */
3762         {79517, 70, 10},        /* 69  */
3763         {84035, 71, 10},        /* 70  */
3764         {89053, 72, 10},        /* 71  */
3765         {94717, 73, 9}          /* 72  */
3766 };
3767
3768 static void
3769 sctp_hs_cwnd_increase(struct sctp_tcb *stcb, struct sctp_nets *net)
3770 {
3771         int cur_val, i, indx, incr;
3772
3773         cur_val = net->cwnd >> 10;
3774         indx = SCTP_HS_TABLE_SIZE - 1;
3775
3776         if (cur_val < sctp_cwnd_adjust[0].cwnd) {
3777                 /* normal mode */
3778                 if (net->net_ack > net->mtu) {
3779                         net->cwnd += net->mtu;
3780                         if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) {
3781                                 sctp_log_cwnd(stcb, net, net->mtu, SCTP_CWND_LOG_FROM_SS);
3782                         }
3783                 } else {
3784                         net->cwnd += net->net_ack;
3785                         if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) {
3786                                 sctp_log_cwnd(stcb, net, net->net_ack, SCTP_CWND_LOG_FROM_SS);
3787                         }
3788                 }
3789         } else {
3790                 for (i = net->last_hs_used; i < SCTP_HS_TABLE_SIZE; i++) {
3791                         if (cur_val < sctp_cwnd_adjust[i].cwnd) {
3792                                 indx = i;
3793                                 break;
3794                         }
3795                 }
3796                 net->last_hs_used = indx;
3797                 incr = ((sctp_cwnd_adjust[indx].increase) << 10);
3798                 net->cwnd += incr;
3799                 if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) {
3800                         sctp_log_cwnd(stcb, net, incr, SCTP_CWND_LOG_FROM_SS);
3801                 }
3802         }
3803 }
3804
3805 static void
3806 sctp_hs_cwnd_decrease(struct sctp_tcb *stcb, struct sctp_nets *net)
3807 {
3808         int cur_val, i, indx;
3809         int old_cwnd = net->cwnd;
3810
3811         cur_val = net->cwnd >> 10;
3812         indx = net->last_hs_used;
3813         if (cur_val < sctp_cwnd_adjust[0].cwnd) {
3814                 /* normal mode */
3815                 net->ssthresh = net->cwnd / 2;
3816                 if (net->ssthresh < (net->mtu * 2)) {
3817                         net->ssthresh = 2 * net->mtu;
3818                 }
3819                 net->cwnd = net->ssthresh;
3820         } else {
3821                 /* drop by the proper amount */
3822                 net->ssthresh = net->cwnd - (int)((net->cwnd / 100) *
3823                     sctp_cwnd_adjust[net->last_hs_used].drop_percent);
3824                 net->cwnd = net->ssthresh;
3825                 /* now where are we */
3826                 indx = net->last_hs_used;
3827                 cur_val = net->cwnd >> 10;
3828                 /* reset where we are in the table */
3829                 if (cur_val < sctp_cwnd_adjust[0].cwnd) {
3830                         /* feel out of hs */
3831                         net->last_hs_used = 0;
3832                 } else {
3833                         for (i = indx; i >= 1; i--) {
3834                                 if (cur_val > sctp_cwnd_adjust[i - 1].cwnd) {
3835                                         break;
3836                                 }
3837                         }
3838                         net->last_hs_used = indx;
3839                 }
3840         }
3841         if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) {
3842                 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_FR);
3843         }
3844 }
3845
3846 #endif
3847
3848
3849 static void
3850 sctp_cwnd_update(struct sctp_tcb *stcb,
3851     struct sctp_association *asoc,
3852     int accum_moved, int reneged_all, int will_exit)
3853 {
3854         struct sctp_nets *net;
3855
3856         /******************************/
3857         /* update cwnd and Early FR   */
3858         /******************************/
3859         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3860
3861 #ifdef JANA_CMT_FAST_RECOVERY
3862                 /*
3863                  * CMT fast recovery code. Need to debug.
3864                  */
3865                 if (net->fast_retran_loss_recovery && net->new_pseudo_cumack) {
3866                         if (compare_with_wrap(asoc->last_acked_seq,
3867                             net->fast_recovery_tsn, MAX_TSN) ||
3868                             (asoc->last_acked_seq == net->fast_recovery_tsn) ||
3869                             compare_with_wrap(net->pseudo_cumack, net->fast_recovery_tsn, MAX_TSN) ||
3870                             (net->pseudo_cumack == net->fast_recovery_tsn)) {
3871                                 net->will_exit_fast_recovery = 1;
3872                         }
3873                 }
3874 #endif
3875                 if (sctp_early_fr) {
3876                         /*
3877                          * So, first of all do we need to have a Early FR
3878                          * timer running?
3879                          */
3880                         if (((TAILQ_FIRST(&asoc->sent_queue)) &&
3881                             (net->ref_count > 1) &&
3882                             (net->flight_size < net->cwnd)) ||
3883                             (reneged_all)) {
3884                                 /*
3885                                  * yes, so in this case stop it if its
3886                                  * running, and then restart it. Reneging
3887                                  * all is a special case where we want to
3888                                  * run the Early FR timer and then force the
3889                                  * last few unacked to be sent, causing us
3890                                  * to illicit a sack with gaps to force out
3891                                  * the others.
3892                                  */
3893                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
3894                                         SCTP_STAT_INCR(sctps_earlyfrstpidsck2);
3895                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
3896                                             SCTP_FROM_SCTP_INDATA + SCTP_LOC_20);
3897                                 }
3898                                 SCTP_STAT_INCR(sctps_earlyfrstrid);
3899                                 sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net);
3900                         } else {
3901                                 /* No, stop it if its running */
3902                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
3903                                         SCTP_STAT_INCR(sctps_earlyfrstpidsck3);
3904                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
3905                                             SCTP_FROM_SCTP_INDATA + SCTP_LOC_21);
3906                                 }
3907                         }
3908                 }
3909                 /* if nothing was acked on this destination skip it */
3910                 if (net->net_ack == 0) {
3911                         if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
3912                                 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FROM_SACK);
3913                         }
3914                         continue;
3915                 }
3916                 if (net->net_ack2 > 0) {
3917                         /*
3918                          * Karn's rule applies to clearing error count, this
3919                          * is optional.
3920                          */
3921                         net->error_count = 0;
3922                         if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
3923                             SCTP_ADDR_NOT_REACHABLE) {
3924                                 /* addr came good */
3925                                 net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE;
3926                                 net->dest_state |= SCTP_ADDR_REACHABLE;
3927                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
3928                                     SCTP_RECEIVED_SACK, (void *)net);
3929                                 /* now was it the primary? if so restore */
3930                                 if (net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
3931                                         (void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net);
3932                                 }
3933                         }
3934                 }
3935 #ifdef JANA_CMT_FAST_RECOVERY
3936                 /*
3937                  * CMT fast recovery code
3938                  */
3939                 /*
3940                  * if (sctp_cmt_on_off == 1 &&
3941                  * net->fast_retran_loss_recovery &&
3942                  * net->will_exit_fast_recovery == 0) { // @@@ Do something
3943                  * }       else if (sctp_cmt_on_off == 0 &&
3944                  * asoc->fast_retran_loss_recovery && will_exit == 0) {
3945                  */
3946 #endif
3947
3948                 if (asoc->fast_retran_loss_recovery && will_exit == 0 && sctp_cmt_on_off == 0) {
3949                         /*
3950                          * If we are in loss recovery we skip any cwnd
3951                          * update
3952                          */
3953                         goto skip_cwnd_update;
3954                 }
3955                 /*
3956                  * CMT: CUC algorithm. Update cwnd if pseudo-cumack has
3957                  * moved.
3958                  */
3959                 if (accum_moved || (sctp_cmt_on_off && net->new_pseudo_cumack)) {
3960                         /* If the cumulative ack moved we can proceed */
3961                         if (net->cwnd <= net->ssthresh) {
3962                                 /* We are in slow start */
3963                                 if (net->flight_size + net->net_ack >=
3964                                     net->cwnd) {
3965 #ifdef SCTP_HIGH_SPEED
3966                                         sctp_hs_cwnd_increase(stcb, net);
3967 #else
3968                                         if (net->net_ack > (net->mtu * sctp_L2_abc_variable)) {
3969                                                 net->cwnd += (net->mtu * sctp_L2_abc_variable);
3970                                                 if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) {
3971                                                         sctp_log_cwnd(stcb, net, net->mtu,
3972                                                             SCTP_CWND_LOG_FROM_SS);
3973                                                 }
3974                                         } else {
3975                                                 net->cwnd += net->net_ack;
3976                                                 sctp_log_cwnd(stcb, net, net->net_ack,
3977                                                     SCTP_CWND_LOG_FROM_SS);
3978                                         }
3979 #endif
3980                                 } else {
3981                                         unsigned int dif;
3982
3983                                         dif = net->cwnd - (net->flight_size +
3984                                             net->net_ack);
3985                                         if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
3986                                                 sctp_log_cwnd(stcb, net, net->net_ack,
3987                                                     SCTP_CWND_LOG_NOADV_SS);
3988                                         }
3989                                 }
3990                         } else {
3991                                 /* We are in congestion avoidance */
3992                                 if (net->flight_size + net->net_ack >=
3993                                     net->cwnd) {
3994                                         /*
3995                                          * add to pba only if we had a
3996                                          * cwnd's worth (or so) in flight OR
3997                                          * the burst limit was applied.
3998                                          */
3999                                         net->partial_bytes_acked +=
4000                                             net->net_ack;
4001
4002                                         /*
4003                                          * Do we need to increase (if pba is
4004                                          * > cwnd)?
4005                                          */
4006                                         if (net->partial_bytes_acked >=
4007                                             net->cwnd) {
4008                                                 if (net->cwnd <
4009                                                     net->partial_bytes_acked) {
4010                                                         net->partial_bytes_acked -=
4011                                                             net->cwnd;
4012                                                 } else {
4013                                                         net->partial_bytes_acked =
4014                                                             0;
4015                                                 }
4016                                                 net->cwnd += net->mtu;
4017                                                 if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) {
4018                                                         sctp_log_cwnd(stcb, net, net->mtu,
4019                                                             SCTP_CWND_LOG_FROM_CA);
4020                                                 }
4021                                         } else {
4022                                                 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
4023                                                         sctp_log_cwnd(stcb, net, net->net_ack,
4024                                                             SCTP_CWND_LOG_NOADV_CA);
4025                                                 }
4026                                         }
4027                                 } else {
4028                                         unsigned int dif;
4029
4030                                         if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
4031                                                 sctp_log_cwnd(stcb, net, net->net_ack,
4032                                                     SCTP_CWND_LOG_NOADV_CA);
4033                                         }
4034                                         dif = net->cwnd - (net->flight_size +
4035                                             net->net_ack);
4036                                 }
4037                         }
4038                 } else {
4039                         if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
4040                                 sctp_log_cwnd(stcb, net, net->mtu,
4041                                     SCTP_CWND_LOG_NO_CUMACK);
4042                         }
4043                 }
4044 skip_cwnd_update:
4045                 /*
4046                  * NOW, according to Karn's rule do we need to restore the
4047                  * RTO timer back? Check our net_ack2. If not set then we
4048                  * have a ambiguity.. i.e. all data ack'd was sent to more
4049                  * than one place.
4050                  */
4051                 if (net->net_ack2) {
4052                         /* restore any doubled timers */
4053                         net->RTO = ((net->lastsa >> 2) + net->lastsv) >> 1;
4054                         if (net->RTO < stcb->asoc.minrto) {
4055                                 net->RTO = stcb->asoc.minrto;
4056                         }
4057                         if (net->RTO > stcb->asoc.maxrto) {
4058                                 net->RTO = stcb->asoc.maxrto;
4059                         }
4060                 }
4061         }
4062 }
4063
4064 static void
4065 sctp_fs_audit(struct sctp_association *asoc)
4066 {
4067         struct sctp_tmit_chunk *chk;
4068         int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
4069
4070         TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
4071                 if (chk->sent < SCTP_DATAGRAM_RESEND) {
4072                         inflight++;
4073                 } else if (chk->sent == SCTP_DATAGRAM_RESEND) {
4074                         resend++;
4075                 } else if (chk->sent < SCTP_DATAGRAM_ACKED) {
4076                         inbetween++;
4077                 } else if (chk->sent > SCTP_DATAGRAM_ACKED) {
4078                         above++;
4079                 } else {
4080                         acked++;
4081                 }
4082         }
4083
4084         if ((inflight > 0) || (inbetween > 0)) {
4085 #ifdef INVARIANTS
4086                 panic("Flight size-express incorrect? \n");
4087 #else
4088                 SCTP_PRINTF("Flight size-express incorrect inflight:%d inbetween:%d\n",
4089                     inflight, inbetween);
4090 #endif
4091         }
4092 }
4093
4094
4095 static void
4096 sctp_window_probe_recovery(struct sctp_tcb *stcb,
4097     struct sctp_association *asoc,
4098     struct sctp_nets *net,
4099     struct sctp_tmit_chunk *tp1)
4100 {
4101         struct sctp_tmit_chunk *chk;
4102
4103         /* First setup this one and get it moved back */
4104         tp1->sent = SCTP_DATAGRAM_UNSENT;
4105         tp1->window_probe = 0;
4106         if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
4107                 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
4108                     tp1->whoTo->flight_size,
4109                     tp1->book_size,
4110                     (uintptr_t) tp1->whoTo,
4111                     tp1->rec.data.TSN_seq);
4112         }
4113         sctp_flight_size_decrease(tp1);
4114         sctp_total_flight_decrease(stcb, tp1);
4115         TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4116         TAILQ_INSERT_HEAD(&asoc->send_queue, tp1, sctp_next);
4117         asoc->sent_queue_cnt--;
4118         asoc->send_queue_cnt++;
4119         /*
4120          * Now all guys marked for RESEND on the sent_queue must be moved
4121          * back too.
4122          */
4123         TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
4124                 if (chk->sent == SCTP_DATAGRAM_RESEND) {
4125                         /* Another chunk to move */
4126                         chk->sent = SCTP_DATAGRAM_UNSENT;
4127                         chk->window_probe = 0;
4128                         /* It should not be in flight */
4129                         TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4130                         TAILQ_INSERT_AFTER(&asoc->send_queue, tp1, chk, sctp_next);
4131                         asoc->sent_queue_cnt--;
4132                         asoc->send_queue_cnt++;
4133                         sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4134                 }
4135         }
4136 }
4137
4138
4139 void
4140 sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
4141     uint32_t rwnd, int nonce_sum_flag, int *abort_now)
4142 {
4143         struct sctp_nets *net;
4144         struct sctp_association *asoc;
4145         struct sctp_tmit_chunk *tp1, *tp2;
4146         uint32_t old_rwnd;
4147         int win_probe_recovery = 0;
4148         int win_probe_recovered = 0;
4149         int j, done_once = 0;
4150
4151
4152         if (sctp_logging_level & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4153                 sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
4154                     rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4155         }
4156         SCTP_TCB_LOCK_ASSERT(stcb);
4157         asoc = &stcb->asoc;
4158         old_rwnd = asoc->peers_rwnd;
4159         if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
4160                 /* old ack */
4161                 return;
4162         } else if (asoc->last_acked_seq == cumack) {
4163                 /* Window update sack */
4164                 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4165                     (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
4166                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4167                         /* SWS sender side engages */
4168                         asoc->peers_rwnd = 0;
4169                 }
4170                 if (asoc->peers_rwnd > old_rwnd) {
4171                         goto again;
4172                 }
4173                 return;
4174
4175         }
4176         /* First setup for CC stuff */
4177         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4178                 net->prev_cwnd = net->cwnd;
4179                 net->net_ack = 0;
4180                 net->net_ack2 = 0;
4181
4182                 /*
4183                  * CMT: Reset CUC and Fast recovery algo variables before
4184                  * SACK processing
4185                  */
4186                 net->new_pseudo_cumack = 0;
4187                 net->will_exit_fast_recovery = 0;
4188         }
4189         if (sctp_strict_sacks) {
4190                 uint32_t send_s;
4191
4192                 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4193                         tp1 = TAILQ_LAST(&asoc->sent_queue,
4194                             sctpchunk_listhead);
4195                         send_s = tp1->rec.data.TSN_seq + 1;
4196                 } else {
4197                         send_s = asoc->sending_seq;
4198                 }
4199                 if ((cumack == send_s) ||
4200                     compare_with_wrap(cumack, send_s, MAX_TSN)) {
4201 #ifndef INVARIANTS
4202                         struct mbuf *oper;
4203
4204 #endif
4205 #ifdef INVARIANTS
4206                         panic("Impossible sack 1");
4207 #else
4208                         *abort_now = 1;
4209                         /* XXX */
4210                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4211                             0, M_DONTWAIT, 1, MT_DATA);
4212                         if (oper) {
4213                                 struct sctp_paramhdr *ph;
4214                                 uint32_t *ippp;
4215
4216                                 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4217                                     sizeof(uint32_t);
4218                                 ph = mtod(oper, struct sctp_paramhdr *);
4219                                 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4220                                 ph->param_length = htons(SCTP_BUF_LEN(oper));
4221                                 ippp = (uint32_t *) (ph + 1);
4222                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4223                         }
4224                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4225                         sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper);
4226                         return;
4227 #endif
4228                 }
4229         }
4230         asoc->this_sack_highest_gap = cumack;
4231         stcb->asoc.overall_error_count = 0;
4232         if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
4233                 /* process the new consecutive TSN first */
4234                 tp1 = TAILQ_FIRST(&asoc->sent_queue);
4235                 while (tp1) {
4236                         tp2 = TAILQ_NEXT(tp1, sctp_next);
4237                         if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
4238                             MAX_TSN) ||
4239                             cumack == tp1->rec.data.TSN_seq) {
4240                                 if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4241                                         /*
4242                                          * ECN Nonce: Add the nonce to the
4243                                          * sender's nonce sum
4244                                          */
4245                                         asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4246                                         if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4247                                                 /*
4248                                                  * If it is less than ACKED,
4249                                                  * it is now no-longer in
4250                                                  * flight. Higher values may
4251                                                  * occur during marking
4252                                                  */
4253                                                 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4254                                                         if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
4255                                                                 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4256                                                                     tp1->whoTo->flight_size,
4257                                                                     tp1->book_size,
4258                                                                     (uintptr_t) tp1->whoTo,
4259                                                                     tp1->rec.data.TSN_seq);
4260                                                         }
4261                                                         sctp_flight_size_decrease(tp1);
4262                                                         sctp_total_flight_decrease(stcb, tp1);
4263                                                 }
4264                                                 tp1->whoTo->net_ack += tp1->send_size;
4265                                                 if (tp1->snd_count < 2) {
4266                                                         /*
4267                                                          * True
4268                                                          * non-retransmited
4269                                                          * chunk
4270                                                          */
4271                                                         tp1->whoTo->net_ack2 +=
4272                                                             tp1->send_size;
4273
4274                                                         /* update RTO too? */
4275                                                         if (tp1->do_rtt) {
4276                                                                 tp1->whoTo->RTO =
4277                                                                     sctp_calculate_rto(stcb,
4278                                                                     asoc, tp1->whoTo,
4279                                                                     &tp1->sent_rcv_time);
4280                                                                 tp1->do_rtt = 0;
4281                                                         }
4282                                                 }
4283                                                 /*
4284                                                  * CMT: CUCv2 algorithm.
4285                                                  * From the cumack'd TSNs,
4286                                                  * for each TSN being acked
4287                                                  * for the first time, set
4288                                                  * the following variables
4289                                                  * for the corresp
4290                                                  * destination.
4291                                                  * new_pseudo_cumack will
4292                                                  * trigger a cwnd update.
4293                                                  * find_(rtx_)pseudo_cumack
4294                                                  * will trigger search for
4295                                                  * the next expected
4296                                                  * (rtx-)pseudo-cumack.
4297                                                  */
4298                                                 tp1->whoTo->new_pseudo_cumack = 1;
4299                                                 tp1->whoTo->find_pseudo_cumack = 1;
4300                                                 tp1->whoTo->find_rtx_pseudo_cumack = 1;
4301
4302                                                 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
4303                                                         sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
4304                                                 }
4305                                         }
4306                                         if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4307                                                 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4308                                         }
4309                                         if (tp1->rec.data.chunk_was_revoked) {
4310                                                 /* deflate the cwnd */
4311                                                 tp1->whoTo->cwnd -= tp1->book_size;
4312                                                 tp1->rec.data.chunk_was_revoked = 0;
4313                                         }
4314                                         tp1->sent = SCTP_DATAGRAM_ACKED;
4315                                 }
4316                         } else {
4317                                 break;
4318                         }
4319                         TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4320                         if (tp1->data) {
4321                                 sctp_free_bufspace(stcb, asoc, tp1, 1);
4322                                 sctp_m_freem(tp1->data);
4323                         }
4324                         if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4325                                 sctp_log_sack(asoc->last_acked_seq,
4326                                     cumack,
4327                                     tp1->rec.data.TSN_seq,
4328                                     0,
4329                                     0,
4330                                     SCTP_LOG_FREE_SENT);
4331                         }
4332                         tp1->data = NULL;
4333                         asoc->sent_queue_cnt--;
4334                         sctp_free_remote_addr(tp1->whoTo);
4335                         sctp_free_a_chunk(stcb, tp1);
4336                         tp1 = tp2;
4337                 }
4338         }
4339         if (stcb->sctp_socket) {
4340                 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4341                 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
4342                         sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
4343                 }
4344                 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4345         } else {
4346                 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
4347                         sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
4348                 }
4349         }
4350
4351
4352         if (asoc->last_acked_seq != cumack)
4353                 sctp_cwnd_update(stcb, asoc, 1, 0, 0);
4354
4355         asoc->last_acked_seq = cumack;
4356
4357         if (TAILQ_EMPTY(&asoc->sent_queue)) {
4358                 /* nothing left in-flight */
4359                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4360                         net->flight_size = 0;
4361                         net->partial_bytes_acked = 0;
4362                 }
4363                 asoc->total_flight = 0;
4364                 asoc->total_flight_count = 0;
4365         }
4366         /* Fix up the a-p-a-p for future PR-SCTP sends */
4367         if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
4368                 asoc->advanced_peer_ack_point = cumack;
4369         }
4370         /* ECN Nonce updates */
4371         if (asoc->ecn_nonce_allowed) {
4372                 if (asoc->nonce_sum_check) {
4373                         if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
4374                                 if (asoc->nonce_wait_for_ecne == 0) {
4375                                         struct sctp_tmit_chunk *lchk;
4376
4377                                         lchk = TAILQ_FIRST(&asoc->send_queue);
4378                                         asoc->nonce_wait_for_ecne = 1;
4379                                         if (lchk) {
4380                                                 asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
4381                                         } else {
4382                                                 asoc->nonce_wait_tsn = asoc->sending_seq;
4383                                         }
4384                                 } else {
4385                                         if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
4386                                             (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
4387                                                 /*
4388                                                  * Misbehaving peer. We need
4389                                                  * to react to this guy
4390                                                  */
4391                                                 asoc->ecn_allowed = 0;
4392                                                 asoc->ecn_nonce_allowed = 0;
4393                                         }
4394                                 }
4395                         }
4396                 } else {
4397                         /* See if Resynchronization Possible */
4398                         if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
4399                                 asoc->nonce_sum_check = 1;
4400                                 /*
4401                                  * now we must calculate what the base is.
4402                                  * We do this based on two things, we know
4403                                  * the total's for all the segments
4404                                  * gap-acked in the SACK (none), We also
4405                                  * know the SACK's nonce sum, its in
4406                                  * nonce_sum_flag. So we can build a truth
4407                                  * table to back-calculate the new value of
4408                                  * asoc->nonce_sum_expect_base:
4409                                  * 
4410                                  * SACK-flag-Value         Seg-Sums Base 0 0 0
4411                                  * 1                    0 1 0 1 1 1
4412                                  * 1 0
4413                                  */
4414                                 asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
4415                         }
4416                 }
4417         }
4418         /* RWND update */
4419         asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4420             (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
4421         if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4422                 /* SWS sender side engages */
4423                 asoc->peers_rwnd = 0;
4424         }
4425         if (asoc->peers_rwnd > old_rwnd) {
4426                 win_probe_recovery = 1;
4427         }
4428         /* Now assure a timer where data is queued at */
4429 again:
4430         j = 0;
4431         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4432                 if (win_probe_recovery && (net->window_probe)) {
4433                         net->window_probe = 0;
4434                         win_probe_recovered = 1;
4435                         /*
4436                          * Find first chunk that was used with window probe
4437                          * and clear the sent
4438                          */
4439                         /* sa_ignore FREED_MEMORY */
4440                         TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4441                                 if (tp1->window_probe) {
4442                                         /* move back to data send queue */
4443                                         sctp_window_probe_recovery(stcb, asoc, net, tp1);
4444                                         break;
4445                                 }
4446                         }
4447                 }
4448                 if (net->flight_size) {
4449                         int to_ticks;
4450
4451                         if (net->RTO == 0) {
4452                                 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4453                         } else {
4454                                 to_ticks = MSEC_TO_TICKS(net->RTO);
4455                         }
4456                         j++;
4457                         (void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4458                             sctp_timeout_handler, &net->rxt_timer);
4459                 } else {
4460                         if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4461                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4462                                     stcb, net,
4463                                     SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4464                         }
4465                         if (sctp_early_fr) {
4466                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4467                                         SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4468                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4469                                             SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
4470                                 }
4471                         }
4472                 }
4473         }
4474         if ((j == 0) &&
4475             (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4476             (asoc->sent_queue_retran_cnt == 0) &&
4477             (win_probe_recovered == 0) &&
4478             (done_once == 0)) {
4479                 /* huh, this should not happen */
4480                 sctp_fs_audit(asoc);
4481                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4482                         net->flight_size = 0;
4483                 }
4484                 asoc->total_flight = 0;
4485                 asoc->total_flight_count = 0;
4486                 asoc->sent_queue_retran_cnt = 0;
4487                 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4488                         if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4489                                 sctp_flight_size_increase(tp1);
4490                                 sctp_total_flight_increase(stcb, tp1);
4491                         } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4492                                 asoc->sent_queue_retran_cnt++;
4493                         }
4494                 }
4495                 done_once = 1;
4496                 goto again;
4497         }
4498         /**********************************/
4499         /* Now what about shutdown issues */
4500         /**********************************/
4501         if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4502                 /* nothing left on sendqueue.. consider done */
4503                 /* clean up */
4504                 if ((asoc->stream_queue_cnt == 1) &&
4505                     ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4506                     (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4507                     (asoc->locked_on_sending)
4508                     ) {
4509                         struct sctp_stream_queue_pending *sp;
4510
4511                         /*
4512                          * I may be in a state where we got all across.. but
4513                          * cannot write more due to a shutdown... we abort
4514                          * since the user did not indicate EOR in this case.
4515                          * The sp will be cleaned during free of the asoc.
4516                          */
4517                         sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4518                             sctp_streamhead);
4519                         if ((sp) && (sp->length == 0) && (sp->msg_is_complete == 0)) {
4520                                 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4521                                 asoc->locked_on_sending = NULL;
4522                                 asoc->stream_queue_cnt--;
4523                         }
4524                 }
4525                 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4526                     (asoc->stream_queue_cnt == 0)) {
4527                         if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4528                                 /* Need to abort here */
4529                                 struct mbuf *oper;
4530
4531                 abort_out_now:
4532                                 *abort_now = 1;
4533                                 /* XXX */
4534                                 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4535                                     0, M_DONTWAIT, 1, MT_DATA);
4536                                 if (oper) {
4537                                         struct sctp_paramhdr *ph;
4538                                         uint32_t *ippp;
4539
4540                                         SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4541                                             sizeof(uint32_t);
4542                                         ph = mtod(oper, struct sctp_paramhdr *);
4543                                         ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4544                                         ph->param_length = htons(SCTP_BUF_LEN(oper));
4545                                         ippp = (uint32_t *) (ph + 1);
4546                                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4547                                 }
4548                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4549                                 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper);
4550                         } else {
4551                                 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4552                                     (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4553                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4554                                 }
4555                                 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
4556                                 sctp_stop_timers_for_shutdown(stcb);
4557                                 sctp_send_shutdown(stcb,
4558                                     stcb->asoc.primary_destination);
4559                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4560                                     stcb->sctp_ep, stcb, asoc->primary_destination);
4561                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4562                                     stcb->sctp_ep, stcb, asoc->primary_destination);
4563                         }
4564                 } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4565                     (asoc->stream_queue_cnt == 0)) {
4566                         if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4567                                 goto abort_out_now;
4568                         }
4569                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4570                         asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
4571                         sctp_send_shutdown_ack(stcb,
4572                             stcb->asoc.primary_destination);
4573
4574                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4575                             stcb->sctp_ep, stcb, asoc->primary_destination);
4576                 }
4577         }
4578         if (sctp_logging_level & SCTP_SACK_RWND_LOGGING_ENABLE) {
4579                 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4580                     rwnd,
4581                     stcb->asoc.peers_rwnd,
4582                     stcb->asoc.total_flight,
4583                     stcb->asoc.total_output_queue_size);
4584         }
4585 }
4586
4587
4588
4589 void
4590 sctp_handle_sack(struct mbuf *m, int offset,
4591     struct sctp_sack_chunk *ch, struct sctp_tcb *stcb,
4592     struct sctp_nets *net_from, int *abort_now, int sack_len, uint32_t rwnd)
4593 {
4594         struct sctp_association *asoc;
4595         struct sctp_sack *sack;
4596         struct sctp_tmit_chunk *tp1, *tp2;
4597         uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
4598                  this_sack_lowest_newack;
4599         uint32_t sav_cum_ack;
4600         uint16_t num_seg, num_dup;
4601         uint16_t wake_him = 0;
4602         unsigned int sack_length;
4603         uint32_t send_s = 0;
4604         long j;
4605         int accum_moved = 0;
4606         int will_exit_fast_recovery = 0;
4607         uint32_t a_rwnd, old_rwnd;
4608         int win_probe_recovery = 0;
4609         int win_probe_recovered = 0;
4610         struct sctp_nets *net = NULL;
4611         int nonce_sum_flag, ecn_seg_sums = 0;
4612         int done_once;
4613         uint8_t reneged_all = 0;
4614         uint8_t cmt_dac_flag;
4615
4616         /*
4617          * we take any chance we can to service our queues since we cannot
4618          * get awoken when the socket is read from :<
4619          */
4620         /*
4621          * Now perform the actual SACK handling: 1) Verify that it is not an
4622          * old sack, if so discard. 2) If there is nothing left in the send
4623          * queue (cum-ack is equal to last acked) then you have a duplicate
4624          * too, update any rwnd change and verify no timers are running.
4625          * then return. 3) Process any new consequtive data i.e. cum-ack
4626          * moved process these first and note that it moved. 4) Process any
4627          * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4628          * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4629          * sync up flightsizes and things, stop all timers and also check
4630          * for shutdown_pending state. If so then go ahead and send off the
4631          * shutdown. If in shutdown recv, send off the shutdown-ack and
4632          * start that timer, Ret. 9) Strike any non-acked things and do FR
4633          * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4634          * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4635          * if in shutdown_recv state.
4636          */
4637         SCTP_TCB_LOCK_ASSERT(stcb);
4638         sack = &ch->sack;
4639         /* CMT DAC algo */
4640         this_sack_lowest_newack = 0;
4641         j = 0;
4642         sack_length = (unsigned int)sack_len;
4643         /* ECN Nonce */
4644         SCTP_STAT_INCR(sctps_slowpath_sack);
4645         nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
4646         cum_ack = last_tsn = ntohl(sack->cum_tsn_ack);
4647         num_seg = ntohs(sack->num_gap_ack_blks);
4648         a_rwnd = rwnd;
4649
4650         if (sctp_logging_level & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4651                 sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4652                     rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4653         }
4654         /* CMT DAC algo */
4655         cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
4656         num_dup = ntohs(sack->num_dup_tsns);
4657
4658         old_rwnd = stcb->asoc.peers_rwnd;
4659         stcb->asoc.overall_error_count = 0;
4660         asoc = &stcb->asoc;
4661         if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4662                 sctp_log_sack(asoc->last_acked_seq,
4663                     cum_ack,
4664                     0,
4665                     num_seg,
4666                     num_dup,
4667                     SCTP_LOG_NEW_SACK);
4668         }
4669         if ((num_dup) && (sctp_logging_level & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
4670                 int off_to_dup, iii;
4671                 uint32_t *dupdata, dblock;
4672
4673                 off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) + sizeof(struct sctp_sack_chunk);
4674                 if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= sack_length) {
4675                         dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4676                             sizeof(uint32_t), (uint8_t *) & dblock);
4677                         off_to_dup += sizeof(uint32_t);
4678                         if (dupdata) {
4679                                 for (iii = 0; iii < num_dup; iii++) {
4680                                         sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4681                                         dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4682                                             sizeof(uint32_t), (uint8_t *) & dblock);
4683                                         if (dupdata == NULL)
4684                                                 break;
4685                                         off_to_dup += sizeof(uint32_t);
4686
4687
4688                                 }
4689                         }
4690                 } else {
4691                         SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d sack_len:%d num gaps:%d\n",
4692                             off_to_dup, num_dup, sack_length, num_seg);
4693                 }
4694         }
4695         if (sctp_strict_sacks) {
4696                 /* reality check */
4697                 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4698                         tp1 = TAILQ_LAST(&asoc->sent_queue,
4699                             sctpchunk_listhead);
4700                         send_s = tp1->rec.data.TSN_seq + 1;
4701                 } else {
4702                         send_s = asoc->sending_seq;
4703                 }
4704                 if (cum_ack == send_s ||
4705                     compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
4706 #ifndef INVARIANTS
4707                         struct mbuf *oper;
4708
4709 #endif
4710 #ifdef INVARIANTS
4711         hopeless_peer:
4712                         panic("Impossible sack 1");
4713 #else
4714
4715
4716                         /*
4717                          * no way, we have not even sent this TSN out yet.
4718                          * Peer is hopelessly messed up with us.
4719                          */
4720         hopeless_peer:
4721                         *abort_now = 1;
4722                         /* XXX */
4723                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4724                             0, M_DONTWAIT, 1, MT_DATA);
4725                         if (oper) {
4726                                 struct sctp_paramhdr *ph;
4727                                 uint32_t *ippp;
4728
4729                                 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4730                                     sizeof(uint32_t);
4731                                 ph = mtod(oper, struct sctp_paramhdr *);
4732                                 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4733                                 ph->param_length = htons(SCTP_BUF_LEN(oper));
4734                                 ippp = (uint32_t *) (ph + 1);
4735                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4736                         }
4737                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4738                         sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper);
4739                         return;
4740 #endif
4741                 }
4742         }
4743         /**********************/
4744         /* 1) check the range */
4745         /**********************/
4746         if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
4747                 /* acking something behind */
4748                 return;
4749         }
4750         sav_cum_ack = asoc->last_acked_seq;
4751
4752         /* update the Rwnd of the peer */
4753         if (TAILQ_EMPTY(&asoc->sent_queue) &&
4754             TAILQ_EMPTY(&asoc->send_queue) &&
4755             (asoc->stream_queue_cnt == 0)
4756             ) {
4757                 /* nothing left on send/sent and strmq */
4758                 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
4759                         sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4760                             asoc->peers_rwnd, 0, 0, a_rwnd);
4761                 }
4762                 asoc->peers_rwnd = a_rwnd;
4763                 if (asoc->sent_queue_retran_cnt) {
4764                         asoc->sent_queue_retran_cnt = 0;
4765                 }
4766                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4767                         /* SWS sender side engages */
4768                         asoc->peers_rwnd = 0;
4769                 }
4770                 /* stop any timers */
4771                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4772                         sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4773                             stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4774                         if (sctp_early_fr) {
4775                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4776                                         SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
4777                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4778                                             SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4779                                 }
4780                         }
4781                         net->partial_bytes_acked = 0;
4782                         net->flight_size = 0;
4783                 }
4784                 asoc->total_flight = 0;
4785                 asoc->total_flight_count = 0;
4786                 return;
4787         }
4788         /*
4789          * We init netAckSz and netAckSz2 to 0. These are used to track 2
4790          * things. The total byte count acked is tracked in netAckSz AND
4791          * netAck2 is used to track the total bytes acked that are un-
4792          * amibguious and were never retransmitted. We track these on a per
4793          * destination address basis.
4794          */
4795         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4796                 net->prev_cwnd = net->cwnd;
4797                 net->net_ack = 0;
4798                 net->net_ack2 = 0;
4799
4800                 /*
4801                  * CMT: Reset CUC and Fast recovery algo variables before
4802                  * SACK processing
4803                  */
4804                 net->new_pseudo_cumack = 0;
4805                 net->will_exit_fast_recovery = 0;
4806         }
4807         /* process the new consecutive TSN first */
4808         tp1 = TAILQ_FIRST(&asoc->sent_queue);
4809         while (tp1) {
4810                 if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
4811                     MAX_TSN) ||
4812                     last_tsn == tp1->rec.data.TSN_seq) {
4813                         if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4814                                 /*
4815                                  * ECN Nonce: Add the nonce to the sender's
4816                                  * nonce sum
4817                                  */
4818                                 asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4819                                 accum_moved = 1;
4820                                 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4821                                         /*
4822                                          * If it is less than ACKED, it is
4823                                          * now no-longer in flight. Higher
4824                                          * values may occur during marking
4825                                          */
4826                                         if ((tp1->whoTo->dest_state &
4827                                             SCTP_ADDR_UNCONFIRMED) &&
4828                                             (tp1->snd_count < 2)) {
4829                                                 /*
4830                                                  * If there was no retran
4831                                                  * and the address is
4832                                                  * un-confirmed and we sent
4833                                                  * there and are now
4834                                                  * sacked.. its confirmed,
4835                                                  * mark it so.
4836                                                  */
4837                                                 tp1->whoTo->dest_state &=
4838                                                     ~SCTP_ADDR_UNCONFIRMED;
4839                                         }
4840                                         if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4841                                                 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
4842                                                         sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4843                                                             tp1->whoTo->flight_size,
4844                                                             tp1->book_size,
4845                                                             (uintptr_t) tp1->whoTo,
4846                                                             tp1->rec.data.TSN_seq);
4847                                                 }
4848                                                 sctp_flight_size_decrease(tp1);
4849                                                 sctp_total_flight_decrease(stcb, tp1);
4850                                         }
4851                                         tp1->whoTo->net_ack += tp1->send_size;
4852
4853                                         /* CMT SFR and DAC algos */
4854                                         this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4855                                         tp1->whoTo->saw_newack = 1;
4856
4857                                         if (tp1->snd_count < 2) {
4858                                                 /*
4859                                                  * True non-retransmited
4860                                                  * chunk
4861                                                  */
4862                                                 tp1->whoTo->net_ack2 +=
4863                                                     tp1->send_size;
4864
4865                                                 /* update RTO too? */
4866                                                 if (tp1->do_rtt) {
4867                                                         tp1->whoTo->RTO =
4868                                                             sctp_calculate_rto(stcb,
4869                                                             asoc, tp1->whoTo,
4870                                                             &tp1->sent_rcv_time);
4871                                                         tp1->do_rtt = 0;
4872                                                 }
4873                                         }
4874                                         /*
4875                                          * CMT: CUCv2 algorithm. From the
4876                                          * cumack'd TSNs, for each TSN being
4877                                          * acked for the first time, set the
4878                                          * following variables for the
4879                                          * corresp destination.
4880                                          * new_pseudo_cumack will trigger a
4881                                          * cwnd update.
4882                                          * find_(rtx_)pseudo_cumack will
4883                                          * trigger search for the next
4884                                          * expected (rtx-)pseudo-cumack.
4885                                          */
4886                                         tp1->whoTo->new_pseudo_cumack = 1;
4887                                         tp1->whoTo->find_pseudo_cumack = 1;
4888                                         tp1->whoTo->find_rtx_pseudo_cumack = 1;
4889
4890
4891                                         if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4892                                                 sctp_log_sack(asoc->last_acked_seq,
4893                                                     cum_ack,
4894                                                     tp1->rec.data.TSN_seq,
4895                                                     0,
4896                                                     0,
4897                                                     SCTP_LOG_TSN_ACKED);
4898                                         }
4899                                         if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
4900                                                 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
4901                                         }
4902                                 }
4903                                 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4904                                         sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4905 #ifdef SCTP_AUDITING_ENABLED
4906                                         sctp_audit_log(0xB3,
4907                                             (asoc->sent_queue_retran_cnt & 0x000000ff));
4908 #endif
4909                                 }
4910                                 if (tp1->rec.data.chunk_was_revoked) {
4911                                         /* deflate the cwnd */
4912                                         tp1->whoTo->cwnd -= tp1->book_size;
4913                                         tp1->rec.data.chunk_was_revoked = 0;
4914                                 }
4915                                 tp1->sent = SCTP_DATAGRAM_ACKED;
4916                         }
4917                 } else {
4918                         break;
4919                 }
4920                 tp1 = TAILQ_NEXT(tp1, sctp_next);
4921         }
4922         biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4923         /* always set this up to cum-ack */
4924         asoc->this_sack_highest_gap = last_tsn;
4925
4926         /* Move offset up to point to gaps/dups */
4927         offset += sizeof(struct sctp_sack_chunk);
4928         if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_sack_chunk)) > sack_length) {
4929
4930                 /* skip corrupt segments */
4931                 goto skip_segments;
4932         }
4933         if (num_seg > 0) {
4934
4935                 /*
4936                  * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4937                  * to be greater than the cumack. Also reset saw_newack to 0
4938                  * for all dests.
4939                  */
4940                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4941                         net->saw_newack = 0;
4942                         net->this_sack_highest_newack = last_tsn;
4943                 }
4944
4945                 /*
4946                  * thisSackHighestGap will increase while handling NEW
4947                  * segments this_sack_highest_newack will increase while
4948                  * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4949                  * used for CMT DAC algo. saw_newack will also change.
4950                  */
4951                 sctp_handle_segments(m, &offset, stcb, asoc, ch, last_tsn,
4952                     &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
4953                     num_seg, &ecn_seg_sums);
4954
4955                 if (sctp_strict_sacks) {
4956                         /*
4957                          * validate the biggest_tsn_acked in the gap acks if
4958                          * strict adherence is wanted.
4959                          */
4960                         if ((biggest_tsn_acked == send_s) ||
4961                             (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
4962                                 /*
4963                                  * peer is either confused or we are under
4964                                  * attack. We must abort.
4965                                  */
4966                                 goto hopeless_peer;
4967                         }
4968                 }
4969         }
4970 skip_segments:
4971         /*******************************************/
4972         /* cancel ALL T3-send timer if accum moved */
4973         /*******************************************/
4974         if (sctp_cmt_on_off) {
4975                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4976                         if (net->new_pseudo_cumack)
4977                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4978                                     stcb, net,
4979                                     SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4980
4981                 }
4982         } else {
4983                 if (accum_moved) {
4984                         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4985                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4986                                     stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4987                         }
4988                 }
4989         }
4990         /********************************************/
4991         /* drop the acked chunks from the sendqueue */
4992         /********************************************/
4993         asoc->last_acked_seq = cum_ack;
4994
4995         tp1 = TAILQ_FIRST(&asoc->sent_queue);
4996         if (tp1 == NULL)
4997                 goto done_with_it;
4998         do {
4999                 if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
5000                     MAX_TSN)) {
5001                         break;
5002                 }
5003                 if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
5004                         /* no more sent on list */
5005                         break;
5006                 }
5007                 tp2 = TAILQ_NEXT(tp1, sctp_next);
5008                 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
5009                 /*
5010                  * Friendlier printf in lieu of panic now that I think its
5011                  * fixed
5012                  */
5013
5014                 if (tp1->pr_sctp_on) {
5015                         if (asoc->pr_sctp_cnt != 0)
5016                                 asoc->pr_sctp_cnt--;
5017                 }
5018                 if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
5019                     (asoc->total_flight > 0)) {
5020 #ifdef INVARIANTS
5021                         panic("Warning flight size is postive and should be 0");
5022 #else
5023                         SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
5024                             asoc->total_flight);
5025 #endif
5026                         asoc->total_flight = 0;
5027                 }
5028                 if (tp1->data) {
5029                         sctp_free_bufspace(stcb, asoc, tp1, 1);
5030                         sctp_m_freem(tp1->data);
5031                         if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
5032                                 asoc->sent_queue_cnt_removeable--;
5033                         }
5034                 }
5035                 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
5036                         sctp_log_sack(asoc->last_acked_seq,
5037                             cum_ack,
5038                             tp1->rec.data.TSN_seq,
5039                             0,
5040                             0,
5041                             SCTP_LOG_FREE_SENT);
5042                 }
5043                 tp1->data = NULL;
5044                 asoc->sent_queue_cnt--;
5045                 sctp_free_remote_addr(tp1->whoTo);
5046
5047                 sctp_free_a_chunk(stcb, tp1);
5048                 wake_him++;
5049                 tp1 = tp2;
5050         } while (tp1 != NULL);
5051
5052 done_with_it:
5053         if ((wake_him) && (stcb->sctp_socket)) {
5054                 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
5055                 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
5056                         sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
5057                 }
5058                 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
5059         } else {
5060                 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
5061                         sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
5062                 }
5063         }
5064
5065         if (asoc->fast_retran_loss_recovery && accum_moved) {
5066                 if (compare_with_wrap(asoc->last_acked_seq,
5067                     asoc->fast_recovery_tsn, MAX_TSN) ||
5068                     asoc->last_acked_seq == asoc->fast_recovery_tsn) {
5069                         /* Setup so we will exit RFC2582 fast recovery */
5070                         will_exit_fast_recovery = 1;
5071                 }
5072         }
5073         /*
5074          * Check for revoked fragments:
5075          * 
5076          * if Previous sack - Had no frags then we can't have any revoked if
5077          * Previous sack - Had frag's then - If we now have frags aka
5078          * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
5079          * some of them. else - The peer revoked all ACKED fragments, since
5080          * we had some before and now we have NONE.
5081          */
5082
5083         if (num_seg)
5084                 sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
5085         else if (asoc->saw_sack_with_frags) {
5086                 int cnt_revoked = 0;
5087
5088                 tp1 = TAILQ_FIRST(&asoc->sent_queue);
5089                 if (tp1 != NULL) {
5090                         /* Peer revoked all dg's marked or acked */
5091                         TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5092                                 if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
5093                                     (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
5094                                         tp1->sent = SCTP_DATAGRAM_SENT;
5095                                         if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
5096                                                 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
5097                                                     tp1->whoTo->flight_size,
5098                                                     tp1->book_size,
5099                                                     (uintptr_t) tp1->whoTo,
5100                                                     tp1->rec.data.TSN_seq);
5101                                         }
5102                                         sctp_flight_size_increase(tp1);
5103                                         sctp_total_flight_increase(stcb, tp1);
5104                                         tp1->rec.data.chunk_was_revoked = 1;
5105                                         /*
5106                                          * To ensure that this increase in
5107                                          * flightsize, which is artificial,
5108                                          * does not throttle the sender, we
5109                                          * also increase the cwnd
5110                                          * artificially.
5111                                          */
5112                                         tp1->whoTo->cwnd += tp1->book_size;
5113                                         cnt_revoked++;
5114                                 }
5115                         }
5116                         if (cnt_revoked) {
5117                                 reneged_all = 1;
5118                         }
5119                 }
5120                 asoc->saw_sack_with_frags = 0;
5121         }
5122         if (num_seg)
5123                 asoc->saw_sack_with_frags = 1;
5124         else
5125                 asoc->saw_sack_with_frags = 0;
5126
5127
5128         sctp_cwnd_update(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
5129
5130         if (TAILQ_EMPTY(&asoc->sent_queue)) {
5131                 /* nothing left in-flight */
5132                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5133                         /* stop all timers */
5134                         if (sctp_early_fr) {
5135                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5136                                         SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5137                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5138                                             SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
5139                                 }
5140                         }
5141                         sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5142                             stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
5143                         net->flight_size = 0;
5144                         net->partial_bytes_acked = 0;
5145                 }
5146                 asoc->total_flight = 0;
5147                 asoc->total_flight_count = 0;
5148         }
5149         /**********************************/
5150         /* Now what about shutdown issues */
5151         /**********************************/
5152         if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
5153                 /* nothing left on sendqueue.. consider done */
5154                 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
5155                         sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5156                             asoc->peers_rwnd, 0, 0, a_rwnd);
5157                 }
5158                 asoc->peers_rwnd = a_rwnd;
5159                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5160                         /* SWS sender side engages */
5161                         asoc->peers_rwnd = 0;
5162                 }
5163                 /* clean up */
5164                 if ((asoc->stream_queue_cnt == 1) &&
5165                     ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
5166                     (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
5167                     (asoc->locked_on_sending)
5168                     ) {
5169                         struct sctp_stream_queue_pending *sp;
5170
5171                         /*
5172                          * I may be in a state where we got all across.. but
5173                          * cannot write more due to a shutdown... we abort
5174                          * since the user did not indicate EOR in this case.
5175                          */
5176                         sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
5177                             sctp_streamhead);
5178                         if ((sp) && (sp->length == 0) && (sp->msg_is_complete == 0)) {
5179                                 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
5180                                 asoc->locked_on_sending = NULL;
5181                                 asoc->stream_queue_cnt--;
5182                         }
5183                 }
5184                 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
5185                     (asoc->stream_queue_cnt == 0)) {
5186                         if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5187                                 /* Need to abort here */
5188                                 struct mbuf *oper;
5189
5190                 abort_out_now:
5191                                 *abort_now = 1;
5192                                 /* XXX */
5193                                 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
5194                                     0, M_DONTWAIT, 1, MT_DATA);
5195                                 if (oper) {
5196                                         struct sctp_paramhdr *ph;
5197                                         uint32_t *ippp;
5198
5199                                         SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5200                                             sizeof(uint32_t);
5201                                         ph = mtod(oper, struct sctp_paramhdr *);
5202                                         ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
5203                                         ph->param_length = htons(SCTP_BUF_LEN(oper));
5204                                         ippp = (uint32_t *) (ph + 1);
5205                                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
5206                                 }
5207                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
5208                                 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper);
5209                                 return;
5210                         } else {
5211                                 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
5212                                     (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
5213                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5214                                 }
5215                                 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
5216                                 sctp_stop_timers_for_shutdown(stcb);
5217                                 sctp_send_shutdown(stcb,
5218                                     stcb->asoc.primary_destination);
5219                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
5220                                     stcb->sctp_ep, stcb, asoc->primary_destination);
5221                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
5222                                     stcb->sctp_ep, stcb, asoc->primary_destination);
5223                         }
5224                         return;
5225                 } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
5226                     (asoc->stream_queue_cnt == 0)) {
5227                         if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5228                                 goto abort_out_now;
5229                         }
5230                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5231                         asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
5232                         sctp_send_shutdown_ack(stcb,
5233                             stcb->asoc.primary_destination);
5234
5235                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
5236                             stcb->sctp_ep, stcb, asoc->primary_destination);
5237                         return;
5238                 }
5239         }
5240         /*
5241          * Now here we are going to recycle net_ack for a different use...
5242          * HEADS UP.
5243          */
5244         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5245                 net->net_ack = 0;
5246         }
5247
5248         /*
5249          * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5250          * to be done. Setting this_sack_lowest_newack to the cum_ack will
5251          * automatically ensure that.
5252          */
5253         if (sctp_cmt_on_off && sctp_cmt_use_dac && (cmt_dac_flag == 0)) {
5254                 this_sack_lowest_newack = cum_ack;
5255         }
5256         if (num_seg > 0) {
5257                 sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5258                     biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5259         }
5260         /*********************************************/
5261         /* Here we perform PR-SCTP procedures        */
5262         /* (section 4.2)                             */
5263         /*********************************************/
5264         /* C1. update advancedPeerAckPoint */
5265         if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
5266                 asoc->advanced_peer_ack_point = cum_ack;
5267         }
5268         /* C2. try to further move advancedPeerAckPoint ahead */
5269
5270         if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5271                 struct sctp_tmit_chunk *lchk;
5272
5273                 lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5274                 /* C3. See if we need to send a Fwd-TSN */
5275                 if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
5276                     MAX_TSN)) {
5277                         /*
5278                          * ISSUE with ECN, see FWD-TSN processing for notes
5279                          * on issues that will occur when the ECN NONCE
5280                          * stuff is put into SCTP for cross checking.
5281                          */
5282                         send_forward_tsn(stcb, asoc);
5283
5284                         /*
5285                          * ECN Nonce: Disable Nonce Sum check when FWD TSN
5286                          * is sent and store resync tsn
5287                          */
5288                         asoc->nonce_sum_check = 0;
5289                         asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
5290                         if (lchk) {
5291                                 /* Assure a timer is up */
5292                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5293                                     stcb->sctp_ep, stcb, lchk->whoTo);
5294                         }
5295                 }
5296         }
5297         /*
5298          * CMT fast recovery code. Need to debug. ((sctp_cmt_on_off == 1) &&
5299          * (net->fast_retran_loss_recovery == 0)))
5300          */
5301         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5302                 if ((asoc->fast_retran_loss_recovery == 0) || (sctp_cmt_on_off == 1)) {
5303                         /* out of a RFC2582 Fast recovery window? */
5304                         if (net->net_ack > 0) {
5305                                 /*
5306                                  * per section 7.2.3, are there any
5307                                  * destinations that had a fast retransmit
5308                                  * to them. If so what we need to do is
5309                                  * adjust ssthresh and cwnd.
5310                                  */
5311                                 struct sctp_tmit_chunk *lchk;
5312
5313 #ifdef  SCTP_HIGH_SPEED
5314                                 sctp_hs_cwnd_decrease(stcb, net);
5315 #else
5316                                 int old_cwnd = net->cwnd;
5317
5318                                 net->ssthresh = net->cwnd / 2;
5319                                 if (net->ssthresh < (net->mtu * 2)) {
5320                                         net->ssthresh = 2 * net->mtu;
5321                                 }
5322                                 net->cwnd = net->ssthresh;
5323                                 if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) {
5324                                         sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd),
5325                                             SCTP_CWND_LOG_FROM_FR);
5326                                 }
5327 #endif
5328
5329                                 lchk = TAILQ_FIRST(&asoc->send_queue);
5330
5331                                 net->partial_bytes_acked = 0;
5332                                 /* Turn on fast recovery window */
5333                                 asoc->fast_retran_loss_recovery = 1;
5334                                 if (lchk == NULL) {
5335                                         /* Mark end of the window */
5336                                         asoc->fast_recovery_tsn = asoc->sending_seq - 1;
5337                                 } else {
5338                                         asoc->fast_recovery_tsn = lchk->rec.data.TSN_seq - 1;
5339                                 }
5340
5341                                 /*
5342                                  * CMT fast recovery -- per destination
5343                                  * recovery variable.
5344                                  */
5345                                 net->fast_retran_loss_recovery = 1;
5346
5347                                 if (lchk == NULL) {
5348                                         /* Mark end of the window */
5349                                         net->fast_recovery_tsn = asoc->sending_seq - 1;
5350                                 } else {
5351                                         net->fast_recovery_tsn = lchk->rec.data.TSN_seq - 1;
5352                                 }
5353
5354
5355
5356                                 /*
5357                                  * Disable Nonce Sum Checking and store the
5358                                  * resync tsn
5359                                  */
5360                                 asoc->nonce_sum_check = 0;
5361                                 asoc->nonce_resync_tsn = asoc->fast_recovery_tsn + 1;
5362
5363                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND,
5364                                     stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_32);
5365                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5366                                     stcb->sctp_ep, stcb, net);
5367                         }
5368                 } else if (net->net_ack > 0) {
5369                         /*
5370                          * Mark a peg that we WOULD have done a cwnd
5371                          * reduction but RFC2582 prevented this action.
5372                          */
5373                         SCTP_STAT_INCR(sctps_fastretransinrtt);
5374                 }
5375         }
5376
5377
5378         /******************************************************************
5379          *  Here we do the stuff with ECN Nonce checking.
5380          *  We basically check to see if the nonce sum flag was incorrect
5381          *  or if resynchronization needs to be done. Also if we catch a
5382          *  misbehaving receiver we give him the kick.
5383          ******************************************************************/
5384
5385         if (asoc->ecn_nonce_allowed) {
5386                 if (asoc->nonce_sum_check) {
5387                         if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
5388                                 if (asoc->nonce_wait_for_ecne == 0) {
5389                                         struct sctp_tmit_chunk *lchk;
5390
5391                                         lchk = TAILQ_FIRST(&asoc->send_queue);
5392                                         asoc->nonce_wait_for_ecne = 1;
5393                                         if (lchk) {
5394                                                 asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
5395                                         } else {
5396                                                 asoc->nonce_wait_tsn = asoc->sending_seq;
5397                                         }
5398                                 } else {
5399                                         if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
5400                                             (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
5401                                                 /*
5402                                                  * Misbehaving peer. We need
5403                                                  * to react to this guy
5404                                                  */
5405                                                 asoc->ecn_allowed = 0;
5406                                                 asoc->ecn_nonce_allowed = 0;
5407                                         }
5408                                 }
5409                         }
5410                 } else {
5411                         /* See if Resynchronization Possible */
5412                         if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
5413                                 asoc->nonce_sum_check = 1;
5414                                 /*
5415                                  * now we must calculate what the base is.
5416                                  * We do this based on two things, we know
5417                                  * the total's for all the segments
5418                                  * gap-acked in the SACK, its stored in
5419                                  * ecn_seg_sums. We also know the SACK's
5420                                  * nonce sum, its in nonce_sum_flag. So we
5421                                  * can build a truth table to back-calculate
5422                                  * the new value of
5423                                  * asoc->nonce_sum_expect_base:
5424                                  * 
5425                                  * SACK-flag-Value         Seg-Sums Base 0 0 0
5426                                  * 1                    0 1 0 1 1 1
5427                                  * 1 0
5428                                  */
5429                                 asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
5430                         }
5431                 }
5432         }
5433         /* Now are we exiting loss recovery ? */
5434         if (will_exit_fast_recovery) {
5435                 /* Ok, we must exit fast recovery */
5436                 asoc->fast_retran_loss_recovery = 0;
5437         }
5438         if ((asoc->sat_t3_loss_recovery) &&
5439             ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
5440             MAX_TSN) ||
5441             (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
5442                 /* end satellite t3 loss recovery */
5443                 asoc->sat_t3_loss_recovery = 0;
5444         }
5445         /*
5446          * CMT Fast recovery
5447          */
5448         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5449                 if (net->will_exit_fast_recovery) {
5450                         /* Ok, we must exit fast recovery */
5451                         net->fast_retran_loss_recovery = 0;
5452                 }
5453         }
5454
5455         /* Adjust and set the new rwnd value */
5456         if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
5457                 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5458                     asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * sctp_peer_chunk_oh), a_rwnd);
5459         }
5460         asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
5461             (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
5462         if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5463                 /* SWS sender side engages */
5464                 asoc->peers_rwnd = 0;
5465         }
5466         if (asoc->peers_rwnd > old_rwnd) {
5467                 win_probe_recovery = 1;
5468         }
5469         /*
5470          * Now we must setup so we have a timer up for anyone with
5471          * outstanding data.
5472          */
5473         done_once = 0;
5474 again:
5475         j = 0;
5476         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5477                 if (win_probe_recovery && (net->window_probe)) {
5478                         net->window_probe = 0;
5479                         win_probe_recovered = 1;
5480                         /*-
5481                          * Find first chunk that was used with
5482                          * window probe and clear the event. Put
5483                          * it back into the send queue as if has
5484                          * not been sent.
5485                          */
5486                         TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5487                                 if (tp1->window_probe) {
5488                                         sctp_window_probe_recovery(stcb, asoc, net, tp1);
5489                                         break;
5490                                 }
5491                         }
5492                 }
5493                 if (net->flight_size) {
5494                         j++;
5495                         sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5496                             stcb->sctp_ep, stcb, net);
5497                 } else {
5498                         if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5499                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5500                                     stcb, net,
5501                                     SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5502                         }
5503                         if (sctp_early_fr) {
5504                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5505                                         SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5506                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5507                                             SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
5508                                 }
5509                         }
5510                 }
5511         }
5512         if ((j == 0) &&
5513             (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5514             (asoc->sent_queue_retran_cnt == 0) &&
5515             (win_probe_recovered == 0) &&
5516             (done_once == 0)) {
5517                 /* huh, this should not happen */
5518                 sctp_fs_audit(asoc);
5519                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5520                         net->flight_size = 0;
5521                 }
5522                 asoc->total_flight = 0;
5523                 asoc->total_flight_count = 0;
5524                 asoc->sent_queue_retran_cnt = 0;
5525                 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5526                         if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5527                                 sctp_flight_size_increase(tp1);
5528                                 sctp_total_flight_increase(stcb, tp1);
5529                         } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5530                                 asoc->sent_queue_retran_cnt++;
5531                         }
5532                 }
5533                 done_once = 1;
5534                 goto again;
5535         }
5536         if (sctp_logging_level & SCTP_SACK_RWND_LOGGING_ENABLE) {
5537                 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5538                     a_rwnd,
5539                     stcb->asoc.peers_rwnd,
5540                     stcb->asoc.total_flight,
5541                     stcb->asoc.total_output_queue_size);
5542         }
5543 }
5544
5545 void
5546 sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
5547     struct sctp_nets *netp, int *abort_flag)
5548 {
5549         /* Copy cum-ack */
5550         uint32_t cum_ack, a_rwnd;
5551
5552         cum_ack = ntohl(cp->cumulative_tsn_ack);
5553         /* Arrange so a_rwnd does NOT change */
5554         a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5555
5556         /* Now call the express sack handling */
5557         sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 0, abort_flag);
5558 }
5559
5560 static void
5561 sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5562     struct sctp_stream_in *strmin)
5563 {
5564         struct sctp_queued_to_read *ctl, *nctl;
5565         struct sctp_association *asoc;
5566         int tt;
5567
5568         asoc = &stcb->asoc;
5569         tt = strmin->last_sequence_delivered;
5570         /*
5571          * First deliver anything prior to and including the stream no that
5572          * came in
5573          */
5574         ctl = TAILQ_FIRST(&strmin->inqueue);
5575         while (ctl) {
5576                 nctl = TAILQ_NEXT(ctl, next);
5577                 if (compare_with_wrap(tt, ctl->sinfo_ssn, MAX_SEQ) ||
5578                     (tt == ctl->sinfo_ssn)) {
5579                         /* this is deliverable now */
5580                         TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5581                         /* subtract pending on streams */
5582                         asoc->size_on_all_streams -= ctl->length;
5583                         sctp_ucount_decr(asoc->cnt_on_all_streams);
5584                         /* deliver it to at least the delivery-q */
5585                         if (stcb->sctp_socket) {
5586                                 sctp_add_to_readq(stcb->sctp_ep, stcb,
5587                                     ctl,
5588                                     &stcb->sctp_socket->so_rcv, 1);
5589                         }
5590                 } else {
5591                         /* no more delivery now. */
5592                         break;
5593                 }
5594                 ctl = nctl;
5595         }
5596         /*
5597          * now we must deliver things in queue the normal way  if any are
5598          * now ready.
5599          */
5600         tt = strmin->last_sequence_delivered + 1;
5601         ctl = TAILQ_FIRST(&strmin->inqueue);
5602         while (ctl) {
5603                 nctl = TAILQ_NEXT(ctl, next);
5604                 if (tt == ctl->sinfo_ssn) {
5605                         /* this is deliverable now */
5606                         TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5607                         /* subtract pending on streams */
5608                         asoc->size_on_all_streams -= ctl->length;
5609                         sctp_ucount_decr(asoc->cnt_on_all_streams);
5610                         /* deliver it to at least the delivery-q */
5611                         strmin->last_sequence_delivered = ctl->sinfo_ssn;
5612                         if (stcb->sctp_socket) {
5613                                 sctp_add_to_readq(stcb->sctp_ep, stcb,
5614                                     ctl,
5615                                     &stcb->sctp_socket->so_rcv, 1);
5616                         }
5617                         tt = strmin->last_sequence_delivered + 1;
5618                 } else {
5619                         break;
5620                 }
5621                 ctl = nctl;
5622         }
5623 }
5624
5625 void
5626 sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5627     struct sctp_forward_tsn_chunk *fwd, int *abort_flag)
5628 {
5629         /*
5630          * ISSUES that MUST be fixed for ECN! When we are the sender of the
5631          * forward TSN, when the SACK comes back that acknowledges the
5632          * FWD-TSN we must reset the NONCE sum to match correctly. This will
5633          * get quite tricky since we may have sent more data interveneing
5634          * and must carefully account for what the SACK says on the nonce
5635          * and any gaps that are reported. This work will NOT be done here,
5636          * but I note it here since it is really related to PR-SCTP and
5637          * FWD-TSN's
5638          */
5639
5640         /* The pr-sctp fwd tsn */
5641         /*
5642          * here we will perform all the data receiver side steps for
5643          * processing FwdTSN, as required in by pr-sctp draft:
5644          * 
5645          * Assume we get FwdTSN(x):
5646          * 
5647          * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5648          * others we have 3) examine and update re-ordering queue on
5649          * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5650          * report where we are.
5651          */
5652         struct sctp_strseq *stseq;
5653         struct sctp_association *asoc;
5654         uint32_t new_cum_tsn, gap, back_out_htsn;
5655         unsigned int i, cnt_gone, fwd_sz, cumack_set_flag, m_size;
5656         struct sctp_stream_in *strm;
5657         struct sctp_tmit_chunk *chk, *at;
5658
5659         cumack_set_flag = 0;
5660         asoc = &stcb->asoc;
5661         cnt_gone = 0;
5662         if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5663                 SCTPDBG(SCTP_DEBUG_INDATA1,
5664                     "Bad size too small/big fwd-tsn\n");
5665                 return;
5666         }
5667         m_size = (stcb->asoc.mapping_array_size << 3);
5668         /*************************************************************/
5669         /* 1. Here we update local cumTSN and shift the bitmap array */
5670         /*************************************************************/
5671         new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5672
5673         if (compare_with_wrap(asoc->cumulative_tsn, new_cum_tsn, MAX_TSN) ||
5674             asoc->cumulative_tsn == new_cum_tsn) {
5675                 /* Already got there ... */
5676                 return;
5677         }
5678         back_out_htsn = asoc->highest_tsn_inside_map;
5679         if (compare_with_wrap(new_cum_tsn, asoc->highest_tsn_inside_map,
5680             MAX_TSN)) {
5681                 asoc->highest_tsn_inside_map = new_cum_tsn;
5682                 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
5683                         sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5684                 }
5685         }
5686         /*
5687          * now we know the new TSN is more advanced, let's find the actual
5688          * gap
5689          */
5690         if ((compare_with_wrap(new_cum_tsn, asoc->mapping_array_base_tsn,
5691             MAX_TSN)) ||
5692             (new_cum_tsn == asoc->mapping_array_base_tsn)) {
5693                 gap = new_cum_tsn - asoc->mapping_array_base_tsn;
5694         } else {
5695                 /* try to prevent underflow here */
5696                 gap = new_cum_tsn + (MAX_TSN - asoc->mapping_array_base_tsn) + 1;
5697         }
5698
5699         if (gap > m_size) {
5700                 asoc->highest_tsn_inside_map = back_out_htsn;
5701                 if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
5702                         struct mbuf *oper;
5703
5704                         /*
5705                          * out of range (of single byte chunks in the rwnd I
5706                          * give out). This must be an attacker.
5707                          */
5708                         *abort_flag = 1;
5709                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
5710                             0, M_DONTWAIT, 1, MT_DATA);
5711                         if (oper) {
5712                                 struct sctp_paramhdr *ph;
5713                                 uint32_t *ippp;
5714
5715                                 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5716                                     (sizeof(uint32_t) * 3);
5717                                 ph = mtod(oper, struct sctp_paramhdr *);
5718                                 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5719                                 ph->param_length = htons(SCTP_BUF_LEN(oper));
5720                                 ippp = (uint32_t *) (ph + 1);
5721                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
5722                                 ippp++;
5723                                 *ippp = asoc->highest_tsn_inside_map;
5724                                 ippp++;
5725                                 *ippp = new_cum_tsn;
5726                         }
5727                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
5728                         sctp_abort_an_association(stcb->sctp_ep, stcb,
5729                             SCTP_PEER_FAULTY, oper);
5730                         return;
5731                 }
5732                 if (asoc->highest_tsn_inside_map >
5733                     asoc->mapping_array_base_tsn) {
5734                         gap = asoc->highest_tsn_inside_map -
5735                             asoc->mapping_array_base_tsn;
5736                 } else {
5737                         gap = asoc->highest_tsn_inside_map +
5738                             (MAX_TSN - asoc->mapping_array_base_tsn) + 1;
5739                 }
5740                 SCTP_STAT_INCR(sctps_fwdtsn_map_over);
5741                 cumack_set_flag = 1;
5742         }
5743         SCTP_TCB_LOCK_ASSERT(stcb);
5744         for (i = 0; i <= gap; i++) {
5745                 SCTP_SET_TSN_PRESENT(asoc->mapping_array, i);
5746         }
5747         /*
5748          * Now after marking all, slide thing forward but no sack please.
5749          */
5750         sctp_sack_check(stcb, 0, 0, abort_flag);
5751         if (*abort_flag)
5752                 return;
5753
5754         if (cumack_set_flag) {
5755                 /*
5756                  * fwd-tsn went outside my gap array - not a common
5757                  * occurance. Do the same thing we do when a cookie-echo
5758                  * arrives.
5759                  */
5760                 asoc->highest_tsn_inside_map = new_cum_tsn - 1;
5761                 asoc->mapping_array_base_tsn = new_cum_tsn;
5762                 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
5763                 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
5764                         sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5765                 }
5766                 asoc->last_echo_tsn = asoc->highest_tsn_inside_map;
5767         }
5768         /*************************************************************/
5769         /* 2. Clear up re-assembly queue                             */
5770         /*************************************************************/
5771
5772         /*
5773          * First service it if pd-api is up, just in case we can progress it
5774          * forward
5775          */
5776         if (asoc->fragmented_delivery_inprogress) {
5777                 sctp_service_reassembly(stcb, asoc);
5778         }
5779         if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5780                 /* For each one on here see if we need to toss it */
5781                 /*
5782                  * For now large messages held on the reasmqueue that are
5783                  * complete will be tossed too. We could in theory do more
5784                  * work to spin through and stop after dumping one msg aka
5785                  * seeing the start of a new msg at the head, and call the
5786                  * delivery function... to see if it can be delivered... But
5787                  * for now we just dump everything on the queue.
5788                  */
5789                 chk = TAILQ_FIRST(&asoc->reasmqueue);
5790                 while (chk) {
5791                         at = TAILQ_NEXT(chk, sctp_next);
5792                         if (compare_with_wrap(asoc->cumulative_tsn,
5793                             chk->rec.data.TSN_seq, MAX_TSN) ||
5794                             asoc->cumulative_tsn == chk->rec.data.TSN_seq) {
5795                                 /* It needs to be tossed */
5796                                 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5797                                 if (compare_with_wrap(chk->rec.data.TSN_seq,
5798                                     asoc->tsn_last_delivered, MAX_TSN)) {
5799                                         asoc->tsn_last_delivered =
5800                                             chk->rec.data.TSN_seq;
5801                                         asoc->str_of_pdapi =
5802                                             chk->rec.data.stream_number;
5803                                         asoc->ssn_of_pdapi =
5804                                             chk->rec.data.stream_seq;
5805                                         asoc->fragment_flags =
5806                                             chk->rec.data.rcv_flags;
5807                                 }
5808                                 asoc->size_on_reasm_queue -= chk->send_size;
5809                                 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5810                                 cnt_gone++;
5811
5812                                 /* Clear up any stream problem */
5813                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
5814                                     SCTP_DATA_UNORDERED &&
5815                                     (compare_with_wrap(chk->rec.data.stream_seq,
5816                                     asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
5817                                     MAX_SEQ))) {
5818                                         /*
5819                                          * We must dump forward this streams
5820                                          * sequence number if the chunk is
5821                                          * not unordered that is being
5822                                          * skipped. There is a chance that
5823                                          * if the peer does not include the
5824                                          * last fragment in its FWD-TSN we
5825                                          * WILL have a problem here since
5826                                          * you would have a partial chunk in
5827                                          * queue that may not be
5828                                          * deliverable. Also if a Partial
5829                                          * delivery API as started the user
5830                                          * may get a partial chunk. The next
5831                                          * read returning a new chunk...
5832                                          * really ugly but I see no way
5833                                          * around it! Maybe a notify??
5834                                          */
5835                                         asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
5836                                             chk->rec.data.stream_seq;
5837                                 }
5838                                 if (chk->data) {
5839                                         sctp_m_freem(chk->data);
5840                                         chk->data = NULL;
5841                                 }
5842                                 sctp_free_remote_addr(chk->whoTo);
5843                                 sctp_free_a_chunk(stcb, chk);
5844                         } else {
5845                                 /*
5846                                  * Ok we have gone beyond the end of the
5847                                  * fwd-tsn's mark. Some checks...
5848                                  */
5849                                 if ((asoc->fragmented_delivery_inprogress) &&
5850                                     (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
5851                                         uint32_t str_seq;
5852
5853                                         /*
5854                                          * Special case PD-API is up and
5855                                          * what we fwd-tsn' over includes
5856                                          * one that had the LAST_FRAG. We no
5857                                          * longer need to do the PD-API.
5858                                          */
5859                                         asoc->fragmented_delivery_inprogress = 0;
5860
5861                                         str_seq = (asoc->str_of_pdapi << 16) | asoc->ssn_of_pdapi;
5862                                         sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5863                                             stcb, SCTP_PARTIAL_DELIVERY_ABORTED, (void *)&str_seq);
5864
5865                                 }
5866                                 break;
5867                         }
5868                         chk = at;
5869                 }
5870         }
5871         if (asoc->fragmented_delivery_inprogress) {
5872                 /*
5873                  * Ok we removed cnt_gone chunks in the PD-API queue that
5874                  * were being delivered. So now we must turn off the flag.
5875                  */
5876                 uint32_t str_seq;
5877
5878                 str_seq = (asoc->str_of_pdapi << 16) | asoc->ssn_of_pdapi;
5879                 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5880                     stcb, SCTP_PARTIAL_DELIVERY_ABORTED, (void *)&str_seq);
5881                 asoc->fragmented_delivery_inprogress = 0;
5882         }
5883         /*************************************************************/
5884         /* 3. Update the PR-stream re-ordering queues                */
5885         /*************************************************************/
5886         stseq = (struct sctp_strseq *)((caddr_t)fwd + sizeof(*fwd));
5887         fwd_sz -= sizeof(*fwd);
5888         {
5889                 /* New method. */
5890                 unsigned int num_str;
5891
5892                 num_str = fwd_sz / sizeof(struct sctp_strseq);
5893                 for (i = 0; i < num_str; i++) {
5894                         uint16_t st;
5895                         unsigned char *xx;
5896
5897                         /* Convert */
5898                         xx = (unsigned char *)&stseq[i];
5899                         st = ntohs(stseq[i].stream);
5900                         stseq[i].stream = st;
5901                         st = ntohs(stseq[i].sequence);
5902                         stseq[i].sequence = st;
5903                         /* now process */
5904                         if (stseq[i].stream > asoc->streamincnt) {
5905                                 /*
5906                                  * It is arguable if we should continue.
5907                                  * Since the peer sent bogus stream info we
5908                                  * may be in deep trouble.. a return may be
5909                                  * a better choice?
5910                                  */
5911                                 continue;
5912                         }
5913                         strm = &asoc->strmin[stseq[i].stream];
5914                         if (compare_with_wrap(stseq[i].sequence,
5915                             strm->last_sequence_delivered, MAX_SEQ)) {
5916                                 /* Update the sequence number */
5917                                 strm->last_sequence_delivered =
5918                                     stseq[i].sequence;
5919                         }
5920                         /* now kick the stream the new way */
5921                         sctp_kick_prsctp_reorder_queue(stcb, strm);
5922                 }
5923         }
5924         if (TAILQ_FIRST(&asoc->reasmqueue)) {
5925                 /* now lets kick out and check for more fragmented delivery */
5926                 sctp_deliver_reasm_check(stcb, &stcb->asoc);
5927         }
5928 }