xref: /freebsd/sys/netinet/sctp_output.c (revision d0b2dbfa)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * a) Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  *
14  * b) Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the distribution.
17  *
18  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #include <netinet/sctp_os.h>
37 #include <sys/proc.h>
38 #include <netinet/sctp_var.h>
39 #include <netinet/sctp_sysctl.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctputil.h>
43 #include <netinet/sctp_output.h>
44 #include <netinet/sctp_uio.h>
45 #include <netinet/sctputil.h>
46 #include <netinet/sctp_auth.h>
47 #include <netinet/sctp_timer.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_indata.h>
50 #include <netinet/sctp_bsd_addr.h>
51 #include <netinet/sctp_input.h>
52 #include <netinet/sctp_crc32.h>
53 #include <netinet/sctp_kdtrace.h>
54 #if defined(INET) || defined(INET6)
55 #include <netinet/udp.h>
56 #endif
57 #include <netinet/udp_var.h>
58 #include <machine/in_cksum.h>
59 
60 #define SCTP_MAX_GAPS_INARRAY 4
61 struct sack_track {
62 	uint8_t right_edge;	/* mergable on the right edge */
63 	uint8_t left_edge;	/* mergable on the left edge */
64 	uint8_t num_entries;
65 	uint8_t spare;
66 	struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
67 };
68 
69 const struct sack_track sack_array[256] = {
70 	{0, 0, 0, 0,		/* 0x00 */
71 		{{0, 0},
72 		{0, 0},
73 		{0, 0},
74 		{0, 0}
75 		}
76 	},
77 	{1, 0, 1, 0,		/* 0x01 */
78 		{{0, 0},
79 		{0, 0},
80 		{0, 0},
81 		{0, 0}
82 		}
83 	},
84 	{0, 0, 1, 0,		/* 0x02 */
85 		{{1, 1},
86 		{0, 0},
87 		{0, 0},
88 		{0, 0}
89 		}
90 	},
91 	{1, 0, 1, 0,		/* 0x03 */
92 		{{0, 1},
93 		{0, 0},
94 		{0, 0},
95 		{0, 0}
96 		}
97 	},
98 	{0, 0, 1, 0,		/* 0x04 */
99 		{{2, 2},
100 		{0, 0},
101 		{0, 0},
102 		{0, 0}
103 		}
104 	},
105 	{1, 0, 2, 0,		/* 0x05 */
106 		{{0, 0},
107 		{2, 2},
108 		{0, 0},
109 		{0, 0}
110 		}
111 	},
112 	{0, 0, 1, 0,		/* 0x06 */
113 		{{1, 2},
114 		{0, 0},
115 		{0, 0},
116 		{0, 0}
117 		}
118 	},
119 	{1, 0, 1, 0,		/* 0x07 */
120 		{{0, 2},
121 		{0, 0},
122 		{0, 0},
123 		{0, 0}
124 		}
125 	},
126 	{0, 0, 1, 0,		/* 0x08 */
127 		{{3, 3},
128 		{0, 0},
129 		{0, 0},
130 		{0, 0}
131 		}
132 	},
133 	{1, 0, 2, 0,		/* 0x09 */
134 		{{0, 0},
135 		{3, 3},
136 		{0, 0},
137 		{0, 0}
138 		}
139 	},
140 	{0, 0, 2, 0,		/* 0x0a */
141 		{{1, 1},
142 		{3, 3},
143 		{0, 0},
144 		{0, 0}
145 		}
146 	},
147 	{1, 0, 2, 0,		/* 0x0b */
148 		{{0, 1},
149 		{3, 3},
150 		{0, 0},
151 		{0, 0}
152 		}
153 	},
154 	{0, 0, 1, 0,		/* 0x0c */
155 		{{2, 3},
156 		{0, 0},
157 		{0, 0},
158 		{0, 0}
159 		}
160 	},
161 	{1, 0, 2, 0,		/* 0x0d */
162 		{{0, 0},
163 		{2, 3},
164 		{0, 0},
165 		{0, 0}
166 		}
167 	},
168 	{0, 0, 1, 0,		/* 0x0e */
169 		{{1, 3},
170 		{0, 0},
171 		{0, 0},
172 		{0, 0}
173 		}
174 	},
175 	{1, 0, 1, 0,		/* 0x0f */
176 		{{0, 3},
177 		{0, 0},
178 		{0, 0},
179 		{0, 0}
180 		}
181 	},
182 	{0, 0, 1, 0,		/* 0x10 */
183 		{{4, 4},
184 		{0, 0},
185 		{0, 0},
186 		{0, 0}
187 		}
188 	},
189 	{1, 0, 2, 0,		/* 0x11 */
190 		{{0, 0},
191 		{4, 4},
192 		{0, 0},
193 		{0, 0}
194 		}
195 	},
196 	{0, 0, 2, 0,		/* 0x12 */
197 		{{1, 1},
198 		{4, 4},
199 		{0, 0},
200 		{0, 0}
201 		}
202 	},
203 	{1, 0, 2, 0,		/* 0x13 */
204 		{{0, 1},
205 		{4, 4},
206 		{0, 0},
207 		{0, 0}
208 		}
209 	},
210 	{0, 0, 2, 0,		/* 0x14 */
211 		{{2, 2},
212 		{4, 4},
213 		{0, 0},
214 		{0, 0}
215 		}
216 	},
217 	{1, 0, 3, 0,		/* 0x15 */
218 		{{0, 0},
219 		{2, 2},
220 		{4, 4},
221 		{0, 0}
222 		}
223 	},
224 	{0, 0, 2, 0,		/* 0x16 */
225 		{{1, 2},
226 		{4, 4},
227 		{0, 0},
228 		{0, 0}
229 		}
230 	},
231 	{1, 0, 2, 0,		/* 0x17 */
232 		{{0, 2},
233 		{4, 4},
234 		{0, 0},
235 		{0, 0}
236 		}
237 	},
238 	{0, 0, 1, 0,		/* 0x18 */
239 		{{3, 4},
240 		{0, 0},
241 		{0, 0},
242 		{0, 0}
243 		}
244 	},
245 	{1, 0, 2, 0,		/* 0x19 */
246 		{{0, 0},
247 		{3, 4},
248 		{0, 0},
249 		{0, 0}
250 		}
251 	},
252 	{0, 0, 2, 0,		/* 0x1a */
253 		{{1, 1},
254 		{3, 4},
255 		{0, 0},
256 		{0, 0}
257 		}
258 	},
259 	{1, 0, 2, 0,		/* 0x1b */
260 		{{0, 1},
261 		{3, 4},
262 		{0, 0},
263 		{0, 0}
264 		}
265 	},
266 	{0, 0, 1, 0,		/* 0x1c */
267 		{{2, 4},
268 		{0, 0},
269 		{0, 0},
270 		{0, 0}
271 		}
272 	},
273 	{1, 0, 2, 0,		/* 0x1d */
274 		{{0, 0},
275 		{2, 4},
276 		{0, 0},
277 		{0, 0}
278 		}
279 	},
280 	{0, 0, 1, 0,		/* 0x1e */
281 		{{1, 4},
282 		{0, 0},
283 		{0, 0},
284 		{0, 0}
285 		}
286 	},
287 	{1, 0, 1, 0,		/* 0x1f */
288 		{{0, 4},
289 		{0, 0},
290 		{0, 0},
291 		{0, 0}
292 		}
293 	},
294 	{0, 0, 1, 0,		/* 0x20 */
295 		{{5, 5},
296 		{0, 0},
297 		{0, 0},
298 		{0, 0}
299 		}
300 	},
301 	{1, 0, 2, 0,		/* 0x21 */
302 		{{0, 0},
303 		{5, 5},
304 		{0, 0},
305 		{0, 0}
306 		}
307 	},
308 	{0, 0, 2, 0,		/* 0x22 */
309 		{{1, 1},
310 		{5, 5},
311 		{0, 0},
312 		{0, 0}
313 		}
314 	},
315 	{1, 0, 2, 0,		/* 0x23 */
316 		{{0, 1},
317 		{5, 5},
318 		{0, 0},
319 		{0, 0}
320 		}
321 	},
322 	{0, 0, 2, 0,		/* 0x24 */
323 		{{2, 2},
324 		{5, 5},
325 		{0, 0},
326 		{0, 0}
327 		}
328 	},
329 	{1, 0, 3, 0,		/* 0x25 */
330 		{{0, 0},
331 		{2, 2},
332 		{5, 5},
333 		{0, 0}
334 		}
335 	},
336 	{0, 0, 2, 0,		/* 0x26 */
337 		{{1, 2},
338 		{5, 5},
339 		{0, 0},
340 		{0, 0}
341 		}
342 	},
343 	{1, 0, 2, 0,		/* 0x27 */
344 		{{0, 2},
345 		{5, 5},
346 		{0, 0},
347 		{0, 0}
348 		}
349 	},
350 	{0, 0, 2, 0,		/* 0x28 */
351 		{{3, 3},
352 		{5, 5},
353 		{0, 0},
354 		{0, 0}
355 		}
356 	},
357 	{1, 0, 3, 0,		/* 0x29 */
358 		{{0, 0},
359 		{3, 3},
360 		{5, 5},
361 		{0, 0}
362 		}
363 	},
364 	{0, 0, 3, 0,		/* 0x2a */
365 		{{1, 1},
366 		{3, 3},
367 		{5, 5},
368 		{0, 0}
369 		}
370 	},
371 	{1, 0, 3, 0,		/* 0x2b */
372 		{{0, 1},
373 		{3, 3},
374 		{5, 5},
375 		{0, 0}
376 		}
377 	},
378 	{0, 0, 2, 0,		/* 0x2c */
379 		{{2, 3},
380 		{5, 5},
381 		{0, 0},
382 		{0, 0}
383 		}
384 	},
385 	{1, 0, 3, 0,		/* 0x2d */
386 		{{0, 0},
387 		{2, 3},
388 		{5, 5},
389 		{0, 0}
390 		}
391 	},
392 	{0, 0, 2, 0,		/* 0x2e */
393 		{{1, 3},
394 		{5, 5},
395 		{0, 0},
396 		{0, 0}
397 		}
398 	},
399 	{1, 0, 2, 0,		/* 0x2f */
400 		{{0, 3},
401 		{5, 5},
402 		{0, 0},
403 		{0, 0}
404 		}
405 	},
406 	{0, 0, 1, 0,		/* 0x30 */
407 		{{4, 5},
408 		{0, 0},
409 		{0, 0},
410 		{0, 0}
411 		}
412 	},
413 	{1, 0, 2, 0,		/* 0x31 */
414 		{{0, 0},
415 		{4, 5},
416 		{0, 0},
417 		{0, 0}
418 		}
419 	},
420 	{0, 0, 2, 0,		/* 0x32 */
421 		{{1, 1},
422 		{4, 5},
423 		{0, 0},
424 		{0, 0}
425 		}
426 	},
427 	{1, 0, 2, 0,		/* 0x33 */
428 		{{0, 1},
429 		{4, 5},
430 		{0, 0},
431 		{0, 0}
432 		}
433 	},
434 	{0, 0, 2, 0,		/* 0x34 */
435 		{{2, 2},
436 		{4, 5},
437 		{0, 0},
438 		{0, 0}
439 		}
440 	},
441 	{1, 0, 3, 0,		/* 0x35 */
442 		{{0, 0},
443 		{2, 2},
444 		{4, 5},
445 		{0, 0}
446 		}
447 	},
448 	{0, 0, 2, 0,		/* 0x36 */
449 		{{1, 2},
450 		{4, 5},
451 		{0, 0},
452 		{0, 0}
453 		}
454 	},
455 	{1, 0, 2, 0,		/* 0x37 */
456 		{{0, 2},
457 		{4, 5},
458 		{0, 0},
459 		{0, 0}
460 		}
461 	},
462 	{0, 0, 1, 0,		/* 0x38 */
463 		{{3, 5},
464 		{0, 0},
465 		{0, 0},
466 		{0, 0}
467 		}
468 	},
469 	{1, 0, 2, 0,		/* 0x39 */
470 		{{0, 0},
471 		{3, 5},
472 		{0, 0},
473 		{0, 0}
474 		}
475 	},
476 	{0, 0, 2, 0,		/* 0x3a */
477 		{{1, 1},
478 		{3, 5},
479 		{0, 0},
480 		{0, 0}
481 		}
482 	},
483 	{1, 0, 2, 0,		/* 0x3b */
484 		{{0, 1},
485 		{3, 5},
486 		{0, 0},
487 		{0, 0}
488 		}
489 	},
490 	{0, 0, 1, 0,		/* 0x3c */
491 		{{2, 5},
492 		{0, 0},
493 		{0, 0},
494 		{0, 0}
495 		}
496 	},
497 	{1, 0, 2, 0,		/* 0x3d */
498 		{{0, 0},
499 		{2, 5},
500 		{0, 0},
501 		{0, 0}
502 		}
503 	},
504 	{0, 0, 1, 0,		/* 0x3e */
505 		{{1, 5},
506 		{0, 0},
507 		{0, 0},
508 		{0, 0}
509 		}
510 	},
511 	{1, 0, 1, 0,		/* 0x3f */
512 		{{0, 5},
513 		{0, 0},
514 		{0, 0},
515 		{0, 0}
516 		}
517 	},
518 	{0, 0, 1, 0,		/* 0x40 */
519 		{{6, 6},
520 		{0, 0},
521 		{0, 0},
522 		{0, 0}
523 		}
524 	},
525 	{1, 0, 2, 0,		/* 0x41 */
526 		{{0, 0},
527 		{6, 6},
528 		{0, 0},
529 		{0, 0}
530 		}
531 	},
532 	{0, 0, 2, 0,		/* 0x42 */
533 		{{1, 1},
534 		{6, 6},
535 		{0, 0},
536 		{0, 0}
537 		}
538 	},
539 	{1, 0, 2, 0,		/* 0x43 */
540 		{{0, 1},
541 		{6, 6},
542 		{0, 0},
543 		{0, 0}
544 		}
545 	},
546 	{0, 0, 2, 0,		/* 0x44 */
547 		{{2, 2},
548 		{6, 6},
549 		{0, 0},
550 		{0, 0}
551 		}
552 	},
553 	{1, 0, 3, 0,		/* 0x45 */
554 		{{0, 0},
555 		{2, 2},
556 		{6, 6},
557 		{0, 0}
558 		}
559 	},
560 	{0, 0, 2, 0,		/* 0x46 */
561 		{{1, 2},
562 		{6, 6},
563 		{0, 0},
564 		{0, 0}
565 		}
566 	},
567 	{1, 0, 2, 0,		/* 0x47 */
568 		{{0, 2},
569 		{6, 6},
570 		{0, 0},
571 		{0, 0}
572 		}
573 	},
574 	{0, 0, 2, 0,		/* 0x48 */
575 		{{3, 3},
576 		{6, 6},
577 		{0, 0},
578 		{0, 0}
579 		}
580 	},
581 	{1, 0, 3, 0,		/* 0x49 */
582 		{{0, 0},
583 		{3, 3},
584 		{6, 6},
585 		{0, 0}
586 		}
587 	},
588 	{0, 0, 3, 0,		/* 0x4a */
589 		{{1, 1},
590 		{3, 3},
591 		{6, 6},
592 		{0, 0}
593 		}
594 	},
595 	{1, 0, 3, 0,		/* 0x4b */
596 		{{0, 1},
597 		{3, 3},
598 		{6, 6},
599 		{0, 0}
600 		}
601 	},
602 	{0, 0, 2, 0,		/* 0x4c */
603 		{{2, 3},
604 		{6, 6},
605 		{0, 0},
606 		{0, 0}
607 		}
608 	},
609 	{1, 0, 3, 0,		/* 0x4d */
610 		{{0, 0},
611 		{2, 3},
612 		{6, 6},
613 		{0, 0}
614 		}
615 	},
616 	{0, 0, 2, 0,		/* 0x4e */
617 		{{1, 3},
618 		{6, 6},
619 		{0, 0},
620 		{0, 0}
621 		}
622 	},
623 	{1, 0, 2, 0,		/* 0x4f */
624 		{{0, 3},
625 		{6, 6},
626 		{0, 0},
627 		{0, 0}
628 		}
629 	},
630 	{0, 0, 2, 0,		/* 0x50 */
631 		{{4, 4},
632 		{6, 6},
633 		{0, 0},
634 		{0, 0}
635 		}
636 	},
637 	{1, 0, 3, 0,		/* 0x51 */
638 		{{0, 0},
639 		{4, 4},
640 		{6, 6},
641 		{0, 0}
642 		}
643 	},
644 	{0, 0, 3, 0,		/* 0x52 */
645 		{{1, 1},
646 		{4, 4},
647 		{6, 6},
648 		{0, 0}
649 		}
650 	},
651 	{1, 0, 3, 0,		/* 0x53 */
652 		{{0, 1},
653 		{4, 4},
654 		{6, 6},
655 		{0, 0}
656 		}
657 	},
658 	{0, 0, 3, 0,		/* 0x54 */
659 		{{2, 2},
660 		{4, 4},
661 		{6, 6},
662 		{0, 0}
663 		}
664 	},
665 	{1, 0, 4, 0,		/* 0x55 */
666 		{{0, 0},
667 		{2, 2},
668 		{4, 4},
669 		{6, 6}
670 		}
671 	},
672 	{0, 0, 3, 0,		/* 0x56 */
673 		{{1, 2},
674 		{4, 4},
675 		{6, 6},
676 		{0, 0}
677 		}
678 	},
679 	{1, 0, 3, 0,		/* 0x57 */
680 		{{0, 2},
681 		{4, 4},
682 		{6, 6},
683 		{0, 0}
684 		}
685 	},
686 	{0, 0, 2, 0,		/* 0x58 */
687 		{{3, 4},
688 		{6, 6},
689 		{0, 0},
690 		{0, 0}
691 		}
692 	},
693 	{1, 0, 3, 0,		/* 0x59 */
694 		{{0, 0},
695 		{3, 4},
696 		{6, 6},
697 		{0, 0}
698 		}
699 	},
700 	{0, 0, 3, 0,		/* 0x5a */
701 		{{1, 1},
702 		{3, 4},
703 		{6, 6},
704 		{0, 0}
705 		}
706 	},
707 	{1, 0, 3, 0,		/* 0x5b */
708 		{{0, 1},
709 		{3, 4},
710 		{6, 6},
711 		{0, 0}
712 		}
713 	},
714 	{0, 0, 2, 0,		/* 0x5c */
715 		{{2, 4},
716 		{6, 6},
717 		{0, 0},
718 		{0, 0}
719 		}
720 	},
721 	{1, 0, 3, 0,		/* 0x5d */
722 		{{0, 0},
723 		{2, 4},
724 		{6, 6},
725 		{0, 0}
726 		}
727 	},
728 	{0, 0, 2, 0,		/* 0x5e */
729 		{{1, 4},
730 		{6, 6},
731 		{0, 0},
732 		{0, 0}
733 		}
734 	},
735 	{1, 0, 2, 0,		/* 0x5f */
736 		{{0, 4},
737 		{6, 6},
738 		{0, 0},
739 		{0, 0}
740 		}
741 	},
742 	{0, 0, 1, 0,		/* 0x60 */
743 		{{5, 6},
744 		{0, 0},
745 		{0, 0},
746 		{0, 0}
747 		}
748 	},
749 	{1, 0, 2, 0,		/* 0x61 */
750 		{{0, 0},
751 		{5, 6},
752 		{0, 0},
753 		{0, 0}
754 		}
755 	},
756 	{0, 0, 2, 0,		/* 0x62 */
757 		{{1, 1},
758 		{5, 6},
759 		{0, 0},
760 		{0, 0}
761 		}
762 	},
763 	{1, 0, 2, 0,		/* 0x63 */
764 		{{0, 1},
765 		{5, 6},
766 		{0, 0},
767 		{0, 0}
768 		}
769 	},
770 	{0, 0, 2, 0,		/* 0x64 */
771 		{{2, 2},
772 		{5, 6},
773 		{0, 0},
774 		{0, 0}
775 		}
776 	},
777 	{1, 0, 3, 0,		/* 0x65 */
778 		{{0, 0},
779 		{2, 2},
780 		{5, 6},
781 		{0, 0}
782 		}
783 	},
784 	{0, 0, 2, 0,		/* 0x66 */
785 		{{1, 2},
786 		{5, 6},
787 		{0, 0},
788 		{0, 0}
789 		}
790 	},
791 	{1, 0, 2, 0,		/* 0x67 */
792 		{{0, 2},
793 		{5, 6},
794 		{0, 0},
795 		{0, 0}
796 		}
797 	},
798 	{0, 0, 2, 0,		/* 0x68 */
799 		{{3, 3},
800 		{5, 6},
801 		{0, 0},
802 		{0, 0}
803 		}
804 	},
805 	{1, 0, 3, 0,		/* 0x69 */
806 		{{0, 0},
807 		{3, 3},
808 		{5, 6},
809 		{0, 0}
810 		}
811 	},
812 	{0, 0, 3, 0,		/* 0x6a */
813 		{{1, 1},
814 		{3, 3},
815 		{5, 6},
816 		{0, 0}
817 		}
818 	},
819 	{1, 0, 3, 0,		/* 0x6b */
820 		{{0, 1},
821 		{3, 3},
822 		{5, 6},
823 		{0, 0}
824 		}
825 	},
826 	{0, 0, 2, 0,		/* 0x6c */
827 		{{2, 3},
828 		{5, 6},
829 		{0, 0},
830 		{0, 0}
831 		}
832 	},
833 	{1, 0, 3, 0,		/* 0x6d */
834 		{{0, 0},
835 		{2, 3},
836 		{5, 6},
837 		{0, 0}
838 		}
839 	},
840 	{0, 0, 2, 0,		/* 0x6e */
841 		{{1, 3},
842 		{5, 6},
843 		{0, 0},
844 		{0, 0}
845 		}
846 	},
847 	{1, 0, 2, 0,		/* 0x6f */
848 		{{0, 3},
849 		{5, 6},
850 		{0, 0},
851 		{0, 0}
852 		}
853 	},
854 	{0, 0, 1, 0,		/* 0x70 */
855 		{{4, 6},
856 		{0, 0},
857 		{0, 0},
858 		{0, 0}
859 		}
860 	},
861 	{1, 0, 2, 0,		/* 0x71 */
862 		{{0, 0},
863 		{4, 6},
864 		{0, 0},
865 		{0, 0}
866 		}
867 	},
868 	{0, 0, 2, 0,		/* 0x72 */
869 		{{1, 1},
870 		{4, 6},
871 		{0, 0},
872 		{0, 0}
873 		}
874 	},
875 	{1, 0, 2, 0,		/* 0x73 */
876 		{{0, 1},
877 		{4, 6},
878 		{0, 0},
879 		{0, 0}
880 		}
881 	},
882 	{0, 0, 2, 0,		/* 0x74 */
883 		{{2, 2},
884 		{4, 6},
885 		{0, 0},
886 		{0, 0}
887 		}
888 	},
889 	{1, 0, 3, 0,		/* 0x75 */
890 		{{0, 0},
891 		{2, 2},
892 		{4, 6},
893 		{0, 0}
894 		}
895 	},
896 	{0, 0, 2, 0,		/* 0x76 */
897 		{{1, 2},
898 		{4, 6},
899 		{0, 0},
900 		{0, 0}
901 		}
902 	},
903 	{1, 0, 2, 0,		/* 0x77 */
904 		{{0, 2},
905 		{4, 6},
906 		{0, 0},
907 		{0, 0}
908 		}
909 	},
910 	{0, 0, 1, 0,		/* 0x78 */
911 		{{3, 6},
912 		{0, 0},
913 		{0, 0},
914 		{0, 0}
915 		}
916 	},
917 	{1, 0, 2, 0,		/* 0x79 */
918 		{{0, 0},
919 		{3, 6},
920 		{0, 0},
921 		{0, 0}
922 		}
923 	},
924 	{0, 0, 2, 0,		/* 0x7a */
925 		{{1, 1},
926 		{3, 6},
927 		{0, 0},
928 		{0, 0}
929 		}
930 	},
931 	{1, 0, 2, 0,		/* 0x7b */
932 		{{0, 1},
933 		{3, 6},
934 		{0, 0},
935 		{0, 0}
936 		}
937 	},
938 	{0, 0, 1, 0,		/* 0x7c */
939 		{{2, 6},
940 		{0, 0},
941 		{0, 0},
942 		{0, 0}
943 		}
944 	},
945 	{1, 0, 2, 0,		/* 0x7d */
946 		{{0, 0},
947 		{2, 6},
948 		{0, 0},
949 		{0, 0}
950 		}
951 	},
952 	{0, 0, 1, 0,		/* 0x7e */
953 		{{1, 6},
954 		{0, 0},
955 		{0, 0},
956 		{0, 0}
957 		}
958 	},
959 	{1, 0, 1, 0,		/* 0x7f */
960 		{{0, 6},
961 		{0, 0},
962 		{0, 0},
963 		{0, 0}
964 		}
965 	},
966 	{0, 1, 1, 0,		/* 0x80 */
967 		{{7, 7},
968 		{0, 0},
969 		{0, 0},
970 		{0, 0}
971 		}
972 	},
973 	{1, 1, 2, 0,		/* 0x81 */
974 		{{0, 0},
975 		{7, 7},
976 		{0, 0},
977 		{0, 0}
978 		}
979 	},
980 	{0, 1, 2, 0,		/* 0x82 */
981 		{{1, 1},
982 		{7, 7},
983 		{0, 0},
984 		{0, 0}
985 		}
986 	},
987 	{1, 1, 2, 0,		/* 0x83 */
988 		{{0, 1},
989 		{7, 7},
990 		{0, 0},
991 		{0, 0}
992 		}
993 	},
994 	{0, 1, 2, 0,		/* 0x84 */
995 		{{2, 2},
996 		{7, 7},
997 		{0, 0},
998 		{0, 0}
999 		}
1000 	},
1001 	{1, 1, 3, 0,		/* 0x85 */
1002 		{{0, 0},
1003 		{2, 2},
1004 		{7, 7},
1005 		{0, 0}
1006 		}
1007 	},
1008 	{0, 1, 2, 0,		/* 0x86 */
1009 		{{1, 2},
1010 		{7, 7},
1011 		{0, 0},
1012 		{0, 0}
1013 		}
1014 	},
1015 	{1, 1, 2, 0,		/* 0x87 */
1016 		{{0, 2},
1017 		{7, 7},
1018 		{0, 0},
1019 		{0, 0}
1020 		}
1021 	},
1022 	{0, 1, 2, 0,		/* 0x88 */
1023 		{{3, 3},
1024 		{7, 7},
1025 		{0, 0},
1026 		{0, 0}
1027 		}
1028 	},
1029 	{1, 1, 3, 0,		/* 0x89 */
1030 		{{0, 0},
1031 		{3, 3},
1032 		{7, 7},
1033 		{0, 0}
1034 		}
1035 	},
1036 	{0, 1, 3, 0,		/* 0x8a */
1037 		{{1, 1},
1038 		{3, 3},
1039 		{7, 7},
1040 		{0, 0}
1041 		}
1042 	},
1043 	{1, 1, 3, 0,		/* 0x8b */
1044 		{{0, 1},
1045 		{3, 3},
1046 		{7, 7},
1047 		{0, 0}
1048 		}
1049 	},
1050 	{0, 1, 2, 0,		/* 0x8c */
1051 		{{2, 3},
1052 		{7, 7},
1053 		{0, 0},
1054 		{0, 0}
1055 		}
1056 	},
1057 	{1, 1, 3, 0,		/* 0x8d */
1058 		{{0, 0},
1059 		{2, 3},
1060 		{7, 7},
1061 		{0, 0}
1062 		}
1063 	},
1064 	{0, 1, 2, 0,		/* 0x8e */
1065 		{{1, 3},
1066 		{7, 7},
1067 		{0, 0},
1068 		{0, 0}
1069 		}
1070 	},
1071 	{1, 1, 2, 0,		/* 0x8f */
1072 		{{0, 3},
1073 		{7, 7},
1074 		{0, 0},
1075 		{0, 0}
1076 		}
1077 	},
1078 	{0, 1, 2, 0,		/* 0x90 */
1079 		{{4, 4},
1080 		{7, 7},
1081 		{0, 0},
1082 		{0, 0}
1083 		}
1084 	},
1085 	{1, 1, 3, 0,		/* 0x91 */
1086 		{{0, 0},
1087 		{4, 4},
1088 		{7, 7},
1089 		{0, 0}
1090 		}
1091 	},
1092 	{0, 1, 3, 0,		/* 0x92 */
1093 		{{1, 1},
1094 		{4, 4},
1095 		{7, 7},
1096 		{0, 0}
1097 		}
1098 	},
1099 	{1, 1, 3, 0,		/* 0x93 */
1100 		{{0, 1},
1101 		{4, 4},
1102 		{7, 7},
1103 		{0, 0}
1104 		}
1105 	},
1106 	{0, 1, 3, 0,		/* 0x94 */
1107 		{{2, 2},
1108 		{4, 4},
1109 		{7, 7},
1110 		{0, 0}
1111 		}
1112 	},
1113 	{1, 1, 4, 0,		/* 0x95 */
1114 		{{0, 0},
1115 		{2, 2},
1116 		{4, 4},
1117 		{7, 7}
1118 		}
1119 	},
1120 	{0, 1, 3, 0,		/* 0x96 */
1121 		{{1, 2},
1122 		{4, 4},
1123 		{7, 7},
1124 		{0, 0}
1125 		}
1126 	},
1127 	{1, 1, 3, 0,		/* 0x97 */
1128 		{{0, 2},
1129 		{4, 4},
1130 		{7, 7},
1131 		{0, 0}
1132 		}
1133 	},
1134 	{0, 1, 2, 0,		/* 0x98 */
1135 		{{3, 4},
1136 		{7, 7},
1137 		{0, 0},
1138 		{0, 0}
1139 		}
1140 	},
1141 	{1, 1, 3, 0,		/* 0x99 */
1142 		{{0, 0},
1143 		{3, 4},
1144 		{7, 7},
1145 		{0, 0}
1146 		}
1147 	},
1148 	{0, 1, 3, 0,		/* 0x9a */
1149 		{{1, 1},
1150 		{3, 4},
1151 		{7, 7},
1152 		{0, 0}
1153 		}
1154 	},
1155 	{1, 1, 3, 0,		/* 0x9b */
1156 		{{0, 1},
1157 		{3, 4},
1158 		{7, 7},
1159 		{0, 0}
1160 		}
1161 	},
1162 	{0, 1, 2, 0,		/* 0x9c */
1163 		{{2, 4},
1164 		{7, 7},
1165 		{0, 0},
1166 		{0, 0}
1167 		}
1168 	},
1169 	{1, 1, 3, 0,		/* 0x9d */
1170 		{{0, 0},
1171 		{2, 4},
1172 		{7, 7},
1173 		{0, 0}
1174 		}
1175 	},
1176 	{0, 1, 2, 0,		/* 0x9e */
1177 		{{1, 4},
1178 		{7, 7},
1179 		{0, 0},
1180 		{0, 0}
1181 		}
1182 	},
1183 	{1, 1, 2, 0,		/* 0x9f */
1184 		{{0, 4},
1185 		{7, 7},
1186 		{0, 0},
1187 		{0, 0}
1188 		}
1189 	},
1190 	{0, 1, 2, 0,		/* 0xa0 */
1191 		{{5, 5},
1192 		{7, 7},
1193 		{0, 0},
1194 		{0, 0}
1195 		}
1196 	},
1197 	{1, 1, 3, 0,		/* 0xa1 */
1198 		{{0, 0},
1199 		{5, 5},
1200 		{7, 7},
1201 		{0, 0}
1202 		}
1203 	},
1204 	{0, 1, 3, 0,		/* 0xa2 */
1205 		{{1, 1},
1206 		{5, 5},
1207 		{7, 7},
1208 		{0, 0}
1209 		}
1210 	},
1211 	{1, 1, 3, 0,		/* 0xa3 */
1212 		{{0, 1},
1213 		{5, 5},
1214 		{7, 7},
1215 		{0, 0}
1216 		}
1217 	},
1218 	{0, 1, 3, 0,		/* 0xa4 */
1219 		{{2, 2},
1220 		{5, 5},
1221 		{7, 7},
1222 		{0, 0}
1223 		}
1224 	},
1225 	{1, 1, 4, 0,		/* 0xa5 */
1226 		{{0, 0},
1227 		{2, 2},
1228 		{5, 5},
1229 		{7, 7}
1230 		}
1231 	},
1232 	{0, 1, 3, 0,		/* 0xa6 */
1233 		{{1, 2},
1234 		{5, 5},
1235 		{7, 7},
1236 		{0, 0}
1237 		}
1238 	},
1239 	{1, 1, 3, 0,		/* 0xa7 */
1240 		{{0, 2},
1241 		{5, 5},
1242 		{7, 7},
1243 		{0, 0}
1244 		}
1245 	},
1246 	{0, 1, 3, 0,		/* 0xa8 */
1247 		{{3, 3},
1248 		{5, 5},
1249 		{7, 7},
1250 		{0, 0}
1251 		}
1252 	},
1253 	{1, 1, 4, 0,		/* 0xa9 */
1254 		{{0, 0},
1255 		{3, 3},
1256 		{5, 5},
1257 		{7, 7}
1258 		}
1259 	},
1260 	{0, 1, 4, 0,		/* 0xaa */
1261 		{{1, 1},
1262 		{3, 3},
1263 		{5, 5},
1264 		{7, 7}
1265 		}
1266 	},
1267 	{1, 1, 4, 0,		/* 0xab */
1268 		{{0, 1},
1269 		{3, 3},
1270 		{5, 5},
1271 		{7, 7}
1272 		}
1273 	},
1274 	{0, 1, 3, 0,		/* 0xac */
1275 		{{2, 3},
1276 		{5, 5},
1277 		{7, 7},
1278 		{0, 0}
1279 		}
1280 	},
1281 	{1, 1, 4, 0,		/* 0xad */
1282 		{{0, 0},
1283 		{2, 3},
1284 		{5, 5},
1285 		{7, 7}
1286 		}
1287 	},
1288 	{0, 1, 3, 0,		/* 0xae */
1289 		{{1, 3},
1290 		{5, 5},
1291 		{7, 7},
1292 		{0, 0}
1293 		}
1294 	},
1295 	{1, 1, 3, 0,		/* 0xaf */
1296 		{{0, 3},
1297 		{5, 5},
1298 		{7, 7},
1299 		{0, 0}
1300 		}
1301 	},
1302 	{0, 1, 2, 0,		/* 0xb0 */
1303 		{{4, 5},
1304 		{7, 7},
1305 		{0, 0},
1306 		{0, 0}
1307 		}
1308 	},
1309 	{1, 1, 3, 0,		/* 0xb1 */
1310 		{{0, 0},
1311 		{4, 5},
1312 		{7, 7},
1313 		{0, 0}
1314 		}
1315 	},
1316 	{0, 1, 3, 0,		/* 0xb2 */
1317 		{{1, 1},
1318 		{4, 5},
1319 		{7, 7},
1320 		{0, 0}
1321 		}
1322 	},
1323 	{1, 1, 3, 0,		/* 0xb3 */
1324 		{{0, 1},
1325 		{4, 5},
1326 		{7, 7},
1327 		{0, 0}
1328 		}
1329 	},
1330 	{0, 1, 3, 0,		/* 0xb4 */
1331 		{{2, 2},
1332 		{4, 5},
1333 		{7, 7},
1334 		{0, 0}
1335 		}
1336 	},
1337 	{1, 1, 4, 0,		/* 0xb5 */
1338 		{{0, 0},
1339 		{2, 2},
1340 		{4, 5},
1341 		{7, 7}
1342 		}
1343 	},
1344 	{0, 1, 3, 0,		/* 0xb6 */
1345 		{{1, 2},
1346 		{4, 5},
1347 		{7, 7},
1348 		{0, 0}
1349 		}
1350 	},
1351 	{1, 1, 3, 0,		/* 0xb7 */
1352 		{{0, 2},
1353 		{4, 5},
1354 		{7, 7},
1355 		{0, 0}
1356 		}
1357 	},
1358 	{0, 1, 2, 0,		/* 0xb8 */
1359 		{{3, 5},
1360 		{7, 7},
1361 		{0, 0},
1362 		{0, 0}
1363 		}
1364 	},
1365 	{1, 1, 3, 0,		/* 0xb9 */
1366 		{{0, 0},
1367 		{3, 5},
1368 		{7, 7},
1369 		{0, 0}
1370 		}
1371 	},
1372 	{0, 1, 3, 0,		/* 0xba */
1373 		{{1, 1},
1374 		{3, 5},
1375 		{7, 7},
1376 		{0, 0}
1377 		}
1378 	},
1379 	{1, 1, 3, 0,		/* 0xbb */
1380 		{{0, 1},
1381 		{3, 5},
1382 		{7, 7},
1383 		{0, 0}
1384 		}
1385 	},
1386 	{0, 1, 2, 0,		/* 0xbc */
1387 		{{2, 5},
1388 		{7, 7},
1389 		{0, 0},
1390 		{0, 0}
1391 		}
1392 	},
1393 	{1, 1, 3, 0,		/* 0xbd */
1394 		{{0, 0},
1395 		{2, 5},
1396 		{7, 7},
1397 		{0, 0}
1398 		}
1399 	},
1400 	{0, 1, 2, 0,		/* 0xbe */
1401 		{{1, 5},
1402 		{7, 7},
1403 		{0, 0},
1404 		{0, 0}
1405 		}
1406 	},
1407 	{1, 1, 2, 0,		/* 0xbf */
1408 		{{0, 5},
1409 		{7, 7},
1410 		{0, 0},
1411 		{0, 0}
1412 		}
1413 	},
1414 	{0, 1, 1, 0,		/* 0xc0 */
1415 		{{6, 7},
1416 		{0, 0},
1417 		{0, 0},
1418 		{0, 0}
1419 		}
1420 	},
1421 	{1, 1, 2, 0,		/* 0xc1 */
1422 		{{0, 0},
1423 		{6, 7},
1424 		{0, 0},
1425 		{0, 0}
1426 		}
1427 	},
1428 	{0, 1, 2, 0,		/* 0xc2 */
1429 		{{1, 1},
1430 		{6, 7},
1431 		{0, 0},
1432 		{0, 0}
1433 		}
1434 	},
1435 	{1, 1, 2, 0,		/* 0xc3 */
1436 		{{0, 1},
1437 		{6, 7},
1438 		{0, 0},
1439 		{0, 0}
1440 		}
1441 	},
1442 	{0, 1, 2, 0,		/* 0xc4 */
1443 		{{2, 2},
1444 		{6, 7},
1445 		{0, 0},
1446 		{0, 0}
1447 		}
1448 	},
1449 	{1, 1, 3, 0,		/* 0xc5 */
1450 		{{0, 0},
1451 		{2, 2},
1452 		{6, 7},
1453 		{0, 0}
1454 		}
1455 	},
1456 	{0, 1, 2, 0,		/* 0xc6 */
1457 		{{1, 2},
1458 		{6, 7},
1459 		{0, 0},
1460 		{0, 0}
1461 		}
1462 	},
1463 	{1, 1, 2, 0,		/* 0xc7 */
1464 		{{0, 2},
1465 		{6, 7},
1466 		{0, 0},
1467 		{0, 0}
1468 		}
1469 	},
1470 	{0, 1, 2, 0,		/* 0xc8 */
1471 		{{3, 3},
1472 		{6, 7},
1473 		{0, 0},
1474 		{0, 0}
1475 		}
1476 	},
1477 	{1, 1, 3, 0,		/* 0xc9 */
1478 		{{0, 0},
1479 		{3, 3},
1480 		{6, 7},
1481 		{0, 0}
1482 		}
1483 	},
1484 	{0, 1, 3, 0,		/* 0xca */
1485 		{{1, 1},
1486 		{3, 3},
1487 		{6, 7},
1488 		{0, 0}
1489 		}
1490 	},
1491 	{1, 1, 3, 0,		/* 0xcb */
1492 		{{0, 1},
1493 		{3, 3},
1494 		{6, 7},
1495 		{0, 0}
1496 		}
1497 	},
1498 	{0, 1, 2, 0,		/* 0xcc */
1499 		{{2, 3},
1500 		{6, 7},
1501 		{0, 0},
1502 		{0, 0}
1503 		}
1504 	},
1505 	{1, 1, 3, 0,		/* 0xcd */
1506 		{{0, 0},
1507 		{2, 3},
1508 		{6, 7},
1509 		{0, 0}
1510 		}
1511 	},
1512 	{0, 1, 2, 0,		/* 0xce */
1513 		{{1, 3},
1514 		{6, 7},
1515 		{0, 0},
1516 		{0, 0}
1517 		}
1518 	},
1519 	{1, 1, 2, 0,		/* 0xcf */
1520 		{{0, 3},
1521 		{6, 7},
1522 		{0, 0},
1523 		{0, 0}
1524 		}
1525 	},
1526 	{0, 1, 2, 0,		/* 0xd0 */
1527 		{{4, 4},
1528 		{6, 7},
1529 		{0, 0},
1530 		{0, 0}
1531 		}
1532 	},
1533 	{1, 1, 3, 0,		/* 0xd1 */
1534 		{{0, 0},
1535 		{4, 4},
1536 		{6, 7},
1537 		{0, 0}
1538 		}
1539 	},
1540 	{0, 1, 3, 0,		/* 0xd2 */
1541 		{{1, 1},
1542 		{4, 4},
1543 		{6, 7},
1544 		{0, 0}
1545 		}
1546 	},
1547 	{1, 1, 3, 0,		/* 0xd3 */
1548 		{{0, 1},
1549 		{4, 4},
1550 		{6, 7},
1551 		{0, 0}
1552 		}
1553 	},
1554 	{0, 1, 3, 0,		/* 0xd4 */
1555 		{{2, 2},
1556 		{4, 4},
1557 		{6, 7},
1558 		{0, 0}
1559 		}
1560 	},
1561 	{1, 1, 4, 0,		/* 0xd5 */
1562 		{{0, 0},
1563 		{2, 2},
1564 		{4, 4},
1565 		{6, 7}
1566 		}
1567 	},
1568 	{0, 1, 3, 0,		/* 0xd6 */
1569 		{{1, 2},
1570 		{4, 4},
1571 		{6, 7},
1572 		{0, 0}
1573 		}
1574 	},
1575 	{1, 1, 3, 0,		/* 0xd7 */
1576 		{{0, 2},
1577 		{4, 4},
1578 		{6, 7},
1579 		{0, 0}
1580 		}
1581 	},
1582 	{0, 1, 2, 0,		/* 0xd8 */
1583 		{{3, 4},
1584 		{6, 7},
1585 		{0, 0},
1586 		{0, 0}
1587 		}
1588 	},
1589 	{1, 1, 3, 0,		/* 0xd9 */
1590 		{{0, 0},
1591 		{3, 4},
1592 		{6, 7},
1593 		{0, 0}
1594 		}
1595 	},
1596 	{0, 1, 3, 0,		/* 0xda */
1597 		{{1, 1},
1598 		{3, 4},
1599 		{6, 7},
1600 		{0, 0}
1601 		}
1602 	},
1603 	{1, 1, 3, 0,		/* 0xdb */
1604 		{{0, 1},
1605 		{3, 4},
1606 		{6, 7},
1607 		{0, 0}
1608 		}
1609 	},
1610 	{0, 1, 2, 0,		/* 0xdc */
1611 		{{2, 4},
1612 		{6, 7},
1613 		{0, 0},
1614 		{0, 0}
1615 		}
1616 	},
1617 	{1, 1, 3, 0,		/* 0xdd */
1618 		{{0, 0},
1619 		{2, 4},
1620 		{6, 7},
1621 		{0, 0}
1622 		}
1623 	},
1624 	{0, 1, 2, 0,		/* 0xde */
1625 		{{1, 4},
1626 		{6, 7},
1627 		{0, 0},
1628 		{0, 0}
1629 		}
1630 	},
1631 	{1, 1, 2, 0,		/* 0xdf */
1632 		{{0, 4},
1633 		{6, 7},
1634 		{0, 0},
1635 		{0, 0}
1636 		}
1637 	},
1638 	{0, 1, 1, 0,		/* 0xe0 */
1639 		{{5, 7},
1640 		{0, 0},
1641 		{0, 0},
1642 		{0, 0}
1643 		}
1644 	},
1645 	{1, 1, 2, 0,		/* 0xe1 */
1646 		{{0, 0},
1647 		{5, 7},
1648 		{0, 0},
1649 		{0, 0}
1650 		}
1651 	},
1652 	{0, 1, 2, 0,		/* 0xe2 */
1653 		{{1, 1},
1654 		{5, 7},
1655 		{0, 0},
1656 		{0, 0}
1657 		}
1658 	},
1659 	{1, 1, 2, 0,		/* 0xe3 */
1660 		{{0, 1},
1661 		{5, 7},
1662 		{0, 0},
1663 		{0, 0}
1664 		}
1665 	},
1666 	{0, 1, 2, 0,		/* 0xe4 */
1667 		{{2, 2},
1668 		{5, 7},
1669 		{0, 0},
1670 		{0, 0}
1671 		}
1672 	},
1673 	{1, 1, 3, 0,		/* 0xe5 */
1674 		{{0, 0},
1675 		{2, 2},
1676 		{5, 7},
1677 		{0, 0}
1678 		}
1679 	},
1680 	{0, 1, 2, 0,		/* 0xe6 */
1681 		{{1, 2},
1682 		{5, 7},
1683 		{0, 0},
1684 		{0, 0}
1685 		}
1686 	},
1687 	{1, 1, 2, 0,		/* 0xe7 */
1688 		{{0, 2},
1689 		{5, 7},
1690 		{0, 0},
1691 		{0, 0}
1692 		}
1693 	},
1694 	{0, 1, 2, 0,		/* 0xe8 */
1695 		{{3, 3},
1696 		{5, 7},
1697 		{0, 0},
1698 		{0, 0}
1699 		}
1700 	},
1701 	{1, 1, 3, 0,		/* 0xe9 */
1702 		{{0, 0},
1703 		{3, 3},
1704 		{5, 7},
1705 		{0, 0}
1706 		}
1707 	},
1708 	{0, 1, 3, 0,		/* 0xea */
1709 		{{1, 1},
1710 		{3, 3},
1711 		{5, 7},
1712 		{0, 0}
1713 		}
1714 	},
1715 	{1, 1, 3, 0,		/* 0xeb */
1716 		{{0, 1},
1717 		{3, 3},
1718 		{5, 7},
1719 		{0, 0}
1720 		}
1721 	},
1722 	{0, 1, 2, 0,		/* 0xec */
1723 		{{2, 3},
1724 		{5, 7},
1725 		{0, 0},
1726 		{0, 0}
1727 		}
1728 	},
1729 	{1, 1, 3, 0,		/* 0xed */
1730 		{{0, 0},
1731 		{2, 3},
1732 		{5, 7},
1733 		{0, 0}
1734 		}
1735 	},
1736 	{0, 1, 2, 0,		/* 0xee */
1737 		{{1, 3},
1738 		{5, 7},
1739 		{0, 0},
1740 		{0, 0}
1741 		}
1742 	},
1743 	{1, 1, 2, 0,		/* 0xef */
1744 		{{0, 3},
1745 		{5, 7},
1746 		{0, 0},
1747 		{0, 0}
1748 		}
1749 	},
1750 	{0, 1, 1, 0,		/* 0xf0 */
1751 		{{4, 7},
1752 		{0, 0},
1753 		{0, 0},
1754 		{0, 0}
1755 		}
1756 	},
1757 	{1, 1, 2, 0,		/* 0xf1 */
1758 		{{0, 0},
1759 		{4, 7},
1760 		{0, 0},
1761 		{0, 0}
1762 		}
1763 	},
1764 	{0, 1, 2, 0,		/* 0xf2 */
1765 		{{1, 1},
1766 		{4, 7},
1767 		{0, 0},
1768 		{0, 0}
1769 		}
1770 	},
1771 	{1, 1, 2, 0,		/* 0xf3 */
1772 		{{0, 1},
1773 		{4, 7},
1774 		{0, 0},
1775 		{0, 0}
1776 		}
1777 	},
1778 	{0, 1, 2, 0,		/* 0xf4 */
1779 		{{2, 2},
1780 		{4, 7},
1781 		{0, 0},
1782 		{0, 0}
1783 		}
1784 	},
1785 	{1, 1, 3, 0,		/* 0xf5 */
1786 		{{0, 0},
1787 		{2, 2},
1788 		{4, 7},
1789 		{0, 0}
1790 		}
1791 	},
1792 	{0, 1, 2, 0,		/* 0xf6 */
1793 		{{1, 2},
1794 		{4, 7},
1795 		{0, 0},
1796 		{0, 0}
1797 		}
1798 	},
1799 	{1, 1, 2, 0,		/* 0xf7 */
1800 		{{0, 2},
1801 		{4, 7},
1802 		{0, 0},
1803 		{0, 0}
1804 		}
1805 	},
1806 	{0, 1, 1, 0,		/* 0xf8 */
1807 		{{3, 7},
1808 		{0, 0},
1809 		{0, 0},
1810 		{0, 0}
1811 		}
1812 	},
1813 	{1, 1, 2, 0,		/* 0xf9 */
1814 		{{0, 0},
1815 		{3, 7},
1816 		{0, 0},
1817 		{0, 0}
1818 		}
1819 	},
1820 	{0, 1, 2, 0,		/* 0xfa */
1821 		{{1, 1},
1822 		{3, 7},
1823 		{0, 0},
1824 		{0, 0}
1825 		}
1826 	},
1827 	{1, 1, 2, 0,		/* 0xfb */
1828 		{{0, 1},
1829 		{3, 7},
1830 		{0, 0},
1831 		{0, 0}
1832 		}
1833 	},
1834 	{0, 1, 1, 0,		/* 0xfc */
1835 		{{2, 7},
1836 		{0, 0},
1837 		{0, 0},
1838 		{0, 0}
1839 		}
1840 	},
1841 	{1, 1, 2, 0,		/* 0xfd */
1842 		{{0, 0},
1843 		{2, 7},
1844 		{0, 0},
1845 		{0, 0}
1846 		}
1847 	},
1848 	{0, 1, 1, 0,		/* 0xfe */
1849 		{{1, 7},
1850 		{0, 0},
1851 		{0, 0},
1852 		{0, 0}
1853 		}
1854 	},
1855 	{1, 1, 1, 0,		/* 0xff */
1856 		{{0, 7},
1857 		{0, 0},
1858 		{0, 0},
1859 		{0, 0}
1860 		}
1861 	}
1862 };
1863 
1864 int
1865 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1866     struct sctp_scoping *scope,
1867     int do_update)
1868 {
1869 	if ((scope->loopback_scope == 0) &&
1870 	    (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1871 		/*
1872 		 * skip loopback if not in scope *
1873 		 */
1874 		return (0);
1875 	}
1876 	switch (ifa->address.sa.sa_family) {
1877 #ifdef INET
1878 	case AF_INET:
1879 		if (scope->ipv4_addr_legal) {
1880 			struct sockaddr_in *sin;
1881 
1882 			sin = &ifa->address.sin;
1883 			if (sin->sin_addr.s_addr == 0) {
1884 				/* not in scope , unspecified */
1885 				return (0);
1886 			}
1887 			if ((scope->ipv4_local_scope == 0) &&
1888 			    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1889 				/* private address not in scope */
1890 				return (0);
1891 			}
1892 		} else {
1893 			return (0);
1894 		}
1895 		break;
1896 #endif
1897 #ifdef INET6
1898 	case AF_INET6:
1899 		if (scope->ipv6_addr_legal) {
1900 			struct sockaddr_in6 *sin6;
1901 
1902 			/*
1903 			 * Must update the flags,  bummer, which means any
1904 			 * IFA locks must now be applied HERE <->
1905 			 */
1906 			if (do_update) {
1907 				sctp_gather_internal_ifa_flags(ifa);
1908 			}
1909 			if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1910 				return (0);
1911 			}
1912 			/* ok to use deprecated addresses? */
1913 			sin6 = &ifa->address.sin6;
1914 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1915 				/* skip unspecified addresses */
1916 				return (0);
1917 			}
1918 			if (	/* (local_scope == 0) && */
1919 			    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1920 				return (0);
1921 			}
1922 			if ((scope->site_scope == 0) &&
1923 			    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1924 				return (0);
1925 			}
1926 		} else {
1927 			return (0);
1928 		}
1929 		break;
1930 #endif
1931 	default:
1932 		return (0);
1933 	}
1934 	return (1);
1935 }
1936 
1937 static struct mbuf *
1938 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa, uint16_t *len)
1939 {
1940 #if defined(INET) || defined(INET6)
1941 	struct sctp_paramhdr *paramh;
1942 	struct mbuf *mret;
1943 	uint16_t plen;
1944 #endif
1945 
1946 	switch (ifa->address.sa.sa_family) {
1947 #ifdef INET
1948 	case AF_INET:
1949 		plen = (uint16_t)sizeof(struct sctp_ipv4addr_param);
1950 		break;
1951 #endif
1952 #ifdef INET6
1953 	case AF_INET6:
1954 		plen = (uint16_t)sizeof(struct sctp_ipv6addr_param);
1955 		break;
1956 #endif
1957 	default:
1958 		return (m);
1959 	}
1960 #if defined(INET) || defined(INET6)
1961 	if (M_TRAILINGSPACE(m) >= plen) {
1962 		/* easy side we just drop it on the end */
1963 		paramh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1964 		mret = m;
1965 	} else {
1966 		/* Need more space */
1967 		mret = m;
1968 		while (SCTP_BUF_NEXT(mret) != NULL) {
1969 			mret = SCTP_BUF_NEXT(mret);
1970 		}
1971 		SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(plen, 0, M_NOWAIT, 1, MT_DATA);
1972 		if (SCTP_BUF_NEXT(mret) == NULL) {
1973 			/* We are hosed, can't add more addresses */
1974 			return (m);
1975 		}
1976 		mret = SCTP_BUF_NEXT(mret);
1977 		paramh = mtod(mret, struct sctp_paramhdr *);
1978 	}
1979 	/* now add the parameter */
1980 	switch (ifa->address.sa.sa_family) {
1981 #ifdef INET
1982 	case AF_INET:
1983 		{
1984 			struct sctp_ipv4addr_param *ipv4p;
1985 			struct sockaddr_in *sin;
1986 
1987 			sin = &ifa->address.sin;
1988 			ipv4p = (struct sctp_ipv4addr_param *)paramh;
1989 			paramh->param_type = htons(SCTP_IPV4_ADDRESS);
1990 			paramh->param_length = htons(plen);
1991 			ipv4p->addr = sin->sin_addr.s_addr;
1992 			SCTP_BUF_LEN(mret) += plen;
1993 			break;
1994 		}
1995 #endif
1996 #ifdef INET6
1997 	case AF_INET6:
1998 		{
1999 			struct sctp_ipv6addr_param *ipv6p;
2000 			struct sockaddr_in6 *sin6;
2001 
2002 			sin6 = &ifa->address.sin6;
2003 			ipv6p = (struct sctp_ipv6addr_param *)paramh;
2004 			paramh->param_type = htons(SCTP_IPV6_ADDRESS);
2005 			paramh->param_length = htons(plen);
2006 			memcpy(ipv6p->addr, &sin6->sin6_addr,
2007 			    sizeof(ipv6p->addr));
2008 			/* clear embedded scope in the address */
2009 			in6_clearscope((struct in6_addr *)ipv6p->addr);
2010 			SCTP_BUF_LEN(mret) += plen;
2011 			break;
2012 		}
2013 #endif
2014 	default:
2015 		return (m);
2016 	}
2017 	if (len != NULL) {
2018 		*len += plen;
2019 	}
2020 	return (mret);
2021 #endif
2022 }
2023 
2024 struct mbuf *
2025 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2026     struct sctp_scoping *scope,
2027     struct mbuf *m_at, int cnt_inits_to,
2028     uint16_t *padding_len, uint16_t *chunk_len)
2029 {
2030 	struct sctp_vrf *vrf = NULL;
2031 	int cnt, limit_out = 0, total_count;
2032 	uint32_t vrf_id;
2033 
2034 	vrf_id = inp->def_vrf_id;
2035 	SCTP_IPI_ADDR_RLOCK();
2036 	vrf = sctp_find_vrf(vrf_id);
2037 	if (vrf == NULL) {
2038 		SCTP_IPI_ADDR_RUNLOCK();
2039 		return (m_at);
2040 	}
2041 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2042 		struct sctp_ifa *sctp_ifap;
2043 		struct sctp_ifn *sctp_ifnp;
2044 
2045 		cnt = cnt_inits_to;
2046 		if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2047 			limit_out = 1;
2048 			cnt = SCTP_ADDRESS_LIMIT;
2049 			goto skip_count;
2050 		}
2051 		LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2052 			if ((scope->loopback_scope == 0) &&
2053 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2054 				/*
2055 				 * Skip loopback devices if loopback_scope
2056 				 * not set
2057 				 */
2058 				continue;
2059 			}
2060 			LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2061 #ifdef INET
2062 				if ((sctp_ifap->address.sa.sa_family == AF_INET) &&
2063 				    (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2064 				    &sctp_ifap->address.sin.sin_addr) != 0)) {
2065 					continue;
2066 				}
2067 #endif
2068 #ifdef INET6
2069 				if ((sctp_ifap->address.sa.sa_family == AF_INET6) &&
2070 				    (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2071 				    &sctp_ifap->address.sin6.sin6_addr) != 0)) {
2072 					continue;
2073 				}
2074 #endif
2075 				if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2076 					continue;
2077 				}
2078 				if (sctp_is_address_in_scope(sctp_ifap, scope, 1) == 0) {
2079 					continue;
2080 				}
2081 				cnt++;
2082 				if (cnt > SCTP_ADDRESS_LIMIT) {
2083 					break;
2084 				}
2085 			}
2086 			if (cnt > SCTP_ADDRESS_LIMIT) {
2087 				break;
2088 			}
2089 		}
2090 skip_count:
2091 		if (cnt > 1) {
2092 			total_count = 0;
2093 			LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2094 				cnt = 0;
2095 				if ((scope->loopback_scope == 0) &&
2096 				    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2097 					/*
2098 					 * Skip loopback devices if
2099 					 * loopback_scope not set
2100 					 */
2101 					continue;
2102 				}
2103 				LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2104 #ifdef INET
2105 					if ((sctp_ifap->address.sa.sa_family == AF_INET) &&
2106 					    (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2107 					    &sctp_ifap->address.sin.sin_addr) != 0)) {
2108 						continue;
2109 					}
2110 #endif
2111 #ifdef INET6
2112 					if ((sctp_ifap->address.sa.sa_family == AF_INET6) &&
2113 					    (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2114 					    &sctp_ifap->address.sin6.sin6_addr) != 0)) {
2115 						continue;
2116 					}
2117 #endif
2118 					if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2119 						continue;
2120 					}
2121 					if (sctp_is_address_in_scope(sctp_ifap,
2122 					    scope, 0) == 0) {
2123 						continue;
2124 					}
2125 					if ((chunk_len != NULL) &&
2126 					    (padding_len != NULL) &&
2127 					    (*padding_len > 0)) {
2128 						memset(mtod(m_at, caddr_t)+*chunk_len, 0, *padding_len);
2129 						SCTP_BUF_LEN(m_at) += *padding_len;
2130 						*chunk_len += *padding_len;
2131 						*padding_len = 0;
2132 					}
2133 					m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap, chunk_len);
2134 					if (limit_out) {
2135 						cnt++;
2136 						total_count++;
2137 						if (cnt >= 2) {
2138 							/*
2139 							 * two from each
2140 							 * address
2141 							 */
2142 							break;
2143 						}
2144 						if (total_count > SCTP_ADDRESS_LIMIT) {
2145 							/* No more addresses */
2146 							break;
2147 						}
2148 					}
2149 				}
2150 			}
2151 		}
2152 	} else {
2153 		struct sctp_laddr *laddr;
2154 
2155 		cnt = cnt_inits_to;
2156 		/* First, how many ? */
2157 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2158 			if (laddr->ifa == NULL) {
2159 				continue;
2160 			}
2161 			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2162 				/*
2163 				 * Address being deleted by the system, dont
2164 				 * list.
2165 				 */
2166 				continue;
2167 			if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2168 				/*
2169 				 * Address being deleted on this ep don't
2170 				 * list.
2171 				 */
2172 				continue;
2173 			}
2174 			if (sctp_is_address_in_scope(laddr->ifa,
2175 			    scope, 1) == 0) {
2176 				continue;
2177 			}
2178 			cnt++;
2179 		}
2180 		/*
2181 		 * To get through a NAT we only list addresses if we have
2182 		 * more than one. That way if you just bind a single address
2183 		 * we let the source of the init dictate our address.
2184 		 */
2185 		if (cnt > 1) {
2186 			cnt = cnt_inits_to;
2187 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2188 				if (laddr->ifa == NULL) {
2189 					continue;
2190 				}
2191 				if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
2192 					continue;
2193 				}
2194 				if (sctp_is_address_in_scope(laddr->ifa,
2195 				    scope, 0) == 0) {
2196 					continue;
2197 				}
2198 				if ((chunk_len != NULL) &&
2199 				    (padding_len != NULL) &&
2200 				    (*padding_len > 0)) {
2201 					memset(mtod(m_at, caddr_t)+*chunk_len, 0, *padding_len);
2202 					SCTP_BUF_LEN(m_at) += *padding_len;
2203 					*chunk_len += *padding_len;
2204 					*padding_len = 0;
2205 				}
2206 				m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa, chunk_len);
2207 				cnt++;
2208 				if (cnt >= SCTP_ADDRESS_LIMIT) {
2209 					break;
2210 				}
2211 			}
2212 		}
2213 	}
2214 	SCTP_IPI_ADDR_RUNLOCK();
2215 	return (m_at);
2216 }
2217 
2218 static struct sctp_ifa *
2219 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2220     uint8_t dest_is_loop,
2221     uint8_t dest_is_priv,
2222     sa_family_t fam)
2223 {
2224 	uint8_t dest_is_global = 0;
2225 
2226 	/* dest_is_priv is true if destination is a private address */
2227 	/* dest_is_loop is true if destination is a loopback addresses */
2228 
2229 	/**
2230 	 * Here we determine if its a preferred address. A preferred address
2231 	 * means it is the same scope or higher scope then the destination.
2232 	 * L = loopback, P = private, G = global
2233 	 * -----------------------------------------
2234 	 *    src    |  dest | result
2235 	 *  ----------------------------------------
2236 	 *     L     |    L  |    yes
2237 	 *  -----------------------------------------
2238 	 *     P     |    L  |    yes-v4 no-v6
2239 	 *  -----------------------------------------
2240 	 *     G     |    L  |    yes-v4 no-v6
2241 	 *  -----------------------------------------
2242 	 *     L     |    P  |    no
2243 	 *  -----------------------------------------
2244 	 *     P     |    P  |    yes
2245 	 *  -----------------------------------------
2246 	 *     G     |    P  |    no
2247 	 *   -----------------------------------------
2248 	 *     L     |    G  |    no
2249 	 *   -----------------------------------------
2250 	 *     P     |    G  |    no
2251 	 *    -----------------------------------------
2252 	 *     G     |    G  |    yes
2253 	 *    -----------------------------------------
2254 	 */
2255 
2256 	if (ifa->address.sa.sa_family != fam) {
2257 		/* forget mis-matched family */
2258 		return (NULL);
2259 	}
2260 	if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2261 		dest_is_global = 1;
2262 	}
2263 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2264 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2265 	/* Ok the address may be ok */
2266 #ifdef INET6
2267 	if (fam == AF_INET6) {
2268 		/* ok to use deprecated addresses? no lets not! */
2269 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2270 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2271 			return (NULL);
2272 		}
2273 		if (ifa->src_is_priv && !ifa->src_is_loop) {
2274 			if (dest_is_loop) {
2275 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2276 				return (NULL);
2277 			}
2278 		}
2279 		if (ifa->src_is_glob) {
2280 			if (dest_is_loop) {
2281 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2282 				return (NULL);
2283 			}
2284 		}
2285 	}
2286 #endif
2287 	/*
2288 	 * Now that we know what is what, implement or table this could in
2289 	 * theory be done slicker (it used to be), but this is
2290 	 * straightforward and easier to validate :-)
2291 	 */
2292 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2293 	    ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2294 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2295 	    dest_is_loop, dest_is_priv, dest_is_global);
2296 
2297 	if ((ifa->src_is_loop) && (dest_is_priv)) {
2298 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2299 		return (NULL);
2300 	}
2301 	if ((ifa->src_is_glob) && (dest_is_priv)) {
2302 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2303 		return (NULL);
2304 	}
2305 	if ((ifa->src_is_loop) && (dest_is_global)) {
2306 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2307 		return (NULL);
2308 	}
2309 	if ((ifa->src_is_priv) && (dest_is_global)) {
2310 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2311 		return (NULL);
2312 	}
2313 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2314 	/* its a preferred address */
2315 	return (ifa);
2316 }
2317 
2318 static struct sctp_ifa *
2319 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2320     uint8_t dest_is_loop,
2321     uint8_t dest_is_priv,
2322     sa_family_t fam)
2323 {
2324 	uint8_t dest_is_global = 0;
2325 
2326 	/**
2327 	 * Here we determine if its a acceptable address. A acceptable
2328 	 * address means it is the same scope or higher scope but we can
2329 	 * allow for NAT which means its ok to have a global dest and a
2330 	 * private src.
2331 	 *
2332 	 * L = loopback, P = private, G = global
2333 	 * -----------------------------------------
2334 	 *  src    |  dest | result
2335 	 * -----------------------------------------
2336 	 *   L     |   L   |    yes
2337 	 *  -----------------------------------------
2338 	 *   P     |   L   |    yes-v4 no-v6
2339 	 *  -----------------------------------------
2340 	 *   G     |   L   |    yes
2341 	 * -----------------------------------------
2342 	 *   L     |   P   |    no
2343 	 * -----------------------------------------
2344 	 *   P     |   P   |    yes
2345 	 * -----------------------------------------
2346 	 *   G     |   P   |    yes - May not work
2347 	 * -----------------------------------------
2348 	 *   L     |   G   |    no
2349 	 * -----------------------------------------
2350 	 *   P     |   G   |    yes - May not work
2351 	 * -----------------------------------------
2352 	 *   G     |   G   |    yes
2353 	 * -----------------------------------------
2354 	 */
2355 
2356 	if (ifa->address.sa.sa_family != fam) {
2357 		/* forget non matching family */
2358 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n",
2359 		    ifa->address.sa.sa_family, fam);
2360 		return (NULL);
2361 	}
2362 	/* Ok the address may be ok */
2363 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa);
2364 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n",
2365 	    dest_is_loop, dest_is_priv);
2366 	if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2367 		dest_is_global = 1;
2368 	}
2369 #ifdef INET6
2370 	if (fam == AF_INET6) {
2371 		/* ok to use deprecated addresses? */
2372 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2373 			return (NULL);
2374 		}
2375 		if (ifa->src_is_priv) {
2376 			/* Special case, linklocal to loop */
2377 			if (dest_is_loop)
2378 				return (NULL);
2379 		}
2380 	}
2381 #endif
2382 	/*
2383 	 * Now that we know what is what, implement our table. This could in
2384 	 * theory be done slicker (it used to be), but this is
2385 	 * straightforward and easier to validate :-)
2386 	 */
2387 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n",
2388 	    ifa->src_is_loop,
2389 	    dest_is_priv);
2390 	if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2391 		return (NULL);
2392 	}
2393 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n",
2394 	    ifa->src_is_loop,
2395 	    dest_is_global);
2396 	if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2397 		return (NULL);
2398 	}
2399 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n");
2400 	/* its an acceptable address */
2401 	return (ifa);
2402 }
2403 
2404 int
2405 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2406 {
2407 	struct sctp_laddr *laddr;
2408 
2409 	if (stcb == NULL) {
2410 		/* There are no restrictions, no TCB :-) */
2411 		return (0);
2412 	}
2413 	LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2414 		if (laddr->ifa == NULL) {
2415 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2416 			    __func__);
2417 			continue;
2418 		}
2419 		if (laddr->ifa == ifa) {
2420 			/* Yes it is on the list */
2421 			return (1);
2422 		}
2423 	}
2424 	return (0);
2425 }
2426 
2427 int
2428 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2429 {
2430 	struct sctp_laddr *laddr;
2431 
2432 	if (ifa == NULL)
2433 		return (0);
2434 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2435 		if (laddr->ifa == NULL) {
2436 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2437 			    __func__);
2438 			continue;
2439 		}
2440 		if ((laddr->ifa == ifa) && laddr->action == 0)
2441 			/* same pointer */
2442 			return (1);
2443 	}
2444 	return (0);
2445 }
2446 
2447 static struct sctp_ifa *
2448 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2449     sctp_route_t *ro,
2450     uint32_t vrf_id,
2451     int non_asoc_addr_ok,
2452     uint8_t dest_is_priv,
2453     uint8_t dest_is_loop,
2454     sa_family_t fam)
2455 {
2456 	struct sctp_laddr *laddr, *starting_point;
2457 	void *ifn;
2458 	int resettotop = 0;
2459 	struct sctp_ifn *sctp_ifn;
2460 	struct sctp_ifa *sctp_ifa, *sifa;
2461 	struct sctp_vrf *vrf;
2462 	uint32_t ifn_index;
2463 
2464 	vrf = sctp_find_vrf(vrf_id);
2465 	if (vrf == NULL)
2466 		return (NULL);
2467 
2468 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2469 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2470 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2471 	/*
2472 	 * first question, is the ifn we will emit on in our list, if so, we
2473 	 * want such an address. Note that we first looked for a preferred
2474 	 * address.
2475 	 */
2476 	if (sctp_ifn) {
2477 		/* is a preferred one on the interface we route out? */
2478 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2479 #ifdef INET
2480 			if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2481 			    (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2482 			    &sctp_ifa->address.sin.sin_addr) != 0)) {
2483 				continue;
2484 			}
2485 #endif
2486 #ifdef INET6
2487 			if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2488 			    (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2489 			    &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2490 				continue;
2491 			}
2492 #endif
2493 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2494 			    (non_asoc_addr_ok == 0))
2495 				continue;
2496 			sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2497 			    dest_is_loop,
2498 			    dest_is_priv, fam);
2499 			if (sifa == NULL)
2500 				continue;
2501 			if (sctp_is_addr_in_ep(inp, sifa)) {
2502 				atomic_add_int(&sifa->refcount, 1);
2503 				return (sifa);
2504 			}
2505 		}
2506 	}
2507 	/*
2508 	 * ok, now we now need to find one on the list of the addresses. We
2509 	 * can't get one on the emitting interface so let's find first a
2510 	 * preferred one. If not that an acceptable one otherwise... we
2511 	 * return NULL.
2512 	 */
2513 	starting_point = inp->next_addr_touse;
2514 once_again:
2515 	if (inp->next_addr_touse == NULL) {
2516 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2517 		resettotop = 1;
2518 	}
2519 	for (laddr = inp->next_addr_touse; laddr;
2520 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2521 		if (laddr->ifa == NULL) {
2522 			/* address has been removed */
2523 			continue;
2524 		}
2525 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2526 			/* address is being deleted */
2527 			continue;
2528 		}
2529 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2530 		    dest_is_priv, fam);
2531 		if (sifa == NULL)
2532 			continue;
2533 		atomic_add_int(&sifa->refcount, 1);
2534 		return (sifa);
2535 	}
2536 	if (resettotop == 0) {
2537 		inp->next_addr_touse = NULL;
2538 		goto once_again;
2539 	}
2540 
2541 	inp->next_addr_touse = starting_point;
2542 	resettotop = 0;
2543 once_again_too:
2544 	if (inp->next_addr_touse == NULL) {
2545 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2546 		resettotop = 1;
2547 	}
2548 
2549 	/* ok, what about an acceptable address in the inp */
2550 	for (laddr = inp->next_addr_touse; laddr;
2551 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2552 		if (laddr->ifa == NULL) {
2553 			/* address has been removed */
2554 			continue;
2555 		}
2556 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2557 			/* address is being deleted */
2558 			continue;
2559 		}
2560 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2561 		    dest_is_priv, fam);
2562 		if (sifa == NULL)
2563 			continue;
2564 		atomic_add_int(&sifa->refcount, 1);
2565 		return (sifa);
2566 	}
2567 	if (resettotop == 0) {
2568 		inp->next_addr_touse = NULL;
2569 		goto once_again_too;
2570 	}
2571 
2572 	/*
2573 	 * no address bound can be a source for the destination we are in
2574 	 * trouble
2575 	 */
2576 	return (NULL);
2577 }
2578 
2579 static struct sctp_ifa *
2580 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2581     struct sctp_tcb *stcb,
2582     sctp_route_t *ro,
2583     uint32_t vrf_id,
2584     uint8_t dest_is_priv,
2585     uint8_t dest_is_loop,
2586     int non_asoc_addr_ok,
2587     sa_family_t fam)
2588 {
2589 	struct sctp_laddr *laddr, *starting_point;
2590 	void *ifn;
2591 	struct sctp_ifn *sctp_ifn;
2592 	struct sctp_ifa *sctp_ifa, *sifa;
2593 	uint8_t start_at_beginning = 0;
2594 	struct sctp_vrf *vrf;
2595 	uint32_t ifn_index;
2596 
2597 	/*
2598 	 * first question, is the ifn we will emit on in our list, if so, we
2599 	 * want that one.
2600 	 */
2601 	vrf = sctp_find_vrf(vrf_id);
2602 	if (vrf == NULL)
2603 		return (NULL);
2604 
2605 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2606 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2607 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2608 
2609 	/*
2610 	 * first question, is the ifn we will emit on in our list?  If so,
2611 	 * we want that one. First we look for a preferred. Second, we go
2612 	 * for an acceptable.
2613 	 */
2614 	if (sctp_ifn) {
2615 		/* first try for a preferred address on the ep */
2616 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2617 #ifdef INET
2618 			if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2619 			    (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2620 			    &sctp_ifa->address.sin.sin_addr) != 0)) {
2621 				continue;
2622 			}
2623 #endif
2624 #ifdef INET6
2625 			if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2626 			    (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2627 			    &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2628 				continue;
2629 			}
2630 #endif
2631 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2632 				continue;
2633 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2634 				sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2635 				if (sifa == NULL)
2636 					continue;
2637 				if (((non_asoc_addr_ok == 0) &&
2638 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2639 				    (non_asoc_addr_ok &&
2640 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2641 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2642 					/* on the no-no list */
2643 					continue;
2644 				}
2645 				atomic_add_int(&sifa->refcount, 1);
2646 				return (sifa);
2647 			}
2648 		}
2649 		/* next try for an acceptable address on the ep */
2650 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2651 #ifdef INET
2652 			if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2653 			    (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2654 			    &sctp_ifa->address.sin.sin_addr) != 0)) {
2655 				continue;
2656 			}
2657 #endif
2658 #ifdef INET6
2659 			if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2660 			    (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2661 			    &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2662 				continue;
2663 			}
2664 #endif
2665 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2666 				continue;
2667 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2668 				sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2669 				if (sifa == NULL)
2670 					continue;
2671 				if (((non_asoc_addr_ok == 0) &&
2672 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2673 				    (non_asoc_addr_ok &&
2674 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2675 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2676 					/* on the no-no list */
2677 					continue;
2678 				}
2679 				atomic_add_int(&sifa->refcount, 1);
2680 				return (sifa);
2681 			}
2682 		}
2683 	}
2684 	/*
2685 	 * if we can't find one like that then we must look at all addresses
2686 	 * bound to pick one at first preferable then secondly acceptable.
2687 	 */
2688 	starting_point = stcb->asoc.last_used_address;
2689 sctp_from_the_top:
2690 	if (stcb->asoc.last_used_address == NULL) {
2691 		start_at_beginning = 1;
2692 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2693 	}
2694 	/* search beginning with the last used address */
2695 	for (laddr = stcb->asoc.last_used_address; laddr;
2696 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2697 		if (laddr->ifa == NULL) {
2698 			/* address has been removed */
2699 			continue;
2700 		}
2701 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2702 			/* address is being deleted */
2703 			continue;
2704 		}
2705 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2706 		if (sifa == NULL)
2707 			continue;
2708 		if (((non_asoc_addr_ok == 0) &&
2709 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2710 		    (non_asoc_addr_ok &&
2711 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2712 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2713 			/* on the no-no list */
2714 			continue;
2715 		}
2716 		stcb->asoc.last_used_address = laddr;
2717 		atomic_add_int(&sifa->refcount, 1);
2718 		return (sifa);
2719 	}
2720 	if (start_at_beginning == 0) {
2721 		stcb->asoc.last_used_address = NULL;
2722 		goto sctp_from_the_top;
2723 	}
2724 	/* now try for any higher scope than the destination */
2725 	stcb->asoc.last_used_address = starting_point;
2726 	start_at_beginning = 0;
2727 sctp_from_the_top2:
2728 	if (stcb->asoc.last_used_address == NULL) {
2729 		start_at_beginning = 1;
2730 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2731 	}
2732 	/* search beginning with the last used address */
2733 	for (laddr = stcb->asoc.last_used_address; laddr;
2734 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2735 		if (laddr->ifa == NULL) {
2736 			/* address has been removed */
2737 			continue;
2738 		}
2739 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2740 			/* address is being deleted */
2741 			continue;
2742 		}
2743 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2744 		    dest_is_priv, fam);
2745 		if (sifa == NULL)
2746 			continue;
2747 		if (((non_asoc_addr_ok == 0) &&
2748 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2749 		    (non_asoc_addr_ok &&
2750 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2751 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2752 			/* on the no-no list */
2753 			continue;
2754 		}
2755 		stcb->asoc.last_used_address = laddr;
2756 		atomic_add_int(&sifa->refcount, 1);
2757 		return (sifa);
2758 	}
2759 	if (start_at_beginning == 0) {
2760 		stcb->asoc.last_used_address = NULL;
2761 		goto sctp_from_the_top2;
2762 	}
2763 	return (NULL);
2764 }
2765 
2766 static struct sctp_ifa *
2767 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2768     struct sctp_inpcb *inp,
2769     struct sctp_tcb *stcb,
2770     int non_asoc_addr_ok,
2771     uint8_t dest_is_loop,
2772     uint8_t dest_is_priv,
2773     int addr_wanted,
2774     sa_family_t fam,
2775     sctp_route_t *ro)
2776 {
2777 	struct sctp_ifa *ifa, *sifa;
2778 	int num_eligible_addr = 0;
2779 #ifdef INET6
2780 	struct sockaddr_in6 sin6, lsa6;
2781 
2782 	if (fam == AF_INET6) {
2783 		memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2784 		(void)sa6_recoverscope(&sin6);
2785 	}
2786 #endif				/* INET6 */
2787 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2788 #ifdef INET
2789 		if ((ifa->address.sa.sa_family == AF_INET) &&
2790 		    (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2791 		    &ifa->address.sin.sin_addr) != 0)) {
2792 			continue;
2793 		}
2794 #endif
2795 #ifdef INET6
2796 		if ((ifa->address.sa.sa_family == AF_INET6) &&
2797 		    (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2798 		    &ifa->address.sin6.sin6_addr) != 0)) {
2799 			continue;
2800 		}
2801 #endif
2802 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2803 		    (non_asoc_addr_ok == 0))
2804 			continue;
2805 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2806 		    dest_is_priv, fam);
2807 		if (sifa == NULL)
2808 			continue;
2809 #ifdef INET6
2810 		if (fam == AF_INET6 &&
2811 		    dest_is_loop &&
2812 		    sifa->src_is_loop && sifa->src_is_priv) {
2813 			/*
2814 			 * don't allow fe80::1 to be a src on loop ::1, we
2815 			 * don't list it to the peer so we will get an
2816 			 * abort.
2817 			 */
2818 			continue;
2819 		}
2820 		if (fam == AF_INET6 &&
2821 		    IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2822 		    IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2823 			/*
2824 			 * link-local <-> link-local must belong to the same
2825 			 * scope.
2826 			 */
2827 			memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2828 			(void)sa6_recoverscope(&lsa6);
2829 			if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2830 				continue;
2831 			}
2832 		}
2833 #endif				/* INET6 */
2834 
2835 		/*
2836 		 * Check if the IPv6 address matches to next-hop. In the
2837 		 * mobile case, old IPv6 address may be not deleted from the
2838 		 * interface. Then, the interface has previous and new
2839 		 * addresses.  We should use one corresponding to the
2840 		 * next-hop.  (by micchie)
2841 		 */
2842 #ifdef INET6
2843 		if (stcb && fam == AF_INET6 &&
2844 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2845 			if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro) == 0) {
2846 				continue;
2847 			}
2848 		}
2849 #endif
2850 #ifdef INET
2851 		/* Avoid topologically incorrect IPv4 address */
2852 		if (stcb && fam == AF_INET &&
2853 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2854 			if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2855 				continue;
2856 			}
2857 		}
2858 #endif
2859 		if (stcb) {
2860 			if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
2861 				continue;
2862 			}
2863 			if (((non_asoc_addr_ok == 0) &&
2864 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2865 			    (non_asoc_addr_ok &&
2866 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2867 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2868 				/*
2869 				 * It is restricted for some reason..
2870 				 * probably not yet added.
2871 				 */
2872 				continue;
2873 			}
2874 		}
2875 		if (num_eligible_addr >= addr_wanted) {
2876 			return (sifa);
2877 		}
2878 		num_eligible_addr++;
2879 	}
2880 	return (NULL);
2881 }
2882 
2883 static int
2884 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2885     struct sctp_inpcb *inp,
2886     struct sctp_tcb *stcb,
2887     int non_asoc_addr_ok,
2888     uint8_t dest_is_loop,
2889     uint8_t dest_is_priv,
2890     sa_family_t fam)
2891 {
2892 	struct sctp_ifa *ifa, *sifa;
2893 	int num_eligible_addr = 0;
2894 
2895 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2896 #ifdef INET
2897 		if ((ifa->address.sa.sa_family == AF_INET) &&
2898 		    (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2899 		    &ifa->address.sin.sin_addr) != 0)) {
2900 			continue;
2901 		}
2902 #endif
2903 #ifdef INET6
2904 		if ((ifa->address.sa.sa_family == AF_INET6) &&
2905 		    (stcb != NULL) &&
2906 		    (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2907 		    &ifa->address.sin6.sin6_addr) != 0)) {
2908 			continue;
2909 		}
2910 #endif
2911 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2912 		    (non_asoc_addr_ok == 0)) {
2913 			continue;
2914 		}
2915 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2916 		    dest_is_priv, fam);
2917 		if (sifa == NULL) {
2918 			continue;
2919 		}
2920 		if (stcb) {
2921 			if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
2922 				continue;
2923 			}
2924 			if (((non_asoc_addr_ok == 0) &&
2925 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2926 			    (non_asoc_addr_ok &&
2927 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2928 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2929 				/*
2930 				 * It is restricted for some reason..
2931 				 * probably not yet added.
2932 				 */
2933 				continue;
2934 			}
2935 		}
2936 		num_eligible_addr++;
2937 	}
2938 	return (num_eligible_addr);
2939 }
2940 
2941 static struct sctp_ifa *
2942 sctp_choose_boundall(struct sctp_inpcb *inp,
2943     struct sctp_tcb *stcb,
2944     struct sctp_nets *net,
2945     sctp_route_t *ro,
2946     uint32_t vrf_id,
2947     uint8_t dest_is_priv,
2948     uint8_t dest_is_loop,
2949     int non_asoc_addr_ok,
2950     sa_family_t fam)
2951 {
2952 	int cur_addr_num = 0, num_preferred = 0;
2953 	void *ifn;
2954 	struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2955 	struct sctp_ifa *sctp_ifa, *sifa;
2956 	uint32_t ifn_index;
2957 	struct sctp_vrf *vrf;
2958 #ifdef INET
2959 	int retried = 0;
2960 #endif
2961 
2962 	/*-
2963 	 * For boundall we can use any address in the association.
2964 	 * If non_asoc_addr_ok is set we can use any address (at least in
2965 	 * theory). So we look for preferred addresses first. If we find one,
2966 	 * we use it. Otherwise we next try to get an address on the
2967 	 * interface, which we should be able to do (unless non_asoc_addr_ok
2968 	 * is false and we are routed out that way). In these cases where we
2969 	 * can't use the address of the interface we go through all the
2970 	 * ifn's looking for an address we can use and fill that in. Punting
2971 	 * means we send back address 0, which will probably cause problems
2972 	 * actually since then IP will fill in the address of the route ifn,
2973 	 * which means we probably already rejected it.. i.e. here comes an
2974 	 * abort :-<.
2975 	 */
2976 	vrf = sctp_find_vrf(vrf_id);
2977 	if (vrf == NULL)
2978 		return (NULL);
2979 
2980 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2981 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2982 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn from route:%p ifn_index:%d\n", ifn, ifn_index);
2983 	emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2984 	if (sctp_ifn == NULL) {
2985 		/* ?? We don't have this guy ?? */
2986 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2987 		goto bound_all_plan_b;
2988 	}
2989 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2990 	    ifn_index, sctp_ifn->ifn_name);
2991 
2992 	if (net) {
2993 		cur_addr_num = net->indx_of_eligible_next_to_use;
2994 	}
2995 	num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2996 	    inp, stcb,
2997 	    non_asoc_addr_ok,
2998 	    dest_is_loop,
2999 	    dest_is_priv, fam);
3000 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
3001 	    num_preferred, sctp_ifn->ifn_name);
3002 	if (num_preferred == 0) {
3003 		/*
3004 		 * no eligible addresses, we must use some other interface
3005 		 * address if we can find one.
3006 		 */
3007 		goto bound_all_plan_b;
3008 	}
3009 	/*
3010 	 * Ok we have num_eligible_addr set with how many we can use, this
3011 	 * may vary from call to call due to addresses being deprecated
3012 	 * etc..
3013 	 */
3014 	if (cur_addr_num >= num_preferred) {
3015 		cur_addr_num = 0;
3016 	}
3017 	/*
3018 	 * select the nth address from the list (where cur_addr_num is the
3019 	 * nth) and 0 is the first one, 1 is the second one etc...
3020 	 */
3021 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
3022 
3023 	sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop,
3024 	    dest_is_priv, cur_addr_num, fam, ro);
3025 
3026 	/* if sctp_ifa is NULL something changed??, fall to plan b. */
3027 	if (sctp_ifa) {
3028 		atomic_add_int(&sctp_ifa->refcount, 1);
3029 		if (net) {
3030 			/* save off where the next one we will want */
3031 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3032 		}
3033 		return (sctp_ifa);
3034 	}
3035 	/*
3036 	 * plan_b: Look at all interfaces and find a preferred address. If
3037 	 * no preferred fall through to plan_c.
3038 	 */
3039 bound_all_plan_b:
3040 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
3041 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3042 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
3043 		    sctp_ifn->ifn_name);
3044 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3045 			/* wrong base scope */
3046 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
3047 			continue;
3048 		}
3049 		if ((sctp_ifn == looked_at) && looked_at) {
3050 			/* already looked at this guy */
3051 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
3052 			continue;
3053 		}
3054 		num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok,
3055 		    dest_is_loop, dest_is_priv, fam);
3056 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
3057 		    "Found ifn:%p %d preferred source addresses\n",
3058 		    ifn, num_preferred);
3059 		if (num_preferred == 0) {
3060 			/* None on this interface. */
3061 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "No preferred -- skipping to next\n");
3062 			continue;
3063 		}
3064 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
3065 		    "num preferred:%d on interface:%p cur_addr_num:%d\n",
3066 		    num_preferred, (void *)sctp_ifn, cur_addr_num);
3067 
3068 		/*
3069 		 * Ok we have num_eligible_addr set with how many we can
3070 		 * use, this may vary from call to call due to addresses
3071 		 * being deprecated etc..
3072 		 */
3073 		if (cur_addr_num >= num_preferred) {
3074 			cur_addr_num = 0;
3075 		}
3076 		sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop,
3077 		    dest_is_priv, cur_addr_num, fam, ro);
3078 		if (sifa == NULL)
3079 			continue;
3080 		if (net) {
3081 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3082 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
3083 			    cur_addr_num);
3084 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
3085 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
3086 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
3087 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
3088 		}
3089 		atomic_add_int(&sifa->refcount, 1);
3090 		return (sifa);
3091 	}
3092 #ifdef INET
3093 again_with_private_addresses_allowed:
3094 #endif
3095 	/* plan_c: do we have an acceptable address on the emit interface */
3096 	sifa = NULL;
3097 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
3098 	if (emit_ifn == NULL) {
3099 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jump to Plan D - no emit_ifn\n");
3100 		goto plan_d;
3101 	}
3102 	LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
3103 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", (void *)sctp_ifa);
3104 #ifdef INET
3105 		if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3106 		    (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3107 		    &sctp_ifa->address.sin.sin_addr) != 0)) {
3108 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jailed\n");
3109 			continue;
3110 		}
3111 #endif
3112 #ifdef INET6
3113 		if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3114 		    (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3115 		    &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3116 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jailed\n");
3117 			continue;
3118 		}
3119 #endif
3120 		if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3121 		    (non_asoc_addr_ok == 0)) {
3122 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Defer\n");
3123 			continue;
3124 		}
3125 		sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
3126 		    dest_is_priv, fam);
3127 		if (sifa == NULL) {
3128 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n");
3129 			continue;
3130 		}
3131 		if (stcb) {
3132 			if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) {
3133 				SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n");
3134 				sifa = NULL;
3135 				continue;
3136 			}
3137 			if (((non_asoc_addr_ok == 0) &&
3138 			    (sctp_is_addr_restricted(stcb, sifa))) ||
3139 			    (non_asoc_addr_ok &&
3140 			    (sctp_is_addr_restricted(stcb, sifa)) &&
3141 			    (!sctp_is_addr_pending(stcb, sifa)))) {
3142 				/*
3143 				 * It is restricted for some reason..
3144 				 * probably not yet added.
3145 				 */
3146 				SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its restricted\n");
3147 				sifa = NULL;
3148 				continue;
3149 			}
3150 		}
3151 		atomic_add_int(&sifa->refcount, 1);
3152 		goto out;
3153 	}
3154 plan_d:
3155 	/*
3156 	 * plan_d: We are in trouble. No preferred address on the emit
3157 	 * interface. And not even a preferred address on all interfaces. Go
3158 	 * out and see if we can find an acceptable address somewhere
3159 	 * amongst all interfaces.
3160 	 */
3161 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", (void *)looked_at);
3162 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3163 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3164 			/* wrong base scope */
3165 			continue;
3166 		}
3167 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3168 #ifdef INET
3169 			if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3170 			    (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3171 			    &sctp_ifa->address.sin.sin_addr) != 0)) {
3172 				continue;
3173 			}
3174 #endif
3175 #ifdef INET6
3176 			if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3177 			    (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3178 			    &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3179 				continue;
3180 			}
3181 #endif
3182 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3183 			    (non_asoc_addr_ok == 0))
3184 				continue;
3185 			sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3186 			    dest_is_loop,
3187 			    dest_is_priv, fam);
3188 			if (sifa == NULL)
3189 				continue;
3190 			if (stcb) {
3191 				if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) {
3192 					sifa = NULL;
3193 					continue;
3194 				}
3195 				if (((non_asoc_addr_ok == 0) &&
3196 				    (sctp_is_addr_restricted(stcb, sifa))) ||
3197 				    (non_asoc_addr_ok &&
3198 				    (sctp_is_addr_restricted(stcb, sifa)) &&
3199 				    (!sctp_is_addr_pending(stcb, sifa)))) {
3200 					/*
3201 					 * It is restricted for some
3202 					 * reason.. probably not yet added.
3203 					 */
3204 					sifa = NULL;
3205 					continue;
3206 				}
3207 			}
3208 			goto out;
3209 		}
3210 	}
3211 #ifdef INET
3212 	if (stcb) {
3213 		if ((retried == 0) && (stcb->asoc.scope.ipv4_local_scope == 0)) {
3214 			stcb->asoc.scope.ipv4_local_scope = 1;
3215 			retried = 1;
3216 			goto again_with_private_addresses_allowed;
3217 		} else if (retried == 1) {
3218 			stcb->asoc.scope.ipv4_local_scope = 0;
3219 		}
3220 	}
3221 #endif
3222 out:
3223 #ifdef INET
3224 	if (sifa) {
3225 		if (retried == 1) {
3226 			LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3227 				if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3228 					/* wrong base scope */
3229 					continue;
3230 				}
3231 				LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3232 					struct sctp_ifa *tmp_sifa;
3233 
3234 #ifdef INET
3235 					if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3236 					    (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3237 					    &sctp_ifa->address.sin.sin_addr) != 0)) {
3238 						continue;
3239 					}
3240 #endif
3241 #ifdef INET6
3242 					if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3243 					    (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3244 					    &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3245 						continue;
3246 					}
3247 #endif
3248 					if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3249 					    (non_asoc_addr_ok == 0))
3250 						continue;
3251 					tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3252 					    dest_is_loop,
3253 					    dest_is_priv, fam);
3254 					if (tmp_sifa == NULL) {
3255 						continue;
3256 					}
3257 					if (tmp_sifa == sifa) {
3258 						continue;
3259 					}
3260 					if (stcb) {
3261 						if (sctp_is_address_in_scope(tmp_sifa,
3262 						    &stcb->asoc.scope, 0) == 0) {
3263 							continue;
3264 						}
3265 						if (((non_asoc_addr_ok == 0) &&
3266 						    (sctp_is_addr_restricted(stcb, tmp_sifa))) ||
3267 						    (non_asoc_addr_ok &&
3268 						    (sctp_is_addr_restricted(stcb, tmp_sifa)) &&
3269 						    (!sctp_is_addr_pending(stcb, tmp_sifa)))) {
3270 							/*
3271 							 * It is restricted
3272 							 * for some reason..
3273 							 * probably not yet
3274 							 * added.
3275 							 */
3276 							continue;
3277 						}
3278 					}
3279 					if ((tmp_sifa->address.sin.sin_family == AF_INET) &&
3280 					    (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) {
3281 						sctp_add_local_addr_restricted(stcb, tmp_sifa);
3282 					}
3283 				}
3284 			}
3285 		}
3286 		atomic_add_int(&sifa->refcount, 1);
3287 	}
3288 #endif
3289 	return (sifa);
3290 }
3291 
3292 /* tcb may be NULL */
3293 struct sctp_ifa *
3294 sctp_source_address_selection(struct sctp_inpcb *inp,
3295     struct sctp_tcb *stcb,
3296     sctp_route_t *ro,
3297     struct sctp_nets *net,
3298     int non_asoc_addr_ok, uint32_t vrf_id)
3299 {
3300 	struct sctp_ifa *answer;
3301 	uint8_t dest_is_priv, dest_is_loop;
3302 	sa_family_t fam;
3303 #ifdef INET
3304 	struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3305 #endif
3306 #ifdef INET6
3307 	struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3308 #endif
3309 
3310 	/**
3311 	 * Rules:
3312 	 * - Find the route if needed, cache if I can.
3313 	 * - Look at interface address in route, Is it in the bound list. If so we
3314 	 *   have the best source.
3315 	 * - If not we must rotate amongst the addresses.
3316 	 *
3317 	 * Caveats and issues
3318 	 *
3319 	 * Do we need to pay attention to scope. We can have a private address
3320 	 * or a global address we are sourcing or sending to. So if we draw
3321 	 * it out
3322 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3323 	 * For V4
3324 	 * ------------------------------------------
3325 	 *      source     *      dest  *  result
3326 	 * -----------------------------------------
3327 	 * <a>  Private    *    Global  *  NAT
3328 	 * -----------------------------------------
3329 	 * <b>  Private    *    Private *  No problem
3330 	 * -----------------------------------------
3331 	 * <c>  Global     *    Private *  Huh, How will this work?
3332 	 * -----------------------------------------
3333 	 * <d>  Global     *    Global  *  No Problem
3334 	 *------------------------------------------
3335 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3336 	 * For V6
3337 	 *------------------------------------------
3338 	 *      source     *      dest  *  result
3339 	 * -----------------------------------------
3340 	 * <a>  Linklocal  *    Global  *
3341 	 * -----------------------------------------
3342 	 * <b>  Linklocal  * Linklocal  *  No problem
3343 	 * -----------------------------------------
3344 	 * <c>  Global     * Linklocal  *  Huh, How will this work?
3345 	 * -----------------------------------------
3346 	 * <d>  Global     *    Global  *  No Problem
3347 	 *------------------------------------------
3348 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3349 	 *
3350 	 * And then we add to that what happens if there are multiple addresses
3351 	 * assigned to an interface. Remember the ifa on a ifn is a linked
3352 	 * list of addresses. So one interface can have more than one IP
3353 	 * address. What happens if we have both a private and a global
3354 	 * address? Do we then use context of destination to sort out which
3355 	 * one is best? And what about NAT's sending P->G may get you a NAT
3356 	 * translation, or should you select the G thats on the interface in
3357 	 * preference.
3358 	 *
3359 	 * Decisions:
3360 	 *
3361 	 * - count the number of addresses on the interface.
3362 	 * - if it is one, no problem except case <c>.
3363 	 *   For <a> we will assume a NAT out there.
3364 	 * - if there are more than one, then we need to worry about scope P
3365 	 *   or G. We should prefer G -> G and P -> P if possible.
3366 	 *   Then as a secondary fall back to mixed types G->P being a last
3367 	 *   ditch one.
3368 	 * - The above all works for bound all, but bound specific we need to
3369 	 *   use the same concept but instead only consider the bound
3370 	 *   addresses. If the bound set is NOT assigned to the interface then
3371 	 *   we must use rotation amongst the bound addresses..
3372 	 */
3373 	if (ro->ro_nh == NULL) {
3374 		/*
3375 		 * Need a route to cache.
3376 		 */
3377 		SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
3378 	}
3379 	if (ro->ro_nh == NULL) {
3380 		return (NULL);
3381 	}
3382 	fam = ro->ro_dst.sa_family;
3383 	dest_is_priv = dest_is_loop = 0;
3384 	/* Setup our scopes for the destination */
3385 	switch (fam) {
3386 #ifdef INET
3387 	case AF_INET:
3388 		/* Scope based on outbound address */
3389 		if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3390 			dest_is_loop = 1;
3391 			if (net != NULL) {
3392 				/* mark it as local */
3393 				net->addr_is_local = 1;
3394 			}
3395 		} else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3396 			dest_is_priv = 1;
3397 		}
3398 		break;
3399 #endif
3400 #ifdef INET6
3401 	case AF_INET6:
3402 		/* Scope based on outbound address */
3403 		if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3404 		    SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3405 			/*
3406 			 * If the address is a loopback address, which
3407 			 * consists of "::1" OR "fe80::1%lo0", we are
3408 			 * loopback scope. But we don't use dest_is_priv
3409 			 * (link local addresses).
3410 			 */
3411 			dest_is_loop = 1;
3412 			if (net != NULL) {
3413 				/* mark it as local */
3414 				net->addr_is_local = 1;
3415 			}
3416 		} else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3417 			dest_is_priv = 1;
3418 		}
3419 		break;
3420 #endif
3421 	}
3422 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3423 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst);
3424 	SCTP_IPI_ADDR_RLOCK();
3425 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3426 		/*
3427 		 * Bound all case
3428 		 */
3429 		answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3430 		    dest_is_priv, dest_is_loop,
3431 		    non_asoc_addr_ok, fam);
3432 		SCTP_IPI_ADDR_RUNLOCK();
3433 		return (answer);
3434 	}
3435 	/*
3436 	 * Subset bound case
3437 	 */
3438 	if (stcb) {
3439 		answer = sctp_choose_boundspecific_stcb(inp, stcb, ro,
3440 		    vrf_id, dest_is_priv,
3441 		    dest_is_loop,
3442 		    non_asoc_addr_ok, fam);
3443 	} else {
3444 		answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3445 		    non_asoc_addr_ok,
3446 		    dest_is_priv,
3447 		    dest_is_loop, fam);
3448 	}
3449 	SCTP_IPI_ADDR_RUNLOCK();
3450 	return (answer);
3451 }
3452 
3453 static bool
3454 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, size_t cpsize)
3455 {
3456 	struct cmsghdr cmh;
3457 	struct sctp_sndinfo sndinfo;
3458 	struct sctp_prinfo prinfo;
3459 	struct sctp_authinfo authinfo;
3460 	int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3461 	bool found;
3462 
3463 	/*
3464 	 * Independent of how many mbufs, find the c_type inside the control
3465 	 * structure and copy out the data.
3466 	 */
3467 	found = false;
3468 	tot_len = SCTP_BUF_LEN(control);
3469 	for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3470 		rem_len = tot_len - off;
3471 		if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3472 			/* There is not enough room for one more. */
3473 			return (found);
3474 		}
3475 		m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3476 		if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3477 			/* We dont't have a complete CMSG header. */
3478 			return (found);
3479 		}
3480 		if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3481 			/* We don't have the complete CMSG. */
3482 			return (found);
3483 		}
3484 		cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3485 		cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3486 		if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3487 		    ((c_type == cmh.cmsg_type) ||
3488 		    ((c_type == SCTP_SNDRCV) &&
3489 		    ((cmh.cmsg_type == SCTP_SNDINFO) ||
3490 		    (cmh.cmsg_type == SCTP_PRINFO) ||
3491 		    (cmh.cmsg_type == SCTP_AUTHINFO))))) {
3492 			if (c_type == cmh.cmsg_type) {
3493 				if (cpsize > INT_MAX) {
3494 					return (found);
3495 				}
3496 				if (cmsg_data_len < (int)cpsize) {
3497 					return (found);
3498 				}
3499 				/* It is exactly what we want. Copy it out. */
3500 				m_copydata(control, cmsg_data_off, (int)cpsize, (caddr_t)data);
3501 				return (1);
3502 			} else {
3503 				struct sctp_sndrcvinfo *sndrcvinfo;
3504 
3505 				sndrcvinfo = (struct sctp_sndrcvinfo *)data;
3506 				if (!found) {
3507 					if (cpsize < sizeof(struct sctp_sndrcvinfo)) {
3508 						return (found);
3509 					}
3510 					memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo));
3511 				}
3512 				switch (cmh.cmsg_type) {
3513 				case SCTP_SNDINFO:
3514 					if (cmsg_data_len < (int)sizeof(struct sctp_sndinfo)) {
3515 						return (found);
3516 					}
3517 					m_copydata(control, cmsg_data_off, sizeof(struct sctp_sndinfo), (caddr_t)&sndinfo);
3518 					sndrcvinfo->sinfo_stream = sndinfo.snd_sid;
3519 					sndrcvinfo->sinfo_flags = sndinfo.snd_flags;
3520 					sndrcvinfo->sinfo_ppid = sndinfo.snd_ppid;
3521 					sndrcvinfo->sinfo_context = sndinfo.snd_context;
3522 					sndrcvinfo->sinfo_assoc_id = sndinfo.snd_assoc_id;
3523 					break;
3524 				case SCTP_PRINFO:
3525 					if (cmsg_data_len < (int)sizeof(struct sctp_prinfo)) {
3526 						return (found);
3527 					}
3528 					m_copydata(control, cmsg_data_off, sizeof(struct sctp_prinfo), (caddr_t)&prinfo);
3529 					if (prinfo.pr_policy != SCTP_PR_SCTP_NONE) {
3530 						sndrcvinfo->sinfo_timetolive = prinfo.pr_value;
3531 					} else {
3532 						sndrcvinfo->sinfo_timetolive = 0;
3533 					}
3534 					sndrcvinfo->sinfo_flags |= prinfo.pr_policy;
3535 					break;
3536 				case SCTP_AUTHINFO:
3537 					if (cmsg_data_len < (int)sizeof(struct sctp_authinfo)) {
3538 						return (found);
3539 					}
3540 					m_copydata(control, cmsg_data_off, sizeof(struct sctp_authinfo), (caddr_t)&authinfo);
3541 					sndrcvinfo->sinfo_keynumber_valid = 1;
3542 					sndrcvinfo->sinfo_keynumber = authinfo.auth_keynumber;
3543 					break;
3544 				default:
3545 					return (found);
3546 				}
3547 				found = true;
3548 			}
3549 		}
3550 	}
3551 	return (found);
3552 }
3553 
3554 static int
3555 sctp_process_cmsgs_for_init(struct sctp_tcb *stcb, struct mbuf *control, int *error)
3556 {
3557 	struct cmsghdr cmh;
3558 	struct sctp_initmsg initmsg;
3559 #ifdef INET
3560 	struct sockaddr_in sin;
3561 #endif
3562 #ifdef INET6
3563 	struct sockaddr_in6 sin6;
3564 #endif
3565 	int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3566 
3567 	tot_len = SCTP_BUF_LEN(control);
3568 	for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3569 		rem_len = tot_len - off;
3570 		if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3571 			/* There is not enough room for one more. */
3572 			*error = EINVAL;
3573 			return (1);
3574 		}
3575 		m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3576 		if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3577 			/* We dont't have a complete CMSG header. */
3578 			*error = EINVAL;
3579 			return (1);
3580 		}
3581 		if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3582 			/* We don't have the complete CMSG. */
3583 			*error = EINVAL;
3584 			return (1);
3585 		}
3586 		cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3587 		cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3588 		if (cmh.cmsg_level == IPPROTO_SCTP) {
3589 			switch (cmh.cmsg_type) {
3590 			case SCTP_INIT:
3591 				if (cmsg_data_len < (int)sizeof(struct sctp_initmsg)) {
3592 					*error = EINVAL;
3593 					return (1);
3594 				}
3595 				m_copydata(control, cmsg_data_off, sizeof(struct sctp_initmsg), (caddr_t)&initmsg);
3596 				if (initmsg.sinit_max_attempts)
3597 					stcb->asoc.max_init_times = initmsg.sinit_max_attempts;
3598 				if (initmsg.sinit_num_ostreams)
3599 					stcb->asoc.pre_open_streams = initmsg.sinit_num_ostreams;
3600 				if (initmsg.sinit_max_instreams)
3601 					stcb->asoc.max_inbound_streams = initmsg.sinit_max_instreams;
3602 				if (initmsg.sinit_max_init_timeo)
3603 					stcb->asoc.initial_init_rto_max = initmsg.sinit_max_init_timeo;
3604 				if (stcb->asoc.streamoutcnt < stcb->asoc.pre_open_streams) {
3605 					struct sctp_stream_out *tmp_str;
3606 					unsigned int i;
3607 #if defined(SCTP_DETAILED_STR_STATS)
3608 					int j;
3609 #endif
3610 
3611 					/* Default is NOT correct */
3612 					SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, default:%d pre_open:%d\n",
3613 					    stcb->asoc.streamoutcnt, stcb->asoc.pre_open_streams);
3614 					SCTP_TCB_UNLOCK(stcb);
3615 					SCTP_MALLOC(tmp_str,
3616 					    struct sctp_stream_out *,
3617 					    (stcb->asoc.pre_open_streams * sizeof(struct sctp_stream_out)),
3618 					    SCTP_M_STRMO);
3619 					SCTP_TCB_LOCK(stcb);
3620 					if (tmp_str != NULL) {
3621 						SCTP_FREE(stcb->asoc.strmout, SCTP_M_STRMO);
3622 						stcb->asoc.strmout = tmp_str;
3623 						stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt = stcb->asoc.pre_open_streams;
3624 					} else {
3625 						stcb->asoc.pre_open_streams = stcb->asoc.streamoutcnt;
3626 					}
3627 					for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3628 						TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3629 						stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL);
3630 						stcb->asoc.strmout[i].chunks_on_queues = 0;
3631 #if defined(SCTP_DETAILED_STR_STATS)
3632 						for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
3633 							stcb->asoc.strmout[i].abandoned_sent[j] = 0;
3634 							stcb->asoc.strmout[i].abandoned_unsent[j] = 0;
3635 						}
3636 #else
3637 						stcb->asoc.strmout[i].abandoned_sent[0] = 0;
3638 						stcb->asoc.strmout[i].abandoned_unsent[0] = 0;
3639 #endif
3640 						stcb->asoc.strmout[i].next_mid_ordered = 0;
3641 						stcb->asoc.strmout[i].next_mid_unordered = 0;
3642 						stcb->asoc.strmout[i].sid = i;
3643 						stcb->asoc.strmout[i].last_msg_incomplete = 0;
3644 						stcb->asoc.strmout[i].state = SCTP_STREAM_OPENING;
3645 					}
3646 				}
3647 				break;
3648 #ifdef INET
3649 			case SCTP_DSTADDRV4:
3650 				if (cmsg_data_len < (int)sizeof(struct in_addr)) {
3651 					*error = EINVAL;
3652 					return (1);
3653 				}
3654 				memset(&sin, 0, sizeof(struct sockaddr_in));
3655 				sin.sin_family = AF_INET;
3656 				sin.sin_len = sizeof(struct sockaddr_in);
3657 				sin.sin_port = stcb->rport;
3658 				m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3659 				if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3660 				    (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3661 				    IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3662 					*error = EINVAL;
3663 					return (1);
3664 				}
3665 				if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port,
3666 				    SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3667 					*error = ENOBUFS;
3668 					return (1);
3669 				}
3670 				break;
3671 #endif
3672 #ifdef INET6
3673 			case SCTP_DSTADDRV6:
3674 				if (cmsg_data_len < (int)sizeof(struct in6_addr)) {
3675 					*error = EINVAL;
3676 					return (1);
3677 				}
3678 				memset(&sin6, 0, sizeof(struct sockaddr_in6));
3679 				sin6.sin6_family = AF_INET6;
3680 				sin6.sin6_len = sizeof(struct sockaddr_in6);
3681 				sin6.sin6_port = stcb->rport;
3682 				m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3683 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) ||
3684 				    IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
3685 					*error = EINVAL;
3686 					return (1);
3687 				}
3688 #ifdef INET
3689 				if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3690 					in6_sin6_2_sin(&sin, &sin6);
3691 					if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3692 					    (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3693 					    IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3694 						*error = EINVAL;
3695 						return (1);
3696 					}
3697 					if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port,
3698 					    SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3699 						*error = ENOBUFS;
3700 						return (1);
3701 					}
3702 				} else
3703 #endif
3704 					if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin6, NULL, stcb->asoc.port,
3705 				    SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3706 					*error = ENOBUFS;
3707 					return (1);
3708 				}
3709 				break;
3710 #endif
3711 			default:
3712 				break;
3713 			}
3714 		}
3715 	}
3716 	return (0);
3717 }
3718 
3719 #if defined(INET) || defined(INET6)
3720 static struct sctp_tcb *
3721 sctp_findassociation_cmsgs(struct sctp_inpcb **inp_p,
3722     uint16_t port,
3723     struct mbuf *control,
3724     struct sctp_nets **net_p,
3725     int *error)
3726 {
3727 	struct cmsghdr cmh;
3728 	struct sctp_tcb *stcb;
3729 	struct sockaddr *addr;
3730 #ifdef INET
3731 	struct sockaddr_in sin;
3732 #endif
3733 #ifdef INET6
3734 	struct sockaddr_in6 sin6;
3735 #endif
3736 	int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3737 
3738 	tot_len = SCTP_BUF_LEN(control);
3739 	for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3740 		rem_len = tot_len - off;
3741 		if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3742 			/* There is not enough room for one more. */
3743 			*error = EINVAL;
3744 			return (NULL);
3745 		}
3746 		m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3747 		if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3748 			/* We dont't have a complete CMSG header. */
3749 			*error = EINVAL;
3750 			return (NULL);
3751 		}
3752 		if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3753 			/* We don't have the complete CMSG. */
3754 			*error = EINVAL;
3755 			return (NULL);
3756 		}
3757 		cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3758 		cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3759 		if (cmh.cmsg_level == IPPROTO_SCTP) {
3760 			switch (cmh.cmsg_type) {
3761 #ifdef INET
3762 			case SCTP_DSTADDRV4:
3763 				if (cmsg_data_len < (int)sizeof(struct in_addr)) {
3764 					*error = EINVAL;
3765 					return (NULL);
3766 				}
3767 				memset(&sin, 0, sizeof(struct sockaddr_in));
3768 				sin.sin_family = AF_INET;
3769 				sin.sin_len = sizeof(struct sockaddr_in);
3770 				sin.sin_port = port;
3771 				m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3772 				addr = (struct sockaddr *)&sin;
3773 				break;
3774 #endif
3775 #ifdef INET6
3776 			case SCTP_DSTADDRV6:
3777 				if (cmsg_data_len < (int)sizeof(struct in6_addr)) {
3778 					*error = EINVAL;
3779 					return (NULL);
3780 				}
3781 				memset(&sin6, 0, sizeof(struct sockaddr_in6));
3782 				sin6.sin6_family = AF_INET6;
3783 				sin6.sin6_len = sizeof(struct sockaddr_in6);
3784 				sin6.sin6_port = port;
3785 				m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3786 #ifdef INET
3787 				if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3788 					in6_sin6_2_sin(&sin, &sin6);
3789 					addr = (struct sockaddr *)&sin;
3790 				} else
3791 #endif
3792 					addr = (struct sockaddr *)&sin6;
3793 				break;
3794 #endif
3795 			default:
3796 				addr = NULL;
3797 				break;
3798 			}
3799 			if (addr) {
3800 				stcb = sctp_findassociation_ep_addr(inp_p, addr, net_p, NULL, NULL);
3801 				if (stcb != NULL) {
3802 					return (stcb);
3803 				}
3804 			}
3805 		}
3806 	}
3807 	return (NULL);
3808 }
3809 #endif
3810 
3811 static struct mbuf *
3812 sctp_add_cookie(struct mbuf *init, int init_offset,
3813     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t **signature)
3814 {
3815 	struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3816 	struct sctp_state_cookie *stc;
3817 	struct sctp_paramhdr *ph;
3818 	uint16_t cookie_sz;
3819 
3820 	mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3821 	    sizeof(struct sctp_paramhdr)), 0,
3822 	    M_NOWAIT, 1, MT_DATA);
3823 	if (mret == NULL) {
3824 		return (NULL);
3825 	}
3826 	copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_NOWAIT);
3827 	if (copy_init == NULL) {
3828 		sctp_m_freem(mret);
3829 		return (NULL);
3830 	}
3831 #ifdef SCTP_MBUF_LOGGING
3832 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3833 		sctp_log_mbc(copy_init, SCTP_MBUF_ICOPY);
3834 	}
3835 #endif
3836 	copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3837 	    M_NOWAIT);
3838 	if (copy_initack == NULL) {
3839 		sctp_m_freem(mret);
3840 		sctp_m_freem(copy_init);
3841 		return (NULL);
3842 	}
3843 #ifdef SCTP_MBUF_LOGGING
3844 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3845 		sctp_log_mbc(copy_initack, SCTP_MBUF_ICOPY);
3846 	}
3847 #endif
3848 	/* easy side we just drop it on the end */
3849 	ph = mtod(mret, struct sctp_paramhdr *);
3850 	SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3851 	    sizeof(struct sctp_paramhdr);
3852 	stc = (struct sctp_state_cookie *)((caddr_t)ph +
3853 	    sizeof(struct sctp_paramhdr));
3854 	ph->param_type = htons(SCTP_STATE_COOKIE);
3855 	ph->param_length = 0;	/* fill in at the end */
3856 	/* Fill in the stc cookie data */
3857 	memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3858 
3859 	/* tack the INIT and then the INIT-ACK onto the chain */
3860 	cookie_sz = 0;
3861 	for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3862 		cookie_sz += SCTP_BUF_LEN(m_at);
3863 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3864 			SCTP_BUF_NEXT(m_at) = copy_init;
3865 			break;
3866 		}
3867 	}
3868 	for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3869 		cookie_sz += SCTP_BUF_LEN(m_at);
3870 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3871 			SCTP_BUF_NEXT(m_at) = copy_initack;
3872 			break;
3873 		}
3874 	}
3875 	for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3876 		cookie_sz += SCTP_BUF_LEN(m_at);
3877 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3878 			break;
3879 		}
3880 	}
3881 	sig = sctp_get_mbuf_for_msg(SCTP_SIGNATURE_SIZE, 0, M_NOWAIT, 1, MT_DATA);
3882 	if (sig == NULL) {
3883 		/* no space, so free the entire chain */
3884 		sctp_m_freem(mret);
3885 		return (NULL);
3886 	}
3887 	SCTP_BUF_NEXT(m_at) = sig;
3888 	SCTP_BUF_LEN(sig) = SCTP_SIGNATURE_SIZE;
3889 	cookie_sz += SCTP_SIGNATURE_SIZE;
3890 	ph->param_length = htons(cookie_sz);
3891 	*signature = (uint8_t *)mtod(sig, caddr_t);
3892 	memset(*signature, 0, SCTP_SIGNATURE_SIZE);
3893 	return (mret);
3894 }
3895 
3896 static uint8_t
3897 sctp_get_ect(struct sctp_tcb *stcb)
3898 {
3899 	if ((stcb != NULL) && (stcb->asoc.ecn_supported == 1)) {
3900 		return (SCTP_ECT0_BIT);
3901 	} else {
3902 		return (0);
3903 	}
3904 }
3905 
3906 #if defined(INET) || defined(INET6)
3907 static void
3908 sctp_handle_no_route(struct sctp_tcb *stcb,
3909     struct sctp_nets *net,
3910     int so_locked)
3911 {
3912 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n");
3913 
3914 	if (net) {
3915 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was ");
3916 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa);
3917 		if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3918 			if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3919 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", (void *)net);
3920 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3921 				    stcb, 0,
3922 				    (void *)net,
3923 				    so_locked);
3924 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
3925 				net->dest_state &= ~SCTP_ADDR_PF;
3926 			}
3927 		}
3928 		if (stcb) {
3929 			if (net == stcb->asoc.primary_destination) {
3930 				/* need a new primary */
3931 				struct sctp_nets *alt;
3932 
3933 				alt = sctp_find_alternate_net(stcb, net, 0);
3934 				if (alt != net) {
3935 					if (stcb->asoc.alternate) {
3936 						sctp_free_remote_addr(stcb->asoc.alternate);
3937 					}
3938 					stcb->asoc.alternate = alt;
3939 					atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
3940 					if (net->ro._s_addr) {
3941 						sctp_free_ifa(net->ro._s_addr);
3942 						net->ro._s_addr = NULL;
3943 					}
3944 					net->src_addr_selected = 0;
3945 				}
3946 			}
3947 		}
3948 	}
3949 }
3950 #endif
3951 
3952 static int
3953 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3954     struct sctp_tcb *stcb,	/* may be NULL */
3955     struct sctp_nets *net,
3956     struct sockaddr *to,
3957     struct mbuf *m,
3958     uint32_t auth_offset,
3959     struct sctp_auth_chunk *auth,
3960     uint16_t auth_keyid,
3961     int nofragment_flag,
3962     int ecn_ok,
3963     int out_of_asoc_ok,
3964     uint16_t src_port,
3965     uint16_t dest_port,
3966     uint32_t v_tag,
3967     uint16_t port,
3968     union sctp_sockstore *over_addr,
3969     uint8_t mflowtype, uint32_t mflowid,
3970     bool use_zero_crc,
3971     int so_locked)
3972 {
3973 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3974 	/**
3975 	 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet header
3976 	 * WITH an SCTPHDR but no IP header, endpoint inp and sa structure:
3977 	 * - fill in the HMAC digest of any AUTH chunk in the packet.
3978 	 * - calculate and fill in the SCTP checksum.
3979 	 * - prepend an IP address header.
3980 	 * - if boundall use INADDR_ANY.
3981 	 * - if boundspecific do source address selection.
3982 	 * - set fragmentation option for ipV4.
3983 	 * - On return from IP output, check/adjust mtu size of output
3984 	 *   interface and smallest_mtu size as well.
3985 	 */
3986 	/* Will need ifdefs around this */
3987 	struct mbuf *newm;
3988 	struct sctphdr *sctphdr;
3989 	int packet_length;
3990 	int ret;
3991 #if defined(INET) || defined(INET6)
3992 	uint32_t vrf_id;
3993 #endif
3994 #if defined(INET) || defined(INET6)
3995 	struct mbuf *o_pak;
3996 	sctp_route_t *ro = NULL;
3997 	struct udphdr *udp = NULL;
3998 #endif
3999 	uint8_t tos_value;
4000 
4001 	if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
4002 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4003 		sctp_m_freem(m);
4004 		return (EFAULT);
4005 	}
4006 #if defined(INET) || defined(INET6)
4007 	if (stcb) {
4008 		vrf_id = stcb->asoc.vrf_id;
4009 	} else {
4010 		vrf_id = inp->def_vrf_id;
4011 	}
4012 #endif
4013 	/* fill in the HMAC digest for any AUTH chunk in the packet */
4014 	if ((auth != NULL) && (stcb != NULL)) {
4015 		sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
4016 	}
4017 
4018 	if (net) {
4019 		tos_value = net->dscp;
4020 	} else if (stcb) {
4021 		tos_value = stcb->asoc.default_dscp;
4022 	} else {
4023 		tos_value = inp->sctp_ep.default_dscp;
4024 	}
4025 
4026 	switch (to->sa_family) {
4027 #ifdef INET
4028 	case AF_INET:
4029 		{
4030 			struct ip *ip = NULL;
4031 			sctp_route_t iproute;
4032 			int len;
4033 
4034 			len = SCTP_MIN_V4_OVERHEAD;
4035 			if (port) {
4036 				len += sizeof(struct udphdr);
4037 			}
4038 			newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA);
4039 			if (newm == NULL) {
4040 				sctp_m_freem(m);
4041 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4042 				return (ENOMEM);
4043 			}
4044 			SCTP_ALIGN_TO_END(newm, len);
4045 			SCTP_BUF_LEN(newm) = len;
4046 			SCTP_BUF_NEXT(newm) = m;
4047 			m = newm;
4048 			if (net != NULL) {
4049 				m->m_pkthdr.flowid = net->flowid;
4050 				M_HASHTYPE_SET(m, net->flowtype);
4051 			} else {
4052 				m->m_pkthdr.flowid = mflowid;
4053 				M_HASHTYPE_SET(m, mflowtype);
4054 			}
4055 			packet_length = sctp_calculate_len(m);
4056 			ip = mtod(m, struct ip *);
4057 			ip->ip_v = IPVERSION;
4058 			ip->ip_hl = (sizeof(struct ip) >> 2);
4059 			if (tos_value == 0) {
4060 				/*
4061 				 * This means especially, that it is not set
4062 				 * at the SCTP layer. So use the value from
4063 				 * the IP layer.
4064 				 */
4065 				tos_value = inp->ip_inp.inp.inp_ip_tos;
4066 			}
4067 			tos_value &= 0xfc;
4068 			if (ecn_ok) {
4069 				tos_value |= sctp_get_ect(stcb);
4070 			}
4071 			if ((nofragment_flag) && (port == 0)) {
4072 				ip->ip_off = htons(IP_DF);
4073 			} else {
4074 				ip->ip_off = htons(0);
4075 			}
4076 			/* FreeBSD has a function for ip_id's */
4077 			ip_fillid(ip);
4078 
4079 			ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
4080 			ip->ip_len = htons(packet_length);
4081 			ip->ip_tos = tos_value;
4082 			if (port) {
4083 				ip->ip_p = IPPROTO_UDP;
4084 			} else {
4085 				ip->ip_p = IPPROTO_SCTP;
4086 			}
4087 			ip->ip_sum = 0;
4088 			if (net == NULL) {
4089 				ro = &iproute;
4090 				memset(&iproute, 0, sizeof(iproute));
4091 				memcpy(&ro->ro_dst, to, to->sa_len);
4092 			} else {
4093 				ro = (sctp_route_t *)&net->ro;
4094 			}
4095 			/* Now the address selection part */
4096 			ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
4097 
4098 			/* call the routine to select the src address */
4099 			if (net && out_of_asoc_ok == 0) {
4100 				if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4101 					sctp_free_ifa(net->ro._s_addr);
4102 					net->ro._s_addr = NULL;
4103 					net->src_addr_selected = 0;
4104 					RO_NHFREE(ro);
4105 				}
4106 				if (net->src_addr_selected == 0) {
4107 					/* Cache the source address */
4108 					net->ro._s_addr = sctp_source_address_selection(inp, stcb,
4109 					    ro, net, 0,
4110 					    vrf_id);
4111 					net->src_addr_selected = 1;
4112 				}
4113 				if (net->ro._s_addr == NULL) {
4114 					/* No route to host */
4115 					net->src_addr_selected = 0;
4116 					sctp_handle_no_route(stcb, net, so_locked);
4117 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4118 					sctp_m_freem(m);
4119 					return (EHOSTUNREACH);
4120 				}
4121 				ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
4122 			} else {
4123 				if (over_addr == NULL) {
4124 					struct sctp_ifa *_lsrc;
4125 
4126 					_lsrc = sctp_source_address_selection(inp, stcb, ro,
4127 					    net,
4128 					    out_of_asoc_ok,
4129 					    vrf_id);
4130 					if (_lsrc == NULL) {
4131 						sctp_handle_no_route(stcb, net, so_locked);
4132 						SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4133 						sctp_m_freem(m);
4134 						return (EHOSTUNREACH);
4135 					}
4136 					ip->ip_src = _lsrc->address.sin.sin_addr;
4137 					sctp_free_ifa(_lsrc);
4138 				} else {
4139 					ip->ip_src = over_addr->sin.sin_addr;
4140 					SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
4141 				}
4142 			}
4143 			if (port) {
4144 				if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4145 					sctp_handle_no_route(stcb, net, so_locked);
4146 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4147 					sctp_m_freem(m);
4148 					return (EHOSTUNREACH);
4149 				}
4150 				udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
4151 				udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4152 				udp->uh_dport = port;
4153 				udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip)));
4154 				if (V_udp_cksum) {
4155 					udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
4156 				} else {
4157 					udp->uh_sum = 0;
4158 				}
4159 				sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4160 			} else {
4161 				sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
4162 			}
4163 
4164 			sctphdr->src_port = src_port;
4165 			sctphdr->dest_port = dest_port;
4166 			sctphdr->v_tag = v_tag;
4167 			sctphdr->checksum = 0;
4168 
4169 			/*
4170 			 * If source address selection fails and we find no
4171 			 * route then the ip_output should fail as well with
4172 			 * a NO_ROUTE_TO_HOST type error. We probably should
4173 			 * catch that somewhere and abort the association
4174 			 * right away (assuming this is an INIT being sent).
4175 			 */
4176 			if (ro->ro_nh == NULL) {
4177 				/*
4178 				 * src addr selection failed to find a route
4179 				 * (or valid source addr), so we can't get
4180 				 * there from here (yet)!
4181 				 */
4182 				sctp_handle_no_route(stcb, net, so_locked);
4183 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4184 				sctp_m_freem(m);
4185 				return (EHOSTUNREACH);
4186 			}
4187 			if (ro != &iproute) {
4188 				memcpy(&iproute, ro, sizeof(*ro));
4189 			}
4190 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
4191 			    (uint32_t)(ntohl(ip->ip_src.s_addr)));
4192 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
4193 			    (uint32_t)(ntohl(ip->ip_dst.s_addr)));
4194 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
4195 			    (void *)ro->ro_nh);
4196 
4197 			if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4198 				/* failed to prepend data, give up */
4199 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4200 				sctp_m_freem(m);
4201 				return (ENOMEM);
4202 			}
4203 			SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4204 			if (port) {
4205 				if (use_zero_crc) {
4206 					SCTP_STAT_INCR(sctps_sendzerocrc);
4207 				} else {
4208 					sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
4209 					SCTP_STAT_INCR(sctps_sendswcrc);
4210 				}
4211 				if (V_udp_cksum) {
4212 					SCTP_ENABLE_UDP_CSUM(o_pak);
4213 				}
4214 			} else {
4215 				if (use_zero_crc) {
4216 					SCTP_STAT_INCR(sctps_sendzerocrc);
4217 				} else {
4218 					m->m_pkthdr.csum_flags = CSUM_SCTP;
4219 					m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
4220 					SCTP_STAT_INCR(sctps_sendhwcrc);
4221 				}
4222 			}
4223 #ifdef SCTP_PACKET_LOGGING
4224 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4225 				sctp_packet_log(o_pak);
4226 #endif
4227 			/* send it out.  table id is taken from stcb */
4228 			SCTP_PROBE5(send, NULL, stcb, ip, stcb, sctphdr);
4229 			SCTP_IP_OUTPUT(ret, o_pak, ro, inp, vrf_id);
4230 			if (port) {
4231 				UDPSTAT_INC(udps_opackets);
4232 			}
4233 			SCTP_STAT_INCR(sctps_sendpackets);
4234 			SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4235 			if (ret)
4236 				SCTP_STAT_INCR(sctps_senderrors);
4237 
4238 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
4239 			if (net == NULL) {
4240 				/* free tempy routes */
4241 				RO_NHFREE(ro);
4242 			} else {
4243 				if ((ro->ro_nh != NULL) && (net->ro._s_addr) &&
4244 				    ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) {
4245 					uint32_t mtu;
4246 
4247 					mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_nh);
4248 					if (mtu > 0) {
4249 						if (net->port) {
4250 							mtu -= sizeof(struct udphdr);
4251 						}
4252 						if (mtu < net->mtu) {
4253 							net->mtu = mtu;
4254 							if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) {
4255 								sctp_pathmtu_adjustment(stcb, mtu, true);
4256 							}
4257 						}
4258 					}
4259 				} else if (ro->ro_nh == NULL) {
4260 					/* route was freed */
4261 					if (net->ro._s_addr &&
4262 					    net->src_addr_selected) {
4263 						sctp_free_ifa(net->ro._s_addr);
4264 						net->ro._s_addr = NULL;
4265 					}
4266 					net->src_addr_selected = 0;
4267 				}
4268 			}
4269 			return (ret);
4270 		}
4271 #endif
4272 #ifdef INET6
4273 	case AF_INET6:
4274 		{
4275 			uint32_t flowlabel, flowinfo;
4276 			struct ip6_hdr *ip6h;
4277 			struct route_in6 ip6route;
4278 			struct ifnet *ifp;
4279 			struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
4280 			int prev_scope = 0;
4281 			struct sockaddr_in6 lsa6_storage;
4282 			int error;
4283 			u_short prev_port = 0;
4284 			int len;
4285 
4286 			if (net) {
4287 				flowlabel = net->flowlabel;
4288 			} else if (stcb) {
4289 				flowlabel = stcb->asoc.default_flowlabel;
4290 			} else {
4291 				flowlabel = inp->sctp_ep.default_flowlabel;
4292 			}
4293 			if (flowlabel == 0) {
4294 				/*
4295 				 * This means especially, that it is not set
4296 				 * at the SCTP layer. So use the value from
4297 				 * the IP layer.
4298 				 */
4299 				flowlabel = ntohl(((struct inpcb *)inp)->inp_flow);
4300 			}
4301 			flowlabel &= 0x000fffff;
4302 			len = SCTP_MIN_OVERHEAD;
4303 			if (port) {
4304 				len += sizeof(struct udphdr);
4305 			}
4306 			newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA);
4307 			if (newm == NULL) {
4308 				sctp_m_freem(m);
4309 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4310 				return (ENOMEM);
4311 			}
4312 			SCTP_ALIGN_TO_END(newm, len);
4313 			SCTP_BUF_LEN(newm) = len;
4314 			SCTP_BUF_NEXT(newm) = m;
4315 			m = newm;
4316 			if (net != NULL) {
4317 				m->m_pkthdr.flowid = net->flowid;
4318 				M_HASHTYPE_SET(m, net->flowtype);
4319 			} else {
4320 				m->m_pkthdr.flowid = mflowid;
4321 				M_HASHTYPE_SET(m, mflowtype);
4322 			}
4323 			packet_length = sctp_calculate_len(m);
4324 
4325 			ip6h = mtod(m, struct ip6_hdr *);
4326 			/* protect *sin6 from overwrite */
4327 			sin6 = (struct sockaddr_in6 *)to;
4328 			tmp = *sin6;
4329 			sin6 = &tmp;
4330 
4331 			/* KAME hack: embed scopeid */
4332 			if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4333 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4334 				sctp_m_freem(m);
4335 				return (EINVAL);
4336 			}
4337 			if (net == NULL) {
4338 				memset(&ip6route, 0, sizeof(ip6route));
4339 				ro = (sctp_route_t *)&ip6route;
4340 				memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
4341 			} else {
4342 				ro = (sctp_route_t *)&net->ro;
4343 			}
4344 			/*
4345 			 * We assume here that inp_flow is in host byte
4346 			 * order within the TCB!
4347 			 */
4348 			if (tos_value == 0) {
4349 				/*
4350 				 * This means especially, that it is not set
4351 				 * at the SCTP layer. So use the value from
4352 				 * the IP layer.
4353 				 */
4354 				tos_value = (ntohl(((struct inpcb *)inp)->inp_flow) >> 20) & 0xff;
4355 			}
4356 			tos_value &= 0xfc;
4357 			if (ecn_ok) {
4358 				tos_value |= sctp_get_ect(stcb);
4359 			}
4360 			flowinfo = 0x06;
4361 			flowinfo <<= 8;
4362 			flowinfo |= tos_value;
4363 			flowinfo <<= 20;
4364 			flowinfo |= flowlabel;
4365 			ip6h->ip6_flow = htonl(flowinfo);
4366 			if (port) {
4367 				ip6h->ip6_nxt = IPPROTO_UDP;
4368 			} else {
4369 				ip6h->ip6_nxt = IPPROTO_SCTP;
4370 			}
4371 			ip6h->ip6_plen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr)));
4372 			ip6h->ip6_dst = sin6->sin6_addr;
4373 
4374 			/*
4375 			 * Add SRC address selection here: we can only reuse
4376 			 * to a limited degree the kame src-addr-sel, since
4377 			 * we can try their selection but it may not be
4378 			 * bound.
4379 			 */
4380 			memset(&lsa6_tmp, 0, sizeof(lsa6_tmp));
4381 			lsa6_tmp.sin6_family = AF_INET6;
4382 			lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
4383 			lsa6 = &lsa6_tmp;
4384 			if (net && out_of_asoc_ok == 0) {
4385 				if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4386 					sctp_free_ifa(net->ro._s_addr);
4387 					net->ro._s_addr = NULL;
4388 					net->src_addr_selected = 0;
4389 					RO_NHFREE(ro);
4390 				}
4391 				if (net->src_addr_selected == 0) {
4392 					sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4393 					/* KAME hack: embed scopeid */
4394 					if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4395 						SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4396 						sctp_m_freem(m);
4397 						return (EINVAL);
4398 					}
4399 					/* Cache the source address */
4400 					net->ro._s_addr = sctp_source_address_selection(inp,
4401 					    stcb,
4402 					    ro,
4403 					    net,
4404 					    0,
4405 					    vrf_id);
4406 					(void)sa6_recoverscope(sin6);
4407 					net->src_addr_selected = 1;
4408 				}
4409 				if (net->ro._s_addr == NULL) {
4410 					SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
4411 					net->src_addr_selected = 0;
4412 					sctp_handle_no_route(stcb, net, so_locked);
4413 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4414 					sctp_m_freem(m);
4415 					return (EHOSTUNREACH);
4416 				}
4417 				lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
4418 			} else {
4419 				sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
4420 				/* KAME hack: embed scopeid */
4421 				if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4422 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4423 					sctp_m_freem(m);
4424 					return (EINVAL);
4425 				}
4426 				if (over_addr == NULL) {
4427 					struct sctp_ifa *_lsrc;
4428 
4429 					_lsrc = sctp_source_address_selection(inp, stcb, ro,
4430 					    net,
4431 					    out_of_asoc_ok,
4432 					    vrf_id);
4433 					if (_lsrc == NULL) {
4434 						sctp_handle_no_route(stcb, net, so_locked);
4435 						SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4436 						sctp_m_freem(m);
4437 						return (EHOSTUNREACH);
4438 					}
4439 					lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
4440 					sctp_free_ifa(_lsrc);
4441 				} else {
4442 					lsa6->sin6_addr = over_addr->sin6.sin6_addr;
4443 					SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
4444 				}
4445 				(void)sa6_recoverscope(sin6);
4446 			}
4447 			lsa6->sin6_port = inp->sctp_lport;
4448 
4449 			if (ro->ro_nh == NULL) {
4450 				/*
4451 				 * src addr selection failed to find a route
4452 				 * (or valid source addr), so we can't get
4453 				 * there from here!
4454 				 */
4455 				sctp_handle_no_route(stcb, net, so_locked);
4456 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4457 				sctp_m_freem(m);
4458 				return (EHOSTUNREACH);
4459 			}
4460 			/*
4461 			 * XXX: sa6 may not have a valid sin6_scope_id in
4462 			 * the non-SCOPEDROUTING case.
4463 			 */
4464 			memset(&lsa6_storage, 0, sizeof(lsa6_storage));
4465 			lsa6_storage.sin6_family = AF_INET6;
4466 			lsa6_storage.sin6_len = sizeof(lsa6_storage);
4467 			lsa6_storage.sin6_addr = lsa6->sin6_addr;
4468 			if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
4469 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
4470 				sctp_m_freem(m);
4471 				return (error);
4472 			}
4473 			/* XXX */
4474 			lsa6_storage.sin6_addr = lsa6->sin6_addr;
4475 			lsa6_storage.sin6_port = inp->sctp_lport;
4476 			lsa6 = &lsa6_storage;
4477 			ip6h->ip6_src = lsa6->sin6_addr;
4478 
4479 			if (port) {
4480 				if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4481 					sctp_handle_no_route(stcb, net, so_locked);
4482 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4483 					sctp_m_freem(m);
4484 					return (EHOSTUNREACH);
4485 				}
4486 				udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4487 				udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4488 				udp->uh_dport = port;
4489 				udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr)));
4490 				udp->uh_sum = 0;
4491 				sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4492 			} else {
4493 				sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4494 			}
4495 
4496 			sctphdr->src_port = src_port;
4497 			sctphdr->dest_port = dest_port;
4498 			sctphdr->v_tag = v_tag;
4499 			sctphdr->checksum = 0;
4500 
4501 			/*
4502 			 * We set the hop limit now since there is a good
4503 			 * chance that our ro pointer is now filled
4504 			 */
4505 			ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
4506 			ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4507 
4508 #ifdef SCTP_DEBUG
4509 			/* Copy to be sure something bad is not happening */
4510 			sin6->sin6_addr = ip6h->ip6_dst;
4511 			lsa6->sin6_addr = ip6h->ip6_src;
4512 #endif
4513 
4514 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
4515 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
4516 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
4517 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
4518 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
4519 			if (net) {
4520 				sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4521 				/*
4522 				 * preserve the port and scope for link
4523 				 * local send
4524 				 */
4525 				prev_scope = sin6->sin6_scope_id;
4526 				prev_port = sin6->sin6_port;
4527 			}
4528 
4529 			if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4530 				/* failed to prepend data, give up */
4531 				sctp_m_freem(m);
4532 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4533 				return (ENOMEM);
4534 			}
4535 			SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4536 			if (port) {
4537 				sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4538 				SCTP_STAT_INCR(sctps_sendswcrc);
4539 				if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4540 					udp->uh_sum = 0xffff;
4541 				}
4542 			} else {
4543 				m->m_pkthdr.csum_flags = CSUM_SCTP_IPV6;
4544 				m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
4545 				SCTP_STAT_INCR(sctps_sendhwcrc);
4546 			}
4547 			/* send it out. table id is taken from stcb */
4548 #ifdef SCTP_PACKET_LOGGING
4549 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4550 				sctp_packet_log(o_pak);
4551 #endif
4552 			SCTP_PROBE5(send, NULL, stcb, ip6h, stcb, sctphdr);
4553 			SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, inp, vrf_id);
4554 			if (net) {
4555 				/* for link local this must be done */
4556 				sin6->sin6_scope_id = prev_scope;
4557 				sin6->sin6_port = prev_port;
4558 			}
4559 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4560 			if (port) {
4561 				UDPSTAT_INC(udps_opackets);
4562 			}
4563 			SCTP_STAT_INCR(sctps_sendpackets);
4564 			SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4565 			if (ret) {
4566 				SCTP_STAT_INCR(sctps_senderrors);
4567 			}
4568 			if (net == NULL) {
4569 				/* Now if we had a temp route free it */
4570 				RO_NHFREE(ro);
4571 			} else {
4572 				/*
4573 				 * PMTU check versus smallest asoc MTU goes
4574 				 * here
4575 				 */
4576 				if (ro->ro_nh == NULL) {
4577 					/* Route was freed */
4578 					if (net->ro._s_addr &&
4579 					    net->src_addr_selected) {
4580 						sctp_free_ifa(net->ro._s_addr);
4581 						net->ro._s_addr = NULL;
4582 					}
4583 					net->src_addr_selected = 0;
4584 				}
4585 				if ((ro->ro_nh != NULL) && (net->ro._s_addr) &&
4586 				    ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) {
4587 					uint32_t mtu;
4588 
4589 					mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_nh);
4590 					if (mtu > 0) {
4591 						if (net->port) {
4592 							mtu -= sizeof(struct udphdr);
4593 						}
4594 						if (mtu < net->mtu) {
4595 							net->mtu = mtu;
4596 							if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) {
4597 								sctp_pathmtu_adjustment(stcb, mtu, false);
4598 							}
4599 						}
4600 					}
4601 				} else if (ifp != NULL) {
4602 					if ((ND_IFINFO(ifp)->linkmtu > 0) &&
4603 					    (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4604 						sctp_pathmtu_adjustment(stcb, ND_IFINFO(ifp)->linkmtu, false);
4605 					}
4606 				}
4607 			}
4608 			return (ret);
4609 		}
4610 #endif
4611 	default:
4612 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4613 		    ((struct sockaddr *)to)->sa_family);
4614 		sctp_m_freem(m);
4615 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4616 		return (EFAULT);
4617 	}
4618 }
4619 
4620 void
4621 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked)
4622 {
4623 	struct mbuf *m, *m_last;
4624 	struct sctp_nets *net;
4625 	struct sctp_init_chunk *init;
4626 	struct sctp_supported_addr_param *sup_addr;
4627 	struct sctp_adaptation_layer_indication *ali;
4628 	struct sctp_zero_checksum_acceptable *zero_chksum;
4629 	struct sctp_supported_chunk_types_param *pr_supported;
4630 	struct sctp_paramhdr *ph;
4631 	int cnt_inits_to = 0;
4632 	int error;
4633 	uint16_t num_ext, chunk_len, padding_len, parameter_len;
4634 
4635 	/* INIT's always go to the primary (and usually ONLY address) */
4636 	net = stcb->asoc.primary_destination;
4637 	if (net == NULL) {
4638 		net = TAILQ_FIRST(&stcb->asoc.nets);
4639 		if (net == NULL) {
4640 			/* TSNH */
4641 			return;
4642 		}
4643 		/* we confirm any address we send an INIT to */
4644 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4645 		(void)sctp_set_primary_addr(stcb, NULL, net);
4646 	} else {
4647 		/* we confirm any address we send an INIT to */
4648 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4649 	}
4650 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4651 #ifdef INET6
4652 	if (net->ro._l_addr.sa.sa_family == AF_INET6) {
4653 		/*
4654 		 * special hook, if we are sending to link local it will not
4655 		 * show up in our private address count.
4656 		 */
4657 		if (IN6_IS_ADDR_LINKLOCAL(&net->ro._l_addr.sin6.sin6_addr))
4658 			cnt_inits_to = 1;
4659 	}
4660 #endif
4661 	if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4662 		/* This case should not happen */
4663 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4664 		return;
4665 	}
4666 	/* start the INIT timer */
4667 	sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4668 
4669 	m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_NOWAIT, 1, MT_DATA);
4670 	if (m == NULL) {
4671 		/* No memory, INIT timer will re-attempt. */
4672 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4673 		return;
4674 	}
4675 	chunk_len = (uint16_t)sizeof(struct sctp_init_chunk);
4676 	padding_len = 0;
4677 	/* Now lets put the chunk header in place */
4678 	init = mtod(m, struct sctp_init_chunk *);
4679 	/* now the chunk header */
4680 	init->ch.chunk_type = SCTP_INITIATION;
4681 	init->ch.chunk_flags = 0;
4682 	/* fill in later from mbuf we build */
4683 	init->ch.chunk_length = 0;
4684 	/* place in my tag */
4685 	init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4686 	/* set up some of the credits. */
4687 	init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4688 	    SCTP_MINIMAL_RWND));
4689 	init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4690 	init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4691 	init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4692 
4693 	/* Adaptation layer indication parameter */
4694 	if (inp->sctp_ep.adaptation_layer_indicator_provided) {
4695 		parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication);
4696 		ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t)+chunk_len);
4697 		ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4698 		ali->ph.param_length = htons(parameter_len);
4699 		ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator);
4700 		chunk_len += parameter_len;
4701 	}
4702 
4703 	/* ECN parameter */
4704 	if (stcb->asoc.ecn_supported == 1) {
4705 		parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4706 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
4707 		ph->param_type = htons(SCTP_ECN_CAPABLE);
4708 		ph->param_length = htons(parameter_len);
4709 		chunk_len += parameter_len;
4710 	}
4711 
4712 	/* PR-SCTP supported parameter */
4713 	if (stcb->asoc.prsctp_supported == 1) {
4714 		parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4715 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
4716 		ph->param_type = htons(SCTP_PRSCTP_SUPPORTED);
4717 		ph->param_length = htons(parameter_len);
4718 		chunk_len += parameter_len;
4719 	}
4720 
4721 	/* Zero checksum acceptable parameter */
4722 	if (stcb->asoc.rcv_edmid != SCTP_EDMID_NONE) {
4723 		parameter_len = (uint16_t)sizeof(struct sctp_zero_checksum_acceptable);
4724 		zero_chksum = (struct sctp_zero_checksum_acceptable *)(mtod(m, caddr_t)+chunk_len);
4725 		zero_chksum->ph.param_type = htons(SCTP_ZERO_CHECKSUM_ACCEPTABLE);
4726 		zero_chksum->ph.param_length = htons(parameter_len);
4727 		zero_chksum->edmid = htonl(stcb->asoc.rcv_edmid);
4728 		chunk_len += parameter_len;
4729 	}
4730 
4731 	/* Add NAT friendly parameter. */
4732 	if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4733 		parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4734 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
4735 		ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4736 		ph->param_length = htons(parameter_len);
4737 		chunk_len += parameter_len;
4738 	}
4739 
4740 	/* And now tell the peer which extensions we support */
4741 	num_ext = 0;
4742 	pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+chunk_len);
4743 	if (stcb->asoc.prsctp_supported == 1) {
4744 		pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4745 		if (stcb->asoc.idata_supported) {
4746 			pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN;
4747 		}
4748 	}
4749 	if (stcb->asoc.auth_supported == 1) {
4750 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4751 	}
4752 	if (stcb->asoc.asconf_supported == 1) {
4753 		pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4754 		pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4755 	}
4756 	if (stcb->asoc.reconfig_supported == 1) {
4757 		pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4758 	}
4759 	if (stcb->asoc.idata_supported) {
4760 		pr_supported->chunk_types[num_ext++] = SCTP_IDATA;
4761 	}
4762 	if (stcb->asoc.nrsack_supported == 1) {
4763 		pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4764 	}
4765 	if (stcb->asoc.pktdrop_supported == 1) {
4766 		pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4767 	}
4768 	if (num_ext > 0) {
4769 		parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext;
4770 		pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4771 		pr_supported->ph.param_length = htons(parameter_len);
4772 		padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4773 		chunk_len += parameter_len;
4774 	}
4775 	/* add authentication parameters */
4776 	if (stcb->asoc.auth_supported) {
4777 		/* attach RANDOM parameter, if available */
4778 		if (stcb->asoc.authinfo.random != NULL) {
4779 			struct sctp_auth_random *randp;
4780 
4781 			if (padding_len > 0) {
4782 				memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4783 				chunk_len += padding_len;
4784 				padding_len = 0;
4785 			}
4786 			randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+chunk_len);
4787 			parameter_len = (uint16_t)sizeof(struct sctp_auth_random) + stcb->asoc.authinfo.random_len;
4788 			/* random key already contains the header */
4789 			memcpy(randp, stcb->asoc.authinfo.random->key, parameter_len);
4790 			padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4791 			chunk_len += parameter_len;
4792 		}
4793 		/* add HMAC_ALGO parameter */
4794 		if (stcb->asoc.local_hmacs != NULL) {
4795 			struct sctp_auth_hmac_algo *hmacs;
4796 
4797 			if (padding_len > 0) {
4798 				memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4799 				chunk_len += padding_len;
4800 				padding_len = 0;
4801 			}
4802 			hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+chunk_len);
4803 			parameter_len = (uint16_t)(sizeof(struct sctp_auth_hmac_algo) +
4804 			    stcb->asoc.local_hmacs->num_algo * sizeof(uint16_t));
4805 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4806 			hmacs->ph.param_length = htons(parameter_len);
4807 			sctp_serialize_hmaclist(stcb->asoc.local_hmacs, (uint8_t *)hmacs->hmac_ids);
4808 			padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4809 			chunk_len += parameter_len;
4810 		}
4811 		/* add CHUNKS parameter */
4812 		if (stcb->asoc.local_auth_chunks != NULL) {
4813 			struct sctp_auth_chunk_list *chunks;
4814 
4815 			if (padding_len > 0) {
4816 				memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4817 				chunk_len += padding_len;
4818 				padding_len = 0;
4819 			}
4820 			chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+chunk_len);
4821 			parameter_len = (uint16_t)(sizeof(struct sctp_auth_chunk_list) +
4822 			    sctp_auth_get_chklist_size(stcb->asoc.local_auth_chunks));
4823 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4824 			chunks->ph.param_length = htons(parameter_len);
4825 			sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks, chunks->chunk_types);
4826 			padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4827 			chunk_len += parameter_len;
4828 		}
4829 	}
4830 
4831 	/* now any cookie time extensions */
4832 	if (stcb->asoc.cookie_preserve_req > 0) {
4833 		struct sctp_cookie_perserve_param *cookie_preserve;
4834 
4835 		if (padding_len > 0) {
4836 			memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4837 			chunk_len += padding_len;
4838 			padding_len = 0;
4839 		}
4840 		parameter_len = (uint16_t)sizeof(struct sctp_cookie_perserve_param);
4841 		cookie_preserve = (struct sctp_cookie_perserve_param *)(mtod(m, caddr_t)+chunk_len);
4842 		cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4843 		cookie_preserve->ph.param_length = htons(parameter_len);
4844 		cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4845 		stcb->asoc.cookie_preserve_req = 0;
4846 		chunk_len += parameter_len;
4847 	}
4848 
4849 	if (stcb->asoc.scope.ipv4_addr_legal || stcb->asoc.scope.ipv6_addr_legal) {
4850 		uint8_t i;
4851 
4852 		if (padding_len > 0) {
4853 			memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4854 			chunk_len += padding_len;
4855 			padding_len = 0;
4856 		}
4857 		parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4858 		if (stcb->asoc.scope.ipv4_addr_legal) {
4859 			parameter_len += (uint16_t)sizeof(uint16_t);
4860 		}
4861 		if (stcb->asoc.scope.ipv6_addr_legal) {
4862 			parameter_len += (uint16_t)sizeof(uint16_t);
4863 		}
4864 		sup_addr = (struct sctp_supported_addr_param *)(mtod(m, caddr_t)+chunk_len);
4865 		sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4866 		sup_addr->ph.param_length = htons(parameter_len);
4867 		i = 0;
4868 		if (stcb->asoc.scope.ipv4_addr_legal) {
4869 			sup_addr->addr_type[i++] = htons(SCTP_IPV4_ADDRESS);
4870 		}
4871 		if (stcb->asoc.scope.ipv6_addr_legal) {
4872 			sup_addr->addr_type[i++] = htons(SCTP_IPV6_ADDRESS);
4873 		}
4874 		padding_len = 4 - 2 * i;
4875 		chunk_len += parameter_len;
4876 	}
4877 
4878 	SCTP_BUF_LEN(m) = chunk_len;
4879 	/* now the addresses */
4880 	/*
4881 	 * To optimize this we could put the scoping stuff into a structure
4882 	 * and remove the individual uint8's from the assoc structure. Then
4883 	 * we could just sifa in the address within the stcb. But for now
4884 	 * this is a quick hack to get the address stuff teased apart.
4885 	 */
4886 	m_last = sctp_add_addresses_to_i_ia(inp, stcb, &stcb->asoc.scope,
4887 	    m, cnt_inits_to,
4888 	    &padding_len, &chunk_len);
4889 
4890 	init->ch.chunk_length = htons(chunk_len);
4891 	if (padding_len > 0) {
4892 		if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
4893 			sctp_m_freem(m);
4894 			return;
4895 		}
4896 	}
4897 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4898 	if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
4899 	    (struct sockaddr *)&net->ro._l_addr,
4900 	    m, 0, NULL, 0, 0, 0, 0,
4901 	    inp->sctp_lport, stcb->rport, htonl(0),
4902 	    net->port, NULL,
4903 	    0, 0,
4904 	    false, so_locked))) {
4905 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error);
4906 		if (error == ENOBUFS) {
4907 			stcb->asoc.ifp_had_enobuf = 1;
4908 			SCTP_STAT_INCR(sctps_lowlevelerr);
4909 		}
4910 	} else {
4911 		stcb->asoc.ifp_had_enobuf = 0;
4912 	}
4913 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4914 	(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4915 }
4916 
4917 struct mbuf *
4918 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4919     int param_offset, int *abort_processing,
4920     struct sctp_chunkhdr *cp,
4921     int *nat_friendly,
4922     int *cookie_found)
4923 {
4924 	/*
4925 	 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4926 	 * being equal to the beginning of the params i.e. (iphlen +
4927 	 * sizeof(struct sctp_init_msg) parse through the parameters to the
4928 	 * end of the mbuf verifying that all parameters are known.
4929 	 *
4930 	 * For unknown parameters build and return a mbuf with
4931 	 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4932 	 * processing this chunk stop, and set *abort_processing to 1.
4933 	 *
4934 	 * By having param_offset be pre-set to where parameters begin it is
4935 	 * hoped that this routine may be reused in the future by new
4936 	 * features.
4937 	 */
4938 	struct sctp_paramhdr *phdr, params;
4939 
4940 	struct mbuf *mat, *m_tmp, *op_err, *op_err_last;
4941 	int at, limit, pad_needed;
4942 	uint16_t ptype, plen, padded_size;
4943 
4944 	*abort_processing = 0;
4945 	if (cookie_found != NULL) {
4946 		*cookie_found = 0;
4947 	}
4948 	mat = in_initpkt;
4949 	limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4950 	at = param_offset;
4951 	op_err = NULL;
4952 	op_err_last = NULL;
4953 	pad_needed = 0;
4954 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4955 	phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4956 	while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4957 		ptype = ntohs(phdr->param_type);
4958 		plen = ntohs(phdr->param_length);
4959 		if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4960 			/* wacked parameter */
4961 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4962 			goto invalid_size;
4963 		}
4964 		limit -= SCTP_SIZE32(plen);
4965 		/*-
4966 		 * All parameters for all chunks that we know/understand are
4967 		 * listed here. We process them other places and make
4968 		 * appropriate stop actions per the upper bits. However this
4969 		 * is the generic routine processor's can call to get back
4970 		 * an operr.. to either incorporate (init-ack) or send.
4971 		 */
4972 		padded_size = SCTP_SIZE32(plen);
4973 		switch (ptype) {
4974 			/* Param's with variable size */
4975 		case SCTP_HEARTBEAT_INFO:
4976 		case SCTP_UNRECOG_PARAM:
4977 		case SCTP_ERROR_CAUSE_IND:
4978 			/* ok skip fwd */
4979 			at += padded_size;
4980 			break;
4981 		case SCTP_STATE_COOKIE:
4982 			if (cookie_found != NULL) {
4983 				*cookie_found = 1;
4984 			}
4985 			at += padded_size;
4986 			break;
4987 			/* Param's with variable size within a range */
4988 		case SCTP_CHUNK_LIST:
4989 		case SCTP_SUPPORTED_CHUNK_EXT:
4990 			if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4991 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4992 				goto invalid_size;
4993 			}
4994 			at += padded_size;
4995 			break;
4996 		case SCTP_SUPPORTED_ADDRTYPE:
4997 			if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4998 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4999 				goto invalid_size;
5000 			}
5001 			at += padded_size;
5002 			break;
5003 		case SCTP_RANDOM:
5004 			if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
5005 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
5006 				goto invalid_size;
5007 			}
5008 			at += padded_size;
5009 			break;
5010 		case SCTP_SET_PRIM_ADDR:
5011 		case SCTP_DEL_IP_ADDRESS:
5012 		case SCTP_ADD_IP_ADDRESS:
5013 			if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
5014 			    (padded_size != sizeof(struct sctp_asconf_addr_param))) {
5015 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
5016 				goto invalid_size;
5017 			}
5018 			at += padded_size;
5019 			break;
5020 			/* Param's with a fixed size */
5021 		case SCTP_IPV4_ADDRESS:
5022 			if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
5023 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
5024 				goto invalid_size;
5025 			}
5026 			at += padded_size;
5027 			break;
5028 		case SCTP_IPV6_ADDRESS:
5029 			if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
5030 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
5031 				goto invalid_size;
5032 			}
5033 			at += padded_size;
5034 			break;
5035 		case SCTP_COOKIE_PRESERVE:
5036 			if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
5037 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
5038 				goto invalid_size;
5039 			}
5040 			at += padded_size;
5041 			break;
5042 		case SCTP_HAS_NAT_SUPPORT:
5043 			*nat_friendly = 1;
5044 			/* FALLTHROUGH */
5045 		case SCTP_PRSCTP_SUPPORTED:
5046 			if (padded_size != sizeof(struct sctp_paramhdr)) {
5047 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen);
5048 				goto invalid_size;
5049 			}
5050 			at += padded_size;
5051 			break;
5052 		case SCTP_ECN_CAPABLE:
5053 			if (padded_size != sizeof(struct sctp_paramhdr)) {
5054 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
5055 				goto invalid_size;
5056 			}
5057 			at += padded_size;
5058 			break;
5059 		case SCTP_ULP_ADAPTATION:
5060 			if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
5061 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
5062 				goto invalid_size;
5063 			}
5064 			at += padded_size;
5065 			break;
5066 		case SCTP_SUCCESS_REPORT:
5067 			if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
5068 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
5069 				goto invalid_size;
5070 			}
5071 			at += padded_size;
5072 			break;
5073 		case SCTP_HOSTNAME_ADDRESS:
5074 			{
5075 				/* Hostname parameters are deprecated. */
5076 				struct sctp_gen_error_cause *cause;
5077 				int l_len;
5078 
5079 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
5080 				*abort_processing = 1;
5081 				sctp_m_freem(op_err);
5082 				op_err = NULL;
5083 				op_err_last = NULL;
5084 #ifdef INET6
5085 				l_len = SCTP_MIN_OVERHEAD;
5086 #else
5087 				l_len = SCTP_MIN_V4_OVERHEAD;
5088 #endif
5089 				l_len += sizeof(struct sctp_chunkhdr);
5090 				l_len += sizeof(struct sctp_gen_error_cause);
5091 				op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5092 				if (op_err != NULL) {
5093 					/*
5094 					 * Pre-reserve space for IP, SCTP,
5095 					 * and chunk header.
5096 					 */
5097 #ifdef INET6
5098 					SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5099 #else
5100 					SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5101 #endif
5102 					SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5103 					SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5104 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5105 					cause = mtod(op_err, struct sctp_gen_error_cause *);
5106 					cause->code = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
5107 					cause->length = htons((uint16_t)(sizeof(struct sctp_gen_error_cause) + plen));
5108 					SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT);
5109 					if (SCTP_BUF_NEXT(op_err) == NULL) {
5110 						sctp_m_freem(op_err);
5111 						op_err = NULL;
5112 						op_err_last = NULL;
5113 					}
5114 				}
5115 				return (op_err);
5116 			}
5117 		default:
5118 			/*
5119 			 * we do not recognize the parameter figure out what
5120 			 * we do.
5121 			 */
5122 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
5123 			if ((ptype & 0x4000) == 0x4000) {
5124 				/* Report bit is set?? */
5125 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
5126 				if (op_err == NULL) {
5127 					int l_len;
5128 
5129 					/* Ok need to try to get an mbuf */
5130 #ifdef INET6
5131 					l_len = SCTP_MIN_OVERHEAD;
5132 #else
5133 					l_len = SCTP_MIN_V4_OVERHEAD;
5134 #endif
5135 					l_len += sizeof(struct sctp_chunkhdr);
5136 					l_len += sizeof(struct sctp_paramhdr);
5137 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5138 					if (op_err) {
5139 						SCTP_BUF_LEN(op_err) = 0;
5140 #ifdef INET6
5141 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5142 #else
5143 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5144 #endif
5145 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5146 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5147 						op_err_last = op_err;
5148 					}
5149 				}
5150 				if (op_err != NULL) {
5151 					/* If we have space */
5152 					struct sctp_paramhdr *param;
5153 
5154 					if (pad_needed > 0) {
5155 						op_err_last = sctp_add_pad_tombuf(op_err_last, pad_needed);
5156 					}
5157 					if (op_err_last == NULL) {
5158 						sctp_m_freem(op_err);
5159 						op_err = NULL;
5160 						op_err_last = NULL;
5161 						goto more_processing;
5162 					}
5163 					if (M_TRAILINGSPACE(op_err_last) < (int)sizeof(struct sctp_paramhdr)) {
5164 						m_tmp = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
5165 						if (m_tmp == NULL) {
5166 							sctp_m_freem(op_err);
5167 							op_err = NULL;
5168 							op_err_last = NULL;
5169 							goto more_processing;
5170 						}
5171 						SCTP_BUF_LEN(m_tmp) = 0;
5172 						SCTP_BUF_NEXT(m_tmp) = NULL;
5173 						SCTP_BUF_NEXT(op_err_last) = m_tmp;
5174 						op_err_last = m_tmp;
5175 					}
5176 					param = (struct sctp_paramhdr *)(mtod(op_err_last, caddr_t)+SCTP_BUF_LEN(op_err_last));
5177 					param->param_type = htons(SCTP_UNRECOG_PARAM);
5178 					param->param_length = htons((uint16_t)sizeof(struct sctp_paramhdr) + plen);
5179 					SCTP_BUF_LEN(op_err_last) += sizeof(struct sctp_paramhdr);
5180 					SCTP_BUF_NEXT(op_err_last) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT);
5181 					if (SCTP_BUF_NEXT(op_err_last) == NULL) {
5182 						sctp_m_freem(op_err);
5183 						op_err = NULL;
5184 						op_err_last = NULL;
5185 						goto more_processing;
5186 					} else {
5187 						while (SCTP_BUF_NEXT(op_err_last) != NULL) {
5188 							op_err_last = SCTP_BUF_NEXT(op_err_last);
5189 						}
5190 					}
5191 					if (plen % 4 != 0) {
5192 						pad_needed = 4 - (plen % 4);
5193 					} else {
5194 						pad_needed = 0;
5195 					}
5196 				}
5197 			}
5198 	more_processing:
5199 			if ((ptype & 0x8000) == 0x0000) {
5200 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
5201 				return (op_err);
5202 			} else {
5203 				/* skip this chunk and continue processing */
5204 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
5205 				at += SCTP_SIZE32(plen);
5206 			}
5207 			break;
5208 		}
5209 		phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
5210 	}
5211 	return (op_err);
5212 invalid_size:
5213 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
5214 	*abort_processing = 1;
5215 	sctp_m_freem(op_err);
5216 	op_err = NULL;
5217 	op_err_last = NULL;
5218 	if (phdr != NULL) {
5219 		struct sctp_paramhdr *param;
5220 		int l_len;
5221 #ifdef INET6
5222 		l_len = SCTP_MIN_OVERHEAD;
5223 #else
5224 		l_len = SCTP_MIN_V4_OVERHEAD;
5225 #endif
5226 		l_len += sizeof(struct sctp_chunkhdr);
5227 		l_len += (2 * sizeof(struct sctp_paramhdr));
5228 		op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5229 		if (op_err) {
5230 			SCTP_BUF_LEN(op_err) = 0;
5231 #ifdef INET6
5232 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5233 #else
5234 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5235 #endif
5236 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5237 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5238 			SCTP_BUF_LEN(op_err) = 2 * sizeof(struct sctp_paramhdr);
5239 			param = mtod(op_err, struct sctp_paramhdr *);
5240 			param->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5241 			param->param_length = htons(2 * sizeof(struct sctp_paramhdr));
5242 			param++;
5243 			param->param_type = htons(ptype);
5244 			param->param_length = htons(plen);
5245 		}
5246 	}
5247 	return (op_err);
5248 }
5249 
5250 /*
5251  * Given a INIT chunk, look through the parameters to verify that there
5252  * are no new addresses.
5253  * Return true, if there is a new address or there is a problem parsing
5254    the parameters. Provide an optional error cause used when sending an ABORT.
5255  * Return false, if there are no new addresses and there is no problem in
5256    parameter processing.
5257  */
5258 static bool
5259 sctp_are_there_new_addresses(struct sctp_association *asoc,
5260     struct mbuf *in_initpkt, int offset, int limit, struct sockaddr *src,
5261     struct mbuf **op_err)
5262 {
5263 	struct sockaddr *sa_touse;
5264 	struct sockaddr *sa;
5265 	struct sctp_paramhdr *phdr, params;
5266 	struct sctp_nets *net;
5267 #ifdef INET
5268 	struct sockaddr_in sin4, *sa4;
5269 #endif
5270 #ifdef INET6
5271 	struct sockaddr_in6 sin6, *sa6;
5272 #endif
5273 	uint16_t ptype, plen;
5274 	bool fnd, check_src;
5275 
5276 	*op_err = NULL;
5277 #ifdef INET
5278 	memset(&sin4, 0, sizeof(sin4));
5279 	sin4.sin_family = AF_INET;
5280 	sin4.sin_len = sizeof(sin4);
5281 #endif
5282 #ifdef INET6
5283 	memset(&sin6, 0, sizeof(sin6));
5284 	sin6.sin6_family = AF_INET6;
5285 	sin6.sin6_len = sizeof(sin6);
5286 #endif
5287 	/* First what about the src address of the pkt ? */
5288 	check_src = false;
5289 	switch (src->sa_family) {
5290 #ifdef INET
5291 	case AF_INET:
5292 		if (asoc->scope.ipv4_addr_legal) {
5293 			check_src = true;
5294 		}
5295 		break;
5296 #endif
5297 #ifdef INET6
5298 	case AF_INET6:
5299 		if (asoc->scope.ipv6_addr_legal) {
5300 			check_src = true;
5301 		}
5302 		break;
5303 #endif
5304 	default:
5305 		/* TSNH */
5306 		break;
5307 	}
5308 	if (check_src) {
5309 		fnd = false;
5310 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5311 			sa = (struct sockaddr *)&net->ro._l_addr;
5312 			if (sa->sa_family == src->sa_family) {
5313 #ifdef INET
5314 				if (sa->sa_family == AF_INET) {
5315 					struct sockaddr_in *src4;
5316 
5317 					sa4 = (struct sockaddr_in *)sa;
5318 					src4 = (struct sockaddr_in *)src;
5319 					if (sa4->sin_addr.s_addr == src4->sin_addr.s_addr) {
5320 						fnd = true;
5321 						break;
5322 					}
5323 				}
5324 #endif
5325 #ifdef INET6
5326 				if (sa->sa_family == AF_INET6) {
5327 					struct sockaddr_in6 *src6;
5328 
5329 					sa6 = (struct sockaddr_in6 *)sa;
5330 					src6 = (struct sockaddr_in6 *)src;
5331 					if (SCTP6_ARE_ADDR_EQUAL(sa6, src6)) {
5332 						fnd = true;
5333 						break;
5334 					}
5335 				}
5336 #endif
5337 			}
5338 		}
5339 		if (!fnd) {
5340 			/*
5341 			 * If sending an ABORT in case of an additional
5342 			 * address, don't use the new address error cause.
5343 			 * This looks no different than if no listener was
5344 			 * present.
5345 			 */
5346 			*op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), "Address added");
5347 			return (true);
5348 		}
5349 	}
5350 	/* Ok so far lets munge through the rest of the packet */
5351 	offset += sizeof(struct sctp_init_chunk);
5352 	phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
5353 	while (phdr) {
5354 		sa_touse = NULL;
5355 		ptype = ntohs(phdr->param_type);
5356 		plen = ntohs(phdr->param_length);
5357 		if (offset + plen > limit) {
5358 			*op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Partial parameter");
5359 			return (true);
5360 		}
5361 		if (plen < sizeof(struct sctp_paramhdr)) {
5362 			*op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length too small");
5363 			return (true);
5364 		}
5365 		switch (ptype) {
5366 #ifdef INET
5367 		case SCTP_IPV4_ADDRESS:
5368 			{
5369 				struct sctp_ipv4addr_param *p4, p4_buf;
5370 
5371 				if (plen != sizeof(struct sctp_ipv4addr_param)) {
5372 					*op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length illegal");
5373 					return (true);
5374 				}
5375 				phdr = sctp_get_next_param(in_initpkt, offset,
5376 				    (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
5377 				if (phdr == NULL) {
5378 					*op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
5379 					return (true);
5380 				}
5381 				if (asoc->scope.ipv4_addr_legal) {
5382 					p4 = (struct sctp_ipv4addr_param *)phdr;
5383 					sin4.sin_addr.s_addr = p4->addr;
5384 					sa_touse = (struct sockaddr *)&sin4;
5385 				}
5386 				break;
5387 			}
5388 #endif
5389 #ifdef INET6
5390 		case SCTP_IPV6_ADDRESS:
5391 			{
5392 				struct sctp_ipv6addr_param *p6, p6_buf;
5393 
5394 				if (plen != sizeof(struct sctp_ipv6addr_param)) {
5395 					*op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length illegal");
5396 					return (true);
5397 				}
5398 				phdr = sctp_get_next_param(in_initpkt, offset,
5399 				    (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
5400 				if (phdr == NULL) {
5401 					*op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
5402 					return (true);
5403 				}
5404 				if (asoc->scope.ipv6_addr_legal) {
5405 					p6 = (struct sctp_ipv6addr_param *)phdr;
5406 					memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
5407 					    sizeof(p6->addr));
5408 					sa_touse = (struct sockaddr *)&sin6;
5409 				}
5410 				break;
5411 			}
5412 #endif
5413 		default:
5414 			sa_touse = NULL;
5415 			break;
5416 		}
5417 		if (sa_touse) {
5418 			/* ok, sa_touse points to one to check */
5419 			fnd = false;
5420 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5421 				sa = (struct sockaddr *)&net->ro._l_addr;
5422 				if (sa->sa_family != sa_touse->sa_family) {
5423 					continue;
5424 				}
5425 #ifdef INET
5426 				if (sa->sa_family == AF_INET) {
5427 					sa4 = (struct sockaddr_in *)sa;
5428 					if (sa4->sin_addr.s_addr ==
5429 					    sin4.sin_addr.s_addr) {
5430 						fnd = true;
5431 						break;
5432 					}
5433 				}
5434 #endif
5435 #ifdef INET6
5436 				if (sa->sa_family == AF_INET6) {
5437 					sa6 = (struct sockaddr_in6 *)sa;
5438 					if (SCTP6_ARE_ADDR_EQUAL(
5439 					    sa6, &sin6)) {
5440 						fnd = true;
5441 						break;
5442 					}
5443 				}
5444 #endif
5445 			}
5446 			if (!fnd) {
5447 				/*
5448 				 * If sending an ABORT in case of an
5449 				 * additional address, don't use the new
5450 				 * address error cause. This looks no
5451 				 * different than if no listener was
5452 				 * present.
5453 				 */
5454 				*op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), "Address added");
5455 				return (true);
5456 			}
5457 		}
5458 		offset += SCTP_SIZE32(plen);
5459 		if (offset >= limit) {
5460 			break;
5461 		}
5462 		phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
5463 	}
5464 	return (false);
5465 }
5466 
5467 /*
5468  * Given a MBUF chain that was sent into us containing an INIT. Build a
5469  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
5470  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
5471  * message (i.e. the struct sctp_init_msg).
5472  */
5473 void
5474 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
5475     struct sctp_nets *src_net, struct mbuf *init_pkt,
5476     int iphlen, int offset,
5477     struct sockaddr *src, struct sockaddr *dst,
5478     struct sctphdr *sh, struct sctp_init_chunk *init_chk,
5479     uint8_t mflowtype, uint32_t mflowid,
5480     uint32_t vrf_id, uint16_t port)
5481 {
5482 	struct sctp_association *asoc;
5483 	struct mbuf *m, *m_tmp, *m_last, *m_cookie, *op_err;
5484 	struct sctp_init_ack_chunk *initack;
5485 	struct sctp_adaptation_layer_indication *ali;
5486 	struct sctp_zero_checksum_acceptable *zero_chksum;
5487 	struct sctp_supported_chunk_types_param *pr_supported;
5488 	struct sctp_paramhdr *ph;
5489 	union sctp_sockstore *over_addr;
5490 	struct sctp_scoping scp;
5491 	struct timeval now;
5492 #ifdef INET
5493 	struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
5494 	struct sockaddr_in *src4 = (struct sockaddr_in *)src;
5495 	struct sockaddr_in *sin;
5496 #endif
5497 #ifdef INET6
5498 	struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
5499 	struct sockaddr_in6 *src6 = (struct sockaddr_in6 *)src;
5500 	struct sockaddr_in6 *sin6;
5501 #endif
5502 	struct sockaddr *to;
5503 	struct sctp_state_cookie stc;
5504 	struct sctp_nets *net = NULL;
5505 	uint8_t *signature = NULL;
5506 	int cnt_inits_to = 0;
5507 	uint16_t his_limit, i_want;
5508 	int abort_flag;
5509 	int nat_friendly = 0;
5510 	int error;
5511 	struct socket *so;
5512 	uint16_t num_ext, chunk_len, padding_len, parameter_len;
5513 
5514 	if (stcb) {
5515 		asoc = &stcb->asoc;
5516 	} else {
5517 		asoc = NULL;
5518 	}
5519 	if ((asoc != NULL) &&
5520 	    (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT)) {
5521 		if (sctp_are_there_new_addresses(asoc, init_pkt, offset, offset + ntohs(init_chk->ch.chunk_length), src, &op_err)) {
5522 			/*
5523 			 * new addresses, out of here in non-cookie-wait
5524 			 * states
5525 			 */
5526 			sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err,
5527 			    mflowtype, mflowid, inp->fibnum,
5528 			    vrf_id, port);
5529 			return;
5530 		}
5531 		if (src_net != NULL && (src_net->port != port)) {
5532 			/*
5533 			 * change of remote encapsulation port, out of here
5534 			 * in non-cookie-wait states
5535 			 *
5536 			 * Send an ABORT, without an specific error cause.
5537 			 * This looks no different than if no listener was
5538 			 * present.
5539 			 */
5540 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5541 			    "Remote encapsulation port changed");
5542 			sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err,
5543 			    mflowtype, mflowid, inp->fibnum,
5544 			    vrf_id, port);
5545 			return;
5546 		}
5547 	}
5548 	abort_flag = 0;
5549 	op_err = sctp_arethere_unrecognized_parameters(init_pkt,
5550 	    (offset + sizeof(struct sctp_init_chunk)),
5551 	    &abort_flag,
5552 	    (struct sctp_chunkhdr *)init_chk,
5553 	    &nat_friendly, NULL);
5554 	if (abort_flag) {
5555 do_a_abort:
5556 		if (op_err == NULL) {
5557 			char msg[SCTP_DIAG_INFO_LEN];
5558 
5559 			SCTP_SNPRINTF(msg, sizeof(msg), "%s:%d at %s", __FILE__, __LINE__, __func__);
5560 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5561 			    msg);
5562 		}
5563 		sctp_send_abort(init_pkt, iphlen, src, dst, sh,
5564 		    init_chk->init.initiate_tag, op_err,
5565 		    mflowtype, mflowid, inp->fibnum,
5566 		    vrf_id, port);
5567 		return;
5568 	}
5569 	m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
5570 	if (m == NULL) {
5571 		/* No memory, INIT timer will re-attempt. */
5572 		sctp_m_freem(op_err);
5573 		return;
5574 	}
5575 	chunk_len = (uint16_t)sizeof(struct sctp_init_ack_chunk);
5576 	padding_len = 0;
5577 
5578 	/*
5579 	 * We might not overwrite the identification[] completely and on
5580 	 * some platforms time_entered will contain some padding. Therefore
5581 	 * zero out the cookie to avoid putting uninitialized memory on the
5582 	 * wire.
5583 	 */
5584 	memset(&stc, 0, sizeof(struct sctp_state_cookie));
5585 
5586 	/* the time I built cookie */
5587 	(void)SCTP_GETTIME_TIMEVAL(&now);
5588 	stc.time_entered.tv_sec = now.tv_sec;
5589 	stc.time_entered.tv_usec = now.tv_usec;
5590 
5591 	/* populate any tie tags */
5592 	if (asoc != NULL) {
5593 		/* unlock before tag selections */
5594 		stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5595 		stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5596 		stc.cookie_life = asoc->cookie_life;
5597 		net = asoc->primary_destination;
5598 	} else {
5599 		stc.tie_tag_my_vtag = 0;
5600 		stc.tie_tag_peer_vtag = 0;
5601 		/* life I will award this cookie */
5602 		stc.cookie_life = inp->sctp_ep.def_cookie_life;
5603 	}
5604 
5605 	/* copy in the ports for later check */
5606 	stc.myport = sh->dest_port;
5607 	stc.peerport = sh->src_port;
5608 
5609 	/*
5610 	 * If we wanted to honor cookie life extensions, we would add to
5611 	 * stc.cookie_life. For now we should NOT honor any extension
5612 	 */
5613 	stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5614 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5615 		stc.ipv6_addr_legal = 1;
5616 		if (SCTP_IPV6_V6ONLY(inp)) {
5617 			stc.ipv4_addr_legal = 0;
5618 		} else {
5619 			stc.ipv4_addr_legal = 1;
5620 		}
5621 	} else {
5622 		stc.ipv6_addr_legal = 0;
5623 		stc.ipv4_addr_legal = 1;
5624 	}
5625 	stc.ipv4_scope = 0;
5626 	if (net == NULL) {
5627 		to = src;
5628 		switch (dst->sa_family) {
5629 #ifdef INET
5630 		case AF_INET:
5631 			{
5632 				/* lookup address */
5633 				stc.address[0] = src4->sin_addr.s_addr;
5634 				stc.address[1] = 0;
5635 				stc.address[2] = 0;
5636 				stc.address[3] = 0;
5637 				stc.addr_type = SCTP_IPV4_ADDRESS;
5638 				/* local from address */
5639 				stc.laddress[0] = dst4->sin_addr.s_addr;
5640 				stc.laddress[1] = 0;
5641 				stc.laddress[2] = 0;
5642 				stc.laddress[3] = 0;
5643 				stc.laddr_type = SCTP_IPV4_ADDRESS;
5644 				/* scope_id is only for v6 */
5645 				stc.scope_id = 0;
5646 				if ((IN4_ISPRIVATE_ADDRESS(&src4->sin_addr)) ||
5647 				    (IN4_ISPRIVATE_ADDRESS(&dst4->sin_addr))) {
5648 					stc.ipv4_scope = 1;
5649 				}
5650 				/* Must use the address in this case */
5651 				if (sctp_is_address_on_local_host(src, vrf_id)) {
5652 					stc.loopback_scope = 1;
5653 					stc.ipv4_scope = 1;
5654 					stc.site_scope = 1;
5655 					stc.local_scope = 0;
5656 				}
5657 				break;
5658 			}
5659 #endif
5660 #ifdef INET6
5661 		case AF_INET6:
5662 			{
5663 				stc.addr_type = SCTP_IPV6_ADDRESS;
5664 				memcpy(&stc.address, &src6->sin6_addr, sizeof(struct in6_addr));
5665 				stc.scope_id = ntohs(in6_getscope(&src6->sin6_addr));
5666 				if (sctp_is_address_on_local_host(src, vrf_id)) {
5667 					stc.loopback_scope = 1;
5668 					stc.local_scope = 0;
5669 					stc.site_scope = 1;
5670 					stc.ipv4_scope = 1;
5671 				} else if (IN6_IS_ADDR_LINKLOCAL(&src6->sin6_addr) ||
5672 				    IN6_IS_ADDR_LINKLOCAL(&dst6->sin6_addr)) {
5673 					/*
5674 					 * If the new destination or source
5675 					 * is a LINK_LOCAL we must have
5676 					 * common both site and local scope.
5677 					 * Don't set local scope though
5678 					 * since we must depend on the
5679 					 * source to be added implicitly. We
5680 					 * cannot assure just because we
5681 					 * share one link that all links are
5682 					 * common.
5683 					 */
5684 					stc.local_scope = 0;
5685 					stc.site_scope = 1;
5686 					stc.ipv4_scope = 1;
5687 					/*
5688 					 * we start counting for the private
5689 					 * address stuff at 1. since the
5690 					 * link local we source from won't
5691 					 * show up in our scoped count.
5692 					 */
5693 					cnt_inits_to = 1;
5694 					/*
5695 					 * pull out the scope_id from
5696 					 * incoming pkt
5697 					 */
5698 				} else if (IN6_IS_ADDR_SITELOCAL(&src6->sin6_addr) ||
5699 				    IN6_IS_ADDR_SITELOCAL(&dst6->sin6_addr)) {
5700 					/*
5701 					 * If the new destination or source
5702 					 * is SITE_LOCAL then we must have
5703 					 * site scope in common.
5704 					 */
5705 					stc.site_scope = 1;
5706 				}
5707 				memcpy(&stc.laddress, &dst6->sin6_addr, sizeof(struct in6_addr));
5708 				stc.laddr_type = SCTP_IPV6_ADDRESS;
5709 				break;
5710 			}
5711 #endif
5712 		default:
5713 			/* TSNH */
5714 			goto do_a_abort;
5715 			break;
5716 		}
5717 	} else {
5718 		/* set the scope per the existing tcb */
5719 
5720 #ifdef INET6
5721 		struct sctp_nets *lnet;
5722 #endif
5723 
5724 		stc.loopback_scope = asoc->scope.loopback_scope;
5725 		stc.ipv4_scope = asoc->scope.ipv4_local_scope;
5726 		stc.site_scope = asoc->scope.site_scope;
5727 		stc.local_scope = asoc->scope.local_scope;
5728 #ifdef INET6
5729 		/* Why do we not consider IPv4 LL addresses? */
5730 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5731 			if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5732 				if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5733 					/*
5734 					 * if we have a LL address, start
5735 					 * counting at 1.
5736 					 */
5737 					cnt_inits_to = 1;
5738 				}
5739 			}
5740 		}
5741 #endif
5742 		/* use the net pointer */
5743 		to = (struct sockaddr *)&net->ro._l_addr;
5744 		switch (to->sa_family) {
5745 #ifdef INET
5746 		case AF_INET:
5747 			sin = (struct sockaddr_in *)to;
5748 			stc.address[0] = sin->sin_addr.s_addr;
5749 			stc.address[1] = 0;
5750 			stc.address[2] = 0;
5751 			stc.address[3] = 0;
5752 			stc.addr_type = SCTP_IPV4_ADDRESS;
5753 			if (net->src_addr_selected == 0) {
5754 				/*
5755 				 * strange case here, the INIT should have
5756 				 * did the selection.
5757 				 */
5758 				net->ro._s_addr = sctp_source_address_selection(inp,
5759 				    stcb, (sctp_route_t *)&net->ro,
5760 				    net, 0, vrf_id);
5761 				if (net->ro._s_addr == NULL) {
5762 					sctp_m_freem(op_err);
5763 					sctp_m_freem(m);
5764 					return;
5765 				}
5766 
5767 				net->src_addr_selected = 1;
5768 			}
5769 			stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5770 			stc.laddress[1] = 0;
5771 			stc.laddress[2] = 0;
5772 			stc.laddress[3] = 0;
5773 			stc.laddr_type = SCTP_IPV4_ADDRESS;
5774 			/* scope_id is only for v6 */
5775 			stc.scope_id = 0;
5776 			break;
5777 #endif
5778 #ifdef INET6
5779 		case AF_INET6:
5780 			sin6 = (struct sockaddr_in6 *)to;
5781 			memcpy(&stc.address, &sin6->sin6_addr,
5782 			    sizeof(struct in6_addr));
5783 			stc.addr_type = SCTP_IPV6_ADDRESS;
5784 			stc.scope_id = sin6->sin6_scope_id;
5785 			if (net->src_addr_selected == 0) {
5786 				/*
5787 				 * strange case here, the INIT should have
5788 				 * done the selection.
5789 				 */
5790 				net->ro._s_addr = sctp_source_address_selection(inp,
5791 				    stcb, (sctp_route_t *)&net->ro,
5792 				    net, 0, vrf_id);
5793 				if (net->ro._s_addr == NULL) {
5794 					sctp_m_freem(op_err);
5795 					sctp_m_freem(m);
5796 					return;
5797 				}
5798 
5799 				net->src_addr_selected = 1;
5800 			}
5801 			memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5802 			    sizeof(struct in6_addr));
5803 			stc.laddr_type = SCTP_IPV6_ADDRESS;
5804 			break;
5805 #endif
5806 		}
5807 	}
5808 	if (asoc != NULL) {
5809 		stc.rcv_edmid = asoc->rcv_edmid;
5810 	} else {
5811 		stc.rcv_edmid = inp->rcv_edmid;
5812 	}
5813 	/* Now lets put the SCTP header in place */
5814 	initack = mtod(m, struct sctp_init_ack_chunk *);
5815 	/* Save it off for quick ref */
5816 	stc.peers_vtag = ntohl(init_chk->init.initiate_tag);
5817 	/* who are we */
5818 	memcpy(stc.identification, SCTP_VERSION_STRING,
5819 	    min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5820 	memset(stc.reserved, 0, SCTP_RESERVE_SPACE);
5821 	/* now the chunk header */
5822 	initack->ch.chunk_type = SCTP_INITIATION_ACK;
5823 	initack->ch.chunk_flags = 0;
5824 	/* fill in later from mbuf we build */
5825 	initack->ch.chunk_length = 0;
5826 	/* place in my tag */
5827 	if ((asoc != NULL) &&
5828 	    ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
5829 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_INUSE) ||
5830 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED))) {
5831 		/* re-use the v-tags and init-seq here */
5832 		initack->init.initiate_tag = htonl(asoc->my_vtag);
5833 		initack->init.initial_tsn = htonl(asoc->init_seq_number);
5834 	} else {
5835 		uint32_t vtag, itsn;
5836 
5837 		if (asoc) {
5838 			atomic_add_int(&asoc->refcnt, 1);
5839 			SCTP_TCB_UNLOCK(stcb);
5840 	new_tag:
5841 			SCTP_INP_INFO_RLOCK();
5842 			vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5843 			SCTP_INP_INFO_RUNLOCK();
5844 			if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5845 				/*
5846 				 * Got a duplicate vtag on some guy behind a
5847 				 * nat make sure we don't use it.
5848 				 */
5849 				goto new_tag;
5850 			}
5851 			initack->init.initiate_tag = htonl(vtag);
5852 			/* get a TSN to use too */
5853 			itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5854 			initack->init.initial_tsn = htonl(itsn);
5855 			SCTP_TCB_LOCK(stcb);
5856 			atomic_subtract_int(&asoc->refcnt, 1);
5857 		} else {
5858 			SCTP_INP_INCR_REF(inp);
5859 			SCTP_INP_RUNLOCK(inp);
5860 			SCTP_INP_INFO_RLOCK();
5861 			vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5862 			SCTP_INP_INFO_RUNLOCK();
5863 			initack->init.initiate_tag = htonl(vtag);
5864 			/* get a TSN to use too */
5865 			initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5866 			SCTP_INP_RLOCK(inp);
5867 			SCTP_INP_DECR_REF(inp);
5868 		}
5869 	}
5870 	/* save away my tag to */
5871 	stc.my_vtag = initack->init.initiate_tag;
5872 
5873 	/* set up some of the credits. */
5874 	so = inp->sctp_socket;
5875 	if (so == NULL) {
5876 		/* memory problem */
5877 		sctp_m_freem(op_err);
5878 		sctp_m_freem(m);
5879 		return;
5880 	} else {
5881 		initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5882 	}
5883 	/* set what I want */
5884 	his_limit = ntohs(init_chk->init.num_inbound_streams);
5885 	/* choose what I want */
5886 	if (asoc != NULL) {
5887 		if (asoc->streamoutcnt > asoc->pre_open_streams) {
5888 			i_want = asoc->streamoutcnt;
5889 		} else {
5890 			i_want = asoc->pre_open_streams;
5891 		}
5892 	} else {
5893 		i_want = inp->sctp_ep.pre_open_stream_count;
5894 	}
5895 	if (his_limit < i_want) {
5896 		/* I Want more :< */
5897 		initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5898 	} else {
5899 		/* I can have what I want :> */
5900 		initack->init.num_outbound_streams = htons(i_want);
5901 	}
5902 	/* tell him his limit. */
5903 	initack->init.num_inbound_streams =
5904 	    htons(inp->sctp_ep.max_open_streams_intome);
5905 
5906 	/* adaptation layer indication parameter */
5907 	if (inp->sctp_ep.adaptation_layer_indicator_provided) {
5908 		parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication);
5909 		ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t)+chunk_len);
5910 		ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5911 		ali->ph.param_length = htons(parameter_len);
5912 		ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator);
5913 		chunk_len += parameter_len;
5914 	}
5915 
5916 	/* ECN parameter */
5917 	if (((asoc != NULL) && (asoc->ecn_supported == 1)) ||
5918 	    ((asoc == NULL) && (inp->ecn_supported == 1))) {
5919 		parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5920 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
5921 		ph->param_type = htons(SCTP_ECN_CAPABLE);
5922 		ph->param_length = htons(parameter_len);
5923 		chunk_len += parameter_len;
5924 	}
5925 
5926 	/* PR-SCTP supported parameter */
5927 	if (((asoc != NULL) && (asoc->prsctp_supported == 1)) ||
5928 	    ((asoc == NULL) && (inp->prsctp_supported == 1))) {
5929 		parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5930 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
5931 		ph->param_type = htons(SCTP_PRSCTP_SUPPORTED);
5932 		ph->param_length = htons(parameter_len);
5933 		chunk_len += parameter_len;
5934 	}
5935 
5936 	/* Zero checksum acceptable parameter */
5937 	if (((asoc != NULL) && (asoc->rcv_edmid != SCTP_EDMID_NONE)) ||
5938 	    ((asoc == NULL) && (inp->rcv_edmid != SCTP_EDMID_NONE))) {
5939 		parameter_len = (uint16_t)sizeof(struct sctp_zero_checksum_acceptable);
5940 		zero_chksum = (struct sctp_zero_checksum_acceptable *)(mtod(m, caddr_t)+chunk_len);
5941 		zero_chksum->ph.param_type = htons(SCTP_ZERO_CHECKSUM_ACCEPTABLE);
5942 		zero_chksum->ph.param_length = htons(parameter_len);
5943 		if (asoc != NULL) {
5944 			zero_chksum->edmid = htonl(asoc->rcv_edmid);
5945 		} else {
5946 			zero_chksum->edmid = htonl(inp->rcv_edmid);
5947 		}
5948 		chunk_len += parameter_len;
5949 	}
5950 
5951 	/* Add NAT friendly parameter */
5952 	if (nat_friendly) {
5953 		parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5954 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
5955 		ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5956 		ph->param_length = htons(parameter_len);
5957 		chunk_len += parameter_len;
5958 	}
5959 
5960 	/* And now tell the peer which extensions we support */
5961 	num_ext = 0;
5962 	pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+chunk_len);
5963 	if (((asoc != NULL) && (asoc->prsctp_supported == 1)) ||
5964 	    ((asoc == NULL) && (inp->prsctp_supported == 1))) {
5965 		pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5966 		if (((asoc != NULL) && (asoc->idata_supported == 1)) ||
5967 		    ((asoc == NULL) && (inp->idata_supported == 1))) {
5968 			pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN;
5969 		}
5970 	}
5971 	if (((asoc != NULL) && (asoc->auth_supported == 1)) ||
5972 	    ((asoc == NULL) && (inp->auth_supported == 1))) {
5973 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5974 	}
5975 	if (((asoc != NULL) && (asoc->asconf_supported == 1)) ||
5976 	    ((asoc == NULL) && (inp->asconf_supported == 1))) {
5977 		pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5978 		pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5979 	}
5980 	if (((asoc != NULL) && (asoc->reconfig_supported == 1)) ||
5981 	    ((asoc == NULL) && (inp->reconfig_supported == 1))) {
5982 		pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5983 	}
5984 	if (((asoc != NULL) && (asoc->idata_supported == 1)) ||
5985 	    ((asoc == NULL) && (inp->idata_supported == 1))) {
5986 		pr_supported->chunk_types[num_ext++] = SCTP_IDATA;
5987 	}
5988 	if (((asoc != NULL) && (asoc->nrsack_supported == 1)) ||
5989 	    ((asoc == NULL) && (inp->nrsack_supported == 1))) {
5990 		pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5991 	}
5992 	if (((asoc != NULL) && (asoc->pktdrop_supported == 1)) ||
5993 	    ((asoc == NULL) && (inp->pktdrop_supported == 1))) {
5994 		pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5995 	}
5996 	if (num_ext > 0) {
5997 		parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext;
5998 		pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5999 		pr_supported->ph.param_length = htons(parameter_len);
6000 		padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6001 		chunk_len += parameter_len;
6002 	}
6003 
6004 	/* add authentication parameters */
6005 	if (((asoc != NULL) && (asoc->auth_supported == 1)) ||
6006 	    ((asoc == NULL) && (inp->auth_supported == 1))) {
6007 		struct sctp_auth_random *randp;
6008 		struct sctp_auth_hmac_algo *hmacs;
6009 		struct sctp_auth_chunk_list *chunks;
6010 
6011 		if (padding_len > 0) {
6012 			memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
6013 			chunk_len += padding_len;
6014 			padding_len = 0;
6015 		}
6016 		/* generate and add RANDOM parameter */
6017 		randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+chunk_len);
6018 		parameter_len = (uint16_t)sizeof(struct sctp_auth_random) +
6019 		    SCTP_AUTH_RANDOM_SIZE_DEFAULT;
6020 		randp->ph.param_type = htons(SCTP_RANDOM);
6021 		randp->ph.param_length = htons(parameter_len);
6022 		SCTP_READ_RANDOM(randp->random_data, SCTP_AUTH_RANDOM_SIZE_DEFAULT);
6023 		padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6024 		chunk_len += parameter_len;
6025 
6026 		if (padding_len > 0) {
6027 			memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
6028 			chunk_len += padding_len;
6029 			padding_len = 0;
6030 		}
6031 		/* add HMAC_ALGO parameter */
6032 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+chunk_len);
6033 		parameter_len = (uint16_t)sizeof(struct sctp_auth_hmac_algo) +
6034 		    sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
6035 		    (uint8_t *)hmacs->hmac_ids);
6036 		hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
6037 		hmacs->ph.param_length = htons(parameter_len);
6038 		padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6039 		chunk_len += parameter_len;
6040 
6041 		if (padding_len > 0) {
6042 			memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
6043 			chunk_len += padding_len;
6044 			padding_len = 0;
6045 		}
6046 		/* add CHUNKS parameter */
6047 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+chunk_len);
6048 		parameter_len = (uint16_t)sizeof(struct sctp_auth_chunk_list) +
6049 		    sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
6050 		    chunks->chunk_types);
6051 		chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
6052 		chunks->ph.param_length = htons(parameter_len);
6053 		padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6054 		chunk_len += parameter_len;
6055 	}
6056 	SCTP_BUF_LEN(m) = chunk_len;
6057 	m_last = m;
6058 	/* now the addresses */
6059 	/*
6060 	 * To optimize this we could put the scoping stuff into a structure
6061 	 * and remove the individual uint8's from the stc structure. Then we
6062 	 * could just sifa in the address within the stc.. but for now this
6063 	 * is a quick hack to get the address stuff teased apart.
6064 	 */
6065 	scp.ipv4_addr_legal = stc.ipv4_addr_legal;
6066 	scp.ipv6_addr_legal = stc.ipv6_addr_legal;
6067 	scp.loopback_scope = stc.loopback_scope;
6068 	scp.ipv4_local_scope = stc.ipv4_scope;
6069 	scp.local_scope = stc.local_scope;
6070 	scp.site_scope = stc.site_scope;
6071 	m_last = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_last,
6072 	    cnt_inits_to,
6073 	    &padding_len, &chunk_len);
6074 	/* padding_len can only be positive, if no addresses have been added */
6075 	if (padding_len > 0) {
6076 		memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
6077 		chunk_len += padding_len;
6078 		SCTP_BUF_LEN(m) += padding_len;
6079 		padding_len = 0;
6080 	}
6081 
6082 	/* tack on the operational error if present */
6083 	if (op_err) {
6084 		parameter_len = 0;
6085 		for (m_tmp = op_err; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
6086 			parameter_len += SCTP_BUF_LEN(m_tmp);
6087 		}
6088 		padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6089 		SCTP_BUF_NEXT(m_last) = op_err;
6090 		while (SCTP_BUF_NEXT(m_last) != NULL) {
6091 			m_last = SCTP_BUF_NEXT(m_last);
6092 		}
6093 		chunk_len += parameter_len;
6094 	}
6095 	if (padding_len > 0) {
6096 		m_last = sctp_add_pad_tombuf(m_last, padding_len);
6097 		if (m_last == NULL) {
6098 			/* Houston we have a problem, no space */
6099 			sctp_m_freem(m);
6100 			return;
6101 		}
6102 		chunk_len += padding_len;
6103 		padding_len = 0;
6104 	}
6105 	/* Now we must build a cookie */
6106 	m_cookie = sctp_add_cookie(init_pkt, offset, m, 0, &stc, &signature);
6107 	if (m_cookie == NULL) {
6108 		/* memory problem */
6109 		sctp_m_freem(m);
6110 		return;
6111 	}
6112 	/* Now append the cookie to the end and update the space/size */
6113 	SCTP_BUF_NEXT(m_last) = m_cookie;
6114 	parameter_len = 0;
6115 	for (m_tmp = m_cookie; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
6116 		parameter_len += SCTP_BUF_LEN(m_tmp);
6117 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
6118 			m_last = m_tmp;
6119 		}
6120 	}
6121 	padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6122 	chunk_len += parameter_len;
6123 
6124 	/*
6125 	 * Place in the size, but we don't include the last pad (if any) in
6126 	 * the INIT-ACK.
6127 	 */
6128 	initack->ch.chunk_length = htons(chunk_len);
6129 
6130 	/*
6131 	 * Time to sign the cookie, we don't sign over the cookie signature
6132 	 * though thus we set trailer.
6133 	 */
6134 	(void)sctp_hmac_m(SCTP_HMAC,
6135 	    (uint8_t *)inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
6136 	    SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
6137 	    (uint8_t *)signature, SCTP_SIGNATURE_SIZE);
6138 	/*
6139 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
6140 	 * here since the timer will drive a retranmission.
6141 	 */
6142 	if (padding_len > 0) {
6143 		if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
6144 			sctp_m_freem(m);
6145 			return;
6146 		}
6147 	}
6148 	if (stc.loopback_scope) {
6149 		over_addr = (union sctp_sockstore *)dst;
6150 	} else {
6151 		over_addr = NULL;
6152 	}
6153 
6154 	if ((error = sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
6155 	    0, 0,
6156 	    inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
6157 	    port, over_addr,
6158 	    mflowtype, mflowid,
6159 	    false,		/* XXXMT: Improve this! */
6160 	    SCTP_SO_NOT_LOCKED))) {
6161 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error);
6162 		if (error == ENOBUFS) {
6163 			if (asoc != NULL) {
6164 				asoc->ifp_had_enobuf = 1;
6165 			}
6166 			SCTP_STAT_INCR(sctps_lowlevelerr);
6167 		}
6168 	} else {
6169 		if (asoc != NULL) {
6170 			asoc->ifp_had_enobuf = 0;
6171 		}
6172 	}
6173 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
6174 }
6175 
6176 static void
6177 sctp_prune_prsctp(struct sctp_tcb *stcb,
6178     struct sctp_association *asoc,
6179     struct sctp_nonpad_sndrcvinfo *srcv,
6180     int dataout)
6181 {
6182 	int freed_spc = 0;
6183 	struct sctp_tmit_chunk *chk, *nchk;
6184 
6185 	SCTP_TCB_LOCK_ASSERT(stcb);
6186 	if ((asoc->prsctp_supported) &&
6187 	    (asoc->sent_queue_cnt_removeable > 0)) {
6188 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6189 			/*
6190 			 * Look for chunks marked with the PR_SCTP flag AND
6191 			 * the buffer space flag. If the one being sent is
6192 			 * equal or greater priority then purge the old one
6193 			 * and free some space.
6194 			 */
6195 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6196 				/*
6197 				 * This one is PR-SCTP AND buffer space
6198 				 * limited type
6199 				 */
6200 				if (chk->rec.data.timetodrop.tv_sec > (long)srcv->sinfo_timetolive) {
6201 					/*
6202 					 * Lower numbers equates to higher
6203 					 * priority. So if the one we are
6204 					 * looking at has a larger priority,
6205 					 * we want to drop the data and NOT
6206 					 * retransmit it.
6207 					 */
6208 					if (chk->data) {
6209 						/*
6210 						 * We release the book_size
6211 						 * if the mbuf is here
6212 						 */
6213 						int ret_spc;
6214 						uint8_t sent;
6215 
6216 						if (chk->sent > SCTP_DATAGRAM_UNSENT)
6217 							sent = 1;
6218 						else
6219 							sent = 0;
6220 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6221 						    sent,
6222 						    SCTP_SO_LOCKED);
6223 						freed_spc += ret_spc;
6224 						if (freed_spc >= dataout) {
6225 							return;
6226 						}
6227 					}	/* if chunk was present */
6228 				}	/* if of sufficient priority */
6229 			}	/* if chunk has enabled */
6230 		}		/* tailqforeach */
6231 
6232 		TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
6233 			/* Here we must move to the sent queue and mark */
6234 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6235 				if (chk->rec.data.timetodrop.tv_sec > (long)srcv->sinfo_timetolive) {
6236 					if (chk->data) {
6237 						/*
6238 						 * We release the book_size
6239 						 * if the mbuf is here
6240 						 */
6241 						int ret_spc;
6242 
6243 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6244 						    0, SCTP_SO_LOCKED);
6245 
6246 						freed_spc += ret_spc;
6247 						if (freed_spc >= dataout) {
6248 							return;
6249 						}
6250 					}	/* end if chk->data */
6251 				}	/* end if right class */
6252 			}	/* end if chk pr-sctp */
6253 		}		/* tailqforeachsafe (chk) */
6254 	}			/* if enabled in asoc */
6255 }
6256 
6257 uint32_t
6258 sctp_get_frag_point(struct sctp_tcb *stcb)
6259 {
6260 	struct sctp_association *asoc;
6261 	uint32_t frag_point, overhead;
6262 
6263 	asoc = &stcb->asoc;
6264 	/* Consider IP header and SCTP common header. */
6265 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6266 		overhead = SCTP_MIN_OVERHEAD;
6267 	} else {
6268 		overhead = SCTP_MIN_V4_OVERHEAD;
6269 	}
6270 	/* Consider DATA/IDATA chunk header and AUTH header, if needed. */
6271 	if (asoc->idata_supported) {
6272 		overhead += sizeof(struct sctp_idata_chunk);
6273 		if (sctp_auth_is_required_chunk(SCTP_IDATA, asoc->peer_auth_chunks)) {
6274 			overhead += sctp_get_auth_chunk_len(asoc->peer_hmac_id);
6275 		}
6276 	} else {
6277 		overhead += sizeof(struct sctp_data_chunk);
6278 		if (sctp_auth_is_required_chunk(SCTP_DATA, asoc->peer_auth_chunks)) {
6279 			overhead += sctp_get_auth_chunk_len(asoc->peer_hmac_id);
6280 		}
6281 	}
6282 	KASSERT(overhead % 4 == 0,
6283 	    ("overhead (%u) not a multiple of 4", overhead));
6284 	/* Consider padding. */
6285 	if (asoc->smallest_mtu % 4 > 0) {
6286 		overhead += (asoc->smallest_mtu % 4);
6287 	}
6288 	KASSERT(asoc->smallest_mtu > overhead,
6289 	    ("Association MTU (%u) too small for overhead (%u)",
6290 	    asoc->smallest_mtu, overhead));
6291 	frag_point = asoc->smallest_mtu - overhead;
6292 	KASSERT(frag_point % 4 == 0,
6293 	    ("frag_point (%u) not a multiple of 4", frag_point));
6294 	/* Honor MAXSEG socket option. */
6295 	if ((asoc->sctp_frag_point > 0) &&
6296 	    (asoc->sctp_frag_point < frag_point)) {
6297 		frag_point = asoc->sctp_frag_point;
6298 	}
6299 	return (frag_point);
6300 }
6301 
6302 static void
6303 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
6304 {
6305 	/*
6306 	 * We assume that the user wants PR_SCTP_TTL if the user provides a
6307 	 * positive lifetime but does not specify any PR_SCTP policy.
6308 	 */
6309 	if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
6310 		sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
6311 	} else if (sp->timetolive > 0) {
6312 		sp->sinfo_flags |= SCTP_PR_SCTP_TTL;
6313 		sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
6314 	} else {
6315 		return;
6316 	}
6317 	switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
6318 	case CHUNK_FLAGS_PR_SCTP_BUF:
6319 		/*
6320 		 * Time to live is a priority stored in tv_sec when doing
6321 		 * the buffer drop thing.
6322 		 */
6323 		sp->ts.tv_sec = sp->timetolive;
6324 		sp->ts.tv_usec = 0;
6325 		break;
6326 	case CHUNK_FLAGS_PR_SCTP_TTL:
6327 		{
6328 			struct timeval tv;
6329 
6330 			(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6331 			tv.tv_sec = sp->timetolive / 1000;
6332 			tv.tv_usec = (sp->timetolive * 1000) % 1000000;
6333 			/*
6334 			 * TODO sctp_constants.h needs alternative time
6335 			 * macros when _KERNEL is undefined.
6336 			 */
6337 			timevaladd(&sp->ts, &tv);
6338 		}
6339 		break;
6340 	case CHUNK_FLAGS_PR_SCTP_RTX:
6341 		/*
6342 		 * Time to live is a the number or retransmissions stored in
6343 		 * tv_sec.
6344 		 */
6345 		sp->ts.tv_sec = sp->timetolive;
6346 		sp->ts.tv_usec = 0;
6347 		break;
6348 	default:
6349 		SCTPDBG(SCTP_DEBUG_USRREQ1,
6350 		    "Unknown PR_SCTP policy %u.\n",
6351 		    PR_SCTP_POLICY(sp->sinfo_flags));
6352 		break;
6353 	}
6354 }
6355 
6356 static int
6357 sctp_msg_append(struct sctp_tcb *stcb,
6358     struct sctp_nets *net,
6359     struct mbuf *m,
6360     struct sctp_nonpad_sndrcvinfo *srcv)
6361 {
6362 	int error = 0;
6363 	struct mbuf *at;
6364 	struct sctp_stream_queue_pending *sp = NULL;
6365 	struct sctp_stream_out *strm;
6366 
6367 	SCTP_TCB_LOCK_ASSERT(stcb);
6368 
6369 	/*
6370 	 * Given an mbuf chain, put it into the association send queue and
6371 	 * place it on the wheel
6372 	 */
6373 	if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
6374 		/* Invalid stream number */
6375 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6376 		error = EINVAL;
6377 		goto out_now;
6378 	}
6379 	if ((stcb->asoc.stream_locked) &&
6380 	    (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
6381 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6382 		error = EINVAL;
6383 		goto out_now;
6384 	}
6385 	if ((stcb->asoc.strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPEN) &&
6386 	    (stcb->asoc.strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPENING)) {
6387 		/*
6388 		 * Can't queue any data while stream reset is underway.
6389 		 */
6390 		if (stcb->asoc.strmout[srcv->sinfo_stream].state > SCTP_STREAM_OPEN) {
6391 			error = EAGAIN;
6392 		} else {
6393 			error = EINVAL;
6394 		}
6395 		goto out_now;
6396 	}
6397 	/* Now can we send this? */
6398 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) ||
6399 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
6400 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
6401 	    (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
6402 		/* got data while shutting down */
6403 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EPIPE);
6404 		error = EPIPE;
6405 		goto out_now;
6406 	}
6407 	sctp_alloc_a_strmoq(stcb, sp);
6408 	if (sp == NULL) {
6409 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6410 		error = ENOMEM;
6411 		goto out_now;
6412 	}
6413 	sp->sinfo_flags = srcv->sinfo_flags;
6414 	sp->timetolive = srcv->sinfo_timetolive;
6415 	sp->ppid = srcv->sinfo_ppid;
6416 	sp->context = srcv->sinfo_context;
6417 	sp->fsn = 0;
6418 	if (sp->sinfo_flags & SCTP_ADDR_OVER) {
6419 		sp->net = net;
6420 		atomic_add_int(&sp->net->ref_count, 1);
6421 	} else {
6422 		sp->net = NULL;
6423 	}
6424 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6425 	sp->sid = srcv->sinfo_stream;
6426 	sp->msg_is_complete = 1;
6427 	sp->sender_all_done = 1;
6428 	sp->some_taken = 0;
6429 	sp->data = m;
6430 	sp->tail_mbuf = NULL;
6431 	sctp_set_prsctp_policy(sp);
6432 	/*
6433 	 * We could in theory (for sendall) sifa the length in, but we would
6434 	 * still have to hunt through the chain since we need to setup the
6435 	 * tail_mbuf
6436 	 */
6437 	sp->length = 0;
6438 	for (at = m; at; at = SCTP_BUF_NEXT(at)) {
6439 		if (SCTP_BUF_NEXT(at) == NULL)
6440 			sp->tail_mbuf = at;
6441 		sp->length += SCTP_BUF_LEN(at);
6442 	}
6443 	if (srcv->sinfo_keynumber_valid) {
6444 		sp->auth_keyid = srcv->sinfo_keynumber;
6445 	} else {
6446 		sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
6447 	}
6448 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
6449 		sctp_auth_key_acquire(stcb, sp->auth_keyid);
6450 		sp->holds_key_ref = 1;
6451 	}
6452 	strm = &stcb->asoc.strmout[srcv->sinfo_stream];
6453 	sctp_snd_sb_alloc(stcb, sp->length);
6454 	atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
6455 	TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
6456 	stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp);
6457 	m = NULL;
6458 out_now:
6459 	if (m) {
6460 		sctp_m_freem(m);
6461 	}
6462 	return (error);
6463 }
6464 
6465 static struct mbuf *
6466 sctp_copy_mbufchain(struct mbuf *clonechain,
6467     struct mbuf *outchain,
6468     struct mbuf **endofchain,
6469     int can_take_mbuf,
6470     int sizeofcpy,
6471     uint8_t copy_by_ref)
6472 {
6473 	struct mbuf *m;
6474 	struct mbuf *appendchain;
6475 	caddr_t cp;
6476 	int len;
6477 
6478 	if (endofchain == NULL) {
6479 		/* error */
6480 error_out:
6481 		if (outchain)
6482 			sctp_m_freem(outchain);
6483 		return (NULL);
6484 	}
6485 	if (can_take_mbuf) {
6486 		appendchain = clonechain;
6487 	} else {
6488 		if (!copy_by_ref &&
6489 		    (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))) {
6490 			/* Its not in a cluster */
6491 			if (*endofchain == NULL) {
6492 				/* lets get a mbuf cluster */
6493 				if (outchain == NULL) {
6494 					/* This is the general case */
6495 			new_mbuf:
6496 					outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER);
6497 					if (outchain == NULL) {
6498 						goto error_out;
6499 					}
6500 					SCTP_BUF_LEN(outchain) = 0;
6501 					*endofchain = outchain;
6502 					/* get the prepend space */
6503 					SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
6504 				} else {
6505 					/*
6506 					 * We really should not get a NULL
6507 					 * in endofchain
6508 					 */
6509 					/* find end */
6510 					m = outchain;
6511 					while (m) {
6512 						if (SCTP_BUF_NEXT(m) == NULL) {
6513 							*endofchain = m;
6514 							break;
6515 						}
6516 						m = SCTP_BUF_NEXT(m);
6517 					}
6518 					/* sanity */
6519 					if (*endofchain == NULL) {
6520 						/*
6521 						 * huh, TSNH XXX maybe we
6522 						 * should panic
6523 						 */
6524 						sctp_m_freem(outchain);
6525 						goto new_mbuf;
6526 					}
6527 				}
6528 				/* get the new end of length */
6529 				len = (int)M_TRAILINGSPACE(*endofchain);
6530 			} else {
6531 				/* how much is left at the end? */
6532 				len = (int)M_TRAILINGSPACE(*endofchain);
6533 			}
6534 			/* Find the end of the data, for appending */
6535 			cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
6536 
6537 			/* Now lets copy it out */
6538 			if (len >= sizeofcpy) {
6539 				/* It all fits, copy it in */
6540 				m_copydata(clonechain, 0, sizeofcpy, cp);
6541 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6542 			} else {
6543 				/* fill up the end of the chain */
6544 				if (len > 0) {
6545 					m_copydata(clonechain, 0, len, cp);
6546 					SCTP_BUF_LEN((*endofchain)) += len;
6547 					/* now we need another one */
6548 					sizeofcpy -= len;
6549 				}
6550 				m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER);
6551 				if (m == NULL) {
6552 					/* We failed */
6553 					goto error_out;
6554 				}
6555 				SCTP_BUF_NEXT((*endofchain)) = m;
6556 				*endofchain = m;
6557 				cp = mtod((*endofchain), caddr_t);
6558 				m_copydata(clonechain, len, sizeofcpy, cp);
6559 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6560 			}
6561 			return (outchain);
6562 		} else {
6563 			/* copy the old fashion way */
6564 			appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_NOWAIT);
6565 #ifdef SCTP_MBUF_LOGGING
6566 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6567 				sctp_log_mbc(appendchain, SCTP_MBUF_ICOPY);
6568 			}
6569 #endif
6570 		}
6571 	}
6572 	if (appendchain == NULL) {
6573 		/* error */
6574 		if (outchain)
6575 			sctp_m_freem(outchain);
6576 		return (NULL);
6577 	}
6578 	if (outchain) {
6579 		/* tack on to the end */
6580 		if (*endofchain != NULL) {
6581 			SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6582 		} else {
6583 			m = outchain;
6584 			while (m) {
6585 				if (SCTP_BUF_NEXT(m) == NULL) {
6586 					SCTP_BUF_NEXT(m) = appendchain;
6587 					break;
6588 				}
6589 				m = SCTP_BUF_NEXT(m);
6590 			}
6591 		}
6592 		/*
6593 		 * save off the end and update the end-chain position
6594 		 */
6595 		m = appendchain;
6596 		while (m) {
6597 			if (SCTP_BUF_NEXT(m) == NULL) {
6598 				*endofchain = m;
6599 				break;
6600 			}
6601 			m = SCTP_BUF_NEXT(m);
6602 		}
6603 		return (outchain);
6604 	} else {
6605 		/* save off the end and update the end-chain position */
6606 		m = appendchain;
6607 		while (m) {
6608 			if (SCTP_BUF_NEXT(m) == NULL) {
6609 				*endofchain = m;
6610 				break;
6611 			}
6612 			m = SCTP_BUF_NEXT(m);
6613 		}
6614 		return (appendchain);
6615 	}
6616 }
6617 
6618 static int
6619 sctp_med_chunk_output(struct sctp_inpcb *inp,
6620     struct sctp_tcb *stcb,
6621     struct sctp_association *asoc,
6622     int *num_out,
6623     int *reason_code,
6624     int control_only, int from_where,
6625     struct timeval *now, int *now_filled,
6626     uint32_t frag_point, int so_locked);
6627 
6628 static void
6629 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6630     uint32_t val SCTP_UNUSED)
6631 {
6632 	struct sctp_copy_all *ca;
6633 	struct mbuf *m;
6634 	int ret = 0;
6635 	int added_control = 0;
6636 	int un_sent, do_chunk_output = 1;
6637 	struct sctp_association *asoc;
6638 	struct sctp_nets *net;
6639 
6640 	ca = (struct sctp_copy_all *)ptr;
6641 	if (ca->m == NULL) {
6642 		return;
6643 	}
6644 	if (ca->inp != inp) {
6645 		/* TSNH */
6646 		return;
6647 	}
6648 	if (ca->sndlen > 0) {
6649 		m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_NOWAIT);
6650 		if (m == NULL) {
6651 			/* can't copy so we are done */
6652 			ca->cnt_failed++;
6653 			return;
6654 		}
6655 #ifdef SCTP_MBUF_LOGGING
6656 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6657 			sctp_log_mbc(m, SCTP_MBUF_ICOPY);
6658 		}
6659 #endif
6660 	} else {
6661 		m = NULL;
6662 	}
6663 	SCTP_TCB_LOCK_ASSERT(stcb);
6664 	if (stcb->asoc.alternate) {
6665 		net = stcb->asoc.alternate;
6666 	} else {
6667 		net = stcb->asoc.primary_destination;
6668 	}
6669 	if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6670 		/* Abort this assoc with m as the user defined reason */
6671 		if (m != NULL) {
6672 			SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_NOWAIT);
6673 		} else {
6674 			m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
6675 			    0, M_NOWAIT, 1, MT_DATA);
6676 			SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr);
6677 		}
6678 		if (m != NULL) {
6679 			struct sctp_paramhdr *ph;
6680 
6681 			ph = mtod(m, struct sctp_paramhdr *);
6682 			ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6683 			ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + ca->sndlen));
6684 		}
6685 		/*
6686 		 * We add one here to keep the assoc from dis-appearing on
6687 		 * us.
6688 		 */
6689 		atomic_add_int(&stcb->asoc.refcnt, 1);
6690 		sctp_abort_an_association(inp, stcb, m, false, SCTP_SO_NOT_LOCKED);
6691 		/*
6692 		 * sctp_abort_an_association calls sctp_free_asoc() free
6693 		 * association will NOT free it since we incremented the
6694 		 * refcnt .. we do this to prevent it being freed and things
6695 		 * getting tricky since we could end up (from free_asoc)
6696 		 * calling inpcb_free which would get a recursive lock call
6697 		 * to the iterator lock.. But as a consequence of that the
6698 		 * stcb will return to us un-locked.. since free_asoc
6699 		 * returns with either no TCB or the TCB unlocked, we must
6700 		 * relock.. to unlock in the iterator timer :-0
6701 		 */
6702 		SCTP_TCB_LOCK(stcb);
6703 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
6704 		goto no_chunk_output;
6705 	} else {
6706 		if (m != NULL) {
6707 			ret = sctp_msg_append(stcb, net, m, &ca->sndrcv);
6708 		}
6709 		asoc = &stcb->asoc;
6710 		if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6711 			/* shutdown this assoc */
6712 			if (TAILQ_EMPTY(&asoc->send_queue) &&
6713 			    TAILQ_EMPTY(&asoc->sent_queue) &&
6714 			    sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED) == 0) {
6715 				if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
6716 					goto abort_anyway;
6717 				}
6718 				/*
6719 				 * there is nothing queued to send, so I'm
6720 				 * done...
6721 				 */
6722 				if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
6723 				    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6724 				    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6725 					/*
6726 					 * only send SHUTDOWN the first time
6727 					 * through
6728 					 */
6729 					if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
6730 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6731 					}
6732 					SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
6733 					sctp_stop_timers_for_shutdown(stcb);
6734 					sctp_send_shutdown(stcb, net);
6735 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6736 					    net);
6737 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6738 					    NULL);
6739 					added_control = 1;
6740 					do_chunk_output = 0;
6741 				}
6742 			} else {
6743 				/*
6744 				 * we still got (or just got) data to send,
6745 				 * so set SHUTDOWN_PENDING
6746 				 */
6747 				/*
6748 				 * XXX sockets draft says that SCTP_EOF
6749 				 * should be sent with no data.  currently,
6750 				 * we will allow user data to be sent first
6751 				 * and move to SHUTDOWN-PENDING
6752 				 */
6753 				if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
6754 				    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6755 				    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6756 					if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
6757 						SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
6758 					}
6759 					SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
6760 					if (TAILQ_EMPTY(&asoc->send_queue) &&
6761 					    TAILQ_EMPTY(&asoc->sent_queue) &&
6762 					    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6763 						struct mbuf *op_err;
6764 						char msg[SCTP_DIAG_INFO_LEN];
6765 
6766 				abort_anyway:
6767 						SCTP_SNPRINTF(msg, sizeof(msg),
6768 						    "%s:%d at %s", __FILE__, __LINE__, __func__);
6769 						op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
6770 						    msg);
6771 						atomic_add_int(&stcb->asoc.refcnt, 1);
6772 						sctp_abort_an_association(stcb->sctp_ep, stcb,
6773 						    op_err, false, SCTP_SO_NOT_LOCKED);
6774 						atomic_subtract_int(&stcb->asoc.refcnt, 1);
6775 						goto no_chunk_output;
6776 					}
6777 				}
6778 			}
6779 		}
6780 	}
6781 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6782 	    (stcb->asoc.stream_queue_cnt * SCTP_DATA_CHUNK_OVERHEAD(stcb)));
6783 
6784 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6785 	    (stcb->asoc.total_flight > 0) &&
6786 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
6787 		do_chunk_output = 0;
6788 	}
6789 	if (do_chunk_output)
6790 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6791 	else if (added_control) {
6792 		struct timeval now;
6793 		int num_out, reason, now_filled = 0;
6794 
6795 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6796 		    &reason, 1, 1, &now, &now_filled,
6797 		    sctp_get_frag_point(stcb),
6798 		    SCTP_SO_NOT_LOCKED);
6799 	}
6800 no_chunk_output:
6801 	if (ret) {
6802 		ca->cnt_failed++;
6803 	} else {
6804 		ca->cnt_sent++;
6805 	}
6806 }
6807 
6808 static void
6809 sctp_sendall_completes(void *ptr, uint32_t val SCTP_UNUSED)
6810 {
6811 	struct sctp_copy_all *ca;
6812 
6813 	ca = (struct sctp_copy_all *)ptr;
6814 	/*
6815 	 * Do a notify here? Kacheong suggests that the notify be done at
6816 	 * the send time.. so you would push up a notification if any send
6817 	 * failed. Don't know if this is feasible since the only failures we
6818 	 * have is "memory" related and if you cannot get an mbuf to send
6819 	 * the data you surely can't get an mbuf to send up to notify the
6820 	 * user you can't send the data :->
6821 	 */
6822 
6823 	/* now free everything */
6824 	if (ca->inp) {
6825 		/* Lets clear the flag to allow others to run. */
6826 		SCTP_INP_WLOCK(ca->inp);
6827 		ca->inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6828 		SCTP_INP_WUNLOCK(ca->inp);
6829 	}
6830 	sctp_m_freem(ca->m);
6831 	SCTP_FREE(ca, SCTP_M_COPYAL);
6832 }
6833 
6834 static struct mbuf *
6835 sctp_copy_out_all(struct uio *uio, ssize_t len)
6836 {
6837 	struct mbuf *ret, *at;
6838 	ssize_t left, willcpy, cancpy, error;
6839 
6840 	ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAITOK, 1, MT_DATA);
6841 	if (ret == NULL) {
6842 		/* TSNH */
6843 		return (NULL);
6844 	}
6845 	left = len;
6846 	SCTP_BUF_LEN(ret) = 0;
6847 	/* save space for the data chunk header */
6848 	cancpy = (int)M_TRAILINGSPACE(ret);
6849 	willcpy = min(cancpy, left);
6850 	at = ret;
6851 	while (left > 0) {
6852 		/* Align data to the end */
6853 		error = uiomove(mtod(at, caddr_t), (int)willcpy, uio);
6854 		if (error) {
6855 	err_out_now:
6856 			sctp_m_freem(at);
6857 			return (NULL);
6858 		}
6859 		SCTP_BUF_LEN(at) = (int)willcpy;
6860 		SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6861 		left -= willcpy;
6862 		if (left > 0) {
6863 			SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg((unsigned int)left, 0, M_WAITOK, 1, MT_DATA);
6864 			if (SCTP_BUF_NEXT(at) == NULL) {
6865 				goto err_out_now;
6866 			}
6867 			at = SCTP_BUF_NEXT(at);
6868 			SCTP_BUF_LEN(at) = 0;
6869 			cancpy = (int)M_TRAILINGSPACE(at);
6870 			willcpy = min(cancpy, left);
6871 		}
6872 	}
6873 	return (ret);
6874 }
6875 
6876 static int
6877 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6878     struct sctp_nonpad_sndrcvinfo *srcv)
6879 {
6880 	int ret;
6881 	struct sctp_copy_all *ca;
6882 
6883 	if (uio->uio_resid > (ssize_t)SCTP_BASE_SYSCTL(sctp_sendall_limit)) {
6884 		/* You must not be larger than the limit! */
6885 		return (EMSGSIZE);
6886 	}
6887 	SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6888 	    SCTP_M_COPYAL);
6889 	if (ca == NULL) {
6890 		sctp_m_freem(m);
6891 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6892 		return (ENOMEM);
6893 	}
6894 	memset(ca, 0, sizeof(struct sctp_copy_all));
6895 
6896 	ca->inp = inp;
6897 	if (srcv != NULL) {
6898 		memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6899 	}
6900 
6901 	/* Serialize. */
6902 	SCTP_INP_WLOCK(inp);
6903 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SND_ITERATOR_UP) != 0) {
6904 		SCTP_INP_WUNLOCK(inp);
6905 		sctp_m_freem(m);
6906 		SCTP_FREE(ca, SCTP_M_COPYAL);
6907 		return (EBUSY);
6908 	}
6909 	inp->sctp_flags |= SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6910 	SCTP_INP_WUNLOCK(inp);
6911 
6912 	/*
6913 	 * take off the sendall flag, it would be bad if we failed to do
6914 	 * this :-0
6915 	 */
6916 	ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6917 	/* get length and mbuf chain */
6918 	if (uio) {
6919 		ca->sndlen = uio->uio_resid;
6920 		ca->m = sctp_copy_out_all(uio, ca->sndlen);
6921 		if (ca->m == NULL) {
6922 			SCTP_FREE(ca, SCTP_M_COPYAL);
6923 			sctp_m_freem(m);
6924 			SCTP_INP_WLOCK(inp);
6925 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6926 			SCTP_INP_WUNLOCK(inp);
6927 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6928 			return (ENOMEM);
6929 		}
6930 	} else {
6931 		/* Gather the length of the send */
6932 		struct mbuf *mat;
6933 
6934 		ca->sndlen = 0;
6935 		for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
6936 			ca->sndlen += SCTP_BUF_LEN(mat);
6937 		}
6938 	}
6939 	ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6940 	    SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6941 	    SCTP_ASOC_ANY_STATE,
6942 	    (void *)ca, 0,
6943 	    sctp_sendall_completes, inp, 1);
6944 	if (ret) {
6945 		SCTP_INP_WLOCK(inp);
6946 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6947 		SCTP_INP_WUNLOCK(inp);
6948 		SCTP_FREE(ca, SCTP_M_COPYAL);
6949 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6950 		return (EFAULT);
6951 	}
6952 	return (0);
6953 }
6954 
6955 void
6956 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6957 {
6958 	struct sctp_tmit_chunk *chk, *nchk;
6959 
6960 	TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
6961 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6962 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6963 			asoc->ctrl_queue_cnt--;
6964 			if (chk->data) {
6965 				sctp_m_freem(chk->data);
6966 				chk->data = NULL;
6967 			}
6968 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6969 		}
6970 	}
6971 }
6972 
6973 void
6974 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6975 {
6976 	struct sctp_association *asoc;
6977 	struct sctp_tmit_chunk *chk, *nchk;
6978 	struct sctp_asconf_chunk *acp;
6979 
6980 	asoc = &stcb->asoc;
6981 	TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
6982 		/* find SCTP_ASCONF chunk in queue */
6983 		if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6984 			if (chk->data) {
6985 				acp = mtod(chk->data, struct sctp_asconf_chunk *);
6986 				if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) {
6987 					/* Not Acked yet */
6988 					break;
6989 				}
6990 			}
6991 			TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6992 			asoc->ctrl_queue_cnt--;
6993 			if (chk->data) {
6994 				sctp_m_freem(chk->data);
6995 				chk->data = NULL;
6996 			}
6997 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6998 		}
6999 	}
7000 }
7001 
7002 static void
7003 sctp_clean_up_datalist(struct sctp_tcb *stcb,
7004     struct sctp_association *asoc,
7005     struct sctp_tmit_chunk **data_list,
7006     int bundle_at,
7007     struct sctp_nets *net)
7008 {
7009 	int i;
7010 	struct sctp_tmit_chunk *tp1;
7011 
7012 	for (i = 0; i < bundle_at; i++) {
7013 		/* off of the send queue */
7014 		TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next);
7015 		asoc->send_queue_cnt--;
7016 		if (i > 0) {
7017 			/*
7018 			 * Any chunk NOT 0 you zap the time chunk 0 gets
7019 			 * zapped or set based on if a RTO measurement is
7020 			 * needed.
7021 			 */
7022 			data_list[i]->do_rtt = 0;
7023 		}
7024 		/* record time */
7025 		data_list[i]->sent_rcv_time = net->last_sent_time;
7026 		data_list[i]->rec.data.cwnd_at_send = net->cwnd;
7027 		data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.tsn;
7028 		if (data_list[i]->whoTo == NULL) {
7029 			data_list[i]->whoTo = net;
7030 			atomic_add_int(&net->ref_count, 1);
7031 		}
7032 		/* on to the sent queue */
7033 		tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
7034 		if ((tp1) && SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) {
7035 			struct sctp_tmit_chunk *tpp;
7036 
7037 			/* need to move back */
7038 	back_up_more:
7039 			tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
7040 			if (tpp == NULL) {
7041 				TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
7042 				goto all_done;
7043 			}
7044 			tp1 = tpp;
7045 			if (SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) {
7046 				goto back_up_more;
7047 			}
7048 			TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
7049 		} else {
7050 			TAILQ_INSERT_TAIL(&asoc->sent_queue,
7051 			    data_list[i],
7052 			    sctp_next);
7053 		}
7054 all_done:
7055 		/* This does not lower until the cum-ack passes it */
7056 		asoc->sent_queue_cnt++;
7057 		if ((asoc->peers_rwnd <= 0) &&
7058 		    (asoc->total_flight == 0) &&
7059 		    (bundle_at == 1)) {
7060 			/* Mark the chunk as being a window probe */
7061 			SCTP_STAT_INCR(sctps_windowprobed);
7062 		}
7063 #ifdef SCTP_AUDITING_ENABLED
7064 		sctp_audit_log(0xC2, 3);
7065 #endif
7066 		data_list[i]->sent = SCTP_DATAGRAM_SENT;
7067 		data_list[i]->snd_count = 1;
7068 		data_list[i]->rec.data.chunk_was_revoked = 0;
7069 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7070 			sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
7071 			    data_list[i]->whoTo->flight_size,
7072 			    data_list[i]->book_size,
7073 			    (uint32_t)(uintptr_t)data_list[i]->whoTo,
7074 			    data_list[i]->rec.data.tsn);
7075 		}
7076 		sctp_flight_size_increase(data_list[i]);
7077 		sctp_total_flight_increase(stcb, data_list[i]);
7078 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7079 			sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
7080 			    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
7081 		}
7082 		asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
7083 		    (uint32_t)(data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
7084 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7085 			/* SWS sender side engages */
7086 			asoc->peers_rwnd = 0;
7087 		}
7088 	}
7089 	if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) {
7090 		(*asoc->cc_functions.sctp_cwnd_update_packet_transmitted) (stcb, net);
7091 	}
7092 }
7093 
7094 static void
7095 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked)
7096 {
7097 	struct sctp_tmit_chunk *chk, *nchk;
7098 
7099 	TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
7100 		if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7101 		    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
7102 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7103 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7104 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
7105 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7106 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7107 		    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7108 		    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7109 		    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7110 		    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7111 		    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7112 			/* Stray chunks must be cleaned up */
7113 	clean_up_anyway:
7114 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7115 			asoc->ctrl_queue_cnt--;
7116 			if (chk->data) {
7117 				sctp_m_freem(chk->data);
7118 				chk->data = NULL;
7119 			}
7120 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
7121 				asoc->fwd_tsn_cnt--;
7122 			}
7123 			sctp_free_a_chunk(stcb, chk, so_locked);
7124 		} else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
7125 			/* special handling, we must look into the param */
7126 			if (chk != asoc->str_reset) {
7127 				goto clean_up_anyway;
7128 			}
7129 		}
7130 	}
7131 }
7132 
7133 static uint32_t
7134 sctp_can_we_split_this(struct sctp_tcb *stcb, uint32_t length,
7135     uint32_t space_left, uint32_t frag_point, int eeor_on)
7136 {
7137 	/*
7138 	 * Make a decision on if I should split a msg into multiple parts.
7139 	 * This is only asked of incomplete messages.
7140 	 */
7141 	if (eeor_on) {
7142 		/*
7143 		 * If we are doing EEOR we need to always send it if its the
7144 		 * entire thing, since it might be all the guy is putting in
7145 		 * the hopper.
7146 		 */
7147 		if (space_left >= length) {
7148 			/*-
7149 			 * If we have data outstanding,
7150 			 * we get another chance when the sack
7151 			 * arrives to transmit - wait for more data
7152 			 */
7153 			if (stcb->asoc.total_flight == 0) {
7154 				/*
7155 				 * If nothing is in flight, we zero the
7156 				 * packet counter.
7157 				 */
7158 				return (length);
7159 			}
7160 			return (0);
7161 
7162 		} else {
7163 			/* You can fill the rest */
7164 			return (space_left);
7165 		}
7166 	}
7167 	/*-
7168 	 * For those strange folk that make the send buffer
7169 	 * smaller than our fragmentation point, we can't
7170 	 * get a full msg in so we have to allow splitting.
7171 	 */
7172 	if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
7173 		return (length);
7174 	}
7175 	if ((length <= space_left) ||
7176 	    ((length - space_left) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
7177 		/* Sub-optimal residual don't split in non-eeor mode. */
7178 		return (0);
7179 	}
7180 	/*
7181 	 * If we reach here length is larger than the space_left. Do we wish
7182 	 * to split it for the sake of packet putting together?
7183 	 */
7184 	if (space_left >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
7185 		/* Its ok to split it */
7186 		return (min(space_left, frag_point));
7187 	}
7188 	/* Nope, can't split */
7189 	return (0);
7190 }
7191 
7192 static uint32_t
7193 sctp_move_to_outqueue(struct sctp_tcb *stcb,
7194     struct sctp_nets *net,
7195     struct sctp_stream_out *strq,
7196     uint32_t space_left,
7197     uint32_t frag_point,
7198     int *giveup,
7199     int eeor_mode,
7200     int *bail,
7201     int so_locked)
7202 {
7203 	/* Move from the stream to the send_queue keeping track of the total */
7204 	struct sctp_association *asoc;
7205 	struct sctp_stream_queue_pending *sp;
7206 	struct sctp_tmit_chunk *chk;
7207 	struct sctp_data_chunk *dchkh = NULL;
7208 	struct sctp_idata_chunk *ndchkh = NULL;
7209 	uint32_t to_move, length;
7210 	int leading;
7211 	uint8_t rcv_flags = 0;
7212 	uint8_t some_taken;
7213 
7214 	SCTP_TCB_LOCK_ASSERT(stcb);
7215 	asoc = &stcb->asoc;
7216 one_more_time:
7217 	/* sa_ignore FREED_MEMORY */
7218 	sp = TAILQ_FIRST(&strq->outqueue);
7219 	if (sp == NULL) {
7220 		sp = TAILQ_FIRST(&strq->outqueue);
7221 		if (sp) {
7222 			goto one_more_time;
7223 		}
7224 		if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_EXPLICIT_EOR) == 0) &&
7225 		    (stcb->asoc.idata_supported == 0) &&
7226 		    (strq->last_msg_incomplete)) {
7227 			SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
7228 			    strq->sid,
7229 			    strq->last_msg_incomplete);
7230 			strq->last_msg_incomplete = 0;
7231 		}
7232 		to_move = 0;
7233 		goto out_of;
7234 	}
7235 	if ((sp->msg_is_complete) && (sp->length == 0)) {
7236 		if (sp->sender_all_done) {
7237 			/*
7238 			 * We are doing deferred cleanup. Last time through
7239 			 * when we took all the data the sender_all_done was
7240 			 * not set.
7241 			 */
7242 			if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
7243 				SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
7244 				SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
7245 				    sp->sender_all_done,
7246 				    sp->length,
7247 				    sp->msg_is_complete,
7248 				    sp->put_last_out);
7249 			}
7250 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7251 			TAILQ_REMOVE(&strq->outqueue, sp, next);
7252 			stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp);
7253 			if ((strq->state == SCTP_STREAM_RESET_PENDING) &&
7254 			    (strq->chunks_on_queues == 0) &&
7255 			    TAILQ_EMPTY(&strq->outqueue)) {
7256 				stcb->asoc.trigger_reset = 1;
7257 			}
7258 			if (sp->net) {
7259 				sctp_free_remote_addr(sp->net);
7260 				sp->net = NULL;
7261 			}
7262 			if (sp->data) {
7263 				sctp_m_freem(sp->data);
7264 				sp->data = NULL;
7265 			}
7266 			sctp_free_a_strmoq(stcb, sp, so_locked);
7267 			/* back to get the next msg */
7268 			goto one_more_time;
7269 		} else {
7270 			/*
7271 			 * sender just finished this but still holds a
7272 			 * reference
7273 			 */
7274 			*giveup = 1;
7275 			to_move = 0;
7276 			goto out_of;
7277 		}
7278 	} else {
7279 		/* is there some to get */
7280 		if (sp->length == 0) {
7281 			/* no */
7282 			*giveup = 1;
7283 			to_move = 0;
7284 			goto out_of;
7285 		} else if (sp->discard_rest) {
7286 			/* Whack down the size */
7287 			atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
7288 			if ((stcb->sctp_socket != NULL) &&
7289 			    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7290 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
7291 				SCTP_SB_DECR(&stcb->sctp_socket->so_snd, sp->length);
7292 			}
7293 			if (sp->data) {
7294 				sctp_m_freem(sp->data);
7295 				sp->data = NULL;
7296 				sp->tail_mbuf = NULL;
7297 			}
7298 			sp->length = 0;
7299 			sp->some_taken = 1;
7300 			*giveup = 1;
7301 			to_move = 0;
7302 			goto out_of;
7303 		}
7304 	}
7305 	some_taken = sp->some_taken;
7306 	length = sp->length;
7307 	if (sp->msg_is_complete) {
7308 		/* The message is complete */
7309 		to_move = min(length, frag_point);
7310 		if (to_move == length) {
7311 			/* All of it fits in the MTU */
7312 			if (sp->some_taken) {
7313 				rcv_flags |= SCTP_DATA_LAST_FRAG;
7314 			} else {
7315 				rcv_flags |= SCTP_DATA_NOT_FRAG;
7316 			}
7317 			sp->put_last_out = 1;
7318 			if (sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) {
7319 				rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
7320 			}
7321 		} else {
7322 			/* Not all of it fits, we fragment */
7323 			if (sp->some_taken == 0) {
7324 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
7325 			}
7326 			sp->some_taken = 1;
7327 		}
7328 	} else {
7329 		to_move = sctp_can_we_split_this(stcb, length, space_left, frag_point, eeor_mode);
7330 		if (to_move > 0) {
7331 			if (to_move >= length) {
7332 				to_move = length;
7333 			}
7334 			if (sp->some_taken == 0) {
7335 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
7336 				sp->some_taken = 1;
7337 			}
7338 		} else {
7339 			/* Nothing to take. */
7340 			*giveup = 1;
7341 			to_move = 0;
7342 			goto out_of;
7343 		}
7344 	}
7345 
7346 	/* If we reach here, we can copy out a chunk */
7347 	sctp_alloc_a_chunk(stcb, chk);
7348 	if (chk == NULL) {
7349 		/* No chunk memory */
7350 		*giveup = 1;
7351 		to_move = 0;
7352 		goto out_of;
7353 	}
7354 	/*
7355 	 * Setup for unordered if needed by looking at the user sent info
7356 	 * flags.
7357 	 */
7358 	if (sp->sinfo_flags & SCTP_UNORDERED) {
7359 		rcv_flags |= SCTP_DATA_UNORDERED;
7360 	}
7361 	if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
7362 	    (sp->sinfo_flags & SCTP_EOF) == SCTP_EOF) {
7363 		rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
7364 	}
7365 	/* clear out the chunk before setting up */
7366 	memset(chk, 0, sizeof(*chk));
7367 	chk->rec.data.rcv_flags = rcv_flags;
7368 
7369 	if (to_move >= length) {
7370 		/* we think we can steal the whole thing */
7371 		if (to_move < sp->length) {
7372 			/* bail, it changed */
7373 			goto dont_do_it;
7374 		}
7375 		chk->data = sp->data;
7376 		chk->last_mbuf = sp->tail_mbuf;
7377 		/* register the stealing */
7378 		sp->data = sp->tail_mbuf = NULL;
7379 	} else {
7380 		struct mbuf *m;
7381 
7382 dont_do_it:
7383 		chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_NOWAIT);
7384 		chk->last_mbuf = NULL;
7385 		if (chk->data == NULL) {
7386 			sp->some_taken = some_taken;
7387 			sctp_free_a_chunk(stcb, chk, so_locked);
7388 			*bail = 1;
7389 			to_move = 0;
7390 			goto out_of;
7391 		}
7392 #ifdef SCTP_MBUF_LOGGING
7393 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7394 			sctp_log_mbc(chk->data, SCTP_MBUF_ICOPY);
7395 		}
7396 #endif
7397 		/* Pull off the data */
7398 		m_adj(sp->data, to_move);
7399 		/* Now lets work our way down and compact it */
7400 		m = sp->data;
7401 		while (m && (SCTP_BUF_LEN(m) == 0)) {
7402 			sp->data = SCTP_BUF_NEXT(m);
7403 			SCTP_BUF_NEXT(m) = NULL;
7404 			if (sp->tail_mbuf == m) {
7405 				/*-
7406 				 * Freeing tail? TSNH since
7407 				 * we supposedly were taking less
7408 				 * than the sp->length.
7409 				 */
7410 #ifdef INVARIANTS
7411 				panic("Huh, freeing tail? - TSNH");
7412 #else
7413 				SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
7414 				sp->tail_mbuf = sp->data = NULL;
7415 				sp->length = 0;
7416 #endif
7417 			}
7418 			sctp_m_free(m);
7419 			m = sp->data;
7420 		}
7421 	}
7422 	if (SCTP_BUF_IS_EXTENDED(chk->data)) {
7423 		chk->copy_by_ref = 1;
7424 	} else {
7425 		chk->copy_by_ref = 0;
7426 	}
7427 	/*
7428 	 * get last_mbuf and counts of mb usage This is ugly but hopefully
7429 	 * its only one mbuf.
7430 	 */
7431 	if (chk->last_mbuf == NULL) {
7432 		chk->last_mbuf = chk->data;
7433 		while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
7434 			chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
7435 		}
7436 	}
7437 
7438 	if (to_move > length) {
7439 		/*- This should not happen either
7440 		 * since we always lower to_move to the size
7441 		 * of sp->length if its larger.
7442 		 */
7443 #ifdef INVARIANTS
7444 		panic("Huh, how can to_move be larger?");
7445 #else
7446 		SCTP_PRINTF("Huh, how can to_move be larger?\n");
7447 		sp->length = 0;
7448 #endif
7449 	} else {
7450 		atomic_subtract_int(&sp->length, to_move);
7451 	}
7452 	leading = SCTP_DATA_CHUNK_OVERHEAD(stcb);
7453 	if (M_LEADINGSPACE(chk->data) < leading) {
7454 		/* Not enough room for a chunk header, get some */
7455 		struct mbuf *m;
7456 
7457 		m = sctp_get_mbuf_for_msg(1, 0, M_NOWAIT, 1, MT_DATA);
7458 		if (m == NULL) {
7459 			/*
7460 			 * we're in trouble here. _PREPEND below will free
7461 			 * all the data if there is no leading space, so we
7462 			 * must put the data back and restore.
7463 			 */
7464 			if (sp->data == NULL) {
7465 				/* unsteal the data */
7466 				sp->data = chk->data;
7467 				sp->tail_mbuf = chk->last_mbuf;
7468 			} else {
7469 				struct mbuf *m_tmp;
7470 
7471 				/* reassemble the data */
7472 				m_tmp = sp->data;
7473 				sp->data = chk->data;
7474 				SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7475 			}
7476 			sp->some_taken = some_taken;
7477 			atomic_add_int(&sp->length, to_move);
7478 			chk->data = NULL;
7479 			*bail = 1;
7480 			sctp_free_a_chunk(stcb, chk, so_locked);
7481 			to_move = 0;
7482 			goto out_of;
7483 		} else {
7484 			SCTP_BUF_LEN(m) = 0;
7485 			SCTP_BUF_NEXT(m) = chk->data;
7486 			chk->data = m;
7487 			M_ALIGN(chk->data, 4);
7488 		}
7489 	}
7490 	SCTP_BUF_PREPEND(chk->data, SCTP_DATA_CHUNK_OVERHEAD(stcb), M_NOWAIT);
7491 	if (chk->data == NULL) {
7492 		/* HELP, TSNH since we assured it would not above? */
7493 #ifdef INVARIANTS
7494 		panic("prepend fails HELP?");
7495 #else
7496 		SCTP_PRINTF("prepend fails HELP?\n");
7497 		sctp_free_a_chunk(stcb, chk, so_locked);
7498 #endif
7499 		*bail = 1;
7500 		to_move = 0;
7501 		goto out_of;
7502 	}
7503 	sctp_snd_sb_alloc(stcb, SCTP_DATA_CHUNK_OVERHEAD(stcb));
7504 	chk->book_size = chk->send_size = (uint16_t)(to_move + SCTP_DATA_CHUNK_OVERHEAD(stcb));
7505 	chk->book_size_scale = 0;
7506 	chk->sent = SCTP_DATAGRAM_UNSENT;
7507 
7508 	chk->flags = 0;
7509 	chk->asoc = &stcb->asoc;
7510 	chk->pad_inplace = 0;
7511 	chk->no_fr_allowed = 0;
7512 	if (stcb->asoc.idata_supported == 0) {
7513 		if (rcv_flags & SCTP_DATA_UNORDERED) {
7514 			/* Just use 0. The receiver ignores the values. */
7515 			chk->rec.data.mid = 0;
7516 		} else {
7517 			chk->rec.data.mid = strq->next_mid_ordered;
7518 			if (rcv_flags & SCTP_DATA_LAST_FRAG) {
7519 				strq->next_mid_ordered++;
7520 			}
7521 		}
7522 	} else {
7523 		if (rcv_flags & SCTP_DATA_UNORDERED) {
7524 			chk->rec.data.mid = strq->next_mid_unordered;
7525 			if (rcv_flags & SCTP_DATA_LAST_FRAG) {
7526 				strq->next_mid_unordered++;
7527 			}
7528 		} else {
7529 			chk->rec.data.mid = strq->next_mid_ordered;
7530 			if (rcv_flags & SCTP_DATA_LAST_FRAG) {
7531 				strq->next_mid_ordered++;
7532 			}
7533 		}
7534 	}
7535 	chk->rec.data.sid = sp->sid;
7536 	chk->rec.data.ppid = sp->ppid;
7537 	chk->rec.data.context = sp->context;
7538 	chk->rec.data.doing_fast_retransmit = 0;
7539 
7540 	chk->rec.data.timetodrop = sp->ts;
7541 	chk->flags = sp->act_flags;
7542 
7543 	if (sp->net) {
7544 		chk->whoTo = sp->net;
7545 		atomic_add_int(&chk->whoTo->ref_count, 1);
7546 	} else
7547 		chk->whoTo = NULL;
7548 
7549 	if (sp->holds_key_ref) {
7550 		chk->auth_keyid = sp->auth_keyid;
7551 		sctp_auth_key_acquire(stcb, chk->auth_keyid);
7552 		chk->holds_key_ref = 1;
7553 	}
7554 	stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, to_move);
7555 	chk->rec.data.tsn = atomic_fetchadd_int(&asoc->sending_seq, 1);
7556 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7557 		sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7558 		    (uint32_t)(uintptr_t)stcb, sp->length,
7559 		    (uint32_t)((chk->rec.data.sid << 16) | (0x0000ffff & chk->rec.data.mid)),
7560 		    chk->rec.data.tsn);
7561 	}
7562 	if (stcb->asoc.idata_supported == 0) {
7563 		dchkh = mtod(chk->data, struct sctp_data_chunk *);
7564 	} else {
7565 		ndchkh = mtod(chk->data, struct sctp_idata_chunk *);
7566 	}
7567 	/*
7568 	 * Put the rest of the things in place now. Size was done earlier in
7569 	 * previous loop prior to padding.
7570 	 */
7571 
7572 	SCTP_TCB_LOCK_ASSERT(stcb);
7573 #ifdef SCTP_ASOCLOG_OF_TSNS
7574 	if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7575 		asoc->tsn_out_at = 0;
7576 		asoc->tsn_out_wrapped = 1;
7577 	}
7578 	asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.tsn;
7579 	asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.sid;
7580 	asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.mid;
7581 	asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7582 	asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7583 	asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7584 	asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7585 	asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7586 	asoc->tsn_out_at++;
7587 #endif
7588 	if (stcb->asoc.idata_supported == 0) {
7589 		dchkh->ch.chunk_type = SCTP_DATA;
7590 		dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7591 		dchkh->dp.tsn = htonl(chk->rec.data.tsn);
7592 		dchkh->dp.sid = htons(strq->sid);
7593 		dchkh->dp.ssn = htons((uint16_t)chk->rec.data.mid);
7594 		dchkh->dp.ppid = chk->rec.data.ppid;
7595 		dchkh->ch.chunk_length = htons(chk->send_size);
7596 	} else {
7597 		ndchkh->ch.chunk_type = SCTP_IDATA;
7598 		ndchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7599 		ndchkh->dp.tsn = htonl(chk->rec.data.tsn);
7600 		ndchkh->dp.sid = htons(strq->sid);
7601 		ndchkh->dp.reserved = htons(0);
7602 		ndchkh->dp.mid = htonl(chk->rec.data.mid);
7603 		if (sp->fsn == 0)
7604 			ndchkh->dp.ppid_fsn.ppid = chk->rec.data.ppid;
7605 		else
7606 			ndchkh->dp.ppid_fsn.fsn = htonl(sp->fsn);
7607 		sp->fsn++;
7608 		ndchkh->ch.chunk_length = htons(chk->send_size);
7609 	}
7610 	/* Now advance the chk->send_size by the actual pad needed. */
7611 	if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7612 		/* need a pad */
7613 		struct mbuf *lm;
7614 		int pads;
7615 
7616 		pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7617 		lm = sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf);
7618 		if (lm != NULL) {
7619 			chk->last_mbuf = lm;
7620 			chk->pad_inplace = 1;
7621 		}
7622 		chk->send_size += pads;
7623 	}
7624 	if (PR_SCTP_ENABLED(chk->flags)) {
7625 		asoc->pr_sctp_cnt++;
7626 	}
7627 	if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7628 		/* All done pull and kill the message */
7629 		if (sp->put_last_out == 0) {
7630 			SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7631 			SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
7632 			    sp->sender_all_done,
7633 			    sp->length,
7634 			    sp->msg_is_complete,
7635 			    sp->put_last_out);
7636 		}
7637 		atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7638 		TAILQ_REMOVE(&strq->outqueue, sp, next);
7639 		stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp);
7640 		if ((strq->state == SCTP_STREAM_RESET_PENDING) &&
7641 		    (strq->chunks_on_queues == 0) &&
7642 		    TAILQ_EMPTY(&strq->outqueue)) {
7643 			stcb->asoc.trigger_reset = 1;
7644 		}
7645 		if (sp->net) {
7646 			sctp_free_remote_addr(sp->net);
7647 			sp->net = NULL;
7648 		}
7649 		if (sp->data) {
7650 			sctp_m_freem(sp->data);
7651 			sp->data = NULL;
7652 		}
7653 		sctp_free_a_strmoq(stcb, sp, so_locked);
7654 	}
7655 	asoc->chunks_on_out_queue++;
7656 	strq->chunks_on_queues++;
7657 	TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7658 	asoc->send_queue_cnt++;
7659 out_of:
7660 	return (to_move);
7661 }
7662 
7663 static void
7664 sctp_fill_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
7665     uint32_t frag_point, int eeor_mode, int *quit_now,
7666     int so_locked)
7667 {
7668 	struct sctp_association *asoc;
7669 	struct sctp_stream_out *strq;
7670 	uint32_t space_left, moved, total_moved;
7671 	int bail, giveup;
7672 
7673 	SCTP_TCB_LOCK_ASSERT(stcb);
7674 	asoc = &stcb->asoc;
7675 	total_moved = 0;
7676 	switch (net->ro._l_addr.sa.sa_family) {
7677 #ifdef INET
7678 	case AF_INET:
7679 		space_left = net->mtu - SCTP_MIN_V4_OVERHEAD;
7680 		break;
7681 #endif
7682 #ifdef INET6
7683 	case AF_INET6:
7684 		space_left = net->mtu - SCTP_MIN_OVERHEAD;
7685 		break;
7686 #endif
7687 	default:
7688 		/* TSNH */
7689 		space_left = net->mtu;
7690 		break;
7691 	}
7692 	/* Need an allowance for the data chunk header too */
7693 	space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb);
7694 
7695 	/* must make even word boundary */
7696 	space_left &= 0xfffffffc;
7697 	strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7698 	giveup = 0;
7699 	bail = 0;
7700 	while ((space_left > 0) && (strq != NULL)) {
7701 		moved = sctp_move_to_outqueue(stcb, net, strq, space_left,
7702 		    frag_point, &giveup, eeor_mode,
7703 		    &bail, so_locked);
7704 		if ((giveup != 0) || (bail != 0)) {
7705 			break;
7706 		}
7707 		strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7708 		total_moved += moved;
7709 		if (space_left >= moved) {
7710 			space_left -= moved;
7711 		} else {
7712 			space_left = 0;
7713 		}
7714 		if (space_left >= SCTP_DATA_CHUNK_OVERHEAD(stcb)) {
7715 			space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb);
7716 		} else {
7717 			space_left = 0;
7718 		}
7719 		space_left &= 0xfffffffc;
7720 	}
7721 	if (bail != 0)
7722 		*quit_now = 1;
7723 
7724 	stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc);
7725 
7726 	if (total_moved == 0) {
7727 		if ((stcb->asoc.sctp_cmt_on_off == 0) &&
7728 		    (net == stcb->asoc.primary_destination)) {
7729 			/* ran dry for primary network net */
7730 			SCTP_STAT_INCR(sctps_primary_randry);
7731 		} else if (stcb->asoc.sctp_cmt_on_off > 0) {
7732 			/* ran dry with CMT on */
7733 			SCTP_STAT_INCR(sctps_cmt_randry);
7734 		}
7735 	}
7736 }
7737 
7738 void
7739 sctp_fix_ecn_echo(struct sctp_association *asoc)
7740 {
7741 	struct sctp_tmit_chunk *chk;
7742 
7743 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7744 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7745 			chk->sent = SCTP_DATAGRAM_UNSENT;
7746 		}
7747 	}
7748 }
7749 
7750 void
7751 sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net)
7752 {
7753 	struct sctp_association *asoc;
7754 	struct sctp_tmit_chunk *chk;
7755 	struct sctp_stream_queue_pending *sp;
7756 	unsigned int i;
7757 
7758 	if (net == NULL) {
7759 		return;
7760 	}
7761 	asoc = &stcb->asoc;
7762 	for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
7763 		TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
7764 			if (sp->net == net) {
7765 				sctp_free_remote_addr(sp->net);
7766 				sp->net = NULL;
7767 			}
7768 		}
7769 	}
7770 	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7771 		if (chk->whoTo == net) {
7772 			sctp_free_remote_addr(chk->whoTo);
7773 			chk->whoTo = NULL;
7774 		}
7775 	}
7776 }
7777 
7778 int
7779 sctp_med_chunk_output(struct sctp_inpcb *inp,
7780     struct sctp_tcb *stcb,
7781     struct sctp_association *asoc,
7782     int *num_out,
7783     int *reason_code,
7784     int control_only, int from_where,
7785     struct timeval *now, int *now_filled,
7786     uint32_t frag_point, int so_locked)
7787 {
7788 	/**
7789 	 * Ok this is the generic chunk service queue. we must do the
7790 	 * following:
7791 	 * - Service the stream queue that is next, moving any
7792 	 *   message (note I must get a complete message i.e. FIRST/MIDDLE and
7793 	 *   LAST to the out queue in one pass) and assigning TSN's. This
7794 	 *   only applies though if the peer does not support NDATA. For NDATA
7795 	 *   chunks its ok to not send the entire message ;-)
7796 	 * - Check to see if the cwnd/rwnd allows any output, if so we go ahead and
7797 	 *   formulate and send the low level chunks. Making sure to combine
7798 	 *   any control in the control chunk queue also.
7799 	 */
7800 	struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL;
7801 	struct mbuf *outchain, *endoutchain;
7802 	struct sctp_tmit_chunk *chk, *nchk;
7803 
7804 	/* temp arrays for unlinking */
7805 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7806 	int no_fragmentflg, error;
7807 	unsigned int max_rwnd_per_dest, max_send_per_dest;
7808 	int one_chunk, hbflag, skip_data_for_this_net;
7809 	int asconf, cookie, no_out_cnt;
7810 	int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7811 	unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7812 	int tsns_sent = 0;
7813 	uint32_t auth_offset;
7814 	struct sctp_auth_chunk *auth;
7815 	uint16_t auth_keyid;
7816 	int override_ok = 1;
7817 	int skip_fill_up = 0;
7818 	int data_auth_reqd = 0;
7819 
7820 	/*
7821 	 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7822 	 * destination.
7823 	 */
7824 	int quit_now = 0;
7825 	bool use_zero_crc;
7826 
7827 	*num_out = 0;
7828 	*reason_code = 0;
7829 	auth_keyid = stcb->asoc.authinfo.active_keyid;
7830 	if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7831 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7832 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7833 		eeor_mode = 1;
7834 	} else {
7835 		eeor_mode = 0;
7836 	}
7837 	ctl_cnt = no_out_cnt = asconf = cookie = 0;
7838 	/*
7839 	 * First lets prime the pump. For each destination, if there is room
7840 	 * in the flight size, attempt to pull an MTU's worth out of the
7841 	 * stream queues into the general send_queue
7842 	 */
7843 #ifdef SCTP_AUDITING_ENABLED
7844 	sctp_audit_log(0xC2, 2);
7845 #endif
7846 	SCTP_TCB_LOCK_ASSERT(stcb);
7847 	hbflag = 0;
7848 	if (control_only)
7849 		no_data_chunks = 1;
7850 	else
7851 		no_data_chunks = 0;
7852 
7853 	/* Nothing to possible to send? */
7854 	if ((TAILQ_EMPTY(&asoc->control_send_queue) ||
7855 	    (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) &&
7856 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7857 	    TAILQ_EMPTY(&asoc->send_queue) &&
7858 	    sctp_is_there_unsent_data(stcb, so_locked) == 0) {
7859 nothing_to_send:
7860 		*reason_code = 9;
7861 		return (0);
7862 	}
7863 	if (asoc->peers_rwnd == 0) {
7864 		/* No room in peers rwnd */
7865 		*reason_code = 1;
7866 		if (asoc->total_flight > 0) {
7867 			/* we are allowed one chunk in flight */
7868 			no_data_chunks = 1;
7869 		}
7870 	}
7871 	if (stcb->asoc.ecn_echo_cnt_onq) {
7872 		/* Record where a sack goes, if any */
7873 		if (no_data_chunks &&
7874 		    (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) {
7875 			/* Nothing but ECNe to send - we don't do that */
7876 			goto nothing_to_send;
7877 		}
7878 		TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7879 			if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7880 			    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7881 				sack_goes_to = chk->whoTo;
7882 				break;
7883 			}
7884 		}
7885 	}
7886 	max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7887 	if (stcb->sctp_socket)
7888 		max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets;
7889 	else
7890 		max_send_per_dest = 0;
7891 	if (no_data_chunks == 0) {
7892 		/* How many non-directed chunks are there? */
7893 		TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7894 			if (chk->whoTo == NULL) {
7895 				/*
7896 				 * We already have non-directed chunks on
7897 				 * the queue, no need to do a fill-up.
7898 				 */
7899 				skip_fill_up = 1;
7900 				break;
7901 			}
7902 		}
7903 	}
7904 	if ((no_data_chunks == 0) &&
7905 	    (skip_fill_up == 0) &&
7906 	    (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) {
7907 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7908 			/*
7909 			 * This for loop we are in takes in each net, if
7910 			 * its's got space in cwnd and has data sent to it
7911 			 * (when CMT is off) then it calls
7912 			 * sctp_fill_outqueue for the net. This gets data on
7913 			 * the send queue for that network.
7914 			 *
7915 			 * In sctp_fill_outqueue TSN's are assigned and data
7916 			 * is copied out of the stream buffers. Note mostly
7917 			 * copy by reference (we hope).
7918 			 */
7919 			net->window_probe = 0;
7920 			if ((net != stcb->asoc.alternate) &&
7921 			    ((net->dest_state & SCTP_ADDR_PF) ||
7922 			    ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) ||
7923 			    (net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
7924 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7925 					sctp_log_cwnd(stcb, net, 1,
7926 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7927 				}
7928 				continue;
7929 			}
7930 			if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
7931 			    (net->flight_size == 0)) {
7932 				(*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
7933 			}
7934 			if (net->flight_size >= net->cwnd) {
7935 				/* skip this network, no room - can't fill */
7936 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7937 					sctp_log_cwnd(stcb, net, 3,
7938 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7939 				}
7940 				continue;
7941 			}
7942 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7943 				sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7944 			}
7945 			sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked);
7946 			if (quit_now) {
7947 				/* memory alloc failure */
7948 				no_data_chunks = 1;
7949 				break;
7950 			}
7951 		}
7952 	}
7953 	/* now service each destination and send out what we can for it */
7954 	/* Nothing to send? */
7955 	if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7956 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7957 	    TAILQ_EMPTY(&asoc->send_queue)) {
7958 		*reason_code = 8;
7959 		return (0);
7960 	}
7961 
7962 	if (asoc->sctp_cmt_on_off > 0) {
7963 		/* get the last start point */
7964 		start_at = asoc->last_net_cmt_send_started;
7965 		if (start_at == NULL) {
7966 			/* null so to beginning */
7967 			start_at = TAILQ_FIRST(&asoc->nets);
7968 		} else {
7969 			start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7970 			if (start_at == NULL) {
7971 				start_at = TAILQ_FIRST(&asoc->nets);
7972 			}
7973 		}
7974 		asoc->last_net_cmt_send_started = start_at;
7975 	} else {
7976 		start_at = TAILQ_FIRST(&asoc->nets);
7977 	}
7978 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7979 		if (chk->whoTo == NULL) {
7980 			if (asoc->alternate) {
7981 				chk->whoTo = asoc->alternate;
7982 			} else {
7983 				chk->whoTo = asoc->primary_destination;
7984 			}
7985 			atomic_add_int(&chk->whoTo->ref_count, 1);
7986 		}
7987 	}
7988 	old_start_at = NULL;
7989 again_one_more_time:
7990 	for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7991 		/* how much can we send? */
7992 		/* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7993 		if (old_start_at && (old_start_at == net)) {
7994 			/* through list completely. */
7995 			break;
7996 		}
7997 		tsns_sent = 0xa;
7998 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7999 		    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
8000 		    (net->flight_size >= net->cwnd)) {
8001 			/*
8002 			 * Nothing on control or asconf and flight is full,
8003 			 * we can skip even in the CMT case.
8004 			 */
8005 			continue;
8006 		}
8007 		bundle_at = 0;
8008 		endoutchain = outchain = NULL;
8009 		auth = NULL;
8010 		auth_offset = 0;
8011 		no_fragmentflg = 1;
8012 		one_chunk = 0;
8013 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
8014 			skip_data_for_this_net = 1;
8015 		} else {
8016 			skip_data_for_this_net = 0;
8017 		}
8018 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8019 #ifdef INET
8020 		case AF_INET:
8021 			mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8022 			break;
8023 #endif
8024 #ifdef INET6
8025 		case AF_INET6:
8026 			mtu = net->mtu - SCTP_MIN_OVERHEAD;
8027 			break;
8028 #endif
8029 		default:
8030 			/* TSNH */
8031 			mtu = net->mtu;
8032 			break;
8033 		}
8034 		mx_mtu = mtu;
8035 		to_out = 0;
8036 		if (mtu > asoc->peers_rwnd) {
8037 			if (asoc->total_flight > 0) {
8038 				/* We have a packet in flight somewhere */
8039 				r_mtu = asoc->peers_rwnd;
8040 			} else {
8041 				/* We are always allowed to send one MTU out */
8042 				one_chunk = 1;
8043 				r_mtu = mtu;
8044 			}
8045 		} else {
8046 			r_mtu = mtu;
8047 		}
8048 		error = 0;
8049 		/************************/
8050 		/* ASCONF transmission */
8051 		/************************/
8052 		/* Now first lets go through the asconf queue */
8053 		TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
8054 			if (chk->rec.chunk_id.id != SCTP_ASCONF) {
8055 				continue;
8056 			}
8057 			if (chk->whoTo == NULL) {
8058 				if (asoc->alternate == NULL) {
8059 					if (asoc->primary_destination != net) {
8060 						break;
8061 					}
8062 				} else {
8063 					if (asoc->alternate != net) {
8064 						break;
8065 					}
8066 				}
8067 			} else {
8068 				if (chk->whoTo != net) {
8069 					break;
8070 				}
8071 			}
8072 			if (chk->data == NULL) {
8073 				break;
8074 			}
8075 			if (chk->sent != SCTP_DATAGRAM_UNSENT &&
8076 			    chk->sent != SCTP_DATAGRAM_RESEND) {
8077 				break;
8078 			}
8079 			/*
8080 			 * if no AUTH is yet included and this chunk
8081 			 * requires it, make sure to account for it.  We
8082 			 * don't apply the size until the AUTH chunk is
8083 			 * actually added below in case there is no room for
8084 			 * this chunk. NOTE: we overload the use of "omtu"
8085 			 * here
8086 			 */
8087 			if ((auth == NULL) &&
8088 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8089 			    stcb->asoc.peer_auth_chunks)) {
8090 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8091 			} else
8092 				omtu = 0;
8093 			/* Here we do NOT factor the r_mtu */
8094 			if ((chk->send_size < (int)(mtu - omtu)) ||
8095 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8096 				/*
8097 				 * We probably should glom the mbuf chain
8098 				 * from the chk->data for control but the
8099 				 * problem is it becomes yet one more level
8100 				 * of tracking to do if for some reason
8101 				 * output fails. Then I have got to
8102 				 * reconstruct the merged control chain.. el
8103 				 * yucko.. for now we take the easy way and
8104 				 * do the copy
8105 				 */
8106 				/*
8107 				 * Add an AUTH chunk, if chunk requires it
8108 				 * save the offset into the chain for AUTH
8109 				 */
8110 				if ((auth == NULL) &&
8111 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8112 				    stcb->asoc.peer_auth_chunks))) {
8113 					outchain = sctp_add_auth_chunk(outchain,
8114 					    &endoutchain,
8115 					    &auth,
8116 					    &auth_offset,
8117 					    stcb,
8118 					    chk->rec.chunk_id.id);
8119 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8120 				}
8121 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8122 				    (int)chk->rec.chunk_id.can_take_data,
8123 				    chk->send_size, chk->copy_by_ref);
8124 				if (outchain == NULL) {
8125 					*reason_code = 8;
8126 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8127 					return (ENOMEM);
8128 				}
8129 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8130 				/* update our MTU size */
8131 				if (mtu > (chk->send_size + omtu))
8132 					mtu -= (chk->send_size + omtu);
8133 				else
8134 					mtu = 0;
8135 				to_out += (chk->send_size + omtu);
8136 				/* Do clear IP_DF ? */
8137 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8138 					no_fragmentflg = 0;
8139 				}
8140 				if (chk->rec.chunk_id.can_take_data)
8141 					chk->data = NULL;
8142 				/*
8143 				 * set hb flag since we can use these for
8144 				 * RTO
8145 				 */
8146 				hbflag = 1;
8147 				asconf = 1;
8148 				/*
8149 				 * should sysctl this: don't bundle data
8150 				 * with ASCONF since it requires AUTH
8151 				 */
8152 				no_data_chunks = 1;
8153 				chk->sent = SCTP_DATAGRAM_SENT;
8154 				if (chk->whoTo == NULL) {
8155 					chk->whoTo = net;
8156 					atomic_add_int(&net->ref_count, 1);
8157 				}
8158 				chk->snd_count++;
8159 				if (mtu == 0) {
8160 					/*
8161 					 * Ok we are out of room but we can
8162 					 * output without effecting the
8163 					 * flight size since this little guy
8164 					 * is a control only packet.
8165 					 */
8166 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8167 					/*
8168 					 * do NOT clear the asconf flag as
8169 					 * it is used to do appropriate
8170 					 * source address selection.
8171 					 */
8172 					if (*now_filled == 0) {
8173 						(void)SCTP_GETTIME_TIMEVAL(now);
8174 						*now_filled = 1;
8175 					}
8176 					net->last_sent_time = *now;
8177 					hbflag = 0;
8178 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8179 					    (struct sockaddr *)&net->ro._l_addr,
8180 					    outchain, auth_offset, auth,
8181 					    stcb->asoc.authinfo.active_keyid,
8182 					    no_fragmentflg, 0, asconf,
8183 					    inp->sctp_lport, stcb->rport,
8184 					    htonl(stcb->asoc.peer_vtag),
8185 					    net->port, NULL,
8186 					    0, 0,
8187 					    false, so_locked))) {
8188 						/*
8189 						 * error, we could not
8190 						 * output
8191 						 */
8192 						SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8193 						if (from_where == 0) {
8194 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
8195 						}
8196 						if (error == ENOBUFS) {
8197 							asoc->ifp_had_enobuf = 1;
8198 							SCTP_STAT_INCR(sctps_lowlevelerr);
8199 						}
8200 						/* error, could not output */
8201 						if (error == EHOSTUNREACH) {
8202 							/*
8203 							 * Destination went
8204 							 * unreachable
8205 							 * during this send
8206 							 */
8207 							sctp_move_chunks_from_net(stcb, net);
8208 						}
8209 						asconf = 0;
8210 						*reason_code = 7;
8211 						break;
8212 					} else {
8213 						asoc->ifp_had_enobuf = 0;
8214 					}
8215 					/*
8216 					 * increase the number we sent, if a
8217 					 * cookie is sent we don't tell them
8218 					 * any was sent out.
8219 					 */
8220 					outchain = endoutchain = NULL;
8221 					auth = NULL;
8222 					auth_offset = 0;
8223 					asconf = 0;
8224 					if (!no_out_cnt)
8225 						*num_out += ctl_cnt;
8226 					/* recalc a clean slate and setup */
8227 					switch (net->ro._l_addr.sa.sa_family) {
8228 #ifdef INET
8229 					case AF_INET:
8230 						mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8231 						break;
8232 #endif
8233 #ifdef INET6
8234 					case AF_INET6:
8235 						mtu = net->mtu - SCTP_MIN_OVERHEAD;
8236 						break;
8237 #endif
8238 					default:
8239 						/* TSNH */
8240 						mtu = net->mtu;
8241 						break;
8242 					}
8243 					to_out = 0;
8244 					no_fragmentflg = 1;
8245 				}
8246 			}
8247 		}
8248 		if (error != 0) {
8249 			/* try next net */
8250 			continue;
8251 		}
8252 		/************************/
8253 		/* Control transmission */
8254 		/************************/
8255 		/* Now first lets go through the control queue */
8256 		TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
8257 			if ((sack_goes_to) &&
8258 			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO) &&
8259 			    (chk->whoTo != sack_goes_to)) {
8260 				/*
8261 				 * if we have a sack in queue, and we are
8262 				 * looking at an ecn echo that is NOT queued
8263 				 * to where the sack is going..
8264 				 */
8265 				if (chk->whoTo == net) {
8266 					/*
8267 					 * Don't transmit it to where its
8268 					 * going (current net)
8269 					 */
8270 					continue;
8271 				} else if (sack_goes_to == net) {
8272 					/*
8273 					 * But do transmit it to this
8274 					 * address
8275 					 */
8276 					goto skip_net_check;
8277 				}
8278 			}
8279 			if (chk->whoTo == NULL) {
8280 				if (asoc->alternate == NULL) {
8281 					if (asoc->primary_destination != net) {
8282 						continue;
8283 					}
8284 				} else {
8285 					if (asoc->alternate != net) {
8286 						continue;
8287 					}
8288 				}
8289 			} else {
8290 				if (chk->whoTo != net) {
8291 					continue;
8292 				}
8293 			}
8294 	skip_net_check:
8295 			if (chk->data == NULL) {
8296 				continue;
8297 			}
8298 			if (chk->sent != SCTP_DATAGRAM_UNSENT) {
8299 				/*
8300 				 * It must be unsent. Cookies and ASCONF's
8301 				 * hang around but there timers will force
8302 				 * when marked for resend.
8303 				 */
8304 				continue;
8305 			}
8306 			/*
8307 			 * if no AUTH is yet included and this chunk
8308 			 * requires it, make sure to account for it.  We
8309 			 * don't apply the size until the AUTH chunk is
8310 			 * actually added below in case there is no room for
8311 			 * this chunk. NOTE: we overload the use of "omtu"
8312 			 * here
8313 			 */
8314 			if ((auth == NULL) &&
8315 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8316 			    stcb->asoc.peer_auth_chunks)) {
8317 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8318 			} else
8319 				omtu = 0;
8320 			/* Here we do NOT factor the r_mtu */
8321 			if ((chk->send_size <= (int)(mtu - omtu)) ||
8322 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8323 				/*
8324 				 * We probably should glom the mbuf chain
8325 				 * from the chk->data for control but the
8326 				 * problem is it becomes yet one more level
8327 				 * of tracking to do if for some reason
8328 				 * output fails. Then I have got to
8329 				 * reconstruct the merged control chain.. el
8330 				 * yucko.. for now we take the easy way and
8331 				 * do the copy
8332 				 */
8333 				/*
8334 				 * Add an AUTH chunk, if chunk requires it
8335 				 * save the offset into the chain for AUTH
8336 				 */
8337 				if ((auth == NULL) &&
8338 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8339 				    stcb->asoc.peer_auth_chunks))) {
8340 					outchain = sctp_add_auth_chunk(outchain,
8341 					    &endoutchain,
8342 					    &auth,
8343 					    &auth_offset,
8344 					    stcb,
8345 					    chk->rec.chunk_id.id);
8346 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8347 				}
8348 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8349 				    (int)chk->rec.chunk_id.can_take_data,
8350 				    chk->send_size, chk->copy_by_ref);
8351 				if (outchain == NULL) {
8352 					*reason_code = 8;
8353 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8354 					return (ENOMEM);
8355 				}
8356 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8357 				/* update our MTU size */
8358 				if (mtu > (chk->send_size + omtu))
8359 					mtu -= (chk->send_size + omtu);
8360 				else
8361 					mtu = 0;
8362 				to_out += (chk->send_size + omtu);
8363 				/* Do clear IP_DF ? */
8364 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8365 					no_fragmentflg = 0;
8366 				}
8367 				if (chk->rec.chunk_id.can_take_data)
8368 					chk->data = NULL;
8369 				/* Mark things to be removed, if needed */
8370 				if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8371 				    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
8372 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
8373 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
8374 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
8375 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
8376 				    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
8377 				    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
8378 				    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
8379 				    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
8380 				    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
8381 					if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
8382 						hbflag = 1;
8383 					}
8384 					/* remove these chunks at the end */
8385 					if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8386 					    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
8387 						/* turn off the timer */
8388 						if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
8389 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
8390 							    inp, stcb, NULL,
8391 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
8392 						}
8393 					}
8394 					ctl_cnt++;
8395 				} else {
8396 					/*
8397 					 * Other chunks, since they have
8398 					 * timers running (i.e. COOKIE) we
8399 					 * just "trust" that it gets sent or
8400 					 * retransmitted.
8401 					 */
8402 					ctl_cnt++;
8403 					if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8404 						cookie = 1;
8405 						no_out_cnt = 1;
8406 					} else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
8407 						/*
8408 						 * Increment ecne send count
8409 						 * here this means we may be
8410 						 * over-zealous in our
8411 						 * counting if the send
8412 						 * fails, but its the best
8413 						 * place to do it (we used
8414 						 * to do it in the queue of
8415 						 * the chunk, but that did
8416 						 * not tell how many times
8417 						 * it was sent.
8418 						 */
8419 						SCTP_STAT_INCR(sctps_sendecne);
8420 					}
8421 					chk->sent = SCTP_DATAGRAM_SENT;
8422 					if (chk->whoTo == NULL) {
8423 						chk->whoTo = net;
8424 						atomic_add_int(&net->ref_count, 1);
8425 					}
8426 					chk->snd_count++;
8427 				}
8428 				if (mtu == 0) {
8429 					/*
8430 					 * Ok we are out of room but we can
8431 					 * output without effecting the
8432 					 * flight size since this little guy
8433 					 * is a control only packet.
8434 					 */
8435 					switch (asoc->snd_edmid) {
8436 					case SCTP_EDMID_LOWER_LAYER_DTLS:
8437 						use_zero_crc = true;
8438 						break;
8439 					default:
8440 						use_zero_crc = false;
8441 						break;
8442 					}
8443 					if (asconf) {
8444 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8445 						use_zero_crc = false;
8446 						/*
8447 						 * do NOT clear the asconf
8448 						 * flag as it is used to do
8449 						 * appropriate source
8450 						 * address selection.
8451 						 */
8452 					}
8453 					if (cookie) {
8454 						sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8455 						use_zero_crc = false;
8456 						cookie = 0;
8457 					}
8458 					/* Only HB or ASCONF advances time */
8459 					if (hbflag) {
8460 						if (*now_filled == 0) {
8461 							(void)SCTP_GETTIME_TIMEVAL(now);
8462 							*now_filled = 1;
8463 						}
8464 						net->last_sent_time = *now;
8465 						hbflag = 0;
8466 					}
8467 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8468 					    (struct sockaddr *)&net->ro._l_addr,
8469 					    outchain,
8470 					    auth_offset, auth,
8471 					    stcb->asoc.authinfo.active_keyid,
8472 					    no_fragmentflg, 0, asconf,
8473 					    inp->sctp_lport, stcb->rport,
8474 					    htonl(stcb->asoc.peer_vtag),
8475 					    net->port, NULL,
8476 					    0, 0,
8477 					    use_zero_crc, so_locked))) {
8478 						/*
8479 						 * error, we could not
8480 						 * output
8481 						 */
8482 						SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8483 						if (from_where == 0) {
8484 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
8485 						}
8486 						if (error == ENOBUFS) {
8487 							asoc->ifp_had_enobuf = 1;
8488 							SCTP_STAT_INCR(sctps_lowlevelerr);
8489 						}
8490 						if (error == EHOSTUNREACH) {
8491 							/*
8492 							 * Destination went
8493 							 * unreachable
8494 							 * during this send
8495 							 */
8496 							sctp_move_chunks_from_net(stcb, net);
8497 						}
8498 						asconf = 0;
8499 						*reason_code = 7;
8500 						break;
8501 					} else {
8502 						asoc->ifp_had_enobuf = 0;
8503 					}
8504 					/*
8505 					 * increase the number we sent, if a
8506 					 * cookie is sent we don't tell them
8507 					 * any was sent out.
8508 					 */
8509 					outchain = endoutchain = NULL;
8510 					auth = NULL;
8511 					auth_offset = 0;
8512 					asconf = 0;
8513 					if (!no_out_cnt)
8514 						*num_out += ctl_cnt;
8515 					/* recalc a clean slate and setup */
8516 					switch (net->ro._l_addr.sa.sa_family) {
8517 #ifdef INET
8518 					case AF_INET:
8519 						mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8520 						break;
8521 #endif
8522 #ifdef INET6
8523 					case AF_INET6:
8524 						mtu = net->mtu - SCTP_MIN_OVERHEAD;
8525 						break;
8526 #endif
8527 					default:
8528 						/* TSNH */
8529 						mtu = net->mtu;
8530 						break;
8531 					}
8532 					to_out = 0;
8533 					no_fragmentflg = 1;
8534 				}
8535 			}
8536 		}
8537 		if (error != 0) {
8538 			/* try next net */
8539 			continue;
8540 		}
8541 		/* JRI: if dest is in PF state, do not send data to it */
8542 		if ((asoc->sctp_cmt_on_off > 0) &&
8543 		    (net != stcb->asoc.alternate) &&
8544 		    (net->dest_state & SCTP_ADDR_PF)) {
8545 			goto no_data_fill;
8546 		}
8547 		if (net->flight_size >= net->cwnd) {
8548 			goto no_data_fill;
8549 		}
8550 		if ((asoc->sctp_cmt_on_off > 0) &&
8551 		    (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) &&
8552 		    (net->flight_size > max_rwnd_per_dest)) {
8553 			goto no_data_fill;
8554 		}
8555 		/*
8556 		 * We need a specific accounting for the usage of the send
8557 		 * buffer. We also need to check the number of messages per
8558 		 * net. For now, this is better than nothing and it disabled
8559 		 * by default...
8560 		 */
8561 		if ((asoc->sctp_cmt_on_off > 0) &&
8562 		    (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) &&
8563 		    (max_send_per_dest > 0) &&
8564 		    (net->flight_size > max_send_per_dest)) {
8565 			goto no_data_fill;
8566 		}
8567 		/*********************/
8568 		/* Data transmission */
8569 		/*********************/
8570 		/*
8571 		 * if AUTH for DATA is required and no AUTH has been added
8572 		 * yet, account for this in the mtu now... if no data can be
8573 		 * bundled, this adjustment won't matter anyways since the
8574 		 * packet will be going out...
8575 		 */
8576 		data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8577 		    stcb->asoc.peer_auth_chunks);
8578 		if (data_auth_reqd && (auth == NULL)) {
8579 			mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8580 		}
8581 		/* now lets add any data within the MTU constraints */
8582 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8583 #ifdef INET
8584 		case AF_INET:
8585 			if (net->mtu > SCTP_MIN_V4_OVERHEAD)
8586 				omtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8587 			else
8588 				omtu = 0;
8589 			break;
8590 #endif
8591 #ifdef INET6
8592 		case AF_INET6:
8593 			if (net->mtu > SCTP_MIN_OVERHEAD)
8594 				omtu = net->mtu - SCTP_MIN_OVERHEAD;
8595 			else
8596 				omtu = 0;
8597 			break;
8598 #endif
8599 		default:
8600 			/* TSNH */
8601 			omtu = 0;
8602 			break;
8603 		}
8604 		if ((((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
8605 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) &&
8606 		    (skip_data_for_this_net == 0)) ||
8607 		    (cookie)) {
8608 			TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
8609 				if (no_data_chunks) {
8610 					/* let only control go out */
8611 					*reason_code = 1;
8612 					break;
8613 				}
8614 				if (net->flight_size >= net->cwnd) {
8615 					/* skip this net, no room for data */
8616 					*reason_code = 2;
8617 					break;
8618 				}
8619 				if ((chk->whoTo != NULL) &&
8620 				    (chk->whoTo != net)) {
8621 					/* Don't send the chunk on this net */
8622 					continue;
8623 				}
8624 
8625 				if (asoc->sctp_cmt_on_off == 0) {
8626 					if ((asoc->alternate) &&
8627 					    (asoc->alternate != net) &&
8628 					    (chk->whoTo == NULL)) {
8629 						continue;
8630 					} else if ((net != asoc->primary_destination) &&
8631 						    (asoc->alternate == NULL) &&
8632 					    (chk->whoTo == NULL)) {
8633 						continue;
8634 					}
8635 				}
8636 				if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8637 					/*-
8638 					 * strange, we have a chunk that is
8639 					 * to big for its destination and
8640 					 * yet no fragment ok flag.
8641 					 * Something went wrong when the
8642 					 * PMTU changed...we did not mark
8643 					 * this chunk for some reason?? I
8644 					 * will fix it here by letting IP
8645 					 * fragment it for now and printing
8646 					 * a warning. This really should not
8647 					 * happen ...
8648 					 */
8649 					SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8650 					    chk->send_size, mtu);
8651 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8652 				}
8653 				if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8654 				    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
8655 					struct sctp_data_chunk *dchkh;
8656 
8657 					dchkh = mtod(chk->data, struct sctp_data_chunk *);
8658 					dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8659 				}
8660 				if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8661 				    ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8662 					/* ok we will add this one */
8663 
8664 					/*
8665 					 * Add an AUTH chunk, if chunk
8666 					 * requires it, save the offset into
8667 					 * the chain for AUTH
8668 					 */
8669 					if (data_auth_reqd) {
8670 						if (auth == NULL) {
8671 							outchain = sctp_add_auth_chunk(outchain,
8672 							    &endoutchain,
8673 							    &auth,
8674 							    &auth_offset,
8675 							    stcb,
8676 							    SCTP_DATA);
8677 							auth_keyid = chk->auth_keyid;
8678 							override_ok = 0;
8679 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8680 						} else if (override_ok) {
8681 							/*
8682 							 * use this data's
8683 							 * keyid
8684 							 */
8685 							auth_keyid = chk->auth_keyid;
8686 							override_ok = 0;
8687 						} else if (auth_keyid != chk->auth_keyid) {
8688 							/*
8689 							 * different keyid,
8690 							 * so done bundling
8691 							 */
8692 							break;
8693 						}
8694 					}
8695 					outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8696 					    chk->send_size, chk->copy_by_ref);
8697 					if (outchain == NULL) {
8698 						SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8699 						if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8700 							sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8701 						}
8702 						*reason_code = 3;
8703 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8704 						return (ENOMEM);
8705 					}
8706 					/* update our MTU size */
8707 					/* Do clear IP_DF ? */
8708 					if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8709 						no_fragmentflg = 0;
8710 					}
8711 					/* unsigned subtraction of mtu */
8712 					if (mtu > chk->send_size)
8713 						mtu -= chk->send_size;
8714 					else
8715 						mtu = 0;
8716 					/* unsigned subtraction of r_mtu */
8717 					if (r_mtu > chk->send_size)
8718 						r_mtu -= chk->send_size;
8719 					else
8720 						r_mtu = 0;
8721 
8722 					to_out += chk->send_size;
8723 					if ((to_out > mx_mtu) && no_fragmentflg) {
8724 #ifdef INVARIANTS
8725 						panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8726 #else
8727 						SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8728 						    mx_mtu, to_out);
8729 #endif
8730 					}
8731 					chk->window_probe = 0;
8732 					data_list[bundle_at++] = chk;
8733 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8734 						break;
8735 					}
8736 					if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8737 						if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8738 							SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8739 						} else {
8740 							SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8741 						}
8742 						if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8743 						    ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8744 							/*
8745 							 * Count number of
8746 							 * user msg's that
8747 							 * were fragmented
8748 							 * we do this by
8749 							 * counting when we
8750 							 * see a LAST
8751 							 * fragment only.
8752 							 */
8753 							SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8754 					}
8755 					if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8756 						if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8757 							data_list[0]->window_probe = 1;
8758 							net->window_probe = 1;
8759 						}
8760 						break;
8761 					}
8762 				} else {
8763 					/*
8764 					 * Must be sent in order of the
8765 					 * TSN's (on a network)
8766 					 */
8767 					break;
8768 				}
8769 			}	/* for (chunk gather loop for this net) */
8770 		}		/* if asoc.state OPEN */
8771 no_data_fill:
8772 		/* Is there something to send for this destination? */
8773 		if (outchain) {
8774 			switch (asoc->snd_edmid) {
8775 			case SCTP_EDMID_LOWER_LAYER_DTLS:
8776 				use_zero_crc = true;
8777 				break;
8778 			default:
8779 				use_zero_crc = false;
8780 				break;
8781 			}
8782 			/* We may need to start a control timer or two */
8783 			if (asconf) {
8784 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8785 				    stcb, net);
8786 				use_zero_crc = false;
8787 				/*
8788 				 * do NOT clear the asconf flag as it is
8789 				 * used to do appropriate source address
8790 				 * selection.
8791 				 */
8792 			}
8793 			if (cookie) {
8794 				sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8795 				use_zero_crc = false;
8796 				cookie = 0;
8797 			}
8798 			/* must start a send timer if data is being sent */
8799 			if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8800 				/*
8801 				 * no timer running on this destination
8802 				 * restart it.
8803 				 */
8804 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8805 			}
8806 			if (bundle_at || hbflag) {
8807 				/* For data/asconf and hb set time */
8808 				if (*now_filled == 0) {
8809 					(void)SCTP_GETTIME_TIMEVAL(now);
8810 					*now_filled = 1;
8811 				}
8812 				net->last_sent_time = *now;
8813 			}
8814 			/* Now send it, if there is anything to send :> */
8815 			if ((error = sctp_lowlevel_chunk_output(inp,
8816 			    stcb,
8817 			    net,
8818 			    (struct sockaddr *)&net->ro._l_addr,
8819 			    outchain,
8820 			    auth_offset,
8821 			    auth,
8822 			    auth_keyid,
8823 			    no_fragmentflg,
8824 			    bundle_at,
8825 			    asconf,
8826 			    inp->sctp_lport, stcb->rport,
8827 			    htonl(stcb->asoc.peer_vtag),
8828 			    net->port, NULL,
8829 			    0, 0,
8830 			    use_zero_crc,
8831 			    so_locked))) {
8832 				/* error, we could not output */
8833 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8834 				if (from_where == 0) {
8835 					SCTP_STAT_INCR(sctps_lowlevelerrusr);
8836 				}
8837 				if (error == ENOBUFS) {
8838 					asoc->ifp_had_enobuf = 1;
8839 					SCTP_STAT_INCR(sctps_lowlevelerr);
8840 				}
8841 				if (error == EHOSTUNREACH) {
8842 					/*
8843 					 * Destination went unreachable
8844 					 * during this send
8845 					 */
8846 					sctp_move_chunks_from_net(stcb, net);
8847 				}
8848 				asconf = 0;
8849 				*reason_code = 6;
8850 				/*-
8851 				 * I add this line to be paranoid. As far as
8852 				 * I can tell the continue, takes us back to
8853 				 * the top of the for, but just to make sure
8854 				 * I will reset these again here.
8855 				 */
8856 				ctl_cnt = 0;
8857 				continue;	/* This takes us back to the
8858 						 * for() for the nets. */
8859 			} else {
8860 				asoc->ifp_had_enobuf = 0;
8861 			}
8862 			endoutchain = NULL;
8863 			auth = NULL;
8864 			auth_offset = 0;
8865 			asconf = 0;
8866 			if (!no_out_cnt) {
8867 				*num_out += (ctl_cnt + bundle_at);
8868 			}
8869 			if (bundle_at) {
8870 				/* setup for a RTO measurement */
8871 				tsns_sent = data_list[0]->rec.data.tsn;
8872 				/* fill time if not already filled */
8873 				if (*now_filled == 0) {
8874 					(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8875 					*now_filled = 1;
8876 					*now = asoc->time_last_sent;
8877 				} else {
8878 					asoc->time_last_sent = *now;
8879 				}
8880 				if (net->rto_needed) {
8881 					data_list[0]->do_rtt = 1;
8882 					net->rto_needed = 0;
8883 				}
8884 				SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8885 				sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8886 			}
8887 			if (one_chunk) {
8888 				break;
8889 			}
8890 		}
8891 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8892 			sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8893 		}
8894 	}
8895 	if (old_start_at == NULL) {
8896 		old_start_at = start_at;
8897 		start_at = TAILQ_FIRST(&asoc->nets);
8898 		if (old_start_at)
8899 			goto again_one_more_time;
8900 	}
8901 
8902 	/*
8903 	 * At the end there should be no NON timed chunks hanging on this
8904 	 * queue.
8905 	 */
8906 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8907 		sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8908 	}
8909 	if ((*num_out == 0) && (*reason_code == 0)) {
8910 		*reason_code = 4;
8911 	} else {
8912 		*reason_code = 5;
8913 	}
8914 	sctp_clean_up_ctl(stcb, asoc, so_locked);
8915 	return (0);
8916 }
8917 
8918 void
8919 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8920 {
8921 	/*-
8922 	 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8923 	 * the control chunk queue.
8924 	 */
8925 	struct sctp_chunkhdr *hdr;
8926 	struct sctp_tmit_chunk *chk;
8927 	struct mbuf *mat, *last_mbuf;
8928 	uint32_t chunk_length;
8929 	uint16_t padding_length;
8930 
8931 	SCTP_TCB_LOCK_ASSERT(stcb);
8932 	SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_NOWAIT);
8933 	if (op_err == NULL) {
8934 		return;
8935 	}
8936 	last_mbuf = NULL;
8937 	chunk_length = 0;
8938 	for (mat = op_err; mat != NULL; mat = SCTP_BUF_NEXT(mat)) {
8939 		chunk_length += SCTP_BUF_LEN(mat);
8940 		if (SCTP_BUF_NEXT(mat) == NULL) {
8941 			last_mbuf = mat;
8942 		}
8943 	}
8944 	if (chunk_length > SCTP_MAX_CHUNK_LENGTH) {
8945 		sctp_m_freem(op_err);
8946 		return;
8947 	}
8948 	padding_length = chunk_length % 4;
8949 	if (padding_length != 0) {
8950 		padding_length = 4 - padding_length;
8951 	}
8952 	if (padding_length != 0) {
8953 		if (sctp_add_pad_tombuf(last_mbuf, padding_length) == NULL) {
8954 			sctp_m_freem(op_err);
8955 			return;
8956 		}
8957 	}
8958 	sctp_alloc_a_chunk(stcb, chk);
8959 	if (chk == NULL) {
8960 		/* no memory */
8961 		sctp_m_freem(op_err);
8962 		return;
8963 	}
8964 	chk->copy_by_ref = 0;
8965 	chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8966 	chk->rec.chunk_id.can_take_data = 0;
8967 	chk->flags = 0;
8968 	chk->send_size = (uint16_t)chunk_length;
8969 	chk->sent = SCTP_DATAGRAM_UNSENT;
8970 	chk->snd_count = 0;
8971 	chk->asoc = &stcb->asoc;
8972 	chk->data = op_err;
8973 	chk->whoTo = NULL;
8974 	hdr = mtod(op_err, struct sctp_chunkhdr *);
8975 	hdr->chunk_type = SCTP_OPERATION_ERROR;
8976 	hdr->chunk_flags = 0;
8977 	hdr->chunk_length = htons(chk->send_size);
8978 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8979 	chk->asoc->ctrl_queue_cnt++;
8980 }
8981 
8982 int
8983 sctp_send_cookie_echo(struct mbuf *m,
8984     int offset, int limit,
8985     struct sctp_tcb *stcb,
8986     struct sctp_nets *net)
8987 {
8988 	/*-
8989 	 * pull out the cookie and put it at the front of the control chunk
8990 	 * queue.
8991 	 */
8992 	int at;
8993 	struct mbuf *cookie;
8994 	struct sctp_paramhdr param, *phdr;
8995 	struct sctp_chunkhdr *hdr;
8996 	struct sctp_tmit_chunk *chk;
8997 	uint16_t ptype, plen;
8998 
8999 	SCTP_TCB_LOCK_ASSERT(stcb);
9000 	/* First find the cookie in the param area */
9001 	cookie = NULL;
9002 	at = offset + sizeof(struct sctp_init_chunk);
9003 	for (;;) {
9004 		phdr = sctp_get_next_param(m, at, &param, sizeof(param));
9005 		if (phdr == NULL) {
9006 			return (-3);
9007 		}
9008 		ptype = ntohs(phdr->param_type);
9009 		plen = ntohs(phdr->param_length);
9010 		if (plen < sizeof(struct sctp_paramhdr)) {
9011 			return (-6);
9012 		}
9013 		if (ptype == SCTP_STATE_COOKIE) {
9014 			int pad;
9015 
9016 			/* found the cookie */
9017 			if (at + plen > limit) {
9018 				return (-7);
9019 			}
9020 			cookie = SCTP_M_COPYM(m, at, plen, M_NOWAIT);
9021 			if (cookie == NULL) {
9022 				/* No memory */
9023 				return (-2);
9024 			}
9025 			if ((pad = (plen % 4)) > 0) {
9026 				pad = 4 - pad;
9027 			}
9028 			if (pad > 0) {
9029 				if (sctp_pad_lastmbuf(cookie, pad, NULL) == NULL) {
9030 					return (-8);
9031 				}
9032 			}
9033 #ifdef SCTP_MBUF_LOGGING
9034 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9035 				sctp_log_mbc(cookie, SCTP_MBUF_ICOPY);
9036 			}
9037 #endif
9038 			break;
9039 		}
9040 		at += SCTP_SIZE32(plen);
9041 	}
9042 	/* ok, we got the cookie lets change it into a cookie echo chunk */
9043 	/* first the change from param to cookie */
9044 	hdr = mtod(cookie, struct sctp_chunkhdr *);
9045 	hdr->chunk_type = SCTP_COOKIE_ECHO;
9046 	hdr->chunk_flags = 0;
9047 	/* get the chunk stuff now and place it in the FRONT of the queue */
9048 	sctp_alloc_a_chunk(stcb, chk);
9049 	if (chk == NULL) {
9050 		/* no memory */
9051 		sctp_m_freem(cookie);
9052 		return (-5);
9053 	}
9054 	chk->copy_by_ref = 0;
9055 	chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
9056 	chk->rec.chunk_id.can_take_data = 0;
9057 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9058 	chk->send_size = SCTP_SIZE32(plen);
9059 	chk->sent = SCTP_DATAGRAM_UNSENT;
9060 	chk->snd_count = 0;
9061 	chk->asoc = &stcb->asoc;
9062 	chk->data = cookie;
9063 	chk->whoTo = net;
9064 	atomic_add_int(&chk->whoTo->ref_count, 1);
9065 	TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
9066 	chk->asoc->ctrl_queue_cnt++;
9067 	return (0);
9068 }
9069 
9070 void
9071 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
9072     struct mbuf *m,
9073     int offset,
9074     int chk_length,
9075     struct sctp_nets *net)
9076 {
9077 	/*
9078 	 * take a HB request and make it into a HB ack and send it.
9079 	 */
9080 	struct mbuf *outchain;
9081 	struct sctp_chunkhdr *chdr;
9082 	struct sctp_tmit_chunk *chk;
9083 
9084 	if (net == NULL)
9085 		/* must have a net pointer */
9086 		return;
9087 
9088 	outchain = SCTP_M_COPYM(m, offset, chk_length, M_NOWAIT);
9089 	if (outchain == NULL) {
9090 		/* gak out of memory */
9091 		return;
9092 	}
9093 #ifdef SCTP_MBUF_LOGGING
9094 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9095 		sctp_log_mbc(outchain, SCTP_MBUF_ICOPY);
9096 	}
9097 #endif
9098 	chdr = mtod(outchain, struct sctp_chunkhdr *);
9099 	chdr->chunk_type = SCTP_HEARTBEAT_ACK;
9100 	chdr->chunk_flags = 0;
9101 	if (chk_length % 4 != 0) {
9102 		sctp_pad_lastmbuf(outchain, 4 - (chk_length % 4), NULL);
9103 	}
9104 	sctp_alloc_a_chunk(stcb, chk);
9105 	if (chk == NULL) {
9106 		/* no memory */
9107 		sctp_m_freem(outchain);
9108 		return;
9109 	}
9110 	chk->copy_by_ref = 0;
9111 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
9112 	chk->rec.chunk_id.can_take_data = 1;
9113 	chk->flags = 0;
9114 	chk->send_size = chk_length;
9115 	chk->sent = SCTP_DATAGRAM_UNSENT;
9116 	chk->snd_count = 0;
9117 	chk->asoc = &stcb->asoc;
9118 	chk->data = outchain;
9119 	chk->whoTo = net;
9120 	atomic_add_int(&chk->whoTo->ref_count, 1);
9121 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9122 	chk->asoc->ctrl_queue_cnt++;
9123 }
9124 
9125 void
9126 sctp_send_cookie_ack(struct sctp_tcb *stcb)
9127 {
9128 	/* formulate and queue a cookie-ack back to sender */
9129 	struct mbuf *cookie_ack;
9130 	struct sctp_chunkhdr *hdr;
9131 	struct sctp_tmit_chunk *chk;
9132 
9133 	SCTP_TCB_LOCK_ASSERT(stcb);
9134 
9135 	cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER);
9136 	if (cookie_ack == NULL) {
9137 		/* no mbuf's */
9138 		return;
9139 	}
9140 	SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
9141 	sctp_alloc_a_chunk(stcb, chk);
9142 	if (chk == NULL) {
9143 		/* no memory */
9144 		sctp_m_freem(cookie_ack);
9145 		return;
9146 	}
9147 	chk->copy_by_ref = 0;
9148 	chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
9149 	chk->rec.chunk_id.can_take_data = 1;
9150 	chk->flags = 0;
9151 	chk->send_size = sizeof(struct sctp_chunkhdr);
9152 	chk->sent = SCTP_DATAGRAM_UNSENT;
9153 	chk->snd_count = 0;
9154 	chk->asoc = &stcb->asoc;
9155 	chk->data = cookie_ack;
9156 	if (chk->asoc->last_control_chunk_from != NULL) {
9157 		chk->whoTo = chk->asoc->last_control_chunk_from;
9158 		atomic_add_int(&chk->whoTo->ref_count, 1);
9159 	} else {
9160 		chk->whoTo = NULL;
9161 	}
9162 	hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
9163 	hdr->chunk_type = SCTP_COOKIE_ACK;
9164 	hdr->chunk_flags = 0;
9165 	hdr->chunk_length = htons(chk->send_size);
9166 	SCTP_BUF_LEN(cookie_ack) = chk->send_size;
9167 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9168 	chk->asoc->ctrl_queue_cnt++;
9169 	return;
9170 }
9171 
9172 void
9173 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
9174 {
9175 	/* formulate and queue a SHUTDOWN-ACK back to the sender */
9176 	struct mbuf *m_shutdown_ack;
9177 	struct sctp_shutdown_ack_chunk *ack_cp;
9178 	struct sctp_tmit_chunk *chk;
9179 
9180 	m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_NOWAIT, 1, MT_HEADER);
9181 	if (m_shutdown_ack == NULL) {
9182 		/* no mbuf's */
9183 		return;
9184 	}
9185 	SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
9186 	sctp_alloc_a_chunk(stcb, chk);
9187 	if (chk == NULL) {
9188 		/* no memory */
9189 		sctp_m_freem(m_shutdown_ack);
9190 		return;
9191 	}
9192 	chk->copy_by_ref = 0;
9193 	chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
9194 	chk->rec.chunk_id.can_take_data = 1;
9195 	chk->flags = 0;
9196 	chk->send_size = sizeof(struct sctp_chunkhdr);
9197 	chk->sent = SCTP_DATAGRAM_UNSENT;
9198 	chk->snd_count = 0;
9199 	chk->asoc = &stcb->asoc;
9200 	chk->data = m_shutdown_ack;
9201 	chk->whoTo = net;
9202 	if (chk->whoTo) {
9203 		atomic_add_int(&chk->whoTo->ref_count, 1);
9204 	}
9205 	ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
9206 	ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
9207 	ack_cp->ch.chunk_flags = 0;
9208 	ack_cp->ch.chunk_length = htons(chk->send_size);
9209 	SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
9210 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9211 	chk->asoc->ctrl_queue_cnt++;
9212 	return;
9213 }
9214 
9215 void
9216 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
9217 {
9218 	/* formulate and queue a SHUTDOWN to the sender */
9219 	struct mbuf *m_shutdown;
9220 	struct sctp_shutdown_chunk *shutdown_cp;
9221 	struct sctp_tmit_chunk *chk;
9222 
9223 	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
9224 		if (chk->rec.chunk_id.id == SCTP_SHUTDOWN) {
9225 			/* We already have a SHUTDOWN queued. Reuse it. */
9226 			if (chk->whoTo) {
9227 				sctp_free_remote_addr(chk->whoTo);
9228 				chk->whoTo = NULL;
9229 			}
9230 			break;
9231 		}
9232 	}
9233 	if (chk == NULL) {
9234 		m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_NOWAIT, 1, MT_HEADER);
9235 		if (m_shutdown == NULL) {
9236 			/* no mbuf's */
9237 			return;
9238 		}
9239 		SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
9240 		sctp_alloc_a_chunk(stcb, chk);
9241 		if (chk == NULL) {
9242 			/* no memory */
9243 			sctp_m_freem(m_shutdown);
9244 			return;
9245 		}
9246 		chk->copy_by_ref = 0;
9247 		chk->rec.chunk_id.id = SCTP_SHUTDOWN;
9248 		chk->rec.chunk_id.can_take_data = 1;
9249 		chk->flags = 0;
9250 		chk->send_size = sizeof(struct sctp_shutdown_chunk);
9251 		chk->sent = SCTP_DATAGRAM_UNSENT;
9252 		chk->snd_count = 0;
9253 		chk->asoc = &stcb->asoc;
9254 		chk->data = m_shutdown;
9255 		chk->whoTo = net;
9256 		if (chk->whoTo) {
9257 			atomic_add_int(&chk->whoTo->ref_count, 1);
9258 		}
9259 		shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
9260 		shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
9261 		shutdown_cp->ch.chunk_flags = 0;
9262 		shutdown_cp->ch.chunk_length = htons(chk->send_size);
9263 		shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
9264 		SCTP_BUF_LEN(m_shutdown) = chk->send_size;
9265 		TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9266 		chk->asoc->ctrl_queue_cnt++;
9267 	} else {
9268 		TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, sctp_next);
9269 		chk->whoTo = net;
9270 		if (chk->whoTo) {
9271 			atomic_add_int(&chk->whoTo->ref_count, 1);
9272 		}
9273 		shutdown_cp = mtod(chk->data, struct sctp_shutdown_chunk *);
9274 		shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
9275 		TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
9276 	}
9277 	return;
9278 }
9279 
9280 void
9281 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
9282 {
9283 	/*
9284 	 * formulate and queue an ASCONF to the peer. ASCONF parameters
9285 	 * should be queued on the assoc queue.
9286 	 */
9287 	struct sctp_tmit_chunk *chk;
9288 	struct mbuf *m_asconf;
9289 	int len;
9290 
9291 	SCTP_TCB_LOCK_ASSERT(stcb);
9292 
9293 	if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
9294 	    (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
9295 		/* can't send a new one if there is one in flight already */
9296 		return;
9297 	}
9298 
9299 	/* compose an ASCONF chunk, maximum length is PMTU */
9300 	m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
9301 	if (m_asconf == NULL) {
9302 		return;
9303 	}
9304 
9305 	sctp_alloc_a_chunk(stcb, chk);
9306 	if (chk == NULL) {
9307 		/* no memory */
9308 		sctp_m_freem(m_asconf);
9309 		return;
9310 	}
9311 
9312 	chk->copy_by_ref = 0;
9313 	chk->rec.chunk_id.id = SCTP_ASCONF;
9314 	chk->rec.chunk_id.can_take_data = 0;
9315 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9316 	chk->data = m_asconf;
9317 	chk->send_size = len;
9318 	chk->sent = SCTP_DATAGRAM_UNSENT;
9319 	chk->snd_count = 0;
9320 	chk->asoc = &stcb->asoc;
9321 	chk->whoTo = net;
9322 	if (chk->whoTo) {
9323 		atomic_add_int(&chk->whoTo->ref_count, 1);
9324 	}
9325 	TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
9326 	chk->asoc->ctrl_queue_cnt++;
9327 	return;
9328 }
9329 
9330 void
9331 sctp_send_asconf_ack(struct sctp_tcb *stcb)
9332 {
9333 	/*
9334 	 * formulate and queue a asconf-ack back to sender. the asconf-ack
9335 	 * must be stored in the tcb.
9336 	 */
9337 	struct sctp_tmit_chunk *chk;
9338 	struct sctp_asconf_ack *ack, *latest_ack;
9339 	struct mbuf *m_ack;
9340 	struct sctp_nets *net = NULL;
9341 
9342 	SCTP_TCB_LOCK_ASSERT(stcb);
9343 	/* Get the latest ASCONF-ACK */
9344 	latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
9345 	if (latest_ack == NULL) {
9346 		return;
9347 	}
9348 	if (latest_ack->last_sent_to != NULL &&
9349 	    latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
9350 		/* we're doing a retransmission */
9351 		net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
9352 		if (net == NULL) {
9353 			/* no alternate */
9354 			if (stcb->asoc.last_control_chunk_from == NULL) {
9355 				if (stcb->asoc.alternate) {
9356 					net = stcb->asoc.alternate;
9357 				} else {
9358 					net = stcb->asoc.primary_destination;
9359 				}
9360 			} else {
9361 				net = stcb->asoc.last_control_chunk_from;
9362 			}
9363 		}
9364 	} else {
9365 		/* normal case */
9366 		if (stcb->asoc.last_control_chunk_from == NULL) {
9367 			if (stcb->asoc.alternate) {
9368 				net = stcb->asoc.alternate;
9369 			} else {
9370 				net = stcb->asoc.primary_destination;
9371 			}
9372 		} else {
9373 			net = stcb->asoc.last_control_chunk_from;
9374 		}
9375 	}
9376 	latest_ack->last_sent_to = net;
9377 
9378 	TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
9379 		if (ack->data == NULL) {
9380 			continue;
9381 		}
9382 
9383 		/* copy the asconf_ack */
9384 		m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_NOWAIT);
9385 		if (m_ack == NULL) {
9386 			/* couldn't copy it */
9387 			return;
9388 		}
9389 #ifdef SCTP_MBUF_LOGGING
9390 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9391 			sctp_log_mbc(m_ack, SCTP_MBUF_ICOPY);
9392 		}
9393 #endif
9394 
9395 		sctp_alloc_a_chunk(stcb, chk);
9396 		if (chk == NULL) {
9397 			/* no memory */
9398 			if (m_ack)
9399 				sctp_m_freem(m_ack);
9400 			return;
9401 		}
9402 		chk->copy_by_ref = 0;
9403 		chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
9404 		chk->rec.chunk_id.can_take_data = 1;
9405 		chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9406 		chk->whoTo = net;
9407 		if (chk->whoTo) {
9408 			atomic_add_int(&chk->whoTo->ref_count, 1);
9409 		}
9410 		chk->data = m_ack;
9411 		chk->send_size = ack->len;
9412 		chk->sent = SCTP_DATAGRAM_UNSENT;
9413 		chk->snd_count = 0;
9414 		chk->asoc = &stcb->asoc;
9415 
9416 		TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9417 		chk->asoc->ctrl_queue_cnt++;
9418 	}
9419 	return;
9420 }
9421 
9422 static int
9423 sctp_chunk_retransmission(struct sctp_inpcb *inp,
9424     struct sctp_tcb *stcb,
9425     struct sctp_association *asoc,
9426     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked)
9427 {
9428 	/*-
9429 	 * send out one MTU of retransmission. If fast_retransmit is
9430 	 * happening we ignore the cwnd. Otherwise we obey the cwnd and
9431 	 * rwnd. For a Cookie or Asconf in the control chunk queue we
9432 	 * retransmit them by themselves.
9433 	 *
9434 	 * For data chunks we will pick out the lowest TSN's in the sent_queue
9435 	 * marked for resend and bundle them all together (up to a MTU of
9436 	 * destination). The address to send to should have been
9437 	 * selected/changed where the retransmission was marked (i.e. in FR
9438 	 * or t3-timeout routines).
9439 	 */
9440 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
9441 	struct sctp_tmit_chunk *chk, *fwd;
9442 	struct mbuf *m, *endofchain;
9443 	struct sctp_nets *net = NULL;
9444 	uint32_t tsns_sent = 0;
9445 	int no_fragmentflg, bundle_at;
9446 	unsigned int mtu;
9447 	int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
9448 	struct sctp_auth_chunk *auth = NULL;
9449 	uint32_t auth_offset = 0;
9450 	uint16_t auth_keyid;
9451 	int override_ok = 1;
9452 	int data_auth_reqd = 0;
9453 	uint32_t dmtu = 0;
9454 	bool use_zero_crc;
9455 
9456 	SCTP_TCB_LOCK_ASSERT(stcb);
9457 	tmr_started = ctl_cnt = 0;
9458 	no_fragmentflg = 1;
9459 	fwd_tsn = 0;
9460 	*cnt_out = 0;
9461 	fwd = NULL;
9462 	endofchain = m = NULL;
9463 	auth_keyid = stcb->asoc.authinfo.active_keyid;
9464 #ifdef SCTP_AUDITING_ENABLED
9465 	sctp_audit_log(0xC3, 1);
9466 #endif
9467 	if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
9468 	    (TAILQ_EMPTY(&asoc->control_send_queue))) {
9469 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
9470 		    asoc->sent_queue_retran_cnt);
9471 		asoc->sent_queue_cnt = 0;
9472 		asoc->sent_queue_cnt_removeable = 0;
9473 		/* send back 0/0 so we enter normal transmission */
9474 		*cnt_out = 0;
9475 		return (0);
9476 	}
9477 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9478 		if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
9479 		    (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
9480 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
9481 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
9482 				continue;
9483 			}
9484 			if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
9485 				if (chk != asoc->str_reset) {
9486 					/*
9487 					 * not eligible for retran if its
9488 					 * not ours
9489 					 */
9490 					continue;
9491 				}
9492 			}
9493 			ctl_cnt++;
9494 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9495 				fwd_tsn = 1;
9496 			}
9497 			/*
9498 			 * Add an AUTH chunk, if chunk requires it save the
9499 			 * offset into the chain for AUTH
9500 			 */
9501 			if ((auth == NULL) &&
9502 			    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9503 			    stcb->asoc.peer_auth_chunks))) {
9504 				m = sctp_add_auth_chunk(m, &endofchain,
9505 				    &auth, &auth_offset,
9506 				    stcb,
9507 				    chk->rec.chunk_id.id);
9508 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9509 			}
9510 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9511 			break;
9512 		}
9513 	}
9514 	one_chunk = 0;
9515 	/* do we have control chunks to retransmit? */
9516 	if (m != NULL) {
9517 		/* Start a timer no matter if we succeed or fail */
9518 		switch (asoc->snd_edmid) {
9519 		case SCTP_EDMID_LOWER_LAYER_DTLS:
9520 			use_zero_crc = true;
9521 			break;
9522 		default:
9523 			use_zero_crc = false;
9524 			break;
9525 		}
9526 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9527 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
9528 			use_zero_crc = false;
9529 		} else if (chk->rec.chunk_id.id == SCTP_ASCONF) {
9530 			/* XXXMT: Can this happen? */
9531 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
9532 			use_zero_crc = false;
9533 		}
9534 		chk->snd_count++;	/* update our count */
9535 		if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
9536 		    (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
9537 		    auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9538 		    no_fragmentflg, 0, 0,
9539 		    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9540 		    chk->whoTo->port, NULL,
9541 		    0, 0,
9542 		    use_zero_crc,
9543 		    so_locked))) {
9544 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
9545 			if (error == ENOBUFS) {
9546 				asoc->ifp_had_enobuf = 1;
9547 				SCTP_STAT_INCR(sctps_lowlevelerr);
9548 			}
9549 			return (error);
9550 		} else {
9551 			asoc->ifp_had_enobuf = 0;
9552 		}
9553 		endofchain = NULL;
9554 		auth = NULL;
9555 		auth_offset = 0;
9556 		/*
9557 		 * We don't want to mark the net->sent time here since this
9558 		 * we use this for HB and retrans cannot measure RTT
9559 		 */
9560 		/* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
9561 		*cnt_out += 1;
9562 		chk->sent = SCTP_DATAGRAM_SENT;
9563 		sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
9564 		if (fwd_tsn == 0) {
9565 			return (0);
9566 		} else {
9567 			/* Clean up the fwd-tsn list */
9568 			sctp_clean_up_ctl(stcb, asoc, so_locked);
9569 			return (0);
9570 		}
9571 	}
9572 	/*
9573 	 * Ok, it is just data retransmission we need to do or that and a
9574 	 * fwd-tsn with it all.
9575 	 */
9576 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
9577 		return (SCTP_RETRAN_DONE);
9578 	}
9579 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) ||
9580 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT)) {
9581 		/* not yet open, resend the cookie and that is it */
9582 		return (1);
9583 	}
9584 #ifdef SCTP_AUDITING_ENABLED
9585 	sctp_auditing(20, inp, stcb, NULL);
9586 #endif
9587 	data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9588 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9589 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
9590 			/* No, not sent to this net or not ready for rtx */
9591 			continue;
9592 		}
9593 		if (chk->data == NULL) {
9594 			SCTP_PRINTF("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
9595 			    chk->rec.data.tsn, chk->snd_count, chk->sent);
9596 			continue;
9597 		}
9598 		if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9599 		    (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9600 			struct mbuf *op_err;
9601 			char msg[SCTP_DIAG_INFO_LEN];
9602 
9603 			SCTP_SNPRINTF(msg, sizeof(msg), "TSN %8.8x retransmitted %d times, giving up",
9604 			    chk->rec.data.tsn, chk->snd_count);
9605 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
9606 			    msg);
9607 			atomic_add_int(&stcb->asoc.refcnt, 1);
9608 			sctp_abort_an_association(stcb->sctp_ep, stcb, op_err,
9609 			    false, so_locked);
9610 			SCTP_TCB_LOCK(stcb);
9611 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
9612 			return (SCTP_RETRAN_EXIT);
9613 		}
9614 		/* pick up the net */
9615 		net = chk->whoTo;
9616 		switch (net->ro._l_addr.sa.sa_family) {
9617 #ifdef INET
9618 		case AF_INET:
9619 			mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9620 			break;
9621 #endif
9622 #ifdef INET6
9623 		case AF_INET6:
9624 			mtu = net->mtu - SCTP_MIN_OVERHEAD;
9625 			break;
9626 #endif
9627 		default:
9628 			/* TSNH */
9629 			mtu = net->mtu;
9630 			break;
9631 		}
9632 
9633 		if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9634 			/* No room in peers rwnd */
9635 			uint32_t tsn;
9636 
9637 			tsn = asoc->last_acked_seq + 1;
9638 			if (tsn == chk->rec.data.tsn) {
9639 				/*
9640 				 * we make a special exception for this
9641 				 * case. The peer has no rwnd but is missing
9642 				 * the lowest chunk.. which is probably what
9643 				 * is holding up the rwnd.
9644 				 */
9645 				goto one_chunk_around;
9646 			}
9647 			return (1);
9648 		}
9649 one_chunk_around:
9650 		if (asoc->peers_rwnd < mtu) {
9651 			one_chunk = 1;
9652 			if ((asoc->peers_rwnd == 0) &&
9653 			    (asoc->total_flight == 0)) {
9654 				chk->window_probe = 1;
9655 				chk->whoTo->window_probe = 1;
9656 			}
9657 		}
9658 #ifdef SCTP_AUDITING_ENABLED
9659 		sctp_audit_log(0xC3, 2);
9660 #endif
9661 		bundle_at = 0;
9662 		m = NULL;
9663 		net->fast_retran_ip = 0;
9664 		if (chk->rec.data.doing_fast_retransmit == 0) {
9665 			/*
9666 			 * if no FR in progress skip destination that have
9667 			 * flight_size > cwnd.
9668 			 */
9669 			if (net->flight_size >= net->cwnd) {
9670 				continue;
9671 			}
9672 		} else {
9673 			/*
9674 			 * Mark the destination net to have FR recovery
9675 			 * limits put on it.
9676 			 */
9677 			*fr_done = 1;
9678 			net->fast_retran_ip = 1;
9679 		}
9680 
9681 		/*
9682 		 * if no AUTH is yet included and this chunk requires it,
9683 		 * make sure to account for it.  We don't apply the size
9684 		 * until the AUTH chunk is actually added below in case
9685 		 * there is no room for this chunk.
9686 		 */
9687 		if (data_auth_reqd && (auth == NULL)) {
9688 			dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9689 		} else
9690 			dmtu = 0;
9691 
9692 		if ((chk->send_size <= (mtu - dmtu)) ||
9693 		    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9694 			/* ok we will add this one */
9695 			if (data_auth_reqd) {
9696 				if (auth == NULL) {
9697 					m = sctp_add_auth_chunk(m,
9698 					    &endofchain,
9699 					    &auth,
9700 					    &auth_offset,
9701 					    stcb,
9702 					    SCTP_DATA);
9703 					auth_keyid = chk->auth_keyid;
9704 					override_ok = 0;
9705 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9706 				} else if (override_ok) {
9707 					auth_keyid = chk->auth_keyid;
9708 					override_ok = 0;
9709 				} else if (chk->auth_keyid != auth_keyid) {
9710 					/* different keyid, so done bundling */
9711 					break;
9712 				}
9713 			}
9714 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9715 			if (m == NULL) {
9716 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9717 				return (ENOMEM);
9718 			}
9719 			/* Do clear IP_DF ? */
9720 			if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9721 				no_fragmentflg = 0;
9722 			}
9723 			/* update our MTU size */
9724 			if (mtu > (chk->send_size + dmtu))
9725 				mtu -= (chk->send_size + dmtu);
9726 			else
9727 				mtu = 0;
9728 			data_list[bundle_at++] = chk;
9729 			if (one_chunk && (asoc->total_flight <= 0)) {
9730 				SCTP_STAT_INCR(sctps_windowprobed);
9731 			}
9732 		}
9733 		if (one_chunk == 0) {
9734 			/*
9735 			 * now are there anymore forward from chk to pick
9736 			 * up?
9737 			 */
9738 			for (fwd = TAILQ_NEXT(chk, sctp_next); fwd != NULL; fwd = TAILQ_NEXT(fwd, sctp_next)) {
9739 				if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9740 					/* Nope, not for retran */
9741 					continue;
9742 				}
9743 				if (fwd->whoTo != net) {
9744 					/* Nope, not the net in question */
9745 					continue;
9746 				}
9747 				if (data_auth_reqd && (auth == NULL)) {
9748 					dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9749 				} else
9750 					dmtu = 0;
9751 				if (fwd->send_size <= (mtu - dmtu)) {
9752 					if (data_auth_reqd) {
9753 						if (auth == NULL) {
9754 							m = sctp_add_auth_chunk(m,
9755 							    &endofchain,
9756 							    &auth,
9757 							    &auth_offset,
9758 							    stcb,
9759 							    SCTP_DATA);
9760 							auth_keyid = fwd->auth_keyid;
9761 							override_ok = 0;
9762 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9763 						} else if (override_ok) {
9764 							auth_keyid = fwd->auth_keyid;
9765 							override_ok = 0;
9766 						} else if (fwd->auth_keyid != auth_keyid) {
9767 							/*
9768 							 * different keyid,
9769 							 * so done bundling
9770 							 */
9771 							break;
9772 						}
9773 					}
9774 					m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9775 					if (m == NULL) {
9776 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9777 						return (ENOMEM);
9778 					}
9779 					/* Do clear IP_DF ? */
9780 					if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9781 						no_fragmentflg = 0;
9782 					}
9783 					/* update our MTU size */
9784 					if (mtu > (fwd->send_size + dmtu))
9785 						mtu -= (fwd->send_size + dmtu);
9786 					else
9787 						mtu = 0;
9788 					data_list[bundle_at++] = fwd;
9789 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9790 						break;
9791 					}
9792 				} else {
9793 					/* can't fit so we are done */
9794 					break;
9795 				}
9796 			}
9797 		}
9798 		/* Is there something to send for this destination? */
9799 		if (m) {
9800 			/*
9801 			 * No matter if we fail/or succeed we should start a
9802 			 * timer. A failure is like a lost IP packet :-)
9803 			 */
9804 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9805 				/*
9806 				 * no timer running on this destination
9807 				 * restart it.
9808 				 */
9809 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9810 				tmr_started = 1;
9811 			}
9812 			switch (asoc->snd_edmid) {
9813 			case SCTP_EDMID_LOWER_LAYER_DTLS:
9814 				use_zero_crc = true;
9815 				break;
9816 			default:
9817 				use_zero_crc = false;
9818 				break;
9819 			}
9820 			/* Now lets send it, if there is anything to send :> */
9821 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9822 			    (struct sockaddr *)&net->ro._l_addr, m,
9823 			    auth_offset, auth, auth_keyid,
9824 			    no_fragmentflg, 0, 0,
9825 			    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9826 			    net->port, NULL,
9827 			    0, 0,
9828 			    use_zero_crc,
9829 			    so_locked))) {
9830 				/* error, we could not output */
9831 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
9832 				if (error == ENOBUFS) {
9833 					asoc->ifp_had_enobuf = 1;
9834 					SCTP_STAT_INCR(sctps_lowlevelerr);
9835 				}
9836 				return (error);
9837 			} else {
9838 				asoc->ifp_had_enobuf = 0;
9839 			}
9840 			endofchain = NULL;
9841 			auth = NULL;
9842 			auth_offset = 0;
9843 			/* For HB's */
9844 			/*
9845 			 * We don't want to mark the net->sent time here
9846 			 * since this we use this for HB and retrans cannot
9847 			 * measure RTT
9848 			 */
9849 			/* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9850 
9851 			/* For auto-close */
9852 			if (*now_filled == 0) {
9853 				(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9854 				*now = asoc->time_last_sent;
9855 				*now_filled = 1;
9856 			} else {
9857 				asoc->time_last_sent = *now;
9858 			}
9859 			*cnt_out += bundle_at;
9860 #ifdef SCTP_AUDITING_ENABLED
9861 			sctp_audit_log(0xC4, bundle_at);
9862 #endif
9863 			if (bundle_at) {
9864 				tsns_sent = data_list[0]->rec.data.tsn;
9865 			}
9866 			for (i = 0; i < bundle_at; i++) {
9867 				SCTP_STAT_INCR(sctps_sendretransdata);
9868 				data_list[i]->sent = SCTP_DATAGRAM_SENT;
9869 				/*
9870 				 * When we have a revoked data, and we
9871 				 * retransmit it, then we clear the revoked
9872 				 * flag since this flag dictates if we
9873 				 * subtracted from the fs
9874 				 */
9875 				if (data_list[i]->rec.data.chunk_was_revoked) {
9876 					/* Deflate the cwnd */
9877 					data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9878 					data_list[i]->rec.data.chunk_was_revoked = 0;
9879 				}
9880 				data_list[i]->snd_count++;
9881 				sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9882 				/* record the time */
9883 				data_list[i]->sent_rcv_time = asoc->time_last_sent;
9884 				if (data_list[i]->book_size_scale) {
9885 					/*
9886 					 * need to double the book size on
9887 					 * this one
9888 					 */
9889 					data_list[i]->book_size_scale = 0;
9890 					/*
9891 					 * Since we double the booksize, we
9892 					 * must also double the output queue
9893 					 * size, since this get shrunk when
9894 					 * we free by this amount.
9895 					 */
9896 					atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9897 					data_list[i]->book_size *= 2;
9898 				} else {
9899 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9900 						sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9901 						    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9902 					}
9903 					asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9904 					    (uint32_t)(data_list[i]->send_size +
9905 					    SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9906 				}
9907 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9908 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9909 					    data_list[i]->whoTo->flight_size,
9910 					    data_list[i]->book_size,
9911 					    (uint32_t)(uintptr_t)data_list[i]->whoTo,
9912 					    data_list[i]->rec.data.tsn);
9913 				}
9914 				sctp_flight_size_increase(data_list[i]);
9915 				sctp_total_flight_increase(stcb, data_list[i]);
9916 				if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9917 					/* SWS sender side engages */
9918 					asoc->peers_rwnd = 0;
9919 				}
9920 				if ((i == 0) &&
9921 				    (data_list[i]->rec.data.doing_fast_retransmit)) {
9922 					SCTP_STAT_INCR(sctps_sendfastretrans);
9923 					if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9924 					    (tmr_started == 0)) {
9925 						/*-
9926 						 * ok we just fast-retrans'd
9927 						 * the lowest TSN, i.e the
9928 						 * first on the list. In
9929 						 * this case we want to give
9930 						 * some more time to get a
9931 						 * SACK back without a
9932 						 * t3-expiring.
9933 						 */
9934 						sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9935 						    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
9936 						sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9937 					}
9938 				}
9939 			}
9940 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9941 				sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9942 			}
9943 #ifdef SCTP_AUDITING_ENABLED
9944 			sctp_auditing(21, inp, stcb, NULL);
9945 #endif
9946 		} else {
9947 			/* None will fit */
9948 			return (1);
9949 		}
9950 		if (asoc->sent_queue_retran_cnt <= 0) {
9951 			/* all done we have no more to retran */
9952 			asoc->sent_queue_retran_cnt = 0;
9953 			break;
9954 		}
9955 		if (one_chunk) {
9956 			/* No more room in rwnd */
9957 			return (1);
9958 		}
9959 		/* stop the for loop here. we sent out a packet */
9960 		break;
9961 	}
9962 	return (0);
9963 }
9964 
9965 static void
9966 sctp_timer_validation(struct sctp_inpcb *inp,
9967     struct sctp_tcb *stcb,
9968     struct sctp_association *asoc)
9969 {
9970 	struct sctp_nets *net;
9971 
9972 	/* Validate that a timer is running somewhere */
9973 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9974 		if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9975 			/* Here is a timer */
9976 			return;
9977 		}
9978 	}
9979 	SCTP_TCB_LOCK_ASSERT(stcb);
9980 	/* Gak, we did not have a timer somewhere */
9981 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9982 	if (asoc->alternate) {
9983 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->alternate);
9984 	} else {
9985 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9986 	}
9987 	return;
9988 }
9989 
9990 void
9991 sctp_chunk_output(struct sctp_inpcb *inp,
9992     struct sctp_tcb *stcb,
9993     int from_where,
9994     int so_locked)
9995 {
9996 	/*-
9997 	 * Ok this is the generic chunk service queue. we must do the
9998 	 * following:
9999 	 * - See if there are retransmits pending, if so we must
10000 	 *   do these first.
10001 	 * - Service the stream queue that is next, moving any
10002 	 *   message (note I must get a complete message i.e.
10003 	 *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
10004 	 *   TSN's
10005 	 * - Check to see if the cwnd/rwnd allows any output, if so we
10006 	 *   go ahead and formulate and send the low level chunks. Making sure
10007 	 *   to combine any control in the control chunk queue also.
10008 	 */
10009 	struct sctp_association *asoc;
10010 	struct sctp_nets *net;
10011 	int error = 0, num_out, tot_out = 0, ret = 0, reason_code;
10012 	unsigned int burst_cnt = 0;
10013 	struct timeval now;
10014 	int now_filled = 0;
10015 	int nagle_on;
10016 	uint32_t frag_point = sctp_get_frag_point(stcb);
10017 	int un_sent = 0;
10018 	int fr_done;
10019 	unsigned int tot_frs = 0;
10020 
10021 	asoc = &stcb->asoc;
10022 do_it_again:
10023 	/* The Nagle algorithm is only applied when handling a send call. */
10024 	if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
10025 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
10026 			nagle_on = 0;
10027 		} else {
10028 			nagle_on = 1;
10029 		}
10030 	} else {
10031 		nagle_on = 0;
10032 	}
10033 	SCTP_TCB_LOCK_ASSERT(stcb);
10034 
10035 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
10036 
10037 	if ((un_sent <= 0) &&
10038 	    (TAILQ_EMPTY(&asoc->control_send_queue)) &&
10039 	    (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
10040 	    (asoc->sent_queue_retran_cnt == 0) &&
10041 	    (asoc->trigger_reset == 0)) {
10042 		/* Nothing to do unless there is something to be sent left */
10043 		return;
10044 	}
10045 	/*
10046 	 * Do we have something to send, data or control AND a sack timer
10047 	 * running, if so piggy-back the sack.
10048 	 */
10049 	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
10050 		sctp_send_sack(stcb, so_locked);
10051 		sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
10052 		    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
10053 	}
10054 	while (asoc->sent_queue_retran_cnt) {
10055 		/*-
10056 		 * Ok, it is retransmission time only, we send out only ONE
10057 		 * packet with a single call off to the retran code.
10058 		 */
10059 		if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
10060 			/*-
10061 			 * Special hook for handling cookies discarded
10062 			 * by peer that carried data. Send cookie-ack only
10063 			 * and then the next call with get the retran's.
10064 			 */
10065 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
10066 			    from_where,
10067 			    &now, &now_filled, frag_point, so_locked);
10068 			return;
10069 		} else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
10070 			/* if its not from a HB then do it */
10071 			fr_done = 0;
10072 			ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
10073 			if (fr_done) {
10074 				tot_frs++;
10075 			}
10076 		} else {
10077 			/*
10078 			 * its from any other place, we don't allow retran
10079 			 * output (only control)
10080 			 */
10081 			ret = 1;
10082 		}
10083 		if (ret > 0) {
10084 			/* Can't send anymore */
10085 			/*-
10086 			 * now lets push out control by calling med-level
10087 			 * output once. this assures that we WILL send HB's
10088 			 * if queued too.
10089 			 */
10090 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
10091 			    from_where,
10092 			    &now, &now_filled, frag_point, so_locked);
10093 #ifdef SCTP_AUDITING_ENABLED
10094 			sctp_auditing(8, inp, stcb, NULL);
10095 #endif
10096 			sctp_timer_validation(inp, stcb, asoc);
10097 			return;
10098 		}
10099 		if (ret < 0) {
10100 			/*-
10101 			 * The count was off.. retran is not happening so do
10102 			 * the normal retransmission.
10103 			 */
10104 #ifdef SCTP_AUDITING_ENABLED
10105 			sctp_auditing(9, inp, stcb, NULL);
10106 #endif
10107 			if (ret == SCTP_RETRAN_EXIT) {
10108 				return;
10109 			}
10110 			break;
10111 		}
10112 		if (from_where == SCTP_OUTPUT_FROM_T3) {
10113 			/* Only one transmission allowed out of a timeout */
10114 #ifdef SCTP_AUDITING_ENABLED
10115 			sctp_auditing(10, inp, stcb, NULL);
10116 #endif
10117 			/* Push out any control */
10118 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
10119 			    &now, &now_filled, frag_point, so_locked);
10120 			return;
10121 		}
10122 		if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) {
10123 			/* Hit FR burst limit */
10124 			return;
10125 		}
10126 		if ((num_out == 0) && (ret == 0)) {
10127 			/* No more retrans to send */
10128 			break;
10129 		}
10130 	}
10131 #ifdef SCTP_AUDITING_ENABLED
10132 	sctp_auditing(12, inp, stcb, NULL);
10133 #endif
10134 	/* Check for bad destinations, if they exist move chunks around. */
10135 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
10136 		if ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) {
10137 			/*-
10138 			 * if possible move things off of this address we
10139 			 * still may send below due to the dormant state but
10140 			 * we try to find an alternate address to send to
10141 			 * and if we have one we move all queued data on the
10142 			 * out wheel to this alternate address.
10143 			 */
10144 			if (net->ref_count > 1)
10145 				sctp_move_chunks_from_net(stcb, net);
10146 		} else {
10147 			/*-
10148 			 * if ((asoc->sat_network) || (net->addr_is_local))
10149 			 * { burst_limit = asoc->max_burst *
10150 			 * SCTP_SAT_NETWORK_BURST_INCR; }
10151 			 */
10152 			if (asoc->max_burst > 0) {
10153 				if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
10154 					if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) {
10155 						/*
10156 						 * JRS - Use the congestion
10157 						 * control given in the
10158 						 * congestion control module
10159 						 */
10160 						asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst);
10161 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10162 							sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED);
10163 						}
10164 						SCTP_STAT_INCR(sctps_maxburstqueued);
10165 					}
10166 					net->fast_retran_ip = 0;
10167 				} else {
10168 					if (net->flight_size == 0) {
10169 						/*
10170 						 * Should be decaying the
10171 						 * cwnd here
10172 						 */
10173 						;
10174 					}
10175 				}
10176 			}
10177 		}
10178 	}
10179 	burst_cnt = 0;
10180 	do {
10181 		error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
10182 		    &reason_code, 0, from_where,
10183 		    &now, &now_filled, frag_point, so_locked);
10184 		if (error) {
10185 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
10186 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10187 				sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
10188 			}
10189 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10190 				sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
10191 				sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
10192 			}
10193 			break;
10194 		}
10195 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
10196 
10197 		tot_out += num_out;
10198 		burst_cnt++;
10199 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10200 			sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
10201 			if (num_out == 0) {
10202 				sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
10203 			}
10204 		}
10205 		if (nagle_on) {
10206 			/*
10207 			 * When the Nagle algorithm is used, look at how
10208 			 * much is unsent, then if its smaller than an MTU
10209 			 * and we have data in flight we stop, except if we
10210 			 * are handling a fragmented user message.
10211 			 */
10212 			un_sent = stcb->asoc.total_output_queue_size - stcb->asoc.total_flight;
10213 			if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
10214 			    (stcb->asoc.total_flight > 0)) {
10215 /*	&&		     sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {*/
10216 				break;
10217 			}
10218 		}
10219 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
10220 		    TAILQ_EMPTY(&asoc->send_queue) &&
10221 		    sctp_is_there_unsent_data(stcb, so_locked) == 0) {
10222 			/* Nothing left to send */
10223 			break;
10224 		}
10225 		if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
10226 			/* Nothing left to send */
10227 			break;
10228 		}
10229 	} while (num_out &&
10230 	    ((asoc->max_burst == 0) ||
10231 	    SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
10232 	    (burst_cnt < asoc->max_burst)));
10233 
10234 	if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
10235 		if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) {
10236 			SCTP_STAT_INCR(sctps_maxburstqueued);
10237 			asoc->burst_limit_applied = 1;
10238 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10239 				sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
10240 			}
10241 		} else {
10242 			asoc->burst_limit_applied = 0;
10243 		}
10244 	}
10245 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10246 		sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
10247 	}
10248 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
10249 	    tot_out);
10250 
10251 	/*-
10252 	 * Now we need to clean up the control chunk chain if a ECNE is on
10253 	 * it. It must be marked as UNSENT again so next call will continue
10254 	 * to send it until such time that we get a CWR, to remove it.
10255 	 */
10256 	if (stcb->asoc.ecn_echo_cnt_onq)
10257 		sctp_fix_ecn_echo(asoc);
10258 
10259 	if (stcb->asoc.trigger_reset) {
10260 		if (sctp_send_stream_reset_out_if_possible(stcb, so_locked) == 0) {
10261 			goto do_it_again;
10262 		}
10263 	}
10264 	return;
10265 }
10266 
10267 int
10268 sctp_output(
10269     struct sctp_inpcb *inp,
10270     struct mbuf *m,
10271     struct sockaddr *addr,
10272     struct mbuf *control,
10273     struct thread *p,
10274     int flags)
10275 {
10276 	if (inp == NULL) {
10277 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10278 		return (EINVAL);
10279 	}
10280 
10281 	if (inp->sctp_socket == NULL) {
10282 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10283 		return (EINVAL);
10284 	}
10285 	return (sctp_sosend(inp->sctp_socket,
10286 	    addr,
10287 	    (struct uio *)NULL,
10288 	    m,
10289 	    control,
10290 	    flags, p
10291 	    ));
10292 }
10293 
10294 void
10295 send_forward_tsn(struct sctp_tcb *stcb,
10296     struct sctp_association *asoc)
10297 {
10298 	struct sctp_tmit_chunk *chk, *at, *tp1, *last;
10299 	struct sctp_forward_tsn_chunk *fwdtsn;
10300 	struct sctp_strseq *strseq;
10301 	struct sctp_strseq_mid *strseq_m;
10302 	uint32_t advance_peer_ack_point;
10303 	unsigned int cnt_of_space, i, ovh;
10304 	unsigned int space_needed;
10305 	unsigned int cnt_of_skipped = 0;
10306 
10307 	SCTP_TCB_LOCK_ASSERT(stcb);
10308 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10309 		if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
10310 			/* mark it to unsent */
10311 			chk->sent = SCTP_DATAGRAM_UNSENT;
10312 			chk->snd_count = 0;
10313 			/* Do we correct its output location? */
10314 			if (chk->whoTo) {
10315 				sctp_free_remote_addr(chk->whoTo);
10316 				chk->whoTo = NULL;
10317 			}
10318 			goto sctp_fill_in_rest;
10319 		}
10320 	}
10321 	/* Ok if we reach here we must build one */
10322 	sctp_alloc_a_chunk(stcb, chk);
10323 	if (chk == NULL) {
10324 		return;
10325 	}
10326 	asoc->fwd_tsn_cnt++;
10327 	chk->copy_by_ref = 0;
10328 	/*
10329 	 * We don't do the old thing here since this is used not for on-wire
10330 	 * but to tell if we are sending a fwd-tsn by the stack during
10331 	 * output. And if its a IFORWARD or a FORWARD it is a fwd-tsn.
10332 	 */
10333 	chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
10334 	chk->rec.chunk_id.can_take_data = 0;
10335 	chk->flags = 0;
10336 	chk->asoc = asoc;
10337 	chk->whoTo = NULL;
10338 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
10339 	if (chk->data == NULL) {
10340 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
10341 		return;
10342 	}
10343 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10344 	chk->sent = SCTP_DATAGRAM_UNSENT;
10345 	chk->snd_count = 0;
10346 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
10347 	asoc->ctrl_queue_cnt++;
10348 sctp_fill_in_rest:
10349 	/*-
10350 	 * Here we go through and fill out the part that deals with
10351 	 * stream/seq of the ones we skip.
10352 	 */
10353 	SCTP_BUF_LEN(chk->data) = 0;
10354 	TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
10355 		if ((at->sent != SCTP_FORWARD_TSN_SKIP) &&
10356 		    (at->sent != SCTP_DATAGRAM_NR_ACKED)) {
10357 			/* no more to look at */
10358 			break;
10359 		}
10360 		if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
10361 			/* We don't report these */
10362 			continue;
10363 		}
10364 		cnt_of_skipped++;
10365 	}
10366 	if (asoc->idata_supported) {
10367 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
10368 		    (cnt_of_skipped * sizeof(struct sctp_strseq_mid)));
10369 	} else {
10370 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
10371 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
10372 	}
10373 	cnt_of_space = (unsigned int)M_TRAILINGSPACE(chk->data);
10374 
10375 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
10376 		ovh = SCTP_MIN_OVERHEAD;
10377 	} else {
10378 		ovh = SCTP_MIN_V4_OVERHEAD;
10379 	}
10380 	if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
10381 		/* trim to a mtu size */
10382 		cnt_of_space = asoc->smallest_mtu - ovh;
10383 	}
10384 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10385 		sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10386 		    0xff, 0, cnt_of_skipped,
10387 		    asoc->advanced_peer_ack_point);
10388 	}
10389 	advance_peer_ack_point = asoc->advanced_peer_ack_point;
10390 	if (cnt_of_space < space_needed) {
10391 		/*-
10392 		 * ok we must trim down the chunk by lowering the
10393 		 * advance peer ack point.
10394 		 */
10395 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10396 			sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10397 			    0xff, 0xff, cnt_of_space,
10398 			    space_needed);
10399 		}
10400 		cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
10401 		if (asoc->idata_supported) {
10402 			cnt_of_skipped /= sizeof(struct sctp_strseq_mid);
10403 		} else {
10404 			cnt_of_skipped /= sizeof(struct sctp_strseq);
10405 		}
10406 		/*-
10407 		 * Go through and find the TSN that will be the one
10408 		 * we report.
10409 		 */
10410 		at = TAILQ_FIRST(&asoc->sent_queue);
10411 		if (at != NULL) {
10412 			for (i = 0; i < cnt_of_skipped; i++) {
10413 				tp1 = TAILQ_NEXT(at, sctp_next);
10414 				if (tp1 == NULL) {
10415 					break;
10416 				}
10417 				at = tp1;
10418 			}
10419 		}
10420 		if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10421 			sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10422 			    0xff, cnt_of_skipped, at->rec.data.tsn,
10423 			    asoc->advanced_peer_ack_point);
10424 		}
10425 		last = at;
10426 		/*-
10427 		 * last now points to last one I can report, update
10428 		 * peer ack point
10429 		 */
10430 		if (last) {
10431 			advance_peer_ack_point = last->rec.data.tsn;
10432 		}
10433 		if (asoc->idata_supported) {
10434 			space_needed = sizeof(struct sctp_forward_tsn_chunk) +
10435 			    cnt_of_skipped * sizeof(struct sctp_strseq_mid);
10436 		} else {
10437 			space_needed = sizeof(struct sctp_forward_tsn_chunk) +
10438 			    cnt_of_skipped * sizeof(struct sctp_strseq);
10439 		}
10440 	}
10441 	chk->send_size = space_needed;
10442 	/* Setup the chunk */
10443 	fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
10444 	fwdtsn->ch.chunk_length = htons(chk->send_size);
10445 	fwdtsn->ch.chunk_flags = 0;
10446 	if (asoc->idata_supported) {
10447 		fwdtsn->ch.chunk_type = SCTP_IFORWARD_CUM_TSN;
10448 	} else {
10449 		fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
10450 	}
10451 	fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
10452 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10453 	fwdtsn++;
10454 	/*-
10455 	 * Move pointer to after the fwdtsn and transfer to the
10456 	 * strseq pointer.
10457 	 */
10458 	if (asoc->idata_supported) {
10459 		strseq_m = (struct sctp_strseq_mid *)fwdtsn;
10460 		strseq = NULL;
10461 	} else {
10462 		strseq = (struct sctp_strseq *)fwdtsn;
10463 		strseq_m = NULL;
10464 	}
10465 	/*-
10466 	 * Now populate the strseq list. This is done blindly
10467 	 * without pulling out duplicate stream info. This is
10468 	 * inefficient but won't harm the process since the peer will
10469 	 * look at these in sequence and will thus release anything.
10470 	 * It could mean we exceed the PMTU and chop off some that
10471 	 * we could have included.. but this is unlikely (aka 1432/4
10472 	 * would mean 300+ stream seq's would have to be reported in
10473 	 * one FWD-TSN. With a bit of work we can later FIX this to
10474 	 * optimize and pull out duplicates.. but it does add more
10475 	 * overhead. So for now... not!
10476 	 */
10477 	i = 0;
10478 	TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
10479 		if (i >= cnt_of_skipped) {
10480 			break;
10481 		}
10482 		if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
10483 			/* We don't report these */
10484 			continue;
10485 		}
10486 		if (at->rec.data.tsn == advance_peer_ack_point) {
10487 			at->rec.data.fwd_tsn_cnt = 0;
10488 		}
10489 		if (asoc->idata_supported) {
10490 			strseq_m->sid = htons(at->rec.data.sid);
10491 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
10492 				strseq_m->flags = htons(PR_SCTP_UNORDERED_FLAG);
10493 			} else {
10494 				strseq_m->flags = 0;
10495 			}
10496 			strseq_m->mid = htonl(at->rec.data.mid);
10497 			strseq_m++;
10498 		} else {
10499 			strseq->sid = htons(at->rec.data.sid);
10500 			strseq->ssn = htons((uint16_t)at->rec.data.mid);
10501 			strseq++;
10502 		}
10503 		i++;
10504 	}
10505 	return;
10506 }
10507 
10508 void
10509 sctp_send_sack(struct sctp_tcb *stcb, int so_locked)
10510 {
10511 	/*-
10512 	 * Queue up a SACK or NR-SACK in the control queue.
10513 	 * We must first check to see if a SACK or NR-SACK is
10514 	 * somehow on the control queue.
10515 	 * If so, we will take and and remove the old one.
10516 	 */
10517 	struct sctp_association *asoc;
10518 	struct sctp_tmit_chunk *chk, *a_chk;
10519 	struct sctp_sack_chunk *sack;
10520 	struct sctp_nr_sack_chunk *nr_sack;
10521 	struct sctp_gap_ack_block *gap_descriptor;
10522 	const struct sack_track *selector;
10523 	int mergeable = 0;
10524 	int offset;
10525 	caddr_t limit;
10526 	uint32_t *dup;
10527 	int limit_reached = 0;
10528 	unsigned int i, siz, j;
10529 	unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
10530 	int num_dups = 0;
10531 	int space_req;
10532 	uint32_t highest_tsn;
10533 	uint8_t flags;
10534 	uint8_t type;
10535 	uint8_t tsn_map;
10536 
10537 	if (stcb->asoc.nrsack_supported == 1) {
10538 		type = SCTP_NR_SELECTIVE_ACK;
10539 	} else {
10540 		type = SCTP_SELECTIVE_ACK;
10541 	}
10542 	a_chk = NULL;
10543 	asoc = &stcb->asoc;
10544 	SCTP_TCB_LOCK_ASSERT(stcb);
10545 	if (asoc->last_data_chunk_from == NULL) {
10546 		/* Hmm we never received anything */
10547 		return;
10548 	}
10549 	sctp_slide_mapping_arrays(stcb);
10550 	sctp_set_rwnd(stcb, asoc);
10551 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10552 		if (chk->rec.chunk_id.id == type) {
10553 			/* Hmm, found a sack already on queue, remove it */
10554 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
10555 			asoc->ctrl_queue_cnt--;
10556 			a_chk = chk;
10557 			if (a_chk->data) {
10558 				sctp_m_freem(a_chk->data);
10559 				a_chk->data = NULL;
10560 			}
10561 			if (a_chk->whoTo) {
10562 				sctp_free_remote_addr(a_chk->whoTo);
10563 				a_chk->whoTo = NULL;
10564 			}
10565 			break;
10566 		}
10567 	}
10568 	if (a_chk == NULL) {
10569 		sctp_alloc_a_chunk(stcb, a_chk);
10570 		if (a_chk == NULL) {
10571 			/* No memory so we drop the idea, and set a timer */
10572 			if (stcb->asoc.delayed_ack) {
10573 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10574 				    stcb->sctp_ep, stcb, NULL,
10575 				    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
10576 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10577 				    stcb->sctp_ep, stcb, NULL);
10578 			} else {
10579 				stcb->asoc.send_sack = 1;
10580 			}
10581 			return;
10582 		}
10583 		a_chk->copy_by_ref = 0;
10584 		a_chk->rec.chunk_id.id = type;
10585 		a_chk->rec.chunk_id.can_take_data = 1;
10586 	}
10587 	/* Clear our pkt counts */
10588 	asoc->data_pkts_seen = 0;
10589 
10590 	a_chk->flags = 0;
10591 	a_chk->asoc = asoc;
10592 	a_chk->snd_count = 0;
10593 	a_chk->send_size = 0;	/* fill in later */
10594 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
10595 	a_chk->whoTo = NULL;
10596 
10597 	if ((asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE) == 0) {
10598 		/*-
10599 		 * Ok, the destination for the SACK is unreachable, lets see if
10600 		 * we can select an alternate to asoc->last_data_chunk_from
10601 		 */
10602 		a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
10603 		if (a_chk->whoTo == NULL) {
10604 			/* Nope, no alternate */
10605 			a_chk->whoTo = asoc->last_data_chunk_from;
10606 		}
10607 	} else {
10608 		a_chk->whoTo = asoc->last_data_chunk_from;
10609 	}
10610 	if (a_chk->whoTo) {
10611 		atomic_add_int(&a_chk->whoTo->ref_count, 1);
10612 	}
10613 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) {
10614 		highest_tsn = asoc->highest_tsn_inside_map;
10615 	} else {
10616 		highest_tsn = asoc->highest_tsn_inside_nr_map;
10617 	}
10618 	if (highest_tsn == asoc->cumulative_tsn) {
10619 		/* no gaps */
10620 		if (type == SCTP_SELECTIVE_ACK) {
10621 			space_req = sizeof(struct sctp_sack_chunk);
10622 		} else {
10623 			space_req = sizeof(struct sctp_nr_sack_chunk);
10624 		}
10625 	} else {
10626 		/* gaps get a cluster */
10627 		space_req = MCLBYTES;
10628 	}
10629 	/* Ok now lets formulate a MBUF with our sack */
10630 	a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_NOWAIT, 1, MT_DATA);
10631 	if ((a_chk->data == NULL) ||
10632 	    (a_chk->whoTo == NULL)) {
10633 		/* rats, no mbuf memory */
10634 		if (a_chk->data) {
10635 			/* was a problem with the destination */
10636 			sctp_m_freem(a_chk->data);
10637 			a_chk->data = NULL;
10638 		}
10639 		sctp_free_a_chunk(stcb, a_chk, so_locked);
10640 		/* sa_ignore NO_NULL_CHK */
10641 		if (stcb->asoc.delayed_ack) {
10642 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10643 			    stcb->sctp_ep, stcb, NULL,
10644 			    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
10645 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10646 			    stcb->sctp_ep, stcb, NULL);
10647 		} else {
10648 			stcb->asoc.send_sack = 1;
10649 		}
10650 		return;
10651 	}
10652 	/* ok, lets go through and fill it in */
10653 	SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10654 	space = (unsigned int)M_TRAILINGSPACE(a_chk->data);
10655 	if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10656 		space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10657 	}
10658 	limit = mtod(a_chk->data, caddr_t);
10659 	limit += space;
10660 
10661 	flags = 0;
10662 
10663 	if ((asoc->sctp_cmt_on_off > 0) &&
10664 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10665 		/*-
10666 		 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10667 		 * received, then set high bit to 1, else 0. Reset
10668 		 * pkts_rcvd.
10669 		 */
10670 		flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10671 		asoc->cmt_dac_pkts_rcvd = 0;
10672 	}
10673 #ifdef SCTP_ASOCLOG_OF_TSNS
10674 	stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10675 	stcb->asoc.cumack_log_atsnt++;
10676 	if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10677 		stcb->asoc.cumack_log_atsnt = 0;
10678 	}
10679 #endif
10680 	/* reset the readers interpretation */
10681 	stcb->freed_by_sorcv_sincelast = 0;
10682 
10683 	if (type == SCTP_SELECTIVE_ACK) {
10684 		sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10685 		nr_sack = NULL;
10686 		gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10687 		if (highest_tsn > asoc->mapping_array_base_tsn) {
10688 			siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10689 		} else {
10690 			siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + highest_tsn + 7) / 8;
10691 		}
10692 	} else {
10693 		sack = NULL;
10694 		nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10695 		gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10696 		if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
10697 			siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10698 		} else {
10699 			siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10700 		}
10701 	}
10702 
10703 	if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10704 		offset = 1;
10705 	} else {
10706 		offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10707 	}
10708 	if (((type == SCTP_SELECTIVE_ACK) &&
10709 	    SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) ||
10710 	    ((type == SCTP_NR_SELECTIVE_ACK) &&
10711 	    SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) {
10712 		/* we have a gap .. maybe */
10713 		for (i = 0; i < siz; i++) {
10714 			tsn_map = asoc->mapping_array[i];
10715 			if (type == SCTP_SELECTIVE_ACK) {
10716 				tsn_map |= asoc->nr_mapping_array[i];
10717 			}
10718 			if (i == 0) {
10719 				/*
10720 				 * Clear all bits corresponding to TSNs
10721 				 * smaller or equal to the cumulative TSN.
10722 				 */
10723 				tsn_map &= (~0U << (1 - offset));
10724 			}
10725 			selector = &sack_array[tsn_map];
10726 			if (mergeable && selector->right_edge) {
10727 				/*
10728 				 * Backup, left and right edges were ok to
10729 				 * merge.
10730 				 */
10731 				num_gap_blocks--;
10732 				gap_descriptor--;
10733 			}
10734 			if (selector->num_entries == 0)
10735 				mergeable = 0;
10736 			else {
10737 				for (j = 0; j < selector->num_entries; j++) {
10738 					if (mergeable && selector->right_edge) {
10739 						/*
10740 						 * do a merge by NOT setting
10741 						 * the left side
10742 						 */
10743 						mergeable = 0;
10744 					} else {
10745 						/*
10746 						 * no merge, set the left
10747 						 * side
10748 						 */
10749 						mergeable = 0;
10750 						gap_descriptor->start = htons((selector->gaps[j].start + offset));
10751 					}
10752 					gap_descriptor->end = htons((selector->gaps[j].end + offset));
10753 					num_gap_blocks++;
10754 					gap_descriptor++;
10755 					if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10756 						/* no more room */
10757 						limit_reached = 1;
10758 						break;
10759 					}
10760 				}
10761 				if (selector->left_edge) {
10762 					mergeable = 1;
10763 				}
10764 			}
10765 			if (limit_reached) {
10766 				/* Reached the limit stop */
10767 				break;
10768 			}
10769 			offset += 8;
10770 		}
10771 	}
10772 	if ((type == SCTP_NR_SELECTIVE_ACK) &&
10773 	    (limit_reached == 0)) {
10774 		mergeable = 0;
10775 
10776 		if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) {
10777 			siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10778 		} else {
10779 			siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10780 		}
10781 
10782 		if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10783 			offset = 1;
10784 		} else {
10785 			offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10786 		}
10787 		if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) {
10788 			/* we have a gap .. maybe */
10789 			for (i = 0; i < siz; i++) {
10790 				tsn_map = asoc->nr_mapping_array[i];
10791 				if (i == 0) {
10792 					/*
10793 					 * Clear all bits corresponding to
10794 					 * TSNs smaller or equal to the
10795 					 * cumulative TSN.
10796 					 */
10797 					tsn_map &= (~0U << (1 - offset));
10798 				}
10799 				selector = &sack_array[tsn_map];
10800 				if (mergeable && selector->right_edge) {
10801 					/*
10802 					 * Backup, left and right edges were
10803 					 * ok to merge.
10804 					 */
10805 					num_nr_gap_blocks--;
10806 					gap_descriptor--;
10807 				}
10808 				if (selector->num_entries == 0)
10809 					mergeable = 0;
10810 				else {
10811 					for (j = 0; j < selector->num_entries; j++) {
10812 						if (mergeable && selector->right_edge) {
10813 							/*
10814 							 * do a merge by NOT
10815 							 * setting the left
10816 							 * side
10817 							 */
10818 							mergeable = 0;
10819 						} else {
10820 							/*
10821 							 * no merge, set the
10822 							 * left side
10823 							 */
10824 							mergeable = 0;
10825 							gap_descriptor->start = htons((selector->gaps[j].start + offset));
10826 						}
10827 						gap_descriptor->end = htons((selector->gaps[j].end + offset));
10828 						num_nr_gap_blocks++;
10829 						gap_descriptor++;
10830 						if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10831 							/* no more room */
10832 							limit_reached = 1;
10833 							break;
10834 						}
10835 					}
10836 					if (selector->left_edge) {
10837 						mergeable = 1;
10838 					}
10839 				}
10840 				if (limit_reached) {
10841 					/* Reached the limit stop */
10842 					break;
10843 				}
10844 				offset += 8;
10845 			}
10846 		}
10847 	}
10848 	/* now we must add any dups we are going to report. */
10849 	if ((limit_reached == 0) && (asoc->numduptsns)) {
10850 		dup = (uint32_t *)gap_descriptor;
10851 		for (i = 0; i < asoc->numduptsns; i++) {
10852 			*dup = htonl(asoc->dup_tsns[i]);
10853 			dup++;
10854 			num_dups++;
10855 			if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10856 				/* no more room */
10857 				break;
10858 			}
10859 		}
10860 		asoc->numduptsns = 0;
10861 	}
10862 	/*
10863 	 * now that the chunk is prepared queue it to the control chunk
10864 	 * queue.
10865 	 */
10866 	if (type == SCTP_SELECTIVE_ACK) {
10867 		a_chk->send_size = (uint16_t)(sizeof(struct sctp_sack_chunk) +
10868 		    (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10869 		    num_dups * sizeof(int32_t));
10870 		SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10871 		sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10872 		sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10873 		sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10874 		sack->sack.num_dup_tsns = htons(num_dups);
10875 		sack->ch.chunk_type = type;
10876 		sack->ch.chunk_flags = flags;
10877 		sack->ch.chunk_length = htons(a_chk->send_size);
10878 	} else {
10879 		a_chk->send_size = (uint16_t)(sizeof(struct sctp_nr_sack_chunk) +
10880 		    (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10881 		    num_dups * sizeof(int32_t));
10882 		SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10883 		nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10884 		nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10885 		nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10886 		nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10887 		nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10888 		nr_sack->nr_sack.reserved = 0;
10889 		nr_sack->ch.chunk_type = type;
10890 		nr_sack->ch.chunk_flags = flags;
10891 		nr_sack->ch.chunk_length = htons(a_chk->send_size);
10892 	}
10893 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10894 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
10895 	asoc->ctrl_queue_cnt++;
10896 	asoc->send_sack = 0;
10897 	SCTP_STAT_INCR(sctps_sendsacks);
10898 	return;
10899 }
10900 
10901 void
10902 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked)
10903 {
10904 	struct mbuf *m_abort, *m, *m_last;
10905 	struct mbuf *m_out, *m_end = NULL;
10906 	struct sctp_abort_chunk *abort;
10907 	struct sctp_auth_chunk *auth = NULL;
10908 	struct sctp_nets *net;
10909 	uint32_t vtag;
10910 	uint32_t auth_offset = 0;
10911 	int error;
10912 	uint16_t cause_len, chunk_len, padding_len;
10913 	bool use_zero_crc;
10914 
10915 	SCTP_TCB_LOCK_ASSERT(stcb);
10916 	/*-
10917 	 * Add an AUTH chunk, if chunk requires it and save the offset into
10918 	 * the chain for AUTH
10919 	 */
10920 	if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10921 	    stcb->asoc.peer_auth_chunks)) {
10922 		m_out = sctp_add_auth_chunk(NULL, &m_end, &auth, &auth_offset,
10923 		    stcb, SCTP_ABORT_ASSOCIATION);
10924 		SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10925 	} else {
10926 		m_out = NULL;
10927 	}
10928 	switch (stcb->asoc.snd_edmid) {
10929 	case SCTP_EDMID_LOWER_LAYER_DTLS:
10930 		use_zero_crc = true;
10931 		break;
10932 	default:
10933 		use_zero_crc = false;
10934 		break;
10935 	}
10936 	m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_NOWAIT, 1, MT_HEADER);
10937 	if (m_abort == NULL) {
10938 		if (m_out) {
10939 			sctp_m_freem(m_out);
10940 		}
10941 		if (operr) {
10942 			sctp_m_freem(operr);
10943 		}
10944 		return;
10945 	}
10946 	/* link in any error */
10947 	SCTP_BUF_NEXT(m_abort) = operr;
10948 	cause_len = 0;
10949 	m_last = NULL;
10950 	for (m = operr; m; m = SCTP_BUF_NEXT(m)) {
10951 		cause_len += (uint16_t)SCTP_BUF_LEN(m);
10952 		if (SCTP_BUF_NEXT(m) == NULL) {
10953 			m_last = m;
10954 		}
10955 	}
10956 	SCTP_BUF_LEN(m_abort) = sizeof(struct sctp_abort_chunk);
10957 	chunk_len = (uint16_t)sizeof(struct sctp_abort_chunk) + cause_len;
10958 	padding_len = SCTP_SIZE32(chunk_len) - chunk_len;
10959 	if (m_out == NULL) {
10960 		/* NO Auth chunk prepended, so reserve space in front */
10961 		SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10962 		m_out = m_abort;
10963 	} else {
10964 		/* Put AUTH chunk at the front of the chain */
10965 		SCTP_BUF_NEXT(m_end) = m_abort;
10966 	}
10967 	if (stcb->asoc.alternate) {
10968 		net = stcb->asoc.alternate;
10969 	} else {
10970 		net = stcb->asoc.primary_destination;
10971 	}
10972 	/* Fill in the ABORT chunk header. */
10973 	abort = mtod(m_abort, struct sctp_abort_chunk *);
10974 	abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10975 	if (stcb->asoc.peer_vtag == 0) {
10976 		/* This happens iff the assoc is in COOKIE-WAIT state. */
10977 		vtag = stcb->asoc.my_vtag;
10978 		abort->ch.chunk_flags = SCTP_HAD_NO_TCB;
10979 	} else {
10980 		vtag = stcb->asoc.peer_vtag;
10981 		abort->ch.chunk_flags = 0;
10982 	}
10983 	abort->ch.chunk_length = htons(chunk_len);
10984 	/* Add padding, if necessary. */
10985 	if (padding_len > 0) {
10986 		if ((m_last == NULL) ||
10987 		    (sctp_add_pad_tombuf(m_last, padding_len) == NULL)) {
10988 			sctp_m_freem(m_out);
10989 			return;
10990 		}
10991 	}
10992 	if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10993 	    (struct sockaddr *)&net->ro._l_addr,
10994 	    m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, 0,
10995 	    stcb->sctp_ep->sctp_lport, stcb->rport, htonl(vtag),
10996 	    stcb->asoc.primary_destination->port, NULL,
10997 	    0, 0,
10998 	    use_zero_crc,
10999 	    so_locked))) {
11000 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
11001 		if (error == ENOBUFS) {
11002 			stcb->asoc.ifp_had_enobuf = 1;
11003 			SCTP_STAT_INCR(sctps_lowlevelerr);
11004 		}
11005 	} else {
11006 		stcb->asoc.ifp_had_enobuf = 0;
11007 	}
11008 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11009 }
11010 
11011 void
11012 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
11013     struct sctp_nets *net,
11014     int reflect_vtag)
11015 {
11016 	/* formulate and SEND a SHUTDOWN-COMPLETE */
11017 	struct mbuf *m_shutdown_comp;
11018 	struct sctp_shutdown_complete_chunk *shutdown_complete;
11019 	uint32_t vtag;
11020 	int error;
11021 	uint8_t flags;
11022 	bool use_zero_crc;
11023 
11024 	m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER);
11025 	if (m_shutdown_comp == NULL) {
11026 		/* no mbuf's */
11027 		return;
11028 	}
11029 	if (reflect_vtag) {
11030 		flags = SCTP_HAD_NO_TCB;
11031 		vtag = stcb->asoc.my_vtag;
11032 	} else {
11033 		flags = 0;
11034 		vtag = stcb->asoc.peer_vtag;
11035 	}
11036 	switch (stcb->asoc.snd_edmid) {
11037 	case SCTP_EDMID_LOWER_LAYER_DTLS:
11038 		use_zero_crc = true;
11039 		break;
11040 	default:
11041 		use_zero_crc = false;
11042 		break;
11043 	}
11044 	shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
11045 	shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
11046 	shutdown_complete->ch.chunk_flags = flags;
11047 	shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
11048 	SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
11049 	if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
11050 	    (struct sockaddr *)&net->ro._l_addr,
11051 	    m_shutdown_comp, 0, NULL, 0, 1, 0, 0,
11052 	    stcb->sctp_ep->sctp_lport, stcb->rport,
11053 	    htonl(vtag),
11054 	    net->port, NULL,
11055 	    0, 0,
11056 	    use_zero_crc,
11057 	    SCTP_SO_NOT_LOCKED))) {
11058 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
11059 		if (error == ENOBUFS) {
11060 			stcb->asoc.ifp_had_enobuf = 1;
11061 			SCTP_STAT_INCR(sctps_lowlevelerr);
11062 		}
11063 	} else {
11064 		stcb->asoc.ifp_had_enobuf = 0;
11065 	}
11066 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11067 	return;
11068 }
11069 
11070 static void
11071 sctp_send_resp_msg(struct sockaddr *src, struct sockaddr *dst,
11072     struct sctphdr *sh, uint32_t vtag,
11073     uint8_t type, struct mbuf *cause,
11074     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
11075     uint32_t vrf_id, uint16_t port)
11076 {
11077 	struct mbuf *o_pak;
11078 	struct mbuf *mout;
11079 	struct sctphdr *shout;
11080 	struct sctp_chunkhdr *ch;
11081 #if defined(INET) || defined(INET6)
11082 	struct udphdr *udp;
11083 #endif
11084 	int ret, len, cause_len, padding_len;
11085 #ifdef INET
11086 	struct sockaddr_in *src_sin, *dst_sin;
11087 	struct ip *ip;
11088 #endif
11089 #ifdef INET6
11090 	struct sockaddr_in6 *src_sin6, *dst_sin6;
11091 	struct ip6_hdr *ip6;
11092 #endif
11093 
11094 	/* Compute the length of the cause and add final padding. */
11095 	cause_len = 0;
11096 	if (cause != NULL) {
11097 		struct mbuf *m_at, *m_last = NULL;
11098 
11099 		for (m_at = cause; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
11100 			if (SCTP_BUF_NEXT(m_at) == NULL)
11101 				m_last = m_at;
11102 			cause_len += SCTP_BUF_LEN(m_at);
11103 		}
11104 		padding_len = cause_len % 4;
11105 		if (padding_len != 0) {
11106 			padding_len = 4 - padding_len;
11107 		}
11108 		if (padding_len != 0) {
11109 			if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
11110 				sctp_m_freem(cause);
11111 				return;
11112 			}
11113 		}
11114 	} else {
11115 		padding_len = 0;
11116 	}
11117 	/* Get an mbuf for the header. */
11118 	len = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
11119 	switch (dst->sa_family) {
11120 #ifdef INET
11121 	case AF_INET:
11122 		len += sizeof(struct ip);
11123 		break;
11124 #endif
11125 #ifdef INET6
11126 	case AF_INET6:
11127 		len += sizeof(struct ip6_hdr);
11128 		break;
11129 #endif
11130 	default:
11131 		break;
11132 	}
11133 #if defined(INET) || defined(INET6)
11134 	if (port) {
11135 		len += sizeof(struct udphdr);
11136 	}
11137 #endif
11138 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_NOWAIT, 1, MT_DATA);
11139 	if (mout == NULL) {
11140 		if (cause) {
11141 			sctp_m_freem(cause);
11142 		}
11143 		return;
11144 	}
11145 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
11146 	SCTP_BUF_LEN(mout) = len;
11147 	SCTP_BUF_NEXT(mout) = cause;
11148 	M_SETFIB(mout, fibnum);
11149 	mout->m_pkthdr.flowid = mflowid;
11150 	M_HASHTYPE_SET(mout, mflowtype);
11151 #ifdef INET
11152 	ip = NULL;
11153 #endif
11154 #ifdef INET6
11155 	ip6 = NULL;
11156 #endif
11157 	switch (dst->sa_family) {
11158 #ifdef INET
11159 	case AF_INET:
11160 		src_sin = (struct sockaddr_in *)src;
11161 		dst_sin = (struct sockaddr_in *)dst;
11162 		ip = mtod(mout, struct ip *);
11163 		ip->ip_v = IPVERSION;
11164 		ip->ip_hl = (sizeof(struct ip) >> 2);
11165 		ip->ip_tos = 0;
11166 		ip->ip_off = htons(IP_DF);
11167 		ip_fillid(ip);
11168 		ip->ip_ttl = MODULE_GLOBAL(ip_defttl);
11169 		if (port) {
11170 			ip->ip_p = IPPROTO_UDP;
11171 		} else {
11172 			ip->ip_p = IPPROTO_SCTP;
11173 		}
11174 		ip->ip_src.s_addr = dst_sin->sin_addr.s_addr;
11175 		ip->ip_dst.s_addr = src_sin->sin_addr.s_addr;
11176 		ip->ip_sum = 0;
11177 		len = sizeof(struct ip);
11178 		shout = (struct sctphdr *)((caddr_t)ip + len);
11179 		break;
11180 #endif
11181 #ifdef INET6
11182 	case AF_INET6:
11183 		src_sin6 = (struct sockaddr_in6 *)src;
11184 		dst_sin6 = (struct sockaddr_in6 *)dst;
11185 		ip6 = mtod(mout, struct ip6_hdr *);
11186 		ip6->ip6_flow = htonl(0x60000000);
11187 		if (V_ip6_auto_flowlabel) {
11188 			ip6->ip6_flow |= (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
11189 		}
11190 		ip6->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11191 		if (port) {
11192 			ip6->ip6_nxt = IPPROTO_UDP;
11193 		} else {
11194 			ip6->ip6_nxt = IPPROTO_SCTP;
11195 		}
11196 		ip6->ip6_src = dst_sin6->sin6_addr;
11197 		ip6->ip6_dst = src_sin6->sin6_addr;
11198 		len = sizeof(struct ip6_hdr);
11199 		shout = (struct sctphdr *)((caddr_t)ip6 + len);
11200 		break;
11201 #endif
11202 	default:
11203 		len = 0;
11204 		shout = mtod(mout, struct sctphdr *);
11205 		break;
11206 	}
11207 #if defined(INET) || defined(INET6)
11208 	if (port) {
11209 		if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
11210 			sctp_m_freem(mout);
11211 			return;
11212 		}
11213 		udp = (struct udphdr *)shout;
11214 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11215 		udp->uh_dport = port;
11216 		udp->uh_sum = 0;
11217 		udp->uh_ulen = htons((uint16_t)(sizeof(struct udphdr) +
11218 		    sizeof(struct sctphdr) +
11219 		    sizeof(struct sctp_chunkhdr) +
11220 		    cause_len + padding_len));
11221 		len += sizeof(struct udphdr);
11222 		shout = (struct sctphdr *)((caddr_t)shout + sizeof(struct udphdr));
11223 	} else {
11224 		udp = NULL;
11225 	}
11226 #endif
11227 	shout->src_port = sh->dest_port;
11228 	shout->dest_port = sh->src_port;
11229 	shout->checksum = 0;
11230 	if (vtag) {
11231 		shout->v_tag = htonl(vtag);
11232 	} else {
11233 		shout->v_tag = sh->v_tag;
11234 	}
11235 	len += sizeof(struct sctphdr);
11236 	ch = (struct sctp_chunkhdr *)((caddr_t)shout + sizeof(struct sctphdr));
11237 	ch->chunk_type = type;
11238 	if (vtag) {
11239 		ch->chunk_flags = 0;
11240 	} else {
11241 		ch->chunk_flags = SCTP_HAD_NO_TCB;
11242 	}
11243 	ch->chunk_length = htons((uint16_t)(sizeof(struct sctp_chunkhdr) + cause_len));
11244 	len += sizeof(struct sctp_chunkhdr);
11245 	len += cause_len + padding_len;
11246 
11247 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11248 		sctp_m_freem(mout);
11249 		return;
11250 	}
11251 	SCTP_ATTACH_CHAIN(o_pak, mout, len);
11252 	switch (dst->sa_family) {
11253 #ifdef INET
11254 	case AF_INET:
11255 		if (port) {
11256 			if (V_udp_cksum) {
11257 				udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11258 			} else {
11259 				udp->uh_sum = 0;
11260 			}
11261 		}
11262 		ip->ip_len = htons(len);
11263 		if (port) {
11264 			shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip) + sizeof(struct udphdr));
11265 			SCTP_STAT_INCR(sctps_sendswcrc);
11266 			if (V_udp_cksum) {
11267 				SCTP_ENABLE_UDP_CSUM(o_pak);
11268 			}
11269 		} else {
11270 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
11271 			mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
11272 			SCTP_STAT_INCR(sctps_sendhwcrc);
11273 		}
11274 #ifdef SCTP_PACKET_LOGGING
11275 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
11276 			sctp_packet_log(o_pak);
11277 		}
11278 #endif
11279 		SCTP_PROBE5(send, NULL, NULL, ip, NULL, shout);
11280 		SCTP_IP_OUTPUT(ret, o_pak, NULL, NULL, vrf_id);
11281 		break;
11282 #endif
11283 #ifdef INET6
11284 	case AF_INET6:
11285 		ip6->ip6_plen = htons((uint16_t)(len - sizeof(struct ip6_hdr)));
11286 		if (port) {
11287 			shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11288 			SCTP_STAT_INCR(sctps_sendswcrc);
11289 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11290 				udp->uh_sum = 0xffff;
11291 			}
11292 		} else {
11293 			mout->m_pkthdr.csum_flags = CSUM_SCTP_IPV6;
11294 			mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
11295 			SCTP_STAT_INCR(sctps_sendhwcrc);
11296 		}
11297 #ifdef SCTP_PACKET_LOGGING
11298 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
11299 			sctp_packet_log(o_pak);
11300 		}
11301 #endif
11302 		SCTP_PROBE5(send, NULL, NULL, ip6, NULL, shout);
11303 		SCTP_IP6_OUTPUT(ret, o_pak, NULL, NULL, NULL, vrf_id);
11304 		break;
11305 #endif
11306 	default:
11307 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
11308 		    dst->sa_family);
11309 		sctp_m_freem(mout);
11310 		SCTP_LTRACE_ERR_RET_PKT(mout, NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11311 		return;
11312 	}
11313 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
11314 	if (port) {
11315 		UDPSTAT_INC(udps_opackets);
11316 	}
11317 	SCTP_STAT_INCR(sctps_sendpackets);
11318 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11319 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11320 	if (ret) {
11321 		SCTP_STAT_INCR(sctps_senderrors);
11322 	}
11323 	return;
11324 }
11325 
11326 void
11327 sctp_send_shutdown_complete2(struct sockaddr *src, struct sockaddr *dst,
11328     struct sctphdr *sh,
11329     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
11330     uint32_t vrf_id, uint16_t port)
11331 {
11332 	sctp_send_resp_msg(src, dst, sh, 0, SCTP_SHUTDOWN_COMPLETE, NULL,
11333 	    mflowtype, mflowid, fibnum,
11334 	    vrf_id, port);
11335 }
11336 
11337 void
11338 sctp_send_hb(struct sctp_tcb *stcb, struct sctp_nets *net, int so_locked)
11339 {
11340 	struct sctp_tmit_chunk *chk;
11341 	struct sctp_heartbeat_chunk *hb;
11342 	struct timeval now;
11343 
11344 	SCTP_TCB_LOCK_ASSERT(stcb);
11345 	if (net == NULL) {
11346 		return;
11347 	}
11348 	(void)SCTP_GETTIME_TIMEVAL(&now);
11349 	switch (net->ro._l_addr.sa.sa_family) {
11350 #ifdef INET
11351 	case AF_INET:
11352 		break;
11353 #endif
11354 #ifdef INET6
11355 	case AF_INET6:
11356 		break;
11357 #endif
11358 	default:
11359 		return;
11360 	}
11361 	sctp_alloc_a_chunk(stcb, chk);
11362 	if (chk == NULL) {
11363 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
11364 		return;
11365 	}
11366 
11367 	chk->copy_by_ref = 0;
11368 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
11369 	chk->rec.chunk_id.can_take_data = 1;
11370 	chk->flags = 0;
11371 	chk->asoc = &stcb->asoc;
11372 	chk->send_size = sizeof(struct sctp_heartbeat_chunk);
11373 
11374 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
11375 	if (chk->data == NULL) {
11376 		sctp_free_a_chunk(stcb, chk, so_locked);
11377 		return;
11378 	}
11379 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11380 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11381 	chk->sent = SCTP_DATAGRAM_UNSENT;
11382 	chk->snd_count = 0;
11383 	chk->whoTo = net;
11384 	atomic_add_int(&chk->whoTo->ref_count, 1);
11385 	/* Now we have a mbuf that we can fill in with the details */
11386 	hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
11387 	memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
11388 	/* fill out chunk header */
11389 	hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
11390 	hb->ch.chunk_flags = 0;
11391 	hb->ch.chunk_length = htons(chk->send_size);
11392 	/* Fill out hb parameter */
11393 	hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
11394 	hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
11395 	hb->heartbeat.hb_info.time_value_1 = (uint32_t)now.tv_sec;
11396 	hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
11397 	/* Did our user request this one, put it in */
11398 	hb->heartbeat.hb_info.addr_family = (uint8_t)net->ro._l_addr.sa.sa_family;
11399 	hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len;
11400 	if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
11401 		/*
11402 		 * we only take from the entropy pool if the address is not
11403 		 * confirmed.
11404 		 */
11405 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11406 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11407 	} else {
11408 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
11409 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
11410 	}
11411 	switch (net->ro._l_addr.sa.sa_family) {
11412 #ifdef INET
11413 	case AF_INET:
11414 		memcpy(hb->heartbeat.hb_info.address,
11415 		    &net->ro._l_addr.sin.sin_addr,
11416 		    sizeof(net->ro._l_addr.sin.sin_addr));
11417 		break;
11418 #endif
11419 #ifdef INET6
11420 	case AF_INET6:
11421 		memcpy(hb->heartbeat.hb_info.address,
11422 		    &net->ro._l_addr.sin6.sin6_addr,
11423 		    sizeof(net->ro._l_addr.sin6.sin6_addr));
11424 		break;
11425 #endif
11426 	default:
11427 		if (chk->data) {
11428 			sctp_m_freem(chk->data);
11429 			chk->data = NULL;
11430 		}
11431 		sctp_free_a_chunk(stcb, chk, so_locked);
11432 		return;
11433 		break;
11434 	}
11435 	net->hb_responded = 0;
11436 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11437 	stcb->asoc.ctrl_queue_cnt++;
11438 	SCTP_STAT_INCR(sctps_sendheartbeat);
11439 	return;
11440 }
11441 
11442 void
11443 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
11444     uint32_t high_tsn)
11445 {
11446 	struct sctp_association *asoc;
11447 	struct sctp_ecne_chunk *ecne;
11448 	struct sctp_tmit_chunk *chk;
11449 
11450 	if (net == NULL) {
11451 		return;
11452 	}
11453 	asoc = &stcb->asoc;
11454 	SCTP_TCB_LOCK_ASSERT(stcb);
11455 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11456 		if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) {
11457 			/* found a previous ECN_ECHO update it if needed */
11458 			uint32_t cnt, ctsn;
11459 
11460 			ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11461 			ctsn = ntohl(ecne->tsn);
11462 			if (SCTP_TSN_GT(high_tsn, ctsn)) {
11463 				ecne->tsn = htonl(high_tsn);
11464 				SCTP_STAT_INCR(sctps_queue_upd_ecne);
11465 			}
11466 			cnt = ntohl(ecne->num_pkts_since_cwr);
11467 			cnt++;
11468 			ecne->num_pkts_since_cwr = htonl(cnt);
11469 			return;
11470 		}
11471 	}
11472 	/* nope could not find one to update so we must build one */
11473 	sctp_alloc_a_chunk(stcb, chk);
11474 	if (chk == NULL) {
11475 		return;
11476 	}
11477 	SCTP_STAT_INCR(sctps_queue_upd_ecne);
11478 	chk->copy_by_ref = 0;
11479 	chk->rec.chunk_id.id = SCTP_ECN_ECHO;
11480 	chk->rec.chunk_id.can_take_data = 0;
11481 	chk->flags = 0;
11482 	chk->asoc = &stcb->asoc;
11483 	chk->send_size = sizeof(struct sctp_ecne_chunk);
11484 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
11485 	if (chk->data == NULL) {
11486 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11487 		return;
11488 	}
11489 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11490 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11491 	chk->sent = SCTP_DATAGRAM_UNSENT;
11492 	chk->snd_count = 0;
11493 	chk->whoTo = net;
11494 	atomic_add_int(&chk->whoTo->ref_count, 1);
11495 
11496 	stcb->asoc.ecn_echo_cnt_onq++;
11497 	ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11498 	ecne->ch.chunk_type = SCTP_ECN_ECHO;
11499 	ecne->ch.chunk_flags = 0;
11500 	ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
11501 	ecne->tsn = htonl(high_tsn);
11502 	ecne->num_pkts_since_cwr = htonl(1);
11503 	TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next);
11504 	asoc->ctrl_queue_cnt++;
11505 }
11506 
11507 void
11508 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
11509     struct mbuf *m, int len, int iphlen, int bad_crc)
11510 {
11511 	struct sctp_association *asoc;
11512 	struct sctp_pktdrop_chunk *drp;
11513 	struct sctp_tmit_chunk *chk;
11514 	uint8_t *datap;
11515 	int was_trunc = 0;
11516 	int fullsz = 0;
11517 	long spc;
11518 	int offset;
11519 	struct sctp_chunkhdr *ch, chunk_buf;
11520 	unsigned int chk_length;
11521 
11522 	if (!stcb) {
11523 		return;
11524 	}
11525 	asoc = &stcb->asoc;
11526 	SCTP_TCB_LOCK_ASSERT(stcb);
11527 	if (asoc->pktdrop_supported == 0) {
11528 		/*-
11529 		 * peer must declare support before I send one.
11530 		 */
11531 		return;
11532 	}
11533 	if (stcb->sctp_socket == NULL) {
11534 		return;
11535 	}
11536 	sctp_alloc_a_chunk(stcb, chk);
11537 	if (chk == NULL) {
11538 		return;
11539 	}
11540 	chk->copy_by_ref = 0;
11541 	chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11542 	chk->rec.chunk_id.can_take_data = 1;
11543 	chk->flags = 0;
11544 	len -= iphlen;
11545 	chk->send_size = len;
11546 	/* Validate that we do not have an ABORT in here. */
11547 	offset = iphlen + sizeof(struct sctphdr);
11548 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11549 	    sizeof(*ch), (uint8_t *)&chunk_buf);
11550 	while (ch != NULL) {
11551 		chk_length = ntohs(ch->chunk_length);
11552 		if (chk_length < sizeof(*ch)) {
11553 			/* break to abort land */
11554 			break;
11555 		}
11556 		switch (ch->chunk_type) {
11557 		case SCTP_PACKET_DROPPED:
11558 		case SCTP_ABORT_ASSOCIATION:
11559 		case SCTP_INITIATION_ACK:
11560 			/**
11561 			 * We don't respond with an PKT-DROP to an ABORT
11562 			 * or PKT-DROP. We also do not respond to an
11563 			 * INIT-ACK, because we can't know if the initiation
11564 			 * tag is correct or not.
11565 			 */
11566 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11567 			return;
11568 		default:
11569 			break;
11570 		}
11571 		offset += SCTP_SIZE32(chk_length);
11572 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11573 		    sizeof(*ch), (uint8_t *)&chunk_buf);
11574 	}
11575 
11576 	if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11577 	    min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11578 		/*
11579 		 * only send 1 mtu worth, trim off the excess on the end.
11580 		 */
11581 		fullsz = len;
11582 		len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11583 		was_trunc = 1;
11584 	}
11585 	chk->asoc = &stcb->asoc;
11586 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
11587 	if (chk->data == NULL) {
11588 jump_out:
11589 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11590 		return;
11591 	}
11592 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11593 	drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11594 	if (drp == NULL) {
11595 		sctp_m_freem(chk->data);
11596 		chk->data = NULL;
11597 		goto jump_out;
11598 	}
11599 	chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11600 	    sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11601 	chk->book_size_scale = 0;
11602 	if (was_trunc) {
11603 		drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11604 		drp->trunc_len = htons(fullsz);
11605 		/*
11606 		 * Len is already adjusted to size minus overhead above take
11607 		 * out the pkt_drop chunk itself from it.
11608 		 */
11609 		chk->send_size = (uint16_t)(len - sizeof(struct sctp_pktdrop_chunk));
11610 		len = chk->send_size;
11611 	} else {
11612 		/* no truncation needed */
11613 		drp->ch.chunk_flags = 0;
11614 		drp->trunc_len = htons(0);
11615 	}
11616 	if (bad_crc) {
11617 		drp->ch.chunk_flags |= SCTP_BADCRC;
11618 	}
11619 	chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11620 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11621 	chk->sent = SCTP_DATAGRAM_UNSENT;
11622 	chk->snd_count = 0;
11623 	if (net) {
11624 		/* we should hit here */
11625 		chk->whoTo = net;
11626 		atomic_add_int(&chk->whoTo->ref_count, 1);
11627 	} else {
11628 		chk->whoTo = NULL;
11629 	}
11630 	drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11631 	drp->ch.chunk_length = htons(chk->send_size);
11632 	spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11633 	if (spc < 0) {
11634 		spc = 0;
11635 	}
11636 	drp->bottle_bw = htonl(spc);
11637 	if (asoc->my_rwnd) {
11638 		drp->current_onq = htonl(asoc->size_on_reasm_queue +
11639 		    asoc->size_on_all_streams +
11640 		    asoc->my_rwnd_control_len +
11641 		    SCTP_SBAVAIL(&stcb->sctp_socket->so_rcv));
11642 	} else {
11643 		/*-
11644 		 * If my rwnd is 0, possibly from mbuf depletion as well as
11645 		 * space used, tell the peer there is NO space aka onq == bw
11646 		 */
11647 		drp->current_onq = htonl(spc);
11648 	}
11649 	drp->reserved = 0;
11650 	datap = drp->data;
11651 	m_copydata(m, iphlen, len, (caddr_t)datap);
11652 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11653 	asoc->ctrl_queue_cnt++;
11654 }
11655 
11656 void
11657 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override)
11658 {
11659 	struct sctp_association *asoc;
11660 	struct sctp_cwr_chunk *cwr;
11661 	struct sctp_tmit_chunk *chk;
11662 
11663 	SCTP_TCB_LOCK_ASSERT(stcb);
11664 	if (net == NULL) {
11665 		return;
11666 	}
11667 	asoc = &stcb->asoc;
11668 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11669 		if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) {
11670 			/*
11671 			 * found a previous CWR queued to same destination
11672 			 * update it if needed
11673 			 */
11674 			uint32_t ctsn;
11675 
11676 			cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11677 			ctsn = ntohl(cwr->tsn);
11678 			if (SCTP_TSN_GT(high_tsn, ctsn)) {
11679 				cwr->tsn = htonl(high_tsn);
11680 			}
11681 			if (override & SCTP_CWR_REDUCE_OVERRIDE) {
11682 				/* Make sure override is carried */
11683 				cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE;
11684 			}
11685 			return;
11686 		}
11687 	}
11688 	sctp_alloc_a_chunk(stcb, chk);
11689 	if (chk == NULL) {
11690 		return;
11691 	}
11692 	chk->copy_by_ref = 0;
11693 	chk->rec.chunk_id.id = SCTP_ECN_CWR;
11694 	chk->rec.chunk_id.can_take_data = 1;
11695 	chk->flags = 0;
11696 	chk->asoc = asoc;
11697 	chk->send_size = sizeof(struct sctp_cwr_chunk);
11698 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
11699 	if (chk->data == NULL) {
11700 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11701 		return;
11702 	}
11703 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11704 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11705 	chk->sent = SCTP_DATAGRAM_UNSENT;
11706 	chk->snd_count = 0;
11707 	chk->whoTo = net;
11708 	atomic_add_int(&chk->whoTo->ref_count, 1);
11709 	cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11710 	cwr->ch.chunk_type = SCTP_ECN_CWR;
11711 	cwr->ch.chunk_flags = override;
11712 	cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11713 	cwr->tsn = htonl(high_tsn);
11714 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
11715 	asoc->ctrl_queue_cnt++;
11716 }
11717 
11718 static int
11719 sctp_add_stream_reset_out(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
11720     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11721 {
11722 	uint16_t len, old_len, i;
11723 	struct sctp_stream_reset_out_request *req_out;
11724 	struct sctp_chunkhdr *ch;
11725 	int at;
11726 	int number_entries = 0;
11727 
11728 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11729 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11730 	/* get to new offset for the param. */
11731 	req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11732 	/* now how long will this param be? */
11733 	for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11734 		if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) &&
11735 		    (stcb->asoc.strmout[i].chunks_on_queues == 0) &&
11736 		    TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
11737 			number_entries++;
11738 		}
11739 	}
11740 	if (number_entries == 0) {
11741 		return (0);
11742 	}
11743 	if (number_entries == stcb->asoc.streamoutcnt) {
11744 		number_entries = 0;
11745 	}
11746 	if (number_entries > SCTP_MAX_STREAMS_AT_ONCE_RESET) {
11747 		number_entries = SCTP_MAX_STREAMS_AT_ONCE_RESET;
11748 	}
11749 	len = (uint16_t)(sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11750 	req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11751 	req_out->ph.param_length = htons(len);
11752 	req_out->request_seq = htonl(seq);
11753 	req_out->response_seq = htonl(resp_seq);
11754 	req_out->send_reset_at_tsn = htonl(last_sent);
11755 	at = 0;
11756 	if (number_entries) {
11757 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11758 			if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) &&
11759 			    (stcb->asoc.strmout[i].chunks_on_queues == 0) &&
11760 			    TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
11761 				req_out->list_of_streams[at] = htons(i);
11762 				at++;
11763 				stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT;
11764 				if (at >= number_entries) {
11765 					break;
11766 				}
11767 			}
11768 		}
11769 	} else {
11770 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11771 			stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT;
11772 		}
11773 	}
11774 	if (SCTP_SIZE32(len) > len) {
11775 		/*-
11776 		 * Need to worry about the pad we may end up adding to the
11777 		 * end. This is easy since the struct is either aligned to 4
11778 		 * bytes or 2 bytes off.
11779 		 */
11780 		req_out->list_of_streams[number_entries] = 0;
11781 	}
11782 	/* now fix the chunk length */
11783 	ch->chunk_length = htons(len + old_len);
11784 	chk->book_size = len + old_len;
11785 	chk->book_size_scale = 0;
11786 	chk->send_size = SCTP_SIZE32(chk->book_size);
11787 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11788 	return (1);
11789 }
11790 
11791 static void
11792 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11793     int number_entries, uint16_t *list,
11794     uint32_t seq)
11795 {
11796 	uint16_t len, old_len, i;
11797 	struct sctp_stream_reset_in_request *req_in;
11798 	struct sctp_chunkhdr *ch;
11799 
11800 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11801 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11802 
11803 	/* get to new offset for the param. */
11804 	req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11805 	/* now how long will this param be? */
11806 	len = (uint16_t)(sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11807 	req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11808 	req_in->ph.param_length = htons(len);
11809 	req_in->request_seq = htonl(seq);
11810 	if (number_entries) {
11811 		for (i = 0; i < number_entries; i++) {
11812 			req_in->list_of_streams[i] = htons(list[i]);
11813 		}
11814 	}
11815 	if (SCTP_SIZE32(len) > len) {
11816 		/*-
11817 		 * Need to worry about the pad we may end up adding to the
11818 		 * end. This is easy since the struct is either aligned to 4
11819 		 * bytes or 2 bytes off.
11820 		 */
11821 		req_in->list_of_streams[number_entries] = 0;
11822 	}
11823 	/* now fix the chunk length */
11824 	ch->chunk_length = htons(len + old_len);
11825 	chk->book_size = len + old_len;
11826 	chk->book_size_scale = 0;
11827 	chk->send_size = SCTP_SIZE32(chk->book_size);
11828 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11829 	return;
11830 }
11831 
11832 static void
11833 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11834     uint32_t seq)
11835 {
11836 	uint16_t len, old_len;
11837 	struct sctp_stream_reset_tsn_request *req_tsn;
11838 	struct sctp_chunkhdr *ch;
11839 
11840 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11841 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11842 
11843 	/* get to new offset for the param. */
11844 	req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11845 	/* now how long will this param be? */
11846 	len = sizeof(struct sctp_stream_reset_tsn_request);
11847 	req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11848 	req_tsn->ph.param_length = htons(len);
11849 	req_tsn->request_seq = htonl(seq);
11850 
11851 	/* now fix the chunk length */
11852 	ch->chunk_length = htons(len + old_len);
11853 	chk->send_size = len + old_len;
11854 	chk->book_size = SCTP_SIZE32(chk->send_size);
11855 	chk->book_size_scale = 0;
11856 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11857 	return;
11858 }
11859 
11860 void
11861 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11862     uint32_t resp_seq, uint32_t result)
11863 {
11864 	uint16_t len, old_len;
11865 	struct sctp_stream_reset_response *resp;
11866 	struct sctp_chunkhdr *ch;
11867 
11868 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11869 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11870 
11871 	/* get to new offset for the param. */
11872 	resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11873 	/* now how long will this param be? */
11874 	len = sizeof(struct sctp_stream_reset_response);
11875 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11876 	resp->ph.param_length = htons(len);
11877 	resp->response_seq = htonl(resp_seq);
11878 	resp->result = ntohl(result);
11879 
11880 	/* now fix the chunk length */
11881 	ch->chunk_length = htons(len + old_len);
11882 	chk->book_size = len + old_len;
11883 	chk->book_size_scale = 0;
11884 	chk->send_size = SCTP_SIZE32(chk->book_size);
11885 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11886 	return;
11887 }
11888 
11889 void
11890 sctp_send_deferred_reset_response(struct sctp_tcb *stcb,
11891     struct sctp_stream_reset_list *ent,
11892     int response)
11893 {
11894 	struct sctp_association *asoc;
11895 	struct sctp_tmit_chunk *chk;
11896 	struct sctp_chunkhdr *ch;
11897 
11898 	asoc = &stcb->asoc;
11899 
11900 	/*
11901 	 * Reset our last reset action to the new one IP -> response
11902 	 * (PERFORMED probably). This assures that if we fail to send, a
11903 	 * retran from the peer will get the new response.
11904 	 */
11905 	asoc->last_reset_action[0] = response;
11906 	if (asoc->stream_reset_outstanding) {
11907 		return;
11908 	}
11909 	sctp_alloc_a_chunk(stcb, chk);
11910 	if (chk == NULL) {
11911 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11912 		return;
11913 	}
11914 	chk->copy_by_ref = 0;
11915 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11916 	chk->rec.chunk_id.can_take_data = 0;
11917 	chk->flags = 0;
11918 	chk->asoc = &stcb->asoc;
11919 	chk->book_size = sizeof(struct sctp_chunkhdr);
11920 	chk->send_size = SCTP_SIZE32(chk->book_size);
11921 	chk->book_size_scale = 0;
11922 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
11923 	if (chk->data == NULL) {
11924 		sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
11925 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11926 		return;
11927 	}
11928 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11929 	/* setup chunk parameters */
11930 	chk->sent = SCTP_DATAGRAM_UNSENT;
11931 	chk->snd_count = 0;
11932 	if (stcb->asoc.alternate) {
11933 		chk->whoTo = stcb->asoc.alternate;
11934 	} else {
11935 		chk->whoTo = stcb->asoc.primary_destination;
11936 	}
11937 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11938 	ch->chunk_type = SCTP_STREAM_RESET;
11939 	ch->chunk_flags = 0;
11940 	ch->chunk_length = htons(chk->book_size);
11941 	atomic_add_int(&chk->whoTo->ref_count, 1);
11942 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11943 	sctp_add_stream_reset_result(chk, ent->seq, response);
11944 	/* insert the chunk for sending */
11945 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11946 	    chk,
11947 	    sctp_next);
11948 	asoc->ctrl_queue_cnt++;
11949 }
11950 
11951 void
11952 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11953     uint32_t resp_seq, uint32_t result,
11954     uint32_t send_una, uint32_t recv_next)
11955 {
11956 	uint16_t len, old_len;
11957 	struct sctp_stream_reset_response_tsn *resp;
11958 	struct sctp_chunkhdr *ch;
11959 
11960 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11961 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11962 
11963 	/* get to new offset for the param. */
11964 	resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11965 	/* now how long will this param be? */
11966 	len = sizeof(struct sctp_stream_reset_response_tsn);
11967 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11968 	resp->ph.param_length = htons(len);
11969 	resp->response_seq = htonl(resp_seq);
11970 	resp->result = htonl(result);
11971 	resp->senders_next_tsn = htonl(send_una);
11972 	resp->receivers_next_tsn = htonl(recv_next);
11973 
11974 	/* now fix the chunk length */
11975 	ch->chunk_length = htons(len + old_len);
11976 	chk->book_size = len + old_len;
11977 	chk->send_size = SCTP_SIZE32(chk->book_size);
11978 	chk->book_size_scale = 0;
11979 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11980 	return;
11981 }
11982 
11983 static void
11984 sctp_add_an_out_stream(struct sctp_tmit_chunk *chk,
11985     uint32_t seq,
11986     uint16_t adding)
11987 {
11988 	uint16_t len, old_len;
11989 	struct sctp_chunkhdr *ch;
11990 	struct sctp_stream_reset_add_strm *addstr;
11991 
11992 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11993 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11994 
11995 	/* get to new offset for the param. */
11996 	addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11997 	/* now how long will this param be? */
11998 	len = sizeof(struct sctp_stream_reset_add_strm);
11999 
12000 	/* Fill it out. */
12001 	addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_OUT_STREAMS);
12002 	addstr->ph.param_length = htons(len);
12003 	addstr->request_seq = htonl(seq);
12004 	addstr->number_of_streams = htons(adding);
12005 	addstr->reserved = 0;
12006 
12007 	/* now fix the chunk length */
12008 	ch->chunk_length = htons(len + old_len);
12009 	chk->send_size = len + old_len;
12010 	chk->book_size = SCTP_SIZE32(chk->send_size);
12011 	chk->book_size_scale = 0;
12012 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
12013 	return;
12014 }
12015 
12016 static void
12017 sctp_add_an_in_stream(struct sctp_tmit_chunk *chk,
12018     uint32_t seq,
12019     uint16_t adding)
12020 {
12021 	uint16_t len, old_len;
12022 	struct sctp_chunkhdr *ch;
12023 	struct sctp_stream_reset_add_strm *addstr;
12024 
12025 	ch = mtod(chk->data, struct sctp_chunkhdr *);
12026 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
12027 
12028 	/* get to new offset for the param. */
12029 	addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
12030 	/* now how long will this param be? */
12031 	len = sizeof(struct sctp_stream_reset_add_strm);
12032 	/* Fill it out. */
12033 	addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_IN_STREAMS);
12034 	addstr->ph.param_length = htons(len);
12035 	addstr->request_seq = htonl(seq);
12036 	addstr->number_of_streams = htons(adding);
12037 	addstr->reserved = 0;
12038 
12039 	/* now fix the chunk length */
12040 	ch->chunk_length = htons(len + old_len);
12041 	chk->send_size = len + old_len;
12042 	chk->book_size = SCTP_SIZE32(chk->send_size);
12043 	chk->book_size_scale = 0;
12044 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
12045 	return;
12046 }
12047 
12048 int
12049 sctp_send_stream_reset_out_if_possible(struct sctp_tcb *stcb, int so_locked)
12050 {
12051 	struct sctp_association *asoc;
12052 	struct sctp_tmit_chunk *chk;
12053 	struct sctp_chunkhdr *ch;
12054 	uint32_t seq;
12055 
12056 	asoc = &stcb->asoc;
12057 	asoc->trigger_reset = 0;
12058 	if (asoc->stream_reset_outstanding) {
12059 		return (EALREADY);
12060 	}
12061 	sctp_alloc_a_chunk(stcb, chk);
12062 	if (chk == NULL) {
12063 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12064 		return (ENOMEM);
12065 	}
12066 	chk->copy_by_ref = 0;
12067 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
12068 	chk->rec.chunk_id.can_take_data = 0;
12069 	chk->flags = 0;
12070 	chk->asoc = &stcb->asoc;
12071 	chk->book_size = sizeof(struct sctp_chunkhdr);
12072 	chk->send_size = SCTP_SIZE32(chk->book_size);
12073 	chk->book_size_scale = 0;
12074 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
12075 	if (chk->data == NULL) {
12076 		sctp_free_a_chunk(stcb, chk, so_locked);
12077 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12078 		return (ENOMEM);
12079 	}
12080 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12081 
12082 	/* setup chunk parameters */
12083 	chk->sent = SCTP_DATAGRAM_UNSENT;
12084 	chk->snd_count = 0;
12085 	if (stcb->asoc.alternate) {
12086 		chk->whoTo = stcb->asoc.alternate;
12087 	} else {
12088 		chk->whoTo = stcb->asoc.primary_destination;
12089 	}
12090 	ch = mtod(chk->data, struct sctp_chunkhdr *);
12091 	ch->chunk_type = SCTP_STREAM_RESET;
12092 	ch->chunk_flags = 0;
12093 	ch->chunk_length = htons(chk->book_size);
12094 	atomic_add_int(&chk->whoTo->ref_count, 1);
12095 	SCTP_BUF_LEN(chk->data) = chk->send_size;
12096 	seq = stcb->asoc.str_reset_seq_out;
12097 	if (sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1))) {
12098 		seq++;
12099 		asoc->stream_reset_outstanding++;
12100 	} else {
12101 		m_freem(chk->data);
12102 		chk->data = NULL;
12103 		sctp_free_a_chunk(stcb, chk, so_locked);
12104 		return (ENOENT);
12105 	}
12106 	asoc->str_reset = chk;
12107 	/* insert the chunk for sending */
12108 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
12109 	    chk,
12110 	    sctp_next);
12111 	asoc->ctrl_queue_cnt++;
12112 
12113 	if (stcb->asoc.send_sack) {
12114 		sctp_send_sack(stcb, so_locked);
12115 	}
12116 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
12117 	return (0);
12118 }
12119 
12120 int
12121 sctp_send_str_reset_req(struct sctp_tcb *stcb,
12122     uint16_t number_entries, uint16_t *list,
12123     uint8_t send_in_req,
12124     uint8_t send_tsn_req,
12125     uint8_t add_stream,
12126     uint16_t adding_o,
12127     uint16_t adding_i, uint8_t peer_asked)
12128 {
12129 	struct sctp_association *asoc;
12130 	struct sctp_tmit_chunk *chk;
12131 	struct sctp_chunkhdr *ch;
12132 	int can_send_out_req = 0;
12133 	uint32_t seq;
12134 
12135 	SCTP_TCB_LOCK_ASSERT(stcb);
12136 
12137 	asoc = &stcb->asoc;
12138 	if (asoc->stream_reset_outstanding) {
12139 		/*-
12140 		 * Already one pending, must get ACK back to clear the flag.
12141 		 */
12142 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
12143 		return (EBUSY);
12144 	}
12145 	if ((send_in_req == 0) && (send_tsn_req == 0) &&
12146 	    (add_stream == 0)) {
12147 		/* nothing to do */
12148 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12149 		return (EINVAL);
12150 	}
12151 	if (send_tsn_req && send_in_req) {
12152 		/* error, can't do that */
12153 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12154 		return (EINVAL);
12155 	} else if (send_in_req) {
12156 		can_send_out_req = 1;
12157 	}
12158 	if (number_entries > (MCLBYTES -
12159 	    SCTP_MIN_OVERHEAD -
12160 	    sizeof(struct sctp_chunkhdr) -
12161 	    sizeof(struct sctp_stream_reset_out_request)) /
12162 	    sizeof(uint16_t)) {
12163 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12164 		return (ENOMEM);
12165 	}
12166 	sctp_alloc_a_chunk(stcb, chk);
12167 	if (chk == NULL) {
12168 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12169 		return (ENOMEM);
12170 	}
12171 	chk->copy_by_ref = 0;
12172 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
12173 	chk->rec.chunk_id.can_take_data = 0;
12174 	chk->flags = 0;
12175 	chk->asoc = &stcb->asoc;
12176 	chk->book_size = sizeof(struct sctp_chunkhdr);
12177 	chk->send_size = SCTP_SIZE32(chk->book_size);
12178 	chk->book_size_scale = 0;
12179 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
12180 	if (chk->data == NULL) {
12181 		sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
12182 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12183 		return (ENOMEM);
12184 	}
12185 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12186 
12187 	/* setup chunk parameters */
12188 	chk->sent = SCTP_DATAGRAM_UNSENT;
12189 	chk->snd_count = 0;
12190 	if (stcb->asoc.alternate) {
12191 		chk->whoTo = stcb->asoc.alternate;
12192 	} else {
12193 		chk->whoTo = stcb->asoc.primary_destination;
12194 	}
12195 	atomic_add_int(&chk->whoTo->ref_count, 1);
12196 	ch = mtod(chk->data, struct sctp_chunkhdr *);
12197 	ch->chunk_type = SCTP_STREAM_RESET;
12198 	ch->chunk_flags = 0;
12199 	ch->chunk_length = htons(chk->book_size);
12200 	SCTP_BUF_LEN(chk->data) = chk->send_size;
12201 
12202 	seq = stcb->asoc.str_reset_seq_out;
12203 	if (can_send_out_req) {
12204 		int ret;
12205 
12206 		ret = sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1));
12207 		if (ret) {
12208 			seq++;
12209 			asoc->stream_reset_outstanding++;
12210 		}
12211 	}
12212 	if ((add_stream & 1) &&
12213 	    ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < adding_o)) {
12214 		/* Need to allocate more */
12215 		struct sctp_stream_out *oldstream;
12216 		struct sctp_stream_queue_pending *sp, *nsp;
12217 		int i;
12218 #if defined(SCTP_DETAILED_STR_STATS)
12219 		int j;
12220 #endif
12221 
12222 		oldstream = stcb->asoc.strmout;
12223 		/* get some more */
12224 		SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
12225 		    (stcb->asoc.streamoutcnt + adding_o) * sizeof(struct sctp_stream_out),
12226 		    SCTP_M_STRMO);
12227 		if (stcb->asoc.strmout == NULL) {
12228 			uint8_t x;
12229 
12230 			stcb->asoc.strmout = oldstream;
12231 			/* Turn off the bit */
12232 			x = add_stream & 0xfe;
12233 			add_stream = x;
12234 			goto skip_stuff;
12235 		}
12236 		/*
12237 		 * Ok now we proceed with copying the old out stuff and
12238 		 * initializing the new stuff.
12239 		 */
12240 		stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, false);
12241 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
12242 			TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
12243 			/* FIX ME FIX ME */
12244 			/*
12245 			 * This should be a SS_COPY operation FIX ME STREAM
12246 			 * SCHEDULER EXPERT
12247 			 */
12248 			stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], &oldstream[i]);
12249 			stcb->asoc.strmout[i].chunks_on_queues = oldstream[i].chunks_on_queues;
12250 #if defined(SCTP_DETAILED_STR_STATS)
12251 			for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
12252 				stcb->asoc.strmout[i].abandoned_sent[j] = oldstream[i].abandoned_sent[j];
12253 				stcb->asoc.strmout[i].abandoned_unsent[j] = oldstream[i].abandoned_unsent[j];
12254 			}
12255 #else
12256 			stcb->asoc.strmout[i].abandoned_sent[0] = oldstream[i].abandoned_sent[0];
12257 			stcb->asoc.strmout[i].abandoned_unsent[0] = oldstream[i].abandoned_unsent[0];
12258 #endif
12259 			stcb->asoc.strmout[i].next_mid_ordered = oldstream[i].next_mid_ordered;
12260 			stcb->asoc.strmout[i].next_mid_unordered = oldstream[i].next_mid_unordered;
12261 			stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
12262 			stcb->asoc.strmout[i].sid = i;
12263 			stcb->asoc.strmout[i].state = oldstream[i].state;
12264 			/* now anything on those queues? */
12265 			TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) {
12266 				TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
12267 				TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
12268 			}
12269 		}
12270 		/* now the new streams */
12271 		stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc);
12272 		for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + adding_o); i++) {
12273 			TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
12274 			stcb->asoc.strmout[i].chunks_on_queues = 0;
12275 #if defined(SCTP_DETAILED_STR_STATS)
12276 			for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
12277 				stcb->asoc.strmout[i].abandoned_sent[j] = 0;
12278 				stcb->asoc.strmout[i].abandoned_unsent[j] = 0;
12279 			}
12280 #else
12281 			stcb->asoc.strmout[i].abandoned_sent[0] = 0;
12282 			stcb->asoc.strmout[i].abandoned_unsent[0] = 0;
12283 #endif
12284 			stcb->asoc.strmout[i].next_mid_ordered = 0;
12285 			stcb->asoc.strmout[i].next_mid_unordered = 0;
12286 			stcb->asoc.strmout[i].sid = i;
12287 			stcb->asoc.strmout[i].last_msg_incomplete = 0;
12288 			stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL);
12289 			stcb->asoc.strmout[i].state = SCTP_STREAM_CLOSED;
12290 		}
12291 		stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + adding_o;
12292 		SCTP_FREE(oldstream, SCTP_M_STRMO);
12293 	}
12294 skip_stuff:
12295 	if ((add_stream & 1) && (adding_o > 0)) {
12296 		asoc->strm_pending_add_size = adding_o;
12297 		asoc->peer_req_out = peer_asked;
12298 		sctp_add_an_out_stream(chk, seq, adding_o);
12299 		seq++;
12300 		asoc->stream_reset_outstanding++;
12301 	}
12302 	if ((add_stream & 2) && (adding_i > 0)) {
12303 		sctp_add_an_in_stream(chk, seq, adding_i);
12304 		seq++;
12305 		asoc->stream_reset_outstanding++;
12306 	}
12307 	if (send_in_req) {
12308 		sctp_add_stream_reset_in(chk, number_entries, list, seq);
12309 		seq++;
12310 		asoc->stream_reset_outstanding++;
12311 	}
12312 	if (send_tsn_req) {
12313 		sctp_add_stream_reset_tsn(chk, seq);
12314 		asoc->stream_reset_outstanding++;
12315 	}
12316 	asoc->str_reset = chk;
12317 	/* insert the chunk for sending */
12318 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
12319 	    chk,
12320 	    sctp_next);
12321 	asoc->ctrl_queue_cnt++;
12322 	if (stcb->asoc.send_sack) {
12323 		sctp_send_sack(stcb, SCTP_SO_LOCKED);
12324 	}
12325 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
12326 	return (0);
12327 }
12328 
12329 void
12330 sctp_send_abort(struct mbuf *m, int iphlen, struct sockaddr *src, struct sockaddr *dst,
12331     struct sctphdr *sh, uint32_t vtag, struct mbuf *cause,
12332     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
12333     uint32_t vrf_id, uint16_t port)
12334 {
12335 	/* Don't respond to an ABORT with an ABORT. */
12336 	if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
12337 		if (cause)
12338 			sctp_m_freem(cause);
12339 		return;
12340 	}
12341 	sctp_send_resp_msg(src, dst, sh, vtag, SCTP_ABORT_ASSOCIATION, cause,
12342 	    mflowtype, mflowid, fibnum,
12343 	    vrf_id, port);
12344 	return;
12345 }
12346 
12347 void
12348 sctp_send_operr_to(struct sockaddr *src, struct sockaddr *dst,
12349     struct sctphdr *sh, uint32_t vtag, struct mbuf *cause,
12350     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
12351     uint32_t vrf_id, uint16_t port)
12352 {
12353 	sctp_send_resp_msg(src, dst, sh, vtag, SCTP_OPERATION_ERROR, cause,
12354 	    mflowtype, mflowid, fibnum,
12355 	    vrf_id, port);
12356 	return;
12357 }
12358 
12359 static struct mbuf *
12360 sctp_copy_resume(struct uio *uio,
12361     int max_send_len,
12362     int user_marks_eor,
12363     int *error,
12364     uint32_t *sndout,
12365     struct mbuf **new_tail)
12366 {
12367 	struct mbuf *m;
12368 
12369 	m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
12370 	    (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
12371 	if (m == NULL) {
12372 		/* The only possible error is EFAULT. */
12373 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12374 		*error = EFAULT;
12375 	} else {
12376 		*sndout = m_length(m, NULL);
12377 		*new_tail = m_last(m);
12378 	}
12379 	return (m);
12380 }
12381 
12382 static int
12383 sctp_copy_one(struct sctp_stream_queue_pending *sp,
12384     struct uio *uio,
12385     int resv_upfront)
12386 {
12387 	sp->data = m_uiotombuf(uio, M_WAITOK, sp->length, resv_upfront, 0);
12388 	if (sp->data == NULL) {
12389 		/* The only possible error is EFAULT. */
12390 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12391 		return (EFAULT);
12392 	}
12393 	sp->tail_mbuf = m_last(sp->data);
12394 	return (0);
12395 }
12396 
12397 static struct sctp_stream_queue_pending *
12398 sctp_copy_it_in(struct sctp_tcb *stcb,
12399     struct sctp_association *asoc,
12400     struct sctp_nonpad_sndrcvinfo *srcv,
12401     struct uio *uio,
12402     struct sctp_nets *net,
12403     ssize_t max_send_len,
12404     int user_marks_eor,
12405     int *error)
12406 {
12407 
12408 	/*-
12409 	 * This routine must be very careful in its work. Protocol
12410 	 * processing is up and running so care must be taken to spl...()
12411 	 * when you need to do something that may effect the stcb/asoc. The
12412 	 * sb is locked however. When data is copied the protocol processing
12413 	 * should be enabled since this is a slower operation...
12414 	 */
12415 	struct sctp_stream_queue_pending *sp;
12416 	int resv_in_first;
12417 
12418 	*error = 0;
12419 	sctp_alloc_a_strmoq(stcb, sp);
12420 	if (sp == NULL) {
12421 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12422 		*error = ENOMEM;
12423 		goto out_now;
12424 	}
12425 	sp->act_flags = 0;
12426 	sp->sender_all_done = 0;
12427 	sp->sinfo_flags = srcv->sinfo_flags;
12428 	sp->timetolive = srcv->sinfo_timetolive;
12429 	sp->ppid = srcv->sinfo_ppid;
12430 	sp->context = srcv->sinfo_context;
12431 	sp->fsn = 0;
12432 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12433 	sp->sid = srcv->sinfo_stream;
12434 	sp->length = (uint32_t)min(uio->uio_resid, max_send_len);
12435 	if ((sp->length == (uint32_t)uio->uio_resid) &&
12436 	    ((user_marks_eor == 0) ||
12437 	    (srcv->sinfo_flags & SCTP_EOF) ||
12438 	    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12439 		sp->msg_is_complete = 1;
12440 	} else {
12441 		sp->msg_is_complete = 0;
12442 	}
12443 	sp->sender_all_done = 0;
12444 	sp->some_taken = 0;
12445 	sp->put_last_out = 0;
12446 	resv_in_first = SCTP_DATA_CHUNK_OVERHEAD(stcb);
12447 	sp->data = sp->tail_mbuf = NULL;
12448 	if (sp->length == 0) {
12449 		goto skip_copy;
12450 	}
12451 	if (srcv->sinfo_keynumber_valid) {
12452 		sp->auth_keyid = srcv->sinfo_keynumber;
12453 	} else {
12454 		sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12455 	}
12456 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12457 		sctp_auth_key_acquire(stcb, sp->auth_keyid);
12458 		sp->holds_key_ref = 1;
12459 	}
12460 	*error = sctp_copy_one(sp, uio, resv_in_first);
12461 skip_copy:
12462 	if (*error) {
12463 		sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
12464 		sp = NULL;
12465 	} else {
12466 		if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12467 			sp->net = net;
12468 			atomic_add_int(&sp->net->ref_count, 1);
12469 		} else {
12470 			sp->net = NULL;
12471 		}
12472 		sctp_set_prsctp_policy(sp);
12473 	}
12474 out_now:
12475 	return (sp);
12476 }
12477 
12478 int
12479 sctp_sosend(struct socket *so,
12480     struct sockaddr *addr,
12481     struct uio *uio,
12482     struct mbuf *top,
12483     struct mbuf *control,
12484     int flags,
12485     struct thread *p)
12486 {
12487 	struct sctp_sndrcvinfo sndrcvninfo;
12488 #if defined(INET) && defined(INET6)
12489 	struct sockaddr_in sin;
12490 #endif
12491 	struct sockaddr *addr_to_use;
12492 	int error;
12493 	bool use_sndinfo;
12494 
12495 	if (control != NULL) {
12496 		/* process cmsg snd/rcv info (maybe a assoc-id) */
12497 		use_sndinfo = sctp_find_cmsg(SCTP_SNDRCV, (void *)&sndrcvninfo, control, sizeof(sndrcvninfo));
12498 	} else {
12499 		use_sndinfo = false;
12500 	}
12501 #if defined(INET) && defined(INET6)
12502 	if ((addr != NULL) && (addr->sa_family == AF_INET6)) {
12503 		struct sockaddr_in6 *sin6;
12504 
12505 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
12506 			SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12507 			return (EINVAL);
12508 		}
12509 		sin6 = (struct sockaddr_in6 *)addr;
12510 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12511 			in6_sin6_2_sin(&sin, sin6);
12512 			addr_to_use = (struct sockaddr *)&sin;
12513 		} else {
12514 			addr_to_use = addr;
12515 		}
12516 	} else {
12517 		addr_to_use = addr;
12518 	}
12519 #else
12520 	addr_to_use = addr;
12521 #endif
12522 	error = sctp_lower_sosend(so, addr_to_use, uio, top, control, flags,
12523 	    use_sndinfo ? &sndrcvninfo : NULL, p);
12524 	return (error);
12525 }
12526 
12527 int
12528 sctp_lower_sosend(struct socket *so,
12529     struct sockaddr *addr,
12530     struct uio *uio,
12531     struct mbuf *top,
12532     struct mbuf *control,
12533     int flags,
12534     struct sctp_sndrcvinfo *srcv,
12535     struct thread *p)
12536 {
12537 	struct sctp_nonpad_sndrcvinfo sndrcvninfo_buf;
12538 	struct epoch_tracker et;
12539 	struct timeval now;
12540 	struct sctp_block_entry be;
12541 	struct sctp_inpcb *inp;
12542 	struct sctp_tcb *stcb = NULL;
12543 	struct sctp_nets *net;
12544 	struct sctp_association *asoc;
12545 	struct sctp_inpcb *t_inp;
12546 	struct sctp_nonpad_sndrcvinfo *sndrcvninfo;
12547 	ssize_t sndlen = 0, max_len, local_add_more;
12548 	ssize_t local_soresv = 0;
12549 	sctp_assoc_t sinfo_assoc_id;
12550 	int user_marks_eor;
12551 	int nagle_applies = 0;
12552 	int error;
12553 	int queue_only = 0, queue_only_for_init = 0;
12554 	int un_sent;
12555 	int now_filled = 0;
12556 	unsigned int inqueue_bytes = 0;
12557 	uint16_t port;
12558 	uint16_t sinfo_flags;
12559 	uint16_t sinfo_stream;
12560 	bool create_lock_applied = false;
12561 	bool free_cnt_applied = false;
12562 	bool some_on_control;
12563 	bool got_all_of_the_send = false;
12564 	bool non_blocking = false;
12565 
12566 	error = 0;
12567 	net = NULL;
12568 	stcb = NULL;
12569 
12570 	if ((uio == NULL) && (top == NULL)) {
12571 		error = EINVAL;
12572 		goto out_unlocked;
12573 	}
12574 	if (addr != NULL) {
12575 		union sctp_sockstore *raddr = (union sctp_sockstore *)addr;
12576 
12577 		switch (raddr->sa.sa_family) {
12578 #ifdef INET
12579 		case AF_INET:
12580 			if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) {
12581 				error = EINVAL;
12582 				goto out_unlocked;
12583 			}
12584 			port = raddr->sin.sin_port;
12585 			break;
12586 #endif
12587 #ifdef INET6
12588 		case AF_INET6:
12589 			if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) {
12590 				error = EINVAL;
12591 				goto out_unlocked;
12592 			}
12593 			port = raddr->sin6.sin6_port;
12594 			break;
12595 #endif
12596 		default:
12597 			error = EAFNOSUPPORT;
12598 			goto out_unlocked;
12599 		}
12600 	} else {
12601 		port = 0;
12602 	}
12603 	if (uio != NULL) {
12604 		if (uio->uio_resid < 0) {
12605 			error = EINVAL;
12606 			goto out_unlocked;
12607 		}
12608 		sndlen = uio->uio_resid;
12609 	} else {
12610 		sndlen = SCTP_HEADER_LEN(top);
12611 	}
12612 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %zd\n",
12613 	    (void *)addr, sndlen);
12614 
12615 	t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12616 	if (inp == NULL) {
12617 		error = EINVAL;
12618 		goto out_unlocked;
12619 	}
12620 	user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12621 	if ((uio == NULL) && (user_marks_eor != 0)) {
12622 		/*-
12623 		 * We do not support eeor mode for
12624 		 * sending with mbuf chains (like sendfile).
12625 		 */
12626 		error = EINVAL;
12627 		goto out_unlocked;
12628 	}
12629 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12630 	    SCTP_IS_LISTENING(inp)) {
12631 		/* The listener can NOT send. */
12632 		error = EINVAL;
12633 		goto out_unlocked;
12634 	}
12635 	atomic_add_int(&inp->total_sends, 1);
12636 
12637 	if (srcv != NULL) {
12638 		sndrcvninfo = (struct sctp_nonpad_sndrcvinfo *)srcv;
12639 		sinfo_assoc_id = sndrcvninfo->sinfo_assoc_id;
12640 		sinfo_flags = sndrcvninfo->sinfo_flags;
12641 		if (INVALID_SINFO_FLAG(sinfo_flags) ||
12642 		    PR_SCTP_INVALID_POLICY(sinfo_flags)) {
12643 			error = EINVAL;
12644 			goto out_unlocked;
12645 		}
12646 		if (sinfo_flags != 0) {
12647 			SCTP_STAT_INCR(sctps_sends_with_flags);
12648 		}
12649 	} else {
12650 		sndrcvninfo = NULL;
12651 		sinfo_flags = inp->def_send.sinfo_flags;
12652 		sinfo_assoc_id = inp->def_send.sinfo_assoc_id;
12653 	}
12654 	if (flags & MSG_EOR) {
12655 		sinfo_flags |= SCTP_EOR;
12656 	}
12657 	if (flags & MSG_EOF) {
12658 		sinfo_flags |= SCTP_EOF;
12659 	}
12660 	if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) {
12661 		error = EINVAL;
12662 		goto out_unlocked;
12663 	}
12664 	SCTP_INP_RLOCK(inp);
12665 	if ((sinfo_flags & SCTP_SENDALL) &&
12666 	    (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
12667 		SCTP_INP_RUNLOCK(inp);
12668 		error = sctp_sendall(inp, uio, top, sndrcvninfo);
12669 		top = NULL;
12670 		goto out_unlocked;
12671 	}
12672 	/* Now we must find the association. */
12673 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12674 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12675 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
12676 		if (stcb != NULL) {
12677 			SCTP_TCB_LOCK(stcb);
12678 		}
12679 		SCTP_INP_RUNLOCK(inp);
12680 	} else if (sinfo_assoc_id > SCTP_ALL_ASSOC) {
12681 		stcb = sctp_findasoc_ep_asocid_locked(inp, sinfo_assoc_id, 1);
12682 		SCTP_INP_RUNLOCK(inp);
12683 		if (stcb != NULL) {
12684 			SCTP_TCB_LOCK_ASSERT(stcb);
12685 		}
12686 	} else if (addr != NULL) {
12687 		/*-
12688 		 * Since we did not use findep we must
12689 		 * increment it, and if we don't find a tcb
12690 		 * decrement it.
12691 		 */
12692 		SCTP_INP_INCR_REF(inp);
12693 		SCTP_INP_RUNLOCK(inp);
12694 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12695 		if (stcb == NULL) {
12696 			SCTP_INP_WLOCK(inp);
12697 			SCTP_INP_DECR_REF(inp);
12698 			SCTP_INP_WUNLOCK(inp);
12699 		} else {
12700 			SCTP_TCB_LOCK_ASSERT(stcb);
12701 		}
12702 	} else {
12703 		SCTP_INP_RUNLOCK(inp);
12704 	}
12705 
12706 #ifdef INVARIANTS
12707 	if (stcb != NULL) {
12708 		SCTP_TCB_LOCK_ASSERT(stcb);
12709 	}
12710 #endif
12711 
12712 	if ((stcb == NULL) && (addr != NULL)) {
12713 		/* Possible implicit send? */
12714 		SCTP_ASOC_CREATE_LOCK(inp);
12715 		create_lock_applied = true;
12716 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12717 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12718 			error = EINVAL;
12719 			goto out_unlocked;
12720 		}
12721 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12722 		    (addr->sa_family == AF_INET6)) {
12723 			error = EINVAL;
12724 			goto out_unlocked;
12725 		}
12726 		SCTP_INP_WLOCK(inp);
12727 		SCTP_INP_INCR_REF(inp);
12728 		SCTP_INP_WUNLOCK(inp);
12729 		/* With the lock applied look again */
12730 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12731 #if defined(INET) || defined(INET6)
12732 		if ((stcb == NULL) && (control != NULL) && (port > 0)) {
12733 			stcb = sctp_findassociation_cmsgs(&t_inp, port, control, &net, &error);
12734 		}
12735 #endif
12736 		if (stcb == NULL) {
12737 			SCTP_INP_WLOCK(inp);
12738 			SCTP_INP_DECR_REF(inp);
12739 			SCTP_INP_WUNLOCK(inp);
12740 		} else {
12741 			SCTP_TCB_LOCK_ASSERT(stcb);
12742 			SCTP_ASOC_CREATE_UNLOCK(inp);
12743 			create_lock_applied = false;
12744 		}
12745 		if (error != 0) {
12746 			goto out_unlocked;
12747 		}
12748 		if (t_inp != inp) {
12749 			error = ENOTCONN;
12750 			goto out_unlocked;
12751 		}
12752 	}
12753 	if (stcb == NULL) {
12754 		if (addr == NULL) {
12755 			error = ENOENT;
12756 			goto out_unlocked;
12757 		} else {
12758 			/* We must go ahead and start the INIT process */
12759 			uint32_t vrf_id;
12760 
12761 			if ((sinfo_flags & SCTP_ABORT) ||
12762 			    ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) {
12763 				/*-
12764 				 * User asks to abort a non-existent assoc,
12765 				 * or EOF a non-existent assoc with no data
12766 				 */
12767 				error = ENOENT;
12768 				goto out_unlocked;
12769 			}
12770 			/* get an asoc/stcb struct */
12771 			vrf_id = inp->def_vrf_id;
12772 			KASSERT(create_lock_applied, ("create_lock_applied is false"));
12773 			stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id,
12774 			    inp->sctp_ep.pre_open_stream_count,
12775 			    inp->sctp_ep.port,
12776 			    p,
12777 			    SCTP_INITIALIZE_AUTH_PARAMS);
12778 			if (stcb == NULL) {
12779 				/* error is setup for us in the call. */
12780 				KASSERT(error != 0, ("error is 0 although stcb is NULL"));
12781 				goto out_unlocked;
12782 			}
12783 			SCTP_TCB_LOCK_ASSERT(stcb);
12784 			SCTP_ASOC_CREATE_UNLOCK(inp);
12785 			create_lock_applied = false;
12786 			/*
12787 			 * Turn on queue only flag to prevent data from
12788 			 * being sent
12789 			 */
12790 			queue_only = 1;
12791 			SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
12792 			(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
12793 			if (control != NULL) {
12794 				if (sctp_process_cmsgs_for_init(stcb, control, &error)) {
12795 					sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
12796 					    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
12797 					stcb = NULL;
12798 					KASSERT(error != 0,
12799 					    ("error is 0 although sctp_process_cmsgs_for_init() indicated an error"));
12800 					goto out_unlocked;
12801 				}
12802 			}
12803 			/* out with the INIT */
12804 			queue_only_for_init = 1;
12805 			/*-
12806 			 * we may want to dig in after this call and adjust the MTU
12807 			 * value. It defaulted to 1500 (constant) but the ro
12808 			 * structure may now have an update and thus we may need to
12809 			 * change it BEFORE we append the message.
12810 			 */
12811 		}
12812 	}
12813 
12814 	KASSERT(!create_lock_applied, ("create_lock_applied is true"));
12815 	KASSERT(stcb != NULL, ("stcb is NULL"));
12816 	SCTP_TCB_LOCK_ASSERT(stcb);
12817 
12818 	asoc = &stcb->asoc;
12819 	if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
12820 	    (asoc->state & SCTP_STATE_WAS_ABORTED)) {
12821 		if (asoc->state & SCTP_STATE_WAS_ABORTED) {
12822 			/* XXX: Could also be ECONNABORTED, not enough info. */
12823 			error = ECONNRESET;
12824 		} else {
12825 			error = ENOTCONN;
12826 		}
12827 		goto out_unlocked;
12828 	}
12829 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
12830 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
12831 		queue_only = 1;
12832 	}
12833 	/* Keep the stcb from being freed under our feet. */
12834 	atomic_add_int(&asoc->refcnt, 1);
12835 	free_cnt_applied = true;
12836 	if (sndrcvninfo == NULL) {
12837 		/* Use a local copy to have a consistent view. */
12838 		sndrcvninfo_buf = asoc->def_send;
12839 		sndrcvninfo = &sndrcvninfo_buf;
12840 		sinfo_flags = sndrcvninfo->sinfo_flags;
12841 		if (flags & MSG_EOR) {
12842 			sinfo_flags |= SCTP_EOR;
12843 		}
12844 		if (flags & MSG_EOF) {
12845 			sinfo_flags |= SCTP_EOF;
12846 		}
12847 	}
12848 	/* Are we aborting? */
12849 	if (sinfo_flags & SCTP_ABORT) {
12850 		struct mbuf *mm;
12851 		struct sctp_paramhdr *ph;
12852 		ssize_t tot_demand, tot_out = 0, max_out;
12853 
12854 		SCTP_STAT_INCR(sctps_sends_with_abort);
12855 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
12856 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
12857 			/* It has to be up before we abort. */
12858 			error = EINVAL;
12859 			goto out_unlocked;
12860 		}
12861 		/* How big is the user initiated abort? */
12862 		if (top != NULL) {
12863 			struct mbuf *cntm;
12864 
12865 			if (sndlen != 0) {
12866 				for (cntm = top; cntm; cntm = SCTP_BUF_NEXT(cntm)) {
12867 					tot_out += SCTP_BUF_LEN(cntm);
12868 				}
12869 			}
12870 			mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
12871 		} else {
12872 			/* Must fit in a MTU */
12873 			tot_out = sndlen;
12874 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12875 			if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12876 				error = EMSGSIZE;
12877 				goto out_unlocked;
12878 			}
12879 			mm = sctp_get_mbuf_for_msg((unsigned int)tot_demand, 0, M_NOWAIT, 1, MT_DATA);
12880 		}
12881 		if (mm == NULL) {
12882 			error = ENOMEM;
12883 			goto out_unlocked;
12884 		}
12885 		max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12886 		max_out -= sizeof(struct sctp_abort_msg);
12887 		if (tot_out > max_out) {
12888 			tot_out = max_out;
12889 		}
12890 		ph = mtod(mm, struct sctp_paramhdr *);
12891 		ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12892 		ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + tot_out));
12893 		ph++;
12894 		SCTP_BUF_LEN(mm) = (int)(tot_out + sizeof(struct sctp_paramhdr));
12895 		if (top == NULL) {
12896 			SCTP_TCB_UNLOCK(stcb);
12897 			error = uiomove((caddr_t)ph, (int)tot_out, uio);
12898 			SCTP_TCB_LOCK(stcb);
12899 			if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
12900 			    (asoc->state & SCTP_STATE_WAS_ABORTED)) {
12901 				sctp_m_freem(mm);
12902 				if (asoc->state & SCTP_STATE_WAS_ABORTED) {
12903 					/*
12904 					 * XXX: Could also be ECONNABORTED,
12905 					 * not enough info.
12906 					 */
12907 					error = ECONNRESET;
12908 				} else {
12909 					error = ENOTCONN;
12910 				}
12911 				goto out_unlocked;
12912 			}
12913 			if (error != 0) {
12914 				/*-
12915 				 * Here if we can't get his data we
12916 				 * still abort we just don't get to
12917 				 * send the users note :-0
12918 				 */
12919 				sctp_m_freem(mm);
12920 				mm = NULL;
12921 				error = 0;
12922 			}
12923 		} else {
12924 			if (sndlen != 0) {
12925 				SCTP_BUF_NEXT(mm) = top;
12926 			}
12927 		}
12928 		atomic_subtract_int(&asoc->refcnt, 1);
12929 		free_cnt_applied = false;
12930 		/* release this lock, otherwise we hang on ourselves */
12931 		NET_EPOCH_ENTER(et);
12932 		sctp_abort_an_association(stcb->sctp_ep, stcb, mm, false, SCTP_SO_LOCKED);
12933 		NET_EPOCH_EXIT(et);
12934 		stcb = NULL;
12935 		/*
12936 		 * In this case top is already chained to mm avoid double
12937 		 * free, since we free it below if top != NULL and driver
12938 		 * would free it after sending the packet out
12939 		 */
12940 		if (sndlen != 0) {
12941 			top = NULL;
12942 		}
12943 		goto out_unlocked;
12944 	}
12945 
12946 	KASSERT(stcb != NULL, ("stcb is NULL"));
12947 	SCTP_TCB_LOCK_ASSERT(stcb);
12948 	KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
12949 	    ("Association about to be freed"));
12950 	KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
12951 	    ("Association was aborted"));
12952 
12953 	if (sinfo_flags & SCTP_ADDR_OVER) {
12954 		if (addr != NULL) {
12955 			net = sctp_findnet(stcb, addr);
12956 		} else {
12957 			net = NULL;
12958 		}
12959 		if ((net == NULL) ||
12960 		    ((port != 0) && (port != stcb->rport))) {
12961 			error = EINVAL;
12962 			goto out_unlocked;
12963 		}
12964 	} else {
12965 		if (asoc->alternate != NULL) {
12966 			net = asoc->alternate;
12967 		} else {
12968 			net = asoc->primary_destination;
12969 		}
12970 	}
12971 	if (sndlen == 0) {
12972 		if (sinfo_flags & SCTP_EOF) {
12973 			got_all_of_the_send = true;
12974 			goto dataless_eof;
12975 		} else {
12976 			error = EINVAL;
12977 			goto out_unlocked;
12978 		}
12979 	}
12980 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12981 		if (sndlen > (ssize_t)asoc->smallest_mtu) {
12982 			error = EMSGSIZE;
12983 			goto out_unlocked;
12984 		}
12985 	}
12986 	sinfo_stream = sndrcvninfo->sinfo_stream;
12987 	/* Is the stream no. valid? */
12988 	if (sinfo_stream >= asoc->streamoutcnt) {
12989 		/* Invalid stream number */
12990 		error = EINVAL;
12991 		goto out_unlocked;
12992 	}
12993 	if ((asoc->strmout[sinfo_stream].state != SCTP_STREAM_OPEN) &&
12994 	    (asoc->strmout[sinfo_stream].state != SCTP_STREAM_OPENING)) {
12995 		/*
12996 		 * Can't queue any data while stream reset is underway.
12997 		 */
12998 		if (asoc->strmout[sinfo_stream].state > SCTP_STREAM_OPEN) {
12999 			error = EAGAIN;
13000 		} else {
13001 			error = EINVAL;
13002 		}
13003 		goto out_unlocked;
13004 	}
13005 	atomic_add_int(&stcb->total_sends, 1);
13006 	if (SCTP_SO_IS_NBIO(so) || (flags & (MSG_NBIO | MSG_DONTWAIT)) != 0) {
13007 		non_blocking = true;
13008 	}
13009 	if (non_blocking) {
13010 		ssize_t amount;
13011 
13012 		inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13013 		if (user_marks_eor == 0) {
13014 			amount = sndlen;
13015 		} else {
13016 			amount = 1;
13017 		}
13018 		if ((SCTP_SB_LIMIT_SND(so) < (amount + inqueue_bytes + asoc->sb_send_resv)) ||
13019 		    (asoc->chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13020 			if ((sndlen > (ssize_t)SCTP_SB_LIMIT_SND(so)) &&
13021 			    (user_marks_eor == 0)) {
13022 				error = EMSGSIZE;
13023 			} else {
13024 				error = EWOULDBLOCK;
13025 			}
13026 			goto out_unlocked;
13027 		}
13028 	}
13029 	atomic_add_int(&asoc->sb_send_resv, (int)sndlen);
13030 	local_soresv = sndlen;
13031 
13032 	KASSERT(stcb != NULL, ("stcb is NULL"));
13033 	SCTP_TCB_LOCK_ASSERT(stcb);
13034 	KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13035 	    ("Association about to be freed"));
13036 	KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13037 	    ("Association was aborted"));
13038 
13039 	/* Ok, we will attempt a msgsnd :> */
13040 	if (p != NULL) {
13041 		p->td_ru.ru_msgsnd++;
13042 	}
13043 	/* Calculate the maximum we can send */
13044 	inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13045 	if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13046 		max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13047 	} else {
13048 		max_len = 0;
13049 	}
13050 	/* Unless E_EOR mode is on, we must make a send FIT in one call. */
13051 	if ((user_marks_eor == 0) &&
13052 	    (sndlen > (ssize_t)SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
13053 		/* It will NEVER fit. */
13054 		error = EMSGSIZE;
13055 		goto out_unlocked;
13056 	}
13057 	if (user_marks_eor != 0) {
13058 		local_add_more = (ssize_t)min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
13059 	} else {
13060 		/*-
13061 		 * For non-eeor the whole message must fit in
13062 		 * the socket send buffer.
13063 		 */
13064 		local_add_more = sndlen;
13065 	}
13066 	if (non_blocking) {
13067 		goto skip_preblock;
13068 	}
13069 	if (((max_len <= local_add_more) && ((ssize_t)SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
13070 	    (max_len == 0) ||
13071 	    ((asoc->chunks_on_out_queue + asoc->stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13072 		/* No room right now! */
13073 		inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13074 		SOCKBUF_LOCK(&so->so_snd);
13075 		while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
13076 		    ((asoc->stream_queue_cnt + asoc->chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13077 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %zd) || (%d+%d > %d)\n",
13078 			    (unsigned int)SCTP_SB_LIMIT_SND(so),
13079 			    inqueue_bytes,
13080 			    local_add_more,
13081 			    asoc->stream_queue_cnt,
13082 			    asoc->chunks_on_out_queue,
13083 			    SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
13084 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13085 				sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, asoc, sndlen);
13086 			}
13087 			be.error = 0;
13088 			stcb->block_entry = &be;
13089 			SCTP_TCB_UNLOCK(stcb);
13090 			error = sbwait(so, SO_SND);
13091 			if (error == 0) {
13092 				if (so->so_error != 0) {
13093 					error = so->so_error;
13094 				}
13095 				if (be.error != 0) {
13096 					error = be.error;
13097 				}
13098 			}
13099 			SOCKBUF_UNLOCK(&so->so_snd);
13100 			SCTP_TCB_LOCK(stcb);
13101 			stcb->block_entry = NULL;
13102 			if (error != 0) {
13103 				goto out_unlocked;
13104 			}
13105 			if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13106 			    (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13107 				if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13108 					/*
13109 					 * XXX: Could also be ECONNABORTED,
13110 					 * not enough info.
13111 					 */
13112 					error = ECONNRESET;
13113 				} else {
13114 					error = ENOTCONN;
13115 				}
13116 				goto out_unlocked;
13117 			}
13118 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13119 				sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13120 				    asoc, asoc->total_output_queue_size);
13121 			}
13122 			inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13123 			SOCKBUF_LOCK(&so->so_snd);
13124 		}
13125 		if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13126 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13127 		} else {
13128 			max_len = 0;
13129 		}
13130 		SOCKBUF_UNLOCK(&so->so_snd);
13131 	}
13132 
13133 skip_preblock:
13134 	KASSERT(stcb != NULL, ("stcb is NULL"));
13135 	SCTP_TCB_LOCK_ASSERT(stcb);
13136 	KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13137 	    ("Association about to be freed"));
13138 	KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13139 	    ("Association was aborted"));
13140 
13141 	/*
13142 	 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
13143 	 * case NOTE: uio will be null when top/mbuf is passed
13144 	 */
13145 	if (top == NULL) {
13146 		struct sctp_stream_queue_pending *sp;
13147 		struct sctp_stream_out *strm;
13148 		uint32_t sndout;
13149 
13150 		if ((asoc->stream_locked) &&
13151 		    (asoc->stream_locked_on != sinfo_stream)) {
13152 			error = EINVAL;
13153 			goto out;
13154 		}
13155 		strm = &asoc->strmout[sinfo_stream];
13156 		if (strm->last_msg_incomplete == 0) {
13157 	do_a_copy_in:
13158 			SCTP_TCB_UNLOCK(stcb);
13159 			sp = sctp_copy_it_in(stcb, asoc, sndrcvninfo, uio, net, max_len, user_marks_eor, &error);
13160 			SCTP_TCB_LOCK(stcb);
13161 			if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13162 			    (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13163 				if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13164 					/*
13165 					 * XXX: Could also be ECONNABORTED,
13166 					 * not enough info.
13167 					 */
13168 					error = ECONNRESET;
13169 				} else {
13170 					error = ENOTCONN;
13171 				}
13172 				goto out;
13173 			}
13174 			if (error != 0) {
13175 				goto out;
13176 			}
13177 			/*
13178 			 * Reject the sending of a new user message, if the
13179 			 * association is about to be shut down.
13180 			 */
13181 			if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) ||
13182 			    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
13183 			    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
13184 			    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
13185 				if (sp->data != 0) {
13186 					sctp_m_freem(sp->data);
13187 					sp->data = NULL;
13188 					sp->tail_mbuf = NULL;
13189 					sp->length = 0;
13190 				}
13191 				if (sp->net != NULL) {
13192 					sctp_free_remote_addr(sp->net);
13193 					sp->net = NULL;
13194 				}
13195 				sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
13196 				error = EPIPE;
13197 				goto out_unlocked;
13198 			}
13199 			/* The out streams might be reallocated. */
13200 			strm = &asoc->strmout[sinfo_stream];
13201 			if (sp->msg_is_complete) {
13202 				strm->last_msg_incomplete = 0;
13203 				asoc->stream_locked = 0;
13204 			} else {
13205 				/*
13206 				 * Just got locked to this guy in case of an
13207 				 * interrupt.
13208 				 */
13209 				strm->last_msg_incomplete = 1;
13210 				if (asoc->idata_supported == 0) {
13211 					asoc->stream_locked = 1;
13212 					asoc->stream_locked_on = sinfo_stream;
13213 				}
13214 				sp->sender_all_done = 0;
13215 			}
13216 			sctp_snd_sb_alloc(stcb, sp->length);
13217 			atomic_add_int(&asoc->stream_queue_cnt, 1);
13218 			if (sinfo_flags & SCTP_UNORDERED) {
13219 				SCTP_STAT_INCR(sctps_sends_with_unord);
13220 			}
13221 			sp->processing = 1;
13222 			TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
13223 			asoc->ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp);
13224 		} else {
13225 			sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
13226 			if (sp == NULL) {
13227 				/* ???? Huh ??? last msg is gone */
13228 #ifdef INVARIANTS
13229 				panic("Warning: Last msg marked incomplete, yet nothing left?");
13230 #else
13231 				SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
13232 				strm->last_msg_incomplete = 0;
13233 #endif
13234 				goto do_a_copy_in;
13235 			}
13236 			if (sp->processing != 0) {
13237 				error = EINVAL;
13238 				goto out;
13239 			} else {
13240 				sp->processing = 1;
13241 			}
13242 		}
13243 
13244 		KASSERT(stcb != NULL, ("stcb is NULL"));
13245 		SCTP_TCB_LOCK_ASSERT(stcb);
13246 		KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13247 		    ("Association about to be freed"));
13248 		KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13249 		    ("Association was aborted"));
13250 
13251 		while (uio->uio_resid > 0) {
13252 			/* How much room do we have? */
13253 			struct mbuf *new_tail, *mm;
13254 
13255 			inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13256 			if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13257 				max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13258 			} else {
13259 				max_len = 0;
13260 			}
13261 			if ((max_len > (ssize_t)SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
13262 			    ((max_len > 0) && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
13263 			    (uio->uio_resid <= max_len)) {
13264 				SCTP_TCB_UNLOCK(stcb);
13265 				sndout = 0;
13266 				new_tail = NULL;
13267 				mm = sctp_copy_resume(uio, (int)max_len, user_marks_eor, &error, &sndout, &new_tail);
13268 				SCTP_TCB_LOCK(stcb);
13269 				if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13270 				    (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13271 					/*
13272 					 * We need to get out. Peer probably
13273 					 * aborted.
13274 					 */
13275 					sctp_m_freem(mm);
13276 					if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13277 						/*
13278 						 * XXX: Could also be
13279 						 * ECONNABORTED, not enough
13280 						 * info.
13281 						 */
13282 						error = ECONNRESET;
13283 					} else {
13284 						error = ENOTCONN;
13285 					}
13286 					goto out;
13287 				}
13288 				if ((mm == NULL) || (error != 0)) {
13289 					if (mm != NULL) {
13290 						sctp_m_freem(mm);
13291 					}
13292 					if (sp != NULL) {
13293 						sp->processing = 0;
13294 					}
13295 					goto out;
13296 				}
13297 				/* Update the mbuf and count */
13298 				if (sp->tail_mbuf != NULL) {
13299 					/* Tack it to the end. */
13300 					SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13301 				} else {
13302 					/* A stolen mbuf. */
13303 					sp->data = mm;
13304 				}
13305 				sp->tail_mbuf = new_tail;
13306 				sctp_snd_sb_alloc(stcb, sndout);
13307 				atomic_add_int(&sp->length, sndout);
13308 				if (sinfo_flags & SCTP_SACK_IMMEDIATELY) {
13309 					sp->sinfo_flags |= SCTP_SACK_IMMEDIATELY;
13310 				}
13311 
13312 				/* Did we reach EOR? */
13313 				if ((uio->uio_resid == 0) &&
13314 				    ((user_marks_eor == 0) ||
13315 				    (sinfo_flags & SCTP_EOF) ||
13316 				    (user_marks_eor && (sinfo_flags & SCTP_EOR)))) {
13317 					sp->msg_is_complete = 1;
13318 				} else {
13319 					sp->msg_is_complete = 0;
13320 				}
13321 			}
13322 
13323 			KASSERT(stcb != NULL, ("stcb is NULL"));
13324 			SCTP_TCB_LOCK_ASSERT(stcb);
13325 			KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13326 			    ("Association about to be freed"));
13327 			KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13328 			    ("Association was aborted"));
13329 
13330 			if (uio->uio_resid == 0) {
13331 				/* got it all? */
13332 				continue;
13333 			}
13334 			/* PR-SCTP? */
13335 			if ((asoc->prsctp_supported) && (asoc->sent_queue_cnt_removeable > 0)) {
13336 				/*
13337 				 * This is ugly but we must assure locking
13338 				 * order
13339 				 */
13340 				sctp_prune_prsctp(stcb, asoc, sndrcvninfo, (int)sndlen);
13341 				inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13342 				if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes)
13343 					max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13344 				else
13345 					max_len = 0;
13346 				if (max_len > 0) {
13347 					continue;
13348 				}
13349 			}
13350 			/* wait for space now */
13351 			if (non_blocking) {
13352 				/* Non-blocking io in place out */
13353 				if (sp != NULL) {
13354 					sp->processing = 0;
13355 				}
13356 				goto skip_out_eof;
13357 			}
13358 			/* What about the INIT, send it maybe */
13359 			if (queue_only_for_init) {
13360 				if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
13361 					/* a collision took us forward? */
13362 					queue_only = 0;
13363 				} else {
13364 					NET_EPOCH_ENTER(et);
13365 					sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13366 					NET_EPOCH_EXIT(et);
13367 					SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
13368 					queue_only = 1;
13369 				}
13370 			}
13371 			if ((net->flight_size > net->cwnd) &&
13372 			    (asoc->sctp_cmt_on_off == 0)) {
13373 				SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13374 				queue_only = 1;
13375 			} else if (asoc->ifp_had_enobuf) {
13376 				SCTP_STAT_INCR(sctps_ifnomemqueued);
13377 				if (net->flight_size > (2 * net->mtu)) {
13378 					queue_only = 1;
13379 				}
13380 				asoc->ifp_had_enobuf = 0;
13381 			}
13382 			un_sent = asoc->total_output_queue_size - asoc->total_flight;
13383 			if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13384 			    (asoc->total_flight > 0) &&
13385 			    (asoc->stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13386 			    (un_sent < (int)(asoc->smallest_mtu - SCTP_MIN_OVERHEAD))) {
13387 				/*-
13388 				 * Ok, Nagle is set on and we have data outstanding.
13389 				 * Don't send anything and let SACKs drive out the
13390 				 * data unless we have a "full" segment to send.
13391 				 */
13392 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13393 					sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13394 				}
13395 				SCTP_STAT_INCR(sctps_naglequeued);
13396 				nagle_applies = 1;
13397 			} else {
13398 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13399 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13400 						sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13401 				}
13402 				SCTP_STAT_INCR(sctps_naglesent);
13403 				nagle_applies = 0;
13404 			}
13405 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13406 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13407 				    nagle_applies, un_sent);
13408 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, asoc->total_output_queue_size,
13409 				    asoc->total_flight,
13410 				    asoc->chunks_on_out_queue, asoc->total_flight_count);
13411 			}
13412 			if (queue_only_for_init) {
13413 				queue_only_for_init = 0;
13414 			}
13415 			if ((queue_only == 0) && (nagle_applies == 0)) {
13416 				/*-
13417 				 * need to start chunk output
13418 				 * before blocking.. note that if
13419 				 * a lock is already applied, then
13420 				 * the input via the net is happening
13421 				 * and I don't need to start output :-D
13422 				 */
13423 				NET_EPOCH_ENTER(et);
13424 				sctp_chunk_output(inp, stcb,
13425 				    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13426 				NET_EPOCH_EXIT(et);
13427 			}
13428 			/*-
13429 			 * This is a bit strange, but I think it will
13430 			 * work. The total_output_queue_size is locked and
13431 			 * protected by the TCB_LOCK, which we just released.
13432 			 * There is a race that can occur between releasing it
13433 			 * above, and me getting the socket lock, where sacks
13434 			 * come in but we have not put the SB_WAIT on the
13435 			 * so_snd buffer to get the wakeup. After the LOCK
13436 			 * is applied the sack_processing will also need to
13437 			 * LOCK the so->so_snd to do the actual sowwakeup(). So
13438 			 * once we have the socket buffer lock if we recheck the
13439 			 * size we KNOW we will get to sleep safely with the
13440 			 * wakeup flag in place.
13441 			 */
13442 			inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13443 			SOCKBUF_LOCK(&so->so_snd);
13444 			if (SCTP_SB_LIMIT_SND(so) <= (inqueue_bytes +
13445 			    min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13446 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13447 					sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13448 					    asoc, uio->uio_resid);
13449 				}
13450 				be.error = 0;
13451 				stcb->block_entry = &be;
13452 				SCTP_TCB_UNLOCK(stcb);
13453 				error = sbwait(so, SO_SND);
13454 				if (error == 0) {
13455 					if (so->so_error != 0)
13456 						error = so->so_error;
13457 					if (be.error != 0) {
13458 						error = be.error;
13459 					}
13460 				}
13461 				SOCKBUF_UNLOCK(&so->so_snd);
13462 				SCTP_TCB_LOCK(stcb);
13463 				stcb->block_entry = NULL;
13464 				if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13465 				    (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13466 					if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13467 						/*
13468 						 * XXX: Could also be
13469 						 * ECONNABORTED, not enough
13470 						 * info.
13471 						 */
13472 						error = ECONNRESET;
13473 					} else {
13474 						error = ENOTCONN;
13475 					}
13476 					goto out_unlocked;
13477 				}
13478 				if (error != 0) {
13479 					if (sp != NULL) {
13480 						sp->processing = 0;
13481 					}
13482 					goto out_unlocked;
13483 				}
13484 			} else {
13485 				SOCKBUF_UNLOCK(&so->so_snd);
13486 			}
13487 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13488 				sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13489 				    asoc, asoc->total_output_queue_size);
13490 			}
13491 		}
13492 
13493 		KASSERT(stcb != NULL, ("stcb is NULL"));
13494 		SCTP_TCB_LOCK_ASSERT(stcb);
13495 		KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13496 		    ("Association about to be freed"));
13497 		KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13498 		    ("Association was aborted"));
13499 
13500 		/* The out streams might be reallocated. */
13501 		strm = &asoc->strmout[sinfo_stream];
13502 		if (sp != NULL) {
13503 			if (sp->msg_is_complete == 0) {
13504 				strm->last_msg_incomplete = 1;
13505 				if (asoc->idata_supported == 0) {
13506 					asoc->stream_locked = 1;
13507 					asoc->stream_locked_on = sinfo_stream;
13508 				}
13509 			} else {
13510 				sp->sender_all_done = 1;
13511 				strm->last_msg_incomplete = 0;
13512 				asoc->stream_locked = 0;
13513 			}
13514 			sp->processing = 0;
13515 		} else {
13516 			SCTP_PRINTF("Huh no sp TSNH?\n");
13517 			strm->last_msg_incomplete = 0;
13518 			asoc->stream_locked = 0;
13519 		}
13520 		if (uio->uio_resid == 0) {
13521 			got_all_of_the_send = true;
13522 		}
13523 	} else {
13524 		error = sctp_msg_append(stcb, net, top, sndrcvninfo);
13525 		top = NULL;
13526 		if ((sinfo_flags & SCTP_EOF) != 0) {
13527 			got_all_of_the_send = true;
13528 		}
13529 	}
13530 	if (error != 0) {
13531 		goto out;
13532 	}
13533 
13534 dataless_eof:
13535 	KASSERT(stcb != NULL, ("stcb is NULL"));
13536 	SCTP_TCB_LOCK_ASSERT(stcb);
13537 	KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13538 	    ("Association about to be freed"));
13539 	KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13540 	    ("Association was aborted"));
13541 
13542 	/* EOF thing ? */
13543 	if ((sinfo_flags & SCTP_EOF) && got_all_of_the_send) {
13544 		SCTP_STAT_INCR(sctps_sends_with_eof);
13545 		error = 0;
13546 		if (TAILQ_EMPTY(&asoc->send_queue) &&
13547 		    TAILQ_EMPTY(&asoc->sent_queue) &&
13548 		    sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED) == 0) {
13549 			if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
13550 				goto abort_anyway;
13551 			}
13552 			/* there is nothing queued to send, so I'm done... */
13553 			if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
13554 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13555 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13556 				struct sctp_nets *netp;
13557 
13558 				/* only send SHUTDOWN the first time through */
13559 				if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
13560 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13561 				}
13562 				SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
13563 				sctp_stop_timers_for_shutdown(stcb);
13564 				if (asoc->alternate != NULL) {
13565 					netp = asoc->alternate;
13566 				} else {
13567 					netp = asoc->primary_destination;
13568 				}
13569 				sctp_send_shutdown(stcb, netp);
13570 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13571 				    netp);
13572 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13573 				    NULL);
13574 			}
13575 		} else {
13576 			/*-
13577 			 * we still got (or just got) data to send, so set
13578 			 * SHUTDOWN_PENDING
13579 			 */
13580 			/*-
13581 			 * XXX sockets draft says that SCTP_EOF should be
13582 			 * sent with no data.  currently, we will allow user
13583 			 * data to be sent first and move to
13584 			 * SHUTDOWN-PENDING
13585 			 */
13586 			if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
13587 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13588 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13589 				if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
13590 					SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
13591 				}
13592 				SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
13593 				if (TAILQ_EMPTY(&asoc->send_queue) &&
13594 				    TAILQ_EMPTY(&asoc->sent_queue) &&
13595 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13596 					struct mbuf *op_err;
13597 					char msg[SCTP_DIAG_INFO_LEN];
13598 
13599 			abort_anyway:
13600 					if (free_cnt_applied) {
13601 						atomic_subtract_int(&asoc->refcnt, 1);
13602 						free_cnt_applied = false;
13603 					}
13604 					SCTP_SNPRINTF(msg, sizeof(msg),
13605 					    "%s:%d at %s", __FILE__, __LINE__, __func__);
13606 					op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
13607 					    msg);
13608 					NET_EPOCH_ENTER(et);
13609 					sctp_abort_an_association(stcb->sctp_ep, stcb,
13610 					    op_err, false, SCTP_SO_LOCKED);
13611 					NET_EPOCH_EXIT(et);
13612 					stcb = NULL;
13613 					error = ECONNABORTED;
13614 					goto out;
13615 				}
13616 				sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13617 			}
13618 		}
13619 	}
13620 
13621 skip_out_eof:
13622 	KASSERT(stcb != NULL, ("stcb is NULL"));
13623 	SCTP_TCB_LOCK_ASSERT(stcb);
13624 	KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13625 	    ("Association about to be freed"));
13626 	KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13627 	    ("Association was aborted"));
13628 
13629 	some_on_control = !TAILQ_EMPTY(&asoc->control_send_queue);
13630 	if (queue_only_for_init) {
13631 		if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
13632 			/* a collision took us forward? */
13633 			queue_only = 0;
13634 		} else {
13635 			NET_EPOCH_ENTER(et);
13636 			sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13637 			NET_EPOCH_EXIT(et);
13638 			SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
13639 			queue_only = 1;
13640 		}
13641 	}
13642 
13643 	KASSERT(stcb != NULL, ("stcb is NULL"));
13644 	SCTP_TCB_LOCK_ASSERT(stcb);
13645 	KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13646 	    ("Association about to be freed"));
13647 	KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13648 	    ("Association was aborted"));
13649 
13650 	if ((net->flight_size > net->cwnd) &&
13651 	    (asoc->sctp_cmt_on_off == 0)) {
13652 		SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13653 		queue_only = 1;
13654 	} else if (asoc->ifp_had_enobuf) {
13655 		SCTP_STAT_INCR(sctps_ifnomemqueued);
13656 		if (net->flight_size > (2 * net->mtu)) {
13657 			queue_only = 1;
13658 		}
13659 		asoc->ifp_had_enobuf = 0;
13660 	}
13661 	un_sent = asoc->total_output_queue_size - asoc->total_flight;
13662 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13663 	    (asoc->total_flight > 0) &&
13664 	    (asoc->stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13665 	    (un_sent < (int)(asoc->smallest_mtu - SCTP_MIN_OVERHEAD))) {
13666 		/*-
13667 		 * Ok, Nagle is set on and we have data outstanding.
13668 		 * Don't send anything and let SACKs drive out the
13669 		 * data unless wen have a "full" segment to send.
13670 		 */
13671 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13672 			sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13673 		}
13674 		SCTP_STAT_INCR(sctps_naglequeued);
13675 		nagle_applies = 1;
13676 	} else {
13677 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13678 			if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13679 				sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13680 		}
13681 		SCTP_STAT_INCR(sctps_naglesent);
13682 		nagle_applies = 0;
13683 	}
13684 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13685 		sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13686 		    nagle_applies, un_sent);
13687 		sctp_misc_ints(SCTP_CWNDLOG_PRESEND, asoc->total_output_queue_size,
13688 		    asoc->total_flight,
13689 		    asoc->chunks_on_out_queue, asoc->total_flight_count);
13690 	}
13691 
13692 	KASSERT(stcb != NULL, ("stcb is NULL"));
13693 	SCTP_TCB_LOCK_ASSERT(stcb);
13694 	KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13695 	    ("Association about to be freed"));
13696 	KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13697 	    ("Association was aborted"));
13698 
13699 	NET_EPOCH_ENTER(et);
13700 	if ((queue_only == 0) && (nagle_applies == 0) && (asoc->peers_rwnd && un_sent)) {
13701 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13702 	} else if ((queue_only == 0) &&
13703 		    (asoc->peers_rwnd == 0) &&
13704 	    (asoc->total_flight == 0)) {
13705 		/* We get to have a probe outstanding */
13706 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13707 	} else if (some_on_control) {
13708 		int num_out, reason;
13709 
13710 		/* Here we do control only */
13711 		(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out,
13712 		    &reason, 1, 1, &now, &now_filled,
13713 		    sctp_get_frag_point(stcb),
13714 		    SCTP_SO_LOCKED);
13715 	}
13716 	NET_EPOCH_EXIT(et);
13717 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13718 	    queue_only, asoc->peers_rwnd, un_sent,
13719 	    asoc->total_flight, asoc->chunks_on_out_queue,
13720 	    asoc->total_output_queue_size, error);
13721 
13722 	KASSERT(stcb != NULL, ("stcb is NULL"));
13723 	SCTP_TCB_LOCK_ASSERT(stcb);
13724 	KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13725 	    ("Association about to be freed"));
13726 	KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13727 	    ("Association was aborted"));
13728 
13729 out:
13730 out_unlocked:
13731 	if (create_lock_applied) {
13732 		SCTP_ASOC_CREATE_UNLOCK(inp);
13733 	}
13734 	if (stcb != NULL) {
13735 		if (local_soresv) {
13736 			atomic_subtract_int(&asoc->sb_send_resv, (int)sndlen);
13737 		}
13738 		if (free_cnt_applied) {
13739 			atomic_subtract_int(&asoc->refcnt, 1);
13740 		}
13741 		SCTP_TCB_UNLOCK(stcb);
13742 	}
13743 	if (top != NULL) {
13744 		sctp_m_freem(top);
13745 	}
13746 	if (control != NULL) {
13747 		sctp_m_freem(control);
13748 	}
13749 	SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, error);
13750 	return (error);
13751 }
13752 
13753 /*
13754  * generate an AUTHentication chunk, if required
13755  */
13756 struct mbuf *
13757 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13758     struct sctp_auth_chunk **auth_ret, uint32_t *offset,
13759     struct sctp_tcb *stcb, uint8_t chunk)
13760 {
13761 	struct mbuf *m_auth;
13762 	struct sctp_auth_chunk *auth;
13763 	int chunk_len;
13764 	struct mbuf *cn;
13765 
13766 	if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13767 	    (stcb == NULL))
13768 		return (m);
13769 
13770 	if (stcb->asoc.auth_supported == 0) {
13771 		return (m);
13772 	}
13773 	/* does the requested chunk require auth? */
13774 	if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13775 		return (m);
13776 	}
13777 	m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_NOWAIT, 1, MT_HEADER);
13778 	if (m_auth == NULL) {
13779 		/* no mbuf's */
13780 		return (m);
13781 	}
13782 	/* reserve some space if this will be the first mbuf */
13783 	if (m == NULL)
13784 		SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13785 	/* fill in the AUTH chunk details */
13786 	auth = mtod(m_auth, struct sctp_auth_chunk *);
13787 	memset(auth, 0, sizeof(*auth));
13788 	auth->ch.chunk_type = SCTP_AUTHENTICATION;
13789 	auth->ch.chunk_flags = 0;
13790 	chunk_len = sizeof(*auth) +
13791 	    sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13792 	auth->ch.chunk_length = htons(chunk_len);
13793 	auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13794 	/* key id and hmac digest will be computed and filled in upon send */
13795 
13796 	/* save the offset where the auth was inserted into the chain */
13797 	*offset = 0;
13798 	for (cn = m; cn; cn = SCTP_BUF_NEXT(cn)) {
13799 		*offset += SCTP_BUF_LEN(cn);
13800 	}
13801 
13802 	/* update length and return pointer to the auth chunk */
13803 	SCTP_BUF_LEN(m_auth) = chunk_len;
13804 	m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13805 	if (auth_ret != NULL)
13806 		*auth_ret = auth;
13807 
13808 	return (m);
13809 }
13810 
13811 #ifdef INET6
13812 int
13813 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t *ro)
13814 {
13815 	struct nd_prefix *pfx = NULL;
13816 	struct nd_pfxrouter *pfxrtr = NULL;
13817 	struct sockaddr_in6 gw6;
13818 
13819 	if (ro == NULL || ro->ro_nh == NULL || src6->sin6_family != AF_INET6)
13820 		return (0);
13821 
13822 	/* get prefix entry of address */
13823 	ND6_RLOCK();
13824 	LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13825 		if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13826 			continue;
13827 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13828 		    &src6->sin6_addr, &pfx->ndpr_mask))
13829 			break;
13830 	}
13831 	/* no prefix entry in the prefix list */
13832 	if (pfx == NULL) {
13833 		ND6_RUNLOCK();
13834 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13835 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13836 		return (0);
13837 	}
13838 
13839 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13840 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13841 
13842 	/* search installed gateway from prefix entry */
13843 	LIST_FOREACH(pfxrtr, &pfx->ndpr_advrtrs, pfr_entry) {
13844 		memset(&gw6, 0, sizeof(struct sockaddr_in6));
13845 		gw6.sin6_family = AF_INET6;
13846 		gw6.sin6_len = sizeof(struct sockaddr_in6);
13847 		memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13848 		    sizeof(struct in6_addr));
13849 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13850 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13851 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13852 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ro->ro_nh->gw_sa);
13853 		if (sctp_cmpaddr((struct sockaddr *)&gw6, &ro->ro_nh->gw_sa)) {
13854 			ND6_RUNLOCK();
13855 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13856 			return (1);
13857 		}
13858 	}
13859 	ND6_RUNLOCK();
13860 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13861 	return (0);
13862 }
13863 #endif
13864 
13865 int
13866 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t *ro)
13867 {
13868 #ifdef INET
13869 	struct sockaddr_in *sin, *mask;
13870 	struct ifaddr *ifa;
13871 	struct in_addr srcnetaddr, gwnetaddr;
13872 
13873 	if (ro == NULL || ro->ro_nh == NULL ||
13874 	    sifa->address.sa.sa_family != AF_INET) {
13875 		return (0);
13876 	}
13877 	ifa = (struct ifaddr *)sifa->ifa;
13878 	mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13879 	sin = &sifa->address.sin;
13880 	srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13881 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13882 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13883 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13884 
13885 	sin = &ro->ro_nh->gw4_sa;
13886 	gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13887 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13888 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ro->ro_nh->gw_sa);
13889 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13890 	if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13891 		return (1);
13892 	}
13893 #endif
13894 	return (0);
13895 }
13896