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