xref: /freebsd/sys/netinet/sctp_output.c (revision 6486b015fc84e96725fef22b0e3363351399ae83)
1 /*-
2  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4  * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * a) Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  * b) Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the distribution.
15  *
16  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $	 */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <netinet/sctp_os.h>
39 #include <sys/proc.h>
40 #include <netinet/sctp_var.h>
41 #include <netinet/sctp_sysctl.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctp_pcb.h>
44 #include <netinet/sctputil.h>
45 #include <netinet/sctp_output.h>
46 #include <netinet/sctp_uio.h>
47 #include <netinet/sctputil.h>
48 #include <netinet/sctp_auth.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_asconf.h>
51 #include <netinet/sctp_indata.h>
52 #include <netinet/sctp_bsd_addr.h>
53 #include <netinet/sctp_input.h>
54 #include <netinet/sctp_crc32.h>
55 #include <netinet/udp.h>
56 #include <netinet/udp_var.h>
57 #include <machine/in_cksum.h>
58 
59 
60 
61 #define SCTP_MAX_GAPS_INARRAY 4
62 struct sack_track {
63 	uint8_t right_edge;	/* mergable on the right edge */
64 	uint8_t left_edge;	/* mergable on the left edge */
65 	uint8_t num_entries;
66 	uint8_t spare;
67 	struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
68 };
69 
70 struct sack_track sack_array[256] = {
71 	{0, 0, 0, 0,		/* 0x00 */
72 		{{0, 0},
73 		{0, 0},
74 		{0, 0},
75 		{0, 0}
76 		}
77 	},
78 	{1, 0, 1, 0,		/* 0x01 */
79 		{{0, 0},
80 		{0, 0},
81 		{0, 0},
82 		{0, 0}
83 		}
84 	},
85 	{0, 0, 1, 0,		/* 0x02 */
86 		{{1, 1},
87 		{0, 0},
88 		{0, 0},
89 		{0, 0}
90 		}
91 	},
92 	{1, 0, 1, 0,		/* 0x03 */
93 		{{0, 1},
94 		{0, 0},
95 		{0, 0},
96 		{0, 0}
97 		}
98 	},
99 	{0, 0, 1, 0,		/* 0x04 */
100 		{{2, 2},
101 		{0, 0},
102 		{0, 0},
103 		{0, 0}
104 		}
105 	},
106 	{1, 0, 2, 0,		/* 0x05 */
107 		{{0, 0},
108 		{2, 2},
109 		{0, 0},
110 		{0, 0}
111 		}
112 	},
113 	{0, 0, 1, 0,		/* 0x06 */
114 		{{1, 2},
115 		{0, 0},
116 		{0, 0},
117 		{0, 0}
118 		}
119 	},
120 	{1, 0, 1, 0,		/* 0x07 */
121 		{{0, 2},
122 		{0, 0},
123 		{0, 0},
124 		{0, 0}
125 		}
126 	},
127 	{0, 0, 1, 0,		/* 0x08 */
128 		{{3, 3},
129 		{0, 0},
130 		{0, 0},
131 		{0, 0}
132 		}
133 	},
134 	{1, 0, 2, 0,		/* 0x09 */
135 		{{0, 0},
136 		{3, 3},
137 		{0, 0},
138 		{0, 0}
139 		}
140 	},
141 	{0, 0, 2, 0,		/* 0x0a */
142 		{{1, 1},
143 		{3, 3},
144 		{0, 0},
145 		{0, 0}
146 		}
147 	},
148 	{1, 0, 2, 0,		/* 0x0b */
149 		{{0, 1},
150 		{3, 3},
151 		{0, 0},
152 		{0, 0}
153 		}
154 	},
155 	{0, 0, 1, 0,		/* 0x0c */
156 		{{2, 3},
157 		{0, 0},
158 		{0, 0},
159 		{0, 0}
160 		}
161 	},
162 	{1, 0, 2, 0,		/* 0x0d */
163 		{{0, 0},
164 		{2, 3},
165 		{0, 0},
166 		{0, 0}
167 		}
168 	},
169 	{0, 0, 1, 0,		/* 0x0e */
170 		{{1, 3},
171 		{0, 0},
172 		{0, 0},
173 		{0, 0}
174 		}
175 	},
176 	{1, 0, 1, 0,		/* 0x0f */
177 		{{0, 3},
178 		{0, 0},
179 		{0, 0},
180 		{0, 0}
181 		}
182 	},
183 	{0, 0, 1, 0,		/* 0x10 */
184 		{{4, 4},
185 		{0, 0},
186 		{0, 0},
187 		{0, 0}
188 		}
189 	},
190 	{1, 0, 2, 0,		/* 0x11 */
191 		{{0, 0},
192 		{4, 4},
193 		{0, 0},
194 		{0, 0}
195 		}
196 	},
197 	{0, 0, 2, 0,		/* 0x12 */
198 		{{1, 1},
199 		{4, 4},
200 		{0, 0},
201 		{0, 0}
202 		}
203 	},
204 	{1, 0, 2, 0,		/* 0x13 */
205 		{{0, 1},
206 		{4, 4},
207 		{0, 0},
208 		{0, 0}
209 		}
210 	},
211 	{0, 0, 2, 0,		/* 0x14 */
212 		{{2, 2},
213 		{4, 4},
214 		{0, 0},
215 		{0, 0}
216 		}
217 	},
218 	{1, 0, 3, 0,		/* 0x15 */
219 		{{0, 0},
220 		{2, 2},
221 		{4, 4},
222 		{0, 0}
223 		}
224 	},
225 	{0, 0, 2, 0,		/* 0x16 */
226 		{{1, 2},
227 		{4, 4},
228 		{0, 0},
229 		{0, 0}
230 		}
231 	},
232 	{1, 0, 2, 0,		/* 0x17 */
233 		{{0, 2},
234 		{4, 4},
235 		{0, 0},
236 		{0, 0}
237 		}
238 	},
239 	{0, 0, 1, 0,		/* 0x18 */
240 		{{3, 4},
241 		{0, 0},
242 		{0, 0},
243 		{0, 0}
244 		}
245 	},
246 	{1, 0, 2, 0,		/* 0x19 */
247 		{{0, 0},
248 		{3, 4},
249 		{0, 0},
250 		{0, 0}
251 		}
252 	},
253 	{0, 0, 2, 0,		/* 0x1a */
254 		{{1, 1},
255 		{3, 4},
256 		{0, 0},
257 		{0, 0}
258 		}
259 	},
260 	{1, 0, 2, 0,		/* 0x1b */
261 		{{0, 1},
262 		{3, 4},
263 		{0, 0},
264 		{0, 0}
265 		}
266 	},
267 	{0, 0, 1, 0,		/* 0x1c */
268 		{{2, 4},
269 		{0, 0},
270 		{0, 0},
271 		{0, 0}
272 		}
273 	},
274 	{1, 0, 2, 0,		/* 0x1d */
275 		{{0, 0},
276 		{2, 4},
277 		{0, 0},
278 		{0, 0}
279 		}
280 	},
281 	{0, 0, 1, 0,		/* 0x1e */
282 		{{1, 4},
283 		{0, 0},
284 		{0, 0},
285 		{0, 0}
286 		}
287 	},
288 	{1, 0, 1, 0,		/* 0x1f */
289 		{{0, 4},
290 		{0, 0},
291 		{0, 0},
292 		{0, 0}
293 		}
294 	},
295 	{0, 0, 1, 0,		/* 0x20 */
296 		{{5, 5},
297 		{0, 0},
298 		{0, 0},
299 		{0, 0}
300 		}
301 	},
302 	{1, 0, 2, 0,		/* 0x21 */
303 		{{0, 0},
304 		{5, 5},
305 		{0, 0},
306 		{0, 0}
307 		}
308 	},
309 	{0, 0, 2, 0,		/* 0x22 */
310 		{{1, 1},
311 		{5, 5},
312 		{0, 0},
313 		{0, 0}
314 		}
315 	},
316 	{1, 0, 2, 0,		/* 0x23 */
317 		{{0, 1},
318 		{5, 5},
319 		{0, 0},
320 		{0, 0}
321 		}
322 	},
323 	{0, 0, 2, 0,		/* 0x24 */
324 		{{2, 2},
325 		{5, 5},
326 		{0, 0},
327 		{0, 0}
328 		}
329 	},
330 	{1, 0, 3, 0,		/* 0x25 */
331 		{{0, 0},
332 		{2, 2},
333 		{5, 5},
334 		{0, 0}
335 		}
336 	},
337 	{0, 0, 2, 0,		/* 0x26 */
338 		{{1, 2},
339 		{5, 5},
340 		{0, 0},
341 		{0, 0}
342 		}
343 	},
344 	{1, 0, 2, 0,		/* 0x27 */
345 		{{0, 2},
346 		{5, 5},
347 		{0, 0},
348 		{0, 0}
349 		}
350 	},
351 	{0, 0, 2, 0,		/* 0x28 */
352 		{{3, 3},
353 		{5, 5},
354 		{0, 0},
355 		{0, 0}
356 		}
357 	},
358 	{1, 0, 3, 0,		/* 0x29 */
359 		{{0, 0},
360 		{3, 3},
361 		{5, 5},
362 		{0, 0}
363 		}
364 	},
365 	{0, 0, 3, 0,		/* 0x2a */
366 		{{1, 1},
367 		{3, 3},
368 		{5, 5},
369 		{0, 0}
370 		}
371 	},
372 	{1, 0, 3, 0,		/* 0x2b */
373 		{{0, 1},
374 		{3, 3},
375 		{5, 5},
376 		{0, 0}
377 		}
378 	},
379 	{0, 0, 2, 0,		/* 0x2c */
380 		{{2, 3},
381 		{5, 5},
382 		{0, 0},
383 		{0, 0}
384 		}
385 	},
386 	{1, 0, 3, 0,		/* 0x2d */
387 		{{0, 0},
388 		{2, 3},
389 		{5, 5},
390 		{0, 0}
391 		}
392 	},
393 	{0, 0, 2, 0,		/* 0x2e */
394 		{{1, 3},
395 		{5, 5},
396 		{0, 0},
397 		{0, 0}
398 		}
399 	},
400 	{1, 0, 2, 0,		/* 0x2f */
401 		{{0, 3},
402 		{5, 5},
403 		{0, 0},
404 		{0, 0}
405 		}
406 	},
407 	{0, 0, 1, 0,		/* 0x30 */
408 		{{4, 5},
409 		{0, 0},
410 		{0, 0},
411 		{0, 0}
412 		}
413 	},
414 	{1, 0, 2, 0,		/* 0x31 */
415 		{{0, 0},
416 		{4, 5},
417 		{0, 0},
418 		{0, 0}
419 		}
420 	},
421 	{0, 0, 2, 0,		/* 0x32 */
422 		{{1, 1},
423 		{4, 5},
424 		{0, 0},
425 		{0, 0}
426 		}
427 	},
428 	{1, 0, 2, 0,		/* 0x33 */
429 		{{0, 1},
430 		{4, 5},
431 		{0, 0},
432 		{0, 0}
433 		}
434 	},
435 	{0, 0, 2, 0,		/* 0x34 */
436 		{{2, 2},
437 		{4, 5},
438 		{0, 0},
439 		{0, 0}
440 		}
441 	},
442 	{1, 0, 3, 0,		/* 0x35 */
443 		{{0, 0},
444 		{2, 2},
445 		{4, 5},
446 		{0, 0}
447 		}
448 	},
449 	{0, 0, 2, 0,		/* 0x36 */
450 		{{1, 2},
451 		{4, 5},
452 		{0, 0},
453 		{0, 0}
454 		}
455 	},
456 	{1, 0, 2, 0,		/* 0x37 */
457 		{{0, 2},
458 		{4, 5},
459 		{0, 0},
460 		{0, 0}
461 		}
462 	},
463 	{0, 0, 1, 0,		/* 0x38 */
464 		{{3, 5},
465 		{0, 0},
466 		{0, 0},
467 		{0, 0}
468 		}
469 	},
470 	{1, 0, 2, 0,		/* 0x39 */
471 		{{0, 0},
472 		{3, 5},
473 		{0, 0},
474 		{0, 0}
475 		}
476 	},
477 	{0, 0, 2, 0,		/* 0x3a */
478 		{{1, 1},
479 		{3, 5},
480 		{0, 0},
481 		{0, 0}
482 		}
483 	},
484 	{1, 0, 2, 0,		/* 0x3b */
485 		{{0, 1},
486 		{3, 5},
487 		{0, 0},
488 		{0, 0}
489 		}
490 	},
491 	{0, 0, 1, 0,		/* 0x3c */
492 		{{2, 5},
493 		{0, 0},
494 		{0, 0},
495 		{0, 0}
496 		}
497 	},
498 	{1, 0, 2, 0,		/* 0x3d */
499 		{{0, 0},
500 		{2, 5},
501 		{0, 0},
502 		{0, 0}
503 		}
504 	},
505 	{0, 0, 1, 0,		/* 0x3e */
506 		{{1, 5},
507 		{0, 0},
508 		{0, 0},
509 		{0, 0}
510 		}
511 	},
512 	{1, 0, 1, 0,		/* 0x3f */
513 		{{0, 5},
514 		{0, 0},
515 		{0, 0},
516 		{0, 0}
517 		}
518 	},
519 	{0, 0, 1, 0,		/* 0x40 */
520 		{{6, 6},
521 		{0, 0},
522 		{0, 0},
523 		{0, 0}
524 		}
525 	},
526 	{1, 0, 2, 0,		/* 0x41 */
527 		{{0, 0},
528 		{6, 6},
529 		{0, 0},
530 		{0, 0}
531 		}
532 	},
533 	{0, 0, 2, 0,		/* 0x42 */
534 		{{1, 1},
535 		{6, 6},
536 		{0, 0},
537 		{0, 0}
538 		}
539 	},
540 	{1, 0, 2, 0,		/* 0x43 */
541 		{{0, 1},
542 		{6, 6},
543 		{0, 0},
544 		{0, 0}
545 		}
546 	},
547 	{0, 0, 2, 0,		/* 0x44 */
548 		{{2, 2},
549 		{6, 6},
550 		{0, 0},
551 		{0, 0}
552 		}
553 	},
554 	{1, 0, 3, 0,		/* 0x45 */
555 		{{0, 0},
556 		{2, 2},
557 		{6, 6},
558 		{0, 0}
559 		}
560 	},
561 	{0, 0, 2, 0,		/* 0x46 */
562 		{{1, 2},
563 		{6, 6},
564 		{0, 0},
565 		{0, 0}
566 		}
567 	},
568 	{1, 0, 2, 0,		/* 0x47 */
569 		{{0, 2},
570 		{6, 6},
571 		{0, 0},
572 		{0, 0}
573 		}
574 	},
575 	{0, 0, 2, 0,		/* 0x48 */
576 		{{3, 3},
577 		{6, 6},
578 		{0, 0},
579 		{0, 0}
580 		}
581 	},
582 	{1, 0, 3, 0,		/* 0x49 */
583 		{{0, 0},
584 		{3, 3},
585 		{6, 6},
586 		{0, 0}
587 		}
588 	},
589 	{0, 0, 3, 0,		/* 0x4a */
590 		{{1, 1},
591 		{3, 3},
592 		{6, 6},
593 		{0, 0}
594 		}
595 	},
596 	{1, 0, 3, 0,		/* 0x4b */
597 		{{0, 1},
598 		{3, 3},
599 		{6, 6},
600 		{0, 0}
601 		}
602 	},
603 	{0, 0, 2, 0,		/* 0x4c */
604 		{{2, 3},
605 		{6, 6},
606 		{0, 0},
607 		{0, 0}
608 		}
609 	},
610 	{1, 0, 3, 0,		/* 0x4d */
611 		{{0, 0},
612 		{2, 3},
613 		{6, 6},
614 		{0, 0}
615 		}
616 	},
617 	{0, 0, 2, 0,		/* 0x4e */
618 		{{1, 3},
619 		{6, 6},
620 		{0, 0},
621 		{0, 0}
622 		}
623 	},
624 	{1, 0, 2, 0,		/* 0x4f */
625 		{{0, 3},
626 		{6, 6},
627 		{0, 0},
628 		{0, 0}
629 		}
630 	},
631 	{0, 0, 2, 0,		/* 0x50 */
632 		{{4, 4},
633 		{6, 6},
634 		{0, 0},
635 		{0, 0}
636 		}
637 	},
638 	{1, 0, 3, 0,		/* 0x51 */
639 		{{0, 0},
640 		{4, 4},
641 		{6, 6},
642 		{0, 0}
643 		}
644 	},
645 	{0, 0, 3, 0,		/* 0x52 */
646 		{{1, 1},
647 		{4, 4},
648 		{6, 6},
649 		{0, 0}
650 		}
651 	},
652 	{1, 0, 3, 0,		/* 0x53 */
653 		{{0, 1},
654 		{4, 4},
655 		{6, 6},
656 		{0, 0}
657 		}
658 	},
659 	{0, 0, 3, 0,		/* 0x54 */
660 		{{2, 2},
661 		{4, 4},
662 		{6, 6},
663 		{0, 0}
664 		}
665 	},
666 	{1, 0, 4, 0,		/* 0x55 */
667 		{{0, 0},
668 		{2, 2},
669 		{4, 4},
670 		{6, 6}
671 		}
672 	},
673 	{0, 0, 3, 0,		/* 0x56 */
674 		{{1, 2},
675 		{4, 4},
676 		{6, 6},
677 		{0, 0}
678 		}
679 	},
680 	{1, 0, 3, 0,		/* 0x57 */
681 		{{0, 2},
682 		{4, 4},
683 		{6, 6},
684 		{0, 0}
685 		}
686 	},
687 	{0, 0, 2, 0,		/* 0x58 */
688 		{{3, 4},
689 		{6, 6},
690 		{0, 0},
691 		{0, 0}
692 		}
693 	},
694 	{1, 0, 3, 0,		/* 0x59 */
695 		{{0, 0},
696 		{3, 4},
697 		{6, 6},
698 		{0, 0}
699 		}
700 	},
701 	{0, 0, 3, 0,		/* 0x5a */
702 		{{1, 1},
703 		{3, 4},
704 		{6, 6},
705 		{0, 0}
706 		}
707 	},
708 	{1, 0, 3, 0,		/* 0x5b */
709 		{{0, 1},
710 		{3, 4},
711 		{6, 6},
712 		{0, 0}
713 		}
714 	},
715 	{0, 0, 2, 0,		/* 0x5c */
716 		{{2, 4},
717 		{6, 6},
718 		{0, 0},
719 		{0, 0}
720 		}
721 	},
722 	{1, 0, 3, 0,		/* 0x5d */
723 		{{0, 0},
724 		{2, 4},
725 		{6, 6},
726 		{0, 0}
727 		}
728 	},
729 	{0, 0, 2, 0,		/* 0x5e */
730 		{{1, 4},
731 		{6, 6},
732 		{0, 0},
733 		{0, 0}
734 		}
735 	},
736 	{1, 0, 2, 0,		/* 0x5f */
737 		{{0, 4},
738 		{6, 6},
739 		{0, 0},
740 		{0, 0}
741 		}
742 	},
743 	{0, 0, 1, 0,		/* 0x60 */
744 		{{5, 6},
745 		{0, 0},
746 		{0, 0},
747 		{0, 0}
748 		}
749 	},
750 	{1, 0, 2, 0,		/* 0x61 */
751 		{{0, 0},
752 		{5, 6},
753 		{0, 0},
754 		{0, 0}
755 		}
756 	},
757 	{0, 0, 2, 0,		/* 0x62 */
758 		{{1, 1},
759 		{5, 6},
760 		{0, 0},
761 		{0, 0}
762 		}
763 	},
764 	{1, 0, 2, 0,		/* 0x63 */
765 		{{0, 1},
766 		{5, 6},
767 		{0, 0},
768 		{0, 0}
769 		}
770 	},
771 	{0, 0, 2, 0,		/* 0x64 */
772 		{{2, 2},
773 		{5, 6},
774 		{0, 0},
775 		{0, 0}
776 		}
777 	},
778 	{1, 0, 3, 0,		/* 0x65 */
779 		{{0, 0},
780 		{2, 2},
781 		{5, 6},
782 		{0, 0}
783 		}
784 	},
785 	{0, 0, 2, 0,		/* 0x66 */
786 		{{1, 2},
787 		{5, 6},
788 		{0, 0},
789 		{0, 0}
790 		}
791 	},
792 	{1, 0, 2, 0,		/* 0x67 */
793 		{{0, 2},
794 		{5, 6},
795 		{0, 0},
796 		{0, 0}
797 		}
798 	},
799 	{0, 0, 2, 0,		/* 0x68 */
800 		{{3, 3},
801 		{5, 6},
802 		{0, 0},
803 		{0, 0}
804 		}
805 	},
806 	{1, 0, 3, 0,		/* 0x69 */
807 		{{0, 0},
808 		{3, 3},
809 		{5, 6},
810 		{0, 0}
811 		}
812 	},
813 	{0, 0, 3, 0,		/* 0x6a */
814 		{{1, 1},
815 		{3, 3},
816 		{5, 6},
817 		{0, 0}
818 		}
819 	},
820 	{1, 0, 3, 0,		/* 0x6b */
821 		{{0, 1},
822 		{3, 3},
823 		{5, 6},
824 		{0, 0}
825 		}
826 	},
827 	{0, 0, 2, 0,		/* 0x6c */
828 		{{2, 3},
829 		{5, 6},
830 		{0, 0},
831 		{0, 0}
832 		}
833 	},
834 	{1, 0, 3, 0,		/* 0x6d */
835 		{{0, 0},
836 		{2, 3},
837 		{5, 6},
838 		{0, 0}
839 		}
840 	},
841 	{0, 0, 2, 0,		/* 0x6e */
842 		{{1, 3},
843 		{5, 6},
844 		{0, 0},
845 		{0, 0}
846 		}
847 	},
848 	{1, 0, 2, 0,		/* 0x6f */
849 		{{0, 3},
850 		{5, 6},
851 		{0, 0},
852 		{0, 0}
853 		}
854 	},
855 	{0, 0, 1, 0,		/* 0x70 */
856 		{{4, 6},
857 		{0, 0},
858 		{0, 0},
859 		{0, 0}
860 		}
861 	},
862 	{1, 0, 2, 0,		/* 0x71 */
863 		{{0, 0},
864 		{4, 6},
865 		{0, 0},
866 		{0, 0}
867 		}
868 	},
869 	{0, 0, 2, 0,		/* 0x72 */
870 		{{1, 1},
871 		{4, 6},
872 		{0, 0},
873 		{0, 0}
874 		}
875 	},
876 	{1, 0, 2, 0,		/* 0x73 */
877 		{{0, 1},
878 		{4, 6},
879 		{0, 0},
880 		{0, 0}
881 		}
882 	},
883 	{0, 0, 2, 0,		/* 0x74 */
884 		{{2, 2},
885 		{4, 6},
886 		{0, 0},
887 		{0, 0}
888 		}
889 	},
890 	{1, 0, 3, 0,		/* 0x75 */
891 		{{0, 0},
892 		{2, 2},
893 		{4, 6},
894 		{0, 0}
895 		}
896 	},
897 	{0, 0, 2, 0,		/* 0x76 */
898 		{{1, 2},
899 		{4, 6},
900 		{0, 0},
901 		{0, 0}
902 		}
903 	},
904 	{1, 0, 2, 0,		/* 0x77 */
905 		{{0, 2},
906 		{4, 6},
907 		{0, 0},
908 		{0, 0}
909 		}
910 	},
911 	{0, 0, 1, 0,		/* 0x78 */
912 		{{3, 6},
913 		{0, 0},
914 		{0, 0},
915 		{0, 0}
916 		}
917 	},
918 	{1, 0, 2, 0,		/* 0x79 */
919 		{{0, 0},
920 		{3, 6},
921 		{0, 0},
922 		{0, 0}
923 		}
924 	},
925 	{0, 0, 2, 0,		/* 0x7a */
926 		{{1, 1},
927 		{3, 6},
928 		{0, 0},
929 		{0, 0}
930 		}
931 	},
932 	{1, 0, 2, 0,		/* 0x7b */
933 		{{0, 1},
934 		{3, 6},
935 		{0, 0},
936 		{0, 0}
937 		}
938 	},
939 	{0, 0, 1, 0,		/* 0x7c */
940 		{{2, 6},
941 		{0, 0},
942 		{0, 0},
943 		{0, 0}
944 		}
945 	},
946 	{1, 0, 2, 0,		/* 0x7d */
947 		{{0, 0},
948 		{2, 6},
949 		{0, 0},
950 		{0, 0}
951 		}
952 	},
953 	{0, 0, 1, 0,		/* 0x7e */
954 		{{1, 6},
955 		{0, 0},
956 		{0, 0},
957 		{0, 0}
958 		}
959 	},
960 	{1, 0, 1, 0,		/* 0x7f */
961 		{{0, 6},
962 		{0, 0},
963 		{0, 0},
964 		{0, 0}
965 		}
966 	},
967 	{0, 1, 1, 0,		/* 0x80 */
968 		{{7, 7},
969 		{0, 0},
970 		{0, 0},
971 		{0, 0}
972 		}
973 	},
974 	{1, 1, 2, 0,		/* 0x81 */
975 		{{0, 0},
976 		{7, 7},
977 		{0, 0},
978 		{0, 0}
979 		}
980 	},
981 	{0, 1, 2, 0,		/* 0x82 */
982 		{{1, 1},
983 		{7, 7},
984 		{0, 0},
985 		{0, 0}
986 		}
987 	},
988 	{1, 1, 2, 0,		/* 0x83 */
989 		{{0, 1},
990 		{7, 7},
991 		{0, 0},
992 		{0, 0}
993 		}
994 	},
995 	{0, 1, 2, 0,		/* 0x84 */
996 		{{2, 2},
997 		{7, 7},
998 		{0, 0},
999 		{0, 0}
1000 		}
1001 	},
1002 	{1, 1, 3, 0,		/* 0x85 */
1003 		{{0, 0},
1004 		{2, 2},
1005 		{7, 7},
1006 		{0, 0}
1007 		}
1008 	},
1009 	{0, 1, 2, 0,		/* 0x86 */
1010 		{{1, 2},
1011 		{7, 7},
1012 		{0, 0},
1013 		{0, 0}
1014 		}
1015 	},
1016 	{1, 1, 2, 0,		/* 0x87 */
1017 		{{0, 2},
1018 		{7, 7},
1019 		{0, 0},
1020 		{0, 0}
1021 		}
1022 	},
1023 	{0, 1, 2, 0,		/* 0x88 */
1024 		{{3, 3},
1025 		{7, 7},
1026 		{0, 0},
1027 		{0, 0}
1028 		}
1029 	},
1030 	{1, 1, 3, 0,		/* 0x89 */
1031 		{{0, 0},
1032 		{3, 3},
1033 		{7, 7},
1034 		{0, 0}
1035 		}
1036 	},
1037 	{0, 1, 3, 0,		/* 0x8a */
1038 		{{1, 1},
1039 		{3, 3},
1040 		{7, 7},
1041 		{0, 0}
1042 		}
1043 	},
1044 	{1, 1, 3, 0,		/* 0x8b */
1045 		{{0, 1},
1046 		{3, 3},
1047 		{7, 7},
1048 		{0, 0}
1049 		}
1050 	},
1051 	{0, 1, 2, 0,		/* 0x8c */
1052 		{{2, 3},
1053 		{7, 7},
1054 		{0, 0},
1055 		{0, 0}
1056 		}
1057 	},
1058 	{1, 1, 3, 0,		/* 0x8d */
1059 		{{0, 0},
1060 		{2, 3},
1061 		{7, 7},
1062 		{0, 0}
1063 		}
1064 	},
1065 	{0, 1, 2, 0,		/* 0x8e */
1066 		{{1, 3},
1067 		{7, 7},
1068 		{0, 0},
1069 		{0, 0}
1070 		}
1071 	},
1072 	{1, 1, 2, 0,		/* 0x8f */
1073 		{{0, 3},
1074 		{7, 7},
1075 		{0, 0},
1076 		{0, 0}
1077 		}
1078 	},
1079 	{0, 1, 2, 0,		/* 0x90 */
1080 		{{4, 4},
1081 		{7, 7},
1082 		{0, 0},
1083 		{0, 0}
1084 		}
1085 	},
1086 	{1, 1, 3, 0,		/* 0x91 */
1087 		{{0, 0},
1088 		{4, 4},
1089 		{7, 7},
1090 		{0, 0}
1091 		}
1092 	},
1093 	{0, 1, 3, 0,		/* 0x92 */
1094 		{{1, 1},
1095 		{4, 4},
1096 		{7, 7},
1097 		{0, 0}
1098 		}
1099 	},
1100 	{1, 1, 3, 0,		/* 0x93 */
1101 		{{0, 1},
1102 		{4, 4},
1103 		{7, 7},
1104 		{0, 0}
1105 		}
1106 	},
1107 	{0, 1, 3, 0,		/* 0x94 */
1108 		{{2, 2},
1109 		{4, 4},
1110 		{7, 7},
1111 		{0, 0}
1112 		}
1113 	},
1114 	{1, 1, 4, 0,		/* 0x95 */
1115 		{{0, 0},
1116 		{2, 2},
1117 		{4, 4},
1118 		{7, 7}
1119 		}
1120 	},
1121 	{0, 1, 3, 0,		/* 0x96 */
1122 		{{1, 2},
1123 		{4, 4},
1124 		{7, 7},
1125 		{0, 0}
1126 		}
1127 	},
1128 	{1, 1, 3, 0,		/* 0x97 */
1129 		{{0, 2},
1130 		{4, 4},
1131 		{7, 7},
1132 		{0, 0}
1133 		}
1134 	},
1135 	{0, 1, 2, 0,		/* 0x98 */
1136 		{{3, 4},
1137 		{7, 7},
1138 		{0, 0},
1139 		{0, 0}
1140 		}
1141 	},
1142 	{1, 1, 3, 0,		/* 0x99 */
1143 		{{0, 0},
1144 		{3, 4},
1145 		{7, 7},
1146 		{0, 0}
1147 		}
1148 	},
1149 	{0, 1, 3, 0,		/* 0x9a */
1150 		{{1, 1},
1151 		{3, 4},
1152 		{7, 7},
1153 		{0, 0}
1154 		}
1155 	},
1156 	{1, 1, 3, 0,		/* 0x9b */
1157 		{{0, 1},
1158 		{3, 4},
1159 		{7, 7},
1160 		{0, 0}
1161 		}
1162 	},
1163 	{0, 1, 2, 0,		/* 0x9c */
1164 		{{2, 4},
1165 		{7, 7},
1166 		{0, 0},
1167 		{0, 0}
1168 		}
1169 	},
1170 	{1, 1, 3, 0,		/* 0x9d */
1171 		{{0, 0},
1172 		{2, 4},
1173 		{7, 7},
1174 		{0, 0}
1175 		}
1176 	},
1177 	{0, 1, 2, 0,		/* 0x9e */
1178 		{{1, 4},
1179 		{7, 7},
1180 		{0, 0},
1181 		{0, 0}
1182 		}
1183 	},
1184 	{1, 1, 2, 0,		/* 0x9f */
1185 		{{0, 4},
1186 		{7, 7},
1187 		{0, 0},
1188 		{0, 0}
1189 		}
1190 	},
1191 	{0, 1, 2, 0,		/* 0xa0 */
1192 		{{5, 5},
1193 		{7, 7},
1194 		{0, 0},
1195 		{0, 0}
1196 		}
1197 	},
1198 	{1, 1, 3, 0,		/* 0xa1 */
1199 		{{0, 0},
1200 		{5, 5},
1201 		{7, 7},
1202 		{0, 0}
1203 		}
1204 	},
1205 	{0, 1, 3, 0,		/* 0xa2 */
1206 		{{1, 1},
1207 		{5, 5},
1208 		{7, 7},
1209 		{0, 0}
1210 		}
1211 	},
1212 	{1, 1, 3, 0,		/* 0xa3 */
1213 		{{0, 1},
1214 		{5, 5},
1215 		{7, 7},
1216 		{0, 0}
1217 		}
1218 	},
1219 	{0, 1, 3, 0,		/* 0xa4 */
1220 		{{2, 2},
1221 		{5, 5},
1222 		{7, 7},
1223 		{0, 0}
1224 		}
1225 	},
1226 	{1, 1, 4, 0,		/* 0xa5 */
1227 		{{0, 0},
1228 		{2, 2},
1229 		{5, 5},
1230 		{7, 7}
1231 		}
1232 	},
1233 	{0, 1, 3, 0,		/* 0xa6 */
1234 		{{1, 2},
1235 		{5, 5},
1236 		{7, 7},
1237 		{0, 0}
1238 		}
1239 	},
1240 	{1, 1, 3, 0,		/* 0xa7 */
1241 		{{0, 2},
1242 		{5, 5},
1243 		{7, 7},
1244 		{0, 0}
1245 		}
1246 	},
1247 	{0, 1, 3, 0,		/* 0xa8 */
1248 		{{3, 3},
1249 		{5, 5},
1250 		{7, 7},
1251 		{0, 0}
1252 		}
1253 	},
1254 	{1, 1, 4, 0,		/* 0xa9 */
1255 		{{0, 0},
1256 		{3, 3},
1257 		{5, 5},
1258 		{7, 7}
1259 		}
1260 	},
1261 	{0, 1, 4, 0,		/* 0xaa */
1262 		{{1, 1},
1263 		{3, 3},
1264 		{5, 5},
1265 		{7, 7}
1266 		}
1267 	},
1268 	{1, 1, 4, 0,		/* 0xab */
1269 		{{0, 1},
1270 		{3, 3},
1271 		{5, 5},
1272 		{7, 7}
1273 		}
1274 	},
1275 	{0, 1, 3, 0,		/* 0xac */
1276 		{{2, 3},
1277 		{5, 5},
1278 		{7, 7},
1279 		{0, 0}
1280 		}
1281 	},
1282 	{1, 1, 4, 0,		/* 0xad */
1283 		{{0, 0},
1284 		{2, 3},
1285 		{5, 5},
1286 		{7, 7}
1287 		}
1288 	},
1289 	{0, 1, 3, 0,		/* 0xae */
1290 		{{1, 3},
1291 		{5, 5},
1292 		{7, 7},
1293 		{0, 0}
1294 		}
1295 	},
1296 	{1, 1, 3, 0,		/* 0xaf */
1297 		{{0, 3},
1298 		{5, 5},
1299 		{7, 7},
1300 		{0, 0}
1301 		}
1302 	},
1303 	{0, 1, 2, 0,		/* 0xb0 */
1304 		{{4, 5},
1305 		{7, 7},
1306 		{0, 0},
1307 		{0, 0}
1308 		}
1309 	},
1310 	{1, 1, 3, 0,		/* 0xb1 */
1311 		{{0, 0},
1312 		{4, 5},
1313 		{7, 7},
1314 		{0, 0}
1315 		}
1316 	},
1317 	{0, 1, 3, 0,		/* 0xb2 */
1318 		{{1, 1},
1319 		{4, 5},
1320 		{7, 7},
1321 		{0, 0}
1322 		}
1323 	},
1324 	{1, 1, 3, 0,		/* 0xb3 */
1325 		{{0, 1},
1326 		{4, 5},
1327 		{7, 7},
1328 		{0, 0}
1329 		}
1330 	},
1331 	{0, 1, 3, 0,		/* 0xb4 */
1332 		{{2, 2},
1333 		{4, 5},
1334 		{7, 7},
1335 		{0, 0}
1336 		}
1337 	},
1338 	{1, 1, 4, 0,		/* 0xb5 */
1339 		{{0, 0},
1340 		{2, 2},
1341 		{4, 5},
1342 		{7, 7}
1343 		}
1344 	},
1345 	{0, 1, 3, 0,		/* 0xb6 */
1346 		{{1, 2},
1347 		{4, 5},
1348 		{7, 7},
1349 		{0, 0}
1350 		}
1351 	},
1352 	{1, 1, 3, 0,		/* 0xb7 */
1353 		{{0, 2},
1354 		{4, 5},
1355 		{7, 7},
1356 		{0, 0}
1357 		}
1358 	},
1359 	{0, 1, 2, 0,		/* 0xb8 */
1360 		{{3, 5},
1361 		{7, 7},
1362 		{0, 0},
1363 		{0, 0}
1364 		}
1365 	},
1366 	{1, 1, 3, 0,		/* 0xb9 */
1367 		{{0, 0},
1368 		{3, 5},
1369 		{7, 7},
1370 		{0, 0}
1371 		}
1372 	},
1373 	{0, 1, 3, 0,		/* 0xba */
1374 		{{1, 1},
1375 		{3, 5},
1376 		{7, 7},
1377 		{0, 0}
1378 		}
1379 	},
1380 	{1, 1, 3, 0,		/* 0xbb */
1381 		{{0, 1},
1382 		{3, 5},
1383 		{7, 7},
1384 		{0, 0}
1385 		}
1386 	},
1387 	{0, 1, 2, 0,		/* 0xbc */
1388 		{{2, 5},
1389 		{7, 7},
1390 		{0, 0},
1391 		{0, 0}
1392 		}
1393 	},
1394 	{1, 1, 3, 0,		/* 0xbd */
1395 		{{0, 0},
1396 		{2, 5},
1397 		{7, 7},
1398 		{0, 0}
1399 		}
1400 	},
1401 	{0, 1, 2, 0,		/* 0xbe */
1402 		{{1, 5},
1403 		{7, 7},
1404 		{0, 0},
1405 		{0, 0}
1406 		}
1407 	},
1408 	{1, 1, 2, 0,		/* 0xbf */
1409 		{{0, 5},
1410 		{7, 7},
1411 		{0, 0},
1412 		{0, 0}
1413 		}
1414 	},
1415 	{0, 1, 1, 0,		/* 0xc0 */
1416 		{{6, 7},
1417 		{0, 0},
1418 		{0, 0},
1419 		{0, 0}
1420 		}
1421 	},
1422 	{1, 1, 2, 0,		/* 0xc1 */
1423 		{{0, 0},
1424 		{6, 7},
1425 		{0, 0},
1426 		{0, 0}
1427 		}
1428 	},
1429 	{0, 1, 2, 0,		/* 0xc2 */
1430 		{{1, 1},
1431 		{6, 7},
1432 		{0, 0},
1433 		{0, 0}
1434 		}
1435 	},
1436 	{1, 1, 2, 0,		/* 0xc3 */
1437 		{{0, 1},
1438 		{6, 7},
1439 		{0, 0},
1440 		{0, 0}
1441 		}
1442 	},
1443 	{0, 1, 2, 0,		/* 0xc4 */
1444 		{{2, 2},
1445 		{6, 7},
1446 		{0, 0},
1447 		{0, 0}
1448 		}
1449 	},
1450 	{1, 1, 3, 0,		/* 0xc5 */
1451 		{{0, 0},
1452 		{2, 2},
1453 		{6, 7},
1454 		{0, 0}
1455 		}
1456 	},
1457 	{0, 1, 2, 0,		/* 0xc6 */
1458 		{{1, 2},
1459 		{6, 7},
1460 		{0, 0},
1461 		{0, 0}
1462 		}
1463 	},
1464 	{1, 1, 2, 0,		/* 0xc7 */
1465 		{{0, 2},
1466 		{6, 7},
1467 		{0, 0},
1468 		{0, 0}
1469 		}
1470 	},
1471 	{0, 1, 2, 0,		/* 0xc8 */
1472 		{{3, 3},
1473 		{6, 7},
1474 		{0, 0},
1475 		{0, 0}
1476 		}
1477 	},
1478 	{1, 1, 3, 0,		/* 0xc9 */
1479 		{{0, 0},
1480 		{3, 3},
1481 		{6, 7},
1482 		{0, 0}
1483 		}
1484 	},
1485 	{0, 1, 3, 0,		/* 0xca */
1486 		{{1, 1},
1487 		{3, 3},
1488 		{6, 7},
1489 		{0, 0}
1490 		}
1491 	},
1492 	{1, 1, 3, 0,		/* 0xcb */
1493 		{{0, 1},
1494 		{3, 3},
1495 		{6, 7},
1496 		{0, 0}
1497 		}
1498 	},
1499 	{0, 1, 2, 0,		/* 0xcc */
1500 		{{2, 3},
1501 		{6, 7},
1502 		{0, 0},
1503 		{0, 0}
1504 		}
1505 	},
1506 	{1, 1, 3, 0,		/* 0xcd */
1507 		{{0, 0},
1508 		{2, 3},
1509 		{6, 7},
1510 		{0, 0}
1511 		}
1512 	},
1513 	{0, 1, 2, 0,		/* 0xce */
1514 		{{1, 3},
1515 		{6, 7},
1516 		{0, 0},
1517 		{0, 0}
1518 		}
1519 	},
1520 	{1, 1, 2, 0,		/* 0xcf */
1521 		{{0, 3},
1522 		{6, 7},
1523 		{0, 0},
1524 		{0, 0}
1525 		}
1526 	},
1527 	{0, 1, 2, 0,		/* 0xd0 */
1528 		{{4, 4},
1529 		{6, 7},
1530 		{0, 0},
1531 		{0, 0}
1532 		}
1533 	},
1534 	{1, 1, 3, 0,		/* 0xd1 */
1535 		{{0, 0},
1536 		{4, 4},
1537 		{6, 7},
1538 		{0, 0}
1539 		}
1540 	},
1541 	{0, 1, 3, 0,		/* 0xd2 */
1542 		{{1, 1},
1543 		{4, 4},
1544 		{6, 7},
1545 		{0, 0}
1546 		}
1547 	},
1548 	{1, 1, 3, 0,		/* 0xd3 */
1549 		{{0, 1},
1550 		{4, 4},
1551 		{6, 7},
1552 		{0, 0}
1553 		}
1554 	},
1555 	{0, 1, 3, 0,		/* 0xd4 */
1556 		{{2, 2},
1557 		{4, 4},
1558 		{6, 7},
1559 		{0, 0}
1560 		}
1561 	},
1562 	{1, 1, 4, 0,		/* 0xd5 */
1563 		{{0, 0},
1564 		{2, 2},
1565 		{4, 4},
1566 		{6, 7}
1567 		}
1568 	},
1569 	{0, 1, 3, 0,		/* 0xd6 */
1570 		{{1, 2},
1571 		{4, 4},
1572 		{6, 7},
1573 		{0, 0}
1574 		}
1575 	},
1576 	{1, 1, 3, 0,		/* 0xd7 */
1577 		{{0, 2},
1578 		{4, 4},
1579 		{6, 7},
1580 		{0, 0}
1581 		}
1582 	},
1583 	{0, 1, 2, 0,		/* 0xd8 */
1584 		{{3, 4},
1585 		{6, 7},
1586 		{0, 0},
1587 		{0, 0}
1588 		}
1589 	},
1590 	{1, 1, 3, 0,		/* 0xd9 */
1591 		{{0, 0},
1592 		{3, 4},
1593 		{6, 7},
1594 		{0, 0}
1595 		}
1596 	},
1597 	{0, 1, 3, 0,		/* 0xda */
1598 		{{1, 1},
1599 		{3, 4},
1600 		{6, 7},
1601 		{0, 0}
1602 		}
1603 	},
1604 	{1, 1, 3, 0,		/* 0xdb */
1605 		{{0, 1},
1606 		{3, 4},
1607 		{6, 7},
1608 		{0, 0}
1609 		}
1610 	},
1611 	{0, 1, 2, 0,		/* 0xdc */
1612 		{{2, 4},
1613 		{6, 7},
1614 		{0, 0},
1615 		{0, 0}
1616 		}
1617 	},
1618 	{1, 1, 3, 0,		/* 0xdd */
1619 		{{0, 0},
1620 		{2, 4},
1621 		{6, 7},
1622 		{0, 0}
1623 		}
1624 	},
1625 	{0, 1, 2, 0,		/* 0xde */
1626 		{{1, 4},
1627 		{6, 7},
1628 		{0, 0},
1629 		{0, 0}
1630 		}
1631 	},
1632 	{1, 1, 2, 0,		/* 0xdf */
1633 		{{0, 4},
1634 		{6, 7},
1635 		{0, 0},
1636 		{0, 0}
1637 		}
1638 	},
1639 	{0, 1, 1, 0,		/* 0xe0 */
1640 		{{5, 7},
1641 		{0, 0},
1642 		{0, 0},
1643 		{0, 0}
1644 		}
1645 	},
1646 	{1, 1, 2, 0,		/* 0xe1 */
1647 		{{0, 0},
1648 		{5, 7},
1649 		{0, 0},
1650 		{0, 0}
1651 		}
1652 	},
1653 	{0, 1, 2, 0,		/* 0xe2 */
1654 		{{1, 1},
1655 		{5, 7},
1656 		{0, 0},
1657 		{0, 0}
1658 		}
1659 	},
1660 	{1, 1, 2, 0,		/* 0xe3 */
1661 		{{0, 1},
1662 		{5, 7},
1663 		{0, 0},
1664 		{0, 0}
1665 		}
1666 	},
1667 	{0, 1, 2, 0,		/* 0xe4 */
1668 		{{2, 2},
1669 		{5, 7},
1670 		{0, 0},
1671 		{0, 0}
1672 		}
1673 	},
1674 	{1, 1, 3, 0,		/* 0xe5 */
1675 		{{0, 0},
1676 		{2, 2},
1677 		{5, 7},
1678 		{0, 0}
1679 		}
1680 	},
1681 	{0, 1, 2, 0,		/* 0xe6 */
1682 		{{1, 2},
1683 		{5, 7},
1684 		{0, 0},
1685 		{0, 0}
1686 		}
1687 	},
1688 	{1, 1, 2, 0,		/* 0xe7 */
1689 		{{0, 2},
1690 		{5, 7},
1691 		{0, 0},
1692 		{0, 0}
1693 		}
1694 	},
1695 	{0, 1, 2, 0,		/* 0xe8 */
1696 		{{3, 3},
1697 		{5, 7},
1698 		{0, 0},
1699 		{0, 0}
1700 		}
1701 	},
1702 	{1, 1, 3, 0,		/* 0xe9 */
1703 		{{0, 0},
1704 		{3, 3},
1705 		{5, 7},
1706 		{0, 0}
1707 		}
1708 	},
1709 	{0, 1, 3, 0,		/* 0xea */
1710 		{{1, 1},
1711 		{3, 3},
1712 		{5, 7},
1713 		{0, 0}
1714 		}
1715 	},
1716 	{1, 1, 3, 0,		/* 0xeb */
1717 		{{0, 1},
1718 		{3, 3},
1719 		{5, 7},
1720 		{0, 0}
1721 		}
1722 	},
1723 	{0, 1, 2, 0,		/* 0xec */
1724 		{{2, 3},
1725 		{5, 7},
1726 		{0, 0},
1727 		{0, 0}
1728 		}
1729 	},
1730 	{1, 1, 3, 0,		/* 0xed */
1731 		{{0, 0},
1732 		{2, 3},
1733 		{5, 7},
1734 		{0, 0}
1735 		}
1736 	},
1737 	{0, 1, 2, 0,		/* 0xee */
1738 		{{1, 3},
1739 		{5, 7},
1740 		{0, 0},
1741 		{0, 0}
1742 		}
1743 	},
1744 	{1, 1, 2, 0,		/* 0xef */
1745 		{{0, 3},
1746 		{5, 7},
1747 		{0, 0},
1748 		{0, 0}
1749 		}
1750 	},
1751 	{0, 1, 1, 0,		/* 0xf0 */
1752 		{{4, 7},
1753 		{0, 0},
1754 		{0, 0},
1755 		{0, 0}
1756 		}
1757 	},
1758 	{1, 1, 2, 0,		/* 0xf1 */
1759 		{{0, 0},
1760 		{4, 7},
1761 		{0, 0},
1762 		{0, 0}
1763 		}
1764 	},
1765 	{0, 1, 2, 0,		/* 0xf2 */
1766 		{{1, 1},
1767 		{4, 7},
1768 		{0, 0},
1769 		{0, 0}
1770 		}
1771 	},
1772 	{1, 1, 2, 0,		/* 0xf3 */
1773 		{{0, 1},
1774 		{4, 7},
1775 		{0, 0},
1776 		{0, 0}
1777 		}
1778 	},
1779 	{0, 1, 2, 0,		/* 0xf4 */
1780 		{{2, 2},
1781 		{4, 7},
1782 		{0, 0},
1783 		{0, 0}
1784 		}
1785 	},
1786 	{1, 1, 3, 0,		/* 0xf5 */
1787 		{{0, 0},
1788 		{2, 2},
1789 		{4, 7},
1790 		{0, 0}
1791 		}
1792 	},
1793 	{0, 1, 2, 0,		/* 0xf6 */
1794 		{{1, 2},
1795 		{4, 7},
1796 		{0, 0},
1797 		{0, 0}
1798 		}
1799 	},
1800 	{1, 1, 2, 0,		/* 0xf7 */
1801 		{{0, 2},
1802 		{4, 7},
1803 		{0, 0},
1804 		{0, 0}
1805 		}
1806 	},
1807 	{0, 1, 1, 0,		/* 0xf8 */
1808 		{{3, 7},
1809 		{0, 0},
1810 		{0, 0},
1811 		{0, 0}
1812 		}
1813 	},
1814 	{1, 1, 2, 0,		/* 0xf9 */
1815 		{{0, 0},
1816 		{3, 7},
1817 		{0, 0},
1818 		{0, 0}
1819 		}
1820 	},
1821 	{0, 1, 2, 0,		/* 0xfa */
1822 		{{1, 1},
1823 		{3, 7},
1824 		{0, 0},
1825 		{0, 0}
1826 		}
1827 	},
1828 	{1, 1, 2, 0,		/* 0xfb */
1829 		{{0, 1},
1830 		{3, 7},
1831 		{0, 0},
1832 		{0, 0}
1833 		}
1834 	},
1835 	{0, 1, 1, 0,		/* 0xfc */
1836 		{{2, 7},
1837 		{0, 0},
1838 		{0, 0},
1839 		{0, 0}
1840 		}
1841 	},
1842 	{1, 1, 2, 0,		/* 0xfd */
1843 		{{0, 0},
1844 		{2, 7},
1845 		{0, 0},
1846 		{0, 0}
1847 		}
1848 	},
1849 	{0, 1, 1, 0,		/* 0xfe */
1850 		{{1, 7},
1851 		{0, 0},
1852 		{0, 0},
1853 		{0, 0}
1854 		}
1855 	},
1856 	{1, 1, 1, 0,		/* 0xff */
1857 		{{0, 7},
1858 		{0, 0},
1859 		{0, 0},
1860 		{0, 0}
1861 		}
1862 	}
1863 };
1864 
1865 
1866 int
1867 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1868     int ipv4_addr_legal,
1869     int ipv6_addr_legal,
1870     int loopback_scope,
1871     int ipv4_local_scope,
1872     int local_scope SCTP_UNUSED,/* XXX */
1873     int site_scope,
1874     int do_update)
1875 {
1876 	if ((loopback_scope == 0) &&
1877 	    (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1878 		/*
1879 		 * skip loopback if not in scope *
1880 		 */
1881 		return (0);
1882 	}
1883 	switch (ifa->address.sa.sa_family) {
1884 #ifdef INET
1885 	case AF_INET:
1886 		if (ipv4_addr_legal) {
1887 			struct sockaddr_in *sin;
1888 
1889 			sin = (struct sockaddr_in *)&ifa->address.sin;
1890 			if (sin->sin_addr.s_addr == 0) {
1891 				/* not in scope , unspecified */
1892 				return (0);
1893 			}
1894 			if ((ipv4_local_scope == 0) &&
1895 			    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1896 				/* private address not in scope */
1897 				return (0);
1898 			}
1899 		} else {
1900 			return (0);
1901 		}
1902 		break;
1903 #endif
1904 #ifdef INET6
1905 	case AF_INET6:
1906 		if (ipv6_addr_legal) {
1907 			struct sockaddr_in6 *sin6;
1908 
1909 			/*
1910 			 * Must update the flags,  bummer, which means any
1911 			 * IFA locks must now be applied HERE <->
1912 			 */
1913 			if (do_update) {
1914 				sctp_gather_internal_ifa_flags(ifa);
1915 			}
1916 			if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1917 				return (0);
1918 			}
1919 			/* ok to use deprecated addresses? */
1920 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1921 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1922 				/* skip unspecifed addresses */
1923 				return (0);
1924 			}
1925 			if (	/* (local_scope == 0) && */
1926 			    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1927 				return (0);
1928 			}
1929 			if ((site_scope == 0) &&
1930 			    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1931 				return (0);
1932 			}
1933 		} else {
1934 			return (0);
1935 		}
1936 		break;
1937 #endif
1938 	default:
1939 		return (0);
1940 	}
1941 	return (1);
1942 }
1943 
1944 static struct mbuf *
1945 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1946 {
1947 	struct sctp_paramhdr *parmh;
1948 	struct mbuf *mret;
1949 	int len;
1950 
1951 	switch (ifa->address.sa.sa_family) {
1952 #ifdef INET
1953 	case AF_INET:
1954 		len = sizeof(struct sctp_ipv4addr_param);
1955 		break;
1956 #endif
1957 #ifdef INET6
1958 	case AF_INET6:
1959 		len = sizeof(struct sctp_ipv6addr_param);
1960 		break;
1961 #endif
1962 	default:
1963 		return (m);
1964 	}
1965 	if (M_TRAILINGSPACE(m) >= len) {
1966 		/* easy side we just drop it on the end */
1967 		parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1968 		mret = m;
1969 	} else {
1970 		/* Need more space */
1971 		mret = m;
1972 		while (SCTP_BUF_NEXT(mret) != NULL) {
1973 			mret = SCTP_BUF_NEXT(mret);
1974 		}
1975 		SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1976 		if (SCTP_BUF_NEXT(mret) == NULL) {
1977 			/* We are hosed, can't add more addresses */
1978 			return (m);
1979 		}
1980 		mret = SCTP_BUF_NEXT(mret);
1981 		parmh = mtod(mret, struct sctp_paramhdr *);
1982 	}
1983 	/* now add the parameter */
1984 	switch (ifa->address.sa.sa_family) {
1985 #ifdef INET
1986 	case AF_INET:
1987 		{
1988 			struct sctp_ipv4addr_param *ipv4p;
1989 			struct sockaddr_in *sin;
1990 
1991 			sin = (struct sockaddr_in *)&ifa->address.sin;
1992 			ipv4p = (struct sctp_ipv4addr_param *)parmh;
1993 			parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1994 			parmh->param_length = htons(len);
1995 			ipv4p->addr = sin->sin_addr.s_addr;
1996 			SCTP_BUF_LEN(mret) += len;
1997 			break;
1998 		}
1999 #endif
2000 #ifdef INET6
2001 	case AF_INET6:
2002 		{
2003 			struct sctp_ipv6addr_param *ipv6p;
2004 			struct sockaddr_in6 *sin6;
2005 
2006 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
2007 			ipv6p = (struct sctp_ipv6addr_param *)parmh;
2008 			parmh->param_type = htons(SCTP_IPV6_ADDRESS);
2009 			parmh->param_length = htons(len);
2010 			memcpy(ipv6p->addr, &sin6->sin6_addr,
2011 			    sizeof(ipv6p->addr));
2012 			/* clear embedded scope in the address */
2013 			in6_clearscope((struct in6_addr *)ipv6p->addr);
2014 			SCTP_BUF_LEN(mret) += len;
2015 			break;
2016 		}
2017 #endif
2018 	default:
2019 		return (m);
2020 	}
2021 	return (mret);
2022 }
2023 
2024 
2025 struct mbuf *
2026 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2027     struct sctp_scoping *scope,
2028     struct mbuf *m_at, int cnt_inits_to)
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 				if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2062 					continue;
2063 				}
2064 				if (sctp_is_address_in_scope(sctp_ifap,
2065 				    scope->ipv4_addr_legal,
2066 				    scope->ipv6_addr_legal,
2067 				    scope->loopback_scope,
2068 				    scope->ipv4_local_scope,
2069 				    scope->local_scope,
2070 				    scope->site_scope, 1) == 0) {
2071 					continue;
2072 				}
2073 				cnt++;
2074 				if (cnt > SCTP_ADDRESS_LIMIT) {
2075 					break;
2076 				}
2077 			}
2078 			if (cnt > SCTP_ADDRESS_LIMIT) {
2079 				break;
2080 			}
2081 		}
2082 skip_count:
2083 		if (cnt > 1) {
2084 			total_count = 0;
2085 			LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2086 				cnt = 0;
2087 				if ((scope->loopback_scope == 0) &&
2088 				    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2089 					/*
2090 					 * Skip loopback devices if
2091 					 * loopback_scope not set
2092 					 */
2093 					continue;
2094 				}
2095 				LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2096 					if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2097 						continue;
2098 					}
2099 					if (sctp_is_address_in_scope(sctp_ifap,
2100 					    scope->ipv4_addr_legal,
2101 					    scope->ipv6_addr_legal,
2102 					    scope->loopback_scope,
2103 					    scope->ipv4_local_scope,
2104 					    scope->local_scope,
2105 					    scope->site_scope, 0) == 0) {
2106 						continue;
2107 					}
2108 					m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2109 					if (limit_out) {
2110 						cnt++;
2111 						total_count++;
2112 						if (cnt >= 2) {
2113 							/*
2114 							 * two from each
2115 							 * address
2116 							 */
2117 							break;
2118 						}
2119 						if (total_count > SCTP_ADDRESS_LIMIT) {
2120 							/* No more addresses */
2121 							break;
2122 						}
2123 					}
2124 				}
2125 			}
2126 		}
2127 	} else {
2128 		struct sctp_laddr *laddr;
2129 
2130 		cnt = cnt_inits_to;
2131 		/* First, how many ? */
2132 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2133 			if (laddr->ifa == NULL) {
2134 				continue;
2135 			}
2136 			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2137 				/*
2138 				 * Address being deleted by the system, dont
2139 				 * list.
2140 				 */
2141 				continue;
2142 			if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2143 				/*
2144 				 * Address being deleted on this ep don't
2145 				 * list.
2146 				 */
2147 				continue;
2148 			}
2149 			if (sctp_is_address_in_scope(laddr->ifa,
2150 			    scope->ipv4_addr_legal,
2151 			    scope->ipv6_addr_legal,
2152 			    scope->loopback_scope,
2153 			    scope->ipv4_local_scope,
2154 			    scope->local_scope,
2155 			    scope->site_scope, 1) == 0) {
2156 				continue;
2157 			}
2158 			cnt++;
2159 		}
2160 		/*
2161 		 * To get through a NAT we only list addresses if we have
2162 		 * more than one. That way if you just bind a single address
2163 		 * we let the source of the init dictate our address.
2164 		 */
2165 		if (cnt > 1) {
2166 			cnt = cnt_inits_to;
2167 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2168 				if (laddr->ifa == NULL) {
2169 					continue;
2170 				}
2171 				if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
2172 					continue;
2173 				}
2174 				if (sctp_is_address_in_scope(laddr->ifa,
2175 				    scope->ipv4_addr_legal,
2176 				    scope->ipv6_addr_legal,
2177 				    scope->loopback_scope,
2178 				    scope->ipv4_local_scope,
2179 				    scope->local_scope,
2180 				    scope->site_scope, 0) == 0) {
2181 					continue;
2182 				}
2183 				m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2184 				cnt++;
2185 				if (cnt >= SCTP_ADDRESS_LIMIT) {
2186 					break;
2187 				}
2188 			}
2189 		}
2190 	}
2191 	SCTP_IPI_ADDR_RUNLOCK();
2192 	return (m_at);
2193 }
2194 
2195 static struct sctp_ifa *
2196 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2197     uint8_t dest_is_loop,
2198     uint8_t dest_is_priv,
2199     sa_family_t fam)
2200 {
2201 	uint8_t dest_is_global = 0;
2202 
2203 	/* dest_is_priv is true if destination is a private address */
2204 	/* dest_is_loop is true if destination is a loopback addresses */
2205 
2206 	/**
2207 	 * Here we determine if its a preferred address. A preferred address
2208 	 * means it is the same scope or higher scope then the destination.
2209 	 * L = loopback, P = private, G = global
2210 	 * -----------------------------------------
2211 	 *    src    |  dest | result
2212 	 *  ----------------------------------------
2213 	 *     L     |    L  |    yes
2214 	 *  -----------------------------------------
2215 	 *     P     |    L  |    yes-v4 no-v6
2216 	 *  -----------------------------------------
2217 	 *     G     |    L  |    yes-v4 no-v6
2218 	 *  -----------------------------------------
2219 	 *     L     |    P  |    no
2220 	 *  -----------------------------------------
2221 	 *     P     |    P  |    yes
2222 	 *  -----------------------------------------
2223 	 *     G     |    P  |    no
2224 	 *   -----------------------------------------
2225 	 *     L     |    G  |    no
2226 	 *   -----------------------------------------
2227 	 *     P     |    G  |    no
2228 	 *    -----------------------------------------
2229 	 *     G     |    G  |    yes
2230 	 *    -----------------------------------------
2231 	 */
2232 
2233 	if (ifa->address.sa.sa_family != fam) {
2234 		/* forget mis-matched family */
2235 		return (NULL);
2236 	}
2237 	if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2238 		dest_is_global = 1;
2239 	}
2240 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2241 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2242 	/* Ok the address may be ok */
2243 #ifdef INET6
2244 	if (fam == AF_INET6) {
2245 		/* ok to use deprecated addresses? no lets not! */
2246 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2247 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2248 			return (NULL);
2249 		}
2250 		if (ifa->src_is_priv && !ifa->src_is_loop) {
2251 			if (dest_is_loop) {
2252 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2253 				return (NULL);
2254 			}
2255 		}
2256 		if (ifa->src_is_glob) {
2257 			if (dest_is_loop) {
2258 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2259 				return (NULL);
2260 			}
2261 		}
2262 	}
2263 #endif
2264 	/*
2265 	 * Now that we know what is what, implement or table this could in
2266 	 * theory be done slicker (it used to be), but this is
2267 	 * straightforward and easier to validate :-)
2268 	 */
2269 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2270 	    ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2271 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2272 	    dest_is_loop, dest_is_priv, dest_is_global);
2273 
2274 	if ((ifa->src_is_loop) && (dest_is_priv)) {
2275 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2276 		return (NULL);
2277 	}
2278 	if ((ifa->src_is_glob) && (dest_is_priv)) {
2279 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2280 		return (NULL);
2281 	}
2282 	if ((ifa->src_is_loop) && (dest_is_global)) {
2283 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2284 		return (NULL);
2285 	}
2286 	if ((ifa->src_is_priv) && (dest_is_global)) {
2287 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2288 		return (NULL);
2289 	}
2290 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2291 	/* its a preferred address */
2292 	return (ifa);
2293 }
2294 
2295 static struct sctp_ifa *
2296 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2297     uint8_t dest_is_loop,
2298     uint8_t dest_is_priv,
2299     sa_family_t fam)
2300 {
2301 	uint8_t dest_is_global = 0;
2302 
2303 	/**
2304 	 * Here we determine if its a acceptable address. A acceptable
2305 	 * address means it is the same scope or higher scope but we can
2306 	 * allow for NAT which means its ok to have a global dest and a
2307 	 * private src.
2308 	 *
2309 	 * L = loopback, P = private, G = global
2310 	 * -----------------------------------------
2311 	 *  src    |  dest | result
2312 	 * -----------------------------------------
2313 	 *   L     |   L   |    yes
2314 	 *  -----------------------------------------
2315 	 *   P     |   L   |    yes-v4 no-v6
2316 	 *  -----------------------------------------
2317 	 *   G     |   L   |    yes
2318 	 * -----------------------------------------
2319 	 *   L     |   P   |    no
2320 	 * -----------------------------------------
2321 	 *   P     |   P   |    yes
2322 	 * -----------------------------------------
2323 	 *   G     |   P   |    yes - May not work
2324 	 * -----------------------------------------
2325 	 *   L     |   G   |    no
2326 	 * -----------------------------------------
2327 	 *   P     |   G   |    yes - May not work
2328 	 * -----------------------------------------
2329 	 *   G     |   G   |    yes
2330 	 * -----------------------------------------
2331 	 */
2332 
2333 	if (ifa->address.sa.sa_family != fam) {
2334 		/* forget non matching family */
2335 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n",
2336 		    ifa->address.sa.sa_family, fam);
2337 		return (NULL);
2338 	}
2339 	/* Ok the address may be ok */
2340 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa);
2341 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n",
2342 	    dest_is_loop, dest_is_priv);
2343 	if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2344 		dest_is_global = 1;
2345 	}
2346 #ifdef INET6
2347 	if (fam == AF_INET6) {
2348 		/* ok to use deprecated addresses? */
2349 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2350 			return (NULL);
2351 		}
2352 		if (ifa->src_is_priv) {
2353 			/* Special case, linklocal to loop */
2354 			if (dest_is_loop)
2355 				return (NULL);
2356 		}
2357 	}
2358 #endif
2359 	/*
2360 	 * Now that we know what is what, implement our table. This could in
2361 	 * theory be done slicker (it used to be), but this is
2362 	 * straightforward and easier to validate :-)
2363 	 */
2364 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n",
2365 	    ifa->src_is_loop,
2366 	    dest_is_priv);
2367 	if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2368 		return (NULL);
2369 	}
2370 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n",
2371 	    ifa->src_is_loop,
2372 	    dest_is_global);
2373 	if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2374 		return (NULL);
2375 	}
2376 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n");
2377 	/* its an acceptable address */
2378 	return (ifa);
2379 }
2380 
2381 int
2382 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2383 {
2384 	struct sctp_laddr *laddr;
2385 
2386 	if (stcb == NULL) {
2387 		/* There are no restrictions, no TCB :-) */
2388 		return (0);
2389 	}
2390 	LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2391 		if (laddr->ifa == NULL) {
2392 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2393 			    __FUNCTION__);
2394 			continue;
2395 		}
2396 		if (laddr->ifa == ifa) {
2397 			/* Yes it is on the list */
2398 			return (1);
2399 		}
2400 	}
2401 	return (0);
2402 }
2403 
2404 
2405 int
2406 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2407 {
2408 	struct sctp_laddr *laddr;
2409 
2410 	if (ifa == NULL)
2411 		return (0);
2412 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2413 		if (laddr->ifa == NULL) {
2414 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2415 			    __FUNCTION__);
2416 			continue;
2417 		}
2418 		if ((laddr->ifa == ifa) && laddr->action == 0)
2419 			/* same pointer */
2420 			return (1);
2421 	}
2422 	return (0);
2423 }
2424 
2425 
2426 
2427 static struct sctp_ifa *
2428 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2429     sctp_route_t * ro,
2430     uint32_t vrf_id,
2431     int non_asoc_addr_ok,
2432     uint8_t dest_is_priv,
2433     uint8_t dest_is_loop,
2434     sa_family_t fam)
2435 {
2436 	struct sctp_laddr *laddr, *starting_point;
2437 	void *ifn;
2438 	int resettotop = 0;
2439 	struct sctp_ifn *sctp_ifn;
2440 	struct sctp_ifa *sctp_ifa, *sifa;
2441 	struct sctp_vrf *vrf;
2442 	uint32_t ifn_index;
2443 
2444 	vrf = sctp_find_vrf(vrf_id);
2445 	if (vrf == NULL)
2446 		return (NULL);
2447 
2448 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2449 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2450 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2451 	/*
2452 	 * first question, is the ifn we will emit on in our list, if so, we
2453 	 * want such an address. Note that we first looked for a preferred
2454 	 * address.
2455 	 */
2456 	if (sctp_ifn) {
2457 		/* is a preferred one on the interface we route out? */
2458 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2459 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2460 			    (non_asoc_addr_ok == 0))
2461 				continue;
2462 			sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2463 			    dest_is_loop,
2464 			    dest_is_priv, fam);
2465 			if (sifa == NULL)
2466 				continue;
2467 			if (sctp_is_addr_in_ep(inp, sifa)) {
2468 				atomic_add_int(&sifa->refcount, 1);
2469 				return (sifa);
2470 			}
2471 		}
2472 	}
2473 	/*
2474 	 * ok, now we now need to find one on the list of the addresses. We
2475 	 * can't get one on the emitting interface so let's find first a
2476 	 * preferred one. If not that an acceptable one otherwise... we
2477 	 * return NULL.
2478 	 */
2479 	starting_point = inp->next_addr_touse;
2480 once_again:
2481 	if (inp->next_addr_touse == NULL) {
2482 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2483 		resettotop = 1;
2484 	}
2485 	for (laddr = inp->next_addr_touse; laddr;
2486 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2487 		if (laddr->ifa == NULL) {
2488 			/* address has been removed */
2489 			continue;
2490 		}
2491 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2492 			/* address is being deleted */
2493 			continue;
2494 		}
2495 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2496 		    dest_is_priv, fam);
2497 		if (sifa == NULL)
2498 			continue;
2499 		atomic_add_int(&sifa->refcount, 1);
2500 		return (sifa);
2501 	}
2502 	if (resettotop == 0) {
2503 		inp->next_addr_touse = NULL;
2504 		goto once_again;
2505 	}
2506 	inp->next_addr_touse = starting_point;
2507 	resettotop = 0;
2508 once_again_too:
2509 	if (inp->next_addr_touse == NULL) {
2510 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2511 		resettotop = 1;
2512 	}
2513 	/* ok, what about an acceptable address in the inp */
2514 	for (laddr = inp->next_addr_touse; laddr;
2515 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2516 		if (laddr->ifa == NULL) {
2517 			/* address has been removed */
2518 			continue;
2519 		}
2520 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2521 			/* address is being deleted */
2522 			continue;
2523 		}
2524 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2525 		    dest_is_priv, fam);
2526 		if (sifa == NULL)
2527 			continue;
2528 		atomic_add_int(&sifa->refcount, 1);
2529 		return (sifa);
2530 	}
2531 	if (resettotop == 0) {
2532 		inp->next_addr_touse = NULL;
2533 		goto once_again_too;
2534 	}
2535 	/*
2536 	 * no address bound can be a source for the destination we are in
2537 	 * trouble
2538 	 */
2539 	return (NULL);
2540 }
2541 
2542 
2543 
2544 static struct sctp_ifa *
2545 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2546     struct sctp_tcb *stcb,
2547     sctp_route_t * ro,
2548     uint32_t vrf_id,
2549     uint8_t dest_is_priv,
2550     uint8_t dest_is_loop,
2551     int non_asoc_addr_ok,
2552     sa_family_t fam)
2553 {
2554 	struct sctp_laddr *laddr, *starting_point;
2555 	void *ifn;
2556 	struct sctp_ifn *sctp_ifn;
2557 	struct sctp_ifa *sctp_ifa, *sifa;
2558 	uint8_t start_at_beginning = 0;
2559 	struct sctp_vrf *vrf;
2560 	uint32_t ifn_index;
2561 
2562 	/*
2563 	 * first question, is the ifn we will emit on in our list, if so, we
2564 	 * want that one.
2565 	 */
2566 	vrf = sctp_find_vrf(vrf_id);
2567 	if (vrf == NULL)
2568 		return (NULL);
2569 
2570 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2571 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2572 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2573 
2574 	/*
2575 	 * first question, is the ifn we will emit on in our list?  If so,
2576 	 * we want that one. First we look for a preferred. Second, we go
2577 	 * for an acceptable.
2578 	 */
2579 	if (sctp_ifn) {
2580 		/* first try for a preferred address on the ep */
2581 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2582 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2583 				continue;
2584 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2585 				sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2586 				if (sifa == NULL)
2587 					continue;
2588 				if (((non_asoc_addr_ok == 0) &&
2589 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2590 				    (non_asoc_addr_ok &&
2591 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2592 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2593 					/* on the no-no list */
2594 					continue;
2595 				}
2596 				atomic_add_int(&sifa->refcount, 1);
2597 				return (sifa);
2598 			}
2599 		}
2600 		/* next try for an acceptable address on the ep */
2601 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2602 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2603 				continue;
2604 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2605 				sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2606 				if (sifa == NULL)
2607 					continue;
2608 				if (((non_asoc_addr_ok == 0) &&
2609 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2610 				    (non_asoc_addr_ok &&
2611 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2612 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2613 					/* on the no-no list */
2614 					continue;
2615 				}
2616 				atomic_add_int(&sifa->refcount, 1);
2617 				return (sifa);
2618 			}
2619 		}
2620 
2621 	}
2622 	/*
2623 	 * if we can't find one like that then we must look at all addresses
2624 	 * bound to pick one at first preferable then secondly acceptable.
2625 	 */
2626 	starting_point = stcb->asoc.last_used_address;
2627 sctp_from_the_top:
2628 	if (stcb->asoc.last_used_address == NULL) {
2629 		start_at_beginning = 1;
2630 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2631 	}
2632 	/* search beginning with the last used address */
2633 	for (laddr = stcb->asoc.last_used_address; laddr;
2634 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2635 		if (laddr->ifa == NULL) {
2636 			/* address has been removed */
2637 			continue;
2638 		}
2639 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2640 			/* address is being deleted */
2641 			continue;
2642 		}
2643 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2644 		if (sifa == NULL)
2645 			continue;
2646 		if (((non_asoc_addr_ok == 0) &&
2647 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2648 		    (non_asoc_addr_ok &&
2649 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2650 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2651 			/* on the no-no list */
2652 			continue;
2653 		}
2654 		stcb->asoc.last_used_address = laddr;
2655 		atomic_add_int(&sifa->refcount, 1);
2656 		return (sifa);
2657 	}
2658 	if (start_at_beginning == 0) {
2659 		stcb->asoc.last_used_address = NULL;
2660 		goto sctp_from_the_top;
2661 	}
2662 	/* now try for any higher scope than the destination */
2663 	stcb->asoc.last_used_address = starting_point;
2664 	start_at_beginning = 0;
2665 sctp_from_the_top2:
2666 	if (stcb->asoc.last_used_address == NULL) {
2667 		start_at_beginning = 1;
2668 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2669 	}
2670 	/* search beginning with the last used address */
2671 	for (laddr = stcb->asoc.last_used_address; laddr;
2672 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2673 		if (laddr->ifa == NULL) {
2674 			/* address has been removed */
2675 			continue;
2676 		}
2677 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2678 			/* address is being deleted */
2679 			continue;
2680 		}
2681 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2682 		    dest_is_priv, fam);
2683 		if (sifa == NULL)
2684 			continue;
2685 		if (((non_asoc_addr_ok == 0) &&
2686 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2687 		    (non_asoc_addr_ok &&
2688 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2689 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2690 			/* on the no-no list */
2691 			continue;
2692 		}
2693 		stcb->asoc.last_used_address = laddr;
2694 		atomic_add_int(&sifa->refcount, 1);
2695 		return (sifa);
2696 	}
2697 	if (start_at_beginning == 0) {
2698 		stcb->asoc.last_used_address = NULL;
2699 		goto sctp_from_the_top2;
2700 	}
2701 	return (NULL);
2702 }
2703 
2704 static struct sctp_ifa *
2705 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2706     struct sctp_tcb *stcb,
2707     int non_asoc_addr_ok,
2708     uint8_t dest_is_loop,
2709     uint8_t dest_is_priv,
2710     int addr_wanted,
2711     sa_family_t fam,
2712     sctp_route_t * ro
2713 )
2714 {
2715 	struct sctp_ifa *ifa, *sifa;
2716 	int num_eligible_addr = 0;
2717 
2718 #ifdef INET6
2719 	struct sockaddr_in6 sin6, lsa6;
2720 
2721 	if (fam == AF_INET6) {
2722 		memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2723 		(void)sa6_recoverscope(&sin6);
2724 	}
2725 #endif				/* INET6 */
2726 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2727 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2728 		    (non_asoc_addr_ok == 0))
2729 			continue;
2730 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2731 		    dest_is_priv, fam);
2732 		if (sifa == NULL)
2733 			continue;
2734 #ifdef INET6
2735 		if (fam == AF_INET6 &&
2736 		    dest_is_loop &&
2737 		    sifa->src_is_loop && sifa->src_is_priv) {
2738 			/*
2739 			 * don't allow fe80::1 to be a src on loop ::1, we
2740 			 * don't list it to the peer so we will get an
2741 			 * abort.
2742 			 */
2743 			continue;
2744 		}
2745 		if (fam == AF_INET6 &&
2746 		    IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2747 		    IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2748 			/*
2749 			 * link-local <-> link-local must belong to the same
2750 			 * scope.
2751 			 */
2752 			memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2753 			(void)sa6_recoverscope(&lsa6);
2754 			if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2755 				continue;
2756 			}
2757 		}
2758 #endif				/* INET6 */
2759 
2760 		/*
2761 		 * Check if the IPv6 address matches to next-hop. In the
2762 		 * mobile case, old IPv6 address may be not deleted from the
2763 		 * interface. Then, the interface has previous and new
2764 		 * addresses.  We should use one corresponding to the
2765 		 * next-hop.  (by micchie)
2766 		 */
2767 #ifdef INET6
2768 		if (stcb && fam == AF_INET6 &&
2769 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2770 			if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2771 			    == 0) {
2772 				continue;
2773 			}
2774 		}
2775 #endif
2776 #ifdef INET
2777 		/* Avoid topologically incorrect IPv4 address */
2778 		if (stcb && fam == AF_INET &&
2779 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2780 			if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2781 				continue;
2782 			}
2783 		}
2784 #endif
2785 		if (stcb) {
2786 			if (sctp_is_address_in_scope(ifa,
2787 			    stcb->asoc.ipv4_addr_legal,
2788 			    stcb->asoc.ipv6_addr_legal,
2789 			    stcb->asoc.loopback_scope,
2790 			    stcb->asoc.ipv4_local_scope,
2791 			    stcb->asoc.local_scope,
2792 			    stcb->asoc.site_scope, 0) == 0) {
2793 				continue;
2794 			}
2795 			if (((non_asoc_addr_ok == 0) &&
2796 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2797 			    (non_asoc_addr_ok &&
2798 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2799 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2800 				/*
2801 				 * It is restricted for some reason..
2802 				 * probably not yet added.
2803 				 */
2804 				continue;
2805 			}
2806 		}
2807 		if (num_eligible_addr >= addr_wanted) {
2808 			return (sifa);
2809 		}
2810 		num_eligible_addr++;
2811 	}
2812 	return (NULL);
2813 }
2814 
2815 
2816 static int
2817 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2818     struct sctp_tcb *stcb,
2819     int non_asoc_addr_ok,
2820     uint8_t dest_is_loop,
2821     uint8_t dest_is_priv,
2822     sa_family_t fam)
2823 {
2824 	struct sctp_ifa *ifa, *sifa;
2825 	int num_eligible_addr = 0;
2826 
2827 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2828 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2829 		    (non_asoc_addr_ok == 0)) {
2830 			continue;
2831 		}
2832 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2833 		    dest_is_priv, fam);
2834 		if (sifa == NULL) {
2835 			continue;
2836 		}
2837 		if (stcb) {
2838 			if (sctp_is_address_in_scope(ifa,
2839 			    stcb->asoc.ipv4_addr_legal,
2840 			    stcb->asoc.ipv6_addr_legal,
2841 			    stcb->asoc.loopback_scope,
2842 			    stcb->asoc.ipv4_local_scope,
2843 			    stcb->asoc.local_scope,
2844 			    stcb->asoc.site_scope, 0) == 0) {
2845 				continue;
2846 			}
2847 			if (((non_asoc_addr_ok == 0) &&
2848 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2849 			    (non_asoc_addr_ok &&
2850 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2851 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2852 				/*
2853 				 * It is restricted for some reason..
2854 				 * probably not yet added.
2855 				 */
2856 				continue;
2857 			}
2858 		}
2859 		num_eligible_addr++;
2860 	}
2861 	return (num_eligible_addr);
2862 }
2863 
2864 static struct sctp_ifa *
2865 sctp_choose_boundall(struct sctp_tcb *stcb,
2866     struct sctp_nets *net,
2867     sctp_route_t * ro,
2868     uint32_t vrf_id,
2869     uint8_t dest_is_priv,
2870     uint8_t dest_is_loop,
2871     int non_asoc_addr_ok,
2872     sa_family_t fam)
2873 {
2874 	int cur_addr_num = 0, num_preferred = 0;
2875 	void *ifn;
2876 	struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2877 	struct sctp_ifa *sctp_ifa, *sifa;
2878 	uint32_t ifn_index;
2879 	struct sctp_vrf *vrf;
2880 
2881 #ifdef INET
2882 	int retried = 0;
2883 
2884 #endif
2885 
2886 	/*-
2887 	 * For boundall we can use any address in the association.
2888 	 * If non_asoc_addr_ok is set we can use any address (at least in
2889 	 * theory). So we look for preferred addresses first. If we find one,
2890 	 * we use it. Otherwise we next try to get an address on the
2891 	 * interface, which we should be able to do (unless non_asoc_addr_ok
2892 	 * is false and we are routed out that way). In these cases where we
2893 	 * can't use the address of the interface we go through all the
2894 	 * ifn's looking for an address we can use and fill that in. Punting
2895 	 * means we send back address 0, which will probably cause problems
2896 	 * actually since then IP will fill in the address of the route ifn,
2897 	 * which means we probably already rejected it.. i.e. here comes an
2898 	 * abort :-<.
2899 	 */
2900 	vrf = sctp_find_vrf(vrf_id);
2901 	if (vrf == NULL)
2902 		return (NULL);
2903 
2904 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2905 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2906 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn from route:%p ifn_index:%d\n", ifn, ifn_index);
2907 	emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2908 	if (sctp_ifn == NULL) {
2909 		/* ?? We don't have this guy ?? */
2910 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2911 		goto bound_all_plan_b;
2912 	}
2913 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2914 	    ifn_index, sctp_ifn->ifn_name);
2915 
2916 	if (net) {
2917 		cur_addr_num = net->indx_of_eligible_next_to_use;
2918 	}
2919 	num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2920 	    stcb,
2921 	    non_asoc_addr_ok,
2922 	    dest_is_loop,
2923 	    dest_is_priv, fam);
2924 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2925 	    num_preferred, sctp_ifn->ifn_name);
2926 	if (num_preferred == 0) {
2927 		/*
2928 		 * no eligible addresses, we must use some other interface
2929 		 * address if we can find one.
2930 		 */
2931 		goto bound_all_plan_b;
2932 	}
2933 	/*
2934 	 * Ok we have num_eligible_addr set with how many we can use, this
2935 	 * may vary from call to call due to addresses being deprecated
2936 	 * etc..
2937 	 */
2938 	if (cur_addr_num >= num_preferred) {
2939 		cur_addr_num = 0;
2940 	}
2941 	/*
2942 	 * select the nth address from the list (where cur_addr_num is the
2943 	 * nth) and 0 is the first one, 1 is the second one etc...
2944 	 */
2945 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2946 
2947 	sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2948 	    dest_is_priv, cur_addr_num, fam, ro);
2949 
2950 	/* if sctp_ifa is NULL something changed??, fall to plan b. */
2951 	if (sctp_ifa) {
2952 		atomic_add_int(&sctp_ifa->refcount, 1);
2953 		if (net) {
2954 			/* save off where the next one we will want */
2955 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2956 		}
2957 		return (sctp_ifa);
2958 	}
2959 	/*
2960 	 * plan_b: Look at all interfaces and find a preferred address. If
2961 	 * no preferred fall through to plan_c.
2962 	 */
2963 bound_all_plan_b:
2964 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2965 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2966 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2967 		    sctp_ifn->ifn_name);
2968 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2969 			/* wrong base scope */
2970 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2971 			continue;
2972 		}
2973 		if ((sctp_ifn == looked_at) && looked_at) {
2974 			/* already looked at this guy */
2975 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2976 			continue;
2977 		}
2978 		num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2979 		    dest_is_loop, dest_is_priv, fam);
2980 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
2981 		    "Found ifn:%p %d preferred source addresses\n",
2982 		    ifn, num_preferred);
2983 		if (num_preferred == 0) {
2984 			/* None on this interface. */
2985 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2986 			continue;
2987 		}
2988 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
2989 		    "num preferred:%d on interface:%p cur_addr_num:%d\n",
2990 		    num_preferred, sctp_ifn, cur_addr_num);
2991 
2992 		/*
2993 		 * Ok we have num_eligible_addr set with how many we can
2994 		 * use, this may vary from call to call due to addresses
2995 		 * being deprecated etc..
2996 		 */
2997 		if (cur_addr_num >= num_preferred) {
2998 			cur_addr_num = 0;
2999 		}
3000 		sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
3001 		    dest_is_priv, cur_addr_num, fam, ro);
3002 		if (sifa == NULL)
3003 			continue;
3004 		if (net) {
3005 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3006 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
3007 			    cur_addr_num);
3008 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
3009 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
3010 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
3011 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
3012 		}
3013 		atomic_add_int(&sifa->refcount, 1);
3014 		return (sifa);
3015 	}
3016 #ifdef INET
3017 again_with_private_addresses_allowed:
3018 #endif
3019 	/* plan_c: do we have an acceptable address on the emit interface */
3020 	sifa = NULL;
3021 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
3022 	if (emit_ifn == NULL) {
3023 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jump to Plan D - no emit_ifn\n");
3024 		goto plan_d;
3025 	}
3026 	LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
3027 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", sctp_ifa);
3028 		if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3029 		    (non_asoc_addr_ok == 0)) {
3030 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Defer\n");
3031 			continue;
3032 		}
3033 		sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
3034 		    dest_is_priv, fam);
3035 		if (sifa == NULL) {
3036 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n");
3037 			continue;
3038 		}
3039 		if (stcb) {
3040 			if (sctp_is_address_in_scope(sifa,
3041 			    stcb->asoc.ipv4_addr_legal,
3042 			    stcb->asoc.ipv6_addr_legal,
3043 			    stcb->asoc.loopback_scope,
3044 			    stcb->asoc.ipv4_local_scope,
3045 			    stcb->asoc.local_scope,
3046 			    stcb->asoc.site_scope, 0) == 0) {
3047 				SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n");
3048 				sifa = NULL;
3049 				continue;
3050 			}
3051 			if (((non_asoc_addr_ok == 0) &&
3052 			    (sctp_is_addr_restricted(stcb, sifa))) ||
3053 			    (non_asoc_addr_ok &&
3054 			    (sctp_is_addr_restricted(stcb, sifa)) &&
3055 			    (!sctp_is_addr_pending(stcb, sifa)))) {
3056 				/*
3057 				 * It is restricted for some reason..
3058 				 * probably not yet added.
3059 				 */
3060 				SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its resticted\n");
3061 				sifa = NULL;
3062 				continue;
3063 			}
3064 		} else {
3065 			printf("Stcb is null - no print\n");
3066 		}
3067 		atomic_add_int(&sifa->refcount, 1);
3068 		goto out;
3069 	}
3070 plan_d:
3071 	/*
3072 	 * plan_d: We are in trouble. No preferred address on the emit
3073 	 * interface. And not even a preferred address on all interfaces. Go
3074 	 * out and see if we can find an acceptable address somewhere
3075 	 * amongst all interfaces.
3076 	 */
3077 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", looked_at);
3078 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3079 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3080 			/* wrong base scope */
3081 			continue;
3082 		}
3083 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3084 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3085 			    (non_asoc_addr_ok == 0))
3086 				continue;
3087 			sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3088 			    dest_is_loop,
3089 			    dest_is_priv, fam);
3090 			if (sifa == NULL)
3091 				continue;
3092 			if (stcb) {
3093 				if (sctp_is_address_in_scope(sifa,
3094 				    stcb->asoc.ipv4_addr_legal,
3095 				    stcb->asoc.ipv6_addr_legal,
3096 				    stcb->asoc.loopback_scope,
3097 				    stcb->asoc.ipv4_local_scope,
3098 				    stcb->asoc.local_scope,
3099 				    stcb->asoc.site_scope, 0) == 0) {
3100 					sifa = NULL;
3101 					continue;
3102 				}
3103 				if (((non_asoc_addr_ok == 0) &&
3104 				    (sctp_is_addr_restricted(stcb, sifa))) ||
3105 				    (non_asoc_addr_ok &&
3106 				    (sctp_is_addr_restricted(stcb, sifa)) &&
3107 				    (!sctp_is_addr_pending(stcb, sifa)))) {
3108 					/*
3109 					 * It is restricted for some
3110 					 * reason.. probably not yet added.
3111 					 */
3112 					sifa = NULL;
3113 					continue;
3114 				}
3115 			}
3116 			goto out;
3117 		}
3118 	}
3119 #ifdef INET
3120 	if ((retried == 0) && (stcb->asoc.ipv4_local_scope == 0)) {
3121 		stcb->asoc.ipv4_local_scope = 1;
3122 		retried = 1;
3123 		goto again_with_private_addresses_allowed;
3124 	} else if (retried == 1) {
3125 		stcb->asoc.ipv4_local_scope = 0;
3126 	}
3127 #endif
3128 out:
3129 #ifdef INET
3130 	if (sifa) {
3131 		if (retried == 1) {
3132 			LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3133 				if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3134 					/* wrong base scope */
3135 					continue;
3136 				}
3137 				LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3138 					struct sctp_ifa *tmp_sifa;
3139 
3140 					if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3141 					    (non_asoc_addr_ok == 0))
3142 						continue;
3143 					tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3144 					    dest_is_loop,
3145 					    dest_is_priv, fam);
3146 					if (tmp_sifa == NULL) {
3147 						continue;
3148 					}
3149 					if (tmp_sifa == sifa) {
3150 						continue;
3151 					}
3152 					if (stcb) {
3153 						if (sctp_is_address_in_scope(tmp_sifa,
3154 						    stcb->asoc.ipv4_addr_legal,
3155 						    stcb->asoc.ipv6_addr_legal,
3156 						    stcb->asoc.loopback_scope,
3157 						    stcb->asoc.ipv4_local_scope,
3158 						    stcb->asoc.local_scope,
3159 						    stcb->asoc.site_scope, 0) == 0) {
3160 							continue;
3161 						}
3162 						if (((non_asoc_addr_ok == 0) &&
3163 						    (sctp_is_addr_restricted(stcb, tmp_sifa))) ||
3164 						    (non_asoc_addr_ok &&
3165 						    (sctp_is_addr_restricted(stcb, tmp_sifa)) &&
3166 						    (!sctp_is_addr_pending(stcb, tmp_sifa)))) {
3167 							/*
3168 							 * It is restricted
3169 							 * for some reason..
3170 							 * probably not yet
3171 							 * added.
3172 							 */
3173 							continue;
3174 						}
3175 					}
3176 					if ((tmp_sifa->address.sin.sin_family == AF_INET) &&
3177 					    (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) {
3178 						sctp_add_local_addr_restricted(stcb, tmp_sifa);
3179 					}
3180 				}
3181 			}
3182 		}
3183 		atomic_add_int(&sifa->refcount, 1);
3184 	}
3185 #endif
3186 	return (sifa);
3187 }
3188 
3189 
3190 
3191 /* tcb may be NULL */
3192 struct sctp_ifa *
3193 sctp_source_address_selection(struct sctp_inpcb *inp,
3194     struct sctp_tcb *stcb,
3195     sctp_route_t * ro,
3196     struct sctp_nets *net,
3197     int non_asoc_addr_ok, uint32_t vrf_id)
3198 {
3199 	struct sctp_ifa *answer;
3200 	uint8_t dest_is_priv, dest_is_loop;
3201 	sa_family_t fam;
3202 
3203 #ifdef INET
3204 	struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3205 
3206 #endif
3207 #ifdef INET6
3208 	struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3209 
3210 #endif
3211 
3212 	/**
3213 	 * Rules: - Find the route if needed, cache if I can. - Look at
3214 	 * interface address in route, Is it in the bound list. If so we
3215 	 * have the best source. - If not we must rotate amongst the
3216 	 * addresses.
3217 	 *
3218 	 * Cavets and issues
3219 	 *
3220 	 * Do we need to pay attention to scope. We can have a private address
3221 	 * or a global address we are sourcing or sending to. So if we draw
3222 	 * it out
3223 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3224 	 * For V4
3225 	 * ------------------------------------------
3226 	 *      source     *      dest  *  result
3227 	 * -----------------------------------------
3228 	 * <a>  Private    *    Global  *  NAT
3229 	 * -----------------------------------------
3230 	 * <b>  Private    *    Private *  No problem
3231 	 * -----------------------------------------
3232 	 * <c>  Global     *    Private *  Huh, How will this work?
3233 	 * -----------------------------------------
3234 	 * <d>  Global     *    Global  *  No Problem
3235 	 *------------------------------------------
3236 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3237 	 * For V6
3238 	 *------------------------------------------
3239 	 *      source     *      dest  *  result
3240 	 * -----------------------------------------
3241 	 * <a>  Linklocal  *    Global  *
3242 	 * -----------------------------------------
3243 	 * <b>  Linklocal  * Linklocal  *  No problem
3244 	 * -----------------------------------------
3245 	 * <c>  Global     * Linklocal  *  Huh, How will this work?
3246 	 * -----------------------------------------
3247 	 * <d>  Global     *    Global  *  No Problem
3248 	 *------------------------------------------
3249 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3250 	 *
3251 	 * And then we add to that what happens if there are multiple addresses
3252 	 * assigned to an interface. Remember the ifa on a ifn is a linked
3253 	 * list of addresses. So one interface can have more than one IP
3254 	 * address. What happens if we have both a private and a global
3255 	 * address? Do we then use context of destination to sort out which
3256 	 * one is best? And what about NAT's sending P->G may get you a NAT
3257 	 * translation, or should you select the G thats on the interface in
3258 	 * preference.
3259 	 *
3260 	 * Decisions:
3261 	 *
3262 	 * - count the number of addresses on the interface.
3263 	 * - if it is one, no problem except case <c>.
3264 	 *   For <a> we will assume a NAT out there.
3265 	 * - if there are more than one, then we need to worry about scope P
3266 	 *   or G. We should prefer G -> G and P -> P if possible.
3267 	 *   Then as a secondary fall back to mixed types G->P being a last
3268 	 *   ditch one.
3269 	 * - The above all works for bound all, but bound specific we need to
3270 	 *   use the same concept but instead only consider the bound
3271 	 *   addresses. If the bound set is NOT assigned to the interface then
3272 	 *   we must use rotation amongst the bound addresses..
3273 	 */
3274 	if (ro->ro_rt == NULL) {
3275 		/*
3276 		 * Need a route to cache.
3277 		 */
3278 		SCTP_RTALLOC(ro, vrf_id);
3279 	}
3280 	if (ro->ro_rt == NULL) {
3281 		return (NULL);
3282 	}
3283 	fam = ro->ro_dst.sa_family;
3284 	dest_is_priv = dest_is_loop = 0;
3285 	/* Setup our scopes for the destination */
3286 	switch (fam) {
3287 #ifdef INET
3288 	case AF_INET:
3289 		/* Scope based on outbound address */
3290 		if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3291 			dest_is_loop = 1;
3292 			if (net != NULL) {
3293 				/* mark it as local */
3294 				net->addr_is_local = 1;
3295 			}
3296 		} else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3297 			dest_is_priv = 1;
3298 		}
3299 		break;
3300 #endif
3301 #ifdef INET6
3302 	case AF_INET6:
3303 		/* Scope based on outbound address */
3304 		if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3305 		    SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3306 			/*
3307 			 * If the address is a loopback address, which
3308 			 * consists of "::1" OR "fe80::1%lo0", we are
3309 			 * loopback scope. But we don't use dest_is_priv
3310 			 * (link local addresses).
3311 			 */
3312 			dest_is_loop = 1;
3313 			if (net != NULL) {
3314 				/* mark it as local */
3315 				net->addr_is_local = 1;
3316 			}
3317 		} else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3318 			dest_is_priv = 1;
3319 		}
3320 		break;
3321 #endif
3322 	}
3323 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3324 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst);
3325 	SCTP_IPI_ADDR_RLOCK();
3326 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3327 		/*
3328 		 * Bound all case
3329 		 */
3330 		answer = sctp_choose_boundall(stcb, net, ro, vrf_id,
3331 		    dest_is_priv, dest_is_loop,
3332 		    non_asoc_addr_ok, fam);
3333 		SCTP_IPI_ADDR_RUNLOCK();
3334 		return (answer);
3335 	}
3336 	/*
3337 	 * Subset bound case
3338 	 */
3339 	if (stcb) {
3340 		answer = sctp_choose_boundspecific_stcb(inp, stcb, ro,
3341 		    vrf_id, dest_is_priv,
3342 		    dest_is_loop,
3343 		    non_asoc_addr_ok, fam);
3344 	} else {
3345 		answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3346 		    non_asoc_addr_ok,
3347 		    dest_is_priv,
3348 		    dest_is_loop, fam);
3349 	}
3350 	SCTP_IPI_ADDR_RUNLOCK();
3351 	return (answer);
3352 }
3353 
3354 static int
3355 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, size_t cpsize)
3356 {
3357 	struct cmsghdr cmh;
3358 	int tlen, at, found;
3359 	struct sctp_sndinfo sndinfo;
3360 	struct sctp_prinfo prinfo;
3361 	struct sctp_authinfo authinfo;
3362 
3363 	tlen = SCTP_BUF_LEN(control);
3364 	at = 0;
3365 	found = 0;
3366 	/*
3367 	 * Independent of how many mbufs, find the c_type inside the control
3368 	 * structure and copy out the data.
3369 	 */
3370 	while (at < tlen) {
3371 		if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3372 			/* There is not enough room for one more. */
3373 			return (found);
3374 		}
3375 		m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3376 		if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) {
3377 			/* We dont't have a complete CMSG header. */
3378 			return (found);
3379 		}
3380 		if (((int)cmh.cmsg_len + at) > tlen) {
3381 			/* We don't have the complete CMSG. */
3382 			return (found);
3383 		}
3384 		if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3385 		    ((c_type == cmh.cmsg_type) ||
3386 		    ((c_type == SCTP_SNDRCV) &&
3387 		    ((cmh.cmsg_type == SCTP_SNDINFO) ||
3388 		    (cmh.cmsg_type == SCTP_PRINFO) ||
3389 		    (cmh.cmsg_type == SCTP_AUTHINFO))))) {
3390 			if (c_type == cmh.cmsg_type) {
3391 				if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3392 					return (found);
3393 				}
3394 				/* It is exactly what we want. Copy it out. */
3395 				m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), cpsize, (caddr_t)data);
3396 				return (1);
3397 			} else {
3398 				struct sctp_sndrcvinfo *sndrcvinfo;
3399 
3400 				sndrcvinfo = (struct sctp_sndrcvinfo *)data;
3401 				if (found == 0) {
3402 					if (cpsize < sizeof(struct sctp_sndrcvinfo)) {
3403 						return (found);
3404 					}
3405 					memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo));
3406 				}
3407 				switch (cmh.cmsg_type) {
3408 				case SCTP_SNDINFO:
3409 					if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_sndinfo)) {
3410 						return (found);
3411 					}
3412 					m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_sndinfo), (caddr_t)&sndinfo);
3413 					sndrcvinfo->sinfo_stream = sndinfo.snd_sid;
3414 					sndrcvinfo->sinfo_flags = sndinfo.snd_flags;
3415 					sndrcvinfo->sinfo_ppid = sndinfo.snd_ppid;
3416 					sndrcvinfo->sinfo_context = sndinfo.snd_context;
3417 					sndrcvinfo->sinfo_assoc_id = sndinfo.snd_assoc_id;
3418 					break;
3419 				case SCTP_PRINFO:
3420 					if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_prinfo)) {
3421 						return (found);
3422 					}
3423 					m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_prinfo), (caddr_t)&prinfo);
3424 					sndrcvinfo->sinfo_timetolive = prinfo.pr_value;
3425 					sndrcvinfo->sinfo_flags |= prinfo.pr_policy;
3426 					break;
3427 				case SCTP_AUTHINFO:
3428 					if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_authinfo)) {
3429 						return (found);
3430 					}
3431 					m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_authinfo), (caddr_t)&authinfo);
3432 					sndrcvinfo->sinfo_keynumber_valid = 1;
3433 					sndrcvinfo->sinfo_keynumber = authinfo.auth_keyid;
3434 					break;
3435 				default:
3436 					return (found);
3437 				}
3438 				found = 1;
3439 			}
3440 		}
3441 		at += CMSG_ALIGN(cmh.cmsg_len);
3442 	}
3443 	return (found);
3444 }
3445 
3446 static int
3447 sctp_process_cmsgs_for_init(struct sctp_tcb *stcb, struct mbuf *control, int *error)
3448 {
3449 	struct cmsghdr cmh;
3450 	int tlen, at;
3451 	struct sctp_initmsg initmsg;
3452 
3453 #ifdef INET
3454 	struct sockaddr_in sin;
3455 
3456 #endif
3457 #ifdef INET6
3458 	struct sockaddr_in6 sin6;
3459 
3460 #endif
3461 
3462 	tlen = SCTP_BUF_LEN(control);
3463 	at = 0;
3464 	while (at < tlen) {
3465 		if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3466 			/* There is not enough room for one more. */
3467 			*error = EINVAL;
3468 			return (1);
3469 		}
3470 		m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3471 		if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) {
3472 			/* We dont't have a complete CMSG header. */
3473 			*error = EINVAL;
3474 			return (1);
3475 		}
3476 		if (((int)cmh.cmsg_len + at) > tlen) {
3477 			/* We don't have the complete CMSG. */
3478 			*error = EINVAL;
3479 			return (1);
3480 		}
3481 		if (cmh.cmsg_level == IPPROTO_SCTP) {
3482 			switch (cmh.cmsg_type) {
3483 			case SCTP_INIT:
3484 				if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_initmsg)) {
3485 					*error = EINVAL;
3486 					return (1);
3487 				}
3488 				m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_initmsg), (caddr_t)&initmsg);
3489 				if (initmsg.sinit_max_attempts)
3490 					stcb->asoc.max_init_times = initmsg.sinit_max_attempts;
3491 				if (initmsg.sinit_num_ostreams)
3492 					stcb->asoc.pre_open_streams = initmsg.sinit_num_ostreams;
3493 				if (initmsg.sinit_max_instreams)
3494 					stcb->asoc.max_inbound_streams = initmsg.sinit_max_instreams;
3495 				if (initmsg.sinit_max_init_timeo)
3496 					stcb->asoc.initial_init_rto_max = initmsg.sinit_max_init_timeo;
3497 				if (stcb->asoc.streamoutcnt < stcb->asoc.pre_open_streams) {
3498 					struct sctp_stream_out *tmp_str;
3499 					unsigned int i;
3500 
3501 					/* Default is NOT correct */
3502 					SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, default:%d pre_open:%d\n",
3503 					    stcb->asoc.streamoutcnt, stcb->asoc.pre_open_streams);
3504 					SCTP_TCB_UNLOCK(stcb);
3505 					SCTP_MALLOC(tmp_str,
3506 					    struct sctp_stream_out *,
3507 					    (stcb->asoc.pre_open_streams * sizeof(struct sctp_stream_out)),
3508 					    SCTP_M_STRMO);
3509 					SCTP_TCB_LOCK(stcb);
3510 					if (tmp_str != NULL) {
3511 						SCTP_FREE(stcb->asoc.strmout, SCTP_M_STRMO);
3512 						stcb->asoc.strmout = tmp_str;
3513 						stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt = stcb->asoc.pre_open_streams;
3514 					} else {
3515 						stcb->asoc.pre_open_streams = stcb->asoc.streamoutcnt;
3516 					}
3517 					for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3518 						stcb->asoc.strmout[i].next_sequence_sent = 0;
3519 						TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3520 						stcb->asoc.strmout[i].stream_no = i;
3521 						stcb->asoc.strmout[i].last_msg_incomplete = 0;
3522 						stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], NULL);
3523 					}
3524 				}
3525 				break;
3526 #ifdef INET
3527 			case SCTP_DSTADDRV4:
3528 				if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in_addr)) {
3529 					*error = EINVAL;
3530 					return (1);
3531 				}
3532 				memset(&sin, 0, sizeof(struct sockaddr_in));
3533 				sin.sin_family = AF_INET;
3534 				sin.sin_len = sizeof(struct sockaddr_in);
3535 				sin.sin_port = stcb->rport;
3536 				m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3537 				if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3538 				    (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3539 				    IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3540 					*error = EINVAL;
3541 					return (1);
3542 				}
3543 				if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL,
3544 				    SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3545 					*error = ENOBUFS;
3546 					return (1);
3547 				}
3548 				break;
3549 #endif
3550 #ifdef INET6
3551 			case SCTP_DSTADDRV6:
3552 				if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in6_addr)) {
3553 					*error = EINVAL;
3554 					return (1);
3555 				}
3556 				memset(&sin6, 0, sizeof(struct sockaddr_in6));
3557 				sin6.sin6_family = AF_INET6;
3558 				sin6.sin6_len = sizeof(struct sockaddr_in6);
3559 				sin6.sin6_port = stcb->rport;
3560 				m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3561 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) ||
3562 				    IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
3563 					*error = EINVAL;
3564 					return (1);
3565 				}
3566 #ifdef INET
3567 				if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3568 					in6_sin6_2_sin(&sin, &sin6);
3569 					if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3570 					    (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3571 					    IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3572 						*error = EINVAL;
3573 						return (1);
3574 					}
3575 					if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL,
3576 					    SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3577 						*error = ENOBUFS;
3578 						return (1);
3579 					}
3580 				} else
3581 #endif
3582 					if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin6, NULL,
3583 				    SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3584 					*error = ENOBUFS;
3585 					return (1);
3586 				}
3587 				break;
3588 #endif
3589 			default:
3590 				break;
3591 			}
3592 		}
3593 		at += CMSG_ALIGN(cmh.cmsg_len);
3594 	}
3595 	return (0);
3596 }
3597 
3598 static struct sctp_tcb *
3599 sctp_findassociation_cmsgs(struct sctp_inpcb **inp_p,
3600     in_port_t port,
3601     struct mbuf *control,
3602     struct sctp_nets **net_p,
3603     int *error)
3604 {
3605 	struct cmsghdr cmh;
3606 	int tlen, at;
3607 	struct sctp_tcb *stcb;
3608 	struct sockaddr *addr;
3609 
3610 #ifdef INET
3611 	struct sockaddr_in sin;
3612 
3613 #endif
3614 #ifdef INET6
3615 	struct sockaddr_in6 sin6;
3616 
3617 #endif
3618 
3619 	tlen = SCTP_BUF_LEN(control);
3620 	at = 0;
3621 	while (at < tlen) {
3622 		if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3623 			/* There is not enough room for one more. */
3624 			*error = EINVAL;
3625 			return (NULL);
3626 		}
3627 		m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3628 		if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) {
3629 			/* We dont't have a complete CMSG header. */
3630 			*error = EINVAL;
3631 			return (NULL);
3632 		}
3633 		if (((int)cmh.cmsg_len + at) > tlen) {
3634 			/* We don't have the complete CMSG. */
3635 			*error = EINVAL;
3636 			return (NULL);
3637 		}
3638 		if (cmh.cmsg_level == IPPROTO_SCTP) {
3639 			switch (cmh.cmsg_type) {
3640 #ifdef INET
3641 			case SCTP_DSTADDRV4:
3642 				if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in_addr)) {
3643 					*error = EINVAL;
3644 					return (NULL);
3645 				}
3646 				memset(&sin, 0, sizeof(struct sockaddr_in));
3647 				sin.sin_family = AF_INET;
3648 				sin.sin_len = sizeof(struct sockaddr_in);
3649 				sin.sin_port = port;
3650 				m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3651 				addr = (struct sockaddr *)&sin;
3652 				break;
3653 #endif
3654 #ifdef INET6
3655 			case SCTP_DSTADDRV6:
3656 				if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in6_addr)) {
3657 					*error = EINVAL;
3658 					return (NULL);
3659 				}
3660 				memset(&sin6, 0, sizeof(struct sockaddr_in6));
3661 				sin6.sin6_family = AF_INET6;
3662 				sin6.sin6_len = sizeof(struct sockaddr_in6);
3663 				sin6.sin6_port = port;
3664 				m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3665 #ifdef INET
3666 				if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3667 					in6_sin6_2_sin(&sin, &sin6);
3668 					addr = (struct sockaddr *)&sin;
3669 				} else
3670 #endif
3671 					addr = (struct sockaddr *)&sin6;
3672 				break;
3673 #endif
3674 			default:
3675 				addr = NULL;
3676 				break;
3677 			}
3678 			if (addr) {
3679 				stcb = sctp_findassociation_ep_addr(inp_p, addr, net_p, NULL, NULL);
3680 				if (stcb != NULL) {
3681 					return (stcb);
3682 				}
3683 			}
3684 		}
3685 		at += CMSG_ALIGN(cmh.cmsg_len);
3686 	}
3687 	return (NULL);
3688 }
3689 
3690 static struct mbuf *
3691 sctp_add_cookie(struct mbuf *init, int init_offset,
3692     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3693 {
3694 	struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3695 	struct sctp_state_cookie *stc;
3696 	struct sctp_paramhdr *ph;
3697 	uint8_t *foo;
3698 	int sig_offset;
3699 	uint16_t cookie_sz;
3700 
3701 	mret = NULL;
3702 	mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3703 	    sizeof(struct sctp_paramhdr)), 0,
3704 	    M_DONTWAIT, 1, MT_DATA);
3705 	if (mret == NULL) {
3706 		return (NULL);
3707 	}
3708 	copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3709 	if (copy_init == NULL) {
3710 		sctp_m_freem(mret);
3711 		return (NULL);
3712 	}
3713 #ifdef SCTP_MBUF_LOGGING
3714 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3715 		struct mbuf *mat;
3716 
3717 		for (mat = copy_init; mat; mat = SCTP_BUF_NEXT(mat)) {
3718 			if (SCTP_BUF_IS_EXTENDED(mat)) {
3719 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3720 			}
3721 		}
3722 	}
3723 #endif
3724 	copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3725 	    M_DONTWAIT);
3726 	if (copy_initack == NULL) {
3727 		sctp_m_freem(mret);
3728 		sctp_m_freem(copy_init);
3729 		return (NULL);
3730 	}
3731 #ifdef SCTP_MBUF_LOGGING
3732 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3733 		struct mbuf *mat;
3734 
3735 		for (mat = copy_initack; mat; mat = SCTP_BUF_NEXT(mat)) {
3736 			if (SCTP_BUF_IS_EXTENDED(mat)) {
3737 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3738 			}
3739 		}
3740 	}
3741 #endif
3742 	/* easy side we just drop it on the end */
3743 	ph = mtod(mret, struct sctp_paramhdr *);
3744 	SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3745 	    sizeof(struct sctp_paramhdr);
3746 	stc = (struct sctp_state_cookie *)((caddr_t)ph +
3747 	    sizeof(struct sctp_paramhdr));
3748 	ph->param_type = htons(SCTP_STATE_COOKIE);
3749 	ph->param_length = 0;	/* fill in at the end */
3750 	/* Fill in the stc cookie data */
3751 	memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3752 
3753 	/* tack the INIT and then the INIT-ACK onto the chain */
3754 	cookie_sz = 0;
3755 	for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3756 		cookie_sz += SCTP_BUF_LEN(m_at);
3757 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3758 			SCTP_BUF_NEXT(m_at) = copy_init;
3759 			break;
3760 		}
3761 	}
3762 	for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3763 		cookie_sz += SCTP_BUF_LEN(m_at);
3764 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3765 			SCTP_BUF_NEXT(m_at) = copy_initack;
3766 			break;
3767 		}
3768 	}
3769 	for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3770 		cookie_sz += SCTP_BUF_LEN(m_at);
3771 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3772 			break;
3773 		}
3774 	}
3775 	sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3776 	if (sig == NULL) {
3777 		/* no space, so free the entire chain */
3778 		sctp_m_freem(mret);
3779 		return (NULL);
3780 	}
3781 	SCTP_BUF_LEN(sig) = 0;
3782 	SCTP_BUF_NEXT(m_at) = sig;
3783 	sig_offset = 0;
3784 	foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3785 	memset(foo, 0, SCTP_SIGNATURE_SIZE);
3786 	*signature = foo;
3787 	SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3788 	cookie_sz += SCTP_SIGNATURE_SIZE;
3789 	ph->param_length = htons(cookie_sz);
3790 	return (mret);
3791 }
3792 
3793 
3794 static uint8_t
3795 sctp_get_ect(struct sctp_tcb *stcb)
3796 {
3797 	if ((stcb != NULL) && (stcb->asoc.ecn_allowed == 1)) {
3798 		return (SCTP_ECT0_BIT);
3799 	} else {
3800 		return (0);
3801 	}
3802 }
3803 
3804 static void
3805 sctp_handle_no_route(struct sctp_tcb *stcb,
3806     struct sctp_nets *net,
3807     int so_locked)
3808 {
3809 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n");
3810 
3811 	if (net) {
3812 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was ");
3813 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa);
3814 		if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3815 			if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3816 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3817 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3818 				    stcb,
3819 				    SCTP_FAILED_THRESHOLD,
3820 				    (void *)net,
3821 				    so_locked);
3822 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
3823 				net->dest_state &= ~SCTP_ADDR_PF;
3824 			}
3825 		}
3826 		if (stcb) {
3827 			if (net == stcb->asoc.primary_destination) {
3828 				/* need a new primary */
3829 				struct sctp_nets *alt;
3830 
3831 				alt = sctp_find_alternate_net(stcb, net, 0);
3832 				if (alt != net) {
3833 					if (stcb->asoc.alternate) {
3834 						sctp_free_remote_addr(stcb->asoc.alternate);
3835 					}
3836 					stcb->asoc.alternate = alt;
3837 					atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
3838 					if (net->ro._s_addr) {
3839 						sctp_free_ifa(net->ro._s_addr);
3840 						net->ro._s_addr = NULL;
3841 					}
3842 					net->src_addr_selected = 0;
3843 				}
3844 			}
3845 		}
3846 	}
3847 }
3848 
3849 static int
3850 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3851     struct sctp_tcb *stcb,	/* may be NULL */
3852     struct sctp_nets *net,
3853     struct sockaddr *to,
3854     struct mbuf *m,
3855     uint32_t auth_offset,
3856     struct sctp_auth_chunk *auth,
3857     uint16_t auth_keyid,
3858     int nofragment_flag,
3859     int ecn_ok,
3860     int out_of_asoc_ok,
3861     uint16_t src_port,
3862     uint16_t dest_port,
3863     uint32_t v_tag,
3864     uint16_t port,
3865 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3866     int so_locked SCTP_UNUSED,
3867 #else
3868     int so_locked,
3869 #endif
3870     union sctp_sockstore *over_addr,
3871     struct mbuf *init
3872 )
3873 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3874 {
3875 	/**
3876 	 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet header
3877 	 * WITH an SCTPHDR but no IP header, endpoint inp and sa structure:
3878 	 * - fill in the HMAC digest of any AUTH chunk in the packet.
3879 	 * - calculate and fill in the SCTP checksum.
3880 	 * - prepend an IP address header.
3881 	 * - if boundall use INADDR_ANY.
3882 	 * - if boundspecific do source address selection.
3883 	 * - set fragmentation option for ipV4.
3884 	 * - On return from IP output, check/adjust mtu size of output
3885 	 *   interface and smallest_mtu size as well.
3886 	 */
3887 	/* Will need ifdefs around this */
3888 	struct mbuf *o_pak;
3889 	struct mbuf *newm;
3890 	struct sctphdr *sctphdr;
3891 	int packet_length;
3892 	int ret;
3893 	uint32_t vrf_id;
3894 	sctp_route_t *ro = NULL;
3895 	struct udphdr *udp = NULL;
3896 	uint8_t tos_value;
3897 
3898 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3899 	struct socket *so = NULL;
3900 
3901 #endif
3902 
3903 	if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3904 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3905 		sctp_m_freem(m);
3906 		return (EFAULT);
3907 	}
3908 	if (stcb) {
3909 		vrf_id = stcb->asoc.vrf_id;
3910 	} else {
3911 		vrf_id = inp->def_vrf_id;
3912 	}
3913 
3914 	/* fill in the HMAC digest for any AUTH chunk in the packet */
3915 	if ((auth != NULL) && (stcb != NULL)) {
3916 		sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
3917 	}
3918 	if (net) {
3919 		tos_value = net->dscp;
3920 	} else if (stcb) {
3921 		tos_value = stcb->asoc.default_dscp;
3922 	} else {
3923 		tos_value = inp->sctp_ep.default_dscp;
3924 	}
3925 
3926 	switch (to->sa_family) {
3927 #ifdef INET
3928 	case AF_INET:
3929 		{
3930 			struct ip *ip = NULL;
3931 			sctp_route_t iproute;
3932 			int len;
3933 
3934 			len = sizeof(struct ip) + sizeof(struct sctphdr);
3935 			if (port) {
3936 				len += sizeof(struct udphdr);
3937 			}
3938 			newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3939 			if (newm == NULL) {
3940 				sctp_m_freem(m);
3941 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3942 				return (ENOMEM);
3943 			}
3944 			SCTP_ALIGN_TO_END(newm, len);
3945 			SCTP_BUF_LEN(newm) = len;
3946 			SCTP_BUF_NEXT(newm) = m;
3947 			m = newm;
3948 			if (net != NULL) {
3949 #ifdef INVARIANTS
3950 				if (net->flowidset == 0) {
3951 					panic("Flow ID not set");
3952 				}
3953 #endif
3954 				m->m_pkthdr.flowid = net->flowid;
3955 				m->m_flags |= M_FLOWID;
3956 			} else {
3957 				if ((init != NULL) && (init->m_flags & M_FLOWID)) {
3958 					m->m_pkthdr.flowid = init->m_pkthdr.flowid;
3959 					m->m_flags |= M_FLOWID;
3960 				}
3961 			}
3962 			packet_length = sctp_calculate_len(m);
3963 			ip = mtod(m, struct ip *);
3964 			ip->ip_v = IPVERSION;
3965 			ip->ip_hl = (sizeof(struct ip) >> 2);
3966 			if (tos_value == 0) {
3967 				/*
3968 				 * This means especially, that it is not set
3969 				 * at the SCTP layer. So use the value from
3970 				 * the IP layer.
3971 				 */
3972 				tos_value = inp->ip_inp.inp.inp_ip_tos;
3973 			}
3974 			tos_value &= 0xfc;
3975 			if (ecn_ok) {
3976 				tos_value |= sctp_get_ect(stcb);
3977 			}
3978 			if ((nofragment_flag) && (port == 0)) {
3979 				ip->ip_off = IP_DF;
3980 			} else
3981 				ip->ip_off = 0;
3982 
3983 			/* FreeBSD has a function for ip_id's */
3984 			ip->ip_id = ip_newid();
3985 
3986 			ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3987 			ip->ip_len = packet_length;
3988 			ip->ip_tos = tos_value;
3989 			if (port) {
3990 				ip->ip_p = IPPROTO_UDP;
3991 			} else {
3992 				ip->ip_p = IPPROTO_SCTP;
3993 			}
3994 			ip->ip_sum = 0;
3995 			if (net == NULL) {
3996 				ro = &iproute;
3997 				memset(&iproute, 0, sizeof(iproute));
3998 				memcpy(&ro->ro_dst, to, to->sa_len);
3999 			} else {
4000 				ro = (sctp_route_t *) & net->ro;
4001 			}
4002 			/* Now the address selection part */
4003 			ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
4004 
4005 			/* call the routine to select the src address */
4006 			if (net && out_of_asoc_ok == 0) {
4007 				if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4008 					sctp_free_ifa(net->ro._s_addr);
4009 					net->ro._s_addr = NULL;
4010 					net->src_addr_selected = 0;
4011 					if (ro->ro_rt) {
4012 						RTFREE(ro->ro_rt);
4013 						ro->ro_rt = NULL;
4014 					}
4015 				}
4016 				if (net->src_addr_selected == 0) {
4017 					/* Cache the source address */
4018 					net->ro._s_addr = sctp_source_address_selection(inp, stcb,
4019 					    ro, net, 0,
4020 					    vrf_id);
4021 					net->src_addr_selected = 1;
4022 				}
4023 				if (net->ro._s_addr == NULL) {
4024 					/* No route to host */
4025 					net->src_addr_selected = 0;
4026 					sctp_handle_no_route(stcb, net, so_locked);
4027 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4028 					sctp_m_freem(m);
4029 					return (EHOSTUNREACH);
4030 				}
4031 				ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
4032 			} else {
4033 				if (over_addr == NULL) {
4034 					struct sctp_ifa *_lsrc;
4035 
4036 					_lsrc = sctp_source_address_selection(inp, stcb, ro,
4037 					    net,
4038 					    out_of_asoc_ok,
4039 					    vrf_id);
4040 					if (_lsrc == NULL) {
4041 						sctp_handle_no_route(stcb, net, so_locked);
4042 						SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4043 						sctp_m_freem(m);
4044 						return (EHOSTUNREACH);
4045 					}
4046 					ip->ip_src = _lsrc->address.sin.sin_addr;
4047 					sctp_free_ifa(_lsrc);
4048 				} else {
4049 					ip->ip_src = over_addr->sin.sin_addr;
4050 					SCTP_RTALLOC(ro, vrf_id);
4051 				}
4052 			}
4053 			if (port) {
4054 				if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4055 					sctp_handle_no_route(stcb, net, so_locked);
4056 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4057 					sctp_m_freem(m);
4058 					return (EHOSTUNREACH);
4059 				}
4060 				udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
4061 				udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4062 				udp->uh_dport = port;
4063 				udp->uh_ulen = htons(packet_length - sizeof(struct ip));
4064 				if (V_udp_cksum) {
4065 					udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
4066 				} else {
4067 					udp->uh_sum = 0;
4068 				}
4069 				sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4070 			} else {
4071 				sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
4072 			}
4073 
4074 			sctphdr->src_port = src_port;
4075 			sctphdr->dest_port = dest_port;
4076 			sctphdr->v_tag = v_tag;
4077 			sctphdr->checksum = 0;
4078 
4079 			/*
4080 			 * If source address selection fails and we find no
4081 			 * route then the ip_output should fail as well with
4082 			 * a NO_ROUTE_TO_HOST type error. We probably should
4083 			 * catch that somewhere and abort the association
4084 			 * right away (assuming this is an INIT being sent).
4085 			 */
4086 			if (ro->ro_rt == NULL) {
4087 				/*
4088 				 * src addr selection failed to find a route
4089 				 * (or valid source addr), so we can't get
4090 				 * there from here (yet)!
4091 				 */
4092 				sctp_handle_no_route(stcb, net, so_locked);
4093 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4094 				sctp_m_freem(m);
4095 				return (EHOSTUNREACH);
4096 			}
4097 			if (ro != &iproute) {
4098 				memcpy(&iproute, ro, sizeof(*ro));
4099 			}
4100 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
4101 			    (uint32_t) (ntohl(ip->ip_src.s_addr)));
4102 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
4103 			    (uint32_t) (ntohl(ip->ip_dst.s_addr)));
4104 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
4105 			    ro->ro_rt);
4106 
4107 			if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4108 				/* failed to prepend data, give up */
4109 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4110 				sctp_m_freem(m);
4111 				return (ENOMEM);
4112 			}
4113 #ifdef  SCTP_PACKET_LOGGING
4114 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4115 				sctp_packet_log(m, packet_length);
4116 #endif
4117 			SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4118 			if (port) {
4119 #if defined(SCTP_WITH_NO_CSUM)
4120 				SCTP_STAT_INCR(sctps_sendnocrc);
4121 #else
4122 				if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4123 				    (stcb) &&
4124 				    (stcb->asoc.loopback_scope))) {
4125 					sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
4126 					SCTP_STAT_INCR(sctps_sendswcrc);
4127 				} else {
4128 					SCTP_STAT_INCR(sctps_sendnocrc);
4129 				}
4130 #endif
4131 				if (V_udp_cksum) {
4132 					SCTP_ENABLE_UDP_CSUM(o_pak);
4133 				}
4134 			} else {
4135 #if defined(SCTP_WITH_NO_CSUM)
4136 				SCTP_STAT_INCR(sctps_sendnocrc);
4137 #else
4138 				m->m_pkthdr.csum_flags = CSUM_SCTP;
4139 				m->m_pkthdr.csum_data = 0;
4140 				SCTP_STAT_INCR(sctps_sendhwcrc);
4141 #endif
4142 			}
4143 			/* send it out.  table id is taken from stcb */
4144 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4145 			if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4146 				so = SCTP_INP_SO(inp);
4147 				SCTP_SOCKET_UNLOCK(so, 0);
4148 			}
4149 #endif
4150 			SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
4151 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4152 			if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4153 				atomic_add_int(&stcb->asoc.refcnt, 1);
4154 				SCTP_TCB_UNLOCK(stcb);
4155 				SCTP_SOCKET_LOCK(so, 0);
4156 				SCTP_TCB_LOCK(stcb);
4157 				atomic_subtract_int(&stcb->asoc.refcnt, 1);
4158 			}
4159 #endif
4160 			SCTP_STAT_INCR(sctps_sendpackets);
4161 			SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4162 			if (ret)
4163 				SCTP_STAT_INCR(sctps_senderrors);
4164 
4165 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
4166 			if (net == NULL) {
4167 				/* free tempy routes */
4168 				if (ro->ro_rt) {
4169 					RTFREE(ro->ro_rt);
4170 					ro->ro_rt = NULL;
4171 				}
4172 			} else {
4173 				/*
4174 				 * PMTU check versus smallest asoc MTU goes
4175 				 * here
4176 				 */
4177 				if ((ro->ro_rt != NULL) &&
4178 				    (net->ro._s_addr)) {
4179 					uint32_t mtu;
4180 
4181 					mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4182 					if (net->port) {
4183 						mtu -= sizeof(struct udphdr);
4184 					}
4185 					if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
4186 						sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4187 						net->mtu = mtu;
4188 					}
4189 				} else if (ro->ro_rt == NULL) {
4190 					/* route was freed */
4191 					if (net->ro._s_addr &&
4192 					    net->src_addr_selected) {
4193 						sctp_free_ifa(net->ro._s_addr);
4194 						net->ro._s_addr = NULL;
4195 					}
4196 					net->src_addr_selected = 0;
4197 				}
4198 			}
4199 			return (ret);
4200 		}
4201 #endif
4202 #ifdef INET6
4203 	case AF_INET6:
4204 		{
4205 			uint32_t flowlabel, flowinfo;
4206 			struct ip6_hdr *ip6h;
4207 			struct route_in6 ip6route;
4208 			struct ifnet *ifp;
4209 			struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
4210 			int prev_scope = 0;
4211 			struct sockaddr_in6 lsa6_storage;
4212 			int error;
4213 			u_short prev_port = 0;
4214 			int len;
4215 
4216 			if (net) {
4217 				flowlabel = net->flowlabel;
4218 			} else if (stcb) {
4219 				flowlabel = stcb->asoc.default_flowlabel;
4220 			} else {
4221 				flowlabel = inp->sctp_ep.default_flowlabel;
4222 			}
4223 			if (flowlabel == 0) {
4224 				/*
4225 				 * This means especially, that it is not set
4226 				 * at the SCTP layer. So use the value from
4227 				 * the IP layer.
4228 				 */
4229 				flowlabel = ntohl(((struct in6pcb *)inp)->in6p_flowinfo);
4230 			}
4231 			flowlabel &= 0x000fffff;
4232 			len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr);
4233 			if (port) {
4234 				len += sizeof(struct udphdr);
4235 			}
4236 			newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
4237 			if (newm == NULL) {
4238 				sctp_m_freem(m);
4239 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4240 				return (ENOMEM);
4241 			}
4242 			SCTP_ALIGN_TO_END(newm, len);
4243 			SCTP_BUF_LEN(newm) = len;
4244 			SCTP_BUF_NEXT(newm) = m;
4245 			m = newm;
4246 			if (net != NULL) {
4247 #ifdef INVARIANTS
4248 				if (net->flowidset == 0) {
4249 					panic("Flow ID not set");
4250 				}
4251 #endif
4252 				m->m_pkthdr.flowid = net->flowid;
4253 				m->m_flags |= M_FLOWID;
4254 			} else {
4255 				if ((init != NULL) && (init->m_flags & M_FLOWID)) {
4256 					m->m_pkthdr.flowid = init->m_pkthdr.flowid;
4257 					m->m_flags |= M_FLOWID;
4258 				}
4259 			}
4260 			packet_length = sctp_calculate_len(m);
4261 
4262 			ip6h = mtod(m, struct ip6_hdr *);
4263 			/* protect *sin6 from overwrite */
4264 			sin6 = (struct sockaddr_in6 *)to;
4265 			tmp = *sin6;
4266 			sin6 = &tmp;
4267 
4268 			/* KAME hack: embed scopeid */
4269 			if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4270 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4271 				return (EINVAL);
4272 			}
4273 			if (net == NULL) {
4274 				memset(&ip6route, 0, sizeof(ip6route));
4275 				ro = (sctp_route_t *) & ip6route;
4276 				memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
4277 			} else {
4278 				ro = (sctp_route_t *) & net->ro;
4279 			}
4280 			/*
4281 			 * We assume here that inp_flow is in host byte
4282 			 * order within the TCB!
4283 			 */
4284 			if (tos_value == 0) {
4285 				/*
4286 				 * This means especially, that it is not set
4287 				 * at the SCTP layer. So use the value from
4288 				 * the IP layer.
4289 				 */
4290 				tos_value = (ntohl(((struct in6pcb *)inp)->in6p_flowinfo) >> 20) & 0xff;
4291 			}
4292 			tos_value &= 0xfc;
4293 			if (ecn_ok) {
4294 				tos_value |= sctp_get_ect(stcb);
4295 			}
4296 			flowinfo = 0x06;
4297 			flowinfo <<= 8;
4298 			flowinfo |= tos_value;
4299 			flowinfo <<= 20;
4300 			flowinfo |= flowlabel;
4301 			ip6h->ip6_flow = htonl(flowinfo);
4302 			if (port) {
4303 				ip6h->ip6_nxt = IPPROTO_UDP;
4304 			} else {
4305 				ip6h->ip6_nxt = IPPROTO_SCTP;
4306 			}
4307 			ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
4308 			ip6h->ip6_dst = sin6->sin6_addr;
4309 
4310 			/*
4311 			 * Add SRC address selection here: we can only reuse
4312 			 * to a limited degree the kame src-addr-sel, since
4313 			 * we can try their selection but it may not be
4314 			 * bound.
4315 			 */
4316 			bzero(&lsa6_tmp, sizeof(lsa6_tmp));
4317 			lsa6_tmp.sin6_family = AF_INET6;
4318 			lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
4319 			lsa6 = &lsa6_tmp;
4320 			if (net && out_of_asoc_ok == 0) {
4321 				if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4322 					sctp_free_ifa(net->ro._s_addr);
4323 					net->ro._s_addr = NULL;
4324 					net->src_addr_selected = 0;
4325 					if (ro->ro_rt) {
4326 						RTFREE(ro->ro_rt);
4327 						ro->ro_rt = NULL;
4328 					}
4329 				}
4330 				if (net->src_addr_selected == 0) {
4331 					sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4332 					/* KAME hack: embed scopeid */
4333 					if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4334 						SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4335 						return (EINVAL);
4336 					}
4337 					/* Cache the source address */
4338 					net->ro._s_addr = sctp_source_address_selection(inp,
4339 					    stcb,
4340 					    ro,
4341 					    net,
4342 					    0,
4343 					    vrf_id);
4344 					(void)sa6_recoverscope(sin6);
4345 					net->src_addr_selected = 1;
4346 				}
4347 				if (net->ro._s_addr == NULL) {
4348 					SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
4349 					net->src_addr_selected = 0;
4350 					sctp_handle_no_route(stcb, net, so_locked);
4351 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4352 					sctp_m_freem(m);
4353 					return (EHOSTUNREACH);
4354 				}
4355 				lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
4356 			} else {
4357 				sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
4358 				/* KAME hack: embed scopeid */
4359 				if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4360 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4361 					return (EINVAL);
4362 				}
4363 				if (over_addr == NULL) {
4364 					struct sctp_ifa *_lsrc;
4365 
4366 					_lsrc = sctp_source_address_selection(inp, stcb, ro,
4367 					    net,
4368 					    out_of_asoc_ok,
4369 					    vrf_id);
4370 					if (_lsrc == NULL) {
4371 						sctp_handle_no_route(stcb, net, so_locked);
4372 						SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4373 						sctp_m_freem(m);
4374 						return (EHOSTUNREACH);
4375 					}
4376 					lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
4377 					sctp_free_ifa(_lsrc);
4378 				} else {
4379 					lsa6->sin6_addr = over_addr->sin6.sin6_addr;
4380 					SCTP_RTALLOC(ro, vrf_id);
4381 				}
4382 				(void)sa6_recoverscope(sin6);
4383 			}
4384 			lsa6->sin6_port = inp->sctp_lport;
4385 
4386 			if (ro->ro_rt == NULL) {
4387 				/*
4388 				 * src addr selection failed to find a route
4389 				 * (or valid source addr), so we can't get
4390 				 * there from here!
4391 				 */
4392 				sctp_handle_no_route(stcb, net, so_locked);
4393 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4394 				sctp_m_freem(m);
4395 				return (EHOSTUNREACH);
4396 			}
4397 			/*
4398 			 * XXX: sa6 may not have a valid sin6_scope_id in
4399 			 * the non-SCOPEDROUTING case.
4400 			 */
4401 			bzero(&lsa6_storage, sizeof(lsa6_storage));
4402 			lsa6_storage.sin6_family = AF_INET6;
4403 			lsa6_storage.sin6_len = sizeof(lsa6_storage);
4404 			lsa6_storage.sin6_addr = lsa6->sin6_addr;
4405 			if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
4406 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
4407 				sctp_m_freem(m);
4408 				return (error);
4409 			}
4410 			/* XXX */
4411 			lsa6_storage.sin6_addr = lsa6->sin6_addr;
4412 			lsa6_storage.sin6_port = inp->sctp_lport;
4413 			lsa6 = &lsa6_storage;
4414 			ip6h->ip6_src = lsa6->sin6_addr;
4415 
4416 			if (port) {
4417 				if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4418 					sctp_handle_no_route(stcb, net, so_locked);
4419 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4420 					sctp_m_freem(m);
4421 					return (EHOSTUNREACH);
4422 				}
4423 				udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4424 				udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4425 				udp->uh_dport = port;
4426 				udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
4427 				udp->uh_sum = 0;
4428 				sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4429 			} else {
4430 				sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4431 			}
4432 
4433 			sctphdr->src_port = src_port;
4434 			sctphdr->dest_port = dest_port;
4435 			sctphdr->v_tag = v_tag;
4436 			sctphdr->checksum = 0;
4437 
4438 			/*
4439 			 * We set the hop limit now since there is a good
4440 			 * chance that our ro pointer is now filled
4441 			 */
4442 			ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
4443 			ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4444 
4445 #ifdef SCTP_DEBUG
4446 			/* Copy to be sure something bad is not happening */
4447 			sin6->sin6_addr = ip6h->ip6_dst;
4448 			lsa6->sin6_addr = ip6h->ip6_src;
4449 #endif
4450 
4451 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
4452 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
4453 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
4454 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
4455 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
4456 			if (net) {
4457 				sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4458 				/*
4459 				 * preserve the port and scope for link
4460 				 * local send
4461 				 */
4462 				prev_scope = sin6->sin6_scope_id;
4463 				prev_port = sin6->sin6_port;
4464 			}
4465 			if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4466 				/* failed to prepend data, give up */
4467 				sctp_m_freem(m);
4468 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4469 				return (ENOMEM);
4470 			}
4471 #ifdef  SCTP_PACKET_LOGGING
4472 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4473 				sctp_packet_log(m, packet_length);
4474 #endif
4475 			SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4476 			if (port) {
4477 #if defined(SCTP_WITH_NO_CSUM)
4478 				SCTP_STAT_INCR(sctps_sendnocrc);
4479 #else
4480 				if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4481 				    (stcb) &&
4482 				    (stcb->asoc.loopback_scope))) {
4483 					sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4484 					SCTP_STAT_INCR(sctps_sendswcrc);
4485 				} else {
4486 					SCTP_STAT_INCR(sctps_sendnocrc);
4487 				}
4488 #endif
4489 				if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4490 					udp->uh_sum = 0xffff;
4491 				}
4492 			} else {
4493 #if defined(SCTP_WITH_NO_CSUM)
4494 				SCTP_STAT_INCR(sctps_sendnocrc);
4495 #else
4496 				m->m_pkthdr.csum_flags = CSUM_SCTP;
4497 				m->m_pkthdr.csum_data = 0;
4498 				SCTP_STAT_INCR(sctps_sendhwcrc);
4499 #endif
4500 			}
4501 			/* send it out. table id is taken from stcb */
4502 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4503 			if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4504 				so = SCTP_INP_SO(inp);
4505 				SCTP_SOCKET_UNLOCK(so, 0);
4506 			}
4507 #endif
4508 			SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4509 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4510 			if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4511 				atomic_add_int(&stcb->asoc.refcnt, 1);
4512 				SCTP_TCB_UNLOCK(stcb);
4513 				SCTP_SOCKET_LOCK(so, 0);
4514 				SCTP_TCB_LOCK(stcb);
4515 				atomic_subtract_int(&stcb->asoc.refcnt, 1);
4516 			}
4517 #endif
4518 			if (net) {
4519 				/* for link local this must be done */
4520 				sin6->sin6_scope_id = prev_scope;
4521 				sin6->sin6_port = prev_port;
4522 			}
4523 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4524 			SCTP_STAT_INCR(sctps_sendpackets);
4525 			SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4526 			if (ret) {
4527 				SCTP_STAT_INCR(sctps_senderrors);
4528 			}
4529 			if (net == NULL) {
4530 				/* Now if we had a temp route free it */
4531 				if (ro->ro_rt) {
4532 					RTFREE(ro->ro_rt);
4533 				}
4534 			} else {
4535 				/*
4536 				 * PMTU check versus smallest asoc MTU goes
4537 				 * here
4538 				 */
4539 				if (ro->ro_rt == NULL) {
4540 					/* Route was freed */
4541 					if (net->ro._s_addr &&
4542 					    net->src_addr_selected) {
4543 						sctp_free_ifa(net->ro._s_addr);
4544 						net->ro._s_addr = NULL;
4545 					}
4546 					net->src_addr_selected = 0;
4547 				}
4548 				if ((ro->ro_rt != NULL) &&
4549 				    (net->ro._s_addr)) {
4550 					uint32_t mtu;
4551 
4552 					mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4553 					if (mtu &&
4554 					    (stcb->asoc.smallest_mtu > mtu)) {
4555 						sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4556 						net->mtu = mtu;
4557 						if (net->port) {
4558 							net->mtu -= sizeof(struct udphdr);
4559 						}
4560 					}
4561 				} else if (ifp) {
4562 					if (ND_IFINFO(ifp)->linkmtu &&
4563 					    (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4564 						sctp_mtu_size_reset(inp,
4565 						    &stcb->asoc,
4566 						    ND_IFINFO(ifp)->linkmtu);
4567 					}
4568 				}
4569 			}
4570 			return (ret);
4571 		}
4572 #endif
4573 	default:
4574 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4575 		    ((struct sockaddr *)to)->sa_family);
4576 		sctp_m_freem(m);
4577 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4578 		return (EFAULT);
4579 	}
4580 }
4581 
4582 
4583 void
4584 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4585 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4586     SCTP_UNUSED
4587 #endif
4588 )
4589 {
4590 	struct mbuf *m, *m_at, *mp_last;
4591 	struct sctp_nets *net;
4592 	struct sctp_init_chunk *init;
4593 	struct sctp_supported_addr_param *sup_addr;
4594 	struct sctp_adaptation_layer_indication *ali;
4595 	struct sctp_ecn_supported_param *ecn;
4596 	struct sctp_prsctp_supported_param *prsctp;
4597 	struct sctp_supported_chunk_types_param *pr_supported;
4598 	int cnt_inits_to = 0;
4599 	int padval, ret;
4600 	int num_ext;
4601 	int p_len;
4602 
4603 	/* INIT's always go to the primary (and usually ONLY address) */
4604 	mp_last = NULL;
4605 	net = stcb->asoc.primary_destination;
4606 	if (net == NULL) {
4607 		net = TAILQ_FIRST(&stcb->asoc.nets);
4608 		if (net == NULL) {
4609 			/* TSNH */
4610 			return;
4611 		}
4612 		/* we confirm any address we send an INIT to */
4613 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4614 		(void)sctp_set_primary_addr(stcb, NULL, net);
4615 	} else {
4616 		/* we confirm any address we send an INIT to */
4617 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4618 	}
4619 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4620 #ifdef INET6
4621 	if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4622 		/*
4623 		 * special hook, if we are sending to link local it will not
4624 		 * show up in our private address count.
4625 		 */
4626 		struct sockaddr_in6 *sin6l;
4627 
4628 		sin6l = &net->ro._l_addr.sin6;
4629 		if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4630 			cnt_inits_to = 1;
4631 	}
4632 #endif
4633 	if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4634 		/* This case should not happen */
4635 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4636 		return;
4637 	}
4638 	/* start the INIT timer */
4639 	sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4640 
4641 	m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4642 	if (m == NULL) {
4643 		/* No memory, INIT timer will re-attempt. */
4644 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4645 		return;
4646 	}
4647 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4648 	/*
4649 	 * assume peer supports asconf in order to be able to queue local
4650 	 * address changes while an INIT is in flight and before the assoc
4651 	 * is established.
4652 	 */
4653 	stcb->asoc.peer_supports_asconf = 1;
4654 	/* Now lets put the SCTP header in place */
4655 	init = mtod(m, struct sctp_init_chunk *);
4656 	/* now the chunk header */
4657 	init->ch.chunk_type = SCTP_INITIATION;
4658 	init->ch.chunk_flags = 0;
4659 	/* fill in later from mbuf we build */
4660 	init->ch.chunk_length = 0;
4661 	/* place in my tag */
4662 	init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4663 	/* set up some of the credits. */
4664 	init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4665 	    SCTP_MINIMAL_RWND));
4666 
4667 	init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4668 	init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4669 	init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4670 	/* now the address restriction */
4671 	/* XXX Should we take the address family of the socket into account? */
4672 	sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init +
4673 	    sizeof(*init));
4674 	sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4675 #ifdef INET6
4676 #ifdef INET
4677 	/* we support 2 types: IPv4/IPv6 */
4678 	sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + 2 * sizeof(uint16_t));
4679 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4680 	sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4681 #else
4682 	/* we support 1 type: IPv6 */
4683 	sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + sizeof(uint16_t));
4684 	sup_addr->addr_type[0] = htons(SCTP_IPV6_ADDRESS);
4685 	sup_addr->addr_type[1] = htons(0);	/* this is the padding */
4686 #endif
4687 #else
4688 	/* we support 1 type: IPv4 */
4689 	sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + sizeof(uint16_t));
4690 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4691 	sup_addr->addr_type[1] = htons(0);	/* this is the padding */
4692 #endif
4693 	SCTP_BUF_LEN(m) += sizeof(struct sctp_supported_addr_param);
4694 	/* adaptation layer indication parameter */
4695 	ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(struct sctp_supported_addr_param));
4696 	ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4697 	ali->ph.param_length = htons(sizeof(*ali));
4698 	ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4699 	SCTP_BUF_LEN(m) += sizeof(*ali);
4700 	ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
4701 
4702 	if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4703 		/* Add NAT friendly parameter */
4704 		struct sctp_paramhdr *ph;
4705 
4706 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4707 		ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4708 		ph->param_length = htons(sizeof(struct sctp_paramhdr));
4709 		SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
4710 		ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph));
4711 	}
4712 	/* now any cookie time extensions */
4713 	if (stcb->asoc.cookie_preserve_req) {
4714 		struct sctp_cookie_perserve_param *cookie_preserve;
4715 
4716 		cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4717 		cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4718 		cookie_preserve->ph.param_length = htons(
4719 		    sizeof(*cookie_preserve));
4720 		cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4721 		SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4722 		ecn = (struct sctp_ecn_supported_param *)(
4723 		    (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4724 		stcb->asoc.cookie_preserve_req = 0;
4725 	}
4726 	/* ECN parameter */
4727 	if (stcb->asoc.ecn_allowed == 1) {
4728 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4729 		ecn->ph.param_length = htons(sizeof(*ecn));
4730 		SCTP_BUF_LEN(m) += sizeof(*ecn);
4731 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4732 		    sizeof(*ecn));
4733 	} else {
4734 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4735 	}
4736 	/* And now tell the peer we do pr-sctp */
4737 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4738 	prsctp->ph.param_length = htons(sizeof(*prsctp));
4739 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
4740 
4741 	/* And now tell the peer we do all the extensions */
4742 	pr_supported = (struct sctp_supported_chunk_types_param *)
4743 	    ((caddr_t)prsctp + sizeof(*prsctp));
4744 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4745 	num_ext = 0;
4746 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4747 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4748 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4749 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4750 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4751 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4752 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4753 	}
4754 	if (stcb->asoc.sctp_nr_sack_on_off == 1) {
4755 		pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4756 	}
4757 	p_len = sizeof(*pr_supported) + num_ext;
4758 	pr_supported->ph.param_length = htons(p_len);
4759 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4760 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4761 
4762 
4763 	/* add authentication parameters */
4764 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4765 		struct sctp_auth_random *randp;
4766 		struct sctp_auth_hmac_algo *hmacs;
4767 		struct sctp_auth_chunk_list *chunks;
4768 
4769 		/* attach RANDOM parameter, if available */
4770 		if (stcb->asoc.authinfo.random != NULL) {
4771 			randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4772 			p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4773 			/* random key already contains the header */
4774 			bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4775 			/* zero out any padding required */
4776 			bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4777 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4778 		}
4779 		/* add HMAC_ALGO parameter */
4780 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4781 		p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4782 		    (uint8_t *) hmacs->hmac_ids);
4783 		if (p_len > 0) {
4784 			p_len += sizeof(*hmacs);
4785 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4786 			hmacs->ph.param_length = htons(p_len);
4787 			/* zero out any padding required */
4788 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4789 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4790 		}
4791 		/* add CHUNKS parameter */
4792 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4793 		p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4794 		    chunks->chunk_types);
4795 		if (p_len > 0) {
4796 			p_len += sizeof(*chunks);
4797 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4798 			chunks->ph.param_length = htons(p_len);
4799 			/* zero out any padding required */
4800 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4801 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4802 		}
4803 	}
4804 	/* now the addresses */
4805 	{
4806 		struct sctp_scoping scp;
4807 
4808 		/*
4809 		 * To optimize this we could put the scoping stuff into a
4810 		 * structure and remove the individual uint8's from the
4811 		 * assoc structure. Then we could just sifa in the address
4812 		 * within the stcb. But for now this is a quick hack to get
4813 		 * the address stuff teased apart.
4814 		 */
4815 
4816 		scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4817 		scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4818 		scp.loopback_scope = stcb->asoc.loopback_scope;
4819 		scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4820 		scp.local_scope = stcb->asoc.local_scope;
4821 		scp.site_scope = stcb->asoc.site_scope;
4822 
4823 		sctp_add_addresses_to_i_ia(inp, stcb, &scp, m, cnt_inits_to);
4824 	}
4825 
4826 	/* calulate the size and update pkt header and chunk header */
4827 	p_len = 0;
4828 	for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4829 		if (SCTP_BUF_NEXT(m_at) == NULL)
4830 			mp_last = m_at;
4831 		p_len += SCTP_BUF_LEN(m_at);
4832 	}
4833 	init->ch.chunk_length = htons(p_len);
4834 	/*
4835 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4836 	 * here since the timer will drive a retranmission.
4837 	 */
4838 
4839 	/* I don't expect this to execute but we will be safe here */
4840 	padval = p_len % 4;
4841 	if ((padval) && (mp_last)) {
4842 		/*
4843 		 * The compiler worries that mp_last may not be set even
4844 		 * though I think it is impossible :-> however we add
4845 		 * mp_last here just in case.
4846 		 */
4847 		ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4848 		if (ret) {
4849 			/* Houston we have a problem, no space */
4850 			sctp_m_freem(m);
4851 			return;
4852 		}
4853 	}
4854 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4855 	ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4856 	    (struct sockaddr *)&net->ro._l_addr,
4857 	    m, 0, NULL, 0, 0, 0, 0,
4858 	    inp->sctp_lport, stcb->rport, htonl(0),
4859 	    net->port, so_locked, NULL, NULL);
4860 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4861 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4862 	(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4863 }
4864 
4865 struct mbuf *
4866 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4867     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
4868 {
4869 	/*
4870 	 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4871 	 * being equal to the beginning of the params i.e. (iphlen +
4872 	 * sizeof(struct sctp_init_msg) parse through the parameters to the
4873 	 * end of the mbuf verifying that all parameters are known.
4874 	 *
4875 	 * For unknown parameters build and return a mbuf with
4876 	 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4877 	 * processing this chunk stop, and set *abort_processing to 1.
4878 	 *
4879 	 * By having param_offset be pre-set to where parameters begin it is
4880 	 * hoped that this routine may be reused in the future by new
4881 	 * features.
4882 	 */
4883 	struct sctp_paramhdr *phdr, params;
4884 
4885 	struct mbuf *mat, *op_err;
4886 	char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4887 	int at, limit, pad_needed;
4888 	uint16_t ptype, plen, padded_size;
4889 	int err_at;
4890 
4891 	*abort_processing = 0;
4892 	mat = in_initpkt;
4893 	err_at = 0;
4894 	limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4895 	at = param_offset;
4896 	op_err = NULL;
4897 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4898 	phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4899 	while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4900 		ptype = ntohs(phdr->param_type);
4901 		plen = ntohs(phdr->param_length);
4902 		if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4903 			/* wacked parameter */
4904 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4905 			goto invalid_size;
4906 		}
4907 		limit -= SCTP_SIZE32(plen);
4908 		/*-
4909 		 * All parameters for all chunks that we know/understand are
4910 		 * listed here. We process them other places and make
4911 		 * appropriate stop actions per the upper bits. However this
4912 		 * is the generic routine processor's can call to get back
4913 		 * an operr.. to either incorporate (init-ack) or send.
4914 		 */
4915 		padded_size = SCTP_SIZE32(plen);
4916 		switch (ptype) {
4917 			/* Param's with variable size */
4918 		case SCTP_HEARTBEAT_INFO:
4919 		case SCTP_STATE_COOKIE:
4920 		case SCTP_UNRECOG_PARAM:
4921 		case SCTP_ERROR_CAUSE_IND:
4922 			/* ok skip fwd */
4923 			at += padded_size;
4924 			break;
4925 			/* Param's with variable size within a range */
4926 		case SCTP_CHUNK_LIST:
4927 		case SCTP_SUPPORTED_CHUNK_EXT:
4928 			if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4929 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4930 				goto invalid_size;
4931 			}
4932 			at += padded_size;
4933 			break;
4934 		case SCTP_SUPPORTED_ADDRTYPE:
4935 			if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4936 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4937 				goto invalid_size;
4938 			}
4939 			at += padded_size;
4940 			break;
4941 		case SCTP_RANDOM:
4942 			if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4943 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4944 				goto invalid_size;
4945 			}
4946 			at += padded_size;
4947 			break;
4948 		case SCTP_SET_PRIM_ADDR:
4949 		case SCTP_DEL_IP_ADDRESS:
4950 		case SCTP_ADD_IP_ADDRESS:
4951 			if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4952 			    (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4953 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4954 				goto invalid_size;
4955 			}
4956 			at += padded_size;
4957 			break;
4958 			/* Param's with a fixed size */
4959 		case SCTP_IPV4_ADDRESS:
4960 			if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4961 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4962 				goto invalid_size;
4963 			}
4964 			at += padded_size;
4965 			break;
4966 		case SCTP_IPV6_ADDRESS:
4967 			if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4968 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4969 				goto invalid_size;
4970 			}
4971 			at += padded_size;
4972 			break;
4973 		case SCTP_COOKIE_PRESERVE:
4974 			if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4975 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4976 				goto invalid_size;
4977 			}
4978 			at += padded_size;
4979 			break;
4980 		case SCTP_HAS_NAT_SUPPORT:
4981 			*nat_friendly = 1;
4982 			/* fall through */
4983 		case SCTP_PRSCTP_SUPPORTED:
4984 
4985 			if (padded_size != sizeof(struct sctp_paramhdr)) {
4986 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen);
4987 				goto invalid_size;
4988 			}
4989 			at += padded_size;
4990 			break;
4991 		case SCTP_ECN_CAPABLE:
4992 			if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4993 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4994 				goto invalid_size;
4995 			}
4996 			at += padded_size;
4997 			break;
4998 		case SCTP_ULP_ADAPTATION:
4999 			if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
5000 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
5001 				goto invalid_size;
5002 			}
5003 			at += padded_size;
5004 			break;
5005 		case SCTP_SUCCESS_REPORT:
5006 			if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
5007 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
5008 				goto invalid_size;
5009 			}
5010 			at += padded_size;
5011 			break;
5012 		case SCTP_HOSTNAME_ADDRESS:
5013 			{
5014 				/* We can NOT handle HOST NAME addresses!! */
5015 				int l_len;
5016 
5017 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
5018 				*abort_processing = 1;
5019 				if (op_err == NULL) {
5020 					/* Ok need to try to get a mbuf */
5021 #ifdef INET6
5022 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5023 #else
5024 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5025 #endif
5026 					l_len += plen;
5027 					l_len += sizeof(struct sctp_paramhdr);
5028 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
5029 					if (op_err) {
5030 						SCTP_BUF_LEN(op_err) = 0;
5031 						/*
5032 						 * pre-reserve space for ip
5033 						 * and sctp header  and
5034 						 * chunk hdr
5035 						 */
5036 #ifdef INET6
5037 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5038 #else
5039 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5040 #endif
5041 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5042 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5043 					}
5044 				}
5045 				if (op_err) {
5046 					/* If we have space */
5047 					struct sctp_paramhdr s;
5048 
5049 					if (err_at % 4) {
5050 						uint32_t cpthis = 0;
5051 
5052 						pad_needed = 4 - (err_at % 4);
5053 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
5054 						err_at += pad_needed;
5055 					}
5056 					s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
5057 					s.param_length = htons(sizeof(s) + plen);
5058 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
5059 					err_at += sizeof(s);
5060 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
5061 					if (phdr == NULL) {
5062 						sctp_m_freem(op_err);
5063 						/*
5064 						 * we are out of memory but
5065 						 * we still need to have a
5066 						 * look at what to do (the
5067 						 * system is in trouble
5068 						 * though).
5069 						 */
5070 						return (NULL);
5071 					}
5072 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
5073 				}
5074 				return (op_err);
5075 				break;
5076 			}
5077 		default:
5078 			/*
5079 			 * we do not recognize the parameter figure out what
5080 			 * we do.
5081 			 */
5082 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
5083 			if ((ptype & 0x4000) == 0x4000) {
5084 				/* Report bit is set?? */
5085 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
5086 				if (op_err == NULL) {
5087 					int l_len;
5088 
5089 					/* Ok need to try to get an mbuf */
5090 #ifdef INET6
5091 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5092 #else
5093 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5094 #endif
5095 					l_len += plen;
5096 					l_len += sizeof(struct sctp_paramhdr);
5097 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
5098 					if (op_err) {
5099 						SCTP_BUF_LEN(op_err) = 0;
5100 #ifdef INET6
5101 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5102 #else
5103 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5104 #endif
5105 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5106 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5107 					}
5108 				}
5109 				if (op_err) {
5110 					/* If we have space */
5111 					struct sctp_paramhdr s;
5112 
5113 					if (err_at % 4) {
5114 						uint32_t cpthis = 0;
5115 
5116 						pad_needed = 4 - (err_at % 4);
5117 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
5118 						err_at += pad_needed;
5119 					}
5120 					s.param_type = htons(SCTP_UNRECOG_PARAM);
5121 					s.param_length = htons(sizeof(s) + plen);
5122 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
5123 					err_at += sizeof(s);
5124 					if (plen > sizeof(tempbuf)) {
5125 						plen = sizeof(tempbuf);
5126 					}
5127 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
5128 					if (phdr == NULL) {
5129 						sctp_m_freem(op_err);
5130 						/*
5131 						 * we are out of memory but
5132 						 * we still need to have a
5133 						 * look at what to do (the
5134 						 * system is in trouble
5135 						 * though).
5136 						 */
5137 						op_err = NULL;
5138 						goto more_processing;
5139 					}
5140 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
5141 					err_at += plen;
5142 				}
5143 			}
5144 	more_processing:
5145 			if ((ptype & 0x8000) == 0x0000) {
5146 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
5147 				return (op_err);
5148 			} else {
5149 				/* skip this chunk and continue processing */
5150 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
5151 				at += SCTP_SIZE32(plen);
5152 			}
5153 			break;
5154 
5155 		}
5156 		phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
5157 	}
5158 	return (op_err);
5159 invalid_size:
5160 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
5161 	*abort_processing = 1;
5162 	if ((op_err == NULL) && phdr) {
5163 		int l_len;
5164 
5165 #ifdef INET6
5166 		l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5167 #else
5168 		l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5169 #endif
5170 		l_len += (2 * sizeof(struct sctp_paramhdr));
5171 		op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
5172 		if (op_err) {
5173 			SCTP_BUF_LEN(op_err) = 0;
5174 #ifdef INET6
5175 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5176 #else
5177 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5178 #endif
5179 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5180 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5181 		}
5182 	}
5183 	if ((op_err) && phdr) {
5184 		struct sctp_paramhdr s;
5185 
5186 		if (err_at % 4) {
5187 			uint32_t cpthis = 0;
5188 
5189 			pad_needed = 4 - (err_at % 4);
5190 			m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
5191 			err_at += pad_needed;
5192 		}
5193 		s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5194 		s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
5195 		m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
5196 		err_at += sizeof(s);
5197 		/* Only copy back the p-hdr that caused the issue */
5198 		m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
5199 	}
5200 	return (op_err);
5201 }
5202 
5203 static int
5204 sctp_are_there_new_addresses(struct sctp_association *asoc,
5205     struct mbuf *in_initpkt, int offset)
5206 {
5207 	/*
5208 	 * Given a INIT packet, look through the packet to verify that there
5209 	 * are NO new addresses. As we go through the parameters add reports
5210 	 * of any un-understood parameters that require an error.  Also we
5211 	 * must return (1) to drop the packet if we see a un-understood
5212 	 * parameter that tells us to drop the chunk.
5213 	 */
5214 	struct sockaddr *sa_touse;
5215 	struct sockaddr *sa;
5216 	struct sctp_paramhdr *phdr, params;
5217 	uint16_t ptype, plen;
5218 	uint8_t fnd;
5219 	struct sctp_nets *net;
5220 	struct ip *iph;
5221 
5222 #ifdef INET
5223 	struct sockaddr_in sin4, *sa4;
5224 
5225 #endif
5226 #ifdef INET6
5227 	struct sockaddr_in6 sin6, *sa6;
5228 	struct ip6_hdr *ip6h;
5229 
5230 #endif
5231 
5232 #ifdef INET
5233 	memset(&sin4, 0, sizeof(sin4));
5234 	sin4.sin_family = AF_INET;
5235 	sin4.sin_len = sizeof(sin4);
5236 #endif
5237 #ifdef INET6
5238 	memset(&sin6, 0, sizeof(sin6));
5239 	sin6.sin6_family = AF_INET6;
5240 	sin6.sin6_len = sizeof(sin6);
5241 #endif
5242 	sa_touse = NULL;
5243 	/* First what about the src address of the pkt ? */
5244 	iph = mtod(in_initpkt, struct ip *);
5245 	switch (iph->ip_v) {
5246 #ifdef INET
5247 	case IPVERSION:
5248 		/* source addr is IPv4 */
5249 		sin4.sin_addr = iph->ip_src;
5250 		sa_touse = (struct sockaddr *)&sin4;
5251 		break;
5252 #endif
5253 #ifdef INET6
5254 	case IPV6_VERSION >> 4:
5255 		/* source addr is IPv6 */
5256 		ip6h = mtod(in_initpkt, struct ip6_hdr *);
5257 		sin6.sin6_addr = ip6h->ip6_src;
5258 		sa_touse = (struct sockaddr *)&sin6;
5259 		break;
5260 #endif
5261 	default:
5262 		return (1);
5263 	}
5264 
5265 	fnd = 0;
5266 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5267 		sa = (struct sockaddr *)&net->ro._l_addr;
5268 		if (sa->sa_family == sa_touse->sa_family) {
5269 #ifdef INET
5270 			if (sa->sa_family == AF_INET) {
5271 				sa4 = (struct sockaddr_in *)sa;
5272 				if (sa4->sin_addr.s_addr == sin4.sin_addr.s_addr) {
5273 					fnd = 1;
5274 					break;
5275 				}
5276 			}
5277 #endif
5278 #ifdef INET6
5279 			if (sa->sa_family == AF_INET6) {
5280 				sa6 = (struct sockaddr_in6 *)sa;
5281 				if (SCTP6_ARE_ADDR_EQUAL(sa6, &sin6)) {
5282 					fnd = 1;
5283 					break;
5284 				}
5285 			}
5286 #endif
5287 		}
5288 	}
5289 	if (fnd == 0) {
5290 		/* New address added! no need to look futher. */
5291 		return (1);
5292 	}
5293 	/* Ok so far lets munge through the rest of the packet */
5294 	offset += sizeof(struct sctp_init_chunk);
5295 	phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
5296 	while (phdr) {
5297 		sa_touse = NULL;
5298 		ptype = ntohs(phdr->param_type);
5299 		plen = ntohs(phdr->param_length);
5300 		switch (ptype) {
5301 #ifdef INET
5302 		case SCTP_IPV4_ADDRESS:
5303 			{
5304 				struct sctp_ipv4addr_param *p4, p4_buf;
5305 
5306 				phdr = sctp_get_next_param(in_initpkt, offset,
5307 				    (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
5308 				if (plen != sizeof(struct sctp_ipv4addr_param) ||
5309 				    phdr == NULL) {
5310 					return (1);
5311 				}
5312 				p4 = (struct sctp_ipv4addr_param *)phdr;
5313 				sin4.sin_addr.s_addr = p4->addr;
5314 				sa_touse = (struct sockaddr *)&sin4;
5315 				break;
5316 			}
5317 #endif
5318 #ifdef INET6
5319 		case SCTP_IPV6_ADDRESS:
5320 			{
5321 				struct sctp_ipv6addr_param *p6, p6_buf;
5322 
5323 				phdr = sctp_get_next_param(in_initpkt, offset,
5324 				    (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
5325 				if (plen != sizeof(struct sctp_ipv6addr_param) ||
5326 				    phdr == NULL) {
5327 					return (1);
5328 				}
5329 				p6 = (struct sctp_ipv6addr_param *)phdr;
5330 				memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
5331 				    sizeof(p6->addr));
5332 				sa_touse = (struct sockaddr *)&sin6;
5333 				break;
5334 			}
5335 #endif
5336 		default:
5337 			sa_touse = NULL;
5338 			break;
5339 		}
5340 		if (sa_touse) {
5341 			/* ok, sa_touse points to one to check */
5342 			fnd = 0;
5343 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5344 				sa = (struct sockaddr *)&net->ro._l_addr;
5345 				if (sa->sa_family != sa_touse->sa_family) {
5346 					continue;
5347 				}
5348 #ifdef INET
5349 				if (sa->sa_family == AF_INET) {
5350 					sa4 = (struct sockaddr_in *)sa;
5351 					if (sa4->sin_addr.s_addr ==
5352 					    sin4.sin_addr.s_addr) {
5353 						fnd = 1;
5354 						break;
5355 					}
5356 				}
5357 #endif
5358 #ifdef INET6
5359 				if (sa->sa_family == AF_INET6) {
5360 					sa6 = (struct sockaddr_in6 *)sa;
5361 					if (SCTP6_ARE_ADDR_EQUAL(
5362 					    sa6, &sin6)) {
5363 						fnd = 1;
5364 						break;
5365 					}
5366 				}
5367 #endif
5368 			}
5369 			if (!fnd) {
5370 				/* New addr added! no need to look further */
5371 				return (1);
5372 			}
5373 		}
5374 		offset += SCTP_SIZE32(plen);
5375 		phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
5376 	}
5377 	return (0);
5378 }
5379 
5380 /*
5381  * Given a MBUF chain that was sent into us containing an INIT. Build a
5382  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
5383  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
5384  * message (i.e. the struct sctp_init_msg).
5385  */
5386 void
5387 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
5388     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
5389     struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
5390 {
5391 	struct sctp_association *asoc;
5392 	struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
5393 	struct sctp_init_ack_chunk *initack;
5394 	struct sctp_adaptation_layer_indication *ali;
5395 	struct sctp_ecn_supported_param *ecn;
5396 	struct sctp_prsctp_supported_param *prsctp;
5397 	struct sctp_supported_chunk_types_param *pr_supported;
5398 	union sctp_sockstore store, store1, *over_addr;
5399 
5400 #ifdef INET
5401 	struct sockaddr_in *sin, *to_sin;
5402 
5403 #endif
5404 #ifdef INET6
5405 	struct sockaddr_in6 *sin6, *to_sin6;
5406 
5407 #endif
5408 	struct ip *iph;
5409 
5410 #ifdef INET6
5411 	struct ip6_hdr *ip6;
5412 
5413 #endif
5414 	struct sockaddr *to;
5415 	struct sctp_state_cookie stc;
5416 	struct sctp_nets *net = NULL;
5417 	uint8_t *signature = NULL;
5418 	int cnt_inits_to = 0;
5419 	uint16_t his_limit, i_want;
5420 	int abort_flag, padval;
5421 	int num_ext;
5422 	int p_len;
5423 	int nat_friendly = 0;
5424 	struct socket *so;
5425 
5426 	if (stcb)
5427 		asoc = &stcb->asoc;
5428 	else
5429 		asoc = NULL;
5430 	mp_last = NULL;
5431 	if ((asoc != NULL) &&
5432 	    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
5433 	    (sctp_are_there_new_addresses(asoc, init_pkt, offset))) {
5434 		/* new addresses, out of here in non-cookie-wait states */
5435 		/*
5436 		 * Send a ABORT, we don't add the new address error clause
5437 		 * though we even set the T bit and copy in the 0 tag.. this
5438 		 * looks no different than if no listener was present.
5439 		 */
5440 		sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
5441 		return;
5442 	}
5443 	abort_flag = 0;
5444 	op_err = sctp_arethere_unrecognized_parameters(init_pkt,
5445 	    (offset + sizeof(struct sctp_init_chunk)),
5446 	    &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
5447 	if (abort_flag) {
5448 do_a_abort:
5449 		sctp_send_abort(init_pkt, iphlen, sh,
5450 		    init_chk->init.initiate_tag, op_err, vrf_id, port);
5451 		return;
5452 	}
5453 	m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
5454 	if (m == NULL) {
5455 		/* No memory, INIT timer will re-attempt. */
5456 		if (op_err)
5457 			sctp_m_freem(op_err);
5458 		return;
5459 	}
5460 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
5461 
5462 	/* the time I built cookie */
5463 	(void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
5464 
5465 	/* populate any tie tags */
5466 	if (asoc != NULL) {
5467 		/* unlock before tag selections */
5468 		stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5469 		stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5470 		stc.cookie_life = asoc->cookie_life;
5471 		net = asoc->primary_destination;
5472 	} else {
5473 		stc.tie_tag_my_vtag = 0;
5474 		stc.tie_tag_peer_vtag = 0;
5475 		/* life I will award this cookie */
5476 		stc.cookie_life = inp->sctp_ep.def_cookie_life;
5477 	}
5478 
5479 	/* copy in the ports for later check */
5480 	stc.myport = sh->dest_port;
5481 	stc.peerport = sh->src_port;
5482 
5483 	/*
5484 	 * If we wanted to honor cookie life extentions, we would add to
5485 	 * stc.cookie_life. For now we should NOT honor any extension
5486 	 */
5487 	stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5488 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5489 		struct inpcb *in_inp;
5490 
5491 		/* Its a V6 socket */
5492 		in_inp = (struct inpcb *)inp;
5493 		stc.ipv6_addr_legal = 1;
5494 		/* Now look at the binding flag to see if V4 will be legal */
5495 		if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
5496 			stc.ipv4_addr_legal = 1;
5497 		} else {
5498 			/* V4 addresses are NOT legal on the association */
5499 			stc.ipv4_addr_legal = 0;
5500 		}
5501 	} else {
5502 		/* Its a V4 socket, no - V6 */
5503 		stc.ipv4_addr_legal = 1;
5504 		stc.ipv6_addr_legal = 0;
5505 	}
5506 
5507 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
5508 	stc.ipv4_scope = 1;
5509 #else
5510 	stc.ipv4_scope = 0;
5511 #endif
5512 	/* now for scope setup */
5513 	memset((caddr_t)&store, 0, sizeof(store));
5514 	memset((caddr_t)&store1, 0, sizeof(store1));
5515 #ifdef INET
5516 	sin = &store.sin;
5517 	to_sin = &store1.sin;
5518 #endif
5519 #ifdef INET6
5520 	sin6 = &store.sin6;
5521 	to_sin6 = &store1.sin6;
5522 #endif
5523 	iph = mtod(init_pkt, struct ip *);
5524 	/* establish the to_addr's */
5525 	switch (iph->ip_v) {
5526 #ifdef INET
5527 	case IPVERSION:
5528 		to_sin->sin_port = sh->dest_port;
5529 		to_sin->sin_family = AF_INET;
5530 		to_sin->sin_len = sizeof(struct sockaddr_in);
5531 		to_sin->sin_addr = iph->ip_dst;
5532 		break;
5533 #endif
5534 #ifdef INET6
5535 	case IPV6_VERSION >> 4:
5536 		ip6 = mtod(init_pkt, struct ip6_hdr *);
5537 		to_sin6->sin6_addr = ip6->ip6_dst;
5538 		to_sin6->sin6_scope_id = 0;
5539 		to_sin6->sin6_port = sh->dest_port;
5540 		to_sin6->sin6_family = AF_INET6;
5541 		to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5542 		break;
5543 #endif
5544 	default:
5545 		goto do_a_abort;
5546 		break;
5547 	}
5548 
5549 	if (net == NULL) {
5550 		to = (struct sockaddr *)&store;
5551 		switch (iph->ip_v) {
5552 #ifdef INET
5553 		case IPVERSION:
5554 			{
5555 				sin->sin_family = AF_INET;
5556 				sin->sin_len = sizeof(struct sockaddr_in);
5557 				sin->sin_port = sh->src_port;
5558 				sin->sin_addr = iph->ip_src;
5559 				/* lookup address */
5560 				stc.address[0] = sin->sin_addr.s_addr;
5561 				stc.address[1] = 0;
5562 				stc.address[2] = 0;
5563 				stc.address[3] = 0;
5564 				stc.addr_type = SCTP_IPV4_ADDRESS;
5565 				/* local from address */
5566 				stc.laddress[0] = to_sin->sin_addr.s_addr;
5567 				stc.laddress[1] = 0;
5568 				stc.laddress[2] = 0;
5569 				stc.laddress[3] = 0;
5570 				stc.laddr_type = SCTP_IPV4_ADDRESS;
5571 				/* scope_id is only for v6 */
5572 				stc.scope_id = 0;
5573 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5574 				if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5575 					stc.ipv4_scope = 1;
5576 				}
5577 #else
5578 				stc.ipv4_scope = 1;
5579 #endif				/* SCTP_DONT_DO_PRIVADDR_SCOPE */
5580 				/* Must use the address in this case */
5581 				if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5582 					stc.loopback_scope = 1;
5583 					stc.ipv4_scope = 1;
5584 					stc.site_scope = 1;
5585 					stc.local_scope = 0;
5586 				}
5587 				break;
5588 			}
5589 #endif
5590 #ifdef INET6
5591 		case IPV6_VERSION >> 4:
5592 			{
5593 				ip6 = mtod(init_pkt, struct ip6_hdr *);
5594 				sin6->sin6_family = AF_INET6;
5595 				sin6->sin6_len = sizeof(struct sockaddr_in6);
5596 				sin6->sin6_port = sh->src_port;
5597 				sin6->sin6_addr = ip6->ip6_src;
5598 				/* lookup address */
5599 				memcpy(&stc.address, &sin6->sin6_addr,
5600 				    sizeof(struct in6_addr));
5601 				sin6->sin6_scope_id = 0;
5602 				stc.addr_type = SCTP_IPV6_ADDRESS;
5603 				stc.scope_id = 0;
5604 				if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5605 					/*
5606 					 * FIX ME: does this have scope from
5607 					 * rcvif?
5608 					 */
5609 					(void)sa6_recoverscope(sin6);
5610 					stc.scope_id = sin6->sin6_scope_id;
5611 					sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5612 					stc.loopback_scope = 1;
5613 					stc.local_scope = 0;
5614 					stc.site_scope = 1;
5615 					stc.ipv4_scope = 1;
5616 				} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5617 					/*
5618 					 * If the new destination is a
5619 					 * LINK_LOCAL we must have common
5620 					 * both site and local scope. Don't
5621 					 * set local scope though since we
5622 					 * must depend on the source to be
5623 					 * added implicitly. We cannot
5624 					 * assure just because we share one
5625 					 * link that all links are common.
5626 					 */
5627 					stc.local_scope = 0;
5628 					stc.site_scope = 1;
5629 					stc.ipv4_scope = 1;
5630 					/*
5631 					 * we start counting for the private
5632 					 * address stuff at 1. since the
5633 					 * link local we source from won't
5634 					 * show up in our scoped count.
5635 					 */
5636 					cnt_inits_to = 1;
5637 					/*
5638 					 * pull out the scope_id from
5639 					 * incoming pkt
5640 					 */
5641 					/*
5642 					 * FIX ME: does this have scope from
5643 					 * rcvif?
5644 					 */
5645 					(void)sa6_recoverscope(sin6);
5646 					stc.scope_id = sin6->sin6_scope_id;
5647 					sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5648 				} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5649 					/*
5650 					 * If the new destination is
5651 					 * SITE_LOCAL then we must have site
5652 					 * scope in common.
5653 					 */
5654 					stc.site_scope = 1;
5655 				}
5656 				memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5657 				stc.laddr_type = SCTP_IPV6_ADDRESS;
5658 				break;
5659 			}
5660 #endif
5661 		default:
5662 			/* TSNH */
5663 			goto do_a_abort;
5664 			break;
5665 		}
5666 	} else {
5667 		/* set the scope per the existing tcb */
5668 
5669 #ifdef INET6
5670 		struct sctp_nets *lnet;
5671 
5672 #endif
5673 
5674 		stc.loopback_scope = asoc->loopback_scope;
5675 		stc.ipv4_scope = asoc->ipv4_local_scope;
5676 		stc.site_scope = asoc->site_scope;
5677 		stc.local_scope = asoc->local_scope;
5678 #ifdef INET6
5679 		/* Why do we not consider IPv4 LL addresses? */
5680 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5681 			if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5682 				if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5683 					/*
5684 					 * if we have a LL address, start
5685 					 * counting at 1.
5686 					 */
5687 					cnt_inits_to = 1;
5688 				}
5689 			}
5690 		}
5691 #endif
5692 		/* use the net pointer */
5693 		to = (struct sockaddr *)&net->ro._l_addr;
5694 		switch (to->sa_family) {
5695 #ifdef INET
5696 		case AF_INET:
5697 			sin = (struct sockaddr_in *)to;
5698 			stc.address[0] = sin->sin_addr.s_addr;
5699 			stc.address[1] = 0;
5700 			stc.address[2] = 0;
5701 			stc.address[3] = 0;
5702 			stc.addr_type = SCTP_IPV4_ADDRESS;
5703 			if (net->src_addr_selected == 0) {
5704 				/*
5705 				 * strange case here, the INIT should have
5706 				 * did the selection.
5707 				 */
5708 				net->ro._s_addr = sctp_source_address_selection(inp,
5709 				    stcb, (sctp_route_t *) & net->ro,
5710 				    net, 0, vrf_id);
5711 				if (net->ro._s_addr == NULL)
5712 					return;
5713 
5714 				net->src_addr_selected = 1;
5715 
5716 			}
5717 			stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5718 			stc.laddress[1] = 0;
5719 			stc.laddress[2] = 0;
5720 			stc.laddress[3] = 0;
5721 			stc.laddr_type = SCTP_IPV4_ADDRESS;
5722 			/* scope_id is only for v6 */
5723 			stc.scope_id = 0;
5724 			break;
5725 #endif
5726 #ifdef INET6
5727 		case AF_INET6:
5728 			sin6 = (struct sockaddr_in6 *)to;
5729 			memcpy(&stc.address, &sin6->sin6_addr,
5730 			    sizeof(struct in6_addr));
5731 			stc.addr_type = SCTP_IPV6_ADDRESS;
5732 			stc.scope_id = sin6->sin6_scope_id;
5733 			if (net->src_addr_selected == 0) {
5734 				/*
5735 				 * strange case here, the INIT should have
5736 				 * did the selection.
5737 				 */
5738 				net->ro._s_addr = sctp_source_address_selection(inp,
5739 				    stcb, (sctp_route_t *) & net->ro,
5740 				    net, 0, vrf_id);
5741 				if (net->ro._s_addr == NULL)
5742 					return;
5743 
5744 				net->src_addr_selected = 1;
5745 			}
5746 			memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5747 			    sizeof(struct in6_addr));
5748 			stc.laddr_type = SCTP_IPV6_ADDRESS;
5749 			break;
5750 #endif
5751 		}
5752 	}
5753 	/* Now lets put the SCTP header in place */
5754 	initack = mtod(m, struct sctp_init_ack_chunk *);
5755 	/* Save it off for quick ref */
5756 	stc.peers_vtag = init_chk->init.initiate_tag;
5757 	/* who are we */
5758 	memcpy(stc.identification, SCTP_VERSION_STRING,
5759 	    min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5760 	memset(stc.reserved, 0, SCTP_RESERVE_SPACE);
5761 	/* now the chunk header */
5762 	initack->ch.chunk_type = SCTP_INITIATION_ACK;
5763 	initack->ch.chunk_flags = 0;
5764 	/* fill in later from mbuf we build */
5765 	initack->ch.chunk_length = 0;
5766 	/* place in my tag */
5767 	if ((asoc != NULL) &&
5768 	    ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5769 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5770 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5771 		/* re-use the v-tags and init-seq here */
5772 		initack->init.initiate_tag = htonl(asoc->my_vtag);
5773 		initack->init.initial_tsn = htonl(asoc->init_seq_number);
5774 	} else {
5775 		uint32_t vtag, itsn;
5776 
5777 		if (hold_inp_lock) {
5778 			SCTP_INP_INCR_REF(inp);
5779 			SCTP_INP_RUNLOCK(inp);
5780 		}
5781 		if (asoc) {
5782 			atomic_add_int(&asoc->refcnt, 1);
5783 			SCTP_TCB_UNLOCK(stcb);
5784 	new_tag:
5785 			vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5786 			if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5787 				/*
5788 				 * Got a duplicate vtag on some guy behind a
5789 				 * nat make sure we don't use it.
5790 				 */
5791 				goto new_tag;
5792 			}
5793 			initack->init.initiate_tag = htonl(vtag);
5794 			/* get a TSN to use too */
5795 			itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5796 			initack->init.initial_tsn = htonl(itsn);
5797 			SCTP_TCB_LOCK(stcb);
5798 			atomic_add_int(&asoc->refcnt, -1);
5799 		} else {
5800 			vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5801 			initack->init.initiate_tag = htonl(vtag);
5802 			/* get a TSN to use too */
5803 			initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5804 		}
5805 		if (hold_inp_lock) {
5806 			SCTP_INP_RLOCK(inp);
5807 			SCTP_INP_DECR_REF(inp);
5808 		}
5809 	}
5810 	/* save away my tag to */
5811 	stc.my_vtag = initack->init.initiate_tag;
5812 
5813 	/* set up some of the credits. */
5814 	so = inp->sctp_socket;
5815 	if (so == NULL) {
5816 		/* memory problem */
5817 		sctp_m_freem(m);
5818 		return;
5819 	} else {
5820 		initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5821 	}
5822 	/* set what I want */
5823 	his_limit = ntohs(init_chk->init.num_inbound_streams);
5824 	/* choose what I want */
5825 	if (asoc != NULL) {
5826 		if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5827 			i_want = asoc->streamoutcnt;
5828 		} else {
5829 			i_want = inp->sctp_ep.pre_open_stream_count;
5830 		}
5831 	} else {
5832 		i_want = inp->sctp_ep.pre_open_stream_count;
5833 	}
5834 	if (his_limit < i_want) {
5835 		/* I Want more :< */
5836 		initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5837 	} else {
5838 		/* I can have what I want :> */
5839 		initack->init.num_outbound_streams = htons(i_want);
5840 	}
5841 	/* tell him his limt. */
5842 	initack->init.num_inbound_streams =
5843 	    htons(inp->sctp_ep.max_open_streams_intome);
5844 
5845 	/* adaptation layer indication parameter */
5846 	ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack));
5847 	ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5848 	ali->ph.param_length = htons(sizeof(*ali));
5849 	ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5850 	SCTP_BUF_LEN(m) += sizeof(*ali);
5851 	ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
5852 
5853 	/* ECN parameter */
5854 	if (((asoc != NULL) && (asoc->ecn_allowed == 1)) ||
5855 	    (inp->sctp_ecn_enable == 1)) {
5856 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5857 		ecn->ph.param_length = htons(sizeof(*ecn));
5858 		SCTP_BUF_LEN(m) += sizeof(*ecn);
5859 
5860 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5861 		    sizeof(*ecn));
5862 	} else {
5863 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5864 	}
5865 	/* And now tell the peer we do  pr-sctp */
5866 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5867 	prsctp->ph.param_length = htons(sizeof(*prsctp));
5868 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
5869 	if (nat_friendly) {
5870 		/* Add NAT friendly parameter */
5871 		struct sctp_paramhdr *ph;
5872 
5873 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5874 		ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5875 		ph->param_length = htons(sizeof(struct sctp_paramhdr));
5876 		SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
5877 	}
5878 	/* And now tell the peer we do all the extensions */
5879 	pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5880 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5881 	num_ext = 0;
5882 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5883 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5884 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5885 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5886 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5887 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5888 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5889 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
5890 		pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5891 	p_len = sizeof(*pr_supported) + num_ext;
5892 	pr_supported->ph.param_length = htons(p_len);
5893 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5894 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5895 
5896 	/* add authentication parameters */
5897 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5898 		struct sctp_auth_random *randp;
5899 		struct sctp_auth_hmac_algo *hmacs;
5900 		struct sctp_auth_chunk_list *chunks;
5901 		uint16_t random_len;
5902 
5903 		/* generate and add RANDOM parameter */
5904 		random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5905 		randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5906 		randp->ph.param_type = htons(SCTP_RANDOM);
5907 		p_len = sizeof(*randp) + random_len;
5908 		randp->ph.param_length = htons(p_len);
5909 		SCTP_READ_RANDOM(randp->random_data, random_len);
5910 		/* zero out any padding required */
5911 		bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5912 		SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5913 
5914 		/* add HMAC_ALGO parameter */
5915 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5916 		p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5917 		    (uint8_t *) hmacs->hmac_ids);
5918 		if (p_len > 0) {
5919 			p_len += sizeof(*hmacs);
5920 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5921 			hmacs->ph.param_length = htons(p_len);
5922 			/* zero out any padding required */
5923 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5924 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5925 		}
5926 		/* add CHUNKS parameter */
5927 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5928 		p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5929 		    chunks->chunk_types);
5930 		if (p_len > 0) {
5931 			p_len += sizeof(*chunks);
5932 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5933 			chunks->ph.param_length = htons(p_len);
5934 			/* zero out any padding required */
5935 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5936 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5937 		}
5938 	}
5939 	m_at = m;
5940 	/* now the addresses */
5941 	{
5942 		struct sctp_scoping scp;
5943 
5944 		/*
5945 		 * To optimize this we could put the scoping stuff into a
5946 		 * structure and remove the individual uint8's from the stc
5947 		 * structure. Then we could just sifa in the address within
5948 		 * the stc.. but for now this is a quick hack to get the
5949 		 * address stuff teased apart.
5950 		 */
5951 		scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5952 		scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5953 		scp.loopback_scope = stc.loopback_scope;
5954 		scp.ipv4_local_scope = stc.ipv4_scope;
5955 		scp.local_scope = stc.local_scope;
5956 		scp.site_scope = stc.site_scope;
5957 		m_at = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_at, cnt_inits_to);
5958 	}
5959 
5960 	/* tack on the operational error if present */
5961 	if (op_err) {
5962 		struct mbuf *ol;
5963 		int llen;
5964 
5965 		llen = 0;
5966 		ol = op_err;
5967 
5968 		while (ol) {
5969 			llen += SCTP_BUF_LEN(ol);
5970 			ol = SCTP_BUF_NEXT(ol);
5971 		}
5972 		if (llen % 4) {
5973 			/* must add a pad to the param */
5974 			uint32_t cpthis = 0;
5975 			int padlen;
5976 
5977 			padlen = 4 - (llen % 4);
5978 			m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5979 		}
5980 		while (SCTP_BUF_NEXT(m_at) != NULL) {
5981 			m_at = SCTP_BUF_NEXT(m_at);
5982 		}
5983 		SCTP_BUF_NEXT(m_at) = op_err;
5984 		while (SCTP_BUF_NEXT(m_at) != NULL) {
5985 			m_at = SCTP_BUF_NEXT(m_at);
5986 		}
5987 	}
5988 	/* pre-calulate the size and update pkt header and chunk header */
5989 	p_len = 0;
5990 	for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5991 		p_len += SCTP_BUF_LEN(m_tmp);
5992 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5993 			/* m_tmp should now point to last one */
5994 			break;
5995 		}
5996 	}
5997 
5998 	/* Now we must build a cookie */
5999 	m_cookie = sctp_add_cookie(init_pkt, offset, m, 0, &stc, &signature);
6000 	if (m_cookie == NULL) {
6001 		/* memory problem */
6002 		sctp_m_freem(m);
6003 		return;
6004 	}
6005 	/* Now append the cookie to the end and update the space/size */
6006 	SCTP_BUF_NEXT(m_tmp) = m_cookie;
6007 
6008 	for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
6009 		p_len += SCTP_BUF_LEN(m_tmp);
6010 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
6011 			/* m_tmp should now point to last one */
6012 			mp_last = m_tmp;
6013 			break;
6014 		}
6015 	}
6016 	/*
6017 	 * Place in the size, but we don't include the last pad (if any) in
6018 	 * the INIT-ACK.
6019 	 */
6020 	initack->ch.chunk_length = htons(p_len);
6021 
6022 	/*
6023 	 * Time to sign the cookie, we don't sign over the cookie signature
6024 	 * though thus we set trailer.
6025 	 */
6026 	(void)sctp_hmac_m(SCTP_HMAC,
6027 	    (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
6028 	    SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
6029 	    (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
6030 	/*
6031 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
6032 	 * here since the timer will drive a retranmission.
6033 	 */
6034 	padval = p_len % 4;
6035 	if ((padval) && (mp_last)) {
6036 		/* see my previous comments on mp_last */
6037 		if (sctp_add_pad_tombuf(mp_last, (4 - padval))) {
6038 			/* Houston we have a problem, no space */
6039 			sctp_m_freem(m);
6040 			return;
6041 		}
6042 	}
6043 	if (stc.loopback_scope) {
6044 		over_addr = &store1;
6045 	} else {
6046 		over_addr = NULL;
6047 	}
6048 
6049 	(void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
6050 	    0, 0,
6051 	    inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
6052 	    port, SCTP_SO_NOT_LOCKED, over_addr, init_pkt);
6053 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
6054 }
6055 
6056 
6057 static void
6058 sctp_prune_prsctp(struct sctp_tcb *stcb,
6059     struct sctp_association *asoc,
6060     struct sctp_sndrcvinfo *srcv,
6061     int dataout)
6062 {
6063 	int freed_spc = 0;
6064 	struct sctp_tmit_chunk *chk, *nchk;
6065 
6066 	SCTP_TCB_LOCK_ASSERT(stcb);
6067 	if ((asoc->peer_supports_prsctp) &&
6068 	    (asoc->sent_queue_cnt_removeable > 0)) {
6069 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6070 			/*
6071 			 * Look for chunks marked with the PR_SCTP flag AND
6072 			 * the buffer space flag. If the one being sent is
6073 			 * equal or greater priority then purge the old one
6074 			 * and free some space.
6075 			 */
6076 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6077 				/*
6078 				 * This one is PR-SCTP AND buffer space
6079 				 * limited type
6080 				 */
6081 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
6082 					/*
6083 					 * Lower numbers equates to higher
6084 					 * priority so if the one we are
6085 					 * looking at has a larger or equal
6086 					 * priority we want to drop the data
6087 					 * and NOT retransmit it.
6088 					 */
6089 					if (chk->data) {
6090 						/*
6091 						 * We release the book_size
6092 						 * if the mbuf is here
6093 						 */
6094 						int ret_spc;
6095 						int cause;
6096 
6097 						if (chk->sent > SCTP_DATAGRAM_UNSENT)
6098 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
6099 						else
6100 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
6101 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6102 						    cause,
6103 						    SCTP_SO_LOCKED);
6104 						freed_spc += ret_spc;
6105 						if (freed_spc >= dataout) {
6106 							return;
6107 						}
6108 					}	/* if chunk was present */
6109 				}	/* if of sufficent priority */
6110 			}	/* if chunk has enabled */
6111 		}		/* tailqforeach */
6112 
6113 		TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
6114 			/* Here we must move to the sent queue and mark */
6115 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6116 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
6117 					if (chk->data) {
6118 						/*
6119 						 * We release the book_size
6120 						 * if the mbuf is here
6121 						 */
6122 						int ret_spc;
6123 
6124 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6125 						    SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
6126 						    SCTP_SO_LOCKED);
6127 
6128 						freed_spc += ret_spc;
6129 						if (freed_spc >= dataout) {
6130 							return;
6131 						}
6132 					}	/* end if chk->data */
6133 				}	/* end if right class */
6134 			}	/* end if chk pr-sctp */
6135 		}		/* tailqforeachsafe (chk) */
6136 	}			/* if enabled in asoc */
6137 }
6138 
6139 int
6140 sctp_get_frag_point(struct sctp_tcb *stcb,
6141     struct sctp_association *asoc)
6142 {
6143 	int siz, ovh;
6144 
6145 	/*
6146 	 * For endpoints that have both v6 and v4 addresses we must reserve
6147 	 * room for the ipv6 header, for those that are only dealing with V4
6148 	 * we use a larger frag point.
6149 	 */
6150 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6151 		ovh = SCTP_MED_OVERHEAD;
6152 	} else {
6153 		ovh = SCTP_MED_V4_OVERHEAD;
6154 	}
6155 
6156 	if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
6157 		siz = asoc->smallest_mtu - ovh;
6158 	else
6159 		siz = (stcb->asoc.sctp_frag_point - ovh);
6160 	/*
6161 	 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
6162 	 */
6163 	/* A data chunk MUST fit in a cluster */
6164 	/* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
6165 	/* } */
6166 
6167 	/* adjust for an AUTH chunk if DATA requires auth */
6168 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
6169 		siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
6170 
6171 	if (siz % 4) {
6172 		/* make it an even word boundary please */
6173 		siz -= (siz % 4);
6174 	}
6175 	return (siz);
6176 }
6177 
6178 static void
6179 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
6180 {
6181 	sp->pr_sctp_on = 0;
6182 	/*
6183 	 * We assume that the user wants PR_SCTP_TTL if the user provides a
6184 	 * positive lifetime but does not specify any PR_SCTP policy. This
6185 	 * is a BAD assumption and causes problems at least with the
6186 	 * U-Vancovers MPI folks. I will change this to be no policy means
6187 	 * NO PR-SCTP.
6188 	 */
6189 	if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
6190 		sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
6191 		sp->pr_sctp_on = 1;
6192 	} else {
6193 		return;
6194 	}
6195 	switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
6196 	case CHUNK_FLAGS_PR_SCTP_BUF:
6197 		/*
6198 		 * Time to live is a priority stored in tv_sec when doing
6199 		 * the buffer drop thing.
6200 		 */
6201 		sp->ts.tv_sec = sp->timetolive;
6202 		sp->ts.tv_usec = 0;
6203 		break;
6204 	case CHUNK_FLAGS_PR_SCTP_TTL:
6205 		{
6206 			struct timeval tv;
6207 
6208 			(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6209 			tv.tv_sec = sp->timetolive / 1000;
6210 			tv.tv_usec = (sp->timetolive * 1000) % 1000000;
6211 			/*
6212 			 * TODO sctp_constants.h needs alternative time
6213 			 * macros when _KERNEL is undefined.
6214 			 */
6215 			timevaladd(&sp->ts, &tv);
6216 		}
6217 		break;
6218 	case CHUNK_FLAGS_PR_SCTP_RTX:
6219 		/*
6220 		 * Time to live is a the number or retransmissions stored in
6221 		 * tv_sec.
6222 		 */
6223 		sp->ts.tv_sec = sp->timetolive;
6224 		sp->ts.tv_usec = 0;
6225 		break;
6226 	default:
6227 		SCTPDBG(SCTP_DEBUG_USRREQ1,
6228 		    "Unknown PR_SCTP policy %u.\n",
6229 		    PR_SCTP_POLICY(sp->sinfo_flags));
6230 		break;
6231 	}
6232 }
6233 
6234 static int
6235 sctp_msg_append(struct sctp_tcb *stcb,
6236     struct sctp_nets *net,
6237     struct mbuf *m,
6238     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
6239 {
6240 	int error = 0;
6241 	struct mbuf *at;
6242 	struct sctp_stream_queue_pending *sp = NULL;
6243 	struct sctp_stream_out *strm;
6244 
6245 	/*
6246 	 * Given an mbuf chain, put it into the association send queue and
6247 	 * place it on the wheel
6248 	 */
6249 	if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
6250 		/* Invalid stream number */
6251 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6252 		error = EINVAL;
6253 		goto out_now;
6254 	}
6255 	if ((stcb->asoc.stream_locked) &&
6256 	    (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
6257 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6258 		error = EINVAL;
6259 		goto out_now;
6260 	}
6261 	strm = &stcb->asoc.strmout[srcv->sinfo_stream];
6262 	/* Now can we send this? */
6263 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
6264 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
6265 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
6266 	    (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
6267 		/* got data while shutting down */
6268 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
6269 		error = ECONNRESET;
6270 		goto out_now;
6271 	}
6272 	sctp_alloc_a_strmoq(stcb, sp);
6273 	if (sp == NULL) {
6274 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6275 		error = ENOMEM;
6276 		goto out_now;
6277 	}
6278 	sp->sinfo_flags = srcv->sinfo_flags;
6279 	sp->timetolive = srcv->sinfo_timetolive;
6280 	sp->ppid = srcv->sinfo_ppid;
6281 	sp->context = srcv->sinfo_context;
6282 	sp->strseq = 0;
6283 	if (sp->sinfo_flags & SCTP_ADDR_OVER) {
6284 		sp->net = net;
6285 		atomic_add_int(&sp->net->ref_count, 1);
6286 	} else {
6287 		sp->net = NULL;
6288 	}
6289 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6290 	sp->stream = srcv->sinfo_stream;
6291 	sp->msg_is_complete = 1;
6292 	sp->sender_all_done = 1;
6293 	sp->some_taken = 0;
6294 	sp->data = m;
6295 	sp->tail_mbuf = NULL;
6296 	sctp_set_prsctp_policy(sp);
6297 	/*
6298 	 * We could in theory (for sendall) sifa the length in, but we would
6299 	 * still have to hunt through the chain since we need to setup the
6300 	 * tail_mbuf
6301 	 */
6302 	sp->length = 0;
6303 	for (at = m; at; at = SCTP_BUF_NEXT(at)) {
6304 		if (SCTP_BUF_NEXT(at) == NULL)
6305 			sp->tail_mbuf = at;
6306 		sp->length += SCTP_BUF_LEN(at);
6307 	}
6308 	if (srcv->sinfo_keynumber_valid) {
6309 		sp->auth_keyid = srcv->sinfo_keynumber;
6310 	} else {
6311 		sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
6312 	}
6313 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
6314 		sctp_auth_key_acquire(stcb, sp->auth_keyid);
6315 		sp->holds_key_ref = 1;
6316 	}
6317 	if (hold_stcb_lock == 0) {
6318 		SCTP_TCB_SEND_LOCK(stcb);
6319 	}
6320 	sctp_snd_sb_alloc(stcb, sp->length);
6321 	atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
6322 	TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
6323 	if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
6324 		sp->strseq = strm->next_sequence_sent;
6325 		strm->next_sequence_sent++;
6326 	}
6327 	stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp, 1);
6328 	m = NULL;
6329 	if (hold_stcb_lock == 0) {
6330 		SCTP_TCB_SEND_UNLOCK(stcb);
6331 	}
6332 out_now:
6333 	if (m) {
6334 		sctp_m_freem(m);
6335 	}
6336 	return (error);
6337 }
6338 
6339 
6340 static struct mbuf *
6341 sctp_copy_mbufchain(struct mbuf *clonechain,
6342     struct mbuf *outchain,
6343     struct mbuf **endofchain,
6344     int can_take_mbuf,
6345     int sizeofcpy,
6346     uint8_t copy_by_ref)
6347 {
6348 	struct mbuf *m;
6349 	struct mbuf *appendchain;
6350 	caddr_t cp;
6351 	int len;
6352 
6353 	if (endofchain == NULL) {
6354 		/* error */
6355 error_out:
6356 		if (outchain)
6357 			sctp_m_freem(outchain);
6358 		return (NULL);
6359 	}
6360 	if (can_take_mbuf) {
6361 		appendchain = clonechain;
6362 	} else {
6363 		if (!copy_by_ref &&
6364 		    (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
6365 		    ) {
6366 			/* Its not in a cluster */
6367 			if (*endofchain == NULL) {
6368 				/* lets get a mbuf cluster */
6369 				if (outchain == NULL) {
6370 					/* This is the general case */
6371 			new_mbuf:
6372 					outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6373 					if (outchain == NULL) {
6374 						goto error_out;
6375 					}
6376 					SCTP_BUF_LEN(outchain) = 0;
6377 					*endofchain = outchain;
6378 					/* get the prepend space */
6379 					SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
6380 				} else {
6381 					/*
6382 					 * We really should not get a NULL
6383 					 * in endofchain
6384 					 */
6385 					/* find end */
6386 					m = outchain;
6387 					while (m) {
6388 						if (SCTP_BUF_NEXT(m) == NULL) {
6389 							*endofchain = m;
6390 							break;
6391 						}
6392 						m = SCTP_BUF_NEXT(m);
6393 					}
6394 					/* sanity */
6395 					if (*endofchain == NULL) {
6396 						/*
6397 						 * huh, TSNH XXX maybe we
6398 						 * should panic
6399 						 */
6400 						sctp_m_freem(outchain);
6401 						goto new_mbuf;
6402 					}
6403 				}
6404 				/* get the new end of length */
6405 				len = M_TRAILINGSPACE(*endofchain);
6406 			} else {
6407 				/* how much is left at the end? */
6408 				len = M_TRAILINGSPACE(*endofchain);
6409 			}
6410 			/* Find the end of the data, for appending */
6411 			cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
6412 
6413 			/* Now lets copy it out */
6414 			if (len >= sizeofcpy) {
6415 				/* It all fits, copy it in */
6416 				m_copydata(clonechain, 0, sizeofcpy, cp);
6417 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6418 			} else {
6419 				/* fill up the end of the chain */
6420 				if (len > 0) {
6421 					m_copydata(clonechain, 0, len, cp);
6422 					SCTP_BUF_LEN((*endofchain)) += len;
6423 					/* now we need another one */
6424 					sizeofcpy -= len;
6425 				}
6426 				m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6427 				if (m == NULL) {
6428 					/* We failed */
6429 					goto error_out;
6430 				}
6431 				SCTP_BUF_NEXT((*endofchain)) = m;
6432 				*endofchain = m;
6433 				cp = mtod((*endofchain), caddr_t);
6434 				m_copydata(clonechain, len, sizeofcpy, cp);
6435 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6436 			}
6437 			return (outchain);
6438 		} else {
6439 			/* copy the old fashion way */
6440 			appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
6441 #ifdef SCTP_MBUF_LOGGING
6442 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6443 				struct mbuf *mat;
6444 
6445 				for (mat = appendchain; mat; mat = SCTP_BUF_NEXT(mat)) {
6446 					if (SCTP_BUF_IS_EXTENDED(mat)) {
6447 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6448 					}
6449 				}
6450 			}
6451 #endif
6452 		}
6453 	}
6454 	if (appendchain == NULL) {
6455 		/* error */
6456 		if (outchain)
6457 			sctp_m_freem(outchain);
6458 		return (NULL);
6459 	}
6460 	if (outchain) {
6461 		/* tack on to the end */
6462 		if (*endofchain != NULL) {
6463 			SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6464 		} else {
6465 			m = outchain;
6466 			while (m) {
6467 				if (SCTP_BUF_NEXT(m) == NULL) {
6468 					SCTP_BUF_NEXT(m) = appendchain;
6469 					break;
6470 				}
6471 				m = SCTP_BUF_NEXT(m);
6472 			}
6473 		}
6474 		/*
6475 		 * save off the end and update the end-chain postion
6476 		 */
6477 		m = appendchain;
6478 		while (m) {
6479 			if (SCTP_BUF_NEXT(m) == NULL) {
6480 				*endofchain = m;
6481 				break;
6482 			}
6483 			m = SCTP_BUF_NEXT(m);
6484 		}
6485 		return (outchain);
6486 	} else {
6487 		/* save off the end and update the end-chain postion */
6488 		m = appendchain;
6489 		while (m) {
6490 			if (SCTP_BUF_NEXT(m) == NULL) {
6491 				*endofchain = m;
6492 				break;
6493 			}
6494 			m = SCTP_BUF_NEXT(m);
6495 		}
6496 		return (appendchain);
6497 	}
6498 }
6499 
6500 int
6501 sctp_med_chunk_output(struct sctp_inpcb *inp,
6502     struct sctp_tcb *stcb,
6503     struct sctp_association *asoc,
6504     int *num_out,
6505     int *reason_code,
6506     int control_only, int from_where,
6507     struct timeval *now, int *now_filled, int frag_point, int so_locked
6508 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6509     SCTP_UNUSED
6510 #endif
6511 );
6512 
6513 static void
6514 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6515     uint32_t val SCTP_UNUSED)
6516 {
6517 	struct sctp_copy_all *ca;
6518 	struct mbuf *m;
6519 	int ret = 0;
6520 	int added_control = 0;
6521 	int un_sent, do_chunk_output = 1;
6522 	struct sctp_association *asoc;
6523 	struct sctp_nets *net;
6524 
6525 	ca = (struct sctp_copy_all *)ptr;
6526 	if (ca->m == NULL) {
6527 		return;
6528 	}
6529 	if (ca->inp != inp) {
6530 		/* TSNH */
6531 		return;
6532 	}
6533 	if ((ca->m) && ca->sndlen) {
6534 		m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6535 		if (m == NULL) {
6536 			/* can't copy so we are done */
6537 			ca->cnt_failed++;
6538 			return;
6539 		}
6540 #ifdef SCTP_MBUF_LOGGING
6541 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6542 			struct mbuf *mat;
6543 
6544 			for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
6545 				if (SCTP_BUF_IS_EXTENDED(mat)) {
6546 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6547 				}
6548 			}
6549 		}
6550 #endif
6551 	} else {
6552 		m = NULL;
6553 	}
6554 	SCTP_TCB_LOCK_ASSERT(stcb);
6555 	if (stcb->asoc.alternate) {
6556 		net = stcb->asoc.alternate;
6557 	} else {
6558 		net = stcb->asoc.primary_destination;
6559 	}
6560 	if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6561 		/* Abort this assoc with m as the user defined reason */
6562 		if (m) {
6563 			struct sctp_paramhdr *ph;
6564 
6565 			SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6566 			if (m) {
6567 				ph = mtod(m, struct sctp_paramhdr *);
6568 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6569 				ph->param_length = htons(ca->sndlen);
6570 			}
6571 			/*
6572 			 * We add one here to keep the assoc from
6573 			 * dis-appearing on us.
6574 			 */
6575 			atomic_add_int(&stcb->asoc.refcnt, 1);
6576 			sctp_abort_an_association(inp, stcb,
6577 			    SCTP_RESPONSE_TO_USER_REQ,
6578 			    m, SCTP_SO_NOT_LOCKED);
6579 			/*
6580 			 * sctp_abort_an_association calls sctp_free_asoc()
6581 			 * free association will NOT free it since we
6582 			 * incremented the refcnt .. we do this to prevent
6583 			 * it being freed and things getting tricky since we
6584 			 * could end up (from free_asoc) calling inpcb_free
6585 			 * which would get a recursive lock call to the
6586 			 * iterator lock.. But as a consequence of that the
6587 			 * stcb will return to us un-locked.. since
6588 			 * free_asoc returns with either no TCB or the TCB
6589 			 * unlocked, we must relock.. to unlock in the
6590 			 * iterator timer :-0
6591 			 */
6592 			SCTP_TCB_LOCK(stcb);
6593 			atomic_add_int(&stcb->asoc.refcnt, -1);
6594 			goto no_chunk_output;
6595 		}
6596 	} else {
6597 		if (m) {
6598 			ret = sctp_msg_append(stcb, net, m,
6599 			    &ca->sndrcv, 1);
6600 		}
6601 		asoc = &stcb->asoc;
6602 		if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6603 			/* shutdown this assoc */
6604 			int cnt;
6605 
6606 			cnt = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
6607 
6608 			if (TAILQ_EMPTY(&asoc->send_queue) &&
6609 			    TAILQ_EMPTY(&asoc->sent_queue) &&
6610 			    (cnt == 0)) {
6611 				if (asoc->locked_on_sending) {
6612 					goto abort_anyway;
6613 				}
6614 				/*
6615 				 * there is nothing queued to send, so I'm
6616 				 * done...
6617 				 */
6618 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6619 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6620 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6621 					/*
6622 					 * only send SHUTDOWN the first time
6623 					 * through
6624 					 */
6625 					sctp_send_shutdown(stcb, net);
6626 					if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6627 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6628 					}
6629 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6630 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6631 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6632 					    net);
6633 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6634 					    asoc->primary_destination);
6635 					added_control = 1;
6636 					do_chunk_output = 0;
6637 				}
6638 			} else {
6639 				/*
6640 				 * we still got (or just got) data to send,
6641 				 * so set SHUTDOWN_PENDING
6642 				 */
6643 				/*
6644 				 * XXX sockets draft says that SCTP_EOF
6645 				 * should be sent with no data.  currently,
6646 				 * we will allow user data to be sent first
6647 				 * and move to SHUTDOWN-PENDING
6648 				 */
6649 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6650 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6651 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6652 					if (asoc->locked_on_sending) {
6653 						/*
6654 						 * Locked to send out the
6655 						 * data
6656 						 */
6657 						struct sctp_stream_queue_pending *sp;
6658 
6659 						sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6660 						if (sp) {
6661 							if ((sp->length == 0) && (sp->msg_is_complete == 0))
6662 								asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6663 						}
6664 					}
6665 					asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6666 					if (TAILQ_EMPTY(&asoc->send_queue) &&
6667 					    TAILQ_EMPTY(&asoc->sent_queue) &&
6668 					    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6669 				abort_anyway:
6670 						atomic_add_int(&stcb->asoc.refcnt, 1);
6671 						sctp_abort_an_association(stcb->sctp_ep, stcb,
6672 						    SCTP_RESPONSE_TO_USER_REQ,
6673 						    NULL, SCTP_SO_NOT_LOCKED);
6674 						atomic_add_int(&stcb->asoc.refcnt, -1);
6675 						goto no_chunk_output;
6676 					}
6677 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6678 					    asoc->primary_destination);
6679 				}
6680 			}
6681 
6682 		}
6683 	}
6684 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6685 	    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6686 
6687 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6688 	    (stcb->asoc.total_flight > 0) &&
6689 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6690 	    ) {
6691 		do_chunk_output = 0;
6692 	}
6693 	if (do_chunk_output)
6694 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6695 	else if (added_control) {
6696 		int num_out = 0, reason = 0, now_filled = 0;
6697 		struct timeval now;
6698 		int frag_point;
6699 
6700 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6701 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6702 		    &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6703 	}
6704 no_chunk_output:
6705 	if (ret) {
6706 		ca->cnt_failed++;
6707 	} else {
6708 		ca->cnt_sent++;
6709 	}
6710 }
6711 
6712 static void
6713 sctp_sendall_completes(void *ptr, uint32_t val SCTP_UNUSED)
6714 {
6715 	struct sctp_copy_all *ca;
6716 
6717 	ca = (struct sctp_copy_all *)ptr;
6718 	/*
6719 	 * Do a notify here? Kacheong suggests that the notify be done at
6720 	 * the send time.. so you would push up a notification if any send
6721 	 * failed. Don't know if this is feasable since the only failures we
6722 	 * have is "memory" related and if you cannot get an mbuf to send
6723 	 * the data you surely can't get an mbuf to send up to notify the
6724 	 * user you can't send the data :->
6725 	 */
6726 
6727 	/* now free everything */
6728 	sctp_m_freem(ca->m);
6729 	SCTP_FREE(ca, SCTP_M_COPYAL);
6730 }
6731 
6732 
6733 #define	MC_ALIGN(m, len) do {						\
6734 	SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1));	\
6735 } while (0)
6736 
6737 
6738 
6739 static struct mbuf *
6740 sctp_copy_out_all(struct uio *uio, int len)
6741 {
6742 	struct mbuf *ret, *at;
6743 	int left, willcpy, cancpy, error;
6744 
6745 	ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6746 	if (ret == NULL) {
6747 		/* TSNH */
6748 		return (NULL);
6749 	}
6750 	left = len;
6751 	SCTP_BUF_LEN(ret) = 0;
6752 	/* save space for the data chunk header */
6753 	cancpy = M_TRAILINGSPACE(ret);
6754 	willcpy = min(cancpy, left);
6755 	at = ret;
6756 	while (left > 0) {
6757 		/* Align data to the end */
6758 		error = uiomove(mtod(at, caddr_t), willcpy, uio);
6759 		if (error) {
6760 	err_out_now:
6761 			sctp_m_freem(at);
6762 			return (NULL);
6763 		}
6764 		SCTP_BUF_LEN(at) = willcpy;
6765 		SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6766 		left -= willcpy;
6767 		if (left > 0) {
6768 			SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6769 			if (SCTP_BUF_NEXT(at) == NULL) {
6770 				goto err_out_now;
6771 			}
6772 			at = SCTP_BUF_NEXT(at);
6773 			SCTP_BUF_LEN(at) = 0;
6774 			cancpy = M_TRAILINGSPACE(at);
6775 			willcpy = min(cancpy, left);
6776 		}
6777 	}
6778 	return (ret);
6779 }
6780 
6781 static int
6782 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6783     struct sctp_sndrcvinfo *srcv)
6784 {
6785 	int ret;
6786 	struct sctp_copy_all *ca;
6787 
6788 	SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6789 	    SCTP_M_COPYAL);
6790 	if (ca == NULL) {
6791 		sctp_m_freem(m);
6792 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6793 		return (ENOMEM);
6794 	}
6795 	memset(ca, 0, sizeof(struct sctp_copy_all));
6796 
6797 	ca->inp = inp;
6798 	if (srcv) {
6799 		memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6800 	}
6801 	/*
6802 	 * take off the sendall flag, it would be bad if we failed to do
6803 	 * this :-0
6804 	 */
6805 	ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6806 	/* get length and mbuf chain */
6807 	if (uio) {
6808 		ca->sndlen = uio->uio_resid;
6809 		ca->m = sctp_copy_out_all(uio, ca->sndlen);
6810 		if (ca->m == NULL) {
6811 			SCTP_FREE(ca, SCTP_M_COPYAL);
6812 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6813 			return (ENOMEM);
6814 		}
6815 	} else {
6816 		/* Gather the length of the send */
6817 		struct mbuf *mat;
6818 
6819 		mat = m;
6820 		ca->sndlen = 0;
6821 		while (m) {
6822 			ca->sndlen += SCTP_BUF_LEN(m);
6823 			m = SCTP_BUF_NEXT(m);
6824 		}
6825 		ca->m = mat;
6826 	}
6827 	ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6828 	    SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6829 	    SCTP_ASOC_ANY_STATE,
6830 	    (void *)ca, 0,
6831 	    sctp_sendall_completes, inp, 1);
6832 	if (ret) {
6833 		SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6834 		SCTP_FREE(ca, SCTP_M_COPYAL);
6835 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6836 		return (EFAULT);
6837 	}
6838 	return (0);
6839 }
6840 
6841 
6842 void
6843 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6844 {
6845 	struct sctp_tmit_chunk *chk, *nchk;
6846 
6847 	TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
6848 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6849 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6850 			if (chk->data) {
6851 				sctp_m_freem(chk->data);
6852 				chk->data = NULL;
6853 			}
6854 			asoc->ctrl_queue_cnt--;
6855 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6856 		}
6857 	}
6858 }
6859 
6860 void
6861 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6862 {
6863 	struct sctp_association *asoc;
6864 	struct sctp_tmit_chunk *chk, *nchk;
6865 	struct sctp_asconf_chunk *acp;
6866 
6867 	asoc = &stcb->asoc;
6868 	TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
6869 		/* find SCTP_ASCONF chunk in queue */
6870 		if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6871 			if (chk->data) {
6872 				acp = mtod(chk->data, struct sctp_asconf_chunk *);
6873 				if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) {
6874 					/* Not Acked yet */
6875 					break;
6876 				}
6877 			}
6878 			TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6879 			if (chk->data) {
6880 				sctp_m_freem(chk->data);
6881 				chk->data = NULL;
6882 			}
6883 			asoc->ctrl_queue_cnt--;
6884 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6885 		}
6886 	}
6887 }
6888 
6889 
6890 static void
6891 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6892     struct sctp_association *asoc,
6893     struct sctp_tmit_chunk **data_list,
6894     int bundle_at,
6895     struct sctp_nets *net)
6896 {
6897 	int i;
6898 	struct sctp_tmit_chunk *tp1;
6899 
6900 	for (i = 0; i < bundle_at; i++) {
6901 		/* off of the send queue */
6902 		TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next);
6903 		asoc->send_queue_cnt--;
6904 		if (i > 0) {
6905 			/*
6906 			 * Any chunk NOT 0 you zap the time chunk 0 gets
6907 			 * zapped or set based on if a RTO measurment is
6908 			 * needed.
6909 			 */
6910 			data_list[i]->do_rtt = 0;
6911 		}
6912 		/* record time */
6913 		data_list[i]->sent_rcv_time = net->last_sent_time;
6914 		data_list[i]->rec.data.cwnd_at_send = net->cwnd;
6915 		data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6916 		if (data_list[i]->whoTo == NULL) {
6917 			data_list[i]->whoTo = net;
6918 			atomic_add_int(&net->ref_count, 1);
6919 		}
6920 		/* on to the sent queue */
6921 		tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6922 		if ((tp1) && SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) {
6923 			struct sctp_tmit_chunk *tpp;
6924 
6925 			/* need to move back */
6926 	back_up_more:
6927 			tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6928 			if (tpp == NULL) {
6929 				TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6930 				goto all_done;
6931 			}
6932 			tp1 = tpp;
6933 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) {
6934 				goto back_up_more;
6935 			}
6936 			TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6937 		} else {
6938 			TAILQ_INSERT_TAIL(&asoc->sent_queue,
6939 			    data_list[i],
6940 			    sctp_next);
6941 		}
6942 all_done:
6943 		/* This does not lower until the cum-ack passes it */
6944 		asoc->sent_queue_cnt++;
6945 		if ((asoc->peers_rwnd <= 0) &&
6946 		    (asoc->total_flight == 0) &&
6947 		    (bundle_at == 1)) {
6948 			/* Mark the chunk as being a window probe */
6949 			SCTP_STAT_INCR(sctps_windowprobed);
6950 		}
6951 #ifdef SCTP_AUDITING_ENABLED
6952 		sctp_audit_log(0xC2, 3);
6953 #endif
6954 		data_list[i]->sent = SCTP_DATAGRAM_SENT;
6955 		data_list[i]->snd_count = 1;
6956 		data_list[i]->rec.data.chunk_was_revoked = 0;
6957 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6958 			sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6959 			    data_list[i]->whoTo->flight_size,
6960 			    data_list[i]->book_size,
6961 			    (uintptr_t) data_list[i]->whoTo,
6962 			    data_list[i]->rec.data.TSN_seq);
6963 		}
6964 		sctp_flight_size_increase(data_list[i]);
6965 		sctp_total_flight_increase(stcb, data_list[i]);
6966 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6967 			sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6968 			    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6969 		}
6970 		asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6971 		    (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6972 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6973 			/* SWS sender side engages */
6974 			asoc->peers_rwnd = 0;
6975 		}
6976 	}
6977 	if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) {
6978 		(*asoc->cc_functions.sctp_cwnd_update_packet_transmitted) (stcb, net);
6979 	}
6980 }
6981 
6982 static void
6983 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked
6984 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6985     SCTP_UNUSED
6986 #endif
6987 )
6988 {
6989 	struct sctp_tmit_chunk *chk, *nchk;
6990 
6991 	TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
6992 		if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6993 		    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
6994 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6995 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6996 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
6997 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6998 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6999 		    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7000 		    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7001 		    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7002 		    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7003 		    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7004 			/* Stray chunks must be cleaned up */
7005 	clean_up_anyway:
7006 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7007 			if (chk->data) {
7008 				sctp_m_freem(chk->data);
7009 				chk->data = NULL;
7010 			}
7011 			asoc->ctrl_queue_cnt--;
7012 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)
7013 				asoc->fwd_tsn_cnt--;
7014 			sctp_free_a_chunk(stcb, chk, so_locked);
7015 		} else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
7016 			/* special handling, we must look into the param */
7017 			if (chk != asoc->str_reset) {
7018 				goto clean_up_anyway;
7019 			}
7020 		}
7021 	}
7022 }
7023 
7024 
7025 static int
7026 sctp_can_we_split_this(struct sctp_tcb *stcb,
7027     uint32_t length,
7028     uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
7029 {
7030 	/*
7031 	 * Make a decision on if I should split a msg into multiple parts.
7032 	 * This is only asked of incomplete messages.
7033 	 */
7034 	if (eeor_on) {
7035 		/*
7036 		 * If we are doing EEOR we need to always send it if its the
7037 		 * entire thing, since it might be all the guy is putting in
7038 		 * the hopper.
7039 		 */
7040 		if (goal_mtu >= length) {
7041 			/*-
7042 			 * If we have data outstanding,
7043 			 * we get another chance when the sack
7044 			 * arrives to transmit - wait for more data
7045 			 */
7046 			if (stcb->asoc.total_flight == 0) {
7047 				/*
7048 				 * If nothing is in flight, we zero the
7049 				 * packet counter.
7050 				 */
7051 				return (length);
7052 			}
7053 			return (0);
7054 
7055 		} else {
7056 			/* You can fill the rest */
7057 			return (goal_mtu);
7058 		}
7059 	}
7060 	/*-
7061 	 * For those strange folk that make the send buffer
7062 	 * smaller than our fragmentation point, we can't
7063 	 * get a full msg in so we have to allow splitting.
7064 	 */
7065 	if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
7066 		return (length);
7067 	}
7068 	if ((length <= goal_mtu) ||
7069 	    ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
7070 		/* Sub-optimial residual don't split in non-eeor mode. */
7071 		return (0);
7072 	}
7073 	/*
7074 	 * If we reach here length is larger than the goal_mtu. Do we wish
7075 	 * to split it for the sake of packet putting together?
7076 	 */
7077 	if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
7078 		/* Its ok to split it */
7079 		return (min(goal_mtu, frag_point));
7080 	}
7081 	/* Nope, can't split */
7082 	return (0);
7083 
7084 }
7085 
7086 static uint32_t
7087 sctp_move_to_outqueue(struct sctp_tcb *stcb,
7088     struct sctp_stream_out *strq,
7089     uint32_t goal_mtu,
7090     uint32_t frag_point,
7091     int *locked,
7092     int *giveup,
7093     int eeor_mode,
7094     int *bail,
7095     int so_locked
7096 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7097     SCTP_UNUSED
7098 #endif
7099 )
7100 {
7101 	/* Move from the stream to the send_queue keeping track of the total */
7102 	struct sctp_association *asoc;
7103 	struct sctp_stream_queue_pending *sp;
7104 	struct sctp_tmit_chunk *chk;
7105 	struct sctp_data_chunk *dchkh;
7106 	uint32_t to_move, length;
7107 	uint8_t rcv_flags = 0;
7108 	uint8_t some_taken;
7109 	uint8_t send_lock_up = 0;
7110 
7111 	SCTP_TCB_LOCK_ASSERT(stcb);
7112 	asoc = &stcb->asoc;
7113 one_more_time:
7114 	/* sa_ignore FREED_MEMORY */
7115 	sp = TAILQ_FIRST(&strq->outqueue);
7116 	if (sp == NULL) {
7117 		*locked = 0;
7118 		if (send_lock_up == 0) {
7119 			SCTP_TCB_SEND_LOCK(stcb);
7120 			send_lock_up = 1;
7121 		}
7122 		sp = TAILQ_FIRST(&strq->outqueue);
7123 		if (sp) {
7124 			goto one_more_time;
7125 		}
7126 		if (strq->last_msg_incomplete) {
7127 			SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
7128 			    strq->stream_no,
7129 			    strq->last_msg_incomplete);
7130 			strq->last_msg_incomplete = 0;
7131 		}
7132 		to_move = 0;
7133 		if (send_lock_up) {
7134 			SCTP_TCB_SEND_UNLOCK(stcb);
7135 			send_lock_up = 0;
7136 		}
7137 		goto out_of;
7138 	}
7139 	if ((sp->msg_is_complete) && (sp->length == 0)) {
7140 		if (sp->sender_all_done) {
7141 			/*
7142 			 * We are doing differed cleanup. Last time through
7143 			 * when we took all the data the sender_all_done was
7144 			 * not set.
7145 			 */
7146 			if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
7147 				SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
7148 				SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7149 				    sp->sender_all_done,
7150 				    sp->length,
7151 				    sp->msg_is_complete,
7152 				    sp->put_last_out,
7153 				    send_lock_up);
7154 			}
7155 			if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
7156 				SCTP_TCB_SEND_LOCK(stcb);
7157 				send_lock_up = 1;
7158 			}
7159 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7160 			TAILQ_REMOVE(&strq->outqueue, sp, next);
7161 			stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up);
7162 			if (sp->net) {
7163 				sctp_free_remote_addr(sp->net);
7164 				sp->net = NULL;
7165 			}
7166 			if (sp->data) {
7167 				sctp_m_freem(sp->data);
7168 				sp->data = NULL;
7169 			}
7170 			sctp_free_a_strmoq(stcb, sp, so_locked);
7171 			/* we can't be locked to it */
7172 			*locked = 0;
7173 			stcb->asoc.locked_on_sending = NULL;
7174 			if (send_lock_up) {
7175 				SCTP_TCB_SEND_UNLOCK(stcb);
7176 				send_lock_up = 0;
7177 			}
7178 			/* back to get the next msg */
7179 			goto one_more_time;
7180 		} else {
7181 			/*
7182 			 * sender just finished this but still holds a
7183 			 * reference
7184 			 */
7185 			*locked = 1;
7186 			*giveup = 1;
7187 			to_move = 0;
7188 			goto out_of;
7189 		}
7190 	} else {
7191 		/* is there some to get */
7192 		if (sp->length == 0) {
7193 			/* no */
7194 			*locked = 1;
7195 			*giveup = 1;
7196 			to_move = 0;
7197 			goto out_of;
7198 		} else if (sp->discard_rest) {
7199 			if (send_lock_up == 0) {
7200 				SCTP_TCB_SEND_LOCK(stcb);
7201 				send_lock_up = 1;
7202 			}
7203 			/* Whack down the size */
7204 			atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
7205 			if ((stcb->sctp_socket != NULL) && \
7206 			    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7207 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
7208 				atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
7209 			}
7210 			if (sp->data) {
7211 				sctp_m_freem(sp->data);
7212 				sp->data = NULL;
7213 				sp->tail_mbuf = NULL;
7214 			}
7215 			sp->length = 0;
7216 			sp->some_taken = 1;
7217 			*locked = 1;
7218 			*giveup = 1;
7219 			to_move = 0;
7220 			goto out_of;
7221 		}
7222 	}
7223 	some_taken = sp->some_taken;
7224 	if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
7225 		sp->msg_is_complete = 1;
7226 	}
7227 re_look:
7228 	length = sp->length;
7229 	if (sp->msg_is_complete) {
7230 		/* The message is complete */
7231 		to_move = min(length, frag_point);
7232 		if (to_move == length) {
7233 			/* All of it fits in the MTU */
7234 			if (sp->some_taken) {
7235 				rcv_flags |= SCTP_DATA_LAST_FRAG;
7236 				sp->put_last_out = 1;
7237 			} else {
7238 				rcv_flags |= SCTP_DATA_NOT_FRAG;
7239 				sp->put_last_out = 1;
7240 			}
7241 		} else {
7242 			/* Not all of it fits, we fragment */
7243 			if (sp->some_taken == 0) {
7244 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
7245 			}
7246 			sp->some_taken = 1;
7247 		}
7248 	} else {
7249 		to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
7250 		if (to_move) {
7251 			/*-
7252 			 * We use a snapshot of length in case it
7253 			 * is expanding during the compare.
7254 			 */
7255 			uint32_t llen;
7256 
7257 			llen = length;
7258 			if (to_move >= llen) {
7259 				to_move = llen;
7260 				if (send_lock_up == 0) {
7261 					/*-
7262 					 * We are taking all of an incomplete msg
7263 					 * thus we need a send lock.
7264 					 */
7265 					SCTP_TCB_SEND_LOCK(stcb);
7266 					send_lock_up = 1;
7267 					if (sp->msg_is_complete) {
7268 						/*
7269 						 * the sender finished the
7270 						 * msg
7271 						 */
7272 						goto re_look;
7273 					}
7274 				}
7275 			}
7276 			if (sp->some_taken == 0) {
7277 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
7278 				sp->some_taken = 1;
7279 			}
7280 		} else {
7281 			/* Nothing to take. */
7282 			if (sp->some_taken) {
7283 				*locked = 1;
7284 			}
7285 			*giveup = 1;
7286 			to_move = 0;
7287 			goto out_of;
7288 		}
7289 	}
7290 
7291 	/* If we reach here, we can copy out a chunk */
7292 	sctp_alloc_a_chunk(stcb, chk);
7293 	if (chk == NULL) {
7294 		/* No chunk memory */
7295 		*giveup = 1;
7296 		to_move = 0;
7297 		goto out_of;
7298 	}
7299 	/*
7300 	 * Setup for unordered if needed by looking at the user sent info
7301 	 * flags.
7302 	 */
7303 	if (sp->sinfo_flags & SCTP_UNORDERED) {
7304 		rcv_flags |= SCTP_DATA_UNORDERED;
7305 	}
7306 	if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
7307 	    ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
7308 		rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
7309 	}
7310 	/* clear out the chunk before setting up */
7311 	memset(chk, 0, sizeof(*chk));
7312 	chk->rec.data.rcv_flags = rcv_flags;
7313 
7314 	if (to_move >= length) {
7315 		/* we think we can steal the whole thing */
7316 		if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
7317 			SCTP_TCB_SEND_LOCK(stcb);
7318 			send_lock_up = 1;
7319 		}
7320 		if (to_move < sp->length) {
7321 			/* bail, it changed */
7322 			goto dont_do_it;
7323 		}
7324 		chk->data = sp->data;
7325 		chk->last_mbuf = sp->tail_mbuf;
7326 		/* register the stealing */
7327 		sp->data = sp->tail_mbuf = NULL;
7328 	} else {
7329 		struct mbuf *m;
7330 
7331 dont_do_it:
7332 		chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
7333 		chk->last_mbuf = NULL;
7334 		if (chk->data == NULL) {
7335 			sp->some_taken = some_taken;
7336 			sctp_free_a_chunk(stcb, chk, so_locked);
7337 			*bail = 1;
7338 			to_move = 0;
7339 			goto out_of;
7340 		}
7341 #ifdef SCTP_MBUF_LOGGING
7342 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7343 			struct mbuf *mat;
7344 
7345 			for (mat = chk->data; mat; mat = SCTP_BUF_NEXT(mat)) {
7346 				if (SCTP_BUF_IS_EXTENDED(mat)) {
7347 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
7348 				}
7349 			}
7350 		}
7351 #endif
7352 		/* Pull off the data */
7353 		m_adj(sp->data, to_move);
7354 		/* Now lets work our way down and compact it */
7355 		m = sp->data;
7356 		while (m && (SCTP_BUF_LEN(m) == 0)) {
7357 			sp->data = SCTP_BUF_NEXT(m);
7358 			SCTP_BUF_NEXT(m) = NULL;
7359 			if (sp->tail_mbuf == m) {
7360 				/*-
7361 				 * Freeing tail? TSNH since
7362 				 * we supposedly were taking less
7363 				 * than the sp->length.
7364 				 */
7365 #ifdef INVARIANTS
7366 				panic("Huh, freing tail? - TSNH");
7367 #else
7368 				SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
7369 				sp->tail_mbuf = sp->data = NULL;
7370 				sp->length = 0;
7371 #endif
7372 
7373 			}
7374 			sctp_m_free(m);
7375 			m = sp->data;
7376 		}
7377 	}
7378 	if (SCTP_BUF_IS_EXTENDED(chk->data)) {
7379 		chk->copy_by_ref = 1;
7380 	} else {
7381 		chk->copy_by_ref = 0;
7382 	}
7383 	/*
7384 	 * get last_mbuf and counts of mb useage This is ugly but hopefully
7385 	 * its only one mbuf.
7386 	 */
7387 	if (chk->last_mbuf == NULL) {
7388 		chk->last_mbuf = chk->data;
7389 		while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
7390 			chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
7391 		}
7392 	}
7393 	if (to_move > length) {
7394 		/*- This should not happen either
7395 		 * since we always lower to_move to the size
7396 		 * of sp->length if its larger.
7397 		 */
7398 #ifdef INVARIANTS
7399 		panic("Huh, how can to_move be larger?");
7400 #else
7401 		SCTP_PRINTF("Huh, how can to_move be larger?\n");
7402 		sp->length = 0;
7403 #endif
7404 	} else {
7405 		atomic_subtract_int(&sp->length, to_move);
7406 	}
7407 	if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
7408 		/* Not enough room for a chunk header, get some */
7409 		struct mbuf *m;
7410 
7411 		m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
7412 		if (m == NULL) {
7413 			/*
7414 			 * we're in trouble here. _PREPEND below will free
7415 			 * all the data if there is no leading space, so we
7416 			 * must put the data back and restore.
7417 			 */
7418 			if (send_lock_up == 0) {
7419 				SCTP_TCB_SEND_LOCK(stcb);
7420 				send_lock_up = 1;
7421 			}
7422 			if (chk->data == NULL) {
7423 				/* unsteal the data */
7424 				sp->data = chk->data;
7425 				sp->tail_mbuf = chk->last_mbuf;
7426 			} else {
7427 				struct mbuf *m_tmp;
7428 
7429 				/* reassemble the data */
7430 				m_tmp = sp->data;
7431 				sp->data = chk->data;
7432 				SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7433 			}
7434 			sp->some_taken = some_taken;
7435 			atomic_add_int(&sp->length, to_move);
7436 			chk->data = NULL;
7437 			*bail = 1;
7438 			sctp_free_a_chunk(stcb, chk, so_locked);
7439 			to_move = 0;
7440 			goto out_of;
7441 		} else {
7442 			SCTP_BUF_LEN(m) = 0;
7443 			SCTP_BUF_NEXT(m) = chk->data;
7444 			chk->data = m;
7445 			M_ALIGN(chk->data, 4);
7446 		}
7447 	}
7448 	SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
7449 	if (chk->data == NULL) {
7450 		/* HELP, TSNH since we assured it would not above? */
7451 #ifdef INVARIANTS
7452 		panic("prepend failes HELP?");
7453 #else
7454 		SCTP_PRINTF("prepend fails HELP?\n");
7455 		sctp_free_a_chunk(stcb, chk, so_locked);
7456 #endif
7457 		*bail = 1;
7458 		to_move = 0;
7459 		goto out_of;
7460 	}
7461 	sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
7462 	chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
7463 	chk->book_size_scale = 0;
7464 	chk->sent = SCTP_DATAGRAM_UNSENT;
7465 
7466 	chk->flags = 0;
7467 	chk->asoc = &stcb->asoc;
7468 	chk->pad_inplace = 0;
7469 	chk->no_fr_allowed = 0;
7470 	chk->rec.data.stream_seq = sp->strseq;
7471 	chk->rec.data.stream_number = sp->stream;
7472 	chk->rec.data.payloadtype = sp->ppid;
7473 	chk->rec.data.context = sp->context;
7474 	chk->rec.data.doing_fast_retransmit = 0;
7475 
7476 	chk->rec.data.timetodrop = sp->ts;
7477 	chk->flags = sp->act_flags;
7478 
7479 	if (sp->net) {
7480 		chk->whoTo = sp->net;
7481 		atomic_add_int(&chk->whoTo->ref_count, 1);
7482 	} else
7483 		chk->whoTo = NULL;
7484 
7485 	if (sp->holds_key_ref) {
7486 		chk->auth_keyid = sp->auth_keyid;
7487 		sctp_auth_key_acquire(stcb, chk->auth_keyid);
7488 		chk->holds_key_ref = 1;
7489 	}
7490 	chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
7491 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7492 		sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7493 		    (uintptr_t) stcb, sp->length,
7494 		    (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
7495 		    chk->rec.data.TSN_seq);
7496 	}
7497 	dchkh = mtod(chk->data, struct sctp_data_chunk *);
7498 	/*
7499 	 * Put the rest of the things in place now. Size was done earlier in
7500 	 * previous loop prior to padding.
7501 	 */
7502 
7503 #ifdef SCTP_ASOCLOG_OF_TSNS
7504 	SCTP_TCB_LOCK_ASSERT(stcb);
7505 	if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7506 		asoc->tsn_out_at = 0;
7507 		asoc->tsn_out_wrapped = 1;
7508 	}
7509 	asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7510 	asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7511 	asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7512 	asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7513 	asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7514 	asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7515 	asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7516 	asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7517 	asoc->tsn_out_at++;
7518 #endif
7519 
7520 	dchkh->ch.chunk_type = SCTP_DATA;
7521 	dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7522 	dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7523 	dchkh->dp.stream_id = htons(strq->stream_no);
7524 	dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7525 	dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7526 	dchkh->ch.chunk_length = htons(chk->send_size);
7527 	/* Now advance the chk->send_size by the actual pad needed. */
7528 	if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7529 		/* need a pad */
7530 		struct mbuf *lm;
7531 		int pads;
7532 
7533 		pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7534 		if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7535 			chk->pad_inplace = 1;
7536 		}
7537 		if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7538 			/* pad added an mbuf */
7539 			chk->last_mbuf = lm;
7540 		}
7541 		chk->send_size += pads;
7542 	}
7543 	/* We only re-set the policy if it is on */
7544 	if (sp->pr_sctp_on) {
7545 		sctp_set_prsctp_policy(sp);
7546 		asoc->pr_sctp_cnt++;
7547 		chk->pr_sctp_on = 1;
7548 	} else {
7549 		chk->pr_sctp_on = 0;
7550 	}
7551 	if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7552 		/* All done pull and kill the message */
7553 		atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7554 		if (sp->put_last_out == 0) {
7555 			SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7556 			SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7557 			    sp->sender_all_done,
7558 			    sp->length,
7559 			    sp->msg_is_complete,
7560 			    sp->put_last_out,
7561 			    send_lock_up);
7562 		}
7563 		if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7564 			SCTP_TCB_SEND_LOCK(stcb);
7565 			send_lock_up = 1;
7566 		}
7567 		TAILQ_REMOVE(&strq->outqueue, sp, next);
7568 		stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up);
7569 		if (sp->net) {
7570 			sctp_free_remote_addr(sp->net);
7571 			sp->net = NULL;
7572 		}
7573 		if (sp->data) {
7574 			sctp_m_freem(sp->data);
7575 			sp->data = NULL;
7576 		}
7577 		sctp_free_a_strmoq(stcb, sp, so_locked);
7578 
7579 		/* we can't be locked to it */
7580 		*locked = 0;
7581 		stcb->asoc.locked_on_sending = NULL;
7582 	} else {
7583 		/* more to go, we are locked */
7584 		*locked = 1;
7585 	}
7586 	asoc->chunks_on_out_queue++;
7587 	TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7588 	asoc->send_queue_cnt++;
7589 out_of:
7590 	if (send_lock_up) {
7591 		SCTP_TCB_SEND_UNLOCK(stcb);
7592 	}
7593 	return (to_move);
7594 }
7595 
7596 
7597 static void
7598 sctp_fill_outqueue(struct sctp_tcb *stcb,
7599     struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now, int so_locked
7600 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7601     SCTP_UNUSED
7602 #endif
7603 )
7604 {
7605 	struct sctp_association *asoc;
7606 	struct sctp_stream_out *strq;
7607 	int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7608 	int locked, giveup;
7609 
7610 	SCTP_TCB_LOCK_ASSERT(stcb);
7611 	asoc = &stcb->asoc;
7612 	switch (net->ro._l_addr.sa.sa_family) {
7613 #ifdef INET
7614 	case AF_INET:
7615 		goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7616 		break;
7617 #endif
7618 #ifdef INET6
7619 	case AF_INET6:
7620 		goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7621 		break;
7622 #endif
7623 	default:
7624 		/* TSNH */
7625 		goal_mtu = net->mtu;
7626 		break;
7627 	}
7628 	/* Need an allowance for the data chunk header too */
7629 	goal_mtu -= sizeof(struct sctp_data_chunk);
7630 
7631 	/* must make even word boundary */
7632 	goal_mtu &= 0xfffffffc;
7633 	if (asoc->locked_on_sending) {
7634 		/* We are stuck on one stream until the message completes. */
7635 		strq = asoc->locked_on_sending;
7636 		locked = 1;
7637 	} else {
7638 		strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7639 		locked = 0;
7640 	}
7641 	while ((goal_mtu > 0) && strq) {
7642 		giveup = 0;
7643 		bail = 0;
7644 		moved_how_much = sctp_move_to_outqueue(stcb, strq, goal_mtu, frag_point, &locked,
7645 		    &giveup, eeor_mode, &bail, so_locked);
7646 		if (moved_how_much)
7647 			stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, moved_how_much);
7648 
7649 		if (locked) {
7650 			asoc->locked_on_sending = strq;
7651 			if ((moved_how_much == 0) || (giveup) || bail)
7652 				/* no more to move for now */
7653 				break;
7654 		} else {
7655 			asoc->locked_on_sending = NULL;
7656 			if ((giveup) || bail) {
7657 				break;
7658 			}
7659 			strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7660 			if (strq == NULL) {
7661 				break;
7662 			}
7663 		}
7664 		total_moved += moved_how_much;
7665 		goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7666 		goal_mtu &= 0xfffffffc;
7667 	}
7668 	if (bail)
7669 		*quit_now = 1;
7670 
7671 	stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc);
7672 
7673 	if (total_moved == 0) {
7674 		if ((stcb->asoc.sctp_cmt_on_off == 0) &&
7675 		    (net == stcb->asoc.primary_destination)) {
7676 			/* ran dry for primary network net */
7677 			SCTP_STAT_INCR(sctps_primary_randry);
7678 		} else if (stcb->asoc.sctp_cmt_on_off > 0) {
7679 			/* ran dry with CMT on */
7680 			SCTP_STAT_INCR(sctps_cmt_randry);
7681 		}
7682 	}
7683 }
7684 
7685 void
7686 sctp_fix_ecn_echo(struct sctp_association *asoc)
7687 {
7688 	struct sctp_tmit_chunk *chk;
7689 
7690 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7691 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7692 			chk->sent = SCTP_DATAGRAM_UNSENT;
7693 		}
7694 	}
7695 }
7696 
7697 void
7698 sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net)
7699 {
7700 	struct sctp_association *asoc;
7701 	struct sctp_tmit_chunk *chk;
7702 	struct sctp_stream_queue_pending *sp;
7703 	unsigned int i;
7704 
7705 	if (net == NULL) {
7706 		return;
7707 	}
7708 	asoc = &stcb->asoc;
7709 	for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
7710 		TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
7711 			if (sp->net == net) {
7712 				sctp_free_remote_addr(sp->net);
7713 				sp->net = NULL;
7714 			}
7715 		}
7716 	}
7717 	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7718 		if (chk->whoTo == net) {
7719 			sctp_free_remote_addr(chk->whoTo);
7720 			chk->whoTo = NULL;
7721 		}
7722 	}
7723 }
7724 
7725 int
7726 sctp_med_chunk_output(struct sctp_inpcb *inp,
7727     struct sctp_tcb *stcb,
7728     struct sctp_association *asoc,
7729     int *num_out,
7730     int *reason_code,
7731     int control_only, int from_where,
7732     struct timeval *now, int *now_filled, int frag_point, int so_locked
7733 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7734     SCTP_UNUSED
7735 #endif
7736 )
7737 {
7738 	/*
7739 	 * Ok this is the generic chunk service queue. we must do the
7740 	 * following: - Service the stream queue that is next, moving any
7741 	 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7742 	 * LAST to the out queue in one pass) and assigning TSN's - Check to
7743 	 * see if the cwnd/rwnd allows any output, if so we go ahead and
7744 	 * fomulate and send the low level chunks. Making sure to combine
7745 	 * any control in the control chunk queue also.
7746 	 */
7747 	struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL;
7748 	struct mbuf *outchain, *endoutchain;
7749 	struct sctp_tmit_chunk *chk, *nchk;
7750 
7751 	/* temp arrays for unlinking */
7752 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7753 	int no_fragmentflg, error;
7754 	unsigned int max_rwnd_per_dest, max_send_per_dest;
7755 	int one_chunk, hbflag, skip_data_for_this_net;
7756 	int asconf, cookie, no_out_cnt;
7757 	int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7758 	unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7759 	int tsns_sent = 0;
7760 	uint32_t auth_offset = 0;
7761 	struct sctp_auth_chunk *auth = NULL;
7762 	uint16_t auth_keyid;
7763 	int override_ok = 1;
7764 	int skip_fill_up = 0;
7765 	int data_auth_reqd = 0;
7766 
7767 	/*
7768 	 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7769 	 * destination.
7770 	 */
7771 	int quit_now = 0;
7772 
7773 	*num_out = 0;
7774 	auth_keyid = stcb->asoc.authinfo.active_keyid;
7775 
7776 	if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7777 	    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7778 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7779 		eeor_mode = 1;
7780 	} else {
7781 		eeor_mode = 0;
7782 	}
7783 	ctl_cnt = no_out_cnt = asconf = cookie = 0;
7784 	/*
7785 	 * First lets prime the pump. For each destination, if there is room
7786 	 * in the flight size, attempt to pull an MTU's worth out of the
7787 	 * stream queues into the general send_queue
7788 	 */
7789 #ifdef SCTP_AUDITING_ENABLED
7790 	sctp_audit_log(0xC2, 2);
7791 #endif
7792 	SCTP_TCB_LOCK_ASSERT(stcb);
7793 	hbflag = 0;
7794 	if ((control_only) || (asoc->stream_reset_outstanding))
7795 		no_data_chunks = 1;
7796 	else
7797 		no_data_chunks = 0;
7798 
7799 	/* Nothing to possible to send? */
7800 	if ((TAILQ_EMPTY(&asoc->control_send_queue) ||
7801 	    (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) &&
7802 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7803 	    TAILQ_EMPTY(&asoc->send_queue) &&
7804 	    stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
7805 nothing_to_send:
7806 		*reason_code = 9;
7807 		return (0);
7808 	}
7809 	if (asoc->peers_rwnd == 0) {
7810 		/* No room in peers rwnd */
7811 		*reason_code = 1;
7812 		if (asoc->total_flight > 0) {
7813 			/* we are allowed one chunk in flight */
7814 			no_data_chunks = 1;
7815 		}
7816 	}
7817 	if (stcb->asoc.ecn_echo_cnt_onq) {
7818 		/* Record where a sack goes, if any */
7819 		if (no_data_chunks &&
7820 		    (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) {
7821 			/* Nothing but ECNe to send - we don't do that */
7822 			goto nothing_to_send;
7823 		}
7824 		TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7825 			if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7826 			    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7827 				sack_goes_to = chk->whoTo;
7828 				break;
7829 			}
7830 		}
7831 	}
7832 	max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7833 	if (stcb->sctp_socket)
7834 		max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets;
7835 	else
7836 		max_send_per_dest = 0;
7837 	if (no_data_chunks == 0) {
7838 		/* How many non-directed chunks are there? */
7839 		TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7840 			if (chk->whoTo == NULL) {
7841 				/*
7842 				 * We already have non-directed chunks on
7843 				 * the queue, no need to do a fill-up.
7844 				 */
7845 				skip_fill_up = 1;
7846 				break;
7847 			}
7848 		}
7849 
7850 	}
7851 	if ((no_data_chunks == 0) &&
7852 	    (skip_fill_up == 0) &&
7853 	    (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) {
7854 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7855 			/*
7856 			 * This for loop we are in takes in each net, if
7857 			 * its's got space in cwnd and has data sent to it
7858 			 * (when CMT is off) then it calls
7859 			 * sctp_fill_outqueue for the net. This gets data on
7860 			 * the send queue for that network.
7861 			 *
7862 			 * In sctp_fill_outqueue TSN's are assigned and data is
7863 			 * copied out of the stream buffers. Note mostly
7864 			 * copy by reference (we hope).
7865 			 */
7866 			net->window_probe = 0;
7867 			if ((net != stcb->asoc.alternate) &&
7868 			    ((net->dest_state & SCTP_ADDR_PF) ||
7869 			    (!(net->dest_state & SCTP_ADDR_REACHABLE)) ||
7870 			    (net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
7871 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7872 					sctp_log_cwnd(stcb, net, 1,
7873 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7874 				}
7875 				continue;
7876 			}
7877 			if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
7878 			    (net->flight_size == 0)) {
7879 				(*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
7880 			}
7881 			if (net->flight_size >= net->cwnd) {
7882 				/* skip this network, no room - can't fill */
7883 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7884 					sctp_log_cwnd(stcb, net, 3,
7885 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7886 				}
7887 				continue;
7888 			}
7889 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7890 				sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7891 			}
7892 			sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked);
7893 			if (quit_now) {
7894 				/* memory alloc failure */
7895 				no_data_chunks = 1;
7896 				break;
7897 			}
7898 		}
7899 	}
7900 	/* now service each destination and send out what we can for it */
7901 	/* Nothing to send? */
7902 	if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7903 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7904 	    TAILQ_EMPTY(&asoc->send_queue)) {
7905 		*reason_code = 8;
7906 		return (0);
7907 	}
7908 	if (asoc->sctp_cmt_on_off > 0) {
7909 		/* get the last start point */
7910 		start_at = asoc->last_net_cmt_send_started;
7911 		if (start_at == NULL) {
7912 			/* null so to beginning */
7913 			start_at = TAILQ_FIRST(&asoc->nets);
7914 		} else {
7915 			start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7916 			if (start_at == NULL) {
7917 				start_at = TAILQ_FIRST(&asoc->nets);
7918 			}
7919 		}
7920 		asoc->last_net_cmt_send_started = start_at;
7921 	} else {
7922 		start_at = TAILQ_FIRST(&asoc->nets);
7923 	}
7924 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7925 		if (chk->whoTo == NULL) {
7926 			if (asoc->alternate) {
7927 				chk->whoTo = asoc->alternate;
7928 			} else {
7929 				chk->whoTo = asoc->primary_destination;
7930 			}
7931 			atomic_add_int(&chk->whoTo->ref_count, 1);
7932 		}
7933 	}
7934 	old_start_at = NULL;
7935 again_one_more_time:
7936 	for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7937 		/* how much can we send? */
7938 		/* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7939 		if (old_start_at && (old_start_at == net)) {
7940 			/* through list ocmpletely. */
7941 			break;
7942 		}
7943 		tsns_sent = 0xa;
7944 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7945 		    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7946 		    (net->flight_size >= net->cwnd)) {
7947 			/*
7948 			 * Nothing on control or asconf and flight is full,
7949 			 * we can skip even in the CMT case.
7950 			 */
7951 			continue;
7952 		}
7953 		bundle_at = 0;
7954 		endoutchain = outchain = NULL;
7955 		no_fragmentflg = 1;
7956 		one_chunk = 0;
7957 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7958 			skip_data_for_this_net = 1;
7959 		} else {
7960 			skip_data_for_this_net = 0;
7961 		}
7962 		if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7963 			/*
7964 			 * if we have a route and an ifp check to see if we
7965 			 * have room to send to this guy
7966 			 */
7967 			struct ifnet *ifp;
7968 
7969 			ifp = net->ro.ro_rt->rt_ifp;
7970 			if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7971 				SCTP_STAT_INCR(sctps_ifnomemqueued);
7972 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7973 					sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7974 				}
7975 				continue;
7976 			}
7977 		}
7978 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7979 #ifdef INET
7980 		case AF_INET:
7981 			mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7982 			break;
7983 #endif
7984 #ifdef INET6
7985 		case AF_INET6:
7986 			mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7987 			break;
7988 #endif
7989 		default:
7990 			/* TSNH */
7991 			mtu = net->mtu;
7992 			break;
7993 		}
7994 		mx_mtu = mtu;
7995 		to_out = 0;
7996 		if (mtu > asoc->peers_rwnd) {
7997 			if (asoc->total_flight > 0) {
7998 				/* We have a packet in flight somewhere */
7999 				r_mtu = asoc->peers_rwnd;
8000 			} else {
8001 				/* We are always allowed to send one MTU out */
8002 				one_chunk = 1;
8003 				r_mtu = mtu;
8004 			}
8005 		} else {
8006 			r_mtu = mtu;
8007 		}
8008 		/************************/
8009 		/* ASCONF transmission */
8010 		/************************/
8011 		/* Now first lets go through the asconf queue */
8012 		TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
8013 			if (chk->rec.chunk_id.id != SCTP_ASCONF) {
8014 				continue;
8015 			}
8016 			if (chk->whoTo == NULL) {
8017 				if (asoc->alternate == NULL) {
8018 					if (asoc->primary_destination != net) {
8019 						break;
8020 					}
8021 				} else {
8022 					if (asoc->alternate != net) {
8023 						break;
8024 					}
8025 				}
8026 			} else {
8027 				if (chk->whoTo != net) {
8028 					break;
8029 				}
8030 			}
8031 			if (chk->data == NULL) {
8032 				break;
8033 			}
8034 			if (chk->sent != SCTP_DATAGRAM_UNSENT &&
8035 			    chk->sent != SCTP_DATAGRAM_RESEND) {
8036 				break;
8037 			}
8038 			/*
8039 			 * if no AUTH is yet included and this chunk
8040 			 * requires it, make sure to account for it.  We
8041 			 * don't apply the size until the AUTH chunk is
8042 			 * actually added below in case there is no room for
8043 			 * this chunk. NOTE: we overload the use of "omtu"
8044 			 * here
8045 			 */
8046 			if ((auth == NULL) &&
8047 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8048 			    stcb->asoc.peer_auth_chunks)) {
8049 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8050 			} else
8051 				omtu = 0;
8052 			/* Here we do NOT factor the r_mtu */
8053 			if ((chk->send_size < (int)(mtu - omtu)) ||
8054 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8055 				/*
8056 				 * We probably should glom the mbuf chain
8057 				 * from the chk->data for control but the
8058 				 * problem is it becomes yet one more level
8059 				 * of tracking to do if for some reason
8060 				 * output fails. Then I have got to
8061 				 * reconstruct the merged control chain.. el
8062 				 * yucko.. for now we take the easy way and
8063 				 * do the copy
8064 				 */
8065 				/*
8066 				 * Add an AUTH chunk, if chunk requires it
8067 				 * save the offset into the chain for AUTH
8068 				 */
8069 				if ((auth == NULL) &&
8070 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8071 				    stcb->asoc.peer_auth_chunks))) {
8072 					outchain = sctp_add_auth_chunk(outchain,
8073 					    &endoutchain,
8074 					    &auth,
8075 					    &auth_offset,
8076 					    stcb,
8077 					    chk->rec.chunk_id.id);
8078 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8079 				}
8080 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8081 				    (int)chk->rec.chunk_id.can_take_data,
8082 				    chk->send_size, chk->copy_by_ref);
8083 				if (outchain == NULL) {
8084 					*reason_code = 8;
8085 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8086 					return (ENOMEM);
8087 				}
8088 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8089 				/* update our MTU size */
8090 				if (mtu > (chk->send_size + omtu))
8091 					mtu -= (chk->send_size + omtu);
8092 				else
8093 					mtu = 0;
8094 				to_out += (chk->send_size + omtu);
8095 				/* Do clear IP_DF ? */
8096 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8097 					no_fragmentflg = 0;
8098 				}
8099 				if (chk->rec.chunk_id.can_take_data)
8100 					chk->data = NULL;
8101 				/*
8102 				 * set hb flag since we can use these for
8103 				 * RTO
8104 				 */
8105 				hbflag = 1;
8106 				asconf = 1;
8107 				/*
8108 				 * should sysctl this: don't bundle data
8109 				 * with ASCONF since it requires AUTH
8110 				 */
8111 				no_data_chunks = 1;
8112 				chk->sent = SCTP_DATAGRAM_SENT;
8113 				if (chk->whoTo == NULL) {
8114 					chk->whoTo = net;
8115 					atomic_add_int(&net->ref_count, 1);
8116 				}
8117 				chk->snd_count++;
8118 				if (mtu == 0) {
8119 					/*
8120 					 * Ok we are out of room but we can
8121 					 * output without effecting the
8122 					 * flight size since this little guy
8123 					 * is a control only packet.
8124 					 */
8125 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8126 					/*
8127 					 * do NOT clear the asconf flag as
8128 					 * it is used to do appropriate
8129 					 * source address selection.
8130 					 */
8131 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8132 					    (struct sockaddr *)&net->ro._l_addr,
8133 					    outchain, auth_offset, auth,
8134 					    stcb->asoc.authinfo.active_keyid,
8135 					    no_fragmentflg, 0, asconf,
8136 					    inp->sctp_lport, stcb->rport,
8137 					    htonl(stcb->asoc.peer_vtag),
8138 					    net->port, so_locked, NULL, NULL))) {
8139 						if (error == ENOBUFS) {
8140 							asoc->ifp_had_enobuf = 1;
8141 							SCTP_STAT_INCR(sctps_lowlevelerr);
8142 						}
8143 						if (from_where == 0) {
8144 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
8145 						}
8146 						if (*now_filled == 0) {
8147 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8148 							*now_filled = 1;
8149 							*now = net->last_sent_time;
8150 						} else {
8151 							net->last_sent_time = *now;
8152 						}
8153 						hbflag = 0;
8154 						/* error, could not output */
8155 						if (error == EHOSTUNREACH) {
8156 							/*
8157 							 * Destination went
8158 							 * unreachable
8159 							 * during this send
8160 							 */
8161 							sctp_move_chunks_from_net(stcb, net);
8162 						}
8163 						*reason_code = 7;
8164 						continue;
8165 					} else
8166 						asoc->ifp_had_enobuf = 0;
8167 					if (*now_filled == 0) {
8168 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8169 						*now_filled = 1;
8170 						*now = net->last_sent_time;
8171 					} else {
8172 						net->last_sent_time = *now;
8173 					}
8174 					hbflag = 0;
8175 					/*
8176 					 * increase the number we sent, if a
8177 					 * cookie is sent we don't tell them
8178 					 * any was sent out.
8179 					 */
8180 					outchain = endoutchain = NULL;
8181 					auth = NULL;
8182 					auth_offset = 0;
8183 					if (!no_out_cnt)
8184 						*num_out += ctl_cnt;
8185 					/* recalc a clean slate and setup */
8186 					switch (net->ro._l_addr.sa.sa_family) {
8187 #ifdef INET
8188 					case AF_INET:
8189 						mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8190 						break;
8191 #endif
8192 #ifdef INET6
8193 					case AF_INET6:
8194 						mtu = net->mtu - SCTP_MIN_OVERHEAD;
8195 						break;
8196 #endif
8197 					default:
8198 						/* TSNH */
8199 						mtu = net->mtu;
8200 						break;
8201 					}
8202 					to_out = 0;
8203 					no_fragmentflg = 1;
8204 				}
8205 			}
8206 		}
8207 		/************************/
8208 		/* Control transmission */
8209 		/************************/
8210 		/* Now first lets go through the control queue */
8211 		TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
8212 			if ((sack_goes_to) &&
8213 			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO) &&
8214 			    (chk->whoTo != sack_goes_to)) {
8215 				/*
8216 				 * if we have a sack in queue, and we are
8217 				 * looking at an ecn echo that is NOT queued
8218 				 * to where the sack is going..
8219 				 */
8220 				if (chk->whoTo == net) {
8221 					/*
8222 					 * Don't transmit it to where its
8223 					 * going (current net)
8224 					 */
8225 					continue;
8226 				} else if (sack_goes_to == net) {
8227 					/*
8228 					 * But do transmit it to this
8229 					 * address
8230 					 */
8231 					goto skip_net_check;
8232 				}
8233 			}
8234 			if (chk->whoTo == NULL) {
8235 				if (asoc->alternate == NULL) {
8236 					if (asoc->primary_destination != net) {
8237 						continue;
8238 					}
8239 				} else {
8240 					if (asoc->alternate != net) {
8241 						continue;
8242 					}
8243 				}
8244 			} else {
8245 				if (chk->whoTo != net) {
8246 					continue;
8247 				}
8248 			}
8249 	skip_net_check:
8250 			if (chk->data == NULL) {
8251 				continue;
8252 			}
8253 			if (chk->sent != SCTP_DATAGRAM_UNSENT) {
8254 				/*
8255 				 * It must be unsent. Cookies and ASCONF's
8256 				 * hang around but there timers will force
8257 				 * when marked for resend.
8258 				 */
8259 				continue;
8260 			}
8261 			/*
8262 			 * if no AUTH is yet included and this chunk
8263 			 * requires it, make sure to account for it.  We
8264 			 * don't apply the size until the AUTH chunk is
8265 			 * actually added below in case there is no room for
8266 			 * this chunk. NOTE: we overload the use of "omtu"
8267 			 * here
8268 			 */
8269 			if ((auth == NULL) &&
8270 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8271 			    stcb->asoc.peer_auth_chunks)) {
8272 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8273 			} else
8274 				omtu = 0;
8275 			/* Here we do NOT factor the r_mtu */
8276 			if ((chk->send_size <= (int)(mtu - omtu)) ||
8277 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8278 				/*
8279 				 * We probably should glom the mbuf chain
8280 				 * from the chk->data for control but the
8281 				 * problem is it becomes yet one more level
8282 				 * of tracking to do if for some reason
8283 				 * output fails. Then I have got to
8284 				 * reconstruct the merged control chain.. el
8285 				 * yucko.. for now we take the easy way and
8286 				 * do the copy
8287 				 */
8288 				/*
8289 				 * Add an AUTH chunk, if chunk requires it
8290 				 * save the offset into the chain for AUTH
8291 				 */
8292 				if ((auth == NULL) &&
8293 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8294 				    stcb->asoc.peer_auth_chunks))) {
8295 					outchain = sctp_add_auth_chunk(outchain,
8296 					    &endoutchain,
8297 					    &auth,
8298 					    &auth_offset,
8299 					    stcb,
8300 					    chk->rec.chunk_id.id);
8301 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8302 				}
8303 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8304 				    (int)chk->rec.chunk_id.can_take_data,
8305 				    chk->send_size, chk->copy_by_ref);
8306 				if (outchain == NULL) {
8307 					*reason_code = 8;
8308 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8309 					return (ENOMEM);
8310 				}
8311 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8312 				/* update our MTU size */
8313 				if (mtu > (chk->send_size + omtu))
8314 					mtu -= (chk->send_size + omtu);
8315 				else
8316 					mtu = 0;
8317 				to_out += (chk->send_size + omtu);
8318 				/* Do clear IP_DF ? */
8319 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8320 					no_fragmentflg = 0;
8321 				}
8322 				if (chk->rec.chunk_id.can_take_data)
8323 					chk->data = NULL;
8324 				/* Mark things to be removed, if needed */
8325 				if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8326 				    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
8327 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
8328 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
8329 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
8330 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
8331 				    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
8332 				    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
8333 				    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
8334 				    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
8335 				    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
8336 					if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
8337 						hbflag = 1;
8338 					}
8339 					/* remove these chunks at the end */
8340 					if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8341 					    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
8342 						/* turn off the timer */
8343 						if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
8344 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
8345 							    inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
8346 						}
8347 					}
8348 					ctl_cnt++;
8349 				} else {
8350 					/*
8351 					 * Other chunks, since they have
8352 					 * timers running (i.e. COOKIE) we
8353 					 * just "trust" that it gets sent or
8354 					 * retransmitted.
8355 					 */
8356 					ctl_cnt++;
8357 					if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8358 						cookie = 1;
8359 						no_out_cnt = 1;
8360 					} else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
8361 						/*
8362 						 * Increment ecne send count
8363 						 * here this means we may be
8364 						 * over-zealous in our
8365 						 * counting if the send
8366 						 * fails, but its the best
8367 						 * place to do it (we used
8368 						 * to do it in the queue of
8369 						 * the chunk, but that did
8370 						 * not tell how many times
8371 						 * it was sent.
8372 						 */
8373 						SCTP_STAT_INCR(sctps_sendecne);
8374 					}
8375 					chk->sent = SCTP_DATAGRAM_SENT;
8376 					if (chk->whoTo == NULL) {
8377 						chk->whoTo = net;
8378 						atomic_add_int(&net->ref_count, 1);
8379 					}
8380 					chk->snd_count++;
8381 				}
8382 				if (mtu == 0) {
8383 					/*
8384 					 * Ok we are out of room but we can
8385 					 * output without effecting the
8386 					 * flight size since this little guy
8387 					 * is a control only packet.
8388 					 */
8389 					if (asconf) {
8390 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8391 						/*
8392 						 * do NOT clear the asconf
8393 						 * flag as it is used to do
8394 						 * appropriate source
8395 						 * address selection.
8396 						 */
8397 					}
8398 					if (cookie) {
8399 						sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8400 						cookie = 0;
8401 					}
8402 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8403 					    (struct sockaddr *)&net->ro._l_addr,
8404 					    outchain,
8405 					    auth_offset, auth,
8406 					    stcb->asoc.authinfo.active_keyid,
8407 					    no_fragmentflg, 0, asconf,
8408 					    inp->sctp_lport, stcb->rport,
8409 					    htonl(stcb->asoc.peer_vtag),
8410 					    net->port, so_locked, NULL, NULL))) {
8411 						if (error == ENOBUFS) {
8412 							asoc->ifp_had_enobuf = 1;
8413 							SCTP_STAT_INCR(sctps_lowlevelerr);
8414 						}
8415 						if (from_where == 0) {
8416 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
8417 						}
8418 						/* error, could not output */
8419 						if (hbflag) {
8420 							if (*now_filled == 0) {
8421 								(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8422 								*now_filled = 1;
8423 								*now = net->last_sent_time;
8424 							} else {
8425 								net->last_sent_time = *now;
8426 							}
8427 							hbflag = 0;
8428 						}
8429 						if (error == EHOSTUNREACH) {
8430 							/*
8431 							 * Destination went
8432 							 * unreachable
8433 							 * during this send
8434 							 */
8435 							sctp_move_chunks_from_net(stcb, net);
8436 						}
8437 						*reason_code = 7;
8438 						continue;
8439 					} else
8440 						asoc->ifp_had_enobuf = 0;
8441 					/* Only HB or ASCONF advances time */
8442 					if (hbflag) {
8443 						if (*now_filled == 0) {
8444 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8445 							*now_filled = 1;
8446 							*now = net->last_sent_time;
8447 						} else {
8448 							net->last_sent_time = *now;
8449 						}
8450 						hbflag = 0;
8451 					}
8452 					/*
8453 					 * increase the number we sent, if a
8454 					 * cookie is sent we don't tell them
8455 					 * any was sent out.
8456 					 */
8457 					outchain = endoutchain = NULL;
8458 					auth = NULL;
8459 					auth_offset = 0;
8460 					if (!no_out_cnt)
8461 						*num_out += ctl_cnt;
8462 					/* recalc a clean slate and setup */
8463 					switch (net->ro._l_addr.sa.sa_family) {
8464 #ifdef INET
8465 					case AF_INET:
8466 						mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8467 						break;
8468 #endif
8469 #ifdef INET6
8470 					case AF_INET6:
8471 						mtu = net->mtu - SCTP_MIN_OVERHEAD;
8472 						break;
8473 #endif
8474 					default:
8475 						/* TSNH */
8476 						mtu = net->mtu;
8477 						break;
8478 					}
8479 					to_out = 0;
8480 					no_fragmentflg = 1;
8481 				}
8482 			}
8483 		}
8484 		/* JRI: if dest is in PF state, do not send data to it */
8485 		if ((asoc->sctp_cmt_on_off > 0) &&
8486 		    (net != stcb->asoc.alternate) &&
8487 		    (net->dest_state & SCTP_ADDR_PF)) {
8488 			goto no_data_fill;
8489 		}
8490 		if (net->flight_size >= net->cwnd) {
8491 			goto no_data_fill;
8492 		}
8493 		if ((asoc->sctp_cmt_on_off > 0) &&
8494 		    (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) &&
8495 		    (net->flight_size > max_rwnd_per_dest)) {
8496 			goto no_data_fill;
8497 		}
8498 		/*
8499 		 * We need a specific accounting for the usage of the send
8500 		 * buffer. We also need to check the number of messages per
8501 		 * net. For now, this is better than nothing and it disabled
8502 		 * by default...
8503 		 */
8504 		if ((asoc->sctp_cmt_on_off > 0) &&
8505 		    (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) &&
8506 		    (max_send_per_dest > 0) &&
8507 		    (net->flight_size > max_send_per_dest)) {
8508 			goto no_data_fill;
8509 		}
8510 		/*********************/
8511 		/* Data transmission */
8512 		/*********************/
8513 		/*
8514 		 * if AUTH for DATA is required and no AUTH has been added
8515 		 * yet, account for this in the mtu now... if no data can be
8516 		 * bundled, this adjustment won't matter anyways since the
8517 		 * packet will be going out...
8518 		 */
8519 		data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8520 		    stcb->asoc.peer_auth_chunks);
8521 		if (data_auth_reqd && (auth == NULL)) {
8522 			mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8523 		}
8524 		/* now lets add any data within the MTU constraints */
8525 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8526 		case AF_INET:
8527 			if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8528 				omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8529 			else
8530 				omtu = 0;
8531 			break;
8532 #ifdef INET6
8533 		case AF_INET6:
8534 			if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8535 				omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8536 			else
8537 				omtu = 0;
8538 			break;
8539 #endif
8540 		default:
8541 			/* TSNH */
8542 			omtu = 0;
8543 			break;
8544 		}
8545 		if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) &&
8546 		    (skip_data_for_this_net == 0)) ||
8547 		    (cookie)) {
8548 			TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
8549 				if (no_data_chunks) {
8550 					/* let only control go out */
8551 					*reason_code = 1;
8552 					break;
8553 				}
8554 				if (net->flight_size >= net->cwnd) {
8555 					/* skip this net, no room for data */
8556 					*reason_code = 2;
8557 					break;
8558 				}
8559 				if ((chk->whoTo != NULL) &&
8560 				    (chk->whoTo != net)) {
8561 					/* Don't send the chunk on this net */
8562 					continue;
8563 				}
8564 				if (asoc->sctp_cmt_on_off == 0) {
8565 					if ((asoc->alternate) &&
8566 					    (asoc->alternate != net) &&
8567 					    (chk->whoTo == NULL)) {
8568 						continue;
8569 					} else if ((net != asoc->primary_destination) &&
8570 						    (asoc->alternate == NULL) &&
8571 					    (chk->whoTo == NULL)) {
8572 						continue;
8573 					}
8574 				}
8575 				if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8576 					/*-
8577 					 * strange, we have a chunk that is
8578 					 * to big for its destination and
8579 					 * yet no fragment ok flag.
8580 					 * Something went wrong when the
8581 					 * PMTU changed...we did not mark
8582 					 * this chunk for some reason?? I
8583 					 * will fix it here by letting IP
8584 					 * fragment it for now and printing
8585 					 * a warning. This really should not
8586 					 * happen ...
8587 					 */
8588 					SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8589 					    chk->send_size, mtu);
8590 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8591 				}
8592 				if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8593 				    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
8594 					struct sctp_data_chunk *dchkh;
8595 
8596 					dchkh = mtod(chk->data, struct sctp_data_chunk *);
8597 					dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8598 				}
8599 				if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8600 				    ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8601 					/* ok we will add this one */
8602 
8603 					/*
8604 					 * Add an AUTH chunk, if chunk
8605 					 * requires it, save the offset into
8606 					 * the chain for AUTH
8607 					 */
8608 					if (data_auth_reqd) {
8609 						if (auth == NULL) {
8610 							outchain = sctp_add_auth_chunk(outchain,
8611 							    &endoutchain,
8612 							    &auth,
8613 							    &auth_offset,
8614 							    stcb,
8615 							    SCTP_DATA);
8616 							auth_keyid = chk->auth_keyid;
8617 							override_ok = 0;
8618 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8619 						} else if (override_ok) {
8620 							/*
8621 							 * use this data's
8622 							 * keyid
8623 							 */
8624 							auth_keyid = chk->auth_keyid;
8625 							override_ok = 0;
8626 						} else if (auth_keyid != chk->auth_keyid) {
8627 							/*
8628 							 * different keyid,
8629 							 * so done bundling
8630 							 */
8631 							break;
8632 						}
8633 					}
8634 					outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8635 					    chk->send_size, chk->copy_by_ref);
8636 					if (outchain == NULL) {
8637 						SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8638 						if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8639 							sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8640 						}
8641 						*reason_code = 3;
8642 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8643 						return (ENOMEM);
8644 					}
8645 					/* upate our MTU size */
8646 					/* Do clear IP_DF ? */
8647 					if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8648 						no_fragmentflg = 0;
8649 					}
8650 					/* unsigned subtraction of mtu */
8651 					if (mtu > chk->send_size)
8652 						mtu -= chk->send_size;
8653 					else
8654 						mtu = 0;
8655 					/* unsigned subtraction of r_mtu */
8656 					if (r_mtu > chk->send_size)
8657 						r_mtu -= chk->send_size;
8658 					else
8659 						r_mtu = 0;
8660 
8661 					to_out += chk->send_size;
8662 					if ((to_out > mx_mtu) && no_fragmentflg) {
8663 #ifdef INVARIANTS
8664 						panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8665 #else
8666 						SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8667 						    mx_mtu, to_out);
8668 #endif
8669 					}
8670 					chk->window_probe = 0;
8671 					data_list[bundle_at++] = chk;
8672 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8673 						break;
8674 					}
8675 					if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8676 						if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8677 							SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8678 						} else {
8679 							SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8680 						}
8681 						if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8682 						    ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8683 							/*
8684 							 * Count number of
8685 							 * user msg's that
8686 							 * were fragmented
8687 							 * we do this by
8688 							 * counting when we
8689 							 * see a LAST
8690 							 * fragment only.
8691 							 */
8692 							SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8693 					}
8694 					if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8695 						if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8696 							data_list[0]->window_probe = 1;
8697 							net->window_probe = 1;
8698 						}
8699 						break;
8700 					}
8701 				} else {
8702 					/*
8703 					 * Must be sent in order of the
8704 					 * TSN's (on a network)
8705 					 */
8706 					break;
8707 				}
8708 			}	/* for (chunk gather loop for this net) */
8709 		}		/* if asoc.state OPEN */
8710 no_data_fill:
8711 		/* Is there something to send for this destination? */
8712 		if (outchain) {
8713 			/* We may need to start a control timer or two */
8714 			if (asconf) {
8715 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8716 				    stcb, net);
8717 				/*
8718 				 * do NOT clear the asconf flag as it is
8719 				 * used to do appropriate source address
8720 				 * selection.
8721 				 */
8722 			}
8723 			if (cookie) {
8724 				sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8725 				cookie = 0;
8726 			}
8727 			/* must start a send timer if data is being sent */
8728 			if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8729 				/*
8730 				 * no timer running on this destination
8731 				 * restart it.
8732 				 */
8733 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8734 			}
8735 			/* Now send it, if there is anything to send :> */
8736 			if ((error = sctp_lowlevel_chunk_output(inp,
8737 			    stcb,
8738 			    net,
8739 			    (struct sockaddr *)&net->ro._l_addr,
8740 			    outchain,
8741 			    auth_offset,
8742 			    auth,
8743 			    auth_keyid,
8744 			    no_fragmentflg,
8745 			    bundle_at,
8746 			    asconf,
8747 			    inp->sctp_lport, stcb->rport,
8748 			    htonl(stcb->asoc.peer_vtag),
8749 			    net->port, so_locked, NULL, NULL))) {
8750 				/* error, we could not output */
8751 				if (error == ENOBUFS) {
8752 					SCTP_STAT_INCR(sctps_lowlevelerr);
8753 					asoc->ifp_had_enobuf = 1;
8754 				}
8755 				if (from_where == 0) {
8756 					SCTP_STAT_INCR(sctps_lowlevelerrusr);
8757 				}
8758 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8759 				if (hbflag) {
8760 					if (*now_filled == 0) {
8761 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8762 						*now_filled = 1;
8763 						*now = net->last_sent_time;
8764 					} else {
8765 						net->last_sent_time = *now;
8766 					}
8767 					hbflag = 0;
8768 				}
8769 				if (error == EHOSTUNREACH) {
8770 					/*
8771 					 * Destination went unreachable
8772 					 * during this send
8773 					 */
8774 					sctp_move_chunks_from_net(stcb, net);
8775 				}
8776 				*reason_code = 6;
8777 				/*-
8778 				 * I add this line to be paranoid. As far as
8779 				 * I can tell the continue, takes us back to
8780 				 * the top of the for, but just to make sure
8781 				 * I will reset these again here.
8782 				 */
8783 				ctl_cnt = bundle_at = 0;
8784 				continue;	/* This takes us back to the
8785 						 * for() for the nets. */
8786 			} else {
8787 				asoc->ifp_had_enobuf = 0;
8788 			}
8789 			endoutchain = NULL;
8790 			auth = NULL;
8791 			auth_offset = 0;
8792 			if (bundle_at || hbflag) {
8793 				/* For data/asconf and hb set time */
8794 				if (*now_filled == 0) {
8795 					(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8796 					*now_filled = 1;
8797 					*now = net->last_sent_time;
8798 				} else {
8799 					net->last_sent_time = *now;
8800 				}
8801 			}
8802 			if (!no_out_cnt) {
8803 				*num_out += (ctl_cnt + bundle_at);
8804 			}
8805 			if (bundle_at) {
8806 				/* setup for a RTO measurement */
8807 				tsns_sent = data_list[0]->rec.data.TSN_seq;
8808 				/* fill time if not already filled */
8809 				if (*now_filled == 0) {
8810 					(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8811 					*now_filled = 1;
8812 					*now = asoc->time_last_sent;
8813 				} else {
8814 					asoc->time_last_sent = *now;
8815 				}
8816 				if (net->rto_needed) {
8817 					data_list[0]->do_rtt = 1;
8818 					net->rto_needed = 0;
8819 				}
8820 				SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8821 				sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8822 			}
8823 			if (one_chunk) {
8824 				break;
8825 			}
8826 		}
8827 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8828 			sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8829 		}
8830 	}
8831 	if (old_start_at == NULL) {
8832 		old_start_at = start_at;
8833 		start_at = TAILQ_FIRST(&asoc->nets);
8834 		if (old_start_at)
8835 			goto again_one_more_time;
8836 	}
8837 	/*
8838 	 * At the end there should be no NON timed chunks hanging on this
8839 	 * queue.
8840 	 */
8841 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8842 		sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8843 	}
8844 	if ((*num_out == 0) && (*reason_code == 0)) {
8845 		*reason_code = 4;
8846 	} else {
8847 		*reason_code = 5;
8848 	}
8849 	sctp_clean_up_ctl(stcb, asoc, so_locked);
8850 	return (0);
8851 }
8852 
8853 void
8854 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8855 {
8856 	/*-
8857 	 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8858 	 * the control chunk queue.
8859 	 */
8860 	struct sctp_chunkhdr *hdr;
8861 	struct sctp_tmit_chunk *chk;
8862 	struct mbuf *mat;
8863 
8864 	SCTP_TCB_LOCK_ASSERT(stcb);
8865 	sctp_alloc_a_chunk(stcb, chk);
8866 	if (chk == NULL) {
8867 		/* no memory */
8868 		sctp_m_freem(op_err);
8869 		return;
8870 	}
8871 	chk->copy_by_ref = 0;
8872 	SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8873 	if (op_err == NULL) {
8874 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
8875 		return;
8876 	}
8877 	chk->send_size = 0;
8878 	mat = op_err;
8879 	while (mat != NULL) {
8880 		chk->send_size += SCTP_BUF_LEN(mat);
8881 		mat = SCTP_BUF_NEXT(mat);
8882 	}
8883 	chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8884 	chk->rec.chunk_id.can_take_data = 1;
8885 	chk->sent = SCTP_DATAGRAM_UNSENT;
8886 	chk->snd_count = 0;
8887 	chk->flags = 0;
8888 	chk->asoc = &stcb->asoc;
8889 	chk->data = op_err;
8890 	chk->whoTo = NULL;
8891 	hdr = mtod(op_err, struct sctp_chunkhdr *);
8892 	hdr->chunk_type = SCTP_OPERATION_ERROR;
8893 	hdr->chunk_flags = 0;
8894 	hdr->chunk_length = htons(chk->send_size);
8895 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8896 	    chk,
8897 	    sctp_next);
8898 	chk->asoc->ctrl_queue_cnt++;
8899 }
8900 
8901 int
8902 sctp_send_cookie_echo(struct mbuf *m,
8903     int offset,
8904     struct sctp_tcb *stcb,
8905     struct sctp_nets *net)
8906 {
8907 	/*-
8908 	 * pull out the cookie and put it at the front of the control chunk
8909 	 * queue.
8910 	 */
8911 	int at;
8912 	struct mbuf *cookie;
8913 	struct sctp_paramhdr parm, *phdr;
8914 	struct sctp_chunkhdr *hdr;
8915 	struct sctp_tmit_chunk *chk;
8916 	uint16_t ptype, plen;
8917 
8918 	/* First find the cookie in the param area */
8919 	cookie = NULL;
8920 	at = offset + sizeof(struct sctp_init_chunk);
8921 
8922 	SCTP_TCB_LOCK_ASSERT(stcb);
8923 	do {
8924 		phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8925 		if (phdr == NULL) {
8926 			return (-3);
8927 		}
8928 		ptype = ntohs(phdr->param_type);
8929 		plen = ntohs(phdr->param_length);
8930 		if (ptype == SCTP_STATE_COOKIE) {
8931 			int pad;
8932 
8933 			/* found the cookie */
8934 			if ((pad = (plen % 4))) {
8935 				plen += 4 - pad;
8936 			}
8937 			cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8938 			if (cookie == NULL) {
8939 				/* No memory */
8940 				return (-2);
8941 			}
8942 #ifdef SCTP_MBUF_LOGGING
8943 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8944 				struct mbuf *mat;
8945 
8946 				for (mat = cookie; mat; mat = SCTP_BUF_NEXT(mat)) {
8947 					if (SCTP_BUF_IS_EXTENDED(mat)) {
8948 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8949 					}
8950 				}
8951 			}
8952 #endif
8953 			break;
8954 		}
8955 		at += SCTP_SIZE32(plen);
8956 	} while (phdr);
8957 	if (cookie == NULL) {
8958 		/* Did not find the cookie */
8959 		return (-3);
8960 	}
8961 	/* ok, we got the cookie lets change it into a cookie echo chunk */
8962 
8963 	/* first the change from param to cookie */
8964 	hdr = mtod(cookie, struct sctp_chunkhdr *);
8965 	hdr->chunk_type = SCTP_COOKIE_ECHO;
8966 	hdr->chunk_flags = 0;
8967 	/* get the chunk stuff now and place it in the FRONT of the queue */
8968 	sctp_alloc_a_chunk(stcb, chk);
8969 	if (chk == NULL) {
8970 		/* no memory */
8971 		sctp_m_freem(cookie);
8972 		return (-5);
8973 	}
8974 	chk->copy_by_ref = 0;
8975 	chk->send_size = plen;
8976 	chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8977 	chk->rec.chunk_id.can_take_data = 0;
8978 	chk->sent = SCTP_DATAGRAM_UNSENT;
8979 	chk->snd_count = 0;
8980 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8981 	chk->asoc = &stcb->asoc;
8982 	chk->data = cookie;
8983 	chk->whoTo = net;
8984 	atomic_add_int(&chk->whoTo->ref_count, 1);
8985 	TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8986 	chk->asoc->ctrl_queue_cnt++;
8987 	return (0);
8988 }
8989 
8990 void
8991 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8992     struct mbuf *m,
8993     int offset,
8994     int chk_length,
8995     struct sctp_nets *net)
8996 {
8997 	/*
8998 	 * take a HB request and make it into a HB ack and send it.
8999 	 */
9000 	struct mbuf *outchain;
9001 	struct sctp_chunkhdr *chdr;
9002 	struct sctp_tmit_chunk *chk;
9003 
9004 
9005 	if (net == NULL)
9006 		/* must have a net pointer */
9007 		return;
9008 
9009 	outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
9010 	if (outchain == NULL) {
9011 		/* gak out of memory */
9012 		return;
9013 	}
9014 #ifdef SCTP_MBUF_LOGGING
9015 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9016 		struct mbuf *mat;
9017 
9018 		for (mat = outchain; mat; mat = SCTP_BUF_NEXT(mat)) {
9019 			if (SCTP_BUF_IS_EXTENDED(mat)) {
9020 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
9021 			}
9022 		}
9023 	}
9024 #endif
9025 	chdr = mtod(outchain, struct sctp_chunkhdr *);
9026 	chdr->chunk_type = SCTP_HEARTBEAT_ACK;
9027 	chdr->chunk_flags = 0;
9028 	if (chk_length % 4) {
9029 		/* need pad */
9030 		uint32_t cpthis = 0;
9031 		int padlen;
9032 
9033 		padlen = 4 - (chk_length % 4);
9034 		m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
9035 	}
9036 	sctp_alloc_a_chunk(stcb, chk);
9037 	if (chk == NULL) {
9038 		/* no memory */
9039 		sctp_m_freem(outchain);
9040 		return;
9041 	}
9042 	chk->copy_by_ref = 0;
9043 	chk->send_size = chk_length;
9044 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
9045 	chk->rec.chunk_id.can_take_data = 1;
9046 	chk->sent = SCTP_DATAGRAM_UNSENT;
9047 	chk->snd_count = 0;
9048 	chk->flags = 0;
9049 	chk->asoc = &stcb->asoc;
9050 	chk->data = outchain;
9051 	chk->whoTo = net;
9052 	atomic_add_int(&chk->whoTo->ref_count, 1);
9053 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9054 	chk->asoc->ctrl_queue_cnt++;
9055 }
9056 
9057 void
9058 sctp_send_cookie_ack(struct sctp_tcb *stcb)
9059 {
9060 	/* formulate and queue a cookie-ack back to sender */
9061 	struct mbuf *cookie_ack;
9062 	struct sctp_chunkhdr *hdr;
9063 	struct sctp_tmit_chunk *chk;
9064 
9065 	cookie_ack = NULL;
9066 	SCTP_TCB_LOCK_ASSERT(stcb);
9067 
9068 	cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
9069 	if (cookie_ack == NULL) {
9070 		/* no mbuf's */
9071 		return;
9072 	}
9073 	SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
9074 	sctp_alloc_a_chunk(stcb, chk);
9075 	if (chk == NULL) {
9076 		/* no memory */
9077 		sctp_m_freem(cookie_ack);
9078 		return;
9079 	}
9080 	chk->copy_by_ref = 0;
9081 	chk->send_size = sizeof(struct sctp_chunkhdr);
9082 	chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
9083 	chk->rec.chunk_id.can_take_data = 1;
9084 	chk->sent = SCTP_DATAGRAM_UNSENT;
9085 	chk->snd_count = 0;
9086 	chk->flags = 0;
9087 	chk->asoc = &stcb->asoc;
9088 	chk->data = cookie_ack;
9089 	if (chk->asoc->last_control_chunk_from != NULL) {
9090 		chk->whoTo = chk->asoc->last_control_chunk_from;
9091 		atomic_add_int(&chk->whoTo->ref_count, 1);
9092 	} else {
9093 		chk->whoTo = NULL;
9094 	}
9095 	hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
9096 	hdr->chunk_type = SCTP_COOKIE_ACK;
9097 	hdr->chunk_flags = 0;
9098 	hdr->chunk_length = htons(chk->send_size);
9099 	SCTP_BUF_LEN(cookie_ack) = chk->send_size;
9100 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9101 	chk->asoc->ctrl_queue_cnt++;
9102 	return;
9103 }
9104 
9105 
9106 void
9107 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
9108 {
9109 	/* formulate and queue a SHUTDOWN-ACK back to the sender */
9110 	struct mbuf *m_shutdown_ack;
9111 	struct sctp_shutdown_ack_chunk *ack_cp;
9112 	struct sctp_tmit_chunk *chk;
9113 
9114 	m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
9115 	if (m_shutdown_ack == NULL) {
9116 		/* no mbuf's */
9117 		return;
9118 	}
9119 	SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
9120 	sctp_alloc_a_chunk(stcb, chk);
9121 	if (chk == NULL) {
9122 		/* no memory */
9123 		sctp_m_freem(m_shutdown_ack);
9124 		return;
9125 	}
9126 	chk->copy_by_ref = 0;
9127 	chk->send_size = sizeof(struct sctp_chunkhdr);
9128 	chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
9129 	chk->rec.chunk_id.can_take_data = 1;
9130 	chk->sent = SCTP_DATAGRAM_UNSENT;
9131 	chk->snd_count = 0;
9132 	chk->flags = 0;
9133 	chk->asoc = &stcb->asoc;
9134 	chk->data = m_shutdown_ack;
9135 	chk->whoTo = net;
9136 	if (chk->whoTo) {
9137 		atomic_add_int(&chk->whoTo->ref_count, 1);
9138 	}
9139 	ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
9140 	ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
9141 	ack_cp->ch.chunk_flags = 0;
9142 	ack_cp->ch.chunk_length = htons(chk->send_size);
9143 	SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
9144 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9145 	chk->asoc->ctrl_queue_cnt++;
9146 	return;
9147 }
9148 
9149 void
9150 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
9151 {
9152 	/* formulate and queue a SHUTDOWN to the sender */
9153 	struct mbuf *m_shutdown;
9154 	struct sctp_shutdown_chunk *shutdown_cp;
9155 	struct sctp_tmit_chunk *chk;
9156 
9157 	m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
9158 	if (m_shutdown == NULL) {
9159 		/* no mbuf's */
9160 		return;
9161 	}
9162 	SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
9163 	sctp_alloc_a_chunk(stcb, chk);
9164 	if (chk == NULL) {
9165 		/* no memory */
9166 		sctp_m_freem(m_shutdown);
9167 		return;
9168 	}
9169 	chk->copy_by_ref = 0;
9170 	chk->send_size = sizeof(struct sctp_shutdown_chunk);
9171 	chk->rec.chunk_id.id = SCTP_SHUTDOWN;
9172 	chk->rec.chunk_id.can_take_data = 1;
9173 	chk->sent = SCTP_DATAGRAM_UNSENT;
9174 	chk->snd_count = 0;
9175 	chk->flags = 0;
9176 	chk->asoc = &stcb->asoc;
9177 	chk->data = m_shutdown;
9178 	chk->whoTo = net;
9179 	if (chk->whoTo) {
9180 		atomic_add_int(&chk->whoTo->ref_count, 1);
9181 	}
9182 	shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
9183 	shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
9184 	shutdown_cp->ch.chunk_flags = 0;
9185 	shutdown_cp->ch.chunk_length = htons(chk->send_size);
9186 	shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
9187 	SCTP_BUF_LEN(m_shutdown) = chk->send_size;
9188 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9189 	chk->asoc->ctrl_queue_cnt++;
9190 	return;
9191 }
9192 
9193 void
9194 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
9195 {
9196 	/*
9197 	 * formulate and queue an ASCONF to the peer. ASCONF parameters
9198 	 * should be queued on the assoc queue.
9199 	 */
9200 	struct sctp_tmit_chunk *chk;
9201 	struct mbuf *m_asconf;
9202 	int len;
9203 
9204 	SCTP_TCB_LOCK_ASSERT(stcb);
9205 
9206 	if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
9207 	    (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
9208 		/* can't send a new one if there is one in flight already */
9209 		return;
9210 	}
9211 	/* compose an ASCONF chunk, maximum length is PMTU */
9212 	m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
9213 	if (m_asconf == NULL) {
9214 		return;
9215 	}
9216 	sctp_alloc_a_chunk(stcb, chk);
9217 	if (chk == NULL) {
9218 		/* no memory */
9219 		sctp_m_freem(m_asconf);
9220 		return;
9221 	}
9222 	chk->copy_by_ref = 0;
9223 	chk->data = m_asconf;
9224 	chk->send_size = len;
9225 	chk->rec.chunk_id.id = SCTP_ASCONF;
9226 	chk->rec.chunk_id.can_take_data = 0;
9227 	chk->sent = SCTP_DATAGRAM_UNSENT;
9228 	chk->snd_count = 0;
9229 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9230 	chk->asoc = &stcb->asoc;
9231 	chk->whoTo = net;
9232 	if (chk->whoTo) {
9233 		atomic_add_int(&chk->whoTo->ref_count, 1);
9234 	}
9235 	TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
9236 	chk->asoc->ctrl_queue_cnt++;
9237 	return;
9238 }
9239 
9240 void
9241 sctp_send_asconf_ack(struct sctp_tcb *stcb)
9242 {
9243 	/*
9244 	 * formulate and queue a asconf-ack back to sender. the asconf-ack
9245 	 * must be stored in the tcb.
9246 	 */
9247 	struct sctp_tmit_chunk *chk;
9248 	struct sctp_asconf_ack *ack, *latest_ack;
9249 	struct mbuf *m_ack;
9250 	struct sctp_nets *net = NULL;
9251 
9252 	SCTP_TCB_LOCK_ASSERT(stcb);
9253 	/* Get the latest ASCONF-ACK */
9254 	latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
9255 	if (latest_ack == NULL) {
9256 		return;
9257 	}
9258 	if (latest_ack->last_sent_to != NULL &&
9259 	    latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
9260 		/* we're doing a retransmission */
9261 		net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
9262 		if (net == NULL) {
9263 			/* no alternate */
9264 			if (stcb->asoc.last_control_chunk_from == NULL) {
9265 				if (stcb->asoc.alternate) {
9266 					net = stcb->asoc.alternate;
9267 				} else {
9268 					net = stcb->asoc.primary_destination;
9269 				}
9270 			} else {
9271 				net = stcb->asoc.last_control_chunk_from;
9272 			}
9273 		}
9274 	} else {
9275 		/* normal case */
9276 		if (stcb->asoc.last_control_chunk_from == NULL) {
9277 			if (stcb->asoc.alternate) {
9278 				net = stcb->asoc.alternate;
9279 			} else {
9280 				net = stcb->asoc.primary_destination;
9281 			}
9282 		} else {
9283 			net = stcb->asoc.last_control_chunk_from;
9284 		}
9285 	}
9286 	latest_ack->last_sent_to = net;
9287 
9288 	TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
9289 		if (ack->data == NULL) {
9290 			continue;
9291 		}
9292 		/* copy the asconf_ack */
9293 		m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
9294 		if (m_ack == NULL) {
9295 			/* couldn't copy it */
9296 			return;
9297 		}
9298 #ifdef SCTP_MBUF_LOGGING
9299 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9300 			struct mbuf *mat;
9301 
9302 			for (mat = m_ack; mat; mat = SCTP_BUF_NEXT(mat)) {
9303 				if (SCTP_BUF_IS_EXTENDED(mat)) {
9304 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
9305 				}
9306 			}
9307 		}
9308 #endif
9309 
9310 		sctp_alloc_a_chunk(stcb, chk);
9311 		if (chk == NULL) {
9312 			/* no memory */
9313 			if (m_ack)
9314 				sctp_m_freem(m_ack);
9315 			return;
9316 		}
9317 		chk->copy_by_ref = 0;
9318 
9319 		chk->whoTo = net;
9320 		if (chk->whoTo) {
9321 			atomic_add_int(&chk->whoTo->ref_count, 1);
9322 		}
9323 		chk->data = m_ack;
9324 		chk->send_size = 0;
9325 		/* Get size */
9326 		chk->send_size = ack->len;
9327 		chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
9328 		chk->rec.chunk_id.can_take_data = 1;
9329 		chk->sent = SCTP_DATAGRAM_UNSENT;
9330 		chk->snd_count = 0;
9331 		chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;	/* XXX */
9332 		chk->asoc = &stcb->asoc;
9333 
9334 		TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9335 		chk->asoc->ctrl_queue_cnt++;
9336 	}
9337 	return;
9338 }
9339 
9340 
9341 static int
9342 sctp_chunk_retransmission(struct sctp_inpcb *inp,
9343     struct sctp_tcb *stcb,
9344     struct sctp_association *asoc,
9345     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
9346 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9347     SCTP_UNUSED
9348 #endif
9349 )
9350 {
9351 	/*-
9352 	 * send out one MTU of retransmission. If fast_retransmit is
9353 	 * happening we ignore the cwnd. Otherwise we obey the cwnd and
9354 	 * rwnd. For a Cookie or Asconf in the control chunk queue we
9355 	 * retransmit them by themselves.
9356 	 *
9357 	 * For data chunks we will pick out the lowest TSN's in the sent_queue
9358 	 * marked for resend and bundle them all together (up to a MTU of
9359 	 * destination). The address to send to should have been
9360 	 * selected/changed where the retransmission was marked (i.e. in FR
9361 	 * or t3-timeout routines).
9362 	 */
9363 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
9364 	struct sctp_tmit_chunk *chk, *fwd;
9365 	struct mbuf *m, *endofchain;
9366 	struct sctp_nets *net = NULL;
9367 	uint32_t tsns_sent = 0;
9368 	int no_fragmentflg, bundle_at, cnt_thru;
9369 	unsigned int mtu;
9370 	int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
9371 	struct sctp_auth_chunk *auth = NULL;
9372 	uint32_t auth_offset = 0;
9373 	uint16_t auth_keyid;
9374 	int override_ok = 1;
9375 	int data_auth_reqd = 0;
9376 	uint32_t dmtu = 0;
9377 
9378 	SCTP_TCB_LOCK_ASSERT(stcb);
9379 	tmr_started = ctl_cnt = bundle_at = error = 0;
9380 	no_fragmentflg = 1;
9381 	fwd_tsn = 0;
9382 	*cnt_out = 0;
9383 	fwd = NULL;
9384 	endofchain = m = NULL;
9385 	auth_keyid = stcb->asoc.authinfo.active_keyid;
9386 #ifdef SCTP_AUDITING_ENABLED
9387 	sctp_audit_log(0xC3, 1);
9388 #endif
9389 	if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
9390 	    (TAILQ_EMPTY(&asoc->control_send_queue))) {
9391 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
9392 		    asoc->sent_queue_retran_cnt);
9393 		asoc->sent_queue_cnt = 0;
9394 		asoc->sent_queue_cnt_removeable = 0;
9395 		/* send back 0/0 so we enter normal transmission */
9396 		*cnt_out = 0;
9397 		return (0);
9398 	}
9399 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9400 		if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
9401 		    (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
9402 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
9403 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
9404 				continue;
9405 			}
9406 			if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
9407 				if (chk != asoc->str_reset) {
9408 					/*
9409 					 * not eligible for retran if its
9410 					 * not ours
9411 					 */
9412 					continue;
9413 				}
9414 			}
9415 			ctl_cnt++;
9416 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9417 				fwd_tsn = 1;
9418 			}
9419 			/*
9420 			 * Add an AUTH chunk, if chunk requires it save the
9421 			 * offset into the chain for AUTH
9422 			 */
9423 			if ((auth == NULL) &&
9424 			    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9425 			    stcb->asoc.peer_auth_chunks))) {
9426 				m = sctp_add_auth_chunk(m, &endofchain,
9427 				    &auth, &auth_offset,
9428 				    stcb,
9429 				    chk->rec.chunk_id.id);
9430 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9431 			}
9432 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9433 			break;
9434 		}
9435 	}
9436 	one_chunk = 0;
9437 	cnt_thru = 0;
9438 	/* do we have control chunks to retransmit? */
9439 	if (m != NULL) {
9440 		/* Start a timer no matter if we suceed or fail */
9441 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9442 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
9443 		} else if (chk->rec.chunk_id.id == SCTP_ASCONF)
9444 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
9445 		chk->snd_count++;	/* update our count */
9446 		if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
9447 		    (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
9448 		    auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9449 		    no_fragmentflg, 0, 0,
9450 		    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9451 		    chk->whoTo->port, so_locked, NULL, NULL))) {
9452 			SCTP_STAT_INCR(sctps_lowlevelerr);
9453 			return (error);
9454 		}
9455 		endofchain = NULL;
9456 		auth = NULL;
9457 		auth_offset = 0;
9458 		/*
9459 		 * We don't want to mark the net->sent time here since this
9460 		 * we use this for HB and retrans cannot measure RTT
9461 		 */
9462 		/* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
9463 		*cnt_out += 1;
9464 		chk->sent = SCTP_DATAGRAM_SENT;
9465 		sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
9466 		if (fwd_tsn == 0) {
9467 			return (0);
9468 		} else {
9469 			/* Clean up the fwd-tsn list */
9470 			sctp_clean_up_ctl(stcb, asoc, so_locked);
9471 			return (0);
9472 		}
9473 	}
9474 	/*
9475 	 * Ok, it is just data retransmission we need to do or that and a
9476 	 * fwd-tsn with it all.
9477 	 */
9478 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
9479 		return (SCTP_RETRAN_DONE);
9480 	}
9481 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
9482 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
9483 		/* not yet open, resend the cookie and that is it */
9484 		return (1);
9485 	}
9486 #ifdef SCTP_AUDITING_ENABLED
9487 	sctp_auditing(20, inp, stcb, NULL);
9488 #endif
9489 	data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9490 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9491 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
9492 			/* No, not sent to this net or not ready for rtx */
9493 			continue;
9494 		}
9495 		if (chk->data == NULL) {
9496 			printf("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
9497 			    chk->rec.data.TSN_seq, chk->snd_count, chk->sent);
9498 			continue;
9499 		}
9500 		if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9501 		    (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9502 			/* Gak, we have exceeded max unlucky retran, abort! */
9503 			SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
9504 			    chk->snd_count,
9505 			    SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
9506 			atomic_add_int(&stcb->asoc.refcnt, 1);
9507 			sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
9508 			SCTP_TCB_LOCK(stcb);
9509 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
9510 			return (SCTP_RETRAN_EXIT);
9511 		}
9512 		/* pick up the net */
9513 		net = chk->whoTo;
9514 		switch (net->ro._l_addr.sa.sa_family) {
9515 #ifdef INET
9516 		case AF_INET:
9517 			mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9518 			break;
9519 #endif
9520 #ifdef INET6
9521 		case AF_INET6:
9522 			mtu = net->mtu - SCTP_MIN_OVERHEAD;
9523 			break;
9524 #endif
9525 		default:
9526 			/* TSNH */
9527 			mtu = net->mtu;
9528 			break;
9529 		}
9530 
9531 		if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9532 			/* No room in peers rwnd */
9533 			uint32_t tsn;
9534 
9535 			tsn = asoc->last_acked_seq + 1;
9536 			if (tsn == chk->rec.data.TSN_seq) {
9537 				/*
9538 				 * we make a special exception for this
9539 				 * case. The peer has no rwnd but is missing
9540 				 * the lowest chunk.. which is probably what
9541 				 * is holding up the rwnd.
9542 				 */
9543 				goto one_chunk_around;
9544 			}
9545 			return (1);
9546 		}
9547 one_chunk_around:
9548 		if (asoc->peers_rwnd < mtu) {
9549 			one_chunk = 1;
9550 			if ((asoc->peers_rwnd == 0) &&
9551 			    (asoc->total_flight == 0)) {
9552 				chk->window_probe = 1;
9553 				chk->whoTo->window_probe = 1;
9554 			}
9555 		}
9556 #ifdef SCTP_AUDITING_ENABLED
9557 		sctp_audit_log(0xC3, 2);
9558 #endif
9559 		bundle_at = 0;
9560 		m = NULL;
9561 		net->fast_retran_ip = 0;
9562 		if (chk->rec.data.doing_fast_retransmit == 0) {
9563 			/*
9564 			 * if no FR in progress skip destination that have
9565 			 * flight_size > cwnd.
9566 			 */
9567 			if (net->flight_size >= net->cwnd) {
9568 				continue;
9569 			}
9570 		} else {
9571 			/*
9572 			 * Mark the destination net to have FR recovery
9573 			 * limits put on it.
9574 			 */
9575 			*fr_done = 1;
9576 			net->fast_retran_ip = 1;
9577 		}
9578 
9579 		/*
9580 		 * if no AUTH is yet included and this chunk requires it,
9581 		 * make sure to account for it.  We don't apply the size
9582 		 * until the AUTH chunk is actually added below in case
9583 		 * there is no room for this chunk.
9584 		 */
9585 		if (data_auth_reqd && (auth == NULL)) {
9586 			dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9587 		} else
9588 			dmtu = 0;
9589 
9590 		if ((chk->send_size <= (mtu - dmtu)) ||
9591 		    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9592 			/* ok we will add this one */
9593 			if (data_auth_reqd) {
9594 				if (auth == NULL) {
9595 					m = sctp_add_auth_chunk(m,
9596 					    &endofchain,
9597 					    &auth,
9598 					    &auth_offset,
9599 					    stcb,
9600 					    SCTP_DATA);
9601 					auth_keyid = chk->auth_keyid;
9602 					override_ok = 0;
9603 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9604 				} else if (override_ok) {
9605 					auth_keyid = chk->auth_keyid;
9606 					override_ok = 0;
9607 				} else if (chk->auth_keyid != auth_keyid) {
9608 					/* different keyid, so done bundling */
9609 					break;
9610 				}
9611 			}
9612 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9613 			if (m == NULL) {
9614 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9615 				return (ENOMEM);
9616 			}
9617 			/* Do clear IP_DF ? */
9618 			if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9619 				no_fragmentflg = 0;
9620 			}
9621 			/* upate our MTU size */
9622 			if (mtu > (chk->send_size + dmtu))
9623 				mtu -= (chk->send_size + dmtu);
9624 			else
9625 				mtu = 0;
9626 			data_list[bundle_at++] = chk;
9627 			if (one_chunk && (asoc->total_flight <= 0)) {
9628 				SCTP_STAT_INCR(sctps_windowprobed);
9629 			}
9630 		}
9631 		if (one_chunk == 0) {
9632 			/*
9633 			 * now are there anymore forward from chk to pick
9634 			 * up?
9635 			 */
9636 			for (fwd = TAILQ_NEXT(chk, sctp_next); fwd != NULL; fwd = TAILQ_NEXT(fwd, sctp_next)) {
9637 				if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9638 					/* Nope, not for retran */
9639 					continue;
9640 				}
9641 				if (fwd->whoTo != net) {
9642 					/* Nope, not the net in question */
9643 					continue;
9644 				}
9645 				if (data_auth_reqd && (auth == NULL)) {
9646 					dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9647 				} else
9648 					dmtu = 0;
9649 				if (fwd->send_size <= (mtu - dmtu)) {
9650 					if (data_auth_reqd) {
9651 						if (auth == NULL) {
9652 							m = sctp_add_auth_chunk(m,
9653 							    &endofchain,
9654 							    &auth,
9655 							    &auth_offset,
9656 							    stcb,
9657 							    SCTP_DATA);
9658 							auth_keyid = fwd->auth_keyid;
9659 							override_ok = 0;
9660 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9661 						} else if (override_ok) {
9662 							auth_keyid = fwd->auth_keyid;
9663 							override_ok = 0;
9664 						} else if (fwd->auth_keyid != auth_keyid) {
9665 							/*
9666 							 * different keyid,
9667 							 * so done bundling
9668 							 */
9669 							break;
9670 						}
9671 					}
9672 					m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9673 					if (m == NULL) {
9674 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9675 						return (ENOMEM);
9676 					}
9677 					/* Do clear IP_DF ? */
9678 					if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9679 						no_fragmentflg = 0;
9680 					}
9681 					/* upate our MTU size */
9682 					if (mtu > (fwd->send_size + dmtu))
9683 						mtu -= (fwd->send_size + dmtu);
9684 					else
9685 						mtu = 0;
9686 					data_list[bundle_at++] = fwd;
9687 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9688 						break;
9689 					}
9690 				} else {
9691 					/* can't fit so we are done */
9692 					break;
9693 				}
9694 			}
9695 		}
9696 		/* Is there something to send for this destination? */
9697 		if (m) {
9698 			/*
9699 			 * No matter if we fail/or suceed we should start a
9700 			 * timer. A failure is like a lost IP packet :-)
9701 			 */
9702 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9703 				/*
9704 				 * no timer running on this destination
9705 				 * restart it.
9706 				 */
9707 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9708 				tmr_started = 1;
9709 			}
9710 			/* Now lets send it, if there is anything to send :> */
9711 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9712 			    (struct sockaddr *)&net->ro._l_addr, m,
9713 			    auth_offset, auth, auth_keyid,
9714 			    no_fragmentflg, 0, 0,
9715 			    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9716 			    net->port, so_locked, NULL, NULL))) {
9717 				/* error, we could not output */
9718 				SCTP_STAT_INCR(sctps_lowlevelerr);
9719 				return (error);
9720 			}
9721 			endofchain = NULL;
9722 			auth = NULL;
9723 			auth_offset = 0;
9724 			/* For HB's */
9725 			/*
9726 			 * We don't want to mark the net->sent time here
9727 			 * since this we use this for HB and retrans cannot
9728 			 * measure RTT
9729 			 */
9730 			/* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9731 
9732 			/* For auto-close */
9733 			cnt_thru++;
9734 			if (*now_filled == 0) {
9735 				(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9736 				*now = asoc->time_last_sent;
9737 				*now_filled = 1;
9738 			} else {
9739 				asoc->time_last_sent = *now;
9740 			}
9741 			*cnt_out += bundle_at;
9742 #ifdef SCTP_AUDITING_ENABLED
9743 			sctp_audit_log(0xC4, bundle_at);
9744 #endif
9745 			if (bundle_at) {
9746 				tsns_sent = data_list[0]->rec.data.TSN_seq;
9747 			}
9748 			for (i = 0; i < bundle_at; i++) {
9749 				SCTP_STAT_INCR(sctps_sendretransdata);
9750 				data_list[i]->sent = SCTP_DATAGRAM_SENT;
9751 				/*
9752 				 * When we have a revoked data, and we
9753 				 * retransmit it, then we clear the revoked
9754 				 * flag since this flag dictates if we
9755 				 * subtracted from the fs
9756 				 */
9757 				if (data_list[i]->rec.data.chunk_was_revoked) {
9758 					/* Deflate the cwnd */
9759 					data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9760 					data_list[i]->rec.data.chunk_was_revoked = 0;
9761 				}
9762 				data_list[i]->snd_count++;
9763 				sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9764 				/* record the time */
9765 				data_list[i]->sent_rcv_time = asoc->time_last_sent;
9766 				if (data_list[i]->book_size_scale) {
9767 					/*
9768 					 * need to double the book size on
9769 					 * this one
9770 					 */
9771 					data_list[i]->book_size_scale = 0;
9772 					/*
9773 					 * Since we double the booksize, we
9774 					 * must also double the output queue
9775 					 * size, since this get shrunk when
9776 					 * we free by this amount.
9777 					 */
9778 					atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9779 					data_list[i]->book_size *= 2;
9780 
9781 
9782 				} else {
9783 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9784 						sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9785 						    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9786 					}
9787 					asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9788 					    (uint32_t) (data_list[i]->send_size +
9789 					    SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9790 				}
9791 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9792 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9793 					    data_list[i]->whoTo->flight_size,
9794 					    data_list[i]->book_size,
9795 					    (uintptr_t) data_list[i]->whoTo,
9796 					    data_list[i]->rec.data.TSN_seq);
9797 				}
9798 				sctp_flight_size_increase(data_list[i]);
9799 				sctp_total_flight_increase(stcb, data_list[i]);
9800 				if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9801 					/* SWS sender side engages */
9802 					asoc->peers_rwnd = 0;
9803 				}
9804 				if ((i == 0) &&
9805 				    (data_list[i]->rec.data.doing_fast_retransmit)) {
9806 					SCTP_STAT_INCR(sctps_sendfastretrans);
9807 					if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9808 					    (tmr_started == 0)) {
9809 						/*-
9810 						 * ok we just fast-retrans'd
9811 						 * the lowest TSN, i.e the
9812 						 * first on the list. In
9813 						 * this case we want to give
9814 						 * some more time to get a
9815 						 * SACK back without a
9816 						 * t3-expiring.
9817 						 */
9818 						sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9819 						    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9820 						sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9821 					}
9822 				}
9823 			}
9824 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9825 				sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9826 			}
9827 #ifdef SCTP_AUDITING_ENABLED
9828 			sctp_auditing(21, inp, stcb, NULL);
9829 #endif
9830 		} else {
9831 			/* None will fit */
9832 			return (1);
9833 		}
9834 		if (asoc->sent_queue_retran_cnt <= 0) {
9835 			/* all done we have no more to retran */
9836 			asoc->sent_queue_retran_cnt = 0;
9837 			break;
9838 		}
9839 		if (one_chunk) {
9840 			/* No more room in rwnd */
9841 			return (1);
9842 		}
9843 		/* stop the for loop here. we sent out a packet */
9844 		break;
9845 	}
9846 	return (0);
9847 }
9848 
9849 static void
9850 sctp_timer_validation(struct sctp_inpcb *inp,
9851     struct sctp_tcb *stcb,
9852     struct sctp_association *asoc)
9853 {
9854 	struct sctp_nets *net;
9855 
9856 	/* Validate that a timer is running somewhere */
9857 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9858 		if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9859 			/* Here is a timer */
9860 			return;
9861 		}
9862 	}
9863 	SCTP_TCB_LOCK_ASSERT(stcb);
9864 	/* Gak, we did not have a timer somewhere */
9865 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9866 	if (asoc->alternate) {
9867 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->alternate);
9868 	} else {
9869 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9870 	}
9871 	return;
9872 }
9873 
9874 void
9875 sctp_chunk_output(struct sctp_inpcb *inp,
9876     struct sctp_tcb *stcb,
9877     int from_where,
9878     int so_locked
9879 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9880     SCTP_UNUSED
9881 #endif
9882 )
9883 {
9884 	/*-
9885 	 * Ok this is the generic chunk service queue. we must do the
9886 	 * following:
9887 	 * - See if there are retransmits pending, if so we must
9888 	 *   do these first.
9889 	 * - Service the stream queue that is next, moving any
9890 	 *   message (note I must get a complete message i.e.
9891 	 *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9892 	 *   TSN's
9893 	 * - Check to see if the cwnd/rwnd allows any output, if so we
9894 	 *   go ahead and fomulate and send the low level chunks. Making sure
9895 	 *   to combine any control in the control chunk queue also.
9896 	 */
9897 	struct sctp_association *asoc;
9898 	struct sctp_nets *net;
9899 	int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0;
9900 	unsigned int burst_cnt = 0;
9901 	struct timeval now;
9902 	int now_filled = 0;
9903 	int nagle_on;
9904 	int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9905 	int un_sent = 0;
9906 	int fr_done;
9907 	unsigned int tot_frs = 0;
9908 
9909 	asoc = &stcb->asoc;
9910 	/* The Nagle algorithm is only applied when handling a send call. */
9911 	if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9912 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9913 			nagle_on = 0;
9914 		} else {
9915 			nagle_on = 1;
9916 		}
9917 	} else {
9918 		nagle_on = 0;
9919 	}
9920 	SCTP_TCB_LOCK_ASSERT(stcb);
9921 
9922 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9923 
9924 	if ((un_sent <= 0) &&
9925 	    (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9926 	    (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
9927 	    (asoc->sent_queue_retran_cnt == 0)) {
9928 		/* Nothing to do unless there is something to be sent left */
9929 		return;
9930 	}
9931 	/*
9932 	 * Do we have something to send, data or control AND a sack timer
9933 	 * running, if so piggy-back the sack.
9934 	 */
9935 	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9936 		sctp_send_sack(stcb, so_locked);
9937 		(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9938 	}
9939 	while (asoc->sent_queue_retran_cnt) {
9940 		/*-
9941 		 * Ok, it is retransmission time only, we send out only ONE
9942 		 * packet with a single call off to the retran code.
9943 		 */
9944 		if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9945 			/*-
9946 			 * Special hook for handling cookiess discarded
9947 			 * by peer that carried data. Send cookie-ack only
9948 			 * and then the next call with get the retran's.
9949 			 */
9950 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9951 			    from_where,
9952 			    &now, &now_filled, frag_point, so_locked);
9953 			return;
9954 		} else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9955 			/* if its not from a HB then do it */
9956 			fr_done = 0;
9957 			ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9958 			if (fr_done) {
9959 				tot_frs++;
9960 			}
9961 		} else {
9962 			/*
9963 			 * its from any other place, we don't allow retran
9964 			 * output (only control)
9965 			 */
9966 			ret = 1;
9967 		}
9968 		if (ret > 0) {
9969 			/* Can't send anymore */
9970 			/*-
9971 			 * now lets push out control by calling med-level
9972 			 * output once. this assures that we WILL send HB's
9973 			 * if queued too.
9974 			 */
9975 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9976 			    from_where,
9977 			    &now, &now_filled, frag_point, so_locked);
9978 #ifdef SCTP_AUDITING_ENABLED
9979 			sctp_auditing(8, inp, stcb, NULL);
9980 #endif
9981 			sctp_timer_validation(inp, stcb, asoc);
9982 			return;
9983 		}
9984 		if (ret < 0) {
9985 			/*-
9986 			 * The count was off.. retran is not happening so do
9987 			 * the normal retransmission.
9988 			 */
9989 #ifdef SCTP_AUDITING_ENABLED
9990 			sctp_auditing(9, inp, stcb, NULL);
9991 #endif
9992 			if (ret == SCTP_RETRAN_EXIT) {
9993 				return;
9994 			}
9995 			break;
9996 		}
9997 		if (from_where == SCTP_OUTPUT_FROM_T3) {
9998 			/* Only one transmission allowed out of a timeout */
9999 #ifdef SCTP_AUDITING_ENABLED
10000 			sctp_auditing(10, inp, stcb, NULL);
10001 #endif
10002 			/* Push out any control */
10003 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
10004 			    &now, &now_filled, frag_point, so_locked);
10005 			return;
10006 		}
10007 		if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) {
10008 			/* Hit FR burst limit */
10009 			return;
10010 		}
10011 		if ((num_out == 0) && (ret == 0)) {
10012 			/* No more retrans to send */
10013 			break;
10014 		}
10015 	}
10016 #ifdef SCTP_AUDITING_ENABLED
10017 	sctp_auditing(12, inp, stcb, NULL);
10018 #endif
10019 	/* Check for bad destinations, if they exist move chunks around. */
10020 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
10021 		if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
10022 			/*-
10023 			 * if possible move things off of this address we
10024 			 * still may send below due to the dormant state but
10025 			 * we try to find an alternate address to send to
10026 			 * and if we have one we move all queued data on the
10027 			 * out wheel to this alternate address.
10028 			 */
10029 			if (net->ref_count > 1)
10030 				sctp_move_chunks_from_net(stcb, net);
10031 		} else {
10032 			/*-
10033 			 * if ((asoc->sat_network) || (net->addr_is_local))
10034 			 * { burst_limit = asoc->max_burst *
10035 			 * SCTP_SAT_NETWORK_BURST_INCR; }
10036 			 */
10037 			if (asoc->max_burst > 0) {
10038 				if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
10039 					if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) {
10040 						/*
10041 						 * JRS - Use the congestion
10042 						 * control given in the
10043 						 * congestion control module
10044 						 */
10045 						asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst);
10046 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10047 							sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED);
10048 						}
10049 						SCTP_STAT_INCR(sctps_maxburstqueued);
10050 					}
10051 					net->fast_retran_ip = 0;
10052 				} else {
10053 					if (net->flight_size == 0) {
10054 						/*
10055 						 * Should be decaying the
10056 						 * cwnd here
10057 						 */
10058 						;
10059 					}
10060 				}
10061 			}
10062 		}
10063 
10064 	}
10065 	burst_cnt = 0;
10066 	do {
10067 		error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
10068 		    &reason_code, 0, from_where,
10069 		    &now, &now_filled, frag_point, so_locked);
10070 		if (error) {
10071 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
10072 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10073 				sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
10074 			}
10075 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10076 				sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
10077 				sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
10078 			}
10079 			break;
10080 		}
10081 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
10082 
10083 		tot_out += num_out;
10084 		burst_cnt++;
10085 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10086 			sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
10087 			if (num_out == 0) {
10088 				sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
10089 			}
10090 		}
10091 		if (nagle_on) {
10092 			/*
10093 			 * When the Nagle algorithm is used, look at how
10094 			 * much is unsent, then if its smaller than an MTU
10095 			 * and we have data in flight we stop, except if we
10096 			 * are handling a fragmented user message.
10097 			 */
10098 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
10099 			    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
10100 			if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
10101 			    (stcb->asoc.total_flight > 0) &&
10102 			    ((stcb->asoc.locked_on_sending == NULL) ||
10103 			    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
10104 				break;
10105 			}
10106 		}
10107 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
10108 		    TAILQ_EMPTY(&asoc->send_queue) &&
10109 		    stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
10110 			/* Nothing left to send */
10111 			break;
10112 		}
10113 		if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
10114 			/* Nothing left to send */
10115 			break;
10116 		}
10117 	} while (num_out &&
10118 	    ((asoc->max_burst == 0) ||
10119 	    SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
10120 	    (burst_cnt < asoc->max_burst)));
10121 
10122 	if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
10123 		if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) {
10124 			SCTP_STAT_INCR(sctps_maxburstqueued);
10125 			asoc->burst_limit_applied = 1;
10126 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10127 				sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
10128 			}
10129 		} else {
10130 			asoc->burst_limit_applied = 0;
10131 		}
10132 	}
10133 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10134 		sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
10135 	}
10136 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
10137 	    tot_out);
10138 
10139 	/*-
10140 	 * Now we need to clean up the control chunk chain if a ECNE is on
10141 	 * it. It must be marked as UNSENT again so next call will continue
10142 	 * to send it until such time that we get a CWR, to remove it.
10143 	 */
10144 	if (stcb->asoc.ecn_echo_cnt_onq)
10145 		sctp_fix_ecn_echo(asoc);
10146 	return;
10147 }
10148 
10149 
10150 int
10151 sctp_output(
10152     struct sctp_inpcb *inp,
10153     struct mbuf *m,
10154     struct sockaddr *addr,
10155     struct mbuf *control,
10156     struct thread *p,
10157     int flags)
10158 {
10159 	if (inp == NULL) {
10160 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10161 		return (EINVAL);
10162 	}
10163 	if (inp->sctp_socket == NULL) {
10164 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10165 		return (EINVAL);
10166 	}
10167 	return (sctp_sosend(inp->sctp_socket,
10168 	    addr,
10169 	    (struct uio *)NULL,
10170 	    m,
10171 	    control,
10172 	    flags, p
10173 	    ));
10174 }
10175 
10176 void
10177 send_forward_tsn(struct sctp_tcb *stcb,
10178     struct sctp_association *asoc)
10179 {
10180 	struct sctp_tmit_chunk *chk;
10181 	struct sctp_forward_tsn_chunk *fwdtsn;
10182 	uint32_t advance_peer_ack_point;
10183 
10184 	SCTP_TCB_LOCK_ASSERT(stcb);
10185 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10186 		if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
10187 			/* mark it to unsent */
10188 			chk->sent = SCTP_DATAGRAM_UNSENT;
10189 			chk->snd_count = 0;
10190 			/* Do we correct its output location? */
10191 			if (chk->whoTo) {
10192 				sctp_free_remote_addr(chk->whoTo);
10193 				chk->whoTo = NULL;
10194 			}
10195 			goto sctp_fill_in_rest;
10196 		}
10197 	}
10198 	/* Ok if we reach here we must build one */
10199 	sctp_alloc_a_chunk(stcb, chk);
10200 	if (chk == NULL) {
10201 		return;
10202 	}
10203 	asoc->fwd_tsn_cnt++;
10204 	chk->copy_by_ref = 0;
10205 	chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
10206 	chk->rec.chunk_id.can_take_data = 0;
10207 	chk->asoc = asoc;
10208 	chk->whoTo = NULL;
10209 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10210 	if (chk->data == NULL) {
10211 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
10212 		return;
10213 	}
10214 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10215 	chk->sent = SCTP_DATAGRAM_UNSENT;
10216 	chk->snd_count = 0;
10217 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
10218 	asoc->ctrl_queue_cnt++;
10219 sctp_fill_in_rest:
10220 	/*-
10221 	 * Here we go through and fill out the part that deals with
10222 	 * stream/seq of the ones we skip.
10223 	 */
10224 	SCTP_BUF_LEN(chk->data) = 0;
10225 	{
10226 		struct sctp_tmit_chunk *at, *tp1, *last;
10227 		struct sctp_strseq *strseq;
10228 		unsigned int cnt_of_space, i, ovh;
10229 		unsigned int space_needed;
10230 		unsigned int cnt_of_skipped = 0;
10231 
10232 		TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
10233 			if (at->sent != SCTP_FORWARD_TSN_SKIP) {
10234 				/* no more to look at */
10235 				break;
10236 			}
10237 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
10238 				/* We don't report these */
10239 				continue;
10240 			}
10241 			cnt_of_skipped++;
10242 		}
10243 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
10244 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
10245 
10246 		cnt_of_space = M_TRAILINGSPACE(chk->data);
10247 
10248 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
10249 			ovh = SCTP_MIN_OVERHEAD;
10250 		} else {
10251 			ovh = SCTP_MIN_V4_OVERHEAD;
10252 		}
10253 		if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
10254 			/* trim to a mtu size */
10255 			cnt_of_space = asoc->smallest_mtu - ovh;
10256 		}
10257 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10258 			sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10259 			    0xff, 0, cnt_of_skipped,
10260 			    asoc->advanced_peer_ack_point);
10261 
10262 		}
10263 		advance_peer_ack_point = asoc->advanced_peer_ack_point;
10264 		if (cnt_of_space < space_needed) {
10265 			/*-
10266 			 * ok we must trim down the chunk by lowering the
10267 			 * advance peer ack point.
10268 			 */
10269 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10270 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10271 				    0xff, 0xff, cnt_of_space,
10272 				    space_needed);
10273 			}
10274 			cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
10275 			cnt_of_skipped /= sizeof(struct sctp_strseq);
10276 			/*-
10277 			 * Go through and find the TSN that will be the one
10278 			 * we report.
10279 			 */
10280 			at = TAILQ_FIRST(&asoc->sent_queue);
10281 			if (at != NULL) {
10282 				for (i = 0; i < cnt_of_skipped; i++) {
10283 					tp1 = TAILQ_NEXT(at, sctp_next);
10284 					if (tp1 == NULL) {
10285 						break;
10286 					}
10287 					at = tp1;
10288 				}
10289 			}
10290 			if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10291 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10292 				    0xff, cnt_of_skipped, at->rec.data.TSN_seq,
10293 				    asoc->advanced_peer_ack_point);
10294 			}
10295 			last = at;
10296 			/*-
10297 			 * last now points to last one I can report, update
10298 			 * peer ack point
10299 			 */
10300 			if (last)
10301 				advance_peer_ack_point = last->rec.data.TSN_seq;
10302 			space_needed = sizeof(struct sctp_forward_tsn_chunk) +
10303 			    cnt_of_skipped * sizeof(struct sctp_strseq);
10304 		}
10305 		chk->send_size = space_needed;
10306 		/* Setup the chunk */
10307 		fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
10308 		fwdtsn->ch.chunk_length = htons(chk->send_size);
10309 		fwdtsn->ch.chunk_flags = 0;
10310 		fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
10311 		fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
10312 		SCTP_BUF_LEN(chk->data) = chk->send_size;
10313 		fwdtsn++;
10314 		/*-
10315 		 * Move pointer to after the fwdtsn and transfer to the
10316 		 * strseq pointer.
10317 		 */
10318 		strseq = (struct sctp_strseq *)fwdtsn;
10319 		/*-
10320 		 * Now populate the strseq list. This is done blindly
10321 		 * without pulling out duplicate stream info. This is
10322 		 * inefficent but won't harm the process since the peer will
10323 		 * look at these in sequence and will thus release anything.
10324 		 * It could mean we exceed the PMTU and chop off some that
10325 		 * we could have included.. but this is unlikely (aka 1432/4
10326 		 * would mean 300+ stream seq's would have to be reported in
10327 		 * one FWD-TSN. With a bit of work we can later FIX this to
10328 		 * optimize and pull out duplcates.. but it does add more
10329 		 * overhead. So for now... not!
10330 		 */
10331 		at = TAILQ_FIRST(&asoc->sent_queue);
10332 		for (i = 0; i < cnt_of_skipped; i++) {
10333 			tp1 = TAILQ_NEXT(at, sctp_next);
10334 			if (tp1 == NULL)
10335 				break;
10336 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
10337 				/* We don't report these */
10338 				i--;
10339 				at = tp1;
10340 				continue;
10341 			}
10342 			if (at->rec.data.TSN_seq == advance_peer_ack_point) {
10343 				at->rec.data.fwd_tsn_cnt = 0;
10344 			}
10345 			strseq->stream = ntohs(at->rec.data.stream_number);
10346 			strseq->sequence = ntohs(at->rec.data.stream_seq);
10347 			strseq++;
10348 			at = tp1;
10349 		}
10350 	}
10351 	return;
10352 }
10353 
10354 void
10355 sctp_send_sack(struct sctp_tcb *stcb, int so_locked
10356 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10357     SCTP_UNUSED
10358 #endif
10359 )
10360 {
10361 	/*-
10362 	 * Queue up a SACK or NR-SACK in the control queue.
10363 	 * We must first check to see if a SACK or NR-SACK is
10364 	 * somehow on the control queue.
10365 	 * If so, we will take and and remove the old one.
10366 	 */
10367 	struct sctp_association *asoc;
10368 	struct sctp_tmit_chunk *chk, *a_chk;
10369 	struct sctp_sack_chunk *sack;
10370 	struct sctp_nr_sack_chunk *nr_sack;
10371 	struct sctp_gap_ack_block *gap_descriptor;
10372 	struct sack_track *selector;
10373 	int mergeable = 0;
10374 	int offset;
10375 	caddr_t limit;
10376 	uint32_t *dup;
10377 	int limit_reached = 0;
10378 	unsigned int i, siz, j;
10379 	unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
10380 	int num_dups = 0;
10381 	int space_req;
10382 	uint32_t highest_tsn;
10383 	uint8_t flags;
10384 	uint8_t type;
10385 	uint8_t tsn_map;
10386 
10387 	if ((stcb->asoc.sctp_nr_sack_on_off == 1) &&
10388 	    (stcb->asoc.peer_supports_nr_sack == 1)) {
10389 		type = SCTP_NR_SELECTIVE_ACK;
10390 	} else {
10391 		type = SCTP_SELECTIVE_ACK;
10392 	}
10393 	a_chk = NULL;
10394 	asoc = &stcb->asoc;
10395 	SCTP_TCB_LOCK_ASSERT(stcb);
10396 	if (asoc->last_data_chunk_from == NULL) {
10397 		/* Hmm we never received anything */
10398 		return;
10399 	}
10400 	sctp_slide_mapping_arrays(stcb);
10401 	sctp_set_rwnd(stcb, asoc);
10402 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10403 		if (chk->rec.chunk_id.id == type) {
10404 			/* Hmm, found a sack already on queue, remove it */
10405 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
10406 			asoc->ctrl_queue_cnt--;
10407 			a_chk = chk;
10408 			if (a_chk->data) {
10409 				sctp_m_freem(a_chk->data);
10410 				a_chk->data = NULL;
10411 			}
10412 			if (a_chk->whoTo) {
10413 				sctp_free_remote_addr(a_chk->whoTo);
10414 				a_chk->whoTo = NULL;
10415 			}
10416 			break;
10417 		}
10418 	}
10419 	if (a_chk == NULL) {
10420 		sctp_alloc_a_chunk(stcb, a_chk);
10421 		if (a_chk == NULL) {
10422 			/* No memory so we drop the idea, and set a timer */
10423 			if (stcb->asoc.delayed_ack) {
10424 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10425 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
10426 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10427 				    stcb->sctp_ep, stcb, NULL);
10428 			} else {
10429 				stcb->asoc.send_sack = 1;
10430 			}
10431 			return;
10432 		}
10433 		a_chk->copy_by_ref = 0;
10434 		a_chk->rec.chunk_id.id = type;
10435 		a_chk->rec.chunk_id.can_take_data = 1;
10436 	}
10437 	/* Clear our pkt counts */
10438 	asoc->data_pkts_seen = 0;
10439 
10440 	a_chk->asoc = asoc;
10441 	a_chk->snd_count = 0;
10442 	a_chk->send_size = 0;	/* fill in later */
10443 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
10444 	a_chk->whoTo = NULL;
10445 
10446 	if ((asoc->numduptsns) ||
10447 	    (!(asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE))) {
10448 		/*-
10449 		 * Ok, we have some duplicates or the destination for the
10450 		 * sack is unreachable, lets see if we can select an
10451 		 * alternate than asoc->last_data_chunk_from
10452 		 */
10453 		if ((asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE) &&
10454 		    (asoc->used_alt_onsack > asoc->numnets)) {
10455 			/* We used an alt last time, don't this time */
10456 			a_chk->whoTo = NULL;
10457 		} else {
10458 			asoc->used_alt_onsack++;
10459 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
10460 		}
10461 		if (a_chk->whoTo == NULL) {
10462 			/* Nope, no alternate */
10463 			a_chk->whoTo = asoc->last_data_chunk_from;
10464 			asoc->used_alt_onsack = 0;
10465 		}
10466 	} else {
10467 		/*
10468 		 * No duplicates so we use the last place we received data
10469 		 * from.
10470 		 */
10471 		asoc->used_alt_onsack = 0;
10472 		a_chk->whoTo = asoc->last_data_chunk_from;
10473 	}
10474 	if (a_chk->whoTo) {
10475 		atomic_add_int(&a_chk->whoTo->ref_count, 1);
10476 	}
10477 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) {
10478 		highest_tsn = asoc->highest_tsn_inside_map;
10479 	} else {
10480 		highest_tsn = asoc->highest_tsn_inside_nr_map;
10481 	}
10482 	if (highest_tsn == asoc->cumulative_tsn) {
10483 		/* no gaps */
10484 		if (type == SCTP_SELECTIVE_ACK) {
10485 			space_req = sizeof(struct sctp_sack_chunk);
10486 		} else {
10487 			space_req = sizeof(struct sctp_nr_sack_chunk);
10488 		}
10489 	} else {
10490 		/* gaps get a cluster */
10491 		space_req = MCLBYTES;
10492 	}
10493 	/* Ok now lets formulate a MBUF with our sack */
10494 	a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
10495 	if ((a_chk->data == NULL) ||
10496 	    (a_chk->whoTo == NULL)) {
10497 		/* rats, no mbuf memory */
10498 		if (a_chk->data) {
10499 			/* was a problem with the destination */
10500 			sctp_m_freem(a_chk->data);
10501 			a_chk->data = NULL;
10502 		}
10503 		sctp_free_a_chunk(stcb, a_chk, so_locked);
10504 		/* sa_ignore NO_NULL_CHK */
10505 		if (stcb->asoc.delayed_ack) {
10506 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10507 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
10508 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10509 			    stcb->sctp_ep, stcb, NULL);
10510 		} else {
10511 			stcb->asoc.send_sack = 1;
10512 		}
10513 		return;
10514 	}
10515 	/* ok, lets go through and fill it in */
10516 	SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10517 	space = M_TRAILINGSPACE(a_chk->data);
10518 	if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10519 		space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10520 	}
10521 	limit = mtod(a_chk->data, caddr_t);
10522 	limit += space;
10523 
10524 	flags = 0;
10525 
10526 	if ((asoc->sctp_cmt_on_off > 0) &&
10527 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10528 		/*-
10529 		 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10530 		 * received, then set high bit to 1, else 0. Reset
10531 		 * pkts_rcvd.
10532 		 */
10533 		flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10534 		asoc->cmt_dac_pkts_rcvd = 0;
10535 	}
10536 #ifdef SCTP_ASOCLOG_OF_TSNS
10537 	stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10538 	stcb->asoc.cumack_log_atsnt++;
10539 	if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10540 		stcb->asoc.cumack_log_atsnt = 0;
10541 	}
10542 #endif
10543 	/* reset the readers interpretation */
10544 	stcb->freed_by_sorcv_sincelast = 0;
10545 
10546 	if (type == SCTP_SELECTIVE_ACK) {
10547 		sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10548 		nr_sack = NULL;
10549 		gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10550 		if (highest_tsn > asoc->mapping_array_base_tsn) {
10551 			siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10552 		} else {
10553 			siz = (((MAX_TSN - highest_tsn) + 1) + highest_tsn + 7) / 8;
10554 		}
10555 	} else {
10556 		sack = NULL;
10557 		nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10558 		gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10559 		if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
10560 			siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10561 		} else {
10562 			siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10563 		}
10564 	}
10565 
10566 	if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10567 		offset = 1;
10568 	} else {
10569 		offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10570 	}
10571 	if (((type == SCTP_SELECTIVE_ACK) &&
10572 	    SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) ||
10573 	    ((type == SCTP_NR_SELECTIVE_ACK) &&
10574 	    SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) {
10575 		/* we have a gap .. maybe */
10576 		for (i = 0; i < siz; i++) {
10577 			tsn_map = asoc->mapping_array[i];
10578 			if (type == SCTP_SELECTIVE_ACK) {
10579 				tsn_map |= asoc->nr_mapping_array[i];
10580 			}
10581 			if (i == 0) {
10582 				/*
10583 				 * Clear all bits corresponding to TSNs
10584 				 * smaller or equal to the cumulative TSN.
10585 				 */
10586 				tsn_map &= (~0 << (1 - offset));
10587 			}
10588 			selector = &sack_array[tsn_map];
10589 			if (mergeable && selector->right_edge) {
10590 				/*
10591 				 * Backup, left and right edges were ok to
10592 				 * merge.
10593 				 */
10594 				num_gap_blocks--;
10595 				gap_descriptor--;
10596 			}
10597 			if (selector->num_entries == 0)
10598 				mergeable = 0;
10599 			else {
10600 				for (j = 0; j < selector->num_entries; j++) {
10601 					if (mergeable && selector->right_edge) {
10602 						/*
10603 						 * do a merge by NOT setting
10604 						 * the left side
10605 						 */
10606 						mergeable = 0;
10607 					} else {
10608 						/*
10609 						 * no merge, set the left
10610 						 * side
10611 						 */
10612 						mergeable = 0;
10613 						gap_descriptor->start = htons((selector->gaps[j].start + offset));
10614 					}
10615 					gap_descriptor->end = htons((selector->gaps[j].end + offset));
10616 					num_gap_blocks++;
10617 					gap_descriptor++;
10618 					if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10619 						/* no more room */
10620 						limit_reached = 1;
10621 						break;
10622 					}
10623 				}
10624 				if (selector->left_edge) {
10625 					mergeable = 1;
10626 				}
10627 			}
10628 			if (limit_reached) {
10629 				/* Reached the limit stop */
10630 				break;
10631 			}
10632 			offset += 8;
10633 		}
10634 	}
10635 	if ((type == SCTP_NR_SELECTIVE_ACK) &&
10636 	    (limit_reached == 0)) {
10637 
10638 		mergeable = 0;
10639 
10640 		if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) {
10641 			siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10642 		} else {
10643 			siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10644 		}
10645 
10646 		if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10647 			offset = 1;
10648 		} else {
10649 			offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10650 		}
10651 		if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) {
10652 			/* we have a gap .. maybe */
10653 			for (i = 0; i < siz; i++) {
10654 				tsn_map = asoc->nr_mapping_array[i];
10655 				if (i == 0) {
10656 					/*
10657 					 * Clear all bits corresponding to
10658 					 * TSNs smaller or equal to the
10659 					 * cumulative TSN.
10660 					 */
10661 					tsn_map &= (~0 << (1 - offset));
10662 				}
10663 				selector = &sack_array[tsn_map];
10664 				if (mergeable && selector->right_edge) {
10665 					/*
10666 					 * Backup, left and right edges were
10667 					 * ok to merge.
10668 					 */
10669 					num_nr_gap_blocks--;
10670 					gap_descriptor--;
10671 				}
10672 				if (selector->num_entries == 0)
10673 					mergeable = 0;
10674 				else {
10675 					for (j = 0; j < selector->num_entries; j++) {
10676 						if (mergeable && selector->right_edge) {
10677 							/*
10678 							 * do a merge by NOT
10679 							 * setting the left
10680 							 * side
10681 							 */
10682 							mergeable = 0;
10683 						} else {
10684 							/*
10685 							 * no merge, set the
10686 							 * left side
10687 							 */
10688 							mergeable = 0;
10689 							gap_descriptor->start = htons((selector->gaps[j].start + offset));
10690 						}
10691 						gap_descriptor->end = htons((selector->gaps[j].end + offset));
10692 						num_nr_gap_blocks++;
10693 						gap_descriptor++;
10694 						if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10695 							/* no more room */
10696 							limit_reached = 1;
10697 							break;
10698 						}
10699 					}
10700 					if (selector->left_edge) {
10701 						mergeable = 1;
10702 					}
10703 				}
10704 				if (limit_reached) {
10705 					/* Reached the limit stop */
10706 					break;
10707 				}
10708 				offset += 8;
10709 			}
10710 		}
10711 	}
10712 	/* now we must add any dups we are going to report. */
10713 	if ((limit_reached == 0) && (asoc->numduptsns)) {
10714 		dup = (uint32_t *) gap_descriptor;
10715 		for (i = 0; i < asoc->numduptsns; i++) {
10716 			*dup = htonl(asoc->dup_tsns[i]);
10717 			dup++;
10718 			num_dups++;
10719 			if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10720 				/* no more room */
10721 				break;
10722 			}
10723 		}
10724 		asoc->numduptsns = 0;
10725 	}
10726 	/*
10727 	 * now that the chunk is prepared queue it to the control chunk
10728 	 * queue.
10729 	 */
10730 	if (type == SCTP_SELECTIVE_ACK) {
10731 		a_chk->send_size = sizeof(struct sctp_sack_chunk) +
10732 		    (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10733 		    num_dups * sizeof(int32_t);
10734 		SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10735 		sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10736 		sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10737 		sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10738 		sack->sack.num_dup_tsns = htons(num_dups);
10739 		sack->ch.chunk_type = type;
10740 		sack->ch.chunk_flags = flags;
10741 		sack->ch.chunk_length = htons(a_chk->send_size);
10742 	} else {
10743 		a_chk->send_size = sizeof(struct sctp_nr_sack_chunk) +
10744 		    (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10745 		    num_dups * sizeof(int32_t);
10746 		SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10747 		nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10748 		nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10749 		nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10750 		nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10751 		nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10752 		nr_sack->nr_sack.reserved = 0;
10753 		nr_sack->ch.chunk_type = type;
10754 		nr_sack->ch.chunk_flags = flags;
10755 		nr_sack->ch.chunk_length = htons(a_chk->send_size);
10756 	}
10757 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10758 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
10759 	asoc->ctrl_queue_cnt++;
10760 	asoc->send_sack = 0;
10761 	SCTP_STAT_INCR(sctps_sendsacks);
10762 	return;
10763 }
10764 
10765 void
10766 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10767 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10768     SCTP_UNUSED
10769 #endif
10770 )
10771 {
10772 	struct mbuf *m_abort;
10773 	struct mbuf *m_out = NULL, *m_end = NULL;
10774 	struct sctp_abort_chunk *abort = NULL;
10775 	int sz;
10776 	uint32_t auth_offset = 0;
10777 	struct sctp_auth_chunk *auth = NULL;
10778 	struct sctp_nets *net;
10779 
10780 	/*-
10781 	 * Add an AUTH chunk, if chunk requires it and save the offset into
10782 	 * the chain for AUTH
10783 	 */
10784 	if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10785 	    stcb->asoc.peer_auth_chunks)) {
10786 		m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10787 		    stcb, SCTP_ABORT_ASSOCIATION);
10788 		SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10789 	}
10790 	SCTP_TCB_LOCK_ASSERT(stcb);
10791 	m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10792 	if (m_abort == NULL) {
10793 		/* no mbuf's */
10794 		if (m_out)
10795 			sctp_m_freem(m_out);
10796 		return;
10797 	}
10798 	/* link in any error */
10799 	SCTP_BUF_NEXT(m_abort) = operr;
10800 	sz = 0;
10801 	if (operr) {
10802 		struct mbuf *n;
10803 
10804 		n = operr;
10805 		while (n) {
10806 			sz += SCTP_BUF_LEN(n);
10807 			n = SCTP_BUF_NEXT(n);
10808 		}
10809 	}
10810 	SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10811 	if (m_out == NULL) {
10812 		/* NO Auth chunk prepended, so reserve space in front */
10813 		SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10814 		m_out = m_abort;
10815 	} else {
10816 		/* Put AUTH chunk at the front of the chain */
10817 		SCTP_BUF_NEXT(m_end) = m_abort;
10818 	}
10819 	if (stcb->asoc.alternate) {
10820 		net = stcb->asoc.alternate;
10821 	} else {
10822 		net = stcb->asoc.primary_destination;
10823 	}
10824 	/* fill in the ABORT chunk */
10825 	abort = mtod(m_abort, struct sctp_abort_chunk *);
10826 	abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10827 	abort->ch.chunk_flags = 0;
10828 	abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10829 
10830 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10831 	    (struct sockaddr *)&net->ro._l_addr,
10832 	    m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, 0,
10833 	    stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10834 	    stcb->asoc.primary_destination->port, so_locked, NULL, NULL);
10835 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10836 }
10837 
10838 void
10839 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10840     struct sctp_nets *net,
10841     int reflect_vtag)
10842 {
10843 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10844 	struct mbuf *m_shutdown_comp;
10845 	struct sctp_shutdown_complete_chunk *shutdown_complete;
10846 	uint32_t vtag;
10847 	uint8_t flags;
10848 
10849 	m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10850 	if (m_shutdown_comp == NULL) {
10851 		/* no mbuf's */
10852 		return;
10853 	}
10854 	if (reflect_vtag) {
10855 		flags = SCTP_HAD_NO_TCB;
10856 		vtag = stcb->asoc.my_vtag;
10857 	} else {
10858 		flags = 0;
10859 		vtag = stcb->asoc.peer_vtag;
10860 	}
10861 	shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10862 	shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10863 	shutdown_complete->ch.chunk_flags = flags;
10864 	shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10865 	SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10866 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10867 	    (struct sockaddr *)&net->ro._l_addr,
10868 	    m_shutdown_comp, 0, NULL, 0, 1, 0, 0,
10869 	    stcb->sctp_ep->sctp_lport, stcb->rport,
10870 	    htonl(vtag),
10871 	    net->port, SCTP_SO_NOT_LOCKED, NULL, NULL);
10872 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10873 	return;
10874 }
10875 
10876 void
10877 sctp_send_shutdown_complete2(struct mbuf *m, struct sctphdr *sh,
10878     uint32_t vrf_id, uint16_t port)
10879 {
10880 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10881 	struct mbuf *o_pak;
10882 	struct mbuf *mout;
10883 	struct ip *iph;
10884 	struct udphdr *udp = NULL;
10885 	int offset_out, len, mlen;
10886 	struct sctp_shutdown_complete_msg *comp_cp;
10887 
10888 #ifdef INET
10889 	struct ip *iph_out;
10890 
10891 #endif
10892 #ifdef INET6
10893 	struct ip6_hdr *ip6, *ip6_out;
10894 
10895 #endif
10896 
10897 	iph = mtod(m, struct ip *);
10898 	switch (iph->ip_v) {
10899 #ifdef INET
10900 	case IPVERSION:
10901 		len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10902 		break;
10903 #endif
10904 #ifdef INET6
10905 	case IPV6_VERSION >> 4:
10906 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10907 		break;
10908 #endif
10909 	default:
10910 		return;
10911 	}
10912 	if (port) {
10913 		len += sizeof(struct udphdr);
10914 	}
10915 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
10916 	if (mout == NULL) {
10917 		return;
10918 	}
10919 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
10920 	SCTP_BUF_LEN(mout) = len;
10921 	SCTP_BUF_NEXT(mout) = NULL;
10922 	if (m->m_flags & M_FLOWID) {
10923 		mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
10924 		mout->m_flags |= M_FLOWID;
10925 	}
10926 #ifdef INET
10927 	iph_out = NULL;
10928 #endif
10929 #ifdef INET6
10930 	ip6_out = NULL;
10931 #endif
10932 	offset_out = 0;
10933 
10934 	switch (iph->ip_v) {
10935 #ifdef INET
10936 	case IPVERSION:
10937 		iph_out = mtod(mout, struct ip *);
10938 
10939 		/* Fill in the IP header for the ABORT */
10940 		iph_out->ip_v = IPVERSION;
10941 		iph_out->ip_hl = (sizeof(struct ip) / 4);
10942 		iph_out->ip_tos = (u_char)0;
10943 		iph_out->ip_id = 0;
10944 		iph_out->ip_off = 0;
10945 		iph_out->ip_ttl = MAXTTL;
10946 		if (port) {
10947 			iph_out->ip_p = IPPROTO_UDP;
10948 		} else {
10949 			iph_out->ip_p = IPPROTO_SCTP;
10950 		}
10951 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10952 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10953 
10954 		/* let IP layer calculate this */
10955 		iph_out->ip_sum = 0;
10956 		offset_out += sizeof(*iph_out);
10957 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10958 		    (caddr_t)iph_out + offset_out);
10959 		break;
10960 #endif
10961 #ifdef INET6
10962 	case IPV6_VERSION >> 4:
10963 		ip6 = (struct ip6_hdr *)iph;
10964 		ip6_out = mtod(mout, struct ip6_hdr *);
10965 
10966 		/* Fill in the IPv6 header for the ABORT */
10967 		ip6_out->ip6_flow = ip6->ip6_flow;
10968 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
10969 		if (port) {
10970 			ip6_out->ip6_nxt = IPPROTO_UDP;
10971 		} else {
10972 			ip6_out->ip6_nxt = IPPROTO_SCTP;
10973 		}
10974 		ip6_out->ip6_src = ip6->ip6_dst;
10975 		ip6_out->ip6_dst = ip6->ip6_src;
10976 		/*
10977 		 * ?? The old code had both the iph len + payload, I think
10978 		 * this is wrong and would never have worked
10979 		 */
10980 		ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10981 		offset_out += sizeof(*ip6_out);
10982 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10983 		    (caddr_t)ip6_out + offset_out);
10984 		break;
10985 #endif				/* INET6 */
10986 	default:
10987 		/* Currently not supported. */
10988 		sctp_m_freem(mout);
10989 		return;
10990 	}
10991 	if (port) {
10992 		if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
10993 			sctp_m_freem(mout);
10994 			return;
10995 		}
10996 		udp = (struct udphdr *)comp_cp;
10997 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10998 		udp->uh_dport = port;
10999 		udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
11000 #ifdef INET
11001 		if (iph_out) {
11002 			if (V_udp_cksum) {
11003 				udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11004 			} else {
11005 				udp->uh_sum = 0;
11006 			}
11007 		}
11008 #endif
11009 		offset_out += sizeof(struct udphdr);
11010 		comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
11011 	}
11012 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11013 		/* no mbuf's */
11014 		sctp_m_freem(mout);
11015 		return;
11016 	}
11017 	/* Now copy in and fill in the ABORT tags etc. */
11018 	comp_cp->sh.src_port = sh->dest_port;
11019 	comp_cp->sh.dest_port = sh->src_port;
11020 	comp_cp->sh.checksum = 0;
11021 	comp_cp->sh.v_tag = sh->v_tag;
11022 	comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
11023 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
11024 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
11025 
11026 #ifdef INET
11027 	if (iph_out != NULL) {
11028 		sctp_route_t ro;
11029 		int ret;
11030 
11031 		mlen = SCTP_BUF_LEN(mout);
11032 		bzero(&ro, sizeof ro);
11033 		/* set IPv4 length */
11034 		iph_out->ip_len = mlen;
11035 #ifdef  SCTP_PACKET_LOGGING
11036 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11037 			sctp_packet_log(mout, mlen);
11038 #endif
11039 		if (port) {
11040 #if defined(SCTP_WITH_NO_CSUM)
11041 			SCTP_STAT_INCR(sctps_sendnocrc);
11042 #else
11043 			comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
11044 			SCTP_STAT_INCR(sctps_sendswcrc);
11045 #endif
11046 			if (V_udp_cksum) {
11047 				SCTP_ENABLE_UDP_CSUM(mout);
11048 			}
11049 		} else {
11050 #if defined(SCTP_WITH_NO_CSUM)
11051 			SCTP_STAT_INCR(sctps_sendnocrc);
11052 #else
11053 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
11054 			mout->m_pkthdr.csum_data = 0;
11055 			SCTP_STAT_INCR(sctps_sendhwcrc);
11056 #endif
11057 		}
11058 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
11059 		/* out it goes */
11060 		SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
11061 
11062 		/* Free the route if we got one back */
11063 		if (ro.ro_rt)
11064 			RTFREE(ro.ro_rt);
11065 	}
11066 #endif
11067 #ifdef INET6
11068 	if (ip6_out != NULL) {
11069 		struct route_in6 ro;
11070 		int ret;
11071 		struct ifnet *ifp = NULL;
11072 
11073 		bzero(&ro, sizeof(ro));
11074 		mlen = SCTP_BUF_LEN(mout);
11075 #ifdef  SCTP_PACKET_LOGGING
11076 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11077 			sctp_packet_log(mout, mlen);
11078 #endif
11079 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
11080 		if (port) {
11081 #if defined(SCTP_WITH_NO_CSUM)
11082 			SCTP_STAT_INCR(sctps_sendnocrc);
11083 #else
11084 			comp_cp->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11085 			SCTP_STAT_INCR(sctps_sendswcrc);
11086 #endif
11087 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), mlen - sizeof(struct ip6_hdr))) == 0) {
11088 				udp->uh_sum = 0xffff;
11089 			}
11090 		} else {
11091 #if defined(SCTP_WITH_NO_CSUM)
11092 			SCTP_STAT_INCR(sctps_sendnocrc);
11093 #else
11094 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
11095 			mout->m_pkthdr.csum_data = 0;
11096 			SCTP_STAT_INCR(sctps_sendhwcrc);
11097 #endif
11098 		}
11099 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
11100 
11101 		/* Free the route if we got one back */
11102 		if (ro.ro_rt)
11103 			RTFREE(ro.ro_rt);
11104 	}
11105 #endif
11106 	SCTP_STAT_INCR(sctps_sendpackets);
11107 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11108 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11109 	return;
11110 
11111 }
11112 
11113 void
11114 sctp_send_hb(struct sctp_tcb *stcb, struct sctp_nets *net, int so_locked
11115 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
11116     SCTP_UNUSED
11117 #endif
11118 )
11119 {
11120 	struct sctp_tmit_chunk *chk;
11121 	struct sctp_heartbeat_chunk *hb;
11122 	struct timeval now;
11123 
11124 	SCTP_TCB_LOCK_ASSERT(stcb);
11125 	if (net == NULL) {
11126 		return;
11127 	}
11128 	(void)SCTP_GETTIME_TIMEVAL(&now);
11129 	switch (net->ro._l_addr.sa.sa_family) {
11130 #ifdef INET
11131 	case AF_INET:
11132 		break;
11133 #endif
11134 #ifdef INET6
11135 	case AF_INET6:
11136 		break;
11137 #endif
11138 	default:
11139 		return;
11140 	}
11141 	sctp_alloc_a_chunk(stcb, chk);
11142 	if (chk == NULL) {
11143 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
11144 		return;
11145 	}
11146 	chk->copy_by_ref = 0;
11147 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
11148 	chk->rec.chunk_id.can_take_data = 1;
11149 	chk->asoc = &stcb->asoc;
11150 	chk->send_size = sizeof(struct sctp_heartbeat_chunk);
11151 
11152 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11153 	if (chk->data == NULL) {
11154 		sctp_free_a_chunk(stcb, chk, so_locked);
11155 		return;
11156 	}
11157 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11158 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11159 	chk->sent = SCTP_DATAGRAM_UNSENT;
11160 	chk->snd_count = 0;
11161 	chk->whoTo = net;
11162 	atomic_add_int(&chk->whoTo->ref_count, 1);
11163 	/* Now we have a mbuf that we can fill in with the details */
11164 	hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
11165 	memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
11166 	/* fill out chunk header */
11167 	hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
11168 	hb->ch.chunk_flags = 0;
11169 	hb->ch.chunk_length = htons(chk->send_size);
11170 	/* Fill out hb parameter */
11171 	hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
11172 	hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
11173 	hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
11174 	hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
11175 	/* Did our user request this one, put it in */
11176 	hb->heartbeat.hb_info.addr_family = net->ro._l_addr.sa.sa_family;
11177 	hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len;
11178 	if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
11179 		/*
11180 		 * we only take from the entropy pool if the address is not
11181 		 * confirmed.
11182 		 */
11183 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11184 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11185 	} else {
11186 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
11187 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
11188 	}
11189 	switch (net->ro._l_addr.sa.sa_family) {
11190 #ifdef INET
11191 	case AF_INET:
11192 		memcpy(hb->heartbeat.hb_info.address,
11193 		    &net->ro._l_addr.sin.sin_addr,
11194 		    sizeof(net->ro._l_addr.sin.sin_addr));
11195 		break;
11196 #endif
11197 #ifdef INET6
11198 	case AF_INET6:
11199 		memcpy(hb->heartbeat.hb_info.address,
11200 		    &net->ro._l_addr.sin6.sin6_addr,
11201 		    sizeof(net->ro._l_addr.sin6.sin6_addr));
11202 		break;
11203 #endif
11204 	default:
11205 		return;
11206 		break;
11207 	}
11208 	net->hb_responded = 0;
11209 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11210 	stcb->asoc.ctrl_queue_cnt++;
11211 	SCTP_STAT_INCR(sctps_sendheartbeat);
11212 	return;
11213 }
11214 
11215 void
11216 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
11217     uint32_t high_tsn)
11218 {
11219 	struct sctp_association *asoc;
11220 	struct sctp_ecne_chunk *ecne;
11221 	struct sctp_tmit_chunk *chk;
11222 
11223 	if (net == NULL) {
11224 		return;
11225 	}
11226 	asoc = &stcb->asoc;
11227 	SCTP_TCB_LOCK_ASSERT(stcb);
11228 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11229 		if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) {
11230 			/* found a previous ECN_ECHO update it if needed */
11231 			uint32_t cnt, ctsn;
11232 
11233 			ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11234 			ctsn = ntohl(ecne->tsn);
11235 			if (SCTP_TSN_GT(high_tsn, ctsn)) {
11236 				ecne->tsn = htonl(high_tsn);
11237 				SCTP_STAT_INCR(sctps_queue_upd_ecne);
11238 			}
11239 			cnt = ntohl(ecne->num_pkts_since_cwr);
11240 			cnt++;
11241 			ecne->num_pkts_since_cwr = htonl(cnt);
11242 			return;
11243 		}
11244 	}
11245 	/* nope could not find one to update so we must build one */
11246 	sctp_alloc_a_chunk(stcb, chk);
11247 	if (chk == NULL) {
11248 		return;
11249 	}
11250 	chk->copy_by_ref = 0;
11251 	SCTP_STAT_INCR(sctps_queue_upd_ecne);
11252 	chk->rec.chunk_id.id = SCTP_ECN_ECHO;
11253 	chk->rec.chunk_id.can_take_data = 0;
11254 	chk->asoc = &stcb->asoc;
11255 	chk->send_size = sizeof(struct sctp_ecne_chunk);
11256 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11257 	if (chk->data == NULL) {
11258 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11259 		return;
11260 	}
11261 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11262 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11263 	chk->sent = SCTP_DATAGRAM_UNSENT;
11264 	chk->snd_count = 0;
11265 	chk->whoTo = net;
11266 	atomic_add_int(&chk->whoTo->ref_count, 1);
11267 
11268 	stcb->asoc.ecn_echo_cnt_onq++;
11269 	ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11270 	ecne->ch.chunk_type = SCTP_ECN_ECHO;
11271 	ecne->ch.chunk_flags = 0;
11272 	ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
11273 	ecne->tsn = htonl(high_tsn);
11274 	ecne->num_pkts_since_cwr = htonl(1);
11275 	TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next);
11276 	asoc->ctrl_queue_cnt++;
11277 }
11278 
11279 void
11280 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
11281     struct mbuf *m, int iphlen, int bad_crc)
11282 {
11283 	struct sctp_association *asoc;
11284 	struct sctp_pktdrop_chunk *drp;
11285 	struct sctp_tmit_chunk *chk;
11286 	uint8_t *datap;
11287 	int len;
11288 	int was_trunc = 0;
11289 	struct ip *iph;
11290 
11291 #ifdef INET6
11292 	struct ip6_hdr *ip6h;
11293 
11294 #endif
11295 	int fullsz = 0, extra = 0;
11296 	long spc;
11297 	int offset;
11298 	struct sctp_chunkhdr *ch, chunk_buf;
11299 	unsigned int chk_length;
11300 
11301 	if (!stcb) {
11302 		return;
11303 	}
11304 	asoc = &stcb->asoc;
11305 	SCTP_TCB_LOCK_ASSERT(stcb);
11306 	if (asoc->peer_supports_pktdrop == 0) {
11307 		/*-
11308 		 * peer must declare support before I send one.
11309 		 */
11310 		return;
11311 	}
11312 	if (stcb->sctp_socket == NULL) {
11313 		return;
11314 	}
11315 	sctp_alloc_a_chunk(stcb, chk);
11316 	if (chk == NULL) {
11317 		return;
11318 	}
11319 	chk->copy_by_ref = 0;
11320 	iph = mtod(m, struct ip *);
11321 	if (iph == NULL) {
11322 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11323 		return;
11324 	}
11325 	switch (iph->ip_v) {
11326 #ifdef INET
11327 	case IPVERSION:
11328 		/* IPv4 */
11329 		len = chk->send_size = iph->ip_len;
11330 		break;
11331 #endif
11332 #ifdef INET6
11333 	case IPV6_VERSION >> 4:
11334 		/* IPv6 */
11335 		ip6h = mtod(m, struct ip6_hdr *);
11336 		len = chk->send_size = htons(ip6h->ip6_plen);
11337 		break;
11338 #endif
11339 	default:
11340 		return;
11341 	}
11342 	/* Validate that we do not have an ABORT in here. */
11343 	offset = iphlen + sizeof(struct sctphdr);
11344 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11345 	    sizeof(*ch), (uint8_t *) & chunk_buf);
11346 	while (ch != NULL) {
11347 		chk_length = ntohs(ch->chunk_length);
11348 		if (chk_length < sizeof(*ch)) {
11349 			/* break to abort land */
11350 			break;
11351 		}
11352 		switch (ch->chunk_type) {
11353 		case SCTP_PACKET_DROPPED:
11354 		case SCTP_ABORT_ASSOCIATION:
11355 		case SCTP_INITIATION_ACK:
11356 			/**
11357 			 * We don't respond with an PKT-DROP to an ABORT
11358 			 * or PKT-DROP. We also do not respond to an
11359 			 * INIT-ACK, because we can't know if the initiation
11360 			 * tag is correct or not.
11361 			 */
11362 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11363 			return;
11364 		default:
11365 			break;
11366 		}
11367 		offset += SCTP_SIZE32(chk_length);
11368 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11369 		    sizeof(*ch), (uint8_t *) & chunk_buf);
11370 	}
11371 
11372 	if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11373 	    min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11374 		/*
11375 		 * only send 1 mtu worth, trim off the excess on the end.
11376 		 */
11377 		fullsz = len - extra;
11378 		len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11379 		was_trunc = 1;
11380 	}
11381 	chk->asoc = &stcb->asoc;
11382 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11383 	if (chk->data == NULL) {
11384 jump_out:
11385 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11386 		return;
11387 	}
11388 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11389 	drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11390 	if (drp == NULL) {
11391 		sctp_m_freem(chk->data);
11392 		chk->data = NULL;
11393 		goto jump_out;
11394 	}
11395 	chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11396 	    sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11397 	chk->book_size_scale = 0;
11398 	if (was_trunc) {
11399 		drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11400 		drp->trunc_len = htons(fullsz);
11401 		/*
11402 		 * Len is already adjusted to size minus overhead above take
11403 		 * out the pkt_drop chunk itself from it.
11404 		 */
11405 		chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
11406 		len = chk->send_size;
11407 	} else {
11408 		/* no truncation needed */
11409 		drp->ch.chunk_flags = 0;
11410 		drp->trunc_len = htons(0);
11411 	}
11412 	if (bad_crc) {
11413 		drp->ch.chunk_flags |= SCTP_BADCRC;
11414 	}
11415 	chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11416 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11417 	chk->sent = SCTP_DATAGRAM_UNSENT;
11418 	chk->snd_count = 0;
11419 	if (net) {
11420 		/* we should hit here */
11421 		chk->whoTo = net;
11422 		atomic_add_int(&chk->whoTo->ref_count, 1);
11423 	} else {
11424 		chk->whoTo = NULL;
11425 	}
11426 	chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11427 	chk->rec.chunk_id.can_take_data = 1;
11428 	drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11429 	drp->ch.chunk_length = htons(chk->send_size);
11430 	spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11431 	if (spc < 0) {
11432 		spc = 0;
11433 	}
11434 	drp->bottle_bw = htonl(spc);
11435 	if (asoc->my_rwnd) {
11436 		drp->current_onq = htonl(asoc->size_on_reasm_queue +
11437 		    asoc->size_on_all_streams +
11438 		    asoc->my_rwnd_control_len +
11439 		    stcb->sctp_socket->so_rcv.sb_cc);
11440 	} else {
11441 		/*-
11442 		 * If my rwnd is 0, possibly from mbuf depletion as well as
11443 		 * space used, tell the peer there is NO space aka onq == bw
11444 		 */
11445 		drp->current_onq = htonl(spc);
11446 	}
11447 	drp->reserved = 0;
11448 	datap = drp->data;
11449 	m_copydata(m, iphlen, len, (caddr_t)datap);
11450 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11451 	asoc->ctrl_queue_cnt++;
11452 }
11453 
11454 void
11455 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override)
11456 {
11457 	struct sctp_association *asoc;
11458 	struct sctp_cwr_chunk *cwr;
11459 	struct sctp_tmit_chunk *chk;
11460 
11461 	asoc = &stcb->asoc;
11462 	SCTP_TCB_LOCK_ASSERT(stcb);
11463 	if (net == NULL) {
11464 		return;
11465 	}
11466 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11467 		if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) {
11468 			/*
11469 			 * found a previous CWR queued to same destination
11470 			 * update it if needed
11471 			 */
11472 			uint32_t ctsn;
11473 
11474 			cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11475 			ctsn = ntohl(cwr->tsn);
11476 			if (SCTP_TSN_GT(high_tsn, ctsn)) {
11477 				cwr->tsn = htonl(high_tsn);
11478 			}
11479 			if (override & SCTP_CWR_REDUCE_OVERRIDE) {
11480 				/* Make sure override is carried */
11481 				cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE;
11482 			}
11483 			return;
11484 		}
11485 	}
11486 	sctp_alloc_a_chunk(stcb, chk);
11487 	if (chk == NULL) {
11488 		return;
11489 	}
11490 	chk->copy_by_ref = 0;
11491 	chk->rec.chunk_id.id = SCTP_ECN_CWR;
11492 	chk->rec.chunk_id.can_take_data = 1;
11493 	chk->asoc = &stcb->asoc;
11494 	chk->send_size = sizeof(struct sctp_cwr_chunk);
11495 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11496 	if (chk->data == NULL) {
11497 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11498 		return;
11499 	}
11500 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11501 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11502 	chk->sent = SCTP_DATAGRAM_UNSENT;
11503 	chk->snd_count = 0;
11504 	chk->whoTo = net;
11505 	atomic_add_int(&chk->whoTo->ref_count, 1);
11506 	cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11507 	cwr->ch.chunk_type = SCTP_ECN_CWR;
11508 	cwr->ch.chunk_flags = override;
11509 	cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11510 	cwr->tsn = htonl(high_tsn);
11511 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11512 	asoc->ctrl_queue_cnt++;
11513 }
11514 
11515 void
11516 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
11517     int number_entries, uint16_t * list,
11518     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11519 {
11520 	int len, old_len, i;
11521 	struct sctp_stream_reset_out_request *req_out;
11522 	struct sctp_chunkhdr *ch;
11523 
11524 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11525 
11526 
11527 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11528 
11529 	/* get to new offset for the param. */
11530 	req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11531 	/* now how long will this param be? */
11532 	len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11533 	req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11534 	req_out->ph.param_length = htons(len);
11535 	req_out->request_seq = htonl(seq);
11536 	req_out->response_seq = htonl(resp_seq);
11537 	req_out->send_reset_at_tsn = htonl(last_sent);
11538 	if (number_entries) {
11539 		for (i = 0; i < number_entries; i++) {
11540 			req_out->list_of_streams[i] = htons(list[i]);
11541 		}
11542 	}
11543 	if (SCTP_SIZE32(len) > len) {
11544 		/*-
11545 		 * Need to worry about the pad we may end up adding to the
11546 		 * end. This is easy since the struct is either aligned to 4
11547 		 * bytes or 2 bytes off.
11548 		 */
11549 		req_out->list_of_streams[number_entries] = 0;
11550 	}
11551 	/* now fix the chunk length */
11552 	ch->chunk_length = htons(len + old_len);
11553 	chk->book_size = len + old_len;
11554 	chk->book_size_scale = 0;
11555 	chk->send_size = SCTP_SIZE32(chk->book_size);
11556 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11557 	return;
11558 }
11559 
11560 
11561 void
11562 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11563     int number_entries, uint16_t * list,
11564     uint32_t seq)
11565 {
11566 	int len, old_len, i;
11567 	struct sctp_stream_reset_in_request *req_in;
11568 	struct sctp_chunkhdr *ch;
11569 
11570 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11571 
11572 
11573 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11574 
11575 	/* get to new offset for the param. */
11576 	req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11577 	/* now how long will this param be? */
11578 	len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11579 	req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11580 	req_in->ph.param_length = htons(len);
11581 	req_in->request_seq = htonl(seq);
11582 	if (number_entries) {
11583 		for (i = 0; i < number_entries; i++) {
11584 			req_in->list_of_streams[i] = htons(list[i]);
11585 		}
11586 	}
11587 	if (SCTP_SIZE32(len) > len) {
11588 		/*-
11589 		 * Need to worry about the pad we may end up adding to the
11590 		 * end. This is easy since the struct is either aligned to 4
11591 		 * bytes or 2 bytes off.
11592 		 */
11593 		req_in->list_of_streams[number_entries] = 0;
11594 	}
11595 	/* now fix the chunk length */
11596 	ch->chunk_length = htons(len + old_len);
11597 	chk->book_size = len + old_len;
11598 	chk->book_size_scale = 0;
11599 	chk->send_size = SCTP_SIZE32(chk->book_size);
11600 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11601 	return;
11602 }
11603 
11604 
11605 void
11606 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11607     uint32_t seq)
11608 {
11609 	int len, old_len;
11610 	struct sctp_stream_reset_tsn_request *req_tsn;
11611 	struct sctp_chunkhdr *ch;
11612 
11613 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11614 
11615 
11616 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11617 
11618 	/* get to new offset for the param. */
11619 	req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11620 	/* now how long will this param be? */
11621 	len = sizeof(struct sctp_stream_reset_tsn_request);
11622 	req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11623 	req_tsn->ph.param_length = htons(len);
11624 	req_tsn->request_seq = htonl(seq);
11625 
11626 	/* now fix the chunk length */
11627 	ch->chunk_length = htons(len + old_len);
11628 	chk->send_size = len + old_len;
11629 	chk->book_size = SCTP_SIZE32(chk->send_size);
11630 	chk->book_size_scale = 0;
11631 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11632 	return;
11633 }
11634 
11635 void
11636 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11637     uint32_t resp_seq, uint32_t result)
11638 {
11639 	int len, old_len;
11640 	struct sctp_stream_reset_response *resp;
11641 	struct sctp_chunkhdr *ch;
11642 
11643 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11644 
11645 
11646 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11647 
11648 	/* get to new offset for the param. */
11649 	resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11650 	/* now how long will this param be? */
11651 	len = sizeof(struct sctp_stream_reset_response);
11652 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11653 	resp->ph.param_length = htons(len);
11654 	resp->response_seq = htonl(resp_seq);
11655 	resp->result = ntohl(result);
11656 
11657 	/* now fix the chunk length */
11658 	ch->chunk_length = htons(len + old_len);
11659 	chk->book_size = len + old_len;
11660 	chk->book_size_scale = 0;
11661 	chk->send_size = SCTP_SIZE32(chk->book_size);
11662 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11663 	return;
11664 
11665 }
11666 
11667 
11668 void
11669 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11670     uint32_t resp_seq, uint32_t result,
11671     uint32_t send_una, uint32_t recv_next)
11672 {
11673 	int len, old_len;
11674 	struct sctp_stream_reset_response_tsn *resp;
11675 	struct sctp_chunkhdr *ch;
11676 
11677 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11678 
11679 
11680 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11681 
11682 	/* get to new offset for the param. */
11683 	resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11684 	/* now how long will this param be? */
11685 	len = sizeof(struct sctp_stream_reset_response_tsn);
11686 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11687 	resp->ph.param_length = htons(len);
11688 	resp->response_seq = htonl(resp_seq);
11689 	resp->result = htonl(result);
11690 	resp->senders_next_tsn = htonl(send_una);
11691 	resp->receivers_next_tsn = htonl(recv_next);
11692 
11693 	/* now fix the chunk length */
11694 	ch->chunk_length = htons(len + old_len);
11695 	chk->book_size = len + old_len;
11696 	chk->send_size = SCTP_SIZE32(chk->book_size);
11697 	chk->book_size_scale = 0;
11698 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11699 	return;
11700 }
11701 
11702 static void
11703 sctp_add_an_out_stream(struct sctp_tmit_chunk *chk,
11704     uint32_t seq,
11705     uint16_t adding)
11706 {
11707 	int len, old_len;
11708 	struct sctp_chunkhdr *ch;
11709 	struct sctp_stream_reset_add_strm *addstr;
11710 
11711 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11712 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11713 
11714 	/* get to new offset for the param. */
11715 	addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11716 	/* now how long will this param be? */
11717 	len = sizeof(struct sctp_stream_reset_add_strm);
11718 
11719 	/* Fill it out. */
11720 	addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_OUT_STREAMS);
11721 	addstr->ph.param_length = htons(len);
11722 	addstr->request_seq = htonl(seq);
11723 	addstr->number_of_streams = htons(adding);
11724 	addstr->reserved = 0;
11725 
11726 	/* now fix the chunk length */
11727 	ch->chunk_length = htons(len + old_len);
11728 	chk->send_size = len + old_len;
11729 	chk->book_size = SCTP_SIZE32(chk->send_size);
11730 	chk->book_size_scale = 0;
11731 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11732 	return;
11733 }
11734 
11735 static void
11736 sctp_add_an_in_stream(struct sctp_tmit_chunk *chk,
11737     uint32_t seq,
11738     uint16_t adding)
11739 {
11740 	int len, old_len;
11741 	struct sctp_chunkhdr *ch;
11742 	struct sctp_stream_reset_add_strm *addstr;
11743 
11744 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11745 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11746 
11747 	/* get to new offset for the param. */
11748 	addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11749 	/* now how long will this param be? */
11750 	len = sizeof(struct sctp_stream_reset_add_strm);
11751 	/* Fill it out. */
11752 	addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_IN_STREAMS);
11753 	addstr->ph.param_length = htons(len);
11754 	addstr->request_seq = htonl(seq);
11755 	addstr->number_of_streams = htons(adding);
11756 	addstr->reserved = 0;
11757 
11758 	/* now fix the chunk length */
11759 	ch->chunk_length = htons(len + old_len);
11760 	chk->send_size = len + old_len;
11761 	chk->book_size = SCTP_SIZE32(chk->send_size);
11762 	chk->book_size_scale = 0;
11763 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11764 	return;
11765 }
11766 
11767 
11768 
11769 int
11770 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11771     int number_entries, uint16_t * list,
11772     uint8_t send_out_req,
11773     uint8_t send_in_req,
11774     uint8_t send_tsn_req,
11775     uint8_t add_stream,
11776     uint16_t adding_o,
11777     uint16_t adding_i, uint8_t peer_asked
11778 )
11779 {
11780 
11781 	struct sctp_association *asoc;
11782 	struct sctp_tmit_chunk *chk;
11783 	struct sctp_chunkhdr *ch;
11784 	uint32_t seq;
11785 
11786 	asoc = &stcb->asoc;
11787 	if (asoc->stream_reset_outstanding) {
11788 		/*-
11789 		 * Already one pending, must get ACK back to clear the flag.
11790 		 */
11791 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11792 		return (EBUSY);
11793 	}
11794 	if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
11795 	    (add_stream == 0)) {
11796 		/* nothing to do */
11797 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11798 		return (EINVAL);
11799 	}
11800 	if (send_tsn_req && (send_out_req || send_in_req)) {
11801 		/* error, can't do that */
11802 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11803 		return (EINVAL);
11804 	}
11805 	sctp_alloc_a_chunk(stcb, chk);
11806 	if (chk == NULL) {
11807 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11808 		return (ENOMEM);
11809 	}
11810 	chk->copy_by_ref = 0;
11811 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11812 	chk->rec.chunk_id.can_take_data = 0;
11813 	chk->asoc = &stcb->asoc;
11814 	chk->book_size = sizeof(struct sctp_chunkhdr);
11815 	chk->send_size = SCTP_SIZE32(chk->book_size);
11816 	chk->book_size_scale = 0;
11817 
11818 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11819 	if (chk->data == NULL) {
11820 		sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
11821 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11822 		return (ENOMEM);
11823 	}
11824 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11825 
11826 	/* setup chunk parameters */
11827 	chk->sent = SCTP_DATAGRAM_UNSENT;
11828 	chk->snd_count = 0;
11829 	if (stcb->asoc.alternate) {
11830 		chk->whoTo = stcb->asoc.alternate;
11831 	} else {
11832 		chk->whoTo = stcb->asoc.primary_destination;
11833 	}
11834 	atomic_add_int(&chk->whoTo->ref_count, 1);
11835 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11836 	ch->chunk_type = SCTP_STREAM_RESET;
11837 	ch->chunk_flags = 0;
11838 	ch->chunk_length = htons(chk->book_size);
11839 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11840 
11841 	seq = stcb->asoc.str_reset_seq_out;
11842 	if (send_out_req) {
11843 		sctp_add_stream_reset_out(chk, number_entries, list,
11844 		    seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1));
11845 		asoc->stream_reset_out_is_outstanding = 1;
11846 		seq++;
11847 		asoc->stream_reset_outstanding++;
11848 	}
11849 	if ((add_stream & 1) &&
11850 	    ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < adding_o)) {
11851 		/* Need to allocate more */
11852 		struct sctp_stream_out *oldstream;
11853 		struct sctp_stream_queue_pending *sp, *nsp;
11854 		int i;
11855 
11856 		oldstream = stcb->asoc.strmout;
11857 		/* get some more */
11858 		SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
11859 		    ((stcb->asoc.streamoutcnt + adding_o) * sizeof(struct sctp_stream_out)),
11860 		    SCTP_M_STRMO);
11861 		if (stcb->asoc.strmout == NULL) {
11862 			uint8_t x;
11863 
11864 			stcb->asoc.strmout = oldstream;
11865 			/* Turn off the bit */
11866 			x = add_stream & 0xfe;
11867 			add_stream = x;
11868 			goto skip_stuff;
11869 		}
11870 		/*
11871 		 * Ok now we proceed with copying the old out stuff and
11872 		 * initializing the new stuff.
11873 		 */
11874 		SCTP_TCB_SEND_LOCK(stcb);
11875 		stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 0, 1);
11876 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11877 			TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
11878 			stcb->asoc.strmout[i].next_sequence_sent = oldstream[i].next_sequence_sent;
11879 			stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
11880 			stcb->asoc.strmout[i].stream_no = i;
11881 			stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], &oldstream[i]);
11882 			/* now anything on those queues? */
11883 			TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) {
11884 				TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
11885 				TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
11886 			}
11887 			/* Now move assoc pointers too */
11888 			if (stcb->asoc.last_out_stream == &oldstream[i]) {
11889 				stcb->asoc.last_out_stream = &stcb->asoc.strmout[i];
11890 			}
11891 			if (stcb->asoc.locked_on_sending == &oldstream[i]) {
11892 				stcb->asoc.locked_on_sending = &stcb->asoc.strmout[i];
11893 			}
11894 		}
11895 		/* now the new streams */
11896 		stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
11897 		for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + adding_o); i++) {
11898 			stcb->asoc.strmout[i].next_sequence_sent = 0x0;
11899 			TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
11900 			stcb->asoc.strmout[i].stream_no = i;
11901 			stcb->asoc.strmout[i].last_msg_incomplete = 0;
11902 			stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], NULL);
11903 		}
11904 		stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + adding_o;
11905 		SCTP_FREE(oldstream, SCTP_M_STRMO);
11906 		SCTP_TCB_SEND_UNLOCK(stcb);
11907 	}
11908 skip_stuff:
11909 	if ((add_stream & 1) && (adding_o > 0)) {
11910 		asoc->strm_pending_add_size = adding_o;
11911 		asoc->peer_req_out = peer_asked;
11912 		sctp_add_an_out_stream(chk, seq, adding_o);
11913 		seq++;
11914 		asoc->stream_reset_outstanding++;
11915 	}
11916 	if ((add_stream & 2) && (adding_i > 0)) {
11917 		sctp_add_an_in_stream(chk, seq, adding_i);
11918 		seq++;
11919 		asoc->stream_reset_outstanding++;
11920 	}
11921 	if (send_in_req) {
11922 		sctp_add_stream_reset_in(chk, number_entries, list, seq);
11923 		seq++;
11924 		asoc->stream_reset_outstanding++;
11925 	}
11926 	if (send_tsn_req) {
11927 		sctp_add_stream_reset_tsn(chk, seq);
11928 		asoc->stream_reset_outstanding++;
11929 	}
11930 	asoc->str_reset = chk;
11931 	/* insert the chunk for sending */
11932 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11933 	    chk,
11934 	    sctp_next);
11935 	asoc->ctrl_queue_cnt++;
11936 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11937 	return (0);
11938 }
11939 
11940 void
11941 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11942     struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11943 {
11944 	/*-
11945 	 * Formulate the abort message, and send it back down.
11946 	 */
11947 	struct mbuf *o_pak;
11948 	struct mbuf *mout;
11949 	struct sctp_abort_msg *abm;
11950 	struct ip *iph;
11951 	struct udphdr *udp;
11952 	int iphlen_out, len;
11953 
11954 #ifdef INET
11955 	struct ip *iph_out;
11956 
11957 #endif
11958 #ifdef INET6
11959 	struct ip6_hdr *ip6, *ip6_out;
11960 
11961 #endif
11962 
11963 	/* don't respond to ABORT with ABORT */
11964 	if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11965 		if (err_cause)
11966 			sctp_m_freem(err_cause);
11967 		return;
11968 	}
11969 	iph = mtod(m, struct ip *);
11970 	switch (iph->ip_v) {
11971 #ifdef INET
11972 	case IPVERSION:
11973 		len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11974 		break;
11975 #endif
11976 #ifdef INET6
11977 	case IPV6_VERSION >> 4:
11978 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11979 		break;
11980 #endif
11981 	default:
11982 		if (err_cause) {
11983 			sctp_m_freem(err_cause);
11984 		}
11985 		return;
11986 	}
11987 	if (port) {
11988 		len += sizeof(struct udphdr);
11989 	}
11990 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11991 	if (mout == NULL) {
11992 		if (err_cause) {
11993 			sctp_m_freem(err_cause);
11994 		}
11995 		return;
11996 	}
11997 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
11998 	SCTP_BUF_LEN(mout) = len;
11999 	SCTP_BUF_NEXT(mout) = err_cause;
12000 	if (m->m_flags & M_FLOWID) {
12001 		mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
12002 		mout->m_flags |= M_FLOWID;
12003 	}
12004 #ifdef INET
12005 	iph_out = NULL;
12006 #endif
12007 #ifdef INET6
12008 	ip6_out = NULL;
12009 #endif
12010 	switch (iph->ip_v) {
12011 #ifdef INET
12012 	case IPVERSION:
12013 		iph_out = mtod(mout, struct ip *);
12014 
12015 		/* Fill in the IP header for the ABORT */
12016 		iph_out->ip_v = IPVERSION;
12017 		iph_out->ip_hl = (sizeof(struct ip) / 4);
12018 		iph_out->ip_tos = (u_char)0;
12019 		iph_out->ip_id = 0;
12020 		iph_out->ip_off = 0;
12021 		iph_out->ip_ttl = MAXTTL;
12022 		if (port) {
12023 			iph_out->ip_p = IPPROTO_UDP;
12024 		} else {
12025 			iph_out->ip_p = IPPROTO_SCTP;
12026 		}
12027 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
12028 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
12029 		/* let IP layer calculate this */
12030 		iph_out->ip_sum = 0;
12031 
12032 		iphlen_out = sizeof(*iph_out);
12033 		abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
12034 		break;
12035 #endif
12036 #ifdef INET6
12037 	case IPV6_VERSION >> 4:
12038 		ip6 = (struct ip6_hdr *)iph;
12039 		ip6_out = mtod(mout, struct ip6_hdr *);
12040 
12041 		/* Fill in the IP6 header for the ABORT */
12042 		ip6_out->ip6_flow = ip6->ip6_flow;
12043 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
12044 		if (port) {
12045 			ip6_out->ip6_nxt = IPPROTO_UDP;
12046 		} else {
12047 			ip6_out->ip6_nxt = IPPROTO_SCTP;
12048 		}
12049 		ip6_out->ip6_src = ip6->ip6_dst;
12050 		ip6_out->ip6_dst = ip6->ip6_src;
12051 
12052 		iphlen_out = sizeof(*ip6_out);
12053 		abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
12054 		break;
12055 #endif				/* INET6 */
12056 	default:
12057 		/* Currently not supported */
12058 		sctp_m_freem(mout);
12059 		return;
12060 	}
12061 
12062 	udp = (struct udphdr *)abm;
12063 	if (port) {
12064 		if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
12065 			sctp_m_freem(mout);
12066 			return;
12067 		}
12068 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
12069 		udp->uh_dport = port;
12070 		/* set udp->uh_ulen later */
12071 		udp->uh_sum = 0;
12072 		iphlen_out += sizeof(struct udphdr);
12073 		abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
12074 	}
12075 	abm->sh.src_port = sh->dest_port;
12076 	abm->sh.dest_port = sh->src_port;
12077 	abm->sh.checksum = 0;
12078 	if (vtag == 0) {
12079 		abm->sh.v_tag = sh->v_tag;
12080 		abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
12081 	} else {
12082 		abm->sh.v_tag = htonl(vtag);
12083 		abm->msg.ch.chunk_flags = 0;
12084 	}
12085 	abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
12086 
12087 	if (err_cause) {
12088 		struct mbuf *m_tmp = err_cause;
12089 		int err_len = 0;
12090 
12091 		/* get length of the err_cause chain */
12092 		while (m_tmp != NULL) {
12093 			err_len += SCTP_BUF_LEN(m_tmp);
12094 			m_tmp = SCTP_BUF_NEXT(m_tmp);
12095 		}
12096 		len = SCTP_BUF_LEN(mout) + err_len;
12097 		if (err_len % 4) {
12098 			/* need pad at end of chunk */
12099 			uint32_t cpthis = 0;
12100 			int padlen;
12101 
12102 			padlen = 4 - (len % 4);
12103 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
12104 			len += padlen;
12105 		}
12106 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
12107 	} else {
12108 		len = SCTP_BUF_LEN(mout);
12109 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
12110 	}
12111 
12112 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12113 		/* no mbuf's */
12114 		sctp_m_freem(mout);
12115 		return;
12116 	}
12117 #ifdef INET
12118 	if (iph_out != NULL) {
12119 		sctp_route_t ro;
12120 		int ret;
12121 
12122 		/* zap the stack pointer to the route */
12123 		bzero(&ro, sizeof ro);
12124 		if (port) {
12125 			udp->uh_ulen = htons(len - sizeof(struct ip));
12126 			if (V_udp_cksum) {
12127 				udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12128 			} else {
12129 				udp->uh_sum = 0;
12130 			}
12131 		}
12132 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
12133 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
12134 		/* set IPv4 length */
12135 		iph_out->ip_len = len;
12136 		/* out it goes */
12137 #ifdef  SCTP_PACKET_LOGGING
12138 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12139 			sctp_packet_log(mout, len);
12140 #endif
12141 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
12142 		if (port) {
12143 #if defined(SCTP_WITH_NO_CSUM)
12144 			SCTP_STAT_INCR(sctps_sendnocrc);
12145 #else
12146 			abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
12147 			SCTP_STAT_INCR(sctps_sendswcrc);
12148 #endif
12149 			if (V_udp_cksum) {
12150 				SCTP_ENABLE_UDP_CSUM(o_pak);
12151 			}
12152 		} else {
12153 #if defined(SCTP_WITH_NO_CSUM)
12154 			SCTP_STAT_INCR(sctps_sendnocrc);
12155 #else
12156 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
12157 			mout->m_pkthdr.csum_data = 0;
12158 			SCTP_STAT_INCR(sctps_sendhwcrc);
12159 #endif
12160 		}
12161 		SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
12162 
12163 		/* Free the route if we got one back */
12164 		if (ro.ro_rt)
12165 			RTFREE(ro.ro_rt);
12166 	}
12167 #endif
12168 #ifdef INET6
12169 	if (ip6_out != NULL) {
12170 		struct route_in6 ro;
12171 		int ret;
12172 		struct ifnet *ifp = NULL;
12173 
12174 		/* zap the stack pointer to the route */
12175 		bzero(&ro, sizeof(ro));
12176 		if (port) {
12177 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
12178 		}
12179 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
12180 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
12181 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
12182 #ifdef  SCTP_PACKET_LOGGING
12183 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12184 			sctp_packet_log(mout, len);
12185 #endif
12186 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
12187 		if (port) {
12188 #if defined(SCTP_WITH_NO_CSUM)
12189 			SCTP_STAT_INCR(sctps_sendnocrc);
12190 #else
12191 			abm->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
12192 			SCTP_STAT_INCR(sctps_sendswcrc);
12193 #endif
12194 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
12195 				udp->uh_sum = 0xffff;
12196 			}
12197 		} else {
12198 #if defined(SCTP_WITH_NO_CSUM)
12199 			SCTP_STAT_INCR(sctps_sendnocrc);
12200 #else
12201 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
12202 			mout->m_pkthdr.csum_data = 0;
12203 			SCTP_STAT_INCR(sctps_sendhwcrc);
12204 #endif
12205 		}
12206 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
12207 
12208 		/* Free the route if we got one back */
12209 		if (ro.ro_rt)
12210 			RTFREE(ro.ro_rt);
12211 	}
12212 #endif
12213 	SCTP_STAT_INCR(sctps_sendpackets);
12214 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12215 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12216 }
12217 
12218 void
12219 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
12220     uint32_t vrf_id, uint16_t port)
12221 {
12222 	struct mbuf *o_pak;
12223 	struct sctphdr *sh, *sh_out;
12224 	struct sctp_chunkhdr *ch;
12225 	struct ip *iph;
12226 	struct udphdr *udp = NULL;
12227 	struct mbuf *mout;
12228 	int iphlen_out, len;
12229 
12230 #ifdef INET
12231 	struct ip *iph_out;
12232 
12233 #endif
12234 #ifdef INET6
12235 	struct ip6_hdr *ip6, *ip6_out;
12236 
12237 #endif
12238 
12239 	iph = mtod(m, struct ip *);
12240 	sh = (struct sctphdr *)((caddr_t)iph + iphlen);
12241 	switch (iph->ip_v) {
12242 #ifdef INET
12243 	case IPVERSION:
12244 		len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
12245 		break;
12246 #endif
12247 #ifdef INET6
12248 	case IPV6_VERSION >> 4:
12249 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
12250 		break;
12251 #endif
12252 	default:
12253 		if (scm) {
12254 			sctp_m_freem(scm);
12255 		}
12256 		return;
12257 	}
12258 	if (port) {
12259 		len += sizeof(struct udphdr);
12260 	}
12261 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
12262 	if (mout == NULL) {
12263 		if (scm) {
12264 			sctp_m_freem(scm);
12265 		}
12266 		return;
12267 	}
12268 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
12269 	SCTP_BUF_LEN(mout) = len;
12270 	SCTP_BUF_NEXT(mout) = scm;
12271 	if (m->m_flags & M_FLOWID) {
12272 		mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
12273 		mout->m_flags |= M_FLOWID;
12274 	}
12275 #ifdef INET
12276 	iph_out = NULL;
12277 #endif
12278 #ifdef INET6
12279 	ip6_out = NULL;
12280 #endif
12281 	switch (iph->ip_v) {
12282 #ifdef INET
12283 	case IPVERSION:
12284 		iph_out = mtod(mout, struct ip *);
12285 
12286 		/* Fill in the IP header for the ABORT */
12287 		iph_out->ip_v = IPVERSION;
12288 		iph_out->ip_hl = (sizeof(struct ip) / 4);
12289 		iph_out->ip_tos = (u_char)0;
12290 		iph_out->ip_id = 0;
12291 		iph_out->ip_off = 0;
12292 		iph_out->ip_ttl = MAXTTL;
12293 		if (port) {
12294 			iph_out->ip_p = IPPROTO_UDP;
12295 		} else {
12296 			iph_out->ip_p = IPPROTO_SCTP;
12297 		}
12298 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
12299 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
12300 		/* let IP layer calculate this */
12301 		iph_out->ip_sum = 0;
12302 
12303 		iphlen_out = sizeof(struct ip);
12304 		sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
12305 		break;
12306 #endif
12307 #ifdef INET6
12308 	case IPV6_VERSION >> 4:
12309 		ip6 = (struct ip6_hdr *)iph;
12310 		ip6_out = mtod(mout, struct ip6_hdr *);
12311 
12312 		/* Fill in the IP6 header for the ABORT */
12313 		ip6_out->ip6_flow = ip6->ip6_flow;
12314 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
12315 		if (port) {
12316 			ip6_out->ip6_nxt = IPPROTO_UDP;
12317 		} else {
12318 			ip6_out->ip6_nxt = IPPROTO_SCTP;
12319 		}
12320 		ip6_out->ip6_src = ip6->ip6_dst;
12321 		ip6_out->ip6_dst = ip6->ip6_src;
12322 
12323 		iphlen_out = sizeof(struct ip6_hdr);
12324 		sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
12325 		break;
12326 #endif				/* INET6 */
12327 	default:
12328 		/* Currently not supported */
12329 		sctp_m_freem(mout);
12330 		return;
12331 	}
12332 
12333 	udp = (struct udphdr *)sh_out;
12334 	if (port) {
12335 		if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
12336 			sctp_m_freem(mout);
12337 			return;
12338 		}
12339 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
12340 		udp->uh_dport = port;
12341 		/* set udp->uh_ulen later */
12342 		udp->uh_sum = 0;
12343 		iphlen_out += sizeof(struct udphdr);
12344 		sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
12345 	}
12346 	sh_out->src_port = sh->dest_port;
12347 	sh_out->dest_port = sh->src_port;
12348 	sh_out->v_tag = vtag;
12349 	sh_out->checksum = 0;
12350 
12351 	ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
12352 	ch->chunk_type = SCTP_OPERATION_ERROR;
12353 	ch->chunk_flags = 0;
12354 
12355 	if (scm) {
12356 		struct mbuf *m_tmp = scm;
12357 		int cause_len = 0;
12358 
12359 		/* get length of the err_cause chain */
12360 		while (m_tmp != NULL) {
12361 			cause_len += SCTP_BUF_LEN(m_tmp);
12362 			m_tmp = SCTP_BUF_NEXT(m_tmp);
12363 		}
12364 		len = SCTP_BUF_LEN(mout) + cause_len;
12365 		if (cause_len % 4) {
12366 			/* need pad at end of chunk */
12367 			uint32_t cpthis = 0;
12368 			int padlen;
12369 
12370 			padlen = 4 - (len % 4);
12371 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
12372 			len += padlen;
12373 		}
12374 		ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
12375 	} else {
12376 		len = SCTP_BUF_LEN(mout);
12377 		ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
12378 	}
12379 
12380 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12381 		/* no mbuf's */
12382 		sctp_m_freem(mout);
12383 		return;
12384 	}
12385 #ifdef INET
12386 	if (iph_out != NULL) {
12387 		sctp_route_t ro;
12388 		int ret;
12389 
12390 		/* zap the stack pointer to the route */
12391 		bzero(&ro, sizeof ro);
12392 		if (port) {
12393 			udp->uh_ulen = htons(len - sizeof(struct ip));
12394 			if (V_udp_cksum) {
12395 				udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12396 			} else {
12397 				udp->uh_sum = 0;
12398 			}
12399 		}
12400 		/* set IPv4 length */
12401 		iph_out->ip_len = len;
12402 		/* out it goes */
12403 #ifdef  SCTP_PACKET_LOGGING
12404 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12405 			sctp_packet_log(mout, len);
12406 #endif
12407 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
12408 		if (port) {
12409 #if defined(SCTP_WITH_NO_CSUM)
12410 			SCTP_STAT_INCR(sctps_sendnocrc);
12411 #else
12412 			sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
12413 			SCTP_STAT_INCR(sctps_sendswcrc);
12414 #endif
12415 			if (V_udp_cksum) {
12416 				SCTP_ENABLE_UDP_CSUM(o_pak);
12417 			}
12418 		} else {
12419 #if defined(SCTP_WITH_NO_CSUM)
12420 			SCTP_STAT_INCR(sctps_sendnocrc);
12421 #else
12422 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
12423 			mout->m_pkthdr.csum_data = 0;
12424 			SCTP_STAT_INCR(sctps_sendhwcrc);
12425 #endif
12426 		}
12427 		SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
12428 
12429 		/* Free the route if we got one back */
12430 		if (ro.ro_rt)
12431 			RTFREE(ro.ro_rt);
12432 	}
12433 #endif
12434 #ifdef INET6
12435 	if (ip6_out != NULL) {
12436 		struct route_in6 ro;
12437 		int ret;
12438 		struct ifnet *ifp = NULL;
12439 
12440 		/* zap the stack pointer to the route */
12441 		bzero(&ro, sizeof(ro));
12442 		if (port) {
12443 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
12444 		}
12445 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
12446 #ifdef  SCTP_PACKET_LOGGING
12447 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12448 			sctp_packet_log(mout, len);
12449 #endif
12450 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
12451 		if (port) {
12452 #if defined(SCTP_WITH_NO_CSUM)
12453 			SCTP_STAT_INCR(sctps_sendnocrc);
12454 #else
12455 			sh_out->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
12456 			SCTP_STAT_INCR(sctps_sendswcrc);
12457 #endif
12458 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
12459 				udp->uh_sum = 0xffff;
12460 			}
12461 		} else {
12462 #if defined(SCTP_WITH_NO_CSUM)
12463 			SCTP_STAT_INCR(sctps_sendnocrc);
12464 #else
12465 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
12466 			mout->m_pkthdr.csum_data = 0;
12467 			SCTP_STAT_INCR(sctps_sendhwcrc);
12468 #endif
12469 		}
12470 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
12471 
12472 		/* Free the route if we got one back */
12473 		if (ro.ro_rt)
12474 			RTFREE(ro.ro_rt);
12475 	}
12476 #endif
12477 	SCTP_STAT_INCR(sctps_sendpackets);
12478 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12479 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12480 }
12481 
12482 static struct mbuf *
12483 sctp_copy_resume(struct uio *uio,
12484     int max_send_len,
12485     int user_marks_eor,
12486     int *error,
12487     uint32_t * sndout,
12488     struct mbuf **new_tail)
12489 {
12490 	struct mbuf *m;
12491 
12492 	m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
12493 	    (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
12494 	if (m == NULL) {
12495 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12496 		*error = ENOMEM;
12497 	} else {
12498 		*sndout = m_length(m, NULL);
12499 		*new_tail = m_last(m);
12500 	}
12501 	return (m);
12502 }
12503 
12504 static int
12505 sctp_copy_one(struct sctp_stream_queue_pending *sp,
12506     struct uio *uio,
12507     int resv_upfront)
12508 {
12509 	int left;
12510 
12511 	left = sp->length;
12512 	sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
12513 	    resv_upfront, 0);
12514 	if (sp->data == NULL) {
12515 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12516 		return (ENOMEM);
12517 	}
12518 	sp->tail_mbuf = m_last(sp->data);
12519 	return (0);
12520 }
12521 
12522 
12523 
12524 static struct sctp_stream_queue_pending *
12525 sctp_copy_it_in(struct sctp_tcb *stcb,
12526     struct sctp_association *asoc,
12527     struct sctp_sndrcvinfo *srcv,
12528     struct uio *uio,
12529     struct sctp_nets *net,
12530     int max_send_len,
12531     int user_marks_eor,
12532     int *error)
12533 {
12534 	/*-
12535 	 * This routine must be very careful in its work. Protocol
12536 	 * processing is up and running so care must be taken to spl...()
12537 	 * when you need to do something that may effect the stcb/asoc. The
12538 	 * sb is locked however. When data is copied the protocol processing
12539 	 * should be enabled since this is a slower operation...
12540 	 */
12541 	struct sctp_stream_queue_pending *sp = NULL;
12542 	int resv_in_first;
12543 
12544 	*error = 0;
12545 	/* Now can we send this? */
12546 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12547 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12548 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12549 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12550 		/* got data while shutting down */
12551 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12552 		*error = ECONNRESET;
12553 		goto out_now;
12554 	}
12555 	sctp_alloc_a_strmoq(stcb, sp);
12556 	if (sp == NULL) {
12557 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12558 		*error = ENOMEM;
12559 		goto out_now;
12560 	}
12561 	sp->act_flags = 0;
12562 	sp->sender_all_done = 0;
12563 	sp->sinfo_flags = srcv->sinfo_flags;
12564 	sp->timetolive = srcv->sinfo_timetolive;
12565 	sp->ppid = srcv->sinfo_ppid;
12566 	sp->context = srcv->sinfo_context;
12567 	sp->strseq = 0;
12568 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12569 
12570 	sp->stream = srcv->sinfo_stream;
12571 	sp->length = min(uio->uio_resid, max_send_len);
12572 	if ((sp->length == (uint32_t) uio->uio_resid) &&
12573 	    ((user_marks_eor == 0) ||
12574 	    (srcv->sinfo_flags & SCTP_EOF) ||
12575 	    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12576 		sp->msg_is_complete = 1;
12577 	} else {
12578 		sp->msg_is_complete = 0;
12579 	}
12580 	sp->sender_all_done = 0;
12581 	sp->some_taken = 0;
12582 	sp->put_last_out = 0;
12583 	resv_in_first = sizeof(struct sctp_data_chunk);
12584 	sp->data = sp->tail_mbuf = NULL;
12585 	if (sp->length == 0) {
12586 		*error = 0;
12587 		goto skip_copy;
12588 	}
12589 	if (srcv->sinfo_keynumber_valid) {
12590 		sp->auth_keyid = srcv->sinfo_keynumber;
12591 	} else {
12592 		sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12593 	}
12594 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12595 		sctp_auth_key_acquire(stcb, sp->auth_keyid);
12596 		sp->holds_key_ref = 1;
12597 	}
12598 	*error = sctp_copy_one(sp, uio, resv_in_first);
12599 skip_copy:
12600 	if (*error) {
12601 		sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
12602 		sp = NULL;
12603 	} else {
12604 		if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12605 			sp->net = net;
12606 			atomic_add_int(&sp->net->ref_count, 1);
12607 		} else {
12608 			sp->net = NULL;
12609 		}
12610 		sctp_set_prsctp_policy(sp);
12611 	}
12612 out_now:
12613 	return (sp);
12614 }
12615 
12616 
12617 int
12618 sctp_sosend(struct socket *so,
12619     struct sockaddr *addr,
12620     struct uio *uio,
12621     struct mbuf *top,
12622     struct mbuf *control,
12623     int flags,
12624     struct thread *p
12625 )
12626 {
12627 	int error, use_sndinfo = 0;
12628 	struct sctp_sndrcvinfo sndrcvninfo;
12629 	struct sockaddr *addr_to_use;
12630 
12631 #if defined(INET) && defined(INET6)
12632 	struct sockaddr_in sin;
12633 
12634 #endif
12635 
12636 	if (control) {
12637 		/* process cmsg snd/rcv info (maybe a assoc-id) */
12638 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&sndrcvninfo, control,
12639 		    sizeof(sndrcvninfo))) {
12640 			/* got one */
12641 			use_sndinfo = 1;
12642 		}
12643 	}
12644 	addr_to_use = addr;
12645 #if defined(INET) && defined(INET6)
12646 	if ((addr) && (addr->sa_family == AF_INET6)) {
12647 		struct sockaddr_in6 *sin6;
12648 
12649 		sin6 = (struct sockaddr_in6 *)addr;
12650 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12651 			in6_sin6_2_sin(&sin, sin6);
12652 			addr_to_use = (struct sockaddr *)&sin;
12653 		}
12654 	}
12655 #endif
12656 	error = sctp_lower_sosend(so, addr_to_use, uio, top,
12657 	    control,
12658 	    flags,
12659 	    use_sndinfo ? &sndrcvninfo : NULL
12660 	    ,p
12661 	    );
12662 	return (error);
12663 }
12664 
12665 
12666 int
12667 sctp_lower_sosend(struct socket *so,
12668     struct sockaddr *addr,
12669     struct uio *uio,
12670     struct mbuf *i_pak,
12671     struct mbuf *control,
12672     int flags,
12673     struct sctp_sndrcvinfo *srcv
12674     ,
12675     struct thread *p
12676 )
12677 {
12678 	unsigned int sndlen = 0, max_len;
12679 	int error, len;
12680 	struct mbuf *top = NULL;
12681 	int queue_only = 0, queue_only_for_init = 0;
12682 	int free_cnt_applied = 0;
12683 	int un_sent;
12684 	int now_filled = 0;
12685 	unsigned int inqueue_bytes = 0;
12686 	struct sctp_block_entry be;
12687 	struct sctp_inpcb *inp;
12688 	struct sctp_tcb *stcb = NULL;
12689 	struct timeval now;
12690 	struct sctp_nets *net;
12691 	struct sctp_association *asoc;
12692 	struct sctp_inpcb *t_inp;
12693 	int user_marks_eor;
12694 	int create_lock_applied = 0;
12695 	int nagle_applies = 0;
12696 	int some_on_control = 0;
12697 	int got_all_of_the_send = 0;
12698 	int hold_tcblock = 0;
12699 	int non_blocking = 0;
12700 	uint32_t local_add_more, local_soresv = 0;
12701 	uint16_t port;
12702 	uint16_t sinfo_flags;
12703 	sctp_assoc_t sinfo_assoc_id;
12704 
12705 	error = 0;
12706 	net = NULL;
12707 	stcb = NULL;
12708 	asoc = NULL;
12709 
12710 	t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12711 	if (inp == NULL) {
12712 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12713 		error = EINVAL;
12714 		if (i_pak) {
12715 			SCTP_RELEASE_PKT(i_pak);
12716 		}
12717 		return (error);
12718 	}
12719 	if ((uio == NULL) && (i_pak == NULL)) {
12720 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12721 		return (EINVAL);
12722 	}
12723 	user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12724 	atomic_add_int(&inp->total_sends, 1);
12725 	if (uio) {
12726 		if (uio->uio_resid < 0) {
12727 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12728 			return (EINVAL);
12729 		}
12730 		sndlen = uio->uio_resid;
12731 	} else {
12732 		top = SCTP_HEADER_TO_CHAIN(i_pak);
12733 		sndlen = SCTP_HEADER_LEN(i_pak);
12734 	}
12735 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
12736 	    addr,
12737 	    sndlen);
12738 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12739 	    (inp->sctp_socket->so_qlimit)) {
12740 		/* The listener can NOT send */
12741 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12742 		error = ENOTCONN;
12743 		goto out_unlocked;
12744 	}
12745 	/**
12746 	 * Pre-screen address, if one is given the sin-len
12747 	 * must be set correctly!
12748 	 */
12749 	if (addr) {
12750 		union sctp_sockstore *raddr = (union sctp_sockstore *)addr;
12751 
12752 		switch (raddr->sa.sa_family) {
12753 #if defined(INET)
12754 		case AF_INET:
12755 			if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) {
12756 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12757 				error = EINVAL;
12758 				goto out_unlocked;
12759 			}
12760 			port = raddr->sin.sin_port;
12761 			break;
12762 #endif
12763 #if defined(INET6)
12764 		case AF_INET6:
12765 			if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) {
12766 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12767 				error = EINVAL;
12768 				goto out_unlocked;
12769 			}
12770 			port = raddr->sin6.sin6_port;
12771 			break;
12772 #endif
12773 		default:
12774 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAFNOSUPPORT);
12775 			error = EAFNOSUPPORT;
12776 			goto out_unlocked;
12777 		}
12778 	} else
12779 		port = 0;
12780 
12781 	if (srcv) {
12782 		sinfo_flags = srcv->sinfo_flags;
12783 		sinfo_assoc_id = srcv->sinfo_assoc_id;
12784 		if (INVALID_SINFO_FLAG(sinfo_flags) ||
12785 		    PR_SCTP_INVALID_POLICY(sinfo_flags)) {
12786 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12787 			error = EINVAL;
12788 			goto out_unlocked;
12789 		}
12790 		if (srcv->sinfo_flags)
12791 			SCTP_STAT_INCR(sctps_sends_with_flags);
12792 	} else {
12793 		sinfo_flags = inp->def_send.sinfo_flags;
12794 		sinfo_assoc_id = inp->def_send.sinfo_assoc_id;
12795 	}
12796 	if (sinfo_flags & SCTP_SENDALL) {
12797 		/* its a sendall */
12798 		error = sctp_sendall(inp, uio, top, srcv);
12799 		top = NULL;
12800 		goto out_unlocked;
12801 	}
12802 	if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) {
12803 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12804 		error = EINVAL;
12805 		goto out_unlocked;
12806 	}
12807 	/* now we must find the assoc */
12808 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12809 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12810 		SCTP_INP_RLOCK(inp);
12811 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
12812 		if (stcb) {
12813 			SCTP_TCB_LOCK(stcb);
12814 			hold_tcblock = 1;
12815 		}
12816 		SCTP_INP_RUNLOCK(inp);
12817 	} else if (sinfo_assoc_id) {
12818 		stcb = sctp_findassociation_ep_asocid(inp, sinfo_assoc_id, 0);
12819 	} else if (addr) {
12820 		/*-
12821 		 * Since we did not use findep we must
12822 		 * increment it, and if we don't find a tcb
12823 		 * decrement it.
12824 		 */
12825 		SCTP_INP_WLOCK(inp);
12826 		SCTP_INP_INCR_REF(inp);
12827 		SCTP_INP_WUNLOCK(inp);
12828 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12829 		if (stcb == NULL) {
12830 			SCTP_INP_WLOCK(inp);
12831 			SCTP_INP_DECR_REF(inp);
12832 			SCTP_INP_WUNLOCK(inp);
12833 		} else {
12834 			hold_tcblock = 1;
12835 		}
12836 	}
12837 	if ((stcb == NULL) && (addr)) {
12838 		/* Possible implicit send? */
12839 		SCTP_ASOC_CREATE_LOCK(inp);
12840 		create_lock_applied = 1;
12841 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12842 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12843 			/* Should I really unlock ? */
12844 			SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12845 			error = EINVAL;
12846 			goto out_unlocked;
12847 
12848 		}
12849 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12850 		    (addr->sa_family == AF_INET6)) {
12851 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12852 			error = EINVAL;
12853 			goto out_unlocked;
12854 		}
12855 		SCTP_INP_WLOCK(inp);
12856 		SCTP_INP_INCR_REF(inp);
12857 		SCTP_INP_WUNLOCK(inp);
12858 		/* With the lock applied look again */
12859 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12860 		if ((stcb == NULL) && (control != NULL) && (port > 0)) {
12861 			stcb = sctp_findassociation_cmsgs(&t_inp, port, control, &net, &error);
12862 		}
12863 		if (stcb == NULL) {
12864 			SCTP_INP_WLOCK(inp);
12865 			SCTP_INP_DECR_REF(inp);
12866 			SCTP_INP_WUNLOCK(inp);
12867 		} else {
12868 			hold_tcblock = 1;
12869 		}
12870 		if (error) {
12871 			goto out_unlocked;
12872 		}
12873 		if (t_inp != inp) {
12874 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12875 			error = ENOTCONN;
12876 			goto out_unlocked;
12877 		}
12878 	}
12879 	if (stcb == NULL) {
12880 		if (addr == NULL) {
12881 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12882 			error = ENOENT;
12883 			goto out_unlocked;
12884 		} else {
12885 			/* We must go ahead and start the INIT process */
12886 			uint32_t vrf_id;
12887 
12888 			if ((sinfo_flags & SCTP_ABORT) ||
12889 			    ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) {
12890 				/*-
12891 				 * User asks to abort a non-existant assoc,
12892 				 * or EOF a non-existant assoc with no data
12893 				 */
12894 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12895 				error = ENOENT;
12896 				goto out_unlocked;
12897 			}
12898 			/* get an asoc/stcb struct */
12899 			vrf_id = inp->def_vrf_id;
12900 #ifdef INVARIANTS
12901 			if (create_lock_applied == 0) {
12902 				panic("Error, should hold create lock and I don't?");
12903 			}
12904 #endif
12905 			stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
12906 			    p
12907 			    );
12908 			if (stcb == NULL) {
12909 				/* Error is setup for us in the call */
12910 				goto out_unlocked;
12911 			}
12912 			if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
12913 				stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
12914 				/*
12915 				 * Set the connected flag so we can queue
12916 				 * data
12917 				 */
12918 				soisconnecting(so);
12919 			}
12920 			hold_tcblock = 1;
12921 			if (create_lock_applied) {
12922 				SCTP_ASOC_CREATE_UNLOCK(inp);
12923 				create_lock_applied = 0;
12924 			} else {
12925 				SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12926 			}
12927 			/*
12928 			 * Turn on queue only flag to prevent data from
12929 			 * being sent
12930 			 */
12931 			queue_only = 1;
12932 			asoc = &stcb->asoc;
12933 			SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12934 			(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12935 
12936 			/* initialize authentication params for the assoc */
12937 			sctp_initialize_auth_params(inp, stcb);
12938 
12939 			if (control) {
12940 				if (sctp_process_cmsgs_for_init(stcb, control, &error)) {
12941 					sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_7);
12942 					hold_tcblock = 0;
12943 					stcb = NULL;
12944 					goto out_unlocked;
12945 				}
12946 			}
12947 			/* out with the INIT */
12948 			queue_only_for_init = 1;
12949 			/*-
12950 			 * we may want to dig in after this call and adjust the MTU
12951 			 * value. It defaulted to 1500 (constant) but the ro
12952 			 * structure may now have an update and thus we may need to
12953 			 * change it BEFORE we append the message.
12954 			 */
12955 		}
12956 	} else
12957 		asoc = &stcb->asoc;
12958 	if (srcv == NULL)
12959 		srcv = (struct sctp_sndrcvinfo *)&asoc->def_send;
12960 	if (srcv->sinfo_flags & SCTP_ADDR_OVER) {
12961 		if (addr)
12962 			net = sctp_findnet(stcb, addr);
12963 		else
12964 			net = NULL;
12965 		if ((net == NULL) ||
12966 		    ((port != 0) && (port != stcb->rport))) {
12967 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12968 			error = EINVAL;
12969 			goto out_unlocked;
12970 		}
12971 	} else {
12972 		if (stcb->asoc.alternate) {
12973 			net = stcb->asoc.alternate;
12974 		} else {
12975 			net = stcb->asoc.primary_destination;
12976 		}
12977 	}
12978 	atomic_add_int(&stcb->total_sends, 1);
12979 	/* Keep the stcb from being freed under our feet */
12980 	atomic_add_int(&asoc->refcnt, 1);
12981 	free_cnt_applied = 1;
12982 
12983 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12984 		if (sndlen > asoc->smallest_mtu) {
12985 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12986 			error = EMSGSIZE;
12987 			goto out_unlocked;
12988 		}
12989 	}
12990 	if (SCTP_SO_IS_NBIO(so)
12991 	    || (flags & MSG_NBIO)
12992 	    ) {
12993 		non_blocking = 1;
12994 	}
12995 	/* would we block? */
12996 	if (non_blocking) {
12997 		if (hold_tcblock == 0) {
12998 			SCTP_TCB_LOCK(stcb);
12999 			hold_tcblock = 1;
13000 		}
13001 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13002 		if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
13003 		    (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13004 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
13005 			if (sndlen > SCTP_SB_LIMIT_SND(so))
13006 				error = EMSGSIZE;
13007 			else
13008 				error = EWOULDBLOCK;
13009 			goto out_unlocked;
13010 		}
13011 		stcb->asoc.sb_send_resv += sndlen;
13012 		SCTP_TCB_UNLOCK(stcb);
13013 		hold_tcblock = 0;
13014 	} else {
13015 		atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
13016 	}
13017 	local_soresv = sndlen;
13018 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13019 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13020 		error = ECONNRESET;
13021 		goto out_unlocked;
13022 	}
13023 	if (create_lock_applied) {
13024 		SCTP_ASOC_CREATE_UNLOCK(inp);
13025 		create_lock_applied = 0;
13026 	}
13027 	if (asoc->stream_reset_outstanding) {
13028 		/*
13029 		 * Can't queue any data while stream reset is underway.
13030 		 */
13031 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
13032 		error = EAGAIN;
13033 		goto out_unlocked;
13034 	}
13035 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
13036 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
13037 		queue_only = 1;
13038 	}
13039 	/* we are now done with all control */
13040 	if (control) {
13041 		sctp_m_freem(control);
13042 		control = NULL;
13043 	}
13044 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
13045 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
13046 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
13047 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
13048 		if (srcv->sinfo_flags & SCTP_ABORT) {
13049 			;
13050 		} else {
13051 			SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13052 			error = ECONNRESET;
13053 			goto out_unlocked;
13054 		}
13055 	}
13056 	/* Ok, we will attempt a msgsnd :> */
13057 	if (p) {
13058 		p->td_ru.ru_msgsnd++;
13059 	}
13060 	/* Are we aborting? */
13061 	if (srcv->sinfo_flags & SCTP_ABORT) {
13062 		struct mbuf *mm;
13063 		int tot_demand, tot_out = 0, max_out;
13064 
13065 		SCTP_STAT_INCR(sctps_sends_with_abort);
13066 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
13067 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
13068 			/* It has to be up before we abort */
13069 			/* how big is the user initiated abort? */
13070 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13071 			error = EINVAL;
13072 			goto out;
13073 		}
13074 		if (hold_tcblock) {
13075 			SCTP_TCB_UNLOCK(stcb);
13076 			hold_tcblock = 0;
13077 		}
13078 		if (top) {
13079 			struct mbuf *cntm = NULL;
13080 
13081 			mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_WAIT, 1, MT_DATA);
13082 			if (sndlen != 0) {
13083 				for (cntm = top; cntm; cntm = SCTP_BUF_NEXT(cntm)) {
13084 					tot_out += SCTP_BUF_LEN(cntm);
13085 				}
13086 			}
13087 		} else {
13088 			/* Must fit in a MTU */
13089 			tot_out = sndlen;
13090 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
13091 			if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
13092 				/* To big */
13093 				SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
13094 				error = EMSGSIZE;
13095 				goto out;
13096 			}
13097 			mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
13098 		}
13099 		if (mm == NULL) {
13100 			SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13101 			error = ENOMEM;
13102 			goto out;
13103 		}
13104 		max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
13105 		max_out -= sizeof(struct sctp_abort_msg);
13106 		if (tot_out > max_out) {
13107 			tot_out = max_out;
13108 		}
13109 		if (mm) {
13110 			struct sctp_paramhdr *ph;
13111 
13112 			/* now move forward the data pointer */
13113 			ph = mtod(mm, struct sctp_paramhdr *);
13114 			ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
13115 			ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
13116 			ph++;
13117 			SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
13118 			if (top == NULL) {
13119 				error = uiomove((caddr_t)ph, (int)tot_out, uio);
13120 				if (error) {
13121 					/*-
13122 					 * Here if we can't get his data we
13123 					 * still abort we just don't get to
13124 					 * send the users note :-0
13125 					 */
13126 					sctp_m_freem(mm);
13127 					mm = NULL;
13128 				}
13129 			} else {
13130 				if (sndlen != 0) {
13131 					SCTP_BUF_NEXT(mm) = top;
13132 				}
13133 			}
13134 		}
13135 		if (hold_tcblock == 0) {
13136 			SCTP_TCB_LOCK(stcb);
13137 		}
13138 		atomic_add_int(&stcb->asoc.refcnt, -1);
13139 		free_cnt_applied = 0;
13140 		/* release this lock, otherwise we hang on ourselves */
13141 		sctp_abort_an_association(stcb->sctp_ep, stcb,
13142 		    SCTP_RESPONSE_TO_USER_REQ,
13143 		    mm, SCTP_SO_LOCKED);
13144 		/* now relock the stcb so everything is sane */
13145 		hold_tcblock = 0;
13146 		stcb = NULL;
13147 		/*
13148 		 * In this case top is already chained to mm avoid double
13149 		 * free, since we free it below if top != NULL and driver
13150 		 * would free it after sending the packet out
13151 		 */
13152 		if (sndlen != 0) {
13153 			top = NULL;
13154 		}
13155 		goto out_unlocked;
13156 	}
13157 	/* Calculate the maximum we can send */
13158 	inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13159 	if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13160 		if (non_blocking) {
13161 			/* we already checked for non-blocking above. */
13162 			max_len = sndlen;
13163 		} else {
13164 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13165 		}
13166 	} else {
13167 		max_len = 0;
13168 	}
13169 	if (hold_tcblock) {
13170 		SCTP_TCB_UNLOCK(stcb);
13171 		hold_tcblock = 0;
13172 	}
13173 	/* Is the stream no. valid? */
13174 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
13175 		/* Invalid stream number */
13176 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13177 		error = EINVAL;
13178 		goto out_unlocked;
13179 	}
13180 	if (asoc->strmout == NULL) {
13181 		/* huh? software error */
13182 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
13183 		error = EFAULT;
13184 		goto out_unlocked;
13185 	}
13186 	/* Unless E_EOR mode is on, we must make a send FIT in one call. */
13187 	if ((user_marks_eor == 0) &&
13188 	    (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
13189 		/* It will NEVER fit */
13190 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
13191 		error = EMSGSIZE;
13192 		goto out_unlocked;
13193 	}
13194 	if ((uio == NULL) && user_marks_eor) {
13195 		/*-
13196 		 * We do not support eeor mode for
13197 		 * sending with mbuf chains (like sendfile).
13198 		 */
13199 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13200 		error = EINVAL;
13201 		goto out_unlocked;
13202 	}
13203 	if (user_marks_eor) {
13204 		local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
13205 	} else {
13206 		/*-
13207 		 * For non-eeor the whole message must fit in
13208 		 * the socket send buffer.
13209 		 */
13210 		local_add_more = sndlen;
13211 	}
13212 	len = 0;
13213 	if (non_blocking) {
13214 		goto skip_preblock;
13215 	}
13216 	if (((max_len <= local_add_more) &&
13217 	    (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
13218 	    (max_len == 0) ||
13219 	    ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13220 		/* No room right now ! */
13221 		SOCKBUF_LOCK(&so->so_snd);
13222 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13223 		while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
13224 		    ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13225 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
13226 			    (unsigned int)SCTP_SB_LIMIT_SND(so),
13227 			    inqueue_bytes,
13228 			    local_add_more,
13229 			    stcb->asoc.stream_queue_cnt,
13230 			    stcb->asoc.chunks_on_out_queue,
13231 			    SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
13232 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13233 				sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, asoc, sndlen);
13234 			}
13235 			be.error = 0;
13236 			stcb->block_entry = &be;
13237 			error = sbwait(&so->so_snd);
13238 			stcb->block_entry = NULL;
13239 			if (error || so->so_error || be.error) {
13240 				if (error == 0) {
13241 					if (so->so_error)
13242 						error = so->so_error;
13243 					if (be.error) {
13244 						error = be.error;
13245 					}
13246 				}
13247 				SOCKBUF_UNLOCK(&so->so_snd);
13248 				goto out_unlocked;
13249 			}
13250 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13251 				sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13252 				    asoc, stcb->asoc.total_output_queue_size);
13253 			}
13254 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13255 				goto out_unlocked;
13256 			}
13257 			inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13258 		}
13259 		if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13260 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13261 		} else {
13262 			max_len = 0;
13263 		}
13264 		SOCKBUF_UNLOCK(&so->so_snd);
13265 	}
13266 skip_preblock:
13267 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13268 		goto out_unlocked;
13269 	}
13270 	/*
13271 	 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
13272 	 * case NOTE: uio will be null when top/mbuf is passed
13273 	 */
13274 	if (sndlen == 0) {
13275 		if (srcv->sinfo_flags & SCTP_EOF) {
13276 			got_all_of_the_send = 1;
13277 			goto dataless_eof;
13278 		} else {
13279 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13280 			error = EINVAL;
13281 			goto out;
13282 		}
13283 	}
13284 	if (top == NULL) {
13285 		struct sctp_stream_queue_pending *sp;
13286 		struct sctp_stream_out *strm;
13287 		uint32_t sndout;
13288 
13289 		SCTP_TCB_SEND_LOCK(stcb);
13290 		if ((asoc->stream_locked) &&
13291 		    (asoc->stream_locked_on != srcv->sinfo_stream)) {
13292 			SCTP_TCB_SEND_UNLOCK(stcb);
13293 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13294 			error = EINVAL;
13295 			goto out;
13296 		}
13297 		SCTP_TCB_SEND_UNLOCK(stcb);
13298 
13299 		strm = &stcb->asoc.strmout[srcv->sinfo_stream];
13300 		if (strm->last_msg_incomplete == 0) {
13301 	do_a_copy_in:
13302 			sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error);
13303 			if ((sp == NULL) || (error)) {
13304 				goto out;
13305 			}
13306 			SCTP_TCB_SEND_LOCK(stcb);
13307 			if (sp->msg_is_complete) {
13308 				strm->last_msg_incomplete = 0;
13309 				asoc->stream_locked = 0;
13310 			} else {
13311 				/*
13312 				 * Just got locked to this guy in case of an
13313 				 * interrupt.
13314 				 */
13315 				strm->last_msg_incomplete = 1;
13316 				asoc->stream_locked = 1;
13317 				asoc->stream_locked_on = srcv->sinfo_stream;
13318 				sp->sender_all_done = 0;
13319 			}
13320 			sctp_snd_sb_alloc(stcb, sp->length);
13321 			atomic_add_int(&asoc->stream_queue_cnt, 1);
13322 			if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
13323 				sp->strseq = strm->next_sequence_sent;
13324 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
13325 					sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
13326 					    (uintptr_t) stcb, sp->length,
13327 					    (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
13328 				}
13329 				strm->next_sequence_sent++;
13330 			} else {
13331 				SCTP_STAT_INCR(sctps_sends_with_unord);
13332 			}
13333 			TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
13334 			stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp, 1);
13335 			SCTP_TCB_SEND_UNLOCK(stcb);
13336 		} else {
13337 			SCTP_TCB_SEND_LOCK(stcb);
13338 			sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
13339 			SCTP_TCB_SEND_UNLOCK(stcb);
13340 			if (sp == NULL) {
13341 				/* ???? Huh ??? last msg is gone */
13342 #ifdef INVARIANTS
13343 				panic("Warning: Last msg marked incomplete, yet nothing left?");
13344 #else
13345 				SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
13346 				strm->last_msg_incomplete = 0;
13347 #endif
13348 				goto do_a_copy_in;
13349 
13350 			}
13351 		}
13352 		while (uio->uio_resid > 0) {
13353 			/* How much room do we have? */
13354 			struct mbuf *new_tail, *mm;
13355 
13356 			if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13357 				max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
13358 			else
13359 				max_len = 0;
13360 
13361 			if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
13362 			    (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
13363 			    (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
13364 				sndout = 0;
13365 				new_tail = NULL;
13366 				if (hold_tcblock) {
13367 					SCTP_TCB_UNLOCK(stcb);
13368 					hold_tcblock = 0;
13369 				}
13370 				mm = sctp_copy_resume(uio, max_len, user_marks_eor, &error, &sndout, &new_tail);
13371 				if ((mm == NULL) || error) {
13372 					if (mm) {
13373 						sctp_m_freem(mm);
13374 					}
13375 					goto out;
13376 				}
13377 				/* Update the mbuf and count */
13378 				SCTP_TCB_SEND_LOCK(stcb);
13379 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13380 					/*
13381 					 * we need to get out. Peer probably
13382 					 * aborted.
13383 					 */
13384 					sctp_m_freem(mm);
13385 					if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
13386 						SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13387 						error = ECONNRESET;
13388 					}
13389 					SCTP_TCB_SEND_UNLOCK(stcb);
13390 					goto out;
13391 				}
13392 				if (sp->tail_mbuf) {
13393 					/* tack it to the end */
13394 					SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13395 					sp->tail_mbuf = new_tail;
13396 				} else {
13397 					/* A stolen mbuf */
13398 					sp->data = mm;
13399 					sp->tail_mbuf = new_tail;
13400 				}
13401 				sctp_snd_sb_alloc(stcb, sndout);
13402 				atomic_add_int(&sp->length, sndout);
13403 				len += sndout;
13404 
13405 				/* Did we reach EOR? */
13406 				if ((uio->uio_resid == 0) &&
13407 				    ((user_marks_eor == 0) ||
13408 				    (srcv->sinfo_flags & SCTP_EOF) ||
13409 				    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
13410 					sp->msg_is_complete = 1;
13411 				} else {
13412 					sp->msg_is_complete = 0;
13413 				}
13414 				SCTP_TCB_SEND_UNLOCK(stcb);
13415 			}
13416 			if (uio->uio_resid == 0) {
13417 				/* got it all? */
13418 				continue;
13419 			}
13420 			/* PR-SCTP? */
13421 			if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
13422 				/*
13423 				 * This is ugly but we must assure locking
13424 				 * order
13425 				 */
13426 				if (hold_tcblock == 0) {
13427 					SCTP_TCB_LOCK(stcb);
13428 					hold_tcblock = 1;
13429 				}
13430 				sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
13431 				inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13432 				if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13433 					max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13434 				else
13435 					max_len = 0;
13436 				if (max_len > 0) {
13437 					continue;
13438 				}
13439 				SCTP_TCB_UNLOCK(stcb);
13440 				hold_tcblock = 0;
13441 			}
13442 			/* wait for space now */
13443 			if (non_blocking) {
13444 				/* Non-blocking io in place out */
13445 				goto skip_out_eof;
13446 			}
13447 			/* What about the INIT, send it maybe */
13448 			if (queue_only_for_init) {
13449 				if (hold_tcblock == 0) {
13450 					SCTP_TCB_LOCK(stcb);
13451 					hold_tcblock = 1;
13452 				}
13453 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13454 					/* a collision took us forward? */
13455 					queue_only = 0;
13456 				} else {
13457 					sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13458 					SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
13459 					queue_only = 1;
13460 				}
13461 			}
13462 			if ((net->flight_size > net->cwnd) &&
13463 			    (asoc->sctp_cmt_on_off == 0)) {
13464 				SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13465 				queue_only = 1;
13466 			} else if (asoc->ifp_had_enobuf) {
13467 				SCTP_STAT_INCR(sctps_ifnomemqueued);
13468 				if (net->flight_size > (2 * net->mtu)) {
13469 					queue_only = 1;
13470 				}
13471 				asoc->ifp_had_enobuf = 0;
13472 			}
13473 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13474 			    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13475 			if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13476 			    (stcb->asoc.total_flight > 0) &&
13477 			    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13478 			    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13479 
13480 				/*-
13481 				 * Ok, Nagle is set on and we have data outstanding.
13482 				 * Don't send anything and let SACKs drive out the
13483 				 * data unless wen have a "full" segment to send.
13484 				 */
13485 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13486 					sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13487 				}
13488 				SCTP_STAT_INCR(sctps_naglequeued);
13489 				nagle_applies = 1;
13490 			} else {
13491 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13492 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13493 						sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13494 				}
13495 				SCTP_STAT_INCR(sctps_naglesent);
13496 				nagle_applies = 0;
13497 			}
13498 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13499 
13500 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13501 				    nagle_applies, un_sent);
13502 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13503 				    stcb->asoc.total_flight,
13504 				    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13505 			}
13506 			if (queue_only_for_init)
13507 				queue_only_for_init = 0;
13508 			if ((queue_only == 0) && (nagle_applies == 0)) {
13509 				/*-
13510 				 * need to start chunk output
13511 				 * before blocking.. note that if
13512 				 * a lock is already applied, then
13513 				 * the input via the net is happening
13514 				 * and I don't need to start output :-D
13515 				 */
13516 				if (hold_tcblock == 0) {
13517 					if (SCTP_TCB_TRYLOCK(stcb)) {
13518 						hold_tcblock = 1;
13519 						sctp_chunk_output(inp,
13520 						    stcb,
13521 						    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13522 					}
13523 				} else {
13524 					sctp_chunk_output(inp,
13525 					    stcb,
13526 					    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13527 				}
13528 				if (hold_tcblock == 1) {
13529 					SCTP_TCB_UNLOCK(stcb);
13530 					hold_tcblock = 0;
13531 				}
13532 			}
13533 			SOCKBUF_LOCK(&so->so_snd);
13534 			/*-
13535 			 * This is a bit strange, but I think it will
13536 			 * work. The total_output_queue_size is locked and
13537 			 * protected by the TCB_LOCK, which we just released.
13538 			 * There is a race that can occur between releasing it
13539 			 * above, and me getting the socket lock, where sacks
13540 			 * come in but we have not put the SB_WAIT on the
13541 			 * so_snd buffer to get the wakeup. After the LOCK
13542 			 * is applied the sack_processing will also need to
13543 			 * LOCK the so->so_snd to do the actual sowwakeup(). So
13544 			 * once we have the socket buffer lock if we recheck the
13545 			 * size we KNOW we will get to sleep safely with the
13546 			 * wakeup flag in place.
13547 			 */
13548 			if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
13549 			    min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13550 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13551 					sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13552 					    asoc, uio->uio_resid);
13553 				}
13554 				be.error = 0;
13555 				stcb->block_entry = &be;
13556 				error = sbwait(&so->so_snd);
13557 				stcb->block_entry = NULL;
13558 
13559 				if (error || so->so_error || be.error) {
13560 					if (error == 0) {
13561 						if (so->so_error)
13562 							error = so->so_error;
13563 						if (be.error) {
13564 							error = be.error;
13565 						}
13566 					}
13567 					SOCKBUF_UNLOCK(&so->so_snd);
13568 					goto out_unlocked;
13569 				}
13570 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13571 					sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13572 					    asoc, stcb->asoc.total_output_queue_size);
13573 				}
13574 			}
13575 			SOCKBUF_UNLOCK(&so->so_snd);
13576 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13577 				goto out_unlocked;
13578 			}
13579 		}
13580 		SCTP_TCB_SEND_LOCK(stcb);
13581 		if (sp) {
13582 			if (sp->msg_is_complete == 0) {
13583 				strm->last_msg_incomplete = 1;
13584 				asoc->stream_locked = 1;
13585 				asoc->stream_locked_on = srcv->sinfo_stream;
13586 			} else {
13587 				sp->sender_all_done = 1;
13588 				strm->last_msg_incomplete = 0;
13589 				asoc->stream_locked = 0;
13590 			}
13591 		} else {
13592 			SCTP_PRINTF("Huh no sp TSNH?\n");
13593 			strm->last_msg_incomplete = 0;
13594 			asoc->stream_locked = 0;
13595 		}
13596 		SCTP_TCB_SEND_UNLOCK(stcb);
13597 		if (uio->uio_resid == 0) {
13598 			got_all_of_the_send = 1;
13599 		}
13600 	} else {
13601 		/* We send in a 0, since we do NOT have any locks */
13602 		error = sctp_msg_append(stcb, net, top, srcv, 0);
13603 		top = NULL;
13604 		if (srcv->sinfo_flags & SCTP_EOF) {
13605 			/*
13606 			 * This should only happen for Panda for the mbuf
13607 			 * send case, which does NOT yet support EEOR mode.
13608 			 * Thus, we can just set this flag to do the proper
13609 			 * EOF handling.
13610 			 */
13611 			got_all_of_the_send = 1;
13612 		}
13613 	}
13614 	if (error) {
13615 		goto out;
13616 	}
13617 dataless_eof:
13618 	/* EOF thing ? */
13619 	if ((srcv->sinfo_flags & SCTP_EOF) &&
13620 	    (got_all_of_the_send == 1) &&
13621 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
13622 		int cnt;
13623 
13624 		SCTP_STAT_INCR(sctps_sends_with_eof);
13625 		error = 0;
13626 		if (hold_tcblock == 0) {
13627 			SCTP_TCB_LOCK(stcb);
13628 			hold_tcblock = 1;
13629 		}
13630 		cnt = sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED);
13631 		if (TAILQ_EMPTY(&asoc->send_queue) &&
13632 		    TAILQ_EMPTY(&asoc->sent_queue) &&
13633 		    (cnt == 0)) {
13634 			if (asoc->locked_on_sending) {
13635 				goto abort_anyway;
13636 			}
13637 			/* there is nothing queued to send, so I'm done... */
13638 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13639 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13640 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13641 				struct sctp_nets *netp;
13642 
13643 				if (stcb->asoc.alternate) {
13644 					netp = stcb->asoc.alternate;
13645 				} else {
13646 					netp = stcb->asoc.primary_destination;
13647 				}
13648 				/* only send SHUTDOWN the first time through */
13649 				sctp_send_shutdown(stcb, netp);
13650 				if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
13651 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13652 				}
13653 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
13654 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
13655 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13656 				    netp);
13657 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13658 				    asoc->primary_destination);
13659 			}
13660 		} else {
13661 			/*-
13662 			 * we still got (or just got) data to send, so set
13663 			 * SHUTDOWN_PENDING
13664 			 */
13665 			/*-
13666 			 * XXX sockets draft says that SCTP_EOF should be
13667 			 * sent with no data.  currently, we will allow user
13668 			 * data to be sent first and move to
13669 			 * SHUTDOWN-PENDING
13670 			 */
13671 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13672 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13673 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13674 				if (hold_tcblock == 0) {
13675 					SCTP_TCB_LOCK(stcb);
13676 					hold_tcblock = 1;
13677 				}
13678 				if (asoc->locked_on_sending) {
13679 					/* Locked to send out the data */
13680 					struct sctp_stream_queue_pending *sp;
13681 
13682 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
13683 					if (sp) {
13684 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
13685 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
13686 					}
13687 				}
13688 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
13689 				if (TAILQ_EMPTY(&asoc->send_queue) &&
13690 				    TAILQ_EMPTY(&asoc->sent_queue) &&
13691 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13692 			abort_anyway:
13693 					if (free_cnt_applied) {
13694 						atomic_add_int(&stcb->asoc.refcnt, -1);
13695 						free_cnt_applied = 0;
13696 					}
13697 					sctp_abort_an_association(stcb->sctp_ep, stcb,
13698 					    SCTP_RESPONSE_TO_USER_REQ,
13699 					    NULL, SCTP_SO_LOCKED);
13700 					/*
13701 					 * now relock the stcb so everything
13702 					 * is sane
13703 					 */
13704 					hold_tcblock = 0;
13705 					stcb = NULL;
13706 					goto out;
13707 				}
13708 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13709 				    asoc->primary_destination);
13710 				sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13711 			}
13712 		}
13713 	}
13714 skip_out_eof:
13715 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
13716 		some_on_control = 1;
13717 	}
13718 	if (queue_only_for_init) {
13719 		if (hold_tcblock == 0) {
13720 			SCTP_TCB_LOCK(stcb);
13721 			hold_tcblock = 1;
13722 		}
13723 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13724 			/* a collision took us forward? */
13725 			queue_only = 0;
13726 		} else {
13727 			sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13728 			SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13729 			queue_only = 1;
13730 		}
13731 	}
13732 	if ((net->flight_size > net->cwnd) &&
13733 	    (stcb->asoc.sctp_cmt_on_off == 0)) {
13734 		SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13735 		queue_only = 1;
13736 	} else if (asoc->ifp_had_enobuf) {
13737 		SCTP_STAT_INCR(sctps_ifnomemqueued);
13738 		if (net->flight_size > (2 * net->mtu)) {
13739 			queue_only = 1;
13740 		}
13741 		asoc->ifp_had_enobuf = 0;
13742 	}
13743 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13744 	    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13745 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13746 	    (stcb->asoc.total_flight > 0) &&
13747 	    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13748 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13749 		/*-
13750 		 * Ok, Nagle is set on and we have data outstanding.
13751 		 * Don't send anything and let SACKs drive out the
13752 		 * data unless wen have a "full" segment to send.
13753 		 */
13754 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13755 			sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13756 		}
13757 		SCTP_STAT_INCR(sctps_naglequeued);
13758 		nagle_applies = 1;
13759 	} else {
13760 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13761 			if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13762 				sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13763 		}
13764 		SCTP_STAT_INCR(sctps_naglesent);
13765 		nagle_applies = 0;
13766 	}
13767 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13768 		sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13769 		    nagle_applies, un_sent);
13770 		sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13771 		    stcb->asoc.total_flight,
13772 		    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13773 	}
13774 	if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13775 		/* we can attempt to send too. */
13776 		if (hold_tcblock == 0) {
13777 			/*
13778 			 * If there is activity recv'ing sacks no need to
13779 			 * send
13780 			 */
13781 			if (SCTP_TCB_TRYLOCK(stcb)) {
13782 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13783 				hold_tcblock = 1;
13784 			}
13785 		} else {
13786 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13787 		}
13788 	} else if ((queue_only == 0) &&
13789 		    (stcb->asoc.peers_rwnd == 0) &&
13790 	    (stcb->asoc.total_flight == 0)) {
13791 		/* We get to have a probe outstanding */
13792 		if (hold_tcblock == 0) {
13793 			hold_tcblock = 1;
13794 			SCTP_TCB_LOCK(stcb);
13795 		}
13796 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13797 	} else if (some_on_control) {
13798 		int num_out, reason, frag_point;
13799 
13800 		/* Here we do control only */
13801 		if (hold_tcblock == 0) {
13802 			hold_tcblock = 1;
13803 			SCTP_TCB_LOCK(stcb);
13804 		}
13805 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13806 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13807 		    &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13808 	}
13809 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13810 	    queue_only, stcb->asoc.peers_rwnd, un_sent,
13811 	    stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13812 	    stcb->asoc.total_output_queue_size, error);
13813 
13814 out:
13815 out_unlocked:
13816 
13817 	if (local_soresv && stcb) {
13818 		atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13819 	}
13820 	if (create_lock_applied) {
13821 		SCTP_ASOC_CREATE_UNLOCK(inp);
13822 	}
13823 	if ((stcb) && hold_tcblock) {
13824 		SCTP_TCB_UNLOCK(stcb);
13825 	}
13826 	if (stcb && free_cnt_applied) {
13827 		atomic_add_int(&stcb->asoc.refcnt, -1);
13828 	}
13829 #ifdef INVARIANTS
13830 	if (stcb) {
13831 		if (mtx_owned(&stcb->tcb_mtx)) {
13832 			panic("Leaving with tcb mtx owned?");
13833 		}
13834 		if (mtx_owned(&stcb->tcb_send_mtx)) {
13835 			panic("Leaving with tcb send mtx owned?");
13836 		}
13837 	}
13838 #endif
13839 #ifdef INVARIANTS
13840 	if (inp) {
13841 		sctp_validate_no_locks(inp);
13842 	} else {
13843 		printf("Warning - inp is NULL so cant validate locks\n");
13844 	}
13845 #endif
13846 	if (top) {
13847 		sctp_m_freem(top);
13848 	}
13849 	if (control) {
13850 		sctp_m_freem(control);
13851 	}
13852 	return (error);
13853 }
13854 
13855 
13856 /*
13857  * generate an AUTHentication chunk, if required
13858  */
13859 struct mbuf *
13860 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13861     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13862     struct sctp_tcb *stcb, uint8_t chunk)
13863 {
13864 	struct mbuf *m_auth;
13865 	struct sctp_auth_chunk *auth;
13866 	int chunk_len;
13867 	struct mbuf *cn;
13868 
13869 	if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13870 	    (stcb == NULL))
13871 		return (m);
13872 
13873 	/* sysctl disabled auth? */
13874 	if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13875 		return (m);
13876 
13877 	/* peer doesn't do auth... */
13878 	if (!stcb->asoc.peer_supports_auth) {
13879 		return (m);
13880 	}
13881 	/* does the requested chunk require auth? */
13882 	if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13883 		return (m);
13884 	}
13885 	m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13886 	if (m_auth == NULL) {
13887 		/* no mbuf's */
13888 		return (m);
13889 	}
13890 	/* reserve some space if this will be the first mbuf */
13891 	if (m == NULL)
13892 		SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13893 	/* fill in the AUTH chunk details */
13894 	auth = mtod(m_auth, struct sctp_auth_chunk *);
13895 	bzero(auth, sizeof(*auth));
13896 	auth->ch.chunk_type = SCTP_AUTHENTICATION;
13897 	auth->ch.chunk_flags = 0;
13898 	chunk_len = sizeof(*auth) +
13899 	    sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13900 	auth->ch.chunk_length = htons(chunk_len);
13901 	auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13902 	/* key id and hmac digest will be computed and filled in upon send */
13903 
13904 	/* save the offset where the auth was inserted into the chain */
13905 	*offset = 0;
13906 	for (cn = m; cn; cn = SCTP_BUF_NEXT(cn)) {
13907 		*offset += SCTP_BUF_LEN(cn);
13908 	}
13909 
13910 	/* update length and return pointer to the auth chunk */
13911 	SCTP_BUF_LEN(m_auth) = chunk_len;
13912 	m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13913 	if (auth_ret != NULL)
13914 		*auth_ret = auth;
13915 
13916 	return (m);
13917 }
13918 
13919 #ifdef INET6
13920 int
13921 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13922 {
13923 	struct nd_prefix *pfx = NULL;
13924 	struct nd_pfxrouter *pfxrtr = NULL;
13925 	struct sockaddr_in6 gw6;
13926 
13927 	if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13928 		return (0);
13929 
13930 	/* get prefix entry of address */
13931 	LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13932 		if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13933 			continue;
13934 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13935 		    &src6->sin6_addr, &pfx->ndpr_mask))
13936 			break;
13937 	}
13938 	/* no prefix entry in the prefix list */
13939 	if (pfx == NULL) {
13940 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13941 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13942 		return (0);
13943 	}
13944 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13945 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13946 
13947 	/* search installed gateway from prefix entry */
13948 	LIST_FOREACH(pfxrtr, &pfx->ndpr_advrtrs, pfr_entry) {
13949 		memset(&gw6, 0, sizeof(struct sockaddr_in6));
13950 		gw6.sin6_family = AF_INET6;
13951 		gw6.sin6_len = sizeof(struct sockaddr_in6);
13952 		memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13953 		    sizeof(struct in6_addr));
13954 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13955 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13956 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13957 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13958 		if (sctp_cmpaddr((struct sockaddr *)&gw6,
13959 		    ro->ro_rt->rt_gateway)) {
13960 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13961 			return (1);
13962 		}
13963 	}
13964 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13965 	return (0);
13966 }
13967 
13968 #endif
13969 
13970 int
13971 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13972 {
13973 #ifdef INET
13974 	struct sockaddr_in *sin, *mask;
13975 	struct ifaddr *ifa;
13976 	struct in_addr srcnetaddr, gwnetaddr;
13977 
13978 	if (ro == NULL || ro->ro_rt == NULL ||
13979 	    sifa->address.sa.sa_family != AF_INET) {
13980 		return (0);
13981 	}
13982 	ifa = (struct ifaddr *)sifa->ifa;
13983 	mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13984 	sin = (struct sockaddr_in *)&sifa->address.sin;
13985 	srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13986 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13987 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13988 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13989 
13990 	sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13991 	gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13992 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13993 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13994 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13995 	if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13996 		return (1);
13997 	}
13998 #endif
13999 	return (0);
14000 }
14001