michael@0: /*- michael@0: * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved. michael@0: * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. michael@0: * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions are met: michael@0: * michael@0: * a) Redistributions of source code must retain the above copyright notice, michael@0: * this list of conditions and the following disclaimer. michael@0: * michael@0: * b) Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in michael@0: * the documentation and/or other materials provided with the distribution. michael@0: * michael@0: * c) Neither the name of Cisco Systems, Inc. nor the names of its michael@0: * contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, michael@0: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE michael@0: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS michael@0: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN michael@0: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF michael@0: * THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifdef __FreeBSD__ michael@0: #include michael@0: __FBSDID("$FreeBSD: head/sys/netinet/sctp_input.c 262252 2014-02-20 20:14:43Z tuexen $"); michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #if !defined(__Userspace_os_Windows) michael@0: #include michael@0: #endif michael@0: #if defined(__FreeBSD__) michael@0: #include michael@0: #endif michael@0: michael@0: #if defined(__APPLE__) michael@0: #define APPLE_FILE_NO 2 michael@0: #endif michael@0: michael@0: michael@0: static void michael@0: sctp_stop_all_cookie_timers(struct sctp_tcb *stcb) michael@0: { michael@0: struct sctp_nets *net; michael@0: michael@0: /* This now not only stops all cookie timers michael@0: * it also stops any INIT timers as well. This michael@0: * will make sure that the timers are stopped in michael@0: * all collision cases. michael@0: */ michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { michael@0: if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) { michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, michael@0: stcb->sctp_ep, michael@0: stcb, michael@0: net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_1); michael@0: } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) { michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_INIT, michael@0: stcb->sctp_ep, michael@0: stcb, michael@0: net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_2); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* INIT handler */ michael@0: static void michael@0: sctp_handle_init(struct mbuf *m, int iphlen, int offset, michael@0: struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh, michael@0: struct sctp_init_chunk *cp, struct sctp_inpcb *inp, michael@0: struct sctp_tcb *stcb, int *abort_no_unlock, michael@0: #if defined(__FreeBSD__) michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: #endif michael@0: uint32_t vrf_id, uint16_t port) michael@0: { michael@0: struct sctp_init *init; michael@0: struct mbuf *op_err; michael@0: michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n", michael@0: (void *)stcb); michael@0: if (stcb == NULL) { michael@0: SCTP_INP_RLOCK(inp); michael@0: } michael@0: /* validate length */ michael@0: if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) { michael@0: op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); michael@0: sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: if (stcb) michael@0: *abort_no_unlock = 1; michael@0: goto outnow; michael@0: } michael@0: /* validate parameters */ michael@0: init = &cp->init; michael@0: if (init->initiate_tag == 0) { michael@0: /* protocol error... send abort */ michael@0: op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); michael@0: sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: if (stcb) michael@0: *abort_no_unlock = 1; michael@0: goto outnow; michael@0: } michael@0: if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) { michael@0: /* invalid parameter... send abort */ michael@0: op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); michael@0: sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: if (stcb) michael@0: *abort_no_unlock = 1; michael@0: goto outnow; michael@0: } michael@0: if (init->num_inbound_streams == 0) { michael@0: /* protocol error... send abort */ michael@0: op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); michael@0: sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: if (stcb) michael@0: *abort_no_unlock = 1; michael@0: goto outnow; michael@0: } michael@0: if (init->num_outbound_streams == 0) { michael@0: /* protocol error... send abort */ michael@0: op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); michael@0: sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: if (stcb) michael@0: *abort_no_unlock = 1; michael@0: goto outnow; michael@0: } michael@0: if (sctp_validate_init_auth_params(m, offset + sizeof(*cp), michael@0: offset + ntohs(cp->ch.chunk_length))) { michael@0: /* auth parameter(s) error... send abort */ michael@0: sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, NULL, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: if (stcb) michael@0: *abort_no_unlock = 1; michael@0: goto outnow; michael@0: } michael@0: /* We are only accepting if we have a socket with positive so_qlimit.*/ michael@0: if ((stcb == NULL) && michael@0: ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || michael@0: (inp->sctp_socket == NULL) || michael@0: (inp->sctp_socket->so_qlimit == 0))) { michael@0: /* michael@0: * FIX ME ?? What about TCP model and we have a michael@0: * match/restart case? Actually no fix is needed. michael@0: * the lookup will always find the existing assoc so stcb michael@0: * would not be NULL. It may be questionable to do this michael@0: * since we COULD just send back the INIT-ACK and hope that michael@0: * the app did accept()'s by the time the COOKIE was sent. But michael@0: * there is a price to pay for COOKIE generation and I don't michael@0: * want to pay it on the chance that the app will actually do michael@0: * some accepts(). The App just looses and should NOT be in michael@0: * this state :-) michael@0: */ michael@0: if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) { michael@0: sctp_send_abort(m, iphlen, src, dst, sh, 0, NULL, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: } michael@0: goto outnow; michael@0: } michael@0: if ((stcb != NULL) && michael@0: (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n"); michael@0: sctp_send_shutdown_ack(stcb, NULL); michael@0: sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); michael@0: } else { michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n"); michael@0: sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, src, dst, michael@0: sh, cp, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port, michael@0: ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED)); michael@0: } michael@0: outnow: michael@0: if (stcb == NULL) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * process peer "INIT/INIT-ACK" chunk returns value < 0 on error michael@0: */ michael@0: michael@0: int michael@0: sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: int unsent_data = 0; michael@0: unsigned int i; michael@0: struct sctp_stream_queue_pending *sp; michael@0: struct sctp_association *asoc; michael@0: michael@0: /* This function returns the number of streams that have michael@0: * true unsent data on them. Note that as it looks through michael@0: * it will clean up any places that have old data that michael@0: * has been sent but left at top of stream queue. michael@0: */ michael@0: asoc = &stcb->asoc; michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { michael@0: /* Check to see if some data queued */ michael@0: for (i = 0; i < stcb->asoc.streamoutcnt; i++) { michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue); michael@0: if (sp == NULL) { michael@0: continue; michael@0: } michael@0: if ((sp->msg_is_complete) && michael@0: (sp->length == 0) && michael@0: (sp->sender_all_done)) { michael@0: /* We are doing differed cleanup. Last michael@0: * time through when we took all the data michael@0: * the sender_all_done was not set. michael@0: */ michael@0: if (sp->put_last_out == 0) { michael@0: SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n"); michael@0: SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n", michael@0: sp->sender_all_done, michael@0: sp->length, michael@0: sp->msg_is_complete, michael@0: sp->put_last_out); michael@0: } michael@0: atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1); michael@0: TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next); michael@0: if (sp->net) { michael@0: sctp_free_remote_addr(sp->net); michael@0: sp->net = NULL; michael@0: } michael@0: if (sp->data) { michael@0: sctp_m_freem(sp->data); michael@0: sp->data = NULL; michael@0: } michael@0: sctp_free_a_strmoq(stcb, sp, so_locked); michael@0: } else { michael@0: unsent_data++; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: return (unsent_data); michael@0: } michael@0: michael@0: static int michael@0: sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb) michael@0: { michael@0: struct sctp_init *init; michael@0: struct sctp_association *asoc; michael@0: struct sctp_nets *lnet; michael@0: unsigned int i; michael@0: michael@0: init = &cp->init; michael@0: asoc = &stcb->asoc; michael@0: /* save off parameters */ michael@0: asoc->peer_vtag = ntohl(init->initiate_tag); michael@0: asoc->peers_rwnd = ntohl(init->a_rwnd); michael@0: /* init tsn's */ michael@0: asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1; michael@0: michael@0: if (!TAILQ_EMPTY(&asoc->nets)) { michael@0: /* update any ssthresh's that may have a default */ michael@0: TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) { michael@0: lnet->ssthresh = asoc->peers_rwnd; michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE|SCTP_CWND_LOGGING_ENABLE)) { michael@0: sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION); michael@0: } michael@0: michael@0: } michael@0: } michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) { michael@0: unsigned int newcnt; michael@0: struct sctp_stream_out *outs; michael@0: struct sctp_stream_queue_pending *sp, *nsp; michael@0: struct sctp_tmit_chunk *chk, *nchk; michael@0: michael@0: /* abandon the upper streams */ michael@0: newcnt = ntohs(init->num_inbound_streams); michael@0: TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { michael@0: if (chk->rec.data.stream_number >= newcnt) { michael@0: TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); michael@0: asoc->send_queue_cnt--; michael@0: if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) { michael@0: asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--; michael@0: #ifdef INVARIANTS michael@0: } else { michael@0: panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number); michael@0: #endif michael@0: } michael@0: if (chk->data != NULL) { michael@0: sctp_free_bufspace(stcb, asoc, chk, 1); michael@0: sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, michael@0: 0, chk, SCTP_SO_NOT_LOCKED); michael@0: if (chk->data) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: } michael@0: } michael@0: sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: } michael@0: } michael@0: if (asoc->strmout) { michael@0: for (i = newcnt; i < asoc->pre_open_streams; i++) { michael@0: outs = &asoc->strmout[i]; michael@0: TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { michael@0: TAILQ_REMOVE(&outs->outqueue, sp, next); michael@0: asoc->stream_queue_cnt--; michael@0: sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, michael@0: stcb, 0, sp, SCTP_SO_NOT_LOCKED); michael@0: if (sp->data) { michael@0: sctp_m_freem(sp->data); michael@0: sp->data = NULL; michael@0: } michael@0: if (sp->net) { michael@0: sctp_free_remote_addr(sp->net); michael@0: sp->net = NULL; michael@0: } michael@0: /* Free the chunk */ michael@0: sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED); michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: } michael@0: } michael@0: } michael@0: /* cut back the count */ michael@0: asoc->pre_open_streams = newcnt; michael@0: } michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams; michael@0: michael@0: /* EY - nr_sack: initialize highest tsn in nr_mapping_array */ michael@0: asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map; michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { michael@0: sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); michael@0: } michael@0: /* This is the next one we expect */ michael@0: asoc->str_reset_seq_in = asoc->asconf_seq_in + 1; michael@0: michael@0: asoc->mapping_array_base_tsn = ntohl(init->initial_tsn); michael@0: asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in; michael@0: michael@0: asoc->advanced_peer_ack_point = asoc->last_acked_seq; michael@0: /* open the requested streams */ michael@0: michael@0: if (asoc->strmin != NULL) { michael@0: /* Free the old ones */ michael@0: struct sctp_queued_to_read *ctl, *nctl; michael@0: michael@0: for (i = 0; i < asoc->streamincnt; i++) { michael@0: TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) { michael@0: TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); michael@0: sctp_free_remote_addr(ctl->whoFrom); michael@0: ctl->whoFrom = NULL; michael@0: sctp_m_freem(ctl->data); michael@0: ctl->data = NULL; michael@0: sctp_free_a_readq(stcb, ctl); michael@0: } michael@0: } michael@0: SCTP_FREE(asoc->strmin, SCTP_M_STRMI); michael@0: } michael@0: if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) { michael@0: asoc->streamincnt = ntohs(init->num_outbound_streams); michael@0: } else { michael@0: asoc->streamincnt = asoc->max_inbound_streams; michael@0: } michael@0: SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt * michael@0: sizeof(struct sctp_stream_in), SCTP_M_STRMI); michael@0: if (asoc->strmin == NULL) { michael@0: /* we didn't get memory for the streams! */ michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n"); michael@0: return (-1); michael@0: } michael@0: for (i = 0; i < asoc->streamincnt; i++) { michael@0: asoc->strmin[i].stream_no = i; michael@0: asoc->strmin[i].last_sequence_delivered = 0xffff; michael@0: TAILQ_INIT(&asoc->strmin[i].inqueue); michael@0: asoc->strmin[i].delivery_started = 0; michael@0: } michael@0: /* michael@0: * load_address_from_init will put the addresses into the michael@0: * association when the COOKIE is processed or the INIT-ACK is michael@0: * processed. Both types of COOKIE's existing and new call this michael@0: * routine. It will remove addresses that are no longer in the michael@0: * association (for the restarting case where addresses are michael@0: * removed). Up front when the INIT arrives we will discard it if it michael@0: * is a restart and new addresses have been added. michael@0: */ michael@0: /* sa_ignore MEMLEAK */ michael@0: return (0); michael@0: } michael@0: michael@0: /* michael@0: * INIT-ACK message processing/consumption returns value < 0 on error michael@0: */ michael@0: static int michael@0: sctp_process_init_ack(struct mbuf *m, int iphlen, int offset, michael@0: struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh, michael@0: struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, michael@0: struct sctp_nets *net, int *abort_no_unlock, michael@0: #if defined(__FreeBSD__) michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: #endif michael@0: uint32_t vrf_id) michael@0: { michael@0: struct sctp_association *asoc; michael@0: struct mbuf *op_err; michael@0: int retval, abort_flag; michael@0: uint32_t initack_limit; michael@0: int nat_friendly = 0; michael@0: michael@0: /* First verify that we have no illegal param's */ michael@0: abort_flag = 0; michael@0: michael@0: op_err = sctp_arethere_unrecognized_parameters(m, michael@0: (offset + sizeof(struct sctp_init_chunk)), michael@0: &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly); michael@0: if (abort_flag) { michael@0: /* Send an abort and notify peer */ michael@0: sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); michael@0: *abort_no_unlock = 1; michael@0: return (-1); michael@0: } michael@0: asoc = &stcb->asoc; michael@0: asoc->peer_supports_nat = (uint8_t)nat_friendly; michael@0: /* process the peer's parameters in the INIT-ACK */ michael@0: retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb); michael@0: if (retval < 0) { michael@0: return (retval); michael@0: } michael@0: initack_limit = offset + ntohs(cp->ch.chunk_length); michael@0: /* load all addresses */ michael@0: if ((retval = sctp_load_addresses_from_init(stcb, m, michael@0: (offset + sizeof(struct sctp_init_chunk)), initack_limit, michael@0: src, dst, NULL))) { michael@0: /* Huh, we should abort */ michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, michael@0: "Load addresses from INIT causes an abort %d\n", michael@0: retval); michael@0: sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, michael@0: src, dst, sh, NULL, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, net->port); michael@0: *abort_no_unlock = 1; michael@0: return (-1); michael@0: } michael@0: /* if the peer doesn't support asconf, flush the asconf queue */ michael@0: if (asoc->peer_supports_asconf == 0) { michael@0: struct sctp_asconf_addr *param, *nparam; michael@0: michael@0: TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) { michael@0: TAILQ_REMOVE(&asoc->asconf_queue, param, next); michael@0: SCTP_FREE(param, SCTP_M_ASC_ADDR); michael@0: } michael@0: } michael@0: michael@0: stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, michael@0: stcb->asoc.local_hmacs); michael@0: if (op_err) { michael@0: sctp_queue_op_err(stcb, op_err); michael@0: /* queuing will steal away the mbuf chain to the out queue */ michael@0: op_err = NULL; michael@0: } michael@0: /* extract the cookie and queue it to "echo" it back... */ michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { michael@0: sctp_misc_ints(SCTP_THRESHOLD_CLEAR, michael@0: stcb->asoc.overall_error_count, michael@0: 0, michael@0: SCTP_FROM_SCTP_INPUT, michael@0: __LINE__); michael@0: } michael@0: stcb->asoc.overall_error_count = 0; michael@0: net->error_count = 0; michael@0: michael@0: /* michael@0: * Cancel the INIT timer, We do this first before queueing the michael@0: * cookie. We always cancel at the primary to assue that we are michael@0: * canceling the timer started by the INIT which always goes to the michael@0: * primary. michael@0: */ michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb, michael@0: asoc->primary_destination, SCTP_FROM_SCTP_INPUT+SCTP_LOC_4); michael@0: michael@0: /* calculate the RTO */ michael@0: net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy, michael@0: SCTP_RTT_FROM_NON_DATA); michael@0: michael@0: retval = sctp_send_cookie_echo(m, offset, stcb, net); michael@0: if (retval < 0) { michael@0: /* michael@0: * No cookie, we probably should send a op error. But in any michael@0: * case if there is no cookie in the INIT-ACK, we can michael@0: * abandon the peer, its broke. michael@0: */ michael@0: if (retval == -3) { michael@0: /* We abort with an error of missing mandatory param */ michael@0: op_err = michael@0: sctp_generate_invmanparam(SCTP_CAUSE_MISSING_PARAM); michael@0: if (op_err) { michael@0: /* michael@0: * Expand beyond to include the mandatory michael@0: * param cookie michael@0: */ michael@0: struct sctp_inv_mandatory_param *mp; michael@0: michael@0: SCTP_BUF_LEN(op_err) = michael@0: sizeof(struct sctp_inv_mandatory_param); michael@0: mp = mtod(op_err, michael@0: struct sctp_inv_mandatory_param *); michael@0: /* Subtract the reserved param */ michael@0: mp->length = michael@0: htons(sizeof(struct sctp_inv_mandatory_param) - 2); michael@0: mp->num_param = htonl(1); michael@0: mp->param = htons(SCTP_STATE_COOKIE); michael@0: mp->resv = 0; michael@0: } michael@0: sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, michael@0: src, dst, sh, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, net->port); michael@0: *abort_no_unlock = 1; michael@0: } michael@0: return (retval); michael@0: } michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: static void michael@0: sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp, michael@0: struct sctp_tcb *stcb, struct sctp_nets *net) michael@0: { michael@0: struct sockaddr_storage store; michael@0: struct sctp_nets *r_net, *f_net; michael@0: struct timeval tv; michael@0: int req_prim = 0; michael@0: uint16_t old_error_counter; michael@0: #ifdef INET michael@0: struct sockaddr_in *sin; michael@0: #endif michael@0: #ifdef INET6 michael@0: struct sockaddr_in6 *sin6; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: struct sockaddr_conn *sconn; michael@0: #endif michael@0: michael@0: if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) { michael@0: /* Invalid length */ michael@0: return; michael@0: } michael@0: michael@0: memset(&store, 0, sizeof(store)); michael@0: switch (cp->heartbeat.hb_info.addr_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) { michael@0: sin = (struct sockaddr_in *)&store; michael@0: sin->sin_family = cp->heartbeat.hb_info.addr_family; michael@0: #ifdef HAVE_SIN_LEN michael@0: sin->sin_len = cp->heartbeat.hb_info.addr_len; michael@0: #endif michael@0: sin->sin_port = stcb->rport; michael@0: memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address, michael@0: sizeof(sin->sin_addr)); michael@0: } else { michael@0: return; michael@0: } michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) { michael@0: sin6 = (struct sockaddr_in6 *)&store; michael@0: sin6->sin6_family = cp->heartbeat.hb_info.addr_family; michael@0: #ifdef HAVE_SIN6_LEN michael@0: sin6->sin6_len = cp->heartbeat.hb_info.addr_len; michael@0: #endif michael@0: sin6->sin6_port = stcb->rport; michael@0: memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address, michael@0: sizeof(sin6->sin6_addr)); michael@0: } else { michael@0: return; michael@0: } michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_conn)) { michael@0: sconn = (struct sockaddr_conn *)&store; michael@0: sconn->sconn_family = cp->heartbeat.hb_info.addr_family; michael@0: #ifdef HAVE_SCONN_LEN michael@0: sconn->sconn_len = cp->heartbeat.hb_info.addr_len; michael@0: #endif michael@0: sconn->sconn_port = stcb->rport; michael@0: memcpy(&sconn->sconn_addr, cp->heartbeat.hb_info.address, michael@0: sizeof(sconn->sconn_addr)); michael@0: } else { michael@0: return; michael@0: } michael@0: break; michael@0: #endif michael@0: default: michael@0: return; michael@0: } michael@0: r_net = sctp_findnet(stcb, (struct sockaddr *)&store); michael@0: if (r_net == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n"); michael@0: return; michael@0: } michael@0: if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) && michael@0: (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) && michael@0: (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) { michael@0: /* michael@0: * If the its a HB and it's random value is correct when can michael@0: * confirm the destination. michael@0: */ michael@0: r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED; michael@0: if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) { michael@0: stcb->asoc.primary_destination = r_net; michael@0: r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY; michael@0: f_net = TAILQ_FIRST(&stcb->asoc.nets); michael@0: if (f_net != r_net) { michael@0: /* first one on the list is NOT the primary michael@0: * sctp_cmpaddr() is much more efficent if michael@0: * the primary is the first on the list, make it michael@0: * so. michael@0: */ michael@0: TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next); michael@0: TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next); michael@0: } michael@0: req_prim = 1; michael@0: } michael@0: sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, michael@0: stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED); michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net); michael@0: } michael@0: old_error_counter = r_net->error_count; michael@0: r_net->error_count = 0; michael@0: r_net->hb_responded = 1; michael@0: tv.tv_sec = cp->heartbeat.hb_info.time_value_1; michael@0: tv.tv_usec = cp->heartbeat.hb_info.time_value_2; michael@0: /* Now lets do a RTO with this */ michael@0: r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy, michael@0: SCTP_RTT_FROM_NON_DATA); michael@0: if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) { michael@0: r_net->dest_state |= SCTP_ADDR_REACHABLE; michael@0: sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, michael@0: 0, (void *)r_net, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: if (r_net->dest_state & SCTP_ADDR_PF) { michael@0: r_net->dest_state &= ~SCTP_ADDR_PF; michael@0: stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net); michael@0: } michael@0: if (old_error_counter > 0) { michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net); michael@0: } michael@0: if (r_net == stcb->asoc.primary_destination) { michael@0: if (stcb->asoc.alternate) { michael@0: /* release the alternate, primary is good */ michael@0: sctp_free_remote_addr(stcb->asoc.alternate); michael@0: stcb->asoc.alternate = NULL; michael@0: } michael@0: } michael@0: /* Mobility adaptation */ michael@0: if (req_prim) { michael@0: if ((sctp_is_mobility_feature_on(stcb->sctp_ep, michael@0: SCTP_MOBILITY_BASE) || michael@0: sctp_is_mobility_feature_on(stcb->sctp_ep, michael@0: SCTP_MOBILITY_FASTHANDOFF)) && michael@0: sctp_is_mobility_feature_on(stcb->sctp_ep, michael@0: SCTP_MOBILITY_PRIM_DELETED)) { michael@0: michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER+SCTP_LOC_7); michael@0: if (sctp_is_mobility_feature_on(stcb->sctp_ep, michael@0: SCTP_MOBILITY_FASTHANDOFF)) { michael@0: sctp_assoc_immediate_retrans(stcb, michael@0: stcb->asoc.primary_destination); michael@0: } michael@0: if (sctp_is_mobility_feature_on(stcb->sctp_ep, michael@0: SCTP_MOBILITY_BASE)) { michael@0: sctp_move_chunks_from_net(stcb, michael@0: stcb->asoc.deleted_primary); michael@0: } michael@0: sctp_delete_prim_timer(stcb->sctp_ep, stcb, michael@0: stcb->asoc.deleted_primary); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static int michael@0: sctp_handle_nat_colliding_state(struct sctp_tcb *stcb) michael@0: { michael@0: /* return 0 means we want you to proceed with the abort michael@0: * non-zero means no abort processing michael@0: */ michael@0: struct sctpasochead *head; michael@0: michael@0: if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) { michael@0: /* generate a new vtag and send init */ michael@0: LIST_REMOVE(stcb, sctp_asocs); michael@0: stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); michael@0: head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; michael@0: /* put it in the bucket in the vtag hash of assoc's for the system */ michael@0: LIST_INSERT_HEAD(head, stcb, sctp_asocs); michael@0: sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); michael@0: return (1); michael@0: } michael@0: if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) { michael@0: /* treat like a case where the cookie expired i.e.: michael@0: * - dump current cookie. michael@0: * - generate a new vtag. michael@0: * - resend init. michael@0: */ michael@0: /* generate a new vtag and send init */ michael@0: LIST_REMOVE(stcb, sctp_asocs); michael@0: stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED; michael@0: stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT; michael@0: sctp_stop_all_cookie_timers(stcb); michael@0: sctp_toss_old_cookies(stcb, &stcb->asoc); michael@0: stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1); michael@0: head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; michael@0: /* put it in the bucket in the vtag hash of assoc's for the system */ michael@0: LIST_INSERT_HEAD(head, stcb, sctp_asocs); michael@0: sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); michael@0: return (1); michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: static int michael@0: sctp_handle_nat_missing_state(struct sctp_tcb *stcb, michael@0: struct sctp_nets *net) michael@0: michael@0: { michael@0: /* return 0 means we want you to proceed with the abort michael@0: * non-zero means no abort processing michael@0: */ michael@0: if (stcb->asoc.peer_supports_auth == 0) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n"); michael@0: return (0); michael@0: } michael@0: sctp_asconf_send_nat_state_update(stcb, net); michael@0: return (1); michael@0: } michael@0: michael@0: michael@0: static void michael@0: sctp_handle_abort(struct sctp_abort_chunk *abort, michael@0: struct sctp_tcb *stcb, struct sctp_nets *net) michael@0: { michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: #endif michael@0: uint16_t len; michael@0: uint16_t error; michael@0: michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n"); michael@0: if (stcb == NULL) michael@0: return; michael@0: michael@0: len = ntohs(abort->ch.chunk_length); michael@0: if (len > sizeof (struct sctp_chunkhdr)) { michael@0: /* Need to check the cause codes for our michael@0: * two magic nat aborts which don't kill the assoc michael@0: * necessarily. michael@0: */ michael@0: struct sctp_missing_nat_state *natc; michael@0: michael@0: natc = (struct sctp_missing_nat_state *)(abort + 1); michael@0: error = ntohs(natc->cause); michael@0: if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", michael@0: abort->ch.chunk_flags); michael@0: if (sctp_handle_nat_colliding_state(stcb)) { michael@0: return; michael@0: } michael@0: } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", michael@0: abort->ch.chunk_flags); michael@0: if (sctp_handle_nat_missing_state(stcb, net)) { michael@0: return; michael@0: } michael@0: } michael@0: } else { michael@0: error = 0; michael@0: } michael@0: /* stop any receive timers */ michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_6); michael@0: /* notify user of the abort and clean up... */ michael@0: sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED); michael@0: /* free the tcb */ michael@0: SCTP_STAT_INCR_COUNTER32(sctps_aborted); michael@0: if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || michael@0: (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { michael@0: SCTP_STAT_DECR_GAUGE32(sctps_currestab); michael@0: } michael@0: #ifdef SCTP_ASOCLOG_OF_TSNS michael@0: sctp_print_out_track_log(stcb); michael@0: #endif michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: so = SCTP_INP_SO(stcb->sctp_ep); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: #endif michael@0: stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; michael@0: (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, michael@0: SCTP_FROM_SCTP_INPUT+SCTP_LOC_6); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n"); michael@0: } michael@0: michael@0: static void michael@0: sctp_start_net_timers(struct sctp_tcb *stcb) michael@0: { michael@0: uint32_t cnt_hb_sent; michael@0: struct sctp_nets *net; michael@0: michael@0: cnt_hb_sent = 0; michael@0: TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { michael@0: /* For each network start: michael@0: * 1) A pmtu timer. michael@0: * 2) A HB timer michael@0: * 3) If the dest in unconfirmed send michael@0: * a hb as well if under max_hb_burst have michael@0: * been sent. michael@0: */ michael@0: sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net); michael@0: if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) && michael@0: (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) { michael@0: sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); michael@0: cnt_hb_sent++; michael@0: } michael@0: } michael@0: if (cnt_hb_sent) { michael@0: sctp_chunk_output(stcb->sctp_ep, stcb, michael@0: SCTP_OUTPUT_FROM_COOKIE_ACK, michael@0: SCTP_SO_NOT_LOCKED); michael@0: } michael@0: } michael@0: michael@0: michael@0: static void michael@0: sctp_handle_shutdown(struct sctp_shutdown_chunk *cp, michael@0: struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag) michael@0: { michael@0: struct sctp_association *asoc; michael@0: int some_on_streamwheel; michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: #endif michael@0: michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, michael@0: "sctp_handle_shutdown: handling SHUTDOWN\n"); michael@0: if (stcb == NULL) michael@0: return; michael@0: asoc = &stcb->asoc; michael@0: if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || michael@0: (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { michael@0: return; michael@0: } michael@0: if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) { michael@0: /* Shutdown NOT the expected size */ michael@0: return; michael@0: } else { michael@0: sctp_update_acked(stcb, cp, abort_flag); michael@0: if (*abort_flag) { michael@0: return; michael@0: } michael@0: } michael@0: if (asoc->control_pdapi) { michael@0: /* With a normal shutdown michael@0: * we assume the end of last record. michael@0: */ michael@0: SCTP_INP_READ_LOCK(stcb->sctp_ep); michael@0: asoc->control_pdapi->end_added = 1; michael@0: asoc->control_pdapi->pdapi_aborted = 1; michael@0: asoc->control_pdapi = NULL; michael@0: SCTP_INP_READ_UNLOCK(stcb->sctp_ep); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: so = SCTP_INP_SO(stcb->sctp_ep); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { michael@0: /* assoc was freed while we were unlocked */ michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: return; michael@0: } michael@0: #endif michael@0: sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: } michael@0: /* goto SHUTDOWN_RECEIVED state to block new requests */ michael@0: if (stcb->sctp_socket) { michael@0: if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && michael@0: (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) && michael@0: (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { michael@0: SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED); michael@0: SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); michael@0: /* notify upper layer that peer has initiated a shutdown */ michael@0: sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); michael@0: michael@0: /* reset time */ michael@0: (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); michael@0: } michael@0: } michael@0: if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { michael@0: /* michael@0: * stop the shutdown timer, since we WILL move to michael@0: * SHUTDOWN-ACK-SENT. michael@0: */ michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_8); michael@0: } michael@0: /* Now is there unsent data on a stream somewhere? */ michael@0: some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED); michael@0: michael@0: if (!TAILQ_EMPTY(&asoc->send_queue) || michael@0: !TAILQ_EMPTY(&asoc->sent_queue) || michael@0: some_on_streamwheel) { michael@0: /* By returning we will push more data out */ michael@0: return; michael@0: } else { michael@0: /* no outstanding data to send, so move on... */ michael@0: /* send SHUTDOWN-ACK */ michael@0: /* move to SHUTDOWN-ACK-SENT state */ michael@0: if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || michael@0: (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { michael@0: SCTP_STAT_DECR_GAUGE32(sctps_currestab); michael@0: } michael@0: SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT); michael@0: SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); michael@0: sctp_stop_timers_for_shutdown(stcb); michael@0: sctp_send_shutdown_ack(stcb, net); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, michael@0: stcb, net); michael@0: } michael@0: } michael@0: michael@0: static void michael@0: sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED, michael@0: struct sctp_tcb *stcb, michael@0: struct sctp_nets *net) michael@0: { michael@0: struct sctp_association *asoc; michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: michael@0: so = SCTP_INP_SO(stcb->sctp_ep); michael@0: #endif michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, michael@0: "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n"); michael@0: if (stcb == NULL) michael@0: return; michael@0: michael@0: asoc = &stcb->asoc; michael@0: /* process according to association state */ michael@0: if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || michael@0: (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) { michael@0: /* unexpected SHUTDOWN-ACK... do OOTB handling... */ michael@0: sctp_send_shutdown_complete(stcb, net, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: return; michael@0: } michael@0: if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && michael@0: (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { michael@0: /* unexpected SHUTDOWN-ACK... so ignore... */ michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: return; michael@0: } michael@0: if (asoc->control_pdapi) { michael@0: /* With a normal shutdown michael@0: * we assume the end of last record. michael@0: */ michael@0: SCTP_INP_READ_LOCK(stcb->sctp_ep); michael@0: asoc->control_pdapi->end_added = 1; michael@0: asoc->control_pdapi->pdapi_aborted = 1; michael@0: asoc->control_pdapi = NULL; michael@0: SCTP_INP_READ_UNLOCK(stcb->sctp_ep); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { michael@0: /* assoc was freed while we were unlocked */ michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: return; michael@0: } michael@0: #endif michael@0: sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: } michael@0: #ifdef INVARIANTS michael@0: if (!TAILQ_EMPTY(&asoc->send_queue) || michael@0: !TAILQ_EMPTY(&asoc->sent_queue) || michael@0: !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { michael@0: panic("Queues are not empty when handling SHUTDOWN-ACK"); michael@0: } michael@0: #endif michael@0: /* stop the timer */ michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_9); michael@0: /* send SHUTDOWN-COMPLETE */ michael@0: sctp_send_shutdown_complete(stcb, net, 0); michael@0: /* notify upper layer protocol */ michael@0: if (stcb->sctp_socket) { michael@0: if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || michael@0: (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { michael@0: stcb->sctp_socket->so_snd.sb_cc = 0; michael@0: } michael@0: sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: SCTP_STAT_INCR_COUNTER32(sctps_shutdown); michael@0: /* free the TCB but first save off the ep */ michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: #endif michael@0: (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, michael@0: SCTP_FROM_SCTP_INPUT+SCTP_LOC_10); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: } michael@0: michael@0: /* michael@0: * Skip past the param header and then we will find the chunk that caused the michael@0: * problem. There are two possiblities ASCONF or FWD-TSN other than that and michael@0: * our peer must be broken. michael@0: */ michael@0: static void michael@0: sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr, michael@0: struct sctp_nets *net) michael@0: { michael@0: struct sctp_chunkhdr *chk; michael@0: michael@0: chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr)); michael@0: switch (chk->chunk_type) { michael@0: case SCTP_ASCONF_ACK: michael@0: case SCTP_ASCONF: michael@0: sctp_asconf_cleanup(stcb, net); michael@0: break; michael@0: case SCTP_FORWARD_CUM_TSN: michael@0: stcb->asoc.peer_supports_prsctp = 0; michael@0: break; michael@0: default: michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, michael@0: "Peer does not support chunk type %d(%x)??\n", michael@0: chk->chunk_type, (uint32_t) chk->chunk_type); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * Skip past the param header and then we will find the param that caused the michael@0: * problem. There are a number of param's in a ASCONF OR the prsctp param michael@0: * these will turn of specific features. michael@0: */ michael@0: static void michael@0: sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr) michael@0: { michael@0: struct sctp_paramhdr *pbad; michael@0: michael@0: pbad = phdr + 1; michael@0: switch (ntohs(pbad->param_type)) { michael@0: /* pr-sctp draft */ michael@0: case SCTP_PRSCTP_SUPPORTED: michael@0: stcb->asoc.peer_supports_prsctp = 0; michael@0: break; michael@0: case SCTP_SUPPORTED_CHUNK_EXT: michael@0: break; michael@0: /* draft-ietf-tsvwg-addip-sctp */ michael@0: case SCTP_HAS_NAT_SUPPORT: michael@0: stcb->asoc.peer_supports_nat = 0; michael@0: break; michael@0: case SCTP_ADD_IP_ADDRESS: michael@0: case SCTP_DEL_IP_ADDRESS: michael@0: case SCTP_SET_PRIM_ADDR: michael@0: stcb->asoc.peer_supports_asconf = 0; michael@0: break; michael@0: case SCTP_SUCCESS_REPORT: michael@0: case SCTP_ERROR_CAUSE_IND: michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n"); michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, michael@0: "Turning off ASCONF to this strange peer\n"); michael@0: stcb->asoc.peer_supports_asconf = 0; michael@0: break; michael@0: default: michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, michael@0: "Peer does not support param type %d(%x)??\n", michael@0: pbad->param_type, (uint32_t) pbad->param_type); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: static int michael@0: sctp_handle_error(struct sctp_chunkhdr *ch, michael@0: struct sctp_tcb *stcb, struct sctp_nets *net) michael@0: { michael@0: int chklen; michael@0: struct sctp_paramhdr *phdr; michael@0: uint16_t error, error_type; michael@0: uint16_t error_len; michael@0: struct sctp_association *asoc; michael@0: int adjust; michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: #endif michael@0: michael@0: /* parse through all of the errors and process */ michael@0: asoc = &stcb->asoc; michael@0: phdr = (struct sctp_paramhdr *)((caddr_t)ch + michael@0: sizeof(struct sctp_chunkhdr)); michael@0: chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr); michael@0: error = 0; michael@0: while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) { michael@0: /* Process an Error Cause */ michael@0: error_type = ntohs(phdr->param_type); michael@0: error_len = ntohs(phdr->param_length); michael@0: if ((error_len > chklen) || (error_len == 0)) { michael@0: /* invalid param length for this param */ michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n", michael@0: chklen, error_len); michael@0: return (0); michael@0: } michael@0: if (error == 0) { michael@0: /* report the first error cause */ michael@0: error = error_type; michael@0: } michael@0: switch (error_type) { michael@0: case SCTP_CAUSE_INVALID_STREAM: michael@0: case SCTP_CAUSE_MISSING_PARAM: michael@0: case SCTP_CAUSE_INVALID_PARAM: michael@0: case SCTP_CAUSE_NO_USER_DATA: michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n", michael@0: error_type); michael@0: break; michael@0: case SCTP_CAUSE_NAT_COLLIDING_STATE: michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n", michael@0: ch->chunk_flags); michael@0: if (sctp_handle_nat_colliding_state(stcb)) { michael@0: return (0); michael@0: } michael@0: break; michael@0: case SCTP_CAUSE_NAT_MISSING_STATE: michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n", michael@0: ch->chunk_flags); michael@0: if (sctp_handle_nat_missing_state(stcb, net)) { michael@0: return (0); michael@0: } michael@0: break; michael@0: case SCTP_CAUSE_STALE_COOKIE: michael@0: /* michael@0: * We only act if we have echoed a cookie and are michael@0: * waiting. michael@0: */ michael@0: if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { michael@0: int *p; michael@0: michael@0: p = (int *)((caddr_t)phdr + sizeof(*phdr)); michael@0: /* Save the time doubled */ michael@0: asoc->cookie_preserve_req = ntohl(*p) << 1; michael@0: asoc->stale_cookie_count++; michael@0: if (asoc->stale_cookie_count > michael@0: asoc->max_init_times) { michael@0: sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED); michael@0: /* now free the asoc */ michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: so = SCTP_INP_SO(stcb->sctp_ep); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: #endif michael@0: (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, michael@0: SCTP_FROM_SCTP_INPUT+SCTP_LOC_11); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: return (-1); michael@0: } michael@0: /* blast back to INIT state */ michael@0: sctp_toss_old_cookies(stcb, &stcb->asoc); michael@0: asoc->state &= ~SCTP_STATE_COOKIE_ECHOED; michael@0: asoc->state |= SCTP_STATE_COOKIE_WAIT; michael@0: sctp_stop_all_cookie_timers(stcb); michael@0: sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: break; michael@0: case SCTP_CAUSE_UNRESOLVABLE_ADDR: michael@0: /* michael@0: * Nothing we can do here, we don't do hostname michael@0: * addresses so if the peer does not like my IPv6 michael@0: * (or IPv4 for that matter) it does not matter. If michael@0: * they don't support that type of address, they can michael@0: * NOT possibly get that packet type... i.e. with no michael@0: * IPv6 you can't recieve a IPv6 packet. so we can michael@0: * safely ignore this one. If we ever added support michael@0: * for HOSTNAME Addresses, then we would need to do michael@0: * something here. michael@0: */ michael@0: break; michael@0: case SCTP_CAUSE_UNRECOG_CHUNK: michael@0: sctp_process_unrecog_chunk(stcb, phdr, net); michael@0: break; michael@0: case SCTP_CAUSE_UNRECOG_PARAM: michael@0: sctp_process_unrecog_param(stcb, phdr); michael@0: break; michael@0: case SCTP_CAUSE_COOKIE_IN_SHUTDOWN: michael@0: /* michael@0: * We ignore this since the timer will drive out a michael@0: * new cookie anyway and there timer will drive us michael@0: * to send a SHUTDOWN_COMPLETE. We can't send one michael@0: * here since we don't have their tag. michael@0: */ michael@0: break; michael@0: case SCTP_CAUSE_DELETING_LAST_ADDR: michael@0: case SCTP_CAUSE_RESOURCE_SHORTAGE: michael@0: case SCTP_CAUSE_DELETING_SRC_ADDR: michael@0: /* michael@0: * We should NOT get these here, but in a michael@0: * ASCONF-ACK. michael@0: */ michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n", michael@0: error_type); michael@0: break; michael@0: case SCTP_CAUSE_OUT_OF_RESC: michael@0: /* michael@0: * And what, pray tell do we do with the fact that michael@0: * the peer is out of resources? Not really sure we michael@0: * could do anything but abort. I suspect this michael@0: * should have came WITH an abort instead of in a michael@0: * OP-ERROR. michael@0: */ michael@0: break; michael@0: default: michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n", michael@0: error_type); michael@0: break; michael@0: } michael@0: adjust = SCTP_SIZE32(error_len); michael@0: chklen -= adjust; michael@0: phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust); michael@0: } michael@0: sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED); michael@0: return (0); michael@0: } michael@0: michael@0: static int michael@0: sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, michael@0: struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh, michael@0: struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb, michael@0: struct sctp_nets *net, int *abort_no_unlock, michael@0: #if defined(__FreeBSD__) michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: #endif michael@0: uint32_t vrf_id) michael@0: { michael@0: struct sctp_init_ack *init_ack; michael@0: struct mbuf *op_err; michael@0: michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, michael@0: "sctp_handle_init_ack: handling INIT-ACK\n"); michael@0: michael@0: if (stcb == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, michael@0: "sctp_handle_init_ack: TCB is null\n"); michael@0: return (-1); michael@0: } michael@0: if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) { michael@0: /* Invalid length */ michael@0: op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); michael@0: sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, michael@0: src, dst, sh, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, net->port); michael@0: *abort_no_unlock = 1; michael@0: return (-1); michael@0: } michael@0: init_ack = &cp->init; michael@0: /* validate parameters */ michael@0: if (init_ack->initiate_tag == 0) { michael@0: /* protocol error... send an abort */ michael@0: op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); michael@0: sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, michael@0: src, dst, sh, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, net->port); michael@0: *abort_no_unlock = 1; michael@0: return (-1); michael@0: } michael@0: if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) { michael@0: /* protocol error... send an abort */ michael@0: op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); michael@0: sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, michael@0: src, dst, sh, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, net->port); michael@0: *abort_no_unlock = 1; michael@0: return (-1); michael@0: } michael@0: if (init_ack->num_inbound_streams == 0) { michael@0: /* protocol error... send an abort */ michael@0: op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); michael@0: sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, michael@0: src, dst, sh, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, net->port); michael@0: *abort_no_unlock = 1; michael@0: return (-1); michael@0: } michael@0: if (init_ack->num_outbound_streams == 0) { michael@0: /* protocol error... send an abort */ michael@0: op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM); michael@0: sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, michael@0: src, dst, sh, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, net->port); michael@0: *abort_no_unlock = 1; michael@0: return (-1); michael@0: } michael@0: /* process according to association state... */ michael@0: switch (stcb->asoc.state & SCTP_STATE_MASK) { michael@0: case SCTP_STATE_COOKIE_WAIT: michael@0: /* this is the expected state for this chunk */ michael@0: /* process the INIT-ACK parameters */ michael@0: if (stcb->asoc.primary_destination->dest_state & michael@0: SCTP_ADDR_UNCONFIRMED) { michael@0: /* michael@0: * The primary is where we sent the INIT, we can michael@0: * always consider it confirmed when the INIT-ACK is michael@0: * returned. Do this before we load addresses michael@0: * though. michael@0: */ michael@0: stcb->asoc.primary_destination->dest_state &= michael@0: ~SCTP_ADDR_UNCONFIRMED; michael@0: sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, michael@0: stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb, michael@0: net, abort_no_unlock, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id) < 0) { michael@0: /* error in parsing parameters */ michael@0: return (-1); michael@0: } michael@0: /* update our state */ michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n"); michael@0: SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED); michael@0: michael@0: /* reset the RTO calc */ michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { michael@0: sctp_misc_ints(SCTP_THRESHOLD_CLEAR, michael@0: stcb->asoc.overall_error_count, michael@0: 0, michael@0: SCTP_FROM_SCTP_INPUT, michael@0: __LINE__); michael@0: } michael@0: stcb->asoc.overall_error_count = 0; michael@0: (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); michael@0: /* michael@0: * collapse the init timer back in case of a exponential michael@0: * backoff michael@0: */ michael@0: sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, michael@0: stcb, net); michael@0: /* michael@0: * the send at the end of the inbound data processing will michael@0: * cause the cookie to be sent michael@0: */ michael@0: break; michael@0: case SCTP_STATE_SHUTDOWN_SENT: michael@0: /* incorrect state... discard */ michael@0: break; michael@0: case SCTP_STATE_COOKIE_ECHOED: michael@0: /* incorrect state... discard */ michael@0: break; michael@0: case SCTP_STATE_OPEN: michael@0: /* incorrect state... discard */ michael@0: break; michael@0: case SCTP_STATE_EMPTY: michael@0: case SCTP_STATE_INUSE: michael@0: default: michael@0: /* incorrect state... discard */ michael@0: return (-1); michael@0: break; michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n"); michael@0: return (0); michael@0: } michael@0: michael@0: static struct sctp_tcb * michael@0: sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, michael@0: struct sockaddr *src, struct sockaddr *dst, michael@0: struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, michael@0: struct sctp_inpcb *inp, struct sctp_nets **netp, michael@0: struct sockaddr *init_src, int *notification, michael@0: int auth_skipped, uint32_t auth_offset, uint32_t auth_len, michael@0: #if defined(__FreeBSD__) michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: #endif michael@0: uint32_t vrf_id, uint16_t port); michael@0: michael@0: michael@0: /* michael@0: * handle a state cookie for an existing association m: input packet mbuf michael@0: * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a michael@0: * "split" mbuf and the cookie signature does not exist offset: offset into michael@0: * mbuf to the cookie-echo chunk michael@0: */ michael@0: static struct sctp_tcb * michael@0: sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset, michael@0: struct sockaddr *src, struct sockaddr *dst, michael@0: struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, michael@0: struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp, michael@0: struct sockaddr *init_src, int *notification, michael@0: int auth_skipped, uint32_t auth_offset, uint32_t auth_len, michael@0: #if defined(__FreeBSD__) michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: #endif michael@0: uint32_t vrf_id, uint16_t port) michael@0: { michael@0: struct sctp_association *asoc; michael@0: struct sctp_init_chunk *init_cp, init_buf; michael@0: struct sctp_init_ack_chunk *initack_cp, initack_buf; michael@0: struct sctp_nets *net; michael@0: struct mbuf *op_err; michael@0: struct sctp_paramhdr *ph; michael@0: int init_offset, initack_offset, i; michael@0: int retval; michael@0: int spec_flag = 0; michael@0: uint32_t how_indx; michael@0: michael@0: net = *netp; michael@0: /* I know that the TCB is non-NULL from the caller */ michael@0: asoc = &stcb->asoc; michael@0: for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) { michael@0: if (asoc->cookie_how[how_indx] == 0) michael@0: break; michael@0: } michael@0: if (how_indx < sizeof(asoc->cookie_how)) { michael@0: asoc->cookie_how[how_indx] = 1; michael@0: } michael@0: if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { michael@0: /* SHUTDOWN came in after sending INIT-ACK */ michael@0: sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination); michael@0: op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), michael@0: 0, M_NOWAIT, 1, MT_DATA); michael@0: if (op_err == NULL) { michael@0: /* FOOBAR */ michael@0: return (NULL); michael@0: } michael@0: /* Set the len */ michael@0: SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); michael@0: ph = mtod(op_err, struct sctp_paramhdr *); michael@0: ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN); michael@0: ph->param_length = htons(sizeof(struct sctp_paramhdr)); michael@0: sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, net->port); michael@0: if (how_indx < sizeof(asoc->cookie_how)) michael@0: asoc->cookie_how[how_indx] = 2; michael@0: return (NULL); michael@0: } michael@0: /* michael@0: * find and validate the INIT chunk in the cookie (peer's info) the michael@0: * INIT should start after the cookie-echo header struct (chunk michael@0: * header, state cookie header struct) michael@0: */ michael@0: init_offset = offset += sizeof(struct sctp_cookie_echo_chunk); michael@0: michael@0: init_cp = (struct sctp_init_chunk *) michael@0: sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), michael@0: (uint8_t *) & init_buf); michael@0: if (init_cp == NULL) { michael@0: /* could not pull a INIT chunk in cookie */ michael@0: return (NULL); michael@0: } michael@0: if (init_cp->ch.chunk_type != SCTP_INITIATION) { michael@0: return (NULL); michael@0: } michael@0: /* michael@0: * find and validate the INIT-ACK chunk in the cookie (my info) the michael@0: * INIT-ACK follows the INIT chunk michael@0: */ michael@0: initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length)); michael@0: initack_cp = (struct sctp_init_ack_chunk *) michael@0: sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), michael@0: (uint8_t *) & initack_buf); michael@0: if (initack_cp == NULL) { michael@0: /* could not pull INIT-ACK chunk in cookie */ michael@0: return (NULL); michael@0: } michael@0: if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { michael@0: return (NULL); michael@0: } michael@0: if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && michael@0: (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) { michael@0: /* michael@0: * case D in Section 5.2.4 Table 2: MMAA process accordingly michael@0: * to get into the OPEN state michael@0: */ michael@0: if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { michael@0: /*- michael@0: * Opps, this means that we somehow generated two vtag's michael@0: * the same. I.e. we did: michael@0: * Us Peer michael@0: * <---INIT(tag=a)------ michael@0: * ----INIT-ACK(tag=t)--> michael@0: * ----INIT(tag=t)------> *1 michael@0: * <---INIT-ACK(tag=a)--- michael@0: * <----CE(tag=t)------------- *2 michael@0: * michael@0: * At point *1 we should be generating a different michael@0: * tag t'. Which means we would throw away the CE and send michael@0: * ours instead. Basically this is case C (throw away side). michael@0: */ michael@0: if (how_indx < sizeof(asoc->cookie_how)) michael@0: asoc->cookie_how[how_indx] = 17; michael@0: return (NULL); michael@0: michael@0: } michael@0: switch (SCTP_GET_STATE(asoc)) { michael@0: case SCTP_STATE_COOKIE_WAIT: michael@0: case SCTP_STATE_COOKIE_ECHOED: michael@0: /* michael@0: * INIT was sent but got a COOKIE_ECHO with the michael@0: * correct tags... just accept it...but we must michael@0: * process the init so that we can make sure we michael@0: * have the right seq no's. michael@0: */ michael@0: /* First we must process the INIT !! */ michael@0: retval = sctp_process_init(init_cp, stcb); michael@0: if (retval < 0) { michael@0: if (how_indx < sizeof(asoc->cookie_how)) michael@0: asoc->cookie_how[how_indx] = 3; michael@0: return (NULL); michael@0: } michael@0: /* we have already processed the INIT so no problem */ michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, michael@0: net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_12); michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_13); michael@0: /* update current state */ michael@0: if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) michael@0: SCTP_STAT_INCR_COUNTER32(sctps_activeestab); michael@0: else michael@0: SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); michael@0: michael@0: SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); michael@0: if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, michael@0: stcb->sctp_ep, stcb, asoc->primary_destination); michael@0: } michael@0: SCTP_STAT_INCR_GAUGE32(sctps_currestab); michael@0: sctp_stop_all_cookie_timers(stcb); michael@0: if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || michael@0: (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && michael@0: (inp->sctp_socket->so_qlimit == 0) michael@0: ) { michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: #endif michael@0: /* michael@0: * Here is where collision would go if we michael@0: * did a connect() and instead got a michael@0: * init/init-ack/cookie done before the michael@0: * init-ack came back.. michael@0: */ michael@0: stcb->sctp_ep->sctp_flags |= michael@0: SCTP_PCB_FLAGS_CONNECTED; michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: so = SCTP_INP_SO(stcb->sctp_ep); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: return (NULL); michael@0: } michael@0: #endif michael@0: soisconnected(stcb->sctp_socket); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: } michael@0: /* notify upper layer */ michael@0: *notification = SCTP_NOTIFY_ASSOC_UP; michael@0: /* michael@0: * since we did not send a HB make sure we michael@0: * don't double things michael@0: */ michael@0: net->hb_responded = 1; michael@0: net->RTO = sctp_calculate_rto(stcb, asoc, net, michael@0: &cookie->time_entered, michael@0: sctp_align_unsafe_makecopy, michael@0: SCTP_RTT_FROM_NON_DATA); michael@0: michael@0: if (stcb->asoc.sctp_autoclose_ticks && michael@0: (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, michael@0: inp, stcb, NULL); michael@0: } michael@0: break; michael@0: default: michael@0: /* michael@0: * we're in the OPEN state (or beyond), so michael@0: * peer must have simply lost the COOKIE-ACK michael@0: */ michael@0: break; michael@0: } /* end switch */ michael@0: sctp_stop_all_cookie_timers(stcb); michael@0: /* michael@0: * We ignore the return code here.. not sure if we should michael@0: * somehow abort.. but we do have an existing asoc. This michael@0: * really should not fail. michael@0: */ michael@0: if (sctp_load_addresses_from_init(stcb, m, michael@0: init_offset + sizeof(struct sctp_init_chunk), michael@0: initack_offset, src, dst, init_src)) { michael@0: if (how_indx < sizeof(asoc->cookie_how)) michael@0: asoc->cookie_how[how_indx] = 4; michael@0: return (NULL); michael@0: } michael@0: /* respond with a COOKIE-ACK */ michael@0: sctp_toss_old_cookies(stcb, asoc); michael@0: sctp_send_cookie_ack(stcb); michael@0: if (how_indx < sizeof(asoc->cookie_how)) michael@0: asoc->cookie_how[how_indx] = 5; michael@0: return (stcb); michael@0: } michael@0: michael@0: if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && michael@0: ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag && michael@0: cookie->tie_tag_my_vtag == 0 && michael@0: cookie->tie_tag_peer_vtag == 0) { michael@0: /* michael@0: * case C in Section 5.2.4 Table 2: XMOO silently discard michael@0: */ michael@0: if (how_indx < sizeof(asoc->cookie_how)) michael@0: asoc->cookie_how[how_indx] = 6; michael@0: return (NULL); michael@0: } michael@0: /* If nat support, and the below and stcb is established, michael@0: * send back a ABORT(colliding state) if we are established. michael@0: */ michael@0: if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) && michael@0: (asoc->peer_supports_nat) && michael@0: ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && michael@0: ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || michael@0: (asoc->peer_vtag == 0)))) { michael@0: /* Special case - Peer's support nat. We may have michael@0: * two init's that we gave out the same tag on since michael@0: * one was not established.. i.e. we get INIT from host-1 michael@0: * behind the nat and we respond tag-a, we get a INIT from michael@0: * host-2 behind the nat and we get tag-a again. Then we michael@0: * bring up host-1 (or 2's) assoc, Then comes the cookie michael@0: * from hsot-2 (or 1). Now we have colliding state. We must michael@0: * send an abort here with colliding state indication. michael@0: */ michael@0: op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), michael@0: 0, M_NOWAIT, 1, MT_DATA); michael@0: if (op_err == NULL) { michael@0: /* FOOBAR */ michael@0: return (NULL); michael@0: } michael@0: /* pre-reserve some space */ michael@0: #ifdef INET6 michael@0: SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr)); michael@0: #else michael@0: SCTP_BUF_RESV_UF(op_err, sizeof(struct ip)); michael@0: #endif michael@0: SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr)); michael@0: SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); michael@0: /* Set the len */ michael@0: SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); michael@0: ph = mtod(op_err, struct sctp_paramhdr *); michael@0: ph->param_type = htons(SCTP_CAUSE_NAT_COLLIDING_STATE); michael@0: ph->param_length = htons(sizeof(struct sctp_paramhdr)); michael@0: sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: return (NULL); michael@0: } michael@0: if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) && michael@0: ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) || michael@0: (asoc->peer_vtag == 0))) { michael@0: /* michael@0: * case B in Section 5.2.4 Table 2: MXAA or MOAA my info michael@0: * should be ok, re-accept peer info michael@0: */ michael@0: if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) { michael@0: /* Extension of case C. michael@0: * If we hit this, then the random number michael@0: * generator returned the same vtag when we michael@0: * first sent our INIT-ACK and when we later sent michael@0: * our INIT. The side with the seq numbers that are michael@0: * different will be the one that normnally would michael@0: * have hit case C. This in effect "extends" our vtags michael@0: * in this collision case to be 64 bits. The same collision michael@0: * could occur aka you get both vtag and seq number the michael@0: * same twice in a row.. but is much less likely. If it michael@0: * did happen then we would proceed through and bring michael@0: * up the assoc.. we may end up with the wrong stream michael@0: * setup however.. which would be bad.. but there is michael@0: * no way to tell.. until we send on a stream that does michael@0: * not exist :-) michael@0: */ michael@0: if (how_indx < sizeof(asoc->cookie_how)) michael@0: asoc->cookie_how[how_indx] = 7; michael@0: michael@0: return (NULL); michael@0: } michael@0: if (how_indx < sizeof(asoc->cookie_how)) michael@0: asoc->cookie_how[how_indx] = 8; michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_14); michael@0: sctp_stop_all_cookie_timers(stcb); michael@0: /* michael@0: * since we did not send a HB make sure we don't double michael@0: * things michael@0: */ michael@0: net->hb_responded = 1; michael@0: if (stcb->asoc.sctp_autoclose_ticks && michael@0: sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, michael@0: NULL); michael@0: } michael@0: asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); michael@0: asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); michael@0: michael@0: if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) { michael@0: /* Ok the peer probably discarded our michael@0: * data (if we echoed a cookie+data). So anything michael@0: * on the sent_queue should be marked for michael@0: * retransmit, we may not get something to michael@0: * kick us so it COULD still take a timeout michael@0: * to move these.. but it can't hurt to mark them. michael@0: */ michael@0: struct sctp_tmit_chunk *chk; michael@0: TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { michael@0: if (chk->sent < SCTP_DATAGRAM_RESEND) { michael@0: chk->sent = SCTP_DATAGRAM_RESEND; michael@0: sctp_flight_size_decrease(chk); michael@0: sctp_total_flight_decrease(stcb, chk); michael@0: sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); michael@0: spec_flag++; michael@0: } michael@0: } michael@0: michael@0: } michael@0: /* process the INIT info (peer's info) */ michael@0: retval = sctp_process_init(init_cp, stcb); michael@0: if (retval < 0) { michael@0: if (how_indx < sizeof(asoc->cookie_how)) michael@0: asoc->cookie_how[how_indx] = 9; michael@0: return (NULL); michael@0: } michael@0: if (sctp_load_addresses_from_init(stcb, m, michael@0: init_offset + sizeof(struct sctp_init_chunk), michael@0: initack_offset, src, dst, init_src)) { michael@0: if (how_indx < sizeof(asoc->cookie_how)) michael@0: asoc->cookie_how[how_indx] = 10; michael@0: return (NULL); michael@0: } michael@0: if ((asoc->state & SCTP_STATE_COOKIE_WAIT) || michael@0: (asoc->state & SCTP_STATE_COOKIE_ECHOED)) { michael@0: *notification = SCTP_NOTIFY_ASSOC_UP; michael@0: michael@0: if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || michael@0: (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && michael@0: (inp->sctp_socket->so_qlimit == 0)) { michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: #endif michael@0: stcb->sctp_ep->sctp_flags |= michael@0: SCTP_PCB_FLAGS_CONNECTED; michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: so = SCTP_INP_SO(stcb->sctp_ep); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: return (NULL); michael@0: } michael@0: #endif michael@0: soisconnected(stcb->sctp_socket); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: } michael@0: if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) michael@0: SCTP_STAT_INCR_COUNTER32(sctps_activeestab); michael@0: else michael@0: SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); michael@0: SCTP_STAT_INCR_GAUGE32(sctps_currestab); michael@0: } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { michael@0: SCTP_STAT_INCR_COUNTER32(sctps_restartestab); michael@0: } else { michael@0: SCTP_STAT_INCR_COUNTER32(sctps_collisionestab); michael@0: } michael@0: SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); michael@0: if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, michael@0: stcb->sctp_ep, stcb, asoc->primary_destination); michael@0: } michael@0: sctp_stop_all_cookie_timers(stcb); michael@0: sctp_toss_old_cookies(stcb, asoc); michael@0: sctp_send_cookie_ack(stcb); michael@0: if (spec_flag) { michael@0: /* only if we have retrans set do we do this. What michael@0: * this call does is get only the COOKIE-ACK out michael@0: * and then when we return the normal call to michael@0: * sctp_chunk_output will get the retrans out michael@0: * behind this. michael@0: */ michael@0: sctp_chunk_output(inp,stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: if (how_indx < sizeof(asoc->cookie_how)) michael@0: asoc->cookie_how[how_indx] = 11; michael@0: michael@0: return (stcb); michael@0: } michael@0: if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag && michael@0: ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) && michael@0: cookie->tie_tag_my_vtag == asoc->my_vtag_nonce && michael@0: cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce && michael@0: cookie->tie_tag_peer_vtag != 0) { michael@0: struct sctpasochead *head; michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: #endif michael@0: michael@0: if (asoc->peer_supports_nat) { michael@0: /* This is a gross gross hack. michael@0: * Just call the cookie_new code since we michael@0: * are allowing a duplicate association. michael@0: * I hope this works... michael@0: */ michael@0: return (sctp_process_cookie_new(m, iphlen, offset, src, dst, michael@0: sh, cookie, cookie_len, michael@0: inp, netp, init_src,notification, michael@0: auth_skipped, auth_offset, auth_len, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port)); michael@0: } michael@0: /* michael@0: * case A in Section 5.2.4 Table 2: XXMM (peer restarted) michael@0: */ michael@0: /* temp code */ michael@0: if (how_indx < sizeof(asoc->cookie_how)) michael@0: asoc->cookie_how[how_indx] = 12; michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_15); michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_16); michael@0: michael@0: /* notify upper layer */ michael@0: *notification = SCTP_NOTIFY_ASSOC_RESTART; michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) && michael@0: (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) && michael@0: (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) { michael@0: SCTP_STAT_INCR_GAUGE32(sctps_currestab); michael@0: } michael@0: if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) { michael@0: SCTP_STAT_INCR_GAUGE32(sctps_restartestab); michael@0: } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { michael@0: SCTP_STAT_INCR_GAUGE32(sctps_collisionestab); michael@0: } michael@0: if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { michael@0: SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, michael@0: stcb->sctp_ep, stcb, asoc->primary_destination); michael@0: michael@0: } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) { michael@0: /* move to OPEN state, if not in SHUTDOWN_SENT */ michael@0: SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); michael@0: } michael@0: asoc->pre_open_streams = michael@0: ntohs(initack_cp->init.num_outbound_streams); michael@0: asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); michael@0: asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; michael@0: asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; michael@0: michael@0: asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; michael@0: michael@0: asoc->str_reset_seq_in = asoc->init_seq_number; michael@0: michael@0: asoc->advanced_peer_ack_point = asoc->last_acked_seq; michael@0: if (asoc->mapping_array) { michael@0: memset(asoc->mapping_array, 0, michael@0: asoc->mapping_array_size); michael@0: } michael@0: if (asoc->nr_mapping_array) { michael@0: memset(asoc->nr_mapping_array, 0, michael@0: asoc->mapping_array_size); michael@0: } michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: so = SCTP_INP_SO(stcb->sctp_ep); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: #endif michael@0: SCTP_INP_INFO_WLOCK(); michael@0: SCTP_INP_WLOCK(stcb->sctp_ep); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: /* send up all the data */ michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: michael@0: sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED); michael@0: for (i = 0; i < stcb->asoc.streamoutcnt; i++) { michael@0: stcb->asoc.strmout[i].chunks_on_queues = 0; michael@0: stcb->asoc.strmout[i].stream_no = i; michael@0: stcb->asoc.strmout[i].next_sequence_send = 0; michael@0: stcb->asoc.strmout[i].last_msg_incomplete = 0; michael@0: } michael@0: /* process the INIT-ACK info (my info) */ michael@0: asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); michael@0: asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); michael@0: michael@0: /* pull from vtag hash */ michael@0: LIST_REMOVE(stcb, sctp_asocs); michael@0: /* re-insert to new vtag position */ michael@0: head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, michael@0: SCTP_BASE_INFO(hashasocmark))]; michael@0: /* michael@0: * put it in the bucket in the vtag hash of assoc's for the michael@0: * system michael@0: */ michael@0: LIST_INSERT_HEAD(head, stcb, sctp_asocs); michael@0: michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: SCTP_INP_WUNLOCK(stcb->sctp_ep); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: asoc->total_flight = 0; michael@0: asoc->total_flight_count = 0; michael@0: /* process the INIT info (peer's info) */ michael@0: retval = sctp_process_init(init_cp, stcb); michael@0: if (retval < 0) { michael@0: if (how_indx < sizeof(asoc->cookie_how)) michael@0: asoc->cookie_how[how_indx] = 13; michael@0: michael@0: return (NULL); michael@0: } michael@0: /* michael@0: * since we did not send a HB make sure we don't double michael@0: * things michael@0: */ michael@0: net->hb_responded = 1; michael@0: michael@0: if (sctp_load_addresses_from_init(stcb, m, michael@0: init_offset + sizeof(struct sctp_init_chunk), michael@0: initack_offset, src, dst, init_src)) { michael@0: if (how_indx < sizeof(asoc->cookie_how)) michael@0: asoc->cookie_how[how_indx] = 14; michael@0: michael@0: return (NULL); michael@0: } michael@0: /* respond with a COOKIE-ACK */ michael@0: sctp_stop_all_cookie_timers(stcb); michael@0: sctp_toss_old_cookies(stcb, asoc); michael@0: sctp_send_cookie_ack(stcb); michael@0: if (how_indx < sizeof(asoc->cookie_how)) michael@0: asoc->cookie_how[how_indx] = 15; michael@0: michael@0: return (stcb); michael@0: } michael@0: if (how_indx < sizeof(asoc->cookie_how)) michael@0: asoc->cookie_how[how_indx] = 16; michael@0: /* all other cases... */ michael@0: return (NULL); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * handle a state cookie for a new association m: input packet mbuf chain-- michael@0: * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf michael@0: * and the cookie signature does not exist offset: offset into mbuf to the michael@0: * cookie-echo chunk length: length of the cookie chunk to: where the init michael@0: * was from returns a new TCB michael@0: */ michael@0: static struct sctp_tcb * michael@0: sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset, michael@0: struct sockaddr *src, struct sockaddr *dst, michael@0: struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len, michael@0: struct sctp_inpcb *inp, struct sctp_nets **netp, michael@0: struct sockaddr *init_src, int *notification, michael@0: int auth_skipped, uint32_t auth_offset, uint32_t auth_len, michael@0: #if defined(__FreeBSD__) michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: #endif michael@0: uint32_t vrf_id, uint16_t port) michael@0: { michael@0: struct sctp_tcb *stcb; michael@0: struct sctp_init_chunk *init_cp, init_buf; michael@0: struct sctp_init_ack_chunk *initack_cp, initack_buf; michael@0: struct sockaddr_storage sa_store; michael@0: struct sockaddr *initack_src = (struct sockaddr *)&sa_store; michael@0: struct sctp_association *asoc; michael@0: int init_offset, initack_offset, initack_limit; michael@0: int retval; michael@0: int error = 0; michael@0: uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE]; michael@0: #ifdef INET michael@0: struct sockaddr_in *sin; michael@0: #endif michael@0: #ifdef INET6 michael@0: struct sockaddr_in6 *sin6; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: struct sockaddr_conn *sconn; michael@0: #endif michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: michael@0: so = SCTP_INP_SO(inp); michael@0: #endif michael@0: michael@0: /* michael@0: * find and validate the INIT chunk in the cookie (peer's info) the michael@0: * INIT should start after the cookie-echo header struct (chunk michael@0: * header, state cookie header struct) michael@0: */ michael@0: init_offset = offset + sizeof(struct sctp_cookie_echo_chunk); michael@0: init_cp = (struct sctp_init_chunk *) michael@0: sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk), michael@0: (uint8_t *) & init_buf); michael@0: if (init_cp == NULL) { michael@0: /* could not pull a INIT chunk in cookie */ michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, michael@0: "process_cookie_new: could not pull INIT chunk hdr\n"); michael@0: return (NULL); michael@0: } michael@0: if (init_cp->ch.chunk_type != SCTP_INITIATION) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n"); michael@0: return (NULL); michael@0: } michael@0: initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length)); michael@0: /* michael@0: * find and validate the INIT-ACK chunk in the cookie (my info) the michael@0: * INIT-ACK follows the INIT chunk michael@0: */ michael@0: initack_cp = (struct sctp_init_ack_chunk *) michael@0: sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk), michael@0: (uint8_t *) & initack_buf); michael@0: if (initack_cp == NULL) { michael@0: /* could not pull INIT-ACK chunk in cookie */ michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n"); michael@0: return (NULL); michael@0: } michael@0: if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) { michael@0: return (NULL); michael@0: } michael@0: /* michael@0: * NOTE: We can't use the INIT_ACK's chk_length to determine the michael@0: * "initack_limit" value. This is because the chk_length field michael@0: * includes the length of the cookie, but the cookie is omitted when michael@0: * the INIT and INIT_ACK are tacked onto the cookie... michael@0: */ michael@0: initack_limit = offset + cookie_len; michael@0: michael@0: /* michael@0: * now that we know the INIT/INIT-ACK are in place, create a new TCB michael@0: * and popluate michael@0: */ michael@0: michael@0: /* michael@0: * Here we do a trick, we set in NULL for the proc/thread argument. We michael@0: * do this since in effect we only use the p argument when michael@0: * the socket is unbound and we must do an implicit bind. michael@0: * Since we are getting a cookie, we cannot be unbound. michael@0: */ michael@0: stcb = sctp_aloc_assoc(inp, init_src, &error, michael@0: ntohl(initack_cp->init.initiate_tag), vrf_id, michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 michael@0: (struct thread *)NULL michael@0: #elif defined(__Windows__) michael@0: (PKTHREAD)NULL michael@0: #else michael@0: (struct proc *)NULL michael@0: #endif michael@0: ); michael@0: if (stcb == NULL) { michael@0: struct mbuf *op_err; michael@0: michael@0: /* memory problem? */ michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, michael@0: "process_cookie_new: no room for another TCB!\n"); michael@0: op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); michael@0: michael@0: sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, michael@0: src, dst, sh, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: return (NULL); michael@0: } michael@0: /* get the correct sctp_nets */ michael@0: if (netp) michael@0: *netp = sctp_findnet(stcb, init_src); michael@0: michael@0: asoc = &stcb->asoc; michael@0: /* get scope variables out of cookie */ michael@0: asoc->scope.ipv4_local_scope = cookie->ipv4_scope; michael@0: asoc->scope.site_scope = cookie->site_scope; michael@0: asoc->scope.local_scope = cookie->local_scope; michael@0: asoc->scope.loopback_scope = cookie->loopback_scope; michael@0: michael@0: #if defined(__Userspace__) michael@0: if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) || michael@0: (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal) || michael@0: (asoc->scope.conn_addr_legal != cookie->conn_addr_legal)) { michael@0: #else michael@0: if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) || michael@0: (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) { michael@0: #endif michael@0: struct mbuf *op_err; michael@0: michael@0: /* michael@0: * Houston we have a problem. The EP changed while the michael@0: * cookie was in flight. Only recourse is to abort the michael@0: * association. michael@0: */ michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); michael@0: sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen, michael@0: src, dst, sh, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: #endif michael@0: (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, michael@0: SCTP_FROM_SCTP_INPUT+SCTP_LOC_16); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: return (NULL); michael@0: } michael@0: /* process the INIT-ACK info (my info) */ michael@0: asoc->my_vtag = ntohl(initack_cp->init.initiate_tag); michael@0: asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd); michael@0: asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams); michael@0: asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn); michael@0: asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number; michael@0: asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; michael@0: asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1; michael@0: asoc->str_reset_seq_in = asoc->init_seq_number; michael@0: michael@0: asoc->advanced_peer_ack_point = asoc->last_acked_seq; michael@0: michael@0: /* process the INIT info (peer's info) */ michael@0: if (netp) michael@0: retval = sctp_process_init(init_cp, stcb); michael@0: else michael@0: retval = 0; michael@0: if (retval < 0) { michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: #endif michael@0: (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_16); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: return (NULL); michael@0: } michael@0: /* load all addresses */ michael@0: if (sctp_load_addresses_from_init(stcb, m, michael@0: init_offset + sizeof(struct sctp_init_chunk), initack_offset, michael@0: src, dst, init_src)) { michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: #endif michael@0: (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_17); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: return (NULL); michael@0: } michael@0: /* michael@0: * verify any preceding AUTH chunk that was skipped michael@0: */ michael@0: /* pull the local authentication parameters from the cookie/init-ack */ michael@0: sctp_auth_get_cookie_params(stcb, m, michael@0: initack_offset + sizeof(struct sctp_init_ack_chunk), michael@0: initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk))); michael@0: if (auth_skipped) { michael@0: struct sctp_auth_chunk *auth; michael@0: michael@0: auth = (struct sctp_auth_chunk *) michael@0: sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf); michael@0: if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) { michael@0: /* auth HMAC failed, dump the assoc and packet */ michael@0: SCTPDBG(SCTP_DEBUG_AUTH1, michael@0: "COOKIE-ECHO: AUTH failed\n"); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: #endif michael@0: (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_18); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: return (NULL); michael@0: } else { michael@0: /* remaining chunks checked... good to go */ michael@0: stcb->asoc.authenticated = 1; michael@0: } michael@0: } michael@0: /* update current state */ michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); michael@0: SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); michael@0: if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, michael@0: stcb->sctp_ep, stcb, asoc->primary_destination); michael@0: } michael@0: sctp_stop_all_cookie_timers(stcb); michael@0: SCTP_STAT_INCR_COUNTER32(sctps_passiveestab); michael@0: SCTP_STAT_INCR_GAUGE32(sctps_currestab); michael@0: michael@0: /* michael@0: * if we're doing ASCONFs, check to see if we have any new local michael@0: * addresses that need to get added to the peer (eg. addresses michael@0: * changed while cookie echo in flight). This needs to be done michael@0: * after we go to the OPEN state to do the correct asconf michael@0: * processing. else, make sure we have the correct addresses in our michael@0: * lists michael@0: */ michael@0: michael@0: /* warning, we re-use sin, sin6, sa_store here! */ michael@0: /* pull in local_address (our "from" address) */ michael@0: switch (cookie->laddr_type) { michael@0: #ifdef INET michael@0: case SCTP_IPV4_ADDRESS: michael@0: /* source addr is IPv4 */ michael@0: sin = (struct sockaddr_in *)initack_src; michael@0: memset(sin, 0, sizeof(*sin)); michael@0: sin->sin_family = AF_INET; michael@0: #ifdef HAVE_SIN_LEN michael@0: sin->sin_len = sizeof(struct sockaddr_in); michael@0: #endif michael@0: sin->sin_addr.s_addr = cookie->laddress[0]; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case SCTP_IPV6_ADDRESS: michael@0: /* source addr is IPv6 */ michael@0: sin6 = (struct sockaddr_in6 *)initack_src; michael@0: memset(sin6, 0, sizeof(*sin6)); michael@0: sin6->sin6_family = AF_INET6; michael@0: #ifdef HAVE_SIN6_LEN michael@0: sin6->sin6_len = sizeof(struct sockaddr_in6); michael@0: #endif michael@0: sin6->sin6_scope_id = cookie->scope_id; michael@0: memcpy(&sin6->sin6_addr, cookie->laddress, michael@0: sizeof(sin6->sin6_addr)); michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case SCTP_CONN_ADDRESS: michael@0: /* source addr is IPv4 */ michael@0: sconn = (struct sockaddr_conn *)initack_src; michael@0: memset(sconn, 0, sizeof(struct sockaddr_conn)); michael@0: sconn->sconn_family = AF_CONN; michael@0: #ifdef HAVE_SCONN_LEN michael@0: sconn->sconn_len = sizeof(struct sockaddr_conn); michael@0: #endif michael@0: memcpy(&sconn->sconn_addr, cookie->laddress, sizeof(void *)); michael@0: break; michael@0: #endif michael@0: default: michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: #endif michael@0: (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_19); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: return (NULL); michael@0: } michael@0: michael@0: /* set up to notify upper layer */ michael@0: *notification = SCTP_NOTIFY_ASSOC_UP; michael@0: if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || michael@0: (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && michael@0: (inp->sctp_socket->so_qlimit == 0)) { michael@0: /* michael@0: * This is an endpoint that called connect() how it got a michael@0: * cookie that is NEW is a bit of a mystery. It must be that michael@0: * the INIT was sent, but before it got there.. a complete michael@0: * INIT/INIT-ACK/COOKIE arrived. But of course then it michael@0: * should have went to the other code.. not here.. oh well.. michael@0: * a bit of protection is worth having.. michael@0: */ michael@0: stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: return (NULL); michael@0: } michael@0: #endif michael@0: soisconnected(stcb->sctp_socket); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && michael@0: (inp->sctp_socket->so_qlimit)) { michael@0: /* michael@0: * We don't want to do anything with this one. Since it is michael@0: * the listening guy. The timer will get started for michael@0: * accepted connections in the caller. michael@0: */ michael@0: ; michael@0: } michael@0: /* since we did not send a HB make sure we don't double things */ michael@0: if ((netp) && (*netp)) michael@0: (*netp)->hb_responded = 1; michael@0: michael@0: if (stcb->asoc.sctp_autoclose_ticks && michael@0: sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL); michael@0: } michael@0: /* calculate the RTT */ michael@0: (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); michael@0: if ((netp) && (*netp)) { michael@0: (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp, michael@0: &cookie->time_entered, sctp_align_unsafe_makecopy, michael@0: SCTP_RTT_FROM_NON_DATA); michael@0: } michael@0: /* respond with a COOKIE-ACK */ michael@0: sctp_send_cookie_ack(stcb); michael@0: michael@0: /* michael@0: * check the address lists for any ASCONFs that need to be sent michael@0: * AFTER the cookie-ack is sent michael@0: */ michael@0: sctp_check_address_list(stcb, m, michael@0: initack_offset + sizeof(struct sctp_init_ack_chunk), michael@0: initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)), michael@0: initack_src, cookie->local_scope, cookie->site_scope, michael@0: cookie->ipv4_scope, cookie->loopback_scope); michael@0: michael@0: michael@0: return (stcb); michael@0: } michael@0: michael@0: /* michael@0: * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e michael@0: * we NEED to make sure we are not already using the vtag. If so we michael@0: * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit! michael@0: head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, michael@0: SCTP_BASE_INFO(hashasocmark))]; michael@0: LIST_FOREACH(stcb, head, sctp_asocs) { michael@0: if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) { michael@0: -- SEND ABORT - TRY AGAIN -- michael@0: } michael@0: } michael@0: */ michael@0: michael@0: /* michael@0: * handles a COOKIE-ECHO message stcb: modified to either a new or left as michael@0: * existing (non-NULL) TCB michael@0: */ michael@0: static struct mbuf * michael@0: sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset, michael@0: struct sockaddr *src, struct sockaddr *dst, michael@0: struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp, michael@0: struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp, michael@0: int auth_skipped, uint32_t auth_offset, uint32_t auth_len, michael@0: struct sctp_tcb **locked_tcb, michael@0: #if defined(__FreeBSD__) michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: #endif michael@0: uint32_t vrf_id, uint16_t port) michael@0: { michael@0: struct sctp_state_cookie *cookie; michael@0: struct sctp_tcb *l_stcb = *stcb; michael@0: struct sctp_inpcb *l_inp; michael@0: struct sockaddr *to; michael@0: struct sctp_pcb *ep; michael@0: struct mbuf *m_sig; michael@0: uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE]; michael@0: uint8_t *sig; michael@0: uint8_t cookie_ok = 0; michael@0: unsigned int sig_offset, cookie_offset; michael@0: unsigned int cookie_len; michael@0: struct timeval now; michael@0: struct timeval time_expires; michael@0: int notification = 0; michael@0: struct sctp_nets *netl; michael@0: int had_a_existing_tcb = 0; michael@0: int send_int_conf = 0; michael@0: #ifdef INET michael@0: struct sockaddr_in sin; michael@0: #endif michael@0: #ifdef INET6 michael@0: struct sockaddr_in6 sin6; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: struct sockaddr_conn sconn; michael@0: #endif michael@0: michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, michael@0: "sctp_handle_cookie: handling COOKIE-ECHO\n"); michael@0: michael@0: if (inp_p == NULL) { michael@0: return (NULL); michael@0: } michael@0: cookie = &cp->cookie; michael@0: cookie_offset = offset + sizeof(struct sctp_chunkhdr); michael@0: cookie_len = ntohs(cp->ch.chunk_length); michael@0: michael@0: if ((cookie->peerport != sh->src_port) && michael@0: (cookie->myport != sh->dest_port) && michael@0: (cookie->my_vtag != sh->v_tag)) { michael@0: /* michael@0: * invalid ports or bad tag. Note that we always leave the michael@0: * v_tag in the header in network order and when we stored michael@0: * it in the my_vtag slot we also left it in network order. michael@0: * This maintains the match even though it may be in the michael@0: * opposite byte order of the machine :-> michael@0: */ michael@0: return (NULL); michael@0: } michael@0: if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) + michael@0: sizeof(struct sctp_init_chunk) + michael@0: sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) { michael@0: /* cookie too small */ michael@0: return (NULL); michael@0: } michael@0: /* michael@0: * split off the signature into its own mbuf (since it should not be michael@0: * calculated in the sctp_hmac_m() call). michael@0: */ michael@0: sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE; michael@0: m_sig = m_split(m, sig_offset, M_NOWAIT); michael@0: if (m_sig == NULL) { michael@0: /* out of memory or ?? */ michael@0: return (NULL); michael@0: } michael@0: #ifdef SCTP_MBUF_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { michael@0: struct mbuf *mat; michael@0: michael@0: for (mat = m_sig; mat; mat = SCTP_BUF_NEXT(mat)) { michael@0: if (SCTP_BUF_IS_EXTENDED(mat)) { michael@0: sctp_log_mb(mat, SCTP_MBUF_SPLIT); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /* michael@0: * compute the signature/digest for the cookie michael@0: */ michael@0: ep = &(*inp_p)->sctp_ep; michael@0: l_inp = *inp_p; michael@0: if (l_stcb) { michael@0: SCTP_TCB_UNLOCK(l_stcb); michael@0: } michael@0: SCTP_INP_RLOCK(l_inp); michael@0: if (l_stcb) { michael@0: SCTP_TCB_LOCK(l_stcb); michael@0: } michael@0: /* which cookie is it? */ michael@0: if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) && michael@0: (ep->current_secret_number != ep->last_secret_number)) { michael@0: /* it's the old cookie */ michael@0: (void)sctp_hmac_m(SCTP_HMAC, michael@0: (uint8_t *)ep->secret_key[(int)ep->last_secret_number], michael@0: SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); michael@0: } else { michael@0: /* it's the current cookie */ michael@0: (void)sctp_hmac_m(SCTP_HMAC, michael@0: (uint8_t *)ep->secret_key[(int)ep->current_secret_number], michael@0: SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); michael@0: } michael@0: /* get the signature */ michael@0: SCTP_INP_RUNLOCK(l_inp); michael@0: sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig); michael@0: if (sig == NULL) { michael@0: /* couldn't find signature */ michael@0: sctp_m_freem(m_sig); michael@0: return (NULL); michael@0: } michael@0: /* compare the received digest with the computed digest */ michael@0: if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) { michael@0: /* try the old cookie? */ michael@0: if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) && michael@0: (ep->current_secret_number != ep->last_secret_number)) { michael@0: /* compute digest with old */ michael@0: (void)sctp_hmac_m(SCTP_HMAC, michael@0: (uint8_t *)ep->secret_key[(int)ep->last_secret_number], michael@0: SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0); michael@0: /* compare */ michael@0: if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0) michael@0: cookie_ok = 1; michael@0: } michael@0: } else { michael@0: cookie_ok = 1; michael@0: } michael@0: michael@0: /* michael@0: * Now before we continue we must reconstruct our mbuf so that michael@0: * normal processing of any other chunks will work. michael@0: */ michael@0: { michael@0: struct mbuf *m_at; michael@0: michael@0: m_at = m; michael@0: while (SCTP_BUF_NEXT(m_at) != NULL) { michael@0: m_at = SCTP_BUF_NEXT(m_at); michael@0: } michael@0: SCTP_BUF_NEXT(m_at) = m_sig; michael@0: } michael@0: michael@0: if (cookie_ok == 0) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n"); michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, michael@0: "offset = %u, cookie_offset = %u, sig_offset = %u\n", michael@0: (uint32_t) offset, cookie_offset, sig_offset); michael@0: return (NULL); michael@0: } michael@0: michael@0: /* michael@0: * check the cookie timestamps to be sure it's not stale michael@0: */ michael@0: (void)SCTP_GETTIME_TIMEVAL(&now); michael@0: /* Expire time is in Ticks, so we convert to seconds */ michael@0: time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life); michael@0: time_expires.tv_usec = cookie->time_entered.tv_usec; michael@0: /* TODO sctp_constants.h needs alternative time macros when michael@0: * _KERNEL is undefined. michael@0: */ michael@0: #ifndef __FreeBSD__ michael@0: if (timercmp(&now, &time_expires, >)) michael@0: #else michael@0: if (timevalcmp(&now, &time_expires, >)) michael@0: #endif michael@0: { michael@0: /* cookie is stale! */ michael@0: struct mbuf *op_err; michael@0: struct sctp_stale_cookie_msg *scm; michael@0: uint32_t tim; michael@0: op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg), michael@0: 0, M_NOWAIT, 1, MT_DATA); michael@0: if (op_err == NULL) { michael@0: /* FOOBAR */ michael@0: return (NULL); michael@0: } michael@0: /* Set the len */ michael@0: SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg); michael@0: scm = mtod(op_err, struct sctp_stale_cookie_msg *); michael@0: scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE); michael@0: scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) + michael@0: (sizeof(uint32_t)))); michael@0: /* seconds to usec */ michael@0: tim = (now.tv_sec - time_expires.tv_sec) * 1000000; michael@0: /* add in usec */ michael@0: if (tim == 0) michael@0: tim = now.tv_usec - cookie->time_entered.tv_usec; michael@0: scm->time_usec = htonl(tim); michael@0: sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: return (NULL); michael@0: } michael@0: /* michael@0: * Now we must see with the lookup address if we have an existing michael@0: * asoc. This will only happen if we were in the COOKIE-WAIT state michael@0: * and a INIT collided with us and somewhere the peer sent the michael@0: * cookie on another address besides the single address our assoc michael@0: * had for him. In this case we will have one of the tie-tags set at michael@0: * least AND the address field in the cookie can be used to look it michael@0: * up. michael@0: */ michael@0: to = NULL; michael@0: switch (cookie->addr_type) { michael@0: #ifdef INET6 michael@0: case SCTP_IPV6_ADDRESS: michael@0: memset(&sin6, 0, sizeof(sin6)); michael@0: sin6.sin6_family = AF_INET6; michael@0: #ifdef HAVE_SIN6_LEN michael@0: sin6.sin6_len = sizeof(sin6); michael@0: #endif michael@0: sin6.sin6_port = sh->src_port; michael@0: sin6.sin6_scope_id = cookie->scope_id; michael@0: memcpy(&sin6.sin6_addr.s6_addr, cookie->address, michael@0: sizeof(sin6.sin6_addr.s6_addr)); michael@0: to = (struct sockaddr *)&sin6; michael@0: break; michael@0: #endif michael@0: #ifdef INET michael@0: case SCTP_IPV4_ADDRESS: michael@0: memset(&sin, 0, sizeof(sin)); michael@0: sin.sin_family = AF_INET; michael@0: #ifdef HAVE_SIN_LEN michael@0: sin.sin_len = sizeof(sin); michael@0: #endif michael@0: sin.sin_port = sh->src_port; michael@0: sin.sin_addr.s_addr = cookie->address[0]; michael@0: to = (struct sockaddr *)&sin; michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case SCTP_CONN_ADDRESS: michael@0: memset(&sconn, 0, sizeof(struct sockaddr_conn)); michael@0: sconn.sconn_family = AF_CONN; michael@0: #ifdef HAVE_SCONN_LEN michael@0: sconn.sconn_len = sizeof(struct sockaddr_conn); michael@0: #endif michael@0: sconn.sconn_port = sh->src_port; michael@0: memcpy(&sconn.sconn_addr, cookie->address, sizeof(void *)); michael@0: to = (struct sockaddr *)&sconn; michael@0: break; michael@0: #endif michael@0: default: michael@0: /* This should not happen */ michael@0: return (NULL); michael@0: } michael@0: if ((*stcb == NULL) && to) { michael@0: /* Yep, lets check */ michael@0: *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL); michael@0: if (*stcb == NULL) { michael@0: /* michael@0: * We should have only got back the same inp. If we michael@0: * got back a different ep we have a problem. The michael@0: * original findep got back l_inp and now michael@0: */ michael@0: if (l_inp != *inp_p) { michael@0: SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n"); michael@0: } michael@0: } else { michael@0: if (*locked_tcb == NULL) { michael@0: /* In this case we found the assoc only michael@0: * after we locked the create lock. This means michael@0: * we are in a colliding case and we must make michael@0: * sure that we unlock the tcb if its one of the michael@0: * cases where we throw away the incoming packets. michael@0: */ michael@0: *locked_tcb = *stcb; michael@0: michael@0: /* We must also increment the inp ref count michael@0: * since the ref_count flags was set when we michael@0: * did not find the TCB, now we found it which michael@0: * reduces the refcount.. we must raise it back michael@0: * out to balance it all :-) michael@0: */ michael@0: SCTP_INP_INCR_REF((*stcb)->sctp_ep); michael@0: if ((*stcb)->sctp_ep != l_inp) { michael@0: SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n", michael@0: (void *)(*stcb)->sctp_ep, (void *)l_inp); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: if (to == NULL) { michael@0: return (NULL); michael@0: } michael@0: michael@0: cookie_len -= SCTP_SIGNATURE_SIZE; michael@0: if (*stcb == NULL) { michael@0: /* this is the "normal" case... get a new TCB */ michael@0: *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh, michael@0: cookie, cookie_len, *inp_p, michael@0: netp, to, ¬ification, michael@0: auth_skipped, auth_offset, auth_len, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: } else { michael@0: /* this is abnormal... cookie-echo on existing TCB */ michael@0: had_a_existing_tcb = 1; michael@0: *stcb = sctp_process_cookie_existing(m, iphlen, offset, michael@0: src, dst, sh, michael@0: cookie, cookie_len, *inp_p, *stcb, netp, to, michael@0: ¬ification, auth_skipped, auth_offset, auth_len, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: } michael@0: michael@0: if (*stcb == NULL) { michael@0: /* still no TCB... must be bad cookie-echo */ michael@0: return (NULL); michael@0: } michael@0: #if defined(__FreeBSD__) michael@0: if ((*netp != NULL) && (use_mflowid != 0)) { michael@0: (*netp)->flowid = mflowid; michael@0: #ifdef INVARIANTS michael@0: (*netp)->flowidset = 1; michael@0: #endif michael@0: } michael@0: #endif michael@0: /* michael@0: * Ok, we built an association so confirm the address we sent the michael@0: * INIT-ACK to. michael@0: */ michael@0: netl = sctp_findnet(*stcb, to); michael@0: /* michael@0: * This code should in theory NOT run but michael@0: */ michael@0: if (netl == NULL) { michael@0: /* TSNH! Huh, why do I need to add this address here? */ michael@0: if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) { michael@0: return (NULL); michael@0: } michael@0: netl = sctp_findnet(*stcb, to); michael@0: } michael@0: if (netl) { michael@0: if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) { michael@0: netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED; michael@0: (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL, michael@0: netl); michael@0: send_int_conf = 1; michael@0: } michael@0: } michael@0: sctp_start_net_timers(*stcb); michael@0: if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { michael@0: if (!had_a_existing_tcb || michael@0: (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { michael@0: /* michael@0: * If we have a NEW cookie or the connect never michael@0: * reached the connected state during collision we michael@0: * must do the TCP accept thing. michael@0: */ michael@0: struct socket *so, *oso; michael@0: struct sctp_inpcb *inp; michael@0: michael@0: if (notification == SCTP_NOTIFY_ASSOC_RESTART) { michael@0: /* michael@0: * For a restart we will keep the same michael@0: * socket, no need to do anything. I THINK!! michael@0: */ michael@0: sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); michael@0: if (send_int_conf) { michael@0: sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, michael@0: (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: return (m); michael@0: } michael@0: oso = (*inp_p)->sctp_socket; michael@0: #if (defined(__FreeBSD__) && __FreeBSD_version < 700000) michael@0: /* michael@0: * We do this to keep the sockets side happy during michael@0: * the sonewcon ONLY. michael@0: */ michael@0: NET_LOCK_GIANT(); michael@0: #endif michael@0: atomic_add_int(&(*stcb)->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK((*stcb)); michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_SET(oso->so_vnet); michael@0: #endif michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_LOCK(oso, 1); michael@0: #endif michael@0: so = sonewconn(oso, 0 michael@0: #if defined(__APPLE__) michael@0: ,NULL michael@0: #endif michael@0: #ifdef __Panda__ michael@0: ,NULL , (*inp_p)->def_vrf_id michael@0: #endif michael@0: ); michael@0: #if (defined(__FreeBSD__) && __FreeBSD_version < 700000) michael@0: NET_UNLOCK_GIANT(); michael@0: #endif michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_UNLOCK(oso, 1); michael@0: #endif michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_RESTORE(); michael@0: #endif michael@0: SCTP_TCB_LOCK((*stcb)); michael@0: atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); michael@0: michael@0: if (so == NULL) { michael@0: struct mbuf *op_err; michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *pcb_so; michael@0: #endif michael@0: /* Too many sockets */ michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n"); michael@0: op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); michael@0: sctp_abort_association(*inp_p, NULL, m, iphlen, michael@0: src, dst, sh, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: pcb_so = SCTP_INP_SO(*inp_p); michael@0: atomic_add_int(&(*stcb)->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK((*stcb)); michael@0: SCTP_SOCKET_LOCK(pcb_so, 1); michael@0: SCTP_TCB_LOCK((*stcb)); michael@0: atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); michael@0: #endif michael@0: (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_20); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(pcb_so, 1); michael@0: #endif michael@0: return (NULL); michael@0: } michael@0: inp = (struct sctp_inpcb *)so->so_pcb; michael@0: SCTP_INP_INCR_REF(inp); michael@0: /* michael@0: * We add the unbound flag here so that michael@0: * if we get an soabort() before we get the michael@0: * move_pcb done, we will properly cleanup. michael@0: */ michael@0: inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | michael@0: SCTP_PCB_FLAGS_CONNECTED | michael@0: SCTP_PCB_FLAGS_IN_TCPPOOL | michael@0: SCTP_PCB_FLAGS_UNBOUND | michael@0: (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) | michael@0: SCTP_PCB_FLAGS_DONT_WAKE); michael@0: inp->sctp_features = (*inp_p)->sctp_features; michael@0: inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features; michael@0: inp->sctp_socket = so; michael@0: inp->sctp_frag_point = (*inp_p)->sctp_frag_point; michael@0: inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off; michael@0: inp->sctp_ecn_enable = (*inp_p)->sctp_ecn_enable; michael@0: inp->partial_delivery_point = (*inp_p)->partial_delivery_point; michael@0: inp->sctp_context = (*inp_p)->sctp_context; michael@0: inp->local_strreset_support = (*inp_p)->local_strreset_support; michael@0: inp->inp_starting_point_for_iterator = NULL; michael@0: #if defined(__Userspace__) michael@0: inp->ulp_info = (*inp_p)->ulp_info; michael@0: inp->recv_callback = (*inp_p)->recv_callback; michael@0: inp->send_callback = (*inp_p)->send_callback; michael@0: inp->send_sb_threshold = (*inp_p)->send_sb_threshold; michael@0: #endif michael@0: /* michael@0: * copy in the authentication parameters from the michael@0: * original endpoint michael@0: */ michael@0: if (inp->sctp_ep.local_hmacs) michael@0: sctp_free_hmaclist(inp->sctp_ep.local_hmacs); michael@0: inp->sctp_ep.local_hmacs = michael@0: sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs); michael@0: if (inp->sctp_ep.local_auth_chunks) michael@0: sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); michael@0: inp->sctp_ep.local_auth_chunks = michael@0: sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks); michael@0: michael@0: /* michael@0: * Now we must move it from one hash table to michael@0: * another and get the tcb in the right place. michael@0: */ michael@0: michael@0: /* This is where the one-2-one socket is put into michael@0: * the accept state waiting for the accept! michael@0: */ michael@0: if (*stcb) { michael@0: (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE; michael@0: } michael@0: sctp_move_pcb_and_assoc(*inp_p, inp, *stcb); michael@0: michael@0: atomic_add_int(&(*stcb)->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK((*stcb)); michael@0: michael@0: #if defined(__FreeBSD__) michael@0: sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, michael@0: 0); michael@0: #else michael@0: sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, M_NOWAIT); michael@0: #endif michael@0: SCTP_TCB_LOCK((*stcb)); michael@0: atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); michael@0: michael@0: michael@0: /* now we must check to see if we were aborted while michael@0: * the move was going on and the lock/unlock happened. michael@0: */ michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { michael@0: /* yep it was, we leave the michael@0: * assoc attached to the socket since michael@0: * the sctp_inpcb_free() call will send michael@0: * an abort for us. michael@0: */ michael@0: SCTP_INP_DECR_REF(inp); michael@0: return (NULL); michael@0: } michael@0: SCTP_INP_DECR_REF(inp); michael@0: /* Switch over to the new guy */ michael@0: *inp_p = inp; michael@0: sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); michael@0: if (send_int_conf) { michael@0: sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, michael@0: (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: michael@0: /* Pull it from the incomplete queue and wake the guy */ michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: atomic_add_int(&(*stcb)->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK((*stcb)); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: #endif michael@0: soisconnected(so); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_TCB_LOCK((*stcb)); michael@0: atomic_subtract_int(&(*stcb)->asoc.refcnt, 1); michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: return (m); michael@0: } michael@0: } michael@0: if (notification) { michael@0: sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: if (send_int_conf) { michael@0: sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED, michael@0: (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: return (m); michael@0: } michael@0: michael@0: static void michael@0: sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED, michael@0: struct sctp_tcb *stcb, struct sctp_nets *net) michael@0: { michael@0: /* cp must not be used, others call this without a c-ack :-) */ michael@0: struct sctp_association *asoc; michael@0: michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, michael@0: "sctp_handle_cookie_ack: handling COOKIE-ACK\n"); michael@0: if (stcb == NULL) michael@0: return; michael@0: michael@0: asoc = &stcb->asoc; michael@0: michael@0: sctp_stop_all_cookie_timers(stcb); michael@0: /* process according to association state */ michael@0: if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) { michael@0: /* state change only needed when I am in right state */ michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n"); michael@0: SCTP_SET_STATE(asoc, SCTP_STATE_OPEN); michael@0: sctp_start_net_timers(stcb); michael@0: if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, michael@0: stcb->sctp_ep, stcb, asoc->primary_destination); michael@0: michael@0: } michael@0: /* update RTO */ michael@0: SCTP_STAT_INCR_COUNTER32(sctps_activeestab); michael@0: SCTP_STAT_INCR_GAUGE32(sctps_currestab); michael@0: if (asoc->overall_error_count == 0) { michael@0: net->RTO = sctp_calculate_rto(stcb, asoc, net, michael@0: &asoc->time_entered, sctp_align_safe_nocopy, michael@0: SCTP_RTT_FROM_NON_DATA); michael@0: } michael@0: (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered); michael@0: sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); michael@0: if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || michael@0: (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: michael@0: #endif michael@0: stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: so = SCTP_INP_SO(stcb->sctp_ep); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: #endif michael@0: if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) { michael@0: soisconnected(stcb->sctp_socket); michael@0: } michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: } michael@0: /* michael@0: * since we did not send a HB make sure we don't double michael@0: * things michael@0: */ michael@0: net->hb_responded = 1; michael@0: michael@0: if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { michael@0: /* We don't need to do the asconf thing, michael@0: * nor hb or autoclose if the socket is closed. michael@0: */ michael@0: goto closed_socket; michael@0: } michael@0: michael@0: sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, michael@0: stcb, net); michael@0: michael@0: michael@0: if (stcb->asoc.sctp_autoclose_ticks && michael@0: sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, michael@0: stcb->sctp_ep, stcb, NULL); michael@0: } michael@0: /* michael@0: * send ASCONF if parameters are pending and ASCONFs are michael@0: * allowed (eg. addresses changed when init/cookie echo were michael@0: * in flight) michael@0: */ michael@0: if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) && michael@0: (stcb->asoc.peer_supports_asconf) && michael@0: (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) { michael@0: #ifdef SCTP_TIMER_BASED_ASCONF michael@0: sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, michael@0: stcb->sctp_ep, stcb, michael@0: stcb->asoc.primary_destination); michael@0: #else michael@0: sctp_send_asconf(stcb, stcb->asoc.primary_destination, michael@0: SCTP_ADDR_NOT_LOCKED); michael@0: #endif michael@0: } michael@0: } michael@0: closed_socket: michael@0: /* Toss the cookie if I can */ michael@0: sctp_toss_old_cookies(stcb, asoc); michael@0: if (!TAILQ_EMPTY(&asoc->sent_queue)) { michael@0: /* Restart the timer if we have pending data */ michael@0: struct sctp_tmit_chunk *chk; michael@0: michael@0: chk = TAILQ_FIRST(&asoc->sent_queue); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); michael@0: } michael@0: } michael@0: michael@0: static void michael@0: sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp, michael@0: struct sctp_tcb *stcb) michael@0: { michael@0: struct sctp_nets *net; michael@0: struct sctp_tmit_chunk *lchk; michael@0: struct sctp_ecne_chunk bkup; michael@0: uint8_t override_bit; michael@0: uint32_t tsn, window_data_tsn; michael@0: int len; michael@0: unsigned int pkt_cnt; michael@0: michael@0: len = ntohs(cp->ch.chunk_length); michael@0: if ((len != sizeof(struct sctp_ecne_chunk)) && michael@0: (len != sizeof(struct old_sctp_ecne_chunk))) { michael@0: return; michael@0: } michael@0: if (len == sizeof(struct old_sctp_ecne_chunk)) { michael@0: /* Its the old format */ michael@0: memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk)); michael@0: bkup.num_pkts_since_cwr = htonl(1); michael@0: cp = &bkup; michael@0: } michael@0: SCTP_STAT_INCR(sctps_recvecne); michael@0: tsn = ntohl(cp->tsn); michael@0: pkt_cnt = ntohl(cp->num_pkts_since_cwr); michael@0: lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead); michael@0: if (lchk == NULL) { michael@0: window_data_tsn = stcb->asoc.sending_seq - 1; michael@0: } else { michael@0: window_data_tsn = lchk->rec.data.TSN_seq; michael@0: } michael@0: michael@0: /* Find where it was sent to if possible. */ michael@0: net = NULL; michael@0: TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) { michael@0: if (lchk->rec.data.TSN_seq == tsn) { michael@0: net = lchk->whoTo; michael@0: net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send; michael@0: break; michael@0: } michael@0: if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) { michael@0: break; michael@0: } michael@0: } michael@0: if (net == NULL) { michael@0: /* michael@0: * What to do. A previous send of a michael@0: * CWR was possibly lost. See how old it is, we michael@0: * may have it marked on the actual net. michael@0: */ michael@0: TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { michael@0: if (tsn == net->last_cwr_tsn) { michael@0: /* Found him, send it off */ michael@0: break; michael@0: } michael@0: } michael@0: if (net == NULL) { michael@0: /* michael@0: * If we reach here, we need to send a special michael@0: * CWR that says hey, we did this a long time michael@0: * ago and you lost the response. michael@0: */ michael@0: net = TAILQ_FIRST(&stcb->asoc.nets); michael@0: if (net == NULL) { michael@0: /* TSNH */ michael@0: return; michael@0: } michael@0: override_bit = SCTP_CWR_REDUCE_OVERRIDE; michael@0: } else { michael@0: override_bit = 0; michael@0: } michael@0: } else { michael@0: override_bit = 0; michael@0: } michael@0: if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) && michael@0: ((override_bit&SCTP_CWR_REDUCE_OVERRIDE) == 0)) { michael@0: /* JRS - Use the congestion control given in the pluggable CC module */ michael@0: stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt); michael@0: /* michael@0: * We reduce once every RTT. So we will only lower cwnd at michael@0: * the next sending seq i.e. the window_data_tsn michael@0: */ michael@0: net->cwr_window_tsn = window_data_tsn; michael@0: net->ecn_ce_pkt_cnt += pkt_cnt; michael@0: net->lost_cnt = pkt_cnt; michael@0: net->last_cwr_tsn = tsn; michael@0: } else { michael@0: override_bit |= SCTP_CWR_IN_SAME_WINDOW; michael@0: if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) && michael@0: ((override_bit&SCTP_CWR_REDUCE_OVERRIDE) == 0)) { michael@0: /* michael@0: * Another loss in the same window update how michael@0: * many marks/packets lost we have had. michael@0: */ michael@0: int cnt = 1; michael@0: if (pkt_cnt > net->lost_cnt) { michael@0: /* Should be the case */ michael@0: cnt = (pkt_cnt - net->lost_cnt); michael@0: net->ecn_ce_pkt_cnt += cnt; michael@0: } michael@0: net->lost_cnt = pkt_cnt; michael@0: net->last_cwr_tsn = tsn; michael@0: /* michael@0: * Most CC functions will ignore this call, since we are in-window michael@0: * yet of the initial CE the peer saw. michael@0: */ michael@0: stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt); michael@0: } michael@0: } michael@0: /* michael@0: * We always send a CWR this way if our previous one was lost our michael@0: * peer will get an update, or if it is not time again to reduce we michael@0: * still get the cwr to the peer. Note we set the override when we michael@0: * could not find the TSN on the chunk or the destination network. michael@0: */ michael@0: sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit); michael@0: } michael@0: michael@0: static void michael@0: sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net) michael@0: { michael@0: /* michael@0: * Here we get a CWR from the peer. We must look in the outqueue and michael@0: * make sure that we have a covered ECNE in the control chunk part. michael@0: * If so remove it. michael@0: */ michael@0: struct sctp_tmit_chunk *chk; michael@0: struct sctp_ecne_chunk *ecne; michael@0: int override; michael@0: uint32_t cwr_tsn; michael@0: cwr_tsn = ntohl(cp->tsn); michael@0: michael@0: override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE; michael@0: TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) { michael@0: if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) { michael@0: continue; michael@0: } michael@0: if ((override == 0) && (chk->whoTo != net)) { michael@0: /* Must be from the right src unless override is set */ michael@0: continue; michael@0: } michael@0: ecne = mtod(chk->data, struct sctp_ecne_chunk *); michael@0: if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) { michael@0: /* this covers this ECNE, we can remove it */ michael@0: stcb->asoc.ecn_echo_cnt_onq--; michael@0: TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, michael@0: sctp_next); michael@0: if (chk->data) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: } michael@0: stcb->asoc.ctrl_queue_cnt--; michael@0: sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); michael@0: if (override == 0) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void michael@0: sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED, michael@0: struct sctp_tcb *stcb, struct sctp_nets *net) michael@0: { michael@0: struct sctp_association *asoc; michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: #endif michael@0: michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, michael@0: "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n"); michael@0: if (stcb == NULL) michael@0: return; michael@0: michael@0: asoc = &stcb->asoc; michael@0: /* process according to association state */ michael@0: if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) { michael@0: /* unexpected SHUTDOWN-COMPLETE... so ignore... */ michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, michael@0: "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n"); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: return; michael@0: } michael@0: /* notify upper layer protocol */ michael@0: if (stcb->sctp_socket) { michael@0: sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: #ifdef INVARIANTS michael@0: if (!TAILQ_EMPTY(&asoc->send_queue) || michael@0: !TAILQ_EMPTY(&asoc->sent_queue) || michael@0: !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) { michael@0: panic("Queues are not empty when handling SHUTDOWN-COMPLETE"); michael@0: } michael@0: #endif michael@0: /* stop the timer */ michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_22); michael@0: SCTP_STAT_INCR_COUNTER32(sctps_shutdown); michael@0: /* free the TCB */ michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, michael@0: "sctp_handle_shutdown_complete: calls free-asoc\n"); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: so = SCTP_INP_SO(stcb->sctp_ep); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: #endif michael@0: (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_23); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: return; michael@0: } michael@0: michael@0: static int michael@0: process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc, michael@0: struct sctp_nets *net, uint8_t flg) michael@0: { michael@0: switch (desc->chunk_type) { michael@0: case SCTP_DATA: michael@0: /* find the tsn to resend (possibly */ michael@0: { michael@0: uint32_t tsn; michael@0: struct sctp_tmit_chunk *tp1; michael@0: michael@0: tsn = ntohl(desc->tsn_ifany); michael@0: TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { michael@0: if (tp1->rec.data.TSN_seq == tsn) { michael@0: /* found it */ michael@0: break; michael@0: } michael@0: if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) { michael@0: /* not found */ michael@0: tp1 = NULL; michael@0: break; michael@0: } michael@0: } michael@0: if (tp1 == NULL) { michael@0: /* michael@0: * Do it the other way , aka without paying michael@0: * attention to queue seq order. michael@0: */ michael@0: SCTP_STAT_INCR(sctps_pdrpdnfnd); michael@0: TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { michael@0: if (tp1->rec.data.TSN_seq == tsn) { michael@0: /* found it */ michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: if (tp1 == NULL) { michael@0: SCTP_STAT_INCR(sctps_pdrptsnnf); michael@0: } michael@0: if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) { michael@0: uint8_t *ddp; michael@0: michael@0: if (((flg & SCTP_BADCRC) == 0) && michael@0: ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { michael@0: return (0); michael@0: } michael@0: if ((stcb->asoc.peers_rwnd == 0) && michael@0: ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) { michael@0: SCTP_STAT_INCR(sctps_pdrpdiwnp); michael@0: return (0); michael@0: } michael@0: if (stcb->asoc.peers_rwnd == 0 && michael@0: (flg & SCTP_FROM_MIDDLE_BOX)) { michael@0: SCTP_STAT_INCR(sctps_pdrpdizrw); michael@0: return (0); michael@0: } michael@0: ddp = (uint8_t *) (mtod(tp1->data, caddr_t) + michael@0: sizeof(struct sctp_data_chunk)); michael@0: { michael@0: unsigned int iii; michael@0: michael@0: for (iii = 0; iii < sizeof(desc->data_bytes); michael@0: iii++) { michael@0: if (ddp[iii] != desc->data_bytes[iii]) { michael@0: SCTP_STAT_INCR(sctps_pdrpbadd); michael@0: return (-1); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (tp1->do_rtt) { michael@0: /* michael@0: * this guy had a RTO calculation michael@0: * pending on it, cancel it michael@0: */ michael@0: if (tp1->whoTo->rto_needed == 0) { michael@0: tp1->whoTo->rto_needed = 1; michael@0: } michael@0: tp1->do_rtt = 0; michael@0: } michael@0: SCTP_STAT_INCR(sctps_pdrpmark); michael@0: if (tp1->sent != SCTP_DATAGRAM_RESEND) michael@0: sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); michael@0: /* michael@0: * mark it as if we were doing a FR, since michael@0: * we will be getting gap ack reports behind michael@0: * the info from the router. michael@0: */ michael@0: tp1->rec.data.doing_fast_retransmit = 1; michael@0: /* michael@0: * mark the tsn with what sequences can michael@0: * cause a new FR. michael@0: */ michael@0: if (TAILQ_EMPTY(&stcb->asoc.send_queue)) { michael@0: tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq; michael@0: } else { michael@0: tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq; michael@0: } michael@0: michael@0: /* restart the timer */ michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, michael@0: stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT+SCTP_LOC_24); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, michael@0: stcb, tp1->whoTo); michael@0: michael@0: /* fix counts and things */ michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { michael@0: sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP, michael@0: tp1->whoTo->flight_size, michael@0: tp1->book_size, michael@0: (uintptr_t)stcb, michael@0: tp1->rec.data.TSN_seq); michael@0: } michael@0: if (tp1->sent < SCTP_DATAGRAM_RESEND) { michael@0: sctp_flight_size_decrease(tp1); michael@0: sctp_total_flight_decrease(stcb, tp1); michael@0: } michael@0: tp1->sent = SCTP_DATAGRAM_RESEND; michael@0: } { michael@0: /* audit code */ michael@0: unsigned int audit; michael@0: michael@0: audit = 0; michael@0: TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) { michael@0: if (tp1->sent == SCTP_DATAGRAM_RESEND) michael@0: audit++; michael@0: } michael@0: TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue, michael@0: sctp_next) { michael@0: if (tp1->sent == SCTP_DATAGRAM_RESEND) michael@0: audit++; michael@0: } michael@0: if (audit != stcb->asoc.sent_queue_retran_cnt) { michael@0: SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n", michael@0: audit, stcb->asoc.sent_queue_retran_cnt); michael@0: #ifndef SCTP_AUDITING_ENABLED michael@0: stcb->asoc.sent_queue_retran_cnt = audit; michael@0: #endif michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: case SCTP_ASCONF: michael@0: { michael@0: struct sctp_tmit_chunk *asconf; michael@0: michael@0: TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue, michael@0: sctp_next) { michael@0: if (asconf->rec.chunk_id.id == SCTP_ASCONF) { michael@0: break; michael@0: } michael@0: } michael@0: if (asconf) { michael@0: if (asconf->sent != SCTP_DATAGRAM_RESEND) michael@0: sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); michael@0: asconf->sent = SCTP_DATAGRAM_RESEND; michael@0: asconf->snd_count--; michael@0: } michael@0: } michael@0: break; michael@0: case SCTP_INITIATION: michael@0: /* resend the INIT */ michael@0: stcb->asoc.dropped_special_cnt++; michael@0: if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) { michael@0: /* michael@0: * If we can get it in, in a few attempts we do michael@0: * this, otherwise we let the timer fire. michael@0: */ michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, michael@0: stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_25); michael@0: sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: break; michael@0: case SCTP_SELECTIVE_ACK: michael@0: case SCTP_NR_SELECTIVE_ACK: michael@0: /* resend the sack */ michael@0: sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); michael@0: break; michael@0: case SCTP_HEARTBEAT_REQUEST: michael@0: /* resend a demand HB */ michael@0: if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) { michael@0: /* Only retransmit if we KNOW we wont destroy the tcb */ michael@0: sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: break; michael@0: case SCTP_SHUTDOWN: michael@0: sctp_send_shutdown(stcb, net); michael@0: break; michael@0: case SCTP_SHUTDOWN_ACK: michael@0: sctp_send_shutdown_ack(stcb, net); michael@0: break; michael@0: case SCTP_COOKIE_ECHO: michael@0: { michael@0: struct sctp_tmit_chunk *cookie; michael@0: michael@0: cookie = NULL; michael@0: TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, michael@0: sctp_next) { michael@0: if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) { michael@0: break; michael@0: } michael@0: } michael@0: if (cookie) { michael@0: if (cookie->sent != SCTP_DATAGRAM_RESEND) michael@0: sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); michael@0: cookie->sent = SCTP_DATAGRAM_RESEND; michael@0: sctp_stop_all_cookie_timers(stcb); michael@0: } michael@0: } michael@0: break; michael@0: case SCTP_COOKIE_ACK: michael@0: sctp_send_cookie_ack(stcb); michael@0: break; michael@0: case SCTP_ASCONF_ACK: michael@0: /* resend last asconf ack */ michael@0: sctp_send_asconf_ack(stcb); michael@0: break; michael@0: case SCTP_FORWARD_CUM_TSN: michael@0: send_forward_tsn(stcb, &stcb->asoc); michael@0: break; michael@0: /* can't do anything with these */ michael@0: case SCTP_PACKET_DROPPED: michael@0: case SCTP_INITIATION_ACK: /* this should not happen */ michael@0: case SCTP_HEARTBEAT_ACK: michael@0: case SCTP_ABORT_ASSOCIATION: michael@0: case SCTP_OPERATION_ERROR: michael@0: case SCTP_SHUTDOWN_COMPLETE: michael@0: case SCTP_ECN_ECHO: michael@0: case SCTP_ECN_CWR: michael@0: default: michael@0: break; michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: void michael@0: sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list) michael@0: { michael@0: uint32_t i; michael@0: uint16_t temp; michael@0: michael@0: /* michael@0: * We set things to 0xffff since this is the last delivered sequence michael@0: * and we will be sending in 0 after the reset. michael@0: */ michael@0: michael@0: if (number_entries) { michael@0: for (i = 0; i < number_entries; i++) { michael@0: temp = ntohs(list[i]); michael@0: if (temp >= stcb->asoc.streamincnt) { michael@0: continue; michael@0: } michael@0: stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff; michael@0: } michael@0: } else { michael@0: list = NULL; michael@0: for (i = 0; i < stcb->asoc.streamincnt; i++) { michael@0: stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; michael@0: } michael@0: } michael@0: sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: michael@0: static void michael@0: sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list) michael@0: { michael@0: uint32_t i; michael@0: uint16_t temp; michael@0: michael@0: if (number_entries > 0) { michael@0: for (i = 0; i < number_entries; i++) { michael@0: temp = ntohs(list[i]); michael@0: if (temp >= stcb->asoc.streamoutcnt) { michael@0: /* no such stream */ michael@0: continue; michael@0: } michael@0: stcb->asoc.strmout[temp].next_sequence_send = 0; michael@0: } michael@0: } else { michael@0: for (i = 0; i < stcb->asoc.streamoutcnt; i++) { michael@0: stcb->asoc.strmout[i].next_sequence_send = 0; michael@0: } michael@0: } michael@0: sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: michael@0: michael@0: struct sctp_stream_reset_out_request * michael@0: sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk) michael@0: { michael@0: struct sctp_association *asoc; michael@0: struct sctp_chunkhdr *ch; michael@0: struct sctp_stream_reset_out_request *r; michael@0: struct sctp_tmit_chunk *chk; michael@0: int len, clen; michael@0: michael@0: asoc = &stcb->asoc; michael@0: if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { michael@0: asoc->stream_reset_outstanding = 0; michael@0: return (NULL); michael@0: } michael@0: if (stcb->asoc.str_reset == NULL) { michael@0: asoc->stream_reset_outstanding = 0; michael@0: return (NULL); michael@0: } michael@0: chk = stcb->asoc.str_reset; michael@0: if (chk->data == NULL) { michael@0: return (NULL); michael@0: } michael@0: if (bchk) { michael@0: /* he wants a copy of the chk pointer */ michael@0: *bchk = chk; michael@0: } michael@0: clen = chk->send_size; michael@0: ch = mtod(chk->data, struct sctp_chunkhdr *); michael@0: r = (struct sctp_stream_reset_out_request *)(ch + 1); michael@0: if (ntohl(r->request_seq) == seq) { michael@0: /* found it */ michael@0: return (r); michael@0: } michael@0: len = SCTP_SIZE32(ntohs(r->ph.param_length)); michael@0: if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) { michael@0: /* move to the next one, there can only be a max of two */ michael@0: r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len); michael@0: if (ntohl(r->request_seq) == seq) { michael@0: return (r); michael@0: } michael@0: } michael@0: /* that seq is not here */ michael@0: return (NULL); michael@0: } michael@0: michael@0: static void michael@0: sctp_clean_up_stream_reset(struct sctp_tcb *stcb) michael@0: { michael@0: struct sctp_association *asoc; michael@0: struct sctp_tmit_chunk *chk = stcb->asoc.str_reset; michael@0: michael@0: if (stcb->asoc.str_reset == NULL) { michael@0: return; michael@0: } michael@0: asoc = &stcb->asoc; michael@0: michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT+SCTP_LOC_26); michael@0: TAILQ_REMOVE(&asoc->control_send_queue, michael@0: chk, michael@0: sctp_next); michael@0: if (chk->data) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: } michael@0: asoc->ctrl_queue_cnt--; michael@0: sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); michael@0: /*sa_ignore NO_NULL_CHK*/ michael@0: stcb->asoc.str_reset = NULL; michael@0: } michael@0: michael@0: michael@0: static int michael@0: sctp_handle_stream_reset_response(struct sctp_tcb *stcb, michael@0: uint32_t seq, uint32_t action, michael@0: struct sctp_stream_reset_response *respin) michael@0: { michael@0: uint16_t type; michael@0: int lparm_len; michael@0: struct sctp_association *asoc = &stcb->asoc; michael@0: struct sctp_tmit_chunk *chk; michael@0: struct sctp_stream_reset_out_request *srparam; michael@0: uint32_t number_entries; michael@0: michael@0: if (asoc->stream_reset_outstanding == 0) { michael@0: /* duplicate */ michael@0: return (0); michael@0: } michael@0: if (seq == stcb->asoc.str_reset_seq_out) { michael@0: srparam = sctp_find_stream_reset(stcb, seq, &chk); michael@0: if (srparam) { michael@0: stcb->asoc.str_reset_seq_out++; michael@0: type = ntohs(srparam->ph.param_type); michael@0: lparm_len = ntohs(srparam->ph.param_length); michael@0: if (type == SCTP_STR_RESET_OUT_REQUEST) { michael@0: number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t); michael@0: asoc->stream_reset_out_is_outstanding = 0; michael@0: if (asoc->stream_reset_outstanding) michael@0: asoc->stream_reset_outstanding--; michael@0: if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { michael@0: /* do it */ michael@0: sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams); michael@0: } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { michael@0: sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); michael@0: } else { michael@0: sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: } else if (type == SCTP_STR_RESET_IN_REQUEST) { michael@0: /* Answered my request */ michael@0: number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t); michael@0: if (asoc->stream_reset_outstanding) michael@0: asoc->stream_reset_outstanding--; michael@0: if (action == SCTP_STREAM_RESET_RESULT_DENIED) { michael@0: sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb, michael@0: number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); michael@0: } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) { michael@0: sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, michael@0: number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) { michael@0: /* Ok we now may have more streams */ michael@0: int num_stream; michael@0: michael@0: num_stream = stcb->asoc.strm_pending_add_size; michael@0: if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) { michael@0: /* TSNH */ michael@0: num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt; michael@0: } michael@0: stcb->asoc.strm_pending_add_size = 0; michael@0: if (asoc->stream_reset_outstanding) michael@0: asoc->stream_reset_outstanding--; michael@0: if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { michael@0: /* Put the new streams into effect */ michael@0: stcb->asoc.streamoutcnt += num_stream; michael@0: sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0); michael@0: } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { michael@0: sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, michael@0: SCTP_STREAM_CHANGE_DENIED); michael@0: } else { michael@0: sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, michael@0: SCTP_STREAM_CHANGE_FAILED); michael@0: } michael@0: } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) { michael@0: if (asoc->stream_reset_outstanding) michael@0: asoc->stream_reset_outstanding--; michael@0: if (action == SCTP_STREAM_RESET_RESULT_DENIED) { michael@0: sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, michael@0: SCTP_STREAM_CHANGE_DENIED); michael@0: } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) { michael@0: sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, michael@0: SCTP_STREAM_CHANGE_FAILED); michael@0: } michael@0: } else if (type == SCTP_STR_RESET_TSN_REQUEST) { michael@0: /** michael@0: * a) Adopt the new in tsn. michael@0: * b) reset the map michael@0: * c) Adopt the new out-tsn michael@0: */ michael@0: struct sctp_stream_reset_response_tsn *resp; michael@0: struct sctp_forward_tsn_chunk fwdtsn; michael@0: int abort_flag = 0; michael@0: if (respin == NULL) { michael@0: /* huh ? */ michael@0: return (0); michael@0: } michael@0: if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) { michael@0: resp = (struct sctp_stream_reset_response_tsn *)respin; michael@0: asoc->stream_reset_outstanding--; michael@0: fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); michael@0: fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; michael@0: fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1); michael@0: sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); michael@0: if (abort_flag) { michael@0: return (1); michael@0: } michael@0: stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1); michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { michael@0: sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); michael@0: } michael@0: michael@0: stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map; michael@0: stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn); michael@0: memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); michael@0: michael@0: stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map; michael@0: memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); michael@0: michael@0: stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn); michael@0: stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn; michael@0: michael@0: sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); michael@0: sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); michael@0: sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0); michael@0: } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) { michael@0: sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), michael@0: SCTP_ASSOC_RESET_DENIED); michael@0: } else { michael@0: sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), michael@0: SCTP_ASSOC_RESET_FAILED); michael@0: } michael@0: } michael@0: /* get rid of the request and get the request flags */ michael@0: if (asoc->stream_reset_outstanding == 0) { michael@0: sctp_clean_up_stream_reset(stcb); michael@0: } michael@0: } michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: static void michael@0: sctp_handle_str_reset_request_in(struct sctp_tcb *stcb, michael@0: struct sctp_tmit_chunk *chk, michael@0: struct sctp_stream_reset_in_request *req, int trunc) michael@0: { michael@0: uint32_t seq; michael@0: int len, i; michael@0: int number_entries; michael@0: uint16_t temp; michael@0: michael@0: /* michael@0: * peer wants me to send a str-reset to him for my outgoing seq's if michael@0: * seq_in is right. michael@0: */ michael@0: struct sctp_association *asoc = &stcb->asoc; michael@0: michael@0: seq = ntohl(req->request_seq); michael@0: if (asoc->str_reset_seq_in == seq) { michael@0: asoc->last_reset_action[1] = asoc->last_reset_action[0]; michael@0: if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) { michael@0: asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; michael@0: } else if (trunc) { michael@0: /* Can't do it, since they exceeded our buffer size */ michael@0: asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; michael@0: } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) { michael@0: len = ntohs(req->ph.param_length); michael@0: number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t)); michael@0: for (i = 0; i < number_entries; i++) { michael@0: temp = ntohs(req->list_of_streams[i]); michael@0: req->list_of_streams[i] = temp; michael@0: } michael@0: asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; michael@0: sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams, michael@0: asoc->str_reset_seq_out, michael@0: seq, (asoc->sending_seq - 1)); michael@0: asoc->stream_reset_out_is_outstanding = 1; michael@0: asoc->str_reset = chk; michael@0: sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo); michael@0: stcb->asoc.stream_reset_outstanding++; michael@0: } else { michael@0: /* Can't do it, since we have sent one out */ michael@0: asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS; michael@0: } michael@0: sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); michael@0: asoc->str_reset_seq_in++; michael@0: } else if (asoc->str_reset_seq_in - 1 == seq) { michael@0: sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); michael@0: } else if (asoc->str_reset_seq_in - 2 == seq) { michael@0: sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); michael@0: } else { michael@0: sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); michael@0: } michael@0: } michael@0: michael@0: static int michael@0: sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb, michael@0: struct sctp_tmit_chunk *chk, michael@0: struct sctp_stream_reset_tsn_request *req) michael@0: { michael@0: /* reset all in and out and update the tsn */ michael@0: /* michael@0: * A) reset my str-seq's on in and out. B) Select a receive next, michael@0: * and set cum-ack to it. Also process this selected number as a michael@0: * fwd-tsn as well. C) set in the response my next sending seq. michael@0: */ michael@0: struct sctp_forward_tsn_chunk fwdtsn; michael@0: struct sctp_association *asoc = &stcb->asoc; michael@0: int abort_flag = 0; michael@0: uint32_t seq; michael@0: michael@0: seq = ntohl(req->request_seq); michael@0: if (asoc->str_reset_seq_in == seq) { michael@0: asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0]; michael@0: if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { michael@0: asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; michael@0: } else { michael@0: fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk)); michael@0: fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN; michael@0: fwdtsn.ch.chunk_flags = 0; michael@0: fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1); michael@0: sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0); michael@0: if (abort_flag) { michael@0: return (1); michael@0: } michael@0: asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA; michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { michael@0: sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); michael@0: } michael@0: asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map; michael@0: asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1; michael@0: memset(asoc->mapping_array, 0, asoc->mapping_array_size); michael@0: asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map; michael@0: memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size); michael@0: atomic_add_int(&asoc->sending_seq, 1); michael@0: /* save off historical data for retrans */ michael@0: asoc->last_sending_seq[1] = asoc->last_sending_seq[0]; michael@0: asoc->last_sending_seq[0] = asoc->sending_seq; michael@0: asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0]; michael@0: asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn; michael@0: sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL); michael@0: sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL); michael@0: asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; michael@0: sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0); michael@0: } michael@0: sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], michael@0: asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]); michael@0: asoc->str_reset_seq_in++; michael@0: } else if (asoc->str_reset_seq_in - 1 == seq) { michael@0: sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0], michael@0: asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]); michael@0: } else if (asoc->str_reset_seq_in - 2 == seq) { michael@0: sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1], michael@0: asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]); michael@0: } else { michael@0: sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: static void michael@0: sctp_handle_str_reset_request_out(struct sctp_tcb *stcb, michael@0: struct sctp_tmit_chunk *chk, michael@0: struct sctp_stream_reset_out_request *req, int trunc) michael@0: { michael@0: uint32_t seq, tsn; michael@0: int number_entries, len; michael@0: struct sctp_association *asoc = &stcb->asoc; michael@0: michael@0: seq = ntohl(req->request_seq); michael@0: michael@0: /* now if its not a duplicate we process it */ michael@0: if (asoc->str_reset_seq_in == seq) { michael@0: len = ntohs(req->ph.param_length); michael@0: number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t)); michael@0: /* michael@0: * the sender is resetting, handle the list issue.. we must michael@0: * a) verify if we can do the reset, if so no problem b) If michael@0: * we can't do the reset we must copy the request. c) queue michael@0: * it, and setup the data in processor to trigger it off michael@0: * when needed and dequeue all the queued data. michael@0: */ michael@0: tsn = ntohl(req->send_reset_at_tsn); michael@0: michael@0: /* move the reset action back one */ michael@0: asoc->last_reset_action[1] = asoc->last_reset_action[0]; michael@0: if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) { michael@0: asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; michael@0: } else if (trunc) { michael@0: asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; michael@0: } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) { michael@0: /* we can do it now */ michael@0: sctp_reset_in_stream(stcb, number_entries, req->list_of_streams); michael@0: asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; michael@0: } else { michael@0: /* michael@0: * we must queue it up and thus wait for the TSN's michael@0: * to arrive that are at or before tsn michael@0: */ michael@0: struct sctp_stream_reset_list *liste; michael@0: int siz; michael@0: michael@0: siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t)); michael@0: SCTP_MALLOC(liste, struct sctp_stream_reset_list *, michael@0: siz, SCTP_M_STRESET); michael@0: if (liste == NULL) { michael@0: /* gak out of memory */ michael@0: asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; michael@0: sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); michael@0: return; michael@0: } michael@0: liste->tsn = tsn; michael@0: liste->number_entries = number_entries; michael@0: memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t)); michael@0: TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp); michael@0: asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; michael@0: } michael@0: sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); michael@0: asoc->str_reset_seq_in++; michael@0: } else if ((asoc->str_reset_seq_in - 1) == seq) { michael@0: /* michael@0: * one seq back, just echo back last action since my michael@0: * response was lost. michael@0: */ michael@0: sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); michael@0: } else if ((asoc->str_reset_seq_in - 2) == seq) { michael@0: /* michael@0: * two seq back, just echo back last action since my michael@0: * response was lost. michael@0: */ michael@0: sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); michael@0: } else { michael@0: sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); michael@0: } michael@0: } michael@0: michael@0: static void michael@0: sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, michael@0: struct sctp_stream_reset_add_strm *str_add) michael@0: { michael@0: /* michael@0: * Peer is requesting to add more streams. michael@0: * If its within our max-streams we will michael@0: * allow it. michael@0: */ michael@0: uint32_t num_stream, i; michael@0: uint32_t seq; michael@0: struct sctp_association *asoc = &stcb->asoc; michael@0: struct sctp_queued_to_read *ctl, *nctl; michael@0: michael@0: /* Get the number. */ michael@0: seq = ntohl(str_add->request_seq); michael@0: num_stream = ntohs(str_add->number_of_streams); michael@0: /* Now what would be the new total? */ michael@0: if (asoc->str_reset_seq_in == seq) { michael@0: num_stream += stcb->asoc.streamincnt; michael@0: stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; michael@0: if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { michael@0: asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; michael@0: } else if ((num_stream > stcb->asoc.max_inbound_streams) || michael@0: (num_stream > 0xffff)) { michael@0: /* We must reject it they ask for to many */ michael@0: denied: michael@0: stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; michael@0: } else { michael@0: /* Ok, we can do that :-) */ michael@0: struct sctp_stream_in *oldstrm; michael@0: michael@0: /* save off the old */ michael@0: oldstrm = stcb->asoc.strmin; michael@0: SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *, michael@0: (num_stream * sizeof(struct sctp_stream_in)), michael@0: SCTP_M_STRMI); michael@0: if (stcb->asoc.strmin == NULL) { michael@0: stcb->asoc.strmin = oldstrm; michael@0: goto denied; michael@0: } michael@0: /* copy off the old data */ michael@0: for (i = 0; i < stcb->asoc.streamincnt; i++) { michael@0: TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); michael@0: stcb->asoc.strmin[i].stream_no = i; michael@0: stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered; michael@0: stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started; michael@0: /* now anything on those queues? */ michael@0: TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) { michael@0: TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next); michael@0: TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next); michael@0: } michael@0: } michael@0: /* Init the new streams */ michael@0: for (i = stcb->asoc.streamincnt; i < num_stream; i++) { michael@0: TAILQ_INIT(&stcb->asoc.strmin[i].inqueue); michael@0: stcb->asoc.strmin[i].stream_no = i; michael@0: stcb->asoc.strmin[i].last_sequence_delivered = 0xffff; michael@0: stcb->asoc.strmin[i].delivery_started = 0; michael@0: } michael@0: SCTP_FREE(oldstrm, SCTP_M_STRMI); michael@0: /* update the size */ michael@0: stcb->asoc.streamincnt = num_stream; michael@0: stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; michael@0: sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0); michael@0: } michael@0: sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); michael@0: asoc->str_reset_seq_in++; michael@0: } else if ((asoc->str_reset_seq_in - 1) == seq) { michael@0: /* michael@0: * one seq back, just echo back last action since my michael@0: * response was lost. michael@0: */ michael@0: sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); michael@0: } else if ((asoc->str_reset_seq_in - 2) == seq) { michael@0: /* michael@0: * two seq back, just echo back last action since my michael@0: * response was lost. michael@0: */ michael@0: sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); michael@0: } else { michael@0: sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); michael@0: michael@0: } michael@0: } michael@0: michael@0: static void michael@0: sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk, michael@0: struct sctp_stream_reset_add_strm *str_add) michael@0: { michael@0: /* michael@0: * Peer is requesting to add more streams. michael@0: * If its within our max-streams we will michael@0: * allow it. michael@0: */ michael@0: uint16_t num_stream; michael@0: uint32_t seq; michael@0: struct sctp_association *asoc = &stcb->asoc; michael@0: michael@0: /* Get the number. */ michael@0: seq = ntohl(str_add->request_seq); michael@0: num_stream = ntohs(str_add->number_of_streams); michael@0: /* Now what would be the new total? */ michael@0: if (asoc->str_reset_seq_in == seq) { michael@0: stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0]; michael@0: if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) { michael@0: asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; michael@0: } else if (stcb->asoc.stream_reset_outstanding) { michael@0: /* We must reject it we have something pending */ michael@0: stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS; michael@0: } else { michael@0: /* Ok, we can do that :-) */ michael@0: int mychk; michael@0: mychk = stcb->asoc.streamoutcnt; michael@0: mychk += num_stream; michael@0: if (mychk < 0x10000) { michael@0: stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED; michael@0: if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, 1, num_stream, 0, 1)) { michael@0: stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; michael@0: } michael@0: } else { michael@0: stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED; michael@0: } michael@0: } michael@0: sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]); michael@0: asoc->str_reset_seq_in++; michael@0: } else if ((asoc->str_reset_seq_in - 1) == seq) { michael@0: /* michael@0: * one seq back, just echo back last action since my michael@0: * response was lost. michael@0: */ michael@0: sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]); michael@0: } else if ((asoc->str_reset_seq_in - 2) == seq) { michael@0: /* michael@0: * two seq back, just echo back last action since my michael@0: * response was lost. michael@0: */ michael@0: sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]); michael@0: } else { michael@0: sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO); michael@0: } michael@0: } michael@0: michael@0: #if !defined(__Panda__) michael@0: #ifdef __GNUC__ michael@0: __attribute__ ((noinline)) michael@0: #endif michael@0: #endif michael@0: static int michael@0: sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset, michael@0: struct sctp_chunkhdr *ch_req) michael@0: { michael@0: int chk_length, param_len, ptype; michael@0: struct sctp_paramhdr pstore; michael@0: uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE]; michael@0: uint32_t seq = 0; michael@0: int num_req = 0; michael@0: int trunc = 0; michael@0: struct sctp_tmit_chunk *chk; michael@0: struct sctp_chunkhdr *ch; michael@0: struct sctp_paramhdr *ph; michael@0: int ret_code = 0; michael@0: int num_param = 0; michael@0: michael@0: /* now it may be a reset or a reset-response */ michael@0: chk_length = ntohs(ch_req->chunk_length); michael@0: michael@0: /* setup for adding the response */ michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: return (ret_code); michael@0: } michael@0: chk->rec.chunk_id.id = SCTP_STREAM_RESET; michael@0: chk->rec.chunk_id.can_take_data = 0; michael@0: chk->asoc = &stcb->asoc; michael@0: chk->no_fr_allowed = 0; michael@0: chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr); michael@0: chk->book_size_scale = 0; michael@0: chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (chk->data == NULL) { michael@0: strres_nochunk: michael@0: if (chk->data) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: } michael@0: sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); michael@0: return (ret_code); michael@0: } michael@0: SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD); michael@0: michael@0: /* setup chunk parameters */ michael@0: chk->sent = SCTP_DATAGRAM_UNSENT; michael@0: chk->snd_count = 0; michael@0: chk->whoTo = NULL; michael@0: michael@0: ch = mtod(chk->data, struct sctp_chunkhdr *); michael@0: ch->chunk_type = SCTP_STREAM_RESET; michael@0: ch->chunk_flags = 0; michael@0: ch->chunk_length = htons(chk->send_size); michael@0: SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size); michael@0: offset += sizeof(struct sctp_chunkhdr); michael@0: while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) { michael@0: ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore); michael@0: if (ph == NULL) michael@0: break; michael@0: param_len = ntohs(ph->param_length); michael@0: if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) { michael@0: /* bad param */ michael@0: break; michael@0: } michael@0: ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)), michael@0: (uint8_t *)&cstore); michael@0: ptype = ntohs(ph->param_type); michael@0: num_param++; michael@0: if (param_len > (int)sizeof(cstore)) { michael@0: trunc = 1; michael@0: } else { michael@0: trunc = 0; michael@0: } michael@0: if (num_param > SCTP_MAX_RESET_PARAMS) { michael@0: /* hit the max of parameters already sorry.. */ michael@0: break; michael@0: } michael@0: if (ptype == SCTP_STR_RESET_OUT_REQUEST) { michael@0: struct sctp_stream_reset_out_request *req_out; michael@0: req_out = (struct sctp_stream_reset_out_request *)ph; michael@0: num_req++; michael@0: if (stcb->asoc.stream_reset_outstanding) { michael@0: seq = ntohl(req_out->response_seq); michael@0: if (seq == stcb->asoc.str_reset_seq_out) { michael@0: /* implicit ack */ michael@0: (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL); michael@0: } michael@0: } michael@0: sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc); michael@0: } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) { michael@0: struct sctp_stream_reset_add_strm *str_add; michael@0: str_add = (struct sctp_stream_reset_add_strm *)ph; michael@0: num_req++; michael@0: sctp_handle_str_reset_add_strm(stcb, chk, str_add); michael@0: } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) { michael@0: struct sctp_stream_reset_add_strm *str_add; michael@0: str_add = (struct sctp_stream_reset_add_strm *)ph; michael@0: num_req++; michael@0: sctp_handle_str_reset_add_out_strm(stcb, chk, str_add); michael@0: } else if (ptype == SCTP_STR_RESET_IN_REQUEST) { michael@0: struct sctp_stream_reset_in_request *req_in; michael@0: num_req++; michael@0: req_in = (struct sctp_stream_reset_in_request *)ph; michael@0: sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc); michael@0: } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) { michael@0: struct sctp_stream_reset_tsn_request *req_tsn; michael@0: num_req++; michael@0: req_tsn = (struct sctp_stream_reset_tsn_request *)ph; michael@0: if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) { michael@0: ret_code = 1; michael@0: goto strres_nochunk; michael@0: } michael@0: /* no more */ michael@0: break; michael@0: } else if (ptype == SCTP_STR_RESET_RESPONSE) { michael@0: struct sctp_stream_reset_response *resp; michael@0: uint32_t result; michael@0: resp = (struct sctp_stream_reset_response *)ph; michael@0: seq = ntohl(resp->response_seq); michael@0: result = ntohl(resp->result); michael@0: if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) { michael@0: ret_code = 1; michael@0: goto strres_nochunk; michael@0: } michael@0: } else { michael@0: break; michael@0: } michael@0: offset += SCTP_SIZE32(param_len); michael@0: chk_length -= SCTP_SIZE32(param_len); michael@0: } michael@0: if (num_req == 0) { michael@0: /* we have no response free the stuff */ michael@0: goto strres_nochunk; michael@0: } michael@0: /* ok we have a chunk to link in */ michael@0: TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, michael@0: chk, michael@0: sctp_next); michael@0: stcb->asoc.ctrl_queue_cnt++; michael@0: return (ret_code); michael@0: } michael@0: michael@0: /* michael@0: * Handle a router or endpoints report of a packet loss, there are two ways michael@0: * to handle this, either we get the whole packet and must disect it michael@0: * ourselves (possibly with truncation and or corruption) or it is a summary michael@0: * from a middle box that did the disectting for us. michael@0: */ michael@0: static void michael@0: sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp, michael@0: struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit) michael@0: { michael@0: uint32_t bottle_bw, on_queue; michael@0: uint16_t trunc_len; michael@0: unsigned int chlen; michael@0: unsigned int at; michael@0: struct sctp_chunk_desc desc; michael@0: struct sctp_chunkhdr *ch; michael@0: michael@0: chlen = ntohs(cp->ch.chunk_length); michael@0: chlen -= sizeof(struct sctp_pktdrop_chunk); michael@0: /* XXX possible chlen underflow */ michael@0: if (chlen == 0) { michael@0: ch = NULL; michael@0: if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) michael@0: SCTP_STAT_INCR(sctps_pdrpbwrpt); michael@0: } else { michael@0: ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr)); michael@0: chlen -= sizeof(struct sctphdr); michael@0: /* XXX possible chlen underflow */ michael@0: memset(&desc, 0, sizeof(desc)); michael@0: } michael@0: trunc_len = (uint16_t) ntohs(cp->trunc_len); michael@0: if (trunc_len > limit) { michael@0: trunc_len = limit; michael@0: } michael@0: michael@0: /* now the chunks themselves */ michael@0: while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) { michael@0: desc.chunk_type = ch->chunk_type; michael@0: /* get amount we need to move */ michael@0: at = ntohs(ch->chunk_length); michael@0: if (at < sizeof(struct sctp_chunkhdr)) { michael@0: /* corrupt chunk, maybe at the end? */ michael@0: SCTP_STAT_INCR(sctps_pdrpcrupt); michael@0: break; michael@0: } michael@0: if (trunc_len == 0) { michael@0: /* we are supposed to have all of it */ michael@0: if (at > chlen) { michael@0: /* corrupt skip it */ michael@0: SCTP_STAT_INCR(sctps_pdrpcrupt); michael@0: break; michael@0: } michael@0: } else { michael@0: /* is there enough of it left ? */ michael@0: if (desc.chunk_type == SCTP_DATA) { michael@0: if (chlen < (sizeof(struct sctp_data_chunk) + michael@0: sizeof(desc.data_bytes))) { michael@0: break; michael@0: } michael@0: } else { michael@0: if (chlen < sizeof(struct sctp_chunkhdr)) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: if (desc.chunk_type == SCTP_DATA) { michael@0: /* can we get out the tsn? */ michael@0: if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) michael@0: SCTP_STAT_INCR(sctps_pdrpmbda); michael@0: michael@0: if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) { michael@0: /* yep */ michael@0: struct sctp_data_chunk *dcp; michael@0: uint8_t *ddp; michael@0: unsigned int iii; michael@0: michael@0: dcp = (struct sctp_data_chunk *)ch; michael@0: ddp = (uint8_t *) (dcp + 1); michael@0: for (iii = 0; iii < sizeof(desc.data_bytes); iii++) { michael@0: desc.data_bytes[iii] = ddp[iii]; michael@0: } michael@0: desc.tsn_ifany = dcp->dp.tsn; michael@0: } else { michael@0: /* nope we are done. */ michael@0: SCTP_STAT_INCR(sctps_pdrpnedat); michael@0: break; michael@0: } michael@0: } else { michael@0: if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)) michael@0: SCTP_STAT_INCR(sctps_pdrpmbct); michael@0: } michael@0: michael@0: if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) { michael@0: SCTP_STAT_INCR(sctps_pdrppdbrk); michael@0: break; michael@0: } michael@0: if (SCTP_SIZE32(at) > chlen) { michael@0: break; michael@0: } michael@0: chlen -= SCTP_SIZE32(at); michael@0: if (chlen < sizeof(struct sctp_chunkhdr)) { michael@0: /* done, none left */ michael@0: break; michael@0: } michael@0: ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at)); michael@0: } michael@0: /* Now update any rwnd --- possibly */ michael@0: if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) { michael@0: /* From a peer, we get a rwnd report */ michael@0: uint32_t a_rwnd; michael@0: michael@0: SCTP_STAT_INCR(sctps_pdrpfehos); michael@0: michael@0: bottle_bw = ntohl(cp->bottle_bw); michael@0: on_queue = ntohl(cp->current_onq); michael@0: if (bottle_bw && on_queue) { michael@0: /* a rwnd report is in here */ michael@0: if (bottle_bw > on_queue) michael@0: a_rwnd = bottle_bw - on_queue; michael@0: else michael@0: a_rwnd = 0; michael@0: michael@0: if (a_rwnd == 0) michael@0: stcb->asoc.peers_rwnd = 0; michael@0: else { michael@0: if (a_rwnd > stcb->asoc.total_flight) { michael@0: stcb->asoc.peers_rwnd = michael@0: a_rwnd - stcb->asoc.total_flight; michael@0: } else { michael@0: stcb->asoc.peers_rwnd = 0; michael@0: } michael@0: if (stcb->asoc.peers_rwnd < michael@0: stcb->sctp_ep->sctp_ep.sctp_sws_sender) { michael@0: /* SWS sender side engages */ michael@0: stcb->asoc.peers_rwnd = 0; michael@0: } michael@0: } michael@0: } michael@0: } else { michael@0: SCTP_STAT_INCR(sctps_pdrpfmbox); michael@0: } michael@0: michael@0: /* now middle boxes in sat networks get a cwnd bump */ michael@0: if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) && michael@0: (stcb->asoc.sat_t3_loss_recovery == 0) && michael@0: (stcb->asoc.sat_network)) { michael@0: /* michael@0: * This is debateable but for sat networks it makes sense michael@0: * Note if a T3 timer has went off, we will prohibit any michael@0: * changes to cwnd until we exit the t3 loss recovery. michael@0: */ michael@0: stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb, michael@0: net, cp, &bottle_bw, &on_queue); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to michael@0: * still contain IP/SCTP header - stcb: is the tcb found for this packet - michael@0: * offset: offset into the mbuf chain to first chunkhdr - length: is the michael@0: * length of the complete packet outputs: - length: modified to remaining michael@0: * length after control processing - netp: modified to new sctp_nets after michael@0: * cookie-echo processing - return NULL to discard the packet (ie. no asoc, michael@0: * bad packet,...) otherwise return the tcb for this packet michael@0: */ michael@0: #if !defined(__Panda__) michael@0: #ifdef __GNUC__ michael@0: __attribute__ ((noinline)) michael@0: #endif michael@0: #endif michael@0: static struct sctp_tcb * michael@0: sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length, michael@0: struct sockaddr *src, struct sockaddr *dst, michael@0: struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp, michael@0: struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen, michael@0: #if defined(__FreeBSD__) michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: #endif michael@0: uint32_t vrf_id, uint16_t port) michael@0: { michael@0: struct sctp_association *asoc; michael@0: uint32_t vtag_in; michael@0: int num_chunks = 0; /* number of control chunks processed */ michael@0: uint32_t chk_length; michael@0: int ret; michael@0: int abort_no_unlock = 0; michael@0: int ecne_seen = 0; michael@0: /* michael@0: * How big should this be, and should it be alloc'd? Lets try the michael@0: * d-mtu-ceiling for now (2k) and that should hopefully work ... michael@0: * until we get into jumbo grams and such.. michael@0: */ michael@0: uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE]; michael@0: struct sctp_tcb *locked_tcb = stcb; michael@0: int got_auth = 0; michael@0: uint32_t auth_offset = 0, auth_len = 0; michael@0: int auth_skipped = 0; michael@0: int asconf_cnt = 0; michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: #endif michael@0: michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n", michael@0: iphlen, *offset, length, (void *)stcb); michael@0: michael@0: /* validate chunk header length... */ michael@0: if (ntohs(ch->chunk_length) < sizeof(*ch)) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n", michael@0: ntohs(ch->chunk_length)); michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: } michael@0: /* michael@0: * validate the verification tag michael@0: */ michael@0: vtag_in = ntohl(sh->v_tag); michael@0: michael@0: if (locked_tcb) { michael@0: SCTP_TCB_LOCK_ASSERT(locked_tcb); michael@0: } michael@0: if (ch->chunk_type == SCTP_INITIATION) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n", michael@0: ntohs(ch->chunk_length), vtag_in); michael@0: if (vtag_in != 0) { michael@0: /* protocol error- silently discard... */ michael@0: SCTP_STAT_INCR(sctps_badvtag); michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: } michael@0: } else if (ch->chunk_type != SCTP_COOKIE_ECHO) { michael@0: /* michael@0: * If there is no stcb, skip the AUTH chunk and process michael@0: * later after a stcb is found (to validate the lookup was michael@0: * valid. michael@0: */ michael@0: if ((ch->chunk_type == SCTP_AUTHENTICATION) && michael@0: (stcb == NULL) && michael@0: !SCTP_BASE_SYSCTL(sctp_auth_disable)) { michael@0: /* save this chunk for later processing */ michael@0: auth_skipped = 1; michael@0: auth_offset = *offset; michael@0: auth_len = ntohs(ch->chunk_length); michael@0: michael@0: /* (temporarily) move past this chunk */ michael@0: *offset += SCTP_SIZE32(auth_len); michael@0: if (*offset >= length) { michael@0: /* no more data left in the mbuf chain */ michael@0: *offset = length; michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: } michael@0: ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, michael@0: sizeof(struct sctp_chunkhdr), chunk_buf); michael@0: } michael@0: if (ch == NULL) { michael@0: /* Help */ michael@0: *offset = length; michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: } michael@0: if (ch->chunk_type == SCTP_COOKIE_ECHO) { michael@0: goto process_control_chunks; michael@0: } michael@0: /* michael@0: * first check if it's an ASCONF with an unknown src addr we michael@0: * need to look inside to find the association michael@0: */ michael@0: if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) { michael@0: struct sctp_chunkhdr *asconf_ch = ch; michael@0: uint32_t asconf_offset = 0, asconf_len = 0; michael@0: michael@0: /* inp's refcount may be reduced */ michael@0: SCTP_INP_INCR_REF(inp); michael@0: michael@0: asconf_offset = *offset; michael@0: do { michael@0: asconf_len = ntohs(asconf_ch->chunk_length); michael@0: if (asconf_len < sizeof(struct sctp_asconf_paramhdr)) michael@0: break; michael@0: stcb = sctp_findassociation_ep_asconf(m, michael@0: *offset, michael@0: dst, michael@0: sh, &inp, netp, vrf_id); michael@0: if (stcb != NULL) michael@0: break; michael@0: asconf_offset += SCTP_SIZE32(asconf_len); michael@0: asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset, michael@0: sizeof(struct sctp_chunkhdr), chunk_buf); michael@0: } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF); michael@0: if (stcb == NULL) { michael@0: /* michael@0: * reduce inp's refcount if not reduced in michael@0: * sctp_findassociation_ep_asconf(). michael@0: */ michael@0: SCTP_INP_DECR_REF(inp); michael@0: } else { michael@0: locked_tcb = stcb; michael@0: } michael@0: michael@0: /* now go back and verify any auth chunk to be sure */ michael@0: if (auth_skipped && (stcb != NULL)) { michael@0: struct sctp_auth_chunk *auth; michael@0: michael@0: auth = (struct sctp_auth_chunk *) michael@0: sctp_m_getptr(m, auth_offset, michael@0: auth_len, chunk_buf); michael@0: got_auth = 1; michael@0: auth_skipped = 0; michael@0: if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, michael@0: auth_offset)) { michael@0: /* auth HMAC failed so dump it */ michael@0: *offset = length; michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: } else { michael@0: /* remaining chunks are HMAC checked */ michael@0: stcb->asoc.authenticated = 1; michael@0: } michael@0: } michael@0: } michael@0: if (stcb == NULL) { michael@0: /* no association, so it's out of the blue... */ michael@0: sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: *offset = length; michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: } michael@0: asoc = &stcb->asoc; michael@0: /* ABORT and SHUTDOWN can use either v_tag... */ michael@0: if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) || michael@0: (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) || michael@0: (ch->chunk_type == SCTP_PACKET_DROPPED)) { michael@0: /* Take the T-bit always into account. */ michael@0: if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) && michael@0: (vtag_in == asoc->my_vtag)) || michael@0: (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) && michael@0: (vtag_in == asoc->peer_vtag))) { michael@0: /* this is valid */ michael@0: } else { michael@0: /* drop this packet... */ michael@0: SCTP_STAT_INCR(sctps_badvtag); michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: } michael@0: } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { michael@0: if (vtag_in != asoc->my_vtag) { michael@0: /* michael@0: * this could be a stale SHUTDOWN-ACK or the michael@0: * peer never got the SHUTDOWN-COMPLETE and michael@0: * is still hung; we have started a new asoc michael@0: * but it won't complete until the shutdown michael@0: * is completed michael@0: */ michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: sctp_handle_ootb(m, iphlen, *offset, src, dst, michael@0: sh, inp, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: return (NULL); michael@0: } michael@0: } else { michael@0: /* for all other chunks, vtag must match */ michael@0: if (vtag_in != asoc->my_vtag) { michael@0: /* invalid vtag... */ michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, michael@0: "invalid vtag: %xh, expect %xh\n", michael@0: vtag_in, asoc->my_vtag); michael@0: SCTP_STAT_INCR(sctps_badvtag); michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: } michael@0: } /* end if !SCTP_COOKIE_ECHO */ michael@0: /* michael@0: * process all control chunks... michael@0: */ michael@0: if (((ch->chunk_type == SCTP_SELECTIVE_ACK) || michael@0: (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) || michael@0: (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) && michael@0: (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { michael@0: /* implied cookie-ack.. we must have lost the ack */ michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { michael@0: sctp_misc_ints(SCTP_THRESHOLD_CLEAR, michael@0: stcb->asoc.overall_error_count, michael@0: 0, michael@0: SCTP_FROM_SCTP_INPUT, michael@0: __LINE__); michael@0: } michael@0: stcb->asoc.overall_error_count = 0; michael@0: sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, michael@0: *netp); michael@0: } michael@0: michael@0: process_control_chunks: michael@0: while (IS_SCTP_CONTROL(ch)) { michael@0: /* validate chunk length */ michael@0: chk_length = ntohs(ch->chunk_length); michael@0: SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n", michael@0: ch->chunk_type, chk_length); michael@0: SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length); michael@0: if (chk_length < sizeof(*ch) || michael@0: (*offset + (int)chk_length) > length) { michael@0: *offset = length; michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: } michael@0: SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks); michael@0: /* michael@0: * INIT-ACK only gets the init ack "header" portion only michael@0: * because we don't have to process the peer's COOKIE. All michael@0: * others get a complete chunk. michael@0: */ michael@0: if ((ch->chunk_type == SCTP_INITIATION_ACK) || michael@0: (ch->chunk_type == SCTP_INITIATION)) { michael@0: /* get an init-ack chunk */ michael@0: ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, michael@0: sizeof(struct sctp_init_ack_chunk), chunk_buf); michael@0: if (ch == NULL) { michael@0: *offset = length; michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: } michael@0: } else { michael@0: /* For cookies and all other chunks. */ michael@0: if (chk_length > sizeof(chunk_buf)) { michael@0: /* michael@0: * use just the size of the chunk buffer michael@0: * so the front part of our chunks fit in michael@0: * contiguous space up to the chunk buffer michael@0: * size (508 bytes). michael@0: * For chunks that need to get more than that michael@0: * they must use the sctp_m_getptr() function michael@0: * or other means (e.g. know how to parse mbuf michael@0: * chains). Cookies do this already. michael@0: */ michael@0: ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, michael@0: (sizeof(chunk_buf) - 4), michael@0: chunk_buf); michael@0: if (ch == NULL) { michael@0: *offset = length; michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: } michael@0: } else { michael@0: /* We can fit it all */ michael@0: ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, michael@0: chk_length, chunk_buf); michael@0: if (ch == NULL) { michael@0: SCTP_PRINTF("sctp_process_control: Can't get the all data....\n"); michael@0: *offset = length; michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: } michael@0: } michael@0: } michael@0: num_chunks++; michael@0: /* Save off the last place we got a control from */ michael@0: if (stcb != NULL) { michael@0: if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) { michael@0: /* michael@0: * allow last_control to be NULL if michael@0: * ASCONF... ASCONF processing will find the michael@0: * right net later michael@0: */ michael@0: if ((netp != NULL) && (*netp != NULL)) michael@0: stcb->asoc.last_control_chunk_from = *netp; michael@0: } michael@0: } michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_audit_log(0xB0, ch->chunk_type); michael@0: #endif michael@0: michael@0: /* check to see if this chunk required auth, but isn't */ michael@0: if ((stcb != NULL) && michael@0: !SCTP_BASE_SYSCTL(sctp_auth_disable) && michael@0: sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) && michael@0: !stcb->asoc.authenticated) { michael@0: /* "silently" ignore */ michael@0: SCTP_STAT_INCR(sctps_recvauthmissing); michael@0: goto next_chunk; michael@0: } michael@0: switch (ch->chunk_type) { michael@0: case SCTP_INITIATION: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n"); michael@0: /* The INIT chunk must be the only chunk. */ michael@0: if ((num_chunks > 1) || michael@0: (length - *offset > (int)SCTP_SIZE32(chk_length))) { michael@0: sctp_abort_association(inp, stcb, m, iphlen, michael@0: src, dst, sh, NULL, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: /* Honor our resource limit. */ michael@0: if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) { michael@0: struct mbuf *op_err; michael@0: michael@0: op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); michael@0: sctp_abort_association(inp, stcb, m, iphlen, michael@0: src, dst, sh, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: sctp_handle_init(m, iphlen, *offset, src, dst, sh, michael@0: (struct sctp_init_chunk *)ch, inp, michael@0: stcb, &abort_no_unlock, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: *offset = length; michael@0: if ((!abort_no_unlock) && (locked_tcb)) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: break; michael@0: case SCTP_PAD_CHUNK: michael@0: break; michael@0: case SCTP_INITIATION_ACK: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n"); michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { michael@0: /* We are not interested anymore */ michael@0: if ((stcb) && (stcb->asoc.total_output_queue_size)) { michael@0: ; michael@0: } else { michael@0: if (locked_tcb != stcb) { michael@0: /* Very unlikely */ michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: *offset = length; michael@0: if (stcb) { michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: so = SCTP_INP_SO(inp); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: #endif michael@0: (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_27); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: } michael@0: return (NULL); michael@0: } michael@0: } michael@0: /* The INIT-ACK chunk must be the only chunk. */ michael@0: if ((num_chunks > 1) || michael@0: (length - *offset > (int)SCTP_SIZE32(chk_length))) { michael@0: *offset = length; michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: } michael@0: if ((netp) && (*netp)) { michael@0: ret = sctp_handle_init_ack(m, iphlen, *offset, michael@0: src, dst, sh, michael@0: (struct sctp_init_ack_chunk *)ch, michael@0: stcb, *netp, michael@0: &abort_no_unlock, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id); michael@0: } else { michael@0: ret = -1; michael@0: } michael@0: *offset = length; michael@0: if (abort_no_unlock) { michael@0: return (NULL); michael@0: } michael@0: /* michael@0: * Special case, I must call the output routine to michael@0: * get the cookie echoed michael@0: */ michael@0: if ((stcb != NULL) && (ret == 0)) { michael@0: sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: break; michael@0: case SCTP_SELECTIVE_ACK: michael@0: { michael@0: struct sctp_sack_chunk *sack; michael@0: int abort_now = 0; michael@0: uint32_t a_rwnd, cum_ack; michael@0: uint16_t num_seg, num_dup; michael@0: uint8_t flags; michael@0: int offset_seg, offset_dup; michael@0: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n"); michael@0: SCTP_STAT_INCR(sctps_recvsacks); michael@0: if (stcb == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n"); michael@0: break; michael@0: } michael@0: if (chk_length < sizeof(struct sctp_sack_chunk)) { michael@0: SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n"); michael@0: break; michael@0: } michael@0: if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { michael@0: /*- michael@0: * If we have sent a shutdown-ack, we will pay no michael@0: * attention to a sack sent in to us since michael@0: * we don't care anymore. michael@0: */ michael@0: break; michael@0: } michael@0: sack = (struct sctp_sack_chunk *)ch; michael@0: flags = ch->chunk_flags; michael@0: cum_ack = ntohl(sack->sack.cum_tsn_ack); michael@0: num_seg = ntohs(sack->sack.num_gap_ack_blks); michael@0: num_dup = ntohs(sack->sack.num_dup_tsns); michael@0: a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd); michael@0: if (sizeof(struct sctp_sack_chunk) + michael@0: num_seg * sizeof(struct sctp_gap_ack_block) + michael@0: num_dup * sizeof(uint32_t) != chk_length) { michael@0: SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n"); michael@0: break; michael@0: } michael@0: offset_seg = *offset + sizeof(struct sctp_sack_chunk); michael@0: offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", michael@0: cum_ack, num_seg, a_rwnd); michael@0: stcb->asoc.seen_a_sack_this_pkt = 1; michael@0: if ((stcb->asoc.pr_sctp_cnt == 0) && michael@0: (num_seg == 0) && michael@0: SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && michael@0: (stcb->asoc.saw_sack_with_frags == 0) && michael@0: (stcb->asoc.saw_sack_with_nr_frags == 0) && michael@0: (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) michael@0: ) { michael@0: /* We have a SIMPLE sack having no prior segments and michael@0: * data on sent queue to be acked.. Use the faster michael@0: * path sack processing. We also allow window update michael@0: * sacks with no missing segments to go this way too. michael@0: */ michael@0: sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen); michael@0: } else { michael@0: if (netp && *netp) michael@0: sctp_handle_sack(m, offset_seg, offset_dup, stcb, michael@0: num_seg, 0, num_dup, &abort_now, flags, michael@0: cum_ack, a_rwnd, ecne_seen); michael@0: } michael@0: if (abort_now) { michael@0: /* ABORT signal from sack processing */ michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: if (TAILQ_EMPTY(&stcb->asoc.send_queue) && michael@0: TAILQ_EMPTY(&stcb->asoc.sent_queue) && michael@0: (stcb->asoc.stream_queue_cnt == 0)) { michael@0: sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: } michael@0: break; michael@0: /* EY - nr_sack: If the received chunk is an nr_sack chunk */ michael@0: case SCTP_NR_SELECTIVE_ACK: michael@0: { michael@0: struct sctp_nr_sack_chunk *nr_sack; michael@0: int abort_now = 0; michael@0: uint32_t a_rwnd, cum_ack; michael@0: uint16_t num_seg, num_nr_seg, num_dup; michael@0: uint8_t flags; michael@0: int offset_seg, offset_dup; michael@0: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n"); michael@0: SCTP_STAT_INCR(sctps_recvsacks); michael@0: if (stcb == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n"); michael@0: break; michael@0: } michael@0: if ((stcb->asoc.sctp_nr_sack_on_off == 0) || michael@0: (stcb->asoc.peer_supports_nr_sack == 0)) { michael@0: goto unknown_chunk; michael@0: } michael@0: if (chk_length < sizeof(struct sctp_nr_sack_chunk)) { michael@0: SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n"); michael@0: break; michael@0: } michael@0: if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) { michael@0: /*- michael@0: * If we have sent a shutdown-ack, we will pay no michael@0: * attention to a sack sent in to us since michael@0: * we don't care anymore. michael@0: */ michael@0: break; michael@0: } michael@0: nr_sack = (struct sctp_nr_sack_chunk *)ch; michael@0: flags = ch->chunk_flags; michael@0: cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack); michael@0: num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks); michael@0: num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks); michael@0: num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns); michael@0: a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd); michael@0: if (sizeof(struct sctp_nr_sack_chunk) + michael@0: (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) + michael@0: num_dup * sizeof(uint32_t) != chk_length) { michael@0: SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n"); michael@0: break; michael@0: } michael@0: offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk); michael@0: offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block); michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n", michael@0: cum_ack, num_seg, a_rwnd); michael@0: stcb->asoc.seen_a_sack_this_pkt = 1; michael@0: if ((stcb->asoc.pr_sctp_cnt == 0) && michael@0: (num_seg == 0) && (num_nr_seg == 0) && michael@0: SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) && michael@0: (stcb->asoc.saw_sack_with_frags == 0) && michael@0: (stcb->asoc.saw_sack_with_nr_frags == 0) && michael@0: (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) { michael@0: /* michael@0: * We have a SIMPLE sack having no michael@0: * prior segments and data on sent michael@0: * queue to be acked. Use the michael@0: * faster path sack processing. We michael@0: * also allow window update sacks michael@0: * with no missing segments to go michael@0: * this way too. michael@0: */ michael@0: sctp_express_handle_sack(stcb, cum_ack, a_rwnd, michael@0: &abort_now, ecne_seen); michael@0: } else { michael@0: if (netp && *netp) michael@0: sctp_handle_sack(m, offset_seg, offset_dup, stcb, michael@0: num_seg, num_nr_seg, num_dup, &abort_now, flags, michael@0: cum_ack, a_rwnd, ecne_seen); michael@0: } michael@0: if (abort_now) { michael@0: /* ABORT signal from sack processing */ michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: if (TAILQ_EMPTY(&stcb->asoc.send_queue) && michael@0: TAILQ_EMPTY(&stcb->asoc.sent_queue) && michael@0: (stcb->asoc.stream_queue_cnt == 0)) { michael@0: sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: } michael@0: break; michael@0: michael@0: case SCTP_HEARTBEAT_REQUEST: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n"); michael@0: if ((stcb) && netp && *netp) { michael@0: SCTP_STAT_INCR(sctps_recvheartbeat); michael@0: sctp_send_heartbeat_ack(stcb, m, *offset, michael@0: chk_length, *netp); michael@0: michael@0: /* He's alive so give him credit */ michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { michael@0: sctp_misc_ints(SCTP_THRESHOLD_CLEAR, michael@0: stcb->asoc.overall_error_count, michael@0: 0, michael@0: SCTP_FROM_SCTP_INPUT, michael@0: __LINE__); michael@0: } michael@0: stcb->asoc.overall_error_count = 0; michael@0: } michael@0: break; michael@0: case SCTP_HEARTBEAT_ACK: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n"); michael@0: if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) { michael@0: /* Its not ours */ michael@0: *offset = length; michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: } michael@0: /* He's alive so give him credit */ michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { michael@0: sctp_misc_ints(SCTP_THRESHOLD_CLEAR, michael@0: stcb->asoc.overall_error_count, michael@0: 0, michael@0: SCTP_FROM_SCTP_INPUT, michael@0: __LINE__); michael@0: } michael@0: stcb->asoc.overall_error_count = 0; michael@0: SCTP_STAT_INCR(sctps_recvheartbeatack); michael@0: if (netp && *netp) michael@0: sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch, michael@0: stcb, *netp); michael@0: break; michael@0: case SCTP_ABORT_ASSOCIATION: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n", michael@0: (void *)stcb); michael@0: if ((stcb) && netp && *netp) michael@0: sctp_handle_abort((struct sctp_abort_chunk *)ch, michael@0: stcb, *netp); michael@0: *offset = length; michael@0: return (NULL); michael@0: break; michael@0: case SCTP_SHUTDOWN: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n", michael@0: (void *)stcb); michael@0: if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) { michael@0: *offset = length; michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: } michael@0: if (netp && *netp) { michael@0: int abort_flag = 0; michael@0: michael@0: sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch, michael@0: stcb, *netp, &abort_flag); michael@0: if (abort_flag) { michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: } michael@0: break; michael@0: case SCTP_SHUTDOWN_ACK: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", (void *)stcb); michael@0: if ((stcb) && (netp) && (*netp)) michael@0: sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp); michael@0: *offset = length; michael@0: return (NULL); michael@0: break; michael@0: michael@0: case SCTP_OPERATION_ERROR: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n"); michael@0: if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) { michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: break; michael@0: case SCTP_COOKIE_ECHO: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, michael@0: "SCTP_COOKIE-ECHO, stcb %p\n", (void *)stcb); michael@0: if ((stcb) && (stcb->asoc.total_output_queue_size)) { michael@0: ; michael@0: } else { michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { michael@0: /* We are not interested anymore */ michael@0: abend: michael@0: if (stcb) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: } michael@0: /* michael@0: * First are we accepting? We do this again here michael@0: * since it is possible that a previous endpoint WAS michael@0: * listening responded to a INIT-ACK and then michael@0: * closed. We opened and bound.. and are now no michael@0: * longer listening. michael@0: */ michael@0: michael@0: if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) { michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && michael@0: (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) { michael@0: struct mbuf *op_err; michael@0: michael@0: op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC); michael@0: sctp_abort_association(inp, stcb, m, iphlen, michael@0: src, dst, sh, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: } michael@0: *offset = length; michael@0: return (NULL); michael@0: } else { michael@0: struct mbuf *ret_buf; michael@0: struct sctp_inpcb *linp; michael@0: if (stcb) { michael@0: linp = NULL; michael@0: } else { michael@0: linp = inp; michael@0: } michael@0: michael@0: if (linp) { michael@0: SCTP_ASOC_CREATE_LOCK(linp); michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { michael@0: SCTP_ASOC_CREATE_UNLOCK(linp); michael@0: goto abend; michael@0: } michael@0: } michael@0: michael@0: if (netp) { michael@0: ret_buf = michael@0: sctp_handle_cookie_echo(m, iphlen, michael@0: *offset, michael@0: src, dst, michael@0: sh, michael@0: (struct sctp_cookie_echo_chunk *)ch, michael@0: &inp, &stcb, netp, michael@0: auth_skipped, michael@0: auth_offset, michael@0: auth_len, michael@0: &locked_tcb, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, michael@0: mflowid, michael@0: #endif michael@0: vrf_id, michael@0: port); michael@0: } else { michael@0: ret_buf = NULL; michael@0: } michael@0: if (linp) { michael@0: SCTP_ASOC_CREATE_UNLOCK(linp); michael@0: } michael@0: if (ret_buf == NULL) { michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, michael@0: "GAK, null buffer\n"); michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: /* if AUTH skipped, see if it verified... */ michael@0: if (auth_skipped) { michael@0: got_auth = 1; michael@0: auth_skipped = 0; michael@0: } michael@0: if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) { michael@0: /* michael@0: * Restart the timer if we have michael@0: * pending data michael@0: */ michael@0: struct sctp_tmit_chunk *chk; michael@0: michael@0: chk = TAILQ_FIRST(&stcb->asoc.sent_queue); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo); michael@0: } michael@0: } michael@0: break; michael@0: case SCTP_COOKIE_ACK: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", (void *)stcb); michael@0: if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) { michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: } michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { michael@0: /* We are not interested anymore */ michael@0: if ((stcb) && (stcb->asoc.total_output_queue_size)) { michael@0: ; michael@0: } else if (stcb) { michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: so = SCTP_INP_SO(inp); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: #endif michael@0: (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_27); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: } michael@0: /* He's alive so give him credit */ michael@0: if ((stcb) && netp && *netp) { michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { michael@0: sctp_misc_ints(SCTP_THRESHOLD_CLEAR, michael@0: stcb->asoc.overall_error_count, michael@0: 0, michael@0: SCTP_FROM_SCTP_INPUT, michael@0: __LINE__); michael@0: } michael@0: stcb->asoc.overall_error_count = 0; michael@0: sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch,stcb, *netp); michael@0: } michael@0: break; michael@0: case SCTP_ECN_ECHO: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n"); michael@0: /* He's alive so give him credit */ michael@0: if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) { michael@0: /* Its not ours */ michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: if (stcb) { michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { michael@0: sctp_misc_ints(SCTP_THRESHOLD_CLEAR, michael@0: stcb->asoc.overall_error_count, michael@0: 0, michael@0: SCTP_FROM_SCTP_INPUT, michael@0: __LINE__); michael@0: } michael@0: stcb->asoc.overall_error_count = 0; michael@0: sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, michael@0: stcb); michael@0: ecne_seen = 1; michael@0: } michael@0: break; michael@0: case SCTP_ECN_CWR: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n"); michael@0: /* He's alive so give him credit */ michael@0: if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) { michael@0: /* Its not ours */ michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: if (stcb) { michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { michael@0: sctp_misc_ints(SCTP_THRESHOLD_CLEAR, michael@0: stcb->asoc.overall_error_count, michael@0: 0, michael@0: SCTP_FROM_SCTP_INPUT, michael@0: __LINE__); michael@0: } michael@0: stcb->asoc.overall_error_count = 0; michael@0: sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp); michael@0: } michael@0: break; michael@0: case SCTP_SHUTDOWN_COMPLETE: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", (void *)stcb); michael@0: /* must be first and only chunk */ michael@0: if ((num_chunks > 1) || michael@0: (length - *offset > (int)SCTP_SIZE32(chk_length))) { michael@0: *offset = length; michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: return (NULL); michael@0: } michael@0: if ((stcb) && netp && *netp) { michael@0: sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch, michael@0: stcb, *netp); michael@0: } michael@0: *offset = length; michael@0: return (NULL); michael@0: break; michael@0: case SCTP_ASCONF: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n"); michael@0: /* He's alive so give him credit */ michael@0: if (stcb) { michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { michael@0: sctp_misc_ints(SCTP_THRESHOLD_CLEAR, michael@0: stcb->asoc.overall_error_count, michael@0: 0, michael@0: SCTP_FROM_SCTP_INPUT, michael@0: __LINE__); michael@0: } michael@0: stcb->asoc.overall_error_count = 0; michael@0: sctp_handle_asconf(m, *offset, src, michael@0: (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0); michael@0: asconf_cnt++; michael@0: } michael@0: break; michael@0: case SCTP_ASCONF_ACK: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n"); michael@0: if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) { michael@0: /* Its not ours */ michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: if ((stcb) && netp && *netp) { michael@0: /* He's alive so give him credit */ michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { michael@0: sctp_misc_ints(SCTP_THRESHOLD_CLEAR, michael@0: stcb->asoc.overall_error_count, michael@0: 0, michael@0: SCTP_FROM_SCTP_INPUT, michael@0: __LINE__); michael@0: } michael@0: stcb->asoc.overall_error_count = 0; michael@0: sctp_handle_asconf_ack(m, *offset, michael@0: (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock); michael@0: if (abort_no_unlock) michael@0: return (NULL); michael@0: } michael@0: break; michael@0: case SCTP_FORWARD_CUM_TSN: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n"); michael@0: if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) { michael@0: /* Its not ours */ michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: michael@0: /* He's alive so give him credit */ michael@0: if (stcb) { michael@0: int abort_flag = 0; michael@0: stcb->asoc.overall_error_count = 0; michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { michael@0: sctp_misc_ints(SCTP_THRESHOLD_CLEAR, michael@0: stcb->asoc.overall_error_count, michael@0: 0, michael@0: SCTP_FROM_SCTP_INPUT, michael@0: __LINE__); michael@0: } michael@0: *fwd_tsn_seen = 1; michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { michael@0: /* We are not interested anymore */ michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: so = SCTP_INP_SO(inp); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: #endif michael@0: (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_29); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: sctp_handle_forward_tsn(stcb, michael@0: (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset); michael@0: if (abort_flag) { michael@0: *offset = length; michael@0: return (NULL); michael@0: } else { michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { michael@0: sctp_misc_ints(SCTP_THRESHOLD_CLEAR, michael@0: stcb->asoc.overall_error_count, michael@0: 0, michael@0: SCTP_FROM_SCTP_INPUT, michael@0: __LINE__); michael@0: } michael@0: stcb->asoc.overall_error_count = 0; michael@0: } michael@0: michael@0: } michael@0: break; michael@0: case SCTP_STREAM_RESET: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n"); michael@0: if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) { michael@0: /* Its not ours */ michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: if (stcb->asoc.peer_supports_strreset == 0) { michael@0: /* michael@0: * hmm, peer should have announced this, but michael@0: * we will turn it on since he is sending us michael@0: * a stream reset. michael@0: */ michael@0: stcb->asoc.peer_supports_strreset = 1; michael@0: } michael@0: if (sctp_handle_stream_reset(stcb, m, *offset, ch)) { michael@0: /* stop processing */ michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: break; michael@0: case SCTP_PACKET_DROPPED: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n"); michael@0: /* re-get it all please */ michael@0: if (chk_length < sizeof(struct sctp_pktdrop_chunk)) { michael@0: /* Its not ours */ michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: michael@0: michael@0: if (ch && (stcb) && netp && (*netp)) { michael@0: sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch, michael@0: stcb, *netp, michael@0: min(chk_length, (sizeof(chunk_buf) - 4))); michael@0: michael@0: } michael@0: michael@0: break; michael@0: michael@0: case SCTP_AUTHENTICATION: michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n"); michael@0: if (SCTP_BASE_SYSCTL(sctp_auth_disable)) michael@0: goto unknown_chunk; michael@0: michael@0: if (stcb == NULL) { michael@0: /* save the first AUTH for later processing */ michael@0: if (auth_skipped == 0) { michael@0: auth_offset = *offset; michael@0: auth_len = chk_length; michael@0: auth_skipped = 1; michael@0: } michael@0: /* skip this chunk (temporarily) */ michael@0: goto next_chunk; michael@0: } michael@0: if ((chk_length < (sizeof(struct sctp_auth_chunk))) || michael@0: (chk_length > (sizeof(struct sctp_auth_chunk) + michael@0: SCTP_AUTH_DIGEST_LEN_MAX))) { michael@0: /* Its not ours */ michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: if (got_auth == 1) { michael@0: /* skip this chunk... it's already auth'd */ michael@0: goto next_chunk; michael@0: } michael@0: got_auth = 1; michael@0: if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, michael@0: m, *offset)) { michael@0: /* auth HMAC failed so dump the packet */ michael@0: *offset = length; michael@0: return (stcb); michael@0: } else { michael@0: /* remaining chunks are HMAC checked */ michael@0: stcb->asoc.authenticated = 1; michael@0: } michael@0: break; michael@0: michael@0: default: michael@0: unknown_chunk: michael@0: /* it's an unknown chunk! */ michael@0: if ((ch->chunk_type & 0x40) && (stcb != NULL)) { michael@0: struct mbuf *mm; michael@0: struct sctp_paramhdr *phd; michael@0: michael@0: mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), michael@0: 0, M_NOWAIT, 1, MT_DATA); michael@0: if (mm) { michael@0: phd = mtod(mm, struct sctp_paramhdr *); michael@0: /* michael@0: * We cheat and use param type since michael@0: * we did not bother to define a michael@0: * error cause struct. They are the michael@0: * same basic format with different michael@0: * names. michael@0: */ michael@0: phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK); michael@0: phd->param_length = htons(chk_length + sizeof(*phd)); michael@0: SCTP_BUF_LEN(mm) = sizeof(*phd); michael@0: SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, chk_length, M_NOWAIT); michael@0: if (SCTP_BUF_NEXT(mm)) { michael@0: if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(mm), SCTP_SIZE32(chk_length) - chk_length, NULL)) { michael@0: sctp_m_freem(mm); michael@0: } else { michael@0: #ifdef SCTP_MBUF_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { michael@0: struct mbuf *mat; michael@0: michael@0: for (mat = SCTP_BUF_NEXT(mm); mat; mat = SCTP_BUF_NEXT(mat)) { michael@0: if (SCTP_BUF_IS_EXTENDED(mat)) { michael@0: sctp_log_mb(mat, SCTP_MBUF_ICOPY); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: sctp_queue_op_err(stcb, mm); michael@0: } michael@0: } else { michael@0: sctp_m_freem(mm); michael@0: } michael@0: } michael@0: } michael@0: if ((ch->chunk_type & 0x80) == 0) { michael@0: /* discard this packet */ michael@0: *offset = length; michael@0: return (stcb); michael@0: } /* else skip this bad chunk and continue... */ michael@0: break; michael@0: } /* switch (ch->chunk_type) */ michael@0: michael@0: michael@0: next_chunk: michael@0: /* get the next chunk */ michael@0: *offset += SCTP_SIZE32(chk_length); michael@0: if (*offset >= length) { michael@0: /* no more data left in the mbuf chain */ michael@0: break; michael@0: } michael@0: ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset, michael@0: sizeof(struct sctp_chunkhdr), chunk_buf); michael@0: if (ch == NULL) { michael@0: if (locked_tcb) { michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: *offset = length; michael@0: return (NULL); michael@0: } michael@0: } /* while */ michael@0: michael@0: if (asconf_cnt > 0 && stcb != NULL) { michael@0: sctp_send_asconf_ack(stcb); michael@0: } michael@0: return (stcb); michael@0: } michael@0: michael@0: michael@0: #ifdef INVARIANTS michael@0: #ifdef __GNUC__ michael@0: __attribute__((noinline)) michael@0: #endif michael@0: void michael@0: sctp_validate_no_locks(struct sctp_inpcb *inp) michael@0: { michael@0: #ifndef __APPLE__ michael@0: struct sctp_tcb *lstcb; michael@0: michael@0: LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) { michael@0: if (mtx_owned(&lstcb->tcb_mtx)) { michael@0: panic("Own lock on stcb at return from input"); michael@0: } michael@0: } michael@0: if (mtx_owned(&inp->inp_create_mtx)) { michael@0: panic("Own create lock on inp"); michael@0: } michael@0: if (mtx_owned(&inp->inp_mtx)) { michael@0: panic("Own inp lock on inp"); michael@0: } michael@0: #endif michael@0: } michael@0: #endif michael@0: michael@0: /* michael@0: * common input chunk processing (v4 and v6) michael@0: */ michael@0: void michael@0: sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length, michael@0: struct sockaddr *src, struct sockaddr *dst, michael@0: struct sctphdr *sh, struct sctp_chunkhdr *ch, michael@0: #if !defined(SCTP_WITH_NO_CSUM) michael@0: uint8_t compute_crc, michael@0: #endif michael@0: uint8_t ecn_bits, michael@0: #if defined(__FreeBSD__) michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: #endif michael@0: uint32_t vrf_id, uint16_t port) michael@0: { michael@0: uint32_t high_tsn; michael@0: int fwd_tsn_seen = 0, data_processed = 0; michael@0: struct mbuf *m = *mm; michael@0: int un_sent; michael@0: int cnt_ctrl_ready = 0; michael@0: struct sctp_inpcb *inp = NULL, *inp_decr = NULL; michael@0: struct sctp_tcb *stcb = NULL; michael@0: struct sctp_nets *net = NULL; michael@0: michael@0: SCTP_STAT_INCR(sctps_recvdatagrams); michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_audit_log(0xE0, 1); michael@0: sctp_auditing(0, inp, stcb, net); michael@0: #endif michael@0: #if !defined(SCTP_WITH_NO_CSUM) michael@0: if (compute_crc != 0) { michael@0: uint32_t check, calc_check; michael@0: michael@0: check = sh->checksum; michael@0: sh->checksum = 0; michael@0: calc_check = sctp_calculate_cksum(m, iphlen); michael@0: sh->checksum = check; michael@0: if (calc_check != check) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", michael@0: calc_check, check, (void *)m, length, iphlen); michael@0: stcb = sctp_findassociation_addr(m, offset, src, dst, michael@0: sh, ch, &inp, &net, vrf_id); michael@0: if ((net != NULL) && (port != 0)) { michael@0: if (net->port == 0) { michael@0: sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); michael@0: } michael@0: net->port = port; michael@0: } michael@0: #if defined(__FreeBSD__) michael@0: if ((net != NULL) && (use_mflowid != 0)) { michael@0: net->flowid = mflowid; michael@0: #ifdef INVARIANTS michael@0: net->flowidset = 1; michael@0: #endif michael@0: } michael@0: #endif michael@0: if ((inp != NULL) && (stcb != NULL)) { michael@0: sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1); michael@0: sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); michael@0: } else if ((inp != NULL) && (stcb == NULL)) { michael@0: inp_decr = inp; michael@0: } michael@0: SCTP_STAT_INCR(sctps_badsum); michael@0: SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); michael@0: goto out; michael@0: } michael@0: } michael@0: #endif michael@0: /* Destination port of 0 is illegal, based on RFC4960. */ michael@0: if (sh->dest_port == 0) { michael@0: SCTP_STAT_INCR(sctps_hdrops); michael@0: goto out; michael@0: } michael@0: stcb = sctp_findassociation_addr(m, offset, src, dst, michael@0: sh, ch, &inp, &net, vrf_id); michael@0: if ((net != NULL) && (port != 0)) { michael@0: if (net->port == 0) { michael@0: sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); michael@0: } michael@0: net->port = port; michael@0: } michael@0: #if defined(__FreeBSD__) michael@0: if ((net != NULL) && (use_mflowid != 0)) { michael@0: net->flowid = mflowid; michael@0: #ifdef INVARIANTS michael@0: net->flowidset = 1; michael@0: #endif michael@0: } michael@0: #endif michael@0: if (inp == NULL) { michael@0: SCTP_STAT_INCR(sctps_noport); michael@0: #if defined(__FreeBSD__) && (((__FreeBSD_version < 900000) && (__FreeBSD_version >= 804000)) || (__FreeBSD_version > 900000)) michael@0: if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) { michael@0: goto out; michael@0: } michael@0: #endif michael@0: if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { michael@0: sctp_send_shutdown_complete2(src, dst, sh, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: goto out; michael@0: } michael@0: if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { michael@0: goto out; michael@0: } michael@0: if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) { michael@0: if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) || michael@0: ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) && michael@0: (ch->chunk_type != SCTP_INIT))) { michael@0: sctp_send_abort(m, iphlen, src, dst, michael@0: sh, 0, NULL, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: } michael@0: } michael@0: goto out; michael@0: } else if (stcb == NULL) { michael@0: inp_decr = inp; michael@0: } michael@0: #ifdef IPSEC michael@0: /*- michael@0: * I very much doubt any of the IPSEC stuff will work but I have no michael@0: * idea, so I will leave it in place. michael@0: */ michael@0: if (inp != NULL) { michael@0: switch (dst->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: if (ipsec4_in_reject(m, &inp->ip_inp.inp)) { michael@0: #if defined(__FreeBSD__) && (__FreeBSD_version > 1000036) michael@0: IPSECSTAT_INC(ips_in_polvio); michael@0: #else michael@0: MODULE_GLOBAL(ipsec4stat).in_polvio++; michael@0: #endif michael@0: SCTP_STAT_INCR(sctps_hdrops); michael@0: goto out; michael@0: } michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: if (ipsec6_in_reject(m, &inp->ip_inp.inp)) { michael@0: #if defined(__FreeBSD__) && (__FreeBSD_version > 1000036) michael@0: IPSEC6STAT_INC(ips_in_polvio); michael@0: #else michael@0: MODULE_GLOBAL(ipsec6stat).in_polvio++; michael@0: #endif michael@0: SCTP_STAT_INCR(sctps_hdrops); michael@0: goto out; michael@0: } michael@0: break; michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n", michael@0: (void *)m, iphlen, offset, length, (void *)stcb); michael@0: if (stcb) { michael@0: /* always clear this before beginning a packet */ michael@0: stcb->asoc.authenticated = 0; michael@0: stcb->asoc.seen_a_sack_this_pkt = 0; michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n", michael@0: (void *)stcb, stcb->asoc.state); michael@0: michael@0: if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) || michael@0: (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { michael@0: /*- michael@0: * If we hit here, we had a ref count michael@0: * up when the assoc was aborted and the michael@0: * timer is clearing out the assoc, we should michael@0: * NOT respond to any packet.. its OOTB. michael@0: */ michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: stcb = NULL; michael@0: sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: goto out; michael@0: } michael@0: michael@0: } michael@0: if (IS_SCTP_CONTROL(ch)) { michael@0: /* process the control portion of the SCTP packet */ michael@0: /* sa_ignore NO_NULL_CHK */ michael@0: stcb = sctp_process_control(m, iphlen, &offset, length, michael@0: src, dst, sh, ch, michael@0: inp, stcb, &net, &fwd_tsn_seen, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: if (stcb) { michael@0: /* This covers us if the cookie-echo was there michael@0: * and it changes our INP. michael@0: */ michael@0: inp = stcb->sctp_ep; michael@0: if ((net) && (port)) { michael@0: if (net->port == 0) { michael@0: sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr)); michael@0: } michael@0: net->port = port; michael@0: } michael@0: } michael@0: } else { michael@0: /* michael@0: * no control chunks, so pre-process DATA chunks (these michael@0: * checks are taken care of by control processing) michael@0: */ michael@0: michael@0: /* michael@0: * if DATA only packet, and auth is required, then punt... michael@0: * can't have authenticated without any AUTH (control) michael@0: * chunks michael@0: */ michael@0: if ((stcb != NULL) && michael@0: !SCTP_BASE_SYSCTL(sctp_auth_disable) && michael@0: sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) { michael@0: /* "silently" ignore */ michael@0: SCTP_STAT_INCR(sctps_recvauthmissing); michael@0: goto out; michael@0: } michael@0: if (stcb == NULL) { michael@0: /* out of the blue DATA chunk */ michael@0: sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: goto out; michael@0: } michael@0: if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) { michael@0: /* v_tag mismatch! */ michael@0: SCTP_STAT_INCR(sctps_badvtag); michael@0: goto out; michael@0: } michael@0: } michael@0: michael@0: if (stcb == NULL) { michael@0: /* michael@0: * no valid TCB for this packet, or we found it's a bad michael@0: * packet while processing control, or we're done with this michael@0: * packet (done or skip rest of data), so we drop it... michael@0: */ michael@0: goto out; michael@0: } michael@0: michael@0: /* michael@0: * DATA chunk processing michael@0: */ michael@0: /* plow through the data chunks while length > offset */ michael@0: michael@0: /* michael@0: * Rest should be DATA only. Check authentication state if AUTH for michael@0: * DATA is required. michael@0: */ michael@0: if ((length > offset) && michael@0: (stcb != NULL) && michael@0: !SCTP_BASE_SYSCTL(sctp_auth_disable) && michael@0: sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) && michael@0: !stcb->asoc.authenticated) { michael@0: /* "silently" ignore */ michael@0: SCTP_STAT_INCR(sctps_recvauthmissing); michael@0: SCTPDBG(SCTP_DEBUG_AUTH1, michael@0: "Data chunk requires AUTH, skipped\n"); michael@0: goto trigger_send; michael@0: } michael@0: if (length > offset) { michael@0: int retval; michael@0: michael@0: /* michael@0: * First check to make sure our state is correct. We would michael@0: * not get here unless we really did have a tag, so we don't michael@0: * abort if this happens, just dump the chunk silently. michael@0: */ michael@0: switch (SCTP_GET_STATE(&stcb->asoc)) { michael@0: case SCTP_STATE_COOKIE_ECHOED: michael@0: /* michael@0: * we consider data with valid tags in this state michael@0: * shows us the cookie-ack was lost. Imply it was michael@0: * there. michael@0: */ michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { michael@0: sctp_misc_ints(SCTP_THRESHOLD_CLEAR, michael@0: stcb->asoc.overall_error_count, michael@0: 0, michael@0: SCTP_FROM_SCTP_INPUT, michael@0: __LINE__); michael@0: } michael@0: stcb->asoc.overall_error_count = 0; michael@0: sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net); michael@0: break; michael@0: case SCTP_STATE_COOKIE_WAIT: michael@0: /* michael@0: * We consider OOTB any data sent during asoc setup. michael@0: */ michael@0: sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: goto out; michael@0: /*sa_ignore NOTREACHED*/ michael@0: break; michael@0: case SCTP_STATE_EMPTY: /* should not happen */ michael@0: case SCTP_STATE_INUSE: /* should not happen */ michael@0: case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */ michael@0: case SCTP_STATE_SHUTDOWN_ACK_SENT: michael@0: default: michael@0: goto out; michael@0: /*sa_ignore NOTREACHED*/ michael@0: break; michael@0: case SCTP_STATE_OPEN: michael@0: case SCTP_STATE_SHUTDOWN_SENT: michael@0: break; michael@0: } michael@0: /* plow through the data chunks while length > offset */ michael@0: retval = sctp_process_data(mm, iphlen, &offset, length, michael@0: src, dst, sh, michael@0: inp, stcb, net, &high_tsn, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: if (retval == 2) { michael@0: /* michael@0: * The association aborted, NO UNLOCK needed since michael@0: * the association is destroyed. michael@0: */ michael@0: stcb = NULL; michael@0: goto out; michael@0: } michael@0: data_processed = 1; michael@0: /* michael@0: * Anything important needs to have been m_copy'ed in michael@0: * process_data michael@0: */ michael@0: } michael@0: michael@0: /* take care of ecn */ michael@0: if ((data_processed == 1) && michael@0: (stcb->asoc.ecn_allowed == 1) && michael@0: ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) { michael@0: /* Yep, we need to add a ECNE */ michael@0: sctp_send_ecn_echo(stcb, net, high_tsn); michael@0: } michael@0: michael@0: if ((data_processed == 0) && (fwd_tsn_seen)) { michael@0: int was_a_gap; michael@0: uint32_t highest_tsn; michael@0: michael@0: if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) { michael@0: highest_tsn = stcb->asoc.highest_tsn_inside_nr_map; michael@0: } else { michael@0: highest_tsn = stcb->asoc.highest_tsn_inside_map; michael@0: } michael@0: was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn); michael@0: stcb->asoc.send_sack = 1; michael@0: sctp_sack_check(stcb, was_a_gap); michael@0: } else if (fwd_tsn_seen) { michael@0: stcb->asoc.send_sack = 1; michael@0: } michael@0: /* trigger send of any chunks in queue... */ michael@0: trigger_send: michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_audit_log(0xE0, 2); michael@0: sctp_auditing(1, inp, stcb, net); michael@0: #endif michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, michael@0: "Check for chunk output prw:%d tqe:%d tf=%d\n", michael@0: stcb->asoc.peers_rwnd, michael@0: TAILQ_EMPTY(&stcb->asoc.control_send_queue), michael@0: stcb->asoc.total_flight); michael@0: un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight); michael@0: if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) { michael@0: cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq; michael@0: } michael@0: if (cnt_ctrl_ready || michael@0: ((un_sent) && michael@0: (stcb->asoc.peers_rwnd > 0 || michael@0: (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n"); michael@0: sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED); michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n"); michael@0: } michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_audit_log(0xE0, 3); michael@0: sctp_auditing(2, inp, stcb, net); michael@0: #endif michael@0: out: michael@0: if (stcb != NULL) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } michael@0: if (inp_decr != NULL) { michael@0: /* reduce ref-count */ michael@0: SCTP_INP_WLOCK(inp_decr); michael@0: SCTP_INP_DECR_REF(inp_decr); michael@0: SCTP_INP_WUNLOCK(inp_decr); michael@0: } michael@0: #ifdef INVARIANTS michael@0: if (inp != NULL) { michael@0: sctp_validate_no_locks(inp); michael@0: } michael@0: #endif michael@0: return; michael@0: } michael@0: michael@0: #if 0 michael@0: static void michael@0: sctp_print_mbuf_chain(struct mbuf *m) michael@0: { michael@0: for (; m; m = SCTP_BUF_NEXT(m)) { michael@0: SCTP_PRINTF("%p: m_len = %ld\n", (void *)m, SCTP_BUF_LEN(m)); michael@0: if (SCTP_BUF_IS_EXTENDED(m)) michael@0: SCTP_PRINTF("%p: extend_size = %d\n", (void *)m, SCTP_BUF_EXTEND_SIZE(m)); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef INET michael@0: #if !defined(__Userspace__) michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: void michael@0: sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port) michael@0: #elif defined(__Panda__) michael@0: void michael@0: sctp_input(pakhandle_type i_pak) michael@0: #else michael@0: void michael@0: #if __STDC__ michael@0: sctp_input(struct mbuf *i_pak,...) michael@0: #else michael@0: sctp_input(i_pak, va_alist) michael@0: struct mbuf *i_pak; michael@0: #endif michael@0: #endif michael@0: { michael@0: struct mbuf *m; michael@0: int iphlen; michael@0: uint32_t vrf_id = 0; michael@0: uint8_t ecn_bits; michael@0: struct sockaddr_in src, dst; michael@0: struct ip *ip; michael@0: struct sctphdr *sh; michael@0: struct sctp_chunkhdr *ch; michael@0: int length, offset; michael@0: #if !defined(SCTP_WITH_NO_CSUM) michael@0: uint8_t compute_crc; michael@0: #endif michael@0: #if defined(__FreeBSD__) michael@0: uint32_t mflowid; michael@0: uint8_t use_mflowid; michael@0: #endif michael@0: #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)) michael@0: uint16_t port = 0; michael@0: #endif michael@0: michael@0: #if defined(__Panda__) michael@0: /* This is Evil, but its the only way to make panda work right. */ michael@0: iphlen = sizeof(struct ip); michael@0: #else michael@0: iphlen = off; michael@0: #endif michael@0: if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) { michael@0: SCTP_RELEASE_PKT(i_pak); michael@0: return; michael@0: } michael@0: m = SCTP_HEADER_TO_CHAIN(i_pak); michael@0: #ifdef __Panda__ michael@0: SCTP_DETACH_HEADER_FROM_CHAIN(i_pak); michael@0: (void)SCTP_RELEASE_HEADER(i_pak); michael@0: #endif michael@0: #ifdef SCTP_MBUF_LOGGING michael@0: /* Log in any input mbufs */ michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { michael@0: struct mbuf *mat; michael@0: michael@0: for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) { michael@0: if (SCTP_BUF_IS_EXTENDED(mat)) { michael@0: sctp_log_mb(mat, SCTP_MBUF_INPUT); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: #ifdef SCTP_PACKET_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) { michael@0: sctp_packet_log(m); michael@0: } michael@0: #endif michael@0: #if defined(__FreeBSD__) michael@0: #if __FreeBSD_version > 1000049 michael@0: SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, michael@0: "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n", michael@0: m->m_pkthdr.len, michael@0: if_name(m->m_pkthdr.rcvif), michael@0: (int)m->m_pkthdr.csum_flags, CSUM_BITS); michael@0: #elif __FreeBSD_version >= 800000 michael@0: SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, michael@0: "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", michael@0: m->m_pkthdr.len, michael@0: if_name(m->m_pkthdr.rcvif), michael@0: m->m_pkthdr.csum_flags); michael@0: #else michael@0: SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, michael@0: "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", michael@0: m->m_pkthdr.len, michael@0: m->m_pkthdr.rcvif->if_xname, michael@0: m->m_pkthdr.csum_flags); michael@0: #endif michael@0: #endif michael@0: #if defined(__APPLE__) michael@0: SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, michael@0: "sctp_input(): Packet of length %d received on %s%d with csum_flags 0x%x.\n", michael@0: m->m_pkthdr.len, michael@0: m->m_pkthdr.rcvif->if_name, michael@0: m->m_pkthdr.rcvif->if_unit, michael@0: m->m_pkthdr.csum_flags); michael@0: #endif michael@0: #if defined(__Windows__) michael@0: SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, michael@0: "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", michael@0: m->m_pkthdr.len, michael@0: m->m_pkthdr.rcvif->if_xname, michael@0: m->m_pkthdr.csum_flags); michael@0: #endif michael@0: #if defined(__FreeBSD__) michael@0: if (m->m_flags & M_FLOWID) { michael@0: mflowid = m->m_pkthdr.flowid; michael@0: use_mflowid = 1; michael@0: } else { michael@0: mflowid = 0; michael@0: use_mflowid = 0; michael@0: } michael@0: #endif michael@0: SCTP_STAT_INCR(sctps_recvpackets); michael@0: SCTP_STAT_INCR_COUNTER64(sctps_inpackets); michael@0: /* Get IP, SCTP, and first chunk header together in the first mbuf. */ michael@0: offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); michael@0: if (SCTP_BUF_LEN(m) < offset) { michael@0: if ((m = m_pullup(m, offset)) == NULL) { michael@0: SCTP_STAT_INCR(sctps_hdrops); michael@0: return; michael@0: } michael@0: } michael@0: ip = mtod(m, struct ip *); michael@0: sh = (struct sctphdr *)((caddr_t)ip + iphlen); michael@0: ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); michael@0: offset -= sizeof(struct sctp_chunkhdr); michael@0: memset(&src, 0, sizeof(struct sockaddr_in)); michael@0: src.sin_family = AF_INET; michael@0: #ifdef HAVE_SIN_LEN michael@0: src.sin_len = sizeof(struct sockaddr_in); michael@0: #endif michael@0: src.sin_port = sh->src_port; michael@0: src.sin_addr = ip->ip_src; michael@0: memset(&dst, 0, sizeof(struct sockaddr_in)); michael@0: dst.sin_family = AF_INET; michael@0: #ifdef HAVE_SIN_LEN michael@0: dst.sin_len = sizeof(struct sockaddr_in); michael@0: #endif michael@0: dst.sin_port = sh->dest_port; michael@0: dst.sin_addr = ip->ip_dst; michael@0: #if defined(__Windows__) michael@0: NTOHS(ip->ip_len); michael@0: #endif michael@0: #if defined(__Userspace_os_Linux) || defined(__Userspace_os_Windows) michael@0: ip->ip_len = ntohs(ip->ip_len); michael@0: #endif michael@0: #if defined(__FreeBSD__) michael@0: #if __FreeBSD_version >= 1000000 michael@0: length = ntohs(ip->ip_len); michael@0: #else michael@0: length = ip->ip_len + iphlen; michael@0: #endif michael@0: #elif defined(__APPLE__) michael@0: length = ip->ip_len + iphlen; michael@0: #elif defined(__Userspace__) michael@0: #if defined(__Userspace_os_Linux) || defined(__Userspace_os_Windows) michael@0: length = ip->ip_len; michael@0: #else michael@0: length = ip->ip_len + iphlen; michael@0: #endif michael@0: #else michael@0: length = ip->ip_len; michael@0: #endif michael@0: /* Validate mbuf chain length with IP payload length. */ michael@0: if (SCTP_HEADER_LEN(m) != length) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, michael@0: "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m)); michael@0: SCTP_STAT_INCR(sctps_hdrops); michael@0: goto out; michael@0: } michael@0: /* SCTP does not allow broadcasts or multicasts */ michael@0: if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) { michael@0: goto out; michael@0: } michael@0: if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) { michael@0: goto out; michael@0: } michael@0: ecn_bits = ip->ip_tos; michael@0: #if defined(SCTP_WITH_NO_CSUM) michael@0: SCTP_STAT_INCR(sctps_recvnocrc); michael@0: #else michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 michael@0: if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { michael@0: SCTP_STAT_INCR(sctps_recvhwcrc); michael@0: compute_crc = 0; michael@0: } else { michael@0: #else michael@0: if (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) && michael@0: ((src.sin_addr.s_addr == dst.sin_addr.s_addr) || michael@0: (SCTP_IS_IT_LOOPBACK(m)))) { michael@0: SCTP_STAT_INCR(sctps_recvnocrc); michael@0: compute_crc = 0; michael@0: } else { michael@0: #endif michael@0: SCTP_STAT_INCR(sctps_recvswcrc); michael@0: compute_crc = 1; michael@0: } michael@0: #endif michael@0: sctp_common_input_processing(&m, iphlen, offset, length, michael@0: (struct sockaddr *)&src, michael@0: (struct sockaddr *)&dst, michael@0: sh, ch, michael@0: #if !defined(SCTP_WITH_NO_CSUM) michael@0: compute_crc, michael@0: #endif michael@0: ecn_bits, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: out: michael@0: if (m) { michael@0: sctp_m_freem(m); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) michael@0: extern int *sctp_cpuarry; michael@0: #endif michael@0: michael@0: void michael@0: sctp_input(struct mbuf *m, int off) michael@0: { michael@0: #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) michael@0: struct ip *ip; michael@0: struct sctphdr *sh; michael@0: int offset; michael@0: int cpu_to_use; michael@0: uint32_t flowid, tag; michael@0: michael@0: if (mp_ncpus > 1) { michael@0: if (m->m_flags & M_FLOWID) { michael@0: flowid = m->m_pkthdr.flowid; michael@0: } else { michael@0: /* No flow id built by lower layers michael@0: * fix it so we create one. michael@0: */ michael@0: offset = off + sizeof(struct sctphdr); michael@0: if (SCTP_BUF_LEN(m) < offset) { michael@0: if ((m = m_pullup(m, offset)) == NULL) { michael@0: SCTP_STAT_INCR(sctps_hdrops); michael@0: return; michael@0: } michael@0: } michael@0: ip = mtod(m, struct ip *); michael@0: sh = (struct sctphdr *)((caddr_t)ip + off); michael@0: tag = htonl(sh->v_tag); michael@0: flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port); michael@0: m->m_pkthdr.flowid = flowid; michael@0: m->m_flags |= M_FLOWID; michael@0: } michael@0: cpu_to_use = sctp_cpuarry[flowid % mp_ncpus]; michael@0: sctp_queue_to_mcore(m, off, cpu_to_use); michael@0: return; michael@0: } michael@0: #endif michael@0: sctp_input_with_port(m, off, 0); michael@0: } michael@0: #endif michael@0: #endif