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