]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/sctp_input.c
MFC r323861:
[FreeBSD/FreeBSD.git] / sys / netinet / sctp_input.c
1 /*-
2  * Copyright (c) 2001-2008, 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_auth.h>
45 #include <netinet/sctp_indata.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctp_bsd_addr.h>
48 #include <netinet/sctp_timer.h>
49 #include <netinet/sctp_crc32.h>
50 #if defined(INET) || defined(INET6)
51 #include <netinet/udp.h>
52 #endif
53 #include <sys/smp.h>
54
55
56
57 static void
58 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
59 {
60         struct sctp_nets *net;
61
62         /*
63          * This now not only stops all cookie timers it also stops any INIT
64          * timers as well. This will make sure that the timers are stopped
65          * in all collision cases.
66          */
67         SCTP_TCB_LOCK_ASSERT(stcb);
68         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
69                 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
70                         sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
71                             stcb->sctp_ep,
72                             stcb,
73                             net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
74                 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
75                         sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
76                             stcb->sctp_ep,
77                             stcb,
78                             net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
79                 }
80         }
81 }
82
83 /* INIT handler */
84 static void
85 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
86     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
87     struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
88     struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_no_unlock,
89     uint8_t mflowtype, uint32_t mflowid,
90     uint32_t vrf_id, uint16_t port)
91 {
92         struct sctp_init *init;
93         struct mbuf *op_err;
94
95         SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
96             (void *)stcb);
97         if (stcb == NULL) {
98                 SCTP_INP_RLOCK(inp);
99         }
100         /* validate length */
101         if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
102                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
103                 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
104                     mflowtype, mflowid,
105                     vrf_id, port);
106                 if (stcb)
107                         *abort_no_unlock = 1;
108                 goto outnow;
109         }
110         /* validate parameters */
111         init = &cp->init;
112         if (init->initiate_tag == 0) {
113                 /* protocol error... send abort */
114                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
115                 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
116                     mflowtype, mflowid,
117                     vrf_id, port);
118                 if (stcb)
119                         *abort_no_unlock = 1;
120                 goto outnow;
121         }
122         if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
123                 /* invalid parameter... send abort */
124                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
125                 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
126                     mflowtype, mflowid,
127                     vrf_id, port);
128                 if (stcb)
129                         *abort_no_unlock = 1;
130                 goto outnow;
131         }
132         if (init->num_inbound_streams == 0) {
133                 /* protocol error... send abort */
134                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
135                 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
136                     mflowtype, mflowid,
137                     vrf_id, port);
138                 if (stcb)
139                         *abort_no_unlock = 1;
140                 goto outnow;
141         }
142         if (init->num_outbound_streams == 0) {
143                 /* protocol error... send abort */
144                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
145                 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
146                     mflowtype, mflowid,
147                     vrf_id, port);
148                 if (stcb)
149                         *abort_no_unlock = 1;
150                 goto outnow;
151         }
152         if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
153             offset + ntohs(cp->ch.chunk_length))) {
154                 /* auth parameter(s) error... send abort */
155                 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
156                     "Problem with AUTH parameters");
157                 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
158                     mflowtype, mflowid,
159                     vrf_id, port);
160                 if (stcb)
161                         *abort_no_unlock = 1;
162                 goto outnow;
163         }
164         /* We are only accepting if we have a socket with positive
165          * so_qlimit. */
166         if ((stcb == NULL) &&
167             ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
168             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
169             (inp->sctp_socket == NULL) ||
170             (inp->sctp_socket->so_qlimit == 0))) {
171                 /*
172                  * FIX ME ?? What about TCP model and we have a
173                  * match/restart case? Actually no fix is needed. the lookup
174                  * will always find the existing assoc so stcb would not be
175                  * NULL. It may be questionable to do this since we COULD
176                  * just send back the INIT-ACK and hope that the app did
177                  * accept()'s by the time the COOKIE was sent. But there is
178                  * a price to pay for COOKIE generation and I don't want to
179                  * pay it on the chance that the app will actually do some
180                  * accepts(). The App just looses and should NOT be in this
181                  * state :-)
182                  */
183                 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
184                         op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
185                             "No listener");
186                         sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
187                             mflowtype, mflowid, inp->fibnum,
188                             vrf_id, port);
189                 }
190                 goto outnow;
191         }
192         if ((stcb != NULL) &&
193             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
194                 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
195                 sctp_send_shutdown_ack(stcb, NULL);
196                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
197         } else {
198                 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
199                 sctp_send_initiate_ack(inp, stcb, net, m, iphlen, offset,
200                     src, dst, sh, cp,
201                     mflowtype, mflowid,
202                     vrf_id, port);
203         }
204 outnow:
205         if (stcb == NULL) {
206                 SCTP_INP_RUNLOCK(inp);
207         }
208 }
209
210 /*
211  * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
212  */
213
214 int
215 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
216 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
217     SCTP_UNUSED
218 #endif
219 )
220 {
221         int unsent_data;
222         unsigned int i;
223         struct sctp_stream_queue_pending *sp;
224         struct sctp_association *asoc;
225
226         /*
227          * This function returns if any stream has true unsent data on it.
228          * Note that as it looks through it will clean up any places that
229          * have old data that has been sent but left at top of stream queue.
230          */
231         asoc = &stcb->asoc;
232         unsent_data = 0;
233         SCTP_TCB_SEND_LOCK(stcb);
234         if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
235                 /* Check to see if some data queued */
236                 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
237                         /* sa_ignore FREED_MEMORY */
238                         sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
239                         if (sp == NULL) {
240                                 continue;
241                         }
242                         if ((sp->msg_is_complete) &&
243                             (sp->length == 0) &&
244                             (sp->sender_all_done)) {
245                                 /*
246                                  * We are doing differed cleanup. Last time
247                                  * through when we took all the data the
248                                  * sender_all_done was not set.
249                                  */
250                                 if (sp->put_last_out == 0) {
251                                         SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
252                                         SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
253                                             sp->sender_all_done,
254                                             sp->length,
255                                             sp->msg_is_complete,
256                                             sp->put_last_out);
257                                 }
258                                 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
259                                 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
260                                 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, &asoc->strmout[i], sp, 1);
261                                 if (sp->net) {
262                                         sctp_free_remote_addr(sp->net);
263                                         sp->net = NULL;
264                                 }
265                                 if (sp->data) {
266                                         sctp_m_freem(sp->data);
267                                         sp->data = NULL;
268                                 }
269                                 sctp_free_a_strmoq(stcb, sp, so_locked);
270                                 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
271                                         unsent_data++;
272                                 }
273                         } else {
274                                 unsent_data++;
275                         }
276                         if (unsent_data > 0) {
277                                 break;
278                         }
279                 }
280         }
281         SCTP_TCB_SEND_UNLOCK(stcb);
282         return (unsent_data);
283 }
284
285 static int
286 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
287 {
288         struct sctp_init *init;
289         struct sctp_association *asoc;
290         struct sctp_nets *lnet;
291         unsigned int i;
292
293         init = &cp->init;
294         asoc = &stcb->asoc;
295         /* save off parameters */
296         asoc->peer_vtag = ntohl(init->initiate_tag);
297         asoc->peers_rwnd = ntohl(init->a_rwnd);
298         /* init tsn's */
299         asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
300
301         if (!TAILQ_EMPTY(&asoc->nets)) {
302                 /* update any ssthresh's that may have a default */
303                 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
304                         lnet->ssthresh = asoc->peers_rwnd;
305                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
306                                 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
307                         }
308                 }
309         }
310         SCTP_TCB_SEND_LOCK(stcb);
311         if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
312                 unsigned int newcnt;
313                 struct sctp_stream_out *outs;
314                 struct sctp_stream_queue_pending *sp, *nsp;
315                 struct sctp_tmit_chunk *chk, *nchk;
316
317                 /* abandon the upper streams */
318                 newcnt = ntohs(init->num_inbound_streams);
319                 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
320                         if (chk->rec.data.sid >= newcnt) {
321                                 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
322                                 asoc->send_queue_cnt--;
323                                 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
324                                         asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
325 #ifdef INVARIANTS
326                                 } else {
327                                         panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
328 #endif
329                                 }
330                                 if (chk->data != NULL) {
331                                         sctp_free_bufspace(stcb, asoc, chk, 1);
332                                         sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
333                                             0, chk, SCTP_SO_NOT_LOCKED);
334                                         if (chk->data) {
335                                                 sctp_m_freem(chk->data);
336                                                 chk->data = NULL;
337                                         }
338                                 }
339                                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
340                                 /* sa_ignore FREED_MEMORY */
341                         }
342                 }
343                 if (asoc->strmout) {
344                         for (i = newcnt; i < asoc->pre_open_streams; i++) {
345                                 outs = &asoc->strmout[i];
346                                 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
347                                         atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
348                                         TAILQ_REMOVE(&outs->outqueue, sp, next);
349                                         stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
350                                         sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
351                                             stcb, 0, sp, SCTP_SO_NOT_LOCKED);
352                                         if (sp->data) {
353                                                 sctp_m_freem(sp->data);
354                                                 sp->data = NULL;
355                                         }
356                                         if (sp->net) {
357                                                 sctp_free_remote_addr(sp->net);
358                                                 sp->net = NULL;
359                                         }
360                                         /* Free the chunk */
361                                         sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
362                                         /* sa_ignore FREED_MEMORY */
363                                 }
364                                 outs->state = SCTP_STREAM_CLOSED;
365                         }
366                 }
367                 /* cut back the count */
368                 asoc->pre_open_streams = newcnt;
369         }
370         SCTP_TCB_SEND_UNLOCK(stcb);
371         asoc->streamoutcnt = asoc->pre_open_streams;
372         if (asoc->strmout) {
373                 for (i = 0; i < asoc->streamoutcnt; i++) {
374                         asoc->strmout[i].state = SCTP_STREAM_OPEN;
375                 }
376         }
377         /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
378         asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
379         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
380                 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
381         }
382         /* This is the next one we expect */
383         asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
384
385         asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
386         asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
387
388         asoc->advanced_peer_ack_point = asoc->last_acked_seq;
389         /* open the requested streams */
390
391         if (asoc->strmin != NULL) {
392                 /* Free the old ones */
393                 for (i = 0; i < asoc->streamincnt; i++) {
394                         sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
395                         sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
396                 }
397                 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
398         }
399         if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
400                 asoc->streamincnt = ntohs(init->num_outbound_streams);
401         } else {
402                 asoc->streamincnt = asoc->max_inbound_streams;
403         }
404         SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
405             sizeof(struct sctp_stream_in), SCTP_M_STRMI);
406         if (asoc->strmin == NULL) {
407                 /* we didn't get memory for the streams! */
408                 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
409                 return (-1);
410         }
411         for (i = 0; i < asoc->streamincnt; i++) {
412                 asoc->strmin[i].sid = i;
413                 asoc->strmin[i].last_mid_delivered = 0xffffffff;
414                 TAILQ_INIT(&asoc->strmin[i].inqueue);
415                 TAILQ_INIT(&asoc->strmin[i].uno_inqueue);
416                 asoc->strmin[i].pd_api_started = 0;
417                 asoc->strmin[i].delivery_started = 0;
418         }
419         /*
420          * load_address_from_init will put the addresses into the
421          * association when the COOKIE is processed or the INIT-ACK is
422          * processed. Both types of COOKIE's existing and new call this
423          * routine. It will remove addresses that are no longer in the
424          * association (for the restarting case where addresses are
425          * removed). Up front when the INIT arrives we will discard it if it
426          * is a restart and new addresses have been added.
427          */
428         /* sa_ignore MEMLEAK */
429         return (0);
430 }
431
432 /*
433  * INIT-ACK message processing/consumption returns value < 0 on error
434  */
435 static int
436 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
437     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
438     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
439     struct sctp_nets *net, int *abort_no_unlock,
440     uint8_t mflowtype, uint32_t mflowid,
441     uint32_t vrf_id)
442 {
443         struct sctp_association *asoc;
444         struct mbuf *op_err;
445         int retval, abort_flag;
446         uint32_t initack_limit;
447         int nat_friendly = 0;
448
449         /* First verify that we have no illegal param's */
450         abort_flag = 0;
451
452         op_err = sctp_arethere_unrecognized_parameters(m,
453             (offset + sizeof(struct sctp_init_chunk)),
454             &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
455         if (abort_flag) {
456                 /* Send an abort and notify peer */
457                 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
458                 *abort_no_unlock = 1;
459                 return (-1);
460         }
461         asoc = &stcb->asoc;
462         asoc->peer_supports_nat = (uint8_t)nat_friendly;
463         /* process the peer's parameters in the INIT-ACK */
464         retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
465         if (retval < 0) {
466                 return (retval);
467         }
468         initack_limit = offset + ntohs(cp->ch.chunk_length);
469         /* load all addresses */
470         if ((retval = sctp_load_addresses_from_init(stcb, m,
471             (offset + sizeof(struct sctp_init_chunk)), initack_limit,
472             src, dst, NULL, stcb->asoc.port))) {
473                 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
474                     "Problem with address parameters");
475                 SCTPDBG(SCTP_DEBUG_INPUT1,
476                     "Load addresses from INIT causes an abort %d\n",
477                     retval);
478                 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
479                     src, dst, sh, op_err,
480                     mflowtype, mflowid,
481                     vrf_id, net->port);
482                 *abort_no_unlock = 1;
483                 return (-1);
484         }
485         /* if the peer doesn't support asconf, flush the asconf queue */
486         if (asoc->asconf_supported == 0) {
487                 struct sctp_asconf_addr *param, *nparam;
488
489                 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
490                         TAILQ_REMOVE(&asoc->asconf_queue, param, next);
491                         SCTP_FREE(param, SCTP_M_ASC_ADDR);
492                 }
493         }
494         stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
495             stcb->asoc.local_hmacs);
496         if (op_err) {
497                 sctp_queue_op_err(stcb, op_err);
498                 /* queuing will steal away the mbuf chain to the out queue */
499                 op_err = NULL;
500         }
501         /* extract the cookie and queue it to "echo" it back... */
502         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
503                 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
504                     stcb->asoc.overall_error_count,
505                     0,
506                     SCTP_FROM_SCTP_INPUT,
507                     __LINE__);
508         }
509         stcb->asoc.overall_error_count = 0;
510         net->error_count = 0;
511
512         /*
513          * Cancel the INIT timer, We do this first before queueing the
514          * cookie. We always cancel at the primary to assue that we are
515          * canceling the timer started by the INIT which always goes to the
516          * primary.
517          */
518         sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
519             asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
520
521         /* calculate the RTO */
522         net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy,
523             SCTP_RTT_FROM_NON_DATA);
524         retval = sctp_send_cookie_echo(m, offset, stcb, net);
525         if (retval < 0) {
526                 /*
527                  * No cookie, we probably should send a op error. But in any
528                  * case if there is no cookie in the INIT-ACK, we can
529                  * abandon the peer, its broke.
530                  */
531                 if (retval == -3) {
532                         uint16_t len;
533
534                         len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
535                         /* We abort with an error of missing mandatory param */
536                         op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
537                         if (op_err != NULL) {
538                                 struct sctp_error_missing_param *cause;
539
540                                 SCTP_BUF_LEN(op_err) = len;
541                                 cause = mtod(op_err, struct sctp_error_missing_param *);
542                                 /* Subtract the reserved param */
543                                 cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
544                                 cause->cause.length = htons(len);
545                                 cause->num_missing_params = htonl(1);
546                                 cause->type[0] = htons(SCTP_STATE_COOKIE);
547                         }
548                         sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
549                             src, dst, sh, op_err,
550                             mflowtype, mflowid,
551                             vrf_id, net->port);
552                         *abort_no_unlock = 1;
553                 }
554                 return (retval);
555         }
556         return (0);
557 }
558
559 static void
560 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
561     struct sctp_tcb *stcb, struct sctp_nets *net)
562 {
563         union sctp_sockstore store;
564         struct sctp_nets *r_net, *f_net;
565         struct timeval tv;
566         int req_prim = 0;
567         uint16_t old_error_counter;
568
569         if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
570                 /* Invalid length */
571                 return;
572         }
573         memset(&store, 0, sizeof(store));
574         switch (cp->heartbeat.hb_info.addr_family) {
575 #ifdef INET
576         case AF_INET:
577                 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
578                         store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
579                         store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
580                         store.sin.sin_port = stcb->rport;
581                         memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
582                             sizeof(store.sin.sin_addr));
583                 } else {
584                         return;
585                 }
586                 break;
587 #endif
588 #ifdef INET6
589         case AF_INET6:
590                 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
591                         store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
592                         store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
593                         store.sin6.sin6_port = stcb->rport;
594                         memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
595                 } else {
596                         return;
597                 }
598                 break;
599 #endif
600         default:
601                 return;
602         }
603         r_net = sctp_findnet(stcb, &store.sa);
604         if (r_net == NULL) {
605                 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
606                 return;
607         }
608         if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
609             (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
610             (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
611                 /*
612                  * If the its a HB and it's random value is correct when can
613                  * confirm the destination.
614                  */
615                 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
616                 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
617                         stcb->asoc.primary_destination = r_net;
618                         r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
619                         f_net = TAILQ_FIRST(&stcb->asoc.nets);
620                         if (f_net != r_net) {
621                                 /*
622                                  * first one on the list is NOT the primary
623                                  * sctp_cmpaddr() is much more efficient if
624                                  * the primary is the first on the list,
625                                  * make it so.
626                                  */
627                                 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
628                                 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
629                         }
630                         req_prim = 1;
631                 }
632                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
633                     stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
634                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
635                     r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
636                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
637         }
638         old_error_counter = r_net->error_count;
639         r_net->error_count = 0;
640         r_net->hb_responded = 1;
641         tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
642         tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
643         /* Now lets do a RTO with this */
644         r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy,
645             SCTP_RTT_FROM_NON_DATA);
646         if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
647                 r_net->dest_state |= SCTP_ADDR_REACHABLE;
648                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
649                     0, (void *)r_net, SCTP_SO_NOT_LOCKED);
650         }
651         if (r_net->dest_state & SCTP_ADDR_PF) {
652                 r_net->dest_state &= ~SCTP_ADDR_PF;
653                 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
654         }
655         if (old_error_counter > 0) {
656                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
657                     stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
658                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
659         }
660         if (r_net == stcb->asoc.primary_destination) {
661                 if (stcb->asoc.alternate) {
662                         /* release the alternate, primary is good */
663                         sctp_free_remote_addr(stcb->asoc.alternate);
664                         stcb->asoc.alternate = NULL;
665                 }
666         }
667         /* Mobility adaptation */
668         if (req_prim) {
669                 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
670                     SCTP_MOBILITY_BASE) ||
671                     sctp_is_mobility_feature_on(stcb->sctp_ep,
672                     SCTP_MOBILITY_FASTHANDOFF)) &&
673                     sctp_is_mobility_feature_on(stcb->sctp_ep,
674                     SCTP_MOBILITY_PRIM_DELETED)) {
675
676                         sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
677                             stcb->sctp_ep, stcb, NULL,
678                             SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
679                         if (sctp_is_mobility_feature_on(stcb->sctp_ep,
680                             SCTP_MOBILITY_FASTHANDOFF)) {
681                                 sctp_assoc_immediate_retrans(stcb,
682                                     stcb->asoc.primary_destination);
683                         }
684                         if (sctp_is_mobility_feature_on(stcb->sctp_ep,
685                             SCTP_MOBILITY_BASE)) {
686                                 sctp_move_chunks_from_net(stcb,
687                                     stcb->asoc.deleted_primary);
688                         }
689                         sctp_delete_prim_timer(stcb->sctp_ep, stcb,
690                             stcb->asoc.deleted_primary);
691                 }
692         }
693 }
694
695 static int
696 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
697 {
698         /*
699          * return 0 means we want you to proceed with the abort non-zero
700          * means no abort processing
701          */
702         struct sctpasochead *head;
703
704         if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
705                 /* generate a new vtag and send init */
706                 LIST_REMOVE(stcb, sctp_asocs);
707                 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
708                 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
709                 /*
710                  * put it in the bucket in the vtag hash of assoc's for the
711                  * system
712                  */
713                 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
714                 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
715                 return (1);
716         }
717         if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
718                 /*
719                  * treat like a case where the cookie expired i.e.: - dump
720                  * current cookie. - generate a new vtag. - resend init.
721                  */
722                 /* generate a new vtag and send init */
723                 LIST_REMOVE(stcb, sctp_asocs);
724                 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
725                 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
726                 sctp_stop_all_cookie_timers(stcb);
727                 sctp_toss_old_cookies(stcb, &stcb->asoc);
728                 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
729                 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
730                 /*
731                  * put it in the bucket in the vtag hash of assoc's for the
732                  * system
733                  */
734                 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
735                 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
736                 return (1);
737         }
738         return (0);
739 }
740
741 static int
742 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
743     struct sctp_nets *net)
744 {
745         /*
746          * return 0 means we want you to proceed with the abort non-zero
747          * means no abort processing
748          */
749         if (stcb->asoc.auth_supported == 0) {
750                 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
751                 return (0);
752         }
753         sctp_asconf_send_nat_state_update(stcb, net);
754         return (1);
755 }
756
757
758 static void
759 sctp_handle_abort(struct sctp_abort_chunk *abort,
760     struct sctp_tcb *stcb, struct sctp_nets *net)
761 {
762 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
763         struct socket *so;
764 #endif
765         uint16_t len;
766         uint16_t error;
767
768         SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
769         if (stcb == NULL)
770                 return;
771
772         len = ntohs(abort->ch.chunk_length);
773         if (len > sizeof(struct sctp_chunkhdr)) {
774                 /*
775                  * Need to check the cause codes for our two magic nat
776                  * aborts which don't kill the assoc necessarily.
777                  */
778                 struct sctp_gen_error_cause *cause;
779
780                 cause = (struct sctp_gen_error_cause *)(abort + 1);
781                 error = ntohs(cause->code);
782                 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
783                         SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
784                             abort->ch.chunk_flags);
785                         if (sctp_handle_nat_colliding_state(stcb)) {
786                                 return;
787                         }
788                 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
789                         SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
790                             abort->ch.chunk_flags);
791                         if (sctp_handle_nat_missing_state(stcb, net)) {
792                                 return;
793                         }
794                 }
795         } else {
796                 error = 0;
797         }
798         /* stop any receive timers */
799         sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
800             SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
801         /* notify user of the abort and clean up... */
802         sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
803         /* free the tcb */
804         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
805         if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
806             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
807                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
808         }
809 #ifdef SCTP_ASOCLOG_OF_TSNS
810         sctp_print_out_track_log(stcb);
811 #endif
812 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
813         so = SCTP_INP_SO(stcb->sctp_ep);
814         atomic_add_int(&stcb->asoc.refcnt, 1);
815         SCTP_TCB_UNLOCK(stcb);
816         SCTP_SOCKET_LOCK(so, 1);
817         SCTP_TCB_LOCK(stcb);
818         atomic_subtract_int(&stcb->asoc.refcnt, 1);
819 #endif
820         stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
821         (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
822             SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
823 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
824         SCTP_SOCKET_UNLOCK(so, 1);
825 #endif
826         SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
827 }
828
829 static void
830 sctp_start_net_timers(struct sctp_tcb *stcb)
831 {
832         uint32_t cnt_hb_sent;
833         struct sctp_nets *net;
834
835         cnt_hb_sent = 0;
836         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
837                 /*
838                  * For each network start: 1) A pmtu timer. 2) A HB timer 3)
839                  * If the dest in unconfirmed send a hb as well if under
840                  * max_hb_burst have been sent.
841                  */
842                 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
843                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
844                 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
845                     (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
846                         sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
847                         cnt_hb_sent++;
848                 }
849         }
850         if (cnt_hb_sent) {
851                 sctp_chunk_output(stcb->sctp_ep, stcb,
852                     SCTP_OUTPUT_FROM_COOKIE_ACK,
853                     SCTP_SO_NOT_LOCKED);
854         }
855 }
856
857
858 static void
859 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
860     struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
861 {
862         struct sctp_association *asoc;
863         int some_on_streamwheel;
864         int old_state;
865 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
866         struct socket *so;
867 #endif
868
869         SCTPDBG(SCTP_DEBUG_INPUT2,
870             "sctp_handle_shutdown: handling SHUTDOWN\n");
871         if (stcb == NULL)
872                 return;
873         asoc = &stcb->asoc;
874         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
875             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
876                 return;
877         }
878         if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
879                 /* Shutdown NOT the expected size */
880                 return;
881         }
882         old_state = SCTP_GET_STATE(asoc);
883         sctp_update_acked(stcb, cp, abort_flag);
884         if (*abort_flag) {
885                 return;
886         }
887         if (asoc->control_pdapi) {
888                 /*
889                  * With a normal shutdown we assume the end of last record.
890                  */
891                 SCTP_INP_READ_LOCK(stcb->sctp_ep);
892                 if (asoc->control_pdapi->on_strm_q) {
893                         struct sctp_stream_in *strm;
894
895                         strm = &asoc->strmin[asoc->control_pdapi->sinfo_stream];
896                         if (asoc->control_pdapi->on_strm_q == SCTP_ON_UNORDERED) {
897                                 /* Unordered */
898                                 TAILQ_REMOVE(&strm->uno_inqueue, asoc->control_pdapi, next_instrm);
899                                 asoc->control_pdapi->on_strm_q = 0;
900                         } else if (asoc->control_pdapi->on_strm_q == SCTP_ON_ORDERED) {
901                                 /* Ordered */
902                                 TAILQ_REMOVE(&strm->inqueue, asoc->control_pdapi, next_instrm);
903                                 asoc->control_pdapi->on_strm_q = 0;
904 #ifdef INVARIANTS
905                         } else {
906                                 panic("Unknown state on ctrl:%p on_strm_q:%d",
907                                     asoc->control_pdapi,
908                                     asoc->control_pdapi->on_strm_q);
909 #endif
910                         }
911                 }
912                 asoc->control_pdapi->end_added = 1;
913                 asoc->control_pdapi->pdapi_aborted = 1;
914                 asoc->control_pdapi = NULL;
915                 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
916 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
917                 so = SCTP_INP_SO(stcb->sctp_ep);
918                 atomic_add_int(&stcb->asoc.refcnt, 1);
919                 SCTP_TCB_UNLOCK(stcb);
920                 SCTP_SOCKET_LOCK(so, 1);
921                 SCTP_TCB_LOCK(stcb);
922                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
923                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
924                         /* assoc was freed while we were unlocked */
925                         SCTP_SOCKET_UNLOCK(so, 1);
926                         return;
927                 }
928 #endif
929                 if (stcb->sctp_socket) {
930                         sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
931                 }
932 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
933                 SCTP_SOCKET_UNLOCK(so, 1);
934 #endif
935         }
936         /* goto SHUTDOWN_RECEIVED state to block new requests */
937         if (stcb->sctp_socket) {
938                 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
939                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
940                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
941                         SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
942                         SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
943                         /*
944                          * notify upper layer that peer has initiated a
945                          * shutdown
946                          */
947                         sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
948
949                         /* reset time */
950                         (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
951                 }
952         }
953         if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
954                 /*
955                  * stop the shutdown timer, since we WILL move to
956                  * SHUTDOWN-ACK-SENT.
957                  */
958                 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
959                     net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
960         }
961         /* Now is there unsent data on a stream somewhere? */
962         some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
963
964         if (!TAILQ_EMPTY(&asoc->send_queue) ||
965             !TAILQ_EMPTY(&asoc->sent_queue) ||
966             some_on_streamwheel) {
967                 /* By returning we will push more data out */
968                 return;
969         } else {
970                 /* no outstanding data to send, so move on... */
971                 /* send SHUTDOWN-ACK */
972                 /* move to SHUTDOWN-ACK-SENT state */
973                 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
974                     (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
975                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
976                 }
977                 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
978                 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
979                         SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
980                         sctp_stop_timers_for_shutdown(stcb);
981                         sctp_send_shutdown_ack(stcb, net);
982                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
983                             stcb->sctp_ep, stcb, net);
984                 } else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
985                         sctp_send_shutdown_ack(stcb, net);
986                 }
987         }
988 }
989
990 static void
991 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
992     struct sctp_tcb *stcb,
993     struct sctp_nets *net)
994 {
995         struct sctp_association *asoc;
996 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
997         struct socket *so;
998
999         so = SCTP_INP_SO(stcb->sctp_ep);
1000 #endif
1001         SCTPDBG(SCTP_DEBUG_INPUT2,
1002             "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
1003         if (stcb == NULL)
1004                 return;
1005
1006         asoc = &stcb->asoc;
1007         /* process according to association state */
1008         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
1009             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
1010                 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
1011                 sctp_send_shutdown_complete(stcb, net, 1);
1012                 SCTP_TCB_UNLOCK(stcb);
1013                 return;
1014         }
1015         if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
1016             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1017                 /* unexpected SHUTDOWN-ACK... so ignore... */
1018                 SCTP_TCB_UNLOCK(stcb);
1019                 return;
1020         }
1021         if (asoc->control_pdapi) {
1022                 /*
1023                  * With a normal shutdown we assume the end of last record.
1024                  */
1025                 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1026                 asoc->control_pdapi->end_added = 1;
1027                 asoc->control_pdapi->pdapi_aborted = 1;
1028                 asoc->control_pdapi = NULL;
1029                 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1030 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1031                 atomic_add_int(&stcb->asoc.refcnt, 1);
1032                 SCTP_TCB_UNLOCK(stcb);
1033                 SCTP_SOCKET_LOCK(so, 1);
1034                 SCTP_TCB_LOCK(stcb);
1035                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1036                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1037                         /* assoc was freed while we were unlocked */
1038                         SCTP_SOCKET_UNLOCK(so, 1);
1039                         return;
1040                 }
1041 #endif
1042                 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1043 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1044                 SCTP_SOCKET_UNLOCK(so, 1);
1045 #endif
1046         }
1047 #ifdef INVARIANTS
1048         if (!TAILQ_EMPTY(&asoc->send_queue) ||
1049             !TAILQ_EMPTY(&asoc->sent_queue) ||
1050             sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
1051                 panic("Queues are not empty when handling SHUTDOWN-ACK");
1052         }
1053 #endif
1054         /* stop the timer */
1055         sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
1056             SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1057         /* send SHUTDOWN-COMPLETE */
1058         sctp_send_shutdown_complete(stcb, net, 0);
1059         /* notify upper layer protocol */
1060         if (stcb->sctp_socket) {
1061                 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1062                     (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1063                         stcb->sctp_socket->so_snd.sb_cc = 0;
1064                 }
1065                 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1066         }
1067         SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1068         /* free the TCB but first save off the ep */
1069 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1070         atomic_add_int(&stcb->asoc.refcnt, 1);
1071         SCTP_TCB_UNLOCK(stcb);
1072         SCTP_SOCKET_LOCK(so, 1);
1073         SCTP_TCB_LOCK(stcb);
1074         atomic_subtract_int(&stcb->asoc.refcnt, 1);
1075 #endif
1076         (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1077             SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1078 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1079         SCTP_SOCKET_UNLOCK(so, 1);
1080 #endif
1081 }
1082
1083 /*
1084  * Skip past the param header and then we will find the chunk that caused the
1085  * problem. There are two possibilities ASCONF or FWD-TSN other than that and
1086  * our peer must be broken.
1087  */
1088 static void
1089 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1090     struct sctp_nets *net)
1091 {
1092         struct sctp_chunkhdr *chk;
1093
1094         chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1095         switch (chk->chunk_type) {
1096         case SCTP_ASCONF_ACK:
1097         case SCTP_ASCONF:
1098                 sctp_asconf_cleanup(stcb, net);
1099                 break;
1100         case SCTP_IFORWARD_CUM_TSN:
1101         case SCTP_FORWARD_CUM_TSN:
1102                 stcb->asoc.prsctp_supported = 0;
1103                 break;
1104         default:
1105                 SCTPDBG(SCTP_DEBUG_INPUT2,
1106                     "Peer does not support chunk type %d(%x)??\n",
1107                     chk->chunk_type, (uint32_t)chk->chunk_type);
1108                 break;
1109         }
1110 }
1111
1112 /*
1113  * Skip past the param header and then we will find the param that caused the
1114  * problem.  There are a number of param's in a ASCONF OR the prsctp param
1115  * these will turn of specific features.
1116  * XXX: Is this the right thing to do?
1117  */
1118 static void
1119 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1120 {
1121         struct sctp_paramhdr *pbad;
1122
1123         pbad = phdr + 1;
1124         switch (ntohs(pbad->param_type)) {
1125                 /* pr-sctp draft */
1126         case SCTP_PRSCTP_SUPPORTED:
1127                 stcb->asoc.prsctp_supported = 0;
1128                 break;
1129         case SCTP_SUPPORTED_CHUNK_EXT:
1130                 break;
1131                 /* draft-ietf-tsvwg-addip-sctp */
1132         case SCTP_HAS_NAT_SUPPORT:
1133                 stcb->asoc.peer_supports_nat = 0;
1134                 break;
1135         case SCTP_ADD_IP_ADDRESS:
1136         case SCTP_DEL_IP_ADDRESS:
1137         case SCTP_SET_PRIM_ADDR:
1138                 stcb->asoc.asconf_supported = 0;
1139                 break;
1140         case SCTP_SUCCESS_REPORT:
1141         case SCTP_ERROR_CAUSE_IND:
1142                 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1143                 SCTPDBG(SCTP_DEBUG_INPUT2,
1144                     "Turning off ASCONF to this strange peer\n");
1145                 stcb->asoc.asconf_supported = 0;
1146                 break;
1147         default:
1148                 SCTPDBG(SCTP_DEBUG_INPUT2,
1149                     "Peer does not support param type %d(%x)??\n",
1150                     pbad->param_type, (uint32_t)pbad->param_type);
1151                 break;
1152         }
1153 }
1154
1155 static int
1156 sctp_handle_error(struct sctp_chunkhdr *ch,
1157     struct sctp_tcb *stcb, struct sctp_nets *net)
1158 {
1159         int chklen;
1160         struct sctp_paramhdr *phdr;
1161         uint16_t error, error_type;
1162         uint16_t error_len;
1163         struct sctp_association *asoc;
1164         int adjust;
1165 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1166         struct socket *so;
1167 #endif
1168
1169         /* parse through all of the errors and process */
1170         asoc = &stcb->asoc;
1171         phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1172             sizeof(struct sctp_chunkhdr));
1173         chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1174         error = 0;
1175         while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1176                 /* Process an Error Cause */
1177                 error_type = ntohs(phdr->param_type);
1178                 error_len = ntohs(phdr->param_length);
1179                 if ((error_len > chklen) || (error_len == 0)) {
1180                         /* invalid param length for this param */
1181                         SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1182                             chklen, error_len);
1183                         return (0);
1184                 }
1185                 if (error == 0) {
1186                         /* report the first error cause */
1187                         error = error_type;
1188                 }
1189                 switch (error_type) {
1190                 case SCTP_CAUSE_INVALID_STREAM:
1191                 case SCTP_CAUSE_MISSING_PARAM:
1192                 case SCTP_CAUSE_INVALID_PARAM:
1193                 case SCTP_CAUSE_NO_USER_DATA:
1194                         SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1195                             error_type);
1196                         break;
1197                 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1198                         SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1199                             ch->chunk_flags);
1200                         if (sctp_handle_nat_colliding_state(stcb)) {
1201                                 return (0);
1202                         }
1203                         break;
1204                 case SCTP_CAUSE_NAT_MISSING_STATE:
1205                         SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1206                             ch->chunk_flags);
1207                         if (sctp_handle_nat_missing_state(stcb, net)) {
1208                                 return (0);
1209                         }
1210                         break;
1211                 case SCTP_CAUSE_STALE_COOKIE:
1212                         /*
1213                          * We only act if we have echoed a cookie and are
1214                          * waiting.
1215                          */
1216                         if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1217                                 int *p;
1218
1219                                 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1220                                 /* Save the time doubled */
1221                                 asoc->cookie_preserve_req = ntohl(*p) << 1;
1222                                 asoc->stale_cookie_count++;
1223                                 if (asoc->stale_cookie_count >
1224                                     asoc->max_init_times) {
1225                                         sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1226                                         /* now free the asoc */
1227 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1228                                         so = SCTP_INP_SO(stcb->sctp_ep);
1229                                         atomic_add_int(&stcb->asoc.refcnt, 1);
1230                                         SCTP_TCB_UNLOCK(stcb);
1231                                         SCTP_SOCKET_LOCK(so, 1);
1232                                         SCTP_TCB_LOCK(stcb);
1233                                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
1234 #endif
1235                                         (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1236                                             SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1237 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1238                                         SCTP_SOCKET_UNLOCK(so, 1);
1239 #endif
1240                                         return (-1);
1241                                 }
1242                                 /* blast back to INIT state */
1243                                 sctp_toss_old_cookies(stcb, &stcb->asoc);
1244                                 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1245                                 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1246                                 sctp_stop_all_cookie_timers(stcb);
1247                                 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1248                         }
1249                         break;
1250                 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1251                         /*
1252                          * Nothing we can do here, we don't do hostname
1253                          * addresses so if the peer does not like my IPv6
1254                          * (or IPv4 for that matter) it does not matter. If
1255                          * they don't support that type of address, they can
1256                          * NOT possibly get that packet type... i.e. with no
1257                          * IPv6 you can't receive a IPv6 packet. so we can
1258                          * safely ignore this one. If we ever added support
1259                          * for HOSTNAME Addresses, then we would need to do
1260                          * something here.
1261                          */
1262                         break;
1263                 case SCTP_CAUSE_UNRECOG_CHUNK:
1264                         sctp_process_unrecog_chunk(stcb, phdr, net);
1265                         break;
1266                 case SCTP_CAUSE_UNRECOG_PARAM:
1267                         sctp_process_unrecog_param(stcb, phdr);
1268                         break;
1269                 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1270                         /*
1271                          * We ignore this since the timer will drive out a
1272                          * new cookie anyway and there timer will drive us
1273                          * to send a SHUTDOWN_COMPLETE. We can't send one
1274                          * here since we don't have their tag.
1275                          */
1276                         break;
1277                 case SCTP_CAUSE_DELETING_LAST_ADDR:
1278                 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1279                 case SCTP_CAUSE_DELETING_SRC_ADDR:
1280                         /*
1281                          * We should NOT get these here, but in a
1282                          * ASCONF-ACK.
1283                          */
1284                         SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1285                             error_type);
1286                         break;
1287                 case SCTP_CAUSE_OUT_OF_RESC:
1288                         /*
1289                          * And what, pray tell do we do with the fact that
1290                          * the peer is out of resources? Not really sure we
1291                          * could do anything but abort. I suspect this
1292                          * should have came WITH an abort instead of in a
1293                          * OP-ERROR.
1294                          */
1295                         break;
1296                 default:
1297                         SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1298                             error_type);
1299                         break;
1300                 }
1301                 adjust = SCTP_SIZE32(error_len);
1302                 chklen -= adjust;
1303                 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1304         }
1305         sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED);
1306         return (0);
1307 }
1308
1309 static int
1310 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1311     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1312     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1313     struct sctp_nets *net, int *abort_no_unlock,
1314     uint8_t mflowtype, uint32_t mflowid,
1315     uint32_t vrf_id)
1316 {
1317         struct sctp_init_ack *init_ack;
1318         struct mbuf *op_err;
1319
1320         SCTPDBG(SCTP_DEBUG_INPUT2,
1321             "sctp_handle_init_ack: handling INIT-ACK\n");
1322
1323         if (stcb == NULL) {
1324                 SCTPDBG(SCTP_DEBUG_INPUT2,
1325                     "sctp_handle_init_ack: TCB is null\n");
1326                 return (-1);
1327         }
1328         if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1329                 /* Invalid length */
1330                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1331                 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1332                     src, dst, sh, op_err,
1333                     mflowtype, mflowid,
1334                     vrf_id, net->port);
1335                 *abort_no_unlock = 1;
1336                 return (-1);
1337         }
1338         init_ack = &cp->init;
1339         /* validate parameters */
1340         if (init_ack->initiate_tag == 0) {
1341                 /* protocol error... send an abort */
1342                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1343                 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1344                     src, dst, sh, op_err,
1345                     mflowtype, mflowid,
1346                     vrf_id, net->port);
1347                 *abort_no_unlock = 1;
1348                 return (-1);
1349         }
1350         if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1351                 /* protocol error... send an abort */
1352                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1353                 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1354                     src, dst, sh, op_err,
1355                     mflowtype, mflowid,
1356                     vrf_id, net->port);
1357                 *abort_no_unlock = 1;
1358                 return (-1);
1359         }
1360         if (init_ack->num_inbound_streams == 0) {
1361                 /* protocol error... send an abort */
1362                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1363                 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1364                     src, dst, sh, op_err,
1365                     mflowtype, mflowid,
1366                     vrf_id, net->port);
1367                 *abort_no_unlock = 1;
1368                 return (-1);
1369         }
1370         if (init_ack->num_outbound_streams == 0) {
1371                 /* protocol error... send an abort */
1372                 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1373                 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1374                     src, dst, sh, op_err,
1375                     mflowtype, mflowid,
1376                     vrf_id, net->port);
1377                 *abort_no_unlock = 1;
1378                 return (-1);
1379         }
1380         /* process according to association state... */
1381         switch (stcb->asoc.state & SCTP_STATE_MASK) {
1382         case SCTP_STATE_COOKIE_WAIT:
1383                 /* this is the expected state for this chunk */
1384                 /* process the INIT-ACK parameters */
1385                 if (stcb->asoc.primary_destination->dest_state &
1386                     SCTP_ADDR_UNCONFIRMED) {
1387                         /*
1388                          * The primary is where we sent the INIT, we can
1389                          * always consider it confirmed when the INIT-ACK is
1390                          * returned. Do this before we load addresses
1391                          * though.
1392                          */
1393                         stcb->asoc.primary_destination->dest_state &=
1394                             ~SCTP_ADDR_UNCONFIRMED;
1395                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1396                             stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1397                 }
1398                 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1399                     net, abort_no_unlock,
1400                     mflowtype, mflowid,
1401                     vrf_id) < 0) {
1402                         /* error in parsing parameters */
1403                         return (-1);
1404                 }
1405                 /* update our state */
1406                 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1407                 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1408
1409                 /* reset the RTO calc */
1410                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1411                         sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1412                             stcb->asoc.overall_error_count,
1413                             0,
1414                             SCTP_FROM_SCTP_INPUT,
1415                             __LINE__);
1416                 }
1417                 stcb->asoc.overall_error_count = 0;
1418                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1419                 /*
1420                  * collapse the init timer back in case of a exponential
1421                  * backoff
1422                  */
1423                 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1424                     stcb, net);
1425                 /*
1426                  * the send at the end of the inbound data processing will
1427                  * cause the cookie to be sent
1428                  */
1429                 break;
1430         case SCTP_STATE_SHUTDOWN_SENT:
1431                 /* incorrect state... discard */
1432                 break;
1433         case SCTP_STATE_COOKIE_ECHOED:
1434                 /* incorrect state... discard */
1435                 break;
1436         case SCTP_STATE_OPEN:
1437                 /* incorrect state... discard */
1438                 break;
1439         case SCTP_STATE_EMPTY:
1440         case SCTP_STATE_INUSE:
1441         default:
1442                 /* incorrect state... discard */
1443                 return (-1);
1444                 break;
1445         }
1446         SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1447         return (0);
1448 }
1449
1450 static struct sctp_tcb *
1451 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1452     struct sockaddr *src, struct sockaddr *dst,
1453     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1454     struct sctp_inpcb *inp, struct sctp_nets **netp,
1455     struct sockaddr *init_src, int *notification,
1456     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1457     uint8_t mflowtype, uint32_t mflowid,
1458     uint32_t vrf_id, uint16_t port);
1459
1460
1461 /*
1462  * handle a state cookie for an existing association m: input packet mbuf
1463  * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1464  * "split" mbuf and the cookie signature does not exist offset: offset into
1465  * mbuf to the cookie-echo chunk
1466  */
1467 static struct sctp_tcb *
1468 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1469     struct sockaddr *src, struct sockaddr *dst,
1470     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1471     struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1472     struct sockaddr *init_src, int *notification,
1473     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1474     uint8_t mflowtype, uint32_t mflowid,
1475     uint32_t vrf_id, uint16_t port)
1476 {
1477         struct sctp_association *asoc;
1478         struct sctp_init_chunk *init_cp, init_buf;
1479         struct sctp_init_ack_chunk *initack_cp, initack_buf;
1480         struct sctp_nets *net;
1481         struct mbuf *op_err;
1482         int init_offset, initack_offset, i;
1483         int retval;
1484         int spec_flag = 0;
1485         uint32_t how_indx;
1486 #if defined(SCTP_DETAILED_STR_STATS)
1487         int j;
1488 #endif
1489
1490         net = *netp;
1491         /* I know that the TCB is non-NULL from the caller */
1492         asoc = &stcb->asoc;
1493         for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1494                 if (asoc->cookie_how[how_indx] == 0)
1495                         break;
1496         }
1497         if (how_indx < sizeof(asoc->cookie_how)) {
1498                 asoc->cookie_how[how_indx] = 1;
1499         }
1500         if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1501                 /* SHUTDOWN came in after sending INIT-ACK */
1502                 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1503                 op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1504                 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1505                     mflowtype, mflowid, inp->fibnum,
1506                     vrf_id, net->port);
1507                 if (how_indx < sizeof(asoc->cookie_how))
1508                         asoc->cookie_how[how_indx] = 2;
1509                 return (NULL);
1510         }
1511         /*
1512          * find and validate the INIT chunk in the cookie (peer's info) the
1513          * INIT should start after the cookie-echo header struct (chunk
1514          * header, state cookie header struct)
1515          */
1516         init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1517
1518         init_cp = (struct sctp_init_chunk *)
1519             sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1520             (uint8_t *)&init_buf);
1521         if (init_cp == NULL) {
1522                 /* could not pull a INIT chunk in cookie */
1523                 return (NULL);
1524         }
1525         if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1526                 return (NULL);
1527         }
1528         /*
1529          * find and validate the INIT-ACK chunk in the cookie (my info) the
1530          * INIT-ACK follows the INIT chunk
1531          */
1532         initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1533         initack_cp = (struct sctp_init_ack_chunk *)
1534             sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1535             (uint8_t *)&initack_buf);
1536         if (initack_cp == NULL) {
1537                 /* could not pull INIT-ACK chunk in cookie */
1538                 return (NULL);
1539         }
1540         if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1541                 return (NULL);
1542         }
1543         if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1544             (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1545                 /*
1546                  * case D in Section 5.2.4 Table 2: MMAA process accordingly
1547                  * to get into the OPEN state
1548                  */
1549                 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1550                         /*-
1551                          * Opps, this means that we somehow generated two vtag's
1552                          * the same. I.e. we did:
1553                          *  Us               Peer
1554                          *   <---INIT(tag=a)------
1555                          *   ----INIT-ACK(tag=t)-->
1556                          *   ----INIT(tag=t)------> *1
1557                          *   <---INIT-ACK(tag=a)---
1558                          *   <----CE(tag=t)------------- *2
1559                          *
1560                          * At point *1 we should be generating a different
1561                          * tag t'. Which means we would throw away the CE and send
1562                          * ours instead. Basically this is case C (throw away side).
1563                          */
1564                         if (how_indx < sizeof(asoc->cookie_how))
1565                                 asoc->cookie_how[how_indx] = 17;
1566                         return (NULL);
1567
1568                 }
1569                 switch (SCTP_GET_STATE(asoc)) {
1570                 case SCTP_STATE_COOKIE_WAIT:
1571                 case SCTP_STATE_COOKIE_ECHOED:
1572                         /*
1573                          * INIT was sent but got a COOKIE_ECHO with the
1574                          * correct tags... just accept it...but we must
1575                          * process the init so that we can make sure we have
1576                          * the right seq no's.
1577                          */
1578                         /* First we must process the INIT !! */
1579                         retval = sctp_process_init(init_cp, stcb);
1580                         if (retval < 0) {
1581                                 if (how_indx < sizeof(asoc->cookie_how))
1582                                         asoc->cookie_how[how_indx] = 3;
1583                                 return (NULL);
1584                         }
1585                         /* we have already processed the INIT so no problem */
1586                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1587                             stcb, net,
1588                             SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1589                         sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1590                             stcb, net,
1591                             SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1592                         /* update current state */
1593                         if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1594                                 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1595                         else
1596                                 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1597
1598                         SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1599                         if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1600                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1601                                     stcb->sctp_ep, stcb, asoc->primary_destination);
1602                         }
1603                         SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1604                         sctp_stop_all_cookie_timers(stcb);
1605                         if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1606                             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1607                             (inp->sctp_socket->so_qlimit == 0)
1608                             ) {
1609 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1610                                 struct socket *so;
1611 #endif
1612                                 /*
1613                                  * Here is where collision would go if we
1614                                  * did a connect() and instead got a
1615                                  * init/init-ack/cookie done before the
1616                                  * init-ack came back..
1617                                  */
1618                                 stcb->sctp_ep->sctp_flags |=
1619                                     SCTP_PCB_FLAGS_CONNECTED;
1620 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1621                                 so = SCTP_INP_SO(stcb->sctp_ep);
1622                                 atomic_add_int(&stcb->asoc.refcnt, 1);
1623                                 SCTP_TCB_UNLOCK(stcb);
1624                                 SCTP_SOCKET_LOCK(so, 1);
1625                                 SCTP_TCB_LOCK(stcb);
1626                                 atomic_add_int(&stcb->asoc.refcnt, -1);
1627                                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1628                                         SCTP_SOCKET_UNLOCK(so, 1);
1629                                         return (NULL);
1630                                 }
1631 #endif
1632                                 soisconnected(stcb->sctp_socket);
1633 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1634                                 SCTP_SOCKET_UNLOCK(so, 1);
1635 #endif
1636                         }
1637                         /* notify upper layer */
1638                         *notification = SCTP_NOTIFY_ASSOC_UP;
1639                         /*
1640                          * since we did not send a HB make sure we don't
1641                          * double things
1642                          */
1643                         net->hb_responded = 1;
1644                         net->RTO = sctp_calculate_rto(stcb, asoc, net,
1645                             &cookie->time_entered,
1646                             sctp_align_unsafe_makecopy,
1647                             SCTP_RTT_FROM_NON_DATA);
1648
1649                         if (stcb->asoc.sctp_autoclose_ticks &&
1650                             (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1651                                 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1652                                     inp, stcb, NULL);
1653                         }
1654                         break;
1655                 default:
1656                         /*
1657                          * we're in the OPEN state (or beyond), so peer must
1658                          * have simply lost the COOKIE-ACK
1659                          */
1660                         break;
1661                 }               /* end switch */
1662                 sctp_stop_all_cookie_timers(stcb);
1663                 /*
1664                  * We ignore the return code here.. not sure if we should
1665                  * somehow abort.. but we do have an existing asoc. This
1666                  * really should not fail.
1667                  */
1668                 if (sctp_load_addresses_from_init(stcb, m,
1669                     init_offset + sizeof(struct sctp_init_chunk),
1670                     initack_offset, src, dst, init_src, stcb->asoc.port)) {
1671                         if (how_indx < sizeof(asoc->cookie_how))
1672                                 asoc->cookie_how[how_indx] = 4;
1673                         return (NULL);
1674                 }
1675                 /* respond with a COOKIE-ACK */
1676                 sctp_toss_old_cookies(stcb, asoc);
1677                 sctp_send_cookie_ack(stcb);
1678                 if (how_indx < sizeof(asoc->cookie_how))
1679                         asoc->cookie_how[how_indx] = 5;
1680                 return (stcb);
1681         }
1682         if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1683             ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1684             cookie->tie_tag_my_vtag == 0 &&
1685             cookie->tie_tag_peer_vtag == 0) {
1686                 /*
1687                  * case C in Section 5.2.4 Table 2: XMOO silently discard
1688                  */
1689                 if (how_indx < sizeof(asoc->cookie_how))
1690                         asoc->cookie_how[how_indx] = 6;
1691                 return (NULL);
1692         }
1693         /*
1694          * If nat support, and the below and stcb is established, send back
1695          * a ABORT(colliding state) if we are established.
1696          */
1697         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1698             (asoc->peer_supports_nat) &&
1699             ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1700             ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1701             (asoc->peer_vtag == 0)))) {
1702                 /*
1703                  * Special case - Peer's support nat. We may have two init's
1704                  * that we gave out the same tag on since one was not
1705                  * established.. i.e. we get INIT from host-1 behind the nat
1706                  * and we respond tag-a, we get a INIT from host-2 behind
1707                  * the nat and we get tag-a again. Then we bring up host-1
1708                  * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1709                  * Now we have colliding state. We must send an abort here
1710                  * with colliding state indication.
1711                  */
1712                 op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1713                 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1714                     mflowtype, mflowid, inp->fibnum,
1715                     vrf_id, port);
1716                 return (NULL);
1717         }
1718         if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1719             ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1720             (asoc->peer_vtag == 0))) {
1721                 /*
1722                  * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1723                  * should be ok, re-accept peer info
1724                  */
1725                 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1726                         /*
1727                          * Extension of case C. If we hit this, then the
1728                          * random number generator returned the same vtag
1729                          * when we first sent our INIT-ACK and when we later
1730                          * sent our INIT. The side with the seq numbers that
1731                          * are different will be the one that normnally
1732                          * would have hit case C. This in effect "extends"
1733                          * our vtags in this collision case to be 64 bits.
1734                          * The same collision could occur aka you get both
1735                          * vtag and seq number the same twice in a row.. but
1736                          * is much less likely. If it did happen then we
1737                          * would proceed through and bring up the assoc.. we
1738                          * may end up with the wrong stream setup however..
1739                          * which would be bad.. but there is no way to
1740                          * tell.. until we send on a stream that does not
1741                          * exist :-)
1742                          */
1743                         if (how_indx < sizeof(asoc->cookie_how))
1744                                 asoc->cookie_how[how_indx] = 7;
1745
1746                         return (NULL);
1747                 }
1748                 if (how_indx < sizeof(asoc->cookie_how))
1749                         asoc->cookie_how[how_indx] = 8;
1750                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1751                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1752                 sctp_stop_all_cookie_timers(stcb);
1753                 /*
1754                  * since we did not send a HB make sure we don't double
1755                  * things
1756                  */
1757                 net->hb_responded = 1;
1758                 if (stcb->asoc.sctp_autoclose_ticks &&
1759                     sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1760                         sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1761                             NULL);
1762                 }
1763                 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1764                 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1765
1766                 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1767                         /*
1768                          * Ok the peer probably discarded our data (if we
1769                          * echoed a cookie+data). So anything on the
1770                          * sent_queue should be marked for retransmit, we
1771                          * may not get something to kick us so it COULD
1772                          * still take a timeout to move these.. but it can't
1773                          * hurt to mark them.
1774                          */
1775                         struct sctp_tmit_chunk *chk;
1776
1777                         TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1778                                 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1779                                         chk->sent = SCTP_DATAGRAM_RESEND;
1780                                         sctp_flight_size_decrease(chk);
1781                                         sctp_total_flight_decrease(stcb, chk);
1782                                         sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1783                                         spec_flag++;
1784                                 }
1785                         }
1786
1787                 }
1788                 /* process the INIT info (peer's info) */
1789                 retval = sctp_process_init(init_cp, stcb);
1790                 if (retval < 0) {
1791                         if (how_indx < sizeof(asoc->cookie_how))
1792                                 asoc->cookie_how[how_indx] = 9;
1793                         return (NULL);
1794                 }
1795                 if (sctp_load_addresses_from_init(stcb, m,
1796                     init_offset + sizeof(struct sctp_init_chunk),
1797                     initack_offset, src, dst, init_src, stcb->asoc.port)) {
1798                         if (how_indx < sizeof(asoc->cookie_how))
1799                                 asoc->cookie_how[how_indx] = 10;
1800                         return (NULL);
1801                 }
1802                 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1803                     (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1804                         *notification = SCTP_NOTIFY_ASSOC_UP;
1805
1806                         if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1807                             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1808                             (inp->sctp_socket->so_qlimit == 0)) {
1809 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1810                                 struct socket *so;
1811 #endif
1812                                 stcb->sctp_ep->sctp_flags |=
1813                                     SCTP_PCB_FLAGS_CONNECTED;
1814 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1815                                 so = SCTP_INP_SO(stcb->sctp_ep);
1816                                 atomic_add_int(&stcb->asoc.refcnt, 1);
1817                                 SCTP_TCB_UNLOCK(stcb);
1818                                 SCTP_SOCKET_LOCK(so, 1);
1819                                 SCTP_TCB_LOCK(stcb);
1820                                 atomic_add_int(&stcb->asoc.refcnt, -1);
1821                                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1822                                         SCTP_SOCKET_UNLOCK(so, 1);
1823                                         return (NULL);
1824                                 }
1825 #endif
1826                                 soisconnected(stcb->sctp_socket);
1827 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1828                                 SCTP_SOCKET_UNLOCK(so, 1);
1829 #endif
1830                         }
1831                         if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1832                                 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1833                         else
1834                                 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1835                         SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1836                 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1837                         SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1838                 } else {
1839                         SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1840                 }
1841                 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1842                 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1843                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1844                             stcb->sctp_ep, stcb, asoc->primary_destination);
1845                 }
1846                 sctp_stop_all_cookie_timers(stcb);
1847                 sctp_toss_old_cookies(stcb, asoc);
1848                 sctp_send_cookie_ack(stcb);
1849                 if (spec_flag) {
1850                         /*
1851                          * only if we have retrans set do we do this. What
1852                          * this call does is get only the COOKIE-ACK out and
1853                          * then when we return the normal call to
1854                          * sctp_chunk_output will get the retrans out behind
1855                          * this.
1856                          */
1857                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1858                 }
1859                 if (how_indx < sizeof(asoc->cookie_how))
1860                         asoc->cookie_how[how_indx] = 11;
1861
1862                 return (stcb);
1863         }
1864         if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1865             ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1866             cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1867             cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1868             cookie->tie_tag_peer_vtag != 0) {
1869                 struct sctpasochead *head;
1870 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1871                 struct socket *so;
1872 #endif
1873
1874                 if (asoc->peer_supports_nat) {
1875                         /*
1876                          * This is a gross gross hack. Just call the
1877                          * cookie_new code since we are allowing a duplicate
1878                          * association. I hope this works...
1879                          */
1880                         return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1881                             sh, cookie, cookie_len,
1882                             inp, netp, init_src, notification,
1883                             auth_skipped, auth_offset, auth_len,
1884                             mflowtype, mflowid,
1885                             vrf_id, port));
1886                 }
1887                 /*
1888                  * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1889                  */
1890                 /* temp code */
1891                 if (how_indx < sizeof(asoc->cookie_how))
1892                         asoc->cookie_how[how_indx] = 12;
1893                 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
1894                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1895                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1896                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
1897
1898                 /* notify upper layer */
1899                 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1900                 atomic_add_int(&stcb->asoc.refcnt, 1);
1901                 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1902                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1903                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1904                         SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1905                 }
1906                 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1907                         SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1908                 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1909                         SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1910                 }
1911                 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1912                         SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1913                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1914                             stcb->sctp_ep, stcb, asoc->primary_destination);
1915
1916                 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1917                         /* move to OPEN state, if not in SHUTDOWN_SENT */
1918                         SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1919                 }
1920                 asoc->pre_open_streams =
1921                     ntohs(initack_cp->init.num_outbound_streams);
1922                 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1923                 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1924                 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1925
1926                 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1927
1928                 asoc->str_reset_seq_in = asoc->init_seq_number;
1929
1930                 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1931                 if (asoc->mapping_array) {
1932                         memset(asoc->mapping_array, 0,
1933                             asoc->mapping_array_size);
1934                 }
1935                 if (asoc->nr_mapping_array) {
1936                         memset(asoc->nr_mapping_array, 0,
1937                             asoc->mapping_array_size);
1938                 }
1939                 SCTP_TCB_UNLOCK(stcb);
1940 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1941                 so = SCTP_INP_SO(stcb->sctp_ep);
1942                 SCTP_SOCKET_LOCK(so, 1);
1943 #endif
1944                 SCTP_INP_INFO_WLOCK();
1945                 SCTP_INP_WLOCK(stcb->sctp_ep);
1946                 SCTP_TCB_LOCK(stcb);
1947                 atomic_add_int(&stcb->asoc.refcnt, -1);
1948                 /* send up all the data */
1949                 SCTP_TCB_SEND_LOCK(stcb);
1950
1951                 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED);
1952                 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1953                         stcb->asoc.strmout[i].chunks_on_queues = 0;
1954 #if defined(SCTP_DETAILED_STR_STATS)
1955                         for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1956                                 asoc->strmout[i].abandoned_sent[j] = 0;
1957                                 asoc->strmout[i].abandoned_unsent[j] = 0;
1958                         }
1959 #else
1960                         asoc->strmout[i].abandoned_sent[0] = 0;
1961                         asoc->strmout[i].abandoned_unsent[0] = 0;
1962 #endif
1963                         stcb->asoc.strmout[i].sid = i;
1964                         stcb->asoc.strmout[i].next_mid_ordered = 0;
1965                         stcb->asoc.strmout[i].next_mid_unordered = 0;
1966                         stcb->asoc.strmout[i].last_msg_incomplete = 0;
1967                 }
1968                 /* process the INIT-ACK info (my info) */
1969                 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1970                 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1971
1972                 /* pull from vtag hash */
1973                 LIST_REMOVE(stcb, sctp_asocs);
1974                 /* re-insert to new vtag position */
1975                 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1976                     SCTP_BASE_INFO(hashasocmark))];
1977                 /*
1978                  * put it in the bucket in the vtag hash of assoc's for the
1979                  * system
1980                  */
1981                 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1982
1983                 SCTP_TCB_SEND_UNLOCK(stcb);
1984                 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1985                 SCTP_INP_INFO_WUNLOCK();
1986 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1987                 SCTP_SOCKET_UNLOCK(so, 1);
1988 #endif
1989                 asoc->total_flight = 0;
1990                 asoc->total_flight_count = 0;
1991                 /* process the INIT info (peer's info) */
1992                 retval = sctp_process_init(init_cp, stcb);
1993                 if (retval < 0) {
1994                         if (how_indx < sizeof(asoc->cookie_how))
1995                                 asoc->cookie_how[how_indx] = 13;
1996
1997                         return (NULL);
1998                 }
1999                 /*
2000                  * since we did not send a HB make sure we don't double
2001                  * things
2002                  */
2003                 net->hb_responded = 1;
2004
2005                 if (sctp_load_addresses_from_init(stcb, m,
2006                     init_offset + sizeof(struct sctp_init_chunk),
2007                     initack_offset, src, dst, init_src, stcb->asoc.port)) {
2008                         if (how_indx < sizeof(asoc->cookie_how))
2009                                 asoc->cookie_how[how_indx] = 14;
2010
2011                         return (NULL);
2012                 }
2013                 /* respond with a COOKIE-ACK */
2014                 sctp_stop_all_cookie_timers(stcb);
2015                 sctp_toss_old_cookies(stcb, asoc);
2016                 sctp_send_cookie_ack(stcb);
2017                 if (how_indx < sizeof(asoc->cookie_how))
2018                         asoc->cookie_how[how_indx] = 15;
2019
2020                 return (stcb);
2021         }
2022         if (how_indx < sizeof(asoc->cookie_how))
2023                 asoc->cookie_how[how_indx] = 16;
2024         /* all other cases... */
2025         return (NULL);
2026 }
2027
2028
2029 /*
2030  * handle a state cookie for a new association m: input packet mbuf chain--
2031  * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2032  * and the cookie signature does not exist offset: offset into mbuf to the
2033  * cookie-echo chunk length: length of the cookie chunk to: where the init
2034  * was from returns a new TCB
2035  */
2036 static struct sctp_tcb *
2037 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2038     struct sockaddr *src, struct sockaddr *dst,
2039     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2040     struct sctp_inpcb *inp, struct sctp_nets **netp,
2041     struct sockaddr *init_src, int *notification,
2042     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2043     uint8_t mflowtype, uint32_t mflowid,
2044     uint32_t vrf_id, uint16_t port)
2045 {
2046         struct sctp_tcb *stcb;
2047         struct sctp_init_chunk *init_cp, init_buf;
2048         struct sctp_init_ack_chunk *initack_cp, initack_buf;
2049         union sctp_sockstore store;
2050         struct sctp_association *asoc;
2051         int init_offset, initack_offset, initack_limit;
2052         int retval;
2053         int error = 0;
2054         uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2055 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2056         struct socket *so;
2057
2058         so = SCTP_INP_SO(inp);
2059 #endif
2060
2061         /*
2062          * find and validate the INIT chunk in the cookie (peer's info) the
2063          * INIT should start after the cookie-echo header struct (chunk
2064          * header, state cookie header struct)
2065          */
2066         init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2067         init_cp = (struct sctp_init_chunk *)
2068             sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2069             (uint8_t *)&init_buf);
2070         if (init_cp == NULL) {
2071                 /* could not pull a INIT chunk in cookie */
2072                 SCTPDBG(SCTP_DEBUG_INPUT1,
2073                     "process_cookie_new: could not pull INIT chunk hdr\n");
2074                 return (NULL);
2075         }
2076         if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2077                 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2078                 return (NULL);
2079         }
2080         initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2081         /*
2082          * find and validate the INIT-ACK chunk in the cookie (my info) the
2083          * INIT-ACK follows the INIT chunk
2084          */
2085         initack_cp = (struct sctp_init_ack_chunk *)
2086             sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2087             (uint8_t *)&initack_buf);
2088         if (initack_cp == NULL) {
2089                 /* could not pull INIT-ACK chunk in cookie */
2090                 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2091                 return (NULL);
2092         }
2093         if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2094                 return (NULL);
2095         }
2096         /*
2097          * NOTE: We can't use the INIT_ACK's chk_length to determine the
2098          * "initack_limit" value.  This is because the chk_length field
2099          * includes the length of the cookie, but the cookie is omitted when
2100          * the INIT and INIT_ACK are tacked onto the cookie...
2101          */
2102         initack_limit = offset + cookie_len;
2103
2104         /*
2105          * now that we know the INIT/INIT-ACK are in place, create a new TCB
2106          * and popluate
2107          */
2108
2109         /*
2110          * Here we do a trick, we set in NULL for the proc/thread argument.
2111          * We do this since in effect we only use the p argument when the
2112          * socket is unbound and we must do an implicit bind. Since we are
2113          * getting a cookie, we cannot be unbound.
2114          */
2115         stcb = sctp_aloc_assoc(inp, init_src, &error,
2116             ntohl(initack_cp->init.initiate_tag), vrf_id,
2117             ntohs(initack_cp->init.num_outbound_streams),
2118             port,
2119             (struct thread *)NULL
2120             );
2121         if (stcb == NULL) {
2122                 struct mbuf *op_err;
2123
2124                 /* memory problem? */
2125                 SCTPDBG(SCTP_DEBUG_INPUT1,
2126                     "process_cookie_new: no room for another TCB!\n");
2127                 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2128                 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2129                     src, dst, sh, op_err,
2130                     mflowtype, mflowid,
2131                     vrf_id, port);
2132                 return (NULL);
2133         }
2134         /* get the correct sctp_nets */
2135         if (netp)
2136                 *netp = sctp_findnet(stcb, init_src);
2137
2138         asoc = &stcb->asoc;
2139         /* get scope variables out of cookie */
2140         asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2141         asoc->scope.site_scope = cookie->site_scope;
2142         asoc->scope.local_scope = cookie->local_scope;
2143         asoc->scope.loopback_scope = cookie->loopback_scope;
2144
2145         if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2146             (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2147                 struct mbuf *op_err;
2148
2149                 /*
2150                  * Houston we have a problem. The EP changed while the
2151                  * cookie was in flight. Only recourse is to abort the
2152                  * association.
2153                  */
2154                 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2155                 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2156                     src, dst, sh, op_err,
2157                     mflowtype, mflowid,
2158                     vrf_id, port);
2159 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2160                 atomic_add_int(&stcb->asoc.refcnt, 1);
2161                 SCTP_TCB_UNLOCK(stcb);
2162                 SCTP_SOCKET_LOCK(so, 1);
2163                 SCTP_TCB_LOCK(stcb);
2164                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2165 #endif
2166                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2167                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2168 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2169                 SCTP_SOCKET_UNLOCK(so, 1);
2170 #endif
2171                 return (NULL);
2172         }
2173         /* process the INIT-ACK info (my info) */
2174         asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2175         asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2176         asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2177         asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2178         asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2179         asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2180         asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2181         asoc->str_reset_seq_in = asoc->init_seq_number;
2182
2183         asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2184
2185         /* process the INIT info (peer's info) */
2186         if (netp)
2187                 retval = sctp_process_init(init_cp, stcb);
2188         else
2189                 retval = 0;
2190         if (retval < 0) {
2191 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2192                 atomic_add_int(&stcb->asoc.refcnt, 1);
2193                 SCTP_TCB_UNLOCK(stcb);
2194                 SCTP_SOCKET_LOCK(so, 1);
2195                 SCTP_TCB_LOCK(stcb);
2196                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2197 #endif
2198                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2199                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2200 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2201                 SCTP_SOCKET_UNLOCK(so, 1);
2202 #endif
2203                 return (NULL);
2204         }
2205         /* load all addresses */
2206         if (sctp_load_addresses_from_init(stcb, m,
2207             init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2208             src, dst, init_src, port)) {
2209 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2210                 atomic_add_int(&stcb->asoc.refcnt, 1);
2211                 SCTP_TCB_UNLOCK(stcb);
2212                 SCTP_SOCKET_LOCK(so, 1);
2213                 SCTP_TCB_LOCK(stcb);
2214                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2215 #endif
2216                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2217                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2218 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2219                 SCTP_SOCKET_UNLOCK(so, 1);
2220 #endif
2221                 return (NULL);
2222         }
2223         /*
2224          * verify any preceding AUTH chunk that was skipped
2225          */
2226         /* pull the local authentication parameters from the cookie/init-ack */
2227         sctp_auth_get_cookie_params(stcb, m,
2228             initack_offset + sizeof(struct sctp_init_ack_chunk),
2229             initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2230         if (auth_skipped) {
2231                 struct sctp_auth_chunk *auth;
2232
2233                 auth = (struct sctp_auth_chunk *)
2234                     sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2235                 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2236                         /* auth HMAC failed, dump the assoc and packet */
2237                         SCTPDBG(SCTP_DEBUG_AUTH1,
2238                             "COOKIE-ECHO: AUTH failed\n");
2239 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2240                         atomic_add_int(&stcb->asoc.refcnt, 1);
2241                         SCTP_TCB_UNLOCK(stcb);
2242                         SCTP_SOCKET_LOCK(so, 1);
2243                         SCTP_TCB_LOCK(stcb);
2244                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
2245 #endif
2246                         (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2247                             SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2248 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2249                         SCTP_SOCKET_UNLOCK(so, 1);
2250 #endif
2251                         return (NULL);
2252                 } else {
2253                         /* remaining chunks checked... good to go */
2254                         stcb->asoc.authenticated = 1;
2255                 }
2256         }
2257         /*
2258          * if we're doing ASCONFs, check to see if we have any new local
2259          * addresses that need to get added to the peer (eg. addresses
2260          * changed while cookie echo in flight).  This needs to be done
2261          * after we go to the OPEN state to do the correct asconf
2262          * processing. else, make sure we have the correct addresses in our
2263          * lists
2264          */
2265
2266         /* warning, we re-use sin, sin6, sa_store here! */
2267         /* pull in local_address (our "from" address) */
2268         switch (cookie->laddr_type) {
2269 #ifdef INET
2270         case SCTP_IPV4_ADDRESS:
2271                 /* source addr is IPv4 */
2272                 memset(&store.sin, 0, sizeof(struct sockaddr_in));
2273                 store.sin.sin_family = AF_INET;
2274                 store.sin.sin_len = sizeof(struct sockaddr_in);
2275                 store.sin.sin_addr.s_addr = cookie->laddress[0];
2276                 break;
2277 #endif
2278 #ifdef INET6
2279         case SCTP_IPV6_ADDRESS:
2280                 /* source addr is IPv6 */
2281                 memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
2282                 store.sin6.sin6_family = AF_INET6;
2283                 store.sin6.sin6_len = sizeof(struct sockaddr_in6);
2284                 store.sin6.sin6_scope_id = cookie->scope_id;
2285                 memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2286                 break;
2287 #endif
2288         default:
2289 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2290                 atomic_add_int(&stcb->asoc.refcnt, 1);
2291                 SCTP_TCB_UNLOCK(stcb);
2292                 SCTP_SOCKET_LOCK(so, 1);
2293                 SCTP_TCB_LOCK(stcb);
2294                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2295 #endif
2296                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2297                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2298 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2299                 SCTP_SOCKET_UNLOCK(so, 1);
2300 #endif
2301                 return (NULL);
2302         }
2303
2304         /* update current state */
2305         SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2306         SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2307         if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2308                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2309                     stcb->sctp_ep, stcb, asoc->primary_destination);
2310         }
2311         sctp_stop_all_cookie_timers(stcb);
2312         SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2313         SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2314
2315         /* set up to notify upper layer */
2316         *notification = SCTP_NOTIFY_ASSOC_UP;
2317         if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2318             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2319             (inp->sctp_socket->so_qlimit == 0)) {
2320                 /*
2321                  * This is an endpoint that called connect() how it got a
2322                  * cookie that is NEW is a bit of a mystery. It must be that
2323                  * the INIT was sent, but before it got there.. a complete
2324                  * INIT/INIT-ACK/COOKIE arrived. But of course then it
2325                  * should have went to the other code.. not here.. oh well..
2326                  * a bit of protection is worth having..
2327                  */
2328                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2329 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2330                 atomic_add_int(&stcb->asoc.refcnt, 1);
2331                 SCTP_TCB_UNLOCK(stcb);
2332                 SCTP_SOCKET_LOCK(so, 1);
2333                 SCTP_TCB_LOCK(stcb);
2334                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2335                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2336                         SCTP_SOCKET_UNLOCK(so, 1);
2337                         return (NULL);
2338                 }
2339 #endif
2340                 soisconnected(stcb->sctp_socket);
2341 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2342                 SCTP_SOCKET_UNLOCK(so, 1);
2343 #endif
2344         } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2345             (inp->sctp_socket->so_qlimit)) {
2346                 /*
2347                  * We don't want to do anything with this one. Since it is
2348                  * the listening guy. The timer will get started for
2349                  * accepted connections in the caller.
2350                  */
2351                 ;
2352         }
2353         /* since we did not send a HB make sure we don't double things */
2354         if ((netp) && (*netp))
2355                 (*netp)->hb_responded = 1;
2356
2357         if (stcb->asoc.sctp_autoclose_ticks &&
2358             sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2359                 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2360         }
2361         (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2362         if ((netp != NULL) && (*netp != NULL)) {
2363                 /* calculate the RTT and set the encaps port */
2364                 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2365                     &cookie->time_entered, sctp_align_unsafe_makecopy,
2366                     SCTP_RTT_FROM_NON_DATA);
2367         }
2368         /* respond with a COOKIE-ACK */
2369         sctp_send_cookie_ack(stcb);
2370
2371         /*
2372          * check the address lists for any ASCONFs that need to be sent
2373          * AFTER the cookie-ack is sent
2374          */
2375         sctp_check_address_list(stcb, m,
2376             initack_offset + sizeof(struct sctp_init_ack_chunk),
2377             initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2378             &store.sa, cookie->local_scope, cookie->site_scope,
2379             cookie->ipv4_scope, cookie->loopback_scope);
2380
2381
2382         return (stcb);
2383 }
2384
2385 /*
2386  * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2387  * we NEED to make sure we are not already using the vtag. If so we
2388  * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2389         head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2390                                                             SCTP_BASE_INFO(hashasocmark))];
2391         LIST_FOREACH(stcb, head, sctp_asocs) {
2392                 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep))  {
2393                        -- SEND ABORT - TRY AGAIN --
2394                 }
2395         }
2396 */
2397
2398 /*
2399  * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2400  * existing (non-NULL) TCB
2401  */
2402 static struct mbuf *
2403 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2404     struct sockaddr *src, struct sockaddr *dst,
2405     struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2406     struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2407     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2408     struct sctp_tcb **locked_tcb,
2409     uint8_t mflowtype, uint32_t mflowid,
2410     uint32_t vrf_id, uint16_t port)
2411 {
2412         struct sctp_state_cookie *cookie;
2413         struct sctp_tcb *l_stcb = *stcb;
2414         struct sctp_inpcb *l_inp;
2415         struct sockaddr *to;
2416         struct sctp_pcb *ep;
2417         struct mbuf *m_sig;
2418         uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2419         uint8_t *sig;
2420         uint8_t cookie_ok = 0;
2421         unsigned int sig_offset, cookie_offset;
2422         unsigned int cookie_len;
2423         struct timeval now;
2424         struct timeval time_expires;
2425         int notification = 0;
2426         struct sctp_nets *netl;
2427         int had_a_existing_tcb = 0;
2428         int send_int_conf = 0;
2429 #ifdef INET
2430         struct sockaddr_in sin;
2431 #endif
2432 #ifdef INET6
2433         struct sockaddr_in6 sin6;
2434 #endif
2435
2436         SCTPDBG(SCTP_DEBUG_INPUT2,
2437             "sctp_handle_cookie: handling COOKIE-ECHO\n");
2438
2439         if (inp_p == NULL) {
2440                 return (NULL);
2441         }
2442         cookie = &cp->cookie;
2443         cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2444         cookie_len = ntohs(cp->ch.chunk_length);
2445
2446         if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2447             sizeof(struct sctp_init_chunk) +
2448             sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2449                 /* cookie too small */
2450                 return (NULL);
2451         }
2452         if ((cookie->peerport != sh->src_port) ||
2453             (cookie->myport != sh->dest_port) ||
2454             (cookie->my_vtag != sh->v_tag)) {
2455                 /*
2456                  * invalid ports or bad tag.  Note that we always leave the
2457                  * v_tag in the header in network order and when we stored
2458                  * it in the my_vtag slot we also left it in network order.
2459                  * This maintains the match even though it may be in the
2460                  * opposite byte order of the machine :->
2461                  */
2462                 return (NULL);
2463         }
2464         /*
2465          * split off the signature into its own mbuf (since it should not be
2466          * calculated in the sctp_hmac_m() call).
2467          */
2468         sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2469         m_sig = m_split(m, sig_offset, M_NOWAIT);
2470         if (m_sig == NULL) {
2471                 /* out of memory or ?? */
2472                 return (NULL);
2473         }
2474 #ifdef SCTP_MBUF_LOGGING
2475         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2476                 sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2477         }
2478 #endif
2479
2480         /*
2481          * compute the signature/digest for the cookie
2482          */
2483         ep = &(*inp_p)->sctp_ep;
2484         l_inp = *inp_p;
2485         if (l_stcb) {
2486                 SCTP_TCB_UNLOCK(l_stcb);
2487         }
2488         SCTP_INP_RLOCK(l_inp);
2489         if (l_stcb) {
2490                 SCTP_TCB_LOCK(l_stcb);
2491         }
2492         /* which cookie is it? */
2493         if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2494             (ep->current_secret_number != ep->last_secret_number)) {
2495                 /* it's the old cookie */
2496                 (void)sctp_hmac_m(SCTP_HMAC,
2497                     (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2498                     SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2499         } else {
2500                 /* it's the current cookie */
2501                 (void)sctp_hmac_m(SCTP_HMAC,
2502                     (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2503                     SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2504         }
2505         /* get the signature */
2506         SCTP_INP_RUNLOCK(l_inp);
2507         sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2508         if (sig == NULL) {
2509                 /* couldn't find signature */
2510                 sctp_m_freem(m_sig);
2511                 return (NULL);
2512         }
2513         /* compare the received digest with the computed digest */
2514         if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2515                 /* try the old cookie? */
2516                 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2517                     (ep->current_secret_number != ep->last_secret_number)) {
2518                         /* compute digest with old */
2519                         (void)sctp_hmac_m(SCTP_HMAC,
2520                             (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2521                             SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2522                         /* compare */
2523                         if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2524                                 cookie_ok = 1;
2525                 }
2526         } else {
2527                 cookie_ok = 1;
2528         }
2529
2530         /*
2531          * Now before we continue we must reconstruct our mbuf so that
2532          * normal processing of any other chunks will work.
2533          */
2534         {
2535                 struct mbuf *m_at;
2536
2537                 m_at = m;
2538                 while (SCTP_BUF_NEXT(m_at) != NULL) {
2539                         m_at = SCTP_BUF_NEXT(m_at);
2540                 }
2541                 SCTP_BUF_NEXT(m_at) = m_sig;
2542         }
2543
2544         if (cookie_ok == 0) {
2545                 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2546                 SCTPDBG(SCTP_DEBUG_INPUT2,
2547                     "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2548                     (uint32_t)offset, cookie_offset, sig_offset);
2549                 return (NULL);
2550         }
2551         /*
2552          * check the cookie timestamps to be sure it's not stale
2553          */
2554         (void)SCTP_GETTIME_TIMEVAL(&now);
2555         /* Expire time is in Ticks, so we convert to seconds */
2556         time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2557         time_expires.tv_usec = cookie->time_entered.tv_usec;
2558         if (timevalcmp(&now, &time_expires, >)) {
2559                 /* cookie is stale! */
2560                 struct mbuf *op_err;
2561                 struct sctp_error_stale_cookie *cause;
2562                 struct timeval diff;
2563                 uint32_t staleness;
2564
2565                 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2566                     0, M_NOWAIT, 1, MT_DATA);
2567                 if (op_err == NULL) {
2568                         /* FOOBAR */
2569                         return (NULL);
2570                 }
2571                 /* Set the len */
2572                 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
2573                 cause = mtod(op_err, struct sctp_error_stale_cookie *);
2574                 cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2575                 cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
2576                     (sizeof(uint32_t))));
2577                 diff = now;
2578                 timevalsub(&diff, &time_expires);
2579                 if (diff.tv_sec > UINT32_MAX / 1000000) {
2580                         staleness = UINT32_MAX;
2581                 } else {
2582                         staleness = diff.tv_sec * 1000000;
2583                 }
2584                 if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
2585                         staleness += diff.tv_usec;
2586                 } else {
2587                         staleness = UINT32_MAX;
2588                 }
2589                 cause->stale_time = htonl(staleness);
2590                 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2591                     mflowtype, mflowid, l_inp->fibnum,
2592                     vrf_id, port);
2593                 return (NULL);
2594         }
2595         /*
2596          * Now we must see with the lookup address if we have an existing
2597          * asoc. This will only happen if we were in the COOKIE-WAIT state
2598          * and a INIT collided with us and somewhere the peer sent the
2599          * cookie on another address besides the single address our assoc
2600          * had for him. In this case we will have one of the tie-tags set at
2601          * least AND the address field in the cookie can be used to look it
2602          * up.
2603          */
2604         to = NULL;
2605         switch (cookie->addr_type) {
2606 #ifdef INET6
2607         case SCTP_IPV6_ADDRESS:
2608                 memset(&sin6, 0, sizeof(sin6));
2609                 sin6.sin6_family = AF_INET6;
2610                 sin6.sin6_len = sizeof(sin6);
2611                 sin6.sin6_port = sh->src_port;
2612                 sin6.sin6_scope_id = cookie->scope_id;
2613                 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2614                     sizeof(sin6.sin6_addr.s6_addr));
2615                 to = (struct sockaddr *)&sin6;
2616                 break;
2617 #endif
2618 #ifdef INET
2619         case SCTP_IPV4_ADDRESS:
2620                 memset(&sin, 0, sizeof(sin));
2621                 sin.sin_family = AF_INET;
2622                 sin.sin_len = sizeof(sin);
2623                 sin.sin_port = sh->src_port;
2624                 sin.sin_addr.s_addr = cookie->address[0];
2625                 to = (struct sockaddr *)&sin;
2626                 break;
2627 #endif
2628         default:
2629                 /* This should not happen */
2630                 return (NULL);
2631         }
2632         if (*stcb == NULL) {
2633                 /* Yep, lets check */
2634                 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2635                 if (*stcb == NULL) {
2636                         /*
2637                          * We should have only got back the same inp. If we
2638                          * got back a different ep we have a problem. The
2639                          * original findep got back l_inp and now
2640                          */
2641                         if (l_inp != *inp_p) {
2642                                 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2643                         }
2644                 } else {
2645                         if (*locked_tcb == NULL) {
2646                                 /*
2647                                  * In this case we found the assoc only
2648                                  * after we locked the create lock. This
2649                                  * means we are in a colliding case and we
2650                                  * must make sure that we unlock the tcb if
2651                                  * its one of the cases where we throw away
2652                                  * the incoming packets.
2653                                  */
2654                                 *locked_tcb = *stcb;
2655
2656                                 /*
2657                                  * We must also increment the inp ref count
2658                                  * since the ref_count flags was set when we
2659                                  * did not find the TCB, now we found it
2660                                  * which reduces the refcount.. we must
2661                                  * raise it back out to balance it all :-)
2662                                  */
2663                                 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2664                                 if ((*stcb)->sctp_ep != l_inp) {
2665                                         SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2666                                             (void *)(*stcb)->sctp_ep, (void *)l_inp);
2667                                 }
2668                         }
2669                 }
2670         }
2671         cookie_len -= SCTP_SIGNATURE_SIZE;
2672         if (*stcb == NULL) {
2673                 /* this is the "normal" case... get a new TCB */
2674                 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2675                     cookie, cookie_len, *inp_p,
2676                     netp, to, &notification,
2677                     auth_skipped, auth_offset, auth_len,
2678                     mflowtype, mflowid,
2679                     vrf_id, port);
2680         } else {
2681                 /* this is abnormal... cookie-echo on existing TCB */
2682                 had_a_existing_tcb = 1;
2683                 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2684                     src, dst, sh,
2685                     cookie, cookie_len, *inp_p, *stcb, netp, to,
2686                     &notification, auth_skipped, auth_offset, auth_len,
2687                     mflowtype, mflowid,
2688                     vrf_id, port);
2689         }
2690
2691         if (*stcb == NULL) {
2692                 /* still no TCB... must be bad cookie-echo */
2693                 return (NULL);
2694         }
2695         if (*netp != NULL) {
2696                 (*netp)->flowtype = mflowtype;
2697                 (*netp)->flowid = mflowid;
2698         }
2699         /*
2700          * Ok, we built an association so confirm the address we sent the
2701          * INIT-ACK to.
2702          */
2703         netl = sctp_findnet(*stcb, to);
2704         /*
2705          * This code should in theory NOT run but
2706          */
2707         if (netl == NULL) {
2708                 /* TSNH! Huh, why do I need to add this address here? */
2709                 if (sctp_add_remote_addr(*stcb, to, NULL, port,
2710                     SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2711                         return (NULL);
2712                 }
2713                 netl = sctp_findnet(*stcb, to);
2714         }
2715         if (netl) {
2716                 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2717                         netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2718                         (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2719                             netl);
2720                         send_int_conf = 1;
2721                 }
2722         }
2723         sctp_start_net_timers(*stcb);
2724         if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2725                 if (!had_a_existing_tcb ||
2726                     (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2727                         /*
2728                          * If we have a NEW cookie or the connect never
2729                          * reached the connected state during collision we
2730                          * must do the TCP accept thing.
2731                          */
2732                         struct socket *so, *oso;
2733                         struct sctp_inpcb *inp;
2734
2735                         if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2736                                 /*
2737                                  * For a restart we will keep the same
2738                                  * socket, no need to do anything. I THINK!!
2739                                  */
2740                                 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2741                                 if (send_int_conf) {
2742                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2743                                             (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2744                                 }
2745                                 return (m);
2746                         }
2747                         oso = (*inp_p)->sctp_socket;
2748                         atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2749                         SCTP_TCB_UNLOCK((*stcb));
2750                         CURVNET_SET(oso->so_vnet);
2751                         so = sonewconn(oso, 0
2752                             );
2753                         CURVNET_RESTORE();
2754                         SCTP_TCB_LOCK((*stcb));
2755                         atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2756
2757                         if (so == NULL) {
2758                                 struct mbuf *op_err;
2759 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2760                                 struct socket *pcb_so;
2761 #endif
2762                                 /* Too many sockets */
2763                                 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2764                                 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2765                                 sctp_abort_association(*inp_p, NULL, m, iphlen,
2766                                     src, dst, sh, op_err,
2767                                     mflowtype, mflowid,
2768                                     vrf_id, port);
2769 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2770                                 pcb_so = SCTP_INP_SO(*inp_p);
2771                                 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2772                                 SCTP_TCB_UNLOCK((*stcb));
2773                                 SCTP_SOCKET_LOCK(pcb_so, 1);
2774                                 SCTP_TCB_LOCK((*stcb));
2775                                 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2776 #endif
2777                                 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2778                                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2779 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2780                                 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2781 #endif
2782                                 return (NULL);
2783                         }
2784                         inp = (struct sctp_inpcb *)so->so_pcb;
2785                         SCTP_INP_INCR_REF(inp);
2786                         /*
2787                          * We add the unbound flag here so that if we get an
2788                          * soabort() before we get the move_pcb done, we
2789                          * will properly cleanup.
2790                          */
2791                         inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2792                             SCTP_PCB_FLAGS_CONNECTED |
2793                             SCTP_PCB_FLAGS_IN_TCPPOOL |
2794                             SCTP_PCB_FLAGS_UNBOUND |
2795                             (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2796                             SCTP_PCB_FLAGS_DONT_WAKE);
2797                         inp->sctp_features = (*inp_p)->sctp_features;
2798                         inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2799                         inp->sctp_socket = so;
2800                         inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2801                         inp->max_cwnd = (*inp_p)->max_cwnd;
2802                         inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2803                         inp->ecn_supported = (*inp_p)->ecn_supported;
2804                         inp->prsctp_supported = (*inp_p)->prsctp_supported;
2805                         inp->auth_supported = (*inp_p)->auth_supported;
2806                         inp->asconf_supported = (*inp_p)->asconf_supported;
2807                         inp->reconfig_supported = (*inp_p)->reconfig_supported;
2808                         inp->nrsack_supported = (*inp_p)->nrsack_supported;
2809                         inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2810                         inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2811                         inp->sctp_context = (*inp_p)->sctp_context;
2812                         inp->local_strreset_support = (*inp_p)->local_strreset_support;
2813                         inp->fibnum = (*inp_p)->fibnum;
2814                         inp->inp_starting_point_for_iterator = NULL;
2815                         /*
2816                          * copy in the authentication parameters from the
2817                          * original endpoint
2818                          */
2819                         if (inp->sctp_ep.local_hmacs)
2820                                 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2821                         inp->sctp_ep.local_hmacs =
2822                             sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2823                         if (inp->sctp_ep.local_auth_chunks)
2824                                 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2825                         inp->sctp_ep.local_auth_chunks =
2826                             sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2827
2828                         /*
2829                          * Now we must move it from one hash table to
2830                          * another and get the tcb in the right place.
2831                          */
2832
2833                         /*
2834                          * This is where the one-2-one socket is put into
2835                          * the accept state waiting for the accept!
2836                          */
2837                         if (*stcb) {
2838                                 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2839                         }
2840                         sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2841
2842                         atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2843                         SCTP_TCB_UNLOCK((*stcb));
2844
2845                         sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2846                             0);
2847                         SCTP_TCB_LOCK((*stcb));
2848                         atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2849
2850
2851                         /*
2852                          * now we must check to see if we were aborted while
2853                          * the move was going on and the lock/unlock
2854                          * happened.
2855                          */
2856                         if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2857                                 /*
2858                                  * yep it was, we leave the assoc attached
2859                                  * to the socket since the sctp_inpcb_free()
2860                                  * call will send an abort for us.
2861                                  */
2862                                 SCTP_INP_DECR_REF(inp);
2863                                 return (NULL);
2864                         }
2865                         SCTP_INP_DECR_REF(inp);
2866                         /* Switch over to the new guy */
2867                         *inp_p = inp;
2868                         sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2869                         if (send_int_conf) {
2870                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2871                                     (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2872                         }
2873                         /*
2874                          * Pull it from the incomplete queue and wake the
2875                          * guy
2876                          */
2877 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2878                         atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2879                         SCTP_TCB_UNLOCK((*stcb));
2880                         SCTP_SOCKET_LOCK(so, 1);
2881 #endif
2882                         soisconnected(so);
2883 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2884                         SCTP_TCB_LOCK((*stcb));
2885                         atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2886                         SCTP_SOCKET_UNLOCK(so, 1);
2887 #endif
2888                         return (m);
2889                 }
2890         }
2891         if (notification) {
2892                 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2893         }
2894         if (send_int_conf) {
2895                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2896                     (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2897         }
2898         return (m);
2899 }
2900
2901 static void
2902 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2903     struct sctp_tcb *stcb, struct sctp_nets *net)
2904 {
2905         /* cp must not be used, others call this without a c-ack :-) */
2906         struct sctp_association *asoc;
2907
2908         SCTPDBG(SCTP_DEBUG_INPUT2,
2909             "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2910         if ((stcb == NULL) || (net == NULL)) {
2911                 return;
2912         }
2913         asoc = &stcb->asoc;
2914
2915         sctp_stop_all_cookie_timers(stcb);
2916         /* process according to association state */
2917         if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2918                 /* state change only needed when I am in right state */
2919                 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2920                 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2921                 sctp_start_net_timers(stcb);
2922                 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2923                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2924                             stcb->sctp_ep, stcb, asoc->primary_destination);
2925
2926                 }
2927                 /* update RTO */
2928                 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2929                 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2930                 if (asoc->overall_error_count == 0) {
2931                         net->RTO = sctp_calculate_rto(stcb, asoc, net,
2932                             &asoc->time_entered, sctp_align_safe_nocopy,
2933                             SCTP_RTT_FROM_NON_DATA);
2934                 }
2935                 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2936                 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2937                 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2938                     (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2939 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2940                         struct socket *so;
2941
2942 #endif
2943                         stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2944 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2945                         so = SCTP_INP_SO(stcb->sctp_ep);
2946                         atomic_add_int(&stcb->asoc.refcnt, 1);
2947                         SCTP_TCB_UNLOCK(stcb);
2948                         SCTP_SOCKET_LOCK(so, 1);
2949                         SCTP_TCB_LOCK(stcb);
2950                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
2951 #endif
2952                         if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2953                                 soisconnected(stcb->sctp_socket);
2954                         }
2955 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2956                         SCTP_SOCKET_UNLOCK(so, 1);
2957 #endif
2958                 }
2959                 /*
2960                  * since we did not send a HB make sure we don't double
2961                  * things
2962                  */
2963                 net->hb_responded = 1;
2964
2965                 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2966                         /*
2967                          * We don't need to do the asconf thing, nor hb or
2968                          * autoclose if the socket is closed.
2969                          */
2970                         goto closed_socket;
2971                 }
2972                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2973                     stcb, net);
2974
2975
2976                 if (stcb->asoc.sctp_autoclose_ticks &&
2977                     sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2978                         sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2979                             stcb->sctp_ep, stcb, NULL);
2980                 }
2981                 /*
2982                  * send ASCONF if parameters are pending and ASCONFs are
2983                  * allowed (eg. addresses changed when init/cookie echo were
2984                  * in flight)
2985                  */
2986                 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2987                     (stcb->asoc.asconf_supported == 1) &&
2988                     (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2989 #ifdef SCTP_TIMER_BASED_ASCONF
2990                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2991                             stcb->sctp_ep, stcb,
2992                             stcb->asoc.primary_destination);
2993 #else
2994                         sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2995                             SCTP_ADDR_NOT_LOCKED);
2996 #endif
2997                 }
2998         }
2999 closed_socket:
3000         /* Toss the cookie if I can */
3001         sctp_toss_old_cookies(stcb, asoc);
3002         if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3003                 /* Restart the timer if we have pending data */
3004                 struct sctp_tmit_chunk *chk;
3005
3006                 chk = TAILQ_FIRST(&asoc->sent_queue);
3007                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3008         }
3009 }
3010
3011 static void
3012 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3013     struct sctp_tcb *stcb)
3014 {
3015         struct sctp_nets *net;
3016         struct sctp_tmit_chunk *lchk;
3017         struct sctp_ecne_chunk bkup;
3018         uint8_t override_bit;
3019         uint32_t tsn, window_data_tsn;
3020         int len;
3021         unsigned int pkt_cnt;
3022
3023         len = ntohs(cp->ch.chunk_length);
3024         if ((len != sizeof(struct sctp_ecne_chunk)) &&
3025             (len != sizeof(struct old_sctp_ecne_chunk))) {
3026                 return;
3027         }
3028         if (len == sizeof(struct old_sctp_ecne_chunk)) {
3029                 /* Its the old format */
3030                 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3031                 bkup.num_pkts_since_cwr = htonl(1);
3032                 cp = &bkup;
3033         }
3034         SCTP_STAT_INCR(sctps_recvecne);
3035         tsn = ntohl(cp->tsn);
3036         pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3037         lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3038         if (lchk == NULL) {
3039                 window_data_tsn = stcb->asoc.sending_seq - 1;
3040         } else {
3041                 window_data_tsn = lchk->rec.data.tsn;
3042         }
3043
3044         /* Find where it was sent to if possible. */
3045         net = NULL;
3046         TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3047                 if (lchk->rec.data.tsn == tsn) {
3048                         net = lchk->whoTo;
3049                         net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3050                         break;
3051                 }
3052                 if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
3053                         break;
3054                 }
3055         }
3056         if (net == NULL) {
3057                 /*
3058                  * What to do. A previous send of a CWR was possibly lost.
3059                  * See how old it is, we may have it marked on the actual
3060                  * net.
3061                  */
3062                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3063                         if (tsn == net->last_cwr_tsn) {
3064                                 /* Found him, send it off */
3065                                 break;
3066                         }
3067                 }
3068                 if (net == NULL) {
3069                         /*
3070                          * If we reach here, we need to send a special CWR
3071                          * that says hey, we did this a long time ago and
3072                          * you lost the response.
3073                          */
3074                         net = TAILQ_FIRST(&stcb->asoc.nets);
3075                         if (net == NULL) {
3076                                 /* TSNH */
3077                                 return;
3078                         }
3079                         override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3080                 } else {
3081                         override_bit = 0;
3082                 }
3083         } else {
3084                 override_bit = 0;
3085         }
3086         if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3087             ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3088                 /*
3089                  * JRS - Use the congestion control given in the pluggable
3090                  * CC module
3091                  */
3092                 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3093                 /*
3094                  * We reduce once every RTT. So we will only lower cwnd at
3095                  * the next sending seq i.e. the window_data_tsn
3096                  */
3097                 net->cwr_window_tsn = window_data_tsn;
3098                 net->ecn_ce_pkt_cnt += pkt_cnt;
3099                 net->lost_cnt = pkt_cnt;
3100                 net->last_cwr_tsn = tsn;
3101         } else {
3102                 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3103                 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3104                     ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3105                         /*
3106                          * Another loss in the same window update how many
3107                          * marks/packets lost we have had.
3108                          */
3109                         int cnt = 1;
3110
3111                         if (pkt_cnt > net->lost_cnt) {
3112                                 /* Should be the case */
3113                                 cnt = (pkt_cnt - net->lost_cnt);
3114                                 net->ecn_ce_pkt_cnt += cnt;
3115                         }
3116                         net->lost_cnt = pkt_cnt;
3117                         net->last_cwr_tsn = tsn;
3118                         /*
3119                          * Most CC functions will ignore this call, since we
3120                          * are in-window yet of the initial CE the peer saw.
3121                          */
3122                         stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3123                 }
3124         }
3125         /*
3126          * We always send a CWR this way if our previous one was lost our
3127          * peer will get an update, or if it is not time again to reduce we
3128          * still get the cwr to the peer. Note we set the override when we
3129          * could not find the TSN on the chunk or the destination network.
3130          */
3131         sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3132 }
3133
3134 static void
3135 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3136 {
3137         /*
3138          * Here we get a CWR from the peer. We must look in the outqueue and
3139          * make sure that we have a covered ECNE in the control chunk part.
3140          * If so remove it.
3141          */
3142         struct sctp_tmit_chunk *chk, *nchk;
3143         struct sctp_ecne_chunk *ecne;
3144         int override;
3145         uint32_t cwr_tsn;
3146
3147         cwr_tsn = ntohl(cp->tsn);
3148         override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3149         TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
3150                 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3151                         continue;
3152                 }
3153                 if ((override == 0) && (chk->whoTo != net)) {
3154                         /* Must be from the right src unless override is set */
3155                         continue;
3156                 }
3157                 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3158                 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3159                         /* this covers this ECNE, we can remove it */
3160                         stcb->asoc.ecn_echo_cnt_onq--;
3161                         TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3162                             sctp_next);
3163                         sctp_m_freem(chk->data);
3164                         chk->data = NULL;
3165                         stcb->asoc.ctrl_queue_cnt--;
3166                         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3167                         if (override == 0) {
3168                                 break;
3169                         }
3170                 }
3171         }
3172 }
3173
3174 static void
3175 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3176     struct sctp_tcb *stcb, struct sctp_nets *net)
3177 {
3178         struct sctp_association *asoc;
3179 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3180         struct socket *so;
3181 #endif
3182
3183         SCTPDBG(SCTP_DEBUG_INPUT2,
3184             "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3185         if (stcb == NULL)
3186                 return;
3187
3188         asoc = &stcb->asoc;
3189         /* process according to association state */
3190         if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3191                 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3192                 SCTPDBG(SCTP_DEBUG_INPUT2,
3193                     "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3194                 SCTP_TCB_UNLOCK(stcb);
3195                 return;
3196         }
3197         /* notify upper layer protocol */
3198         if (stcb->sctp_socket) {
3199                 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3200         }
3201 #ifdef INVARIANTS
3202         if (!TAILQ_EMPTY(&asoc->send_queue) ||
3203             !TAILQ_EMPTY(&asoc->sent_queue) ||
3204             sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
3205                 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3206         }
3207 #endif
3208         /* stop the timer */
3209         sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3210             SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3211         SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3212         /* free the TCB */
3213         SCTPDBG(SCTP_DEBUG_INPUT2,
3214             "sctp_handle_shutdown_complete: calls free-asoc\n");
3215 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3216         so = SCTP_INP_SO(stcb->sctp_ep);
3217         atomic_add_int(&stcb->asoc.refcnt, 1);
3218         SCTP_TCB_UNLOCK(stcb);
3219         SCTP_SOCKET_LOCK(so, 1);
3220         SCTP_TCB_LOCK(stcb);
3221         atomic_subtract_int(&stcb->asoc.refcnt, 1);
3222 #endif
3223         (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3224             SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3225 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3226         SCTP_SOCKET_UNLOCK(so, 1);
3227 #endif
3228         return;
3229 }
3230
3231 static int
3232 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3233     struct sctp_nets *net, uint8_t flg)
3234 {
3235         switch (desc->chunk_type) {
3236         case SCTP_DATA:
3237                 /* find the tsn to resend (possibly */
3238                 {
3239                         uint32_t tsn;
3240                         struct sctp_tmit_chunk *tp1;
3241
3242                         tsn = ntohl(desc->tsn_ifany);
3243                         TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3244                                 if (tp1->rec.data.tsn == tsn) {
3245                                         /* found it */
3246                                         break;
3247                                 }
3248                                 if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3249                                         /* not found */
3250                                         tp1 = NULL;
3251                                         break;
3252                                 }
3253                         }
3254                         if (tp1 == NULL) {
3255                                 /*
3256                                  * Do it the other way , aka without paying
3257                                  * attention to queue seq order.
3258                                  */
3259                                 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3260                                 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3261                                         if (tp1->rec.data.tsn == tsn) {
3262                                                 /* found it */
3263                                                 break;
3264                                         }
3265                                 }
3266                         }
3267                         if (tp1 == NULL) {
3268                                 SCTP_STAT_INCR(sctps_pdrptsnnf);
3269                         }
3270                         if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3271                                 uint8_t *ddp;
3272
3273                                 if (((flg & SCTP_BADCRC) == 0) &&
3274                                     ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3275                                         return (0);
3276                                 }
3277                                 if ((stcb->asoc.peers_rwnd == 0) &&
3278                                     ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3279                                         SCTP_STAT_INCR(sctps_pdrpdiwnp);
3280                                         return (0);
3281                                 }
3282                                 if (stcb->asoc.peers_rwnd == 0 &&
3283                                     (flg & SCTP_FROM_MIDDLE_BOX)) {
3284                                         SCTP_STAT_INCR(sctps_pdrpdizrw);
3285                                         return (0);
3286                                 }
3287                                 ddp = (uint8_t *)(mtod(tp1->data, caddr_t)+
3288                                     sizeof(struct sctp_data_chunk));
3289                                 {
3290                                         unsigned int iii;
3291
3292                                         for (iii = 0; iii < sizeof(desc->data_bytes);
3293                                             iii++) {
3294                                                 if (ddp[iii] != desc->data_bytes[iii]) {
3295                                                         SCTP_STAT_INCR(sctps_pdrpbadd);
3296                                                         return (-1);
3297                                                 }
3298                                         }
3299                                 }
3300
3301                                 if (tp1->do_rtt) {
3302                                         /*
3303                                          * this guy had a RTO calculation
3304                                          * pending on it, cancel it
3305                                          */
3306                                         if (tp1->whoTo->rto_needed == 0) {
3307                                                 tp1->whoTo->rto_needed = 1;
3308                                         }
3309                                         tp1->do_rtt = 0;
3310                                 }
3311                                 SCTP_STAT_INCR(sctps_pdrpmark);
3312                                 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3313                                         sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3314                                 /*
3315                                  * mark it as if we were doing a FR, since
3316                                  * we will be getting gap ack reports behind
3317                                  * the info from the router.
3318                                  */
3319                                 tp1->rec.data.doing_fast_retransmit = 1;
3320                                 /*
3321                                  * mark the tsn with what sequences can
3322                                  * cause a new FR.
3323                                  */
3324                                 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3325                                         tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3326                                 } else {
3327                                         tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3328                                 }
3329
3330                                 /* restart the timer */
3331                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3332                                     stcb, tp1->whoTo,
3333                                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3334                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3335                                     stcb, tp1->whoTo);
3336
3337                                 /* fix counts and things */
3338                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3339                                         sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3340                                             tp1->whoTo->flight_size,
3341                                             tp1->book_size,
3342                                             (uint32_t)(uintptr_t)stcb,
3343                                             tp1->rec.data.tsn);
3344                                 }
3345                                 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3346                                         sctp_flight_size_decrease(tp1);
3347                                         sctp_total_flight_decrease(stcb, tp1);
3348                                 }
3349                                 tp1->sent = SCTP_DATAGRAM_RESEND;
3350                         } {
3351                                 /* audit code */
3352                                 unsigned int audit;
3353
3354                                 audit = 0;
3355                                 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3356                                         if (tp1->sent == SCTP_DATAGRAM_RESEND)
3357                                                 audit++;
3358                                 }
3359                                 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3360                                     sctp_next) {
3361                                         if (tp1->sent == SCTP_DATAGRAM_RESEND)
3362                                                 audit++;
3363                                 }
3364                                 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3365                                         SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3366                                             audit, stcb->asoc.sent_queue_retran_cnt);
3367 #ifndef SCTP_AUDITING_ENABLED
3368                                         stcb->asoc.sent_queue_retran_cnt = audit;
3369 #endif
3370                                 }
3371                         }
3372                 }
3373                 break;
3374         case SCTP_ASCONF:
3375                 {
3376                         struct sctp_tmit_chunk *asconf;
3377
3378                         TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3379                             sctp_next) {
3380                                 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3381                                         break;
3382                                 }
3383                         }
3384                         if (asconf) {
3385                                 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3386                                         sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3387                                 asconf->sent = SCTP_DATAGRAM_RESEND;
3388                                 asconf->snd_count--;
3389                         }
3390                 }
3391                 break;
3392         case SCTP_INITIATION:
3393                 /* resend the INIT */
3394                 stcb->asoc.dropped_special_cnt++;
3395                 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3396                         /*
3397                          * If we can get it in, in a few attempts we do
3398                          * this, otherwise we let the timer fire.
3399                          */
3400                         sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3401                             stcb, net,
3402                             SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3403                         sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3404                 }
3405                 break;
3406         case SCTP_SELECTIVE_ACK:
3407         case SCTP_NR_SELECTIVE_ACK:
3408                 /* resend the sack */
3409                 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3410                 break;
3411         case SCTP_HEARTBEAT_REQUEST:
3412                 /* resend a demand HB */
3413                 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3414                         /*
3415                          * Only retransmit if we KNOW we wont destroy the
3416                          * tcb
3417                          */
3418                         sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3419                 }
3420                 break;
3421         case SCTP_SHUTDOWN:
3422                 sctp_send_shutdown(stcb, net);
3423                 break;
3424         case SCTP_SHUTDOWN_ACK:
3425                 sctp_send_shutdown_ack(stcb, net);
3426                 break;
3427         case SCTP_COOKIE_ECHO:
3428                 {
3429                         struct sctp_tmit_chunk *cookie;
3430
3431                         cookie = NULL;
3432                         TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3433                             sctp_next) {
3434                                 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3435                                         break;
3436                                 }
3437                         }
3438                         if (cookie) {
3439                                 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3440                                         sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3441                                 cookie->sent = SCTP_DATAGRAM_RESEND;
3442                                 sctp_stop_all_cookie_timers(stcb);
3443                         }
3444                 }
3445                 break;
3446         case SCTP_COOKIE_ACK:
3447                 sctp_send_cookie_ack(stcb);
3448                 break;
3449         case SCTP_ASCONF_ACK:
3450                 /* resend last asconf ack */
3451                 sctp_send_asconf_ack(stcb);
3452                 break;
3453         case SCTP_IFORWARD_CUM_TSN:
3454         case SCTP_FORWARD_CUM_TSN:
3455                 send_forward_tsn(stcb, &stcb->asoc);
3456                 break;
3457                 /* can't do anything with these */
3458         case SCTP_PACKET_DROPPED:
3459         case SCTP_INITIATION_ACK:       /* this should not happen */
3460         case SCTP_HEARTBEAT_ACK:
3461         case SCTP_ABORT_ASSOCIATION:
3462         case SCTP_OPERATION_ERROR:
3463         case SCTP_SHUTDOWN_COMPLETE:
3464         case SCTP_ECN_ECHO:
3465         case SCTP_ECN_CWR:
3466         default:
3467                 break;
3468         }
3469         return (0);
3470 }
3471
3472 void
3473 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3474 {
3475         uint32_t i;
3476         uint16_t temp;
3477
3478         /*
3479          * We set things to 0xffffffff since this is the last delivered
3480          * sequence and we will be sending in 0 after the reset.
3481          */
3482
3483         if (number_entries) {
3484                 for (i = 0; i < number_entries; i++) {
3485                         temp = ntohs(list[i]);
3486                         if (temp >= stcb->asoc.streamincnt) {
3487                                 continue;
3488                         }
3489                         stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3490                 }
3491         } else {
3492                 list = NULL;
3493                 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3494                         stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3495                 }
3496         }
3497         sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3498 }
3499
3500 static void
3501 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3502 {
3503         uint32_t i;
3504         uint16_t temp;
3505
3506         if (number_entries > 0) {
3507                 for (i = 0; i < number_entries; i++) {
3508                         temp = ntohs(list[i]);
3509                         if (temp >= stcb->asoc.streamoutcnt) {
3510                                 /* no such stream */
3511                                 continue;
3512                         }
3513                         stcb->asoc.strmout[temp].next_mid_ordered = 0;
3514                         stcb->asoc.strmout[temp].next_mid_unordered = 0;
3515                 }
3516         } else {
3517                 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3518                         stcb->asoc.strmout[i].next_mid_ordered = 0;
3519                         stcb->asoc.strmout[i].next_mid_unordered = 0;
3520                 }
3521         }
3522         sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3523 }
3524
3525 static void
3526 sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3527 {
3528         uint32_t i;
3529         uint16_t temp;
3530
3531         if (number_entries > 0) {
3532                 for (i = 0; i < number_entries; i++) {
3533                         temp = ntohs(list[i]);
3534                         if (temp >= stcb->asoc.streamoutcnt) {
3535                                 /* no such stream */
3536                                 continue;
3537                         }
3538                         stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
3539                 }
3540         } else {
3541                 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3542                         stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
3543                 }
3544         }
3545 }
3546
3547
3548 struct sctp_stream_reset_request *
3549 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3550 {
3551         struct sctp_association *asoc;
3552         struct sctp_chunkhdr *ch;
3553         struct sctp_stream_reset_request *r;
3554         struct sctp_tmit_chunk *chk;
3555         int len, clen;
3556
3557         asoc = &stcb->asoc;
3558         if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3559                 asoc->stream_reset_outstanding = 0;
3560                 return (NULL);
3561         }
3562         if (stcb->asoc.str_reset == NULL) {
3563                 asoc->stream_reset_outstanding = 0;
3564                 return (NULL);
3565         }
3566         chk = stcb->asoc.str_reset;
3567         if (chk->data == NULL) {
3568                 return (NULL);
3569         }
3570         if (bchk) {
3571                 /* he wants a copy of the chk pointer */
3572                 *bchk = chk;
3573         }
3574         clen = chk->send_size;
3575         ch = mtod(chk->data, struct sctp_chunkhdr *);
3576         r = (struct sctp_stream_reset_request *)(ch + 1);
3577         if (ntohl(r->request_seq) == seq) {
3578                 /* found it */
3579                 return (r);
3580         }
3581         len = SCTP_SIZE32(ntohs(r->ph.param_length));
3582         if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3583                 /* move to the next one, there can only be a max of two */
3584                 r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3585                 if (ntohl(r->request_seq) == seq) {
3586                         return (r);
3587                 }
3588         }
3589         /* that seq is not here */
3590         return (NULL);
3591 }
3592
3593 static void
3594 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3595 {
3596         struct sctp_association *asoc;
3597         struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3598
3599         if (stcb->asoc.str_reset == NULL) {
3600                 return;
3601         }
3602         asoc = &stcb->asoc;
3603
3604         sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
3605             chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3606         TAILQ_REMOVE(&asoc->control_send_queue,
3607             chk,
3608             sctp_next);
3609         if (chk->data) {
3610                 sctp_m_freem(chk->data);
3611                 chk->data = NULL;
3612         }
3613         asoc->ctrl_queue_cnt--;
3614         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3615         /* sa_ignore NO_NULL_CHK */
3616         stcb->asoc.str_reset = NULL;
3617 }
3618
3619
3620 static int
3621 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3622     uint32_t seq, uint32_t action,
3623     struct sctp_stream_reset_response *respin)
3624 {
3625         uint16_t type;
3626         int lparam_len;
3627         struct sctp_association *asoc = &stcb->asoc;
3628         struct sctp_tmit_chunk *chk;
3629         struct sctp_stream_reset_request *req_param;
3630         struct sctp_stream_reset_out_request *req_out_param;
3631         struct sctp_stream_reset_in_request *req_in_param;
3632         uint32_t number_entries;
3633
3634         if (asoc->stream_reset_outstanding == 0) {
3635                 /* duplicate */
3636                 return (0);
3637         }
3638         if (seq == stcb->asoc.str_reset_seq_out) {
3639                 req_param = sctp_find_stream_reset(stcb, seq, &chk);
3640                 if (req_param != NULL) {
3641                         stcb->asoc.str_reset_seq_out++;
3642                         type = ntohs(req_param->ph.param_type);
3643                         lparam_len = ntohs(req_param->ph.param_length);
3644                         if (type == SCTP_STR_RESET_OUT_REQUEST) {
3645                                 int no_clear = 0;
3646
3647                                 req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3648                                 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3649                                 asoc->stream_reset_out_is_outstanding = 0;
3650                                 if (asoc->stream_reset_outstanding)
3651                                         asoc->stream_reset_outstanding--;
3652                                 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3653                                         /* do it */
3654                                         sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3655                                 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3656                                         sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3657                                 } else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3658                                         /*
3659                                          * Set it up so we don't stop
3660                                          * retransmitting
3661                                          */
3662                                         asoc->stream_reset_outstanding++;
3663                                         stcb->asoc.str_reset_seq_out--;
3664                                         asoc->stream_reset_out_is_outstanding = 1;
3665                                         no_clear = 1;
3666                                 } else {
3667                                         sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3668                                 }
3669                                 if (no_clear == 0) {
3670                                         sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
3671                                 }
3672                         } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3673                                 req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3674                                 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3675                                 if (asoc->stream_reset_outstanding)
3676                                         asoc->stream_reset_outstanding--;
3677                                 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3678                                         sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3679                                             number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3680                                 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3681                                         sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3682                                             number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3683                                 }
3684                         } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3685                                 /* Ok we now may have more streams */
3686                                 int num_stream;
3687
3688                                 num_stream = stcb->asoc.strm_pending_add_size;
3689                                 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3690                                         /* TSNH */
3691                                         num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3692                                 }
3693                                 stcb->asoc.strm_pending_add_size = 0;
3694                                 if (asoc->stream_reset_outstanding)
3695                                         asoc->stream_reset_outstanding--;
3696                                 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3697                                         /* Put the new streams into effect */
3698                                         int i;
3699
3700                                         for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
3701                                                 asoc->strmout[i].state = SCTP_STREAM_OPEN;
3702                                         }
3703                                         asoc->streamoutcnt += num_stream;
3704                                         sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3705                                 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3706                                         sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3707                                             SCTP_STREAM_CHANGE_DENIED);
3708                                 } else {
3709                                         sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3710                                             SCTP_STREAM_CHANGE_FAILED);
3711                                 }
3712                         } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3713                                 if (asoc->stream_reset_outstanding)
3714                                         asoc->stream_reset_outstanding--;
3715                                 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3716                                         sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3717                                             SCTP_STREAM_CHANGE_DENIED);
3718                                 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3719                                         sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3720                                             SCTP_STREAM_CHANGE_FAILED);
3721                                 }
3722                         } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3723                                 /**
3724                                  * a) Adopt the new in tsn.
3725                                  * b) reset the map
3726                                  * c) Adopt the new out-tsn
3727                                  */
3728                                 struct sctp_stream_reset_response_tsn *resp;
3729                                 struct sctp_forward_tsn_chunk fwdtsn;
3730                                 int abort_flag = 0;
3731
3732                                 if (respin == NULL) {
3733                                         /* huh ? */
3734                                         return (0);
3735                                 }
3736                                 if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
3737                                         return (0);
3738                                 }
3739                                 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3740                                         resp = (struct sctp_stream_reset_response_tsn *)respin;
3741                                         asoc->stream_reset_outstanding--;
3742                                         fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3743                                         fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3744                                         fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3745                                         sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3746                                         if (abort_flag) {
3747                                                 return (1);
3748                                         }
3749                                         stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3750                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3751                                                 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3752                                         }
3753                                         stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3754                                         stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3755                                         memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3756
3757                                         stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3758                                         memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3759
3760                                         stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3761                                         stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3762
3763                                         sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3764                                         sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3765                                         sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3766                                 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3767                                         sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3768                                             SCTP_ASSOC_RESET_DENIED);
3769                                 } else {
3770                                         sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3771                                             SCTP_ASSOC_RESET_FAILED);
3772                                 }
3773                         }
3774                         /* get rid of the request and get the request flags */
3775                         if (asoc->stream_reset_outstanding == 0) {
3776                                 sctp_clean_up_stream_reset(stcb);
3777                         }
3778                 }
3779         }
3780         if (asoc->stream_reset_outstanding == 0) {
3781                 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3782         }
3783         return (0);
3784 }
3785
3786 static void
3787 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3788     struct sctp_tmit_chunk *chk,
3789     struct sctp_stream_reset_in_request *req, int trunc)
3790 {
3791         uint32_t seq;
3792         int len, i;
3793         int number_entries;
3794         uint16_t temp;
3795
3796         /*
3797          * peer wants me to send a str-reset to him for my outgoing seq's if
3798          * seq_in is right.
3799          */
3800         struct sctp_association *asoc = &stcb->asoc;
3801
3802         seq = ntohl(req->request_seq);
3803         if (asoc->str_reset_seq_in == seq) {
3804                 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3805                 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3806                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3807                 } else if (trunc) {
3808                         /* Can't do it, since they exceeded our buffer size  */
3809                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3810                 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3811                         len = ntohs(req->ph.param_length);
3812                         number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3813                         if (number_entries) {
3814                                 for (i = 0; i < number_entries; i++) {
3815                                         temp = ntohs(req->list_of_streams[i]);
3816                                         if (temp >= stcb->asoc.streamoutcnt) {
3817                                                 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3818                                                 goto bad_boy;
3819                                         }
3820                                         req->list_of_streams[i] = temp;
3821                                 }
3822                                 for (i = 0; i < number_entries; i++) {
3823                                         if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
3824                                                 stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
3825                                         }
3826                                 }
3827                         } else {
3828                                 /* Its all */
3829                                 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3830                                         if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
3831                                                 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
3832                                 }
3833                         }
3834                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3835                 } else {
3836                         /* Can't do it, since we have sent one out */
3837                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3838                 }
3839 bad_boy:
3840                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3841                 asoc->str_reset_seq_in++;
3842         } else if (asoc->str_reset_seq_in - 1 == seq) {
3843                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3844         } else if (asoc->str_reset_seq_in - 2 == seq) {
3845                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3846         } else {
3847                 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3848         }
3849         sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3850 }
3851
3852 static int
3853 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3854     struct sctp_tmit_chunk *chk,
3855     struct sctp_stream_reset_tsn_request *req)
3856 {
3857         /* reset all in and out and update the tsn */
3858         /*
3859          * A) reset my str-seq's on in and out. B) Select a receive next,
3860          * and set cum-ack to it. Also process this selected number as a
3861          * fwd-tsn as well. C) set in the response my next sending seq.
3862          */
3863         struct sctp_forward_tsn_chunk fwdtsn;
3864         struct sctp_association *asoc = &stcb->asoc;
3865         int abort_flag = 0;
3866         uint32_t seq;
3867
3868         seq = ntohl(req->request_seq);
3869         if (asoc->str_reset_seq_in == seq) {
3870                 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3871                 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3872                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3873                 } else {
3874                         fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3875                         fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3876                         fwdtsn.ch.chunk_flags = 0;
3877                         fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3878                         sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3879                         if (abort_flag) {
3880                                 return (1);
3881                         }
3882                         asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3883                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3884                                 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3885                         }
3886                         asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3887                         asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3888                         memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3889                         asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3890                         memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3891                         atomic_add_int(&asoc->sending_seq, 1);
3892                         /* save off historical data for retrans */
3893                         asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3894                         asoc->last_sending_seq[0] = asoc->sending_seq;
3895                         asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3896                         asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3897                         sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3898                         sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3899                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3900                         sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3901                 }
3902                 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3903                     asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3904                 asoc->str_reset_seq_in++;
3905         } else if (asoc->str_reset_seq_in - 1 == seq) {
3906                 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3907                     asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3908         } else if (asoc->str_reset_seq_in - 2 == seq) {
3909                 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3910                     asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3911         } else {
3912                 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3913         }
3914         return (0);
3915 }
3916
3917 static void
3918 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3919     struct sctp_tmit_chunk *chk,
3920     struct sctp_stream_reset_out_request *req, int trunc)
3921 {
3922         uint32_t seq, tsn;
3923         int number_entries, len;
3924         struct sctp_association *asoc = &stcb->asoc;
3925
3926         seq = ntohl(req->request_seq);
3927
3928         /* now if its not a duplicate we process it */
3929         if (asoc->str_reset_seq_in == seq) {
3930                 len = ntohs(req->ph.param_length);
3931                 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3932                 /*
3933                  * the sender is resetting, handle the list issue.. we must
3934                  * a) verify if we can do the reset, if so no problem b) If
3935                  * we can't do the reset we must copy the request. c) queue
3936                  * it, and setup the data in processor to trigger it off
3937                  * when needed and dequeue all the queued data.
3938                  */
3939                 tsn = ntohl(req->send_reset_at_tsn);
3940
3941                 /* move the reset action back one */
3942                 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3943                 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3944                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3945                 } else if (trunc) {
3946                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3947                 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3948                         /* we can do it now */
3949                         sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3950                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3951                 } else {
3952                         /*
3953                          * we must queue it up and thus wait for the TSN's
3954                          * to arrive that are at or before tsn
3955                          */
3956                         struct sctp_stream_reset_list *liste;
3957                         int siz;
3958
3959                         siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3960                         SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3961                             siz, SCTP_M_STRESET);
3962                         if (liste == NULL) {
3963                                 /* gak out of memory */
3964                                 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3965                                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3966                                 return;
3967                         }
3968                         liste->seq = seq;
3969                         liste->tsn = tsn;
3970                         liste->number_entries = number_entries;
3971                         memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3972                         TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3973                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
3974                 }
3975                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3976                 asoc->str_reset_seq_in++;
3977         } else if ((asoc->str_reset_seq_in - 1) == seq) {
3978                 /*
3979                  * one seq back, just echo back last action since my
3980                  * response was lost.
3981                  */
3982                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3983         } else if ((asoc->str_reset_seq_in - 2) == seq) {
3984                 /*
3985                  * two seq back, just echo back last action since my
3986                  * response was lost.
3987                  */
3988                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3989         } else {
3990                 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3991         }
3992 }
3993
3994 static void
3995 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3996     struct sctp_stream_reset_add_strm *str_add)
3997 {
3998         /*
3999          * Peer is requesting to add more streams. If its within our
4000          * max-streams we will allow it.
4001          */
4002         uint32_t num_stream, i;
4003         uint32_t seq;
4004         struct sctp_association *asoc = &stcb->asoc;
4005         struct sctp_queued_to_read *ctl, *nctl;
4006
4007         /* Get the number. */
4008         seq = ntohl(str_add->request_seq);
4009         num_stream = ntohs(str_add->number_of_streams);
4010         /* Now what would be the new total? */
4011         if (asoc->str_reset_seq_in == seq) {
4012                 num_stream += stcb->asoc.streamincnt;
4013                 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4014                 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4015                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4016                 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
4017                     (num_stream > 0xffff)) {
4018                         /* We must reject it they ask for to many */
4019         denied:
4020                         stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4021                 } else {
4022                         /* Ok, we can do that :-) */
4023                         struct sctp_stream_in *oldstrm;
4024
4025                         /* save off the old */
4026                         oldstrm = stcb->asoc.strmin;
4027                         SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
4028                             (num_stream * sizeof(struct sctp_stream_in)),
4029                             SCTP_M_STRMI);
4030                         if (stcb->asoc.strmin == NULL) {
4031                                 stcb->asoc.strmin = oldstrm;
4032                                 goto denied;
4033                         }
4034                         /* copy off the old data */
4035                         for (i = 0; i < stcb->asoc.streamincnt; i++) {
4036                                 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4037                                 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4038                                 stcb->asoc.strmin[i].sid = i;
4039                                 stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
4040                                 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
4041                                 stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
4042                                 /* now anything on those queues? */
4043                                 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
4044                                         TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
4045                                         TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
4046                                 }
4047                                 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
4048                                         TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
4049                                         TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
4050                                 }
4051                         }
4052                         /* Init the new streams */
4053                         for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
4054                                 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4055                                 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4056                                 stcb->asoc.strmin[i].sid = i;
4057                                 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
4058                                 stcb->asoc.strmin[i].pd_api_started = 0;
4059                                 stcb->asoc.strmin[i].delivery_started = 0;
4060                         }
4061                         SCTP_FREE(oldstrm, SCTP_M_STRMI);
4062                         /* update the size */
4063                         stcb->asoc.streamincnt = num_stream;
4064                         stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4065                         sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4066                 }
4067                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4068                 asoc->str_reset_seq_in++;
4069         } else if ((asoc->str_reset_seq_in - 1) == seq) {
4070                 /*
4071                  * one seq back, just echo back last action since my
4072                  * response was lost.
4073                  */
4074                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4075         } else if ((asoc->str_reset_seq_in - 2) == seq) {
4076                 /*
4077                  * two seq back, just echo back last action since my
4078                  * response was lost.
4079                  */
4080                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4081         } else {
4082                 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4083
4084         }
4085 }
4086
4087 static void
4088 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4089     struct sctp_stream_reset_add_strm *str_add)
4090 {
4091         /*
4092          * Peer is requesting to add more streams. If its within our
4093          * max-streams we will allow it.
4094          */
4095         uint16_t num_stream;
4096         uint32_t seq;
4097         struct sctp_association *asoc = &stcb->asoc;
4098
4099         /* Get the number. */
4100         seq = ntohl(str_add->request_seq);
4101         num_stream = ntohs(str_add->number_of_streams);
4102         /* Now what would be the new total? */
4103         if (asoc->str_reset_seq_in == seq) {
4104                 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4105                 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4106                         asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4107                 } else if (stcb->asoc.stream_reset_outstanding) {
4108                         /* We must reject it we have something pending */
4109                         stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4110                 } else {
4111                         /* Ok, we can do that :-) */
4112                         int mychk;
4113
4114                         mychk = stcb->asoc.streamoutcnt;
4115                         mychk += num_stream;
4116                         if (mychk < 0x10000) {
4117                                 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4118                                 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
4119                                         stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4120                                 }
4121                         } else {
4122                                 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4123                         }
4124                 }
4125                 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4126                 asoc->str_reset_seq_in++;
4127         } else if ((asoc->str_reset_seq_in - 1) == seq) {
4128                 /*
4129                  * one seq back, just echo back last action since my
4130                  * response was lost.
4131                  */
4132                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4133         } else if ((asoc->str_reset_seq_in - 2) == seq) {
4134                 /*
4135                  * two seq back, just echo back last action since my
4136                  * response was lost.
4137                  */
4138                 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4139         } else {
4140                 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4141         }
4142 }
4143
4144 #ifdef __GNUC__
4145 __attribute__((noinline))
4146 #endif
4147 static int
4148 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4149     struct sctp_chunkhdr *ch_req)
4150 {
4151         uint16_t remaining_length, param_len, ptype;
4152         struct sctp_paramhdr pstore;
4153         uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4154         uint32_t seq = 0;
4155         int num_req = 0;
4156         int trunc = 0;
4157         struct sctp_tmit_chunk *chk;
4158         struct sctp_chunkhdr *ch;
4159         struct sctp_paramhdr *ph;
4160         int ret_code = 0;
4161         int num_param = 0;
4162
4163         /* now it may be a reset or a reset-response */
4164         remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
4165
4166         /* setup for adding the response */
4167         sctp_alloc_a_chunk(stcb, chk);
4168         if (chk == NULL) {
4169                 return (ret_code);
4170         }
4171         chk->copy_by_ref = 0;
4172         chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4173         chk->rec.chunk_id.can_take_data = 0;
4174         chk->flags = 0;
4175         chk->asoc = &stcb->asoc;
4176         chk->no_fr_allowed = 0;
4177         chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4178         chk->book_size_scale = 0;
4179         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4180         if (chk->data == NULL) {
4181 strres_nochunk:
4182                 if (chk->data) {
4183                         sctp_m_freem(chk->data);
4184                         chk->data = NULL;
4185                 }
4186                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4187                 return (ret_code);
4188         }
4189         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4190
4191         /* setup chunk parameters */
4192         chk->sent = SCTP_DATAGRAM_UNSENT;
4193         chk->snd_count = 0;
4194         chk->whoTo = NULL;
4195
4196         ch = mtod(chk->data, struct sctp_chunkhdr *);
4197         ch->chunk_type = SCTP_STREAM_RESET;
4198         ch->chunk_flags = 0;
4199         ch->chunk_length = htons(chk->send_size);
4200         SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4201         offset += sizeof(struct sctp_chunkhdr);
4202         while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4203                 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
4204                 if (ph == NULL) {
4205                         /* TSNH */
4206                         break;
4207                 }
4208                 param_len = ntohs(ph->param_length);
4209                 if ((param_len > remaining_length) ||
4210                     (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
4211                         /* bad parameter length */
4212                         break;
4213                 }
4214                 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4215                     (uint8_t *)&cstore);
4216                 if (ph == NULL) {
4217                         /* TSNH */
4218                         break;
4219                 }
4220                 ptype = ntohs(ph->param_type);
4221                 num_param++;
4222                 if (param_len > sizeof(cstore)) {
4223                         trunc = 1;
4224                 } else {
4225                         trunc = 0;
4226                 }
4227                 if (num_param > SCTP_MAX_RESET_PARAMS) {
4228                         /* hit the max of parameters already sorry.. */
4229                         break;
4230                 }
4231                 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4232                         struct sctp_stream_reset_out_request *req_out;
4233
4234                         if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
4235                                 break;
4236                         }
4237                         req_out = (struct sctp_stream_reset_out_request *)ph;
4238                         num_req++;
4239                         if (stcb->asoc.stream_reset_outstanding) {
4240                                 seq = ntohl(req_out->response_seq);
4241                                 if (seq == stcb->asoc.str_reset_seq_out) {
4242                                         /* implicit ack */
4243                                         (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4244                                 }
4245                         }
4246                         sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4247                 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4248                         struct sctp_stream_reset_add_strm *str_add;
4249
4250                         if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4251                                 break;
4252                         }
4253                         str_add = (struct sctp_stream_reset_add_strm *)ph;
4254                         num_req++;
4255                         sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4256                 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4257                         struct sctp_stream_reset_add_strm *str_add;
4258
4259                         if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4260                                 break;
4261                         }
4262                         str_add = (struct sctp_stream_reset_add_strm *)ph;
4263                         num_req++;
4264                         sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4265                 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4266                         struct sctp_stream_reset_in_request *req_in;
4267
4268                         num_req++;
4269                         req_in = (struct sctp_stream_reset_in_request *)ph;
4270                         sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4271                 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4272                         struct sctp_stream_reset_tsn_request *req_tsn;
4273
4274                         num_req++;
4275                         req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4276                         if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4277                                 ret_code = 1;
4278                                 goto strres_nochunk;
4279                         }
4280                         /* no more */
4281                         break;
4282                 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4283                         struct sctp_stream_reset_response *resp;
4284                         uint32_t result;
4285
4286                         if (param_len < sizeof(struct sctp_stream_reset_response)) {
4287                                 break;
4288                         }
4289                         resp = (struct sctp_stream_reset_response *)ph;
4290                         seq = ntohl(resp->response_seq);
4291                         result = ntohl(resp->result);
4292                         if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4293                                 ret_code = 1;
4294                                 goto strres_nochunk;
4295                         }
4296                 } else {
4297                         break;
4298                 }
4299                 offset += SCTP_SIZE32(param_len);
4300                 if (remaining_length >= SCTP_SIZE32(param_len)) {
4301                         remaining_length -= SCTP_SIZE32(param_len);
4302                 } else {
4303                         remaining_length = 0;
4304                 }
4305         }
4306         if (num_req == 0) {
4307                 /* we have no response free the stuff */
4308                 goto strres_nochunk;
4309         }
4310         /* ok we have a chunk to link in */
4311         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4312             chk,
4313             sctp_next);
4314         stcb->asoc.ctrl_queue_cnt++;
4315         return (ret_code);
4316 }
4317
4318 /*
4319  * Handle a router or endpoints report of a packet loss, there are two ways
4320  * to handle this, either we get the whole packet and must disect it
4321  * ourselves (possibly with truncation and or corruption) or it is a summary
4322  * from a middle box that did the disectting for us.
4323  */
4324 static void
4325 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4326     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4327 {
4328         uint32_t bottle_bw, on_queue;
4329         uint16_t trunc_len;
4330         unsigned int chlen;
4331         unsigned int at;
4332         struct sctp_chunk_desc desc;
4333         struct sctp_chunkhdr *ch;
4334
4335         chlen = ntohs(cp->ch.chunk_length);
4336         chlen -= sizeof(struct sctp_pktdrop_chunk);
4337         /* XXX possible chlen underflow */
4338         if (chlen == 0) {
4339                 ch = NULL;
4340                 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4341                         SCTP_STAT_INCR(sctps_pdrpbwrpt);
4342         } else {
4343                 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4344                 chlen -= sizeof(struct sctphdr);
4345                 /* XXX possible chlen underflow */
4346                 memset(&desc, 0, sizeof(desc));
4347         }
4348         trunc_len = (uint16_t)ntohs(cp->trunc_len);
4349         if (trunc_len > limit) {
4350                 trunc_len = limit;
4351         }
4352         /* now the chunks themselves */
4353         while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4354                 desc.chunk_type = ch->chunk_type;
4355                 /* get amount we need to move */
4356                 at = ntohs(ch->chunk_length);
4357                 if (at < sizeof(struct sctp_chunkhdr)) {
4358                         /* corrupt chunk, maybe at the end? */
4359                         SCTP_STAT_INCR(sctps_pdrpcrupt);
4360                         break;
4361                 }
4362                 if (trunc_len == 0) {
4363                         /* we are supposed to have all of it */
4364                         if (at > chlen) {
4365                                 /* corrupt skip it */
4366                                 SCTP_STAT_INCR(sctps_pdrpcrupt);
4367                                 break;
4368                         }
4369                 } else {
4370                         /* is there enough of it left ? */
4371                         if (desc.chunk_type == SCTP_DATA) {
4372                                 if (chlen < (sizeof(struct sctp_data_chunk) +
4373                                     sizeof(desc.data_bytes))) {
4374                                         break;
4375                                 }
4376                         } else {
4377                                 if (chlen < sizeof(struct sctp_chunkhdr)) {
4378                                         break;
4379                                 }
4380                         }
4381                 }
4382                 if (desc.chunk_type == SCTP_DATA) {
4383                         /* can we get out the tsn? */
4384                         if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4385                                 SCTP_STAT_INCR(sctps_pdrpmbda);
4386
4387                         if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4388                                 /* yep */
4389                                 struct sctp_data_chunk *dcp;
4390                                 uint8_t *ddp;
4391                                 unsigned int iii;
4392
4393                                 dcp = (struct sctp_data_chunk *)ch;
4394                                 ddp = (uint8_t *)(dcp + 1);
4395                                 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4396                                         desc.data_bytes[iii] = ddp[iii];
4397                                 }
4398                                 desc.tsn_ifany = dcp->dp.tsn;
4399                         } else {
4400                                 /* nope we are done. */
4401                                 SCTP_STAT_INCR(sctps_pdrpnedat);
4402                                 break;
4403                         }
4404                 } else {
4405                         if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4406                                 SCTP_STAT_INCR(sctps_pdrpmbct);
4407                 }
4408
4409                 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4410                         SCTP_STAT_INCR(sctps_pdrppdbrk);
4411                         break;
4412                 }
4413                 if (SCTP_SIZE32(at) > chlen) {
4414                         break;
4415                 }
4416                 chlen -= SCTP_SIZE32(at);
4417                 if (chlen < sizeof(struct sctp_chunkhdr)) {
4418                         /* done, none left */
4419                         break;
4420                 }
4421                 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4422         }
4423         /* Now update any rwnd --- possibly */
4424         if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4425                 /* From a peer, we get a rwnd report */
4426                 uint32_t a_rwnd;
4427
4428                 SCTP_STAT_INCR(sctps_pdrpfehos);
4429
4430                 bottle_bw = ntohl(cp->bottle_bw);
4431                 on_queue = ntohl(cp->current_onq);
4432                 if (bottle_bw && on_queue) {
4433                         /* a rwnd report is in here */
4434                         if (bottle_bw > on_queue)
4435                                 a_rwnd = bottle_bw - on_queue;
4436                         else
4437                                 a_rwnd = 0;
4438
4439                         if (a_rwnd == 0)
4440                                 stcb->asoc.peers_rwnd = 0;
4441                         else {
4442                                 if (a_rwnd > stcb->asoc.total_flight) {
4443                                         stcb->asoc.peers_rwnd =
4444                                             a_rwnd - stcb->asoc.total_flight;
4445                                 } else {
4446                                         stcb->asoc.peers_rwnd = 0;
4447                                 }
4448                                 if (stcb->asoc.peers_rwnd <
4449                                     stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4450                                         /* SWS sender side engages */
4451                                         stcb->asoc.peers_rwnd = 0;
4452                                 }
4453                         }
4454                 }
4455         } else {
4456                 SCTP_STAT_INCR(sctps_pdrpfmbox);
4457         }
4458
4459         /* now middle boxes in sat networks get a cwnd bump */
4460         if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4461             (stcb->asoc.sat_t3_loss_recovery == 0) &&
4462             (stcb->asoc.sat_network)) {
4463                 /*
4464                  * This is debatable but for sat networks it makes sense
4465                  * Note if a T3 timer has went off, we will prohibit any
4466                  * changes to cwnd until we exit the t3 loss recovery.
4467                  */
4468                 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4469                     net, cp, &bottle_bw, &on_queue);
4470         }
4471 }
4472
4473 /*
4474  * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4475  * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4476  * offset: offset into the mbuf chain to first chunkhdr - length: is the
4477  * length of the complete packet outputs: - length: modified to remaining
4478  * length after control processing - netp: modified to new sctp_nets after
4479  * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4480  * bad packet,...) otherwise return the tcb for this packet
4481  */
4482 #ifdef __GNUC__
4483 __attribute__((noinline))
4484 #endif
4485 static struct sctp_tcb *
4486 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4487     struct sockaddr *src, struct sockaddr *dst,
4488     struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4489     struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4490     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4491     uint32_t vrf_id, uint16_t port)
4492 {
4493         struct sctp_association *asoc;
4494         struct mbuf *op_err;
4495         char msg[SCTP_DIAG_INFO_LEN];
4496         uint32_t vtag_in;
4497         int num_chunks = 0;     /* number of control chunks processed */
4498         uint32_t chk_length;
4499         int ret;
4500         int abort_no_unlock = 0;
4501         int ecne_seen = 0;
4502
4503         /*
4504          * How big should this be, and should it be alloc'd? Lets try the
4505          * d-mtu-ceiling for now (2k) and that should hopefully work ...
4506          * until we get into jumbo grams and such..
4507          */
4508         uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4509         struct sctp_tcb *locked_tcb = stcb;
4510         int got_auth = 0;
4511         uint32_t auth_offset = 0, auth_len = 0;
4512         int auth_skipped = 0;
4513         int asconf_cnt = 0;
4514 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4515         struct socket *so;
4516 #endif
4517
4518         SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4519             iphlen, *offset, length, (void *)stcb);
4520
4521         /* validate chunk header length... */
4522         if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4523                 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4524                     ntohs(ch->chunk_length));
4525                 if (locked_tcb) {
4526                         SCTP_TCB_UNLOCK(locked_tcb);
4527                 }
4528                 return (NULL);
4529         }
4530         /*
4531          * validate the verification tag
4532          */
4533         vtag_in = ntohl(sh->v_tag);
4534
4535         if (locked_tcb) {
4536                 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4537         }
4538         if (ch->chunk_type == SCTP_INITIATION) {
4539                 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4540                     ntohs(ch->chunk_length), vtag_in);
4541                 if (vtag_in != 0) {
4542                         /* protocol error- silently discard... */
4543                         SCTP_STAT_INCR(sctps_badvtag);
4544                         if (locked_tcb) {
4545                                 SCTP_TCB_UNLOCK(locked_tcb);
4546                         }
4547                         return (NULL);
4548                 }
4549         } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4550                 /*
4551                  * If there is no stcb, skip the AUTH chunk and process
4552                  * later after a stcb is found (to validate the lookup was
4553                  * valid.
4554                  */
4555                 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4556                     (stcb == NULL) &&
4557                     (inp->auth_supported == 1)) {
4558                         /* save this chunk for later processing */
4559                         auth_skipped = 1;
4560                         auth_offset = *offset;
4561                         auth_len = ntohs(ch->chunk_length);
4562
4563                         /* (temporarily) move past this chunk */
4564                         *offset += SCTP_SIZE32(auth_len);
4565                         if (*offset >= length) {
4566                                 /* no more data left in the mbuf chain */
4567                                 *offset = length;
4568                                 if (locked_tcb) {
4569                                         SCTP_TCB_UNLOCK(locked_tcb);
4570                                 }
4571                                 return (NULL);
4572                         }
4573                         ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4574                             sizeof(struct sctp_chunkhdr), chunk_buf);
4575                 }
4576                 if (ch == NULL) {
4577                         /* Help */
4578                         *offset = length;
4579                         if (locked_tcb) {
4580                                 SCTP_TCB_UNLOCK(locked_tcb);
4581                         }
4582                         return (NULL);
4583                 }
4584                 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4585                         goto process_control_chunks;
4586                 }
4587                 /*
4588                  * first check if it's an ASCONF with an unknown src addr we
4589                  * need to look inside to find the association
4590                  */
4591                 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4592                         struct sctp_chunkhdr *asconf_ch = ch;
4593                         uint32_t asconf_offset = 0, asconf_len = 0;
4594
4595                         /* inp's refcount may be reduced */
4596                         SCTP_INP_INCR_REF(inp);
4597
4598                         asconf_offset = *offset;
4599                         do {
4600                                 asconf_len = ntohs(asconf_ch->chunk_length);
4601                                 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4602                                         break;
4603                                 stcb = sctp_findassociation_ep_asconf(m,
4604                                     *offset,
4605                                     dst,
4606                                     sh, &inp, netp, vrf_id);
4607                                 if (stcb != NULL)
4608                                         break;
4609                                 asconf_offset += SCTP_SIZE32(asconf_len);
4610                                 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4611                                     sizeof(struct sctp_chunkhdr), chunk_buf);
4612                         } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4613                         if (stcb == NULL) {
4614                                 /*
4615                                  * reduce inp's refcount if not reduced in
4616                                  * sctp_findassociation_ep_asconf().
4617                                  */
4618                                 SCTP_INP_DECR_REF(inp);
4619                         } else {
4620                                 locked_tcb = stcb;
4621                         }
4622
4623                         /* now go back and verify any auth chunk to be sure */
4624                         if (auth_skipped && (stcb != NULL)) {
4625                                 struct sctp_auth_chunk *auth;
4626
4627                                 auth = (struct sctp_auth_chunk *)
4628                                     sctp_m_getptr(m, auth_offset,
4629                                     auth_len, chunk_buf);
4630                                 got_auth = 1;
4631                                 auth_skipped = 0;
4632                                 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4633                                     auth_offset)) {
4634                                         /* auth HMAC failed so dump it */
4635                                         *offset = length;
4636                                         if (locked_tcb) {
4637                                                 SCTP_TCB_UNLOCK(locked_tcb);
4638                                         }
4639                                         return (NULL);
4640                                 } else {
4641                                         /* remaining chunks are HMAC checked */
4642                                         stcb->asoc.authenticated = 1;
4643                                 }
4644                         }
4645                 }
4646                 if (stcb == NULL) {
4647                         snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4648                         op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4649                             msg);
4650                         /* no association, so it's out of the blue... */
4651                         sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4652                             mflowtype, mflowid, inp->fibnum,
4653                             vrf_id, port);
4654                         *offset = length;
4655                         if (locked_tcb) {
4656                                 SCTP_TCB_UNLOCK(locked_tcb);
4657                         }
4658                         return (NULL);
4659                 }
4660                 asoc = &stcb->asoc;
4661                 /* ABORT and SHUTDOWN can use either v_tag... */
4662                 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4663                     (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4664                     (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4665                         /* Take the T-bit always into account. */
4666                         if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
4667                             (vtag_in == asoc->my_vtag)) ||
4668                             (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4669                             (vtag_in == asoc->peer_vtag))) {
4670                                 /* this is valid */
4671                         } else {
4672                                 /* drop this packet... */
4673                                 SCTP_STAT_INCR(sctps_badvtag);
4674                                 if (locked_tcb) {
4675                                         SCTP_TCB_UNLOCK(locked_tcb);
4676                                 }
4677                                 return (NULL);
4678                         }
4679                 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4680                         if (vtag_in != asoc->my_vtag) {
4681                                 /*
4682                                  * this could be a stale SHUTDOWN-ACK or the
4683                                  * peer never got the SHUTDOWN-COMPLETE and
4684                                  * is still hung; we have started a new asoc
4685                                  * but it won't complete until the shutdown
4686                                  * is completed
4687                                  */
4688                                 if (locked_tcb) {
4689                                         SCTP_TCB_UNLOCK(locked_tcb);
4690                                 }
4691                                 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4692                                 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4693                                     msg);
4694                                 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4695                                     sh, inp, op_err,
4696                                     mflowtype, mflowid, fibnum,
4697                                     vrf_id, port);
4698                                 return (NULL);
4699                         }
4700                 } else {
4701                         /* for all other chunks, vtag must match */
4702                         if (vtag_in != asoc->my_vtag) {
4703                                 /* invalid vtag... */
4704                                 SCTPDBG(SCTP_DEBUG_INPUT3,
4705                                     "invalid vtag: %xh, expect %xh\n",
4706                                     vtag_in, asoc->my_vtag);
4707                                 SCTP_STAT_INCR(sctps_badvtag);
4708                                 if (locked_tcb) {
4709                                         SCTP_TCB_UNLOCK(locked_tcb);
4710                                 }
4711                                 *offset = length;
4712                                 return (NULL);
4713                         }
4714                 }
4715         }                       /* end if !SCTP_COOKIE_ECHO */
4716         /*
4717          * process all control chunks...
4718          */
4719         if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4720             (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4721             (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4722             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4723                 /* implied cookie-ack.. we must have lost the ack */
4724                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4725                         sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4726                             stcb->asoc.overall_error_count,
4727                             0,
4728                             SCTP_FROM_SCTP_INPUT,
4729                             __LINE__);
4730                 }
4731                 stcb->asoc.overall_error_count = 0;
4732                 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4733                     *netp);
4734         }
4735 process_control_chunks:
4736         while (IS_SCTP_CONTROL(ch)) {
4737                 /* validate chunk length */
4738                 chk_length = ntohs(ch->chunk_length);
4739                 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4740                     ch->chunk_type, chk_length);
4741                 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4742                 if (chk_length < sizeof(*ch) ||
4743                     (*offset + (int)chk_length) > length) {
4744                         *offset = length;
4745                         if (locked_tcb) {
4746                                 SCTP_TCB_UNLOCK(locked_tcb);
4747                         }
4748                         return (NULL);
4749                 }
4750                 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4751                 /*
4752                  * INIT-ACK only gets the init ack "header" portion only
4753                  * because we don't have to process the peer's COOKIE. All
4754                  * others get a complete chunk.
4755                  */
4756                 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4757                     (ch->chunk_type == SCTP_INITIATION)) {
4758                         /* get an init-ack chunk */
4759                         ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4760                             sizeof(struct sctp_init_ack_chunk), chunk_buf);
4761                         if (ch == NULL) {
4762                                 *offset = length;
4763                                 if (locked_tcb) {
4764                                         SCTP_TCB_UNLOCK(locked_tcb);
4765                                 }
4766                                 return (NULL);
4767                         }
4768                 } else {
4769                         /* For cookies and all other chunks. */
4770                         if (chk_length > sizeof(chunk_buf)) {
4771                                 /*
4772                                  * use just the size of the chunk buffer so
4773                                  * the front part of our chunks fit in
4774                                  * contiguous space up to the chunk buffer
4775                                  * size (508 bytes). For chunks that need to
4776                                  * get more than that they must use the
4777                                  * sctp_m_getptr() function or other means
4778                                  * (e.g. know how to parse mbuf chains).
4779                                  * Cookies do this already.
4780                                  */
4781                                 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4782                                     (sizeof(chunk_buf) - 4),
4783                                     chunk_buf);
4784                                 if (ch == NULL) {
4785                                         *offset = length;
4786                                         if (locked_tcb) {
4787                                                 SCTP_TCB_UNLOCK(locked_tcb);
4788                                         }
4789                                         return (NULL);
4790                                 }
4791                         } else {
4792                                 /* We can fit it all */
4793                                 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4794                                     chk_length, chunk_buf);
4795                                 if (ch == NULL) {
4796                                         SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4797                                         *offset = length;
4798                                         if (locked_tcb) {
4799                                                 SCTP_TCB_UNLOCK(locked_tcb);
4800                                         }
4801                                         return (NULL);
4802                                 }
4803                         }
4804                 }
4805                 num_chunks++;
4806                 /* Save off the last place we got a control from */
4807                 if (stcb != NULL) {
4808                         if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4809                                 /*
4810                                  * allow last_control to be NULL if
4811                                  * ASCONF... ASCONF processing will find the
4812                                  * right net later
4813                                  */
4814                                 if ((netp != NULL) && (*netp != NULL))
4815                                         stcb->asoc.last_control_chunk_from = *netp;
4816                         }
4817                 }
4818 #ifdef SCTP_AUDITING_ENABLED
4819                 sctp_audit_log(0xB0, ch->chunk_type);
4820 #endif
4821
4822                 /* check to see if this chunk required auth, but isn't */
4823                 if ((stcb != NULL) &&
4824                     (stcb->asoc.auth_supported == 1) &&
4825                     sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4826                     !stcb->asoc.authenticated) {
4827                         /* "silently" ignore */
4828                         SCTP_STAT_INCR(sctps_recvauthmissing);
4829                         goto next_chunk;
4830                 }
4831                 switch (ch->chunk_type) {
4832                 case SCTP_INITIATION:
4833                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4834                         /* The INIT chunk must be the only chunk. */
4835                         if ((num_chunks > 1) ||
4836                             (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4837                                 /* RFC 4960 requires that no ABORT is sent */
4838                                 *offset = length;
4839                                 if (locked_tcb) {
4840                                         SCTP_TCB_UNLOCK(locked_tcb);
4841                                 }
4842                                 return (NULL);
4843                         }
4844                         /* Honor our resource limit. */
4845                         if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4846                                 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4847                                 sctp_abort_association(inp, stcb, m, iphlen,
4848                                     src, dst, sh, op_err,
4849                                     mflowtype, mflowid,
4850                                     vrf_id, port);
4851                                 *offset = length;
4852                                 return (NULL);
4853                         }
4854                         sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4855                             (struct sctp_init_chunk *)ch, inp,
4856                             stcb, *netp, &abort_no_unlock,
4857                             mflowtype, mflowid,
4858                             vrf_id, port);
4859                         *offset = length;
4860                         if ((!abort_no_unlock) && (locked_tcb)) {
4861                                 SCTP_TCB_UNLOCK(locked_tcb);
4862                         }
4863                         return (NULL);
4864                         break;
4865                 case SCTP_PAD_CHUNK:
4866                         break;
4867                 case SCTP_INITIATION_ACK:
4868                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4869                         if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4870                                 /* We are not interested anymore */
4871                                 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4872                                         ;
4873                                 } else {
4874                                         if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
4875                                                 /* Very unlikely */
4876                                                 SCTP_TCB_UNLOCK(locked_tcb);
4877                                         }
4878                                         *offset = length;
4879                                         if (stcb) {
4880 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4881                                                 so = SCTP_INP_SO(inp);
4882                                                 atomic_add_int(&stcb->asoc.refcnt, 1);
4883                                                 SCTP_TCB_UNLOCK(stcb);
4884                                                 SCTP_SOCKET_LOCK(so, 1);
4885                                                 SCTP_TCB_LOCK(stcb);
4886                                                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4887 #endif
4888                                                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4889                                                     SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4890 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4891                                                 SCTP_SOCKET_UNLOCK(so, 1);
4892 #endif
4893                                         }
4894                                         return (NULL);
4895                                 }
4896                         }
4897                         /* The INIT-ACK chunk must be the only chunk. */
4898                         if ((num_chunks > 1) ||
4899                             (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4900                                 *offset = length;
4901                                 if (locked_tcb) {
4902                                         SCTP_TCB_UNLOCK(locked_tcb);
4903                                 }
4904                                 return (NULL);
4905                         }
4906                         if ((netp) && (*netp)) {
4907                                 ret = sctp_handle_init_ack(m, iphlen, *offset,
4908                                     src, dst, sh,
4909                                     (struct sctp_init_ack_chunk *)ch,
4910                                     stcb, *netp,
4911                                     &abort_no_unlock,
4912                                     mflowtype, mflowid,
4913                                     vrf_id);
4914                         } else {
4915                                 ret = -1;
4916                         }
4917                         *offset = length;
4918                         if (abort_no_unlock) {
4919                                 return (NULL);
4920                         }
4921                         /*
4922                          * Special case, I must call the output routine to
4923                          * get the cookie echoed
4924                          */
4925                         if ((stcb != NULL) && (ret == 0)) {
4926                                 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4927                         }
4928                         if (locked_tcb) {
4929                                 SCTP_TCB_UNLOCK(locked_tcb);
4930                         }
4931                         return (NULL);
4932                         break;
4933                 case SCTP_SELECTIVE_ACK:
4934                         {
4935                                 struct sctp_sack_chunk *sack;
4936                                 int abort_now = 0;
4937                                 uint32_t a_rwnd, cum_ack;
4938                                 uint16_t num_seg, num_dup;
4939                                 uint8_t flags;
4940                                 int offset_seg, offset_dup;
4941
4942                                 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4943                                 SCTP_STAT_INCR(sctps_recvsacks);
4944                                 if (stcb == NULL) {
4945                                         SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4946                                         break;
4947                                 }
4948                                 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4949                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4950                                         break;
4951                                 }
4952                                 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4953                                         /*-
4954                                          * If we have sent a shutdown-ack, we will pay no
4955                                          * attention to a sack sent in to us since
4956                                          * we don't care anymore.
4957                                          */
4958                                         break;
4959                                 }
4960                                 sack = (struct sctp_sack_chunk *)ch;
4961                                 flags = ch->chunk_flags;
4962                                 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4963                                 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4964                                 num_dup = ntohs(sack->sack.num_dup_tsns);
4965                                 a_rwnd = (uint32_t)ntohl(sack->sack.a_rwnd);
4966                                 if (sizeof(struct sctp_sack_chunk) +
4967                                     num_seg * sizeof(struct sctp_gap_ack_block) +
4968                                     num_dup * sizeof(uint32_t) != chk_length) {
4969                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4970                                         break;
4971                                 }
4972                                 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4973                                 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4974                                 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4975                                     cum_ack, num_seg, a_rwnd);
4976                                 stcb->asoc.seen_a_sack_this_pkt = 1;
4977                                 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4978                                     (num_seg == 0) &&
4979                                     SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4980                                     (stcb->asoc.saw_sack_with_frags == 0) &&
4981                                     (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4982                                     (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4983                                     ) {
4984                                         /*
4985                                          * We have a SIMPLE sack having no
4986                                          * prior segments and data on sent
4987                                          * queue to be acked.. Use the
4988                                          * faster path sack processing. We
4989                                          * also allow window update sacks
4990                                          * with no missing segments to go
4991                                          * this way too.
4992                                          */
4993                                         sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4994                                 } else {
4995                                         if (netp && *netp)
4996                                                 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4997                                                     num_seg, 0, num_dup, &abort_now, flags,
4998                                                     cum_ack, a_rwnd, ecne_seen);
4999                                 }
5000                                 if (abort_now) {
5001                                         /* ABORT signal from sack processing */
5002                                         *offset = length;
5003                                         return (NULL);
5004                                 }
5005                                 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5006                                     TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5007                                     (stcb->asoc.stream_queue_cnt == 0)) {
5008                                         sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5009                                 }
5010                         }
5011                         break;
5012                         /*
5013                          * EY - nr_sack:  If the received chunk is an
5014                          * nr_sack chunk
5015                          */
5016                 case SCTP_NR_SELECTIVE_ACK:
5017                         {
5018                                 struct sctp_nr_sack_chunk *nr_sack;
5019                                 int abort_now = 0;
5020                                 uint32_t a_rwnd, cum_ack;
5021                                 uint16_t num_seg, num_nr_seg, num_dup;
5022                                 uint8_t flags;
5023                                 int offset_seg, offset_dup;
5024
5025                                 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
5026                                 SCTP_STAT_INCR(sctps_recvsacks);
5027                                 if (stcb == NULL) {
5028                                         SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
5029                                         break;
5030                                 }
5031                                 if (stcb->asoc.nrsack_supported == 0) {
5032                                         goto unknown_chunk;
5033                                 }
5034                                 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
5035                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
5036                                         break;
5037                                 }
5038                                 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
5039                                         /*-
5040                                          * If we have sent a shutdown-ack, we will pay no
5041                                          * attention to a sack sent in to us since
5042                                          * we don't care anymore.
5043                                          */
5044                                         break;
5045                                 }
5046                                 nr_sack = (struct sctp_nr_sack_chunk *)ch;
5047                                 flags = ch->chunk_flags;
5048                                 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
5049                                 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
5050                                 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
5051                                 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
5052                                 a_rwnd = (uint32_t)ntohl(nr_sack->nr_sack.a_rwnd);
5053                                 if (sizeof(struct sctp_nr_sack_chunk) +
5054                                     (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
5055                                     num_dup * sizeof(uint32_t) != chk_length) {
5056                                         SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
5057                                         break;
5058                                 }
5059                                 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
5060                                 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
5061                                 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
5062                                     cum_ack, num_seg, a_rwnd);
5063                                 stcb->asoc.seen_a_sack_this_pkt = 1;
5064                                 if ((stcb->asoc.pr_sctp_cnt == 0) &&
5065                                     (num_seg == 0) && (num_nr_seg == 0) &&
5066                                     SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5067                                     (stcb->asoc.saw_sack_with_frags == 0) &&
5068                                     (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5069                                     (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
5070                                         /*
5071                                          * We have a SIMPLE sack having no
5072                                          * prior segments and data on sent
5073                                          * queue to be acked. Use the faster
5074                                          * path sack processing. We also
5075                                          * allow window update sacks with no
5076                                          * missing segments to go this way
5077                                          * too.
5078                                          */
5079                                         sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
5080                                             &abort_now, ecne_seen);
5081                                 } else {
5082                                         if (netp && *netp)
5083                                                 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5084                                                     num_seg, num_nr_seg, num_dup, &abort_now, flags,
5085                                                     cum_ack, a_rwnd, ecne_seen);
5086                                 }
5087                                 if (abort_now) {
5088                                         /* ABORT signal from sack processing */
5089                                         *offset = length;
5090                                         return (NULL);
5091                                 }
5092                                 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5093                                     TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5094                                     (stcb->asoc.stream_queue_cnt == 0)) {
5095                                         sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5096                                 }
5097                         }
5098                         break;
5099
5100                 case SCTP_HEARTBEAT_REQUEST:
5101                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5102                         if ((stcb) && netp && *netp) {
5103                                 SCTP_STAT_INCR(sctps_recvheartbeat);
5104                                 sctp_send_heartbeat_ack(stcb, m, *offset,
5105                                     chk_length, *netp);
5106
5107                                 /* He's alive so give him credit */
5108                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5109                                         sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5110                                             stcb->asoc.overall_error_count,
5111                                             0,
5112                                             SCTP_FROM_SCTP_INPUT,
5113                                             __LINE__);
5114                                 }
5115                                 stcb->asoc.overall_error_count = 0;
5116                         }
5117                         break;
5118                 case SCTP_HEARTBEAT_ACK:
5119                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
5120                         if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5121                                 /* Its not ours */
5122                                 *offset = length;
5123                                 if (locked_tcb) {
5124                                         SCTP_TCB_UNLOCK(locked_tcb);
5125                                 }
5126                                 return (NULL);
5127                         }
5128                         /* He's alive so give him credit */
5129                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5130                                 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5131                                     stcb->asoc.overall_error_count,
5132                                     0,
5133                                     SCTP_FROM_SCTP_INPUT,
5134                                     __LINE__);
5135                         }
5136                         stcb->asoc.overall_error_count = 0;
5137                         SCTP_STAT_INCR(sctps_recvheartbeatack);
5138                         if (netp && *netp)
5139                                 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5140                                     stcb, *netp);
5141                         break;
5142                 case SCTP_ABORT_ASSOCIATION:
5143                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5144                             (void *)stcb);
5145                         if ((stcb) && netp && *netp)
5146                                 sctp_handle_abort((struct sctp_abort_chunk *)ch,
5147                                     stcb, *netp);
5148                         *offset = length;
5149                         return (NULL);
5150                         break;
5151                 case SCTP_SHUTDOWN:
5152                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5153                             (void *)stcb);
5154                         if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5155                                 *offset = length;
5156                                 if (locked_tcb) {
5157                                         SCTP_TCB_UNLOCK(locked_tcb);
5158                                 }
5159                                 return (NULL);
5160                         }
5161                         if (netp && *netp) {
5162                                 int abort_flag = 0;
5163
5164                                 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5165                                     stcb, *netp, &abort_flag);
5166                                 if (abort_flag) {
5167                                         *offset = length;
5168                                         return (NULL);
5169                                 }
5170                         }
5171                         break;
5172                 case SCTP_SHUTDOWN_ACK:
5173                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", (void *)stcb);
5174                         if ((stcb) && (netp) && (*netp))
5175                                 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5176                         *offset = length;
5177                         return (NULL);
5178                         break;
5179
5180                 case SCTP_OPERATION_ERROR:
5181                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5182                         if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5183                                 *offset = length;
5184                                 return (NULL);
5185                         }
5186                         break;
5187                 case SCTP_COOKIE_ECHO:
5188                         SCTPDBG(SCTP_DEBUG_INPUT3,
5189                             "SCTP_COOKIE-ECHO, stcb %p\n", (void *)stcb);
5190                         if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5191                                 ;
5192                         } else {
5193                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5194                                         /* We are not interested anymore */
5195                         abend:
5196                                         if (stcb) {
5197                                                 SCTP_TCB_UNLOCK(stcb);
5198                                         }
5199                                         *offset = length;
5200                                         return (NULL);
5201                                 }
5202                         }
5203                         /*
5204                          * First are we accepting? We do this again here
5205                          * since it is possible that a previous endpoint WAS
5206                          * listening responded to a INIT-ACK and then
5207                          * closed. We opened and bound.. and are now no
5208                          * longer listening.
5209                          */
5210
5211                         if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
5212                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5213                                     (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5214                                         op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
5215                                         sctp_abort_association(inp, stcb, m, iphlen,
5216                                             src, dst, sh, op_err,
5217                                             mflowtype, mflowid,
5218                                             vrf_id, port);
5219                                 }
5220                                 *offset = length;
5221                                 return (NULL);
5222                         } else {
5223                                 struct mbuf *ret_buf;
5224                                 struct sctp_inpcb *linp;
5225
5226                                 if (stcb) {
5227                                         linp = NULL;
5228                                 } else {
5229                                         linp = inp;
5230                                 }
5231
5232                                 if (linp) {
5233                                         SCTP_ASOC_CREATE_LOCK(linp);
5234                                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5235                                             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5236                                                 SCTP_ASOC_CREATE_UNLOCK(linp);
5237                                                 goto abend;
5238                                         }
5239                                 }
5240                                 if (netp) {
5241                                         ret_buf =
5242                                             sctp_handle_cookie_echo(m, iphlen,
5243                                             *offset,
5244                                             src, dst,
5245                                             sh,
5246                                             (struct sctp_cookie_echo_chunk *)ch,
5247                                             &inp, &stcb, netp,
5248                                             auth_skipped,
5249                                             auth_offset,
5250                                             auth_len,
5251                                             &locked_tcb,
5252                                             mflowtype,
5253                                             mflowid,
5254                                             vrf_id,
5255                                             port);
5256                                 } else {
5257                                         ret_buf = NULL;
5258                                 }
5259                                 if (linp) {
5260                                         SCTP_ASOC_CREATE_UNLOCK(linp);
5261                                 }
5262                                 if (ret_buf == NULL) {
5263                                         if (locked_tcb) {
5264                                                 SCTP_TCB_UNLOCK(locked_tcb);
5265                                         }
5266                                         SCTPDBG(SCTP_DEBUG_INPUT3,
5267                                             "GAK, null buffer\n");
5268                                         *offset = length;
5269                                         return (NULL);
5270                                 }
5271                                 /* if AUTH skipped, see if it verified... */
5272                                 if (auth_skipped) {
5273                                         got_auth = 1;
5274                                         auth_skipped = 0;
5275                                 }
5276                                 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5277                                         /*
5278                                          * Restart the timer if we have
5279                                          * pending data
5280                                          */
5281                                         struct sctp_tmit_chunk *chk;
5282
5283                                         chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5284                                         sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5285                                 }
5286                         }
5287                         break;
5288                 case SCTP_COOKIE_ACK:
5289                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", (void *)stcb);
5290                         if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5291                                 if (locked_tcb) {
5292                                         SCTP_TCB_UNLOCK(locked_tcb);
5293                                 }
5294                                 return (NULL);
5295                         }
5296                         if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5297                                 /* We are not interested anymore */
5298                                 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5299                                         ;
5300                                 } else if (stcb) {
5301 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5302                                         so = SCTP_INP_SO(inp);
5303                                         atomic_add_int(&stcb->asoc.refcnt, 1);
5304                                         SCTP_TCB_UNLOCK(stcb);
5305                                         SCTP_SOCKET_LOCK(so, 1);
5306                                         SCTP_TCB_LOCK(stcb);
5307                                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
5308 #endif
5309                                         (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5310                                             SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5311 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5312                                         SCTP_SOCKET_UNLOCK(so, 1);
5313 #endif
5314                                         *offset = length;
5315                                         return (NULL);
5316                                 }
5317                         }
5318                         /* He's alive so give him credit */
5319                         if ((stcb) && netp && *netp) {
5320                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5321                                         sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5322                                             stcb->asoc.overall_error_count,
5323                                             0,
5324                                             SCTP_FROM_SCTP_INPUT,
5325                                             __LINE__);
5326                                 }
5327                                 stcb->asoc.overall_error_count = 0;
5328                                 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5329                         }
5330                         break;
5331                 case SCTP_ECN_ECHO:
5332                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5333                         /* He's alive so give him credit */
5334                         if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5335                                 /* Its not ours */
5336                                 if (locked_tcb) {
5337                                         SCTP_TCB_UNLOCK(locked_tcb);
5338                                 }
5339                                 *offset = length;
5340                                 return (NULL);
5341                         }
5342                         if (stcb) {
5343                                 if (stcb->asoc.ecn_supported == 0) {
5344                                         goto unknown_chunk;
5345                                 }
5346                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5347                                         sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5348                                             stcb->asoc.overall_error_count,
5349                                             0,
5350                                             SCTP_FROM_SCTP_INPUT,
5351                                             __LINE__);
5352                                 }
5353                                 stcb->asoc.overall_error_count = 0;
5354                                 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5355                                     stcb);
5356                                 ecne_seen = 1;
5357                         }
5358                         break;
5359                 case SCTP_ECN_CWR:
5360                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5361                         /* He's alive so give him credit */
5362                         if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5363                                 /* Its not ours */
5364                                 if (locked_tcb) {
5365                                         SCTP_TCB_UNLOCK(locked_tcb);
5366                                 }
5367                                 *offset = length;
5368                                 return (NULL);
5369                         }
5370                         if (stcb) {
5371                                 if (stcb->asoc.ecn_supported == 0) {
5372                                         goto unknown_chunk;
5373                                 }
5374                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5375                                         sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5376                                             stcb->asoc.overall_error_count,
5377                                             0,
5378                                             SCTP_FROM_SCTP_INPUT,
5379                                             __LINE__);
5380                                 }
5381                                 stcb->asoc.overall_error_count = 0;
5382                                 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5383                         }
5384                         break;
5385                 case SCTP_SHUTDOWN_COMPLETE:
5386                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", (void *)stcb);
5387                         /* must be first and only chunk */
5388                         if ((num_chunks > 1) ||
5389                             (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5390                                 *offset = length;
5391                                 if (locked_tcb) {
5392                                         SCTP_TCB_UNLOCK(locked_tcb);
5393                                 }
5394                                 return (NULL);
5395                         }
5396                         if ((stcb) && netp && *netp) {
5397                                 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5398                                     stcb, *netp);
5399                         }
5400                         *offset = length;
5401                         return (NULL);
5402                         break;
5403                 case SCTP_ASCONF:
5404                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5405                         /* He's alive so give him credit */
5406                         if (stcb) {
5407                                 if (stcb->asoc.asconf_supported == 0) {
5408                                         goto unknown_chunk;
5409                                 }
5410                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5411                                         sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5412                                             stcb->asoc.overall_error_count,
5413                                             0,
5414                                             SCTP_FROM_SCTP_INPUT,
5415                                             __LINE__);
5416                                 }
5417                                 stcb->asoc.overall_error_count = 0;
5418                                 sctp_handle_asconf(m, *offset, src,
5419                                     (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5420                                 asconf_cnt++;
5421                         }
5422                         break;
5423                 case SCTP_ASCONF_ACK:
5424                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5425                         if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5426                                 /* Its not ours */
5427                                 if (locked_tcb) {
5428                                         SCTP_TCB_UNLOCK(locked_tcb);
5429                                 }
5430                                 *offset = length;
5431                                 return (NULL);
5432                         }
5433                         if ((stcb) && netp && *netp) {
5434                                 if (stcb->asoc.asconf_supported == 0) {
5435                                         goto unknown_chunk;
5436                                 }
5437                                 /* He's alive so give him credit */
5438                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5439                                         sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5440                                             stcb->asoc.overall_error_count,
5441                                             0,
5442                                             SCTP_FROM_SCTP_INPUT,
5443                                             __LINE__);
5444                                 }
5445                                 stcb->asoc.overall_error_count = 0;
5446                                 sctp_handle_asconf_ack(m, *offset,
5447                                     (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5448                                 if (abort_no_unlock)
5449                                         return (NULL);
5450                         }
5451                         break;
5452                 case SCTP_FORWARD_CUM_TSN:
5453                 case SCTP_IFORWARD_CUM_TSN:
5454                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5455                         if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5456                                 /* Its not ours */
5457                                 if (locked_tcb) {
5458                                         SCTP_TCB_UNLOCK(locked_tcb);
5459                                 }
5460                                 *offset = length;
5461                                 return (NULL);
5462                         }
5463                         /* He's alive so give him credit */
5464                         if (stcb) {
5465                                 int abort_flag = 0;
5466
5467                                 if (stcb->asoc.prsctp_supported == 0) {
5468                                         goto unknown_chunk;
5469                                 }
5470                                 stcb->asoc.overall_error_count = 0;
5471                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5472                                         sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5473                                             stcb->asoc.overall_error_count,
5474                                             0,
5475                                             SCTP_FROM_SCTP_INPUT,
5476                                             __LINE__);
5477                                 }
5478                                 *fwd_tsn_seen = 1;
5479                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5480                                         /* We are not interested anymore */
5481 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5482                                         so = SCTP_INP_SO(inp);
5483                                         atomic_add_int(&stcb->asoc.refcnt, 1);
5484                                         SCTP_TCB_UNLOCK(stcb);
5485                                         SCTP_SOCKET_LOCK(so, 1);
5486                                         SCTP_TCB_LOCK(stcb);
5487                                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
5488 #endif
5489                                         (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5490                                             SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5491 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5492                                         SCTP_SOCKET_UNLOCK(so, 1);
5493 #endif
5494                                         *offset = length;
5495                                         return (NULL);
5496                                 }
5497                                 /*
5498                                  * For sending a SACK this looks like DATA
5499                                  * chunks.
5500                                  */
5501                                 stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
5502                                 sctp_handle_forward_tsn(stcb,
5503                                     (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5504                                 if (abort_flag) {
5505                                         *offset = length;
5506                                         return (NULL);
5507                                 } else {
5508                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5509                                                 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5510                                                     stcb->asoc.overall_error_count,
5511                                                     0,
5512                                                     SCTP_FROM_SCTP_INPUT,
5513                                                     __LINE__);
5514                                         }
5515                                         stcb->asoc.overall_error_count = 0;
5516                                 }
5517
5518                         }
5519                         break;
5520                 case SCTP_STREAM_RESET:
5521                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5522                         if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5523                                 /* Its not ours */
5524                                 if (locked_tcb) {
5525                                         SCTP_TCB_UNLOCK(locked_tcb);
5526                                 }
5527                                 *offset = length;
5528                                 return (NULL);
5529                         }
5530                         if (stcb->asoc.reconfig_supported == 0) {
5531                                 goto unknown_chunk;
5532                         }
5533                         if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5534                                 /* stop processing */
5535                                 *offset = length;
5536                                 return (NULL);
5537                         }
5538                         break;
5539                 case SCTP_PACKET_DROPPED:
5540                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5541                         /* re-get it all please */
5542                         if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5543                                 /* Its not ours */
5544                                 if (locked_tcb) {
5545                                         SCTP_TCB_UNLOCK(locked_tcb);
5546                                 }
5547                                 *offset = length;
5548                                 return (NULL);
5549                         }
5550                         if (ch && (stcb) && netp && (*netp)) {
5551                                 if (stcb->asoc.pktdrop_supported == 0) {
5552                                         goto unknown_chunk;
5553                                 }
5554                                 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5555                                     stcb, *netp,
5556                                     min(chk_length, (sizeof(chunk_buf) - 4)));
5557
5558                         }
5559                         break;
5560                 case SCTP_AUTHENTICATION:
5561                         SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5562                         if (stcb == NULL) {
5563                                 /* save the first AUTH for later processing */
5564                                 if (auth_skipped == 0) {
5565                                         auth_offset = *offset;
5566                                         auth_len = chk_length;
5567                                         auth_skipped = 1;
5568                                 }
5569                                 /* skip this chunk (temporarily) */
5570                                 goto next_chunk;
5571                         }
5572                         if (stcb->asoc.auth_supported == 0) {
5573                                 goto unknown_chunk;
5574                         }
5575                         if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5576                             (chk_length > (sizeof(struct sctp_auth_chunk) +
5577                             SCTP_AUTH_DIGEST_LEN_MAX))) {
5578                                 /* Its not ours */
5579                                 if (locked_tcb) {
5580                                         SCTP_TCB_UNLOCK(locked_tcb);
5581                                 }
5582                                 *offset = length;
5583                                 return (NULL);
5584                         }
5585                         if (got_auth == 1) {
5586                                 /* skip this chunk... it's already auth'd */
5587                                 goto next_chunk;
5588                         }
5589                         got_auth = 1;
5590                         if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5591                             m, *offset)) {
5592                                 /* auth HMAC failed so dump the packet */
5593                                 *offset = length;
5594                                 return (stcb);
5595                         } else {
5596                                 /* remaining chunks are HMAC checked */
5597                                 stcb->asoc.authenticated = 1;
5598                         }
5599                         break;
5600
5601                 default:
5602         unknown_chunk:
5603                         /* it's an unknown chunk! */
5604                         if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5605                                 struct sctp_gen_error_cause *cause;
5606                                 int len;
5607
5608                                 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5609                                     0, M_NOWAIT, 1, MT_DATA);
5610                                 if (op_err != NULL) {
5611                                         len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
5612                                         cause = mtod(op_err, struct sctp_gen_error_cause *);
5613                                         cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5614                                         cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
5615                                         SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5616                                         SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
5617                                         if (SCTP_BUF_NEXT(op_err) != NULL) {
5618 #ifdef SCTP_MBUF_LOGGING
5619                                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5620                                                         sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5621                                                 }
5622 #endif
5623                                                 sctp_queue_op_err(stcb, op_err);
5624                                         } else {
5625                                                 sctp_m_freem(op_err);
5626                                         }
5627                                 }
5628                         }
5629                         if ((ch->chunk_type & 0x80) == 0) {
5630                                 /* discard this packet */
5631                                 *offset = length;
5632                                 return (stcb);
5633                         }       /* else skip this bad chunk and continue... */
5634                         break;
5635                 }               /* switch (ch->chunk_type) */
5636
5637
5638 next_chunk:
5639                 /* get the next chunk */
5640                 *offset += SCTP_SIZE32(chk_length);
5641                 if (*offset >= length) {
5642                         /* no more data left in the mbuf chain */
5643                         break;
5644                 }
5645                 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5646                     sizeof(struct sctp_chunkhdr), chunk_buf);
5647                 if (ch == NULL) {
5648                         if (locked_tcb) {
5649                                 SCTP_TCB_UNLOCK(locked_tcb);
5650                         }
5651                         *offset = length;
5652                         return (NULL);
5653                 }
5654         }                       /* while */
5655
5656         if (asconf_cnt > 0 && stcb != NULL) {
5657                 sctp_send_asconf_ack(stcb);
5658         }
5659         return (stcb);
5660 }
5661
5662
5663 /*
5664  * common input chunk processing (v4 and v6)
5665  */
5666 void
5667 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5668     struct sockaddr *src, struct sockaddr *dst,
5669     struct sctphdr *sh, struct sctp_chunkhdr *ch,
5670 #if !defined(SCTP_WITH_NO_CSUM)
5671     uint8_t compute_crc,
5672 #endif
5673     uint8_t ecn_bits,
5674     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5675     uint32_t vrf_id, uint16_t port)
5676 {
5677         uint32_t high_tsn;
5678         int fwd_tsn_seen = 0, data_processed = 0;
5679         struct mbuf *m = *mm, *op_err;
5680         char msg[SCTP_DIAG_INFO_LEN];
5681         int un_sent;
5682         int cnt_ctrl_ready = 0;
5683         struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5684         struct sctp_tcb *stcb = NULL;
5685         struct sctp_nets *net = NULL;
5686
5687         SCTP_STAT_INCR(sctps_recvdatagrams);
5688 #ifdef SCTP_AUDITING_ENABLED
5689         sctp_audit_log(0xE0, 1);
5690         sctp_auditing(0, inp, stcb, net);
5691 #endif
5692 #if !defined(SCTP_WITH_NO_CSUM)
5693         if (compute_crc != 0) {
5694                 uint32_t check, calc_check;
5695
5696                 check = sh->checksum;
5697                 sh->checksum = 0;
5698                 calc_check = sctp_calculate_cksum(m, iphlen);
5699                 sh->checksum = check;
5700                 if (calc_check != check) {
5701                         SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
5702                             calc_check, check, (void *)m, length, iphlen);
5703                         stcb = sctp_findassociation_addr(m, offset, src, dst,
5704                             sh, ch, &inp, &net, vrf_id);
5705 #if defined(INET) || defined(INET6)
5706                         if ((ch->chunk_type != SCTP_INITIATION) &&
5707                             (net != NULL) && (net->port != port)) {
5708                                 if (net->port == 0) {
5709                                         /* UDP encapsulation turned on. */
5710                                         net->mtu -= sizeof(struct udphdr);
5711                                         if (stcb->asoc.smallest_mtu > net->mtu) {
5712                                                 sctp_pathmtu_adjustment(stcb, net->mtu);
5713                                         }
5714                                 } else if (port == 0) {
5715                                         /* UDP encapsulation turned off. */
5716                                         net->mtu += sizeof(struct udphdr);
5717                                         /* XXX Update smallest_mtu */
5718                                 }
5719                                 net->port = port;
5720                         }
5721 #endif
5722                         if (net != NULL) {
5723                                 net->flowtype = mflowtype;
5724                                 net->flowid = mflowid;
5725                         }
5726                         if ((inp != NULL) && (stcb != NULL)) {
5727                                 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5728                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5729                         } else if ((inp != NULL) && (stcb == NULL)) {
5730                                 inp_decr = inp;
5731                         }
5732                         SCTP_STAT_INCR(sctps_badsum);
5733                         SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5734                         goto out;
5735                 }
5736         }
5737 #endif
5738         /* Destination port of 0 is illegal, based on RFC4960. */
5739         if (sh->dest_port == 0) {
5740                 SCTP_STAT_INCR(sctps_hdrops);
5741                 goto out;
5742         }
5743         stcb = sctp_findassociation_addr(m, offset, src, dst,
5744             sh, ch, &inp, &net, vrf_id);
5745 #if defined(INET) || defined(INET6)
5746         if ((ch->chunk_type != SCTP_INITIATION) &&
5747             (net != NULL) && (net->port != port)) {
5748                 if (net->port == 0) {
5749                         /* UDP encapsulation turned on. */
5750                         net->mtu -= sizeof(struct udphdr);
5751                         if (stcb->asoc.smallest_mtu > net->mtu) {
5752                                 sctp_pathmtu_adjustment(stcb, net->mtu);
5753                         }
5754                 } else if (port == 0) {
5755                         /* UDP encapsulation turned off. */
5756                         net->mtu += sizeof(struct udphdr);
5757                         /* XXX Update smallest_mtu */
5758                 }
5759                 net->port = port;
5760         }
5761 #endif
5762         if (net != NULL) {
5763                 net->flowtype = mflowtype;
5764                 net->flowid = mflowid;
5765         }
5766         if (inp == NULL) {
5767                 SCTP_STAT_INCR(sctps_noport);
5768                 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5769                         goto out;
5770                 }
5771                 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5772                         sctp_send_shutdown_complete2(src, dst, sh,
5773                             mflowtype, mflowid, fibnum,
5774                             vrf_id, port);
5775                         goto out;
5776                 }
5777                 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5778                         goto out;
5779                 }
5780                 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5781                         if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5782                             ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5783                             (ch->chunk_type != SCTP_INIT))) {
5784                                 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5785                                     "Out of the blue");
5786                                 sctp_send_abort(m, iphlen, src, dst,
5787                                     sh, 0, op_err,
5788                                     mflowtype, mflowid, fibnum,
5789                                     vrf_id, port);
5790                         }
5791                 }
5792                 goto out;
5793         } else if (stcb == NULL) {
5794                 inp_decr = inp;
5795         }
5796         SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5797             (void *)m, iphlen, offset, length, (void *)stcb);
5798         if (stcb) {
5799                 /* always clear this before beginning a packet */
5800                 stcb->asoc.authenticated = 0;
5801                 stcb->asoc.seen_a_sack_this_pkt = 0;
5802                 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5803                     (void *)stcb, stcb->asoc.state);
5804
5805                 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5806                     (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5807                         /*-
5808                          * If we hit here, we had a ref count
5809                          * up when the assoc was aborted and the
5810                          * timer is clearing out the assoc, we should
5811                          * NOT respond to any packet.. its OOTB.
5812                          */
5813                         SCTP_TCB_UNLOCK(stcb);
5814                         stcb = NULL;
5815                         snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5816                         op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5817                             msg);
5818                         sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5819                             mflowtype, mflowid, inp->fibnum,
5820                             vrf_id, port);
5821                         goto out;
5822                 }
5823         }
5824         if (IS_SCTP_CONTROL(ch)) {
5825                 /* process the control portion of the SCTP packet */
5826                 /* sa_ignore NO_NULL_CHK */
5827                 stcb = sctp_process_control(m, iphlen, &offset, length,
5828                     src, dst, sh, ch,
5829                     inp, stcb, &net, &fwd_tsn_seen,
5830                     mflowtype, mflowid, fibnum,
5831                     vrf_id, port);
5832                 if (stcb) {
5833                         /*
5834                          * This covers us if the cookie-echo was there and
5835                          * it changes our INP.
5836                          */
5837                         inp = stcb->sctp_ep;
5838 #if defined(INET) || defined(INET6)
5839                         if ((ch->chunk_type != SCTP_INITIATION) &&
5840                             (net != NULL) && (net->port != port)) {
5841                                 if (net->port == 0) {
5842                                         /* UDP encapsulation turned on. */
5843                                         net->mtu -= sizeof(struct udphdr);
5844                                         if (stcb->asoc.smallest_mtu > net->mtu) {
5845                                                 sctp_pathmtu_adjustment(stcb, net->mtu);
5846                                         }
5847                                 } else if (port == 0) {
5848                                         /* UDP encapsulation turned off. */
5849                                         net->mtu += sizeof(struct udphdr);
5850                                         /* XXX Update smallest_mtu */
5851                                 }
5852                                 net->port = port;
5853                         }
5854 #endif
5855                 }
5856         } else {
5857                 /*
5858                  * no control chunks, so pre-process DATA chunks (these
5859                  * checks are taken care of by control processing)
5860                  */
5861
5862                 /*
5863                  * if DATA only packet, and auth is required, then punt...
5864                  * can't have authenticated without any AUTH (control)
5865                  * chunks
5866                  */
5867                 if ((stcb != NULL) &&
5868                     (stcb->asoc.auth_supported == 1) &&
5869                     sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5870                         /* "silently" ignore */
5871                         SCTP_STAT_INCR(sctps_recvauthmissing);
5872                         goto out;
5873                 }
5874                 if (stcb == NULL) {
5875                         /* out of the blue DATA chunk */
5876                         snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5877                         op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5878                             msg);
5879                         sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5880                             mflowtype, mflowid, fibnum,
5881                             vrf_id, port);
5882                         goto out;
5883                 }
5884                 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5885                         /* v_tag mismatch! */
5886                         SCTP_STAT_INCR(sctps_badvtag);
5887                         goto out;
5888                 }
5889         }
5890
5891         if (stcb == NULL) {
5892                 /*
5893                  * no valid TCB for this packet, or we found it's a bad
5894                  * packet while processing control, or we're done with this
5895                  * packet (done or skip rest of data), so we drop it...
5896                  */
5897                 goto out;
5898         }
5899         /*
5900          * DATA chunk processing
5901          */
5902         /* plow through the data chunks while length > offset */
5903
5904         /*
5905          * Rest should be DATA only.  Check authentication state if AUTH for
5906          * DATA is required.
5907          */
5908         if ((length > offset) &&
5909             (stcb != NULL) &&
5910             (stcb->asoc.auth_supported == 1) &&
5911             sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5912             !stcb->asoc.authenticated) {
5913                 /* "silently" ignore */
5914                 SCTP_STAT_INCR(sctps_recvauthmissing);
5915                 SCTPDBG(SCTP_DEBUG_AUTH1,
5916                     "Data chunk requires AUTH, skipped\n");
5917                 goto trigger_send;
5918         }
5919         if (length > offset) {
5920                 int retval;
5921
5922                 /*
5923                  * First check to make sure our state is correct. We would
5924                  * not get here unless we really did have a tag, so we don't
5925                  * abort if this happens, just dump the chunk silently.
5926                  */
5927                 switch (SCTP_GET_STATE(&stcb->asoc)) {
5928                 case SCTP_STATE_COOKIE_ECHOED:
5929                         /*
5930                          * we consider data with valid tags in this state
5931                          * shows us the cookie-ack was lost. Imply it was
5932                          * there.
5933                          */
5934                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5935                                 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5936                                     stcb->asoc.overall_error_count,
5937                                     0,
5938                                     SCTP_FROM_SCTP_INPUT,
5939                                     __LINE__);
5940                         }
5941                         stcb->asoc.overall_error_count = 0;
5942                         sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5943                         break;
5944                 case SCTP_STATE_COOKIE_WAIT:
5945                         /*
5946                          * We consider OOTB any data sent during asoc setup.
5947                          */
5948                         snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5949                         op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5950                             msg);
5951                         sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5952                             mflowtype, mflowid, inp->fibnum,
5953                             vrf_id, port);
5954                         goto out;
5955                         /* sa_ignore NOTREACHED */
5956                         break;
5957                 case SCTP_STATE_EMPTY:  /* should not happen */
5958                 case SCTP_STATE_INUSE:  /* should not happen */
5959                 case SCTP_STATE_SHUTDOWN_RECEIVED:      /* This is a peer error */
5960                 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5961                 default:
5962                         goto out;
5963                         /* sa_ignore NOTREACHED */
5964                         break;
5965                 case SCTP_STATE_OPEN:
5966                 case SCTP_STATE_SHUTDOWN_SENT:
5967                         break;
5968                 }
5969                 /* plow through the data chunks while length > offset */
5970                 retval = sctp_process_data(mm, iphlen, &offset, length,
5971                     inp, stcb, net, &high_tsn);
5972                 if (retval == 2) {
5973                         /*
5974                          * The association aborted, NO UNLOCK needed since
5975                          * the association is destroyed.
5976                          */
5977                         stcb = NULL;
5978                         goto out;
5979                 }
5980                 data_processed = 1;
5981                 /*
5982                  * Anything important needs to have been m_copy'ed in
5983                  * process_data
5984                  */
5985         }
5986         /* take care of ecn */
5987         if ((data_processed == 1) &&
5988             (stcb->asoc.ecn_supported == 1) &&
5989             ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5990                 /* Yep, we need to add a ECNE */
5991                 sctp_send_ecn_echo(stcb, net, high_tsn);
5992         }
5993         if ((data_processed == 0) && (fwd_tsn_seen)) {
5994                 int was_a_gap;
5995                 uint32_t highest_tsn;
5996
5997                 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5998                         highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5999                 } else {
6000                         highest_tsn = stcb->asoc.highest_tsn_inside_map;
6001                 }
6002                 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
6003                 stcb->asoc.send_sack = 1;
6004                 sctp_sack_check(stcb, was_a_gap);
6005         } else if (fwd_tsn_seen) {
6006                 stcb->asoc.send_sack = 1;
6007         }
6008         /* trigger send of any chunks in queue... */
6009 trigger_send:
6010 #ifdef SCTP_AUDITING_ENABLED
6011         sctp_audit_log(0xE0, 2);
6012         sctp_auditing(1, inp, stcb, net);
6013 #endif
6014         SCTPDBG(SCTP_DEBUG_INPUT1,
6015             "Check for chunk output prw:%d tqe:%d tf=%d\n",
6016             stcb->asoc.peers_rwnd,
6017             TAILQ_EMPTY(&stcb->asoc.control_send_queue),
6018             stcb->asoc.total_flight);
6019         un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
6020         if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
6021                 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
6022         }
6023         if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
6024             cnt_ctrl_ready ||
6025             stcb->asoc.trigger_reset ||
6026             ((un_sent) &&
6027             (stcb->asoc.peers_rwnd > 0 ||
6028             (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
6029                 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
6030                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
6031                 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
6032         }
6033 #ifdef SCTP_AUDITING_ENABLED
6034         sctp_audit_log(0xE0, 3);
6035         sctp_auditing(2, inp, stcb, net);
6036 #endif
6037 out:
6038         if (stcb != NULL) {
6039                 SCTP_TCB_UNLOCK(stcb);
6040         }
6041         if (inp_decr != NULL) {
6042                 /* reduce ref-count */
6043                 SCTP_INP_WLOCK(inp_decr);
6044                 SCTP_INP_DECR_REF(inp_decr);
6045                 SCTP_INP_WUNLOCK(inp_decr);
6046         }
6047         return;
6048 }
6049
6050 #ifdef INET
6051 void
6052 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
6053 {
6054         struct mbuf *m;
6055         int iphlen;
6056         uint32_t vrf_id = 0;
6057         uint8_t ecn_bits;
6058         struct sockaddr_in src, dst;
6059         struct ip *ip;
6060         struct sctphdr *sh;
6061         struct sctp_chunkhdr *ch;
6062         int length, offset;
6063 #if !defined(SCTP_WITH_NO_CSUM)
6064         uint8_t compute_crc;
6065 #endif
6066         uint32_t mflowid;
6067         uint8_t mflowtype;
6068         uint16_t fibnum;
6069
6070         iphlen = off;
6071         if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
6072                 SCTP_RELEASE_PKT(i_pak);
6073                 return;
6074         }
6075         m = SCTP_HEADER_TO_CHAIN(i_pak);
6076 #ifdef SCTP_MBUF_LOGGING
6077         /* Log in any input mbufs */
6078         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6079                 sctp_log_mbc(m, SCTP_MBUF_INPUT);
6080         }
6081 #endif
6082 #ifdef SCTP_PACKET_LOGGING
6083         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
6084                 sctp_packet_log(m);
6085         }
6086 #endif
6087         SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6088             "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
6089             m->m_pkthdr.len,
6090             if_name(m->m_pkthdr.rcvif),
6091             (int)m->m_pkthdr.csum_flags, CSUM_BITS);
6092         mflowid = m->m_pkthdr.flowid;
6093         mflowtype = M_HASHTYPE_GET(m);
6094         fibnum = M_GETFIB(m);
6095         SCTP_STAT_INCR(sctps_recvpackets);
6096         SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
6097         /* Get IP, SCTP, and first chunk header together in the first mbuf. */
6098         offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6099         if (SCTP_BUF_LEN(m) < offset) {
6100                 if ((m = m_pullup(m, offset)) == NULL) {
6101                         SCTP_STAT_INCR(sctps_hdrops);
6102                         return;
6103                 }
6104         }
6105         ip = mtod(m, struct ip *);
6106         sh = (struct sctphdr *)((caddr_t)ip + iphlen);
6107         ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
6108         offset -= sizeof(struct sctp_chunkhdr);
6109         memset(&src, 0, sizeof(struct sockaddr_in));
6110         src.sin_family = AF_INET;
6111         src.sin_len = sizeof(struct sockaddr_in);
6112         src.sin_port = sh->src_port;
6113         src.sin_addr = ip->ip_src;
6114         memset(&dst, 0, sizeof(struct sockaddr_in));
6115         dst.sin_family = AF_INET;
6116         dst.sin_len = sizeof(struct sockaddr_in);
6117         dst.sin_port = sh->dest_port;
6118         dst.sin_addr = ip->ip_dst;
6119         length = ntohs(ip->ip_len);
6120         /* Validate mbuf chain length with IP payload length. */
6121         if (SCTP_HEADER_LEN(m) != length) {
6122                 SCTPDBG(SCTP_DEBUG_INPUT1,
6123                     "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
6124                 SCTP_STAT_INCR(sctps_hdrops);
6125                 goto out;
6126         }
6127         /* SCTP does not allow broadcasts or multicasts */
6128         if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
6129                 goto out;
6130         }
6131         if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
6132                 goto out;
6133         }
6134         ecn_bits = ip->ip_tos;
6135 #if defined(SCTP_WITH_NO_CSUM)
6136         SCTP_STAT_INCR(sctps_recvnocrc);
6137 #else
6138         if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
6139                 SCTP_STAT_INCR(sctps_recvhwcrc);
6140                 compute_crc = 0;
6141         } else {
6142                 SCTP_STAT_INCR(sctps_recvswcrc);
6143                 compute_crc = 1;
6144         }
6145 #endif
6146         sctp_common_input_processing(&m, iphlen, offset, length,
6147             (struct sockaddr *)&src,
6148             (struct sockaddr *)&dst,
6149             sh, ch,
6150 #if !defined(SCTP_WITH_NO_CSUM)
6151             compute_crc,
6152 #endif
6153             ecn_bits,
6154             mflowtype, mflowid, fibnum,
6155             vrf_id, port);
6156 out:
6157         if (m) {
6158                 sctp_m_freem(m);
6159         }
6160         return;
6161 }
6162
6163 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6164 extern int *sctp_cpuarry;
6165 #endif
6166
6167 int
6168 sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
6169 {
6170         struct mbuf *m;
6171         int off;
6172
6173         m = *mp;
6174         off = *offp;
6175 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6176         if (mp_ncpus > 1) {
6177                 struct ip *ip;
6178                 struct sctphdr *sh;
6179                 int offset;
6180                 int cpu_to_use;
6181                 uint32_t flowid, tag;
6182
6183                 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
6184                         flowid = m->m_pkthdr.flowid;
6185                 } else {
6186                         /*
6187                          * No flow id built by lower layers fix it so we
6188                          * create one.
6189                          */
6190                         offset = off + sizeof(struct sctphdr);
6191                         if (SCTP_BUF_LEN(m) < offset) {
6192                                 if ((m = m_pullup(m, offset)) == NULL) {
6193                                         SCTP_STAT_INCR(sctps_hdrops);
6194                                         return (IPPROTO_DONE);
6195                                 }
6196                         }
6197                         ip = mtod(m, struct ip *);
6198                         sh = (struct sctphdr *)((caddr_t)ip + off);
6199                         tag = htonl(sh->v_tag);
6200                         flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6201                         m->m_pkthdr.flowid = flowid;
6202                         M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
6203                 }
6204                 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6205                 sctp_queue_to_mcore(m, off, cpu_to_use);
6206                 return (IPPROTO_DONE);
6207         }
6208 #endif
6209         sctp_input_with_port(m, off, 0);
6210         return (IPPROTO_DONE);
6211 }
6212 #endif