xref: /freebsd/sys/netinet/sctp_output.c (revision 57c4583f70ab9d25b3aed17f20ec7843f9673539)
1 /*-
2  * Copyright (c) 2001-2006, Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $	 */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_ipsec.h"
37 #include "opt_compat.h"
38 #include "opt_inet6.h"
39 #include "opt_inet.h"
40 #include "opt_sctp.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/domain.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/proc.h>
50 #include <sys/kernel.h>
51 #include <sys/sysctl.h>
52 #include <sys/resourcevar.h>
53 #include <sys/uio.h>
54 #ifdef INET6
55 #include <sys/domain.h>
56 #endif
57 
58 #include <sys/limits.h>
59 #include <machine/cpu.h>
60 
61 #include <net/if.h>
62 #include <net/if_types.h>
63 
64 #include <net/if_var.h>
65 
66 #include <net/route.h>
67 
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip.h>
71 #include <netinet/in_pcb.h>
72 #include <netinet/in_var.h>
73 #include <netinet/ip_var.h>
74 
75 #ifdef INET6
76 #include <netinet/ip6.h>
77 #include <netinet6/ip6_var.h>
78 #include <netinet6/scope6_var.h>
79 #include <netinet6/nd6.h>
80 
81 #include <netinet6/in6_pcb.h>
82 
83 #include <netinet/icmp6.h>
84 
85 #endif				/* INET6 */
86 
87 
88 
89 #ifndef in6pcb
90 #define in6pcb		inpcb
91 #endif
92 
93 #ifdef IPSEC
94 #include <netinet6/ipsec.h>
95 #include <netkey/key.h>
96 #endif				/* IPSEC */
97 
98 #include <netinet/sctp_os.h>
99 #include <netinet/sctp_var.h>
100 #include <netinet/sctp_header.h>
101 #include <netinet/sctp_pcb.h>
102 #include <netinet/sctputil.h>
103 #include <netinet/sctp_output.h>
104 #include <netinet/sctp_uio.h>
105 #include <netinet/sctputil.h>
106 #include <netinet/sctp_auth.h>
107 #include <netinet/sctp_timer.h>
108 #include <netinet/sctp_asconf.h>
109 #include <netinet/sctp_indata.h>
110 #include <netinet/sctp_bsd_addr.h>
111 
112 #ifdef SCTP_DEBUG
113 extern uint32_t sctp_debug_on;
114 
115 #endif
116 
117 
118 
119 #define SCTP_MAX_GAPS_INARRAY 4
120 struct sack_track {
121 	uint8_t right_edge;	/* mergable on the right edge */
122 	uint8_t left_edge;	/* mergable on the left edge */
123 	uint8_t num_entries;
124 	uint8_t spare;
125 	struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
126 };
127 
128 struct sack_track sack_array[256] = {
129 	{0, 0, 0, 0,		/* 0x00 */
130 		{{0, 0},
131 		{0, 0},
132 		{0, 0},
133 		{0, 0}
134 		}
135 	},
136 	{1, 0, 1, 0,		/* 0x01 */
137 		{{0, 0},
138 		{0, 0},
139 		{0, 0},
140 		{0, 0}
141 		}
142 	},
143 	{0, 0, 1, 0,		/* 0x02 */
144 		{{1, 1},
145 		{0, 0},
146 		{0, 0},
147 		{0, 0}
148 		}
149 	},
150 	{1, 0, 1, 0,		/* 0x03 */
151 		{{0, 1},
152 		{0, 0},
153 		{0, 0},
154 		{0, 0}
155 		}
156 	},
157 	{0, 0, 1, 0,		/* 0x04 */
158 		{{2, 2},
159 		{0, 0},
160 		{0, 0},
161 		{0, 0}
162 		}
163 	},
164 	{1, 0, 2, 0,		/* 0x05 */
165 		{{0, 0},
166 		{2, 2},
167 		{0, 0},
168 		{0, 0}
169 		}
170 	},
171 	{0, 0, 1, 0,		/* 0x06 */
172 		{{1, 2},
173 		{0, 0},
174 		{0, 0},
175 		{0, 0}
176 		}
177 	},
178 	{1, 0, 1, 0,		/* 0x07 */
179 		{{0, 2},
180 		{0, 0},
181 		{0, 0},
182 		{0, 0}
183 		}
184 	},
185 	{0, 0, 1, 0,		/* 0x08 */
186 		{{3, 3},
187 		{0, 0},
188 		{0, 0},
189 		{0, 0}
190 		}
191 	},
192 	{1, 0, 2, 0,		/* 0x09 */
193 		{{0, 0},
194 		{3, 3},
195 		{0, 0},
196 		{0, 0}
197 		}
198 	},
199 	{0, 0, 2, 0,		/* 0x0a */
200 		{{1, 1},
201 		{3, 3},
202 		{0, 0},
203 		{0, 0}
204 		}
205 	},
206 	{1, 0, 2, 0,		/* 0x0b */
207 		{{0, 1},
208 		{3, 3},
209 		{0, 0},
210 		{0, 0}
211 		}
212 	},
213 	{0, 0, 1, 0,		/* 0x0c */
214 		{{2, 3},
215 		{0, 0},
216 		{0, 0},
217 		{0, 0}
218 		}
219 	},
220 	{1, 0, 2, 0,		/* 0x0d */
221 		{{0, 0},
222 		{2, 3},
223 		{0, 0},
224 		{0, 0}
225 		}
226 	},
227 	{0, 0, 1, 0,		/* 0x0e */
228 		{{1, 3},
229 		{0, 0},
230 		{0, 0},
231 		{0, 0}
232 		}
233 	},
234 	{1, 0, 1, 0,		/* 0x0f */
235 		{{0, 3},
236 		{0, 0},
237 		{0, 0},
238 		{0, 0}
239 		}
240 	},
241 	{0, 0, 1, 0,		/* 0x10 */
242 		{{4, 4},
243 		{0, 0},
244 		{0, 0},
245 		{0, 0}
246 		}
247 	},
248 	{1, 0, 2, 0,		/* 0x11 */
249 		{{0, 0},
250 		{4, 4},
251 		{0, 0},
252 		{0, 0}
253 		}
254 	},
255 	{0, 0, 2, 0,		/* 0x12 */
256 		{{1, 1},
257 		{4, 4},
258 		{0, 0},
259 		{0, 0}
260 		}
261 	},
262 	{1, 0, 2, 0,		/* 0x13 */
263 		{{0, 1},
264 		{4, 4},
265 		{0, 0},
266 		{0, 0}
267 		}
268 	},
269 	{0, 0, 2, 0,		/* 0x14 */
270 		{{2, 2},
271 		{4, 4},
272 		{0, 0},
273 		{0, 0}
274 		}
275 	},
276 	{1, 0, 3, 0,		/* 0x15 */
277 		{{0, 0},
278 		{2, 2},
279 		{4, 4},
280 		{0, 0}
281 		}
282 	},
283 	{0, 0, 2, 0,		/* 0x16 */
284 		{{1, 2},
285 		{4, 4},
286 		{0, 0},
287 		{0, 0}
288 		}
289 	},
290 	{1, 0, 2, 0,		/* 0x17 */
291 		{{0, 2},
292 		{4, 4},
293 		{0, 0},
294 		{0, 0}
295 		}
296 	},
297 	{0, 0, 1, 0,		/* 0x18 */
298 		{{3, 4},
299 		{0, 0},
300 		{0, 0},
301 		{0, 0}
302 		}
303 	},
304 	{1, 0, 2, 0,		/* 0x19 */
305 		{{0, 0},
306 		{3, 4},
307 		{0, 0},
308 		{0, 0}
309 		}
310 	},
311 	{0, 0, 2, 0,		/* 0x1a */
312 		{{1, 1},
313 		{3, 4},
314 		{0, 0},
315 		{0, 0}
316 		}
317 	},
318 	{1, 0, 2, 0,		/* 0x1b */
319 		{{0, 1},
320 		{3, 4},
321 		{0, 0},
322 		{0, 0}
323 		}
324 	},
325 	{0, 0, 1, 0,		/* 0x1c */
326 		{{2, 4},
327 		{0, 0},
328 		{0, 0},
329 		{0, 0}
330 		}
331 	},
332 	{1, 0, 2, 0,		/* 0x1d */
333 		{{0, 0},
334 		{2, 4},
335 		{0, 0},
336 		{0, 0}
337 		}
338 	},
339 	{0, 0, 1, 0,		/* 0x1e */
340 		{{1, 4},
341 		{0, 0},
342 		{0, 0},
343 		{0, 0}
344 		}
345 	},
346 	{1, 0, 1, 0,		/* 0x1f */
347 		{{0, 4},
348 		{0, 0},
349 		{0, 0},
350 		{0, 0}
351 		}
352 	},
353 	{0, 0, 1, 0,		/* 0x20 */
354 		{{5, 5},
355 		{0, 0},
356 		{0, 0},
357 		{0, 0}
358 		}
359 	},
360 	{1, 0, 2, 0,		/* 0x21 */
361 		{{0, 0},
362 		{5, 5},
363 		{0, 0},
364 		{0, 0}
365 		}
366 	},
367 	{0, 0, 2, 0,		/* 0x22 */
368 		{{1, 1},
369 		{5, 5},
370 		{0, 0},
371 		{0, 0}
372 		}
373 	},
374 	{1, 0, 2, 0,		/* 0x23 */
375 		{{0, 1},
376 		{5, 5},
377 		{0, 0},
378 		{0, 0}
379 		}
380 	},
381 	{0, 0, 2, 0,		/* 0x24 */
382 		{{2, 2},
383 		{5, 5},
384 		{0, 0},
385 		{0, 0}
386 		}
387 	},
388 	{1, 0, 3, 0,		/* 0x25 */
389 		{{0, 0},
390 		{2, 2},
391 		{5, 5},
392 		{0, 0}
393 		}
394 	},
395 	{0, 0, 2, 0,		/* 0x26 */
396 		{{1, 2},
397 		{5, 5},
398 		{0, 0},
399 		{0, 0}
400 		}
401 	},
402 	{1, 0, 2, 0,		/* 0x27 */
403 		{{0, 2},
404 		{5, 5},
405 		{0, 0},
406 		{0, 0}
407 		}
408 	},
409 	{0, 0, 2, 0,		/* 0x28 */
410 		{{3, 3},
411 		{5, 5},
412 		{0, 0},
413 		{0, 0}
414 		}
415 	},
416 	{1, 0, 3, 0,		/* 0x29 */
417 		{{0, 0},
418 		{3, 3},
419 		{5, 5},
420 		{0, 0}
421 		}
422 	},
423 	{0, 0, 3, 0,		/* 0x2a */
424 		{{1, 1},
425 		{3, 3},
426 		{5, 5},
427 		{0, 0}
428 		}
429 	},
430 	{1, 0, 3, 0,		/* 0x2b */
431 		{{0, 1},
432 		{3, 3},
433 		{5, 5},
434 		{0, 0}
435 		}
436 	},
437 	{0, 0, 2, 0,		/* 0x2c */
438 		{{2, 3},
439 		{5, 5},
440 		{0, 0},
441 		{0, 0}
442 		}
443 	},
444 	{1, 0, 3, 0,		/* 0x2d */
445 		{{0, 0},
446 		{2, 3},
447 		{5, 5},
448 		{0, 0}
449 		}
450 	},
451 	{0, 0, 2, 0,		/* 0x2e */
452 		{{1, 3},
453 		{5, 5},
454 		{0, 0},
455 		{0, 0}
456 		}
457 	},
458 	{1, 0, 2, 0,		/* 0x2f */
459 		{{0, 3},
460 		{5, 5},
461 		{0, 0},
462 		{0, 0}
463 		}
464 	},
465 	{0, 0, 1, 0,		/* 0x30 */
466 		{{4, 5},
467 		{0, 0},
468 		{0, 0},
469 		{0, 0}
470 		}
471 	},
472 	{1, 0, 2, 0,		/* 0x31 */
473 		{{0, 0},
474 		{4, 5},
475 		{0, 0},
476 		{0, 0}
477 		}
478 	},
479 	{0, 0, 2, 0,		/* 0x32 */
480 		{{1, 1},
481 		{4, 5},
482 		{0, 0},
483 		{0, 0}
484 		}
485 	},
486 	{1, 0, 2, 0,		/* 0x33 */
487 		{{0, 1},
488 		{4, 5},
489 		{0, 0},
490 		{0, 0}
491 		}
492 	},
493 	{0, 0, 2, 0,		/* 0x34 */
494 		{{2, 2},
495 		{4, 5},
496 		{0, 0},
497 		{0, 0}
498 		}
499 	},
500 	{1, 0, 3, 0,		/* 0x35 */
501 		{{0, 0},
502 		{2, 2},
503 		{4, 5},
504 		{0, 0}
505 		}
506 	},
507 	{0, 0, 2, 0,		/* 0x36 */
508 		{{1, 2},
509 		{4, 5},
510 		{0, 0},
511 		{0, 0}
512 		}
513 	},
514 	{1, 0, 2, 0,		/* 0x37 */
515 		{{0, 2},
516 		{4, 5},
517 		{0, 0},
518 		{0, 0}
519 		}
520 	},
521 	{0, 0, 1, 0,		/* 0x38 */
522 		{{3, 5},
523 		{0, 0},
524 		{0, 0},
525 		{0, 0}
526 		}
527 	},
528 	{1, 0, 2, 0,		/* 0x39 */
529 		{{0, 0},
530 		{3, 5},
531 		{0, 0},
532 		{0, 0}
533 		}
534 	},
535 	{0, 0, 2, 0,		/* 0x3a */
536 		{{1, 1},
537 		{3, 5},
538 		{0, 0},
539 		{0, 0}
540 		}
541 	},
542 	{1, 0, 2, 0,		/* 0x3b */
543 		{{0, 1},
544 		{3, 5},
545 		{0, 0},
546 		{0, 0}
547 		}
548 	},
549 	{0, 0, 1, 0,		/* 0x3c */
550 		{{2, 5},
551 		{0, 0},
552 		{0, 0},
553 		{0, 0}
554 		}
555 	},
556 	{1, 0, 2, 0,		/* 0x3d */
557 		{{0, 0},
558 		{2, 5},
559 		{0, 0},
560 		{0, 0}
561 		}
562 	},
563 	{0, 0, 1, 0,		/* 0x3e */
564 		{{1, 5},
565 		{0, 0},
566 		{0, 0},
567 		{0, 0}
568 		}
569 	},
570 	{1, 0, 1, 0,		/* 0x3f */
571 		{{0, 5},
572 		{0, 0},
573 		{0, 0},
574 		{0, 0}
575 		}
576 	},
577 	{0, 0, 1, 0,		/* 0x40 */
578 		{{6, 6},
579 		{0, 0},
580 		{0, 0},
581 		{0, 0}
582 		}
583 	},
584 	{1, 0, 2, 0,		/* 0x41 */
585 		{{0, 0},
586 		{6, 6},
587 		{0, 0},
588 		{0, 0}
589 		}
590 	},
591 	{0, 0, 2, 0,		/* 0x42 */
592 		{{1, 1},
593 		{6, 6},
594 		{0, 0},
595 		{0, 0}
596 		}
597 	},
598 	{1, 0, 2, 0,		/* 0x43 */
599 		{{0, 1},
600 		{6, 6},
601 		{0, 0},
602 		{0, 0}
603 		}
604 	},
605 	{0, 0, 2, 0,		/* 0x44 */
606 		{{2, 2},
607 		{6, 6},
608 		{0, 0},
609 		{0, 0}
610 		}
611 	},
612 	{1, 0, 3, 0,		/* 0x45 */
613 		{{0, 0},
614 		{2, 2},
615 		{6, 6},
616 		{0, 0}
617 		}
618 	},
619 	{0, 0, 2, 0,		/* 0x46 */
620 		{{1, 2},
621 		{6, 6},
622 		{0, 0},
623 		{0, 0}
624 		}
625 	},
626 	{1, 0, 2, 0,		/* 0x47 */
627 		{{0, 2},
628 		{6, 6},
629 		{0, 0},
630 		{0, 0}
631 		}
632 	},
633 	{0, 0, 2, 0,		/* 0x48 */
634 		{{3, 3},
635 		{6, 6},
636 		{0, 0},
637 		{0, 0}
638 		}
639 	},
640 	{1, 0, 3, 0,		/* 0x49 */
641 		{{0, 0},
642 		{3, 3},
643 		{6, 6},
644 		{0, 0}
645 		}
646 	},
647 	{0, 0, 3, 0,		/* 0x4a */
648 		{{1, 1},
649 		{3, 3},
650 		{6, 6},
651 		{0, 0}
652 		}
653 	},
654 	{1, 0, 3, 0,		/* 0x4b */
655 		{{0, 1},
656 		{3, 3},
657 		{6, 6},
658 		{0, 0}
659 		}
660 	},
661 	{0, 0, 2, 0,		/* 0x4c */
662 		{{2, 3},
663 		{6, 6},
664 		{0, 0},
665 		{0, 0}
666 		}
667 	},
668 	{1, 0, 3, 0,		/* 0x4d */
669 		{{0, 0},
670 		{2, 3},
671 		{6, 6},
672 		{0, 0}
673 		}
674 	},
675 	{0, 0, 2, 0,		/* 0x4e */
676 		{{1, 3},
677 		{6, 6},
678 		{0, 0},
679 		{0, 0}
680 		}
681 	},
682 	{1, 0, 2, 0,		/* 0x4f */
683 		{{0, 3},
684 		{6, 6},
685 		{0, 0},
686 		{0, 0}
687 		}
688 	},
689 	{0, 0, 2, 0,		/* 0x50 */
690 		{{4, 4},
691 		{6, 6},
692 		{0, 0},
693 		{0, 0}
694 		}
695 	},
696 	{1, 0, 3, 0,		/* 0x51 */
697 		{{0, 0},
698 		{4, 4},
699 		{6, 6},
700 		{0, 0}
701 		}
702 	},
703 	{0, 0, 3, 0,		/* 0x52 */
704 		{{1, 1},
705 		{4, 4},
706 		{6, 6},
707 		{0, 0}
708 		}
709 	},
710 	{1, 0, 3, 0,		/* 0x53 */
711 		{{0, 1},
712 		{4, 4},
713 		{6, 6},
714 		{0, 0}
715 		}
716 	},
717 	{0, 0, 3, 0,		/* 0x54 */
718 		{{2, 2},
719 		{4, 4},
720 		{6, 6},
721 		{0, 0}
722 		}
723 	},
724 	{1, 0, 4, 0,		/* 0x55 */
725 		{{0, 0},
726 		{2, 2},
727 		{4, 4},
728 		{6, 6}
729 		}
730 	},
731 	{0, 0, 3, 0,		/* 0x56 */
732 		{{1, 2},
733 		{4, 4},
734 		{6, 6},
735 		{0, 0}
736 		}
737 	},
738 	{1, 0, 3, 0,		/* 0x57 */
739 		{{0, 2},
740 		{4, 4},
741 		{6, 6},
742 		{0, 0}
743 		}
744 	},
745 	{0, 0, 2, 0,		/* 0x58 */
746 		{{3, 4},
747 		{6, 6},
748 		{0, 0},
749 		{0, 0}
750 		}
751 	},
752 	{1, 0, 3, 0,		/* 0x59 */
753 		{{0, 0},
754 		{3, 4},
755 		{6, 6},
756 		{0, 0}
757 		}
758 	},
759 	{0, 0, 3, 0,		/* 0x5a */
760 		{{1, 1},
761 		{3, 4},
762 		{6, 6},
763 		{0, 0}
764 		}
765 	},
766 	{1, 0, 3, 0,		/* 0x5b */
767 		{{0, 1},
768 		{3, 4},
769 		{6, 6},
770 		{0, 0}
771 		}
772 	},
773 	{0, 0, 2, 0,		/* 0x5c */
774 		{{2, 4},
775 		{6, 6},
776 		{0, 0},
777 		{0, 0}
778 		}
779 	},
780 	{1, 0, 3, 0,		/* 0x5d */
781 		{{0, 0},
782 		{2, 4},
783 		{6, 6},
784 		{0, 0}
785 		}
786 	},
787 	{0, 0, 2, 0,		/* 0x5e */
788 		{{1, 4},
789 		{6, 6},
790 		{0, 0},
791 		{0, 0}
792 		}
793 	},
794 	{1, 0, 2, 0,		/* 0x5f */
795 		{{0, 4},
796 		{6, 6},
797 		{0, 0},
798 		{0, 0}
799 		}
800 	},
801 	{0, 0, 1, 0,		/* 0x60 */
802 		{{5, 6},
803 		{0, 0},
804 		{0, 0},
805 		{0, 0}
806 		}
807 	},
808 	{1, 0, 2, 0,		/* 0x61 */
809 		{{0, 0},
810 		{5, 6},
811 		{0, 0},
812 		{0, 0}
813 		}
814 	},
815 	{0, 0, 2, 0,		/* 0x62 */
816 		{{1, 1},
817 		{5, 6},
818 		{0, 0},
819 		{0, 0}
820 		}
821 	},
822 	{1, 0, 2, 0,		/* 0x63 */
823 		{{0, 1},
824 		{5, 6},
825 		{0, 0},
826 		{0, 0}
827 		}
828 	},
829 	{0, 0, 2, 0,		/* 0x64 */
830 		{{2, 2},
831 		{5, 6},
832 		{0, 0},
833 		{0, 0}
834 		}
835 	},
836 	{1, 0, 3, 0,		/* 0x65 */
837 		{{0, 0},
838 		{2, 2},
839 		{5, 6},
840 		{0, 0}
841 		}
842 	},
843 	{0, 0, 2, 0,		/* 0x66 */
844 		{{1, 2},
845 		{5, 6},
846 		{0, 0},
847 		{0, 0}
848 		}
849 	},
850 	{1, 0, 2, 0,		/* 0x67 */
851 		{{0, 2},
852 		{5, 6},
853 		{0, 0},
854 		{0, 0}
855 		}
856 	},
857 	{0, 0, 2, 0,		/* 0x68 */
858 		{{3, 3},
859 		{5, 6},
860 		{0, 0},
861 		{0, 0}
862 		}
863 	},
864 	{1, 0, 3, 0,		/* 0x69 */
865 		{{0, 0},
866 		{3, 3},
867 		{5, 6},
868 		{0, 0}
869 		}
870 	},
871 	{0, 0, 3, 0,		/* 0x6a */
872 		{{1, 1},
873 		{3, 3},
874 		{5, 6},
875 		{0, 0}
876 		}
877 	},
878 	{1, 0, 3, 0,		/* 0x6b */
879 		{{0, 1},
880 		{3, 3},
881 		{5, 6},
882 		{0, 0}
883 		}
884 	},
885 	{0, 0, 2, 0,		/* 0x6c */
886 		{{2, 3},
887 		{5, 6},
888 		{0, 0},
889 		{0, 0}
890 		}
891 	},
892 	{1, 0, 3, 0,		/* 0x6d */
893 		{{0, 0},
894 		{2, 3},
895 		{5, 6},
896 		{0, 0}
897 		}
898 	},
899 	{0, 0, 2, 0,		/* 0x6e */
900 		{{1, 3},
901 		{5, 6},
902 		{0, 0},
903 		{0, 0}
904 		}
905 	},
906 	{1, 0, 2, 0,		/* 0x6f */
907 		{{0, 3},
908 		{5, 6},
909 		{0, 0},
910 		{0, 0}
911 		}
912 	},
913 	{0, 0, 1, 0,		/* 0x70 */
914 		{{4, 6},
915 		{0, 0},
916 		{0, 0},
917 		{0, 0}
918 		}
919 	},
920 	{1, 0, 2, 0,		/* 0x71 */
921 		{{0, 0},
922 		{4, 6},
923 		{0, 0},
924 		{0, 0}
925 		}
926 	},
927 	{0, 0, 2, 0,		/* 0x72 */
928 		{{1, 1},
929 		{4, 6},
930 		{0, 0},
931 		{0, 0}
932 		}
933 	},
934 	{1, 0, 2, 0,		/* 0x73 */
935 		{{0, 1},
936 		{4, 6},
937 		{0, 0},
938 		{0, 0}
939 		}
940 	},
941 	{0, 0, 2, 0,		/* 0x74 */
942 		{{2, 2},
943 		{4, 6},
944 		{0, 0},
945 		{0, 0}
946 		}
947 	},
948 	{1, 0, 3, 0,		/* 0x75 */
949 		{{0, 0},
950 		{2, 2},
951 		{4, 6},
952 		{0, 0}
953 		}
954 	},
955 	{0, 0, 2, 0,		/* 0x76 */
956 		{{1, 2},
957 		{4, 6},
958 		{0, 0},
959 		{0, 0}
960 		}
961 	},
962 	{1, 0, 2, 0,		/* 0x77 */
963 		{{0, 2},
964 		{4, 6},
965 		{0, 0},
966 		{0, 0}
967 		}
968 	},
969 	{0, 0, 1, 0,		/* 0x78 */
970 		{{3, 6},
971 		{0, 0},
972 		{0, 0},
973 		{0, 0}
974 		}
975 	},
976 	{1, 0, 2, 0,		/* 0x79 */
977 		{{0, 0},
978 		{3, 6},
979 		{0, 0},
980 		{0, 0}
981 		}
982 	},
983 	{0, 0, 2, 0,		/* 0x7a */
984 		{{1, 1},
985 		{3, 6},
986 		{0, 0},
987 		{0, 0}
988 		}
989 	},
990 	{1, 0, 2, 0,		/* 0x7b */
991 		{{0, 1},
992 		{3, 6},
993 		{0, 0},
994 		{0, 0}
995 		}
996 	},
997 	{0, 0, 1, 0,		/* 0x7c */
998 		{{2, 6},
999 		{0, 0},
1000 		{0, 0},
1001 		{0, 0}
1002 		}
1003 	},
1004 	{1, 0, 2, 0,		/* 0x7d */
1005 		{{0, 0},
1006 		{2, 6},
1007 		{0, 0},
1008 		{0, 0}
1009 		}
1010 	},
1011 	{0, 0, 1, 0,		/* 0x7e */
1012 		{{1, 6},
1013 		{0, 0},
1014 		{0, 0},
1015 		{0, 0}
1016 		}
1017 	},
1018 	{1, 0, 1, 0,		/* 0x7f */
1019 		{{0, 6},
1020 		{0, 0},
1021 		{0, 0},
1022 		{0, 0}
1023 		}
1024 	},
1025 	{0, 1, 1, 0,		/* 0x80 */
1026 		{{7, 7},
1027 		{0, 0},
1028 		{0, 0},
1029 		{0, 0}
1030 		}
1031 	},
1032 	{1, 1, 2, 0,		/* 0x81 */
1033 		{{0, 0},
1034 		{7, 7},
1035 		{0, 0},
1036 		{0, 0}
1037 		}
1038 	},
1039 	{0, 1, 2, 0,		/* 0x82 */
1040 		{{1, 1},
1041 		{7, 7},
1042 		{0, 0},
1043 		{0, 0}
1044 		}
1045 	},
1046 	{1, 1, 2, 0,		/* 0x83 */
1047 		{{0, 1},
1048 		{7, 7},
1049 		{0, 0},
1050 		{0, 0}
1051 		}
1052 	},
1053 	{0, 1, 2, 0,		/* 0x84 */
1054 		{{2, 2},
1055 		{7, 7},
1056 		{0, 0},
1057 		{0, 0}
1058 		}
1059 	},
1060 	{1, 1, 3, 0,		/* 0x85 */
1061 		{{0, 0},
1062 		{2, 2},
1063 		{7, 7},
1064 		{0, 0}
1065 		}
1066 	},
1067 	{0, 1, 2, 0,		/* 0x86 */
1068 		{{1, 2},
1069 		{7, 7},
1070 		{0, 0},
1071 		{0, 0}
1072 		}
1073 	},
1074 	{1, 1, 2, 0,		/* 0x87 */
1075 		{{0, 2},
1076 		{7, 7},
1077 		{0, 0},
1078 		{0, 0}
1079 		}
1080 	},
1081 	{0, 1, 2, 0,		/* 0x88 */
1082 		{{3, 3},
1083 		{7, 7},
1084 		{0, 0},
1085 		{0, 0}
1086 		}
1087 	},
1088 	{1, 1, 3, 0,		/* 0x89 */
1089 		{{0, 0},
1090 		{3, 3},
1091 		{7, 7},
1092 		{0, 0}
1093 		}
1094 	},
1095 	{0, 1, 3, 0,		/* 0x8a */
1096 		{{1, 1},
1097 		{3, 3},
1098 		{7, 7},
1099 		{0, 0}
1100 		}
1101 	},
1102 	{1, 1, 3, 0,		/* 0x8b */
1103 		{{0, 1},
1104 		{3, 3},
1105 		{7, 7},
1106 		{0, 0}
1107 		}
1108 	},
1109 	{0, 1, 2, 0,		/* 0x8c */
1110 		{{2, 3},
1111 		{7, 7},
1112 		{0, 0},
1113 		{0, 0}
1114 		}
1115 	},
1116 	{1, 1, 3, 0,		/* 0x8d */
1117 		{{0, 0},
1118 		{2, 3},
1119 		{7, 7},
1120 		{0, 0}
1121 		}
1122 	},
1123 	{0, 1, 2, 0,		/* 0x8e */
1124 		{{1, 3},
1125 		{7, 7},
1126 		{0, 0},
1127 		{0, 0}
1128 		}
1129 	},
1130 	{1, 1, 2, 0,		/* 0x8f */
1131 		{{0, 3},
1132 		{7, 7},
1133 		{0, 0},
1134 		{0, 0}
1135 		}
1136 	},
1137 	{0, 1, 2, 0,		/* 0x90 */
1138 		{{4, 4},
1139 		{7, 7},
1140 		{0, 0},
1141 		{0, 0}
1142 		}
1143 	},
1144 	{1, 1, 3, 0,		/* 0x91 */
1145 		{{0, 0},
1146 		{4, 4},
1147 		{7, 7},
1148 		{0, 0}
1149 		}
1150 	},
1151 	{0, 1, 3, 0,		/* 0x92 */
1152 		{{1, 1},
1153 		{4, 4},
1154 		{7, 7},
1155 		{0, 0}
1156 		}
1157 	},
1158 	{1, 1, 3, 0,		/* 0x93 */
1159 		{{0, 1},
1160 		{4, 4},
1161 		{7, 7},
1162 		{0, 0}
1163 		}
1164 	},
1165 	{0, 1, 3, 0,		/* 0x94 */
1166 		{{2, 2},
1167 		{4, 4},
1168 		{7, 7},
1169 		{0, 0}
1170 		}
1171 	},
1172 	{1, 1, 4, 0,		/* 0x95 */
1173 		{{0, 0},
1174 		{2, 2},
1175 		{4, 4},
1176 		{7, 7}
1177 		}
1178 	},
1179 	{0, 1, 3, 0,		/* 0x96 */
1180 		{{1, 2},
1181 		{4, 4},
1182 		{7, 7},
1183 		{0, 0}
1184 		}
1185 	},
1186 	{1, 1, 3, 0,		/* 0x97 */
1187 		{{0, 2},
1188 		{4, 4},
1189 		{7, 7},
1190 		{0, 0}
1191 		}
1192 	},
1193 	{0, 1, 2, 0,		/* 0x98 */
1194 		{{3, 4},
1195 		{7, 7},
1196 		{0, 0},
1197 		{0, 0}
1198 		}
1199 	},
1200 	{1, 1, 3, 0,		/* 0x99 */
1201 		{{0, 0},
1202 		{3, 4},
1203 		{7, 7},
1204 		{0, 0}
1205 		}
1206 	},
1207 	{0, 1, 3, 0,		/* 0x9a */
1208 		{{1, 1},
1209 		{3, 4},
1210 		{7, 7},
1211 		{0, 0}
1212 		}
1213 	},
1214 	{1, 1, 3, 0,		/* 0x9b */
1215 		{{0, 1},
1216 		{3, 4},
1217 		{7, 7},
1218 		{0, 0}
1219 		}
1220 	},
1221 	{0, 1, 2, 0,		/* 0x9c */
1222 		{{2, 4},
1223 		{7, 7},
1224 		{0, 0},
1225 		{0, 0}
1226 		}
1227 	},
1228 	{1, 1, 3, 0,		/* 0x9d */
1229 		{{0, 0},
1230 		{2, 4},
1231 		{7, 7},
1232 		{0, 0}
1233 		}
1234 	},
1235 	{0, 1, 2, 0,		/* 0x9e */
1236 		{{1, 4},
1237 		{7, 7},
1238 		{0, 0},
1239 		{0, 0}
1240 		}
1241 	},
1242 	{1, 1, 2, 0,		/* 0x9f */
1243 		{{0, 4},
1244 		{7, 7},
1245 		{0, 0},
1246 		{0, 0}
1247 		}
1248 	},
1249 	{0, 1, 2, 0,		/* 0xa0 */
1250 		{{5, 5},
1251 		{7, 7},
1252 		{0, 0},
1253 		{0, 0}
1254 		}
1255 	},
1256 	{1, 1, 3, 0,		/* 0xa1 */
1257 		{{0, 0},
1258 		{5, 5},
1259 		{7, 7},
1260 		{0, 0}
1261 		}
1262 	},
1263 	{0, 1, 3, 0,		/* 0xa2 */
1264 		{{1, 1},
1265 		{5, 5},
1266 		{7, 7},
1267 		{0, 0}
1268 		}
1269 	},
1270 	{1, 1, 3, 0,		/* 0xa3 */
1271 		{{0, 1},
1272 		{5, 5},
1273 		{7, 7},
1274 		{0, 0}
1275 		}
1276 	},
1277 	{0, 1, 3, 0,		/* 0xa4 */
1278 		{{2, 2},
1279 		{5, 5},
1280 		{7, 7},
1281 		{0, 0}
1282 		}
1283 	},
1284 	{1, 1, 4, 0,		/* 0xa5 */
1285 		{{0, 0},
1286 		{2, 2},
1287 		{5, 5},
1288 		{7, 7}
1289 		}
1290 	},
1291 	{0, 1, 3, 0,		/* 0xa6 */
1292 		{{1, 2},
1293 		{5, 5},
1294 		{7, 7},
1295 		{0, 0}
1296 		}
1297 	},
1298 	{1, 1, 3, 0,		/* 0xa7 */
1299 		{{0, 2},
1300 		{5, 5},
1301 		{7, 7},
1302 		{0, 0}
1303 		}
1304 	},
1305 	{0, 1, 3, 0,		/* 0xa8 */
1306 		{{3, 3},
1307 		{5, 5},
1308 		{7, 7},
1309 		{0, 0}
1310 		}
1311 	},
1312 	{1, 1, 4, 0,		/* 0xa9 */
1313 		{{0, 0},
1314 		{3, 3},
1315 		{5, 5},
1316 		{7, 7}
1317 		}
1318 	},
1319 	{0, 1, 4, 0,		/* 0xaa */
1320 		{{1, 1},
1321 		{3, 3},
1322 		{5, 5},
1323 		{7, 7}
1324 		}
1325 	},
1326 	{1, 1, 4, 0,		/* 0xab */
1327 		{{0, 1},
1328 		{3, 3},
1329 		{5, 5},
1330 		{7, 7}
1331 		}
1332 	},
1333 	{0, 1, 3, 0,		/* 0xac */
1334 		{{2, 3},
1335 		{5, 5},
1336 		{7, 7},
1337 		{0, 0}
1338 		}
1339 	},
1340 	{1, 1, 4, 0,		/* 0xad */
1341 		{{0, 0},
1342 		{2, 3},
1343 		{5, 5},
1344 		{7, 7}
1345 		}
1346 	},
1347 	{0, 1, 3, 0,		/* 0xae */
1348 		{{1, 3},
1349 		{5, 5},
1350 		{7, 7},
1351 		{0, 0}
1352 		}
1353 	},
1354 	{1, 1, 3, 0,		/* 0xaf */
1355 		{{0, 3},
1356 		{5, 5},
1357 		{7, 7},
1358 		{0, 0}
1359 		}
1360 	},
1361 	{0, 1, 2, 0,		/* 0xb0 */
1362 		{{4, 5},
1363 		{7, 7},
1364 		{0, 0},
1365 		{0, 0}
1366 		}
1367 	},
1368 	{1, 1, 3, 0,		/* 0xb1 */
1369 		{{0, 0},
1370 		{4, 5},
1371 		{7, 7},
1372 		{0, 0}
1373 		}
1374 	},
1375 	{0, 1, 3, 0,		/* 0xb2 */
1376 		{{1, 1},
1377 		{4, 5},
1378 		{7, 7},
1379 		{0, 0}
1380 		}
1381 	},
1382 	{1, 1, 3, 0,		/* 0xb3 */
1383 		{{0, 1},
1384 		{4, 5},
1385 		{7, 7},
1386 		{0, 0}
1387 		}
1388 	},
1389 	{0, 1, 3, 0,		/* 0xb4 */
1390 		{{2, 2},
1391 		{4, 5},
1392 		{7, 7},
1393 		{0, 0}
1394 		}
1395 	},
1396 	{1, 1, 4, 0,		/* 0xb5 */
1397 		{{0, 0},
1398 		{2, 2},
1399 		{4, 5},
1400 		{7, 7}
1401 		}
1402 	},
1403 	{0, 1, 3, 0,		/* 0xb6 */
1404 		{{1, 2},
1405 		{4, 5},
1406 		{7, 7},
1407 		{0, 0}
1408 		}
1409 	},
1410 	{1, 1, 3, 0,		/* 0xb7 */
1411 		{{0, 2},
1412 		{4, 5},
1413 		{7, 7},
1414 		{0, 0}
1415 		}
1416 	},
1417 	{0, 1, 2, 0,		/* 0xb8 */
1418 		{{3, 5},
1419 		{7, 7},
1420 		{0, 0},
1421 		{0, 0}
1422 		}
1423 	},
1424 	{1, 1, 3, 0,		/* 0xb9 */
1425 		{{0, 0},
1426 		{3, 5},
1427 		{7, 7},
1428 		{0, 0}
1429 		}
1430 	},
1431 	{0, 1, 3, 0,		/* 0xba */
1432 		{{1, 1},
1433 		{3, 5},
1434 		{7, 7},
1435 		{0, 0}
1436 		}
1437 	},
1438 	{1, 1, 3, 0,		/* 0xbb */
1439 		{{0, 1},
1440 		{3, 5},
1441 		{7, 7},
1442 		{0, 0}
1443 		}
1444 	},
1445 	{0, 1, 2, 0,		/* 0xbc */
1446 		{{2, 5},
1447 		{7, 7},
1448 		{0, 0},
1449 		{0, 0}
1450 		}
1451 	},
1452 	{1, 1, 3, 0,		/* 0xbd */
1453 		{{0, 0},
1454 		{2, 5},
1455 		{7, 7},
1456 		{0, 0}
1457 		}
1458 	},
1459 	{0, 1, 2, 0,		/* 0xbe */
1460 		{{1, 5},
1461 		{7, 7},
1462 		{0, 0},
1463 		{0, 0}
1464 		}
1465 	},
1466 	{1, 1, 2, 0,		/* 0xbf */
1467 		{{0, 5},
1468 		{7, 7},
1469 		{0, 0},
1470 		{0, 0}
1471 		}
1472 	},
1473 	{0, 1, 1, 0,		/* 0xc0 */
1474 		{{6, 7},
1475 		{0, 0},
1476 		{0, 0},
1477 		{0, 0}
1478 		}
1479 	},
1480 	{1, 1, 2, 0,		/* 0xc1 */
1481 		{{0, 0},
1482 		{6, 7},
1483 		{0, 0},
1484 		{0, 0}
1485 		}
1486 	},
1487 	{0, 1, 2, 0,		/* 0xc2 */
1488 		{{1, 1},
1489 		{6, 7},
1490 		{0, 0},
1491 		{0, 0}
1492 		}
1493 	},
1494 	{1, 1, 2, 0,		/* 0xc3 */
1495 		{{0, 1},
1496 		{6, 7},
1497 		{0, 0},
1498 		{0, 0}
1499 		}
1500 	},
1501 	{0, 1, 2, 0,		/* 0xc4 */
1502 		{{2, 2},
1503 		{6, 7},
1504 		{0, 0},
1505 		{0, 0}
1506 		}
1507 	},
1508 	{1, 1, 3, 0,		/* 0xc5 */
1509 		{{0, 0},
1510 		{2, 2},
1511 		{6, 7},
1512 		{0, 0}
1513 		}
1514 	},
1515 	{0, 1, 2, 0,		/* 0xc6 */
1516 		{{1, 2},
1517 		{6, 7},
1518 		{0, 0},
1519 		{0, 0}
1520 		}
1521 	},
1522 	{1, 1, 2, 0,		/* 0xc7 */
1523 		{{0, 2},
1524 		{6, 7},
1525 		{0, 0},
1526 		{0, 0}
1527 		}
1528 	},
1529 	{0, 1, 2, 0,		/* 0xc8 */
1530 		{{3, 3},
1531 		{6, 7},
1532 		{0, 0},
1533 		{0, 0}
1534 		}
1535 	},
1536 	{1, 1, 3, 0,		/* 0xc9 */
1537 		{{0, 0},
1538 		{3, 3},
1539 		{6, 7},
1540 		{0, 0}
1541 		}
1542 	},
1543 	{0, 1, 3, 0,		/* 0xca */
1544 		{{1, 1},
1545 		{3, 3},
1546 		{6, 7},
1547 		{0, 0}
1548 		}
1549 	},
1550 	{1, 1, 3, 0,		/* 0xcb */
1551 		{{0, 1},
1552 		{3, 3},
1553 		{6, 7},
1554 		{0, 0}
1555 		}
1556 	},
1557 	{0, 1, 2, 0,		/* 0xcc */
1558 		{{2, 3},
1559 		{6, 7},
1560 		{0, 0},
1561 		{0, 0}
1562 		}
1563 	},
1564 	{1, 1, 3, 0,		/* 0xcd */
1565 		{{0, 0},
1566 		{2, 3},
1567 		{6, 7},
1568 		{0, 0}
1569 		}
1570 	},
1571 	{0, 1, 2, 0,		/* 0xce */
1572 		{{1, 3},
1573 		{6, 7},
1574 		{0, 0},
1575 		{0, 0}
1576 		}
1577 	},
1578 	{1, 1, 2, 0,		/* 0xcf */
1579 		{{0, 3},
1580 		{6, 7},
1581 		{0, 0},
1582 		{0, 0}
1583 		}
1584 	},
1585 	{0, 1, 2, 0,		/* 0xd0 */
1586 		{{4, 4},
1587 		{6, 7},
1588 		{0, 0},
1589 		{0, 0}
1590 		}
1591 	},
1592 	{1, 1, 3, 0,		/* 0xd1 */
1593 		{{0, 0},
1594 		{4, 4},
1595 		{6, 7},
1596 		{0, 0}
1597 		}
1598 	},
1599 	{0, 1, 3, 0,		/* 0xd2 */
1600 		{{1, 1},
1601 		{4, 4},
1602 		{6, 7},
1603 		{0, 0}
1604 		}
1605 	},
1606 	{1, 1, 3, 0,		/* 0xd3 */
1607 		{{0, 1},
1608 		{4, 4},
1609 		{6, 7},
1610 		{0, 0}
1611 		}
1612 	},
1613 	{0, 1, 3, 0,		/* 0xd4 */
1614 		{{2, 2},
1615 		{4, 4},
1616 		{6, 7},
1617 		{0, 0}
1618 		}
1619 	},
1620 	{1, 1, 4, 0,		/* 0xd5 */
1621 		{{0, 0},
1622 		{2, 2},
1623 		{4, 4},
1624 		{6, 7}
1625 		}
1626 	},
1627 	{0, 1, 3, 0,		/* 0xd6 */
1628 		{{1, 2},
1629 		{4, 4},
1630 		{6, 7},
1631 		{0, 0}
1632 		}
1633 	},
1634 	{1, 1, 3, 0,		/* 0xd7 */
1635 		{{0, 2},
1636 		{4, 4},
1637 		{6, 7},
1638 		{0, 0}
1639 		}
1640 	},
1641 	{0, 1, 2, 0,		/* 0xd8 */
1642 		{{3, 4},
1643 		{6, 7},
1644 		{0, 0},
1645 		{0, 0}
1646 		}
1647 	},
1648 	{1, 1, 3, 0,		/* 0xd9 */
1649 		{{0, 0},
1650 		{3, 4},
1651 		{6, 7},
1652 		{0, 0}
1653 		}
1654 	},
1655 	{0, 1, 3, 0,		/* 0xda */
1656 		{{1, 1},
1657 		{3, 4},
1658 		{6, 7},
1659 		{0, 0}
1660 		}
1661 	},
1662 	{1, 1, 3, 0,		/* 0xdb */
1663 		{{0, 1},
1664 		{3, 4},
1665 		{6, 7},
1666 		{0, 0}
1667 		}
1668 	},
1669 	{0, 1, 2, 0,		/* 0xdc */
1670 		{{2, 4},
1671 		{6, 7},
1672 		{0, 0},
1673 		{0, 0}
1674 		}
1675 	},
1676 	{1, 1, 3, 0,		/* 0xdd */
1677 		{{0, 0},
1678 		{2, 4},
1679 		{6, 7},
1680 		{0, 0}
1681 		}
1682 	},
1683 	{0, 1, 2, 0,		/* 0xde */
1684 		{{1, 4},
1685 		{6, 7},
1686 		{0, 0},
1687 		{0, 0}
1688 		}
1689 	},
1690 	{1, 1, 2, 0,		/* 0xdf */
1691 		{{0, 4},
1692 		{6, 7},
1693 		{0, 0},
1694 		{0, 0}
1695 		}
1696 	},
1697 	{0, 1, 1, 0,		/* 0xe0 */
1698 		{{5, 7},
1699 		{0, 0},
1700 		{0, 0},
1701 		{0, 0}
1702 		}
1703 	},
1704 	{1, 1, 2, 0,		/* 0xe1 */
1705 		{{0, 0},
1706 		{5, 7},
1707 		{0, 0},
1708 		{0, 0}
1709 		}
1710 	},
1711 	{0, 1, 2, 0,		/* 0xe2 */
1712 		{{1, 1},
1713 		{5, 7},
1714 		{0, 0},
1715 		{0, 0}
1716 		}
1717 	},
1718 	{1, 1, 2, 0,		/* 0xe3 */
1719 		{{0, 1},
1720 		{5, 7},
1721 		{0, 0},
1722 		{0, 0}
1723 		}
1724 	},
1725 	{0, 1, 2, 0,		/* 0xe4 */
1726 		{{2, 2},
1727 		{5, 7},
1728 		{0, 0},
1729 		{0, 0}
1730 		}
1731 	},
1732 	{1, 1, 3, 0,		/* 0xe5 */
1733 		{{0, 0},
1734 		{2, 2},
1735 		{5, 7},
1736 		{0, 0}
1737 		}
1738 	},
1739 	{0, 1, 2, 0,		/* 0xe6 */
1740 		{{1, 2},
1741 		{5, 7},
1742 		{0, 0},
1743 		{0, 0}
1744 		}
1745 	},
1746 	{1, 1, 2, 0,		/* 0xe7 */
1747 		{{0, 2},
1748 		{5, 7},
1749 		{0, 0},
1750 		{0, 0}
1751 		}
1752 	},
1753 	{0, 1, 2, 0,		/* 0xe8 */
1754 		{{3, 3},
1755 		{5, 7},
1756 		{0, 0},
1757 		{0, 0}
1758 		}
1759 	},
1760 	{1, 1, 3, 0,		/* 0xe9 */
1761 		{{0, 0},
1762 		{3, 3},
1763 		{5, 7},
1764 		{0, 0}
1765 		}
1766 	},
1767 	{0, 1, 3, 0,		/* 0xea */
1768 		{{1, 1},
1769 		{3, 3},
1770 		{5, 7},
1771 		{0, 0}
1772 		}
1773 	},
1774 	{1, 1, 3, 0,		/* 0xeb */
1775 		{{0, 1},
1776 		{3, 3},
1777 		{5, 7},
1778 		{0, 0}
1779 		}
1780 	},
1781 	{0, 1, 2, 0,		/* 0xec */
1782 		{{2, 3},
1783 		{5, 7},
1784 		{0, 0},
1785 		{0, 0}
1786 		}
1787 	},
1788 	{1, 1, 3, 0,		/* 0xed */
1789 		{{0, 0},
1790 		{2, 3},
1791 		{5, 7},
1792 		{0, 0}
1793 		}
1794 	},
1795 	{0, 1, 2, 0,		/* 0xee */
1796 		{{1, 3},
1797 		{5, 7},
1798 		{0, 0},
1799 		{0, 0}
1800 		}
1801 	},
1802 	{1, 1, 2, 0,		/* 0xef */
1803 		{{0, 3},
1804 		{5, 7},
1805 		{0, 0},
1806 		{0, 0}
1807 		}
1808 	},
1809 	{0, 1, 1, 0,		/* 0xf0 */
1810 		{{4, 7},
1811 		{0, 0},
1812 		{0, 0},
1813 		{0, 0}
1814 		}
1815 	},
1816 	{1, 1, 2, 0,		/* 0xf1 */
1817 		{{0, 0},
1818 		{4, 7},
1819 		{0, 0},
1820 		{0, 0}
1821 		}
1822 	},
1823 	{0, 1, 2, 0,		/* 0xf2 */
1824 		{{1, 1},
1825 		{4, 7},
1826 		{0, 0},
1827 		{0, 0}
1828 		}
1829 	},
1830 	{1, 1, 2, 0,		/* 0xf3 */
1831 		{{0, 1},
1832 		{4, 7},
1833 		{0, 0},
1834 		{0, 0}
1835 		}
1836 	},
1837 	{0, 1, 2, 0,		/* 0xf4 */
1838 		{{2, 2},
1839 		{4, 7},
1840 		{0, 0},
1841 		{0, 0}
1842 		}
1843 	},
1844 	{1, 1, 3, 0,		/* 0xf5 */
1845 		{{0, 0},
1846 		{2, 2},
1847 		{4, 7},
1848 		{0, 0}
1849 		}
1850 	},
1851 	{0, 1, 2, 0,		/* 0xf6 */
1852 		{{1, 2},
1853 		{4, 7},
1854 		{0, 0},
1855 		{0, 0}
1856 		}
1857 	},
1858 	{1, 1, 2, 0,		/* 0xf7 */
1859 		{{0, 2},
1860 		{4, 7},
1861 		{0, 0},
1862 		{0, 0}
1863 		}
1864 	},
1865 	{0, 1, 1, 0,		/* 0xf8 */
1866 		{{3, 7},
1867 		{0, 0},
1868 		{0, 0},
1869 		{0, 0}
1870 		}
1871 	},
1872 	{1, 1, 2, 0,		/* 0xf9 */
1873 		{{0, 0},
1874 		{3, 7},
1875 		{0, 0},
1876 		{0, 0}
1877 		}
1878 	},
1879 	{0, 1, 2, 0,		/* 0xfa */
1880 		{{1, 1},
1881 		{3, 7},
1882 		{0, 0},
1883 		{0, 0}
1884 		}
1885 	},
1886 	{1, 1, 2, 0,		/* 0xfb */
1887 		{{0, 1},
1888 		{3, 7},
1889 		{0, 0},
1890 		{0, 0}
1891 		}
1892 	},
1893 	{0, 1, 1, 0,		/* 0xfc */
1894 		{{2, 7},
1895 		{0, 0},
1896 		{0, 0},
1897 		{0, 0}
1898 		}
1899 	},
1900 	{1, 1, 2, 0,		/* 0xfd */
1901 		{{0, 0},
1902 		{2, 7},
1903 		{0, 0},
1904 		{0, 0}
1905 		}
1906 	},
1907 	{0, 1, 1, 0,		/* 0xfe */
1908 		{{1, 7},
1909 		{0, 0},
1910 		{0, 0},
1911 		{0, 0}
1912 		}
1913 	},
1914 	{1, 1, 1, 0,		/* 0xff */
1915 		{{0, 7},
1916 		{0, 0},
1917 		{0, 0},
1918 		{0, 0}
1919 		}
1920 	}
1921 };
1922 
1923 
1924 
1925 
1926 extern int sctp_peer_chunk_oh;
1927 
1928 static int
1929 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
1930 {
1931 	struct cmsghdr cmh;
1932 	int tlen, at;
1933 
1934 	tlen = control->m_len;
1935 	at = 0;
1936 	/*
1937 	 * Independent of how many mbufs, find the c_type inside the control
1938 	 * structure and copy out the data.
1939 	 */
1940 	while (at < tlen) {
1941 		if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
1942 			/* not enough room for one more we are done. */
1943 			return (0);
1944 		}
1945 		m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
1946 		if ((cmh.cmsg_len + at) > tlen) {
1947 			/*
1948 			 * this is real messed up since there is not enough
1949 			 * data here to cover the cmsg header. We are done.
1950 			 */
1951 			return (0);
1952 		}
1953 		if ((cmh.cmsg_level == IPPROTO_SCTP) &&
1954 		    (c_type == cmh.cmsg_type)) {
1955 			/* found the one we want, copy it out */
1956 			at += CMSG_ALIGN(sizeof(struct cmsghdr));
1957 			if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
1958 				/*
1959 				 * space of cmsg_len after header not big
1960 				 * enough
1961 				 */
1962 				return (0);
1963 			}
1964 			m_copydata(control, at, cpsize, data);
1965 			return (1);
1966 		} else {
1967 			at += CMSG_ALIGN(cmh.cmsg_len);
1968 			if (cmh.cmsg_len == 0) {
1969 				break;
1970 			}
1971 		}
1972 	}
1973 	/* not found */
1974 	return (0);
1975 }
1976 
1977 
1978 extern int sctp_mbuf_threshold_count;
1979 
1980 
1981 __inline struct mbuf *
1982 sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header,
1983     int how, int allonebuf, int type)
1984 {
1985 	struct mbuf *m = NULL;
1986 	int aloc_size;
1987 	int index = 0;
1988 	int mbuf_threshold;
1989 
1990 	if (want_header) {
1991 		MGETHDR(m, how, type);
1992 	} else {
1993 		MGET(m, how, type);
1994 	}
1995 	if (m == NULL) {
1996 		return (NULL);
1997 	}
1998 	if (allonebuf == 0)
1999 		mbuf_threshold = sctp_mbuf_threshold_count;
2000 	else
2001 		mbuf_threshold = 1;
2002 
2003 
2004 	if (space_needed > (((mbuf_threshold - 1) * MLEN) + MHLEN)) {
2005 try_again:
2006 		index = 4;
2007 		if (space_needed <= MCLBYTES) {
2008 			aloc_size = MCLBYTES;
2009 		} else if (space_needed <= MJUMPAGESIZE) {
2010 			aloc_size = MJUMPAGESIZE;
2011 			index = 5;
2012 		} else if (space_needed <= MJUM9BYTES) {
2013 			aloc_size = MJUM9BYTES;
2014 			index = 6;
2015 		} else {
2016 			aloc_size = MJUM16BYTES;
2017 			index = 7;
2018 		}
2019 		m_cljget(m, how, aloc_size);
2020 		if (m == NULL) {
2021 			return (NULL);
2022 		}
2023 		if ((m->m_flags & M_EXT) == 0) {
2024 			if ((aloc_size != MCLBYTES) &&
2025 			    (allonebuf == 0)) {
2026 				aloc_size -= 10;
2027 				goto try_again;
2028 			}
2029 			sctp_m_freem(m);
2030 			return (NULL);
2031 		}
2032 	}
2033 	m->m_len = 0;
2034 	m->m_next = m->m_nextpkt = NULL;
2035 #ifdef SCTP_MBUF_LOGGING
2036 	if (m->m_flags & M_EXT) {
2037 		sctp_log_mb(m, SCTP_MBUF_IALLOC);
2038 	}
2039 #endif
2040 
2041 	if (want_header) {
2042 		m->m_pkthdr.len = 0;
2043 	}
2044 	return (m);
2045 }
2046 
2047 
2048 
2049 static struct mbuf *
2050 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
2051     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in)
2052 {
2053 	struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
2054 	struct sctp_state_cookie *stc;
2055 	struct sctp_paramhdr *ph;
2056 	uint8_t *signature;
2057 	int sig_offset;
2058 	uint16_t cookie_sz;
2059 
2060 	mret = NULL;
2061 
2062 
2063 	mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
2064 	    sizeof(struct sctp_paramhdr)), 0, M_DONTWAIT, 1, MT_DATA);
2065 	if (mret == NULL) {
2066 		return (NULL);
2067 	}
2068 	copy_init = sctp_m_copym(init, init_offset, M_COPYALL, M_DONTWAIT);
2069 	if (copy_init == NULL) {
2070 		sctp_m_freem(mret);
2071 		return (NULL);
2072 	}
2073 	copy_initack = sctp_m_copym(initack, initack_offset, M_COPYALL,
2074 	    M_DONTWAIT);
2075 	if (copy_initack == NULL) {
2076 		sctp_m_freem(mret);
2077 		sctp_m_freem(copy_init);
2078 		return (NULL);
2079 	}
2080 	/* easy side we just drop it on the end */
2081 	ph = mtod(mret, struct sctp_paramhdr *);
2082 	mret->m_len = sizeof(struct sctp_state_cookie) +
2083 	    sizeof(struct sctp_paramhdr);
2084 	stc = (struct sctp_state_cookie *)((caddr_t)ph +
2085 	    sizeof(struct sctp_paramhdr));
2086 	ph->param_type = htons(SCTP_STATE_COOKIE);
2087 	ph->param_length = 0;	/* fill in at the end */
2088 	/* Fill in the stc cookie data */
2089 	*stc = *stc_in;
2090 
2091 	/* tack the INIT and then the INIT-ACK onto the chain */
2092 	cookie_sz = 0;
2093 	m_at = mret;
2094 	for (m_at = mret; m_at; m_at = m_at->m_next) {
2095 		cookie_sz += m_at->m_len;
2096 		if (m_at->m_next == NULL) {
2097 			m_at->m_next = copy_init;
2098 			break;
2099 		}
2100 	}
2101 
2102 	for (m_at = copy_init; m_at; m_at = m_at->m_next) {
2103 		cookie_sz += m_at->m_len;
2104 		if (m_at->m_next == NULL) {
2105 			m_at->m_next = copy_initack;
2106 			break;
2107 		}
2108 	}
2109 
2110 	for (m_at = copy_initack; m_at; m_at = m_at->m_next) {
2111 		cookie_sz += m_at->m_len;
2112 		if (m_at->m_next == NULL) {
2113 			break;
2114 		}
2115 	}
2116 	sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
2117 	if (sig == NULL) {
2118 		/* no space, so free the entire chain */
2119 		sctp_m_freem(mret);
2120 		return (NULL);
2121 	}
2122 	sig->m_len = 0;
2123 	m_at->m_next = sig;
2124 	sig_offset = 0;
2125 	signature = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
2126 	/* Time to sign the cookie */
2127 	sctp_hmac_m(SCTP_HMAC,
2128 	    (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
2129 	    SCTP_SECRET_SIZE, mret, sizeof(struct sctp_paramhdr),
2130 	    (uint8_t *) signature);
2131 	sig->m_len += SCTP_SIGNATURE_SIZE;
2132 	cookie_sz += SCTP_SIGNATURE_SIZE;
2133 
2134 	ph->param_length = htons(cookie_sz);
2135 	return (mret);
2136 }
2137 
2138 
2139 static __inline uint8_t
2140 sctp_get_ect(struct sctp_tcb *stcb,
2141     struct sctp_tmit_chunk *chk)
2142 {
2143 	uint8_t this_random;
2144 
2145 	/* Huh? */
2146 	if (sctp_ecn_enable == 0)
2147 		return (0);
2148 
2149 	if (sctp_ecn_nonce == 0)
2150 		/* no nonce, always return ECT0 */
2151 		return (SCTP_ECT0_BIT);
2152 
2153 	if (stcb->asoc.peer_supports_ecn_nonce == 0) {
2154 		/* Peer does NOT support it, so we send a ECT0 only */
2155 		return (SCTP_ECT0_BIT);
2156 	}
2157 	if (chk == NULL)
2158 		return (SCTP_ECT0_BIT);
2159 
2160 	if (((stcb->asoc.hb_random_idx == 3) &&
2161 	    (stcb->asoc.hb_ect_randombit > 7)) ||
2162 	    (stcb->asoc.hb_random_idx > 3)) {
2163 		uint32_t rndval;
2164 
2165 		rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
2166 		memcpy(stcb->asoc.hb_random_values, &rndval,
2167 		    sizeof(stcb->asoc.hb_random_values));
2168 		this_random = stcb->asoc.hb_random_values[0];
2169 		stcb->asoc.hb_random_idx = 0;
2170 		stcb->asoc.hb_ect_randombit = 0;
2171 	} else {
2172 		if (stcb->asoc.hb_ect_randombit > 7) {
2173 			stcb->asoc.hb_ect_randombit = 0;
2174 			stcb->asoc.hb_random_idx++;
2175 		}
2176 		this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
2177 	}
2178 	if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
2179 		if (chk != NULL)
2180 			/* ECN Nonce stuff */
2181 			chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
2182 		stcb->asoc.hb_ect_randombit++;
2183 		return (SCTP_ECT1_BIT);
2184 	} else {
2185 		stcb->asoc.hb_ect_randombit++;
2186 		return (SCTP_ECT0_BIT);
2187 	}
2188 }
2189 
2190 extern int sctp_no_csum_on_loopback;
2191 
2192 static int
2193 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
2194     struct sctp_tcb *stcb,	/* may be NULL */
2195     struct sctp_nets *net,
2196     struct sockaddr *to,
2197     struct mbuf *m,
2198     uint32_t auth_offset,
2199     struct sctp_auth_chunk *auth,
2200     int nofragment_flag,
2201     int ecn_ok,
2202     struct sctp_tmit_chunk *chk,
2203     int out_of_asoc_ok)
2204 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
2205 {
2206 	/*
2207 	 * Given a mbuf chain (via m_next) that holds a packet header WITH a
2208 	 * SCTPHDR but no IP header, endpoint inp and sa structure. - fill
2209 	 * in the HMAC digest of any AUTH chunk in the packet - calculate
2210 	 * SCTP checksum and fill in - prepend a IP address header - if
2211 	 * boundall use INADDR_ANY - if boundspecific do source address
2212 	 * selection - set fragmentation option for ipV4 - On return from IP
2213 	 * output, check/adjust mtu size - of output interface and
2214 	 * smallest_mtu size as well.
2215 	 */
2216 	struct sctphdr *sctphdr;
2217 	int o_flgs;
2218 	uint32_t csum;
2219 	int ret;
2220 	unsigned int have_mtu;
2221 	struct route *ro;
2222 
2223 	if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
2224 		sctp_m_freem(m);
2225 		return (EFAULT);
2226 	}
2227 	if ((m->m_flags & M_PKTHDR) == 0) {
2228 #ifdef SCTP_DEBUG
2229 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2230 			printf("Software error: sctp_lowlevel_chunk_output() called with non pkthdr!\n");
2231 		}
2232 #endif
2233 		sctp_m_freem(m);
2234 		return (EFAULT);
2235 	}
2236 	/* fill in the HMAC digest for any AUTH chunk in the packet */
2237 	if ((auth != NULL) && (stcb != NULL)) {
2238 		sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb);
2239 	}
2240 	/* Calculate the csum and fill in the length of the packet */
2241 	sctphdr = mtod(m, struct sctphdr *);
2242 	have_mtu = 0;
2243 	if (sctp_no_csum_on_loopback &&
2244 	    (stcb) &&
2245 	    (stcb->asoc.loopback_scope)) {
2246 		sctphdr->checksum = 0;
2247 		/*
2248 		 * This can probably now be taken out since my audit shows
2249 		 * no more bad pktlen's coming in. But we will wait a while
2250 		 * yet.
2251 		 */
2252 		m->m_pkthdr.len = sctp_calculate_len(m);
2253 	} else {
2254 		sctphdr->checksum = 0;
2255 		csum = sctp_calculate_sum(m, &m->m_pkthdr.len, 0);
2256 		sctphdr->checksum = csum;
2257 	}
2258 	if (to->sa_family == AF_INET) {
2259 		struct ip *ip;
2260 		struct route iproute;
2261 		uint8_t tos_value;
2262 
2263 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
2264 		if (m == NULL) {
2265 			/* failed to prepend data, give up */
2266 			return (ENOMEM);
2267 		}
2268 		ip = mtod(m, struct ip *);
2269 		ip->ip_v = IPVERSION;
2270 		ip->ip_hl = (sizeof(struct ip) >> 2);
2271 		if (net) {
2272 			tos_value = net->tos_flowlabel & 0x000000ff;
2273 		} else {
2274 			tos_value = inp->ip_inp.inp.inp_ip_tos;
2275 		}
2276 		if (nofragment_flag) {
2277 #if defined(WITH_CONVERT_IP_OFF) || defined(__FreeBSD__) || defined(__APPLE__)
2278 			ip->ip_off = IP_DF;
2279 #else
2280 			ip->ip_off = htons(IP_DF);
2281 #endif
2282 		} else
2283 			ip->ip_off = 0;
2284 
2285 
2286 		/* FreeBSD has a function for ip_id's */
2287 		ip->ip_id = ip_newid();
2288 
2289 		ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
2290 		ip->ip_len = m->m_pkthdr.len;
2291 		if (stcb) {
2292 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
2293 				/* Enable ECN */
2294 				ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
2295 			} else {
2296 				/* No ECN */
2297 				ip->ip_tos = (u_char)(tos_value & 0xfc);
2298 			}
2299 		} else {
2300 			/* no association at all */
2301 			ip->ip_tos = (tos_value & 0xfc);
2302 		}
2303 		ip->ip_p = IPPROTO_SCTP;
2304 		ip->ip_sum = 0;
2305 		if (net == NULL) {
2306 			ro = &iproute;
2307 			memset(&iproute, 0, sizeof(iproute));
2308 			memcpy(&ro->ro_dst, to, to->sa_len);
2309 		} else {
2310 			ro = (struct route *)&net->ro;
2311 		}
2312 		/* Now the address selection part */
2313 		ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
2314 
2315 		/* call the routine to select the src address */
2316 		if (net) {
2317 			if (net->src_addr_selected == 0) {
2318 				/* Cache the source address */
2319 				((struct sockaddr_in *)&net->ro._s_addr)->sin_addr = sctp_ipv4_source_address_selection(inp,
2320 				    stcb,
2321 				    ro, net, out_of_asoc_ok);
2322 				if (ro->ro_rt)
2323 					net->src_addr_selected = 1;
2324 			}
2325 			ip->ip_src = ((struct sockaddr_in *)&net->ro._s_addr)->sin_addr;
2326 		} else {
2327 			ip->ip_src = sctp_ipv4_source_address_selection(inp,
2328 			    stcb, ro, net, out_of_asoc_ok);
2329 		}
2330 
2331 		/*
2332 		 * If source address selection fails and we find no route
2333 		 * then the ip_ouput should fail as well with a
2334 		 * NO_ROUTE_TO_HOST type error. We probably should catch
2335 		 * that somewhere and abort the association right away
2336 		 * (assuming this is an INIT being sent).
2337 		 */
2338 		if ((ro->ro_rt == NULL)) {
2339 			/*
2340 			 * src addr selection failed to find a route (or
2341 			 * valid source addr), so we can't get there from
2342 			 * here!
2343 			 */
2344 #ifdef SCTP_DEBUG
2345 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2346 				printf("low_level_output: dropped v4 packet- no valid source addr\n");
2347 				printf("Destination was %x\n", (uint32_t) (ntohl(ip->ip_dst.s_addr)));
2348 			}
2349 #endif				/* SCTP_DEBUG */
2350 			if (net) {
2351 				if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb)
2352 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
2353 					    stcb,
2354 					    SCTP_FAILED_THRESHOLD,
2355 					    (void *)net);
2356 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
2357 				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
2358 				if (stcb) {
2359 					if (net == stcb->asoc.primary_destination) {
2360 						/* need a new primary */
2361 						struct sctp_nets *alt;
2362 
2363 						alt = sctp_find_alternate_net(stcb, net, 0);
2364 						if (alt != net) {
2365 							if (sctp_set_primary_addr(stcb,
2366 							    (struct sockaddr *)NULL,
2367 							    alt) == 0) {
2368 								net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
2369 								net->src_addr_selected = 0;
2370 							}
2371 						}
2372 					}
2373 				}
2374 			}
2375 			sctp_m_freem(m);
2376 			return (EHOSTUNREACH);
2377 		} else {
2378 			have_mtu = ro->ro_rt->rt_ifp->if_mtu;
2379 		}
2380 		if (inp->sctp_socket) {
2381 			o_flgs = (IP_RAWOUTPUT | (inp->sctp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)));
2382 		} else {
2383 			o_flgs = IP_RAWOUTPUT;
2384 		}
2385 #ifdef SCTP_DEBUG
2386 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
2387 			printf("Calling ipv4 output routine from low level src addr:%x\n",
2388 			    (uint32_t) (ntohl(ip->ip_src.s_addr)));
2389 			printf("Destination is %x\n", (uint32_t) (ntohl(ip->ip_dst.s_addr)));
2390 			printf("RTP route is %p through\n", ro->ro_rt);
2391 		}
2392 #endif
2393 
2394 		if ((have_mtu) && (net) && (have_mtu > net->mtu)) {
2395 			ro->ro_rt->rt_ifp->if_mtu = net->mtu;
2396 		}
2397 		if (ro != &iproute) {
2398 			memcpy(&iproute, ro, sizeof(*ro));
2399 		}
2400 		ret = ip_output(m, inp->ip_inp.inp.inp_options,
2401 		    ro, o_flgs, inp->ip_inp.inp.inp_moptions
2402 		    ,(struct inpcb *)NULL
2403 		    );
2404 		if ((ro->ro_rt) && (have_mtu) && (net) && (have_mtu > net->mtu)) {
2405 			ro->ro_rt->rt_ifp->if_mtu = have_mtu;
2406 		}
2407 		SCTP_STAT_INCR(sctps_sendpackets);
2408 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
2409 		if (ret)
2410 			SCTP_STAT_INCR(sctps_senderrors);
2411 #ifdef SCTP_DEBUG
2412 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
2413 			printf("Ip output returns %d\n", ret);
2414 		}
2415 #endif
2416 		if (net == NULL) {
2417 			/* free tempy routes */
2418 			if (ro->ro_rt)
2419 				RTFREE(ro->ro_rt);
2420 		} else {
2421 			/* PMTU check versus smallest asoc MTU goes here */
2422 			if (ro->ro_rt != NULL) {
2423 				if (ro->ro_rt->rt_rmx.rmx_mtu &&
2424 				    (stcb->asoc.smallest_mtu > ro->ro_rt->rt_rmx.rmx_mtu)) {
2425 					sctp_mtu_size_reset(inp, &stcb->asoc,
2426 					    ro->ro_rt->rt_rmx.rmx_mtu);
2427 				}
2428 			} else {
2429 				/* route was freed */
2430 				net->src_addr_selected = 0;
2431 			}
2432 		}
2433 		return (ret);
2434 	}
2435 #ifdef INET6
2436 	else if (to->sa_family == AF_INET6) {
2437 		uint32_t flowlabel;
2438 		struct ip6_hdr *ip6h;
2439 
2440 		struct route_in6 ip6route;
2441 
2442 		struct ifnet *ifp;
2443 		u_char flowTop;
2444 		uint16_t flowBottom;
2445 		u_char tosBottom, tosTop;
2446 		struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
2447 		struct sockaddr_in6 lsa6_storage;
2448 		int prev_scope = 0;
2449 		int error;
2450 		u_short prev_port = 0;
2451 
2452 		if (net != NULL) {
2453 			flowlabel = net->tos_flowlabel;
2454 		} else {
2455 			flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2456 		}
2457 		M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
2458 		if (m == NULL) {
2459 			/* failed to prepend data, give up */
2460 			return (ENOMEM);
2461 		}
2462 		ip6h = mtod(m, struct ip6_hdr *);
2463 
2464 		/*
2465 		 * We assume here that inp_flow is in host byte order within
2466 		 * the TCB!
2467 		 */
2468 		flowBottom = flowlabel & 0x0000ffff;
2469 		flowTop = ((flowlabel & 0x000f0000) >> 16);
2470 		tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
2471 		/* protect *sin6 from overwrite */
2472 		sin6 = (struct sockaddr_in6 *)to;
2473 		tmp = *sin6;
2474 		sin6 = &tmp;
2475 
2476 		/* KAME hack: embed scopeid */
2477 		if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
2478 			return (EINVAL);
2479 		if (net == NULL) {
2480 			memset(&ip6route, 0, sizeof(ip6route));
2481 			ro = (struct route *)&ip6route;
2482 			memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
2483 		} else {
2484 			ro = (struct route *)&net->ro;
2485 		}
2486 		if (stcb != NULL) {
2487 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
2488 				/* Enable ECN */
2489 				tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
2490 			} else {
2491 				/* No ECN */
2492 				tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
2493 			}
2494 		} else {
2495 			/* we could get no asoc if it is a O-O-T-B packet */
2496 			tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
2497 		}
2498 		ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
2499 		ip6h->ip6_nxt = IPPROTO_SCTP;
2500 		ip6h->ip6_plen = m->m_pkthdr.len;
2501 		ip6h->ip6_dst = sin6->sin6_addr;
2502 
2503 		/*
2504 		 * Add SRC address selection here: we can only reuse to a
2505 		 * limited degree the kame src-addr-sel, since we can try
2506 		 * their selection but it may not be bound.
2507 		 */
2508 		bzero(&lsa6_tmp, sizeof(lsa6_tmp));
2509 		lsa6_tmp.sin6_family = AF_INET6;
2510 		lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
2511 		lsa6 = &lsa6_tmp;
2512 		if (net) {
2513 			if (net->src_addr_selected == 0) {
2514 				/* Cache the source address */
2515 				((struct sockaddr_in6 *)&net->ro._s_addr)->sin6_addr = sctp_ipv6_source_address_selection(inp,
2516 				    stcb, ro, net, out_of_asoc_ok);
2517 
2518 				if (ro->ro_rt)
2519 					net->src_addr_selected = 1;
2520 			}
2521 			lsa6->sin6_addr = ((struct sockaddr_in6 *)&net->ro._s_addr)->sin6_addr;
2522 		} else {
2523 			lsa6->sin6_addr = sctp_ipv6_source_address_selection(
2524 			    inp, stcb, ro, net, out_of_asoc_ok);
2525 		}
2526 		lsa6->sin6_port = inp->sctp_lport;
2527 
2528 		if ((ro->ro_rt == NULL)) {
2529 			/*
2530 			 * src addr selection failed to find a route (or
2531 			 * valid source addr), so we can't get there from
2532 			 * here!
2533 			 */
2534 #ifdef SCTP_DEBUG
2535 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2536 				printf("low_level_output: dropped v6 pkt- no valid source addr\n");
2537 			}
2538 #endif
2539 			sctp_m_freem(m);
2540 			if (net) {
2541 				if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb)
2542 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
2543 					    stcb,
2544 					    SCTP_FAILED_THRESHOLD,
2545 					    (void *)net);
2546 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
2547 				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
2548 				if (stcb) {
2549 					if (net == stcb->asoc.primary_destination) {
2550 						/* need a new primary */
2551 						struct sctp_nets *alt;
2552 
2553 						alt = sctp_find_alternate_net(stcb, net, 0);
2554 						if (alt != net) {
2555 							if (sctp_set_primary_addr(stcb,
2556 							    (struct sockaddr *)NULL,
2557 							    alt) == 0) {
2558 								net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
2559 								net->src_addr_selected = 0;
2560 							}
2561 						}
2562 					}
2563 				}
2564 			}
2565 			return (EHOSTUNREACH);
2566 		}
2567 		/*
2568 		 * XXX: sa6 may not have a valid sin6_scope_id in the
2569 		 * non-SCOPEDROUTING case.
2570 		 */
2571 		bzero(&lsa6_storage, sizeof(lsa6_storage));
2572 		lsa6_storage.sin6_family = AF_INET6;
2573 		lsa6_storage.sin6_len = sizeof(lsa6_storage);
2574 		if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
2575 			sctp_m_freem(m);
2576 			return (error);
2577 		}
2578 		/* XXX */
2579 		lsa6_storage.sin6_addr = lsa6->sin6_addr;
2580 		lsa6_storage.sin6_port = inp->sctp_lport;
2581 		lsa6 = &lsa6_storage;
2582 		ip6h->ip6_src = lsa6->sin6_addr;
2583 
2584 		/*
2585 		 * We set the hop limit now since there is a good chance
2586 		 * that our ro pointer is now filled
2587 		 */
2588 		ip6h->ip6_hlim = in6_selecthlim((struct in6pcb *)&inp->ip_inp.inp,
2589 		    (ro ?
2590 		    (ro->ro_rt ? (ro->ro_rt->rt_ifp) : (NULL)) :
2591 		    (NULL)));
2592 		o_flgs = 0;
2593 		ifp = ro->ro_rt->rt_ifp;
2594 #ifdef SCTP_DEBUG
2595 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
2596 			/* Copy to be sure something bad is not happening */
2597 			sin6->sin6_addr = ip6h->ip6_dst;
2598 			lsa6->sin6_addr = ip6h->ip6_src;
2599 
2600 			printf("Calling ipv6 output routine from low level\n");
2601 			printf("src: ");
2602 			sctp_print_address((struct sockaddr *)lsa6);
2603 			printf("dst: ");
2604 			sctp_print_address((struct sockaddr *)sin6);
2605 		}
2606 #endif				/* SCTP_DEBUG */
2607 		if (net) {
2608 			sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
2609 			/* preserve the port and scope for link local send */
2610 			prev_scope = sin6->sin6_scope_id;
2611 			prev_port = sin6->sin6_port;
2612 		}
2613 		ret = ip6_output(m, ((struct in6pcb *)inp)->in6p_outputopts,
2614 		    (struct route_in6 *)ro,
2615 		    o_flgs,
2616 		    ((struct in6pcb *)inp)->in6p_moptions,
2617 		    &ifp
2618 		    ,NULL
2619 		    );
2620 		if (net) {
2621 			/* for link local this must be done */
2622 			sin6->sin6_scope_id = prev_scope;
2623 			sin6->sin6_port = prev_port;
2624 		}
2625 #ifdef SCTP_DEBUG
2626 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
2627 			printf("return from send is %d\n", ret);
2628 		}
2629 #endif				/* SCTP_DEBUG_OUTPUT */
2630 		SCTP_STAT_INCR(sctps_sendpackets);
2631 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
2632 		if (ret)
2633 			SCTP_STAT_INCR(sctps_senderrors);
2634 		if (net == NULL) {
2635 			/* Now if we had a temp route free it */
2636 			if (ro->ro_rt) {
2637 				RTFREE(ro->ro_rt);
2638 			}
2639 		} else {
2640 			/* PMTU check versus smallest asoc MTU goes here */
2641 			if (ro->ro_rt == NULL) {
2642 				/* Route was freed */
2643 				net->src_addr_selected = 0;
2644 			}
2645 			if (ro->ro_rt != NULL) {
2646 				if (ro->ro_rt->rt_rmx.rmx_mtu &&
2647 				    (stcb->asoc.smallest_mtu > ro->ro_rt->rt_rmx.rmx_mtu)) {
2648 					sctp_mtu_size_reset(inp,
2649 					    &stcb->asoc,
2650 					    ro->ro_rt->rt_rmx.rmx_mtu);
2651 				}
2652 			} else if (ifp) {
2653 				if (ND_IFINFO(ifp)->linkmtu &&
2654 				    (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
2655 					sctp_mtu_size_reset(inp,
2656 					    &stcb->asoc,
2657 					    ND_IFINFO(ifp)->linkmtu);
2658 				}
2659 			}
2660 		}
2661 		return (ret);
2662 	}
2663 #endif
2664 	else {
2665 #ifdef SCTP_DEBUG
2666 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2667 			printf("Unknown protocol (TSNH) type %d\n", ((struct sockaddr *)to)->sa_family);
2668 		}
2669 #endif
2670 		sctp_m_freem(m);
2671 		return (EFAULT);
2672 	}
2673 }
2674 
2675 
2676 void
2677 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
2678 {
2679 	struct mbuf *m, *m_at, *m_last;
2680 	struct sctp_nets *net;
2681 	struct sctp_init_msg *initm;
2682 	struct sctp_supported_addr_param *sup_addr;
2683 	struct sctp_ecn_supported_param *ecn;
2684 	struct sctp_prsctp_supported_param *prsctp;
2685 	struct sctp_ecn_nonce_supported_param *ecn_nonce;
2686 	struct sctp_supported_chunk_types_param *pr_supported;
2687 	int cnt_inits_to = 0;
2688 	int padval, ret;
2689 	int num_ext;
2690 	int p_len;
2691 
2692 	/* INIT's always go to the primary (and usually ONLY address) */
2693 	m_last = NULL;
2694 	net = stcb->asoc.primary_destination;
2695 	if (net == NULL) {
2696 		net = TAILQ_FIRST(&stcb->asoc.nets);
2697 		if (net == NULL) {
2698 			/* TSNH */
2699 			return;
2700 		}
2701 		/* we confirm any address we send an INIT to */
2702 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2703 		sctp_set_primary_addr(stcb, NULL, net);
2704 	} else {
2705 		/* we confirm any address we send an INIT to */
2706 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2707 	}
2708 #ifdef SCTP_DEBUG
2709 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2710 		printf("Sending INIT\n");
2711 	}
2712 #endif
2713 	if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
2714 		/*
2715 		 * special hook, if we are sending to link local it will not
2716 		 * show up in our private address count.
2717 		 */
2718 		struct sockaddr_in6 *sin6l;
2719 
2720 		sin6l = &net->ro._l_addr.sin6;
2721 		if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
2722 			cnt_inits_to = 1;
2723 	}
2724 	if (callout_pending(&net->rxt_timer.timer)) {
2725 		/* This case should not happen */
2726 		return;
2727 	}
2728 	/* start the INIT timer */
2729 	if (sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net)) {
2730 		/* we are hosed since I can't start the INIT timer? */
2731 		return;
2732 	}
2733 	m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
2734 	if (m == NULL) {
2735 		/* No memory, INIT timer will re-attempt. */
2736 		return;
2737 	}
2738 	m->m_pkthdr.len = 0;
2739 	m->m_data += SCTP_MIN_OVERHEAD;
2740 	m->m_len = sizeof(struct sctp_init_msg);
2741 	/* Now lets put the SCTP header in place */
2742 	initm = mtod(m, struct sctp_init_msg *);
2743 	initm->sh.src_port = inp->sctp_lport;
2744 	initm->sh.dest_port = stcb->rport;
2745 	initm->sh.v_tag = 0;
2746 	initm->sh.checksum = 0;	/* calculate later */
2747 	/* now the chunk header */
2748 	initm->msg.ch.chunk_type = SCTP_INITIATION;
2749 	initm->msg.ch.chunk_flags = 0;
2750 	/* fill in later from mbuf we build */
2751 	initm->msg.ch.chunk_length = 0;
2752 	/* place in my tag */
2753 	initm->msg.init.initiate_tag = htonl(stcb->asoc.my_vtag);
2754 	/* set up some of the credits. */
2755 	initm->msg.init.a_rwnd = htonl(max(inp->sctp_socket->so_rcv.sb_hiwat,
2756 	    SCTP_MINIMAL_RWND));
2757 
2758 	initm->msg.init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
2759 	initm->msg.init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
2760 	initm->msg.init.initial_tsn = htonl(stcb->asoc.init_seq_number);
2761 	/* now the address restriction */
2762 	sup_addr = (struct sctp_supported_addr_param *)((caddr_t)initm +
2763 	    sizeof(*initm));
2764 	sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
2765 	/* we support 2 types IPv6/IPv4 */
2766 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) +
2767 	    sizeof(uint16_t));
2768 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
2769 	sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
2770 	m->m_len += sizeof(*sup_addr) + sizeof(uint16_t);
2771 
2772 	if (inp->sctp_ep.adaptation_layer_indicator) {
2773 		struct sctp_adaptation_layer_indication *ali;
2774 
2775 		ali = (struct sctp_adaptation_layer_indication *)(
2776 		    (caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
2777 		ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
2778 		ali->ph.param_length = htons(sizeof(*ali));
2779 		ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
2780 		m->m_len += sizeof(*ali);
2781 		ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
2782 		    sizeof(*ali));
2783 	} else {
2784 		ecn = (struct sctp_ecn_supported_param *)((caddr_t)sup_addr +
2785 		    sizeof(*sup_addr) + sizeof(uint16_t));
2786 	}
2787 
2788 	/* now any cookie time extensions */
2789 	if (stcb->asoc.cookie_preserve_req) {
2790 		struct sctp_cookie_perserve_param *cookie_preserve;
2791 
2792 		cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
2793 		cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
2794 		cookie_preserve->ph.param_length = htons(
2795 		    sizeof(*cookie_preserve));
2796 		cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
2797 		m->m_len += sizeof(*cookie_preserve);
2798 		ecn = (struct sctp_ecn_supported_param *)(
2799 		    (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
2800 		stcb->asoc.cookie_preserve_req = 0;
2801 	}
2802 	/* ECN parameter */
2803 	if (sctp_ecn_enable == 1) {
2804 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
2805 		ecn->ph.param_length = htons(sizeof(*ecn));
2806 		m->m_len += sizeof(*ecn);
2807 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
2808 		    sizeof(*ecn));
2809 	} else {
2810 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
2811 	}
2812 	/* And now tell the peer we do pr-sctp */
2813 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
2814 	prsctp->ph.param_length = htons(sizeof(*prsctp));
2815 	m->m_len += sizeof(*prsctp);
2816 
2817 	/* And now tell the peer we do all the extensions */
2818 	pr_supported = (struct sctp_supported_chunk_types_param *)
2819 	    ((caddr_t)prsctp + sizeof(*prsctp));
2820 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
2821 	num_ext = 0;
2822 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
2823 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
2824 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
2825 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
2826 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
2827 	if (!sctp_auth_disable)
2828 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
2829 	p_len = sizeof(*pr_supported) + num_ext;
2830 	pr_supported->ph.param_length = htons(p_len);
2831 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
2832 	m->m_len += SCTP_SIZE32(p_len);
2833 
2834 	/* ECN nonce: And now tell the peer we support ECN nonce */
2835 	if (sctp_ecn_nonce) {
2836 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
2837 		    ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
2838 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
2839 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
2840 		m->m_len += sizeof(*ecn_nonce);
2841 	}
2842 	/* add authentication parameters */
2843 	if (!sctp_auth_disable) {
2844 		struct sctp_auth_random *random;
2845 		struct sctp_auth_hmac_algo *hmacs;
2846 		struct sctp_auth_chunk_list *chunks;
2847 
2848 		/* attach RANDOM parameter, if available */
2849 		if (stcb->asoc.authinfo.random != NULL) {
2850 			random = (struct sctp_auth_random *)(mtod(m, caddr_t)+m->m_len);
2851 			random->ph.param_type = htons(SCTP_RANDOM);
2852 			p_len = sizeof(*random) + stcb->asoc.authinfo.random_len;
2853 			random->ph.param_length = htons(p_len);
2854 			bcopy(stcb->asoc.authinfo.random->key, random->random_data,
2855 			    stcb->asoc.authinfo.random_len);
2856 			/* zero out any padding required */
2857 			bzero((caddr_t)random + p_len, SCTP_SIZE32(p_len) - p_len);
2858 			m->m_len += SCTP_SIZE32(p_len);
2859 		}
2860 		/* add HMAC_ALGO parameter */
2861 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+m->m_len);
2862 		p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
2863 		    (uint8_t *) hmacs->hmac_ids);
2864 		if (p_len > 0) {
2865 			p_len += sizeof(*hmacs);
2866 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
2867 			hmacs->ph.param_length = htons(p_len);
2868 			/* zero out any padding required */
2869 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
2870 			m->m_len += SCTP_SIZE32(p_len);
2871 		}
2872 		/* add CHUNKS parameter */
2873 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+m->m_len);
2874 		p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
2875 		    chunks->chunk_types);
2876 		if (p_len > 0) {
2877 			p_len += sizeof(*chunks);
2878 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
2879 			chunks->ph.param_length = htons(p_len);
2880 			/* zero out any padding required */
2881 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
2882 			m->m_len += SCTP_SIZE32(p_len);
2883 		}
2884 	}
2885 	m_at = m;
2886 	/* now the addresses */
2887 	{
2888 		struct sctp_scoping scp;
2889 
2890 		/*
2891 		 * To optimize this we could put the scoping stuff into a
2892 		 * structure and remove the individual uint8's from the
2893 		 * assoc structure. Then we could just pass in the address
2894 		 * within the stcb.. but for now this is a quick hack to get
2895 		 * the address stuff teased apart.
2896 		 */
2897 		scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
2898 		scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
2899 		scp.loopback_scope = stcb->asoc.loopback_scope;
2900 		scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
2901 		scp.local_scope = stcb->asoc.local_scope;
2902 		scp.site_scope = stcb->asoc.site_scope;
2903 
2904 		m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
2905 	}
2906 
2907 
2908 	/* calulate the size and update pkt header and chunk header */
2909 	m->m_pkthdr.len = 0;
2910 	for (m_at = m; m_at; m_at = m_at->m_next) {
2911 		if (m_at->m_next == NULL)
2912 			m_last = m_at;
2913 		m->m_pkthdr.len += m_at->m_len;
2914 	}
2915 	initm->msg.ch.chunk_length = htons((m->m_pkthdr.len -
2916 	    sizeof(struct sctphdr)));
2917 	/*
2918 	 * We pass 0 here to NOT set IP_DF if its IPv4, we ignore the return
2919 	 * here since the timer will drive a retranmission.
2920 	 */
2921 
2922 	/* I don't expect this to execute but we will be safe here */
2923 	padval = m->m_pkthdr.len % 4;
2924 	if ((padval) && (m_last)) {
2925 		/*
2926 		 * The compiler worries that m_last may not be set even
2927 		 * though I think it is impossible :-> however we add m_last
2928 		 * here just in case.
2929 		 */
2930 		int ret;
2931 
2932 		ret = sctp_add_pad_tombuf(m_last, (4 - padval));
2933 		if (ret) {
2934 			/* Houston we have a problem, no space */
2935 			sctp_m_freem(m);
2936 			return;
2937 		}
2938 		m->m_pkthdr.len += padval;
2939 	}
2940 	ret = sctp_lowlevel_chunk_output(inp, stcb, net,
2941 	    (struct sockaddr *)&net->ro._l_addr,
2942 	    m, 0, NULL, 0, 0, NULL, 0);
2943 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
2944 	sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
2945 	SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
2946 }
2947 
2948 struct mbuf *
2949 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
2950     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp)
2951 {
2952 	/*
2953 	 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
2954 	 * being equal to the beginning of the params i.e. (iphlen +
2955 	 * sizeof(struct sctp_init_msg) parse through the parameters to the
2956 	 * end of the mbuf verifying that all parameters are known.
2957 	 *
2958 	 * For unknown parameters build and return a mbuf with
2959 	 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
2960 	 * processing this chunk stop, and set *abort_processing to 1.
2961 	 *
2962 	 * By having param_offset be pre-set to where parameters begin it is
2963 	 * hoped that this routine may be reused in the future by new
2964 	 * features.
2965 	 */
2966 	struct sctp_paramhdr *phdr, params;
2967 
2968 	struct mbuf *mat, *op_err;
2969 	char tempbuf[2048];
2970 	int at, limit, pad_needed;
2971 	uint16_t ptype, plen;
2972 	int err_at;
2973 
2974 	*abort_processing = 0;
2975 	mat = in_initpkt;
2976 	err_at = 0;
2977 	limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
2978 	at = param_offset;
2979 	op_err = NULL;
2980 
2981 	phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
2982 	while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
2983 		ptype = ntohs(phdr->param_type);
2984 		plen = ntohs(phdr->param_length);
2985 		limit -= SCTP_SIZE32(plen);
2986 		if (plen < sizeof(struct sctp_paramhdr)) {
2987 #ifdef SCTP_DEBUG
2988 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2989 				printf("sctp_output.c:Impossible length in parameter < %d\n", plen);
2990 			}
2991 #endif
2992 			*abort_processing = 1;
2993 			break;
2994 		}
2995 		/*
2996 		 * All parameters for all chunks that we know/understand are
2997 		 * listed here. We process them other places and make
2998 		 * appropriate stop actions per the upper bits. However this
2999 		 * is the generic routine processor's can call to get back
3000 		 * an operr.. to either incorporate (init-ack) or send.
3001 		 */
3002 		if ((ptype == SCTP_HEARTBEAT_INFO) ||
3003 		    (ptype == SCTP_IPV4_ADDRESS) ||
3004 		    (ptype == SCTP_IPV6_ADDRESS) ||
3005 		    (ptype == SCTP_STATE_COOKIE) ||
3006 		    (ptype == SCTP_UNRECOG_PARAM) ||
3007 		    (ptype == SCTP_COOKIE_PRESERVE) ||
3008 		    (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
3009 		    (ptype == SCTP_PRSCTP_SUPPORTED) ||
3010 		    (ptype == SCTP_ADD_IP_ADDRESS) ||
3011 		    (ptype == SCTP_DEL_IP_ADDRESS) ||
3012 		    (ptype == SCTP_ECN_CAPABLE) ||
3013 		    (ptype == SCTP_ULP_ADAPTATION) ||
3014 		    (ptype == SCTP_ERROR_CAUSE_IND) ||
3015 		    (ptype == SCTP_RANDOM) ||
3016 		    (ptype == SCTP_CHUNK_LIST) ||
3017 		    (ptype == SCTP_CHUNK_LIST) ||
3018 		    (ptype == SCTP_SET_PRIM_ADDR) ||
3019 		    (ptype == SCTP_SUCCESS_REPORT) ||
3020 		    (ptype == SCTP_ULP_ADAPTATION) ||
3021 		    (ptype == SCTP_SUPPORTED_CHUNK_EXT) ||
3022 		    (ptype == SCTP_ECN_NONCE_SUPPORTED)
3023 		    ) {
3024 			/* no skip it */
3025 			at += SCTP_SIZE32(plen);
3026 		} else if (ptype == SCTP_HOSTNAME_ADDRESS) {
3027 			/* We can NOT handle HOST NAME addresses!! */
3028 			int l_len;
3029 
3030 #ifdef SCTP_DEBUG
3031 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
3032 				printf("Can't handle hostname addresses.. abort processing\n");
3033 			}
3034 #endif
3035 			*abort_processing = 1;
3036 			if (op_err == NULL) {
3037 				/* Ok need to try to get a mbuf */
3038 				l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
3039 				l_len += plen;
3040 				l_len += sizeof(struct sctp_paramhdr);
3041 				op_err = sctp_get_mbuf_for_msg(l_len, 1, M_DONTWAIT, 1, MT_DATA);
3042 				if (op_err) {
3043 					op_err->m_len = 0;
3044 					op_err->m_pkthdr.len = 0;
3045 					/*
3046 					 * pre-reserve space for ip and sctp
3047 					 * header  and chunk hdr
3048 					 */
3049 					op_err->m_data += sizeof(struct ip6_hdr);
3050 					op_err->m_data += sizeof(struct sctphdr);
3051 					op_err->m_data += sizeof(struct sctp_chunkhdr);
3052 				}
3053 			}
3054 			if (op_err) {
3055 				/* If we have space */
3056 				struct sctp_paramhdr s;
3057 
3058 				if (err_at % 4) {
3059 					uint32_t cpthis = 0;
3060 
3061 					pad_needed = 4 - (err_at % 4);
3062 					m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
3063 					err_at += pad_needed;
3064 				}
3065 				s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
3066 				s.param_length = htons(sizeof(s) + plen);
3067 				m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
3068 				err_at += sizeof(s);
3069 				phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, plen);
3070 				if (phdr == NULL) {
3071 					sctp_m_freem(op_err);
3072 					/*
3073 					 * we are out of memory but we still
3074 					 * need to have a look at what to do
3075 					 * (the system is in trouble
3076 					 * though).
3077 					 */
3078 					return (NULL);
3079 				}
3080 				m_copyback(op_err, err_at, plen, (caddr_t)phdr);
3081 				err_at += plen;
3082 			}
3083 			return (op_err);
3084 		} else {
3085 			/*
3086 			 * we do not recognize the parameter figure out what
3087 			 * we do.
3088 			 */
3089 			if ((ptype & 0x4000) == 0x4000) {
3090 				/* Report bit is set?? */
3091 				if (op_err == NULL) {
3092 					int l_len;
3093 
3094 					/* Ok need to try to get an mbuf */
3095 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
3096 					l_len += plen;
3097 					l_len += sizeof(struct sctp_paramhdr);
3098 					op_err = sctp_get_mbuf_for_msg(l_len, 1, M_DONTWAIT, 1, MT_DATA);
3099 					if (op_err) {
3100 						op_err->m_len = 0;
3101 						op_err->m_pkthdr.len = 0;
3102 						op_err->m_data += sizeof(struct ip6_hdr);
3103 						op_err->m_data += sizeof(struct sctphdr);
3104 						op_err->m_data += sizeof(struct sctp_chunkhdr);
3105 					}
3106 				}
3107 				if (op_err) {
3108 					/* If we have space */
3109 					struct sctp_paramhdr s;
3110 
3111 					if (err_at % 4) {
3112 						uint32_t cpthis = 0;
3113 
3114 						pad_needed = 4 - (err_at % 4);
3115 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
3116 						err_at += pad_needed;
3117 					}
3118 					s.param_type = htons(SCTP_UNRECOG_PARAM);
3119 					s.param_length = htons(sizeof(s) + plen);
3120 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
3121 					err_at += sizeof(s);
3122 					if (plen > sizeof(tempbuf)) {
3123 						plen = sizeof(tempbuf);
3124 					}
3125 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, plen);
3126 					if (phdr == NULL) {
3127 						sctp_m_freem(op_err);
3128 						/*
3129 						 * we are out of memory but
3130 						 * we still need to have a
3131 						 * look at what to do (the
3132 						 * system is in trouble
3133 						 * though).
3134 						 */
3135 						goto more_processing;
3136 					}
3137 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
3138 					err_at += plen;
3139 				}
3140 			}
3141 	more_processing:
3142 			if ((ptype & 0x8000) == 0x0000) {
3143 				return (op_err);
3144 			} else {
3145 				/* skip this chunk and continue processing */
3146 				at += SCTP_SIZE32(plen);
3147 			}
3148 
3149 		}
3150 		phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
3151 	}
3152 	return (op_err);
3153 }
3154 
3155 static int
3156 sctp_are_there_new_addresses(struct sctp_association *asoc,
3157     struct mbuf *in_initpkt, int iphlen, int offset)
3158 {
3159 	/*
3160 	 * Given a INIT packet, look through the packet to verify that there
3161 	 * are NO new addresses. As we go through the parameters add reports
3162 	 * of any un-understood parameters that require an error.  Also we
3163 	 * must return (1) to drop the packet if we see a un-understood
3164 	 * parameter that tells us to drop the chunk.
3165 	 */
3166 	struct sockaddr_in sin4, *sa4;
3167 	struct sockaddr_in6 sin6, *sa6;
3168 	struct sockaddr *sa_touse;
3169 	struct sockaddr *sa;
3170 	struct sctp_paramhdr *phdr, params;
3171 	struct ip *iph;
3172 	struct mbuf *mat;
3173 	uint16_t ptype, plen;
3174 	int err_at;
3175 	uint8_t fnd;
3176 	struct sctp_nets *net;
3177 
3178 	memset(&sin4, 0, sizeof(sin4));
3179 	memset(&sin6, 0, sizeof(sin6));
3180 	sin4.sin_family = AF_INET;
3181 	sin4.sin_len = sizeof(sin4);
3182 	sin6.sin6_family = AF_INET6;
3183 	sin6.sin6_len = sizeof(sin6);
3184 
3185 	sa_touse = NULL;
3186 	/* First what about the src address of the pkt ? */
3187 	iph = mtod(in_initpkt, struct ip *);
3188 	if (iph->ip_v == IPVERSION) {
3189 		/* source addr is IPv4 */
3190 		sin4.sin_addr = iph->ip_src;
3191 		sa_touse = (struct sockaddr *)&sin4;
3192 	} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
3193 		/* source addr is IPv6 */
3194 		struct ip6_hdr *ip6h;
3195 
3196 		ip6h = mtod(in_initpkt, struct ip6_hdr *);
3197 		sin6.sin6_addr = ip6h->ip6_src;
3198 		sa_touse = (struct sockaddr *)&sin6;
3199 	} else {
3200 		return (1);
3201 	}
3202 
3203 	fnd = 0;
3204 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3205 		sa = (struct sockaddr *)&net->ro._l_addr;
3206 		if (sa->sa_family == sa_touse->sa_family) {
3207 			if (sa->sa_family == AF_INET) {
3208 				sa4 = (struct sockaddr_in *)sa;
3209 				if (sa4->sin_addr.s_addr ==
3210 				    sin4.sin_addr.s_addr) {
3211 					fnd = 1;
3212 					break;
3213 				}
3214 			} else if (sa->sa_family == AF_INET6) {
3215 				sa6 = (struct sockaddr_in6 *)sa;
3216 				if (SCTP6_ARE_ADDR_EQUAL(&sa6->sin6_addr,
3217 				    &sin6.sin6_addr)) {
3218 					fnd = 1;
3219 					break;
3220 				}
3221 			}
3222 		}
3223 	}
3224 	if (fnd == 0) {
3225 		/* New address added! no need to look futher. */
3226 		return (1);
3227 	}
3228 	/* Ok so far lets munge through the rest of the packet */
3229 	mat = in_initpkt;
3230 	err_at = 0;
3231 	sa_touse = NULL;
3232 	offset += sizeof(struct sctp_init_chunk);
3233 	phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
3234 	while (phdr) {
3235 		ptype = ntohs(phdr->param_type);
3236 		plen = ntohs(phdr->param_length);
3237 		if (ptype == SCTP_IPV4_ADDRESS) {
3238 			struct sctp_ipv4addr_param *p4, p4_buf;
3239 
3240 			phdr = sctp_get_next_param(mat, offset,
3241 			    (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
3242 			if (plen != sizeof(struct sctp_ipv4addr_param) ||
3243 			    phdr == NULL) {
3244 				return (1);
3245 			}
3246 			p4 = (struct sctp_ipv4addr_param *)phdr;
3247 			sin4.sin_addr.s_addr = p4->addr;
3248 			sa_touse = (struct sockaddr *)&sin4;
3249 		} else if (ptype == SCTP_IPV6_ADDRESS) {
3250 			struct sctp_ipv6addr_param *p6, p6_buf;
3251 
3252 			phdr = sctp_get_next_param(mat, offset,
3253 			    (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
3254 			if (plen != sizeof(struct sctp_ipv6addr_param) ||
3255 			    phdr == NULL) {
3256 				return (1);
3257 			}
3258 			p6 = (struct sctp_ipv6addr_param *)phdr;
3259 			memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
3260 			    sizeof(p6->addr));
3261 			sa_touse = (struct sockaddr *)&sin4;
3262 		}
3263 		if (sa_touse) {
3264 			/* ok, sa_touse points to one to check */
3265 			fnd = 0;
3266 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3267 				sa = (struct sockaddr *)&net->ro._l_addr;
3268 				if (sa->sa_family != sa_touse->sa_family) {
3269 					continue;
3270 				}
3271 				if (sa->sa_family == AF_INET) {
3272 					sa4 = (struct sockaddr_in *)sa;
3273 					if (sa4->sin_addr.s_addr ==
3274 					    sin4.sin_addr.s_addr) {
3275 						fnd = 1;
3276 						break;
3277 					}
3278 				} else if (sa->sa_family == AF_INET6) {
3279 					sa6 = (struct sockaddr_in6 *)sa;
3280 					if (SCTP6_ARE_ADDR_EQUAL(
3281 					    &sa6->sin6_addr, &sin6.sin6_addr)) {
3282 						fnd = 1;
3283 						break;
3284 					}
3285 				}
3286 			}
3287 			if (!fnd) {
3288 				/* New addr added! no need to look further */
3289 				return (1);
3290 			}
3291 		}
3292 		offset += SCTP_SIZE32(plen);
3293 		phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
3294 	}
3295 	return (0);
3296 }
3297 
3298 /*
3299  * Given a MBUF chain that was sent into us containing an INIT. Build a
3300  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
3301  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
3302  * message (i.e. the struct sctp_init_msg).
3303  */
3304 void
3305 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3306     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
3307     struct sctp_init_chunk *init_chk)
3308 {
3309 	struct sctp_association *asoc;
3310 	struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *m_last;
3311 	struct sctp_init_msg *initackm_out;
3312 	struct sctp_ecn_supported_param *ecn;
3313 	struct sctp_prsctp_supported_param *prsctp;
3314 	struct sctp_ecn_nonce_supported_param *ecn_nonce;
3315 	struct sctp_supported_chunk_types_param *pr_supported;
3316 	struct sockaddr_storage store;
3317 	struct sockaddr_in *sin;
3318 	struct sockaddr_in6 *sin6;
3319 	struct route *ro;
3320 	struct ip *iph;
3321 	struct ip6_hdr *ip6;
3322 	struct sockaddr *to;
3323 	struct sctp_state_cookie stc;
3324 	struct sctp_nets *net = NULL;
3325 	int cnt_inits_to = 0;
3326 	uint16_t his_limit, i_want;
3327 	int abort_flag, padval, sz_of;
3328 	int num_ext;
3329 	int p_len;
3330 
3331 	if (stcb) {
3332 		asoc = &stcb->asoc;
3333 	} else {
3334 		asoc = NULL;
3335 	}
3336 	m_last = NULL;
3337 	if ((asoc != NULL) &&
3338 	    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
3339 	    (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
3340 		/* new addresses, out of here in non-cookie-wait states */
3341 		/*
3342 		 * Send a ABORT, we don't add the new address error clause
3343 		 * though we even set the T bit and copy in the 0 tag.. this
3344 		 * looks no different than if no listener was present.
3345 		 */
3346 		sctp_send_abort(init_pkt, iphlen, sh, 0, NULL);
3347 		return;
3348 	}
3349 	abort_flag = 0;
3350 	op_err = sctp_arethere_unrecognized_parameters(init_pkt,
3351 	    (offset + sizeof(struct sctp_init_chunk)),
3352 	    &abort_flag, (struct sctp_chunkhdr *)init_chk);
3353 	if (abort_flag) {
3354 		sctp_send_abort(init_pkt, iphlen, sh, init_chk->init.initiate_tag, op_err);
3355 		return;
3356 	}
3357 	m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
3358 	if (m == NULL) {
3359 		/* No memory, INIT timer will re-attempt. */
3360 		if (op_err)
3361 			sctp_m_freem(op_err);
3362 		return;
3363 	}
3364 	m->m_data += SCTP_MIN_OVERHEAD;
3365 	m->m_pkthdr.rcvif = 0;
3366 	m->m_len = sizeof(struct sctp_init_msg);
3367 
3368 	/* the time I built cookie */
3369 	SCTP_GETTIME_TIMEVAL(&stc.time_entered);
3370 
3371 	/* populate any tie tags */
3372 	if (asoc != NULL) {
3373 		/* unlock before tag selections */
3374 		stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
3375 		stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
3376 		stc.cookie_life = asoc->cookie_life;
3377 		net = asoc->primary_destination;
3378 	} else {
3379 		stc.tie_tag_my_vtag = 0;
3380 		stc.tie_tag_peer_vtag = 0;
3381 		/* life I will award this cookie */
3382 		stc.cookie_life = inp->sctp_ep.def_cookie_life;
3383 	}
3384 
3385 	/* copy in the ports for later check */
3386 	stc.myport = sh->dest_port;
3387 	stc.peerport = sh->src_port;
3388 
3389 	/*
3390 	 * If we wanted to honor cookie life extentions, we would add to
3391 	 * stc.cookie_life. For now we should NOT honor any extension
3392 	 */
3393 	stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
3394 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3395 		struct inpcb *in_inp;
3396 
3397 		/* Its a V6 socket */
3398 		in_inp = (struct inpcb *)inp;
3399 		stc.ipv6_addr_legal = 1;
3400 		/* Now look at the binding flag to see if V4 will be legal */
3401 		if (
3402 		    (in_inp->inp_flags & IN6P_IPV6_V6ONLY)
3403 		    == 0) {
3404 			stc.ipv4_addr_legal = 1;
3405 		} else {
3406 			/* V4 addresses are NOT legal on the association */
3407 			stc.ipv4_addr_legal = 0;
3408 		}
3409 	} else {
3410 		/* Its a V4 socket, no - V6 */
3411 		stc.ipv4_addr_legal = 1;
3412 		stc.ipv6_addr_legal = 0;
3413 	}
3414 
3415 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
3416 	stc.ipv4_scope = 1;
3417 #else
3418 	stc.ipv4_scope = 0;
3419 #endif
3420 	/* now for scope setup */
3421 	memset((caddr_t)&store, 0, sizeof(store));
3422 	sin = (struct sockaddr_in *)&store;
3423 	sin6 = (struct sockaddr_in6 *)&store;
3424 	if (net == NULL) {
3425 		to = (struct sockaddr *)&store;
3426 		iph = mtod(init_pkt, struct ip *);
3427 		if (iph->ip_v == IPVERSION) {
3428 			struct in_addr addr;
3429 			struct route iproute;
3430 
3431 			sin->sin_family = AF_INET;
3432 			sin->sin_len = sizeof(struct sockaddr_in);
3433 			sin->sin_port = sh->src_port;
3434 			sin->sin_addr = iph->ip_src;
3435 			/* lookup address */
3436 			stc.address[0] = sin->sin_addr.s_addr;
3437 			stc.address[1] = 0;
3438 			stc.address[2] = 0;
3439 			stc.address[3] = 0;
3440 			stc.addr_type = SCTP_IPV4_ADDRESS;
3441 			/* local from address */
3442 			memset(&iproute, 0, sizeof(iproute));
3443 			ro = &iproute;
3444 			memcpy(&ro->ro_dst, sin, sizeof(*sin));
3445 			addr = sctp_ipv4_source_address_selection(inp, NULL,
3446 			    ro, NULL, 0);
3447 			if (ro->ro_rt) {
3448 				RTFREE(ro->ro_rt);
3449 			}
3450 			stc.laddress[0] = addr.s_addr;
3451 			stc.laddress[1] = 0;
3452 			stc.laddress[2] = 0;
3453 			stc.laddress[3] = 0;
3454 			stc.laddr_type = SCTP_IPV4_ADDRESS;
3455 			/* scope_id is only for v6 */
3456 			stc.scope_id = 0;
3457 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
3458 			if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3459 				stc.ipv4_scope = 1;
3460 			}
3461 #else
3462 			stc.ipv4_scope = 1;
3463 #endif				/* SCTP_DONT_DO_PRIVADDR_SCOPE */
3464 			/* Must use the address in this case */
3465 			if (sctp_is_address_on_local_host((struct sockaddr *)sin)) {
3466 				stc.loopback_scope = 1;
3467 				stc.ipv4_scope = 1;
3468 				stc.site_scope = 1;
3469 				stc.local_scope = 1;
3470 			}
3471 		} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
3472 			struct in6_addr addr;
3473 
3474 			struct route_in6 iproute6;
3475 
3476 			ip6 = mtod(init_pkt, struct ip6_hdr *);
3477 			sin6->sin6_family = AF_INET6;
3478 			sin6->sin6_len = sizeof(struct sockaddr_in6);
3479 			sin6->sin6_port = sh->src_port;
3480 			sin6->sin6_addr = ip6->ip6_src;
3481 			/* lookup address */
3482 			memcpy(&stc.address, &sin6->sin6_addr,
3483 			    sizeof(struct in6_addr));
3484 			sin6->sin6_scope_id = 0;
3485 			stc.addr_type = SCTP_IPV6_ADDRESS;
3486 			stc.scope_id = 0;
3487 			if (sctp_is_address_on_local_host((struct sockaddr *)sin6)) {
3488 				stc.loopback_scope = 1;
3489 				stc.local_scope = 1;
3490 				stc.site_scope = 1;
3491 				stc.ipv4_scope = 1;
3492 			} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3493 				/*
3494 				 * If the new destination is a LINK_LOCAL we
3495 				 * must have common both site and local
3496 				 * scope. Don't set local scope though since
3497 				 * we must depend on the source to be added
3498 				 * implicitly. We cannot assure just because
3499 				 * we share one link that all links are
3500 				 * common.
3501 				 */
3502 				stc.local_scope = 0;
3503 				stc.site_scope = 1;
3504 				stc.ipv4_scope = 1;
3505 				/*
3506 				 * we start counting for the private address
3507 				 * stuff at 1. since the link local we
3508 				 * source from won't show up in our scoped
3509 				 * count.
3510 				 */
3511 				cnt_inits_to = 1;
3512 				/* pull out the scope_id from incoming pkt */
3513 				/* FIX ME: does this have scope from rcvif? */
3514 				(void)sa6_recoverscope(sin6);
3515 				sa6_embedscope(sin6, ip6_use_defzone);
3516 				stc.scope_id = sin6->sin6_scope_id;
3517 			} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3518 				/*
3519 				 * If the new destination is SITE_LOCAL then
3520 				 * we must have site scope in common.
3521 				 */
3522 				stc.site_scope = 1;
3523 			}
3524 			/* local from address */
3525 			memset(&iproute6, 0, sizeof(iproute6));
3526 			ro = (struct route *)&iproute6;
3527 			memcpy(&ro->ro_dst, sin6, sizeof(*sin6));
3528 			addr = sctp_ipv6_source_address_selection(inp, NULL,
3529 			    ro, NULL, 0);
3530 			if (ro->ro_rt) {
3531 				RTFREE(ro->ro_rt);
3532 			}
3533 			memcpy(&stc.laddress, &addr, sizeof(struct in6_addr));
3534 			stc.laddr_type = SCTP_IPV6_ADDRESS;
3535 		}
3536 	} else {
3537 		/* set the scope per the existing tcb */
3538 		struct sctp_nets *lnet;
3539 
3540 		stc.loopback_scope = asoc->loopback_scope;
3541 		stc.ipv4_scope = asoc->ipv4_local_scope;
3542 		stc.site_scope = asoc->site_scope;
3543 		stc.local_scope = asoc->local_scope;
3544 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
3545 			if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
3546 				if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
3547 					/*
3548 					 * if we have a LL address, start
3549 					 * counting at 1.
3550 					 */
3551 					cnt_inits_to = 1;
3552 				}
3553 			}
3554 		}
3555 
3556 		/* use the net pointer */
3557 		to = (struct sockaddr *)&net->ro._l_addr;
3558 		if (to->sa_family == AF_INET) {
3559 			sin = (struct sockaddr_in *)to;
3560 			stc.address[0] = sin->sin_addr.s_addr;
3561 			stc.address[1] = 0;
3562 			stc.address[2] = 0;
3563 			stc.address[3] = 0;
3564 			stc.addr_type = SCTP_IPV4_ADDRESS;
3565 			if (net->src_addr_selected == 0) {
3566 				/*
3567 				 * strange case here, the INIT should have
3568 				 * did the selection.
3569 				 */
3570 				net->ro._s_addr.sin.sin_addr =
3571 				    sctp_ipv4_source_address_selection(inp,
3572 				    stcb, (struct route *)&net->ro, net, 0);
3573 				net->src_addr_selected = 1;
3574 
3575 			}
3576 			stc.laddress[0] = net->ro._s_addr.sin.sin_addr.s_addr;
3577 			stc.laddress[1] = 0;
3578 			stc.laddress[2] = 0;
3579 			stc.laddress[3] = 0;
3580 			stc.laddr_type = SCTP_IPV4_ADDRESS;
3581 		} else if (to->sa_family == AF_INET6) {
3582 			sin6 = (struct sockaddr_in6 *)to;
3583 			memcpy(&stc.address, &sin6->sin6_addr,
3584 			    sizeof(struct in6_addr));
3585 			stc.addr_type = SCTP_IPV6_ADDRESS;
3586 			if (net->src_addr_selected == 0) {
3587 				/*
3588 				 * strange case here, the INIT should have
3589 				 * did the selection.
3590 				 */
3591 				net->ro._s_addr.sin6.sin6_addr =
3592 				    sctp_ipv6_source_address_selection(inp,
3593 				    stcb, (struct route *)&net->ro, net, 0);
3594 				net->src_addr_selected = 1;
3595 			}
3596 			memcpy(&stc.laddress, &net->ro._l_addr.sin6.sin6_addr,
3597 			    sizeof(struct in6_addr));
3598 			stc.laddr_type = SCTP_IPV6_ADDRESS;
3599 		}
3600 	}
3601 	/* Now lets put the SCTP header in place */
3602 	initackm_out = mtod(m, struct sctp_init_msg *);
3603 	initackm_out->sh.src_port = inp->sctp_lport;
3604 	initackm_out->sh.dest_port = sh->src_port;
3605 	initackm_out->sh.v_tag = init_chk->init.initiate_tag;
3606 	/* Save it off for quick ref */
3607 	stc.peers_vtag = init_chk->init.initiate_tag;
3608 	initackm_out->sh.checksum = 0;	/* calculate later */
3609 	/* who are we */
3610 	memcpy(stc.identification, SCTP_VERSION_STRING,
3611 	    min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
3612 	/* now the chunk header */
3613 	initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
3614 	initackm_out->msg.ch.chunk_flags = 0;
3615 	/* fill in later from mbuf we build */
3616 	initackm_out->msg.ch.chunk_length = 0;
3617 	/* place in my tag */
3618 	if ((asoc != NULL) &&
3619 	    ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
3620 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
3621 		/* re-use the v-tags and init-seq here */
3622 		initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
3623 		initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
3624 	} else {
3625 		if (asoc) {
3626 			atomic_add_int(&asoc->refcnt, 1);
3627 			SCTP_TCB_UNLOCK(stcb);
3628 			initackm_out->msg.init.initiate_tag = htonl(sctp_select_a_tag(inp));
3629 			/* get a TSN to use too */
3630 			initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
3631 			SCTP_TCB_LOCK(stcb);
3632 			atomic_add_int(&asoc->refcnt, -1);
3633 		} else {
3634 			initackm_out->msg.init.initiate_tag = htonl(sctp_select_a_tag(inp));
3635 			/* get a TSN to use too */
3636 			initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
3637 		}
3638 	}
3639 	/* save away my tag to */
3640 	stc.my_vtag = initackm_out->msg.init.initiate_tag;
3641 
3642 	/* set up some of the credits. */
3643 	initackm_out->msg.init.a_rwnd = htonl(max(inp->sctp_socket->so_rcv.sb_hiwat, SCTP_MINIMAL_RWND));
3644 	/* set what I want */
3645 	his_limit = ntohs(init_chk->init.num_inbound_streams);
3646 	/* choose what I want */
3647 	if (asoc != NULL) {
3648 		if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
3649 			i_want = asoc->streamoutcnt;
3650 		} else {
3651 			i_want = inp->sctp_ep.pre_open_stream_count;
3652 		}
3653 	} else {
3654 		i_want = inp->sctp_ep.pre_open_stream_count;
3655 	}
3656 	if (his_limit < i_want) {
3657 		/* I Want more :< */
3658 		initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
3659 	} else {
3660 		/* I can have what I want :> */
3661 		initackm_out->msg.init.num_outbound_streams = htons(i_want);
3662 	}
3663 	/* tell him his limt. */
3664 	initackm_out->msg.init.num_inbound_streams =
3665 	    htons(inp->sctp_ep.max_open_streams_intome);
3666 	/* setup the ECN pointer */
3667 
3668 	if (inp->sctp_ep.adaptation_layer_indicator) {
3669 		struct sctp_adaptation_layer_indication *ali;
3670 
3671 		ali = (struct sctp_adaptation_layer_indication *)(
3672 		    (caddr_t)initackm_out + sizeof(*initackm_out));
3673 		ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
3674 		ali->ph.param_length = htons(sizeof(*ali));
3675 		ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
3676 		m->m_len += sizeof(*ali);
3677 		ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
3678 		    sizeof(*ali));
3679 	} else {
3680 		ecn = (struct sctp_ecn_supported_param *)(
3681 		    (caddr_t)initackm_out + sizeof(*initackm_out));
3682 	}
3683 
3684 	/* ECN parameter */
3685 	if (sctp_ecn_enable == 1) {
3686 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
3687 		ecn->ph.param_length = htons(sizeof(*ecn));
3688 		m->m_len += sizeof(*ecn);
3689 
3690 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
3691 		    sizeof(*ecn));
3692 	} else {
3693 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
3694 	}
3695 	/* And now tell the peer we do  pr-sctp */
3696 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
3697 	prsctp->ph.param_length = htons(sizeof(*prsctp));
3698 	m->m_len += sizeof(*prsctp);
3699 
3700 	/* And now tell the peer we do all the extensions */
3701 	pr_supported = (struct sctp_supported_chunk_types_param *)
3702 	    ((caddr_t)prsctp + sizeof(*prsctp));
3703 
3704 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
3705 	num_ext = 0;
3706 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
3707 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
3708 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
3709 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
3710 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
3711 	if (!sctp_auth_disable)
3712 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
3713 	p_len = sizeof(*pr_supported) + num_ext;
3714 	pr_supported->ph.param_length = htons(p_len);
3715 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
3716 	m->m_len += SCTP_SIZE32(p_len);
3717 
3718 	/* ECN nonce: And now tell the peer we support ECN nonce */
3719 	if (sctp_ecn_nonce) {
3720 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
3721 		    ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
3722 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
3723 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
3724 		m->m_len += sizeof(*ecn_nonce);
3725 	}
3726 	/* add authentication parameters */
3727 	if (!sctp_auth_disable) {
3728 		struct sctp_auth_random *random;
3729 		struct sctp_auth_hmac_algo *hmacs;
3730 		struct sctp_auth_chunk_list *chunks;
3731 		uint16_t random_len;
3732 
3733 		/* generate and add RANDOM parameter */
3734 		random_len = sctp_auth_random_len;
3735 		random = (struct sctp_auth_random *)(mtod(m, caddr_t)+m->m_len);
3736 		random->ph.param_type = htons(SCTP_RANDOM);
3737 		p_len = sizeof(*random) + random_len;
3738 		random->ph.param_length = htons(p_len);
3739 		sctp_read_random(random->random_data, random_len);
3740 		/* zero out any padding required */
3741 		bzero((caddr_t)random + p_len, SCTP_SIZE32(p_len) - p_len);
3742 		m->m_len += SCTP_SIZE32(p_len);
3743 
3744 		/* add HMAC_ALGO parameter */
3745 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+m->m_len);
3746 		p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
3747 		    (uint8_t *) hmacs->hmac_ids);
3748 		if (p_len > 0) {
3749 			p_len += sizeof(*hmacs);
3750 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
3751 			hmacs->ph.param_length = htons(p_len);
3752 			/* zero out any padding required */
3753 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
3754 			m->m_len += SCTP_SIZE32(p_len);
3755 		}
3756 		/* add CHUNKS parameter */
3757 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+m->m_len);
3758 		p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
3759 		    chunks->chunk_types);
3760 		if (p_len > 0) {
3761 			p_len += sizeof(*chunks);
3762 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
3763 			chunks->ph.param_length = htons(p_len);
3764 			/* zero out any padding required */
3765 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
3766 			m->m_len += SCTP_SIZE32(p_len);
3767 		}
3768 	}
3769 	m_at = m;
3770 	/* now the addresses */
3771 	{
3772 		struct sctp_scoping scp;
3773 
3774 		/*
3775 		 * To optimize this we could put the scoping stuff into a
3776 		 * structure and remove the individual uint8's from the stc
3777 		 * structure. Then we could just pass in the address within
3778 		 * the stc.. but for now this is a quick hack to get the
3779 		 * address stuff teased apart.
3780 		 */
3781 		scp.ipv4_addr_legal = stc.ipv4_addr_legal;
3782 		scp.ipv6_addr_legal = stc.ipv6_addr_legal;
3783 		scp.loopback_scope = stc.loopback_scope;
3784 		scp.ipv4_local_scope = stc.ipv4_scope;
3785 		scp.local_scope = stc.local_scope;
3786 		scp.site_scope = stc.site_scope;
3787 		m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
3788 	}
3789 
3790 	/* tack on the operational error if present */
3791 	if (op_err) {
3792 		if (op_err->m_pkthdr.len % 4) {
3793 			/* must add a pad to the param */
3794 			uint32_t cpthis = 0;
3795 			int padlen;
3796 
3797 			padlen = 4 - (op_err->m_pkthdr.len % 4);
3798 			m_copyback(op_err, op_err->m_pkthdr.len, padlen, (caddr_t)&cpthis);
3799 		}
3800 		while (m_at->m_next != NULL) {
3801 			m_at = m_at->m_next;
3802 		}
3803 		m_at->m_next = op_err;
3804 		while (m_at->m_next != NULL) {
3805 			m_at = m_at->m_next;
3806 		}
3807 	}
3808 	/* Get total size of init packet */
3809 	sz_of = SCTP_SIZE32(ntohs(init_chk->ch.chunk_length));
3810 	/* pre-calulate the size and update pkt header and chunk header */
3811 	m->m_pkthdr.len = 0;
3812 	for (m_tmp = m; m_tmp; m_tmp = m_tmp->m_next) {
3813 		m->m_pkthdr.len += m_tmp->m_len;
3814 		if (m_tmp->m_next == NULL) {
3815 			/* m_tmp should now point to last one */
3816 			break;
3817 		}
3818 	}
3819 	/*
3820 	 * Figure now the size of the cookie. We know the size of the
3821 	 * INIT-ACK. The Cookie is going to be the size of INIT, INIT-ACK,
3822 	 * COOKIE-STRUCTURE and SIGNATURE.
3823 	 */
3824 
3825 	/*
3826 	 * take our earlier INIT calc and add in the sz we just calculated
3827 	 * minus the size of the sctphdr (its not included in chunk size
3828 	 */
3829 
3830 	/* add once for the INIT-ACK */
3831 	sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
3832 
3833 	/* add a second time for the INIT-ACK in the cookie */
3834 	sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
3835 
3836 	/* Now add the cookie header and cookie message struct */
3837 	sz_of += sizeof(struct sctp_state_cookie_param);
3838 	/* ...and add the size of our signature */
3839 	sz_of += SCTP_SIGNATURE_SIZE;
3840 	initackm_out->msg.ch.chunk_length = htons(sz_of);
3841 
3842 	/* Now we must build a cookie */
3843 	m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
3844 	    sizeof(struct sctphdr), &stc);
3845 	if (m_cookie == NULL) {
3846 		/* memory problem */
3847 		sctp_m_freem(m);
3848 		return;
3849 	}
3850 	/* Now append the cookie to the end and update the space/size */
3851 	m_tmp->m_next = m_cookie;
3852 	for (; m_tmp; m_tmp = m_tmp->m_next) {
3853 		m->m_pkthdr.len += m_tmp->m_len;
3854 		if (m_tmp->m_next == NULL) {
3855 			/* m_tmp should now point to last one */
3856 			m_last = m_tmp;
3857 			break;
3858 		}
3859 	}
3860 
3861 	/*
3862 	 * We pass 0 here to NOT set IP_DF if its IPv4, we ignore the return
3863 	 * here since the timer will drive a retranmission.
3864 	 */
3865 	padval = m->m_pkthdr.len % 4;
3866 	if ((padval) && (m_last)) {
3867 		/* see my previous comments on m_last */
3868 		int ret;
3869 
3870 		ret = sctp_add_pad_tombuf(m_last, (4 - padval));
3871 		if (ret) {
3872 			/* Houston we have a problem, no space */
3873 			sctp_m_freem(m);
3874 			return;
3875 		}
3876 		m->m_pkthdr.len += padval;
3877 	}
3878 	sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
3879 	    NULL, 0);
3880 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
3881 }
3882 
3883 
3884 void
3885 sctp_insert_on_wheel(struct sctp_tcb *stcb,
3886     struct sctp_association *asoc,
3887     struct sctp_stream_out *strq, int holds_lock)
3888 {
3889 	struct sctp_stream_out *stre, *strn;
3890 
3891 	if (holds_lock == 0)
3892 		SCTP_TCB_SEND_LOCK(stcb);
3893 	if ((strq->next_spoke.tqe_next) ||
3894 	    (strq->next_spoke.tqe_prev)) {
3895 		/* already on wheel */
3896 		goto outof_here;
3897 	}
3898 	stre = TAILQ_FIRST(&asoc->out_wheel);
3899 	if (stre == NULL) {
3900 		/* only one on wheel */
3901 		TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
3902 		goto outof_here;
3903 	}
3904 	for (; stre; stre = strn) {
3905 		strn = TAILQ_NEXT(stre, next_spoke);
3906 		if (stre->stream_no > strq->stream_no) {
3907 			TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
3908 			goto outof_here;
3909 		} else if (stre->stream_no == strq->stream_no) {
3910 			/* huh, should not happen */
3911 			goto outof_here;
3912 		} else if (strn == NULL) {
3913 			/* next one is null */
3914 			TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
3915 			    next_spoke);
3916 		}
3917 	}
3918 outof_here:
3919 	if (holds_lock == 0)
3920 		SCTP_TCB_SEND_UNLOCK(stcb);
3921 
3922 
3923 }
3924 
3925 static void
3926 sctp_remove_from_wheel(struct sctp_tcb *stcb,
3927     struct sctp_association *asoc,
3928     struct sctp_stream_out *strq)
3929 {
3930 	/* take off and then setup so we know it is not on the wheel */
3931 	SCTP_TCB_SEND_LOCK(stcb);
3932 	TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
3933 	strq->next_spoke.tqe_next = NULL;
3934 	strq->next_spoke.tqe_prev = NULL;
3935 	SCTP_TCB_SEND_UNLOCK(stcb);
3936 }
3937 
3938 
3939 static void
3940 sctp_prune_prsctp(struct sctp_tcb *stcb,
3941     struct sctp_association *asoc,
3942     struct sctp_sndrcvinfo *srcv,
3943     int dataout)
3944 {
3945 	int freed_spc = 0;
3946 	struct sctp_tmit_chunk *chk, *nchk;
3947 
3948 	SCTP_TCB_LOCK_ASSERT(stcb);
3949 	if ((asoc->peer_supports_prsctp) &&
3950 	    (asoc->sent_queue_cnt_removeable > 0)) {
3951 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3952 			/*
3953 			 * Look for chunks marked with the PR_SCTP flag AND
3954 			 * the buffer space flag. If the one being sent is
3955 			 * equal or greater priority then purge the old one
3956 			 * and free some space.
3957 			 */
3958 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
3959 				/*
3960 				 * This one is PR-SCTP AND buffer space
3961 				 * limited type
3962 				 */
3963 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
3964 					/*
3965 					 * Lower numbers equates to higher
3966 					 * priority so if the one we are
3967 					 * looking at has a larger or equal
3968 					 * priority we want to drop the data
3969 					 * and NOT retransmit it.
3970 					 */
3971 					if (chk->data) {
3972 						/*
3973 						 * We release the book_size
3974 						 * if the mbuf is here
3975 						 */
3976 						int ret_spc;
3977 						int cause;
3978 
3979 						if (chk->sent > SCTP_DATAGRAM_UNSENT)
3980 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
3981 						else
3982 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
3983 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
3984 						    cause,
3985 						    &asoc->sent_queue);
3986 						freed_spc += ret_spc;
3987 						if (freed_spc >= dataout) {
3988 							return;
3989 						}
3990 					}	/* if chunk was present */
3991 				}	/* if of sufficent priority */
3992 			}	/* if chunk has enabled */
3993 		}		/* tailqforeach */
3994 
3995 		chk = TAILQ_FIRST(&asoc->send_queue);
3996 		while (chk) {
3997 			nchk = TAILQ_NEXT(chk, sctp_next);
3998 			/* Here we must move to the sent queue and mark */
3999 			if (PR_SCTP_TTL_ENABLED(chk->flags)) {
4000 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
4001 					if (chk->data) {
4002 						/*
4003 						 * We release the book_size
4004 						 * if the mbuf is here
4005 						 */
4006 						int ret_spc;
4007 
4008 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
4009 						    SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
4010 						    &asoc->send_queue);
4011 
4012 						freed_spc += ret_spc;
4013 						if (freed_spc >= dataout) {
4014 							return;
4015 						}
4016 					}	/* end if chk->data */
4017 				}	/* end if right class */
4018 			}	/* end if chk pr-sctp */
4019 			chk = nchk;
4020 		}		/* end while (chk) */
4021 	}			/* if enabled in asoc */
4022 }
4023 
4024 __inline int
4025 sctp_get_frag_point(struct sctp_tcb *stcb,
4026     struct sctp_association *asoc)
4027 {
4028 	int siz, ovh;
4029 
4030 	/*
4031 	 * For endpoints that have both v6 and v4 addresses we must reserve
4032 	 * room for the ipv6 header, for those that are only dealing with V4
4033 	 * we use a larger frag point.
4034 	 */
4035 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4036 		ovh = SCTP_MED_OVERHEAD;
4037 	} else {
4038 		ovh = SCTP_MED_V4_OVERHEAD;
4039 	}
4040 
4041 	if (stcb->sctp_ep->sctp_frag_point > asoc->smallest_mtu)
4042 		siz = asoc->smallest_mtu - ovh;
4043 	else
4044 		siz = (stcb->sctp_ep->sctp_frag_point - ovh);
4045 	/*
4046 	 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
4047 	 */
4048 	/* A data chunk MUST fit in a cluster */
4049 	/* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
4050 	/* } */
4051 
4052 	/* adjust for an AUTH chunk if DATA requires auth */
4053 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
4054 		siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
4055 
4056 	if (siz % 4) {
4057 		/* make it an even word boundary please */
4058 		siz -= (siz % 4);
4059 	}
4060 	return (siz);
4061 }
4062 extern unsigned int sctp_max_chunks_on_queue;
4063 
4064 static void
4065 sctp_set_prsctp_policy(struct sctp_tcb *stcb,
4066     struct sctp_stream_queue_pending *sp)
4067 {
4068 	sp->pr_sctp_on = 0;
4069 	if (stcb->asoc.peer_supports_prsctp) {
4070 		/*
4071 		 * We assume that the user wants PR_SCTP_TTL if the user
4072 		 * provides a positive lifetime but does not specify any
4073 		 * PR_SCTP policy. This is a BAD assumption and causes
4074 		 * problems at least with the U-Vancovers MPI folks. I will
4075 		 * change this to be no policy means NO PR-SCTP.
4076 		 */
4077 		if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
4078 			sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
4079 			sp->pr_sctp_on = 1;
4080 		} else {
4081 			goto sctp_no_policy;
4082 		}
4083 		switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
4084 		case CHUNK_FLAGS_PR_SCTP_BUF:
4085 			/*
4086 			 * Time to live is a priority stored in tv_sec when
4087 			 * doing the buffer drop thing.
4088 			 */
4089 			sp->ts.tv_sec = sp->timetolive;
4090 			sp->ts.tv_usec = 0;
4091 			break;
4092 		case CHUNK_FLAGS_PR_SCTP_TTL:
4093 			{
4094 				struct timeval tv;
4095 
4096 				SCTP_GETTIME_TIMEVAL(&sp->ts);
4097 				tv.tv_sec = sp->timetolive / 1000;
4098 				tv.tv_usec = (sp->timetolive * 1000) % 1000000;
4099 				timevaladd(&sp->ts, &tv);
4100 			}
4101 			break;
4102 		case CHUNK_FLAGS_PR_SCTP_RTX:
4103 			/*
4104 			 * Time to live is a the number or retransmissions
4105 			 * stored in tv_sec.
4106 			 */
4107 			sp->ts.tv_sec = sp->timetolive;
4108 			sp->ts.tv_usec = 0;
4109 			break;
4110 		default:
4111 #ifdef SCTP_DEBUG
4112 			if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
4113 				printf("Unknown PR_SCTP policy %u.\n", PR_SCTP_POLICY(sp->sinfo_flags));
4114 			}
4115 #endif
4116 			break;
4117 		}
4118 	}
4119 sctp_no_policy:
4120 	if (sp->sinfo_flags & SCTP_UNORDERED)
4121 		sp->act_flags |= SCTP_DATA_UNORDERED;
4122 
4123 }
4124 
4125 
4126 static int
4127 sctp_msg_append(struct sctp_tcb *stcb,
4128     struct sctp_nets *net,
4129     struct mbuf *m,
4130     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
4131 {
4132 	int error = 0, holds_lock;
4133 	struct mbuf *at;
4134 	struct sctp_stream_queue_pending *sp = NULL;
4135 	struct sctp_stream_out *strm;
4136 
4137 	/*
4138 	 * Given an mbuf chain, put it into the association send queue and
4139 	 * place it on the wheel
4140 	 */
4141 	holds_lock = hold_stcb_lock;
4142 	if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
4143 		/* Invalid stream number */
4144 		error = EINVAL;
4145 		goto out_now;
4146 	}
4147 	if ((stcb->asoc.stream_locked) &&
4148 	    (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
4149 		error = EAGAIN;
4150 		goto out_now;
4151 	}
4152 	strm = &stcb->asoc.strmout[srcv->sinfo_stream];
4153 	/* Now can we send this? */
4154 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
4155 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
4156 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
4157 	    (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
4158 		/* got data while shutting down */
4159 		error = ECONNRESET;
4160 		goto out_now;
4161 	}
4162 	sp = (struct sctp_stream_queue_pending *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_strmoq);
4163 	if (sp == NULL) {
4164 		error = ENOMEM;
4165 		goto out_now;
4166 	}
4167 	SCTP_INCR_STRMOQ_COUNT();
4168 	sp->act_flags = 0;
4169 	sp->sinfo_flags = srcv->sinfo_flags;
4170 	sp->timetolive = srcv->sinfo_timetolive;
4171 	sp->ppid = srcv->sinfo_ppid;
4172 	sp->context = srcv->sinfo_context;
4173 	sp->strseq = 0;
4174 	if (sp->sinfo_flags & SCTP_ADDR_OVER) {
4175 		sp->net = net;
4176 		sp->addr_over = 1;
4177 	} else {
4178 		sp->net = stcb->asoc.primary_destination;
4179 		sp->addr_over = 0;
4180 	}
4181 	atomic_add_int(&sp->net->ref_count, 1);
4182 	SCTP_GETTIME_TIMEVAL(&sp->ts);
4183 	sp->stream = srcv->sinfo_stream;
4184 	sp->msg_is_complete = 1;
4185 	sp->some_taken = 0;
4186 	sp->data = m;
4187 	sp->tail_mbuf = NULL;
4188 	sp->length = 0;
4189 	at = m;
4190 	sctp_set_prsctp_policy(stcb, sp);
4191 	while (at) {
4192 		if (at->m_next == NULL)
4193 			sp->tail_mbuf = at;
4194 		sp->length += at->m_len;
4195 		at = at->m_next;
4196 	}
4197 	if (sp->data->m_flags & M_PKTHDR) {
4198 		sp->data->m_pkthdr.len = sp->length;
4199 	} else {
4200 		/* Get an HDR in front please */
4201 		at = sctp_get_mbuf_for_msg(1, 1, M_DONTWAIT, 1, MT_DATA);
4202 		if (at) {
4203 			at->m_pkthdr.len = sp->length;
4204 			at->m_len = 0;
4205 			at->m_next = sp->data;
4206 			sp->data = at;
4207 		}
4208 	}
4209 	SCTP_TCB_SEND_LOCK(stcb);
4210 	sctp_snd_sb_alloc(stcb, sp->length);
4211 	stcb->asoc.stream_queue_cnt++;
4212 	TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
4213 	if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
4214 		sp->strseq = strm->next_sequence_sent;
4215 		strm->next_sequence_sent++;
4216 	}
4217 	if ((strm->next_spoke.tqe_next == NULL) &&
4218 	    (strm->next_spoke.tqe_prev == NULL)) {
4219 		/* Not on wheel, insert */
4220 		sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 0);
4221 	}
4222 	m = NULL;
4223 	SCTP_TCB_SEND_UNLOCK(stcb);
4224 out_now:
4225 	if (m) {
4226 		sctp_m_freem(m);
4227 	}
4228 	return (error);
4229 }
4230 
4231 
4232 static struct mbuf *
4233 sctp_copy_mbufchain(struct mbuf *clonechain,
4234     struct mbuf *outchain,
4235     struct mbuf **endofchain,
4236     int can_take_mbuf,
4237     int sizeofcpy,
4238     uint8_t copy_by_ref)
4239 {
4240 	struct mbuf *m;
4241 	struct mbuf *appendchain;
4242 	caddr_t cp;
4243 	int len;
4244 
4245 	if (endofchain == NULL) {
4246 		/* error */
4247 error_out:
4248 		if (outchain)
4249 			sctp_m_freem(outchain);
4250 		return (NULL);
4251 	}
4252 	if (can_take_mbuf) {
4253 		appendchain = clonechain;
4254 	} else {
4255 		if (!copy_by_ref && (sizeofcpy <= ((((sctp_mbuf_threshold_count - 1) * MLEN) + MHLEN)))) {
4256 			/* Its not in a cluster */
4257 			if (*endofchain == NULL) {
4258 				/* lets get a mbuf cluster */
4259 				if (outchain == NULL) {
4260 					/* This is the general case */
4261 			new_mbuf:
4262 					outchain = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_HEADER);
4263 					if (outchain == NULL) {
4264 						goto error_out;
4265 					}
4266 					outchain->m_len = 0;
4267 					*endofchain = outchain;
4268 					/* get the prepend space */
4269 					outchain->m_data += (SCTP_FIRST_MBUF_RESV + 4);
4270 				} else {
4271 					/*
4272 					 * We really should not get a NULL
4273 					 * in endofchain
4274 					 */
4275 					/* find end */
4276 					m = outchain;
4277 					while (m) {
4278 						if (m->m_next == NULL) {
4279 							*endofchain = m;
4280 							break;
4281 						}
4282 						m = m->m_next;
4283 					}
4284 					/* sanity */
4285 					if (*endofchain == NULL) {
4286 						/*
4287 						 * huh, TSNH XXX maybe we
4288 						 * should panic
4289 						 */
4290 						sctp_m_freem(outchain);
4291 						goto new_mbuf;
4292 					}
4293 				}
4294 				/* get the new end of length */
4295 				len = M_TRAILINGSPACE(*endofchain);
4296 			} else {
4297 				/* how much is left at the end? */
4298 				len = M_TRAILINGSPACE(*endofchain);
4299 			}
4300 			/* Find the end of the data, for appending */
4301 			cp = (mtod((*endofchain), caddr_t)+(*endofchain)->m_len);
4302 
4303 			/* Now lets copy it out */
4304 			if (len >= sizeofcpy) {
4305 				/* It all fits, copy it in */
4306 				m_copydata(clonechain, 0, sizeofcpy, cp);
4307 				(*endofchain)->m_len += sizeofcpy;
4308 				if (outchain->m_flags & M_PKTHDR)
4309 					outchain->m_pkthdr.len += sizeofcpy;
4310 			} else {
4311 				/* fill up the end of the chain */
4312 				if (len > 0) {
4313 					m_copydata(clonechain, 0, len, cp);
4314 					(*endofchain)->m_len += len;
4315 					if (outchain->m_flags & M_PKTHDR)
4316 						outchain->m_pkthdr.len += len;
4317 					/* now we need another one */
4318 					sizeofcpy -= len;
4319 				}
4320 				m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_HEADER);
4321 				if (m == NULL) {
4322 					/* We failed */
4323 					goto error_out;
4324 				}
4325 				(*endofchain)->m_next = m;
4326 				*endofchain = m;
4327 				cp = mtod((*endofchain), caddr_t);
4328 				m_copydata(clonechain, len, sizeofcpy, cp);
4329 				(*endofchain)->m_len += sizeofcpy;
4330 				if (outchain->m_flags & M_PKTHDR) {
4331 					outchain->m_pkthdr.len += sizeofcpy;
4332 				}
4333 			}
4334 			return (outchain);
4335 		} else {
4336 			/* copy the old fashion way */
4337 			/*
4338 			 * Supposedly m_copypacket is an optimization, use
4339 			 * it if we can
4340 			 */
4341 			if (clonechain->m_flags & M_PKTHDR) {
4342 				appendchain = m_copypacket(clonechain, M_DONTWAIT);
4343 			} else {
4344 				appendchain = m_copy(clonechain, 0, M_COPYALL);
4345 			}
4346 
4347 		}
4348 	}
4349 	if (appendchain == NULL) {
4350 		/* error */
4351 		if (outchain)
4352 			sctp_m_freem(outchain);
4353 		return (NULL);
4354 	}
4355 	/* if outchain is null, check our special reservation flag */
4356 	if (outchain == NULL) {
4357 		/*
4358 		 * need a lead mbuf in this one if we don't have space for:
4359 		 * - E-net header (12+2+2) - IP header (20/40) - SCTP Common
4360 		 * Header (12)
4361 		 */
4362 		if (M_LEADINGSPACE(appendchain) < (SCTP_FIRST_MBUF_RESV)) {
4363 			outchain = sctp_get_mbuf_for_msg(8, 1, M_DONTWAIT, 1, MT_HEADER);
4364 			if (outchain) {
4365 				/*
4366 				 * if we don't hit here we have a problem
4367 				 * anyway :o We reserve all the mbuf for
4368 				 * prepends.
4369 				 */
4370 				outchain->m_pkthdr.len = 0;
4371 				outchain->m_len = 0;
4372 				outchain->m_next = NULL;
4373 				MH_ALIGN(outchain, 4);
4374 				*endofchain = outchain;
4375 			}
4376 		}
4377 	}
4378 	if (outchain) {
4379 		/* tack on to the end */
4380 		if (*endofchain != NULL) {
4381 			(*endofchain)->m_next = appendchain;
4382 		} else {
4383 			m = outchain;
4384 			while (m) {
4385 				if (m->m_next == NULL) {
4386 					m->m_next = appendchain;
4387 					break;
4388 				}
4389 				m = m->m_next;
4390 			}
4391 		}
4392 		if (outchain->m_flags & M_PKTHDR) {
4393 			int append_tot;
4394 
4395 			m = appendchain;
4396 			append_tot = 0;
4397 			while (m) {
4398 				append_tot += m->m_len;
4399 				if (m->m_next == NULL) {
4400 					*endofchain = m;
4401 				}
4402 				m = m->m_next;
4403 			}
4404 			outchain->m_pkthdr.len += append_tot;
4405 		} else {
4406 			/*
4407 			 * save off the end and update the end-chain postion
4408 			 */
4409 			m = appendchain;
4410 			while (m) {
4411 				if (m->m_next == NULL) {
4412 					*endofchain = m;
4413 					break;
4414 				}
4415 				m = m->m_next;
4416 			}
4417 		}
4418 		return (outchain);
4419 	} else {
4420 		/* save off the end and update the end-chain postion */
4421 		m = appendchain;
4422 		while (m) {
4423 			if (m->m_next == NULL) {
4424 				*endofchain = m;
4425 				break;
4426 			}
4427 			m = m->m_next;
4428 		}
4429 		return (appendchain);
4430 	}
4431 }
4432 
4433 int
4434 sctp_med_chunk_output(struct sctp_inpcb *inp,
4435     struct sctp_tcb *stcb,
4436     struct sctp_association *asoc,
4437     int *num_out,
4438     int *reason_code,
4439     int control_only, int *cwnd_full, int from_where,
4440     struct timeval *now, int *now_filled, int frag_point);
4441 
4442 static void
4443 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
4444     uint32_t val)
4445 {
4446 	struct sctp_copy_all *ca;
4447 	struct mbuf *m;
4448 	int ret = 0;
4449 	int added_control = 0;
4450 	int un_sent, do_chunk_output = 1;
4451 	struct sctp_association *asoc;
4452 
4453 	ca = (struct sctp_copy_all *)ptr;
4454 	if (ca->m == NULL) {
4455 		return;
4456 	}
4457 	if (ca->inp != inp) {
4458 		/* TSNH */
4459 		return;
4460 	}
4461 	if ((ca->m) && (ca->m->m_pkthdr.len)) {
4462 		m = m_copym(ca->m, 0, M_COPYALL, M_DONTWAIT);
4463 		if (m == NULL) {
4464 			/* can't copy so we are done */
4465 			ca->cnt_failed++;
4466 			return;
4467 		}
4468 	} else {
4469 		m = NULL;
4470 	}
4471 	SCTP_TCB_LOCK_ASSERT(stcb);
4472 	if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
4473 		/* Abort this assoc with m as the user defined reason */
4474 		if (m) {
4475 			struct sctp_paramhdr *ph;
4476 
4477 			M_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
4478 			if (m) {
4479 				ph = mtod(m, struct sctp_paramhdr *);
4480 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4481 				ph->param_length = htons(m->m_pkthdr.len);
4482 			}
4483 			/*
4484 			 * We add one here to keep the assoc from
4485 			 * dis-appearing on us.
4486 			 */
4487 			atomic_add_int(&stcb->asoc.refcnt, 1);
4488 			sctp_abort_an_association(inp, stcb,
4489 			    SCTP_RESPONSE_TO_USER_REQ,
4490 			    m);
4491 			/*
4492 			 * sctp_abort_an_association calls sctp_free_asoc()
4493 			 * free association will NOT free it since we
4494 			 * incremented the refcnt .. we do this to prevent
4495 			 * it being freed and things getting tricky since we
4496 			 * could end up (from free_asoc) calling inpcb_free
4497 			 * which would get a recursive lock call to the
4498 			 * iterator lock.. But as a consequence of that the
4499 			 * stcb will return to us un-locked.. since
4500 			 * free_asoc returns with either no TCB or the TCB
4501 			 * unlocked, we must relock.. to unlock in the
4502 			 * iterator timer :-0
4503 			 */
4504 			SCTP_TCB_LOCK(stcb);
4505 			atomic_add_int(&stcb->asoc.refcnt, -1);
4506 			goto no_chunk_output;
4507 		}
4508 	} else {
4509 		if (m) {
4510 			ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
4511 			    &ca->sndrcv, 1);
4512 		}
4513 		asoc = &stcb->asoc;
4514 		if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
4515 			/* shutdown this assoc */
4516 			if (TAILQ_EMPTY(&asoc->send_queue) &&
4517 			    TAILQ_EMPTY(&asoc->sent_queue) &&
4518 			    (asoc->stream_queue_cnt == 0)) {
4519 				if (asoc->locked_on_sending) {
4520 					goto abort_anyway;
4521 				}
4522 				/*
4523 				 * there is nothing queued to send, so I'm
4524 				 * done...
4525 				 */
4526 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
4527 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
4528 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4529 					/*
4530 					 * only send SHUTDOWN the first time
4531 					 * through
4532 					 */
4533 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
4534 					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
4535 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4536 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
4537 					    asoc->primary_destination);
4538 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
4539 					    asoc->primary_destination);
4540 					added_control = 1;
4541 					do_chunk_output = 0;
4542 				}
4543 			} else {
4544 				/*
4545 				 * we still got (or just got) data to send,
4546 				 * so set SHUTDOWN_PENDING
4547 				 */
4548 				/*
4549 				 * XXX sockets draft says that SCTP_EOF
4550 				 * should be sent with no data.  currently,
4551 				 * we will allow user data to be sent first
4552 				 * and move to SHUTDOWN-PENDING
4553 				 */
4554 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
4555 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
4556 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4557 					if (asoc->locked_on_sending) {
4558 						/*
4559 						 * Locked to send out the
4560 						 * data
4561 						 */
4562 						struct sctp_stream_queue_pending *sp;
4563 
4564 						sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
4565 						if (sp) {
4566 							if ((sp->length == 0) && (sp->msg_is_complete == 0))
4567 								asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4568 						}
4569 					}
4570 					asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
4571 					if (TAILQ_EMPTY(&asoc->send_queue) &&
4572 					    TAILQ_EMPTY(&asoc->sent_queue) &&
4573 					    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
4574 				abort_anyway:
4575 						atomic_add_int(&stcb->asoc.refcnt, 1);
4576 						sctp_abort_an_association(stcb->sctp_ep, stcb,
4577 						    SCTP_RESPONSE_TO_USER_REQ,
4578 						    NULL);
4579 						atomic_add_int(&stcb->asoc.refcnt, -1);
4580 						goto no_chunk_output;
4581 					}
4582 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
4583 					    asoc->primary_destination);
4584 				}
4585 			}
4586 
4587 		}
4588 	}
4589 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
4590 	    ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)));
4591 
4592 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
4593 	    (stcb->asoc.total_flight > 0) &&
4594 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
4595 	    ) {
4596 		do_chunk_output = 0;
4597 	}
4598 	if (do_chunk_output)
4599 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND);
4600 	else if (added_control) {
4601 		int num_out = 0, reason = 0, cwnd_full = 0, now_filled = 0;
4602 		struct timeval now;
4603 		int frag_point;
4604 
4605 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
4606 		sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
4607 		    &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point);
4608 	}
4609 no_chunk_output:
4610 	if (ret) {
4611 		ca->cnt_failed++;
4612 	} else {
4613 		ca->cnt_sent++;
4614 	}
4615 }
4616 
4617 static void
4618 sctp_sendall_completes(void *ptr, uint32_t val)
4619 {
4620 	struct sctp_copy_all *ca;
4621 
4622 	ca = (struct sctp_copy_all *)ptr;
4623 	/*
4624 	 * Do a notify here? Kacheong suggests that the notify be done at
4625 	 * the send time.. so you would push up a notification if any send
4626 	 * failed. Don't know if this is feasable since the only failures we
4627 	 * have is "memory" related and if you cannot get an mbuf to send
4628 	 * the data you surely can't get an mbuf to send up to notify the
4629 	 * user you can't send the data :->
4630 	 */
4631 
4632 	/* now free everything */
4633 	sctp_m_freem(ca->m);
4634 	SCTP_FREE(ca);
4635 }
4636 
4637 
4638 #define	MC_ALIGN(m, len) do {						\
4639 	(m)->m_data += (MCLBYTES - (len)) & ~(sizeof(long) - 1);		\
4640 } while (0)
4641 
4642 
4643 
4644 static struct mbuf *
4645 sctp_copy_out_all(struct uio *uio, int len)
4646 {
4647 	struct mbuf *ret, *at;
4648 	int left, willcpy, cancpy, error;
4649 
4650 	ret = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_WAIT, 1, MT_DATA);
4651 	if (ret == NULL) {
4652 		/* TSNH */
4653 		return (NULL);
4654 	}
4655 	left = len;
4656 	ret->m_len = 0;
4657 	ret->m_pkthdr.len = len;
4658 	/* save space for the data chunk header */
4659 	cancpy = M_TRAILINGSPACE(ret);
4660 	willcpy = min(cancpy, left);
4661 	at = ret;
4662 	while (left > 0) {
4663 		/* Align data to the end */
4664 		error = uiomove(mtod(at, caddr_t), willcpy, uio);
4665 		if (error) {
4666 	err_out_now:
4667 			sctp_m_freem(at);
4668 			return (NULL);
4669 		}
4670 		at->m_len = willcpy;
4671 		at->m_nextpkt = at->m_next = 0;
4672 		left -= willcpy;
4673 		if (left > 0) {
4674 			at->m_next = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
4675 			if (at->m_next == NULL) {
4676 				goto err_out_now;
4677 			}
4678 			at = at->m_next;
4679 			at->m_len = 0;
4680 			cancpy = M_TRAILINGSPACE(at);
4681 			willcpy = min(cancpy, left);
4682 		}
4683 	}
4684 	return (ret);
4685 }
4686 
4687 static int
4688 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
4689     struct sctp_sndrcvinfo *srcv)
4690 {
4691 	int ret;
4692 	struct sctp_copy_all *ca;
4693 
4694 	SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
4695 	    "CopyAll");
4696 	if (ca == NULL) {
4697 		sctp_m_freem(m);
4698 		return (ENOMEM);
4699 	}
4700 	memset(ca, 0, sizeof(struct sctp_copy_all));
4701 
4702 	ca->inp = inp;
4703 	ca->sndrcv = *srcv;
4704 	/*
4705 	 * take off the sendall flag, it would be bad if we failed to do
4706 	 * this :-0
4707 	 */
4708 	ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
4709 	/* get length and mbuf chain */
4710 	if (uio) {
4711 		ca->sndlen = uio->uio_resid;
4712 		ca->m = sctp_copy_out_all(uio, ca->sndlen);
4713 		if (ca->m == NULL) {
4714 	out_no_mem:
4715 			SCTP_FREE(ca);
4716 			return (ENOMEM);
4717 		}
4718 	} else {
4719 		if ((m->m_flags & M_PKTHDR) == 0) {
4720 			struct mbuf *mat;
4721 
4722 			mat = m;
4723 			ca->sndlen = 0;
4724 			while (m) {
4725 				ca->sndlen += m->m_len;
4726 				m = m->m_next;
4727 			}
4728 			mat = sctp_get_mbuf_for_msg(1, 1, M_WAIT, 1, MT_DATA);
4729 			if (mat) {
4730 				sctp_m_freem(m);
4731 				goto out_no_mem;
4732 			}
4733 			/* We MUST have a header on the front */
4734 			mat->m_next = m;
4735 			mat->m_len = 0;
4736 			mat->m_pkthdr.len = ca->sndlen;
4737 			ca->m = mat;
4738 		} else {
4739 			ca->sndlen = m->m_pkthdr.len;
4740 		}
4741 		ca->m = m;
4742 	}
4743 	ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator,
4744 	    SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES, SCTP_ASOC_ANY_STATE,
4745 	    (void *)ca, 0,
4746 	    sctp_sendall_completes, inp, 1);
4747 	if (ret) {
4748 #ifdef SCTP_DEBUG
4749 		printf("Failed to initiate iterator for sendall\n");
4750 #endif
4751 		SCTP_FREE(ca);
4752 		return (EFAULT);
4753 	}
4754 	return (0);
4755 }
4756 
4757 
4758 void
4759 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
4760 {
4761 	struct sctp_tmit_chunk *chk, *nchk;
4762 
4763 	chk = TAILQ_FIRST(&asoc->control_send_queue);
4764 	while (chk) {
4765 		nchk = TAILQ_NEXT(chk, sctp_next);
4766 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
4767 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4768 			if (chk->data) {
4769 				sctp_m_freem(chk->data);
4770 				chk->data = NULL;
4771 			}
4772 			asoc->ctrl_queue_cnt--;
4773 			if (chk->whoTo)
4774 				sctp_free_remote_addr(chk->whoTo);
4775 			sctp_free_a_chunk(stcb, chk);
4776 		}
4777 		chk = nchk;
4778 	}
4779 }
4780 
4781 void
4782 sctp_toss_old_asconf(struct sctp_tcb *stcb)
4783 {
4784 	struct sctp_association *asoc;
4785 	struct sctp_tmit_chunk *chk, *chk_tmp;
4786 
4787 	asoc = &stcb->asoc;
4788 	for (chk = TAILQ_FIRST(&asoc->control_send_queue); chk != NULL;
4789 	    chk = chk_tmp) {
4790 		/* get next chk */
4791 		chk_tmp = TAILQ_NEXT(chk, sctp_next);
4792 		/* find SCTP_ASCONF chunk in queue (only one ever in queue) */
4793 		if (chk->rec.chunk_id.id == SCTP_ASCONF) {
4794 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4795 			if (chk->data) {
4796 				sctp_m_freem(chk->data);
4797 				chk->data = NULL;
4798 			}
4799 			asoc->ctrl_queue_cnt--;
4800 			if (chk->whoTo)
4801 				sctp_free_remote_addr(chk->whoTo);
4802 			sctp_free_a_chunk(stcb, chk);
4803 		}
4804 	}
4805 }
4806 
4807 
4808 static __inline void
4809 sctp_clean_up_datalist(struct sctp_tcb *stcb,
4810 
4811     struct sctp_association *asoc,
4812     struct sctp_tmit_chunk **data_list,
4813     int bundle_at,
4814     struct sctp_nets *net)
4815 {
4816 	int i;
4817 	struct sctp_tmit_chunk *tp1;
4818 
4819 	for (i = 0; i < bundle_at; i++) {
4820 		/* off of the send queue */
4821 		if (i) {
4822 			/*
4823 			 * Any chunk NOT 0 you zap the time chunk 0 gets
4824 			 * zapped or set based on if a RTO measurment is
4825 			 * needed.
4826 			 */
4827 			data_list[i]->do_rtt = 0;
4828 		}
4829 		/* record time */
4830 		data_list[i]->sent_rcv_time = net->last_sent_time;
4831 		data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
4832 		TAILQ_REMOVE(&asoc->send_queue,
4833 		    data_list[i],
4834 		    sctp_next);
4835 		/* on to the sent queue */
4836 		tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
4837 		if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
4838 		    data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
4839 			struct sctp_tmit_chunk *tpp;
4840 
4841 			/* need to move back */
4842 	back_up_more:
4843 			tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
4844 			if (tpp == NULL) {
4845 				TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
4846 				goto all_done;
4847 			}
4848 			tp1 = tpp;
4849 			if (compare_with_wrap(tp1->rec.data.TSN_seq,
4850 			    data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
4851 				goto back_up_more;
4852 			}
4853 			TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
4854 		} else {
4855 			TAILQ_INSERT_TAIL(&asoc->sent_queue,
4856 			    data_list[i],
4857 			    sctp_next);
4858 		}
4859 all_done:
4860 		/* This does not lower until the cum-ack passes it */
4861 		asoc->sent_queue_cnt++;
4862 		asoc->send_queue_cnt--;
4863 		if ((asoc->peers_rwnd <= 0) &&
4864 		    (asoc->total_flight == 0) &&
4865 		    (bundle_at == 1)) {
4866 			/* Mark the chunk as being a window probe */
4867 			SCTP_STAT_INCR(sctps_windowprobed);
4868 			data_list[i]->rec.data.state_flags |= SCTP_WINDOW_PROBE;
4869 		} else {
4870 			data_list[i]->rec.data.state_flags &= ~SCTP_WINDOW_PROBE;
4871 		}
4872 #ifdef SCTP_AUDITING_ENABLED
4873 		sctp_audit_log(0xC2, 3);
4874 #endif
4875 		data_list[i]->sent = SCTP_DATAGRAM_SENT;
4876 		data_list[i]->snd_count = 1;
4877 		data_list[i]->rec.data.chunk_was_revoked = 0;
4878 		net->flight_size += data_list[i]->book_size;
4879 		asoc->total_flight += data_list[i]->book_size;
4880 		asoc->total_flight_count++;
4881 #ifdef SCTP_LOG_RWND
4882 		sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
4883 		    asoc->peers_rwnd, data_list[i]->send_size, sctp_peer_chunk_oh);
4884 #endif
4885 		asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
4886 		    (uint32_t) (data_list[i]->send_size + sctp_peer_chunk_oh));
4887 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4888 			/* SWS sender side engages */
4889 			asoc->peers_rwnd = 0;
4890 		}
4891 	}
4892 }
4893 
4894 static __inline void
4895 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
4896 {
4897 	struct sctp_tmit_chunk *chk, *nchk;
4898 
4899 	for (chk = TAILQ_FIRST(&asoc->control_send_queue);
4900 	    chk; chk = nchk) {
4901 		nchk = TAILQ_NEXT(chk, sctp_next);
4902 		if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
4903 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
4904 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
4905 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
4906 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
4907 		    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
4908 		    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
4909 		    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
4910 		    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
4911 		    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
4912 			/* Stray chunks must be cleaned up */
4913 	clean_up_anyway:
4914 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4915 			if (chk->data) {
4916 				sctp_m_freem(chk->data);
4917 				chk->data = NULL;
4918 			}
4919 			asoc->ctrl_queue_cnt--;
4920 			sctp_free_remote_addr(chk->whoTo);
4921 			sctp_free_a_chunk(stcb, chk);
4922 		} else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
4923 			/* special handling, we must look into the param */
4924 			if (chk != asoc->str_reset) {
4925 				goto clean_up_anyway;
4926 			}
4927 		}
4928 	}
4929 }
4930 
4931 extern int sctp_min_split_point;
4932 
4933 static __inline int
4934 sctp_can_we_split_this(struct sctp_tcb *stcb,
4935     struct sctp_stream_queue_pending *sp,
4936     int goal_mtu, int frag_point, int eeor_on)
4937 {
4938 	/*
4939 	 * Make a decision on if I should split a msg into multiple parts.
4940 	 */
4941 	if (goal_mtu < sctp_min_split_point) {
4942 		/* you don't want enough */
4943 		return (0);
4944 	}
4945 	if (sp->msg_is_complete == 0) {
4946 		if (eeor_on) {
4947 			/*
4948 			 * If we are doing EEOR we need to always send it if
4949 			 * its the entire thing.
4950 			 */
4951 			if (goal_mtu >= sp->length)
4952 				return (sp->length);
4953 		} else {
4954 			if (goal_mtu >= sp->length) {
4955 				/*
4956 				 * If we cannot fill the amount needed there
4957 				 * is no sense of splitting the chunk.
4958 				 */
4959 				return (0);
4960 			}
4961 		}
4962 		/*
4963 		 * If we reach here sp->length is larger than the goal_mtu.
4964 		 * Do we wish to split it for the sake of packet putting
4965 		 * together?
4966 		 */
4967 		if (goal_mtu >= min(sctp_min_split_point, stcb->asoc.smallest_mtu)) {
4968 			/* Its ok to split it */
4969 			return (min(goal_mtu, frag_point));
4970 		}
4971 	} else {
4972 		/* We can always split a complete message to make it fit */
4973 		if (goal_mtu >= sp->length)
4974 			/* Take it all */
4975 			return (sp->length);
4976 
4977 		return (min(goal_mtu, frag_point));
4978 	}
4979 	/* Nope, can't split */
4980 	return (0);
4981 
4982 }
4983 
4984 static int
4985 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
4986     struct sctp_stream_out *strq,
4987     int goal_mtu,
4988     int frag_point,
4989     int *locked,
4990     int *giveup,
4991     int eeor_mode)
4992 {
4993 	/* Move from the stream to the send_queue keeping track of the total */
4994 	struct sctp_association *asoc;
4995 	struct sctp_stream_queue_pending *sp;
4996 	struct sctp_tmit_chunk *chk;
4997 	struct sctp_data_chunk *dchkh;
4998 	int to_move;
4999 	uint8_t rcv_flags = 0;
5000 
5001 	SCTP_TCB_LOCK_ASSERT(stcb);
5002 	asoc = &stcb->asoc;
5003 	sp = TAILQ_FIRST(&strq->outqueue);
5004 
5005 	if (sp == NULL) {
5006 		*locked = 0;
5007 		SCTP_TCB_SEND_LOCK(stcb);
5008 		if (strq->last_msg_incomplete) {
5009 			printf("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
5010 			    strq->stream_no, strq->last_msg_incomplete);
5011 			strq->last_msg_incomplete = 0;
5012 		}
5013 		SCTP_TCB_SEND_UNLOCK(stcb);
5014 		return (0);
5015 	}
5016 	SCTP_TCB_SEND_LOCK(stcb);
5017 	if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
5018 		/* Must wait for more data, must be last msg */
5019 		*locked = 1;
5020 		*giveup = 1;
5021 		SCTP_TCB_SEND_UNLOCK(stcb);
5022 		return (0);
5023 	} else if (sp->length == 0) {
5024 		/* This should not happen */
5025 		panic("sp length is 0?");
5026 	}
5027 	if ((goal_mtu >= sp->length) && (sp->msg_is_complete)) {
5028 		/* It all fits and its a complete msg, no brainer */
5029 		to_move = min(sp->length, frag_point);
5030 		if (to_move == sp->length) {
5031 			/* Getting it all */
5032 			if (sp->some_taken) {
5033 				rcv_flags |= SCTP_DATA_LAST_FRAG;
5034 			} else {
5035 				rcv_flags |= SCTP_DATA_NOT_FRAG;
5036 			}
5037 		} else {
5038 			/* Not getting it all, frag point overrides */
5039 			if (sp->some_taken == 0) {
5040 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
5041 			}
5042 			sp->some_taken = 1;
5043 		}
5044 	} else {
5045 		to_move = sctp_can_we_split_this(stcb,
5046 		    sp, goal_mtu, frag_point, eeor_mode);
5047 		if (to_move) {
5048 			if (to_move >= sp->length) {
5049 				to_move = sp->length;
5050 			}
5051 			if (sp->some_taken == 0) {
5052 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
5053 			}
5054 			sp->some_taken = 1;
5055 		} else {
5056 			if (sp->some_taken) {
5057 				*locked = 1;
5058 			}
5059 			*giveup = 1;
5060 			SCTP_TCB_SEND_UNLOCK(stcb);
5061 			return (0);
5062 		}
5063 	}
5064 	SCTP_TCB_SEND_UNLOCK(stcb);
5065 	/* If we reach here, we can copy out a chunk */
5066 	sctp_alloc_a_chunk(stcb, chk);
5067 	if (chk == NULL) {
5068 		/* No chunk memory */
5069 out_gu:
5070 		*giveup = 1;
5071 		return (0);
5072 	}
5073 	/* clear it */
5074 	memset(chk, sizeof(*chk), 0);
5075 	chk->rec.data.rcv_flags = rcv_flags;
5076 	SCTP_TCB_SEND_LOCK(stcb);
5077 	sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
5078 	if (sp->data->m_flags & M_EXT) {
5079 		chk->copy_by_ref = 1;
5080 	} else {
5081 		chk->copy_by_ref = 0;
5082 	}
5083 	if (to_move >= sp->length) {
5084 		/* we can steal the whole thing */
5085 		chk->data = sp->data;
5086 		chk->last_mbuf = sp->tail_mbuf;
5087 		/* register the stealing */
5088 		sp->data = sp->tail_mbuf = NULL;
5089 	} else {
5090 		struct mbuf *m;
5091 
5092 		chk->data = m_copym(sp->data, 0, to_move, M_DONTWAIT);
5093 		chk->last_mbuf = NULL;
5094 		if (chk->data == NULL) {
5095 			sctp_free_a_chunk(stcb, chk);
5096 			SCTP_TCB_SEND_UNLOCK(stcb);
5097 			goto out_gu;
5098 		}
5099 		/* Pull off the data */
5100 		m_adj(sp->data, to_move);
5101 		/*
5102 		 * Now lets work our way down and compact it
5103 		 */
5104 		m = sp->data;
5105 		while (m && (m->m_len == 0)) {
5106 			sp->data = m->m_next;
5107 			m->m_next = NULL;
5108 			if (sp->tail_mbuf == m) {
5109 				/* freeing tail */
5110 				sp->tail_mbuf = sp->data;
5111 			}
5112 			sctp_m_free(m);
5113 			m = sp->data;
5114 		}
5115 	}
5116 	if (to_move > sp->length) {
5117 		panic("Huh, how can to_move be larger?");
5118 	} else
5119 		sp->length -= to_move;
5120 
5121 	/* Update the new length in */
5122 	if (sp->data && (sp->data->m_flags & M_PKTHDR)) {
5123 		/* update length */
5124 		sp->data->m_pkthdr.len = sp->length;
5125 	}
5126 	if (M_LEADINGSPACE(chk->data) < sizeof(struct sctp_data_chunk)) {
5127 		/* Not enough room for a chunk header, get some */
5128 		struct mbuf *m;
5129 
5130 		m = sctp_get_mbuf_for_msg(1, 1, M_DONTWAIT, 0, MT_DATA);
5131 		if (m == NULL) {
5132 			printf("We will Panic maybe, out of mbufs\n");
5133 		} else {
5134 			m->m_len = 0;
5135 			m->m_next = chk->data;
5136 			chk->data = m;
5137 			chk->data->m_pkthdr.len = to_move;
5138 			MH_ALIGN(chk->data, 4);
5139 		}
5140 	}
5141 	M_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
5142 	if (chk->data == NULL) {
5143 		/* HELP */
5144 		sctp_free_a_chunk(stcb, chk);
5145 		SCTP_TCB_SEND_UNLOCK(stcb);
5146 		goto out_gu;
5147 	}
5148 	chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
5149 	chk->sent = SCTP_DATAGRAM_UNSENT;
5150 
5151 	/*
5152 	 * get last_mbuf and counts of mb useage This is ugly but hopefully
5153 	 * its only one mbuf.
5154 	 */
5155 	if (chk->last_mbuf == NULL) {
5156 		chk->last_mbuf = chk->data;
5157 		while (chk->last_mbuf->m_next != NULL) {
5158 			chk->last_mbuf = chk->last_mbuf->m_next;
5159 		}
5160 	}
5161 	chk->flags = 0;
5162 	chk->asoc = &stcb->asoc;
5163 	chk->pad_inplace = 0;
5164 	chk->no_fr_allowed = 0;
5165 	chk->rec.data.stream_seq = sp->strseq;
5166 	chk->rec.data.stream_number = sp->stream;
5167 	chk->rec.data.payloadtype = sp->ppid;
5168 	chk->rec.data.context = sp->context;
5169 	chk->rec.data.doing_fast_retransmit = 0;
5170 	chk->rec.data.ect_nonce = 0;	/* ECN Nonce */
5171 
5172 	chk->rec.data.timetodrop = sp->ts;
5173 	chk->flags = sp->act_flags;
5174 	chk->addr_over = sp->addr_over;
5175 
5176 	chk->whoTo = net;
5177 	atomic_add_int(&chk->whoTo->ref_count, 1);
5178 
5179 	chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
5180 #ifdef SCTP_LOG_SENDING_STR
5181 	sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
5182 	    (uintptr_t) stcb, (uintptr_t) sp,
5183 	    (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
5184 	    chk->rec.data.TSN_seq);
5185 #endif
5186 
5187 	dchkh = mtod(chk->data, struct sctp_data_chunk *);
5188 	/*
5189 	 * Put the rest of the things in place now. Size was done earlier in
5190 	 * previous loop prior to padding.
5191 	 */
5192 	dchkh->ch.chunk_type = SCTP_DATA;
5193 	dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
5194 	dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
5195 	dchkh->dp.stream_id = htons(strq->stream_no);
5196 	dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
5197 	dchkh->dp.protocol_id = chk->rec.data.payloadtype;
5198 	dchkh->ch.chunk_length = htons(chk->send_size);
5199 	/*
5200 	 * Now advance the chk->send_size by the actual pad needed.
5201 	 */
5202 	if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
5203 		/* need a pad */
5204 		struct mbuf *lm;
5205 		int pads;
5206 
5207 		pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
5208 		if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
5209 			chk->pad_inplace = 1;
5210 		}
5211 		if ((lm = chk->last_mbuf->m_next) != NULL) {
5212 			/* pad added an mbuf */
5213 			chk->last_mbuf = lm;
5214 		}
5215 		if (chk->data->m_flags & M_PKTHDR) {
5216 			chk->data->m_pkthdr.len += pads;
5217 		}
5218 		chk->send_size += pads;
5219 	}
5220 	/* We only re-set the policy if it is on */
5221 	if (sp->pr_sctp_on)
5222 		sctp_set_prsctp_policy(stcb, sp);
5223 
5224 	if (sp->msg_is_complete && (sp->length == 0)) {
5225 		/* All done pull and kill the message */
5226 		asoc->stream_queue_cnt--;
5227 		TAILQ_REMOVE(&strq->outqueue, sp, next);
5228 		sctp_free_remote_addr(sp->net);
5229 		if (sp->data) {
5230 			sctp_m_freem(sp->data);
5231 			sp->data = NULL;
5232 		}
5233 		sctp_free_a_strmoq(stcb, sp);
5234 
5235 		/* we can't be locked to it */
5236 		*locked = 0;
5237 		stcb->asoc.locked_on_sending = NULL;
5238 	} else {
5239 		/* more to go, we are locked */
5240 		*locked = 1;
5241 	}
5242 	asoc->chunks_on_out_queue++;
5243 	if (sp->pr_sctp_on) {
5244 		asoc->pr_sctp_cnt++;
5245 		chk->pr_sctp_on = 1;
5246 	} else {
5247 		chk->pr_sctp_on = 0;
5248 	}
5249 	TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
5250 	asoc->send_queue_cnt++;
5251 	SCTP_TCB_SEND_UNLOCK(stcb);
5252 	return (to_move);
5253 }
5254 
5255 
5256 static struct sctp_stream_out *
5257 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
5258 {
5259 	struct sctp_stream_out *strq;
5260 
5261 	/* Find the next stream to use */
5262 	if (asoc->last_out_stream == NULL) {
5263 		strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
5264 		if (asoc->last_out_stream == NULL) {
5265 			/* huh nothing on the wheel, TSNH */
5266 			return (NULL);
5267 		}
5268 		goto done_it;
5269 	}
5270 	strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
5271 done_it:
5272 	if (strq == NULL) {
5273 		strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
5274 	}
5275 	return (strq);
5276 
5277 }
5278 
5279 static void
5280 sctp_fill_outqueue(struct sctp_tcb *stcb,
5281     struct sctp_nets *net, int frag_point, int eeor_mode)
5282 {
5283 	struct sctp_association *asoc;
5284 	struct sctp_stream_out *strq, *strqn;
5285 	int goal_mtu, moved_how_much, total_moved = 0;
5286 	int locked, giveup;
5287 	struct sctp_stream_queue_pending *sp;
5288 
5289 	SCTP_TCB_LOCK_ASSERT(stcb);
5290 	asoc = &stcb->asoc;
5291 #ifdef AF_INET6
5292 	if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
5293 		goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
5294 	} else {
5295 		/* ?? not sure what else to do */
5296 		goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
5297 	}
5298 #else
5299 	goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
5300 	mtu_fromwheel = 0;
5301 #endif
5302 	/* Need an allowance for the data chunk header too */
5303 	goal_mtu -= sizeof(struct sctp_data_chunk);
5304 
5305 	/* must make even word boundary */
5306 	goal_mtu &= 0xfffffffc;
5307 	if (asoc->locked_on_sending) {
5308 		/* We are stuck on one stream until the message completes. */
5309 		strqn = strq = asoc->locked_on_sending;
5310 		locked = 1;
5311 	} else {
5312 		strqn = strq = sctp_select_a_stream(stcb, asoc);
5313 		locked = 0;
5314 	}
5315 
5316 	while ((goal_mtu > 0) && strq) {
5317 		sp = TAILQ_FIRST(&strq->outqueue);
5318 		/*
5319 		 * If CMT is off, we must validate that the stream in
5320 		 * question has the first item pointed towards are network
5321 		 * destionation requested by the caller. Note that if we
5322 		 * turn out to be locked to a stream (assigning TSN's then
5323 		 * we must stop, since we cannot look for another stream
5324 		 * with data to send to that destination). In CMT's case, by
5325 		 * skipping this check, we will send one data packet towards
5326 		 * the requested net.
5327 		 */
5328 		if (sp == NULL) {
5329 			break;
5330 		}
5331 		if ((sp->net != net) && (sctp_cmt_on_off == 0)) {
5332 			/* none for this network */
5333 			if (locked) {
5334 				break;
5335 			} else {
5336 				strq = sctp_select_a_stream(stcb, asoc);
5337 				if (strq == NULL)
5338 					/* none left */
5339 					break;
5340 				if (strqn == strq) {
5341 					/* I have circled */
5342 					break;
5343 				}
5344 				continue;
5345 			}
5346 		}
5347 		giveup = 0;
5348 		moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
5349 		    &giveup, eeor_mode);
5350 		asoc->last_out_stream = strq;
5351 		if (locked) {
5352 			asoc->locked_on_sending = strq;
5353 			if ((moved_how_much == 0) || (giveup))
5354 				/* no more to move for now */
5355 				break;
5356 		} else {
5357 			asoc->locked_on_sending = NULL;
5358 			if (TAILQ_FIRST(&strq->outqueue) == NULL) {
5359 				sctp_remove_from_wheel(stcb, asoc, strq);
5360 			}
5361 			if (giveup) {
5362 				break;
5363 			}
5364 			strq = sctp_select_a_stream(stcb, asoc);
5365 			if (strq == NULL) {
5366 				break;
5367 			}
5368 		}
5369 		total_moved += moved_how_much;
5370 		goal_mtu -= moved_how_much;
5371 		goal_mtu &= 0xfffffffc;
5372 	}
5373 	if (total_moved == 0) {
5374 		if ((sctp_cmt_on_off == 0) &&
5375 		    (net == stcb->asoc.primary_destination)) {
5376 			/* ran dry for primary network net */
5377 			SCTP_STAT_INCR(sctps_primary_randry);
5378 		} else if (sctp_cmt_on_off) {
5379 			/* ran dry with CMT on */
5380 			SCTP_STAT_INCR(sctps_cmt_randry);
5381 		}
5382 	}
5383 }
5384 
5385 __inline void
5386 sctp_fix_ecn_echo(struct sctp_association *asoc)
5387 {
5388 	struct sctp_tmit_chunk *chk;
5389 
5390 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
5391 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
5392 			chk->sent = SCTP_DATAGRAM_UNSENT;
5393 		}
5394 	}
5395 }
5396 
5397 static void
5398 sctp_move_to_an_alt(struct sctp_tcb *stcb,
5399     struct sctp_association *asoc,
5400     struct sctp_nets *net)
5401 {
5402 	struct sctp_tmit_chunk *chk;
5403 	struct sctp_nets *a_net;
5404 
5405 	SCTP_TCB_LOCK_ASSERT(stcb);
5406 	a_net = sctp_find_alternate_net(stcb, net, 0);
5407 	if ((a_net != net) &&
5408 	    ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
5409 		/*
5410 		 * We only proceed if a valid alternate is found that is not
5411 		 * this one and is reachable. Here we must move all chunks
5412 		 * queued in the send queue off of the destination address
5413 		 * to our alternate.
5414 		 */
5415 		TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
5416 			if (chk->whoTo == net) {
5417 				/* Move the chunk to our alternate */
5418 				sctp_free_remote_addr(chk->whoTo);
5419 				chk->whoTo = a_net;
5420 				atomic_add_int(&a_net->ref_count, 1);
5421 			}
5422 		}
5423 	}
5424 }
5425 
5426 extern int sctp_early_fr;
5427 
5428 int
5429 sctp_med_chunk_output(struct sctp_inpcb *inp,
5430     struct sctp_tcb *stcb,
5431     struct sctp_association *asoc,
5432     int *num_out,
5433     int *reason_code,
5434     int control_only, int *cwnd_full, int from_where,
5435     struct timeval *now, int *now_filled, int frag_point)
5436 {
5437 	/*
5438 	 * Ok this is the generic chunk service queue. we must do the
5439 	 * following: - Service the stream queue that is next, moving any
5440 	 * message (note I must get a complete message i.e. FIRST/MIDDLE and
5441 	 * LAST to the out queue in one pass) and assigning TSN's - Check to
5442 	 * see if the cwnd/rwnd allows any output, if so we go ahead and
5443 	 * fomulate and send the low level chunks. Making sure to combine
5444 	 * any control in the control chunk queue also.
5445 	 */
5446 	struct sctp_nets *net;
5447 	struct mbuf *outchain, *endoutchain;
5448 	struct sctp_tmit_chunk *chk, *nchk;
5449 	struct sctphdr *shdr;
5450 
5451 	/* temp arrays for unlinking */
5452 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
5453 	int no_fragmentflg, error;
5454 	int one_chunk, hbflag;
5455 	int asconf, cookie, no_out_cnt;
5456 	int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind, eeor_mode;
5457 	unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
5458 
5459 	*num_out = 0;
5460 	struct sctp_nets *start_at, *old_startat = NULL, *send_start_at;
5461 
5462 	cwnd_full_ind = 0;
5463 	int tsns_sent = 0;
5464 	uint32_t auth_offset = 0;
5465 	struct sctp_auth_chunk *auth = NULL;
5466 
5467 	if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
5468 	    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
5469 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
5470 		eeor_mode = 1;
5471 	} else {
5472 		eeor_mode = 0;
5473 	}
5474 	ctl_cnt = no_out_cnt = asconf = cookie = 0;
5475 	/*
5476 	 * First lets prime the pump. For each destination, if there is room
5477 	 * in the flight size, attempt to pull an MTU's worth out of the
5478 	 * stream queues into the general send_queue
5479 	 */
5480 #ifdef SCTP_AUDITING_ENABLED
5481 	sctp_audit_log(0xC2, 2);
5482 #endif
5483 	SCTP_TCB_LOCK_ASSERT(stcb);
5484 	hbflag = 0;
5485 	if ((control_only) || (asoc->stream_reset_outstanding))
5486 		no_data_chunks = 1;
5487 	else
5488 		no_data_chunks = 0;
5489 
5490 	/* Nothing to possible to send? */
5491 	if (TAILQ_EMPTY(&asoc->control_send_queue) &&
5492 	    TAILQ_EMPTY(&asoc->send_queue) &&
5493 	    TAILQ_EMPTY(&asoc->out_wheel)) {
5494 		*reason_code = 9;
5495 		return (0);
5496 	}
5497 	if (asoc->peers_rwnd == 0) {
5498 		/* No room in peers rwnd */
5499 		*cwnd_full = 1;
5500 		*reason_code = 1;
5501 		if (asoc->total_flight > 0) {
5502 			/* we are allowed one chunk in flight */
5503 			no_data_chunks = 1;
5504 		}
5505 	}
5506 	if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
5507 		if (sctp_cmt_on_off) {
5508 			/*
5509 			 * for CMT we start at the next one past the one we
5510 			 * last added data to.
5511 			 */
5512 			if (TAILQ_FIRST(&asoc->send_queue) != NULL) {
5513 				goto skip_the_fill_from_streams;
5514 			}
5515 			if (asoc->last_net_data_came_from) {
5516 				net = TAILQ_NEXT(asoc->last_net_data_came_from, sctp_next);
5517 				if (net == NULL) {
5518 					net = TAILQ_FIRST(&asoc->nets);
5519 				}
5520 			} else {
5521 				/* back to start */
5522 				net = TAILQ_FIRST(&asoc->nets);
5523 			}
5524 
5525 		} else {
5526 			net = asoc->primary_destination;
5527 			if (net == NULL) {
5528 				/* TSNH */
5529 				net = TAILQ_FIRST(&asoc->nets);
5530 			}
5531 		}
5532 		start_at = net;
5533 one_more_time:
5534 		for (; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
5535 			if (old_startat && (old_startat == net)) {
5536 				break;
5537 			}
5538 			if ((sctp_cmt_on_off == 0) && (net->ref_count < 2)) {
5539 				/* nothing can be in queue for this guy */
5540 				continue;
5541 			}
5542 			if (net->flight_size >= net->cwnd) {
5543 				/* skip this network, no room */
5544 				cwnd_full_ind++;
5545 				continue;
5546 			}
5547 			/*
5548 			 * @@@ JRI : this for loop we are in takes in each
5549 			 * net, if its's got space in cwnd and has data sent
5550 			 * to it (when CMT is off) then it calls
5551 			 * sctp_fill_outqueue for the net. This gets data on
5552 			 * the send queue for that network.
5553 			 *
5554 			 * In sctp_fill_outqueue TSN's are assigned and data is
5555 			 * copied out of the stream buffers. Note mostly
5556 			 * copy by reference (we hope).
5557 			 */
5558 #ifdef SCTP_CWND_LOGGING
5559 			sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
5560 #endif
5561 			sctp_fill_outqueue(stcb, net, frag_point, eeor_mode);
5562 		}
5563 		if (start_at != TAILQ_FIRST(&asoc->nets)) {
5564 			/* got to pick up the beginning stuff. */
5565 			old_startat = start_at;
5566 			start_at = net = TAILQ_FIRST(&asoc->nets);
5567 			goto one_more_time;
5568 		}
5569 	}
5570 skip_the_fill_from_streams:
5571 	*cwnd_full = cwnd_full_ind;
5572 	/* now service each destination and send out what we can for it */
5573 	/* Nothing to send? */
5574 	if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
5575 	    (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
5576 		*reason_code = 8;
5577 		return (0);
5578 	}
5579 	chk = TAILQ_FIRST(&asoc->send_queue);
5580 	if (chk) {
5581 		send_start_at = chk->whoTo;
5582 	} else {
5583 		send_start_at = TAILQ_FIRST(&asoc->nets);
5584 	}
5585 	old_startat = NULL;
5586 again_one_more_time:
5587 	for (net = send_start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
5588 		/* how much can we send? */
5589 		/* printf("Examine for sending net:%x\n", (uint32_t)net); */
5590 		if (old_startat && (old_startat == net)) {
5591 			/* through list ocmpletely. */
5592 			break;
5593 		}
5594 		tsns_sent = 0;
5595 		if (net->ref_count < 2) {
5596 			/*
5597 			 * Ref-count of 1 so we cannot have data or control
5598 			 * queued to this address. Skip it.
5599 			 */
5600 			continue;
5601 		}
5602 		ctl_cnt = bundle_at = 0;
5603 		endoutchain = outchain = NULL;
5604 		no_fragmentflg = 1;
5605 		one_chunk = 0;
5606 
5607 		if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
5608 			/*
5609 			 * if we have a route and an ifp check to see if we
5610 			 * have room to send to this guy
5611 			 */
5612 			struct ifnet *ifp;
5613 
5614 			ifp = net->ro.ro_rt->rt_ifp;
5615 			if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
5616 				SCTP_STAT_INCR(sctps_ifnomemqueued);
5617 #ifdef SCTP_LOG_MAXBURST
5618 				sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
5619 #endif
5620 				continue;
5621 			}
5622 		}
5623 		if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
5624 			mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
5625 		} else {
5626 			mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
5627 		}
5628 		mx_mtu = mtu;
5629 		to_out = 0;
5630 		if (mtu > asoc->peers_rwnd) {
5631 			if (asoc->total_flight > 0) {
5632 				/* We have a packet in flight somewhere */
5633 				r_mtu = asoc->peers_rwnd;
5634 			} else {
5635 				/* We are always allowed to send one MTU out */
5636 				one_chunk = 1;
5637 				r_mtu = mtu;
5638 			}
5639 		} else {
5640 			r_mtu = mtu;
5641 		}
5642 		/************************/
5643 		/* Control transmission */
5644 		/************************/
5645 		/* Now first lets go through the control queue */
5646 		for (chk = TAILQ_FIRST(&asoc->control_send_queue);
5647 		    chk; chk = nchk) {
5648 			nchk = TAILQ_NEXT(chk, sctp_next);
5649 			if (chk->whoTo != net) {
5650 				/*
5651 				 * No, not sent to the network we are
5652 				 * looking at
5653 				 */
5654 				continue;
5655 			}
5656 			if (chk->data == NULL) {
5657 				continue;
5658 			}
5659 			if ((chk->data->m_flags & M_PKTHDR) == 0) {
5660 				/*
5661 				 * NOTE: the chk queue MUST have the PKTHDR
5662 				 * flag set on it with a total in the
5663 				 * m_pkthdr.len field!! else the chunk will
5664 				 * ALWAYS be skipped
5665 				 */
5666 				continue;
5667 			}
5668 			if (chk->sent != SCTP_DATAGRAM_UNSENT) {
5669 				/*
5670 				 * It must be unsent. Cookies and ASCONF's
5671 				 * hang around but there timers will force
5672 				 * when marked for resend.
5673 				 */
5674 				continue;
5675 			}
5676 			/*
5677 			 * if no AUTH is yet included and this chunk
5678 			 * requires it, make sure to account for it.  We
5679 			 * don't apply the size until the AUTH chunk is
5680 			 * actually added below in case there is no room for
5681 			 * this chunk. NOTE: we overload the use of "omtu"
5682 			 * here
5683 			 */
5684 			if ((auth == NULL) &&
5685 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
5686 			    stcb->asoc.peer_auth_chunks)) {
5687 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5688 			} else
5689 				omtu = 0;
5690 			/* Here we do NOT factor the r_mtu */
5691 			if ((chk->data->m_pkthdr.len < (int)(mtu - omtu)) ||
5692 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
5693 				/*
5694 				 * We probably should glom the mbuf chain
5695 				 * from the chk->data for control but the
5696 				 * problem is it becomes yet one more level
5697 				 * of tracking to do if for some reason
5698 				 * output fails. Then I have got to
5699 				 * reconstruct the merged control chain.. el
5700 				 * yucko.. for now we take the easy way and
5701 				 * do the copy
5702 				 */
5703 				/*
5704 				 * Add an AUTH chunk, if chunk requires it
5705 				 * save the offset into the chain for AUTH
5706 				 */
5707 				if ((auth == NULL) &&
5708 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
5709 				    stcb->asoc.peer_auth_chunks))) {
5710 					outchain = sctp_add_auth_chunk(outchain,
5711 					    &endoutchain,
5712 					    &auth,
5713 					    &auth_offset,
5714 					    stcb,
5715 					    chk->rec.chunk_id.id);
5716 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5717 				}
5718 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
5719 				    (int)chk->rec.chunk_id.can_take_data,
5720 				    chk->data->m_pkthdr.len, chk->copy_by_ref);
5721 				if (outchain == NULL) {
5722 					*reason_code = 8;
5723 					return (ENOMEM);
5724 				}
5725 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5726 				/* update our MTU size */
5727 				if (mtu > (chk->data->m_pkthdr.len + omtu))
5728 					mtu -= (chk->data->m_pkthdr.len + omtu);
5729 				else
5730 					mtu = 0;
5731 				to_out += (chk->data->m_pkthdr.len + omtu);
5732 				/* Do clear IP_DF ? */
5733 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
5734 					no_fragmentflg = 0;
5735 				}
5736 				if (chk->rec.chunk_id.can_take_data)
5737 					chk->data = NULL;
5738 				/* Mark things to be removed, if needed */
5739 				if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
5740 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
5741 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
5742 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
5743 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
5744 				    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
5745 				    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
5746 				    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
5747 				    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
5748 				    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
5749 
5750 					if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST)
5751 						hbflag = 1;
5752 					/* remove these chunks at the end */
5753 					if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
5754 						/* turn off the timer */
5755 						if (callout_pending(&stcb->asoc.dack_timer.timer)) {
5756 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
5757 							    inp, stcb, net);
5758 						}
5759 					}
5760 					ctl_cnt++;
5761 				} else {
5762 					/*
5763 					 * Other chunks, since they have
5764 					 * timers running (i.e. COOKIE or
5765 					 * ASCONF) we just "trust" that it
5766 					 * gets sent or retransmitted.
5767 					 */
5768 					ctl_cnt++;
5769 					if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
5770 						cookie = 1;
5771 						no_out_cnt = 1;
5772 					} else if (chk->rec.chunk_id.id == SCTP_ASCONF) {
5773 						/*
5774 						 * set hb flag since we can
5775 						 * use these for RTO
5776 						 */
5777 						hbflag = 1;
5778 						asconf = 1;
5779 					}
5780 					chk->sent = SCTP_DATAGRAM_SENT;
5781 					chk->snd_count++;
5782 				}
5783 				if (mtu == 0) {
5784 					/*
5785 					 * Ok we are out of room but we can
5786 					 * output without effecting the
5787 					 * flight size since this little guy
5788 					 * is a control only packet.
5789 					 */
5790 					if (asconf) {
5791 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
5792 						asconf = 0;
5793 					}
5794 					if (cookie) {
5795 						sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
5796 						cookie = 0;
5797 					}
5798 					M_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
5799 					if (outchain == NULL) {
5800 						/* no memory */
5801 						error = ENOBUFS;
5802 						goto error_out_again;
5803 					}
5804 					shdr = mtod(outchain, struct sctphdr *);
5805 					shdr->src_port = inp->sctp_lport;
5806 					shdr->dest_port = stcb->rport;
5807 					shdr->v_tag = htonl(stcb->asoc.peer_vtag);
5808 					shdr->checksum = 0;
5809 					auth_offset += sizeof(struct sctphdr);
5810 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
5811 					    (struct sockaddr *)&net->ro._l_addr,
5812 					    outchain, auth_offset, auth,
5813 					    no_fragmentflg, 0, NULL, asconf))) {
5814 						if (error == ENOBUFS) {
5815 							asoc->ifp_had_enobuf = 1;
5816 						}
5817 						SCTP_STAT_INCR(sctps_lowlevelerr);
5818 						if (from_where == 0) {
5819 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
5820 						}
5821 				error_out_again:
5822 						/* error, could not output */
5823 						if (hbflag) {
5824 							if (*now_filled == 0) {
5825 								SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5826 								*now_filled = 1;
5827 								*now = net->last_sent_time;
5828 							} else {
5829 								net->last_sent_time = *now;
5830 							}
5831 							hbflag = 0;
5832 						}
5833 						if (error == EHOSTUNREACH) {
5834 							/*
5835 							 * Destination went
5836 							 * unreachable
5837 							 * during this send
5838 							 */
5839 							sctp_move_to_an_alt(stcb, asoc, net);
5840 						}
5841 						sctp_clean_up_ctl(stcb, asoc);
5842 						*reason_code = 7;
5843 						return (error);
5844 					} else
5845 						asoc->ifp_had_enobuf = 0;
5846 					/* Only HB or ASCONF advances time */
5847 					if (hbflag) {
5848 						if (*now_filled == 0) {
5849 							SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5850 							*now_filled = 1;
5851 							*now = net->last_sent_time;
5852 						} else {
5853 							net->last_sent_time = *now;
5854 						}
5855 						hbflag = 0;
5856 					}
5857 					/*
5858 					 * increase the number we sent, if a
5859 					 * cookie is sent we don't tell them
5860 					 * any was sent out.
5861 					 */
5862 					outchain = endoutchain = NULL;
5863 					auth = NULL;
5864 					auth_offset = 0;
5865 					if (!no_out_cnt)
5866 						*num_out += ctl_cnt;
5867 					/* recalc a clean slate and setup */
5868 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5869 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
5870 					} else {
5871 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
5872 					}
5873 					to_out = 0;
5874 					no_fragmentflg = 1;
5875 				}
5876 			}
5877 		}
5878 		/*********************/
5879 		/* Data transmission */
5880 		/*********************/
5881 		/*
5882 		 * if AUTH for DATA is required and no AUTH has been added
5883 		 * yet, account for this in the mtu now... if no data can be
5884 		 * bundled, this adjustment won't matter anyways since the
5885 		 * packet will be going out...
5886 		 */
5887 		if ((auth == NULL) &&
5888 		    sctp_auth_is_required_chunk(SCTP_DATA,
5889 		    stcb->asoc.peer_auth_chunks)) {
5890 			mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5891 		}
5892 		/* now lets add any data within the MTU constraints */
5893 		if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
5894 			if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
5895 				omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
5896 			else
5897 				omtu = 0;
5898 		} else {
5899 			if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
5900 				omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
5901 			else
5902 				omtu = 0;
5903 		}
5904 		if (((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) ||
5905 		    (cookie)) {
5906 			for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
5907 				if (no_data_chunks) {
5908 					/* let only control go out */
5909 					*reason_code = 1;
5910 					break;
5911 				}
5912 				if (net->flight_size >= net->cwnd) {
5913 					/* skip this net, no room for data */
5914 					*reason_code = 2;
5915 					break;
5916 				}
5917 				nchk = TAILQ_NEXT(chk, sctp_next);
5918 				if (chk->whoTo != net) {
5919 					/* No, not sent to this net */
5920 					continue;
5921 				}
5922 				if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
5923 					/*
5924 					 * strange, we have a chunk that is
5925 					 * to bit for its destination and
5926 					 * yet no fragment ok flag.
5927 					 * Something went wrong when the
5928 					 * PMTU changed...we did not mark
5929 					 * this chunk for some reason?? I
5930 					 * will fix it here by letting IP
5931 					 * fragment it for now and printing
5932 					 * a warning. This really should not
5933 					 * happen ...
5934 					 */
5935 #ifdef SCTP_DEBUG
5936 					printf("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
5937 					    chk->send_size, mtu);
5938 #endif
5939 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
5940 				}
5941 				if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
5942 				    ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
5943 					/* ok we will add this one */
5944 
5945 					/*
5946 					 * Add an AUTH chunk, if chunk
5947 					 * requires it, save the offset into
5948 					 * the chain for AUTH
5949 					 */
5950 					if ((auth == NULL) &&
5951 					    (sctp_auth_is_required_chunk(SCTP_DATA,
5952 					    stcb->asoc.peer_auth_chunks))) {
5953 
5954 						outchain = sctp_add_auth_chunk(outchain,
5955 						    &endoutchain,
5956 						    &auth,
5957 						    &auth_offset,
5958 						    stcb,
5959 						    SCTP_DATA);
5960 						SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5961 					}
5962 					outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
5963 					    chk->send_size, chk->copy_by_ref);
5964 					if (outchain == NULL) {
5965 #ifdef SCTP_DEBUG
5966 						if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5967 							printf("No memory?\n");
5968 						}
5969 #endif
5970 						if (!callout_pending(&net->rxt_timer.timer)) {
5971 							sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
5972 						}
5973 						*reason_code = 3;
5974 						return (ENOMEM);
5975 					}
5976 					/* upate our MTU size */
5977 					/* Do clear IP_DF ? */
5978 					if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
5979 						no_fragmentflg = 0;
5980 					}
5981 					/* unsigned subtraction of mtu */
5982 					if (mtu > chk->send_size)
5983 						mtu -= chk->send_size;
5984 					else
5985 						mtu = 0;
5986 					/* unsigned subtraction of r_mtu */
5987 					if (r_mtu > chk->send_size)
5988 						r_mtu -= chk->send_size;
5989 					else
5990 						r_mtu = 0;
5991 
5992 					to_out += chk->send_size;
5993 					if (to_out > mx_mtu) {
5994 #ifdef INVARIENT
5995 						panic("gag");
5996 #else
5997 						printf("Exceeding mtu of %d out size is %d\n",
5998 						    mx_mtu, to_out);
5999 #endif
6000 					}
6001 					data_list[bundle_at++] = chk;
6002 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
6003 						mtu = 0;
6004 						break;
6005 					}
6006 					if (chk->sent == SCTP_DATAGRAM_UNSENT) {
6007 						if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
6008 							SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
6009 						} else {
6010 							SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
6011 						}
6012 						if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
6013 						    ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
6014 							SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
6015 					}
6016 					if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
6017 						break;
6018 					}
6019 				} else {
6020 					/*
6021 					 * Must be sent in order of the
6022 					 * TSN's (on a network)
6023 					 */
6024 					break;
6025 				}
6026 			}	/* for () */
6027 		}		/* if asoc.state OPEN */
6028 		/* Is there something to send for this destination? */
6029 		if (outchain) {
6030 			/* We may need to start a control timer or two */
6031 			if (asconf) {
6032 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
6033 				asconf = 0;
6034 			}
6035 			if (cookie) {
6036 				sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
6037 				cookie = 0;
6038 			}
6039 			/* must start a send timer if data is being sent */
6040 			if (bundle_at && (!callout_pending(&net->rxt_timer.timer))) {
6041 				/*
6042 				 * no timer running on this destination
6043 				 * restart it.
6044 				 */
6045 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6046 			}
6047 			/* Now send it, if there is anything to send :> */
6048 			M_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
6049 			if (outchain == NULL) {
6050 				/* out of mbufs */
6051 				error = ENOBUFS;
6052 				goto errored_send;
6053 			}
6054 			shdr = mtod(outchain, struct sctphdr *);
6055 			shdr->src_port = inp->sctp_lport;
6056 			shdr->dest_port = stcb->rport;
6057 			shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6058 			shdr->checksum = 0;
6059 			auth_offset += sizeof(struct sctphdr);
6060 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
6061 			    (struct sockaddr *)&net->ro._l_addr,
6062 			    outchain,
6063 			    auth_offset,
6064 			    auth,
6065 			    no_fragmentflg,
6066 			    bundle_at,
6067 			    data_list[0],
6068 			    asconf))) {
6069 				/* error, we could not output */
6070 				if (error == ENOBUFS) {
6071 					asoc->ifp_had_enobuf = 1;
6072 				}
6073 				SCTP_STAT_INCR(sctps_lowlevelerr);
6074 				if (from_where == 0) {
6075 					SCTP_STAT_INCR(sctps_lowlevelerrusr);
6076 				}
6077 		errored_send:
6078 #ifdef SCTP_DEBUG
6079 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6080 					printf("Gak send error %d\n", error);
6081 				}
6082 #endif
6083 				if (hbflag) {
6084 					if (*now_filled == 0) {
6085 						SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
6086 						*now_filled = 1;
6087 						*now = net->last_sent_time;
6088 					} else {
6089 						net->last_sent_time = *now;
6090 					}
6091 					hbflag = 0;
6092 				}
6093 				if (error == EHOSTUNREACH) {
6094 					/*
6095 					 * Destination went unreachable
6096 					 * during this send
6097 					 */
6098 					sctp_move_to_an_alt(stcb, asoc, net);
6099 				}
6100 				sctp_clean_up_ctl(stcb, asoc);
6101 				*reason_code = 6;
6102 				return (error);
6103 			} else {
6104 				asoc->ifp_had_enobuf = 0;
6105 			}
6106 			outchain = endoutchain = NULL;
6107 			auth = NULL;
6108 			auth_offset = 0;
6109 			if (bundle_at || hbflag) {
6110 				/* For data/asconf and hb set time */
6111 				if (*now_filled == 0) {
6112 					SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
6113 					*now_filled = 1;
6114 					*now = net->last_sent_time;
6115 				} else {
6116 					net->last_sent_time = *now;
6117 				}
6118 			}
6119 			if (!no_out_cnt) {
6120 				*num_out += (ctl_cnt + bundle_at);
6121 			}
6122 			if (bundle_at) {
6123 				/* if (!net->rto_pending) { */
6124 				/* setup for a RTO measurement */
6125 				/* net->rto_pending = 1; */
6126 				tsns_sent = data_list[0]->rec.data.TSN_seq;
6127 
6128 				data_list[0]->do_rtt = 1;
6129 				/* } else { */
6130 				/* data_list[0]->do_rtt = 0; */
6131 				/* } */
6132 				SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
6133 				sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
6134 				if (sctp_early_fr) {
6135 					if (net->flight_size < net->cwnd) {
6136 						/* start or restart it */
6137 						if (callout_pending(&net->fr_timer.timer)) {
6138 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
6139 						}
6140 						SCTP_STAT_INCR(sctps_earlyfrstrout);
6141 						sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
6142 					} else {
6143 						/* stop it if its running */
6144 						if (callout_pending(&net->fr_timer.timer)) {
6145 							SCTP_STAT_INCR(sctps_earlyfrstpout);
6146 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
6147 						}
6148 					}
6149 				}
6150 			}
6151 			if (one_chunk) {
6152 				break;
6153 			}
6154 		}
6155 #ifdef SCTP_CWND_LOGGING
6156 		sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
6157 #endif
6158 	}
6159 	if (old_startat == NULL) {
6160 		old_startat = send_start_at;
6161 		send_start_at = TAILQ_FIRST(&asoc->nets);
6162 		goto again_one_more_time;
6163 	}
6164 	/*
6165 	 * At the end there should be no NON timed chunks hanging on this
6166 	 * queue.
6167 	 */
6168 #ifdef SCTP_CWND_LOGGING
6169 	sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
6170 #endif
6171 	if ((*num_out == 0) && (*reason_code == 0)) {
6172 		*reason_code = 4;
6173 	} else {
6174 		*reason_code = 5;
6175 	}
6176 	sctp_clean_up_ctl(stcb, asoc);
6177 	return (0);
6178 }
6179 
6180 void
6181 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
6182 {
6183 	/*
6184 	 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
6185 	 * the control chunk queue.
6186 	 */
6187 	struct sctp_chunkhdr *hdr;
6188 	struct sctp_tmit_chunk *chk;
6189 	struct mbuf *mat;
6190 
6191 	SCTP_TCB_LOCK_ASSERT(stcb);
6192 	sctp_alloc_a_chunk(stcb, chk);
6193 	if (chk == NULL) {
6194 		/* no memory */
6195 		sctp_m_freem(op_err);
6196 		return;
6197 	}
6198 	chk->copy_by_ref = 0;
6199 	M_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
6200 	if (op_err == NULL) {
6201 		sctp_free_a_chunk(stcb, chk);
6202 		return;
6203 	}
6204 	chk->send_size = 0;
6205 	mat = op_err;
6206 	while (mat != NULL) {
6207 		chk->send_size += mat->m_len;
6208 		mat = mat->m_next;
6209 	}
6210 	chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
6211 	chk->rec.chunk_id.can_take_data = 1;
6212 	chk->sent = SCTP_DATAGRAM_UNSENT;
6213 	chk->snd_count = 0;
6214 	chk->flags = 0;
6215 	chk->asoc = &stcb->asoc;
6216 	chk->data = op_err;
6217 	chk->whoTo = chk->asoc->primary_destination;
6218 	atomic_add_int(&chk->whoTo->ref_count, 1);
6219 	hdr = mtod(op_err, struct sctp_chunkhdr *);
6220 	hdr->chunk_type = SCTP_OPERATION_ERROR;
6221 	hdr->chunk_flags = 0;
6222 	hdr->chunk_length = htons(chk->send_size);
6223 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
6224 	    chk,
6225 	    sctp_next);
6226 	chk->asoc->ctrl_queue_cnt++;
6227 }
6228 
6229 int
6230 sctp_send_cookie_echo(struct mbuf *m,
6231     int offset,
6232     struct sctp_tcb *stcb,
6233     struct sctp_nets *net)
6234 {
6235 	/*
6236 	 * pull out the cookie and put it at the front of the control chunk
6237 	 * queue.
6238 	 */
6239 	int at;
6240 	struct mbuf *cookie, *mat;
6241 	struct sctp_paramhdr parm, *phdr;
6242 	struct sctp_chunkhdr *hdr;
6243 	struct sctp_tmit_chunk *chk;
6244 	uint16_t ptype, plen;
6245 
6246 	/* First find the cookie in the param area */
6247 	cookie = NULL;
6248 	at = offset + sizeof(struct sctp_init_chunk);
6249 
6250 	SCTP_TCB_LOCK_ASSERT(stcb);
6251 	do {
6252 		phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
6253 		if (phdr == NULL) {
6254 			return (-3);
6255 		}
6256 		ptype = ntohs(phdr->param_type);
6257 		plen = ntohs(phdr->param_length);
6258 		if (ptype == SCTP_STATE_COOKIE) {
6259 			int pad;
6260 
6261 			/* found the cookie */
6262 			if ((pad = (plen % 4))) {
6263 				plen += 4 - pad;
6264 			}
6265 			cookie = sctp_m_copym(m, at, plen, M_DONTWAIT);
6266 			if (cookie == NULL) {
6267 				/* No memory */
6268 				return (-2);
6269 			}
6270 			break;
6271 		}
6272 		at += SCTP_SIZE32(plen);
6273 	} while (phdr);
6274 	if (cookie == NULL) {
6275 		/* Did not find the cookie */
6276 		return (-3);
6277 	}
6278 	/* ok, we got the cookie lets change it into a cookie echo chunk */
6279 
6280 	/* first the change from param to cookie */
6281 	hdr = mtod(cookie, struct sctp_chunkhdr *);
6282 	hdr->chunk_type = SCTP_COOKIE_ECHO;
6283 	hdr->chunk_flags = 0;
6284 	/* now we MUST have a PKTHDR on it */
6285 	if ((cookie->m_flags & M_PKTHDR) != M_PKTHDR) {
6286 		/* we hope this happens rarely */
6287 		mat = sctp_get_mbuf_for_msg(8, 1, M_DONTWAIT, 1, MT_HEADER);
6288 		if (mat == NULL) {
6289 			sctp_m_freem(cookie);
6290 			return (-4);
6291 		}
6292 		mat->m_len = 0;
6293 		mat->m_pkthdr.rcvif = 0;
6294 		mat->m_next = cookie;
6295 		cookie = mat;
6296 	}
6297 	cookie->m_pkthdr.len = plen;
6298 	/* get the chunk stuff now and place it in the FRONT of the queue */
6299 	sctp_alloc_a_chunk(stcb, chk);
6300 	if (chk == NULL) {
6301 		/* no memory */
6302 		sctp_m_freem(cookie);
6303 		return (-5);
6304 	}
6305 	chk->copy_by_ref = 0;
6306 	chk->send_size = cookie->m_pkthdr.len;
6307 	chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
6308 	chk->rec.chunk_id.can_take_data = 0;
6309 	chk->sent = SCTP_DATAGRAM_UNSENT;
6310 	chk->snd_count = 0;
6311 	chk->flags = 0;
6312 	chk->asoc = &stcb->asoc;
6313 	chk->data = cookie;
6314 	chk->whoTo = chk->asoc->primary_destination;
6315 	atomic_add_int(&chk->whoTo->ref_count, 1);
6316 	TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
6317 	chk->asoc->ctrl_queue_cnt++;
6318 	return (0);
6319 }
6320 
6321 void
6322 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
6323     struct mbuf *m,
6324     int offset,
6325     int chk_length,
6326     struct sctp_nets *net)
6327 {
6328 	/*
6329 	 * take a HB request and make it into a HB ack and send it.
6330 	 */
6331 	struct mbuf *outchain;
6332 	struct sctp_chunkhdr *chdr;
6333 	struct sctp_tmit_chunk *chk;
6334 
6335 
6336 	if (net == NULL)
6337 		/* must have a net pointer */
6338 		return;
6339 
6340 	outchain = sctp_m_copym(m, offset, chk_length, M_DONTWAIT);
6341 	if (outchain == NULL) {
6342 		/* gak out of memory */
6343 		return;
6344 	}
6345 	chdr = mtod(outchain, struct sctp_chunkhdr *);
6346 	chdr->chunk_type = SCTP_HEARTBEAT_ACK;
6347 	chdr->chunk_flags = 0;
6348 	if ((outchain->m_flags & M_PKTHDR) != M_PKTHDR) {
6349 		/* should not happen but we are cautious. */
6350 		struct mbuf *tmp;
6351 
6352 		tmp = sctp_get_mbuf_for_msg(1, 1, M_DONTWAIT, 1, MT_HEADER);
6353 		if (tmp == NULL) {
6354 			return;
6355 		}
6356 		tmp->m_len = 0;
6357 		tmp->m_pkthdr.rcvif = 0;
6358 		tmp->m_next = outchain;
6359 		outchain = tmp;
6360 	}
6361 	outchain->m_pkthdr.len = chk_length;
6362 	if (chk_length % 4) {
6363 		/* need pad */
6364 		uint32_t cpthis = 0;
6365 		int padlen;
6366 
6367 		padlen = 4 - (outchain->m_pkthdr.len % 4);
6368 		m_copyback(outchain, outchain->m_pkthdr.len, padlen,
6369 		    (caddr_t)&cpthis);
6370 	}
6371 	sctp_alloc_a_chunk(stcb, chk);
6372 	if (chk == NULL) {
6373 		/* no memory */
6374 		sctp_m_freem(outchain);
6375 		return;
6376 	}
6377 	chk->copy_by_ref = 0;
6378 	chk->send_size = chk_length;
6379 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
6380 	chk->rec.chunk_id.can_take_data = 1;
6381 	chk->sent = SCTP_DATAGRAM_UNSENT;
6382 	chk->snd_count = 0;
6383 	chk->flags = 0;
6384 	chk->asoc = &stcb->asoc;
6385 	chk->data = outchain;
6386 	chk->whoTo = net;
6387 	atomic_add_int(&chk->whoTo->ref_count, 1);
6388 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6389 	chk->asoc->ctrl_queue_cnt++;
6390 }
6391 
6392 int
6393 sctp_send_cookie_ack(struct sctp_tcb *stcb)
6394 {
6395 	/* formulate and queue a cookie-ack back to sender */
6396 	struct mbuf *cookie_ack;
6397 	struct sctp_chunkhdr *hdr;
6398 	struct sctp_tmit_chunk *chk;
6399 
6400 	cookie_ack = NULL;
6401 	SCTP_TCB_LOCK_ASSERT(stcb);
6402 
6403 	cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 1, M_DONTWAIT, 1, MT_HEADER);
6404 	if (cookie_ack == NULL) {
6405 		/* no mbuf's */
6406 		return (-1);
6407 	}
6408 	cookie_ack->m_data += SCTP_MIN_OVERHEAD;
6409 	sctp_alloc_a_chunk(stcb, chk);
6410 	if (chk == NULL) {
6411 		/* no memory */
6412 		sctp_m_freem(cookie_ack);
6413 		return (-1);
6414 	}
6415 	chk->copy_by_ref = 0;
6416 	chk->send_size = sizeof(struct sctp_chunkhdr);
6417 	chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
6418 	chk->rec.chunk_id.can_take_data = 1;
6419 	chk->sent = SCTP_DATAGRAM_UNSENT;
6420 	chk->snd_count = 0;
6421 	chk->flags = 0;
6422 	chk->asoc = &stcb->asoc;
6423 	chk->data = cookie_ack;
6424 	if (chk->asoc->last_control_chunk_from != NULL) {
6425 		chk->whoTo = chk->asoc->last_control_chunk_from;
6426 	} else {
6427 		chk->whoTo = chk->asoc->primary_destination;
6428 	}
6429 	atomic_add_int(&chk->whoTo->ref_count, 1);
6430 	hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
6431 	hdr->chunk_type = SCTP_COOKIE_ACK;
6432 	hdr->chunk_flags = 0;
6433 	hdr->chunk_length = htons(chk->send_size);
6434 	cookie_ack->m_pkthdr.len = cookie_ack->m_len = chk->send_size;
6435 	cookie_ack->m_pkthdr.rcvif = 0;
6436 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6437 	chk->asoc->ctrl_queue_cnt++;
6438 	return (0);
6439 }
6440 
6441 
6442 int
6443 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
6444 {
6445 	/* formulate and queue a SHUTDOWN-ACK back to the sender */
6446 	struct mbuf *m_shutdown_ack;
6447 	struct sctp_shutdown_ack_chunk *ack_cp;
6448 	struct sctp_tmit_chunk *chk;
6449 
6450 	m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 1, M_DONTWAIT, 1, MT_HEADER);
6451 	if (m_shutdown_ack == NULL) {
6452 		/* no mbuf's */
6453 		return (-1);
6454 	}
6455 	m_shutdown_ack->m_data += SCTP_MIN_OVERHEAD;
6456 	sctp_alloc_a_chunk(stcb, chk);
6457 	if (chk == NULL) {
6458 		/* no memory */
6459 		sctp_m_freem(m_shutdown_ack);
6460 		return (-1);
6461 	}
6462 	chk->copy_by_ref = 0;
6463 
6464 	chk->send_size = sizeof(struct sctp_chunkhdr);
6465 	chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
6466 	chk->rec.chunk_id.can_take_data = 1;
6467 	chk->sent = SCTP_DATAGRAM_UNSENT;
6468 	chk->snd_count = 0;
6469 	chk->flags = 0;
6470 	chk->asoc = &stcb->asoc;
6471 	chk->data = m_shutdown_ack;
6472 	chk->whoTo = net;
6473 	atomic_add_int(&net->ref_count, 1);
6474 
6475 	ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
6476 	ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
6477 	ack_cp->ch.chunk_flags = 0;
6478 	ack_cp->ch.chunk_length = htons(chk->send_size);
6479 	m_shutdown_ack->m_pkthdr.len = m_shutdown_ack->m_len = chk->send_size;
6480 	m_shutdown_ack->m_pkthdr.rcvif = 0;
6481 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6482 	chk->asoc->ctrl_queue_cnt++;
6483 	return (0);
6484 }
6485 
6486 int
6487 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
6488 {
6489 	/* formulate and queue a SHUTDOWN to the sender */
6490 	struct mbuf *m_shutdown;
6491 	struct sctp_shutdown_chunk *shutdown_cp;
6492 	struct sctp_tmit_chunk *chk;
6493 
6494 	m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 1, M_DONTWAIT, 1, MT_HEADER);
6495 	if (m_shutdown == NULL) {
6496 		/* no mbuf's */
6497 		return (-1);
6498 	}
6499 	m_shutdown->m_data += SCTP_MIN_OVERHEAD;
6500 	sctp_alloc_a_chunk(stcb, chk);
6501 	if (chk == NULL) {
6502 		/* no memory */
6503 		sctp_m_freem(m_shutdown);
6504 		return (-1);
6505 	}
6506 	chk->copy_by_ref = 0;
6507 	chk->send_size = sizeof(struct sctp_shutdown_chunk);
6508 	chk->rec.chunk_id.id = SCTP_SHUTDOWN;
6509 	chk->rec.chunk_id.can_take_data = 1;
6510 	chk->sent = SCTP_DATAGRAM_UNSENT;
6511 	chk->snd_count = 0;
6512 	chk->flags = 0;
6513 	chk->asoc = &stcb->asoc;
6514 	chk->data = m_shutdown;
6515 	chk->whoTo = net;
6516 	atomic_add_int(&net->ref_count, 1);
6517 
6518 	shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
6519 	shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
6520 	shutdown_cp->ch.chunk_flags = 0;
6521 	shutdown_cp->ch.chunk_length = htons(chk->send_size);
6522 	shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
6523 	m_shutdown->m_pkthdr.len = m_shutdown->m_len = chk->send_size;
6524 	m_shutdown->m_pkthdr.rcvif = 0;
6525 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6526 	chk->asoc->ctrl_queue_cnt++;
6527 	return (0);
6528 }
6529 
6530 int
6531 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net)
6532 {
6533 	/*
6534 	 * formulate and queue an ASCONF to the peer ASCONF parameters
6535 	 * should be queued on the assoc queue
6536 	 */
6537 	struct sctp_tmit_chunk *chk;
6538 	struct mbuf *m_asconf;
6539 	struct sctp_asconf_chunk *acp;
6540 
6541 
6542 	SCTP_TCB_LOCK_ASSERT(stcb);
6543 	/* compose an ASCONF chunk, maximum length is PMTU */
6544 	m_asconf = sctp_compose_asconf(stcb);
6545 	if (m_asconf == NULL) {
6546 		return (-1);
6547 	}
6548 	acp = mtod(m_asconf, struct sctp_asconf_chunk *);
6549 	sctp_alloc_a_chunk(stcb, chk);
6550 	if (chk == NULL) {
6551 		/* no memory */
6552 		sctp_m_freem(m_asconf);
6553 		return (-1);
6554 	}
6555 	chk->copy_by_ref = 0;
6556 	chk->data = m_asconf;
6557 	chk->send_size = m_asconf->m_pkthdr.len;
6558 	chk->rec.chunk_id.id = SCTP_ASCONF;
6559 	chk->rec.chunk_id.can_take_data = 0;
6560 	chk->sent = SCTP_DATAGRAM_UNSENT;
6561 	chk->snd_count = 0;
6562 	chk->flags = 0;
6563 	chk->asoc = &stcb->asoc;
6564 	chk->whoTo = chk->asoc->primary_destination;
6565 	atomic_add_int(&chk->whoTo->ref_count, 1);
6566 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6567 	chk->asoc->ctrl_queue_cnt++;
6568 	return (0);
6569 }
6570 
6571 int
6572 sctp_send_asconf_ack(struct sctp_tcb *stcb, uint32_t retrans)
6573 {
6574 	/*
6575 	 * formulate and queue a asconf-ack back to sender the asconf-ack
6576 	 * must be stored in the tcb
6577 	 */
6578 	struct sctp_tmit_chunk *chk;
6579 	struct mbuf *m_ack;
6580 
6581 	SCTP_TCB_LOCK_ASSERT(stcb);
6582 	/* is there a asconf-ack mbuf chain to send? */
6583 	if (stcb->asoc.last_asconf_ack_sent == NULL) {
6584 		return (-1);
6585 	}
6586 	/* copy the asconf_ack */
6587 	/*
6588 	 * Supposedly the m_copypacket is a optimzation, use it if we can.
6589 	 */
6590 	if (stcb->asoc.last_asconf_ack_sent->m_flags & M_PKTHDR) {
6591 		m_ack = m_copypacket(stcb->asoc.last_asconf_ack_sent, M_DONTWAIT);
6592 	} else
6593 		m_ack = m_copy(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL);
6594 
6595 	if (m_ack == NULL) {
6596 		/* couldn't copy it */
6597 
6598 		return (-1);
6599 	}
6600 	sctp_alloc_a_chunk(stcb, chk);
6601 	if (chk == NULL) {
6602 		/* no memory */
6603 		if (m_ack)
6604 			sctp_m_freem(m_ack);
6605 		return (-1);
6606 	}
6607 	chk->copy_by_ref = 0;
6608 	/* figure out where it goes to */
6609 	if (retrans) {
6610 		/* we're doing a retransmission */
6611 		if (stcb->asoc.used_alt_asconfack > 2) {
6612 			/* tried alternate nets already, go back */
6613 			chk->whoTo = NULL;
6614 		} else {
6615 			/* need to try and alternate net */
6616 			chk->whoTo = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
6617 			stcb->asoc.used_alt_asconfack++;
6618 		}
6619 		if (chk->whoTo == NULL) {
6620 			/* no alternate */
6621 			if (stcb->asoc.last_control_chunk_from == NULL)
6622 				chk->whoTo = stcb->asoc.primary_destination;
6623 			else
6624 				chk->whoTo = stcb->asoc.last_control_chunk_from;
6625 			stcb->asoc.used_alt_asconfack = 0;
6626 		}
6627 	} else {
6628 		/* normal case */
6629 		if (stcb->asoc.last_control_chunk_from == NULL)
6630 			chk->whoTo = stcb->asoc.primary_destination;
6631 		else
6632 			chk->whoTo = stcb->asoc.last_control_chunk_from;
6633 		stcb->asoc.used_alt_asconfack = 0;
6634 	}
6635 	chk->data = m_ack;
6636 	chk->send_size = m_ack->m_pkthdr.len;
6637 	chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
6638 	chk->rec.chunk_id.can_take_data = 1;
6639 	chk->sent = SCTP_DATAGRAM_UNSENT;
6640 	chk->snd_count = 0;
6641 	chk->flags = 0;
6642 	chk->asoc = &stcb->asoc;
6643 	atomic_add_int(&chk->whoTo->ref_count, 1);
6644 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6645 	chk->asoc->ctrl_queue_cnt++;
6646 	return (0);
6647 }
6648 
6649 
6650 static int
6651 sctp_chunk_retransmission(struct sctp_inpcb *inp,
6652     struct sctp_tcb *stcb,
6653     struct sctp_association *asoc,
6654     int *cnt_out, struct timeval *now, int *now_filled)
6655 {
6656 	/*
6657 	 * send out one MTU of retransmission. If fast_retransmit is
6658 	 * happening we ignore the cwnd. Otherwise we obey the cwnd and
6659 	 * rwnd. For a Cookie or Asconf in the control chunk queue we
6660 	 * retransmit them by themselves.
6661 	 *
6662 	 * For data chunks we will pick out the lowest TSN's in the sent_queue
6663 	 * marked for resend and bundle them all together (up to a MTU of
6664 	 * destination). The address to send to should have been
6665 	 * selected/changed where the retransmission was marked (i.e. in FR
6666 	 * or t3-timeout routines).
6667 	 */
6668 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
6669 	struct sctp_tmit_chunk *chk, *fwd;
6670 	struct mbuf *m, *endofchain;
6671 	struct sctphdr *shdr;
6672 	int asconf;
6673 	struct sctp_nets *net;
6674 	uint32_t tsns_sent = 0;
6675 	int no_fragmentflg, bundle_at, cnt_thru;
6676 	unsigned int mtu;
6677 	int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
6678 	struct sctp_auth_chunk *auth = NULL;
6679 	uint32_t auth_offset = 0;
6680 	uint32_t dmtu = 0;
6681 
6682 	SCTP_TCB_LOCK_ASSERT(stcb);
6683 	tmr_started = ctl_cnt = bundle_at = error = 0;
6684 	no_fragmentflg = 1;
6685 	asconf = 0;
6686 	fwd_tsn = 0;
6687 	*cnt_out = 0;
6688 	fwd = NULL;
6689 	endofchain = m = NULL;
6690 #ifdef SCTP_AUDITING_ENABLED
6691 	sctp_audit_log(0xC3, 1);
6692 #endif
6693 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
6694 #ifdef SCTP_DEBUG
6695 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6696 			printf("SCTP hits empty queue with cnt set to %d?\n",
6697 			    asoc->sent_queue_retran_cnt);
6698 		}
6699 #endif
6700 		asoc->sent_queue_cnt = 0;
6701 		asoc->sent_queue_cnt_removeable = 0;
6702 	}
6703 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
6704 		if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
6705 		    (chk->rec.chunk_id.id == SCTP_ASCONF) ||
6706 		    (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
6707 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
6708 			if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6709 				if (chk != asoc->str_reset) {
6710 					/*
6711 					 * not eligible for retran if its
6712 					 * not ours
6713 					 */
6714 					continue;
6715 				}
6716 			}
6717 			ctl_cnt++;
6718 			if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6719 				no_fragmentflg = 1;
6720 				asconf = 1;
6721 			}
6722 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
6723 				fwd_tsn = 1;
6724 				fwd = chk;
6725 			}
6726 			/*
6727 			 * Add an AUTH chunk, if chunk requires it save the
6728 			 * offset into the chain for AUTH
6729 			 */
6730 			if ((auth == NULL) &&
6731 			    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
6732 			    stcb->asoc.peer_auth_chunks))) {
6733 				m = sctp_add_auth_chunk(m, &endofchain,
6734 				    &auth, &auth_offset,
6735 				    stcb,
6736 				    chk->rec.chunk_id.id);
6737 			}
6738 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
6739 			break;
6740 		}
6741 	}
6742 	one_chunk = 0;
6743 	cnt_thru = 0;
6744 	/* do we have control chunks to retransmit? */
6745 	if (m != NULL) {
6746 		/* Start a timer no matter if we suceed or fail */
6747 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6748 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
6749 		} else if (chk->rec.chunk_id.id == SCTP_ASCONF)
6750 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
6751 
6752 		M_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
6753 		if (m == NULL) {
6754 			return (ENOBUFS);
6755 		}
6756 		shdr = mtod(m, struct sctphdr *);
6757 		shdr->src_port = inp->sctp_lport;
6758 		shdr->dest_port = stcb->rport;
6759 		shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6760 		shdr->checksum = 0;
6761 		auth_offset += sizeof(struct sctphdr);
6762 		chk->snd_count++;	/* update our count */
6763 
6764 		if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
6765 		    (struct sockaddr *)&chk->whoTo->ro._l_addr, m, auth_offset,
6766 		    auth, no_fragmentflg, 0, NULL, asconf))) {
6767 			SCTP_STAT_INCR(sctps_lowlevelerr);
6768 			return (error);
6769 		}
6770 		m = endofchain = NULL;
6771 		auth = NULL;
6772 		auth_offset = 0;
6773 		/*
6774 		 * We don't want to mark the net->sent time here since this
6775 		 * we use this for HB and retrans cannot measure RTT
6776 		 */
6777 		/* SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
6778 		*cnt_out += 1;
6779 		chk->sent = SCTP_DATAGRAM_SENT;
6780 		sctp_ucount_decr(asoc->sent_queue_retran_cnt);
6781 		if (fwd_tsn == 0) {
6782 			return (0);
6783 		} else {
6784 			/* Clean up the fwd-tsn list */
6785 			sctp_clean_up_ctl(stcb, asoc);
6786 			return (0);
6787 		}
6788 	}
6789 	/*
6790 	 * Ok, it is just data retransmission we need to do or that and a
6791 	 * fwd-tsn with it all.
6792 	 */
6793 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
6794 		return (-1);
6795 	}
6796 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
6797 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
6798 		/* not yet open, resend the cookie and that is it */
6799 		return (1);
6800 	}
6801 #ifdef SCTP_AUDITING_ENABLED
6802 	sctp_auditing(20, inp, stcb, NULL);
6803 #endif
6804 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6805 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
6806 			/* No, not sent to this net or not ready for rtx */
6807 			continue;
6808 
6809 		}
6810 		/* pick up the net */
6811 		net = chk->whoTo;
6812 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6813 			mtu = (net->mtu - SCTP_MIN_OVERHEAD);
6814 		} else {
6815 			mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
6816 		}
6817 
6818 		if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
6819 			/* No room in peers rwnd */
6820 			uint32_t tsn;
6821 
6822 			tsn = asoc->last_acked_seq + 1;
6823 			if (tsn == chk->rec.data.TSN_seq) {
6824 				/*
6825 				 * we make a special exception for this
6826 				 * case. The peer has no rwnd but is missing
6827 				 * the lowest chunk.. which is probably what
6828 				 * is holding up the rwnd.
6829 				 */
6830 				goto one_chunk_around;
6831 			}
6832 			return (1);
6833 		}
6834 one_chunk_around:
6835 		if (asoc->peers_rwnd < mtu) {
6836 			one_chunk = 1;
6837 		}
6838 #ifdef SCTP_AUDITING_ENABLED
6839 		sctp_audit_log(0xC3, 2);
6840 #endif
6841 		bundle_at = 0;
6842 		m = NULL;
6843 		net->fast_retran_ip = 0;
6844 		if (chk->rec.data.doing_fast_retransmit == 0) {
6845 			/*
6846 			 * if no FR in progress skip destination that have
6847 			 * flight_size > cwnd.
6848 			 */
6849 			if (net->flight_size >= net->cwnd) {
6850 				continue;
6851 			}
6852 		} else {
6853 			/*
6854 			 * Mark the destination net to have FR recovery
6855 			 * limits put on it.
6856 			 */
6857 			net->fast_retran_ip = 1;
6858 		}
6859 
6860 		/*
6861 		 * if no AUTH is yet included and this chunk requires it,
6862 		 * make sure to account for it.  We don't apply the size
6863 		 * until the AUTH chunk is actually added below in case
6864 		 * there is no room for this chunk.
6865 		 */
6866 		if ((auth == NULL) &&
6867 		    sctp_auth_is_required_chunk(SCTP_DATA,
6868 		    stcb->asoc.peer_auth_chunks)) {
6869 			dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
6870 		} else
6871 			dmtu = 0;
6872 
6873 		if ((chk->send_size <= (mtu - dmtu)) ||
6874 		    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
6875 			/* ok we will add this one */
6876 			if ((auth == NULL) &&
6877 			    (sctp_auth_is_required_chunk(SCTP_DATA,
6878 			    stcb->asoc.peer_auth_chunks))) {
6879 				m = sctp_add_auth_chunk(m, &endofchain,
6880 				    &auth, &auth_offset,
6881 				    stcb, SCTP_DATA);
6882 			}
6883 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
6884 			if (m == NULL) {
6885 				return (ENOMEM);
6886 			}
6887 			/* Do clear IP_DF ? */
6888 			if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
6889 				no_fragmentflg = 0;
6890 			}
6891 			/* upate our MTU size */
6892 			if (mtu > (chk->send_size + dmtu))
6893 				mtu -= (chk->send_size + dmtu);
6894 			else
6895 				mtu = 0;
6896 			data_list[bundle_at++] = chk;
6897 			if (one_chunk && (asoc->total_flight <= 0)) {
6898 				SCTP_STAT_INCR(sctps_windowprobed);
6899 				chk->rec.data.state_flags |= SCTP_WINDOW_PROBE;
6900 			}
6901 		}
6902 		if (one_chunk == 0) {
6903 			/*
6904 			 * now are there anymore forward from chk to pick
6905 			 * up?
6906 			 */
6907 			fwd = TAILQ_NEXT(chk, sctp_next);
6908 			while (fwd) {
6909 				if (fwd->sent != SCTP_DATAGRAM_RESEND) {
6910 					/* Nope, not for retran */
6911 					fwd = TAILQ_NEXT(fwd, sctp_next);
6912 					continue;
6913 				}
6914 				if (fwd->whoTo != net) {
6915 					/* Nope, not the net in question */
6916 					fwd = TAILQ_NEXT(fwd, sctp_next);
6917 					continue;
6918 				}
6919 				if ((auth == NULL) &&
6920 				    sctp_auth_is_required_chunk(SCTP_DATA,
6921 				    stcb->asoc.peer_auth_chunks)) {
6922 					dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
6923 				} else
6924 					dmtu = 0;
6925 				if (fwd->send_size <= (mtu - dmtu)) {
6926 					if ((auth == NULL) &&
6927 					    (sctp_auth_is_required_chunk(SCTP_DATA,
6928 					    stcb->asoc.peer_auth_chunks))) {
6929 						m = sctp_add_auth_chunk(m,
6930 						    &endofchain,
6931 						    &auth, &auth_offset,
6932 						    stcb,
6933 						    SCTP_DATA);
6934 					}
6935 					m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
6936 					if (m == NULL) {
6937 						return (ENOMEM);
6938 					}
6939 					/* Do clear IP_DF ? */
6940 					if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
6941 						no_fragmentflg = 0;
6942 					}
6943 					/* upate our MTU size */
6944 					if (mtu > (fwd->send_size + dmtu))
6945 						mtu -= (fwd->send_size + dmtu);
6946 					else
6947 						mtu = 0;
6948 					data_list[bundle_at++] = fwd;
6949 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
6950 						break;
6951 					}
6952 					fwd = TAILQ_NEXT(fwd, sctp_next);
6953 				} else {
6954 					/* can't fit so we are done */
6955 					break;
6956 				}
6957 			}
6958 		}
6959 		/* Is there something to send for this destination? */
6960 		if (m) {
6961 			/*
6962 			 * No matter if we fail/or suceed we should start a
6963 			 * timer. A failure is like a lost IP packet :-)
6964 			 */
6965 			if (!callout_pending(&net->rxt_timer.timer)) {
6966 				/*
6967 				 * no timer running on this destination
6968 				 * restart it.
6969 				 */
6970 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6971 				tmr_started = 1;
6972 			}
6973 			M_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
6974 			if (m == NULL) {
6975 				return (ENOBUFS);
6976 			}
6977 			shdr = mtod(m, struct sctphdr *);
6978 			shdr->src_port = inp->sctp_lport;
6979 			shdr->dest_port = stcb->rport;
6980 			shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6981 			shdr->checksum = 0;
6982 			auth_offset += sizeof(struct sctphdr);
6983 			/* Now lets send it, if there is anything to send :> */
6984 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
6985 			    (struct sockaddr *)&net->ro._l_addr, m, auth_offset,
6986 			    auth, no_fragmentflg, 0, NULL, asconf))) {
6987 				/* error, we could not output */
6988 				SCTP_STAT_INCR(sctps_lowlevelerr);
6989 				return (error);
6990 			}
6991 			m = endofchain = NULL;
6992 			auth = NULL;
6993 			auth_offset = 0;
6994 			/* For HB's */
6995 			/*
6996 			 * We don't want to mark the net->sent time here
6997 			 * since this we use this for HB and retrans cannot
6998 			 * measure RTT
6999 			 */
7000 			/* SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
7001 
7002 			/* For auto-close */
7003 			cnt_thru++;
7004 			if (*now_filled == 0) {
7005 				SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
7006 				*now = asoc->time_last_sent;
7007 				*now_filled = 1;
7008 			} else {
7009 				asoc->time_last_sent = *now;
7010 			}
7011 			*cnt_out += bundle_at;
7012 #ifdef SCTP_AUDITING_ENABLED
7013 			sctp_audit_log(0xC4, bundle_at);
7014 #endif
7015 			if (bundle_at) {
7016 				tsns_sent = data_list[0]->rec.data.TSN_seq;
7017 			}
7018 			for (i = 0; i < bundle_at; i++) {
7019 				SCTP_STAT_INCR(sctps_sendretransdata);
7020 				data_list[i]->sent = SCTP_DATAGRAM_SENT;
7021 				/*
7022 				 * When we have a revoked data, and we
7023 				 * retransmit it, then we clear the revoked
7024 				 * flag since this flag dictates if we
7025 				 * subtracted from the fs
7026 				 */
7027 				data_list[i]->rec.data.chunk_was_revoked = 0;
7028 				data_list[i]->snd_count++;
7029 				sctp_ucount_decr(asoc->sent_queue_retran_cnt);
7030 				/* record the time */
7031 				data_list[i]->sent_rcv_time = asoc->time_last_sent;
7032 				if (asoc->sent_queue_retran_cnt < 0) {
7033 					asoc->sent_queue_retran_cnt = 0;
7034 				}
7035 				net->flight_size += data_list[i]->book_size;
7036 				asoc->total_flight += data_list[i]->book_size;
7037 				if (data_list[i]->book_size_scale) {
7038 					/*
7039 					 * need to double the book size on
7040 					 * this one
7041 					 */
7042 					data_list[i]->book_size_scale = 0;
7043 					data_list[i]->book_size *= 2;
7044 				} else {
7045 					sctp_ucount_incr(asoc->total_flight_count);
7046 #ifdef SCTP_LOG_RWND
7047 					sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
7048 					    asoc->peers_rwnd, data_list[i]->send_size, sctp_peer_chunk_oh);
7049 #endif
7050 					asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
7051 					    (uint32_t) (data_list[i]->send_size +
7052 					    sctp_peer_chunk_oh));
7053 				}
7054 				if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7055 					/* SWS sender side engages */
7056 					asoc->peers_rwnd = 0;
7057 				}
7058 				if ((i == 0) &&
7059 				    (data_list[i]->rec.data.doing_fast_retransmit)) {
7060 					SCTP_STAT_INCR(sctps_sendfastretrans);
7061 					if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
7062 					    (tmr_started == 0)) {
7063 						/*
7064 						 * ok we just fast-retrans'd
7065 						 * the lowest TSN, i.e the
7066 						 * first on the list. In
7067 						 * this case we want to give
7068 						 * some more time to get a
7069 						 * SACK back without a
7070 						 * t3-expiring.
7071 						 */
7072 						sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
7073 						sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
7074 					}
7075 				}
7076 			}
7077 #ifdef SCTP_CWND_LOGGING
7078 			sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
7079 #endif
7080 #ifdef SCTP_AUDITING_ENABLED
7081 			sctp_auditing(21, inp, stcb, NULL);
7082 #endif
7083 		} else {
7084 			/* None will fit */
7085 			return (1);
7086 		}
7087 		if (asoc->sent_queue_retran_cnt <= 0) {
7088 			/* all done we have no more to retran */
7089 			asoc->sent_queue_retran_cnt = 0;
7090 			break;
7091 		}
7092 		if (one_chunk) {
7093 			/* No more room in rwnd */
7094 			return (1);
7095 		}
7096 		/* stop the for loop here. we sent out a packet */
7097 		break;
7098 	}
7099 	return (0);
7100 }
7101 
7102 
7103 static int
7104 sctp_timer_validation(struct sctp_inpcb *inp,
7105     struct sctp_tcb *stcb,
7106     struct sctp_association *asoc,
7107     int ret)
7108 {
7109 	struct sctp_nets *net;
7110 
7111 	/* Validate that a timer is running somewhere */
7112 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7113 		if (callout_pending(&net->rxt_timer.timer)) {
7114 			/* Here is a timer */
7115 			return (ret);
7116 		}
7117 	}
7118 	SCTP_TCB_LOCK_ASSERT(stcb);
7119 	/* Gak, we did not have a timer somewhere */
7120 #ifdef SCTP_DEBUG
7121 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7122 		printf("Deadlock avoided starting timer on a dest at retran\n");
7123 	}
7124 #endif
7125 	sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
7126 	return (ret);
7127 }
7128 
7129 int
7130 sctp_chunk_output(struct sctp_inpcb *inp,
7131     struct sctp_tcb *stcb,
7132     int from_where)
7133 {
7134 	/*
7135 	 * Ok this is the generic chunk service queue. we must do the
7136 	 * following: - See if there are retransmits pending, if so we must
7137 	 * do these first and return. - Service the stream queue that is
7138 	 * next, moving any message (note I must get a complete message i.e.
7139 	 * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
7140 	 * TSN's - Check to see if the cwnd/rwnd allows any output, if so we
7141 	 * go ahead and fomulate and send the low level chunks. Making sure
7142 	 * to combine any control in the control chunk queue also.
7143 	 */
7144 	struct sctp_association *asoc;
7145 	struct sctp_nets *net;
7146 	int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
7147 	    burst_cnt = 0, burst_limit = 0;
7148 	struct timeval now;
7149 	int now_filled = 0;
7150 	int cwnd_full = 0;
7151 	int nagle_on = 0;
7152 	int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
7153 	int un_sent = 0;
7154 
7155 	asoc = &stcb->asoc;
7156 	if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
7157 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
7158 			nagle_on = 0;
7159 		} else {
7160 			nagle_on = 1;
7161 		}
7162 	}
7163 	SCTP_TCB_LOCK_ASSERT(stcb);
7164 
7165 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
7166 
7167 	if ((un_sent <= 0) &&
7168 	    (TAILQ_EMPTY(&asoc->control_send_queue)) &&
7169 	    (asoc->sent_queue_retran_cnt == 0)) {
7170 		/* Nothing to do unless there is something to be sent left */
7171 		return (error);
7172 	}
7173 	/*
7174 	 * Do we have something to send, data or control AND a sack timer
7175 	 * running, if so piggy-back the sack.
7176 	 */
7177 	if (callout_pending(&stcb->asoc.dack_timer.timer)) {
7178 		sctp_send_sack(stcb);
7179 		callout_stop(&stcb->asoc.dack_timer.timer);
7180 	}
7181 	while (asoc->sent_queue_retran_cnt) {
7182 		/*
7183 		 * Ok, it is retransmission time only, we send out only ONE
7184 		 * packet with a single call off to the retran code.
7185 		 */
7186 		if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
7187 			/*
7188 			 * Special hook for handling cookiess discarded by
7189 			 * peer that carried data. Send cookie-ack only and
7190 			 * then the next call with get the retran's.
7191 			 */
7192 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
7193 			    &cwnd_full, from_where,
7194 			    &now, &now_filled, frag_point);
7195 			return (0);
7196 		} else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
7197 			/* if its not from a HB then do it */
7198 			ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled);
7199 		} else {
7200 			/*
7201 			 * its from any other place, we don't allow retran
7202 			 * output (only control)
7203 			 */
7204 			ret = 1;
7205 		}
7206 		if (ret > 0) {
7207 			/* Can't send anymore */
7208 			/*
7209 			 * now lets push out control by calling med-level
7210 			 * output once. this assures that we WILL send HB's
7211 			 * if queued too.
7212 			 */
7213 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
7214 			    &cwnd_full, from_where,
7215 			    &now, &now_filled, frag_point);
7216 #ifdef SCTP_AUDITING_ENABLED
7217 			sctp_auditing(8, inp, stcb, NULL);
7218 #endif
7219 			return (sctp_timer_validation(inp, stcb, asoc, ret));
7220 		}
7221 		if (ret < 0) {
7222 			/*
7223 			 * The count was off.. retran is not happening so do
7224 			 * the normal retransmission.
7225 			 */
7226 #ifdef SCTP_AUDITING_ENABLED
7227 			sctp_auditing(9, inp, stcb, NULL);
7228 #endif
7229 			break;
7230 		}
7231 		if (from_where == SCTP_OUTPUT_FROM_T3) {
7232 			/* Only one transmission allowed out of a timeout */
7233 #ifdef SCTP_AUDITING_ENABLED
7234 			sctp_auditing(10, inp, stcb, NULL);
7235 #endif
7236 			/* Push out any control */
7237 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
7238 			    &now, &now_filled, frag_point);
7239 			return (ret);
7240 		}
7241 		if ((num_out == 0) && (ret == 0)) {
7242 			/* No more retrans to send */
7243 			break;
7244 		}
7245 	}
7246 #ifdef SCTP_AUDITING_ENABLED
7247 	sctp_auditing(12, inp, stcb, NULL);
7248 #endif
7249 	/* Check for bad destinations, if they exist move chunks around. */
7250 	burst_limit = asoc->max_burst;
7251 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7252 		if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
7253 		    SCTP_ADDR_NOT_REACHABLE) {
7254 			/*
7255 			 * if possible move things off of this address we
7256 			 * still may send below due to the dormant state but
7257 			 * we try to find an alternate address to send to
7258 			 * and if we have one we move all queued data on the
7259 			 * out wheel to this alternate address.
7260 			 */
7261 			if (net->ref_count > 1)
7262 				sctp_move_to_an_alt(stcb, asoc, net);
7263 		} else {
7264 			/*
7265 			 * if ((asoc->sat_network) || (net->addr_is_local))
7266 			 * { burst_limit = asoc->max_burst *
7267 			 * SCTP_SAT_NETWORK_BURST_INCR; }
7268 			 */
7269 			if (sctp_use_cwnd_based_maxburst) {
7270 				if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
7271 					int old_cwnd;
7272 
7273 					if (net->ssthresh < net->cwnd)
7274 						net->ssthresh = net->cwnd;
7275 					old_cwnd = net->cwnd;
7276 					net->cwnd = (net->flight_size + (burst_limit * net->mtu));
7277 
7278 #ifdef SCTP_CWND_MONITOR
7279 					sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_BRST);
7280 #endif
7281 
7282 #ifdef SCTP_LOG_MAXBURST
7283 					sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
7284 #endif
7285 					SCTP_STAT_INCR(sctps_maxburstqueued);
7286 				}
7287 				net->fast_retran_ip = 0;
7288 			} else {
7289 				if (net->flight_size == 0) {
7290 					/* Should be decaying the cwnd here */
7291 					;
7292 				}
7293 			}
7294 		}
7295 
7296 	}
7297 	burst_cnt = 0;
7298 	cwnd_full = 0;
7299 	do {
7300 		error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
7301 		    &reason_code, 0, &cwnd_full, from_where,
7302 		    &now, &now_filled, frag_point);
7303 		if (error) {
7304 #ifdef SCTP_DEBUG
7305 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7306 				printf("Error %d was returned from med-c-op\n", error);
7307 			}
7308 #endif
7309 #ifdef SCTP_LOG_MAXBURST
7310 			sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
7311 #endif
7312 #ifdef SCTP_CWND_LOGGING
7313 			sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
7314 			sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
7315 #endif
7316 
7317 			break;
7318 		}
7319 #ifdef SCTP_DEBUG
7320 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7321 			printf("m-c-o put out %d\n", num_out);
7322 		}
7323 #endif
7324 		tot_out += num_out;
7325 		burst_cnt++;
7326 #ifdef SCTP_CWND_LOGGING
7327 		sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
7328 		if (num_out == 0) {
7329 			sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
7330 		}
7331 #endif
7332 		if (nagle_on) {
7333 			/*
7334 			 * When nagle is on, we look at how much is un_sent,
7335 			 * then if its smaller than an MTU and we have data
7336 			 * in flight we stop.
7337 			 */
7338 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
7339 			    ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count)
7340 			    * sizeof(struct sctp_data_chunk)));
7341 			if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
7342 			    (stcb->asoc.total_flight > 0)) {
7343 				break;
7344 			}
7345 		}
7346 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7347 		    TAILQ_EMPTY(&asoc->send_queue) &&
7348 		    TAILQ_EMPTY(&asoc->out_wheel)) {
7349 			/* Nothing left to send */
7350 			break;
7351 		}
7352 		if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
7353 			/* Nothing left to send */
7354 			break;
7355 		}
7356 	} while (num_out && (sctp_use_cwnd_based_maxburst ||
7357 	    (burst_cnt < burst_limit)));
7358 
7359 	if (sctp_use_cwnd_based_maxburst == 0) {
7360 		if (burst_cnt >= burst_limit) {
7361 			SCTP_STAT_INCR(sctps_maxburstqueued);
7362 			asoc->burst_limit_applied = 1;
7363 #ifdef SCTP_LOG_MAXBURST
7364 			sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
7365 #endif
7366 		} else {
7367 			asoc->burst_limit_applied = 0;
7368 		}
7369 	}
7370 #ifdef SCTP_CWND_LOGGING
7371 	sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
7372 #endif
7373 #ifdef SCTP_DEBUG
7374 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7375 		printf("Ok, we have put out %d chunks\n", tot_out);
7376 	}
7377 #endif
7378 	/*
7379 	 * Now we need to clean up the control chunk chain if a ECNE is on
7380 	 * it. It must be marked as UNSENT again so next call will continue
7381 	 * to send it until such time that we get a CWR, to remove it.
7382 	 */
7383 	if (stcb->asoc.ecn_echo_cnt_onq)
7384 		sctp_fix_ecn_echo(asoc);
7385 	return (error);
7386 }
7387 
7388 
7389 int
7390 sctp_output(inp, m, addr, control, p, flags)
7391 	struct sctp_inpcb *inp;
7392 	struct mbuf *m;
7393 	struct sockaddr *addr;
7394 	struct mbuf *control;
7395 
7396 	struct thread *p;
7397 	int flags;
7398 {
7399 	if (inp == NULL) {
7400 		return (EINVAL);
7401 	}
7402 	if (inp->sctp_socket == NULL) {
7403 		return (EINVAL);
7404 	}
7405 	return (sctp_sosend(inp->sctp_socket,
7406 	    addr,
7407 	    (struct uio *)NULL,
7408 	    m,
7409 	    control,
7410 	    flags,
7411 	    p));
7412 }
7413 
7414 void
7415 send_forward_tsn(struct sctp_tcb *stcb,
7416     struct sctp_association *asoc)
7417 {
7418 	struct sctp_tmit_chunk *chk;
7419 	struct sctp_forward_tsn_chunk *fwdtsn;
7420 
7421 	SCTP_TCB_LOCK_ASSERT(stcb);
7422 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7423 		if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
7424 			/* mark it to unsent */
7425 			chk->sent = SCTP_DATAGRAM_UNSENT;
7426 			chk->snd_count = 0;
7427 			/* Do we correct its output location? */
7428 			if (chk->whoTo != asoc->primary_destination) {
7429 				sctp_free_remote_addr(chk->whoTo);
7430 				chk->whoTo = asoc->primary_destination;
7431 				atomic_add_int(&chk->whoTo->ref_count, 1);
7432 			}
7433 			goto sctp_fill_in_rest;
7434 		}
7435 	}
7436 	/* Ok if we reach here we must build one */
7437 	sctp_alloc_a_chunk(stcb, chk);
7438 	if (chk == NULL) {
7439 		return;
7440 	}
7441 	chk->copy_by_ref = 0;
7442 	chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
7443 	chk->rec.chunk_id.can_take_data = 0;
7444 	chk->asoc = asoc;
7445 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
7446 	if (chk->data == NULL) {
7447 		atomic_subtract_int(&chk->whoTo->ref_count, 1);
7448 		sctp_free_a_chunk(stcb, chk);
7449 		return;
7450 	}
7451 	chk->data->m_data += SCTP_MIN_OVERHEAD;
7452 	chk->sent = SCTP_DATAGRAM_UNSENT;
7453 	chk->snd_count = 0;
7454 	chk->whoTo = asoc->primary_destination;
7455 	atomic_add_int(&chk->whoTo->ref_count, 1);
7456 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
7457 	asoc->ctrl_queue_cnt++;
7458 sctp_fill_in_rest:
7459 	/*
7460 	 * Here we go through and fill out the part that deals with
7461 	 * stream/seq of the ones we skip.
7462 	 */
7463 	chk->data->m_pkthdr.len = chk->data->m_len = 0;
7464 	{
7465 		struct sctp_tmit_chunk *at, *tp1, *last;
7466 		struct sctp_strseq *strseq;
7467 		unsigned int cnt_of_space, i, ovh;
7468 		unsigned int space_needed;
7469 		unsigned int cnt_of_skipped = 0;
7470 
7471 		TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
7472 			if (at->sent != SCTP_FORWARD_TSN_SKIP) {
7473 				/* no more to look at */
7474 				break;
7475 			}
7476 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
7477 				/* We don't report these */
7478 				continue;
7479 			}
7480 			cnt_of_skipped++;
7481 		}
7482 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
7483 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
7484 
7485 		cnt_of_space = M_TRAILINGSPACE(chk->data);
7486 
7487 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7488 			ovh = SCTP_MIN_OVERHEAD;
7489 		} else {
7490 			ovh = SCTP_MIN_V4_OVERHEAD;
7491 		}
7492 		if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
7493 			/* trim to a mtu size */
7494 			cnt_of_space = asoc->smallest_mtu - ovh;
7495 		}
7496 		if (cnt_of_space < space_needed) {
7497 			/*
7498 			 * ok we must trim down the chunk by lowering the
7499 			 * advance peer ack point.
7500 			 */
7501 			cnt_of_skipped = (cnt_of_space -
7502 			    ((sizeof(struct sctp_forward_tsn_chunk)) /
7503 			    sizeof(struct sctp_strseq)));
7504 			/*
7505 			 * Go through and find the TSN that will be the one
7506 			 * we report.
7507 			 */
7508 			at = TAILQ_FIRST(&asoc->sent_queue);
7509 			for (i = 0; i < cnt_of_skipped; i++) {
7510 				tp1 = TAILQ_NEXT(at, sctp_next);
7511 				at = tp1;
7512 			}
7513 			last = at;
7514 			/*
7515 			 * last now points to last one I can report, update
7516 			 * peer ack point
7517 			 */
7518 			asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
7519 			space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
7520 		}
7521 		chk->send_size = space_needed;
7522 		/* Setup the chunk */
7523 		fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
7524 		fwdtsn->ch.chunk_length = htons(chk->send_size);
7525 		fwdtsn->ch.chunk_flags = 0;
7526 		fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
7527 		fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
7528 		chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
7529 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
7530 		chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
7531 		fwdtsn++;
7532 		/*
7533 		 * Move pointer to after the fwdtsn and transfer to the
7534 		 * strseq pointer.
7535 		 */
7536 		strseq = (struct sctp_strseq *)fwdtsn;
7537 		/*
7538 		 * Now populate the strseq list. This is done blindly
7539 		 * without pulling out duplicate stream info. This is
7540 		 * inefficent but won't harm the process since the peer will
7541 		 * look at these in sequence and will thus release anything.
7542 		 * It could mean we exceed the PMTU and chop off some that
7543 		 * we could have included.. but this is unlikely (aka 1432/4
7544 		 * would mean 300+ stream seq's would have to be reported in
7545 		 * one FWD-TSN. With a bit of work we can later FIX this to
7546 		 * optimize and pull out duplcates.. but it does add more
7547 		 * overhead. So for now... not!
7548 		 */
7549 		at = TAILQ_FIRST(&asoc->sent_queue);
7550 		for (i = 0; i < cnt_of_skipped; i++) {
7551 			tp1 = TAILQ_NEXT(at, sctp_next);
7552 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
7553 				/* We don't report these */
7554 				i--;
7555 				at = tp1;
7556 				continue;
7557 			}
7558 			strseq->stream = ntohs(at->rec.data.stream_number);
7559 			strseq->sequence = ntohs(at->rec.data.stream_seq);
7560 			strseq++;
7561 			at = tp1;
7562 		}
7563 	}
7564 	return;
7565 
7566 }
7567 
7568 void
7569 sctp_send_sack(struct sctp_tcb *stcb)
7570 {
7571 	/*
7572 	 * Queue up a SACK in the control queue. We must first check to see
7573 	 * if a SACK is somehow on the control queue. If so, we will take
7574 	 * and and remove the old one.
7575 	 */
7576 	struct sctp_association *asoc;
7577 	struct sctp_tmit_chunk *chk, *a_chk;
7578 	struct sctp_sack_chunk *sack;
7579 	struct sctp_gap_ack_block *gap_descriptor;
7580 	struct sack_track *selector;
7581 	int mergeable = 0;
7582 	int offset;
7583 	caddr_t limit;
7584 	uint32_t *dup;
7585 	int limit_reached = 0;
7586 	unsigned int i, jstart, siz, j;
7587 	unsigned int num_gap_blocks = 0, space;
7588 	int num_dups = 0;
7589 	int space_req;
7590 
7591 
7592 	a_chk = NULL;
7593 	asoc = &stcb->asoc;
7594 	SCTP_TCB_LOCK_ASSERT(stcb);
7595 	if (asoc->last_data_chunk_from == NULL) {
7596 		/* Hmm we never received anything */
7597 		return;
7598 	}
7599 	sctp_set_rwnd(stcb, asoc);
7600 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7601 		if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
7602 			/* Hmm, found a sack already on queue, remove it */
7603 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7604 			asoc->ctrl_queue_cnt++;
7605 			a_chk = chk;
7606 			if (a_chk->data) {
7607 				sctp_m_freem(a_chk->data);
7608 				a_chk->data = NULL;
7609 			}
7610 			sctp_free_remote_addr(a_chk->whoTo);
7611 			a_chk->whoTo = NULL;
7612 			break;
7613 		}
7614 	}
7615 	if (a_chk == NULL) {
7616 		sctp_alloc_a_chunk(stcb, a_chk);
7617 		if (a_chk == NULL) {
7618 			/* No memory so we drop the idea, and set a timer */
7619 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7620 			    stcb->sctp_ep, stcb, NULL);
7621 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
7622 			    stcb->sctp_ep, stcb, NULL);
7623 			return;
7624 		}
7625 		a_chk->copy_by_ref = 0;
7626 		/* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
7627 		a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK;
7628 		a_chk->rec.chunk_id.can_take_data = 1;
7629 	}
7630 	a_chk->asoc = asoc;
7631 	a_chk->snd_count = 0;
7632 	a_chk->send_size = 0;	/* fill in later */
7633 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
7634 
7635 	if ((asoc->numduptsns) ||
7636 	    (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
7637 	    ) {
7638 		/*
7639 		 * Ok, we have some duplicates or the destination for the
7640 		 * sack is unreachable, lets see if we can select an
7641 		 * alternate than asoc->last_data_chunk_from
7642 		 */
7643 		if ((!(asoc->last_data_chunk_from->dest_state &
7644 		    SCTP_ADDR_NOT_REACHABLE)) &&
7645 		    (asoc->used_alt_onsack > asoc->numnets)) {
7646 			/* We used an alt last time, don't this time */
7647 			a_chk->whoTo = NULL;
7648 		} else {
7649 			asoc->used_alt_onsack++;
7650 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
7651 		}
7652 		if (a_chk->whoTo == NULL) {
7653 			/* Nope, no alternate */
7654 			a_chk->whoTo = asoc->last_data_chunk_from;
7655 			asoc->used_alt_onsack = 0;
7656 		}
7657 	} else {
7658 		/*
7659 		 * No duplicates so we use the last place we received data
7660 		 * from.
7661 		 */
7662 		asoc->used_alt_onsack = 0;
7663 		a_chk->whoTo = asoc->last_data_chunk_from;
7664 	}
7665 	if (a_chk->whoTo) {
7666 		atomic_add_int(&a_chk->whoTo->ref_count, 1);
7667 	}
7668 	if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
7669 		/* no gaps */
7670 		space_req = sizeof(struct sctp_sack_chunk);
7671 	} else {
7672 		/* gaps get a cluster */
7673 		space_req = MCLBYTES;
7674 	}
7675 	/* Ok now lets formulate a MBUF with our sack */
7676 	a_chk->data = sctp_get_mbuf_for_msg(space_req, 1, M_DONTWAIT, 1, MT_DATA);
7677 	if ((a_chk->data == NULL) ||
7678 	    (a_chk->whoTo == NULL)) {
7679 		/* rats, no mbuf memory */
7680 		if (a_chk->data) {
7681 			/* was a problem with the destination */
7682 			sctp_m_freem(a_chk->data);
7683 			a_chk->data = NULL;
7684 		}
7685 		if (a_chk->whoTo)
7686 			atomic_subtract_int(&a_chk->whoTo->ref_count, 1);
7687 		sctp_free_a_chunk(stcb, a_chk);
7688 		sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7689 		    stcb->sctp_ep, stcb, NULL);
7690 		sctp_timer_start(SCTP_TIMER_TYPE_RECV,
7691 		    stcb->sctp_ep, stcb, NULL);
7692 		return;
7693 	}
7694 	/* ok, lets go through and fill it in */
7695 	a_chk->data->m_data += SCTP_MIN_OVERHEAD;
7696 	space = M_TRAILINGSPACE(a_chk->data);
7697 	if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
7698 		space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
7699 	}
7700 	limit = mtod(a_chk->data, caddr_t);
7701 	limit += space;
7702 
7703 	sack = mtod(a_chk->data, struct sctp_sack_chunk *);
7704 	sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
7705 	/* 0x01 is used by nonce for ecn */
7706 	sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
7707 	if (sctp_cmt_on_off && sctp_cmt_use_dac) {
7708 		/*
7709 		 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
7710 		 * received, then set high bit to 1, else 0. Reset
7711 		 * pkts_rcvd.
7712 		 */
7713 		sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
7714 		asoc->cmt_dac_pkts_rcvd = 0;
7715 	}
7716 	sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
7717 	sack->sack.a_rwnd = htonl(asoc->my_rwnd);
7718 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
7719 
7720 	/* reset the readers interpretation */
7721 	stcb->freed_by_sorcv_sincelast = 0;
7722 
7723 	gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
7724 
7725 
7726 	siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
7727 	if (asoc->cumulative_tsn < asoc->mapping_array_base_tsn) {
7728 		offset = 1;
7729 		/*
7730 		 * cum-ack behind the mapping array, so we start and use all
7731 		 * entries.
7732 		 */
7733 		jstart = 0;
7734 	} else {
7735 		offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
7736 		/*
7737 		 * we skip the first one when the cum-ack is at or above the
7738 		 * mapping array base.
7739 		 */
7740 		jstart = 1;
7741 	}
7742 	if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
7743 		/* we have a gap .. maybe */
7744 		for (i = 0; i < siz; i++) {
7745 			selector = &sack_array[asoc->mapping_array[i]];
7746 			if (mergeable && selector->right_edge) {
7747 				/*
7748 				 * Backup, left and right edges were ok to
7749 				 * merge.
7750 				 */
7751 				num_gap_blocks--;
7752 				gap_descriptor--;
7753 			}
7754 			if (selector->num_entries == 0)
7755 				mergeable = 0;
7756 			else {
7757 				for (j = jstart; j < selector->num_entries; j++) {
7758 					if (mergeable && selector->right_edge) {
7759 						/*
7760 						 * do a merge by NOT setting
7761 						 * the left side
7762 						 */
7763 						mergeable = 0;
7764 					} else {
7765 						/*
7766 						 * no merge, set the left
7767 						 * side
7768 						 */
7769 						mergeable = 0;
7770 						gap_descriptor->start = htons((selector->gaps[j].start + offset));
7771 					}
7772 					gap_descriptor->end = htons((selector->gaps[j].end + offset));
7773 					num_gap_blocks++;
7774 					gap_descriptor++;
7775 					if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
7776 						/* no more room */
7777 						limit_reached = 1;
7778 						break;
7779 					}
7780 				}
7781 				if (selector->left_edge) {
7782 					mergeable = 1;
7783 				}
7784 			}
7785 			jstart = 0;
7786 			offset += 8;
7787 		}
7788 		if (num_gap_blocks == 0) {
7789 			/* reneged all chunks */
7790 			asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
7791 		}
7792 	}
7793 	/* now we must add any dups we are going to report. */
7794 	if ((limit_reached == 0) && (asoc->numduptsns)) {
7795 		dup = (uint32_t *) gap_descriptor;
7796 		for (i = 0; i < asoc->numduptsns; i++) {
7797 			*dup = htonl(asoc->dup_tsns[i]);
7798 			dup++;
7799 			num_dups++;
7800 			if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
7801 				/* no more room */
7802 				break;
7803 			}
7804 		}
7805 		asoc->numduptsns = 0;
7806 	}
7807 	/*
7808 	 * now that the chunk is prepared queue it to the control chunk
7809 	 * queue.
7810 	 */
7811 	a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
7812 	    (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
7813 	    (num_dups * sizeof(int32_t)));
7814 	a_chk->data->m_pkthdr.len = a_chk->data->m_len = a_chk->send_size;
7815 	sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
7816 	sack->sack.num_dup_tsns = htons(num_dups);
7817 	sack->ch.chunk_length = htons(a_chk->send_size);
7818 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
7819 	asoc->ctrl_queue_cnt++;
7820 	SCTP_STAT_INCR(sctps_sendsacks);
7821 	return;
7822 }
7823 
7824 
7825 void
7826 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr)
7827 {
7828 	struct mbuf *m_abort;
7829 	struct mbuf *m_out = NULL, *m_end = NULL;
7830 	struct sctp_abort_chunk *abort = NULL;
7831 	int sz;
7832 	uint32_t auth_offset = 0;
7833 	struct sctp_auth_chunk *auth = NULL;
7834 	struct sctphdr *shdr;
7835 
7836 	/*
7837 	 * Add an AUTH chunk, if chunk requires it and save the offset into
7838 	 * the chain for AUTH
7839 	 */
7840 	if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
7841 	    stcb->asoc.peer_auth_chunks)) {
7842 		m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
7843 		    stcb, SCTP_ABORT_ASSOCIATION);
7844 	}
7845 	SCTP_TCB_LOCK_ASSERT(stcb);
7846 	m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 1, M_DONTWAIT, 1, MT_HEADER);
7847 	if (m_abort == NULL) {
7848 		/* no mbuf's */
7849 		if (m_out)
7850 			sctp_m_freem(m_out);
7851 		return;
7852 	}
7853 	/* link in any error */
7854 	m_abort->m_next = operr;
7855 	sz = 0;
7856 	if (operr) {
7857 		struct mbuf *n;
7858 
7859 		n = operr;
7860 		while (n) {
7861 			sz += n->m_len;
7862 			n = n->m_next;
7863 		}
7864 	}
7865 	m_abort->m_len = sizeof(*abort);
7866 	m_abort->m_pkthdr.len = m_abort->m_len + sz;
7867 	m_abort->m_pkthdr.rcvif = 0;
7868 	if (m_out == NULL) {
7869 		/* NO Auth chunk prepended, so reserve space in front */
7870 		m_abort->m_data += SCTP_MIN_OVERHEAD;
7871 		m_out = m_abort;
7872 	} else {
7873 		/* Put AUTH chunk at the front of the chain */
7874 		m_out->m_pkthdr.len += m_abort->m_pkthdr.len;
7875 		m_end->m_next = m_abort;
7876 	}
7877 
7878 	/* fill in the ABORT chunk */
7879 	abort = mtod(m_abort, struct sctp_abort_chunk *);
7880 	abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
7881 	abort->ch.chunk_flags = 0;
7882 	abort->ch.chunk_length = htons(sizeof(*abort) + sz);
7883 
7884 	/* prepend and fill in the SCTP header */
7885 	M_PREPEND(m_out, sizeof(struct sctphdr), M_DONTWAIT);
7886 	if (m_out == NULL) {
7887 		/* TSNH: no memory */
7888 		return;
7889 	}
7890 	shdr = mtod(m_out, struct sctphdr *);
7891 	shdr->src_port = stcb->sctp_ep->sctp_lport;
7892 	shdr->dest_port = stcb->rport;
7893 	shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7894 	shdr->checksum = 0;
7895 	auth_offset += sizeof(struct sctphdr);
7896 
7897 	sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
7898 	    stcb->asoc.primary_destination,
7899 	    (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
7900 	    m_out, auth_offset, auth, 1, 0, NULL, 0);
7901 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7902 }
7903 
7904 int
7905 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
7906     struct sctp_nets *net)
7907 {
7908 	/* formulate and SEND a SHUTDOWN-COMPLETE */
7909 	struct mbuf *m_shutdown_comp;
7910 	struct sctp_shutdown_complete_msg *comp_cp;
7911 
7912 	m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_complete_msg), 1, M_DONTWAIT, 1, MT_HEADER);
7913 	if (m_shutdown_comp == NULL) {
7914 		/* no mbuf's */
7915 		return (-1);
7916 	}
7917 	m_shutdown_comp->m_data += sizeof(struct ip6_hdr);
7918 	comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
7919 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
7920 	comp_cp->shut_cmp.ch.chunk_flags = 0;
7921 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
7922 	comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
7923 	comp_cp->sh.dest_port = stcb->rport;
7924 	comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
7925 	comp_cp->sh.checksum = 0;
7926 
7927 	m_shutdown_comp->m_pkthdr.len = m_shutdown_comp->m_len = sizeof(struct sctp_shutdown_complete_msg);
7928 	m_shutdown_comp->m_pkthdr.rcvif = 0;
7929 	sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
7930 	    (struct sockaddr *)&net->ro._l_addr,
7931 	    m_shutdown_comp, 0, NULL, 1, 0, NULL, 0);
7932 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7933 	return (0);
7934 }
7935 
7936 int
7937 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh)
7938 {
7939 	/* formulate and SEND a SHUTDOWN-COMPLETE */
7940 	struct mbuf *mout;
7941 	struct ip *iph, *iph_out;
7942 	struct ip6_hdr *ip6, *ip6_out;
7943 	int offset_out;
7944 	struct sctp_shutdown_complete_msg *comp_cp;
7945 
7946 	mout = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_complete_msg), 1, M_DONTWAIT, 1, MT_HEADER);
7947 	if (mout == NULL) {
7948 		/* no mbuf's */
7949 		return (-1);
7950 	}
7951 	iph = mtod(m, struct ip *);
7952 	iph_out = NULL;
7953 	ip6_out = NULL;
7954 	offset_out = 0;
7955 	if (iph->ip_v == IPVERSION) {
7956 		mout->m_len = sizeof(struct ip) +
7957 		    sizeof(struct sctp_shutdown_complete_msg);
7958 		mout->m_next = NULL;
7959 		iph_out = mtod(mout, struct ip *);
7960 
7961 		/* Fill in the IP header for the ABORT */
7962 		iph_out->ip_v = IPVERSION;
7963 		iph_out->ip_hl = (sizeof(struct ip) / 4);
7964 		iph_out->ip_tos = (u_char)0;
7965 		iph_out->ip_id = 0;
7966 		iph_out->ip_off = 0;
7967 		iph_out->ip_ttl = MAXTTL;
7968 		iph_out->ip_p = IPPROTO_SCTP;
7969 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
7970 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
7971 
7972 		/* let IP layer calculate this */
7973 		iph_out->ip_sum = 0;
7974 		offset_out += sizeof(*iph_out);
7975 		comp_cp = (struct sctp_shutdown_complete_msg *)(
7976 		    (caddr_t)iph_out + offset_out);
7977 	} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
7978 		ip6 = (struct ip6_hdr *)iph;
7979 		mout->m_len = sizeof(struct ip6_hdr) +
7980 		    sizeof(struct sctp_shutdown_complete_msg);
7981 		mout->m_next = NULL;
7982 		ip6_out = mtod(mout, struct ip6_hdr *);
7983 
7984 		/* Fill in the IPv6 header for the ABORT */
7985 		ip6_out->ip6_flow = ip6->ip6_flow;
7986 		ip6_out->ip6_hlim = ip6_defhlim;
7987 		ip6_out->ip6_nxt = IPPROTO_SCTP;
7988 		ip6_out->ip6_src = ip6->ip6_dst;
7989 		ip6_out->ip6_dst = ip6->ip6_src;
7990 		ip6_out->ip6_plen = mout->m_len;
7991 		offset_out += sizeof(*ip6_out);
7992 		comp_cp = (struct sctp_shutdown_complete_msg *)(
7993 		    (caddr_t)ip6_out + offset_out);
7994 	} else {
7995 		/* Currently not supported. */
7996 		return (-1);
7997 	}
7998 
7999 	/* Now copy in and fill in the ABORT tags etc. */
8000 	comp_cp->sh.src_port = sh->dest_port;
8001 	comp_cp->sh.dest_port = sh->src_port;
8002 	comp_cp->sh.checksum = 0;
8003 	comp_cp->sh.v_tag = sh->v_tag;
8004 	comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
8005 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
8006 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
8007 
8008 	mout->m_pkthdr.len = mout->m_len;
8009 	/* add checksum */
8010 	if ((sctp_no_csum_on_loopback) &&
8011 	    (m->m_pkthdr.rcvif) &&
8012 	    (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
8013 		comp_cp->sh.checksum = 0;
8014 	} else {
8015 		comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
8016 	}
8017 
8018 	/* zap the rcvif, it should be null */
8019 	mout->m_pkthdr.rcvif = 0;
8020 	/* zap the stack pointer to the route */
8021 	if (iph_out != NULL) {
8022 		struct route ro;
8023 
8024 		bzero(&ro, sizeof ro);
8025 		/* set IPv4 length */
8026 		iph_out->ip_len = mout->m_pkthdr.len;
8027 		/* out it goes */
8028 		ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL
8029 		    ,NULL
8030 		    );
8031 		/* Free the route if we got one back */
8032 		if (ro.ro_rt)
8033 			RTFREE(ro.ro_rt);
8034 	} else if (ip6_out != NULL) {
8035 		struct route_in6 ro;
8036 
8037 
8038 		bzero(&ro, sizeof(ro));
8039 		ip6_output(mout, NULL, &ro, 0, NULL, NULL
8040 		    ,NULL
8041 		    );
8042 		/* Free the route if we got one back */
8043 		if (ro.ro_rt)
8044 			RTFREE(ro.ro_rt);
8045 	}
8046 	SCTP_STAT_INCR(sctps_sendpackets);
8047 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
8048 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8049 	return (0);
8050 }
8051 
8052 static struct sctp_nets *
8053 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
8054 {
8055 	struct sctp_nets *net, *hnet;
8056 	int ms_goneby, highest_ms, state_overide = 0;
8057 
8058 	SCTP_GETTIME_TIMEVAL(now);
8059 	highest_ms = 0;
8060 	hnet = NULL;
8061 	SCTP_TCB_LOCK_ASSERT(stcb);
8062 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
8063 		if (
8064 		    ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
8065 		    (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
8066 		    ) {
8067 			/*
8068 			 * Skip this guy from consideration if HB is off AND
8069 			 * its confirmed
8070 			 */
8071 			continue;
8072 		}
8073 		if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
8074 			/* skip this dest net from consideration */
8075 			continue;
8076 		}
8077 		if (net->last_sent_time.tv_sec) {
8078 			/* Sent to so we subtract */
8079 			ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
8080 		} else
8081 			/* Never been sent to */
8082 			ms_goneby = 0x7fffffff;
8083 		/*
8084 		 * When the address state is unconfirmed but still
8085 		 * considered reachable, we HB at a higher rate. Once it
8086 		 * goes confirmed OR reaches the "unreachable" state, thenw
8087 		 * we cut it back to HB at a more normal pace.
8088 		 */
8089 		if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
8090 			state_overide = 1;
8091 		} else {
8092 			state_overide = 0;
8093 		}
8094 
8095 		if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
8096 		    (ms_goneby > highest_ms)) {
8097 			highest_ms = ms_goneby;
8098 			hnet = net;
8099 		}
8100 	}
8101 	if (hnet &&
8102 	    ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
8103 		state_overide = 1;
8104 	} else {
8105 		state_overide = 0;
8106 	}
8107 
8108 	if (highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
8109 		/*
8110 		 * Found the one with longest delay bounds OR it is
8111 		 * unconfirmed and still not marked unreachable.
8112 		 */
8113 #ifdef SCTP_DEBUG
8114 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8115 			printf("net:%p is the hb winner -",
8116 			    hnet);
8117 			if (hnet)
8118 				sctp_print_address((struct sockaddr *)&hnet->ro._l_addr);
8119 			else
8120 				printf(" none\n");
8121 		}
8122 #endif
8123 		/* update the timer now */
8124 		hnet->last_sent_time = *now;
8125 		return (hnet);
8126 	}
8127 	/* Nothing to HB */
8128 	return (NULL);
8129 }
8130 
8131 int
8132 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
8133 {
8134 	struct sctp_tmit_chunk *chk;
8135 	struct sctp_nets *net;
8136 	struct sctp_heartbeat_chunk *hb;
8137 	struct timeval now;
8138 	struct sockaddr_in *sin;
8139 	struct sockaddr_in6 *sin6;
8140 
8141 	SCTP_TCB_LOCK_ASSERT(stcb);
8142 	if (user_req == 0) {
8143 		net = sctp_select_hb_destination(stcb, &now);
8144 		if (net == NULL) {
8145 			/*
8146 			 * All our busy none to send to, just start the
8147 			 * timer again.
8148 			 */
8149 			if (stcb->asoc.state == 0) {
8150 				return (0);
8151 			}
8152 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
8153 			    stcb->sctp_ep,
8154 			    stcb,
8155 			    net);
8156 			return (0);
8157 		}
8158 	} else {
8159 		net = u_net;
8160 		if (net == NULL) {
8161 			return (0);
8162 		}
8163 		SCTP_GETTIME_TIMEVAL(&now);
8164 	}
8165 	sin = (struct sockaddr_in *)&net->ro._l_addr;
8166 	if (sin->sin_family != AF_INET) {
8167 		if (sin->sin_family != AF_INET6) {
8168 			/* huh */
8169 			return (0);
8170 		}
8171 	}
8172 	sctp_alloc_a_chunk(stcb, chk);
8173 	if (chk == NULL) {
8174 #ifdef SCTP_DEBUG
8175 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8176 			printf("Gak, can't get a chunk for hb\n");
8177 		}
8178 #endif
8179 		return (0);
8180 	}
8181 	chk->copy_by_ref = 0;
8182 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
8183 	chk->rec.chunk_id.can_take_data = 1;
8184 	chk->asoc = &stcb->asoc;
8185 	chk->send_size = sizeof(struct sctp_heartbeat_chunk);
8186 
8187 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 1, M_DONTWAIT, 1, MT_HEADER);
8188 	if (chk->data == NULL) {
8189 		sctp_free_a_chunk(stcb, chk);
8190 		return (0);
8191 	}
8192 	chk->data->m_data += SCTP_MIN_OVERHEAD;
8193 	chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8194 	chk->sent = SCTP_DATAGRAM_UNSENT;
8195 	chk->snd_count = 0;
8196 	chk->whoTo = net;
8197 	atomic_add_int(&chk->whoTo->ref_count, 1);
8198 	/* Now we have a mbuf that we can fill in with the details */
8199 	hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
8200 
8201 	/* fill out chunk header */
8202 	hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
8203 	hb->ch.chunk_flags = 0;
8204 	hb->ch.chunk_length = htons(chk->send_size);
8205 	/* Fill out hb parameter */
8206 	hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
8207 	hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
8208 	hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
8209 	hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
8210 	/* Did our user request this one, put it in */
8211 	hb->heartbeat.hb_info.user_req = user_req;
8212 	hb->heartbeat.hb_info.addr_family = sin->sin_family;
8213 	hb->heartbeat.hb_info.addr_len = sin->sin_len;
8214 	if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
8215 		/*
8216 		 * we only take from the entropy pool if the address is not
8217 		 * confirmed.
8218 		 */
8219 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
8220 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
8221 	} else {
8222 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
8223 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
8224 	}
8225 	if (sin->sin_family == AF_INET) {
8226 		memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
8227 	} else if (sin->sin_family == AF_INET6) {
8228 		/* We leave the scope the way it is in our lookup table. */
8229 		sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
8230 		memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
8231 	} else {
8232 		/* huh compiler bug */
8233 		return (0);
8234 	}
8235 	/* ok we have a destination that needs a beat */
8236 	/* lets do the theshold management Qiaobing style */
8237 	if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
8238 	    stcb->asoc.max_send_times)) {
8239 		/*
8240 		 * we have lost the association, in a way this is quite bad
8241 		 * since we really are one less time since we really did not
8242 		 * send yet. This is the down side to the Q's style as
8243 		 * defined in the RFC and not my alternate style defined in
8244 		 * the RFC.
8245 		 */
8246 		atomic_subtract_int(&chk->whoTo->ref_count, 1);
8247 		if (chk->data != NULL) {
8248 			sctp_m_freem(chk->data);
8249 			chk->data = NULL;
8250 		}
8251 		sctp_free_a_chunk(stcb, chk);
8252 		return (-1);
8253 	}
8254 	net->hb_responded = 0;
8255 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8256 	stcb->asoc.ctrl_queue_cnt++;
8257 	SCTP_STAT_INCR(sctps_sendheartbeat);
8258 	/*
8259 	 * Call directly med level routine to put out the chunk. It will
8260 	 * always tumble out control chunks aka HB but it may even tumble
8261 	 * out data too.
8262 	 */
8263 	return (1);
8264 }
8265 
8266 void
8267 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
8268     uint32_t high_tsn)
8269 {
8270 	struct sctp_association *asoc;
8271 	struct sctp_ecne_chunk *ecne;
8272 	struct sctp_tmit_chunk *chk;
8273 
8274 	asoc = &stcb->asoc;
8275 	SCTP_TCB_LOCK_ASSERT(stcb);
8276 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8277 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
8278 			/* found a previous ECN_ECHO update it if needed */
8279 			ecne = mtod(chk->data, struct sctp_ecne_chunk *);
8280 			ecne->tsn = htonl(high_tsn);
8281 			return;
8282 		}
8283 	}
8284 	/* nope could not find one to update so we must build one */
8285 	sctp_alloc_a_chunk(stcb, chk);
8286 	if (chk == NULL) {
8287 		return;
8288 	}
8289 	chk->copy_by_ref = 0;
8290 	SCTP_STAT_INCR(sctps_sendecne);
8291 	chk->rec.chunk_id.id = SCTP_ECN_ECHO;
8292 	chk->rec.chunk_id.can_take_data = 0;
8293 	chk->asoc = &stcb->asoc;
8294 	chk->send_size = sizeof(struct sctp_ecne_chunk);
8295 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 1, M_DONTWAIT, 1, MT_HEADER);
8296 	if (chk->data == NULL) {
8297 		sctp_free_a_chunk(stcb, chk);
8298 		return;
8299 	}
8300 	chk->data->m_data += SCTP_MIN_OVERHEAD;
8301 	chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8302 	chk->sent = SCTP_DATAGRAM_UNSENT;
8303 	chk->snd_count = 0;
8304 	chk->whoTo = net;
8305 	atomic_add_int(&chk->whoTo->ref_count, 1);
8306 	stcb->asoc.ecn_echo_cnt_onq++;
8307 	ecne = mtod(chk->data, struct sctp_ecne_chunk *);
8308 	ecne->ch.chunk_type = SCTP_ECN_ECHO;
8309 	ecne->ch.chunk_flags = 0;
8310 	ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
8311 	ecne->tsn = htonl(high_tsn);
8312 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8313 	asoc->ctrl_queue_cnt++;
8314 }
8315 
8316 void
8317 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
8318     struct mbuf *m, int iphlen, int bad_crc)
8319 {
8320 	struct sctp_association *asoc;
8321 	struct sctp_pktdrop_chunk *drp;
8322 	struct sctp_tmit_chunk *chk;
8323 	uint8_t *datap;
8324 	int len;
8325 	unsigned int small_one;
8326 	struct ip *iph;
8327 
8328 	long spc;
8329 
8330 	asoc = &stcb->asoc;
8331 	SCTP_TCB_LOCK_ASSERT(stcb);
8332 	if (asoc->peer_supports_pktdrop == 0) {
8333 		/*
8334 		 * peer must declare support before I send one.
8335 		 */
8336 		return;
8337 	}
8338 	if (stcb->sctp_socket == NULL) {
8339 		return;
8340 	}
8341 	sctp_alloc_a_chunk(stcb, chk);
8342 	if (chk == NULL) {
8343 		return;
8344 	}
8345 	chk->copy_by_ref = 0;
8346 	iph = mtod(m, struct ip *);
8347 	if (iph == NULL) {
8348 		return;
8349 	}
8350 	if (iph->ip_v == IPVERSION) {
8351 		/* IPv4 */
8352 		len = chk->send_size = iph->ip_len;
8353 	} else {
8354 		struct ip6_hdr *ip6h;
8355 
8356 		/* IPv6 */
8357 		ip6h = mtod(m, struct ip6_hdr *);
8358 		len = chk->send_size = htons(ip6h->ip6_plen);
8359 	}
8360 	if ((len + iphlen) > m->m_pkthdr.len) {
8361 		/* huh */
8362 		chk->send_size = len = m->m_pkthdr.len - iphlen;
8363 	}
8364 	chk->asoc = &stcb->asoc;
8365 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
8366 	if (chk->data == NULL) {
8367 jump_out:
8368 		sctp_free_a_chunk(stcb, chk);
8369 		return;
8370 	}
8371 	chk->data->m_data += SCTP_MIN_OVERHEAD;
8372 	drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
8373 	if (drp == NULL) {
8374 		sctp_m_freem(chk->data);
8375 		chk->data = NULL;
8376 		goto jump_out;
8377 	}
8378 	small_one = asoc->smallest_mtu;
8379 	if (small_one > MCLBYTES) {
8380 		/* Only one cluster worth of data MAX */
8381 		small_one = MCLBYTES;
8382 	}
8383 	chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
8384 	    sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
8385 	if (chk->book_size > small_one) {
8386 		drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
8387 		drp->trunc_len = htons(chk->send_size);
8388 		chk->send_size = small_one - (SCTP_MED_OVERHEAD +
8389 		    sizeof(struct sctp_pktdrop_chunk) +
8390 		    sizeof(struct sctphdr));
8391 		len = chk->send_size;
8392 	} else {
8393 		/* no truncation needed */
8394 		drp->ch.chunk_flags = 0;
8395 		drp->trunc_len = htons(0);
8396 	}
8397 	if (bad_crc) {
8398 		drp->ch.chunk_flags |= SCTP_BADCRC;
8399 	}
8400 	chk->send_size += sizeof(struct sctp_pktdrop_chunk);
8401 	chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8402 	chk->sent = SCTP_DATAGRAM_UNSENT;
8403 	chk->snd_count = 0;
8404 	if (net) {
8405 		/* we should hit here */
8406 		chk->whoTo = net;
8407 	} else {
8408 		chk->whoTo = asoc->primary_destination;
8409 	}
8410 	atomic_add_int(&chk->whoTo->ref_count, 1);
8411 	chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
8412 	chk->rec.chunk_id.can_take_data = 1;
8413 	drp->ch.chunk_type = SCTP_PACKET_DROPPED;
8414 	drp->ch.chunk_length = htons(chk->send_size);
8415 	spc = stcb->sctp_socket->so_rcv.sb_hiwat;
8416 	if (spc < 0) {
8417 		spc = 0;
8418 	}
8419 	drp->bottle_bw = htonl(spc);
8420 	if (asoc->my_rwnd) {
8421 		drp->current_onq = htonl(asoc->size_on_reasm_queue +
8422 		    asoc->size_on_all_streams +
8423 		    asoc->my_rwnd_control_len +
8424 		    stcb->sctp_socket->so_rcv.sb_cc);
8425 	} else {
8426 		/*
8427 		 * If my rwnd is 0, possibly from mbuf depletion as well as
8428 		 * space used, tell the peer there is NO space aka onq == bw
8429 		 */
8430 		drp->current_onq = htonl(spc);
8431 	}
8432 	drp->reserved = 0;
8433 	datap = drp->data;
8434 	m_copydata(m, iphlen, len, (caddr_t)datap);
8435 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8436 	asoc->ctrl_queue_cnt++;
8437 }
8438 
8439 void
8440 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
8441 {
8442 	struct sctp_association *asoc;
8443 	struct sctp_cwr_chunk *cwr;
8444 	struct sctp_tmit_chunk *chk;
8445 
8446 	asoc = &stcb->asoc;
8447 	SCTP_TCB_LOCK_ASSERT(stcb);
8448 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8449 		if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
8450 			/* found a previous ECN_CWR update it if needed */
8451 			cwr = mtod(chk->data, struct sctp_cwr_chunk *);
8452 			if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
8453 			    MAX_TSN)) {
8454 				cwr->tsn = htonl(high_tsn);
8455 			}
8456 			return;
8457 		}
8458 	}
8459 	/* nope could not find one to update so we must build one */
8460 	sctp_alloc_a_chunk(stcb, chk);
8461 	if (chk == NULL) {
8462 		return;
8463 	}
8464 	chk->copy_by_ref = 0;
8465 	chk->rec.chunk_id.id = SCTP_ECN_CWR;
8466 	chk->rec.chunk_id.can_take_data = 1;
8467 	chk->asoc = &stcb->asoc;
8468 	chk->send_size = sizeof(struct sctp_cwr_chunk);
8469 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 1, M_DONTWAIT, 1, MT_HEADER);
8470 	if (chk->data == NULL) {
8471 		sctp_free_a_chunk(stcb, chk);
8472 		return;
8473 	}
8474 	chk->data->m_data += SCTP_MIN_OVERHEAD;
8475 	chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8476 	chk->sent = SCTP_DATAGRAM_UNSENT;
8477 	chk->snd_count = 0;
8478 	chk->whoTo = net;
8479 	atomic_add_int(&chk->whoTo->ref_count, 1);
8480 	cwr = mtod(chk->data, struct sctp_cwr_chunk *);
8481 	cwr->ch.chunk_type = SCTP_ECN_CWR;
8482 	cwr->ch.chunk_flags = 0;
8483 	cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
8484 	cwr->tsn = htonl(high_tsn);
8485 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8486 	asoc->ctrl_queue_cnt++;
8487 }
8488 
8489 void
8490 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
8491     int number_entries, uint16_t * list,
8492     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
8493 {
8494 	int len, old_len, i;
8495 	struct sctp_stream_reset_out_request *req_out;
8496 	struct sctp_chunkhdr *ch;
8497 
8498 	ch = mtod(chk->data, struct sctp_chunkhdr *);
8499 
8500 
8501 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
8502 
8503 	/* get to new offset for the param. */
8504 	req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
8505 	/* now how long will this param be? */
8506 	len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
8507 	req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
8508 	req_out->ph.param_length = htons(len);
8509 	req_out->request_seq = htonl(seq);
8510 	req_out->response_seq = htonl(resp_seq);
8511 	req_out->send_reset_at_tsn = htonl(last_sent);
8512 	if (number_entries) {
8513 		for (i = 0; i < number_entries; i++) {
8514 			req_out->list_of_streams[i] = htons(list[i]);
8515 		}
8516 	}
8517 	if (SCTP_SIZE32(len) > len) {
8518 		/*
8519 		 * Need to worry about the pad we may end up adding to the
8520 		 * end. This is easy since the struct is either aligned to 4
8521 		 * bytes or 2 bytes off.
8522 		 */
8523 		req_out->list_of_streams[number_entries] = 0;
8524 	}
8525 	/* now fix the chunk length */
8526 	ch->chunk_length = htons(len + old_len);
8527 	chk->send_size = len + old_len;
8528 	chk->book_size = SCTP_SIZE32(chk->send_size);
8529 	chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8530 	return;
8531 }
8532 
8533 
8534 void
8535 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
8536     int number_entries, uint16_t * list,
8537     uint32_t seq)
8538 {
8539 	int len, old_len, i;
8540 	struct sctp_stream_reset_in_request *req_in;
8541 	struct sctp_chunkhdr *ch;
8542 
8543 	ch = mtod(chk->data, struct sctp_chunkhdr *);
8544 
8545 
8546 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
8547 
8548 	/* get to new offset for the param. */
8549 	req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
8550 	/* now how long will this param be? */
8551 	len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
8552 	req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
8553 	req_in->ph.param_length = htons(len);
8554 	req_in->request_seq = htonl(seq);
8555 	if (number_entries) {
8556 		for (i = 0; i < number_entries; i++) {
8557 			req_in->list_of_streams[i] = htons(list[i]);
8558 		}
8559 	}
8560 	if (SCTP_SIZE32(len) > len) {
8561 		/*
8562 		 * Need to worry about the pad we may end up adding to the
8563 		 * end. This is easy since the struct is either aligned to 4
8564 		 * bytes or 2 bytes off.
8565 		 */
8566 		req_in->list_of_streams[number_entries] = 0;
8567 	}
8568 	/* now fix the chunk length */
8569 	ch->chunk_length = htons(len + old_len);
8570 	chk->send_size = len + old_len;
8571 	chk->book_size = SCTP_SIZE32(chk->send_size);
8572 	chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8573 	return;
8574 }
8575 
8576 
8577 void
8578 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
8579     uint32_t seq)
8580 {
8581 	int len, old_len;
8582 	struct sctp_stream_reset_tsn_request *req_tsn;
8583 	struct sctp_chunkhdr *ch;
8584 
8585 	ch = mtod(chk->data, struct sctp_chunkhdr *);
8586 
8587 
8588 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
8589 
8590 	/* get to new offset for the param. */
8591 	req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
8592 	/* now how long will this param be? */
8593 	len = sizeof(struct sctp_stream_reset_tsn_request);
8594 	req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
8595 	req_tsn->ph.param_length = htons(len);
8596 	req_tsn->request_seq = htonl(seq);
8597 
8598 	/* now fix the chunk length */
8599 	ch->chunk_length = htons(len + old_len);
8600 	chk->send_size = len + old_len;
8601 	chk->book_size = SCTP_SIZE32(chk->send_size);
8602 	chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8603 	return;
8604 }
8605 
8606 void
8607 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
8608     uint32_t resp_seq, uint32_t result)
8609 {
8610 	int len, old_len;
8611 	struct sctp_stream_reset_response *resp;
8612 	struct sctp_chunkhdr *ch;
8613 
8614 	ch = mtod(chk->data, struct sctp_chunkhdr *);
8615 
8616 
8617 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
8618 
8619 	/* get to new offset for the param. */
8620 	resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
8621 	/* now how long will this param be? */
8622 	len = sizeof(struct sctp_stream_reset_response);
8623 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
8624 	resp->ph.param_length = htons(len);
8625 	resp->response_seq = htonl(resp_seq);
8626 	resp->result = ntohl(result);
8627 
8628 	/* now fix the chunk length */
8629 	ch->chunk_length = htons(len + old_len);
8630 	chk->send_size = len + old_len;
8631 	chk->book_size = SCTP_SIZE32(chk->send_size);
8632 	chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8633 	return;
8634 
8635 }
8636 
8637 
8638 void
8639 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
8640     uint32_t resp_seq, uint32_t result,
8641     uint32_t send_una, uint32_t recv_next)
8642 {
8643 	int len, old_len;
8644 	struct sctp_stream_reset_response_tsn *resp;
8645 	struct sctp_chunkhdr *ch;
8646 
8647 	ch = mtod(chk->data, struct sctp_chunkhdr *);
8648 
8649 
8650 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
8651 
8652 	/* get to new offset for the param. */
8653 	resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
8654 	/* now how long will this param be? */
8655 	len = sizeof(struct sctp_stream_reset_response_tsn);
8656 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
8657 	resp->ph.param_length = htons(len);
8658 	resp->response_seq = htonl(resp_seq);
8659 	resp->result = htonl(result);
8660 	resp->senders_next_tsn = htonl(send_una);
8661 	resp->receivers_next_tsn = htonl(recv_next);
8662 
8663 	/* now fix the chunk length */
8664 	ch->chunk_length = htons(len + old_len);
8665 	chk->send_size = len + old_len;
8666 	chk->book_size = SCTP_SIZE32(chk->send_size);
8667 	chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8668 	return;
8669 }
8670 
8671 
8672 int
8673 sctp_send_str_reset_req(struct sctp_tcb *stcb,
8674     int number_entries, uint16_t * list,
8675     uint8_t send_out_req, uint32_t resp_seq,
8676     uint8_t send_in_req,
8677     uint8_t send_tsn_req)
8678 {
8679 
8680 	struct sctp_association *asoc;
8681 	struct sctp_tmit_chunk *chk;
8682 	struct sctp_chunkhdr *ch;
8683 	uint32_t seq;
8684 
8685 	asoc = &stcb->asoc;
8686 	if (asoc->stream_reset_outstanding) {
8687 		/*
8688 		 * Already one pending, must get ACK back to clear the flag.
8689 		 */
8690 		return (EBUSY);
8691 	}
8692 	if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0)) {
8693 		/* nothing to do */
8694 		return (EINVAL);
8695 	}
8696 	if (send_tsn_req && (send_out_req || send_in_req)) {
8697 		/* error, can't do that */
8698 		return (EINVAL);
8699 	}
8700 	sctp_alloc_a_chunk(stcb, chk);
8701 	if (chk == NULL) {
8702 		return (ENOMEM);
8703 	}
8704 	chk->copy_by_ref = 0;
8705 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
8706 	chk->rec.chunk_id.can_take_data = 0;
8707 	chk->asoc = &stcb->asoc;
8708 	chk->book_size = SCTP_SIZE32(chk->send_size = sizeof(struct sctp_chunkhdr));
8709 
8710 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
8711 	if (chk->data == NULL) {
8712 		sctp_free_a_chunk(stcb, chk);
8713 		return (ENOMEM);
8714 	}
8715 	chk->data->m_data += SCTP_MIN_OVERHEAD;
8716 
8717 	/* setup chunk parameters */
8718 	chk->sent = SCTP_DATAGRAM_UNSENT;
8719 	chk->snd_count = 0;
8720 	chk->whoTo = asoc->primary_destination;
8721 	atomic_add_int(&chk->whoTo->ref_count, 1);
8722 
8723 	ch = mtod(chk->data, struct sctp_chunkhdr *);
8724 	ch->chunk_type = SCTP_STREAM_RESET;
8725 	ch->chunk_flags = 0;
8726 	ch->chunk_length = htons(chk->send_size);
8727 	chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8728 
8729 	seq = stcb->asoc.str_reset_seq_out;
8730 	if (send_out_req) {
8731 		sctp_add_stream_reset_out(chk, number_entries, list,
8732 		    seq, resp_seq, (stcb->asoc.sending_seq - 1));
8733 		asoc->stream_reset_out_is_outstanding = 1;
8734 		seq++;
8735 		asoc->stream_reset_outstanding++;
8736 	}
8737 	if (send_in_req) {
8738 		sctp_add_stream_reset_in(chk, number_entries, list, seq);
8739 		asoc->stream_reset_outstanding++;
8740 	}
8741 	if (send_tsn_req) {
8742 		sctp_add_stream_reset_tsn(chk, seq);
8743 		asoc->stream_reset_outstanding++;
8744 	}
8745 	asoc->str_reset = chk;
8746 
8747 	/* insert the chunk for sending */
8748 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
8749 	    chk,
8750 	    sctp_next);
8751 	asoc->ctrl_queue_cnt++;
8752 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
8753 	return (0);
8754 }
8755 
8756 void
8757 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
8758     struct mbuf *err_cause)
8759 {
8760 	/*
8761 	 * Formulate the abort message, and send it back down.
8762 	 */
8763 	struct mbuf *mout;
8764 	struct sctp_abort_msg *abm;
8765 	struct ip *iph, *iph_out;
8766 	struct ip6_hdr *ip6, *ip6_out;
8767 	int iphlen_out;
8768 
8769 	/* don't respond to ABORT with ABORT */
8770 	if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
8771 		if (err_cause)
8772 			sctp_m_freem(err_cause);
8773 		return;
8774 	}
8775 	mout = sctp_get_mbuf_for_msg((sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg)),
8776 	    1, M_DONTWAIT, 1, MT_HEADER);
8777 	if (mout == NULL) {
8778 		if (err_cause)
8779 			sctp_m_freem(err_cause);
8780 		return;
8781 	}
8782 	iph = mtod(m, struct ip *);
8783 	iph_out = NULL;
8784 	ip6_out = NULL;
8785 	if (iph->ip_v == IPVERSION) {
8786 		iph_out = mtod(mout, struct ip *);
8787 		mout->m_len = sizeof(*iph_out) + sizeof(*abm);
8788 		mout->m_next = err_cause;
8789 
8790 		/* Fill in the IP header for the ABORT */
8791 		iph_out->ip_v = IPVERSION;
8792 		iph_out->ip_hl = (sizeof(struct ip) / 4);
8793 		iph_out->ip_tos = (u_char)0;
8794 		iph_out->ip_id = 0;
8795 		iph_out->ip_off = 0;
8796 		iph_out->ip_ttl = MAXTTL;
8797 		iph_out->ip_p = IPPROTO_SCTP;
8798 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
8799 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
8800 		/* let IP layer calculate this */
8801 		iph_out->ip_sum = 0;
8802 
8803 		iphlen_out = sizeof(*iph_out);
8804 		abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
8805 	} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
8806 		ip6 = (struct ip6_hdr *)iph;
8807 		ip6_out = mtod(mout, struct ip6_hdr *);
8808 		mout->m_len = sizeof(*ip6_out) + sizeof(*abm);
8809 		mout->m_next = err_cause;
8810 
8811 		/* Fill in the IP6 header for the ABORT */
8812 		ip6_out->ip6_flow = ip6->ip6_flow;
8813 		ip6_out->ip6_hlim = ip6_defhlim;
8814 		ip6_out->ip6_nxt = IPPROTO_SCTP;
8815 		ip6_out->ip6_src = ip6->ip6_dst;
8816 		ip6_out->ip6_dst = ip6->ip6_src;
8817 
8818 		iphlen_out = sizeof(*ip6_out);
8819 		abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
8820 	} else {
8821 		/* Currently not supported */
8822 		return;
8823 	}
8824 
8825 	abm->sh.src_port = sh->dest_port;
8826 	abm->sh.dest_port = sh->src_port;
8827 	abm->sh.checksum = 0;
8828 	if (vtag == 0) {
8829 		abm->sh.v_tag = sh->v_tag;
8830 		abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
8831 	} else {
8832 		abm->sh.v_tag = htonl(vtag);
8833 		abm->msg.ch.chunk_flags = 0;
8834 	}
8835 	abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
8836 
8837 	if (err_cause) {
8838 		struct mbuf *m_tmp = err_cause;
8839 		int err_len = 0;
8840 
8841 		/* get length of the err_cause chain */
8842 		while (m_tmp != NULL) {
8843 			err_len += m_tmp->m_len;
8844 			m_tmp = m_tmp->m_next;
8845 		}
8846 		mout->m_pkthdr.len = mout->m_len + err_len;
8847 		if (err_len % 4) {
8848 			/* need pad at end of chunk */
8849 			uint32_t cpthis = 0;
8850 			int padlen;
8851 
8852 			padlen = 4 - (mout->m_pkthdr.len % 4);
8853 			m_copyback(mout, mout->m_pkthdr.len, padlen, (caddr_t)&cpthis);
8854 		}
8855 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
8856 	} else {
8857 		mout->m_pkthdr.len = mout->m_len;
8858 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
8859 	}
8860 
8861 	/* add checksum */
8862 	if ((sctp_no_csum_on_loopback) &&
8863 	    (m->m_pkthdr.rcvif) &&
8864 	    (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
8865 		abm->sh.checksum = 0;
8866 	} else {
8867 		abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
8868 	}
8869 
8870 	/* zap the rcvif, it should be null */
8871 	mout->m_pkthdr.rcvif = 0;
8872 	if (iph_out != NULL) {
8873 		struct route ro;
8874 
8875 		/* zap the stack pointer to the route */
8876 		bzero(&ro, sizeof ro);
8877 #ifdef SCTP_DEBUG
8878 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
8879 			printf("sctp_send_abort calling ip_output:\n");
8880 			sctp_print_address_pkt(iph_out, &abm->sh);
8881 		}
8882 #endif
8883 		/* set IPv4 length */
8884 		iph_out->ip_len = mout->m_pkthdr.len;
8885 		/* out it goes */
8886 		(void)ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL
8887 		    ,NULL
8888 		    );
8889 		/* Free the route if we got one back */
8890 		if (ro.ro_rt)
8891 			RTFREE(ro.ro_rt);
8892 	} else if (ip6_out != NULL) {
8893 		struct route_in6 ro;
8894 
8895 
8896 		/* zap the stack pointer to the route */
8897 		bzero(&ro, sizeof(ro));
8898 #ifdef SCTP_DEBUG
8899 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
8900 			printf("sctp_send_abort calling ip6_output:\n");
8901 			sctp_print_address_pkt((struct ip *)ip6_out, &abm->sh);
8902 		}
8903 #endif
8904 		ip6_output(mout, NULL, &ro, 0, NULL, NULL
8905 		    ,NULL
8906 		    );
8907 		/* Free the route if we got one back */
8908 		if (ro.ro_rt)
8909 			RTFREE(ro.ro_rt);
8910 	}
8911 	SCTP_STAT_INCR(sctps_sendpackets);
8912 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
8913 }
8914 
8915 void
8916 sctp_send_operr_to(struct mbuf *m, int iphlen,
8917     struct mbuf *scm,
8918     uint32_t vtag)
8919 {
8920 	struct sctphdr *ihdr;
8921 	int retcode;
8922 	struct sctphdr *ohdr;
8923 	struct sctp_chunkhdr *ophdr;
8924 
8925 	struct ip *iph;
8926 
8927 #ifdef SCTP_DEBUG
8928 	struct sockaddr_in6 lsa6, fsa6;
8929 
8930 #endif
8931 	uint32_t val;
8932 
8933 	iph = mtod(m, struct ip *);
8934 	ihdr = (struct sctphdr *)((caddr_t)iph + iphlen);
8935 	if (!(scm->m_flags & M_PKTHDR)) {
8936 		/* must be a pkthdr */
8937 		printf("Huh, not a packet header in send_operr\n");
8938 		sctp_m_freem(scm);
8939 		return;
8940 	}
8941 	M_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), M_DONTWAIT);
8942 	if (scm == NULL) {
8943 		/* can't send because we can't add a mbuf */
8944 		return;
8945 	}
8946 	ohdr = mtod(scm, struct sctphdr *);
8947 	ohdr->src_port = ihdr->dest_port;
8948 	ohdr->dest_port = ihdr->src_port;
8949 	ohdr->v_tag = vtag;
8950 	ohdr->checksum = 0;
8951 	ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
8952 	ophdr->chunk_type = SCTP_OPERATION_ERROR;
8953 	ophdr->chunk_flags = 0;
8954 	ophdr->chunk_length = htons(scm->m_pkthdr.len - sizeof(struct sctphdr));
8955 	if (scm->m_pkthdr.len % 4) {
8956 		/* need padding */
8957 		uint32_t cpthis = 0;
8958 		int padlen;
8959 
8960 		padlen = 4 - (scm->m_pkthdr.len % 4);
8961 		m_copyback(scm, scm->m_pkthdr.len, padlen, (caddr_t)&cpthis);
8962 	}
8963 	if ((sctp_no_csum_on_loopback) &&
8964 	    (m->m_pkthdr.rcvif) &&
8965 	    (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
8966 		val = 0;
8967 	} else {
8968 		val = sctp_calculate_sum(scm, NULL, 0);
8969 	}
8970 	ohdr->checksum = val;
8971 	if (iph->ip_v == IPVERSION) {
8972 		/* V4 */
8973 		struct ip *out;
8974 		struct route ro;
8975 
8976 		M_PREPEND(scm, sizeof(struct ip), M_DONTWAIT);
8977 		if (scm == NULL)
8978 			return;
8979 		bzero(&ro, sizeof ro);
8980 		out = mtod(scm, struct ip *);
8981 		out->ip_v = iph->ip_v;
8982 		out->ip_hl = (sizeof(struct ip) / 4);
8983 		out->ip_tos = iph->ip_tos;
8984 		out->ip_id = iph->ip_id;
8985 		out->ip_off = 0;
8986 		out->ip_ttl = MAXTTL;
8987 		out->ip_p = IPPROTO_SCTP;
8988 		out->ip_sum = 0;
8989 		out->ip_src = iph->ip_dst;
8990 		out->ip_dst = iph->ip_src;
8991 		out->ip_len = scm->m_pkthdr.len;
8992 		retcode = ip_output(scm, 0, &ro, IP_RAWOUTPUT, NULL
8993 		    ,NULL
8994 		    );
8995 		SCTP_STAT_INCR(sctps_sendpackets);
8996 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
8997 		/* Free the route if we got one back */
8998 		if (ro.ro_rt)
8999 			RTFREE(ro.ro_rt);
9000 	} else {
9001 		/* V6 */
9002 		struct route_in6 ro;
9003 
9004 		struct ip6_hdr *out6, *in6;
9005 
9006 		M_PREPEND(scm, sizeof(struct ip6_hdr), M_DONTWAIT);
9007 		if (scm == NULL)
9008 			return;
9009 		bzero(&ro, sizeof ro);
9010 		in6 = mtod(m, struct ip6_hdr *);
9011 		out6 = mtod(scm, struct ip6_hdr *);
9012 		out6->ip6_flow = in6->ip6_flow;
9013 		out6->ip6_hlim = ip6_defhlim;
9014 		out6->ip6_nxt = IPPROTO_SCTP;
9015 		out6->ip6_src = in6->ip6_dst;
9016 		out6->ip6_dst = in6->ip6_src;
9017 
9018 #ifdef SCTP_DEBUG
9019 		bzero(&lsa6, sizeof(lsa6));
9020 		lsa6.sin6_len = sizeof(lsa6);
9021 		lsa6.sin6_family = AF_INET6;
9022 		lsa6.sin6_addr = out6->ip6_src;
9023 		bzero(&fsa6, sizeof(fsa6));
9024 		fsa6.sin6_len = sizeof(fsa6);
9025 		fsa6.sin6_family = AF_INET6;
9026 		fsa6.sin6_addr = out6->ip6_dst;
9027 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9028 			printf("sctp_operr_to calling ipv6 output:\n");
9029 			printf("src: ");
9030 			sctp_print_address((struct sockaddr *)&lsa6);
9031 			printf("dst ");
9032 			sctp_print_address((struct sockaddr *)&fsa6);
9033 		}
9034 #endif				/* SCTP_DEBUG */
9035 		ip6_output(scm, NULL, &ro, 0, NULL, NULL
9036 		    ,NULL
9037 		    );
9038 		SCTP_STAT_INCR(sctps_sendpackets);
9039 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
9040 		/* Free the route if we got one back */
9041 		if (ro.ro_rt)
9042 			RTFREE(ro.ro_rt);
9043 	}
9044 }
9045 
9046 
9047 
9048 static struct mbuf *
9049 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
9050     struct uio *uio,
9051     struct sctp_sndrcvinfo *srcv,
9052     int max_send_len,
9053     int user_marks_eor,
9054     int *error,
9055     uint32_t * sndout,
9056     struct mbuf **new_tail)
9057 {
9058 	int left, cancpy, willcpy, need_hdr = 0;
9059 	struct mbuf *m, *prev, *head;
9060 
9061 	left = min(uio->uio_resid, max_send_len);
9062 	/* Always get a header just in case */
9063 	need_hdr = 1;
9064 
9065 	head = sctp_get_mbuf_for_msg(left, need_hdr, M_WAIT, 0, MT_DATA);
9066 	cancpy = M_TRAILINGSPACE(head);
9067 	willcpy = min(cancpy, left);
9068 	*error = uiomove(mtod(head, caddr_t), willcpy, uio);
9069 	if (*error) {
9070 		sctp_m_freem(head);
9071 		return (NULL);
9072 	}
9073 	*sndout += willcpy;
9074 	left -= willcpy;
9075 	head->m_len = willcpy;
9076 	m = head;
9077 	*new_tail = head;
9078 	while (left > 0) {
9079 		/* move in user data */
9080 		m->m_next = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 0, MT_DATA);
9081 		if (m->m_next == NULL) {
9082 			sctp_m_freem(head);
9083 			*new_tail = NULL;
9084 			*error = ENOMEM;
9085 			return (NULL);
9086 		}
9087 		prev = m;
9088 		m = m->m_next;
9089 		cancpy = M_TRAILINGSPACE(m);
9090 		willcpy = min(cancpy, left);
9091 		*error = uiomove(mtod(m, caddr_t), willcpy, uio);
9092 		if (*error) {
9093 			sctp_m_freem(head);
9094 			*new_tail = NULL;
9095 			*error = EFAULT;
9096 			return (NULL);
9097 		}
9098 		m->m_len = willcpy;
9099 		left -= willcpy;
9100 		*sndout += willcpy;
9101 		*new_tail = m;
9102 		if (left == 0) {
9103 			m->m_next = NULL;
9104 		}
9105 	}
9106 	return (head);
9107 }
9108 
9109 static int
9110 sctp_copy_one(struct sctp_stream_queue_pending *sp,
9111     struct uio *uio,
9112     int resv_upfront)
9113 {
9114 	int left, cancpy, willcpy, error;
9115 	struct mbuf *m, *head;
9116 	int cpsz = 0;
9117 
9118 	/* First one gets a header */
9119 	left = sp->length;
9120 	head = m = sctp_get_mbuf_for_msg((left + resv_upfront), 1, M_WAIT, 0, MT_DATA);
9121 	if (m == NULL) {
9122 		return (ENOMEM);
9123 	}
9124 	/*
9125 	 * Add this one for m in now, that way if the alloc fails we won't
9126 	 * have a bad cnt.
9127 	 */
9128 	m->m_data += resv_upfront;
9129 	cancpy = M_TRAILINGSPACE(m);
9130 	willcpy = min(cancpy, left);
9131 	while (left > 0) {
9132 		/* move in user data */
9133 		error = uiomove(mtod(m, caddr_t), willcpy, uio);
9134 		if (error) {
9135 			sctp_m_freem(head);
9136 			return (error);
9137 		}
9138 		m->m_len = willcpy;
9139 		left -= willcpy;
9140 		cpsz += willcpy;
9141 		if (left > 0) {
9142 			m->m_next = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 0, MT_DATA);
9143 			if (m->m_next == NULL) {
9144 				/*
9145 				 * the head goes back to caller, he can free
9146 				 * the rest
9147 				 */
9148 				sctp_m_freem(head);
9149 				return (ENOMEM);
9150 			}
9151 			m = m->m_next;
9152 			cancpy = M_TRAILINGSPACE(m);
9153 			willcpy = min(cancpy, left);
9154 		} else {
9155 			sp->tail_mbuf = m;
9156 			m->m_next = NULL;
9157 		}
9158 	}
9159 	sp->data = head;
9160 	sp->length = cpsz;
9161 	return (0);
9162 }
9163 
9164 
9165 
9166 static struct sctp_stream_queue_pending *
9167 sctp_copy_it_in(struct sctp_tcb *stcb,
9168     struct sctp_association *asoc,
9169     struct sctp_sndrcvinfo *srcv,
9170     struct uio *uio,
9171     struct sctp_nets *net,
9172     int max_send_len,
9173     int user_marks_eor,
9174     int *errno,
9175     int non_blocking)
9176 {
9177 	/*
9178 	 * This routine must be very careful in its work. Protocol
9179 	 * processing is up and running so care must be taken to spl...()
9180 	 * when you need to do something that may effect the stcb/asoc. The
9181 	 * sb is locked however. When data is copied the protocol processing
9182 	 * should be enabled since this is a slower operation...
9183 	 */
9184 	struct sctp_stream_queue_pending *sp = NULL;
9185 	int resv_in_first;
9186 
9187 	*errno = 0;
9188 	/*
9189 	 * Unless E_EOR mode is on, we must make a send FIT in one call.
9190 	 */
9191 	if (((user_marks_eor == 0) && non_blocking) &&
9192 	    (uio->uio_resid > stcb->sctp_socket->so_snd.sb_hiwat)) {
9193 		/* It will NEVER fit */
9194 		*errno = EMSGSIZE;
9195 		goto out_now;
9196 	}
9197 	/* Now can we send this? */
9198 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
9199 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
9200 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
9201 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
9202 		/* got data while shutting down */
9203 		*errno = ECONNRESET;
9204 		goto out_now;
9205 	}
9206 	sp = (struct sctp_stream_queue_pending *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_strmoq);
9207 	if (sp == NULL) {
9208 		*errno = ENOMEM;
9209 		goto out_now;
9210 	}
9211 	SCTP_INCR_STRMOQ_COUNT();
9212 	sp->act_flags = 0;
9213 	sp->sinfo_flags = srcv->sinfo_flags;
9214 	sp->timetolive = srcv->sinfo_timetolive;
9215 	sp->ppid = srcv->sinfo_ppid;
9216 	sp->context = srcv->sinfo_context;
9217 	sp->strseq = 0;
9218 	SCTP_GETTIME_TIMEVAL(&sp->ts);
9219 
9220 	sp->stream = srcv->sinfo_stream;
9221 	sp->length = min(uio->uio_resid, max_send_len);
9222 	if ((sp->length == uio->uio_resid) &&
9223 	    ((user_marks_eor == 0) ||
9224 	    (srcv->sinfo_flags & SCTP_EOF) ||
9225 	    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))
9226 	    ) {
9227 		sp->msg_is_complete = 1;
9228 	} else {
9229 		sp->msg_is_complete = 0;
9230 	}
9231 	sp->some_taken = 0;
9232 	resv_in_first = sizeof(struct sctp_data_chunk);
9233 	sp->data = sp->tail_mbuf = NULL;
9234 	*errno = sctp_copy_one(sp, uio, resv_in_first);
9235 	if (*errno) {
9236 		sctp_free_a_strmoq(stcb, sp);
9237 		sp->data = NULL;
9238 		sp->net = NULL;
9239 		sp = NULL;
9240 	} else {
9241 		if (sp->sinfo_flags & SCTP_ADDR_OVER) {
9242 			sp->net = net;
9243 			sp->addr_over = 1;
9244 		} else {
9245 			sp->net = asoc->primary_destination;
9246 			sp->addr_over = 0;
9247 		}
9248 		atomic_add_int(&sp->net->ref_count, 1);
9249 		sp->data->m_pkthdr.len = sp->length;
9250 		sctp_set_prsctp_policy(stcb, sp);
9251 	}
9252 out_now:
9253 	return (sp);
9254 }
9255 
9256 
9257 int
9258 sctp_sosend(struct socket *so,
9259     struct sockaddr *addr,
9260     struct uio *uio,
9261     struct mbuf *top,
9262     struct mbuf *control,
9263     int flags
9264     ,
9265     struct thread *p
9266 )
9267 {
9268 	struct sctp_inpcb *inp;
9269 	int s, error, use_rcvinfo = 0;
9270 	struct sctp_sndrcvinfo srcv;
9271 
9272 	inp = (struct sctp_inpcb *)so->so_pcb;
9273 	s = splnet();
9274 	if (control) {
9275 		/* process cmsg snd/rcv info (maybe a assoc-id) */
9276 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
9277 		    sizeof(srcv))) {
9278 			/* got one */
9279 			use_rcvinfo = 1;
9280 		}
9281 	}
9282 	error = sctp_lower_sosend(so, addr, uio, top, control, flags,
9283 	    use_rcvinfo, &srcv, p);
9284 	splx(s);
9285 	return (error);
9286 }
9287 
9288 
9289 extern unsigned int sctp_add_more_threshold;
9290 int
9291 sctp_lower_sosend(struct socket *so,
9292     struct sockaddr *addr,
9293     struct uio *uio,
9294     struct mbuf *top,
9295     struct mbuf *control,
9296     int flags,
9297     int use_rcvinfo,
9298     struct sctp_sndrcvinfo *srcv,
9299     struct thread *p
9300 )
9301 {
9302 	unsigned int sndlen, max_len;
9303 	int error, len;
9304 	int s, queue_only = 0, queue_only_for_init = 0;
9305 	int free_cnt_applied = 0;
9306 	int un_sent = 0;
9307 	int now_filled = 0;
9308 	struct sctp_block_entry be;
9309 	struct sctp_inpcb *inp;
9310 	struct sctp_tcb *stcb = NULL;
9311 	struct timeval now;
9312 	struct sctp_nets *net;
9313 	struct sctp_association *asoc;
9314 	struct sctp_inpcb *t_inp;
9315 	int create_lock_applied = 0;
9316 	int nagle_applies = 0;
9317 	int some_on_control = 0;
9318 	int got_all_of_the_send = 0;
9319 	int hold_tcblock = 0;
9320 	int non_blocking = 0;
9321 
9322 	error = 0;
9323 	net = NULL;
9324 	stcb = NULL;
9325 	asoc = NULL;
9326 	t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
9327 	if (uio)
9328 		sndlen = uio->uio_resid;
9329 	else
9330 		sndlen = top->m_pkthdr.len;
9331 
9332 	s = splnet();
9333 	hold_tcblock = 0;
9334 
9335 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
9336 	    (inp->sctp_socket->so_qlimit)) {
9337 		/* The listener can NOT send */
9338 		error = EFAULT;
9339 		splx(s);
9340 		goto out_unlocked;
9341 	}
9342 	if ((use_rcvinfo) && srcv) {
9343 		if (srcv->sinfo_flags & SCTP_SENDALL) {
9344 			/* its a sendall */
9345 			error = sctp_sendall(inp, uio, top, srcv);
9346 			top = NULL;
9347 			splx(s);
9348 			goto out_unlocked;
9349 		}
9350 	}
9351 	/* now we must find the assoc */
9352 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
9353 		SCTP_INP_RLOCK(inp);
9354 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
9355 		if (stcb == NULL) {
9356 			SCTP_INP_RUNLOCK(inp);
9357 			error = ENOTCONN;
9358 			splx(s);
9359 			goto out_unlocked;
9360 		}
9361 		hold_tcblock = 0;
9362 		SCTP_INP_RUNLOCK(inp);
9363 		if (addr)
9364 			/* Must locate the net structure if addr given */
9365 			net = sctp_findnet(stcb, addr);
9366 		else
9367 			net = stcb->asoc.primary_destination;
9368 
9369 	} else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
9370 		stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
9371 		if (stcb) {
9372 			if (addr)
9373 				/*
9374 				 * Must locate the net structure if addr
9375 				 * given
9376 				 */
9377 				net = sctp_findnet(stcb, addr);
9378 			else
9379 				net = stcb->asoc.primary_destination;
9380 		}
9381 		hold_tcblock = 0;
9382 	} else if (addr) {
9383 		/*
9384 		 * Since we did not use findep we must increment it, and if
9385 		 * we don't find a tcb decrement it.
9386 		 */
9387 		SCTP_INP_WLOCK(inp);
9388 		SCTP_INP_INCR_REF(inp);
9389 		SCTP_INP_WUNLOCK(inp);
9390 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
9391 		if (stcb == NULL) {
9392 			SCTP_INP_WLOCK(inp);
9393 			SCTP_INP_DECR_REF(inp);
9394 			SCTP_INP_WUNLOCK(inp);
9395 		} else {
9396 			hold_tcblock = 1;
9397 		}
9398 	}
9399 	if ((stcb == NULL) && (addr)) {
9400 		/* Possible implicit send? */
9401 		SCTP_ASOC_CREATE_LOCK(inp);
9402 		create_lock_applied = 1;
9403 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
9404 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
9405 			/* Should I really unlock ? */
9406 			error = EFAULT;
9407 			splx(s);
9408 			goto out_unlocked;
9409 
9410 		}
9411 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
9412 		    (addr->sa_family == AF_INET6)) {
9413 			error = EINVAL;
9414 			splx(s);
9415 			goto out_unlocked;
9416 		}
9417 	}
9418 	if (stcb == NULL) {
9419 		if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
9420 			error = ENOTCONN;
9421 			splx(s);
9422 			goto out_unlocked;
9423 		} else if (addr == NULL) {
9424 			error = ENOENT;
9425 			splx(s);
9426 			goto out_unlocked;
9427 		} else {
9428 			/*
9429 			 * UDP style, we must go ahead and start the INIT
9430 			 * process
9431 			 */
9432 			if ((use_rcvinfo) && (srcv) &&
9433 			    (srcv->sinfo_flags & SCTP_ABORT)) {
9434 				/* User asks to abort a non-existant asoc */
9435 				error = ENOENT;
9436 				splx(s);
9437 				goto out_unlocked;
9438 			}
9439 			/* get an asoc/stcb struct */
9440 			stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
9441 			if (stcb == NULL) {
9442 				/* Error is setup for us in the call */
9443 				splx(s);
9444 				goto out_unlocked;
9445 			}
9446 			if (create_lock_applied) {
9447 				SCTP_ASOC_CREATE_UNLOCK(inp);
9448 				create_lock_applied = 0;
9449 			} else {
9450 				printf("Huh-3? create lock should have been on??\n");
9451 			}
9452 			/*
9453 			 * Turn on queue only flag to prevent data from
9454 			 * being sent
9455 			 */
9456 			queue_only = 1;
9457 			asoc = &stcb->asoc;
9458 			asoc->state = SCTP_STATE_COOKIE_WAIT;
9459 			SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
9460 
9461 			/* initialize authentication params for the assoc */
9462 			sctp_initialize_auth_params(inp, stcb);
9463 
9464 			if (control) {
9465 				/*
9466 				 * see if a init structure exists in cmsg
9467 				 * headers
9468 				 */
9469 				struct sctp_initmsg initm;
9470 				int i;
9471 
9472 				if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
9473 				    sizeof(initm))) {
9474 					/*
9475 					 * we have an INIT override of the
9476 					 * default
9477 					 */
9478 					if (initm.sinit_max_attempts)
9479 						asoc->max_init_times = initm.sinit_max_attempts;
9480 					if (initm.sinit_num_ostreams)
9481 						asoc->pre_open_streams = initm.sinit_num_ostreams;
9482 					if (initm.sinit_max_instreams)
9483 						asoc->max_inbound_streams = initm.sinit_max_instreams;
9484 					if (initm.sinit_max_init_timeo)
9485 						asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
9486 					if (asoc->streamoutcnt < asoc->pre_open_streams) {
9487 						/* Default is NOT correct */
9488 #ifdef SCTP_DEBUG
9489 						if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
9490 							printf("Ok, defout:%d pre_open:%d\n",
9491 							    asoc->streamoutcnt, asoc->pre_open_streams);
9492 						}
9493 #endif
9494 						SCTP_FREE(asoc->strmout);
9495 						asoc->strmout = NULL;
9496 						asoc->streamoutcnt = asoc->pre_open_streams;
9497 						/*
9498 						 * What happens if this
9499 						 * fails? .. we panic ...
9500 						 */
9501 						{
9502 							struct sctp_stream_out *tmp_str;
9503 							int had_lock = 0;
9504 
9505 							if (hold_tcblock) {
9506 								had_lock = 1;
9507 								SCTP_TCB_UNLOCK(stcb);
9508 							}
9509 							SCTP_MALLOC(tmp_str,
9510 							    struct sctp_stream_out *,
9511 							    asoc->streamoutcnt *
9512 							    sizeof(struct sctp_stream_out),
9513 							    "StreamsOut");
9514 							if (had_lock) {
9515 								SCTP_TCB_LOCK(stcb);
9516 							}
9517 							asoc->strmout = tmp_str;
9518 						}
9519 						for (i = 0; i < asoc->streamoutcnt; i++) {
9520 							/*
9521 							 * inbound side must
9522 							 * be set to 0xffff,
9523 							 * also NOTE when we
9524 							 * get the INIT-ACK
9525 							 * back (for INIT
9526 							 * sender) we MUST
9527 							 * reduce the count
9528 							 * (streamoutcnt)
9529 							 * but first check
9530 							 * if we sent to any
9531 							 * of the upper
9532 							 * streams that were
9533 							 * dropped (if some
9534 							 * were). Those that
9535 							 * were dropped must
9536 							 * be notified to
9537 							 * the upper layer
9538 							 * as failed to
9539 							 * send.
9540 							 */
9541 							asoc->strmout[i].next_sequence_sent = 0x0;
9542 							TAILQ_INIT(&asoc->strmout[i].outqueue);
9543 							asoc->strmout[i].stream_no = i;
9544 							asoc->strmout[i].last_msg_incomplete = 0;
9545 							asoc->strmout[i].next_spoke.tqe_next = 0;
9546 							asoc->strmout[i].next_spoke.tqe_prev = 0;
9547 						}
9548 					}
9549 				}
9550 			}
9551 			hold_tcblock = 1;
9552 			/* out with the INIT */
9553 			queue_only_for_init = 1;
9554 			/*
9555 			 * we may want to dig in after this call and adjust
9556 			 * the MTU value. It defaulted to 1500 (constant)
9557 			 * but the ro structure may now have an update and
9558 			 * thus we may need to change it BEFORE we append
9559 			 * the message.
9560 			 */
9561 			net = stcb->asoc.primary_destination;
9562 			asoc = &stcb->asoc;
9563 		}
9564 	}
9565 	if (((so->so_state & SS_NBIO)
9566 	    || (flags & MSG_NBIO)
9567 	    )) {
9568 		non_blocking = 1;
9569 	}
9570 	asoc = &stcb->asoc;
9571 	/* would we block? */
9572 	if (non_blocking) {
9573 		if ((so->so_snd.sb_hiwat <
9574 		    (sndlen + stcb->asoc.total_output_queue_size)) ||
9575 		    (stcb->asoc.chunks_on_out_queue >
9576 		    sctp_max_chunks_on_queue)) {
9577 			error = EWOULDBLOCK;
9578 			splx(s);
9579 			goto out_unlocked;
9580 		}
9581 	}
9582 	/* Keep the stcb from being freed under our feet */
9583 	atomic_add_int(&stcb->asoc.refcnt, 1);
9584 	free_cnt_applied = 1;
9585 
9586 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
9587 		error = ECONNRESET;
9588 		goto out_unlocked;
9589 	}
9590 	if (create_lock_applied) {
9591 		SCTP_ASOC_CREATE_UNLOCK(inp);
9592 		create_lock_applied = 0;
9593 	}
9594 	if (asoc->stream_reset_outstanding) {
9595 		/*
9596 		 * Can't queue any data while stream reset is underway.
9597 		 */
9598 		error = EAGAIN;
9599 		goto out_unlocked;
9600 	}
9601 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
9602 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
9603 		queue_only = 1;
9604 	}
9605 	if ((use_rcvinfo == 0) || (srcv == NULL)) {
9606 		/* Grab the default stuff from the asoc */
9607 		srcv = &stcb->asoc.def_send;
9608 	}
9609 	/* we are now done with all control */
9610 	if (control) {
9611 		sctp_m_freem(control);
9612 		control = NULL;
9613 	}
9614 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
9615 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
9616 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
9617 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
9618 		if ((use_rcvinfo) &&
9619 		    (srcv->sinfo_flags & SCTP_ABORT)) {
9620 			;
9621 		} else {
9622 			error = ECONNRESET;
9623 			splx(s);
9624 			goto out_unlocked;
9625 		}
9626 	}
9627 	/* Ok, we will attempt a msgsnd :> */
9628 	if (p) {
9629 		p->td_proc->p_stats->p_ru.ru_msgsnd++;
9630 	}
9631 	if (stcb) {
9632 		if (net && ((srcv->sinfo_flags & SCTP_ADDR_OVER))) {
9633 			/* we take the override or the unconfirmed */
9634 			;
9635 		} else {
9636 			net = stcb->asoc.primary_destination;
9637 		}
9638 	}
9639 	if ((net->flight_size > net->cwnd) && (sctp_cmt_on_off == 0)) {
9640 		/*
9641 		 * CMT: Added check for CMT above. net above is the primary
9642 		 * dest. If CMT is ON, sender should always attempt to send
9643 		 * with the output routine sctp_fill_outqueue() that loops
9644 		 * through all destination addresses. Therefore, if CMT is
9645 		 * ON, queue_only is NOT set to 1 here, so that
9646 		 * sctp_chunk_output() can be called below.
9647 		 */
9648 		queue_only = 1;
9649 
9650 	} else if (asoc->ifp_had_enobuf) {
9651 		SCTP_STAT_INCR(sctps_ifnomemqueued);
9652 		if (net->flight_size > (net->mtu * 2))
9653 			queue_only = 1;
9654 		asoc->ifp_had_enobuf = 0;
9655 	} else {
9656 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9657 		    ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)));
9658 	}
9659 	/* Are we aborting? */
9660 	if (srcv->sinfo_flags & SCTP_ABORT) {
9661 		struct mbuf *mm;
9662 		int tot_demand, tot_out, max;
9663 
9664 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
9665 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
9666 			/* It has to be up before we abort */
9667 			/* how big is the user initiated abort? */
9668 			error = EINVAL;
9669 			goto out;
9670 		}
9671 		if (hold_tcblock) {
9672 			SCTP_TCB_UNLOCK(stcb);
9673 			hold_tcblock = 0;
9674 		}
9675 		if (top) {
9676 			mm = sctp_get_mbuf_for_msg(1, 1, M_WAIT, 1, MT_DATA);
9677 			if (top->m_flags & M_PKTHDR)
9678 				tot_out = top->m_pkthdr.len;
9679 			else {
9680 				struct mbuf *cntm;
9681 
9682 				tot_out = 0;
9683 				cntm = top;
9684 				while (cntm) {
9685 					tot_out += cntm->m_len;
9686 					cntm = cntm->m_next;
9687 				}
9688 			}
9689 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
9690 		} else {
9691 			/* Must fit in a MTU */
9692 			tot_out = uio->uio_resid;
9693 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
9694 			mm = sctp_get_mbuf_for_msg(tot_demand, 1, M_WAIT, 1, MT_DATA);
9695 		}
9696 		if (mm == NULL) {
9697 			error = ENOMEM;
9698 			goto out;
9699 		}
9700 		max = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
9701 		max -= sizeof(struct sctp_abort_msg);
9702 		if (tot_out > max) {
9703 			tot_out = max;
9704 		}
9705 		if (mm) {
9706 			struct sctp_paramhdr *ph;
9707 
9708 			/* now move forward the data pointer */
9709 			ph = mtod(mm, struct sctp_paramhdr *);
9710 			ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
9711 			ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
9712 			ph++;
9713 			mm->m_pkthdr.len = tot_out + sizeof(struct sctp_paramhdr);
9714 			mm->m_len = mm->m_pkthdr.len;
9715 			if (top == NULL) {
9716 				error = uiomove((caddr_t)ph, (int)tot_out, uio);
9717 				if (error) {
9718 					/*
9719 					 * Here if we can't get his data we
9720 					 * still abort we just don't get to
9721 					 * send the users note :-0
9722 					 */
9723 					sctp_m_freem(mm);
9724 					mm = NULL;
9725 				}
9726 			} else {
9727 				mm->m_next = top;
9728 			}
9729 		}
9730 		if (hold_tcblock == 0) {
9731 			SCTP_TCB_LOCK(stcb);
9732 			hold_tcblock = 1;
9733 		}
9734 		atomic_add_int(&stcb->asoc.refcnt, -1);
9735 		free_cnt_applied = 0;
9736 		/* release this lock, otherwise we hang on ourselves */
9737 		sctp_abort_an_association(stcb->sctp_ep, stcb,
9738 		    SCTP_RESPONSE_TO_USER_REQ,
9739 		    mm);
9740 		/* now relock the stcb so everything is sane */
9741 		hold_tcblock = 0;
9742 		stcb = NULL;
9743 		goto out_unlocked;
9744 	}
9745 	/* Calculate the maximum we can send */
9746 	if (so->so_snd.sb_hiwat > stcb->asoc.total_output_queue_size) {
9747 		max_len = so->so_snd.sb_hiwat - stcb->asoc.total_output_queue_size;
9748 	} else {
9749 		max_len = 0;
9750 	}
9751 	if (hold_tcblock) {
9752 		SCTP_TCB_UNLOCK(stcb);
9753 		hold_tcblock = 0;
9754 	}
9755 	splx(s);
9756 	/* Is the stream no. valid? */
9757 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
9758 		/* Invalid stream number */
9759 		error = EINVAL;
9760 		goto out_unlocked;
9761 	}
9762 	if (asoc->strmout == NULL) {
9763 		/* huh? software error */
9764 		error = EFAULT;
9765 		goto out_unlocked;
9766 	}
9767 	len = 0;
9768 	if (max_len < sctp_add_more_threshold) {
9769 		/* No room right no ! */
9770 		SOCKBUF_LOCK(&so->so_snd);
9771 		while (so->so_snd.sb_hiwat < (stcb->asoc.total_output_queue_size + sctp_add_more_threshold)) {
9772 #ifdef SCTP_BLK_LOGGING
9773 			sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA,
9774 			    so, asoc, uio->uio_resid);
9775 #endif
9776 			be.error = 0;
9777 			stcb->block_entry = &be;
9778 			error = sbwait(&so->so_snd);
9779 			stcb->block_entry = NULL;
9780 			if (error || so->so_error || be.error) {
9781 				if (error == 0) {
9782 					if (so->so_error)
9783 						error = so->so_error;
9784 					if (be.error) {
9785 						error = be.error;
9786 					}
9787 				}
9788 				SOCKBUF_UNLOCK(&so->so_snd);
9789 				goto out_unlocked;
9790 			}
9791 #ifdef SCTP_BLK_LOGGING
9792 			sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
9793 			    so, asoc, stcb->asoc.total_output_queue_size);
9794 #endif
9795 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
9796 				goto out_unlocked;
9797 			}
9798 		}
9799 		if (so->so_snd.sb_hiwat > stcb->asoc.total_output_queue_size) {
9800 			max_len = so->so_snd.sb_hiwat - stcb->asoc.total_output_queue_size;
9801 		} else {
9802 			max_len = 0;
9803 		}
9804 		SOCKBUF_UNLOCK(&so->so_snd);
9805 	}
9806 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
9807 		goto out_unlocked;
9808 	}
9809 	if (top == NULL) {
9810 		struct sctp_stream_queue_pending *sp;
9811 		struct sctp_stream_out *strm;
9812 		uint32_t sndout, initial_out;
9813 		int user_marks_eor;
9814 
9815 		if (uio->uio_resid == 0) {
9816 			if (srcv->sinfo_flags & SCTP_EOF) {
9817 				got_all_of_the_send = 1;
9818 				goto dataless_eof;
9819 			} else {
9820 				error = EINVAL;
9821 				goto out;
9822 			}
9823 		}
9824 		initial_out = uio->uio_resid;
9825 
9826 		if ((asoc->stream_locked) &&
9827 		    (asoc->stream_locked_on != srcv->sinfo_stream)) {
9828 			error = EAGAIN;
9829 			goto out;
9830 		}
9831 		strm = &stcb->asoc.strmout[srcv->sinfo_stream];
9832 		user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
9833 		if (strm->last_msg_incomplete == 0) {
9834 			sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
9835 			if ((sp == NULL) || (error)) {
9836 				goto out;
9837 			}
9838 			SCTP_TCB_SEND_LOCK(stcb);
9839 			if (sp->msg_is_complete) {
9840 				strm->last_msg_incomplete = 0;
9841 				asoc->stream_locked = 0;
9842 			} else {
9843 				/*
9844 				 * Just got locked to this guy in case of an
9845 				 * interupt.
9846 				 */
9847 				strm->last_msg_incomplete = 1;
9848 				asoc->stream_locked = 1;
9849 				asoc->stream_locked_on = srcv->sinfo_stream;
9850 			}
9851 			sctp_snd_sb_alloc(stcb, sp->length);
9852 
9853 			asoc->stream_queue_cnt++;
9854 			TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
9855 			if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
9856 				sp->strseq = strm->next_sequence_sent;
9857 #ifdef SCTP_LOG_SENDING_STR
9858 				sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
9859 				    (uintptr_t) stcb, (uintptr_t) sp,
9860 				    (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
9861 #endif
9862 				strm->next_sequence_sent++;
9863 			}
9864 			if ((strm->next_spoke.tqe_next == NULL) &&
9865 			    (strm->next_spoke.tqe_prev == NULL)) {
9866 				/* Not on wheel, insert */
9867 				sctp_insert_on_wheel(stcb, asoc, strm, 1);
9868 			}
9869 			SCTP_TCB_SEND_UNLOCK(stcb);
9870 		} else {
9871 			sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
9872 		}
9873 		while (uio->uio_resid > 0) {
9874 			/* How much room do we have? */
9875 			struct mbuf *new_tail, *mm;
9876 
9877 			if (so->so_snd.sb_hiwat > stcb->asoc.total_output_queue_size)
9878 				max_len = so->so_snd.sb_hiwat - stcb->asoc.total_output_queue_size;
9879 			else
9880 				max_len = 0;
9881 
9882 			if ((max_len > sctp_add_more_threshold) ||
9883 			    (uio->uio_resid && (uio->uio_resid < max_len))) {
9884 				sndout = 0;
9885 				new_tail = NULL;
9886 				if (hold_tcblock) {
9887 					SCTP_TCB_UNLOCK(stcb);
9888 					hold_tcblock = 0;
9889 				}
9890 				mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
9891 				if ((mm == NULL) || error) {
9892 					if (mm) {
9893 						sctp_m_freem(mm);
9894 					}
9895 					goto out;
9896 				}
9897 				/* Update the mbuf and count */
9898 				SCTP_TCB_SEND_LOCK(stcb);
9899 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
9900 					/*
9901 					 * we need to get out. Peer probably
9902 					 * aborted.
9903 					 */
9904 					sctp_m_freem(mm);
9905 					if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED)
9906 						error = ECONNRESET;
9907 					goto out;
9908 				}
9909 				if (sp->tail_mbuf) {
9910 					/* tack it to the end */
9911 					sp->tail_mbuf->m_next = mm;
9912 					sp->tail_mbuf = new_tail;
9913 				} else {
9914 					/* A stolen mbuf */
9915 					sp->data = mm;
9916 					sp->tail_mbuf = new_tail;
9917 				}
9918 				sctp_snd_sb_alloc(stcb, sndout);
9919 				sp->length += sndout;
9920 				len += sndout;
9921 				/* Did we reach EOR? */
9922 				if ((uio->uio_resid == 0) &&
9923 				    ((user_marks_eor == 0) ||
9924 				    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))
9925 				    ) {
9926 					sp->msg_is_complete = 1;
9927 				} else {
9928 					sp->msg_is_complete = 0;
9929 				}
9930 				if (sp->data->m_flags & M_PKTHDR) {
9931 					/* update length */
9932 					sp->data->m_pkthdr.len = sp->length;
9933 				}
9934 				SCTP_TCB_SEND_UNLOCK(stcb);
9935 			}
9936 			if (uio->uio_resid == 0) {
9937 				/* got it all? */
9938 				continue;
9939 			}
9940 			/* PR-SCTP? */
9941 			if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
9942 				/*
9943 				 * This is ugly but we must assure locking
9944 				 * order
9945 				 */
9946 				if (hold_tcblock == 0) {
9947 					SCTP_TCB_LOCK(stcb);
9948 					hold_tcblock = 1;
9949 				}
9950 				sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
9951 				if (so->so_snd.sb_hiwat > stcb->asoc.total_output_queue_size)
9952 					max_len = so->so_snd.sb_hiwat - stcb->asoc.total_output_queue_size;
9953 				else
9954 					max_len = 0;
9955 				if (max_len > 0) {
9956 					continue;
9957 				}
9958 				SCTP_TCB_UNLOCK(stcb);
9959 				hold_tcblock = 0;
9960 			}
9961 			/* wait for space now */
9962 			if (non_blocking) {
9963 				/* Non-blocking io in place out */
9964 				goto skip_out_eof;
9965 			}
9966 			if ((net->flight_size > net->cwnd) &&
9967 			    (sctp_cmt_on_off == 0)) {
9968 				queue_only = 1;
9969 
9970 			} else if (asoc->ifp_had_enobuf) {
9971 				SCTP_STAT_INCR(sctps_ifnomemqueued);
9972 				if (net->flight_size > (net->mtu * 2)) {
9973 					queue_only = 1;
9974 				} else {
9975 					queue_only = 0;
9976 				}
9977 				asoc->ifp_had_enobuf = 0;
9978 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9979 				    ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
9980 				    sizeof(struct sctp_data_chunk)));
9981 			} else {
9982 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9983 				    ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
9984 				    sizeof(struct sctp_data_chunk)));
9985 				queue_only = 0;
9986 			}
9987 			if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
9988 			    (stcb->asoc.total_flight > 0) &&
9989 			    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
9990 			    ) {
9991 
9992 				/*
9993 				 * Ok, Nagle is set on and we have data
9994 				 * outstanding. Don't send anything and let
9995 				 * SACKs drive out the data unless wen have
9996 				 * a "full" segment to send.
9997 				 */
9998 #ifdef SCTP_NAGLE_LOGGING
9999 				sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
10000 #endif
10001 				SCTP_STAT_INCR(sctps_naglequeued);
10002 				nagle_applies = 1;
10003 			} else {
10004 #ifdef SCTP_NAGLE_LOGGING
10005 				if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
10006 					sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
10007 #endif
10008 				SCTP_STAT_INCR(sctps_naglesent);
10009 				nagle_applies = 0;
10010 			}
10011 			/* What about the INIT, send it maybe */
10012 #ifdef SCTP_BLK_LOGGING
10013 			sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only, nagle_applies, un_sent);
10014 			sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size, stcb->asoc.total_flight,
10015 			    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
10016 #endif
10017 			if (queue_only_for_init) {
10018 				if (hold_tcblock == 0) {
10019 					SCTP_TCB_LOCK(stcb);
10020 					hold_tcblock = 1;
10021 				}
10022 				sctp_send_initiate(inp, stcb);
10023 				queue_only_for_init = 0;
10024 				queue_only = 1;
10025 				SCTP_TCB_UNLOCK(stcb);
10026 				hold_tcblock = 0;
10027 			}
10028 			if ((queue_only == 0) && (nagle_applies == 0)
10029 			    ) {
10030 				/*
10031 				 * need to start chunk output before
10032 				 * blocking.. note that if a lock is already
10033 				 * applied, then the input via the net is
10034 				 * happening and I don't need to start
10035 				 * output :-D
10036 				 */
10037 				if (hold_tcblock == 0) {
10038 					if (SCTP_TCB_TRYLOCK(stcb)) {
10039 						hold_tcblock = 1;
10040 						sctp_chunk_output(inp,
10041 						    stcb,
10042 						    SCTP_OUTPUT_FROM_USR_SEND);
10043 
10044 					}
10045 				} else {
10046 					sctp_chunk_output(inp,
10047 					    stcb,
10048 					    SCTP_OUTPUT_FROM_USR_SEND);
10049 				}
10050 				if (hold_tcblock == 1) {
10051 					SCTP_TCB_UNLOCK(stcb);
10052 					hold_tcblock = 0;
10053 				}
10054 			}
10055 			SOCKBUF_LOCK(&so->so_snd);
10056 			/*
10057 			 * This is a bit strange, but I think it will work.
10058 			 * The total_output_queue_size is locked and
10059 			 * protected by the TCB_LOCK, which we just
10060 			 * released. There is a race that can occur between
10061 			 * releasing it above, and me getting the socket
10062 			 * lock, where sacks come in but we have not put the
10063 			 * SB_WAIT on the so_snd buffer to get the wakeup.
10064 			 * After the LOCK is applied the sack_processing
10065 			 * will also need to LOCK the so->so_snd to do the
10066 			 * actual sowwakeup(). So once we have the socket
10067 			 * buffer lock if we recheck the size we KNOW we
10068 			 * will get to sleep safely with the wakeup flag in
10069 			 * place.
10070 			 */
10071 			if (so->so_snd.sb_hiwat < (stcb->asoc.total_output_queue_size + sctp_add_more_threshold)) {
10072 #ifdef SCTP_BLK_LOGGING
10073 				sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
10074 				    so, asoc, uio->uio_resid);
10075 #endif
10076 				be.error = 0;
10077 				stcb->block_entry = &be;
10078 				error = sbwait(&so->so_snd);
10079 				stcb->block_entry = NULL;
10080 
10081 				if (error || so->so_error || be.error) {
10082 					if (error == 0) {
10083 						if (so->so_error)
10084 							error = so->so_error;
10085 						if (be.error) {
10086 							error = be.error;
10087 						}
10088 					}
10089 					SOCKBUF_UNLOCK(&so->so_snd);
10090 					goto out_unlocked;
10091 				}
10092 #ifdef SCTP_BLK_LOGGING
10093 				sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
10094 				    so, asoc, stcb->asoc.total_output_queue_size);
10095 #endif
10096 			}
10097 			SOCKBUF_UNLOCK(&so->so_snd);
10098 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
10099 				goto out_unlocked;
10100 			}
10101 		}
10102 		SCTP_TCB_SEND_LOCK(stcb);
10103 		if (sp->msg_is_complete == 0) {
10104 			strm->last_msg_incomplete = 1;
10105 			asoc->stream_locked = 1;
10106 			asoc->stream_locked_on = srcv->sinfo_stream;
10107 		} else {
10108 			strm->last_msg_incomplete = 0;
10109 			asoc->stream_locked = 0;
10110 		}
10111 		SCTP_TCB_SEND_UNLOCK(stcb);
10112 		if (uio->uio_resid == 0) {
10113 			got_all_of_the_send = 1;
10114 		}
10115 	} else if (top) {
10116 		/* We send in a 0, since we do NOT have any locks */
10117 		error = sctp_msg_append(stcb, net, top, srcv, 0);
10118 		top = NULL;
10119 	}
10120 	if (error) {
10121 		goto out;
10122 	}
10123 dataless_eof:
10124 	/* EOF thing ? */
10125 	if ((srcv->sinfo_flags & SCTP_EOF) &&
10126 	    (got_all_of_the_send == 1) &&
10127 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
10128 	    ) {
10129 		error = 0;
10130 		if (hold_tcblock == 0) {
10131 			SCTP_TCB_LOCK(stcb);
10132 			hold_tcblock = 1;
10133 		}
10134 		if (TAILQ_EMPTY(&asoc->send_queue) &&
10135 		    TAILQ_EMPTY(&asoc->sent_queue) &&
10136 		    (asoc->stream_queue_cnt == 0)) {
10137 			if (asoc->locked_on_sending) {
10138 				goto abort_anyway;
10139 			}
10140 			/* there is nothing queued to send, so I'm done... */
10141 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
10142 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
10143 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
10144 				/* only send SHUTDOWN the first time through */
10145 				sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
10146 				asoc->state = SCTP_STATE_SHUTDOWN_SENT;
10147 				SCTP_STAT_DECR_GAUGE32(sctps_currestab);
10148 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
10149 				    asoc->primary_destination);
10150 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
10151 				    asoc->primary_destination);
10152 			}
10153 		} else {
10154 			/*
10155 			 * we still got (or just got) data to send, so set
10156 			 * SHUTDOWN_PENDING
10157 			 */
10158 			/*
10159 			 * XXX sockets draft says that SCTP_EOF should be
10160 			 * sent with no data.  currently, we will allow user
10161 			 * data to be sent first and move to
10162 			 * SHUTDOWN-PENDING
10163 			 */
10164 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
10165 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
10166 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
10167 				if (hold_tcblock == 0) {
10168 					SCTP_TCB_LOCK(stcb);
10169 					hold_tcblock = 1;
10170 				}
10171 				if (asoc->locked_on_sending) {
10172 					/* Locked to send out the data */
10173 					struct sctp_stream_queue_pending *sp;
10174 
10175 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
10176 					if (sp) {
10177 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
10178 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
10179 					}
10180 				}
10181 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
10182 				if (TAILQ_EMPTY(&asoc->send_queue) &&
10183 				    TAILQ_EMPTY(&asoc->sent_queue) &&
10184 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
10185 			abort_anyway:
10186 					if (free_cnt_applied) {
10187 						atomic_add_int(&stcb->asoc.refcnt, -1);
10188 						free_cnt_applied = 0;
10189 					}
10190 					sctp_abort_an_association(stcb->sctp_ep, stcb,
10191 					    SCTP_RESPONSE_TO_USER_REQ,
10192 					    NULL);
10193 					/*
10194 					 * now relock the stcb so everything
10195 					 * is sane
10196 					 */
10197 					hold_tcblock = 0;
10198 					stcb = NULL;
10199 					goto out;
10200 				}
10201 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
10202 				    asoc->primary_destination);
10203 			}
10204 		}
10205 	}
10206 skip_out_eof:
10207 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
10208 		some_on_control = 1;
10209 	}
10210 	if ((net->flight_size > net->cwnd) &&
10211 	    (sctp_cmt_on_off == 0)) {
10212 		queue_only = 1;
10213 	} else if (asoc->ifp_had_enobuf) {
10214 		SCTP_STAT_INCR(sctps_ifnomemqueued);
10215 		if (net->flight_size > (net->mtu * 2)) {
10216 			queue_only = 1;
10217 		} else {
10218 			queue_only = 0;
10219 		}
10220 		asoc->ifp_had_enobuf = 0;
10221 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
10222 		    ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
10223 		    sizeof(struct sctp_data_chunk)));
10224 	} else {
10225 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
10226 		    ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
10227 		    sizeof(struct sctp_data_chunk)));
10228 		queue_only = 0;
10229 	}
10230 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
10231 	    (stcb->asoc.total_flight > 0) &&
10232 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
10233 	    ) {
10234 
10235 		/*
10236 		 * Ok, Nagle is set on and we have data outstanding. Don't
10237 		 * send anything and let SACKs drive out the data unless wen
10238 		 * have a "full" segment to send.
10239 		 */
10240 #ifdef SCTP_NAGLE_LOGGING
10241 		sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
10242 #endif
10243 		SCTP_STAT_INCR(sctps_naglequeued);
10244 		nagle_applies = 1;
10245 	} else {
10246 #ifdef SCTP_NAGLE_LOGGING
10247 		if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
10248 			sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
10249 #endif
10250 		SCTP_STAT_INCR(sctps_naglesent);
10251 		nagle_applies = 0;
10252 	}
10253 	if (queue_only_for_init) {
10254 		if (hold_tcblock == 0) {
10255 			SCTP_TCB_LOCK(stcb);
10256 			hold_tcblock = 1;
10257 		}
10258 		sctp_send_initiate(inp, stcb);
10259 		queue_only_for_init = 0;
10260 		queue_only = 1;
10261 	}
10262 	if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
10263 		/* we can attempt to send too. */
10264 		s = splnet();
10265 		if (hold_tcblock == 0) {
10266 			/*
10267 			 * If there is activity recv'ing sacks no need to
10268 			 * send
10269 			 */
10270 			if (SCTP_TCB_TRYLOCK(stcb)) {
10271 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND);
10272 				hold_tcblock = 1;
10273 			}
10274 		} else {
10275 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND);
10276 		}
10277 		splx(s);
10278 	} else if ((queue_only == 0) &&
10279 		    (stcb->asoc.peers_rwnd == 0) &&
10280 	    (stcb->asoc.total_flight == 0)) {
10281 		/* We get to have a probe outstanding */
10282 		if (hold_tcblock == 0) {
10283 			hold_tcblock = 1;
10284 			SCTP_TCB_LOCK(stcb);
10285 		}
10286 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND);
10287 	} else if (some_on_control) {
10288 		int num_out, reason, cwnd_full, frag_point;
10289 
10290 		/* Here we do control only */
10291 		if (hold_tcblock == 0) {
10292 			hold_tcblock = 1;
10293 			SCTP_TCB_LOCK(stcb);
10294 		}
10295 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
10296 		sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
10297 		    &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point);
10298 	}
10299 #ifdef SCTP_DEBUG
10300 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
10301 		printf("USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d \n",
10302 		    queue_only, stcb->asoc.peers_rwnd, un_sent,
10303 		    stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
10304 		    stcb->asoc.total_output_queue_size);
10305 	}
10306 #endif
10307 out:
10308 out_unlocked:
10309 
10310 	if (create_lock_applied) {
10311 		SCTP_ASOC_CREATE_UNLOCK(inp);
10312 		create_lock_applied = 0;
10313 	}
10314 	if ((stcb) && hold_tcblock) {
10315 		SCTP_TCB_UNLOCK(stcb);
10316 	}
10317 	if (stcb && free_cnt_applied) {
10318 		atomic_add_int(&stcb->asoc.refcnt, -1);
10319 	}
10320 #ifdef INVARIENTS
10321 	if (stcb) {
10322 		if (mtx_owned(&stcb->tcb_mtx)) {
10323 			panic("Leaving with tcb mtx owned?");
10324 		}
10325 		if (mtx_owned(&stcb->tcb_send_mtx)) {
10326 			panic("Leaving with tcb send mtx owned?");
10327 		}
10328 	}
10329 #endif
10330 	if (top) {
10331 		sctp_m_freem(top);
10332 	}
10333 	if (control) {
10334 		sctp_m_freem(control);
10335 	}
10336 	return (error);
10337 }
10338 
10339 
10340 /*
10341  * generate an AUTHentication chunk, if required
10342  */
10343 struct mbuf *
10344 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
10345     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
10346     struct sctp_tcb *stcb, uint8_t chunk)
10347 {
10348 	struct mbuf *m_auth;
10349 	struct sctp_auth_chunk *auth;
10350 	int chunk_len;
10351 
10352 	if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
10353 	    (stcb == NULL))
10354 		return (m);
10355 
10356 	/* sysctl disabled auth? */
10357 	if (sctp_auth_disable)
10358 		return (m);
10359 
10360 	/* peer doesn't do auth... */
10361 	if (!stcb->asoc.peer_supports_auth) {
10362 		return (m);
10363 	}
10364 	/* does the requested chunk require auth? */
10365 	if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
10366 		return (m);
10367 	}
10368 	m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 1, M_DONTWAIT, 1, MT_HEADER);
10369 	if (m_auth == NULL) {
10370 		/* no mbuf's */
10371 		return (m);
10372 	}
10373 	/* reserve some space if this will be the first mbuf */
10374 	if (m == NULL)
10375 		m_auth->m_data += SCTP_MIN_OVERHEAD;
10376 	/* fill in the AUTH chunk details */
10377 	auth = mtod(m_auth, struct sctp_auth_chunk *);
10378 	bzero(auth, sizeof(*auth));
10379 	auth->ch.chunk_type = SCTP_AUTHENTICATION;
10380 	auth->ch.chunk_flags = 0;
10381 	chunk_len = sizeof(*auth) +
10382 	    sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
10383 	auth->ch.chunk_length = htons(chunk_len);
10384 	auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
10385 	/* key id and hmac digest will be computed and filled in upon send */
10386 
10387 	/* save the offset where the auth was inserted into the chain */
10388 	if (m != NULL)
10389 		*offset = m->m_pkthdr.len;
10390 	else
10391 		*offset = 0;
10392 
10393 	/* update length and return pointer to the auth chunk */
10394 	m_auth->m_pkthdr.len = m_auth->m_len = chunk_len;
10395 	m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
10396 	if (auth_ret != NULL)
10397 		*auth_ret = auth;
10398 
10399 	return (m);
10400 }
10401