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