Merge from vendor branch TEXINFO:
[dragonfly.git] / sys / netinet / sctp_timer.c
1 /*      $KAME: sctp_timer.c,v 1.28 2004/08/17 04:06:20 itojun Exp $     */
2 /*      $DragonFly: src/sys/netinet/sctp_timer.c,v 1.3 2005/07/15 15:15:27 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 #if !(defined(__OpenBSD__) || defined(__APPLE__))
33 #include "opt_ipsec.h"
34 #endif
35 #if defined(__FreeBSD__) || defined(__DragonFly__)
36 #include "opt_compat.h"
37 #include "opt_inet6.h"
38 #include "opt_inet.h"
39 #endif
40 #if defined(__NetBSD__)
41 #include "opt_inet.h"
42 #endif
43 #ifdef __APPLE__
44 #include <sctp.h>
45 #elif !defined(__OpenBSD__)
46 #include "opt_sctp.h"
47 #endif
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #ifndef __OpenBSD__
54 #include <sys/domain.h>
55 #endif
56 #include <sys/protosw.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/proc.h>
60 #include <sys/kernel.h>
61 #include <sys/sysctl.h>
62 #ifdef INET6
63 #include <sys/domain.h>
64 #endif
65
66 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
67 #include <sys/limits.h>
68 #else
69 #include <machine/limits.h>
70 #endif
71
72 #include <net/if.h>
73 #include <net/if_types.h>
74 #include <net/route.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #define _IP_VHL
78 #include <netinet/ip.h>
79 #include <netinet/in_pcb.h>
80 #include <netinet/in_var.h>
81 #include <netinet/ip_var.h>
82
83 #ifdef INET6
84 #include <netinet/ip6.h>
85 #include <netinet6/ip6_var.h>
86 #endif /* INET6 */
87
88 #include <netinet/sctp_pcb.h>
89
90 #ifdef IPSEC
91 #ifndef __OpenBSD__
92 #include <netinet6/ipsec.h>
93 #include <netkey/key.h>
94 #else
95 #undef IPSEC
96 #endif
97 #endif /* IPSEC */
98 #ifdef INET6
99 #include <netinet6/sctp6_var.h>
100 #endif
101 #include <netinet/sctp_var.h>
102 #include <netinet/sctp_timer.h>
103 #include <netinet/sctputil.h>
104 #include <netinet/sctp_output.h>
105 #include <netinet/sctp_hashdriver.h>
106 #include <netinet/sctp_header.h>
107 #include <netinet/sctp_indata.h>
108 #include <netinet/sctp_asconf.h>
109
110 #include <netinet/sctp.h>
111 #include <netinet/sctp_uio.h>
112
113 #include <net/net_osdep.h>
114
115 #ifdef SCTP_DEBUG
116 extern u_int32_t sctp_debug_on;
117 #endif /* SCTP_DEBUG */
118
119 void
120 sctp_audit_retranmission_queue(struct sctp_association *asoc)
121 {
122         struct sctp_tmit_chunk *chk;
123
124 #ifdef SCTP_DEBUG
125         if (sctp_debug_on & SCTP_DEBUG_TIMER4) {
126                 printf("Audit invoked on send queue cnt:%d onqueue:%d\n",
127                     asoc->sent_queue_retran_cnt,
128                     asoc->sent_queue_cnt);
129         }
130 #endif /* SCTP_DEBUG */
131         asoc->sent_queue_retran_cnt = 0;
132         asoc->sent_queue_cnt = 0;
133         TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
134                 if (chk->sent == SCTP_DATAGRAM_RESEND) {
135                         asoc->sent_queue_retran_cnt++;
136                 }
137                 asoc->sent_queue_cnt++;
138         }
139         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
140                 if (chk->sent == SCTP_DATAGRAM_RESEND) {
141                         asoc->sent_queue_retran_cnt++;
142                 }
143         }
144 #ifdef SCTP_DEBUG
145         if (sctp_debug_on & SCTP_DEBUG_TIMER4) {
146                 printf("Audit completes retran:%d onqueue:%d\n",
147                     asoc->sent_queue_retran_cnt,
148                     asoc->sent_queue_cnt);
149         }
150 #endif /* SCTP_DEBUG */
151 }
152
153 int
154 sctp_threshold_management(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
155     struct sctp_nets *net, uint16_t threshold)
156 {
157         if (net) {
158                 net->error_count++;
159 #ifdef SCTP_DEBUG
160                 if (sctp_debug_on & SCTP_DEBUG_TIMER4) {
161                         printf("Error count for %p now %d thresh:%d\n",
162                             net, net->error_count,
163                             net->failure_threshold);
164                 }
165 #endif /* SCTP_DEBUG */
166                 if (net->error_count >= net->failure_threshold) {
167                         /* We had a threshold failure */
168                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
169                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
170                                 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
171                                 if (net == stcb->asoc.primary_destination) {
172                                         net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
173                                 }
174                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
175                                                 stcb,
176                                                 SCTP_FAILED_THRESHOLD,
177                                                 (void *)net);
178                         }
179                 }
180                 /*********HOLD THIS COMMENT FOR PATCH OF ALTERNATE
181                  *********ROUTING CODE
182                  */
183                 /*********HOLD THIS COMMENT FOR END OF PATCH OF ALTERNATE
184                  *********ROUTING CODE
185                  */
186         }
187         if (stcb == NULL)
188                 return (0);
189
190         if (net) {
191                 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0) {
192                         stcb->asoc.overall_error_count++;
193                 }
194         } else {
195                 stcb->asoc.overall_error_count++;
196         }
197 #ifdef SCTP_DEBUG
198         if (sctp_debug_on & SCTP_DEBUG_TIMER4) {
199                 printf("Overall error count for %p now %d thresh:%u state:%x\n",
200                        &stcb->asoc,
201                        stcb->asoc.overall_error_count,
202                        (u_int)threshold,
203                        ((net == NULL) ? (u_int)0 : (u_int)net->dest_state));
204         }
205 #endif /* SCTP_DEBUG */
206         /* We specifically do not do >= to give the assoc one more
207          * change before we fail it.
208          */
209         if (stcb->asoc.overall_error_count > threshold) {
210                 /* Abort notification sends a ULP notify */
211                 struct mbuf *oper;
212                 MGET(oper, MB_DONTWAIT, MT_DATA);
213                 if (oper) {
214                         struct sctp_paramhdr *ph;
215                         u_int32_t *ippp;
216
217                         oper->m_len = sizeof(struct sctp_paramhdr) +
218                             sizeof(*ippp);
219                         ph = mtod(oper, struct sctp_paramhdr *);
220                         ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
221                         ph->param_length = htons(oper->m_len);
222                         ippp = (u_int32_t *)(ph + 1);
223                         *ippp = htonl(0x40000001);
224                 }
225                 sctp_abort_an_association(inp, stcb, SCTP_FAILED_THRESHOLD, oper);
226                 return (1);
227         }
228         return (0);
229 }
230
231 struct sctp_nets *
232 sctp_find_alternate_net(struct sctp_tcb *stcb,
233                         struct sctp_nets *net)
234 {
235         /* Find and return an alternate network if possible */
236         struct sctp_nets *alt, *mnet;
237         int once;
238
239         if (stcb->asoc.numnets == 1) {
240                 /* No others but net */
241                 return (TAILQ_FIRST(&stcb->asoc.nets));
242         }
243         mnet = net;
244         once = 0;
245
246         if (mnet == NULL) {
247                 mnet = TAILQ_FIRST(&stcb->asoc.nets);
248         }
249         do {
250                 alt = TAILQ_NEXT(mnet, sctp_next);
251                 if (alt == NULL) {
252                         once++;
253                         if (once > 1) {
254                                 break;
255                         }
256                         alt = TAILQ_FIRST(&stcb->asoc.nets);
257                 }
258                 if (alt->ro.ro_rt == NULL) {
259 #ifndef SCOPEDROUTING
260                         struct sockaddr_in6 *sin6;
261                         sin6 = (struct sockaddr_in6 *)&alt->ro._l_addr;
262                         if (sin6->sin6_family == AF_INET6) {
263 #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__) || defined(__DragonFly__)
264                                 (void)in6_embedscope(&sin6->sin6_addr, sin6,
265                                                      NULL, NULL);
266 #else
267                                 (void)in6_embedscope(&sin6->sin6_addr, sin6);
268 #endif
269                         }
270 #endif
271 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
272                         rtalloc_ign((struct route*)&alt->ro, 0UL);
273 #else
274                         rtalloc((struct route*)&alt->ro);
275 #endif
276 #ifndef SCOPEDROUTING
277                         if (sin6->sin6_family == AF_INET6) {
278                                 (void)in6_recoverscope(sin6, &sin6->sin6_addr,
279                                     NULL);
280                         }
281 #endif
282                         alt->src_addr_selected = 0;
283                 }
284                 if (
285                         ((alt->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE) &&
286                         (alt->ro.ro_rt != NULL) &&
287                         (!(alt->dest_state & SCTP_ADDR_UNCONFIRMED))
288                         ) {
289                         /* Found a reachable address */
290                         break;
291                 }
292                 mnet = alt;
293         } while (alt != NULL);
294
295         if (alt == NULL) {
296                 /* Case where NO insv network exists (dormant state) */
297                 /* we rotate destinations */
298                 once = 0;
299                 mnet = net;
300                 do {
301                         alt = TAILQ_NEXT(mnet, sctp_next);
302                         if (alt == NULL) {
303                                 once++;
304                                 if (once > 1) {
305                                         break;
306                                 }
307                                 alt = TAILQ_FIRST(&stcb->asoc.nets);
308                         }
309                         if ((!(alt->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
310                             (alt != net)) {
311                                 /* Found an alternate address */
312                                 break;
313                         }
314                         mnet = alt;
315                 } while (alt != NULL);
316         }
317         if (alt == NULL) {
318                 return (net);
319         }
320         return (alt);
321 }
322
323 static void
324 sctp_backoff_on_timeout(struct sctp_tcb *stcb,
325                         struct sctp_nets *net,
326                         int win_probe,
327                         int num_marked)
328 {
329 #ifdef SCTP_DEBUG
330         int oldRTO;
331
332         oldRTO = net->RTO;
333 #endif /* SCTP_DEBUG */
334         net->RTO <<= 1;
335 #ifdef SCTP_DEBUG
336         if (sctp_debug_on & SCTP_DEBUG_TIMER2) {
337                 printf("Timer doubles from %d ms -to-> %d ms\n",
338                        oldRTO, net->RTO);
339         }
340 #endif /* SCTP_DEBUG */
341
342         if (net->RTO > stcb->asoc.maxrto) {
343                 net->RTO = stcb->asoc.maxrto;
344 #ifdef SCTP_DEBUG
345                 if (sctp_debug_on & SCTP_DEBUG_TIMER2) {
346                         printf("Growth capped by maxrto %d\n",
347                                net->RTO);
348                 }
349 #endif /* SCTP_DEBUG */
350         }
351
352
353         if ((win_probe == 0) && num_marked) {
354                 /* We don't apply penalty to window probe scenarios */
355 #ifdef SCTP_CWND_LOGGING
356                 int old_cwnd=net->cwnd;
357 #endif
358                 net->ssthresh = net->cwnd >> 1;
359                 if (net->ssthresh < (net->mtu << 1)) {
360                         net->ssthresh = (net->mtu << 1);
361                 }
362                 net->cwnd = net->mtu;
363                 /* floor of 1 mtu */
364                 if (net->cwnd < net->mtu)
365                         net->cwnd = net->mtu;
366 #ifdef SCTP_CWND_LOGGING
367                 sctp_log_cwnd(net, net->cwnd-old_cwnd, SCTP_CWND_LOG_FROM_RTX);
368 #endif
369
370                 net->partial_bytes_acked = 0;
371 #ifdef SCTP_DEBUG
372                 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
373                         printf("collapse cwnd to 1MTU ssthresh to %d\n",
374                                net->ssthresh);
375                 }
376 #endif
377
378         }
379 }
380
381
382 static int
383 sctp_mark_all_for_resend(struct sctp_tcb *stcb,
384                          struct sctp_nets *net,
385                          struct sctp_nets *alt,
386                          int *num_marked)
387 {
388
389         /*
390          * Mark all chunks (well not all) that were sent to *net for retransmission.
391          * Move them to alt for there destination as well... We only
392          * mark chunks that have been outstanding long enough to have
393          * received feed-back.
394          */
395         struct sctp_tmit_chunk *chk, *tp2;
396         struct sctp_nets *lnets;
397         struct timeval now, min_wait, tv;
398         int cur_rto;
399         int win_probes, non_win_probes, orig_rwnd, audit_tf, num_mk, fir;
400         unsigned int cnt_mk;
401         u_int32_t orig_flight;
402         u_int32_t tsnlast, tsnfirst;
403
404         /* none in flight now */
405         audit_tf = 0;
406         fir=0;
407         /* figure out how long a data chunk must be pending
408          * before we can mark it ..
409          */
410         SCTP_GETTIME_TIMEVAL(&now);
411         /* get cur rto in micro-seconds */
412         cur_rto = (((net->lastsa >> 2) + net->lastsv) >> 1);
413 #ifdef SCTP_FR_LOGGING
414         sctp_log_fr(cur_rto, 0, 0, SCTP_FR_T3_MARK_TIME);
415 #endif
416         cur_rto *= 1000;
417 #ifdef SCTP_FR_LOGGING
418         sctp_log_fr(cur_rto, 0, 0, SCTP_FR_T3_MARK_TIME);
419 #endif
420         tv.tv_sec = cur_rto / 1000000;
421         tv.tv_usec = cur_rto % 1000000;
422 #ifndef __FreeBSD__
423         timersub(&now, &tv, &min_wait);
424 #else
425         min_wait = now;
426         timevalsub(&min_wait, &tv);
427 #endif
428         if (min_wait.tv_sec < 0 || min_wait.tv_usec < 0) {
429                 /*
430                  * if we hit here, we don't
431                  * have enough seconds on the clock to account
432                  * for the RTO. We just let the lower seconds
433                  * be the bounds and don't worry about it. This
434                  * may mean we will mark a lot more than we should.
435                  */
436                 min_wait.tv_sec = min_wait.tv_usec = 0;
437         }
438 #ifdef SCTP_FR_LOGGING
439         sctp_log_fr(cur_rto, now.tv_sec, now.tv_usec, SCTP_FR_T3_MARK_TIME);
440         sctp_log_fr(0, min_wait.tv_sec, min_wait.tv_usec, SCTP_FR_T3_MARK_TIME);
441 #endif
442         stcb->asoc.total_flight -= net->flight_size;
443         if (stcb->asoc.total_flight < 0) {
444                 audit_tf = 1;
445                 stcb->asoc.total_flight = 0;
446         }
447         /* Our rwnd will be incorrect here since we are not adding
448          * back the cnt * mbuf but we will fix that down below.
449          */
450         orig_rwnd = stcb->asoc.peers_rwnd;
451         orig_flight = net->flight_size;
452         stcb->asoc.peers_rwnd += net->flight_size;
453         net->flight_size = 0;
454         net->rto_pending = 0;
455         net->fast_retran_ip= 0;
456         win_probes = non_win_probes = 0;
457 #ifdef SCTP_DEBUG
458         if (sctp_debug_on & SCTP_DEBUG_TIMER2) {
459                 printf("Marking ALL un-acked for retransmission at t3-timeout\n");
460         }
461 #endif /* SCTP_DEBUG */
462         /* Now on to each chunk */
463         num_mk = cnt_mk = 0;
464         tsnfirst = tsnlast = 0;
465         chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
466         for (;chk != NULL; chk = tp2) {
467                 tp2 = TAILQ_NEXT(chk, sctp_next);
468                 if ((compare_with_wrap(stcb->asoc.last_acked_seq,
469                                        chk->rec.data.TSN_seq,
470                                        MAX_TSN)) ||
471                     (stcb->asoc.last_acked_seq == chk->rec.data.TSN_seq)) {
472                         /* Strange case our list got out of order? */
473                         printf("Our list is out of order?\n");
474                         TAILQ_REMOVE(&stcb->asoc.sent_queue, chk, sctp_next);
475                         if (chk->data) {
476                                 sctp_release_pr_sctp_chunk(stcb, chk, 0xffff,
477                                     &stcb->asoc.sent_queue);
478                                 if (chk->flags & SCTP_PR_SCTP_BUFFER) {
479                                         stcb->asoc.sent_queue_cnt_removeable--;
480                                 }
481                         }
482                         stcb->asoc.sent_queue_cnt--;
483                         sctp_free_remote_addr(chk->whoTo);
484                         sctppcbinfo.ipi_count_chunk--;
485                         if ((int)sctppcbinfo.ipi_count_chunk < 0) {
486                                 panic("Chunk count is going negative");
487                         }
488                         SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
489                         sctppcbinfo.ipi_gencnt_chunk++;
490                         continue;
491                 }
492                 if ((chk->whoTo == net) && (chk->sent < SCTP_DATAGRAM_ACKED)) {
493                         /* found one to mark:
494                          * If it is less than DATAGRAM_ACKED it MUST
495                          * not be a skipped or marked TSN but instead
496                          * one that is either already set for retransmission OR
497                          * one that needs retransmission.
498                          */
499
500                         /* validate its been outstanding long enough */
501 #ifdef SCTP_FR_LOGGING
502                         sctp_log_fr(chk->rec.data.TSN_seq,
503                                     chk->sent_rcv_time.tv_sec,
504                                     chk->sent_rcv_time.tv_usec,
505                                     SCTP_FR_T3_MARK_TIME);
506 #endif
507                         if (chk->sent_rcv_time.tv_sec > min_wait.tv_sec) {
508                                 /* we have reached a chunk that was sent some
509                                  * seconds past our min.. forget it we will
510                                  * find no more to send.
511                                  */
512 #ifdef SCTP_FR_LOGGING
513                                 sctp_log_fr(0,
514                                             chk->sent_rcv_time.tv_sec,
515                                             chk->sent_rcv_time.tv_usec,
516                                             SCTP_FR_T3_STOPPED);
517 #endif
518                                 continue;
519                         } else if (chk->sent_rcv_time.tv_sec == min_wait.tv_sec) {
520                                 /* we must look at the micro seconds to know.
521                                  */
522                                 if (chk->sent_rcv_time.tv_usec >= min_wait.tv_usec) {
523                                         /* ok it was sent after our boundary time. */
524 #ifdef SCTP_FR_LOGGING
525                                         sctp_log_fr(0,
526                                                     chk->sent_rcv_time.tv_sec,
527                                                     chk->sent_rcv_time.tv_usec,
528                                                     SCTP_FR_T3_STOPPED);
529 #endif
530                                         continue;
531                                 }
532                         }
533                         stcb->asoc.total_flight_count--;
534                         if (stcb->asoc.total_flight_count < 0) {
535                                 stcb->asoc.total_flight_count = 0;
536                         }
537                         if ((chk->flags & (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) == SCTP_PR_SCTP_ENABLED) {
538                                 /* Is it expired? */
539                                 if ((now.tv_sec > chk->rec.data.timetodrop.tv_sec) ||
540                                     ((chk->rec.data.timetodrop.tv_sec == now.tv_sec) &&
541                                      (now.tv_usec > chk->rec.data.timetodrop.tv_usec))) {
542                                         /* Yes so drop it */
543                                         if (chk->data) {
544                                                 sctp_release_pr_sctp_chunk(stcb,
545                                                     chk,
546                                                     (SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_SENT),
547                                                     &stcb->asoc.sent_queue);
548                                         }
549                                 }
550                                 continue;
551                         }
552                         if (chk->sent != SCTP_DATAGRAM_RESEND) {
553                                 stcb->asoc.sent_queue_retran_cnt++;
554                                 num_mk++;
555                                 if (fir == 0) {
556                                         fir = 1;
557 #ifdef SCTP_DEBUG
558                                         if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
559                                                 printf("First TSN marked was %x\n",
560                                                        chk->rec.data.TSN_seq);
561                                         }
562 #endif
563                                         tsnfirst = chk->rec.data.TSN_seq;
564                                 }
565                                 tsnlast = chk->rec.data.TSN_seq;
566 #ifdef SCTP_FR_LOGGING
567                                 sctp_log_fr(chk->rec.data.TSN_seq, chk->snd_count,
568                                             0, SCTP_FR_T3_MARKED);
569
570 #endif
571                         }
572                         chk->sent = SCTP_DATAGRAM_RESEND;
573                         /* reset the TSN for striking and other FR stuff */
574                         chk->rec.data.doing_fast_retransmit = 0;
575 #ifdef SCTP_DEBUG
576                         if (sctp_debug_on & SCTP_DEBUG_TIMER3) {
577                                 printf("mark TSN:%x for retransmission\n", chk->rec.data.TSN_seq);
578                         }
579 #endif /* SCTP_DEBUG */
580                         /* Clear any time so NO RTT is being done */
581                         chk->do_rtt = 0;
582                         /* Bump up the count */
583                         if (compare_with_wrap(chk->rec.data.TSN_seq,
584                                               stcb->asoc.t3timeout_highest_marked,
585                                               MAX_TSN)) {
586                                 /* TSN_seq > than t3timeout so update */
587                                 stcb->asoc.t3timeout_highest_marked = chk->rec.data.TSN_seq;
588                         }
589                         if (alt != net) {
590                                 sctp_free_remote_addr(chk->whoTo);
591                                 chk->whoTo = alt;
592                                 alt->ref_count++;
593                         }
594                         if ((chk->rec.data.state_flags & SCTP_WINDOW_PROBE) !=
595                             SCTP_WINDOW_PROBE) {
596                                 non_win_probes++;
597                         } else {
598                                 chk->rec.data.state_flags &= ~SCTP_WINDOW_PROBE;
599                                 win_probes++;
600                         }
601                 }
602                 if (chk->sent == SCTP_DATAGRAM_RESEND) {
603                         cnt_mk++;
604                 }
605         }
606
607 #ifdef SCTP_FR_LOGGING
608         sctp_log_fr(tsnfirst, tsnlast, num_mk, SCTP_FR_T3_TIMEOUT);
609 #endif
610         /* compensate for the number we marked */
611         stcb->asoc.peers_rwnd += (num_mk /* * sizeof(struct mbuf)*/);
612
613 #ifdef SCTP_DEBUG
614         if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
615                 if (num_mk) {
616                         printf("LAST TSN marked was %x\n", tsnlast);
617                         printf("Num marked for retransmission was %d peer-rwd:%ld\n",
618                                num_mk, (u_long)stcb->asoc.peers_rwnd);
619                         printf("LAST TSN marked was %x\n", tsnlast);
620                         printf("Num marked for retransmission was %d peer-rwd:%d\n",
621                                num_mk,
622                                (int)stcb->asoc.peers_rwnd
623                                 );
624                 }
625         }
626 #endif
627         *num_marked = num_mk;
628         if (stcb->asoc.sent_queue_retran_cnt != cnt_mk) {
629                 printf("Local Audit says there are %d for retran asoc cnt:%d\n",
630                        cnt_mk, stcb->asoc.sent_queue_retran_cnt);
631 #ifndef SCTP_AUDITING_ENABLED
632                 stcb->asoc.sent_queue_retran_cnt = cnt_mk;
633 #endif
634         }
635 #ifdef SCTP_DEBUG
636         if (sctp_debug_on & SCTP_DEBUG_TIMER3) {
637                 printf("**************************\n");
638         }
639 #endif /* SCTP_DEBUG */
640
641         /* Now check for a ECN Echo that may be stranded */
642         TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
643                 if ((chk->whoTo == net) &&
644                     (chk->rec.chunk_id == SCTP_ECN_ECHO)) {
645                         sctp_free_remote_addr(chk->whoTo);
646                         chk->whoTo = alt;
647                         if (chk->sent != SCTP_DATAGRAM_RESEND) {
648                                 chk->sent = SCTP_DATAGRAM_RESEND;
649                                 stcb->asoc.sent_queue_retran_cnt++;
650                         }
651                         alt->ref_count++;
652                 }
653         }
654         if ((orig_rwnd == 0) && (stcb->asoc.total_flight == 0) &&
655             (orig_flight <= net->mtu)) {
656                 /*
657                  * If the LAST packet sent was not acked and our rwnd is 0
658                  * then we are in a win-probe state.
659                  */
660                 win_probes = 1;
661                 non_win_probes = 0;
662 #ifdef SCTP_DEBUG
663                 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
664                         printf("WIN_PROBE set via o_rwnd=0 tf=0 and all:%d fit in mtu:%d\n",
665                                orig_flight, net->mtu);
666                 }
667 #endif
668         }
669
670         if (audit_tf) {
671 #ifdef SCTP_DEBUG
672                 if (sctp_debug_on & SCTP_DEBUG_TIMER4) {
673                         printf("Audit total flight due to negative value net:%p\n",
674                             net);
675                 }
676 #endif /* SCTP_DEBUG */
677                 stcb->asoc.total_flight = 0;
678                 stcb->asoc.total_flight_count = 0;
679                 /* Clear all networks flight size */
680                 TAILQ_FOREACH(lnets, &stcb->asoc.nets, sctp_next) {
681                         lnets->flight_size = 0;
682 #ifdef SCTP_DEBUG
683                         if (sctp_debug_on & SCTP_DEBUG_TIMER4) {
684                                 printf("Net:%p c-f cwnd:%d ssthresh:%d\n",
685                                     lnets, lnets->cwnd, lnets->ssthresh);
686                         }
687 #endif /* SCTP_DEBUG */
688                 }
689                 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
690                         if (chk->sent < SCTP_DATAGRAM_RESEND) {
691                                 stcb->asoc.total_flight += chk->book_size;
692                                 chk->whoTo->flight_size += chk->book_size;
693                                 stcb->asoc.total_flight_count++;
694                         }
695                 }
696         }
697         /* Setup the ecn nonce re-sync point. We
698          * do this since retranmissions are NOT
699          * setup for ECN. This means that do to
700          * Karn's rule, we don't know the total
701          * of the peers ecn bits.
702          */
703         chk = TAILQ_FIRST(&stcb->asoc.send_queue);
704         if (chk == NULL) {
705                 stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
706         } else {
707                 stcb->asoc.nonce_resync_tsn = chk->rec.data.TSN_seq;
708         }
709         stcb->asoc.nonce_wait_for_ecne = 0;
710         stcb->asoc.nonce_sum_check = 0;
711         /* We return 1 if we only have a window probe outstanding */
712         if (win_probes && (non_win_probes == 0)) {
713                 return (1);
714         }
715         return (0);
716 }
717
718 static void
719 sctp_move_all_chunks_to_alt(struct sctp_tcb *stcb,
720                             struct sctp_nets *net,
721                             struct sctp_nets *alt)
722 {
723         struct sctp_association *asoc;
724         struct sctp_stream_out *outs;
725         struct sctp_tmit_chunk *chk;
726
727         if (net == alt)
728                 /* nothing to do */
729                 return;
730
731         asoc = &stcb->asoc;
732
733         /*
734          * now through all the streams checking for chunks sent to our
735          * bad network.
736          */
737         TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
738                 /* now clean up any chunks here */
739                 TAILQ_FOREACH(chk, &outs->outqueue, sctp_next) {
740                         if (chk->whoTo == net) {
741                                 sctp_free_remote_addr(chk->whoTo);
742                                 chk->whoTo = alt;
743                                 alt->ref_count++;
744                         }
745                 }
746         }
747         /* Now check the pending queue */
748         TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
749                 if (chk->whoTo == net) {
750                         sctp_free_remote_addr(chk->whoTo);
751                         chk->whoTo = alt;
752                         alt->ref_count++;
753                 }
754         }
755
756 }
757
758 int
759 sctp_t3rxt_timer(struct sctp_inpcb *inp,
760                  struct sctp_tcb *stcb,
761                  struct sctp_nets *net)
762 {
763         struct sctp_nets *alt;
764         int win_probe, num_mk;
765
766
767 #ifdef SCTP_FR_LOGGING
768         sctp_log_fr(0, 0, 0, SCTP_FR_T3_TIMEOUT);
769 #endif
770         /* Find an alternate and mark those for retransmission */
771         alt = sctp_find_alternate_net(stcb, net);
772         win_probe = sctp_mark_all_for_resend(stcb, net, alt, &num_mk);
773
774         /* FR Loss recovery just ended with the T3. */
775         stcb->asoc.fast_retran_loss_recovery = 0;
776
777         /* setup the sat loss recovery that prevents
778          * satellite cwnd advance.
779          */
780         stcb->asoc.sat_t3_loss_recovery = 1;
781         stcb->asoc.sat_t3_recovery_tsn = stcb->asoc.sending_seq;
782
783         /* Backoff the timer and cwnd */
784         sctp_backoff_on_timeout(stcb, net, win_probe, num_mk);
785         if (win_probe == 0) {
786                 /* We don't do normal threshold management on window probes */
787                 if (sctp_threshold_management(inp, stcb, net,
788                                               stcb->asoc.max_send_times)) {
789                         /* Association was destroyed */
790                         return (1);
791                 } else {
792                         if (net != stcb->asoc.primary_destination) {
793                                 /* send a immediate HB if our RTO is stale */
794                                 struct  timeval now;
795                                 unsigned int ms_goneby;
796                                 SCTP_GETTIME_TIMEVAL(&now);
797                                 if (net->last_sent_time.tv_sec) {
798                                         ms_goneby = (now.tv_sec - net->last_sent_time.tv_sec) * 1000;
799                                 } else {
800                                         ms_goneby = 0;
801                                 }
802                                 if ((ms_goneby > net->RTO) || (net->RTO == 0)) {
803                                         /* no recent feed back in an RTO or more, request a RTT update */
804                                         sctp_send_hb(stcb, 1, net);
805                                 }
806                         }
807                 }
808         } else {
809                 /*
810                  * For a window probe we don't penalize the net's but only
811                  * the association. This may fail it if SACKs are not coming
812                  * back. If sack's are coming with rwnd locked at 0, we will
813                  * continue to hold things waiting for rwnd to raise
814                  */
815                 if (sctp_threshold_management(inp, stcb, NULL,
816                                               stcb->asoc.max_send_times)) {
817                         /* Association was destroyed */
818                         return (1);
819                 }
820         }
821         if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
822                 /* Move all pending over too */
823                 sctp_move_all_chunks_to_alt(stcb, net, alt);
824                 /* Was it our primary? */
825                 if ((stcb->asoc.primary_destination == net) && (alt != net)) {
826                         /*
827                          * Yes, note it as such and find an alternate
828                          * note: this means HB code must use this to resent
829                          * the primary if it goes active AND if someone does
830                          * a change-primary then this flag must be cleared
831                          * from any net structures.
832                          */
833                         if (sctp_set_primary_addr(stcb,
834                                                  (struct sockaddr *)NULL,
835                                                  alt) == 0) {
836                                 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
837                                 net->src_addr_selected = 0;
838                         }
839                 }
840         }
841         /*
842          * Special case for cookie-echo'ed case, we don't do output
843          * but must await the COOKIE-ACK before retransmission
844          */
845         if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
846                 /*
847                  * Here we just reset the timer and start again since we
848                  * have not established the asoc
849                  */
850 #ifdef SCTP_DEBUG
851                 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
852                         printf("Special cookie case return\n");
853                 }
854 #endif /* SCTP_DEBUG */
855                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
856                 return (0);
857         }
858         if (stcb->asoc.peer_supports_prsctp) {
859                 struct sctp_tmit_chunk *lchk;
860                 lchk = sctp_try_advance_peer_ack_point(stcb, &stcb->asoc);
861                 /* C3. See if we need to send a Fwd-TSN */
862                 if (compare_with_wrap(stcb->asoc.advanced_peer_ack_point,
863                                       stcb->asoc.last_acked_seq, MAX_TSN)) {
864                         /*
865                          * ISSUE with ECN, see FWD-TSN processing for notes
866                          * on issues that will occur when the ECN NONCE stuff
867                          * is put into SCTP for cross checking.
868                          */
869 #ifdef SCTP_DEBUG
870                         if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
871                                 printf("Forward TSN time\n");
872                         }
873 #endif /* SCTP_DEBUG */
874                         send_forward_tsn(stcb, &stcb->asoc);
875                         if (lchk) {
876                                 /* Assure a timer is up */
877                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, lchk->whoTo);
878                         }
879                 }
880         }
881         return (0);
882 }
883
884 int
885 sctp_t1init_timer(struct sctp_inpcb *inp,
886                   struct sctp_tcb *stcb,
887                   struct sctp_nets *net)
888 {
889         /* bump the thresholds */
890         if (stcb->asoc.delayed_connection) {
891                 /* special hook for delayed connection. The
892                  * library did NOT complete the rest of its
893                  * sends.
894                  */
895                 stcb->asoc.delayed_connection = 0;
896                 sctp_send_initiate(inp, stcb);
897                 return (0);
898         }
899         if (sctp_threshold_management(inp, stcb, net,
900                                       stcb->asoc.max_init_times)) {
901                 /* Association was destroyed */
902                 return (1);
903         }
904         stcb->asoc.dropped_special_cnt = 0;
905         sctp_backoff_on_timeout(stcb, stcb->asoc.primary_destination, 1, 0);
906         if (stcb->asoc.initial_init_rto_max < net->RTO) {
907                 net->RTO = stcb->asoc.initial_init_rto_max;
908         }
909         if (stcb->asoc.numnets > 1) {
910                 /* If we have more than one addr use it */
911                 struct sctp_nets *alt;
912                 alt = sctp_find_alternate_net(stcb, stcb->asoc.primary_destination);
913                 if ((alt != NULL) && (alt != stcb->asoc.primary_destination)) {
914                         sctp_move_all_chunks_to_alt(stcb, stcb->asoc.primary_destination, alt);
915                         stcb->asoc.primary_destination = alt;
916                 }
917         }
918         /* Send out a new init */
919         sctp_send_initiate(inp, stcb);
920         return (0);
921 }
922
923 /*
924  * For cookie and asconf we actually need to find and mark for resend,
925  * then increment the resend counter (after all the threshold management
926  * stuff of course).
927  */
928 int  sctp_cookie_timer(struct sctp_inpcb *inp,
929                        struct sctp_tcb *stcb,
930                        struct sctp_nets *net)
931 {
932         struct sctp_nets *alt;
933         struct sctp_tmit_chunk *cookie;
934         /* first before all else we must find the cookie */
935         TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue, sctp_next) {
936                 if (cookie->rec.chunk_id == SCTP_COOKIE_ECHO) {
937                         break;
938                 }
939         }
940         if (cookie == NULL) {
941                 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
942                         /* FOOBAR! */
943                         struct mbuf *oper;
944                         MGET(oper, MB_DONTWAIT, MT_DATA);
945                         if (oper) {
946                                 struct sctp_paramhdr *ph;
947                                 u_int32_t *ippp;
948
949                                 oper->m_len = sizeof(struct sctp_paramhdr) +
950                                     sizeof(*ippp);
951                                 ph = mtod(oper, struct sctp_paramhdr *);
952                                 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
953                                 ph->param_length = htons(oper->m_len);
954                                 ippp = (u_int32_t *)(ph + 1);
955                                 *ippp = htonl(0x40000002);
956                         }
957                         sctp_abort_an_association(inp, stcb, SCTP_INTERNAL_ERROR,
958                             oper);
959                 }
960                 return (1);
961         }
962         /* Ok we found the cookie, threshold management next */
963         if (sctp_threshold_management(inp, stcb, cookie->whoTo,
964             stcb->asoc.max_init_times)) {
965                 /* Assoc is over */
966                 return (1);
967         }
968         /*
969          * cleared theshold management now lets backoff the address &
970          * select an alternate
971          */
972         stcb->asoc.dropped_special_cnt = 0;
973         sctp_backoff_on_timeout(stcb, cookie->whoTo, 1, 0);
974         alt = sctp_find_alternate_net(stcb, cookie->whoTo);
975         if (alt != cookie->whoTo) {
976                 sctp_free_remote_addr(cookie->whoTo);
977                 cookie->whoTo = alt;
978                 alt->ref_count++;
979         }
980         /* Now mark the retran info */
981         if (cookie->sent != SCTP_DATAGRAM_RESEND) {
982                 stcb->asoc.sent_queue_retran_cnt++;
983         }
984         cookie->sent = SCTP_DATAGRAM_RESEND;
985         /*
986          * Now call the output routine to kick out the cookie again, Note we
987          * don't mark any chunks for retran so that FR will need to kick in
988          * to move these (or a send timer).
989          */
990         return (0);
991 }
992
993 int sctp_strreset_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
994     struct sctp_nets *net)
995 {
996         struct sctp_nets *alt;
997         struct sctp_tmit_chunk *strrst, *chk;
998         struct sctp_stream_reset_req *strreq;
999         /* find the existing STRRESET */
1000         TAILQ_FOREACH(strrst, &stcb->asoc.control_send_queue,
1001                       sctp_next) {
1002                 if (strrst->rec.chunk_id == SCTP_STREAM_RESET) {
1003                         /* is it what we want */
1004                         strreq = mtod(strrst->data, struct sctp_stream_reset_req *);
1005                         if (strreq->sr_req.ph.param_type == ntohs(SCTP_STR_RESET_REQUEST)) {
1006                                 break;
1007                         }
1008                 }
1009         }
1010         if (strrst == NULL) {
1011 #ifdef SCTP_DEBUG
1012                 if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
1013                         printf("Strange, strreset timer fires, but I can't find an str-reset?\n");
1014                 }
1015 #endif /* SCTP_DEBUG */
1016                 return (0);
1017         }
1018         /* do threshold management */
1019         if (sctp_threshold_management(inp, stcb, strrst->whoTo,
1020                                       stcb->asoc.max_send_times)) {
1021                 /* Assoc is over */
1022                 return (1);
1023         }
1024
1025         /*
1026          * cleared theshold management
1027          * now lets backoff the address & select an alternate
1028          */
1029         sctp_backoff_on_timeout(stcb, strrst->whoTo, 1, 0);
1030         alt = sctp_find_alternate_net(stcb, strrst->whoTo);
1031         sctp_free_remote_addr(strrst->whoTo);
1032         strrst->whoTo = alt;
1033         alt->ref_count++;
1034
1035         /* See if a ECN Echo is also stranded */
1036         TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1037                 if ((chk->whoTo == net) &&
1038                     (chk->rec.chunk_id == SCTP_ECN_ECHO)) {
1039                         sctp_free_remote_addr(chk->whoTo);
1040                         if (chk->sent != SCTP_DATAGRAM_RESEND) {
1041                                 chk->sent = SCTP_DATAGRAM_RESEND;
1042                                 stcb->asoc.sent_queue_retran_cnt++;
1043                         }
1044                         chk->whoTo = alt;
1045                         alt->ref_count++;
1046                 }
1047         }
1048         if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1049                 /*
1050                  * If the address went un-reachable, we need to move
1051                  * to alternates for ALL chk's in queue
1052                  */
1053                 sctp_move_all_chunks_to_alt(stcb, net, alt);
1054         }
1055         /* mark the retran info */
1056         if (strrst->sent != SCTP_DATAGRAM_RESEND)
1057                 stcb->asoc.sent_queue_retran_cnt++;
1058         strrst->sent = SCTP_DATAGRAM_RESEND;
1059
1060         /* restart the timer */
1061         sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, inp, stcb, strrst->whoTo);
1062         return (0);
1063 }
1064
1065 int sctp_asconf_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1066     struct sctp_nets *net)
1067 {
1068         struct sctp_nets *alt;
1069         struct sctp_tmit_chunk *asconf, *chk;
1070
1071         /* is this the first send, or a retransmission? */
1072         if (stcb->asoc.asconf_sent == 0) {
1073                 /* compose a new ASCONF chunk and send it */
1074                 sctp_send_asconf(stcb, net);
1075         } else {
1076                 /* Retransmission of the existing ASCONF needed... */
1077
1078                 /* find the existing ASCONF */
1079                 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
1080                     sctp_next) {
1081                         if (asconf->rec.chunk_id == SCTP_ASCONF) {
1082                                 break;
1083                         }
1084                 }
1085                 if (asconf == NULL) {
1086 #ifdef SCTP_DEBUG
1087                         if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
1088                                 printf("Strange, asconf timer fires, but I can't find an asconf?\n");
1089                         }
1090 #endif /* SCTP_DEBUG */
1091                         return (0);
1092                 }
1093                 /* do threshold management */
1094                 if (sctp_threshold_management(inp, stcb, asconf->whoTo,
1095                     stcb->asoc.max_send_times)) {
1096                         /* Assoc is over */
1097                         return (1);
1098                 }
1099
1100                 /* PETER? FIX? How will the following code ever run? If
1101                  * the max_send_times is hit, threshold managment will
1102                  * blow away the association?
1103                  */
1104                 if (asconf->snd_count > stcb->asoc.max_send_times) {
1105                         /*
1106                          * Something is rotten, peer is not responding to
1107                          * ASCONFs but maybe is to data etc.  e.g. it is not
1108                          * properly handling the chunk type upper bits
1109                          * Mark this peer as ASCONF incapable and cleanup
1110                          */
1111 #ifdef SCTP_DEBUG
1112                         if (sctp_debug_on & SCTP_DEBUG_TIMER1) {
1113                                 printf("asconf_timer: Peer has not responded to our repeated ASCONFs\n");
1114                         }
1115 #endif /* SCTP_DEBUG */
1116                         sctp_asconf_cleanup(stcb, net);
1117                         return (0);
1118                 }
1119                 /*
1120                  * cleared theshold management
1121                  * now lets backoff the address & select an alternate
1122                  */
1123                 sctp_backoff_on_timeout(stcb, asconf->whoTo, 1, 0);
1124                 alt = sctp_find_alternate_net(stcb, asconf->whoTo);
1125                 sctp_free_remote_addr(asconf->whoTo);
1126                 asconf->whoTo = alt;
1127                 alt->ref_count++;
1128
1129                 /* See if a ECN Echo is also stranded */
1130                 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
1131                         if ((chk->whoTo == net) &&
1132                             (chk->rec.chunk_id == SCTP_ECN_ECHO)) {
1133                                 sctp_free_remote_addr(chk->whoTo);
1134                                 chk->whoTo = alt;
1135                                 if (chk->sent != SCTP_DATAGRAM_RESEND) {
1136                                         chk->sent = SCTP_DATAGRAM_RESEND;
1137                                         stcb->asoc.sent_queue_retran_cnt++;
1138                                 }
1139                                 alt->ref_count++;
1140
1141                         }
1142                 }
1143                 if (net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
1144                         /*
1145                          * If the address went un-reachable, we need to move
1146                          * to alternates for ALL chk's in queue
1147                          */
1148                         sctp_move_all_chunks_to_alt(stcb, net, alt);
1149                 }
1150                 /* mark the retran info */
1151                 if (asconf->sent != SCTP_DATAGRAM_RESEND)
1152                         stcb->asoc.sent_queue_retran_cnt++;
1153                 asconf->sent = SCTP_DATAGRAM_RESEND;
1154         }
1155         return (0);
1156 }
1157
1158 /*
1159  * For the shutdown and shutdown-ack, we do not keep one around on the
1160  * control queue. This means we must generate a new one and call the general
1161  * chunk output routine, AFTER having done threshold
1162  * management.
1163  */
1164 int
1165 sctp_shutdown_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1166     struct sctp_nets *net)
1167 {
1168         struct sctp_nets *alt;
1169         /* first threshold managment */
1170         if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1171                 /* Assoc is over */
1172                 return (1);
1173         }
1174         /* second select an alternative */
1175         alt = sctp_find_alternate_net(stcb, net);
1176
1177         /* third generate a shutdown into the queue for out net */
1178 #ifdef SCTP_DEBUG
1179         if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
1180                 printf("%s:%d sends a shutdown\n",
1181                        __FILE__,
1182                        __LINE__
1183                         );
1184         }
1185 #endif
1186         if (alt) {
1187                 sctp_send_shutdown(stcb, alt);
1188         } else {
1189                 /* if alt is NULL, there is no dest
1190                  * to send to??
1191                  */
1192                 return (0);
1193         }
1194         /* fourth restart timer */
1195         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, alt);
1196         return (0);
1197 }
1198
1199 int sctp_shutdownack_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1200     struct sctp_nets *net)
1201 {
1202         struct sctp_nets *alt;
1203         /* first threshold managment */
1204         if (sctp_threshold_management(inp, stcb, net, stcb->asoc.max_send_times)) {
1205                 /* Assoc is over */
1206                 return (1);
1207         }
1208         /* second select an alternative */
1209         alt = sctp_find_alternate_net(stcb, net);
1210
1211         /* third generate a shutdown into the queue for out net */
1212         sctp_send_shutdown_ack(stcb, alt);
1213
1214         /* fourth restart timer */
1215         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, alt);
1216         return (0);
1217 }
1218
1219 static void
1220 sctp_audit_stream_queues_for_size(struct sctp_inpcb *inp,
1221                                   struct sctp_tcb *stcb)
1222 {
1223         struct sctp_stream_out *outs;
1224         struct sctp_tmit_chunk *chk;
1225         unsigned int chks_in_queue=0;
1226
1227         if ((stcb == NULL) || (inp == NULL))
1228                 return;
1229         if (TAILQ_EMPTY(&stcb->asoc.out_wheel)) {
1230                 printf("Strange, out_wheel empty nothing on sent/send and  tot=%lu?\n",
1231                     (u_long)stcb->asoc.total_output_queue_size);
1232                 stcb->asoc.total_output_queue_size = 0;
1233                 return;
1234         }
1235         if (stcb->asoc.sent_queue_retran_cnt) {
1236                 printf("Hmm, sent_queue_retran_cnt is non-zero %d\n",
1237                     stcb->asoc.sent_queue_retran_cnt);
1238                 stcb->asoc.sent_queue_retran_cnt = 0;
1239         }
1240         /* Check to see if some data queued, if so report it */
1241         TAILQ_FOREACH(outs, &stcb->asoc.out_wheel, next_spoke) {
1242                 if (!TAILQ_EMPTY(&outs->outqueue)) {
1243                         TAILQ_FOREACH(chk, &outs->outqueue, sctp_next) {
1244                                 chks_in_queue++;
1245                         }
1246                 }
1247         }
1248         if (chks_in_queue != stcb->asoc.stream_queue_cnt) {
1249                 printf("Hmm, stream queue cnt at %d I counted %d in stream out wheel\n",
1250                        stcb->asoc.stream_queue_cnt, chks_in_queue);
1251         }
1252         if (chks_in_queue) {
1253                 /* call the output queue function */
1254                 sctp_chunk_output(inp, stcb, 1);
1255                 if ((TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1256                     (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1257                         /* Probably should go in and make it go back through and add fragments allowed */
1258                         printf("Still nothing moved %d chunks are stuck\n", chks_in_queue);
1259                 }
1260         } else {
1261                 printf("Found no chunks on any queue tot:%lu\n",
1262                     (u_long)stcb->asoc.total_output_queue_size);
1263                 stcb->asoc.total_output_queue_size = 0;
1264         }
1265 }
1266
1267 int
1268 sctp_heartbeat_timer(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1269     struct sctp_nets *net)
1270 {
1271         int cnt_of_unconf=0;
1272
1273         if (net) {
1274                 if (net->hb_responded == 0) {
1275                         sctp_backoff_on_timeout(stcb, net, 1, 0);
1276                 }
1277                 /* Zero PBA, if it needs it */
1278                 if (net->partial_bytes_acked) {
1279                         net->partial_bytes_acked = 0;
1280                 }
1281         }
1282         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1283                 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1284                     (net->dest_state & SCTP_ADDR_REACHABLE)) {
1285                         cnt_of_unconf++;
1286                 }
1287         }
1288         if ((stcb->asoc.total_output_queue_size > 0) &&
1289             (TAILQ_EMPTY(&stcb->asoc.send_queue)) &&
1290             (TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
1291                 sctp_audit_stream_queues_for_size(inp, stcb);
1292         }
1293         /* Send a new HB, this will do threshold managment, pick a new dest */
1294         if (sctp_send_hb(stcb, 0, NULL) < 0) {
1295                 return (1);
1296         }
1297         if (cnt_of_unconf > 1) {
1298                 /*
1299                  * this will send out extra hb's up to maxburst if
1300                  * there are any unconfirmed addresses.
1301                  */
1302                 int cnt_sent = 1;
1303                 while ((cnt_sent < stcb->asoc.max_burst) && (cnt_of_unconf > 1)) {
1304                         if (sctp_send_hb(stcb, 0, NULL) == 0)
1305                                 break;
1306                         cnt_of_unconf--;
1307                         cnt_sent++;
1308                 }
1309         }
1310         return (0);
1311 }
1312
1313 #define SCTP_NUMBER_OF_MTU_SIZES 18
1314 static u_int32_t mtu_sizes[]={
1315         68,
1316         296,
1317         508,
1318         512,
1319         544,
1320         576,
1321         1006,
1322         1492,
1323         1500,
1324         1536,
1325         2002,
1326         2048,
1327         4352,
1328         4464,
1329         8166,
1330         17914,
1331         32000,
1332         65535
1333 };
1334
1335
1336 static u_int32_t
1337 sctp_getnext_mtu(struct sctp_inpcb *inp, u_int32_t cur_mtu)
1338 {
1339         /* select another MTU that is just bigger than this one */
1340         int i;
1341
1342         for (i = 0; i < SCTP_NUMBER_OF_MTU_SIZES; i++) {
1343                 if (cur_mtu < mtu_sizes[i]) {
1344                     /* no max_mtu is bigger than this one */
1345                     return (mtu_sizes[i]);
1346                 }
1347         }
1348         /* here return the highest allowable */
1349         return (cur_mtu);
1350 }
1351
1352
1353 void sctp_pathmtu_timer(struct sctp_inpcb *inp,
1354                         struct sctp_tcb *stcb,
1355                         struct sctp_nets *net)
1356 {
1357         u_int32_t next_mtu;
1358
1359         /* restart the timer in any case */
1360         next_mtu = sctp_getnext_mtu(inp, net->mtu);
1361         if (next_mtu <= net->mtu) {
1362             /* nothing to do */
1363             return;
1364         }
1365         if (net->ro.ro_rt != NULL) {
1366                 /* only if we have a route and interface do we
1367                  * set anything. Note we always restart
1368                  * the timer though just in case it is updated
1369                  * (i.e. the ifp) or route/ifp is populated.
1370                  */
1371                 if (net->ro.ro_rt->rt_ifp != NULL) {
1372                         if (net->ro.ro_rt->rt_ifp->if_mtu > next_mtu) {
1373                                 /* ok it will fit out the door */
1374                                 net->mtu = next_mtu;
1375                         }
1376                 }
1377         }
1378         /* restart the timer */
1379         sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
1380 }
1381
1382 void sctp_autoclose_timer(struct sctp_inpcb *inp,
1383                           struct sctp_tcb *stcb,
1384                           struct sctp_nets *net)
1385 {
1386         struct timeval tn, *tim_touse;
1387         struct sctp_association *asoc;
1388         int ticks_gone_by;
1389
1390         SCTP_GETTIME_TIMEVAL(&tn);
1391         if (stcb->asoc.sctp_autoclose_ticks &&
1392             (inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
1393                 /* Auto close is on */
1394                 asoc = &stcb->asoc;
1395                 /* pick the time to use */
1396                 if (asoc->time_last_rcvd.tv_sec >
1397                     asoc->time_last_sent.tv_sec) {
1398                         tim_touse = &asoc->time_last_rcvd;
1399                 } else {
1400                         tim_touse = &asoc->time_last_sent;
1401                 }
1402                 /* Now has long enough transpired to autoclose? */
1403                 ticks_gone_by = ((tn.tv_sec - tim_touse->tv_sec) * hz);
1404                 if ((ticks_gone_by > 0) &&
1405                     (ticks_gone_by >= (int)asoc->sctp_autoclose_ticks)) {
1406                         /*
1407                          * autoclose time has hit, call the output routine,
1408                          * which should do nothing just to be SURE we don't
1409                          * have hanging data. We can then safely check the
1410                          * queues and know that we are clear to send shutdown
1411                          */
1412                         sctp_chunk_output(inp, stcb, 9);
1413                         /* Are we clean? */
1414                         if (TAILQ_EMPTY(&asoc->send_queue) &&
1415                             TAILQ_EMPTY(&asoc->sent_queue)) {
1416                                 /*
1417                                  * there is nothing queued to send,
1418                                  * so I'm done...
1419                                  */
1420                                 if (SCTP_GET_STATE(asoc) !=
1421                                     SCTP_STATE_SHUTDOWN_SENT) {
1422                                         /* only send SHUTDOWN 1st time thru */
1423 #ifdef SCTP_DEBUG
1424                                         if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
1425                                                 printf("%s:%d sends a shutdown\n",
1426                                                        __FILE__,
1427                                                        __LINE__
1428                                                         );
1429                                         }
1430 #endif
1431                                         sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
1432                                         asoc->state = SCTP_STATE_SHUTDOWN_SENT;
1433                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1434                                             stcb->sctp_ep, stcb,
1435                                             asoc->primary_destination);
1436                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1437                                             stcb->sctp_ep, stcb,
1438                                             asoc->primary_destination);
1439                                 }
1440                         }
1441                 } else {
1442                         /*
1443                          * No auto close at this time, reset t-o to
1444                          * check later
1445                          */
1446                         int tmp;
1447                         /* fool the timer startup to use the time left */
1448                         tmp = asoc->sctp_autoclose_ticks;
1449                         asoc->sctp_autoclose_ticks -= ticks_gone_by;
1450                         sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1451                                          net);
1452                         /* restore the real tick value */
1453                         asoc->sctp_autoclose_ticks = tmp;
1454                 }
1455         }
1456 }
1457
1458 void
1459 sctp_iterator_timer(struct sctp_iterator *it)
1460 {
1461         int cnt= 0;
1462         /* only one iterator can run at a
1463          * time. This is the only way we
1464          * can cleanly pull ep's from underneath
1465          * all the running interators when a
1466          * ep is freed.
1467          */
1468         SCTP_ITERATOR_LOCK();
1469         if (it->inp == NULL) {
1470                 /* iterator is complete */
1471         done_with_iterator:
1472                 SCTP_ITERATOR_UNLOCK();
1473                 SCTP_INP_INFO_WLOCK();
1474                 LIST_REMOVE(it, sctp_nxt_itr);
1475                 /* stopping the callout is not needed, in theory,
1476                  * but I am paranoid.
1477                  */
1478                 SCTP_INP_INFO_WUNLOCK();
1479                 callout_stop(&it->tmr.timer);
1480                 if (it->function_atend != NULL) {
1481                         (*it->function_atend)(it->pointer, it->val);
1482                 }
1483                 FREE(it, M_PCB);
1484                 return;
1485         }
1486  select_a_new_ep:
1487         SCTP_INP_WLOCK(it->inp);
1488         while ((it->pcb_flags) && ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) {
1489                 /* we do not like this ep */
1490                 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1491                         SCTP_INP_WUNLOCK(it->inp);
1492                         goto done_with_iterator;
1493                 }
1494                 SCTP_INP_WUNLOCK(it->inp);
1495                 it->inp = LIST_NEXT(it->inp, sctp_list);
1496                 if (it->inp == NULL) {
1497                         goto done_with_iterator;
1498                 }
1499                 SCTP_INP_WLOCK(it->inp);
1500         }
1501         if ((it->inp->inp_starting_point_for_iterator != NULL) &&
1502             (it->inp->inp_starting_point_for_iterator != it)) {
1503                 printf("Iterator collision, we must wait for other iterator at %x\n",
1504                        (u_int)it->inp);
1505                 SCTP_INP_WUNLOCK(it->inp);
1506                 goto start_timer_return;
1507         }
1508         /* now we do the actual write to this guy */
1509         it->inp->inp_starting_point_for_iterator = it;
1510         SCTP_INP_WUNLOCK(it->inp);
1511         SCTP_INP_RLOCK(it->inp);
1512         /* if we reach here we found a inp acceptable, now through each
1513          * one that has the association in the right state
1514          */
1515         if (it->stcb == NULL) {
1516                 it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1517         }
1518         if (it->stcb->asoc.stcb_starting_point_for_iterator == it) {
1519                 it->stcb->asoc.stcb_starting_point_for_iterator = NULL;
1520         }
1521         while (it->stcb) {
1522                 SCTP_TCB_LOCK(it->stcb);
1523                 if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1524                         SCTP_TCB_UNLOCK(it->stcb);
1525                         it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1526                         continue;
1527                 }
1528                 cnt++;
1529                 /* run function on this one */
1530                 SCTP_INP_RUNLOCK(it->inp);
1531                 (*it->function_toapply)(it->inp, it->stcb, it->pointer, it->val);
1532                 sctp_chunk_output(it->inp, it->stcb, 1);
1533                 SCTP_TCB_UNLOCK(it->stcb);
1534                 /* see if we have limited out */
1535                 if (cnt > SCTP_MAX_ITERATOR_AT_ONCE) {
1536                         it->stcb->asoc.stcb_starting_point_for_iterator = it;
1537                 start_timer_return:
1538                         SCTP_ITERATOR_UNLOCK();
1539                         sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR, (struct sctp_inpcb *)it, NULL, NULL);
1540                         return;
1541                 }
1542                 SCTP_INP_RLOCK(it->inp);
1543                 it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1544         }
1545         /* if we reach here, we ran out of stcb's in the inp we are looking at */
1546         SCTP_INP_RUNLOCK(it->inp);
1547         SCTP_INP_WLOCK(it->inp);
1548         it->inp->inp_starting_point_for_iterator = NULL;
1549         SCTP_INP_WUNLOCK(it->inp);
1550         if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1551                 it->inp = NULL;
1552         } else {
1553                 SCTP_INP_INFO_RLOCK();
1554                 it->inp = LIST_NEXT(it->inp, sctp_list);
1555                 SCTP_INP_INFO_RUNLOCK();
1556         }
1557         if (it->inp == NULL) {
1558                 goto done_with_iterator;
1559         }
1560         goto select_a_new_ep;
1561 }