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