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