1 /* $KAME: sctp_input.c,v 1.27 2005/03/06 16:04:17 itojun Exp $ */
2 /* $DragonFly: src/sys/netinet/sctp_input.c,v 1.6 2005/07/15 17:19:28 eirikn Exp $ */
5 * Copyright (C) 2002, 2003, 2004 Cisco Systems Inc,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if !(defined(__OpenBSD__) || defined(__APPLE__))
34 #include "opt_ipsec.h"
36 #if defined(__FreeBSD__) || defined(__DragonFly__)
37 #include "opt_compat.h"
38 #include "opt_inet6.h"
41 #if defined(__NetBSD__)
46 #elif !defined(__OpenBSD__)
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/malloc.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/domain.h>
58 #include <sys/protosw.h>
59 #include <sys/kernel.h>
60 #include <sys/errno.h>
61 #include <sys/syslog.h>
62 #include <sys/thread2.h>
64 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
65 #include <sys/limits.h>
67 #include <machine/limits.h>
69 #include <machine/cpu.h>
72 #include <net/route.h>
73 #include <net/if_types.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/in_pcb.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip_var.h>
83 #include <netinet/ip6.h>
84 #include <netinet6/ip6_var.h>
87 #include <netinet/ip_icmp.h>
88 #include <netinet/icmp_var.h>
89 #include <netinet/sctp_var.h>
90 #include <netinet/sctp_pcb.h>
91 #include <netinet/sctp_header.h>
92 #include <netinet/sctputil.h>
93 #include <netinet/sctp_output.h>
94 #include <netinet/sctp_input.h>
95 #include <netinet/sctp_hashdriver.h>
96 #include <netinet/sctp_indata.h>
97 #include <netinet/sctp_asconf.h>
101 #elif !defined(__FreeBSD__)
102 #include <machine/stdarg.h>
107 #include <netinet6/ipsec.h>
108 #include <netkey/key.h>
114 #include <net/net_osdep.h>
117 extern u_int32_t sctp_debug_on;
122 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
123 struct sctphdr *sh, struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
124 struct sctp_tcb *stcb, struct sctp_nets *net)
126 struct sctp_init *init;
129 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
130 printf("sctp_handle_init: handling INIT tcb:%p\n", stcb);
135 /* First are we accepting? */
136 if (((inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) == 0) ||
137 (inp->sctp_socket->so_qlimit == 0)) {
138 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
141 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
143 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
144 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
147 /* validate parameters */
148 if (init->initiate_tag == 0) {
149 /* protocol error... send abort */
150 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
151 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
154 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
155 /* invalid parameter... send abort */
156 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
157 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
160 if (init->num_inbound_streams == 0) {
161 /* protocol error... send abort */
162 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
163 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
166 if (init->num_outbound_streams == 0) {
167 /* protocol error... send abort */
168 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
169 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
173 /* send an INIT-ACK w/cookie */
175 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
176 printf("sctp_handle_init: sending INIT-ACK\n");
180 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp);
184 * process peer "INIT/INIT-ACK" chunk
185 * returns value < 0 on error
189 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb,
190 struct sctp_nets *net)
192 struct sctp_init *init;
193 struct sctp_association *asoc;
194 struct sctp_nets *lnet;
199 /* save off parameters */
200 asoc->peer_vtag = ntohl(init->initiate_tag);
201 asoc->peers_rwnd = ntohl(init->a_rwnd);
203 if (TAILQ_FIRST(&asoc->nets)) {
204 /* update any ssthresh's that may have a default */
205 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
206 lnet->ssthresh = asoc->peers_rwnd;
209 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
211 struct sctp_stream_out *outs;
212 struct sctp_tmit_chunk *chk;
214 /* cut back on number of streams */
215 newcnt = ntohs(init->num_inbound_streams);
216 /* This if is probably not needed but I am cautious */
218 /* First make sure no data chunks are trapped */
219 for (i=newcnt; i < asoc->pre_open_streams; i++) {
220 outs = &asoc->strmout[i];
221 chk = TAILQ_FIRST(&outs->outqueue);
223 TAILQ_REMOVE(&outs->outqueue, chk,
225 asoc->stream_queue_cnt--;
226 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL,
227 stcb, SCTP_NOTIFY_DATAGRAM_UNSENT,
230 sctp_m_freem(chk->data);
233 sctp_free_remote_addr(chk->whoTo);
237 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
238 sctppcbinfo.ipi_count_chunk--;
239 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
240 panic("Chunk count is negative");
242 sctppcbinfo.ipi_gencnt_chunk++;
243 chk = TAILQ_FIRST(&outs->outqueue);
247 /* cut back the count and abandon the upper streams */
248 asoc->pre_open_streams = newcnt;
250 asoc->streamincnt = ntohs(init->num_outbound_streams);
251 if (asoc->streamincnt > MAX_SCTP_STREAMS) {
252 asoc->streamincnt = MAX_SCTP_STREAMS;
255 asoc->streamoutcnt = asoc->pre_open_streams;
257 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
258 #ifdef SCTP_MAP_LOGGING
259 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
261 /* This is the next one we expect */
262 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
264 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
265 asoc->cumulative_tsn = asoc->asconf_seq_in;
266 asoc->last_echo_tsn = asoc->asconf_seq_in;
267 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
268 /* open the requested streams */
269 if (asoc->strmin != NULL) {
270 /* Free the old ones */
271 FREE(asoc->strmin, M_PCB);
273 MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
274 sizeof(struct sctp_stream_in), M_PCB, M_NOWAIT);
275 if (asoc->strmin == NULL) {
276 /* we didn't get memory for the streams! */
278 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
279 printf("process_init: couldn't get memory for the streams!\n");
284 for (i = 0; i < asoc->streamincnt; i++) {
285 asoc->strmin[i].stream_no = i;
286 asoc->strmin[i].last_sequence_delivered = 0xffff;
288 * U-stream ranges will be set when the cookie
289 * is unpacked. Or for the INIT sender they
290 * are un set (if pr-sctp not supported) when the
293 TAILQ_INIT(&asoc->strmin[i].inqueue);
295 * we are not on any wheel, pr-sctp streams
296 * will go on the wheel when they have data waiting
299 asoc->strmin[i].next_spoke.tqe_next = 0;
300 asoc->strmin[i].next_spoke.tqe_prev = 0;
304 * load_address_from_init will put the addresses into the
305 * association when the COOKIE is processed or the INIT-ACK
306 * is processed. Both types of COOKIE's existing and new
307 * call this routine. It will remove addresses that
308 * are no longer in the association (for the restarting
309 * case where addresses are removed). Up front when the
310 * INIT arrives we will discard it if it is a restart
311 * and new addresses have been added.
317 * INIT-ACK message processing/consumption
318 * returns value < 0 on error
321 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
322 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
323 struct sctp_nets *net)
325 struct sctp_association *asoc;
327 int retval, abort_flag;
328 uint32_t initack_limit;
329 /* First verify that we have no illegal param's */
333 op_err = sctp_arethere_unrecognized_parameters(m,
334 (offset+sizeof(struct sctp_init_chunk)) ,
335 &abort_flag, (struct sctp_chunkhdr *)cp);
337 /* Send an abort and notify peer */
338 if (op_err != NULL) {
339 sctp_send_operr_to(m, iphlen, op_err, cp->init.initiate_tag);
342 * Just notify (abort_assoc does this if
345 sctp_abort_notification(stcb, 0);
347 * No sense in further INIT's since
348 * we will get the same param back
350 sctp_free_assoc(stcb->sctp_ep, stcb);
355 /* process the peer's parameters in the INIT-ACK */
356 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net);
361 initack_limit = offset + ntohs(cp->ch.chunk_length);
362 /* load all addresses */
363 if (sctp_load_addresses_from_init(stcb, m, iphlen,
364 (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh,
366 /* Huh, we should abort */
367 sctp_abort_notification(stcb, 0);
368 sctp_free_assoc(stcb->sctp_ep, stcb);
372 sctp_queue_op_err(stcb, op_err);
373 /* queuing will steal away the mbuf chain to the out queue */
376 /* extract the cookie and queue it to "echo" it back... */
377 stcb->asoc.overall_error_count = 0;
378 net->error_count = 0;
379 retval = sctp_send_cookie_echo(m, offset, stcb, net);
382 * No cookie, we probably should send a op error.
383 * But in any case if there is no cookie in the INIT-ACK,
384 * we can abandon the peer, its broke.
387 /* We abort with an error of missing mandatory param */
390 sctp_generate_invmanparam(SCTP_CAUSE_MISS_PARAM);
393 * Expand beyond to include the mandatory
396 struct sctp_inv_mandatory_param *mp;
398 sizeof(struct sctp_inv_mandatory_param);
400 struct sctp_inv_mandatory_param *);
401 /* Subtract the reserved param */
403 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
404 mp->num_param = htonl(1);
405 mp->param = htons(SCTP_STATE_COOKIE);
408 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
415 * Cancel the INIT timer, We do this first before queueing
416 * the cookie. We always cancel at the primary to assue that
417 * we are canceling the timer started by the INIT which always
418 * goes to the primary.
420 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
421 asoc->primary_destination);
423 /* calculate the RTO */
424 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered);
430 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
431 struct sctp_tcb *stcb, struct sctp_nets *net)
433 struct sockaddr_storage store;
434 struct sockaddr_in *sin;
435 struct sockaddr_in6 *sin6;
436 struct sctp_nets *r_net;
439 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
444 sin = (struct sockaddr_in *)&store;
445 sin6 = (struct sockaddr_in6 *)&store;
447 memset(&store, 0, sizeof(store));
448 if (cp->heartbeat.hb_info.addr_family == AF_INET &&
449 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
450 sin->sin_family = cp->heartbeat.hb_info.addr_family;
451 sin->sin_len = cp->heartbeat.hb_info.addr_len;
452 sin->sin_port = stcb->rport;
453 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
454 sizeof(sin->sin_addr));
455 } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 &&
456 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
457 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
458 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
459 sin6->sin6_port = stcb->rport;
460 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
461 sizeof(sin6->sin6_addr));
464 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
465 printf("unsupported address family");
470 r_net = sctp_findnet(stcb, (struct sockaddr *)sin);
473 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
474 printf("Huh? I can't find the address I sent it to, discard\n");
479 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
480 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
481 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
483 * If the its a HB and it's random value is correct when
484 * can confirm the destination.
486 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
487 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
488 stcb, 0, (void *)r_net);
490 r_net->error_count = 0;
491 r_net->hb_responded = 1;
492 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
493 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
494 if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
495 r_net->dest_state = SCTP_ADDR_REACHABLE;
496 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
497 SCTP_HEARTBEAT_SUCCESS, (void *)r_net);
499 /* now was it the primary? if so restore */
500 if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
501 sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net);
504 /* Now lets do a RTO with this */
505 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv);
509 sctp_handle_abort(struct sctp_abort_chunk *cp,
510 struct sctp_tcb *stcb, struct sctp_nets *net)
512 struct sctp_inpcb *inp;
515 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
516 printf("sctp_handle_abort: handling ABORT\n");
521 /* verify that the destination addr is in the association */
522 /* ignore abort for addresses being deleted */
524 /* stop any receive timers */
525 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net);
526 /* notify user of the abort and clean up... */
527 sctp_abort_notification(stcb, 0);
530 sctp_free_assoc(stcb->sctp_ep, stcb);
532 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
533 printf("sctp_handle_abort: finished\n");
539 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
540 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
542 struct sctp_association *asoc;
543 int some_on_streamwheel;
546 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
547 printf("sctp_handle_shutdown: handling SHUTDOWN\n");
553 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
554 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
558 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
559 /* update current data status */
561 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
562 printf("Warning Shutdown NOT the expected size.. skipping (%d:%d)\n",
563 ntohs(cp->ch.chunk_length),
564 (int)sizeof(struct sctp_shutdown_chunk));
569 sctp_update_acked(stcb, cp, net, abort_flag);
572 /* goto SHUTDOWN_RECEIVED state to block new requests */
573 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
574 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
575 asoc->state = SCTP_STATE_SHUTDOWN_RECEIVED;
577 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
578 printf("Moving to SHUTDOWN-RECEIVED state\n");
581 /* notify upper layer that peer has initiated a shutdown */
582 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL);
584 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
585 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
587 /* Set the flag so we cannot send more, we
588 * would call the function but we don't want to
589 * wake up the ulp necessarily.
591 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
592 stcb->sctp_ep->sctp_socket->so_rcv.sb_state |= SBS_CANTSENDMORE;
594 stcb->sctp_ep->sctp_socket->so_state |= SS_CANTSENDMORE;
598 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
600 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
602 * stop the shutdown timer, since we WILL move
603 * to SHUTDOWN-ACK-SENT.
605 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
607 /* Now are we there yet? */
608 some_on_streamwheel = 0;
609 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
610 /* Check to see if some data queued */
611 struct sctp_stream_out *outs;
612 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
613 if (!TAILQ_EMPTY(&outs->outqueue)) {
614 some_on_streamwheel = 1;
620 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
621 printf("some_on_streamwheel:%d send_q_empty:%d sent_q_empty:%d\n",
623 !TAILQ_EMPTY(&asoc->send_queue),
624 !TAILQ_EMPTY(&asoc->sent_queue));
627 if (!TAILQ_EMPTY(&asoc->send_queue) ||
628 !TAILQ_EMPTY(&asoc->sent_queue) ||
629 some_on_streamwheel) {
630 /* By returning we will push more data out */
633 /* no outstanding data to send, so move on... */
634 /* send SHUTDOWN-ACK */
635 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
636 /* move to SHUTDOWN-ACK-SENT state */
637 asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
639 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
640 printf("moving to SHUTDOWN_ACK state\n");
643 /* start SHUTDOWN timer */
644 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
650 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp,
651 struct sctp_tcb *stcb, struct sctp_nets *net)
653 struct sctp_association *asoc;
656 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
657 printf("sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
664 /* process according to association state */
665 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
666 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
667 /* unexpected SHUTDOWN-ACK... so ignore... */
670 /* are the queues empty? */
671 if (!TAILQ_EMPTY(&asoc->send_queue) ||
672 !TAILQ_EMPTY(&asoc->sent_queue) ||
673 !TAILQ_EMPTY(&asoc->out_wheel)) {
674 sctp_report_all_outbound(stcb);
677 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
678 /* send SHUTDOWN-COMPLETE */
679 sctp_send_shutdown_complete(stcb, net);
680 /* notify upper layer protocol */
681 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL);
682 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
683 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
684 stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
685 /* Set the connected flag to disconnected */
686 stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
687 stcb->sctp_ep->sctp_socket->so_snd.sb_mbcnt = 0;
688 soisdisconnected(stcb->sctp_ep->sctp_socket);
690 /* free the TCB but first save off the ep */
691 sctp_free_assoc(stcb->sctp_ep, stcb);
695 * Skip past the param header and then we will find the chunk that
696 * caused the problem. There are two possiblities ASCONF or FWD-TSN
697 * other than that and our peer must be broken.
700 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
701 struct sctp_nets *net)
703 struct sctp_chunkhdr *chk;
705 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
706 switch (chk->chunk_type) {
707 case SCTP_ASCONF_ACK:
709 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
710 printf("Strange peer, snds ASCONF but does not recongnize asconf-ack?\n");
715 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
716 printf("Peer does not support ASCONF/ASCONF-ACK chunks\n");
718 #endif /* SCTP_DEBUG */
719 sctp_asconf_cleanup(stcb, net);
721 case SCTP_FORWARD_CUM_TSN:
722 stcb->asoc.peer_supports_prsctp = 0;
726 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
727 printf("Peer does not support chunk type %d(%x)??\n",
728 chk->chunk_type, (u_int)chk->chunk_type);
736 * Skip past the param header and then we will find the param that
737 * caused the problem. There are a number of param's in a ASCONF
738 * OR the prsctp param these will turn of specific features.
741 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
743 struct sctp_paramhdr *pbad;
746 switch (ntohs(pbad->param_type)) {
748 case SCTP_PRSCTP_SUPPORTED:
749 stcb->asoc.peer_supports_prsctp = 0;
751 case SCTP_SUPPORTED_CHUNK_EXT:
753 /* draft-ietf-tsvwg-addip-sctp */
754 case SCTP_ECN_NONCE_SUPPORTED:
755 stcb->asoc.peer_supports_ecn_nonce = 0;
756 stcb->asoc.ecn_nonce_allowed = 0;
757 stcb->asoc.ecn_allowed = 0;
759 case SCTP_ADD_IP_ADDRESS:
760 case SCTP_DEL_IP_ADDRESS:
761 stcb->asoc.peer_supports_asconf = 0;
763 case SCTP_SET_PRIM_ADDR:
764 stcb->asoc.peer_supports_asconf_setprim = 0;
766 case SCTP_SUCCESS_REPORT:
767 case SCTP_ERROR_CAUSE_IND:
769 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
770 printf("Huh, the peer does not support success? or error cause?\n");
771 printf("Turning off ASCONF to this strange peer\n");
774 stcb->asoc.peer_supports_asconf = 0;
775 stcb->asoc.peer_supports_asconf_setprim = 0;
779 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
780 printf("Peer does not support base param type %d(%x)??\n",
781 pbad->param_type, (u_int)pbad->param_type);
789 sctp_handle_error(struct sctp_chunkhdr *ch,
790 struct sctp_tcb *stcb, struct sctp_nets *net)
793 struct sctp_paramhdr *phdr;
796 struct sctp_association *asoc;
799 /* parse through all of the errors and process */
801 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
802 sizeof(struct sctp_chunkhdr));
803 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
804 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
805 /* Process an Error Cause */
806 error_type = ntohs(phdr->param_type);
807 error_len = ntohs(phdr->param_length);
808 if ((error_len > chklen) || (error_len == 0)) {
809 /* invalid param length for this param */
811 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
812 printf("Bogus length in error param- chunk left:%d errorlen:%d\n",
815 #endif /* SCTP_DEBUG */
818 switch (error_type) {
819 case SCTP_CAUSE_INV_STRM:
820 case SCTP_CAUSE_MISS_PARAM:
821 case SCTP_CAUSE_INVALID_PARAM:
822 case SCTP_CAUSE_NOUSER_DATA:
824 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
825 printf("Software error we got a %d back? We have a bug :/ (or do they?)\n",
830 case SCTP_CAUSE_STALE_COOKIE:
831 /* We only act if we have echoed a cookie and are waiting. */
832 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
834 p = (int *)((caddr_t)phdr + sizeof(*phdr));
835 /* Save the time doubled */
836 asoc->cookie_preserve_req = ntohl(*p) << 1;
837 asoc->stale_cookie_count++;
838 if (asoc->stale_cookie_count >
839 asoc->max_init_times) {
840 sctp_abort_notification(stcb, 0);
841 /* now free the asoc */
842 sctp_free_assoc(stcb->sctp_ep, stcb);
845 /* blast back to INIT state */
846 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
847 asoc->state |= SCTP_STATE_COOKIE_WAIT;
848 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
849 stcb->sctp_ep, stcb, net);
850 sctp_send_initiate(stcb->sctp_ep, stcb);
853 case SCTP_CAUSE_UNRESOLV_ADDR:
855 * Nothing we can do here, we don't do hostname
856 * addresses so if the peer does not like my IPv6 (or
857 * IPv4 for that matter) it does not matter. If they
858 * don't support that type of address, they can NOT
859 * possibly get that packet type... i.e. with no IPv6
860 * you can't recieve a IPv6 packet. so we can safely
861 * ignore this one. If we ever added support for
862 * HOSTNAME Addresses, then we would need to do
866 case SCTP_CAUSE_UNRECOG_CHUNK:
867 sctp_process_unrecog_chunk(stcb, phdr, net);
869 case SCTP_CAUSE_UNRECOG_PARAM:
870 sctp_process_unrecog_param(stcb, phdr);
872 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
874 * We ignore this since the timer will drive out a new
875 * cookie anyway and there timer will drive us to send
876 * a SHUTDOWN_COMPLETE. We can't send one here since
877 * we don't have their tag.
880 case SCTP_CAUSE_DELETEING_LAST_ADDR:
881 case SCTP_CAUSE_OPERATION_REFUSED:
882 case SCTP_CAUSE_DELETING_SRC_ADDR:
883 /* We should NOT get these here, but in a ASCONF-ACK. */
885 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
886 printf("Peer sends ASCONF errors in a Operational Error?<%d>?\n",
891 case SCTP_CAUSE_OUT_OF_RESC:
893 * And what, pray tell do we do with the fact
894 * that the peer is out of resources? Not
895 * really sure we could do anything but abort.
896 * I suspect this should have came WITH an
897 * abort instead of in a OP-ERROR.
902 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
903 /* don't know what this error cause is... */
904 printf("sctp_handle_error: unknown error type = 0x%xh\n",
907 #endif /* SCTP_DEBUG */
910 adjust = SCTP_SIZE32(error_len);
912 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
918 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
919 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
920 struct sctp_nets *net)
922 struct sctp_init_ack *init_ack;
927 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
928 printf("sctp_handle_init_ack: handling INIT-ACK\n");
933 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
934 printf("sctp_handle_init_ack: TCB is null\n");
939 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
941 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
942 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
946 init_ack = &cp->init;
947 /* validate parameters */
948 if (init_ack->initiate_tag == 0) {
949 /* protocol error... send an abort */
950 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
951 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
955 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
956 /* protocol error... send an abort */
957 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
958 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
962 if (init_ack->num_inbound_streams == 0) {
963 /* protocol error... send an abort */
964 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
965 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
969 if (init_ack->num_outbound_streams == 0) {
970 /* protocol error... send an abort */
971 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
972 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
977 /* process according to association state... */
978 state = &stcb->asoc.state;
979 switch (*state & SCTP_STATE_MASK) {
980 case SCTP_STATE_COOKIE_WAIT:
981 /* this is the expected state for this chunk */
982 /* process the INIT-ACK parameters */
983 if (stcb->asoc.primary_destination->dest_state &
984 SCTP_ADDR_UNCONFIRMED) {
986 * The primary is where we sent the INIT, we can
987 * always consider it confirmed when the INIT-ACK
988 * is returned. Do this before we load addresses
991 stcb->asoc.primary_destination->dest_state &=
992 ~SCTP_ADDR_UNCONFIRMED;
993 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
994 stcb, 0, (void *)stcb->asoc.primary_destination);
996 if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, net
998 /* error in parsing parameters */
1000 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1001 printf("sctp_process_init_ack: error in msg, discarding\n");
1006 /* update our state */
1008 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1009 printf("moving to COOKIE-ECHOED state\n");
1012 if (*state & SCTP_STATE_SHUTDOWN_PENDING) {
1013 *state = SCTP_STATE_COOKIE_ECHOED |
1014 SCTP_STATE_SHUTDOWN_PENDING;
1016 *state = SCTP_STATE_COOKIE_ECHOED;
1019 /* reset the RTO calc */
1020 stcb->asoc.overall_error_count = 0;
1021 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1023 * collapse the init timer back in case of a exponential backoff
1025 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1028 * the send at the end of the inbound data processing will
1029 * cause the cookie to be sent
1032 case SCTP_STATE_SHUTDOWN_SENT:
1033 /* incorrect state... discard */
1035 case SCTP_STATE_COOKIE_ECHOED:
1036 /* incorrect state... discard */
1038 case SCTP_STATE_OPEN:
1039 /* incorrect state... discard */
1041 case SCTP_STATE_EMPTY:
1042 case SCTP_STATE_INUSE:
1044 /* incorrect state... discard */
1046 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1047 printf("Leaving handle-init-ack default\n");
1052 } /* end switch asoc state */
1054 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1055 printf("Leaving handle-init-ack end\n");
1063 * handle a state cookie for an existing association
1064 * m: input packet mbuf chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk
1065 * note: this is a "split" mbuf and the cookie signature does not exist
1066 * offset: offset into mbuf to the cookie-echo chunk
1068 static struct sctp_tcb *
1069 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1070 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1071 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
1072 struct sockaddr *init_src, int *notification)
1074 struct sctp_association *asoc;
1075 struct sctp_init_chunk *init_cp, init_buf;
1076 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1078 int init_offset, initack_offset;
1081 /* I know that the TCB is non-NULL from the caller */
1084 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1085 /* SHUTDOWN came in after sending INIT-ACK */
1086 struct mbuf *op_err;
1087 struct sctp_paramhdr *ph;
1089 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1091 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1092 printf("sctp_handle_cookie: got a cookie, while shutting down!\n");
1095 MGETHDR(op_err, MB_DONTWAIT, MT_HEADER);
1096 if (op_err == NULL) {
1100 /* pre-reserve some space */
1101 op_err->m_data += sizeof(struct ip6_hdr);
1102 op_err->m_data += sizeof(struct sctphdr);
1103 op_err->m_data += sizeof(struct sctp_chunkhdr);
1105 op_err->m_len = op_err->m_pkthdr.len = sizeof(struct sctp_paramhdr);
1106 ph = mtod(op_err, struct sctp_paramhdr *);
1107 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1108 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1109 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag);
1113 * find and validate the INIT chunk in the cookie (peer's info)
1114 * the INIT should start after the cookie-echo header struct
1115 * (chunk header, state cookie header struct)
1117 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1119 init_cp = (struct sctp_init_chunk *)
1120 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1121 (u_int8_t *)&init_buf);
1122 if (init_cp == NULL) {
1123 /* could not pull a INIT chunk in cookie */
1125 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1126 printf("process_cookie_existing: could not pull INIT chunk hdr\n");
1128 #endif /* SCTP_DEBUG */
1131 chk_length = ntohs(init_cp->ch.chunk_length);
1132 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1134 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1135 printf("process_cookie_existing: could not find INIT chunk!\n");
1137 #endif /* SCTP_DEBUG */
1142 * find and validate the INIT-ACK chunk in the cookie (my info)
1143 * the INIT-ACK follows the INIT chunk
1145 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1146 initack_cp = (struct sctp_init_ack_chunk *)
1147 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1148 (u_int8_t *)&initack_buf);
1149 if (initack_cp == NULL) {
1150 /* could not pull INIT-ACK chunk in cookie */
1152 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1153 printf("process_cookie_existing: could not pull INIT-ACK chunk hdr\n");
1155 #endif /* SCTP_DEBUG */
1158 chk_length = ntohs(initack_cp->ch.chunk_length);
1159 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1161 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1162 printf("process_cookie_existing: could not find INIT-ACK chunk!\n");
1164 #endif /* SCTP_DEBUG */
1167 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1168 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1170 * case D in Section 5.2.4 Table 2: MMAA
1171 * process accordingly to get into the OPEN state
1173 switch SCTP_GET_STATE(asoc) {
1174 case SCTP_STATE_COOKIE_WAIT:
1176 * INIT was sent, but got got a COOKIE_ECHO with
1177 * the correct tags... just accept it...
1179 /* First we must process the INIT !! */
1180 retval = sctp_process_init(init_cp, stcb, net);
1183 printf("process_cookie_existing: INIT processing failed\n");
1187 /* intentional fall through to below... */
1189 case SCTP_STATE_COOKIE_ECHOED:
1190 /* Duplicate INIT case */
1191 /* we have already processed the INIT so no problem */
1192 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1194 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
1195 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb,
1197 /* update current state */
1198 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1199 asoc->state = SCTP_STATE_OPEN |
1200 SCTP_STATE_SHUTDOWN_PENDING;
1201 } else if ((asoc->state & SCTP_STATE_SHUTDOWN_SENT) == 0) {
1202 /* if ok, move to OPEN state */
1203 asoc->state = SCTP_STATE_OPEN;
1205 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1206 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1207 (!(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING))) {
1209 * Here is where collision would go if we did a
1210 * connect() and instead got a
1211 * init/init-ack/cookie done before the
1212 * init-ack came back..
1214 stcb->sctp_ep->sctp_flags |=
1215 SCTP_PCB_FLAGS_CONNECTED;
1216 soisconnected(stcb->sctp_ep->sctp_socket);
1218 /* notify upper layer */
1219 *notification = SCTP_NOTIFY_ASSOC_UP;
1220 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1223 * since we did not send a HB make sure we don't double
1226 net->hb_responded = 1;
1228 if (stcb->asoc.sctp_autoclose_ticks &&
1229 (inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
1230 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1236 * we're in the OPEN state (or beyond), so peer
1237 * must have simply lost the COOKIE-ACK
1243 * We ignore the return code here.. not sure if we should
1244 * somehow abort.. but we do have an existing asoc. This
1245 * really should not fail.
1247 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1248 init_offset + sizeof(struct sctp_init_chunk),
1249 initack_offset, sh, init_src)) {
1251 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1252 printf("Weird cookie load_address failure on cookie existing - 1\n");
1258 /* respond with a COOKIE-ACK */
1259 sctp_send_cookie_ack(stcb);
1262 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1263 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1264 cookie->tie_tag_my_vtag == 0 &&
1265 cookie->tie_tag_peer_vtag == 0) {
1267 * case C in Section 5.2.4 Table 2: XMOO
1272 if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag &&
1273 (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag ||
1274 init_cp->init.initiate_tag == 0)) {
1276 * case B in Section 5.2.4 Table 2: MXAA or MOAA
1277 * my info should be ok, re-accept peer info
1279 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1280 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
1281 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
1282 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1284 * since we did not send a HB make sure we don't double things
1286 net->hb_responded = 1;
1287 if (stcb->asoc.sctp_autoclose_ticks &&
1288 (inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
1289 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1292 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1293 asoc->pre_open_streams =
1294 ntohs(initack_cp->init.num_outbound_streams);
1295 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1296 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out =
1297 asoc->init_seq_number;
1298 asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
1299 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1300 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1301 asoc->str_reset_seq_in = asoc->init_seq_number;
1302 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1304 /* process the INIT info (peer's info) */
1305 retval = sctp_process_init(init_cp, stcb, net);
1308 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1309 printf("process_cookie_existing: INIT processing failed\n");
1314 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1315 init_offset + sizeof(struct sctp_init_chunk),
1316 initack_offset, sh, init_src)) {
1318 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1319 printf("Weird cookie load_address failure on cookie existing - 2\n");
1325 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1326 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1327 *notification = SCTP_NOTIFY_ASSOC_UP;
1329 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1330 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1331 !(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
1332 stcb->sctp_ep->sctp_flags |=
1333 SCTP_PCB_FLAGS_CONNECTED;
1334 soisconnected(stcb->sctp_ep->sctp_socket);
1337 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1338 asoc->state = SCTP_STATE_OPEN |
1339 SCTP_STATE_SHUTDOWN_PENDING;
1341 asoc->state = SCTP_STATE_OPEN;
1343 sctp_send_cookie_ack(stcb);
1347 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1348 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1349 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1350 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1351 cookie->tie_tag_peer_vtag != 0) {
1353 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1355 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
1356 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
1357 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1359 /* notify upper layer */
1360 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1362 /* send up all the data */
1363 sctp_report_all_outbound(stcb);
1365 /* process the INIT-ACK info (my info) */
1366 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1367 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1368 asoc->pre_open_streams =
1369 ntohs(initack_cp->init.num_outbound_streams);
1370 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1371 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out =
1372 asoc->init_seq_number;
1373 asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
1374 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1375 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1376 asoc->str_reset_seq_in = asoc->init_seq_number;
1378 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1379 if (asoc->mapping_array)
1380 memset(asoc->mapping_array, 0,
1381 asoc->mapping_array_size);
1382 /* process the INIT info (peer's info) */
1383 retval = sctp_process_init(init_cp, stcb, net);
1386 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1387 printf("process_cookie_existing: INIT processing failed\n");
1393 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1395 * since we did not send a HB make sure we don't double things
1397 net->hb_responded = 1;
1399 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1400 init_offset + sizeof(struct sctp_init_chunk),
1401 initack_offset, sh, init_src)) {
1403 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1404 printf("Weird cookie load_address failure on cookie existing - 3\n");
1410 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1411 asoc->state = SCTP_STATE_OPEN |
1412 SCTP_STATE_SHUTDOWN_PENDING;
1413 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1414 /* move to OPEN state, if not in SHUTDOWN_SENT */
1415 asoc->state = SCTP_STATE_OPEN;
1417 /* respond with a COOKIE-ACK */
1418 sctp_send_cookie_ack(stcb);
1422 /* all other cases... */
1427 * handle a state cookie for a new association
1428 * m: input packet mbuf chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk
1429 * note: this is a "split" mbuf and the cookie signature does not exist
1430 * offset: offset into mbuf to the cookie-echo chunk
1431 * length: length of the cookie chunk
1432 * to: where the init was from
1435 static struct sctp_tcb *
1436 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1437 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1438 struct sctp_inpcb *inp, struct sctp_nets **netp,
1439 struct sockaddr *init_src, int *notification)
1441 struct sctp_tcb *stcb;
1442 struct sctp_init_chunk *init_cp, init_buf;
1443 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1444 struct sockaddr_storage sa_store;
1445 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
1446 struct sockaddr_in *sin;
1447 struct sockaddr_in6 *sin6;
1448 struct sctp_association *asoc;
1450 int init_offset, initack_offset, initack_limit;
1455 * find and validate the INIT chunk in the cookie (peer's info)
1456 * the INIT should start after the cookie-echo header struct
1457 * (chunk header, state cookie header struct)
1459 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
1460 init_cp = (struct sctp_init_chunk *)
1461 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1462 (u_int8_t *)&init_buf);
1463 if (init_cp == NULL) {
1464 /* could not pull a INIT chunk in cookie */
1466 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1467 printf("process_cookie_new: could not pull INIT chunk hdr\n");
1469 #endif /* SCTP_DEBUG */
1472 chk_length = ntohs(init_cp->ch.chunk_length);
1473 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1475 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1476 printf("HUH? process_cookie_new: could not find INIT chunk!\n");
1478 #endif /* SCTP_DEBUG */
1482 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1484 * find and validate the INIT-ACK chunk in the cookie (my info)
1485 * the INIT-ACK follows the INIT chunk
1487 initack_cp = (struct sctp_init_ack_chunk *)
1488 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1489 (u_int8_t *)&initack_buf);
1490 if (initack_cp == NULL) {
1491 /* could not pull INIT-ACK chunk in cookie */
1493 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1494 printf("process_cookie_new: could not pull INIT-ACK chunk hdr\n");
1496 #endif /* SCTP_DEBUG */
1499 chk_length = ntohs(initack_cp->ch.chunk_length);
1500 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1502 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1504 pp = (u_int8_t *)initack_cp;
1505 printf("process_cookie_new: could not find INIT-ACK chunk!\n");
1506 printf("Found bytes %x %x %x %x at postion %d\n",
1507 (u_int)pp[0], (u_int)pp[1], (u_int)pp[2],
1508 (u_int)pp[3], initack_offset);
1510 #endif /* SCTP_DEBUG */
1513 initack_limit = initack_offset + SCTP_SIZE32(chk_length);
1516 * now that we know the INIT/INIT-ACK are in place,
1517 * create a new TCB and popluate
1519 stcb = sctp_aloc_assoc(inp, init_src, 0, &error, ntohl(initack_cp->init.initiate_tag));
1521 struct mbuf *op_err;
1522 /* memory problem? */
1524 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1525 printf("process_cookie_new: no room for another TCB!\n");
1527 #endif /* SCTP_DEBUG */
1528 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1529 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
1534 /* get the correct sctp_nets */
1535 *netp = sctp_findnet(stcb, init_src);
1537 /* get scope variables out of cookie */
1538 asoc->ipv4_local_scope = cookie->ipv4_scope;
1539 asoc->site_scope = cookie->site_scope;
1540 asoc->local_scope = cookie->local_scope;
1541 asoc->loopback_scope = cookie->loopback_scope;
1543 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
1544 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
1545 struct mbuf *op_err;
1547 * Houston we have a problem. The EP changed while the cookie
1548 * was in flight. Only recourse is to abort the association.
1550 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1551 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
1556 /* process the INIT-ACK info (my info) */
1557 old_tag = asoc->my_vtag;
1558 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1559 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1560 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1561 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1562 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1563 asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
1564 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1565 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1566 asoc->str_reset_seq_in = asoc->init_seq_number;
1568 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1570 /* process the INIT info (peer's info) */
1571 retval = sctp_process_init(init_cp, stcb, *netp);
1574 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1575 printf("process_cookie_new: INIT processing failed\n");
1578 sctp_free_assoc(inp, stcb);
1581 /* load all addresses */
1582 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1583 init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
1585 sctp_free_assoc(inp, stcb);
1589 /* update current state */
1591 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1592 printf("moving to OPEN state\n");
1595 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1596 asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
1598 asoc->state = SCTP_STATE_OPEN;
1600 /* calculate the RTT */
1601 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
1602 &cookie->time_entered);
1605 * if we're doing ASCONFs, check to see if we have any new
1606 * local addresses that need to get added to the peer (eg.
1607 * addresses changed while cookie echo in flight). This needs
1608 * to be done after we go to the OPEN state to do the correct
1609 * asconf processing.
1610 * else, make sure we have the correct addresses in our lists
1613 /* warning, we re-use sin, sin6, sa_store here! */
1614 /* pull in local_address (our "from" address) */
1615 if (cookie->laddr_type == SCTP_IPV4_ADDRESS) {
1616 /* source addr is IPv4 */
1617 sin = (struct sockaddr_in *)initack_src;
1618 memset(sin, 0, sizeof(*sin));
1619 sin->sin_family = AF_INET;
1620 sin->sin_len = sizeof(struct sockaddr_in);
1621 sin->sin_addr.s_addr = cookie->laddress[0];
1622 } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) {
1623 /* source addr is IPv6 */
1624 sin6 = (struct sockaddr_in6 *)initack_src;
1625 memset(sin6, 0, sizeof(*sin6));
1626 sin6->sin6_family = AF_INET6;
1627 sin6->sin6_len = sizeof(struct sockaddr_in6);
1628 sin6->sin6_scope_id = cookie->scope_id;
1629 memcpy(&sin6->sin6_addr, cookie->laddress,
1630 sizeof(sin6->sin6_addr));
1632 sctp_free_assoc(inp, stcb);
1636 sctp_check_address_list(stcb, m, initack_offset +
1637 sizeof(struct sctp_init_ack_chunk), initack_limit,
1638 initack_src, cookie->local_scope, cookie->site_scope,
1639 cookie->ipv4_scope, cookie->loopback_scope);
1642 /* set up to notify upper layer */
1643 *notification = SCTP_NOTIFY_ASSOC_UP;
1644 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1645 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1646 !(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
1648 * This is an endpoint that called connect()
1649 * how it got a cookie that is NEW is a bit of
1650 * a mystery. It must be that the INIT was sent, but
1651 * before it got there.. a complete INIT/INIT-ACK/COOKIE
1652 * arrived. But of course then it should have went to
1653 * the other code.. not here.. oh well.. a bit of protection
1656 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1657 soisconnected(stcb->sctp_ep->sctp_socket);
1658 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, *netp);
1659 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1660 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
1662 * We don't want to do anything with this
1663 * one. Since it is the listening guy. The timer will
1664 * get started for accepted connections in the caller.
1668 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, *netp);
1670 /* since we did not send a HB make sure we don't double things */
1671 (*netp)->hb_responded = 1;
1673 if (stcb->asoc.sctp_autoclose_ticks &&
1674 (inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
1675 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
1678 /* respond with a COOKIE-ACK */
1679 sctp_send_cookie_ack(stcb);
1686 * handles a COOKIE-ECHO message
1687 * stcb: modified to either a new or left as existing (non-NULL) TCB
1689 static struct mbuf *
1690 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
1691 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
1692 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp)
1694 struct sctp_state_cookie *cookie;
1695 struct sockaddr_in6 sin6;
1696 struct sockaddr_in sin;
1697 struct sctp_tcb *l_stcb=*stcb;
1698 struct sctp_inpcb *l_inp;
1699 struct sockaddr *to;
1700 struct sctp_pcb *ep;
1702 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
1704 uint8_t cookie_ok = 0;
1705 unsigned int size_of_pkt, sig_offset, cookie_offset;
1706 unsigned int cookie_len;
1708 struct timeval time_expires;
1709 struct sockaddr_storage dest_store;
1710 struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
1712 int notification = 0;
1713 struct sctp_nets *netl;
1714 int had_a_existing_tcb = 0;
1717 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1718 printf("sctp_handle_cookie: handling COOKIE-ECHO\n");
1722 if (inp_p == NULL) {
1724 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1725 printf("sctp_handle_cookie: null inp_p!\n");
1730 /* First get the destination address setup too. */
1731 iph = mtod(m, struct ip *);
1732 if (iph->ip_v == IPVERSION) {
1734 struct sockaddr_in *sin;
1735 sin = (struct sockaddr_in *)(localep_sa);
1736 memset(sin, 0, sizeof(*sin));
1737 sin->sin_family = AF_INET;
1738 sin->sin_len = sizeof(*sin);
1739 sin->sin_port = sh->dest_port;
1740 sin->sin_addr.s_addr = iph->ip_dst.s_addr ;
1741 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1743 struct ip6_hdr *ip6;
1744 struct sockaddr_in6 *sin6;
1745 sin6 = (struct sockaddr_in6 *)(localep_sa);
1746 memset(sin6, 0, sizeof(*sin6));
1747 sin6->sin6_family = AF_INET6;
1748 sin6->sin6_len = sizeof(struct sockaddr_in6);
1749 ip6 = mtod(m, struct ip6_hdr *);
1750 sin6->sin6_port = sh->dest_port;
1751 sin6->sin6_addr = ip6->ip6_dst;
1756 cookie = &cp->cookie;
1757 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
1758 cookie_len = ntohs(cp->ch.chunk_length);
1760 if ((cookie->peerport != sh->src_port) &&
1761 (cookie->myport != sh->dest_port) &&
1762 (cookie->my_vtag != sh->v_tag)) {
1764 * invalid ports or bad tag. Note that we always leave
1765 * the v_tag in the header in network order and when we
1766 * stored it in the my_vtag slot we also left it in network
1767 * order. This maintians the match even though it may be in
1768 * the opposite byte order of the machine :->
1773 /* compute size of packet */
1774 if (m->m_flags & M_PKTHDR) {
1775 size_of_pkt = m->m_pkthdr.len;
1777 /* Should have a pkt hdr really */
1781 while (mat != NULL) {
1782 size_of_pkt += mat->m_len;
1786 if (cookie_len > size_of_pkt ||
1787 cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
1788 sizeof(struct sctp_init_chunk) +
1789 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
1790 /* cookie too long! or too small */
1792 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1793 printf("sctp_handle_cookie: cookie_len=%u, pkt size=%u\n", cookie_len, size_of_pkt);
1795 #endif /* SCTP_DEBUG */
1799 * split off the signature into its own mbuf (since it
1800 * should not be calculated in the sctp_hash_digest_m() call).
1802 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
1803 if (sig_offset > size_of_pkt) {
1804 /* packet not correct size! */
1805 /* XXX this may already be accounted for earlier... */
1807 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1808 printf("sctp_handle_cookie: sig offset=%u, pkt size=%u\n", sig_offset, size_of_pkt);
1814 m_sig = m_split(m, sig_offset, MB_DONTWAIT);
1815 if (m_sig == NULL) {
1816 /* out of memory or ?? */
1818 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1819 printf("sctp_handle_cookie: couldn't m_split the signature\n");
1825 * compute the signature/digest for the cookie
1827 ep = &(*inp_p)->sctp_ep;
1830 SCTP_TCB_UNLOCK(l_stcb);
1832 SCTP_INP_RLOCK(l_inp);
1834 SCTP_TCB_LOCK(l_stcb);
1836 /* which cookie is it? */
1837 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
1838 (ep->current_secret_number != ep->last_secret_number)) {
1839 /* it's the old cookie */
1841 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1842 printf("sctp_handle_cookie: old cookie sig\n");
1845 sctp_hash_digest_m((char *)ep->secret_key[(int)ep->last_secret_number],
1846 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1848 /* it's the current cookie */
1850 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1851 printf("sctp_handle_cookie: current cookie sig\n");
1854 sctp_hash_digest_m((char *)ep->secret_key[(int)ep->current_secret_number],
1855 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1857 /* get the signature */
1858 SCTP_INP_RUNLOCK(l_inp);
1859 sig = (u_int8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (u_int8_t *)&tmp_sig);
1861 /* couldn't find signature */
1863 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1864 printf("sctp_handle_cookie: couldn't pull the signature\n");
1869 /* compare the received digest with the computed digest */
1870 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
1871 /* try the old cookie? */
1872 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
1873 (ep->current_secret_number != ep->last_secret_number)) {
1874 /* compute digest with old */
1876 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1877 printf("sctp_handle_cookie: old cookie sig\n");
1880 sctp_hash_digest_m((char *)ep->secret_key[(int)ep->last_secret_number],
1881 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1883 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
1891 * Now before we continue we must reconstruct our mbuf so
1892 * that normal processing of any other chunks will work.
1897 while (m_at->m_next != NULL) {
1898 m_at = m_at->m_next;
1900 m_at->m_next = m_sig;
1901 if (m->m_flags & M_PKTHDR) {
1903 * We should only do this if and only if the front
1904 * mbuf has a m_pkthdr... it should in theory.
1906 if (m_sig->m_flags & M_PKTHDR) {
1907 /* Add back to the pkt hdr of main m chain */
1908 m->m_pkthdr.len += m_sig->m_len;
1911 * Got a problem, no pkthdr in split chain.
1912 * TSNH but we will handle it just in case
1916 printf("Warning: Hitting m_split join TSNH code - fixed\n");
1919 mmlen += lat->m_len;
1922 m->m_pkthdr.len += mmlen;
1927 if (cookie_ok == 0) {
1929 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1930 printf("handle_cookie_echo: cookie signature validation failed!\n");
1931 printf("offset = %u, cookie_offset = %u, sig_offset = %u\n",
1932 (u_int32_t)offset, cookie_offset, sig_offset);
1938 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1939 printf("handle_cookie_echo: cookie signature validation passed\n");
1944 * check the cookie timestamps to be sure it's not stale
1946 SCTP_GETTIME_TIMEVAL(&now);
1947 /* Expire time is in Ticks, so we convert to seconds */
1948 time_expires.tv_sec = cookie->time_entered.tv_sec + cookie->cookie_life;
1949 time_expires.tv_usec = cookie->time_entered.tv_usec;
1951 if (timercmp(&now, &time_expires, >))
1953 if (timevalcmp(&now, &time_expires, >))
1956 /* cookie is stale! */
1957 struct mbuf *op_err;
1958 struct sctp_stale_cookie_msg *scm;
1961 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1962 printf("sctp_handle_cookie: got a STALE cookie!\n");
1965 MGETHDR(op_err, MB_DONTWAIT, MT_HEADER);
1966 if (op_err == NULL) {
1970 /* pre-reserve some space */
1971 op_err->m_data += sizeof(struct ip6_hdr);
1972 op_err->m_data += sizeof(struct sctphdr);
1973 op_err->m_data += sizeof(struct sctp_chunkhdr);
1976 op_err->m_len = op_err->m_pkthdr.len = sizeof(struct sctp_stale_cookie_msg);
1977 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
1978 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
1979 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
1980 (sizeof(u_int32_t))));
1981 /* seconds to usec */
1982 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
1985 tim = now.tv_usec - cookie->time_entered.tv_usec;
1986 scm->time_usec = htonl(tim);
1987 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag);
1991 * Now we must see with the lookup address if we have an existing
1992 * asoc. This will only happen if we were in the COOKIE-WAIT state
1993 * and a INIT collided with us and somewhere the peer sent the
1994 * cookie on another address besides the single address our assoc
1995 * had for him. In this case we will have one of the tie-tags set
1996 * at least AND the address field in the cookie can be used to
2000 if (cookie->addr_type == SCTP_IPV6_ADDRESS) {
2001 memset(&sin6, 0, sizeof(sin6));
2002 sin6.sin6_family = AF_INET6;
2003 sin6.sin6_len = sizeof(sin6);
2004 sin6.sin6_port = sh->src_port;
2005 sin6.sin6_scope_id = cookie->scope_id;
2006 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2007 sizeof(sin6.sin6_addr.s6_addr));
2008 to = (struct sockaddr *)&sin6;
2009 } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) {
2010 memset(&sin, 0, sizeof(sin));
2011 sin.sin_family = AF_INET;
2012 sin.sin_len = sizeof(sin);
2013 sin.sin_port = sh->src_port;
2014 sin.sin_addr.s_addr = cookie->address[0];
2015 to = (struct sockaddr *)&sin;
2018 if ((*stcb == NULL) && to) {
2019 /* Yep, lets check */
2020 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
2021 if (*stcb == NULL) {
2022 /* We should have only got back the same inp. If we
2023 * got back a different ep we have a problem. The original
2024 * findep got back l_inp and now
2026 if (l_inp != *inp_p) {
2027 printf("Bad problem find_ep got a diff inp then special_locate?\n");
2032 cookie_len -= SCTP_SIGNATURE_SIZE;
2033 if (*stcb == NULL) {
2034 /* this is the "normal" case... get a new TCB */
2036 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2037 printf("sctp_handle_cookie: processing NEW cookie\n");
2040 *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
2041 cookie_len, *inp_p, netp, to, ¬ification);
2042 /* now always decrement, since this is the normal
2043 * case.. we had no tcb when we entered.
2046 /* this is abnormal... cookie-echo on existing TCB */
2048 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2049 printf("sctp_handle_cookie: processing EXISTING cookie\n");
2052 had_a_existing_tcb = 1;
2053 *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
2054 cookie, cookie_len, *inp_p, *stcb, *netp, to, ¬ification);
2057 if (*stcb == NULL) {
2058 /* still no TCB... must be bad cookie-echo */
2060 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2061 printf("handle_cookie_echo: ACK! don't have a TCB!\n");
2063 #endif /* SCTP_DEBUG */
2068 * Ok, we built an association so confirm the address
2069 * we sent the INIT-ACK to.
2071 netl = sctp_findnet(*stcb, to);
2072 /* This code should in theory NOT run but
2077 printf("TSNH! Huh, why do I need to add this address here?\n");
2079 ret = sctp_add_remote_addr(*stcb, to, 0, 100);
2080 netl = sctp_findnet(*stcb, to);
2083 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2084 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2085 sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2087 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2088 (*stcb), 0, (void *)netl);
2093 printf("Could not add source address for some reason\n");
2097 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2098 if (!had_a_existing_tcb ||
2099 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2101 * If we have a NEW cookie or the connect never reached
2102 * the connected state during collision we must do the
2105 struct socket *so, *oso;
2106 struct sctp_inpcb *inp;
2107 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2109 * For a restart we will keep the same socket,
2110 * no need to do anything. I THINK!!
2112 sctp_ulp_notify(notification, *stcb, 0, NULL);
2115 oso = (*inp_p)->sctp_socket;
2116 SCTP_TCB_UNLOCK((*stcb));
2117 so = sonewconn(oso, SS_ISCONNECTED);
2118 SCTP_INP_WLOCK((*stcb)->sctp_ep);
2119 SCTP_TCB_LOCK((*stcb));
2120 SCTP_INP_WUNLOCK((*stcb)->sctp_ep);
2122 struct mbuf *op_err;
2123 /* Too many sockets */
2125 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
2126 printf("process_cookie_new: no room for another socket!\n");
2128 #endif /* SCTP_DEBUG */
2129 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2130 sctp_abort_association(*inp_p, NULL, m, iphlen,
2132 sctp_free_assoc(*inp_p, *stcb);
2135 inp = (struct sctp_inpcb *)so->so_pcb;
2136 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2137 SCTP_PCB_FLAGS_CONNECTED |
2138 SCTP_PCB_FLAGS_IN_TCPPOOL |
2139 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2140 SCTP_PCB_FLAGS_DONT_WAKE);
2141 inp->sctp_socket = so;
2144 * Now we must move it from one hash table to another
2145 * and get the tcb in the right place.
2147 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2149 /* Switch over to the new guy */
2152 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp,
2155 sctp_ulp_notify(notification, *stcb, 0, NULL);
2159 if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2160 sctp_ulp_notify(notification, *stcb, 0, NULL);
2166 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp,
2167 struct sctp_tcb *stcb, struct sctp_nets *net)
2169 /* cp must not be used, others call this without a c-ack :-) */
2170 struct sctp_association *asoc;
2173 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2174 printf("sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2182 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, stcb, net);
2184 /* process according to association state */
2185 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2186 /* state change only needed when I am in right state */
2188 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2189 printf("moving to OPEN state\n");
2192 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2193 asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
2195 asoc->state = SCTP_STATE_OPEN;
2199 if (asoc->overall_error_count == 0) {
2200 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2201 &asoc->time_entered);
2203 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2204 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL);
2205 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2206 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2207 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2208 soisconnected(stcb->sctp_ep->sctp_socket);
2210 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2212 /* since we did not send a HB make sure we don't double things */
2213 net->hb_responded = 1;
2215 if (stcb->asoc.sctp_autoclose_ticks &&
2216 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
2217 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2218 stcb->sctp_ep, stcb, NULL);
2222 * set ASCONF timer if ASCONFs are pending and allowed
2223 * (eg. addresses changed when init/cookie echo in flight)
2225 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) &&
2226 (stcb->asoc.peer_supports_asconf) &&
2227 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2228 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2229 stcb->sctp_ep, stcb,
2230 stcb->asoc.primary_destination);
2234 /* Toss the cookie if I can */
2235 sctp_toss_old_cookies(asoc);
2236 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2237 /* Restart the timer if we have pending data */
2238 struct sctp_tmit_chunk *chk;
2239 chk = TAILQ_FIRST(&asoc->sent_queue);
2241 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2249 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2250 struct sctp_tcb *stcb)
2252 struct sctp_nets *net;
2253 struct sctp_tmit_chunk *lchk;
2255 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) {
2258 sctp_pegs[SCTP_ECNE_RCVD]++;
2259 tsn = ntohl(cp->tsn);
2260 /* ECN Nonce stuff: need a resync and disable the nonce sum check */
2261 /* Also we make sure we disable the nonce_wait */
2262 lchk = TAILQ_FIRST(&stcb->asoc.send_queue);
2264 stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
2266 stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq;
2268 stcb->asoc.nonce_wait_for_ecne = 0;
2269 stcb->asoc.nonce_sum_check = 0;
2271 /* Find where it was sent, if possible */
2273 lchk = TAILQ_FIRST(&stcb->asoc.sent_queue);
2275 if (lchk->rec.data.TSN_seq == tsn) {
2279 if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ))
2281 lchk = TAILQ_NEXT(lchk, sctp_next);
2284 /* default is we use the primary */
2285 net = stcb->asoc.primary_destination;
2287 if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) {
2288 #ifdef SCTP_CWND_LOGGING
2291 #ifdef SCTP_CWND_LOGGING
2292 old_cwnd = net->cwnd;
2294 sctp_pegs[SCTP_CWR_PERFO]++;
2295 net->ssthresh = net->cwnd / 2;
2296 if (net->ssthresh < net->mtu) {
2297 net->ssthresh = net->mtu;
2298 /* here back off the timer as well, to slow us down */
2301 net->cwnd = net->ssthresh;
2302 #ifdef SCTP_CWND_LOGGING
2303 sctp_log_cwnd(net, (net->cwnd-old_cwnd), SCTP_CWND_LOG_FROM_SAT);
2305 /* we reduce once every RTT. So we will only lower
2306 * cwnd at the next sending seq i.e. the resync_tsn.
2308 stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn;
2311 * We always send a CWR this way if our previous one was lost
2312 * our peer will get an update, or if it is not time again
2313 * to reduce we still get the cwr to the peer.
2315 sctp_send_cwr(stcb, net, tsn);
2319 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb)
2321 /* Here we get a CWR from the peer. We must look in
2322 * the outqueue and make sure that we have a covered
2323 * ECNE in teh control chunk part. If so remove it.
2325 struct sctp_tmit_chunk *chk;
2326 struct sctp_ecne_chunk *ecne;
2328 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
2329 if (chk->rec.chunk_id != SCTP_ECN_ECHO) {
2332 /* Look for and remove if it is the right TSN. Since
2333 * there is only ONE ECNE on the control queue at
2334 * any one time we don't need to worry about more than
2337 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
2338 if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn),
2339 MAX_TSN) || (cp->tsn == ecne->tsn)) {
2340 /* this covers this ECNE, we can remove it */
2341 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
2344 sctp_m_freem(chk->data);
2347 stcb->asoc.ctrl_queue_cnt--;
2348 sctp_free_remote_addr(chk->whoTo);
2349 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
2350 sctppcbinfo.ipi_count_chunk--;
2351 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
2352 panic("Chunk count is negative");
2354 sctppcbinfo.ipi_gencnt_chunk++;
2361 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp,
2362 struct sctp_tcb *stcb, struct sctp_nets *net)
2364 struct sctp_association *asoc;
2367 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2368 printf("sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
2375 /* process according to association state */
2376 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
2377 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
2380 /* notify upper layer protocol */
2381 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL);
2382 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2383 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2384 stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
2385 stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
2386 stcb->sctp_ep->sctp_socket->so_snd.sb_mbcnt = 0;
2387 soisdisconnected(stcb->sctp_ep->sctp_socket);
2389 /* are the queues empty? they should be */
2390 if (!TAILQ_EMPTY(&asoc->send_queue) ||
2391 !TAILQ_EMPTY(&asoc->sent_queue) ||
2392 !TAILQ_EMPTY(&asoc->out_wheel)) {
2393 sctp_report_all_outbound(stcb);
2395 /* stop the timer */
2396 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
2398 sctp_free_assoc(stcb->sctp_ep, stcb);
2403 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
2404 struct sctp_nets *net, u_int8_t flg)
2406 switch (desc->chunk_type) {
2408 /* find the tsn to resend (possibly */
2411 struct sctp_tmit_chunk *tp1;
2412 tsn = ntohl(desc->tsn_ifany);
2413 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2415 if (tp1->rec.data.TSN_seq == tsn) {
2419 if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn,
2425 tp1 = TAILQ_NEXT(tp1, sctp_next);
2428 /* Do it the other way */
2429 sctp_pegs[SCTP_PDRP_DNFND]++;
2430 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2432 if (tp1->rec.data.TSN_seq == tsn) {
2436 tp1 = TAILQ_NEXT(tp1, sctp_next);
2440 sctp_pegs[SCTP_PDRP_TSNNF]++;
2442 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
2444 if (((tp1->rec.data.state_flags & SCTP_WINDOW_PROBE) == SCTP_WINDOW_PROBE) &&
2445 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
2446 sctp_pegs[SCTP_PDRP_DIWNP]++;
2449 if (stcb->asoc.peers_rwnd == 0 &&
2450 (flg & SCTP_FROM_MIDDLE_BOX)) {
2451 sctp_pegs[SCTP_PDRP_DIZRW]++;
2454 ddp = (u_int8_t *)(mtod(tp1->data, caddr_t) +
2455 sizeof(struct sctp_data_chunk));
2458 for (iii = 0; iii < sizeof(desc->data_bytes);
2460 if (ddp[iii] != desc->data_bytes[iii]) {
2461 sctp_pegs[SCTP_PDRP_BADD]++;
2466 if (tp1->sent != SCTP_DATAGRAM_RESEND) {
2467 stcb->asoc.sent_queue_retran_cnt++;
2469 /* We zero out the nonce so resync not needed */
2470 tp1->rec.data.ect_nonce = 0;
2474 * this guy had a RTO calculation pending on it,
2477 tp1->whoTo->rto_pending = 0;
2480 sctp_pegs[SCTP_PDRP_MARK]++;
2481 tp1->sent = SCTP_DATAGRAM_RESEND;
2483 * mark it as if we were doing a FR, since we
2484 * will be getting gap ack reports behind the
2485 * info from the router.
2487 tp1->rec.data.doing_fast_retransmit = 1;
2489 * mark the tsn with what sequences can cause a new FR.
2491 if (TAILQ_EMPTY(&stcb->asoc.send_queue) ) {
2492 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
2494 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
2497 /* restart the timer */
2498 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2500 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2503 /* fix counts and things */
2504 tp1->whoTo->flight_size -= tp1->send_size;
2505 if (tp1->whoTo->flight_size < 0) {
2506 tp1->whoTo->flight_size = 0;
2508 stcb->asoc.total_flight -= tp1->book_size;
2509 if (stcb->asoc.total_flight < 0) {
2510 stcb->asoc.total_flight = 0;
2512 stcb->asoc.total_flight_count--;
2513 if (stcb->asoc.total_flight_count < 0) {
2514 stcb->asoc.total_flight_count = 0;
2522 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
2523 if (tp1->sent == SCTP_DATAGRAM_RESEND)
2526 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
2528 if (tp1->sent == SCTP_DATAGRAM_RESEND)
2531 if (audit != stcb->asoc.sent_queue_retran_cnt) {
2532 printf("**Local Audit finds cnt:%d asoc cnt:%d\n",
2533 audit, stcb->asoc.sent_queue_retran_cnt);
2534 #ifndef SCTP_AUDITING_ENABLED
2535 stcb->asoc.sent_queue_retran_cnt = audit;
2543 struct sctp_tmit_chunk *asconf;
2544 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
2546 if (asconf->rec.chunk_id == SCTP_ASCONF) {
2551 if (asconf->sent != SCTP_DATAGRAM_RESEND)
2552 stcb->asoc.sent_queue_retran_cnt++;
2553 asconf->sent = SCTP_DATAGRAM_RESEND;
2554 asconf->snd_count--;
2558 case SCTP_INITIATION:
2559 /* resend the INIT */
2560 stcb->asoc.dropped_special_cnt++;
2561 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
2563 * If we can get it in, in a few attempts we do this,
2564 * otherwise we let the timer fire.
2566 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
2568 sctp_send_initiate(stcb->sctp_ep, stcb);
2571 case SCTP_SELECTIVE_ACK:
2572 /* resend the sack */
2573 sctp_send_sack(stcb);
2575 case SCTP_HEARTBEAT_REQUEST:
2576 /* resend a demand HB */
2577 sctp_send_hb(stcb, 1, net);
2581 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2582 printf("%s:%d sends a shutdown\n",
2588 sctp_send_shutdown(stcb, net);
2590 case SCTP_SHUTDOWN_ACK:
2591 sctp_send_shutdown_ack(stcb, net);
2593 case SCTP_COOKIE_ECHO:
2595 struct sctp_tmit_chunk *cookie;
2597 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
2599 if (cookie->rec.chunk_id == SCTP_COOKIE_ECHO) {
2604 if (cookie->sent != SCTP_DATAGRAM_RESEND)
2605 stcb->asoc.sent_queue_retran_cnt++;
2606 cookie->sent = SCTP_DATAGRAM_RESEND;
2607 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, stcb, net);
2611 case SCTP_COOKIE_ACK:
2612 sctp_send_cookie_ack(stcb);
2614 case SCTP_ASCONF_ACK:
2615 /* resend last asconf ack */
2616 sctp_send_asconf_ack(stcb, 1);
2618 case SCTP_FORWARD_CUM_TSN:
2619 send_forward_tsn(stcb, &stcb->asoc);
2621 /* can't do anything with these */
2622 case SCTP_PACKET_DROPPED:
2623 case SCTP_INITIATION_ACK: /* this should not happen */
2624 case SCTP_HEARTBEAT_ACK:
2625 case SCTP_ABORT_ASSOCIATION:
2626 case SCTP_OPERATION_ERROR:
2627 case SCTP_SHUTDOWN_COMPLETE:
2637 sctp_reset_in_stream(struct sctp_tcb *stcb,
2638 struct sctp_stream_reset_response *resp, int number_entries)
2641 uint16_t *list, temp;
2643 /* We set things to 0xffff since this is the last delivered
2644 * sequence and we will be sending in 0 after the reset.
2647 if (resp->reset_flags & SCTP_RESET_PERFORMED) {
2648 if (number_entries) {
2649 list = resp->list_of_streams;
2650 for (i = 0; i < number_entries; i++) {
2651 temp = ntohs(list[i]);
2653 if (list[i] >= stcb->asoc.streamincnt) {
2654 printf("Invalid stream in-stream reset %d\n", list[i]);
2657 stcb->asoc.strmin[(list[i])].last_sequence_delivered = 0xffff;
2661 for (i = 0; i < stcb->asoc.streamincnt; i++) {
2662 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
2665 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list);
2670 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
2672 struct sctp_tmit_chunk *chk, *nchk;
2673 struct sctp_association *asoc;
2677 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
2679 nchk = TAILQ_NEXT(chk, sctp_next);
2680 if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
2681 struct sctp_stream_reset_req *strreq;
2682 strreq = mtod(chk->data, struct sctp_stream_reset_req *);
2683 if (strreq->sr_req.ph.param_type == ntohs(SCTP_STR_RESET_RESPONSE)) {
2684 /* we only clean up the request */
2686 } else if (strreq->sr_req.ph.param_type != ntohs(SCTP_STR_RESET_REQUEST)) {
2687 printf("TSNH, an unknown stream reset request is in queue %x\n",
2688 (u_int)ntohs(strreq->sr_req.ph.param_type));
2691 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
2692 TAILQ_REMOVE(&asoc->control_send_queue,
2696 sctp_m_freem(chk->data);
2699 asoc->ctrl_queue_cnt--;
2700 sctp_free_remote_addr(chk->whoTo);
2701 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
2702 sctppcbinfo.ipi_count_chunk--;
2703 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
2704 panic("Chunk count is negative");
2706 sctppcbinfo.ipi_gencnt_chunk++;
2707 /* we can only have one of these so we break */
2715 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
2716 struct sctp_stream_reset_response *resp)
2719 int number_entries, param_length;
2721 param_length = ntohs(resp->ph.param_length);
2722 seq = ntohl(resp->reset_req_seq_resp);
2723 if (seq == stcb->asoc.str_reset_seq_out) {
2724 sctp_clean_up_stream_reset(stcb);
2725 stcb->asoc.str_reset_seq_out++;
2726 stcb->asoc.stream_reset_outstanding = 0;
2727 tsn = ntohl(resp->reset_at_tsn);
2728 number_entries = (param_length - sizeof(struct sctp_stream_reset_response))/sizeof(uint16_t);
2730 if ((tsn == stcb->asoc.cumulative_tsn) ||
2731 (compare_with_wrap(stcb->asoc.cumulative_tsn, tsn, MAX_TSN))) {
2732 /* no problem we are good to go */
2733 sctp_reset_in_stream(stcb, resp, number_entries);
2735 /* So, we have a stream reset but there
2736 * is pending data. We need to copy
2737 * out the stream_reset and then queue
2738 * any data = or > resp->reset_at_tsn
2740 if (stcb->asoc.pending_reply != NULL) {
2742 * This IS WRONG. We need
2743 * to queue each of these up
2744 * and only release the chunks
2745 * for each reset that the cum-ack
2746 * goes by. This is a short cut.
2748 FREE(stcb->asoc.pending_reply, M_PCB);
2750 MALLOC(stcb->asoc.pending_reply, struct sctp_stream_reset_response *, param_length,
2752 memcpy(stcb->asoc.pending_reply, resp, param_length);
2758 printf("Duplicate old stream reset resp next:%x this one:%x\n",
2759 stcb->asoc.str_reset_seq_out, seq);
2766 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct sctp_stream_reset_req *sr_req)
2768 int chk_length, param_len;
2769 struct sctp_paramhdr *ph;
2770 /* now it may be a reset or a reset-response */
2771 struct sctp_stream_reset_request *req;
2772 struct sctp_stream_reset_response *resp;
2773 chk_length = ntohs(sr_req->ch.chunk_length);
2775 ph = (struct sctp_paramhdr *)&sr_req->sr_req;
2776 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_request)) {
2777 param_len = ntohs(ph->param_length);
2778 if (ntohs(ph->param_type) == SCTP_STR_RESET_REQUEST) {
2779 /* this will send the ACK and do the reset if needed */
2780 req = (struct sctp_stream_reset_request *)ph;
2781 sctp_send_str_reset_ack(stcb, req);
2782 } else if (ntohs(ph->param_type) == SCTP_STR_RESET_RESPONSE) {
2783 /* Now here is a tricky one. We reset our receive side
2784 * of the streams. But what happens if the peers
2785 * next sending TSN is NOT equal to 1 minus our cumack?
2786 * And if his cumack is not equal to our next one out - 1
2787 * we have another problem if this is receprical.
2789 resp = (struct sctp_stream_reset_response *)ph;
2790 sctp_handle_stream_reset_response(stcb, resp);
2792 ph = (struct sctp_paramhdr *)((caddr_t)ph + SCTP_SIZE32(param_len));
2793 chk_length -= SCTP_SIZE32(param_len);
2798 * Handle a router or endpoints report of a packet loss, there
2799 * are two ways to handle this, either we get the whole packet
2800 * and must disect it ourselves (possibly with truncation and
2801 * or corruption) or it is a summary from a middle box that did
2802 * the disectting for us.
2805 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
2806 struct sctp_tcb *stcb, struct sctp_nets *net)
2808 u_int32_t bottle_bw, on_queue;
2809 u_int16_t trunc_len;
2812 struct sctp_chunk_desc desc;
2813 struct sctp_chunkhdr *ch;
2815 chlen = ntohs(cp->ch.chunk_length);
2816 chlen -= sizeof(struct sctp_pktdrop_chunk);
2817 /* XXX possible chlen underflow */
2820 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
2821 sctp_pegs[SCTP_PDRP_BWRPT]++;
2823 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
2824 chlen -= sizeof(struct sctphdr);
2825 /* XXX possible chlen underflow */
2826 memset(&desc, 0, sizeof(desc));
2829 /* first update a rwnd possibly */
2830 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
2831 /* From a peer, we get a rwnd report */
2834 sctp_pegs[SCTP_PDRP_FEHOS]++;
2836 bottle_bw = ntohl(cp->bottle_bw);
2837 on_queue = ntohl(cp->current_onq);
2838 if (bottle_bw && on_queue) {
2839 /* a rwnd report is in here */
2840 if (bottle_bw > on_queue)
2841 a_rwnd = bottle_bw - on_queue;
2846 stcb->asoc.peers_rwnd = 0;
2848 if (a_rwnd > stcb->asoc.total_flight) {
2849 stcb->asoc.peers_rwnd =
2850 a_rwnd - stcb->asoc.total_flight;
2852 stcb->asoc.peers_rwnd = 0;
2854 if (stcb->asoc.peers_rwnd <
2855 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
2856 /* SWS sender side engages */
2857 stcb->asoc.peers_rwnd = 0;
2862 sctp_pegs[SCTP_PDRP_FMBOX]++;
2864 trunc_len = (u_int16_t)ntohs(cp->trunc_len);
2865 /* now the chunks themselves */
2866 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
2867 desc.chunk_type = ch->chunk_type;
2868 /* get amount we need to move */
2869 at = ntohs(ch->chunk_length);
2870 if (at < sizeof(struct sctp_chunkhdr)) {
2871 /* corrupt chunk, maybe at the end? */
2872 sctp_pegs[SCTP_PDRP_CRUPT]++;
2875 if (trunc_len == 0) {
2876 /* we are supposed to have all of it */
2878 /* corrupt skip it */
2879 sctp_pegs[SCTP_PDRP_CRUPT]++;
2883 /* is there enough of it left ? */
2884 if (desc.chunk_type == SCTP_DATA) {
2885 if (chlen < (sizeof(struct sctp_data_chunk) +
2886 sizeof(desc.data_bytes))) {
2890 if (chlen < sizeof(struct sctp_chunkhdr)) {
2895 if (desc.chunk_type == SCTP_DATA) {
2896 /* can we get out the tsn? */
2897 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
2898 sctp_pegs[SCTP_PDRP_MB_DA]++;
2900 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(u_int32_t)) ) {
2902 struct sctp_data_chunk *dcp;
2905 dcp = (struct sctp_data_chunk *)ch;
2906 ddp = (u_int8_t *)(dcp + 1);
2907 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
2908 desc.data_bytes[iii] = ddp[iii];
2910 desc.tsn_ifany = dcp->dp.tsn;
2912 /* nope we are done. */
2913 sctp_pegs[SCTP_PDRP_NEDAT]++;
2917 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
2918 sctp_pegs[SCTP_PDRP_MB_CT]++;
2921 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
2922 sctp_pegs[SCTP_PDRP_PDBRK]++;
2925 if (SCTP_SIZE32(at) > chlen) {
2928 chlen -= SCTP_SIZE32(at);
2929 if (chlen < sizeof(struct sctp_chunkhdr)) {
2930 /* done, none left */
2933 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
2936 /* now middle boxes in sat networks get a cwnd bump */
2937 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
2938 (stcb->asoc.sat_t3_loss_recovery == 0) &&
2939 (stcb->asoc.sat_network)) {
2941 * This is debateable but for sat networks it makes sense
2942 * Note if a T3 timer has went off, we will prohibit any
2943 * changes to cwnd until we exit the t3 loss recovery.
2947 #ifdef SCTP_CWND_LOGGING
2948 int old_cwnd=net->cwnd;
2950 /* need real RTT for this calc */
2951 rtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2952 /* get bottle neck bw */
2953 bottle_bw = ntohl(cp->bottle_bw);
2954 /* and whats on queue */
2955 on_queue = ntohl(cp->current_onq);
2957 * adjust the on-queue if our flight is more it could be
2958 * that the router has not yet gotten data "in-flight" to it
2960 if (on_queue < net->flight_size)
2961 on_queue = net->flight_size;
2963 /* calculate the available space */
2964 bw_avail = (bottle_bw*rtt)/1000;
2965 if (bw_avail > bottle_bw) {
2967 * Cap the growth to no more than the bottle neck.
2968 * This can happen as RTT slides up due to queues.
2969 * It also means if you have more than a 1 second
2970 * RTT with a empty queue you will be limited to
2971 * the bottle_bw per second no matter if
2972 * other points have 1/2 the RTT and you could
2975 bw_avail = bottle_bw;
2978 if (on_queue > bw_avail) {
2980 * No room for anything else don't allow anything
2981 * else to be "added to the fire".
2983 int seg_inflight, seg_onqueue, my_portion;
2984 net->partial_bytes_acked = 0;
2986 /* how much are we over queue size? */
2987 incr = on_queue - bw_avail;
2988 if (stcb->asoc.seen_a_sack_this_pkt) {
2989 /* undo any cwnd adjustment that
2990 * the sack might have made
2992 net->cwnd = net->prev_cwnd;
2995 /* Now how much of that is mine? */
2996 seg_inflight = net->flight_size / net->mtu;
2997 seg_onqueue = on_queue / net->mtu;
2998 my_portion = (incr * seg_inflight)/seg_onqueue;
3000 /* Have I made an adjustment already */
3001 if (net->cwnd > net->flight_size) {
3002 /* for this flight I made an adjustment
3003 * we need to decrease the portion by a share
3004 * our previous adjustment.
3007 diff_adj = net->cwnd - net->flight_size;
3008 if (diff_adj > my_portion)
3011 my_portion -= diff_adj;
3014 /* back down to the previous cwnd (assume
3015 * we have had a sack before this packet). minus
3016 * what ever portion of the overage is my fault.
3018 net->cwnd -= my_portion;
3020 /* we will NOT back down more than 1 MTU */
3021 if (net->cwnd <= net->mtu) {
3022 net->cwnd = net->mtu;
3025 net->ssthresh = net->cwnd - 1;
3028 * Take 1/4 of the space left or
3029 * max burst up .. whichever is less.
3031 incr = min((bw_avail - on_queue) >> 2,
3032 (int)stcb->asoc.max_burst * (int)net->mtu);
3035 if (net->cwnd > bw_avail) {
3036 /* We can't exceed the pipe size */
3037 net->cwnd = bw_avail;
3039 if (net->cwnd < net->mtu) {
3040 /* We always have 1 MTU */
3041 net->cwnd = net->mtu;
3043 #ifdef SCTP_CWND_LOGGING
3044 if (net->cwnd - old_cwnd != 0) {
3045 /* log only changes */
3046 sctp_log_cwnd(net, (net->cwnd - old_cwnd),
3047 SCTP_CWND_LOG_FROM_SAT);
3053 extern int sctp_strict_init;
3056 * handles all control chunks in a packet
3058 * - m: mbuf chain, assumed to still contain IP/SCTP header
3059 * - stcb: is the tcb found for this packet
3060 * - offset: offset into the mbuf chain to first chunkhdr
3061 * - length: is the length of the complete packet
3063 * - length: modified to remaining length after control processing
3064 * - netp: modified to new sctp_nets after cookie-echo processing
3065 * - return NULL to discard the packet (ie. no asoc, bad packet,...)
3066 * otherwise return the tcb for this packet
3068 static struct sctp_tcb *
3069 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
3070 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
3071 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen)
3073 struct sctp_association *asoc;
3075 int num_chunks = 0; /* number of control chunks processed */
3080 * How big should this be, and should it be alloc'd?
3081 * Lets try the d-mtu-ceiling for now (2k) and that should
3082 * hopefully work ... until we get into jumbo grams and such..
3084 u_int8_t chunk_buf[DEFAULT_CHUNK_BUFFER];
3085 struct sctp_tcb *locked_tcb = stcb;
3088 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3089 printf("sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
3090 iphlen, *offset, length, stcb);
3092 #endif /* SCTP_DEBUG */
3094 /* validate chunk header length... */
3095 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
3100 * validate the verification tag
3103 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3104 printf("sctp_process_control: validating vtags\n");
3106 #endif /* SCTP_DEBUG */
3107 vtag_in = ntohl(sh->v_tag);
3108 if (ch->chunk_type == SCTP_INITIATION) {
3110 /* protocol error- silently discard... */
3112 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3113 printf("sctp_process_control: INIT with vtag != 0\n");
3115 #endif /* SCTP_DEBUG */
3116 sctp_pegs[SCTP_BAD_VTAGS]++;
3118 SCTP_TCB_UNLOCK(locked_tcb);
3121 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
3123 * first check if it's an ASCONF with an unknown src addr
3124 * we need to look inside to find the association
3126 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
3127 stcb = sctp_findassociation_ep_asconf(m, iphlen,
3128 *offset, sh, &inp, netp);
3131 /* no association, so it's out of the blue... */
3132 sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL);
3134 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3135 printf("sctp_process_control: handling OOTB packet, chunk type=%xh\n",
3138 #endif /* SCTP_DEBUG */
3141 SCTP_TCB_UNLOCK(locked_tcb);
3145 /* ABORT and SHUTDOWN can use either v_tag... */
3146 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
3147 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
3148 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
3149 if ((vtag_in == asoc->my_vtag) ||
3150 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
3151 (vtag_in == asoc->peer_vtag))) {
3154 /* drop this packet... */
3155 sctp_pegs[SCTP_BAD_VTAGS]++;
3157 SCTP_TCB_UNLOCK(locked_tcb);
3160 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
3161 if (vtag_in != asoc->my_vtag) {
3163 * this could be a stale SHUTDOWN-ACK or the
3164 * peer never got the SHUTDOWN-COMPLETE and
3165 * is still hung; we have started a new asoc
3166 * but it won't complete until the shutdown is
3170 SCTP_TCB_UNLOCK(locked_tcb);
3171 sctp_handle_ootb(m, iphlen, *offset, sh, inp,
3176 /* for all other chunks, vtag must match */
3178 if (vtag_in != asoc->my_vtag) {
3179 /* invalid vtag... */
3181 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3182 printf("invalid vtag: %xh, expect %xh\n", vtag_in, asoc->my_vtag);
3184 #endif /* SCTP_DEBUG */
3185 sctp_pegs[SCTP_BAD_VTAGS]++;
3187 SCTP_TCB_UNLOCK(locked_tcb);
3192 } /* end if !SCTP_COOKIE_ECHO */
3195 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3196 printf("sctp_process_control: vtags ok, processing ctrl chunks\n");
3198 #endif /* SCTP_DEBUG */
3201 * process all control chunks...
3203 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
3204 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
3205 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
3206 /* implied cookie-ack.. we must have lost the ack */
3207 stcb->asoc.overall_error_count = 0;
3208 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
3211 while (IS_SCTP_CONTROL(ch)) {
3212 /* validate chunk length */
3213 chk_length = ntohs(ch->chunk_length);
3215 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
3216 printf("sctp_process_control: processing a chunk type=%u, len=%u\n", ch->chunk_type, chk_length);
3218 #endif /* SCTP_DEBUG */
3219 if ((size_t)chk_length < sizeof(*ch) ||
3220 (*offset + chk_length) > length) {
3222 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3223 printf("sctp_process_control: chunk length invalid! *offset:%u, chk_length:%u > length:%u\n",
3224 *offset, length, chk_length);
3226 #endif /* SCTP_DEBUG */
3229 SCTP_TCB_UNLOCK(locked_tcb);
3234 * INIT-ACK only gets the init ack "header" portion only
3235 * because we don't have to process the peer's COOKIE.
3236 * All others get a complete chunk.
3238 if (ch->chunk_type == SCTP_INITIATION_ACK) {
3239 /* get an init-ack chunk */
3240 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3241 sizeof(struct sctp_init_ack), chunk_buf);
3245 SCTP_TCB_UNLOCK(locked_tcb);
3249 /* get a complete chunk... */
3250 if ((size_t)chk_length > sizeof(chunk_buf)) {
3252 struct sctp_paramhdr *phdr;
3254 MGETHDR(oper, MB_DONTWAIT, MT_HEADER);
3256 /* pre-reserve some space */
3258 sizeof(struct sctp_chunkhdr);
3260 mtod(oper, struct sctp_paramhdr *);
3262 htons(SCTP_CAUSE_OUT_OF_RESC);
3263 phdr->param_length =
3264 htons(sizeof(struct sctp_paramhdr));
3265 sctp_queue_op_err(stcb, oper);
3268 SCTP_TCB_UNLOCK(locked_tcb);
3271 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3272 chk_length, chunk_buf);
3274 printf("sctp_process_control: Can't get the all data....\n");
3277 SCTP_TCB_UNLOCK(locked_tcb);
3283 /* Save off the last place we got a control from */
3284 if ((*netp) && stcb) {
3285 stcb->asoc.last_control_chunk_from = *netp;
3287 #ifdef SCTP_AUDITING_ENABLED
3288 sctp_audit_log(0xB0, ch->chunk_type);
3290 switch (ch->chunk_type) {
3291 case SCTP_INITIATION:
3292 /* must be first and only chunk */
3294 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3295 printf("SCTP_INIT\n");
3297 #endif /* SCTP_DEBUG */
3298 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3299 /* We are not interested anymore */
3301 SCTP_TCB_UNLOCK(locked_tcb);
3302 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3303 /* finish the job now */
3304 sctp_inpcb_free(inp, 1);
3309 if ((num_chunks > 1) ||
3310 (sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) {
3313 SCTP_TCB_UNLOCK(locked_tcb);
3316 if ((stcb != NULL) &&
3317 (SCTP_GET_STATE(&stcb->asoc) ==
3318 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3319 sctp_send_shutdown_ack(stcb,
3320 stcb->asoc.primary_destination);
3323 SCTP_TCB_UNLOCK(locked_tcb);
3326 sctp_handle_init(m, iphlen, *offset, sh,
3327 (struct sctp_init_chunk *)ch, inp, stcb, *netp);
3330 SCTP_TCB_UNLOCK(locked_tcb);
3333 case SCTP_INITIATION_ACK:
3334 /* must be first and only chunk */
3336 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3337 printf("SCTP_INIT-ACK\n");
3339 #endif /* SCTP_DEBUG */
3340 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3341 /* We are not interested anymore */
3343 SCTP_TCB_UNLOCK(locked_tcb);
3346 sctp_free_assoc(inp, stcb);
3348 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3349 /* finish the job now */
3350 sctp_inpcb_free(inp, 1);
3355 if ((num_chunks > 1) ||
3356 (sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) {
3358 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3359 printf("Length is %d rounded chk_length:%d .. dropping\n",
3361 SCTP_SIZE32(chk_length));
3366 SCTP_TCB_UNLOCK(locked_tcb);
3369 ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
3370 (struct sctp_init_ack_chunk *)ch, stcb, *netp);
3372 * Special case, I must call the output routine
3373 * to get the cookie echoed
3375 if ((stcb) && ret == 0)
3376 sctp_chunk_output(stcb->sctp_ep, stcb, 2);
3379 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3380 printf("All done INIT-ACK processing\n");
3384 SCTP_TCB_UNLOCK(locked_tcb);
3387 case SCTP_SELECTIVE_ACK:
3389 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3390 printf("SCTP_SACK\n");
3392 #endif /* SCTP_DEBUG */
3393 sctp_pegs[SCTP_PEG_SACKS_SEEN]++;
3396 stcb->asoc.seen_a_sack_this_pkt = 1;
3397 sctp_handle_sack((struct sctp_sack_chunk *)ch,
3398 stcb, *netp, &abort_now);
3400 /* ABORT signal from sack processing */
3406 case SCTP_HEARTBEAT_REQUEST:
3408 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3409 printf("SCTP_HEARTBEAT\n");
3411 #endif /* SCTP_DEBUG */
3412 sctp_pegs[SCTP_HB_RECV]++;
3413 sctp_send_heartbeat_ack(stcb, m, *offset, chk_length,
3416 /* He's alive so give him credit */
3417 stcb->asoc.overall_error_count = 0;
3419 case SCTP_HEARTBEAT_ACK:
3421 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3422 printf("SCTP_HEARTBEAT-ACK\n");
3424 #endif /* SCTP_DEBUG */
3426 /* He's alive so give him credit */
3427 stcb->asoc.overall_error_count = 0;
3429 sctp_pegs[SCTP_HB_ACK_RECV]++;
3430 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
3433 case SCTP_ABORT_ASSOCIATION:
3435 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3436 printf("SCTP_ABORT\n");
3438 #endif /* SCTP_DEBUG */
3439 sctp_handle_abort((struct sctp_abort_chunk *)ch,
3446 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3447 printf("SCTP_SHUTDOWN\n");
3449 #endif /* SCTP_DEBUG */
3452 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
3453 stcb, *netp, &abort_flag);
3460 case SCTP_SHUTDOWN_ACK:
3462 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3463 printf("SCTP_SHUTDOWN-ACK\n");
3465 #endif /* SCTP_DEBUG */
3466 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
3470 case SCTP_OPERATION_ERROR:
3472 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3473 printf("SCTP_OP-ERR\n");
3475 #endif /* SCTP_DEBUG */
3476 if (sctp_handle_error(ch, stcb, *netp) < 0) {
3481 case SCTP_COOKIE_ECHO:
3483 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3484 printf("SCTP_COOKIE-ECHO stcb is %p\n", stcb);
3486 #endif /* SCTP_DEBUG */
3487 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3488 /* We are not interested anymore */
3491 sctp_free_assoc(inp, stcb);
3493 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3494 /* finish the job now */
3495 sctp_inpcb_free(inp, 1);
3501 * First are we accepting?
3502 * We do this again here since it is possible
3503 * that a previous endpoint WAS listening responded to
3504 * a INIT-ACK and then closed. We opened and bound..
3505 * and are now no longer listening.
3507 if (((inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) == 0) ||
3508 (inp->sctp_socket->so_qlimit == 0)) {
3509 sctp_abort_association(inp, stcb, m, iphlen, sh,
3513 } else if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
3514 /* we are accepting so check limits like TCP */
3515 if (inp->sctp_socket->so_qlen >
3516 inp->sctp_socket->so_qlimit) {
3519 struct sctp_paramhdr *phdr;
3521 MGETHDR(oper, MB_DONTWAIT, MT_HEADER);
3524 oper->m_pkthdr.len =
3525 sizeof(struct sctp_paramhdr);
3527 struct sctp_paramhdr *);
3529 htons(SCTP_CAUSE_OUT_OF_RESC);
3530 phdr->param_length =
3531 htons(sizeof(struct sctp_paramhdr));
3533 sctp_abort_association(inp, stcb, m,
3540 struct mbuf *ret_buf;
3541 ret_buf = sctp_handle_cookie_echo(m, iphlen,
3543 (struct sctp_cookie_echo_chunk *)ch, &inp,
3546 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3547 printf("ret_buf:%p length:%d off:%d\n",
3548 ret_buf, length, *offset);
3550 #endif /* SCTP_DEBUG */
3552 if (ret_buf == NULL) {
3554 SCTP_TCB_UNLOCK(locked_tcb);
3557 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3558 printf("GAK, null buffer\n");
3560 #endif /* SCTP_DEBUG */
3564 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
3566 * Restart the timer if we have pending
3569 struct sctp_tmit_chunk *chk;
3570 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
3572 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
3573 stcb->sctp_ep, stcb,
3579 case SCTP_COOKIE_ACK:
3581 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3582 printf("SCTP_COOKIE-ACK\n");
3584 #endif /* SCTP_DEBUG */
3586 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3587 /* We are not interested anymore */
3588 sctp_free_assoc(inp, stcb);
3592 /* He's alive so give him credit */
3593 stcb->asoc.overall_error_count = 0;
3594 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch,
3599 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3600 printf("SCTP_ECN-ECHO\n");
3602 #endif /* SCTP_DEBUG */
3603 /* He's alive so give him credit */
3604 stcb->asoc.overall_error_count = 0;
3605 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
3610 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3611 printf("SCTP_ECN-CWR\n");
3613 #endif /* SCTP_DEBUG */
3614 /* He's alive so give him credit */
3615 stcb->asoc.overall_error_count = 0;
3617 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb);
3619 case SCTP_SHUTDOWN_COMPLETE:
3621 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3622 printf("SCTP_SHUTDOWN-COMPLETE\n");
3624 #endif /* SCTP_DEBUG */
3625 /* must be first and only chunk */
3626 if ((num_chunks > 1) ||
3627 (length - *offset > SCTP_SIZE32(chk_length))) {
3630 SCTP_TCB_UNLOCK(locked_tcb);
3634 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
3641 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3642 printf("SCTP_ASCONF\n");
3644 #endif /* SCTP_DEBUG */
3645 /* He's alive so give him credit */
3646 stcb->asoc.overall_error_count = 0;
3648 sctp_handle_asconf(m, *offset,
3649 (struct sctp_asconf_chunk *)ch, stcb, *netp);
3651 case SCTP_ASCONF_ACK:
3653 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3654 printf("SCTP_ASCONF-ACK\n");
3656 #endif /* SCTP_DEBUG */
3657 /* He's alive so give him credit */
3658 stcb->asoc.overall_error_count = 0;
3660 sctp_handle_asconf_ack(m, *offset,
3661 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp);
3663 case SCTP_FORWARD_CUM_TSN:
3665 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3666 printf("SCTP_FWD-TSN\n");
3668 #endif /* SCTP_DEBUG */
3669 /* He's alive so give him credit */
3672 stcb->asoc.overall_error_count = 0;
3674 sctp_handle_forward_tsn(stcb,
3675 (struct sctp_forward_tsn_chunk *)ch, &abort_flag);
3680 stcb->asoc.overall_error_count = 0;
3685 case SCTP_STREAM_RESET:
3687 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3688 printf("SCTP_STREAM_RESET\n");
3690 #endif /* SCTP_DEBUG */
3691 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3692 chk_length, chunk_buf);
3693 if (stcb->asoc.peer_supports_strreset == 0) {
3694 /* hmm, peer should have annonced this, but
3695 * we will turn it on since he is sending us
3698 stcb->asoc.peer_supports_strreset = 1;
3700 sctp_handle_stream_reset(stcb, (struct sctp_stream_reset_req *)ch);
3702 case SCTP_PACKET_DROPPED:
3704 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3705 printf("SCTP_PACKET_DROPPED\n");
3707 #endif /* SCTP_DEBUG */
3708 /* re-get it all please */
3709 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3710 chk_length, chunk_buf);
3712 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
3718 /* it's an unknown chunk! */
3719 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
3721 struct sctp_paramhdr *phd;
3722 MGETHDR(mm, MB_DONTWAIT, MT_HEADER);
3724 phd = mtod(mm, struct sctp_paramhdr *);
3725 /* We cheat and use param type since we
3726 * did not bother to define a error
3728 * They are the same basic format with
3732 htons(SCTP_CAUSE_UNRECOG_CHUNK);
3734 htons(chk_length + sizeof(*phd));
3735 mm->m_len = sizeof(*phd);
3736 mm->m_next = sctp_m_copym(m, *offset,
3737 SCTP_SIZE32(chk_length),
3741 SCTP_SIZE32(chk_length) +
3743 sctp_queue_op_err(stcb, mm);
3748 SCTP_DEBUG_INPUT1) {
3749 printf("Gak can't copy the chunk into operr %d bytes\n",
3757 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3758 printf("Gak can't mgethdr for op-err of unrec chunk\n");
3763 if ((ch->chunk_type & 0x80) == 0) {
3764 /* discard this packet */
3767 } /* else skip this bad chunk and continue... */
3769 } /* switch (ch->chunk_type) */
3770 /* get the next chunk */
3771 *offset += SCTP_SIZE32(chk_length);
3772 if (*offset >= length) {
3773 /* no more data left in the mbuf chain */
3776 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3777 sizeof(struct sctp_chunkhdr), chunk_buf);
3780 SCTP_TCB_UNLOCK(locked_tcb);
3790 * Process the ECN bits we have something set so
3791 * we must look to see if it is ECN(0) or ECN(1) or CE
3794 sctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net,
3797 if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
3799 } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) {
3801 * we only add to the nonce sum for ECT1, ECT0
3802 * does not change the NS bit (that we have
3803 * yet to find a way to send it yet).
3806 /* ECN Nonce stuff */
3807 stcb->asoc.receiver_nonce_sum++;
3808 stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM;
3811 * Drag up the last_echo point if cumack is larger since we
3812 * don't want the point falling way behind by more than 2^^31
3813 * and then having it be incorrect.
3815 if (compare_with_wrap(stcb->asoc.cumulative_tsn,
3816 stcb->asoc.last_echo_tsn, MAX_TSN)) {
3817 stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
3819 } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) {
3821 * Drag up the last_echo point if cumack is larger since we
3822 * don't want the point falling way behind by more than 2^^31
3823 * and then having it be incorrect.
3825 if (compare_with_wrap(stcb->asoc.cumulative_tsn,
3826 stcb->asoc.last_echo_tsn, MAX_TSN)) {
3827 stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
3833 sctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net,
3834 u_int32_t high_tsn, u_int8_t ecn_bits)
3836 if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
3838 * we possibly must notify the sender that a congestion
3839 * window reduction is in order. We do this
3840 * by adding a ECNE chunk to the output chunk
3841 * queue. The incoming CWR will remove this chunk.
3843 if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn,
3845 /* Yep, we need to add a ECNE */
3846 sctp_send_ecn_echo(stcb, net, high_tsn);
3847 stcb->asoc.last_echo_tsn = high_tsn;
3853 * common input chunk processing (v4 and v6)
3856 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
3857 int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
3858 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
3862 * Control chunk processing
3865 int fwd_tsn_seen = 0, data_processed = 0;
3866 struct mbuf *m = *mm;
3869 sctp_pegs[SCTP_DATAGRAMS_RCVD]++;
3870 #ifdef SCTP_AUDITING_ENABLED
3871 sctp_audit_log(0xE0, 1);
3872 sctp_auditing(0, inp, stcb, net);
3876 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3877 printf("Ok, Common input processing called, m:%x iphlen:%d offset:%d\n",
3878 (u_int)m, iphlen, offset);
3880 #endif /* SCTP_DEBUG */
3881 if (IS_SCTP_CONTROL(ch)) {
3882 /* process the control portion of the SCTP packet */
3884 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3885 printf("Processing control\n");
3887 #endif /* SCTP_DEBUG */
3889 stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
3890 inp, stcb, &net, &fwd_tsn_seen);
3893 * no control chunks, so pre-process DATA chunks
3894 * (these checks are taken care of by control processing)
3897 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3898 printf("No control present\n");
3900 #endif /* SCTP_DEBUG */
3903 /* out of the blue DATA chunk */
3904 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL);
3907 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
3908 /* v_tag mismatch! */
3909 sctp_pegs[SCTP_BAD_VTAGS]++;
3910 SCTP_TCB_UNLOCK(stcb);
3916 * no valid TCB for this packet,
3917 * or we found it's a bad packet while processing control,
3918 * or we're done with this packet (done or skip rest of data),
3924 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3925 printf("Ok, control finished time to look for data (%d) offset:%d\n",
3928 #endif /* SCTP_DEBUG */
3930 * DATA chunk processing
3932 /* plow through the data chunks while length > offset */
3933 stcb->asoc.seen_a_sack_this_pkt = 0;
3935 if (length > offset) {
3938 * First check to make sure our state is correct.
3939 * We would not get here unless we really did have a
3940 * tag, so we don't abort if this happens, just
3941 * dump the chunk silently.
3943 switch (SCTP_GET_STATE(&stcb->asoc)) {
3944 case SCTP_STATE_COOKIE_ECHOED:
3946 * we consider data with valid tags in
3947 * this state shows us the cookie-ack was lost.
3948 * Imply it was there.
3950 stcb->asoc.overall_error_count = 0;
3951 sctp_handle_cookie_ack(
3952 (struct sctp_cookie_ack_chunk *)ch, stcb, net);
3954 case SCTP_STATE_COOKIE_WAIT:
3956 * We consider OOTB any data sent during asoc setup.
3958 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL);
3959 SCTP_TCB_UNLOCK(stcb);
3962 case SCTP_STATE_EMPTY: /* should not happen */
3963 case SCTP_STATE_INUSE: /* should not happen */
3964 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
3965 case SCTP_STATE_SHUTDOWN_ACK_SENT:
3968 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3969 printf("Got data in invalid state %d.. dropping\n", stcb->asoc.state);
3972 SCTP_TCB_UNLOCK(stcb);
3975 case SCTP_STATE_OPEN:
3976 case SCTP_STATE_SHUTDOWN_SENT:
3979 /* take care of ECN, part 1. */
3980 if (stcb->asoc.ecn_allowed &&
3981 (ecn_bits & (SCTP_ECT0_BIT|SCTP_ECT1_BIT)) ) {
3982 sctp_process_ecn_marked_a(stcb, net, ecn_bits);
3984 /* plow through the data chunks while length > offset */
3985 retval = sctp_process_data(mm, iphlen, &offset, length, sh,
3986 inp, stcb, net, &high_tsn);
3988 /* The association aborted, NO UNLOCK needed
3989 * since the association is destroyed.
3996 /* take care of ecn part 2. */
3997 if (stcb->asoc.ecn_allowed && (ecn_bits & (SCTP_ECT0_BIT|SCTP_ECT1_BIT)) ) {
3998 sctp_process_ecn_marked_b(stcb, net, high_tsn, ecn_bits);
4004 * Anything important needs to have been m_copy'ed in
4008 if ((data_processed == 0) && (fwd_tsn_seen)) {
4010 if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
4011 stcb->asoc.cumulative_tsn, MAX_TSN)) {
4012 /* there was a gap before this data was processed */
4015 sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
4017 /* Again, we aborted so NO UNLOCK needed */
4021 /* trigger send of any chunks in queue... */
4022 #ifdef SCTP_AUDITING_ENABLED
4023 sctp_audit_log(0xE0, 2);
4024 sctp_auditing(1, inp, stcb, net);
4027 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4028 printf("Check for chunk output prw:%d tqe:%d tf=%d\n",
4029 stcb->asoc.peers_rwnd,
4030 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
4031 stcb->asoc.total_flight);
4034 if (stcb->asoc.peers_rwnd > 0 ||
4035 !TAILQ_EMPTY(&stcb->asoc.control_send_queue) ||
4036 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)) {
4038 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4039 printf("Calling chunk OUTPUT\n");
4042 sctp_chunk_output(inp, stcb, 3);
4044 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4045 printf("chunk OUTPUT returns\n");
4050 #ifdef SCTP_AUDITING_ENABLED
4051 sctp_audit_log(0xE0, 3);
4052 sctp_auditing(2, inp, stcb, net);
4054 SCTP_TCB_UNLOCK(stcb);
4058 #if defined(__OpenBSD__)
4060 sctp_saveopt(struct sctp_inpcb *inp, struct mbuf **mp, struct ip *ip,
4063 if (inp->ip_inp.inp.inp_flags & INP_RECVDSTADDR) {
4064 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
4065 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
4067 mp = &(*mp)->m_next;
4072 extern int sctp_no_csum_on_loopback;
4074 #if defined(__FreeBSD__) || defined(__APPLE__)
4082 sctp_input(struct mbuf *m, ...)
4084 sctp_input(m, va_alist)
4093 struct sctp_inpcb *inp = NULL;
4094 struct mbuf *opts = 0;
4096 /* Don't think this is needed */
4097 /* struct ip6_recvpktopts opts6;*/
4100 u_int32_t check, calc_check;
4101 struct sctp_nets *net;
4102 struct sctp_tcb *stcb = NULL;
4103 struct sctp_chunkhdr *ch;
4104 int refcount_up = 0;
4105 int length, mlen, offset;
4106 #if defined(__OpenBSD__) && defined(IPSEC)
4107 struct inpcb *i_inp;
4109 struct tdb_ident *tdbi;
4114 #if !(defined(__FreeBSD__) || defined(__APPLE__))
4115 #ifdef __DragonFly__
4118 iphlen = __va_arg(ap, int);
4124 iphlen = va_arg(ap, int);
4131 sctp_pegs[SCTP_INPKTS]++;
4133 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4134 printf("V4 input gets a packet iphlen:%d pktlen:%d\n", iphlen, m->m_pkthdr.len);
4138 /* Don't think this is needed */
4139 /* bzero(&opts6, sizeof(opts6));*/
4143 * Strip IP options, we don't allow any in or out.
4145 if ((size_t)iphlen > sizeof(struct ip)) {
4147 iphlen = sizeof(struct ip);
4151 * Get IP, SCTP, and first chunk header together in first mbuf.
4153 ip = mtod(m, struct ip *);
4154 offset = iphlen + sizeof(*sh) + sizeof(*ch);
4155 if (m->m_len < offset) {
4156 if ((m = m_pullup(m, offset)) == 0) {
4157 sctp_pegs[SCTP_HDR_DROPS]++;
4160 ip = mtod(m, struct ip *);
4162 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
4163 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
4165 /* SCTP does not allow broadcasts or multicasts */
4166 #if defined(__NetBSD__) || defined(__OpenBSD__)
4167 if (IN_MULTICAST(ip->ip_dst.s_addr))
4169 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)))
4172 sctp_pegs[SCTP_IN_MCAST]++;
4175 if (in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
4176 sctp_pegs[SCTP_IN_MCAST]++;
4180 /* destination port of 0 is illegal, based on RFC2960. */
4181 if (sh->dest_port == 0) {
4182 sctp_pegs[SCTP_HDR_DROPS]++;
4186 /* validate SCTP checksum */
4187 if ((sctp_no_csum_on_loopback == 0) ||
4188 (m->m_pkthdr.rcvif == NULL) ||
4189 (m->m_pkthdr.rcvif->if_type != IFT_LOOP)) {
4190 /* we do NOT validate things from the loopback if the
4191 * sysctl is set to 1.
4193 check = sh->checksum; /* save incoming checksum */
4194 if ((check == 0) && (sctp_no_csum_on_loopback)) {
4195 /* special hook for where we got a local address
4196 * somehow routed across a non IFT_LOOP type interface
4198 if (ip->ip_src.s_addr == ip->ip_dst.s_addr)
4199 goto sctp_skip_csum_4;
4201 sh->checksum = 0; /* prepare for calc */
4202 calc_check = sctp_calculate_sum(m, &mlen, iphlen);
4203 if (calc_check != check) {
4205 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4206 printf("Bad CSUM on SCTP packet calc_check:%x check:%x m:%x mlen:%d iphlen:%d\n",
4207 calc_check, check, (u_int)m, mlen, iphlen);
4211 stcb = sctp_findassociation_addr(m, iphlen,
4212 offset - sizeof(*ch),
4213 sh, ch, &inp, &net);
4214 if ((inp) && (stcb)) {
4215 sctp_send_packet_dropped(stcb, net, m, iphlen,
4217 sctp_chunk_output(inp, stcb, 2);
4218 } else if ((inp != NULL) && (stcb == NULL)) {
4221 sctp_pegs[SCTP_BAD_CSUM]++;
4224 sh->checksum = calc_check;
4227 mlen = m->m_pkthdr.len;
4229 /* validate mbuf chain length with IP payload length */
4230 #if defined(__NetBSD__) || defined(__OpenBSD__)
4231 /* Open BSD gives us the len in network order, fix it */
4234 if (mlen < (ip->ip_len - iphlen)) {
4235 sctp_pegs[SCTP_HDR_DROPS]++;
4240 * Locate pcb and tcb for datagram
4241 * sctp_findassociation_addr() wants IP/SCTP/first chunk header...
4244 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4245 printf("V4 find association\n");
4249 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
4250 sh, ch, &inp, &net);
4251 /* inp's ref-count increased && stcb locked */
4253 struct sctp_init_chunk *init_chk, chunk_buf;
4255 sctp_pegs[SCTP_NOPORTS]++;
4258 * we use the bandwidth limiting to protect against
4259 * sending too many ABORTS all at once. In this case
4260 * these count the same as an ICMP message.
4262 if (badport_bandlim(0) < 0)
4264 #endif /* ICMP_BANDLIM */
4266 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4267 printf("Sending a ABORT from packet entry!\n");
4270 if (ch->chunk_type == SCTP_INITIATION) {
4271 /* we do a trick here to get the INIT tag,
4272 * dig in and get the tag from the INIT and
4273 * put it in the common header.
4275 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4276 iphlen + sizeof(*sh), sizeof(*init_chk),
4277 (u_int8_t *)&chunk_buf);
4278 if (init_chk != NULL)
4279 sh->v_tag = init_chk->init.initiate_tag;
4281 sctp_send_abort(m, iphlen, sh, 0, NULL);
4283 } else if (stcb == NULL) {
4288 * I very much doubt any of the IPSEC stuff will work but I have
4289 * no idea, so I will leave it in place.
4293 /* FIX ME: this don't work... :) */
4295 /* Find most recent IPsec tag */
4296 i_inp = &inp->ip_inp.inp;
4297 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
4300 tdbi = (struct tdb_ident *)(mtag + 1);
4301 tdb = gettdb(tdbi->spi, &tdbi->dst, tdbi->proto);
4304 ipsp_spd_lookup(m, af, iphlen, &error, IPSP_DIRECTION_IN,
4308 sctp_pegs[SCTP_HDR_DROPS]++;
4313 if (i_inp->inp_tdb_in != tdb) {
4315 tdb_add_inp(tdb, i_inp, 1);
4316 if (i_inp->inp_ipo == NULL) {
4317 i_inp->inp_ipo = ipsec_add_policy(i_inp, af,
4318 IPSP_DIRECTION_OUT);
4319 if (i_inp->inp_ipo == NULL) {
4321 sctp_pegs[SCTP_HDR_DROPS]++;
4325 if (i_inp->inp_ipo->ipo_dstid == NULL &&
4326 tdb->tdb_srcid != NULL) {
4327 i_inp->inp_ipo->ipo_dstid = tdb->tdb_srcid;
4328 tdb->tdb_srcid->ref_count++;
4330 if (i_inp->inp_ipsec_remotecred == NULL &&
4331 tdb->tdb_remote_cred != NULL) {
4332 i_inp->inp_ipsec_remotecred =
4333 tdb->tdb_remote_cred;
4334 tdb->tdb_remote_cred->ref_count++;
4336 if (i_inp->inp_ipsec_remoteauth == NULL &&
4337 tdb->tdb_remote_auth != NULL) {
4338 i_inp->inp_ipsec_remoteauth =
4339 tdb->tdb_remote_auth;
4340 tdb->tdb_remote_auth->ref_count++;
4342 } else { /* Just reset */
4343 TAILQ_REMOVE(&i_inp->inp_tdb_in->tdb_inp_in, i_inp,
4345 i_inp->inp_tdb_in = NULL;
4351 if (ipsec4_in_reject_so(m, inp->ip_inp.inp.inp_socket)) {
4352 ipsecstat.in_polvio++;
4353 sctp_pegs[SCTP_HDR_DROPS]++;
4360 * Construct sockaddr format source address.
4361 * Stuff source address and datagram in user buffer.
4363 if ((inp->ip_inp.inp.inp_flags & INP_CONTROLOPTS)
4365 || (inp->sctp_socket->so_options & SO_TIMESTAMP)
4369 sctp_saveopt(inp, &opts, ip, m);
4371 ip_savecontrol((struct inpcb *)inp, &opts, ip, m);
4376 * common chunk processing
4378 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4379 length = ip->ip_len + iphlen;
4381 length = ip->ip_len - (ip->ip_hl << 2) + iphlen;
4383 offset -= sizeof(struct sctp_chunkhdr);
4385 ecn_bits = ip->ip_tos;
4387 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
4388 inp, stcb, net, ecn_bits);
4389 /* inp's ref-count reduced && stcb unlocked */
4397 if ((inp) && (refcount_up)) {
4398 /* reduce ref-count */
4399 SCTP_INP_WLOCK(inp);
4400 SCTP_INP_DECR_REF(inp);
4401 SCTP_INP_WUNLOCK(inp);
4407 SCTP_TCB_UNLOCK(stcb);
4409 if ((inp) && (refcount_up)) {
4410 /* reduce ref-count */
4411 SCTP_INP_WLOCK(inp);
4412 SCTP_INP_DECR_REF(inp);
4413 SCTP_INP_WUNLOCK(inp);