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.13 2008/04/20 13:44:25 swildner Exp $ */
5 * Copyright (C) 2002, 2003, 2004 Cisco Systems Inc,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if !(defined(__OpenBSD__) || defined(__APPLE__))
34 #include "opt_ipsec.h"
36 #if defined(__FreeBSD__) || defined(__DragonFly__)
37 #include "opt_compat.h"
38 #include "opt_inet6.h"
41 #if defined(__NetBSD__)
46 #elif !defined(__OpenBSD__)
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/malloc.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/socketvar2.h>
57 #include <sys/sysctl.h>
58 #include <sys/domain.h>
59 #include <sys/protosw.h>
60 #include <sys/kernel.h>
61 #include <sys/errno.h>
62 #include <sys/syslog.h>
63 #include <sys/thread2.h>
65 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
66 #include <sys/limits.h>
68 #include <machine/limits.h>
70 #include <machine/cpu.h>
73 #include <net/route.h>
74 #include <net/if_types.h>
76 #include <netinet/in.h>
77 #include <netinet/in_systm.h>
78 #include <netinet/ip.h>
79 #include <netinet/in_pcb.h>
80 #include <netinet/in_var.h>
81 #include <netinet/ip_var.h>
84 #include <netinet/ip6.h>
85 #include <netinet6/ip6_var.h>
88 #include <netinet/ip_icmp.h>
89 #include <netinet/icmp_var.h>
90 #include <netinet/sctp_var.h>
91 #include <netinet/sctp_pcb.h>
92 #include <netinet/sctp_header.h>
93 #include <netinet/sctputil.h>
94 #include <netinet/sctp_output.h>
95 #include <netinet/sctp_input.h>
96 #include <netinet/sctp_hashdriver.h>
97 #include <netinet/sctp_indata.h>
98 #include <netinet/sctp_asconf.h>
102 #elif !defined(__FreeBSD__)
103 #include <machine/stdarg.h>
108 #include <netinet6/ipsec.h>
109 #include <netproto/key/key.h>
115 #include <net/net_osdep.h>
118 extern u_int32_t sctp_debug_on;
123 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
124 struct sctphdr *sh, struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
125 struct sctp_tcb *stcb, struct sctp_nets *net)
127 struct sctp_init *init;
130 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
131 kprintf("sctp_handle_init: handling INIT tcb:%p\n", stcb);
136 /* First are we accepting? */
137 if (((inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) == 0) ||
138 (inp->sctp_socket->so_qlimit == 0)) {
139 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
142 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
144 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
145 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
148 /* validate parameters */
149 if (init->initiate_tag == 0) {
150 /* protocol error... send abort */
151 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
152 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
155 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
156 /* invalid parameter... send abort */
157 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
158 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
161 if (init->num_inbound_streams == 0) {
162 /* protocol error... send abort */
163 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
164 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
167 if (init->num_outbound_streams == 0) {
168 /* protocol error... send abort */
169 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
170 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
174 /* send an INIT-ACK w/cookie */
176 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
177 kprintf("sctp_handle_init: sending INIT-ACK\n");
181 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp);
185 * process peer "INIT/INIT-ACK" chunk
186 * returns value < 0 on error
190 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb,
191 struct sctp_nets *net)
193 struct sctp_init *init;
194 struct sctp_association *asoc;
195 struct sctp_nets *lnet;
200 /* save off parameters */
201 asoc->peer_vtag = ntohl(init->initiate_tag);
202 asoc->peers_rwnd = ntohl(init->a_rwnd);
204 if (TAILQ_FIRST(&asoc->nets)) {
205 /* update any ssthresh's that may have a default */
206 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
207 lnet->ssthresh = asoc->peers_rwnd;
210 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
212 struct sctp_stream_out *outs;
213 struct sctp_tmit_chunk *chk;
215 /* cut back on number of streams */
216 newcnt = ntohs(init->num_inbound_streams);
217 /* This if is probably not needed but I am cautious */
219 /* First make sure no data chunks are trapped */
220 for (i=newcnt; i < asoc->pre_open_streams; i++) {
221 outs = &asoc->strmout[i];
222 chk = TAILQ_FIRST(&outs->outqueue);
224 TAILQ_REMOVE(&outs->outqueue, chk,
226 asoc->stream_queue_cnt--;
227 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL,
228 stcb, SCTP_NOTIFY_DATAGRAM_UNSENT,
231 sctp_m_freem(chk->data);
234 sctp_free_remote_addr(chk->whoTo);
238 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
239 sctppcbinfo.ipi_count_chunk--;
240 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
241 panic("Chunk count is negative");
243 sctppcbinfo.ipi_gencnt_chunk++;
244 chk = TAILQ_FIRST(&outs->outqueue);
248 /* cut back the count and abandon the upper streams */
249 asoc->pre_open_streams = newcnt;
251 asoc->streamincnt = ntohs(init->num_outbound_streams);
252 if (asoc->streamincnt > MAX_SCTP_STREAMS) {
253 asoc->streamincnt = MAX_SCTP_STREAMS;
256 asoc->streamoutcnt = asoc->pre_open_streams;
258 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
259 #ifdef SCTP_MAP_LOGGING
260 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
262 /* This is the next one we expect */
263 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
265 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
266 asoc->cumulative_tsn = asoc->asconf_seq_in;
267 asoc->last_echo_tsn = asoc->asconf_seq_in;
268 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
269 /* open the requested streams */
270 if (asoc->strmin != NULL) {
271 /* Free the old ones */
272 FREE(asoc->strmin, M_PCB);
274 MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
275 sizeof(struct sctp_stream_in), M_PCB, M_NOWAIT);
276 if (asoc->strmin == NULL) {
277 /* we didn't get memory for the streams! */
279 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
280 kprintf("process_init: couldn't get memory for the streams!\n");
285 for (i = 0; i < asoc->streamincnt; i++) {
286 asoc->strmin[i].stream_no = i;
287 asoc->strmin[i].last_sequence_delivered = 0xffff;
289 * U-stream ranges will be set when the cookie
290 * is unpacked. Or for the INIT sender they
291 * are un set (if pr-sctp not supported) when the
294 TAILQ_INIT(&asoc->strmin[i].inqueue);
296 * we are not on any wheel, pr-sctp streams
297 * will go on the wheel when they have data waiting
300 asoc->strmin[i].next_spoke.tqe_next = 0;
301 asoc->strmin[i].next_spoke.tqe_prev = 0;
305 * load_address_from_init will put the addresses into the
306 * association when the COOKIE is processed or the INIT-ACK
307 * is processed. Both types of COOKIE's existing and new
308 * call this routine. It will remove addresses that
309 * are no longer in the association (for the restarting
310 * case where addresses are removed). Up front when the
311 * INIT arrives we will discard it if it is a restart
312 * and new addresses have been added.
318 * INIT-ACK message processing/consumption
319 * returns value < 0 on error
322 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
323 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
324 struct sctp_nets *net)
326 struct sctp_association *asoc;
328 int retval, abort_flag;
329 uint32_t initack_limit;
330 /* First verify that we have no illegal param's */
334 op_err = sctp_arethere_unrecognized_parameters(m,
335 (offset+sizeof(struct sctp_init_chunk)) ,
336 &abort_flag, (struct sctp_chunkhdr *)cp);
338 /* Send an abort and notify peer */
339 if (op_err != NULL) {
340 sctp_send_operr_to(m, iphlen, op_err, cp->init.initiate_tag);
343 * Just notify (abort_assoc does this if
346 sctp_abort_notification(stcb, 0);
348 * No sense in further INIT's since
349 * we will get the same param back
351 sctp_free_assoc(stcb->sctp_ep, stcb);
356 /* process the peer's parameters in the INIT-ACK */
357 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net);
362 initack_limit = offset + ntohs(cp->ch.chunk_length);
363 /* load all addresses */
364 if (sctp_load_addresses_from_init(stcb, m, iphlen,
365 (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh,
367 /* Huh, we should abort */
368 sctp_abort_notification(stcb, 0);
369 sctp_free_assoc(stcb->sctp_ep, stcb);
373 sctp_queue_op_err(stcb, op_err);
374 /* queuing will steal away the mbuf chain to the out queue */
377 /* extract the cookie and queue it to "echo" it back... */
378 stcb->asoc.overall_error_count = 0;
379 net->error_count = 0;
380 retval = sctp_send_cookie_echo(m, offset, stcb, net);
383 * No cookie, we probably should send a op error.
384 * But in any case if there is no cookie in the INIT-ACK,
385 * we can abandon the peer, its broke.
388 /* We abort with an error of missing mandatory param */
391 sctp_generate_invmanparam(SCTP_CAUSE_MISS_PARAM);
394 * Expand beyond to include the mandatory
397 struct sctp_inv_mandatory_param *mp;
399 sizeof(struct sctp_inv_mandatory_param);
401 struct sctp_inv_mandatory_param *);
402 /* Subtract the reserved param */
404 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
405 mp->num_param = htonl(1);
406 mp->param = htons(SCTP_STATE_COOKIE);
409 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
416 * Cancel the INIT timer, We do this first before queueing
417 * the cookie. We always cancel at the primary to assue that
418 * we are canceling the timer started by the INIT which always
419 * goes to the primary.
421 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
422 asoc->primary_destination);
424 /* calculate the RTO */
425 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered);
431 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
432 struct sctp_tcb *stcb, struct sctp_nets *net)
434 struct sockaddr_storage store;
435 struct sockaddr_in *sin;
436 struct sockaddr_in6 *sin6;
437 struct sctp_nets *r_net;
440 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
445 sin = (struct sockaddr_in *)&store;
446 sin6 = (struct sockaddr_in6 *)&store;
448 memset(&store, 0, sizeof(store));
449 if (cp->heartbeat.hb_info.addr_family == AF_INET &&
450 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
451 sin->sin_family = cp->heartbeat.hb_info.addr_family;
452 sin->sin_len = cp->heartbeat.hb_info.addr_len;
453 sin->sin_port = stcb->rport;
454 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
455 sizeof(sin->sin_addr));
456 } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 &&
457 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
458 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
459 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
460 sin6->sin6_port = stcb->rport;
461 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
462 sizeof(sin6->sin6_addr));
465 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
466 kprintf("unsupported address family");
471 r_net = sctp_findnet(stcb, (struct sockaddr *)sin);
474 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
475 kprintf("Huh? I can't find the address I sent it to, discard\n");
480 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
481 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
482 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
484 * If the its a HB and it's random value is correct when
485 * can confirm the destination.
487 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
488 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
489 stcb, 0, (void *)r_net);
491 r_net->error_count = 0;
492 r_net->hb_responded = 1;
493 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
494 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
495 if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
496 r_net->dest_state = SCTP_ADDR_REACHABLE;
497 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
498 SCTP_HEARTBEAT_SUCCESS, (void *)r_net);
500 /* now was it the primary? if so restore */
501 if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
502 sctp_set_primary_addr(stcb, NULL, r_net);
505 /* Now lets do a RTO with this */
506 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv);
510 sctp_handle_abort(struct sctp_abort_chunk *cp,
511 struct sctp_tcb *stcb, struct sctp_nets *net)
513 struct sctp_inpcb *inp;
516 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
517 kprintf("sctp_handle_abort: handling ABORT\n");
522 /* verify that the destination addr is in the association */
523 /* ignore abort for addresses being deleted */
525 /* stop any receive timers */
526 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net);
527 /* notify user of the abort and clean up... */
528 sctp_abort_notification(stcb, 0);
531 sctp_free_assoc(stcb->sctp_ep, stcb);
533 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
534 kprintf("sctp_handle_abort: finished\n");
540 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
541 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
543 struct sctp_association *asoc;
544 int some_on_streamwheel;
547 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
548 kprintf("sctp_handle_shutdown: handling SHUTDOWN\n");
554 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
555 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
559 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
560 /* update current data status */
562 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
563 kprintf("Warning Shutdown NOT the expected size.. skipping (%d:%d)\n",
564 ntohs(cp->ch.chunk_length),
565 (int)sizeof(struct sctp_shutdown_chunk));
570 sctp_update_acked(stcb, cp, net, abort_flag);
573 /* goto SHUTDOWN_RECEIVED state to block new requests */
574 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
575 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
576 asoc->state = SCTP_STATE_SHUTDOWN_RECEIVED;
578 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
579 kprintf("Moving to SHUTDOWN-RECEIVED state\n");
582 /* notify upper layer that peer has initiated a shutdown */
583 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL);
585 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
586 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
588 /* Set the flag so we cannot send more, we
589 * would call the function but we don't want to
590 * wake up the ulp necessarily.
592 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
593 stcb->sctp_ep->sctp_socket->so_rcv.sb_state |= SBS_CANTSENDMORE;
595 sosetstate(stcb->sctp_ep->sctp_socket, SS_CANTSENDMORE);
599 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
601 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
603 * stop the shutdown timer, since we WILL move
604 * to SHUTDOWN-ACK-SENT.
606 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
608 /* Now are we there yet? */
609 some_on_streamwheel = 0;
610 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
611 /* Check to see if some data queued */
612 struct sctp_stream_out *outs;
613 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
614 if (!TAILQ_EMPTY(&outs->outqueue)) {
615 some_on_streamwheel = 1;
621 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
622 kprintf("some_on_streamwheel:%d send_q_empty:%d sent_q_empty:%d\n",
624 !TAILQ_EMPTY(&asoc->send_queue),
625 !TAILQ_EMPTY(&asoc->sent_queue));
628 if (!TAILQ_EMPTY(&asoc->send_queue) ||
629 !TAILQ_EMPTY(&asoc->sent_queue) ||
630 some_on_streamwheel) {
631 /* By returning we will push more data out */
634 /* no outstanding data to send, so move on... */
635 /* send SHUTDOWN-ACK */
636 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
637 /* move to SHUTDOWN-ACK-SENT state */
638 asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
640 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
641 kprintf("moving to SHUTDOWN_ACK state\n");
644 /* start SHUTDOWN timer */
645 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
651 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp,
652 struct sctp_tcb *stcb, struct sctp_nets *net)
654 struct sctp_association *asoc;
657 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
658 kprintf("sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
665 /* process according to association state */
666 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
667 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
668 /* unexpected SHUTDOWN-ACK... so ignore... */
671 /* are the queues empty? */
672 if (!TAILQ_EMPTY(&asoc->send_queue) ||
673 !TAILQ_EMPTY(&asoc->sent_queue) ||
674 !TAILQ_EMPTY(&asoc->out_wheel)) {
675 sctp_report_all_outbound(stcb);
678 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
679 /* send SHUTDOWN-COMPLETE */
680 sctp_send_shutdown_complete(stcb, net);
681 /* notify upper layer protocol */
682 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL);
683 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
684 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
685 stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
686 /* Set the connected flag to disconnected */
687 stcb->sctp_ep->sctp_socket->so_snd.ssb_cc = 0;
688 stcb->sctp_ep->sctp_socket->so_snd.ssb_mbcnt = 0;
689 soisdisconnected(stcb->sctp_ep->sctp_socket);
691 /* free the TCB but first save off the ep */
692 sctp_free_assoc(stcb->sctp_ep, stcb);
696 * Skip past the param header and then we will find the chunk that
697 * caused the problem. There are two possiblities ASCONF or FWD-TSN
698 * other than that and our peer must be broken.
701 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
702 struct sctp_nets *net)
704 struct sctp_chunkhdr *chk;
706 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
707 switch (chk->chunk_type) {
708 case SCTP_ASCONF_ACK:
710 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
711 kprintf("Strange peer, snds ASCONF but does not recognize asconf-ack?\n");
716 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
717 kprintf("Peer does not support ASCONF/ASCONF-ACK chunks\n");
719 #endif /* SCTP_DEBUG */
720 sctp_asconf_cleanup(stcb, net);
722 case SCTP_FORWARD_CUM_TSN:
723 stcb->asoc.peer_supports_prsctp = 0;
727 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
728 kprintf("Peer does not support chunk type %d(%x)??\n",
729 chk->chunk_type, (u_int)chk->chunk_type);
737 * Skip past the param header and then we will find the param that
738 * caused the problem. There are a number of param's in a ASCONF
739 * OR the prsctp param these will turn of specific features.
742 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
744 struct sctp_paramhdr *pbad;
747 switch (ntohs(pbad->param_type)) {
749 case SCTP_PRSCTP_SUPPORTED:
750 stcb->asoc.peer_supports_prsctp = 0;
752 case SCTP_SUPPORTED_CHUNK_EXT:
754 /* draft-ietf-tsvwg-addip-sctp */
755 case SCTP_ECN_NONCE_SUPPORTED:
756 stcb->asoc.peer_supports_ecn_nonce = 0;
757 stcb->asoc.ecn_nonce_allowed = 0;
758 stcb->asoc.ecn_allowed = 0;
760 case SCTP_ADD_IP_ADDRESS:
761 case SCTP_DEL_IP_ADDRESS:
762 stcb->asoc.peer_supports_asconf = 0;
764 case SCTP_SET_PRIM_ADDR:
765 stcb->asoc.peer_supports_asconf_setprim = 0;
767 case SCTP_SUCCESS_REPORT:
768 case SCTP_ERROR_CAUSE_IND:
770 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
771 kprintf("Huh, the peer does not support success? or error cause?\n");
772 kprintf("Turning off ASCONF to this strange peer\n");
775 stcb->asoc.peer_supports_asconf = 0;
776 stcb->asoc.peer_supports_asconf_setprim = 0;
780 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
781 kprintf("Peer does not support base param type %d(%x)??\n",
782 pbad->param_type, (u_int)pbad->param_type);
790 sctp_handle_error(struct sctp_chunkhdr *ch,
791 struct sctp_tcb *stcb, struct sctp_nets *net)
794 struct sctp_paramhdr *phdr;
797 struct sctp_association *asoc;
800 /* parse through all of the errors and process */
802 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
803 sizeof(struct sctp_chunkhdr));
804 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
805 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
806 /* Process an Error Cause */
807 error_type = ntohs(phdr->param_type);
808 error_len = ntohs(phdr->param_length);
809 if ((error_len > chklen) || (error_len == 0)) {
810 /* invalid param length for this param */
812 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
813 kprintf("Bogus length in error param- chunk left:%d errorlen:%d\n",
816 #endif /* SCTP_DEBUG */
819 switch (error_type) {
820 case SCTP_CAUSE_INV_STRM:
821 case SCTP_CAUSE_MISS_PARAM:
822 case SCTP_CAUSE_INVALID_PARAM:
823 case SCTP_CAUSE_NOUSER_DATA:
825 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
826 kprintf("Software error we got a %d back? We have a bug :/ (or do they?)\n",
831 case SCTP_CAUSE_STALE_COOKIE:
832 /* We only act if we have echoed a cookie and are waiting. */
833 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
835 p = (int *)((caddr_t)phdr + sizeof(*phdr));
836 /* Save the time doubled */
837 asoc->cookie_preserve_req = ntohl(*p) << 1;
838 asoc->stale_cookie_count++;
839 if (asoc->stale_cookie_count >
840 asoc->max_init_times) {
841 sctp_abort_notification(stcb, 0);
842 /* now free the asoc */
843 sctp_free_assoc(stcb->sctp_ep, stcb);
846 /* blast back to INIT state */
847 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
848 asoc->state |= SCTP_STATE_COOKIE_WAIT;
849 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
850 stcb->sctp_ep, stcb, net);
851 sctp_send_initiate(stcb->sctp_ep, stcb);
854 case SCTP_CAUSE_UNRESOLV_ADDR:
856 * Nothing we can do here, we don't do hostname
857 * addresses so if the peer does not like my IPv6 (or
858 * IPv4 for that matter) it does not matter. If they
859 * don't support that type of address, they can NOT
860 * possibly get that packet type... i.e. with no IPv6
861 * you can't recieve a IPv6 packet. so we can safely
862 * ignore this one. If we ever added support for
863 * HOSTNAME Addresses, then we would need to do
867 case SCTP_CAUSE_UNRECOG_CHUNK:
868 sctp_process_unrecog_chunk(stcb, phdr, net);
870 case SCTP_CAUSE_UNRECOG_PARAM:
871 sctp_process_unrecog_param(stcb, phdr);
873 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
875 * We ignore this since the timer will drive out a new
876 * cookie anyway and there timer will drive us to send
877 * a SHUTDOWN_COMPLETE. We can't send one here since
878 * we don't have their tag.
881 case SCTP_CAUSE_DELETEING_LAST_ADDR:
882 case SCTP_CAUSE_OPERATION_REFUSED:
883 case SCTP_CAUSE_DELETING_SRC_ADDR:
884 /* We should NOT get these here, but in a ASCONF-ACK. */
886 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
887 kprintf("Peer sends ASCONF errors in a Operational Error?<%d>?\n",
892 case SCTP_CAUSE_OUT_OF_RESC:
894 * And what, pray tell do we do with the fact
895 * that the peer is out of resources? Not
896 * really sure we could do anything but abort.
897 * I suspect this should have came WITH an
898 * abort instead of in a OP-ERROR.
903 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
904 /* don't know what this error cause is... */
905 kprintf("sctp_handle_error: unknown error type = 0x%xh\n",
908 #endif /* SCTP_DEBUG */
911 adjust = SCTP_SIZE32(error_len);
913 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
919 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
920 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
921 struct sctp_nets *net)
923 struct sctp_init_ack *init_ack;
928 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
929 kprintf("sctp_handle_init_ack: handling INIT-ACK\n");
934 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
935 kprintf("sctp_handle_init_ack: TCB is null\n");
940 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
942 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
943 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
947 init_ack = &cp->init;
948 /* validate parameters */
949 if (init_ack->initiate_tag == 0) {
950 /* protocol error... send an abort */
951 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
952 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
956 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
957 /* protocol error... send an abort */
958 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
959 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
963 if (init_ack->num_inbound_streams == 0) {
964 /* protocol error... send an abort */
965 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
966 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
970 if (init_ack->num_outbound_streams == 0) {
971 /* protocol error... send an abort */
972 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
973 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
978 /* process according to association state... */
979 state = &stcb->asoc.state;
980 switch (*state & SCTP_STATE_MASK) {
981 case SCTP_STATE_COOKIE_WAIT:
982 /* this is the expected state for this chunk */
983 /* process the INIT-ACK parameters */
984 if (stcb->asoc.primary_destination->dest_state &
985 SCTP_ADDR_UNCONFIRMED) {
987 * The primary is where we sent the INIT, we can
988 * always consider it confirmed when the INIT-ACK
989 * is returned. Do this before we load addresses
992 stcb->asoc.primary_destination->dest_state &=
993 ~SCTP_ADDR_UNCONFIRMED;
994 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
995 stcb, 0, (void *)stcb->asoc.primary_destination);
997 if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, net
999 /* error in parsing parameters */
1001 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1002 kprintf("sctp_process_init_ack: error in msg, discarding\n");
1007 /* update our state */
1009 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1010 kprintf("moving to COOKIE-ECHOED state\n");
1013 if (*state & SCTP_STATE_SHUTDOWN_PENDING) {
1014 *state = SCTP_STATE_COOKIE_ECHOED |
1015 SCTP_STATE_SHUTDOWN_PENDING;
1017 *state = SCTP_STATE_COOKIE_ECHOED;
1020 /* reset the RTO calc */
1021 stcb->asoc.overall_error_count = 0;
1022 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1024 * collapse the init timer back in case of a exponential backoff
1026 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1029 * the send at the end of the inbound data processing will
1030 * cause the cookie to be sent
1033 case SCTP_STATE_SHUTDOWN_SENT:
1034 /* incorrect state... discard */
1036 case SCTP_STATE_COOKIE_ECHOED:
1037 /* incorrect state... discard */
1039 case SCTP_STATE_OPEN:
1040 /* incorrect state... discard */
1042 case SCTP_STATE_EMPTY:
1043 case SCTP_STATE_INUSE:
1045 /* incorrect state... discard */
1047 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1048 kprintf("Leaving handle-init-ack default\n");
1053 } /* end switch asoc state */
1055 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1056 kprintf("Leaving handle-init-ack end\n");
1064 * handle a state cookie for an existing association
1065 * m: input packet mbuf chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk
1066 * note: this is a "split" mbuf and the cookie signature does not exist
1067 * offset: offset into mbuf to the cookie-echo chunk
1069 static struct sctp_tcb *
1070 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1071 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1072 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
1073 struct sockaddr *init_src, int *notification)
1075 struct sctp_association *asoc;
1076 struct sctp_init_chunk *init_cp, init_buf;
1077 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1079 int init_offset, initack_offset;
1082 /* I know that the TCB is non-NULL from the caller */
1085 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1086 /* SHUTDOWN came in after sending INIT-ACK */
1087 struct mbuf *op_err;
1088 struct sctp_paramhdr *ph;
1090 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1092 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1093 kprintf("sctp_handle_cookie: got a cookie, while shutting down!\n");
1096 MGETHDR(op_err, MB_DONTWAIT, MT_HEADER);
1097 if (op_err == NULL) {
1101 /* pre-reserve some space */
1102 op_err->m_data += sizeof(struct ip6_hdr);
1103 op_err->m_data += sizeof(struct sctphdr);
1104 op_err->m_data += sizeof(struct sctp_chunkhdr);
1106 op_err->m_len = op_err->m_pkthdr.len = sizeof(struct sctp_paramhdr);
1107 ph = mtod(op_err, struct sctp_paramhdr *);
1108 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1109 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1110 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag);
1114 * find and validate the INIT chunk in the cookie (peer's info)
1115 * the INIT should start after the cookie-echo header struct
1116 * (chunk header, state cookie header struct)
1118 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1120 init_cp = (struct sctp_init_chunk *)
1121 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1122 (u_int8_t *)&init_buf);
1123 if (init_cp == NULL) {
1124 /* could not pull a INIT chunk in cookie */
1126 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1127 kprintf("process_cookie_existing: could not pull INIT chunk hdr\n");
1129 #endif /* SCTP_DEBUG */
1132 chk_length = ntohs(init_cp->ch.chunk_length);
1133 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1135 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1136 kprintf("process_cookie_existing: could not find INIT chunk!\n");
1138 #endif /* SCTP_DEBUG */
1143 * find and validate the INIT-ACK chunk in the cookie (my info)
1144 * the INIT-ACK follows the INIT chunk
1146 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1147 initack_cp = (struct sctp_init_ack_chunk *)
1148 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1149 (u_int8_t *)&initack_buf);
1150 if (initack_cp == NULL) {
1151 /* could not pull INIT-ACK chunk in cookie */
1153 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1154 kprintf("process_cookie_existing: could not pull INIT-ACK chunk hdr\n");
1156 #endif /* SCTP_DEBUG */
1159 chk_length = ntohs(initack_cp->ch.chunk_length);
1160 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1162 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1163 kprintf("process_cookie_existing: could not find INIT-ACK chunk!\n");
1165 #endif /* SCTP_DEBUG */
1168 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1169 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1171 * case D in Section 5.2.4 Table 2: MMAA
1172 * process accordingly to get into the OPEN state
1174 switch SCTP_GET_STATE(asoc) {
1175 case SCTP_STATE_COOKIE_WAIT:
1177 * INIT was sent, but got got a COOKIE_ECHO with
1178 * the correct tags... just accept it...
1180 /* First we must process the INIT !! */
1181 retval = sctp_process_init(init_cp, stcb, net);
1184 kprintf("process_cookie_existing: INIT processing failed\n");
1188 /* intentional fall through to below... */
1190 case SCTP_STATE_COOKIE_ECHOED:
1191 /* Duplicate INIT case */
1192 /* we have already processed the INIT so no problem */
1193 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1195 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
1196 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb,
1198 /* update current state */
1199 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1200 asoc->state = SCTP_STATE_OPEN |
1201 SCTP_STATE_SHUTDOWN_PENDING;
1202 } else if ((asoc->state & SCTP_STATE_SHUTDOWN_SENT) == 0) {
1203 /* if ok, move to OPEN state */
1204 asoc->state = SCTP_STATE_OPEN;
1206 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1207 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1208 (!(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING))) {
1210 * Here is where collision would go if we did a
1211 * connect() and instead got a
1212 * init/init-ack/cookie done before the
1213 * init-ack came back..
1215 stcb->sctp_ep->sctp_flags |=
1216 SCTP_PCB_FLAGS_CONNECTED;
1217 soisconnected(stcb->sctp_ep->sctp_socket);
1219 /* notify upper layer */
1220 *notification = SCTP_NOTIFY_ASSOC_UP;
1221 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1224 * since we did not send a HB make sure we don't double
1227 net->hb_responded = 1;
1229 if (stcb->asoc.sctp_autoclose_ticks &&
1230 (inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
1231 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1237 * we're in the OPEN state (or beyond), so peer
1238 * must have simply lost the COOKIE-ACK
1244 * We ignore the return code here.. not sure if we should
1245 * somehow abort.. but we do have an existing asoc. This
1246 * really should not fail.
1248 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1249 init_offset + sizeof(struct sctp_init_chunk),
1250 initack_offset, sh, init_src)) {
1252 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1253 kprintf("Weird cookie load_address failure on cookie existing - 1\n");
1259 /* respond with a COOKIE-ACK */
1260 sctp_send_cookie_ack(stcb);
1263 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1264 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1265 cookie->tie_tag_my_vtag == 0 &&
1266 cookie->tie_tag_peer_vtag == 0) {
1268 * case C in Section 5.2.4 Table 2: XMOO
1273 if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag &&
1274 (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag ||
1275 init_cp->init.initiate_tag == 0)) {
1277 * case B in Section 5.2.4 Table 2: MXAA or MOAA
1278 * my info should be ok, re-accept peer info
1280 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1281 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
1282 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
1283 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1285 * since we did not send a HB make sure we don't double things
1287 net->hb_responded = 1;
1288 if (stcb->asoc.sctp_autoclose_ticks &&
1289 (inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
1290 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1293 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1294 asoc->pre_open_streams =
1295 ntohs(initack_cp->init.num_outbound_streams);
1296 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1297 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out =
1298 asoc->init_seq_number;
1299 asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
1300 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1301 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1302 asoc->str_reset_seq_in = asoc->init_seq_number;
1303 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1305 /* process the INIT info (peer's info) */
1306 retval = sctp_process_init(init_cp, stcb, net);
1309 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1310 kprintf("process_cookie_existing: INIT processing failed\n");
1315 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1316 init_offset + sizeof(struct sctp_init_chunk),
1317 initack_offset, sh, init_src)) {
1319 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1320 kprintf("Weird cookie load_address failure on cookie existing - 2\n");
1326 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1327 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1328 *notification = SCTP_NOTIFY_ASSOC_UP;
1330 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1331 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1332 !(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
1333 stcb->sctp_ep->sctp_flags |=
1334 SCTP_PCB_FLAGS_CONNECTED;
1335 soisconnected(stcb->sctp_ep->sctp_socket);
1338 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1339 asoc->state = SCTP_STATE_OPEN |
1340 SCTP_STATE_SHUTDOWN_PENDING;
1342 asoc->state = SCTP_STATE_OPEN;
1344 sctp_send_cookie_ack(stcb);
1348 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1349 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1350 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1351 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1352 cookie->tie_tag_peer_vtag != 0) {
1354 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1356 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
1357 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
1358 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1360 /* notify upper layer */
1361 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1363 /* send up all the data */
1364 sctp_report_all_outbound(stcb);
1366 /* process the INIT-ACK info (my info) */
1367 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1368 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1369 asoc->pre_open_streams =
1370 ntohs(initack_cp->init.num_outbound_streams);
1371 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1372 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out =
1373 asoc->init_seq_number;
1374 asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
1375 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1376 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1377 asoc->str_reset_seq_in = asoc->init_seq_number;
1379 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1380 if (asoc->mapping_array)
1381 memset(asoc->mapping_array, 0,
1382 asoc->mapping_array_size);
1383 /* process the INIT info (peer's info) */
1384 retval = sctp_process_init(init_cp, stcb, net);
1387 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1388 kprintf("process_cookie_existing: INIT processing failed\n");
1394 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1396 * since we did not send a HB make sure we don't double things
1398 net->hb_responded = 1;
1400 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1401 init_offset + sizeof(struct sctp_init_chunk),
1402 initack_offset, sh, init_src)) {
1404 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1405 kprintf("Weird cookie load_address failure on cookie existing - 3\n");
1411 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1412 asoc->state = SCTP_STATE_OPEN |
1413 SCTP_STATE_SHUTDOWN_PENDING;
1414 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1415 /* move to OPEN state, if not in SHUTDOWN_SENT */
1416 asoc->state = SCTP_STATE_OPEN;
1418 /* respond with a COOKIE-ACK */
1419 sctp_send_cookie_ack(stcb);
1423 /* all other cases... */
1428 * handle a state cookie for a new association
1429 * m: input packet mbuf chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk
1430 * note: this is a "split" mbuf and the cookie signature does not exist
1431 * offset: offset into mbuf to the cookie-echo chunk
1432 * length: length of the cookie chunk
1433 * to: where the init was from
1436 static struct sctp_tcb *
1437 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1438 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1439 struct sctp_inpcb *inp, struct sctp_nets **netp,
1440 struct sockaddr *init_src, int *notification)
1442 struct sctp_tcb *stcb;
1443 struct sctp_init_chunk *init_cp, init_buf;
1444 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1445 struct sockaddr_storage sa_store;
1446 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
1447 struct sockaddr_in *sin;
1448 struct sockaddr_in6 *sin6;
1449 struct sctp_association *asoc;
1451 int init_offset, initack_offset, initack_limit;
1456 * find and validate the INIT chunk in the cookie (peer's info)
1457 * the INIT should start after the cookie-echo header struct
1458 * (chunk header, state cookie header struct)
1460 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
1461 init_cp = (struct sctp_init_chunk *)
1462 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1463 (u_int8_t *)&init_buf);
1464 if (init_cp == NULL) {
1465 /* could not pull a INIT chunk in cookie */
1467 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1468 kprintf("process_cookie_new: could not pull INIT chunk hdr\n");
1470 #endif /* SCTP_DEBUG */
1473 chk_length = ntohs(init_cp->ch.chunk_length);
1474 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1476 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1477 kprintf("HUH? process_cookie_new: could not find INIT chunk!\n");
1479 #endif /* SCTP_DEBUG */
1483 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1485 * find and validate the INIT-ACK chunk in the cookie (my info)
1486 * the INIT-ACK follows the INIT chunk
1488 initack_cp = (struct sctp_init_ack_chunk *)
1489 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1490 (u_int8_t *)&initack_buf);
1491 if (initack_cp == NULL) {
1492 /* could not pull INIT-ACK chunk in cookie */
1494 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1495 kprintf("process_cookie_new: could not pull INIT-ACK chunk hdr\n");
1497 #endif /* SCTP_DEBUG */
1500 chk_length = ntohs(initack_cp->ch.chunk_length);
1501 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1503 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1505 pp = (u_int8_t *)initack_cp;
1506 kprintf("process_cookie_new: could not find INIT-ACK chunk!\n");
1507 kprintf("Found bytes %x %x %x %x at position %d\n",
1508 (u_int)pp[0], (u_int)pp[1], (u_int)pp[2],
1509 (u_int)pp[3], initack_offset);
1511 #endif /* SCTP_DEBUG */
1514 initack_limit = initack_offset + SCTP_SIZE32(chk_length);
1517 * now that we know the INIT/INIT-ACK are in place,
1518 * create a new TCB and popluate
1520 stcb = sctp_aloc_assoc(inp, init_src, 0, &error, ntohl(initack_cp->init.initiate_tag));
1522 struct mbuf *op_err;
1523 /* memory problem? */
1525 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1526 kprintf("process_cookie_new: no room for another TCB!\n");
1528 #endif /* SCTP_DEBUG */
1529 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1530 sctp_abort_association(inp, NULL, m, iphlen,
1535 /* get the correct sctp_nets */
1536 *netp = sctp_findnet(stcb, init_src);
1538 /* get scope variables out of cookie */
1539 asoc->ipv4_local_scope = cookie->ipv4_scope;
1540 asoc->site_scope = cookie->site_scope;
1541 asoc->local_scope = cookie->local_scope;
1542 asoc->loopback_scope = cookie->loopback_scope;
1544 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
1545 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
1546 struct mbuf *op_err;
1548 * Houston we have a problem. The EP changed while the cookie
1549 * was in flight. Only recourse is to abort the association.
1551 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1552 sctp_abort_association(inp, NULL, m, iphlen,
1557 /* process the INIT-ACK info (my info) */
1558 old_tag = asoc->my_vtag;
1559 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1560 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1561 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1562 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1563 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1564 asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
1565 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1566 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1567 asoc->str_reset_seq_in = asoc->init_seq_number;
1569 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1571 /* process the INIT info (peer's info) */
1572 retval = sctp_process_init(init_cp, stcb, *netp);
1575 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1576 kprintf("process_cookie_new: INIT processing failed\n");
1579 sctp_free_assoc(inp, stcb);
1582 /* load all addresses */
1583 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1584 init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
1586 sctp_free_assoc(inp, stcb);
1590 /* update current state */
1592 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1593 kprintf("moving to OPEN state\n");
1596 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1597 asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
1599 asoc->state = SCTP_STATE_OPEN;
1601 /* calculate the RTT */
1602 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
1603 &cookie->time_entered);
1606 * if we're doing ASCONFs, check to see if we have any new
1607 * local addresses that need to get added to the peer (eg.
1608 * addresses changed while cookie echo in flight). This needs
1609 * to be done after we go to the OPEN state to do the correct
1610 * asconf processing.
1611 * else, make sure we have the correct addresses in our lists
1614 /* warning, we re-use sin, sin6, sa_store here! */
1615 /* pull in local_address (our "from" address) */
1616 if (cookie->laddr_type == SCTP_IPV4_ADDRESS) {
1617 /* source addr is IPv4 */
1618 sin = (struct sockaddr_in *)initack_src;
1619 memset(sin, 0, sizeof(*sin));
1620 sin->sin_family = AF_INET;
1621 sin->sin_len = sizeof(struct sockaddr_in);
1622 sin->sin_addr.s_addr = cookie->laddress[0];
1623 } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) {
1624 /* source addr is IPv6 */
1625 sin6 = (struct sockaddr_in6 *)initack_src;
1626 memset(sin6, 0, sizeof(*sin6));
1627 sin6->sin6_family = AF_INET6;
1628 sin6->sin6_len = sizeof(struct sockaddr_in6);
1629 sin6->sin6_scope_id = cookie->scope_id;
1630 memcpy(&sin6->sin6_addr, cookie->laddress,
1631 sizeof(sin6->sin6_addr));
1633 sctp_free_assoc(inp, stcb);
1637 sctp_check_address_list(stcb, m, initack_offset +
1638 sizeof(struct sctp_init_ack_chunk), initack_limit,
1639 initack_src, cookie->local_scope, cookie->site_scope,
1640 cookie->ipv4_scope, cookie->loopback_scope);
1643 /* set up to notify upper layer */
1644 *notification = SCTP_NOTIFY_ASSOC_UP;
1645 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1646 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1647 !(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
1649 * This is an endpoint that called connect()
1650 * how it got a cookie that is NEW is a bit of
1651 * a mystery. It must be that the INIT was sent, but
1652 * before it got there.. a complete INIT/INIT-ACK/COOKIE
1653 * arrived. But of course then it should have went to
1654 * the other code.. not here.. oh well.. a bit of protection
1657 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1658 soisconnected(stcb->sctp_ep->sctp_socket);
1659 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, *netp);
1660 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1661 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
1663 * We don't want to do anything with this
1664 * one. Since it is the listening guy. The timer will
1665 * get started for accepted connections in the caller.
1669 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, *netp);
1671 /* since we did not send a HB make sure we don't double things */
1672 (*netp)->hb_responded = 1;
1674 if (stcb->asoc.sctp_autoclose_ticks &&
1675 (inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
1676 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
1679 /* respond with a COOKIE-ACK */
1680 sctp_send_cookie_ack(stcb);
1687 * handles a COOKIE-ECHO message
1688 * stcb: modified to either a new or left as existing (non-NULL) TCB
1690 static struct mbuf *
1691 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
1692 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
1693 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp)
1695 struct sctp_state_cookie *cookie;
1696 struct sockaddr_in6 sin6;
1697 struct sockaddr_in sin;
1698 struct sctp_tcb *l_stcb=*stcb;
1699 struct sctp_inpcb *l_inp;
1700 struct sockaddr *to;
1701 struct sctp_pcb *ep;
1703 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
1705 uint8_t cookie_ok = 0;
1706 unsigned int size_of_pkt, sig_offset, cookie_offset;
1707 unsigned int cookie_len;
1709 struct timeval time_expires;
1710 struct sockaddr_storage dest_store;
1711 struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
1713 int notification = 0;
1714 struct sctp_nets *netl;
1715 int had_a_existing_tcb = 0;
1718 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1719 kprintf("sctp_handle_cookie: handling COOKIE-ECHO\n");
1723 if (inp_p == NULL) {
1725 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1726 kprintf("sctp_handle_cookie: null inp_p!\n");
1731 /* First get the destination address setup too. */
1732 iph = mtod(m, struct ip *);
1733 if (iph->ip_v == IPVERSION) {
1735 struct sockaddr_in *sin;
1736 sin = (struct sockaddr_in *)(localep_sa);
1737 memset(sin, 0, sizeof(*sin));
1738 sin->sin_family = AF_INET;
1739 sin->sin_len = sizeof(*sin);
1740 sin->sin_port = sh->dest_port;
1741 sin->sin_addr.s_addr = iph->ip_dst.s_addr ;
1742 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1744 struct ip6_hdr *ip6;
1745 struct sockaddr_in6 *sin6;
1746 sin6 = (struct sockaddr_in6 *)(localep_sa);
1747 memset(sin6, 0, sizeof(*sin6));
1748 sin6->sin6_family = AF_INET6;
1749 sin6->sin6_len = sizeof(struct sockaddr_in6);
1750 ip6 = mtod(m, struct ip6_hdr *);
1751 sin6->sin6_port = sh->dest_port;
1752 sin6->sin6_addr = ip6->ip6_dst;
1757 cookie = &cp->cookie;
1758 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
1759 cookie_len = ntohs(cp->ch.chunk_length);
1761 if ((cookie->peerport != sh->src_port) &&
1762 (cookie->myport != sh->dest_port) &&
1763 (cookie->my_vtag != sh->v_tag)) {
1765 * invalid ports or bad tag. Note that we always leave
1766 * the v_tag in the header in network order and when we
1767 * stored it in the my_vtag slot we also left it in network
1768 * order. This maintians the match even though it may be in
1769 * the opposite byte order of the machine :->
1774 /* compute size of packet */
1775 if (m->m_flags & M_PKTHDR) {
1776 size_of_pkt = m->m_pkthdr.len;
1778 /* Should have a pkt hdr really */
1782 while (mat != NULL) {
1783 size_of_pkt += mat->m_len;
1787 if (cookie_len > size_of_pkt ||
1788 cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
1789 sizeof(struct sctp_init_chunk) +
1790 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
1791 /* cookie too long! or too small */
1793 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1794 kprintf("sctp_handle_cookie: cookie_len=%u, pkt size=%u\n", cookie_len, size_of_pkt);
1796 #endif /* SCTP_DEBUG */
1800 * split off the signature into its own mbuf (since it
1801 * should not be calculated in the sctp_hash_digest_m() call).
1803 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
1804 if (sig_offset > size_of_pkt) {
1805 /* packet not correct size! */
1806 /* XXX this may already be accounted for earlier... */
1808 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1809 kprintf("sctp_handle_cookie: sig offset=%u, pkt size=%u\n", sig_offset, size_of_pkt);
1815 m_sig = m_split(m, sig_offset, MB_DONTWAIT);
1816 if (m_sig == NULL) {
1817 /* out of memory or ?? */
1819 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1820 kprintf("sctp_handle_cookie: couldn't m_split the signature\n");
1826 * compute the signature/digest for the cookie
1828 ep = &(*inp_p)->sctp_ep;
1831 SCTP_TCB_UNLOCK(l_stcb);
1833 SCTP_INP_RLOCK(l_inp);
1835 SCTP_TCB_LOCK(l_stcb);
1837 /* which cookie is it? */
1838 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
1839 (ep->current_secret_number != ep->last_secret_number)) {
1840 /* it's the old cookie */
1842 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1843 kprintf("sctp_handle_cookie: old cookie sig\n");
1846 sctp_hash_digest_m((char *)ep->secret_key[(int)ep->last_secret_number],
1847 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1849 /* it's the current cookie */
1851 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1852 kprintf("sctp_handle_cookie: current cookie sig\n");
1855 sctp_hash_digest_m((char *)ep->secret_key[(int)ep->current_secret_number],
1856 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1858 /* get the signature */
1859 SCTP_INP_RUNLOCK(l_inp);
1860 sig = (u_int8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (u_int8_t *)&tmp_sig);
1862 /* couldn't find signature */
1864 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1865 kprintf("sctp_handle_cookie: couldn't pull the signature\n");
1870 /* compare the received digest with the computed digest */
1871 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
1872 /* try the old cookie? */
1873 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
1874 (ep->current_secret_number != ep->last_secret_number)) {
1875 /* compute digest with old */
1877 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1878 kprintf("sctp_handle_cookie: old cookie sig\n");
1881 sctp_hash_digest_m((char *)ep->secret_key[(int)ep->last_secret_number],
1882 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1884 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
1892 * Now before we continue we must reconstruct our mbuf so
1893 * that normal processing of any other chunks will work.
1898 while (m_at->m_next != NULL) {
1899 m_at = m_at->m_next;
1901 m_at->m_next = m_sig;
1902 if (m->m_flags & M_PKTHDR) {
1904 * We should only do this if and only if the front
1905 * mbuf has a m_pkthdr... it should in theory.
1907 if (m_sig->m_flags & M_PKTHDR) {
1908 /* Add back to the pkt hdr of main m chain */
1909 m->m_pkthdr.len += m_sig->m_len;
1912 * Got a problem, no pkthdr in split chain.
1913 * TSNH but we will handle it just in case
1917 kprintf("Warning: Hitting m_split join TSNH code - fixed\n");
1920 mmlen += lat->m_len;
1923 m->m_pkthdr.len += mmlen;
1928 if (cookie_ok == 0) {
1930 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1931 kprintf("handle_cookie_echo: cookie signature validation failed!\n");
1932 kprintf("offset = %u, cookie_offset = %u, sig_offset = %u\n",
1933 (u_int32_t)offset, cookie_offset, sig_offset);
1939 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1940 kprintf("handle_cookie_echo: cookie signature validation passed\n");
1945 * check the cookie timestamps to be sure it's not stale
1947 SCTP_GETTIME_TIMEVAL(&now);
1948 /* Expire time is in Ticks, so we convert to seconds */
1949 time_expires.tv_sec = cookie->time_entered.tv_sec + cookie->cookie_life;
1950 time_expires.tv_usec = cookie->time_entered.tv_usec;
1952 if (timercmp(&now, &time_expires, >))
1954 if (timevalcmp(&now, &time_expires, >))
1957 /* cookie is stale! */
1958 struct mbuf *op_err;
1959 struct sctp_stale_cookie_msg *scm;
1962 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1963 kprintf("sctp_handle_cookie: got a STALE cookie!\n");
1966 MGETHDR(op_err, MB_DONTWAIT, MT_HEADER);
1967 if (op_err == NULL) {
1971 /* pre-reserve some space */
1972 op_err->m_data += sizeof(struct ip6_hdr);
1973 op_err->m_data += sizeof(struct sctphdr);
1974 op_err->m_data += sizeof(struct sctp_chunkhdr);
1977 op_err->m_len = op_err->m_pkthdr.len = sizeof(struct sctp_stale_cookie_msg);
1978 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
1979 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
1980 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
1981 (sizeof(u_int32_t))));
1982 /* seconds to usec */
1983 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
1986 tim = now.tv_usec - cookie->time_entered.tv_usec;
1987 scm->time_usec = htonl(tim);
1988 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag);
1992 * Now we must see with the lookup address if we have an existing
1993 * asoc. This will only happen if we were in the COOKIE-WAIT state
1994 * and a INIT collided with us and somewhere the peer sent the
1995 * cookie on another address besides the single address our assoc
1996 * had for him. In this case we will have one of the tie-tags set
1997 * at least AND the address field in the cookie can be used to
2001 if (cookie->addr_type == SCTP_IPV6_ADDRESS) {
2002 memset(&sin6, 0, sizeof(sin6));
2003 sin6.sin6_family = AF_INET6;
2004 sin6.sin6_len = sizeof(sin6);
2005 sin6.sin6_port = sh->src_port;
2006 sin6.sin6_scope_id = cookie->scope_id;
2007 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2008 sizeof(sin6.sin6_addr.s6_addr));
2009 to = (struct sockaddr *)&sin6;
2010 } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) {
2011 memset(&sin, 0, sizeof(sin));
2012 sin.sin_family = AF_INET;
2013 sin.sin_len = sizeof(sin);
2014 sin.sin_port = sh->src_port;
2015 sin.sin_addr.s_addr = cookie->address[0];
2016 to = (struct sockaddr *)&sin;
2019 if ((*stcb == NULL) && to) {
2020 /* Yep, lets check */
2021 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
2022 if (*stcb == NULL) {
2023 /* We should have only got back the same inp. If we
2024 * got back a different ep we have a problem. The original
2025 * findep got back l_inp and now
2027 if (l_inp != *inp_p) {
2028 kprintf("Bad problem find_ep got a diff inp then special_locate?\n");
2033 cookie_len -= SCTP_SIGNATURE_SIZE;
2034 if (*stcb == NULL) {
2035 /* this is the "normal" case... get a new TCB */
2037 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2038 kprintf("sctp_handle_cookie: processing NEW cookie\n");
2041 *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
2042 cookie_len, *inp_p, netp, to, ¬ification);
2043 /* now always decrement, since this is the normal
2044 * case.. we had no tcb when we entered.
2047 /* this is abnormal... cookie-echo on existing TCB */
2049 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2050 kprintf("sctp_handle_cookie: processing EXISTING cookie\n");
2053 had_a_existing_tcb = 1;
2054 *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
2055 cookie, cookie_len, *inp_p, *stcb, *netp, to, ¬ification);
2058 if (*stcb == NULL) {
2059 /* still no TCB... must be bad cookie-echo */
2061 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2062 kprintf("handle_cookie_echo: ACK! don't have a TCB!\n");
2064 #endif /* SCTP_DEBUG */
2069 * Ok, we built an association so confirm the address
2070 * we sent the INIT-ACK to.
2072 netl = sctp_findnet(*stcb, to);
2073 /* This code should in theory NOT run but
2078 kprintf("TSNH! Huh, why do I need to add this address here?\n");
2080 ret = sctp_add_remote_addr(*stcb, to, 0, 100);
2081 netl = sctp_findnet(*stcb, to);
2084 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2085 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2086 sctp_set_primary_addr((*stcb), NULL,
2088 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2089 (*stcb), 0, (void *)netl);
2094 kprintf("Could not add source address for some reason\n");
2098 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2099 if (!had_a_existing_tcb ||
2100 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2102 * If we have a NEW cookie or the connect never reached
2103 * the connected state during collision we must do the
2106 struct socket *so, *oso;
2107 struct sctp_inpcb *inp;
2108 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2110 * For a restart we will keep the same socket,
2111 * no need to do anything. I THINK!!
2113 sctp_ulp_notify(notification, *stcb, 0, NULL);
2116 oso = (*inp_p)->sctp_socket;
2117 SCTP_TCB_UNLOCK((*stcb));
2118 so = sonewconn(oso, SS_ISCONNECTED);
2119 SCTP_INP_WLOCK((*stcb)->sctp_ep);
2120 SCTP_TCB_LOCK((*stcb));
2121 SCTP_INP_WUNLOCK((*stcb)->sctp_ep);
2123 struct mbuf *op_err;
2124 /* Too many sockets */
2126 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
2127 kprintf("process_cookie_new: no room for another socket!\n");
2129 #endif /* SCTP_DEBUG */
2130 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2131 sctp_abort_association(*inp_p, NULL, m, iphlen,
2133 sctp_free_assoc(*inp_p, *stcb);
2136 inp = (struct sctp_inpcb *)so->so_pcb;
2137 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2138 SCTP_PCB_FLAGS_CONNECTED |
2139 SCTP_PCB_FLAGS_IN_TCPPOOL |
2140 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2141 SCTP_PCB_FLAGS_DONT_WAKE);
2142 inp->sctp_socket = so;
2145 * Now we must move it from one hash table to another
2146 * and get the tcb in the right place.
2148 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2150 /* Switch over to the new guy */
2153 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp,
2156 sctp_ulp_notify(notification, *stcb, 0, NULL);
2160 if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2161 sctp_ulp_notify(notification, *stcb, 0, NULL);
2167 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp,
2168 struct sctp_tcb *stcb, struct sctp_nets *net)
2170 /* cp must not be used, others call this without a c-ack :-) */
2171 struct sctp_association *asoc;
2174 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2175 kprintf("sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2183 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, stcb, net);
2185 /* process according to association state */
2186 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2187 /* state change only needed when I am in right state */
2189 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2190 kprintf("moving to OPEN state\n");
2193 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2194 asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
2196 asoc->state = SCTP_STATE_OPEN;
2200 if (asoc->overall_error_count == 0) {
2201 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2202 &asoc->time_entered);
2204 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2205 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL);
2206 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2207 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2208 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2209 soisconnected(stcb->sctp_ep->sctp_socket);
2211 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2213 /* since we did not send a HB make sure we don't double things */
2214 net->hb_responded = 1;
2216 if (stcb->asoc.sctp_autoclose_ticks &&
2217 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
2218 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2219 stcb->sctp_ep, stcb, NULL);
2223 * set ASCONF timer if ASCONFs are pending and allowed
2224 * (eg. addresses changed when init/cookie echo in flight)
2226 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) &&
2227 (stcb->asoc.peer_supports_asconf) &&
2228 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2229 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2230 stcb->sctp_ep, stcb,
2231 stcb->asoc.primary_destination);
2235 /* Toss the cookie if I can */
2236 sctp_toss_old_cookies(asoc);
2237 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2238 /* Restart the timer if we have pending data */
2239 struct sctp_tmit_chunk *chk;
2240 chk = TAILQ_FIRST(&asoc->sent_queue);
2242 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2250 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2251 struct sctp_tcb *stcb)
2253 struct sctp_nets *net;
2254 struct sctp_tmit_chunk *lchk;
2256 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) {
2259 sctp_pegs[SCTP_ECNE_RCVD]++;
2260 tsn = ntohl(cp->tsn);
2261 /* ECN Nonce stuff: need a resync and disable the nonce sum check */
2262 /* Also we make sure we disable the nonce_wait */
2263 lchk = TAILQ_FIRST(&stcb->asoc.send_queue);
2265 stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
2267 stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq;
2269 stcb->asoc.nonce_wait_for_ecne = 0;
2270 stcb->asoc.nonce_sum_check = 0;
2272 /* Find where it was sent, if possible */
2274 lchk = TAILQ_FIRST(&stcb->asoc.sent_queue);
2276 if (lchk->rec.data.TSN_seq == tsn) {
2280 if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ))
2282 lchk = TAILQ_NEXT(lchk, sctp_next);
2285 /* default is we use the primary */
2286 net = stcb->asoc.primary_destination;
2288 if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) {
2289 #ifdef SCTP_CWND_LOGGING
2292 #ifdef SCTP_CWND_LOGGING
2293 old_cwnd = net->cwnd;
2295 sctp_pegs[SCTP_CWR_PERFO]++;
2296 net->ssthresh = net->cwnd / 2;
2297 if (net->ssthresh < net->mtu) {
2298 net->ssthresh = net->mtu;
2299 /* here back off the timer as well, to slow us down */
2302 net->cwnd = net->ssthresh;
2303 #ifdef SCTP_CWND_LOGGING
2304 sctp_log_cwnd(net, (net->cwnd-old_cwnd), SCTP_CWND_LOG_FROM_SAT);
2306 /* we reduce once every RTT. So we will only lower
2307 * cwnd at the next sending seq i.e. the resync_tsn.
2309 stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn;
2312 * We always send a CWR this way if our previous one was lost
2313 * our peer will get an update, or if it is not time again
2314 * to reduce we still get the cwr to the peer.
2316 sctp_send_cwr(stcb, net, tsn);
2320 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb)
2322 /* Here we get a CWR from the peer. We must look in
2323 * the outqueue and make sure that we have a covered
2324 * ECNE in teh control chunk part. If so remove it.
2326 struct sctp_tmit_chunk *chk;
2327 struct sctp_ecne_chunk *ecne;
2329 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
2330 if (chk->rec.chunk_id != SCTP_ECN_ECHO) {
2333 /* Look for and remove if it is the right TSN. Since
2334 * there is only ONE ECNE on the control queue at
2335 * any one time we don't need to worry about more than
2338 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
2339 if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn),
2340 MAX_TSN) || (cp->tsn == ecne->tsn)) {
2341 /* this covers this ECNE, we can remove it */
2342 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
2345 sctp_m_freem(chk->data);
2348 stcb->asoc.ctrl_queue_cnt--;
2349 sctp_free_remote_addr(chk->whoTo);
2350 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
2351 sctppcbinfo.ipi_count_chunk--;
2352 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
2353 panic("Chunk count is negative");
2355 sctppcbinfo.ipi_gencnt_chunk++;
2362 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp,
2363 struct sctp_tcb *stcb, struct sctp_nets *net)
2365 struct sctp_association *asoc;
2368 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2369 kprintf("sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
2376 /* process according to association state */
2377 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
2378 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
2381 /* notify upper layer protocol */
2382 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL);
2383 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2384 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2385 stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
2386 stcb->sctp_ep->sctp_socket->so_snd.ssb_cc = 0;
2387 stcb->sctp_ep->sctp_socket->so_snd.ssb_mbcnt = 0;
2388 soisdisconnected(stcb->sctp_ep->sctp_socket);
2390 /* are the queues empty? they should be */
2391 if (!TAILQ_EMPTY(&asoc->send_queue) ||
2392 !TAILQ_EMPTY(&asoc->sent_queue) ||
2393 !TAILQ_EMPTY(&asoc->out_wheel)) {
2394 sctp_report_all_outbound(stcb);
2396 /* stop the timer */
2397 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
2399 sctp_free_assoc(stcb->sctp_ep, stcb);
2404 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
2405 struct sctp_nets *net, u_int8_t flg)
2407 switch (desc->chunk_type) {
2409 /* find the tsn to resend (possibly */
2412 struct sctp_tmit_chunk *tp1;
2413 tsn = ntohl(desc->tsn_ifany);
2414 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2416 if (tp1->rec.data.TSN_seq == tsn) {
2420 if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn,
2426 tp1 = TAILQ_NEXT(tp1, sctp_next);
2429 /* Do it the other way */
2430 sctp_pegs[SCTP_PDRP_DNFND]++;
2431 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2433 if (tp1->rec.data.TSN_seq == tsn) {
2437 tp1 = TAILQ_NEXT(tp1, sctp_next);
2441 sctp_pegs[SCTP_PDRP_TSNNF]++;
2443 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
2445 if (((tp1->rec.data.state_flags & SCTP_WINDOW_PROBE) == SCTP_WINDOW_PROBE) &&
2446 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
2447 sctp_pegs[SCTP_PDRP_DIWNP]++;
2450 if (stcb->asoc.peers_rwnd == 0 &&
2451 (flg & SCTP_FROM_MIDDLE_BOX)) {
2452 sctp_pegs[SCTP_PDRP_DIZRW]++;
2455 ddp = (u_int8_t *)(mtod(tp1->data, caddr_t) +
2456 sizeof(struct sctp_data_chunk));
2459 for (iii = 0; iii < sizeof(desc->data_bytes);
2461 if (ddp[iii] != desc->data_bytes[iii]) {
2462 sctp_pegs[SCTP_PDRP_BADD]++;
2467 if (tp1->sent != SCTP_DATAGRAM_RESEND) {
2468 stcb->asoc.sent_queue_retran_cnt++;
2470 /* We zero out the nonce so resync not needed */
2471 tp1->rec.data.ect_nonce = 0;
2475 * this guy had a RTO calculation pending on it,
2478 tp1->whoTo->rto_pending = 0;
2481 sctp_pegs[SCTP_PDRP_MARK]++;
2482 tp1->sent = SCTP_DATAGRAM_RESEND;
2484 * mark it as if we were doing a FR, since we
2485 * will be getting gap ack reports behind the
2486 * info from the router.
2488 tp1->rec.data.doing_fast_retransmit = 1;
2490 * mark the tsn with what sequences can cause a new FR.
2492 if (TAILQ_EMPTY(&stcb->asoc.send_queue) ) {
2493 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
2495 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
2498 /* restart the timer */
2499 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2501 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2504 /* fix counts and things */
2505 tp1->whoTo->flight_size -= tp1->send_size;
2506 if (tp1->whoTo->flight_size < 0) {
2507 tp1->whoTo->flight_size = 0;
2509 stcb->asoc.total_flight -= tp1->book_size;
2510 if (stcb->asoc.total_flight < 0) {
2511 stcb->asoc.total_flight = 0;
2513 stcb->asoc.total_flight_count--;
2514 if (stcb->asoc.total_flight_count < 0) {
2515 stcb->asoc.total_flight_count = 0;
2523 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
2524 if (tp1->sent == SCTP_DATAGRAM_RESEND)
2527 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
2529 if (tp1->sent == SCTP_DATAGRAM_RESEND)
2532 if (audit != stcb->asoc.sent_queue_retran_cnt) {
2533 kprintf("**Local Audit finds cnt:%d asoc cnt:%d\n",
2534 audit, stcb->asoc.sent_queue_retran_cnt);
2535 #ifndef SCTP_AUDITING_ENABLED
2536 stcb->asoc.sent_queue_retran_cnt = audit;
2544 struct sctp_tmit_chunk *asconf;
2545 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
2547 if (asconf->rec.chunk_id == SCTP_ASCONF) {
2552 if (asconf->sent != SCTP_DATAGRAM_RESEND)
2553 stcb->asoc.sent_queue_retran_cnt++;
2554 asconf->sent = SCTP_DATAGRAM_RESEND;
2555 asconf->snd_count--;
2559 case SCTP_INITIATION:
2560 /* resend the INIT */
2561 stcb->asoc.dropped_special_cnt++;
2562 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
2564 * If we can get it in, in a few attempts we do this,
2565 * otherwise we let the timer fire.
2567 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
2569 sctp_send_initiate(stcb->sctp_ep, stcb);
2572 case SCTP_SELECTIVE_ACK:
2573 /* resend the sack */
2574 sctp_send_sack(stcb);
2576 case SCTP_HEARTBEAT_REQUEST:
2577 /* resend a demand HB */
2578 sctp_send_hb(stcb, 1, net);
2582 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2583 kprintf("%s:%d sends a shutdown\n",
2589 sctp_send_shutdown(stcb, net);
2591 case SCTP_SHUTDOWN_ACK:
2592 sctp_send_shutdown_ack(stcb, net);
2594 case SCTP_COOKIE_ECHO:
2596 struct sctp_tmit_chunk *cookie;
2598 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
2600 if (cookie->rec.chunk_id == SCTP_COOKIE_ECHO) {
2605 if (cookie->sent != SCTP_DATAGRAM_RESEND)
2606 stcb->asoc.sent_queue_retran_cnt++;
2607 cookie->sent = SCTP_DATAGRAM_RESEND;
2608 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, stcb, net);
2612 case SCTP_COOKIE_ACK:
2613 sctp_send_cookie_ack(stcb);
2615 case SCTP_ASCONF_ACK:
2616 /* resend last asconf ack */
2617 sctp_send_asconf_ack(stcb, 1);
2619 case SCTP_FORWARD_CUM_TSN:
2620 send_forward_tsn(stcb, &stcb->asoc);
2622 /* can't do anything with these */
2623 case SCTP_PACKET_DROPPED:
2624 case SCTP_INITIATION_ACK: /* this should not happen */
2625 case SCTP_HEARTBEAT_ACK:
2626 case SCTP_ABORT_ASSOCIATION:
2627 case SCTP_OPERATION_ERROR:
2628 case SCTP_SHUTDOWN_COMPLETE:
2638 sctp_reset_in_stream(struct sctp_tcb *stcb,
2639 struct sctp_stream_reset_response *resp, int number_entries)
2642 uint16_t *list, temp;
2644 /* We set things to 0xffff since this is the last delivered
2645 * sequence and we will be sending in 0 after the reset.
2648 if (resp->reset_flags & SCTP_RESET_PERFORMED) {
2649 if (number_entries) {
2650 list = resp->list_of_streams;
2651 for (i = 0; i < number_entries; i++) {
2652 temp = ntohs(list[i]);
2654 if (list[i] >= stcb->asoc.streamincnt) {
2655 kprintf("Invalid stream in-stream reset %d\n", list[i]);
2658 stcb->asoc.strmin[(list[i])].last_sequence_delivered = 0xffff;
2662 for (i = 0; i < stcb->asoc.streamincnt; i++) {
2663 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
2666 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list);
2671 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
2673 struct sctp_tmit_chunk *chk, *nchk;
2674 struct sctp_association *asoc;
2678 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
2680 nchk = TAILQ_NEXT(chk, sctp_next);
2681 if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
2682 struct sctp_stream_reset_req *strreq;
2683 strreq = mtod(chk->data, struct sctp_stream_reset_req *);
2684 if (strreq->sr_req.ph.param_type == ntohs(SCTP_STR_RESET_RESPONSE)) {
2685 /* we only clean up the request */
2687 } else if (strreq->sr_req.ph.param_type != ntohs(SCTP_STR_RESET_REQUEST)) {
2688 kprintf("TSNH, an unknown stream reset request is in queue %x\n",
2689 (u_int)ntohs(strreq->sr_req.ph.param_type));
2692 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
2693 TAILQ_REMOVE(&asoc->control_send_queue,
2697 sctp_m_freem(chk->data);
2700 asoc->ctrl_queue_cnt--;
2701 sctp_free_remote_addr(chk->whoTo);
2702 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
2703 sctppcbinfo.ipi_count_chunk--;
2704 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
2705 panic("Chunk count is negative");
2707 sctppcbinfo.ipi_gencnt_chunk++;
2708 /* we can only have one of these so we break */
2716 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
2717 struct sctp_stream_reset_response *resp)
2720 int number_entries, param_length;
2722 param_length = ntohs(resp->ph.param_length);
2723 seq = ntohl(resp->reset_req_seq_resp);
2724 if (seq == stcb->asoc.str_reset_seq_out) {
2725 sctp_clean_up_stream_reset(stcb);
2726 stcb->asoc.str_reset_seq_out++;
2727 stcb->asoc.stream_reset_outstanding = 0;
2728 tsn = ntohl(resp->reset_at_tsn);
2729 number_entries = (param_length - sizeof(struct sctp_stream_reset_response))/sizeof(uint16_t);
2731 if ((tsn == stcb->asoc.cumulative_tsn) ||
2732 (compare_with_wrap(stcb->asoc.cumulative_tsn, tsn, MAX_TSN))) {
2733 /* no problem we are good to go */
2734 sctp_reset_in_stream(stcb, resp, number_entries);
2736 /* So, we have a stream reset but there
2737 * is pending data. We need to copy
2738 * out the stream_reset and then queue
2739 * any data = or > resp->reset_at_tsn
2741 if (stcb->asoc.pending_reply != NULL) {
2743 * This IS WRONG. We need
2744 * to queue each of these up
2745 * and only release the chunks
2746 * for each reset that the cum-ack
2747 * goes by. This is a short cut.
2749 FREE(stcb->asoc.pending_reply, M_PCB);
2750 stcb->asoc.pending_reply = NULL;
2752 MALLOC(stcb->asoc.pending_reply, struct sctp_stream_reset_response *, param_length,
2754 if (stcb->asoc.pending_reply == NULL)
2756 memcpy(stcb->asoc.pending_reply, resp, param_length);
2762 kprintf("Duplicate old stream reset resp next:%x this one:%x\n",
2763 stcb->asoc.str_reset_seq_out, seq);
2770 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct sctp_stream_reset_req *sr_req)
2772 int chk_length, param_len;
2773 struct sctp_paramhdr *ph;
2774 /* now it may be a reset or a reset-response */
2775 struct sctp_stream_reset_request *req;
2776 struct sctp_stream_reset_response *resp;
2777 chk_length = ntohs(sr_req->ch.chunk_length);
2779 ph = (struct sctp_paramhdr *)&sr_req->sr_req;
2780 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_request)) {
2781 param_len = ntohs(ph->param_length);
2782 if (ntohs(ph->param_type) == SCTP_STR_RESET_REQUEST) {
2783 /* this will send the ACK and do the reset if needed */
2784 req = (struct sctp_stream_reset_request *)ph;
2785 sctp_send_str_reset_ack(stcb, req);
2786 } else if (ntohs(ph->param_type) == SCTP_STR_RESET_RESPONSE) {
2787 /* Now here is a tricky one. We reset our receive side
2788 * of the streams. But what happens if the peers
2789 * next sending TSN is NOT equal to 1 minus our cumack?
2790 * And if his cumack is not equal to our next one out - 1
2791 * we have another problem if this is receprical.
2793 resp = (struct sctp_stream_reset_response *)ph;
2794 sctp_handle_stream_reset_response(stcb, resp);
2796 ph = (struct sctp_paramhdr *)((caddr_t)ph + SCTP_SIZE32(param_len));
2797 chk_length -= SCTP_SIZE32(param_len);
2802 * Handle a router or endpoints report of a packet loss, there
2803 * are two ways to handle this, either we get the whole packet
2804 * and must disect it ourselves (possibly with truncation and
2805 * or corruption) or it is a summary from a middle box that did
2806 * the disectting for us.
2809 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
2810 struct sctp_tcb *stcb, struct sctp_nets *net)
2812 u_int32_t bottle_bw, on_queue;
2813 u_int16_t trunc_len;
2816 struct sctp_chunk_desc desc;
2817 struct sctp_chunkhdr *ch;
2819 chlen = ntohs(cp->ch.chunk_length);
2820 chlen -= sizeof(struct sctp_pktdrop_chunk);
2821 /* XXX possible chlen underflow */
2824 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
2825 sctp_pegs[SCTP_PDRP_BWRPT]++;
2827 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
2828 chlen -= sizeof(struct sctphdr);
2829 /* XXX possible chlen underflow */
2830 memset(&desc, 0, sizeof(desc));
2833 /* first update a rwnd possibly */
2834 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
2835 /* From a peer, we get a rwnd report */
2838 sctp_pegs[SCTP_PDRP_FEHOS]++;
2840 bottle_bw = ntohl(cp->bottle_bw);
2841 on_queue = ntohl(cp->current_onq);
2842 if (bottle_bw && on_queue) {
2843 /* a rwnd report is in here */
2844 if (bottle_bw > on_queue)
2845 a_rwnd = bottle_bw - on_queue;
2850 stcb->asoc.peers_rwnd = 0;
2852 if (a_rwnd > stcb->asoc.total_flight) {
2853 stcb->asoc.peers_rwnd =
2854 a_rwnd - stcb->asoc.total_flight;
2856 stcb->asoc.peers_rwnd = 0;
2858 if (stcb->asoc.peers_rwnd <
2859 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
2860 /* SWS sender side engages */
2861 stcb->asoc.peers_rwnd = 0;
2866 sctp_pegs[SCTP_PDRP_FMBOX]++;
2868 trunc_len = (u_int16_t)ntohs(cp->trunc_len);
2869 /* now the chunks themselves */
2870 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
2871 desc.chunk_type = ch->chunk_type;
2872 /* get amount we need to move */
2873 at = ntohs(ch->chunk_length);
2874 if (at < sizeof(struct sctp_chunkhdr)) {
2875 /* corrupt chunk, maybe at the end? */
2876 sctp_pegs[SCTP_PDRP_CRUPT]++;
2879 if (trunc_len == 0) {
2880 /* we are supposed to have all of it */
2882 /* corrupt skip it */
2883 sctp_pegs[SCTP_PDRP_CRUPT]++;
2887 /* is there enough of it left ? */
2888 if (desc.chunk_type == SCTP_DATA) {
2889 if (chlen < (sizeof(struct sctp_data_chunk) +
2890 sizeof(desc.data_bytes))) {
2894 if (chlen < sizeof(struct sctp_chunkhdr)) {
2899 if (desc.chunk_type == SCTP_DATA) {
2900 /* can we get out the tsn? */
2901 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
2902 sctp_pegs[SCTP_PDRP_MB_DA]++;
2904 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(u_int32_t)) ) {
2906 struct sctp_data_chunk *dcp;
2909 dcp = (struct sctp_data_chunk *)ch;
2910 ddp = (u_int8_t *)(dcp + 1);
2911 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
2912 desc.data_bytes[iii] = ddp[iii];
2914 desc.tsn_ifany = dcp->dp.tsn;
2916 /* nope we are done. */
2917 sctp_pegs[SCTP_PDRP_NEDAT]++;
2921 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
2922 sctp_pegs[SCTP_PDRP_MB_CT]++;
2925 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
2926 sctp_pegs[SCTP_PDRP_PDBRK]++;
2929 if (SCTP_SIZE32(at) > chlen) {
2932 chlen -= SCTP_SIZE32(at);
2933 if (chlen < sizeof(struct sctp_chunkhdr)) {
2934 /* done, none left */
2937 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
2940 /* now middle boxes in sat networks get a cwnd bump */
2941 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
2942 (stcb->asoc.sat_t3_loss_recovery == 0) &&
2943 (stcb->asoc.sat_network)) {
2945 * This is debateable but for sat networks it makes sense
2946 * Note if a T3 timer has went off, we will prohibit any
2947 * changes to cwnd until we exit the t3 loss recovery.
2951 #ifdef SCTP_CWND_LOGGING
2952 int old_cwnd=net->cwnd;
2954 /* need real RTT for this calc */
2955 rtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2956 /* get bottle neck bw */
2957 bottle_bw = ntohl(cp->bottle_bw);
2958 /* and whats on queue */
2959 on_queue = ntohl(cp->current_onq);
2961 * adjust the on-queue if our flight is more it could be
2962 * that the router has not yet gotten data "in-flight" to it
2964 if (on_queue < net->flight_size)
2965 on_queue = net->flight_size;
2967 /* calculate the available space */
2968 bw_avail = (bottle_bw*rtt)/1000;
2969 if (bw_avail > bottle_bw) {
2971 * Cap the growth to no more than the bottle neck.
2972 * This can happen as RTT slides up due to queues.
2973 * It also means if you have more than a 1 second
2974 * RTT with a empty queue you will be limited to
2975 * the bottle_bw per second no matter if
2976 * other points have 1/2 the RTT and you could
2979 bw_avail = bottle_bw;
2982 if (on_queue > bw_avail) {
2984 * No room for anything else don't allow anything
2985 * else to be "added to the fire".
2987 int seg_inflight, seg_onqueue, my_portion;
2988 net->partial_bytes_acked = 0;
2990 /* how much are we over queue size? */
2991 incr = on_queue - bw_avail;
2992 if (stcb->asoc.seen_a_sack_this_pkt) {
2993 /* undo any cwnd adjustment that
2994 * the sack might have made
2996 net->cwnd = net->prev_cwnd;
2999 /* Now how much of that is mine? */
3000 seg_inflight = net->flight_size / net->mtu;
3001 seg_onqueue = on_queue / net->mtu;
3002 my_portion = (incr * seg_inflight)/seg_onqueue;
3004 /* Have I made an adjustment already */
3005 if (net->cwnd > net->flight_size) {
3006 /* for this flight I made an adjustment
3007 * we need to decrease the portion by a share
3008 * our previous adjustment.
3011 diff_adj = net->cwnd - net->flight_size;
3012 if (diff_adj > my_portion)
3015 my_portion -= diff_adj;
3018 /* back down to the previous cwnd (assume
3019 * we have had a sack before this packet). minus
3020 * what ever portion of the overage is my fault.
3022 net->cwnd -= my_portion;
3024 /* we will NOT back down more than 1 MTU */
3025 if (net->cwnd <= net->mtu) {
3026 net->cwnd = net->mtu;
3029 net->ssthresh = net->cwnd - 1;
3032 * Take 1/4 of the space left or
3033 * max burst up .. whichever is less.
3035 incr = min((bw_avail - on_queue) >> 2,
3036 (int)stcb->asoc.max_burst * (int)net->mtu);
3039 if (net->cwnd > bw_avail) {
3040 /* We can't exceed the pipe size */
3041 net->cwnd = bw_avail;
3043 if (net->cwnd < net->mtu) {
3044 /* We always have 1 MTU */
3045 net->cwnd = net->mtu;
3047 #ifdef SCTP_CWND_LOGGING
3048 if (net->cwnd - old_cwnd != 0) {
3049 /* log only changes */
3050 sctp_log_cwnd(net, (net->cwnd - old_cwnd),
3051 SCTP_CWND_LOG_FROM_SAT);
3057 extern int sctp_strict_init;
3060 * handles all control chunks in a packet
3062 * - m: mbuf chain, assumed to still contain IP/SCTP header
3063 * - stcb: is the tcb found for this packet
3064 * - offset: offset into the mbuf chain to first chunkhdr
3065 * - length: is the length of the complete packet
3067 * - length: modified to remaining length after control processing
3068 * - netp: modified to new sctp_nets after cookie-echo processing
3069 * - return NULL to discard the packet (ie. no asoc, bad packet,...)
3070 * otherwise return the tcb for this packet
3072 static struct sctp_tcb *
3073 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
3074 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
3075 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen)
3077 struct sctp_association *asoc;
3079 int num_chunks = 0; /* number of control chunks processed */
3084 * How big should this be, and should it be alloc'd?
3085 * Lets try the d-mtu-ceiling for now (2k) and that should
3086 * hopefully work ... until we get into jumbo grams and such..
3088 u_int8_t chunk_buf[DEFAULT_CHUNK_BUFFER];
3089 struct sctp_tcb *locked_tcb = stcb;
3092 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3093 kprintf("sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
3094 iphlen, *offset, length, stcb);
3096 #endif /* SCTP_DEBUG */
3098 /* validate chunk header length... */
3099 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
3104 * validate the verification tag
3107 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3108 kprintf("sctp_process_control: validating vtags\n");
3110 #endif /* SCTP_DEBUG */
3111 vtag_in = ntohl(sh->v_tag);
3112 if (ch->chunk_type == SCTP_INITIATION) {
3114 /* protocol error- silently discard... */
3116 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3117 kprintf("sctp_process_control: INIT with vtag != 0\n");
3119 #endif /* SCTP_DEBUG */
3120 sctp_pegs[SCTP_BAD_VTAGS]++;
3122 SCTP_TCB_UNLOCK(locked_tcb);
3125 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
3127 * first check if it's an ASCONF with an unknown src addr
3128 * we need to look inside to find the association
3130 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
3131 stcb = sctp_findassociation_ep_asconf(m, iphlen,
3132 *offset, sh, &inp, netp);
3135 /* no association, so it's out of the blue... */
3136 sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL);
3138 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3139 kprintf("sctp_process_control: handling OOTB packet, chunk type=%xh\n",
3142 #endif /* SCTP_DEBUG */
3145 SCTP_TCB_UNLOCK(locked_tcb);
3149 /* ABORT and SHUTDOWN can use either v_tag... */
3150 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
3151 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
3152 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
3153 if ((vtag_in == asoc->my_vtag) ||
3154 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
3155 (vtag_in == asoc->peer_vtag))) {
3158 /* drop this packet... */
3159 sctp_pegs[SCTP_BAD_VTAGS]++;
3161 SCTP_TCB_UNLOCK(locked_tcb);
3164 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
3165 if (vtag_in != asoc->my_vtag) {
3167 * this could be a stale SHUTDOWN-ACK or the
3168 * peer never got the SHUTDOWN-COMPLETE and
3169 * is still hung; we have started a new asoc
3170 * but it won't complete until the shutdown is
3174 SCTP_TCB_UNLOCK(locked_tcb);
3175 sctp_handle_ootb(m, iphlen, *offset, sh, inp,
3180 /* for all other chunks, vtag must match */
3182 if (vtag_in != asoc->my_vtag) {
3183 /* invalid vtag... */
3185 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3186 kprintf("invalid vtag: %xh, expect %xh\n", vtag_in, asoc->my_vtag);
3188 #endif /* SCTP_DEBUG */
3189 sctp_pegs[SCTP_BAD_VTAGS]++;
3191 SCTP_TCB_UNLOCK(locked_tcb);
3196 } /* end if !SCTP_COOKIE_ECHO */
3199 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3200 kprintf("sctp_process_control: vtags ok, processing ctrl chunks\n");
3202 #endif /* SCTP_DEBUG */
3205 * process all control chunks...
3207 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
3208 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
3209 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
3210 /* implied cookie-ack.. we must have lost the ack */
3211 stcb->asoc.overall_error_count = 0;
3212 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
3215 while (IS_SCTP_CONTROL(ch)) {
3216 /* validate chunk length */
3217 chk_length = ntohs(ch->chunk_length);
3219 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
3220 kprintf("sctp_process_control: processing a chunk type=%u, len=%u\n", ch->chunk_type, chk_length);
3222 #endif /* SCTP_DEBUG */
3223 if ((size_t)chk_length < sizeof(*ch) ||
3224 (*offset + chk_length) > length) {
3226 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3227 kprintf("sctp_process_control: chunk length invalid! *offset:%u, chk_length:%u > length:%u\n",
3228 *offset, length, chk_length);
3230 #endif /* SCTP_DEBUG */
3233 SCTP_TCB_UNLOCK(locked_tcb);
3238 * INIT-ACK only gets the init ack "header" portion only
3239 * because we don't have to process the peer's COOKIE.
3240 * All others get a complete chunk.
3242 if (ch->chunk_type == SCTP_INITIATION_ACK) {
3243 /* get an init-ack chunk */
3244 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3245 sizeof(struct sctp_init_ack), chunk_buf);
3249 SCTP_TCB_UNLOCK(locked_tcb);
3253 /* get a complete chunk... */
3254 if ((size_t)chk_length > sizeof(chunk_buf)) {
3256 struct sctp_paramhdr *phdr;
3258 MGETHDR(oper, MB_DONTWAIT, MT_HEADER);
3260 /* pre-reserve some space */
3262 sizeof(struct sctp_chunkhdr);
3264 mtod(oper, struct sctp_paramhdr *);
3266 htons(SCTP_CAUSE_OUT_OF_RESC);
3267 phdr->param_length =
3268 htons(sizeof(struct sctp_paramhdr));
3269 sctp_queue_op_err(stcb, oper);
3272 SCTP_TCB_UNLOCK(locked_tcb);
3275 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3276 chk_length, chunk_buf);
3278 kprintf("sctp_process_control: Can't get the all data....\n");
3281 SCTP_TCB_UNLOCK(locked_tcb);
3287 /* Save off the last place we got a control from */
3288 if ((*netp) && stcb) {
3289 stcb->asoc.last_control_chunk_from = *netp;
3291 #ifdef SCTP_AUDITING_ENABLED
3292 sctp_audit_log(0xB0, ch->chunk_type);
3294 switch (ch->chunk_type) {
3295 case SCTP_INITIATION:
3296 /* must be first and only chunk */
3298 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3299 kprintf("SCTP_INIT\n");
3301 #endif /* SCTP_DEBUG */
3302 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3303 /* We are not interested anymore */
3305 SCTP_TCB_UNLOCK(locked_tcb);
3306 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3307 /* finish the job now */
3308 sctp_inpcb_free(inp, 1);
3313 if ((num_chunks > 1) ||
3314 (sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) {
3317 SCTP_TCB_UNLOCK(locked_tcb);
3320 if ((stcb != NULL) &&
3321 (SCTP_GET_STATE(&stcb->asoc) ==
3322 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3323 sctp_send_shutdown_ack(stcb,
3324 stcb->asoc.primary_destination);
3327 SCTP_TCB_UNLOCK(locked_tcb);
3330 sctp_handle_init(m, iphlen, *offset, sh,
3331 (struct sctp_init_chunk *)ch, inp, stcb, *netp);
3334 SCTP_TCB_UNLOCK(locked_tcb);
3337 case SCTP_INITIATION_ACK:
3338 /* must be first and only chunk */
3340 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3341 kprintf("SCTP_INIT-ACK\n");
3343 #endif /* SCTP_DEBUG */
3344 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3345 /* We are not interested anymore */
3347 SCTP_TCB_UNLOCK(locked_tcb);
3350 sctp_free_assoc(inp, stcb);
3352 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3353 /* finish the job now */
3354 sctp_inpcb_free(inp, 1);
3359 if ((num_chunks > 1) ||
3360 (sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) {
3362 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3363 kprintf("Length is %d rounded chk_length:%d .. dropping\n",
3365 SCTP_SIZE32(chk_length));
3370 SCTP_TCB_UNLOCK(locked_tcb);
3373 ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
3374 (struct sctp_init_ack_chunk *)ch, stcb, *netp);
3376 * Special case, I must call the output routine
3377 * to get the cookie echoed
3379 if ((stcb) && ret == 0)
3380 sctp_chunk_output(stcb->sctp_ep, stcb, 2);
3383 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3384 kprintf("All done INIT-ACK processing\n");
3388 SCTP_TCB_UNLOCK(locked_tcb);
3391 case SCTP_SELECTIVE_ACK:
3393 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3394 kprintf("SCTP_SACK\n");
3396 #endif /* SCTP_DEBUG */
3397 sctp_pegs[SCTP_PEG_SACKS_SEEN]++;
3400 stcb->asoc.seen_a_sack_this_pkt = 1;
3401 sctp_handle_sack((struct sctp_sack_chunk *)ch,
3402 stcb, *netp, &abort_now);
3404 /* ABORT signal from sack processing */
3410 case SCTP_HEARTBEAT_REQUEST:
3412 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3413 kprintf("SCTP_HEARTBEAT\n");
3415 #endif /* SCTP_DEBUG */
3416 sctp_pegs[SCTP_HB_RECV]++;
3417 sctp_send_heartbeat_ack(stcb, m, *offset, chk_length,
3420 /* He's alive so give him credit */
3421 stcb->asoc.overall_error_count = 0;
3423 case SCTP_HEARTBEAT_ACK:
3425 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3426 kprintf("SCTP_HEARTBEAT-ACK\n");
3428 #endif /* SCTP_DEBUG */
3430 /* He's alive so give him credit */
3431 stcb->asoc.overall_error_count = 0;
3433 sctp_pegs[SCTP_HB_ACK_RECV]++;
3434 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
3437 case SCTP_ABORT_ASSOCIATION:
3439 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3440 kprintf("SCTP_ABORT\n");
3442 #endif /* SCTP_DEBUG */
3443 sctp_handle_abort((struct sctp_abort_chunk *)ch,
3450 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3451 kprintf("SCTP_SHUTDOWN\n");
3453 #endif /* SCTP_DEBUG */
3456 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
3457 stcb, *netp, &abort_flag);
3464 case SCTP_SHUTDOWN_ACK:
3466 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3467 kprintf("SCTP_SHUTDOWN-ACK\n");
3469 #endif /* SCTP_DEBUG */
3470 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
3474 case SCTP_OPERATION_ERROR:
3476 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3477 kprintf("SCTP_OP-ERR\n");
3479 #endif /* SCTP_DEBUG */
3480 if (sctp_handle_error(ch, stcb, *netp) < 0) {
3485 case SCTP_COOKIE_ECHO:
3487 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3488 kprintf("SCTP_COOKIE-ECHO stcb is %p\n", stcb);
3490 #endif /* SCTP_DEBUG */
3491 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3492 /* We are not interested anymore */
3495 sctp_free_assoc(inp, stcb);
3497 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3498 /* finish the job now */
3499 sctp_inpcb_free(inp, 1);
3505 * First are we accepting?
3506 * We do this again here since it is possible
3507 * that a previous endpoint WAS listening responded to
3508 * a INIT-ACK and then closed. We opened and bound..
3509 * and are now no longer listening.
3511 if (((inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) == 0) ||
3512 (inp->sctp_socket->so_qlimit == 0)) {
3513 sctp_abort_association(inp, stcb, m, iphlen, sh,
3517 } else if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
3518 /* we are accepting so check limits like TCP */
3519 if (inp->sctp_socket->so_qlen >
3520 inp->sctp_socket->so_qlimit) {
3523 struct sctp_paramhdr *phdr;
3525 MGETHDR(oper, MB_DONTWAIT, MT_HEADER);
3528 oper->m_pkthdr.len =
3529 sizeof(struct sctp_paramhdr);
3531 struct sctp_paramhdr *);
3533 htons(SCTP_CAUSE_OUT_OF_RESC);
3534 phdr->param_length =
3535 htons(sizeof(struct sctp_paramhdr));
3537 sctp_abort_association(inp, stcb, m,
3544 struct mbuf *ret_buf;
3545 ret_buf = sctp_handle_cookie_echo(m, iphlen,
3547 (struct sctp_cookie_echo_chunk *)ch, &inp,
3550 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3551 kprintf("ret_buf:%p length:%d off:%d\n",
3552 ret_buf, length, *offset);
3554 #endif /* SCTP_DEBUG */
3556 if (ret_buf == NULL) {
3558 SCTP_TCB_UNLOCK(locked_tcb);
3561 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3562 kprintf("GAK, null buffer\n");
3564 #endif /* SCTP_DEBUG */
3568 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
3570 * Restart the timer if we have pending
3573 struct sctp_tmit_chunk *chk;
3574 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
3576 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
3577 stcb->sctp_ep, stcb,
3583 case SCTP_COOKIE_ACK:
3585 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3586 kprintf("SCTP_COOKIE-ACK\n");
3588 #endif /* SCTP_DEBUG */
3590 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3591 /* We are not interested anymore */
3592 sctp_free_assoc(inp, stcb);
3596 /* He's alive so give him credit */
3597 stcb->asoc.overall_error_count = 0;
3598 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch,
3603 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3604 kprintf("SCTP_ECN-ECHO\n");
3606 #endif /* SCTP_DEBUG */
3607 /* He's alive so give him credit */
3608 stcb->asoc.overall_error_count = 0;
3609 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
3614 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3615 kprintf("SCTP_ECN-CWR\n");
3617 #endif /* SCTP_DEBUG */
3618 /* He's alive so give him credit */
3619 stcb->asoc.overall_error_count = 0;
3621 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb);
3623 case SCTP_SHUTDOWN_COMPLETE:
3625 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3626 kprintf("SCTP_SHUTDOWN-COMPLETE\n");
3628 #endif /* SCTP_DEBUG */
3629 /* must be first and only chunk */
3630 if ((num_chunks > 1) ||
3631 (length - *offset > SCTP_SIZE32(chk_length))) {
3634 SCTP_TCB_UNLOCK(locked_tcb);
3638 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
3645 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3646 kprintf("SCTP_ASCONF\n");
3648 #endif /* SCTP_DEBUG */
3649 /* He's alive so give him credit */
3650 stcb->asoc.overall_error_count = 0;
3652 sctp_handle_asconf(m, *offset,
3653 (struct sctp_asconf_chunk *)ch, stcb, *netp);
3655 case SCTP_ASCONF_ACK:
3657 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3658 kprintf("SCTP_ASCONF-ACK\n");
3660 #endif /* SCTP_DEBUG */
3661 /* He's alive so give him credit */
3662 stcb->asoc.overall_error_count = 0;
3664 sctp_handle_asconf_ack(m, *offset,
3665 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp);
3667 case SCTP_FORWARD_CUM_TSN:
3669 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3670 kprintf("SCTP_FWD-TSN\n");
3672 #endif /* SCTP_DEBUG */
3673 /* He's alive so give him credit */
3676 stcb->asoc.overall_error_count = 0;
3678 sctp_handle_forward_tsn(stcb,
3679 (struct sctp_forward_tsn_chunk *)ch, &abort_flag);
3684 stcb->asoc.overall_error_count = 0;
3689 case SCTP_STREAM_RESET:
3691 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3692 kprintf("SCTP_STREAM_RESET\n");
3694 #endif /* SCTP_DEBUG */
3695 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3696 chk_length, chunk_buf);
3697 if (stcb->asoc.peer_supports_strreset == 0) {
3698 /* hmm, peer should have annonced this, but
3699 * we will turn it on since he is sending us
3702 stcb->asoc.peer_supports_strreset = 1;
3704 sctp_handle_stream_reset(stcb, (struct sctp_stream_reset_req *)ch);
3706 case SCTP_PACKET_DROPPED:
3708 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3709 kprintf("SCTP_PACKET_DROPPED\n");
3711 #endif /* SCTP_DEBUG */
3712 /* re-get it all please */
3713 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3714 chk_length, chunk_buf);
3716 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
3722 /* it's an unknown chunk! */
3723 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
3725 struct sctp_paramhdr *phd;
3726 MGETHDR(mm, MB_DONTWAIT, MT_HEADER);
3728 phd = mtod(mm, struct sctp_paramhdr *);
3729 /* We cheat and use param type since we
3730 * did not bother to define a error
3732 * They are the same basic format with
3736 htons(SCTP_CAUSE_UNRECOG_CHUNK);
3738 htons(chk_length + sizeof(*phd));
3739 mm->m_len = sizeof(*phd);
3740 mm->m_next = sctp_m_copym(m, *offset,
3741 SCTP_SIZE32(chk_length),
3745 SCTP_SIZE32(chk_length) +
3747 sctp_queue_op_err(stcb, mm);
3752 SCTP_DEBUG_INPUT1) {
3753 kprintf("Gak can't copy the chunk into operr %d bytes\n",
3761 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3762 kprintf("Gak can't mgethdr for op-err of unrec chunk\n");
3767 if ((ch->chunk_type & 0x80) == 0) {
3768 /* discard this packet */
3771 } /* else skip this bad chunk and continue... */
3773 } /* switch (ch->chunk_type) */
3774 /* get the next chunk */
3775 *offset += SCTP_SIZE32(chk_length);
3776 if (*offset >= length) {
3777 /* no more data left in the mbuf chain */
3780 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3781 sizeof(struct sctp_chunkhdr), chunk_buf);
3784 SCTP_TCB_UNLOCK(locked_tcb);
3794 * Process the ECN bits we have something set so
3795 * we must look to see if it is ECN(0) or ECN(1) or CE
3798 sctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net,
3801 if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
3803 } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) {
3805 * we only add to the nonce sum for ECT1, ECT0
3806 * does not change the NS bit (that we have
3807 * yet to find a way to send it yet).
3810 /* ECN Nonce stuff */
3811 stcb->asoc.receiver_nonce_sum++;
3812 stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM;
3815 * Drag up the last_echo point if cumack is larger since we
3816 * don't want the point falling way behind by more than 2^^31
3817 * and then having it be incorrect.
3819 if (compare_with_wrap(stcb->asoc.cumulative_tsn,
3820 stcb->asoc.last_echo_tsn, MAX_TSN)) {
3821 stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
3823 } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) {
3825 * Drag up the last_echo point if cumack is larger since we
3826 * don't want the point falling way behind by more than 2^^31
3827 * and then having it be incorrect.
3829 if (compare_with_wrap(stcb->asoc.cumulative_tsn,
3830 stcb->asoc.last_echo_tsn, MAX_TSN)) {
3831 stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
3837 sctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net,
3838 u_int32_t high_tsn, u_int8_t ecn_bits)
3840 if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
3842 * we possibly must notify the sender that a congestion
3843 * window reduction is in order. We do this
3844 * by adding a ECNE chunk to the output chunk
3845 * queue. The incoming CWR will remove this chunk.
3847 if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn,
3849 /* Yep, we need to add a ECNE */
3850 sctp_send_ecn_echo(stcb, net, high_tsn);
3851 stcb->asoc.last_echo_tsn = high_tsn;
3857 * common input chunk processing (v4 and v6)
3860 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
3861 int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
3862 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
3866 * Control chunk processing
3869 int fwd_tsn_seen = 0, data_processed = 0;
3870 struct mbuf *m = *mm;
3873 sctp_pegs[SCTP_DATAGRAMS_RCVD]++;
3874 #ifdef SCTP_AUDITING_ENABLED
3875 sctp_audit_log(0xE0, 1);
3876 sctp_auditing(0, inp, stcb, net);
3880 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3881 kprintf("Ok, Common input processing called, m:%x iphlen:%d offset:%d\n",
3882 (u_int)m, iphlen, offset);
3884 #endif /* SCTP_DEBUG */
3885 if (IS_SCTP_CONTROL(ch)) {
3886 /* process the control portion of the SCTP packet */
3888 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3889 kprintf("Processing control\n");
3891 #endif /* SCTP_DEBUG */
3893 stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
3894 inp, stcb, &net, &fwd_tsn_seen);
3897 * no control chunks, so pre-process DATA chunks
3898 * (these checks are taken care of by control processing)
3901 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3902 kprintf("No control present\n");
3904 #endif /* SCTP_DEBUG */
3907 /* out of the blue DATA chunk */
3908 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL);
3911 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
3912 /* v_tag mismatch! */
3913 sctp_pegs[SCTP_BAD_VTAGS]++;
3914 SCTP_TCB_UNLOCK(stcb);
3920 * no valid TCB for this packet,
3921 * or we found it's a bad packet while processing control,
3922 * or we're done with this packet (done or skip rest of data),
3928 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3929 kprintf("Ok, control finished time to look for data (%d) offset:%d\n",
3932 #endif /* SCTP_DEBUG */
3934 * DATA chunk processing
3936 /* plow through the data chunks while length > offset */
3937 stcb->asoc.seen_a_sack_this_pkt = 0;
3939 if (length > offset) {
3942 * First check to make sure our state is correct.
3943 * We would not get here unless we really did have a
3944 * tag, so we don't abort if this happens, just
3945 * dump the chunk silently.
3947 switch (SCTP_GET_STATE(&stcb->asoc)) {
3948 case SCTP_STATE_COOKIE_ECHOED:
3950 * we consider data with valid tags in
3951 * this state shows us the cookie-ack was lost.
3952 * Imply it was there.
3954 stcb->asoc.overall_error_count = 0;
3955 sctp_handle_cookie_ack(
3956 (struct sctp_cookie_ack_chunk *)ch, stcb, net);
3958 case SCTP_STATE_COOKIE_WAIT:
3960 * We consider OOTB any data sent during asoc setup.
3962 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL);
3963 SCTP_TCB_UNLOCK(stcb);
3966 case SCTP_STATE_EMPTY: /* should not happen */
3967 case SCTP_STATE_INUSE: /* should not happen */
3968 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
3969 case SCTP_STATE_SHUTDOWN_ACK_SENT:
3972 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3973 kprintf("Got data in invalid state %d.. dropping\n", stcb->asoc.state);
3976 SCTP_TCB_UNLOCK(stcb);
3979 case SCTP_STATE_OPEN:
3980 case SCTP_STATE_SHUTDOWN_SENT:
3983 /* take care of ECN, part 1. */
3984 if (stcb->asoc.ecn_allowed &&
3985 (ecn_bits & (SCTP_ECT0_BIT|SCTP_ECT1_BIT)) ) {
3986 sctp_process_ecn_marked_a(stcb, net, ecn_bits);
3988 /* plow through the data chunks while length > offset */
3989 retval = sctp_process_data(mm, iphlen, &offset, length, sh,
3990 inp, stcb, net, &high_tsn);
3992 /* The association aborted, NO UNLOCK needed
3993 * since the association is destroyed.
4000 /* take care of ecn part 2. */
4001 if (stcb->asoc.ecn_allowed && (ecn_bits & (SCTP_ECT0_BIT|SCTP_ECT1_BIT)) ) {
4002 sctp_process_ecn_marked_b(stcb, net, high_tsn, ecn_bits);
4008 * Anything important needs to have been m_copy'ed in
4012 if ((data_processed == 0) && (fwd_tsn_seen)) {
4014 if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
4015 stcb->asoc.cumulative_tsn, MAX_TSN)) {
4016 /* there was a gap before this data was processed */
4019 sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
4021 /* Again, we aborted so NO UNLOCK needed */
4025 /* trigger send of any chunks in queue... */
4026 #ifdef SCTP_AUDITING_ENABLED
4027 sctp_audit_log(0xE0, 2);
4028 sctp_auditing(1, inp, stcb, net);
4031 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4032 kprintf("Check for chunk output prw:%d tqe:%d tf=%d\n",
4033 stcb->asoc.peers_rwnd,
4034 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
4035 stcb->asoc.total_flight);
4038 if (stcb->asoc.peers_rwnd > 0 ||
4039 !TAILQ_EMPTY(&stcb->asoc.control_send_queue) ||
4040 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)) {
4042 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4043 kprintf("Calling chunk OUTPUT\n");
4046 sctp_chunk_output(inp, stcb, 3);
4048 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4049 kprintf("chunk OUTPUT returns\n");
4054 #ifdef SCTP_AUDITING_ENABLED
4055 sctp_audit_log(0xE0, 3);
4056 sctp_auditing(2, inp, stcb, net);
4058 SCTP_TCB_UNLOCK(stcb);
4062 #if defined(__OpenBSD__)
4064 sctp_saveopt(struct sctp_inpcb *inp, struct mbuf **mp, struct ip *ip,
4067 if (inp->ip_inp.inp.inp_flags & INP_RECVDSTADDR) {
4068 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
4069 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
4071 mp = &(*mp)->m_next;
4076 extern int sctp_no_csum_on_loopback;
4079 sctp_input(struct mbuf **mp, int *offp, int proto)
4082 struct mbuf *m = *mp;
4086 struct sctp_inpcb *inp = NULL;
4087 struct mbuf *opts = 0;
4089 /* Don't think this is needed */
4090 /* struct ip6_recvpktopts opts6;*/
4093 u_int32_t check, calc_check;
4094 struct sctp_nets *net;
4095 struct sctp_tcb *stcb = NULL;
4096 struct sctp_chunkhdr *ch;
4097 int refcount_up = 0;
4098 int length, mlen, offset;
4099 #if defined(__OpenBSD__) && defined(IPSEC)
4100 struct inpcb *i_inp;
4102 struct tdb_ident *tdbi;
4109 sctp_pegs[SCTP_INPKTS]++;
4111 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4112 kprintf("V4 input gets a packet iphlen:%d pktlen:%d\n", iphlen, m->m_pkthdr.len);
4116 /* Don't think this is needed */
4117 /* bzero(&opts6, sizeof(opts6));*/
4121 * Strip IP options, we don't allow any in or out.
4123 if ((size_t)iphlen > sizeof(struct ip)) {
4125 iphlen = sizeof(struct ip);
4129 * Get IP, SCTP, and first chunk header together in first mbuf.
4131 ip = mtod(m, struct ip *);
4132 offset = iphlen + sizeof(*sh) + sizeof(*ch);
4133 if (m->m_len < offset) {
4134 if ((m = m_pullup(m, offset)) == 0) {
4135 sctp_pegs[SCTP_HDR_DROPS]++;
4136 return(IPPROTO_DONE);
4138 ip = mtod(m, struct ip *);
4140 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
4141 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
4143 /* SCTP does not allow broadcasts or multicasts */
4144 #if defined(__NetBSD__) || defined(__OpenBSD__)
4145 if (IN_MULTICAST(ip->ip_dst.s_addr))
4147 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)))
4150 sctp_pegs[SCTP_IN_MCAST]++;
4153 if (in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
4154 sctp_pegs[SCTP_IN_MCAST]++;
4158 /* destination port of 0 is illegal, based on RFC2960. */
4159 if (sh->dest_port == 0) {
4160 sctp_pegs[SCTP_HDR_DROPS]++;
4164 /* validate SCTP checksum */
4165 if ((sctp_no_csum_on_loopback == 0) ||
4166 (m->m_pkthdr.rcvif == NULL) ||
4167 (m->m_pkthdr.rcvif->if_type != IFT_LOOP)) {
4168 /* we do NOT validate things from the loopback if the
4169 * sysctl is set to 1.
4171 check = sh->checksum; /* save incoming checksum */
4172 if ((check == 0) && (sctp_no_csum_on_loopback)) {
4173 /* special hook for where we got a local address
4174 * somehow routed across a non IFT_LOOP type interface
4176 if (ip->ip_src.s_addr == ip->ip_dst.s_addr)
4177 goto sctp_skip_csum_4;
4179 sh->checksum = 0; /* prepare for calc */
4180 calc_check = sctp_calculate_sum(m, &mlen, iphlen);
4181 if (calc_check != check) {
4183 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4184 kprintf("Bad CSUM on SCTP packet calc_check:%x check:%x m:%x mlen:%d iphlen:%d\n",
4185 calc_check, check, (u_int)m, mlen, iphlen);
4189 stcb = sctp_findassociation_addr(m, iphlen,
4190 offset - sizeof(*ch),
4191 sh, ch, &inp, &net);
4192 if ((inp) && (stcb)) {
4193 sctp_send_packet_dropped(stcb, net, m, iphlen,
4195 sctp_chunk_output(inp, stcb, 2);
4196 } else if ((inp != NULL) && (stcb == NULL)) {
4199 sctp_pegs[SCTP_BAD_CSUM]++;
4202 sh->checksum = calc_check;
4205 mlen = m->m_pkthdr.len;
4207 /* validate mbuf chain length with IP payload length */
4208 #if defined(__NetBSD__) || defined(__OpenBSD__)
4209 /* Open BSD gives us the len in network order, fix it */
4212 if (mlen < (ip->ip_len - iphlen)) {
4213 sctp_pegs[SCTP_HDR_DROPS]++;
4218 * Locate pcb and tcb for datagram
4219 * sctp_findassociation_addr() wants IP/SCTP/first chunk header...
4222 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4223 kprintf("V4 find association\n");
4227 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
4228 sh, ch, &inp, &net);
4229 /* inp's ref-count increased && stcb locked */
4231 struct sctp_init_chunk *init_chk, chunk_buf;
4233 sctp_pegs[SCTP_NOPORTS]++;
4236 * we use the bandwidth limiting to protect against
4237 * sending too many ABORTS all at once. In this case
4238 * these count the same as an ICMP message.
4240 if (badport_bandlim(0) < 0)
4242 #endif /* ICMP_BANDLIM */
4244 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4245 kprintf("Sending a ABORT from packet entry!\n");
4248 if (ch->chunk_type == SCTP_INITIATION) {
4249 /* we do a trick here to get the INIT tag,
4250 * dig in and get the tag from the INIT and
4251 * put it in the common header.
4253 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4254 iphlen + sizeof(*sh), sizeof(*init_chk),
4255 (u_int8_t *)&chunk_buf);
4256 if (init_chk != NULL)
4257 sh->v_tag = init_chk->init.initiate_tag;
4259 sctp_send_abort(m, iphlen, sh, 0, NULL);
4261 } else if (stcb == NULL) {
4266 * I very much doubt any of the IPSEC stuff will work but I have
4267 * no idea, so I will leave it in place.
4271 /* FIX ME: this don't work... :) */
4273 /* Find most recent IPsec tag */
4274 i_inp = &inp->ip_inp.inp;
4275 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
4278 tdbi = (struct tdb_ident *)(mtag + 1);
4279 tdb = gettdb(tdbi->spi, &tdbi->dst, tdbi->proto);
4282 ipsp_spd_lookup(m, af, iphlen, &error, IPSP_DIRECTION_IN,
4286 sctp_pegs[SCTP_HDR_DROPS]++;
4291 if (i_inp->inp_tdb_in != tdb) {
4293 tdb_add_inp(tdb, i_inp, 1);
4294 if (i_inp->inp_ipo == NULL) {
4295 i_inp->inp_ipo = ipsec_add_policy(i_inp, af,
4296 IPSP_DIRECTION_OUT);
4297 if (i_inp->inp_ipo == NULL) {
4299 sctp_pegs[SCTP_HDR_DROPS]++;
4303 if (i_inp->inp_ipo->ipo_dstid == NULL &&
4304 tdb->tdb_srcid != NULL) {
4305 i_inp->inp_ipo->ipo_dstid = tdb->tdb_srcid;
4306 tdb->tdb_srcid->ref_count++;
4308 if (i_inp->inp_ipsec_remotecred == NULL &&
4309 tdb->tdb_remote_cred != NULL) {
4310 i_inp->inp_ipsec_remotecred =
4311 tdb->tdb_remote_cred;
4312 tdb->tdb_remote_cred->ref_count++;
4314 if (i_inp->inp_ipsec_remoteauth == NULL &&
4315 tdb->tdb_remote_auth != NULL) {
4316 i_inp->inp_ipsec_remoteauth =
4317 tdb->tdb_remote_auth;
4318 tdb->tdb_remote_auth->ref_count++;
4320 } else { /* Just reset */
4321 TAILQ_REMOVE(&i_inp->inp_tdb_in->tdb_inp_in, i_inp,
4323 i_inp->inp_tdb_in = NULL;
4329 if (ipsec4_in_reject_so(m, inp->ip_inp.inp.inp_socket)) {
4330 ipsecstat.in_polvio++;
4331 sctp_pegs[SCTP_HDR_DROPS]++;
4338 * Construct sockaddr format source address.
4339 * Stuff source address and datagram in user buffer.
4341 if ((inp->ip_inp.inp.inp_flags & INP_CONTROLOPTS)
4343 || (inp->sctp_socket->so_options & SO_TIMESTAMP)
4347 sctp_saveopt(inp, &opts, ip, m);
4349 ip_savecontrol((struct inpcb *)inp, &opts, ip, m);
4354 * common chunk processing
4356 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4357 length = ip->ip_len + iphlen;
4359 length = ip->ip_len - (ip->ip_hl << 2) + iphlen;
4361 offset -= sizeof(struct sctp_chunkhdr);
4363 ecn_bits = ip->ip_tos;
4365 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
4366 inp, stcb, net, ecn_bits);
4367 /* inp's ref-count reduced && stcb unlocked */
4375 if ((inp) && (refcount_up)) {
4376 /* reduce ref-count */
4377 SCTP_INP_WLOCK(inp);
4378 SCTP_INP_DECR_REF(inp);
4379 SCTP_INP_WUNLOCK(inp);
4382 return(IPPROTO_DONE);
4385 SCTP_TCB_UNLOCK(stcb);
4387 if ((inp) && (refcount_up)) {
4388 /* reduce ref-count */
4389 SCTP_INP_WLOCK(inp);
4390 SCTP_INP_DECR_REF(inp);
4391 SCTP_INP_WUNLOCK(inp);
4399 return(IPPROTO_DONE);