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