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.1 2005/07/15 14:46:16 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__)
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>
63 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
64 #include <sys/limits.h>
66 #include <machine/limits.h>
68 #include <machine/cpu.h>
71 #include <net/route.h>
72 #include <net/if_types.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/in_pcb.h>
78 #include <netinet/in_var.h>
79 #include <netinet/ip_var.h>
82 #include <netinet/ip6.h>
83 #include <netinet6/ip6_var.h>
86 #include <netinet/ip_icmp.h>
87 #include <netinet/icmp_var.h>
88 #include <netinet/sctp_var.h>
89 #include <netinet/sctp_pcb.h>
90 #include <netinet/sctp_header.h>
91 #include <netinet/sctputil.h>
92 #include <netinet/sctp_output.h>
93 #include <netinet/sctp_input.h>
94 #include <netinet/sctp_hashdriver.h>
95 #include <netinet/sctp_indata.h>
96 #include <netinet/sctp_asconf.h>
100 #elif !defined(__FreeBSD__)
101 #include <machine/stdarg.h>
106 #include <netinet6/ipsec.h>
107 #include <netkey/key.h>
113 #include <net/net_osdep.h>
116 extern u_int32_t sctp_debug_on;
121 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
122 struct sctphdr *sh, struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
123 struct sctp_tcb *stcb, struct sctp_nets *net)
125 struct sctp_init *init;
128 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
129 printf("sctp_handle_init: handling INIT tcb:%p\n", stcb);
134 /* First are we accepting? */
135 if (((inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) == 0) ||
136 (inp->sctp_socket->so_qlimit == 0)) {
137 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
140 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
142 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
143 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
146 /* validate parameters */
147 if (init->initiate_tag == 0) {
148 /* protocol error... send abort */
149 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
150 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
153 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
154 /* invalid parameter... send abort */
155 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
156 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
159 if (init->num_inbound_streams == 0) {
160 /* protocol error... send abort */
161 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
162 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
165 if (init->num_outbound_streams == 0) {
166 /* protocol error... send abort */
167 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
168 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
172 /* send an INIT-ACK w/cookie */
174 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
175 printf("sctp_handle_init: sending INIT-ACK\n");
179 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp);
183 * process peer "INIT/INIT-ACK" chunk
184 * returns value < 0 on error
188 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb,
189 struct sctp_nets *net)
191 struct sctp_init *init;
192 struct sctp_association *asoc;
193 struct sctp_nets *lnet;
198 /* save off parameters */
199 asoc->peer_vtag = ntohl(init->initiate_tag);
200 asoc->peers_rwnd = ntohl(init->a_rwnd);
202 if (TAILQ_FIRST(&asoc->nets)) {
203 /* update any ssthresh's that may have a default */
204 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
205 lnet->ssthresh = asoc->peers_rwnd;
208 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
210 struct sctp_stream_out *outs;
211 struct sctp_tmit_chunk *chk;
213 /* cut back on number of streams */
214 newcnt = ntohs(init->num_inbound_streams);
215 /* This if is probably not needed but I am cautious */
217 /* First make sure no data chunks are trapped */
218 for (i=newcnt; i < asoc->pre_open_streams; i++) {
219 outs = &asoc->strmout[i];
220 chk = TAILQ_FIRST(&outs->outqueue);
222 TAILQ_REMOVE(&outs->outqueue, chk,
224 asoc->stream_queue_cnt--;
225 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL,
226 stcb, SCTP_NOTIFY_DATAGRAM_UNSENT,
229 sctp_m_freem(chk->data);
232 sctp_free_remote_addr(chk->whoTo);
236 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
237 sctppcbinfo.ipi_count_chunk--;
238 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
239 panic("Chunk count is negative");
241 sctppcbinfo.ipi_gencnt_chunk++;
242 chk = TAILQ_FIRST(&outs->outqueue);
246 /* cut back the count and abandon the upper streams */
247 asoc->pre_open_streams = newcnt;
249 asoc->streamincnt = ntohs(init->num_outbound_streams);
250 if (asoc->streamincnt > MAX_SCTP_STREAMS) {
251 asoc->streamincnt = MAX_SCTP_STREAMS;
254 asoc->streamoutcnt = asoc->pre_open_streams;
256 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
257 #ifdef SCTP_MAP_LOGGING
258 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
260 /* This is the next one we expect */
261 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
263 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
264 asoc->cumulative_tsn = asoc->asconf_seq_in;
265 asoc->last_echo_tsn = asoc->asconf_seq_in;
266 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
267 /* open the requested streams */
268 if (asoc->strmin != NULL) {
269 /* Free the old ones */
270 FREE(asoc->strmin, M_PCB);
272 MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
273 sizeof(struct sctp_stream_in), M_PCB, M_NOWAIT);
274 if (asoc->strmin == NULL) {
275 /* we didn't get memory for the streams! */
277 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
278 printf("process_init: couldn't get memory for the streams!\n");
283 for (i = 0; i < asoc->streamincnt; i++) {
284 asoc->strmin[i].stream_no = i;
285 asoc->strmin[i].last_sequence_delivered = 0xffff;
287 * U-stream ranges will be set when the cookie
288 * is unpacked. Or for the INIT sender they
289 * are un set (if pr-sctp not supported) when the
292 TAILQ_INIT(&asoc->strmin[i].inqueue);
294 * we are not on any wheel, pr-sctp streams
295 * will go on the wheel when they have data waiting
298 asoc->strmin[i].next_spoke.tqe_next = 0;
299 asoc->strmin[i].next_spoke.tqe_prev = 0;
303 * load_address_from_init will put the addresses into the
304 * association when the COOKIE is processed or the INIT-ACK
305 * is processed. Both types of COOKIE's existing and new
306 * call this routine. It will remove addresses that
307 * are no longer in the association (for the restarting
308 * case where addresses are removed). Up front when the
309 * INIT arrives we will discard it if it is a restart
310 * and new addresses have been added.
316 * INIT-ACK message processing/consumption
317 * returns value < 0 on error
320 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
321 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
322 struct sctp_nets *net)
324 struct sctp_association *asoc;
326 int retval, abort_flag;
327 uint32_t initack_limit;
328 /* First verify that we have no illegal param's */
332 op_err = sctp_arethere_unrecognized_parameters(m,
333 (offset+sizeof(struct sctp_init_chunk)) ,
334 &abort_flag, (struct sctp_chunkhdr *)cp);
336 /* Send an abort and notify peer */
337 if (op_err != NULL) {
338 sctp_send_operr_to(m, iphlen, op_err, cp->init.initiate_tag);
341 * Just notify (abort_assoc does this if
344 sctp_abort_notification(stcb, 0);
346 * No sense in further INIT's since
347 * we will get the same param back
349 sctp_free_assoc(stcb->sctp_ep, stcb);
354 /* process the peer's parameters in the INIT-ACK */
355 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net);
360 initack_limit = offset + ntohs(cp->ch.chunk_length);
361 /* load all addresses */
362 if (sctp_load_addresses_from_init(stcb, m, iphlen,
363 (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh,
365 /* Huh, we should abort */
366 sctp_abort_notification(stcb, 0);
367 sctp_free_assoc(stcb->sctp_ep, stcb);
371 sctp_queue_op_err(stcb, op_err);
372 /* queuing will steal away the mbuf chain to the out queue */
375 /* extract the cookie and queue it to "echo" it back... */
376 stcb->asoc.overall_error_count = 0;
377 net->error_count = 0;
378 retval = sctp_send_cookie_echo(m, offset, stcb, net);
381 * No cookie, we probably should send a op error.
382 * But in any case if there is no cookie in the INIT-ACK,
383 * we can abandon the peer, its broke.
386 /* We abort with an error of missing mandatory param */
389 sctp_generate_invmanparam(SCTP_CAUSE_MISS_PARAM);
392 * Expand beyond to include the mandatory
395 struct sctp_inv_mandatory_param *mp;
397 sizeof(struct sctp_inv_mandatory_param);
399 struct sctp_inv_mandatory_param *);
400 /* Subtract the reserved param */
402 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
403 mp->num_param = htonl(1);
404 mp->param = htons(SCTP_STATE_COOKIE);
407 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
414 * Cancel the INIT timer, We do this first before queueing
415 * the cookie. We always cancel at the primary to assue that
416 * we are canceling the timer started by the INIT which always
417 * goes to the primary.
419 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
420 asoc->primary_destination);
422 /* calculate the RTO */
423 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered);
429 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
430 struct sctp_tcb *stcb, struct sctp_nets *net)
432 struct sockaddr_storage store;
433 struct sockaddr_in *sin;
434 struct sockaddr_in6 *sin6;
435 struct sctp_nets *r_net;
438 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
443 sin = (struct sockaddr_in *)&store;
444 sin6 = (struct sockaddr_in6 *)&store;
446 memset(&store, 0, sizeof(store));
447 if (cp->heartbeat.hb_info.addr_family == AF_INET &&
448 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
449 sin->sin_family = cp->heartbeat.hb_info.addr_family;
450 sin->sin_len = cp->heartbeat.hb_info.addr_len;
451 sin->sin_port = stcb->rport;
452 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
453 sizeof(sin->sin_addr));
454 } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 &&
455 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
456 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
457 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
458 sin6->sin6_port = stcb->rport;
459 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
460 sizeof(sin6->sin6_addr));
463 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
464 printf("unsupported address family");
469 r_net = sctp_findnet(stcb, (struct sockaddr *)sin);
472 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
473 printf("Huh? I can't find the address I sent it to, discard\n");
478 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
479 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
480 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
482 * If the its a HB and it's random value is correct when
483 * can confirm the destination.
485 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
486 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
487 stcb, 0, (void *)r_net);
489 r_net->error_count = 0;
490 r_net->hb_responded = 1;
491 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
492 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
493 if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
494 r_net->dest_state = SCTP_ADDR_REACHABLE;
495 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
496 SCTP_HEARTBEAT_SUCCESS, (void *)r_net);
498 /* now was it the primary? if so restore */
499 if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
500 sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net);
503 /* Now lets do a RTO with this */
504 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv);
508 sctp_handle_abort(struct sctp_abort_chunk *cp,
509 struct sctp_tcb *stcb, struct sctp_nets *net)
511 struct sctp_inpcb *inp;
514 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
515 printf("sctp_handle_abort: handling ABORT\n");
520 /* verify that the destination addr is in the association */
521 /* ignore abort for addresses being deleted */
523 /* stop any receive timers */
524 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net);
525 /* notify user of the abort and clean up... */
526 sctp_abort_notification(stcb, 0);
529 sctp_free_assoc(stcb->sctp_ep, stcb);
531 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
532 printf("sctp_handle_abort: finished\n");
538 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
539 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
541 struct sctp_association *asoc;
542 int some_on_streamwheel;
545 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
546 printf("sctp_handle_shutdown: handling SHUTDOWN\n");
552 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
553 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
557 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
558 /* update current data status */
560 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
561 printf("Warning Shutdown NOT the expected size.. skipping (%d:%d)\n",
562 ntohs(cp->ch.chunk_length),
563 (int)sizeof(struct sctp_shutdown_chunk));
568 sctp_update_acked(stcb, cp, net, abort_flag);
571 /* goto SHUTDOWN_RECEIVED state to block new requests */
572 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
573 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
574 asoc->state = SCTP_STATE_SHUTDOWN_RECEIVED;
576 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
577 printf("Moving to SHUTDOWN-RECEIVED state\n");
580 /* notify upper layer that peer has initiated a shutdown */
581 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL);
583 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
584 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
586 /* Set the flag so we cannot send more, we
587 * would call the function but we don't want to
588 * wake up the ulp necessarily.
590 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
591 stcb->sctp_ep->sctp_socket->so_rcv.sb_state |= SBS_CANTSENDMORE;
593 stcb->sctp_ep->sctp_socket->so_state |= SS_CANTSENDMORE;
597 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
599 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
601 * stop the shutdown timer, since we WILL move
602 * to SHUTDOWN-ACK-SENT.
604 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
606 /* Now are we there yet? */
607 some_on_streamwheel = 0;
608 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
609 /* Check to see if some data queued */
610 struct sctp_stream_out *outs;
611 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
612 if (!TAILQ_EMPTY(&outs->outqueue)) {
613 some_on_streamwheel = 1;
619 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
620 printf("some_on_streamwheel:%d send_q_empty:%d sent_q_empty:%d\n",
622 !TAILQ_EMPTY(&asoc->send_queue),
623 !TAILQ_EMPTY(&asoc->sent_queue));
626 if (!TAILQ_EMPTY(&asoc->send_queue) ||
627 !TAILQ_EMPTY(&asoc->sent_queue) ||
628 some_on_streamwheel) {
629 /* By returning we will push more data out */
632 /* no outstanding data to send, so move on... */
633 /* send SHUTDOWN-ACK */
634 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
635 /* move to SHUTDOWN-ACK-SENT state */
636 asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
638 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
639 printf("moving to SHUTDOWN_ACK state\n");
642 /* start SHUTDOWN timer */
643 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
649 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp,
650 struct sctp_tcb *stcb, struct sctp_nets *net)
652 struct sctp_association *asoc;
655 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
656 printf("sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
663 /* process according to association state */
664 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
665 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
666 /* unexpected SHUTDOWN-ACK... so ignore... */
669 /* are the queues empty? */
670 if (!TAILQ_EMPTY(&asoc->send_queue) ||
671 !TAILQ_EMPTY(&asoc->sent_queue) ||
672 !TAILQ_EMPTY(&asoc->out_wheel)) {
673 sctp_report_all_outbound(stcb);
676 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
677 /* send SHUTDOWN-COMPLETE */
678 sctp_send_shutdown_complete(stcb, net);
679 /* notify upper layer protocol */
680 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL);
681 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
682 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
683 stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
684 /* Set the connected flag to disconnected */
685 stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
686 stcb->sctp_ep->sctp_socket->so_snd.sb_mbcnt = 0;
687 soisdisconnected(stcb->sctp_ep->sctp_socket);
689 /* free the TCB but first save off the ep */
690 sctp_free_assoc(stcb->sctp_ep, stcb);
694 * Skip past the param header and then we will find the chunk that
695 * caused the problem. There are two possiblities ASCONF or FWD-TSN
696 * other than that and our peer must be broken.
699 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
700 struct sctp_nets *net)
702 struct sctp_chunkhdr *chk;
704 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
705 switch (chk->chunk_type) {
706 case SCTP_ASCONF_ACK:
708 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
709 printf("Strange peer, snds ASCONF but does not recongnize asconf-ack?\n");
714 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
715 printf("Peer does not support ASCONF/ASCONF-ACK chunks\n");
717 #endif /* SCTP_DEBUG */
718 sctp_asconf_cleanup(stcb, net);
720 case SCTP_FORWARD_CUM_TSN:
721 stcb->asoc.peer_supports_prsctp = 0;
725 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
726 printf("Peer does not support chunk type %d(%x)??\n",
727 chk->chunk_type, (u_int)chk->chunk_type);
735 * Skip past the param header and then we will find the param that
736 * caused the problem. There are a number of param's in a ASCONF
737 * OR the prsctp param these will turn of specific features.
740 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
742 struct sctp_paramhdr *pbad;
745 switch (ntohs(pbad->param_type)) {
747 case SCTP_PRSCTP_SUPPORTED:
748 stcb->asoc.peer_supports_prsctp = 0;
750 case SCTP_SUPPORTED_CHUNK_EXT:
752 /* draft-ietf-tsvwg-addip-sctp */
753 case SCTP_ECN_NONCE_SUPPORTED:
754 stcb->asoc.peer_supports_ecn_nonce = 0;
755 stcb->asoc.ecn_nonce_allowed = 0;
756 stcb->asoc.ecn_allowed = 0;
758 case SCTP_ADD_IP_ADDRESS:
759 case SCTP_DEL_IP_ADDRESS:
760 stcb->asoc.peer_supports_asconf = 0;
762 case SCTP_SET_PRIM_ADDR:
763 stcb->asoc.peer_supports_asconf_setprim = 0;
765 case SCTP_SUCCESS_REPORT:
766 case SCTP_ERROR_CAUSE_IND:
768 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
769 printf("Huh, the peer does not support success? or error cause?\n");
770 printf("Turning off ASCONF to this strange peer\n");
773 stcb->asoc.peer_supports_asconf = 0;
774 stcb->asoc.peer_supports_asconf_setprim = 0;
778 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
779 printf("Peer does not support base param type %d(%x)??\n",
780 pbad->param_type, (u_int)pbad->param_type);
788 sctp_handle_error(struct sctp_chunkhdr *ch,
789 struct sctp_tcb *stcb, struct sctp_nets *net)
792 struct sctp_paramhdr *phdr;
795 struct sctp_association *asoc;
798 /* parse through all of the errors and process */
800 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
801 sizeof(struct sctp_chunkhdr));
802 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
803 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
804 /* Process an Error Cause */
805 error_type = ntohs(phdr->param_type);
806 error_len = ntohs(phdr->param_length);
807 if ((error_len > chklen) || (error_len == 0)) {
808 /* invalid param length for this param */
810 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
811 printf("Bogus length in error param- chunk left:%d errorlen:%d\n",
814 #endif /* SCTP_DEBUG */
817 switch (error_type) {
818 case SCTP_CAUSE_INV_STRM:
819 case SCTP_CAUSE_MISS_PARAM:
820 case SCTP_CAUSE_INVALID_PARAM:
821 case SCTP_CAUSE_NOUSER_DATA:
823 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
824 printf("Software error we got a %d back? We have a bug :/ (or do they?)\n",
829 case SCTP_CAUSE_STALE_COOKIE:
830 /* We only act if we have echoed a cookie and are waiting. */
831 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
833 p = (int *)((caddr_t)phdr + sizeof(*phdr));
834 /* Save the time doubled */
835 asoc->cookie_preserve_req = ntohl(*p) << 1;
836 asoc->stale_cookie_count++;
837 if (asoc->stale_cookie_count >
838 asoc->max_init_times) {
839 sctp_abort_notification(stcb, 0);
840 /* now free the asoc */
841 sctp_free_assoc(stcb->sctp_ep, stcb);
844 /* blast back to INIT state */
845 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
846 asoc->state |= SCTP_STATE_COOKIE_WAIT;
847 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
848 stcb->sctp_ep, stcb, net);
849 sctp_send_initiate(stcb->sctp_ep, stcb);
852 case SCTP_CAUSE_UNRESOLV_ADDR:
854 * Nothing we can do here, we don't do hostname
855 * addresses so if the peer does not like my IPv6 (or
856 * IPv4 for that matter) it does not matter. If they
857 * don't support that type of address, they can NOT
858 * possibly get that packet type... i.e. with no IPv6
859 * you can't recieve a IPv6 packet. so we can safely
860 * ignore this one. If we ever added support for
861 * HOSTNAME Addresses, then we would need to do
865 case SCTP_CAUSE_UNRECOG_CHUNK:
866 sctp_process_unrecog_chunk(stcb, phdr, net);
868 case SCTP_CAUSE_UNRECOG_PARAM:
869 sctp_process_unrecog_param(stcb, phdr);
871 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
873 * We ignore this since the timer will drive out a new
874 * cookie anyway and there timer will drive us to send
875 * a SHUTDOWN_COMPLETE. We can't send one here since
876 * we don't have their tag.
879 case SCTP_CAUSE_DELETEING_LAST_ADDR:
880 case SCTP_CAUSE_OPERATION_REFUSED:
881 case SCTP_CAUSE_DELETING_SRC_ADDR:
882 /* We should NOT get these here, but in a ASCONF-ACK. */
884 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
885 printf("Peer sends ASCONF errors in a Operational Error?<%d>?\n",
890 case SCTP_CAUSE_OUT_OF_RESC:
892 * And what, pray tell do we do with the fact
893 * that the peer is out of resources? Not
894 * really sure we could do anything but abort.
895 * I suspect this should have came WITH an
896 * abort instead of in a OP-ERROR.
901 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
902 /* don't know what this error cause is... */
903 printf("sctp_handle_error: unknown error type = 0x%xh\n",
906 #endif /* SCTP_DEBUG */
909 adjust = SCTP_SIZE32(error_len);
911 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
917 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
918 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
919 struct sctp_nets *net)
921 struct sctp_init_ack *init_ack;
926 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
927 printf("sctp_handle_init_ack: handling INIT-ACK\n");
932 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
933 printf("sctp_handle_init_ack: TCB is null\n");
938 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
940 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
941 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
945 init_ack = &cp->init;
946 /* validate parameters */
947 if (init_ack->initiate_tag == 0) {
948 /* protocol error... send an abort */
949 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
950 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
954 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
955 /* protocol error... send an abort */
956 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
957 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
961 if (init_ack->num_inbound_streams == 0) {
962 /* protocol error... send an abort */
963 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
964 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
968 if (init_ack->num_outbound_streams == 0) {
969 /* protocol error... send an abort */
970 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
971 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
976 /* process according to association state... */
977 state = &stcb->asoc.state;
978 switch (*state & SCTP_STATE_MASK) {
979 case SCTP_STATE_COOKIE_WAIT:
980 /* this is the expected state for this chunk */
981 /* process the INIT-ACK parameters */
982 if (stcb->asoc.primary_destination->dest_state &
983 SCTP_ADDR_UNCONFIRMED) {
985 * The primary is where we sent the INIT, we can
986 * always consider it confirmed when the INIT-ACK
987 * is returned. Do this before we load addresses
990 stcb->asoc.primary_destination->dest_state &=
991 ~SCTP_ADDR_UNCONFIRMED;
992 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
993 stcb, 0, (void *)stcb->asoc.primary_destination);
995 if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, net
997 /* error in parsing parameters */
999 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1000 printf("sctp_process_init_ack: error in msg, discarding\n");
1005 /* update our state */
1007 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1008 printf("moving to COOKIE-ECHOED state\n");
1011 if (*state & SCTP_STATE_SHUTDOWN_PENDING) {
1012 *state = SCTP_STATE_COOKIE_ECHOED |
1013 SCTP_STATE_SHUTDOWN_PENDING;
1015 *state = SCTP_STATE_COOKIE_ECHOED;
1018 /* reset the RTO calc */
1019 stcb->asoc.overall_error_count = 0;
1020 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1022 * collapse the init timer back in case of a exponential backoff
1024 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1027 * the send at the end of the inbound data processing will
1028 * cause the cookie to be sent
1031 case SCTP_STATE_SHUTDOWN_SENT:
1032 /* incorrect state... discard */
1034 case SCTP_STATE_COOKIE_ECHOED:
1035 /* incorrect state... discard */
1037 case SCTP_STATE_OPEN:
1038 /* incorrect state... discard */
1040 case SCTP_STATE_EMPTY:
1041 case SCTP_STATE_INUSE:
1043 /* incorrect state... discard */
1045 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1046 printf("Leaving handle-init-ack default\n");
1051 } /* end switch asoc state */
1053 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1054 printf("Leaving handle-init-ack end\n");
1062 * handle a state cookie for an existing association
1063 * m: input packet mbuf chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk
1064 * note: this is a "split" mbuf and the cookie signature does not exist
1065 * offset: offset into mbuf to the cookie-echo chunk
1067 static struct sctp_tcb *
1068 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1069 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1070 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
1071 struct sockaddr *init_src, int *notification)
1073 struct sctp_association *asoc;
1074 struct sctp_init_chunk *init_cp, init_buf;
1075 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1077 int init_offset, initack_offset;
1080 /* I know that the TCB is non-NULL from the caller */
1083 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1084 /* SHUTDOWN came in after sending INIT-ACK */
1085 struct mbuf *op_err;
1086 struct sctp_paramhdr *ph;
1088 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1090 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1091 printf("sctp_handle_cookie: got a cookie, while shutting down!\n");
1094 MGETHDR(op_err, M_DONTWAIT, MT_HEADER);
1095 if (op_err == NULL) {
1099 /* pre-reserve some space */
1100 op_err->m_data += sizeof(struct ip6_hdr);
1101 op_err->m_data += sizeof(struct sctphdr);
1102 op_err->m_data += sizeof(struct sctp_chunkhdr);
1104 op_err->m_len = op_err->m_pkthdr.len = sizeof(struct sctp_paramhdr);
1105 ph = mtod(op_err, struct sctp_paramhdr *);
1106 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1107 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1108 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag);
1112 * find and validate the INIT chunk in the cookie (peer's info)
1113 * the INIT should start after the cookie-echo header struct
1114 * (chunk header, state cookie header struct)
1116 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1118 init_cp = (struct sctp_init_chunk *)
1119 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1120 (u_int8_t *)&init_buf);
1121 if (init_cp == NULL) {
1122 /* could not pull a INIT chunk in cookie */
1124 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1125 printf("process_cookie_existing: could not pull INIT chunk hdr\n");
1127 #endif /* SCTP_DEBUG */
1130 chk_length = ntohs(init_cp->ch.chunk_length);
1131 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1133 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1134 printf("process_cookie_existing: could not find INIT chunk!\n");
1136 #endif /* SCTP_DEBUG */
1141 * find and validate the INIT-ACK chunk in the cookie (my info)
1142 * the INIT-ACK follows the INIT chunk
1144 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1145 initack_cp = (struct sctp_init_ack_chunk *)
1146 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1147 (u_int8_t *)&initack_buf);
1148 if (initack_cp == NULL) {
1149 /* could not pull INIT-ACK chunk in cookie */
1151 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1152 printf("process_cookie_existing: could not pull INIT-ACK chunk hdr\n");
1154 #endif /* SCTP_DEBUG */
1157 chk_length = ntohs(initack_cp->ch.chunk_length);
1158 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1160 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1161 printf("process_cookie_existing: could not find INIT-ACK chunk!\n");
1163 #endif /* SCTP_DEBUG */
1166 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1167 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1169 * case D in Section 5.2.4 Table 2: MMAA
1170 * process accordingly to get into the OPEN state
1172 switch SCTP_GET_STATE(asoc) {
1173 case SCTP_STATE_COOKIE_WAIT:
1175 * INIT was sent, but got got a COOKIE_ECHO with
1176 * the correct tags... just accept it...
1178 /* First we must process the INIT !! */
1179 retval = sctp_process_init(init_cp, stcb, net);
1182 printf("process_cookie_existing: INIT processing failed\n");
1186 /* intentional fall through to below... */
1188 case SCTP_STATE_COOKIE_ECHOED:
1189 /* Duplicate INIT case */
1190 /* we have already processed the INIT so no problem */
1191 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1193 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
1194 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb,
1196 /* update current state */
1197 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1198 asoc->state = SCTP_STATE_OPEN |
1199 SCTP_STATE_SHUTDOWN_PENDING;
1200 } else if ((asoc->state & SCTP_STATE_SHUTDOWN_SENT) == 0) {
1201 /* if ok, move to OPEN state */
1202 asoc->state = SCTP_STATE_OPEN;
1204 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1205 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1206 (!(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING))) {
1208 * Here is where collision would go if we did a
1209 * connect() and instead got a
1210 * init/init-ack/cookie done before the
1211 * init-ack came back..
1213 stcb->sctp_ep->sctp_flags |=
1214 SCTP_PCB_FLAGS_CONNECTED;
1215 soisconnected(stcb->sctp_ep->sctp_socket);
1217 /* notify upper layer */
1218 *notification = SCTP_NOTIFY_ASSOC_UP;
1219 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1222 * since we did not send a HB make sure we don't double
1225 net->hb_responded = 1;
1227 if (stcb->asoc.sctp_autoclose_ticks &&
1228 (inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
1229 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1235 * we're in the OPEN state (or beyond), so peer
1236 * must have simply lost the COOKIE-ACK
1242 * We ignore the return code here.. not sure if we should
1243 * somehow abort.. but we do have an existing asoc. This
1244 * really should not fail.
1246 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1247 init_offset + sizeof(struct sctp_init_chunk),
1248 initack_offset, sh, init_src)) {
1250 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1251 printf("Weird cookie load_address failure on cookie existing - 1\n");
1257 /* respond with a COOKIE-ACK */
1258 sctp_send_cookie_ack(stcb);
1261 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1262 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1263 cookie->tie_tag_my_vtag == 0 &&
1264 cookie->tie_tag_peer_vtag == 0) {
1266 * case C in Section 5.2.4 Table 2: XMOO
1271 if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag &&
1272 (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag ||
1273 init_cp->init.initiate_tag == 0)) {
1275 * case B in Section 5.2.4 Table 2: MXAA or MOAA
1276 * my info should be ok, re-accept peer info
1278 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1279 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
1280 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
1281 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1283 * since we did not send a HB make sure we don't double things
1285 net->hb_responded = 1;
1286 if (stcb->asoc.sctp_autoclose_ticks &&
1287 (inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
1288 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1291 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1292 asoc->pre_open_streams =
1293 ntohs(initack_cp->init.num_outbound_streams);
1294 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1295 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out =
1296 asoc->init_seq_number;
1297 asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
1298 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1299 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1300 asoc->str_reset_seq_in = asoc->init_seq_number;
1301 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1303 /* process the INIT info (peer's info) */
1304 retval = sctp_process_init(init_cp, stcb, net);
1307 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1308 printf("process_cookie_existing: INIT processing failed\n");
1313 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1314 init_offset + sizeof(struct sctp_init_chunk),
1315 initack_offset, sh, init_src)) {
1317 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1318 printf("Weird cookie load_address failure on cookie existing - 2\n");
1324 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1325 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1326 *notification = SCTP_NOTIFY_ASSOC_UP;
1328 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1329 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1330 !(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
1331 stcb->sctp_ep->sctp_flags |=
1332 SCTP_PCB_FLAGS_CONNECTED;
1333 soisconnected(stcb->sctp_ep->sctp_socket);
1336 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1337 asoc->state = SCTP_STATE_OPEN |
1338 SCTP_STATE_SHUTDOWN_PENDING;
1340 asoc->state = SCTP_STATE_OPEN;
1342 sctp_send_cookie_ack(stcb);
1346 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1347 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1348 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1349 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1350 cookie->tie_tag_peer_vtag != 0) {
1352 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1354 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
1355 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
1356 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1358 /* notify upper layer */
1359 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1361 /* send up all the data */
1362 sctp_report_all_outbound(stcb);
1364 /* process the INIT-ACK info (my info) */
1365 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1366 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1367 asoc->pre_open_streams =
1368 ntohs(initack_cp->init.num_outbound_streams);
1369 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1370 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out =
1371 asoc->init_seq_number;
1372 asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
1373 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1374 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1375 asoc->str_reset_seq_in = asoc->init_seq_number;
1377 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1378 if (asoc->mapping_array)
1379 memset(asoc->mapping_array, 0,
1380 asoc->mapping_array_size);
1381 /* process the INIT info (peer's info) */
1382 retval = sctp_process_init(init_cp, stcb, net);
1385 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1386 printf("process_cookie_existing: INIT processing failed\n");
1392 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1394 * since we did not send a HB make sure we don't double things
1396 net->hb_responded = 1;
1398 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1399 init_offset + sizeof(struct sctp_init_chunk),
1400 initack_offset, sh, init_src)) {
1402 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1403 printf("Weird cookie load_address failure on cookie existing - 3\n");
1409 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1410 asoc->state = SCTP_STATE_OPEN |
1411 SCTP_STATE_SHUTDOWN_PENDING;
1412 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1413 /* move to OPEN state, if not in SHUTDOWN_SENT */
1414 asoc->state = SCTP_STATE_OPEN;
1416 /* respond with a COOKIE-ACK */
1417 sctp_send_cookie_ack(stcb);
1421 /* all other cases... */
1426 * handle a state cookie for a new association
1427 * m: input packet mbuf chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk
1428 * note: this is a "split" mbuf and the cookie signature does not exist
1429 * offset: offset into mbuf to the cookie-echo chunk
1430 * length: length of the cookie chunk
1431 * to: where the init was from
1434 static struct sctp_tcb *
1435 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1436 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1437 struct sctp_inpcb *inp, struct sctp_nets **netp,
1438 struct sockaddr *init_src, int *notification)
1440 struct sctp_tcb *stcb;
1441 struct sctp_init_chunk *init_cp, init_buf;
1442 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1443 struct sockaddr_storage sa_store;
1444 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
1445 struct sockaddr_in *sin;
1446 struct sockaddr_in6 *sin6;
1447 struct sctp_association *asoc;
1449 int init_offset, initack_offset, initack_limit;
1454 * find and validate the INIT chunk in the cookie (peer's info)
1455 * the INIT should start after the cookie-echo header struct
1456 * (chunk header, state cookie header struct)
1458 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
1459 init_cp = (struct sctp_init_chunk *)
1460 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1461 (u_int8_t *)&init_buf);
1462 if (init_cp == NULL) {
1463 /* could not pull a INIT chunk in cookie */
1465 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1466 printf("process_cookie_new: could not pull INIT chunk hdr\n");
1468 #endif /* SCTP_DEBUG */
1471 chk_length = ntohs(init_cp->ch.chunk_length);
1472 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1474 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1475 printf("HUH? process_cookie_new: could not find INIT chunk!\n");
1477 #endif /* SCTP_DEBUG */
1481 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1483 * find and validate the INIT-ACK chunk in the cookie (my info)
1484 * the INIT-ACK follows the INIT chunk
1486 initack_cp = (struct sctp_init_ack_chunk *)
1487 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1488 (u_int8_t *)&initack_buf);
1489 if (initack_cp == NULL) {
1490 /* could not pull INIT-ACK chunk in cookie */
1492 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1493 printf("process_cookie_new: could not pull INIT-ACK chunk hdr\n");
1495 #endif /* SCTP_DEBUG */
1498 chk_length = ntohs(initack_cp->ch.chunk_length);
1499 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1501 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1503 pp = (u_int8_t *)initack_cp;
1504 printf("process_cookie_new: could not find INIT-ACK chunk!\n");
1505 printf("Found bytes %x %x %x %x at postion %d\n",
1506 (u_int)pp[0], (u_int)pp[1], (u_int)pp[2],
1507 (u_int)pp[3], initack_offset);
1509 #endif /* SCTP_DEBUG */
1512 initack_limit = initack_offset + SCTP_SIZE32(chk_length);
1515 * now that we know the INIT/INIT-ACK are in place,
1516 * create a new TCB and popluate
1518 stcb = sctp_aloc_assoc(inp, init_src, 0, &error, ntohl(initack_cp->init.initiate_tag));
1520 struct mbuf *op_err;
1521 /* memory problem? */
1523 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1524 printf("process_cookie_new: no room for another TCB!\n");
1526 #endif /* SCTP_DEBUG */
1527 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1528 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
1533 /* get the correct sctp_nets */
1534 *netp = sctp_findnet(stcb, init_src);
1536 /* get scope variables out of cookie */
1537 asoc->ipv4_local_scope = cookie->ipv4_scope;
1538 asoc->site_scope = cookie->site_scope;
1539 asoc->local_scope = cookie->local_scope;
1540 asoc->loopback_scope = cookie->loopback_scope;
1542 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
1543 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
1544 struct mbuf *op_err;
1546 * Houston we have a problem. The EP changed while the cookie
1547 * was in flight. Only recourse is to abort the association.
1549 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1550 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
1555 /* process the INIT-ACK info (my info) */
1556 old_tag = asoc->my_vtag;
1557 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1558 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1559 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1560 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1561 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1562 asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
1563 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1564 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1565 asoc->str_reset_seq_in = asoc->init_seq_number;
1567 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1569 /* process the INIT info (peer's info) */
1570 retval = sctp_process_init(init_cp, stcb, *netp);
1573 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1574 printf("process_cookie_new: INIT processing failed\n");
1577 sctp_free_assoc(inp, stcb);
1580 /* load all addresses */
1581 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1582 init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
1584 sctp_free_assoc(inp, stcb);
1588 /* update current state */
1590 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1591 printf("moving to OPEN state\n");
1594 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1595 asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
1597 asoc->state = SCTP_STATE_OPEN;
1599 /* calculate the RTT */
1600 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
1601 &cookie->time_entered);
1604 * if we're doing ASCONFs, check to see if we have any new
1605 * local addresses that need to get added to the peer (eg.
1606 * addresses changed while cookie echo in flight). This needs
1607 * to be done after we go to the OPEN state to do the correct
1608 * asconf processing.
1609 * else, make sure we have the correct addresses in our lists
1612 /* warning, we re-use sin, sin6, sa_store here! */
1613 /* pull in local_address (our "from" address) */
1614 if (cookie->laddr_type == SCTP_IPV4_ADDRESS) {
1615 /* source addr is IPv4 */
1616 sin = (struct sockaddr_in *)initack_src;
1617 memset(sin, 0, sizeof(*sin));
1618 sin->sin_family = AF_INET;
1619 sin->sin_len = sizeof(struct sockaddr_in);
1620 sin->sin_addr.s_addr = cookie->laddress[0];
1621 } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) {
1622 /* source addr is IPv6 */
1623 sin6 = (struct sockaddr_in6 *)initack_src;
1624 memset(sin6, 0, sizeof(*sin6));
1625 sin6->sin6_family = AF_INET6;
1626 sin6->sin6_len = sizeof(struct sockaddr_in6);
1627 sin6->sin6_scope_id = cookie->scope_id;
1628 memcpy(&sin6->sin6_addr, cookie->laddress,
1629 sizeof(sin6->sin6_addr));
1631 sctp_free_assoc(inp, stcb);
1635 sctp_check_address_list(stcb, m, initack_offset +
1636 sizeof(struct sctp_init_ack_chunk), initack_limit,
1637 initack_src, cookie->local_scope, cookie->site_scope,
1638 cookie->ipv4_scope, cookie->loopback_scope);
1641 /* set up to notify upper layer */
1642 *notification = SCTP_NOTIFY_ASSOC_UP;
1643 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1644 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1645 !(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
1647 * This is an endpoint that called connect()
1648 * how it got a cookie that is NEW is a bit of
1649 * a mystery. It must be that the INIT was sent, but
1650 * before it got there.. a complete INIT/INIT-ACK/COOKIE
1651 * arrived. But of course then it should have went to
1652 * the other code.. not here.. oh well.. a bit of protection
1655 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1656 soisconnected(stcb->sctp_ep->sctp_socket);
1657 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, *netp);
1658 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1659 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
1661 * We don't want to do anything with this
1662 * one. Since it is the listening guy. The timer will
1663 * get started for accepted connections in the caller.
1667 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, *netp);
1669 /* since we did not send a HB make sure we don't double things */
1670 (*netp)->hb_responded = 1;
1672 if (stcb->asoc.sctp_autoclose_ticks &&
1673 (inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
1674 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
1677 /* respond with a COOKIE-ACK */
1678 sctp_send_cookie_ack(stcb);
1685 * handles a COOKIE-ECHO message
1686 * stcb: modified to either a new or left as existing (non-NULL) TCB
1688 static struct mbuf *
1689 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
1690 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
1691 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp)
1693 struct sctp_state_cookie *cookie;
1694 struct sockaddr_in6 sin6;
1695 struct sockaddr_in sin;
1696 struct sctp_tcb *l_stcb=*stcb;
1697 struct sctp_inpcb *l_inp;
1698 struct sockaddr *to;
1699 struct sctp_pcb *ep;
1701 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
1703 uint8_t cookie_ok = 0;
1704 unsigned int size_of_pkt, sig_offset, cookie_offset;
1705 unsigned int cookie_len;
1707 struct timeval time_expires;
1708 struct sockaddr_storage dest_store;
1709 struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
1711 int notification = 0;
1712 struct sctp_nets *netl;
1713 int had_a_existing_tcb = 0;
1716 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1717 printf("sctp_handle_cookie: handling COOKIE-ECHO\n");
1721 if (inp_p == NULL) {
1723 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1724 printf("sctp_handle_cookie: null inp_p!\n");
1729 /* First get the destination address setup too. */
1730 iph = mtod(m, struct ip *);
1731 if (iph->ip_v == IPVERSION) {
1733 struct sockaddr_in *sin;
1734 sin = (struct sockaddr_in *)(localep_sa);
1735 memset(sin, 0, sizeof(*sin));
1736 sin->sin_family = AF_INET;
1737 sin->sin_len = sizeof(*sin);
1738 sin->sin_port = sh->dest_port;
1739 sin->sin_addr.s_addr = iph->ip_dst.s_addr ;
1740 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1742 struct ip6_hdr *ip6;
1743 struct sockaddr_in6 *sin6;
1744 sin6 = (struct sockaddr_in6 *)(localep_sa);
1745 memset(sin6, 0, sizeof(*sin6));
1746 sin6->sin6_family = AF_INET6;
1747 sin6->sin6_len = sizeof(struct sockaddr_in6);
1748 ip6 = mtod(m, struct ip6_hdr *);
1749 sin6->sin6_port = sh->dest_port;
1750 sin6->sin6_addr = ip6->ip6_dst;
1755 cookie = &cp->cookie;
1756 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
1757 cookie_len = ntohs(cp->ch.chunk_length);
1759 if ((cookie->peerport != sh->src_port) &&
1760 (cookie->myport != sh->dest_port) &&
1761 (cookie->my_vtag != sh->v_tag)) {
1763 * invalid ports or bad tag. Note that we always leave
1764 * the v_tag in the header in network order and when we
1765 * stored it in the my_vtag slot we also left it in network
1766 * order. This maintians the match even though it may be in
1767 * the opposite byte order of the machine :->
1772 /* compute size of packet */
1773 if (m->m_flags & M_PKTHDR) {
1774 size_of_pkt = m->m_pkthdr.len;
1776 /* Should have a pkt hdr really */
1780 while (mat != NULL) {
1781 size_of_pkt += mat->m_len;
1785 if (cookie_len > size_of_pkt ||
1786 cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
1787 sizeof(struct sctp_init_chunk) +
1788 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
1789 /* cookie too long! or too small */
1791 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1792 printf("sctp_handle_cookie: cookie_len=%u, pkt size=%u\n", cookie_len, size_of_pkt);
1794 #endif /* SCTP_DEBUG */
1798 * split off the signature into its own mbuf (since it
1799 * should not be calculated in the sctp_hash_digest_m() call).
1801 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
1802 if (sig_offset > size_of_pkt) {
1803 /* packet not correct size! */
1804 /* XXX this may already be accounted for earlier... */
1806 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1807 printf("sctp_handle_cookie: sig offset=%u, pkt size=%u\n", sig_offset, size_of_pkt);
1813 m_sig = m_split(m, sig_offset, M_DONTWAIT);
1814 if (m_sig == NULL) {
1815 /* out of memory or ?? */
1817 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1818 printf("sctp_handle_cookie: couldn't m_split the signature\n");
1824 * compute the signature/digest for the cookie
1826 ep = &(*inp_p)->sctp_ep;
1829 SCTP_TCB_UNLOCK(l_stcb);
1831 SCTP_INP_RLOCK(l_inp);
1833 SCTP_TCB_LOCK(l_stcb);
1835 /* which cookie is it? */
1836 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
1837 (ep->current_secret_number != ep->last_secret_number)) {
1838 /* it's the old cookie */
1840 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1841 printf("sctp_handle_cookie: old cookie sig\n");
1844 sctp_hash_digest_m((char *)ep->secret_key[(int)ep->last_secret_number],
1845 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1847 /* it's the current cookie */
1849 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1850 printf("sctp_handle_cookie: current cookie sig\n");
1853 sctp_hash_digest_m((char *)ep->secret_key[(int)ep->current_secret_number],
1854 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1856 /* get the signature */
1857 SCTP_INP_RUNLOCK(l_inp);
1858 sig = (u_int8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (u_int8_t *)&tmp_sig);
1860 /* couldn't find signature */
1862 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1863 printf("sctp_handle_cookie: couldn't pull the signature\n");
1868 /* compare the received digest with the computed digest */
1869 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
1870 /* try the old cookie? */
1871 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
1872 (ep->current_secret_number != ep->last_secret_number)) {
1873 /* compute digest with old */
1875 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1876 printf("sctp_handle_cookie: old cookie sig\n");
1879 sctp_hash_digest_m((char *)ep->secret_key[(int)ep->last_secret_number],
1880 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1882 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
1890 * Now before we continue we must reconstruct our mbuf so
1891 * that normal processing of any other chunks will work.
1896 while (m_at->m_next != NULL) {
1897 m_at = m_at->m_next;
1899 m_at->m_next = m_sig;
1900 if (m->m_flags & M_PKTHDR) {
1902 * We should only do this if and only if the front
1903 * mbuf has a m_pkthdr... it should in theory.
1905 if (m_sig->m_flags & M_PKTHDR) {
1906 /* Add back to the pkt hdr of main m chain */
1907 m->m_pkthdr.len += m_sig->m_len;
1910 * Got a problem, no pkthdr in split chain.
1911 * TSNH but we will handle it just in case
1915 printf("Warning: Hitting m_split join TSNH code - fixed\n");
1918 mmlen += lat->m_len;
1921 m->m_pkthdr.len += mmlen;
1926 if (cookie_ok == 0) {
1928 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1929 printf("handle_cookie_echo: cookie signature validation failed!\n");
1930 printf("offset = %u, cookie_offset = %u, sig_offset = %u\n",
1931 (u_int32_t)offset, cookie_offset, sig_offset);
1937 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1938 printf("handle_cookie_echo: cookie signature validation passed\n");
1943 * check the cookie timestamps to be sure it's not stale
1945 SCTP_GETTIME_TIMEVAL(&now);
1946 /* Expire time is in Ticks, so we convert to seconds */
1947 time_expires.tv_sec = cookie->time_entered.tv_sec + cookie->cookie_life;
1948 time_expires.tv_usec = cookie->time_entered.tv_usec;
1950 if (timercmp(&now, &time_expires, >))
1952 if (timevalcmp(&now, &time_expires, >))
1955 /* cookie is stale! */
1956 struct mbuf *op_err;
1957 struct sctp_stale_cookie_msg *scm;
1960 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1961 printf("sctp_handle_cookie: got a STALE cookie!\n");
1964 MGETHDR(op_err, M_DONTWAIT, MT_HEADER);
1965 if (op_err == NULL) {
1969 /* pre-reserve some space */
1970 op_err->m_data += sizeof(struct ip6_hdr);
1971 op_err->m_data += sizeof(struct sctphdr);
1972 op_err->m_data += sizeof(struct sctp_chunkhdr);
1975 op_err->m_len = op_err->m_pkthdr.len = sizeof(struct sctp_stale_cookie_msg);
1976 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
1977 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
1978 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
1979 (sizeof(u_int32_t))));
1980 /* seconds to usec */
1981 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
1984 tim = now.tv_usec - cookie->time_entered.tv_usec;
1985 scm->time_usec = htonl(tim);
1986 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag);
1990 * Now we must see with the lookup address if we have an existing
1991 * asoc. This will only happen if we were in the COOKIE-WAIT state
1992 * and a INIT collided with us and somewhere the peer sent the
1993 * cookie on another address besides the single address our assoc
1994 * had for him. In this case we will have one of the tie-tags set
1995 * at least AND the address field in the cookie can be used to
1999 if (cookie->addr_type == SCTP_IPV6_ADDRESS) {
2000 memset(&sin6, 0, sizeof(sin6));
2001 sin6.sin6_family = AF_INET6;
2002 sin6.sin6_len = sizeof(sin6);
2003 sin6.sin6_port = sh->src_port;
2004 sin6.sin6_scope_id = cookie->scope_id;
2005 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2006 sizeof(sin6.sin6_addr.s6_addr));
2007 to = (struct sockaddr *)&sin6;
2008 } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) {
2009 memset(&sin, 0, sizeof(sin));
2010 sin.sin_family = AF_INET;
2011 sin.sin_len = sizeof(sin);
2012 sin.sin_port = sh->src_port;
2013 sin.sin_addr.s_addr = cookie->address[0];
2014 to = (struct sockaddr *)&sin;
2017 if ((*stcb == NULL) && to) {
2018 /* Yep, lets check */
2019 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
2020 if (*stcb == NULL) {
2021 /* We should have only got back the same inp. If we
2022 * got back a different ep we have a problem. The original
2023 * findep got back l_inp and now
2025 if (l_inp != *inp_p) {
2026 printf("Bad problem find_ep got a diff inp then special_locate?\n");
2031 cookie_len -= SCTP_SIGNATURE_SIZE;
2032 if (*stcb == NULL) {
2033 /* this is the "normal" case... get a new TCB */
2035 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2036 printf("sctp_handle_cookie: processing NEW cookie\n");
2039 *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
2040 cookie_len, *inp_p, netp, to, ¬ification);
2041 /* now always decrement, since this is the normal
2042 * case.. we had no tcb when we entered.
2045 /* this is abnormal... cookie-echo on existing TCB */
2047 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2048 printf("sctp_handle_cookie: processing EXISTING cookie\n");
2051 had_a_existing_tcb = 1;
2052 *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
2053 cookie, cookie_len, *inp_p, *stcb, *netp, to, ¬ification);
2056 if (*stcb == NULL) {
2057 /* still no TCB... must be bad cookie-echo */
2059 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2060 printf("handle_cookie_echo: ACK! don't have a TCB!\n");
2062 #endif /* SCTP_DEBUG */
2067 * Ok, we built an association so confirm the address
2068 * we sent the INIT-ACK to.
2070 netl = sctp_findnet(*stcb, to);
2071 /* This code should in theory NOT run but
2076 printf("TSNH! Huh, why do I need to add this address here?\n");
2078 ret = sctp_add_remote_addr(*stcb, to, 0, 100);
2079 netl = sctp_findnet(*stcb, to);
2082 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2083 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2084 sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2086 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2087 (*stcb), 0, (void *)netl);
2092 printf("Could not add source address for some reason\n");
2096 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2097 if (!had_a_existing_tcb ||
2098 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2100 * If we have a NEW cookie or the connect never reached
2101 * the connected state during collision we must do the
2104 struct socket *so, *oso;
2105 struct sctp_inpcb *inp;
2106 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2108 * For a restart we will keep the same socket,
2109 * no need to do anything. I THINK!!
2111 sctp_ulp_notify(notification, *stcb, 0, NULL);
2114 oso = (*inp_p)->sctp_socket;
2115 SCTP_TCB_UNLOCK((*stcb));
2116 so = sonewconn(oso, SS_ISCONNECTED);
2117 SCTP_INP_WLOCK((*stcb)->sctp_ep);
2118 SCTP_TCB_LOCK((*stcb));
2119 SCTP_INP_WUNLOCK((*stcb)->sctp_ep);
2121 struct mbuf *op_err;
2122 /* Too many sockets */
2124 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
2125 printf("process_cookie_new: no room for another socket!\n");
2127 #endif /* SCTP_DEBUG */
2128 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2129 sctp_abort_association(*inp_p, NULL, m, iphlen,
2131 sctp_free_assoc(*inp_p, *stcb);
2134 inp = (struct sctp_inpcb *)so->so_pcb;
2135 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2136 SCTP_PCB_FLAGS_CONNECTED |
2137 SCTP_PCB_FLAGS_IN_TCPPOOL |
2138 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2139 SCTP_PCB_FLAGS_DONT_WAKE);
2140 inp->sctp_socket = so;
2143 * Now we must move it from one hash table to another
2144 * and get the tcb in the right place.
2146 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2148 /* Switch over to the new guy */
2151 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp,
2154 sctp_ulp_notify(notification, *stcb, 0, NULL);
2158 if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2159 sctp_ulp_notify(notification, *stcb, 0, NULL);
2165 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp,
2166 struct sctp_tcb *stcb, struct sctp_nets *net)
2168 /* cp must not be used, others call this without a c-ack :-) */
2169 struct sctp_association *asoc;
2172 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2173 printf("sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2181 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, stcb, net);
2183 /* process according to association state */
2184 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2185 /* state change only needed when I am in right state */
2187 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2188 printf("moving to OPEN state\n");
2191 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2192 asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
2194 asoc->state = SCTP_STATE_OPEN;
2198 if (asoc->overall_error_count == 0) {
2199 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2200 &asoc->time_entered);
2202 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2203 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL);
2204 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2205 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2206 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2207 soisconnected(stcb->sctp_ep->sctp_socket);
2209 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2211 /* since we did not send a HB make sure we don't double things */
2212 net->hb_responded = 1;
2214 if (stcb->asoc.sctp_autoclose_ticks &&
2215 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
2216 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2217 stcb->sctp_ep, stcb, NULL);
2221 * set ASCONF timer if ASCONFs are pending and allowed
2222 * (eg. addresses changed when init/cookie echo in flight)
2224 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) &&
2225 (stcb->asoc.peer_supports_asconf) &&
2226 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2227 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2228 stcb->sctp_ep, stcb,
2229 stcb->asoc.primary_destination);
2233 /* Toss the cookie if I can */
2234 sctp_toss_old_cookies(asoc);
2235 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2236 /* Restart the timer if we have pending data */
2237 struct sctp_tmit_chunk *chk;
2238 chk = TAILQ_FIRST(&asoc->sent_queue);
2240 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2248 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2249 struct sctp_tcb *stcb)
2251 struct sctp_nets *net;
2252 struct sctp_tmit_chunk *lchk;
2254 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) {
2257 sctp_pegs[SCTP_ECNE_RCVD]++;
2258 tsn = ntohl(cp->tsn);
2259 /* ECN Nonce stuff: need a resync and disable the nonce sum check */
2260 /* Also we make sure we disable the nonce_wait */
2261 lchk = TAILQ_FIRST(&stcb->asoc.send_queue);
2263 stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
2265 stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq;
2267 stcb->asoc.nonce_wait_for_ecne = 0;
2268 stcb->asoc.nonce_sum_check = 0;
2270 /* Find where it was sent, if possible */
2272 lchk = TAILQ_FIRST(&stcb->asoc.sent_queue);
2274 if (lchk->rec.data.TSN_seq == tsn) {
2278 if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ))
2280 lchk = TAILQ_NEXT(lchk, sctp_next);
2283 /* default is we use the primary */
2284 net = stcb->asoc.primary_destination;
2286 if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) {
2287 #ifdef SCTP_CWND_LOGGING
2290 #ifdef SCTP_CWND_LOGGING
2291 old_cwnd = net->cwnd;
2293 sctp_pegs[SCTP_CWR_PERFO]++;
2294 net->ssthresh = net->cwnd / 2;
2295 if (net->ssthresh < net->mtu) {
2296 net->ssthresh = net->mtu;
2297 /* here back off the timer as well, to slow us down */
2300 net->cwnd = net->ssthresh;
2301 #ifdef SCTP_CWND_LOGGING
2302 sctp_log_cwnd(net, (net->cwnd-old_cwnd), SCTP_CWND_LOG_FROM_SAT);
2304 /* we reduce once every RTT. So we will only lower
2305 * cwnd at the next sending seq i.e. the resync_tsn.
2307 stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn;
2310 * We always send a CWR this way if our previous one was lost
2311 * our peer will get an update, or if it is not time again
2312 * to reduce we still get the cwr to the peer.
2314 sctp_send_cwr(stcb, net, tsn);
2318 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb)
2320 /* Here we get a CWR from the peer. We must look in
2321 * the outqueue and make sure that we have a covered
2322 * ECNE in teh control chunk part. If so remove it.
2324 struct sctp_tmit_chunk *chk;
2325 struct sctp_ecne_chunk *ecne;
2327 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
2328 if (chk->rec.chunk_id != SCTP_ECN_ECHO) {
2331 /* Look for and remove if it is the right TSN. Since
2332 * there is only ONE ECNE on the control queue at
2333 * any one time we don't need to worry about more than
2336 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
2337 if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn),
2338 MAX_TSN) || (cp->tsn == ecne->tsn)) {
2339 /* this covers this ECNE, we can remove it */
2340 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
2343 sctp_m_freem(chk->data);
2346 stcb->asoc.ctrl_queue_cnt--;
2347 sctp_free_remote_addr(chk->whoTo);
2348 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
2349 sctppcbinfo.ipi_count_chunk--;
2350 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
2351 panic("Chunk count is negative");
2353 sctppcbinfo.ipi_gencnt_chunk++;
2360 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp,
2361 struct sctp_tcb *stcb, struct sctp_nets *net)
2363 struct sctp_association *asoc;
2366 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2367 printf("sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
2374 /* process according to association state */
2375 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
2376 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
2379 /* notify upper layer protocol */
2380 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL);
2381 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2382 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2383 stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
2384 stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
2385 stcb->sctp_ep->sctp_socket->so_snd.sb_mbcnt = 0;
2386 soisdisconnected(stcb->sctp_ep->sctp_socket);
2388 /* are the queues empty? they should be */
2389 if (!TAILQ_EMPTY(&asoc->send_queue) ||
2390 !TAILQ_EMPTY(&asoc->sent_queue) ||
2391 !TAILQ_EMPTY(&asoc->out_wheel)) {
2392 sctp_report_all_outbound(stcb);
2394 /* stop the timer */
2395 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
2397 sctp_free_assoc(stcb->sctp_ep, stcb);
2402 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
2403 struct sctp_nets *net, u_int8_t flg)
2405 switch (desc->chunk_type) {
2407 /* find the tsn to resend (possibly */
2410 struct sctp_tmit_chunk *tp1;
2411 tsn = ntohl(desc->tsn_ifany);
2412 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2414 if (tp1->rec.data.TSN_seq == tsn) {
2418 if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn,
2424 tp1 = TAILQ_NEXT(tp1, sctp_next);
2427 /* Do it the other way */
2428 sctp_pegs[SCTP_PDRP_DNFND]++;
2429 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2431 if (tp1->rec.data.TSN_seq == tsn) {
2435 tp1 = TAILQ_NEXT(tp1, sctp_next);
2439 sctp_pegs[SCTP_PDRP_TSNNF]++;
2441 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
2443 if (((tp1->rec.data.state_flags & SCTP_WINDOW_PROBE) == SCTP_WINDOW_PROBE) &&
2444 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
2445 sctp_pegs[SCTP_PDRP_DIWNP]++;
2448 if (stcb->asoc.peers_rwnd == 0 &&
2449 (flg & SCTP_FROM_MIDDLE_BOX)) {
2450 sctp_pegs[SCTP_PDRP_DIZRW]++;
2453 ddp = (u_int8_t *)(mtod(tp1->data, caddr_t) +
2454 sizeof(struct sctp_data_chunk));
2457 for (iii = 0; iii < sizeof(desc->data_bytes);
2459 if (ddp[iii] != desc->data_bytes[iii]) {
2460 sctp_pegs[SCTP_PDRP_BADD]++;
2465 if (tp1->sent != SCTP_DATAGRAM_RESEND) {
2466 stcb->asoc.sent_queue_retran_cnt++;
2468 /* We zero out the nonce so resync not needed */
2469 tp1->rec.data.ect_nonce = 0;
2473 * this guy had a RTO calculation pending on it,
2476 tp1->whoTo->rto_pending = 0;
2479 sctp_pegs[SCTP_PDRP_MARK]++;
2480 tp1->sent = SCTP_DATAGRAM_RESEND;
2482 * mark it as if we were doing a FR, since we
2483 * will be getting gap ack reports behind the
2484 * info from the router.
2486 tp1->rec.data.doing_fast_retransmit = 1;
2488 * mark the tsn with what sequences can cause a new FR.
2490 if (TAILQ_EMPTY(&stcb->asoc.send_queue) ) {
2491 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
2493 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
2496 /* restart the timer */
2497 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2499 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2502 /* fix counts and things */
2503 tp1->whoTo->flight_size -= tp1->send_size;
2504 if (tp1->whoTo->flight_size < 0) {
2505 tp1->whoTo->flight_size = 0;
2507 stcb->asoc.total_flight -= tp1->book_size;
2508 if (stcb->asoc.total_flight < 0) {
2509 stcb->asoc.total_flight = 0;
2511 stcb->asoc.total_flight_count--;
2512 if (stcb->asoc.total_flight_count < 0) {
2513 stcb->asoc.total_flight_count = 0;
2521 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
2522 if (tp1->sent == SCTP_DATAGRAM_RESEND)
2525 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
2527 if (tp1->sent == SCTP_DATAGRAM_RESEND)
2530 if (audit != stcb->asoc.sent_queue_retran_cnt) {
2531 printf("**Local Audit finds cnt:%d asoc cnt:%d\n",
2532 audit, stcb->asoc.sent_queue_retran_cnt);
2533 #ifndef SCTP_AUDITING_ENABLED
2534 stcb->asoc.sent_queue_retran_cnt = audit;
2542 struct sctp_tmit_chunk *asconf;
2543 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
2545 if (asconf->rec.chunk_id == SCTP_ASCONF) {
2550 if (asconf->sent != SCTP_DATAGRAM_RESEND)
2551 stcb->asoc.sent_queue_retran_cnt++;
2552 asconf->sent = SCTP_DATAGRAM_RESEND;
2553 asconf->snd_count--;
2557 case SCTP_INITIATION:
2558 /* resend the INIT */
2559 stcb->asoc.dropped_special_cnt++;
2560 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
2562 * If we can get it in, in a few attempts we do this,
2563 * otherwise we let the timer fire.
2565 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
2567 sctp_send_initiate(stcb->sctp_ep, stcb);
2570 case SCTP_SELECTIVE_ACK:
2571 /* resend the sack */
2572 sctp_send_sack(stcb);
2574 case SCTP_HEARTBEAT_REQUEST:
2575 /* resend a demand HB */
2576 sctp_send_hb(stcb, 1, net);
2580 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2581 printf("%s:%d sends a shutdown\n",
2587 sctp_send_shutdown(stcb, net);
2589 case SCTP_SHUTDOWN_ACK:
2590 sctp_send_shutdown_ack(stcb, net);
2592 case SCTP_COOKIE_ECHO:
2594 struct sctp_tmit_chunk *cookie;
2596 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
2598 if (cookie->rec.chunk_id == SCTP_COOKIE_ECHO) {
2603 if (cookie->sent != SCTP_DATAGRAM_RESEND)
2604 stcb->asoc.sent_queue_retran_cnt++;
2605 cookie->sent = SCTP_DATAGRAM_RESEND;
2606 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, stcb, net);
2610 case SCTP_COOKIE_ACK:
2611 sctp_send_cookie_ack(stcb);
2613 case SCTP_ASCONF_ACK:
2614 /* resend last asconf ack */
2615 sctp_send_asconf_ack(stcb, 1);
2617 case SCTP_FORWARD_CUM_TSN:
2618 send_forward_tsn(stcb, &stcb->asoc);
2620 /* can't do anything with these */
2621 case SCTP_PACKET_DROPPED:
2622 case SCTP_INITIATION_ACK: /* this should not happen */
2623 case SCTP_HEARTBEAT_ACK:
2624 case SCTP_ABORT_ASSOCIATION:
2625 case SCTP_OPERATION_ERROR:
2626 case SCTP_SHUTDOWN_COMPLETE:
2636 sctp_reset_in_stream(struct sctp_tcb *stcb,
2637 struct sctp_stream_reset_response *resp, int number_entries)
2640 uint16_t *list, temp;
2642 /* We set things to 0xffff since this is the last delivered
2643 * sequence and we will be sending in 0 after the reset.
2646 if (resp->reset_flags & SCTP_RESET_PERFORMED) {
2647 if (number_entries) {
2648 list = resp->list_of_streams;
2649 for (i = 0; i < number_entries; i++) {
2650 temp = ntohs(list[i]);
2652 if (list[i] >= stcb->asoc.streamincnt) {
2653 printf("Invalid stream in-stream reset %d\n", list[i]);
2656 stcb->asoc.strmin[(list[i])].last_sequence_delivered = 0xffff;
2660 for (i = 0; i < stcb->asoc.streamincnt; i++) {
2661 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
2664 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list);
2669 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
2671 struct sctp_tmit_chunk *chk, *nchk;
2672 struct sctp_association *asoc;
2676 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
2678 nchk = TAILQ_NEXT(chk, sctp_next);
2679 if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
2680 struct sctp_stream_reset_req *strreq;
2681 strreq = mtod(chk->data, struct sctp_stream_reset_req *);
2682 if (strreq->sr_req.ph.param_type == ntohs(SCTP_STR_RESET_RESPONSE)) {
2683 /* we only clean up the request */
2685 } else if (strreq->sr_req.ph.param_type != ntohs(SCTP_STR_RESET_REQUEST)) {
2686 printf("TSNH, an unknown stream reset request is in queue %x\n",
2687 (u_int)ntohs(strreq->sr_req.ph.param_type));
2690 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
2691 TAILQ_REMOVE(&asoc->control_send_queue,
2695 sctp_m_freem(chk->data);
2698 asoc->ctrl_queue_cnt--;
2699 sctp_free_remote_addr(chk->whoTo);
2700 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
2701 sctppcbinfo.ipi_count_chunk--;
2702 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
2703 panic("Chunk count is negative");
2705 sctppcbinfo.ipi_gencnt_chunk++;
2706 /* we can only have one of these so we break */
2714 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
2715 struct sctp_stream_reset_response *resp)
2718 int number_entries, param_length;
2720 param_length = ntohs(resp->ph.param_length);
2721 seq = ntohl(resp->reset_req_seq_resp);
2722 if (seq == stcb->asoc.str_reset_seq_out) {
2723 sctp_clean_up_stream_reset(stcb);
2724 stcb->asoc.str_reset_seq_out++;
2725 stcb->asoc.stream_reset_outstanding = 0;
2726 tsn = ntohl(resp->reset_at_tsn);
2727 number_entries = (param_length - sizeof(struct sctp_stream_reset_response))/sizeof(uint16_t);
2729 if ((tsn == stcb->asoc.cumulative_tsn) ||
2730 (compare_with_wrap(stcb->asoc.cumulative_tsn, tsn, MAX_TSN))) {
2731 /* no problem we are good to go */
2732 sctp_reset_in_stream(stcb, resp, number_entries);
2734 /* So, we have a stream reset but there
2735 * is pending data. We need to copy
2736 * out the stream_reset and then queue
2737 * any data = or > resp->reset_at_tsn
2739 if (stcb->asoc.pending_reply != NULL) {
2741 * This IS WRONG. We need
2742 * to queue each of these up
2743 * and only release the chunks
2744 * for each reset that the cum-ack
2745 * goes by. This is a short cut.
2747 FREE(stcb->asoc.pending_reply, M_PCB);
2749 MALLOC(stcb->asoc.pending_reply, struct sctp_stream_reset_response *, param_length,
2751 memcpy(stcb->asoc.pending_reply, resp, param_length);
2757 printf("Duplicate old stream reset resp next:%x this one:%x\n",
2758 stcb->asoc.str_reset_seq_out, seq);
2765 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct sctp_stream_reset_req *sr_req)
2767 int chk_length, param_len;
2768 struct sctp_paramhdr *ph;
2769 /* now it may be a reset or a reset-response */
2770 struct sctp_stream_reset_request *req;
2771 struct sctp_stream_reset_response *resp;
2772 chk_length = ntohs(sr_req->ch.chunk_length);
2774 ph = (struct sctp_paramhdr *)&sr_req->sr_req;
2775 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_request)) {
2776 param_len = ntohs(ph->param_length);
2777 if (ntohs(ph->param_type) == SCTP_STR_RESET_REQUEST) {
2778 /* this will send the ACK and do the reset if needed */
2779 req = (struct sctp_stream_reset_request *)ph;
2780 sctp_send_str_reset_ack(stcb, req);
2781 } else if (ntohs(ph->param_type) == SCTP_STR_RESET_RESPONSE) {
2782 /* Now here is a tricky one. We reset our receive side
2783 * of the streams. But what happens if the peers
2784 * next sending TSN is NOT equal to 1 minus our cumack?
2785 * And if his cumack is not equal to our next one out - 1
2786 * we have another problem if this is receprical.
2788 resp = (struct sctp_stream_reset_response *)ph;
2789 sctp_handle_stream_reset_response(stcb, resp);
2791 ph = (struct sctp_paramhdr *)((caddr_t)ph + SCTP_SIZE32(param_len));
2792 chk_length -= SCTP_SIZE32(param_len);
2797 * Handle a router or endpoints report of a packet loss, there
2798 * are two ways to handle this, either we get the whole packet
2799 * and must disect it ourselves (possibly with truncation and
2800 * or corruption) or it is a summary from a middle box that did
2801 * the disectting for us.
2804 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
2805 struct sctp_tcb *stcb, struct sctp_nets *net)
2807 u_int32_t bottle_bw, on_queue;
2808 u_int16_t trunc_len;
2811 struct sctp_chunk_desc desc;
2812 struct sctp_chunkhdr *ch;
2814 chlen = ntohs(cp->ch.chunk_length);
2815 chlen -= sizeof(struct sctp_pktdrop_chunk);
2816 /* XXX possible chlen underflow */
2819 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
2820 sctp_pegs[SCTP_PDRP_BWRPT]++;
2822 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
2823 chlen -= sizeof(struct sctphdr);
2824 /* XXX possible chlen underflow */
2825 memset(&desc, 0, sizeof(desc));
2828 /* first update a rwnd possibly */
2829 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
2830 /* From a peer, we get a rwnd report */
2833 sctp_pegs[SCTP_PDRP_FEHOS]++;
2835 bottle_bw = ntohl(cp->bottle_bw);
2836 on_queue = ntohl(cp->current_onq);
2837 if (bottle_bw && on_queue) {
2838 /* a rwnd report is in here */
2839 if (bottle_bw > on_queue)
2840 a_rwnd = bottle_bw - on_queue;
2845 stcb->asoc.peers_rwnd = 0;
2847 if (a_rwnd > stcb->asoc.total_flight) {
2848 stcb->asoc.peers_rwnd =
2849 a_rwnd - stcb->asoc.total_flight;
2851 stcb->asoc.peers_rwnd = 0;
2853 if (stcb->asoc.peers_rwnd <
2854 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
2855 /* SWS sender side engages */
2856 stcb->asoc.peers_rwnd = 0;
2861 sctp_pegs[SCTP_PDRP_FMBOX]++;
2863 trunc_len = (u_int16_t)ntohs(cp->trunc_len);
2864 /* now the chunks themselves */
2865 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
2866 desc.chunk_type = ch->chunk_type;
2867 /* get amount we need to move */
2868 at = ntohs(ch->chunk_length);
2869 if (at < sizeof(struct sctp_chunkhdr)) {
2870 /* corrupt chunk, maybe at the end? */
2871 sctp_pegs[SCTP_PDRP_CRUPT]++;
2874 if (trunc_len == 0) {
2875 /* we are supposed to have all of it */
2877 /* corrupt skip it */
2878 sctp_pegs[SCTP_PDRP_CRUPT]++;
2882 /* is there enough of it left ? */
2883 if (desc.chunk_type == SCTP_DATA) {
2884 if (chlen < (sizeof(struct sctp_data_chunk) +
2885 sizeof(desc.data_bytes))) {
2889 if (chlen < sizeof(struct sctp_chunkhdr)) {
2894 if (desc.chunk_type == SCTP_DATA) {
2895 /* can we get out the tsn? */
2896 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
2897 sctp_pegs[SCTP_PDRP_MB_DA]++;
2899 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(u_int32_t)) ) {
2901 struct sctp_data_chunk *dcp;
2904 dcp = (struct sctp_data_chunk *)ch;
2905 ddp = (u_int8_t *)(dcp + 1);
2906 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
2907 desc.data_bytes[iii] = ddp[iii];
2909 desc.tsn_ifany = dcp->dp.tsn;
2911 /* nope we are done. */
2912 sctp_pegs[SCTP_PDRP_NEDAT]++;
2916 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
2917 sctp_pegs[SCTP_PDRP_MB_CT]++;
2920 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
2921 sctp_pegs[SCTP_PDRP_PDBRK]++;
2924 if (SCTP_SIZE32(at) > chlen) {
2927 chlen -= SCTP_SIZE32(at);
2928 if (chlen < sizeof(struct sctp_chunkhdr)) {
2929 /* done, none left */
2932 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
2935 /* now middle boxes in sat networks get a cwnd bump */
2936 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
2937 (stcb->asoc.sat_t3_loss_recovery == 0) &&
2938 (stcb->asoc.sat_network)) {
2940 * This is debateable but for sat networks it makes sense
2941 * Note if a T3 timer has went off, we will prohibit any
2942 * changes to cwnd until we exit the t3 loss recovery.
2946 #ifdef SCTP_CWND_LOGGING
2947 int old_cwnd=net->cwnd;
2949 /* need real RTT for this calc */
2950 rtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2951 /* get bottle neck bw */
2952 bottle_bw = ntohl(cp->bottle_bw);
2953 /* and whats on queue */
2954 on_queue = ntohl(cp->current_onq);
2956 * adjust the on-queue if our flight is more it could be
2957 * that the router has not yet gotten data "in-flight" to it
2959 if (on_queue < net->flight_size)
2960 on_queue = net->flight_size;
2962 /* calculate the available space */
2963 bw_avail = (bottle_bw*rtt)/1000;
2964 if (bw_avail > bottle_bw) {
2966 * Cap the growth to no more than the bottle neck.
2967 * This can happen as RTT slides up due to queues.
2968 * It also means if you have more than a 1 second
2969 * RTT with a empty queue you will be limited to
2970 * the bottle_bw per second no matter if
2971 * other points have 1/2 the RTT and you could
2974 bw_avail = bottle_bw;
2977 if (on_queue > bw_avail) {
2979 * No room for anything else don't allow anything
2980 * else to be "added to the fire".
2982 int seg_inflight, seg_onqueue, my_portion;
2983 net->partial_bytes_acked = 0;
2985 /* how much are we over queue size? */
2986 incr = on_queue - bw_avail;
2987 if (stcb->asoc.seen_a_sack_this_pkt) {
2988 /* undo any cwnd adjustment that
2989 * the sack might have made
2991 net->cwnd = net->prev_cwnd;
2994 /* Now how much of that is mine? */
2995 seg_inflight = net->flight_size / net->mtu;
2996 seg_onqueue = on_queue / net->mtu;
2997 my_portion = (incr * seg_inflight)/seg_onqueue;
2999 /* Have I made an adjustment already */
3000 if (net->cwnd > net->flight_size) {
3001 /* for this flight I made an adjustment
3002 * we need to decrease the portion by a share
3003 * our previous adjustment.
3006 diff_adj = net->cwnd - net->flight_size;
3007 if (diff_adj > my_portion)
3010 my_portion -= diff_adj;
3013 /* back down to the previous cwnd (assume
3014 * we have had a sack before this packet). minus
3015 * what ever portion of the overage is my fault.
3017 net->cwnd -= my_portion;
3019 /* we will NOT back down more than 1 MTU */
3020 if (net->cwnd <= net->mtu) {
3021 net->cwnd = net->mtu;
3024 net->ssthresh = net->cwnd - 1;
3027 * Take 1/4 of the space left or
3028 * max burst up .. whichever is less.
3030 incr = min((bw_avail - on_queue) >> 2,
3031 (int)stcb->asoc.max_burst * (int)net->mtu);
3034 if (net->cwnd > bw_avail) {
3035 /* We can't exceed the pipe size */
3036 net->cwnd = bw_avail;
3038 if (net->cwnd < net->mtu) {
3039 /* We always have 1 MTU */
3040 net->cwnd = net->mtu;
3042 #ifdef SCTP_CWND_LOGGING
3043 if (net->cwnd - old_cwnd != 0) {
3044 /* log only changes */
3045 sctp_log_cwnd(net, (net->cwnd - old_cwnd),
3046 SCTP_CWND_LOG_FROM_SAT);
3052 extern int sctp_strict_init;
3055 * handles all control chunks in a packet
3057 * - m: mbuf chain, assumed to still contain IP/SCTP header
3058 * - stcb: is the tcb found for this packet
3059 * - offset: offset into the mbuf chain to first chunkhdr
3060 * - length: is the length of the complete packet
3062 * - length: modified to remaining length after control processing
3063 * - netp: modified to new sctp_nets after cookie-echo processing
3064 * - return NULL to discard the packet (ie. no asoc, bad packet,...)
3065 * otherwise return the tcb for this packet
3067 static struct sctp_tcb *
3068 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
3069 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
3070 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen)
3072 struct sctp_association *asoc;
3074 int num_chunks = 0; /* number of control chunks processed */
3079 * How big should this be, and should it be alloc'd?
3080 * Lets try the d-mtu-ceiling for now (2k) and that should
3081 * hopefully work ... until we get into jumbo grams and such..
3083 u_int8_t chunk_buf[DEFAULT_CHUNK_BUFFER];
3084 struct sctp_tcb *locked_tcb = stcb;
3087 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3088 printf("sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
3089 iphlen, *offset, length, stcb);
3091 #endif /* SCTP_DEBUG */
3093 /* validate chunk header length... */
3094 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
3099 * validate the verification tag
3102 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3103 printf("sctp_process_control: validating vtags\n");
3105 #endif /* SCTP_DEBUG */
3106 vtag_in = ntohl(sh->v_tag);
3107 if (ch->chunk_type == SCTP_INITIATION) {
3109 /* protocol error- silently discard... */
3111 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3112 printf("sctp_process_control: INIT with vtag != 0\n");
3114 #endif /* SCTP_DEBUG */
3115 sctp_pegs[SCTP_BAD_VTAGS]++;
3117 SCTP_TCB_UNLOCK(locked_tcb);
3120 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
3122 * first check if it's an ASCONF with an unknown src addr
3123 * we need to look inside to find the association
3125 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
3126 stcb = sctp_findassociation_ep_asconf(m, iphlen,
3127 *offset, sh, &inp, netp);
3130 /* no association, so it's out of the blue... */
3131 sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL);
3133 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3134 printf("sctp_process_control: handling OOTB packet, chunk type=%xh\n",
3137 #endif /* SCTP_DEBUG */
3140 SCTP_TCB_UNLOCK(locked_tcb);
3144 /* ABORT and SHUTDOWN can use either v_tag... */
3145 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
3146 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
3147 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
3148 if ((vtag_in == asoc->my_vtag) ||
3149 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
3150 (vtag_in == asoc->peer_vtag))) {
3153 /* drop this packet... */
3154 sctp_pegs[SCTP_BAD_VTAGS]++;
3156 SCTP_TCB_UNLOCK(locked_tcb);
3159 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
3160 if (vtag_in != asoc->my_vtag) {
3162 * this could be a stale SHUTDOWN-ACK or the
3163 * peer never got the SHUTDOWN-COMPLETE and
3164 * is still hung; we have started a new asoc
3165 * but it won't complete until the shutdown is
3169 SCTP_TCB_UNLOCK(locked_tcb);
3170 sctp_handle_ootb(m, iphlen, *offset, sh, inp,
3175 /* for all other chunks, vtag must match */
3177 if (vtag_in != asoc->my_vtag) {
3178 /* invalid vtag... */
3180 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3181 printf("invalid vtag: %xh, expect %xh\n", vtag_in, asoc->my_vtag);
3183 #endif /* SCTP_DEBUG */
3184 sctp_pegs[SCTP_BAD_VTAGS]++;
3186 SCTP_TCB_UNLOCK(locked_tcb);
3191 } /* end if !SCTP_COOKIE_ECHO */
3194 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3195 printf("sctp_process_control: vtags ok, processing ctrl chunks\n");
3197 #endif /* SCTP_DEBUG */
3200 * process all control chunks...
3202 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
3203 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
3204 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
3205 /* implied cookie-ack.. we must have lost the ack */
3206 stcb->asoc.overall_error_count = 0;
3207 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
3210 while (IS_SCTP_CONTROL(ch)) {
3211 /* validate chunk length */
3212 chk_length = ntohs(ch->chunk_length);
3214 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
3215 printf("sctp_process_control: processing a chunk type=%u, len=%u\n", ch->chunk_type, chk_length);
3217 #endif /* SCTP_DEBUG */
3218 if ((size_t)chk_length < sizeof(*ch) ||
3219 (*offset + chk_length) > length) {
3221 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3222 printf("sctp_process_control: chunk length invalid! *offset:%u, chk_length:%u > length:%u\n",
3223 *offset, length, chk_length);
3225 #endif /* SCTP_DEBUG */
3228 SCTP_TCB_UNLOCK(locked_tcb);
3233 * INIT-ACK only gets the init ack "header" portion only
3234 * because we don't have to process the peer's COOKIE.
3235 * All others get a complete chunk.
3237 if (ch->chunk_type == SCTP_INITIATION_ACK) {
3238 /* get an init-ack chunk */
3239 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3240 sizeof(struct sctp_init_ack), chunk_buf);
3244 SCTP_TCB_UNLOCK(locked_tcb);
3248 /* get a complete chunk... */
3249 if ((size_t)chk_length > sizeof(chunk_buf)) {
3251 struct sctp_paramhdr *phdr;
3253 MGETHDR(oper, M_DONTWAIT, MT_HEADER);
3255 /* pre-reserve some space */
3257 sizeof(struct sctp_chunkhdr);
3259 mtod(oper, struct sctp_paramhdr *);
3261 htons(SCTP_CAUSE_OUT_OF_RESC);
3262 phdr->param_length =
3263 htons(sizeof(struct sctp_paramhdr));
3264 sctp_queue_op_err(stcb, oper);
3267 SCTP_TCB_UNLOCK(locked_tcb);
3270 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3271 chk_length, chunk_buf);
3273 printf("sctp_process_control: Can't get the all data....\n");
3276 SCTP_TCB_UNLOCK(locked_tcb);
3282 /* Save off the last place we got a control from */
3283 if ((*netp) && stcb) {
3284 stcb->asoc.last_control_chunk_from = *netp;
3286 #ifdef SCTP_AUDITING_ENABLED
3287 sctp_audit_log(0xB0, ch->chunk_type);
3289 switch (ch->chunk_type) {
3290 case SCTP_INITIATION:
3291 /* must be first and only chunk */
3293 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3294 printf("SCTP_INIT\n");
3296 #endif /* SCTP_DEBUG */
3297 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3298 /* We are not interested anymore */
3300 SCTP_TCB_UNLOCK(locked_tcb);
3301 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3302 /* finish the job now */
3303 sctp_inpcb_free(inp, 1);
3308 if ((num_chunks > 1) ||
3309 (sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) {
3312 SCTP_TCB_UNLOCK(locked_tcb);
3315 if ((stcb != NULL) &&
3316 (SCTP_GET_STATE(&stcb->asoc) ==
3317 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3318 sctp_send_shutdown_ack(stcb,
3319 stcb->asoc.primary_destination);
3322 SCTP_TCB_UNLOCK(locked_tcb);
3325 sctp_handle_init(m, iphlen, *offset, sh,
3326 (struct sctp_init_chunk *)ch, inp, stcb, *netp);
3329 SCTP_TCB_UNLOCK(locked_tcb);
3332 case SCTP_INITIATION_ACK:
3333 /* must be first and only chunk */
3335 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3336 printf("SCTP_INIT-ACK\n");
3338 #endif /* SCTP_DEBUG */
3339 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3340 /* We are not interested anymore */
3342 SCTP_TCB_UNLOCK(locked_tcb);
3345 sctp_free_assoc(inp, stcb);
3347 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3348 /* finish the job now */
3349 sctp_inpcb_free(inp, 1);
3354 if ((num_chunks > 1) ||
3355 (sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) {
3357 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3358 printf("Length is %d rounded chk_length:%d .. dropping\n",
3360 SCTP_SIZE32(chk_length));
3365 SCTP_TCB_UNLOCK(locked_tcb);
3368 ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
3369 (struct sctp_init_ack_chunk *)ch, stcb, *netp);
3371 * Special case, I must call the output routine
3372 * to get the cookie echoed
3374 if ((stcb) && ret == 0)
3375 sctp_chunk_output(stcb->sctp_ep, stcb, 2);
3378 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3379 printf("All done INIT-ACK processing\n");
3383 SCTP_TCB_UNLOCK(locked_tcb);
3386 case SCTP_SELECTIVE_ACK:
3388 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3389 printf("SCTP_SACK\n");
3391 #endif /* SCTP_DEBUG */
3392 sctp_pegs[SCTP_PEG_SACKS_SEEN]++;
3395 stcb->asoc.seen_a_sack_this_pkt = 1;
3396 sctp_handle_sack((struct sctp_sack_chunk *)ch,
3397 stcb, *netp, &abort_now);
3399 /* ABORT signal from sack processing */
3405 case SCTP_HEARTBEAT_REQUEST:
3407 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3408 printf("SCTP_HEARTBEAT\n");
3410 #endif /* SCTP_DEBUG */
3411 sctp_pegs[SCTP_HB_RECV]++;
3412 sctp_send_heartbeat_ack(stcb, m, *offset, chk_length,
3415 /* He's alive so give him credit */
3416 stcb->asoc.overall_error_count = 0;
3418 case SCTP_HEARTBEAT_ACK:
3420 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3421 printf("SCTP_HEARTBEAT-ACK\n");
3423 #endif /* SCTP_DEBUG */
3425 /* He's alive so give him credit */
3426 stcb->asoc.overall_error_count = 0;
3428 sctp_pegs[SCTP_HB_ACK_RECV]++;
3429 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
3432 case SCTP_ABORT_ASSOCIATION:
3434 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3435 printf("SCTP_ABORT\n");
3437 #endif /* SCTP_DEBUG */
3438 sctp_handle_abort((struct sctp_abort_chunk *)ch,
3445 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3446 printf("SCTP_SHUTDOWN\n");
3448 #endif /* SCTP_DEBUG */
3451 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
3452 stcb, *netp, &abort_flag);
3459 case SCTP_SHUTDOWN_ACK:
3461 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3462 printf("SCTP_SHUTDOWN-ACK\n");
3464 #endif /* SCTP_DEBUG */
3465 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
3469 case SCTP_OPERATION_ERROR:
3471 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3472 printf("SCTP_OP-ERR\n");
3474 #endif /* SCTP_DEBUG */
3475 if (sctp_handle_error(ch, stcb, *netp) < 0) {
3480 case SCTP_COOKIE_ECHO:
3482 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3483 printf("SCTP_COOKIE-ECHO stcb is %p\n", stcb);
3485 #endif /* SCTP_DEBUG */
3486 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3487 /* We are not interested anymore */
3490 sctp_free_assoc(inp, stcb);
3492 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3493 /* finish the job now */
3494 sctp_inpcb_free(inp, 1);
3500 * First are we accepting?
3501 * We do this again here since it is possible
3502 * that a previous endpoint WAS listening responded to
3503 * a INIT-ACK and then closed. We opened and bound..
3504 * and are now no longer listening.
3506 if (((inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) == 0) ||
3507 (inp->sctp_socket->so_qlimit == 0)) {
3508 sctp_abort_association(inp, stcb, m, iphlen, sh,
3512 } else if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
3513 /* we are accepting so check limits like TCP */
3514 if (inp->sctp_socket->so_qlen >
3515 inp->sctp_socket->so_qlimit) {
3518 struct sctp_paramhdr *phdr;
3520 MGETHDR(oper, M_DONTWAIT, MT_HEADER);
3523 oper->m_pkthdr.len =
3524 sizeof(struct sctp_paramhdr);
3526 struct sctp_paramhdr *);
3528 htons(SCTP_CAUSE_OUT_OF_RESC);
3529 phdr->param_length =
3530 htons(sizeof(struct sctp_paramhdr));
3532 sctp_abort_association(inp, stcb, m,
3539 struct mbuf *ret_buf;
3540 ret_buf = sctp_handle_cookie_echo(m, iphlen,
3542 (struct sctp_cookie_echo_chunk *)ch, &inp,
3545 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3546 printf("ret_buf:%p length:%d off:%d\n",
3547 ret_buf, length, *offset);
3549 #endif /* SCTP_DEBUG */
3551 if (ret_buf == NULL) {
3553 SCTP_TCB_UNLOCK(locked_tcb);
3556 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3557 printf("GAK, null buffer\n");
3559 #endif /* SCTP_DEBUG */
3563 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
3565 * Restart the timer if we have pending
3568 struct sctp_tmit_chunk *chk;
3569 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
3571 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
3572 stcb->sctp_ep, stcb,
3578 case SCTP_COOKIE_ACK:
3580 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3581 printf("SCTP_COOKIE-ACK\n");
3583 #endif /* SCTP_DEBUG */
3585 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3586 /* We are not interested anymore */
3587 sctp_free_assoc(inp, stcb);
3591 /* He's alive so give him credit */
3592 stcb->asoc.overall_error_count = 0;
3593 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch,
3598 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3599 printf("SCTP_ECN-ECHO\n");
3601 #endif /* SCTP_DEBUG */
3602 /* He's alive so give him credit */
3603 stcb->asoc.overall_error_count = 0;
3604 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
3609 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3610 printf("SCTP_ECN-CWR\n");
3612 #endif /* SCTP_DEBUG */
3613 /* He's alive so give him credit */
3614 stcb->asoc.overall_error_count = 0;
3616 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb);
3618 case SCTP_SHUTDOWN_COMPLETE:
3620 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3621 printf("SCTP_SHUTDOWN-COMPLETE\n");
3623 #endif /* SCTP_DEBUG */
3624 /* must be first and only chunk */
3625 if ((num_chunks > 1) ||
3626 (length - *offset > SCTP_SIZE32(chk_length))) {
3629 SCTP_TCB_UNLOCK(locked_tcb);
3633 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
3640 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3641 printf("SCTP_ASCONF\n");
3643 #endif /* SCTP_DEBUG */
3644 /* He's alive so give him credit */
3645 stcb->asoc.overall_error_count = 0;
3647 sctp_handle_asconf(m, *offset,
3648 (struct sctp_asconf_chunk *)ch, stcb, *netp);
3650 case SCTP_ASCONF_ACK:
3652 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3653 printf("SCTP_ASCONF-ACK\n");
3655 #endif /* SCTP_DEBUG */
3656 /* He's alive so give him credit */
3657 stcb->asoc.overall_error_count = 0;
3659 sctp_handle_asconf_ack(m, *offset,
3660 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp);
3662 case SCTP_FORWARD_CUM_TSN:
3664 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3665 printf("SCTP_FWD-TSN\n");
3667 #endif /* SCTP_DEBUG */
3668 /* He's alive so give him credit */
3671 stcb->asoc.overall_error_count = 0;
3673 sctp_handle_forward_tsn(stcb,
3674 (struct sctp_forward_tsn_chunk *)ch, &abort_flag);
3679 stcb->asoc.overall_error_count = 0;
3684 case SCTP_STREAM_RESET:
3686 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3687 printf("SCTP_STREAM_RESET\n");
3689 #endif /* SCTP_DEBUG */
3690 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3691 chk_length, chunk_buf);
3692 if (stcb->asoc.peer_supports_strreset == 0) {
3693 /* hmm, peer should have annonced this, but
3694 * we will turn it on since he is sending us
3697 stcb->asoc.peer_supports_strreset = 1;
3699 sctp_handle_stream_reset(stcb, (struct sctp_stream_reset_req *)ch);
3701 case SCTP_PACKET_DROPPED:
3703 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3704 printf("SCTP_PACKET_DROPPED\n");
3706 #endif /* SCTP_DEBUG */
3707 /* re-get it all please */
3708 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3709 chk_length, chunk_buf);
3711 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
3717 /* it's an unknown chunk! */
3718 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
3720 struct sctp_paramhdr *phd;
3721 MGETHDR(mm, M_DONTWAIT, MT_HEADER);
3723 phd = mtod(mm, struct sctp_paramhdr *);
3724 /* We cheat and use param type since we
3725 * did not bother to define a error
3727 * They are the same basic format with
3731 htons(SCTP_CAUSE_UNRECOG_CHUNK);
3733 htons(chk_length + sizeof(*phd));
3734 mm->m_len = sizeof(*phd);
3735 mm->m_next = sctp_m_copym(m, *offset,
3736 SCTP_SIZE32(chk_length),
3740 SCTP_SIZE32(chk_length) +
3742 sctp_queue_op_err(stcb, mm);
3747 SCTP_DEBUG_INPUT1) {
3748 printf("Gak can't copy the chunk into operr %d bytes\n",
3756 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3757 printf("Gak can't mgethdr for op-err of unrec chunk\n");
3762 if ((ch->chunk_type & 0x80) == 0) {
3763 /* discard this packet */
3766 } /* else skip this bad chunk and continue... */
3768 } /* switch (ch->chunk_type) */
3769 /* get the next chunk */
3770 *offset += SCTP_SIZE32(chk_length);
3771 if (*offset >= length) {
3772 /* no more data left in the mbuf chain */
3775 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3776 sizeof(struct sctp_chunkhdr), chunk_buf);
3779 SCTP_TCB_UNLOCK(locked_tcb);
3789 * Process the ECN bits we have something set so
3790 * we must look to see if it is ECN(0) or ECN(1) or CE
3793 sctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net,
3796 if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
3798 } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) {
3800 * we only add to the nonce sum for ECT1, ECT0
3801 * does not change the NS bit (that we have
3802 * yet to find a way to send it yet).
3805 /* ECN Nonce stuff */
3806 stcb->asoc.receiver_nonce_sum++;
3807 stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM;
3810 * Drag up the last_echo point if cumack is larger since we
3811 * don't want the point falling way behind by more than 2^^31
3812 * and then having it be incorrect.
3814 if (compare_with_wrap(stcb->asoc.cumulative_tsn,
3815 stcb->asoc.last_echo_tsn, MAX_TSN)) {
3816 stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
3818 } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) {
3820 * Drag up the last_echo point if cumack is larger since we
3821 * don't want the point falling way behind by more than 2^^31
3822 * and then having it be incorrect.
3824 if (compare_with_wrap(stcb->asoc.cumulative_tsn,
3825 stcb->asoc.last_echo_tsn, MAX_TSN)) {
3826 stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
3832 sctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net,
3833 u_int32_t high_tsn, u_int8_t ecn_bits)
3835 if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
3837 * we possibly must notify the sender that a congestion
3838 * window reduction is in order. We do this
3839 * by adding a ECNE chunk to the output chunk
3840 * queue. The incoming CWR will remove this chunk.
3842 if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn,
3844 /* Yep, we need to add a ECNE */
3845 sctp_send_ecn_echo(stcb, net, high_tsn);
3846 stcb->asoc.last_echo_tsn = high_tsn;
3852 * common input chunk processing (v4 and v6)
3855 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
3856 int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
3857 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
3861 * Control chunk processing
3864 int fwd_tsn_seen = 0, data_processed = 0;
3865 struct mbuf *m = *mm;
3868 sctp_pegs[SCTP_DATAGRAMS_RCVD]++;
3869 #ifdef SCTP_AUDITING_ENABLED
3870 sctp_audit_log(0xE0, 1);
3871 sctp_auditing(0, inp, stcb, net);
3875 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3876 printf("Ok, Common input processing called, m:%x iphlen:%d offset:%d\n",
3877 (u_int)m, iphlen, offset);
3879 #endif /* SCTP_DEBUG */
3880 if (IS_SCTP_CONTROL(ch)) {
3881 /* process the control portion of the SCTP packet */
3883 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3884 printf("Processing control\n");
3886 #endif /* SCTP_DEBUG */
3888 stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
3889 inp, stcb, &net, &fwd_tsn_seen);
3892 * no control chunks, so pre-process DATA chunks
3893 * (these checks are taken care of by control processing)
3896 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3897 printf("No control present\n");
3899 #endif /* SCTP_DEBUG */
3902 /* out of the blue DATA chunk */
3903 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL);
3906 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
3907 /* v_tag mismatch! */
3908 sctp_pegs[SCTP_BAD_VTAGS]++;
3909 SCTP_TCB_UNLOCK(stcb);
3915 * no valid TCB for this packet,
3916 * or we found it's a bad packet while processing control,
3917 * or we're done with this packet (done or skip rest of data),
3923 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3924 printf("Ok, control finished time to look for data (%d) offset:%d\n",
3927 #endif /* SCTP_DEBUG */
3929 * DATA chunk processing
3931 /* plow through the data chunks while length > offset */
3932 stcb->asoc.seen_a_sack_this_pkt = 0;
3934 if (length > offset) {
3937 * First check to make sure our state is correct.
3938 * We would not get here unless we really did have a
3939 * tag, so we don't abort if this happens, just
3940 * dump the chunk silently.
3942 switch (SCTP_GET_STATE(&stcb->asoc)) {
3943 case SCTP_STATE_COOKIE_ECHOED:
3945 * we consider data with valid tags in
3946 * this state shows us the cookie-ack was lost.
3947 * Imply it was there.
3949 stcb->asoc.overall_error_count = 0;
3950 sctp_handle_cookie_ack(
3951 (struct sctp_cookie_ack_chunk *)ch, stcb, net);
3953 case SCTP_STATE_COOKIE_WAIT:
3955 * We consider OOTB any data sent during asoc setup.
3957 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL);
3958 SCTP_TCB_UNLOCK(stcb);
3961 case SCTP_STATE_EMPTY: /* should not happen */
3962 case SCTP_STATE_INUSE: /* should not happen */
3963 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
3964 case SCTP_STATE_SHUTDOWN_ACK_SENT:
3967 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3968 printf("Got data in invalid state %d.. dropping\n", stcb->asoc.state);
3971 SCTP_TCB_UNLOCK(stcb);
3974 case SCTP_STATE_OPEN:
3975 case SCTP_STATE_SHUTDOWN_SENT:
3978 /* take care of ECN, part 1. */
3979 if (stcb->asoc.ecn_allowed &&
3980 (ecn_bits & (SCTP_ECT0_BIT|SCTP_ECT1_BIT)) ) {
3981 sctp_process_ecn_marked_a(stcb, net, ecn_bits);
3983 /* plow through the data chunks while length > offset */
3984 retval = sctp_process_data(mm, iphlen, &offset, length, sh,
3985 inp, stcb, net, &high_tsn);
3987 /* The association aborted, NO UNLOCK needed
3988 * since the association is destroyed.
3995 /* take care of ecn part 2. */
3996 if (stcb->asoc.ecn_allowed && (ecn_bits & (SCTP_ECT0_BIT|SCTP_ECT1_BIT)) ) {
3997 sctp_process_ecn_marked_b(stcb, net, high_tsn, ecn_bits);
4003 * Anything important needs to have been m_copy'ed in
4007 if ((data_processed == 0) && (fwd_tsn_seen)) {
4009 if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
4010 stcb->asoc.cumulative_tsn, MAX_TSN)) {
4011 /* there was a gap before this data was processed */
4014 sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
4016 /* Again, we aborted so NO UNLOCK needed */
4020 /* trigger send of any chunks in queue... */
4021 #ifdef SCTP_AUDITING_ENABLED
4022 sctp_audit_log(0xE0, 2);
4023 sctp_auditing(1, inp, stcb, net);
4026 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4027 printf("Check for chunk output prw:%d tqe:%d tf=%d\n",
4028 stcb->asoc.peers_rwnd,
4029 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
4030 stcb->asoc.total_flight);
4033 if (stcb->asoc.peers_rwnd > 0 ||
4034 !TAILQ_EMPTY(&stcb->asoc.control_send_queue) ||
4035 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)) {
4037 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4038 printf("Calling chunk OUTPUT\n");
4041 sctp_chunk_output(inp, stcb, 3);
4043 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4044 printf("chunk OUTPUT returns\n");
4049 #ifdef SCTP_AUDITING_ENABLED
4050 sctp_audit_log(0xE0, 3);
4051 sctp_auditing(2, inp, stcb, net);
4053 SCTP_TCB_UNLOCK(stcb);
4057 #if defined(__OpenBSD__)
4059 sctp_saveopt(struct sctp_inpcb *inp, struct mbuf **mp, struct ip *ip,
4062 if (inp->ip_inp.inp.inp_flags & INP_RECVDSTADDR) {
4063 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
4064 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
4066 mp = &(*mp)->m_next;
4071 extern int sctp_no_csum_on_loopback;
4073 #if defined(__FreeBSD__) || defined(__APPLE__)
4081 sctp_input(struct mbuf *m, ...)
4083 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__))
4119 iphlen = off = va_arg(ap, int);
4125 sctp_pegs[SCTP_INPKTS]++;
4127 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4128 printf("V4 input gets a packet iphlen:%d pktlen:%d\n", iphlen, m->m_pkthdr.len);
4132 /* Don't think this is needed */
4133 /* bzero(&opts6, sizeof(opts6));*/
4137 * Strip IP options, we don't allow any in or out.
4139 if ((size_t)iphlen > sizeof(struct ip)) {
4140 ip_stripoptions(m, (struct mbuf *)0);
4141 iphlen = sizeof(struct ip);
4145 * Get IP, SCTP, and first chunk header together in first mbuf.
4147 ip = mtod(m, struct ip *);
4148 offset = iphlen + sizeof(*sh) + sizeof(*ch);
4149 if (m->m_len < offset) {
4150 if ((m = m_pullup(m, offset)) == 0) {
4151 sctp_pegs[SCTP_HDR_DROPS]++;
4154 ip = mtod(m, struct ip *);
4156 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
4157 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
4159 /* SCTP does not allow broadcasts or multicasts */
4160 #if defined(__NetBSD__) || defined(__OpenBSD__)
4161 if (IN_MULTICAST(ip->ip_dst.s_addr))
4163 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)))
4166 sctp_pegs[SCTP_IN_MCAST]++;
4169 if (in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
4170 sctp_pegs[SCTP_IN_MCAST]++;
4174 /* destination port of 0 is illegal, based on RFC2960. */
4175 if (sh->dest_port == 0) {
4176 sctp_pegs[SCTP_HDR_DROPS]++;
4180 /* validate SCTP checksum */
4181 if ((sctp_no_csum_on_loopback == 0) ||
4182 (m->m_pkthdr.rcvif == NULL) ||
4183 (m->m_pkthdr.rcvif->if_type != IFT_LOOP)) {
4184 /* we do NOT validate things from the loopback if the
4185 * sysctl is set to 1.
4187 check = sh->checksum; /* save incoming checksum */
4188 if ((check == 0) && (sctp_no_csum_on_loopback)) {
4189 /* special hook for where we got a local address
4190 * somehow routed across a non IFT_LOOP type interface
4192 if (ip->ip_src.s_addr == ip->ip_dst.s_addr)
4193 goto sctp_skip_csum_4;
4195 sh->checksum = 0; /* prepare for calc */
4196 calc_check = sctp_calculate_sum(m, &mlen, iphlen);
4197 if (calc_check != check) {
4199 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4200 printf("Bad CSUM on SCTP packet calc_check:%x check:%x m:%x mlen:%d iphlen:%d\n",
4201 calc_check, check, (u_int)m, mlen, iphlen);
4205 stcb = sctp_findassociation_addr(m, iphlen,
4206 offset - sizeof(*ch),
4207 sh, ch, &inp, &net);
4208 if ((inp) && (stcb)) {
4209 sctp_send_packet_dropped(stcb, net, m, iphlen,
4211 sctp_chunk_output(inp, stcb, 2);
4212 } else if ((inp != NULL) && (stcb == NULL)) {
4215 sctp_pegs[SCTP_BAD_CSUM]++;
4218 sh->checksum = calc_check;
4221 mlen = m->m_pkthdr.len;
4223 /* validate mbuf chain length with IP payload length */
4224 #if defined(__NetBSD__) || defined(__OpenBSD__)
4225 /* Open BSD gives us the len in network order, fix it */
4228 if (mlen < (ip->ip_len - iphlen)) {
4229 sctp_pegs[SCTP_HDR_DROPS]++;
4234 * Locate pcb and tcb for datagram
4235 * sctp_findassociation_addr() wants IP/SCTP/first chunk header...
4238 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4239 printf("V4 find association\n");
4243 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
4244 sh, ch, &inp, &net);
4245 /* inp's ref-count increased && stcb locked */
4247 struct sctp_init_chunk *init_chk, chunk_buf;
4249 sctp_pegs[SCTP_NOPORTS]++;
4252 * we use the bandwidth limiting to protect against
4253 * sending too many ABORTS all at once. In this case
4254 * these count the same as an ICMP message.
4256 if (badport_bandlim(0) < 0)
4258 #endif /* ICMP_BANDLIM */
4260 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4261 printf("Sending a ABORT from packet entry!\n");
4264 if (ch->chunk_type == SCTP_INITIATION) {
4265 /* we do a trick here to get the INIT tag,
4266 * dig in and get the tag from the INIT and
4267 * put it in the common header.
4269 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4270 iphlen + sizeof(*sh), sizeof(*init_chk),
4271 (u_int8_t *)&chunk_buf);
4272 if (init_chk != NULL)
4273 sh->v_tag = init_chk->init.initiate_tag;
4275 sctp_send_abort(m, iphlen, sh, 0, NULL);
4277 } else if (stcb == NULL) {
4282 * I very much doubt any of the IPSEC stuff will work but I have
4283 * no idea, so I will leave it in place.
4287 /* FIX ME: this don't work... :) */
4289 /* Find most recent IPsec tag */
4290 i_inp = &inp->ip_inp.inp;
4291 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
4294 tdbi = (struct tdb_ident *)(mtag + 1);
4295 tdb = gettdb(tdbi->spi, &tdbi->dst, tdbi->proto);
4298 ipsp_spd_lookup(m, af, iphlen, &error, IPSP_DIRECTION_IN,
4302 sctp_pegs[SCTP_HDR_DROPS]++;
4307 if (i_inp->inp_tdb_in != tdb) {
4309 tdb_add_inp(tdb, i_inp, 1);
4310 if (i_inp->inp_ipo == NULL) {
4311 i_inp->inp_ipo = ipsec_add_policy(i_inp, af,
4312 IPSP_DIRECTION_OUT);
4313 if (i_inp->inp_ipo == NULL) {
4315 sctp_pegs[SCTP_HDR_DROPS]++;
4319 if (i_inp->inp_ipo->ipo_dstid == NULL &&
4320 tdb->tdb_srcid != NULL) {
4321 i_inp->inp_ipo->ipo_dstid = tdb->tdb_srcid;
4322 tdb->tdb_srcid->ref_count++;
4324 if (i_inp->inp_ipsec_remotecred == NULL &&
4325 tdb->tdb_remote_cred != NULL) {
4326 i_inp->inp_ipsec_remotecred =
4327 tdb->tdb_remote_cred;
4328 tdb->tdb_remote_cred->ref_count++;
4330 if (i_inp->inp_ipsec_remoteauth == NULL &&
4331 tdb->tdb_remote_auth != NULL) {
4332 i_inp->inp_ipsec_remoteauth =
4333 tdb->tdb_remote_auth;
4334 tdb->tdb_remote_auth->ref_count++;
4336 } else { /* Just reset */
4337 TAILQ_REMOVE(&i_inp->inp_tdb_in->tdb_inp_in, i_inp,
4339 i_inp->inp_tdb_in = NULL;
4345 if (ipsec4_in_reject_so(m, inp->ip_inp.inp.inp_socket)) {
4346 ipsecstat.in_polvio++;
4347 sctp_pegs[SCTP_HDR_DROPS]++;
4354 * Construct sockaddr format source address.
4355 * Stuff source address and datagram in user buffer.
4357 if ((inp->ip_inp.inp.inp_flags & INP_CONTROLOPTS)
4359 || (inp->sctp_socket->so_options & SO_TIMESTAMP)
4363 sctp_saveopt(inp, &opts, ip, m);
4365 ip_savecontrol((struct inpcb *)inp, &opts, ip, m);
4370 * common chunk processing
4372 #if defined(__FreeBSD__) || defined(__APPLE__)
4373 length = ip->ip_len + iphlen;
4375 length = ip->ip_len - (ip->ip_hl << 2) + iphlen;
4377 offset -= sizeof(struct sctp_chunkhdr);
4379 ecn_bits = ip->ip_tos;
4380 #if defined(__NetBSD__) || defined(__OpenBSD__)
4385 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
4386 inp, stcb, net, ecn_bits);
4387 /* inp's ref-count reduced && stcb unlocked */
4395 if ((inp) && (refcount_up)) {
4396 /* reduce ref-count */
4397 SCTP_INP_WLOCK(inp);
4398 SCTP_INP_DECR_REF(inp);
4399 SCTP_INP_WUNLOCK(inp);
4405 SCTP_TCB_UNLOCK(stcb);
4407 if ((inp) && (refcount_up)) {
4408 /* reduce ref-count */
4409 SCTP_INP_WLOCK(inp);
4410 SCTP_INP_DECR_REF(inp);
4411 SCTP_INP_WUNLOCK(inp);