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