2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD: head/sys/netinet/sctp_input.c 271230 2014-09-07 18:05:37Z tuexen $");
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_var.h>
40 #include <netinet/sctp_sysctl.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctputil.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_input.h>
46 #include <netinet/sctp_auth.h>
47 #include <netinet/sctp_indata.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_bsd_addr.h>
50 #include <netinet/sctp_timer.h>
51 #include <netinet/sctp_crc32.h>
52 #if defined(INET) || defined(INET6)
53 #if !defined(__Userspace_os_Windows)
54 #include <netinet/udp.h>
57 #if defined(__FreeBSD__)
61 #if defined(__APPLE__)
62 #define APPLE_FILE_NO 2
67 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
69 struct sctp_nets *net;
71 /* This now not only stops all cookie timers
72 * it also stops any INIT timers as well. This
73 * will make sure that the timers are stopped in
74 * all collision cases.
76 SCTP_TCB_LOCK_ASSERT(stcb);
77 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
78 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
79 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
82 net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_1);
83 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
84 sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
87 net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_2);
94 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
95 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
96 struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
97 struct sctp_tcb *stcb, int *abort_no_unlock,
98 #if defined(__FreeBSD__)
99 uint8_t use_mflowid, uint32_t mflowid,
101 uint32_t vrf_id, uint16_t port)
103 struct sctp_init *init;
106 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
111 /* validate length */
112 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
113 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
114 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
115 #if defined(__FreeBSD__)
116 use_mflowid, mflowid,
120 *abort_no_unlock = 1;
123 /* validate parameters */
125 if (init->initiate_tag == 0) {
126 /* protocol error... send abort */
127 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
128 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
129 #if defined(__FreeBSD__)
130 use_mflowid, mflowid,
134 *abort_no_unlock = 1;
137 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
138 /* invalid parameter... send abort */
139 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
140 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
141 #if defined(__FreeBSD__)
142 use_mflowid, mflowid,
146 *abort_no_unlock = 1;
149 if (init->num_inbound_streams == 0) {
150 /* protocol error... send abort */
151 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
152 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
153 #if defined(__FreeBSD__)
154 use_mflowid, mflowid,
158 *abort_no_unlock = 1;
161 if (init->num_outbound_streams == 0) {
162 /* protocol error... send abort */
163 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
164 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
165 #if defined(__FreeBSD__)
166 use_mflowid, mflowid,
170 *abort_no_unlock = 1;
173 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
174 offset + ntohs(cp->ch.chunk_length))) {
175 /* auth parameter(s) error... send abort */
176 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
177 "Problem with AUTH parameters");
178 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
179 #if defined(__FreeBSD__)
180 use_mflowid, mflowid,
184 *abort_no_unlock = 1;
187 /* We are only accepting if we have a socket with positive so_qlimit.*/
188 if ((stcb == NULL) &&
189 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
190 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
191 (inp->sctp_socket == NULL) ||
192 (inp->sctp_socket->so_qlimit == 0))) {
194 * FIX ME ?? What about TCP model and we have a
195 * match/restart case? Actually no fix is needed.
196 * the lookup will always find the existing assoc so stcb
197 * would not be NULL. It may be questionable to do this
198 * since we COULD just send back the INIT-ACK and hope that
199 * the app did accept()'s by the time the COOKIE was sent. But
200 * there is a price to pay for COOKIE generation and I don't
201 * want to pay it on the chance that the app will actually do
202 * some accepts(). The App just looses and should NOT be in
205 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
206 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
208 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
209 #if defined(__FreeBSD__)
210 use_mflowid, mflowid,
216 if ((stcb != NULL) &&
217 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
218 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
219 sctp_send_shutdown_ack(stcb, NULL);
220 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
222 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
223 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, src, dst,
225 #if defined(__FreeBSD__)
226 use_mflowid, mflowid,
229 ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED));
233 SCTP_INP_RUNLOCK(inp);
238 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
242 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
243 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
250 struct sctp_stream_queue_pending *sp;
251 struct sctp_association *asoc;
253 /* This function returns the number of streams that have
254 * true unsent data on them. Note that as it looks through
255 * it will clean up any places that have old data that
256 * has been sent but left at top of stream queue.
259 SCTP_TCB_SEND_LOCK(stcb);
260 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
261 /* Check to see if some data queued */
262 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
263 /*sa_ignore FREED_MEMORY*/
264 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
268 if ((sp->msg_is_complete) &&
270 (sp->sender_all_done)) {
271 /* We are doing differed cleanup. Last
272 * time through when we took all the data
273 * the sender_all_done was not set.
275 if (sp->put_last_out == 0) {
276 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
277 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
283 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
284 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
286 sctp_free_remote_addr(sp->net);
290 sctp_m_freem(sp->data);
293 sctp_free_a_strmoq(stcb, sp, so_locked);
300 SCTP_TCB_SEND_UNLOCK(stcb);
301 return (unsent_data);
305 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
307 struct sctp_init *init;
308 struct sctp_association *asoc;
309 struct sctp_nets *lnet;
314 /* save off parameters */
315 asoc->peer_vtag = ntohl(init->initiate_tag);
316 asoc->peers_rwnd = ntohl(init->a_rwnd);
318 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
320 if (!TAILQ_EMPTY(&asoc->nets)) {
321 /* update any ssthresh's that may have a default */
322 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
323 lnet->ssthresh = asoc->peers_rwnd;
324 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE|SCTP_CWND_LOGGING_ENABLE)) {
325 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
330 SCTP_TCB_SEND_LOCK(stcb);
331 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
333 struct sctp_stream_out *outs;
334 struct sctp_stream_queue_pending *sp, *nsp;
335 struct sctp_tmit_chunk *chk, *nchk;
337 /* abandon the upper streams */
338 newcnt = ntohs(init->num_inbound_streams);
339 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
340 if (chk->rec.data.stream_number >= newcnt) {
341 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
342 asoc->send_queue_cnt--;
343 if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
344 asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
347 panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
350 if (chk->data != NULL) {
351 sctp_free_bufspace(stcb, asoc, chk, 1);
352 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
353 0, chk, SCTP_SO_NOT_LOCKED);
355 sctp_m_freem(chk->data);
359 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
360 /*sa_ignore FREED_MEMORY*/
364 for (i = newcnt; i < asoc->pre_open_streams; i++) {
365 outs = &asoc->strmout[i];
366 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
367 TAILQ_REMOVE(&outs->outqueue, sp, next);
368 asoc->stream_queue_cnt--;
369 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
370 stcb, 0, sp, SCTP_SO_NOT_LOCKED);
372 sctp_m_freem(sp->data);
376 sctp_free_remote_addr(sp->net);
380 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
381 /*sa_ignore FREED_MEMORY*/
385 /* cut back the count */
386 asoc->pre_open_streams = newcnt;
388 SCTP_TCB_SEND_UNLOCK(stcb);
389 asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
391 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
392 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
393 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
394 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
396 /* This is the next one we expect */
397 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
399 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
400 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
402 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
403 /* open the requested streams */
405 if (asoc->strmin != NULL) {
406 /* Free the old ones */
407 struct sctp_queued_to_read *ctl, *nctl;
409 for (i = 0; i < asoc->streamincnt; i++) {
410 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
411 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
412 sctp_free_remote_addr(ctl->whoFrom);
414 sctp_m_freem(ctl->data);
416 sctp_free_a_readq(stcb, ctl);
419 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
421 if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
422 asoc->streamincnt = ntohs(init->num_outbound_streams);
424 asoc->streamincnt = asoc->max_inbound_streams;
426 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
427 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
428 if (asoc->strmin == NULL) {
429 /* we didn't get memory for the streams! */
430 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
433 for (i = 0; i < asoc->streamincnt; i++) {
434 asoc->strmin[i].stream_no = i;
435 asoc->strmin[i].last_sequence_delivered = 0xffff;
436 TAILQ_INIT(&asoc->strmin[i].inqueue);
437 asoc->strmin[i].delivery_started = 0;
440 * load_address_from_init will put the addresses into the
441 * association when the COOKIE is processed or the INIT-ACK is
442 * processed. Both types of COOKIE's existing and new call this
443 * routine. It will remove addresses that are no longer in the
444 * association (for the restarting case where addresses are
445 * removed). Up front when the INIT arrives we will discard it if it
446 * is a restart and new addresses have been added.
448 /* sa_ignore MEMLEAK */
453 * INIT-ACK message processing/consumption returns value < 0 on error
456 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
457 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
458 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
459 struct sctp_nets *net, int *abort_no_unlock,
460 #if defined(__FreeBSD__)
461 uint8_t use_mflowid, uint32_t mflowid,
465 struct sctp_association *asoc;
467 int retval, abort_flag;
468 uint32_t initack_limit;
469 int nat_friendly = 0;
471 /* First verify that we have no illegal param's */
474 op_err = sctp_arethere_unrecognized_parameters(m,
475 (offset + sizeof(struct sctp_init_chunk)),
476 &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
478 /* Send an abort and notify peer */
479 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
480 *abort_no_unlock = 1;
484 asoc->peer_supports_nat = (uint8_t)nat_friendly;
485 /* process the peer's parameters in the INIT-ACK */
486 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
490 initack_limit = offset + ntohs(cp->ch.chunk_length);
491 /* load all addresses */
492 if ((retval = sctp_load_addresses_from_init(stcb, m,
493 (offset + sizeof(struct sctp_init_chunk)), initack_limit,
495 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
496 "Problem with address parameters");
497 SCTPDBG(SCTP_DEBUG_INPUT1,
498 "Load addresses from INIT causes an abort %d\n",
500 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
501 src, dst, sh, op_err,
502 #if defined(__FreeBSD__)
503 use_mflowid, mflowid,
506 *abort_no_unlock = 1;
509 /* if the peer doesn't support asconf, flush the asconf queue */
510 if (asoc->asconf_supported == 0) {
511 struct sctp_asconf_addr *param, *nparam;
513 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
514 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
515 SCTP_FREE(param, SCTP_M_ASC_ADDR);
519 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
520 stcb->asoc.local_hmacs);
522 sctp_queue_op_err(stcb, op_err);
523 /* queuing will steal away the mbuf chain to the out queue */
526 /* extract the cookie and queue it to "echo" it back... */
527 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
528 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
529 stcb->asoc.overall_error_count,
531 SCTP_FROM_SCTP_INPUT,
534 stcb->asoc.overall_error_count = 0;
535 net->error_count = 0;
538 * Cancel the INIT timer, We do this first before queueing the
539 * cookie. We always cancel at the primary to assue that we are
540 * canceling the timer started by the INIT which always goes to the
543 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
544 asoc->primary_destination, SCTP_FROM_SCTP_INPUT+SCTP_LOC_4);
546 /* calculate the RTO */
547 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy,
548 SCTP_RTT_FROM_NON_DATA);
550 retval = sctp_send_cookie_echo(m, offset, stcb, net);
553 * No cookie, we probably should send a op error. But in any
554 * case if there is no cookie in the INIT-ACK, we can
555 * abandon the peer, its broke.
558 /* We abort with an error of missing mandatory param */
559 op_err = sctp_generate_cause(SCTP_CAUSE_MISSING_PARAM, "");
562 * Expand beyond to include the mandatory
565 struct sctp_inv_mandatory_param *mp;
567 SCTP_BUF_LEN(op_err) =
568 sizeof(struct sctp_inv_mandatory_param);
570 struct sctp_inv_mandatory_param *);
571 /* Subtract the reserved param */
573 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
574 mp->num_param = htonl(1);
575 mp->param = htons(SCTP_STATE_COOKIE);
578 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
579 src, dst, sh, op_err,
580 #if defined(__FreeBSD__)
581 use_mflowid, mflowid,
584 *abort_no_unlock = 1;
593 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
594 struct sctp_tcb *stcb, struct sctp_nets *net)
596 union sctp_sockstore store;
597 struct sctp_nets *r_net, *f_net;
600 uint16_t old_error_counter;
602 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
607 memset(&store, 0, sizeof(store));
608 switch (cp->heartbeat.hb_info.addr_family) {
611 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
612 store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
614 store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
616 store.sin.sin_port = stcb->rport;
617 memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
618 sizeof(store.sin.sin_addr));
626 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
627 store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
629 store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
631 store.sin6.sin6_port = stcb->rport;
632 memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
638 #if defined(__Userspace__)
640 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_conn)) {
641 store.sconn.sconn_family = cp->heartbeat.hb_info.addr_family;
642 #ifdef HAVE_SCONN_LEN
643 store.sconn.sconn_len = cp->heartbeat.hb_info.addr_len;
645 store.sconn.sconn_port = stcb->rport;
646 memcpy(&store.sconn.sconn_addr, cp->heartbeat.hb_info.address, sizeof(void *));
655 r_net = sctp_findnet(stcb, &store.sa);
657 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
660 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
661 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
662 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
664 * If the its a HB and it's random value is correct when can
665 * confirm the destination.
667 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
668 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
669 stcb->asoc.primary_destination = r_net;
670 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
671 f_net = TAILQ_FIRST(&stcb->asoc.nets);
672 if (f_net != r_net) {
673 /* first one on the list is NOT the primary
674 * sctp_cmpaddr() is much more efficent if
675 * the primary is the first on the list, make it
678 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
679 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
683 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
684 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
685 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
686 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
688 old_error_counter = r_net->error_count;
689 r_net->error_count = 0;
690 r_net->hb_responded = 1;
691 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
692 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
693 /* Now lets do a RTO with this */
694 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy,
695 SCTP_RTT_FROM_NON_DATA);
696 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
697 r_net->dest_state |= SCTP_ADDR_REACHABLE;
698 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
699 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
701 if (r_net->dest_state & SCTP_ADDR_PF) {
702 r_net->dest_state &= ~SCTP_ADDR_PF;
703 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
705 if (old_error_counter > 0) {
706 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
707 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
709 if (r_net == stcb->asoc.primary_destination) {
710 if (stcb->asoc.alternate) {
711 /* release the alternate, primary is good */
712 sctp_free_remote_addr(stcb->asoc.alternate);
713 stcb->asoc.alternate = NULL;
716 /* Mobility adaptation */
718 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
719 SCTP_MOBILITY_BASE) ||
720 sctp_is_mobility_feature_on(stcb->sctp_ep,
721 SCTP_MOBILITY_FASTHANDOFF)) &&
722 sctp_is_mobility_feature_on(stcb->sctp_ep,
723 SCTP_MOBILITY_PRIM_DELETED)) {
725 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER+SCTP_LOC_7);
726 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
727 SCTP_MOBILITY_FASTHANDOFF)) {
728 sctp_assoc_immediate_retrans(stcb,
729 stcb->asoc.primary_destination);
731 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
732 SCTP_MOBILITY_BASE)) {
733 sctp_move_chunks_from_net(stcb,
734 stcb->asoc.deleted_primary);
736 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
737 stcb->asoc.deleted_primary);
743 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
745 /* return 0 means we want you to proceed with the abort
746 * non-zero means no abort processing
748 struct sctpasochead *head;
750 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
751 /* generate a new vtag and send init */
752 LIST_REMOVE(stcb, sctp_asocs);
753 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
754 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
755 /* put it in the bucket in the vtag hash of assoc's for the system */
756 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
757 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
760 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
761 /* treat like a case where the cookie expired i.e.:
762 * - dump current cookie.
763 * - generate a new vtag.
766 /* generate a new vtag and send init */
767 LIST_REMOVE(stcb, sctp_asocs);
768 stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
769 stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
770 sctp_stop_all_cookie_timers(stcb);
771 sctp_toss_old_cookies(stcb, &stcb->asoc);
772 stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
773 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
774 /* put it in the bucket in the vtag hash of assoc's for the system */
775 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
776 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
783 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
784 struct sctp_nets *net)
786 /* return 0 means we want you to proceed with the abort
787 * non-zero means no abort processing
789 if (stcb->asoc.auth_supported == 0) {
790 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
793 sctp_asconf_send_nat_state_update(stcb, net);
799 sctp_handle_abort(struct sctp_abort_chunk *abort,
800 struct sctp_tcb *stcb, struct sctp_nets *net)
802 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
808 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
812 len = ntohs(abort->ch.chunk_length);
813 if (len > sizeof (struct sctp_chunkhdr)) {
814 /* Need to check the cause codes for our
815 * two magic nat aborts which don't kill the assoc
818 struct sctp_missing_nat_state *natc;
820 natc = (struct sctp_missing_nat_state *)(abort + 1);
821 error = ntohs(natc->cause);
822 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
823 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
824 abort->ch.chunk_flags);
825 if (sctp_handle_nat_colliding_state(stcb)) {
828 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
829 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
830 abort->ch.chunk_flags);
831 if (sctp_handle_nat_missing_state(stcb, net)) {
838 /* stop any receive timers */
839 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_6);
840 /* notify user of the abort and clean up... */
841 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
843 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
844 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
845 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
846 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
848 #ifdef SCTP_ASOCLOG_OF_TSNS
849 sctp_print_out_track_log(stcb);
851 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
852 so = SCTP_INP_SO(stcb->sctp_ep);
853 atomic_add_int(&stcb->asoc.refcnt, 1);
854 SCTP_TCB_UNLOCK(stcb);
855 SCTP_SOCKET_LOCK(so, 1);
857 atomic_subtract_int(&stcb->asoc.refcnt, 1);
859 stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
860 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
861 SCTP_FROM_SCTP_INPUT+SCTP_LOC_6);
862 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
863 SCTP_SOCKET_UNLOCK(so, 1);
865 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
869 sctp_start_net_timers(struct sctp_tcb *stcb)
871 uint32_t cnt_hb_sent;
872 struct sctp_nets *net;
875 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
876 /* For each network start:
879 * 3) If the dest in unconfirmed send
880 * a hb as well if under max_hb_burst have
883 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
884 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
885 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
886 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
887 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
892 sctp_chunk_output(stcb->sctp_ep, stcb,
893 SCTP_OUTPUT_FROM_COOKIE_ACK,
900 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
901 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
903 struct sctp_association *asoc;
904 int some_on_streamwheel;
905 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
909 SCTPDBG(SCTP_DEBUG_INPUT2,
910 "sctp_handle_shutdown: handling SHUTDOWN\n");
914 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
915 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
918 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
919 /* Shutdown NOT the expected size */
922 sctp_update_acked(stcb, cp, abort_flag);
927 if (asoc->control_pdapi) {
928 /* With a normal shutdown
929 * we assume the end of last record.
931 SCTP_INP_READ_LOCK(stcb->sctp_ep);
932 asoc->control_pdapi->end_added = 1;
933 asoc->control_pdapi->pdapi_aborted = 1;
934 asoc->control_pdapi = NULL;
935 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
936 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
937 so = SCTP_INP_SO(stcb->sctp_ep);
938 atomic_add_int(&stcb->asoc.refcnt, 1);
939 SCTP_TCB_UNLOCK(stcb);
940 SCTP_SOCKET_LOCK(so, 1);
942 atomic_subtract_int(&stcb->asoc.refcnt, 1);
943 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
944 /* assoc was freed while we were unlocked */
945 SCTP_SOCKET_UNLOCK(so, 1);
949 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
950 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
951 SCTP_SOCKET_UNLOCK(so, 1);
954 /* goto SHUTDOWN_RECEIVED state to block new requests */
955 if (stcb->sctp_socket) {
956 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
957 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
958 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
959 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
960 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
961 /* notify upper layer that peer has initiated a shutdown */
962 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
965 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
968 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
970 * stop the shutdown timer, since we WILL move to
973 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_8);
975 /* Now is there unsent data on a stream somewhere? */
976 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
978 if (!TAILQ_EMPTY(&asoc->send_queue) ||
979 !TAILQ_EMPTY(&asoc->sent_queue) ||
980 some_on_streamwheel) {
981 /* By returning we will push more data out */
984 /* no outstanding data to send, so move on... */
985 /* send SHUTDOWN-ACK */
986 /* move to SHUTDOWN-ACK-SENT state */
987 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
988 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
989 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
991 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
992 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
993 sctp_stop_timers_for_shutdown(stcb);
994 sctp_send_shutdown_ack(stcb, net);
995 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
1001 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
1002 struct sctp_tcb *stcb,
1003 struct sctp_nets *net)
1005 struct sctp_association *asoc;
1006 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1009 so = SCTP_INP_SO(stcb->sctp_ep);
1011 SCTPDBG(SCTP_DEBUG_INPUT2,
1012 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
1017 /* process according to association state */
1018 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
1019 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
1020 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
1021 sctp_send_shutdown_complete(stcb, net, 1);
1022 SCTP_TCB_UNLOCK(stcb);
1025 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
1026 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1027 /* unexpected SHUTDOWN-ACK... so ignore... */
1028 SCTP_TCB_UNLOCK(stcb);
1031 if (asoc->control_pdapi) {
1032 /* With a normal shutdown
1033 * we assume the end of last record.
1035 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1036 asoc->control_pdapi->end_added = 1;
1037 asoc->control_pdapi->pdapi_aborted = 1;
1038 asoc->control_pdapi = NULL;
1039 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1040 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1041 atomic_add_int(&stcb->asoc.refcnt, 1);
1042 SCTP_TCB_UNLOCK(stcb);
1043 SCTP_SOCKET_LOCK(so, 1);
1044 SCTP_TCB_LOCK(stcb);
1045 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1046 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1047 /* assoc was freed while we were unlocked */
1048 SCTP_SOCKET_UNLOCK(so, 1);
1052 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1053 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1054 SCTP_SOCKET_UNLOCK(so, 1);
1058 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1059 !TAILQ_EMPTY(&asoc->sent_queue) ||
1060 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
1061 panic("Queues are not empty when handling SHUTDOWN-ACK");
1064 /* stop the timer */
1065 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_9);
1066 /* send SHUTDOWN-COMPLETE */
1067 sctp_send_shutdown_complete(stcb, net, 0);
1068 /* notify upper layer protocol */
1069 if (stcb->sctp_socket) {
1070 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1071 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1072 stcb->sctp_socket->so_snd.sb_cc = 0;
1074 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1076 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1077 /* free the TCB but first save off the ep */
1078 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1079 atomic_add_int(&stcb->asoc.refcnt, 1);
1080 SCTP_TCB_UNLOCK(stcb);
1081 SCTP_SOCKET_LOCK(so, 1);
1082 SCTP_TCB_LOCK(stcb);
1083 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1085 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1086 SCTP_FROM_SCTP_INPUT+SCTP_LOC_10);
1087 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1088 SCTP_SOCKET_UNLOCK(so, 1);
1093 * Skip past the param header and then we will find the chunk that caused the
1094 * problem. There are two possiblities ASCONF or FWD-TSN other than that and
1095 * our peer must be broken.
1098 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1099 struct sctp_nets *net)
1101 struct sctp_chunkhdr *chk;
1103 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1104 switch (chk->chunk_type) {
1105 case SCTP_ASCONF_ACK:
1107 sctp_asconf_cleanup(stcb, net);
1109 case SCTP_FORWARD_CUM_TSN:
1110 stcb->asoc.prsctp_supported = 0;
1113 SCTPDBG(SCTP_DEBUG_INPUT2,
1114 "Peer does not support chunk type %d(%x)??\n",
1115 chk->chunk_type, (uint32_t) chk->chunk_type);
1121 * Skip past the param header and then we will find the param that caused the
1122 * problem. There are a number of param's in a ASCONF OR the prsctp param
1123 * these will turn of specific features.
1124 * XXX: Is this the right thing to do?
1127 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1129 struct sctp_paramhdr *pbad;
1132 switch (ntohs(pbad->param_type)) {
1134 case SCTP_PRSCTP_SUPPORTED:
1135 stcb->asoc.prsctp_supported = 0;
1137 case SCTP_SUPPORTED_CHUNK_EXT:
1139 /* draft-ietf-tsvwg-addip-sctp */
1140 case SCTP_HAS_NAT_SUPPORT:
1141 stcb->asoc.peer_supports_nat = 0;
1143 case SCTP_ADD_IP_ADDRESS:
1144 case SCTP_DEL_IP_ADDRESS:
1145 case SCTP_SET_PRIM_ADDR:
1146 stcb->asoc.asconf_supported = 0;
1148 case SCTP_SUCCESS_REPORT:
1149 case SCTP_ERROR_CAUSE_IND:
1150 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1151 SCTPDBG(SCTP_DEBUG_INPUT2,
1152 "Turning off ASCONF to this strange peer\n");
1153 stcb->asoc.asconf_supported = 0;
1156 SCTPDBG(SCTP_DEBUG_INPUT2,
1157 "Peer does not support param type %d(%x)??\n",
1158 pbad->param_type, (uint32_t) pbad->param_type);
1164 sctp_handle_error(struct sctp_chunkhdr *ch,
1165 struct sctp_tcb *stcb, struct sctp_nets *net)
1168 struct sctp_paramhdr *phdr;
1169 uint16_t error, error_type;
1171 struct sctp_association *asoc;
1173 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1177 /* parse through all of the errors and process */
1179 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1180 sizeof(struct sctp_chunkhdr));
1181 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1183 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1184 /* Process an Error Cause */
1185 error_type = ntohs(phdr->param_type);
1186 error_len = ntohs(phdr->param_length);
1187 if ((error_len > chklen) || (error_len == 0)) {
1188 /* invalid param length for this param */
1189 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1194 /* report the first error cause */
1197 switch (error_type) {
1198 case SCTP_CAUSE_INVALID_STREAM:
1199 case SCTP_CAUSE_MISSING_PARAM:
1200 case SCTP_CAUSE_INVALID_PARAM:
1201 case SCTP_CAUSE_NO_USER_DATA:
1202 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1205 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1206 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1208 if (sctp_handle_nat_colliding_state(stcb)) {
1212 case SCTP_CAUSE_NAT_MISSING_STATE:
1213 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1215 if (sctp_handle_nat_missing_state(stcb, net)) {
1219 case SCTP_CAUSE_STALE_COOKIE:
1221 * We only act if we have echoed a cookie and are
1224 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1227 p = (int *)((caddr_t)phdr + sizeof(*phdr));
1228 /* Save the time doubled */
1229 asoc->cookie_preserve_req = ntohl(*p) << 1;
1230 asoc->stale_cookie_count++;
1231 if (asoc->stale_cookie_count >
1232 asoc->max_init_times) {
1233 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1234 /* now free the asoc */
1235 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1236 so = SCTP_INP_SO(stcb->sctp_ep);
1237 atomic_add_int(&stcb->asoc.refcnt, 1);
1238 SCTP_TCB_UNLOCK(stcb);
1239 SCTP_SOCKET_LOCK(so, 1);
1240 SCTP_TCB_LOCK(stcb);
1241 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1243 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1244 SCTP_FROM_SCTP_INPUT+SCTP_LOC_11);
1245 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1246 SCTP_SOCKET_UNLOCK(so, 1);
1250 /* blast back to INIT state */
1251 sctp_toss_old_cookies(stcb, &stcb->asoc);
1252 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1253 asoc->state |= SCTP_STATE_COOKIE_WAIT;
1254 sctp_stop_all_cookie_timers(stcb);
1255 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1258 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1260 * Nothing we can do here, we don't do hostname
1261 * addresses so if the peer does not like my IPv6
1262 * (or IPv4 for that matter) it does not matter. If
1263 * they don't support that type of address, they can
1264 * NOT possibly get that packet type... i.e. with no
1265 * IPv6 you can't recieve a IPv6 packet. so we can
1266 * safely ignore this one. If we ever added support
1267 * for HOSTNAME Addresses, then we would need to do
1271 case SCTP_CAUSE_UNRECOG_CHUNK:
1272 sctp_process_unrecog_chunk(stcb, phdr, net);
1274 case SCTP_CAUSE_UNRECOG_PARAM:
1275 sctp_process_unrecog_param(stcb, phdr);
1277 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1279 * We ignore this since the timer will drive out a
1280 * new cookie anyway and there timer will drive us
1281 * to send a SHUTDOWN_COMPLETE. We can't send one
1282 * here since we don't have their tag.
1285 case SCTP_CAUSE_DELETING_LAST_ADDR:
1286 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1287 case SCTP_CAUSE_DELETING_SRC_ADDR:
1289 * We should NOT get these here, but in a
1292 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1295 case SCTP_CAUSE_OUT_OF_RESC:
1297 * And what, pray tell do we do with the fact that
1298 * the peer is out of resources? Not really sure we
1299 * could do anything but abort. I suspect this
1300 * should have came WITH an abort instead of in a
1305 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1309 adjust = SCTP_SIZE32(error_len);
1311 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1313 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED);
1318 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1319 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1320 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1321 struct sctp_nets *net, int *abort_no_unlock,
1322 #if defined(__FreeBSD__)
1323 uint8_t use_mflowid, uint32_t mflowid,
1327 struct sctp_init_ack *init_ack;
1328 struct mbuf *op_err;
1330 SCTPDBG(SCTP_DEBUG_INPUT2,
1331 "sctp_handle_init_ack: handling INIT-ACK\n");
1334 SCTPDBG(SCTP_DEBUG_INPUT2,
1335 "sctp_handle_init_ack: TCB is null\n");
1338 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1339 /* Invalid length */
1340 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1341 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1342 src, dst, sh, op_err,
1343 #if defined(__FreeBSD__)
1344 use_mflowid, mflowid,
1347 *abort_no_unlock = 1;
1350 init_ack = &cp->init;
1351 /* validate parameters */
1352 if (init_ack->initiate_tag == 0) {
1353 /* protocol error... send an abort */
1354 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1355 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1356 src, dst, sh, op_err,
1357 #if defined(__FreeBSD__)
1358 use_mflowid, mflowid,
1361 *abort_no_unlock = 1;
1364 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1365 /* protocol error... send an abort */
1366 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1367 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1368 src, dst, sh, op_err,
1369 #if defined(__FreeBSD__)
1370 use_mflowid, mflowid,
1373 *abort_no_unlock = 1;
1376 if (init_ack->num_inbound_streams == 0) {
1377 /* protocol error... send an abort */
1378 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1379 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1380 src, dst, sh, op_err,
1381 #if defined(__FreeBSD__)
1382 use_mflowid, mflowid,
1385 *abort_no_unlock = 1;
1388 if (init_ack->num_outbound_streams == 0) {
1389 /* protocol error... send an abort */
1390 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1391 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1392 src, dst, sh, op_err,
1393 #if defined(__FreeBSD__)
1394 use_mflowid, mflowid,
1397 *abort_no_unlock = 1;
1400 /* process according to association state... */
1401 switch (stcb->asoc.state & SCTP_STATE_MASK) {
1402 case SCTP_STATE_COOKIE_WAIT:
1403 /* this is the expected state for this chunk */
1404 /* process the INIT-ACK parameters */
1405 if (stcb->asoc.primary_destination->dest_state &
1406 SCTP_ADDR_UNCONFIRMED) {
1408 * The primary is where we sent the INIT, we can
1409 * always consider it confirmed when the INIT-ACK is
1410 * returned. Do this before we load addresses
1413 stcb->asoc.primary_destination->dest_state &=
1414 ~SCTP_ADDR_UNCONFIRMED;
1415 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1416 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1418 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1419 net, abort_no_unlock,
1420 #if defined(__FreeBSD__)
1421 use_mflowid, mflowid,
1424 /* error in parsing parameters */
1427 /* update our state */
1428 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1429 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1431 /* reset the RTO calc */
1432 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1433 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1434 stcb->asoc.overall_error_count,
1436 SCTP_FROM_SCTP_INPUT,
1439 stcb->asoc.overall_error_count = 0;
1440 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1442 * collapse the init timer back in case of a exponential
1445 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1448 * the send at the end of the inbound data processing will
1449 * cause the cookie to be sent
1452 case SCTP_STATE_SHUTDOWN_SENT:
1453 /* incorrect state... discard */
1455 case SCTP_STATE_COOKIE_ECHOED:
1456 /* incorrect state... discard */
1458 case SCTP_STATE_OPEN:
1459 /* incorrect state... discard */
1461 case SCTP_STATE_EMPTY:
1462 case SCTP_STATE_INUSE:
1464 /* incorrect state... discard */
1468 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1472 static struct sctp_tcb *
1473 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1474 struct sockaddr *src, struct sockaddr *dst,
1475 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1476 struct sctp_inpcb *inp, struct sctp_nets **netp,
1477 struct sockaddr *init_src, int *notification,
1478 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1479 #if defined(__FreeBSD__)
1480 uint8_t use_mflowid, uint32_t mflowid,
1482 uint32_t vrf_id, uint16_t port);
1486 * handle a state cookie for an existing association m: input packet mbuf
1487 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1488 * "split" mbuf and the cookie signature does not exist offset: offset into
1489 * mbuf to the cookie-echo chunk
1491 static struct sctp_tcb *
1492 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1493 struct sockaddr *src, struct sockaddr *dst,
1494 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1495 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1496 struct sockaddr *init_src, int *notification,
1497 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1498 #if defined(__FreeBSD__)
1499 uint8_t use_mflowid, uint32_t mflowid,
1501 uint32_t vrf_id, uint16_t port)
1503 struct sctp_association *asoc;
1504 struct sctp_init_chunk *init_cp, init_buf;
1505 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1506 struct sctp_nets *net;
1507 struct mbuf *op_err;
1508 int init_offset, initack_offset, i;
1512 #if defined(SCTP_DETAILED_STR_STATS)
1517 /* I know that the TCB is non-NULL from the caller */
1519 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1520 if (asoc->cookie_how[how_indx] == 0)
1523 if (how_indx < sizeof(asoc->cookie_how)) {
1524 asoc->cookie_how[how_indx] = 1;
1526 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1527 /* SHUTDOWN came in after sending INIT-ACK */
1528 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1529 op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1530 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1531 #if defined(__FreeBSD__)
1532 use_mflowid, mflowid,
1535 if (how_indx < sizeof(asoc->cookie_how))
1536 asoc->cookie_how[how_indx] = 2;
1540 * find and validate the INIT chunk in the cookie (peer's info) the
1541 * INIT should start after the cookie-echo header struct (chunk
1542 * header, state cookie header struct)
1544 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1546 init_cp = (struct sctp_init_chunk *)
1547 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1548 (uint8_t *) & init_buf);
1549 if (init_cp == NULL) {
1550 /* could not pull a INIT chunk in cookie */
1553 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1557 * find and validate the INIT-ACK chunk in the cookie (my info) the
1558 * INIT-ACK follows the INIT chunk
1560 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1561 initack_cp = (struct sctp_init_ack_chunk *)
1562 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1563 (uint8_t *) & initack_buf);
1564 if (initack_cp == NULL) {
1565 /* could not pull INIT-ACK chunk in cookie */
1568 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1571 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1572 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1574 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1575 * to get into the OPEN state
1577 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1579 * Opps, this means that we somehow generated two vtag's
1580 * the same. I.e. we did:
1582 * <---INIT(tag=a)------
1583 * ----INIT-ACK(tag=t)-->
1584 * ----INIT(tag=t)------> *1
1585 * <---INIT-ACK(tag=a)---
1586 * <----CE(tag=t)------------- *2
1588 * At point *1 we should be generating a different
1589 * tag t'. Which means we would throw away the CE and send
1590 * ours instead. Basically this is case C (throw away side).
1592 if (how_indx < sizeof(asoc->cookie_how))
1593 asoc->cookie_how[how_indx] = 17;
1597 switch (SCTP_GET_STATE(asoc)) {
1598 case SCTP_STATE_COOKIE_WAIT:
1599 case SCTP_STATE_COOKIE_ECHOED:
1601 * INIT was sent but got a COOKIE_ECHO with the
1602 * correct tags... just accept it...but we must
1603 * process the init so that we can make sure we
1604 * have the right seq no's.
1606 /* First we must process the INIT !! */
1607 retval = sctp_process_init(init_cp, stcb);
1609 if (how_indx < sizeof(asoc->cookie_how))
1610 asoc->cookie_how[how_indx] = 3;
1613 /* we have already processed the INIT so no problem */
1614 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1615 net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_12);
1616 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_13);
1617 /* update current state */
1618 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1619 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1621 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1623 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1624 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1625 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1626 stcb->sctp_ep, stcb, asoc->primary_destination);
1628 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1629 sctp_stop_all_cookie_timers(stcb);
1630 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1631 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1632 (inp->sctp_socket->so_qlimit == 0)
1634 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1638 * Here is where collision would go if we
1639 * did a connect() and instead got a
1640 * init/init-ack/cookie done before the
1641 * init-ack came back..
1643 stcb->sctp_ep->sctp_flags |=
1644 SCTP_PCB_FLAGS_CONNECTED;
1645 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1646 so = SCTP_INP_SO(stcb->sctp_ep);
1647 atomic_add_int(&stcb->asoc.refcnt, 1);
1648 SCTP_TCB_UNLOCK(stcb);
1649 SCTP_SOCKET_LOCK(so, 1);
1650 SCTP_TCB_LOCK(stcb);
1651 atomic_add_int(&stcb->asoc.refcnt, -1);
1652 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1653 SCTP_SOCKET_UNLOCK(so, 1);
1657 soisconnected(stcb->sctp_socket);
1658 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1659 SCTP_SOCKET_UNLOCK(so, 1);
1662 /* notify upper layer */
1663 *notification = SCTP_NOTIFY_ASSOC_UP;
1665 * since we did not send a HB make sure we
1666 * don't double things
1668 net->hb_responded = 1;
1669 net->RTO = sctp_calculate_rto(stcb, asoc, net,
1670 &cookie->time_entered,
1671 sctp_align_unsafe_makecopy,
1672 SCTP_RTT_FROM_NON_DATA);
1674 if (stcb->asoc.sctp_autoclose_ticks &&
1675 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1676 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1682 * we're in the OPEN state (or beyond), so
1683 * peer must have simply lost the COOKIE-ACK
1687 sctp_stop_all_cookie_timers(stcb);
1689 * We ignore the return code here.. not sure if we should
1690 * somehow abort.. but we do have an existing asoc. This
1691 * really should not fail.
1693 if (sctp_load_addresses_from_init(stcb, m,
1694 init_offset + sizeof(struct sctp_init_chunk),
1695 initack_offset, src, dst, init_src)) {
1696 if (how_indx < sizeof(asoc->cookie_how))
1697 asoc->cookie_how[how_indx] = 4;
1700 /* respond with a COOKIE-ACK */
1701 sctp_toss_old_cookies(stcb, asoc);
1702 sctp_send_cookie_ack(stcb);
1703 if (how_indx < sizeof(asoc->cookie_how))
1704 asoc->cookie_how[how_indx] = 5;
1708 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1709 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1710 cookie->tie_tag_my_vtag == 0 &&
1711 cookie->tie_tag_peer_vtag == 0) {
1713 * case C in Section 5.2.4 Table 2: XMOO silently discard
1715 if (how_indx < sizeof(asoc->cookie_how))
1716 asoc->cookie_how[how_indx] = 6;
1719 /* If nat support, and the below and stcb is established,
1720 * send back a ABORT(colliding state) if we are established.
1722 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1723 (asoc->peer_supports_nat) &&
1724 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1725 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1726 (asoc->peer_vtag == 0)))) {
1727 /* Special case - Peer's support nat. We may have
1728 * two init's that we gave out the same tag on since
1729 * one was not established.. i.e. we get INIT from host-1
1730 * behind the nat and we respond tag-a, we get a INIT from
1731 * host-2 behind the nat and we get tag-a again. Then we
1732 * bring up host-1 (or 2's) assoc, Then comes the cookie
1733 * from hsot-2 (or 1). Now we have colliding state. We must
1734 * send an abort here with colliding state indication.
1736 op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1737 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1738 #if defined(__FreeBSD__)
1739 use_mflowid, mflowid,
1744 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1745 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1746 (asoc->peer_vtag == 0))) {
1748 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1749 * should be ok, re-accept peer info
1751 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1752 /* Extension of case C.
1753 * If we hit this, then the random number
1754 * generator returned the same vtag when we
1755 * first sent our INIT-ACK and when we later sent
1756 * our INIT. The side with the seq numbers that are
1757 * different will be the one that normnally would
1758 * have hit case C. This in effect "extends" our vtags
1759 * in this collision case to be 64 bits. The same collision
1760 * could occur aka you get both vtag and seq number the
1761 * same twice in a row.. but is much less likely. If it
1762 * did happen then we would proceed through and bring
1763 * up the assoc.. we may end up with the wrong stream
1764 * setup however.. which would be bad.. but there is
1765 * no way to tell.. until we send on a stream that does
1768 if (how_indx < sizeof(asoc->cookie_how))
1769 asoc->cookie_how[how_indx] = 7;
1773 if (how_indx < sizeof(asoc->cookie_how))
1774 asoc->cookie_how[how_indx] = 8;
1775 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_14);
1776 sctp_stop_all_cookie_timers(stcb);
1778 * since we did not send a HB make sure we don't double
1781 net->hb_responded = 1;
1782 if (stcb->asoc.sctp_autoclose_ticks &&
1783 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1784 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1787 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1788 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1790 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1791 /* Ok the peer probably discarded our
1792 * data (if we echoed a cookie+data). So anything
1793 * on the sent_queue should be marked for
1794 * retransmit, we may not get something to
1795 * kick us so it COULD still take a timeout
1796 * to move these.. but it can't hurt to mark them.
1798 struct sctp_tmit_chunk *chk;
1799 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1800 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1801 chk->sent = SCTP_DATAGRAM_RESEND;
1802 sctp_flight_size_decrease(chk);
1803 sctp_total_flight_decrease(stcb, chk);
1804 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1810 /* process the INIT info (peer's info) */
1811 retval = sctp_process_init(init_cp, stcb);
1813 if (how_indx < sizeof(asoc->cookie_how))
1814 asoc->cookie_how[how_indx] = 9;
1817 if (sctp_load_addresses_from_init(stcb, m,
1818 init_offset + sizeof(struct sctp_init_chunk),
1819 initack_offset, src, dst, init_src)) {
1820 if (how_indx < sizeof(asoc->cookie_how))
1821 asoc->cookie_how[how_indx] = 10;
1824 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1825 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1826 *notification = SCTP_NOTIFY_ASSOC_UP;
1828 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1829 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1830 (inp->sctp_socket->so_qlimit == 0)) {
1831 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1834 stcb->sctp_ep->sctp_flags |=
1835 SCTP_PCB_FLAGS_CONNECTED;
1836 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1837 so = SCTP_INP_SO(stcb->sctp_ep);
1838 atomic_add_int(&stcb->asoc.refcnt, 1);
1839 SCTP_TCB_UNLOCK(stcb);
1840 SCTP_SOCKET_LOCK(so, 1);
1841 SCTP_TCB_LOCK(stcb);
1842 atomic_add_int(&stcb->asoc.refcnt, -1);
1843 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1844 SCTP_SOCKET_UNLOCK(so, 1);
1848 soisconnected(stcb->sctp_socket);
1849 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1850 SCTP_SOCKET_UNLOCK(so, 1);
1853 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1854 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1856 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1857 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1858 } else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1859 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1861 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1863 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1864 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1865 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1866 stcb->sctp_ep, stcb, asoc->primary_destination);
1868 sctp_stop_all_cookie_timers(stcb);
1869 sctp_toss_old_cookies(stcb, asoc);
1870 sctp_send_cookie_ack(stcb);
1872 /* only if we have retrans set do we do this. What
1873 * this call does is get only the COOKIE-ACK out
1874 * and then when we return the normal call to
1875 * sctp_chunk_output will get the retrans out
1878 sctp_chunk_output(inp,stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1880 if (how_indx < sizeof(asoc->cookie_how))
1881 asoc->cookie_how[how_indx] = 11;
1885 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1886 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1887 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1888 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1889 cookie->tie_tag_peer_vtag != 0) {
1890 struct sctpasochead *head;
1891 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1895 if (asoc->peer_supports_nat) {
1896 /* This is a gross gross hack.
1897 * Just call the cookie_new code since we
1898 * are allowing a duplicate association.
1899 * I hope this works...
1901 return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1902 sh, cookie, cookie_len,
1903 inp, netp, init_src,notification,
1904 auth_skipped, auth_offset, auth_len,
1905 #if defined(__FreeBSD__)
1906 use_mflowid, mflowid,
1911 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1914 if (how_indx < sizeof(asoc->cookie_how))
1915 asoc->cookie_how[how_indx] = 12;
1916 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_15);
1917 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_16);
1919 /* notify upper layer */
1920 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1921 atomic_add_int(&stcb->asoc.refcnt, 1);
1922 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1923 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1924 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1925 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1927 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1928 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1929 } else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1930 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1932 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1933 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1934 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1935 stcb->sctp_ep, stcb, asoc->primary_destination);
1937 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1938 /* move to OPEN state, if not in SHUTDOWN_SENT */
1939 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1941 asoc->pre_open_streams =
1942 ntohs(initack_cp->init.num_outbound_streams);
1943 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1944 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1945 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1947 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1949 asoc->str_reset_seq_in = asoc->init_seq_number;
1951 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1952 if (asoc->mapping_array) {
1953 memset(asoc->mapping_array, 0,
1954 asoc->mapping_array_size);
1956 if (asoc->nr_mapping_array) {
1957 memset(asoc->nr_mapping_array, 0,
1958 asoc->mapping_array_size);
1960 SCTP_TCB_UNLOCK(stcb);
1961 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1962 so = SCTP_INP_SO(stcb->sctp_ep);
1963 SCTP_SOCKET_LOCK(so, 1);
1965 SCTP_INP_INFO_WLOCK();
1966 SCTP_INP_WLOCK(stcb->sctp_ep);
1967 SCTP_TCB_LOCK(stcb);
1968 atomic_add_int(&stcb->asoc.refcnt, -1);
1969 /* send up all the data */
1970 SCTP_TCB_SEND_LOCK(stcb);
1972 sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED);
1973 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1974 stcb->asoc.strmout[i].chunks_on_queues = 0;
1975 #if defined(SCTP_DETAILED_STR_STATS)
1976 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1977 asoc->strmout[i].abandoned_sent[j] = 0;
1978 asoc->strmout[i].abandoned_unsent[j] = 0;
1981 asoc->strmout[i].abandoned_sent[0] = 0;
1982 asoc->strmout[i].abandoned_unsent[0] = 0;
1984 stcb->asoc.strmout[i].stream_no = i;
1985 stcb->asoc.strmout[i].next_sequence_send = 0;
1986 stcb->asoc.strmout[i].last_msg_incomplete = 0;
1988 /* process the INIT-ACK info (my info) */
1989 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1990 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1992 /* pull from vtag hash */
1993 LIST_REMOVE(stcb, sctp_asocs);
1994 /* re-insert to new vtag position */
1995 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1996 SCTP_BASE_INFO(hashasocmark))];
1998 * put it in the bucket in the vtag hash of assoc's for the
2001 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
2003 SCTP_TCB_SEND_UNLOCK(stcb);
2004 SCTP_INP_WUNLOCK(stcb->sctp_ep);
2005 SCTP_INP_INFO_WUNLOCK();
2006 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2007 SCTP_SOCKET_UNLOCK(so, 1);
2009 asoc->total_flight = 0;
2010 asoc->total_flight_count = 0;
2011 /* process the INIT info (peer's info) */
2012 retval = sctp_process_init(init_cp, stcb);
2014 if (how_indx < sizeof(asoc->cookie_how))
2015 asoc->cookie_how[how_indx] = 13;
2020 * since we did not send a HB make sure we don't double
2023 net->hb_responded = 1;
2025 if (sctp_load_addresses_from_init(stcb, m,
2026 init_offset + sizeof(struct sctp_init_chunk),
2027 initack_offset, src, dst, init_src)) {
2028 if (how_indx < sizeof(asoc->cookie_how))
2029 asoc->cookie_how[how_indx] = 14;
2033 /* respond with a COOKIE-ACK */
2034 sctp_stop_all_cookie_timers(stcb);
2035 sctp_toss_old_cookies(stcb, asoc);
2036 sctp_send_cookie_ack(stcb);
2037 if (how_indx < sizeof(asoc->cookie_how))
2038 asoc->cookie_how[how_indx] = 15;
2042 if (how_indx < sizeof(asoc->cookie_how))
2043 asoc->cookie_how[how_indx] = 16;
2044 /* all other cases... */
2050 * handle a state cookie for a new association m: input packet mbuf chain--
2051 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2052 * and the cookie signature does not exist offset: offset into mbuf to the
2053 * cookie-echo chunk length: length of the cookie chunk to: where the init
2054 * was from returns a new TCB
2056 static struct sctp_tcb *
2057 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2058 struct sockaddr *src, struct sockaddr *dst,
2059 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2060 struct sctp_inpcb *inp, struct sctp_nets **netp,
2061 struct sockaddr *init_src, int *notification,
2062 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2063 #if defined(__FreeBSD__)
2064 uint8_t use_mflowid, uint32_t mflowid,
2066 uint32_t vrf_id, uint16_t port)
2068 struct sctp_tcb *stcb;
2069 struct sctp_init_chunk *init_cp, init_buf;
2070 struct sctp_init_ack_chunk *initack_cp, initack_buf;
2071 union sctp_sockstore store;
2072 struct sctp_association *asoc;
2073 int init_offset, initack_offset, initack_limit;
2076 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2077 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2080 so = SCTP_INP_SO(inp);
2084 * find and validate the INIT chunk in the cookie (peer's info) the
2085 * INIT should start after the cookie-echo header struct (chunk
2086 * header, state cookie header struct)
2088 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2089 init_cp = (struct sctp_init_chunk *)
2090 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2091 (uint8_t *) & init_buf);
2092 if (init_cp == NULL) {
2093 /* could not pull a INIT chunk in cookie */
2094 SCTPDBG(SCTP_DEBUG_INPUT1,
2095 "process_cookie_new: could not pull INIT chunk hdr\n");
2098 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2099 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2102 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2104 * find and validate the INIT-ACK chunk in the cookie (my info) the
2105 * INIT-ACK follows the INIT chunk
2107 initack_cp = (struct sctp_init_ack_chunk *)
2108 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2109 (uint8_t *) & initack_buf);
2110 if (initack_cp == NULL) {
2111 /* could not pull INIT-ACK chunk in cookie */
2112 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2115 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2119 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2120 * "initack_limit" value. This is because the chk_length field
2121 * includes the length of the cookie, but the cookie is omitted when
2122 * the INIT and INIT_ACK are tacked onto the cookie...
2124 initack_limit = offset + cookie_len;
2127 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2132 * Here we do a trick, we set in NULL for the proc/thread argument. We
2133 * do this since in effect we only use the p argument when
2134 * the socket is unbound and we must do an implicit bind.
2135 * Since we are getting a cookie, we cannot be unbound.
2137 stcb = sctp_aloc_assoc(inp, init_src, &error,
2138 ntohl(initack_cp->init.initiate_tag), vrf_id,
2139 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2140 (struct thread *)NULL
2141 #elif defined(__Windows__)
2148 struct mbuf *op_err;
2150 /* memory problem? */
2151 SCTPDBG(SCTP_DEBUG_INPUT1,
2152 "process_cookie_new: no room for another TCB!\n");
2153 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2154 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2155 src, dst, sh, op_err,
2156 #if defined(__FreeBSD__)
2157 use_mflowid, mflowid,
2162 /* get the correct sctp_nets */
2164 *netp = sctp_findnet(stcb, init_src);
2167 /* get scope variables out of cookie */
2168 asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2169 asoc->scope.site_scope = cookie->site_scope;
2170 asoc->scope.local_scope = cookie->local_scope;
2171 asoc->scope.loopback_scope = cookie->loopback_scope;
2173 #if defined(__Userspace__)
2174 if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2175 (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal) ||
2176 (asoc->scope.conn_addr_legal != cookie->conn_addr_legal)) {
2178 if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2179 (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2181 struct mbuf *op_err;
2184 * Houston we have a problem. The EP changed while the
2185 * cookie was in flight. Only recourse is to abort the
2188 atomic_add_int(&stcb->asoc.refcnt, 1);
2189 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2190 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2191 src, dst, sh, op_err,
2192 #if defined(__FreeBSD__)
2193 use_mflowid, mflowid,
2196 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2197 SCTP_TCB_UNLOCK(stcb);
2198 SCTP_SOCKET_LOCK(so, 1);
2199 SCTP_TCB_LOCK(stcb);
2201 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2202 SCTP_FROM_SCTP_INPUT+SCTP_LOC_16);
2203 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2204 SCTP_SOCKET_UNLOCK(so, 1);
2206 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2209 /* process the INIT-ACK info (my info) */
2210 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2211 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2212 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2213 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2214 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2215 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2216 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2217 asoc->str_reset_seq_in = asoc->init_seq_number;
2219 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2221 /* process the INIT info (peer's info) */
2223 retval = sctp_process_init(init_cp, stcb);
2227 atomic_add_int(&stcb->asoc.refcnt, 1);
2228 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2229 SCTP_TCB_UNLOCK(stcb);
2230 SCTP_SOCKET_LOCK(so, 1);
2231 SCTP_TCB_LOCK(stcb);
2233 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_16);
2234 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2235 SCTP_SOCKET_UNLOCK(so, 1);
2237 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2240 /* load all addresses */
2241 if (sctp_load_addresses_from_init(stcb, m,
2242 init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2243 src, dst, init_src)) {
2244 atomic_add_int(&stcb->asoc.refcnt, 1);
2245 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2246 SCTP_TCB_UNLOCK(stcb);
2247 SCTP_SOCKET_LOCK(so, 1);
2248 SCTP_TCB_LOCK(stcb);
2250 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_17);
2251 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2252 SCTP_SOCKET_UNLOCK(so, 1);
2254 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2258 * verify any preceding AUTH chunk that was skipped
2260 /* pull the local authentication parameters from the cookie/init-ack */
2261 sctp_auth_get_cookie_params(stcb, m,
2262 initack_offset + sizeof(struct sctp_init_ack_chunk),
2263 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2265 struct sctp_auth_chunk *auth;
2267 auth = (struct sctp_auth_chunk *)
2268 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2269 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2270 /* auth HMAC failed, dump the assoc and packet */
2271 SCTPDBG(SCTP_DEBUG_AUTH1,
2272 "COOKIE-ECHO: AUTH failed\n");
2273 atomic_add_int(&stcb->asoc.refcnt, 1);
2274 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2275 SCTP_TCB_UNLOCK(stcb);
2276 SCTP_SOCKET_LOCK(so, 1);
2277 SCTP_TCB_LOCK(stcb);
2279 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_18);
2280 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2281 SCTP_SOCKET_UNLOCK(so, 1);
2283 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2286 /* remaining chunks checked... good to go */
2287 stcb->asoc.authenticated = 1;
2290 /* update current state */
2291 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2292 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2293 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2294 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2295 stcb->sctp_ep, stcb, asoc->primary_destination);
2297 sctp_stop_all_cookie_timers(stcb);
2298 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2299 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2302 * if we're doing ASCONFs, check to see if we have any new local
2303 * addresses that need to get added to the peer (eg. addresses
2304 * changed while cookie echo in flight). This needs to be done
2305 * after we go to the OPEN state to do the correct asconf
2306 * processing. else, make sure we have the correct addresses in our
2310 /* warning, we re-use sin, sin6, sa_store here! */
2311 /* pull in local_address (our "from" address) */
2312 switch (cookie->laddr_type) {
2314 case SCTP_IPV4_ADDRESS:
2315 /* source addr is IPv4 */
2316 memset(&store.sin, 0, sizeof(struct sockaddr_in));
2317 store.sin.sin_family = AF_INET;
2319 store.sin.sin_len = sizeof(struct sockaddr_in);
2321 store.sin.sin_addr.s_addr = cookie->laddress[0];
2325 case SCTP_IPV6_ADDRESS:
2326 /* source addr is IPv6 */
2327 memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
2328 store.sin6.sin6_family = AF_INET6;
2329 #ifdef HAVE_SIN6_LEN
2330 store.sin6.sin6_len = sizeof(struct sockaddr_in6);
2332 store.sin6.sin6_scope_id = cookie->scope_id;
2333 memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2336 #if defined(__Userspace__)
2337 case SCTP_CONN_ADDRESS:
2338 /* source addr is conn */
2339 memset(&store.sconn, 0, sizeof(struct sockaddr_conn));
2340 store.sconn.sconn_family = AF_CONN;
2341 #ifdef HAVE_SCONN_LEN
2342 store.sconn.sconn_len = sizeof(struct sockaddr_conn);
2344 memcpy(&store.sconn.sconn_addr, cookie->laddress, sizeof(void *));
2348 atomic_add_int(&stcb->asoc.refcnt, 1);
2349 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2350 SCTP_TCB_UNLOCK(stcb);
2351 SCTP_SOCKET_LOCK(so, 1);
2352 SCTP_TCB_LOCK(stcb);
2354 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_19);
2355 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2356 SCTP_SOCKET_UNLOCK(so, 1);
2358 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2362 /* set up to notify upper layer */
2363 *notification = SCTP_NOTIFY_ASSOC_UP;
2364 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2365 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2366 (inp->sctp_socket->so_qlimit == 0)) {
2368 * This is an endpoint that called connect() how it got a
2369 * cookie that is NEW is a bit of a mystery. It must be that
2370 * the INIT was sent, but before it got there.. a complete
2371 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2372 * should have went to the other code.. not here.. oh well..
2373 * a bit of protection is worth having..
2375 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2376 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2377 atomic_add_int(&stcb->asoc.refcnt, 1);
2378 SCTP_TCB_UNLOCK(stcb);
2379 SCTP_SOCKET_LOCK(so, 1);
2380 SCTP_TCB_LOCK(stcb);
2381 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2382 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2383 SCTP_SOCKET_UNLOCK(so, 1);
2387 soisconnected(stcb->sctp_socket);
2388 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2389 SCTP_SOCKET_UNLOCK(so, 1);
2391 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2392 (inp->sctp_socket->so_qlimit)) {
2394 * We don't want to do anything with this one. Since it is
2395 * the listening guy. The timer will get started for
2396 * accepted connections in the caller.
2400 /* since we did not send a HB make sure we don't double things */
2401 if ((netp) && (*netp))
2402 (*netp)->hb_responded = 1;
2404 if (stcb->asoc.sctp_autoclose_ticks &&
2405 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2406 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2408 /* calculate the RTT */
2409 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2410 if ((netp) && (*netp)) {
2411 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2412 &cookie->time_entered, sctp_align_unsafe_makecopy,
2413 SCTP_RTT_FROM_NON_DATA);
2415 /* respond with a COOKIE-ACK */
2416 sctp_send_cookie_ack(stcb);
2419 * check the address lists for any ASCONFs that need to be sent
2420 * AFTER the cookie-ack is sent
2422 sctp_check_address_list(stcb, m,
2423 initack_offset + sizeof(struct sctp_init_ack_chunk),
2424 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2425 &store.sa, cookie->local_scope, cookie->site_scope,
2426 cookie->ipv4_scope, cookie->loopback_scope);
2433 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2434 * we NEED to make sure we are not already using the vtag. If so we
2435 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2436 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2437 SCTP_BASE_INFO(hashasocmark))];
2438 LIST_FOREACH(stcb, head, sctp_asocs) {
2439 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2440 -- SEND ABORT - TRY AGAIN --
2446 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2447 * existing (non-NULL) TCB
2449 static struct mbuf *
2450 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2451 struct sockaddr *src, struct sockaddr *dst,
2452 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2453 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2454 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2455 struct sctp_tcb **locked_tcb,
2456 #if defined(__FreeBSD__)
2457 uint8_t use_mflowid, uint32_t mflowid,
2459 uint32_t vrf_id, uint16_t port)
2461 struct sctp_state_cookie *cookie;
2462 struct sctp_tcb *l_stcb = *stcb;
2463 struct sctp_inpcb *l_inp;
2464 struct sockaddr *to;
2465 struct sctp_pcb *ep;
2467 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2469 uint8_t cookie_ok = 0;
2470 unsigned int sig_offset, cookie_offset;
2471 unsigned int cookie_len;
2473 struct timeval time_expires;
2474 int notification = 0;
2475 struct sctp_nets *netl;
2476 int had_a_existing_tcb = 0;
2477 int send_int_conf = 0;
2479 struct sockaddr_in sin;
2482 struct sockaddr_in6 sin6;
2484 #if defined(__Userspace__)
2485 struct sockaddr_conn sconn;
2488 SCTPDBG(SCTP_DEBUG_INPUT2,
2489 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2491 if (inp_p == NULL) {
2494 cookie = &cp->cookie;
2495 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2496 cookie_len = ntohs(cp->ch.chunk_length);
2498 if ((cookie->peerport != sh->src_port) &&
2499 (cookie->myport != sh->dest_port) &&
2500 (cookie->my_vtag != sh->v_tag)) {
2502 * invalid ports or bad tag. Note that we always leave the
2503 * v_tag in the header in network order and when we stored
2504 * it in the my_vtag slot we also left it in network order.
2505 * This maintains the match even though it may be in the
2506 * opposite byte order of the machine :->
2510 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2511 sizeof(struct sctp_init_chunk) +
2512 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2513 /* cookie too small */
2517 * split off the signature into its own mbuf (since it should not be
2518 * calculated in the sctp_hmac_m() call).
2520 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2521 m_sig = m_split(m, sig_offset, M_NOWAIT);
2522 if (m_sig == NULL) {
2523 /* out of memory or ?? */
2526 #ifdef SCTP_MBUF_LOGGING
2527 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2530 for (mat = m_sig; mat; mat = SCTP_BUF_NEXT(mat)) {
2531 if (SCTP_BUF_IS_EXTENDED(mat)) {
2532 sctp_log_mb(mat, SCTP_MBUF_SPLIT);
2539 * compute the signature/digest for the cookie
2541 ep = &(*inp_p)->sctp_ep;
2544 SCTP_TCB_UNLOCK(l_stcb);
2546 SCTP_INP_RLOCK(l_inp);
2548 SCTP_TCB_LOCK(l_stcb);
2550 /* which cookie is it? */
2551 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2552 (ep->current_secret_number != ep->last_secret_number)) {
2553 /* it's the old cookie */
2554 (void)sctp_hmac_m(SCTP_HMAC,
2555 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2556 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2558 /* it's the current cookie */
2559 (void)sctp_hmac_m(SCTP_HMAC,
2560 (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2561 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2563 /* get the signature */
2564 SCTP_INP_RUNLOCK(l_inp);
2565 sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2567 /* couldn't find signature */
2568 sctp_m_freem(m_sig);
2571 /* compare the received digest with the computed digest */
2572 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2573 /* try the old cookie? */
2574 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2575 (ep->current_secret_number != ep->last_secret_number)) {
2576 /* compute digest with old */
2577 (void)sctp_hmac_m(SCTP_HMAC,
2578 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2579 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2581 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2589 * Now before we continue we must reconstruct our mbuf so that
2590 * normal processing of any other chunks will work.
2596 while (SCTP_BUF_NEXT(m_at) != NULL) {
2597 m_at = SCTP_BUF_NEXT(m_at);
2599 SCTP_BUF_NEXT(m_at) = m_sig;
2602 if (cookie_ok == 0) {
2603 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2604 SCTPDBG(SCTP_DEBUG_INPUT2,
2605 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2606 (uint32_t) offset, cookie_offset, sig_offset);
2611 * check the cookie timestamps to be sure it's not stale
2613 (void)SCTP_GETTIME_TIMEVAL(&now);
2614 /* Expire time is in Ticks, so we convert to seconds */
2615 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2616 time_expires.tv_usec = cookie->time_entered.tv_usec;
2617 /* TODO sctp_constants.h needs alternative time macros when
2618 * _KERNEL is undefined.
2621 if (timercmp(&now, &time_expires, >))
2623 if (timevalcmp(&now, &time_expires, >))
2626 /* cookie is stale! */
2627 struct mbuf *op_err;
2628 struct sctp_stale_cookie_msg *scm;
2630 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2631 0, M_NOWAIT, 1, MT_DATA);
2632 if (op_err == NULL) {
2637 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2638 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2639 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2640 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2641 (sizeof(uint32_t))));
2642 /* seconds to usec */
2643 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2646 tim = now.tv_usec - cookie->time_entered.tv_usec;
2647 scm->time_usec = htonl(tim);
2648 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2649 #if defined(__FreeBSD__)
2650 use_mflowid, mflowid,
2656 * Now we must see with the lookup address if we have an existing
2657 * asoc. This will only happen if we were in the COOKIE-WAIT state
2658 * and a INIT collided with us and somewhere the peer sent the
2659 * cookie on another address besides the single address our assoc
2660 * had for him. In this case we will have one of the tie-tags set at
2661 * least AND the address field in the cookie can be used to look it
2665 switch (cookie->addr_type) {
2667 case SCTP_IPV6_ADDRESS:
2668 memset(&sin6, 0, sizeof(sin6));
2669 sin6.sin6_family = AF_INET6;
2670 #ifdef HAVE_SIN6_LEN
2671 sin6.sin6_len = sizeof(sin6);
2673 sin6.sin6_port = sh->src_port;
2674 sin6.sin6_scope_id = cookie->scope_id;
2675 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2676 sizeof(sin6.sin6_addr.s6_addr));
2677 to = (struct sockaddr *)&sin6;
2681 case SCTP_IPV4_ADDRESS:
2682 memset(&sin, 0, sizeof(sin));
2683 sin.sin_family = AF_INET;
2685 sin.sin_len = sizeof(sin);
2687 sin.sin_port = sh->src_port;
2688 sin.sin_addr.s_addr = cookie->address[0];
2689 to = (struct sockaddr *)&sin;
2692 #if defined(__Userspace__)
2693 case SCTP_CONN_ADDRESS:
2694 memset(&sconn, 0, sizeof(struct sockaddr_conn));
2695 sconn.sconn_family = AF_CONN;
2696 #ifdef HAVE_SCONN_LEN
2697 sconn.sconn_len = sizeof(struct sockaddr_conn);
2699 sconn.sconn_port = sh->src_port;
2700 memcpy(&sconn.sconn_addr, cookie->address, sizeof(void *));
2701 to = (struct sockaddr *)&sconn;
2705 /* This should not happen */
2708 if ((*stcb == NULL) && to) {
2709 /* Yep, lets check */
2710 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2711 if (*stcb == NULL) {
2713 * We should have only got back the same inp. If we
2714 * got back a different ep we have a problem. The
2715 * original findep got back l_inp and now
2717 if (l_inp != *inp_p) {
2718 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2721 if (*locked_tcb == NULL) {
2722 /* In this case we found the assoc only
2723 * after we locked the create lock. This means
2724 * we are in a colliding case and we must make
2725 * sure that we unlock the tcb if its one of the
2726 * cases where we throw away the incoming packets.
2728 *locked_tcb = *stcb;
2730 /* We must also increment the inp ref count
2731 * since the ref_count flags was set when we
2732 * did not find the TCB, now we found it which
2733 * reduces the refcount.. we must raise it back
2734 * out to balance it all :-)
2736 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2737 if ((*stcb)->sctp_ep != l_inp) {
2738 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2739 (void *)(*stcb)->sctp_ep, (void *)l_inp);
2748 cookie_len -= SCTP_SIGNATURE_SIZE;
2749 if (*stcb == NULL) {
2750 /* this is the "normal" case... get a new TCB */
2751 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2752 cookie, cookie_len, *inp_p,
2753 netp, to, ¬ification,
2754 auth_skipped, auth_offset, auth_len,
2755 #if defined(__FreeBSD__)
2756 use_mflowid, mflowid,
2760 /* this is abnormal... cookie-echo on existing TCB */
2761 had_a_existing_tcb = 1;
2762 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2764 cookie, cookie_len, *inp_p, *stcb, netp, to,
2765 ¬ification, auth_skipped, auth_offset, auth_len,
2766 #if defined(__FreeBSD__)
2767 use_mflowid, mflowid,
2772 if (*stcb == NULL) {
2773 /* still no TCB... must be bad cookie-echo */
2776 #if defined(__FreeBSD__)
2777 if ((*netp != NULL) && (use_mflowid != 0)) {
2778 (*netp)->flowid = mflowid;
2780 (*netp)->flowidset = 1;
2785 * Ok, we built an association so confirm the address we sent the
2788 netl = sctp_findnet(*stcb, to);
2790 * This code should in theory NOT run but
2793 /* TSNH! Huh, why do I need to add this address here? */
2794 if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2797 netl = sctp_findnet(*stcb, to);
2800 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2801 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2802 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2807 sctp_start_net_timers(*stcb);
2808 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2809 if (!had_a_existing_tcb ||
2810 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2812 * If we have a NEW cookie or the connect never
2813 * reached the connected state during collision we
2814 * must do the TCP accept thing.
2816 struct socket *so, *oso;
2817 struct sctp_inpcb *inp;
2819 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2821 * For a restart we will keep the same
2822 * socket, no need to do anything. I THINK!!
2824 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2825 if (send_int_conf) {
2826 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2827 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2831 oso = (*inp_p)->sctp_socket;
2832 #if (defined(__FreeBSD__) && __FreeBSD_version < 700000)
2834 * We do this to keep the sockets side happy during
2835 * the sonewcon ONLY.
2839 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2840 SCTP_TCB_UNLOCK((*stcb));
2841 #if defined(__FreeBSD__) && __FreeBSD_version >= 801000
2842 CURVNET_SET(oso->so_vnet);
2844 #if defined(__APPLE__)
2845 SCTP_SOCKET_LOCK(oso, 1);
2847 so = sonewconn(oso, 0
2848 #if defined(__APPLE__)
2852 ,NULL , (*inp_p)->def_vrf_id
2855 #if (defined(__FreeBSD__) && __FreeBSD_version < 700000)
2858 #if defined(__APPLE__)
2859 SCTP_SOCKET_UNLOCK(oso, 1);
2861 #if defined(__FreeBSD__) && __FreeBSD_version >= 801000
2864 SCTP_TCB_LOCK((*stcb));
2865 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2868 struct mbuf *op_err;
2869 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2870 struct socket *pcb_so;
2872 /* Too many sockets */
2873 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2874 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2875 sctp_abort_association(*inp_p, NULL, m, iphlen,
2876 src, dst, sh, op_err,
2877 #if defined(__FreeBSD__)
2878 use_mflowid, mflowid,
2881 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2882 pcb_so = SCTP_INP_SO(*inp_p);
2883 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2884 SCTP_TCB_UNLOCK((*stcb));
2885 SCTP_SOCKET_LOCK(pcb_so, 1);
2886 SCTP_TCB_LOCK((*stcb));
2887 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2889 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_20);
2890 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2891 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2895 inp = (struct sctp_inpcb *)so->so_pcb;
2896 SCTP_INP_INCR_REF(inp);
2898 * We add the unbound flag here so that
2899 * if we get an soabort() before we get the
2900 * move_pcb done, we will properly cleanup.
2902 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2903 SCTP_PCB_FLAGS_CONNECTED |
2904 SCTP_PCB_FLAGS_IN_TCPPOOL |
2905 SCTP_PCB_FLAGS_UNBOUND |
2906 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2907 SCTP_PCB_FLAGS_DONT_WAKE);
2908 inp->sctp_features = (*inp_p)->sctp_features;
2909 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2910 inp->sctp_socket = so;
2911 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2912 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2913 inp->ecn_supported = (*inp_p)->ecn_supported;
2914 inp->prsctp_supported = (*inp_p)->prsctp_supported;
2915 inp->auth_supported = (*inp_p)->auth_supported;
2916 inp->asconf_supported = (*inp_p)->asconf_supported;
2917 inp->reconfig_supported = (*inp_p)->reconfig_supported;
2918 inp->nrsack_supported = (*inp_p)->nrsack_supported;
2919 inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2920 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2921 inp->sctp_context = (*inp_p)->sctp_context;
2922 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2923 inp->inp_starting_point_for_iterator = NULL;
2924 #if defined(__Userspace__)
2925 inp->ulp_info = (*inp_p)->ulp_info;
2926 inp->recv_callback = (*inp_p)->recv_callback;
2927 inp->send_callback = (*inp_p)->send_callback;
2928 inp->send_sb_threshold = (*inp_p)->send_sb_threshold;
2931 * copy in the authentication parameters from the
2934 if (inp->sctp_ep.local_hmacs)
2935 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2936 inp->sctp_ep.local_hmacs =
2937 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2938 if (inp->sctp_ep.local_auth_chunks)
2939 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2940 inp->sctp_ep.local_auth_chunks =
2941 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2944 * Now we must move it from one hash table to
2945 * another and get the tcb in the right place.
2948 /* This is where the one-2-one socket is put into
2949 * the accept state waiting for the accept!
2952 (*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2954 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2956 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2957 SCTP_TCB_UNLOCK((*stcb));
2959 #if defined(__FreeBSD__)
2960 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2963 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, M_NOWAIT);
2965 SCTP_TCB_LOCK((*stcb));
2966 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2969 /* now we must check to see if we were aborted while
2970 * the move was going on and the lock/unlock happened.
2972 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2973 /* yep it was, we leave the
2974 * assoc attached to the socket since
2975 * the sctp_inpcb_free() call will send
2978 SCTP_INP_DECR_REF(inp);
2981 SCTP_INP_DECR_REF(inp);
2982 /* Switch over to the new guy */
2984 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2985 if (send_int_conf) {
2986 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2987 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2990 /* Pull it from the incomplete queue and wake the guy */
2991 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2992 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2993 SCTP_TCB_UNLOCK((*stcb));
2994 SCTP_SOCKET_LOCK(so, 1);
2997 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2998 SCTP_TCB_LOCK((*stcb));
2999 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
3000 SCTP_SOCKET_UNLOCK(so, 1);
3006 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3008 if (send_int_conf) {
3009 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
3010 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
3016 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
3017 struct sctp_tcb *stcb, struct sctp_nets *net)
3019 /* cp must not be used, others call this without a c-ack :-) */
3020 struct sctp_association *asoc;
3022 SCTPDBG(SCTP_DEBUG_INPUT2,
3023 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
3024 if ((stcb == NULL) || (net == NULL)) {
3030 sctp_stop_all_cookie_timers(stcb);
3031 /* process according to association state */
3032 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
3033 /* state change only needed when I am in right state */
3034 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
3035 SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
3036 sctp_start_net_timers(stcb);
3037 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
3038 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
3039 stcb->sctp_ep, stcb, asoc->primary_destination);
3043 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
3044 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
3045 if (asoc->overall_error_count == 0) {
3046 net->RTO = sctp_calculate_rto(stcb, asoc, net,
3047 &asoc->time_entered, sctp_align_safe_nocopy,
3048 SCTP_RTT_FROM_NON_DATA);
3050 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
3051 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3052 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3053 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3054 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3058 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3059 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3060 so = SCTP_INP_SO(stcb->sctp_ep);
3061 atomic_add_int(&stcb->asoc.refcnt, 1);
3062 SCTP_TCB_UNLOCK(stcb);
3063 SCTP_SOCKET_LOCK(so, 1);
3064 SCTP_TCB_LOCK(stcb);
3065 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3067 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
3068 soisconnected(stcb->sctp_socket);
3070 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3071 SCTP_SOCKET_UNLOCK(so, 1);
3075 * since we did not send a HB make sure we don't double
3078 net->hb_responded = 1;
3080 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3081 /* We don't need to do the asconf thing,
3082 * nor hb or autoclose if the socket is closed.
3087 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
3091 if (stcb->asoc.sctp_autoclose_ticks &&
3092 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
3093 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
3094 stcb->sctp_ep, stcb, NULL);
3097 * send ASCONF if parameters are pending and ASCONFs are
3098 * allowed (eg. addresses changed when init/cookie echo were
3101 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
3102 (stcb->asoc.asconf_supported == 1) &&
3103 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
3104 #ifdef SCTP_TIMER_BASED_ASCONF
3105 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
3106 stcb->sctp_ep, stcb,
3107 stcb->asoc.primary_destination);
3109 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
3110 SCTP_ADDR_NOT_LOCKED);
3115 /* Toss the cookie if I can */
3116 sctp_toss_old_cookies(stcb, asoc);
3117 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3118 /* Restart the timer if we have pending data */
3119 struct sctp_tmit_chunk *chk;
3121 chk = TAILQ_FIRST(&asoc->sent_queue);
3122 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3127 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3128 struct sctp_tcb *stcb)
3130 struct sctp_nets *net;
3131 struct sctp_tmit_chunk *lchk;
3132 struct sctp_ecne_chunk bkup;
3133 uint8_t override_bit;
3134 uint32_t tsn, window_data_tsn;
3136 unsigned int pkt_cnt;
3138 len = ntohs(cp->ch.chunk_length);
3139 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3140 (len != sizeof(struct old_sctp_ecne_chunk))) {
3143 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3144 /* Its the old format */
3145 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3146 bkup.num_pkts_since_cwr = htonl(1);
3149 SCTP_STAT_INCR(sctps_recvecne);
3150 tsn = ntohl(cp->tsn);
3151 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3152 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3154 window_data_tsn = stcb->asoc.sending_seq - 1;
3156 window_data_tsn = lchk->rec.data.TSN_seq;
3159 /* Find where it was sent to if possible. */
3161 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3162 if (lchk->rec.data.TSN_seq == tsn) {
3164 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3167 if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) {
3173 * What to do. A previous send of a
3174 * CWR was possibly lost. See how old it is, we
3175 * may have it marked on the actual net.
3177 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3178 if (tsn == net->last_cwr_tsn) {
3179 /* Found him, send it off */
3185 * If we reach here, we need to send a special
3186 * CWR that says hey, we did this a long time
3187 * ago and you lost the response.
3189 net = TAILQ_FIRST(&stcb->asoc.nets);
3194 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3201 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3202 ((override_bit&SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3203 /* JRS - Use the congestion control given in the pluggable CC module */
3204 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3206 * We reduce once every RTT. So we will only lower cwnd at
3207 * the next sending seq i.e. the window_data_tsn
3209 net->cwr_window_tsn = window_data_tsn;
3210 net->ecn_ce_pkt_cnt += pkt_cnt;
3211 net->lost_cnt = pkt_cnt;
3212 net->last_cwr_tsn = tsn;
3214 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3215 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3216 ((override_bit&SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3218 * Another loss in the same window update how
3219 * many marks/packets lost we have had.
3222 if (pkt_cnt > net->lost_cnt) {
3223 /* Should be the case */
3224 cnt = (pkt_cnt - net->lost_cnt);
3225 net->ecn_ce_pkt_cnt += cnt;
3227 net->lost_cnt = pkt_cnt;
3228 net->last_cwr_tsn = tsn;
3230 * Most CC functions will ignore this call, since we are in-window
3231 * yet of the initial CE the peer saw.
3233 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3237 * We always send a CWR this way if our previous one was lost our
3238 * peer will get an update, or if it is not time again to reduce we
3239 * still get the cwr to the peer. Note we set the override when we
3240 * could not find the TSN on the chunk or the destination network.
3242 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3246 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3249 * Here we get a CWR from the peer. We must look in the outqueue and
3250 * make sure that we have a covered ECNE in the control chunk part.
3253 struct sctp_tmit_chunk *chk;
3254 struct sctp_ecne_chunk *ecne;
3257 cwr_tsn = ntohl(cp->tsn);
3259 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3260 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
3261 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3264 if ((override == 0) && (chk->whoTo != net)) {
3265 /* Must be from the right src unless override is set */
3268 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3269 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3270 /* this covers this ECNE, we can remove it */
3271 stcb->asoc.ecn_echo_cnt_onq--;
3272 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3275 sctp_m_freem(chk->data);
3278 stcb->asoc.ctrl_queue_cnt--;
3279 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3280 if (override == 0) {
3288 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3289 struct sctp_tcb *stcb, struct sctp_nets *net)
3291 struct sctp_association *asoc;
3292 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3296 SCTPDBG(SCTP_DEBUG_INPUT2,
3297 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3302 /* process according to association state */
3303 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3304 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3305 SCTPDBG(SCTP_DEBUG_INPUT2,
3306 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3307 SCTP_TCB_UNLOCK(stcb);
3310 /* notify upper layer protocol */
3311 if (stcb->sctp_socket) {
3312 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3315 if (!TAILQ_EMPTY(&asoc->send_queue) ||
3316 !TAILQ_EMPTY(&asoc->sent_queue) ||
3317 !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
3318 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3321 /* stop the timer */
3322 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_22);
3323 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3325 SCTPDBG(SCTP_DEBUG_INPUT2,
3326 "sctp_handle_shutdown_complete: calls free-asoc\n");
3327 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3328 so = SCTP_INP_SO(stcb->sctp_ep);
3329 atomic_add_int(&stcb->asoc.refcnt, 1);
3330 SCTP_TCB_UNLOCK(stcb);
3331 SCTP_SOCKET_LOCK(so, 1);
3332 SCTP_TCB_LOCK(stcb);
3333 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3335 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_23);
3336 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3337 SCTP_SOCKET_UNLOCK(so, 1);
3343 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3344 struct sctp_nets *net, uint8_t flg)
3346 switch (desc->chunk_type) {
3348 /* find the tsn to resend (possibly */
3351 struct sctp_tmit_chunk *tp1;
3353 tsn = ntohl(desc->tsn_ifany);
3354 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3355 if (tp1->rec.data.TSN_seq == tsn) {
3359 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) {
3367 * Do it the other way , aka without paying
3368 * attention to queue seq order.
3370 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3371 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3372 if (tp1->rec.data.TSN_seq == tsn) {
3379 SCTP_STAT_INCR(sctps_pdrptsnnf);
3381 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3384 if (((flg & SCTP_BADCRC) == 0) &&
3385 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3388 if ((stcb->asoc.peers_rwnd == 0) &&
3389 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3390 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3393 if (stcb->asoc.peers_rwnd == 0 &&
3394 (flg & SCTP_FROM_MIDDLE_BOX)) {
3395 SCTP_STAT_INCR(sctps_pdrpdizrw);
3398 ddp = (uint8_t *) (mtod(tp1->data, caddr_t) +
3399 sizeof(struct sctp_data_chunk));
3403 for (iii = 0; iii < sizeof(desc->data_bytes);
3405 if (ddp[iii] != desc->data_bytes[iii]) {
3406 SCTP_STAT_INCR(sctps_pdrpbadd);
3414 * this guy had a RTO calculation
3415 * pending on it, cancel it
3417 if (tp1->whoTo->rto_needed == 0) {
3418 tp1->whoTo->rto_needed = 1;
3422 SCTP_STAT_INCR(sctps_pdrpmark);
3423 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3424 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3426 * mark it as if we were doing a FR, since
3427 * we will be getting gap ack reports behind
3428 * the info from the router.
3430 tp1->rec.data.doing_fast_retransmit = 1;
3432 * mark the tsn with what sequences can
3435 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3436 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3438 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
3441 /* restart the timer */
3442 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3443 stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT+SCTP_LOC_24);
3444 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3447 /* fix counts and things */
3448 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3449 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3450 tp1->whoTo->flight_size,
3453 tp1->rec.data.TSN_seq);
3455 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3456 sctp_flight_size_decrease(tp1);
3457 sctp_total_flight_decrease(stcb, tp1);
3459 tp1->sent = SCTP_DATAGRAM_RESEND;
3465 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3466 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3469 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3471 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3474 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3475 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3476 audit, stcb->asoc.sent_queue_retran_cnt);
3477 #ifndef SCTP_AUDITING_ENABLED
3478 stcb->asoc.sent_queue_retran_cnt = audit;
3486 struct sctp_tmit_chunk *asconf;
3488 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3490 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3495 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3496 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3497 asconf->sent = SCTP_DATAGRAM_RESEND;
3498 asconf->snd_count--;
3502 case SCTP_INITIATION:
3503 /* resend the INIT */
3504 stcb->asoc.dropped_special_cnt++;
3505 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3507 * If we can get it in, in a few attempts we do
3508 * this, otherwise we let the timer fire.
3510 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3511 stcb, net, SCTP_FROM_SCTP_INPUT+SCTP_LOC_25);
3512 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3515 case SCTP_SELECTIVE_ACK:
3516 case SCTP_NR_SELECTIVE_ACK:
3517 /* resend the sack */
3518 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3520 case SCTP_HEARTBEAT_REQUEST:
3521 /* resend a demand HB */
3522 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3523 /* Only retransmit if we KNOW we wont destroy the tcb */
3524 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3528 sctp_send_shutdown(stcb, net);
3530 case SCTP_SHUTDOWN_ACK:
3531 sctp_send_shutdown_ack(stcb, net);
3533 case SCTP_COOKIE_ECHO:
3535 struct sctp_tmit_chunk *cookie;
3538 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3540 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3545 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3546 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3547 cookie->sent = SCTP_DATAGRAM_RESEND;
3548 sctp_stop_all_cookie_timers(stcb);
3552 case SCTP_COOKIE_ACK:
3553 sctp_send_cookie_ack(stcb);
3555 case SCTP_ASCONF_ACK:
3556 /* resend last asconf ack */
3557 sctp_send_asconf_ack(stcb);
3559 case SCTP_FORWARD_CUM_TSN:
3560 send_forward_tsn(stcb, &stcb->asoc);
3562 /* can't do anything with these */
3563 case SCTP_PACKET_DROPPED:
3564 case SCTP_INITIATION_ACK: /* this should not happen */
3565 case SCTP_HEARTBEAT_ACK:
3566 case SCTP_ABORT_ASSOCIATION:
3567 case SCTP_OPERATION_ERROR:
3568 case SCTP_SHUTDOWN_COMPLETE:
3578 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3584 * We set things to 0xffff since this is the last delivered sequence
3585 * and we will be sending in 0 after the reset.
3588 if (number_entries) {
3589 for (i = 0; i < number_entries; i++) {
3590 temp = ntohs(list[i]);
3591 if (temp >= stcb->asoc.streamincnt) {
3594 stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
3598 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3599 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3602 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3606 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3611 if (number_entries > 0) {
3612 for (i = 0; i < number_entries; i++) {
3613 temp = ntohs(list[i]);
3614 if (temp >= stcb->asoc.streamoutcnt) {
3615 /* no such stream */
3618 stcb->asoc.strmout[temp].next_sequence_send = 0;
3621 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3622 stcb->asoc.strmout[i].next_sequence_send = 0;
3625 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3629 struct sctp_stream_reset_out_request *
3630 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3632 struct sctp_association *asoc;
3633 struct sctp_chunkhdr *ch;
3634 struct sctp_stream_reset_out_request *r;
3635 struct sctp_tmit_chunk *chk;
3639 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3640 asoc->stream_reset_outstanding = 0;
3643 if (stcb->asoc.str_reset == NULL) {
3644 asoc->stream_reset_outstanding = 0;
3647 chk = stcb->asoc.str_reset;
3648 if (chk->data == NULL) {
3652 /* he wants a copy of the chk pointer */
3655 clen = chk->send_size;
3656 ch = mtod(chk->data, struct sctp_chunkhdr *);
3657 r = (struct sctp_stream_reset_out_request *)(ch + 1);
3658 if (ntohl(r->request_seq) == seq) {
3662 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3663 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3664 /* move to the next one, there can only be a max of two */
3665 r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
3666 if (ntohl(r->request_seq) == seq) {
3670 /* that seq is not here */
3675 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3677 struct sctp_association *asoc;
3678 struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3680 if (stcb->asoc.str_reset == NULL) {
3685 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT+SCTP_LOC_26);
3686 TAILQ_REMOVE(&asoc->control_send_queue,
3690 sctp_m_freem(chk->data);
3693 asoc->ctrl_queue_cnt--;
3694 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3695 /*sa_ignore NO_NULL_CHK*/
3696 stcb->asoc.str_reset = NULL;
3701 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3702 uint32_t seq, uint32_t action,
3703 struct sctp_stream_reset_response *respin)
3707 struct sctp_association *asoc = &stcb->asoc;
3708 struct sctp_tmit_chunk *chk;
3709 struct sctp_stream_reset_out_request *srparam;
3710 uint32_t number_entries;
3712 if (asoc->stream_reset_outstanding == 0) {
3716 if (seq == stcb->asoc.str_reset_seq_out) {
3717 srparam = sctp_find_stream_reset(stcb, seq, &chk);
3719 stcb->asoc.str_reset_seq_out++;
3720 type = ntohs(srparam->ph.param_type);
3721 lparm_len = ntohs(srparam->ph.param_length);
3722 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3723 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3724 asoc->stream_reset_out_is_outstanding = 0;
3725 if (asoc->stream_reset_outstanding)
3726 asoc->stream_reset_outstanding--;
3727 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3729 sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
3730 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3731 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3733 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3735 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3736 /* Answered my request */
3737 number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3738 if (asoc->stream_reset_outstanding)
3739 asoc->stream_reset_outstanding--;
3740 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3741 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3742 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3743 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3744 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3745 number_entries, srparam->list_of_streams, SCTP_SO_NOT_LOCKED);
3747 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3748 /* Ok we now may have more streams */
3751 num_stream = stcb->asoc.strm_pending_add_size;
3752 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3754 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3756 stcb->asoc.strm_pending_add_size = 0;
3757 if (asoc->stream_reset_outstanding)
3758 asoc->stream_reset_outstanding--;
3759 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3760 /* Put the new streams into effect */
3761 stcb->asoc.streamoutcnt += num_stream;
3762 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3763 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3764 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3765 SCTP_STREAM_CHANGE_DENIED);
3767 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3768 SCTP_STREAM_CHANGE_FAILED);
3770 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3771 if (asoc->stream_reset_outstanding)
3772 asoc->stream_reset_outstanding--;
3773 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3774 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3775 SCTP_STREAM_CHANGE_DENIED);
3776 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3777 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3778 SCTP_STREAM_CHANGE_FAILED);
3780 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3782 * a) Adopt the new in tsn.
3784 * c) Adopt the new out-tsn
3786 struct sctp_stream_reset_response_tsn *resp;
3787 struct sctp_forward_tsn_chunk fwdtsn;
3789 if (respin == NULL) {
3793 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3794 resp = (struct sctp_stream_reset_response_tsn *)respin;
3795 asoc->stream_reset_outstanding--;
3796 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3797 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3798 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3799 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3803 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3804 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3805 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3808 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3809 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3810 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3812 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3813 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3815 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3816 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3818 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3819 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3820 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3821 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3822 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3823 SCTP_ASSOC_RESET_DENIED);
3825 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3826 SCTP_ASSOC_RESET_FAILED);
3829 /* get rid of the request and get the request flags */
3830 if (asoc->stream_reset_outstanding == 0) {
3831 sctp_clean_up_stream_reset(stcb);
3839 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3840 struct sctp_tmit_chunk *chk,
3841 struct sctp_stream_reset_in_request *req, int trunc)
3849 * peer wants me to send a str-reset to him for my outgoing seq's if
3852 struct sctp_association *asoc = &stcb->asoc;
3854 seq = ntohl(req->request_seq);
3855 if (asoc->str_reset_seq_in == seq) {
3856 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3857 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3858 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3860 /* Can't do it, since they exceeded our buffer size */
3861 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3862 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3863 len = ntohs(req->ph.param_length);
3864 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3865 for (i = 0; i < number_entries; i++) {
3866 temp = ntohs(req->list_of_streams[i]);
3867 req->list_of_streams[i] = temp;
3869 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3870 sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
3871 asoc->str_reset_seq_out,
3872 seq, (asoc->sending_seq - 1));
3873 asoc->stream_reset_out_is_outstanding = 1;
3874 asoc->str_reset = chk;
3875 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
3876 stcb->asoc.stream_reset_outstanding++;
3878 /* Can't do it, since we have sent one out */
3879 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3881 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3882 asoc->str_reset_seq_in++;
3883 } else if (asoc->str_reset_seq_in - 1 == seq) {
3884 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3885 } else if (asoc->str_reset_seq_in - 2 == seq) {
3886 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3888 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3893 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3894 struct sctp_tmit_chunk *chk,
3895 struct sctp_stream_reset_tsn_request *req)
3897 /* reset all in and out and update the tsn */
3899 * A) reset my str-seq's on in and out. B) Select a receive next,
3900 * and set cum-ack to it. Also process this selected number as a
3901 * fwd-tsn as well. C) set in the response my next sending seq.
3903 struct sctp_forward_tsn_chunk fwdtsn;
3904 struct sctp_association *asoc = &stcb->asoc;
3908 seq = ntohl(req->request_seq);
3909 if (asoc->str_reset_seq_in == seq) {
3910 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3911 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3912 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3914 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3915 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3916 fwdtsn.ch.chunk_flags = 0;
3917 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3918 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3922 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3923 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3924 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3926 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3927 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3928 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3929 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3930 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3931 atomic_add_int(&asoc->sending_seq, 1);
3932 /* save off historical data for retrans */
3933 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3934 asoc->last_sending_seq[0] = asoc->sending_seq;
3935 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3936 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3937 sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3938 sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3939 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3940 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3942 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3943 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3944 asoc->str_reset_seq_in++;
3945 } else if (asoc->str_reset_seq_in - 1 == seq) {
3946 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3947 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3948 } else if (asoc->str_reset_seq_in - 2 == seq) {
3949 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3950 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3952 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3958 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3959 struct sctp_tmit_chunk *chk,
3960 struct sctp_stream_reset_out_request *req, int trunc)
3963 int number_entries, len;
3964 struct sctp_association *asoc = &stcb->asoc;
3966 seq = ntohl(req->request_seq);
3968 /* now if its not a duplicate we process it */
3969 if (asoc->str_reset_seq_in == seq) {
3970 len = ntohs(req->ph.param_length);
3971 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3973 * the sender is resetting, handle the list issue.. we must
3974 * a) verify if we can do the reset, if so no problem b) If
3975 * we can't do the reset we must copy the request. c) queue
3976 * it, and setup the data in processor to trigger it off
3977 * when needed and dequeue all the queued data.
3979 tsn = ntohl(req->send_reset_at_tsn);
3981 /* move the reset action back one */
3982 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3983 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3984 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3986 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3987 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3988 /* we can do it now */
3989 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3990 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3993 * we must queue it up and thus wait for the TSN's
3994 * to arrive that are at or before tsn
3996 struct sctp_stream_reset_list *liste;
3999 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
4000 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
4001 siz, SCTP_M_STRESET);
4002 if (liste == NULL) {
4003 /* gak out of memory */
4004 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4005 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4009 liste->number_entries = number_entries;
4010 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
4011 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
4012 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4014 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4015 asoc->str_reset_seq_in++;
4016 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4018 * one seq back, just echo back last action since my
4019 * response was lost.
4021 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4022 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4024 * two seq back, just echo back last action since my
4025 * response was lost.
4027 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4029 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4034 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4035 struct sctp_stream_reset_add_strm *str_add)
4038 * Peer is requesting to add more streams.
4039 * If its within our max-streams we will
4042 uint32_t num_stream, i;
4044 struct sctp_association *asoc = &stcb->asoc;
4045 struct sctp_queued_to_read *ctl, *nctl;
4047 /* Get the number. */
4048 seq = ntohl(str_add->request_seq);
4049 num_stream = ntohs(str_add->number_of_streams);
4050 /* Now what would be the new total? */
4051 if (asoc->str_reset_seq_in == seq) {
4052 num_stream += stcb->asoc.streamincnt;
4053 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4054 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4055 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4056 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
4057 (num_stream > 0xffff)) {
4058 /* We must reject it they ask for to many */
4060 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4062 /* Ok, we can do that :-) */
4063 struct sctp_stream_in *oldstrm;
4065 /* save off the old */
4066 oldstrm = stcb->asoc.strmin;
4067 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
4068 (num_stream * sizeof(struct sctp_stream_in)),
4070 if (stcb->asoc.strmin == NULL) {
4071 stcb->asoc.strmin = oldstrm;
4074 /* copy off the old data */
4075 for (i = 0; i < stcb->asoc.streamincnt; i++) {
4076 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4077 stcb->asoc.strmin[i].stream_no = i;
4078 stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered;
4079 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
4080 /* now anything on those queues? */
4081 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) {
4082 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next);
4083 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next);
4086 /* Init the new streams */
4087 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
4088 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4089 stcb->asoc.strmin[i].stream_no = i;
4090 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
4091 stcb->asoc.strmin[i].delivery_started = 0;
4093 SCTP_FREE(oldstrm, SCTP_M_STRMI);
4094 /* update the size */
4095 stcb->asoc.streamincnt = num_stream;
4096 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4097 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4099 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4100 asoc->str_reset_seq_in++;
4101 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4103 * one seq back, just echo back last action since my
4104 * response was lost.
4106 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4107 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4109 * two seq back, just echo back last action since my
4110 * response was lost.
4112 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4114 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4120 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4121 struct sctp_stream_reset_add_strm *str_add)
4124 * Peer is requesting to add more streams.
4125 * If its within our max-streams we will
4128 uint16_t num_stream;
4130 struct sctp_association *asoc = &stcb->asoc;
4132 /* Get the number. */
4133 seq = ntohl(str_add->request_seq);
4134 num_stream = ntohs(str_add->number_of_streams);
4135 /* Now what would be the new total? */
4136 if (asoc->str_reset_seq_in == seq) {
4137 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4138 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4139 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4140 } else if (stcb->asoc.stream_reset_outstanding) {
4141 /* We must reject it we have something pending */
4142 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4144 /* Ok, we can do that :-) */
4146 mychk = stcb->asoc.streamoutcnt;
4147 mychk += num_stream;
4148 if (mychk < 0x10000) {
4149 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4150 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, 1, num_stream, 0, 1)) {
4151 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4154 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4157 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4158 asoc->str_reset_seq_in++;
4159 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4161 * one seq back, just echo back last action since my
4162 * response was lost.
4164 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4165 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4167 * two seq back, just echo back last action since my
4168 * response was lost.
4170 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4172 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4176 #if !defined(__Panda__)
4178 __attribute__ ((noinline))
4182 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4183 struct sctp_chunkhdr *ch_req)
4185 int chk_length, param_len, ptype;
4186 struct sctp_paramhdr pstore;
4187 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4191 struct sctp_tmit_chunk *chk;
4192 struct sctp_chunkhdr *ch;
4193 struct sctp_paramhdr *ph;
4197 /* now it may be a reset or a reset-response */
4198 chk_length = ntohs(ch_req->chunk_length);
4200 /* setup for adding the response */
4201 sctp_alloc_a_chunk(stcb, chk);
4205 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4206 chk->rec.chunk_id.can_take_data = 0;
4207 chk->asoc = &stcb->asoc;
4208 chk->no_fr_allowed = 0;
4209 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4210 chk->book_size_scale = 0;
4211 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4212 if (chk->data == NULL) {
4215 sctp_m_freem(chk->data);
4218 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4221 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4223 /* setup chunk parameters */
4224 chk->sent = SCTP_DATAGRAM_UNSENT;
4228 ch = mtod(chk->data, struct sctp_chunkhdr *);
4229 ch->chunk_type = SCTP_STREAM_RESET;
4230 ch->chunk_flags = 0;
4231 ch->chunk_length = htons(chk->send_size);
4232 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4233 offset += sizeof(struct sctp_chunkhdr);
4234 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
4235 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
4238 param_len = ntohs(ph->param_length);
4239 if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
4243 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
4244 (uint8_t *)&cstore);
4245 ptype = ntohs(ph->param_type);
4247 if (param_len > (int)sizeof(cstore)) {
4252 if (num_param > SCTP_MAX_RESET_PARAMS) {
4253 /* hit the max of parameters already sorry.. */
4256 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4257 struct sctp_stream_reset_out_request *req_out;
4258 req_out = (struct sctp_stream_reset_out_request *)ph;
4260 if (stcb->asoc.stream_reset_outstanding) {
4261 seq = ntohl(req_out->response_seq);
4262 if (seq == stcb->asoc.str_reset_seq_out) {
4264 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4267 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4268 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4269 struct sctp_stream_reset_add_strm *str_add;
4270 str_add = (struct sctp_stream_reset_add_strm *)ph;
4272 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4273 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4274 struct sctp_stream_reset_add_strm *str_add;
4275 str_add = (struct sctp_stream_reset_add_strm *)ph;
4277 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4278 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4279 struct sctp_stream_reset_in_request *req_in;
4281 req_in = (struct sctp_stream_reset_in_request *)ph;
4282 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4283 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4284 struct sctp_stream_reset_tsn_request *req_tsn;
4286 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4287 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4289 goto strres_nochunk;
4293 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4294 struct sctp_stream_reset_response *resp;
4296 resp = (struct sctp_stream_reset_response *)ph;
4297 seq = ntohl(resp->response_seq);
4298 result = ntohl(resp->result);
4299 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4301 goto strres_nochunk;
4306 offset += SCTP_SIZE32(param_len);
4307 chk_length -= SCTP_SIZE32(param_len);
4310 /* we have no response free the stuff */
4311 goto strres_nochunk;
4313 /* ok we have a chunk to link in */
4314 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4317 stcb->asoc.ctrl_queue_cnt++;
4322 * Handle a router or endpoints report of a packet loss, there are two ways
4323 * to handle this, either we get the whole packet and must disect it
4324 * ourselves (possibly with truncation and or corruption) or it is a summary
4325 * from a middle box that did the disectting for us.
4328 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4329 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4331 uint32_t bottle_bw, on_queue;
4335 struct sctp_chunk_desc desc;
4336 struct sctp_chunkhdr *ch;
4338 chlen = ntohs(cp->ch.chunk_length);
4339 chlen -= sizeof(struct sctp_pktdrop_chunk);
4340 /* XXX possible chlen underflow */
4343 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4344 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4346 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4347 chlen -= sizeof(struct sctphdr);
4348 /* XXX possible chlen underflow */
4349 memset(&desc, 0, sizeof(desc));
4351 trunc_len = (uint16_t) ntohs(cp->trunc_len);
4352 if (trunc_len > limit) {
4356 /* now the chunks themselves */
4357 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4358 desc.chunk_type = ch->chunk_type;
4359 /* get amount we need to move */
4360 at = ntohs(ch->chunk_length);
4361 if (at < sizeof(struct sctp_chunkhdr)) {
4362 /* corrupt chunk, maybe at the end? */
4363 SCTP_STAT_INCR(sctps_pdrpcrupt);
4366 if (trunc_len == 0) {
4367 /* we are supposed to have all of it */
4369 /* corrupt skip it */
4370 SCTP_STAT_INCR(sctps_pdrpcrupt);
4374 /* is there enough of it left ? */
4375 if (desc.chunk_type == SCTP_DATA) {
4376 if (chlen < (sizeof(struct sctp_data_chunk) +
4377 sizeof(desc.data_bytes))) {
4381 if (chlen < sizeof(struct sctp_chunkhdr)) {
4386 if (desc.chunk_type == SCTP_DATA) {
4387 /* can we get out the tsn? */
4388 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4389 SCTP_STAT_INCR(sctps_pdrpmbda);
4391 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4393 struct sctp_data_chunk *dcp;
4397 dcp = (struct sctp_data_chunk *)ch;
4398 ddp = (uint8_t *) (dcp + 1);
4399 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4400 desc.data_bytes[iii] = ddp[iii];
4402 desc.tsn_ifany = dcp->dp.tsn;
4404 /* nope we are done. */
4405 SCTP_STAT_INCR(sctps_pdrpnedat);
4409 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4410 SCTP_STAT_INCR(sctps_pdrpmbct);
4413 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4414 SCTP_STAT_INCR(sctps_pdrppdbrk);
4417 if (SCTP_SIZE32(at) > chlen) {
4420 chlen -= SCTP_SIZE32(at);
4421 if (chlen < sizeof(struct sctp_chunkhdr)) {
4422 /* done, none left */
4425 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4427 /* Now update any rwnd --- possibly */
4428 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4429 /* From a peer, we get a rwnd report */
4432 SCTP_STAT_INCR(sctps_pdrpfehos);
4434 bottle_bw = ntohl(cp->bottle_bw);
4435 on_queue = ntohl(cp->current_onq);
4436 if (bottle_bw && on_queue) {
4437 /* a rwnd report is in here */
4438 if (bottle_bw > on_queue)
4439 a_rwnd = bottle_bw - on_queue;
4444 stcb->asoc.peers_rwnd = 0;
4446 if (a_rwnd > stcb->asoc.total_flight) {
4447 stcb->asoc.peers_rwnd =
4448 a_rwnd - stcb->asoc.total_flight;
4450 stcb->asoc.peers_rwnd = 0;
4452 if (stcb->asoc.peers_rwnd <
4453 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4454 /* SWS sender side engages */
4455 stcb->asoc.peers_rwnd = 0;
4460 SCTP_STAT_INCR(sctps_pdrpfmbox);
4463 /* now middle boxes in sat networks get a cwnd bump */
4464 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4465 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4466 (stcb->asoc.sat_network)) {
4468 * This is debateable but for sat networks it makes sense
4469 * Note if a T3 timer has went off, we will prohibit any
4470 * changes to cwnd until we exit the t3 loss recovery.
4472 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4473 net, cp, &bottle_bw, &on_queue);
4478 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4479 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4480 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4481 * length of the complete packet outputs: - length: modified to remaining
4482 * length after control processing - netp: modified to new sctp_nets after
4483 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4484 * bad packet,...) otherwise return the tcb for this packet
4486 #if !defined(__Panda__)
4488 __attribute__ ((noinline))
4491 static struct sctp_tcb *
4492 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4493 struct sockaddr *src, struct sockaddr *dst,
4494 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4495 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4496 #if defined(__FreeBSD__)
4497 uint8_t use_mflowid, uint32_t mflowid,
4499 uint32_t vrf_id, uint16_t port)
4501 struct sctp_association *asoc;
4502 struct mbuf *op_err;
4503 char msg[SCTP_DIAG_INFO_LEN];
4505 int num_chunks = 0; /* number of control chunks processed */
4506 uint32_t chk_length;
4508 int abort_no_unlock = 0;
4511 * How big should this be, and should it be alloc'd? Lets try the
4512 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4513 * until we get into jumbo grams and such..
4515 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4516 struct sctp_tcb *locked_tcb = stcb;
4518 uint32_t auth_offset = 0, auth_len = 0;
4519 int auth_skipped = 0;
4521 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4525 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4526 iphlen, *offset, length, (void *)stcb);
4528 /* validate chunk header length... */
4529 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4530 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4531 ntohs(ch->chunk_length));
4533 SCTP_TCB_UNLOCK(locked_tcb);
4538 * validate the verification tag
4540 vtag_in = ntohl(sh->v_tag);
4543 SCTP_TCB_LOCK_ASSERT(locked_tcb);
4545 if (ch->chunk_type == SCTP_INITIATION) {
4546 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4547 ntohs(ch->chunk_length), vtag_in);
4549 /* protocol error- silently discard... */
4550 SCTP_STAT_INCR(sctps_badvtag);
4552 SCTP_TCB_UNLOCK(locked_tcb);
4556 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4558 * If there is no stcb, skip the AUTH chunk and process
4559 * later after a stcb is found (to validate the lookup was
4562 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4564 (inp->auth_supported == 1)) {
4565 /* save this chunk for later processing */
4567 auth_offset = *offset;
4568 auth_len = ntohs(ch->chunk_length);
4570 /* (temporarily) move past this chunk */
4571 *offset += SCTP_SIZE32(auth_len);
4572 if (*offset >= length) {
4573 /* no more data left in the mbuf chain */
4576 SCTP_TCB_UNLOCK(locked_tcb);
4580 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4581 sizeof(struct sctp_chunkhdr), chunk_buf);
4587 SCTP_TCB_UNLOCK(locked_tcb);
4591 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4592 goto process_control_chunks;
4595 * first check if it's an ASCONF with an unknown src addr we
4596 * need to look inside to find the association
4598 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4599 struct sctp_chunkhdr *asconf_ch = ch;
4600 uint32_t asconf_offset = 0, asconf_len = 0;
4602 /* inp's refcount may be reduced */
4603 SCTP_INP_INCR_REF(inp);
4605 asconf_offset = *offset;
4607 asconf_len = ntohs(asconf_ch->chunk_length);
4608 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4610 stcb = sctp_findassociation_ep_asconf(m,
4613 sh, &inp, netp, vrf_id);
4616 asconf_offset += SCTP_SIZE32(asconf_len);
4617 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4618 sizeof(struct sctp_chunkhdr), chunk_buf);
4619 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4622 * reduce inp's refcount if not reduced in
4623 * sctp_findassociation_ep_asconf().
4625 SCTP_INP_DECR_REF(inp);
4630 /* now go back and verify any auth chunk to be sure */
4631 if (auth_skipped && (stcb != NULL)) {
4632 struct sctp_auth_chunk *auth;
4634 auth = (struct sctp_auth_chunk *)
4635 sctp_m_getptr(m, auth_offset,
4636 auth_len, chunk_buf);
4639 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4641 /* auth HMAC failed so dump it */
4644 SCTP_TCB_UNLOCK(locked_tcb);
4648 /* remaining chunks are HMAC checked */
4649 stcb->asoc.authenticated = 1;
4654 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s\n", __FILE__, __LINE__, __FUNCTION__);
4655 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4657 /* no association, so it's out of the blue... */
4658 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4659 #if defined(__FreeBSD__)
4660 use_mflowid, mflowid,
4665 SCTP_TCB_UNLOCK(locked_tcb);
4670 /* ABORT and SHUTDOWN can use either v_tag... */
4671 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4672 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4673 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4674 /* Take the T-bit always into account. */
4675 if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
4676 (vtag_in == asoc->my_vtag)) ||
4677 (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4678 (vtag_in == asoc->peer_vtag))) {
4681 /* drop this packet... */
4682 SCTP_STAT_INCR(sctps_badvtag);
4684 SCTP_TCB_UNLOCK(locked_tcb);
4688 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4689 if (vtag_in != asoc->my_vtag) {
4691 * this could be a stale SHUTDOWN-ACK or the
4692 * peer never got the SHUTDOWN-COMPLETE and
4693 * is still hung; we have started a new asoc
4694 * but it won't complete until the shutdown
4698 SCTP_TCB_UNLOCK(locked_tcb);
4700 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s\n", __FILE__, __LINE__, __FUNCTION__);
4701 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4703 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4705 #if defined(__FreeBSD__)
4706 use_mflowid, mflowid,
4712 /* for all other chunks, vtag must match */
4713 if (vtag_in != asoc->my_vtag) {
4714 /* invalid vtag... */
4715 SCTPDBG(SCTP_DEBUG_INPUT3,
4716 "invalid vtag: %xh, expect %xh\n",
4717 vtag_in, asoc->my_vtag);
4718 SCTP_STAT_INCR(sctps_badvtag);
4720 SCTP_TCB_UNLOCK(locked_tcb);
4726 } /* end if !SCTP_COOKIE_ECHO */
4728 * process all control chunks...
4730 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4731 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4732 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4733 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4734 /* implied cookie-ack.. we must have lost the ack */
4735 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4736 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4737 stcb->asoc.overall_error_count,
4739 SCTP_FROM_SCTP_INPUT,
4742 stcb->asoc.overall_error_count = 0;
4743 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4747 process_control_chunks:
4748 while (IS_SCTP_CONTROL(ch)) {
4749 /* validate chunk length */
4750 chk_length = ntohs(ch->chunk_length);
4751 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4752 ch->chunk_type, chk_length);
4753 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4754 if (chk_length < sizeof(*ch) ||
4755 (*offset + (int)chk_length) > length) {
4758 SCTP_TCB_UNLOCK(locked_tcb);
4762 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4764 * INIT-ACK only gets the init ack "header" portion only
4765 * because we don't have to process the peer's COOKIE. All
4766 * others get a complete chunk.
4768 if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4769 (ch->chunk_type == SCTP_INITIATION)) {
4770 /* get an init-ack chunk */
4771 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4772 sizeof(struct sctp_init_ack_chunk), chunk_buf);
4776 SCTP_TCB_UNLOCK(locked_tcb);
4781 /* For cookies and all other chunks. */
4782 if (chk_length > sizeof(chunk_buf)) {
4784 * use just the size of the chunk buffer
4785 * so the front part of our chunks fit in
4786 * contiguous space up to the chunk buffer
4788 * For chunks that need to get more than that
4789 * they must use the sctp_m_getptr() function
4790 * or other means (e.g. know how to parse mbuf
4791 * chains). Cookies do this already.
4793 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4794 (sizeof(chunk_buf) - 4),
4799 SCTP_TCB_UNLOCK(locked_tcb);
4804 /* We can fit it all */
4805 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4806 chk_length, chunk_buf);
4808 SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4811 SCTP_TCB_UNLOCK(locked_tcb);
4818 /* Save off the last place we got a control from */
4820 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4822 * allow last_control to be NULL if
4823 * ASCONF... ASCONF processing will find the
4826 if ((netp != NULL) && (*netp != NULL))
4827 stcb->asoc.last_control_chunk_from = *netp;
4830 #ifdef SCTP_AUDITING_ENABLED
4831 sctp_audit_log(0xB0, ch->chunk_type);
4834 /* check to see if this chunk required auth, but isn't */
4835 if ((stcb != NULL) &&
4836 (stcb->asoc.auth_supported == 1) &&
4837 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4838 !stcb->asoc.authenticated) {
4839 /* "silently" ignore */
4840 SCTP_STAT_INCR(sctps_recvauthmissing);
4843 switch (ch->chunk_type) {
4844 case SCTP_INITIATION:
4845 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4846 /* The INIT chunk must be the only chunk. */
4847 if ((num_chunks > 1) ||
4848 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4849 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4850 "INIT not the only chunk");
4851 sctp_abort_association(inp, stcb, m, iphlen,
4852 src, dst, sh, op_err,
4853 #if defined(__FreeBSD__)
4854 use_mflowid, mflowid,
4860 /* Honor our resource limit. */
4861 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4862 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4863 sctp_abort_association(inp, stcb, m, iphlen,
4864 src, dst, sh, op_err,
4865 #if defined(__FreeBSD__)
4866 use_mflowid, mflowid,
4872 sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4873 (struct sctp_init_chunk *)ch, inp,
4874 stcb, &abort_no_unlock,
4875 #if defined(__FreeBSD__)
4876 use_mflowid, mflowid,
4880 if ((!abort_no_unlock) && (locked_tcb)) {
4881 SCTP_TCB_UNLOCK(locked_tcb);
4885 case SCTP_PAD_CHUNK:
4887 case SCTP_INITIATION_ACK:
4888 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4889 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4890 /* We are not interested anymore */
4891 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4894 if (locked_tcb != stcb) {
4896 SCTP_TCB_UNLOCK(locked_tcb);
4900 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4901 so = SCTP_INP_SO(inp);
4902 atomic_add_int(&stcb->asoc.refcnt, 1);
4903 SCTP_TCB_UNLOCK(stcb);
4904 SCTP_SOCKET_LOCK(so, 1);
4905 SCTP_TCB_LOCK(stcb);
4906 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4908 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_27);
4909 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4910 SCTP_SOCKET_UNLOCK(so, 1);
4916 /* The INIT-ACK chunk must be the only chunk. */
4917 if ((num_chunks > 1) ||
4918 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4921 SCTP_TCB_UNLOCK(locked_tcb);
4925 if ((netp) && (*netp)) {
4926 ret = sctp_handle_init_ack(m, iphlen, *offset,
4928 (struct sctp_init_ack_chunk *)ch,
4931 #if defined(__FreeBSD__)
4932 use_mflowid, mflowid,
4939 if (abort_no_unlock) {
4943 * Special case, I must call the output routine to
4944 * get the cookie echoed
4946 if ((stcb != NULL) && (ret == 0)) {
4947 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4950 SCTP_TCB_UNLOCK(locked_tcb);
4954 case SCTP_SELECTIVE_ACK:
4956 struct sctp_sack_chunk *sack;
4958 uint32_t a_rwnd, cum_ack;
4959 uint16_t num_seg, num_dup;
4961 int offset_seg, offset_dup;
4963 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4964 SCTP_STAT_INCR(sctps_recvsacks);
4966 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4969 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4970 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4973 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4975 * If we have sent a shutdown-ack, we will pay no
4976 * attention to a sack sent in to us since
4977 * we don't care anymore.
4981 sack = (struct sctp_sack_chunk *)ch;
4982 flags = ch->chunk_flags;
4983 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4984 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4985 num_dup = ntohs(sack->sack.num_dup_tsns);
4986 a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4987 if (sizeof(struct sctp_sack_chunk) +
4988 num_seg * sizeof(struct sctp_gap_ack_block) +
4989 num_dup * sizeof(uint32_t) != chk_length) {
4990 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4993 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4994 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4995 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4996 cum_ack, num_seg, a_rwnd);
4997 stcb->asoc.seen_a_sack_this_pkt = 1;
4998 if ((stcb->asoc.pr_sctp_cnt == 0) &&
5000 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5001 (stcb->asoc.saw_sack_with_frags == 0) &&
5002 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5003 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
5005 /* We have a SIMPLE sack having no prior segments and
5006 * data on sent queue to be acked.. Use the faster
5007 * path sack processing. We also allow window update
5008 * sacks with no missing segments to go this way too.
5010 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
5013 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5014 num_seg, 0, num_dup, &abort_now, flags,
5015 cum_ack, a_rwnd, ecne_seen);
5018 /* ABORT signal from sack processing */
5022 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5023 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5024 (stcb->asoc.stream_queue_cnt == 0)) {
5025 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5029 /* EY - nr_sack: If the received chunk is an nr_sack chunk */
5030 case SCTP_NR_SELECTIVE_ACK:
5032 struct sctp_nr_sack_chunk *nr_sack;
5034 uint32_t a_rwnd, cum_ack;
5035 uint16_t num_seg, num_nr_seg, num_dup;
5037 int offset_seg, offset_dup;
5039 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
5040 SCTP_STAT_INCR(sctps_recvsacks);
5042 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
5045 if (stcb->asoc.nrsack_supported == 0) {
5048 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
5049 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
5052 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
5054 * If we have sent a shutdown-ack, we will pay no
5055 * attention to a sack sent in to us since
5056 * we don't care anymore.
5060 nr_sack = (struct sctp_nr_sack_chunk *)ch;
5061 flags = ch->chunk_flags;
5062 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
5063 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
5064 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
5065 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
5066 a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
5067 if (sizeof(struct sctp_nr_sack_chunk) +
5068 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
5069 num_dup * sizeof(uint32_t) != chk_length) {
5070 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
5073 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
5074 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
5075 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
5076 cum_ack, num_seg, a_rwnd);
5077 stcb->asoc.seen_a_sack_this_pkt = 1;
5078 if ((stcb->asoc.pr_sctp_cnt == 0) &&
5079 (num_seg == 0) && (num_nr_seg == 0) &&
5080 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5081 (stcb->asoc.saw_sack_with_frags == 0) &&
5082 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5083 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
5085 * We have a SIMPLE sack having no
5086 * prior segments and data on sent
5087 * queue to be acked. Use the
5088 * faster path sack processing. We
5089 * also allow window update sacks
5090 * with no missing segments to go
5093 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
5094 &abort_now, ecne_seen);
5097 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5098 num_seg, num_nr_seg, num_dup, &abort_now, flags,
5099 cum_ack, a_rwnd, ecne_seen);
5102 /* ABORT signal from sack processing */
5106 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5107 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5108 (stcb->asoc.stream_queue_cnt == 0)) {
5109 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5114 case SCTP_HEARTBEAT_REQUEST:
5115 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5116 if ((stcb) && netp && *netp) {
5117 SCTP_STAT_INCR(sctps_recvheartbeat);
5118 sctp_send_heartbeat_ack(stcb, m, *offset,
5121 /* He's alive so give him credit */
5122 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5123 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5124 stcb->asoc.overall_error_count,
5126 SCTP_FROM_SCTP_INPUT,
5129 stcb->asoc.overall_error_count = 0;
5132 case SCTP_HEARTBEAT_ACK:
5133 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
5134 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5138 SCTP_TCB_UNLOCK(locked_tcb);
5142 /* He's alive so give him credit */
5143 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5144 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5145 stcb->asoc.overall_error_count,
5147 SCTP_FROM_SCTP_INPUT,
5150 stcb->asoc.overall_error_count = 0;
5151 SCTP_STAT_INCR(sctps_recvheartbeatack);
5153 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5156 case SCTP_ABORT_ASSOCIATION:
5157 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5159 if ((stcb) && netp && *netp)
5160 sctp_handle_abort((struct sctp_abort_chunk *)ch,
5166 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5168 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5171 SCTP_TCB_UNLOCK(locked_tcb);
5175 if (netp && *netp) {
5178 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5179 stcb, *netp, &abort_flag);
5186 case SCTP_SHUTDOWN_ACK:
5187 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", (void *)stcb);
5188 if ((stcb) && (netp) && (*netp))
5189 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5194 case SCTP_OPERATION_ERROR:
5195 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5196 if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5201 case SCTP_COOKIE_ECHO:
5202 SCTPDBG(SCTP_DEBUG_INPUT3,
5203 "SCTP_COOKIE-ECHO, stcb %p\n", (void *)stcb);
5204 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5207 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5208 /* We are not interested anymore */
5211 SCTP_TCB_UNLOCK(stcb);
5218 * First are we accepting? We do this again here
5219 * since it is possible that a previous endpoint WAS
5220 * listening responded to a INIT-ACK and then
5221 * closed. We opened and bound.. and are now no
5225 if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
5226 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5227 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5228 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
5229 sctp_abort_association(inp, stcb, m, iphlen,
5230 src, dst, sh, op_err,
5231 #if defined(__FreeBSD__)
5232 use_mflowid, mflowid,
5239 struct mbuf *ret_buf;
5240 struct sctp_inpcb *linp;
5248 SCTP_ASOC_CREATE_LOCK(linp);
5249 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5250 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5251 SCTP_ASOC_CREATE_UNLOCK(linp);
5258 sctp_handle_cookie_echo(m, iphlen,
5262 (struct sctp_cookie_echo_chunk *)ch,
5268 #if defined(__FreeBSD__)
5278 SCTP_ASOC_CREATE_UNLOCK(linp);
5280 if (ret_buf == NULL) {
5282 SCTP_TCB_UNLOCK(locked_tcb);
5284 SCTPDBG(SCTP_DEBUG_INPUT3,
5285 "GAK, null buffer\n");
5289 /* if AUTH skipped, see if it verified... */
5294 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5296 * Restart the timer if we have
5299 struct sctp_tmit_chunk *chk;
5301 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5302 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5306 case SCTP_COOKIE_ACK:
5307 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", (void *)stcb);
5308 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5310 SCTP_TCB_UNLOCK(locked_tcb);
5314 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5315 /* We are not interested anymore */
5316 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5319 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5320 so = SCTP_INP_SO(inp);
5321 atomic_add_int(&stcb->asoc.refcnt, 1);
5322 SCTP_TCB_UNLOCK(stcb);
5323 SCTP_SOCKET_LOCK(so, 1);
5324 SCTP_TCB_LOCK(stcb);
5325 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5327 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_27);
5328 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5329 SCTP_SOCKET_UNLOCK(so, 1);
5335 /* He's alive so give him credit */
5336 if ((stcb) && netp && *netp) {
5337 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5338 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5339 stcb->asoc.overall_error_count,
5341 SCTP_FROM_SCTP_INPUT,
5344 stcb->asoc.overall_error_count = 0;
5345 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch,stcb, *netp);
5349 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5350 /* He's alive so give him credit */
5351 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5354 SCTP_TCB_UNLOCK(locked_tcb);
5360 if (stcb->asoc.ecn_supported == 0) {
5363 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5364 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5365 stcb->asoc.overall_error_count,
5367 SCTP_FROM_SCTP_INPUT,
5370 stcb->asoc.overall_error_count = 0;
5371 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5377 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5378 /* He's alive so give him credit */
5379 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5382 SCTP_TCB_UNLOCK(locked_tcb);
5388 if (stcb->asoc.ecn_supported == 0) {
5391 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5392 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5393 stcb->asoc.overall_error_count,
5395 SCTP_FROM_SCTP_INPUT,
5398 stcb->asoc.overall_error_count = 0;
5399 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5402 case SCTP_SHUTDOWN_COMPLETE:
5403 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", (void *)stcb);
5404 /* must be first and only chunk */
5405 if ((num_chunks > 1) ||
5406 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5409 SCTP_TCB_UNLOCK(locked_tcb);
5413 if ((stcb) && netp && *netp) {
5414 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5421 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5422 /* He's alive so give him credit */
5424 if (stcb->asoc.asconf_supported == 0) {
5427 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5428 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5429 stcb->asoc.overall_error_count,
5431 SCTP_FROM_SCTP_INPUT,
5434 stcb->asoc.overall_error_count = 0;
5435 sctp_handle_asconf(m, *offset, src,
5436 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5440 case SCTP_ASCONF_ACK:
5441 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5442 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5445 SCTP_TCB_UNLOCK(locked_tcb);
5450 if ((stcb) && netp && *netp) {
5451 if (stcb->asoc.asconf_supported == 0) {
5454 /* He's alive so give him credit */
5455 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5456 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5457 stcb->asoc.overall_error_count,
5459 SCTP_FROM_SCTP_INPUT,
5462 stcb->asoc.overall_error_count = 0;
5463 sctp_handle_asconf_ack(m, *offset,
5464 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5465 if (abort_no_unlock)
5469 case SCTP_FORWARD_CUM_TSN:
5470 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5471 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5474 SCTP_TCB_UNLOCK(locked_tcb);
5480 /* He's alive so give him credit */
5484 if (stcb->asoc.prsctp_supported == 0) {
5487 stcb->asoc.overall_error_count = 0;
5488 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5489 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5490 stcb->asoc.overall_error_count,
5492 SCTP_FROM_SCTP_INPUT,
5496 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5497 /* We are not interested anymore */
5498 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5499 so = SCTP_INP_SO(inp);
5500 atomic_add_int(&stcb->asoc.refcnt, 1);
5501 SCTP_TCB_UNLOCK(stcb);
5502 SCTP_SOCKET_LOCK(so, 1);
5503 SCTP_TCB_LOCK(stcb);
5504 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5506 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT+SCTP_LOC_29);
5507 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5508 SCTP_SOCKET_UNLOCK(so, 1);
5513 sctp_handle_forward_tsn(stcb,
5514 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5519 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5520 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5521 stcb->asoc.overall_error_count,
5523 SCTP_FROM_SCTP_INPUT,
5526 stcb->asoc.overall_error_count = 0;
5531 case SCTP_STREAM_RESET:
5532 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5533 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5536 SCTP_TCB_UNLOCK(locked_tcb);
5541 if (stcb->asoc.reconfig_supported == 0) {
5544 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5545 /* stop processing */
5550 case SCTP_PACKET_DROPPED:
5551 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5552 /* re-get it all please */
5553 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5556 SCTP_TCB_UNLOCK(locked_tcb);
5563 if (ch && (stcb) && netp && (*netp)) {
5564 if (stcb->asoc.pktdrop_supported == 0) {
5567 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5569 min(chk_length, (sizeof(chunk_buf) - 4)));
5574 case SCTP_AUTHENTICATION:
5575 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5577 /* save the first AUTH for later processing */
5578 if (auth_skipped == 0) {
5579 auth_offset = *offset;
5580 auth_len = chk_length;
5583 /* skip this chunk (temporarily) */
5586 if (stcb->asoc.auth_supported == 0) {
5589 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5590 (chk_length > (sizeof(struct sctp_auth_chunk) +
5591 SCTP_AUTH_DIGEST_LEN_MAX))) {
5594 SCTP_TCB_UNLOCK(locked_tcb);
5599 if (got_auth == 1) {
5600 /* skip this chunk... it's already auth'd */
5604 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5606 /* auth HMAC failed so dump the packet */
5610 /* remaining chunks are HMAC checked */
5611 stcb->asoc.authenticated = 1;
5617 /* it's an unknown chunk! */
5618 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5620 struct sctp_paramhdr *phd;
5622 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
5623 0, M_NOWAIT, 1, MT_DATA);
5625 phd = mtod(mm, struct sctp_paramhdr *);
5627 * We cheat and use param type since
5628 * we did not bother to define a
5629 * error cause struct. They are the
5630 * same basic format with different
5633 phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5634 phd->param_length = htons(chk_length + sizeof(*phd));
5635 SCTP_BUF_LEN(mm) = sizeof(*phd);
5636 SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, chk_length, M_NOWAIT);
5637 if (SCTP_BUF_NEXT(mm)) {
5638 if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(mm), SCTP_SIZE32(chk_length) - chk_length, NULL) == NULL) {
5641 #ifdef SCTP_MBUF_LOGGING
5642 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5645 for (mat = SCTP_BUF_NEXT(mm); mat; mat = SCTP_BUF_NEXT(mat)) {
5646 if (SCTP_BUF_IS_EXTENDED(mat)) {
5647 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5652 sctp_queue_op_err(stcb, mm);
5659 if ((ch->chunk_type & 0x80) == 0) {
5660 /* discard this packet */
5663 } /* else skip this bad chunk and continue... */
5665 } /* switch (ch->chunk_type) */
5669 /* get the next chunk */
5670 *offset += SCTP_SIZE32(chk_length);
5671 if (*offset >= length) {
5672 /* no more data left in the mbuf chain */
5675 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5676 sizeof(struct sctp_chunkhdr), chunk_buf);
5679 SCTP_TCB_UNLOCK(locked_tcb);
5686 if (asconf_cnt > 0 && stcb != NULL) {
5687 sctp_send_asconf_ack(stcb);
5695 __attribute__((noinline))
5698 sctp_validate_no_locks(struct sctp_inpcb *inp)
5701 struct sctp_tcb *lstcb;
5703 LIST_FOREACH(lstcb, &inp->sctp_asoc_list, sctp_tcblist) {
5704 if (mtx_owned(&lstcb->tcb_mtx)) {
5705 panic("Own lock on stcb at return from input");
5708 if (mtx_owned(&inp->inp_create_mtx)) {
5709 panic("Own create lock on inp");
5711 if (mtx_owned(&inp->inp_mtx)) {
5712 panic("Own inp lock on inp");
5719 * common input chunk processing (v4 and v6)
5722 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5723 struct sockaddr *src, struct sockaddr *dst,
5724 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5725 #if !defined(SCTP_WITH_NO_CSUM)
5726 uint8_t compute_crc,
5729 #if defined(__FreeBSD__)
5730 uint8_t use_mflowid, uint32_t mflowid,
5732 uint32_t vrf_id, uint16_t port)
5735 int fwd_tsn_seen = 0, data_processed = 0;
5736 struct mbuf *m = *mm, *op_err;
5737 char msg[SCTP_DIAG_INFO_LEN];
5739 int cnt_ctrl_ready = 0;
5740 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5741 struct sctp_tcb *stcb = NULL;
5742 struct sctp_nets *net = NULL;
5744 SCTP_STAT_INCR(sctps_recvdatagrams);
5745 #ifdef SCTP_AUDITING_ENABLED
5746 sctp_audit_log(0xE0, 1);
5747 sctp_auditing(0, inp, stcb, net);
5749 #if !defined(SCTP_WITH_NO_CSUM)
5750 if (compute_crc != 0) {
5751 uint32_t check, calc_check;
5753 check = sh->checksum;
5755 calc_check = sctp_calculate_cksum(m, iphlen);
5756 sh->checksum = check;
5757 if (calc_check != check) {
5758 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5759 calc_check, check, (void *)m, length, iphlen);
5760 stcb = sctp_findassociation_addr(m, offset, src, dst,
5761 sh, ch, &inp, &net, vrf_id);
5762 #if defined(INET) || defined(INET6)
5763 if ((net != NULL) && (port != 0)) {
5764 if (net->port == 0) {
5765 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5770 #if defined(__FreeBSD__)
5771 if ((net != NULL) && (use_mflowid != 0)) {
5772 net->flowid = mflowid;
5778 if ((inp != NULL) && (stcb != NULL)) {
5779 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5780 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5781 } else if ((inp != NULL) && (stcb == NULL)) {
5784 SCTP_STAT_INCR(sctps_badsum);
5785 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5790 /* Destination port of 0 is illegal, based on RFC4960. */
5791 if (sh->dest_port == 0) {
5792 SCTP_STAT_INCR(sctps_hdrops);
5795 stcb = sctp_findassociation_addr(m, offset, src, dst,
5796 sh, ch, &inp, &net, vrf_id);
5797 #if defined(INET) || defined(INET6)
5798 if ((net != NULL) && (port != 0)) {
5799 if (net->port == 0) {
5800 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5805 #if defined(__FreeBSD__)
5806 if ((net != NULL) && (use_mflowid != 0)) {
5807 net->flowid = mflowid;
5814 SCTP_STAT_INCR(sctps_noport);
5815 #if defined(__FreeBSD__) && (((__FreeBSD_version < 900000) && (__FreeBSD_version >= 804000)) || (__FreeBSD_version > 900000))
5816 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5820 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5821 sctp_send_shutdown_complete2(src, dst, sh,
5822 #if defined(__FreeBSD__)
5823 use_mflowid, mflowid,
5828 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5831 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5832 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5833 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5834 (ch->chunk_type != SCTP_INIT))) {
5835 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5837 sctp_send_abort(m, iphlen, src, dst,
5839 #if defined(__FreeBSD__)
5840 use_mflowid, mflowid,
5846 } else if (stcb == NULL) {
5851 * I very much doubt any of the IPSEC stuff will work but I have no
5852 * idea, so I will leave it in place.
5855 switch (dst->sa_family) {
5858 if (ipsec4_in_reject(m, &inp->ip_inp.inp)) {
5859 #if defined(__FreeBSD__) && (__FreeBSD_version > 1000036)
5860 IPSECSTAT_INC(ips_in_polvio);
5862 MODULE_GLOBAL(ipsec4stat).in_polvio++;
5864 SCTP_STAT_INCR(sctps_hdrops);
5871 if (ipsec6_in_reject(m, &inp->ip_inp.inp)) {
5872 #if defined(__FreeBSD__) && (__FreeBSD_version > 1000036)
5873 IPSEC6STAT_INC(ips_in_polvio);
5875 MODULE_GLOBAL(ipsec6stat).in_polvio++;
5877 SCTP_STAT_INCR(sctps_hdrops);
5887 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5888 (void *)m, iphlen, offset, length, (void *)stcb);
5890 /* always clear this before beginning a packet */
5891 stcb->asoc.authenticated = 0;
5892 stcb->asoc.seen_a_sack_this_pkt = 0;
5893 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5894 (void *)stcb, stcb->asoc.state);
5896 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5897 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5899 * If we hit here, we had a ref count
5900 * up when the assoc was aborted and the
5901 * timer is clearing out the assoc, we should
5902 * NOT respond to any packet.. its OOTB.
5904 SCTP_TCB_UNLOCK(stcb);
5906 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s\n", __FILE__, __LINE__, __FUNCTION__);
5907 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5909 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5910 #if defined(__FreeBSD__)
5911 use_mflowid, mflowid,
5918 if (IS_SCTP_CONTROL(ch)) {
5919 /* process the control portion of the SCTP packet */
5920 /* sa_ignore NO_NULL_CHK */
5921 stcb = sctp_process_control(m, iphlen, &offset, length,
5923 inp, stcb, &net, &fwd_tsn_seen,
5924 #if defined(__FreeBSD__)
5925 use_mflowid, mflowid,
5929 /* This covers us if the cookie-echo was there
5930 * and it changes our INP.
5932 inp = stcb->sctp_ep;
5933 #if defined(INET) || defined(INET6)
5934 if ((net) && (port)) {
5935 if (net->port == 0) {
5936 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
5944 * no control chunks, so pre-process DATA chunks (these
5945 * checks are taken care of by control processing)
5949 * if DATA only packet, and auth is required, then punt...
5950 * can't have authenticated without any AUTH (control)
5953 if ((stcb != NULL) &&
5954 (stcb->asoc.auth_supported == 1) &&
5955 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5956 /* "silently" ignore */
5957 SCTP_STAT_INCR(sctps_recvauthmissing);
5961 /* out of the blue DATA chunk */
5962 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s\n", __FILE__, __LINE__, __FUNCTION__);
5963 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5965 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5966 #if defined(__FreeBSD__)
5967 use_mflowid, mflowid,
5972 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5973 /* v_tag mismatch! */
5974 SCTP_STAT_INCR(sctps_badvtag);
5981 * no valid TCB for this packet, or we found it's a bad
5982 * packet while processing control, or we're done with this
5983 * packet (done or skip rest of data), so we drop it...
5989 * DATA chunk processing
5991 /* plow through the data chunks while length > offset */
5994 * Rest should be DATA only. Check authentication state if AUTH for
5997 if ((length > offset) &&
5999 (stcb->asoc.auth_supported == 1) &&
6000 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
6001 !stcb->asoc.authenticated) {
6002 /* "silently" ignore */
6003 SCTP_STAT_INCR(sctps_recvauthmissing);
6004 SCTPDBG(SCTP_DEBUG_AUTH1,
6005 "Data chunk requires AUTH, skipped\n");
6008 if (length > offset) {
6012 * First check to make sure our state is correct. We would
6013 * not get here unless we really did have a tag, so we don't
6014 * abort if this happens, just dump the chunk silently.
6016 switch (SCTP_GET_STATE(&stcb->asoc)) {
6017 case SCTP_STATE_COOKIE_ECHOED:
6019 * we consider data with valid tags in this state
6020 * shows us the cookie-ack was lost. Imply it was
6023 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
6024 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
6025 stcb->asoc.overall_error_count,
6027 SCTP_FROM_SCTP_INPUT,
6030 stcb->asoc.overall_error_count = 0;
6031 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
6033 case SCTP_STATE_COOKIE_WAIT:
6035 * We consider OOTB any data sent during asoc setup.
6037 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s\n", __FILE__, __LINE__, __FUNCTION__);
6038 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
6040 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
6041 #if defined(__FreeBSD__)
6042 use_mflowid, mflowid,
6046 /*sa_ignore NOTREACHED*/
6048 case SCTP_STATE_EMPTY: /* should not happen */
6049 case SCTP_STATE_INUSE: /* should not happen */
6050 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
6051 case SCTP_STATE_SHUTDOWN_ACK_SENT:
6054 /*sa_ignore NOTREACHED*/
6056 case SCTP_STATE_OPEN:
6057 case SCTP_STATE_SHUTDOWN_SENT:
6060 /* plow through the data chunks while length > offset */
6061 retval = sctp_process_data(mm, iphlen, &offset, length,
6063 inp, stcb, net, &high_tsn,
6064 #if defined(__FreeBSD__)
6065 use_mflowid, mflowid,
6070 * The association aborted, NO UNLOCK needed since
6071 * the association is destroyed.
6078 * Anything important needs to have been m_copy'ed in
6083 /* take care of ecn */
6084 if ((data_processed == 1) &&
6085 (stcb->asoc.ecn_supported == 1) &&
6086 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
6087 /* Yep, we need to add a ECNE */
6088 sctp_send_ecn_echo(stcb, net, high_tsn);
6091 if ((data_processed == 0) && (fwd_tsn_seen)) {
6093 uint32_t highest_tsn;
6095 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
6096 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
6098 highest_tsn = stcb->asoc.highest_tsn_inside_map;
6100 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
6101 stcb->asoc.send_sack = 1;
6102 sctp_sack_check(stcb, was_a_gap);
6103 } else if (fwd_tsn_seen) {
6104 stcb->asoc.send_sack = 1;
6106 /* trigger send of any chunks in queue... */
6108 #ifdef SCTP_AUDITING_ENABLED
6109 sctp_audit_log(0xE0, 2);
6110 sctp_auditing(1, inp, stcb, net);
6112 SCTPDBG(SCTP_DEBUG_INPUT1,
6113 "Check for chunk output prw:%d tqe:%d tf=%d\n",
6114 stcb->asoc.peers_rwnd,
6115 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
6116 stcb->asoc.total_flight);
6117 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
6118 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
6119 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
6121 if (cnt_ctrl_ready ||
6123 (stcb->asoc.peers_rwnd > 0 ||
6124 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
6125 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
6126 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
6127 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
6129 #ifdef SCTP_AUDITING_ENABLED
6130 sctp_audit_log(0xE0, 3);
6131 sctp_auditing(2, inp, stcb, net);
6135 SCTP_TCB_UNLOCK(stcb);
6137 if (inp_decr != NULL) {
6138 /* reduce ref-count */
6139 SCTP_INP_WLOCK(inp_decr);
6140 SCTP_INP_DECR_REF(inp_decr);
6141 SCTP_INP_WUNLOCK(inp_decr);
6145 sctp_validate_no_locks(inp);
6153 sctp_print_mbuf_chain(struct mbuf *m)
6155 for (; m; m = SCTP_BUF_NEXT(m)) {
6156 SCTP_PRINTF("%p: m_len = %ld\n", (void *)m, SCTP_BUF_LEN(m));
6157 if (SCTP_BUF_IS_EXTENDED(m))
6158 SCTP_PRINTF("%p: extend_size = %d\n", (void *)m, SCTP_BUF_EXTEND_SIZE(m));
6164 #if !defined(__Userspace__)
6165 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
6167 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
6168 #elif defined(__Panda__)
6170 sctp_input(pakhandle_type i_pak)
6174 sctp_input(struct mbuf *i_pak,...)
6176 sctp_input(i_pak, va_alist)
6183 uint32_t vrf_id = 0;
6185 struct sockaddr_in src, dst;
6188 struct sctp_chunkhdr *ch;
6190 #if !defined(SCTP_WITH_NO_CSUM)
6191 uint8_t compute_crc;
6193 #if defined(__FreeBSD__)
6195 uint8_t use_mflowid;
6197 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__))
6201 #if defined(__Panda__)
6202 /* This is Evil, but its the only way to make panda work right. */
6203 iphlen = sizeof(struct ip);
6207 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
6208 SCTP_RELEASE_PKT(i_pak);
6211 m = SCTP_HEADER_TO_CHAIN(i_pak);
6213 SCTP_DETACH_HEADER_FROM_CHAIN(i_pak);
6214 (void)SCTP_RELEASE_HEADER(i_pak);
6216 #ifdef SCTP_MBUF_LOGGING
6217 /* Log in any input mbufs */
6218 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6221 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
6222 if (SCTP_BUF_IS_EXTENDED(mat)) {
6223 sctp_log_mb(mat, SCTP_MBUF_INPUT);
6228 #ifdef SCTP_PACKET_LOGGING
6229 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
6233 #if defined(__FreeBSD__)
6234 #if __FreeBSD_version > 1000049
6235 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6236 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
6238 if_name(m->m_pkthdr.rcvif),
6239 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
6240 #elif __FreeBSD_version >= 800000
6241 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6242 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
6244 if_name(m->m_pkthdr.rcvif),
6245 m->m_pkthdr.csum_flags);
6247 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6248 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
6250 m->m_pkthdr.rcvif->if_xname,
6251 m->m_pkthdr.csum_flags);
6254 #if defined(__APPLE__)
6255 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6256 "sctp_input(): Packet of length %d received on %s%d with csum_flags 0x%x.\n",
6258 m->m_pkthdr.rcvif->if_name,
6259 m->m_pkthdr.rcvif->if_unit,
6260 m->m_pkthdr.csum_flags);
6262 #if defined(__Windows__)
6263 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
6264 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
6266 m->m_pkthdr.rcvif->if_xname,
6267 m->m_pkthdr.csum_flags);
6269 #if defined(__FreeBSD__)
6270 if (m->m_flags & M_FLOWID) {
6271 mflowid = m->m_pkthdr.flowid;
6278 SCTP_STAT_INCR(sctps_recvpackets);
6279 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
6280 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
6281 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6282 if (SCTP_BUF_LEN(m) < offset) {
6283 if ((m = m_pullup(m, offset)) == NULL) {
6284 SCTP_STAT_INCR(sctps_hdrops);
6288 ip = mtod(m, struct ip *);
6289 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
6290 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
6291 offset -= sizeof(struct sctp_chunkhdr);
6292 memset(&src, 0, sizeof(struct sockaddr_in));
6293 src.sin_family = AF_INET;
6295 src.sin_len = sizeof(struct sockaddr_in);
6297 src.sin_port = sh->src_port;
6298 src.sin_addr = ip->ip_src;
6299 memset(&dst, 0, sizeof(struct sockaddr_in));
6300 dst.sin_family = AF_INET;
6302 dst.sin_len = sizeof(struct sockaddr_in);
6304 dst.sin_port = sh->dest_port;
6305 dst.sin_addr = ip->ip_dst;
6306 #if defined(__Windows__)
6309 #if defined(__Userspace_os_Linux) || defined(__Userspace_os_Windows)
6310 ip->ip_len = ntohs(ip->ip_len);
6312 #if defined(__FreeBSD__)
6313 #if __FreeBSD_version >= 1000000
6314 length = ntohs(ip->ip_len);
6316 length = ip->ip_len + iphlen;
6318 #elif defined(__APPLE__)
6319 length = ip->ip_len + iphlen;
6320 #elif defined(__Userspace__)
6321 #if defined(__Userspace_os_Linux) || defined(__Userspace_os_Windows)
6322 length = ip->ip_len;
6324 length = ip->ip_len + iphlen;
6327 length = ip->ip_len;
6329 /* Validate mbuf chain length with IP payload length. */
6330 if (SCTP_HEADER_LEN(m) != length) {
6331 SCTPDBG(SCTP_DEBUG_INPUT1,
6332 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
6333 SCTP_STAT_INCR(sctps_hdrops);
6336 /* SCTP does not allow broadcasts or multicasts */
6337 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
6340 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
6343 ecn_bits = ip->ip_tos;
6344 #if defined(SCTP_WITH_NO_CSUM)
6345 SCTP_STAT_INCR(sctps_recvnocrc);
6347 #if defined(__FreeBSD__) && __FreeBSD_version >= 800000
6348 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
6349 SCTP_STAT_INCR(sctps_recvhwcrc);
6353 if (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
6354 ((src.sin_addr.s_addr == dst.sin_addr.s_addr) ||
6355 (SCTP_IS_IT_LOOPBACK(m)))) {
6356 SCTP_STAT_INCR(sctps_recvnocrc);
6360 SCTP_STAT_INCR(sctps_recvswcrc);
6364 sctp_common_input_processing(&m, iphlen, offset, length,
6365 (struct sockaddr *)&src,
6366 (struct sockaddr *)&dst,
6368 #if !defined(SCTP_WITH_NO_CSUM)
6372 #if defined(__FreeBSD__)
6373 use_mflowid, mflowid,
6383 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6384 extern int *sctp_cpuarry;
6387 #if defined(__FreeBSD__) && __FreeBSD_version >= 1100020
6389 sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
6398 sctp_input(struct mbuf *m, int off)
6401 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6407 uint32_t flowid, tag;
6409 if (m->m_flags & M_FLOWID) {
6410 flowid = m->m_pkthdr.flowid;
6412 /* No flow id built by lower layers
6413 * fix it so we create one.
6415 offset = off + sizeof(struct sctphdr);
6416 if (SCTP_BUF_LEN(m) < offset) {
6417 if ((m = m_pullup(m, offset)) == NULL) {
6418 SCTP_STAT_INCR(sctps_hdrops);
6419 #if defined(__FreeBSD__) && __FreeBSD_version >= 1100020
6420 return (IPPROTO_DONE);
6426 ip = mtod(m, struct ip *);
6427 sh = (struct sctphdr *)((caddr_t)ip + off);
6428 tag = htonl(sh->v_tag);
6429 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6430 m->m_pkthdr.flowid = flowid;
6431 m->m_flags |= M_FLOWID;
6433 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6434 sctp_queue_to_mcore(m, off, cpu_to_use);
6435 #if defined(__FreeBSD__) && __FreeBSD_version >= 1100020
6436 return (IPPROTO_DONE);
6442 sctp_input_with_port(m, off, 0);
6443 #if defined(__FreeBSD__) && __FreeBSD_version >= 1100020
6444 return (IPPROTO_DONE);