]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/netinet/sctp_indata.c
Copy head to stable/8 as part of 8.0 Release cycle.
[FreeBSD/stable/8.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 #define SCTP_CALC_TSN_TO_GAP(gap, tsn, mapping_tsn) do { \
49                                         if ((compare_with_wrap(tsn, mapping_tsn, MAX_TSN)) || \
50                         (tsn == mapping_tsn)) { \
51                                                 gap = tsn - mapping_tsn; \
52                                         } else { \
53                                                 gap = (MAX_TSN - mapping_tsn) + tsn + 1; \
54                                         } \
55                   } while(0)
56
57 #define SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc) do { \
58                     if (asoc->mapping_array_base_tsn == asoc->nr_mapping_array_base_tsn) { \
59                        SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, nr_gap); \
60                     } else {\
61                        int lgap; \
62                        SCTP_CALC_TSN_TO_GAP(lgap, tsn, asoc->mapping_array_base_tsn); \
63                        SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, lgap); \
64                     } \
65                   } while(0)
66
67 /*
68  * NOTES: On the outbound side of things I need to check the sack timer to
69  * see if I should generate a sack into the chunk queue (if I have data to
70  * send that is and will be sending it .. for bundling.
71  *
72  * The callback in sctp_usrreq.c will get called when the socket is read from.
73  * This will cause sctp_service_queues() to get called on the top entry in
74  * the list.
75  */
76
77 void
78 sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
79 {
80         asoc->my_rwnd = sctp_calc_rwnd(stcb, asoc);
81 }
82
83 /* Calculate what the rwnd would be */
84 uint32_t
85 sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
86 {
87         uint32_t calc = 0;
88
89         /*
90          * This is really set wrong with respect to a 1-2-m socket. Since
91          * the sb_cc is the count that everyone as put up. When we re-write
92          * sctp_soreceive then we will fix this so that ONLY this
93          * associations data is taken into account.
94          */
95         if (stcb->sctp_socket == NULL)
96                 return (calc);
97
98         if (stcb->asoc.sb_cc == 0 &&
99             asoc->size_on_reasm_queue == 0 &&
100             asoc->size_on_all_streams == 0) {
101                 /* Full rwnd granted */
102                 calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket), SCTP_MINIMAL_RWND);
103                 return (calc);
104         }
105         /* get actual space */
106         calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
107
108         /*
109          * take out what has NOT been put on socket queue and we yet hold
110          * for putting up.
111          */
112         calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_reasm_queue);
113         calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_all_streams);
114
115         if (calc == 0) {
116                 /* out of space */
117                 return (calc);
118         }
119         /* what is the overhead of all these rwnd's */
120         calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
121         /*
122          * If the window gets too small due to ctrl-stuff, reduce it to 1,
123          * even it is 0. SWS engaged
124          */
125         if (calc < stcb->asoc.my_rwnd_control_len) {
126                 calc = 1;
127         }
128         return (calc);
129 }
130
131
132
133 /*
134  * Build out our readq entry based on the incoming packet.
135  */
136 struct sctp_queued_to_read *
137 sctp_build_readq_entry(struct sctp_tcb *stcb,
138     struct sctp_nets *net,
139     uint32_t tsn, uint32_t ppid,
140     uint32_t context, uint16_t stream_no,
141     uint16_t stream_seq, uint8_t flags,
142     struct mbuf *dm)
143 {
144         struct sctp_queued_to_read *read_queue_e = NULL;
145
146         sctp_alloc_a_readq(stcb, read_queue_e);
147         if (read_queue_e == NULL) {
148                 goto failed_build;
149         }
150         read_queue_e->sinfo_stream = stream_no;
151         read_queue_e->sinfo_ssn = stream_seq;
152         read_queue_e->sinfo_flags = (flags << 8);
153         read_queue_e->sinfo_ppid = ppid;
154         read_queue_e->sinfo_context = stcb->asoc.context;
155         read_queue_e->sinfo_timetolive = 0;
156         read_queue_e->sinfo_tsn = tsn;
157         read_queue_e->sinfo_cumtsn = tsn;
158         read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
159         read_queue_e->whoFrom = net;
160         read_queue_e->length = 0;
161         atomic_add_int(&net->ref_count, 1);
162         read_queue_e->data = dm;
163         read_queue_e->spec_flags = 0;
164         read_queue_e->tail_mbuf = NULL;
165         read_queue_e->aux_data = NULL;
166         read_queue_e->stcb = stcb;
167         read_queue_e->port_from = stcb->rport;
168         read_queue_e->do_not_ref_stcb = 0;
169         read_queue_e->end_added = 0;
170         read_queue_e->some_taken = 0;
171         read_queue_e->pdapi_aborted = 0;
172 failed_build:
173         return (read_queue_e);
174 }
175
176
177 /*
178  * Build out our readq entry based on the incoming packet.
179  */
180 static struct sctp_queued_to_read *
181 sctp_build_readq_entry_chk(struct sctp_tcb *stcb,
182     struct sctp_tmit_chunk *chk)
183 {
184         struct sctp_queued_to_read *read_queue_e = NULL;
185
186         sctp_alloc_a_readq(stcb, read_queue_e);
187         if (read_queue_e == NULL) {
188                 goto failed_build;
189         }
190         read_queue_e->sinfo_stream = chk->rec.data.stream_number;
191         read_queue_e->sinfo_ssn = chk->rec.data.stream_seq;
192         read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8);
193         read_queue_e->sinfo_ppid = chk->rec.data.payloadtype;
194         read_queue_e->sinfo_context = stcb->asoc.context;
195         read_queue_e->sinfo_timetolive = 0;
196         read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq;
197         read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq;
198         read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
199         read_queue_e->whoFrom = chk->whoTo;
200         read_queue_e->aux_data = NULL;
201         read_queue_e->length = 0;
202         atomic_add_int(&chk->whoTo->ref_count, 1);
203         read_queue_e->data = chk->data;
204         read_queue_e->tail_mbuf = NULL;
205         read_queue_e->stcb = stcb;
206         read_queue_e->port_from = stcb->rport;
207         read_queue_e->spec_flags = 0;
208         read_queue_e->do_not_ref_stcb = 0;
209         read_queue_e->end_added = 0;
210         read_queue_e->some_taken = 0;
211         read_queue_e->pdapi_aborted = 0;
212 failed_build:
213         return (read_queue_e);
214 }
215
216
217 struct mbuf *
218 sctp_build_ctl_nchunk(struct sctp_inpcb *inp,
219     struct sctp_sndrcvinfo *sinfo)
220 {
221         struct sctp_sndrcvinfo *outinfo;
222         struct cmsghdr *cmh;
223         struct mbuf *ret;
224         int len;
225         int use_extended = 0;
226
227         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
228                 /* user does not want the sndrcv ctl */
229                 return (NULL);
230         }
231         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
232                 use_extended = 1;
233                 len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
234         } else {
235                 len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
236         }
237
238
239         ret = sctp_get_mbuf_for_msg(len,
240             0, M_DONTWAIT, 1, MT_DATA);
241
242         if (ret == NULL) {
243                 /* No space */
244                 return (ret);
245         }
246         /* We need a CMSG header followed by the struct  */
247         cmh = mtod(ret, struct cmsghdr *);
248         outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
249         cmh->cmsg_level = IPPROTO_SCTP;
250         if (use_extended) {
251                 cmh->cmsg_type = SCTP_EXTRCV;
252                 cmh->cmsg_len = len;
253                 memcpy(outinfo, sinfo, len);
254         } else {
255                 cmh->cmsg_type = SCTP_SNDRCV;
256                 cmh->cmsg_len = len;
257                 *outinfo = *sinfo;
258         }
259         SCTP_BUF_LEN(ret) = cmh->cmsg_len;
260         return (ret);
261 }
262
263
264 char *
265 sctp_build_ctl_cchunk(struct sctp_inpcb *inp,
266     int *control_len,
267     struct sctp_sndrcvinfo *sinfo)
268 {
269         struct sctp_sndrcvinfo *outinfo;
270         struct cmsghdr *cmh;
271         char *buf;
272         int len;
273         int use_extended = 0;
274
275         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
276                 /* user does not want the sndrcv ctl */
277                 return (NULL);
278         }
279         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
280                 use_extended = 1;
281                 len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
282         } else {
283                 len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
284         }
285         SCTP_MALLOC(buf, char *, len, SCTP_M_CMSG);
286         if (buf == NULL) {
287                 /* No space */
288                 return (buf);
289         }
290         /* We need a CMSG header followed by the struct  */
291         cmh = (struct cmsghdr *)buf;
292         outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
293         cmh->cmsg_level = IPPROTO_SCTP;
294         if (use_extended) {
295                 cmh->cmsg_type = SCTP_EXTRCV;
296                 cmh->cmsg_len = len;
297                 memcpy(outinfo, sinfo, len);
298         } else {
299                 cmh->cmsg_type = SCTP_SNDRCV;
300                 cmh->cmsg_len = len;
301                 *outinfo = *sinfo;
302         }
303         *control_len = len;
304         return (buf);
305 }
306
307
308 /*
309  * We are delivering currently from the reassembly queue. We must continue to
310  * deliver until we either: 1) run out of space. 2) run out of sequential
311  * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
312  */
313 static void
314 sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
315 {
316         struct sctp_tmit_chunk *chk;
317         uint16_t nxt_todel;
318         uint16_t stream_no;
319         int end = 0;
320         int cntDel;
321
322         /* EY if any out-of-order delivered, then tag it nr on nr_map */
323         uint32_t nr_tsn, nr_gap;
324
325         struct sctp_queued_to_read *control, *ctl, *ctlat;
326
327         if (stcb == NULL)
328                 return;
329
330         cntDel = stream_no = 0;
331         if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
332             (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
333             (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
334                 /* socket above is long gone or going.. */
335 abandon:
336                 asoc->fragmented_delivery_inprogress = 0;
337                 chk = TAILQ_FIRST(&asoc->reasmqueue);
338                 while (chk) {
339                         TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
340                         asoc->size_on_reasm_queue -= chk->send_size;
341                         sctp_ucount_decr(asoc->cnt_on_reasm_queue);
342                         /*
343                          * Lose the data pointer, since its in the socket
344                          * buffer
345                          */
346                         if (chk->data) {
347                                 sctp_m_freem(chk->data);
348                                 chk->data = NULL;
349                         }
350                         /* Now free the address and data */
351                         sctp_free_a_chunk(stcb, chk);
352                         /* sa_ignore FREED_MEMORY */
353                         chk = TAILQ_FIRST(&asoc->reasmqueue);
354                 }
355                 return;
356         }
357         SCTP_TCB_LOCK_ASSERT(stcb);
358         do {
359                 chk = TAILQ_FIRST(&asoc->reasmqueue);
360                 if (chk == NULL) {
361                         return;
362                 }
363                 if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
364                         /* Can't deliver more :< */
365                         return;
366                 }
367                 stream_no = chk->rec.data.stream_number;
368                 nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
369                 if (nxt_todel != chk->rec.data.stream_seq &&
370                     (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
371                         /*
372                          * Not the next sequence to deliver in its stream OR
373                          * unordered
374                          */
375                         return;
376                 }
377                 if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
378
379                         control = sctp_build_readq_entry_chk(stcb, chk);
380                         if (control == NULL) {
381                                 /* out of memory? */
382                                 return;
383                         }
384                         /* save it off for our future deliveries */
385                         stcb->asoc.control_pdapi = control;
386                         if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
387                                 end = 1;
388                         else
389                                 end = 0;
390                         sctp_add_to_readq(stcb->sctp_ep,
391                             stcb, control, &stcb->sctp_socket->so_rcv, end,
392                             SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
393                         cntDel++;
394                 } else {
395                         if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
396                                 end = 1;
397                         else
398                                 end = 0;
399                         if (sctp_append_to_readq(stcb->sctp_ep, stcb,
400                             stcb->asoc.control_pdapi,
401                             chk->data, end, chk->rec.data.TSN_seq,
402                             &stcb->sctp_socket->so_rcv)) {
403                                 /*
404                                  * something is very wrong, either
405                                  * control_pdapi is NULL, or the tail_mbuf
406                                  * is corrupt, or there is a EOM already on
407                                  * the mbuf chain.
408                                  */
409                                 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
410                                         goto abandon;
411                                 } else {
412 #ifdef INVARIANTS
413                                         if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
414                                                 panic("This should not happen control_pdapi NULL?");
415                                         }
416                                         /* if we did not panic, it was a EOM */
417                                         panic("Bad chunking ??");
418 #else
419                                         if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
420                                                 SCTP_PRINTF("This should not happen control_pdapi NULL?\n");
421                                         }
422                                         SCTP_PRINTF("Bad chunking ??\n");
423                                         SCTP_PRINTF("Dumping re-assembly queue this will probably hose the association\n");
424
425 #endif
426                                         goto abandon;
427                                 }
428                         }
429                         cntDel++;
430                 }
431                 /* pull it we did it */
432                 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
433                 /*
434                  * EY this is the chunk that should be tagged nr gapped
435                  * calculate the gap and such then tag this TSN nr
436                  * chk->rec.data.TSN_seq
437                  */
438                 /*
439                  * EY!-TODO- this tsn should be tagged nr only if it is
440                  * out-of-order, the if statement should be modified
441                  */
442                 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
443
444                         nr_tsn = chk->rec.data.TSN_seq;
445                         SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
446                         if ((nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3)) ||
447                             (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
448                                 /*
449                                  * EY The 1st should never happen, as in
450                                  * process_a_data_chunk method this check
451                                  * should be done
452                                  */
453                                 /*
454                                  * EY The 2nd should never happen, because
455                                  * nr_mapping_array is always expanded when
456                                  * mapping_array is expanded
457                                  */
458                                 printf("Impossible nr_gap ack range failed\n");
459                         } else {
460                                 SCTP_TCB_LOCK_ASSERT(stcb);
461                                 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
462                                 SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
463                                 if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
464                                         asoc->highest_tsn_inside_nr_map = nr_tsn;
465                         }
466                 }
467                 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
468                         asoc->fragmented_delivery_inprogress = 0;
469                         if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
470                                 asoc->strmin[stream_no].last_sequence_delivered++;
471                         }
472                         if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
473                                 SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
474                         }
475                 } else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
476                         /*
477                          * turn the flag back on since we just  delivered
478                          * yet another one.
479                          */
480                         asoc->fragmented_delivery_inprogress = 1;
481                 }
482                 asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
483                 asoc->last_flags_delivered = chk->rec.data.rcv_flags;
484                 asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
485                 asoc->last_strm_no_delivered = chk->rec.data.stream_number;
486
487                 asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
488                 asoc->size_on_reasm_queue -= chk->send_size;
489                 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
490                 /* free up the chk */
491                 chk->data = NULL;
492                 sctp_free_a_chunk(stcb, chk);
493
494                 if (asoc->fragmented_delivery_inprogress == 0) {
495                         /*
496                          * Now lets see if we can deliver the next one on
497                          * the stream
498                          */
499                         struct sctp_stream_in *strm;
500
501                         strm = &asoc->strmin[stream_no];
502                         nxt_todel = strm->last_sequence_delivered + 1;
503                         ctl = TAILQ_FIRST(&strm->inqueue);
504                         if (ctl && (nxt_todel == ctl->sinfo_ssn)) {
505                                 while (ctl != NULL) {
506                                         /* Deliver more if we can. */
507                                         if (nxt_todel == ctl->sinfo_ssn) {
508                                                 ctlat = TAILQ_NEXT(ctl, next);
509                                                 TAILQ_REMOVE(&strm->inqueue, ctl, next);
510                                                 asoc->size_on_all_streams -= ctl->length;
511                                                 sctp_ucount_decr(asoc->cnt_on_all_streams);
512                                                 strm->last_sequence_delivered++;
513                                                 /*
514                                                  * EY will be used to
515                                                  * calculate nr-gap
516                                                  */
517                                                 nr_tsn = ctl->sinfo_tsn;
518                                                 sctp_add_to_readq(stcb->sctp_ep, stcb,
519                                                     ctl,
520                                                     &stcb->sctp_socket->so_rcv, 1,
521                                                     SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
522                                                 /*
523                                                  * EY -now something is
524                                                  * delivered, calculate
525                                                  * nr_gap and tag this tsn
526                                                  * NR
527                                                  */
528                                                 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
529                                                         SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
530                                                         if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
531                                                             (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
532                                                                 printf("Impossible NR gap calculation?\n");
533                                                                 /*
534                                                                  * EY The
535                                                                  * 1st
536                                                                  * should
537                                                                  * never
538                                                                  * happen,
539                                                                  * as in
540                                                                  * process_a_
541                                                                  * data_chunk
542                                                                  *  method
543                                                                  * this
544                                                                  * check
545                                                                  * should be
546                                                                  * done
547                                                                  */
548                                                                 /*
549                                                                  * EY The
550                                                                  * 2nd
551                                                                  * should
552                                                                  * never
553                                                                  * happen,
554                                                                  * because
555                                                                  * nr_mapping
556                                                                  * _array is
557                                                                  * always
558                                                                  * expanded
559                                                                  * when
560                                                                  * mapping_ar
561                                                                  * ray is
562                                                                  * expanded
563                                                                  */
564                                                         } else {
565                                                                 SCTP_TCB_LOCK_ASSERT(stcb);
566                                                                 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
567                                                                 SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
568                                                                 if (compare_with_wrap(nr_tsn,
569                                                                     asoc->highest_tsn_inside_nr_map,
570                                                                     MAX_TSN))
571                                                                         asoc->highest_tsn_inside_nr_map = nr_tsn;
572                                                         }
573                                                 }
574                                                 ctl = ctlat;
575                                         } else {
576                                                 break;
577                                         }
578                                         nxt_todel = strm->last_sequence_delivered + 1;
579                                 }
580                         }
581                         break;
582                 }
583                 /* sa_ignore FREED_MEMORY */
584                 chk = TAILQ_FIRST(&asoc->reasmqueue);
585         } while (chk);
586 }
587
588 /*
589  * Queue the chunk either right into the socket buffer if it is the next one
590  * to go OR put it in the correct place in the delivery queue.  If we do
591  * append to the so_buf, keep doing so until we are out of order. One big
592  * question still remains, what to do when the socket buffer is FULL??
593  */
594 static void
595 sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
596     struct sctp_queued_to_read *control, int *abort_flag)
597 {
598         /*
599          * FIX-ME maybe? What happens when the ssn wraps? If we are getting
600          * all the data in one stream this could happen quite rapidly. One
601          * could use the TSN to keep track of things, but this scheme breaks
602          * down in the other type of stream useage that could occur. Send a
603          * single msg to stream 0, send 4Billion messages to stream 1, now
604          * send a message to stream 0. You have a situation where the TSN
605          * has wrapped but not in the stream. Is this worth worrying about
606          * or should we just change our queue sort at the bottom to be by
607          * TSN.
608          * 
609          * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
610          * with TSN 1? If the peer is doing some sort of funky TSN/SSN
611          * assignment this could happen... and I don't see how this would be
612          * a violation. So for now I am undecided an will leave the sort by
613          * SSN alone. Maybe a hybred approach is the answer
614          * 
615          */
616         struct sctp_stream_in *strm;
617         struct sctp_queued_to_read *at;
618         int queue_needed;
619         uint16_t nxt_todel;
620         struct mbuf *oper;
621
622         /* EY- will be used to calculate nr-gap for a tsn */
623         uint32_t nr_tsn, nr_gap;
624
625         queue_needed = 1;
626         asoc->size_on_all_streams += control->length;
627         sctp_ucount_incr(asoc->cnt_on_all_streams);
628         strm = &asoc->strmin[control->sinfo_stream];
629         nxt_todel = strm->last_sequence_delivered + 1;
630         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
631                 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
632         }
633         SCTPDBG(SCTP_DEBUG_INDATA1,
634             "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
635             (uint32_t) control->sinfo_stream,
636             (uint32_t) strm->last_sequence_delivered,
637             (uint32_t) nxt_todel);
638         if (compare_with_wrap(strm->last_sequence_delivered,
639             control->sinfo_ssn, MAX_SEQ) ||
640             (strm->last_sequence_delivered == control->sinfo_ssn)) {
641                 /* The incoming sseq is behind where we last delivered? */
642                 SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort  association\n",
643                     control->sinfo_ssn, strm->last_sequence_delivered);
644 protocol_error:
645                 /*
646                  * throw it in the stream so it gets cleaned up in
647                  * association destruction
648                  */
649                 TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
650                 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
651                     0, M_DONTWAIT, 1, MT_DATA);
652                 if (oper) {
653                         struct sctp_paramhdr *ph;
654                         uint32_t *ippp;
655
656                         SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
657                             (sizeof(uint32_t) * 3);
658                         ph = mtod(oper, struct sctp_paramhdr *);
659                         ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
660                         ph->param_length = htons(SCTP_BUF_LEN(oper));
661                         ippp = (uint32_t *) (ph + 1);
662                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_1);
663                         ippp++;
664                         *ippp = control->sinfo_tsn;
665                         ippp++;
666                         *ippp = ((control->sinfo_stream << 16) | control->sinfo_ssn);
667                 }
668                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
669                 sctp_abort_an_association(stcb->sctp_ep, stcb,
670                     SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
671
672                 *abort_flag = 1;
673                 return;
674
675         }
676         if (nxt_todel == control->sinfo_ssn) {
677                 /* can be delivered right away? */
678                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
679                         sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
680                 }
681                 /* EY it wont be queued if it could be delivered directly */
682                 queue_needed = 0;
683                 asoc->size_on_all_streams -= control->length;
684                 sctp_ucount_decr(asoc->cnt_on_all_streams);
685                 strm->last_sequence_delivered++;
686                 /* EY will be used to calculate nr-gap */
687                 nr_tsn = control->sinfo_tsn;
688                 sctp_add_to_readq(stcb->sctp_ep, stcb,
689                     control,
690                     &stcb->sctp_socket->so_rcv, 1,
691                     SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
692                 /*
693                  * EY this is the chunk that should be tagged nr gapped
694                  * calculate the gap and such then tag this TSN nr
695                  * chk->rec.data.TSN_seq
696                  */
697                 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
698                         SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
699                         if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
700                             (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
701                                 printf("Impossible nr_tsn set 2?\n");
702                                 /*
703                                  * EY The 1st should never happen, as in
704                                  * process_a_data_chunk method this check
705                                  * should be done
706                                  */
707                                 /*
708                                  * EY The 2nd should never happen, because
709                                  * nr_mapping_array is always expanded when
710                                  * mapping_array is expanded
711                                  */
712                         } else {
713                                 SCTP_TCB_LOCK_ASSERT(stcb);
714                                 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
715                                 SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
716                                 if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
717                                         asoc->highest_tsn_inside_nr_map = nr_tsn;
718                         }
719                 }
720                 control = TAILQ_FIRST(&strm->inqueue);
721                 while (control != NULL) {
722                         /* all delivered */
723                         nxt_todel = strm->last_sequence_delivered + 1;
724                         if (nxt_todel == control->sinfo_ssn) {
725                                 at = TAILQ_NEXT(control, next);
726                                 TAILQ_REMOVE(&strm->inqueue, control, next);
727                                 asoc->size_on_all_streams -= control->length;
728                                 sctp_ucount_decr(asoc->cnt_on_all_streams);
729                                 strm->last_sequence_delivered++;
730                                 /*
731                                  * We ignore the return of deliver_data here
732                                  * since we always can hold the chunk on the
733                                  * d-queue. And we have a finite number that
734                                  * can be delivered from the strq.
735                                  */
736                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
737                                         sctp_log_strm_del(control, NULL,
738                                             SCTP_STR_LOG_FROM_IMMED_DEL);
739                                 }
740                                 /* EY will be used to calculate nr-gap */
741                                 nr_tsn = control->sinfo_tsn;
742                                 sctp_add_to_readq(stcb->sctp_ep, stcb,
743                                     control,
744                                     &stcb->sctp_socket->so_rcv, 1,
745                                     SCTP_READ_LOCK_NOT_HELD,
746                                     SCTP_SO_NOT_LOCKED);
747                                 /*
748                                  * EY this is the chunk that should be
749                                  * tagged nr gapped calculate the gap and
750                                  * such then tag this TSN nr
751                                  * chk->rec.data.TSN_seq
752                                  */
753                                 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
754                                         SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
755                                         if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
756                                             (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
757                                                 printf("Impossible nr TSN set 3?\n");
758                                                 /*
759                                                  * EY The 1st should never
760                                                  * happen, as in
761                                                  * process_a_data_chunk
762                                                  * method this check should
763                                                  * be done
764                                                  */
765                                                 /*
766                                                  * EY The 2nd should never
767                                                  * happen, because
768                                                  * nr_mapping_array is
769                                                  * always expanded when
770                                                  * mapping_array is expanded
771                                                  */
772                                         } else {
773                                                 SCTP_TCB_LOCK_ASSERT(stcb);
774                                                 SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
775                                                 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
776                                                 if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map,
777                                                     MAX_TSN))
778                                                         asoc->highest_tsn_inside_nr_map = nr_tsn;
779                                         }
780                                 }
781                                 control = at;
782                                 continue;
783                         }
784                         break;
785                 }
786         }
787         if (queue_needed) {
788                 /*
789                  * Ok, we did not deliver this guy, find the correct place
790                  * to put it on the queue.
791                  */
792                 if ((compare_with_wrap(asoc->cumulative_tsn,
793                     control->sinfo_tsn, MAX_TSN)) ||
794                     (control->sinfo_tsn == asoc->cumulative_tsn)) {
795                         goto protocol_error;
796                 }
797                 if (TAILQ_EMPTY(&strm->inqueue)) {
798                         /* Empty queue */
799                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
800                                 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
801                         }
802                         TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
803                 } else {
804                         TAILQ_FOREACH(at, &strm->inqueue, next) {
805                                 if (compare_with_wrap(at->sinfo_ssn,
806                                     control->sinfo_ssn, MAX_SEQ)) {
807                                         /*
808                                          * one in queue is bigger than the
809                                          * new one, insert before this one
810                                          */
811                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
812                                                 sctp_log_strm_del(control, at,
813                                                     SCTP_STR_LOG_FROM_INSERT_MD);
814                                         }
815                                         TAILQ_INSERT_BEFORE(at, control, next);
816                                         break;
817                                 } else if (at->sinfo_ssn == control->sinfo_ssn) {
818                                         /*
819                                          * Gak, He sent me a duplicate str
820                                          * seq number
821                                          */
822                                         /*
823                                          * foo bar, I guess I will just free
824                                          * this new guy, should we abort
825                                          * too? FIX ME MAYBE? Or it COULD be
826                                          * that the SSN's have wrapped.
827                                          * Maybe I should compare to TSN
828                                          * somehow... sigh for now just blow
829                                          * away the chunk!
830                                          */
831
832                                         if (control->data)
833                                                 sctp_m_freem(control->data);
834                                         control->data = NULL;
835                                         asoc->size_on_all_streams -= control->length;
836                                         sctp_ucount_decr(asoc->cnt_on_all_streams);
837                                         if (control->whoFrom)
838                                                 sctp_free_remote_addr(control->whoFrom);
839                                         control->whoFrom = NULL;
840                                         sctp_free_a_readq(stcb, control);
841                                         return;
842                                 } else {
843                                         if (TAILQ_NEXT(at, next) == NULL) {
844                                                 /*
845                                                  * We are at the end, insert
846                                                  * it after this one
847                                                  */
848                                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
849                                                         sctp_log_strm_del(control, at,
850                                                             SCTP_STR_LOG_FROM_INSERT_TL);
851                                                 }
852                                                 TAILQ_INSERT_AFTER(&strm->inqueue,
853                                                     at, control, next);
854                                                 break;
855                                         }
856                                 }
857                         }
858                 }
859         }
860 }
861
862 /*
863  * Returns two things: You get the total size of the deliverable parts of the
864  * first fragmented message on the reassembly queue. And you get a 1 back if
865  * all of the message is ready or a 0 back if the message is still incomplete
866  */
867 static int
868 sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
869 {
870         struct sctp_tmit_chunk *chk;
871         uint32_t tsn;
872
873         *t_size = 0;
874         chk = TAILQ_FIRST(&asoc->reasmqueue);
875         if (chk == NULL) {
876                 /* nothing on the queue */
877                 return (0);
878         }
879         if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
880                 /* Not a first on the queue */
881                 return (0);
882         }
883         tsn = chk->rec.data.TSN_seq;
884         while (chk) {
885                 if (tsn != chk->rec.data.TSN_seq) {
886                         return (0);
887                 }
888                 *t_size += chk->send_size;
889                 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
890                         return (1);
891                 }
892                 tsn++;
893                 chk = TAILQ_NEXT(chk, sctp_next);
894         }
895         return (0);
896 }
897
898 static void
899 sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
900 {
901         struct sctp_tmit_chunk *chk;
902         uint16_t nxt_todel;
903         uint32_t tsize;
904
905 doit_again:
906         chk = TAILQ_FIRST(&asoc->reasmqueue);
907         if (chk == NULL) {
908                 /* Huh? */
909                 asoc->size_on_reasm_queue = 0;
910                 asoc->cnt_on_reasm_queue = 0;
911                 return;
912         }
913         if (asoc->fragmented_delivery_inprogress == 0) {
914                 nxt_todel =
915                     asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
916                 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
917                     (nxt_todel == chk->rec.data.stream_seq ||
918                     (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
919                         /*
920                          * Yep the first one is here and its ok to deliver
921                          * but should we?
922                          */
923                         if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
924                             (tsize >= stcb->sctp_ep->partial_delivery_point))) {
925
926                                 /*
927                                  * Yes, we setup to start reception, by
928                                  * backing down the TSN just in case we
929                                  * can't deliver. If we
930                                  */
931                                 asoc->fragmented_delivery_inprogress = 1;
932                                 asoc->tsn_last_delivered =
933                                     chk->rec.data.TSN_seq - 1;
934                                 asoc->str_of_pdapi =
935                                     chk->rec.data.stream_number;
936                                 asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
937                                 asoc->pdapi_ppid = chk->rec.data.payloadtype;
938                                 asoc->fragment_flags = chk->rec.data.rcv_flags;
939                                 sctp_service_reassembly(stcb, asoc);
940                         }
941                 }
942         } else {
943                 /*
944                  * Service re-assembly will deliver stream data queued at
945                  * the end of fragmented delivery.. but it wont know to go
946                  * back and call itself again... we do that here with the
947                  * got doit_again
948                  */
949                 sctp_service_reassembly(stcb, asoc);
950                 if (asoc->fragmented_delivery_inprogress == 0) {
951                         /*
952                          * finished our Fragmented delivery, could be more
953                          * waiting?
954                          */
955                         goto doit_again;
956                 }
957         }
958 }
959
960 /*
961  * Dump onto the re-assembly queue, in its proper place. After dumping on the
962  * queue, see if anthing can be delivered. If so pull it off (or as much as
963  * we can. If we run out of space then we must dump what we can and set the
964  * appropriate flag to say we queued what we could.
965  */
966 static void
967 sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
968     struct sctp_tmit_chunk *chk, int *abort_flag)
969 {
970         struct mbuf *oper;
971         uint32_t cum_ackp1, last_tsn, prev_tsn, post_tsn;
972         u_char last_flags;
973         struct sctp_tmit_chunk *at, *prev, *next;
974
975         prev = next = NULL;
976         cum_ackp1 = asoc->tsn_last_delivered + 1;
977         if (TAILQ_EMPTY(&asoc->reasmqueue)) {
978                 /* This is the first one on the queue */
979                 TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
980                 /*
981                  * we do not check for delivery of anything when only one
982                  * fragment is here
983                  */
984                 asoc->size_on_reasm_queue = chk->send_size;
985                 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
986                 if (chk->rec.data.TSN_seq == cum_ackp1) {
987                         if (asoc->fragmented_delivery_inprogress == 0 &&
988                             (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
989                             SCTP_DATA_FIRST_FRAG) {
990                                 /*
991                                  * An empty queue, no delivery inprogress,
992                                  * we hit the next one and it does NOT have
993                                  * a FIRST fragment mark.
994                                  */
995                                 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
996                                 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
997                                     0, M_DONTWAIT, 1, MT_DATA);
998
999                                 if (oper) {
1000                                         struct sctp_paramhdr *ph;
1001                                         uint32_t *ippp;
1002
1003                                         SCTP_BUF_LEN(oper) =
1004                                             sizeof(struct sctp_paramhdr) +
1005                                             (sizeof(uint32_t) * 3);
1006                                         ph = mtod(oper, struct sctp_paramhdr *);
1007                                         ph->param_type =
1008                                             htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1009                                         ph->param_length = htons(SCTP_BUF_LEN(oper));
1010                                         ippp = (uint32_t *) (ph + 1);
1011                                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_2);
1012                                         ippp++;
1013                                         *ippp = chk->rec.data.TSN_seq;
1014                                         ippp++;
1015                                         *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1016
1017                                 }
1018                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
1019                                 sctp_abort_an_association(stcb->sctp_ep, stcb,
1020                                     SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1021                                 *abort_flag = 1;
1022                         } else if (asoc->fragmented_delivery_inprogress &&
1023                             (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1024                                 /*
1025                                  * We are doing a partial delivery and the
1026                                  * NEXT chunk MUST be either the LAST or
1027                                  * MIDDLE fragment NOT a FIRST
1028                                  */
1029                                 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
1030                                 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1031                                     0, M_DONTWAIT, 1, MT_DATA);
1032                                 if (oper) {
1033                                         struct sctp_paramhdr *ph;
1034                                         uint32_t *ippp;
1035
1036                                         SCTP_BUF_LEN(oper) =
1037                                             sizeof(struct sctp_paramhdr) +
1038                                             (3 * sizeof(uint32_t));
1039                                         ph = mtod(oper, struct sctp_paramhdr *);
1040                                         ph->param_type =
1041                                             htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1042                                         ph->param_length = htons(SCTP_BUF_LEN(oper));
1043                                         ippp = (uint32_t *) (ph + 1);
1044                                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_3);
1045                                         ippp++;
1046                                         *ippp = chk->rec.data.TSN_seq;
1047                                         ippp++;
1048                                         *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1049                                 }
1050                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
1051                                 sctp_abort_an_association(stcb->sctp_ep, stcb,
1052                                     SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1053                                 *abort_flag = 1;
1054                         } else if (asoc->fragmented_delivery_inprogress) {
1055                                 /*
1056                                  * Here we are ok with a MIDDLE or LAST
1057                                  * piece
1058                                  */
1059                                 if (chk->rec.data.stream_number !=
1060                                     asoc->str_of_pdapi) {
1061                                         /* Got to be the right STR No */
1062                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
1063                                             chk->rec.data.stream_number,
1064                                             asoc->str_of_pdapi);
1065                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1066                                             0, M_DONTWAIT, 1, MT_DATA);
1067                                         if (oper) {
1068                                                 struct sctp_paramhdr *ph;
1069                                                 uint32_t *ippp;
1070
1071                                                 SCTP_BUF_LEN(oper) =
1072                                                     sizeof(struct sctp_paramhdr) +
1073                                                     (sizeof(uint32_t) * 3);
1074                                                 ph = mtod(oper,
1075                                                     struct sctp_paramhdr *);
1076                                                 ph->param_type =
1077                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1078                                                 ph->param_length =
1079                                                     htons(SCTP_BUF_LEN(oper));
1080                                                 ippp = (uint32_t *) (ph + 1);
1081                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
1082                                                 ippp++;
1083                                                 *ippp = chk->rec.data.TSN_seq;
1084                                                 ippp++;
1085                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1086                                         }
1087                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
1088                                         sctp_abort_an_association(stcb->sctp_ep,
1089                                             stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1090                                         *abort_flag = 1;
1091                                 } else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
1092                                             SCTP_DATA_UNORDERED &&
1093                                             chk->rec.data.stream_seq !=
1094                                     asoc->ssn_of_pdapi) {
1095                                         /* Got to be the right STR Seq */
1096                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
1097                                             chk->rec.data.stream_seq,
1098                                             asoc->ssn_of_pdapi);
1099                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1100                                             0, M_DONTWAIT, 1, MT_DATA);
1101                                         if (oper) {
1102                                                 struct sctp_paramhdr *ph;
1103                                                 uint32_t *ippp;
1104
1105                                                 SCTP_BUF_LEN(oper) =
1106                                                     sizeof(struct sctp_paramhdr) +
1107                                                     (3 * sizeof(uint32_t));
1108                                                 ph = mtod(oper,
1109                                                     struct sctp_paramhdr *);
1110                                                 ph->param_type =
1111                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1112                                                 ph->param_length =
1113                                                     htons(SCTP_BUF_LEN(oper));
1114                                                 ippp = (uint32_t *) (ph + 1);
1115                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
1116                                                 ippp++;
1117                                                 *ippp = chk->rec.data.TSN_seq;
1118                                                 ippp++;
1119                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1120
1121                                         }
1122                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
1123                                         sctp_abort_an_association(stcb->sctp_ep,
1124                                             stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1125                                         *abort_flag = 1;
1126                                 }
1127                         }
1128                 }
1129                 return;
1130         }
1131         /* Find its place */
1132         TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1133                 if (compare_with_wrap(at->rec.data.TSN_seq,
1134                     chk->rec.data.TSN_seq, MAX_TSN)) {
1135                         /*
1136                          * one in queue is bigger than the new one, insert
1137                          * before this one
1138                          */
1139                         /* A check */
1140                         asoc->size_on_reasm_queue += chk->send_size;
1141                         sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1142                         next = at;
1143                         TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1144                         break;
1145                 } else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
1146                         /* Gak, He sent me a duplicate str seq number */
1147                         /*
1148                          * foo bar, I guess I will just free this new guy,
1149                          * should we abort too? FIX ME MAYBE? Or it COULD be
1150                          * that the SSN's have wrapped. Maybe I should
1151                          * compare to TSN somehow... sigh for now just blow
1152                          * away the chunk!
1153                          */
1154                         if (chk->data) {
1155                                 sctp_m_freem(chk->data);
1156                                 chk->data = NULL;
1157                         }
1158                         sctp_free_a_chunk(stcb, chk);
1159                         return;
1160                 } else {
1161                         last_flags = at->rec.data.rcv_flags;
1162                         last_tsn = at->rec.data.TSN_seq;
1163                         prev = at;
1164                         if (TAILQ_NEXT(at, sctp_next) == NULL) {
1165                                 /*
1166                                  * We are at the end, insert it after this
1167                                  * one
1168                                  */
1169                                 /* check it first */
1170                                 asoc->size_on_reasm_queue += chk->send_size;
1171                                 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1172                                 TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
1173                                 break;
1174                         }
1175                 }
1176         }
1177         /* Now the audits */
1178         if (prev) {
1179                 prev_tsn = chk->rec.data.TSN_seq - 1;
1180                 if (prev_tsn == prev->rec.data.TSN_seq) {
1181                         /*
1182                          * Ok the one I am dropping onto the end is the
1183                          * NEXT. A bit of valdiation here.
1184                          */
1185                         if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1186                             SCTP_DATA_FIRST_FRAG ||
1187                             (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1188                             SCTP_DATA_MIDDLE_FRAG) {
1189                                 /*
1190                                  * Insert chk MUST be a MIDDLE or LAST
1191                                  * fragment
1192                                  */
1193                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1194                                     SCTP_DATA_FIRST_FRAG) {
1195                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
1196                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
1197                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1198                                             0, M_DONTWAIT, 1, MT_DATA);
1199                                         if (oper) {
1200                                                 struct sctp_paramhdr *ph;
1201                                                 uint32_t *ippp;
1202
1203                                                 SCTP_BUF_LEN(oper) =
1204                                                     sizeof(struct sctp_paramhdr) +
1205                                                     (3 * sizeof(uint32_t));
1206                                                 ph = mtod(oper,
1207                                                     struct sctp_paramhdr *);
1208                                                 ph->param_type =
1209                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1210                                                 ph->param_length =
1211                                                     htons(SCTP_BUF_LEN(oper));
1212                                                 ippp = (uint32_t *) (ph + 1);
1213                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1214                                                 ippp++;
1215                                                 *ippp = chk->rec.data.TSN_seq;
1216                                                 ippp++;
1217                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1218
1219                                         }
1220                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
1221                                         sctp_abort_an_association(stcb->sctp_ep,
1222                                             stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1223                                         *abort_flag = 1;
1224                                         return;
1225                                 }
1226                                 if (chk->rec.data.stream_number !=
1227                                     prev->rec.data.stream_number) {
1228                                         /*
1229                                          * Huh, need the correct STR here,
1230                                          * they must be the same.
1231                                          */
1232                                         SCTP_PRINTF("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1233                                             chk->rec.data.stream_number,
1234                                             prev->rec.data.stream_number);
1235                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1236                                             0, M_DONTWAIT, 1, MT_DATA);
1237                                         if (oper) {
1238                                                 struct sctp_paramhdr *ph;
1239                                                 uint32_t *ippp;
1240
1241                                                 SCTP_BUF_LEN(oper) =
1242                                                     sizeof(struct sctp_paramhdr) +
1243                                                     (3 * sizeof(uint32_t));
1244                                                 ph = mtod(oper,
1245                                                     struct sctp_paramhdr *);
1246                                                 ph->param_type =
1247                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1248                                                 ph->param_length =
1249                                                     htons(SCTP_BUF_LEN(oper));
1250                                                 ippp = (uint32_t *) (ph + 1);
1251                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1252                                                 ippp++;
1253                                                 *ippp = chk->rec.data.TSN_seq;
1254                                                 ippp++;
1255                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1256                                         }
1257                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1258                                         sctp_abort_an_association(stcb->sctp_ep,
1259                                             stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1260
1261                                         *abort_flag = 1;
1262                                         return;
1263                                 }
1264                                 if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1265                                     chk->rec.data.stream_seq !=
1266                                     prev->rec.data.stream_seq) {
1267                                         /*
1268                                          * Huh, need the correct STR here,
1269                                          * they must be the same.
1270                                          */
1271                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1272                                             chk->rec.data.stream_seq,
1273                                             prev->rec.data.stream_seq);
1274                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1275                                             0, M_DONTWAIT, 1, MT_DATA);
1276                                         if (oper) {
1277                                                 struct sctp_paramhdr *ph;
1278                                                 uint32_t *ippp;
1279
1280                                                 SCTP_BUF_LEN(oper) =
1281                                                     sizeof(struct sctp_paramhdr) +
1282                                                     (3 * sizeof(uint32_t));
1283                                                 ph = mtod(oper,
1284                                                     struct sctp_paramhdr *);
1285                                                 ph->param_type =
1286                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1287                                                 ph->param_length =
1288                                                     htons(SCTP_BUF_LEN(oper));
1289                                                 ippp = (uint32_t *) (ph + 1);
1290                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1291                                                 ippp++;
1292                                                 *ippp = chk->rec.data.TSN_seq;
1293                                                 ippp++;
1294                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1295                                         }
1296                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1297                                         sctp_abort_an_association(stcb->sctp_ep,
1298                                             stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1299
1300                                         *abort_flag = 1;
1301                                         return;
1302                                 }
1303                         } else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1304                             SCTP_DATA_LAST_FRAG) {
1305                                 /* Insert chk MUST be a FIRST */
1306                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1307                                     SCTP_DATA_FIRST_FRAG) {
1308                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1309                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1310                                             0, M_DONTWAIT, 1, MT_DATA);
1311                                         if (oper) {
1312                                                 struct sctp_paramhdr *ph;
1313                                                 uint32_t *ippp;
1314
1315                                                 SCTP_BUF_LEN(oper) =
1316                                                     sizeof(struct sctp_paramhdr) +
1317                                                     (3 * sizeof(uint32_t));
1318                                                 ph = mtod(oper,
1319                                                     struct sctp_paramhdr *);
1320                                                 ph->param_type =
1321                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1322                                                 ph->param_length =
1323                                                     htons(SCTP_BUF_LEN(oper));
1324                                                 ippp = (uint32_t *) (ph + 1);
1325                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1326                                                 ippp++;
1327                                                 *ippp = chk->rec.data.TSN_seq;
1328                                                 ippp++;
1329                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1330
1331                                         }
1332                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1333                                         sctp_abort_an_association(stcb->sctp_ep,
1334                                             stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1335
1336                                         *abort_flag = 1;
1337                                         return;
1338                                 }
1339                         }
1340                 }
1341         }
1342         if (next) {
1343                 post_tsn = chk->rec.data.TSN_seq + 1;
1344                 if (post_tsn == next->rec.data.TSN_seq) {
1345                         /*
1346                          * Ok the one I am inserting ahead of is my NEXT
1347                          * one. A bit of valdiation here.
1348                          */
1349                         if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1350                                 /* Insert chk MUST be a last fragment */
1351                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1352                                     != SCTP_DATA_LAST_FRAG) {
1353                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1354                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1355                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1356                                             0, M_DONTWAIT, 1, MT_DATA);
1357                                         if (oper) {
1358                                                 struct sctp_paramhdr *ph;
1359                                                 uint32_t *ippp;
1360
1361                                                 SCTP_BUF_LEN(oper) =
1362                                                     sizeof(struct sctp_paramhdr) +
1363                                                     (3 * sizeof(uint32_t));
1364                                                 ph = mtod(oper,
1365                                                     struct sctp_paramhdr *);
1366                                                 ph->param_type =
1367                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1368                                                 ph->param_length =
1369                                                     htons(SCTP_BUF_LEN(oper));
1370                                                 ippp = (uint32_t *) (ph + 1);
1371                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1372                                                 ippp++;
1373                                                 *ippp = chk->rec.data.TSN_seq;
1374                                                 ippp++;
1375                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1376                                         }
1377                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1378                                         sctp_abort_an_association(stcb->sctp_ep,
1379                                             stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1380
1381                                         *abort_flag = 1;
1382                                         return;
1383                                 }
1384                         } else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1385                                     SCTP_DATA_MIDDLE_FRAG ||
1386                                     (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1387                             SCTP_DATA_LAST_FRAG) {
1388                                 /*
1389                                  * Insert chk CAN be MIDDLE or FIRST NOT
1390                                  * LAST
1391                                  */
1392                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1393                                     SCTP_DATA_LAST_FRAG) {
1394                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1395                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1396                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1397                                             0, M_DONTWAIT, 1, MT_DATA);
1398                                         if (oper) {
1399                                                 struct sctp_paramhdr *ph;
1400                                                 uint32_t *ippp;
1401
1402                                                 SCTP_BUF_LEN(oper) =
1403                                                     sizeof(struct sctp_paramhdr) +
1404                                                     (3 * sizeof(uint32_t));
1405                                                 ph = mtod(oper,
1406                                                     struct sctp_paramhdr *);
1407                                                 ph->param_type =
1408                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1409                                                 ph->param_length =
1410                                                     htons(SCTP_BUF_LEN(oper));
1411                                                 ippp = (uint32_t *) (ph + 1);
1412                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1413                                                 ippp++;
1414                                                 *ippp = chk->rec.data.TSN_seq;
1415                                                 ippp++;
1416                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1417
1418                                         }
1419                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1420                                         sctp_abort_an_association(stcb->sctp_ep,
1421                                             stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1422
1423                                         *abort_flag = 1;
1424                                         return;
1425                                 }
1426                                 if (chk->rec.data.stream_number !=
1427                                     next->rec.data.stream_number) {
1428                                         /*
1429                                          * Huh, need the correct STR here,
1430                                          * they must be the same.
1431                                          */
1432                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1433                                             chk->rec.data.stream_number,
1434                                             next->rec.data.stream_number);
1435                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1436                                             0, M_DONTWAIT, 1, MT_DATA);
1437                                         if (oper) {
1438                                                 struct sctp_paramhdr *ph;
1439                                                 uint32_t *ippp;
1440
1441                                                 SCTP_BUF_LEN(oper) =
1442                                                     sizeof(struct sctp_paramhdr) +
1443                                                     (3 * sizeof(uint32_t));
1444                                                 ph = mtod(oper,
1445                                                     struct sctp_paramhdr *);
1446                                                 ph->param_type =
1447                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1448                                                 ph->param_length =
1449                                                     htons(SCTP_BUF_LEN(oper));
1450                                                 ippp = (uint32_t *) (ph + 1);
1451                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1452                                                 ippp++;
1453                                                 *ippp = chk->rec.data.TSN_seq;
1454                                                 ippp++;
1455                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1456
1457                                         }
1458                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1459                                         sctp_abort_an_association(stcb->sctp_ep,
1460                                             stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1461
1462                                         *abort_flag = 1;
1463                                         return;
1464                                 }
1465                                 if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1466                                     chk->rec.data.stream_seq !=
1467                                     next->rec.data.stream_seq) {
1468                                         /*
1469                                          * Huh, need the correct STR here,
1470                                          * they must be the same.
1471                                          */
1472                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1473                                             chk->rec.data.stream_seq,
1474                                             next->rec.data.stream_seq);
1475                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1476                                             0, M_DONTWAIT, 1, MT_DATA);
1477                                         if (oper) {
1478                                                 struct sctp_paramhdr *ph;
1479                                                 uint32_t *ippp;
1480
1481                                                 SCTP_BUF_LEN(oper) =
1482                                                     sizeof(struct sctp_paramhdr) +
1483                                                     (3 * sizeof(uint32_t));
1484                                                 ph = mtod(oper,
1485                                                     struct sctp_paramhdr *);
1486                                                 ph->param_type =
1487                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1488                                                 ph->param_length =
1489                                                     htons(SCTP_BUF_LEN(oper));
1490                                                 ippp = (uint32_t *) (ph + 1);
1491                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1492                                                 ippp++;
1493                                                 *ippp = chk->rec.data.TSN_seq;
1494                                                 ippp++;
1495                                                 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1496                                         }
1497                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1498                                         sctp_abort_an_association(stcb->sctp_ep,
1499                                             stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1500
1501                                         *abort_flag = 1;
1502                                         return;
1503                                 }
1504                         }
1505                 }
1506         }
1507         /* Do we need to do some delivery? check */
1508         sctp_deliver_reasm_check(stcb, asoc);
1509 }
1510
1511 /*
1512  * This is an unfortunate routine. It checks to make sure a evil guy is not
1513  * stuffing us full of bad packet fragments. A broken peer could also do this
1514  * but this is doubtful. It is to bad I must worry about evil crackers sigh
1515  * :< more cycles.
1516  */
1517 static int
1518 sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1519     uint32_t TSN_seq)
1520 {
1521         struct sctp_tmit_chunk *at;
1522         uint32_t tsn_est;
1523
1524         TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1525                 if (compare_with_wrap(TSN_seq,
1526                     at->rec.data.TSN_seq, MAX_TSN)) {
1527                         /* is it one bigger? */
1528                         tsn_est = at->rec.data.TSN_seq + 1;
1529                         if (tsn_est == TSN_seq) {
1530                                 /* yep. It better be a last then */
1531                                 if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1532                                     SCTP_DATA_LAST_FRAG) {
1533                                         /*
1534                                          * Ok this guy belongs next to a guy
1535                                          * that is NOT last, it should be a
1536                                          * middle/last, not a complete
1537                                          * chunk.
1538                                          */
1539                                         return (1);
1540                                 } else {
1541                                         /*
1542                                          * This guy is ok since its a LAST
1543                                          * and the new chunk is a fully
1544                                          * self- contained one.
1545                                          */
1546                                         return (0);
1547                                 }
1548                         }
1549                 } else if (TSN_seq == at->rec.data.TSN_seq) {
1550                         /* Software error since I have a dup? */
1551                         return (1);
1552                 } else {
1553                         /*
1554                          * Ok, 'at' is larger than new chunk but does it
1555                          * need to be right before it.
1556                          */
1557                         tsn_est = TSN_seq + 1;
1558                         if (tsn_est == at->rec.data.TSN_seq) {
1559                                 /* Yep, It better be a first */
1560                                 if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1561                                     SCTP_DATA_FIRST_FRAG) {
1562                                         return (1);
1563                                 } else {
1564                                         return (0);
1565                                 }
1566                         }
1567                 }
1568         }
1569         return (0);
1570 }
1571
1572
1573 static int
1574 sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1575     struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1576     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1577     int *break_flag, int last_chunk)
1578 {
1579         /* Process a data chunk */
1580         /* struct sctp_tmit_chunk *chk; */
1581         struct sctp_tmit_chunk *chk;
1582         uint32_t tsn, gap;
1583
1584         /* EY - for nr_sack */
1585         uint32_t nr_gap;
1586         struct mbuf *dmbuf;
1587         int indx, the_len;
1588         int need_reasm_check = 0;
1589         uint16_t strmno, strmseq;
1590         struct mbuf *oper;
1591         struct sctp_queued_to_read *control;
1592         int ordered;
1593         uint32_t protocol_id;
1594         uint8_t chunk_flags;
1595         struct sctp_stream_reset_list *liste;
1596
1597         chk = NULL;
1598         tsn = ntohl(ch->dp.tsn);
1599         chunk_flags = ch->ch.chunk_flags;
1600         if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1601                 asoc->send_sack = 1;
1602         }
1603         protocol_id = ch->dp.protocol_id;
1604         ordered = ((ch->ch.chunk_flags & SCTP_DATA_UNORDERED) == 0);
1605         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1606                 sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
1607         }
1608         if (stcb == NULL) {
1609                 return (0);
1610         }
1611         SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
1612         if (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN) ||
1613             asoc->cumulative_tsn == tsn) {
1614                 /* It is a duplicate */
1615                 SCTP_STAT_INCR(sctps_recvdupdata);
1616                 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1617                         /* Record a dup for the next outbound sack */
1618                         asoc->dup_tsns[asoc->numduptsns] = tsn;
1619                         asoc->numduptsns++;
1620                 }
1621                 asoc->send_sack = 1;
1622                 return (0);
1623         }
1624         /* Calculate the number of TSN's between the base and this TSN */
1625         SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1626         if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1627                 /* Can't hold the bit in the mapping at max array, toss it */
1628                 return (0);
1629         }
1630         if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1631                 SCTP_TCB_LOCK_ASSERT(stcb);
1632                 if (sctp_expand_mapping_array(asoc, gap)) {
1633                         /* Can't expand, drop it */
1634                         return (0);
1635                 }
1636         }
1637         /* EY - for nr_sack */
1638         nr_gap = gap;
1639
1640         if (compare_with_wrap(tsn, *high_tsn, MAX_TSN)) {
1641                 *high_tsn = tsn;
1642         }
1643         /* See if we have received this one already */
1644         if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
1645                 SCTP_STAT_INCR(sctps_recvdupdata);
1646                 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1647                         /* Record a dup for the next outbound sack */
1648                         asoc->dup_tsns[asoc->numduptsns] = tsn;
1649                         asoc->numduptsns++;
1650                 }
1651                 asoc->send_sack = 1;
1652                 return (0);
1653         }
1654         /*
1655          * Check to see about the GONE flag, duplicates would cause a sack
1656          * to be sent up above
1657          */
1658         if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1659             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1660             (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1661             ) {
1662                 /*
1663                  * wait a minute, this guy is gone, there is no longer a
1664                  * receiver. Send peer an ABORT!
1665                  */
1666                 struct mbuf *op_err;
1667
1668                 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1669                 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err, SCTP_SO_NOT_LOCKED);
1670                 *abort_flag = 1;
1671                 return (0);
1672         }
1673         /*
1674          * Now before going further we see if there is room. If NOT then we
1675          * MAY let one through only IF this TSN is the one we are waiting
1676          * for on a partial delivery API.
1677          */
1678
1679         /* now do the tests */
1680         if (((asoc->cnt_on_all_streams +
1681             asoc->cnt_on_reasm_queue +
1682             asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1683             (((int)asoc->my_rwnd) <= 0)) {
1684                 /*
1685                  * When we have NO room in the rwnd we check to make sure
1686                  * the reader is doing its job...
1687                  */
1688                 if (stcb->sctp_socket->so_rcv.sb_cc) {
1689                         /* some to read, wake-up */
1690 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1691                         struct socket *so;
1692
1693                         so = SCTP_INP_SO(stcb->sctp_ep);
1694                         atomic_add_int(&stcb->asoc.refcnt, 1);
1695                         SCTP_TCB_UNLOCK(stcb);
1696                         SCTP_SOCKET_LOCK(so, 1);
1697                         SCTP_TCB_LOCK(stcb);
1698                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
1699                         if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1700                                 /* assoc was freed while we were unlocked */
1701                                 SCTP_SOCKET_UNLOCK(so, 1);
1702                                 return (0);
1703                         }
1704 #endif
1705                         sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1706 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1707                         SCTP_SOCKET_UNLOCK(so, 1);
1708 #endif
1709                 }
1710                 /* now is it in the mapping array of what we have accepted? */
1711                 if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1712                         /* Nope not in the valid range dump it */
1713                         sctp_set_rwnd(stcb, asoc);
1714                         if ((asoc->cnt_on_all_streams +
1715                             asoc->cnt_on_reasm_queue +
1716                             asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
1717                                 SCTP_STAT_INCR(sctps_datadropchklmt);
1718                         } else {
1719                                 SCTP_STAT_INCR(sctps_datadroprwnd);
1720                         }
1721                         indx = *break_flag;
1722                         *break_flag = 1;
1723                         return (0);
1724                 }
1725         }
1726         strmno = ntohs(ch->dp.stream_id);
1727         if (strmno >= asoc->streamincnt) {
1728                 struct sctp_paramhdr *phdr;
1729                 struct mbuf *mb;
1730
1731                 mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1732                     0, M_DONTWAIT, 1, MT_DATA);
1733                 if (mb != NULL) {
1734                         /* add some space up front so prepend will work well */
1735                         SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1736                         phdr = mtod(mb, struct sctp_paramhdr *);
1737                         /*
1738                          * Error causes are just param's and this one has
1739                          * two back to back phdr, one with the error type
1740                          * and size, the other with the streamid and a rsvd
1741                          */
1742                         SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1743                         phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1744                         phdr->param_length =
1745                             htons(sizeof(struct sctp_paramhdr) * 2);
1746                         phdr++;
1747                         /* We insert the stream in the type field */
1748                         phdr->param_type = ch->dp.stream_id;
1749                         /* And set the length to 0 for the rsvd field */
1750                         phdr->param_length = 0;
1751                         sctp_queue_op_err(stcb, mb);
1752                 }
1753                 SCTP_STAT_INCR(sctps_badsid);
1754                 SCTP_TCB_LOCK_ASSERT(stcb);
1755                 SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
1756                 /* EY set this tsn present in  nr_sack's nr_mapping_array */
1757                 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
1758                         SCTP_TCB_LOCK_ASSERT(stcb);
1759                         SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
1760                         SCTP_REVERSE_OUT_TSN_PRES(gap, tsn, asoc);
1761                 }
1762                 if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1763                         /* we have a new high score */
1764                         asoc->highest_tsn_inside_map = tsn;
1765                         /* EY nr_sack version of the above */
1766                         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack)
1767                                 asoc->highest_tsn_inside_nr_map = tsn;
1768                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1769                                 sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
1770                         }
1771                 }
1772                 if (tsn == (asoc->cumulative_tsn + 1)) {
1773                         /* Update cum-ack */
1774                         asoc->cumulative_tsn = tsn;
1775                 }
1776                 return (0);
1777         }
1778         /*
1779          * Before we continue lets validate that we are not being fooled by
1780          * an evil attacker. We can only have 4k chunks based on our TSN
1781          * spread allowed by the mapping array 512 * 8 bits, so there is no
1782          * way our stream sequence numbers could have wrapped. We of course
1783          * only validate the FIRST fragment so the bit must be set.
1784          */
1785         strmseq = ntohs(ch->dp.stream_sequence);
1786 #ifdef SCTP_ASOCLOG_OF_TSNS
1787         SCTP_TCB_LOCK_ASSERT(stcb);
1788         if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
1789                 asoc->tsn_in_at = 0;
1790                 asoc->tsn_in_wrapped = 1;
1791         }
1792         asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1793         asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1794         asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1795         asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1796         asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
1797         asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
1798         asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
1799         asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1800         asoc->tsn_in_at++;
1801 #endif
1802         if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1803             (TAILQ_EMPTY(&asoc->resetHead)) &&
1804             (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
1805             (compare_with_wrap(asoc->strmin[strmno].last_sequence_delivered,
1806             strmseq, MAX_SEQ) ||
1807             asoc->strmin[strmno].last_sequence_delivered == strmseq)) {
1808                 /* The incoming sseq is behind where we last delivered? */
1809                 SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1810                     strmseq, asoc->strmin[strmno].last_sequence_delivered);
1811                 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1812                     0, M_DONTWAIT, 1, MT_DATA);
1813                 if (oper) {
1814                         struct sctp_paramhdr *ph;
1815                         uint32_t *ippp;
1816
1817                         SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1818                             (3 * sizeof(uint32_t));
1819                         ph = mtod(oper, struct sctp_paramhdr *);
1820                         ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1821                         ph->param_length = htons(SCTP_BUF_LEN(oper));
1822                         ippp = (uint32_t *) (ph + 1);
1823                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1824                         ippp++;
1825                         *ippp = tsn;
1826                         ippp++;
1827                         *ippp = ((strmno << 16) | strmseq);
1828
1829                 }
1830                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1831                 sctp_abort_an_association(stcb->sctp_ep, stcb,
1832                     SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1833                 *abort_flag = 1;
1834                 return (0);
1835         }
1836         /************************************
1837          * From here down we may find ch-> invalid
1838          * so its a good idea NOT to use it.
1839          *************************************/
1840
1841         the_len = (chk_length - sizeof(struct sctp_data_chunk));
1842         if (last_chunk == 0) {
1843                 dmbuf = SCTP_M_COPYM(*m,
1844                     (offset + sizeof(struct sctp_data_chunk)),
1845                     the_len, M_DONTWAIT);
1846 #ifdef SCTP_MBUF_LOGGING
1847                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
1848                         struct mbuf *mat;
1849
1850                         mat = dmbuf;
1851                         while (mat) {
1852                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
1853                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1854                                 }
1855                                 mat = SCTP_BUF_NEXT(mat);
1856                         }
1857                 }
1858 #endif
1859         } else {
1860                 /* We can steal the last chunk */
1861                 int l_len;
1862
1863                 dmbuf = *m;
1864                 /* lop off the top part */
1865                 m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1866                 if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1867                         l_len = SCTP_BUF_LEN(dmbuf);
1868                 } else {
1869                         /*
1870                          * need to count up the size hopefully does not hit
1871                          * this to often :-0
1872                          */
1873                         struct mbuf *lat;
1874
1875                         l_len = 0;
1876                         lat = dmbuf;
1877                         while (lat) {
1878                                 l_len += SCTP_BUF_LEN(lat);
1879                                 lat = SCTP_BUF_NEXT(lat);
1880                         }
1881                 }
1882                 if (l_len > the_len) {
1883                         /* Trim the end round bytes off  too */
1884                         m_adj(dmbuf, -(l_len - the_len));
1885                 }
1886         }
1887         if (dmbuf == NULL) {
1888                 SCTP_STAT_INCR(sctps_nomem);
1889                 return (0);
1890         }
1891         if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1892             asoc->fragmented_delivery_inprogress == 0 &&
1893             TAILQ_EMPTY(&asoc->resetHead) &&
1894             ((ordered == 0) ||
1895             ((asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1896             TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1897                 /* Candidate for express delivery */
1898                 /*
1899                  * Its not fragmented, No PD-API is up, Nothing in the
1900                  * delivery queue, Its un-ordered OR ordered and the next to
1901                  * deliver AND nothing else is stuck on the stream queue,
1902                  * And there is room for it in the socket buffer. Lets just
1903                  * stuff it up the buffer....
1904                  */
1905
1906                 /* It would be nice to avoid this copy if we could :< */
1907                 sctp_alloc_a_readq(stcb, control);
1908                 sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1909                     protocol_id,
1910                     stcb->asoc.context,
1911                     strmno, strmseq,
1912                     chunk_flags,
1913                     dmbuf);
1914                 if (control == NULL) {
1915                         goto failed_express_del;
1916                 }
1917                 sctp_add_to_readq(stcb->sctp_ep, stcb,
1918                     control, &stcb->sctp_socket->so_rcv,
1919                     1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1920
1921                 /*
1922                  * EY here I should check if this delivered tsn is
1923                  * out_of_order, if yes then update the nr_map
1924                  */
1925                 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
1926                         /*
1927                          * EY check if the mapping_array and nr_mapping
1928                          * array are consistent
1929                          */
1930                         if (asoc->mapping_array_base_tsn != asoc->nr_mapping_array_base_tsn)
1931                                 /*
1932                                  * printf("EY-IN
1933                                  * sctp_process_a_data_chunk(5): Something
1934                                  * is wrong the map base tsn" "\nEY-and
1935                                  * nr_map base tsn should be equal.");
1936                                  */
1937                                 /* EY debugging block */
1938                         {
1939                                 /*
1940                                  * printf("\nEY-Calculating an
1941                                  * nr_gap!!\nmapping_array_size = %d
1942                                  * nr_mapping_array_size = %d"
1943                                  * "\nEY-mapping_array_base = %d
1944                                  * nr_mapping_array_base =
1945                                  * %d\nEY-highest_tsn_inside_map = %d"
1946                                  * "highest_tsn_inside_nr_map = %d\nEY-TSN =
1947                                  * %d nr_gap = %d",asoc->mapping_array_size,
1948                                  * asoc->nr_mapping_array_size,
1949                                  * asoc->mapping_array_base_tsn,
1950                                  * asoc->nr_mapping_array_base_tsn,
1951                                  * asoc->highest_tsn_inside_map,
1952                                  * asoc->highest_tsn_inside_nr_map,tsn,nr_gap
1953                                  * );
1954                                  */
1955                         }
1956                         /* EY - not %100 sure about the lock thing */
1957                         SCTP_TCB_LOCK_ASSERT(stcb);
1958                         SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
1959                         SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
1960                         if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
1961                                 asoc->highest_tsn_inside_nr_map = tsn;
1962                 }
1963                 if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1964                         /* for ordered, bump what we delivered */
1965                         asoc->strmin[strmno].last_sequence_delivered++;
1966                 }
1967                 SCTP_STAT_INCR(sctps_recvexpress);
1968                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
1969                         sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1970                             SCTP_STR_LOG_FROM_EXPRS_DEL);
1971                 }
1972                 control = NULL;
1973                 goto finish_express_del;
1974         }
1975 failed_express_del:
1976         /* If we reach here this is a new chunk */
1977         chk = NULL;
1978         control = NULL;
1979         /* Express for fragmented delivery? */
1980         if ((asoc->fragmented_delivery_inprogress) &&
1981             (stcb->asoc.control_pdapi) &&
1982             (asoc->str_of_pdapi == strmno) &&
1983             (asoc->ssn_of_pdapi == strmseq)
1984             ) {
1985                 control = stcb->asoc.control_pdapi;
1986                 if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1987                         /* Can't be another first? */
1988                         goto failed_pdapi_express_del;
1989                 }
1990                 if (tsn == (control->sinfo_tsn + 1)) {
1991                         /* Yep, we can add it on */
1992                         int end = 0;
1993                         uint32_t cumack;
1994
1995                         if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1996                                 end = 1;
1997                         }
1998                         cumack = asoc->cumulative_tsn;
1999                         if ((cumack + 1) == tsn)
2000                                 cumack = tsn;
2001
2002                         if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
2003                             tsn,
2004                             &stcb->sctp_socket->so_rcv)) {
2005                                 SCTP_PRINTF("Append fails end:%d\n", end);
2006                                 goto failed_pdapi_express_del;
2007                         }
2008                         /*
2009                          * EY It is appended to the read queue in prev if
2010                          * block here I should check if this delivered tsn
2011                          * is out_of_order, if yes then update the nr_map
2012                          */
2013                         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2014                                 /* EY debugging block */
2015                                 {
2016                                         /*
2017                                          * printf("\nEY-Calculating an
2018                                          * nr_gap!!\nEY-mapping_array_size =
2019                                          * %d nr_mapping_array_size = %d"
2020                                          * "\nEY-mapping_array_base = %d
2021                                          * nr_mapping_array_base =
2022                                          * %d\nEY-highest_tsn_inside_map =
2023                                          * %d" "highest_tsn_inside_nr_map =
2024                                          * %d\nEY-TSN = %d nr_gap =
2025                                          * %d",asoc->mapping_array_size,
2026                                          * asoc->nr_mapping_array_size,
2027                                          * asoc->mapping_array_base_tsn,
2028                                          * asoc->nr_mapping_array_base_tsn,
2029                                          * asoc->highest_tsn_inside_map,
2030                                          * asoc->highest_tsn_inside_nr_map,ts
2031                                          * n,nr_gap);
2032                                          */
2033                                 }
2034                                 /* EY - not %100 sure about the lock thing */
2035                                 SCTP_TCB_LOCK_ASSERT(stcb);
2036                                 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
2037                                 SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
2038                                 if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
2039                                         asoc->highest_tsn_inside_nr_map = tsn;
2040                         }
2041                         SCTP_STAT_INCR(sctps_recvexpressm);
2042                         control->sinfo_tsn = tsn;
2043                         asoc->tsn_last_delivered = tsn;
2044                         asoc->fragment_flags = chunk_flags;
2045                         asoc->tsn_of_pdapi_last_delivered = tsn;
2046                         asoc->last_flags_delivered = chunk_flags;
2047                         asoc->last_strm_seq_delivered = strmseq;
2048                         asoc->last_strm_no_delivered = strmno;
2049                         if (end) {
2050                                 /* clean up the flags and such */
2051                                 asoc->fragmented_delivery_inprogress = 0;
2052                                 if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
2053                                         asoc->strmin[strmno].last_sequence_delivered++;
2054                                 }
2055                                 stcb->asoc.control_pdapi = NULL;
2056                                 if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
2057                                         /*
2058                                          * There could be another message
2059                                          * ready
2060                                          */
2061                                         need_reasm_check = 1;
2062                                 }
2063                         }
2064                         control = NULL;
2065                         goto finish_express_del;
2066                 }
2067         }
2068 failed_pdapi_express_del:
2069         control = NULL;
2070         if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
2071                 sctp_alloc_a_chunk(stcb, chk);
2072                 if (chk == NULL) {
2073                         /* No memory so we drop the chunk */
2074                         SCTP_STAT_INCR(sctps_nomem);
2075                         if (last_chunk == 0) {
2076                                 /* we copied it, free the copy */
2077                                 sctp_m_freem(dmbuf);
2078                         }
2079                         return (0);
2080                 }
2081                 chk->rec.data.TSN_seq = tsn;
2082                 chk->no_fr_allowed = 0;
2083                 chk->rec.data.stream_seq = strmseq;
2084                 chk->rec.data.stream_number = strmno;
2085                 chk->rec.data.payloadtype = protocol_id;
2086                 chk->rec.data.context = stcb->asoc.context;
2087                 chk->rec.data.doing_fast_retransmit = 0;
2088                 chk->rec.data.rcv_flags = chunk_flags;
2089                 chk->asoc = asoc;
2090                 chk->send_size = the_len;
2091                 chk->whoTo = net;
2092                 atomic_add_int(&net->ref_count, 1);
2093                 chk->data = dmbuf;
2094         } else {
2095                 sctp_alloc_a_readq(stcb, control);
2096                 sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
2097                     protocol_id,
2098                     stcb->asoc.context,
2099                     strmno, strmseq,
2100                     chunk_flags,
2101                     dmbuf);
2102                 if (control == NULL) {
2103                         /* No memory so we drop the chunk */
2104                         SCTP_STAT_INCR(sctps_nomem);
2105                         if (last_chunk == 0) {
2106                                 /* we copied it, free the copy */
2107                                 sctp_m_freem(dmbuf);
2108                         }
2109                         return (0);
2110                 }
2111                 control->length = the_len;
2112         }
2113
2114         /* Mark it as received */
2115         /* Now queue it where it belongs */
2116         if (control != NULL) {
2117                 /* First a sanity check */
2118                 if (asoc->fragmented_delivery_inprogress) {
2119                         /*
2120                          * Ok, we have a fragmented delivery in progress if
2121                          * this chunk is next to deliver OR belongs in our
2122                          * view to the reassembly, the peer is evil or
2123                          * broken.
2124                          */
2125                         uint32_t estimate_tsn;
2126
2127                         estimate_tsn = asoc->tsn_last_delivered + 1;
2128                         if (TAILQ_EMPTY(&asoc->reasmqueue) &&
2129                             (estimate_tsn == control->sinfo_tsn)) {
2130                                 /* Evil/Broke peer */
2131                                 sctp_m_freem(control->data);
2132                                 control->data = NULL;
2133                                 if (control->whoFrom) {
2134                                         sctp_free_remote_addr(control->whoFrom);
2135                                         control->whoFrom = NULL;
2136                                 }
2137                                 sctp_free_a_readq(stcb, control);
2138                                 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2139                                     0, M_DONTWAIT, 1, MT_DATA);
2140                                 if (oper) {
2141                                         struct sctp_paramhdr *ph;
2142                                         uint32_t *ippp;
2143
2144                                         SCTP_BUF_LEN(oper) =
2145                                             sizeof(struct sctp_paramhdr) +
2146                                             (3 * sizeof(uint32_t));
2147                                         ph = mtod(oper, struct sctp_paramhdr *);
2148                                         ph->param_type =
2149                                             htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2150                                         ph->param_length = htons(SCTP_BUF_LEN(oper));
2151                                         ippp = (uint32_t *) (ph + 1);
2152                                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
2153                                         ippp++;
2154                                         *ippp = tsn;
2155                                         ippp++;
2156                                         *ippp = ((strmno << 16) | strmseq);
2157                                 }
2158                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
2159                                 sctp_abort_an_association(stcb->sctp_ep, stcb,
2160                                     SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2161
2162                                 *abort_flag = 1;
2163                                 return (0);
2164                         } else {
2165                                 if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
2166                                         sctp_m_freem(control->data);
2167                                         control->data = NULL;
2168                                         if (control->whoFrom) {
2169                                                 sctp_free_remote_addr(control->whoFrom);
2170                                                 control->whoFrom = NULL;
2171                                         }
2172                                         sctp_free_a_readq(stcb, control);
2173
2174                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2175                                             0, M_DONTWAIT, 1, MT_DATA);
2176                                         if (oper) {
2177                                                 struct sctp_paramhdr *ph;
2178                                                 uint32_t *ippp;
2179
2180                                                 SCTP_BUF_LEN(oper) =
2181                                                     sizeof(struct sctp_paramhdr) +
2182                                                     (3 * sizeof(uint32_t));
2183                                                 ph = mtod(oper,
2184                                                     struct sctp_paramhdr *);
2185                                                 ph->param_type =
2186                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2187                                                 ph->param_length =
2188                                                     htons(SCTP_BUF_LEN(oper));
2189                                                 ippp = (uint32_t *) (ph + 1);
2190                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
2191                                                 ippp++;
2192                                                 *ippp = tsn;
2193                                                 ippp++;
2194                                                 *ippp = ((strmno << 16) | strmseq);
2195                                         }
2196                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
2197                                         sctp_abort_an_association(stcb->sctp_ep,
2198                                             stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2199
2200                                         *abort_flag = 1;
2201                                         return (0);
2202                                 }
2203                         }
2204                 } else {
2205                         /* No PDAPI running */
2206                         if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
2207                                 /*
2208                                  * Reassembly queue is NOT empty validate
2209                                  * that this tsn does not need to be in
2210                                  * reasembly queue. If it does then our peer
2211                                  * is broken or evil.
2212                                  */
2213                                 if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
2214                                         sctp_m_freem(control->data);
2215                                         control->data = NULL;
2216                                         if (control->whoFrom) {
2217                                                 sctp_free_remote_addr(control->whoFrom);
2218                                                 control->whoFrom = NULL;
2219                                         }
2220                                         sctp_free_a_readq(stcb, control);
2221                                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2222                                             0, M_DONTWAIT, 1, MT_DATA);
2223                                         if (oper) {
2224                                                 struct sctp_paramhdr *ph;
2225                                                 uint32_t *ippp;
2226
2227                                                 SCTP_BUF_LEN(oper) =
2228                                                     sizeof(struct sctp_paramhdr) +
2229                                                     (3 * sizeof(uint32_t));
2230                                                 ph = mtod(oper,
2231                                                     struct sctp_paramhdr *);
2232                                                 ph->param_type =
2233                                                     htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2234                                                 ph->param_length =
2235                                                     htons(SCTP_BUF_LEN(oper));
2236                                                 ippp = (uint32_t *) (ph + 1);
2237                                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
2238                                                 ippp++;
2239                                                 *ippp = tsn;
2240                                                 ippp++;
2241                                                 *ippp = ((strmno << 16) | strmseq);
2242                                         }
2243                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
2244                                         sctp_abort_an_association(stcb->sctp_ep,
2245                                             stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2246
2247                                         *abort_flag = 1;
2248                                         return (0);
2249                                 }
2250                         }
2251                 }
2252                 /* ok, if we reach here we have passed the sanity checks */
2253                 if (chunk_flags & SCTP_DATA_UNORDERED) {
2254                         /* queue directly into socket buffer */
2255                         sctp_add_to_readq(stcb->sctp_ep, stcb,
2256                             control,
2257                             &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
2258
2259                         /*
2260                          * EY It is added to the read queue in prev if block
2261                          * here I should check if this delivered tsn is
2262                          * out_of_order, if yes then update the nr_map
2263                          */
2264                         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2265                                 /*
2266                                  * EY check if the mapping_array and
2267                                  * nr_mapping array are consistent
2268                                  */
2269                                 if (asoc->mapping_array_base_tsn != asoc->nr_mapping_array_base_tsn)
2270                                         /*
2271                                          * printf("EY-IN
2272                                          * sctp_process_a_data_chunk(6):
2273                                          * Something is wrong the map base
2274                                          * tsn" "\nEY-and nr_map base tsn
2275                                          * should be equal.");
2276                                          */
2277                                         /*
2278                                          * EY - not %100 sure about the lock
2279                                          * thing, i think we don't need the
2280                                          * below,
2281                                          */
2282                                         /* SCTP_TCB_LOCK_ASSERT(stcb); */
2283                                 {
2284                                         /*
2285                                          * printf("\nEY-Calculating an
2286                                          * nr_gap!!\nEY-mapping_array_size =
2287                                          * %d nr_mapping_array_size = %d"
2288                                          * "\nEY-mapping_array_base = %d
2289                                          * nr_mapping_array_base =
2290                                          * %d\nEY-highest_tsn_inside_map =
2291                                          * %d" "highest_tsn_inside_nr_map =
2292                                          * %d\nEY-TSN = %d nr_gap =
2293                                          * %d",asoc->mapping_array_size,
2294                                          * asoc->nr_mapping_array_size,
2295                                          * asoc->mapping_array_base_tsn,
2296                                          * asoc->nr_mapping_array_base_tsn,
2297                                          * asoc->highest_tsn_inside_map,
2298                                          * asoc->highest_tsn_inside_nr_map,ts
2299                                          * n,nr_gap);
2300                                          */
2301                                 }
2302                                 SCTP_TCB_LOCK_ASSERT(stcb);
2303                                 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
2304                                 SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
2305                                 if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
2306                                         asoc->highest_tsn_inside_nr_map = tsn;
2307                         }
2308                 } else {
2309                         /*
2310                          * Special check for when streams are resetting. We
2311                          * could be more smart about this and check the
2312                          * actual stream to see if it is not being reset..
2313                          * that way we would not create a HOLB when amongst
2314                          * streams being reset and those not being reset.
2315                          * 
2316                          * We take complete messages that have a stream reset
2317                          * intervening (aka the TSN is after where our
2318                          * cum-ack needs to be) off and put them on a
2319                          * pending_reply_queue. The reassembly ones we do
2320                          * not have to worry about since they are all sorted
2321                          * and proceessed by TSN order. It is only the
2322                          * singletons I must worry about.
2323                          */
2324                         if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2325                             ((compare_with_wrap(tsn, liste->tsn, MAX_TSN)))
2326                             ) {
2327                                 /*
2328                                  * yep its past where we need to reset... go
2329                                  * ahead and queue it.
2330                                  */
2331                                 if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2332                                         /* first one on */
2333                                         TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2334                                 } else {
2335                                         struct sctp_queued_to_read *ctlOn;
2336                                         unsigned char inserted = 0;
2337
2338                                         ctlOn = TAILQ_FIRST(&asoc->pending_reply_queue);
2339                                         while (ctlOn) {
2340                                                 if (compare_with_wrap(control->sinfo_tsn,
2341                                                     ctlOn->sinfo_tsn, MAX_TSN)) {
2342                                                         ctlOn = TAILQ_NEXT(ctlOn, next);
2343                                                 } else {
2344                                                         /* found it */
2345                                                         TAILQ_INSERT_BEFORE(ctlOn, control, next);
2346                                                         inserted = 1;
2347                                                         break;
2348                                                 }
2349                                         }
2350                                         if (inserted == 0) {
2351                                                 /*
2352                                                  * must be put at end, use
2353                                                  * prevP (all setup from
2354                                                  * loop) to setup nextP.
2355                                                  */
2356                                                 TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2357                                         }
2358                                 }
2359                         } else {
2360                                 sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2361                                 if (*abort_flag) {
2362                                         return (0);
2363                                 }
2364                         }
2365                 }
2366         } else {
2367                 /* Into the re-assembly queue */
2368                 sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2369                 if (*abort_flag) {
2370                         /*
2371                          * the assoc is now gone and chk was put onto the
2372                          * reasm queue, which has all been freed.
2373                          */
2374                         *m = NULL;
2375                         return (0);
2376                 }
2377         }
2378 finish_express_del:
2379         if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
2380                 /* we have a new high score */
2381                 asoc->highest_tsn_inside_map = tsn;
2382                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2383                         sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2384                 }
2385         }
2386         if (tsn == (asoc->cumulative_tsn + 1)) {
2387                 /* Update cum-ack */
2388                 asoc->cumulative_tsn = tsn;
2389         }
2390         if (last_chunk) {
2391                 *m = NULL;
2392         }
2393         if (ordered) {
2394                 SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2395         } else {
2396                 SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2397         }
2398         SCTP_STAT_INCR(sctps_recvdata);
2399         /* Set it present please */
2400         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
2401                 sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
2402         }
2403         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2404                 sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2405                     asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
2406         }
2407         SCTP_TCB_LOCK_ASSERT(stcb);
2408         SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
2409
2410         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) &&
2411             asoc->peer_supports_nr_sack &&
2412             (SCTP_BASE_SYSCTL(sctp_do_drain) == 0)) {
2413                 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
2414                 SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
2415                 if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
2416                         asoc->highest_tsn_inside_nr_map = tsn;
2417                 }
2418         }
2419         /* check the special flag for stream resets */
2420         if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2421             ((compare_with_wrap(asoc->cumulative_tsn, liste->tsn, MAX_TSN)) ||
2422             (asoc->cumulative_tsn == liste->tsn))
2423             ) {
2424                 /*
2425                  * we have finished working through the backlogged TSN's now
2426                  * time to reset streams. 1: call reset function. 2: free
2427                  * pending_reply space 3: distribute any chunks in
2428                  * pending_reply_queue.
2429                  */
2430                 struct sctp_queued_to_read *ctl;
2431
2432                 sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
2433                 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2434                 SCTP_FREE(liste, SCTP_M_STRESET);
2435                 /* sa_ignore FREED_MEMORY */
2436                 liste = TAILQ_FIRST(&asoc->resetHead);
2437                 ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2438                 if (ctl && (liste == NULL)) {
2439                         /* All can be removed */
2440                         while (ctl) {
2441                                 TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2442                                 sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2443                                 if (*abort_flag) {
2444                                         return (0);
2445                                 }
2446                                 ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2447                         }
2448                 } else if (ctl) {
2449                         /* more than one in queue */
2450                         while (!compare_with_wrap(ctl->sinfo_tsn, liste->tsn, MAX_TSN)) {
2451                                 /*
2452                                  * if ctl->sinfo_tsn is <= liste->tsn we can
2453                                  * process it which is the NOT of
2454                                  * ctl->sinfo_tsn > liste->tsn
2455                                  */
2456                                 TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2457                                 sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2458                                 if (*abort_flag) {
2459                                         return (0);
2460                                 }
2461                                 ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2462                         }
2463                 }
2464                 /*
2465                  * Now service re-assembly to pick up anything that has been
2466                  * held on reassembly queue?
2467                  */
2468                 sctp_deliver_reasm_check(stcb, asoc);
2469                 need_reasm_check = 0;
2470         }
2471         if (need_reasm_check) {
2472                 /* Another one waits ? */
2473                 sctp_deliver_reasm_check(stcb, asoc);
2474         }
2475         return (1);
2476 }
2477
2478 int8_t sctp_map_lookup_tab[256] = {
2479         -1, 0, -1, 1, -1, 0, -1, 2,
2480         -1, 0, -1, 1, -1, 0, -1, 3,
2481         -1, 0, -1, 1, -1, 0, -1, 2,
2482         -1, 0, -1, 1, -1, 0, -1, 4,
2483         -1, 0, -1, 1, -1, 0, -1, 2,
2484         -1, 0, -1, 1, -1, 0, -1, 3,
2485         -1, 0, -1, 1, -1, 0, -1, 2,
2486         -1, 0, -1, 1, -1, 0, -1, 5,
2487         -1, 0, -1, 1, -1, 0, -1, 2,
2488         -1, 0, -1, 1, -1, 0, -1, 3,
2489         -1, 0, -1, 1, -1, 0, -1, 2,
2490         -1, 0, -1, 1, -1, 0, -1, 4,
2491         -1, 0, -1, 1, -1, 0, -1, 2,
2492         -1, 0, -1, 1, -1, 0, -1, 3,
2493         -1, 0, -1, 1, -1, 0, -1, 2,
2494         -1, 0, -1, 1, -1, 0, -1, 6,
2495         -1, 0, -1, 1, -1, 0, -1, 2,
2496         -1, 0, -1, 1, -1, 0, -1, 3,
2497         -1, 0, -1, 1, -1, 0, -1, 2,
2498         -1, 0, -1, 1, -1, 0, -1, 4,
2499         -1, 0, -1, 1, -1, 0, -1, 2,
2500         -1, 0, -1, 1, -1, 0, -1, 3,
2501         -1, 0, -1, 1, -1, 0, -1, 2,
2502         -1, 0, -1, 1, -1, 0, -1, 5,
2503         -1, 0, -1, 1, -1, 0, -1, 2,
2504         -1, 0, -1, 1, -1, 0, -1, 3,
2505         -1, 0, -1, 1, -1, 0, -1, 2,
2506         -1, 0, -1, 1, -1, 0, -1, 4,
2507         -1, 0, -1, 1, -1, 0, -1, 2,
2508         -1, 0, -1, 1, -1, 0, -1, 3,
2509         -1, 0, -1, 1, -1, 0, -1, 2,
2510         -1, 0, -1, 1, -1, 0, -1, 7,
2511 };
2512
2513
2514 void
2515 sctp_sack_check(struct sctp_tcb *stcb, int ok_to_sack, int was_a_gap, int *abort_flag)
2516 {
2517         /*
2518          * Now we also need to check the mapping array in a couple of ways.
2519          * 1) Did we move the cum-ack point?
2520          */
2521         struct sctp_association *asoc;
2522         int at;
2523         int last_all_ones = 0;
2524         int slide_from, slide_end, lgap, distance;
2525
2526         /* EY nr_mapping array variables */
2527         /* int nr_at; */
2528         /* int nr_last_all_ones = 0; */
2529         /* int nr_slide_from, nr_slide_end, nr_lgap, nr_distance; */
2530
2531         uint32_t old_cumack, old_base, old_highest;
2532         unsigned char aux_array[64];
2533
2534         /*
2535          * EY! Don't think this is required but I am immitating the code for
2536          * map just to make sure
2537          */
2538         unsigned char nr_aux_array[64];
2539
2540         asoc = &stcb->asoc;
2541         at = 0;
2542
2543         old_cumack = asoc->cumulative_tsn;
2544         old_base = asoc->mapping_array_base_tsn;
2545         old_highest = asoc->highest_tsn_inside_map;
2546         if (asoc->mapping_array_size < 64)
2547                 memcpy(aux_array, asoc->mapping_array,
2548                     asoc->mapping_array_size);
2549         else
2550                 memcpy(aux_array, asoc->mapping_array, 64);
2551         /* EY do the same for nr_mapping_array */
2552         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2553
2554                 if (asoc->nr_mapping_array_size != asoc->mapping_array_size) {
2555                         /*
2556                          * printf("\nEY-IN sack_check method: \nEY-" "The
2557                          * size of map and nr_map are inconsitent")
2558                          */ ;
2559                 }
2560                 if (asoc->nr_mapping_array_base_tsn != asoc->mapping_array_base_tsn) {
2561                         /*
2562                          * printf("\nEY-IN sack_check method VERY CRUCIAL
2563                          * error: \nEY-" "The base tsns of map and nr_map
2564                          * are inconsitent")
2565                          */ ;
2566                 }
2567                 /* EY! just immitating the above code */
2568                 if (asoc->nr_mapping_array_size < 64)
2569                         memcpy(nr_aux_array, asoc->nr_mapping_array,
2570                             asoc->nr_mapping_array_size);
2571                 else
2572                         memcpy(aux_array, asoc->nr_mapping_array, 64);
2573         }
2574         /*
2575          * We could probably improve this a small bit by calculating the
2576          * offset of the current cum-ack as the starting point.
2577          */
2578         at = 0;
2579         for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
2580
2581                 if (asoc->mapping_array[slide_from] == 0xff) {
2582                         at += 8;
2583                         last_all_ones = 1;
2584                 } else {
2585                         /* there is a 0 bit */
2586                         at += sctp_map_lookup_tab[asoc->mapping_array[slide_from]];
2587                         last_all_ones = 0;
2588                         break;
2589                 }
2590         }
2591         asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - last_all_ones);
2592         /* at is one off, since in the table a embedded -1 is present */
2593         at++;
2594
2595         if (compare_with_wrap(asoc->cumulative_tsn,
2596             asoc->highest_tsn_inside_map,
2597             MAX_TSN)) {
2598 #ifdef INVARIANTS
2599                 panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2600                     asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2601 #else
2602                 SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2603                     asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2604                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2605                         sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2606                 }
2607                 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2608                 asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2609 #endif
2610         }
2611         if ((asoc->cumulative_tsn == asoc->highest_tsn_inside_map) && (at >= 8)) {
2612                 /* The complete array was completed by a single FR */
2613                 /* higest becomes the cum-ack */
2614                 int clr;
2615
2616                 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
2617                 /* clear the array */
2618                 clr = (at >> 3) + 1;
2619                 if (clr > asoc->mapping_array_size) {
2620                         clr = asoc->mapping_array_size;
2621                 }
2622                 memset(asoc->mapping_array, 0, clr);
2623                 /* base becomes one ahead of the cum-ack */
2624                 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2625
2626                 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2627
2628                         if (clr > asoc->nr_mapping_array_size)
2629                                 clr = asoc->nr_mapping_array_size;
2630
2631                         memset(asoc->nr_mapping_array, 0, clr);
2632                         /* base becomes one ahead of the cum-ack */
2633                         asoc->nr_mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2634                         asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2635                 }
2636                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2637                         sctp_log_map(old_base, old_cumack, old_highest,
2638                             SCTP_MAP_PREPARE_SLIDE);
2639                         sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2640                             asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_CLEARED);
2641                 }
2642         } else if (at >= 8) {
2643                 /* we can slide the mapping array down */
2644                 /* slide_from holds where we hit the first NON 0xff byte */
2645
2646                 /*
2647                  * now calculate the ceiling of the move using our highest
2648                  * TSN value
2649                  */
2650                 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
2651                         lgap = asoc->highest_tsn_inside_map -
2652                             asoc->mapping_array_base_tsn;
2653                 } else {
2654                         lgap = (MAX_TSN - asoc->mapping_array_base_tsn) +
2655                             asoc->highest_tsn_inside_map + 1;
2656                 }
2657                 slide_end = lgap >> 3;
2658                 if (slide_end < slide_from) {
2659 #ifdef INVARIANTS
2660                         panic("impossible slide");
2661 #else
2662                         printf("impossible slide?\n");
2663                         return;
2664 #endif
2665                 }
2666                 if (slide_end > asoc->mapping_array_size) {
2667 #ifdef INVARIANTS
2668                         panic("would overrun buffer");
2669 #else
2670                         printf("Gak, would have overrun map end:%d slide_end:%d\n",
2671                             asoc->mapping_array_size, slide_end);
2672                         slide_end = asoc->mapping_array_size;
2673 #endif
2674                 }
2675                 distance = (slide_end - slide_from) + 1;
2676                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2677                         sctp_log_map(old_base, old_cumack, old_highest,
2678                             SCTP_MAP_PREPARE_SLIDE);
2679                         sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2680                             (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
2681                 }
2682                 if (distance + slide_from > asoc->mapping_array_size ||
2683                     distance < 0) {
2684                         /*
2685                          * Here we do NOT slide forward the array so that
2686                          * hopefully when more data comes in to fill it up
2687                          * we will be able to slide it forward. Really I
2688                          * don't think this should happen :-0
2689                          */
2690
2691                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2692                                 sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2693                                     (uint32_t) asoc->mapping_array_size,
2694                                     SCTP_MAP_SLIDE_NONE);
2695                         }
2696                 } else {
2697                         int ii;
2698
2699                         for (ii = 0; ii < distance; ii++) {
2700                                 asoc->mapping_array[ii] =
2701                                     asoc->mapping_array[slide_from + ii];
2702                         }
2703                         for (ii = distance; ii <= slide_end; ii++) {
2704                                 asoc->mapping_array[ii] = 0;
2705                         }
2706                         asoc->mapping_array_base_tsn += (slide_from << 3);
2707                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2708                                 sctp_log_map(asoc->mapping_array_base_tsn,
2709                                     asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2710                                     SCTP_MAP_SLIDE_RESULT);
2711                         }
2712                         /*
2713                          * EY if doing nr_sacks then slide the
2714                          * nr_mapping_array accordingly please
2715                          */
2716                         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2717                                 for (ii = 0; ii < distance; ii++) {
2718                                         asoc->nr_mapping_array[ii] =
2719                                             asoc->nr_mapping_array[slide_from + ii];
2720                                 }
2721                                 for (ii = distance; ii <= slide_end; ii++) {
2722                                         asoc->nr_mapping_array[ii] = 0;
2723                                 }
2724                                 asoc->nr_mapping_array_base_tsn += (slide_from << 3);
2725                         }
2726                 }
2727         }
2728         /*
2729          * Now we need to see if we need to queue a sack or just start the
2730          * timer (if allowed).
2731          */
2732         if (ok_to_sack) {
2733                 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2734                         /*
2735                          * Ok special case, in SHUTDOWN-SENT case. here we
2736                          * maker sure SACK timer is off and instead send a
2737                          * SHUTDOWN and a SACK
2738                          */
2739                         if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2740                                 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2741                                     stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2742                         }
2743                         sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
2744                         /*
2745                          * EY if nr_sacks used then send an nr-sack , a sack
2746                          * otherwise
2747                          */
2748                         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
2749                                 sctp_send_nr_sack(stcb);
2750                         else
2751                                 sctp_send_sack(stcb);
2752                 } else {
2753                         int is_a_gap;
2754
2755                         /* is there a gap now ? */
2756                         is_a_gap = compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2757                             stcb->asoc.cumulative_tsn, MAX_TSN);
2758
2759                         /*
2760                          * CMT DAC algorithm: increase number of packets
2761                          * received since last ack
2762                          */
2763                         stcb->asoc.cmt_dac_pkts_rcvd++;
2764
2765                         if ((stcb->asoc.send_sack == 1) ||      /* We need to send a
2766                                                                  * SACK */
2767                             ((was_a_gap) && (is_a_gap == 0)) || /* was a gap, but no
2768                                                                  * longer is one */
2769                             (stcb->asoc.numduptsns) ||  /* we have dup's */
2770                             (is_a_gap) ||       /* is still a gap */
2771                             (stcb->asoc.delayed_ack == 0) ||    /* Delayed sack disabled */
2772                             (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) /* hit limit of pkts */
2773                             ) {
2774
2775                                 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off)) &&
2776                                     (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
2777                                     (stcb->asoc.send_sack == 0) &&
2778                                     (stcb->asoc.numduptsns == 0) &&
2779                                     (stcb->asoc.delayed_ack) &&
2780                                     (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2781
2782                                         /*
2783                                          * CMT DAC algorithm: With CMT,
2784                                          * delay acks even in the face of
2785                                          * 
2786                                          * reordering. Therefore, if acks that
2787                                          * do not have to be sent because of
2788                                          * the above reasons, will be
2789                                          * delayed. That is, acks that would
2790                                          * have been sent due to gap reports
2791                                          * will be delayed with DAC. Start
2792                                          * the delayed ack timer.
2793                                          */
2794                                         sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2795                                             stcb->sctp_ep, stcb, NULL);
2796                                 } else {
2797                                         /*
2798                                          * Ok we must build a SACK since the
2799                                          * timer is pending, we got our
2800                                          * first packet OR there are gaps or
2801                                          * duplicates.
2802                                          */
2803                                         (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2804                                         /*
2805                                          * EY if nr_sacks used then send an
2806                                          * nr-sack , a sack otherwise
2807                                          */
2808                                         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
2809                                                 sctp_send_nr_sack(stcb);
2810                                         else
2811                                                 sctp_send_sack(stcb);
2812                                 }
2813                         } else {
2814                                 if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2815                                         sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2816                                             stcb->sctp_ep, stcb, NULL);
2817                                 }
2818                         }
2819                 }
2820         }
2821 }
2822
2823 void
2824 sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2825 {
2826         struct sctp_tmit_chunk *chk;
2827         uint32_t tsize;
2828         uint16_t nxt_todel;
2829
2830         if (asoc->fragmented_delivery_inprogress) {
2831                 sctp_service_reassembly(stcb, asoc);
2832         }
2833         /* Can we proceed further, i.e. the PD-API is complete */
2834         if (asoc->fragmented_delivery_inprogress) {
2835                 /* no */
2836                 return;
2837         }
2838         /*
2839          * Now is there some other chunk I can deliver from the reassembly
2840          * queue.
2841          */
2842 doit_again:
2843         chk = TAILQ_FIRST(&asoc->reasmqueue);
2844         if (chk == NULL) {
2845                 asoc->size_on_reasm_queue = 0;
2846                 asoc->cnt_on_reasm_queue = 0;
2847                 return;
2848         }
2849         nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2850         if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2851             ((nxt_todel == chk->rec.data.stream_seq) ||
2852             (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2853                 /*
2854                  * Yep the first one is here. We setup to start reception,
2855                  * by backing down the TSN just in case we can't deliver.
2856                  */
2857
2858                 /*
2859                  * Before we start though either all of the message should
2860                  * be here or 1/4 the socket buffer max or nothing on the
2861                  * delivery queue and something can be delivered.
2862                  */
2863                 if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
2864                     (tsize >= stcb->sctp_ep->partial_delivery_point))) {
2865                         asoc->fragmented_delivery_inprogress = 1;
2866                         asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2867                         asoc->str_of_pdapi = chk->rec.data.stream_number;
2868                         asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2869                         asoc->pdapi_ppid = chk->rec.data.payloadtype;
2870                         asoc->fragment_flags = chk->rec.data.rcv_flags;
2871                         sctp_service_reassembly(stcb, asoc);
2872                         if (asoc->fragmented_delivery_inprogress == 0) {
2873                                 goto doit_again;
2874                         }
2875                 }
2876         }
2877 }
2878
2879 int
2880 sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2881     struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2882     struct sctp_nets *net, uint32_t * high_tsn)
2883 {
2884         struct sctp_data_chunk *ch, chunk_buf;
2885         struct sctp_association *asoc;
2886         int num_chunks = 0;     /* number of control chunks processed */
2887         int stop_proc = 0;
2888         int chk_length, break_flag, last_chunk;
2889         int abort_flag = 0, was_a_gap = 0;
2890         struct mbuf *m;
2891
2892         /* set the rwnd */
2893         sctp_set_rwnd(stcb, &stcb->asoc);
2894
2895         m = *mm;
2896         SCTP_TCB_LOCK_ASSERT(stcb);
2897         asoc = &stcb->asoc;
2898         if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2899             stcb->asoc.cumulative_tsn, MAX_TSN)) {
2900                 /* there was a gap before this data was processed */
2901                 was_a_gap = 1;
2902         }
2903         /*
2904          * setup where we got the last DATA packet from for any SACK that
2905          * may need to go out. Don't bump the net. This is done ONLY when a
2906          * chunk is assigned.
2907          */
2908         asoc->last_data_chunk_from = net;
2909
2910         /*-
2911          * Now before we proceed we must figure out if this is a wasted
2912          * cluster... i.e. it is a small packet sent in and yet the driver
2913          * underneath allocated a full cluster for it. If so we must copy it
2914          * to a smaller mbuf and free up the cluster mbuf. This will help
2915          * with cluster starvation. Note for __Panda__ we don't do this
2916          * since it has clusters all the way down to 64 bytes.
2917          */
2918         if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2919                 /* we only handle mbufs that are singletons.. not chains */
2920                 m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2921                 if (m) {
2922                         /* ok lets see if we can copy the data up */
2923                         caddr_t *from, *to;
2924
2925                         /* get the pointers and copy */
2926                         to = mtod(m, caddr_t *);
2927                         from = mtod((*mm), caddr_t *);
2928                         memcpy(to, from, SCTP_BUF_LEN((*mm)));
2929                         /* copy the length and free up the old */
2930                         SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2931                         sctp_m_freem(*mm);
2932                         /* sucess, back copy */
2933                         *mm = m;
2934                 } else {
2935                         /* We are in trouble in the mbuf world .. yikes */
2936                         m = *mm;
2937                 }
2938         }
2939         /* get pointer to the first chunk header */
2940         ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2941             sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2942         if (ch == NULL) {
2943                 return (1);
2944         }
2945         /*
2946          * process all DATA chunks...
2947          */
2948         *high_tsn = asoc->cumulative_tsn;
2949         break_flag = 0;
2950         asoc->data_pkts_seen++;
2951         while (stop_proc == 0) {
2952                 /* validate chunk length */
2953                 chk_length = ntohs(ch->ch.chunk_length);
2954                 if (length - *offset < chk_length) {
2955                         /* all done, mutulated chunk */
2956                         stop_proc = 1;
2957                         break;
2958                 }
2959                 if (ch->ch.chunk_type == SCTP_DATA) {
2960                         if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2961                                 /*
2962                                  * Need to send an abort since we had a
2963                                  * invalid data chunk.
2964                                  */
2965                                 struct mbuf *op_err;
2966
2967                                 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2968                                     0, M_DONTWAIT, 1, MT_DATA);
2969
2970                                 if (op_err) {
2971                                         struct sctp_paramhdr *ph;
2972                                         uint32_t *ippp;
2973
2974                                         SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2975                                             (2 * sizeof(uint32_t));
2976                                         ph = mtod(op_err, struct sctp_paramhdr *);
2977                                         ph->param_type =
2978                                             htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2979                                         ph->param_length = htons(SCTP_BUF_LEN(op_err));
2980                                         ippp = (uint32_t *) (ph + 1);
2981                                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2982                                         ippp++;
2983                                         *ippp = asoc->cumulative_tsn;
2984
2985                                 }
2986                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2987                                 sctp_abort_association(inp, stcb, m, iphlen, sh,
2988                                     op_err, 0, net->port);
2989                                 return (2);
2990                         }
2991 #ifdef SCTP_AUDITING_ENABLED
2992                         sctp_audit_log(0xB1, 0);
2993 #endif
2994                         if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2995                                 last_chunk = 1;
2996                         } else {
2997                                 last_chunk = 0;
2998                         }
2999                         if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
3000                             chk_length, net, high_tsn, &abort_flag, &break_flag,
3001                             last_chunk)) {
3002                                 num_chunks++;
3003                         }
3004                         if (abort_flag)
3005                                 return (2);
3006
3007                         if (break_flag) {
3008                                 /*
3009                                  * Set because of out of rwnd space and no
3010                                  * drop rep space left.
3011                                  */
3012                                 stop_proc = 1;
3013                                 break;
3014                         }
3015                 } else {
3016                         /* not a data chunk in the data region */
3017                         switch (ch->ch.chunk_type) {
3018                         case SCTP_INITIATION:
3019                         case SCTP_INITIATION_ACK:
3020                         case SCTP_SELECTIVE_ACK:
3021                         case SCTP_NR_SELECTIVE_ACK:     /* EY */
3022                         case SCTP_HEARTBEAT_REQUEST:
3023                         case SCTP_HEARTBEAT_ACK:
3024                         case SCTP_ABORT_ASSOCIATION:
3025                         case SCTP_SHUTDOWN:
3026                         case SCTP_SHUTDOWN_ACK:
3027                         case SCTP_OPERATION_ERROR:
3028                         case SCTP_COOKIE_ECHO:
3029                         case SCTP_COOKIE_ACK:
3030                         case SCTP_ECN_ECHO:
3031                         case SCTP_ECN_CWR:
3032                         case SCTP_SHUTDOWN_COMPLETE:
3033                         case SCTP_AUTHENTICATION:
3034                         case SCTP_ASCONF_ACK:
3035                         case SCTP_PACKET_DROPPED:
3036                         case SCTP_STREAM_RESET:
3037                         case SCTP_FORWARD_CUM_TSN:
3038                         case SCTP_ASCONF:
3039                                 /*
3040                                  * Now, what do we do with KNOWN chunks that
3041                                  * are NOT in the right place?
3042                                  * 
3043                                  * For now, I do nothing but ignore them. We
3044                                  * may later want to add sysctl stuff to
3045                                  * switch out and do either an ABORT() or
3046                                  * possibly process them.
3047                                  */
3048                                 if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) {
3049                                         struct mbuf *op_err;
3050
3051                                         op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
3052                                         sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0, net->port);
3053                                         return (2);
3054                                 }
3055                                 break;
3056                         default:
3057                                 /* unknown chunk type, use bit rules */
3058                                 if (ch->ch.chunk_type & 0x40) {
3059                                         /* Add a error report to the queue */
3060                                         struct mbuf *merr;
3061                                         struct sctp_paramhdr *phd;
3062
3063                                         merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
3064                                         if (merr) {
3065                                                 phd = mtod(merr, struct sctp_paramhdr *);
3066                                                 /*
3067                                                  * We cheat and use param
3068                                                  * type since we did not
3069                                                  * bother to define a error
3070                                                  * cause struct. They are
3071                                                  * the same basic format
3072                                                  * with different names.
3073                                                  */
3074                                                 phd->param_type =
3075                                                     htons(SCTP_CAUSE_UNRECOG_CHUNK);
3076                                                 phd->param_length =
3077                                                     htons(chk_length + sizeof(*phd));
3078                                                 SCTP_BUF_LEN(merr) = sizeof(*phd);
3079                                                 SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset,
3080                                                     SCTP_SIZE32(chk_length),
3081                                                     M_DONTWAIT);
3082                                                 if (SCTP_BUF_NEXT(merr)) {
3083                                                         sctp_queue_op_err(stcb, merr);
3084                                                 } else {
3085                                                         sctp_m_freem(merr);
3086                                                 }
3087                                         }
3088                                 }
3089                                 if ((ch->ch.chunk_type & 0x80) == 0) {
3090                                         /* discard the rest of this packet */
3091                                         stop_proc = 1;
3092                                 }       /* else skip this bad chunk and
3093                                          * continue... */
3094                                 break;
3095                         };      /* switch of chunk type */
3096                 }
3097                 *offset += SCTP_SIZE32(chk_length);
3098                 if ((*offset >= length) || stop_proc) {
3099                         /* no more data left in the mbuf chain */
3100                         stop_proc = 1;
3101                         continue;
3102                 }
3103                 ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
3104                     sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
3105                 if (ch == NULL) {
3106                         *offset = length;
3107                         stop_proc = 1;
3108                         break;
3109
3110                 }
3111         }                       /* while */
3112         if (break_flag) {
3113                 /*
3114                  * we need to report rwnd overrun drops.
3115                  */
3116                 sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
3117         }
3118         if (num_chunks) {
3119                 /*
3120                  * Did we get data, if so update the time for auto-close and
3121                  * give peer credit for being alive.
3122                  */
3123                 SCTP_STAT_INCR(sctps_recvpktwithdata);
3124                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3125                         sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3126                             stcb->asoc.overall_error_count,
3127                             0,
3128                             SCTP_FROM_SCTP_INDATA,
3129                             __LINE__);
3130                 }
3131                 stcb->asoc.overall_error_count = 0;
3132                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
3133         }
3134         /* now service all of the reassm queue if needed */
3135         if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
3136                 sctp_service_queues(stcb, asoc);
3137
3138         if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
3139                 /* Assure that we ack right away */
3140                 stcb->asoc.send_sack = 1;
3141         }
3142         /* Start a sack timer or QUEUE a SACK for sending */
3143         if ((stcb->asoc.cumulative_tsn == stcb->asoc.highest_tsn_inside_map) &&
3144             (stcb->asoc.mapping_array[0] != 0xff)) {
3145                 if ((stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) ||
3146                     (stcb->asoc.delayed_ack == 0) ||
3147                     (stcb->asoc.numduptsns) ||
3148                     (stcb->asoc.send_sack == 1)) {
3149                         if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
3150                                 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
3151                         }
3152                         /*
3153                          * EY if nr_sacks used then send an nr-sack , a sack
3154                          * otherwise
3155                          */
3156                         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
3157                                 sctp_send_nr_sack(stcb);
3158                         else
3159                                 sctp_send_sack(stcb);
3160                 } else {
3161                         if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
3162                                 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
3163                                     stcb->sctp_ep, stcb, NULL);
3164                         }
3165                 }
3166         } else {
3167                 sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
3168         }
3169         if (abort_flag)
3170                 return (2);
3171
3172         return (0);
3173 }
3174
3175 static void
3176 sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
3177     struct sctp_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
3178     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
3179     int num_seg, int *ecn_seg_sums)
3180 {
3181         /************************************************/
3182         /* process fragments and update sendqueue        */
3183         /************************************************/
3184         struct sctp_sack *sack;
3185         struct sctp_gap_ack_block *frag, block;
3186         struct sctp_tmit_chunk *tp1;
3187         int i, j;
3188         unsigned int theTSN;
3189         int num_frs = 0;
3190
3191         uint16_t frag_strt, frag_end, primary_flag_set;
3192         u_long last_frag_high;
3193
3194         /*
3195          * @@@ JRI : TODO: This flag is not used anywhere .. remove?
3196          */
3197         if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3198                 primary_flag_set = 1;
3199         } else {
3200                 primary_flag_set = 0;
3201         }
3202         sack = &ch->sack;
3203
3204         frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3205             sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3206         *offset += sizeof(block);
3207         if (frag == NULL) {
3208                 return;
3209         }
3210         tp1 = NULL;
3211         last_frag_high = 0;
3212         for (i = 0; i < num_seg; i++) {
3213                 frag_strt = ntohs(frag->start);
3214                 frag_end = ntohs(frag->end);
3215                 /* some sanity checks on the fragment offsets */
3216                 if (frag_strt > frag_end) {
3217                         /* this one is malformed, skip */
3218                         frag++;
3219                         continue;
3220                 }
3221                 if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
3222                     MAX_TSN))
3223                         *biggest_tsn_acked = frag_end + last_tsn;
3224
3225                 /* mark acked dgs and find out the highestTSN being acked */
3226                 if (tp1 == NULL) {
3227                         tp1 = TAILQ_FIRST(&asoc->sent_queue);
3228
3229                         /* save the locations of the last frags */
3230                         last_frag_high = frag_end + last_tsn;
3231                 } else {
3232                         /*
3233                          * now lets see if we need to reset the queue due to
3234                          * a out-of-order SACK fragment
3235                          */
3236                         if (compare_with_wrap(frag_strt + last_tsn,
3237                             last_frag_high, MAX_TSN)) {
3238                                 /*
3239                                  * if the new frag starts after the last TSN
3240                                  * frag covered, we are ok and this one is
3241                                  * beyond the last one
3242                                  */
3243                                 ;
3244                         } else {
3245                                 /*
3246                                  * ok, they have reset us, so we need to
3247                                  * reset the queue this will cause extra
3248                                  * hunting but hey, they chose the
3249                                  * performance hit when they failed to order
3250                                  * their gaps
3251                                  */
3252                                 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3253                         }
3254                         last_frag_high = frag_end + last_tsn;
3255                 }
3256                 for (j = frag_strt; j <= frag_end; j++) {
3257                         theTSN = j + last_tsn;
3258                         while (tp1) {
3259                                 if (tp1->rec.data.doing_fast_retransmit)
3260                                         num_frs++;
3261
3262                                 /*
3263                                  * CMT: CUCv2 algorithm. For each TSN being
3264                                  * processed from the sent queue, track the
3265                                  * next expected pseudo-cumack, or
3266                                  * rtx_pseudo_cumack, if required. Separate
3267                                  * cumack trackers for first transmissions,
3268                                  * and retransmissions.
3269                                  */
3270                                 if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
3271                                     (tp1->snd_count == 1)) {
3272                                         tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
3273                                         tp1->whoTo->find_pseudo_cumack = 0;
3274                                 }
3275                                 if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
3276                                     (tp1->snd_count > 1)) {
3277                                         tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
3278                                         tp1->whoTo->find_rtx_pseudo_cumack = 0;
3279                                 }
3280                                 if (tp1->rec.data.TSN_seq == theTSN) {
3281                                         if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
3282                                                 /*
3283                                                  * must be held until
3284                                                  * cum-ack passes
3285                                                  */
3286                                                 /*
3287                                                  * ECN Nonce: Add the nonce
3288                                                  * value to the sender's
3289                                                  * nonce sum
3290                                                  */
3291                                                 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3292                                                         /*-
3293                                                          * If it is less than RESEND, it is
3294                                                          * now no-longer in flight.
3295                                                          * Higher values may already be set
3296                                                          * via previous Gap Ack Blocks...
3297                                                          * i.e. ACKED or RESEND.
3298                                                          */
3299                                                         if (compare_with_wrap(tp1->rec.data.TSN_seq,
3300                                                             *biggest_newly_acked_tsn, MAX_TSN)) {
3301                                                                 *biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
3302                                                         }
3303                                                         /*
3304                                                          * CMT: SFR algo
3305                                                          * (and HTNA) - set
3306                                                          * saw_newack to 1
3307                                                          * for dest being
3308                                                          * newly acked.
3309                                                          * update
3310                                                          * this_sack_highest_
3311                                                          * newack if
3312                                                          * appropriate.
3313                                                          */
3314                                                         if (tp1->rec.data.chunk_was_revoked == 0)
3315                                                                 tp1->whoTo->saw_newack = 1;
3316
3317                                                         if (compare_with_wrap(tp1->rec.data.TSN_seq,
3318                                                             tp1->whoTo->this_sack_highest_newack,
3319                                                             MAX_TSN)) {
3320                                                                 tp1->whoTo->this_sack_highest_newack =
3321                                                                     tp1->rec.data.TSN_seq;
3322                                                         }
3323                                                         /*
3324                                                          * CMT DAC algo:
3325                                                          * also update
3326                                                          * this_sack_lowest_n
3327                                                          * ewack
3328                                                          */
3329                                                         if (*this_sack_lowest_newack == 0) {
3330                                                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3331                                                                         sctp_log_sack(*this_sack_lowest_newack,
3332                                                                             last_tsn,
3333                                                                             tp1->rec.data.TSN_seq,
3334                                                                             0,
3335                                                                             0,
3336                                                                             SCTP_LOG_TSN_ACKED);
3337                                                                 }
3338                                                                 *this_sack_lowest_newack = tp1->rec.data.TSN_seq;
3339                                                         }
3340                                                         /*
3341                                                          * CMT: CUCv2
3342                                                          * algorithm. If
3343                                                          * (rtx-)pseudo-cumac
3344                                                          * k for corresp
3345                                                          * dest is being
3346                                                          * acked, then we
3347                                                          * have a new
3348                                                          * (rtx-)pseudo-cumac
3349                                                          * k. Set
3350                                                          * new_(rtx_)pseudo_c
3351                                                          * umack to TRUE so
3352                                                          * that the cwnd for
3353                                                          * this dest can be
3354                                                          * updated. Also
3355                                                          * trigger search
3356                                                          * for the next
3357                                                          * expected
3358                                                          * (rtx-)pseudo-cumac
3359                                                          * k. Separate
3360                                                          * pseudo_cumack
3361                                                          * trackers for
3362                                                          * first
3363                                                          * transmissions and
3364                                                          * retransmissions.
3365                                                          */
3366                                                         if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
3367                                                                 if (tp1->rec.data.chunk_was_revoked == 0) {
3368                                                                         tp1->whoTo->new_pseudo_cumack = 1;
3369                                                                 }
3370                                                                 tp1->whoTo->find_pseudo_cumack = 1;
3371                                                         }
3372                                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
3373                                                                 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
3374                                                         }
3375                                                         if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
3376                                                                 if (tp1->rec.data.chunk_was_revoked == 0) {
3377                                                                         tp1->whoTo->new_pseudo_cumack = 1;
3378                                                                 }
3379                                                                 tp1->whoTo->find_rtx_pseudo_cumack = 1;
3380                                                         }
3381                                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3382                                                                 sctp_log_sack(*biggest_newly_acked_tsn,
3383                                                                     last_tsn,
3384                                                                     tp1->rec.data.TSN_seq,
3385                                                                     frag_strt,
3386                                                                     frag_end,
3387                                                                     SCTP_LOG_TSN_ACKED);
3388                                                         }
3389                                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3390                                                                 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
3391                                                                     tp1->whoTo->flight_size,
3392                                                                     tp1->book_size,
3393                                                                     (uintptr_t) tp1->whoTo,
3394                                                                     tp1->rec.data.TSN_seq);
3395                                                         }
3396                                                         sctp_flight_size_decrease(tp1);
3397                                                         sctp_total_flight_decrease(stcb, tp1);
3398
3399                                                         tp1->whoTo->net_ack += tp1->send_size;
3400                                                         if (tp1->snd_count < 2) {
3401                                                                 /*
3402                                                                  * True
3403                                                                  * non-retran
3404                                                                  * smited
3405                                                                  * chunk */
3406                                                                 tp1->whoTo->net_ack2 += tp1->send_size;
3407
3408                                                                 /*
3409                                                                  * update RTO
3410                                                                  * too ? */
3411                                                                 if (tp1->do_rtt) {
3412                                                                         tp1->whoTo->RTO =
3413                                                                             sctp_calculate_rto(stcb,
3414                                                                             asoc,
3415                                                                             tp1->whoTo,
3416                                                                             &tp1->sent_rcv_time,
3417                                                                             sctp_align_safe_nocopy);
3418                                                                         tp1->do_rtt = 0;
3419                                                                 }
3420                                                         }
3421                                                 }
3422                                                 if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
3423                                                         (*ecn_seg_sums) += tp1->rec.data.ect_nonce;
3424                                                         (*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
3425                                                         if (compare_with_wrap(tp1->rec.data.TSN_seq,
3426                                                             asoc->this_sack_highest_gap,
3427                                                             MAX_TSN)) {
3428                                                                 asoc->this_sack_highest_gap =
3429                                                                     tp1->rec.data.TSN_seq;
3430                                                         }
3431                                                         if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3432                                                                 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3433 #ifdef SCTP_AUDITING_ENABLED
3434                                                                 sctp_audit_log(0xB2,
3435                                                                     (asoc->sent_queue_retran_cnt & 0x000000ff));
3436 #endif
3437                                                         }
3438                                                 }
3439                                                 /*
3440                                                  * All chunks NOT UNSENT
3441                                                  * fall through here and are
3442                                                  * marked (leave PR-SCTP
3443                                                  * ones that are to skip
3444                                                  * alone though)
3445                                                  */
3446                                                 if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
3447                                                         tp1->sent = SCTP_DATAGRAM_MARKED;
3448
3449                                                 if (tp1->rec.data.chunk_was_revoked) {
3450                                                         /* deflate the cwnd */
3451                                                         tp1->whoTo->cwnd -= tp1->book_size;
3452                                                         tp1->rec.data.chunk_was_revoked = 0;
3453                                                 }
3454                                         }
3455                                         break;
3456                                 }       /* if (tp1->TSN_seq == theTSN) */
3457                                 if (compare_with_wrap(tp1->rec.data.TSN_seq, theTSN,
3458                                     MAX_TSN))
3459                                         break;
3460
3461                                 tp1 = TAILQ_NEXT(tp1, sctp_next);
3462                         }       /* end while (tp1) */
3463                 }               /* end for (j = fragStart */
3464                 frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3465                     sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3466                 *offset += sizeof(block);
3467                 if (frag == NULL) {
3468                         break;
3469                 }
3470         }
3471         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3472                 if (num_frs)
3473                         sctp_log_fr(*biggest_tsn_acked,
3474                             *biggest_newly_acked_tsn,
3475                             last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
3476         }
3477 }
3478
3479 static void
3480 sctp_check_for_revoked(struct sctp_tcb *stcb,
3481     struct sctp_association *asoc, uint32_t cumack,
3482     u_long biggest_tsn_acked)
3483 {
3484         struct sctp_tmit_chunk *tp1;
3485         int tot_revoked = 0;
3486
3487         tp1 = TAILQ_FIRST(&asoc->sent_queue);
3488         while (tp1) {
3489                 if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
3490                     MAX_TSN)) {
3491                         /*
3492                          * ok this guy is either ACK or MARKED. If it is
3493                          * ACKED it has been previously acked but not this
3494                          * time i.e. revoked.  If it is MARKED it was ACK'ed
3495                          * again.
3496                          */
3497                         if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3498                             MAX_TSN))
3499                                 break;
3500
3501
3502                         if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3503                                 /* it has been revoked */
3504                                 tp1->sent = SCTP_DATAGRAM_SENT;
3505                                 tp1->rec.data.chunk_was_revoked = 1;
3506                                 /*
3507                                  * We must add this stuff back in to assure
3508                                  * timers and such get started.
3509                                  */
3510                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3511                                         sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3512                                             tp1->whoTo->flight_size,
3513                                             tp1->book_size,
3514                                             (uintptr_t) tp1->whoTo,
3515                                             tp1->rec.data.TSN_seq);
3516                                 }
3517                                 sctp_flight_size_increase(tp1);
3518                                 sctp_total_flight_increase(stcb, tp1);
3519                                 /*
3520                                  * We inflate the cwnd to compensate for our
3521                                  * artificial inflation of the flight_size.
3522                                  */
3523                                 tp1->whoTo->cwnd += tp1->book_size;
3524                                 tot_revoked++;
3525                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3526                                         sctp_log_sack(asoc->last_acked_seq,
3527                                             cumack,
3528                                             tp1->rec.data.TSN_seq,
3529                                             0,
3530                                             0,
3531                                             SCTP_LOG_TSN_REVOKED);
3532                                 }
3533                         } else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3534                                 /* it has been re-acked in this SACK */
3535                                 tp1->sent = SCTP_DATAGRAM_ACKED;
3536                         }
3537                 }
3538                 if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3539                         break;
3540                 tp1 = TAILQ_NEXT(tp1, sctp_next);
3541         }
3542         if (tot_revoked > 0) {
3543                 /*
3544                  * Setup the ecn nonce re-sync point. We do this since once
3545                  * data is revoked we begin to retransmit things, which do
3546                  * NOT have the ECN bits set. This means we are now out of
3547                  * sync and must wait until we get back in sync with the
3548                  * peer to check ECN bits.
3549                  */
3550                 tp1 = TAILQ_FIRST(&asoc->send_queue);
3551                 if (tp1 == NULL) {
3552                         asoc->nonce_resync_tsn = asoc->sending_seq;
3553                 } else {
3554                         asoc->nonce_resync_tsn = tp1->rec.data.TSN_seq;
3555                 }
3556                 asoc->nonce_wait_for_ecne = 0;
3557                 asoc->nonce_sum_check = 0;
3558         }
3559 }
3560
3561
3562 static void
3563 sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
3564     u_long biggest_tsn_acked, u_long biggest_tsn_newly_acked, u_long this_sack_lowest_newack, int accum_moved)
3565 {
3566         struct sctp_tmit_chunk *tp1;
3567         int strike_flag = 0;
3568         struct timeval now;
3569         int tot_retrans = 0;
3570         uint32_t sending_seq;
3571         struct sctp_nets *net;
3572         int num_dests_sacked = 0;
3573
3574         /*
3575          * select the sending_seq, this is either the next thing ready to be
3576          * sent but not transmitted, OR, the next seq we assign.
3577          */
3578         tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3579         if (tp1 == NULL) {
3580                 sending_seq = asoc->sending_seq;
3581         } else {
3582                 sending_seq = tp1->rec.data.TSN_seq;
3583         }
3584
3585         /* CMT DAC algo: finding out if SACK is a mixed SACK */
3586         if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3587                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3588                         if (net->saw_newack)
3589                                 num_dests_sacked++;
3590                 }
3591         }
3592         if (stcb->asoc.peer_supports_prsctp) {
3593                 (void)SCTP_GETTIME_TIMEVAL(&now);
3594         }
3595         tp1 = TAILQ_FIRST(&asoc->sent_queue);
3596         while (tp1) {
3597                 strike_flag = 0;
3598                 if (tp1->no_fr_allowed) {
3599                         /* this one had a timeout or something */
3600                         tp1 = TAILQ_NEXT(tp1, sctp_next);
3601                         continue;
3602                 }
3603                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3604                         if (tp1->sent < SCTP_DATAGRAM_RESEND)
3605                                 sctp_log_fr(biggest_tsn_newly_acked,
3606                                     tp1->rec.data.TSN_seq,
3607                                     tp1->sent,
3608                                     SCTP_FR_LOG_CHECK_STRIKE);
3609                 }
3610                 if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3611                     MAX_TSN) ||
3612                     tp1->sent == SCTP_DATAGRAM_UNSENT) {
3613                         /* done */
3614                         break;
3615                 }
3616                 if (stcb->asoc.peer_supports_prsctp) {
3617                         if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3618                                 /* Is it expired? */
3619                                 if (
3620                                 /*
3621                                  * TODO sctp_constants.h needs alternative
3622                                  * time macros when _KERNEL is undefined.
3623                                  */
3624                                     (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
3625                                     ) {
3626                                         /* Yes so drop it */
3627                                         if (tp1->data != NULL) {
3628                                                 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3629                                                     (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3630                                                     SCTP_SO_NOT_LOCKED);
3631                                         }
3632                                         tp1 = TAILQ_NEXT(tp1, sctp_next);
3633                                         continue;
3634                                 }
3635                         }
3636                 }
3637                 if (compare_with_wrap(tp1->rec.data.TSN_seq,
3638                     asoc->this_sack_highest_gap, MAX_TSN)) {
3639                         /* we are beyond the tsn in the sack  */
3640                         break;
3641                 }
3642                 if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3643                         /* either a RESEND, ACKED, or MARKED */
3644                         /* skip */
3645                         tp1 = TAILQ_NEXT(tp1, sctp_next);
3646                         continue;
3647                 }
3648                 /*
3649                  * CMT : SFR algo (covers part of DAC and HTNA as well)
3650                  */
3651                 if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3652                         /*
3653                          * No new acks were receieved for data sent to this
3654                          * dest. Therefore, according to the SFR algo for
3655                          * CMT, no data sent to this dest can be marked for
3656                          * FR using this SACK.
3657                          */
3658                         tp1 = TAILQ_NEXT(tp1, sctp_next);
3659                         continue;
3660                 } else if (tp1->whoTo && compare_with_wrap(tp1->rec.data.TSN_seq,
3661                     tp1->whoTo->this_sack_highest_newack, MAX_TSN)) {
3662                         /*
3663                          * CMT: New acks were receieved for data sent to
3664                          * this dest. But no new acks were seen for data
3665                          * sent after tp1. Therefore, according to the SFR
3666                          * algo for CMT, tp1 cannot be marked for FR using
3667                          * this SACK. This step covers part of the DAC algo
3668                          * and the HTNA algo as well.
3669                          */
3670                         tp1 = TAILQ_NEXT(tp1, sctp_next);
3671                         continue;
3672                 }
3673                 /*
3674                  * Here we check to see if we were have already done a FR
3675                  * and if so we see if the biggest TSN we saw in the sack is
3676                  * smaller than the recovery point. If so we don't strike
3677                  * the tsn... otherwise we CAN strike the TSN.
3678                  */
3679                 /*
3680                  * @@@ JRI: Check for CMT if (accum_moved &&
3681                  * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
3682                  * 0)) {
3683                  */
3684                 if (accum_moved && asoc->fast_retran_loss_recovery) {
3685                         /*
3686                          * Strike the TSN if in fast-recovery and cum-ack
3687                          * moved.
3688                          */
3689                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3690                                 sctp_log_fr(biggest_tsn_newly_acked,
3691                                     tp1->rec.data.TSN_seq,
3692                                     tp1->sent,
3693                                     SCTP_FR_LOG_STRIKE_CHUNK);
3694                         }
3695                         if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3696                                 tp1->sent++;
3697                         }
3698                         if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3699                                 /*
3700                                  * CMT DAC algorithm: If SACK flag is set to
3701                                  * 0, then lowest_newack test will not pass
3702                                  * because it would have been set to the
3703                                  * cumack earlier. If not already to be
3704                                  * rtx'd, If not a mixed sack and if tp1 is
3705                                  * not between two sacked TSNs, then mark by
3706                                  * one more. NOTE that we are marking by one
3707                                  * additional time since the SACK DAC flag
3708                                  * indicates that two packets have been
3709                                  * received after this missing TSN.
3710                                  */
3711                                 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3712                                     compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3713                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3714                                                 sctp_log_fr(16 + num_dests_sacked,
3715                                                     tp1->rec.data.TSN_seq,
3716                                                     tp1->sent,
3717                                                     SCTP_FR_LOG_STRIKE_CHUNK);
3718                                         }
3719                                         tp1->sent++;
3720                                 }
3721                         }
3722                 } else if ((tp1->rec.data.doing_fast_retransmit) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
3723                         /*
3724                          * For those that have done a FR we must take
3725                          * special consideration if we strike. I.e the
3726                          * biggest_newly_acked must be higher than the
3727                          * sending_seq at the time we did the FR.
3728                          */
3729                         if (
3730 #ifdef SCTP_FR_TO_ALTERNATE
3731                         /*
3732                          * If FR's go to new networks, then we must only do
3733                          * this for singly homed asoc's. However if the FR's
3734                          * go to the same network (Armando's work) then its
3735                          * ok to FR multiple times.
3736                          */
3737                             (asoc->numnets < 2)
3738 #else
3739                             (1)
3740 #endif
3741                             ) {
3742
3743                                 if ((compare_with_wrap(biggest_tsn_newly_acked,
3744                                     tp1->rec.data.fast_retran_tsn, MAX_TSN)) ||
3745                                     (biggest_tsn_newly_acked ==
3746                                     tp1->rec.data.fast_retran_tsn)) {
3747                                         /*
3748                                          * Strike the TSN, since this ack is
3749                                          * beyond where things were when we
3750                                          * did a FR.
3751                                          */
3752                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3753                                                 sctp_log_fr(biggest_tsn_newly_acked,
3754                                                     tp1->rec.data.TSN_seq,
3755                                                     tp1->sent,
3756                                                     SCTP_FR_LOG_STRIKE_CHUNK);
3757                                         }
3758                                         if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3759                                                 tp1->sent++;
3760                                         }
3761                                         strike_flag = 1;
3762                                         if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3763                                                 /*
3764                                                  * CMT DAC algorithm: If
3765                                                  * SACK flag is set to 0,
3766                                                  * then lowest_newack test
3767                                                  * will not pass because it
3768                                                  * would have been set to
3769                                                  * the cumack earlier. If
3770                                                  * not already to be rtx'd,
3771                                                  * If not a mixed sack and
3772                                                  * if tp1 is not between two
3773                                                  * sacked TSNs, then mark by
3774                                                  * one more. NOTE that we
3775                                                  * are marking by one
3776                                                  * additional time since the
3777                                                  * SACK DAC flag indicates
3778                                                  * that two packets have
3779                                                  * been received after this
3780                                                  * missing TSN.
3781                                                  */
3782                                                 if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
3783                                                     (num_dests_sacked == 1) &&
3784                                                     compare_with_wrap(this_sack_lowest_newack,
3785                                                     tp1->rec.data.TSN_seq, MAX_TSN)) {
3786                                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3787                                                                 sctp_log_fr(32 + num_dests_sacked,
3788                                                                     tp1->rec.data.TSN_seq,
3789                                                                     tp1->sent,
3790                                                                     SCTP_FR_LOG_STRIKE_CHUNK);
3791                                                         }
3792                                                         if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3793                                                                 tp1->sent++;
3794                                                         }
3795                                                 }
3796                                         }
3797                                 }
3798                         }
3799                         /*
3800                          * JRI: TODO: remove code for HTNA algo. CMT's SFR
3801                          * algo covers HTNA.
3802                          */
3803                 } else if (compare_with_wrap(tp1->rec.data.TSN_seq,
3804                     biggest_tsn_newly_acked, MAX_TSN)) {
3805                         /*
3806                          * We don't strike these: This is the  HTNA
3807                          * algorithm i.e. we don't strike If our TSN is
3808                          * larger than the Highest TSN Newly Acked.
3809                          */
3810                         ;
3811                 } else {
3812                         /* Strike the TSN */
3813                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3814                                 sctp_log_fr(biggest_tsn_newly_acked,
3815                                     tp1->rec.data.TSN_seq,
3816                                     tp1->sent,
3817                                     SCTP_FR_LOG_STRIKE_CHUNK);
3818                         }
3819                         if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3820                                 tp1->sent++;
3821                         }
3822                         if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3823                                 /*
3824                                  * CMT DAC algorithm: If SACK flag is set to
3825                                  * 0, then lowest_newack test will not pass
3826                                  * because it would have been set to the
3827                                  * cumack earlier. If not already to be
3828                                  * rtx'd, If not a mixed sack and if tp1 is
3829                                  * not between two sacked TSNs, then mark by
3830                                  * one more. NOTE that we are marking by one
3831                                  * additional time since the SACK DAC flag
3832                                  * indicates that two packets have been
3833                                  * received after this missing TSN.
3834                                  */
3835                                 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3836                                     compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3837                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3838                                                 sctp_log_fr(48 + num_dests_sacked,
3839                                                     tp1->rec.data.TSN_seq,
3840                                                     tp1->sent,
3841                                                     SCTP_FR_LOG_STRIKE_CHUNK);
3842                                         }
3843                                         tp1->sent++;
3844                                 }
3845                         }
3846                 }
3847                 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3848                         struct sctp_nets *alt;
3849
3850                         /* fix counts and things */
3851                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3852                                 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3853                                     (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3854                                     tp1->book_size,
3855                                     (uintptr_t) tp1->whoTo,
3856                                     tp1->rec.data.TSN_seq);
3857                         }
3858                         if (tp1->whoTo) {
3859                                 tp1->whoTo->net_ack++;
3860                                 sctp_flight_size_decrease(tp1);
3861                         }
3862                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3863                                 sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3864                                     asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3865                         }
3866                         /* add back to the rwnd */
3867                         asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3868
3869                         /* remove from the total flight */
3870                         sctp_total_flight_decrease(stcb, tp1);
3871
3872                         if ((stcb->asoc.peer_supports_prsctp) &&
3873                             (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3874                                 /*
3875                                  * Has it been retransmitted tv_sec times? -
3876                                  * we store the retran count there.
3877                                  */
3878                                 if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3879                                         /* Yes, so drop it */
3880                                         if (tp1->data != NULL) {
3881                                                 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3882                                                     (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3883                                                     SCTP_SO_NOT_LOCKED);
3884                                         }
3885                                         /* Make sure to flag we had a FR */
3886                                         tp1->whoTo->net_ack++;
3887                                         tp1 = TAILQ_NEXT(tp1, sctp_next);
3888                                         continue;
3889                                 }
3890                         }
3891                         /* printf("OK, we are now ready to FR this guy\n"); */
3892                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3893                                 sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3894                                     0, SCTP_FR_MARKED);
3895                         }
3896                         if (strike_flag) {
3897                                 /* This is a subsequent FR */
3898                                 SCTP_STAT_INCR(sctps_sendmultfastretrans);
3899                         }
3900                         sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3901                         if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
3902                                 /*
3903                                  * CMT: Using RTX_SSTHRESH policy for CMT.
3904                                  * If CMT is being used, then pick dest with
3905                                  * largest ssthresh for any retransmission.
3906                                  */
3907                                 tp1->no_fr_allowed = 1;
3908                                 alt = tp1->whoTo;
3909                                 /* sa_ignore NO_NULL_CHK */
3910                                 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
3911                                         /*
3912                                          * JRS 5/18/07 - If CMT PF is on,
3913                                          * use the PF version of
3914                                          * find_alt_net()
3915                                          */
3916                                         alt = sctp_find_alternate_net(stcb, alt, 2);
3917                                 } else {
3918                                         /*
3919                                          * JRS 5/18/07 - If only CMT is on,
3920                                          * use the CMT version of
3921                                          * find_alt_net()
3922                                          */
3923                                         /* sa_ignore NO_NULL_CHK */
3924                                         alt = sctp_find_alternate_net(stcb, alt, 1);
3925                                 }
3926                                 if (alt == NULL) {
3927                                         alt = tp1->whoTo;
3928                                 }
3929                                 /*
3930                                  * CUCv2: If a different dest is picked for
3931                                  * the retransmission, then new
3932                                  * (rtx-)pseudo_cumack needs to be tracked
3933                                  * for orig dest. Let CUCv2 track new (rtx-)
3934                                  * pseudo-cumack always.
3935                                  */
3936                                 if (tp1->whoTo) {
3937                                         tp1->whoTo->find_pseudo_cumack = 1;
3938                                         tp1->whoTo->find_rtx_pseudo_cumack = 1;
3939                                 }
3940                         } else {/* CMT is OFF */
3941
3942 #ifdef SCTP_FR_TO_ALTERNATE
3943                                 /* Can we find an alternate? */
3944                                 alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3945 #else
3946                                 /*
3947                                  * default behavior is to NOT retransmit
3948                                  * FR's to an alternate. Armando Caro's
3949                                  * paper details why.
3950                                  */
3951                                 alt = tp1->whoTo;
3952 #endif
3953                         }
3954
3955                         tp1->rec.data.doing_fast_retransmit = 1;
3956                         tot_retrans++;
3957                         /* mark the sending seq for possible subsequent FR's */
3958                         /*
3959                          * printf("Marking TSN for FR new value %x\n",
3960                          * (uint32_t)tpi->rec.data.TSN_seq);
3961                          */
3962                         if (TAILQ_EMPTY(&asoc->send_queue)) {
3963                                 /*
3964                                  * If the queue of send is empty then its
3965                                  * the next sequence number that will be
3966                                  * assigned so we subtract one from this to
3967                                  * get the one we last sent.
3968                                  */
3969                                 tp1->rec.data.fast_retran_tsn = sending_seq;
3970                         } else {
3971                                 /*
3972                                  * If there are chunks on the send queue
3973                                  * (unsent data that has made it from the
3974                                  * stream queues but not out the door, we
3975                                  * take the first one (which will have the
3976                                  * lowest TSN) and subtract one to get the
3977                                  * one we last sent.
3978                                  */
3979                                 struct sctp_tmit_chunk *ttt;
3980
3981                                 ttt = TAILQ_FIRST(&asoc->send_queue);
3982                                 tp1->rec.data.fast_retran_tsn =
3983                                     ttt->rec.data.TSN_seq;
3984                         }
3985
3986                         if (tp1->do_rtt) {
3987                                 /*
3988                                  * this guy had a RTO calculation pending on
3989                                  * it, cancel it
3990                                  */
3991                                 tp1->do_rtt = 0;
3992                         }
3993                         if (alt != tp1->whoTo) {
3994                                 /* yes, there is an alternate. */
3995                                 sctp_free_remote_addr(tp1->whoTo);
3996                                 /* sa_ignore FREED_MEMORY */
3997                                 tp1->whoTo = alt;
3998                                 atomic_add_int(&alt->ref_count, 1);
3999                         }
4000                 }
4001                 tp1 = TAILQ_NEXT(tp1, sctp_next);
4002         }                       /* while (tp1) */
4003
4004         if (tot_retrans > 0) {
4005                 /*
4006                  * Setup the ecn nonce re-sync point. We do this since once
4007                  * we go to FR something we introduce a Karn's rule scenario
4008                  * and won't know the totals for the ECN bits.
4009                  */
4010                 asoc->nonce_resync_tsn = sending_seq;
4011                 asoc->nonce_wait_for_ecne = 0;
4012                 asoc->nonce_sum_check = 0;
4013         }
4014 }
4015
4016 struct sctp_tmit_chunk *
4017 sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
4018     struct sctp_association *asoc)
4019 {
4020         struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
4021         struct timeval now;
4022         int now_filled = 0;
4023
4024         if (asoc->peer_supports_prsctp == 0) {
4025                 return (NULL);
4026         }
4027         tp1 = TAILQ_FIRST(&asoc->sent_queue);
4028         while (tp1) {
4029                 if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
4030                     tp1->sent != SCTP_DATAGRAM_RESEND) {
4031                         /* no chance to advance, out of here */
4032                         break;
4033                 }
4034                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
4035                         if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
4036                                 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
4037                                     asoc->advanced_peer_ack_point,
4038                                     tp1->rec.data.TSN_seq, 0, 0);
4039                         }
4040                 }
4041                 if (!PR_SCTP_ENABLED(tp1->flags)) {
4042                         /*
4043                          * We can't fwd-tsn past any that are reliable aka
4044                          * retransmitted until the asoc fails.
4045                          */
4046                         break;
4047                 }
4048                 if (!now_filled) {
4049                         (void)SCTP_GETTIME_TIMEVAL(&now);
4050                         now_filled = 1;
4051                 }
4052                 tp2 = TAILQ_NEXT(tp1, sctp_next);
4053                 /*
4054                  * now we got a chunk which is marked for another
4055                  * retransmission to a PR-stream but has run out its chances
4056                  * already maybe OR has been marked to skip now. Can we skip
4057                  * it if its a resend?
4058                  */
4059                 if (tp1->sent == SCTP_DATAGRAM_RESEND &&
4060                     (PR_SCTP_TTL_ENABLED(tp1->flags))) {
4061                         /*
4062                          * Now is this one marked for resend and its time is
4063                          * now up?
4064                          */
4065                         if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
4066                                 /* Yes so drop it */
4067                                 if (tp1->data) {
4068                                         (void)sctp_release_pr_sctp_chunk(stcb, tp1,
4069                                             (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
4070                                             SCTP_SO_NOT_LOCKED);
4071                                 }
4072                         } else {
4073                                 /*
4074                                  * No, we are done when hit one for resend
4075                                  * whos time as not expired.
4076                                  */
4077                                 break;
4078                         }
4079                 }
4080                 /*
4081                  * Ok now if this chunk is marked to drop it we can clean up
4082                  * the chunk, advance our peer ack point and we can check
4083                  * the next chunk.
4084                  */
4085                 if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
4086                         /* advance PeerAckPoint goes forward */
4087                         if (compare_with_wrap(tp1->rec.data.TSN_seq,
4088                             asoc->advanced_peer_ack_point,
4089                             MAX_TSN)) {
4090
4091                                 asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
4092                                 a_adv = tp1;
4093                         } else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
4094                                 /* No update but we do save the chk */
4095                                 a_adv = tp1;
4096                         }
4097                 } else {
4098                         /*
4099                          * If it is still in RESEND we can advance no
4100                          * further
4101                          */
4102                         break;
4103                 }
4104                 /*
4105                  * If we hit here we just dumped tp1, move to next tsn on
4106                  * sent queue.
4107                  */
4108                 tp1 = tp2;
4109         }
4110         return (a_adv);
4111 }
4112
4113 static int
4114 sctp_fs_audit(struct sctp_association *asoc)
4115 {
4116         struct sctp_tmit_chunk *chk;
4117         int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
4118         int entry_flight, entry_cnt, ret;
4119
4120         entry_flight = asoc->total_flight;
4121         entry_cnt = asoc->total_flight_count;
4122         ret = 0;
4123
4124         if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
4125                 return (0);
4126
4127         TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
4128                 if (chk->sent < SCTP_DATAGRAM_RESEND) {
4129                         printf("Chk TSN:%u size:%d inflight cnt:%d\n",
4130                             chk->rec.data.TSN_seq,
4131                             chk->send_size,
4132                             chk->snd_count
4133                             );
4134                         inflight++;
4135                 } else if (chk->sent == SCTP_DATAGRAM_RESEND) {
4136                         resend++;
4137                 } else if (chk->sent < SCTP_DATAGRAM_ACKED) {
4138                         inbetween++;
4139                 } else if (chk->sent > SCTP_DATAGRAM_ACKED) {
4140                         above++;
4141                 } else {
4142                         acked++;
4143                 }
4144         }
4145
4146         if ((inflight > 0) || (inbetween > 0)) {
4147 #ifdef INVARIANTS
4148                 panic("Flight size-express incorrect? \n");
4149 #else
4150                 printf("asoc->total_flight:%d cnt:%d\n",
4151                     entry_flight, entry_cnt);
4152
4153                 SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n",
4154                     inflight, inbetween, resend, above, acked);
4155                 ret = 1;
4156 #endif
4157         }
4158         return (ret);
4159 }
4160
4161
4162 static void
4163 sctp_window_probe_recovery(struct sctp_tcb *stcb,
4164     struct sctp_association *asoc,
4165     struct sctp_nets *net,
4166     struct sctp_tmit_chunk *tp1)
4167 {
4168         tp1->window_probe = 0;
4169         if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
4170                 /* TSN's skipped we do NOT move back. */
4171                 sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
4172                     tp1->whoTo->flight_size,
4173                     tp1->book_size,
4174                     (uintptr_t) tp1->whoTo,
4175                     tp1->rec.data.TSN_seq);
4176                 return;
4177         }
4178         /* First setup this by shrinking flight */
4179         sctp_flight_size_decrease(tp1);
4180         sctp_total_flight_decrease(stcb, tp1);
4181         /* Now mark for resend */
4182         tp1->sent = SCTP_DATAGRAM_RESEND;
4183         asoc->sent_queue_retran_cnt++;
4184         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4185                 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
4186                     tp1->whoTo->flight_size,
4187                     tp1->book_size,
4188                     (uintptr_t) tp1->whoTo,
4189                     tp1->rec.data.TSN_seq);
4190         }
4191 }
4192
4193 void
4194 sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
4195     uint32_t rwnd, int nonce_sum_flag, int *abort_now)
4196 {
4197         struct sctp_nets *net;
4198         struct sctp_association *asoc;
4199         struct sctp_tmit_chunk *tp1, *tp2;
4200         uint32_t old_rwnd;
4201         int win_probe_recovery = 0;
4202         int win_probe_recovered = 0;
4203         int j, done_once = 0;
4204
4205         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4206                 sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
4207                     rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4208         }
4209         SCTP_TCB_LOCK_ASSERT(stcb);
4210 #ifdef SCTP_ASOCLOG_OF_TSNS
4211         stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
4212         stcb->asoc.cumack_log_at++;
4213         if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
4214                 stcb->asoc.cumack_log_at = 0;
4215         }
4216 #endif
4217         asoc = &stcb->asoc;
4218         old_rwnd = asoc->peers_rwnd;
4219         if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
4220                 /* old ack */
4221                 return;
4222         } else if (asoc->last_acked_seq == cumack) {
4223                 /* Window update sack */
4224                 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4225                     (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4226                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4227                         /* SWS sender side engages */
4228                         asoc->peers_rwnd = 0;
4229                 }
4230                 if (asoc->peers_rwnd > old_rwnd) {
4231                         goto again;
4232                 }
4233                 return;
4234         }
4235         /* First setup for CC stuff */
4236         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4237                 net->prev_cwnd = net->cwnd;
4238                 net->net_ack = 0;
4239                 net->net_ack2 = 0;
4240
4241                 /*
4242                  * CMT: Reset CUC and Fast recovery algo variables before
4243                  * SACK processing
4244                  */
4245                 net->new_pseudo_cumack = 0;
4246                 net->will_exit_fast_recovery = 0;
4247         }
4248         if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4249                 uint32_t send_s;
4250
4251                 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4252                         tp1 = TAILQ_LAST(&asoc->sent_queue,
4253                             sctpchunk_listhead);
4254                         send_s = tp1->rec.data.TSN_seq + 1;
4255                 } else {
4256                         send_s = asoc->sending_seq;
4257                 }
4258                 if ((cumack == send_s) ||
4259                     compare_with_wrap(cumack, send_s, MAX_TSN)) {
4260 #ifndef INVARIANTS
4261                         struct mbuf *oper;
4262
4263 #endif
4264 #ifdef INVARIANTS
4265                         panic("Impossible sack 1");
4266 #else
4267                         *abort_now = 1;
4268                         /* XXX */
4269                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4270                             0, M_DONTWAIT, 1, MT_DATA);
4271                         if (oper) {
4272                                 struct sctp_paramhdr *ph;
4273                                 uint32_t *ippp;
4274
4275                                 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4276                                     sizeof(uint32_t);
4277                                 ph = mtod(oper, struct sctp_paramhdr *);
4278                                 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4279                                 ph->param_length = htons(SCTP_BUF_LEN(oper));
4280                                 ippp = (uint32_t *) (ph + 1);
4281                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4282                         }
4283                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4284                         sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4285                         return;
4286 #endif
4287                 }
4288         }
4289         asoc->this_sack_highest_gap = cumack;
4290         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4291                 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4292                     stcb->asoc.overall_error_count,
4293                     0,
4294                     SCTP_FROM_SCTP_INDATA,
4295                     __LINE__);
4296         }
4297         stcb->asoc.overall_error_count = 0;
4298         if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
4299                 /* process the new consecutive TSN first */
4300                 tp1 = TAILQ_FIRST(&asoc->sent_queue);
4301                 while (tp1) {
4302                         tp2 = TAILQ_NEXT(tp1, sctp_next);
4303                         if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
4304                             MAX_TSN) ||
4305                             cumack == tp1->rec.data.TSN_seq) {
4306                                 if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
4307                                         printf("Warning, an unsent is now acked?\n");
4308                                 }
4309                                 /*
4310                                  * ECN Nonce: Add the nonce to the sender's
4311                                  * nonce sum
4312                                  */
4313                                 asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4314                                 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4315                                         /*
4316                                          * If it is less than ACKED, it is
4317                                          * now no-longer in flight. Higher
4318                                          * values may occur during marking
4319                                          */
4320                                         if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4321                                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4322                                                         sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4323                                                             tp1->whoTo->flight_size,
4324                                                             tp1->book_size,
4325                                                             (uintptr_t) tp1->whoTo,
4326                                                             tp1->rec.data.TSN_seq);
4327                                                 }
4328                                                 sctp_flight_size_decrease(tp1);
4329                                                 /* sa_ignore NO_NULL_CHK */
4330                                                 sctp_total_flight_decrease(stcb, tp1);
4331                                         }
4332                                         tp1->whoTo->net_ack += tp1->send_size;
4333                                         if (tp1->snd_count < 2) {
4334                                                 /*
4335                                                  * True non-retransmited
4336                                                  * chunk
4337                                                  */
4338                                                 tp1->whoTo->net_ack2 +=
4339                                                     tp1->send_size;
4340
4341                                                 /* update RTO too? */
4342                                                 if (tp1->do_rtt) {
4343                                                         tp1->whoTo->RTO =
4344                                                         /*
4345                                                          * sa_ignore
4346                                                          * NO_NULL_CHK
4347                                                          */
4348                                                             sctp_calculate_rto(stcb,
4349                                                             asoc, tp1->whoTo,
4350                                                             &tp1->sent_rcv_time,
4351                                                             sctp_align_safe_nocopy);
4352                                                         tp1->do_rtt = 0;
4353                                                 }
4354                                         }
4355                                         /*
4356                                          * CMT: CUCv2 algorithm. From the
4357                                          * cumack'd TSNs, for each TSN being
4358                                          * acked for the first time, set the
4359                                          * following variables for the
4360                                          * corresp destination.
4361                                          * new_pseudo_cumack will trigger a
4362                                          * cwnd update.
4363                                          * find_(rtx_)pseudo_cumack will
4364                                          * trigger search for the next
4365                                          * expected (rtx-)pseudo-cumack.
4366                                          */
4367                                         tp1->whoTo->new_pseudo_cumack = 1;
4368                                         tp1->whoTo->find_pseudo_cumack = 1;
4369                                         tp1->whoTo->find_rtx_pseudo_cumack = 1;
4370
4371                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4372                                                 /* sa_ignore NO_NULL_CHK */
4373                                                 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
4374                                         }
4375                                 }
4376                                 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4377                                         sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4378                                 }
4379                                 if (tp1->rec.data.chunk_was_revoked) {
4380                                         /* deflate the cwnd */
4381                                         tp1->whoTo->cwnd -= tp1->book_size;
4382                                         tp1->rec.data.chunk_was_revoked = 0;
4383                                 }
4384                                 tp1->sent = SCTP_DATAGRAM_ACKED;
4385                                 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4386                                 if (tp1->data) {
4387                                         /* sa_ignore NO_NULL_CHK */
4388                                         sctp_free_bufspace(stcb, asoc, tp1, 1);
4389                                         sctp_m_freem(tp1->data);
4390                                 }
4391                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4392                                         sctp_log_sack(asoc->last_acked_seq,
4393                                             cumack,
4394                                             tp1->rec.data.TSN_seq,
4395                                             0,
4396                                             0,
4397                                             SCTP_LOG_FREE_SENT);
4398                                 }
4399                                 tp1->data = NULL;
4400                                 asoc->sent_queue_cnt--;
4401                                 sctp_free_a_chunk(stcb, tp1);
4402                                 tp1 = tp2;
4403                         } else {
4404                                 break;
4405                         }
4406                 }
4407
4408         }
4409         /* sa_ignore NO_NULL_CHK */
4410         if (stcb->sctp_socket) {
4411 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4412                 struct socket *so;
4413
4414 #endif
4415
4416                 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4417                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4418                         /* sa_ignore NO_NULL_CHK */
4419                         sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
4420                 }
4421 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4422                 so = SCTP_INP_SO(stcb->sctp_ep);
4423                 atomic_add_int(&stcb->asoc.refcnt, 1);
4424                 SCTP_TCB_UNLOCK(stcb);
4425                 SCTP_SOCKET_LOCK(so, 1);
4426                 SCTP_TCB_LOCK(stcb);
4427                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4428                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4429                         /* assoc was freed while we were unlocked */
4430                         SCTP_SOCKET_UNLOCK(so, 1);
4431                         return;
4432                 }
4433 #endif
4434                 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4435 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4436                 SCTP_SOCKET_UNLOCK(so, 1);
4437 #endif
4438         } else {
4439                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4440                         sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
4441                 }
4442         }
4443
4444         /* JRS - Use the congestion control given in the CC module */
4445         if (asoc->last_acked_seq != cumack)
4446                 asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
4447
4448         asoc->last_acked_seq = cumack;
4449
4450         if (TAILQ_EMPTY(&asoc->sent_queue)) {
4451                 /* nothing left in-flight */
4452                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4453                         net->flight_size = 0;
4454                         net->partial_bytes_acked = 0;
4455                 }
4456                 asoc->total_flight = 0;
4457                 asoc->total_flight_count = 0;
4458         }
4459         /* ECN Nonce updates */
4460         if (asoc->ecn_nonce_allowed) {
4461                 if (asoc->nonce_sum_check) {
4462                         if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
4463                                 if (asoc->nonce_wait_for_ecne == 0) {
4464                                         struct sctp_tmit_chunk *lchk;
4465
4466                                         lchk = TAILQ_FIRST(&asoc->send_queue);
4467                                         asoc->nonce_wait_for_ecne = 1;
4468                                         if (lchk) {
4469                                                 asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
4470                                         } else {
4471                                                 asoc->nonce_wait_tsn = asoc->sending_seq;
4472                                         }
4473                                 } else {
4474                                         if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
4475                                             (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
4476                                                 /*
4477                                                  * Misbehaving peer. We need
4478                                                  * to react to this guy
4479                                                  */
4480                                                 asoc->ecn_allowed = 0;
4481                                                 asoc->ecn_nonce_allowed = 0;
4482                                         }
4483                                 }
4484                         }
4485                 } else {
4486                         /* See if Resynchronization Possible */
4487                         if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
4488                                 asoc->nonce_sum_check = 1;
4489                                 /*
4490                                  * now we must calculate what the base is.
4491                                  * We do this based on two things, we know
4492                                  * the total's for all the segments
4493                                  * gap-acked in the SACK (none), We also
4494                                  * know the SACK's nonce sum, its in
4495                                  * nonce_sum_flag. So we can build a truth
4496                                  * table to back-calculate the new value of
4497                                  * asoc->nonce_sum_expect_base:
4498                                  * 
4499                                  * SACK-flag-Value         Seg-Sums Base 0 0 0
4500                                  * 1                    0 1 0 1 1 1
4501                                  * 1 0
4502                                  */
4503                                 asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
4504                         }
4505                 }
4506         }
4507         /* RWND update */
4508         asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4509             (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4510         if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4511                 /* SWS sender side engages */
4512                 asoc->peers_rwnd = 0;
4513         }
4514         if (asoc->peers_rwnd > old_rwnd) {
4515                 win_probe_recovery = 1;
4516         }
4517         /* Now assure a timer where data is queued at */
4518 again:
4519         j = 0;
4520         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4521                 int to_ticks;
4522
4523                 if (win_probe_recovery && (net->window_probe)) {
4524                         win_probe_recovered = 1;
4525                         /*
4526                          * Find first chunk that was used with window probe
4527                          * and clear the sent
4528                          */
4529                         /* sa_ignore FREED_MEMORY */
4530                         TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4531                                 if (tp1->window_probe) {
4532                                         sctp_window_probe_recovery(stcb, asoc, net, tp1);
4533                                         break;
4534                                 }
4535                         }
4536                 }
4537                 if (net->RTO == 0) {
4538                         to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4539                 } else {
4540                         to_ticks = MSEC_TO_TICKS(net->RTO);
4541                 }
4542                 if (net->flight_size) {
4543                         j++;
4544                         (void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4545                             sctp_timeout_handler, &net->rxt_timer);
4546                         if (net->window_probe) {
4547                                 net->window_probe = 0;
4548                         }
4549                 } else {
4550                         if (net->window_probe) {
4551                                 /*
4552                                  * In window probes we must assure a timer
4553                                  * is still running there
4554                                  */
4555                                 net->window_probe = 0;
4556                                 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4557                                         SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4558                                             sctp_timeout_handler, &net->rxt_timer);
4559                                 }
4560                         } else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4561                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4562                                     stcb, net,
4563                                     SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4564                         }
4565                         if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4566                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4567                                         SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4568                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4569                                             SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
4570                                 }
4571                         }
4572                 }
4573         }
4574         if ((j == 0) &&
4575             (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4576             (asoc->sent_queue_retran_cnt == 0) &&
4577             (win_probe_recovered == 0) &&
4578             (done_once == 0)) {
4579                 /*
4580                  * huh, this should not happen unless all packets are
4581                  * PR-SCTP and marked to skip of course.
4582                  */
4583                 if (sctp_fs_audit(asoc)) {
4584                         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4585                                 if (net->flight_size) {
4586                                         net->flight_size = 0;
4587                                 }
4588                         }
4589                         asoc->total_flight = 0;
4590                         asoc->total_flight_count = 0;
4591                         asoc->sent_queue_retran_cnt = 0;
4592                         TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4593                                 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4594                                         sctp_flight_size_increase(tp1);
4595                                         sctp_total_flight_increase(stcb, tp1);
4596                                 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4597                                         asoc->sent_queue_retran_cnt++;
4598                                 }
4599                         }
4600                 }
4601                 done_once = 1;
4602                 goto again;
4603         }
4604         /**********************************/
4605         /* Now what about shutdown issues */
4606         /**********************************/
4607         if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4608                 /* nothing left on sendqueue.. consider done */
4609                 /* clean up */
4610                 if ((asoc->stream_queue_cnt == 1) &&
4611                     ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4612                     (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4613                     (asoc->locked_on_sending)
4614                     ) {
4615                         struct sctp_stream_queue_pending *sp;
4616
4617                         /*
4618                          * I may be in a state where we got all across.. but
4619                          * cannot write more due to a shutdown... we abort
4620                          * since the user did not indicate EOR in this case.
4621                          * The sp will be cleaned during free of the asoc.
4622                          */
4623                         sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4624                             sctp_streamhead);
4625                         if ((sp) && (sp->length == 0)) {
4626                                 /* Let cleanup code purge it */
4627                                 if (sp->msg_is_complete) {
4628                                         asoc->stream_queue_cnt--;
4629                                 } else {
4630                                         asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4631                                         asoc->locked_on_sending = NULL;
4632                                         asoc->stream_queue_cnt--;
4633                                 }
4634                         }
4635                 }
4636                 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4637                     (asoc->stream_queue_cnt == 0)) {
4638                         if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4639                                 /* Need to abort here */
4640                                 struct mbuf *oper;
4641
4642                 abort_out_now:
4643                                 *abort_now = 1;
4644                                 /* XXX */
4645                                 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4646                                     0, M_DONTWAIT, 1, MT_DATA);
4647                                 if (oper) {
4648                                         struct sctp_paramhdr *ph;
4649                                         uint32_t *ippp;
4650
4651                                         SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4652                                             sizeof(uint32_t);
4653                                         ph = mtod(oper, struct sctp_paramhdr *);
4654                                         ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4655                                         ph->param_length = htons(SCTP_BUF_LEN(oper));
4656                                         ippp = (uint32_t *) (ph + 1);
4657                                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4658                                 }
4659                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4660                                 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4661                         } else {
4662                                 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4663                                     (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4664                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4665                                 }
4666                                 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4667                                 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4668                                 sctp_stop_timers_for_shutdown(stcb);
4669                                 sctp_send_shutdown(stcb,
4670                                     stcb->asoc.primary_destination);
4671                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4672                                     stcb->sctp_ep, stcb, asoc->primary_destination);
4673                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4674                                     stcb->sctp_ep, stcb, asoc->primary_destination);
4675                         }
4676                 } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4677                     (asoc->stream_queue_cnt == 0)) {
4678                         if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4679                                 goto abort_out_now;
4680                         }
4681                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4682                         SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4683                         SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4684                         sctp_send_shutdown_ack(stcb,
4685                             stcb->asoc.primary_destination);
4686
4687                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4688                             stcb->sctp_ep, stcb, asoc->primary_destination);
4689                 }
4690         }
4691         /*********************************************/
4692         /* Here we perform PR-SCTP procedures        */
4693         /* (section 4.2)                             */
4694         /*********************************************/
4695         /* C1. update advancedPeerAckPoint */
4696         if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
4697                 asoc->advanced_peer_ack_point = cumack;
4698         }
4699         /* PR-Sctp issues need to be addressed too */
4700         if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
4701                 struct sctp_tmit_chunk *lchk;
4702                 uint32_t old_adv_peer_ack_point;
4703
4704                 old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4705                 lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4706                 /* C3. See if we need to send a Fwd-TSN */
4707                 if (compare_with_wrap(asoc->advanced_peer_ack_point, cumack,
4708                     MAX_TSN)) {
4709                         /*
4710                          * ISSUE with ECN, see FWD-TSN processing for notes
4711                          * on issues that will occur when the ECN NONCE
4712                          * stuff is put into SCTP for cross checking.
4713                          */
4714                         if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
4715                             MAX_TSN)) {
4716                                 send_forward_tsn(stcb, asoc);
4717                                 /*
4718                                  * ECN Nonce: Disable Nonce Sum check when
4719                                  * FWD TSN is sent and store resync tsn
4720                                  */
4721                                 asoc->nonce_sum_check = 0;
4722                                 asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
4723                         } else if (lchk) {
4724                                 /* try to FR fwd-tsn's that get lost too */
4725                                 lchk->rec.data.fwd_tsn_cnt++;
4726                                 if (lchk->rec.data.fwd_tsn_cnt > 3) {
4727                                         send_forward_tsn(stcb, asoc);
4728                                         lchk->rec.data.fwd_tsn_cnt = 0;
4729                                 }
4730                         }
4731                 }
4732                 if (lchk) {
4733                         /* Assure a timer is up */
4734                         sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4735                             stcb->sctp_ep, stcb, lchk->whoTo);
4736                 }
4737         }
4738         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4739                 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4740                     rwnd,
4741                     stcb->asoc.peers_rwnd,
4742                     stcb->asoc.total_flight,
4743                     stcb->asoc.total_output_queue_size);
4744         }
4745 }
4746
4747 void
4748 sctp_handle_sack(struct mbuf *m, int offset,
4749     struct sctp_sack_chunk *ch, struct sctp_tcb *stcb,
4750     struct sctp_nets *net_from, int *abort_now, int sack_len, uint32_t rwnd)
4751 {
4752         struct sctp_association *asoc;
4753         struct sctp_sack *sack;
4754         struct sctp_tmit_chunk *tp1, *tp2;
4755         uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
4756                  this_sack_lowest_newack;
4757         uint32_t sav_cum_ack;
4758         uint16_t num_seg, num_dup;
4759         uint16_t wake_him = 0;
4760         unsigned int sack_length;
4761         uint32_t send_s = 0;
4762         long j;
4763         int accum_moved = 0;
4764         int will_exit_fast_recovery = 0;
4765         uint32_t a_rwnd, old_rwnd;
4766         int win_probe_recovery = 0;
4767         int win_probe_recovered = 0;
4768         struct sctp_nets *net = NULL;
4769         int nonce_sum_flag, ecn_seg_sums = 0;
4770         int done_once;
4771         uint8_t reneged_all = 0;
4772         uint8_t cmt_dac_flag;
4773
4774         /*
4775          * we take any chance we can to service our queues since we cannot
4776          * get awoken when the socket is read from :<
4777          */
4778         /*
4779          * Now perform the actual SACK handling: 1) Verify that it is not an
4780          * old sack, if so discard. 2) If there is nothing left in the send
4781          * queue (cum-ack is equal to last acked) then you have a duplicate
4782          * too, update any rwnd change and verify no timers are running.
4783          * then return. 3) Process any new consequtive data i.e. cum-ack
4784          * moved process these first and note that it moved. 4) Process any
4785          * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4786          * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4787          * sync up flightsizes and things, stop all timers and also check
4788          * for shutdown_pending state. If so then go ahead and send off the
4789          * shutdown. If in shutdown recv, send off the shutdown-ack and
4790          * start that timer, Ret. 9) Strike any non-acked things and do FR
4791          * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4792          * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4793          * if in shutdown_recv state.
4794          */
4795         SCTP_TCB_LOCK_ASSERT(stcb);
4796         sack = &ch->sack;
4797         /* CMT DAC algo */
4798         this_sack_lowest_newack = 0;
4799         j = 0;
4800         sack_length = (unsigned int)sack_len;
4801         /* ECN Nonce */
4802         SCTP_STAT_INCR(sctps_slowpath_sack);
4803         nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
4804         cum_ack = last_tsn = ntohl(sack->cum_tsn_ack);
4805 #ifdef SCTP_ASOCLOG_OF_TSNS
4806         stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
4807         stcb->asoc.cumack_log_at++;
4808         if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
4809                 stcb->asoc.cumack_log_at = 0;
4810         }
4811 #endif
4812         num_seg = ntohs(sack->num_gap_ack_blks);
4813         a_rwnd = rwnd;
4814
4815         /* CMT DAC algo */
4816         cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
4817         num_dup = ntohs(sack->num_dup_tsns);
4818
4819         old_rwnd = stcb->asoc.peers_rwnd;
4820         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4821                 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4822                     stcb->asoc.overall_error_count,
4823                     0,
4824                     SCTP_FROM_SCTP_INDATA,
4825                     __LINE__);
4826         }
4827         stcb->asoc.overall_error_count = 0;
4828         asoc = &stcb->asoc;
4829         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4830                 sctp_log_sack(asoc->last_acked_seq,
4831                     cum_ack,
4832                     0,
4833                     num_seg,
4834                     num_dup,
4835                     SCTP_LOG_NEW_SACK);
4836         }
4837         if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
4838                 int off_to_dup, iii;
4839                 uint32_t *dupdata, dblock;
4840
4841                 off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) + sizeof(struct sctp_sack_chunk);
4842                 if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= sack_length) {
4843                         dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4844                             sizeof(uint32_t), (uint8_t *) & dblock);
4845                         off_to_dup += sizeof(uint32_t);
4846                         if (dupdata) {
4847                                 for (iii = 0; iii < num_dup; iii++) {
4848                                         sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4849                                         dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4850                                             sizeof(uint32_t), (uint8_t *) & dblock);
4851                                         if (dupdata == NULL)
4852                                                 break;
4853                                         off_to_dup += sizeof(uint32_t);
4854                                 }
4855                         }
4856                 } else {
4857                         SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d sack_len:%d num gaps:%d\n",
4858                             off_to_dup, num_dup, sack_length, num_seg);
4859                 }
4860         }
4861         if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4862                 /* reality check */
4863                 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4864                         tp1 = TAILQ_LAST(&asoc->sent_queue,
4865                             sctpchunk_listhead);
4866                         send_s = tp1->rec.data.TSN_seq + 1;
4867                 } else {
4868                         send_s = asoc->sending_seq;
4869                 }
4870                 if (cum_ack == send_s ||
4871                     compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
4872 #ifndef INVARIANTS
4873                         struct mbuf *oper;
4874
4875 #endif
4876 #ifdef INVARIANTS
4877         hopeless_peer:
4878                         panic("Impossible sack 1");
4879 #else
4880
4881
4882                         /*
4883                          * no way, we have not even sent this TSN out yet.
4884                          * Peer is hopelessly messed up with us.
4885                          */
4886         hopeless_peer:
4887                         *abort_now = 1;
4888                         /* XXX */
4889                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4890                             0, M_DONTWAIT, 1, MT_DATA);
4891                         if (oper) {
4892                                 struct sctp_paramhdr *ph;
4893                                 uint32_t *ippp;
4894
4895                                 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4896                                     sizeof(uint32_t);
4897                                 ph = mtod(oper, struct sctp_paramhdr *);
4898                                 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4899                                 ph->param_length = htons(SCTP_BUF_LEN(oper));
4900                                 ippp = (uint32_t *) (ph + 1);
4901                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4902                         }
4903                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4904                         sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4905                         return;
4906 #endif
4907                 }
4908         }
4909         /**********************/
4910         /* 1) check the range */
4911         /**********************/
4912         if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
4913                 /* acking something behind */
4914                 return;
4915         }
4916         sav_cum_ack = asoc->last_acked_seq;
4917
4918         /* update the Rwnd of the peer */
4919         if (TAILQ_EMPTY(&asoc->sent_queue) &&
4920             TAILQ_EMPTY(&asoc->send_queue) &&
4921             (asoc->stream_queue_cnt == 0)
4922             ) {
4923                 /* nothing left on send/sent and strmq */
4924                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4925                         sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4926                             asoc->peers_rwnd, 0, 0, a_rwnd);
4927                 }
4928                 asoc->peers_rwnd = a_rwnd;
4929                 if (asoc->sent_queue_retran_cnt) {
4930                         asoc->sent_queue_retran_cnt = 0;
4931                 }
4932                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4933                         /* SWS sender side engages */
4934                         asoc->peers_rwnd = 0;
4935                 }
4936                 /* stop any timers */
4937                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4938                         sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4939                             stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4940                         if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4941                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4942                                         SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
4943                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4944                                             SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4945                                 }
4946                         }
4947                         net->partial_bytes_acked = 0;
4948                         net->flight_size = 0;
4949                 }
4950                 asoc->total_flight = 0;
4951                 asoc->total_flight_count = 0;
4952                 return;
4953         }
4954         /*
4955          * We init netAckSz and netAckSz2 to 0. These are used to track 2
4956          * things. The total byte count acked is tracked in netAckSz AND
4957          * netAck2 is used to track the total bytes acked that are un-
4958          * amibguious and were never retransmitted. We track these on a per
4959          * destination address basis.
4960          */
4961         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4962                 net->prev_cwnd = net->cwnd;
4963                 net->net_ack = 0;
4964                 net->net_ack2 = 0;
4965
4966                 /*
4967                  * CMT: Reset CUC and Fast recovery algo variables before
4968                  * SACK processing
4969                  */
4970                 net->new_pseudo_cumack = 0;
4971                 net->will_exit_fast_recovery = 0;
4972         }
4973         /* process the new consecutive TSN first */
4974         tp1 = TAILQ_FIRST(&asoc->sent_queue);
4975         while (tp1) {
4976                 if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
4977                     MAX_TSN) ||
4978                     last_tsn == tp1->rec.data.TSN_seq) {
4979                         if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4980                                 /*
4981                                  * ECN Nonce: Add the nonce to the sender's
4982                                  * nonce sum
4983                                  */
4984                                 asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4985                                 accum_moved = 1;
4986                                 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4987                                         /*
4988                                          * If it is less than ACKED, it is
4989                                          * now no-longer in flight. Higher
4990                                          * values may occur during marking
4991                                          */
4992                                         if ((tp1->whoTo->dest_state &
4993                                             SCTP_ADDR_UNCONFIRMED) &&
4994                                             (tp1->snd_count < 2)) {
4995                                                 /*
4996                                                  * If there was no retran
4997                                                  * and the address is
4998                                                  * un-confirmed and we sent
4999                                                  * there and are now
5000                                                  * sacked.. its confirmed,
5001                                                  * mark it so.
5002                                                  */
5003                                                 tp1->whoTo->dest_state &=
5004                                                     ~SCTP_ADDR_UNCONFIRMED;
5005                                         }
5006                                         if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5007                                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
5008                                                         sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
5009                                                             tp1->whoTo->flight_size,
5010                                                             tp1->book_size,
5011                                                             (uintptr_t) tp1->whoTo,
5012                                                             tp1->rec.data.TSN_seq);
5013                                                 }
5014                                                 sctp_flight_size_decrease(tp1);
5015                                                 sctp_total_flight_decrease(stcb, tp1);
5016                                         }
5017                                         tp1->whoTo->net_ack += tp1->send_size;
5018
5019                                         /* CMT SFR and DAC algos */
5020                                         this_sack_lowest_newack = tp1->rec.data.TSN_seq;
5021                                         tp1->whoTo->saw_newack = 1;
5022
5023                                         if (tp1->snd_count < 2) {
5024                                                 /*
5025                                                  * True non-retransmited
5026                                                  * chunk
5027                                                  */
5028                                                 tp1->whoTo->net_ack2 +=
5029                                                     tp1->send_size;
5030
5031                                                 /* update RTO too? */
5032                                                 if (tp1->do_rtt) {
5033                                                         tp1->whoTo->RTO =
5034                                                             sctp_calculate_rto(stcb,
5035                                                             asoc, tp1->whoTo,
5036                                                             &tp1->sent_rcv_time,
5037                                                             sctp_align_safe_nocopy);
5038                                                         tp1->do_rtt = 0;
5039                                                 }
5040                                         }
5041                                         /*
5042                                          * CMT: CUCv2 algorithm. From the
5043                                          * cumack'd TSNs, for each TSN being
5044                                          * acked for the first time, set the
5045                                          * following variables for the
5046                                          * corresp destination.
5047                                          * new_pseudo_cumack will trigger a
5048                                          * cwnd update.
5049                                          * find_(rtx_)pseudo_cumack will
5050                                          * trigger search for the next
5051                                          * expected (rtx-)pseudo-cumack.
5052                                          */
5053                                         tp1->whoTo->new_pseudo_cumack = 1;
5054                                         tp1->whoTo->find_pseudo_cumack = 1;
5055                                         tp1->whoTo->find_rtx_pseudo_cumack = 1;
5056
5057
5058                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
5059                                                 sctp_log_sack(asoc->last_acked_seq,
5060                                                     cum_ack,
5061                                                     tp1->rec.data.TSN_seq,
5062                                                     0,
5063                                                     0,
5064                                                     SCTP_LOG_TSN_ACKED);
5065                                         }
5066                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
5067                                                 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
5068                                         }
5069                                 }
5070                                 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5071                                         sctp_ucount_decr(asoc->sent_queue_retran_cnt);
5072 #ifdef SCTP_AUDITING_ENABLED
5073                                         sctp_audit_log(0xB3,
5074                                             (asoc->sent_queue_retran_cnt & 0x000000ff));
5075 #endif
5076                                 }
5077                                 if (tp1->rec.data.chunk_was_revoked) {
5078                                         /* deflate the cwnd */
5079                                         tp1->whoTo->cwnd -= tp1->book_size;
5080                                         tp1->rec.data.chunk_was_revoked = 0;
5081                                 }
5082                                 tp1->sent = SCTP_DATAGRAM_ACKED;
5083                         }
5084                 } else {
5085                         break;
5086                 }
5087                 tp1 = TAILQ_NEXT(tp1, sctp_next);
5088         }
5089         biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
5090         /* always set this up to cum-ack */
5091         asoc->this_sack_highest_gap = last_tsn;
5092
5093         /* Move offset up to point to gaps/dups */
5094         offset += sizeof(struct sctp_sack_chunk);
5095         if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_sack_chunk)) > sack_length) {
5096
5097                 /* skip corrupt segments */
5098                 goto skip_segments;
5099         }
5100         if (num_seg > 0) {
5101
5102                 /*
5103                  * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
5104                  * to be greater than the cumack. Also reset saw_newack to 0
5105                  * for all dests.
5106                  */
5107                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5108                         net->saw_newack = 0;
5109                         net->this_sack_highest_newack = last_tsn;
5110                 }
5111
5112                 /*
5113                  * thisSackHighestGap will increase while handling NEW
5114                  * segments this_sack_highest_newack will increase while
5115                  * handling NEWLY ACKED chunks. this_sack_lowest_newack is
5116                  * used for CMT DAC algo. saw_newack will also change.
5117                  */
5118                 sctp_handle_segments(m, &offset, stcb, asoc, ch, last_tsn,
5119                     &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
5120                     num_seg, &ecn_seg_sums);
5121
5122                 if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
5123                         /*
5124                          * validate the biggest_tsn_acked in the gap acks if
5125                          * strict adherence is wanted.
5126                          */
5127                         if ((biggest_tsn_acked == send_s) ||
5128                             (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
5129                                 /*
5130                                  * peer is either confused or we are under
5131                                  * attack. We must abort.
5132                                  */
5133                                 goto hopeless_peer;
5134                         }
5135                 }
5136         }
5137 skip_segments:
5138         /*******************************************/
5139         /* cancel ALL T3-send timer if accum moved */
5140         /*******************************************/
5141         if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
5142                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5143                         if (net->new_pseudo_cumack)
5144                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5145                                     stcb, net,
5146                                     SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
5147
5148                 }
5149         } else {
5150                 if (accum_moved) {
5151                         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5152                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5153                                     stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
5154                         }
5155                 }
5156         }
5157         /********************************************/
5158         /* drop the acked chunks from the sendqueue */
5159         /********************************************/
5160         asoc->last_acked_seq = cum_ack;
5161
5162         tp1 = TAILQ_FIRST(&asoc->sent_queue);
5163         if (tp1 == NULL)
5164                 goto done_with_it;
5165         do {
5166                 if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
5167                     MAX_TSN)) {
5168                         break;
5169                 }
5170                 if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
5171                         /* no more sent on list */
5172                         printf("Warning, tp1->sent == %d and its now acked?\n",
5173                             tp1->sent);
5174                 }
5175                 tp2 = TAILQ_NEXT(tp1, sctp_next);
5176                 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
5177                 if (tp1->pr_sctp_on) {
5178                         if (asoc->pr_sctp_cnt != 0)
5179                                 asoc->pr_sctp_cnt--;
5180                 }
5181                 if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
5182                     (asoc->total_flight > 0)) {
5183 #ifdef INVARIANTS
5184                         panic("Warning flight size is postive and should be 0");
5185 #else
5186                         SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
5187                             asoc->total_flight);
5188 #endif
5189                         asoc->total_flight = 0;
5190                 }
5191                 if (tp1->data) {
5192                         /* sa_ignore NO_NULL_CHK */
5193                         sctp_free_bufspace(stcb, asoc, tp1, 1);
5194                         sctp_m_freem(tp1->data);
5195                         if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
5196                                 asoc->sent_queue_cnt_removeable--;
5197                         }
5198                 }
5199                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
5200                         sctp_log_sack(asoc->last_acked_seq,
5201                             cum_ack,
5202                             tp1->rec.data.TSN_seq,
5203                             0,
5204                             0,
5205                             SCTP_LOG_FREE_SENT);
5206                 }
5207                 tp1->data = NULL;
5208                 asoc->sent_queue_cnt--;
5209                 sctp_free_a_chunk(stcb, tp1);
5210                 wake_him++;
5211                 tp1 = tp2;
5212         } while (tp1 != NULL);
5213
5214 done_with_it:
5215         /* sa_ignore NO_NULL_CHK */
5216         if ((wake_him) && (stcb->sctp_socket)) {
5217 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5218                 struct socket *so;
5219
5220 #endif
5221                 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
5222                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
5223                         sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
5224                 }
5225 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5226                 so = SCTP_INP_SO(stcb->sctp_ep);
5227                 atomic_add_int(&stcb->asoc.refcnt, 1);
5228                 SCTP_TCB_UNLOCK(stcb);
5229                 SCTP_SOCKET_LOCK(so, 1);
5230                 SCTP_TCB_LOCK(stcb);
5231                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5232                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
5233                         /* assoc was freed while we were unlocked */
5234                         SCTP_SOCKET_UNLOCK(so, 1);
5235                         return;
5236                 }
5237 #endif
5238                 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
5239 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5240                 SCTP_SOCKET_UNLOCK(so, 1);
5241 #endif
5242         } else {
5243                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
5244                         sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
5245                 }
5246         }
5247
5248         if (asoc->fast_retran_loss_recovery && accum_moved) {
5249                 if (compare_with_wrap(asoc->last_acked_seq,
5250                     asoc->fast_recovery_tsn, MAX_TSN) ||
5251                     asoc->last_acked_seq == asoc->fast_recovery_tsn) {
5252                         /* Setup so we will exit RFC2582 fast recovery */
5253                         will_exit_fast_recovery = 1;
5254                 }
5255         }
5256         /*
5257          * Check for revoked fragments:
5258          * 
5259          * if Previous sack - Had no frags then we can't have any revoked if
5260          * Previous sack - Had frag's then - If we now have frags aka
5261          * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
5262          * some of them. else - The peer revoked all ACKED fragments, since
5263          * we had some before and now we have NONE.
5264          */
5265
5266         if (num_seg)
5267                 sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
5268         else if (asoc->saw_sack_with_frags) {
5269                 int cnt_revoked = 0;
5270
5271                 tp1 = TAILQ_FIRST(&asoc->sent_queue);
5272                 if (tp1 != NULL) {
5273                         /* Peer revoked all dg's marked or acked */
5274                         TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5275                                 if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
5276                                     (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
5277                                         tp1->sent = SCTP_DATAGRAM_SENT;
5278                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
5279                                                 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
5280                                                     tp1->whoTo->flight_size,
5281                                                     tp1->book_size,
5282                                                     (uintptr_t) tp1->whoTo,
5283                                                     tp1->rec.data.TSN_seq);
5284                                         }
5285                                         sctp_flight_size_increase(tp1);
5286                                         sctp_total_flight_increase(stcb, tp1);
5287                                         tp1->rec.data.chunk_was_revoked = 1;
5288                                         /*
5289                                          * To ensure that this increase in
5290                                          * flightsize, which is artificial,
5291                                          * does not throttle the sender, we
5292                                          * also increase the cwnd
5293                                          * artificially.
5294                                          */
5295                                         tp1->whoTo->cwnd += tp1->book_size;
5296                                         cnt_revoked++;
5297                                 }
5298                         }
5299                         if (cnt_revoked) {
5300                                 reneged_all = 1;
5301                         }
5302                 }
5303                 asoc->saw_sack_with_frags = 0;
5304         }
5305         if (num_seg)
5306                 asoc->saw_sack_with_frags = 1;
5307         else
5308                 asoc->saw_sack_with_frags = 0;
5309
5310         /* JRS - Use the congestion control given in the CC module */
5311         asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
5312
5313         if (TAILQ_EMPTY(&asoc->sent_queue)) {
5314                 /* nothing left in-flight */
5315                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5316                         /* stop all timers */
5317                         if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
5318                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5319                                         SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5320                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5321                                             SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
5322                                 }
5323                         }
5324                         sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5325                             stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
5326                         net->flight_size = 0;
5327                         net->partial_bytes_acked = 0;
5328                 }
5329                 asoc->total_flight = 0;
5330                 asoc->total_flight_count = 0;
5331         }
5332         /**********************************/
5333         /* Now what about shutdown issues */
5334         /**********************************/
5335         if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
5336                 /* nothing left on sendqueue.. consider done */
5337                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5338                         sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5339                             asoc->peers_rwnd, 0, 0, a_rwnd);
5340                 }
5341                 asoc->peers_rwnd = a_rwnd;
5342                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5343                         /* SWS sender side engages */
5344                         asoc->peers_rwnd = 0;
5345                 }
5346                 /* clean up */
5347                 if ((asoc->stream_queue_cnt == 1) &&
5348                     ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
5349                     (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
5350                     (asoc->locked_on_sending)
5351                     ) {
5352                         struct sctp_stream_queue_pending *sp;
5353
5354                         /*
5355                          * I may be in a state where we got all across.. but
5356                          * cannot write more due to a shutdown... we abort
5357                          * since the user did not indicate EOR in this case.
5358                          */
5359                         sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
5360                             sctp_streamhead);
5361                         if ((sp) && (sp->length == 0)) {
5362                                 asoc->locked_on_sending = NULL;
5363                                 if (sp->msg_is_complete) {
5364                                         asoc->stream_queue_cnt--;
5365                                 } else {
5366                                         asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
5367                                         asoc->stream_queue_cnt--;
5368                                 }
5369                         }
5370                 }
5371                 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
5372                     (asoc->stream_queue_cnt == 0)) {
5373                         if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5374                                 /* Need to abort here */
5375                                 struct mbuf *oper;
5376
5377                 abort_out_now:
5378                                 *abort_now = 1;
5379                                 /* XXX */
5380                                 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
5381                                     0, M_DONTWAIT, 1, MT_DATA);
5382                                 if (oper) {
5383                                         struct sctp_paramhdr *ph;
5384                                         uint32_t *ippp;
5385
5386                                         SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5387                                             sizeof(uint32_t);
5388                                         ph = mtod(oper, struct sctp_paramhdr *);
5389                                         ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
5390                                         ph->param_length = htons(SCTP_BUF_LEN(oper));
5391                                         ippp = (uint32_t *) (ph + 1);
5392                                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
5393                                 }
5394                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
5395                                 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
5396                                 return;
5397                         } else {
5398                                 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
5399                                     (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
5400                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5401                                 }
5402                                 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
5403                                 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5404                                 sctp_stop_timers_for_shutdown(stcb);
5405                                 sctp_send_shutdown(stcb,
5406                                     stcb->asoc.primary_destination);
5407                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
5408                                     stcb->sctp_ep, stcb, asoc->primary_destination);
5409                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
5410                                     stcb->sctp_ep, stcb, asoc->primary_destination);
5411                         }
5412                         return;
5413                 } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
5414                     (asoc->stream_queue_cnt == 0)) {
5415                         if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5416                                 goto abort_out_now;
5417                         }
5418                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5419                         SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
5420                         SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5421                         sctp_send_shutdown_ack(stcb,
5422                             stcb->asoc.primary_destination);
5423
5424                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
5425                             stcb->sctp_ep, stcb, asoc->primary_destination);
5426                         return;
5427                 }
5428         }
5429         /*
5430          * Now here we are going to recycle net_ack for a different use...
5431          * HEADS UP.
5432          */
5433         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5434                 net->net_ack = 0;
5435         }
5436
5437         /*
5438          * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5439          * to be done. Setting this_sack_lowest_newack to the cum_ack will
5440          * automatically ensure that.
5441          */
5442         if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac) && (cmt_dac_flag == 0)) {
5443                 this_sack_lowest_newack = cum_ack;
5444         }
5445         if (num_seg > 0) {
5446                 sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5447                     biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5448         }
5449         /* JRS - Use the congestion control given in the CC module */
5450         asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
5451
5452         /******************************************************************
5453          *  Here we do the stuff with ECN Nonce checking.
5454          *  We basically check to see if the nonce sum flag was incorrect
5455          *  or if resynchronization needs to be done. Also if we catch a
5456          *  misbehaving receiver we give him the kick.
5457          ******************************************************************/
5458
5459         if (asoc->ecn_nonce_allowed) {
5460                 if (asoc->nonce_sum_check) {
5461                         if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
5462                                 if (asoc->nonce_wait_for_ecne == 0) {
5463                                         struct sctp_tmit_chunk *lchk;
5464
5465                                         lchk = TAILQ_FIRST(&asoc->send_queue);
5466                                         asoc->nonce_wait_for_ecne = 1;
5467                                         if (lchk) {
5468                                                 asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
5469                                         } else {
5470                                                 asoc->nonce_wait_tsn = asoc->sending_seq;
5471                                         }
5472                                 } else {
5473                                         if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
5474                                             (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
5475                                                 /*
5476                                                  * Misbehaving peer. We need
5477                                                  * to react to this guy
5478                                                  */
5479                                                 asoc->ecn_allowed = 0;
5480                                                 asoc->ecn_nonce_allowed = 0;
5481                                         }
5482                                 }
5483                         }
5484                 } else {
5485                         /* See if Resynchronization Possible */
5486                         if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
5487                                 asoc->nonce_sum_check = 1;
5488                                 /*
5489                                  * now we must calculate what the base is.
5490                                  * We do this based on two things, we know
5491                                  * the total's for all the segments
5492                                  * gap-acked in the SACK, its stored in
5493                                  * ecn_seg_sums. We also know the SACK's
5494                                  * nonce sum, its in nonce_sum_flag. So we
5495                                  * can build a truth table to back-calculate
5496                                  * the new value of
5497                                  * asoc->nonce_sum_expect_base:
5498                                  * 
5499                                  * SACK-flag-Value         Seg-Sums Base 0 0 0
5500                                  * 1                    0 1 0 1 1 1
5501                                  * 1 0
5502                                  */
5503                                 asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
5504                         }
5505                 }
5506         }
5507         /* Now are we exiting loss recovery ? */
5508         if (will_exit_fast_recovery) {
5509                 /* Ok, we must exit fast recovery */
5510                 asoc->fast_retran_loss_recovery = 0;
5511         }
5512         if ((asoc->sat_t3_loss_recovery) &&
5513             ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
5514             MAX_TSN) ||
5515             (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
5516                 /* end satellite t3 loss recovery */
5517                 asoc->sat_t3_loss_recovery = 0;
5518         }
5519         /*
5520          * CMT Fast recovery
5521          */
5522         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5523                 if (net->will_exit_fast_recovery) {
5524                         /* Ok, we must exit fast recovery */
5525                         net->fast_retran_loss_recovery = 0;
5526                 }
5527         }
5528
5529         /* Adjust and set the new rwnd value */
5530         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5531                 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5532                     asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
5533         }
5534         asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
5535             (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
5536         if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5537                 /* SWS sender side engages */
5538                 asoc->peers_rwnd = 0;
5539         }
5540         if (asoc->peers_rwnd > old_rwnd) {
5541                 win_probe_recovery = 1;
5542         }
5543         /*
5544          * Now we must setup so we have a timer up for anyone with
5545          * outstanding data.
5546          */
5547         done_once = 0;
5548 again:
5549         j = 0;
5550         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5551                 if (win_probe_recovery && (net->window_probe)) {
5552                         win_probe_recovered = 1;
5553                         /*-
5554                          * Find first chunk that was used with
5555                          * window probe and clear the event. Put
5556                          * it back into the send queue as if has
5557                          * not been sent.
5558                          */
5559                         TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5560                                 if (tp1->window_probe) {
5561                                         sctp_window_probe_recovery(stcb, asoc, net, tp1);
5562                                         break;
5563                                 }
5564                         }
5565                 }
5566                 if (net->flight_size) {
5567                         j++;
5568                         sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5569                             stcb->sctp_ep, stcb, net);
5570                         if (net->window_probe) {
5571                         }
5572                 } else {
5573                         if (net->window_probe) {
5574                                 /*
5575                                  * In window probes we must assure a timer
5576                                  * is still running there
5577                                  */
5578
5579                                 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5580                                         sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5581                                             stcb->sctp_ep, stcb, net);
5582
5583                                 }
5584                         } else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5585                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5586                                     stcb, net,
5587                                     SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5588                         }
5589                         if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
5590                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5591                                         SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5592                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5593                                             SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
5594                                 }
5595                         }
5596                 }
5597         }
5598         if ((j == 0) &&
5599             (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5600             (asoc->sent_queue_retran_cnt == 0) &&
5601             (win_probe_recovered == 0) &&
5602             (done_once == 0)) {
5603                 /*
5604                  * huh, this should not happen unless all packets are
5605                  * PR-SCTP and marked to skip of course.
5606                  */
5607                 if (sctp_fs_audit(asoc)) {
5608                         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5609                                 net->flight_size = 0;
5610                         }
5611                         asoc->total_flight = 0;
5612                         asoc->total_flight_count = 0;
5613                         asoc->sent_queue_retran_cnt = 0;
5614                         TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5615                                 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5616                                         sctp_flight_size_increase(tp1);
5617                                         sctp_total_flight_increase(stcb, tp1);
5618                                 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5619                                         asoc->sent_queue_retran_cnt++;
5620                                 }
5621                         }
5622                 }
5623                 done_once = 1;
5624                 goto again;
5625         }
5626         /* Fix up the a-p-a-p for future PR-SCTP sends */
5627         if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
5628                 asoc->advanced_peer_ack_point = cum_ack;
5629         }
5630         /* C2. try to further move advancedPeerAckPoint ahead */
5631         if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5632                 struct sctp_tmit_chunk *lchk;
5633                 uint32_t old_adv_peer_ack_point;
5634
5635                 old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
5636                 lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5637                 /* C3. See if we need to send a Fwd-TSN */
5638                 if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
5639                     MAX_TSN)) {
5640                         /*
5641                          * ISSUE with ECN, see FWD-TSN processing for notes
5642                          * on issues that will occur when the ECN NONCE
5643                          * stuff is put into SCTP for cross checking.
5644                          */
5645                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
5646                                 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
5647                                     0xee, cum_ack, asoc->advanced_peer_ack_point,
5648                                     old_adv_peer_ack_point);
5649                         }
5650                         if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
5651                             MAX_TSN)) {
5652                                 send_forward_tsn(stcb, asoc);
5653                                 /*
5654                                  * ECN Nonce: Disable Nonce Sum check when
5655                                  * FWD TSN is sent and store resync tsn
5656                                  */
5657                                 asoc->nonce_sum_check = 0;
5658                                 asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
5659                         } else if (lchk) {
5660                                 /* try to FR fwd-tsn's that get lost too */
5661                                 lchk->rec.data.fwd_tsn_cnt++;
5662                                 if (lchk->rec.data.fwd_tsn_cnt > 3) {
5663                                         send_forward_tsn(stcb, asoc);
5664                                         lchk->rec.data.fwd_tsn_cnt = 0;
5665                                 }
5666                         }
5667                 }
5668                 if (lchk) {
5669                         /* Assure a timer is up */
5670                         sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5671                             stcb->sctp_ep, stcb, lchk->whoTo);
5672                 }
5673         }
5674         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
5675                 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5676                     a_rwnd,
5677                     stcb->asoc.peers_rwnd,
5678                     stcb->asoc.total_flight,
5679                     stcb->asoc.total_output_queue_size);
5680         }
5681 }
5682
5683 void
5684 sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
5685     struct sctp_nets *netp, int *abort_flag)
5686 {
5687         /* Copy cum-ack */
5688         uint32_t cum_ack, a_rwnd;
5689
5690         cum_ack = ntohl(cp->cumulative_tsn_ack);
5691         /* Arrange so a_rwnd does NOT change */
5692         a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5693
5694         /* Now call the express sack handling */
5695         sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 0, abort_flag);
5696 }
5697
5698 static void
5699 sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5700     struct sctp_stream_in *strmin)
5701 {
5702         struct sctp_queued_to_read *ctl, *nctl;
5703         struct sctp_association *asoc;
5704         int tt;
5705
5706         /* EY -used to calculate nr_gap information */
5707         uint32_t nr_tsn, nr_gap;
5708
5709         asoc = &stcb->asoc;
5710         tt = strmin->last_sequence_delivered;
5711         /*
5712          * First deliver anything prior to and including the stream no that
5713          * came in
5714          */
5715         ctl = TAILQ_FIRST(&strmin->inqueue);
5716         while (ctl) {
5717                 nctl = TAILQ_NEXT(ctl, next);
5718                 if (compare_with_wrap(tt, ctl->sinfo_ssn, MAX_SEQ) ||
5719                     (tt == ctl->sinfo_ssn)) {
5720                         /* this is deliverable now */
5721                         TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5722                         /* subtract pending on streams */
5723                         asoc->size_on_all_streams -= ctl->length;
5724                         sctp_ucount_decr(asoc->cnt_on_all_streams);
5725                         /* deliver it to at least the delivery-q */
5726                         if (stcb->sctp_socket) {
5727                                 /* EY need the tsn info for calculating nr */
5728                                 nr_tsn = ctl->sinfo_tsn;
5729                                 sctp_add_to_readq(stcb->sctp_ep, stcb,
5730                                     ctl,
5731                                     &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
5732                                 /*
5733                                  * EY this is the chunk that should be
5734                                  * tagged nr gapped calculate the gap and
5735                                  * such then tag this TSN nr
5736                                  * chk->rec.data.TSN_seq
5737                                  */
5738                                 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
5739
5740                                         SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
5741                                         if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
5742                                             (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
5743                                                 /*
5744                                                  * EY These should never
5745                                                  * happen- explained before
5746                                                  */
5747                                         } else {
5748                                                 SCTP_TCB_LOCK_ASSERT(stcb);
5749                                                 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
5750                                                 SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
5751                                                 if (compare_with_wrap(nr_tsn,
5752                                                     asoc->highest_tsn_inside_nr_map,
5753                                                     MAX_TSN))
5754                                                         asoc->highest_tsn_inside_nr_map = nr_tsn;
5755                                         }
5756                                         if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, nr_gap))
5757                                                 /*
5758                                                  * printf("In
5759                                                  * sctp_kick_prsctp_reorder_q
5760                                                  * ueue(7): Something wrong,
5761                                                  * the TSN to be tagged"
5762                                                  * "\nas NR is not even in
5763                                                  * the mapping_array, or map
5764                                                  * and nr_map are
5765                                                  * inconsistent");
5766                                                  */
5767                                                 /*
5768                                                  * EY - not %100 sure about
5769                                                  * the lock thing, don't
5770                                                  * think its required
5771                                                  */
5772                                                 /*
5773                                                  * SCTP_TCB_LOCK_ASSERT(stcb)
5774                                                  * ;
5775                                                  */
5776                                         {
5777                                                 /*
5778                                                  * printf("\nCalculating an
5779                                                  * nr_gap!!\nmapping_array_si
5780                                                  * ze = %d
5781                                                  * nr_mapping_array_size =
5782                                                  * %d" "\nmapping_array_base
5783                                                  * = %d
5784                                                  * nr_mapping_array_base =
5785                                                  * %d\nhighest_tsn_inside_map
5786                                                  *  = %d"
5787                                                  * "highest_tsn_inside_nr_map
5788                                                  *  = %d\nTSN = %d nr_gap =
5789                                                  * %d",asoc->mapping_array_si
5790                                                  * ze,
5791                                                  * asoc->nr_mapping_array_siz
5792                                                  * e,
5793                                                  * asoc->mapping_array_base_t
5794                                                  * sn,
5795                                                  * asoc->nr_mapping_array_bas
5796                                                  * e_tsn,
5797                                                  * asoc->highest_tsn_inside_m
5798                                                  * ap,
5799                                                  * asoc->highest_tsn_inside_n
5800                                                  * r_map,tsn,nr_gap);
5801                                                  */
5802                                         }
5803                                 }
5804                         }
5805                 } else {
5806                         /* no more delivery now. */
5807                         break;
5808                 }
5809                 ctl = nctl;
5810         }
5811         /*
5812          * now we must deliver things in queue the normal way  if any are
5813          * now ready.
5814          */
5815         tt = strmin->last_sequence_delivered + 1;
5816         ctl = TAILQ_FIRST(&strmin->inqueue);
5817         while (ctl) {
5818                 nctl = TAILQ_NEXT(ctl, next);
5819                 if (tt == ctl->sinfo_ssn) {
5820                         /* this is deliverable now */
5821                         TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5822                         /* subtract pending on streams */
5823                         asoc->size_on_all_streams -= ctl->length;
5824                         sctp_ucount_decr(asoc->cnt_on_all_streams);
5825                         /* deliver it to at least the delivery-q */
5826                         strmin->last_sequence_delivered = ctl->sinfo_ssn;
5827                         if (stcb->sctp_socket) {
5828                                 /* EY */
5829                                 nr_tsn = ctl->sinfo_tsn;
5830                                 sctp_add_to_readq(stcb->sctp_ep, stcb,
5831                                     ctl,
5832                                     &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
5833                                 /*
5834                                  * EY this is the chunk that should be
5835                                  * tagged nr gapped calculate the gap and
5836                                  * such then tag this TSN nr
5837                                  * chk->rec.data.TSN_seq
5838                                  */
5839                                 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
5840                                         SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
5841                                         if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
5842                                             (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
5843                                                 /*
5844                                                  * EY These should never
5845                                                  * happen, explained before
5846                                                  */
5847                                         } else {
5848                                                 SCTP_TCB_LOCK_ASSERT(stcb);
5849                                                 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
5850                                                 SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
5851                                                 if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map,
5852                                                     MAX_TSN))
5853                                                         asoc->highest_tsn_inside_nr_map = nr_tsn;
5854                                         }
5855                                         if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, nr_gap))
5856                                                 /*
5857                                                  * printf("In
5858                                                  * sctp_kick_prsctp_reorder_q
5859                                                  * ueue(8): Something wrong,
5860                                                  * the TSN to be tagged"
5861                                                  * "\nas NR is not even in
5862                                                  * the mapping_array, or map
5863                                                  * and nr_map are
5864                                                  * inconsistent");
5865                                                  */
5866                                                 /*
5867                                                  * EY - not %100 sure about
5868                                                  * the lock thing, don't
5869                                                  * think its required
5870                                                  */
5871                                                 /*
5872                                                  * SCTP_TCB_LOCK_ASSERT(stcb)
5873                                                  * ;
5874                                                  */
5875                                         {
5876                                                 /*
5877                                                  * printf("\nCalculating an
5878                                                  * nr_gap!!\nmapping_array_si
5879                                                  * ze = %d
5880                                                  * nr_mapping_array_size =
5881                                                  * %d" "\nmapping_array_base
5882                                                  * = %d
5883                                                  * nr_mapping_array_base =
5884                                                  * %d\nhighest_tsn_inside_map
5885                                                  *  = %d"
5886                                                  * "highest_tsn_inside_nr_map
5887                                                  *  = %d\nTSN = %d nr_gap =
5888                                                  * %d",asoc->mapping_array_si
5889                                                  * ze,
5890                                                  * asoc->nr_mapping_array_siz
5891                                                  * e,
5892                                                  * asoc->mapping_array_base_t
5893                                                  * sn,
5894                                                  * asoc->nr_mapping_array_bas
5895                                                  * e_tsn,
5896                                                  * asoc->highest_tsn_inside_m
5897                                                  * ap,
5898                                                  * asoc->highest_tsn_inside_n
5899                                                  * r_map,tsn,nr_gap);
5900                                                  */
5901                                         }
5902                                 }
5903                         }
5904                         tt = strmin->last_sequence_delivered + 1;
5905                 } else {
5906                         break;
5907                 }
5908                 ctl = nctl;
5909         }
5910 }
5911
5912 static void
5913 sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
5914     struct sctp_association *asoc,
5915     uint16_t stream, uint16_t seq)
5916 {
5917         struct sctp_tmit_chunk *chk, *at;
5918
5919         if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5920                 /* For each one on here see if we need to toss it */
5921                 /*
5922                  * For now large messages held on the reasmqueue that are
5923                  * complete will be tossed too. We could in theory do more
5924                  * work to spin through and stop after dumping one msg aka
5925                  * seeing the start of a new msg at the head, and call the
5926                  * delivery function... to see if it can be delivered... But
5927                  * for now we just dump everything on the queue.
5928                  */
5929                 chk = TAILQ_FIRST(&asoc->reasmqueue);
5930                 while (chk) {
5931                         at = TAILQ_NEXT(chk, sctp_next);
5932                         /*
5933                          * Do not toss it if on a different stream or marked
5934                          * for unordered delivery in which case the stream
5935                          * sequence number has no meaning.
5936                          */
5937                         if ((chk->rec.data.stream_number != stream) ||
5938                             ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == SCTP_DATA_UNORDERED)) {
5939                                 chk = at;
5940                                 continue;
5941                         }
5942                         if (chk->rec.data.stream_seq == seq) {
5943                                 /* It needs to be tossed */
5944                                 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5945                                 if (compare_with_wrap(chk->rec.data.TSN_seq,
5946                                     asoc->tsn_last_delivered, MAX_TSN)) {
5947                                         asoc->tsn_last_delivered =
5948                                             chk->rec.data.TSN_seq;
5949                                         asoc->str_of_pdapi =
5950                                             chk->rec.data.stream_number;
5951                                         asoc->ssn_of_pdapi =
5952                                             chk->rec.data.stream_seq;
5953                                         asoc->fragment_flags =
5954                                             chk->rec.data.rcv_flags;
5955                                 }
5956                                 asoc->size_on_reasm_queue -= chk->send_size;
5957                                 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5958
5959                                 /* Clear up any stream problem */
5960                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
5961                                     SCTP_DATA_UNORDERED &&
5962                                     (compare_with_wrap(chk->rec.data.stream_seq,
5963                                     asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
5964                                     MAX_SEQ))) {
5965                                         /*
5966                                          * We must dump forward this streams
5967                                          * sequence number if the chunk is
5968                                          * not unordered that is being
5969                                          * skipped. There is a chance that
5970                                          * if the peer does not include the
5971                                          * last fragment in its FWD-TSN we
5972                                          * WILL have a problem here since
5973                                          * you would have a partial chunk in
5974                                          * queue that may not be
5975                                          * deliverable. Also if a Partial
5976                                          * delivery API as started the user
5977                                          * may get a partial chunk. The next
5978                                          * read returning a new chunk...
5979                                          * really ugly but I see no way
5980                                          * around it! Maybe a notify??
5981                                          */
5982                                         asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
5983                                             chk->rec.data.stream_seq;
5984                                 }
5985                                 if (chk->data) {
5986                                         sctp_m_freem(chk->data);
5987                                         chk->data = NULL;
5988                                 }
5989                                 sctp_free_a_chunk(stcb, chk);
5990                         } else if (compare_with_wrap(chk->rec.data.stream_seq, seq, MAX_SEQ)) {
5991                                 /*
5992                                  * If the stream_seq is > than the purging
5993                                  * one, we are done
5994                                  */
5995                                 break;
5996                         }
5997                         chk = at;
5998                 }
5999         }
6000 }
6001
6002
6003 void
6004 sctp_handle_forward_tsn(struct sctp_tcb *stcb,
6005     struct sctp_forward_tsn_chunk *fwd, int *abort_flag, struct mbuf *m, int offset)
6006 {
6007         /*
6008          * ISSUES that MUST be fixed for ECN! When we are the sender of the
6009          * forward TSN, when the SACK comes back that acknowledges the
6010          * FWD-TSN we must reset the NONCE sum to match correctly. This will
6011          * get quite tricky since we may have sent more data interveneing
6012          * and must carefully account for what the SACK says on the nonce
6013          * and any gaps that are reported. This work will NOT be done here,
6014          * but I note it here since it is really related to PR-SCTP and
6015          * FWD-TSN's
6016          */
6017
6018         /* The pr-sctp fwd tsn */
6019         /*
6020          * here we will perform all the data receiver side steps for
6021          * processing FwdTSN, as required in by pr-sctp draft:
6022          * 
6023          * Assume we get FwdTSN(x):
6024          * 
6025          * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
6026          * others we have 3) examine and update re-ordering queue on
6027          * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
6028          * report where we are.
6029          */
6030         struct sctp_association *asoc;
6031         uint32_t new_cum_tsn, gap;
6032         unsigned int i, fwd_sz, cumack_set_flag, m_size;
6033         uint32_t str_seq;
6034         struct sctp_stream_in *strm;
6035         struct sctp_tmit_chunk *chk, *at;
6036         struct sctp_queued_to_read *ctl, *sv;
6037
6038         cumack_set_flag = 0;
6039         asoc = &stcb->asoc;
6040         if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
6041                 SCTPDBG(SCTP_DEBUG_INDATA1,
6042                     "Bad size too small/big fwd-tsn\n");
6043                 return;
6044         }
6045         m_size = (stcb->asoc.mapping_array_size << 3);
6046         /*************************************************************/
6047         /* 1. Here we update local cumTSN and shift the bitmap array */
6048         /*************************************************************/
6049         new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
6050
6051         if (compare_with_wrap(asoc->cumulative_tsn, new_cum_tsn, MAX_TSN) ||
6052             asoc->cumulative_tsn == new_cum_tsn) {
6053                 /* Already got there ... */
6054                 return;
6055         }
6056         if (compare_with_wrap(new_cum_tsn, asoc->highest_tsn_inside_map,
6057             MAX_TSN)) {
6058                 asoc->highest_tsn_inside_map = new_cum_tsn;
6059                 /* EY nr_mapping_array version of the above */
6060                 /*
6061                  * if(SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) &&
6062                  * asoc->peer_supports_nr_sack)
6063                  */
6064                 asoc->highest_tsn_inside_nr_map = new_cum_tsn;
6065                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6066                         sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
6067                 }
6068         }
6069         /*
6070          * now we know the new TSN is more advanced, let's find the actual
6071          * gap
6072          */
6073         SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
6074         if (gap >= m_size) {
6075                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6076                         sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
6077                 }
6078                 if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
6079                         struct mbuf *oper;
6080
6081                         /*
6082                          * out of range (of single byte chunks in the rwnd I
6083                          * give out). This must be an attacker.
6084                          */
6085                         *abort_flag = 1;
6086                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
6087                             0, M_DONTWAIT, 1, MT_DATA);
6088                         if (oper) {
6089                                 struct sctp_paramhdr *ph;
6090                                 uint32_t *ippp;
6091
6092                                 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
6093                                     (sizeof(uint32_t) * 3);
6094                                 ph = mtod(oper, struct sctp_paramhdr *);
6095                                 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
6096                                 ph->param_length = htons(SCTP_BUF_LEN(oper));
6097                                 ippp = (uint32_t *) (ph + 1);
6098                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
6099                                 ippp++;
6100                                 *ippp = asoc->highest_tsn_inside_map;
6101                                 ippp++;
6102                                 *ippp = new_cum_tsn;
6103                         }
6104                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
6105                         sctp_abort_an_association(stcb->sctp_ep, stcb,
6106                             SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
6107                         return;
6108                 }
6109                 SCTP_STAT_INCR(sctps_fwdtsn_map_over);
6110                 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
6111                 cumack_set_flag = 1;
6112                 asoc->mapping_array_base_tsn = new_cum_tsn + 1;
6113                 asoc->cumulative_tsn = asoc->highest_tsn_inside_map = new_cum_tsn;
6114                 /* EY - nr_sack: nr_mapping_array version of the above */
6115                 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
6116                         memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.nr_mapping_array_size);
6117                         asoc->nr_mapping_array_base_tsn = new_cum_tsn + 1;
6118                         asoc->highest_tsn_inside_nr_map = new_cum_tsn;
6119                         if (asoc->nr_mapping_array_size != asoc->mapping_array_size) {
6120                                 /*
6121                                  * printf("IN sctp_handle_forward_tsn:
6122                                  * Something is wrong the size of" "map and
6123                                  * nr_map should be equal!")
6124                                  */ ;
6125                         }
6126                 }
6127                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6128                         sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
6129                 }
6130                 asoc->last_echo_tsn = asoc->highest_tsn_inside_map;
6131         } else {
6132                 SCTP_TCB_LOCK_ASSERT(stcb);
6133                 for (i = 0; i <= gap; i++) {
6134                         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack
6135                             && SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
6136                                 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
6137                         } else {
6138                                 SCTP_SET_TSN_PRESENT(asoc->mapping_array, i);
6139                         }
6140                 }
6141                 /*
6142                  * Now after marking all, slide thing forward but no sack
6143                  * please.
6144                  */
6145                 sctp_sack_check(stcb, 0, 0, abort_flag);
6146                 if (*abort_flag)
6147                         return;
6148         }
6149         /*************************************************************/
6150         /* 2. Clear up re-assembly queue                             */
6151         /*************************************************************/
6152         /*
6153          * First service it if pd-api is up, just in case we can progress it
6154          * forward
6155          */
6156         if (asoc->fragmented_delivery_inprogress) {
6157                 sctp_service_reassembly(stcb, asoc);
6158         }
6159         if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
6160                 /* For each one on here see if we need to toss it */
6161                 /*
6162                  * For now large messages held on the reasmqueue that are
6163                  * complete will be tossed too. We could in theory do more
6164                  * work to spin through and stop after dumping one msg aka
6165                  * seeing the start of a new msg at the head, and call the
6166                  * delivery function... to see if it can be delivered... But
6167                  * for now we just dump everything on the queue.
6168                  */
6169                 chk = TAILQ_FIRST(&asoc->reasmqueue);
6170                 while (chk) {
6171                         at = TAILQ_NEXT(chk, sctp_next);
6172                         if ((compare_with_wrap(new_cum_tsn,
6173                             chk->rec.data.TSN_seq, MAX_TSN)) ||
6174                             (new_cum_tsn == chk->rec.data.TSN_seq)) {
6175                                 /* It needs to be tossed */
6176                                 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
6177                                 if (compare_with_wrap(chk->rec.data.TSN_seq,
6178                                     asoc->tsn_last_delivered, MAX_TSN)) {
6179                                         asoc->tsn_last_delivered =
6180                                             chk->rec.data.TSN_seq;
6181                                         asoc->str_of_pdapi =
6182                                             chk->rec.data.stream_number;
6183                                         asoc->ssn_of_pdapi =
6184                                             chk->rec.data.stream_seq;
6185                                         asoc->fragment_flags =
6186                                             chk->rec.data.rcv_flags;
6187                                 }
6188                                 asoc->size_on_reasm_queue -= chk->send_size;
6189                                 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
6190
6191                                 /* Clear up any stream problem */
6192                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
6193                                     SCTP_DATA_UNORDERED &&
6194                                     (compare_with_wrap(chk->rec.data.stream_seq,
6195                                     asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
6196                                     MAX_SEQ))) {
6197                                         /*
6198                                          * We must dump forward this streams
6199                                          * sequence number if the chunk is
6200                                          * not unordered that is being
6201                                          * skipped. There is a chance that
6202                                          * if the peer does not include the
6203                                          * last fragment in its FWD-TSN we
6204                                          * WILL have a problem here since
6205                                          * you would have a partial chunk in
6206                                          * queue that may not be
6207                                          * deliverable. Also if a Partial
6208                                          * delivery API as started the user
6209                                          * may get a partial chunk. The next
6210                                          * read returning a new chunk...
6211                                          * really ugly but I see no way
6212                                          * around it! Maybe a notify??
6213                                          */
6214                                         asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
6215                                             chk->rec.data.stream_seq;
6216                                 }
6217                                 if (chk->data) {
6218                                         sctp_m_freem(chk->data);
6219                                         chk->data = NULL;
6220                                 }
6221                                 sctp_free_a_chunk(stcb, chk);
6222                         } else {
6223                                 /*
6224                                  * Ok we have gone beyond the end of the
6225                                  * fwd-tsn's mark.
6226                                  */
6227                                 break;
6228                         }
6229                         chk = at;
6230                 }
6231         }
6232         /*******************************************************/
6233         /* 3. Update the PR-stream re-ordering queues and fix  */
6234         /* delivery issues as needed.                       */
6235         /*******************************************************/
6236         fwd_sz -= sizeof(*fwd);
6237         if (m && fwd_sz) {
6238                 /* New method. */
6239                 unsigned int num_str;
6240                 struct sctp_strseq *stseq, strseqbuf;
6241
6242                 offset += sizeof(*fwd);
6243
6244                 SCTP_INP_READ_LOCK(stcb->sctp_ep);
6245                 num_str = fwd_sz / sizeof(struct sctp_strseq);
6246                 for (i = 0; i < num_str; i++) {
6247                         uint16_t st;
6248
6249                         stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
6250                             sizeof(struct sctp_strseq),
6251                             (uint8_t *) & strseqbuf);
6252                         offset += sizeof(struct sctp_strseq);
6253                         if (stseq == NULL) {
6254                                 break;
6255                         }
6256                         /* Convert */
6257                         st = ntohs(stseq->stream);
6258                         stseq->stream = st;
6259                         st = ntohs(stseq->sequence);
6260                         stseq->sequence = st;
6261
6262                         /* now process */
6263
6264                         /*
6265                          * Ok we now look for the stream/seq on the read
6266                          * queue where its not all delivered. If we find it
6267                          * we transmute the read entry into a PDI_ABORTED.
6268                          */
6269                         if (stseq->stream >= asoc->streamincnt) {
6270                                 /* screwed up streams, stop!  */
6271                                 break;
6272                         }
6273                         if ((asoc->str_of_pdapi == stseq->stream) &&
6274                             (asoc->ssn_of_pdapi == stseq->sequence)) {
6275                                 /*
6276                                  * If this is the one we were partially
6277                                  * delivering now then we no longer are.
6278                                  * Note this will change with the reassembly
6279                                  * re-write.
6280                                  */
6281                                 asoc->fragmented_delivery_inprogress = 0;
6282                         }
6283                         sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence);
6284                         TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
6285                                 if ((ctl->sinfo_stream == stseq->stream) &&
6286                                     (ctl->sinfo_ssn == stseq->sequence)) {
6287                                         str_seq = (stseq->stream << 16) | stseq->sequence;
6288                                         ctl->end_added = 1;
6289                                         ctl->pdapi_aborted = 1;
6290                                         sv = stcb->asoc.control_pdapi;
6291                                         stcb->asoc.control_pdapi = ctl;
6292                                         sctp_notify_partial_delivery_indication(stcb,
6293                                             SCTP_PARTIAL_DELIVERY_ABORTED,
6294                                             SCTP_HOLDS_LOCK,
6295                                             str_seq);
6296                                         stcb->asoc.control_pdapi = sv;
6297                                         break;
6298                                 } else if ((ctl->sinfo_stream == stseq->stream) &&
6299                                     (compare_with_wrap(ctl->sinfo_ssn, stseq->sequence, MAX_SEQ))) {
6300                                         /* We are past our victim SSN */
6301                                         break;
6302                                 }
6303                         }
6304                         strm = &asoc->strmin[stseq->stream];
6305                         if (compare_with_wrap(stseq->sequence,
6306                             strm->last_sequence_delivered, MAX_SEQ)) {
6307                                 /* Update the sequence number */
6308                                 strm->last_sequence_delivered =
6309                                     stseq->sequence;
6310                         }
6311                         /* now kick the stream the new way */
6312                         /* sa_ignore NO_NULL_CHK */
6313                         sctp_kick_prsctp_reorder_queue(stcb, strm);
6314                 }
6315                 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
6316         }
6317         if (TAILQ_FIRST(&asoc->reasmqueue)) {
6318                 /* now lets kick out and check for more fragmented delivery */
6319                 /* sa_ignore NO_NULL_CHK */
6320                 sctp_deliver_reasm_check(stcb, &stcb->asoc);
6321         }
6322 }
6323
6324 /* EY fully identical to sctp_express_handle_sack, duplicated for only naming convention */
6325 void
6326 sctp_express_handle_nr_sack(struct sctp_tcb *stcb, uint32_t cumack,
6327     uint32_t rwnd, int nonce_sum_flag, int *abort_now)
6328 {
6329         struct sctp_nets *net;
6330         struct sctp_association *asoc;
6331         struct sctp_tmit_chunk *tp1, *tp2;
6332         uint32_t old_rwnd;
6333         int win_probe_recovery = 0;
6334         int win_probe_recovered = 0;
6335         int j, done_once = 0;
6336
6337         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
6338                 sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
6339                     rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
6340         }
6341         SCTP_TCB_LOCK_ASSERT(stcb);
6342 #ifdef SCTP_ASOCLOG_OF_TSNS
6343         stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
6344         stcb->asoc.cumack_log_at++;
6345         if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
6346                 stcb->asoc.cumack_log_at = 0;
6347         }
6348 #endif
6349         asoc = &stcb->asoc;
6350         old_rwnd = asoc->peers_rwnd;
6351         if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
6352                 /* old ack */
6353                 return;
6354         } else if (asoc->last_acked_seq == cumack) {
6355                 /* Window update sack */
6356                 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
6357                     (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
6358                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6359                         /* SWS sender side engages */
6360                         asoc->peers_rwnd = 0;
6361                 }
6362                 if (asoc->peers_rwnd > old_rwnd) {
6363                         goto again;
6364                 }
6365                 return;
6366         }
6367         /* First setup for CC stuff */
6368         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6369                 net->prev_cwnd = net->cwnd;
6370                 net->net_ack = 0;
6371                 net->net_ack2 = 0;
6372
6373                 /*
6374                  * CMT: Reset CUC and Fast recovery algo variables before
6375                  * SACK processing
6376                  */
6377                 net->new_pseudo_cumack = 0;
6378                 net->will_exit_fast_recovery = 0;
6379         }
6380         if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
6381                 uint32_t send_s;
6382
6383                 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
6384                         tp1 = TAILQ_LAST(&asoc->sent_queue,
6385                             sctpchunk_listhead);
6386                         send_s = tp1->rec.data.TSN_seq + 1;
6387                 } else {
6388                         send_s = asoc->sending_seq;
6389                 }
6390                 if ((cumack == send_s) ||
6391                     compare_with_wrap(cumack, send_s, MAX_TSN)) {
6392 #ifndef INVARIANTS
6393                         struct mbuf *oper;
6394
6395 #endif
6396 #ifdef INVARIANTS
6397                         panic("Impossible sack 1");
6398 #else
6399                         *abort_now = 1;
6400                         /* XXX */
6401                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
6402                             0, M_DONTWAIT, 1, MT_DATA);
6403                         if (oper) {
6404                                 struct sctp_paramhdr *ph;
6405                                 uint32_t *ippp;
6406
6407                                 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
6408                                     sizeof(uint32_t);
6409                                 ph = mtod(oper, struct sctp_paramhdr *);
6410                                 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
6411                                 ph->param_length = htons(SCTP_BUF_LEN(oper));
6412                                 ippp = (uint32_t *) (ph + 1);
6413                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
6414                         }
6415                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
6416                         sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
6417                         return;
6418 #endif
6419                 }
6420         }
6421         asoc->this_sack_highest_gap = cumack;
6422         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
6423                 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
6424                     stcb->asoc.overall_error_count,
6425                     0,
6426                     SCTP_FROM_SCTP_INDATA,
6427                     __LINE__);
6428         }
6429         stcb->asoc.overall_error_count = 0;
6430         if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
6431                 /* process the new consecutive TSN first */
6432                 tp1 = TAILQ_FIRST(&asoc->sent_queue);
6433                 while (tp1) {
6434                         tp2 = TAILQ_NEXT(tp1, sctp_next);
6435                         if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
6436                             MAX_TSN) ||
6437                             cumack == tp1->rec.data.TSN_seq) {
6438                                 if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
6439                                         printf("Warning, an unsent is now acked?\n");
6440                                 }
6441                                 /*
6442                                  * ECN Nonce: Add the nonce to the sender's
6443                                  * nonce sum
6444                                  */
6445                                 asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
6446                                 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
6447                                         /*
6448                                          * If it is less than ACKED, it is
6449                                          * now no-longer in flight. Higher
6450                                          * values may occur during marking
6451                                          */
6452                                         if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6453                                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6454                                                         sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
6455                                                             tp1->whoTo->flight_size,
6456                                                             tp1->book_size,
6457                                                             (uintptr_t) tp1->whoTo,
6458                                                             tp1->rec.data.TSN_seq);
6459                                                 }
6460                                                 sctp_flight_size_decrease(tp1);
6461                                                 /* sa_ignore NO_NULL_CHK */
6462                                                 sctp_total_flight_decrease(stcb, tp1);
6463                                         }
6464                                         tp1->whoTo->net_ack += tp1->send_size;
6465                                         if (tp1->snd_count < 2) {
6466                                                 /*
6467                                                  * True non-retransmited
6468                                                  * chunk
6469                                                  */
6470                                                 tp1->whoTo->net_ack2 +=
6471                                                     tp1->send_size;
6472
6473                                                 /* update RTO too? */
6474                                                 if (tp1->do_rtt) {
6475                                                         tp1->whoTo->RTO =
6476                                                         /*
6477                                                          * sa_ignore
6478                                                          * NO_NULL_CHK
6479                                                          */
6480                                                             sctp_calculate_rto(stcb,
6481                                                             asoc, tp1->whoTo,
6482                                                             &tp1->sent_rcv_time,
6483                                                             sctp_align_safe_nocopy);
6484                                                         tp1->do_rtt = 0;
6485                                                 }
6486                                         }
6487                                         /*
6488                                          * CMT: CUCv2 algorithm. From the
6489                                          * cumack'd TSNs, for each TSN being
6490                                          * acked for the first time, set the
6491                                          * following variables for the
6492                                          * corresp destination.
6493                                          * new_pseudo_cumack will trigger a
6494                                          * cwnd update.
6495                                          * find_(rtx_)pseudo_cumack will
6496                                          * trigger search for the next
6497                                          * expected (rtx-)pseudo-cumack.
6498                                          */
6499                                         tp1->whoTo->new_pseudo_cumack = 1;
6500                                         tp1->whoTo->find_pseudo_cumack = 1;
6501                                         tp1->whoTo->find_rtx_pseudo_cumack = 1;
6502
6503                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
6504                                                 /* sa_ignore NO_NULL_CHK */
6505                                                 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
6506                                         }
6507                                 }
6508                                 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
6509                                         sctp_ucount_decr(asoc->sent_queue_retran_cnt);
6510                                 }
6511                                 if (tp1->rec.data.chunk_was_revoked) {
6512                                         /* deflate the cwnd */
6513                                         tp1->whoTo->cwnd -= tp1->book_size;
6514                                         tp1->rec.data.chunk_was_revoked = 0;
6515                                 }
6516                                 tp1->sent = SCTP_DATAGRAM_ACKED;
6517                                 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
6518                                 if (tp1->data) {
6519                                         /* sa_ignore NO_NULL_CHK */
6520                                         sctp_free_bufspace(stcb, asoc, tp1, 1);
6521                                         sctp_m_freem(tp1->data);
6522                                 }
6523                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
6524                                         sctp_log_sack(asoc->last_acked_seq,
6525                                             cumack,
6526                                             tp1->rec.data.TSN_seq,
6527                                             0,
6528                                             0,
6529                                             SCTP_LOG_FREE_SENT);
6530                                 }
6531                                 tp1->data = NULL;
6532                                 asoc->sent_queue_cnt--;
6533                                 sctp_free_a_chunk(stcb, tp1);
6534                                 tp1 = tp2;
6535                         } else {
6536                                 break;
6537                         }
6538                 }
6539
6540         }
6541         /* sa_ignore NO_NULL_CHK */
6542         if (stcb->sctp_socket) {
6543 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6544                 struct socket *so;
6545
6546 #endif
6547
6548                 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
6549                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
6550                         /* sa_ignore NO_NULL_CHK */
6551                         sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
6552                 }
6553 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6554                 so = SCTP_INP_SO(stcb->sctp_ep);
6555                 atomic_add_int(&stcb->asoc.refcnt, 1);
6556                 SCTP_TCB_UNLOCK(stcb);
6557                 SCTP_SOCKET_LOCK(so, 1);
6558                 SCTP_TCB_LOCK(stcb);
6559                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
6560                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6561                         /* assoc was freed while we were unlocked */
6562                         SCTP_SOCKET_UNLOCK(so, 1);
6563                         return;
6564                 }
6565 #endif
6566                 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
6567 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6568                 SCTP_SOCKET_UNLOCK(so, 1);
6569 #endif
6570         } else {
6571                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
6572                         sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
6573                 }
6574         }
6575
6576         /* JRS - Use the congestion control given in the CC module */
6577         if (asoc->last_acked_seq != cumack)
6578                 asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
6579
6580         asoc->last_acked_seq = cumack;
6581
6582         if (TAILQ_EMPTY(&asoc->sent_queue)) {
6583                 /* nothing left in-flight */
6584                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6585                         net->flight_size = 0;
6586                         net->partial_bytes_acked = 0;
6587                 }
6588                 asoc->total_flight = 0;
6589                 asoc->total_flight_count = 0;
6590         }
6591         /* Fix up the a-p-a-p for future PR-SCTP sends */
6592         if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
6593                 asoc->advanced_peer_ack_point = cumack;
6594         }
6595         /* ECN Nonce updates */
6596         if (asoc->ecn_nonce_allowed) {
6597                 if (asoc->nonce_sum_check) {
6598                         if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
6599                                 if (asoc->nonce_wait_for_ecne == 0) {
6600                                         struct sctp_tmit_chunk *lchk;
6601
6602                                         lchk = TAILQ_FIRST(&asoc->send_queue);
6603                                         asoc->nonce_wait_for_ecne = 1;
6604                                         if (lchk) {
6605                                                 asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
6606                                         } else {
6607                                                 asoc->nonce_wait_tsn = asoc->sending_seq;
6608                                         }
6609                                 } else {
6610                                         if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
6611                                             (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
6612                                                 /*
6613                                                  * Misbehaving peer. We need
6614                                                  * to react to this guy
6615                                                  */
6616                                                 asoc->ecn_allowed = 0;
6617                                                 asoc->ecn_nonce_allowed = 0;
6618                                         }
6619                                 }
6620                         }
6621                 } else {
6622                         /* See if Resynchronization Possible */
6623                         if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
6624                                 asoc->nonce_sum_check = 1;
6625                                 /*
6626                                  * now we must calculate what the base is.
6627                                  * We do this based on two things, we know
6628                                  * the total's for all the segments
6629                                  * gap-acked in the SACK (none), We also
6630                                  * know the SACK's nonce sum, its in
6631                                  * nonce_sum_flag. So we can build a truth
6632                                  * table to back-calculate the new value of
6633                                  * asoc->nonce_sum_expect_base:
6634                                  * 
6635                                  * SACK-flag-Value         Seg-Sums Base 0 0 0
6636                                  * 1                    0 1 0 1 1 1 1 0
6637                                  */
6638                                 asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
6639                         }
6640                 }
6641         }
6642         /* RWND update */
6643         asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
6644             (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
6645         if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6646                 /* SWS sender side engages */
6647                 asoc->peers_rwnd = 0;
6648         }
6649         if (asoc->peers_rwnd > old_rwnd) {
6650                 win_probe_recovery = 1;
6651         }
6652         /* Now assure a timer where data is queued at */
6653 again:
6654         j = 0;
6655         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6656                 int to_ticks;
6657
6658                 if (win_probe_recovery && (net->window_probe)) {
6659                         win_probe_recovered = 1;
6660                         /*
6661                          * Find first chunk that was used with window probe
6662                          * and clear the sent
6663                          */
6664                         /* sa_ignore FREED_MEMORY */
6665                         TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
6666                                 if (tp1->window_probe) {
6667                                         /* move back to data send queue */
6668                                         sctp_window_probe_recovery(stcb, asoc, net, tp1);
6669                                         break;
6670                                 }
6671                         }
6672                 }
6673                 if (net->RTO == 0) {
6674                         to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
6675                 } else {
6676                         to_ticks = MSEC_TO_TICKS(net->RTO);
6677                 }
6678                 if (net->flight_size) {
6679
6680                         j++;
6681                         (void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
6682                             sctp_timeout_handler, &net->rxt_timer);
6683                         if (net->window_probe) {
6684                                 net->window_probe = 0;
6685                         }
6686                 } else {
6687                         if (net->window_probe) {
6688                                 /*
6689                                  * In window probes we must assure a timer
6690                                  * is still running there
6691                                  */
6692                                 net->window_probe = 0;
6693                                 (void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
6694                                     sctp_timeout_handler, &net->rxt_timer);
6695                         } else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
6696                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
6697                                     stcb, net,
6698                                     SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
6699                         }
6700                         if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
6701                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
6702                                         SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
6703                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
6704                                             SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
6705                                 }
6706                         }
6707                 }
6708         }
6709         if ((j == 0) &&
6710             (!TAILQ_EMPTY(&asoc->sent_queue)) &&
6711             (asoc->sent_queue_retran_cnt == 0) &&
6712             (win_probe_recovered == 0) &&
6713             (done_once == 0)) {
6714                 /*
6715                  * huh, this should not happen unless all packets are
6716                  * PR-SCTP and marked to skip of course.
6717                  */
6718                 if (sctp_fs_audit(asoc)) {
6719                         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6720                                 net->flight_size = 0;
6721                         }
6722                         asoc->total_flight = 0;
6723                         asoc->total_flight_count = 0;
6724                         asoc->sent_queue_retran_cnt = 0;
6725                         TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
6726                                 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6727                                         sctp_flight_size_increase(tp1);
6728                                         sctp_total_flight_increase(stcb, tp1);
6729                                 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
6730                                         asoc->sent_queue_retran_cnt++;
6731                                 }
6732                         }
6733                 }
6734                 done_once = 1;
6735                 goto again;
6736         }
6737         /**********************************/
6738         /* Now what about shutdown issues */
6739         /**********************************/
6740         if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
6741                 /* nothing left on sendqueue.. consider done */
6742                 /* clean up */
6743                 if ((asoc->stream_queue_cnt == 1) &&
6744                     ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
6745                     (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
6746                     (asoc->locked_on_sending)
6747                     ) {
6748                         struct sctp_stream_queue_pending *sp;
6749
6750                         /*
6751                          * I may be in a state where we got all across.. but
6752                          * cannot write more due to a shutdown... we abort
6753                          * since the user did not indicate EOR in this case.
6754                          * The sp will be cleaned during free of the asoc.
6755                          */
6756                         sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
6757                             sctp_streamhead);
6758                         if ((sp) && (sp->length == 0)) {
6759                                 /* Let cleanup code purge it */
6760                                 if (sp->msg_is_complete) {
6761                                         asoc->stream_queue_cnt--;
6762                                 } else {
6763                                         asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6764                                         asoc->locked_on_sending = NULL;
6765                                         asoc->stream_queue_cnt--;
6766                                 }
6767                         }
6768                 }
6769                 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
6770                     (asoc->stream_queue_cnt == 0)) {
6771                         if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
6772                                 /* Need to abort here */
6773                                 struct mbuf *oper;
6774
6775                 abort_out_now:
6776                                 *abort_now = 1;
6777                                 /* XXX */
6778                                 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
6779                                     0, M_DONTWAIT, 1, MT_DATA);
6780                                 if (oper) {
6781                                         struct sctp_paramhdr *ph;
6782                                         uint32_t *ippp;
6783
6784                                         SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
6785                                             sizeof(uint32_t);
6786                                         ph = mtod(oper, struct sctp_paramhdr *);
6787                                         ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6788                                         ph->param_length = htons(SCTP_BUF_LEN(oper));
6789                                         ippp = (uint32_t *) (ph + 1);
6790                                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
6791                                 }
6792                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
6793                                 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
6794                         } else {
6795                                 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
6796                                     (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
6797                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6798                                 }
6799                                 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6800                                 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6801                                 sctp_stop_timers_for_shutdown(stcb);
6802                                 sctp_send_shutdown(stcb,
6803                                     stcb->asoc.primary_destination);
6804                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
6805                                     stcb->sctp_ep, stcb, asoc->primary_destination);
6806                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
6807                                     stcb->sctp_ep, stcb, asoc->primary_destination);
6808                         }
6809                 } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
6810                     (asoc->stream_queue_cnt == 0)) {
6811                         if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
6812                                 goto abort_out_now;
6813                         }
6814                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6815                         SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
6816                         SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6817                         sctp_send_shutdown_ack(stcb,
6818                             stcb->asoc.primary_destination);
6819
6820                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
6821                             stcb->sctp_ep, stcb, asoc->primary_destination);
6822                 }
6823         }
6824         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
6825                 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
6826                     rwnd,
6827                     stcb->asoc.peers_rwnd,
6828                     stcb->asoc.total_flight,
6829                     stcb->asoc.total_output_queue_size);
6830         }
6831 }
6832
6833 /* EY! nr_sack version of sctp_handle_segments, nr-gapped TSNs get removed from RtxQ in this method*/
6834 static void
6835 sctp_handle_nr_sack_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
6836     struct sctp_nr_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
6837     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
6838     uint32_t num_seg, uint32_t num_nr_seg, int *ecn_seg_sums)
6839 {
6840         /************************************************/
6841         /* process fragments and update sendqueue        */
6842         /************************************************/
6843         struct sctp_nr_sack *nr_sack;
6844         struct sctp_gap_ack_block *frag, block;
6845         struct sctp_nr_gap_ack_block *nr_frag, nr_block;
6846         struct sctp_tmit_chunk *tp1;
6847         uint32_t i, j;
6848         int wake_him = 0;
6849         uint32_t theTSN;
6850         int num_frs = 0;
6851
6852         uint16_t frag_strt, frag_end, primary_flag_set;
6853         uint16_t nr_frag_strt, nr_frag_end;
6854
6855         uint32_t last_frag_high;
6856         uint32_t last_nr_frag_high;
6857
6858         /*
6859          * @@@ JRI : TODO: This flag is not used anywhere .. remove?
6860          */
6861         if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
6862                 primary_flag_set = 1;
6863         } else {
6864                 primary_flag_set = 0;
6865         }
6866         nr_sack = &ch->nr_sack;
6867
6868         /*
6869          * EY! - I will process nr_gaps similarly,by going to this position
6870          * again if All bit is set
6871          */
6872         frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
6873             sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
6874         *offset += sizeof(block);
6875         if (frag == NULL) {
6876                 return;
6877         }
6878         tp1 = NULL;
6879         last_frag_high = 0;
6880         for (i = 0; i < num_seg; i++) {
6881                 frag_strt = ntohs(frag->start);
6882                 frag_end = ntohs(frag->end);
6883                 /* some sanity checks on the fargment offsets */
6884                 if (frag_strt > frag_end) {
6885                         /* this one is malformed, skip */
6886                         frag++;
6887                         continue;
6888                 }
6889                 if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
6890                     MAX_TSN))
6891                         *biggest_tsn_acked = frag_end + last_tsn;
6892
6893                 /* mark acked dgs and find out the highestTSN being acked */
6894                 if (tp1 == NULL) {
6895                         tp1 = TAILQ_FIRST(&asoc->sent_queue);
6896
6897                         /* save the locations of the last frags */
6898                         last_frag_high = frag_end + last_tsn;
6899                 } else {
6900                         /*
6901                          * now lets see if we need to reset the queue due to
6902                          * a out-of-order SACK fragment
6903                          */
6904                         if (compare_with_wrap(frag_strt + last_tsn,
6905                             last_frag_high, MAX_TSN)) {
6906                                 /*
6907                                  * if the new frag starts after the last TSN
6908                                  * frag covered, we are ok and this one is
6909                                  * beyond the last one
6910                                  */
6911                                 ;
6912                         } else {
6913                                 /*
6914                                  * ok, they have reset us, so we need to
6915                                  * reset the queue this will cause extra
6916                                  * hunting but hey, they chose the
6917                                  * performance hit when they failed to order
6918                                  * there gaps..
6919                                  */
6920                                 tp1 = TAILQ_FIRST(&asoc->sent_queue);
6921                         }
6922                         last_frag_high = frag_end + last_tsn;
6923                 }
6924                 for (j = frag_strt; j <= frag_end; j++) {
6925                         theTSN = j + last_tsn;
6926                         while (tp1) {
6927                                 if (tp1->rec.data.doing_fast_retransmit)
6928                                         num_frs++;
6929
6930                                 /*
6931                                  * CMT: CUCv2 algorithm. For each TSN being
6932                                  * processed from the sent queue, track the
6933                                  * next expected pseudo-cumack, or
6934                                  * rtx_pseudo_cumack, if required. Separate
6935                                  * cumack trackers for first transmissions,
6936                                  * and retransmissions.
6937                                  */
6938                                 if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
6939                                     (tp1->snd_count == 1)) {
6940                                         tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
6941                                         tp1->whoTo->find_pseudo_cumack = 0;
6942                                 }
6943                                 if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
6944                                     (tp1->snd_count > 1)) {
6945                                         tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
6946                                         tp1->whoTo->find_rtx_pseudo_cumack = 0;
6947                                 }
6948                                 if (tp1->rec.data.TSN_seq == theTSN) {
6949                                         if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
6950                                                 /*
6951                                                  * must be held until
6952                                                  * cum-ack passes
6953                                                  */
6954                                                 /*
6955                                                  * ECN Nonce: Add the nonce
6956                                                  * value to the sender's
6957                                                  * nonce sum
6958                                                  */
6959                                                 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6960                                                         /*-
6961                                                          * If it is less than RESEND, it is
6962                                                          * now no-longer in flight.
6963                                                          * Higher values may already be set
6964                                                          * via previous Gap Ack Blocks...
6965                                                          * i.e. ACKED or RESEND.
6966                                                          */
6967                                                         if (compare_with_wrap(tp1->rec.data.TSN_seq,
6968                                                             *biggest_newly_acked_tsn, MAX_TSN)) {
6969                                                                 *biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
6970                                                         }
6971                                                         /*
6972                                                          * CMT: SFR algo
6973                                                          * (and HTNA) - set
6974                                                          * saw_newack to 1
6975                                                          * for dest being
6976                                                          * newly acked.
6977                                                          * update
6978                                                          * this_sack_highest_
6979                                                          * newack if
6980                                                          * appropriate.
6981                                                          */
6982                                                         if (tp1->rec.data.chunk_was_revoked == 0)
6983                                                                 tp1->whoTo->saw_newack = 1;
6984
6985                                                         if (compare_with_wrap(tp1->rec.data.TSN_seq,
6986                                                             tp1->whoTo->this_sack_highest_newack,
6987                                                             MAX_TSN)) {
6988                                                                 tp1->whoTo->this_sack_highest_newack =
6989                                                                     tp1->rec.data.TSN_seq;
6990                                                         }
6991                                                         /*
6992                                                          * CMT DAC algo:
6993                                                          * also update
6994                                                          * this_sack_lowest_n
6995                                                          * ewack
6996                                                          */
6997                                                         if (*this_sack_lowest_newack == 0) {
6998                                                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
6999                                                                         sctp_log_sack(*this_sack_lowest_newack,
7000                                                                             last_tsn,
7001                                                                             tp1->rec.data.TSN_seq,
7002                                                                             0,
7003                                                                             0,
7004                                                                             SCTP_LOG_TSN_ACKED);
7005                                                                 }
7006                                                                 *this_sack_lowest_newack = tp1->rec.data.TSN_seq;
7007                                                         }
7008                                                         /*
7009                                                          * CMT: CUCv2
7010                                                          * algorithm. If
7011                                                          * (rtx-)pseudo-cumac
7012                                                          * k for corresp
7013                                                          * dest is being
7014                                                          * acked, then we
7015                                                          * have a new
7016                                                          * (rtx-)pseudo-cumac
7017                                                          * k. Set
7018                                                          * new_(rtx_)pseudo_c
7019                                                          * umack to TRUE so
7020                                                          * that the cwnd for
7021                                                          * this dest can be
7022                                                          * updated. Also
7023                                                          * trigger search
7024                                                          * for the next
7025                                                          * expected
7026                                                          * (rtx-)pseudo-cumac
7027                                                          * k. Separate
7028                                                          * pseudo_cumack
7029                                                          * trackers for
7030                                                          * first
7031                                                          * transmissions and
7032                                                          * retransmissions.
7033                                                          */
7034                                                         if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
7035                                                                 if (tp1->rec.data.chunk_was_revoked == 0) {
7036                                                                         tp1->whoTo->new_pseudo_cumack = 1;
7037                                                                 }
7038                                                                 tp1->whoTo->find_pseudo_cumack = 1;
7039                                                         }
7040                                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7041                                                                 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
7042                                                         }
7043                                                         if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
7044                                                                 if (tp1->rec.data.chunk_was_revoked == 0) {
7045                                                                         tp1->whoTo->new_pseudo_cumack = 1;
7046                                                                 }
7047                                                                 tp1->whoTo->find_rtx_pseudo_cumack = 1;
7048                                                         }
7049                                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7050                                                                 sctp_log_sack(*biggest_newly_acked_tsn,
7051                                                                     last_tsn,
7052                                                                     tp1->rec.data.TSN_seq,
7053                                                                     frag_strt,
7054                                                                     frag_end,
7055                                                                     SCTP_LOG_TSN_ACKED);
7056                                                         }
7057                                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7058                                                                 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
7059                                                                     tp1->whoTo->flight_size,
7060                                                                     tp1->book_size,
7061                                                                     (uintptr_t) tp1->whoTo,
7062                                                                     tp1->rec.data.TSN_seq);
7063                                                         }
7064                                                         sctp_flight_size_decrease(tp1);
7065                                                         sctp_total_flight_decrease(stcb, tp1);
7066
7067                                                         tp1->whoTo->net_ack += tp1->send_size;
7068                                                         if (tp1->snd_count < 2) {
7069                                                                 /*
7070                                                                  * True
7071                                                                  * non-retran
7072                                                                  * smited
7073                                                                  * chunk
7074                                                                  */
7075                                                                 tp1->whoTo->net_ack2 += tp1->send_size;
7076
7077                                                                 /*
7078                                                                  * update
7079                                                                  * RTO too ?
7080                                                                  */
7081                                                                 if (tp1->do_rtt) {
7082                                                                         tp1->whoTo->RTO =
7083                                                                             sctp_calculate_rto(stcb,
7084                                                                             asoc,
7085                                                                             tp1->whoTo,
7086                                                                             &tp1->sent_rcv_time,
7087                                                                             sctp_align_safe_nocopy);
7088                                                                         tp1->do_rtt = 0;
7089                                                                 }
7090                                                         }
7091                                                 }
7092                                                 if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
7093                                                         (*ecn_seg_sums) += tp1->rec.data.ect_nonce;
7094                                                         (*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
7095                                                         if (compare_with_wrap(tp1->rec.data.TSN_seq,
7096                                                             asoc->this_sack_highest_gap,
7097                                                             MAX_TSN)) {
7098                                                                 asoc->this_sack_highest_gap =
7099                                                                     tp1->rec.data.TSN_seq;
7100                                                         }
7101                                                         if (tp1->sent == SCTP_DATAGRAM_RESEND) {
7102                                                                 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
7103 #ifdef SCTP_AUDITING_ENABLED
7104                                                                 sctp_audit_log(0xB2,
7105                                                                     (asoc->sent_queue_retran_cnt & 0x000000ff));
7106 #endif
7107                                                         }
7108                                                 }
7109                                                 /*
7110                                                  * All chunks NOT UNSENT
7111                                                  * fall through here and are
7112                                                  * marked
7113                                                  */
7114                                                 if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
7115                                                         tp1->sent = SCTP_DATAGRAM_NR_MARKED;
7116                                                 if (tp1->rec.data.chunk_was_revoked) {
7117                                                         /* deflate the cwnd */
7118                                                         tp1->whoTo->cwnd -= tp1->book_size;
7119                                                         tp1->rec.data.chunk_was_revoked = 0;
7120                                                 }
7121                                         }
7122                                         break;
7123                                 }       /* if (tp1->TSN_seq == theTSN) */
7124                                 if (compare_with_wrap(tp1->rec.data.TSN_seq, theTSN,
7125                                     MAX_TSN))
7126                                         break;
7127
7128                                 tp1 = TAILQ_NEXT(tp1, sctp_next);
7129                         }       /* end while (tp1) */
7130                 }               /* end for (j = fragStart */
7131                 frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
7132                     sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
7133                 *offset += sizeof(block);
7134                 if (frag == NULL) {
7135                         break;
7136                 }
7137         }
7138
7139         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
7140                 if (num_frs)
7141                         sctp_log_fr(*biggest_tsn_acked,
7142                             *biggest_newly_acked_tsn,
7143                             last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
7144         }
7145         nr_frag = (struct sctp_nr_gap_ack_block *)sctp_m_getptr(m, *offset,
7146             sizeof(struct sctp_nr_gap_ack_block), (uint8_t *) & nr_block);
7147         *offset += sizeof(nr_block);
7148
7149
7150
7151         if (nr_frag == NULL) {
7152                 return;
7153         }
7154         tp1 = NULL;
7155         last_nr_frag_high = 0;
7156
7157         for (i = 0; i < num_nr_seg; i++) {
7158
7159                 nr_frag_strt = ntohs(nr_frag->start);
7160                 nr_frag_end = ntohs(nr_frag->end);
7161
7162                 /* some sanity checks on the nr fargment offsets */
7163                 if (nr_frag_strt > nr_frag_end) {
7164                         /* this one is malformed, skip */
7165                         nr_frag++;
7166                         continue;
7167                 }
7168                 /* mark acked dgs and find out the highestTSN being acked */
7169                 if (tp1 == NULL) {
7170                         tp1 = TAILQ_FIRST(&asoc->sent_queue);
7171
7172                         /* save the locations of the last frags */
7173                         last_nr_frag_high = nr_frag_end + last_tsn;
7174                 } else {
7175                         /*
7176                          * now lets see if we need to reset the queue due to
7177                          * a out-of-order SACK fragment
7178                          */
7179                         if (compare_with_wrap(nr_frag_strt + last_tsn,
7180                             last_nr_frag_high, MAX_TSN)) {
7181                                 /*
7182                                  * if the new frag starts after the last TSN
7183                                  * frag covered, we are ok and this one is
7184                                  * beyond the last one
7185                                  */
7186                                 ;
7187                         } else {
7188                                 /*
7189                                  * ok, they have reset us, so we need to
7190                                  * reset the queue this will cause extra
7191                                  * hunting but hey, they chose the
7192                                  * performance hit when they failed to order
7193                                  * there gaps..
7194                                  */
7195                                 tp1 = TAILQ_FIRST(&asoc->sent_queue);
7196                         }
7197                         last_nr_frag_high = nr_frag_end + last_tsn;
7198                 }
7199
7200                 for (j = nr_frag_strt + last_tsn; (compare_with_wrap((nr_frag_end + last_tsn), j, MAX_TSN)); j++) {
7201                         while (tp1) {
7202                                 if (tp1->rec.data.TSN_seq == j) {
7203                                         if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
7204                                                 if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
7205                                                         tp1->sent = SCTP_DATAGRAM_NR_MARKED;
7206                                                 /*
7207                                                  * TAILQ_REMOVE(&asoc->sent_q
7208                                                  * ueue, tp1, sctp_next);
7209                                                  */
7210                                                 if (tp1->data) {
7211                                                         /*
7212                                                          * sa_ignore
7213                                                          * NO_NULL_CHK
7214                                                          */
7215                                                         sctp_free_bufspace(stcb, asoc, tp1, 1);
7216                                                         sctp_m_freem(tp1->data);
7217                                                 }
7218                                                 tp1->data = NULL;
7219                                                 /* asoc->sent_queue_cnt--; */
7220                                                 /*
7221                                                  * sctp_free_a_chunk(stcb,
7222                                                  * tp1);
7223                                                  */
7224                                                 wake_him++;
7225                                         }
7226                                         break;
7227                                 }       /* if (tp1->TSN_seq == j) */
7228                                 if (compare_with_wrap(tp1->rec.data.TSN_seq, j,
7229                                     MAX_TSN))
7230                                         break;
7231                                 tp1 = TAILQ_NEXT(tp1, sctp_next);
7232                         }       /* end while (tp1) */
7233
7234                 }               /* end for (j = nrFragStart */
7235
7236                 nr_frag = (struct sctp_nr_gap_ack_block *)sctp_m_getptr(m, *offset,
7237                     sizeof(struct sctp_nr_gap_ack_block), (uint8_t *) & nr_block);
7238                 *offset += sizeof(nr_block);
7239                 if (nr_frag == NULL) {
7240                         break;
7241                 }
7242         }
7243
7244         /*
7245          * EY- wake up the socket if things have been removed from the sent
7246          * queue
7247          */
7248         if ((wake_him) && (stcb->sctp_socket)) {
7249 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7250                 struct socket *so;
7251
7252 #endif
7253                 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
7254                 /*
7255                  * if (SCTP_BASE_SYSCTL(sctp_logging_level) &
7256                  * SCTP_WAKE_LOGGING_ENABLE) { sctp_wakeup_log(stcb,
7257                  * cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);}
7258                  */
7259 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7260                 so = SCTP_INP_SO(stcb->sctp_ep);
7261                 atomic_add_int(&stcb->asoc.refcnt, 1);
7262                 SCTP_TCB_UNLOCK(stcb);
7263                 SCTP_SOCKET_LOCK(so, 1);
7264                 SCTP_TCB_LOCK(stcb);
7265                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
7266                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
7267                         /* assoc was freed while we were unlocked */
7268                         SCTP_SOCKET_UNLOCK(so, 1);
7269                         return;
7270                 }
7271 #endif
7272                 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
7273 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7274                 SCTP_SOCKET_UNLOCK(so, 1);
7275 #endif
7276         }                       /* else { if
7277                                  * (SCTP_BASE_SYSCTL(sctp_logging_level) &
7278                                  * SCTP_WAKE_LOGGING_ENABLE) {
7279                                  * sctp_wakeup_log(stcb, cum_ack, wake_him,
7280                                  * SCTP_NOWAKE_FROM_SACK); } } */
7281 }
7282
7283 /* EY- nr_sack */
7284 /* Identifies the non-renegable tsns that are revoked*/
7285 static void
7286 sctp_check_for_nr_revoked(struct sctp_tcb *stcb,
7287     struct sctp_association *asoc, uint32_t cumack,
7288     u_long biggest_tsn_acked)
7289 {
7290         struct sctp_tmit_chunk *tp1;
7291
7292         tp1 = TAILQ_FIRST(&asoc->sent_queue);
7293         while (tp1) {
7294                 if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
7295                     MAX_TSN)) {
7296                         /*
7297                          * ok this guy is either ACK or MARKED. If it is
7298                          * ACKED it has been previously acked but not this
7299                          * time i.e. revoked.  If it is MARKED it was ACK'ed
7300                          * again.
7301                          */
7302                         if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
7303                             MAX_TSN))
7304                                 break;
7305
7306
7307                         if (tp1->sent == SCTP_DATAGRAM_NR_ACKED) {
7308                                 /*
7309                                  * EY! a non-renegable TSN is revoked, need
7310                                  * to abort the association
7311                                  */
7312                                 /*
7313                                  * EY TODO: put in the code to abort the
7314                                  * assoc.
7315                                  */
7316                                 return;
7317                         } else if (tp1->sent == SCTP_DATAGRAM_NR_MARKED) {
7318                                 /* it has been re-acked in this SACK */
7319                                 tp1->sent = SCTP_DATAGRAM_NR_ACKED;
7320                         }
7321                 }
7322                 if (tp1->sent == SCTP_DATAGRAM_UNSENT)
7323                         break;
7324                 tp1 = TAILQ_NEXT(tp1, sctp_next);
7325         }
7326 }
7327
7328 /* EY! nr_sack version of sctp_handle_sack, nr_gap_ack processing should be added to this method*/
7329 void
7330 sctp_handle_nr_sack(struct mbuf *m, int offset,
7331     struct sctp_nr_sack_chunk *ch, struct sctp_tcb *stcb,
7332     struct sctp_nets *net_from, int *abort_now, int nr_sack_len, uint32_t rwnd)
7333 {
7334         struct sctp_association *asoc;
7335
7336         /* EY sack */
7337         struct sctp_nr_sack *nr_sack;
7338         struct sctp_tmit_chunk *tp1, *tp2;
7339         uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
7340                  this_sack_lowest_newack;
7341         uint32_t sav_cum_ack;
7342
7343         /* EY num_seg */
7344         uint16_t num_seg, num_nr_seg, num_dup;
7345         uint16_t wake_him = 0;
7346         unsigned int nr_sack_length;
7347         uint32_t send_s = 0;
7348         long j;
7349         int accum_moved = 0;
7350         int will_exit_fast_recovery = 0;
7351         uint32_t a_rwnd, old_rwnd;
7352         int win_probe_recovery = 0;
7353         int win_probe_recovered = 0;
7354         struct sctp_nets *net = NULL;
7355         int nonce_sum_flag, ecn_seg_sums = 0;
7356         int done_once;
7357         uint8_t reneged_all = 0;
7358         uint8_t cmt_dac_flag;
7359
7360         /*
7361          * we take any chance we can to service our queues since we cannot
7362          * get awoken when the socket is read from :<
7363          */
7364         /*
7365          * Now perform the actual SACK handling: 1) Verify that it is not an
7366          * old sack, if so discard. 2) If there is nothing left in the send
7367          * queue (cum-ack is equal to last acked) then you have a duplicate
7368          * too, update any rwnd change and verify no timers are running.
7369          * then return. 3) Process any new consequtive data i.e. cum-ack
7370          * moved process these first and note that it moved. 4) Process any
7371          * sack blocks. 5) Drop any acked from the queue. 6) Check for any
7372          * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
7373          * sync up flightsizes and things, stop all timers and also check
7374          * for shutdown_pending state. If so then go ahead and send off the
7375          * shutdown. If in shutdown recv, send off the shutdown-ack and
7376          * start that timer, Ret. 9) Strike any non-acked things and do FR
7377          * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
7378          * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
7379          * if in shutdown_recv state.
7380          */
7381         SCTP_TCB_LOCK_ASSERT(stcb);
7382         nr_sack = &ch->nr_sack;
7383         /* CMT DAC algo */
7384         this_sack_lowest_newack = 0;
7385         j = 0;
7386         nr_sack_length = (unsigned int)nr_sack_len;
7387         /* ECN Nonce */
7388         SCTP_STAT_INCR(sctps_slowpath_sack);
7389         nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
7390         cum_ack = last_tsn = ntohl(nr_sack->cum_tsn_ack);
7391 #ifdef SCTP_ASOCLOG_OF_TSNS
7392         stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
7393         stcb->asoc.cumack_log_at++;
7394         if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
7395                 stcb->asoc.cumack_log_at = 0;
7396         }
7397 #endif
7398         num_seg = ntohs(nr_sack->num_gap_ack_blks);
7399         num_nr_seg = ntohs(nr_sack->num_nr_gap_ack_blks);
7400         a_rwnd = rwnd;
7401
7402         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
7403                 sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
7404                     rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
7405         }
7406         /* CMT DAC algo */
7407         cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
7408         num_dup = ntohs(nr_sack->num_dup_tsns);
7409
7410         old_rwnd = stcb->asoc.peers_rwnd;
7411         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
7412                 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
7413                     stcb->asoc.overall_error_count,
7414                     0,
7415                     SCTP_FROM_SCTP_INDATA,
7416                     __LINE__);
7417         }
7418         stcb->asoc.overall_error_count = 0;
7419         asoc = &stcb->asoc;
7420         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7421                 sctp_log_sack(asoc->last_acked_seq,
7422                     cum_ack,
7423                     0,
7424                     num_seg,
7425                     num_dup,
7426                     SCTP_LOG_NEW_SACK);
7427         }
7428         if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
7429                 int off_to_dup, iii;
7430                 uint32_t *dupdata, dblock;
7431
7432                 off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) +
7433                     (num_nr_seg * sizeof(struct sctp_nr_gap_ack_block)) + sizeof(struct sctp_nr_sack_chunk);
7434                 if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= nr_sack_length) {
7435                         dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
7436                             sizeof(uint32_t), (uint8_t *) & dblock);
7437                         off_to_dup += sizeof(uint32_t);
7438                         if (dupdata) {
7439                                 for (iii = 0; iii < num_dup; iii++) {
7440                                         sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
7441                                         dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
7442                                             sizeof(uint32_t), (uint8_t *) & dblock);
7443                                         if (dupdata == NULL)
7444                                                 break;
7445                                         off_to_dup += sizeof(uint32_t);
7446                                 }
7447                         }
7448                 } else {
7449                         SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d nr_sack_len:%d num gaps:%d num nr_gaps:%d\n",
7450                             off_to_dup, num_dup, nr_sack_length, num_seg, num_nr_seg);
7451                 }
7452         }
7453         if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
7454                 /* reality check */
7455                 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
7456                         tp1 = TAILQ_LAST(&asoc->sent_queue,
7457                             sctpchunk_listhead);
7458                         send_s = tp1->rec.data.TSN_seq + 1;
7459                 } else {
7460                         send_s = asoc->sending_seq;
7461                 }
7462                 if (cum_ack == send_s ||
7463                     compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
7464 #ifndef INVARIANTS
7465                         struct mbuf *oper;
7466
7467 #endif
7468 #ifdef INVARIANTS
7469         hopeless_peer:
7470                         panic("Impossible sack 1");
7471 #else
7472
7473
7474                         /*
7475                          * no way, we have not even sent this TSN out yet.
7476                          * Peer is hopelessly messed up with us.
7477                          */
7478         hopeless_peer:
7479                         *abort_now = 1;
7480                         /* XXX */
7481                         oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
7482                             0, M_DONTWAIT, 1, MT_DATA);
7483                         if (oper) {
7484                                 struct sctp_paramhdr *ph;
7485                                 uint32_t *ippp;
7486
7487                                 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
7488                                     sizeof(uint32_t);
7489                                 ph = mtod(oper, struct sctp_paramhdr *);
7490                                 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
7491                                 ph->param_length = htons(SCTP_BUF_LEN(oper));
7492                                 ippp = (uint32_t *) (ph + 1);
7493                                 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
7494                         }
7495                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
7496                         sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
7497                         return;
7498 #endif
7499                 }
7500         }
7501         /**********************/
7502         /* 1) check the range */
7503         /**********************/
7504         if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
7505                 /* acking something behind */
7506                 return;
7507         }
7508         sav_cum_ack = asoc->last_acked_seq;
7509
7510         /* update the Rwnd of the peer */
7511         if (TAILQ_EMPTY(&asoc->sent_queue) &&
7512             TAILQ_EMPTY(&asoc->send_queue) &&
7513             (asoc->stream_queue_cnt == 0)
7514             ) {
7515                 /* nothing left on send/sent and strmq */
7516                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7517                         sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
7518                             asoc->peers_rwnd, 0, 0, a_rwnd);
7519                 }
7520                 asoc->peers_rwnd = a_rwnd;
7521                 if (asoc->sent_queue_retran_cnt) {
7522                         asoc->sent_queue_retran_cnt = 0;
7523                 }
7524                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7525                         /* SWS sender side engages */
7526                         asoc->peers_rwnd = 0;
7527                 }
7528                 /* stop any timers */
7529                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7530                         sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7531                             stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
7532                         if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
7533                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7534                                         SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
7535                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
7536                                             SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
7537                                 }
7538                         }
7539                         net->partial_bytes_acked = 0;
7540                         net->flight_size = 0;
7541                 }
7542                 asoc->total_flight = 0;
7543                 asoc->total_flight_count = 0;
7544                 return;
7545         }
7546         /*
7547          * We init netAckSz and netAckSz2 to 0. These are used to track 2
7548          * things. The total byte count acked is tracked in netAckSz AND
7549          * netAck2 is used to track the total bytes acked that are un-
7550          * amibguious and were never retransmitted. We track these on a per
7551          * destination address basis.
7552          */
7553         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7554                 net->prev_cwnd = net->cwnd;
7555                 net->net_ack = 0;
7556                 net->net_ack2 = 0;
7557
7558                 /*
7559                  * CMT: Reset CUC and Fast recovery algo variables before
7560                  * SACK processing
7561                  */
7562                 net->new_pseudo_cumack = 0;
7563                 net->will_exit_fast_recovery = 0;
7564         }
7565         /* process the new consecutive TSN first */
7566         tp1 = TAILQ_FIRST(&asoc->sent_queue);
7567         while (tp1) {
7568                 if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
7569                     MAX_TSN) ||
7570                     last_tsn == tp1->rec.data.TSN_seq) {
7571                         if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
7572                                 /*
7573                                  * ECN Nonce: Add the nonce to the sender's
7574                                  * nonce sum
7575                                  */
7576                                 asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
7577                                 accum_moved = 1;
7578                                 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
7579                                         /*
7580                                          * If it is less than ACKED, it is
7581                                          * now no-longer in flight. Higher
7582                                          * values may occur during marking
7583                                          */
7584                                         if ((tp1->whoTo->dest_state &
7585                                             SCTP_ADDR_UNCONFIRMED) &&
7586                                             (tp1->snd_count < 2)) {
7587                                                 /*
7588                                                  * If there was no retran
7589                                                  * and the address is
7590                                                  * un-confirmed and we sent
7591                                                  * there and are now
7592                                                  * sacked.. its confirmed,
7593                                                  * mark it so.
7594                                                  */
7595                                                 tp1->whoTo->dest_state &=
7596                                                     ~SCTP_ADDR_UNCONFIRMED;
7597                                         }
7598                                         if (tp1->sent < SCTP_DATAGRAM_RESEND) {
7599                                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7600                                                         sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
7601                                                             tp1->whoTo->flight_size,
7602                                                             tp1->book_size,
7603                                                             (uintptr_t) tp1->whoTo,
7604                                                             tp1->rec.data.TSN_seq);
7605                                                 }
7606                                                 sctp_flight_size_decrease(tp1);
7607                                                 sctp_total_flight_decrease(stcb, tp1);
7608                                         }
7609                                         tp1->whoTo->net_ack += tp1->send_size;
7610
7611                                         /* CMT SFR and DAC algos */
7612                                         this_sack_lowest_newack = tp1->rec.data.TSN_seq;
7613                                         tp1->whoTo->saw_newack = 1;
7614
7615                                         if (tp1->snd_count < 2) {
7616                                                 /*
7617                                                  * True non-retransmited
7618                                                  * chunk
7619                                                  */
7620                                                 tp1->whoTo->net_ack2 +=
7621                                                     tp1->send_size;
7622
7623                                                 /* update RTO too? */
7624                                                 if (tp1->do_rtt) {
7625                                                         tp1->whoTo->RTO =
7626                                                             sctp_calculate_rto(stcb,
7627                                                             asoc, tp1->whoTo,
7628                                                             &tp1->sent_rcv_time,
7629                                                             sctp_align_safe_nocopy);
7630                                                         tp1->do_rtt = 0;
7631                                                 }
7632                                         }
7633                                         /*
7634                                          * CMT: CUCv2 algorithm. From the
7635                                          * cumack'd TSNs, for each TSN being
7636                                          * acked for the first time, set the
7637                                          * following variables for the
7638                                          * corresp destination.
7639                                          * new_pseudo_cumack will trigger a
7640                                          * cwnd update.
7641                                          * find_(rtx_)pseudo_cumack will
7642                                          * trigger search for the next
7643                                          * expected (rtx-)pseudo-cumack.
7644                                          */
7645                                         tp1->whoTo->new_pseudo_cumack = 1;
7646                                         tp1->whoTo->find_pseudo_cumack = 1;
7647                                         tp1->whoTo->find_rtx_pseudo_cumack = 1;
7648
7649
7650                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7651                                                 sctp_log_sack(asoc->last_acked_seq,
7652                                                     cum_ack,
7653                                                     tp1->rec.data.TSN_seq,
7654                                                     0,
7655                                                     0,
7656                                                     SCTP_LOG_TSN_ACKED);
7657                                         }
7658                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7659                                                 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
7660                                         }
7661                                 }
7662                                 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
7663                                         sctp_ucount_decr(asoc->sent_queue_retran_cnt);
7664 #ifdef SCTP_AUDITING_ENABLED
7665                                         sctp_audit_log(0xB3,
7666                                             (asoc->sent_queue_retran_cnt & 0x000000ff));
7667 #endif
7668                                 }
7669                                 if (tp1->rec.data.chunk_was_revoked) {
7670                                         /* deflate the cwnd */
7671                                         tp1->whoTo->cwnd -= tp1->book_size;
7672                                         tp1->rec.data.chunk_was_revoked = 0;
7673                                 }
7674                                 tp1->sent = SCTP_DATAGRAM_ACKED;
7675                         }
7676                 } else {
7677                         break;
7678                 }
7679                 tp1 = TAILQ_NEXT(tp1, sctp_next);
7680         }
7681         biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
7682         /* always set this up to cum-ack */
7683         asoc->this_sack_highest_gap = last_tsn;
7684
7685         /* Move offset up to point to gaps/dups */
7686         offset += sizeof(struct sctp_nr_sack_chunk);
7687         if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_nr_sack_chunk)) > nr_sack_length) {
7688
7689                 /* skip corrupt segments */
7690                 goto skip_segments;
7691         }
7692         if (num_seg > 0) {
7693
7694                 /*
7695                  * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
7696                  * to be greater than the cumack. Also reset saw_newack to 0
7697                  * for all dests.
7698                  */
7699                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7700                         net->saw_newack = 0;
7701                         net->this_sack_highest_newack = last_tsn;
7702                 }
7703
7704                 /*
7705                  * thisSackHighestGap will increase while handling NEW
7706                  * segments this_sack_highest_newack will increase while
7707                  * handling NEWLY ACKED chunks. this_sack_lowest_newack is
7708                  * used for CMT DAC algo. saw_newack will also change.
7709                  */
7710
7711                 sctp_handle_nr_sack_segments(m, &offset, stcb, asoc, ch, last_tsn,
7712                     &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
7713                     num_seg, num_nr_seg, &ecn_seg_sums);
7714
7715
7716                 if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
7717                         /*
7718                          * validate the biggest_tsn_acked in the gap acks if
7719                          * strict adherence is wanted.
7720                          */
7721                         if ((biggest_tsn_acked == send_s) ||
7722                             (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
7723                                 /*
7724                                  * peer is either confused or we are under
7725                                  * attack. We must abort.
7726                                  */
7727                                 goto hopeless_peer;
7728                         }
7729                 }
7730         }
7731 skip_segments:
7732         /*******************************************/
7733         /* cancel ALL T3-send timer if accum moved */
7734         /*******************************************/
7735         if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7736                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7737                         if (net->new_pseudo_cumack)
7738                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7739                                     stcb, net,
7740                                     SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
7741
7742                 }
7743         } else {
7744                 if (accum_moved) {
7745                         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7746                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7747                                     stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
7748                         }
7749                 }
7750         }
7751         /********************************************/
7752         /* drop the acked chunks from the sendqueue */
7753         /********************************************/
7754         asoc->last_acked_seq = cum_ack;
7755
7756         tp1 = TAILQ_FIRST(&asoc->sent_queue);
7757         if (tp1 == NULL)
7758                 goto done_with_it;
7759         do {
7760                 if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
7761                     MAX_TSN)) {
7762                         break;
7763                 }
7764                 if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
7765                         /* no more sent on list */
7766                         printf("Warning, tp1->sent == %d and its now acked?\n",
7767                             tp1->sent);
7768                 }
7769                 tp2 = TAILQ_NEXT(tp1, sctp_next);
7770                 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
7771                 if (tp1->pr_sctp_on) {
7772                         if (asoc->pr_sctp_cnt != 0)
7773                                 asoc->pr_sctp_cnt--;
7774                 }
7775                 if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
7776                     (asoc->total_flight > 0)) {
7777 #ifdef INVARIANTS
7778                         panic("Warning flight size is postive and should be 0");
7779 #else
7780                         SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
7781                             asoc->total_flight);
7782 #endif
7783                         asoc->total_flight = 0;
7784                 }
7785                 if (tp1->data) {
7786                         /* sa_ignore NO_NULL_CHK */
7787                         sctp_free_bufspace(stcb, asoc, tp1, 1);
7788                         sctp_m_freem(tp1->data);
7789                         if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
7790                                 asoc->sent_queue_cnt_removeable--;
7791                         }
7792                 }
7793                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7794                         sctp_log_sack(asoc->last_acked_seq,
7795                             cum_ack,
7796                             tp1->rec.data.TSN_seq,
7797                             0,
7798                             0,
7799                             SCTP_LOG_FREE_SENT);
7800                 }
7801                 tp1->data = NULL;
7802                 asoc->sent_queue_cnt--;
7803                 sctp_free_a_chunk(stcb, tp1);
7804                 wake_him++;
7805                 tp1 = tp2;
7806         } while (tp1 != NULL);
7807
7808 done_with_it:
7809         /* sa_ignore NO_NULL_CHK */
7810         if ((wake_him) && (stcb->sctp_socket)) {
7811 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7812                 struct socket *so;
7813
7814 #endif
7815                 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
7816                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
7817                         sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
7818                 }
7819 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7820                 so = SCTP_INP_SO(stcb->sctp_ep);
7821                 atomic_add_int(&stcb->asoc.refcnt, 1);
7822                 SCTP_TCB_UNLOCK(stcb);
7823                 SCTP_SOCKET_LOCK(so, 1);
7824                 SCTP_TCB_LOCK(stcb);
7825                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
7826                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
7827                         /* assoc was freed while we were unlocked */
7828                         SCTP_SOCKET_UNLOCK(so, 1);
7829                         return;
7830                 }
7831 #endif
7832                 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
7833 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7834                 SCTP_SOCKET_UNLOCK(so, 1);
7835 #endif
7836         } else {
7837                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
7838                         sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
7839                 }
7840         }
7841
7842         if (asoc->fast_retran_loss_recovery && accum_moved) {
7843                 if (compare_with_wrap(asoc->last_acked_seq,
7844                     asoc->fast_recovery_tsn, MAX_TSN) ||
7845                     asoc->last_acked_seq == asoc->fast_recovery_tsn) {
7846                         /* Setup so we will exit RFC2582 fast recovery */
7847                         will_exit_fast_recovery = 1;
7848                 }
7849         }
7850         /*
7851          * Check for revoked fragments:
7852          * 
7853          * if Previous sack - Had no frags then we can't have any revoked if
7854          * Previous sack - Had frag's then - If we now have frags aka
7855          * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
7856          * some of them. else - The peer revoked all ACKED fragments, since
7857          * we had some before and now we have NONE.
7858          */
7859
7860         if (num_seg)
7861                 sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
7862
7863         else if (asoc->saw_sack_with_frags) {
7864                 int cnt_revoked = 0;
7865
7866                 tp1 = TAILQ_FIRST(&asoc->sent_queue);
7867                 if (tp1 != NULL) {
7868                         /* Peer revoked all dg's marked or acked */
7869                         TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
7870                                 /*
7871                                  * EY- maybe check only if it is nr_acked
7872                                  * nr_marked may not be possible
7873                                  */
7874                                 if ((tp1->sent == SCTP_DATAGRAM_NR_ACKED) ||
7875                                     (tp1->sent == SCTP_DATAGRAM_NR_MARKED)) {
7876                                         /*
7877                                          * EY! - TODO: Something previously
7878                                          * nr_gapped is reneged, abort the
7879                                          * association
7880                                          */
7881                                         return;
7882                                 }
7883                                 if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
7884                                     (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
7885                                         tp1->sent = SCTP_DATAGRAM_SENT;
7886                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7887                                                 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
7888                                                     tp1->whoTo->flight_size,
7889                                                     tp1->book_size,
7890                                                     (uintptr_t) tp1->whoTo,
7891                                                     tp1->rec.data.TSN_seq);
7892                                         }
7893                                         sctp_flight_size_increase(tp1);
7894                                         sctp_total_flight_increase(stcb, tp1);
7895                                         tp1->rec.data.chunk_was_revoked = 1;
7896                                         /*
7897                                          * To ensure that this increase in
7898                                          * flightsize, which is artificial,
7899                                          * does not throttle the sender, we
7900                                          * also increase the cwnd
7901                                          * artificially.
7902                                          */
7903                                         tp1->whoTo->cwnd += tp1->book_size;
7904                                         cnt_revoked++;
7905                                 }
7906                         }
7907                         if (cnt_revoked) {
7908                                 reneged_all = 1;
7909                         }
7910                 }
7911                 asoc->saw_sack_with_frags = 0;
7912         }
7913         if (num_seg)
7914                 asoc->saw_sack_with_frags = 1;
7915         else
7916                 asoc->saw_sack_with_frags = 0;
7917
7918         /* EY! - not sure about if there should be an IF */
7919         if (num_nr_seg)
7920                 sctp_check_for_nr_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
7921         else if (asoc->saw_sack_with_nr_frags) {
7922                 /*
7923                  * EY!- TODO: all previously nr_gapped chunks have been
7924                  * reneged abort the association
7925                  */
7926                 asoc->saw_sack_with_nr_frags = 0;
7927         }
7928         if (num_nr_seg)
7929                 asoc->saw_sack_with_nr_frags = 1;
7930         else
7931                 asoc->saw_sack_with_nr_frags = 0;
7932         /* JRS - Use the congestion control given in the CC module */
7933         asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
7934
7935         if (TAILQ_EMPTY(&asoc->sent_queue)) {
7936                 /* nothing left in-flight */
7937                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7938                         /* stop all timers */
7939                         if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
7940                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7941                                         SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
7942                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
7943                                             SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
7944                                 }
7945                         }
7946                         sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7947                             stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
7948                         net->flight_size = 0;
7949                         net->partial_bytes_acked = 0;
7950                 }
7951                 asoc->total_flight = 0;
7952                 asoc->total_flight_count = 0;
7953         }
7954         /**********************************/
7955         /* Now what about shutdown issues */
7956         /**********************************/
7957         if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
7958                 /* nothing left on sendqueue.. consider done */
7959                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7960                         sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
7961                             asoc->peers_rwnd, 0, 0, a_rwnd);
7962                 }
7963                 asoc->peers_rwnd = a_rwnd;
7964                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7965                         /* SWS sender side engages */
7966                         asoc->peers_rwnd = 0;
7967                 }
7968                 /* clean up */
7969                 if ((asoc->stream_queue_cnt == 1) &&
7970                     ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7971                     (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
7972                     (asoc->locked_on_sending)
7973                     ) {
7974                         struct sctp_stream_queue_pending *sp;
7975
7976                         /*
7977                          * I may be in a state where we got all across.. but
7978                          * cannot write more due to a shutdown... we abort
7979                          * since the user did not indicate EOR in this case.
7980                          */
7981                         sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
7982                             sctp_streamhead);
7983                         if ((sp) && (sp->length == 0)) {
7984                                 asoc->locked_on_sending = NULL;
7985                                 if (sp->msg_is_complete) {
7986                                         asoc->stream_queue_cnt--;
7987                                 } else {
7988                                         asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
7989                                         asoc->stream_queue_cnt--;
7990                                 }
7991                         }
7992                 }
7993                 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
7994                     (asoc->stream_queue_cnt == 0)) {
7995                         if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
7996                                 /* Need to abort here */
7997                                 struct mbuf *oper;
7998
7999                 abort_out_now:
8000                                 *abort_now = 1;
8001                                 /* XXX */
8002                                 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
8003                                     0, M_DONTWAIT, 1, MT_DATA);
8004                                 if (oper) {
8005                                         struct sctp_paramhdr *ph;
8006                                         uint32_t *ippp;
8007
8008                                         SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
8009                                             sizeof(uint32_t);
8010                                         ph = mtod(oper, struct sctp_paramhdr *);
8011                                         ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
8012                                         ph->param_length = htons(SCTP_BUF_LEN(oper));
8013                                         ippp = (uint32_t *) (ph + 1);
8014                                         *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
8015                                 }
8016                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
8017                                 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
8018                                 return;
8019                         } else {
8020                                 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
8021                                     (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
8022                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
8023                                 }
8024                                 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
8025                                 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
8026                                 sctp_stop_timers_for_shutdown(stcb);
8027                                 sctp_send_shutdown(stcb,
8028                                     stcb->asoc.primary_destination);
8029                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
8030                                     stcb->sctp_ep, stcb, asoc->primary_destination);
8031                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
8032                                     stcb->sctp_ep, stcb, asoc->primary_destination);
8033                         }
8034                         return;
8035                 } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
8036                     (asoc->stream_queue_cnt == 0)) {
8037                         if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
8038                                 goto abort_out_now;
8039                         }
8040                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
8041                         SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
8042                         SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
8043                         sctp_send_shutdown_ack(stcb,
8044                             stcb->asoc.primary_destination);
8045
8046                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
8047                             stcb->sctp_ep, stcb, asoc->primary_destination);
8048                         return;
8049                 }
8050         }
8051         /*
8052          * Now here we are going to recycle net_ack for a different use...
8053          * HEADS UP.
8054          */
8055         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8056                 net->net_ack = 0;
8057         }
8058
8059         /*
8060          * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
8061          * to be done. Setting this_sack_lowest_newack to the cum_ack will
8062          * automatically ensure that.
8063          */
8064         if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac) && (cmt_dac_flag == 0)) {
8065                 this_sack_lowest_newack = cum_ack;
8066         }
8067         if (num_seg > 0) {
8068                 sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
8069                     biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
8070         }
8071         /* JRS - Use the congestion control given in the CC module */
8072         asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
8073
8074         /******************************************************************
8075          *  Here we do the stuff with ECN Nonce checking.
8076          *  We basically check to see if the nonce sum flag was incorrect
8077          *  or if resynchronization needs to be done. Also if we catch a
8078          *  misbehaving receiver we give him the kick.
8079          ******************************************************************/
8080
8081         if (asoc->ecn_nonce_allowed) {
8082                 if (asoc->nonce_sum_check) {
8083                         if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
8084                                 if (asoc->nonce_wait_for_ecne == 0) {
8085                                         struct sctp_tmit_chunk *lchk;
8086
8087                                         lchk = TAILQ_FIRST(&asoc->send_queue);
8088                                         asoc->nonce_wait_for_ecne = 1;
8089                                         if (lchk) {
8090                                                 asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
8091                                         } else {
8092                                                 asoc->nonce_wait_tsn = asoc->sending_seq;
8093                                         }
8094                                 } else {
8095                                         if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
8096                                             (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
8097                                                 /*
8098                                                  * Misbehaving peer. We need
8099                                                  * to react to this guy
8100                                                  */
8101                                                 asoc->ecn_allowed = 0;
8102                                                 asoc->ecn_nonce_allowed = 0;
8103                                         }
8104                                 }
8105                         }
8106                 } else {
8107                         /* See if Resynchronization Possible */
8108                         if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
8109                                 asoc->nonce_sum_check = 1;
8110                                 /*
8111                                  * now we must calculate what the base is.
8112                                  * We do this based on two things, we know
8113                                  * the total's for all the segments
8114                                  * gap-acked in the SACK, its stored in
8115                                  * ecn_seg_sums. We also know the SACK's
8116                                  * nonce sum, its in nonce_sum_flag. So we
8117                                  * can build a truth table to back-calculate
8118                                  * the new value of
8119                                  * asoc->nonce_sum_expect_base:
8120                                  * 
8121                                  * SACK-flag-Value         Seg-Sums Base 0 0 0
8122                                  * 1                    0 1 0 1 1 1 1 0
8123                                  */
8124                                 asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
8125                         }
8126                 }
8127         }
8128         /* Now are we exiting loss recovery ? */
8129         if (will_exit_fast_recovery) {
8130                 /* Ok, we must exit fast recovery */
8131                 asoc->fast_retran_loss_recovery = 0;
8132         }
8133         if ((asoc->sat_t3_loss_recovery) &&
8134             ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
8135             MAX_TSN) ||
8136             (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
8137                 /* end satellite t3 loss recovery */
8138                 asoc->sat_t3_loss_recovery = 0;
8139         }
8140         /*
8141          * CMT Fast recovery
8142          */
8143         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8144                 if (net->will_exit_fast_recovery) {
8145                         /* Ok, we must exit fast recovery */
8146                         net->fast_retran_loss_recovery = 0;
8147                 }
8148         }
8149
8150         /* Adjust and set the new rwnd value */
8151         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
8152                 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
8153                     asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
8154         }
8155         asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
8156             (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
8157         if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
8158                 /* SWS sender side engages */
8159                 asoc->peers_rwnd = 0;
8160         }
8161         if (asoc->peers_rwnd > old_rwnd) {
8162                 win_probe_recovery = 1;
8163         }
8164         /*
8165          * Now we must setup so we have a timer up for anyone with
8166          * outstanding data.
8167          */
8168         done_once = 0;
8169 again:
8170         j = 0;
8171         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8172                 if (win_probe_recovery && (net->window_probe)) {
8173                         win_probe_recovered = 1;
8174                         /*-
8175                          * Find first chunk that was used with
8176                          * window probe and clear the event. Put
8177                          * it back into the send queue as if has
8178                          * not been sent.
8179                          */
8180                         TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
8181                                 if (tp1->window_probe) {
8182                                         sctp_window_probe_recovery(stcb, asoc, net, tp1);
8183                                         break;
8184                                 }
8185                         }
8186                 }
8187                 if (net->flight_size) {
8188                         j++;
8189                         sctp_timer_start(SCTP_TIMER_TYPE_SEND,
8190                             stcb->sctp_ep, stcb, net);
8191                         if (net->window_probe) {
8192                                 net->window_probe = 0;
8193                         }
8194                 } else {
8195                         if (net->window_probe) {
8196                                 net->window_probe = 0;
8197                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
8198                                     stcb->sctp_ep, stcb, net);
8199                         } else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8200                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
8201                                     stcb, net,
8202                                     SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
8203                         }
8204                         if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8205                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8206                                         SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
8207                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
8208                                             SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
8209                                 }
8210                         }
8211                 }
8212         }
8213         if ((j == 0) &&
8214             (!TAILQ_EMPTY(&asoc->sent_queue)) &&
8215             (asoc->sent_queue_retran_cnt == 0) &&
8216             (win_probe_recovered == 0) &&
8217             (done_once == 0)) {
8218                 /*
8219                  * huh, this should not happen unless all packets are
8220                  * PR-SCTP and marked to skip of course.
8221                  */
8222                 if (sctp_fs_audit(asoc)) {
8223                         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8224                                 net->flight_size = 0;
8225                         }
8226                         asoc->total_flight = 0;
8227                         asoc->total_flight_count = 0;
8228                         asoc->sent_queue_retran_cnt = 0;
8229                         TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
8230                                 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
8231                                         sctp_flight_size_increase(tp1);
8232                                         sctp_total_flight_increase(stcb, tp1);
8233                                 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
8234                                         asoc->sent_queue_retran_cnt++;
8235                                 }
8236                         }
8237                 }
8238                 done_once = 1;
8239                 goto again;
8240         }
8241         /*********************************************/
8242         /* Here we perform PR-SCTP procedures        */
8243         /* (section 4.2)                             */
8244         /*********************************************/
8245         /* C1. update advancedPeerAckPoint */
8246         if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
8247                 asoc->advanced_peer_ack_point = cum_ack;
8248         }
8249         /* C2. try to further move advancedPeerAckPoint ahead */
8250         if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
8251                 struct sctp_tmit_chunk *lchk;
8252                 uint32_t old_adv_peer_ack_point;
8253
8254                 old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
8255                 lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
8256                 /* C3. See if we need to send a Fwd-TSN */
8257                 if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
8258                     MAX_TSN)) {
8259                         /*
8260                          * ISSUE with ECN, see FWD-TSN processing for notes
8261                          * on issues that will occur when the ECN NONCE
8262                          * stuff is put into SCTP for cross checking.
8263                          */
8264                         if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
8265                             MAX_TSN)) {
8266                                 send_forward_tsn(stcb, asoc);
8267                                 /*
8268                                  * ECN Nonce: Disable Nonce Sum check when
8269                                  * FWD TSN is sent and store resync tsn
8270                                  */
8271                                 asoc->nonce_sum_check = 0;
8272                                 asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
8273                         } else if (lchk) {
8274                                 /* try to FR fwd-tsn's that get lost too */
8275                                 lchk->rec.data.fwd_tsn_cnt++;
8276                                 if (lchk->rec.data.fwd_tsn_cnt > 3) {
8277                                         send_forward_tsn(stcb, asoc);
8278                                         lchk->rec.data.fwd_tsn_cnt = 0;
8279                                 }
8280                         }
8281                 }
8282                 if (lchk) {
8283                         /* Assure a timer is up */
8284                         sctp_timer_start(SCTP_TIMER_TYPE_SEND,
8285                             stcb->sctp_ep, stcb, lchk->whoTo);
8286                 }
8287         }
8288         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
8289                 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
8290                     a_rwnd,
8291                     stcb->asoc.peers_rwnd,
8292                     stcb->asoc.total_flight,
8293                     stcb->asoc.total_output_queue_size);
8294         }
8295 }