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