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