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