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