xref: /freebsd/sys/netinet/sctp_output.c (revision 0efd6615cd5f39b67cec82a7034e655f3b5801e3)
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_INUSE) ||
3621 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
3622 		/* re-use the v-tags and init-seq here */
3623 		initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
3624 		initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
3625 	} else {
3626 		if (asoc) {
3627 			atomic_add_int(&asoc->refcnt, 1);
3628 			SCTP_TCB_UNLOCK(stcb);
3629 			initackm_out->msg.init.initiate_tag = htonl(sctp_select_a_tag(inp));
3630 			/* get a TSN to use too */
3631 			initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
3632 			SCTP_TCB_LOCK(stcb);
3633 			atomic_add_int(&asoc->refcnt, -1);
3634 		} else {
3635 			initackm_out->msg.init.initiate_tag = htonl(sctp_select_a_tag(inp));
3636 			/* get a TSN to use too */
3637 			initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
3638 		}
3639 	}
3640 	/* save away my tag to */
3641 	stc.my_vtag = initackm_out->msg.init.initiate_tag;
3642 
3643 	/* set up some of the credits. */
3644 	initackm_out->msg.init.a_rwnd = htonl(max(inp->sctp_socket->so_rcv.sb_hiwat, SCTP_MINIMAL_RWND));
3645 	/* set what I want */
3646 	his_limit = ntohs(init_chk->init.num_inbound_streams);
3647 	/* choose what I want */
3648 	if (asoc != NULL) {
3649 		if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
3650 			i_want = asoc->streamoutcnt;
3651 		} else {
3652 			i_want = inp->sctp_ep.pre_open_stream_count;
3653 		}
3654 	} else {
3655 		i_want = inp->sctp_ep.pre_open_stream_count;
3656 	}
3657 	if (his_limit < i_want) {
3658 		/* I Want more :< */
3659 		initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
3660 	} else {
3661 		/* I can have what I want :> */
3662 		initackm_out->msg.init.num_outbound_streams = htons(i_want);
3663 	}
3664 	/* tell him his limt. */
3665 	initackm_out->msg.init.num_inbound_streams =
3666 	    htons(inp->sctp_ep.max_open_streams_intome);
3667 	/* setup the ECN pointer */
3668 
3669 	if (inp->sctp_ep.adaptation_layer_indicator) {
3670 		struct sctp_adaptation_layer_indication *ali;
3671 
3672 		ali = (struct sctp_adaptation_layer_indication *)(
3673 		    (caddr_t)initackm_out + sizeof(*initackm_out));
3674 		ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
3675 		ali->ph.param_length = htons(sizeof(*ali));
3676 		ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
3677 		m->m_len += sizeof(*ali);
3678 		ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
3679 		    sizeof(*ali));
3680 	} else {
3681 		ecn = (struct sctp_ecn_supported_param *)(
3682 		    (caddr_t)initackm_out + sizeof(*initackm_out));
3683 	}
3684 
3685 	/* ECN parameter */
3686 	if (sctp_ecn_enable == 1) {
3687 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
3688 		ecn->ph.param_length = htons(sizeof(*ecn));
3689 		m->m_len += sizeof(*ecn);
3690 
3691 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
3692 		    sizeof(*ecn));
3693 	} else {
3694 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
3695 	}
3696 	/* And now tell the peer we do  pr-sctp */
3697 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
3698 	prsctp->ph.param_length = htons(sizeof(*prsctp));
3699 	m->m_len += sizeof(*prsctp);
3700 
3701 	/* And now tell the peer we do all the extensions */
3702 	pr_supported = (struct sctp_supported_chunk_types_param *)
3703 	    ((caddr_t)prsctp + sizeof(*prsctp));
3704 
3705 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
3706 	num_ext = 0;
3707 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
3708 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
3709 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
3710 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
3711 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
3712 	if (!sctp_auth_disable)
3713 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
3714 	p_len = sizeof(*pr_supported) + num_ext;
3715 	pr_supported->ph.param_length = htons(p_len);
3716 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
3717 	m->m_len += SCTP_SIZE32(p_len);
3718 
3719 	/* ECN nonce: And now tell the peer we support ECN nonce */
3720 	if (sctp_ecn_nonce) {
3721 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
3722 		    ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
3723 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
3724 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
3725 		m->m_len += sizeof(*ecn_nonce);
3726 	}
3727 	/* add authentication parameters */
3728 	if (!sctp_auth_disable) {
3729 		struct sctp_auth_random *random;
3730 		struct sctp_auth_hmac_algo *hmacs;
3731 		struct sctp_auth_chunk_list *chunks;
3732 		uint16_t random_len;
3733 
3734 		/* generate and add RANDOM parameter */
3735 		random_len = sctp_auth_random_len;
3736 		random = (struct sctp_auth_random *)(mtod(m, caddr_t)+m->m_len);
3737 		random->ph.param_type = htons(SCTP_RANDOM);
3738 		p_len = sizeof(*random) + random_len;
3739 		random->ph.param_length = htons(p_len);
3740 		sctp_read_random(random->random_data, random_len);
3741 		/* zero out any padding required */
3742 		bzero((caddr_t)random + p_len, SCTP_SIZE32(p_len) - p_len);
3743 		m->m_len += SCTP_SIZE32(p_len);
3744 
3745 		/* add HMAC_ALGO parameter */
3746 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+m->m_len);
3747 		p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
3748 		    (uint8_t *) hmacs->hmac_ids);
3749 		if (p_len > 0) {
3750 			p_len += sizeof(*hmacs);
3751 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
3752 			hmacs->ph.param_length = htons(p_len);
3753 			/* zero out any padding required */
3754 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
3755 			m->m_len += SCTP_SIZE32(p_len);
3756 		}
3757 		/* add CHUNKS parameter */
3758 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+m->m_len);
3759 		p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
3760 		    chunks->chunk_types);
3761 		if (p_len > 0) {
3762 			p_len += sizeof(*chunks);
3763 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
3764 			chunks->ph.param_length = htons(p_len);
3765 			/* zero out any padding required */
3766 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
3767 			m->m_len += SCTP_SIZE32(p_len);
3768 		}
3769 	}
3770 	m_at = m;
3771 	/* now the addresses */
3772 	{
3773 		struct sctp_scoping scp;
3774 
3775 		/*
3776 		 * To optimize this we could put the scoping stuff into a
3777 		 * structure and remove the individual uint8's from the stc
3778 		 * structure. Then we could just pass in the address within
3779 		 * the stc.. but for now this is a quick hack to get the
3780 		 * address stuff teased apart.
3781 		 */
3782 		scp.ipv4_addr_legal = stc.ipv4_addr_legal;
3783 		scp.ipv6_addr_legal = stc.ipv6_addr_legal;
3784 		scp.loopback_scope = stc.loopback_scope;
3785 		scp.ipv4_local_scope = stc.ipv4_scope;
3786 		scp.local_scope = stc.local_scope;
3787 		scp.site_scope = stc.site_scope;
3788 		m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
3789 	}
3790 
3791 	/* tack on the operational error if present */
3792 	if (op_err) {
3793 		if (op_err->m_pkthdr.len % 4) {
3794 			/* must add a pad to the param */
3795 			uint32_t cpthis = 0;
3796 			int padlen;
3797 
3798 			padlen = 4 - (op_err->m_pkthdr.len % 4);
3799 			m_copyback(op_err, op_err->m_pkthdr.len, padlen, (caddr_t)&cpthis);
3800 		}
3801 		while (m_at->m_next != NULL) {
3802 			m_at = m_at->m_next;
3803 		}
3804 		m_at->m_next = op_err;
3805 		while (m_at->m_next != NULL) {
3806 			m_at = m_at->m_next;
3807 		}
3808 	}
3809 	/* Get total size of init packet */
3810 	sz_of = SCTP_SIZE32(ntohs(init_chk->ch.chunk_length));
3811 	/* pre-calulate the size and update pkt header and chunk header */
3812 	m->m_pkthdr.len = 0;
3813 	for (m_tmp = m; m_tmp; m_tmp = m_tmp->m_next) {
3814 		m->m_pkthdr.len += m_tmp->m_len;
3815 		if (m_tmp->m_next == NULL) {
3816 			/* m_tmp should now point to last one */
3817 			break;
3818 		}
3819 	}
3820 	/*
3821 	 * Figure now the size of the cookie. We know the size of the
3822 	 * INIT-ACK. The Cookie is going to be the size of INIT, INIT-ACK,
3823 	 * COOKIE-STRUCTURE and SIGNATURE.
3824 	 */
3825 
3826 	/*
3827 	 * take our earlier INIT calc and add in the sz we just calculated
3828 	 * minus the size of the sctphdr (its not included in chunk size
3829 	 */
3830 
3831 	/* add once for the INIT-ACK */
3832 	sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
3833 
3834 	/* add a second time for the INIT-ACK in the cookie */
3835 	sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
3836 
3837 	/* Now add the cookie header and cookie message struct */
3838 	sz_of += sizeof(struct sctp_state_cookie_param);
3839 	/* ...and add the size of our signature */
3840 	sz_of += SCTP_SIGNATURE_SIZE;
3841 	initackm_out->msg.ch.chunk_length = htons(sz_of);
3842 
3843 	/* Now we must build a cookie */
3844 	m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
3845 	    sizeof(struct sctphdr), &stc);
3846 	if (m_cookie == NULL) {
3847 		/* memory problem */
3848 		sctp_m_freem(m);
3849 		return;
3850 	}
3851 	/* Now append the cookie to the end and update the space/size */
3852 	m_tmp->m_next = m_cookie;
3853 	for (; m_tmp; m_tmp = m_tmp->m_next) {
3854 		m->m_pkthdr.len += m_tmp->m_len;
3855 		if (m_tmp->m_next == NULL) {
3856 			/* m_tmp should now point to last one */
3857 			m_last = m_tmp;
3858 			break;
3859 		}
3860 	}
3861 
3862 	/*
3863 	 * We pass 0 here to NOT set IP_DF if its IPv4, we ignore the return
3864 	 * here since the timer will drive a retranmission.
3865 	 */
3866 	padval = m->m_pkthdr.len % 4;
3867 	if ((padval) && (m_last)) {
3868 		/* see my previous comments on m_last */
3869 		int ret;
3870 
3871 		ret = sctp_add_pad_tombuf(m_last, (4 - padval));
3872 		if (ret) {
3873 			/* Houston we have a problem, no space */
3874 			sctp_m_freem(m);
3875 			return;
3876 		}
3877 		m->m_pkthdr.len += padval;
3878 	}
3879 	sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
3880 	    NULL, 0);
3881 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
3882 }
3883 
3884 
3885 void
3886 sctp_insert_on_wheel(struct sctp_tcb *stcb,
3887     struct sctp_association *asoc,
3888     struct sctp_stream_out *strq, int holds_lock)
3889 {
3890 	struct sctp_stream_out *stre, *strn;
3891 
3892 	if (holds_lock == 0)
3893 		SCTP_TCB_SEND_LOCK(stcb);
3894 	if ((strq->next_spoke.tqe_next) ||
3895 	    (strq->next_spoke.tqe_prev)) {
3896 		/* already on wheel */
3897 		goto outof_here;
3898 	}
3899 	stre = TAILQ_FIRST(&asoc->out_wheel);
3900 	if (stre == NULL) {
3901 		/* only one on wheel */
3902 		TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
3903 		goto outof_here;
3904 	}
3905 	for (; stre; stre = strn) {
3906 		strn = TAILQ_NEXT(stre, next_spoke);
3907 		if (stre->stream_no > strq->stream_no) {
3908 			TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
3909 			goto outof_here;
3910 		} else if (stre->stream_no == strq->stream_no) {
3911 			/* huh, should not happen */
3912 			goto outof_here;
3913 		} else if (strn == NULL) {
3914 			/* next one is null */
3915 			TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
3916 			    next_spoke);
3917 		}
3918 	}
3919 outof_here:
3920 	if (holds_lock == 0)
3921 		SCTP_TCB_SEND_UNLOCK(stcb);
3922 
3923 
3924 }
3925 
3926 static void
3927 sctp_remove_from_wheel(struct sctp_tcb *stcb,
3928     struct sctp_association *asoc,
3929     struct sctp_stream_out *strq)
3930 {
3931 	/* take off and then setup so we know it is not on the wheel */
3932 	SCTP_TCB_SEND_LOCK(stcb);
3933 	if (TAILQ_FIRST(&strq->outqueue)) {
3934 		/* more was added */
3935 		SCTP_TCB_SEND_UNLOCK(stcb);
3936 		return;
3937 	}
3938 	TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
3939 	strq->next_spoke.tqe_next = NULL;
3940 	strq->next_spoke.tqe_prev = NULL;
3941 	SCTP_TCB_SEND_UNLOCK(stcb);
3942 }
3943 
3944 static void
3945 sctp_prune_prsctp(struct sctp_tcb *stcb,
3946     struct sctp_association *asoc,
3947     struct sctp_sndrcvinfo *srcv,
3948     int dataout)
3949 {
3950 	int freed_spc = 0;
3951 	struct sctp_tmit_chunk *chk, *nchk;
3952 
3953 	SCTP_TCB_LOCK_ASSERT(stcb);
3954 	if ((asoc->peer_supports_prsctp) &&
3955 	    (asoc->sent_queue_cnt_removeable > 0)) {
3956 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3957 			/*
3958 			 * Look for chunks marked with the PR_SCTP flag AND
3959 			 * the buffer space flag. If the one being sent is
3960 			 * equal or greater priority then purge the old one
3961 			 * and free some space.
3962 			 */
3963 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
3964 				/*
3965 				 * This one is PR-SCTP AND buffer space
3966 				 * limited type
3967 				 */
3968 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
3969 					/*
3970 					 * Lower numbers equates to higher
3971 					 * priority so if the one we are
3972 					 * looking at has a larger or equal
3973 					 * priority we want to drop the data
3974 					 * and NOT retransmit it.
3975 					 */
3976 					if (chk->data) {
3977 						/*
3978 						 * We release the book_size
3979 						 * if the mbuf is here
3980 						 */
3981 						int ret_spc;
3982 						int cause;
3983 
3984 						if (chk->sent > SCTP_DATAGRAM_UNSENT)
3985 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
3986 						else
3987 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
3988 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
3989 						    cause,
3990 						    &asoc->sent_queue);
3991 						freed_spc += ret_spc;
3992 						if (freed_spc >= dataout) {
3993 							return;
3994 						}
3995 					}	/* if chunk was present */
3996 				}	/* if of sufficent priority */
3997 			}	/* if chunk has enabled */
3998 		}		/* tailqforeach */
3999 
4000 		chk = TAILQ_FIRST(&asoc->send_queue);
4001 		while (chk) {
4002 			nchk = TAILQ_NEXT(chk, sctp_next);
4003 			/* Here we must move to the sent queue and mark */
4004 			if (PR_SCTP_TTL_ENABLED(chk->flags)) {
4005 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
4006 					if (chk->data) {
4007 						/*
4008 						 * We release the book_size
4009 						 * if the mbuf is here
4010 						 */
4011 						int ret_spc;
4012 
4013 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
4014 						    SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
4015 						    &asoc->send_queue);
4016 
4017 						freed_spc += ret_spc;
4018 						if (freed_spc >= dataout) {
4019 							return;
4020 						}
4021 					}	/* end if chk->data */
4022 				}	/* end if right class */
4023 			}	/* end if chk pr-sctp */
4024 			chk = nchk;
4025 		}		/* end while (chk) */
4026 	}			/* if enabled in asoc */
4027 }
4028 
4029 __inline int
4030 sctp_get_frag_point(struct sctp_tcb *stcb,
4031     struct sctp_association *asoc)
4032 {
4033 	int siz, ovh;
4034 
4035 	/*
4036 	 * For endpoints that have both v6 and v4 addresses we must reserve
4037 	 * room for the ipv6 header, for those that are only dealing with V4
4038 	 * we use a larger frag point.
4039 	 */
4040 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4041 		ovh = SCTP_MED_OVERHEAD;
4042 	} else {
4043 		ovh = SCTP_MED_V4_OVERHEAD;
4044 	}
4045 
4046 	if (stcb->sctp_ep->sctp_frag_point > asoc->smallest_mtu)
4047 		siz = asoc->smallest_mtu - ovh;
4048 	else
4049 		siz = (stcb->sctp_ep->sctp_frag_point - ovh);
4050 	/*
4051 	 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
4052 	 */
4053 	/* A data chunk MUST fit in a cluster */
4054 	/* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
4055 	/* } */
4056 
4057 	/* adjust for an AUTH chunk if DATA requires auth */
4058 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
4059 		siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
4060 
4061 	if (siz % 4) {
4062 		/* make it an even word boundary please */
4063 		siz -= (siz % 4);
4064 	}
4065 	return (siz);
4066 }
4067 extern unsigned int sctp_max_chunks_on_queue;
4068 
4069 static void
4070 sctp_set_prsctp_policy(struct sctp_tcb *stcb,
4071     struct sctp_stream_queue_pending *sp)
4072 {
4073 	sp->pr_sctp_on = 0;
4074 	if (stcb->asoc.peer_supports_prsctp) {
4075 		/*
4076 		 * We assume that the user wants PR_SCTP_TTL if the user
4077 		 * provides a positive lifetime but does not specify any
4078 		 * PR_SCTP policy. This is a BAD assumption and causes
4079 		 * problems at least with the U-Vancovers MPI folks. I will
4080 		 * change this to be no policy means NO PR-SCTP.
4081 		 */
4082 		if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
4083 			sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
4084 			sp->pr_sctp_on = 1;
4085 		} else {
4086 			return;
4087 		}
4088 		switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
4089 		case CHUNK_FLAGS_PR_SCTP_BUF:
4090 			/*
4091 			 * Time to live is a priority stored in tv_sec when
4092 			 * doing the buffer drop thing.
4093 			 */
4094 			sp->ts.tv_sec = sp->timetolive;
4095 			sp->ts.tv_usec = 0;
4096 			break;
4097 		case CHUNK_FLAGS_PR_SCTP_TTL:
4098 			{
4099 				struct timeval tv;
4100 
4101 				SCTP_GETTIME_TIMEVAL(&sp->ts);
4102 				tv.tv_sec = sp->timetolive / 1000;
4103 				tv.tv_usec = (sp->timetolive * 1000) % 1000000;
4104 				timevaladd(&sp->ts, &tv);
4105 			}
4106 			break;
4107 		case CHUNK_FLAGS_PR_SCTP_RTX:
4108 			/*
4109 			 * Time to live is a the number or retransmissions
4110 			 * stored in tv_sec.
4111 			 */
4112 			sp->ts.tv_sec = sp->timetolive;
4113 			sp->ts.tv_usec = 0;
4114 			break;
4115 		default:
4116 #ifdef SCTP_DEBUG
4117 			if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
4118 				printf("Unknown PR_SCTP policy %u.\n", PR_SCTP_POLICY(sp->sinfo_flags));
4119 			}
4120 #endif
4121 			break;
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->sinfo_flags = srcv->sinfo_flags;
4169 	sp->timetolive = srcv->sinfo_timetolive;
4170 	sp->ppid = srcv->sinfo_ppid;
4171 	sp->context = srcv->sinfo_context;
4172 	sp->strseq = 0;
4173 	if (sp->sinfo_flags & SCTP_ADDR_OVER) {
4174 		sp->net = net;
4175 		sp->addr_over = 1;
4176 	} else {
4177 		sp->net = stcb->asoc.primary_destination;
4178 		sp->addr_over = 0;
4179 	}
4180 	atomic_add_int(&sp->net->ref_count, 1);
4181 	SCTP_GETTIME_TIMEVAL(&sp->ts);
4182 	sp->stream = srcv->sinfo_stream;
4183 	sp->msg_is_complete = 1;
4184 	sp->some_taken = 0;
4185 	sp->data = m;
4186 	sp->tail_mbuf = NULL;
4187 	sp->length = 0;
4188 	at = m;
4189 	sctp_set_prsctp_policy(stcb, sp);
4190 	while (at) {
4191 		if (at->m_next == NULL)
4192 			sp->tail_mbuf = at;
4193 		sp->length += at->m_len;
4194 		at = at->m_next;
4195 	}
4196 	if (sp->data->m_flags & M_PKTHDR) {
4197 		sp->data->m_pkthdr.len = sp->length;
4198 	} else {
4199 		/* Get an HDR in front please */
4200 		at = sctp_get_mbuf_for_msg(1, 1, M_DONTWAIT, 1, MT_DATA);
4201 		if (at) {
4202 			at->m_pkthdr.len = sp->length;
4203 			at->m_len = 0;
4204 			at->m_next = sp->data;
4205 			sp->data = at;
4206 		}
4207 	}
4208 	SCTP_TCB_SEND_LOCK(stcb);
4209 	sctp_snd_sb_alloc(stcb, sp->length);
4210 	stcb->asoc.stream_queue_cnt++;
4211 	TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
4212 	if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
4213 		sp->strseq = strm->next_sequence_sent;
4214 		strm->next_sequence_sent++;
4215 	}
4216 	if ((strm->next_spoke.tqe_next == NULL) &&
4217 	    (strm->next_spoke.tqe_prev == NULL)) {
4218 		/* Not on wheel, insert */
4219 		sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
4220 	}
4221 	m = NULL;
4222 	SCTP_TCB_SEND_UNLOCK(stcb);
4223 out_now:
4224 	if (m) {
4225 		sctp_m_freem(m);
4226 	}
4227 	return (error);
4228 }
4229 
4230 
4231 static struct mbuf *
4232 sctp_copy_mbufchain(struct mbuf *clonechain,
4233     struct mbuf *outchain,
4234     struct mbuf **endofchain,
4235     int can_take_mbuf,
4236     int sizeofcpy,
4237     uint8_t copy_by_ref)
4238 {
4239 	struct mbuf *m;
4240 	struct mbuf *appendchain;
4241 	caddr_t cp;
4242 	int len;
4243 
4244 	if (endofchain == NULL) {
4245 		/* error */
4246 error_out:
4247 		if (outchain)
4248 			sctp_m_freem(outchain);
4249 		return (NULL);
4250 	}
4251 	if (can_take_mbuf) {
4252 		appendchain = clonechain;
4253 	} else {
4254 		if (!copy_by_ref && (sizeofcpy <= ((((sctp_mbuf_threshold_count - 1) * MLEN) + MHLEN)))) {
4255 			/* Its not in a cluster */
4256 			if (*endofchain == NULL) {
4257 				/* lets get a mbuf cluster */
4258 				if (outchain == NULL) {
4259 					/* This is the general case */
4260 			new_mbuf:
4261 					outchain = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_HEADER);
4262 					if (outchain == NULL) {
4263 						goto error_out;
4264 					}
4265 					outchain->m_len = 0;
4266 					*endofchain = outchain;
4267 					/* get the prepend space */
4268 					outchain->m_data += (SCTP_FIRST_MBUF_RESV + 4);
4269 				} else {
4270 					/*
4271 					 * We really should not get a NULL
4272 					 * in endofchain
4273 					 */
4274 					/* find end */
4275 					m = outchain;
4276 					while (m) {
4277 						if (m->m_next == NULL) {
4278 							*endofchain = m;
4279 							break;
4280 						}
4281 						m = m->m_next;
4282 					}
4283 					/* sanity */
4284 					if (*endofchain == NULL) {
4285 						/*
4286 						 * huh, TSNH XXX maybe we
4287 						 * should panic
4288 						 */
4289 						sctp_m_freem(outchain);
4290 						goto new_mbuf;
4291 					}
4292 				}
4293 				/* get the new end of length */
4294 				len = M_TRAILINGSPACE(*endofchain);
4295 			} else {
4296 				/* how much is left at the end? */
4297 				len = M_TRAILINGSPACE(*endofchain);
4298 			}
4299 			/* Find the end of the data, for appending */
4300 			cp = (mtod((*endofchain), caddr_t)+(*endofchain)->m_len);
4301 
4302 			/* Now lets copy it out */
4303 			if (len >= sizeofcpy) {
4304 				/* It all fits, copy it in */
4305 				m_copydata(clonechain, 0, sizeofcpy, cp);
4306 				(*endofchain)->m_len += sizeofcpy;
4307 				if (outchain->m_flags & M_PKTHDR)
4308 					outchain->m_pkthdr.len += sizeofcpy;
4309 			} else {
4310 				/* fill up the end of the chain */
4311 				if (len > 0) {
4312 					m_copydata(clonechain, 0, len, cp);
4313 					(*endofchain)->m_len += len;
4314 					if (outchain->m_flags & M_PKTHDR)
4315 						outchain->m_pkthdr.len += len;
4316 					/* now we need another one */
4317 					sizeofcpy -= len;
4318 				}
4319 				m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_HEADER);
4320 				if (m == NULL) {
4321 					/* We failed */
4322 					goto error_out;
4323 				}
4324 				(*endofchain)->m_next = m;
4325 				*endofchain = m;
4326 				cp = mtod((*endofchain), caddr_t);
4327 				m_copydata(clonechain, len, sizeofcpy, cp);
4328 				(*endofchain)->m_len += sizeofcpy;
4329 				if (outchain->m_flags & M_PKTHDR) {
4330 					outchain->m_pkthdr.len += sizeofcpy;
4331 				}
4332 			}
4333 			return (outchain);
4334 		} else {
4335 			/* copy the old fashion way */
4336 			/*
4337 			 * Supposedly m_copypacket is an optimization, use
4338 			 * it if we can
4339 			 */
4340 			if (clonechain->m_flags & M_PKTHDR) {
4341 				appendchain = m_copypacket(clonechain, M_DONTWAIT);
4342 			} else {
4343 				appendchain = m_copy(clonechain, 0, M_COPYALL);
4344 			}
4345 
4346 		}
4347 	}
4348 	if (appendchain == NULL) {
4349 		/* error */
4350 		if (outchain)
4351 			sctp_m_freem(outchain);
4352 		return (NULL);
4353 	}
4354 	/* if outchain is null, check our special reservation flag */
4355 	if (outchain == NULL) {
4356 		/*
4357 		 * need a lead mbuf in this one if we don't have space for:
4358 		 * - E-net header (12+2+2) - IP header (20/40) - SCTP Common
4359 		 * Header (12)
4360 		 */
4361 		if (M_LEADINGSPACE(appendchain) < (SCTP_FIRST_MBUF_RESV)) {
4362 			outchain = sctp_get_mbuf_for_msg(8, 1, M_DONTWAIT, 1, MT_HEADER);
4363 			if (outchain) {
4364 				/*
4365 				 * if we don't hit here we have a problem
4366 				 * anyway :o We reserve all the mbuf for
4367 				 * prepends.
4368 				 */
4369 				outchain->m_pkthdr.len = 0;
4370 				outchain->m_len = 0;
4371 				outchain->m_next = NULL;
4372 				MH_ALIGN(outchain, 4);
4373 				*endofchain = outchain;
4374 			}
4375 		}
4376 	}
4377 	if (outchain) {
4378 		/* tack on to the end */
4379 		if (*endofchain != NULL) {
4380 			(*endofchain)->m_next = appendchain;
4381 		} else {
4382 			m = outchain;
4383 			while (m) {
4384 				if (m->m_next == NULL) {
4385 					m->m_next = appendchain;
4386 					break;
4387 				}
4388 				m = m->m_next;
4389 			}
4390 		}
4391 		if (outchain->m_flags & M_PKTHDR) {
4392 			int append_tot;
4393 
4394 			m = appendchain;
4395 			append_tot = 0;
4396 			while (m) {
4397 				append_tot += m->m_len;
4398 				if (m->m_next == NULL) {
4399 					*endofchain = m;
4400 				}
4401 				m = m->m_next;
4402 			}
4403 			outchain->m_pkthdr.len += append_tot;
4404 		} else {
4405 			/*
4406 			 * save off the end and update the end-chain postion
4407 			 */
4408 			m = appendchain;
4409 			while (m) {
4410 				if (m->m_next == NULL) {
4411 					*endofchain = m;
4412 					break;
4413 				}
4414 				m = m->m_next;
4415 			}
4416 		}
4417 		return (outchain);
4418 	} else {
4419 		/* save off the end and update the end-chain postion */
4420 		m = appendchain;
4421 		while (m) {
4422 			if (m->m_next == NULL) {
4423 				*endofchain = m;
4424 				break;
4425 			}
4426 			m = m->m_next;
4427 		}
4428 		return (appendchain);
4429 	}
4430 }
4431 
4432 int
4433 sctp_med_chunk_output(struct sctp_inpcb *inp,
4434     struct sctp_tcb *stcb,
4435     struct sctp_association *asoc,
4436     int *num_out,
4437     int *reason_code,
4438     int control_only, int *cwnd_full, int from_where,
4439     struct timeval *now, int *now_filled, int frag_point);
4440 
4441 static void
4442 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
4443     uint32_t val)
4444 {
4445 	struct sctp_copy_all *ca;
4446 	struct mbuf *m;
4447 	int ret = 0;
4448 	int added_control = 0;
4449 	int un_sent, do_chunk_output = 1;
4450 	struct sctp_association *asoc;
4451 
4452 	ca = (struct sctp_copy_all *)ptr;
4453 	if (ca->m == NULL) {
4454 		return;
4455 	}
4456 	if (ca->inp != inp) {
4457 		/* TSNH */
4458 		return;
4459 	}
4460 	if ((ca->m) && (ca->m->m_pkthdr.len)) {
4461 		m = m_copym(ca->m, 0, M_COPYALL, M_DONTWAIT);
4462 		if (m == NULL) {
4463 			/* can't copy so we are done */
4464 			ca->cnt_failed++;
4465 			return;
4466 		}
4467 	} else {
4468 		m = NULL;
4469 	}
4470 	SCTP_TCB_LOCK_ASSERT(stcb);
4471 	if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
4472 		/* Abort this assoc with m as the user defined reason */
4473 		if (m) {
4474 			struct sctp_paramhdr *ph;
4475 
4476 			M_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
4477 			if (m) {
4478 				ph = mtod(m, struct sctp_paramhdr *);
4479 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4480 				ph->param_length = htons(m->m_pkthdr.len);
4481 			}
4482 			/*
4483 			 * We add one here to keep the assoc from
4484 			 * dis-appearing on us.
4485 			 */
4486 			atomic_add_int(&stcb->asoc.refcnt, 1);
4487 			sctp_abort_an_association(inp, stcb,
4488 			    SCTP_RESPONSE_TO_USER_REQ,
4489 			    m);
4490 			/*
4491 			 * sctp_abort_an_association calls sctp_free_asoc()
4492 			 * free association will NOT free it since we
4493 			 * incremented the refcnt .. we do this to prevent
4494 			 * it being freed and things getting tricky since we
4495 			 * could end up (from free_asoc) calling inpcb_free
4496 			 * which would get a recursive lock call to the
4497 			 * iterator lock.. But as a consequence of that the
4498 			 * stcb will return to us un-locked.. since
4499 			 * free_asoc returns with either no TCB or the TCB
4500 			 * unlocked, we must relock.. to unlock in the
4501 			 * iterator timer :-0
4502 			 */
4503 			SCTP_TCB_LOCK(stcb);
4504 			atomic_add_int(&stcb->asoc.refcnt, -1);
4505 			goto no_chunk_output;
4506 		}
4507 	} else {
4508 		if (m) {
4509 			ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
4510 			    &ca->sndrcv, 1);
4511 		}
4512 		asoc = &stcb->asoc;
4513 		if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
4514 			/* shutdown this assoc */
4515 			if (TAILQ_EMPTY(&asoc->send_queue) &&
4516 			    TAILQ_EMPTY(&asoc->sent_queue) &&
4517 			    (asoc->stream_queue_cnt == 0)) {
4518 				if (asoc->locked_on_sending) {
4519 					goto abort_anyway;
4520 				}
4521 				/*
4522 				 * there is nothing queued to send, so I'm
4523 				 * done...
4524 				 */
4525 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
4526 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
4527 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4528 					/*
4529 					 * only send SHUTDOWN the first time
4530 					 * through
4531 					 */
4532 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
4533 					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
4534 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4535 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
4536 					    asoc->primary_destination);
4537 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
4538 					    asoc->primary_destination);
4539 					added_control = 1;
4540 					do_chunk_output = 0;
4541 				}
4542 			} else {
4543 				/*
4544 				 * we still got (or just got) data to send,
4545 				 * so set SHUTDOWN_PENDING
4546 				 */
4547 				/*
4548 				 * XXX sockets draft says that SCTP_EOF
4549 				 * should be sent with no data.  currently,
4550 				 * we will allow user data to be sent first
4551 				 * and move to SHUTDOWN-PENDING
4552 				 */
4553 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
4554 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
4555 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4556 					if (asoc->locked_on_sending) {
4557 						/*
4558 						 * Locked to send out the
4559 						 * data
4560 						 */
4561 						struct sctp_stream_queue_pending *sp;
4562 
4563 						sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
4564 						if (sp) {
4565 							if ((sp->length == 0) && (sp->msg_is_complete == 0))
4566 								asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4567 						}
4568 					}
4569 					asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
4570 					if (TAILQ_EMPTY(&asoc->send_queue) &&
4571 					    TAILQ_EMPTY(&asoc->sent_queue) &&
4572 					    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
4573 				abort_anyway:
4574 						atomic_add_int(&stcb->asoc.refcnt, 1);
4575 						sctp_abort_an_association(stcb->sctp_ep, stcb,
4576 						    SCTP_RESPONSE_TO_USER_REQ,
4577 						    NULL);
4578 						atomic_add_int(&stcb->asoc.refcnt, -1);
4579 						goto no_chunk_output;
4580 					}
4581 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
4582 					    asoc->primary_destination);
4583 				}
4584 			}
4585 
4586 		}
4587 	}
4588 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
4589 	    ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)));
4590 
4591 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
4592 	    (stcb->asoc.total_flight > 0) &&
4593 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
4594 	    ) {
4595 		do_chunk_output = 0;
4596 	}
4597 	if (do_chunk_output)
4598 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND);
4599 	else if (added_control) {
4600 		int num_out = 0, reason = 0, cwnd_full = 0, now_filled = 0;
4601 		struct timeval now;
4602 		int frag_point;
4603 
4604 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
4605 		sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
4606 		    &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point);
4607 	}
4608 no_chunk_output:
4609 	if (ret) {
4610 		ca->cnt_failed++;
4611 	} else {
4612 		ca->cnt_sent++;
4613 	}
4614 }
4615 
4616 static void
4617 sctp_sendall_completes(void *ptr, uint32_t val)
4618 {
4619 	struct sctp_copy_all *ca;
4620 
4621 	ca = (struct sctp_copy_all *)ptr;
4622 	/*
4623 	 * Do a notify here? Kacheong suggests that the notify be done at
4624 	 * the send time.. so you would push up a notification if any send
4625 	 * failed. Don't know if this is feasable since the only failures we
4626 	 * have is "memory" related and if you cannot get an mbuf to send
4627 	 * the data you surely can't get an mbuf to send up to notify the
4628 	 * user you can't send the data :->
4629 	 */
4630 
4631 	/* now free everything */
4632 	sctp_m_freem(ca->m);
4633 	SCTP_FREE(ca);
4634 }
4635 
4636 
4637 #define	MC_ALIGN(m, len) do {						\
4638 	(m)->m_data += (MCLBYTES - (len)) & ~(sizeof(long) - 1);		\
4639 } while (0)
4640 
4641 
4642 
4643 static struct mbuf *
4644 sctp_copy_out_all(struct uio *uio, int len)
4645 {
4646 	struct mbuf *ret, *at;
4647 	int left, willcpy, cancpy, error;
4648 
4649 	ret = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_WAIT, 1, MT_DATA);
4650 	if (ret == NULL) {
4651 		/* TSNH */
4652 		return (NULL);
4653 	}
4654 	left = len;
4655 	ret->m_len = 0;
4656 	ret->m_pkthdr.len = len;
4657 	/* save space for the data chunk header */
4658 	cancpy = M_TRAILINGSPACE(ret);
4659 	willcpy = min(cancpy, left);
4660 	at = ret;
4661 	while (left > 0) {
4662 		/* Align data to the end */
4663 		error = uiomove(mtod(at, caddr_t), willcpy, uio);
4664 		if (error) {
4665 	err_out_now:
4666 			sctp_m_freem(at);
4667 			return (NULL);
4668 		}
4669 		at->m_len = willcpy;
4670 		at->m_nextpkt = at->m_next = 0;
4671 		left -= willcpy;
4672 		if (left > 0) {
4673 			at->m_next = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
4674 			if (at->m_next == NULL) {
4675 				goto err_out_now;
4676 			}
4677 			at = at->m_next;
4678 			at->m_len = 0;
4679 			cancpy = M_TRAILINGSPACE(at);
4680 			willcpy = min(cancpy, left);
4681 		}
4682 	}
4683 	return (ret);
4684 }
4685 
4686 static int
4687 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
4688     struct sctp_sndrcvinfo *srcv)
4689 {
4690 	int ret;
4691 	struct sctp_copy_all *ca;
4692 
4693 	SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
4694 	    "CopyAll");
4695 	if (ca == NULL) {
4696 		sctp_m_freem(m);
4697 		return (ENOMEM);
4698 	}
4699 	memset(ca, 0, sizeof(struct sctp_copy_all));
4700 
4701 	ca->inp = inp;
4702 	ca->sndrcv = *srcv;
4703 	/*
4704 	 * take off the sendall flag, it would be bad if we failed to do
4705 	 * this :-0
4706 	 */
4707 	ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
4708 	/* get length and mbuf chain */
4709 	if (uio) {
4710 		ca->sndlen = uio->uio_resid;
4711 		ca->m = sctp_copy_out_all(uio, ca->sndlen);
4712 		if (ca->m == NULL) {
4713 	out_no_mem:
4714 			SCTP_FREE(ca);
4715 			return (ENOMEM);
4716 		}
4717 	} else {
4718 		if ((m->m_flags & M_PKTHDR) == 0) {
4719 			struct mbuf *mat;
4720 
4721 			mat = m;
4722 			ca->sndlen = 0;
4723 			while (m) {
4724 				ca->sndlen += m->m_len;
4725 				m = m->m_next;
4726 			}
4727 			mat = sctp_get_mbuf_for_msg(1, 1, M_WAIT, 1, MT_DATA);
4728 			if (mat) {
4729 				sctp_m_freem(m);
4730 				goto out_no_mem;
4731 			}
4732 			/* We MUST have a header on the front */
4733 			mat->m_next = m;
4734 			mat->m_len = 0;
4735 			mat->m_pkthdr.len = ca->sndlen;
4736 			ca->m = mat;
4737 		} else {
4738 			ca->sndlen = m->m_pkthdr.len;
4739 		}
4740 		ca->m = m;
4741 	}
4742 	ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator,
4743 	    SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES, SCTP_ASOC_ANY_STATE,
4744 	    (void *)ca, 0,
4745 	    sctp_sendall_completes, inp, 1);
4746 	if (ret) {
4747 #ifdef SCTP_DEBUG
4748 		printf("Failed to initiate iterator for sendall\n");
4749 #endif
4750 		SCTP_FREE(ca);
4751 		return (EFAULT);
4752 	}
4753 	return (0);
4754 }
4755 
4756 
4757 void
4758 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
4759 {
4760 	struct sctp_tmit_chunk *chk, *nchk;
4761 
4762 	chk = TAILQ_FIRST(&asoc->control_send_queue);
4763 	while (chk) {
4764 		nchk = TAILQ_NEXT(chk, sctp_next);
4765 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
4766 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4767 			if (chk->data) {
4768 				sctp_m_freem(chk->data);
4769 				chk->data = NULL;
4770 			}
4771 			asoc->ctrl_queue_cnt--;
4772 			if (chk->whoTo)
4773 				sctp_free_remote_addr(chk->whoTo);
4774 			sctp_free_a_chunk(stcb, chk);
4775 		}
4776 		chk = nchk;
4777 	}
4778 }
4779 
4780 void
4781 sctp_toss_old_asconf(struct sctp_tcb *stcb)
4782 {
4783 	struct sctp_association *asoc;
4784 	struct sctp_tmit_chunk *chk, *chk_tmp;
4785 
4786 	asoc = &stcb->asoc;
4787 	for (chk = TAILQ_FIRST(&asoc->control_send_queue); chk != NULL;
4788 	    chk = chk_tmp) {
4789 		/* get next chk */
4790 		chk_tmp = TAILQ_NEXT(chk, sctp_next);
4791 		/* find SCTP_ASCONF chunk in queue (only one ever in queue) */
4792 		if (chk->rec.chunk_id.id == SCTP_ASCONF) {
4793 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4794 			if (chk->data) {
4795 				sctp_m_freem(chk->data);
4796 				chk->data = NULL;
4797 			}
4798 			asoc->ctrl_queue_cnt--;
4799 			if (chk->whoTo)
4800 				sctp_free_remote_addr(chk->whoTo);
4801 			sctp_free_a_chunk(stcb, chk);
4802 		}
4803 	}
4804 }
4805 
4806 
4807 static __inline void
4808 sctp_clean_up_datalist(struct sctp_tcb *stcb,
4809 
4810     struct sctp_association *asoc,
4811     struct sctp_tmit_chunk **data_list,
4812     int bundle_at,
4813     struct sctp_nets *net)
4814 {
4815 	int i;
4816 	struct sctp_tmit_chunk *tp1;
4817 
4818 	for (i = 0; i < bundle_at; i++) {
4819 		/* off of the send queue */
4820 		if (i) {
4821 			/*
4822 			 * Any chunk NOT 0 you zap the time chunk 0 gets
4823 			 * zapped or set based on if a RTO measurment is
4824 			 * needed.
4825 			 */
4826 			data_list[i]->do_rtt = 0;
4827 		}
4828 		/* record time */
4829 		data_list[i]->sent_rcv_time = net->last_sent_time;
4830 		data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
4831 		TAILQ_REMOVE(&asoc->send_queue,
4832 		    data_list[i],
4833 		    sctp_next);
4834 		/* on to the sent queue */
4835 		tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
4836 		if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
4837 		    data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
4838 			struct sctp_tmit_chunk *tpp;
4839 
4840 			/* need to move back */
4841 	back_up_more:
4842 			tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
4843 			if (tpp == NULL) {
4844 				TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
4845 				goto all_done;
4846 			}
4847 			tp1 = tpp;
4848 			if (compare_with_wrap(tp1->rec.data.TSN_seq,
4849 			    data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
4850 				goto back_up_more;
4851 			}
4852 			TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
4853 		} else {
4854 			TAILQ_INSERT_TAIL(&asoc->sent_queue,
4855 			    data_list[i],
4856 			    sctp_next);
4857 		}
4858 all_done:
4859 		/* This does not lower until the cum-ack passes it */
4860 		asoc->sent_queue_cnt++;
4861 		asoc->send_queue_cnt--;
4862 		if ((asoc->peers_rwnd <= 0) &&
4863 		    (asoc->total_flight == 0) &&
4864 		    (bundle_at == 1)) {
4865 			/* Mark the chunk as being a window probe */
4866 			SCTP_STAT_INCR(sctps_windowprobed);
4867 			data_list[i]->rec.data.state_flags |= SCTP_WINDOW_PROBE;
4868 		} else {
4869 			data_list[i]->rec.data.state_flags &= ~SCTP_WINDOW_PROBE;
4870 		}
4871 #ifdef SCTP_AUDITING_ENABLED
4872 		sctp_audit_log(0xC2, 3);
4873 #endif
4874 		data_list[i]->sent = SCTP_DATAGRAM_SENT;
4875 		data_list[i]->snd_count = 1;
4876 		data_list[i]->rec.data.chunk_was_revoked = 0;
4877 #ifdef SCTP_FLIGHT_LOGGING
4878 		sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
4879 		    data_list[i]->whoTo->flight_size,
4880 		    data_list[i]->book_size,
4881 		    (uintptr_t) stcb,
4882 		    data_list[i]->rec.data.TSN_seq);
4883 #endif
4884 		net->flight_size += data_list[i]->book_size;
4885 		asoc->total_flight += data_list[i]->book_size;
4886 		asoc->total_flight_count++;
4887 #ifdef SCTP_LOG_RWND
4888 		sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
4889 		    asoc->peers_rwnd, data_list[i]->send_size, sctp_peer_chunk_oh);
4890 #endif
4891 		asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
4892 		    (uint32_t) (data_list[i]->send_size + sctp_peer_chunk_oh));
4893 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4894 			/* SWS sender side engages */
4895 			asoc->peers_rwnd = 0;
4896 		}
4897 	}
4898 }
4899 
4900 static __inline void
4901 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
4902 {
4903 	struct sctp_tmit_chunk *chk, *nchk;
4904 
4905 	for (chk = TAILQ_FIRST(&asoc->control_send_queue);
4906 	    chk; chk = nchk) {
4907 		nchk = TAILQ_NEXT(chk, sctp_next);
4908 		if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
4909 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
4910 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
4911 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
4912 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
4913 		    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
4914 		    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
4915 		    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
4916 		    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
4917 		    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
4918 			/* Stray chunks must be cleaned up */
4919 	clean_up_anyway:
4920 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4921 			if (chk->data) {
4922 				sctp_m_freem(chk->data);
4923 				chk->data = NULL;
4924 			}
4925 			asoc->ctrl_queue_cnt--;
4926 			sctp_free_remote_addr(chk->whoTo);
4927 			sctp_free_a_chunk(stcb, chk);
4928 		} else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
4929 			/* special handling, we must look into the param */
4930 			if (chk != asoc->str_reset) {
4931 				goto clean_up_anyway;
4932 			}
4933 		}
4934 	}
4935 }
4936 
4937 extern int sctp_min_split_point;
4938 
4939 static __inline int
4940 sctp_can_we_split_this(struct sctp_tcb *stcb,
4941     struct sctp_stream_queue_pending *sp,
4942     int goal_mtu, int frag_point, int eeor_on)
4943 {
4944 	/*
4945 	 * Make a decision on if I should split a msg into multiple parts.
4946 	 */
4947 	if (goal_mtu < sctp_min_split_point) {
4948 		/* you don't want enough */
4949 		return (0);
4950 	}
4951 	if (sp->msg_is_complete == 0) {
4952 		if (eeor_on) {
4953 			/*
4954 			 * If we are doing EEOR we need to always send it if
4955 			 * its the entire thing.
4956 			 */
4957 			if (goal_mtu >= sp->length)
4958 				return (sp->length);
4959 		} else {
4960 			if (goal_mtu >= sp->length) {
4961 				/*
4962 				 * If we cannot fill the amount needed there
4963 				 * is no sense of splitting the chunk.
4964 				 */
4965 				return (0);
4966 			}
4967 		}
4968 		/*
4969 		 * If we reach here sp->length is larger than the goal_mtu.
4970 		 * Do we wish to split it for the sake of packet putting
4971 		 * together?
4972 		 */
4973 		if (goal_mtu >= min(sctp_min_split_point, stcb->asoc.smallest_mtu)) {
4974 			/* Its ok to split it */
4975 			return (min(goal_mtu, frag_point));
4976 		}
4977 	} else {
4978 		/* We can always split a complete message to make it fit */
4979 		if (goal_mtu >= sp->length)
4980 			/* Take it all */
4981 			return (sp->length);
4982 
4983 		return (min(goal_mtu, frag_point));
4984 	}
4985 	/* Nope, can't split */
4986 	return (0);
4987 
4988 }
4989 
4990 static int
4991 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
4992     struct sctp_stream_out *strq,
4993     int goal_mtu,
4994     int frag_point,
4995     int *locked,
4996     int *giveup,
4997     int eeor_mode)
4998 {
4999 	/* Move from the stream to the send_queue keeping track of the total */
5000 	struct sctp_association *asoc;
5001 	struct sctp_stream_queue_pending *sp;
5002 	struct sctp_tmit_chunk *chk;
5003 	struct sctp_data_chunk *dchkh;
5004 	int to_move;
5005 	uint8_t rcv_flags = 0;
5006 	uint8_t some_taken;
5007 	uint8_t took_all = 0;
5008 
5009 	SCTP_TCB_LOCK_ASSERT(stcb);
5010 	asoc = &stcb->asoc;
5011 	sp = TAILQ_FIRST(&strq->outqueue);
5012 	if (sp == NULL) {
5013 		*locked = 0;
5014 		SCTP_TCB_SEND_LOCK(stcb);
5015 		if (strq->last_msg_incomplete) {
5016 			printf("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
5017 			    strq->stream_no, strq->last_msg_incomplete);
5018 			strq->last_msg_incomplete = 0;
5019 		}
5020 		SCTP_TCB_SEND_UNLOCK(stcb);
5021 		return (0);
5022 	}
5023 	SCTP_TCB_SEND_LOCK(stcb);
5024 	if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
5025 		/* Must wait for more data, must be last msg */
5026 		*locked = 1;
5027 		*giveup = 1;
5028 		SCTP_TCB_SEND_UNLOCK(stcb);
5029 		return (0);
5030 	} else if (sp->length == 0) {
5031 		/* This should not happen */
5032 		panic("sp length is 0?");
5033 	}
5034 	some_taken = sp->some_taken;
5035 	if ((goal_mtu >= sp->length) && (sp->msg_is_complete)) {
5036 		/* It all fits and its a complete msg, no brainer */
5037 		to_move = min(sp->length, frag_point);
5038 		if (to_move == sp->length) {
5039 			/* Getting it all */
5040 			if (sp->some_taken) {
5041 				rcv_flags |= SCTP_DATA_LAST_FRAG;
5042 			} else {
5043 				rcv_flags |= SCTP_DATA_NOT_FRAG;
5044 			}
5045 		} else {
5046 			/* Not getting it all, frag point overrides */
5047 			if (sp->some_taken == 0) {
5048 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
5049 			}
5050 			sp->some_taken = 1;
5051 		}
5052 	} else {
5053 		to_move = sctp_can_we_split_this(stcb, sp, goal_mtu,
5054 		    frag_point, eeor_mode);
5055 		if (to_move) {
5056 			if (to_move >= sp->length) {
5057 				to_move = sp->length;
5058 			}
5059 			if (sp->some_taken == 0) {
5060 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
5061 			}
5062 			sp->some_taken = 1;
5063 		} else {
5064 			if (sp->some_taken) {
5065 				*locked = 1;
5066 			}
5067 			*giveup = 1;
5068 			SCTP_TCB_SEND_UNLOCK(stcb);
5069 			return (0);
5070 		}
5071 	}
5072 	SCTP_TCB_SEND_UNLOCK(stcb);
5073 	/* If we reach here, we can copy out a chunk */
5074 	sctp_alloc_a_chunk(stcb, chk);
5075 	if (chk == NULL) {
5076 		/* No chunk memory */
5077 out_gu:
5078 		*giveup = 1;
5079 		return (0);
5080 	}
5081 	/*
5082 	 * Setup for unordered if needed by looking at the user sent info
5083 	 * flags.
5084 	 */
5085 	if (sp->sinfo_flags & SCTP_UNORDERED) {
5086 		rcv_flags |= SCTP_DATA_UNORDERED;
5087 	}
5088 	/* clear out the chunk before setting up */
5089 	memset(chk, sizeof(*chk), 0);
5090 	chk->rec.data.rcv_flags = rcv_flags;
5091 	SCTP_TCB_SEND_LOCK(stcb);
5092 	if (sp->data->m_flags & M_EXT) {
5093 		chk->copy_by_ref = 1;
5094 	} else {
5095 		chk->copy_by_ref = 0;
5096 	}
5097 	if (to_move >= sp->length) {
5098 		/* we can steal the whole thing */
5099 		chk->data = sp->data;
5100 		chk->last_mbuf = sp->tail_mbuf;
5101 		/* register the stealing */
5102 		sp->data = sp->tail_mbuf = NULL;
5103 		took_all = 1;
5104 	} else {
5105 		struct mbuf *m;
5106 
5107 		chk->data = m_copym(sp->data, 0, to_move, M_DONTWAIT);
5108 		chk->last_mbuf = NULL;
5109 		if (chk->data == NULL) {
5110 			sp->some_taken = some_taken;
5111 			sctp_free_a_chunk(stcb, chk);
5112 			SCTP_TCB_SEND_UNLOCK(stcb);
5113 			goto out_gu;
5114 		}
5115 		/* Pull off the data */
5116 		m_adj(sp->data, to_move);
5117 		/* Now lets work our way down and compact it */
5118 		m = sp->data;
5119 		while (m && (m->m_len == 0)) {
5120 			sp->data = m->m_next;
5121 			m->m_next = NULL;
5122 			if (sp->tail_mbuf == m) {
5123 				/* freeing tail */
5124 				sp->tail_mbuf = sp->data;
5125 			}
5126 			sctp_m_free(m);
5127 			m = sp->data;
5128 		}
5129 	}
5130 	if (to_move > sp->length) {
5131 		panic("Huh, how can to_move be larger?");
5132 	} else {
5133 		sp->length -= to_move;
5134 	}
5135 
5136 	/* Update the new length in */
5137 	if (sp->data && (sp->data->m_flags & M_PKTHDR)) {
5138 		/* update length */
5139 		sp->data->m_pkthdr.len = sp->length;
5140 	}
5141 	if (M_LEADINGSPACE(chk->data) < sizeof(struct sctp_data_chunk)) {
5142 		/* Not enough room for a chunk header, get some */
5143 		struct mbuf *m;
5144 
5145 		m = sctp_get_mbuf_for_msg(1, 1, M_DONTWAIT, 0, MT_DATA);
5146 		if (m == NULL) {
5147 			/*
5148 			 * we're in trouble here. M_PREPEND below will free
5149 			 * all the data if there is no leading space, so we
5150 			 * must put the data back and restore.
5151 			 */
5152 			if (took_all) {
5153 				/* unsteal the data */
5154 				sp->data = chk->data;
5155 				sp->tail_mbuf = chk->last_mbuf;
5156 			} else {
5157 				struct mbuf *m;
5158 
5159 				/* reassemble the data */
5160 				m = sp->data;
5161 				sp->data = chk->data;
5162 				sp->data->m_next = m;
5163 			}
5164 			sp->some_taken = some_taken;
5165 			sp->length += to_move;
5166 			if (sp->data && (sp->data->m_flags & M_PKTHDR)) {
5167 				sp->data->m_pkthdr.len = sp->length;
5168 			}
5169 			sctp_free_a_chunk(stcb, chk);
5170 			SCTP_TCB_SEND_UNLOCK(stcb);
5171 			goto out_gu;
5172 		} else {
5173 			m->m_len = 0;
5174 			m->m_next = chk->data;
5175 			chk->data = m;
5176 			chk->data->m_pkthdr.len = to_move;
5177 			MH_ALIGN(chk->data, 4);
5178 		}
5179 	}
5180 	M_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
5181 	if (chk->data == NULL) {
5182 		/* HELP */
5183 		sctp_free_a_chunk(stcb, chk);
5184 		SCTP_TCB_SEND_UNLOCK(stcb);
5185 		goto out_gu;
5186 	}
5187 	sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
5188 	chk->book_size = chk->send_size = (to_move +
5189 	    sizeof(struct sctp_data_chunk));
5190 	chk->sent = SCTP_DATAGRAM_UNSENT;
5191 
5192 	/*
5193 	 * get last_mbuf and counts of mb useage This is ugly but hopefully
5194 	 * its only one mbuf.
5195 	 */
5196 	if (chk->last_mbuf == NULL) {
5197 		chk->last_mbuf = chk->data;
5198 		while (chk->last_mbuf->m_next != NULL) {
5199 			chk->last_mbuf = chk->last_mbuf->m_next;
5200 		}
5201 	}
5202 	chk->flags = 0;
5203 	chk->asoc = &stcb->asoc;
5204 	chk->pad_inplace = 0;
5205 	chk->no_fr_allowed = 0;
5206 	chk->rec.data.stream_seq = sp->strseq;
5207 	chk->rec.data.stream_number = sp->stream;
5208 	chk->rec.data.payloadtype = sp->ppid;
5209 	chk->rec.data.context = sp->context;
5210 	chk->rec.data.doing_fast_retransmit = 0;
5211 	chk->rec.data.ect_nonce = 0;	/* ECN Nonce */
5212 
5213 	chk->rec.data.timetodrop = sp->ts;
5214 	chk->flags = sp->act_flags;
5215 	chk->addr_over = sp->addr_over;
5216 
5217 	chk->whoTo = net;
5218 	atomic_add_int(&chk->whoTo->ref_count, 1);
5219 
5220 	chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
5221 #ifdef SCTP_LOG_SENDING_STR
5222 	sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
5223 	    (uintptr_t) stcb, (uintptr_t) sp,
5224 	    (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
5225 	    chk->rec.data.TSN_seq);
5226 #endif
5227 
5228 	dchkh = mtod(chk->data, struct sctp_data_chunk *);
5229 	/*
5230 	 * Put the rest of the things in place now. Size was done earlier in
5231 	 * previous loop prior to padding.
5232 	 */
5233 	dchkh->ch.chunk_type = SCTP_DATA;
5234 	dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
5235 	dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
5236 	dchkh->dp.stream_id = htons(strq->stream_no);
5237 	dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
5238 	dchkh->dp.protocol_id = chk->rec.data.payloadtype;
5239 	dchkh->ch.chunk_length = htons(chk->send_size);
5240 	/* Now advance the chk->send_size by the actual pad needed. */
5241 	if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
5242 		/* need a pad */
5243 		struct mbuf *lm;
5244 		int pads;
5245 
5246 		pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
5247 		if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
5248 			chk->pad_inplace = 1;
5249 		}
5250 		if ((lm = chk->last_mbuf->m_next) != NULL) {
5251 			/* pad added an mbuf */
5252 			chk->last_mbuf = lm;
5253 		}
5254 		if (chk->data->m_flags & M_PKTHDR) {
5255 			chk->data->m_pkthdr.len += pads;
5256 		}
5257 		chk->send_size += pads;
5258 	}
5259 	/* We only re-set the policy if it is on */
5260 	if (sp->pr_sctp_on) {
5261 		sctp_set_prsctp_policy(stcb, sp);
5262 	}
5263 	if (sp->msg_is_complete && (sp->length == 0)) {
5264 		/* All done pull and kill the message */
5265 		asoc->stream_queue_cnt--;
5266 		TAILQ_REMOVE(&strq->outqueue, sp, next);
5267 		sctp_free_remote_addr(sp->net);
5268 		if (sp->data) {
5269 			sctp_m_freem(sp->data);
5270 			sp->data = NULL;
5271 		}
5272 		sctp_free_a_strmoq(stcb, sp);
5273 
5274 		/* we can't be locked to it */
5275 		*locked = 0;
5276 		stcb->asoc.locked_on_sending = NULL;
5277 	} else {
5278 		/* more to go, we are locked */
5279 		*locked = 1;
5280 	}
5281 	asoc->chunks_on_out_queue++;
5282 	if (sp->pr_sctp_on) {
5283 		asoc->pr_sctp_cnt++;
5284 		chk->pr_sctp_on = 1;
5285 	} else {
5286 		chk->pr_sctp_on = 0;
5287 	}
5288 	TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
5289 	asoc->send_queue_cnt++;
5290 	SCTP_TCB_SEND_UNLOCK(stcb);
5291 	return (to_move);
5292 }
5293 
5294 
5295 static struct sctp_stream_out *
5296 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
5297 {
5298 	struct sctp_stream_out *strq;
5299 
5300 	/* Find the next stream to use */
5301 	if (asoc->last_out_stream == NULL) {
5302 		strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
5303 		if (asoc->last_out_stream == NULL) {
5304 			/* huh nothing on the wheel, TSNH */
5305 			return (NULL);
5306 		}
5307 		goto done_it;
5308 	}
5309 	strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
5310 done_it:
5311 	if (strq == NULL) {
5312 		strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
5313 	}
5314 	return (strq);
5315 
5316 }
5317 
5318 static void
5319 sctp_fill_outqueue(struct sctp_tcb *stcb,
5320     struct sctp_nets *net, int frag_point, int eeor_mode)
5321 {
5322 	struct sctp_association *asoc;
5323 	struct sctp_stream_out *strq, *strqn;
5324 	int goal_mtu, moved_how_much, total_moved = 0;
5325 	int locked, giveup;
5326 	struct sctp_stream_queue_pending *sp;
5327 
5328 	SCTP_TCB_LOCK_ASSERT(stcb);
5329 	asoc = &stcb->asoc;
5330 #ifdef AF_INET6
5331 	if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
5332 		goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
5333 	} else {
5334 		/* ?? not sure what else to do */
5335 		goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
5336 	}
5337 #else
5338 	goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
5339 	mtu_fromwheel = 0;
5340 #endif
5341 	/* Need an allowance for the data chunk header too */
5342 	goal_mtu -= sizeof(struct sctp_data_chunk);
5343 
5344 	/* must make even word boundary */
5345 	goal_mtu &= 0xfffffffc;
5346 	if (asoc->locked_on_sending) {
5347 		/* We are stuck on one stream until the message completes. */
5348 		strqn = strq = asoc->locked_on_sending;
5349 		locked = 1;
5350 	} else {
5351 		strqn = strq = sctp_select_a_stream(stcb, asoc);
5352 		locked = 0;
5353 	}
5354 
5355 	while ((goal_mtu > 0) && strq) {
5356 		sp = TAILQ_FIRST(&strq->outqueue);
5357 		/*
5358 		 * If CMT is off, we must validate that the stream in
5359 		 * question has the first item pointed towards are network
5360 		 * destionation requested by the caller. Note that if we
5361 		 * turn out to be locked to a stream (assigning TSN's then
5362 		 * we must stop, since we cannot look for another stream
5363 		 * with data to send to that destination). In CMT's case, by
5364 		 * skipping this check, we will send one data packet towards
5365 		 * the requested net.
5366 		 */
5367 		if (sp == NULL) {
5368 			break;
5369 		}
5370 		if ((sp->net != net) && (sctp_cmt_on_off == 0)) {
5371 			/* none for this network */
5372 			if (locked) {
5373 				break;
5374 			} else {
5375 				strq = sctp_select_a_stream(stcb, asoc);
5376 				if (strq == NULL)
5377 					/* none left */
5378 					break;
5379 				if (strqn == strq) {
5380 					/* I have circled */
5381 					break;
5382 				}
5383 				continue;
5384 			}
5385 		}
5386 		giveup = 0;
5387 		moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
5388 		    &giveup, eeor_mode);
5389 		asoc->last_out_stream = strq;
5390 		if (locked) {
5391 			asoc->locked_on_sending = strq;
5392 			if ((moved_how_much == 0) || (giveup))
5393 				/* no more to move for now */
5394 				break;
5395 		} else {
5396 			asoc->locked_on_sending = NULL;
5397 			if (TAILQ_FIRST(&strq->outqueue) == NULL) {
5398 				sctp_remove_from_wheel(stcb, asoc, strq);
5399 			}
5400 			if (giveup) {
5401 				break;
5402 			}
5403 			strq = sctp_select_a_stream(stcb, asoc);
5404 			if (strq == NULL) {
5405 				break;
5406 			}
5407 		}
5408 		total_moved += moved_how_much;
5409 		goal_mtu -= moved_how_much;
5410 		goal_mtu &= 0xfffffffc;
5411 	}
5412 	if (total_moved == 0) {
5413 		if ((sctp_cmt_on_off == 0) &&
5414 		    (net == stcb->asoc.primary_destination)) {
5415 			/* ran dry for primary network net */
5416 			SCTP_STAT_INCR(sctps_primary_randry);
5417 		} else if (sctp_cmt_on_off) {
5418 			/* ran dry with CMT on */
5419 			SCTP_STAT_INCR(sctps_cmt_randry);
5420 		}
5421 	}
5422 }
5423 
5424 __inline void
5425 sctp_fix_ecn_echo(struct sctp_association *asoc)
5426 {
5427 	struct sctp_tmit_chunk *chk;
5428 
5429 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
5430 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
5431 			chk->sent = SCTP_DATAGRAM_UNSENT;
5432 		}
5433 	}
5434 }
5435 
5436 static void
5437 sctp_move_to_an_alt(struct sctp_tcb *stcb,
5438     struct sctp_association *asoc,
5439     struct sctp_nets *net)
5440 {
5441 	struct sctp_tmit_chunk *chk;
5442 	struct sctp_nets *a_net;
5443 
5444 	SCTP_TCB_LOCK_ASSERT(stcb);
5445 	a_net = sctp_find_alternate_net(stcb, net, 0);
5446 	if ((a_net != net) &&
5447 	    ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
5448 		/*
5449 		 * We only proceed if a valid alternate is found that is not
5450 		 * this one and is reachable. Here we must move all chunks
5451 		 * queued in the send queue off of the destination address
5452 		 * to our alternate.
5453 		 */
5454 		TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
5455 			if (chk->whoTo == net) {
5456 				/* Move the chunk to our alternate */
5457 				sctp_free_remote_addr(chk->whoTo);
5458 				chk->whoTo = a_net;
5459 				atomic_add_int(&a_net->ref_count, 1);
5460 			}
5461 		}
5462 	}
5463 }
5464 
5465 extern int sctp_early_fr;
5466 
5467 int
5468 sctp_med_chunk_output(struct sctp_inpcb *inp,
5469     struct sctp_tcb *stcb,
5470     struct sctp_association *asoc,
5471     int *num_out,
5472     int *reason_code,
5473     int control_only, int *cwnd_full, int from_where,
5474     struct timeval *now, int *now_filled, int frag_point)
5475 {
5476 	/*
5477 	 * Ok this is the generic chunk service queue. we must do the
5478 	 * following: - Service the stream queue that is next, moving any
5479 	 * message (note I must get a complete message i.e. FIRST/MIDDLE and
5480 	 * LAST to the out queue in one pass) and assigning TSN's - Check to
5481 	 * see if the cwnd/rwnd allows any output, if so we go ahead and
5482 	 * fomulate and send the low level chunks. Making sure to combine
5483 	 * any control in the control chunk queue also.
5484 	 */
5485 	struct sctp_nets *net;
5486 	struct mbuf *outchain, *endoutchain;
5487 	struct sctp_tmit_chunk *chk, *nchk;
5488 	struct sctphdr *shdr;
5489 
5490 	/* temp arrays for unlinking */
5491 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
5492 	int no_fragmentflg, error;
5493 	int one_chunk, hbflag;
5494 	int asconf, cookie, no_out_cnt;
5495 	int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind, eeor_mode;
5496 	unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
5497 
5498 	*num_out = 0;
5499 	struct sctp_nets *start_at, *old_startat = NULL, *send_start_at;
5500 
5501 	cwnd_full_ind = 0;
5502 	int tsns_sent = 0;
5503 	uint32_t auth_offset = 0;
5504 	struct sctp_auth_chunk *auth = NULL;
5505 
5506 	if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
5507 	    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
5508 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
5509 		eeor_mode = 1;
5510 	} else {
5511 		eeor_mode = 0;
5512 	}
5513 	ctl_cnt = no_out_cnt = asconf = cookie = 0;
5514 	/*
5515 	 * First lets prime the pump. For each destination, if there is room
5516 	 * in the flight size, attempt to pull an MTU's worth out of the
5517 	 * stream queues into the general send_queue
5518 	 */
5519 #ifdef SCTP_AUDITING_ENABLED
5520 	sctp_audit_log(0xC2, 2);
5521 #endif
5522 	SCTP_TCB_LOCK_ASSERT(stcb);
5523 	hbflag = 0;
5524 	if ((control_only) || (asoc->stream_reset_outstanding))
5525 		no_data_chunks = 1;
5526 	else
5527 		no_data_chunks = 0;
5528 
5529 	/* Nothing to possible to send? */
5530 	if (TAILQ_EMPTY(&asoc->control_send_queue) &&
5531 	    TAILQ_EMPTY(&asoc->send_queue) &&
5532 	    TAILQ_EMPTY(&asoc->out_wheel)) {
5533 		*reason_code = 9;
5534 		return (0);
5535 	}
5536 	if (asoc->peers_rwnd == 0) {
5537 		/* No room in peers rwnd */
5538 		*cwnd_full = 1;
5539 		*reason_code = 1;
5540 		if (asoc->total_flight > 0) {
5541 			/* we are allowed one chunk in flight */
5542 			no_data_chunks = 1;
5543 		}
5544 	}
5545 	if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
5546 		if (sctp_cmt_on_off) {
5547 			/*
5548 			 * for CMT we start at the next one past the one we
5549 			 * last added data to.
5550 			 */
5551 			if (TAILQ_FIRST(&asoc->send_queue) != NULL) {
5552 				goto skip_the_fill_from_streams;
5553 			}
5554 			if (asoc->last_net_data_came_from) {
5555 				net = TAILQ_NEXT(asoc->last_net_data_came_from, sctp_next);
5556 				if (net == NULL) {
5557 					net = TAILQ_FIRST(&asoc->nets);
5558 				}
5559 			} else {
5560 				/* back to start */
5561 				net = TAILQ_FIRST(&asoc->nets);
5562 			}
5563 
5564 		} else {
5565 			net = asoc->primary_destination;
5566 			if (net == NULL) {
5567 				/* TSNH */
5568 				net = TAILQ_FIRST(&asoc->nets);
5569 			}
5570 		}
5571 		start_at = net;
5572 one_more_time:
5573 		for (; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
5574 			if (old_startat && (old_startat == net)) {
5575 				break;
5576 			}
5577 			if ((sctp_cmt_on_off == 0) && (net->ref_count < 2)) {
5578 				/* nothing can be in queue for this guy */
5579 				continue;
5580 			}
5581 			if (net->flight_size >= net->cwnd) {
5582 				/* skip this network, no room */
5583 				cwnd_full_ind++;
5584 				continue;
5585 			}
5586 			/*
5587 			 * @@@ JRI : this for loop we are in takes in each
5588 			 * net, if its's got space in cwnd and has data sent
5589 			 * to it (when CMT is off) then it calls
5590 			 * sctp_fill_outqueue for the net. This gets data on
5591 			 * the send queue for that network.
5592 			 *
5593 			 * In sctp_fill_outqueue TSN's are assigned and data is
5594 			 * copied out of the stream buffers. Note mostly
5595 			 * copy by reference (we hope).
5596 			 */
5597 #ifdef SCTP_CWND_LOGGING
5598 			sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
5599 #endif
5600 			sctp_fill_outqueue(stcb, net, frag_point, eeor_mode);
5601 		}
5602 		if (start_at != TAILQ_FIRST(&asoc->nets)) {
5603 			/* got to pick up the beginning stuff. */
5604 			old_startat = start_at;
5605 			start_at = net = TAILQ_FIRST(&asoc->nets);
5606 			goto one_more_time;
5607 		}
5608 	}
5609 skip_the_fill_from_streams:
5610 	*cwnd_full = cwnd_full_ind;
5611 	/* now service each destination and send out what we can for it */
5612 	/* Nothing to send? */
5613 	if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
5614 	    (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
5615 		*reason_code = 8;
5616 		return (0);
5617 	}
5618 	chk = TAILQ_FIRST(&asoc->send_queue);
5619 	if (chk) {
5620 		send_start_at = chk->whoTo;
5621 	} else {
5622 		send_start_at = TAILQ_FIRST(&asoc->nets);
5623 	}
5624 	old_startat = NULL;
5625 again_one_more_time:
5626 	for (net = send_start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
5627 		/* how much can we send? */
5628 		/* printf("Examine for sending net:%x\n", (uint32_t)net); */
5629 		if (old_startat && (old_startat == net)) {
5630 			/* through list ocmpletely. */
5631 			break;
5632 		}
5633 		tsns_sent = 0;
5634 		if (net->ref_count < 2) {
5635 			/*
5636 			 * Ref-count of 1 so we cannot have data or control
5637 			 * queued to this address. Skip it.
5638 			 */
5639 			continue;
5640 		}
5641 		ctl_cnt = bundle_at = 0;
5642 		endoutchain = outchain = NULL;
5643 		no_fragmentflg = 1;
5644 		one_chunk = 0;
5645 
5646 		if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
5647 			/*
5648 			 * if we have a route and an ifp check to see if we
5649 			 * have room to send to this guy
5650 			 */
5651 			struct ifnet *ifp;
5652 
5653 			ifp = net->ro.ro_rt->rt_ifp;
5654 			if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
5655 				SCTP_STAT_INCR(sctps_ifnomemqueued);
5656 #ifdef SCTP_LOG_MAXBURST
5657 				sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
5658 #endif
5659 				continue;
5660 			}
5661 		}
5662 		if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
5663 			mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
5664 		} else {
5665 			mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
5666 		}
5667 		mx_mtu = mtu;
5668 		to_out = 0;
5669 		if (mtu > asoc->peers_rwnd) {
5670 			if (asoc->total_flight > 0) {
5671 				/* We have a packet in flight somewhere */
5672 				r_mtu = asoc->peers_rwnd;
5673 			} else {
5674 				/* We are always allowed to send one MTU out */
5675 				one_chunk = 1;
5676 				r_mtu = mtu;
5677 			}
5678 		} else {
5679 			r_mtu = mtu;
5680 		}
5681 		/************************/
5682 		/* Control transmission */
5683 		/************************/
5684 		/* Now first lets go through the control queue */
5685 		for (chk = TAILQ_FIRST(&asoc->control_send_queue);
5686 		    chk; chk = nchk) {
5687 			nchk = TAILQ_NEXT(chk, sctp_next);
5688 			if (chk->whoTo != net) {
5689 				/*
5690 				 * No, not sent to the network we are
5691 				 * looking at
5692 				 */
5693 				continue;
5694 			}
5695 			if (chk->data == NULL) {
5696 				continue;
5697 			}
5698 			if ((chk->data->m_flags & M_PKTHDR) == 0) {
5699 				/*
5700 				 * NOTE: the chk queue MUST have the PKTHDR
5701 				 * flag set on it with a total in the
5702 				 * m_pkthdr.len field!! else the chunk will
5703 				 * ALWAYS be skipped
5704 				 */
5705 				continue;
5706 			}
5707 			if (chk->sent != SCTP_DATAGRAM_UNSENT) {
5708 				/*
5709 				 * It must be unsent. Cookies and ASCONF's
5710 				 * hang around but there timers will force
5711 				 * when marked for resend.
5712 				 */
5713 				continue;
5714 			}
5715 			/*
5716 			 * if no AUTH is yet included and this chunk
5717 			 * requires it, make sure to account for it.  We
5718 			 * don't apply the size until the AUTH chunk is
5719 			 * actually added below in case there is no room for
5720 			 * this chunk. NOTE: we overload the use of "omtu"
5721 			 * here
5722 			 */
5723 			if ((auth == NULL) &&
5724 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
5725 			    stcb->asoc.peer_auth_chunks)) {
5726 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5727 			} else
5728 				omtu = 0;
5729 			/* Here we do NOT factor the r_mtu */
5730 			if ((chk->data->m_pkthdr.len < (int)(mtu - omtu)) ||
5731 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
5732 				/*
5733 				 * We probably should glom the mbuf chain
5734 				 * from the chk->data for control but the
5735 				 * problem is it becomes yet one more level
5736 				 * of tracking to do if for some reason
5737 				 * output fails. Then I have got to
5738 				 * reconstruct the merged control chain.. el
5739 				 * yucko.. for now we take the easy way and
5740 				 * do the copy
5741 				 */
5742 				/*
5743 				 * Add an AUTH chunk, if chunk requires it
5744 				 * save the offset into the chain for AUTH
5745 				 */
5746 				if ((auth == NULL) &&
5747 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
5748 				    stcb->asoc.peer_auth_chunks))) {
5749 					outchain = sctp_add_auth_chunk(outchain,
5750 					    &endoutchain,
5751 					    &auth,
5752 					    &auth_offset,
5753 					    stcb,
5754 					    chk->rec.chunk_id.id);
5755 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5756 				}
5757 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
5758 				    (int)chk->rec.chunk_id.can_take_data,
5759 				    chk->data->m_pkthdr.len, chk->copy_by_ref);
5760 				if (outchain == NULL) {
5761 					*reason_code = 8;
5762 					return (ENOMEM);
5763 				}
5764 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5765 				/* update our MTU size */
5766 				if (mtu > (chk->data->m_pkthdr.len + omtu))
5767 					mtu -= (chk->data->m_pkthdr.len + omtu);
5768 				else
5769 					mtu = 0;
5770 				to_out += (chk->data->m_pkthdr.len + omtu);
5771 				/* Do clear IP_DF ? */
5772 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
5773 					no_fragmentflg = 0;
5774 				}
5775 				if (chk->rec.chunk_id.can_take_data)
5776 					chk->data = NULL;
5777 				/* Mark things to be removed, if needed */
5778 				if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
5779 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
5780 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
5781 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
5782 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
5783 				    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
5784 				    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
5785 				    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
5786 				    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
5787 				    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
5788 
5789 					if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST)
5790 						hbflag = 1;
5791 					/* remove these chunks at the end */
5792 					if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
5793 						/* turn off the timer */
5794 						if (callout_pending(&stcb->asoc.dack_timer.timer)) {
5795 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
5796 							    inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
5797 						}
5798 					}
5799 					ctl_cnt++;
5800 				} else {
5801 					/*
5802 					 * Other chunks, since they have
5803 					 * timers running (i.e. COOKIE or
5804 					 * ASCONF) we just "trust" that it
5805 					 * gets sent or retransmitted.
5806 					 */
5807 					ctl_cnt++;
5808 					if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
5809 						cookie = 1;
5810 						no_out_cnt = 1;
5811 					} else if (chk->rec.chunk_id.id == SCTP_ASCONF) {
5812 						/*
5813 						 * set hb flag since we can
5814 						 * use these for RTO
5815 						 */
5816 						hbflag = 1;
5817 						asconf = 1;
5818 					}
5819 					chk->sent = SCTP_DATAGRAM_SENT;
5820 					chk->snd_count++;
5821 				}
5822 				if (mtu == 0) {
5823 					/*
5824 					 * Ok we are out of room but we can
5825 					 * output without effecting the
5826 					 * flight size since this little guy
5827 					 * is a control only packet.
5828 					 */
5829 					if (asconf) {
5830 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
5831 						asconf = 0;
5832 					}
5833 					if (cookie) {
5834 						sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
5835 						cookie = 0;
5836 					}
5837 					M_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
5838 					if (outchain == NULL) {
5839 						/* no memory */
5840 						error = ENOBUFS;
5841 						goto error_out_again;
5842 					}
5843 					shdr = mtod(outchain, struct sctphdr *);
5844 					shdr->src_port = inp->sctp_lport;
5845 					shdr->dest_port = stcb->rport;
5846 					shdr->v_tag = htonl(stcb->asoc.peer_vtag);
5847 					shdr->checksum = 0;
5848 					auth_offset += sizeof(struct sctphdr);
5849 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
5850 					    (struct sockaddr *)&net->ro._l_addr,
5851 					    outchain, auth_offset, auth,
5852 					    no_fragmentflg, 0, NULL, asconf))) {
5853 						if (error == ENOBUFS) {
5854 							asoc->ifp_had_enobuf = 1;
5855 						}
5856 						SCTP_STAT_INCR(sctps_lowlevelerr);
5857 						if (from_where == 0) {
5858 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
5859 						}
5860 				error_out_again:
5861 						/* error, could not output */
5862 						if (hbflag) {
5863 							if (*now_filled == 0) {
5864 								SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5865 								*now_filled = 1;
5866 								*now = net->last_sent_time;
5867 							} else {
5868 								net->last_sent_time = *now;
5869 							}
5870 							hbflag = 0;
5871 						}
5872 						if (error == EHOSTUNREACH) {
5873 							/*
5874 							 * Destination went
5875 							 * unreachable
5876 							 * during this send
5877 							 */
5878 							sctp_move_to_an_alt(stcb, asoc, net);
5879 						}
5880 						sctp_clean_up_ctl(stcb, asoc);
5881 						*reason_code = 7;
5882 						return (error);
5883 					} else
5884 						asoc->ifp_had_enobuf = 0;
5885 					/* Only HB or ASCONF advances time */
5886 					if (hbflag) {
5887 						if (*now_filled == 0) {
5888 							SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5889 							*now_filled = 1;
5890 							*now = net->last_sent_time;
5891 						} else {
5892 							net->last_sent_time = *now;
5893 						}
5894 						hbflag = 0;
5895 					}
5896 					/*
5897 					 * increase the number we sent, if a
5898 					 * cookie is sent we don't tell them
5899 					 * any was sent out.
5900 					 */
5901 					outchain = endoutchain = NULL;
5902 					auth = NULL;
5903 					auth_offset = 0;
5904 					if (!no_out_cnt)
5905 						*num_out += ctl_cnt;
5906 					/* recalc a clean slate and setup */
5907 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5908 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
5909 					} else {
5910 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
5911 					}
5912 					to_out = 0;
5913 					no_fragmentflg = 1;
5914 				}
5915 			}
5916 		}
5917 		/*********************/
5918 		/* Data transmission */
5919 		/*********************/
5920 		/*
5921 		 * if AUTH for DATA is required and no AUTH has been added
5922 		 * yet, account for this in the mtu now... if no data can be
5923 		 * bundled, this adjustment won't matter anyways since the
5924 		 * packet will be going out...
5925 		 */
5926 		if ((auth == NULL) &&
5927 		    sctp_auth_is_required_chunk(SCTP_DATA,
5928 		    stcb->asoc.peer_auth_chunks)) {
5929 			mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5930 		}
5931 		/* now lets add any data within the MTU constraints */
5932 		if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
5933 			if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
5934 				omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
5935 			else
5936 				omtu = 0;
5937 		} else {
5938 			if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
5939 				omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
5940 			else
5941 				omtu = 0;
5942 		}
5943 		if (((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) ||
5944 		    (cookie)) {
5945 			for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
5946 				if (no_data_chunks) {
5947 					/* let only control go out */
5948 					*reason_code = 1;
5949 					break;
5950 				}
5951 				if (net->flight_size >= net->cwnd) {
5952 					/* skip this net, no room for data */
5953 					*reason_code = 2;
5954 					break;
5955 				}
5956 				nchk = TAILQ_NEXT(chk, sctp_next);
5957 				if (chk->whoTo != net) {
5958 					/* No, not sent to this net */
5959 					continue;
5960 				}
5961 				if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
5962 					/*
5963 					 * strange, we have a chunk that is
5964 					 * to bit for its destination and
5965 					 * yet no fragment ok flag.
5966 					 * Something went wrong when the
5967 					 * PMTU changed...we did not mark
5968 					 * this chunk for some reason?? I
5969 					 * will fix it here by letting IP
5970 					 * fragment it for now and printing
5971 					 * a warning. This really should not
5972 					 * happen ...
5973 					 */
5974 #ifdef SCTP_DEBUG
5975 					printf("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
5976 					    chk->send_size, mtu);
5977 #endif
5978 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
5979 				}
5980 				if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
5981 				    ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
5982 					/* ok we will add this one */
5983 
5984 					/*
5985 					 * Add an AUTH chunk, if chunk
5986 					 * requires it, save the offset into
5987 					 * the chain for AUTH
5988 					 */
5989 					if ((auth == NULL) &&
5990 					    (sctp_auth_is_required_chunk(SCTP_DATA,
5991 					    stcb->asoc.peer_auth_chunks))) {
5992 
5993 						outchain = sctp_add_auth_chunk(outchain,
5994 						    &endoutchain,
5995 						    &auth,
5996 						    &auth_offset,
5997 						    stcb,
5998 						    SCTP_DATA);
5999 						SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
6000 					}
6001 					outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
6002 					    chk->send_size, chk->copy_by_ref);
6003 					if (outchain == NULL) {
6004 #ifdef SCTP_DEBUG
6005 						if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6006 							printf("No memory?\n");
6007 						}
6008 #endif
6009 						if (!callout_pending(&net->rxt_timer.timer)) {
6010 							sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6011 						}
6012 						*reason_code = 3;
6013 						return (ENOMEM);
6014 					}
6015 					/* upate our MTU size */
6016 					/* Do clear IP_DF ? */
6017 					if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
6018 						no_fragmentflg = 0;
6019 					}
6020 					/* unsigned subtraction of mtu */
6021 					if (mtu > chk->send_size)
6022 						mtu -= chk->send_size;
6023 					else
6024 						mtu = 0;
6025 					/* unsigned subtraction of r_mtu */
6026 					if (r_mtu > chk->send_size)
6027 						r_mtu -= chk->send_size;
6028 					else
6029 						r_mtu = 0;
6030 
6031 					to_out += chk->send_size;
6032 					if (to_out > mx_mtu) {
6033 #ifdef INVARIANTS
6034 						panic("gag");
6035 #else
6036 						printf("Exceeding mtu of %d out size is %d\n",
6037 						    mx_mtu, to_out);
6038 #endif
6039 					}
6040 					data_list[bundle_at++] = chk;
6041 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
6042 						mtu = 0;
6043 						break;
6044 					}
6045 					if (chk->sent == SCTP_DATAGRAM_UNSENT) {
6046 						if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
6047 							SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
6048 						} else {
6049 							SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
6050 						}
6051 						if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
6052 						    ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
6053 							SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
6054 					}
6055 					if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
6056 						break;
6057 					}
6058 				} else {
6059 					/*
6060 					 * Must be sent in order of the
6061 					 * TSN's (on a network)
6062 					 */
6063 					break;
6064 				}
6065 			}	/* for () */
6066 		}		/* if asoc.state OPEN */
6067 		/* Is there something to send for this destination? */
6068 		if (outchain) {
6069 			/* We may need to start a control timer or two */
6070 			if (asconf) {
6071 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
6072 				asconf = 0;
6073 			}
6074 			if (cookie) {
6075 				sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
6076 				cookie = 0;
6077 			}
6078 			/* must start a send timer if data is being sent */
6079 			if (bundle_at && (!callout_pending(&net->rxt_timer.timer))) {
6080 				/*
6081 				 * no timer running on this destination
6082 				 * restart it.
6083 				 */
6084 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6085 			}
6086 			/* Now send it, if there is anything to send :> */
6087 			M_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
6088 			if (outchain == NULL) {
6089 				/* out of mbufs */
6090 				error = ENOBUFS;
6091 				goto errored_send;
6092 			}
6093 			shdr = mtod(outchain, struct sctphdr *);
6094 			shdr->src_port = inp->sctp_lport;
6095 			shdr->dest_port = stcb->rport;
6096 			shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6097 			shdr->checksum = 0;
6098 			auth_offset += sizeof(struct sctphdr);
6099 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
6100 			    (struct sockaddr *)&net->ro._l_addr,
6101 			    outchain,
6102 			    auth_offset,
6103 			    auth,
6104 			    no_fragmentflg,
6105 			    bundle_at,
6106 			    data_list[0],
6107 			    asconf))) {
6108 				/* error, we could not output */
6109 				if (error == ENOBUFS) {
6110 					asoc->ifp_had_enobuf = 1;
6111 				}
6112 				SCTP_STAT_INCR(sctps_lowlevelerr);
6113 				if (from_where == 0) {
6114 					SCTP_STAT_INCR(sctps_lowlevelerrusr);
6115 				}
6116 		errored_send:
6117 #ifdef SCTP_DEBUG
6118 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6119 					printf("Gak send error %d\n", error);
6120 				}
6121 #endif
6122 				if (hbflag) {
6123 					if (*now_filled == 0) {
6124 						SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
6125 						*now_filled = 1;
6126 						*now = net->last_sent_time;
6127 					} else {
6128 						net->last_sent_time = *now;
6129 					}
6130 					hbflag = 0;
6131 				}
6132 				if (error == EHOSTUNREACH) {
6133 					/*
6134 					 * Destination went unreachable
6135 					 * during this send
6136 					 */
6137 					sctp_move_to_an_alt(stcb, asoc, net);
6138 				}
6139 				sctp_clean_up_ctl(stcb, asoc);
6140 				*reason_code = 6;
6141 				return (error);
6142 			} else {
6143 				asoc->ifp_had_enobuf = 0;
6144 			}
6145 			outchain = endoutchain = NULL;
6146 			auth = NULL;
6147 			auth_offset = 0;
6148 			if (bundle_at || hbflag) {
6149 				/* For data/asconf and hb set time */
6150 				if (*now_filled == 0) {
6151 					SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
6152 					*now_filled = 1;
6153 					*now = net->last_sent_time;
6154 				} else {
6155 					net->last_sent_time = *now;
6156 				}
6157 			}
6158 			if (!no_out_cnt) {
6159 				*num_out += (ctl_cnt + bundle_at);
6160 			}
6161 			if (bundle_at) {
6162 				/* if (!net->rto_pending) { */
6163 				/* setup for a RTO measurement */
6164 				/* net->rto_pending = 1; */
6165 				tsns_sent = data_list[0]->rec.data.TSN_seq;
6166 
6167 				data_list[0]->do_rtt = 1;
6168 				/* } else { */
6169 				/* data_list[0]->do_rtt = 0; */
6170 				/* } */
6171 				SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
6172 				sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
6173 				if (sctp_early_fr) {
6174 					if (net->flight_size < net->cwnd) {
6175 						/* start or restart it */
6176 						if (callout_pending(&net->fr_timer.timer)) {
6177 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
6178 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
6179 						}
6180 						SCTP_STAT_INCR(sctps_earlyfrstrout);
6181 						sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
6182 					} else {
6183 						/* stop it if its running */
6184 						if (callout_pending(&net->fr_timer.timer)) {
6185 							SCTP_STAT_INCR(sctps_earlyfrstpout);
6186 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
6187 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
6188 						}
6189 					}
6190 				}
6191 			}
6192 			if (one_chunk) {
6193 				break;
6194 			}
6195 		}
6196 #ifdef SCTP_CWND_LOGGING
6197 		sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
6198 #endif
6199 	}
6200 	if (old_startat == NULL) {
6201 		old_startat = send_start_at;
6202 		send_start_at = TAILQ_FIRST(&asoc->nets);
6203 		goto again_one_more_time;
6204 	}
6205 	/*
6206 	 * At the end there should be no NON timed chunks hanging on this
6207 	 * queue.
6208 	 */
6209 #ifdef SCTP_CWND_LOGGING
6210 	sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
6211 #endif
6212 	if ((*num_out == 0) && (*reason_code == 0)) {
6213 		*reason_code = 4;
6214 	} else {
6215 		*reason_code = 5;
6216 	}
6217 	sctp_clean_up_ctl(stcb, asoc);
6218 	return (0);
6219 }
6220 
6221 void
6222 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
6223 {
6224 	/*
6225 	 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
6226 	 * the control chunk queue.
6227 	 */
6228 	struct sctp_chunkhdr *hdr;
6229 	struct sctp_tmit_chunk *chk;
6230 	struct mbuf *mat;
6231 
6232 	SCTP_TCB_LOCK_ASSERT(stcb);
6233 	sctp_alloc_a_chunk(stcb, chk);
6234 	if (chk == NULL) {
6235 		/* no memory */
6236 		sctp_m_freem(op_err);
6237 		return;
6238 	}
6239 	chk->copy_by_ref = 0;
6240 	M_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
6241 	if (op_err == NULL) {
6242 		sctp_free_a_chunk(stcb, chk);
6243 		return;
6244 	}
6245 	chk->send_size = 0;
6246 	mat = op_err;
6247 	while (mat != NULL) {
6248 		chk->send_size += mat->m_len;
6249 		mat = mat->m_next;
6250 	}
6251 	chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
6252 	chk->rec.chunk_id.can_take_data = 1;
6253 	chk->sent = SCTP_DATAGRAM_UNSENT;
6254 	chk->snd_count = 0;
6255 	chk->flags = 0;
6256 	chk->asoc = &stcb->asoc;
6257 	chk->data = op_err;
6258 	chk->whoTo = chk->asoc->primary_destination;
6259 	atomic_add_int(&chk->whoTo->ref_count, 1);
6260 	hdr = mtod(op_err, struct sctp_chunkhdr *);
6261 	hdr->chunk_type = SCTP_OPERATION_ERROR;
6262 	hdr->chunk_flags = 0;
6263 	hdr->chunk_length = htons(chk->send_size);
6264 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
6265 	    chk,
6266 	    sctp_next);
6267 	chk->asoc->ctrl_queue_cnt++;
6268 }
6269 
6270 int
6271 sctp_send_cookie_echo(struct mbuf *m,
6272     int offset,
6273     struct sctp_tcb *stcb,
6274     struct sctp_nets *net)
6275 {
6276 	/*
6277 	 * pull out the cookie and put it at the front of the control chunk
6278 	 * queue.
6279 	 */
6280 	int at;
6281 	struct mbuf *cookie, *mat;
6282 	struct sctp_paramhdr parm, *phdr;
6283 	struct sctp_chunkhdr *hdr;
6284 	struct sctp_tmit_chunk *chk;
6285 	uint16_t ptype, plen;
6286 
6287 	/* First find the cookie in the param area */
6288 	cookie = NULL;
6289 	at = offset + sizeof(struct sctp_init_chunk);
6290 
6291 	SCTP_TCB_LOCK_ASSERT(stcb);
6292 	do {
6293 		phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
6294 		if (phdr == NULL) {
6295 			return (-3);
6296 		}
6297 		ptype = ntohs(phdr->param_type);
6298 		plen = ntohs(phdr->param_length);
6299 		if (ptype == SCTP_STATE_COOKIE) {
6300 			int pad;
6301 
6302 			/* found the cookie */
6303 			if ((pad = (plen % 4))) {
6304 				plen += 4 - pad;
6305 			}
6306 			cookie = sctp_m_copym(m, at, plen, M_DONTWAIT);
6307 			if (cookie == NULL) {
6308 				/* No memory */
6309 				return (-2);
6310 			}
6311 			break;
6312 		}
6313 		at += SCTP_SIZE32(plen);
6314 	} while (phdr);
6315 	if (cookie == NULL) {
6316 		/* Did not find the cookie */
6317 		return (-3);
6318 	}
6319 	/* ok, we got the cookie lets change it into a cookie echo chunk */
6320 
6321 	/* first the change from param to cookie */
6322 	hdr = mtod(cookie, struct sctp_chunkhdr *);
6323 	hdr->chunk_type = SCTP_COOKIE_ECHO;
6324 	hdr->chunk_flags = 0;
6325 	/* now we MUST have a PKTHDR on it */
6326 	if ((cookie->m_flags & M_PKTHDR) != M_PKTHDR) {
6327 		/* we hope this happens rarely */
6328 		mat = sctp_get_mbuf_for_msg(8, 1, M_DONTWAIT, 1, MT_HEADER);
6329 		if (mat == NULL) {
6330 			sctp_m_freem(cookie);
6331 			return (-4);
6332 		}
6333 		mat->m_len = 0;
6334 		mat->m_pkthdr.rcvif = 0;
6335 		mat->m_next = cookie;
6336 		cookie = mat;
6337 	}
6338 	cookie->m_pkthdr.len = plen;
6339 	/* get the chunk stuff now and place it in the FRONT of the queue */
6340 	sctp_alloc_a_chunk(stcb, chk);
6341 	if (chk == NULL) {
6342 		/* no memory */
6343 		sctp_m_freem(cookie);
6344 		return (-5);
6345 	}
6346 	chk->copy_by_ref = 0;
6347 	chk->send_size = cookie->m_pkthdr.len;
6348 	chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
6349 	chk->rec.chunk_id.can_take_data = 0;
6350 	chk->sent = SCTP_DATAGRAM_UNSENT;
6351 	chk->snd_count = 0;
6352 	chk->flags = 0;
6353 	chk->asoc = &stcb->asoc;
6354 	chk->data = cookie;
6355 	chk->whoTo = chk->asoc->primary_destination;
6356 	atomic_add_int(&chk->whoTo->ref_count, 1);
6357 	TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
6358 	chk->asoc->ctrl_queue_cnt++;
6359 	return (0);
6360 }
6361 
6362 void
6363 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
6364     struct mbuf *m,
6365     int offset,
6366     int chk_length,
6367     struct sctp_nets *net)
6368 {
6369 	/*
6370 	 * take a HB request and make it into a HB ack and send it.
6371 	 */
6372 	struct mbuf *outchain;
6373 	struct sctp_chunkhdr *chdr;
6374 	struct sctp_tmit_chunk *chk;
6375 
6376 
6377 	if (net == NULL)
6378 		/* must have a net pointer */
6379 		return;
6380 
6381 	outchain = sctp_m_copym(m, offset, chk_length, M_DONTWAIT);
6382 	if (outchain == NULL) {
6383 		/* gak out of memory */
6384 		return;
6385 	}
6386 	chdr = mtod(outchain, struct sctp_chunkhdr *);
6387 	chdr->chunk_type = SCTP_HEARTBEAT_ACK;
6388 	chdr->chunk_flags = 0;
6389 	if ((outchain->m_flags & M_PKTHDR) != M_PKTHDR) {
6390 		/* should not happen but we are cautious. */
6391 		struct mbuf *tmp;
6392 
6393 		tmp = sctp_get_mbuf_for_msg(1, 1, M_DONTWAIT, 1, MT_HEADER);
6394 		if (tmp == NULL) {
6395 			return;
6396 		}
6397 		tmp->m_len = 0;
6398 		tmp->m_pkthdr.rcvif = 0;
6399 		tmp->m_next = outchain;
6400 		outchain = tmp;
6401 	}
6402 	outchain->m_pkthdr.len = chk_length;
6403 	if (chk_length % 4) {
6404 		/* need pad */
6405 		uint32_t cpthis = 0;
6406 		int padlen;
6407 
6408 		padlen = 4 - (outchain->m_pkthdr.len % 4);
6409 		m_copyback(outchain, outchain->m_pkthdr.len, padlen,
6410 		    (caddr_t)&cpthis);
6411 	}
6412 	sctp_alloc_a_chunk(stcb, chk);
6413 	if (chk == NULL) {
6414 		/* no memory */
6415 		sctp_m_freem(outchain);
6416 		return;
6417 	}
6418 	chk->copy_by_ref = 0;
6419 	chk->send_size = chk_length;
6420 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
6421 	chk->rec.chunk_id.can_take_data = 1;
6422 	chk->sent = SCTP_DATAGRAM_UNSENT;
6423 	chk->snd_count = 0;
6424 	chk->flags = 0;
6425 	chk->asoc = &stcb->asoc;
6426 	chk->data = outchain;
6427 	chk->whoTo = net;
6428 	atomic_add_int(&chk->whoTo->ref_count, 1);
6429 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6430 	chk->asoc->ctrl_queue_cnt++;
6431 }
6432 
6433 int
6434 sctp_send_cookie_ack(struct sctp_tcb *stcb)
6435 {
6436 	/* formulate and queue a cookie-ack back to sender */
6437 	struct mbuf *cookie_ack;
6438 	struct sctp_chunkhdr *hdr;
6439 	struct sctp_tmit_chunk *chk;
6440 
6441 	cookie_ack = NULL;
6442 	SCTP_TCB_LOCK_ASSERT(stcb);
6443 
6444 	cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 1, M_DONTWAIT, 1, MT_HEADER);
6445 	if (cookie_ack == NULL) {
6446 		/* no mbuf's */
6447 		return (-1);
6448 	}
6449 	cookie_ack->m_data += SCTP_MIN_OVERHEAD;
6450 	sctp_alloc_a_chunk(stcb, chk);
6451 	if (chk == NULL) {
6452 		/* no memory */
6453 		sctp_m_freem(cookie_ack);
6454 		return (-1);
6455 	}
6456 	chk->copy_by_ref = 0;
6457 	chk->send_size = sizeof(struct sctp_chunkhdr);
6458 	chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
6459 	chk->rec.chunk_id.can_take_data = 1;
6460 	chk->sent = SCTP_DATAGRAM_UNSENT;
6461 	chk->snd_count = 0;
6462 	chk->flags = 0;
6463 	chk->asoc = &stcb->asoc;
6464 	chk->data = cookie_ack;
6465 	if (chk->asoc->last_control_chunk_from != NULL) {
6466 		chk->whoTo = chk->asoc->last_control_chunk_from;
6467 	} else {
6468 		chk->whoTo = chk->asoc->primary_destination;
6469 	}
6470 	atomic_add_int(&chk->whoTo->ref_count, 1);
6471 	hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
6472 	hdr->chunk_type = SCTP_COOKIE_ACK;
6473 	hdr->chunk_flags = 0;
6474 	hdr->chunk_length = htons(chk->send_size);
6475 	cookie_ack->m_pkthdr.len = cookie_ack->m_len = chk->send_size;
6476 	cookie_ack->m_pkthdr.rcvif = 0;
6477 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6478 	chk->asoc->ctrl_queue_cnt++;
6479 	return (0);
6480 }
6481 
6482 
6483 int
6484 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
6485 {
6486 	/* formulate and queue a SHUTDOWN-ACK back to the sender */
6487 	struct mbuf *m_shutdown_ack;
6488 	struct sctp_shutdown_ack_chunk *ack_cp;
6489 	struct sctp_tmit_chunk *chk;
6490 
6491 	m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 1, M_DONTWAIT, 1, MT_HEADER);
6492 	if (m_shutdown_ack == NULL) {
6493 		/* no mbuf's */
6494 		return (-1);
6495 	}
6496 	m_shutdown_ack->m_data += SCTP_MIN_OVERHEAD;
6497 	sctp_alloc_a_chunk(stcb, chk);
6498 	if (chk == NULL) {
6499 		/* no memory */
6500 		sctp_m_freem(m_shutdown_ack);
6501 		return (-1);
6502 	}
6503 	chk->copy_by_ref = 0;
6504 
6505 	chk->send_size = sizeof(struct sctp_chunkhdr);
6506 	chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
6507 	chk->rec.chunk_id.can_take_data = 1;
6508 	chk->sent = SCTP_DATAGRAM_UNSENT;
6509 	chk->snd_count = 0;
6510 	chk->flags = 0;
6511 	chk->asoc = &stcb->asoc;
6512 	chk->data = m_shutdown_ack;
6513 	chk->whoTo = net;
6514 	atomic_add_int(&net->ref_count, 1);
6515 
6516 	ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
6517 	ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
6518 	ack_cp->ch.chunk_flags = 0;
6519 	ack_cp->ch.chunk_length = htons(chk->send_size);
6520 	m_shutdown_ack->m_pkthdr.len = m_shutdown_ack->m_len = chk->send_size;
6521 	m_shutdown_ack->m_pkthdr.rcvif = 0;
6522 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6523 	chk->asoc->ctrl_queue_cnt++;
6524 	return (0);
6525 }
6526 
6527 int
6528 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
6529 {
6530 	/* formulate and queue a SHUTDOWN to the sender */
6531 	struct mbuf *m_shutdown;
6532 	struct sctp_shutdown_chunk *shutdown_cp;
6533 	struct sctp_tmit_chunk *chk;
6534 
6535 	m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 1, M_DONTWAIT, 1, MT_HEADER);
6536 	if (m_shutdown == NULL) {
6537 		/* no mbuf's */
6538 		return (-1);
6539 	}
6540 	m_shutdown->m_data += SCTP_MIN_OVERHEAD;
6541 	sctp_alloc_a_chunk(stcb, chk);
6542 	if (chk == NULL) {
6543 		/* no memory */
6544 		sctp_m_freem(m_shutdown);
6545 		return (-1);
6546 	}
6547 	chk->copy_by_ref = 0;
6548 	chk->send_size = sizeof(struct sctp_shutdown_chunk);
6549 	chk->rec.chunk_id.id = SCTP_SHUTDOWN;
6550 	chk->rec.chunk_id.can_take_data = 1;
6551 	chk->sent = SCTP_DATAGRAM_UNSENT;
6552 	chk->snd_count = 0;
6553 	chk->flags = 0;
6554 	chk->asoc = &stcb->asoc;
6555 	chk->data = m_shutdown;
6556 	chk->whoTo = net;
6557 	atomic_add_int(&net->ref_count, 1);
6558 
6559 	shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
6560 	shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
6561 	shutdown_cp->ch.chunk_flags = 0;
6562 	shutdown_cp->ch.chunk_length = htons(chk->send_size);
6563 	shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
6564 	m_shutdown->m_pkthdr.len = m_shutdown->m_len = chk->send_size;
6565 	m_shutdown->m_pkthdr.rcvif = 0;
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(struct sctp_tcb *stcb, struct sctp_nets *net)
6573 {
6574 	/*
6575 	 * formulate and queue an ASCONF to the peer ASCONF parameters
6576 	 * should be queued on the assoc queue
6577 	 */
6578 	struct sctp_tmit_chunk *chk;
6579 	struct mbuf *m_asconf;
6580 	struct sctp_asconf_chunk *acp;
6581 
6582 
6583 	SCTP_TCB_LOCK_ASSERT(stcb);
6584 	/* compose an ASCONF chunk, maximum length is PMTU */
6585 	m_asconf = sctp_compose_asconf(stcb);
6586 	if (m_asconf == NULL) {
6587 		return (-1);
6588 	}
6589 	acp = mtod(m_asconf, struct sctp_asconf_chunk *);
6590 	sctp_alloc_a_chunk(stcb, chk);
6591 	if (chk == NULL) {
6592 		/* no memory */
6593 		sctp_m_freem(m_asconf);
6594 		return (-1);
6595 	}
6596 	chk->copy_by_ref = 0;
6597 	chk->data = m_asconf;
6598 	chk->send_size = m_asconf->m_pkthdr.len;
6599 	chk->rec.chunk_id.id = SCTP_ASCONF;
6600 	chk->rec.chunk_id.can_take_data = 0;
6601 	chk->sent = SCTP_DATAGRAM_UNSENT;
6602 	chk->snd_count = 0;
6603 	chk->flags = 0;
6604 	chk->asoc = &stcb->asoc;
6605 	chk->whoTo = chk->asoc->primary_destination;
6606 	atomic_add_int(&chk->whoTo->ref_count, 1);
6607 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6608 	chk->asoc->ctrl_queue_cnt++;
6609 	return (0);
6610 }
6611 
6612 int
6613 sctp_send_asconf_ack(struct sctp_tcb *stcb, uint32_t retrans)
6614 {
6615 	/*
6616 	 * formulate and queue a asconf-ack back to sender the asconf-ack
6617 	 * must be stored in the tcb
6618 	 */
6619 	struct sctp_tmit_chunk *chk;
6620 	struct mbuf *m_ack;
6621 
6622 	SCTP_TCB_LOCK_ASSERT(stcb);
6623 	/* is there a asconf-ack mbuf chain to send? */
6624 	if (stcb->asoc.last_asconf_ack_sent == NULL) {
6625 		return (-1);
6626 	}
6627 	/* copy the asconf_ack */
6628 	/*
6629 	 * Supposedly the m_copypacket is a optimzation, use it if we can.
6630 	 */
6631 	if (stcb->asoc.last_asconf_ack_sent->m_flags & M_PKTHDR) {
6632 		m_ack = m_copypacket(stcb->asoc.last_asconf_ack_sent, M_DONTWAIT);
6633 	} else
6634 		m_ack = m_copy(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL);
6635 
6636 	if (m_ack == NULL) {
6637 		/* couldn't copy it */
6638 
6639 		return (-1);
6640 	}
6641 	sctp_alloc_a_chunk(stcb, chk);
6642 	if (chk == NULL) {
6643 		/* no memory */
6644 		if (m_ack)
6645 			sctp_m_freem(m_ack);
6646 		return (-1);
6647 	}
6648 	chk->copy_by_ref = 0;
6649 	/* figure out where it goes to */
6650 	if (retrans) {
6651 		/* we're doing a retransmission */
6652 		if (stcb->asoc.used_alt_asconfack > 2) {
6653 			/* tried alternate nets already, go back */
6654 			chk->whoTo = NULL;
6655 		} else {
6656 			/* need to try and alternate net */
6657 			chk->whoTo = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
6658 			stcb->asoc.used_alt_asconfack++;
6659 		}
6660 		if (chk->whoTo == NULL) {
6661 			/* no alternate */
6662 			if (stcb->asoc.last_control_chunk_from == NULL)
6663 				chk->whoTo = stcb->asoc.primary_destination;
6664 			else
6665 				chk->whoTo = stcb->asoc.last_control_chunk_from;
6666 			stcb->asoc.used_alt_asconfack = 0;
6667 		}
6668 	} else {
6669 		/* normal case */
6670 		if (stcb->asoc.last_control_chunk_from == NULL)
6671 			chk->whoTo = stcb->asoc.primary_destination;
6672 		else
6673 			chk->whoTo = stcb->asoc.last_control_chunk_from;
6674 		stcb->asoc.used_alt_asconfack = 0;
6675 	}
6676 	chk->data = m_ack;
6677 	chk->send_size = m_ack->m_pkthdr.len;
6678 	chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
6679 	chk->rec.chunk_id.can_take_data = 1;
6680 	chk->sent = SCTP_DATAGRAM_UNSENT;
6681 	chk->snd_count = 0;
6682 	chk->flags = 0;
6683 	chk->asoc = &stcb->asoc;
6684 	atomic_add_int(&chk->whoTo->ref_count, 1);
6685 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6686 	chk->asoc->ctrl_queue_cnt++;
6687 	return (0);
6688 }
6689 
6690 
6691 static int
6692 sctp_chunk_retransmission(struct sctp_inpcb *inp,
6693     struct sctp_tcb *stcb,
6694     struct sctp_association *asoc,
6695     int *cnt_out, struct timeval *now, int *now_filled)
6696 {
6697 	/*
6698 	 * send out one MTU of retransmission. If fast_retransmit is
6699 	 * happening we ignore the cwnd. Otherwise we obey the cwnd and
6700 	 * rwnd. For a Cookie or Asconf in the control chunk queue we
6701 	 * retransmit them by themselves.
6702 	 *
6703 	 * For data chunks we will pick out the lowest TSN's in the sent_queue
6704 	 * marked for resend and bundle them all together (up to a MTU of
6705 	 * destination). The address to send to should have been
6706 	 * selected/changed where the retransmission was marked (i.e. in FR
6707 	 * or t3-timeout routines).
6708 	 */
6709 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
6710 	struct sctp_tmit_chunk *chk, *fwd;
6711 	struct mbuf *m, *endofchain;
6712 	struct sctphdr *shdr;
6713 	int asconf;
6714 	struct sctp_nets *net;
6715 	uint32_t tsns_sent = 0;
6716 	int no_fragmentflg, bundle_at, cnt_thru;
6717 	unsigned int mtu;
6718 	int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
6719 	struct sctp_auth_chunk *auth = NULL;
6720 	uint32_t auth_offset = 0;
6721 	uint32_t dmtu = 0;
6722 
6723 	SCTP_TCB_LOCK_ASSERT(stcb);
6724 	tmr_started = ctl_cnt = bundle_at = error = 0;
6725 	no_fragmentflg = 1;
6726 	asconf = 0;
6727 	fwd_tsn = 0;
6728 	*cnt_out = 0;
6729 	fwd = NULL;
6730 	endofchain = m = NULL;
6731 #ifdef SCTP_AUDITING_ENABLED
6732 	sctp_audit_log(0xC3, 1);
6733 #endif
6734 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
6735 #ifdef SCTP_DEBUG
6736 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6737 			printf("SCTP hits empty queue with cnt set to %d?\n",
6738 			    asoc->sent_queue_retran_cnt);
6739 		}
6740 #endif
6741 		asoc->sent_queue_cnt = 0;
6742 		asoc->sent_queue_cnt_removeable = 0;
6743 	}
6744 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
6745 		if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
6746 		    (chk->rec.chunk_id.id == SCTP_ASCONF) ||
6747 		    (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
6748 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
6749 			if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6750 				if (chk != asoc->str_reset) {
6751 					/*
6752 					 * not eligible for retran if its
6753 					 * not ours
6754 					 */
6755 					continue;
6756 				}
6757 			}
6758 			ctl_cnt++;
6759 			if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6760 				no_fragmentflg = 1;
6761 				asconf = 1;
6762 			}
6763 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
6764 				fwd_tsn = 1;
6765 				fwd = chk;
6766 			}
6767 			/*
6768 			 * Add an AUTH chunk, if chunk requires it save the
6769 			 * offset into the chain for AUTH
6770 			 */
6771 			if ((auth == NULL) &&
6772 			    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
6773 			    stcb->asoc.peer_auth_chunks))) {
6774 				m = sctp_add_auth_chunk(m, &endofchain,
6775 				    &auth, &auth_offset,
6776 				    stcb,
6777 				    chk->rec.chunk_id.id);
6778 			}
6779 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
6780 			break;
6781 		}
6782 	}
6783 	one_chunk = 0;
6784 	cnt_thru = 0;
6785 	/* do we have control chunks to retransmit? */
6786 	if (m != NULL) {
6787 		/* Start a timer no matter if we suceed or fail */
6788 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6789 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
6790 		} else if (chk->rec.chunk_id.id == SCTP_ASCONF)
6791 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
6792 
6793 		M_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
6794 		if (m == NULL) {
6795 			return (ENOBUFS);
6796 		}
6797 		shdr = mtod(m, struct sctphdr *);
6798 		shdr->src_port = inp->sctp_lport;
6799 		shdr->dest_port = stcb->rport;
6800 		shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6801 		shdr->checksum = 0;
6802 		auth_offset += sizeof(struct sctphdr);
6803 		chk->snd_count++;	/* update our count */
6804 
6805 		if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
6806 		    (struct sockaddr *)&chk->whoTo->ro._l_addr, m, auth_offset,
6807 		    auth, no_fragmentflg, 0, NULL, asconf))) {
6808 			SCTP_STAT_INCR(sctps_lowlevelerr);
6809 			return (error);
6810 		}
6811 		m = endofchain = NULL;
6812 		auth = NULL;
6813 		auth_offset = 0;
6814 		/*
6815 		 * We don't want to mark the net->sent time here since this
6816 		 * we use this for HB and retrans cannot measure RTT
6817 		 */
6818 		/* SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
6819 		*cnt_out += 1;
6820 		chk->sent = SCTP_DATAGRAM_SENT;
6821 		sctp_ucount_decr(asoc->sent_queue_retran_cnt);
6822 		if (fwd_tsn == 0) {
6823 			return (0);
6824 		} else {
6825 			/* Clean up the fwd-tsn list */
6826 			sctp_clean_up_ctl(stcb, asoc);
6827 			return (0);
6828 		}
6829 	}
6830 	/*
6831 	 * Ok, it is just data retransmission we need to do or that and a
6832 	 * fwd-tsn with it all.
6833 	 */
6834 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
6835 		return (-1);
6836 	}
6837 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
6838 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
6839 		/* not yet open, resend the cookie and that is it */
6840 		return (1);
6841 	}
6842 #ifdef SCTP_AUDITING_ENABLED
6843 	sctp_auditing(20, inp, stcb, NULL);
6844 #endif
6845 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6846 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
6847 			/* No, not sent to this net or not ready for rtx */
6848 			continue;
6849 
6850 		}
6851 		/* pick up the net */
6852 		net = chk->whoTo;
6853 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6854 			mtu = (net->mtu - SCTP_MIN_OVERHEAD);
6855 		} else {
6856 			mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
6857 		}
6858 
6859 		if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
6860 			/* No room in peers rwnd */
6861 			uint32_t tsn;
6862 
6863 			tsn = asoc->last_acked_seq + 1;
6864 			if (tsn == chk->rec.data.TSN_seq) {
6865 				/*
6866 				 * we make a special exception for this
6867 				 * case. The peer has no rwnd but is missing
6868 				 * the lowest chunk.. which is probably what
6869 				 * is holding up the rwnd.
6870 				 */
6871 				goto one_chunk_around;
6872 			}
6873 			return (1);
6874 		}
6875 one_chunk_around:
6876 		if (asoc->peers_rwnd < mtu) {
6877 			one_chunk = 1;
6878 		}
6879 #ifdef SCTP_AUDITING_ENABLED
6880 		sctp_audit_log(0xC3, 2);
6881 #endif
6882 		bundle_at = 0;
6883 		m = NULL;
6884 		net->fast_retran_ip = 0;
6885 		if (chk->rec.data.doing_fast_retransmit == 0) {
6886 			/*
6887 			 * if no FR in progress skip destination that have
6888 			 * flight_size > cwnd.
6889 			 */
6890 			if (net->flight_size >= net->cwnd) {
6891 				continue;
6892 			}
6893 		} else {
6894 			/*
6895 			 * Mark the destination net to have FR recovery
6896 			 * limits put on it.
6897 			 */
6898 			net->fast_retran_ip = 1;
6899 		}
6900 
6901 		/*
6902 		 * if no AUTH is yet included and this chunk requires it,
6903 		 * make sure to account for it.  We don't apply the size
6904 		 * until the AUTH chunk is actually added below in case
6905 		 * there is no room for this chunk.
6906 		 */
6907 		if ((auth == NULL) &&
6908 		    sctp_auth_is_required_chunk(SCTP_DATA,
6909 		    stcb->asoc.peer_auth_chunks)) {
6910 			dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
6911 		} else
6912 			dmtu = 0;
6913 
6914 		if ((chk->send_size <= (mtu - dmtu)) ||
6915 		    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
6916 			/* ok we will add this one */
6917 			if ((auth == NULL) &&
6918 			    (sctp_auth_is_required_chunk(SCTP_DATA,
6919 			    stcb->asoc.peer_auth_chunks))) {
6920 				m = sctp_add_auth_chunk(m, &endofchain,
6921 				    &auth, &auth_offset,
6922 				    stcb, SCTP_DATA);
6923 			}
6924 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
6925 			if (m == NULL) {
6926 				return (ENOMEM);
6927 			}
6928 			/* Do clear IP_DF ? */
6929 			if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
6930 				no_fragmentflg = 0;
6931 			}
6932 			/* upate our MTU size */
6933 			if (mtu > (chk->send_size + dmtu))
6934 				mtu -= (chk->send_size + dmtu);
6935 			else
6936 				mtu = 0;
6937 			data_list[bundle_at++] = chk;
6938 			if (one_chunk && (asoc->total_flight <= 0)) {
6939 				SCTP_STAT_INCR(sctps_windowprobed);
6940 				chk->rec.data.state_flags |= SCTP_WINDOW_PROBE;
6941 			}
6942 		}
6943 		if (one_chunk == 0) {
6944 			/*
6945 			 * now are there anymore forward from chk to pick
6946 			 * up?
6947 			 */
6948 			fwd = TAILQ_NEXT(chk, sctp_next);
6949 			while (fwd) {
6950 				if (fwd->sent != SCTP_DATAGRAM_RESEND) {
6951 					/* Nope, not for retran */
6952 					fwd = TAILQ_NEXT(fwd, sctp_next);
6953 					continue;
6954 				}
6955 				if (fwd->whoTo != net) {
6956 					/* Nope, not the net in question */
6957 					fwd = TAILQ_NEXT(fwd, sctp_next);
6958 					continue;
6959 				}
6960 				if ((auth == NULL) &&
6961 				    sctp_auth_is_required_chunk(SCTP_DATA,
6962 				    stcb->asoc.peer_auth_chunks)) {
6963 					dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
6964 				} else
6965 					dmtu = 0;
6966 				if (fwd->send_size <= (mtu - dmtu)) {
6967 					if ((auth == NULL) &&
6968 					    (sctp_auth_is_required_chunk(SCTP_DATA,
6969 					    stcb->asoc.peer_auth_chunks))) {
6970 						m = sctp_add_auth_chunk(m,
6971 						    &endofchain,
6972 						    &auth, &auth_offset,
6973 						    stcb,
6974 						    SCTP_DATA);
6975 					}
6976 					m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
6977 					if (m == NULL) {
6978 						return (ENOMEM);
6979 					}
6980 					/* Do clear IP_DF ? */
6981 					if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
6982 						no_fragmentflg = 0;
6983 					}
6984 					/* upate our MTU size */
6985 					if (mtu > (fwd->send_size + dmtu))
6986 						mtu -= (fwd->send_size + dmtu);
6987 					else
6988 						mtu = 0;
6989 					data_list[bundle_at++] = fwd;
6990 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
6991 						break;
6992 					}
6993 					fwd = TAILQ_NEXT(fwd, sctp_next);
6994 				} else {
6995 					/* can't fit so we are done */
6996 					break;
6997 				}
6998 			}
6999 		}
7000 		/* Is there something to send for this destination? */
7001 		if (m) {
7002 			/*
7003 			 * No matter if we fail/or suceed we should start a
7004 			 * timer. A failure is like a lost IP packet :-)
7005 			 */
7006 			if (!callout_pending(&net->rxt_timer.timer)) {
7007 				/*
7008 				 * no timer running on this destination
7009 				 * restart it.
7010 				 */
7011 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
7012 				tmr_started = 1;
7013 			}
7014 			M_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
7015 			if (m == NULL) {
7016 				return (ENOBUFS);
7017 			}
7018 			shdr = mtod(m, struct sctphdr *);
7019 			shdr->src_port = inp->sctp_lport;
7020 			shdr->dest_port = stcb->rport;
7021 			shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7022 			shdr->checksum = 0;
7023 			auth_offset += sizeof(struct sctphdr);
7024 			/* Now lets send it, if there is anything to send :> */
7025 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7026 			    (struct sockaddr *)&net->ro._l_addr, m, auth_offset,
7027 			    auth, no_fragmentflg, 0, NULL, asconf))) {
7028 				/* error, we could not output */
7029 				SCTP_STAT_INCR(sctps_lowlevelerr);
7030 				return (error);
7031 			}
7032 			m = endofchain = NULL;
7033 			auth = NULL;
7034 			auth_offset = 0;
7035 			/* For HB's */
7036 			/*
7037 			 * We don't want to mark the net->sent time here
7038 			 * since this we use this for HB and retrans cannot
7039 			 * measure RTT
7040 			 */
7041 			/* SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
7042 
7043 			/* For auto-close */
7044 			cnt_thru++;
7045 			if (*now_filled == 0) {
7046 				SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
7047 				*now = asoc->time_last_sent;
7048 				*now_filled = 1;
7049 			} else {
7050 				asoc->time_last_sent = *now;
7051 			}
7052 			*cnt_out += bundle_at;
7053 #ifdef SCTP_AUDITING_ENABLED
7054 			sctp_audit_log(0xC4, bundle_at);
7055 #endif
7056 			if (bundle_at) {
7057 				tsns_sent = data_list[0]->rec.data.TSN_seq;
7058 			}
7059 			for (i = 0; i < bundle_at; i++) {
7060 				SCTP_STAT_INCR(sctps_sendretransdata);
7061 				data_list[i]->sent = SCTP_DATAGRAM_SENT;
7062 				/*
7063 				 * When we have a revoked data, and we
7064 				 * retransmit it, then we clear the revoked
7065 				 * flag since this flag dictates if we
7066 				 * subtracted from the fs
7067 				 */
7068 				data_list[i]->rec.data.chunk_was_revoked = 0;
7069 				data_list[i]->snd_count++;
7070 				sctp_ucount_decr(asoc->sent_queue_retran_cnt);
7071 				/* record the time */
7072 				data_list[i]->sent_rcv_time = asoc->time_last_sent;
7073 				if (asoc->sent_queue_retran_cnt < 0) {
7074 					asoc->sent_queue_retran_cnt = 0;
7075 				}
7076 #ifdef SCTP_FLIGHT_LOGGING
7077 				sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
7078 				    data_list[i]->whoTo->flight_size,
7079 				    data_list[i]->book_size,
7080 				    (uintptr_t) stcb,
7081 				    data_list[i]->rec.data.TSN_seq);
7082 #endif
7083 				net->flight_size += data_list[i]->book_size;
7084 				asoc->total_flight += data_list[i]->book_size;
7085 				if (data_list[i]->book_size_scale) {
7086 					/*
7087 					 * need to double the book size on
7088 					 * this one
7089 					 */
7090 					data_list[i]->book_size_scale = 0;
7091 					data_list[i]->book_size *= 2;
7092 				} else {
7093 					sctp_ucount_incr(asoc->total_flight_count);
7094 #ifdef SCTP_LOG_RWND
7095 					sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
7096 					    asoc->peers_rwnd, data_list[i]->send_size, sctp_peer_chunk_oh);
7097 #endif
7098 					asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
7099 					    (uint32_t) (data_list[i]->send_size +
7100 					    sctp_peer_chunk_oh));
7101 				}
7102 				if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7103 					/* SWS sender side engages */
7104 					asoc->peers_rwnd = 0;
7105 				}
7106 				if ((i == 0) &&
7107 				    (data_list[i]->rec.data.doing_fast_retransmit)) {
7108 					SCTP_STAT_INCR(sctps_sendfastretrans);
7109 					if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
7110 					    (tmr_started == 0)) {
7111 						/*
7112 						 * ok we just fast-retrans'd
7113 						 * the lowest TSN, i.e the
7114 						 * first on the list. In
7115 						 * this case we want to give
7116 						 * some more time to get a
7117 						 * SACK back without a
7118 						 * t3-expiring.
7119 						 */
7120 						sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
7121 						    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
7122 						sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
7123 					}
7124 				}
7125 			}
7126 #ifdef SCTP_CWND_LOGGING
7127 			sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
7128 #endif
7129 #ifdef SCTP_AUDITING_ENABLED
7130 			sctp_auditing(21, inp, stcb, NULL);
7131 #endif
7132 		} else {
7133 			/* None will fit */
7134 			return (1);
7135 		}
7136 		if (asoc->sent_queue_retran_cnt <= 0) {
7137 			/* all done we have no more to retran */
7138 			asoc->sent_queue_retran_cnt = 0;
7139 			break;
7140 		}
7141 		if (one_chunk) {
7142 			/* No more room in rwnd */
7143 			return (1);
7144 		}
7145 		/* stop the for loop here. we sent out a packet */
7146 		break;
7147 	}
7148 	return (0);
7149 }
7150 
7151 
7152 static int
7153 sctp_timer_validation(struct sctp_inpcb *inp,
7154     struct sctp_tcb *stcb,
7155     struct sctp_association *asoc,
7156     int ret)
7157 {
7158 	struct sctp_nets *net;
7159 
7160 	/* Validate that a timer is running somewhere */
7161 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7162 		if (callout_pending(&net->rxt_timer.timer)) {
7163 			/* Here is a timer */
7164 			return (ret);
7165 		}
7166 	}
7167 	SCTP_TCB_LOCK_ASSERT(stcb);
7168 	/* Gak, we did not have a timer somewhere */
7169 #ifdef SCTP_DEBUG
7170 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7171 		printf("Deadlock avoided starting timer on a dest at retran\n");
7172 	}
7173 #endif
7174 	sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
7175 	return (ret);
7176 }
7177 
7178 int
7179 sctp_chunk_output(struct sctp_inpcb *inp,
7180     struct sctp_tcb *stcb,
7181     int from_where)
7182 {
7183 	/*
7184 	 * Ok this is the generic chunk service queue. we must do the
7185 	 * following: - See if there are retransmits pending, if so we must
7186 	 * do these first and return. - Service the stream queue that is
7187 	 * next, moving any message (note I must get a complete message i.e.
7188 	 * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
7189 	 * TSN's - Check to see if the cwnd/rwnd allows any output, if so we
7190 	 * go ahead and fomulate and send the low level chunks. Making sure
7191 	 * to combine any control in the control chunk queue also.
7192 	 */
7193 	struct sctp_association *asoc;
7194 	struct sctp_nets *net;
7195 	int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
7196 	    burst_cnt = 0, burst_limit = 0;
7197 	struct timeval now;
7198 	int now_filled = 0;
7199 	int cwnd_full = 0;
7200 	int nagle_on = 0;
7201 	int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
7202 	int un_sent = 0;
7203 
7204 	asoc = &stcb->asoc;
7205 	if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
7206 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
7207 			nagle_on = 0;
7208 		} else {
7209 			nagle_on = 1;
7210 		}
7211 	}
7212 	SCTP_TCB_LOCK_ASSERT(stcb);
7213 
7214 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
7215 
7216 	if ((un_sent <= 0) &&
7217 	    (TAILQ_EMPTY(&asoc->control_send_queue)) &&
7218 	    (asoc->sent_queue_retran_cnt == 0)) {
7219 		/* Nothing to do unless there is something to be sent left */
7220 		return (error);
7221 	}
7222 	/*
7223 	 * Do we have something to send, data or control AND a sack timer
7224 	 * running, if so piggy-back the sack.
7225 	 */
7226 	if (callout_pending(&stcb->asoc.dack_timer.timer)) {
7227 		sctp_send_sack(stcb);
7228 		callout_stop(&stcb->asoc.dack_timer.timer);
7229 	}
7230 	while (asoc->sent_queue_retran_cnt) {
7231 		/*
7232 		 * Ok, it is retransmission time only, we send out only ONE
7233 		 * packet with a single call off to the retran code.
7234 		 */
7235 		if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
7236 			/*
7237 			 * Special hook for handling cookiess discarded by
7238 			 * peer that carried data. Send cookie-ack only and
7239 			 * then the next call with get the retran's.
7240 			 */
7241 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
7242 			    &cwnd_full, from_where,
7243 			    &now, &now_filled, frag_point);
7244 			return (0);
7245 		} else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
7246 			/* if its not from a HB then do it */
7247 			ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled);
7248 		} else {
7249 			/*
7250 			 * its from any other place, we don't allow retran
7251 			 * output (only control)
7252 			 */
7253 			ret = 1;
7254 		}
7255 		if (ret > 0) {
7256 			/* Can't send anymore */
7257 			/*
7258 			 * now lets push out control by calling med-level
7259 			 * output once. this assures that we WILL send HB's
7260 			 * if queued too.
7261 			 */
7262 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
7263 			    &cwnd_full, from_where,
7264 			    &now, &now_filled, frag_point);
7265 #ifdef SCTP_AUDITING_ENABLED
7266 			sctp_auditing(8, inp, stcb, NULL);
7267 #endif
7268 			return (sctp_timer_validation(inp, stcb, asoc, ret));
7269 		}
7270 		if (ret < 0) {
7271 			/*
7272 			 * The count was off.. retran is not happening so do
7273 			 * the normal retransmission.
7274 			 */
7275 #ifdef SCTP_AUDITING_ENABLED
7276 			sctp_auditing(9, inp, stcb, NULL);
7277 #endif
7278 			break;
7279 		}
7280 		if (from_where == SCTP_OUTPUT_FROM_T3) {
7281 			/* Only one transmission allowed out of a timeout */
7282 #ifdef SCTP_AUDITING_ENABLED
7283 			sctp_auditing(10, inp, stcb, NULL);
7284 #endif
7285 			/* Push out any control */
7286 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
7287 			    &now, &now_filled, frag_point);
7288 			return (ret);
7289 		}
7290 		if ((num_out == 0) && (ret == 0)) {
7291 			/* No more retrans to send */
7292 			break;
7293 		}
7294 	}
7295 #ifdef SCTP_AUDITING_ENABLED
7296 	sctp_auditing(12, inp, stcb, NULL);
7297 #endif
7298 	/* Check for bad destinations, if they exist move chunks around. */
7299 	burst_limit = asoc->max_burst;
7300 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7301 		if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
7302 		    SCTP_ADDR_NOT_REACHABLE) {
7303 			/*
7304 			 * if possible move things off of this address we
7305 			 * still may send below due to the dormant state but
7306 			 * we try to find an alternate address to send to
7307 			 * and if we have one we move all queued data on the
7308 			 * out wheel to this alternate address.
7309 			 */
7310 			if (net->ref_count > 1)
7311 				sctp_move_to_an_alt(stcb, asoc, net);
7312 		} else {
7313 			/*
7314 			 * if ((asoc->sat_network) || (net->addr_is_local))
7315 			 * { burst_limit = asoc->max_burst *
7316 			 * SCTP_SAT_NETWORK_BURST_INCR; }
7317 			 */
7318 			if (sctp_use_cwnd_based_maxburst) {
7319 				if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
7320 					int old_cwnd;
7321 
7322 					if (net->ssthresh < net->cwnd)
7323 						net->ssthresh = net->cwnd;
7324 					old_cwnd = net->cwnd;
7325 					net->cwnd = (net->flight_size + (burst_limit * net->mtu));
7326 
7327 #ifdef SCTP_CWND_MONITOR
7328 					sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_BRST);
7329 #endif
7330 
7331 #ifdef SCTP_LOG_MAXBURST
7332 					sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
7333 #endif
7334 					SCTP_STAT_INCR(sctps_maxburstqueued);
7335 				}
7336 				net->fast_retran_ip = 0;
7337 			} else {
7338 				if (net->flight_size == 0) {
7339 					/* Should be decaying the cwnd here */
7340 					;
7341 				}
7342 			}
7343 		}
7344 
7345 	}
7346 	burst_cnt = 0;
7347 	cwnd_full = 0;
7348 	do {
7349 		error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
7350 		    &reason_code, 0, &cwnd_full, from_where,
7351 		    &now, &now_filled, frag_point);
7352 		if (error) {
7353 #ifdef SCTP_DEBUG
7354 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7355 				printf("Error %d was returned from med-c-op\n", error);
7356 			}
7357 #endif
7358 #ifdef SCTP_LOG_MAXBURST
7359 			sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
7360 #endif
7361 #ifdef SCTP_CWND_LOGGING
7362 			sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
7363 			sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
7364 #endif
7365 
7366 			break;
7367 		}
7368 #ifdef SCTP_DEBUG
7369 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7370 			printf("m-c-o put out %d\n", num_out);
7371 		}
7372 #endif
7373 		tot_out += num_out;
7374 		burst_cnt++;
7375 #ifdef SCTP_CWND_LOGGING
7376 		sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
7377 		if (num_out == 0) {
7378 			sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
7379 		}
7380 #endif
7381 		if (nagle_on) {
7382 			/*
7383 			 * When nagle is on, we look at how much is un_sent,
7384 			 * then if its smaller than an MTU and we have data
7385 			 * in flight we stop.
7386 			 */
7387 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
7388 			    ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count)
7389 			    * sizeof(struct sctp_data_chunk)));
7390 			if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
7391 			    (stcb->asoc.total_flight > 0)) {
7392 				break;
7393 			}
7394 		}
7395 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7396 		    TAILQ_EMPTY(&asoc->send_queue) &&
7397 		    TAILQ_EMPTY(&asoc->out_wheel)) {
7398 			/* Nothing left to send */
7399 			break;
7400 		}
7401 		if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
7402 			/* Nothing left to send */
7403 			break;
7404 		}
7405 	} while (num_out && (sctp_use_cwnd_based_maxburst ||
7406 	    (burst_cnt < burst_limit)));
7407 
7408 	if (sctp_use_cwnd_based_maxburst == 0) {
7409 		if (burst_cnt >= burst_limit) {
7410 			SCTP_STAT_INCR(sctps_maxburstqueued);
7411 			asoc->burst_limit_applied = 1;
7412 #ifdef SCTP_LOG_MAXBURST
7413 			sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
7414 #endif
7415 		} else {
7416 			asoc->burst_limit_applied = 0;
7417 		}
7418 	}
7419 #ifdef SCTP_CWND_LOGGING
7420 	sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
7421 #endif
7422 #ifdef SCTP_DEBUG
7423 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7424 		printf("Ok, we have put out %d chunks\n", tot_out);
7425 	}
7426 #endif
7427 	/*
7428 	 * Now we need to clean up the control chunk chain if a ECNE is on
7429 	 * it. It must be marked as UNSENT again so next call will continue
7430 	 * to send it until such time that we get a CWR, to remove it.
7431 	 */
7432 	if (stcb->asoc.ecn_echo_cnt_onq)
7433 		sctp_fix_ecn_echo(asoc);
7434 	return (error);
7435 }
7436 
7437 
7438 int
7439 sctp_output(inp, m, addr, control, p, flags)
7440 	struct sctp_inpcb *inp;
7441 	struct mbuf *m;
7442 	struct sockaddr *addr;
7443 	struct mbuf *control;
7444 
7445 	struct thread *p;
7446 	int flags;
7447 {
7448 	if (inp == NULL) {
7449 		return (EINVAL);
7450 	}
7451 	if (inp->sctp_socket == NULL) {
7452 		return (EINVAL);
7453 	}
7454 	return (sctp_sosend(inp->sctp_socket,
7455 	    addr,
7456 	    (struct uio *)NULL,
7457 	    m,
7458 	    control,
7459 	    flags,
7460 	    p));
7461 }
7462 
7463 void
7464 send_forward_tsn(struct sctp_tcb *stcb,
7465     struct sctp_association *asoc)
7466 {
7467 	struct sctp_tmit_chunk *chk;
7468 	struct sctp_forward_tsn_chunk *fwdtsn;
7469 
7470 	SCTP_TCB_LOCK_ASSERT(stcb);
7471 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7472 		if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
7473 			/* mark it to unsent */
7474 			chk->sent = SCTP_DATAGRAM_UNSENT;
7475 			chk->snd_count = 0;
7476 			/* Do we correct its output location? */
7477 			if (chk->whoTo != asoc->primary_destination) {
7478 				sctp_free_remote_addr(chk->whoTo);
7479 				chk->whoTo = asoc->primary_destination;
7480 				atomic_add_int(&chk->whoTo->ref_count, 1);
7481 			}
7482 			goto sctp_fill_in_rest;
7483 		}
7484 	}
7485 	/* Ok if we reach here we must build one */
7486 	sctp_alloc_a_chunk(stcb, chk);
7487 	if (chk == NULL) {
7488 		return;
7489 	}
7490 	chk->copy_by_ref = 0;
7491 	chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
7492 	chk->rec.chunk_id.can_take_data = 0;
7493 	chk->asoc = asoc;
7494 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
7495 	if (chk->data == NULL) {
7496 		atomic_subtract_int(&chk->whoTo->ref_count, 1);
7497 		sctp_free_a_chunk(stcb, chk);
7498 		return;
7499 	}
7500 	chk->data->m_data += SCTP_MIN_OVERHEAD;
7501 	chk->sent = SCTP_DATAGRAM_UNSENT;
7502 	chk->snd_count = 0;
7503 	chk->whoTo = asoc->primary_destination;
7504 	atomic_add_int(&chk->whoTo->ref_count, 1);
7505 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
7506 	asoc->ctrl_queue_cnt++;
7507 sctp_fill_in_rest:
7508 	/*
7509 	 * Here we go through and fill out the part that deals with
7510 	 * stream/seq of the ones we skip.
7511 	 */
7512 	chk->data->m_pkthdr.len = chk->data->m_len = 0;
7513 	{
7514 		struct sctp_tmit_chunk *at, *tp1, *last;
7515 		struct sctp_strseq *strseq;
7516 		unsigned int cnt_of_space, i, ovh;
7517 		unsigned int space_needed;
7518 		unsigned int cnt_of_skipped = 0;
7519 
7520 		TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
7521 			if (at->sent != SCTP_FORWARD_TSN_SKIP) {
7522 				/* no more to look at */
7523 				break;
7524 			}
7525 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
7526 				/* We don't report these */
7527 				continue;
7528 			}
7529 			cnt_of_skipped++;
7530 		}
7531 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
7532 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
7533 
7534 		cnt_of_space = M_TRAILINGSPACE(chk->data);
7535 
7536 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7537 			ovh = SCTP_MIN_OVERHEAD;
7538 		} else {
7539 			ovh = SCTP_MIN_V4_OVERHEAD;
7540 		}
7541 		if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
7542 			/* trim to a mtu size */
7543 			cnt_of_space = asoc->smallest_mtu - ovh;
7544 		}
7545 		if (cnt_of_space < space_needed) {
7546 			/*
7547 			 * ok we must trim down the chunk by lowering the
7548 			 * advance peer ack point.
7549 			 */
7550 			cnt_of_skipped = (cnt_of_space -
7551 			    ((sizeof(struct sctp_forward_tsn_chunk)) /
7552 			    sizeof(struct sctp_strseq)));
7553 			/*
7554 			 * Go through and find the TSN that will be the one
7555 			 * we report.
7556 			 */
7557 			at = TAILQ_FIRST(&asoc->sent_queue);
7558 			for (i = 0; i < cnt_of_skipped; i++) {
7559 				tp1 = TAILQ_NEXT(at, sctp_next);
7560 				at = tp1;
7561 			}
7562 			last = at;
7563 			/*
7564 			 * last now points to last one I can report, update
7565 			 * peer ack point
7566 			 */
7567 			asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
7568 			space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
7569 		}
7570 		chk->send_size = space_needed;
7571 		/* Setup the chunk */
7572 		fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
7573 		fwdtsn->ch.chunk_length = htons(chk->send_size);
7574 		fwdtsn->ch.chunk_flags = 0;
7575 		fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
7576 		fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
7577 		chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
7578 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
7579 		chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
7580 		fwdtsn++;
7581 		/*
7582 		 * Move pointer to after the fwdtsn and transfer to the
7583 		 * strseq pointer.
7584 		 */
7585 		strseq = (struct sctp_strseq *)fwdtsn;
7586 		/*
7587 		 * Now populate the strseq list. This is done blindly
7588 		 * without pulling out duplicate stream info. This is
7589 		 * inefficent but won't harm the process since the peer will
7590 		 * look at these in sequence and will thus release anything.
7591 		 * It could mean we exceed the PMTU and chop off some that
7592 		 * we could have included.. but this is unlikely (aka 1432/4
7593 		 * would mean 300+ stream seq's would have to be reported in
7594 		 * one FWD-TSN. With a bit of work we can later FIX this to
7595 		 * optimize and pull out duplcates.. but it does add more
7596 		 * overhead. So for now... not!
7597 		 */
7598 		at = TAILQ_FIRST(&asoc->sent_queue);
7599 		for (i = 0; i < cnt_of_skipped; i++) {
7600 			tp1 = TAILQ_NEXT(at, sctp_next);
7601 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
7602 				/* We don't report these */
7603 				i--;
7604 				at = tp1;
7605 				continue;
7606 			}
7607 			strseq->stream = ntohs(at->rec.data.stream_number);
7608 			strseq->sequence = ntohs(at->rec.data.stream_seq);
7609 			strseq++;
7610 			at = tp1;
7611 		}
7612 	}
7613 	return;
7614 
7615 }
7616 
7617 void
7618 sctp_send_sack(struct sctp_tcb *stcb)
7619 {
7620 	/*
7621 	 * Queue up a SACK in the control queue. We must first check to see
7622 	 * if a SACK is somehow on the control queue. If so, we will take
7623 	 * and and remove the old one.
7624 	 */
7625 	struct sctp_association *asoc;
7626 	struct sctp_tmit_chunk *chk, *a_chk;
7627 	struct sctp_sack_chunk *sack;
7628 	struct sctp_gap_ack_block *gap_descriptor;
7629 	struct sack_track *selector;
7630 	int mergeable = 0;
7631 	int offset;
7632 	caddr_t limit;
7633 	uint32_t *dup;
7634 	int limit_reached = 0;
7635 	unsigned int i, jstart, siz, j;
7636 	unsigned int num_gap_blocks = 0, space;
7637 	int num_dups = 0;
7638 	int space_req;
7639 
7640 
7641 	a_chk = NULL;
7642 	asoc = &stcb->asoc;
7643 	SCTP_TCB_LOCK_ASSERT(stcb);
7644 	if (asoc->last_data_chunk_from == NULL) {
7645 		/* Hmm we never received anything */
7646 		return;
7647 	}
7648 	sctp_set_rwnd(stcb, asoc);
7649 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7650 		if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
7651 			/* Hmm, found a sack already on queue, remove it */
7652 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7653 			asoc->ctrl_queue_cnt++;
7654 			a_chk = chk;
7655 			if (a_chk->data) {
7656 				sctp_m_freem(a_chk->data);
7657 				a_chk->data = NULL;
7658 			}
7659 			sctp_free_remote_addr(a_chk->whoTo);
7660 			a_chk->whoTo = NULL;
7661 			break;
7662 		}
7663 	}
7664 	if (a_chk == NULL) {
7665 		sctp_alloc_a_chunk(stcb, a_chk);
7666 		if (a_chk == NULL) {
7667 			/* No memory so we drop the idea, and set a timer */
7668 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7669 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
7670 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
7671 			    stcb->sctp_ep, stcb, NULL);
7672 			return;
7673 		}
7674 		a_chk->copy_by_ref = 0;
7675 		/* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
7676 		a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK;
7677 		a_chk->rec.chunk_id.can_take_data = 1;
7678 	}
7679 	a_chk->asoc = asoc;
7680 	a_chk->snd_count = 0;
7681 	a_chk->send_size = 0;	/* fill in later */
7682 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
7683 
7684 	if ((asoc->numduptsns) ||
7685 	    (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
7686 	    ) {
7687 		/*
7688 		 * Ok, we have some duplicates or the destination for the
7689 		 * sack is unreachable, lets see if we can select an
7690 		 * alternate than asoc->last_data_chunk_from
7691 		 */
7692 		if ((!(asoc->last_data_chunk_from->dest_state &
7693 		    SCTP_ADDR_NOT_REACHABLE)) &&
7694 		    (asoc->used_alt_onsack > asoc->numnets)) {
7695 			/* We used an alt last time, don't this time */
7696 			a_chk->whoTo = NULL;
7697 		} else {
7698 			asoc->used_alt_onsack++;
7699 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
7700 		}
7701 		if (a_chk->whoTo == NULL) {
7702 			/* Nope, no alternate */
7703 			a_chk->whoTo = asoc->last_data_chunk_from;
7704 			asoc->used_alt_onsack = 0;
7705 		}
7706 	} else {
7707 		/*
7708 		 * No duplicates so we use the last place we received data
7709 		 * from.
7710 		 */
7711 		asoc->used_alt_onsack = 0;
7712 		a_chk->whoTo = asoc->last_data_chunk_from;
7713 	}
7714 	if (a_chk->whoTo) {
7715 		atomic_add_int(&a_chk->whoTo->ref_count, 1);
7716 	}
7717 	if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
7718 		/* no gaps */
7719 		space_req = sizeof(struct sctp_sack_chunk);
7720 	} else {
7721 		/* gaps get a cluster */
7722 		space_req = MCLBYTES;
7723 	}
7724 	/* Ok now lets formulate a MBUF with our sack */
7725 	a_chk->data = sctp_get_mbuf_for_msg(space_req, 1, M_DONTWAIT, 1, MT_DATA);
7726 	if ((a_chk->data == NULL) ||
7727 	    (a_chk->whoTo == NULL)) {
7728 		/* rats, no mbuf memory */
7729 		if (a_chk->data) {
7730 			/* was a problem with the destination */
7731 			sctp_m_freem(a_chk->data);
7732 			a_chk->data = NULL;
7733 		}
7734 		if (a_chk->whoTo)
7735 			atomic_subtract_int(&a_chk->whoTo->ref_count, 1);
7736 		sctp_free_a_chunk(stcb, a_chk);
7737 		sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7738 		    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
7739 		sctp_timer_start(SCTP_TIMER_TYPE_RECV,
7740 		    stcb->sctp_ep, stcb, NULL);
7741 		return;
7742 	}
7743 	/* ok, lets go through and fill it in */
7744 	a_chk->data->m_data += SCTP_MIN_OVERHEAD;
7745 	space = M_TRAILINGSPACE(a_chk->data);
7746 	if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
7747 		space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
7748 	}
7749 	limit = mtod(a_chk->data, caddr_t);
7750 	limit += space;
7751 
7752 	sack = mtod(a_chk->data, struct sctp_sack_chunk *);
7753 	sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
7754 	/* 0x01 is used by nonce for ecn */
7755 	sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
7756 	if (sctp_cmt_on_off && sctp_cmt_use_dac) {
7757 		/*
7758 		 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
7759 		 * received, then set high bit to 1, else 0. Reset
7760 		 * pkts_rcvd.
7761 		 */
7762 		sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
7763 		asoc->cmt_dac_pkts_rcvd = 0;
7764 	}
7765 	sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
7766 	sack->sack.a_rwnd = htonl(asoc->my_rwnd);
7767 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
7768 
7769 	/* reset the readers interpretation */
7770 	stcb->freed_by_sorcv_sincelast = 0;
7771 
7772 	gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
7773 
7774 
7775 	siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
7776 	if (asoc->cumulative_tsn < asoc->mapping_array_base_tsn) {
7777 		offset = 1;
7778 		/*
7779 		 * cum-ack behind the mapping array, so we start and use all
7780 		 * entries.
7781 		 */
7782 		jstart = 0;
7783 	} else {
7784 		offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
7785 		/*
7786 		 * we skip the first one when the cum-ack is at or above the
7787 		 * mapping array base.
7788 		 */
7789 		jstart = 1;
7790 	}
7791 	if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
7792 		/* we have a gap .. maybe */
7793 		for (i = 0; i < siz; i++) {
7794 			selector = &sack_array[asoc->mapping_array[i]];
7795 			if (mergeable && selector->right_edge) {
7796 				/*
7797 				 * Backup, left and right edges were ok to
7798 				 * merge.
7799 				 */
7800 				num_gap_blocks--;
7801 				gap_descriptor--;
7802 			}
7803 			if (selector->num_entries == 0)
7804 				mergeable = 0;
7805 			else {
7806 				for (j = jstart; j < selector->num_entries; j++) {
7807 					if (mergeable && selector->right_edge) {
7808 						/*
7809 						 * do a merge by NOT setting
7810 						 * the left side
7811 						 */
7812 						mergeable = 0;
7813 					} else {
7814 						/*
7815 						 * no merge, set the left
7816 						 * side
7817 						 */
7818 						mergeable = 0;
7819 						gap_descriptor->start = htons((selector->gaps[j].start + offset));
7820 					}
7821 					gap_descriptor->end = htons((selector->gaps[j].end + offset));
7822 					num_gap_blocks++;
7823 					gap_descriptor++;
7824 					if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
7825 						/* no more room */
7826 						limit_reached = 1;
7827 						break;
7828 					}
7829 				}
7830 				if (selector->left_edge) {
7831 					mergeable = 1;
7832 				}
7833 			}
7834 			jstart = 0;
7835 			offset += 8;
7836 		}
7837 		if (num_gap_blocks == 0) {
7838 			/* reneged all chunks */
7839 			asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
7840 		}
7841 	}
7842 	/* now we must add any dups we are going to report. */
7843 	if ((limit_reached == 0) && (asoc->numduptsns)) {
7844 		dup = (uint32_t *) gap_descriptor;
7845 		for (i = 0; i < asoc->numduptsns; i++) {
7846 			*dup = htonl(asoc->dup_tsns[i]);
7847 			dup++;
7848 			num_dups++;
7849 			if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
7850 				/* no more room */
7851 				break;
7852 			}
7853 		}
7854 		asoc->numduptsns = 0;
7855 	}
7856 	/*
7857 	 * now that the chunk is prepared queue it to the control chunk
7858 	 * queue.
7859 	 */
7860 	a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
7861 	    (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
7862 	    (num_dups * sizeof(int32_t)));
7863 	a_chk->data->m_pkthdr.len = a_chk->data->m_len = a_chk->send_size;
7864 	sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
7865 	sack->sack.num_dup_tsns = htons(num_dups);
7866 	sack->ch.chunk_length = htons(a_chk->send_size);
7867 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
7868 	asoc->ctrl_queue_cnt++;
7869 	SCTP_STAT_INCR(sctps_sendsacks);
7870 	return;
7871 }
7872 
7873 
7874 void
7875 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr)
7876 {
7877 	struct mbuf *m_abort;
7878 	struct mbuf *m_out = NULL, *m_end = NULL;
7879 	struct sctp_abort_chunk *abort = NULL;
7880 	int sz;
7881 	uint32_t auth_offset = 0;
7882 	struct sctp_auth_chunk *auth = NULL;
7883 	struct sctphdr *shdr;
7884 
7885 	/*
7886 	 * Add an AUTH chunk, if chunk requires it and save the offset into
7887 	 * the chain for AUTH
7888 	 */
7889 	if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
7890 	    stcb->asoc.peer_auth_chunks)) {
7891 		m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
7892 		    stcb, SCTP_ABORT_ASSOCIATION);
7893 	}
7894 	SCTP_TCB_LOCK_ASSERT(stcb);
7895 	m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 1, M_DONTWAIT, 1, MT_HEADER);
7896 	if (m_abort == NULL) {
7897 		/* no mbuf's */
7898 		if (m_out)
7899 			sctp_m_freem(m_out);
7900 		return;
7901 	}
7902 	/* link in any error */
7903 	m_abort->m_next = operr;
7904 	sz = 0;
7905 	if (operr) {
7906 		struct mbuf *n;
7907 
7908 		n = operr;
7909 		while (n) {
7910 			sz += n->m_len;
7911 			n = n->m_next;
7912 		}
7913 	}
7914 	m_abort->m_len = sizeof(*abort);
7915 	m_abort->m_pkthdr.len = m_abort->m_len + sz;
7916 	m_abort->m_pkthdr.rcvif = 0;
7917 	if (m_out == NULL) {
7918 		/* NO Auth chunk prepended, so reserve space in front */
7919 		m_abort->m_data += SCTP_MIN_OVERHEAD;
7920 		m_out = m_abort;
7921 	} else {
7922 		/* Put AUTH chunk at the front of the chain */
7923 		m_out->m_pkthdr.len += m_abort->m_pkthdr.len;
7924 		m_end->m_next = m_abort;
7925 	}
7926 
7927 	/* fill in the ABORT chunk */
7928 	abort = mtod(m_abort, struct sctp_abort_chunk *);
7929 	abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
7930 	abort->ch.chunk_flags = 0;
7931 	abort->ch.chunk_length = htons(sizeof(*abort) + sz);
7932 
7933 	/* prepend and fill in the SCTP header */
7934 	M_PREPEND(m_out, sizeof(struct sctphdr), M_DONTWAIT);
7935 	if (m_out == NULL) {
7936 		/* TSNH: no memory */
7937 		return;
7938 	}
7939 	shdr = mtod(m_out, struct sctphdr *);
7940 	shdr->src_port = stcb->sctp_ep->sctp_lport;
7941 	shdr->dest_port = stcb->rport;
7942 	shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7943 	shdr->checksum = 0;
7944 	auth_offset += sizeof(struct sctphdr);
7945 
7946 	sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
7947 	    stcb->asoc.primary_destination,
7948 	    (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
7949 	    m_out, auth_offset, auth, 1, 0, NULL, 0);
7950 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7951 }
7952 
7953 int
7954 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
7955     struct sctp_nets *net)
7956 {
7957 	/* formulate and SEND a SHUTDOWN-COMPLETE */
7958 	struct mbuf *m_shutdown_comp;
7959 	struct sctp_shutdown_complete_msg *comp_cp;
7960 
7961 	m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_complete_msg), 1, M_DONTWAIT, 1, MT_HEADER);
7962 	if (m_shutdown_comp == NULL) {
7963 		/* no mbuf's */
7964 		return (-1);
7965 	}
7966 	m_shutdown_comp->m_data += sizeof(struct ip6_hdr);
7967 	comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
7968 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
7969 	comp_cp->shut_cmp.ch.chunk_flags = 0;
7970 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
7971 	comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
7972 	comp_cp->sh.dest_port = stcb->rport;
7973 	comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
7974 	comp_cp->sh.checksum = 0;
7975 
7976 	m_shutdown_comp->m_pkthdr.len = m_shutdown_comp->m_len = sizeof(struct sctp_shutdown_complete_msg);
7977 	m_shutdown_comp->m_pkthdr.rcvif = 0;
7978 	sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
7979 	    (struct sockaddr *)&net->ro._l_addr,
7980 	    m_shutdown_comp, 0, NULL, 1, 0, NULL, 0);
7981 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7982 	return (0);
7983 }
7984 
7985 int
7986 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh)
7987 {
7988 	/* formulate and SEND a SHUTDOWN-COMPLETE */
7989 	struct mbuf *mout;
7990 	struct ip *iph, *iph_out;
7991 	struct ip6_hdr *ip6, *ip6_out;
7992 	int offset_out;
7993 	struct sctp_shutdown_complete_msg *comp_cp;
7994 
7995 	mout = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_complete_msg), 1, M_DONTWAIT, 1, MT_HEADER);
7996 	if (mout == NULL) {
7997 		/* no mbuf's */
7998 		return (-1);
7999 	}
8000 	iph = mtod(m, struct ip *);
8001 	iph_out = NULL;
8002 	ip6_out = NULL;
8003 	offset_out = 0;
8004 	if (iph->ip_v == IPVERSION) {
8005 		mout->m_len = sizeof(struct ip) +
8006 		    sizeof(struct sctp_shutdown_complete_msg);
8007 		mout->m_next = NULL;
8008 		iph_out = mtod(mout, struct ip *);
8009 
8010 		/* Fill in the IP header for the ABORT */
8011 		iph_out->ip_v = IPVERSION;
8012 		iph_out->ip_hl = (sizeof(struct ip) / 4);
8013 		iph_out->ip_tos = (u_char)0;
8014 		iph_out->ip_id = 0;
8015 		iph_out->ip_off = 0;
8016 		iph_out->ip_ttl = MAXTTL;
8017 		iph_out->ip_p = IPPROTO_SCTP;
8018 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
8019 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
8020 
8021 		/* let IP layer calculate this */
8022 		iph_out->ip_sum = 0;
8023 		offset_out += sizeof(*iph_out);
8024 		comp_cp = (struct sctp_shutdown_complete_msg *)(
8025 		    (caddr_t)iph_out + offset_out);
8026 	} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
8027 		ip6 = (struct ip6_hdr *)iph;
8028 		mout->m_len = sizeof(struct ip6_hdr) +
8029 		    sizeof(struct sctp_shutdown_complete_msg);
8030 		mout->m_next = NULL;
8031 		ip6_out = mtod(mout, struct ip6_hdr *);
8032 
8033 		/* Fill in the IPv6 header for the ABORT */
8034 		ip6_out->ip6_flow = ip6->ip6_flow;
8035 		ip6_out->ip6_hlim = ip6_defhlim;
8036 		ip6_out->ip6_nxt = IPPROTO_SCTP;
8037 		ip6_out->ip6_src = ip6->ip6_dst;
8038 		ip6_out->ip6_dst = ip6->ip6_src;
8039 		ip6_out->ip6_plen = mout->m_len;
8040 		offset_out += sizeof(*ip6_out);
8041 		comp_cp = (struct sctp_shutdown_complete_msg *)(
8042 		    (caddr_t)ip6_out + offset_out);
8043 	} else {
8044 		/* Currently not supported. */
8045 		return (-1);
8046 	}
8047 
8048 	/* Now copy in and fill in the ABORT tags etc. */
8049 	comp_cp->sh.src_port = sh->dest_port;
8050 	comp_cp->sh.dest_port = sh->src_port;
8051 	comp_cp->sh.checksum = 0;
8052 	comp_cp->sh.v_tag = sh->v_tag;
8053 	comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
8054 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
8055 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
8056 
8057 	mout->m_pkthdr.len = mout->m_len;
8058 	/* add checksum */
8059 	if ((sctp_no_csum_on_loopback) &&
8060 	    (m->m_pkthdr.rcvif) &&
8061 	    (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
8062 		comp_cp->sh.checksum = 0;
8063 	} else {
8064 		comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
8065 	}
8066 
8067 	/* zap the rcvif, it should be null */
8068 	mout->m_pkthdr.rcvif = 0;
8069 	/* zap the stack pointer to the route */
8070 	if (iph_out != NULL) {
8071 		struct route ro;
8072 
8073 		bzero(&ro, sizeof ro);
8074 		/* set IPv4 length */
8075 		iph_out->ip_len = mout->m_pkthdr.len;
8076 		/* out it goes */
8077 		ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL
8078 		    ,NULL
8079 		    );
8080 		/* Free the route if we got one back */
8081 		if (ro.ro_rt)
8082 			RTFREE(ro.ro_rt);
8083 	} else if (ip6_out != NULL) {
8084 		struct route_in6 ro;
8085 
8086 
8087 		bzero(&ro, sizeof(ro));
8088 		ip6_output(mout, NULL, &ro, 0, NULL, NULL
8089 		    ,NULL
8090 		    );
8091 		/* Free the route if we got one back */
8092 		if (ro.ro_rt)
8093 			RTFREE(ro.ro_rt);
8094 	}
8095 	SCTP_STAT_INCR(sctps_sendpackets);
8096 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
8097 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8098 	return (0);
8099 }
8100 
8101 static struct sctp_nets *
8102 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
8103 {
8104 	struct sctp_nets *net, *hnet;
8105 	int ms_goneby, highest_ms, state_overide = 0;
8106 
8107 	SCTP_GETTIME_TIMEVAL(now);
8108 	highest_ms = 0;
8109 	hnet = NULL;
8110 	SCTP_TCB_LOCK_ASSERT(stcb);
8111 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
8112 		if (
8113 		    ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
8114 		    (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
8115 		    ) {
8116 			/*
8117 			 * Skip this guy from consideration if HB is off AND
8118 			 * its confirmed
8119 			 */
8120 			continue;
8121 		}
8122 		if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
8123 			/* skip this dest net from consideration */
8124 			continue;
8125 		}
8126 		if (net->last_sent_time.tv_sec) {
8127 			/* Sent to so we subtract */
8128 			ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
8129 		} else
8130 			/* Never been sent to */
8131 			ms_goneby = 0x7fffffff;
8132 		/*
8133 		 * When the address state is unconfirmed but still
8134 		 * considered reachable, we HB at a higher rate. Once it
8135 		 * goes confirmed OR reaches the "unreachable" state, thenw
8136 		 * we cut it back to HB at a more normal pace.
8137 		 */
8138 		if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
8139 			state_overide = 1;
8140 		} else {
8141 			state_overide = 0;
8142 		}
8143 
8144 		if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
8145 		    (ms_goneby > highest_ms)) {
8146 			highest_ms = ms_goneby;
8147 			hnet = net;
8148 		}
8149 	}
8150 	if (hnet &&
8151 	    ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
8152 		state_overide = 1;
8153 	} else {
8154 		state_overide = 0;
8155 	}
8156 
8157 	if (highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
8158 		/*
8159 		 * Found the one with longest delay bounds OR it is
8160 		 * unconfirmed and still not marked unreachable.
8161 		 */
8162 #ifdef SCTP_DEBUG
8163 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8164 			printf("net:%p is the hb winner -",
8165 			    hnet);
8166 			if (hnet)
8167 				sctp_print_address((struct sockaddr *)&hnet->ro._l_addr);
8168 			else
8169 				printf(" none\n");
8170 		}
8171 #endif
8172 		/* update the timer now */
8173 		hnet->last_sent_time = *now;
8174 		return (hnet);
8175 	}
8176 	/* Nothing to HB */
8177 	return (NULL);
8178 }
8179 
8180 int
8181 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
8182 {
8183 	struct sctp_tmit_chunk *chk;
8184 	struct sctp_nets *net;
8185 	struct sctp_heartbeat_chunk *hb;
8186 	struct timeval now;
8187 	struct sockaddr_in *sin;
8188 	struct sockaddr_in6 *sin6;
8189 
8190 	SCTP_TCB_LOCK_ASSERT(stcb);
8191 	if (user_req == 0) {
8192 		net = sctp_select_hb_destination(stcb, &now);
8193 		if (net == NULL) {
8194 			/*
8195 			 * All our busy none to send to, just start the
8196 			 * timer again.
8197 			 */
8198 			if (stcb->asoc.state == 0) {
8199 				return (0);
8200 			}
8201 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
8202 			    stcb->sctp_ep,
8203 			    stcb,
8204 			    net);
8205 			return (0);
8206 		}
8207 	} else {
8208 		net = u_net;
8209 		if (net == NULL) {
8210 			return (0);
8211 		}
8212 		SCTP_GETTIME_TIMEVAL(&now);
8213 	}
8214 	sin = (struct sockaddr_in *)&net->ro._l_addr;
8215 	if (sin->sin_family != AF_INET) {
8216 		if (sin->sin_family != AF_INET6) {
8217 			/* huh */
8218 			return (0);
8219 		}
8220 	}
8221 	sctp_alloc_a_chunk(stcb, chk);
8222 	if (chk == NULL) {
8223 #ifdef SCTP_DEBUG
8224 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8225 			printf("Gak, can't get a chunk for hb\n");
8226 		}
8227 #endif
8228 		return (0);
8229 	}
8230 	chk->copy_by_ref = 0;
8231 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
8232 	chk->rec.chunk_id.can_take_data = 1;
8233 	chk->asoc = &stcb->asoc;
8234 	chk->send_size = sizeof(struct sctp_heartbeat_chunk);
8235 
8236 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 1, M_DONTWAIT, 1, MT_HEADER);
8237 	if (chk->data == NULL) {
8238 		sctp_free_a_chunk(stcb, chk);
8239 		return (0);
8240 	}
8241 	chk->data->m_data += SCTP_MIN_OVERHEAD;
8242 	chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8243 	chk->sent = SCTP_DATAGRAM_UNSENT;
8244 	chk->snd_count = 0;
8245 	chk->whoTo = net;
8246 	atomic_add_int(&chk->whoTo->ref_count, 1);
8247 	/* Now we have a mbuf that we can fill in with the details */
8248 	hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
8249 
8250 	/* fill out chunk header */
8251 	hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
8252 	hb->ch.chunk_flags = 0;
8253 	hb->ch.chunk_length = htons(chk->send_size);
8254 	/* Fill out hb parameter */
8255 	hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
8256 	hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
8257 	hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
8258 	hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
8259 	/* Did our user request this one, put it in */
8260 	hb->heartbeat.hb_info.user_req = user_req;
8261 	hb->heartbeat.hb_info.addr_family = sin->sin_family;
8262 	hb->heartbeat.hb_info.addr_len = sin->sin_len;
8263 	if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
8264 		/*
8265 		 * we only take from the entropy pool if the address is not
8266 		 * confirmed.
8267 		 */
8268 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
8269 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
8270 	} else {
8271 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
8272 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
8273 	}
8274 	if (sin->sin_family == AF_INET) {
8275 		memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
8276 	} else if (sin->sin_family == AF_INET6) {
8277 		/* We leave the scope the way it is in our lookup table. */
8278 		sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
8279 		memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
8280 	} else {
8281 		/* huh compiler bug */
8282 		return (0);
8283 	}
8284 	/* ok we have a destination that needs a beat */
8285 	/* lets do the theshold management Qiaobing style */
8286 
8287 	if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
8288 	    stcb->asoc.max_send_times)) {
8289 		/*
8290 		 * we have lost the association, in a way this is quite bad
8291 		 * since we really are one less time since we really did not
8292 		 * send yet. This is the down side to the Q's style as
8293 		 * defined in the RFC and not my alternate style defined in
8294 		 * the RFC.
8295 		 */
8296 		atomic_subtract_int(&chk->whoTo->ref_count, 1);
8297 		if (chk->data != NULL) {
8298 			sctp_m_freem(chk->data);
8299 			chk->data = NULL;
8300 		}
8301 		sctp_free_a_chunk(stcb, chk);
8302 		return (-1);
8303 	}
8304 	net->hb_responded = 0;
8305 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8306 	stcb->asoc.ctrl_queue_cnt++;
8307 	SCTP_STAT_INCR(sctps_sendheartbeat);
8308 	/*
8309 	 * Call directly med level routine to put out the chunk. It will
8310 	 * always tumble out control chunks aka HB but it may even tumble
8311 	 * out data too.
8312 	 */
8313 	return (1);
8314 }
8315 
8316 void
8317 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
8318     uint32_t high_tsn)
8319 {
8320 	struct sctp_association *asoc;
8321 	struct sctp_ecne_chunk *ecne;
8322 	struct sctp_tmit_chunk *chk;
8323 
8324 	asoc = &stcb->asoc;
8325 	SCTP_TCB_LOCK_ASSERT(stcb);
8326 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8327 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
8328 			/* found a previous ECN_ECHO update it if needed */
8329 			ecne = mtod(chk->data, struct sctp_ecne_chunk *);
8330 			ecne->tsn = htonl(high_tsn);
8331 			return;
8332 		}
8333 	}
8334 	/* nope could not find one to update so we must build one */
8335 	sctp_alloc_a_chunk(stcb, chk);
8336 	if (chk == NULL) {
8337 		return;
8338 	}
8339 	chk->copy_by_ref = 0;
8340 	SCTP_STAT_INCR(sctps_sendecne);
8341 	chk->rec.chunk_id.id = SCTP_ECN_ECHO;
8342 	chk->rec.chunk_id.can_take_data = 0;
8343 	chk->asoc = &stcb->asoc;
8344 	chk->send_size = sizeof(struct sctp_ecne_chunk);
8345 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 1, M_DONTWAIT, 1, MT_HEADER);
8346 	if (chk->data == NULL) {
8347 		sctp_free_a_chunk(stcb, chk);
8348 		return;
8349 	}
8350 	chk->data->m_data += SCTP_MIN_OVERHEAD;
8351 	chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8352 	chk->sent = SCTP_DATAGRAM_UNSENT;
8353 	chk->snd_count = 0;
8354 	chk->whoTo = net;
8355 	atomic_add_int(&chk->whoTo->ref_count, 1);
8356 	stcb->asoc.ecn_echo_cnt_onq++;
8357 	ecne = mtod(chk->data, struct sctp_ecne_chunk *);
8358 	ecne->ch.chunk_type = SCTP_ECN_ECHO;
8359 	ecne->ch.chunk_flags = 0;
8360 	ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
8361 	ecne->tsn = htonl(high_tsn);
8362 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8363 	asoc->ctrl_queue_cnt++;
8364 }
8365 
8366 void
8367 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
8368     struct mbuf *m, int iphlen, int bad_crc)
8369 {
8370 	struct sctp_association *asoc;
8371 	struct sctp_pktdrop_chunk *drp;
8372 	struct sctp_tmit_chunk *chk;
8373 	uint8_t *datap;
8374 	int len;
8375 	unsigned int small_one;
8376 	struct ip *iph;
8377 
8378 	long spc;
8379 
8380 	asoc = &stcb->asoc;
8381 	SCTP_TCB_LOCK_ASSERT(stcb);
8382 	if (asoc->peer_supports_pktdrop == 0) {
8383 		/*
8384 		 * peer must declare support before I send one.
8385 		 */
8386 		return;
8387 	}
8388 	if (stcb->sctp_socket == NULL) {
8389 		return;
8390 	}
8391 	sctp_alloc_a_chunk(stcb, chk);
8392 	if (chk == NULL) {
8393 		return;
8394 	}
8395 	chk->copy_by_ref = 0;
8396 	iph = mtod(m, struct ip *);
8397 	if (iph == NULL) {
8398 		return;
8399 	}
8400 	if (iph->ip_v == IPVERSION) {
8401 		/* IPv4 */
8402 		len = chk->send_size = iph->ip_len;
8403 	} else {
8404 		struct ip6_hdr *ip6h;
8405 
8406 		/* IPv6 */
8407 		ip6h = mtod(m, struct ip6_hdr *);
8408 		len = chk->send_size = htons(ip6h->ip6_plen);
8409 	}
8410 	if ((len + iphlen) > m->m_pkthdr.len) {
8411 		/* huh */
8412 		chk->send_size = len = m->m_pkthdr.len - iphlen;
8413 	}
8414 	chk->asoc = &stcb->asoc;
8415 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
8416 	if (chk->data == NULL) {
8417 jump_out:
8418 		sctp_free_a_chunk(stcb, chk);
8419 		return;
8420 	}
8421 	chk->data->m_data += SCTP_MIN_OVERHEAD;
8422 	drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
8423 	if (drp == NULL) {
8424 		sctp_m_freem(chk->data);
8425 		chk->data = NULL;
8426 		goto jump_out;
8427 	}
8428 	small_one = asoc->smallest_mtu;
8429 	if (small_one > MCLBYTES) {
8430 		/* Only one cluster worth of data MAX */
8431 		small_one = MCLBYTES;
8432 	}
8433 	chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
8434 	    sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
8435 	if (chk->book_size > small_one) {
8436 		drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
8437 		drp->trunc_len = htons(chk->send_size);
8438 		chk->send_size = small_one - (SCTP_MED_OVERHEAD +
8439 		    sizeof(struct sctp_pktdrop_chunk) +
8440 		    sizeof(struct sctphdr));
8441 		len = chk->send_size;
8442 	} else {
8443 		/* no truncation needed */
8444 		drp->ch.chunk_flags = 0;
8445 		drp->trunc_len = htons(0);
8446 	}
8447 	if (bad_crc) {
8448 		drp->ch.chunk_flags |= SCTP_BADCRC;
8449 	}
8450 	chk->send_size += sizeof(struct sctp_pktdrop_chunk);
8451 	chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8452 	chk->sent = SCTP_DATAGRAM_UNSENT;
8453 	chk->snd_count = 0;
8454 	if (net) {
8455 		/* we should hit here */
8456 		chk->whoTo = net;
8457 	} else {
8458 		chk->whoTo = asoc->primary_destination;
8459 	}
8460 	atomic_add_int(&chk->whoTo->ref_count, 1);
8461 	chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
8462 	chk->rec.chunk_id.can_take_data = 1;
8463 	drp->ch.chunk_type = SCTP_PACKET_DROPPED;
8464 	drp->ch.chunk_length = htons(chk->send_size);
8465 	spc = stcb->sctp_socket->so_rcv.sb_hiwat;
8466 	if (spc < 0) {
8467 		spc = 0;
8468 	}
8469 	drp->bottle_bw = htonl(spc);
8470 	if (asoc->my_rwnd) {
8471 		drp->current_onq = htonl(asoc->size_on_reasm_queue +
8472 		    asoc->size_on_all_streams +
8473 		    asoc->my_rwnd_control_len +
8474 		    stcb->sctp_socket->so_rcv.sb_cc);
8475 	} else {
8476 		/*
8477 		 * If my rwnd is 0, possibly from mbuf depletion as well as
8478 		 * space used, tell the peer there is NO space aka onq == bw
8479 		 */
8480 		drp->current_onq = htonl(spc);
8481 	}
8482 	drp->reserved = 0;
8483 	datap = drp->data;
8484 	m_copydata(m, iphlen, len, (caddr_t)datap);
8485 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8486 	asoc->ctrl_queue_cnt++;
8487 }
8488 
8489 void
8490 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
8491 {
8492 	struct sctp_association *asoc;
8493 	struct sctp_cwr_chunk *cwr;
8494 	struct sctp_tmit_chunk *chk;
8495 
8496 	asoc = &stcb->asoc;
8497 	SCTP_TCB_LOCK_ASSERT(stcb);
8498 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8499 		if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
8500 			/* found a previous ECN_CWR update it if needed */
8501 			cwr = mtod(chk->data, struct sctp_cwr_chunk *);
8502 			if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
8503 			    MAX_TSN)) {
8504 				cwr->tsn = htonl(high_tsn);
8505 			}
8506 			return;
8507 		}
8508 	}
8509 	/* nope could not find one to update so we must build one */
8510 	sctp_alloc_a_chunk(stcb, chk);
8511 	if (chk == NULL) {
8512 		return;
8513 	}
8514 	chk->copy_by_ref = 0;
8515 	chk->rec.chunk_id.id = SCTP_ECN_CWR;
8516 	chk->rec.chunk_id.can_take_data = 1;
8517 	chk->asoc = &stcb->asoc;
8518 	chk->send_size = sizeof(struct sctp_cwr_chunk);
8519 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 1, M_DONTWAIT, 1, MT_HEADER);
8520 	if (chk->data == NULL) {
8521 		sctp_free_a_chunk(stcb, chk);
8522 		return;
8523 	}
8524 	chk->data->m_data += SCTP_MIN_OVERHEAD;
8525 	chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8526 	chk->sent = SCTP_DATAGRAM_UNSENT;
8527 	chk->snd_count = 0;
8528 	chk->whoTo = net;
8529 	atomic_add_int(&chk->whoTo->ref_count, 1);
8530 	cwr = mtod(chk->data, struct sctp_cwr_chunk *);
8531 	cwr->ch.chunk_type = SCTP_ECN_CWR;
8532 	cwr->ch.chunk_flags = 0;
8533 	cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
8534 	cwr->tsn = htonl(high_tsn);
8535 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8536 	asoc->ctrl_queue_cnt++;
8537 }
8538 
8539 void
8540 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
8541     int number_entries, uint16_t * list,
8542     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
8543 {
8544 	int len, old_len, i;
8545 	struct sctp_stream_reset_out_request *req_out;
8546 	struct sctp_chunkhdr *ch;
8547 
8548 	ch = mtod(chk->data, struct sctp_chunkhdr *);
8549 
8550 
8551 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
8552 
8553 	/* get to new offset for the param. */
8554 	req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
8555 	/* now how long will this param be? */
8556 	len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
8557 	req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
8558 	req_out->ph.param_length = htons(len);
8559 	req_out->request_seq = htonl(seq);
8560 	req_out->response_seq = htonl(resp_seq);
8561 	req_out->send_reset_at_tsn = htonl(last_sent);
8562 	if (number_entries) {
8563 		for (i = 0; i < number_entries; i++) {
8564 			req_out->list_of_streams[i] = htons(list[i]);
8565 		}
8566 	}
8567 	if (SCTP_SIZE32(len) > len) {
8568 		/*
8569 		 * Need to worry about the pad we may end up adding to the
8570 		 * end. This is easy since the struct is either aligned to 4
8571 		 * bytes or 2 bytes off.
8572 		 */
8573 		req_out->list_of_streams[number_entries] = 0;
8574 	}
8575 	/* now fix the chunk length */
8576 	ch->chunk_length = htons(len + old_len);
8577 	chk->send_size = len + old_len;
8578 	chk->book_size = SCTP_SIZE32(chk->send_size);
8579 	chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8580 	return;
8581 }
8582 
8583 
8584 void
8585 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
8586     int number_entries, uint16_t * list,
8587     uint32_t seq)
8588 {
8589 	int len, old_len, i;
8590 	struct sctp_stream_reset_in_request *req_in;
8591 	struct sctp_chunkhdr *ch;
8592 
8593 	ch = mtod(chk->data, struct sctp_chunkhdr *);
8594 
8595 
8596 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
8597 
8598 	/* get to new offset for the param. */
8599 	req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
8600 	/* now how long will this param be? */
8601 	len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
8602 	req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
8603 	req_in->ph.param_length = htons(len);
8604 	req_in->request_seq = htonl(seq);
8605 	if (number_entries) {
8606 		for (i = 0; i < number_entries; i++) {
8607 			req_in->list_of_streams[i] = htons(list[i]);
8608 		}
8609 	}
8610 	if (SCTP_SIZE32(len) > len) {
8611 		/*
8612 		 * Need to worry about the pad we may end up adding to the
8613 		 * end. This is easy since the struct is either aligned to 4
8614 		 * bytes or 2 bytes off.
8615 		 */
8616 		req_in->list_of_streams[number_entries] = 0;
8617 	}
8618 	/* now fix the chunk length */
8619 	ch->chunk_length = htons(len + old_len);
8620 	chk->send_size = len + old_len;
8621 	chk->book_size = SCTP_SIZE32(chk->send_size);
8622 	chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8623 	return;
8624 }
8625 
8626 
8627 void
8628 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
8629     uint32_t seq)
8630 {
8631 	int len, old_len;
8632 	struct sctp_stream_reset_tsn_request *req_tsn;
8633 	struct sctp_chunkhdr *ch;
8634 
8635 	ch = mtod(chk->data, struct sctp_chunkhdr *);
8636 
8637 
8638 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
8639 
8640 	/* get to new offset for the param. */
8641 	req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
8642 	/* now how long will this param be? */
8643 	len = sizeof(struct sctp_stream_reset_tsn_request);
8644 	req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
8645 	req_tsn->ph.param_length = htons(len);
8646 	req_tsn->request_seq = htonl(seq);
8647 
8648 	/* now fix the chunk length */
8649 	ch->chunk_length = htons(len + old_len);
8650 	chk->send_size = len + old_len;
8651 	chk->book_size = SCTP_SIZE32(chk->send_size);
8652 	chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8653 	return;
8654 }
8655 
8656 void
8657 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
8658     uint32_t resp_seq, uint32_t result)
8659 {
8660 	int len, old_len;
8661 	struct sctp_stream_reset_response *resp;
8662 	struct sctp_chunkhdr *ch;
8663 
8664 	ch = mtod(chk->data, struct sctp_chunkhdr *);
8665 
8666 
8667 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
8668 
8669 	/* get to new offset for the param. */
8670 	resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
8671 	/* now how long will this param be? */
8672 	len = sizeof(struct sctp_stream_reset_response);
8673 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
8674 	resp->ph.param_length = htons(len);
8675 	resp->response_seq = htonl(resp_seq);
8676 	resp->result = ntohl(result);
8677 
8678 	/* now fix the chunk length */
8679 	ch->chunk_length = htons(len + old_len);
8680 	chk->send_size = len + old_len;
8681 	chk->book_size = SCTP_SIZE32(chk->send_size);
8682 	chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8683 	return;
8684 
8685 }
8686 
8687 
8688 void
8689 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
8690     uint32_t resp_seq, uint32_t result,
8691     uint32_t send_una, uint32_t recv_next)
8692 {
8693 	int len, old_len;
8694 	struct sctp_stream_reset_response_tsn *resp;
8695 	struct sctp_chunkhdr *ch;
8696 
8697 	ch = mtod(chk->data, struct sctp_chunkhdr *);
8698 
8699 
8700 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
8701 
8702 	/* get to new offset for the param. */
8703 	resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
8704 	/* now how long will this param be? */
8705 	len = sizeof(struct sctp_stream_reset_response_tsn);
8706 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
8707 	resp->ph.param_length = htons(len);
8708 	resp->response_seq = htonl(resp_seq);
8709 	resp->result = htonl(result);
8710 	resp->senders_next_tsn = htonl(send_una);
8711 	resp->receivers_next_tsn = htonl(recv_next);
8712 
8713 	/* now fix the chunk length */
8714 	ch->chunk_length = htons(len + old_len);
8715 	chk->send_size = len + old_len;
8716 	chk->book_size = SCTP_SIZE32(chk->send_size);
8717 	chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8718 	return;
8719 }
8720 
8721 
8722 int
8723 sctp_send_str_reset_req(struct sctp_tcb *stcb,
8724     int number_entries, uint16_t * list,
8725     uint8_t send_out_req, uint32_t resp_seq,
8726     uint8_t send_in_req,
8727     uint8_t send_tsn_req)
8728 {
8729 
8730 	struct sctp_association *asoc;
8731 	struct sctp_tmit_chunk *chk;
8732 	struct sctp_chunkhdr *ch;
8733 	uint32_t seq;
8734 
8735 	asoc = &stcb->asoc;
8736 	if (asoc->stream_reset_outstanding) {
8737 		/*
8738 		 * Already one pending, must get ACK back to clear the flag.
8739 		 */
8740 		return (EBUSY);
8741 	}
8742 	if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0)) {
8743 		/* nothing to do */
8744 		return (EINVAL);
8745 	}
8746 	if (send_tsn_req && (send_out_req || send_in_req)) {
8747 		/* error, can't do that */
8748 		return (EINVAL);
8749 	}
8750 	sctp_alloc_a_chunk(stcb, chk);
8751 	if (chk == NULL) {
8752 		return (ENOMEM);
8753 	}
8754 	chk->copy_by_ref = 0;
8755 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
8756 	chk->rec.chunk_id.can_take_data = 0;
8757 	chk->asoc = &stcb->asoc;
8758 	chk->book_size = SCTP_SIZE32(chk->send_size = sizeof(struct sctp_chunkhdr));
8759 
8760 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
8761 	if (chk->data == NULL) {
8762 		sctp_free_a_chunk(stcb, chk);
8763 		return (ENOMEM);
8764 	}
8765 	chk->data->m_data += SCTP_MIN_OVERHEAD;
8766 
8767 	/* setup chunk parameters */
8768 	chk->sent = SCTP_DATAGRAM_UNSENT;
8769 	chk->snd_count = 0;
8770 	chk->whoTo = asoc->primary_destination;
8771 	atomic_add_int(&chk->whoTo->ref_count, 1);
8772 
8773 	ch = mtod(chk->data, struct sctp_chunkhdr *);
8774 	ch->chunk_type = SCTP_STREAM_RESET;
8775 	ch->chunk_flags = 0;
8776 	ch->chunk_length = htons(chk->send_size);
8777 	chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8778 
8779 	seq = stcb->asoc.str_reset_seq_out;
8780 	if (send_out_req) {
8781 		sctp_add_stream_reset_out(chk, number_entries, list,
8782 		    seq, resp_seq, (stcb->asoc.sending_seq - 1));
8783 		asoc->stream_reset_out_is_outstanding = 1;
8784 		seq++;
8785 		asoc->stream_reset_outstanding++;
8786 	}
8787 	if (send_in_req) {
8788 		sctp_add_stream_reset_in(chk, number_entries, list, seq);
8789 		asoc->stream_reset_outstanding++;
8790 	}
8791 	if (send_tsn_req) {
8792 		sctp_add_stream_reset_tsn(chk, seq);
8793 		asoc->stream_reset_outstanding++;
8794 	}
8795 	asoc->str_reset = chk;
8796 
8797 	/* insert the chunk for sending */
8798 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
8799 	    chk,
8800 	    sctp_next);
8801 	asoc->ctrl_queue_cnt++;
8802 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
8803 	return (0);
8804 }
8805 
8806 void
8807 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
8808     struct mbuf *err_cause)
8809 {
8810 	/*
8811 	 * Formulate the abort message, and send it back down.
8812 	 */
8813 	struct mbuf *mout;
8814 	struct sctp_abort_msg *abm;
8815 	struct ip *iph, *iph_out;
8816 	struct ip6_hdr *ip6, *ip6_out;
8817 	int iphlen_out;
8818 
8819 	/* don't respond to ABORT with ABORT */
8820 	if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
8821 		if (err_cause)
8822 			sctp_m_freem(err_cause);
8823 		return;
8824 	}
8825 	mout = sctp_get_mbuf_for_msg((sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg)),
8826 	    1, M_DONTWAIT, 1, MT_HEADER);
8827 	if (mout == NULL) {
8828 		if (err_cause)
8829 			sctp_m_freem(err_cause);
8830 		return;
8831 	}
8832 	iph = mtod(m, struct ip *);
8833 	iph_out = NULL;
8834 	ip6_out = NULL;
8835 	if (iph->ip_v == IPVERSION) {
8836 		iph_out = mtod(mout, struct ip *);
8837 		mout->m_len = sizeof(*iph_out) + sizeof(*abm);
8838 		mout->m_next = err_cause;
8839 
8840 		/* Fill in the IP header for the ABORT */
8841 		iph_out->ip_v = IPVERSION;
8842 		iph_out->ip_hl = (sizeof(struct ip) / 4);
8843 		iph_out->ip_tos = (u_char)0;
8844 		iph_out->ip_id = 0;
8845 		iph_out->ip_off = 0;
8846 		iph_out->ip_ttl = MAXTTL;
8847 		iph_out->ip_p = IPPROTO_SCTP;
8848 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
8849 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
8850 		/* let IP layer calculate this */
8851 		iph_out->ip_sum = 0;
8852 
8853 		iphlen_out = sizeof(*iph_out);
8854 		abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
8855 	} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
8856 		ip6 = (struct ip6_hdr *)iph;
8857 		ip6_out = mtod(mout, struct ip6_hdr *);
8858 		mout->m_len = sizeof(*ip6_out) + sizeof(*abm);
8859 		mout->m_next = err_cause;
8860 
8861 		/* Fill in the IP6 header for the ABORT */
8862 		ip6_out->ip6_flow = ip6->ip6_flow;
8863 		ip6_out->ip6_hlim = ip6_defhlim;
8864 		ip6_out->ip6_nxt = IPPROTO_SCTP;
8865 		ip6_out->ip6_src = ip6->ip6_dst;
8866 		ip6_out->ip6_dst = ip6->ip6_src;
8867 
8868 		iphlen_out = sizeof(*ip6_out);
8869 		abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
8870 	} else {
8871 		/* Currently not supported */
8872 		return;
8873 	}
8874 
8875 	abm->sh.src_port = sh->dest_port;
8876 	abm->sh.dest_port = sh->src_port;
8877 	abm->sh.checksum = 0;
8878 	if (vtag == 0) {
8879 		abm->sh.v_tag = sh->v_tag;
8880 		abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
8881 	} else {
8882 		abm->sh.v_tag = htonl(vtag);
8883 		abm->msg.ch.chunk_flags = 0;
8884 	}
8885 	abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
8886 
8887 	if (err_cause) {
8888 		struct mbuf *m_tmp = err_cause;
8889 		int err_len = 0;
8890 
8891 		/* get length of the err_cause chain */
8892 		while (m_tmp != NULL) {
8893 			err_len += m_tmp->m_len;
8894 			m_tmp = m_tmp->m_next;
8895 		}
8896 		mout->m_pkthdr.len = mout->m_len + err_len;
8897 		if (err_len % 4) {
8898 			/* need pad at end of chunk */
8899 			uint32_t cpthis = 0;
8900 			int padlen;
8901 
8902 			padlen = 4 - (mout->m_pkthdr.len % 4);
8903 			m_copyback(mout, mout->m_pkthdr.len, padlen, (caddr_t)&cpthis);
8904 		}
8905 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
8906 	} else {
8907 		mout->m_pkthdr.len = mout->m_len;
8908 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
8909 	}
8910 
8911 	/* add checksum */
8912 	if ((sctp_no_csum_on_loopback) &&
8913 	    (m->m_pkthdr.rcvif) &&
8914 	    (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
8915 		abm->sh.checksum = 0;
8916 	} else {
8917 		abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
8918 	}
8919 
8920 	/* zap the rcvif, it should be null */
8921 	mout->m_pkthdr.rcvif = 0;
8922 	if (iph_out != NULL) {
8923 		struct route ro;
8924 
8925 		/* zap the stack pointer to the route */
8926 		bzero(&ro, sizeof ro);
8927 #ifdef SCTP_DEBUG
8928 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
8929 			printf("sctp_send_abort calling ip_output:\n");
8930 			sctp_print_address_pkt(iph_out, &abm->sh);
8931 		}
8932 #endif
8933 		/* set IPv4 length */
8934 		iph_out->ip_len = mout->m_pkthdr.len;
8935 		/* out it goes */
8936 		(void)ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL
8937 		    ,NULL
8938 		    );
8939 		/* Free the route if we got one back */
8940 		if (ro.ro_rt)
8941 			RTFREE(ro.ro_rt);
8942 	} else if (ip6_out != NULL) {
8943 		struct route_in6 ro;
8944 
8945 
8946 		/* zap the stack pointer to the route */
8947 		bzero(&ro, sizeof(ro));
8948 #ifdef SCTP_DEBUG
8949 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
8950 			printf("sctp_send_abort calling ip6_output:\n");
8951 			sctp_print_address_pkt((struct ip *)ip6_out, &abm->sh);
8952 		}
8953 #endif
8954 		ip6_output(mout, NULL, &ro, 0, NULL, NULL
8955 		    ,NULL
8956 		    );
8957 		/* Free the route if we got one back */
8958 		if (ro.ro_rt)
8959 			RTFREE(ro.ro_rt);
8960 	}
8961 	SCTP_STAT_INCR(sctps_sendpackets);
8962 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
8963 }
8964 
8965 void
8966 sctp_send_operr_to(struct mbuf *m, int iphlen,
8967     struct mbuf *scm,
8968     uint32_t vtag)
8969 {
8970 	struct sctphdr *ihdr;
8971 	int retcode;
8972 	struct sctphdr *ohdr;
8973 	struct sctp_chunkhdr *ophdr;
8974 
8975 	struct ip *iph;
8976 
8977 #ifdef SCTP_DEBUG
8978 	struct sockaddr_in6 lsa6, fsa6;
8979 
8980 #endif
8981 	uint32_t val;
8982 
8983 	iph = mtod(m, struct ip *);
8984 	ihdr = (struct sctphdr *)((caddr_t)iph + iphlen);
8985 	if (!(scm->m_flags & M_PKTHDR)) {
8986 		/* must be a pkthdr */
8987 		printf("Huh, not a packet header in send_operr\n");
8988 		sctp_m_freem(scm);
8989 		return;
8990 	}
8991 	M_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), M_DONTWAIT);
8992 	if (scm == NULL) {
8993 		/* can't send because we can't add a mbuf */
8994 		return;
8995 	}
8996 	ohdr = mtod(scm, struct sctphdr *);
8997 	ohdr->src_port = ihdr->dest_port;
8998 	ohdr->dest_port = ihdr->src_port;
8999 	ohdr->v_tag = vtag;
9000 	ohdr->checksum = 0;
9001 	ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
9002 	ophdr->chunk_type = SCTP_OPERATION_ERROR;
9003 	ophdr->chunk_flags = 0;
9004 	ophdr->chunk_length = htons(scm->m_pkthdr.len - sizeof(struct sctphdr));
9005 	if (scm->m_pkthdr.len % 4) {
9006 		/* need padding */
9007 		uint32_t cpthis = 0;
9008 		int padlen;
9009 
9010 		padlen = 4 - (scm->m_pkthdr.len % 4);
9011 		m_copyback(scm, scm->m_pkthdr.len, padlen, (caddr_t)&cpthis);
9012 	}
9013 	if ((sctp_no_csum_on_loopback) &&
9014 	    (m->m_pkthdr.rcvif) &&
9015 	    (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
9016 		val = 0;
9017 	} else {
9018 		val = sctp_calculate_sum(scm, NULL, 0);
9019 	}
9020 	ohdr->checksum = val;
9021 	if (iph->ip_v == IPVERSION) {
9022 		/* V4 */
9023 		struct ip *out;
9024 		struct route ro;
9025 
9026 		M_PREPEND(scm, sizeof(struct ip), M_DONTWAIT);
9027 		if (scm == NULL)
9028 			return;
9029 		bzero(&ro, sizeof ro);
9030 		out = mtod(scm, struct ip *);
9031 		out->ip_v = iph->ip_v;
9032 		out->ip_hl = (sizeof(struct ip) / 4);
9033 		out->ip_tos = iph->ip_tos;
9034 		out->ip_id = iph->ip_id;
9035 		out->ip_off = 0;
9036 		out->ip_ttl = MAXTTL;
9037 		out->ip_p = IPPROTO_SCTP;
9038 		out->ip_sum = 0;
9039 		out->ip_src = iph->ip_dst;
9040 		out->ip_dst = iph->ip_src;
9041 		out->ip_len = scm->m_pkthdr.len;
9042 		retcode = ip_output(scm, 0, &ro, IP_RAWOUTPUT, NULL
9043 		    ,NULL
9044 		    );
9045 		SCTP_STAT_INCR(sctps_sendpackets);
9046 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
9047 		/* Free the route if we got one back */
9048 		if (ro.ro_rt)
9049 			RTFREE(ro.ro_rt);
9050 	} else {
9051 		/* V6 */
9052 		struct route_in6 ro;
9053 
9054 		struct ip6_hdr *out6, *in6;
9055 
9056 		M_PREPEND(scm, sizeof(struct ip6_hdr), M_DONTWAIT);
9057 		if (scm == NULL)
9058 			return;
9059 		bzero(&ro, sizeof ro);
9060 		in6 = mtod(m, struct ip6_hdr *);
9061 		out6 = mtod(scm, struct ip6_hdr *);
9062 		out6->ip6_flow = in6->ip6_flow;
9063 		out6->ip6_hlim = ip6_defhlim;
9064 		out6->ip6_nxt = IPPROTO_SCTP;
9065 		out6->ip6_src = in6->ip6_dst;
9066 		out6->ip6_dst = in6->ip6_src;
9067 
9068 #ifdef SCTP_DEBUG
9069 		bzero(&lsa6, sizeof(lsa6));
9070 		lsa6.sin6_len = sizeof(lsa6);
9071 		lsa6.sin6_family = AF_INET6;
9072 		lsa6.sin6_addr = out6->ip6_src;
9073 		bzero(&fsa6, sizeof(fsa6));
9074 		fsa6.sin6_len = sizeof(fsa6);
9075 		fsa6.sin6_family = AF_INET6;
9076 		fsa6.sin6_addr = out6->ip6_dst;
9077 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9078 			printf("sctp_operr_to calling ipv6 output:\n");
9079 			printf("src: ");
9080 			sctp_print_address((struct sockaddr *)&lsa6);
9081 			printf("dst ");
9082 			sctp_print_address((struct sockaddr *)&fsa6);
9083 		}
9084 #endif				/* SCTP_DEBUG */
9085 		ip6_output(scm, NULL, &ro, 0, NULL, NULL
9086 		    ,NULL
9087 		    );
9088 		SCTP_STAT_INCR(sctps_sendpackets);
9089 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
9090 		/* Free the route if we got one back */
9091 		if (ro.ro_rt)
9092 			RTFREE(ro.ro_rt);
9093 	}
9094 }
9095 
9096 
9097 
9098 static struct mbuf *
9099 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
9100     struct uio *uio,
9101     struct sctp_sndrcvinfo *srcv,
9102     int max_send_len,
9103     int user_marks_eor,
9104     int *error,
9105     uint32_t * sndout,
9106     struct mbuf **new_tail)
9107 {
9108 	int left, cancpy, willcpy, need_hdr = 0;
9109 	struct mbuf *m, *prev, *head;
9110 
9111 	left = min(uio->uio_resid, max_send_len);
9112 	/* Always get a header just in case */
9113 	need_hdr = 1;
9114 
9115 	head = sctp_get_mbuf_for_msg(left, need_hdr, M_WAIT, 0, MT_DATA);
9116 	cancpy = M_TRAILINGSPACE(head);
9117 	willcpy = min(cancpy, left);
9118 	*error = uiomove(mtod(head, caddr_t), willcpy, uio);
9119 	if (*error) {
9120 		sctp_m_freem(head);
9121 		return (NULL);
9122 	}
9123 	*sndout += willcpy;
9124 	left -= willcpy;
9125 	head->m_len = willcpy;
9126 	m = head;
9127 	*new_tail = head;
9128 	while (left > 0) {
9129 		/* move in user data */
9130 		m->m_next = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 0, MT_DATA);
9131 		if (m->m_next == NULL) {
9132 			sctp_m_freem(head);
9133 			*new_tail = NULL;
9134 			*error = ENOMEM;
9135 			return (NULL);
9136 		}
9137 		prev = m;
9138 		m = m->m_next;
9139 		cancpy = M_TRAILINGSPACE(m);
9140 		willcpy = min(cancpy, left);
9141 		*error = uiomove(mtod(m, caddr_t), willcpy, uio);
9142 		if (*error) {
9143 			sctp_m_freem(head);
9144 			*new_tail = NULL;
9145 			*error = EFAULT;
9146 			return (NULL);
9147 		}
9148 		m->m_len = willcpy;
9149 		left -= willcpy;
9150 		*sndout += willcpy;
9151 		*new_tail = m;
9152 		if (left == 0) {
9153 			m->m_next = NULL;
9154 		}
9155 	}
9156 	return (head);
9157 }
9158 
9159 static int
9160 sctp_copy_one(struct sctp_stream_queue_pending *sp,
9161     struct uio *uio,
9162     int resv_upfront)
9163 {
9164 	int left, cancpy, willcpy, error;
9165 	struct mbuf *m, *head;
9166 	int cpsz = 0;
9167 
9168 	/* First one gets a header */
9169 	left = sp->length;
9170 	head = m = sctp_get_mbuf_for_msg((left + resv_upfront), 1, M_WAIT, 0, MT_DATA);
9171 	if (m == NULL) {
9172 		return (ENOMEM);
9173 	}
9174 	/*
9175 	 * Add this one for m in now, that way if the alloc fails we won't
9176 	 * have a bad cnt.
9177 	 */
9178 	m->m_data += resv_upfront;
9179 	cancpy = M_TRAILINGSPACE(m);
9180 	willcpy = min(cancpy, left);
9181 	while (left > 0) {
9182 		/* move in user data */
9183 		error = uiomove(mtod(m, caddr_t), willcpy, uio);
9184 		if (error) {
9185 			sctp_m_freem(head);
9186 			return (error);
9187 		}
9188 		m->m_len = willcpy;
9189 		left -= willcpy;
9190 		cpsz += willcpy;
9191 		if (left > 0) {
9192 			m->m_next = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 0, MT_DATA);
9193 			if (m->m_next == NULL) {
9194 				/*
9195 				 * the head goes back to caller, he can free
9196 				 * the rest
9197 				 */
9198 				sctp_m_freem(head);
9199 				return (ENOMEM);
9200 			}
9201 			m = m->m_next;
9202 			cancpy = M_TRAILINGSPACE(m);
9203 			willcpy = min(cancpy, left);
9204 		} else {
9205 			sp->tail_mbuf = m;
9206 			m->m_next = NULL;
9207 		}
9208 	}
9209 	sp->data = head;
9210 	sp->length = cpsz;
9211 	return (0);
9212 }
9213 
9214 
9215 
9216 static struct sctp_stream_queue_pending *
9217 sctp_copy_it_in(struct sctp_tcb *stcb,
9218     struct sctp_association *asoc,
9219     struct sctp_sndrcvinfo *srcv,
9220     struct uio *uio,
9221     struct sctp_nets *net,
9222     int max_send_len,
9223     int user_marks_eor,
9224     int *errno,
9225     int non_blocking)
9226 {
9227 	/*
9228 	 * This routine must be very careful in its work. Protocol
9229 	 * processing is up and running so care must be taken to spl...()
9230 	 * when you need to do something that may effect the stcb/asoc. The
9231 	 * sb is locked however. When data is copied the protocol processing
9232 	 * should be enabled since this is a slower operation...
9233 	 */
9234 	struct sctp_stream_queue_pending *sp = NULL;
9235 	int resv_in_first;
9236 
9237 	*errno = 0;
9238 	/*
9239 	 * Unless E_EOR mode is on, we must make a send FIT in one call.
9240 	 */
9241 	if (((user_marks_eor == 0) && non_blocking) &&
9242 	    (uio->uio_resid > stcb->sctp_socket->so_snd.sb_hiwat)) {
9243 		/* It will NEVER fit */
9244 		*errno = EMSGSIZE;
9245 		goto out_now;
9246 	}
9247 	/* Now can we send this? */
9248 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
9249 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
9250 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
9251 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
9252 		/* got data while shutting down */
9253 		*errno = ECONNRESET;
9254 		goto out_now;
9255 	}
9256 	sp = (struct sctp_stream_queue_pending *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_strmoq);
9257 	if (sp == NULL) {
9258 		*errno = ENOMEM;
9259 		goto out_now;
9260 	}
9261 	SCTP_INCR_STRMOQ_COUNT();
9262 	sp->act_flags = 0;
9263 	sp->sinfo_flags = srcv->sinfo_flags;
9264 	sp->timetolive = srcv->sinfo_timetolive;
9265 	sp->ppid = srcv->sinfo_ppid;
9266 	sp->context = srcv->sinfo_context;
9267 	sp->strseq = 0;
9268 	SCTP_GETTIME_TIMEVAL(&sp->ts);
9269 
9270 	sp->stream = srcv->sinfo_stream;
9271 	sp->length = min(uio->uio_resid, max_send_len);
9272 	if ((sp->length == uio->uio_resid) &&
9273 	    ((user_marks_eor == 0) ||
9274 	    (srcv->sinfo_flags & SCTP_EOF) ||
9275 	    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))
9276 	    ) {
9277 		sp->msg_is_complete = 1;
9278 	} else {
9279 		sp->msg_is_complete = 0;
9280 	}
9281 	sp->some_taken = 0;
9282 	resv_in_first = sizeof(struct sctp_data_chunk);
9283 	sp->data = sp->tail_mbuf = NULL;
9284 	*errno = sctp_copy_one(sp, uio, resv_in_first);
9285 	if (*errno) {
9286 		sctp_free_a_strmoq(stcb, sp);
9287 		sp->data = NULL;
9288 		sp->net = NULL;
9289 		sp = NULL;
9290 	} else {
9291 		if (sp->sinfo_flags & SCTP_ADDR_OVER) {
9292 			sp->net = net;
9293 			sp->addr_over = 1;
9294 		} else {
9295 			sp->net = asoc->primary_destination;
9296 			sp->addr_over = 0;
9297 		}
9298 		atomic_add_int(&sp->net->ref_count, 1);
9299 		sp->data->m_pkthdr.len = sp->length;
9300 		sctp_set_prsctp_policy(stcb, sp);
9301 	}
9302 out_now:
9303 	return (sp);
9304 }
9305 
9306 
9307 int
9308 sctp_sosend(struct socket *so,
9309     struct sockaddr *addr,
9310     struct uio *uio,
9311     struct mbuf *top,
9312     struct mbuf *control,
9313     int flags
9314     ,
9315     struct thread *p
9316 )
9317 {
9318 	struct sctp_inpcb *inp;
9319 	int s, error, use_rcvinfo = 0;
9320 	struct sctp_sndrcvinfo srcv;
9321 
9322 	inp = (struct sctp_inpcb *)so->so_pcb;
9323 	s = splnet();
9324 	if (control) {
9325 		/* process cmsg snd/rcv info (maybe a assoc-id) */
9326 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
9327 		    sizeof(srcv))) {
9328 			/* got one */
9329 			use_rcvinfo = 1;
9330 		}
9331 	}
9332 	error = sctp_lower_sosend(so, addr, uio, top, control, flags,
9333 	    use_rcvinfo, &srcv, p);
9334 	splx(s);
9335 	return (error);
9336 }
9337 
9338 
9339 extern unsigned int sctp_add_more_threshold;
9340 int
9341 sctp_lower_sosend(struct socket *so,
9342     struct sockaddr *addr,
9343     struct uio *uio,
9344     struct mbuf *top,
9345     struct mbuf *control,
9346     int flags,
9347     int use_rcvinfo,
9348     struct sctp_sndrcvinfo *srcv,
9349     struct thread *p
9350 )
9351 {
9352 	unsigned int sndlen, max_len;
9353 	int error, len;
9354 	int s, queue_only = 0, queue_only_for_init = 0;
9355 	int free_cnt_applied = 0;
9356 	int un_sent = 0;
9357 	int now_filled = 0;
9358 	struct sctp_block_entry be;
9359 	struct sctp_inpcb *inp;
9360 	struct sctp_tcb *stcb = NULL;
9361 	struct timeval now;
9362 	struct sctp_nets *net;
9363 	struct sctp_association *asoc;
9364 	struct sctp_inpcb *t_inp;
9365 	int create_lock_applied = 0;
9366 	int nagle_applies = 0;
9367 	int some_on_control = 0;
9368 	int got_all_of_the_send = 0;
9369 	int hold_tcblock = 0;
9370 	int non_blocking = 0;
9371 
9372 	error = 0;
9373 	net = NULL;
9374 	stcb = NULL;
9375 	asoc = NULL;
9376 	t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
9377 	if (inp == NULL) {
9378 		error = EFAULT;
9379 		splx(s);
9380 		goto out_unlocked;
9381 	}
9382 	atomic_add_int(&inp->total_sends, 1);
9383 	if (uio)
9384 		sndlen = uio->uio_resid;
9385 	else
9386 		sndlen = top->m_pkthdr.len;
9387 
9388 	s = splnet();
9389 	hold_tcblock = 0;
9390 
9391 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
9392 	    (inp->sctp_socket->so_qlimit)) {
9393 		/* The listener can NOT send */
9394 		error = EFAULT;
9395 		splx(s);
9396 		goto out_unlocked;
9397 	}
9398 	if ((use_rcvinfo) && srcv) {
9399 		if (srcv->sinfo_flags)
9400 			SCTP_STAT_INCR(sctps_sends_with_flags);
9401 
9402 		if (srcv->sinfo_flags & SCTP_SENDALL) {
9403 			/* its a sendall */
9404 			error = sctp_sendall(inp, uio, top, srcv);
9405 			top = NULL;
9406 			splx(s);
9407 			goto out_unlocked;
9408 		}
9409 	}
9410 	/* now we must find the assoc */
9411 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
9412 		SCTP_INP_RLOCK(inp);
9413 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
9414 		if (stcb == NULL) {
9415 			SCTP_INP_RUNLOCK(inp);
9416 			error = ENOTCONN;
9417 			splx(s);
9418 			goto out_unlocked;
9419 		}
9420 		hold_tcblock = 0;
9421 		SCTP_INP_RUNLOCK(inp);
9422 		if (addr)
9423 			/* Must locate the net structure if addr given */
9424 			net = sctp_findnet(stcb, addr);
9425 		else
9426 			net = stcb->asoc.primary_destination;
9427 
9428 	} else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
9429 		stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
9430 		if (stcb) {
9431 			if (addr)
9432 				/*
9433 				 * Must locate the net structure if addr
9434 				 * given
9435 				 */
9436 				net = sctp_findnet(stcb, addr);
9437 			else
9438 				net = stcb->asoc.primary_destination;
9439 		}
9440 		hold_tcblock = 0;
9441 	} else if (addr) {
9442 		/*
9443 		 * Since we did not use findep we must increment it, and if
9444 		 * we don't find a tcb decrement it.
9445 		 */
9446 		SCTP_INP_WLOCK(inp);
9447 		SCTP_INP_INCR_REF(inp);
9448 		SCTP_INP_WUNLOCK(inp);
9449 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
9450 		if (stcb == NULL) {
9451 			SCTP_INP_WLOCK(inp);
9452 			SCTP_INP_DECR_REF(inp);
9453 			SCTP_INP_WUNLOCK(inp);
9454 		} else {
9455 			hold_tcblock = 1;
9456 		}
9457 	}
9458 	if ((stcb == NULL) && (addr)) {
9459 		/* Possible implicit send? */
9460 		SCTP_ASOC_CREATE_LOCK(inp);
9461 		create_lock_applied = 1;
9462 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
9463 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
9464 			/* Should I really unlock ? */
9465 			error = EFAULT;
9466 			splx(s);
9467 			goto out_unlocked;
9468 
9469 		}
9470 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
9471 		    (addr->sa_family == AF_INET6)) {
9472 			error = EINVAL;
9473 			splx(s);
9474 			goto out_unlocked;
9475 		}
9476 		SCTP_INP_WLOCK(inp);
9477 		SCTP_INP_INCR_REF(inp);
9478 		SCTP_INP_WUNLOCK(inp);
9479 		/* With the lock applied look again */
9480 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
9481 		if (stcb == NULL) {
9482 			SCTP_INP_WLOCK(inp);
9483 			SCTP_INP_DECR_REF(inp);
9484 			SCTP_INP_WUNLOCK(inp);
9485 		} else {
9486 			hold_tcblock = 1;
9487 		}
9488 	}
9489 	if (stcb == NULL) {
9490 		if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
9491 			error = ENOTCONN;
9492 			splx(s);
9493 			goto out_unlocked;
9494 		} else if (addr == NULL) {
9495 			error = ENOENT;
9496 			splx(s);
9497 			goto out_unlocked;
9498 		} else {
9499 			/*
9500 			 * UDP style, we must go ahead and start the INIT
9501 			 * process
9502 			 */
9503 			if ((use_rcvinfo) && (srcv) &&
9504 			    (srcv->sinfo_flags & SCTP_ABORT)) {
9505 				/* User asks to abort a non-existant asoc */
9506 				error = ENOENT;
9507 				splx(s);
9508 				goto out_unlocked;
9509 			}
9510 			/* get an asoc/stcb struct */
9511 			stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
9512 			if (stcb == NULL) {
9513 				/* Error is setup for us in the call */
9514 				splx(s);
9515 				goto out_unlocked;
9516 			}
9517 			if (create_lock_applied) {
9518 				SCTP_ASOC_CREATE_UNLOCK(inp);
9519 				create_lock_applied = 0;
9520 			} else {
9521 				printf("Huh-3? create lock should have been on??\n");
9522 			}
9523 			/*
9524 			 * Turn on queue only flag to prevent data from
9525 			 * being sent
9526 			 */
9527 			queue_only = 1;
9528 			asoc = &stcb->asoc;
9529 			asoc->state = SCTP_STATE_COOKIE_WAIT;
9530 			SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
9531 
9532 			/* initialize authentication params for the assoc */
9533 			sctp_initialize_auth_params(inp, stcb);
9534 
9535 			if (control) {
9536 				/*
9537 				 * see if a init structure exists in cmsg
9538 				 * headers
9539 				 */
9540 				struct sctp_initmsg initm;
9541 				int i;
9542 
9543 				if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
9544 				    sizeof(initm))) {
9545 					/*
9546 					 * we have an INIT override of the
9547 					 * default
9548 					 */
9549 					if (initm.sinit_max_attempts)
9550 						asoc->max_init_times = initm.sinit_max_attempts;
9551 					if (initm.sinit_num_ostreams)
9552 						asoc->pre_open_streams = initm.sinit_num_ostreams;
9553 					if (initm.sinit_max_instreams)
9554 						asoc->max_inbound_streams = initm.sinit_max_instreams;
9555 					if (initm.sinit_max_init_timeo)
9556 						asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
9557 					if (asoc->streamoutcnt < asoc->pre_open_streams) {
9558 						/* Default is NOT correct */
9559 #ifdef SCTP_DEBUG
9560 						if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
9561 							printf("Ok, defout:%d pre_open:%d\n",
9562 							    asoc->streamoutcnt, asoc->pre_open_streams);
9563 						}
9564 #endif
9565 						SCTP_FREE(asoc->strmout);
9566 						asoc->strmout = NULL;
9567 						asoc->streamoutcnt = asoc->pre_open_streams;
9568 						/*
9569 						 * What happens if this
9570 						 * fails? .. we panic ...
9571 						 */
9572 						{
9573 							struct sctp_stream_out *tmp_str;
9574 							int had_lock = 0;
9575 
9576 							if (hold_tcblock) {
9577 								had_lock = 1;
9578 								SCTP_TCB_UNLOCK(stcb);
9579 							}
9580 							SCTP_MALLOC(tmp_str,
9581 							    struct sctp_stream_out *,
9582 							    asoc->streamoutcnt *
9583 							    sizeof(struct sctp_stream_out),
9584 							    "StreamsOut");
9585 							if (had_lock) {
9586 								SCTP_TCB_LOCK(stcb);
9587 							}
9588 							asoc->strmout = tmp_str;
9589 						}
9590 						for (i = 0; i < asoc->streamoutcnt; i++) {
9591 							/*
9592 							 * inbound side must
9593 							 * be set to 0xffff,
9594 							 * also NOTE when we
9595 							 * get the INIT-ACK
9596 							 * back (for INIT
9597 							 * sender) we MUST
9598 							 * reduce the count
9599 							 * (streamoutcnt)
9600 							 * but first check
9601 							 * if we sent to any
9602 							 * of the upper
9603 							 * streams that were
9604 							 * dropped (if some
9605 							 * were). Those that
9606 							 * were dropped must
9607 							 * be notified to
9608 							 * the upper layer
9609 							 * as failed to
9610 							 * send.
9611 							 */
9612 							asoc->strmout[i].next_sequence_sent = 0x0;
9613 							TAILQ_INIT(&asoc->strmout[i].outqueue);
9614 							asoc->strmout[i].stream_no = i;
9615 							asoc->strmout[i].last_msg_incomplete = 0;
9616 							asoc->strmout[i].next_spoke.tqe_next = 0;
9617 							asoc->strmout[i].next_spoke.tqe_prev = 0;
9618 						}
9619 					}
9620 				}
9621 			}
9622 			hold_tcblock = 1;
9623 			/* out with the INIT */
9624 			queue_only_for_init = 1;
9625 			/*
9626 			 * we may want to dig in after this call and adjust
9627 			 * the MTU value. It defaulted to 1500 (constant)
9628 			 * but the ro structure may now have an update and
9629 			 * thus we may need to change it BEFORE we append
9630 			 * the message.
9631 			 */
9632 			net = stcb->asoc.primary_destination;
9633 			asoc = &stcb->asoc;
9634 		}
9635 	}
9636 	if (((so->so_state & SS_NBIO)
9637 	    || (flags & MSG_NBIO)
9638 	    )) {
9639 		non_blocking = 1;
9640 	}
9641 	asoc = &stcb->asoc;
9642 	/* would we block? */
9643 	if (non_blocking) {
9644 		if ((so->so_snd.sb_hiwat <
9645 		    (sndlen + stcb->asoc.total_output_queue_size)) ||
9646 		    (stcb->asoc.chunks_on_out_queue >
9647 		    sctp_max_chunks_on_queue)) {
9648 			error = EWOULDBLOCK;
9649 			atomic_add_int(&stcb->sctp_ep->total_nospaces, 1);
9650 			splx(s);
9651 			goto out_unlocked;
9652 		}
9653 	}
9654 	/* Keep the stcb from being freed under our feet */
9655 	atomic_add_int(&stcb->asoc.refcnt, 1);
9656 	free_cnt_applied = 1;
9657 
9658 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
9659 		error = ECONNRESET;
9660 		goto out_unlocked;
9661 	}
9662 	if (create_lock_applied) {
9663 		SCTP_ASOC_CREATE_UNLOCK(inp);
9664 		create_lock_applied = 0;
9665 	}
9666 	if (asoc->stream_reset_outstanding) {
9667 		/*
9668 		 * Can't queue any data while stream reset is underway.
9669 		 */
9670 		error = EAGAIN;
9671 		goto out_unlocked;
9672 	}
9673 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
9674 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
9675 		queue_only = 1;
9676 	}
9677 	if ((use_rcvinfo == 0) || (srcv == NULL)) {
9678 		/* Grab the default stuff from the asoc */
9679 		srcv = &stcb->asoc.def_send;
9680 	}
9681 	/* we are now done with all control */
9682 	if (control) {
9683 		sctp_m_freem(control);
9684 		control = NULL;
9685 	}
9686 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
9687 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
9688 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
9689 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
9690 		if ((use_rcvinfo) &&
9691 		    (srcv->sinfo_flags & SCTP_ABORT)) {
9692 			;
9693 		} else {
9694 			error = ECONNRESET;
9695 			splx(s);
9696 			goto out_unlocked;
9697 		}
9698 	}
9699 	/* Ok, we will attempt a msgsnd :> */
9700 	if (p) {
9701 		p->td_proc->p_stats->p_ru.ru_msgsnd++;
9702 	}
9703 	if (stcb) {
9704 		if (net && ((srcv->sinfo_flags & SCTP_ADDR_OVER))) {
9705 			/* we take the override or the unconfirmed */
9706 			;
9707 		} else {
9708 			net = stcb->asoc.primary_destination;
9709 		}
9710 	}
9711 	if ((net->flight_size > net->cwnd) && (sctp_cmt_on_off == 0)) {
9712 		/*
9713 		 * CMT: Added check for CMT above. net above is the primary
9714 		 * dest. If CMT is ON, sender should always attempt to send
9715 		 * with the output routine sctp_fill_outqueue() that loops
9716 		 * through all destination addresses. Therefore, if CMT is
9717 		 * ON, queue_only is NOT set to 1 here, so that
9718 		 * sctp_chunk_output() can be called below.
9719 		 */
9720 		queue_only = 1;
9721 
9722 	} else if (asoc->ifp_had_enobuf) {
9723 		SCTP_STAT_INCR(sctps_ifnomemqueued);
9724 		if (net->flight_size > (net->mtu * 2))
9725 			queue_only = 1;
9726 		asoc->ifp_had_enobuf = 0;
9727 	} else {
9728 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9729 		    ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)));
9730 	}
9731 	/* Are we aborting? */
9732 	if (srcv->sinfo_flags & SCTP_ABORT) {
9733 		struct mbuf *mm;
9734 		int tot_demand, tot_out, max;
9735 
9736 		SCTP_STAT_INCR(sctps_sends_with_abort);
9737 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
9738 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
9739 			/* It has to be up before we abort */
9740 			/* how big is the user initiated abort? */
9741 			error = EINVAL;
9742 			goto out;
9743 		}
9744 		if (hold_tcblock) {
9745 			SCTP_TCB_UNLOCK(stcb);
9746 			hold_tcblock = 0;
9747 		}
9748 		if (top) {
9749 			mm = sctp_get_mbuf_for_msg(1, 1, M_WAIT, 1, MT_DATA);
9750 			if (top->m_flags & M_PKTHDR)
9751 				tot_out = top->m_pkthdr.len;
9752 			else {
9753 				struct mbuf *cntm;
9754 
9755 				tot_out = 0;
9756 				cntm = top;
9757 				while (cntm) {
9758 					tot_out += cntm->m_len;
9759 					cntm = cntm->m_next;
9760 				}
9761 			}
9762 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
9763 		} else {
9764 			/* Must fit in a MTU */
9765 			tot_out = uio->uio_resid;
9766 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
9767 			mm = sctp_get_mbuf_for_msg(tot_demand, 1, M_WAIT, 1, MT_DATA);
9768 		}
9769 		if (mm == NULL) {
9770 			error = ENOMEM;
9771 			goto out;
9772 		}
9773 		max = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
9774 		max -= sizeof(struct sctp_abort_msg);
9775 		if (tot_out > max) {
9776 			tot_out = max;
9777 		}
9778 		if (mm) {
9779 			struct sctp_paramhdr *ph;
9780 
9781 			/* now move forward the data pointer */
9782 			ph = mtod(mm, struct sctp_paramhdr *);
9783 			ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
9784 			ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
9785 			ph++;
9786 			mm->m_pkthdr.len = tot_out + sizeof(struct sctp_paramhdr);
9787 			mm->m_len = mm->m_pkthdr.len;
9788 			if (top == NULL) {
9789 				error = uiomove((caddr_t)ph, (int)tot_out, uio);
9790 				if (error) {
9791 					/*
9792 					 * Here if we can't get his data we
9793 					 * still abort we just don't get to
9794 					 * send the users note :-0
9795 					 */
9796 					sctp_m_freem(mm);
9797 					mm = NULL;
9798 				}
9799 			} else {
9800 				mm->m_next = top;
9801 			}
9802 		}
9803 		if (hold_tcblock == 0) {
9804 			SCTP_TCB_LOCK(stcb);
9805 			hold_tcblock = 1;
9806 		}
9807 		atomic_add_int(&stcb->asoc.refcnt, -1);
9808 		free_cnt_applied = 0;
9809 		/* release this lock, otherwise we hang on ourselves */
9810 		sctp_abort_an_association(stcb->sctp_ep, stcb,
9811 		    SCTP_RESPONSE_TO_USER_REQ,
9812 		    mm);
9813 		/* now relock the stcb so everything is sane */
9814 		hold_tcblock = 0;
9815 		stcb = NULL;
9816 		goto out_unlocked;
9817 	}
9818 	/* Calculate the maximum we can send */
9819 	if (so->so_snd.sb_hiwat > stcb->asoc.total_output_queue_size) {
9820 		max_len = so->so_snd.sb_hiwat - stcb->asoc.total_output_queue_size;
9821 	} else {
9822 		max_len = 0;
9823 	}
9824 	if (hold_tcblock) {
9825 		SCTP_TCB_UNLOCK(stcb);
9826 		hold_tcblock = 0;
9827 	}
9828 	splx(s);
9829 	/* Is the stream no. valid? */
9830 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
9831 		/* Invalid stream number */
9832 		error = EINVAL;
9833 		goto out_unlocked;
9834 	}
9835 	if (asoc->strmout == NULL) {
9836 		/* huh? software error */
9837 		error = EFAULT;
9838 		goto out_unlocked;
9839 	}
9840 	len = 0;
9841 	if (max_len < sctp_add_more_threshold) {
9842 		/* No room right no ! */
9843 		SOCKBUF_LOCK(&so->so_snd);
9844 		while (so->so_snd.sb_hiwat < (stcb->asoc.total_output_queue_size + sctp_add_more_threshold)) {
9845 #ifdef SCTP_BLK_LOGGING
9846 			sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA,
9847 			    so, asoc, uio->uio_resid);
9848 #endif
9849 			be.error = 0;
9850 			stcb->block_entry = &be;
9851 			error = sbwait(&so->so_snd);
9852 			stcb->block_entry = NULL;
9853 			if (error || so->so_error || be.error) {
9854 				if (error == 0) {
9855 					if (so->so_error)
9856 						error = so->so_error;
9857 					if (be.error) {
9858 						error = be.error;
9859 					}
9860 				}
9861 				SOCKBUF_UNLOCK(&so->so_snd);
9862 				goto out_unlocked;
9863 			}
9864 #ifdef SCTP_BLK_LOGGING
9865 			sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
9866 			    so, asoc, stcb->asoc.total_output_queue_size);
9867 #endif
9868 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
9869 				goto out_unlocked;
9870 			}
9871 		}
9872 		if (so->so_snd.sb_hiwat > stcb->asoc.total_output_queue_size) {
9873 			max_len = so->so_snd.sb_hiwat - stcb->asoc.total_output_queue_size;
9874 		} else {
9875 			max_len = 0;
9876 		}
9877 		SOCKBUF_UNLOCK(&so->so_snd);
9878 	}
9879 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
9880 		goto out_unlocked;
9881 	}
9882 	atomic_add_int(&stcb->total_sends, 1);
9883 	if (top == NULL) {
9884 		struct sctp_stream_queue_pending *sp;
9885 		struct sctp_stream_out *strm;
9886 		uint32_t sndout, initial_out;
9887 		int user_marks_eor;
9888 
9889 		if (uio->uio_resid == 0) {
9890 			if (srcv->sinfo_flags & SCTP_EOF) {
9891 				got_all_of_the_send = 1;
9892 				goto dataless_eof;
9893 			} else {
9894 				error = EINVAL;
9895 				goto out;
9896 			}
9897 		}
9898 		initial_out = uio->uio_resid;
9899 
9900 		if ((asoc->stream_locked) &&
9901 		    (asoc->stream_locked_on != srcv->sinfo_stream)) {
9902 			error = EAGAIN;
9903 			goto out;
9904 		}
9905 		strm = &stcb->asoc.strmout[srcv->sinfo_stream];
9906 		user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
9907 		if (strm->last_msg_incomplete == 0) {
9908 	do_a_copy_in:
9909 			sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
9910 			if ((sp == NULL) || (error)) {
9911 				goto out;
9912 			}
9913 			SCTP_TCB_SEND_LOCK(stcb);
9914 			if (sp->msg_is_complete) {
9915 				strm->last_msg_incomplete = 0;
9916 				asoc->stream_locked = 0;
9917 			} else {
9918 				/*
9919 				 * Just got locked to this guy in case of an
9920 				 * interupt.
9921 				 */
9922 				strm->last_msg_incomplete = 1;
9923 				asoc->stream_locked = 1;
9924 				asoc->stream_locked_on = srcv->sinfo_stream;
9925 			}
9926 			sctp_snd_sb_alloc(stcb, sp->length);
9927 
9928 			asoc->stream_queue_cnt++;
9929 			TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
9930 			if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
9931 				sp->strseq = strm->next_sequence_sent;
9932 #ifdef SCTP_LOG_SENDING_STR
9933 				sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
9934 				    (uintptr_t) stcb, (uintptr_t) sp,
9935 				    (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
9936 #endif
9937 				strm->next_sequence_sent++;
9938 			} else {
9939 				SCTP_STAT_INCR(sctps_sends_with_unord);
9940 			}
9941 
9942 			if ((strm->next_spoke.tqe_next == NULL) &&
9943 			    (strm->next_spoke.tqe_prev == NULL)) {
9944 				/* Not on wheel, insert */
9945 				sctp_insert_on_wheel(stcb, asoc, strm, 1);
9946 			}
9947 			SCTP_TCB_SEND_UNLOCK(stcb);
9948 		} else {
9949 			sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
9950 			if (sp == NULL) {
9951 				/* ???? Huh ??? last msg is gone */
9952 #ifdef INVARIANTS
9953 				panic("Warning: Last msg marked incomplete, yet nothing left?");
9954 #else
9955 				printf("Warning: Last msg marked incomplete, yet nothing left?\n");
9956 				strm->last_msg_incomplete = 0;
9957 #endif
9958 				goto do_a_copy_in;
9959 
9960 			}
9961 		}
9962 		while (uio->uio_resid > 0) {
9963 			/* How much room do we have? */
9964 			struct mbuf *new_tail, *mm;
9965 
9966 			if (so->so_snd.sb_hiwat > stcb->asoc.total_output_queue_size)
9967 				max_len = so->so_snd.sb_hiwat - stcb->asoc.total_output_queue_size;
9968 			else
9969 				max_len = 0;
9970 
9971 			if ((max_len > sctp_add_more_threshold) ||
9972 			    (uio->uio_resid && (uio->uio_resid < max_len))) {
9973 				sndout = 0;
9974 				new_tail = NULL;
9975 				if (hold_tcblock) {
9976 					SCTP_TCB_UNLOCK(stcb);
9977 					hold_tcblock = 0;
9978 				}
9979 				mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
9980 				if ((mm == NULL) || error) {
9981 					if (mm) {
9982 						sctp_m_freem(mm);
9983 					}
9984 					goto out;
9985 				}
9986 				/* Update the mbuf and count */
9987 				SCTP_TCB_SEND_LOCK(stcb);
9988 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
9989 					/*
9990 					 * we need to get out. Peer probably
9991 					 * aborted.
9992 					 */
9993 					sctp_m_freem(mm);
9994 					if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED)
9995 						error = ECONNRESET;
9996 					goto out;
9997 				}
9998 				if (sp->tail_mbuf) {
9999 					/* tack it to the end */
10000 					sp->tail_mbuf->m_next = mm;
10001 					sp->tail_mbuf = new_tail;
10002 				} else {
10003 					/* A stolen mbuf */
10004 					sp->data = mm;
10005 					sp->tail_mbuf = new_tail;
10006 				}
10007 				sctp_snd_sb_alloc(stcb, sndout);
10008 				sp->length += sndout;
10009 				len += sndout;
10010 				/* Did we reach EOR? */
10011 				if ((uio->uio_resid == 0) &&
10012 				    ((user_marks_eor == 0) ||
10013 				    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))
10014 				    ) {
10015 					sp->msg_is_complete = 1;
10016 				} else {
10017 					sp->msg_is_complete = 0;
10018 				}
10019 				if (sp->data->m_flags & M_PKTHDR) {
10020 					/* update length */
10021 					sp->data->m_pkthdr.len = sp->length;
10022 				}
10023 				SCTP_TCB_SEND_UNLOCK(stcb);
10024 			}
10025 			if (uio->uio_resid == 0) {
10026 				/* got it all? */
10027 				continue;
10028 			}
10029 			/* PR-SCTP? */
10030 			if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
10031 				/*
10032 				 * This is ugly but we must assure locking
10033 				 * order
10034 				 */
10035 				if (hold_tcblock == 0) {
10036 					SCTP_TCB_LOCK(stcb);
10037 					hold_tcblock = 1;
10038 				}
10039 				sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
10040 				if (so->so_snd.sb_hiwat > stcb->asoc.total_output_queue_size)
10041 					max_len = so->so_snd.sb_hiwat - stcb->asoc.total_output_queue_size;
10042 				else
10043 					max_len = 0;
10044 				if (max_len > 0) {
10045 					continue;
10046 				}
10047 				SCTP_TCB_UNLOCK(stcb);
10048 				hold_tcblock = 0;
10049 			}
10050 			/* wait for space now */
10051 			if (non_blocking) {
10052 				/* Non-blocking io in place out */
10053 				goto skip_out_eof;
10054 			}
10055 			if ((net->flight_size > net->cwnd) &&
10056 			    (sctp_cmt_on_off == 0)) {
10057 				queue_only = 1;
10058 
10059 			} else if (asoc->ifp_had_enobuf) {
10060 				SCTP_STAT_INCR(sctps_ifnomemqueued);
10061 				if (net->flight_size > (net->mtu * 2)) {
10062 					queue_only = 1;
10063 				} else {
10064 					queue_only = 0;
10065 				}
10066 				asoc->ifp_had_enobuf = 0;
10067 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
10068 				    ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
10069 				    sizeof(struct sctp_data_chunk)));
10070 			} else {
10071 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
10072 				    ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
10073 				    sizeof(struct sctp_data_chunk)));
10074 				queue_only = 0;
10075 			}
10076 			if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
10077 			    (stcb->asoc.total_flight > 0) &&
10078 			    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
10079 			    ) {
10080 
10081 				/*
10082 				 * Ok, Nagle is set on and we have data
10083 				 * outstanding. Don't send anything and let
10084 				 * SACKs drive out the data unless wen have
10085 				 * a "full" segment to send.
10086 				 */
10087 #ifdef SCTP_NAGLE_LOGGING
10088 				sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
10089 #endif
10090 				SCTP_STAT_INCR(sctps_naglequeued);
10091 				nagle_applies = 1;
10092 			} else {
10093 #ifdef SCTP_NAGLE_LOGGING
10094 				if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
10095 					sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
10096 #endif
10097 				SCTP_STAT_INCR(sctps_naglesent);
10098 				nagle_applies = 0;
10099 			}
10100 			/* What about the INIT, send it maybe */
10101 #ifdef SCTP_BLK_LOGGING
10102 			sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only, nagle_applies, un_sent);
10103 			sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size, stcb->asoc.total_flight,
10104 			    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
10105 #endif
10106 			if (queue_only_for_init) {
10107 				if (hold_tcblock == 0) {
10108 					SCTP_TCB_LOCK(stcb);
10109 					hold_tcblock = 1;
10110 				}
10111 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
10112 					/* a collision took us forward? */
10113 					queue_only_for_init = 0;
10114 					queue_only = 0;
10115 				} else {
10116 					sctp_send_initiate(inp, stcb);
10117 					stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
10118 					queue_only_for_init = 0;
10119 					queue_only = 1;
10120 				}
10121 			}
10122 			if ((queue_only == 0) && (nagle_applies == 0)
10123 			    ) {
10124 				/*
10125 				 * need to start chunk output before
10126 				 * blocking.. note that if a lock is already
10127 				 * applied, then the input via the net is
10128 				 * happening and I don't need to start
10129 				 * output :-D
10130 				 */
10131 				if (hold_tcblock == 0) {
10132 					if (SCTP_TCB_TRYLOCK(stcb)) {
10133 						hold_tcblock = 1;
10134 						sctp_chunk_output(inp,
10135 						    stcb,
10136 						    SCTP_OUTPUT_FROM_USR_SEND);
10137 
10138 					}
10139 				} else {
10140 					sctp_chunk_output(inp,
10141 					    stcb,
10142 					    SCTP_OUTPUT_FROM_USR_SEND);
10143 				}
10144 				if (hold_tcblock == 1) {
10145 					SCTP_TCB_UNLOCK(stcb);
10146 					hold_tcblock = 0;
10147 				}
10148 			}
10149 			SOCKBUF_LOCK(&so->so_snd);
10150 			/*
10151 			 * This is a bit strange, but I think it will work.
10152 			 * The total_output_queue_size is locked and
10153 			 * protected by the TCB_LOCK, which we just
10154 			 * released. There is a race that can occur between
10155 			 * releasing it above, and me getting the socket
10156 			 * lock, where sacks come in but we have not put the
10157 			 * SB_WAIT on the so_snd buffer to get the wakeup.
10158 			 * After the LOCK is applied the sack_processing
10159 			 * will also need to LOCK the so->so_snd to do the
10160 			 * actual sowwakeup(). So once we have the socket
10161 			 * buffer lock if we recheck the size we KNOW we
10162 			 * will get to sleep safely with the wakeup flag in
10163 			 * place.
10164 			 */
10165 			if (so->so_snd.sb_hiwat < (stcb->asoc.total_output_queue_size + sctp_add_more_threshold)) {
10166 #ifdef SCTP_BLK_LOGGING
10167 				sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
10168 				    so, asoc, uio->uio_resid);
10169 #endif
10170 				be.error = 0;
10171 				stcb->block_entry = &be;
10172 				error = sbwait(&so->so_snd);
10173 				stcb->block_entry = NULL;
10174 
10175 				if (error || so->so_error || be.error) {
10176 					if (error == 0) {
10177 						if (so->so_error)
10178 							error = so->so_error;
10179 						if (be.error) {
10180 							error = be.error;
10181 						}
10182 					}
10183 					SOCKBUF_UNLOCK(&so->so_snd);
10184 					goto out_unlocked;
10185 				}
10186 #ifdef SCTP_BLK_LOGGING
10187 				sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
10188 				    so, asoc, stcb->asoc.total_output_queue_size);
10189 #endif
10190 			}
10191 			SOCKBUF_UNLOCK(&so->so_snd);
10192 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
10193 				goto out_unlocked;
10194 			}
10195 		}
10196 		SCTP_TCB_SEND_LOCK(stcb);
10197 		if (sp->msg_is_complete == 0) {
10198 			strm->last_msg_incomplete = 1;
10199 			asoc->stream_locked = 1;
10200 			asoc->stream_locked_on = srcv->sinfo_stream;
10201 		} else {
10202 			strm->last_msg_incomplete = 0;
10203 			asoc->stream_locked = 0;
10204 		}
10205 		SCTP_TCB_SEND_UNLOCK(stcb);
10206 		if (uio->uio_resid == 0) {
10207 			got_all_of_the_send = 1;
10208 		}
10209 	} else if (top) {
10210 		/* We send in a 0, since we do NOT have any locks */
10211 		error = sctp_msg_append(stcb, net, top, srcv, 0);
10212 		top = NULL;
10213 	}
10214 	if (error) {
10215 		goto out;
10216 	}
10217 dataless_eof:
10218 	/* EOF thing ? */
10219 	if ((srcv->sinfo_flags & SCTP_EOF) &&
10220 	    (got_all_of_the_send == 1) &&
10221 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
10222 	    ) {
10223 		SCTP_STAT_INCR(sctps_sends_with_eof);
10224 		error = 0;
10225 		if (hold_tcblock == 0) {
10226 			SCTP_TCB_LOCK(stcb);
10227 			hold_tcblock = 1;
10228 		}
10229 		if (TAILQ_EMPTY(&asoc->send_queue) &&
10230 		    TAILQ_EMPTY(&asoc->sent_queue) &&
10231 		    (asoc->stream_queue_cnt == 0)) {
10232 			if (asoc->locked_on_sending) {
10233 				goto abort_anyway;
10234 			}
10235 			/* there is nothing queued to send, so I'm done... */
10236 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
10237 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
10238 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
10239 				/* only send SHUTDOWN the first time through */
10240 				sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
10241 				asoc->state = SCTP_STATE_SHUTDOWN_SENT;
10242 				SCTP_STAT_DECR_GAUGE32(sctps_currestab);
10243 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
10244 				    asoc->primary_destination);
10245 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
10246 				    asoc->primary_destination);
10247 			}
10248 		} else {
10249 			/*
10250 			 * we still got (or just got) data to send, so set
10251 			 * SHUTDOWN_PENDING
10252 			 */
10253 			/*
10254 			 * XXX sockets draft says that SCTP_EOF should be
10255 			 * sent with no data.  currently, we will allow user
10256 			 * data to be sent first and move to
10257 			 * SHUTDOWN-PENDING
10258 			 */
10259 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
10260 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
10261 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
10262 				if (hold_tcblock == 0) {
10263 					SCTP_TCB_LOCK(stcb);
10264 					hold_tcblock = 1;
10265 				}
10266 				if (asoc->locked_on_sending) {
10267 					/* Locked to send out the data */
10268 					struct sctp_stream_queue_pending *sp;
10269 
10270 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
10271 					if (sp) {
10272 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
10273 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
10274 					}
10275 				}
10276 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
10277 				if (TAILQ_EMPTY(&asoc->send_queue) &&
10278 				    TAILQ_EMPTY(&asoc->sent_queue) &&
10279 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
10280 			abort_anyway:
10281 					if (free_cnt_applied) {
10282 						atomic_add_int(&stcb->asoc.refcnt, -1);
10283 						free_cnt_applied = 0;
10284 					}
10285 					sctp_abort_an_association(stcb->sctp_ep, stcb,
10286 					    SCTP_RESPONSE_TO_USER_REQ,
10287 					    NULL);
10288 					/*
10289 					 * now relock the stcb so everything
10290 					 * is sane
10291 					 */
10292 					hold_tcblock = 0;
10293 					stcb = NULL;
10294 					goto out;
10295 				}
10296 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
10297 				    asoc->primary_destination);
10298 			}
10299 		}
10300 	}
10301 skip_out_eof:
10302 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
10303 		some_on_control = 1;
10304 	}
10305 	if ((net->flight_size > net->cwnd) &&
10306 	    (sctp_cmt_on_off == 0)) {
10307 		queue_only = 1;
10308 	} else if (asoc->ifp_had_enobuf) {
10309 		SCTP_STAT_INCR(sctps_ifnomemqueued);
10310 		if (net->flight_size > (net->mtu * 2)) {
10311 			queue_only = 1;
10312 		} else {
10313 			queue_only = 0;
10314 		}
10315 		asoc->ifp_had_enobuf = 0;
10316 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
10317 		    ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
10318 		    sizeof(struct sctp_data_chunk)));
10319 	} else {
10320 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
10321 		    ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) *
10322 		    sizeof(struct sctp_data_chunk)));
10323 		queue_only = 0;
10324 	}
10325 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
10326 	    (stcb->asoc.total_flight > 0) &&
10327 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
10328 	    ) {
10329 
10330 		/*
10331 		 * Ok, Nagle is set on and we have data outstanding. Don't
10332 		 * send anything and let SACKs drive out the data unless wen
10333 		 * have a "full" segment to send.
10334 		 */
10335 #ifdef SCTP_NAGLE_LOGGING
10336 		sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
10337 #endif
10338 		SCTP_STAT_INCR(sctps_naglequeued);
10339 		nagle_applies = 1;
10340 	} else {
10341 #ifdef SCTP_NAGLE_LOGGING
10342 		if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
10343 			sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
10344 #endif
10345 		SCTP_STAT_INCR(sctps_naglesent);
10346 		nagle_applies = 0;
10347 	}
10348 	if (queue_only_for_init) {
10349 		if (hold_tcblock == 0) {
10350 			SCTP_TCB_LOCK(stcb);
10351 			hold_tcblock = 1;
10352 		}
10353 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
10354 			/* a collision took us forward? */
10355 			queue_only_for_init = 0;
10356 			queue_only = 0;
10357 		} else {
10358 			sctp_send_initiate(inp, stcb);
10359 			stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
10360 			queue_only_for_init = 0;
10361 			queue_only = 1;
10362 		}
10363 	}
10364 	if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
10365 		/* we can attempt to send too. */
10366 		s = splnet();
10367 		if (hold_tcblock == 0) {
10368 			/*
10369 			 * If there is activity recv'ing sacks no need to
10370 			 * send
10371 			 */
10372 			if (SCTP_TCB_TRYLOCK(stcb)) {
10373 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND);
10374 				hold_tcblock = 1;
10375 			}
10376 		} else {
10377 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND);
10378 		}
10379 		splx(s);
10380 	} else if ((queue_only == 0) &&
10381 		    (stcb->asoc.peers_rwnd == 0) &&
10382 	    (stcb->asoc.total_flight == 0)) {
10383 		/* We get to have a probe outstanding */
10384 		if (hold_tcblock == 0) {
10385 			hold_tcblock = 1;
10386 			SCTP_TCB_LOCK(stcb);
10387 		}
10388 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND);
10389 	} else if (some_on_control) {
10390 		int num_out, reason, cwnd_full, frag_point;
10391 
10392 		/* Here we do control only */
10393 		if (hold_tcblock == 0) {
10394 			hold_tcblock = 1;
10395 			SCTP_TCB_LOCK(stcb);
10396 		}
10397 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
10398 		sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
10399 		    &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point);
10400 	}
10401 #ifdef SCTP_DEBUG
10402 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
10403 		printf("USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d \n",
10404 		    queue_only, stcb->asoc.peers_rwnd, un_sent,
10405 		    stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
10406 		    stcb->asoc.total_output_queue_size);
10407 	}
10408 #endif
10409 out:
10410 out_unlocked:
10411 
10412 	if (create_lock_applied) {
10413 		SCTP_ASOC_CREATE_UNLOCK(inp);
10414 		create_lock_applied = 0;
10415 	}
10416 	if ((stcb) && hold_tcblock) {
10417 		SCTP_TCB_UNLOCK(stcb);
10418 	}
10419 	if (stcb && free_cnt_applied) {
10420 		atomic_add_int(&stcb->asoc.refcnt, -1);
10421 	}
10422 #ifdef INVARIANTS
10423 	if (stcb) {
10424 		if (mtx_owned(&stcb->tcb_mtx)) {
10425 			panic("Leaving with tcb mtx owned?");
10426 		}
10427 		if (mtx_owned(&stcb->tcb_send_mtx)) {
10428 			panic("Leaving with tcb send mtx owned?");
10429 		}
10430 	}
10431 #endif
10432 	if (top) {
10433 		sctp_m_freem(top);
10434 	}
10435 	if (control) {
10436 		sctp_m_freem(control);
10437 	}
10438 	return (error);
10439 }
10440 
10441 
10442 /*
10443  * generate an AUTHentication chunk, if required
10444  */
10445 struct mbuf *
10446 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
10447     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
10448     struct sctp_tcb *stcb, uint8_t chunk)
10449 {
10450 	struct mbuf *m_auth;
10451 	struct sctp_auth_chunk *auth;
10452 	int chunk_len;
10453 
10454 	if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
10455 	    (stcb == NULL))
10456 		return (m);
10457 
10458 	/* sysctl disabled auth? */
10459 	if (sctp_auth_disable)
10460 		return (m);
10461 
10462 	/* peer doesn't do auth... */
10463 	if (!stcb->asoc.peer_supports_auth) {
10464 		return (m);
10465 	}
10466 	/* does the requested chunk require auth? */
10467 	if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
10468 		return (m);
10469 	}
10470 	m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 1, M_DONTWAIT, 1, MT_HEADER);
10471 	if (m_auth == NULL) {
10472 		/* no mbuf's */
10473 		return (m);
10474 	}
10475 	/* reserve some space if this will be the first mbuf */
10476 	if (m == NULL)
10477 		m_auth->m_data += SCTP_MIN_OVERHEAD;
10478 	/* fill in the AUTH chunk details */
10479 	auth = mtod(m_auth, struct sctp_auth_chunk *);
10480 	bzero(auth, sizeof(*auth));
10481 	auth->ch.chunk_type = SCTP_AUTHENTICATION;
10482 	auth->ch.chunk_flags = 0;
10483 	chunk_len = sizeof(*auth) +
10484 	    sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
10485 	auth->ch.chunk_length = htons(chunk_len);
10486 	auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
10487 	/* key id and hmac digest will be computed and filled in upon send */
10488 
10489 	/* save the offset where the auth was inserted into the chain */
10490 	if (m != NULL)
10491 		*offset = m->m_pkthdr.len;
10492 	else
10493 		*offset = 0;
10494 
10495 	/* update length and return pointer to the auth chunk */
10496 	m_auth->m_pkthdr.len = m_auth->m_len = chunk_len;
10497 	m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
10498 	if (auth_ret != NULL)
10499 		*auth_ret = auth;
10500 
10501 	return (m);
10502 }
10503