Nuke raycontrol(8)
[dragonfly.git] / sys / netinet / sctp_input.c
blob0634aee2c1fd1b5b065f84d5960c7c6fecc8ee8f
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.12 2007/05/13 18:33:58 swildner Exp $ */
4 /*
5 * Copyright (C) 2002, 2003, 2004 Cisco Systems Inc,
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
30 * SUCH DAMAGE.
33 #if !(defined(__OpenBSD__) || defined(__APPLE__))
34 #include "opt_ipsec.h"
35 #endif
36 #if defined(__FreeBSD__) || defined(__DragonFly__)
37 #include "opt_compat.h"
38 #include "opt_inet6.h"
39 #include "opt_inet.h"
40 #endif
41 #if defined(__NetBSD__)
42 #include "opt_inet.h"
43 #endif
44 #ifdef __APPLE__
45 #include <sctp.h>
46 #elif !defined(__OpenBSD__)
47 #include "opt_sctp.h"
48 #endif
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/domain.h>
58 #include <sys/protosw.h>
59 #include <sys/kernel.h>
60 #include <sys/errno.h>
61 #include <sys/syslog.h>
62 #include <sys/thread2.h>
64 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
65 #include <sys/limits.h>
66 #else
67 #include <machine/limits.h>
68 #endif
69 #include <machine/cpu.h>
71 #include <net/if.h>
72 #include <net/route.h>
73 #include <net/if_types.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/in_pcb.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip_var.h>
82 #ifdef INET6
83 #include <netinet/ip6.h>
84 #include <netinet6/ip6_var.h>
85 #endif /* INET6 */
87 #include <netinet/ip_icmp.h>
88 #include <netinet/icmp_var.h>
89 #include <netinet/sctp_var.h>
90 #include <netinet/sctp_pcb.h>
91 #include <netinet/sctp_header.h>
92 #include <netinet/sctputil.h>
93 #include <netinet/sctp_output.h>
94 #include <netinet/sctp_input.h>
95 #include <netinet/sctp_hashdriver.h>
96 #include <netinet/sctp_indata.h>
97 #include <netinet/sctp_asconf.h>
99 #ifdef __APPLE__
100 #include <stdarg.h>
101 #elif !defined(__FreeBSD__)
102 #include <machine/stdarg.h>
103 #endif
105 #ifdef IPSEC
106 #ifndef __OpenBSD__
107 #include <netinet6/ipsec.h>
108 #include <netproto/key/key.h>
109 #else
110 #undef IPSEC
111 #endif
112 #endif /*IPSEC*/
114 #include <net/net_osdep.h>
116 #ifdef SCTP_DEBUG
117 extern u_int32_t sctp_debug_on;
118 #endif
120 /* INIT handler */
121 static void
122 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
123 struct sctphdr *sh, struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
124 struct sctp_tcb *stcb, struct sctp_nets *net)
126 struct sctp_init *init;
127 struct mbuf *op_err;
128 #ifdef SCTP_DEBUG
129 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
130 kprintf("sctp_handle_init: handling INIT tcb:%p\n", stcb);
132 #endif
133 op_err = NULL;
134 init = &cp->init;
135 /* First are we accepting? */
136 if (((inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) == 0) ||
137 (inp->sctp_socket->so_qlimit == 0)) {
138 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
139 return;
141 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
142 /* Invalid length */
143 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
144 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
145 return;
147 /* validate parameters */
148 if (init->initiate_tag == 0) {
149 /* protocol error... send abort */
150 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
151 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
152 return;
154 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
155 /* invalid parameter... send abort */
156 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
157 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
158 return;
160 if (init->num_inbound_streams == 0) {
161 /* protocol error... send abort */
162 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
163 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
164 return;
166 if (init->num_outbound_streams == 0) {
167 /* protocol error... send abort */
168 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
169 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
170 return;
173 /* send an INIT-ACK w/cookie */
174 #ifdef SCTP_DEBUG
175 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
176 kprintf("sctp_handle_init: sending INIT-ACK\n");
178 #endif
180 sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp);
184 * process peer "INIT/INIT-ACK" chunk
185 * returns value < 0 on error
188 static int
189 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb,
190 struct sctp_nets *net)
192 struct sctp_init *init;
193 struct sctp_association *asoc;
194 struct sctp_nets *lnet;
195 unsigned int i;
197 init = &cp->init;
198 asoc = &stcb->asoc;
199 /* save off parameters */
200 asoc->peer_vtag = ntohl(init->initiate_tag);
201 asoc->peers_rwnd = ntohl(init->a_rwnd);
203 if (TAILQ_FIRST(&asoc->nets)) {
204 /* update any ssthresh's that may have a default */
205 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
206 lnet->ssthresh = asoc->peers_rwnd;
209 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
210 unsigned int newcnt;
211 struct sctp_stream_out *outs;
212 struct sctp_tmit_chunk *chk;
214 /* cut back on number of streams */
215 newcnt = ntohs(init->num_inbound_streams);
216 /* This if is probably not needed but I am cautious */
217 if (asoc->strmout) {
218 /* First make sure no data chunks are trapped */
219 for (i=newcnt; i < asoc->pre_open_streams; i++) {
220 outs = &asoc->strmout[i];
221 chk = TAILQ_FIRST(&outs->outqueue);
222 while (chk) {
223 TAILQ_REMOVE(&outs->outqueue, chk,
224 sctp_next);
225 asoc->stream_queue_cnt--;
226 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL,
227 stcb, SCTP_NOTIFY_DATAGRAM_UNSENT,
228 chk);
229 if (chk->data) {
230 sctp_m_freem(chk->data);
231 chk->data = NULL;
233 sctp_free_remote_addr(chk->whoTo);
234 chk->whoTo = NULL;
235 chk->asoc = NULL;
236 /* Free the chunk */
237 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
238 sctppcbinfo.ipi_count_chunk--;
239 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
240 panic("Chunk count is negative");
242 sctppcbinfo.ipi_gencnt_chunk++;
243 chk = TAILQ_FIRST(&outs->outqueue);
247 /* cut back the count and abandon the upper streams */
248 asoc->pre_open_streams = newcnt;
250 asoc->streamincnt = ntohs(init->num_outbound_streams);
251 if (asoc->streamincnt > MAX_SCTP_STREAMS) {
252 asoc->streamincnt = MAX_SCTP_STREAMS;
255 asoc->streamoutcnt = asoc->pre_open_streams;
256 /* init tsn's */
257 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
258 #ifdef SCTP_MAP_LOGGING
259 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
260 #endif
261 /* This is the next one we expect */
262 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
264 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
265 asoc->cumulative_tsn = asoc->asconf_seq_in;
266 asoc->last_echo_tsn = asoc->asconf_seq_in;
267 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
268 /* open the requested streams */
269 if (asoc->strmin != NULL) {
270 /* Free the old ones */
271 FREE(asoc->strmin, M_PCB);
273 MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
274 sizeof(struct sctp_stream_in), M_PCB, M_NOWAIT);
275 if (asoc->strmin == NULL) {
276 /* we didn't get memory for the streams! */
277 #ifdef SCTP_DEBUG
278 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
279 kprintf("process_init: couldn't get memory for the streams!\n");
281 #endif
282 return (-1);
284 for (i = 0; i < asoc->streamincnt; i++) {
285 asoc->strmin[i].stream_no = i;
286 asoc->strmin[i].last_sequence_delivered = 0xffff;
288 * U-stream ranges will be set when the cookie
289 * is unpacked. Or for the INIT sender they
290 * are un set (if pr-sctp not supported) when the
291 * INIT-ACK arrives.
293 TAILQ_INIT(&asoc->strmin[i].inqueue);
295 * we are not on any wheel, pr-sctp streams
296 * will go on the wheel when they have data waiting
297 * for reorder.
299 asoc->strmin[i].next_spoke.tqe_next = 0;
300 asoc->strmin[i].next_spoke.tqe_prev = 0;
304 * load_address_from_init will put the addresses into the
305 * association when the COOKIE is processed or the INIT-ACK
306 * is processed. Both types of COOKIE's existing and new
307 * call this routine. It will remove addresses that
308 * are no longer in the association (for the restarting
309 * case where addresses are removed). Up front when the
310 * INIT arrives we will discard it if it is a restart
311 * and new addresses have been added.
313 return (0);
317 * INIT-ACK message processing/consumption
318 * returns value < 0 on error
320 static int
321 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
322 struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
323 struct sctp_nets *net)
325 struct sctp_association *asoc;
326 struct mbuf *op_err;
327 int retval, abort_flag;
328 uint32_t initack_limit;
329 /* First verify that we have no illegal param's */
330 abort_flag = 0;
331 op_err = NULL;
333 op_err = sctp_arethere_unrecognized_parameters(m,
334 (offset+sizeof(struct sctp_init_chunk)) ,
335 &abort_flag, (struct sctp_chunkhdr *)cp);
336 if (abort_flag) {
337 /* Send an abort and notify peer */
338 if (op_err != NULL) {
339 sctp_send_operr_to(m, iphlen, op_err, cp->init.initiate_tag);
340 } else {
342 * Just notify (abort_assoc does this if
343 * we send an abort).
345 sctp_abort_notification(stcb, 0);
347 * No sense in further INIT's since
348 * we will get the same param back
350 sctp_free_assoc(stcb->sctp_ep, stcb);
352 return (-1);
354 asoc = &stcb->asoc;
355 /* process the peer's parameters in the INIT-ACK */
356 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net);
357 if (retval < 0) {
358 return (retval);
361 initack_limit = offset + ntohs(cp->ch.chunk_length);
362 /* load all addresses */
363 if (sctp_load_addresses_from_init(stcb, m, iphlen,
364 (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh,
365 NULL)) {
366 /* Huh, we should abort */
367 sctp_abort_notification(stcb, 0);
368 sctp_free_assoc(stcb->sctp_ep, stcb);
369 return (-1);
371 if (op_err) {
372 sctp_queue_op_err(stcb, op_err);
373 /* queuing will steal away the mbuf chain to the out queue */
374 op_err = NULL;
376 /* extract the cookie and queue it to "echo" it back... */
377 stcb->asoc.overall_error_count = 0;
378 net->error_count = 0;
379 retval = sctp_send_cookie_echo(m, offset, stcb, net);
380 if (retval < 0) {
382 * No cookie, we probably should send a op error.
383 * But in any case if there is no cookie in the INIT-ACK,
384 * we can abandon the peer, its broke.
386 if (retval == -3) {
387 /* We abort with an error of missing mandatory param */
388 struct mbuf *op_err;
389 op_err =
390 sctp_generate_invmanparam(SCTP_CAUSE_MISS_PARAM);
391 if (op_err) {
393 * Expand beyond to include the mandatory
394 * param cookie
396 struct sctp_inv_mandatory_param *mp;
397 op_err->m_len =
398 sizeof(struct sctp_inv_mandatory_param);
399 mp = mtod(op_err,
400 struct sctp_inv_mandatory_param *);
401 /* Subtract the reserved param */
402 mp->length =
403 htons(sizeof(struct sctp_inv_mandatory_param) - 2);
404 mp->num_param = htonl(1);
405 mp->param = htons(SCTP_STATE_COOKIE);
406 mp->resv = 0;
408 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
409 sh, op_err);
411 return (retval);
415 * Cancel the INIT timer, We do this first before queueing
416 * the cookie. We always cancel at the primary to assue that
417 * we are canceling the timer started by the INIT which always
418 * goes to the primary.
420 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
421 asoc->primary_destination);
423 /* calculate the RTO */
424 net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered);
426 return (0);
429 static void
430 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
431 struct sctp_tcb *stcb, struct sctp_nets *net)
433 struct sockaddr_storage store;
434 struct sockaddr_in *sin;
435 struct sockaddr_in6 *sin6;
436 struct sctp_nets *r_net;
437 struct timeval tv;
439 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
440 /* Invalid length */
441 return;
444 sin = (struct sockaddr_in *)&store;
445 sin6 = (struct sockaddr_in6 *)&store;
447 memset(&store, 0, sizeof(store));
448 if (cp->heartbeat.hb_info.addr_family == AF_INET &&
449 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
450 sin->sin_family = cp->heartbeat.hb_info.addr_family;
451 sin->sin_len = cp->heartbeat.hb_info.addr_len;
452 sin->sin_port = stcb->rport;
453 memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
454 sizeof(sin->sin_addr));
455 } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 &&
456 cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
457 sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
458 sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
459 sin6->sin6_port = stcb->rport;
460 memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
461 sizeof(sin6->sin6_addr));
462 } else {
463 #ifdef SCTP_DEBUG
464 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
465 kprintf("unsupported address family");
467 #endif
468 return;
470 r_net = sctp_findnet(stcb, (struct sockaddr *)sin);
471 if (r_net == NULL) {
472 #ifdef SCTP_DEBUG
473 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
474 kprintf("Huh? I can't find the address I sent it to, discard\n");
476 #endif
477 return;
479 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
480 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
481 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
483 * If the its a HB and it's random value is correct when
484 * can confirm the destination.
486 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
487 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
488 stcb, 0, (void *)r_net);
490 r_net->error_count = 0;
491 r_net->hb_responded = 1;
492 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
493 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
494 if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
495 r_net->dest_state = SCTP_ADDR_REACHABLE;
496 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
497 SCTP_HEARTBEAT_SUCCESS, (void *)r_net);
499 /* now was it the primary? if so restore */
500 if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
501 sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net);
504 /* Now lets do a RTO with this */
505 r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv);
508 static void
509 sctp_handle_abort(struct sctp_abort_chunk *cp,
510 struct sctp_tcb *stcb, struct sctp_nets *net)
512 struct sctp_inpcb *inp;
514 #ifdef SCTP_DEBUG
515 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
516 kprintf("sctp_handle_abort: handling ABORT\n");
518 #endif
519 if (stcb == NULL)
520 return;
521 /* verify that the destination addr is in the association */
522 /* ignore abort for addresses being deleted */
524 /* stop any receive timers */
525 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net);
526 /* notify user of the abort and clean up... */
527 sctp_abort_notification(stcb, 0);
528 /* free the tcb */
529 inp = stcb->sctp_ep;
530 sctp_free_assoc(stcb->sctp_ep, stcb);
531 #ifdef SCTP_DEBUG
532 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
533 kprintf("sctp_handle_abort: finished\n");
535 #endif
538 static void
539 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
540 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
542 struct sctp_association *asoc;
543 int some_on_streamwheel;
545 #ifdef SCTP_DEBUG
546 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
547 kprintf("sctp_handle_shutdown: handling SHUTDOWN\n");
549 #endif
550 if (stcb == NULL)
551 return;
553 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
554 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
555 return;
558 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
559 /* update current data status */
560 #ifdef SCTP_DEBUG
561 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
562 kprintf("Warning Shutdown NOT the expected size.. skipping (%d:%d)\n",
563 ntohs(cp->ch.chunk_length),
564 (int)sizeof(struct sctp_shutdown_chunk));
566 #endif
567 return;
568 } else {
569 sctp_update_acked(stcb, cp, net, abort_flag);
571 asoc = &stcb->asoc;
572 /* goto SHUTDOWN_RECEIVED state to block new requests */
573 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
574 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
575 asoc->state = SCTP_STATE_SHUTDOWN_RECEIVED;
576 #ifdef SCTP_DEBUG
577 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
578 kprintf("Moving to SHUTDOWN-RECEIVED state\n");
580 #endif
581 /* notify upper layer that peer has initiated a shutdown */
582 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL);
584 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
585 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
587 /* Set the flag so we cannot send more, we
588 * would call the function but we don't want to
589 * wake up the ulp necessarily.
591 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
592 stcb->sctp_ep->sctp_socket->so_rcv.sb_state |= SBS_CANTSENDMORE;
593 #else
594 stcb->sctp_ep->sctp_socket->so_state |= SS_CANTSENDMORE;
595 #endif
597 /* reset time */
598 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
600 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
602 * stop the shutdown timer, since we WILL move
603 * to SHUTDOWN-ACK-SENT.
605 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
607 /* Now are we there yet? */
608 some_on_streamwheel = 0;
609 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
610 /* Check to see if some data queued */
611 struct sctp_stream_out *outs;
612 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
613 if (!TAILQ_EMPTY(&outs->outqueue)) {
614 some_on_streamwheel = 1;
615 break;
619 #ifdef SCTP_DEBUG
620 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
621 kprintf("some_on_streamwheel:%d send_q_empty:%d sent_q_empty:%d\n",
622 some_on_streamwheel,
623 !TAILQ_EMPTY(&asoc->send_queue),
624 !TAILQ_EMPTY(&asoc->sent_queue));
626 #endif
627 if (!TAILQ_EMPTY(&asoc->send_queue) ||
628 !TAILQ_EMPTY(&asoc->sent_queue) ||
629 some_on_streamwheel) {
630 /* By returning we will push more data out */
631 return;
632 } else {
633 /* no outstanding data to send, so move on... */
634 /* send SHUTDOWN-ACK */
635 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
636 /* move to SHUTDOWN-ACK-SENT state */
637 asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
638 #ifdef SCTP_DEBUG
639 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
640 kprintf("moving to SHUTDOWN_ACK state\n");
642 #endif
643 /* start SHUTDOWN timer */
644 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
645 stcb, net);
649 static void
650 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp,
651 struct sctp_tcb *stcb, struct sctp_nets *net)
653 struct sctp_association *asoc;
655 #ifdef SCTP_DEBUG
656 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
657 kprintf("sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
659 #endif
660 if (stcb == NULL)
661 return;
663 asoc = &stcb->asoc;
664 /* process according to association state */
665 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
666 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
667 /* unexpected SHUTDOWN-ACK... so ignore... */
668 return;
670 /* are the queues empty? */
671 if (!TAILQ_EMPTY(&asoc->send_queue) ||
672 !TAILQ_EMPTY(&asoc->sent_queue) ||
673 !TAILQ_EMPTY(&asoc->out_wheel)) {
674 sctp_report_all_outbound(stcb);
676 /* stop the timer */
677 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
678 /* send SHUTDOWN-COMPLETE */
679 sctp_send_shutdown_complete(stcb, net);
680 /* notify upper layer protocol */
681 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL);
682 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
683 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
684 stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
685 /* Set the connected flag to disconnected */
686 stcb->sctp_ep->sctp_socket->so_snd.ssb_cc = 0;
687 stcb->sctp_ep->sctp_socket->so_snd.ssb_mbcnt = 0;
688 soisdisconnected(stcb->sctp_ep->sctp_socket);
690 /* free the TCB but first save off the ep */
691 sctp_free_assoc(stcb->sctp_ep, stcb);
695 * Skip past the param header and then we will find the chunk that
696 * caused the problem. There are two possiblities ASCONF or FWD-TSN
697 * other than that and our peer must be broken.
699 static void
700 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
701 struct sctp_nets *net)
703 struct sctp_chunkhdr *chk;
705 chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
706 switch (chk->chunk_type) {
707 case SCTP_ASCONF_ACK:
708 #ifdef SCTP_DEBUG
709 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
710 kprintf("Strange peer, snds ASCONF but does not recognize asconf-ack?\n");
712 #endif
713 case SCTP_ASCONF:
714 #ifdef SCTP_DEBUG
715 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
716 kprintf("Peer does not support ASCONF/ASCONF-ACK chunks\n");
718 #endif /* SCTP_DEBUG */
719 sctp_asconf_cleanup(stcb, net);
720 break;
721 case SCTP_FORWARD_CUM_TSN:
722 stcb->asoc.peer_supports_prsctp = 0;
723 break;
724 default:
725 #ifdef SCTP_DEBUG
726 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
727 kprintf("Peer does not support chunk type %d(%x)??\n",
728 chk->chunk_type, (u_int)chk->chunk_type);
730 #endif
731 break;
736 * Skip past the param header and then we will find the param that
737 * caused the problem. There are a number of param's in a ASCONF
738 * OR the prsctp param these will turn of specific features.
740 static void
741 sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
743 struct sctp_paramhdr *pbad;
745 pbad = phdr + 1;
746 switch (ntohs(pbad->param_type)) {
747 /* pr-sctp draft */
748 case SCTP_PRSCTP_SUPPORTED:
749 stcb->asoc.peer_supports_prsctp = 0;
750 break;
751 case SCTP_SUPPORTED_CHUNK_EXT:
752 break;
753 /* draft-ietf-tsvwg-addip-sctp */
754 case SCTP_ECN_NONCE_SUPPORTED:
755 stcb->asoc.peer_supports_ecn_nonce = 0;
756 stcb->asoc.ecn_nonce_allowed = 0;
757 stcb->asoc.ecn_allowed = 0;
758 break;
759 case SCTP_ADD_IP_ADDRESS:
760 case SCTP_DEL_IP_ADDRESS:
761 stcb->asoc.peer_supports_asconf = 0;
762 break;
763 case SCTP_SET_PRIM_ADDR:
764 stcb->asoc.peer_supports_asconf_setprim = 0;
765 break;
766 case SCTP_SUCCESS_REPORT:
767 case SCTP_ERROR_CAUSE_IND:
768 #ifdef SCTP_DEBUG
769 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
770 kprintf("Huh, the peer does not support success? or error cause?\n");
771 kprintf("Turning off ASCONF to this strange peer\n");
773 #endif
774 stcb->asoc.peer_supports_asconf = 0;
775 stcb->asoc.peer_supports_asconf_setprim = 0;
776 break;
777 default:
778 #ifdef SCTP_DEBUG
779 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
780 kprintf("Peer does not support base param type %d(%x)??\n",
781 pbad->param_type, (u_int)pbad->param_type);
783 #endif
784 break;
788 static int
789 sctp_handle_error(struct sctp_chunkhdr *ch,
790 struct sctp_tcb *stcb, struct sctp_nets *net)
792 int chklen;
793 struct sctp_paramhdr *phdr;
794 uint16_t error_type;
795 uint16_t error_len;
796 struct sctp_association *asoc;
798 int adjust;
799 /* parse through all of the errors and process */
800 asoc = &stcb->asoc;
801 phdr = (struct sctp_paramhdr *)((caddr_t)ch +
802 sizeof(struct sctp_chunkhdr));
803 chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
804 while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
805 /* Process an Error Cause */
806 error_type = ntohs(phdr->param_type);
807 error_len = ntohs(phdr->param_length);
808 if ((error_len > chklen) || (error_len == 0)) {
809 /* invalid param length for this param */
810 #ifdef SCTP_DEBUG
811 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
812 kprintf("Bogus length in error param- chunk left:%d errorlen:%d\n",
813 chklen, error_len);
815 #endif /* SCTP_DEBUG */
816 return (0);
818 switch (error_type) {
819 case SCTP_CAUSE_INV_STRM:
820 case SCTP_CAUSE_MISS_PARAM:
821 case SCTP_CAUSE_INVALID_PARAM:
822 case SCTP_CAUSE_NOUSER_DATA:
823 #ifdef SCTP_DEBUG
824 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
825 kprintf("Software error we got a %d back? We have a bug :/ (or do they?)\n",
826 error_type);
828 #endif
829 break;
830 case SCTP_CAUSE_STALE_COOKIE:
831 /* We only act if we have echoed a cookie and are waiting. */
832 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
833 int *p;
834 p = (int *)((caddr_t)phdr + sizeof(*phdr));
835 /* Save the time doubled */
836 asoc->cookie_preserve_req = ntohl(*p) << 1;
837 asoc->stale_cookie_count++;
838 if (asoc->stale_cookie_count >
839 asoc->max_init_times) {
840 sctp_abort_notification(stcb, 0);
841 /* now free the asoc */
842 sctp_free_assoc(stcb->sctp_ep, stcb);
843 return (-1);
845 /* blast back to INIT state */
846 asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
847 asoc->state |= SCTP_STATE_COOKIE_WAIT;
848 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
849 stcb->sctp_ep, stcb, net);
850 sctp_send_initiate(stcb->sctp_ep, stcb);
852 break;
853 case SCTP_CAUSE_UNRESOLV_ADDR:
855 * Nothing we can do here, we don't do hostname
856 * addresses so if the peer does not like my IPv6 (or
857 * IPv4 for that matter) it does not matter. If they
858 * don't support that type of address, they can NOT
859 * possibly get that packet type... i.e. with no IPv6
860 * you can't recieve a IPv6 packet. so we can safely
861 * ignore this one. If we ever added support for
862 * HOSTNAME Addresses, then we would need to do
863 * something here.
865 break;
866 case SCTP_CAUSE_UNRECOG_CHUNK:
867 sctp_process_unrecog_chunk(stcb, phdr, net);
868 break;
869 case SCTP_CAUSE_UNRECOG_PARAM:
870 sctp_process_unrecog_param(stcb, phdr);
871 break;
872 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
874 * We ignore this since the timer will drive out a new
875 * cookie anyway and there timer will drive us to send
876 * a SHUTDOWN_COMPLETE. We can't send one here since
877 * we don't have their tag.
879 break;
880 case SCTP_CAUSE_DELETEING_LAST_ADDR:
881 case SCTP_CAUSE_OPERATION_REFUSED:
882 case SCTP_CAUSE_DELETING_SRC_ADDR:
883 /* We should NOT get these here, but in a ASCONF-ACK. */
884 #ifdef SCTP_DEBUG
885 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
886 kprintf("Peer sends ASCONF errors in a Operational Error?<%d>?\n",
887 error_type);
889 #endif
890 break;
891 case SCTP_CAUSE_OUT_OF_RESC:
893 * And what, pray tell do we do with the fact
894 * that the peer is out of resources? Not
895 * really sure we could do anything but abort.
896 * I suspect this should have came WITH an
897 * abort instead of in a OP-ERROR.
899 break;
900 default:
901 #ifdef SCTP_DEBUG
902 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
903 /* don't know what this error cause is... */
904 kprintf("sctp_handle_error: unknown error type = 0x%xh\n",
905 error_type);
907 #endif /* SCTP_DEBUG */
908 break;
910 adjust = SCTP_SIZE32(error_len);
911 chklen -= adjust;
912 phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
914 return (0);
917 static int
918 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
919 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
920 struct sctp_nets *net)
922 struct sctp_init_ack *init_ack;
923 int *state;
924 struct mbuf *op_err;
926 #ifdef SCTP_DEBUG
927 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
928 kprintf("sctp_handle_init_ack: handling INIT-ACK\n");
930 #endif
931 if (stcb == NULL) {
932 #ifdef SCTP_DEBUG
933 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
934 kprintf("sctp_handle_init_ack: TCB is null\n");
936 #endif
937 return (-1);
939 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
940 /* Invalid length */
941 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
942 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
943 op_err);
944 return (-1);
946 init_ack = &cp->init;
947 /* validate parameters */
948 if (init_ack->initiate_tag == 0) {
949 /* protocol error... send an abort */
950 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
951 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
952 op_err);
953 return (-1);
955 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
956 /* protocol error... send an abort */
957 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
958 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
959 op_err);
960 return (-1);
962 if (init_ack->num_inbound_streams == 0) {
963 /* protocol error... send an abort */
964 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
965 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
966 op_err);
967 return (-1);
969 if (init_ack->num_outbound_streams == 0) {
970 /* protocol error... send an abort */
971 op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
972 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
973 op_err);
974 return (-1);
977 /* process according to association state... */
978 state = &stcb->asoc.state;
979 switch (*state & SCTP_STATE_MASK) {
980 case SCTP_STATE_COOKIE_WAIT:
981 /* this is the expected state for this chunk */
982 /* process the INIT-ACK parameters */
983 if (stcb->asoc.primary_destination->dest_state &
984 SCTP_ADDR_UNCONFIRMED) {
986 * The primary is where we sent the INIT, we can
987 * always consider it confirmed when the INIT-ACK
988 * is returned. Do this before we load addresses
989 * though.
991 stcb->asoc.primary_destination->dest_state &=
992 ~SCTP_ADDR_UNCONFIRMED;
993 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
994 stcb, 0, (void *)stcb->asoc.primary_destination);
996 if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, net
997 ) < 0) {
998 /* error in parsing parameters */
999 #ifdef SCTP_DEBUG
1000 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1001 kprintf("sctp_process_init_ack: error in msg, discarding\n");
1003 #endif
1004 return (-1);
1006 /* update our state */
1007 #ifdef SCTP_DEBUG
1008 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1009 kprintf("moving to COOKIE-ECHOED state\n");
1011 #endif
1012 if (*state & SCTP_STATE_SHUTDOWN_PENDING) {
1013 *state = SCTP_STATE_COOKIE_ECHOED |
1014 SCTP_STATE_SHUTDOWN_PENDING;
1015 } else {
1016 *state = SCTP_STATE_COOKIE_ECHOED;
1019 /* reset the RTO calc */
1020 stcb->asoc.overall_error_count = 0;
1021 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1023 * collapse the init timer back in case of a exponential backoff
1025 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1026 stcb, net);
1028 * the send at the end of the inbound data processing will
1029 * cause the cookie to be sent
1031 break;
1032 case SCTP_STATE_SHUTDOWN_SENT:
1033 /* incorrect state... discard */
1034 break;
1035 case SCTP_STATE_COOKIE_ECHOED:
1036 /* incorrect state... discard */
1037 break;
1038 case SCTP_STATE_OPEN:
1039 /* incorrect state... discard */
1040 break;
1041 case SCTP_STATE_EMPTY:
1042 case SCTP_STATE_INUSE:
1043 default:
1044 /* incorrect state... discard */
1045 #ifdef SCTP_DEBUG
1046 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1047 kprintf("Leaving handle-init-ack default\n");
1049 #endif
1050 return (-1);
1051 break;
1052 } /* end switch asoc state */
1053 #ifdef SCTP_DEBUG
1054 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1055 kprintf("Leaving handle-init-ack end\n");
1057 #endif
1058 return (0);
1063 * handle a state cookie for an existing association
1064 * m: input packet mbuf chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk
1065 * note: this is a "split" mbuf and the cookie signature does not exist
1066 * offset: offset into mbuf to the cookie-echo chunk
1068 static struct sctp_tcb *
1069 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1070 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1071 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
1072 struct sockaddr *init_src, int *notification)
1074 struct sctp_association *asoc;
1075 struct sctp_init_chunk *init_cp, init_buf;
1076 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1077 int chk_length;
1078 int init_offset, initack_offset;
1079 int retval;
1081 /* I know that the TCB is non-NULL from the caller */
1082 asoc = &stcb->asoc;
1084 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1085 /* SHUTDOWN came in after sending INIT-ACK */
1086 struct mbuf *op_err;
1087 struct sctp_paramhdr *ph;
1089 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1090 #ifdef SCTP_DEBUG
1091 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1092 kprintf("sctp_handle_cookie: got a cookie, while shutting down!\n");
1094 #endif
1095 MGETHDR(op_err, MB_DONTWAIT, MT_HEADER);
1096 if (op_err == NULL) {
1097 /* FOOBAR */
1098 return (NULL);
1100 /* pre-reserve some space */
1101 op_err->m_data += sizeof(struct ip6_hdr);
1102 op_err->m_data += sizeof(struct sctphdr);
1103 op_err->m_data += sizeof(struct sctp_chunkhdr);
1104 /* Set the len */
1105 op_err->m_len = op_err->m_pkthdr.len = sizeof(struct sctp_paramhdr);
1106 ph = mtod(op_err, struct sctp_paramhdr *);
1107 ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1108 ph->param_length = htons(sizeof(struct sctp_paramhdr));
1109 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag);
1110 return (NULL);
1113 * find and validate the INIT chunk in the cookie (peer's info)
1114 * the INIT should start after the cookie-echo header struct
1115 * (chunk header, state cookie header struct)
1117 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1119 init_cp = (struct sctp_init_chunk *)
1120 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1121 (u_int8_t *)&init_buf);
1122 if (init_cp == NULL) {
1123 /* could not pull a INIT chunk in cookie */
1124 #ifdef SCTP_DEBUG
1125 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1126 kprintf("process_cookie_existing: could not pull INIT chunk hdr\n");
1128 #endif /* SCTP_DEBUG */
1129 return (NULL);
1131 chk_length = ntohs(init_cp->ch.chunk_length);
1132 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1133 #ifdef SCTP_DEBUG
1134 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1135 kprintf("process_cookie_existing: could not find INIT chunk!\n");
1137 #endif /* SCTP_DEBUG */
1138 return (NULL);
1142 * find and validate the INIT-ACK chunk in the cookie (my info)
1143 * the INIT-ACK follows the INIT chunk
1145 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1146 initack_cp = (struct sctp_init_ack_chunk *)
1147 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1148 (u_int8_t *)&initack_buf);
1149 if (initack_cp == NULL) {
1150 /* could not pull INIT-ACK chunk in cookie */
1151 #ifdef SCTP_DEBUG
1152 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1153 kprintf("process_cookie_existing: could not pull INIT-ACK chunk hdr\n");
1155 #endif /* SCTP_DEBUG */
1156 return (NULL);
1158 chk_length = ntohs(initack_cp->ch.chunk_length);
1159 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1160 #ifdef SCTP_DEBUG
1161 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1162 kprintf("process_cookie_existing: could not find INIT-ACK chunk!\n");
1164 #endif /* SCTP_DEBUG */
1165 return (NULL);
1167 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1168 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1170 * case D in Section 5.2.4 Table 2: MMAA
1171 * process accordingly to get into the OPEN state
1173 switch SCTP_GET_STATE(asoc) {
1174 case SCTP_STATE_COOKIE_WAIT:
1176 * INIT was sent, but got got a COOKIE_ECHO with
1177 * the correct tags... just accept it...
1179 /* First we must process the INIT !! */
1180 retval = sctp_process_init(init_cp, stcb, net);
1181 if (retval < 0) {
1182 #ifdef SCTP_DEBUG
1183 kprintf("process_cookie_existing: INIT processing failed\n");
1184 #endif
1185 return (NULL);
1187 /* intentional fall through to below... */
1189 case SCTP_STATE_COOKIE_ECHOED:
1190 /* Duplicate INIT case */
1191 /* we have already processed the INIT so no problem */
1192 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1193 net);
1194 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
1195 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb,
1196 net);
1197 /* update current state */
1198 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1199 asoc->state = SCTP_STATE_OPEN |
1200 SCTP_STATE_SHUTDOWN_PENDING;
1201 } else if ((asoc->state & SCTP_STATE_SHUTDOWN_SENT) == 0) {
1202 /* if ok, move to OPEN state */
1203 asoc->state = SCTP_STATE_OPEN;
1205 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1206 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1207 (!(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING))) {
1209 * Here is where collision would go if we did a
1210 * connect() and instead got a
1211 * init/init-ack/cookie done before the
1212 * init-ack came back..
1214 stcb->sctp_ep->sctp_flags |=
1215 SCTP_PCB_FLAGS_CONNECTED;
1216 soisconnected(stcb->sctp_ep->sctp_socket);
1218 /* notify upper layer */
1219 *notification = SCTP_NOTIFY_ASSOC_UP;
1220 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1221 net);
1223 * since we did not send a HB make sure we don't double
1224 * things
1226 net->hb_responded = 1;
1228 if (stcb->asoc.sctp_autoclose_ticks &&
1229 (inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
1230 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1231 inp, stcb, NULL);
1233 break;
1234 default:
1236 * we're in the OPEN state (or beyond), so peer
1237 * must have simply lost the COOKIE-ACK
1239 break;
1240 } /* end switch */
1243 * We ignore the return code here.. not sure if we should
1244 * somehow abort.. but we do have an existing asoc. This
1245 * really should not fail.
1247 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1248 init_offset + sizeof(struct sctp_init_chunk),
1249 initack_offset, sh, init_src)) {
1250 #ifdef SCTP_DEBUG
1251 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1252 kprintf("Weird cookie load_address failure on cookie existing - 1\n");
1254 #endif
1255 return (NULL);
1258 /* respond with a COOKIE-ACK */
1259 sctp_send_cookie_ack(stcb);
1260 return (stcb);
1261 } /* end if */
1262 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1263 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1264 cookie->tie_tag_my_vtag == 0 &&
1265 cookie->tie_tag_peer_vtag == 0) {
1267 * case C in Section 5.2.4 Table 2: XMOO
1268 * silently discard
1270 return (NULL);
1272 if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag &&
1273 (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag ||
1274 init_cp->init.initiate_tag == 0)) {
1276 * case B in Section 5.2.4 Table 2: MXAA or MOAA
1277 * my info should be ok, re-accept peer info
1279 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1280 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
1281 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
1282 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1284 * since we did not send a HB make sure we don't double things
1286 net->hb_responded = 1;
1287 if (stcb->asoc.sctp_autoclose_ticks &&
1288 (inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
1289 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1290 NULL);
1292 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1293 asoc->pre_open_streams =
1294 ntohs(initack_cp->init.num_outbound_streams);
1295 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1296 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out =
1297 asoc->init_seq_number;
1298 asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
1299 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1300 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1301 asoc->str_reset_seq_in = asoc->init_seq_number;
1302 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1304 /* process the INIT info (peer's info) */
1305 retval = sctp_process_init(init_cp, stcb, net);
1306 if (retval < 0) {
1307 #ifdef SCTP_DEBUG
1308 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1309 kprintf("process_cookie_existing: INIT processing failed\n");
1311 #endif
1312 return (NULL);
1314 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1315 init_offset + sizeof(struct sctp_init_chunk),
1316 initack_offset, sh, init_src)) {
1317 #ifdef SCTP_DEBUG
1318 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1319 kprintf("Weird cookie load_address failure on cookie existing - 2\n");
1321 #endif
1322 return (NULL);
1325 if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1326 (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1327 *notification = SCTP_NOTIFY_ASSOC_UP;
1329 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1330 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1331 !(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
1332 stcb->sctp_ep->sctp_flags |=
1333 SCTP_PCB_FLAGS_CONNECTED;
1334 soisconnected(stcb->sctp_ep->sctp_socket);
1337 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1338 asoc->state = SCTP_STATE_OPEN |
1339 SCTP_STATE_SHUTDOWN_PENDING;
1340 } else {
1341 asoc->state = SCTP_STATE_OPEN;
1343 sctp_send_cookie_ack(stcb);
1344 return (stcb);
1347 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1348 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1349 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1350 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1351 cookie->tie_tag_peer_vtag != 0) {
1353 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1355 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
1356 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
1357 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1359 /* notify upper layer */
1360 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1362 /* send up all the data */
1363 sctp_report_all_outbound(stcb);
1365 /* process the INIT-ACK info (my info) */
1366 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1367 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1368 asoc->pre_open_streams =
1369 ntohs(initack_cp->init.num_outbound_streams);
1370 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1371 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out =
1372 asoc->init_seq_number;
1373 asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
1374 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1375 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1376 asoc->str_reset_seq_in = asoc->init_seq_number;
1378 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1379 if (asoc->mapping_array)
1380 memset(asoc->mapping_array, 0,
1381 asoc->mapping_array_size);
1382 /* process the INIT info (peer's info) */
1383 retval = sctp_process_init(init_cp, stcb, net);
1384 if (retval < 0) {
1385 #ifdef SCTP_DEBUG
1386 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1387 kprintf("process_cookie_existing: INIT processing failed\n");
1389 #endif
1390 return (NULL);
1393 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1395 * since we did not send a HB make sure we don't double things
1397 net->hb_responded = 1;
1399 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1400 init_offset + sizeof(struct sctp_init_chunk),
1401 initack_offset, sh, init_src)) {
1402 #ifdef SCTP_DEBUG
1403 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1404 kprintf("Weird cookie load_address failure on cookie existing - 3\n");
1406 #endif
1407 return (NULL);
1410 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1411 asoc->state = SCTP_STATE_OPEN |
1412 SCTP_STATE_SHUTDOWN_PENDING;
1413 } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1414 /* move to OPEN state, if not in SHUTDOWN_SENT */
1415 asoc->state = SCTP_STATE_OPEN;
1417 /* respond with a COOKIE-ACK */
1418 sctp_send_cookie_ack(stcb);
1420 return (stcb);
1422 /* all other cases... */
1423 return (NULL);
1427 * handle a state cookie for a new association
1428 * m: input packet mbuf chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk
1429 * note: this is a "split" mbuf and the cookie signature does not exist
1430 * offset: offset into mbuf to the cookie-echo chunk
1431 * length: length of the cookie chunk
1432 * to: where the init was from
1433 * returns a new TCB
1435 static struct sctp_tcb *
1436 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1437 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1438 struct sctp_inpcb *inp, struct sctp_nets **netp,
1439 struct sockaddr *init_src, int *notification)
1441 struct sctp_tcb *stcb;
1442 struct sctp_init_chunk *init_cp, init_buf;
1443 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1444 struct sockaddr_storage sa_store;
1445 struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
1446 struct sockaddr_in *sin;
1447 struct sockaddr_in6 *sin6;
1448 struct sctp_association *asoc;
1449 int chk_length;
1450 int init_offset, initack_offset, initack_limit;
1451 int retval;
1452 int error = 0;
1453 u_int32_t old_tag;
1455 * find and validate the INIT chunk in the cookie (peer's info)
1456 * the INIT should start after the cookie-echo header struct
1457 * (chunk header, state cookie header struct)
1459 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
1460 init_cp = (struct sctp_init_chunk *)
1461 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1462 (u_int8_t *)&init_buf);
1463 if (init_cp == NULL) {
1464 /* could not pull a INIT chunk in cookie */
1465 #ifdef SCTP_DEBUG
1466 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1467 kprintf("process_cookie_new: could not pull INIT chunk hdr\n");
1469 #endif /* SCTP_DEBUG */
1470 return (NULL);
1472 chk_length = ntohs(init_cp->ch.chunk_length);
1473 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1474 #ifdef SCTP_DEBUG
1475 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1476 kprintf("HUH? process_cookie_new: could not find INIT chunk!\n");
1478 #endif /* SCTP_DEBUG */
1479 return (NULL);
1482 initack_offset = init_offset + SCTP_SIZE32(chk_length);
1484 * find and validate the INIT-ACK chunk in the cookie (my info)
1485 * the INIT-ACK follows the INIT chunk
1487 initack_cp = (struct sctp_init_ack_chunk *)
1488 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1489 (u_int8_t *)&initack_buf);
1490 if (initack_cp == NULL) {
1491 /* could not pull INIT-ACK chunk in cookie */
1492 #ifdef SCTP_DEBUG
1493 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1494 kprintf("process_cookie_new: could not pull INIT-ACK chunk hdr\n");
1496 #endif /* SCTP_DEBUG */
1497 return (NULL);
1499 chk_length = ntohs(initack_cp->ch.chunk_length);
1500 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1501 #ifdef SCTP_DEBUG
1502 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1503 u_int8_t *pp;
1504 pp = (u_int8_t *)initack_cp;
1505 kprintf("process_cookie_new: could not find INIT-ACK chunk!\n");
1506 kprintf("Found bytes %x %x %x %x at postion %d\n",
1507 (u_int)pp[0], (u_int)pp[1], (u_int)pp[2],
1508 (u_int)pp[3], initack_offset);
1510 #endif /* SCTP_DEBUG */
1511 return (NULL);
1513 initack_limit = initack_offset + SCTP_SIZE32(chk_length);
1516 * now that we know the INIT/INIT-ACK are in place,
1517 * create a new TCB and popluate
1519 stcb = sctp_aloc_assoc(inp, init_src, 0, &error, ntohl(initack_cp->init.initiate_tag));
1520 if (stcb == NULL) {
1521 struct mbuf *op_err;
1522 /* memory problem? */
1523 #ifdef SCTP_DEBUG
1524 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1525 kprintf("process_cookie_new: no room for another TCB!\n");
1527 #endif /* SCTP_DEBUG */
1528 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1529 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
1530 sh, op_err);
1531 return (NULL);
1534 /* get the correct sctp_nets */
1535 *netp = sctp_findnet(stcb, init_src);
1536 asoc = &stcb->asoc;
1537 /* get scope variables out of cookie */
1538 asoc->ipv4_local_scope = cookie->ipv4_scope;
1539 asoc->site_scope = cookie->site_scope;
1540 asoc->local_scope = cookie->local_scope;
1541 asoc->loopback_scope = cookie->loopback_scope;
1543 if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
1544 (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
1545 struct mbuf *op_err;
1547 * Houston we have a problem. The EP changed while the cookie
1548 * was in flight. Only recourse is to abort the association.
1550 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1551 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
1552 sh, op_err);
1553 return (NULL);
1556 /* process the INIT-ACK info (my info) */
1557 old_tag = asoc->my_vtag;
1558 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1559 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1560 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1561 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1562 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1563 asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
1564 asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1565 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1566 asoc->str_reset_seq_in = asoc->init_seq_number;
1568 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1570 /* process the INIT info (peer's info) */
1571 retval = sctp_process_init(init_cp, stcb, *netp);
1572 if (retval < 0) {
1573 #ifdef SCTP_DEBUG
1574 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1575 kprintf("process_cookie_new: INIT processing failed\n");
1577 #endif
1578 sctp_free_assoc(inp, stcb);
1579 return (NULL);
1581 /* load all addresses */
1582 if (sctp_load_addresses_from_init(stcb, m, iphlen,
1583 init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
1584 init_src)) {
1585 sctp_free_assoc(inp, stcb);
1586 return (NULL);
1589 /* update current state */
1590 #ifdef SCTP_DEBUG
1591 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1592 kprintf("moving to OPEN state\n");
1594 #endif
1595 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1596 asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
1597 } else {
1598 asoc->state = SCTP_STATE_OPEN;
1600 /* calculate the RTT */
1601 (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
1602 &cookie->time_entered);
1605 * if we're doing ASCONFs, check to see if we have any new
1606 * local addresses that need to get added to the peer (eg.
1607 * addresses changed while cookie echo in flight). This needs
1608 * to be done after we go to the OPEN state to do the correct
1609 * asconf processing.
1610 * else, make sure we have the correct addresses in our lists
1613 /* warning, we re-use sin, sin6, sa_store here! */
1614 /* pull in local_address (our "from" address) */
1615 if (cookie->laddr_type == SCTP_IPV4_ADDRESS) {
1616 /* source addr is IPv4 */
1617 sin = (struct sockaddr_in *)initack_src;
1618 memset(sin, 0, sizeof(*sin));
1619 sin->sin_family = AF_INET;
1620 sin->sin_len = sizeof(struct sockaddr_in);
1621 sin->sin_addr.s_addr = cookie->laddress[0];
1622 } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) {
1623 /* source addr is IPv6 */
1624 sin6 = (struct sockaddr_in6 *)initack_src;
1625 memset(sin6, 0, sizeof(*sin6));
1626 sin6->sin6_family = AF_INET6;
1627 sin6->sin6_len = sizeof(struct sockaddr_in6);
1628 sin6->sin6_scope_id = cookie->scope_id;
1629 memcpy(&sin6->sin6_addr, cookie->laddress,
1630 sizeof(sin6->sin6_addr));
1631 } else {
1632 sctp_free_assoc(inp, stcb);
1633 return (NULL);
1636 sctp_check_address_list(stcb, m, initack_offset +
1637 sizeof(struct sctp_init_ack_chunk), initack_limit,
1638 initack_src, cookie->local_scope, cookie->site_scope,
1639 cookie->ipv4_scope, cookie->loopback_scope);
1642 /* set up to notify upper layer */
1643 *notification = SCTP_NOTIFY_ASSOC_UP;
1644 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1645 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1646 !(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
1648 * This is an endpoint that called connect()
1649 * how it got a cookie that is NEW is a bit of
1650 * a mystery. It must be that the INIT was sent, but
1651 * before it got there.. a complete INIT/INIT-ACK/COOKIE
1652 * arrived. But of course then it should have went to
1653 * the other code.. not here.. oh well.. a bit of protection
1654 * is worth having..
1656 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1657 soisconnected(stcb->sctp_ep->sctp_socket);
1658 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, *netp);
1659 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1660 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
1662 * We don't want to do anything with this
1663 * one. Since it is the listening guy. The timer will
1664 * get started for accepted connections in the caller.
1667 } else {
1668 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, *netp);
1670 /* since we did not send a HB make sure we don't double things */
1671 (*netp)->hb_responded = 1;
1673 if (stcb->asoc.sctp_autoclose_ticks &&
1674 (inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
1675 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
1678 /* respond with a COOKIE-ACK */
1679 sctp_send_cookie_ack(stcb);
1681 return (stcb);
1686 * handles a COOKIE-ECHO message
1687 * stcb: modified to either a new or left as existing (non-NULL) TCB
1689 static struct mbuf *
1690 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
1691 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
1692 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp)
1694 struct sctp_state_cookie *cookie;
1695 struct sockaddr_in6 sin6;
1696 struct sockaddr_in sin;
1697 struct sctp_tcb *l_stcb=*stcb;
1698 struct sctp_inpcb *l_inp;
1699 struct sockaddr *to;
1700 struct sctp_pcb *ep;
1701 struct mbuf *m_sig;
1702 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
1703 uint8_t *sig;
1704 uint8_t cookie_ok = 0;
1705 unsigned int size_of_pkt, sig_offset, cookie_offset;
1706 unsigned int cookie_len;
1707 struct timeval now;
1708 struct timeval time_expires;
1709 struct sockaddr_storage dest_store;
1710 struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
1711 struct ip *iph;
1712 int notification = 0;
1713 struct sctp_nets *netl;
1714 int had_a_existing_tcb = 0;
1716 #ifdef SCTP_DEBUG
1717 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1718 kprintf("sctp_handle_cookie: handling COOKIE-ECHO\n");
1720 #endif
1722 if (inp_p == NULL) {
1723 #ifdef SCTP_DEBUG
1724 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1725 kprintf("sctp_handle_cookie: null inp_p!\n");
1727 #endif
1728 return (NULL);
1730 /* First get the destination address setup too. */
1731 iph = mtod(m, struct ip *);
1732 if (iph->ip_v == IPVERSION) {
1733 /* its IPv4 */
1734 struct sockaddr_in *sin;
1735 sin = (struct sockaddr_in *)(localep_sa);
1736 memset(sin, 0, sizeof(*sin));
1737 sin->sin_family = AF_INET;
1738 sin->sin_len = sizeof(*sin);
1739 sin->sin_port = sh->dest_port;
1740 sin->sin_addr.s_addr = iph->ip_dst.s_addr ;
1741 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1742 /* its IPv6 */
1743 struct ip6_hdr *ip6;
1744 struct sockaddr_in6 *sin6;
1745 sin6 = (struct sockaddr_in6 *)(localep_sa);
1746 memset(sin6, 0, sizeof(*sin6));
1747 sin6->sin6_family = AF_INET6;
1748 sin6->sin6_len = sizeof(struct sockaddr_in6);
1749 ip6 = mtod(m, struct ip6_hdr *);
1750 sin6->sin6_port = sh->dest_port;
1751 sin6->sin6_addr = ip6->ip6_dst;
1752 } else {
1753 return (NULL);
1756 cookie = &cp->cookie;
1757 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
1758 cookie_len = ntohs(cp->ch.chunk_length);
1760 if ((cookie->peerport != sh->src_port) &&
1761 (cookie->myport != sh->dest_port) &&
1762 (cookie->my_vtag != sh->v_tag)) {
1764 * invalid ports or bad tag. Note that we always leave
1765 * the v_tag in the header in network order and when we
1766 * stored it in the my_vtag slot we also left it in network
1767 * order. This maintians the match even though it may be in
1768 * the opposite byte order of the machine :->
1770 return (NULL);
1773 /* compute size of packet */
1774 if (m->m_flags & M_PKTHDR) {
1775 size_of_pkt = m->m_pkthdr.len;
1776 } else {
1777 /* Should have a pkt hdr really */
1778 struct mbuf *mat;
1779 mat = m;
1780 size_of_pkt = 0;
1781 while (mat != NULL) {
1782 size_of_pkt += mat->m_len;
1783 mat = mat->m_next;
1786 if (cookie_len > size_of_pkt ||
1787 cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
1788 sizeof(struct sctp_init_chunk) +
1789 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
1790 /* cookie too long! or too small */
1791 #ifdef SCTP_DEBUG
1792 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1793 kprintf("sctp_handle_cookie: cookie_len=%u, pkt size=%u\n", cookie_len, size_of_pkt);
1795 #endif /* SCTP_DEBUG */
1796 return (NULL);
1799 * split off the signature into its own mbuf (since it
1800 * should not be calculated in the sctp_hash_digest_m() call).
1802 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
1803 if (sig_offset > size_of_pkt) {
1804 /* packet not correct size! */
1805 /* XXX this may already be accounted for earlier... */
1806 #ifdef SCTP_DEBUG
1807 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1808 kprintf("sctp_handle_cookie: sig offset=%u, pkt size=%u\n", sig_offset, size_of_pkt);
1810 #endif
1811 return (NULL);
1814 m_sig = m_split(m, sig_offset, MB_DONTWAIT);
1815 if (m_sig == NULL) {
1816 /* out of memory or ?? */
1817 #ifdef SCTP_DEBUG
1818 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1819 kprintf("sctp_handle_cookie: couldn't m_split the signature\n");
1821 #endif
1822 return (NULL);
1825 * compute the signature/digest for the cookie
1827 ep = &(*inp_p)->sctp_ep;
1828 l_inp = *inp_p;
1829 if (l_stcb) {
1830 SCTP_TCB_UNLOCK(l_stcb);
1832 SCTP_INP_RLOCK(l_inp);
1833 if (l_stcb) {
1834 SCTP_TCB_LOCK(l_stcb);
1836 /* which cookie is it? */
1837 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
1838 (ep->current_secret_number != ep->last_secret_number)) {
1839 /* it's the old cookie */
1840 #ifdef SCTP_DEBUG
1841 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1842 kprintf("sctp_handle_cookie: old cookie sig\n");
1844 #endif
1845 sctp_hash_digest_m((char *)ep->secret_key[(int)ep->last_secret_number],
1846 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1847 } else {
1848 /* it's the current cookie */
1849 #ifdef SCTP_DEBUG
1850 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1851 kprintf("sctp_handle_cookie: current cookie sig\n");
1853 #endif
1854 sctp_hash_digest_m((char *)ep->secret_key[(int)ep->current_secret_number],
1855 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1857 /* get the signature */
1858 SCTP_INP_RUNLOCK(l_inp);
1859 sig = (u_int8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (u_int8_t *)&tmp_sig);
1860 if (sig == NULL) {
1861 /* couldn't find signature */
1862 #ifdef SCTP_DEBUG
1863 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1864 kprintf("sctp_handle_cookie: couldn't pull the signature\n");
1866 #endif
1867 return (NULL);
1869 /* compare the received digest with the computed digest */
1870 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
1871 /* try the old cookie? */
1872 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
1873 (ep->current_secret_number != ep->last_secret_number)) {
1874 /* compute digest with old */
1875 #ifdef SCTP_DEBUG
1876 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1877 kprintf("sctp_handle_cookie: old cookie sig\n");
1879 #endif
1880 sctp_hash_digest_m((char *)ep->secret_key[(int)ep->last_secret_number],
1881 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1882 /* compare */
1883 if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
1884 cookie_ok = 1;
1886 } else {
1887 cookie_ok = 1;
1891 * Now before we continue we must reconstruct our mbuf so
1892 * that normal processing of any other chunks will work.
1895 struct mbuf *m_at;
1896 m_at = m;
1897 while (m_at->m_next != NULL) {
1898 m_at = m_at->m_next;
1900 m_at->m_next = m_sig;
1901 if (m->m_flags & M_PKTHDR) {
1903 * We should only do this if and only if the front
1904 * mbuf has a m_pkthdr... it should in theory.
1906 if (m_sig->m_flags & M_PKTHDR) {
1907 /* Add back to the pkt hdr of main m chain */
1908 m->m_pkthdr.len += m_sig->m_len;
1909 } else {
1911 * Got a problem, no pkthdr in split chain.
1912 * TSNH but we will handle it just in case
1914 int mmlen = 0;
1915 struct mbuf *lat;
1916 kprintf("Warning: Hitting m_split join TSNH code - fixed\n");
1917 lat = m_sig;
1918 while (lat) {
1919 mmlen += lat->m_len;
1920 lat = lat->m_next;
1922 m->m_pkthdr.len += mmlen;
1927 if (cookie_ok == 0) {
1928 #ifdef SCTP_DEBUG
1929 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1930 kprintf("handle_cookie_echo: cookie signature validation failed!\n");
1931 kprintf("offset = %u, cookie_offset = %u, sig_offset = %u\n",
1932 (u_int32_t)offset, cookie_offset, sig_offset);
1934 #endif
1935 return (NULL);
1937 #ifdef SCTP_DEBUG
1938 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1939 kprintf("handle_cookie_echo: cookie signature validation passed\n");
1941 #endif
1944 * check the cookie timestamps to be sure it's not stale
1946 SCTP_GETTIME_TIMEVAL(&now);
1947 /* Expire time is in Ticks, so we convert to seconds */
1948 time_expires.tv_sec = cookie->time_entered.tv_sec + cookie->cookie_life;
1949 time_expires.tv_usec = cookie->time_entered.tv_usec;
1950 #ifndef __FreeBSD__
1951 if (timercmp(&now, &time_expires, >))
1952 #else
1953 if (timevalcmp(&now, &time_expires, >))
1954 #endif
1956 /* cookie is stale! */
1957 struct mbuf *op_err;
1958 struct sctp_stale_cookie_msg *scm;
1959 u_int32_t tim;
1960 #ifdef SCTP_DEBUG
1961 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1962 kprintf("sctp_handle_cookie: got a STALE cookie!\n");
1964 #endif
1965 MGETHDR(op_err, MB_DONTWAIT, MT_HEADER);
1966 if (op_err == NULL) {
1967 /* FOOBAR */
1968 return (NULL);
1970 /* pre-reserve some space */
1971 op_err->m_data += sizeof(struct ip6_hdr);
1972 op_err->m_data += sizeof(struct sctphdr);
1973 op_err->m_data += sizeof(struct sctp_chunkhdr);
1975 /* Set the len */
1976 op_err->m_len = op_err->m_pkthdr.len = sizeof(struct sctp_stale_cookie_msg);
1977 scm = mtod(op_err, struct sctp_stale_cookie_msg *);
1978 scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
1979 scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
1980 (sizeof(u_int32_t))));
1981 /* seconds to usec */
1982 tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
1983 /* add in usec */
1984 if (tim == 0)
1985 tim = now.tv_usec - cookie->time_entered.tv_usec;
1986 scm->time_usec = htonl(tim);
1987 sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag);
1988 return (NULL);
1991 * Now we must see with the lookup address if we have an existing
1992 * asoc. This will only happen if we were in the COOKIE-WAIT state
1993 * and a INIT collided with us and somewhere the peer sent the
1994 * cookie on another address besides the single address our assoc
1995 * had for him. In this case we will have one of the tie-tags set
1996 * at least AND the address field in the cookie can be used to
1997 * look it up.
1999 to = NULL;
2000 if (cookie->addr_type == SCTP_IPV6_ADDRESS) {
2001 memset(&sin6, 0, sizeof(sin6));
2002 sin6.sin6_family = AF_INET6;
2003 sin6.sin6_len = sizeof(sin6);
2004 sin6.sin6_port = sh->src_port;
2005 sin6.sin6_scope_id = cookie->scope_id;
2006 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2007 sizeof(sin6.sin6_addr.s6_addr));
2008 to = (struct sockaddr *)&sin6;
2009 } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) {
2010 memset(&sin, 0, sizeof(sin));
2011 sin.sin_family = AF_INET;
2012 sin.sin_len = sizeof(sin);
2013 sin.sin_port = sh->src_port;
2014 sin.sin_addr.s_addr = cookie->address[0];
2015 to = (struct sockaddr *)&sin;
2018 if ((*stcb == NULL) && to) {
2019 /* Yep, lets check */
2020 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
2021 if (*stcb == NULL) {
2022 /* We should have only got back the same inp. If we
2023 * got back a different ep we have a problem. The original
2024 * findep got back l_inp and now
2026 if (l_inp != *inp_p) {
2027 kprintf("Bad problem find_ep got a diff inp then special_locate?\n");
2032 cookie_len -= SCTP_SIGNATURE_SIZE;
2033 if (*stcb == NULL) {
2034 /* this is the "normal" case... get a new TCB */
2035 #ifdef SCTP_DEBUG
2036 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2037 kprintf("sctp_handle_cookie: processing NEW cookie\n");
2039 #endif
2040 *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
2041 cookie_len, *inp_p, netp, to, &notification);
2042 /* now always decrement, since this is the normal
2043 * case.. we had no tcb when we entered.
2045 } else {
2046 /* this is abnormal... cookie-echo on existing TCB */
2047 #ifdef SCTP_DEBUG
2048 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2049 kprintf("sctp_handle_cookie: processing EXISTING cookie\n");
2051 #endif
2052 had_a_existing_tcb = 1;
2053 *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
2054 cookie, cookie_len, *inp_p, *stcb, *netp, to, &notification);
2057 if (*stcb == NULL) {
2058 /* still no TCB... must be bad cookie-echo */
2059 #ifdef SCTP_DEBUG
2060 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2061 kprintf("handle_cookie_echo: ACK! don't have a TCB!\n");
2063 #endif /* SCTP_DEBUG */
2064 return (NULL);
2068 * Ok, we built an association so confirm the address
2069 * we sent the INIT-ACK to.
2071 netl = sctp_findnet(*stcb, to);
2072 /* This code should in theory NOT run but
2074 if (netl == NULL) {
2075 int ret;
2076 #ifdef SCTP_DEBUG
2077 kprintf("TSNH! Huh, why do I need to add this address here?\n");
2078 #endif
2079 ret = sctp_add_remote_addr(*stcb, to, 0, 100);
2080 netl = sctp_findnet(*stcb, to);
2082 if (netl) {
2083 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2084 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2085 sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2086 netl);
2087 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2088 (*stcb), 0, (void *)netl);
2091 #ifdef SCTP_DEBUG
2092 else {
2093 kprintf("Could not add source address for some reason\n");
2095 #endif
2097 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2098 if (!had_a_existing_tcb ||
2099 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2101 * If we have a NEW cookie or the connect never reached
2102 * the connected state during collision we must do the
2103 * TCP accept thing.
2105 struct socket *so, *oso;
2106 struct sctp_inpcb *inp;
2107 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2109 * For a restart we will keep the same socket,
2110 * no need to do anything. I THINK!!
2112 sctp_ulp_notify(notification, *stcb, 0, NULL);
2113 return (m);
2115 oso = (*inp_p)->sctp_socket;
2116 SCTP_TCB_UNLOCK((*stcb));
2117 so = sonewconn(oso, SS_ISCONNECTED);
2118 SCTP_INP_WLOCK((*stcb)->sctp_ep);
2119 SCTP_TCB_LOCK((*stcb));
2120 SCTP_INP_WUNLOCK((*stcb)->sctp_ep);
2121 if (so == NULL) {
2122 struct mbuf *op_err;
2123 /* Too many sockets */
2124 #ifdef SCTP_DEBUG
2125 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
2126 kprintf("process_cookie_new: no room for another socket!\n");
2128 #endif /* SCTP_DEBUG */
2129 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2130 sctp_abort_association(*inp_p, NULL, m, iphlen,
2131 sh, op_err);
2132 sctp_free_assoc(*inp_p, *stcb);
2133 return (NULL);
2135 inp = (struct sctp_inpcb *)so->so_pcb;
2136 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2137 SCTP_PCB_FLAGS_CONNECTED |
2138 SCTP_PCB_FLAGS_IN_TCPPOOL |
2139 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2140 SCTP_PCB_FLAGS_DONT_WAKE);
2141 inp->sctp_socket = so;
2144 * Now we must move it from one hash table to another
2145 * and get the tcb in the right place.
2147 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2149 /* Switch over to the new guy */
2150 *inp_p = inp;
2152 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp,
2153 *stcb, *netp);
2155 sctp_ulp_notify(notification, *stcb, 0, NULL);
2156 return (m);
2159 if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2160 sctp_ulp_notify(notification, *stcb, 0, NULL);
2162 return (m);
2165 static void
2166 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp,
2167 struct sctp_tcb *stcb, struct sctp_nets *net)
2169 /* cp must not be used, others call this without a c-ack :-) */
2170 struct sctp_association *asoc;
2172 #ifdef SCTP_DEBUG
2173 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2174 kprintf("sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2176 #endif
2177 if (stcb == NULL)
2178 return;
2180 asoc = &stcb->asoc;
2182 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, stcb, net);
2184 /* process according to association state */
2185 if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2186 /* state change only needed when I am in right state */
2187 #ifdef SCTP_DEBUG
2188 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2189 kprintf("moving to OPEN state\n");
2191 #endif
2192 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2193 asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
2194 } else {
2195 asoc->state = SCTP_STATE_OPEN;
2198 /* update RTO */
2199 if (asoc->overall_error_count == 0) {
2200 net->RTO = sctp_calculate_rto(stcb, asoc, net,
2201 &asoc->time_entered);
2203 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2204 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL);
2205 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2206 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2207 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2208 soisconnected(stcb->sctp_ep->sctp_socket);
2210 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2211 stcb, net);
2212 /* since we did not send a HB make sure we don't double things */
2213 net->hb_responded = 1;
2215 if (stcb->asoc.sctp_autoclose_ticks &&
2216 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
2217 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2218 stcb->sctp_ep, stcb, NULL);
2222 * set ASCONF timer if ASCONFs are pending and allowed
2223 * (eg. addresses changed when init/cookie echo in flight)
2225 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) &&
2226 (stcb->asoc.peer_supports_asconf) &&
2227 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2228 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2229 stcb->sctp_ep, stcb,
2230 stcb->asoc.primary_destination);
2234 /* Toss the cookie if I can */
2235 sctp_toss_old_cookies(asoc);
2236 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2237 /* Restart the timer if we have pending data */
2238 struct sctp_tmit_chunk *chk;
2239 chk = TAILQ_FIRST(&asoc->sent_queue);
2240 if (chk) {
2241 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2242 stcb, chk->whoTo);
2248 static void
2249 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2250 struct sctp_tcb *stcb)
2252 struct sctp_nets *net;
2253 struct sctp_tmit_chunk *lchk;
2254 u_int32_t tsn;
2255 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) {
2256 return;
2258 sctp_pegs[SCTP_ECNE_RCVD]++;
2259 tsn = ntohl(cp->tsn);
2260 /* ECN Nonce stuff: need a resync and disable the nonce sum check */
2261 /* Also we make sure we disable the nonce_wait */
2262 lchk = TAILQ_FIRST(&stcb->asoc.send_queue);
2263 if (lchk == NULL) {
2264 stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
2265 } else {
2266 stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq;
2268 stcb->asoc.nonce_wait_for_ecne = 0;
2269 stcb->asoc.nonce_sum_check = 0;
2271 /* Find where it was sent, if possible */
2272 net = NULL;
2273 lchk = TAILQ_FIRST(&stcb->asoc.sent_queue);
2274 while (lchk) {
2275 if (lchk->rec.data.TSN_seq == tsn) {
2276 net = lchk->whoTo;
2277 break;
2279 if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ))
2280 break;
2281 lchk = TAILQ_NEXT(lchk, sctp_next);
2283 if (net == NULL)
2284 /* default is we use the primary */
2285 net = stcb->asoc.primary_destination;
2287 if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) {
2288 #ifdef SCTP_CWND_LOGGING
2289 int old_cwnd;
2290 #endif
2291 #ifdef SCTP_CWND_LOGGING
2292 old_cwnd = net->cwnd;
2293 #endif
2294 sctp_pegs[SCTP_CWR_PERFO]++;
2295 net->ssthresh = net->cwnd / 2;
2296 if (net->ssthresh < net->mtu) {
2297 net->ssthresh = net->mtu;
2298 /* here back off the timer as well, to slow us down */
2299 net->RTO <<= 2;
2301 net->cwnd = net->ssthresh;
2302 #ifdef SCTP_CWND_LOGGING
2303 sctp_log_cwnd(net, (net->cwnd-old_cwnd), SCTP_CWND_LOG_FROM_SAT);
2304 #endif
2305 /* we reduce once every RTT. So we will only lower
2306 * cwnd at the next sending seq i.e. the resync_tsn.
2308 stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn;
2311 * We always send a CWR this way if our previous one was lost
2312 * our peer will get an update, or if it is not time again
2313 * to reduce we still get the cwr to the peer.
2315 sctp_send_cwr(stcb, net, tsn);
2318 static void
2319 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb)
2321 /* Here we get a CWR from the peer. We must look in
2322 * the outqueue and make sure that we have a covered
2323 * ECNE in teh control chunk part. If so remove it.
2325 struct sctp_tmit_chunk *chk;
2326 struct sctp_ecne_chunk *ecne;
2328 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
2329 if (chk->rec.chunk_id != SCTP_ECN_ECHO) {
2330 continue;
2332 /* Look for and remove if it is the right TSN. Since
2333 * there is only ONE ECNE on the control queue at
2334 * any one time we don't need to worry about more than
2335 * one!
2337 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
2338 if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn),
2339 MAX_TSN) || (cp->tsn == ecne->tsn)) {
2340 /* this covers this ECNE, we can remove it */
2341 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
2342 sctp_next);
2343 if (chk->data) {
2344 sctp_m_freem(chk->data);
2345 chk->data = NULL;
2347 stcb->asoc.ctrl_queue_cnt--;
2348 sctp_free_remote_addr(chk->whoTo);
2349 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
2350 sctppcbinfo.ipi_count_chunk--;
2351 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
2352 panic("Chunk count is negative");
2354 sctppcbinfo.ipi_gencnt_chunk++;
2355 break;
2360 static void
2361 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp,
2362 struct sctp_tcb *stcb, struct sctp_nets *net)
2364 struct sctp_association *asoc;
2366 #ifdef SCTP_DEBUG
2367 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2368 kprintf("sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
2370 #endif
2371 if (stcb == NULL)
2372 return;
2374 asoc = &stcb->asoc;
2375 /* process according to association state */
2376 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
2377 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
2378 return;
2380 /* notify upper layer protocol */
2381 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL);
2382 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2383 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2384 stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
2385 stcb->sctp_ep->sctp_socket->so_snd.ssb_cc = 0;
2386 stcb->sctp_ep->sctp_socket->so_snd.ssb_mbcnt = 0;
2387 soisdisconnected(stcb->sctp_ep->sctp_socket);
2389 /* are the queues empty? they should be */
2390 if (!TAILQ_EMPTY(&asoc->send_queue) ||
2391 !TAILQ_EMPTY(&asoc->sent_queue) ||
2392 !TAILQ_EMPTY(&asoc->out_wheel)) {
2393 sctp_report_all_outbound(stcb);
2395 /* stop the timer */
2396 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
2397 /* free the TCB */
2398 sctp_free_assoc(stcb->sctp_ep, stcb);
2399 return;
2402 static int
2403 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
2404 struct sctp_nets *net, u_int8_t flg)
2406 switch (desc->chunk_type) {
2407 case SCTP_DATA:
2408 /* find the tsn to resend (possibly */
2410 u_int32_t tsn;
2411 struct sctp_tmit_chunk *tp1;
2412 tsn = ntohl(desc->tsn_ifany);
2413 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2414 while (tp1) {
2415 if (tp1->rec.data.TSN_seq == tsn) {
2416 /* found it */
2417 break;
2419 if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn,
2420 MAX_TSN)) {
2421 /* not found */
2422 tp1 = NULL;
2423 break;
2425 tp1 = TAILQ_NEXT(tp1, sctp_next);
2427 if (tp1 == NULL) {
2428 /* Do it the other way */
2429 sctp_pegs[SCTP_PDRP_DNFND]++;
2430 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2431 while (tp1) {
2432 if (tp1->rec.data.TSN_seq == tsn) {
2433 /* found it */
2434 break;
2436 tp1 = TAILQ_NEXT(tp1, sctp_next);
2439 if (tp1 == NULL) {
2440 sctp_pegs[SCTP_PDRP_TSNNF]++;
2442 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
2443 u_int8_t *ddp;
2444 if (((tp1->rec.data.state_flags & SCTP_WINDOW_PROBE) == SCTP_WINDOW_PROBE) &&
2445 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
2446 sctp_pegs[SCTP_PDRP_DIWNP]++;
2447 return (0);
2449 if (stcb->asoc.peers_rwnd == 0 &&
2450 (flg & SCTP_FROM_MIDDLE_BOX)) {
2451 sctp_pegs[SCTP_PDRP_DIZRW]++;
2452 return (0);
2454 ddp = (u_int8_t *)(mtod(tp1->data, caddr_t) +
2455 sizeof(struct sctp_data_chunk));
2457 unsigned int iii;
2458 for (iii = 0; iii < sizeof(desc->data_bytes);
2459 iii++) {
2460 if (ddp[iii] != desc->data_bytes[iii]) {
2461 sctp_pegs[SCTP_PDRP_BADD]++;
2462 return (-1);
2466 if (tp1->sent != SCTP_DATAGRAM_RESEND) {
2467 stcb->asoc.sent_queue_retran_cnt++;
2469 /* We zero out the nonce so resync not needed */
2470 tp1->rec.data.ect_nonce = 0;
2472 if (tp1->do_rtt) {
2474 * this guy had a RTO calculation pending on it,
2475 * cancel it
2477 tp1->whoTo->rto_pending = 0;
2478 tp1->do_rtt = 0;
2480 sctp_pegs[SCTP_PDRP_MARK]++;
2481 tp1->sent = SCTP_DATAGRAM_RESEND;
2483 * mark it as if we were doing a FR, since we
2484 * will be getting gap ack reports behind the
2485 * info from the router.
2487 tp1->rec.data.doing_fast_retransmit = 1;
2489 * mark the tsn with what sequences can cause a new FR.
2491 if (TAILQ_EMPTY(&stcb->asoc.send_queue) ) {
2492 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
2493 } else {
2494 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
2497 /* restart the timer */
2498 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2499 stcb, tp1->whoTo);
2500 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2501 stcb, tp1->whoTo);
2503 /* fix counts and things */
2504 tp1->whoTo->flight_size -= tp1->send_size;
2505 if (tp1->whoTo->flight_size < 0) {
2506 tp1->whoTo->flight_size = 0;
2508 stcb->asoc.total_flight -= tp1->book_size;
2509 if (stcb->asoc.total_flight < 0) {
2510 stcb->asoc.total_flight = 0;
2512 stcb->asoc.total_flight_count--;
2513 if (stcb->asoc.total_flight_count < 0) {
2514 stcb->asoc.total_flight_count = 0;
2516 tp1->snd_count--;
2519 /* audit code */
2520 unsigned int audit;
2521 audit = 0;
2522 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
2523 if (tp1->sent == SCTP_DATAGRAM_RESEND)
2524 audit++;
2526 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
2527 sctp_next) {
2528 if (tp1->sent == SCTP_DATAGRAM_RESEND)
2529 audit++;
2531 if (audit != stcb->asoc.sent_queue_retran_cnt) {
2532 kprintf("**Local Audit finds cnt:%d asoc cnt:%d\n",
2533 audit, stcb->asoc.sent_queue_retran_cnt);
2534 #ifndef SCTP_AUDITING_ENABLED
2535 stcb->asoc.sent_queue_retran_cnt = audit;
2536 #endif
2540 break;
2541 case SCTP_ASCONF:
2543 struct sctp_tmit_chunk *asconf;
2544 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
2545 sctp_next) {
2546 if (asconf->rec.chunk_id == SCTP_ASCONF) {
2547 break;
2550 if (asconf) {
2551 if (asconf->sent != SCTP_DATAGRAM_RESEND)
2552 stcb->asoc.sent_queue_retran_cnt++;
2553 asconf->sent = SCTP_DATAGRAM_RESEND;
2554 asconf->snd_count--;
2557 break;
2558 case SCTP_INITIATION:
2559 /* resend the INIT */
2560 stcb->asoc.dropped_special_cnt++;
2561 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
2563 * If we can get it in, in a few attempts we do this,
2564 * otherwise we let the timer fire.
2566 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
2567 stcb, net);
2568 sctp_send_initiate(stcb->sctp_ep, stcb);
2570 break;
2571 case SCTP_SELECTIVE_ACK:
2572 /* resend the sack */
2573 sctp_send_sack(stcb);
2574 break;
2575 case SCTP_HEARTBEAT_REQUEST:
2576 /* resend a demand HB */
2577 sctp_send_hb(stcb, 1, net);
2578 break;
2579 case SCTP_SHUTDOWN:
2580 #ifdef SCTP_DEBUG
2581 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2582 kprintf("%s:%d sends a shutdown\n",
2583 __FILE__,
2584 __LINE__
2587 #endif
2588 sctp_send_shutdown(stcb, net);
2589 break;
2590 case SCTP_SHUTDOWN_ACK:
2591 sctp_send_shutdown_ack(stcb, net);
2592 break;
2593 case SCTP_COOKIE_ECHO:
2595 struct sctp_tmit_chunk *cookie;
2596 cookie = NULL;
2597 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
2598 sctp_next) {
2599 if (cookie->rec.chunk_id == SCTP_COOKIE_ECHO) {
2600 break;
2603 if (cookie) {
2604 if (cookie->sent != SCTP_DATAGRAM_RESEND)
2605 stcb->asoc.sent_queue_retran_cnt++;
2606 cookie->sent = SCTP_DATAGRAM_RESEND;
2607 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, stcb, net);
2610 break;
2611 case SCTP_COOKIE_ACK:
2612 sctp_send_cookie_ack(stcb);
2613 break;
2614 case SCTP_ASCONF_ACK:
2615 /* resend last asconf ack */
2616 sctp_send_asconf_ack(stcb, 1);
2617 break;
2618 case SCTP_FORWARD_CUM_TSN:
2619 send_forward_tsn(stcb, &stcb->asoc);
2620 break;
2621 /* can't do anything with these */
2622 case SCTP_PACKET_DROPPED:
2623 case SCTP_INITIATION_ACK: /* this should not happen */
2624 case SCTP_HEARTBEAT_ACK:
2625 case SCTP_ABORT_ASSOCIATION:
2626 case SCTP_OPERATION_ERROR:
2627 case SCTP_SHUTDOWN_COMPLETE:
2628 case SCTP_ECN_ECHO:
2629 case SCTP_ECN_CWR:
2630 default:
2631 break;
2633 return (0);
2636 static void
2637 sctp_reset_in_stream(struct sctp_tcb *stcb,
2638 struct sctp_stream_reset_response *resp, int number_entries)
2640 int i;
2641 uint16_t *list, temp;
2643 /* We set things to 0xffff since this is the last delivered
2644 * sequence and we will be sending in 0 after the reset.
2647 if (resp->reset_flags & SCTP_RESET_PERFORMED) {
2648 if (number_entries) {
2649 list = resp->list_of_streams;
2650 for (i = 0; i < number_entries; i++) {
2651 temp = ntohs(list[i]);
2652 list[i] = temp;
2653 if (list[i] >= stcb->asoc.streamincnt) {
2654 kprintf("Invalid stream in-stream reset %d\n", list[i]);
2655 continue;
2657 stcb->asoc.strmin[(list[i])].last_sequence_delivered = 0xffff;
2659 } else {
2660 list = NULL;
2661 for (i = 0; i < stcb->asoc.streamincnt; i++) {
2662 stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
2665 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list);
2669 static void
2670 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
2672 struct sctp_tmit_chunk *chk, *nchk;
2673 struct sctp_association *asoc;
2675 asoc = &stcb->asoc;
2677 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
2678 chk; chk = nchk) {
2679 nchk = TAILQ_NEXT(chk, sctp_next);
2680 if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
2681 struct sctp_stream_reset_req *strreq;
2682 strreq = mtod(chk->data, struct sctp_stream_reset_req *);
2683 if (strreq->sr_req.ph.param_type == ntohs(SCTP_STR_RESET_RESPONSE)) {
2684 /* we only clean up the request */
2685 continue;
2686 } else if (strreq->sr_req.ph.param_type != ntohs(SCTP_STR_RESET_REQUEST)) {
2687 kprintf("TSNH, an unknown stream reset request is in queue %x\n",
2688 (u_int)ntohs(strreq->sr_req.ph.param_type));
2689 continue;
2691 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
2692 TAILQ_REMOVE(&asoc->control_send_queue,
2693 chk,
2694 sctp_next);
2695 if (chk->data) {
2696 sctp_m_freem(chk->data);
2697 chk->data = NULL;
2699 asoc->ctrl_queue_cnt--;
2700 sctp_free_remote_addr(chk->whoTo);
2701 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
2702 sctppcbinfo.ipi_count_chunk--;
2703 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
2704 panic("Chunk count is negative");
2706 sctppcbinfo.ipi_gencnt_chunk++;
2707 /* we can only have one of these so we break */
2708 break;
2714 void
2715 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
2716 struct sctp_stream_reset_response *resp)
2718 uint32_t seq, tsn;
2719 int number_entries, param_length;
2721 param_length = ntohs(resp->ph.param_length);
2722 seq = ntohl(resp->reset_req_seq_resp);
2723 if (seq == stcb->asoc.str_reset_seq_out) {
2724 sctp_clean_up_stream_reset(stcb);
2725 stcb->asoc.str_reset_seq_out++;
2726 stcb->asoc.stream_reset_outstanding = 0;
2727 tsn = ntohl(resp->reset_at_tsn);
2728 number_entries = (param_length - sizeof(struct sctp_stream_reset_response))/sizeof(uint16_t);
2729 tsn--;
2730 if ((tsn == stcb->asoc.cumulative_tsn) ||
2731 (compare_with_wrap(stcb->asoc.cumulative_tsn, tsn, MAX_TSN))) {
2732 /* no problem we are good to go */
2733 sctp_reset_in_stream(stcb, resp, number_entries);
2734 } else {
2735 /* So, we have a stream reset but there
2736 * is pending data. We need to copy
2737 * out the stream_reset and then queue
2738 * any data = or > resp->reset_at_tsn
2740 if (stcb->asoc.pending_reply != NULL) {
2741 /* FIX ME FIX ME
2742 * This IS WRONG. We need
2743 * to queue each of these up
2744 * and only release the chunks
2745 * for each reset that the cum-ack
2746 * goes by. This is a short cut.
2748 FREE(stcb->asoc.pending_reply, M_PCB);
2749 stcb->asoc.pending_reply = NULL;
2751 MALLOC(stcb->asoc.pending_reply, struct sctp_stream_reset_response *, param_length,
2752 M_PCB, M_NOWAIT);
2753 if (stcb->asoc.pending_reply == NULL)
2754 return; /* XXX */
2755 memcpy(stcb->asoc.pending_reply, resp, param_length);
2758 } else {
2759 /* duplicate */
2760 #ifdef SCTP_DEBUG
2761 kprintf("Duplicate old stream reset resp next:%x this one:%x\n",
2762 stcb->asoc.str_reset_seq_out, seq);
2763 #endif
2768 static void
2769 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct sctp_stream_reset_req *sr_req)
2771 int chk_length, param_len;
2772 struct sctp_paramhdr *ph;
2773 /* now it may be a reset or a reset-response */
2774 struct sctp_stream_reset_request *req;
2775 struct sctp_stream_reset_response *resp;
2776 chk_length = ntohs(sr_req->ch.chunk_length);
2778 ph = (struct sctp_paramhdr *)&sr_req->sr_req;
2779 while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_request)) {
2780 param_len = ntohs(ph->param_length);
2781 if (ntohs(ph->param_type) == SCTP_STR_RESET_REQUEST) {
2782 /* this will send the ACK and do the reset if needed */
2783 req = (struct sctp_stream_reset_request *)ph;
2784 sctp_send_str_reset_ack(stcb, req);
2785 } else if (ntohs(ph->param_type) == SCTP_STR_RESET_RESPONSE) {
2786 /* Now here is a tricky one. We reset our receive side
2787 * of the streams. But what happens if the peers
2788 * next sending TSN is NOT equal to 1 minus our cumack?
2789 * And if his cumack is not equal to our next one out - 1
2790 * we have another problem if this is receprical.
2792 resp = (struct sctp_stream_reset_response *)ph;
2793 sctp_handle_stream_reset_response(stcb, resp);
2795 ph = (struct sctp_paramhdr *)((caddr_t)ph + SCTP_SIZE32(param_len));
2796 chk_length -= SCTP_SIZE32(param_len);
2801 * Handle a router or endpoints report of a packet loss, there
2802 * are two ways to handle this, either we get the whole packet
2803 * and must disect it ourselves (possibly with truncation and
2804 * or corruption) or it is a summary from a middle box that did
2805 * the disectting for us.
2807 static void
2808 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
2809 struct sctp_tcb *stcb, struct sctp_nets *net)
2811 u_int32_t bottle_bw, on_queue;
2812 u_int16_t trunc_len;
2813 unsigned int chlen;
2814 unsigned int at;
2815 struct sctp_chunk_desc desc;
2816 struct sctp_chunkhdr *ch;
2818 chlen = ntohs(cp->ch.chunk_length);
2819 chlen -= sizeof(struct sctp_pktdrop_chunk);
2820 /* XXX possible chlen underflow */
2821 if (chlen == 0) {
2822 ch = NULL;
2823 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
2824 sctp_pegs[SCTP_PDRP_BWRPT]++;
2825 } else {
2826 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
2827 chlen -= sizeof(struct sctphdr);
2828 /* XXX possible chlen underflow */
2829 memset(&desc, 0, sizeof(desc));
2832 /* first update a rwnd possibly */
2833 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
2834 /* From a peer, we get a rwnd report */
2835 u_int32_t a_rwnd;
2837 sctp_pegs[SCTP_PDRP_FEHOS]++;
2839 bottle_bw = ntohl(cp->bottle_bw);
2840 on_queue = ntohl(cp->current_onq);
2841 if (bottle_bw && on_queue) {
2842 /* a rwnd report is in here */
2843 if (bottle_bw > on_queue)
2844 a_rwnd = bottle_bw - on_queue;
2845 else
2846 a_rwnd = 0;
2848 if (a_rwnd <= 0)
2849 stcb->asoc.peers_rwnd = 0;
2850 else {
2851 if (a_rwnd > stcb->asoc.total_flight) {
2852 stcb->asoc.peers_rwnd =
2853 a_rwnd - stcb->asoc.total_flight;
2854 } else {
2855 stcb->asoc.peers_rwnd = 0;
2857 if (stcb->asoc.peers_rwnd <
2858 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
2859 /* SWS sender side engages */
2860 stcb->asoc.peers_rwnd = 0;
2864 } else {
2865 sctp_pegs[SCTP_PDRP_FMBOX]++;
2867 trunc_len = (u_int16_t)ntohs(cp->trunc_len);
2868 /* now the chunks themselves */
2869 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
2870 desc.chunk_type = ch->chunk_type;
2871 /* get amount we need to move */
2872 at = ntohs(ch->chunk_length);
2873 if (at < sizeof(struct sctp_chunkhdr)) {
2874 /* corrupt chunk, maybe at the end? */
2875 sctp_pegs[SCTP_PDRP_CRUPT]++;
2876 break;
2878 if (trunc_len == 0) {
2879 /* we are supposed to have all of it */
2880 if (at > chlen) {
2881 /* corrupt skip it */
2882 sctp_pegs[SCTP_PDRP_CRUPT]++;
2883 break;
2885 } else {
2886 /* is there enough of it left ? */
2887 if (desc.chunk_type == SCTP_DATA) {
2888 if (chlen < (sizeof(struct sctp_data_chunk) +
2889 sizeof(desc.data_bytes))) {
2890 break;
2892 } else {
2893 if (chlen < sizeof(struct sctp_chunkhdr)) {
2894 break;
2898 if (desc.chunk_type == SCTP_DATA) {
2899 /* can we get out the tsn? */
2900 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
2901 sctp_pegs[SCTP_PDRP_MB_DA]++;
2903 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(u_int32_t)) ) {
2904 /* yep */
2905 struct sctp_data_chunk *dcp;
2906 u_int8_t *ddp;
2907 unsigned int iii;
2908 dcp = (struct sctp_data_chunk *)ch;
2909 ddp = (u_int8_t *)(dcp + 1);
2910 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
2911 desc.data_bytes[iii] = ddp[iii];
2913 desc.tsn_ifany = dcp->dp.tsn;
2914 } else {
2915 /* nope we are done. */
2916 sctp_pegs[SCTP_PDRP_NEDAT]++;
2917 break;
2919 } else {
2920 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
2921 sctp_pegs[SCTP_PDRP_MB_CT]++;
2924 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
2925 sctp_pegs[SCTP_PDRP_PDBRK]++;
2926 break;
2928 if (SCTP_SIZE32(at) > chlen) {
2929 break;
2931 chlen -= SCTP_SIZE32(at);
2932 if (chlen < sizeof(struct sctp_chunkhdr)) {
2933 /* done, none left */
2934 break;
2936 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
2939 /* now middle boxes in sat networks get a cwnd bump */
2940 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
2941 (stcb->asoc.sat_t3_loss_recovery == 0) &&
2942 (stcb->asoc.sat_network)) {
2944 * This is debateable but for sat networks it makes sense
2945 * Note if a T3 timer has went off, we will prohibit any
2946 * changes to cwnd until we exit the t3 loss recovery.
2948 u_int32_t bw_avail;
2949 int rtt, incr;
2950 #ifdef SCTP_CWND_LOGGING
2951 int old_cwnd=net->cwnd;
2952 #endif
2953 /* need real RTT for this calc */
2954 rtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2955 /* get bottle neck bw */
2956 bottle_bw = ntohl(cp->bottle_bw);
2957 /* and whats on queue */
2958 on_queue = ntohl(cp->current_onq);
2960 * adjust the on-queue if our flight is more it could be
2961 * that the router has not yet gotten data "in-flight" to it
2963 if (on_queue < net->flight_size)
2964 on_queue = net->flight_size;
2966 /* calculate the available space */
2967 bw_avail = (bottle_bw*rtt)/1000;
2968 if (bw_avail > bottle_bw) {
2970 * Cap the growth to no more than the bottle neck.
2971 * This can happen as RTT slides up due to queues.
2972 * It also means if you have more than a 1 second
2973 * RTT with a empty queue you will be limited to
2974 * the bottle_bw per second no matter if
2975 * other points have 1/2 the RTT and you could
2976 * get more out...
2978 bw_avail = bottle_bw;
2981 if (on_queue > bw_avail) {
2983 * No room for anything else don't allow anything
2984 * else to be "added to the fire".
2986 int seg_inflight, seg_onqueue, my_portion;
2987 net->partial_bytes_acked = 0;
2989 /* how much are we over queue size? */
2990 incr = on_queue - bw_avail;
2991 if (stcb->asoc.seen_a_sack_this_pkt) {
2992 /* undo any cwnd adjustment that
2993 * the sack might have made
2995 net->cwnd = net->prev_cwnd;
2998 /* Now how much of that is mine? */
2999 seg_inflight = net->flight_size / net->mtu;
3000 seg_onqueue = on_queue / net->mtu;
3001 my_portion = (incr * seg_inflight)/seg_onqueue;
3003 /* Have I made an adjustment already */
3004 if (net->cwnd > net->flight_size) {
3005 /* for this flight I made an adjustment
3006 * we need to decrease the portion by a share
3007 * our previous adjustment.
3009 int diff_adj;
3010 diff_adj = net->cwnd - net->flight_size;
3011 if (diff_adj > my_portion)
3012 my_portion = 0;
3013 else
3014 my_portion -= diff_adj;
3017 /* back down to the previous cwnd (assume
3018 * we have had a sack before this packet). minus
3019 * what ever portion of the overage is my fault.
3021 net->cwnd -= my_portion;
3023 /* we will NOT back down more than 1 MTU */
3024 if (net->cwnd <= net->mtu) {
3025 net->cwnd = net->mtu;
3027 /* force into CA */
3028 net->ssthresh = net->cwnd - 1;
3029 } else {
3031 * Take 1/4 of the space left or
3032 * max burst up .. whichever is less.
3034 incr = min((bw_avail - on_queue) >> 2,
3035 (int)stcb->asoc.max_burst * (int)net->mtu);
3036 net->cwnd += incr;
3038 if (net->cwnd > bw_avail) {
3039 /* We can't exceed the pipe size */
3040 net->cwnd = bw_avail;
3042 if (net->cwnd < net->mtu) {
3043 /* We always have 1 MTU */
3044 net->cwnd = net->mtu;
3046 #ifdef SCTP_CWND_LOGGING
3047 if (net->cwnd - old_cwnd != 0) {
3048 /* log only changes */
3049 sctp_log_cwnd(net, (net->cwnd - old_cwnd),
3050 SCTP_CWND_LOG_FROM_SAT);
3052 #endif
3056 extern int sctp_strict_init;
3059 * handles all control chunks in a packet
3060 * inputs:
3061 * - m: mbuf chain, assumed to still contain IP/SCTP header
3062 * - stcb: is the tcb found for this packet
3063 * - offset: offset into the mbuf chain to first chunkhdr
3064 * - length: is the length of the complete packet
3065 * outputs:
3066 * - length: modified to remaining length after control processing
3067 * - netp: modified to new sctp_nets after cookie-echo processing
3068 * - return NULL to discard the packet (ie. no asoc, bad packet,...)
3069 * otherwise return the tcb for this packet
3071 static struct sctp_tcb *
3072 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
3073 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
3074 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen)
3076 struct sctp_association *asoc;
3077 u_int32_t vtag_in;
3078 int num_chunks = 0; /* number of control chunks processed */
3079 int chk_length;
3080 int ret;
3083 * How big should this be, and should it be alloc'd?
3084 * Lets try the d-mtu-ceiling for now (2k) and that should
3085 * hopefully work ... until we get into jumbo grams and such..
3087 u_int8_t chunk_buf[DEFAULT_CHUNK_BUFFER];
3088 struct sctp_tcb *locked_tcb = stcb;
3090 #ifdef SCTP_DEBUG
3091 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3092 kprintf("sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
3093 iphlen, *offset, length, stcb);
3095 #endif /* SCTP_DEBUG */
3097 /* validate chunk header length... */
3098 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
3099 return (NULL);
3103 * validate the verification tag
3105 #ifdef SCTP_DEBUG
3106 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3107 kprintf("sctp_process_control: validating vtags\n");
3109 #endif /* SCTP_DEBUG */
3110 vtag_in = ntohl(sh->v_tag);
3111 if (ch->chunk_type == SCTP_INITIATION) {
3112 if (vtag_in != 0) {
3113 /* protocol error- silently discard... */
3114 #ifdef SCTP_DEBUG
3115 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3116 kprintf("sctp_process_control: INIT with vtag != 0\n");
3118 #endif /* SCTP_DEBUG */
3119 sctp_pegs[SCTP_BAD_VTAGS]++;
3120 if (locked_tcb)
3121 SCTP_TCB_UNLOCK(locked_tcb);
3122 return (NULL);
3124 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
3126 * first check if it's an ASCONF with an unknown src addr
3127 * we need to look inside to find the association
3129 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
3130 stcb = sctp_findassociation_ep_asconf(m, iphlen,
3131 *offset, sh, &inp, netp);
3133 if (stcb == NULL) {
3134 /* no association, so it's out of the blue... */
3135 sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL);
3136 #ifdef SCTP_DEBUG
3137 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3138 kprintf("sctp_process_control: handling OOTB packet, chunk type=%xh\n",
3139 ch->chunk_type);
3141 #endif /* SCTP_DEBUG */
3142 *offset = length;
3143 if (locked_tcb)
3144 SCTP_TCB_UNLOCK(locked_tcb);
3145 return (NULL);
3147 asoc = &stcb->asoc;
3148 /* ABORT and SHUTDOWN can use either v_tag... */
3149 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
3150 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
3151 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
3152 if ((vtag_in == asoc->my_vtag) ||
3153 ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
3154 (vtag_in == asoc->peer_vtag))) {
3155 /* this is valid */
3156 } else {
3157 /* drop this packet... */
3158 sctp_pegs[SCTP_BAD_VTAGS]++;
3159 if (locked_tcb)
3160 SCTP_TCB_UNLOCK(locked_tcb);
3161 return (NULL);
3163 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
3164 if (vtag_in != asoc->my_vtag) {
3166 * this could be a stale SHUTDOWN-ACK or the
3167 * peer never got the SHUTDOWN-COMPLETE and
3168 * is still hung; we have started a new asoc
3169 * but it won't complete until the shutdown is
3170 * completed
3172 if (locked_tcb)
3173 SCTP_TCB_UNLOCK(locked_tcb);
3174 sctp_handle_ootb(m, iphlen, *offset, sh, inp,
3175 NULL);
3176 return (NULL);
3178 } else {
3179 /* for all other chunks, vtag must match */
3181 if (vtag_in != asoc->my_vtag) {
3182 /* invalid vtag... */
3183 #ifdef SCTP_DEBUG
3184 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3185 kprintf("invalid vtag: %xh, expect %xh\n", vtag_in, asoc->my_vtag);
3187 #endif /* SCTP_DEBUG */
3188 sctp_pegs[SCTP_BAD_VTAGS]++;
3189 if (locked_tcb)
3190 SCTP_TCB_UNLOCK(locked_tcb);
3191 *offset = length;
3192 return (NULL);
3195 } /* end if !SCTP_COOKIE_ECHO */
3197 #ifdef SCTP_DEBUG
3198 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3199 kprintf("sctp_process_control: vtags ok, processing ctrl chunks\n");
3201 #endif /* SCTP_DEBUG */
3204 * process all control chunks...
3206 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
3207 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
3208 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
3209 /* implied cookie-ack.. we must have lost the ack */
3210 stcb->asoc.overall_error_count = 0;
3211 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
3214 while (IS_SCTP_CONTROL(ch)) {
3215 /* validate chunk length */
3216 chk_length = ntohs(ch->chunk_length);
3217 #ifdef SCTP_DEBUG
3218 if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
3219 kprintf("sctp_process_control: processing a chunk type=%u, len=%u\n", ch->chunk_type, chk_length);
3221 #endif /* SCTP_DEBUG */
3222 if ((size_t)chk_length < sizeof(*ch) ||
3223 (*offset + chk_length) > length) {
3224 #ifdef SCTP_DEBUG
3225 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3226 kprintf("sctp_process_control: chunk length invalid! *offset:%u, chk_length:%u > length:%u\n",
3227 *offset, length, chk_length);
3229 #endif /* SCTP_DEBUG */
3230 *offset = length;
3231 if (locked_tcb)
3232 SCTP_TCB_UNLOCK(locked_tcb);
3233 return (NULL);
3237 * INIT-ACK only gets the init ack "header" portion only
3238 * because we don't have to process the peer's COOKIE.
3239 * All others get a complete chunk.
3241 if (ch->chunk_type == SCTP_INITIATION_ACK) {
3242 /* get an init-ack chunk */
3243 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3244 sizeof(struct sctp_init_ack), chunk_buf);
3245 if (ch == NULL) {
3246 *offset = length;
3247 if (locked_tcb)
3248 SCTP_TCB_UNLOCK(locked_tcb);
3249 return (NULL);
3251 } else {
3252 /* get a complete chunk... */
3253 if ((size_t)chk_length > sizeof(chunk_buf)) {
3254 struct mbuf *oper;
3255 struct sctp_paramhdr *phdr;
3256 oper = NULL;
3257 MGETHDR(oper, MB_DONTWAIT, MT_HEADER);
3258 if (oper) {
3259 /* pre-reserve some space */
3260 oper->m_data +=
3261 sizeof(struct sctp_chunkhdr);
3262 phdr =
3263 mtod(oper, struct sctp_paramhdr *);
3264 phdr->param_type =
3265 htons(SCTP_CAUSE_OUT_OF_RESC);
3266 phdr->param_length =
3267 htons(sizeof(struct sctp_paramhdr));
3268 sctp_queue_op_err(stcb, oper);
3270 if (locked_tcb)
3271 SCTP_TCB_UNLOCK(locked_tcb);
3272 return (NULL);
3274 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3275 chk_length, chunk_buf);
3276 if (ch == NULL) {
3277 kprintf("sctp_process_control: Can't get the all data....\n");
3278 *offset = length;
3279 if (locked_tcb)
3280 SCTP_TCB_UNLOCK(locked_tcb);
3281 return (NULL);
3285 num_chunks++;
3286 /* Save off the last place we got a control from */
3287 if ((*netp) && stcb) {
3288 stcb->asoc.last_control_chunk_from = *netp;
3290 #ifdef SCTP_AUDITING_ENABLED
3291 sctp_audit_log(0xB0, ch->chunk_type);
3292 #endif
3293 switch (ch->chunk_type) {
3294 case SCTP_INITIATION:
3295 /* must be first and only chunk */
3296 #ifdef SCTP_DEBUG
3297 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3298 kprintf("SCTP_INIT\n");
3300 #endif /* SCTP_DEBUG */
3301 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3302 /* We are not interested anymore */
3303 if (locked_tcb)
3304 SCTP_TCB_UNLOCK(locked_tcb);
3305 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3306 /* finish the job now */
3307 sctp_inpcb_free(inp, 1);
3309 *offset = length;
3310 return (NULL);
3312 if ((num_chunks > 1) ||
3313 (sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) {
3314 *offset = length;
3315 if (locked_tcb)
3316 SCTP_TCB_UNLOCK(locked_tcb);
3317 return (NULL);
3319 if ((stcb != NULL) &&
3320 (SCTP_GET_STATE(&stcb->asoc) ==
3321 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3322 sctp_send_shutdown_ack(stcb,
3323 stcb->asoc.primary_destination);
3324 *offset = length;
3325 if (locked_tcb)
3326 SCTP_TCB_UNLOCK(locked_tcb);
3327 return (NULL);
3329 sctp_handle_init(m, iphlen, *offset, sh,
3330 (struct sctp_init_chunk *)ch, inp, stcb, *netp);
3331 *offset = length;
3332 if (locked_tcb)
3333 SCTP_TCB_UNLOCK(locked_tcb);
3334 return (NULL);
3335 break;
3336 case SCTP_INITIATION_ACK:
3337 /* must be first and only chunk */
3338 #ifdef SCTP_DEBUG
3339 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3340 kprintf("SCTP_INIT-ACK\n");
3342 #endif /* SCTP_DEBUG */
3343 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3344 /* We are not interested anymore */
3345 if (locked_tcb)
3346 SCTP_TCB_UNLOCK(locked_tcb);
3347 *offset = length;
3348 if (stcb) {
3349 sctp_free_assoc(inp, stcb);
3350 } else {
3351 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3352 /* finish the job now */
3353 sctp_inpcb_free(inp, 1);
3356 return (NULL);
3358 if ((num_chunks > 1) ||
3359 (sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) {
3360 #ifdef SCTP_DEBUG
3361 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3362 kprintf("Length is %d rounded chk_length:%d .. dropping\n",
3363 length - *offset,
3364 SCTP_SIZE32(chk_length));
3366 #endif
3367 *offset = length;
3368 if (locked_tcb)
3369 SCTP_TCB_UNLOCK(locked_tcb);
3370 return (NULL);
3372 ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
3373 (struct sctp_init_ack_chunk *)ch, stcb, *netp);
3375 * Special case, I must call the output routine
3376 * to get the cookie echoed
3378 if ((stcb) && ret == 0)
3379 sctp_chunk_output(stcb->sctp_ep, stcb, 2);
3380 *offset = length;
3381 #ifdef SCTP_DEBUG
3382 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3383 kprintf("All done INIT-ACK processing\n");
3385 #endif
3386 if (locked_tcb)
3387 SCTP_TCB_UNLOCK(locked_tcb);
3388 return (NULL);
3389 break;
3390 case SCTP_SELECTIVE_ACK:
3391 #ifdef SCTP_DEBUG
3392 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3393 kprintf("SCTP_SACK\n");
3395 #endif /* SCTP_DEBUG */
3396 sctp_pegs[SCTP_PEG_SACKS_SEEN]++;
3398 int abort_now = 0;
3399 stcb->asoc.seen_a_sack_this_pkt = 1;
3400 sctp_handle_sack((struct sctp_sack_chunk *)ch,
3401 stcb, *netp, &abort_now);
3402 if (abort_now) {
3403 /* ABORT signal from sack processing */
3404 *offset = length;
3405 return (NULL);
3408 break;
3409 case SCTP_HEARTBEAT_REQUEST:
3410 #ifdef SCTP_DEBUG
3411 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3412 kprintf("SCTP_HEARTBEAT\n");
3414 #endif /* SCTP_DEBUG */
3415 sctp_pegs[SCTP_HB_RECV]++;
3416 sctp_send_heartbeat_ack(stcb, m, *offset, chk_length,
3417 *netp);
3419 /* He's alive so give him credit */
3420 stcb->asoc.overall_error_count = 0;
3421 break;
3422 case SCTP_HEARTBEAT_ACK:
3423 #ifdef SCTP_DEBUG
3424 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3425 kprintf("SCTP_HEARTBEAT-ACK\n");
3427 #endif /* SCTP_DEBUG */
3429 /* He's alive so give him credit */
3430 stcb->asoc.overall_error_count = 0;
3432 sctp_pegs[SCTP_HB_ACK_RECV]++;
3433 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
3434 stcb, *netp);
3435 break;
3436 case SCTP_ABORT_ASSOCIATION:
3437 #ifdef SCTP_DEBUG
3438 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3439 kprintf("SCTP_ABORT\n");
3441 #endif /* SCTP_DEBUG */
3442 sctp_handle_abort((struct sctp_abort_chunk *)ch,
3443 stcb, *netp);
3444 *offset = length;
3445 return (NULL);
3446 break;
3447 case SCTP_SHUTDOWN:
3448 #ifdef SCTP_DEBUG
3449 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3450 kprintf("SCTP_SHUTDOWN\n");
3452 #endif /* SCTP_DEBUG */
3454 int abort_flag = 0;
3455 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
3456 stcb, *netp, &abort_flag);
3457 if (abort_flag) {
3458 *offset = length;
3459 return (NULL);
3462 break;
3463 case SCTP_SHUTDOWN_ACK:
3464 #ifdef SCTP_DEBUG
3465 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3466 kprintf("SCTP_SHUTDOWN-ACK\n");
3468 #endif /* SCTP_DEBUG */
3469 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
3470 *offset = length;
3471 return (NULL);
3472 break;
3473 case SCTP_OPERATION_ERROR:
3474 #ifdef SCTP_DEBUG
3475 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3476 kprintf("SCTP_OP-ERR\n");
3478 #endif /* SCTP_DEBUG */
3479 if (sctp_handle_error(ch, stcb, *netp) < 0) {
3480 *offset = length;
3481 return (NULL);
3483 break;
3484 case SCTP_COOKIE_ECHO:
3485 #ifdef SCTP_DEBUG
3486 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3487 kprintf("SCTP_COOKIE-ECHO stcb is %p\n", stcb);
3489 #endif /* SCTP_DEBUG */
3490 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3491 /* We are not interested anymore */
3492 *offset = length;
3493 if (stcb) {
3494 sctp_free_assoc(inp, stcb);
3495 } else {
3496 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3497 /* finish the job now */
3498 sctp_inpcb_free(inp, 1);
3501 return (NULL);
3504 * First are we accepting?
3505 * We do this again here since it is possible
3506 * that a previous endpoint WAS listening responded to
3507 * a INIT-ACK and then closed. We opened and bound..
3508 * and are now no longer listening.
3510 if (((inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) == 0) ||
3511 (inp->sctp_socket->so_qlimit == 0)) {
3512 sctp_abort_association(inp, stcb, m, iphlen, sh,
3513 NULL);
3514 *offset = length;
3515 return (NULL);
3516 } else if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
3517 /* we are accepting so check limits like TCP */
3518 if (inp->sctp_socket->so_qlen >
3519 inp->sctp_socket->so_qlimit) {
3520 /* no space */
3521 struct mbuf *oper;
3522 struct sctp_paramhdr *phdr;
3523 oper = NULL;
3524 MGETHDR(oper, MB_DONTWAIT, MT_HEADER);
3525 if (oper) {
3526 oper->m_len =
3527 oper->m_pkthdr.len =
3528 sizeof(struct sctp_paramhdr);
3529 phdr = mtod(oper,
3530 struct sctp_paramhdr *);
3531 phdr->param_type =
3532 htons(SCTP_CAUSE_OUT_OF_RESC);
3533 phdr->param_length =
3534 htons(sizeof(struct sctp_paramhdr));
3536 sctp_abort_association(inp, stcb, m,
3537 iphlen, sh, oper);
3538 *offset = length;
3539 return (NULL);
3543 struct mbuf *ret_buf;
3544 ret_buf = sctp_handle_cookie_echo(m, iphlen,
3545 *offset, sh,
3546 (struct sctp_cookie_echo_chunk *)ch, &inp,
3547 &stcb, netp);
3548 #ifdef SCTP_DEBUG
3549 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3550 kprintf("ret_buf:%p length:%d off:%d\n",
3551 ret_buf, length, *offset);
3553 #endif /* SCTP_DEBUG */
3555 if (ret_buf == NULL) {
3556 if (locked_tcb) {
3557 SCTP_TCB_UNLOCK(locked_tcb);
3559 #ifdef SCTP_DEBUG
3560 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3561 kprintf("GAK, null buffer\n");
3563 #endif /* SCTP_DEBUG */
3564 *offset = length;
3565 return (NULL);
3567 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
3569 * Restart the timer if we have pending
3570 * data
3572 struct sctp_tmit_chunk *chk;
3573 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
3574 if (chk) {
3575 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
3576 stcb->sctp_ep, stcb,
3577 chk->whoTo);
3581 break;
3582 case SCTP_COOKIE_ACK:
3583 #ifdef SCTP_DEBUG
3584 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3585 kprintf("SCTP_COOKIE-ACK\n");
3587 #endif /* SCTP_DEBUG */
3589 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3590 /* We are not interested anymore */
3591 sctp_free_assoc(inp, stcb);
3592 *offset = length;
3593 return (NULL);
3595 /* He's alive so give him credit */
3596 stcb->asoc.overall_error_count = 0;
3597 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch,
3598 stcb, *netp);
3599 break;
3600 case SCTP_ECN_ECHO:
3601 #ifdef SCTP_DEBUG
3602 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3603 kprintf("SCTP_ECN-ECHO\n");
3605 #endif /* SCTP_DEBUG */
3606 /* He's alive so give him credit */
3607 stcb->asoc.overall_error_count = 0;
3608 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
3609 stcb);
3610 break;
3611 case SCTP_ECN_CWR:
3612 #ifdef SCTP_DEBUG
3613 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3614 kprintf("SCTP_ECN-CWR\n");
3616 #endif /* SCTP_DEBUG */
3617 /* He's alive so give him credit */
3618 stcb->asoc.overall_error_count = 0;
3620 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb);
3621 break;
3622 case SCTP_SHUTDOWN_COMPLETE:
3623 #ifdef SCTP_DEBUG
3624 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3625 kprintf("SCTP_SHUTDOWN-COMPLETE\n");
3627 #endif /* SCTP_DEBUG */
3628 /* must be first and only chunk */
3629 if ((num_chunks > 1) ||
3630 (length - *offset > SCTP_SIZE32(chk_length))) {
3631 *offset = length;
3632 if (locked_tcb)
3633 SCTP_TCB_UNLOCK(locked_tcb);
3635 return (NULL);
3637 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
3638 stcb, *netp);
3639 *offset = length;
3640 return (NULL);
3641 break;
3642 case SCTP_ASCONF:
3643 #ifdef SCTP_DEBUG
3644 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3645 kprintf("SCTP_ASCONF\n");
3647 #endif /* SCTP_DEBUG */
3648 /* He's alive so give him credit */
3649 stcb->asoc.overall_error_count = 0;
3651 sctp_handle_asconf(m, *offset,
3652 (struct sctp_asconf_chunk *)ch, stcb, *netp);
3653 break;
3654 case SCTP_ASCONF_ACK:
3655 #ifdef SCTP_DEBUG
3656 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3657 kprintf("SCTP_ASCONF-ACK\n");
3659 #endif /* SCTP_DEBUG */
3660 /* He's alive so give him credit */
3661 stcb->asoc.overall_error_count = 0;
3663 sctp_handle_asconf_ack(m, *offset,
3664 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp);
3665 break;
3666 case SCTP_FORWARD_CUM_TSN:
3667 #ifdef SCTP_DEBUG
3668 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3669 kprintf("SCTP_FWD-TSN\n");
3671 #endif /* SCTP_DEBUG */
3672 /* He's alive so give him credit */
3674 int abort_flag = 0;
3675 stcb->asoc.overall_error_count = 0;
3676 *fwd_tsn_seen = 1;
3677 sctp_handle_forward_tsn(stcb,
3678 (struct sctp_forward_tsn_chunk *)ch, &abort_flag);
3679 if (abort_flag) {
3680 *offset = length;
3681 return (NULL);
3682 } else {
3683 stcb->asoc.overall_error_count = 0;
3687 break;
3688 case SCTP_STREAM_RESET:
3689 #ifdef SCTP_DEBUG
3690 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3691 kprintf("SCTP_STREAM_RESET\n");
3693 #endif /* SCTP_DEBUG */
3694 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3695 chk_length, chunk_buf);
3696 if (stcb->asoc.peer_supports_strreset == 0) {
3697 /* hmm, peer should have annonced this, but
3698 * we will turn it on since he is sending us
3699 * a stream reset.
3701 stcb->asoc.peer_supports_strreset = 1;
3703 sctp_handle_stream_reset(stcb, (struct sctp_stream_reset_req *)ch);
3704 break;
3705 case SCTP_PACKET_DROPPED:
3706 #ifdef SCTP_DEBUG
3707 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3708 kprintf("SCTP_PACKET_DROPPED\n");
3710 #endif /* SCTP_DEBUG */
3711 /* re-get it all please */
3712 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3713 chk_length, chunk_buf);
3715 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
3716 stcb, *netp);
3719 break;
3720 default:
3721 /* it's an unknown chunk! */
3722 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
3723 struct mbuf *mm;
3724 struct sctp_paramhdr *phd;
3725 MGETHDR(mm, MB_DONTWAIT, MT_HEADER);
3726 if (mm) {
3727 phd = mtod(mm, struct sctp_paramhdr *);
3728 /* We cheat and use param type since we
3729 * did not bother to define a error
3730 * cause struct.
3731 * They are the same basic format with
3732 * different names.
3734 phd->param_type =
3735 htons(SCTP_CAUSE_UNRECOG_CHUNK);
3736 phd->param_length =
3737 htons(chk_length + sizeof(*phd));
3738 mm->m_len = sizeof(*phd);
3739 mm->m_next = sctp_m_copym(m, *offset,
3740 SCTP_SIZE32(chk_length),
3741 MB_DONTWAIT);
3742 if (mm->m_next) {
3743 mm->m_pkthdr.len =
3744 SCTP_SIZE32(chk_length) +
3745 sizeof(*phd);
3746 sctp_queue_op_err(stcb, mm);
3747 } else {
3748 sctp_m_freem(mm);
3749 #ifdef SCTP_DEBUG
3750 if (sctp_debug_on &
3751 SCTP_DEBUG_INPUT1) {
3752 kprintf("Gak can't copy the chunk into operr %d bytes\n",
3753 chk_length);
3755 #endif
3758 #ifdef SCTP_DEBUG
3759 else {
3760 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3761 kprintf("Gak can't mgethdr for op-err of unrec chunk\n");
3764 #endif
3766 if ((ch->chunk_type & 0x80) == 0) {
3767 /* discard this packet */
3768 *offset = length;
3769 return (stcb);
3770 } /* else skip this bad chunk and continue... */
3771 break;
3772 } /* switch (ch->chunk_type) */
3773 /* get the next chunk */
3774 *offset += SCTP_SIZE32(chk_length);
3775 if (*offset >= length) {
3776 /* no more data left in the mbuf chain */
3777 break;
3779 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3780 sizeof(struct sctp_chunkhdr), chunk_buf);
3781 if (ch == NULL) {
3782 if (locked_tcb)
3783 SCTP_TCB_UNLOCK(locked_tcb);
3784 *offset = length;
3785 return (NULL);
3787 } /* while */
3788 return (stcb);
3793 * Process the ECN bits we have something set so
3794 * we must look to see if it is ECN(0) or ECN(1) or CE
3796 static void
3797 sctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net,
3798 u_int8_t ecn_bits)
3800 if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
3802 } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) {
3804 * we only add to the nonce sum for ECT1, ECT0
3805 * does not change the NS bit (that we have
3806 * yet to find a way to send it yet).
3809 /* ECN Nonce stuff */
3810 stcb->asoc.receiver_nonce_sum++;
3811 stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM;
3814 * Drag up the last_echo point if cumack is larger since we
3815 * don't want the point falling way behind by more than 2^^31
3816 * and then having it be incorrect.
3818 if (compare_with_wrap(stcb->asoc.cumulative_tsn,
3819 stcb->asoc.last_echo_tsn, MAX_TSN)) {
3820 stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
3822 } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) {
3824 * Drag up the last_echo point if cumack is larger since we
3825 * don't want the point falling way behind by more than 2^^31
3826 * and then having it be incorrect.
3828 if (compare_with_wrap(stcb->asoc.cumulative_tsn,
3829 stcb->asoc.last_echo_tsn, MAX_TSN)) {
3830 stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
3835 static void
3836 sctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net,
3837 u_int32_t high_tsn, u_int8_t ecn_bits)
3839 if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
3841 * we possibly must notify the sender that a congestion
3842 * window reduction is in order. We do this
3843 * by adding a ECNE chunk to the output chunk
3844 * queue. The incoming CWR will remove this chunk.
3846 if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn,
3847 MAX_TSN)) {
3848 /* Yep, we need to add a ECNE */
3849 sctp_send_ecn_echo(stcb, net, high_tsn);
3850 stcb->asoc.last_echo_tsn = high_tsn;
3856 * common input chunk processing (v4 and v6)
3859 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
3860 int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
3861 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
3862 u_int8_t ecn_bits)
3865 * Control chunk processing
3867 u_int32_t high_tsn;
3868 int fwd_tsn_seen = 0, data_processed = 0;
3869 struct mbuf *m = *mm;
3870 int abort_flag = 0;
3872 sctp_pegs[SCTP_DATAGRAMS_RCVD]++;
3873 #ifdef SCTP_AUDITING_ENABLED
3874 sctp_audit_log(0xE0, 1);
3875 sctp_auditing(0, inp, stcb, net);
3876 #endif
3878 #ifdef SCTP_DEBUG
3879 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3880 kprintf("Ok, Common input processing called, m:%x iphlen:%d offset:%d\n",
3881 (u_int)m, iphlen, offset);
3883 #endif /* SCTP_DEBUG */
3884 if (IS_SCTP_CONTROL(ch)) {
3885 /* process the control portion of the SCTP packet */
3886 #ifdef SCTP_DEBUG
3887 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3888 kprintf("Processing control\n");
3890 #endif /* SCTP_DEBUG */
3892 stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
3893 inp, stcb, &net, &fwd_tsn_seen);
3894 } else {
3896 * no control chunks, so pre-process DATA chunks
3897 * (these checks are taken care of by control processing)
3899 #ifdef SCTP_DEBUG
3900 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3901 kprintf("No control present\n");
3903 #endif /* SCTP_DEBUG */
3905 if (stcb == NULL) {
3906 /* out of the blue DATA chunk */
3907 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL);
3908 return (1);
3910 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
3911 /* v_tag mismatch! */
3912 sctp_pegs[SCTP_BAD_VTAGS]++;
3913 SCTP_TCB_UNLOCK(stcb);
3914 return (1);
3917 if (stcb == NULL) {
3919 * no valid TCB for this packet,
3920 * or we found it's a bad packet while processing control,
3921 * or we're done with this packet (done or skip rest of data),
3922 * so we drop it...
3924 return (1);
3926 #ifdef SCTP_DEBUG
3927 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3928 kprintf("Ok, control finished time to look for data (%d) offset:%d\n",
3929 length, offset);
3931 #endif /* SCTP_DEBUG */
3933 * DATA chunk processing
3935 /* plow through the data chunks while length > offset */
3936 stcb->asoc.seen_a_sack_this_pkt = 0;
3938 if (length > offset) {
3939 int retval;
3941 * First check to make sure our state is correct.
3942 * We would not get here unless we really did have a
3943 * tag, so we don't abort if this happens, just
3944 * dump the chunk silently.
3946 switch (SCTP_GET_STATE(&stcb->asoc)) {
3947 case SCTP_STATE_COOKIE_ECHOED:
3949 * we consider data with valid tags in
3950 * this state shows us the cookie-ack was lost.
3951 * Imply it was there.
3953 stcb->asoc.overall_error_count = 0;
3954 sctp_handle_cookie_ack(
3955 (struct sctp_cookie_ack_chunk *)ch, stcb, net);
3956 break;
3957 case SCTP_STATE_COOKIE_WAIT:
3959 * We consider OOTB any data sent during asoc setup.
3961 sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL);
3962 SCTP_TCB_UNLOCK(stcb);
3963 return (1);
3964 break;
3965 case SCTP_STATE_EMPTY: /* should not happen */
3966 case SCTP_STATE_INUSE: /* should not happen */
3967 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
3968 case SCTP_STATE_SHUTDOWN_ACK_SENT:
3969 default:
3970 #ifdef SCTP_DEBUG
3971 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3972 kprintf("Got data in invalid state %d.. dropping\n", stcb->asoc.state);
3974 #endif
3975 SCTP_TCB_UNLOCK(stcb);
3976 return (1);
3977 break;
3978 case SCTP_STATE_OPEN:
3979 case SCTP_STATE_SHUTDOWN_SENT:
3980 break;
3982 /* take care of ECN, part 1. */
3983 if (stcb->asoc.ecn_allowed &&
3984 (ecn_bits & (SCTP_ECT0_BIT|SCTP_ECT1_BIT)) ) {
3985 sctp_process_ecn_marked_a(stcb, net, ecn_bits);
3987 /* plow through the data chunks while length > offset */
3988 retval = sctp_process_data(mm, iphlen, &offset, length, sh,
3989 inp, stcb, net, &high_tsn);
3990 if (retval == 2) {
3991 /* The association aborted, NO UNLOCK needed
3992 * since the association is destroyed.
3994 return (0);
3997 data_processed = 1;
3998 if (retval == 0) {
3999 /* take care of ecn part 2. */
4000 if (stcb->asoc.ecn_allowed && (ecn_bits & (SCTP_ECT0_BIT|SCTP_ECT1_BIT)) ) {
4001 sctp_process_ecn_marked_b(stcb, net, high_tsn, ecn_bits);
4007 * Anything important needs to have been m_copy'ed in
4008 * process_data
4011 if ((data_processed == 0) && (fwd_tsn_seen)) {
4012 int was_a_gap = 0;
4013 if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
4014 stcb->asoc.cumulative_tsn, MAX_TSN)) {
4015 /* there was a gap before this data was processed */
4016 was_a_gap = 1;
4018 sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
4019 if (abort_flag) {
4020 /* Again, we aborted so NO UNLOCK needed */
4021 return (0);
4024 /* trigger send of any chunks in queue... */
4025 #ifdef SCTP_AUDITING_ENABLED
4026 sctp_audit_log(0xE0, 2);
4027 sctp_auditing(1, inp, stcb, net);
4028 #endif
4029 #ifdef SCTP_DEBUG
4030 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4031 kprintf("Check for chunk output prw:%d tqe:%d tf=%d\n",
4032 stcb->asoc.peers_rwnd,
4033 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
4034 stcb->asoc.total_flight);
4036 #endif
4037 if (stcb->asoc.peers_rwnd > 0 ||
4038 !TAILQ_EMPTY(&stcb->asoc.control_send_queue) ||
4039 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)) {
4040 #ifdef SCTP_DEBUG
4041 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4042 kprintf("Calling chunk OUTPUT\n");
4044 #endif
4045 sctp_chunk_output(inp, stcb, 3);
4046 #ifdef SCTP_DEBUG
4047 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4048 kprintf("chunk OUTPUT returns\n");
4050 #endif
4053 #ifdef SCTP_AUDITING_ENABLED
4054 sctp_audit_log(0xE0, 3);
4055 sctp_auditing(2, inp, stcb, net);
4056 #endif
4057 SCTP_TCB_UNLOCK(stcb);
4058 return (0);
4061 #if defined(__OpenBSD__)
4062 static void
4063 sctp_saveopt(struct sctp_inpcb *inp, struct mbuf **mp, struct ip *ip,
4064 struct mbuf *m)
4066 if (inp->ip_inp.inp.inp_flags & INP_RECVDSTADDR) {
4067 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
4068 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
4069 if (*mp)
4070 mp = &(*mp)->m_next;
4073 #endif
4075 extern int sctp_no_csum_on_loopback;
4077 #if defined(__FreeBSD__) || defined(__APPLE__)
4078 void
4079 sctp_input(struct mbuf *m, int off)
4080 #else
4081 void
4082 sctp_input(struct mbuf *m, ...)
4083 #endif
4085 int iphlen;
4086 u_int8_t ecn_bits;
4087 struct ip *ip;
4088 struct sctphdr *sh;
4089 struct sctp_inpcb *inp = NULL;
4090 struct mbuf *opts = 0;
4091 /*#ifdef INET6*/
4092 /* Don't think this is needed */
4093 /* struct ip6_recvpktopts opts6;*/
4094 /*#endif INET6 */
4096 u_int32_t check, calc_check;
4097 struct sctp_nets *net;
4098 struct sctp_tcb *stcb = NULL;
4099 struct sctp_chunkhdr *ch;
4100 int refcount_up = 0;
4101 int length, mlen, offset;
4102 #if defined(__OpenBSD__) && defined(IPSEC)
4103 struct inpcb *i_inp;
4104 struct m_tag *mtag;
4105 struct tdb_ident *tdbi;
4106 struct tdb *tdb;
4107 int error;
4108 #endif
4110 #if !(defined(__FreeBSD__) || defined(__APPLE__))
4111 #ifdef __DragonFly__
4112 __va_list ap;
4113 __va_start(ap, m);
4114 iphlen = __va_arg(ap, int);
4115 __va_end(ap);
4116 #else
4117 va_list ap;
4119 va_start(ap, m);
4120 iphlen = va_arg(ap, int);
4121 va_end(ap);
4122 #endif
4123 #else
4124 iphlen = off;
4125 #endif
4126 net = NULL;
4127 sctp_pegs[SCTP_INPKTS]++;
4128 #ifdef SCTP_DEBUG
4129 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4130 kprintf("V4 input gets a packet iphlen:%d pktlen:%d\n", iphlen, m->m_pkthdr.len);
4132 #endif
4133 /*#ifdef INET6*/
4134 /* Don't think this is needed */
4135 /* bzero(&opts6, sizeof(opts6));*/
4136 /*#endif INET6 */
4139 * Strip IP options, we don't allow any in or out.
4141 if ((size_t)iphlen > sizeof(struct ip)) {
4142 ip_stripoptions(m);
4143 iphlen = sizeof(struct ip);
4147 * Get IP, SCTP, and first chunk header together in first mbuf.
4149 ip = mtod(m, struct ip *);
4150 offset = iphlen + sizeof(*sh) + sizeof(*ch);
4151 if (m->m_len < offset) {
4152 if ((m = m_pullup(m, offset)) == 0) {
4153 sctp_pegs[SCTP_HDR_DROPS]++;
4154 return;
4156 ip = mtod(m, struct ip *);
4158 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
4159 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
4161 /* SCTP does not allow broadcasts or multicasts */
4162 #if defined(__NetBSD__) || defined(__OpenBSD__)
4163 if (IN_MULTICAST(ip->ip_dst.s_addr))
4164 #else
4165 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)))
4166 #endif
4168 sctp_pegs[SCTP_IN_MCAST]++;
4169 goto bad;
4171 if (in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
4172 sctp_pegs[SCTP_IN_MCAST]++;
4173 goto bad;
4176 /* destination port of 0 is illegal, based on RFC2960. */
4177 if (sh->dest_port == 0) {
4178 sctp_pegs[SCTP_HDR_DROPS]++;
4179 goto bad;
4182 /* validate SCTP checksum */
4183 if ((sctp_no_csum_on_loopback == 0) ||
4184 (m->m_pkthdr.rcvif == NULL) ||
4185 (m->m_pkthdr.rcvif->if_type != IFT_LOOP)) {
4186 /* we do NOT validate things from the loopback if the
4187 * sysctl is set to 1.
4189 check = sh->checksum; /* save incoming checksum */
4190 if ((check == 0) && (sctp_no_csum_on_loopback)) {
4191 /* special hook for where we got a local address
4192 * somehow routed across a non IFT_LOOP type interface
4194 if (ip->ip_src.s_addr == ip->ip_dst.s_addr)
4195 goto sctp_skip_csum_4;
4197 sh->checksum = 0; /* prepare for calc */
4198 calc_check = sctp_calculate_sum(m, &mlen, iphlen);
4199 if (calc_check != check) {
4200 #ifdef SCTP_DEBUG
4201 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4202 kprintf("Bad CSUM on SCTP packet calc_check:%x check:%x m:%x mlen:%d iphlen:%d\n",
4203 calc_check, check, (u_int)m, mlen, iphlen);
4205 #endif
4207 stcb = sctp_findassociation_addr(m, iphlen,
4208 offset - sizeof(*ch),
4209 sh, ch, &inp, &net);
4210 if ((inp) && (stcb)) {
4211 sctp_send_packet_dropped(stcb, net, m, iphlen,
4213 sctp_chunk_output(inp, stcb, 2);
4214 } else if ((inp != NULL) && (stcb == NULL)) {
4215 refcount_up = 1;
4217 sctp_pegs[SCTP_BAD_CSUM]++;
4218 goto bad;
4220 sh->checksum = calc_check;
4221 } else {
4222 sctp_skip_csum_4:
4223 mlen = m->m_pkthdr.len;
4225 /* validate mbuf chain length with IP payload length */
4226 #if defined(__NetBSD__) || defined(__OpenBSD__)
4227 /* Open BSD gives us the len in network order, fix it */
4228 NTOHS(ip->ip_len);
4229 #endif
4230 if (mlen < (ip->ip_len - iphlen)) {
4231 sctp_pegs[SCTP_HDR_DROPS]++;
4232 goto bad;
4236 * Locate pcb and tcb for datagram
4237 * sctp_findassociation_addr() wants IP/SCTP/first chunk header...
4239 #ifdef SCTP_DEBUG
4240 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4241 kprintf("V4 find association\n");
4243 #endif
4245 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
4246 sh, ch, &inp, &net);
4247 /* inp's ref-count increased && stcb locked */
4248 if (inp == NULL) {
4249 struct sctp_init_chunk *init_chk, chunk_buf;
4251 sctp_pegs[SCTP_NOPORTS]++;
4252 #ifdef ICMP_BANDLIM
4254 * we use the bandwidth limiting to protect against
4255 * sending too many ABORTS all at once. In this case
4256 * these count the same as an ICMP message.
4258 if (badport_bandlim(0) < 0)
4259 goto bad;
4260 #endif /* ICMP_BANDLIM */
4261 #ifdef SCTP_DEBUG
4262 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4263 kprintf("Sending a ABORT from packet entry!\n");
4265 #endif
4266 if (ch->chunk_type == SCTP_INITIATION) {
4267 /* we do a trick here to get the INIT tag,
4268 * dig in and get the tag from the INIT and
4269 * put it in the common header.
4271 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4272 iphlen + sizeof(*sh), sizeof(*init_chk),
4273 (u_int8_t *)&chunk_buf);
4274 if (init_chk != NULL)
4275 sh->v_tag = init_chk->init.initiate_tag;
4277 sctp_send_abort(m, iphlen, sh, 0, NULL);
4278 goto bad;
4279 } else if (stcb == NULL) {
4280 refcount_up = 1;
4282 #ifdef IPSEC
4284 * I very much doubt any of the IPSEC stuff will work but I have
4285 * no idea, so I will leave it in place.
4288 #ifdef __OpenBSD__
4289 /* FIX ME: this don't work... :) */
4291 /* Find most recent IPsec tag */
4292 i_inp = &inp->ip_inp.inp;
4293 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
4294 s = splnet();
4295 if (mtag != NULL) {
4296 tdbi = (struct tdb_ident *)(mtag + 1);
4297 tdb = gettdb(tdbi->spi, &tdbi->dst, tdbi->proto);
4298 } else
4299 tdb = NULL;
4300 ipsp_spd_lookup(m, af, iphlen, &error, IPSP_DIRECTION_IN,
4301 tdb, i_inp);
4302 if (error) {
4303 splx(s);
4304 sctp_pegs[SCTP_HDR_DROPS]++;
4305 goto bad;
4308 /* Latch SA */
4309 if (i_inp->inp_tdb_in != tdb) {
4310 if (tdb) {
4311 tdb_add_inp(tdb, i_inp, 1);
4312 if (i_inp->inp_ipo == NULL) {
4313 i_inp->inp_ipo = ipsec_add_policy(i_inp, af,
4314 IPSP_DIRECTION_OUT);
4315 if (i_inp->inp_ipo == NULL) {
4316 splx(s);
4317 sctp_pegs[SCTP_HDR_DROPS]++;
4318 goto bad;
4321 if (i_inp->inp_ipo->ipo_dstid == NULL &&
4322 tdb->tdb_srcid != NULL) {
4323 i_inp->inp_ipo->ipo_dstid = tdb->tdb_srcid;
4324 tdb->tdb_srcid->ref_count++;
4326 if (i_inp->inp_ipsec_remotecred == NULL &&
4327 tdb->tdb_remote_cred != NULL) {
4328 i_inp->inp_ipsec_remotecred =
4329 tdb->tdb_remote_cred;
4330 tdb->tdb_remote_cred->ref_count++;
4332 if (i_inp->inp_ipsec_remoteauth == NULL &&
4333 tdb->tdb_remote_auth != NULL) {
4334 i_inp->inp_ipsec_remoteauth =
4335 tdb->tdb_remote_auth;
4336 tdb->tdb_remote_auth->ref_count++;
4338 } else { /* Just reset */
4339 TAILQ_REMOVE(&i_inp->inp_tdb_in->tdb_inp_in, i_inp,
4340 inp_tdb_in_next);
4341 i_inp->inp_tdb_in = NULL;
4344 splx(s);
4346 #else
4347 if (ipsec4_in_reject_so(m, inp->ip_inp.inp.inp_socket)) {
4348 ipsecstat.in_polvio++;
4349 sctp_pegs[SCTP_HDR_DROPS]++;
4350 goto bad;
4352 #endif
4353 #endif /* IPSEC */
4356 * Construct sockaddr format source address.
4357 * Stuff source address and datagram in user buffer.
4359 if ((inp->ip_inp.inp.inp_flags & INP_CONTROLOPTS)
4360 #ifndef __OpenBSD__
4361 || (inp->sctp_socket->so_options & SO_TIMESTAMP)
4362 #endif
4364 #ifdef __OpenBSD__
4365 sctp_saveopt(inp, &opts, ip, m);
4366 #else
4367 ip_savecontrol((struct inpcb *)inp, &opts, ip, m);
4368 #endif
4372 * common chunk processing
4374 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4375 length = ip->ip_len + iphlen;
4376 #else
4377 length = ip->ip_len - (ip->ip_hl << 2) + iphlen;
4378 #endif
4379 offset -= sizeof(struct sctp_chunkhdr);
4381 ecn_bits = ip->ip_tos;
4382 crit_enter();
4383 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
4384 inp, stcb, net, ecn_bits);
4385 /* inp's ref-count reduced && stcb unlocked */
4386 crit_exit();
4387 if (m) {
4388 sctp_m_freem(m);
4390 if (opts)
4391 sctp_m_freem(opts);
4393 if ((inp) && (refcount_up)) {
4394 /* reduce ref-count */
4395 SCTP_INP_WLOCK(inp);
4396 SCTP_INP_DECR_REF(inp);
4397 SCTP_INP_WUNLOCK(inp);
4400 return;
4401 bad:
4402 if (stcb)
4403 SCTP_TCB_UNLOCK(stcb);
4405 if ((inp) && (refcount_up)) {
4406 /* reduce ref-count */
4407 SCTP_INP_WLOCK(inp);
4408 SCTP_INP_DECR_REF(inp);
4409 SCTP_INP_WUNLOCK(inp);
4412 if (m) {
4413 sctp_m_freem(m);
4415 if (opts)
4416 sctp_m_freem(opts);
4417 return;