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