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