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