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