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