2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
31 /* $KAME: sctp_input.c,v 1.27 2005/03/06 16:04:17 itojun Exp $ */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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/udp.h>
54 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
56 struct sctp_nets *net;
59 * This now not only stops all cookie timers it also stops any INIT
60 * timers as well. This will make sure that the timers are stopped
61 * in all collision cases.
63 SCTP_TCB_LOCK_ASSERT(stcb);
64 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
65 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
66 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
69 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
70 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
71 sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
74 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
81 sctp_handle_init(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
82 struct sctp_init_chunk *cp, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
83 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id, uint16_t port)
85 struct sctp_init *init;
89 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
93 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
99 /* First are we accepting? */
100 if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) {
101 SCTPDBG(SCTP_DEBUG_INPUT2,
102 "sctp_handle_init: Abort, so_qlimit:%d\n",
103 inp->sctp_socket->so_qlimit);
105 * FIX ME ?? What about TCP model and we have a
106 * match/restart case? Actually no fix is needed. the lookup
107 * will always find the existing assoc so stcb would not be
108 * NULL. It may be questionable to do this since we COULD
109 * just send back the INIT-ACK and hope that the app did
110 * accept()'s by the time the COOKIE was sent. But there is
111 * a price to pay for COOKIE generation and I don't want to
112 * pay it on the chance that the app will actually do some
113 * accepts(). The App just looses and should NOT be in this
116 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
119 *abort_no_unlock = 1;
122 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
124 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
125 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
128 *abort_no_unlock = 1;
131 /* validate parameters */
132 if (init->initiate_tag == 0) {
133 /* protocol error... send abort */
134 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
135 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
138 *abort_no_unlock = 1;
141 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
142 /* invalid parameter... send abort */
143 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
144 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
147 *abort_no_unlock = 1;
150 if (init->num_inbound_streams == 0) {
151 /* protocol error... send abort */
152 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
153 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
156 *abort_no_unlock = 1;
159 if (init->num_outbound_streams == 0) {
160 /* protocol error... send abort */
161 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
162 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
165 *abort_no_unlock = 1;
168 init_limit = offset + ntohs(cp->ch.chunk_length);
169 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
171 /* auth parameter(s) error... send abort */
172 sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, port);
174 *abort_no_unlock = 1;
177 /* send an INIT-ACK w/cookie */
178 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
179 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, port,
180 ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED));
183 SCTP_INP_RUNLOCK(inp);
188 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
192 sctp_is_there_unsent_data(struct sctp_tcb *stcb)
195 struct sctp_stream_queue_pending *sp;
196 struct sctp_stream_out *strq;
197 struct sctp_association *asoc;
200 * This function returns the number of streams that have true unsent
201 * data on them. Note that as it looks through it will clean up any
202 * places that have old data that has been sent but left at top of
206 SCTP_TCB_SEND_LOCK(stcb);
207 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
208 /* Check to see if some data queued */
209 TAILQ_FOREACH(strq, &asoc->out_wheel, next_spoke) {
211 /* sa_ignore FREED_MEMORY */
212 sp = TAILQ_FIRST(&strq->outqueue);
216 if ((sp->msg_is_complete) &&
218 (sp->sender_all_done)) {
220 * We are doing differed cleanup. Last time
221 * through when we took all the data the
222 * sender_all_done was not set.
224 if (sp->put_last_out == 0) {
225 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
226 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
232 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
233 TAILQ_REMOVE(&strq->outqueue, sp, next);
234 sctp_free_remote_addr(sp->net);
236 sctp_m_freem(sp->data);
239 sctp_free_a_strmoq(stcb, sp);
240 goto is_there_another;
247 SCTP_TCB_SEND_UNLOCK(stcb);
248 return (unsent_data);
252 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb,
253 struct sctp_nets *net)
255 struct sctp_init *init;
256 struct sctp_association *asoc;
257 struct sctp_nets *lnet;
262 /* save off parameters */
263 asoc->peer_vtag = ntohl(init->initiate_tag);
264 asoc->peers_rwnd = ntohl(init->a_rwnd);
265 if (TAILQ_FIRST(&asoc->nets)) {
266 /* update any ssthresh's that may have a default */
267 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
268 lnet->ssthresh = asoc->peers_rwnd;
270 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
271 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
275 SCTP_TCB_SEND_LOCK(stcb);
276 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
278 struct sctp_stream_out *outs;
279 struct sctp_stream_queue_pending *sp;
281 /* cut back on number of streams */
282 newcnt = ntohs(init->num_inbound_streams);
283 /* This if is probably not needed but I am cautious */
285 /* First make sure no data chunks are trapped */
286 for (i = newcnt; i < asoc->pre_open_streams; i++) {
287 outs = &asoc->strmout[i];
288 sp = TAILQ_FIRST(&outs->outqueue);
290 TAILQ_REMOVE(&outs->outqueue, sp,
292 asoc->stream_queue_cnt--;
293 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
294 stcb, SCTP_NOTIFY_DATAGRAM_UNSENT,
295 sp, SCTP_SO_NOT_LOCKED);
297 sctp_m_freem(sp->data);
300 sctp_free_remote_addr(sp->net);
303 SCTP_PRINTF("sp:%p tcb:%p weird free case\n",
306 sctp_free_a_strmoq(stcb, sp);
307 /* sa_ignore FREED_MEMORY */
308 sp = TAILQ_FIRST(&outs->outqueue);
312 /* cut back the count and abandon the upper streams */
313 asoc->pre_open_streams = newcnt;
315 SCTP_TCB_SEND_UNLOCK(stcb);
316 asoc->streamoutcnt = asoc->pre_open_streams;
318 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
319 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
320 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
322 /* This is the next one we expect */
323 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
325 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
326 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
327 asoc->last_echo_tsn = asoc->asconf_seq_in;
328 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
329 /* open the requested streams */
331 if (asoc->strmin != NULL) {
332 /* Free the old ones */
333 struct sctp_queued_to_read *ctl;
335 for (i = 0; i < asoc->streamincnt; i++) {
336 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
338 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
339 sctp_free_remote_addr(ctl->whoFrom);
341 sctp_m_freem(ctl->data);
343 sctp_free_a_readq(stcb, ctl);
344 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
347 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
349 asoc->streamincnt = ntohs(init->num_outbound_streams);
350 if (asoc->streamincnt > MAX_SCTP_STREAMS) {
351 asoc->streamincnt = MAX_SCTP_STREAMS;
353 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
354 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
355 if (asoc->strmin == NULL) {
356 /* we didn't get memory for the streams! */
357 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
360 for (i = 0; i < asoc->streamincnt; i++) {
361 asoc->strmin[i].stream_no = i;
362 asoc->strmin[i].last_sequence_delivered = 0xffff;
364 * U-stream ranges will be set when the cookie is unpacked.
365 * Or for the INIT sender they are un set (if pr-sctp not
366 * supported) when the INIT-ACK arrives.
368 TAILQ_INIT(&asoc->strmin[i].inqueue);
369 asoc->strmin[i].delivery_started = 0;
372 * load_address_from_init will put the addresses into the
373 * association when the COOKIE is processed or the INIT-ACK is
374 * processed. Both types of COOKIE's existing and new call this
375 * routine. It will remove addresses that are no longer in the
376 * association (for the restarting case where addresses are
377 * removed). Up front when the INIT arrives we will discard it if it
378 * is a restart and new addresses have been added.
380 /* sa_ignore MEMLEAK */
385 * INIT-ACK message processing/consumption returns value < 0 on error
388 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
389 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
390 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id)
392 struct sctp_association *asoc;
394 int retval, abort_flag;
395 uint32_t initack_limit;
397 /* First verify that we have no illegal param's */
401 op_err = sctp_arethere_unrecognized_parameters(m,
402 (offset + sizeof(struct sctp_init_chunk)),
403 &abort_flag, (struct sctp_chunkhdr *)cp);
405 /* Send an abort and notify peer */
406 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_CAUSE_PROTOCOL_VIOLATION, op_err, SCTP_SO_NOT_LOCKED);
407 *abort_no_unlock = 1;
411 /* process the peer's parameters in the INIT-ACK */
412 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net);
416 initack_limit = offset + ntohs(cp->ch.chunk_length);
417 /* load all addresses */
418 if ((retval = sctp_load_addresses_from_init(stcb, m, iphlen,
419 (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh,
421 /* Huh, we should abort */
422 SCTPDBG(SCTP_DEBUG_INPUT1,
423 "Load addresses from INIT causes an abort %d\n",
425 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
427 *abort_no_unlock = 1;
430 /* if the peer doesn't support asconf, flush the asconf queue */
431 if (asoc->peer_supports_asconf == 0) {
432 struct sctp_asconf_addr *aparam;
434 while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
435 /* sa_ignore FREED_MEMORY */
436 aparam = TAILQ_FIRST(&asoc->asconf_queue);
437 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
438 SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
441 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
442 stcb->asoc.local_hmacs);
444 sctp_queue_op_err(stcb, op_err);
445 /* queuing will steal away the mbuf chain to the out queue */
448 /* extract the cookie and queue it to "echo" it back... */
449 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
450 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
451 stcb->asoc.overall_error_count,
453 SCTP_FROM_SCTP_INPUT,
456 stcb->asoc.overall_error_count = 0;
457 net->error_count = 0;
460 * Cancel the INIT timer, We do this first before queueing the
461 * cookie. We always cancel at the primary to assue that we are
462 * canceling the timer started by the INIT which always goes to the
465 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
466 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
468 /* calculate the RTO */
469 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy);
471 retval = sctp_send_cookie_echo(m, offset, stcb, net);
474 * No cookie, we probably should send a op error. But in any
475 * case if there is no cookie in the INIT-ACK, we can
476 * abandon the peer, its broke.
479 /* We abort with an error of missing mandatory param */
481 sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM);
484 * Expand beyond to include the mandatory
487 struct sctp_inv_mandatory_param *mp;
489 SCTP_BUF_LEN(op_err) =
490 sizeof(struct sctp_inv_mandatory_param);
492 struct sctp_inv_mandatory_param *);
493 /* Subtract the reserved param */
495 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
496 mp->num_param = htonl(1);
497 mp->param = htons(SCTP_STATE_COOKIE);
500 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
501 sh, op_err, 0, net->port);
502 *abort_no_unlock = 1;
510 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
511 struct sctp_tcb *stcb, struct sctp_nets *net)
513 struct sockaddr_storage store;
514 struct sockaddr_in *sin;
515 struct sockaddr_in6 *sin6;
516 struct sctp_nets *r_net;
520 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
524 sin = (struct sockaddr_in *)&store;
525 sin6 = (struct sockaddr_in6 *)&store;
527 memset(&store, 0, sizeof(store));
528 if (cp->heartbeat.hb_info.addr_family == AF_INET &&
529 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
530 sin->sin_family = cp->heartbeat.hb_info.addr_family;
531 sin->sin_len = cp->heartbeat.hb_info.addr_len;
532 sin->sin_port = stcb->rport;
533 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
534 sizeof(sin->sin_addr));
535 } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 &&
536 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
537 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
538 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
539 sin6->sin6_port = stcb->rport;
540 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
541 sizeof(sin6->sin6_addr));
545 r_net = sctp_findnet(stcb, (struct sockaddr *)sin);
547 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
550 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
551 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
552 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
554 * If the its a HB and it's random value is correct when can
555 * confirm the destination.
557 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
558 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
559 stcb->asoc.primary_destination = r_net;
560 r_net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
561 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
562 r_net = TAILQ_FIRST(&stcb->asoc.nets);
563 if (r_net != stcb->asoc.primary_destination) {
565 * first one on the list is NOT the primary
566 * sctp_cmpaddr() is much more efficent if
567 * the primary is the first on the list,
570 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
571 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
575 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
576 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
578 r_net->error_count = 0;
579 r_net->hb_responded = 1;
580 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
581 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
582 if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
583 r_net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE;
584 r_net->dest_state |= SCTP_ADDR_REACHABLE;
585 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
586 SCTP_HEARTBEAT_SUCCESS, (void *)r_net, SCTP_SO_NOT_LOCKED);
587 /* now was it the primary? if so restore */
588 if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
589 (void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net);
593 * JRS 5/14/07 - If CMT PF is on and the destination is in PF state,
594 * set the destination to active state and set the cwnd to one or
595 * two MTU's based on whether PF1 or PF2 is being used. If a T3
596 * timer is running, for the destination, stop the timer because a
597 * PF-heartbeat was received.
599 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
600 SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
601 (net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) {
602 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
603 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
605 SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
607 net->dest_state &= ~SCTP_ADDR_PF;
608 net->cwnd = net->mtu * SCTP_BASE_SYSCTL(sctp_cmt_pf);
609 SCTPDBG(SCTP_DEBUG_INPUT1, "Destination %p moved from PF to reachable with cwnd %d.\n",
612 /* Now lets do a RTO with this */
613 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy);
614 /* Mobility adaptation */
616 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
617 SCTP_MOBILITY_BASE) ||
618 sctp_is_mobility_feature_on(stcb->sctp_ep,
619 SCTP_MOBILITY_FASTHANDOFF)) &&
620 sctp_is_mobility_feature_on(stcb->sctp_ep,
621 SCTP_MOBILITY_PRIM_DELETED)) {
623 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7);
624 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
625 SCTP_MOBILITY_FASTHANDOFF)) {
626 sctp_assoc_immediate_retrans(stcb,
627 stcb->asoc.primary_destination);
629 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
630 SCTP_MOBILITY_BASE)) {
631 sctp_move_chunks_from_deleted_prim(stcb,
632 stcb->asoc.primary_destination);
634 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
635 stcb->asoc.deleted_primary);
641 sctp_handle_abort(struct sctp_abort_chunk *cp,
642 struct sctp_tcb *stcb, struct sctp_nets *net)
644 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
649 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
653 /* stop any receive timers */
654 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
655 /* notify user of the abort and clean up... */
656 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED);
658 #if defined(SCTP_PANIC_ON_ABORT)
659 printf("stcb:%p state:%d rport:%d net:%p\n",
660 stcb, stcb->asoc.state, stcb->rport, net);
661 if (!(stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
662 panic("Received an ABORT");
664 printf("No panic its in state %x closed\n", stcb->asoc.state);
667 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
668 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
669 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
670 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
672 #ifdef SCTP_ASOCLOG_OF_TSNS
673 sctp_print_out_track_log(stcb);
675 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
676 so = SCTP_INP_SO(stcb->sctp_ep);
677 atomic_add_int(&stcb->asoc.refcnt, 1);
678 SCTP_TCB_UNLOCK(stcb);
679 SCTP_SOCKET_LOCK(so, 1);
681 atomic_subtract_int(&stcb->asoc.refcnt, 1);
683 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
684 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
685 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
686 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
687 SCTP_SOCKET_UNLOCK(so, 1);
689 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
693 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
694 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
696 struct sctp_association *asoc;
697 int some_on_streamwheel;
699 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
704 SCTPDBG(SCTP_DEBUG_INPUT2,
705 "sctp_handle_shutdown: handling SHUTDOWN\n");
709 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
710 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
713 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
714 /* Shutdown NOT the expected size */
717 sctp_update_acked(stcb, cp, net, abort_flag);
719 if (asoc->control_pdapi) {
721 * With a normal shutdown we assume the end of last record.
723 SCTP_INP_READ_LOCK(stcb->sctp_ep);
724 asoc->control_pdapi->end_added = 1;
725 asoc->control_pdapi->pdapi_aborted = 1;
726 asoc->control_pdapi = NULL;
727 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
728 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
729 so = SCTP_INP_SO(stcb->sctp_ep);
730 atomic_add_int(&stcb->asoc.refcnt, 1);
731 SCTP_TCB_UNLOCK(stcb);
732 SCTP_SOCKET_LOCK(so, 1);
734 atomic_subtract_int(&stcb->asoc.refcnt, 1);
735 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
736 /* assoc was freed while we were unlocked */
737 SCTP_SOCKET_UNLOCK(so, 1);
741 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
742 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
743 SCTP_SOCKET_UNLOCK(so, 1);
746 /* goto SHUTDOWN_RECEIVED state to block new requests */
747 if (stcb->sctp_socket) {
748 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
749 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
750 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
751 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
752 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
754 * notify upper layer that peer has initiated a
757 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
760 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
763 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
765 * stop the shutdown timer, since we WILL move to
768 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
770 /* Now is there unsent data on a stream somewhere? */
771 some_on_streamwheel = sctp_is_there_unsent_data(stcb);
773 if (!TAILQ_EMPTY(&asoc->send_queue) ||
774 !TAILQ_EMPTY(&asoc->sent_queue) ||
775 some_on_streamwheel) {
776 /* By returning we will push more data out */
779 /* no outstanding data to send, so move on... */
780 /* send SHUTDOWN-ACK */
781 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
782 /* move to SHUTDOWN-ACK-SENT state */
783 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
784 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
785 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
787 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
788 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
789 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
790 SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
791 /* start SHUTDOWN timer */
792 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
798 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp,
799 struct sctp_tcb *stcb, struct sctp_nets *net)
801 struct sctp_association *asoc;
803 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
806 so = SCTP_INP_SO(stcb->sctp_ep);
808 SCTPDBG(SCTP_DEBUG_INPUT2,
809 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
814 /* process according to association state */
815 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
816 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
817 /* unexpected SHUTDOWN-ACK... so ignore... */
818 SCTP_TCB_UNLOCK(stcb);
821 if (asoc->control_pdapi) {
823 * With a normal shutdown we assume the end of last record.
825 SCTP_INP_READ_LOCK(stcb->sctp_ep);
826 asoc->control_pdapi->end_added = 1;
827 asoc->control_pdapi->pdapi_aborted = 1;
828 asoc->control_pdapi = NULL;
829 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
830 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
831 atomic_add_int(&stcb->asoc.refcnt, 1);
832 SCTP_TCB_UNLOCK(stcb);
833 SCTP_SOCKET_LOCK(so, 1);
835 atomic_subtract_int(&stcb->asoc.refcnt, 1);
836 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
837 /* assoc was freed while we were unlocked */
838 SCTP_SOCKET_UNLOCK(so, 1);
842 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
843 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
844 SCTP_SOCKET_UNLOCK(so, 1);
847 /* are the queues empty? */
848 if (!TAILQ_EMPTY(&asoc->send_queue) ||
849 !TAILQ_EMPTY(&asoc->sent_queue) ||
850 !TAILQ_EMPTY(&asoc->out_wheel)) {
851 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED);
854 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
855 /* send SHUTDOWN-COMPLETE */
856 sctp_send_shutdown_complete(stcb, net);
857 /* notify upper layer protocol */
858 if (stcb->sctp_socket) {
859 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
860 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
861 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
862 /* Set the connected flag to disconnected */
863 stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
866 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
867 /* free the TCB but first save off the ep */
868 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
869 atomic_add_int(&stcb->asoc.refcnt, 1);
870 SCTP_TCB_UNLOCK(stcb);
871 SCTP_SOCKET_LOCK(so, 1);
873 atomic_subtract_int(&stcb->asoc.refcnt, 1);
875 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
876 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
877 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
878 SCTP_SOCKET_UNLOCK(so, 1);
883 * Skip past the param header and then we will find the chunk that caused the
884 * problem. There are two possiblities ASCONF or FWD-TSN other than that and
885 * our peer must be broken.
888 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
889 struct sctp_nets *net)
891 struct sctp_chunkhdr *chk;
893 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
894 switch (chk->chunk_type) {
895 case SCTP_ASCONF_ACK:
897 sctp_asconf_cleanup(stcb, net);
899 case SCTP_FORWARD_CUM_TSN:
900 stcb->asoc.peer_supports_prsctp = 0;
903 SCTPDBG(SCTP_DEBUG_INPUT2,
904 "Peer does not support chunk type %d(%x)??\n",
905 chk->chunk_type, (uint32_t) chk->chunk_type);
911 * Skip past the param header and then we will find the param that caused the
912 * problem. There are a number of param's in a ASCONF OR the prsctp param
913 * these will turn of specific features.
916 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
918 struct sctp_paramhdr *pbad;
921 switch (ntohs(pbad->param_type)) {
923 case SCTP_PRSCTP_SUPPORTED:
924 stcb->asoc.peer_supports_prsctp = 0;
926 case SCTP_SUPPORTED_CHUNK_EXT:
928 /* draft-ietf-tsvwg-addip-sctp */
929 case SCTP_ECN_NONCE_SUPPORTED:
930 stcb->asoc.peer_supports_ecn_nonce = 0;
931 stcb->asoc.ecn_nonce_allowed = 0;
932 stcb->asoc.ecn_allowed = 0;
934 case SCTP_ADD_IP_ADDRESS:
935 case SCTP_DEL_IP_ADDRESS:
936 case SCTP_SET_PRIM_ADDR:
937 stcb->asoc.peer_supports_asconf = 0;
939 case SCTP_SUCCESS_REPORT:
940 case SCTP_ERROR_CAUSE_IND:
941 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
942 SCTPDBG(SCTP_DEBUG_INPUT2,
943 "Turning off ASCONF to this strange peer\n");
944 stcb->asoc.peer_supports_asconf = 0;
947 SCTPDBG(SCTP_DEBUG_INPUT2,
948 "Peer does not support param type %d(%x)??\n",
949 pbad->param_type, (uint32_t) pbad->param_type);
955 sctp_handle_error(struct sctp_chunkhdr *ch,
956 struct sctp_tcb *stcb, struct sctp_nets *net)
959 struct sctp_paramhdr *phdr;
962 struct sctp_association *asoc;
965 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
970 /* parse through all of the errors and process */
972 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
973 sizeof(struct sctp_chunkhdr));
974 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
975 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
976 /* Process an Error Cause */
977 error_type = ntohs(phdr->param_type);
978 error_len = ntohs(phdr->param_length);
979 if ((error_len > chklen) || (error_len == 0)) {
980 /* invalid param length for this param */
981 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
985 switch (error_type) {
986 case SCTP_CAUSE_INVALID_STREAM:
987 case SCTP_CAUSE_MISSING_PARAM:
988 case SCTP_CAUSE_INVALID_PARAM:
989 case SCTP_CAUSE_NO_USER_DATA:
990 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
993 case SCTP_CAUSE_STALE_COOKIE:
995 * We only act if we have echoed a cookie and are
998 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1001 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1002 /* Save the time doubled */
1003 asoc->cookie_preserve_req = ntohl(*p) << 1;
1004 asoc->stale_cookie_count++;
1005 if (asoc->stale_cookie_count >
1006 asoc->max_init_times) {
1007 sctp_abort_notification(stcb, 0, SCTP_SO_NOT_LOCKED);
1008 /* now free the asoc */
1009 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1010 so = SCTP_INP_SO(stcb->sctp_ep);
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);
1017 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1018 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1019 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1020 SCTP_SOCKET_UNLOCK(so, 1);
1024 /* blast back to INIT state */
1025 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1026 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1028 sctp_stop_all_cookie_timers(stcb);
1029 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1032 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1034 * Nothing we can do here, we don't do hostname
1035 * addresses so if the peer does not like my IPv6
1036 * (or IPv4 for that matter) it does not matter. If
1037 * they don't support that type of address, they can
1038 * NOT possibly get that packet type... i.e. with no
1039 * IPv6 you can't recieve a IPv6 packet. so we can
1040 * safely ignore this one. If we ever added support
1041 * for HOSTNAME Addresses, then we would need to do
1045 case SCTP_CAUSE_UNRECOG_CHUNK:
1046 sctp_process_unrecog_chunk(stcb, phdr, net);
1048 case SCTP_CAUSE_UNRECOG_PARAM:
1049 sctp_process_unrecog_param(stcb, phdr);
1051 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1053 * We ignore this since the timer will drive out a
1054 * new cookie anyway and there timer will drive us
1055 * to send a SHUTDOWN_COMPLETE. We can't send one
1056 * here since we don't have their tag.
1059 case SCTP_CAUSE_DELETING_LAST_ADDR:
1060 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1061 case SCTP_CAUSE_DELETING_SRC_ADDR:
1063 * We should NOT get these here, but in a
1066 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1069 case SCTP_CAUSE_OUT_OF_RESC:
1071 * And what, pray tell do we do with the fact that
1072 * the peer is out of resources? Not really sure we
1073 * could do anything but abort. I suspect this
1074 * should have came WITH an abort instead of in a
1079 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1083 adjust = SCTP_SIZE32(error_len);
1085 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1091 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1092 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1093 struct sctp_nets *net, int *abort_no_unlock, uint32_t vrf_id)
1095 struct sctp_init_ack *init_ack;
1096 struct mbuf *op_err;
1098 SCTPDBG(SCTP_DEBUG_INPUT2,
1099 "sctp_handle_init_ack: handling INIT-ACK\n");
1102 SCTPDBG(SCTP_DEBUG_INPUT2,
1103 "sctp_handle_init_ack: TCB is null\n");
1106 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1107 /* Invalid length */
1108 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1109 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1110 op_err, 0, net->port);
1111 *abort_no_unlock = 1;
1114 init_ack = &cp->init;
1115 /* validate parameters */
1116 if (init_ack->initiate_tag == 0) {
1117 /* protocol error... send an abort */
1118 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1119 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1120 op_err, 0, net->port);
1121 *abort_no_unlock = 1;
1124 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1125 /* protocol error... send an abort */
1126 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1127 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1128 op_err, 0, net->port);
1129 *abort_no_unlock = 1;
1132 if (init_ack->num_inbound_streams == 0) {
1133 /* protocol error... send an abort */
1134 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1135 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1136 op_err, 0, net->port);
1137 *abort_no_unlock = 1;
1140 if (init_ack->num_outbound_streams == 0) {
1141 /* protocol error... send an abort */
1142 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
1143 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
1144 op_err, 0, net->port);
1145 *abort_no_unlock = 1;
1148 /* process according to association state... */
1149 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1150 case SCTP_STATE_COOKIE_WAIT:
1151 /* this is the expected state for this chunk */
1152 /* process the INIT-ACK parameters */
1153 if (stcb->asoc.primary_destination->dest_state &
1154 SCTP_ADDR_UNCONFIRMED) {
1156 * The primary is where we sent the INIT, we can
1157 * always consider it confirmed when the INIT-ACK is
1158 * returned. Do this before we load addresses
1161 stcb->asoc.primary_destination->dest_state &=
1162 ~SCTP_ADDR_UNCONFIRMED;
1163 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1164 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1166 if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb,
1167 net, abort_no_unlock, vrf_id) < 0) {
1168 /* error in parsing parameters */
1171 /* update our state */
1172 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1173 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1175 /* reset the RTO calc */
1176 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1177 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1178 stcb->asoc.overall_error_count,
1180 SCTP_FROM_SCTP_INPUT,
1183 stcb->asoc.overall_error_count = 0;
1184 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1186 * collapse the init timer back in case of a exponential
1189 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1192 * the send at the end of the inbound data processing will
1193 * cause the cookie to be sent
1196 case SCTP_STATE_SHUTDOWN_SENT:
1197 /* incorrect state... discard */
1199 case SCTP_STATE_COOKIE_ECHOED:
1200 /* incorrect state... discard */
1202 case SCTP_STATE_OPEN:
1203 /* incorrect state... discard */
1205 case SCTP_STATE_EMPTY:
1206 case SCTP_STATE_INUSE:
1208 /* incorrect state... discard */
1212 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1218 * handle a state cookie for an existing association m: input packet mbuf
1219 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1220 * "split" mbuf and the cookie signature does not exist offset: offset into
1221 * mbuf to the cookie-echo chunk
1223 static struct sctp_tcb *
1224 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1225 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1226 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
1227 struct sockaddr *init_src, int *notification, sctp_assoc_t * sac_assoc_id,
1230 struct sctp_association *asoc;
1231 struct sctp_init_chunk *init_cp, init_buf;
1232 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1234 int init_offset, initack_offset, i;
1239 /* I know that the TCB is non-NULL from the caller */
1241 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1242 if (asoc->cookie_how[how_indx] == 0)
1245 if (how_indx < sizeof(asoc->cookie_how)) {
1246 asoc->cookie_how[how_indx] = 1;
1248 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1249 /* SHUTDOWN came in after sending INIT-ACK */
1250 struct mbuf *op_err;
1251 struct sctp_paramhdr *ph;
1253 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1254 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
1255 0, M_DONTWAIT, 1, MT_DATA);
1256 if (op_err == NULL) {
1260 /* pre-reserve some space */
1262 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
1264 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
1266 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
1267 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1269 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
1270 ph = mtod(op_err, struct sctp_paramhdr *);
1271 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1272 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1273 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
1275 if (how_indx < sizeof(asoc->cookie_how))
1276 asoc->cookie_how[how_indx] = 2;
1280 * find and validate the INIT chunk in the cookie (peer's info) the
1281 * INIT should start after the cookie-echo header struct (chunk
1282 * header, state cookie header struct)
1284 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1286 init_cp = (struct sctp_init_chunk *)
1287 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1288 (uint8_t *) & init_buf);
1289 if (init_cp == NULL) {
1290 /* could not pull a INIT chunk in cookie */
1293 chk_length = ntohs(init_cp->ch.chunk_length);
1294 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1298 * find and validate the INIT-ACK chunk in the cookie (my info) the
1299 * INIT-ACK follows the INIT chunk
1301 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1302 initack_cp = (struct sctp_init_ack_chunk *)
1303 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1304 (uint8_t *) & initack_buf);
1305 if (initack_cp == NULL) {
1306 /* could not pull INIT-ACK chunk in cookie */
1309 chk_length = ntohs(initack_cp->ch.chunk_length);
1310 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1313 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1314 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1316 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1317 * to get into the OPEN state
1319 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1321 * Opps, this means that we somehow generated two vtag's
1322 * the same. I.e. we did:
1324 * <---INIT(tag=a)------
1325 * ----INIT-ACK(tag=t)-->
1326 * ----INIT(tag=t)------> *1
1327 * <---INIT-ACK(tag=a)---
1328 * <----CE(tag=t)------------- *2
1330 * At point *1 we should be generating a different
1331 * tag t'. Which means we would throw away the CE and send
1332 * ours instead. Basically this is case C (throw away side).
1334 if (how_indx < sizeof(asoc->cookie_how))
1335 asoc->cookie_how[how_indx] = 17;
1339 switch SCTP_GET_STATE
1341 case SCTP_STATE_COOKIE_WAIT:
1342 case SCTP_STATE_COOKIE_ECHOED:
1344 * INIT was sent but got a COOKIE_ECHO with the
1345 * correct tags... just accept it...but we must
1346 * process the init so that we can make sure we have
1347 * the right seq no's.
1349 /* First we must process the INIT !! */
1350 retval = sctp_process_init(init_cp, stcb, net);
1352 if (how_indx < sizeof(asoc->cookie_how))
1353 asoc->cookie_how[how_indx] = 3;
1356 /* we have already processed the INIT so no problem */
1357 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1358 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1359 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1360 /* update current state */
1361 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1362 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1364 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1366 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1367 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1368 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1369 stcb->sctp_ep, stcb, asoc->primary_destination);
1371 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1372 sctp_stop_all_cookie_timers(stcb);
1373 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1374 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1375 (inp->sctp_socket->so_qlimit == 0)
1377 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1382 * Here is where collision would go if we
1383 * did a connect() and instead got a
1384 * init/init-ack/cookie done before the
1385 * init-ack came back..
1387 stcb->sctp_ep->sctp_flags |=
1388 SCTP_PCB_FLAGS_CONNECTED;
1389 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1390 so = SCTP_INP_SO(stcb->sctp_ep);
1391 atomic_add_int(&stcb->asoc.refcnt, 1);
1392 SCTP_TCB_UNLOCK(stcb);
1393 SCTP_SOCKET_LOCK(so, 1);
1394 SCTP_TCB_LOCK(stcb);
1395 atomic_add_int(&stcb->asoc.refcnt, -1);
1396 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1397 SCTP_SOCKET_UNLOCK(so, 1);
1401 soisconnected(stcb->sctp_socket);
1402 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1403 SCTP_SOCKET_UNLOCK(so, 1);
1406 /* notify upper layer */
1407 *notification = SCTP_NOTIFY_ASSOC_UP;
1409 * since we did not send a HB make sure we don't
1412 net->hb_responded = 1;
1413 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1414 &cookie->time_entered, sctp_align_unsafe_makecopy);
1416 if (stcb->asoc.sctp_autoclose_ticks &&
1417 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1418 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1424 * we're in the OPEN state (or beyond), so peer must
1425 * have simply lost the COOKIE-ACK
1429 sctp_stop_all_cookie_timers(stcb);
1431 * We ignore the return code here.. not sure if we should
1432 * somehow abort.. but we do have an existing asoc. This
1433 * really should not fail.
1435 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1436 init_offset + sizeof(struct sctp_init_chunk),
1437 initack_offset, sh, init_src)) {
1438 if (how_indx < sizeof(asoc->cookie_how))
1439 asoc->cookie_how[how_indx] = 4;
1442 /* respond with a COOKIE-ACK */
1443 sctp_toss_old_cookies(stcb, asoc);
1444 sctp_send_cookie_ack(stcb);
1445 if (how_indx < sizeof(asoc->cookie_how))
1446 asoc->cookie_how[how_indx] = 5;
1449 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1450 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1451 cookie->tie_tag_my_vtag == 0 &&
1452 cookie->tie_tag_peer_vtag == 0) {
1454 * case C in Section 5.2.4 Table 2: XMOO silently discard
1456 if (how_indx < sizeof(asoc->cookie_how))
1457 asoc->cookie_how[how_indx] = 6;
1460 if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag &&
1461 (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag ||
1462 init_cp->init.initiate_tag == 0)) {
1464 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1465 * should be ok, re-accept peer info
1467 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1469 * Extension of case C. If we hit this, then the
1470 * random number generator returned the same vtag
1471 * when we first sent our INIT-ACK and when we later
1472 * sent our INIT. The side with the seq numbers that
1473 * are different will be the one that normnally
1474 * would have hit case C. This in effect "extends"
1475 * our vtags in this collision case to be 64 bits.
1476 * The same collision could occur aka you get both
1477 * vtag and seq number the same twice in a row.. but
1478 * is much less likely. If it did happen then we
1479 * would proceed through and bring up the assoc.. we
1480 * may end up with the wrong stream setup however..
1481 * which would be bad.. but there is no way to
1482 * tell.. until we send on a stream that does not
1485 if (how_indx < sizeof(asoc->cookie_how))
1486 asoc->cookie_how[how_indx] = 7;
1490 if (how_indx < sizeof(asoc->cookie_how))
1491 asoc->cookie_how[how_indx] = 8;
1492 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1493 sctp_stop_all_cookie_timers(stcb);
1495 * since we did not send a HB make sure we don't double
1498 net->hb_responded = 1;
1499 if (stcb->asoc.sctp_autoclose_ticks &&
1500 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1501 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1504 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1505 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1507 /* Note last_cwr_tsn? where is this used? */
1508 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1509 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1511 * Ok the peer probably discarded our data (if we
1512 * echoed a cookie+data). So anything on the
1513 * sent_queue should be marked for retransmit, we
1514 * may not get something to kick us so it COULD
1515 * still take a timeout to move these.. but it can't
1516 * hurt to mark them.
1518 struct sctp_tmit_chunk *chk;
1520 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1521 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1522 chk->sent = SCTP_DATAGRAM_RESEND;
1523 sctp_flight_size_decrease(chk);
1524 sctp_total_flight_decrease(stcb, chk);
1525 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1531 /* process the INIT info (peer's info) */
1532 retval = sctp_process_init(init_cp, stcb, net);
1534 if (how_indx < sizeof(asoc->cookie_how))
1535 asoc->cookie_how[how_indx] = 9;
1538 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1539 init_offset + sizeof(struct sctp_init_chunk),
1540 initack_offset, sh, init_src)) {
1541 if (how_indx < sizeof(asoc->cookie_how))
1542 asoc->cookie_how[how_indx] = 10;
1545 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1546 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1547 *notification = SCTP_NOTIFY_ASSOC_UP;
1549 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1550 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1551 (inp->sctp_socket->so_qlimit == 0)) {
1552 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1556 stcb->sctp_ep->sctp_flags |=
1557 SCTP_PCB_FLAGS_CONNECTED;
1558 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1559 so = SCTP_INP_SO(stcb->sctp_ep);
1560 atomic_add_int(&stcb->asoc.refcnt, 1);
1561 SCTP_TCB_UNLOCK(stcb);
1562 SCTP_SOCKET_LOCK(so, 1);
1563 SCTP_TCB_LOCK(stcb);
1564 atomic_add_int(&stcb->asoc.refcnt, -1);
1565 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1566 SCTP_SOCKET_UNLOCK(so, 1);
1570 soisconnected(stcb->sctp_socket);
1571 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1572 SCTP_SOCKET_UNLOCK(so, 1);
1575 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1576 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1578 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1579 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1580 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1581 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1583 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1585 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1586 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1587 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1588 stcb->sctp_ep, stcb, asoc->primary_destination);
1590 sctp_stop_all_cookie_timers(stcb);
1591 sctp_toss_old_cookies(stcb, asoc);
1592 sctp_send_cookie_ack(stcb);
1595 * only if we have retrans set do we do this. What
1596 * this call does is get only the COOKIE-ACK out and
1597 * then when we return the normal call to
1598 * sctp_chunk_output will get the retrans out behind
1601 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1603 if (how_indx < sizeof(asoc->cookie_how))
1604 asoc->cookie_how[how_indx] = 11;
1608 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1609 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1610 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1611 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1612 cookie->tie_tag_peer_vtag != 0) {
1613 struct sctpasochead *head;
1616 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1619 if (how_indx < sizeof(asoc->cookie_how))
1620 asoc->cookie_how[how_indx] = 12;
1621 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1622 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1624 *sac_assoc_id = sctp_get_associd(stcb);
1625 /* notify upper layer */
1626 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1627 atomic_add_int(&stcb->asoc.refcnt, 1);
1628 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1629 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1630 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1631 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1633 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1634 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1635 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1636 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1638 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1639 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1640 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1641 stcb->sctp_ep, stcb, asoc->primary_destination);
1643 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1644 /* move to OPEN state, if not in SHUTDOWN_SENT */
1645 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1647 asoc->pre_open_streams =
1648 ntohs(initack_cp->init.num_outbound_streams);
1649 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1650 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1651 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1653 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1654 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1656 asoc->str_reset_seq_in = asoc->init_seq_number;
1658 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1659 if (asoc->mapping_array) {
1660 memset(asoc->mapping_array, 0,
1661 asoc->mapping_array_size);
1663 SCTP_TCB_UNLOCK(stcb);
1664 SCTP_INP_INFO_WLOCK();
1665 SCTP_INP_WLOCK(stcb->sctp_ep);
1666 SCTP_TCB_LOCK(stcb);
1667 atomic_add_int(&stcb->asoc.refcnt, -1);
1668 /* send up all the data */
1669 SCTP_TCB_SEND_LOCK(stcb);
1671 sctp_report_all_outbound(stcb, 1, SCTP_SO_NOT_LOCKED);
1672 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1673 stcb->asoc.strmout[i].stream_no = i;
1674 stcb->asoc.strmout[i].next_sequence_sent = 0;
1675 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1677 /* process the INIT-ACK info (my info) */
1678 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1679 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1681 /* pull from vtag hash */
1682 LIST_REMOVE(stcb, sctp_asocs);
1683 /* re-insert to new vtag position */
1684 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1685 SCTP_BASE_INFO(hashasocmark))];
1687 * put it in the bucket in the vtag hash of assoc's for the
1690 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1692 /* Is this the first restart? */
1693 if (stcb->asoc.in_restart_hash == 0) {
1694 /* Ok add it to assoc_id vtag hash */
1695 head = &SCTP_BASE_INFO(sctp_restarthash)[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
1696 SCTP_BASE_INFO(hashrestartmark))];
1697 LIST_INSERT_HEAD(head, stcb, sctp_tcbrestarhash);
1698 stcb->asoc.in_restart_hash = 1;
1700 /* process the INIT info (peer's info) */
1701 SCTP_TCB_SEND_UNLOCK(stcb);
1702 SCTP_INP_WUNLOCK(stcb->sctp_ep);
1703 SCTP_INP_INFO_WUNLOCK();
1705 retval = sctp_process_init(init_cp, stcb, net);
1707 if (how_indx < sizeof(asoc->cookie_how))
1708 asoc->cookie_how[how_indx] = 13;
1713 * since we did not send a HB make sure we don't double
1716 net->hb_responded = 1;
1718 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1719 init_offset + sizeof(struct sctp_init_chunk),
1720 initack_offset, sh, init_src)) {
1721 if (how_indx < sizeof(asoc->cookie_how))
1722 asoc->cookie_how[how_indx] = 14;
1726 /* respond with a COOKIE-ACK */
1727 sctp_stop_all_cookie_timers(stcb);
1728 sctp_toss_old_cookies(stcb, asoc);
1729 sctp_send_cookie_ack(stcb);
1730 if (how_indx < sizeof(asoc->cookie_how))
1731 asoc->cookie_how[how_indx] = 15;
1735 if (how_indx < sizeof(asoc->cookie_how))
1736 asoc->cookie_how[how_indx] = 16;
1737 /* all other cases... */
1743 * handle a state cookie for a new association m: input packet mbuf chain--
1744 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
1745 * and the cookie signature does not exist offset: offset into mbuf to the
1746 * cookie-echo chunk length: length of the cookie chunk to: where the init
1747 * was from returns a new TCB
1749 static struct sctp_tcb *
1750 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1751 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1752 struct sctp_inpcb *inp, struct sctp_nets **netp,
1753 struct sockaddr *init_src, int *notification,
1754 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1755 uint32_t vrf_id, uint16_t port)
1757 struct sctp_tcb *stcb;
1758 struct sctp_init_chunk *init_cp, init_buf;
1759 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1760 struct sockaddr_storage sa_store;
1761 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
1762 struct sockaddr_in *sin;
1763 struct sockaddr_in6 *sin6;
1764 struct sctp_association *asoc;
1766 int init_offset, initack_offset, initack_limit;
1770 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
1772 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1775 so = SCTP_INP_SO(inp);
1779 * find and validate the INIT chunk in the cookie (peer's info) the
1780 * INIT should start after the cookie-echo header struct (chunk
1781 * header, state cookie header struct)
1783 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
1784 init_cp = (struct sctp_init_chunk *)
1785 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1786 (uint8_t *) & init_buf);
1787 if (init_cp == NULL) {
1788 /* could not pull a INIT chunk in cookie */
1789 SCTPDBG(SCTP_DEBUG_INPUT1,
1790 "process_cookie_new: could not pull INIT chunk hdr\n");
1793 chk_length = ntohs(init_cp->ch.chunk_length);
1794 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1795 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
1798 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1800 * find and validate the INIT-ACK chunk in the cookie (my info) the
1801 * INIT-ACK follows the INIT chunk
1803 initack_cp = (struct sctp_init_ack_chunk *)
1804 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1805 (uint8_t *) & initack_buf);
1806 if (initack_cp == NULL) {
1807 /* could not pull INIT-ACK chunk in cookie */
1808 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
1811 chk_length = ntohs(initack_cp->ch.chunk_length);
1812 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1816 * NOTE: We can't use the INIT_ACK's chk_length to determine the
1817 * "initack_limit" value. This is because the chk_length field
1818 * includes the length of the cookie, but the cookie is omitted when
1819 * the INIT and INIT_ACK are tacked onto the cookie...
1821 initack_limit = offset + cookie_len;
1824 * now that we know the INIT/INIT-ACK are in place, create a new TCB
1829 * Here we do a trick, we set in NULL for the proc/thread argument.
1830 * We do this since in effect we only use the p argument when the
1831 * socket is unbound and we must do an implicit bind. Since we are
1832 * getting a cookie, we cannot be unbound.
1834 stcb = sctp_aloc_assoc(inp, init_src, 0, &error,
1835 ntohl(initack_cp->init.initiate_tag), vrf_id,
1836 (struct thread *)NULL
1839 struct mbuf *op_err;
1841 /* memory problem? */
1842 SCTPDBG(SCTP_DEBUG_INPUT1,
1843 "process_cookie_new: no room for another TCB!\n");
1844 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1846 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
1847 sh, op_err, vrf_id, port);
1850 /* get the correct sctp_nets */
1852 *netp = sctp_findnet(stcb, init_src);
1855 /* get scope variables out of cookie */
1856 asoc->ipv4_local_scope = cookie->ipv4_scope;
1857 asoc->site_scope = cookie->site_scope;
1858 asoc->local_scope = cookie->local_scope;
1859 asoc->loopback_scope = cookie->loopback_scope;
1861 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
1862 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
1863 struct mbuf *op_err;
1866 * Houston we have a problem. The EP changed while the
1867 * cookie was in flight. Only recourse is to abort the
1870 atomic_add_int(&stcb->asoc.refcnt, 1);
1871 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1872 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
1873 sh, op_err, vrf_id, port);
1874 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1875 SCTP_TCB_UNLOCK(stcb);
1876 SCTP_SOCKET_LOCK(so, 1);
1877 SCTP_TCB_LOCK(stcb);
1879 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
1880 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1881 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1882 SCTP_SOCKET_UNLOCK(so, 1);
1884 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1887 /* process the INIT-ACK info (my info) */
1888 old_tag = asoc->my_vtag;
1889 asoc->assoc_id = asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1890 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1891 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1892 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1893 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1894 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1895 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1896 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1897 asoc->str_reset_seq_in = asoc->init_seq_number;
1899 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1901 /* process the INIT info (peer's info) */
1903 retval = sctp_process_init(init_cp, stcb, *netp);
1907 atomic_add_int(&stcb->asoc.refcnt, 1);
1908 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1909 SCTP_TCB_UNLOCK(stcb);
1910 SCTP_SOCKET_LOCK(so, 1);
1911 SCTP_TCB_LOCK(stcb);
1913 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1914 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1915 SCTP_SOCKET_UNLOCK(so, 1);
1917 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1920 /* load all addresses */
1921 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1922 init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
1924 atomic_add_int(&stcb->asoc.refcnt, 1);
1925 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1926 SCTP_TCB_UNLOCK(stcb);
1927 SCTP_SOCKET_LOCK(so, 1);
1928 SCTP_TCB_LOCK(stcb);
1930 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
1931 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1932 SCTP_SOCKET_UNLOCK(so, 1);
1934 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1938 * verify any preceding AUTH chunk that was skipped
1940 /* pull the local authentication parameters from the cookie/init-ack */
1941 sctp_auth_get_cookie_params(stcb, m,
1942 initack_offset + sizeof(struct sctp_init_ack_chunk),
1943 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
1945 struct sctp_auth_chunk *auth;
1947 auth = (struct sctp_auth_chunk *)
1948 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
1949 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
1950 /* auth HMAC failed, dump the assoc and packet */
1951 SCTPDBG(SCTP_DEBUG_AUTH1,
1952 "COOKIE-ECHO: AUTH failed\n");
1953 atomic_add_int(&stcb->asoc.refcnt, 1);
1954 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1955 SCTP_TCB_UNLOCK(stcb);
1956 SCTP_SOCKET_LOCK(so, 1);
1957 SCTP_TCB_LOCK(stcb);
1959 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
1960 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1961 SCTP_SOCKET_UNLOCK(so, 1);
1963 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1966 /* remaining chunks checked... good to go */
1967 stcb->asoc.authenticated = 1;
1970 /* update current state */
1971 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
1972 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1973 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1974 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1975 stcb->sctp_ep, stcb, asoc->primary_destination);
1977 sctp_stop_all_cookie_timers(stcb);
1978 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
1979 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1982 * if we're doing ASCONFs, check to see if we have any new local
1983 * addresses that need to get added to the peer (eg. addresses
1984 * changed while cookie echo in flight). This needs to be done
1985 * after we go to the OPEN state to do the correct asconf
1986 * processing. else, make sure we have the correct addresses in our
1990 /* warning, we re-use sin, sin6, sa_store here! */
1991 /* pull in local_address (our "from" address) */
1992 if (cookie->laddr_type == SCTP_IPV4_ADDRESS) {
1993 /* source addr is IPv4 */
1994 sin = (struct sockaddr_in *)initack_src;
1995 memset(sin, 0, sizeof(*sin));
1996 sin->sin_family = AF_INET;
1997 sin->sin_len = sizeof(struct sockaddr_in);
1998 sin->sin_addr.s_addr = cookie->laddress[0];
1999 } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) {
2000 /* source addr is IPv6 */
2001 sin6 = (struct sockaddr_in6 *)initack_src;
2002 memset(sin6, 0, sizeof(*sin6));
2003 sin6->sin6_family = AF_INET6;
2004 sin6->sin6_len = sizeof(struct sockaddr_in6);
2005 sin6->sin6_scope_id = cookie->scope_id;
2006 memcpy(&sin6->sin6_addr, cookie->laddress,
2007 sizeof(sin6->sin6_addr));
2009 atomic_add_int(&stcb->asoc.refcnt, 1);
2010 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2011 SCTP_TCB_UNLOCK(stcb);
2012 SCTP_SOCKET_LOCK(so, 1);
2013 SCTP_TCB_LOCK(stcb);
2015 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2016 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2017 SCTP_SOCKET_UNLOCK(so, 1);
2019 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2023 /* set up to notify upper layer */
2024 *notification = SCTP_NOTIFY_ASSOC_UP;
2025 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2026 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2027 (inp->sctp_socket->so_qlimit == 0)) {
2029 * This is an endpoint that called connect() how it got a
2030 * cookie that is NEW is a bit of a mystery. It must be that
2031 * the INIT was sent, but before it got there.. a complete
2032 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2033 * should have went to the other code.. not here.. oh well..
2034 * a bit of protection is worth having..
2036 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2037 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2038 atomic_add_int(&stcb->asoc.refcnt, 1);
2039 SCTP_TCB_UNLOCK(stcb);
2040 SCTP_SOCKET_LOCK(so, 1);
2041 SCTP_TCB_LOCK(stcb);
2042 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2043 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2044 SCTP_SOCKET_UNLOCK(so, 1);
2048 soisconnected(stcb->sctp_socket);
2049 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2050 SCTP_SOCKET_UNLOCK(so, 1);
2052 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2053 (inp->sctp_socket->so_qlimit)) {
2055 * We don't want to do anything with this one. Since it is
2056 * the listening guy. The timer will get started for
2057 * accepted connections in the caller.
2061 /* since we did not send a HB make sure we don't double things */
2062 if ((netp) && (*netp))
2063 (*netp)->hb_responded = 1;
2065 if (stcb->asoc.sctp_autoclose_ticks &&
2066 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2067 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2069 /* calculate the RTT */
2070 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2071 if ((netp) && (*netp)) {
2072 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2073 &cookie->time_entered, sctp_align_unsafe_makecopy);
2075 /* respond with a COOKIE-ACK */
2076 sctp_send_cookie_ack(stcb);
2079 * check the address lists for any ASCONFs that need to be sent
2080 * AFTER the cookie-ack is sent
2082 sctp_check_address_list(stcb, m,
2083 initack_offset + sizeof(struct sctp_init_ack_chunk),
2084 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2085 initack_src, cookie->local_scope, cookie->site_scope,
2086 cookie->ipv4_scope, cookie->loopback_scope);
2094 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2095 * existing (non-NULL) TCB
2097 static struct mbuf *
2098 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2099 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2100 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2101 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2102 struct sctp_tcb **locked_tcb, uint32_t vrf_id, uint16_t port)
2104 struct sctp_state_cookie *cookie;
2105 struct sockaddr_in6 sin6;
2106 struct sockaddr_in sin;
2107 struct sctp_tcb *l_stcb = *stcb;
2108 struct sctp_inpcb *l_inp;
2109 struct sockaddr *to;
2110 sctp_assoc_t sac_restart_id;
2111 struct sctp_pcb *ep;
2113 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2115 uint8_t cookie_ok = 0;
2116 unsigned int size_of_pkt, sig_offset, cookie_offset;
2117 unsigned int cookie_len;
2119 struct timeval time_expires;
2120 struct sockaddr_storage dest_store;
2121 struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
2123 int notification = 0;
2124 struct sctp_nets *netl;
2125 int had_a_existing_tcb = 0;
2127 SCTPDBG(SCTP_DEBUG_INPUT2,
2128 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2130 if (inp_p == NULL) {
2133 /* First get the destination address setup too. */
2134 iph = mtod(m, struct ip *);
2135 switch (iph->ip_v) {
2139 struct sockaddr_in *lsin;
2141 lsin = (struct sockaddr_in *)(localep_sa);
2142 memset(lsin, 0, sizeof(*lsin));
2143 lsin->sin_family = AF_INET;
2144 lsin->sin_len = sizeof(*lsin);
2145 lsin->sin_port = sh->dest_port;
2146 lsin->sin_addr.s_addr = iph->ip_dst.s_addr;
2147 size_of_pkt = SCTP_GET_IPV4_LENGTH(iph);
2151 case IPV6_VERSION >> 4:
2154 struct ip6_hdr *ip6;
2155 struct sockaddr_in6 *lsin6;
2157 lsin6 = (struct sockaddr_in6 *)(localep_sa);
2158 memset(lsin6, 0, sizeof(*lsin6));
2159 lsin6->sin6_family = AF_INET6;
2160 lsin6->sin6_len = sizeof(struct sockaddr_in6);
2161 ip6 = mtod(m, struct ip6_hdr *);
2162 lsin6->sin6_port = sh->dest_port;
2163 lsin6->sin6_addr = ip6->ip6_dst;
2164 size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen;
2172 cookie = &cp->cookie;
2173 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2174 cookie_len = ntohs(cp->ch.chunk_length);
2176 if ((cookie->peerport != sh->src_port) &&
2177 (cookie->myport != sh->dest_port) &&
2178 (cookie->my_vtag != sh->v_tag)) {
2180 * invalid ports or bad tag. Note that we always leave the
2181 * v_tag in the header in network order and when we stored
2182 * it in the my_vtag slot we also left it in network order.
2183 * This maintains the match even though it may be in the
2184 * opposite byte order of the machine :->
2188 if (cookie_len > size_of_pkt ||
2189 cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2190 sizeof(struct sctp_init_chunk) +
2191 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2192 /* cookie too long! or too small */
2196 * split off the signature into its own mbuf (since it should not be
2197 * calculated in the sctp_hmac_m() call).
2199 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2200 if (sig_offset > size_of_pkt) {
2201 /* packet not correct size! */
2202 /* XXX this may already be accounted for earlier... */
2205 m_sig = m_split(m, sig_offset, M_DONTWAIT);
2206 if (m_sig == NULL) {
2207 /* out of memory or ?? */
2210 #ifdef SCTP_MBUF_LOGGING
2211 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2216 if (SCTP_BUF_IS_EXTENDED(mat)) {
2217 sctp_log_mb(mat, SCTP_MBUF_SPLIT);
2219 mat = SCTP_BUF_NEXT(mat);
2225 * compute the signature/digest for the cookie
2227 ep = &(*inp_p)->sctp_ep;
2230 SCTP_TCB_UNLOCK(l_stcb);
2232 SCTP_INP_RLOCK(l_inp);
2234 SCTP_TCB_LOCK(l_stcb);
2236 /* which cookie is it? */
2237 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2238 (ep->current_secret_number != ep->last_secret_number)) {
2239 /* it's the old cookie */
2240 (void)sctp_hmac_m(SCTP_HMAC,
2241 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2242 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2244 /* it's the current cookie */
2245 (void)sctp_hmac_m(SCTP_HMAC,
2246 (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
2247 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2249 /* get the signature */
2250 SCTP_INP_RUNLOCK(l_inp);
2251 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2253 /* couldn't find signature */
2254 sctp_m_freem(m_sig);
2257 /* compare the received digest with the computed digest */
2258 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2259 /* try the old cookie? */
2260 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2261 (ep->current_secret_number != ep->last_secret_number)) {
2262 /* compute digest with old */
2263 (void)sctp_hmac_m(SCTP_HMAC,
2264 (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2265 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2267 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2275 * Now before we continue we must reconstruct our mbuf so that
2276 * normal processing of any other chunks will work.
2282 while (SCTP_BUF_NEXT(m_at) != NULL) {
2283 m_at = SCTP_BUF_NEXT(m_at);
2285 SCTP_BUF_NEXT(m_at) = m_sig;
2288 if (cookie_ok == 0) {
2289 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2290 SCTPDBG(SCTP_DEBUG_INPUT2,
2291 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2292 (uint32_t) offset, cookie_offset, sig_offset);
2296 * check the cookie timestamps to be sure it's not stale
2298 (void)SCTP_GETTIME_TIMEVAL(&now);
2299 /* Expire time is in Ticks, so we convert to seconds */
2300 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2301 time_expires.tv_usec = cookie->time_entered.tv_usec;
2303 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2306 if (timevalcmp(&now, &time_expires, >)) {
2307 /* cookie is stale! */
2308 struct mbuf *op_err;
2309 struct sctp_stale_cookie_msg *scm;
2312 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2313 0, M_DONTWAIT, 1, MT_DATA);
2314 if (op_err == NULL) {
2318 /* pre-reserve some space */
2320 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
2322 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
2324 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
2325 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
2328 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2329 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2330 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2331 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2332 (sizeof(uint32_t))));
2333 /* seconds to usec */
2334 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2337 tim = now.tv_usec - cookie->time_entered.tv_usec;
2338 scm->time_usec = htonl(tim);
2339 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
2344 * Now we must see with the lookup address if we have an existing
2345 * asoc. This will only happen if we were in the COOKIE-WAIT state
2346 * and a INIT collided with us and somewhere the peer sent the
2347 * cookie on another address besides the single address our assoc
2348 * had for him. In this case we will have one of the tie-tags set at
2349 * least AND the address field in the cookie can be used to look it
2353 if (cookie->addr_type == SCTP_IPV6_ADDRESS) {
2354 memset(&sin6, 0, sizeof(sin6));
2355 sin6.sin6_family = AF_INET6;
2356 sin6.sin6_len = sizeof(sin6);
2357 sin6.sin6_port = sh->src_port;
2358 sin6.sin6_scope_id = cookie->scope_id;
2359 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2360 sizeof(sin6.sin6_addr.s6_addr));
2361 to = (struct sockaddr *)&sin6;
2362 } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) {
2363 memset(&sin, 0, sizeof(sin));
2364 sin.sin_family = AF_INET;
2365 sin.sin_len = sizeof(sin);
2366 sin.sin_port = sh->src_port;
2367 sin.sin_addr.s_addr = cookie->address[0];
2368 to = (struct sockaddr *)&sin;
2370 /* This should not happen */
2373 if ((*stcb == NULL) && to) {
2374 /* Yep, lets check */
2375 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
2376 if (*stcb == NULL) {
2378 * We should have only got back the same inp. If we
2379 * got back a different ep we have a problem. The
2380 * original findep got back l_inp and now
2382 if (l_inp != *inp_p) {
2383 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2386 if (*locked_tcb == NULL) {
2388 * In this case we found the assoc only
2389 * after we locked the create lock. This
2390 * means we are in a colliding case and we
2391 * must make sure that we unlock the tcb if
2392 * its one of the cases where we throw away
2393 * the incoming packets.
2395 *locked_tcb = *stcb;
2398 * We must also increment the inp ref count
2399 * since the ref_count flags was set when we
2400 * did not find the TCB, now we found it
2401 * which reduces the refcount.. we must
2402 * raise it back out to balance it all :-)
2404 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2405 if ((*stcb)->sctp_ep != l_inp) {
2406 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2407 (*stcb)->sctp_ep, l_inp);
2415 cookie_len -= SCTP_SIGNATURE_SIZE;
2416 if (*stcb == NULL) {
2417 /* this is the "normal" case... get a new TCB */
2418 *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
2419 cookie_len, *inp_p, netp, to, ¬ification,
2420 auth_skipped, auth_offset, auth_len, vrf_id, port);
2422 /* this is abnormal... cookie-echo on existing TCB */
2423 had_a_existing_tcb = 1;
2424 *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
2425 cookie, cookie_len, *inp_p, *stcb, *netp, to,
2426 ¬ification, &sac_restart_id, vrf_id);
2429 if (*stcb == NULL) {
2430 /* still no TCB... must be bad cookie-echo */
2434 * Ok, we built an association so confirm the address we sent the
2437 netl = sctp_findnet(*stcb, to);
2439 * This code should in theory NOT run but
2442 /* TSNH! Huh, why do I need to add this address here? */
2445 ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE,
2446 SCTP_IN_COOKIE_PROC);
2447 netl = sctp_findnet(*stcb, to);
2450 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2451 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2452 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2454 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2455 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2459 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p,
2462 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2463 if (!had_a_existing_tcb ||
2464 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2466 * If we have a NEW cookie or the connect never
2467 * reached the connected state during collision we
2468 * must do the TCP accept thing.
2470 struct socket *so, *oso;
2471 struct sctp_inpcb *inp;
2473 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2475 * For a restart we will keep the same
2476 * socket, no need to do anything. I THINK!!
2478 sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id, SCTP_SO_NOT_LOCKED);
2481 oso = (*inp_p)->sctp_socket;
2482 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2483 SCTP_TCB_UNLOCK((*stcb));
2484 so = sonewconn(oso, 0
2486 SCTP_TCB_LOCK((*stcb));
2487 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2490 struct mbuf *op_err;
2492 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2493 struct socket *pcb_so;
2496 /* Too many sockets */
2497 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2498 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2499 sctp_abort_association(*inp_p, NULL, m, iphlen,
2500 sh, op_err, vrf_id, port);
2501 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2502 pcb_so = SCTP_INP_SO(*inp_p);
2503 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2504 SCTP_TCB_UNLOCK((*stcb));
2505 SCTP_SOCKET_LOCK(pcb_so, 1);
2506 SCTP_TCB_LOCK((*stcb));
2507 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2509 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2510 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2511 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2515 inp = (struct sctp_inpcb *)so->so_pcb;
2516 SCTP_INP_INCR_REF(inp);
2518 * We add the unbound flag here so that if we get an
2519 * soabort() before we get the move_pcb done, we
2520 * will properly cleanup.
2522 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2523 SCTP_PCB_FLAGS_CONNECTED |
2524 SCTP_PCB_FLAGS_IN_TCPPOOL |
2525 SCTP_PCB_FLAGS_UNBOUND |
2526 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2527 SCTP_PCB_FLAGS_DONT_WAKE);
2528 inp->sctp_features = (*inp_p)->sctp_features;
2529 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2530 inp->sctp_socket = so;
2531 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2532 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2533 inp->sctp_context = (*inp_p)->sctp_context;
2534 inp->inp_starting_point_for_iterator = NULL;
2536 * copy in the authentication parameters from the
2539 if (inp->sctp_ep.local_hmacs)
2540 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2541 inp->sctp_ep.local_hmacs =
2542 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2543 if (inp->sctp_ep.local_auth_chunks)
2544 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2545 inp->sctp_ep.local_auth_chunks =
2546 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2547 (void)sctp_copy_skeylist(&(*inp_p)->sctp_ep.shared_keys,
2548 &inp->sctp_ep.shared_keys);
2551 * Now we must move it from one hash table to
2552 * another and get the tcb in the right place.
2554 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2556 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2557 SCTP_TCB_UNLOCK((*stcb));
2559 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2561 SCTP_TCB_LOCK((*stcb));
2562 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2566 * now we must check to see if we were aborted while
2567 * the move was going on and the lock/unlock
2570 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2572 * yep it was, we leave the assoc attached
2573 * to the socket since the sctp_inpcb_free()
2574 * call will send an abort for us.
2576 SCTP_INP_DECR_REF(inp);
2579 SCTP_INP_DECR_REF(inp);
2580 /* Switch over to the new guy */
2582 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2585 * Pull it from the incomplete queue and wake the
2588 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2589 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2590 SCTP_TCB_UNLOCK((*stcb));
2591 SCTP_SOCKET_LOCK(so, 1);
2594 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2595 SCTP_TCB_LOCK((*stcb));
2596 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2597 SCTP_SOCKET_UNLOCK(so, 1);
2602 if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2603 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2609 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp,
2610 struct sctp_tcb *stcb, struct sctp_nets *net)
2612 /* cp must not be used, others call this without a c-ack :-) */
2613 struct sctp_association *asoc;
2615 SCTPDBG(SCTP_DEBUG_INPUT2,
2616 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2622 sctp_stop_all_cookie_timers(stcb);
2623 /* process according to association state */
2624 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2625 /* state change only needed when I am in right state */
2626 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2627 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2628 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2629 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2630 stcb->sctp_ep, stcb, asoc->primary_destination);
2634 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2635 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2636 if (asoc->overall_error_count == 0) {
2637 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2638 &asoc->time_entered, sctp_align_safe_nocopy);
2640 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2641 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2642 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2643 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2644 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2648 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2649 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2650 so = SCTP_INP_SO(stcb->sctp_ep);
2651 atomic_add_int(&stcb->asoc.refcnt, 1);
2652 SCTP_TCB_UNLOCK(stcb);
2653 SCTP_SOCKET_LOCK(so, 1);
2654 SCTP_TCB_LOCK(stcb);
2655 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2656 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2657 SCTP_SOCKET_UNLOCK(so, 1);
2661 soisconnected(stcb->sctp_socket);
2662 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2663 SCTP_SOCKET_UNLOCK(so, 1);
2666 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2669 * since we did not send a HB make sure we don't double
2672 net->hb_responded = 1;
2674 if (stcb->asoc.sctp_autoclose_ticks &&
2675 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2676 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2677 stcb->sctp_ep, stcb, NULL);
2680 * send ASCONF if parameters are pending and ASCONFs are
2681 * allowed (eg. addresses changed when init/cookie echo were
2684 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2685 (stcb->asoc.peer_supports_asconf) &&
2686 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2687 #ifdef SCTP_TIMER_BASED_ASCONF
2688 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2689 stcb->sctp_ep, stcb,
2690 stcb->asoc.primary_destination);
2692 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
2693 SCTP_ADDR_NOT_LOCKED);
2697 /* Toss the cookie if I can */
2698 sctp_toss_old_cookies(stcb, asoc);
2699 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2700 /* Restart the timer if we have pending data */
2701 struct sctp_tmit_chunk *chk;
2703 chk = TAILQ_FIRST(&asoc->sent_queue);
2705 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2712 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2713 struct sctp_tcb *stcb)
2715 struct sctp_nets *net;
2716 struct sctp_tmit_chunk *lchk;
2719 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) {
2722 SCTP_STAT_INCR(sctps_recvecne);
2723 tsn = ntohl(cp->tsn);
2724 /* ECN Nonce stuff: need a resync and disable the nonce sum check */
2725 /* Also we make sure we disable the nonce_wait */
2726 lchk = TAILQ_FIRST(&stcb->asoc.send_queue);
2728 stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
2730 stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq;
2732 stcb->asoc.nonce_wait_for_ecne = 0;
2733 stcb->asoc.nonce_sum_check = 0;
2735 /* Find where it was sent, if possible */
2737 lchk = TAILQ_FIRST(&stcb->asoc.sent_queue);
2739 if (lchk->rec.data.TSN_seq == tsn) {
2743 if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ))
2745 lchk = TAILQ_NEXT(lchk, sctp_next);
2748 /* default is we use the primary */
2749 net = stcb->asoc.primary_destination;
2751 if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) {
2753 * JRS - Use the congestion control given in the pluggable
2756 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net);
2758 * we reduce once every RTT. So we will only lower cwnd at
2759 * the next sending seq i.e. the resync_tsn.
2761 stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn;
2764 * We always send a CWR this way if our previous one was lost our
2765 * peer will get an update, or if it is not time again to reduce we
2766 * still get the cwr to the peer.
2768 sctp_send_cwr(stcb, net, tsn);
2772 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb)
2775 * Here we get a CWR from the peer. We must look in the outqueue and
2776 * make sure that we have a covered ECNE in teh control chunk part.
2779 struct sctp_tmit_chunk *chk;
2780 struct sctp_ecne_chunk *ecne;
2782 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
2783 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
2787 * Look for and remove if it is the right TSN. Since there
2788 * is only ONE ECNE on the control queue at any one time we
2789 * don't need to worry about more than one!
2791 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
2792 if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn),
2793 MAX_TSN) || (cp->tsn == ecne->tsn)) {
2794 /* this covers this ECNE, we can remove it */
2795 stcb->asoc.ecn_echo_cnt_onq--;
2796 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
2799 sctp_m_freem(chk->data);
2802 stcb->asoc.ctrl_queue_cnt--;
2803 sctp_free_a_chunk(stcb, chk);
2810 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp,
2811 struct sctp_tcb *stcb, struct sctp_nets *net)
2813 struct sctp_association *asoc;
2815 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2820 SCTPDBG(SCTP_DEBUG_INPUT2,
2821 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
2826 /* process according to association state */
2827 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
2828 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
2829 SCTPDBG(SCTP_DEBUG_INPUT2,
2830 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
2831 SCTP_TCB_UNLOCK(stcb);
2834 /* notify upper layer protocol */
2835 if (stcb->sctp_socket) {
2836 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2837 /* are the queues empty? they should be */
2838 if (!TAILQ_EMPTY(&asoc->send_queue) ||
2839 !TAILQ_EMPTY(&asoc->sent_queue) ||
2840 !TAILQ_EMPTY(&asoc->out_wheel)) {
2841 sctp_report_all_outbound(stcb, 0, SCTP_SO_NOT_LOCKED);
2844 /* stop the timer */
2845 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2846 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
2848 SCTPDBG(SCTP_DEBUG_INPUT2,
2849 "sctp_handle_shutdown_complete: calls free-asoc\n");
2850 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2851 so = SCTP_INP_SO(stcb->sctp_ep);
2852 atomic_add_int(&stcb->asoc.refcnt, 1);
2853 SCTP_TCB_UNLOCK(stcb);
2854 SCTP_SOCKET_LOCK(so, 1);
2855 SCTP_TCB_LOCK(stcb);
2856 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2858 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2859 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2860 SCTP_SOCKET_UNLOCK(so, 1);
2866 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
2867 struct sctp_nets *net, uint8_t flg)
2869 switch (desc->chunk_type) {
2871 /* find the tsn to resend (possibly */
2874 struct sctp_tmit_chunk *tp1;
2876 tsn = ntohl(desc->tsn_ifany);
2877 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2879 if (tp1->rec.data.TSN_seq == tsn) {
2883 if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn,
2889 tp1 = TAILQ_NEXT(tp1, sctp_next);
2893 * Do it the other way , aka without paying
2894 * attention to queue seq order.
2896 SCTP_STAT_INCR(sctps_pdrpdnfnd);
2897 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2899 if (tp1->rec.data.TSN_seq == tsn) {
2903 tp1 = TAILQ_NEXT(tp1, sctp_next);
2907 SCTP_STAT_INCR(sctps_pdrptsnnf);
2909 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
2912 if ((stcb->asoc.peers_rwnd == 0) &&
2913 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
2914 SCTP_STAT_INCR(sctps_pdrpdiwnp);
2917 if (stcb->asoc.peers_rwnd == 0 &&
2918 (flg & SCTP_FROM_MIDDLE_BOX)) {
2919 SCTP_STAT_INCR(sctps_pdrpdizrw);
2922 ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
2923 sizeof(struct sctp_data_chunk));
2927 for (iii = 0; iii < sizeof(desc->data_bytes);
2929 if (ddp[iii] != desc->data_bytes[iii]) {
2930 SCTP_STAT_INCR(sctps_pdrpbadd);
2936 * We zero out the nonce so resync not
2939 tp1->rec.data.ect_nonce = 0;
2943 * this guy had a RTO calculation
2944 * pending on it, cancel it
2948 SCTP_STAT_INCR(sctps_pdrpmark);
2949 if (tp1->sent != SCTP_DATAGRAM_RESEND)
2950 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
2951 tp1->sent = SCTP_DATAGRAM_RESEND;
2953 * mark it as if we were doing a FR, since
2954 * we will be getting gap ack reports behind
2955 * the info from the router.
2957 tp1->rec.data.doing_fast_retransmit = 1;
2959 * mark the tsn with what sequences can
2962 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
2963 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
2965 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
2968 /* restart the timer */
2969 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2970 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
2971 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2974 /* fix counts and things */
2975 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
2976 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
2977 tp1->whoTo->flight_size,
2980 tp1->rec.data.TSN_seq);
2982 sctp_flight_size_decrease(tp1);
2983 sctp_total_flight_decrease(stcb, tp1);
2989 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
2990 if (tp1->sent == SCTP_DATAGRAM_RESEND)
2993 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
2995 if (tp1->sent == SCTP_DATAGRAM_RESEND)
2998 if (audit != stcb->asoc.sent_queue_retran_cnt) {
2999 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3000 audit, stcb->asoc.sent_queue_retran_cnt);
3001 #ifndef SCTP_AUDITING_ENABLED
3002 stcb->asoc.sent_queue_retran_cnt = audit;
3010 struct sctp_tmit_chunk *asconf;
3012 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3014 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3019 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3020 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3021 asconf->sent = SCTP_DATAGRAM_RESEND;
3022 asconf->snd_count--;
3026 case SCTP_INITIATION:
3027 /* resend the INIT */
3028 stcb->asoc.dropped_special_cnt++;
3029 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3031 * If we can get it in, in a few attempts we do
3032 * this, otherwise we let the timer fire.
3034 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3035 stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3036 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3039 case SCTP_SELECTIVE_ACK:
3040 /* resend the sack */
3041 sctp_send_sack(stcb);
3043 case SCTP_HEARTBEAT_REQUEST:
3044 /* resend a demand HB */
3045 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3047 * Only retransmit if we KNOW we wont destroy the
3050 (void)sctp_send_hb(stcb, 1, net);
3054 sctp_send_shutdown(stcb, net);
3056 case SCTP_SHUTDOWN_ACK:
3057 sctp_send_shutdown_ack(stcb, net);
3059 case SCTP_COOKIE_ECHO:
3061 struct sctp_tmit_chunk *cookie;
3064 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3066 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3071 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3072 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3073 cookie->sent = SCTP_DATAGRAM_RESEND;
3074 sctp_stop_all_cookie_timers(stcb);
3078 case SCTP_COOKIE_ACK:
3079 sctp_send_cookie_ack(stcb);
3081 case SCTP_ASCONF_ACK:
3082 /* resend last asconf ack */
3083 sctp_send_asconf_ack(stcb);
3085 case SCTP_FORWARD_CUM_TSN:
3086 send_forward_tsn(stcb, &stcb->asoc);
3088 /* can't do anything with these */
3089 case SCTP_PACKET_DROPPED:
3090 case SCTP_INITIATION_ACK: /* this should not happen */
3091 case SCTP_HEARTBEAT_ACK:
3092 case SCTP_ABORT_ASSOCIATION:
3093 case SCTP_OPERATION_ERROR:
3094 case SCTP_SHUTDOWN_COMPLETE:
3104 sctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3110 * We set things to 0xffff since this is the last delivered sequence
3111 * and we will be sending in 0 after the reset.
3114 if (number_entries) {
3115 for (i = 0; i < number_entries; i++) {
3116 temp = ntohs(list[i]);
3117 if (temp >= stcb->asoc.streamincnt) {
3120 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
3124 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3125 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3128 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3132 sctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
3136 if (number_entries == 0) {
3137 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3138 stcb->asoc.strmout[i].next_sequence_sent = 0;
3140 } else if (number_entries) {
3141 for (i = 0; i < number_entries; i++) {
3144 temp = ntohs(list[i]);
3145 if (temp >= stcb->asoc.streamoutcnt) {
3146 /* no such stream */
3149 stcb->asoc.strmout[temp].next_sequence_sent = 0;
3152 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3156 struct sctp_stream_reset_out_request *
3157 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3159 struct sctp_association *asoc;
3160 struct sctp_stream_reset_out_req *req;
3161 struct sctp_stream_reset_out_request *r;
3162 struct sctp_tmit_chunk *chk;
3166 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3167 asoc->stream_reset_outstanding = 0;
3170 if (stcb->asoc.str_reset == NULL) {
3171 asoc->stream_reset_outstanding = 0;
3174 chk = stcb->asoc.str_reset;
3175 if (chk->data == NULL) {
3179 /* he wants a copy of the chk pointer */
3182 clen = chk->send_size;
3183 req = mtod(chk->data, struct sctp_stream_reset_out_req *);
3185 if (ntohl(r->request_seq) == seq) {
3189 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3190 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3191 /* move to the next one, there can only be a max of two */
3192 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
3193 if (ntohl(r->request_seq) == seq) {
3197 /* that seq is not here */
3202 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3204 struct sctp_association *asoc;
3205 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3207 if (stcb->asoc.str_reset == NULL) {
3212 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3213 TAILQ_REMOVE(&asoc->control_send_queue,
3217 sctp_m_freem(chk->data);
3220 asoc->ctrl_queue_cnt--;
3221 sctp_free_a_chunk(stcb, chk);
3222 /* sa_ignore NO_NULL_CHK */
3223 stcb->asoc.str_reset = NULL;
3228 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3229 uint32_t seq, uint32_t action,
3230 struct sctp_stream_reset_response *respin)
3234 struct sctp_association *asoc = &stcb->asoc;
3235 struct sctp_tmit_chunk *chk;
3236 struct sctp_stream_reset_out_request *srparam;
3239 if (asoc->stream_reset_outstanding == 0) {
3243 if (seq == stcb->asoc.str_reset_seq_out) {
3244 srparam = sctp_find_stream_reset(stcb, seq, &chk);
3246 stcb->asoc.str_reset_seq_out++;
3247 type = ntohs(srparam->ph.param_type);
3248 lparm_len = ntohs(srparam->ph.param_length);
3249 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3250 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3251 asoc->stream_reset_out_is_outstanding = 0;
3252 if (asoc->stream_reset_outstanding)
3253 asoc->stream_reset_outstanding--;
3254 if (action == SCTP_STREAM_RESET_PERFORMED) {
3256 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
3258 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3260 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3261 /* Answered my request */
3262 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3263 if (asoc->stream_reset_outstanding)
3264 asoc->stream_reset_outstanding--;
3265 if (action != SCTP_STREAM_RESET_PERFORMED) {
3266 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3268 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3270 * a) Adopt the new in tsn.
3272 * c) Adopt the new out-tsn
3274 struct sctp_stream_reset_response_tsn *resp;
3275 struct sctp_forward_tsn_chunk fwdtsn;
3278 if (respin == NULL) {
3282 if (action == SCTP_STREAM_RESET_PERFORMED) {
3283 resp = (struct sctp_stream_reset_response_tsn *)respin;
3284 asoc->stream_reset_outstanding--;
3285 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3286 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3287 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3288 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3292 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3293 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3294 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3296 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3297 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3298 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3299 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3300 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3302 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3303 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3307 /* get rid of the request and get the request flags */
3308 if (asoc->stream_reset_outstanding == 0) {
3309 sctp_clean_up_stream_reset(stcb);
3317 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3318 struct sctp_tmit_chunk *chk,
3319 struct sctp_stream_reset_in_request *req, int trunc)
3327 * peer wants me to send a str-reset to him for my outgoing seq's if
3330 struct sctp_association *asoc = &stcb->asoc;
3332 seq = ntohl(req->request_seq);
3333 if (asoc->str_reset_seq_in == seq) {
3335 /* Can't do it, since they exceeded our buffer size */
3336 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3337 asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3338 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3339 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3340 len = ntohs(req->ph.param_length);
3341 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3342 for (i = 0; i < number_entries; i++) {
3343 temp = ntohs(req->list_of_streams[i]);
3344 req->list_of_streams[i] = temp;
3346 /* move the reset action back one */
3347 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3348 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3349 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
3350 asoc->str_reset_seq_out,
3351 seq, (asoc->sending_seq - 1));
3352 asoc->stream_reset_out_is_outstanding = 1;
3353 asoc->str_reset = chk;
3354 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
3355 stcb->asoc.stream_reset_outstanding++;
3357 /* Can't do it, since we have sent one out */
3358 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3359 asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER;
3360 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3362 asoc->str_reset_seq_in++;
3363 } else if (asoc->str_reset_seq_in - 1 == seq) {
3364 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3365 } else if (asoc->str_reset_seq_in - 2 == seq) {
3366 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3368 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3373 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3374 struct sctp_tmit_chunk *chk,
3375 struct sctp_stream_reset_tsn_request *req)
3377 /* reset all in and out and update the tsn */
3379 * A) reset my str-seq's on in and out. B) Select a receive next,
3380 * and set cum-ack to it. Also process this selected number as a
3381 * fwd-tsn as well. C) set in the response my next sending seq.
3383 struct sctp_forward_tsn_chunk fwdtsn;
3384 struct sctp_association *asoc = &stcb->asoc;
3388 seq = ntohl(req->request_seq);
3389 if (asoc->str_reset_seq_in == seq) {
3390 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3391 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3392 fwdtsn.ch.chunk_flags = 0;
3393 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3394 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3398 stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3399 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3400 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3402 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3403 stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1;
3404 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3405 atomic_add_int(&stcb->asoc.sending_seq, 1);
3406 /* save off historical data for retrans */
3407 stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0];
3408 stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq;
3409 stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0];
3410 stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn;
3412 sctp_add_stream_reset_result_tsn(chk,
3413 ntohl(req->request_seq),
3414 SCTP_STREAM_RESET_PERFORMED,
3415 stcb->asoc.sending_seq,
3416 stcb->asoc.mapping_array_base_tsn);
3417 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3418 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3419 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3420 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3422 asoc->str_reset_seq_in++;
3423 } else if (asoc->str_reset_seq_in - 1 == seq) {
3424 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3425 stcb->asoc.last_sending_seq[0],
3426 stcb->asoc.last_base_tsnsent[0]
3428 } else if (asoc->str_reset_seq_in - 2 == seq) {
3429 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3430 stcb->asoc.last_sending_seq[1],
3431 stcb->asoc.last_base_tsnsent[1]
3434 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3440 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3441 struct sctp_tmit_chunk *chk,
3442 struct sctp_stream_reset_out_request *req, int trunc)
3445 int number_entries, len;
3446 struct sctp_association *asoc = &stcb->asoc;
3448 seq = ntohl(req->request_seq);
3450 /* now if its not a duplicate we process it */
3451 if (asoc->str_reset_seq_in == seq) {
3452 len = ntohs(req->ph.param_length);
3453 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3455 * the sender is resetting, handle the list issue.. we must
3456 * a) verify if we can do the reset, if so no problem b) If
3457 * we can't do the reset we must copy the request. c) queue
3458 * it, and setup the data in processor to trigger it off
3459 * when needed and dequeue all the queued data.
3461 tsn = ntohl(req->send_reset_at_tsn);
3463 /* move the reset action back one */
3464 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3466 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3467 asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3468 } else if ((tsn == asoc->cumulative_tsn) ||
3469 (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) {
3470 /* we can do it now */
3471 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3472 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3473 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3476 * we must queue it up and thus wait for the TSN's
3477 * to arrive that are at or before tsn
3479 struct sctp_stream_reset_list *liste;
3482 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3483 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3484 siz, SCTP_M_STRESET);
3485 if (liste == NULL) {
3486 /* gak out of memory */
3487 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3488 asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3492 liste->number_entries = number_entries;
3493 memcpy(&liste->req, req,
3494 (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t))));
3495 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3496 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3497 asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3499 asoc->str_reset_seq_in++;
3500 } else if ((asoc->str_reset_seq_in - 1) == seq) {
3502 * one seq back, just echo back last action since my
3503 * response was lost.
3505 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3506 } else if ((asoc->str_reset_seq_in - 2) == seq) {
3508 * two seq back, just echo back last action since my
3509 * response was lost.
3511 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3513 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3518 __attribute__((noinline))
3521 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3522 struct sctp_stream_reset_out_req *sr_req)
3524 int chk_length, param_len, ptype;
3525 struct sctp_paramhdr pstore;
3526 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
3531 struct sctp_tmit_chunk *chk;
3532 struct sctp_chunkhdr *ch;
3533 struct sctp_paramhdr *ph;
3537 /* now it may be a reset or a reset-response */
3538 chk_length = ntohs(sr_req->ch.chunk_length);
3540 /* setup for adding the response */
3541 sctp_alloc_a_chunk(stcb, chk);
3545 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
3546 chk->rec.chunk_id.can_take_data = 0;
3547 chk->asoc = &stcb->asoc;
3548 chk->no_fr_allowed = 0;
3549 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
3550 chk->book_size_scale = 0;
3551 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
3552 if (chk->data == NULL) {
3555 sctp_m_freem(chk->data);
3558 sctp_free_a_chunk(stcb, chk);
3561 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
3563 /* setup chunk parameters */
3564 chk->sent = SCTP_DATAGRAM_UNSENT;
3566 chk->whoTo = stcb->asoc.primary_destination;
3567 atomic_add_int(&chk->whoTo->ref_count, 1);
3569 ch = mtod(chk->data, struct sctp_chunkhdr *);
3570 ch->chunk_type = SCTP_STREAM_RESET;
3571 ch->chunk_flags = 0;
3572 ch->chunk_length = htons(chk->send_size);
3573 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
3574 offset += sizeof(struct sctp_chunkhdr);
3575 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
3576 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
3579 param_len = ntohs(ph->param_length);
3580 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
3584 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
3585 (uint8_t *) & cstore);
3586 ptype = ntohs(ph->param_type);
3588 if (param_len > (int)sizeof(cstore)) {
3594 if (num_param > SCTP_MAX_RESET_PARAMS) {
3595 /* hit the max of parameters already sorry.. */
3598 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
3599 struct sctp_stream_reset_out_request *req_out;
3601 req_out = (struct sctp_stream_reset_out_request *)ph;
3603 if (stcb->asoc.stream_reset_outstanding) {
3604 seq = ntohl(req_out->response_seq);
3605 if (seq == stcb->asoc.str_reset_seq_out) {
3607 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL);
3610 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
3611 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
3612 struct sctp_stream_reset_in_request *req_in;
3616 req_in = (struct sctp_stream_reset_in_request *)ph;
3618 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
3619 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
3620 struct sctp_stream_reset_tsn_request *req_tsn;
3623 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
3625 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
3627 goto strres_nochunk;
3631 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
3632 struct sctp_stream_reset_response *resp;
3635 resp = (struct sctp_stream_reset_response *)ph;
3636 seq = ntohl(resp->response_seq);
3637 result = ntohl(resp->result);
3638 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
3640 goto strres_nochunk;
3645 offset += SCTP_SIZE32(param_len);
3646 chk_length -= SCTP_SIZE32(param_len);
3649 /* we have no response free the stuff */
3650 goto strres_nochunk;
3652 /* ok we have a chunk to link in */
3653 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
3656 stcb->asoc.ctrl_queue_cnt++;
3661 * Handle a router or endpoints report of a packet loss, there are two ways
3662 * to handle this, either we get the whole packet and must disect it
3663 * ourselves (possibly with truncation and or corruption) or it is a summary
3664 * from a middle box that did the disectting for us.
3667 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
3668 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
3670 uint32_t bottle_bw, on_queue;
3674 struct sctp_chunk_desc desc;
3675 struct sctp_chunkhdr *ch;
3677 chlen = ntohs(cp->ch.chunk_length);
3678 chlen -= sizeof(struct sctp_pktdrop_chunk);
3679 /* XXX possible chlen underflow */
3682 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
3683 SCTP_STAT_INCR(sctps_pdrpbwrpt);
3685 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
3686 chlen -= sizeof(struct sctphdr);
3687 /* XXX possible chlen underflow */
3688 memset(&desc, 0, sizeof(desc));
3690 trunc_len = (uint16_t) ntohs(cp->trunc_len);
3691 if (trunc_len > limit) {
3694 /* now the chunks themselves */
3695 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
3696 desc.chunk_type = ch->chunk_type;
3697 /* get amount we need to move */
3698 at = ntohs(ch->chunk_length);
3699 if (at < sizeof(struct sctp_chunkhdr)) {
3700 /* corrupt chunk, maybe at the end? */
3701 SCTP_STAT_INCR(sctps_pdrpcrupt);
3704 if (trunc_len == 0) {
3705 /* we are supposed to have all of it */
3707 /* corrupt skip it */
3708 SCTP_STAT_INCR(sctps_pdrpcrupt);
3712 /* is there enough of it left ? */
3713 if (desc.chunk_type == SCTP_DATA) {
3714 if (chlen < (sizeof(struct sctp_data_chunk) +
3715 sizeof(desc.data_bytes))) {
3719 if (chlen < sizeof(struct sctp_chunkhdr)) {
3724 if (desc.chunk_type == SCTP_DATA) {
3725 /* can we get out the tsn? */
3726 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
3727 SCTP_STAT_INCR(sctps_pdrpmbda);
3729 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
3731 struct sctp_data_chunk *dcp;
3735 dcp = (struct sctp_data_chunk *)ch;
3736 ddp = (uint8_t *) (dcp + 1);
3737 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
3738 desc.data_bytes[iii] = ddp[iii];
3740 desc.tsn_ifany = dcp->dp.tsn;
3742 /* nope we are done. */
3743 SCTP_STAT_INCR(sctps_pdrpnedat);
3747 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
3748 SCTP_STAT_INCR(sctps_pdrpmbct);
3751 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
3752 SCTP_STAT_INCR(sctps_pdrppdbrk);
3755 if (SCTP_SIZE32(at) > chlen) {
3758 chlen -= SCTP_SIZE32(at);
3759 if (chlen < sizeof(struct sctp_chunkhdr)) {
3760 /* done, none left */
3763 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
3765 /* Now update any rwnd --- possibly */
3766 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
3767 /* From a peer, we get a rwnd report */
3770 SCTP_STAT_INCR(sctps_pdrpfehos);
3772 bottle_bw = ntohl(cp->bottle_bw);
3773 on_queue = ntohl(cp->current_onq);
3774 if (bottle_bw && on_queue) {
3775 /* a rwnd report is in here */
3776 if (bottle_bw > on_queue)
3777 a_rwnd = bottle_bw - on_queue;
3782 stcb->asoc.peers_rwnd = 0;
3784 if (a_rwnd > stcb->asoc.total_flight) {
3785 stcb->asoc.peers_rwnd =
3786 a_rwnd - stcb->asoc.total_flight;
3788 stcb->asoc.peers_rwnd = 0;
3790 if (stcb->asoc.peers_rwnd <
3791 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3792 /* SWS sender side engages */
3793 stcb->asoc.peers_rwnd = 0;
3798 SCTP_STAT_INCR(sctps_pdrpfmbox);
3801 /* now middle boxes in sat networks get a cwnd bump */
3802 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
3803 (stcb->asoc.sat_t3_loss_recovery == 0) &&
3804 (stcb->asoc.sat_network)) {
3806 * This is debateable but for sat networks it makes sense
3807 * Note if a T3 timer has went off, we will prohibit any
3808 * changes to cwnd until we exit the t3 loss recovery.
3810 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
3811 net, cp, &bottle_bw, &on_queue);
3816 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
3817 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
3818 * offset: offset into the mbuf chain to first chunkhdr - length: is the
3819 * length of the complete packet outputs: - length: modified to remaining
3820 * length after control processing - netp: modified to new sctp_nets after
3821 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
3822 * bad packet,...) otherwise return the tcb for this packet
3825 __attribute__((noinline))
3827 static struct sctp_tcb *
3828 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
3829 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
3830 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
3831 uint32_t vrf_id, uint16_t port)
3833 struct sctp_association *asoc;
3835 int num_chunks = 0; /* number of control chunks processed */
3836 uint32_t chk_length;
3838 int abort_no_unlock = 0;
3841 * How big should this be, and should it be alloc'd? Lets try the
3842 * d-mtu-ceiling for now (2k) and that should hopefully work ...
3843 * until we get into jumbo grams and such..
3845 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
3846 struct sctp_tcb *locked_tcb = stcb;
3848 uint32_t auth_offset = 0, auth_len = 0;
3849 int auth_skipped = 0;
3852 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3857 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
3858 iphlen, *offset, length, stcb);
3860 /* validate chunk header length... */
3861 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
3862 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
3863 ntohs(ch->chunk_length));
3865 SCTP_TCB_UNLOCK(locked_tcb);
3870 * validate the verification tag
3872 vtag_in = ntohl(sh->v_tag);
3875 SCTP_TCB_LOCK_ASSERT(locked_tcb);
3877 if (ch->chunk_type == SCTP_INITIATION) {
3878 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
3879 ntohs(ch->chunk_length), vtag_in);
3881 /* protocol error- silently discard... */
3882 SCTP_STAT_INCR(sctps_badvtag);
3884 SCTP_TCB_UNLOCK(locked_tcb);
3888 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
3890 * If there is no stcb, skip the AUTH chunk and process
3891 * later after a stcb is found (to validate the lookup was
3894 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
3896 !SCTP_BASE_SYSCTL(sctp_auth_disable)) {
3897 /* save this chunk for later processing */
3899 auth_offset = *offset;
3900 auth_len = ntohs(ch->chunk_length);
3902 /* (temporarily) move past this chunk */
3903 *offset += SCTP_SIZE32(auth_len);
3904 if (*offset >= length) {
3905 /* no more data left in the mbuf chain */
3908 SCTP_TCB_UNLOCK(locked_tcb);
3912 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3913 sizeof(struct sctp_chunkhdr), chunk_buf);
3919 SCTP_TCB_UNLOCK(locked_tcb);
3923 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
3924 goto process_control_chunks;
3927 * first check if it's an ASCONF with an unknown src addr we
3928 * need to look inside to find the association
3930 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
3931 struct sctp_chunkhdr *asconf_ch = ch;
3932 uint32_t asconf_offset = 0, asconf_len = 0;
3934 /* inp's refcount may be reduced */
3935 SCTP_INP_INCR_REF(inp);
3937 asconf_offset = *offset;
3939 asconf_len = ntohs(asconf_ch->chunk_length);
3940 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
3942 stcb = sctp_findassociation_ep_asconf(m, iphlen,
3943 *offset, sh, &inp, netp);
3946 asconf_offset += SCTP_SIZE32(asconf_len);
3947 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
3948 sizeof(struct sctp_chunkhdr), chunk_buf);
3949 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
3952 * reduce inp's refcount if not reduced in
3953 * sctp_findassociation_ep_asconf().
3955 SCTP_INP_DECR_REF(inp);
3960 /* now go back and verify any auth chunk to be sure */
3961 if (auth_skipped && (stcb != NULL)) {
3962 struct sctp_auth_chunk *auth;
3964 auth = (struct sctp_auth_chunk *)
3965 sctp_m_getptr(m, auth_offset,
3966 auth_len, chunk_buf);
3969 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
3971 /* auth HMAC failed so dump it */
3974 SCTP_TCB_UNLOCK(locked_tcb);
3978 /* remaining chunks are HMAC checked */
3979 stcb->asoc.authenticated = 1;
3984 /* no association, so it's out of the blue... */
3985 sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL,
3989 SCTP_TCB_UNLOCK(locked_tcb);
3994 /* ABORT and SHUTDOWN can use either v_tag... */
3995 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
3996 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
3997 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
3998 if ((vtag_in == asoc->my_vtag) ||
3999 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
4000 (vtag_in == asoc->peer_vtag))) {
4003 /* drop this packet... */
4004 SCTP_STAT_INCR(sctps_badvtag);
4006 SCTP_TCB_UNLOCK(locked_tcb);
4010 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4011 if (vtag_in != asoc->my_vtag) {
4013 * this could be a stale SHUTDOWN-ACK or the
4014 * peer never got the SHUTDOWN-COMPLETE and
4015 * is still hung; we have started a new asoc
4016 * but it won't complete until the shutdown
4020 SCTP_TCB_UNLOCK(locked_tcb);
4022 sctp_handle_ootb(m, iphlen, *offset, sh, inp,
4023 NULL, vrf_id, port);
4027 /* for all other chunks, vtag must match */
4028 if (vtag_in != asoc->my_vtag) {
4029 /* invalid vtag... */
4030 SCTPDBG(SCTP_DEBUG_INPUT3,
4031 "invalid vtag: %xh, expect %xh\n",
4032 vtag_in, asoc->my_vtag);
4033 SCTP_STAT_INCR(sctps_badvtag);
4035 SCTP_TCB_UNLOCK(locked_tcb);
4041 } /* end if !SCTP_COOKIE_ECHO */
4043 * process all control chunks...
4045 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4046 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4047 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4048 /* implied cookie-ack.. we must have lost the ack */
4049 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4050 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4051 stcb->asoc.overall_error_count,
4053 SCTP_FROM_SCTP_INPUT,
4056 stcb->asoc.overall_error_count = 0;
4057 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4060 process_control_chunks:
4061 while (IS_SCTP_CONTROL(ch)) {
4062 /* validate chunk length */
4063 chk_length = ntohs(ch->chunk_length);
4064 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4065 ch->chunk_type, chk_length);
4066 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4067 if (chk_length < sizeof(*ch) ||
4068 (*offset + (int)chk_length) > length) {
4071 SCTP_TCB_UNLOCK(locked_tcb);
4075 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4077 * INIT-ACK only gets the init ack "header" portion only
4078 * because we don't have to process the peer's COOKIE. All
4079 * others get a complete chunk.
4081 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4082 (ch->chunk_type == SCTP_INITIATION)) {
4083 /* get an init-ack chunk */
4084 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4085 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4089 SCTP_TCB_UNLOCK(locked_tcb);
4094 /* For cookies and all other chunks. */
4095 if (chk_length > sizeof(chunk_buf)) {
4097 * use just the size of the chunk buffer so
4098 * the front part of our chunks fit in
4099 * contiguous space up to the chunk buffer
4100 * size (508 bytes). For chunks that need to
4101 * get more than that they must use the
4102 * sctp_m_getptr() function or other means
4103 * (e.g. know how to parse mbuf chains).
4104 * Cookies do this already.
4106 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4107 (sizeof(chunk_buf) - 4),
4112 SCTP_TCB_UNLOCK(locked_tcb);
4117 /* We can fit it all */
4118 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4119 chk_length, chunk_buf);
4121 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4124 SCTP_TCB_UNLOCK(locked_tcb);
4131 /* Save off the last place we got a control from */
4133 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4135 * allow last_control to be NULL if
4136 * ASCONF... ASCONF processing will find the
4139 if ((netp != NULL) && (*netp != NULL))
4140 stcb->asoc.last_control_chunk_from = *netp;
4143 #ifdef SCTP_AUDITING_ENABLED
4144 sctp_audit_log(0xB0, ch->chunk_type);
4147 /* check to see if this chunk required auth, but isn't */
4148 if ((stcb != NULL) &&
4149 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
4150 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4151 !stcb->asoc.authenticated) {
4152 /* "silently" ignore */
4153 SCTP_STAT_INCR(sctps_recvauthmissing);
4156 switch (ch->chunk_type) {
4157 case SCTP_INITIATION:
4158 /* must be first and only chunk */
4159 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4160 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4161 /* We are not interested anymore? */
4162 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4164 * collision case where we are
4165 * sending to them too
4170 SCTP_TCB_UNLOCK(locked_tcb);
4176 if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) ||
4178 (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) {
4181 SCTP_TCB_UNLOCK(locked_tcb);
4185 if ((stcb != NULL) &&
4186 (SCTP_GET_STATE(&stcb->asoc) ==
4187 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4188 sctp_send_shutdown_ack(stcb,
4189 stcb->asoc.primary_destination);
4191 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4193 SCTP_TCB_UNLOCK(locked_tcb);
4198 sctp_handle_init(m, iphlen, *offset, sh,
4199 (struct sctp_init_chunk *)ch, inp,
4200 stcb, *netp, &abort_no_unlock, vrf_id, port);
4202 if (abort_no_unlock)
4207 SCTP_TCB_UNLOCK(locked_tcb);
4211 case SCTP_PAD_CHUNK:
4213 case SCTP_INITIATION_ACK:
4214 /* must be first and only chunk */
4215 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4216 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4217 /* We are not interested anymore */
4218 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4222 SCTP_TCB_UNLOCK(locked_tcb);
4226 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4227 so = SCTP_INP_SO(inp);
4228 atomic_add_int(&stcb->asoc.refcnt, 1);
4229 SCTP_TCB_UNLOCK(stcb);
4230 SCTP_SOCKET_LOCK(so, 1);
4231 SCTP_TCB_LOCK(stcb);
4232 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4234 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4235 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4236 SCTP_SOCKET_UNLOCK(so, 1);
4242 if ((num_chunks > 1) ||
4243 (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) {
4246 SCTP_TCB_UNLOCK(locked_tcb);
4250 if ((netp) && (*netp)) {
4251 ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
4252 (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id);
4257 * Special case, I must call the output routine to
4258 * get the cookie echoed
4260 if (abort_no_unlock)
4263 if ((stcb) && ret == 0)
4264 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4267 SCTP_TCB_UNLOCK(locked_tcb);
4271 case SCTP_SELECTIVE_ACK:
4272 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4273 SCTP_STAT_INCR(sctps_recvsacks);
4275 struct sctp_sack_chunk *sack;
4277 uint32_t a_rwnd, cum_ack;
4281 if ((stcb == NULL) || (chk_length < sizeof(struct sctp_sack_chunk))) {
4282 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on sack chunk, too small\n");
4286 SCTP_TCB_UNLOCK(locked_tcb);
4290 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4292 * If we have sent a shutdown-ack, we will pay no
4293 * attention to a sack sent in to us since
4294 * we don't care anymore.
4298 sack = (struct sctp_sack_chunk *)ch;
4299 nonce_sum_flag = ch->chunk_flags & SCTP_SACK_NONCE_SUM;
4300 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4301 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4302 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4303 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4308 stcb->asoc.seen_a_sack_this_pkt = 1;
4309 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4311 ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) ||
4312 (cum_ack == stcb->asoc.last_acked_seq)) &&
4313 (stcb->asoc.saw_sack_with_frags == 0) &&
4314 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4317 * We have a SIMPLE sack having no
4318 * prior segments and data on sent
4319 * queue to be acked.. Use the
4320 * faster path sack processing. We
4321 * also allow window update sacks
4322 * with no missing segments to go
4325 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag,
4329 sctp_handle_sack(m, *offset,
4330 sack, stcb, *netp, &abort_now, chk_length, a_rwnd);
4333 /* ABORT signal from sack processing */
4339 case SCTP_HEARTBEAT_REQUEST:
4340 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4341 if ((stcb) && netp && *netp) {
4342 SCTP_STAT_INCR(sctps_recvheartbeat);
4343 sctp_send_heartbeat_ack(stcb, m, *offset,
4346 /* He's alive so give him credit */
4347 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4348 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4349 stcb->asoc.overall_error_count,
4351 SCTP_FROM_SCTP_INPUT,
4354 stcb->asoc.overall_error_count = 0;
4357 case SCTP_HEARTBEAT_ACK:
4358 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
4359 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
4363 SCTP_TCB_UNLOCK(locked_tcb);
4367 /* He's alive so give him credit */
4368 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4369 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4370 stcb->asoc.overall_error_count,
4372 SCTP_FROM_SCTP_INPUT,
4375 stcb->asoc.overall_error_count = 0;
4376 SCTP_STAT_INCR(sctps_recvheartbeatack);
4378 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
4381 case SCTP_ABORT_ASSOCIATION:
4382 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
4384 if ((stcb) && netp && *netp)
4385 sctp_handle_abort((struct sctp_abort_chunk *)ch,
4391 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
4393 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
4396 SCTP_TCB_UNLOCK(locked_tcb);
4400 if (netp && *netp) {
4403 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
4404 stcb, *netp, &abort_flag);
4411 case SCTP_SHUTDOWN_ACK:
4412 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb);
4413 if ((stcb) && (netp) && (*netp))
4414 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
4419 case SCTP_OPERATION_ERROR:
4420 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
4421 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
4427 case SCTP_COOKIE_ECHO:
4428 SCTPDBG(SCTP_DEBUG_INPUT3,
4429 "SCTP_COOKIE-ECHO, stcb %p\n", stcb);
4430 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4433 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4434 /* We are not interested anymore */
4440 * First are we accepting? We do this again here
4441 * sincen it is possible that a previous endpoint
4442 * WAS listening responded to a INIT-ACK and then
4443 * closed. We opened and bound.. and are now no
4447 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
4448 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4449 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
4451 struct sctp_paramhdr *phdr;
4453 oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
4454 0, M_DONTWAIT, 1, MT_DATA);
4456 SCTP_BUF_LEN(oper) =
4457 sizeof(struct sctp_paramhdr);
4459 struct sctp_paramhdr *);
4461 htons(SCTP_CAUSE_OUT_OF_RESC);
4462 phdr->param_length =
4463 htons(sizeof(struct sctp_paramhdr));
4465 sctp_abort_association(inp, stcb, m,
4466 iphlen, sh, oper, vrf_id, port);
4471 struct mbuf *ret_buf;
4472 struct sctp_inpcb *linp;
4481 SCTP_ASOC_CREATE_LOCK(linp);
4485 sctp_handle_cookie_echo(m, iphlen,
4487 (struct sctp_cookie_echo_chunk *)ch,
4499 SCTP_ASOC_CREATE_UNLOCK(linp);
4501 if (ret_buf == NULL) {
4503 SCTP_TCB_UNLOCK(locked_tcb);
4505 SCTPDBG(SCTP_DEBUG_INPUT3,
4506 "GAK, null buffer\n");
4511 /* if AUTH skipped, see if it verified... */
4516 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
4518 * Restart the timer if we have
4521 struct sctp_tmit_chunk *chk;
4523 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
4525 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4526 stcb->sctp_ep, stcb,
4532 case SCTP_COOKIE_ACK:
4533 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb);
4534 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
4536 SCTP_TCB_UNLOCK(locked_tcb);
4540 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4541 /* We are not interested anymore */
4542 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4545 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4546 so = SCTP_INP_SO(inp);
4547 atomic_add_int(&stcb->asoc.refcnt, 1);
4548 SCTP_TCB_UNLOCK(stcb);
4549 SCTP_SOCKET_LOCK(so, 1);
4550 SCTP_TCB_LOCK(stcb);
4551 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4553 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4554 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4555 SCTP_SOCKET_UNLOCK(so, 1);
4561 /* He's alive so give him credit */
4562 if ((stcb) && netp && *netp) {
4563 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4564 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4565 stcb->asoc.overall_error_count,
4567 SCTP_FROM_SCTP_INPUT,
4570 stcb->asoc.overall_error_count = 0;
4571 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
4575 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
4576 /* He's alive so give him credit */
4577 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
4580 SCTP_TCB_UNLOCK(locked_tcb);
4586 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4587 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4588 stcb->asoc.overall_error_count,
4590 SCTP_FROM_SCTP_INPUT,
4593 stcb->asoc.overall_error_count = 0;
4594 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
4599 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
4600 /* He's alive so give him credit */
4601 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
4604 SCTP_TCB_UNLOCK(locked_tcb);
4610 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4611 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4612 stcb->asoc.overall_error_count,
4614 SCTP_FROM_SCTP_INPUT,
4617 stcb->asoc.overall_error_count = 0;
4618 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb);
4621 case SCTP_SHUTDOWN_COMPLETE:
4622 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb);
4623 /* must be first and only chunk */
4624 if ((num_chunks > 1) ||
4625 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4628 SCTP_TCB_UNLOCK(locked_tcb);
4632 if ((stcb) && netp && *netp) {
4633 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
4640 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
4641 /* He's alive so give him credit */
4643 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4644 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4645 stcb->asoc.overall_error_count,
4647 SCTP_FROM_SCTP_INPUT,
4650 stcb->asoc.overall_error_count = 0;
4651 sctp_handle_asconf(m, *offset,
4652 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
4656 case SCTP_ASCONF_ACK:
4657 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
4658 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
4661 SCTP_TCB_UNLOCK(locked_tcb);
4666 if ((stcb) && netp && *netp) {
4667 /* He's alive so give him credit */
4668 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4669 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4670 stcb->asoc.overall_error_count,
4672 SCTP_FROM_SCTP_INPUT,
4675 stcb->asoc.overall_error_count = 0;
4676 sctp_handle_asconf_ack(m, *offset,
4677 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
4678 if (abort_no_unlock)
4682 case SCTP_FORWARD_CUM_TSN:
4683 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
4684 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
4687 SCTP_TCB_UNLOCK(locked_tcb);
4692 /* He's alive so give him credit */
4696 stcb->asoc.overall_error_count = 0;
4697 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4698 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4699 stcb->asoc.overall_error_count,
4701 SCTP_FROM_SCTP_INPUT,
4705 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4706 /* We are not interested anymore */
4707 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4708 so = SCTP_INP_SO(inp);
4709 atomic_add_int(&stcb->asoc.refcnt, 1);
4710 SCTP_TCB_UNLOCK(stcb);
4711 SCTP_SOCKET_LOCK(so, 1);
4712 SCTP_TCB_LOCK(stcb);
4713 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4715 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4716 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4717 SCTP_SOCKET_UNLOCK(so, 1);
4722 sctp_handle_forward_tsn(stcb,
4723 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
4728 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4729 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4730 stcb->asoc.overall_error_count,
4732 SCTP_FROM_SCTP_INPUT,
4735 stcb->asoc.overall_error_count = 0;
4740 case SCTP_STREAM_RESET:
4741 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
4742 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
4745 SCTP_TCB_UNLOCK(locked_tcb);
4750 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4751 /* We are not interested anymore */
4752 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4753 so = SCTP_INP_SO(inp);
4754 atomic_add_int(&stcb->asoc.refcnt, 1);
4755 SCTP_TCB_UNLOCK(stcb);
4756 SCTP_SOCKET_LOCK(so, 1);
4757 SCTP_TCB_LOCK(stcb);
4758 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4760 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
4761 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4762 SCTP_SOCKET_UNLOCK(so, 1);
4767 if (stcb->asoc.peer_supports_strreset == 0) {
4769 * hmm, peer should have announced this, but
4770 * we will turn it on since he is sending us
4773 stcb->asoc.peer_supports_strreset = 1;
4775 if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) {
4776 /* stop processing */
4781 case SCTP_PACKET_DROPPED:
4782 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
4783 /* re-get it all please */
4784 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
4787 SCTP_TCB_UNLOCK(locked_tcb);
4792 if (ch && (stcb) && netp && (*netp)) {
4793 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
4795 min(chk_length, (sizeof(chunk_buf) - 4)));
4800 case SCTP_AUTHENTICATION:
4801 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
4802 if (SCTP_BASE_SYSCTL(sctp_auth_disable))
4806 /* save the first AUTH for later processing */
4807 if (auth_skipped == 0) {
4808 auth_offset = *offset;
4809 auth_len = chk_length;
4812 /* skip this chunk (temporarily) */
4815 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
4816 (chk_length > (sizeof(struct sctp_auth_chunk) +
4817 SCTP_AUTH_DIGEST_LEN_MAX))) {
4820 SCTP_TCB_UNLOCK(locked_tcb);
4825 if (got_auth == 1) {
4826 /* skip this chunk... it's already auth'd */
4830 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
4832 /* auth HMAC failed so dump the packet */
4836 /* remaining chunks are HMAC checked */
4837 stcb->asoc.authenticated = 1;
4843 /* it's an unknown chunk! */
4844 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
4846 struct sctp_paramhdr *phd;
4848 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
4849 0, M_DONTWAIT, 1, MT_DATA);
4851 phd = mtod(mm, struct sctp_paramhdr *);
4853 * We cheat and use param type since
4854 * we did not bother to define a
4855 * error cause struct. They are the
4856 * same basic format with different
4859 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
4860 phd->param_length = htons(chk_length + sizeof(*phd));
4861 SCTP_BUF_LEN(mm) = sizeof(*phd);
4862 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length),
4864 if (SCTP_BUF_NEXT(mm)) {
4865 #ifdef SCTP_MBUF_LOGGING
4866 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
4869 mat = SCTP_BUF_NEXT(mm);
4871 if (SCTP_BUF_IS_EXTENDED(mat)) {
4872 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
4874 mat = SCTP_BUF_NEXT(mat);
4878 sctp_queue_op_err(stcb, mm);
4884 if ((ch->chunk_type & 0x80) == 0) {
4885 /* discard this packet */
4888 } /* else skip this bad chunk and continue... */
4890 } /* switch (ch->chunk_type) */
4894 /* get the next chunk */
4895 *offset += SCTP_SIZE32(chk_length);
4896 if (*offset >= length) {
4897 /* no more data left in the mbuf chain */
4900 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4901 sizeof(struct sctp_chunkhdr), chunk_buf);
4904 SCTP_TCB_UNLOCK(locked_tcb);
4911 if (asconf_cnt > 0 && stcb != NULL) {
4912 sctp_send_asconf_ack(stcb);
4919 * Process the ECN bits we have something set so we must look to see if it is
4920 * ECN(0) or ECN(1) or CE
4923 sctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net,
4926 if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
4928 } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) {
4930 * we only add to the nonce sum for ECT1, ECT0 does not
4931 * change the NS bit (that we have yet to find a way to send
4935 /* ECN Nonce stuff */
4936 stcb->asoc.receiver_nonce_sum++;
4937 stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM;
4940 * Drag up the last_echo point if cumack is larger since we
4941 * don't want the point falling way behind by more than
4942 * 2^^31 and then having it be incorrect.
4944 if (compare_with_wrap(stcb->asoc.cumulative_tsn,
4945 stcb->asoc.last_echo_tsn, MAX_TSN)) {
4946 stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
4948 } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) {
4950 * Drag up the last_echo point if cumack is larger since we
4951 * don't want the point falling way behind by more than
4952 * 2^^31 and then having it be incorrect.
4954 if (compare_with_wrap(stcb->asoc.cumulative_tsn,
4955 stcb->asoc.last_echo_tsn, MAX_TSN)) {
4956 stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
4962 sctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net,
4963 uint32_t high_tsn, uint8_t ecn_bits)
4965 if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
4967 * we possibly must notify the sender that a congestion
4968 * window reduction is in order. We do this by adding a ECNE
4969 * chunk to the output chunk queue. The incoming CWR will
4970 * remove this chunk.
4972 if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn,
4974 /* Yep, we need to add a ECNE */
4975 sctp_send_ecn_echo(stcb, net, high_tsn);
4976 stcb->asoc.last_echo_tsn = high_tsn;
4983 sctp_validate_no_locks(struct sctp_inpcb *inp)
4985 struct sctp_tcb *stcb;
4987 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4988 if (mtx_owned(&stcb->tcb_mtx)) {
4989 panic("Own lock on stcb at return from input");
4997 * common input chunk processing (v4 and v6)
5000 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
5001 int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
5002 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
5003 uint8_t ecn_bits, uint32_t vrf_id, uint16_t port)
5006 * Control chunk processing
5009 int fwd_tsn_seen = 0, data_processed = 0;
5010 struct mbuf *m = *mm;
5014 SCTP_STAT_INCR(sctps_recvdatagrams);
5015 #ifdef SCTP_AUDITING_ENABLED
5016 sctp_audit_log(0xE0, 1);
5017 sctp_auditing(0, inp, stcb, net);
5020 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5021 m, iphlen, offset, length, stcb);
5023 /* always clear this before beginning a packet */
5024 stcb->asoc.authenticated = 0;
5025 stcb->asoc.seen_a_sack_this_pkt = 0;
5026 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5027 stcb, stcb->asoc.state);
5029 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5030 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5032 * If we hit here, we had a ref count
5033 * up when the assoc was aborted and the
5034 * timer is clearing out the assoc, we should
5035 * NOT respond to any packet.. its OOTB.
5037 SCTP_TCB_UNLOCK(stcb);
5038 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5043 if (IS_SCTP_CONTROL(ch)) {
5044 /* process the control portion of the SCTP packet */
5045 /* sa_ignore NO_NULL_CHK */
5046 stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
5047 inp, stcb, &net, &fwd_tsn_seen, vrf_id, port);
5050 * This covers us if the cookie-echo was there and
5051 * it changes our INP.
5053 inp = stcb->sctp_ep;
5054 if ((net) && (port)) {
5055 if (net->port == 0) {
5056 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr));
5063 * no control chunks, so pre-process DATA chunks (these
5064 * checks are taken care of by control processing)
5068 * if DATA only packet, and auth is required, then punt...
5069 * can't have authenticated without any AUTH (control)
5072 if ((stcb != NULL) &&
5073 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5074 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5075 /* "silently" ignore */
5076 SCTP_STAT_INCR(sctps_recvauthmissing);
5077 SCTP_TCB_UNLOCK(stcb);
5081 /* out of the blue DATA chunk */
5082 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5086 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5087 /* v_tag mismatch! */
5088 SCTP_STAT_INCR(sctps_badvtag);
5089 SCTP_TCB_UNLOCK(stcb);
5096 * no valid TCB for this packet, or we found it's a bad
5097 * packet while processing control, or we're done with this
5098 * packet (done or skip rest of data), so we drop it...
5103 * DATA chunk processing
5105 /* plow through the data chunks while length > offset */
5108 * Rest should be DATA only. Check authentication state if AUTH for
5111 if ((length > offset) &&
5113 !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5114 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5115 !stcb->asoc.authenticated) {
5116 /* "silently" ignore */
5117 SCTP_STAT_INCR(sctps_recvauthmissing);
5118 SCTPDBG(SCTP_DEBUG_AUTH1,
5119 "Data chunk requires AUTH, skipped\n");
5122 if (length > offset) {
5126 * First check to make sure our state is correct. We would
5127 * not get here unless we really did have a tag, so we don't
5128 * abort if this happens, just dump the chunk silently.
5130 switch (SCTP_GET_STATE(&stcb->asoc)) {
5131 case SCTP_STATE_COOKIE_ECHOED:
5133 * we consider data with valid tags in this state
5134 * shows us the cookie-ack was lost. Imply it was
5137 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5138 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5139 stcb->asoc.overall_error_count,
5141 SCTP_FROM_SCTP_INPUT,
5144 stcb->asoc.overall_error_count = 0;
5145 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5147 case SCTP_STATE_COOKIE_WAIT:
5149 * We consider OOTB any data sent during asoc setup.
5151 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5153 SCTP_TCB_UNLOCK(stcb);
5155 /* sa_ignore NOTREACHED */
5157 case SCTP_STATE_EMPTY: /* should not happen */
5158 case SCTP_STATE_INUSE: /* should not happen */
5159 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5160 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5162 SCTP_TCB_UNLOCK(stcb);
5164 /* sa_ignore NOTREACHED */
5166 case SCTP_STATE_OPEN:
5167 case SCTP_STATE_SHUTDOWN_SENT:
5170 /* take care of ECN, part 1. */
5171 if (stcb->asoc.ecn_allowed &&
5172 (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) {
5173 sctp_process_ecn_marked_a(stcb, net, ecn_bits);
5175 /* plow through the data chunks while length > offset */
5176 retval = sctp_process_data(mm, iphlen, &offset, length, sh,
5177 inp, stcb, net, &high_tsn);
5180 * The association aborted, NO UNLOCK needed since
5181 * the association is destroyed.
5187 /* take care of ecn part 2. */
5188 if (stcb->asoc.ecn_allowed &&
5189 (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) {
5190 sctp_process_ecn_marked_b(stcb, net, high_tsn,
5195 * Anything important needs to have been m_copy'ed in
5199 if ((data_processed == 0) && (fwd_tsn_seen)) {
5202 if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
5203 stcb->asoc.cumulative_tsn, MAX_TSN)) {
5204 /* there was a gap before this data was processed */
5207 sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
5209 /* Again, we aborted so NO UNLOCK needed */
5213 /* trigger send of any chunks in queue... */
5215 #ifdef SCTP_AUDITING_ENABLED
5216 sctp_audit_log(0xE0, 2);
5217 sctp_auditing(1, inp, stcb, net);
5219 SCTPDBG(SCTP_DEBUG_INPUT1,
5220 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5221 stcb->asoc.peers_rwnd,
5222 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5223 stcb->asoc.total_flight);
5224 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5226 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) ||
5228 (stcb->asoc.peers_rwnd > 0 ||
5229 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5230 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5231 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5232 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5234 #ifdef SCTP_AUDITING_ENABLED
5235 sctp_audit_log(0xE0, 3);
5236 sctp_auditing(2, inp, stcb, net);
5238 SCTP_TCB_UNLOCK(stcb);
5241 sctp_validate_no_locks(inp);
5248 sctp_input_with_port(i_pak, off, port)
5253 #ifdef SCTP_MBUF_LOGGING
5259 uint32_t vrf_id = 0;
5263 struct sctp_inpcb *inp = NULL;
5265 uint32_t check, calc_check;
5266 struct sctp_nets *net;
5267 struct sctp_tcb *stcb = NULL;
5268 struct sctp_chunkhdr *ch;
5269 int refcount_up = 0;
5270 int length, mlen, offset;
5273 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5274 SCTP_RELEASE_PKT(i_pak);
5277 mlen = SCTP_HEADER_LEN(i_pak);
5279 m = SCTP_HEADER_TO_CHAIN(i_pak);
5282 SCTP_STAT_INCR(sctps_recvpackets);
5283 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5286 #ifdef SCTP_MBUF_LOGGING
5287 /* Log in any input mbufs */
5288 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5291 if (SCTP_BUF_IS_EXTENDED(mat)) {
5292 sctp_log_mb(mat, SCTP_MBUF_INPUT);
5294 mat = SCTP_BUF_NEXT(mat);
5298 #ifdef SCTP_PACKET_LOGGING
5299 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
5300 sctp_packet_log(m, mlen);
5303 * Must take out the iphlen, since mlen expects this (only effect lb
5309 * Get IP, SCTP, and first chunk header together in first mbuf.
5311 ip = mtod(m, struct ip *);
5312 offset = iphlen + sizeof(*sh) + sizeof(*ch);
5313 if (SCTP_BUF_LEN(m) < offset) {
5314 if ((m = m_pullup(m, offset)) == 0) {
5315 SCTP_STAT_INCR(sctps_hdrops);
5318 ip = mtod(m, struct ip *);
5320 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5321 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
5322 SCTPDBG(SCTP_DEBUG_INPUT1,
5323 "sctp_input() length:%d iphlen:%d\n", mlen, iphlen);
5325 /* SCTP does not allow broadcasts or multicasts */
5326 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
5329 if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) {
5331 * We only look at broadcast if its a front state, All
5332 * others we will not have a tcb for anyway.
5336 /* validate SCTP checksum */
5337 check = sh->checksum; /* save incoming checksum */
5338 if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) &&
5339 ((ip->ip_src.s_addr == ip->ip_dst.s_addr) ||
5340 (SCTP_IS_IT_LOOPBACK(m)))
5342 goto sctp_skip_csum_4;
5344 sh->checksum = 0; /* prepare for calc */
5345 calc_check = sctp_calculate_sum(m, &mlen, iphlen);
5346 if (calc_check != check) {
5347 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5348 calc_check, check, m, mlen, iphlen);
5350 stcb = sctp_findassociation_addr(m, iphlen,
5351 offset - sizeof(*ch),
5354 if ((net) && (port)) {
5355 if (net->port == 0) {
5356 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr));
5360 if ((inp) && (stcb)) {
5361 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
5362 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5363 } else if ((inp != NULL) && (stcb == NULL)) {
5366 SCTP_STAT_INCR(sctps_badsum);
5367 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5370 sh->checksum = calc_check;
5372 /* destination port of 0 is illegal, based on RFC2960. */
5373 if (sh->dest_port == 0) {
5374 SCTP_STAT_INCR(sctps_hdrops);
5377 /* validate mbuf chain length with IP payload length */
5378 if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) {
5379 SCTP_STAT_INCR(sctps_hdrops);
5383 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
5384 * IP/SCTP/first chunk header...
5386 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
5387 sh, ch, &inp, &net, vrf_id);
5388 if ((net) && (port)) {
5389 if (net->port == 0) {
5390 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr));
5394 /* inp's ref-count increased && stcb locked */
5396 struct sctp_init_chunk *init_chk, chunk_buf;
5398 SCTP_STAT_INCR(sctps_noport);
5401 * we use the bandwidth limiting to protect against sending
5402 * too many ABORTS all at once. In this case these count the
5403 * same as an ICMP message.
5405 if (badport_bandlim(0) < 0)
5407 #endif /* ICMP_BANDLIM */
5408 SCTPDBG(SCTP_DEBUG_INPUT1,
5409 "Sending a ABORT from packet entry!\n");
5410 if (ch->chunk_type == SCTP_INITIATION) {
5412 * we do a trick here to get the INIT tag, dig in
5413 * and get the tag from the INIT and put it in the
5416 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
5417 iphlen + sizeof(*sh), sizeof(*init_chk),
5418 (uint8_t *) & chunk_buf);
5419 if (init_chk != NULL)
5420 sh->v_tag = init_chk->init.initiate_tag;
5422 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5423 sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port);
5426 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5429 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
5430 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
5432 } else if (stcb == NULL) {
5437 * I very much doubt any of the IPSEC stuff will work but I have no
5438 * idea, so I will leave it in place.
5440 if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) {
5441 MODULE_GLOBAL(MOD_IPSEC, ipsec4stat).in_polvio++;
5442 SCTP_STAT_INCR(sctps_hdrops);
5448 * common chunk processing
5450 length = ip->ip_len + iphlen;
5451 offset -= sizeof(struct sctp_chunkhdr);
5453 ecn_bits = ip->ip_tos;
5455 /* sa_ignore NO_NULL_CHK */
5456 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
5457 inp, stcb, net, ecn_bits, vrf_id, port);
5458 /* inp's ref-count reduced && stcb unlocked */
5462 if ((inp) && (refcount_up)) {
5463 /* reduce ref-count */
5464 SCTP_INP_DECR_REF(inp);
5469 SCTP_TCB_UNLOCK(stcb);
5471 if ((inp) && (refcount_up)) {
5472 /* reduce ref-count */
5473 SCTP_INP_DECR_REF(inp);
5481 sctp_input(i_pak, off)
5485 sctp_input_with_port(i_pak, off, 0);