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