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