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