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