xref: /freebsd/sys/netinet/sctp_output.c (revision 3c6e15bceeab4470243c60c9a4b5b9cafca9abaa)
1 /*-
2  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $	 */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <netinet/sctp_os.h>
37 #include <sys/proc.h>
38 #include <netinet/sctp_var.h>
39 #include <netinet/sctp_sysctl.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctputil.h>
43 #include <netinet/sctp_output.h>
44 #include <netinet/sctp_uio.h>
45 #include <netinet/sctputil.h>
46 #include <netinet/sctp_auth.h>
47 #include <netinet/sctp_timer.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_indata.h>
50 #include <netinet/sctp_bsd_addr.h>
51 #include <netinet/sctp_input.h>
52 #include <netinet/udp.h>
53 #include <machine/in_cksum.h>
54 
55 
56 
57 #define SCTP_MAX_GAPS_INARRAY 4
58 struct sack_track {
59 	uint8_t right_edge;	/* mergable on the right edge */
60 	uint8_t left_edge;	/* mergable on the left edge */
61 	uint8_t num_entries;
62 	uint8_t spare;
63 	struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
64 };
65 
66 struct sack_track sack_array[256] = {
67 	{0, 0, 0, 0,		/* 0x00 */
68 		{{0, 0},
69 		{0, 0},
70 		{0, 0},
71 		{0, 0}
72 		}
73 	},
74 	{1, 0, 1, 0,		/* 0x01 */
75 		{{0, 0},
76 		{0, 0},
77 		{0, 0},
78 		{0, 0}
79 		}
80 	},
81 	{0, 0, 1, 0,		/* 0x02 */
82 		{{1, 1},
83 		{0, 0},
84 		{0, 0},
85 		{0, 0}
86 		}
87 	},
88 	{1, 0, 1, 0,		/* 0x03 */
89 		{{0, 1},
90 		{0, 0},
91 		{0, 0},
92 		{0, 0}
93 		}
94 	},
95 	{0, 0, 1, 0,		/* 0x04 */
96 		{{2, 2},
97 		{0, 0},
98 		{0, 0},
99 		{0, 0}
100 		}
101 	},
102 	{1, 0, 2, 0,		/* 0x05 */
103 		{{0, 0},
104 		{2, 2},
105 		{0, 0},
106 		{0, 0}
107 		}
108 	},
109 	{0, 0, 1, 0,		/* 0x06 */
110 		{{1, 2},
111 		{0, 0},
112 		{0, 0},
113 		{0, 0}
114 		}
115 	},
116 	{1, 0, 1, 0,		/* 0x07 */
117 		{{0, 2},
118 		{0, 0},
119 		{0, 0},
120 		{0, 0}
121 		}
122 	},
123 	{0, 0, 1, 0,		/* 0x08 */
124 		{{3, 3},
125 		{0, 0},
126 		{0, 0},
127 		{0, 0}
128 		}
129 	},
130 	{1, 0, 2, 0,		/* 0x09 */
131 		{{0, 0},
132 		{3, 3},
133 		{0, 0},
134 		{0, 0}
135 		}
136 	},
137 	{0, 0, 2, 0,		/* 0x0a */
138 		{{1, 1},
139 		{3, 3},
140 		{0, 0},
141 		{0, 0}
142 		}
143 	},
144 	{1, 0, 2, 0,		/* 0x0b */
145 		{{0, 1},
146 		{3, 3},
147 		{0, 0},
148 		{0, 0}
149 		}
150 	},
151 	{0, 0, 1, 0,		/* 0x0c */
152 		{{2, 3},
153 		{0, 0},
154 		{0, 0},
155 		{0, 0}
156 		}
157 	},
158 	{1, 0, 2, 0,		/* 0x0d */
159 		{{0, 0},
160 		{2, 3},
161 		{0, 0},
162 		{0, 0}
163 		}
164 	},
165 	{0, 0, 1, 0,		/* 0x0e */
166 		{{1, 3},
167 		{0, 0},
168 		{0, 0},
169 		{0, 0}
170 		}
171 	},
172 	{1, 0, 1, 0,		/* 0x0f */
173 		{{0, 3},
174 		{0, 0},
175 		{0, 0},
176 		{0, 0}
177 		}
178 	},
179 	{0, 0, 1, 0,		/* 0x10 */
180 		{{4, 4},
181 		{0, 0},
182 		{0, 0},
183 		{0, 0}
184 		}
185 	},
186 	{1, 0, 2, 0,		/* 0x11 */
187 		{{0, 0},
188 		{4, 4},
189 		{0, 0},
190 		{0, 0}
191 		}
192 	},
193 	{0, 0, 2, 0,		/* 0x12 */
194 		{{1, 1},
195 		{4, 4},
196 		{0, 0},
197 		{0, 0}
198 		}
199 	},
200 	{1, 0, 2, 0,		/* 0x13 */
201 		{{0, 1},
202 		{4, 4},
203 		{0, 0},
204 		{0, 0}
205 		}
206 	},
207 	{0, 0, 2, 0,		/* 0x14 */
208 		{{2, 2},
209 		{4, 4},
210 		{0, 0},
211 		{0, 0}
212 		}
213 	},
214 	{1, 0, 3, 0,		/* 0x15 */
215 		{{0, 0},
216 		{2, 2},
217 		{4, 4},
218 		{0, 0}
219 		}
220 	},
221 	{0, 0, 2, 0,		/* 0x16 */
222 		{{1, 2},
223 		{4, 4},
224 		{0, 0},
225 		{0, 0}
226 		}
227 	},
228 	{1, 0, 2, 0,		/* 0x17 */
229 		{{0, 2},
230 		{4, 4},
231 		{0, 0},
232 		{0, 0}
233 		}
234 	},
235 	{0, 0, 1, 0,		/* 0x18 */
236 		{{3, 4},
237 		{0, 0},
238 		{0, 0},
239 		{0, 0}
240 		}
241 	},
242 	{1, 0, 2, 0,		/* 0x19 */
243 		{{0, 0},
244 		{3, 4},
245 		{0, 0},
246 		{0, 0}
247 		}
248 	},
249 	{0, 0, 2, 0,		/* 0x1a */
250 		{{1, 1},
251 		{3, 4},
252 		{0, 0},
253 		{0, 0}
254 		}
255 	},
256 	{1, 0, 2, 0,		/* 0x1b */
257 		{{0, 1},
258 		{3, 4},
259 		{0, 0},
260 		{0, 0}
261 		}
262 	},
263 	{0, 0, 1, 0,		/* 0x1c */
264 		{{2, 4},
265 		{0, 0},
266 		{0, 0},
267 		{0, 0}
268 		}
269 	},
270 	{1, 0, 2, 0,		/* 0x1d */
271 		{{0, 0},
272 		{2, 4},
273 		{0, 0},
274 		{0, 0}
275 		}
276 	},
277 	{0, 0, 1, 0,		/* 0x1e */
278 		{{1, 4},
279 		{0, 0},
280 		{0, 0},
281 		{0, 0}
282 		}
283 	},
284 	{1, 0, 1, 0,		/* 0x1f */
285 		{{0, 4},
286 		{0, 0},
287 		{0, 0},
288 		{0, 0}
289 		}
290 	},
291 	{0, 0, 1, 0,		/* 0x20 */
292 		{{5, 5},
293 		{0, 0},
294 		{0, 0},
295 		{0, 0}
296 		}
297 	},
298 	{1, 0, 2, 0,		/* 0x21 */
299 		{{0, 0},
300 		{5, 5},
301 		{0, 0},
302 		{0, 0}
303 		}
304 	},
305 	{0, 0, 2, 0,		/* 0x22 */
306 		{{1, 1},
307 		{5, 5},
308 		{0, 0},
309 		{0, 0}
310 		}
311 	},
312 	{1, 0, 2, 0,		/* 0x23 */
313 		{{0, 1},
314 		{5, 5},
315 		{0, 0},
316 		{0, 0}
317 		}
318 	},
319 	{0, 0, 2, 0,		/* 0x24 */
320 		{{2, 2},
321 		{5, 5},
322 		{0, 0},
323 		{0, 0}
324 		}
325 	},
326 	{1, 0, 3, 0,		/* 0x25 */
327 		{{0, 0},
328 		{2, 2},
329 		{5, 5},
330 		{0, 0}
331 		}
332 	},
333 	{0, 0, 2, 0,		/* 0x26 */
334 		{{1, 2},
335 		{5, 5},
336 		{0, 0},
337 		{0, 0}
338 		}
339 	},
340 	{1, 0, 2, 0,		/* 0x27 */
341 		{{0, 2},
342 		{5, 5},
343 		{0, 0},
344 		{0, 0}
345 		}
346 	},
347 	{0, 0, 2, 0,		/* 0x28 */
348 		{{3, 3},
349 		{5, 5},
350 		{0, 0},
351 		{0, 0}
352 		}
353 	},
354 	{1, 0, 3, 0,		/* 0x29 */
355 		{{0, 0},
356 		{3, 3},
357 		{5, 5},
358 		{0, 0}
359 		}
360 	},
361 	{0, 0, 3, 0,		/* 0x2a */
362 		{{1, 1},
363 		{3, 3},
364 		{5, 5},
365 		{0, 0}
366 		}
367 	},
368 	{1, 0, 3, 0,		/* 0x2b */
369 		{{0, 1},
370 		{3, 3},
371 		{5, 5},
372 		{0, 0}
373 		}
374 	},
375 	{0, 0, 2, 0,		/* 0x2c */
376 		{{2, 3},
377 		{5, 5},
378 		{0, 0},
379 		{0, 0}
380 		}
381 	},
382 	{1, 0, 3, 0,		/* 0x2d */
383 		{{0, 0},
384 		{2, 3},
385 		{5, 5},
386 		{0, 0}
387 		}
388 	},
389 	{0, 0, 2, 0,		/* 0x2e */
390 		{{1, 3},
391 		{5, 5},
392 		{0, 0},
393 		{0, 0}
394 		}
395 	},
396 	{1, 0, 2, 0,		/* 0x2f */
397 		{{0, 3},
398 		{5, 5},
399 		{0, 0},
400 		{0, 0}
401 		}
402 	},
403 	{0, 0, 1, 0,		/* 0x30 */
404 		{{4, 5},
405 		{0, 0},
406 		{0, 0},
407 		{0, 0}
408 		}
409 	},
410 	{1, 0, 2, 0,		/* 0x31 */
411 		{{0, 0},
412 		{4, 5},
413 		{0, 0},
414 		{0, 0}
415 		}
416 	},
417 	{0, 0, 2, 0,		/* 0x32 */
418 		{{1, 1},
419 		{4, 5},
420 		{0, 0},
421 		{0, 0}
422 		}
423 	},
424 	{1, 0, 2, 0,		/* 0x33 */
425 		{{0, 1},
426 		{4, 5},
427 		{0, 0},
428 		{0, 0}
429 		}
430 	},
431 	{0, 0, 2, 0,		/* 0x34 */
432 		{{2, 2},
433 		{4, 5},
434 		{0, 0},
435 		{0, 0}
436 		}
437 	},
438 	{1, 0, 3, 0,		/* 0x35 */
439 		{{0, 0},
440 		{2, 2},
441 		{4, 5},
442 		{0, 0}
443 		}
444 	},
445 	{0, 0, 2, 0,		/* 0x36 */
446 		{{1, 2},
447 		{4, 5},
448 		{0, 0},
449 		{0, 0}
450 		}
451 	},
452 	{1, 0, 2, 0,		/* 0x37 */
453 		{{0, 2},
454 		{4, 5},
455 		{0, 0},
456 		{0, 0}
457 		}
458 	},
459 	{0, 0, 1, 0,		/* 0x38 */
460 		{{3, 5},
461 		{0, 0},
462 		{0, 0},
463 		{0, 0}
464 		}
465 	},
466 	{1, 0, 2, 0,		/* 0x39 */
467 		{{0, 0},
468 		{3, 5},
469 		{0, 0},
470 		{0, 0}
471 		}
472 	},
473 	{0, 0, 2, 0,		/* 0x3a */
474 		{{1, 1},
475 		{3, 5},
476 		{0, 0},
477 		{0, 0}
478 		}
479 	},
480 	{1, 0, 2, 0,		/* 0x3b */
481 		{{0, 1},
482 		{3, 5},
483 		{0, 0},
484 		{0, 0}
485 		}
486 	},
487 	{0, 0, 1, 0,		/* 0x3c */
488 		{{2, 5},
489 		{0, 0},
490 		{0, 0},
491 		{0, 0}
492 		}
493 	},
494 	{1, 0, 2, 0,		/* 0x3d */
495 		{{0, 0},
496 		{2, 5},
497 		{0, 0},
498 		{0, 0}
499 		}
500 	},
501 	{0, 0, 1, 0,		/* 0x3e */
502 		{{1, 5},
503 		{0, 0},
504 		{0, 0},
505 		{0, 0}
506 		}
507 	},
508 	{1, 0, 1, 0,		/* 0x3f */
509 		{{0, 5},
510 		{0, 0},
511 		{0, 0},
512 		{0, 0}
513 		}
514 	},
515 	{0, 0, 1, 0,		/* 0x40 */
516 		{{6, 6},
517 		{0, 0},
518 		{0, 0},
519 		{0, 0}
520 		}
521 	},
522 	{1, 0, 2, 0,		/* 0x41 */
523 		{{0, 0},
524 		{6, 6},
525 		{0, 0},
526 		{0, 0}
527 		}
528 	},
529 	{0, 0, 2, 0,		/* 0x42 */
530 		{{1, 1},
531 		{6, 6},
532 		{0, 0},
533 		{0, 0}
534 		}
535 	},
536 	{1, 0, 2, 0,		/* 0x43 */
537 		{{0, 1},
538 		{6, 6},
539 		{0, 0},
540 		{0, 0}
541 		}
542 	},
543 	{0, 0, 2, 0,		/* 0x44 */
544 		{{2, 2},
545 		{6, 6},
546 		{0, 0},
547 		{0, 0}
548 		}
549 	},
550 	{1, 0, 3, 0,		/* 0x45 */
551 		{{0, 0},
552 		{2, 2},
553 		{6, 6},
554 		{0, 0}
555 		}
556 	},
557 	{0, 0, 2, 0,		/* 0x46 */
558 		{{1, 2},
559 		{6, 6},
560 		{0, 0},
561 		{0, 0}
562 		}
563 	},
564 	{1, 0, 2, 0,		/* 0x47 */
565 		{{0, 2},
566 		{6, 6},
567 		{0, 0},
568 		{0, 0}
569 		}
570 	},
571 	{0, 0, 2, 0,		/* 0x48 */
572 		{{3, 3},
573 		{6, 6},
574 		{0, 0},
575 		{0, 0}
576 		}
577 	},
578 	{1, 0, 3, 0,		/* 0x49 */
579 		{{0, 0},
580 		{3, 3},
581 		{6, 6},
582 		{0, 0}
583 		}
584 	},
585 	{0, 0, 3, 0,		/* 0x4a */
586 		{{1, 1},
587 		{3, 3},
588 		{6, 6},
589 		{0, 0}
590 		}
591 	},
592 	{1, 0, 3, 0,		/* 0x4b */
593 		{{0, 1},
594 		{3, 3},
595 		{6, 6},
596 		{0, 0}
597 		}
598 	},
599 	{0, 0, 2, 0,		/* 0x4c */
600 		{{2, 3},
601 		{6, 6},
602 		{0, 0},
603 		{0, 0}
604 		}
605 	},
606 	{1, 0, 3, 0,		/* 0x4d */
607 		{{0, 0},
608 		{2, 3},
609 		{6, 6},
610 		{0, 0}
611 		}
612 	},
613 	{0, 0, 2, 0,		/* 0x4e */
614 		{{1, 3},
615 		{6, 6},
616 		{0, 0},
617 		{0, 0}
618 		}
619 	},
620 	{1, 0, 2, 0,		/* 0x4f */
621 		{{0, 3},
622 		{6, 6},
623 		{0, 0},
624 		{0, 0}
625 		}
626 	},
627 	{0, 0, 2, 0,		/* 0x50 */
628 		{{4, 4},
629 		{6, 6},
630 		{0, 0},
631 		{0, 0}
632 		}
633 	},
634 	{1, 0, 3, 0,		/* 0x51 */
635 		{{0, 0},
636 		{4, 4},
637 		{6, 6},
638 		{0, 0}
639 		}
640 	},
641 	{0, 0, 3, 0,		/* 0x52 */
642 		{{1, 1},
643 		{4, 4},
644 		{6, 6},
645 		{0, 0}
646 		}
647 	},
648 	{1, 0, 3, 0,		/* 0x53 */
649 		{{0, 1},
650 		{4, 4},
651 		{6, 6},
652 		{0, 0}
653 		}
654 	},
655 	{0, 0, 3, 0,		/* 0x54 */
656 		{{2, 2},
657 		{4, 4},
658 		{6, 6},
659 		{0, 0}
660 		}
661 	},
662 	{1, 0, 4, 0,		/* 0x55 */
663 		{{0, 0},
664 		{2, 2},
665 		{4, 4},
666 		{6, 6}
667 		}
668 	},
669 	{0, 0, 3, 0,		/* 0x56 */
670 		{{1, 2},
671 		{4, 4},
672 		{6, 6},
673 		{0, 0}
674 		}
675 	},
676 	{1, 0, 3, 0,		/* 0x57 */
677 		{{0, 2},
678 		{4, 4},
679 		{6, 6},
680 		{0, 0}
681 		}
682 	},
683 	{0, 0, 2, 0,		/* 0x58 */
684 		{{3, 4},
685 		{6, 6},
686 		{0, 0},
687 		{0, 0}
688 		}
689 	},
690 	{1, 0, 3, 0,		/* 0x59 */
691 		{{0, 0},
692 		{3, 4},
693 		{6, 6},
694 		{0, 0}
695 		}
696 	},
697 	{0, 0, 3, 0,		/* 0x5a */
698 		{{1, 1},
699 		{3, 4},
700 		{6, 6},
701 		{0, 0}
702 		}
703 	},
704 	{1, 0, 3, 0,		/* 0x5b */
705 		{{0, 1},
706 		{3, 4},
707 		{6, 6},
708 		{0, 0}
709 		}
710 	},
711 	{0, 0, 2, 0,		/* 0x5c */
712 		{{2, 4},
713 		{6, 6},
714 		{0, 0},
715 		{0, 0}
716 		}
717 	},
718 	{1, 0, 3, 0,		/* 0x5d */
719 		{{0, 0},
720 		{2, 4},
721 		{6, 6},
722 		{0, 0}
723 		}
724 	},
725 	{0, 0, 2, 0,		/* 0x5e */
726 		{{1, 4},
727 		{6, 6},
728 		{0, 0},
729 		{0, 0}
730 		}
731 	},
732 	{1, 0, 2, 0,		/* 0x5f */
733 		{{0, 4},
734 		{6, 6},
735 		{0, 0},
736 		{0, 0}
737 		}
738 	},
739 	{0, 0, 1, 0,		/* 0x60 */
740 		{{5, 6},
741 		{0, 0},
742 		{0, 0},
743 		{0, 0}
744 		}
745 	},
746 	{1, 0, 2, 0,		/* 0x61 */
747 		{{0, 0},
748 		{5, 6},
749 		{0, 0},
750 		{0, 0}
751 		}
752 	},
753 	{0, 0, 2, 0,		/* 0x62 */
754 		{{1, 1},
755 		{5, 6},
756 		{0, 0},
757 		{0, 0}
758 		}
759 	},
760 	{1, 0, 2, 0,		/* 0x63 */
761 		{{0, 1},
762 		{5, 6},
763 		{0, 0},
764 		{0, 0}
765 		}
766 	},
767 	{0, 0, 2, 0,		/* 0x64 */
768 		{{2, 2},
769 		{5, 6},
770 		{0, 0},
771 		{0, 0}
772 		}
773 	},
774 	{1, 0, 3, 0,		/* 0x65 */
775 		{{0, 0},
776 		{2, 2},
777 		{5, 6},
778 		{0, 0}
779 		}
780 	},
781 	{0, 0, 2, 0,		/* 0x66 */
782 		{{1, 2},
783 		{5, 6},
784 		{0, 0},
785 		{0, 0}
786 		}
787 	},
788 	{1, 0, 2, 0,		/* 0x67 */
789 		{{0, 2},
790 		{5, 6},
791 		{0, 0},
792 		{0, 0}
793 		}
794 	},
795 	{0, 0, 2, 0,		/* 0x68 */
796 		{{3, 3},
797 		{5, 6},
798 		{0, 0},
799 		{0, 0}
800 		}
801 	},
802 	{1, 0, 3, 0,		/* 0x69 */
803 		{{0, 0},
804 		{3, 3},
805 		{5, 6},
806 		{0, 0}
807 		}
808 	},
809 	{0, 0, 3, 0,		/* 0x6a */
810 		{{1, 1},
811 		{3, 3},
812 		{5, 6},
813 		{0, 0}
814 		}
815 	},
816 	{1, 0, 3, 0,		/* 0x6b */
817 		{{0, 1},
818 		{3, 3},
819 		{5, 6},
820 		{0, 0}
821 		}
822 	},
823 	{0, 0, 2, 0,		/* 0x6c */
824 		{{2, 3},
825 		{5, 6},
826 		{0, 0},
827 		{0, 0}
828 		}
829 	},
830 	{1, 0, 3, 0,		/* 0x6d */
831 		{{0, 0},
832 		{2, 3},
833 		{5, 6},
834 		{0, 0}
835 		}
836 	},
837 	{0, 0, 2, 0,		/* 0x6e */
838 		{{1, 3},
839 		{5, 6},
840 		{0, 0},
841 		{0, 0}
842 		}
843 	},
844 	{1, 0, 2, 0,		/* 0x6f */
845 		{{0, 3},
846 		{5, 6},
847 		{0, 0},
848 		{0, 0}
849 		}
850 	},
851 	{0, 0, 1, 0,		/* 0x70 */
852 		{{4, 6},
853 		{0, 0},
854 		{0, 0},
855 		{0, 0}
856 		}
857 	},
858 	{1, 0, 2, 0,		/* 0x71 */
859 		{{0, 0},
860 		{4, 6},
861 		{0, 0},
862 		{0, 0}
863 		}
864 	},
865 	{0, 0, 2, 0,		/* 0x72 */
866 		{{1, 1},
867 		{4, 6},
868 		{0, 0},
869 		{0, 0}
870 		}
871 	},
872 	{1, 0, 2, 0,		/* 0x73 */
873 		{{0, 1},
874 		{4, 6},
875 		{0, 0},
876 		{0, 0}
877 		}
878 	},
879 	{0, 0, 2, 0,		/* 0x74 */
880 		{{2, 2},
881 		{4, 6},
882 		{0, 0},
883 		{0, 0}
884 		}
885 	},
886 	{1, 0, 3, 0,		/* 0x75 */
887 		{{0, 0},
888 		{2, 2},
889 		{4, 6},
890 		{0, 0}
891 		}
892 	},
893 	{0, 0, 2, 0,		/* 0x76 */
894 		{{1, 2},
895 		{4, 6},
896 		{0, 0},
897 		{0, 0}
898 		}
899 	},
900 	{1, 0, 2, 0,		/* 0x77 */
901 		{{0, 2},
902 		{4, 6},
903 		{0, 0},
904 		{0, 0}
905 		}
906 	},
907 	{0, 0, 1, 0,		/* 0x78 */
908 		{{3, 6},
909 		{0, 0},
910 		{0, 0},
911 		{0, 0}
912 		}
913 	},
914 	{1, 0, 2, 0,		/* 0x79 */
915 		{{0, 0},
916 		{3, 6},
917 		{0, 0},
918 		{0, 0}
919 		}
920 	},
921 	{0, 0, 2, 0,		/* 0x7a */
922 		{{1, 1},
923 		{3, 6},
924 		{0, 0},
925 		{0, 0}
926 		}
927 	},
928 	{1, 0, 2, 0,		/* 0x7b */
929 		{{0, 1},
930 		{3, 6},
931 		{0, 0},
932 		{0, 0}
933 		}
934 	},
935 	{0, 0, 1, 0,		/* 0x7c */
936 		{{2, 6},
937 		{0, 0},
938 		{0, 0},
939 		{0, 0}
940 		}
941 	},
942 	{1, 0, 2, 0,		/* 0x7d */
943 		{{0, 0},
944 		{2, 6},
945 		{0, 0},
946 		{0, 0}
947 		}
948 	},
949 	{0, 0, 1, 0,		/* 0x7e */
950 		{{1, 6},
951 		{0, 0},
952 		{0, 0},
953 		{0, 0}
954 		}
955 	},
956 	{1, 0, 1, 0,		/* 0x7f */
957 		{{0, 6},
958 		{0, 0},
959 		{0, 0},
960 		{0, 0}
961 		}
962 	},
963 	{0, 1, 1, 0,		/* 0x80 */
964 		{{7, 7},
965 		{0, 0},
966 		{0, 0},
967 		{0, 0}
968 		}
969 	},
970 	{1, 1, 2, 0,		/* 0x81 */
971 		{{0, 0},
972 		{7, 7},
973 		{0, 0},
974 		{0, 0}
975 		}
976 	},
977 	{0, 1, 2, 0,		/* 0x82 */
978 		{{1, 1},
979 		{7, 7},
980 		{0, 0},
981 		{0, 0}
982 		}
983 	},
984 	{1, 1, 2, 0,		/* 0x83 */
985 		{{0, 1},
986 		{7, 7},
987 		{0, 0},
988 		{0, 0}
989 		}
990 	},
991 	{0, 1, 2, 0,		/* 0x84 */
992 		{{2, 2},
993 		{7, 7},
994 		{0, 0},
995 		{0, 0}
996 		}
997 	},
998 	{1, 1, 3, 0,		/* 0x85 */
999 		{{0, 0},
1000 		{2, 2},
1001 		{7, 7},
1002 		{0, 0}
1003 		}
1004 	},
1005 	{0, 1, 2, 0,		/* 0x86 */
1006 		{{1, 2},
1007 		{7, 7},
1008 		{0, 0},
1009 		{0, 0}
1010 		}
1011 	},
1012 	{1, 1, 2, 0,		/* 0x87 */
1013 		{{0, 2},
1014 		{7, 7},
1015 		{0, 0},
1016 		{0, 0}
1017 		}
1018 	},
1019 	{0, 1, 2, 0,		/* 0x88 */
1020 		{{3, 3},
1021 		{7, 7},
1022 		{0, 0},
1023 		{0, 0}
1024 		}
1025 	},
1026 	{1, 1, 3, 0,		/* 0x89 */
1027 		{{0, 0},
1028 		{3, 3},
1029 		{7, 7},
1030 		{0, 0}
1031 		}
1032 	},
1033 	{0, 1, 3, 0,		/* 0x8a */
1034 		{{1, 1},
1035 		{3, 3},
1036 		{7, 7},
1037 		{0, 0}
1038 		}
1039 	},
1040 	{1, 1, 3, 0,		/* 0x8b */
1041 		{{0, 1},
1042 		{3, 3},
1043 		{7, 7},
1044 		{0, 0}
1045 		}
1046 	},
1047 	{0, 1, 2, 0,		/* 0x8c */
1048 		{{2, 3},
1049 		{7, 7},
1050 		{0, 0},
1051 		{0, 0}
1052 		}
1053 	},
1054 	{1, 1, 3, 0,		/* 0x8d */
1055 		{{0, 0},
1056 		{2, 3},
1057 		{7, 7},
1058 		{0, 0}
1059 		}
1060 	},
1061 	{0, 1, 2, 0,		/* 0x8e */
1062 		{{1, 3},
1063 		{7, 7},
1064 		{0, 0},
1065 		{0, 0}
1066 		}
1067 	},
1068 	{1, 1, 2, 0,		/* 0x8f */
1069 		{{0, 3},
1070 		{7, 7},
1071 		{0, 0},
1072 		{0, 0}
1073 		}
1074 	},
1075 	{0, 1, 2, 0,		/* 0x90 */
1076 		{{4, 4},
1077 		{7, 7},
1078 		{0, 0},
1079 		{0, 0}
1080 		}
1081 	},
1082 	{1, 1, 3, 0,		/* 0x91 */
1083 		{{0, 0},
1084 		{4, 4},
1085 		{7, 7},
1086 		{0, 0}
1087 		}
1088 	},
1089 	{0, 1, 3, 0,		/* 0x92 */
1090 		{{1, 1},
1091 		{4, 4},
1092 		{7, 7},
1093 		{0, 0}
1094 		}
1095 	},
1096 	{1, 1, 3, 0,		/* 0x93 */
1097 		{{0, 1},
1098 		{4, 4},
1099 		{7, 7},
1100 		{0, 0}
1101 		}
1102 	},
1103 	{0, 1, 3, 0,		/* 0x94 */
1104 		{{2, 2},
1105 		{4, 4},
1106 		{7, 7},
1107 		{0, 0}
1108 		}
1109 	},
1110 	{1, 1, 4, 0,		/* 0x95 */
1111 		{{0, 0},
1112 		{2, 2},
1113 		{4, 4},
1114 		{7, 7}
1115 		}
1116 	},
1117 	{0, 1, 3, 0,		/* 0x96 */
1118 		{{1, 2},
1119 		{4, 4},
1120 		{7, 7},
1121 		{0, 0}
1122 		}
1123 	},
1124 	{1, 1, 3, 0,		/* 0x97 */
1125 		{{0, 2},
1126 		{4, 4},
1127 		{7, 7},
1128 		{0, 0}
1129 		}
1130 	},
1131 	{0, 1, 2, 0,		/* 0x98 */
1132 		{{3, 4},
1133 		{7, 7},
1134 		{0, 0},
1135 		{0, 0}
1136 		}
1137 	},
1138 	{1, 1, 3, 0,		/* 0x99 */
1139 		{{0, 0},
1140 		{3, 4},
1141 		{7, 7},
1142 		{0, 0}
1143 		}
1144 	},
1145 	{0, 1, 3, 0,		/* 0x9a */
1146 		{{1, 1},
1147 		{3, 4},
1148 		{7, 7},
1149 		{0, 0}
1150 		}
1151 	},
1152 	{1, 1, 3, 0,		/* 0x9b */
1153 		{{0, 1},
1154 		{3, 4},
1155 		{7, 7},
1156 		{0, 0}
1157 		}
1158 	},
1159 	{0, 1, 2, 0,		/* 0x9c */
1160 		{{2, 4},
1161 		{7, 7},
1162 		{0, 0},
1163 		{0, 0}
1164 		}
1165 	},
1166 	{1, 1, 3, 0,		/* 0x9d */
1167 		{{0, 0},
1168 		{2, 4},
1169 		{7, 7},
1170 		{0, 0}
1171 		}
1172 	},
1173 	{0, 1, 2, 0,		/* 0x9e */
1174 		{{1, 4},
1175 		{7, 7},
1176 		{0, 0},
1177 		{0, 0}
1178 		}
1179 	},
1180 	{1, 1, 2, 0,		/* 0x9f */
1181 		{{0, 4},
1182 		{7, 7},
1183 		{0, 0},
1184 		{0, 0}
1185 		}
1186 	},
1187 	{0, 1, 2, 0,		/* 0xa0 */
1188 		{{5, 5},
1189 		{7, 7},
1190 		{0, 0},
1191 		{0, 0}
1192 		}
1193 	},
1194 	{1, 1, 3, 0,		/* 0xa1 */
1195 		{{0, 0},
1196 		{5, 5},
1197 		{7, 7},
1198 		{0, 0}
1199 		}
1200 	},
1201 	{0, 1, 3, 0,		/* 0xa2 */
1202 		{{1, 1},
1203 		{5, 5},
1204 		{7, 7},
1205 		{0, 0}
1206 		}
1207 	},
1208 	{1, 1, 3, 0,		/* 0xa3 */
1209 		{{0, 1},
1210 		{5, 5},
1211 		{7, 7},
1212 		{0, 0}
1213 		}
1214 	},
1215 	{0, 1, 3, 0,		/* 0xa4 */
1216 		{{2, 2},
1217 		{5, 5},
1218 		{7, 7},
1219 		{0, 0}
1220 		}
1221 	},
1222 	{1, 1, 4, 0,		/* 0xa5 */
1223 		{{0, 0},
1224 		{2, 2},
1225 		{5, 5},
1226 		{7, 7}
1227 		}
1228 	},
1229 	{0, 1, 3, 0,		/* 0xa6 */
1230 		{{1, 2},
1231 		{5, 5},
1232 		{7, 7},
1233 		{0, 0}
1234 		}
1235 	},
1236 	{1, 1, 3, 0,		/* 0xa7 */
1237 		{{0, 2},
1238 		{5, 5},
1239 		{7, 7},
1240 		{0, 0}
1241 		}
1242 	},
1243 	{0, 1, 3, 0,		/* 0xa8 */
1244 		{{3, 3},
1245 		{5, 5},
1246 		{7, 7},
1247 		{0, 0}
1248 		}
1249 	},
1250 	{1, 1, 4, 0,		/* 0xa9 */
1251 		{{0, 0},
1252 		{3, 3},
1253 		{5, 5},
1254 		{7, 7}
1255 		}
1256 	},
1257 	{0, 1, 4, 0,		/* 0xaa */
1258 		{{1, 1},
1259 		{3, 3},
1260 		{5, 5},
1261 		{7, 7}
1262 		}
1263 	},
1264 	{1, 1, 4, 0,		/* 0xab */
1265 		{{0, 1},
1266 		{3, 3},
1267 		{5, 5},
1268 		{7, 7}
1269 		}
1270 	},
1271 	{0, 1, 3, 0,		/* 0xac */
1272 		{{2, 3},
1273 		{5, 5},
1274 		{7, 7},
1275 		{0, 0}
1276 		}
1277 	},
1278 	{1, 1, 4, 0,		/* 0xad */
1279 		{{0, 0},
1280 		{2, 3},
1281 		{5, 5},
1282 		{7, 7}
1283 		}
1284 	},
1285 	{0, 1, 3, 0,		/* 0xae */
1286 		{{1, 3},
1287 		{5, 5},
1288 		{7, 7},
1289 		{0, 0}
1290 		}
1291 	},
1292 	{1, 1, 3, 0,		/* 0xaf */
1293 		{{0, 3},
1294 		{5, 5},
1295 		{7, 7},
1296 		{0, 0}
1297 		}
1298 	},
1299 	{0, 1, 2, 0,		/* 0xb0 */
1300 		{{4, 5},
1301 		{7, 7},
1302 		{0, 0},
1303 		{0, 0}
1304 		}
1305 	},
1306 	{1, 1, 3, 0,		/* 0xb1 */
1307 		{{0, 0},
1308 		{4, 5},
1309 		{7, 7},
1310 		{0, 0}
1311 		}
1312 	},
1313 	{0, 1, 3, 0,		/* 0xb2 */
1314 		{{1, 1},
1315 		{4, 5},
1316 		{7, 7},
1317 		{0, 0}
1318 		}
1319 	},
1320 	{1, 1, 3, 0,		/* 0xb3 */
1321 		{{0, 1},
1322 		{4, 5},
1323 		{7, 7},
1324 		{0, 0}
1325 		}
1326 	},
1327 	{0, 1, 3, 0,		/* 0xb4 */
1328 		{{2, 2},
1329 		{4, 5},
1330 		{7, 7},
1331 		{0, 0}
1332 		}
1333 	},
1334 	{1, 1, 4, 0,		/* 0xb5 */
1335 		{{0, 0},
1336 		{2, 2},
1337 		{4, 5},
1338 		{7, 7}
1339 		}
1340 	},
1341 	{0, 1, 3, 0,		/* 0xb6 */
1342 		{{1, 2},
1343 		{4, 5},
1344 		{7, 7},
1345 		{0, 0}
1346 		}
1347 	},
1348 	{1, 1, 3, 0,		/* 0xb7 */
1349 		{{0, 2},
1350 		{4, 5},
1351 		{7, 7},
1352 		{0, 0}
1353 		}
1354 	},
1355 	{0, 1, 2, 0,		/* 0xb8 */
1356 		{{3, 5},
1357 		{7, 7},
1358 		{0, 0},
1359 		{0, 0}
1360 		}
1361 	},
1362 	{1, 1, 3, 0,		/* 0xb9 */
1363 		{{0, 0},
1364 		{3, 5},
1365 		{7, 7},
1366 		{0, 0}
1367 		}
1368 	},
1369 	{0, 1, 3, 0,		/* 0xba */
1370 		{{1, 1},
1371 		{3, 5},
1372 		{7, 7},
1373 		{0, 0}
1374 		}
1375 	},
1376 	{1, 1, 3, 0,		/* 0xbb */
1377 		{{0, 1},
1378 		{3, 5},
1379 		{7, 7},
1380 		{0, 0}
1381 		}
1382 	},
1383 	{0, 1, 2, 0,		/* 0xbc */
1384 		{{2, 5},
1385 		{7, 7},
1386 		{0, 0},
1387 		{0, 0}
1388 		}
1389 	},
1390 	{1, 1, 3, 0,		/* 0xbd */
1391 		{{0, 0},
1392 		{2, 5},
1393 		{7, 7},
1394 		{0, 0}
1395 		}
1396 	},
1397 	{0, 1, 2, 0,		/* 0xbe */
1398 		{{1, 5},
1399 		{7, 7},
1400 		{0, 0},
1401 		{0, 0}
1402 		}
1403 	},
1404 	{1, 1, 2, 0,		/* 0xbf */
1405 		{{0, 5},
1406 		{7, 7},
1407 		{0, 0},
1408 		{0, 0}
1409 		}
1410 	},
1411 	{0, 1, 1, 0,		/* 0xc0 */
1412 		{{6, 7},
1413 		{0, 0},
1414 		{0, 0},
1415 		{0, 0}
1416 		}
1417 	},
1418 	{1, 1, 2, 0,		/* 0xc1 */
1419 		{{0, 0},
1420 		{6, 7},
1421 		{0, 0},
1422 		{0, 0}
1423 		}
1424 	},
1425 	{0, 1, 2, 0,		/* 0xc2 */
1426 		{{1, 1},
1427 		{6, 7},
1428 		{0, 0},
1429 		{0, 0}
1430 		}
1431 	},
1432 	{1, 1, 2, 0,		/* 0xc3 */
1433 		{{0, 1},
1434 		{6, 7},
1435 		{0, 0},
1436 		{0, 0}
1437 		}
1438 	},
1439 	{0, 1, 2, 0,		/* 0xc4 */
1440 		{{2, 2},
1441 		{6, 7},
1442 		{0, 0},
1443 		{0, 0}
1444 		}
1445 	},
1446 	{1, 1, 3, 0,		/* 0xc5 */
1447 		{{0, 0},
1448 		{2, 2},
1449 		{6, 7},
1450 		{0, 0}
1451 		}
1452 	},
1453 	{0, 1, 2, 0,		/* 0xc6 */
1454 		{{1, 2},
1455 		{6, 7},
1456 		{0, 0},
1457 		{0, 0}
1458 		}
1459 	},
1460 	{1, 1, 2, 0,		/* 0xc7 */
1461 		{{0, 2},
1462 		{6, 7},
1463 		{0, 0},
1464 		{0, 0}
1465 		}
1466 	},
1467 	{0, 1, 2, 0,		/* 0xc8 */
1468 		{{3, 3},
1469 		{6, 7},
1470 		{0, 0},
1471 		{0, 0}
1472 		}
1473 	},
1474 	{1, 1, 3, 0,		/* 0xc9 */
1475 		{{0, 0},
1476 		{3, 3},
1477 		{6, 7},
1478 		{0, 0}
1479 		}
1480 	},
1481 	{0, 1, 3, 0,		/* 0xca */
1482 		{{1, 1},
1483 		{3, 3},
1484 		{6, 7},
1485 		{0, 0}
1486 		}
1487 	},
1488 	{1, 1, 3, 0,		/* 0xcb */
1489 		{{0, 1},
1490 		{3, 3},
1491 		{6, 7},
1492 		{0, 0}
1493 		}
1494 	},
1495 	{0, 1, 2, 0,		/* 0xcc */
1496 		{{2, 3},
1497 		{6, 7},
1498 		{0, 0},
1499 		{0, 0}
1500 		}
1501 	},
1502 	{1, 1, 3, 0,		/* 0xcd */
1503 		{{0, 0},
1504 		{2, 3},
1505 		{6, 7},
1506 		{0, 0}
1507 		}
1508 	},
1509 	{0, 1, 2, 0,		/* 0xce */
1510 		{{1, 3},
1511 		{6, 7},
1512 		{0, 0},
1513 		{0, 0}
1514 		}
1515 	},
1516 	{1, 1, 2, 0,		/* 0xcf */
1517 		{{0, 3},
1518 		{6, 7},
1519 		{0, 0},
1520 		{0, 0}
1521 		}
1522 	},
1523 	{0, 1, 2, 0,		/* 0xd0 */
1524 		{{4, 4},
1525 		{6, 7},
1526 		{0, 0},
1527 		{0, 0}
1528 		}
1529 	},
1530 	{1, 1, 3, 0,		/* 0xd1 */
1531 		{{0, 0},
1532 		{4, 4},
1533 		{6, 7},
1534 		{0, 0}
1535 		}
1536 	},
1537 	{0, 1, 3, 0,		/* 0xd2 */
1538 		{{1, 1},
1539 		{4, 4},
1540 		{6, 7},
1541 		{0, 0}
1542 		}
1543 	},
1544 	{1, 1, 3, 0,		/* 0xd3 */
1545 		{{0, 1},
1546 		{4, 4},
1547 		{6, 7},
1548 		{0, 0}
1549 		}
1550 	},
1551 	{0, 1, 3, 0,		/* 0xd4 */
1552 		{{2, 2},
1553 		{4, 4},
1554 		{6, 7},
1555 		{0, 0}
1556 		}
1557 	},
1558 	{1, 1, 4, 0,		/* 0xd5 */
1559 		{{0, 0},
1560 		{2, 2},
1561 		{4, 4},
1562 		{6, 7}
1563 		}
1564 	},
1565 	{0, 1, 3, 0,		/* 0xd6 */
1566 		{{1, 2},
1567 		{4, 4},
1568 		{6, 7},
1569 		{0, 0}
1570 		}
1571 	},
1572 	{1, 1, 3, 0,		/* 0xd7 */
1573 		{{0, 2},
1574 		{4, 4},
1575 		{6, 7},
1576 		{0, 0}
1577 		}
1578 	},
1579 	{0, 1, 2, 0,		/* 0xd8 */
1580 		{{3, 4},
1581 		{6, 7},
1582 		{0, 0},
1583 		{0, 0}
1584 		}
1585 	},
1586 	{1, 1, 3, 0,		/* 0xd9 */
1587 		{{0, 0},
1588 		{3, 4},
1589 		{6, 7},
1590 		{0, 0}
1591 		}
1592 	},
1593 	{0, 1, 3, 0,		/* 0xda */
1594 		{{1, 1},
1595 		{3, 4},
1596 		{6, 7},
1597 		{0, 0}
1598 		}
1599 	},
1600 	{1, 1, 3, 0,		/* 0xdb */
1601 		{{0, 1},
1602 		{3, 4},
1603 		{6, 7},
1604 		{0, 0}
1605 		}
1606 	},
1607 	{0, 1, 2, 0,		/* 0xdc */
1608 		{{2, 4},
1609 		{6, 7},
1610 		{0, 0},
1611 		{0, 0}
1612 		}
1613 	},
1614 	{1, 1, 3, 0,		/* 0xdd */
1615 		{{0, 0},
1616 		{2, 4},
1617 		{6, 7},
1618 		{0, 0}
1619 		}
1620 	},
1621 	{0, 1, 2, 0,		/* 0xde */
1622 		{{1, 4},
1623 		{6, 7},
1624 		{0, 0},
1625 		{0, 0}
1626 		}
1627 	},
1628 	{1, 1, 2, 0,		/* 0xdf */
1629 		{{0, 4},
1630 		{6, 7},
1631 		{0, 0},
1632 		{0, 0}
1633 		}
1634 	},
1635 	{0, 1, 1, 0,		/* 0xe0 */
1636 		{{5, 7},
1637 		{0, 0},
1638 		{0, 0},
1639 		{0, 0}
1640 		}
1641 	},
1642 	{1, 1, 2, 0,		/* 0xe1 */
1643 		{{0, 0},
1644 		{5, 7},
1645 		{0, 0},
1646 		{0, 0}
1647 		}
1648 	},
1649 	{0, 1, 2, 0,		/* 0xe2 */
1650 		{{1, 1},
1651 		{5, 7},
1652 		{0, 0},
1653 		{0, 0}
1654 		}
1655 	},
1656 	{1, 1, 2, 0,		/* 0xe3 */
1657 		{{0, 1},
1658 		{5, 7},
1659 		{0, 0},
1660 		{0, 0}
1661 		}
1662 	},
1663 	{0, 1, 2, 0,		/* 0xe4 */
1664 		{{2, 2},
1665 		{5, 7},
1666 		{0, 0},
1667 		{0, 0}
1668 		}
1669 	},
1670 	{1, 1, 3, 0,		/* 0xe5 */
1671 		{{0, 0},
1672 		{2, 2},
1673 		{5, 7},
1674 		{0, 0}
1675 		}
1676 	},
1677 	{0, 1, 2, 0,		/* 0xe6 */
1678 		{{1, 2},
1679 		{5, 7},
1680 		{0, 0},
1681 		{0, 0}
1682 		}
1683 	},
1684 	{1, 1, 2, 0,		/* 0xe7 */
1685 		{{0, 2},
1686 		{5, 7},
1687 		{0, 0},
1688 		{0, 0}
1689 		}
1690 	},
1691 	{0, 1, 2, 0,		/* 0xe8 */
1692 		{{3, 3},
1693 		{5, 7},
1694 		{0, 0},
1695 		{0, 0}
1696 		}
1697 	},
1698 	{1, 1, 3, 0,		/* 0xe9 */
1699 		{{0, 0},
1700 		{3, 3},
1701 		{5, 7},
1702 		{0, 0}
1703 		}
1704 	},
1705 	{0, 1, 3, 0,		/* 0xea */
1706 		{{1, 1},
1707 		{3, 3},
1708 		{5, 7},
1709 		{0, 0}
1710 		}
1711 	},
1712 	{1, 1, 3, 0,		/* 0xeb */
1713 		{{0, 1},
1714 		{3, 3},
1715 		{5, 7},
1716 		{0, 0}
1717 		}
1718 	},
1719 	{0, 1, 2, 0,		/* 0xec */
1720 		{{2, 3},
1721 		{5, 7},
1722 		{0, 0},
1723 		{0, 0}
1724 		}
1725 	},
1726 	{1, 1, 3, 0,		/* 0xed */
1727 		{{0, 0},
1728 		{2, 3},
1729 		{5, 7},
1730 		{0, 0}
1731 		}
1732 	},
1733 	{0, 1, 2, 0,		/* 0xee */
1734 		{{1, 3},
1735 		{5, 7},
1736 		{0, 0},
1737 		{0, 0}
1738 		}
1739 	},
1740 	{1, 1, 2, 0,		/* 0xef */
1741 		{{0, 3},
1742 		{5, 7},
1743 		{0, 0},
1744 		{0, 0}
1745 		}
1746 	},
1747 	{0, 1, 1, 0,		/* 0xf0 */
1748 		{{4, 7},
1749 		{0, 0},
1750 		{0, 0},
1751 		{0, 0}
1752 		}
1753 	},
1754 	{1, 1, 2, 0,		/* 0xf1 */
1755 		{{0, 0},
1756 		{4, 7},
1757 		{0, 0},
1758 		{0, 0}
1759 		}
1760 	},
1761 	{0, 1, 2, 0,		/* 0xf2 */
1762 		{{1, 1},
1763 		{4, 7},
1764 		{0, 0},
1765 		{0, 0}
1766 		}
1767 	},
1768 	{1, 1, 2, 0,		/* 0xf3 */
1769 		{{0, 1},
1770 		{4, 7},
1771 		{0, 0},
1772 		{0, 0}
1773 		}
1774 	},
1775 	{0, 1, 2, 0,		/* 0xf4 */
1776 		{{2, 2},
1777 		{4, 7},
1778 		{0, 0},
1779 		{0, 0}
1780 		}
1781 	},
1782 	{1, 1, 3, 0,		/* 0xf5 */
1783 		{{0, 0},
1784 		{2, 2},
1785 		{4, 7},
1786 		{0, 0}
1787 		}
1788 	},
1789 	{0, 1, 2, 0,		/* 0xf6 */
1790 		{{1, 2},
1791 		{4, 7},
1792 		{0, 0},
1793 		{0, 0}
1794 		}
1795 	},
1796 	{1, 1, 2, 0,		/* 0xf7 */
1797 		{{0, 2},
1798 		{4, 7},
1799 		{0, 0},
1800 		{0, 0}
1801 		}
1802 	},
1803 	{0, 1, 1, 0,		/* 0xf8 */
1804 		{{3, 7},
1805 		{0, 0},
1806 		{0, 0},
1807 		{0, 0}
1808 		}
1809 	},
1810 	{1, 1, 2, 0,		/* 0xf9 */
1811 		{{0, 0},
1812 		{3, 7},
1813 		{0, 0},
1814 		{0, 0}
1815 		}
1816 	},
1817 	{0, 1, 2, 0,		/* 0xfa */
1818 		{{1, 1},
1819 		{3, 7},
1820 		{0, 0},
1821 		{0, 0}
1822 		}
1823 	},
1824 	{1, 1, 2, 0,		/* 0xfb */
1825 		{{0, 1},
1826 		{3, 7},
1827 		{0, 0},
1828 		{0, 0}
1829 		}
1830 	},
1831 	{0, 1, 1, 0,		/* 0xfc */
1832 		{{2, 7},
1833 		{0, 0},
1834 		{0, 0},
1835 		{0, 0}
1836 		}
1837 	},
1838 	{1, 1, 2, 0,		/* 0xfd */
1839 		{{0, 0},
1840 		{2, 7},
1841 		{0, 0},
1842 		{0, 0}
1843 		}
1844 	},
1845 	{0, 1, 1, 0,		/* 0xfe */
1846 		{{1, 7},
1847 		{0, 0},
1848 		{0, 0},
1849 		{0, 0}
1850 		}
1851 	},
1852 	{1, 1, 1, 0,		/* 0xff */
1853 		{{0, 7},
1854 		{0, 0},
1855 		{0, 0},
1856 		{0, 0}
1857 		}
1858 	}
1859 };
1860 
1861 
1862 int
1863 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1864     int ipv4_addr_legal,
1865     int ipv6_addr_legal,
1866     int loopback_scope,
1867     int ipv4_local_scope,
1868     int local_scope,
1869     int site_scope,
1870     int do_update)
1871 {
1872 	if ((loopback_scope == 0) &&
1873 	    (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1874 		/*
1875 		 * skip loopback if not in scope *
1876 		 */
1877 		return (0);
1878 	}
1879 	switch (ifa->address.sa.sa_family) {
1880 	case AF_INET:
1881 		if (ipv4_addr_legal) {
1882 			struct sockaddr_in *sin;
1883 
1884 			sin = (struct sockaddr_in *)&ifa->address.sin;
1885 			if (sin->sin_addr.s_addr == 0) {
1886 				/* not in scope , unspecified */
1887 				return (0);
1888 			}
1889 			if ((ipv4_local_scope == 0) &&
1890 			    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1891 				/* private address not in scope */
1892 				return (0);
1893 			}
1894 		} else {
1895 			return (0);
1896 		}
1897 		break;
1898 #ifdef INET6
1899 	case AF_INET6:
1900 		if (ipv6_addr_legal) {
1901 			struct sockaddr_in6 *sin6;
1902 
1903 			/*
1904 			 * Must update the flags,  bummer, which means any
1905 			 * IFA locks must now be applied HERE <->
1906 			 */
1907 			if (do_update) {
1908 				sctp_gather_internal_ifa_flags(ifa);
1909 			}
1910 			if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1911 				return (0);
1912 			}
1913 			/* ok to use deprecated addresses? */
1914 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1915 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1916 				/* skip unspecifed addresses */
1917 				return (0);
1918 			}
1919 			if (	/* (local_scope == 0) && */
1920 			    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1921 				return (0);
1922 			}
1923 			if ((site_scope == 0) &&
1924 			    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1925 				return (0);
1926 			}
1927 		} else {
1928 			return (0);
1929 		}
1930 		break;
1931 #endif
1932 	default:
1933 		return (0);
1934 	}
1935 	return (1);
1936 }
1937 
1938 static struct mbuf *
1939 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1940 {
1941 	struct sctp_paramhdr *parmh;
1942 	struct mbuf *mret;
1943 	int len;
1944 
1945 	if (ifa->address.sa.sa_family == AF_INET) {
1946 		len = sizeof(struct sctp_ipv4addr_param);
1947 	} else if (ifa->address.sa.sa_family == AF_INET6) {
1948 		len = sizeof(struct sctp_ipv6addr_param);
1949 	} else {
1950 		/* unknown type */
1951 		return (m);
1952 	}
1953 	if (M_TRAILINGSPACE(m) >= len) {
1954 		/* easy side we just drop it on the end */
1955 		parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1956 		mret = m;
1957 	} else {
1958 		/* Need more space */
1959 		mret = m;
1960 		while (SCTP_BUF_NEXT(mret) != NULL) {
1961 			mret = SCTP_BUF_NEXT(mret);
1962 		}
1963 		SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1964 		if (SCTP_BUF_NEXT(mret) == NULL) {
1965 			/* We are hosed, can't add more addresses */
1966 			return (m);
1967 		}
1968 		mret = SCTP_BUF_NEXT(mret);
1969 		parmh = mtod(mret, struct sctp_paramhdr *);
1970 	}
1971 	/* now add the parameter */
1972 	switch (ifa->address.sa.sa_family) {
1973 	case AF_INET:
1974 		{
1975 			struct sctp_ipv4addr_param *ipv4p;
1976 			struct sockaddr_in *sin;
1977 
1978 			sin = (struct sockaddr_in *)&ifa->address.sin;
1979 			ipv4p = (struct sctp_ipv4addr_param *)parmh;
1980 			parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1981 			parmh->param_length = htons(len);
1982 			ipv4p->addr = sin->sin_addr.s_addr;
1983 			SCTP_BUF_LEN(mret) += len;
1984 			break;
1985 		}
1986 #ifdef INET6
1987 	case AF_INET6:
1988 		{
1989 			struct sctp_ipv6addr_param *ipv6p;
1990 			struct sockaddr_in6 *sin6;
1991 
1992 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1993 			ipv6p = (struct sctp_ipv6addr_param *)parmh;
1994 			parmh->param_type = htons(SCTP_IPV6_ADDRESS);
1995 			parmh->param_length = htons(len);
1996 			memcpy(ipv6p->addr, &sin6->sin6_addr,
1997 			    sizeof(ipv6p->addr));
1998 			/* clear embedded scope in the address */
1999 			in6_clearscope((struct in6_addr *)ipv6p->addr);
2000 			SCTP_BUF_LEN(mret) += len;
2001 			break;
2002 		}
2003 #endif
2004 	default:
2005 		return (m);
2006 	}
2007 	return (mret);
2008 }
2009 
2010 
2011 struct mbuf *
2012 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_scoping *scope,
2013     struct mbuf *m_at, int cnt_inits_to)
2014 {
2015 	struct sctp_vrf *vrf = NULL;
2016 	int cnt, limit_out = 0, total_count;
2017 	uint32_t vrf_id;
2018 
2019 	vrf_id = inp->def_vrf_id;
2020 	SCTP_IPI_ADDR_RLOCK();
2021 	vrf = sctp_find_vrf(vrf_id);
2022 	if (vrf == NULL) {
2023 		SCTP_IPI_ADDR_RUNLOCK();
2024 		return (m_at);
2025 	}
2026 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2027 		struct sctp_ifa *sctp_ifap;
2028 		struct sctp_ifn *sctp_ifnp;
2029 
2030 		cnt = cnt_inits_to;
2031 		if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2032 			limit_out = 1;
2033 			cnt = SCTP_ADDRESS_LIMIT;
2034 			goto skip_count;
2035 		}
2036 		LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2037 			if ((scope->loopback_scope == 0) &&
2038 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2039 				/*
2040 				 * Skip loopback devices if loopback_scope
2041 				 * not set
2042 				 */
2043 				continue;
2044 			}
2045 			LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2046 				if (sctp_is_address_in_scope(sctp_ifap,
2047 				    scope->ipv4_addr_legal,
2048 				    scope->ipv6_addr_legal,
2049 				    scope->loopback_scope,
2050 				    scope->ipv4_local_scope,
2051 				    scope->local_scope,
2052 				    scope->site_scope, 1) == 0) {
2053 					continue;
2054 				}
2055 				cnt++;
2056 				if (cnt > SCTP_ADDRESS_LIMIT) {
2057 					break;
2058 				}
2059 			}
2060 			if (cnt > SCTP_ADDRESS_LIMIT) {
2061 				break;
2062 			}
2063 		}
2064 skip_count:
2065 		if (cnt > 1) {
2066 			total_count = 0;
2067 			LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2068 				cnt = 0;
2069 				if ((scope->loopback_scope == 0) &&
2070 				    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2071 					/*
2072 					 * Skip loopback devices if
2073 					 * loopback_scope not set
2074 					 */
2075 					continue;
2076 				}
2077 				LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2078 					if (sctp_is_address_in_scope(sctp_ifap,
2079 					    scope->ipv4_addr_legal,
2080 					    scope->ipv6_addr_legal,
2081 					    scope->loopback_scope,
2082 					    scope->ipv4_local_scope,
2083 					    scope->local_scope,
2084 					    scope->site_scope, 0) == 0) {
2085 						continue;
2086 					}
2087 					m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2088 					if (limit_out) {
2089 						cnt++;
2090 						total_count++;
2091 						if (cnt >= 2) {
2092 							/*
2093 							 * two from each
2094 							 * address
2095 							 */
2096 							break;
2097 						}
2098 						if (total_count > SCTP_ADDRESS_LIMIT) {
2099 							/* No more addresses */
2100 							break;
2101 						}
2102 					}
2103 				}
2104 			}
2105 		}
2106 	} else {
2107 		struct sctp_laddr *laddr;
2108 
2109 		cnt = cnt_inits_to;
2110 		/* First, how many ? */
2111 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2112 			if (laddr->ifa == NULL) {
2113 				continue;
2114 			}
2115 			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2116 				/*
2117 				 * Address being deleted by the system, dont
2118 				 * list.
2119 				 */
2120 				continue;
2121 			if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2122 				/*
2123 				 * Address being deleted on this ep don't
2124 				 * list.
2125 				 */
2126 				continue;
2127 			}
2128 			if (sctp_is_address_in_scope(laddr->ifa,
2129 			    scope->ipv4_addr_legal,
2130 			    scope->ipv6_addr_legal,
2131 			    scope->loopback_scope,
2132 			    scope->ipv4_local_scope,
2133 			    scope->local_scope,
2134 			    scope->site_scope, 1) == 0) {
2135 				continue;
2136 			}
2137 			cnt++;
2138 		}
2139 		if (cnt > SCTP_ADDRESS_LIMIT) {
2140 			limit_out = 1;
2141 		}
2142 		/*
2143 		 * To get through a NAT we only list addresses if we have
2144 		 * more than one. That way if you just bind a single address
2145 		 * we let the source of the init dictate our address.
2146 		 */
2147 		if (cnt > 1) {
2148 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2149 				cnt = 0;
2150 				if (laddr->ifa == NULL) {
2151 					continue;
2152 				}
2153 				if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2154 					continue;
2155 
2156 				if (sctp_is_address_in_scope(laddr->ifa,
2157 				    scope->ipv4_addr_legal,
2158 				    scope->ipv6_addr_legal,
2159 				    scope->loopback_scope,
2160 				    scope->ipv4_local_scope,
2161 				    scope->local_scope,
2162 				    scope->site_scope, 0) == 0) {
2163 					continue;
2164 				}
2165 				m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2166 				cnt++;
2167 				if (cnt >= SCTP_ADDRESS_LIMIT) {
2168 					break;
2169 				}
2170 			}
2171 		}
2172 	}
2173 	SCTP_IPI_ADDR_RUNLOCK();
2174 	return (m_at);
2175 }
2176 
2177 static struct sctp_ifa *
2178 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2179     uint8_t dest_is_loop,
2180     uint8_t dest_is_priv,
2181     sa_family_t fam)
2182 {
2183 	uint8_t dest_is_global = 0;
2184 
2185 	/* dest_is_priv is true if destination is a private address */
2186 	/* dest_is_loop is true if destination is a loopback addresses */
2187 
2188 	/*
2189 	 * Here we determine if its a preferred address. A preferred address
2190 	 * means it is the same scope or higher scope then the destination.
2191 	 * L = loopback, P = private, G = global
2192 	 * ----------------------------------------- src    |  dest | result
2193 	 * ---------------------------------------- L     |    L  |    yes
2194 	 * ----------------------------------------- P     |    L  |
2195 	 * yes-v4 no-v6 ----------------------------------------- G     |
2196 	 * L  |    yes-v4 no-v6 ----------------------------------------- L
2197 	 * |    P  |    no ----------------------------------------- P     |
2198 	 * P  |    yes ----------------------------------------- G     |
2199 	 * P  |    no ----------------------------------------- L     |    G
2200 	 * |    no ----------------------------------------- P     |    G  |
2201 	 * no ----------------------------------------- G     |    G  |
2202 	 * yes -----------------------------------------
2203 	 */
2204 
2205 	if (ifa->address.sa.sa_family != fam) {
2206 		/* forget mis-matched family */
2207 		return (NULL);
2208 	}
2209 	if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2210 		dest_is_global = 1;
2211 	}
2212 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2213 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2214 	/* Ok the address may be ok */
2215 	if (fam == AF_INET6) {
2216 		/* ok to use deprecated addresses? no lets not! */
2217 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2218 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2219 			return (NULL);
2220 		}
2221 		if (ifa->src_is_priv && !ifa->src_is_loop) {
2222 			if (dest_is_loop) {
2223 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2224 				return (NULL);
2225 			}
2226 		}
2227 		if (ifa->src_is_glob) {
2228 			if (dest_is_loop) {
2229 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2230 				return (NULL);
2231 			}
2232 		}
2233 	}
2234 	/*
2235 	 * Now that we know what is what, implement or table this could in
2236 	 * theory be done slicker (it used to be), but this is
2237 	 * straightforward and easier to validate :-)
2238 	 */
2239 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2240 	    ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2241 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2242 	    dest_is_loop, dest_is_priv, dest_is_global);
2243 
2244 	if ((ifa->src_is_loop) && (dest_is_priv)) {
2245 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2246 		return (NULL);
2247 	}
2248 	if ((ifa->src_is_glob) && (dest_is_priv)) {
2249 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2250 		return (NULL);
2251 	}
2252 	if ((ifa->src_is_loop) && (dest_is_global)) {
2253 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2254 		return (NULL);
2255 	}
2256 	if ((ifa->src_is_priv) && (dest_is_global)) {
2257 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2258 		return (NULL);
2259 	}
2260 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2261 	/* its a preferred address */
2262 	return (ifa);
2263 }
2264 
2265 static struct sctp_ifa *
2266 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2267     uint8_t dest_is_loop,
2268     uint8_t dest_is_priv,
2269     sa_family_t fam)
2270 {
2271 	uint8_t dest_is_global = 0;
2272 
2273 
2274 	/*
2275 	 * Here we determine if its a acceptable address. A acceptable
2276 	 * address means it is the same scope or higher scope but we can
2277 	 * allow for NAT which means its ok to have a global dest and a
2278 	 * private src.
2279 	 *
2280 	 * L = loopback, P = private, G = global
2281 	 * ----------------------------------------- src    |  dest | result
2282 	 * ----------------------------------------- L     |   L   |    yes
2283 	 * ----------------------------------------- P     |   L   |
2284 	 * yes-v4 no-v6 ----------------------------------------- G     |
2285 	 * L   |    yes ----------------------------------------- L     |
2286 	 * P   |    no ----------------------------------------- P     |   P
2287 	 * |    yes ----------------------------------------- G     |   P
2288 	 * |    yes - May not work -----------------------------------------
2289 	 * L     |   G   |    no ----------------------------------------- P
2290 	 * |   G   |    yes - May not work
2291 	 * ----------------------------------------- G     |   G   |    yes
2292 	 * -----------------------------------------
2293 	 */
2294 
2295 	if (ifa->address.sa.sa_family != fam) {
2296 		/* forget non matching family */
2297 		return (NULL);
2298 	}
2299 	/* Ok the address may be ok */
2300 	if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2301 		dest_is_global = 1;
2302 	}
2303 	if (fam == AF_INET6) {
2304 		/* ok to use deprecated addresses? */
2305 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2306 			return (NULL);
2307 		}
2308 		if (ifa->src_is_priv) {
2309 			/* Special case, linklocal to loop */
2310 			if (dest_is_loop)
2311 				return (NULL);
2312 		}
2313 	}
2314 	/*
2315 	 * Now that we know what is what, implement our table. This could in
2316 	 * theory be done slicker (it used to be), but this is
2317 	 * straightforward and easier to validate :-)
2318 	 */
2319 	if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2320 		return (NULL);
2321 	}
2322 	if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2323 		return (NULL);
2324 	}
2325 	/* its an acceptable address */
2326 	return (ifa);
2327 }
2328 
2329 int
2330 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2331 {
2332 	struct sctp_laddr *laddr;
2333 
2334 	if (stcb == NULL) {
2335 		/* There are no restrictions, no TCB :-) */
2336 		return (0);
2337 	}
2338 	LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2339 		if (laddr->ifa == NULL) {
2340 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2341 			    __FUNCTION__);
2342 			continue;
2343 		}
2344 		if (laddr->ifa == ifa) {
2345 			/* Yes it is on the list */
2346 			return (1);
2347 		}
2348 	}
2349 	return (0);
2350 }
2351 
2352 
2353 int
2354 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2355 {
2356 	struct sctp_laddr *laddr;
2357 
2358 	if (ifa == NULL)
2359 		return (0);
2360 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2361 		if (laddr->ifa == NULL) {
2362 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2363 			    __FUNCTION__);
2364 			continue;
2365 		}
2366 		if ((laddr->ifa == ifa) && laddr->action == 0)
2367 			/* same pointer */
2368 			return (1);
2369 	}
2370 	return (0);
2371 }
2372 
2373 
2374 
2375 static struct sctp_ifa *
2376 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2377     sctp_route_t * ro,
2378     uint32_t vrf_id,
2379     int non_asoc_addr_ok,
2380     uint8_t dest_is_priv,
2381     uint8_t dest_is_loop,
2382     sa_family_t fam)
2383 {
2384 	struct sctp_laddr *laddr, *starting_point;
2385 	void *ifn;
2386 	int resettotop = 0;
2387 	struct sctp_ifn *sctp_ifn;
2388 	struct sctp_ifa *sctp_ifa, *sifa;
2389 	struct sctp_vrf *vrf;
2390 	uint32_t ifn_index;
2391 
2392 	vrf = sctp_find_vrf(vrf_id);
2393 	if (vrf == NULL)
2394 		return (NULL);
2395 
2396 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2397 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2398 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2399 	/*
2400 	 * first question, is the ifn we will emit on in our list, if so, we
2401 	 * want such an address. Note that we first looked for a preferred
2402 	 * address.
2403 	 */
2404 	if (sctp_ifn) {
2405 		/* is a preferred one on the interface we route out? */
2406 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2407 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2408 			    (non_asoc_addr_ok == 0))
2409 				continue;
2410 			sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2411 			    dest_is_loop,
2412 			    dest_is_priv, fam);
2413 			if (sifa == NULL)
2414 				continue;
2415 			if (sctp_is_addr_in_ep(inp, sifa)) {
2416 				atomic_add_int(&sifa->refcount, 1);
2417 				return (sifa);
2418 			}
2419 		}
2420 	}
2421 	/*
2422 	 * ok, now we now need to find one on the list of the addresses. We
2423 	 * can't get one on the emitting interface so let's find first a
2424 	 * preferred one. If not that an acceptable one otherwise... we
2425 	 * return NULL.
2426 	 */
2427 	starting_point = inp->next_addr_touse;
2428 once_again:
2429 	if (inp->next_addr_touse == NULL) {
2430 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2431 		resettotop = 1;
2432 	}
2433 	for (laddr = inp->next_addr_touse; laddr;
2434 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2435 		if (laddr->ifa == NULL) {
2436 			/* address has been removed */
2437 			continue;
2438 		}
2439 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2440 			/* address is being deleted */
2441 			continue;
2442 		}
2443 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2444 		    dest_is_priv, fam);
2445 		if (sifa == NULL)
2446 			continue;
2447 		atomic_add_int(&sifa->refcount, 1);
2448 		return (sifa);
2449 	}
2450 	if (resettotop == 0) {
2451 		inp->next_addr_touse = NULL;
2452 		goto once_again;
2453 	}
2454 	inp->next_addr_touse = starting_point;
2455 	resettotop = 0;
2456 once_again_too:
2457 	if (inp->next_addr_touse == NULL) {
2458 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2459 		resettotop = 1;
2460 	}
2461 	/* ok, what about an acceptable address in the inp */
2462 	for (laddr = inp->next_addr_touse; laddr;
2463 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2464 		if (laddr->ifa == NULL) {
2465 			/* address has been removed */
2466 			continue;
2467 		}
2468 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2469 			/* address is being deleted */
2470 			continue;
2471 		}
2472 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2473 		    dest_is_priv, fam);
2474 		if (sifa == NULL)
2475 			continue;
2476 		atomic_add_int(&sifa->refcount, 1);
2477 		return (sifa);
2478 	}
2479 	if (resettotop == 0) {
2480 		inp->next_addr_touse = NULL;
2481 		goto once_again_too;
2482 	}
2483 	/*
2484 	 * no address bound can be a source for the destination we are in
2485 	 * trouble
2486 	 */
2487 	return (NULL);
2488 }
2489 
2490 
2491 
2492 static struct sctp_ifa *
2493 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2494     struct sctp_tcb *stcb,
2495     struct sctp_nets *net,
2496     sctp_route_t * ro,
2497     uint32_t vrf_id,
2498     uint8_t dest_is_priv,
2499     uint8_t dest_is_loop,
2500     int non_asoc_addr_ok,
2501     sa_family_t fam)
2502 {
2503 	struct sctp_laddr *laddr, *starting_point;
2504 	void *ifn;
2505 	struct sctp_ifn *sctp_ifn;
2506 	struct sctp_ifa *sctp_ifa, *sifa;
2507 	uint8_t start_at_beginning = 0;
2508 	struct sctp_vrf *vrf;
2509 	uint32_t ifn_index;
2510 
2511 	/*
2512 	 * first question, is the ifn we will emit on in our list, if so, we
2513 	 * want that one.
2514 	 */
2515 	vrf = sctp_find_vrf(vrf_id);
2516 	if (vrf == NULL)
2517 		return (NULL);
2518 
2519 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2520 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2521 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2522 
2523 	/*
2524 	 * first question, is the ifn we will emit on in our list?  If so,
2525 	 * we want that one. First we look for a preferred. Second, we go
2526 	 * for an acceptable.
2527 	 */
2528 	if (sctp_ifn) {
2529 		/* first try for a preferred address on the ep */
2530 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2531 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2532 				continue;
2533 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2534 				sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2535 				if (sifa == NULL)
2536 					continue;
2537 				if (((non_asoc_addr_ok == 0) &&
2538 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2539 				    (non_asoc_addr_ok &&
2540 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2541 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2542 					/* on the no-no list */
2543 					continue;
2544 				}
2545 				atomic_add_int(&sifa->refcount, 1);
2546 				return (sifa);
2547 			}
2548 		}
2549 		/* next try for an acceptable address on the ep */
2550 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2551 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2552 				continue;
2553 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2554 				sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2555 				if (sifa == NULL)
2556 					continue;
2557 				if (((non_asoc_addr_ok == 0) &&
2558 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2559 				    (non_asoc_addr_ok &&
2560 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2561 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2562 					/* on the no-no list */
2563 					continue;
2564 				}
2565 				atomic_add_int(&sifa->refcount, 1);
2566 				return (sifa);
2567 			}
2568 		}
2569 
2570 	}
2571 	/*
2572 	 * if we can't find one like that then we must look at all addresses
2573 	 * bound to pick one at first preferable then secondly acceptable.
2574 	 */
2575 	starting_point = stcb->asoc.last_used_address;
2576 sctp_from_the_top:
2577 	if (stcb->asoc.last_used_address == NULL) {
2578 		start_at_beginning = 1;
2579 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2580 	}
2581 	/* search beginning with the last used address */
2582 	for (laddr = stcb->asoc.last_used_address; laddr;
2583 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2584 		if (laddr->ifa == NULL) {
2585 			/* address has been removed */
2586 			continue;
2587 		}
2588 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2589 			/* address is being deleted */
2590 			continue;
2591 		}
2592 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2593 		if (sifa == NULL)
2594 			continue;
2595 		if (((non_asoc_addr_ok == 0) &&
2596 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2597 		    (non_asoc_addr_ok &&
2598 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2599 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2600 			/* on the no-no list */
2601 			continue;
2602 		}
2603 		stcb->asoc.last_used_address = laddr;
2604 		atomic_add_int(&sifa->refcount, 1);
2605 		return (sifa);
2606 	}
2607 	if (start_at_beginning == 0) {
2608 		stcb->asoc.last_used_address = NULL;
2609 		goto sctp_from_the_top;
2610 	}
2611 	/* now try for any higher scope than the destination */
2612 	stcb->asoc.last_used_address = starting_point;
2613 	start_at_beginning = 0;
2614 sctp_from_the_top2:
2615 	if (stcb->asoc.last_used_address == NULL) {
2616 		start_at_beginning = 1;
2617 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2618 	}
2619 	/* search beginning with the last used address */
2620 	for (laddr = stcb->asoc.last_used_address; laddr;
2621 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2622 		if (laddr->ifa == NULL) {
2623 			/* address has been removed */
2624 			continue;
2625 		}
2626 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2627 			/* address is being deleted */
2628 			continue;
2629 		}
2630 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2631 		    dest_is_priv, fam);
2632 		if (sifa == NULL)
2633 			continue;
2634 		if (((non_asoc_addr_ok == 0) &&
2635 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2636 		    (non_asoc_addr_ok &&
2637 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2638 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2639 			/* on the no-no list */
2640 			continue;
2641 		}
2642 		stcb->asoc.last_used_address = laddr;
2643 		atomic_add_int(&sifa->refcount, 1);
2644 		return (sifa);
2645 	}
2646 	if (start_at_beginning == 0) {
2647 		stcb->asoc.last_used_address = NULL;
2648 		goto sctp_from_the_top2;
2649 	}
2650 	return (NULL);
2651 }
2652 
2653 static struct sctp_ifa *
2654 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2655     struct sctp_tcb *stcb,
2656     int non_asoc_addr_ok,
2657     uint8_t dest_is_loop,
2658     uint8_t dest_is_priv,
2659     int addr_wanted,
2660     sa_family_t fam,
2661     sctp_route_t * ro
2662 )
2663 {
2664 	struct sctp_ifa *ifa, *sifa;
2665 	int num_eligible_addr = 0;
2666 
2667 #ifdef INET6
2668 	struct sockaddr_in6 sin6, lsa6;
2669 
2670 	if (fam == AF_INET6) {
2671 		memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2672 		(void)sa6_recoverscope(&sin6);
2673 	}
2674 #endif				/* INET6 */
2675 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2676 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2677 		    (non_asoc_addr_ok == 0))
2678 			continue;
2679 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2680 		    dest_is_priv, fam);
2681 		if (sifa == NULL)
2682 			continue;
2683 #ifdef INET6
2684 		if (fam == AF_INET6 &&
2685 		    dest_is_loop &&
2686 		    sifa->src_is_loop && sifa->src_is_priv) {
2687 			/*
2688 			 * don't allow fe80::1 to be a src on loop ::1, we
2689 			 * don't list it to the peer so we will get an
2690 			 * abort.
2691 			 */
2692 			continue;
2693 		}
2694 		if (fam == AF_INET6 &&
2695 		    IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2696 		    IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2697 			/*
2698 			 * link-local <-> link-local must belong to the same
2699 			 * scope.
2700 			 */
2701 			memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2702 			(void)sa6_recoverscope(&lsa6);
2703 			if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2704 				continue;
2705 			}
2706 		}
2707 #endif				/* INET6 */
2708 
2709 		/*
2710 		 * Check if the IPv6 address matches to next-hop. In the
2711 		 * mobile case, old IPv6 address may be not deleted from the
2712 		 * interface. Then, the interface has previous and new
2713 		 * addresses.  We should use one corresponding to the
2714 		 * next-hop.  (by micchie)
2715 		 */
2716 #ifdef INET6
2717 		if (stcb && fam == AF_INET6 &&
2718 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2719 			if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2720 			    == 0) {
2721 				continue;
2722 			}
2723 		}
2724 #endif
2725 		/* Avoid topologically incorrect IPv4 address */
2726 		if (stcb && fam == AF_INET &&
2727 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2728 			if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2729 				continue;
2730 			}
2731 		}
2732 		if (stcb) {
2733 			if (((non_asoc_addr_ok == 0) &&
2734 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2735 			    (non_asoc_addr_ok &&
2736 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2737 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2738 				/*
2739 				 * It is restricted for some reason..
2740 				 * probably not yet added.
2741 				 */
2742 				continue;
2743 			}
2744 		}
2745 		if (num_eligible_addr >= addr_wanted) {
2746 			return (sifa);
2747 		}
2748 		num_eligible_addr++;
2749 	}
2750 	return (NULL);
2751 }
2752 
2753 
2754 static int
2755 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2756     struct sctp_tcb *stcb,
2757     int non_asoc_addr_ok,
2758     uint8_t dest_is_loop,
2759     uint8_t dest_is_priv,
2760     sa_family_t fam)
2761 {
2762 	struct sctp_ifa *ifa, *sifa;
2763 	int num_eligible_addr = 0;
2764 
2765 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2766 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2767 		    (non_asoc_addr_ok == 0)) {
2768 			continue;
2769 		}
2770 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2771 		    dest_is_priv, fam);
2772 		if (sifa == NULL) {
2773 			continue;
2774 		}
2775 		if (stcb) {
2776 			if (((non_asoc_addr_ok == 0) &&
2777 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2778 			    (non_asoc_addr_ok &&
2779 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2780 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2781 				/*
2782 				 * It is restricted for some reason..
2783 				 * probably not yet added.
2784 				 */
2785 				continue;
2786 			}
2787 		}
2788 		num_eligible_addr++;
2789 	}
2790 	return (num_eligible_addr);
2791 }
2792 
2793 static struct sctp_ifa *
2794 sctp_choose_boundall(struct sctp_inpcb *inp,
2795     struct sctp_tcb *stcb,
2796     struct sctp_nets *net,
2797     sctp_route_t * ro,
2798     uint32_t vrf_id,
2799     uint8_t dest_is_priv,
2800     uint8_t dest_is_loop,
2801     int non_asoc_addr_ok,
2802     sa_family_t fam)
2803 {
2804 	int cur_addr_num = 0, num_preferred = 0;
2805 	void *ifn;
2806 	struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2807 	struct sctp_ifa *sctp_ifa, *sifa;
2808 	uint32_t ifn_index;
2809 	struct sctp_vrf *vrf;
2810 
2811 	/*-
2812 	 * For boundall we can use any address in the association.
2813 	 * If non_asoc_addr_ok is set we can use any address (at least in
2814 	 * theory). So we look for preferred addresses first. If we find one,
2815 	 * we use it. Otherwise we next try to get an address on the
2816 	 * interface, which we should be able to do (unless non_asoc_addr_ok
2817 	 * is false and we are routed out that way). In these cases where we
2818 	 * can't use the address of the interface we go through all the
2819 	 * ifn's looking for an address we can use and fill that in. Punting
2820 	 * means we send back address 0, which will probably cause problems
2821 	 * actually since then IP will fill in the address of the route ifn,
2822 	 * which means we probably already rejected it.. i.e. here comes an
2823 	 * abort :-<.
2824 	 */
2825 	vrf = sctp_find_vrf(vrf_id);
2826 	if (vrf == NULL)
2827 		return (NULL);
2828 
2829 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2830 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2831 	emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2832 	if (sctp_ifn == NULL) {
2833 		/* ?? We don't have this guy ?? */
2834 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2835 		goto bound_all_plan_b;
2836 	}
2837 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2838 	    ifn_index, sctp_ifn->ifn_name);
2839 
2840 	if (net) {
2841 		cur_addr_num = net->indx_of_eligible_next_to_use;
2842 	}
2843 	num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2844 	    stcb,
2845 	    non_asoc_addr_ok,
2846 	    dest_is_loop,
2847 	    dest_is_priv, fam);
2848 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2849 	    num_preferred, sctp_ifn->ifn_name);
2850 	if (num_preferred == 0) {
2851 		/*
2852 		 * no eligible addresses, we must use some other interface
2853 		 * address if we can find one.
2854 		 */
2855 		goto bound_all_plan_b;
2856 	}
2857 	/*
2858 	 * Ok we have num_eligible_addr set with how many we can use, this
2859 	 * may vary from call to call due to addresses being deprecated
2860 	 * etc..
2861 	 */
2862 	if (cur_addr_num >= num_preferred) {
2863 		cur_addr_num = 0;
2864 	}
2865 	/*
2866 	 * select the nth address from the list (where cur_addr_num is the
2867 	 * nth) and 0 is the first one, 1 is the second one etc...
2868 	 */
2869 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2870 
2871 	sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2872 	    dest_is_priv, cur_addr_num, fam, ro);
2873 
2874 	/* if sctp_ifa is NULL something changed??, fall to plan b. */
2875 	if (sctp_ifa) {
2876 		atomic_add_int(&sctp_ifa->refcount, 1);
2877 		if (net) {
2878 			/* save off where the next one we will want */
2879 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2880 		}
2881 		return (sctp_ifa);
2882 	}
2883 	/*
2884 	 * plan_b: Look at all interfaces and find a preferred address. If
2885 	 * no preferred fall through to plan_c.
2886 	 */
2887 bound_all_plan_b:
2888 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2889 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2890 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2891 		    sctp_ifn->ifn_name);
2892 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2893 			/* wrong base scope */
2894 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2895 			continue;
2896 		}
2897 		if ((sctp_ifn == looked_at) && looked_at) {
2898 			/* already looked at this guy */
2899 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2900 			continue;
2901 		}
2902 		num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2903 		    dest_is_loop, dest_is_priv, fam);
2904 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
2905 		    "Found ifn:%p %d preferred source addresses\n",
2906 		    ifn, num_preferred);
2907 		if (num_preferred == 0) {
2908 			/* None on this interface. */
2909 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2910 			continue;
2911 		}
2912 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
2913 		    "num preferred:%d on interface:%p cur_addr_num:%d\n",
2914 		    num_preferred, sctp_ifn, cur_addr_num);
2915 
2916 		/*
2917 		 * Ok we have num_eligible_addr set with how many we can
2918 		 * use, this may vary from call to call due to addresses
2919 		 * being deprecated etc..
2920 		 */
2921 		if (cur_addr_num >= num_preferred) {
2922 			cur_addr_num = 0;
2923 		}
2924 		sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2925 		    dest_is_priv, cur_addr_num, fam, ro);
2926 		if (sifa == NULL)
2927 			continue;
2928 		if (net) {
2929 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2930 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
2931 			    cur_addr_num);
2932 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
2933 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
2934 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
2935 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
2936 		}
2937 		atomic_add_int(&sifa->refcount, 1);
2938 		return (sifa);
2939 
2940 	}
2941 
2942 	/* plan_c: do we have an acceptable address on the emit interface */
2943 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
2944 	if (emit_ifn == NULL) {
2945 		goto plan_d;
2946 	}
2947 	LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
2948 		if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2949 		    (non_asoc_addr_ok == 0))
2950 			continue;
2951 		sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
2952 		    dest_is_priv, fam);
2953 		if (sifa == NULL)
2954 			continue;
2955 		if (stcb) {
2956 			if (((non_asoc_addr_ok == 0) &&
2957 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2958 			    (non_asoc_addr_ok &&
2959 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2960 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2961 				/*
2962 				 * It is restricted for some reason..
2963 				 * probably not yet added.
2964 				 */
2965 				continue;
2966 			}
2967 		}
2968 		atomic_add_int(&sifa->refcount, 1);
2969 		return (sifa);
2970 	}
2971 plan_d:
2972 	/*
2973 	 * plan_d: We are in trouble. No preferred address on the emit
2974 	 * interface. And not even a preferred address on all interfaces. Go
2975 	 * out and see if we can find an acceptable address somewhere
2976 	 * amongst all interfaces.
2977 	 */
2978 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
2979 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2980 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2981 			/* wrong base scope */
2982 			continue;
2983 		}
2984 		if ((sctp_ifn == looked_at) && looked_at)
2985 			/* already looked at this guy */
2986 			continue;
2987 
2988 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2989 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2990 			    (non_asoc_addr_ok == 0))
2991 				continue;
2992 			sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
2993 			    dest_is_loop,
2994 			    dest_is_priv, fam);
2995 			if (sifa == NULL)
2996 				continue;
2997 			if (stcb) {
2998 				if (((non_asoc_addr_ok == 0) &&
2999 				    (sctp_is_addr_restricted(stcb, sifa))) ||
3000 				    (non_asoc_addr_ok &&
3001 				    (sctp_is_addr_restricted(stcb, sifa)) &&
3002 				    (!sctp_is_addr_pending(stcb, sifa)))) {
3003 					/*
3004 					 * It is restricted for some
3005 					 * reason.. probably not yet added.
3006 					 */
3007 					continue;
3008 				}
3009 			}
3010 			atomic_add_int(&sifa->refcount, 1);
3011 			return (sifa);
3012 		}
3013 	}
3014 	/*
3015 	 * Ok we can find NO address to source from that is not on our
3016 	 * restricted list and non_asoc_address is NOT ok, or it is on our
3017 	 * restricted list. We can't source to it :-(
3018 	 */
3019 	return (NULL);
3020 }
3021 
3022 
3023 
3024 /* tcb may be NULL */
3025 struct sctp_ifa *
3026 sctp_source_address_selection(struct sctp_inpcb *inp,
3027     struct sctp_tcb *stcb,
3028     sctp_route_t * ro,
3029     struct sctp_nets *net,
3030     int non_asoc_addr_ok, uint32_t vrf_id)
3031 {
3032 	struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3033 
3034 #ifdef INET6
3035 	struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3036 
3037 #endif
3038 	struct sctp_ifa *answer;
3039 	uint8_t dest_is_priv, dest_is_loop;
3040 	sa_family_t fam;
3041 
3042 	/*-
3043 	 * Rules: - Find the route if needed, cache if I can. - Look at
3044 	 * interface address in route, Is it in the bound list. If so we
3045 	 * have the best source. - If not we must rotate amongst the
3046 	 * addresses.
3047 	 *
3048 	 * Cavets and issues
3049 	 *
3050 	 * Do we need to pay attention to scope. We can have a private address
3051 	 * or a global address we are sourcing or sending to. So if we draw
3052 	 * it out
3053 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3054 	 * For V4
3055          *------------------------------------------
3056 	 *      source     *      dest  *  result
3057 	 * -----------------------------------------
3058 	 * <a>  Private    *    Global  *  NAT
3059 	 * -----------------------------------------
3060 	 * <b>  Private    *    Private *  No problem
3061 	 * -----------------------------------------
3062          * <c>  Global     *    Private *  Huh, How will this work?
3063 	 * -----------------------------------------
3064          * <d>  Global     *    Global  *  No Problem
3065          *------------------------------------------
3066 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3067 	 * For V6
3068          *------------------------------------------
3069 	 *      source     *      dest  *  result
3070 	 * -----------------------------------------
3071 	 * <a>  Linklocal  *    Global  *
3072 	 * -----------------------------------------
3073 	 * <b>  Linklocal  * Linklocal  *  No problem
3074 	 * -----------------------------------------
3075          * <c>  Global     * Linklocal  *  Huh, How will this work?
3076 	 * -----------------------------------------
3077          * <d>  Global     *    Global  *  No Problem
3078          *------------------------------------------
3079 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3080          *
3081 	 * And then we add to that what happens if there are multiple addresses
3082 	 * assigned to an interface. Remember the ifa on a ifn is a linked
3083 	 * list of addresses. So one interface can have more than one IP
3084 	 * address. What happens if we have both a private and a global
3085 	 * address? Do we then use context of destination to sort out which
3086 	 * one is best? And what about NAT's sending P->G may get you a NAT
3087 	 * translation, or should you select the G thats on the interface in
3088 	 * preference.
3089 	 *
3090 	 * Decisions:
3091 	 *
3092 	 * - count the number of addresses on the interface.
3093          * - if it is one, no problem except case <c>.
3094          *   For <a> we will assume a NAT out there.
3095 	 * - if there are more than one, then we need to worry about scope P
3096 	 *   or G. We should prefer G -> G and P -> P if possible.
3097 	 *   Then as a secondary fall back to mixed types G->P being a last
3098 	 *   ditch one.
3099          * - The above all works for bound all, but bound specific we need to
3100 	 *   use the same concept but instead only consider the bound
3101 	 *   addresses. If the bound set is NOT assigned to the interface then
3102 	 *   we must use rotation amongst the bound addresses..
3103 	 */
3104 	if (ro->ro_rt == NULL) {
3105 		/*
3106 		 * Need a route to cache.
3107 		 */
3108 		SCTP_RTALLOC(ro, vrf_id);
3109 	}
3110 	if (ro->ro_rt == NULL) {
3111 		return (NULL);
3112 	}
3113 	fam = to->sin_family;
3114 	dest_is_priv = dest_is_loop = 0;
3115 	/* Setup our scopes for the destination */
3116 	switch (fam) {
3117 	case AF_INET:
3118 		/* Scope based on outbound address */
3119 		if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3120 			dest_is_loop = 1;
3121 			if (net != NULL) {
3122 				/* mark it as local */
3123 				net->addr_is_local = 1;
3124 			}
3125 		} else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3126 			dest_is_priv = 1;
3127 		}
3128 		break;
3129 #ifdef INET6
3130 	case AF_INET6:
3131 		/* Scope based on outbound address */
3132 		if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3133 		    SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3134 			/*
3135 			 * If the address is a loopback address, which
3136 			 * consists of "::1" OR "fe80::1%lo0", we are
3137 			 * loopback scope. But we don't use dest_is_priv
3138 			 * (link local addresses).
3139 			 */
3140 			dest_is_loop = 1;
3141 			if (net != NULL) {
3142 				/* mark it as local */
3143 				net->addr_is_local = 1;
3144 			}
3145 		} else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3146 			dest_is_priv = 1;
3147 		}
3148 		break;
3149 #endif
3150 	}
3151 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3152 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
3153 	SCTP_IPI_ADDR_RLOCK();
3154 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3155 		/*
3156 		 * Bound all case
3157 		 */
3158 		answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3159 		    dest_is_priv, dest_is_loop,
3160 		    non_asoc_addr_ok, fam);
3161 		SCTP_IPI_ADDR_RUNLOCK();
3162 		return (answer);
3163 	}
3164 	/*
3165 	 * Subset bound case
3166 	 */
3167 	if (stcb) {
3168 		answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
3169 		    vrf_id, dest_is_priv,
3170 		    dest_is_loop,
3171 		    non_asoc_addr_ok, fam);
3172 	} else {
3173 		answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3174 		    non_asoc_addr_ok,
3175 		    dest_is_priv,
3176 		    dest_is_loop, fam);
3177 	}
3178 	SCTP_IPI_ADDR_RUNLOCK();
3179 	return (answer);
3180 }
3181 
3182 static int
3183 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
3184 {
3185 	struct cmsghdr cmh;
3186 	int tlen, at;
3187 
3188 	tlen = SCTP_BUF_LEN(control);
3189 	at = 0;
3190 	/*
3191 	 * Independent of how many mbufs, find the c_type inside the control
3192 	 * structure and copy out the data.
3193 	 */
3194 	while (at < tlen) {
3195 		if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3196 			/* not enough room for one more we are done. */
3197 			return (0);
3198 		}
3199 		m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3200 		if (((int)cmh.cmsg_len + at) > tlen) {
3201 			/*
3202 			 * this is real messed up since there is not enough
3203 			 * data here to cover the cmsg header. We are done.
3204 			 */
3205 			return (0);
3206 		}
3207 		if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3208 		    (c_type == cmh.cmsg_type)) {
3209 			/* found the one we want, copy it out */
3210 			at += CMSG_ALIGN(sizeof(struct cmsghdr));
3211 			if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3212 				/*
3213 				 * space of cmsg_len after header not big
3214 				 * enough
3215 				 */
3216 				return (0);
3217 			}
3218 			m_copydata(control, at, cpsize, data);
3219 			return (1);
3220 		} else {
3221 			at += CMSG_ALIGN(cmh.cmsg_len);
3222 			if (cmh.cmsg_len == 0) {
3223 				break;
3224 			}
3225 		}
3226 	}
3227 	/* not found */
3228 	return (0);
3229 }
3230 
3231 static struct mbuf *
3232 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
3233     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3234 {
3235 	struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3236 	struct sctp_state_cookie *stc;
3237 	struct sctp_paramhdr *ph;
3238 	uint8_t *foo;
3239 	int sig_offset;
3240 	uint16_t cookie_sz;
3241 
3242 	mret = NULL;
3243 	mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3244 	    sizeof(struct sctp_paramhdr)), 0,
3245 	    M_DONTWAIT, 1, MT_DATA);
3246 	if (mret == NULL) {
3247 		return (NULL);
3248 	}
3249 	copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3250 	if (copy_init == NULL) {
3251 		sctp_m_freem(mret);
3252 		return (NULL);
3253 	}
3254 #ifdef SCTP_MBUF_LOGGING
3255 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3256 		struct mbuf *mat;
3257 
3258 		mat = copy_init;
3259 		while (mat) {
3260 			if (SCTP_BUF_IS_EXTENDED(mat)) {
3261 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3262 			}
3263 			mat = SCTP_BUF_NEXT(mat);
3264 		}
3265 	}
3266 #endif
3267 	copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3268 	    M_DONTWAIT);
3269 	if (copy_initack == NULL) {
3270 		sctp_m_freem(mret);
3271 		sctp_m_freem(copy_init);
3272 		return (NULL);
3273 	}
3274 #ifdef SCTP_MBUF_LOGGING
3275 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3276 		struct mbuf *mat;
3277 
3278 		mat = copy_initack;
3279 		while (mat) {
3280 			if (SCTP_BUF_IS_EXTENDED(mat)) {
3281 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3282 			}
3283 			mat = SCTP_BUF_NEXT(mat);
3284 		}
3285 	}
3286 #endif
3287 	/* easy side we just drop it on the end */
3288 	ph = mtod(mret, struct sctp_paramhdr *);
3289 	SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3290 	    sizeof(struct sctp_paramhdr);
3291 	stc = (struct sctp_state_cookie *)((caddr_t)ph +
3292 	    sizeof(struct sctp_paramhdr));
3293 	ph->param_type = htons(SCTP_STATE_COOKIE);
3294 	ph->param_length = 0;	/* fill in at the end */
3295 	/* Fill in the stc cookie data */
3296 	memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3297 
3298 	/* tack the INIT and then the INIT-ACK onto the chain */
3299 	cookie_sz = 0;
3300 	m_at = mret;
3301 	for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3302 		cookie_sz += SCTP_BUF_LEN(m_at);
3303 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3304 			SCTP_BUF_NEXT(m_at) = copy_init;
3305 			break;
3306 		}
3307 	}
3308 
3309 	for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3310 		cookie_sz += SCTP_BUF_LEN(m_at);
3311 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3312 			SCTP_BUF_NEXT(m_at) = copy_initack;
3313 			break;
3314 		}
3315 	}
3316 
3317 	for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3318 		cookie_sz += SCTP_BUF_LEN(m_at);
3319 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3320 			break;
3321 		}
3322 	}
3323 	sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3324 	if (sig == NULL) {
3325 		/* no space, so free the entire chain */
3326 		sctp_m_freem(mret);
3327 		return (NULL);
3328 	}
3329 	SCTP_BUF_LEN(sig) = 0;
3330 	SCTP_BUF_NEXT(m_at) = sig;
3331 	sig_offset = 0;
3332 	foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3333 	memset(foo, 0, SCTP_SIGNATURE_SIZE);
3334 	*signature = foo;
3335 	SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3336 	cookie_sz += SCTP_SIGNATURE_SIZE;
3337 	ph->param_length = htons(cookie_sz);
3338 	return (mret);
3339 }
3340 
3341 
3342 static uint8_t
3343 sctp_get_ect(struct sctp_tcb *stcb,
3344     struct sctp_tmit_chunk *chk)
3345 {
3346 	uint8_t this_random;
3347 
3348 	/* Huh? */
3349 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 0)
3350 		return (0);
3351 
3352 	if (SCTP_BASE_SYSCTL(sctp_ecn_nonce) == 0)
3353 		/* no nonce, always return ECT0 */
3354 		return (SCTP_ECT0_BIT);
3355 
3356 	if (stcb->asoc.peer_supports_ecn_nonce == 0) {
3357 		/* Peer does NOT support it, so we send a ECT0 only */
3358 		return (SCTP_ECT0_BIT);
3359 	}
3360 	if (chk == NULL)
3361 		return (SCTP_ECT0_BIT);
3362 
3363 	if ((stcb->asoc.hb_random_idx > 3) ||
3364 	    ((stcb->asoc.hb_random_idx == 3) &&
3365 	    (stcb->asoc.hb_ect_randombit > 7))) {
3366 		uint32_t rndval;
3367 
3368 warp_drive_sa:
3369 		rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
3370 		memcpy(stcb->asoc.hb_random_values, &rndval,
3371 		    sizeof(stcb->asoc.hb_random_values));
3372 		this_random = stcb->asoc.hb_random_values[0];
3373 		stcb->asoc.hb_random_idx = 0;
3374 		stcb->asoc.hb_ect_randombit = 0;
3375 	} else {
3376 		if (stcb->asoc.hb_ect_randombit > 7) {
3377 			stcb->asoc.hb_ect_randombit = 0;
3378 			stcb->asoc.hb_random_idx++;
3379 			if (stcb->asoc.hb_random_idx > 3) {
3380 				goto warp_drive_sa;
3381 			}
3382 		}
3383 		this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
3384 	}
3385 	if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
3386 		if (chk != NULL)
3387 			/* ECN Nonce stuff */
3388 			chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
3389 		stcb->asoc.hb_ect_randombit++;
3390 		return (SCTP_ECT1_BIT);
3391 	} else {
3392 		stcb->asoc.hb_ect_randombit++;
3393 		return (SCTP_ECT0_BIT);
3394 	}
3395 }
3396 
3397 static int
3398 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3399     struct sctp_tcb *stcb,	/* may be NULL */
3400     struct sctp_nets *net,
3401     struct sockaddr *to,
3402     struct mbuf *m,
3403     uint32_t auth_offset,
3404     struct sctp_auth_chunk *auth,
3405     int nofragment_flag,
3406     int ecn_ok,
3407     struct sctp_tmit_chunk *chk,
3408     int out_of_asoc_ok,
3409     uint16_t port,
3410     int so_locked,
3411 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3412     SCTP_UNUSED
3413 #endif
3414     union sctp_sockstore *over_addr
3415 )
3416 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3417 {
3418 	/*
3419 	 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
3420 	 * header WITH an SCTPHDR but no IP header, endpoint inp and sa
3421 	 * structure: - fill in the HMAC digest of any AUTH chunk in the
3422 	 * packet. - calculate and fill in the SCTP checksum. - prepend an
3423 	 * IP address header. - if boundall use INADDR_ANY. - if
3424 	 * boundspecific do source address selection. - set fragmentation
3425 	 * option for ipV4. - On return from IP output, check/adjust mtu
3426 	 * size of output interface and smallest_mtu size as well.
3427 	 */
3428 	/* Will need ifdefs around this */
3429 	struct mbuf *o_pak;
3430 	struct mbuf *newm;
3431 	struct sctphdr *sctphdr;
3432 	int packet_length;
3433 	uint32_t csum;
3434 	int ret;
3435 	uint32_t vrf_id;
3436 	sctp_route_t *ro = NULL;
3437 	struct udphdr *udp = NULL;
3438 
3439 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3440 	struct socket *so = NULL;
3441 
3442 #endif
3443 
3444 	if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3445 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3446 		sctp_m_freem(m);
3447 		return (EFAULT);
3448 	}
3449 	if (stcb) {
3450 		vrf_id = stcb->asoc.vrf_id;
3451 	} else {
3452 		vrf_id = inp->def_vrf_id;
3453 	}
3454 
3455 	/* fill in the HMAC digest for any AUTH chunk in the packet */
3456 	if ((auth != NULL) && (stcb != NULL)) {
3457 		sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb);
3458 	}
3459 	/* Calculate the csum and fill in the length of the packet */
3460 	sctphdr = mtod(m, struct sctphdr *);
3461 	if (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3462 	    (stcb) &&
3463 	    (to->sa_family == AF_INET) &&
3464 	    (stcb->asoc.loopback_scope)) {
3465 		sctphdr->checksum = 0;
3466 		/*
3467 		 * This can probably now be taken out since my audit shows
3468 		 * no more bad pktlen's coming in. But we will wait a while
3469 		 * yet.
3470 		 */
3471 		packet_length = sctp_calculate_len(m);
3472 	} else {
3473 		sctphdr->checksum = 0;
3474 		csum = sctp_calculate_sum(m, &packet_length, 0);
3475 		sctphdr->checksum = csum;
3476 	}
3477 
3478 	if (to->sa_family == AF_INET) {
3479 		struct ip *ip = NULL;
3480 		sctp_route_t iproute;
3481 		uint8_t tos_value;
3482 
3483 		if (port) {
3484 			newm = sctp_get_mbuf_for_msg(sizeof(struct ip) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
3485 		} else {
3486 			newm = sctp_get_mbuf_for_msg(sizeof(struct ip), 1, M_DONTWAIT, 1, MT_DATA);
3487 		}
3488 		if (newm == NULL) {
3489 			sctp_m_freem(m);
3490 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3491 			return (ENOMEM);
3492 		}
3493 		if (port) {
3494 			SCTP_ALIGN_TO_END(newm, sizeof(struct ip) + sizeof(struct udphdr));
3495 			SCTP_BUF_LEN(newm) = sizeof(struct ip) + sizeof(struct udphdr);
3496 			packet_length += sizeof(struct ip) + sizeof(struct udphdr);
3497 		} else {
3498 			SCTP_ALIGN_TO_END(newm, sizeof(struct ip));
3499 			SCTP_BUF_LEN(newm) = sizeof(struct ip);
3500 			packet_length += sizeof(struct ip);
3501 		}
3502 		SCTP_BUF_NEXT(newm) = m;
3503 		m = newm;
3504 		ip = mtod(m, struct ip *);
3505 		ip->ip_v = IPVERSION;
3506 		ip->ip_hl = (sizeof(struct ip) >> 2);
3507 		if (net) {
3508 			tos_value = net->tos_flowlabel & 0x000000ff;
3509 		} else {
3510 			tos_value = inp->ip_inp.inp.inp_ip_tos;
3511 		}
3512 		if ((nofragment_flag) && (port == 0)) {
3513 #if defined(WITH_CONVERT_IP_OFF) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
3514 			ip->ip_off = IP_DF;
3515 #else
3516 			ip->ip_off = htons(IP_DF);
3517 #endif
3518 		} else
3519 			ip->ip_off = 0;
3520 
3521 		/* FreeBSD has a function for ip_id's */
3522 		ip->ip_id = ip_newid();
3523 
3524 		ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3525 		ip->ip_len = packet_length;
3526 		if (stcb) {
3527 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3528 				/* Enable ECN */
3529 				ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
3530 			} else {
3531 				/* No ECN */
3532 				ip->ip_tos = (u_char)(tos_value & 0xfc);
3533 			}
3534 		} else {
3535 			/* no association at all */
3536 			ip->ip_tos = (tos_value & 0xfc);
3537 		}
3538 		if (port) {
3539 			ip->ip_p = IPPROTO_UDP;
3540 		} else {
3541 			ip->ip_p = IPPROTO_SCTP;
3542 		}
3543 		ip->ip_sum = 0;
3544 		if (net == NULL) {
3545 			ro = &iproute;
3546 			memset(&iproute, 0, sizeof(iproute));
3547 			memcpy(&ro->ro_dst, to, to->sa_len);
3548 		} else {
3549 			ro = (sctp_route_t *) & net->ro;
3550 		}
3551 		/* Now the address selection part */
3552 		ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
3553 
3554 		/* call the routine to select the src address */
3555 		if (net && out_of_asoc_ok == 0) {
3556 			if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3557 				sctp_free_ifa(net->ro._s_addr);
3558 				net->ro._s_addr = NULL;
3559 				net->src_addr_selected = 0;
3560 				if (ro->ro_rt) {
3561 					RTFREE(ro->ro_rt);
3562 					ro->ro_rt = NULL;
3563 				}
3564 			}
3565 			if (net->src_addr_selected == 0) {
3566 				/* Cache the source address */
3567 				net->ro._s_addr = sctp_source_address_selection(inp, stcb,
3568 				    ro, net, 0,
3569 				    vrf_id);
3570 				net->src_addr_selected = 1;
3571 			}
3572 			if (net->ro._s_addr == NULL) {
3573 				/* No route to host */
3574 				net->src_addr_selected = 0;
3575 				goto no_route;
3576 			}
3577 			ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
3578 		} else {
3579 			if (over_addr == NULL) {
3580 				struct sctp_ifa *_lsrc;
3581 
3582 				_lsrc = sctp_source_address_selection(inp, stcb, ro,
3583 				    net,
3584 				    out_of_asoc_ok,
3585 				    vrf_id);
3586 				if (_lsrc == NULL) {
3587 					goto no_route;
3588 				}
3589 				ip->ip_src = _lsrc->address.sin.sin_addr;
3590 				sctp_free_ifa(_lsrc);
3591 			} else {
3592 				ip->ip_src = over_addr->sin.sin_addr;
3593 				SCTP_RTALLOC((&ro->ro_rt), vrf_id);
3594 			}
3595 		}
3596 		if (port) {
3597 			udp = (struct udphdr *)(ip + 1);
3598 			udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3599 			udp->uh_dport = port;
3600 			udp->uh_ulen = htons(packet_length - sizeof(struct ip));
3601 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
3602 		}
3603 		/*
3604 		 * If source address selection fails and we find no route
3605 		 * then the ip_output should fail as well with a
3606 		 * NO_ROUTE_TO_HOST type error. We probably should catch
3607 		 * that somewhere and abort the association right away
3608 		 * (assuming this is an INIT being sent).
3609 		 */
3610 		if ((ro->ro_rt == NULL)) {
3611 			/*
3612 			 * src addr selection failed to find a route (or
3613 			 * valid source addr), so we can't get there from
3614 			 * here (yet)!
3615 			 */
3616 	no_route:
3617 			SCTPDBG(SCTP_DEBUG_OUTPUT1,
3618 			    "%s: dropped packet - no valid source addr\n",
3619 			    __FUNCTION__);
3620 			if (net) {
3621 				SCTPDBG(SCTP_DEBUG_OUTPUT1,
3622 				    "Destination was ");
3623 				SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
3624 				    &net->ro._l_addr.sa);
3625 				if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3626 					if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3627 						SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3628 						sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3629 						    stcb,
3630 						    SCTP_FAILED_THRESHOLD,
3631 						    (void *)net,
3632 						    so_locked);
3633 						net->dest_state &= ~SCTP_ADDR_REACHABLE;
3634 						net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
3635 						/*
3636 						 * JRS 5/14/07 - If a
3637 						 * destination is
3638 						 * unreachable, the PF bit
3639 						 * is turned off.  This
3640 						 * allows an unambiguous use
3641 						 * of the PF bit for
3642 						 * destinations that are
3643 						 * reachable but potentially
3644 						 * failed. If the
3645 						 * destination is set to the
3646 						 * unreachable state, also
3647 						 * set the destination to
3648 						 * the PF state.
3649 						 */
3650 						/*
3651 						 * Add debug message here if
3652 						 * destination is not in PF
3653 						 * state.
3654 						 */
3655 						/*
3656 						 * Stop any running T3
3657 						 * timers here?
3658 						 */
3659 						if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
3660 							net->dest_state &= ~SCTP_ADDR_PF;
3661 							SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
3662 							    net);
3663 						}
3664 					}
3665 				}
3666 				if (stcb) {
3667 					if (net == stcb->asoc.primary_destination) {
3668 						/* need a new primary */
3669 						struct sctp_nets *alt;
3670 
3671 						alt = sctp_find_alternate_net(stcb, net, 0);
3672 						if (alt != net) {
3673 							if (sctp_set_primary_addr(stcb,
3674 							    (struct sockaddr *)NULL,
3675 							    alt) == 0) {
3676 								net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
3677 								if (net->ro._s_addr) {
3678 									sctp_free_ifa(net->ro._s_addr);
3679 									net->ro._s_addr = NULL;
3680 								}
3681 								net->src_addr_selected = 0;
3682 							}
3683 						}
3684 					}
3685 				}
3686 			}
3687 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3688 			sctp_m_freem(m);
3689 			return (EHOSTUNREACH);
3690 		}
3691 		if (ro != &iproute) {
3692 			memcpy(&iproute, ro, sizeof(*ro));
3693 		}
3694 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
3695 		    (uint32_t) (ntohl(ip->ip_src.s_addr)));
3696 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
3697 		    (uint32_t) (ntohl(ip->ip_dst.s_addr)));
3698 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
3699 		    ro->ro_rt);
3700 
3701 		if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3702 			/* failed to prepend data, give up */
3703 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3704 			sctp_m_freem(m);
3705 			return (ENOMEM);
3706 		}
3707 #ifdef  SCTP_PACKET_LOGGING
3708 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
3709 			sctp_packet_log(m, packet_length);
3710 #endif
3711 		SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3712 		if (port) {
3713 			SCTP_ENABLE_UDP_CSUM(o_pak);
3714 		}
3715 		/* send it out.  table id is taken from stcb */
3716 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3717 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3718 			so = SCTP_INP_SO(inp);
3719 			SCTP_SOCKET_UNLOCK(so, 0);
3720 		}
3721 #endif
3722 		SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
3723 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3724 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3725 			atomic_add_int(&stcb->asoc.refcnt, 1);
3726 			SCTP_TCB_UNLOCK(stcb);
3727 			SCTP_SOCKET_LOCK(so, 0);
3728 			SCTP_TCB_LOCK(stcb);
3729 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
3730 		}
3731 #endif
3732 		SCTP_STAT_INCR(sctps_sendpackets);
3733 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3734 		if (ret)
3735 			SCTP_STAT_INCR(sctps_senderrors);
3736 
3737 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
3738 		if (net == NULL) {
3739 			/* free tempy routes */
3740 			if (ro->ro_rt) {
3741 				RTFREE(ro->ro_rt);
3742 				ro->ro_rt = NULL;
3743 			}
3744 		} else {
3745 			/* PMTU check versus smallest asoc MTU goes here */
3746 			if ((ro->ro_rt != NULL) &&
3747 			    (net->ro._s_addr)) {
3748 				uint32_t mtu;
3749 
3750 				mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3751 				if (mtu &&
3752 				    (stcb->asoc.smallest_mtu > mtu)) {
3753 #ifdef SCTP_PRINT_FOR_B_AND_M
3754 					SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n",
3755 					    mtu);
3756 #endif
3757 					sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3758 					net->mtu = mtu;
3759 				}
3760 			} else if (ro->ro_rt == NULL) {
3761 				/* route was freed */
3762 				if (net->ro._s_addr &&
3763 				    net->src_addr_selected) {
3764 					sctp_free_ifa(net->ro._s_addr);
3765 					net->ro._s_addr = NULL;
3766 				}
3767 				net->src_addr_selected = 0;
3768 			}
3769 		}
3770 		return (ret);
3771 	}
3772 #ifdef INET6
3773 	else if (to->sa_family == AF_INET6) {
3774 		uint32_t flowlabel;
3775 		struct ip6_hdr *ip6h;
3776 		struct route_in6 ip6route;
3777 		struct ifnet *ifp;
3778 		u_char flowTop;
3779 		uint16_t flowBottom;
3780 		u_char tosBottom, tosTop;
3781 		struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
3782 		int prev_scope = 0;
3783 		struct sockaddr_in6 lsa6_storage;
3784 		int error;
3785 		u_short prev_port = 0;
3786 
3787 		if (net != NULL) {
3788 			flowlabel = net->tos_flowlabel;
3789 		} else {
3790 			flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
3791 		}
3792 
3793 		if (port) {
3794 			newm = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
3795 		} else {
3796 			newm = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
3797 		}
3798 		if (newm == NULL) {
3799 			sctp_m_freem(m);
3800 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3801 			return (ENOMEM);
3802 		}
3803 		if (port) {
3804 			SCTP_ALIGN_TO_END(newm, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
3805 			SCTP_BUF_LEN(newm) = sizeof(struct ip6_hdr) + sizeof(struct udphdr);
3806 			packet_length += sizeof(struct ip6_hdr) + sizeof(struct udphdr);
3807 		} else {
3808 			SCTP_ALIGN_TO_END(newm, sizeof(struct ip6_hdr));
3809 			SCTP_BUF_LEN(newm) = sizeof(struct ip6_hdr);
3810 			packet_length += sizeof(struct ip6_hdr);
3811 		}
3812 		SCTP_BUF_NEXT(newm) = m;
3813 		m = newm;
3814 
3815 		ip6h = mtod(m, struct ip6_hdr *);
3816 		/*
3817 		 * We assume here that inp_flow is in host byte order within
3818 		 * the TCB!
3819 		 */
3820 		flowBottom = flowlabel & 0x0000ffff;
3821 		flowTop = ((flowlabel & 0x000f0000) >> 16);
3822 		tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
3823 		/* protect *sin6 from overwrite */
3824 		sin6 = (struct sockaddr_in6 *)to;
3825 		tmp = *sin6;
3826 		sin6 = &tmp;
3827 
3828 		/* KAME hack: embed scopeid */
3829 		if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
3830 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3831 			return (EINVAL);
3832 		}
3833 		if (net == NULL) {
3834 			memset(&ip6route, 0, sizeof(ip6route));
3835 			ro = (sctp_route_t *) & ip6route;
3836 			memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
3837 		} else {
3838 			ro = (sctp_route_t *) & net->ro;
3839 		}
3840 		if (stcb != NULL) {
3841 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3842 				/* Enable ECN */
3843 				tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
3844 			} else {
3845 				/* No ECN */
3846 				tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3847 			}
3848 		} else {
3849 			/* we could get no asoc if it is a O-O-T-B packet */
3850 			tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3851 		}
3852 		ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
3853 		if (port) {
3854 			ip6h->ip6_nxt = IPPROTO_UDP;
3855 		} else {
3856 			ip6h->ip6_nxt = IPPROTO_SCTP;
3857 		}
3858 		ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
3859 		ip6h->ip6_dst = sin6->sin6_addr;
3860 
3861 		/*
3862 		 * Add SRC address selection here: we can only reuse to a
3863 		 * limited degree the kame src-addr-sel, since we can try
3864 		 * their selection but it may not be bound.
3865 		 */
3866 		bzero(&lsa6_tmp, sizeof(lsa6_tmp));
3867 		lsa6_tmp.sin6_family = AF_INET6;
3868 		lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
3869 		lsa6 = &lsa6_tmp;
3870 		if (net && out_of_asoc_ok == 0) {
3871 			if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3872 				sctp_free_ifa(net->ro._s_addr);
3873 				net->ro._s_addr = NULL;
3874 				net->src_addr_selected = 0;
3875 				if (ro->ro_rt) {
3876 					RTFREE(ro->ro_rt);
3877 					ro->ro_rt = NULL;
3878 				}
3879 			}
3880 			if (net->src_addr_selected == 0) {
3881 				sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3882 				/* KAME hack: embed scopeid */
3883 				if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
3884 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3885 					return (EINVAL);
3886 				}
3887 				/* Cache the source address */
3888 				net->ro._s_addr = sctp_source_address_selection(inp,
3889 				    stcb,
3890 				    ro,
3891 				    net,
3892 				    0,
3893 				    vrf_id);
3894 				(void)sa6_recoverscope(sin6);
3895 				net->src_addr_selected = 1;
3896 			}
3897 			if (net->ro._s_addr == NULL) {
3898 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
3899 				net->src_addr_selected = 0;
3900 				goto no_route;
3901 			}
3902 			lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
3903 		} else {
3904 			sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
3905 			/* KAME hack: embed scopeid */
3906 			if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
3907 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3908 				return (EINVAL);
3909 			}
3910 			if (over_addr == NULL) {
3911 				struct sctp_ifa *_lsrc;
3912 
3913 				_lsrc = sctp_source_address_selection(inp, stcb, ro,
3914 				    net,
3915 				    out_of_asoc_ok,
3916 				    vrf_id);
3917 				if (_lsrc == NULL) {
3918 					goto no_route;
3919 				}
3920 				lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
3921 				sctp_free_ifa(_lsrc);
3922 			} else {
3923 				lsa6->sin6_addr = over_addr->sin6.sin6_addr;
3924 				SCTP_RTALLOC((&ro->ro_rt), vrf_id);
3925 			}
3926 			(void)sa6_recoverscope(sin6);
3927 		}
3928 		lsa6->sin6_port = inp->sctp_lport;
3929 
3930 		if (ro->ro_rt == NULL) {
3931 			/*
3932 			 * src addr selection failed to find a route (or
3933 			 * valid source addr), so we can't get there from
3934 			 * here!
3935 			 */
3936 			goto no_route;
3937 		}
3938 		/*
3939 		 * XXX: sa6 may not have a valid sin6_scope_id in the
3940 		 * non-SCOPEDROUTING case.
3941 		 */
3942 		bzero(&lsa6_storage, sizeof(lsa6_storage));
3943 		lsa6_storage.sin6_family = AF_INET6;
3944 		lsa6_storage.sin6_len = sizeof(lsa6_storage);
3945 		lsa6_storage.sin6_addr = lsa6->sin6_addr;
3946 		if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
3947 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
3948 			sctp_m_freem(m);
3949 			return (error);
3950 		}
3951 		/* XXX */
3952 		lsa6_storage.sin6_addr = lsa6->sin6_addr;
3953 		lsa6_storage.sin6_port = inp->sctp_lport;
3954 		lsa6 = &lsa6_storage;
3955 		ip6h->ip6_src = lsa6->sin6_addr;
3956 
3957 		if (port) {
3958 			udp = (struct udphdr *)(ip6h + 1);
3959 			udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3960 			udp->uh_dport = port;
3961 			udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
3962 			udp->uh_sum = 0;
3963 		}
3964 		/*
3965 		 * We set the hop limit now since there is a good chance
3966 		 * that our ro pointer is now filled
3967 		 */
3968 		ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
3969 		ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
3970 
3971 #ifdef SCTP_DEBUG
3972 		/* Copy to be sure something bad is not happening */
3973 		sin6->sin6_addr = ip6h->ip6_dst;
3974 		lsa6->sin6_addr = ip6h->ip6_src;
3975 #endif
3976 
3977 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
3978 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
3979 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
3980 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
3981 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
3982 		if (net) {
3983 			sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3984 			/* preserve the port and scope for link local send */
3985 			prev_scope = sin6->sin6_scope_id;
3986 			prev_port = sin6->sin6_port;
3987 		}
3988 		if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3989 			/* failed to prepend data, give up */
3990 			sctp_m_freem(m);
3991 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3992 			return (ENOMEM);
3993 		}
3994 #ifdef  SCTP_PACKET_LOGGING
3995 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
3996 			sctp_packet_log(m, packet_length);
3997 #endif
3998 		SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3999 		if (port) {
4000 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4001 				udp->uh_sum = 0xffff;
4002 			}
4003 		}
4004 		/* send it out. table id is taken from stcb */
4005 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4006 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4007 			so = SCTP_INP_SO(inp);
4008 			SCTP_SOCKET_UNLOCK(so, 0);
4009 		}
4010 #endif
4011 		SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4012 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4013 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4014 			atomic_add_int(&stcb->asoc.refcnt, 1);
4015 			SCTP_TCB_UNLOCK(stcb);
4016 			SCTP_SOCKET_LOCK(so, 0);
4017 			SCTP_TCB_LOCK(stcb);
4018 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
4019 		}
4020 #endif
4021 		if (net) {
4022 			/* for link local this must be done */
4023 			sin6->sin6_scope_id = prev_scope;
4024 			sin6->sin6_port = prev_port;
4025 		}
4026 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4027 		SCTP_STAT_INCR(sctps_sendpackets);
4028 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4029 		if (ret) {
4030 			SCTP_STAT_INCR(sctps_senderrors);
4031 		}
4032 		if (net == NULL) {
4033 			/* Now if we had a temp route free it */
4034 			if (ro->ro_rt) {
4035 				RTFREE(ro->ro_rt);
4036 			}
4037 		} else {
4038 			/* PMTU check versus smallest asoc MTU goes here */
4039 			if (ro->ro_rt == NULL) {
4040 				/* Route was freed */
4041 				if (net->ro._s_addr &&
4042 				    net->src_addr_selected) {
4043 					sctp_free_ifa(net->ro._s_addr);
4044 					net->ro._s_addr = NULL;
4045 				}
4046 				net->src_addr_selected = 0;
4047 			}
4048 			if ((ro->ro_rt != NULL) &&
4049 			    (net->ro._s_addr)) {
4050 				uint32_t mtu;
4051 
4052 				mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4053 				if (mtu &&
4054 				    (stcb->asoc.smallest_mtu > mtu)) {
4055 #ifdef SCTP_PRINT_FOR_B_AND_M
4056 					SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
4057 					    mtu);
4058 #endif
4059 					sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4060 					net->mtu = mtu;
4061 				}
4062 			} else if (ifp) {
4063 				if (ND_IFINFO(ifp)->linkmtu &&
4064 				    (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4065 #ifdef SCTP_PRINT_FOR_B_AND_M
4066 					SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
4067 					    ND_IFINFO(ifp)->linkmtu);
4068 #endif
4069 					sctp_mtu_size_reset(inp,
4070 					    &stcb->asoc,
4071 					    ND_IFINFO(ifp)->linkmtu);
4072 				}
4073 			}
4074 		}
4075 		return (ret);
4076 	}
4077 #endif
4078 	else {
4079 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4080 		    ((struct sockaddr *)to)->sa_family);
4081 		sctp_m_freem(m);
4082 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4083 		return (EFAULT);
4084 	}
4085 }
4086 
4087 
4088 void
4089 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4090 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4091     SCTP_UNUSED
4092 #endif
4093 )
4094 {
4095 	struct mbuf *m, *m_at, *mp_last;
4096 	struct sctp_nets *net;
4097 	struct sctp_init_msg *initm;
4098 	struct sctp_supported_addr_param *sup_addr;
4099 	struct sctp_adaptation_layer_indication *ali;
4100 	struct sctp_ecn_supported_param *ecn;
4101 	struct sctp_prsctp_supported_param *prsctp;
4102 	struct sctp_ecn_nonce_supported_param *ecn_nonce;
4103 	struct sctp_supported_chunk_types_param *pr_supported;
4104 	int cnt_inits_to = 0;
4105 	int padval, ret;
4106 	int num_ext;
4107 	int p_len;
4108 
4109 	/* INIT's always go to the primary (and usually ONLY address) */
4110 	mp_last = NULL;
4111 	net = stcb->asoc.primary_destination;
4112 	if (net == NULL) {
4113 		net = TAILQ_FIRST(&stcb->asoc.nets);
4114 		if (net == NULL) {
4115 			/* TSNH */
4116 			return;
4117 		}
4118 		/* we confirm any address we send an INIT to */
4119 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4120 		(void)sctp_set_primary_addr(stcb, NULL, net);
4121 	} else {
4122 		/* we confirm any address we send an INIT to */
4123 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4124 	}
4125 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4126 #ifdef INET6
4127 	if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4128 		/*
4129 		 * special hook, if we are sending to link local it will not
4130 		 * show up in our private address count.
4131 		 */
4132 		struct sockaddr_in6 *sin6l;
4133 
4134 		sin6l = &net->ro._l_addr.sin6;
4135 		if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4136 			cnt_inits_to = 1;
4137 	}
4138 #endif
4139 	if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4140 		/* This case should not happen */
4141 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4142 		return;
4143 	}
4144 	/* start the INIT timer */
4145 	sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4146 
4147 	m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4148 	if (m == NULL) {
4149 		/* No memory, INIT timer will re-attempt. */
4150 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4151 		return;
4152 	}
4153 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
4154 	/*
4155 	 * assume peer supports asconf in order to be able to queue local
4156 	 * address changes while an INIT is in flight and before the assoc
4157 	 * is established.
4158 	 */
4159 	stcb->asoc.peer_supports_asconf = 1;
4160 	/* Now lets put the SCTP header in place */
4161 	initm = mtod(m, struct sctp_init_msg *);
4162 	initm->sh.src_port = inp->sctp_lport;
4163 	initm->sh.dest_port = stcb->rport;
4164 	initm->sh.v_tag = 0;
4165 	initm->sh.checksum = 0;	/* calculate later */
4166 	/* now the chunk header */
4167 	initm->msg.ch.chunk_type = SCTP_INITIATION;
4168 	initm->msg.ch.chunk_flags = 0;
4169 	/* fill in later from mbuf we build */
4170 	initm->msg.ch.chunk_length = 0;
4171 	/* place in my tag */
4172 	initm->msg.init.initiate_tag = htonl(stcb->asoc.my_vtag);
4173 	/* set up some of the credits. */
4174 	initm->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(inp->sctp_socket),
4175 	    SCTP_MINIMAL_RWND));
4176 
4177 	initm->msg.init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4178 	initm->msg.init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4179 	initm->msg.init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4180 	/* now the address restriction */
4181 	sup_addr = (struct sctp_supported_addr_param *)((caddr_t)initm +
4182 	    sizeof(*initm));
4183 	sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4184 #ifdef INET6
4185 	/* we support 2 types: IPv6/IPv4 */
4186 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
4187 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4188 	sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4189 #else
4190 	/* we support 1 type: IPv4 */
4191 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
4192 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4193 	sup_addr->addr_type[1] = htons(0);	/* this is the padding */
4194 #endif
4195 	SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
4196 
4197 	/* adaptation layer indication parameter */
4198 	ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
4199 	ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4200 	ali->ph.param_length = htons(sizeof(*ali));
4201 	ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4202 	SCTP_BUF_LEN(m) += sizeof(*ali);
4203 	ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
4204 
4205 	/* now any cookie time extensions */
4206 	if (stcb->asoc.cookie_preserve_req) {
4207 		struct sctp_cookie_perserve_param *cookie_preserve;
4208 
4209 		cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4210 		cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4211 		cookie_preserve->ph.param_length = htons(
4212 		    sizeof(*cookie_preserve));
4213 		cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4214 		SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4215 		ecn = (struct sctp_ecn_supported_param *)(
4216 		    (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4217 		stcb->asoc.cookie_preserve_req = 0;
4218 	}
4219 	/* ECN parameter */
4220 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
4221 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4222 		ecn->ph.param_length = htons(sizeof(*ecn));
4223 		SCTP_BUF_LEN(m) += sizeof(*ecn);
4224 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4225 		    sizeof(*ecn));
4226 	} else {
4227 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4228 	}
4229 	/* And now tell the peer we do pr-sctp */
4230 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4231 	prsctp->ph.param_length = htons(sizeof(*prsctp));
4232 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
4233 
4234 	/* And now tell the peer we do all the extensions */
4235 	pr_supported = (struct sctp_supported_chunk_types_param *)
4236 	    ((caddr_t)prsctp + sizeof(*prsctp));
4237 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4238 	num_ext = 0;
4239 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4240 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4241 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4242 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4243 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4244 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
4245 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4246 	p_len = sizeof(*pr_supported) + num_ext;
4247 	pr_supported->ph.param_length = htons(p_len);
4248 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4249 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4250 
4251 	/* ECN nonce: And now tell the peer we support ECN nonce */
4252 	if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
4253 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
4254 		    ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
4255 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
4256 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
4257 		SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
4258 	}
4259 	/* add authentication parameters */
4260 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4261 		struct sctp_auth_random *randp;
4262 		struct sctp_auth_hmac_algo *hmacs;
4263 		struct sctp_auth_chunk_list *chunks;
4264 
4265 		/* attach RANDOM parameter, if available */
4266 		if (stcb->asoc.authinfo.random != NULL) {
4267 			randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4268 			p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4269 #ifdef SCTP_AUTH_DRAFT_04
4270 			randp->ph.param_type = htons(SCTP_RANDOM);
4271 			randp->ph.param_length = htons(p_len);
4272 			bcopy(stcb->asoc.authinfo.random->key,
4273 			    randp->random_data,
4274 			    stcb->asoc.authinfo.random_len);
4275 #else
4276 			/* random key already contains the header */
4277 			bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4278 #endif
4279 			/* zero out any padding required */
4280 			bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4281 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4282 		}
4283 		/* add HMAC_ALGO parameter */
4284 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4285 		p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4286 		    (uint8_t *) hmacs->hmac_ids);
4287 		if (p_len > 0) {
4288 			p_len += sizeof(*hmacs);
4289 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4290 			hmacs->ph.param_length = htons(p_len);
4291 			/* zero out any padding required */
4292 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4293 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4294 		}
4295 		/* add CHUNKS parameter */
4296 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4297 		p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4298 		    chunks->chunk_types);
4299 		if (p_len > 0) {
4300 			p_len += sizeof(*chunks);
4301 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4302 			chunks->ph.param_length = htons(p_len);
4303 			/* zero out any padding required */
4304 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4305 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4306 		}
4307 	}
4308 	m_at = m;
4309 	/* now the addresses */
4310 	{
4311 		struct sctp_scoping scp;
4312 
4313 		/*
4314 		 * To optimize this we could put the scoping stuff into a
4315 		 * structure and remove the individual uint8's from the
4316 		 * assoc structure. Then we could just sifa in the address
4317 		 * within the stcb.. but for now this is a quick hack to get
4318 		 * the address stuff teased apart.
4319 		 */
4320 		scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4321 		scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4322 		scp.loopback_scope = stcb->asoc.loopback_scope;
4323 		scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4324 		scp.local_scope = stcb->asoc.local_scope;
4325 		scp.site_scope = stcb->asoc.site_scope;
4326 
4327 		m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
4328 	}
4329 
4330 	/* calulate the size and update pkt header and chunk header */
4331 	p_len = 0;
4332 	for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4333 		if (SCTP_BUF_NEXT(m_at) == NULL)
4334 			mp_last = m_at;
4335 		p_len += SCTP_BUF_LEN(m_at);
4336 	}
4337 	initm->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
4338 	/*
4339 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4340 	 * here since the timer will drive a retranmission.
4341 	 */
4342 
4343 	/* I don't expect this to execute but we will be safe here */
4344 	padval = p_len % 4;
4345 	if ((padval) && (mp_last)) {
4346 		/*
4347 		 * The compiler worries that mp_last may not be set even
4348 		 * though I think it is impossible :-> however we add
4349 		 * mp_last here just in case.
4350 		 */
4351 		ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4352 		if (ret) {
4353 			/* Houston we have a problem, no space */
4354 			sctp_m_freem(m);
4355 			return;
4356 		}
4357 		p_len += padval;
4358 	}
4359 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4360 	ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4361 	    (struct sockaddr *)&net->ro._l_addr,
4362 	    m, 0, NULL, 0, 0, NULL, 0, net->port, so_locked, NULL);
4363 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4364 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4365 	sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4366 	(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4367 }
4368 
4369 struct mbuf *
4370 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4371     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp)
4372 {
4373 	/*
4374 	 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4375 	 * being equal to the beginning of the params i.e. (iphlen +
4376 	 * sizeof(struct sctp_init_msg) parse through the parameters to the
4377 	 * end of the mbuf verifying that all parameters are known.
4378 	 *
4379 	 * For unknown parameters build and return a mbuf with
4380 	 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4381 	 * processing this chunk stop, and set *abort_processing to 1.
4382 	 *
4383 	 * By having param_offset be pre-set to where parameters begin it is
4384 	 * hoped that this routine may be reused in the future by new
4385 	 * features.
4386 	 */
4387 	struct sctp_paramhdr *phdr, params;
4388 
4389 	struct mbuf *mat, *op_err;
4390 	char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4391 	int at, limit, pad_needed;
4392 	uint16_t ptype, plen, padded_size;
4393 	int err_at;
4394 
4395 	*abort_processing = 0;
4396 	mat = in_initpkt;
4397 	err_at = 0;
4398 	limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4399 	at = param_offset;
4400 	op_err = NULL;
4401 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4402 	phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4403 	while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4404 		ptype = ntohs(phdr->param_type);
4405 		plen = ntohs(phdr->param_length);
4406 		if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4407 			/* wacked parameter */
4408 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4409 			goto invalid_size;
4410 		}
4411 		limit -= SCTP_SIZE32(plen);
4412 		/*-
4413 		 * All parameters for all chunks that we know/understand are
4414 		 * listed here. We process them other places and make
4415 		 * appropriate stop actions per the upper bits. However this
4416 		 * is the generic routine processor's can call to get back
4417 		 * an operr.. to either incorporate (init-ack) or send.
4418 		 */
4419 		padded_size = SCTP_SIZE32(plen);
4420 		switch (ptype) {
4421 			/* Param's with variable size */
4422 		case SCTP_HEARTBEAT_INFO:
4423 		case SCTP_STATE_COOKIE:
4424 		case SCTP_UNRECOG_PARAM:
4425 		case SCTP_ERROR_CAUSE_IND:
4426 			/* ok skip fwd */
4427 			at += padded_size;
4428 			break;
4429 			/* Param's with variable size within a range */
4430 		case SCTP_CHUNK_LIST:
4431 		case SCTP_SUPPORTED_CHUNK_EXT:
4432 			if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4433 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4434 				goto invalid_size;
4435 			}
4436 			at += padded_size;
4437 			break;
4438 		case SCTP_SUPPORTED_ADDRTYPE:
4439 			if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4440 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4441 				goto invalid_size;
4442 			}
4443 			at += padded_size;
4444 			break;
4445 		case SCTP_RANDOM:
4446 			if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4447 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4448 				goto invalid_size;
4449 			}
4450 			at += padded_size;
4451 			break;
4452 		case SCTP_SET_PRIM_ADDR:
4453 		case SCTP_DEL_IP_ADDRESS:
4454 		case SCTP_ADD_IP_ADDRESS:
4455 			if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4456 			    (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4457 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4458 				goto invalid_size;
4459 			}
4460 			at += padded_size;
4461 			break;
4462 			/* Param's with a fixed size */
4463 		case SCTP_IPV4_ADDRESS:
4464 			if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4465 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4466 				goto invalid_size;
4467 			}
4468 			at += padded_size;
4469 			break;
4470 		case SCTP_IPV6_ADDRESS:
4471 			if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4472 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4473 				goto invalid_size;
4474 			}
4475 			at += padded_size;
4476 			break;
4477 		case SCTP_COOKIE_PRESERVE:
4478 			if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4479 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4480 				goto invalid_size;
4481 			}
4482 			at += padded_size;
4483 			break;
4484 		case SCTP_ECN_NONCE_SUPPORTED:
4485 		case SCTP_PRSCTP_SUPPORTED:
4486 			if (padded_size != sizeof(struct sctp_paramhdr)) {
4487 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp %d\n", plen);
4488 				goto invalid_size;
4489 			}
4490 			at += padded_size;
4491 			break;
4492 		case SCTP_ECN_CAPABLE:
4493 			if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4494 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4495 				goto invalid_size;
4496 			}
4497 			at += padded_size;
4498 			break;
4499 		case SCTP_ULP_ADAPTATION:
4500 			if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4501 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4502 				goto invalid_size;
4503 			}
4504 			at += padded_size;
4505 			break;
4506 		case SCTP_SUCCESS_REPORT:
4507 			if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
4508 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
4509 				goto invalid_size;
4510 			}
4511 			at += padded_size;
4512 			break;
4513 		case SCTP_HOSTNAME_ADDRESS:
4514 			{
4515 				/* We can NOT handle HOST NAME addresses!! */
4516 				int l_len;
4517 
4518 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
4519 				*abort_processing = 1;
4520 				if (op_err == NULL) {
4521 					/* Ok need to try to get a mbuf */
4522 #ifdef INET6
4523 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4524 #else
4525 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4526 #endif
4527 					l_len += plen;
4528 					l_len += sizeof(struct sctp_paramhdr);
4529 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4530 					if (op_err) {
4531 						SCTP_BUF_LEN(op_err) = 0;
4532 						/*
4533 						 * pre-reserve space for ip
4534 						 * and sctp header  and
4535 						 * chunk hdr
4536 						 */
4537 #ifdef INET6
4538 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4539 #else
4540 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4541 #endif
4542 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4543 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4544 					}
4545 				}
4546 				if (op_err) {
4547 					/* If we have space */
4548 					struct sctp_paramhdr s;
4549 
4550 					if (err_at % 4) {
4551 						uint32_t cpthis = 0;
4552 
4553 						pad_needed = 4 - (err_at % 4);
4554 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4555 						err_at += pad_needed;
4556 					}
4557 					s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
4558 					s.param_length = htons(sizeof(s) + plen);
4559 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4560 					err_at += sizeof(s);
4561 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4562 					if (phdr == NULL) {
4563 						sctp_m_freem(op_err);
4564 						/*
4565 						 * we are out of memory but
4566 						 * we still need to have a
4567 						 * look at what to do (the
4568 						 * system is in trouble
4569 						 * though).
4570 						 */
4571 						return (NULL);
4572 					}
4573 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4574 					err_at += plen;
4575 				}
4576 				return (op_err);
4577 				break;
4578 			}
4579 		default:
4580 			/*
4581 			 * we do not recognize the parameter figure out what
4582 			 * we do.
4583 			 */
4584 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
4585 			if ((ptype & 0x4000) == 0x4000) {
4586 				/* Report bit is set?? */
4587 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
4588 				if (op_err == NULL) {
4589 					int l_len;
4590 
4591 					/* Ok need to try to get an mbuf */
4592 #ifdef INET6
4593 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4594 #else
4595 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4596 #endif
4597 					l_len += plen;
4598 					l_len += sizeof(struct sctp_paramhdr);
4599 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4600 					if (op_err) {
4601 						SCTP_BUF_LEN(op_err) = 0;
4602 #ifdef INET6
4603 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4604 #else
4605 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4606 #endif
4607 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4608 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4609 					}
4610 				}
4611 				if (op_err) {
4612 					/* If we have space */
4613 					struct sctp_paramhdr s;
4614 
4615 					if (err_at % 4) {
4616 						uint32_t cpthis = 0;
4617 
4618 						pad_needed = 4 - (err_at % 4);
4619 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4620 						err_at += pad_needed;
4621 					}
4622 					s.param_type = htons(SCTP_UNRECOG_PARAM);
4623 					s.param_length = htons(sizeof(s) + plen);
4624 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4625 					err_at += sizeof(s);
4626 					if (plen > sizeof(tempbuf)) {
4627 						plen = sizeof(tempbuf);
4628 					}
4629 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4630 					if (phdr == NULL) {
4631 						sctp_m_freem(op_err);
4632 						/*
4633 						 * we are out of memory but
4634 						 * we still need to have a
4635 						 * look at what to do (the
4636 						 * system is in trouble
4637 						 * though).
4638 						 */
4639 						op_err = NULL;
4640 						goto more_processing;
4641 					}
4642 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4643 					err_at += plen;
4644 				}
4645 			}
4646 	more_processing:
4647 			if ((ptype & 0x8000) == 0x0000) {
4648 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
4649 				return (op_err);
4650 			} else {
4651 				/* skip this chunk and continue processing */
4652 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
4653 				at += SCTP_SIZE32(plen);
4654 			}
4655 			break;
4656 
4657 		}
4658 		phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4659 	}
4660 	return (op_err);
4661 invalid_size:
4662 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
4663 	*abort_processing = 1;
4664 	if ((op_err == NULL) && phdr) {
4665 		int l_len;
4666 
4667 #ifdef INET6
4668 		l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4669 #else
4670 		l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4671 #endif
4672 		l_len += (2 * sizeof(struct sctp_paramhdr));
4673 		op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4674 		if (op_err) {
4675 			SCTP_BUF_LEN(op_err) = 0;
4676 #ifdef INET6
4677 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4678 #else
4679 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4680 #endif
4681 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4682 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4683 		}
4684 	}
4685 	if ((op_err) && phdr) {
4686 		struct sctp_paramhdr s;
4687 
4688 		if (err_at % 4) {
4689 			uint32_t cpthis = 0;
4690 
4691 			pad_needed = 4 - (err_at % 4);
4692 			m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4693 			err_at += pad_needed;
4694 		}
4695 		s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4696 		s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
4697 		m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4698 		err_at += sizeof(s);
4699 		/* Only copy back the p-hdr that caused the issue */
4700 		m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
4701 	}
4702 	return (op_err);
4703 }
4704 
4705 static int
4706 sctp_are_there_new_addresses(struct sctp_association *asoc,
4707     struct mbuf *in_initpkt, int iphlen, int offset)
4708 {
4709 	/*
4710 	 * Given a INIT packet, look through the packet to verify that there
4711 	 * are NO new addresses. As we go through the parameters add reports
4712 	 * of any un-understood parameters that require an error.  Also we
4713 	 * must return (1) to drop the packet if we see a un-understood
4714 	 * parameter that tells us to drop the chunk.
4715 	 */
4716 	struct sockaddr_in sin4, *sa4;
4717 
4718 #ifdef INET6
4719 	struct sockaddr_in6 sin6, *sa6;
4720 
4721 #endif
4722 	struct sockaddr *sa_touse;
4723 	struct sockaddr *sa;
4724 	struct sctp_paramhdr *phdr, params;
4725 	struct ip *iph;
4726 
4727 #ifdef INET6
4728 	struct ip6_hdr *ip6h;
4729 
4730 #endif
4731 	struct mbuf *mat;
4732 	uint16_t ptype, plen;
4733 	int err_at;
4734 	uint8_t fnd;
4735 	struct sctp_nets *net;
4736 
4737 	memset(&sin4, 0, sizeof(sin4));
4738 #ifdef INET6
4739 	memset(&sin6, 0, sizeof(sin6));
4740 #endif
4741 	sin4.sin_family = AF_INET;
4742 	sin4.sin_len = sizeof(sin4);
4743 #ifdef INET6
4744 	sin6.sin6_family = AF_INET6;
4745 	sin6.sin6_len = sizeof(sin6);
4746 #endif
4747 	sa_touse = NULL;
4748 	/* First what about the src address of the pkt ? */
4749 	iph = mtod(in_initpkt, struct ip *);
4750 	switch (iph->ip_v) {
4751 	case IPVERSION:
4752 		/* source addr is IPv4 */
4753 		sin4.sin_addr = iph->ip_src;
4754 		sa_touse = (struct sockaddr *)&sin4;
4755 		break;
4756 #ifdef INET6
4757 	case IPV6_VERSION >> 4:
4758 		/* source addr is IPv6 */
4759 		ip6h = mtod(in_initpkt, struct ip6_hdr *);
4760 		sin6.sin6_addr = ip6h->ip6_src;
4761 		sa_touse = (struct sockaddr *)&sin6;
4762 		break;
4763 #endif
4764 	default:
4765 		return (1);
4766 	}
4767 
4768 	fnd = 0;
4769 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4770 		sa = (struct sockaddr *)&net->ro._l_addr;
4771 		if (sa->sa_family == sa_touse->sa_family) {
4772 			if (sa->sa_family == AF_INET) {
4773 				sa4 = (struct sockaddr_in *)sa;
4774 				if (sa4->sin_addr.s_addr ==
4775 				    sin4.sin_addr.s_addr) {
4776 					fnd = 1;
4777 					break;
4778 				}
4779 			}
4780 #ifdef INET6
4781 			if (sa->sa_family == AF_INET6) {
4782 				sa6 = (struct sockaddr_in6 *)sa;
4783 				if (SCTP6_ARE_ADDR_EQUAL(sa6,
4784 				    &sin6)) {
4785 					fnd = 1;
4786 					break;
4787 				}
4788 			}
4789 #endif
4790 		}
4791 	}
4792 	if (fnd == 0) {
4793 		/* New address added! no need to look futher. */
4794 		return (1);
4795 	}
4796 	/* Ok so far lets munge through the rest of the packet */
4797 	mat = in_initpkt;
4798 	err_at = 0;
4799 	sa_touse = NULL;
4800 	offset += sizeof(struct sctp_init_chunk);
4801 	phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
4802 	while (phdr) {
4803 		ptype = ntohs(phdr->param_type);
4804 		plen = ntohs(phdr->param_length);
4805 		if (ptype == SCTP_IPV4_ADDRESS) {
4806 			struct sctp_ipv4addr_param *p4, p4_buf;
4807 
4808 			phdr = sctp_get_next_param(mat, offset,
4809 			    (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4810 			if (plen != sizeof(struct sctp_ipv4addr_param) ||
4811 			    phdr == NULL) {
4812 				return (1);
4813 			}
4814 			p4 = (struct sctp_ipv4addr_param *)phdr;
4815 			sin4.sin_addr.s_addr = p4->addr;
4816 			sa_touse = (struct sockaddr *)&sin4;
4817 		} else if (ptype == SCTP_IPV6_ADDRESS) {
4818 			struct sctp_ipv6addr_param *p6, p6_buf;
4819 
4820 			phdr = sctp_get_next_param(mat, offset,
4821 			    (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4822 			if (plen != sizeof(struct sctp_ipv6addr_param) ||
4823 			    phdr == NULL) {
4824 				return (1);
4825 			}
4826 			p6 = (struct sctp_ipv6addr_param *)phdr;
4827 #ifdef INET6
4828 			memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4829 			    sizeof(p6->addr));
4830 #endif
4831 			sa_touse = (struct sockaddr *)&sin4;
4832 		}
4833 		if (sa_touse) {
4834 			/* ok, sa_touse points to one to check */
4835 			fnd = 0;
4836 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4837 				sa = (struct sockaddr *)&net->ro._l_addr;
4838 				if (sa->sa_family != sa_touse->sa_family) {
4839 					continue;
4840 				}
4841 				if (sa->sa_family == AF_INET) {
4842 					sa4 = (struct sockaddr_in *)sa;
4843 					if (sa4->sin_addr.s_addr ==
4844 					    sin4.sin_addr.s_addr) {
4845 						fnd = 1;
4846 						break;
4847 					}
4848 				}
4849 #ifdef INET6
4850 				if (sa->sa_family == AF_INET6) {
4851 					sa6 = (struct sockaddr_in6 *)sa;
4852 					if (SCTP6_ARE_ADDR_EQUAL(
4853 					    sa6, &sin6)) {
4854 						fnd = 1;
4855 						break;
4856 					}
4857 				}
4858 #endif
4859 			}
4860 			if (!fnd) {
4861 				/* New addr added! no need to look further */
4862 				return (1);
4863 			}
4864 		}
4865 		offset += SCTP_SIZE32(plen);
4866 		phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
4867 	}
4868 	return (0);
4869 }
4870 
4871 /*
4872  * Given a MBUF chain that was sent into us containing an INIT. Build a
4873  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
4874  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
4875  * message (i.e. the struct sctp_init_msg).
4876  */
4877 void
4878 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4879     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
4880     struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
4881 {
4882 	struct sctp_association *asoc;
4883 	struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
4884 	struct sctp_init_msg *initackm_out;
4885 	struct sctp_adaptation_layer_indication *ali;
4886 	struct sctp_ecn_supported_param *ecn;
4887 	struct sctp_prsctp_supported_param *prsctp;
4888 	struct sctp_ecn_nonce_supported_param *ecn_nonce;
4889 	struct sctp_supported_chunk_types_param *pr_supported;
4890 	union sctp_sockstore store, store1, *over_addr;
4891 	struct sockaddr_in *sin, *to_sin;
4892 
4893 #ifdef INET6
4894 	struct sockaddr_in6 *sin6, *to_sin6;
4895 
4896 #endif
4897 	struct ip *iph;
4898 
4899 #ifdef INET6
4900 	struct ip6_hdr *ip6;
4901 
4902 #endif
4903 	struct sockaddr *to;
4904 	struct sctp_state_cookie stc;
4905 	struct sctp_nets *net = NULL;
4906 	uint8_t *signature = NULL;
4907 	int cnt_inits_to = 0;
4908 	uint16_t his_limit, i_want;
4909 	int abort_flag, padval;
4910 	int num_ext;
4911 	int p_len;
4912 	struct socket *so;
4913 
4914 	if (stcb)
4915 		asoc = &stcb->asoc;
4916 	else
4917 		asoc = NULL;
4918 	mp_last = NULL;
4919 	if ((asoc != NULL) &&
4920 	    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4921 	    (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
4922 		/* new addresses, out of here in non-cookie-wait states */
4923 		/*
4924 		 * Send a ABORT, we don't add the new address error clause
4925 		 * though we even set the T bit and copy in the 0 tag.. this
4926 		 * looks no different than if no listener was present.
4927 		 */
4928 		sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
4929 		return;
4930 	}
4931 	abort_flag = 0;
4932 	op_err = sctp_arethere_unrecognized_parameters(init_pkt,
4933 	    (offset + sizeof(struct sctp_init_chunk)),
4934 	    &abort_flag, (struct sctp_chunkhdr *)init_chk);
4935 	if (abort_flag) {
4936 do_a_abort:
4937 		sctp_send_abort(init_pkt, iphlen, sh,
4938 		    init_chk->init.initiate_tag, op_err, vrf_id, port);
4939 		return;
4940 	}
4941 	m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4942 	if (m == NULL) {
4943 		/* No memory, INIT timer will re-attempt. */
4944 		if (op_err)
4945 			sctp_m_freem(op_err);
4946 		return;
4947 	}
4948 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
4949 
4950 	/* the time I built cookie */
4951 	(void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
4952 
4953 	/* populate any tie tags */
4954 	if (asoc != NULL) {
4955 		/* unlock before tag selections */
4956 		stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
4957 		stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
4958 		stc.cookie_life = asoc->cookie_life;
4959 		net = asoc->primary_destination;
4960 	} else {
4961 		stc.tie_tag_my_vtag = 0;
4962 		stc.tie_tag_peer_vtag = 0;
4963 		/* life I will award this cookie */
4964 		stc.cookie_life = inp->sctp_ep.def_cookie_life;
4965 	}
4966 
4967 	/* copy in the ports for later check */
4968 	stc.myport = sh->dest_port;
4969 	stc.peerport = sh->src_port;
4970 
4971 	/*
4972 	 * If we wanted to honor cookie life extentions, we would add to
4973 	 * stc.cookie_life. For now we should NOT honor any extension
4974 	 */
4975 	stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
4976 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4977 		struct inpcb *in_inp;
4978 
4979 		/* Its a V6 socket */
4980 		in_inp = (struct inpcb *)inp;
4981 		stc.ipv6_addr_legal = 1;
4982 		/* Now look at the binding flag to see if V4 will be legal */
4983 		if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
4984 			stc.ipv4_addr_legal = 1;
4985 		} else {
4986 			/* V4 addresses are NOT legal on the association */
4987 			stc.ipv4_addr_legal = 0;
4988 		}
4989 	} else {
4990 		/* Its a V4 socket, no - V6 */
4991 		stc.ipv4_addr_legal = 1;
4992 		stc.ipv6_addr_legal = 0;
4993 	}
4994 
4995 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
4996 	stc.ipv4_scope = 1;
4997 #else
4998 	stc.ipv4_scope = 0;
4999 #endif
5000 	/* now for scope setup */
5001 	memset((caddr_t)&store, 0, sizeof(store));
5002 	memset((caddr_t)&store1, 0, sizeof(store1));
5003 	sin = &store.sin;
5004 	to_sin = &store1.sin;
5005 #ifdef INET6
5006 	sin6 = &store.sin6;
5007 	to_sin6 = &store1.sin6;
5008 #endif
5009 	iph = mtod(init_pkt, struct ip *);
5010 	/* establish the to_addr's */
5011 	switch (iph->ip_v) {
5012 	case IPVERSION:
5013 		to_sin->sin_port = sh->dest_port;
5014 		to_sin->sin_family = AF_INET;
5015 		to_sin->sin_len = sizeof(struct sockaddr_in);
5016 		to_sin->sin_addr = iph->ip_dst;
5017 		break;
5018 #ifdef INET6
5019 	case IPV6_VERSION >> 4:
5020 		ip6 = mtod(init_pkt, struct ip6_hdr *);
5021 		to_sin6->sin6_addr = ip6->ip6_dst;
5022 		to_sin6->sin6_scope_id = 0;
5023 		to_sin6->sin6_port = sh->dest_port;
5024 		to_sin6->sin6_family = AF_INET6;
5025 		to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5026 		break;
5027 #endif
5028 	default:
5029 		goto do_a_abort;
5030 		break;
5031 	};
5032 
5033 	if (net == NULL) {
5034 		to = (struct sockaddr *)&store;
5035 		switch (iph->ip_v) {
5036 		case IPVERSION:
5037 			{
5038 				sin->sin_family = AF_INET;
5039 				sin->sin_len = sizeof(struct sockaddr_in);
5040 				sin->sin_port = sh->src_port;
5041 				sin->sin_addr = iph->ip_src;
5042 				/* lookup address */
5043 				stc.address[0] = sin->sin_addr.s_addr;
5044 				stc.address[1] = 0;
5045 				stc.address[2] = 0;
5046 				stc.address[3] = 0;
5047 				stc.addr_type = SCTP_IPV4_ADDRESS;
5048 				/* local from address */
5049 				stc.laddress[0] = to_sin->sin_addr.s_addr;
5050 				stc.laddress[1] = 0;
5051 				stc.laddress[2] = 0;
5052 				stc.laddress[3] = 0;
5053 				stc.laddr_type = SCTP_IPV4_ADDRESS;
5054 				/* scope_id is only for v6 */
5055 				stc.scope_id = 0;
5056 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5057 				if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5058 					stc.ipv4_scope = 1;
5059 				}
5060 #else
5061 				stc.ipv4_scope = 1;
5062 #endif				/* SCTP_DONT_DO_PRIVADDR_SCOPE */
5063 				/* Must use the address in this case */
5064 				if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5065 					stc.loopback_scope = 1;
5066 					stc.ipv4_scope = 1;
5067 					stc.site_scope = 1;
5068 					stc.local_scope = 0;
5069 				}
5070 				break;
5071 			}
5072 #ifdef INET6
5073 		case IPV6_VERSION >> 4:
5074 			{
5075 				ip6 = mtod(init_pkt, struct ip6_hdr *);
5076 				sin6->sin6_family = AF_INET6;
5077 				sin6->sin6_len = sizeof(struct sockaddr_in6);
5078 				sin6->sin6_port = sh->src_port;
5079 				sin6->sin6_addr = ip6->ip6_src;
5080 				/* lookup address */
5081 				memcpy(&stc.address, &sin6->sin6_addr,
5082 				    sizeof(struct in6_addr));
5083 				sin6->sin6_scope_id = 0;
5084 				stc.addr_type = SCTP_IPV6_ADDRESS;
5085 				stc.scope_id = 0;
5086 				if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5087 					/*
5088 					 * FIX ME: does this have scope from
5089 					 * rcvif?
5090 					 */
5091 					(void)sa6_recoverscope(sin6);
5092 					stc.scope_id = sin6->sin6_scope_id;
5093 					sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
5094 					stc.loopback_scope = 1;
5095 					stc.local_scope = 0;
5096 					stc.site_scope = 1;
5097 					stc.ipv4_scope = 1;
5098 				} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5099 					/*
5100 					 * If the new destination is a
5101 					 * LINK_LOCAL we must have common
5102 					 * both site and local scope. Don't
5103 					 * set local scope though since we
5104 					 * must depend on the source to be
5105 					 * added implicitly. We cannot
5106 					 * assure just because we share one
5107 					 * link that all links are common.
5108 					 */
5109 					stc.local_scope = 0;
5110 					stc.site_scope = 1;
5111 					stc.ipv4_scope = 1;
5112 					/*
5113 					 * we start counting for the private
5114 					 * address stuff at 1. since the
5115 					 * link local we source from won't
5116 					 * show up in our scoped count.
5117 					 */
5118 					cnt_inits_to = 1;
5119 					/*
5120 					 * pull out the scope_id from
5121 					 * incoming pkt
5122 					 */
5123 					/*
5124 					 * FIX ME: does this have scope from
5125 					 * rcvif?
5126 					 */
5127 					(void)sa6_recoverscope(sin6);
5128 					stc.scope_id = sin6->sin6_scope_id;
5129 					sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
5130 				} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5131 					/*
5132 					 * If the new destination is
5133 					 * SITE_LOCAL then we must have site
5134 					 * scope in common.
5135 					 */
5136 					stc.site_scope = 1;
5137 				}
5138 				memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5139 				stc.laddr_type = SCTP_IPV6_ADDRESS;
5140 				break;
5141 			}
5142 #endif
5143 		default:
5144 			/* TSNH */
5145 			goto do_a_abort;
5146 			break;
5147 		}
5148 	} else {
5149 		/* set the scope per the existing tcb */
5150 
5151 #ifdef INET6
5152 		struct sctp_nets *lnet;
5153 
5154 #endif
5155 
5156 		stc.loopback_scope = asoc->loopback_scope;
5157 		stc.ipv4_scope = asoc->ipv4_local_scope;
5158 		stc.site_scope = asoc->site_scope;
5159 		stc.local_scope = asoc->local_scope;
5160 #ifdef INET6
5161 		/* Why do we not consider IPv4 LL addresses? */
5162 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5163 			if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5164 				if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5165 					/*
5166 					 * if we have a LL address, start
5167 					 * counting at 1.
5168 					 */
5169 					cnt_inits_to = 1;
5170 				}
5171 			}
5172 		}
5173 #endif
5174 		/* use the net pointer */
5175 		to = (struct sockaddr *)&net->ro._l_addr;
5176 		switch (to->sa_family) {
5177 		case AF_INET:
5178 			sin = (struct sockaddr_in *)to;
5179 			stc.address[0] = sin->sin_addr.s_addr;
5180 			stc.address[1] = 0;
5181 			stc.address[2] = 0;
5182 			stc.address[3] = 0;
5183 			stc.addr_type = SCTP_IPV4_ADDRESS;
5184 			if (net->src_addr_selected == 0) {
5185 				/*
5186 				 * strange case here, the INIT should have
5187 				 * did the selection.
5188 				 */
5189 				net->ro._s_addr = sctp_source_address_selection(inp,
5190 				    stcb, (sctp_route_t *) & net->ro,
5191 				    net, 0, vrf_id);
5192 				if (net->ro._s_addr == NULL)
5193 					return;
5194 
5195 				net->src_addr_selected = 1;
5196 
5197 			}
5198 			stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5199 			stc.laddress[1] = 0;
5200 			stc.laddress[2] = 0;
5201 			stc.laddress[3] = 0;
5202 			stc.laddr_type = SCTP_IPV4_ADDRESS;
5203 			break;
5204 #ifdef INET6
5205 		case AF_INET6:
5206 			sin6 = (struct sockaddr_in6 *)to;
5207 			memcpy(&stc.address, &sin6->sin6_addr,
5208 			    sizeof(struct in6_addr));
5209 			stc.addr_type = SCTP_IPV6_ADDRESS;
5210 			if (net->src_addr_selected == 0) {
5211 				/*
5212 				 * strange case here, the INIT should have
5213 				 * did the selection.
5214 				 */
5215 				net->ro._s_addr = sctp_source_address_selection(inp,
5216 				    stcb, (sctp_route_t *) & net->ro,
5217 				    net, 0, vrf_id);
5218 				if (net->ro._s_addr == NULL)
5219 					return;
5220 
5221 				net->src_addr_selected = 1;
5222 			}
5223 			memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5224 			    sizeof(struct in6_addr));
5225 			stc.laddr_type = SCTP_IPV6_ADDRESS;
5226 			break;
5227 #endif
5228 		}
5229 	}
5230 	/* Now lets put the SCTP header in place */
5231 	initackm_out = mtod(m, struct sctp_init_msg *);
5232 	initackm_out->sh.src_port = inp->sctp_lport;
5233 	initackm_out->sh.dest_port = sh->src_port;
5234 	initackm_out->sh.v_tag = init_chk->init.initiate_tag;
5235 	/* Save it off for quick ref */
5236 	stc.peers_vtag = init_chk->init.initiate_tag;
5237 	initackm_out->sh.checksum = 0;	/* calculate later */
5238 	/* who are we */
5239 	memcpy(stc.identification, SCTP_VERSION_STRING,
5240 	    min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5241 	/* now the chunk header */
5242 	initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
5243 	initackm_out->msg.ch.chunk_flags = 0;
5244 	/* fill in later from mbuf we build */
5245 	initackm_out->msg.ch.chunk_length = 0;
5246 	/* place in my tag */
5247 	if ((asoc != NULL) &&
5248 	    ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5249 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5250 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5251 		/* re-use the v-tags and init-seq here */
5252 		initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
5253 		initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
5254 	} else {
5255 		uint32_t vtag, itsn;
5256 
5257 		if (hold_inp_lock) {
5258 			SCTP_INP_INCR_REF(inp);
5259 			SCTP_INP_RUNLOCK(inp);
5260 		}
5261 		if (asoc) {
5262 			atomic_add_int(&asoc->refcnt, 1);
5263 			SCTP_TCB_UNLOCK(stcb);
5264 			vtag = sctp_select_a_tag(inp, 1);
5265 			initackm_out->msg.init.initiate_tag = htonl(vtag);
5266 			/* get a TSN to use too */
5267 			itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5268 			initackm_out->msg.init.initial_tsn = htonl(itsn);
5269 			SCTP_TCB_LOCK(stcb);
5270 			atomic_add_int(&asoc->refcnt, -1);
5271 		} else {
5272 			vtag = sctp_select_a_tag(inp, 1);
5273 			initackm_out->msg.init.initiate_tag = htonl(vtag);
5274 			/* get a TSN to use too */
5275 			initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5276 		}
5277 		if (hold_inp_lock) {
5278 			SCTP_INP_RLOCK(inp);
5279 			SCTP_INP_DECR_REF(inp);
5280 		}
5281 	}
5282 	/* save away my tag to */
5283 	stc.my_vtag = initackm_out->msg.init.initiate_tag;
5284 
5285 	/* set up some of the credits. */
5286 	so = inp->sctp_socket;
5287 	if (so == NULL) {
5288 		/* memory problem */
5289 		sctp_m_freem(m);
5290 		return;
5291 	} else {
5292 		initackm_out->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5293 	}
5294 	/* set what I want */
5295 	his_limit = ntohs(init_chk->init.num_inbound_streams);
5296 	/* choose what I want */
5297 	if (asoc != NULL) {
5298 		if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5299 			i_want = asoc->streamoutcnt;
5300 		} else {
5301 			i_want = inp->sctp_ep.pre_open_stream_count;
5302 		}
5303 	} else {
5304 		i_want = inp->sctp_ep.pre_open_stream_count;
5305 	}
5306 	if (his_limit < i_want) {
5307 		/* I Want more :< */
5308 		initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
5309 	} else {
5310 		/* I can have what I want :> */
5311 		initackm_out->msg.init.num_outbound_streams = htons(i_want);
5312 	}
5313 	/* tell him his limt. */
5314 	initackm_out->msg.init.num_inbound_streams =
5315 	    htons(inp->sctp_ep.max_open_streams_intome);
5316 
5317 	/* adaptation layer indication parameter */
5318 	ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initackm_out + sizeof(*initackm_out));
5319 	ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5320 	ali->ph.param_length = htons(sizeof(*ali));
5321 	ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5322 	SCTP_BUF_LEN(m) += sizeof(*ali);
5323 	ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
5324 
5325 	/* ECN parameter */
5326 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
5327 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5328 		ecn->ph.param_length = htons(sizeof(*ecn));
5329 		SCTP_BUF_LEN(m) += sizeof(*ecn);
5330 
5331 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5332 		    sizeof(*ecn));
5333 	} else {
5334 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5335 	}
5336 	/* And now tell the peer we do  pr-sctp */
5337 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5338 	prsctp->ph.param_length = htons(sizeof(*prsctp));
5339 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
5340 
5341 	/* And now tell the peer we do all the extensions */
5342 	pr_supported = (struct sctp_supported_chunk_types_param *)
5343 	    ((caddr_t)prsctp + sizeof(*prsctp));
5344 
5345 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5346 	num_ext = 0;
5347 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5348 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5349 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5350 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5351 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5352 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5353 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5354 	p_len = sizeof(*pr_supported) + num_ext;
5355 	pr_supported->ph.param_length = htons(p_len);
5356 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5357 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5358 
5359 	/* ECN nonce: And now tell the peer we support ECN nonce */
5360 	if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
5361 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
5362 		    ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
5363 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
5364 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
5365 		SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
5366 	}
5367 	/* add authentication parameters */
5368 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5369 		struct sctp_auth_random *randp;
5370 		struct sctp_auth_hmac_algo *hmacs;
5371 		struct sctp_auth_chunk_list *chunks;
5372 		uint16_t random_len;
5373 
5374 		/* generate and add RANDOM parameter */
5375 		random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5376 		randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5377 		randp->ph.param_type = htons(SCTP_RANDOM);
5378 		p_len = sizeof(*randp) + random_len;
5379 		randp->ph.param_length = htons(p_len);
5380 		SCTP_READ_RANDOM(randp->random_data, random_len);
5381 		/* zero out any padding required */
5382 		bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5383 		SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5384 
5385 		/* add HMAC_ALGO parameter */
5386 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5387 		p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5388 		    (uint8_t *) hmacs->hmac_ids);
5389 		if (p_len > 0) {
5390 			p_len += sizeof(*hmacs);
5391 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5392 			hmacs->ph.param_length = htons(p_len);
5393 			/* zero out any padding required */
5394 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5395 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5396 		}
5397 		/* add CHUNKS parameter */
5398 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5399 		p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5400 		    chunks->chunk_types);
5401 		if (p_len > 0) {
5402 			p_len += sizeof(*chunks);
5403 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5404 			chunks->ph.param_length = htons(p_len);
5405 			/* zero out any padding required */
5406 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5407 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5408 		}
5409 	}
5410 	m_at = m;
5411 	/* now the addresses */
5412 	{
5413 		struct sctp_scoping scp;
5414 
5415 		/*
5416 		 * To optimize this we could put the scoping stuff into a
5417 		 * structure and remove the individual uint8's from the stc
5418 		 * structure. Then we could just sifa in the address within
5419 		 * the stc.. but for now this is a quick hack to get the
5420 		 * address stuff teased apart.
5421 		 */
5422 		scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5423 		scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5424 		scp.loopback_scope = stc.loopback_scope;
5425 		scp.ipv4_local_scope = stc.ipv4_scope;
5426 		scp.local_scope = stc.local_scope;
5427 		scp.site_scope = stc.site_scope;
5428 		m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
5429 	}
5430 
5431 	/* tack on the operational error if present */
5432 	if (op_err) {
5433 		struct mbuf *ol;
5434 		int llen;
5435 
5436 		llen = 0;
5437 		ol = op_err;
5438 		while (ol) {
5439 			llen += SCTP_BUF_LEN(ol);
5440 			ol = SCTP_BUF_NEXT(ol);
5441 		}
5442 		if (llen % 4) {
5443 			/* must add a pad to the param */
5444 			uint32_t cpthis = 0;
5445 			int padlen;
5446 
5447 			padlen = 4 - (llen % 4);
5448 			m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5449 		}
5450 		while (SCTP_BUF_NEXT(m_at) != NULL) {
5451 			m_at = SCTP_BUF_NEXT(m_at);
5452 		}
5453 		SCTP_BUF_NEXT(m_at) = op_err;
5454 		while (SCTP_BUF_NEXT(m_at) != NULL) {
5455 			m_at = SCTP_BUF_NEXT(m_at);
5456 		}
5457 	}
5458 	/* pre-calulate the size and update pkt header and chunk header */
5459 	p_len = 0;
5460 	for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5461 		p_len += SCTP_BUF_LEN(m_tmp);
5462 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5463 			/* m_tmp should now point to last one */
5464 			break;
5465 		}
5466 	}
5467 
5468 	/* Now we must build a cookie */
5469 	m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
5470 	    sizeof(struct sctphdr), &stc, &signature);
5471 	if (m_cookie == NULL) {
5472 		/* memory problem */
5473 		sctp_m_freem(m);
5474 		return;
5475 	}
5476 	/* Now append the cookie to the end and update the space/size */
5477 	SCTP_BUF_NEXT(m_tmp) = m_cookie;
5478 
5479 	for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5480 		p_len += SCTP_BUF_LEN(m_tmp);
5481 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5482 			/* m_tmp should now point to last one */
5483 			mp_last = m_tmp;
5484 			break;
5485 		}
5486 	}
5487 	/*
5488 	 * Place in the size, but we don't include the last pad (if any) in
5489 	 * the INIT-ACK.
5490 	 */
5491 	initackm_out->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
5492 
5493 	/*
5494 	 * Time to sign the cookie, we don't sign over the cookie signature
5495 	 * though thus we set trailer.
5496 	 */
5497 	(void)sctp_hmac_m(SCTP_HMAC,
5498 	    (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
5499 	    SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
5500 	    (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
5501 	/*
5502 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
5503 	 * here since the timer will drive a retranmission.
5504 	 */
5505 	padval = p_len % 4;
5506 	if ((padval) && (mp_last)) {
5507 		/* see my previous comments on mp_last */
5508 		int ret;
5509 
5510 		ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
5511 		if (ret) {
5512 			/* Houston we have a problem, no space */
5513 			sctp_m_freem(m);
5514 			return;
5515 		}
5516 		p_len += padval;
5517 	}
5518 	if (stc.loopback_scope) {
5519 		over_addr = &store1;
5520 	} else {
5521 		over_addr = NULL;
5522 
5523 	}
5524 
5525 	(void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
5526 	    NULL, 0, port, SCTP_SO_NOT_LOCKED, over_addr);
5527 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5528 }
5529 
5530 
5531 void
5532 sctp_insert_on_wheel(struct sctp_tcb *stcb,
5533     struct sctp_association *asoc,
5534     struct sctp_stream_out *strq, int holds_lock)
5535 {
5536 	struct sctp_stream_out *stre, *strn;
5537 
5538 	if (holds_lock == 0) {
5539 		SCTP_TCB_SEND_LOCK(stcb);
5540 	}
5541 	if ((strq->next_spoke.tqe_next) ||
5542 	    (strq->next_spoke.tqe_prev)) {
5543 		/* already on wheel */
5544 		goto outof_here;
5545 	}
5546 	stre = TAILQ_FIRST(&asoc->out_wheel);
5547 	if (stre == NULL) {
5548 		/* only one on wheel */
5549 		TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
5550 		goto outof_here;
5551 	}
5552 	for (; stre; stre = strn) {
5553 		strn = TAILQ_NEXT(stre, next_spoke);
5554 		if (stre->stream_no > strq->stream_no) {
5555 			TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
5556 			goto outof_here;
5557 		} else if (stre->stream_no == strq->stream_no) {
5558 			/* huh, should not happen */
5559 			goto outof_here;
5560 		} else if (strn == NULL) {
5561 			/* next one is null */
5562 			TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
5563 			    next_spoke);
5564 		}
5565 	}
5566 outof_here:
5567 	if (holds_lock == 0) {
5568 		SCTP_TCB_SEND_UNLOCK(stcb);
5569 	}
5570 }
5571 
5572 static void
5573 sctp_remove_from_wheel(struct sctp_tcb *stcb,
5574     struct sctp_association *asoc,
5575     struct sctp_stream_out *strq)
5576 {
5577 	/* take off and then setup so we know it is not on the wheel */
5578 	SCTP_TCB_SEND_LOCK(stcb);
5579 	if (TAILQ_FIRST(&strq->outqueue)) {
5580 		/* more was added */
5581 		SCTP_TCB_SEND_UNLOCK(stcb);
5582 		return;
5583 	}
5584 	TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
5585 	strq->next_spoke.tqe_next = NULL;
5586 	strq->next_spoke.tqe_prev = NULL;
5587 	SCTP_TCB_SEND_UNLOCK(stcb);
5588 }
5589 
5590 static void
5591 sctp_prune_prsctp(struct sctp_tcb *stcb,
5592     struct sctp_association *asoc,
5593     struct sctp_sndrcvinfo *srcv,
5594     int dataout)
5595 {
5596 	int freed_spc = 0;
5597 	struct sctp_tmit_chunk *chk, *nchk;
5598 
5599 	SCTP_TCB_LOCK_ASSERT(stcb);
5600 	if ((asoc->peer_supports_prsctp) &&
5601 	    (asoc->sent_queue_cnt_removeable > 0)) {
5602 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5603 			/*
5604 			 * Look for chunks marked with the PR_SCTP flag AND
5605 			 * the buffer space flag. If the one being sent is
5606 			 * equal or greater priority then purge the old one
5607 			 * and free some space.
5608 			 */
5609 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5610 				/*
5611 				 * This one is PR-SCTP AND buffer space
5612 				 * limited type
5613 				 */
5614 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5615 					/*
5616 					 * Lower numbers equates to higher
5617 					 * priority so if the one we are
5618 					 * looking at has a larger or equal
5619 					 * priority we want to drop the data
5620 					 * and NOT retransmit it.
5621 					 */
5622 					if (chk->data) {
5623 						/*
5624 						 * We release the book_size
5625 						 * if the mbuf is here
5626 						 */
5627 						int ret_spc;
5628 						int cause;
5629 
5630 						if (chk->sent > SCTP_DATAGRAM_UNSENT)
5631 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
5632 						else
5633 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
5634 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5635 						    cause,
5636 						    &asoc->sent_queue, SCTP_SO_LOCKED);
5637 						freed_spc += ret_spc;
5638 						if (freed_spc >= dataout) {
5639 							return;
5640 						}
5641 					}	/* if chunk was present */
5642 				}	/* if of sufficent priority */
5643 			}	/* if chunk has enabled */
5644 		}		/* tailqforeach */
5645 
5646 		chk = TAILQ_FIRST(&asoc->send_queue);
5647 		while (chk) {
5648 			nchk = TAILQ_NEXT(chk, sctp_next);
5649 			/* Here we must move to the sent queue and mark */
5650 			if (PR_SCTP_TTL_ENABLED(chk->flags)) {
5651 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5652 					if (chk->data) {
5653 						/*
5654 						 * We release the book_size
5655 						 * if the mbuf is here
5656 						 */
5657 						int ret_spc;
5658 
5659 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5660 						    SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
5661 						    &asoc->send_queue, SCTP_SO_LOCKED);
5662 
5663 						freed_spc += ret_spc;
5664 						if (freed_spc >= dataout) {
5665 							return;
5666 						}
5667 					}	/* end if chk->data */
5668 				}	/* end if right class */
5669 			}	/* end if chk pr-sctp */
5670 			chk = nchk;
5671 		}		/* end while (chk) */
5672 	}			/* if enabled in asoc */
5673 }
5674 
5675 int
5676 sctp_get_frag_point(struct sctp_tcb *stcb,
5677     struct sctp_association *asoc)
5678 {
5679 	int siz, ovh;
5680 
5681 	/*
5682 	 * For endpoints that have both v6 and v4 addresses we must reserve
5683 	 * room for the ipv6 header, for those that are only dealing with V4
5684 	 * we use a larger frag point.
5685 	 */
5686 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5687 		ovh = SCTP_MED_OVERHEAD;
5688 	} else {
5689 		ovh = SCTP_MED_V4_OVERHEAD;
5690 	}
5691 
5692 	if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
5693 		siz = asoc->smallest_mtu - ovh;
5694 	else
5695 		siz = (stcb->asoc.sctp_frag_point - ovh);
5696 	/*
5697 	 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
5698 	 */
5699 	/* A data chunk MUST fit in a cluster */
5700 	/* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
5701 	/* } */
5702 
5703 	/* adjust for an AUTH chunk if DATA requires auth */
5704 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
5705 		siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5706 
5707 	if (siz % 4) {
5708 		/* make it an even word boundary please */
5709 		siz -= (siz % 4);
5710 	}
5711 	return (siz);
5712 }
5713 
5714 static void
5715 sctp_set_prsctp_policy(struct sctp_tcb *stcb,
5716     struct sctp_stream_queue_pending *sp)
5717 {
5718 	sp->pr_sctp_on = 0;
5719 	if (stcb->asoc.peer_supports_prsctp) {
5720 		/*
5721 		 * We assume that the user wants PR_SCTP_TTL if the user
5722 		 * provides a positive lifetime but does not specify any
5723 		 * PR_SCTP policy. This is a BAD assumption and causes
5724 		 * problems at least with the U-Vancovers MPI folks. I will
5725 		 * change this to be no policy means NO PR-SCTP.
5726 		 */
5727 		if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
5728 			sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
5729 			sp->pr_sctp_on = 1;
5730 		} else {
5731 			return;
5732 		}
5733 		switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
5734 		case CHUNK_FLAGS_PR_SCTP_BUF:
5735 			/*
5736 			 * Time to live is a priority stored in tv_sec when
5737 			 * doing the buffer drop thing.
5738 			 */
5739 			sp->ts.tv_sec = sp->timetolive;
5740 			sp->ts.tv_usec = 0;
5741 			break;
5742 		case CHUNK_FLAGS_PR_SCTP_TTL:
5743 			{
5744 				struct timeval tv;
5745 
5746 				(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5747 				tv.tv_sec = sp->timetolive / 1000;
5748 				tv.tv_usec = (sp->timetolive * 1000) % 1000000;
5749 				/*
5750 				 * TODO sctp_constants.h needs alternative
5751 				 * time macros when _KERNEL is undefined.
5752 				 */
5753 				timevaladd(&sp->ts, &tv);
5754 			}
5755 			break;
5756 		case CHUNK_FLAGS_PR_SCTP_RTX:
5757 			/*
5758 			 * Time to live is a the number or retransmissions
5759 			 * stored in tv_sec.
5760 			 */
5761 			sp->ts.tv_sec = sp->timetolive;
5762 			sp->ts.tv_usec = 0;
5763 			break;
5764 		default:
5765 			SCTPDBG(SCTP_DEBUG_USRREQ1,
5766 			    "Unknown PR_SCTP policy %u.\n",
5767 			    PR_SCTP_POLICY(sp->sinfo_flags));
5768 			break;
5769 		}
5770 	}
5771 }
5772 
5773 static int
5774 sctp_msg_append(struct sctp_tcb *stcb,
5775     struct sctp_nets *net,
5776     struct mbuf *m,
5777     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5778 {
5779 	int error = 0, holds_lock;
5780 	struct mbuf *at;
5781 	struct sctp_stream_queue_pending *sp = NULL;
5782 	struct sctp_stream_out *strm;
5783 
5784 	/*
5785 	 * Given an mbuf chain, put it into the association send queue and
5786 	 * place it on the wheel
5787 	 */
5788 	holds_lock = hold_stcb_lock;
5789 	if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5790 		/* Invalid stream number */
5791 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5792 		error = EINVAL;
5793 		goto out_now;
5794 	}
5795 	if ((stcb->asoc.stream_locked) &&
5796 	    (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5797 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5798 		error = EINVAL;
5799 		goto out_now;
5800 	}
5801 	strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5802 	/* Now can we send this? */
5803 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5804 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5805 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5806 	    (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5807 		/* got data while shutting down */
5808 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
5809 		error = ECONNRESET;
5810 		goto out_now;
5811 	}
5812 	sctp_alloc_a_strmoq(stcb, sp);
5813 	if (sp == NULL) {
5814 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5815 		error = ENOMEM;
5816 		goto out_now;
5817 	}
5818 	sp->sinfo_flags = srcv->sinfo_flags;
5819 	sp->timetolive = srcv->sinfo_timetolive;
5820 	sp->ppid = srcv->sinfo_ppid;
5821 	sp->context = srcv->sinfo_context;
5822 	sp->strseq = 0;
5823 	if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5824 		sp->net = net;
5825 		sp->addr_over = 1;
5826 	} else {
5827 		sp->net = stcb->asoc.primary_destination;
5828 		sp->addr_over = 0;
5829 	}
5830 	atomic_add_int(&sp->net->ref_count, 1);
5831 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5832 	sp->stream = srcv->sinfo_stream;
5833 	sp->msg_is_complete = 1;
5834 	sp->sender_all_done = 1;
5835 	sp->some_taken = 0;
5836 	sp->data = m;
5837 	sp->tail_mbuf = NULL;
5838 	sp->length = 0;
5839 	at = m;
5840 	sctp_set_prsctp_policy(stcb, sp);
5841 	/*
5842 	 * We could in theory (for sendall) sifa the length in, but we would
5843 	 * still have to hunt through the chain since we need to setup the
5844 	 * tail_mbuf
5845 	 */
5846 	while (at) {
5847 		if (SCTP_BUF_NEXT(at) == NULL)
5848 			sp->tail_mbuf = at;
5849 		sp->length += SCTP_BUF_LEN(at);
5850 		at = SCTP_BUF_NEXT(at);
5851 	}
5852 	SCTP_TCB_SEND_LOCK(stcb);
5853 	sctp_snd_sb_alloc(stcb, sp->length);
5854 	atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
5855 	TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
5856 	if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
5857 		sp->strseq = strm->next_sequence_sent;
5858 		strm->next_sequence_sent++;
5859 	}
5860 	if ((strm->next_spoke.tqe_next == NULL) &&
5861 	    (strm->next_spoke.tqe_prev == NULL)) {
5862 		/* Not on wheel, insert */
5863 		sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
5864 	}
5865 	m = NULL;
5866 	SCTP_TCB_SEND_UNLOCK(stcb);
5867 out_now:
5868 	if (m) {
5869 		sctp_m_freem(m);
5870 	}
5871 	return (error);
5872 }
5873 
5874 
5875 static struct mbuf *
5876 sctp_copy_mbufchain(struct mbuf *clonechain,
5877     struct mbuf *outchain,
5878     struct mbuf **endofchain,
5879     int can_take_mbuf,
5880     int sizeofcpy,
5881     uint8_t copy_by_ref)
5882 {
5883 	struct mbuf *m;
5884 	struct mbuf *appendchain;
5885 	caddr_t cp;
5886 	int len;
5887 
5888 	if (endofchain == NULL) {
5889 		/* error */
5890 error_out:
5891 		if (outchain)
5892 			sctp_m_freem(outchain);
5893 		return (NULL);
5894 	}
5895 	if (can_take_mbuf) {
5896 		appendchain = clonechain;
5897 	} else {
5898 		if (!copy_by_ref &&
5899 		    (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
5900 		    ) {
5901 			/* Its not in a cluster */
5902 			if (*endofchain == NULL) {
5903 				/* lets get a mbuf cluster */
5904 				if (outchain == NULL) {
5905 					/* This is the general case */
5906 			new_mbuf:
5907 					outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5908 					if (outchain == NULL) {
5909 						goto error_out;
5910 					}
5911 					SCTP_BUF_LEN(outchain) = 0;
5912 					*endofchain = outchain;
5913 					/* get the prepend space */
5914 					SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
5915 				} else {
5916 					/*
5917 					 * We really should not get a NULL
5918 					 * in endofchain
5919 					 */
5920 					/* find end */
5921 					m = outchain;
5922 					while (m) {
5923 						if (SCTP_BUF_NEXT(m) == NULL) {
5924 							*endofchain = m;
5925 							break;
5926 						}
5927 						m = SCTP_BUF_NEXT(m);
5928 					}
5929 					/* sanity */
5930 					if (*endofchain == NULL) {
5931 						/*
5932 						 * huh, TSNH XXX maybe we
5933 						 * should panic
5934 						 */
5935 						sctp_m_freem(outchain);
5936 						goto new_mbuf;
5937 					}
5938 				}
5939 				/* get the new end of length */
5940 				len = M_TRAILINGSPACE(*endofchain);
5941 			} else {
5942 				/* how much is left at the end? */
5943 				len = M_TRAILINGSPACE(*endofchain);
5944 			}
5945 			/* Find the end of the data, for appending */
5946 			cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
5947 
5948 			/* Now lets copy it out */
5949 			if (len >= sizeofcpy) {
5950 				/* It all fits, copy it in */
5951 				m_copydata(clonechain, 0, sizeofcpy, cp);
5952 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5953 			} else {
5954 				/* fill up the end of the chain */
5955 				if (len > 0) {
5956 					m_copydata(clonechain, 0, len, cp);
5957 					SCTP_BUF_LEN((*endofchain)) += len;
5958 					/* now we need another one */
5959 					sizeofcpy -= len;
5960 				}
5961 				m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5962 				if (m == NULL) {
5963 					/* We failed */
5964 					goto error_out;
5965 				}
5966 				SCTP_BUF_NEXT((*endofchain)) = m;
5967 				*endofchain = m;
5968 				cp = mtod((*endofchain), caddr_t);
5969 				m_copydata(clonechain, len, sizeofcpy, cp);
5970 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5971 			}
5972 			return (outchain);
5973 		} else {
5974 			/* copy the old fashion way */
5975 			appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
5976 #ifdef SCTP_MBUF_LOGGING
5977 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5978 				struct mbuf *mat;
5979 
5980 				mat = appendchain;
5981 				while (mat) {
5982 					if (SCTP_BUF_IS_EXTENDED(mat)) {
5983 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5984 					}
5985 					mat = SCTP_BUF_NEXT(mat);
5986 				}
5987 			}
5988 #endif
5989 		}
5990 	}
5991 	if (appendchain == NULL) {
5992 		/* error */
5993 		if (outchain)
5994 			sctp_m_freem(outchain);
5995 		return (NULL);
5996 	}
5997 	if (outchain) {
5998 		/* tack on to the end */
5999 		if (*endofchain != NULL) {
6000 			SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6001 		} else {
6002 			m = outchain;
6003 			while (m) {
6004 				if (SCTP_BUF_NEXT(m) == NULL) {
6005 					SCTP_BUF_NEXT(m) = appendchain;
6006 					break;
6007 				}
6008 				m = SCTP_BUF_NEXT(m);
6009 			}
6010 		}
6011 		/*
6012 		 * save off the end and update the end-chain postion
6013 		 */
6014 		m = appendchain;
6015 		while (m) {
6016 			if (SCTP_BUF_NEXT(m) == NULL) {
6017 				*endofchain = m;
6018 				break;
6019 			}
6020 			m = SCTP_BUF_NEXT(m);
6021 		}
6022 		return (outchain);
6023 	} else {
6024 		/* save off the end and update the end-chain postion */
6025 		m = appendchain;
6026 		while (m) {
6027 			if (SCTP_BUF_NEXT(m) == NULL) {
6028 				*endofchain = m;
6029 				break;
6030 			}
6031 			m = SCTP_BUF_NEXT(m);
6032 		}
6033 		return (appendchain);
6034 	}
6035 }
6036 
6037 int
6038 sctp_med_chunk_output(struct sctp_inpcb *inp,
6039     struct sctp_tcb *stcb,
6040     struct sctp_association *asoc,
6041     int *num_out,
6042     int *reason_code,
6043     int control_only, int *cwnd_full, int from_where,
6044     struct timeval *now, int *now_filled, int frag_point, int so_locked
6045 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6046     SCTP_UNUSED
6047 #endif
6048 );
6049 
6050 static void
6051 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6052     uint32_t val)
6053 {
6054 	struct sctp_copy_all *ca;
6055 	struct mbuf *m;
6056 	int ret = 0;
6057 	int added_control = 0;
6058 	int un_sent, do_chunk_output = 1;
6059 	struct sctp_association *asoc;
6060 
6061 	ca = (struct sctp_copy_all *)ptr;
6062 	if (ca->m == NULL) {
6063 		return;
6064 	}
6065 	if (ca->inp != inp) {
6066 		/* TSNH */
6067 		return;
6068 	}
6069 	if ((ca->m) && ca->sndlen) {
6070 		m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6071 		if (m == NULL) {
6072 			/* can't copy so we are done */
6073 			ca->cnt_failed++;
6074 			return;
6075 		}
6076 #ifdef SCTP_MBUF_LOGGING
6077 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6078 			struct mbuf *mat;
6079 
6080 			mat = m;
6081 			while (mat) {
6082 				if (SCTP_BUF_IS_EXTENDED(mat)) {
6083 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6084 				}
6085 				mat = SCTP_BUF_NEXT(mat);
6086 			}
6087 		}
6088 #endif
6089 	} else {
6090 		m = NULL;
6091 	}
6092 	SCTP_TCB_LOCK_ASSERT(stcb);
6093 	if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6094 		/* Abort this assoc with m as the user defined reason */
6095 		if (m) {
6096 			struct sctp_paramhdr *ph;
6097 
6098 			SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6099 			if (m) {
6100 				ph = mtod(m, struct sctp_paramhdr *);
6101 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6102 				ph->param_length = htons(ca->sndlen);
6103 			}
6104 			/*
6105 			 * We add one here to keep the assoc from
6106 			 * dis-appearing on us.
6107 			 */
6108 			atomic_add_int(&stcb->asoc.refcnt, 1);
6109 			sctp_abort_an_association(inp, stcb,
6110 			    SCTP_RESPONSE_TO_USER_REQ,
6111 			    m, SCTP_SO_NOT_LOCKED);
6112 			/*
6113 			 * sctp_abort_an_association calls sctp_free_asoc()
6114 			 * free association will NOT free it since we
6115 			 * incremented the refcnt .. we do this to prevent
6116 			 * it being freed and things getting tricky since we
6117 			 * could end up (from free_asoc) calling inpcb_free
6118 			 * which would get a recursive lock call to the
6119 			 * iterator lock.. But as a consequence of that the
6120 			 * stcb will return to us un-locked.. since
6121 			 * free_asoc returns with either no TCB or the TCB
6122 			 * unlocked, we must relock.. to unlock in the
6123 			 * iterator timer :-0
6124 			 */
6125 			SCTP_TCB_LOCK(stcb);
6126 			atomic_add_int(&stcb->asoc.refcnt, -1);
6127 			goto no_chunk_output;
6128 		}
6129 	} else {
6130 		if (m) {
6131 			ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
6132 			    &ca->sndrcv, 1);
6133 		}
6134 		asoc = &stcb->asoc;
6135 		if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6136 			/* shutdown this assoc */
6137 			int cnt;
6138 
6139 			cnt = sctp_is_there_unsent_data(stcb);
6140 
6141 			if (TAILQ_EMPTY(&asoc->send_queue) &&
6142 			    TAILQ_EMPTY(&asoc->sent_queue) &&
6143 			    (cnt == 0)) {
6144 				if (asoc->locked_on_sending) {
6145 					goto abort_anyway;
6146 				}
6147 				/*
6148 				 * there is nothing queued to send, so I'm
6149 				 * done...
6150 				 */
6151 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6152 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6153 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6154 					/*
6155 					 * only send SHUTDOWN the first time
6156 					 * through
6157 					 */
6158 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
6159 					if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6160 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6161 					}
6162 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6163 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6164 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6165 					    asoc->primary_destination);
6166 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6167 					    asoc->primary_destination);
6168 					added_control = 1;
6169 					do_chunk_output = 0;
6170 				}
6171 			} else {
6172 				/*
6173 				 * we still got (or just got) data to send,
6174 				 * so set SHUTDOWN_PENDING
6175 				 */
6176 				/*
6177 				 * XXX sockets draft says that SCTP_EOF
6178 				 * should be sent with no data.  currently,
6179 				 * we will allow user data to be sent first
6180 				 * and move to SHUTDOWN-PENDING
6181 				 */
6182 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6183 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6184 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6185 					if (asoc->locked_on_sending) {
6186 						/*
6187 						 * Locked to send out the
6188 						 * data
6189 						 */
6190 						struct sctp_stream_queue_pending *sp;
6191 
6192 						sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6193 						if (sp) {
6194 							if ((sp->length == 0) && (sp->msg_is_complete == 0))
6195 								asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6196 						}
6197 					}
6198 					asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6199 					if (TAILQ_EMPTY(&asoc->send_queue) &&
6200 					    TAILQ_EMPTY(&asoc->sent_queue) &&
6201 					    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6202 				abort_anyway:
6203 						atomic_add_int(&stcb->asoc.refcnt, 1);
6204 						sctp_abort_an_association(stcb->sctp_ep, stcb,
6205 						    SCTP_RESPONSE_TO_USER_REQ,
6206 						    NULL, SCTP_SO_NOT_LOCKED);
6207 						atomic_add_int(&stcb->asoc.refcnt, -1);
6208 						goto no_chunk_output;
6209 					}
6210 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6211 					    asoc->primary_destination);
6212 				}
6213 			}
6214 
6215 		}
6216 	}
6217 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6218 	    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6219 
6220 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6221 	    (stcb->asoc.total_flight > 0) &&
6222 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6223 	    ) {
6224 		do_chunk_output = 0;
6225 	}
6226 	if (do_chunk_output)
6227 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6228 	else if (added_control) {
6229 		int num_out = 0, reason = 0, cwnd_full = 0, now_filled = 0;
6230 		struct timeval now;
6231 		int frag_point;
6232 
6233 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6234 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6235 		    &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6236 	}
6237 no_chunk_output:
6238 	if (ret) {
6239 		ca->cnt_failed++;
6240 	} else {
6241 		ca->cnt_sent++;
6242 	}
6243 }
6244 
6245 static void
6246 sctp_sendall_completes(void *ptr, uint32_t val)
6247 {
6248 	struct sctp_copy_all *ca;
6249 
6250 	ca = (struct sctp_copy_all *)ptr;
6251 	/*
6252 	 * Do a notify here? Kacheong suggests that the notify be done at
6253 	 * the send time.. so you would push up a notification if any send
6254 	 * failed. Don't know if this is feasable since the only failures we
6255 	 * have is "memory" related and if you cannot get an mbuf to send
6256 	 * the data you surely can't get an mbuf to send up to notify the
6257 	 * user you can't send the data :->
6258 	 */
6259 
6260 	/* now free everything */
6261 	sctp_m_freem(ca->m);
6262 	SCTP_FREE(ca, SCTP_M_COPYAL);
6263 }
6264 
6265 
6266 #define	MC_ALIGN(m, len) do {						\
6267 	SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1));	\
6268 } while (0)
6269 
6270 
6271 
6272 static struct mbuf *
6273 sctp_copy_out_all(struct uio *uio, int len)
6274 {
6275 	struct mbuf *ret, *at;
6276 	int left, willcpy, cancpy, error;
6277 
6278 	ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6279 	if (ret == NULL) {
6280 		/* TSNH */
6281 		return (NULL);
6282 	}
6283 	left = len;
6284 	SCTP_BUF_LEN(ret) = 0;
6285 	/* save space for the data chunk header */
6286 	cancpy = M_TRAILINGSPACE(ret);
6287 	willcpy = min(cancpy, left);
6288 	at = ret;
6289 	while (left > 0) {
6290 		/* Align data to the end */
6291 		error = uiomove(mtod(at, caddr_t), willcpy, uio);
6292 		if (error) {
6293 	err_out_now:
6294 			sctp_m_freem(at);
6295 			return (NULL);
6296 		}
6297 		SCTP_BUF_LEN(at) = willcpy;
6298 		SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6299 		left -= willcpy;
6300 		if (left > 0) {
6301 			SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6302 			if (SCTP_BUF_NEXT(at) == NULL) {
6303 				goto err_out_now;
6304 			}
6305 			at = SCTP_BUF_NEXT(at);
6306 			SCTP_BUF_LEN(at) = 0;
6307 			cancpy = M_TRAILINGSPACE(at);
6308 			willcpy = min(cancpy, left);
6309 		}
6310 	}
6311 	return (ret);
6312 }
6313 
6314 static int
6315 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6316     struct sctp_sndrcvinfo *srcv)
6317 {
6318 	int ret;
6319 	struct sctp_copy_all *ca;
6320 
6321 	SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6322 	    SCTP_M_COPYAL);
6323 	if (ca == NULL) {
6324 		sctp_m_freem(m);
6325 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6326 		return (ENOMEM);
6327 	}
6328 	memset(ca, 0, sizeof(struct sctp_copy_all));
6329 
6330 	ca->inp = inp;
6331 	memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6332 	/*
6333 	 * take off the sendall flag, it would be bad if we failed to do
6334 	 * this :-0
6335 	 */
6336 	ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6337 	/* get length and mbuf chain */
6338 	if (uio) {
6339 		ca->sndlen = uio->uio_resid;
6340 		ca->m = sctp_copy_out_all(uio, ca->sndlen);
6341 		if (ca->m == NULL) {
6342 			SCTP_FREE(ca, SCTP_M_COPYAL);
6343 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6344 			return (ENOMEM);
6345 		}
6346 	} else {
6347 		/* Gather the length of the send */
6348 		struct mbuf *mat;
6349 
6350 		mat = m;
6351 		ca->sndlen = 0;
6352 		while (m) {
6353 			ca->sndlen += SCTP_BUF_LEN(m);
6354 			m = SCTP_BUF_NEXT(m);
6355 		}
6356 		ca->m = mat;
6357 	}
6358 	ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6359 	    SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6360 	    SCTP_ASOC_ANY_STATE,
6361 	    (void *)ca, 0,
6362 	    sctp_sendall_completes, inp, 1);
6363 	if (ret) {
6364 		SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6365 		SCTP_FREE(ca, SCTP_M_COPYAL);
6366 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6367 		return (EFAULT);
6368 	}
6369 	return (0);
6370 }
6371 
6372 
6373 void
6374 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6375 {
6376 	struct sctp_tmit_chunk *chk, *nchk;
6377 
6378 	chk = TAILQ_FIRST(&asoc->control_send_queue);
6379 	while (chk) {
6380 		nchk = TAILQ_NEXT(chk, sctp_next);
6381 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6382 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6383 			if (chk->data) {
6384 				sctp_m_freem(chk->data);
6385 				chk->data = NULL;
6386 			}
6387 			asoc->ctrl_queue_cnt--;
6388 			sctp_free_a_chunk(stcb, chk);
6389 		}
6390 		chk = nchk;
6391 	}
6392 }
6393 
6394 void
6395 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6396 {
6397 	struct sctp_association *asoc;
6398 	struct sctp_tmit_chunk *chk, *chk_tmp;
6399 	struct sctp_asconf_chunk *acp;
6400 
6401 	asoc = &stcb->asoc;
6402 	for (chk = TAILQ_FIRST(&asoc->asconf_send_queue); chk != NULL;
6403 	    chk = chk_tmp) {
6404 		/* get next chk */
6405 		chk_tmp = TAILQ_NEXT(chk, sctp_next);
6406 		/* find SCTP_ASCONF chunk in queue */
6407 		if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6408 			if (chk->data) {
6409 				acp = mtod(chk->data, struct sctp_asconf_chunk *);
6410 				if (compare_with_wrap(ntohl(acp->serial_number), stcb->asoc.asconf_seq_out_acked, MAX_SEQ)) {
6411 					/* Not Acked yet */
6412 					break;
6413 				}
6414 			}
6415 			TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6416 			if (chk->data) {
6417 				sctp_m_freem(chk->data);
6418 				chk->data = NULL;
6419 			}
6420 			asoc->ctrl_queue_cnt--;
6421 			sctp_free_a_chunk(stcb, chk);
6422 		}
6423 	}
6424 }
6425 
6426 
6427 static void
6428 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6429 
6430     struct sctp_association *asoc,
6431     struct sctp_tmit_chunk **data_list,
6432     int bundle_at,
6433     struct sctp_nets *net)
6434 {
6435 	int i;
6436 	struct sctp_tmit_chunk *tp1;
6437 
6438 	for (i = 0; i < bundle_at; i++) {
6439 		/* off of the send queue */
6440 		if (i) {
6441 			/*
6442 			 * Any chunk NOT 0 you zap the time chunk 0 gets
6443 			 * zapped or set based on if a RTO measurment is
6444 			 * needed.
6445 			 */
6446 			data_list[i]->do_rtt = 0;
6447 		}
6448 		/* record time */
6449 		data_list[i]->sent_rcv_time = net->last_sent_time;
6450 		data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6451 		TAILQ_REMOVE(&asoc->send_queue,
6452 		    data_list[i],
6453 		    sctp_next);
6454 		/* on to the sent queue */
6455 		tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6456 		if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
6457 		    data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
6458 			struct sctp_tmit_chunk *tpp;
6459 
6460 			/* need to move back */
6461 	back_up_more:
6462 			tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6463 			if (tpp == NULL) {
6464 				TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6465 				goto all_done;
6466 			}
6467 			tp1 = tpp;
6468 			if (compare_with_wrap(tp1->rec.data.TSN_seq,
6469 			    data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
6470 				goto back_up_more;
6471 			}
6472 			TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6473 		} else {
6474 			TAILQ_INSERT_TAIL(&asoc->sent_queue,
6475 			    data_list[i],
6476 			    sctp_next);
6477 		}
6478 all_done:
6479 		/* This does not lower until the cum-ack passes it */
6480 		asoc->sent_queue_cnt++;
6481 		asoc->send_queue_cnt--;
6482 		if ((asoc->peers_rwnd <= 0) &&
6483 		    (asoc->total_flight == 0) &&
6484 		    (bundle_at == 1)) {
6485 			/* Mark the chunk as being a window probe */
6486 			SCTP_STAT_INCR(sctps_windowprobed);
6487 		}
6488 #ifdef SCTP_AUDITING_ENABLED
6489 		sctp_audit_log(0xC2, 3);
6490 #endif
6491 		data_list[i]->sent = SCTP_DATAGRAM_SENT;
6492 		data_list[i]->snd_count = 1;
6493 		data_list[i]->rec.data.chunk_was_revoked = 0;
6494 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6495 			sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6496 			    data_list[i]->whoTo->flight_size,
6497 			    data_list[i]->book_size,
6498 			    (uintptr_t) data_list[i]->whoTo,
6499 			    data_list[i]->rec.data.TSN_seq);
6500 		}
6501 		sctp_flight_size_increase(data_list[i]);
6502 		sctp_total_flight_increase(stcb, data_list[i]);
6503 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6504 			sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6505 			    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6506 		}
6507 		asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6508 		    (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6509 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6510 			/* SWS sender side engages */
6511 			asoc->peers_rwnd = 0;
6512 		}
6513 	}
6514 }
6515 
6516 static void
6517 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
6518 {
6519 	struct sctp_tmit_chunk *chk, *nchk;
6520 
6521 	for (chk = TAILQ_FIRST(&asoc->control_send_queue);
6522 	    chk; chk = nchk) {
6523 		nchk = TAILQ_NEXT(chk, sctp_next);
6524 		if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6525 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6526 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6527 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6528 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6529 		    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6530 		    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6531 		    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6532 		    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6533 		    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6534 			/* Stray chunks must be cleaned up */
6535 	clean_up_anyway:
6536 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6537 			if (chk->data) {
6538 				sctp_m_freem(chk->data);
6539 				chk->data = NULL;
6540 			}
6541 			asoc->ctrl_queue_cnt--;
6542 			sctp_free_a_chunk(stcb, chk);
6543 		} else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6544 			/* special handling, we must look into the param */
6545 			if (chk != asoc->str_reset) {
6546 				goto clean_up_anyway;
6547 			}
6548 		}
6549 	}
6550 }
6551 
6552 
6553 static int
6554 sctp_can_we_split_this(struct sctp_tcb *stcb,
6555     uint32_t length,
6556     uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6557 {
6558 	/*
6559 	 * Make a decision on if I should split a msg into multiple parts.
6560 	 * This is only asked of incomplete messages.
6561 	 */
6562 	if (eeor_on) {
6563 		/*
6564 		 * If we are doing EEOR we need to always send it if its the
6565 		 * entire thing, since it might be all the guy is putting in
6566 		 * the hopper.
6567 		 */
6568 		if (goal_mtu >= length) {
6569 			/*-
6570 			 * If we have data outstanding,
6571 			 * we get another chance when the sack
6572 			 * arrives to transmit - wait for more data
6573 			 */
6574 			if (stcb->asoc.total_flight == 0) {
6575 				/*
6576 				 * If nothing is in flight, we zero the
6577 				 * packet counter.
6578 				 */
6579 				return (length);
6580 			}
6581 			return (0);
6582 
6583 		} else {
6584 			/* You can fill the rest */
6585 			return (goal_mtu);
6586 		}
6587 	}
6588 	/*-
6589 	 * For those strange folk that make the send buffer
6590 	 * smaller than our fragmentation point, we can't
6591 	 * get a full msg in so we have to allow splitting.
6592 	 */
6593 	if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
6594 		return (length);
6595 	}
6596 	if ((length <= goal_mtu) ||
6597 	    ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
6598 		/* Sub-optimial residual don't split in non-eeor mode. */
6599 		return (0);
6600 	}
6601 	/*
6602 	 * If we reach here length is larger than the goal_mtu. Do we wish
6603 	 * to split it for the sake of packet putting together?
6604 	 */
6605 	if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
6606 		/* Its ok to split it */
6607 		return (min(goal_mtu, frag_point));
6608 	}
6609 	/* Nope, can't split */
6610 	return (0);
6611 
6612 }
6613 
6614 static uint32_t
6615 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
6616     struct sctp_stream_out *strq,
6617     uint32_t goal_mtu,
6618     uint32_t frag_point,
6619     int *locked,
6620     int *giveup,
6621     int eeor_mode,
6622     int *bail)
6623 {
6624 	/* Move from the stream to the send_queue keeping track of the total */
6625 	struct sctp_association *asoc;
6626 	struct sctp_stream_queue_pending *sp;
6627 	struct sctp_tmit_chunk *chk;
6628 	struct sctp_data_chunk *dchkh;
6629 	uint32_t to_move, length;
6630 	uint8_t rcv_flags = 0;
6631 	uint8_t some_taken;
6632 	uint8_t send_lock_up = 0;
6633 
6634 	SCTP_TCB_LOCK_ASSERT(stcb);
6635 	asoc = &stcb->asoc;
6636 one_more_time:
6637 	/* sa_ignore FREED_MEMORY */
6638 	sp = TAILQ_FIRST(&strq->outqueue);
6639 	if (sp == NULL) {
6640 		*locked = 0;
6641 		if (send_lock_up == 0) {
6642 			SCTP_TCB_SEND_LOCK(stcb);
6643 			send_lock_up = 1;
6644 		}
6645 		sp = TAILQ_FIRST(&strq->outqueue);
6646 		if (sp) {
6647 			goto one_more_time;
6648 		}
6649 		if (strq->last_msg_incomplete) {
6650 			SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6651 			    strq->stream_no,
6652 			    strq->last_msg_incomplete);
6653 			strq->last_msg_incomplete = 0;
6654 		}
6655 		to_move = 0;
6656 		if (send_lock_up) {
6657 			SCTP_TCB_SEND_UNLOCK(stcb);
6658 			send_lock_up = 0;
6659 		}
6660 		goto out_of;
6661 	}
6662 	if ((sp->msg_is_complete) && (sp->length == 0)) {
6663 		if (sp->sender_all_done) {
6664 			/*
6665 			 * We are doing differed cleanup. Last time through
6666 			 * when we took all the data the sender_all_done was
6667 			 * not set.
6668 			 */
6669 			if (sp->put_last_out == 0) {
6670 				SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6671 				SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6672 				    sp->sender_all_done,
6673 				    sp->length,
6674 				    sp->msg_is_complete,
6675 				    sp->put_last_out,
6676 				    send_lock_up);
6677 			}
6678 			if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
6679 				SCTP_TCB_SEND_LOCK(stcb);
6680 				send_lock_up = 1;
6681 			}
6682 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6683 			TAILQ_REMOVE(&strq->outqueue, sp, next);
6684 			sctp_free_remote_addr(sp->net);
6685 			if (sp->data) {
6686 				sctp_m_freem(sp->data);
6687 				sp->data = NULL;
6688 			}
6689 			sctp_free_a_strmoq(stcb, sp);
6690 
6691 			/* we can't be locked to it */
6692 			*locked = 0;
6693 			stcb->asoc.locked_on_sending = NULL;
6694 			if (send_lock_up) {
6695 				SCTP_TCB_SEND_UNLOCK(stcb);
6696 				send_lock_up = 0;
6697 			}
6698 			/* back to get the next msg */
6699 			goto one_more_time;
6700 		} else {
6701 			/*
6702 			 * sender just finished this but still holds a
6703 			 * reference
6704 			 */
6705 			*locked = 1;
6706 			*giveup = 1;
6707 			to_move = 0;
6708 			goto out_of;
6709 		}
6710 	} else {
6711 		/* is there some to get */
6712 		if (sp->length == 0) {
6713 			/* no */
6714 			*locked = 1;
6715 			*giveup = 1;
6716 			to_move = 0;
6717 			goto out_of;
6718 		}
6719 	}
6720 	some_taken = sp->some_taken;
6721 	if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6722 		sp->msg_is_complete = 1;
6723 	}
6724 re_look:
6725 	length = sp->length;
6726 	if (sp->msg_is_complete) {
6727 		/* The message is complete */
6728 		to_move = min(length, frag_point);
6729 		if (to_move == length) {
6730 			/* All of it fits in the MTU */
6731 			if (sp->some_taken) {
6732 				rcv_flags |= SCTP_DATA_LAST_FRAG;
6733 				sp->put_last_out = 1;
6734 			} else {
6735 				rcv_flags |= SCTP_DATA_NOT_FRAG;
6736 				sp->put_last_out = 1;
6737 			}
6738 		} else {
6739 			/* Not all of it fits, we fragment */
6740 			if (sp->some_taken == 0) {
6741 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
6742 			}
6743 			sp->some_taken = 1;
6744 		}
6745 	} else {
6746 		to_move = sctp_can_we_split_this(stcb, length, goal_mtu,
6747 		    frag_point, eeor_mode);
6748 		if (to_move) {
6749 			/*-
6750 		         * We use a snapshot of length in case it
6751 		         * is expanding during the compare.
6752 		         */
6753 			uint32_t llen;
6754 
6755 			llen = length;
6756 			if (to_move >= llen) {
6757 				to_move = llen;
6758 				if (send_lock_up == 0) {
6759 					/*-
6760 				         * We are taking all of an incomplete msg
6761 				         * thus we need a send lock.
6762 				         */
6763 					SCTP_TCB_SEND_LOCK(stcb);
6764 					send_lock_up = 1;
6765 					if (sp->msg_is_complete) {
6766 						/*
6767 						 * the sender finished the
6768 						 * msg
6769 						 */
6770 						goto re_look;
6771 					}
6772 				}
6773 			}
6774 			if (sp->some_taken == 0) {
6775 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
6776 				sp->some_taken = 1;
6777 			}
6778 		} else {
6779 			/* Nothing to take. */
6780 			if (sp->some_taken) {
6781 				*locked = 1;
6782 			}
6783 			*giveup = 1;
6784 			to_move = 0;
6785 			goto out_of;
6786 		}
6787 	}
6788 
6789 	/* If we reach here, we can copy out a chunk */
6790 	sctp_alloc_a_chunk(stcb, chk);
6791 	if (chk == NULL) {
6792 		/* No chunk memory */
6793 		*giveup = 1;
6794 		to_move = 0;
6795 		goto out_of;
6796 	}
6797 	/*
6798 	 * Setup for unordered if needed by looking at the user sent info
6799 	 * flags.
6800 	 */
6801 	if (sp->sinfo_flags & SCTP_UNORDERED) {
6802 		rcv_flags |= SCTP_DATA_UNORDERED;
6803 	}
6804 	if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) {
6805 		rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
6806 	}
6807 	/* clear out the chunk before setting up */
6808 	memset(chk, 0, sizeof(*chk));
6809 	chk->rec.data.rcv_flags = rcv_flags;
6810 
6811 	if (to_move >= length) {
6812 		/* we think we can steal the whole thing */
6813 		if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
6814 			SCTP_TCB_SEND_LOCK(stcb);
6815 			send_lock_up = 1;
6816 		}
6817 		if (to_move < sp->length) {
6818 			/* bail, it changed */
6819 			goto dont_do_it;
6820 		}
6821 		chk->data = sp->data;
6822 		chk->last_mbuf = sp->tail_mbuf;
6823 		/* register the stealing */
6824 		sp->data = sp->tail_mbuf = NULL;
6825 	} else {
6826 		struct mbuf *m;
6827 
6828 dont_do_it:
6829 		chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
6830 		chk->last_mbuf = NULL;
6831 		if (chk->data == NULL) {
6832 			sp->some_taken = some_taken;
6833 			sctp_free_a_chunk(stcb, chk);
6834 			*bail = 1;
6835 			to_move = 0;
6836 			goto out_of;
6837 		}
6838 #ifdef SCTP_MBUF_LOGGING
6839 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6840 			struct mbuf *mat;
6841 
6842 			mat = chk->data;
6843 			while (mat) {
6844 				if (SCTP_BUF_IS_EXTENDED(mat)) {
6845 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6846 				}
6847 				mat = SCTP_BUF_NEXT(mat);
6848 			}
6849 		}
6850 #endif
6851 		/* Pull off the data */
6852 		m_adj(sp->data, to_move);
6853 		/* Now lets work our way down and compact it */
6854 		m = sp->data;
6855 		while (m && (SCTP_BUF_LEN(m) == 0)) {
6856 			sp->data = SCTP_BUF_NEXT(m);
6857 			SCTP_BUF_NEXT(m) = NULL;
6858 			if (sp->tail_mbuf == m) {
6859 				/*-
6860 				 * Freeing tail? TSNH since
6861 				 * we supposedly were taking less
6862 				 * than the sp->length.
6863 				 */
6864 #ifdef INVARIANTS
6865 				panic("Huh, freing tail? - TSNH");
6866 #else
6867 				SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
6868 				sp->tail_mbuf = sp->data = NULL;
6869 				sp->length = 0;
6870 #endif
6871 
6872 			}
6873 			sctp_m_free(m);
6874 			m = sp->data;
6875 		}
6876 	}
6877 	if (SCTP_BUF_IS_EXTENDED(chk->data)) {
6878 		chk->copy_by_ref = 1;
6879 	} else {
6880 		chk->copy_by_ref = 0;
6881 	}
6882 	/*
6883 	 * get last_mbuf and counts of mb useage This is ugly but hopefully
6884 	 * its only one mbuf.
6885 	 */
6886 	if (chk->last_mbuf == NULL) {
6887 		chk->last_mbuf = chk->data;
6888 		while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
6889 			chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
6890 		}
6891 	}
6892 	if (to_move > length) {
6893 		/*- This should not happen either
6894 		 * since we always lower to_move to the size
6895 		 * of sp->length if its larger.
6896 		 */
6897 #ifdef INVARIANTS
6898 		panic("Huh, how can to_move be larger?");
6899 #else
6900 		SCTP_PRINTF("Huh, how can to_move be larger?\n");
6901 		sp->length = 0;
6902 #endif
6903 	} else {
6904 		atomic_subtract_int(&sp->length, to_move);
6905 	}
6906 	if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
6907 		/* Not enough room for a chunk header, get some */
6908 		struct mbuf *m;
6909 
6910 		m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
6911 		if (m == NULL) {
6912 			/*
6913 			 * we're in trouble here. _PREPEND below will free
6914 			 * all the data if there is no leading space, so we
6915 			 * must put the data back and restore.
6916 			 */
6917 			if (send_lock_up == 0) {
6918 				SCTP_TCB_SEND_LOCK(stcb);
6919 				send_lock_up = 1;
6920 			}
6921 			if (chk->data == NULL) {
6922 				/* unsteal the data */
6923 				sp->data = chk->data;
6924 				sp->tail_mbuf = chk->last_mbuf;
6925 			} else {
6926 				struct mbuf *m_tmp;
6927 
6928 				/* reassemble the data */
6929 				m_tmp = sp->data;
6930 				sp->data = chk->data;
6931 				SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
6932 			}
6933 			sp->some_taken = some_taken;
6934 			atomic_add_int(&sp->length, to_move);
6935 			chk->data = NULL;
6936 			*bail = 1;
6937 			sctp_free_a_chunk(stcb, chk);
6938 			to_move = 0;
6939 			goto out_of;
6940 		} else {
6941 			SCTP_BUF_LEN(m) = 0;
6942 			SCTP_BUF_NEXT(m) = chk->data;
6943 			chk->data = m;
6944 			M_ALIGN(chk->data, 4);
6945 		}
6946 	}
6947 	SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
6948 	if (chk->data == NULL) {
6949 		/* HELP, TSNH since we assured it would not above? */
6950 #ifdef INVARIANTS
6951 		panic("prepend failes HELP?");
6952 #else
6953 		SCTP_PRINTF("prepend fails HELP?\n");
6954 		sctp_free_a_chunk(stcb, chk);
6955 #endif
6956 		*bail = 1;
6957 		to_move = 0;
6958 		goto out_of;
6959 	}
6960 	sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
6961 	chk->book_size = chk->send_size = (to_move +
6962 	    sizeof(struct sctp_data_chunk));
6963 	chk->book_size_scale = 0;
6964 	chk->sent = SCTP_DATAGRAM_UNSENT;
6965 
6966 	chk->flags = 0;
6967 	chk->asoc = &stcb->asoc;
6968 	chk->pad_inplace = 0;
6969 	chk->no_fr_allowed = 0;
6970 	chk->rec.data.stream_seq = sp->strseq;
6971 	chk->rec.data.stream_number = sp->stream;
6972 	chk->rec.data.payloadtype = sp->ppid;
6973 	chk->rec.data.context = sp->context;
6974 	chk->rec.data.doing_fast_retransmit = 0;
6975 	chk->rec.data.ect_nonce = 0;	/* ECN Nonce */
6976 
6977 	chk->rec.data.timetodrop = sp->ts;
6978 	chk->flags = sp->act_flags;
6979 	chk->addr_over = sp->addr_over;
6980 
6981 	chk->whoTo = net;
6982 	atomic_add_int(&chk->whoTo->ref_count, 1);
6983 
6984 	chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
6985 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
6986 		sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
6987 		    (uintptr_t) stcb, sp->length,
6988 		    (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
6989 		    chk->rec.data.TSN_seq);
6990 	}
6991 	dchkh = mtod(chk->data, struct sctp_data_chunk *);
6992 	/*
6993 	 * Put the rest of the things in place now. Size was done earlier in
6994 	 * previous loop prior to padding.
6995 	 */
6996 
6997 #ifdef SCTP_ASOCLOG_OF_TSNS
6998 	SCTP_TCB_LOCK_ASSERT(stcb);
6999 	if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7000 		asoc->tsn_out_at = 0;
7001 		asoc->tsn_out_wrapped = 1;
7002 	}
7003 	asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7004 	asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7005 	asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7006 	asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7007 	asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7008 	asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7009 	asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7010 	asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7011 	asoc->tsn_out_at++;
7012 #endif
7013 
7014 	dchkh->ch.chunk_type = SCTP_DATA;
7015 	dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7016 	dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7017 	dchkh->dp.stream_id = htons(strq->stream_no);
7018 	dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7019 	dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7020 	dchkh->ch.chunk_length = htons(chk->send_size);
7021 	/* Now advance the chk->send_size by the actual pad needed. */
7022 	if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7023 		/* need a pad */
7024 		struct mbuf *lm;
7025 		int pads;
7026 
7027 		pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7028 		if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7029 			chk->pad_inplace = 1;
7030 		}
7031 		if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7032 			/* pad added an mbuf */
7033 			chk->last_mbuf = lm;
7034 		}
7035 		chk->send_size += pads;
7036 	}
7037 	/* We only re-set the policy if it is on */
7038 	if (sp->pr_sctp_on) {
7039 		sctp_set_prsctp_policy(stcb, sp);
7040 		asoc->pr_sctp_cnt++;
7041 		chk->pr_sctp_on = 1;
7042 	} else {
7043 		chk->pr_sctp_on = 0;
7044 	}
7045 	if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7046 		/* All done pull and kill the message */
7047 		atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7048 		if (sp->put_last_out == 0) {
7049 			SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7050 			SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7051 			    sp->sender_all_done,
7052 			    sp->length,
7053 			    sp->msg_is_complete,
7054 			    sp->put_last_out,
7055 			    send_lock_up);
7056 		}
7057 		if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7058 			SCTP_TCB_SEND_LOCK(stcb);
7059 			send_lock_up = 1;
7060 		}
7061 		TAILQ_REMOVE(&strq->outqueue, sp, next);
7062 		sctp_free_remote_addr(sp->net);
7063 		if (sp->data) {
7064 			sctp_m_freem(sp->data);
7065 			sp->data = NULL;
7066 		}
7067 		sctp_free_a_strmoq(stcb, sp);
7068 
7069 		/* we can't be locked to it */
7070 		*locked = 0;
7071 		stcb->asoc.locked_on_sending = NULL;
7072 	} else {
7073 		/* more to go, we are locked */
7074 		*locked = 1;
7075 	}
7076 	asoc->chunks_on_out_queue++;
7077 	TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7078 	asoc->send_queue_cnt++;
7079 out_of:
7080 	if (send_lock_up) {
7081 		SCTP_TCB_SEND_UNLOCK(stcb);
7082 		send_lock_up = 0;
7083 	}
7084 	return (to_move);
7085 }
7086 
7087 
7088 static struct sctp_stream_out *
7089 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
7090 {
7091 	struct sctp_stream_out *strq;
7092 
7093 	/* Find the next stream to use */
7094 	if (asoc->last_out_stream == NULL) {
7095 		strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
7096 		if (asoc->last_out_stream == NULL) {
7097 			/* huh nothing on the wheel, TSNH */
7098 			return (NULL);
7099 		}
7100 		goto done_it;
7101 	}
7102 	strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7103 done_it:
7104 	if (strq == NULL) {
7105 		strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
7106 	}
7107 	/* Save off the last stream */
7108 	asoc->last_out_stream = strq;
7109 	return (strq);
7110 
7111 }
7112 
7113 
7114 static void
7115 sctp_fill_outqueue(struct sctp_tcb *stcb,
7116     struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
7117 {
7118 	struct sctp_association *asoc;
7119 	struct sctp_stream_out *strq, *strqn, *strqt;
7120 	int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7121 	int locked, giveup;
7122 	struct sctp_stream_queue_pending *sp;
7123 
7124 	SCTP_TCB_LOCK_ASSERT(stcb);
7125 	asoc = &stcb->asoc;
7126 #ifdef INET6
7127 	if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
7128 		goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7129 	} else {
7130 		/* ?? not sure what else to do */
7131 		goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7132 	}
7133 #else
7134 	goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7135 #endif
7136 	/* Need an allowance for the data chunk header too */
7137 	goal_mtu -= sizeof(struct sctp_data_chunk);
7138 
7139 	/* must make even word boundary */
7140 	goal_mtu &= 0xfffffffc;
7141 	if (asoc->locked_on_sending) {
7142 		/* We are stuck on one stream until the message completes. */
7143 		strqn = strq = asoc->locked_on_sending;
7144 		locked = 1;
7145 	} else {
7146 		strqn = strq = sctp_select_a_stream(stcb, asoc);
7147 		locked = 0;
7148 	}
7149 
7150 	while ((goal_mtu > 0) && strq) {
7151 		sp = TAILQ_FIRST(&strq->outqueue);
7152 		/*
7153 		 * If CMT is off, we must validate that the stream in
7154 		 * question has the first item pointed towards are network
7155 		 * destionation requested by the caller. Note that if we
7156 		 * turn out to be locked to a stream (assigning TSN's then
7157 		 * we must stop, since we cannot look for another stream
7158 		 * with data to send to that destination). In CMT's case, by
7159 		 * skipping this check, we will send one data packet towards
7160 		 * the requested net.
7161 		 */
7162 		if (sp == NULL) {
7163 			break;
7164 		}
7165 		if ((sp->net != net) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
7166 			/* none for this network */
7167 			if (locked) {
7168 				break;
7169 			} else {
7170 				strq = sctp_select_a_stream(stcb, asoc);
7171 				if (strq == NULL)
7172 					/* none left */
7173 					break;
7174 				if (strqn == strq) {
7175 					/* I have circled */
7176 					break;
7177 				}
7178 				continue;
7179 			}
7180 		}
7181 		giveup = 0;
7182 		bail = 0;
7183 		moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
7184 		    &giveup, eeor_mode, &bail);
7185 		asoc->last_out_stream = strq;
7186 		if (locked) {
7187 			asoc->locked_on_sending = strq;
7188 			if ((moved_how_much == 0) || (giveup) || bail)
7189 				/* no more to move for now */
7190 				break;
7191 		} else {
7192 			asoc->locked_on_sending = NULL;
7193 			strqt = sctp_select_a_stream(stcb, asoc);
7194 			if (TAILQ_FIRST(&strq->outqueue) == NULL) {
7195 				if (strq == strqn) {
7196 					/* Must move start to next one */
7197 					strqn = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7198 					if (strqn == NULL) {
7199 						strqn = TAILQ_FIRST(&asoc->out_wheel);
7200 						if (strqn == NULL) {
7201 							break;
7202 						}
7203 					}
7204 				}
7205 				sctp_remove_from_wheel(stcb, asoc, strq);
7206 			}
7207 			if ((giveup) || bail) {
7208 				break;
7209 			}
7210 			strq = strqt;
7211 			if (strq == NULL) {
7212 				break;
7213 			}
7214 		}
7215 		total_moved += moved_how_much;
7216 		goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7217 		goal_mtu &= 0xfffffffc;
7218 	}
7219 	if (bail)
7220 		*quit_now = 1;
7221 
7222 	if (total_moved == 0) {
7223 		if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) &&
7224 		    (net == stcb->asoc.primary_destination)) {
7225 			/* ran dry for primary network net */
7226 			SCTP_STAT_INCR(sctps_primary_randry);
7227 		} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7228 			/* ran dry with CMT on */
7229 			SCTP_STAT_INCR(sctps_cmt_randry);
7230 		}
7231 	}
7232 }
7233 
7234 void
7235 sctp_fix_ecn_echo(struct sctp_association *asoc)
7236 {
7237 	struct sctp_tmit_chunk *chk;
7238 
7239 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7240 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7241 			chk->sent = SCTP_DATAGRAM_UNSENT;
7242 		}
7243 	}
7244 }
7245 
7246 static void
7247 sctp_move_to_an_alt(struct sctp_tcb *stcb,
7248     struct sctp_association *asoc,
7249     struct sctp_nets *net)
7250 {
7251 	struct sctp_tmit_chunk *chk;
7252 	struct sctp_nets *a_net;
7253 
7254 	SCTP_TCB_LOCK_ASSERT(stcb);
7255 	/*
7256 	 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
7257 	 * destination using the PF algorithm for finding alternate
7258 	 * destinations.
7259 	 */
7260 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
7261 		a_net = sctp_find_alternate_net(stcb, net, 2);
7262 	} else {
7263 		a_net = sctp_find_alternate_net(stcb, net, 0);
7264 	}
7265 	if ((a_net != net) &&
7266 	    ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
7267 		/*
7268 		 * We only proceed if a valid alternate is found that is not
7269 		 * this one and is reachable. Here we must move all chunks
7270 		 * queued in the send queue off of the destination address
7271 		 * to our alternate.
7272 		 */
7273 		TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7274 			if (chk->whoTo == net) {
7275 				/* Move the chunk to our alternate */
7276 				sctp_free_remote_addr(chk->whoTo);
7277 				chk->whoTo = a_net;
7278 				atomic_add_int(&a_net->ref_count, 1);
7279 			}
7280 		}
7281 	}
7282 }
7283 
7284 int
7285 sctp_med_chunk_output(struct sctp_inpcb *inp,
7286     struct sctp_tcb *stcb,
7287     struct sctp_association *asoc,
7288     int *num_out,
7289     int *reason_code,
7290     int control_only, int *cwnd_full, int from_where,
7291     struct timeval *now, int *now_filled, int frag_point, int so_locked
7292 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7293     SCTP_UNUSED
7294 #endif
7295 )
7296 {
7297 	/*
7298 	 * Ok this is the generic chunk service queue. we must do the
7299 	 * following: - Service the stream queue that is next, moving any
7300 	 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7301 	 * LAST to the out queue in one pass) and assigning TSN's - Check to
7302 	 * see if the cwnd/rwnd allows any output, if so we go ahead and
7303 	 * fomulate and send the low level chunks. Making sure to combine
7304 	 * any control in the control chunk queue also.
7305 	 */
7306 	struct sctp_nets *net;
7307 	struct mbuf *outchain, *endoutchain;
7308 	struct sctp_tmit_chunk *chk, *nchk;
7309 	struct sctphdr *shdr;
7310 
7311 	/* temp arrays for unlinking */
7312 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7313 	int no_fragmentflg, error;
7314 	int one_chunk, hbflag, skip_data_for_this_net;
7315 	int asconf, cookie, no_out_cnt;
7316 	int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind, eeor_mode;
7317 	unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7318 	struct sctp_nets *start_at, *old_startat = NULL, *send_start_at;
7319 	int tsns_sent = 0;
7320 	uint32_t auth_offset = 0;
7321 	struct sctp_auth_chunk *auth = NULL;
7322 
7323 	/*
7324 	 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7325 	 * destination.
7326 	 */
7327 	int pf_hbflag = 0;
7328 	int quit_now = 0;
7329 
7330 	*num_out = 0;
7331 	cwnd_full_ind = 0;
7332 
7333 	if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7334 	    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7335 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7336 		eeor_mode = 1;
7337 	} else {
7338 		eeor_mode = 0;
7339 	}
7340 	ctl_cnt = no_out_cnt = asconf = cookie = 0;
7341 	/*
7342 	 * First lets prime the pump. For each destination, if there is room
7343 	 * in the flight size, attempt to pull an MTU's worth out of the
7344 	 * stream queues into the general send_queue
7345 	 */
7346 #ifdef SCTP_AUDITING_ENABLED
7347 	sctp_audit_log(0xC2, 2);
7348 #endif
7349 	SCTP_TCB_LOCK_ASSERT(stcb);
7350 	hbflag = 0;
7351 	if ((control_only) || (asoc->stream_reset_outstanding))
7352 		no_data_chunks = 1;
7353 	else
7354 		no_data_chunks = 0;
7355 
7356 	/* Nothing to possible to send? */
7357 	if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7358 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7359 	    TAILQ_EMPTY(&asoc->send_queue) &&
7360 	    TAILQ_EMPTY(&asoc->out_wheel)) {
7361 		*reason_code = 9;
7362 		return (0);
7363 	}
7364 	if (asoc->peers_rwnd == 0) {
7365 		/* No room in peers rwnd */
7366 		*cwnd_full = 1;
7367 		*reason_code = 1;
7368 		if (asoc->total_flight > 0) {
7369 			/* we are allowed one chunk in flight */
7370 			no_data_chunks = 1;
7371 		}
7372 	}
7373 	if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
7374 		if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7375 			/*
7376 			 * for CMT we start at the next one past the one we
7377 			 * last added data to.
7378 			 */
7379 			if (TAILQ_FIRST(&asoc->send_queue) != NULL) {
7380 				goto skip_the_fill_from_streams;
7381 			}
7382 			if (asoc->last_net_data_came_from) {
7383 				net = TAILQ_NEXT(asoc->last_net_data_came_from, sctp_next);
7384 				if (net == NULL) {
7385 					net = TAILQ_FIRST(&asoc->nets);
7386 				}
7387 			} else {
7388 				/* back to start */
7389 				net = TAILQ_FIRST(&asoc->nets);
7390 			}
7391 
7392 			/*
7393 			 * JRI-TODO: CMT-MPI. Simply set the first
7394 			 * destination (net) to be optimized for the next
7395 			 * message to be pulled out of the outwheel. 1. peek
7396 			 * at outwheel 2. If large message, set net =
7397 			 * highest_cwnd 3. If small message, set net =
7398 			 * lowest rtt
7399 			 */
7400 		} else {
7401 			net = asoc->primary_destination;
7402 			if (net == NULL) {
7403 				/* TSNH */
7404 				net = TAILQ_FIRST(&asoc->nets);
7405 			}
7406 		}
7407 		start_at = net;
7408 
7409 one_more_time:
7410 		for (; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7411 			net->window_probe = 0;
7412 			if (old_startat && (old_startat == net)) {
7413 				break;
7414 			}
7415 			/*
7416 			 * JRI: if dest is unreachable or unconfirmed, do
7417 			 * not send data to it
7418 			 */
7419 			if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
7420 				continue;
7421 			}
7422 			/*
7423 			 * JRI: if dest is in PF state, do not send data to
7424 			 * it
7425 			 */
7426 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
7427 			    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
7428 			    (net->dest_state & SCTP_ADDR_PF)) {
7429 				continue;
7430 			}
7431 			if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
7432 				/* nothing can be in queue for this guy */
7433 				continue;
7434 			}
7435 			if (net->flight_size >= net->cwnd) {
7436 				/* skip this network, no room */
7437 				cwnd_full_ind++;
7438 				continue;
7439 			}
7440 			/*
7441 			 * JRI : this for loop we are in takes in each net,
7442 			 * if its's got space in cwnd and has data sent to
7443 			 * it (when CMT is off) then it calls
7444 			 * sctp_fill_outqueue for the net. This gets data on
7445 			 * the send queue for that network.
7446 			 *
7447 			 * In sctp_fill_outqueue TSN's are assigned and data is
7448 			 * copied out of the stream buffers. Note mostly
7449 			 * copy by reference (we hope).
7450 			 */
7451 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7452 				sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7453 			}
7454 			sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
7455 			if (quit_now) {
7456 				/* memory alloc failure */
7457 				no_data_chunks = 1;
7458 				goto skip_the_fill_from_streams;
7459 			}
7460 		}
7461 		if (start_at != TAILQ_FIRST(&asoc->nets)) {
7462 			/* got to pick up the beginning stuff. */
7463 			old_startat = start_at;
7464 			start_at = net = TAILQ_FIRST(&asoc->nets);
7465 			if (old_startat)
7466 				goto one_more_time;
7467 		}
7468 	}
7469 skip_the_fill_from_streams:
7470 	*cwnd_full = cwnd_full_ind;
7471 
7472 	/* now service each destination and send out what we can for it */
7473 	/* Nothing to send? */
7474 	if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7475 	    (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7476 	    (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
7477 		*reason_code = 8;
7478 		return (0);
7479 	}
7480 	if (no_data_chunks) {
7481 		chk = TAILQ_FIRST(&asoc->asconf_send_queue);
7482 		if (chk == NULL)
7483 			chk = TAILQ_FIRST(&asoc->control_send_queue);
7484 	} else {
7485 		chk = TAILQ_FIRST(&asoc->send_queue);
7486 	}
7487 	if (chk) {
7488 		send_start_at = chk->whoTo;
7489 	} else {
7490 		send_start_at = TAILQ_FIRST(&asoc->nets);
7491 	}
7492 	old_startat = NULL;
7493 again_one_more_time:
7494 	for (net = send_start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7495 		/* how much can we send? */
7496 		/* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7497 		if (old_startat && (old_startat == net)) {
7498 			/* through list ocmpletely. */
7499 			break;
7500 		}
7501 		tsns_sent = 0;
7502 		if (net->ref_count < 2) {
7503 			/*
7504 			 * Ref-count of 1 so we cannot have data or control
7505 			 * queued to this address. Skip it.
7506 			 */
7507 			continue;
7508 		}
7509 		ctl_cnt = bundle_at = 0;
7510 		endoutchain = outchain = NULL;
7511 		no_fragmentflg = 1;
7512 		one_chunk = 0;
7513 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7514 			skip_data_for_this_net = 1;
7515 		} else {
7516 			skip_data_for_this_net = 0;
7517 		}
7518 		if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7519 			/*
7520 			 * if we have a route and an ifp check to see if we
7521 			 * have room to send to this guy
7522 			 */
7523 			struct ifnet *ifp;
7524 
7525 			ifp = net->ro.ro_rt->rt_ifp;
7526 			if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7527 				SCTP_STAT_INCR(sctps_ifnomemqueued);
7528 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7529 					sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7530 				}
7531 				continue;
7532 			}
7533 		}
7534 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7535 		case AF_INET:
7536 			mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7537 			break;
7538 #ifdef INET6
7539 		case AF_INET6:
7540 			mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7541 			break;
7542 #endif
7543 		default:
7544 			/* TSNH */
7545 			mtu = net->mtu;
7546 			break;
7547 		}
7548 		mx_mtu = mtu;
7549 		to_out = 0;
7550 		if (mtu > asoc->peers_rwnd) {
7551 			if (asoc->total_flight > 0) {
7552 				/* We have a packet in flight somewhere */
7553 				r_mtu = asoc->peers_rwnd;
7554 			} else {
7555 				/* We are always allowed to send one MTU out */
7556 				one_chunk = 1;
7557 				r_mtu = mtu;
7558 			}
7559 		} else {
7560 			r_mtu = mtu;
7561 		}
7562 		/************************/
7563 		/* ASCONF transmission */
7564 		/************************/
7565 		/* Now first lets go through the asconf queue */
7566 		for (chk = TAILQ_FIRST(&asoc->asconf_send_queue);
7567 		    chk; chk = nchk) {
7568 			nchk = TAILQ_NEXT(chk, sctp_next);
7569 			if (chk->rec.chunk_id.id != SCTP_ASCONF) {
7570 				continue;
7571 			}
7572 			if (chk->whoTo != net) {
7573 				/*
7574 				 * No, not sent to the network we are
7575 				 * looking at
7576 				 */
7577 				break;
7578 			}
7579 			if (chk->data == NULL) {
7580 				break;
7581 			}
7582 			if (chk->sent != SCTP_DATAGRAM_UNSENT &&
7583 			    chk->sent != SCTP_DATAGRAM_RESEND) {
7584 				break;
7585 			}
7586 			/*
7587 			 * if no AUTH is yet included and this chunk
7588 			 * requires it, make sure to account for it.  We
7589 			 * don't apply the size until the AUTH chunk is
7590 			 * actually added below in case there is no room for
7591 			 * this chunk. NOTE: we overload the use of "omtu"
7592 			 * here
7593 			 */
7594 			if ((auth == NULL) &&
7595 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7596 			    stcb->asoc.peer_auth_chunks)) {
7597 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7598 			} else
7599 				omtu = 0;
7600 			/* Here we do NOT factor the r_mtu */
7601 			if ((chk->send_size < (int)(mtu - omtu)) ||
7602 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7603 				/*
7604 				 * We probably should glom the mbuf chain
7605 				 * from the chk->data for control but the
7606 				 * problem is it becomes yet one more level
7607 				 * of tracking to do if for some reason
7608 				 * output fails. Then I have got to
7609 				 * reconstruct the merged control chain.. el
7610 				 * yucko.. for now we take the easy way and
7611 				 * do the copy
7612 				 */
7613 				/*
7614 				 * Add an AUTH chunk, if chunk requires it
7615 				 * save the offset into the chain for AUTH
7616 				 */
7617 				if ((auth == NULL) &&
7618 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7619 				    stcb->asoc.peer_auth_chunks))) {
7620 					outchain = sctp_add_auth_chunk(outchain,
7621 					    &endoutchain,
7622 					    &auth,
7623 					    &auth_offset,
7624 					    stcb,
7625 					    chk->rec.chunk_id.id);
7626 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7627 				}
7628 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7629 				    (int)chk->rec.chunk_id.can_take_data,
7630 				    chk->send_size, chk->copy_by_ref);
7631 				if (outchain == NULL) {
7632 					*reason_code = 8;
7633 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7634 					return (ENOMEM);
7635 				}
7636 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7637 				/* update our MTU size */
7638 				if (mtu > (chk->send_size + omtu))
7639 					mtu -= (chk->send_size + omtu);
7640 				else
7641 					mtu = 0;
7642 				to_out += (chk->send_size + omtu);
7643 				/* Do clear IP_DF ? */
7644 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7645 					no_fragmentflg = 0;
7646 				}
7647 				if (chk->rec.chunk_id.can_take_data)
7648 					chk->data = NULL;
7649 				/*
7650 				 * set hb flag since we can use these for
7651 				 * RTO
7652 				 */
7653 				hbflag = 1;
7654 				asconf = 1;
7655 				/*
7656 				 * should sysctl this: don't bundle data
7657 				 * with ASCONF since it requires AUTH
7658 				 */
7659 				no_data_chunks = 1;
7660 				chk->sent = SCTP_DATAGRAM_SENT;
7661 				chk->snd_count++;
7662 				if (mtu == 0) {
7663 					/*
7664 					 * Ok we are out of room but we can
7665 					 * output without effecting the
7666 					 * flight size since this little guy
7667 					 * is a control only packet.
7668 					 */
7669 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7670 					/*
7671 					 * do NOT clear the asconf flag as
7672 					 * it is used to do appropriate
7673 					 * source address selection.
7674 					 */
7675 					SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
7676 					if (outchain == NULL) {
7677 						/* no memory */
7678 						SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
7679 						error = ENOBUFS;
7680 						*reason_code = 7;
7681 						continue;
7682 					}
7683 					shdr = mtod(outchain, struct sctphdr *);
7684 					shdr->src_port = inp->sctp_lport;
7685 					shdr->dest_port = stcb->rport;
7686 					shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7687 					shdr->checksum = 0;
7688 					auth_offset += sizeof(struct sctphdr);
7689 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7690 					    (struct sockaddr *)&net->ro._l_addr,
7691 					    outchain, auth_offset, auth,
7692 					    no_fragmentflg, 0, NULL, asconf, net->port, so_locked, NULL))) {
7693 						if (error == ENOBUFS) {
7694 							asoc->ifp_had_enobuf = 1;
7695 							SCTP_STAT_INCR(sctps_lowlevelerr);
7696 						}
7697 						if (from_where == 0) {
7698 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
7699 						}
7700 						if (*now_filled == 0) {
7701 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7702 							*now_filled = 1;
7703 							*now = net->last_sent_time;
7704 						} else {
7705 							net->last_sent_time = *now;
7706 						}
7707 						hbflag = 0;
7708 						/* error, could not output */
7709 						if (error == EHOSTUNREACH) {
7710 							/*
7711 							 * Destination went
7712 							 * unreachable
7713 							 * during this send
7714 							 */
7715 							sctp_move_to_an_alt(stcb, asoc, net);
7716 						}
7717 						*reason_code = 7;
7718 						continue;
7719 					} else
7720 						asoc->ifp_had_enobuf = 0;
7721 					if (*now_filled == 0) {
7722 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7723 						*now_filled = 1;
7724 						*now = net->last_sent_time;
7725 					} else {
7726 						net->last_sent_time = *now;
7727 					}
7728 					hbflag = 0;
7729 					/*
7730 					 * increase the number we sent, if a
7731 					 * cookie is sent we don't tell them
7732 					 * any was sent out.
7733 					 */
7734 					outchain = endoutchain = NULL;
7735 					auth = NULL;
7736 					auth_offset = 0;
7737 					if (!no_out_cnt)
7738 						*num_out += ctl_cnt;
7739 					/* recalc a clean slate and setup */
7740 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7741 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7742 					} else {
7743 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7744 					}
7745 					to_out = 0;
7746 					no_fragmentflg = 1;
7747 				}
7748 			}
7749 		}
7750 		/************************/
7751 		/* Control transmission */
7752 		/************************/
7753 		/* Now first lets go through the control queue */
7754 		for (chk = TAILQ_FIRST(&asoc->control_send_queue);
7755 		    chk; chk = nchk) {
7756 			nchk = TAILQ_NEXT(chk, sctp_next);
7757 			if (chk->whoTo != net) {
7758 				/*
7759 				 * No, not sent to the network we are
7760 				 * looking at
7761 				 */
7762 				continue;
7763 			}
7764 			if (chk->data == NULL) {
7765 				continue;
7766 			}
7767 			if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7768 				/*
7769 				 * It must be unsent. Cookies and ASCONF's
7770 				 * hang around but there timers will force
7771 				 * when marked for resend.
7772 				 */
7773 				continue;
7774 			}
7775 			/*
7776 			 * if no AUTH is yet included and this chunk
7777 			 * requires it, make sure to account for it.  We
7778 			 * don't apply the size until the AUTH chunk is
7779 			 * actually added below in case there is no room for
7780 			 * this chunk. NOTE: we overload the use of "omtu"
7781 			 * here
7782 			 */
7783 			if ((auth == NULL) &&
7784 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7785 			    stcb->asoc.peer_auth_chunks)) {
7786 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7787 			} else
7788 				omtu = 0;
7789 			/* Here we do NOT factor the r_mtu */
7790 			if ((chk->send_size < (int)(mtu - omtu)) ||
7791 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7792 				/*
7793 				 * We probably should glom the mbuf chain
7794 				 * from the chk->data for control but the
7795 				 * problem is it becomes yet one more level
7796 				 * of tracking to do if for some reason
7797 				 * output fails. Then I have got to
7798 				 * reconstruct the merged control chain.. el
7799 				 * yucko.. for now we take the easy way and
7800 				 * do the copy
7801 				 */
7802 				/*
7803 				 * Add an AUTH chunk, if chunk requires it
7804 				 * save the offset into the chain for AUTH
7805 				 */
7806 				if ((auth == NULL) &&
7807 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7808 				    stcb->asoc.peer_auth_chunks))) {
7809 					outchain = sctp_add_auth_chunk(outchain,
7810 					    &endoutchain,
7811 					    &auth,
7812 					    &auth_offset,
7813 					    stcb,
7814 					    chk->rec.chunk_id.id);
7815 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7816 				}
7817 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7818 				    (int)chk->rec.chunk_id.can_take_data,
7819 				    chk->send_size, chk->copy_by_ref);
7820 				if (outchain == NULL) {
7821 					*reason_code = 8;
7822 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7823 					return (ENOMEM);
7824 				}
7825 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7826 				/* update our MTU size */
7827 				if (mtu > (chk->send_size + omtu))
7828 					mtu -= (chk->send_size + omtu);
7829 				else
7830 					mtu = 0;
7831 				to_out += (chk->send_size + omtu);
7832 				/* Do clear IP_DF ? */
7833 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7834 					no_fragmentflg = 0;
7835 				}
7836 				if (chk->rec.chunk_id.can_take_data)
7837 					chk->data = NULL;
7838 				/* Mark things to be removed, if needed */
7839 				if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7840 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7841 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7842 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7843 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7844 				    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7845 				    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7846 				    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7847 				    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7848 				    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7849 
7850 					if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7851 						hbflag = 1;
7852 						/*
7853 						 * JRS 5/14/07 - Set the
7854 						 * flag to say a heartbeat
7855 						 * is being sent.
7856 						 */
7857 						pf_hbflag = 1;
7858 					}
7859 					/* remove these chunks at the end */
7860 					if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
7861 						/* turn off the timer */
7862 						if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7863 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7864 							    inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7865 						}
7866 					}
7867 					ctl_cnt++;
7868 				} else {
7869 					/*
7870 					 * Other chunks, since they have
7871 					 * timers running (i.e. COOKIE) we
7872 					 * just "trust" that it gets sent or
7873 					 * retransmitted.
7874 					 */
7875 					ctl_cnt++;
7876 					if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7877 						cookie = 1;
7878 						no_out_cnt = 1;
7879 					}
7880 					chk->sent = SCTP_DATAGRAM_SENT;
7881 					chk->snd_count++;
7882 				}
7883 				if (mtu == 0) {
7884 					/*
7885 					 * Ok we are out of room but we can
7886 					 * output without effecting the
7887 					 * flight size since this little guy
7888 					 * is a control only packet.
7889 					 */
7890 					if (asconf) {
7891 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7892 						/*
7893 						 * do NOT clear the asconf
7894 						 * flag as it is used to do
7895 						 * appropriate source
7896 						 * address selection.
7897 						 */
7898 					}
7899 					if (cookie) {
7900 						sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7901 						cookie = 0;
7902 					}
7903 					SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
7904 					if (outchain == NULL) {
7905 						/* no memory */
7906 						SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
7907 						error = ENOBUFS;
7908 						goto error_out_again;
7909 					}
7910 					shdr = mtod(outchain, struct sctphdr *);
7911 					shdr->src_port = inp->sctp_lport;
7912 					shdr->dest_port = stcb->rport;
7913 					shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7914 					shdr->checksum = 0;
7915 					auth_offset += sizeof(struct sctphdr);
7916 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7917 					    (struct sockaddr *)&net->ro._l_addr,
7918 					    outchain, auth_offset, auth,
7919 					    no_fragmentflg, 0, NULL, asconf, net->port, so_locked, NULL))) {
7920 						if (error == ENOBUFS) {
7921 							asoc->ifp_had_enobuf = 1;
7922 							SCTP_STAT_INCR(sctps_lowlevelerr);
7923 						}
7924 						if (from_where == 0) {
7925 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
7926 						}
7927 				error_out_again:
7928 						/* error, could not output */
7929 						if (hbflag) {
7930 							if (*now_filled == 0) {
7931 								(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7932 								*now_filled = 1;
7933 								*now = net->last_sent_time;
7934 							} else {
7935 								net->last_sent_time = *now;
7936 							}
7937 							hbflag = 0;
7938 						}
7939 						if (error == EHOSTUNREACH) {
7940 							/*
7941 							 * Destination went
7942 							 * unreachable
7943 							 * during this send
7944 							 */
7945 							sctp_move_to_an_alt(stcb, asoc, net);
7946 						}
7947 						*reason_code = 7;
7948 						continue;
7949 					} else
7950 						asoc->ifp_had_enobuf = 0;
7951 					/* Only HB or ASCONF advances time */
7952 					if (hbflag) {
7953 						if (*now_filled == 0) {
7954 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7955 							*now_filled = 1;
7956 							*now = net->last_sent_time;
7957 						} else {
7958 							net->last_sent_time = *now;
7959 						}
7960 						hbflag = 0;
7961 					}
7962 					/*
7963 					 * increase the number we sent, if a
7964 					 * cookie is sent we don't tell them
7965 					 * any was sent out.
7966 					 */
7967 					outchain = endoutchain = NULL;
7968 					auth = NULL;
7969 					auth_offset = 0;
7970 					if (!no_out_cnt)
7971 						*num_out += ctl_cnt;
7972 					/* recalc a clean slate and setup */
7973 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7974 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7975 					} else {
7976 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7977 					}
7978 					to_out = 0;
7979 					no_fragmentflg = 1;
7980 				}
7981 			}
7982 		}
7983 		/*********************/
7984 		/* Data transmission */
7985 		/*********************/
7986 		/*
7987 		 * if AUTH for DATA is required and no AUTH has been added
7988 		 * yet, account for this in the mtu now... if no data can be
7989 		 * bundled, this adjustment won't matter anyways since the
7990 		 * packet will be going out...
7991 		 */
7992 		if ((auth == NULL) &&
7993 		    sctp_auth_is_required_chunk(SCTP_DATA,
7994 		    stcb->asoc.peer_auth_chunks)) {
7995 			mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7996 		}
7997 		/* now lets add any data within the MTU constraints */
7998 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7999 		case AF_INET:
8000 			if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8001 				omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8002 			else
8003 				omtu = 0;
8004 			break;
8005 #ifdef INET6
8006 		case AF_INET6:
8007 			if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8008 				omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8009 			else
8010 				omtu = 0;
8011 			break;
8012 #endif
8013 		default:
8014 			/* TSNH */
8015 			omtu = 0;
8016 			break;
8017 		}
8018 		if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) && (skip_data_for_this_net == 0)) ||
8019 		    (cookie)) {
8020 			for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
8021 				if (no_data_chunks) {
8022 					/* let only control go out */
8023 					*reason_code = 1;
8024 					break;
8025 				}
8026 				if (net->flight_size >= net->cwnd) {
8027 					/* skip this net, no room for data */
8028 					*reason_code = 2;
8029 					break;
8030 				}
8031 				nchk = TAILQ_NEXT(chk, sctp_next);
8032 				if (chk->whoTo != net) {
8033 					/* No, not sent to this net */
8034 					continue;
8035 				}
8036 				if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8037 					/*-
8038 					 * strange, we have a chunk that is
8039 					 * to big for its destination and
8040 					 * yet no fragment ok flag.
8041 					 * Something went wrong when the
8042 					 * PMTU changed...we did not mark
8043 					 * this chunk for some reason?? I
8044 					 * will fix it here by letting IP
8045 					 * fragment it for now and printing
8046 					 * a warning. This really should not
8047 					 * happen ...
8048 					 */
8049 					SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8050 					    chk->send_size, mtu);
8051 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8052 				}
8053 				if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8054 				    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
8055 					struct sctp_data_chunk *dchkh;
8056 
8057 					dchkh = mtod(chk->data, struct sctp_data_chunk *);
8058 					dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8059 				}
8060 				if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8061 				    ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8062 					/* ok we will add this one */
8063 
8064 					/*
8065 					 * Add an AUTH chunk, if chunk
8066 					 * requires it, save the offset into
8067 					 * the chain for AUTH
8068 					 */
8069 					if ((auth == NULL) &&
8070 					    (sctp_auth_is_required_chunk(SCTP_DATA,
8071 					    stcb->asoc.peer_auth_chunks))) {
8072 
8073 						outchain = sctp_add_auth_chunk(outchain,
8074 						    &endoutchain,
8075 						    &auth,
8076 						    &auth_offset,
8077 						    stcb,
8078 						    SCTP_DATA);
8079 						SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8080 					}
8081 					outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8082 					    chk->send_size, chk->copy_by_ref);
8083 					if (outchain == NULL) {
8084 						SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8085 						if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8086 							sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8087 						}
8088 						*reason_code = 3;
8089 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8090 						return (ENOMEM);
8091 					}
8092 					/* upate our MTU size */
8093 					/* Do clear IP_DF ? */
8094 					if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8095 						no_fragmentflg = 0;
8096 					}
8097 					/* unsigned subtraction of mtu */
8098 					if (mtu > chk->send_size)
8099 						mtu -= chk->send_size;
8100 					else
8101 						mtu = 0;
8102 					/* unsigned subtraction of r_mtu */
8103 					if (r_mtu > chk->send_size)
8104 						r_mtu -= chk->send_size;
8105 					else
8106 						r_mtu = 0;
8107 
8108 					to_out += chk->send_size;
8109 					if ((to_out > mx_mtu) && no_fragmentflg) {
8110 #ifdef INVARIANTS
8111 						panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8112 #else
8113 						SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8114 						    mx_mtu, to_out);
8115 #endif
8116 					}
8117 					chk->window_probe = 0;
8118 					data_list[bundle_at++] = chk;
8119 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8120 						mtu = 0;
8121 						break;
8122 					}
8123 					if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8124 						if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8125 							SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8126 						} else {
8127 							SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8128 						}
8129 						if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8130 						    ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8131 							/*
8132 							 * Count number of
8133 							 * user msg's that
8134 							 * were fragmented
8135 							 * we do this by
8136 							 * counting when we
8137 							 * see a LAST
8138 							 * fragment only.
8139 							 */
8140 							SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8141 					}
8142 					if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8143 						if (one_chunk) {
8144 							data_list[0]->window_probe = 1;
8145 							net->window_probe = 1;
8146 						}
8147 						break;
8148 					}
8149 				} else {
8150 					/*
8151 					 * Must be sent in order of the
8152 					 * TSN's (on a network)
8153 					 */
8154 					break;
8155 				}
8156 			}	/* for (chunk gather loop for this net) */
8157 		}		/* if asoc.state OPEN */
8158 		/* Is there something to send for this destination? */
8159 		if (outchain) {
8160 			/* We may need to start a control timer or two */
8161 			if (asconf) {
8162 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8163 				    stcb, net);
8164 				/*
8165 				 * do NOT clear the asconf flag as it is
8166 				 * used to do appropriate source address
8167 				 * selection.
8168 				 */
8169 			}
8170 			if (cookie) {
8171 				sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8172 				cookie = 0;
8173 			}
8174 			/* must start a send timer if data is being sent */
8175 			if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8176 				/*
8177 				 * no timer running on this destination
8178 				 * restart it.
8179 				 */
8180 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8181 			} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
8182 				    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
8183 				    pf_hbflag &&
8184 				    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
8185 			    (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8186 				/*
8187 				 * JRS 5/14/07 - If a HB has been sent to a
8188 				 * PF destination and no T3 timer is
8189 				 * currently running, start the T3 timer to
8190 				 * track the HBs that were sent.
8191 				 */
8192 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8193 			}
8194 			/* Now send it, if there is anything to send :> */
8195 			SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
8196 			if (outchain == NULL) {
8197 				/* out of mbufs */
8198 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
8199 				error = ENOBUFS;
8200 				goto errored_send;
8201 			}
8202 			shdr = mtod(outchain, struct sctphdr *);
8203 			shdr->src_port = inp->sctp_lport;
8204 			shdr->dest_port = stcb->rport;
8205 			shdr->v_tag = htonl(stcb->asoc.peer_vtag);
8206 			shdr->checksum = 0;
8207 			auth_offset += sizeof(struct sctphdr);
8208 			if ((error = sctp_lowlevel_chunk_output(inp,
8209 			    stcb,
8210 			    net,
8211 			    (struct sockaddr *)&net->ro._l_addr,
8212 			    outchain,
8213 			    auth_offset,
8214 			    auth,
8215 			    no_fragmentflg,
8216 			    bundle_at,
8217 			    data_list[0],
8218 			    asconf, net->port, so_locked, NULL))) {
8219 				/* error, we could not output */
8220 				if (error == ENOBUFS) {
8221 					SCTP_STAT_INCR(sctps_lowlevelerr);
8222 					asoc->ifp_had_enobuf = 1;
8223 				}
8224 				if (from_where == 0) {
8225 					SCTP_STAT_INCR(sctps_lowlevelerrusr);
8226 				}
8227 		errored_send:
8228 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8229 				if (hbflag) {
8230 					if (*now_filled == 0) {
8231 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8232 						*now_filled = 1;
8233 						*now = net->last_sent_time;
8234 					} else {
8235 						net->last_sent_time = *now;
8236 					}
8237 					hbflag = 0;
8238 				}
8239 				if (error == EHOSTUNREACH) {
8240 					/*
8241 					 * Destination went unreachable
8242 					 * during this send
8243 					 */
8244 					sctp_move_to_an_alt(stcb, asoc, net);
8245 				}
8246 				*reason_code = 6;
8247 				/*-
8248 				 * I add this line to be paranoid. As far as
8249 				 * I can tell the continue, takes us back to
8250 				 * the top of the for, but just to make sure
8251 				 * I will reset these again here.
8252 				 */
8253 				ctl_cnt = bundle_at = 0;
8254 				continue;	/* This takes us back to the
8255 						 * for() for the nets. */
8256 			} else {
8257 				asoc->ifp_had_enobuf = 0;
8258 			}
8259 			outchain = endoutchain = NULL;
8260 			auth = NULL;
8261 			auth_offset = 0;
8262 			if (bundle_at || hbflag) {
8263 				/* For data/asconf and hb set time */
8264 				if (*now_filled == 0) {
8265 					(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8266 					*now_filled = 1;
8267 					*now = net->last_sent_time;
8268 				} else {
8269 					net->last_sent_time = *now;
8270 				}
8271 			}
8272 			if (!no_out_cnt) {
8273 				*num_out += (ctl_cnt + bundle_at);
8274 			}
8275 			if (bundle_at) {
8276 				/* setup for a RTO measurement */
8277 				tsns_sent = data_list[0]->rec.data.TSN_seq;
8278 				/* fill time if not already filled */
8279 				if (*now_filled == 0) {
8280 					(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8281 					*now_filled = 1;
8282 					*now = asoc->time_last_sent;
8283 				} else {
8284 					asoc->time_last_sent = *now;
8285 				}
8286 				data_list[0]->do_rtt = 1;
8287 				SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8288 				sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8289 				if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8290 					if (net->flight_size < net->cwnd) {
8291 						/* start or restart it */
8292 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8293 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8294 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
8295 						}
8296 						SCTP_STAT_INCR(sctps_earlyfrstrout);
8297 						sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
8298 					} else {
8299 						/* stop it if its running */
8300 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8301 							SCTP_STAT_INCR(sctps_earlyfrstpout);
8302 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8303 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
8304 						}
8305 					}
8306 				}
8307 			}
8308 			if (one_chunk) {
8309 				break;
8310 			}
8311 		}
8312 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8313 			sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8314 		}
8315 	}
8316 	if (old_startat == NULL) {
8317 		old_startat = send_start_at;
8318 		send_start_at = TAILQ_FIRST(&asoc->nets);
8319 		if (old_startat)
8320 			goto again_one_more_time;
8321 	}
8322 	/*
8323 	 * At the end there should be no NON timed chunks hanging on this
8324 	 * queue.
8325 	 */
8326 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8327 		sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8328 	}
8329 	if ((*num_out == 0) && (*reason_code == 0)) {
8330 		*reason_code = 4;
8331 	} else {
8332 		*reason_code = 5;
8333 	}
8334 	sctp_clean_up_ctl(stcb, asoc);
8335 	return (0);
8336 }
8337 
8338 void
8339 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8340 {
8341 	/*-
8342 	 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8343 	 * the control chunk queue.
8344 	 */
8345 	struct sctp_chunkhdr *hdr;
8346 	struct sctp_tmit_chunk *chk;
8347 	struct mbuf *mat;
8348 
8349 	SCTP_TCB_LOCK_ASSERT(stcb);
8350 	sctp_alloc_a_chunk(stcb, chk);
8351 	if (chk == NULL) {
8352 		/* no memory */
8353 		sctp_m_freem(op_err);
8354 		return;
8355 	}
8356 	chk->copy_by_ref = 0;
8357 	SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8358 	if (op_err == NULL) {
8359 		sctp_free_a_chunk(stcb, chk);
8360 		return;
8361 	}
8362 	chk->send_size = 0;
8363 	mat = op_err;
8364 	while (mat != NULL) {
8365 		chk->send_size += SCTP_BUF_LEN(mat);
8366 		mat = SCTP_BUF_NEXT(mat);
8367 	}
8368 	chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8369 	chk->rec.chunk_id.can_take_data = 1;
8370 	chk->sent = SCTP_DATAGRAM_UNSENT;
8371 	chk->snd_count = 0;
8372 	chk->flags = 0;
8373 	chk->asoc = &stcb->asoc;
8374 	chk->data = op_err;
8375 	chk->whoTo = chk->asoc->primary_destination;
8376 	atomic_add_int(&chk->whoTo->ref_count, 1);
8377 	hdr = mtod(op_err, struct sctp_chunkhdr *);
8378 	hdr->chunk_type = SCTP_OPERATION_ERROR;
8379 	hdr->chunk_flags = 0;
8380 	hdr->chunk_length = htons(chk->send_size);
8381 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8382 	    chk,
8383 	    sctp_next);
8384 	chk->asoc->ctrl_queue_cnt++;
8385 }
8386 
8387 int
8388 sctp_send_cookie_echo(struct mbuf *m,
8389     int offset,
8390     struct sctp_tcb *stcb,
8391     struct sctp_nets *net)
8392 {
8393 	/*-
8394 	 * pull out the cookie and put it at the front of the control chunk
8395 	 * queue.
8396 	 */
8397 	int at;
8398 	struct mbuf *cookie;
8399 	struct sctp_paramhdr parm, *phdr;
8400 	struct sctp_chunkhdr *hdr;
8401 	struct sctp_tmit_chunk *chk;
8402 	uint16_t ptype, plen;
8403 
8404 	/* First find the cookie in the param area */
8405 	cookie = NULL;
8406 	at = offset + sizeof(struct sctp_init_chunk);
8407 
8408 	SCTP_TCB_LOCK_ASSERT(stcb);
8409 	do {
8410 		phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8411 		if (phdr == NULL) {
8412 			return (-3);
8413 		}
8414 		ptype = ntohs(phdr->param_type);
8415 		plen = ntohs(phdr->param_length);
8416 		if (ptype == SCTP_STATE_COOKIE) {
8417 			int pad;
8418 
8419 			/* found the cookie */
8420 			if ((pad = (plen % 4))) {
8421 				plen += 4 - pad;
8422 			}
8423 			cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8424 			if (cookie == NULL) {
8425 				/* No memory */
8426 				return (-2);
8427 			}
8428 #ifdef SCTP_MBUF_LOGGING
8429 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8430 				struct mbuf *mat;
8431 
8432 				mat = cookie;
8433 				while (mat) {
8434 					if (SCTP_BUF_IS_EXTENDED(mat)) {
8435 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8436 					}
8437 					mat = SCTP_BUF_NEXT(mat);
8438 				}
8439 			}
8440 #endif
8441 			break;
8442 		}
8443 		at += SCTP_SIZE32(plen);
8444 	} while (phdr);
8445 	if (cookie == NULL) {
8446 		/* Did not find the cookie */
8447 		return (-3);
8448 	}
8449 	/* ok, we got the cookie lets change it into a cookie echo chunk */
8450 
8451 	/* first the change from param to cookie */
8452 	hdr = mtod(cookie, struct sctp_chunkhdr *);
8453 	hdr->chunk_type = SCTP_COOKIE_ECHO;
8454 	hdr->chunk_flags = 0;
8455 	/* get the chunk stuff now and place it in the FRONT of the queue */
8456 	sctp_alloc_a_chunk(stcb, chk);
8457 	if (chk == NULL) {
8458 		/* no memory */
8459 		sctp_m_freem(cookie);
8460 		return (-5);
8461 	}
8462 	chk->copy_by_ref = 0;
8463 	chk->send_size = plen;
8464 	chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8465 	chk->rec.chunk_id.can_take_data = 0;
8466 	chk->sent = SCTP_DATAGRAM_UNSENT;
8467 	chk->snd_count = 0;
8468 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8469 	chk->asoc = &stcb->asoc;
8470 	chk->data = cookie;
8471 	chk->whoTo = chk->asoc->primary_destination;
8472 	atomic_add_int(&chk->whoTo->ref_count, 1);
8473 	TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8474 	chk->asoc->ctrl_queue_cnt++;
8475 	return (0);
8476 }
8477 
8478 void
8479 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8480     struct mbuf *m,
8481     int offset,
8482     int chk_length,
8483     struct sctp_nets *net)
8484 {
8485 	/*
8486 	 * take a HB request and make it into a HB ack and send it.
8487 	 */
8488 	struct mbuf *outchain;
8489 	struct sctp_chunkhdr *chdr;
8490 	struct sctp_tmit_chunk *chk;
8491 
8492 
8493 	if (net == NULL)
8494 		/* must have a net pointer */
8495 		return;
8496 
8497 	outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
8498 	if (outchain == NULL) {
8499 		/* gak out of memory */
8500 		return;
8501 	}
8502 #ifdef SCTP_MBUF_LOGGING
8503 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8504 		struct mbuf *mat;
8505 
8506 		mat = outchain;
8507 		while (mat) {
8508 			if (SCTP_BUF_IS_EXTENDED(mat)) {
8509 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8510 			}
8511 			mat = SCTP_BUF_NEXT(mat);
8512 		}
8513 	}
8514 #endif
8515 	chdr = mtod(outchain, struct sctp_chunkhdr *);
8516 	chdr->chunk_type = SCTP_HEARTBEAT_ACK;
8517 	chdr->chunk_flags = 0;
8518 	if (chk_length % 4) {
8519 		/* need pad */
8520 		uint32_t cpthis = 0;
8521 		int padlen;
8522 
8523 		padlen = 4 - (chk_length % 4);
8524 		m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
8525 	}
8526 	sctp_alloc_a_chunk(stcb, chk);
8527 	if (chk == NULL) {
8528 		/* no memory */
8529 		sctp_m_freem(outchain);
8530 		return;
8531 	}
8532 	chk->copy_by_ref = 0;
8533 	chk->send_size = chk_length;
8534 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
8535 	chk->rec.chunk_id.can_take_data = 1;
8536 	chk->sent = SCTP_DATAGRAM_UNSENT;
8537 	chk->snd_count = 0;
8538 	chk->flags = 0;
8539 	chk->asoc = &stcb->asoc;
8540 	chk->data = outchain;
8541 	chk->whoTo = net;
8542 	atomic_add_int(&chk->whoTo->ref_count, 1);
8543 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8544 	chk->asoc->ctrl_queue_cnt++;
8545 }
8546 
8547 void
8548 sctp_send_cookie_ack(struct sctp_tcb *stcb)
8549 {
8550 	/* formulate and queue a cookie-ack back to sender */
8551 	struct mbuf *cookie_ack;
8552 	struct sctp_chunkhdr *hdr;
8553 	struct sctp_tmit_chunk *chk;
8554 
8555 	cookie_ack = NULL;
8556 	SCTP_TCB_LOCK_ASSERT(stcb);
8557 
8558 	cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
8559 	if (cookie_ack == NULL) {
8560 		/* no mbuf's */
8561 		return;
8562 	}
8563 	SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
8564 	sctp_alloc_a_chunk(stcb, chk);
8565 	if (chk == NULL) {
8566 		/* no memory */
8567 		sctp_m_freem(cookie_ack);
8568 		return;
8569 	}
8570 	chk->copy_by_ref = 0;
8571 	chk->send_size = sizeof(struct sctp_chunkhdr);
8572 	chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
8573 	chk->rec.chunk_id.can_take_data = 1;
8574 	chk->sent = SCTP_DATAGRAM_UNSENT;
8575 	chk->snd_count = 0;
8576 	chk->flags = 0;
8577 	chk->asoc = &stcb->asoc;
8578 	chk->data = cookie_ack;
8579 	if (chk->asoc->last_control_chunk_from != NULL) {
8580 		chk->whoTo = chk->asoc->last_control_chunk_from;
8581 	} else {
8582 		chk->whoTo = chk->asoc->primary_destination;
8583 	}
8584 	atomic_add_int(&chk->whoTo->ref_count, 1);
8585 	hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
8586 	hdr->chunk_type = SCTP_COOKIE_ACK;
8587 	hdr->chunk_flags = 0;
8588 	hdr->chunk_length = htons(chk->send_size);
8589 	SCTP_BUF_LEN(cookie_ack) = chk->send_size;
8590 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8591 	chk->asoc->ctrl_queue_cnt++;
8592 	return;
8593 }
8594 
8595 
8596 void
8597 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
8598 {
8599 	/* formulate and queue a SHUTDOWN-ACK back to the sender */
8600 	struct mbuf *m_shutdown_ack;
8601 	struct sctp_shutdown_ack_chunk *ack_cp;
8602 	struct sctp_tmit_chunk *chk;
8603 
8604 	m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8605 	if (m_shutdown_ack == NULL) {
8606 		/* no mbuf's */
8607 		return;
8608 	}
8609 	SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
8610 	sctp_alloc_a_chunk(stcb, chk);
8611 	if (chk == NULL) {
8612 		/* no memory */
8613 		sctp_m_freem(m_shutdown_ack);
8614 		return;
8615 	}
8616 	chk->copy_by_ref = 0;
8617 	chk->send_size = sizeof(struct sctp_chunkhdr);
8618 	chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
8619 	chk->rec.chunk_id.can_take_data = 1;
8620 	chk->sent = SCTP_DATAGRAM_UNSENT;
8621 	chk->snd_count = 0;
8622 	chk->flags = 0;
8623 	chk->asoc = &stcb->asoc;
8624 	chk->data = m_shutdown_ack;
8625 	chk->whoTo = net;
8626 	atomic_add_int(&net->ref_count, 1);
8627 
8628 	ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
8629 	ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
8630 	ack_cp->ch.chunk_flags = 0;
8631 	ack_cp->ch.chunk_length = htons(chk->send_size);
8632 	SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
8633 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8634 	chk->asoc->ctrl_queue_cnt++;
8635 	return;
8636 }
8637 
8638 void
8639 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
8640 {
8641 	/* formulate and queue a SHUTDOWN to the sender */
8642 	struct mbuf *m_shutdown;
8643 	struct sctp_shutdown_chunk *shutdown_cp;
8644 	struct sctp_tmit_chunk *chk;
8645 
8646 	m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8647 	if (m_shutdown == NULL) {
8648 		/* no mbuf's */
8649 		return;
8650 	}
8651 	SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
8652 	sctp_alloc_a_chunk(stcb, chk);
8653 	if (chk == NULL) {
8654 		/* no memory */
8655 		sctp_m_freem(m_shutdown);
8656 		return;
8657 	}
8658 	chk->copy_by_ref = 0;
8659 	chk->send_size = sizeof(struct sctp_shutdown_chunk);
8660 	chk->rec.chunk_id.id = SCTP_SHUTDOWN;
8661 	chk->rec.chunk_id.can_take_data = 1;
8662 	chk->sent = SCTP_DATAGRAM_UNSENT;
8663 	chk->snd_count = 0;
8664 	chk->flags = 0;
8665 	chk->asoc = &stcb->asoc;
8666 	chk->data = m_shutdown;
8667 	chk->whoTo = net;
8668 	atomic_add_int(&net->ref_count, 1);
8669 
8670 	shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
8671 	shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
8672 	shutdown_cp->ch.chunk_flags = 0;
8673 	shutdown_cp->ch.chunk_length = htons(chk->send_size);
8674 	shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
8675 	SCTP_BUF_LEN(m_shutdown) = chk->send_size;
8676 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8677 	chk->asoc->ctrl_queue_cnt++;
8678 	return;
8679 }
8680 
8681 void
8682 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
8683 {
8684 	/*
8685 	 * formulate and queue an ASCONF to the peer. ASCONF parameters
8686 	 * should be queued on the assoc queue.
8687 	 */
8688 	struct sctp_tmit_chunk *chk;
8689 	struct mbuf *m_asconf;
8690 	int len;
8691 
8692 	SCTP_TCB_LOCK_ASSERT(stcb);
8693 
8694 	if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
8695 	    (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
8696 		/* can't send a new one if there is one in flight already */
8697 		return;
8698 	}
8699 	/* compose an ASCONF chunk, maximum length is PMTU */
8700 	m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
8701 	if (m_asconf == NULL) {
8702 		return;
8703 	}
8704 	sctp_alloc_a_chunk(stcb, chk);
8705 	if (chk == NULL) {
8706 		/* no memory */
8707 		sctp_m_freem(m_asconf);
8708 		return;
8709 	}
8710 	chk->copy_by_ref = 0;
8711 	chk->data = m_asconf;
8712 	chk->send_size = len;
8713 	chk->rec.chunk_id.id = SCTP_ASCONF;
8714 	chk->rec.chunk_id.can_take_data = 0;
8715 	chk->sent = SCTP_DATAGRAM_UNSENT;
8716 	chk->snd_count = 0;
8717 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8718 	chk->asoc = &stcb->asoc;
8719 	chk->whoTo = net;
8720 	atomic_add_int(&chk->whoTo->ref_count, 1);
8721 	TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
8722 	chk->asoc->ctrl_queue_cnt++;
8723 	return;
8724 }
8725 
8726 void
8727 sctp_send_asconf_ack(struct sctp_tcb *stcb)
8728 {
8729 	/*
8730 	 * formulate and queue a asconf-ack back to sender. the asconf-ack
8731 	 * must be stored in the tcb.
8732 	 */
8733 	struct sctp_tmit_chunk *chk;
8734 	struct sctp_asconf_ack *ack, *latest_ack;
8735 	struct mbuf *m_ack, *m;
8736 	struct sctp_nets *net = NULL;
8737 
8738 	SCTP_TCB_LOCK_ASSERT(stcb);
8739 	/* Get the latest ASCONF-ACK */
8740 	latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
8741 	if (latest_ack == NULL) {
8742 		return;
8743 	}
8744 	if (latest_ack->last_sent_to != NULL &&
8745 	    latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
8746 		/* we're doing a retransmission */
8747 		net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
8748 		if (net == NULL) {
8749 			/* no alternate */
8750 			if (stcb->asoc.last_control_chunk_from == NULL)
8751 				net = stcb->asoc.primary_destination;
8752 			else
8753 				net = stcb->asoc.last_control_chunk_from;
8754 		}
8755 	} else {
8756 		/* normal case */
8757 		if (stcb->asoc.last_control_chunk_from == NULL)
8758 			net = stcb->asoc.primary_destination;
8759 		else
8760 			net = stcb->asoc.last_control_chunk_from;
8761 	}
8762 	latest_ack->last_sent_to = net;
8763 
8764 	TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
8765 		if (ack->data == NULL) {
8766 			continue;
8767 		}
8768 		/* copy the asconf_ack */
8769 		m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
8770 		if (m_ack == NULL) {
8771 			/* couldn't copy it */
8772 			return;
8773 		}
8774 #ifdef SCTP_MBUF_LOGGING
8775 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8776 			struct mbuf *mat;
8777 
8778 			mat = m_ack;
8779 			while (mat) {
8780 				if (SCTP_BUF_IS_EXTENDED(mat)) {
8781 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8782 				}
8783 				mat = SCTP_BUF_NEXT(mat);
8784 			}
8785 		}
8786 #endif
8787 
8788 		sctp_alloc_a_chunk(stcb, chk);
8789 		if (chk == NULL) {
8790 			/* no memory */
8791 			if (m_ack)
8792 				sctp_m_freem(m_ack);
8793 			return;
8794 		}
8795 		chk->copy_by_ref = 0;
8796 
8797 		chk->whoTo = net;
8798 		chk->data = m_ack;
8799 		chk->send_size = 0;
8800 		/* Get size */
8801 		m = m_ack;
8802 		chk->send_size = ack->len;
8803 		chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8804 		chk->rec.chunk_id.can_take_data = 1;
8805 		chk->sent = SCTP_DATAGRAM_UNSENT;
8806 		chk->snd_count = 0;
8807 		chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;	/* XXX */
8808 		chk->asoc = &stcb->asoc;
8809 		atomic_add_int(&chk->whoTo->ref_count, 1);
8810 
8811 		TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8812 		chk->asoc->ctrl_queue_cnt++;
8813 	}
8814 	return;
8815 }
8816 
8817 
8818 static int
8819 sctp_chunk_retransmission(struct sctp_inpcb *inp,
8820     struct sctp_tcb *stcb,
8821     struct sctp_association *asoc,
8822     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
8823 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8824     SCTP_UNUSED
8825 #endif
8826 )
8827 {
8828 	/*-
8829 	 * send out one MTU of retransmission. If fast_retransmit is
8830 	 * happening we ignore the cwnd. Otherwise we obey the cwnd and
8831 	 * rwnd. For a Cookie or Asconf in the control chunk queue we
8832 	 * retransmit them by themselves.
8833 	 *
8834 	 * For data chunks we will pick out the lowest TSN's in the sent_queue
8835 	 * marked for resend and bundle them all together (up to a MTU of
8836 	 * destination). The address to send to should have been
8837 	 * selected/changed where the retransmission was marked (i.e. in FR
8838 	 * or t3-timeout routines).
8839 	 */
8840 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8841 	struct sctp_tmit_chunk *chk, *fwd;
8842 	struct mbuf *m, *endofchain;
8843 	struct sctphdr *shdr;
8844 	struct sctp_nets *net = NULL;
8845 	uint32_t tsns_sent = 0;
8846 	int no_fragmentflg, bundle_at, cnt_thru;
8847 	unsigned int mtu;
8848 	int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
8849 	struct sctp_auth_chunk *auth = NULL;
8850 	uint32_t auth_offset = 0;
8851 	uint32_t dmtu = 0;
8852 
8853 	SCTP_TCB_LOCK_ASSERT(stcb);
8854 	tmr_started = ctl_cnt = bundle_at = error = 0;
8855 	no_fragmentflg = 1;
8856 	fwd_tsn = 0;
8857 	*cnt_out = 0;
8858 	fwd = NULL;
8859 	endofchain = m = NULL;
8860 #ifdef SCTP_AUDITING_ENABLED
8861 	sctp_audit_log(0xC3, 1);
8862 #endif
8863 	if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
8864 	    (TAILQ_EMPTY(&asoc->control_send_queue))) {
8865 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
8866 		    asoc->sent_queue_retran_cnt);
8867 		asoc->sent_queue_cnt = 0;
8868 		asoc->sent_queue_cnt_removeable = 0;
8869 		/* send back 0/0 so we enter normal transmission */
8870 		*cnt_out = 0;
8871 		return (0);
8872 	}
8873 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8874 		if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
8875 		    (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
8876 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
8877 			if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8878 				if (chk != asoc->str_reset) {
8879 					/*
8880 					 * not eligible for retran if its
8881 					 * not ours
8882 					 */
8883 					continue;
8884 				}
8885 			}
8886 			ctl_cnt++;
8887 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8888 				fwd_tsn = 1;
8889 				fwd = chk;
8890 			}
8891 			/*
8892 			 * Add an AUTH chunk, if chunk requires it save the
8893 			 * offset into the chain for AUTH
8894 			 */
8895 			if ((auth == NULL) &&
8896 			    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8897 			    stcb->asoc.peer_auth_chunks))) {
8898 				m = sctp_add_auth_chunk(m, &endofchain,
8899 				    &auth, &auth_offset,
8900 				    stcb,
8901 				    chk->rec.chunk_id.id);
8902 			}
8903 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8904 			break;
8905 		}
8906 	}
8907 	one_chunk = 0;
8908 	cnt_thru = 0;
8909 	/* do we have control chunks to retransmit? */
8910 	if (m != NULL) {
8911 		/* Start a timer no matter if we suceed or fail */
8912 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8913 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
8914 		} else if (chk->rec.chunk_id.id == SCTP_ASCONF)
8915 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
8916 
8917 		SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
8918 		if (m == NULL) {
8919 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
8920 			return (ENOBUFS);
8921 		}
8922 		shdr = mtod(m, struct sctphdr *);
8923 		shdr->src_port = inp->sctp_lport;
8924 		shdr->dest_port = stcb->rport;
8925 		shdr->v_tag = htonl(stcb->asoc.peer_vtag);
8926 		shdr->checksum = 0;
8927 		auth_offset += sizeof(struct sctphdr);
8928 		chk->snd_count++;	/* update our count */
8929 
8930 		if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
8931 		    (struct sockaddr *)&chk->whoTo->ro._l_addr, m, auth_offset,
8932 		    auth, no_fragmentflg, 0, NULL, 0, chk->whoTo->port, so_locked, NULL))) {
8933 			SCTP_STAT_INCR(sctps_lowlevelerr);
8934 			return (error);
8935 		}
8936 		m = endofchain = NULL;
8937 		auth = NULL;
8938 		auth_offset = 0;
8939 		/*
8940 		 * We don't want to mark the net->sent time here since this
8941 		 * we use this for HB and retrans cannot measure RTT
8942 		 */
8943 		/* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
8944 		*cnt_out += 1;
8945 		chk->sent = SCTP_DATAGRAM_SENT;
8946 		sctp_ucount_decr(asoc->sent_queue_retran_cnt);
8947 		if (fwd_tsn == 0) {
8948 			return (0);
8949 		} else {
8950 			/* Clean up the fwd-tsn list */
8951 			sctp_clean_up_ctl(stcb, asoc);
8952 			return (0);
8953 		}
8954 	}
8955 	/*
8956 	 * Ok, it is just data retransmission we need to do or that and a
8957 	 * fwd-tsn with it all.
8958 	 */
8959 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
8960 		return (SCTP_RETRAN_DONE);
8961 	}
8962 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
8963 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
8964 		/* not yet open, resend the cookie and that is it */
8965 		return (1);
8966 	}
8967 #ifdef SCTP_AUDITING_ENABLED
8968 	sctp_auditing(20, inp, stcb, NULL);
8969 #endif
8970 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
8971 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
8972 			/* No, not sent to this net or not ready for rtx */
8973 			continue;
8974 		}
8975 		if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
8976 		    (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
8977 			/* Gak, we have exceeded max unlucky retran, abort! */
8978 			SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
8979 			    chk->snd_count,
8980 			    SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
8981 			atomic_add_int(&stcb->asoc.refcnt, 1);
8982 			sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
8983 			SCTP_TCB_LOCK(stcb);
8984 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
8985 			return (SCTP_RETRAN_EXIT);
8986 		}
8987 		/* pick up the net */
8988 		net = chk->whoTo;
8989 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8990 			mtu = (net->mtu - SCTP_MIN_OVERHEAD);
8991 		} else {
8992 			mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8993 		}
8994 
8995 		if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
8996 			/* No room in peers rwnd */
8997 			uint32_t tsn;
8998 
8999 			tsn = asoc->last_acked_seq + 1;
9000 			if (tsn == chk->rec.data.TSN_seq) {
9001 				/*
9002 				 * we make a special exception for this
9003 				 * case. The peer has no rwnd but is missing
9004 				 * the lowest chunk.. which is probably what
9005 				 * is holding up the rwnd.
9006 				 */
9007 				goto one_chunk_around;
9008 			}
9009 			return (1);
9010 		}
9011 one_chunk_around:
9012 		if (asoc->peers_rwnd < mtu) {
9013 			one_chunk = 1;
9014 			if ((asoc->peers_rwnd == 0) &&
9015 			    (asoc->total_flight == 0)) {
9016 				chk->window_probe = 1;
9017 				chk->whoTo->window_probe = 1;
9018 			}
9019 		}
9020 #ifdef SCTP_AUDITING_ENABLED
9021 		sctp_audit_log(0xC3, 2);
9022 #endif
9023 		bundle_at = 0;
9024 		m = NULL;
9025 		net->fast_retran_ip = 0;
9026 		if (chk->rec.data.doing_fast_retransmit == 0) {
9027 			/*
9028 			 * if no FR in progress skip destination that have
9029 			 * flight_size > cwnd.
9030 			 */
9031 			if (net->flight_size >= net->cwnd) {
9032 				continue;
9033 			}
9034 		} else {
9035 			/*
9036 			 * Mark the destination net to have FR recovery
9037 			 * limits put on it.
9038 			 */
9039 			*fr_done = 1;
9040 			net->fast_retran_ip = 1;
9041 		}
9042 
9043 		/*
9044 		 * if no AUTH is yet included and this chunk requires it,
9045 		 * make sure to account for it.  We don't apply the size
9046 		 * until the AUTH chunk is actually added below in case
9047 		 * there is no room for this chunk.
9048 		 */
9049 		if ((auth == NULL) &&
9050 		    sctp_auth_is_required_chunk(SCTP_DATA,
9051 		    stcb->asoc.peer_auth_chunks)) {
9052 			dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9053 		} else
9054 			dmtu = 0;
9055 
9056 		if ((chk->send_size <= (mtu - dmtu)) ||
9057 		    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9058 			/* ok we will add this one */
9059 			if ((auth == NULL) &&
9060 			    (sctp_auth_is_required_chunk(SCTP_DATA,
9061 			    stcb->asoc.peer_auth_chunks))) {
9062 				m = sctp_add_auth_chunk(m, &endofchain,
9063 				    &auth, &auth_offset,
9064 				    stcb, SCTP_DATA);
9065 			}
9066 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9067 			if (m == NULL) {
9068 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9069 				return (ENOMEM);
9070 			}
9071 			/* Do clear IP_DF ? */
9072 			if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9073 				no_fragmentflg = 0;
9074 			}
9075 			/* upate our MTU size */
9076 			if (mtu > (chk->send_size + dmtu))
9077 				mtu -= (chk->send_size + dmtu);
9078 			else
9079 				mtu = 0;
9080 			data_list[bundle_at++] = chk;
9081 			if (one_chunk && (asoc->total_flight <= 0)) {
9082 				SCTP_STAT_INCR(sctps_windowprobed);
9083 			}
9084 		}
9085 		if (one_chunk == 0) {
9086 			/*
9087 			 * now are there anymore forward from chk to pick
9088 			 * up?
9089 			 */
9090 			fwd = TAILQ_NEXT(chk, sctp_next);
9091 			while (fwd) {
9092 				if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9093 					/* Nope, not for retran */
9094 					fwd = TAILQ_NEXT(fwd, sctp_next);
9095 					continue;
9096 				}
9097 				if (fwd->whoTo != net) {
9098 					/* Nope, not the net in question */
9099 					fwd = TAILQ_NEXT(fwd, sctp_next);
9100 					continue;
9101 				}
9102 				if ((auth == NULL) &&
9103 				    sctp_auth_is_required_chunk(SCTP_DATA,
9104 				    stcb->asoc.peer_auth_chunks)) {
9105 					dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9106 				} else
9107 					dmtu = 0;
9108 				if (fwd->send_size <= (mtu - dmtu)) {
9109 					if ((auth == NULL) &&
9110 					    (sctp_auth_is_required_chunk(SCTP_DATA,
9111 					    stcb->asoc.peer_auth_chunks))) {
9112 						m = sctp_add_auth_chunk(m,
9113 						    &endofchain,
9114 						    &auth, &auth_offset,
9115 						    stcb,
9116 						    SCTP_DATA);
9117 					}
9118 					m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9119 					if (m == NULL) {
9120 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9121 						return (ENOMEM);
9122 					}
9123 					/* Do clear IP_DF ? */
9124 					if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9125 						no_fragmentflg = 0;
9126 					}
9127 					/* upate our MTU size */
9128 					if (mtu > (fwd->send_size + dmtu))
9129 						mtu -= (fwd->send_size + dmtu);
9130 					else
9131 						mtu = 0;
9132 					data_list[bundle_at++] = fwd;
9133 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9134 						break;
9135 					}
9136 					fwd = TAILQ_NEXT(fwd, sctp_next);
9137 				} else {
9138 					/* can't fit so we are done */
9139 					break;
9140 				}
9141 			}
9142 		}
9143 		/* Is there something to send for this destination? */
9144 		if (m) {
9145 			/*
9146 			 * No matter if we fail/or suceed we should start a
9147 			 * timer. A failure is like a lost IP packet :-)
9148 			 */
9149 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9150 				/*
9151 				 * no timer running on this destination
9152 				 * restart it.
9153 				 */
9154 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9155 				tmr_started = 1;
9156 			}
9157 			SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
9158 			if (m == NULL) {
9159 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
9160 				return (ENOBUFS);
9161 			}
9162 			shdr = mtod(m, struct sctphdr *);
9163 			shdr->src_port = inp->sctp_lport;
9164 			shdr->dest_port = stcb->rport;
9165 			shdr->v_tag = htonl(stcb->asoc.peer_vtag);
9166 			shdr->checksum = 0;
9167 			auth_offset += sizeof(struct sctphdr);
9168 			/* Now lets send it, if there is anything to send :> */
9169 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9170 			    (struct sockaddr *)&net->ro._l_addr, m, auth_offset,
9171 			    auth, no_fragmentflg, 0, NULL, 0, net->port, so_locked, NULL))) {
9172 				/* error, we could not output */
9173 				SCTP_STAT_INCR(sctps_lowlevelerr);
9174 				return (error);
9175 			}
9176 			m = endofchain = NULL;
9177 			auth = NULL;
9178 			auth_offset = 0;
9179 			/* For HB's */
9180 			/*
9181 			 * We don't want to mark the net->sent time here
9182 			 * since this we use this for HB and retrans cannot
9183 			 * measure RTT
9184 			 */
9185 			/* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9186 
9187 			/* For auto-close */
9188 			cnt_thru++;
9189 			if (*now_filled == 0) {
9190 				(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9191 				*now = asoc->time_last_sent;
9192 				*now_filled = 1;
9193 			} else {
9194 				asoc->time_last_sent = *now;
9195 			}
9196 			*cnt_out += bundle_at;
9197 #ifdef SCTP_AUDITING_ENABLED
9198 			sctp_audit_log(0xC4, bundle_at);
9199 #endif
9200 			if (bundle_at) {
9201 				tsns_sent = data_list[0]->rec.data.TSN_seq;
9202 			}
9203 			for (i = 0; i < bundle_at; i++) {
9204 				SCTP_STAT_INCR(sctps_sendretransdata);
9205 				data_list[i]->sent = SCTP_DATAGRAM_SENT;
9206 				/*
9207 				 * When we have a revoked data, and we
9208 				 * retransmit it, then we clear the revoked
9209 				 * flag since this flag dictates if we
9210 				 * subtracted from the fs
9211 				 */
9212 				if (data_list[i]->rec.data.chunk_was_revoked) {
9213 					/* Deflate the cwnd */
9214 					data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9215 					data_list[i]->rec.data.chunk_was_revoked = 0;
9216 				}
9217 				data_list[i]->snd_count++;
9218 				sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9219 				/* record the time */
9220 				data_list[i]->sent_rcv_time = asoc->time_last_sent;
9221 				if (data_list[i]->book_size_scale) {
9222 					/*
9223 					 * need to double the book size on
9224 					 * this one
9225 					 */
9226 					data_list[i]->book_size_scale = 0;
9227 					/*
9228 					 * Since we double the booksize, we
9229 					 * must also double the output queue
9230 					 * size, since this get shrunk when
9231 					 * we free by this amount.
9232 					 */
9233 					atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9234 					data_list[i]->book_size *= 2;
9235 
9236 
9237 				} else {
9238 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9239 						sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9240 						    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9241 					}
9242 					asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9243 					    (uint32_t) (data_list[i]->send_size +
9244 					    SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9245 				}
9246 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9247 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9248 					    data_list[i]->whoTo->flight_size,
9249 					    data_list[i]->book_size,
9250 					    (uintptr_t) data_list[i]->whoTo,
9251 					    data_list[i]->rec.data.TSN_seq);
9252 				}
9253 				sctp_flight_size_increase(data_list[i]);
9254 				sctp_total_flight_increase(stcb, data_list[i]);
9255 				if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9256 					/* SWS sender side engages */
9257 					asoc->peers_rwnd = 0;
9258 				}
9259 				if ((i == 0) &&
9260 				    (data_list[i]->rec.data.doing_fast_retransmit)) {
9261 					SCTP_STAT_INCR(sctps_sendfastretrans);
9262 					if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9263 					    (tmr_started == 0)) {
9264 						/*-
9265 						 * ok we just fast-retrans'd
9266 						 * the lowest TSN, i.e the
9267 						 * first on the list. In
9268 						 * this case we want to give
9269 						 * some more time to get a
9270 						 * SACK back without a
9271 						 * t3-expiring.
9272 						 */
9273 						sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9274 						    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9275 						sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9276 					}
9277 				}
9278 			}
9279 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9280 				sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9281 			}
9282 #ifdef SCTP_AUDITING_ENABLED
9283 			sctp_auditing(21, inp, stcb, NULL);
9284 #endif
9285 		} else {
9286 			/* None will fit */
9287 			return (1);
9288 		}
9289 		if (asoc->sent_queue_retran_cnt <= 0) {
9290 			/* all done we have no more to retran */
9291 			asoc->sent_queue_retran_cnt = 0;
9292 			break;
9293 		}
9294 		if (one_chunk) {
9295 			/* No more room in rwnd */
9296 			return (1);
9297 		}
9298 		/* stop the for loop here. we sent out a packet */
9299 		break;
9300 	}
9301 	return (0);
9302 }
9303 
9304 
9305 static int
9306 sctp_timer_validation(struct sctp_inpcb *inp,
9307     struct sctp_tcb *stcb,
9308     struct sctp_association *asoc,
9309     int ret)
9310 {
9311 	struct sctp_nets *net;
9312 
9313 	/* Validate that a timer is running somewhere */
9314 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9315 		if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9316 			/* Here is a timer */
9317 			return (ret);
9318 		}
9319 	}
9320 	SCTP_TCB_LOCK_ASSERT(stcb);
9321 	/* Gak, we did not have a timer somewhere */
9322 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9323 	sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9324 	return (ret);
9325 }
9326 
9327 void
9328 sctp_chunk_output(struct sctp_inpcb *inp,
9329     struct sctp_tcb *stcb,
9330     int from_where,
9331     int so_locked
9332 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9333     SCTP_UNUSED
9334 #endif
9335 )
9336 {
9337 	/*-
9338 	 * Ok this is the generic chunk service queue. we must do the
9339 	 * following:
9340 	 * - See if there are retransmits pending, if so we must
9341 	 *   do these first.
9342 	 * - Service the stream queue that is next, moving any
9343 	 *   message (note I must get a complete message i.e.
9344 	 *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9345 	 *   TSN's
9346 	 * - Check to see if the cwnd/rwnd allows any output, if so we
9347 	 *   go ahead and fomulate and send the low level chunks. Making sure
9348 	 *   to combine any control in the control chunk queue also.
9349 	 */
9350 	struct sctp_association *asoc;
9351 	struct sctp_nets *net;
9352 	int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
9353 	    burst_cnt = 0, burst_limit = 0;
9354 	struct timeval now;
9355 	int now_filled = 0;
9356 	int cwnd_full = 0;
9357 	int nagle_on = 0;
9358 	int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9359 	int un_sent = 0;
9360 	int fr_done, tot_frs = 0;
9361 
9362 	asoc = &stcb->asoc;
9363 	if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9364 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9365 			nagle_on = 0;
9366 		} else {
9367 			nagle_on = 1;
9368 		}
9369 	}
9370 	SCTP_TCB_LOCK_ASSERT(stcb);
9371 
9372 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9373 
9374 	if ((un_sent <= 0) &&
9375 	    (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9376 	    (asoc->sent_queue_retran_cnt == 0)) {
9377 		/* Nothing to do unless there is something to be sent left */
9378 		return;
9379 	}
9380 	/*
9381 	 * Do we have something to send, data or control AND a sack timer
9382 	 * running, if so piggy-back the sack.
9383 	 */
9384 	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9385 		sctp_send_sack(stcb);
9386 		(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9387 	}
9388 	while (asoc->sent_queue_retran_cnt) {
9389 		/*-
9390 		 * Ok, it is retransmission time only, we send out only ONE
9391 		 * packet with a single call off to the retran code.
9392 		 */
9393 		if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9394 			/*-
9395 			 * Special hook for handling cookiess discarded
9396 			 * by peer that carried data. Send cookie-ack only
9397 			 * and then the next call with get the retran's.
9398 			 */
9399 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9400 			    &cwnd_full, from_where,
9401 			    &now, &now_filled, frag_point, so_locked);
9402 			return;
9403 		} else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9404 			/* if its not from a HB then do it */
9405 			fr_done = 0;
9406 			ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9407 			if (fr_done) {
9408 				tot_frs++;
9409 			}
9410 		} else {
9411 			/*
9412 			 * its from any other place, we don't allow retran
9413 			 * output (only control)
9414 			 */
9415 			ret = 1;
9416 		}
9417 		if (ret > 0) {
9418 			/* Can't send anymore */
9419 			/*-
9420 			 * now lets push out control by calling med-level
9421 			 * output once. this assures that we WILL send HB's
9422 			 * if queued too.
9423 			 */
9424 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9425 			    &cwnd_full, from_where,
9426 			    &now, &now_filled, frag_point, so_locked);
9427 #ifdef SCTP_AUDITING_ENABLED
9428 			sctp_auditing(8, inp, stcb, NULL);
9429 #endif
9430 			(void)sctp_timer_validation(inp, stcb, asoc, ret);
9431 			return;
9432 		}
9433 		if (ret < 0) {
9434 			/*-
9435 			 * The count was off.. retran is not happening so do
9436 			 * the normal retransmission.
9437 			 */
9438 #ifdef SCTP_AUDITING_ENABLED
9439 			sctp_auditing(9, inp, stcb, NULL);
9440 #endif
9441 			if (ret == SCTP_RETRAN_EXIT) {
9442 				return;
9443 			}
9444 			break;
9445 		}
9446 		if (from_where == SCTP_OUTPUT_FROM_T3) {
9447 			/* Only one transmission allowed out of a timeout */
9448 #ifdef SCTP_AUDITING_ENABLED
9449 			sctp_auditing(10, inp, stcb, NULL);
9450 #endif
9451 			/* Push out any control */
9452 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
9453 			    &now, &now_filled, frag_point, so_locked);
9454 			return;
9455 		}
9456 		if (tot_frs > asoc->max_burst) {
9457 			/* Hit FR burst limit */
9458 			return;
9459 		}
9460 		if ((num_out == 0) && (ret == 0)) {
9461 
9462 			/* No more retrans to send */
9463 			break;
9464 		}
9465 	}
9466 #ifdef SCTP_AUDITING_ENABLED
9467 	sctp_auditing(12, inp, stcb, NULL);
9468 #endif
9469 	/* Check for bad destinations, if they exist move chunks around. */
9470 	burst_limit = asoc->max_burst;
9471 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9472 		if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
9473 		    SCTP_ADDR_NOT_REACHABLE) {
9474 			/*-
9475 			 * if possible move things off of this address we
9476 			 * still may send below due to the dormant state but
9477 			 * we try to find an alternate address to send to
9478 			 * and if we have one we move all queued data on the
9479 			 * out wheel to this alternate address.
9480 			 */
9481 			if (net->ref_count > 1)
9482 				sctp_move_to_an_alt(stcb, asoc, net);
9483 		} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
9484 			    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
9485 		    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
9486 			/*
9487 			 * JRS 5/14/07 - If CMT PF is on and the current
9488 			 * destination is in PF state, move all queued data
9489 			 * to an alternate desination.
9490 			 */
9491 			if (net->ref_count > 1)
9492 				sctp_move_to_an_alt(stcb, asoc, net);
9493 		} else {
9494 			/*-
9495 			 * if ((asoc->sat_network) || (net->addr_is_local))
9496 			 * { burst_limit = asoc->max_burst *
9497 			 * SCTP_SAT_NETWORK_BURST_INCR; }
9498 			 */
9499 			if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
9500 				if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
9501 					/*
9502 					 * JRS - Use the congestion control
9503 					 * given in the congestion control
9504 					 * module
9505 					 */
9506 					asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
9507 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9508 						sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
9509 					}
9510 					SCTP_STAT_INCR(sctps_maxburstqueued);
9511 				}
9512 				net->fast_retran_ip = 0;
9513 			} else {
9514 				if (net->flight_size == 0) {
9515 					/* Should be decaying the cwnd here */
9516 					;
9517 				}
9518 			}
9519 		}
9520 
9521 	}
9522 	burst_cnt = 0;
9523 	cwnd_full = 0;
9524 	do {
9525 		error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
9526 		    &reason_code, 0, &cwnd_full, from_where,
9527 		    &now, &now_filled, frag_point, so_locked);
9528 		if (error) {
9529 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
9530 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9531 				sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
9532 			}
9533 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9534 				sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
9535 				sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
9536 			}
9537 			break;
9538 		}
9539 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
9540 
9541 		tot_out += num_out;
9542 		burst_cnt++;
9543 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9544 			sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
9545 			if (num_out == 0) {
9546 				sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
9547 			}
9548 		}
9549 		if (nagle_on) {
9550 			/*-
9551 			 * When nagle is on, we look at how much is un_sent, then
9552 			 * if its smaller than an MTU and we have data in
9553 			 * flight we stop.
9554 			 */
9555 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9556 			    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
9557 			if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
9558 			    (stcb->asoc.total_flight > 0)) {
9559 				break;
9560 			}
9561 		}
9562 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9563 		    TAILQ_EMPTY(&asoc->send_queue) &&
9564 		    TAILQ_EMPTY(&asoc->out_wheel)) {
9565 			/* Nothing left to send */
9566 			break;
9567 		}
9568 		if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
9569 			/* Nothing left to send */
9570 			break;
9571 		}
9572 	} while (num_out && (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
9573 	    (burst_cnt < burst_limit)));
9574 
9575 	if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
9576 		if (burst_cnt >= burst_limit) {
9577 			SCTP_STAT_INCR(sctps_maxburstqueued);
9578 			asoc->burst_limit_applied = 1;
9579 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9580 				sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
9581 			}
9582 		} else {
9583 			asoc->burst_limit_applied = 0;
9584 		}
9585 	}
9586 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9587 		sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
9588 	}
9589 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
9590 	    tot_out);
9591 
9592 	/*-
9593 	 * Now we need to clean up the control chunk chain if a ECNE is on
9594 	 * it. It must be marked as UNSENT again so next call will continue
9595 	 * to send it until such time that we get a CWR, to remove it.
9596 	 */
9597 	if (stcb->asoc.ecn_echo_cnt_onq)
9598 		sctp_fix_ecn_echo(asoc);
9599 	return;
9600 }
9601 
9602 
9603 int
9604 sctp_output(inp, m, addr, control, p, flags)
9605 	struct sctp_inpcb *inp;
9606 	struct mbuf *m;
9607 	struct sockaddr *addr;
9608 	struct mbuf *control;
9609 	struct thread *p;
9610 	int flags;
9611 {
9612 	if (inp == NULL) {
9613 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9614 		return (EINVAL);
9615 	}
9616 	if (inp->sctp_socket == NULL) {
9617 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9618 		return (EINVAL);
9619 	}
9620 	return (sctp_sosend(inp->sctp_socket,
9621 	    addr,
9622 	    (struct uio *)NULL,
9623 	    m,
9624 	    control,
9625 	    flags, p
9626 	    ));
9627 }
9628 
9629 void
9630 send_forward_tsn(struct sctp_tcb *stcb,
9631     struct sctp_association *asoc)
9632 {
9633 	struct sctp_tmit_chunk *chk;
9634 	struct sctp_forward_tsn_chunk *fwdtsn;
9635 
9636 	SCTP_TCB_LOCK_ASSERT(stcb);
9637 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9638 		if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9639 			/* mark it to unsent */
9640 			chk->sent = SCTP_DATAGRAM_UNSENT;
9641 			chk->snd_count = 0;
9642 			/* Do we correct its output location? */
9643 			if (chk->whoTo != asoc->primary_destination) {
9644 				sctp_free_remote_addr(chk->whoTo);
9645 				chk->whoTo = asoc->primary_destination;
9646 				atomic_add_int(&chk->whoTo->ref_count, 1);
9647 			}
9648 			goto sctp_fill_in_rest;
9649 		}
9650 	}
9651 	/* Ok if we reach here we must build one */
9652 	sctp_alloc_a_chunk(stcb, chk);
9653 	if (chk == NULL) {
9654 		return;
9655 	}
9656 	chk->copy_by_ref = 0;
9657 	chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
9658 	chk->rec.chunk_id.can_take_data = 0;
9659 	chk->asoc = asoc;
9660 	chk->whoTo = NULL;
9661 
9662 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9663 	if (chk->data == NULL) {
9664 		sctp_free_a_chunk(stcb, chk);
9665 		return;
9666 	}
9667 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9668 	chk->sent = SCTP_DATAGRAM_UNSENT;
9669 	chk->snd_count = 0;
9670 	chk->whoTo = asoc->primary_destination;
9671 	atomic_add_int(&chk->whoTo->ref_count, 1);
9672 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
9673 	asoc->ctrl_queue_cnt++;
9674 sctp_fill_in_rest:
9675 	/*-
9676 	 * Here we go through and fill out the part that deals with
9677 	 * stream/seq of the ones we skip.
9678 	 */
9679 	SCTP_BUF_LEN(chk->data) = 0;
9680 	{
9681 		struct sctp_tmit_chunk *at, *tp1, *last;
9682 		struct sctp_strseq *strseq;
9683 		unsigned int cnt_of_space, i, ovh;
9684 		unsigned int space_needed;
9685 		unsigned int cnt_of_skipped = 0;
9686 
9687 		TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
9688 			if (at->sent != SCTP_FORWARD_TSN_SKIP) {
9689 				/* no more to look at */
9690 				break;
9691 			}
9692 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9693 				/* We don't report these */
9694 				continue;
9695 			}
9696 			cnt_of_skipped++;
9697 		}
9698 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
9699 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
9700 
9701 		cnt_of_space = M_TRAILINGSPACE(chk->data);
9702 
9703 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9704 			ovh = SCTP_MIN_OVERHEAD;
9705 		} else {
9706 			ovh = SCTP_MIN_V4_OVERHEAD;
9707 		}
9708 		if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
9709 			/* trim to a mtu size */
9710 			cnt_of_space = asoc->smallest_mtu - ovh;
9711 		}
9712 		if (cnt_of_space < space_needed) {
9713 			/*-
9714 			 * ok we must trim down the chunk by lowering the
9715 			 * advance peer ack point.
9716 			 */
9717 			cnt_of_skipped = (cnt_of_space -
9718 			    ((sizeof(struct sctp_forward_tsn_chunk)) /
9719 			    sizeof(struct sctp_strseq)));
9720 			/*-
9721 			 * Go through and find the TSN that will be the one
9722 			 * we report.
9723 			 */
9724 			at = TAILQ_FIRST(&asoc->sent_queue);
9725 			for (i = 0; i < cnt_of_skipped; i++) {
9726 				tp1 = TAILQ_NEXT(at, sctp_next);
9727 				at = tp1;
9728 			}
9729 			last = at;
9730 			/*-
9731 			 * last now points to last one I can report, update
9732 			 * peer ack point
9733 			 */
9734 			asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
9735 			space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
9736 		}
9737 		chk->send_size = space_needed;
9738 		/* Setup the chunk */
9739 		fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
9740 		fwdtsn->ch.chunk_length = htons(chk->send_size);
9741 		fwdtsn->ch.chunk_flags = 0;
9742 		fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
9743 		fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
9744 		chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
9745 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
9746 		SCTP_BUF_LEN(chk->data) = chk->send_size;
9747 		fwdtsn++;
9748 		/*-
9749 		 * Move pointer to after the fwdtsn and transfer to the
9750 		 * strseq pointer.
9751 		 */
9752 		strseq = (struct sctp_strseq *)fwdtsn;
9753 		/*-
9754 		 * Now populate the strseq list. This is done blindly
9755 		 * without pulling out duplicate stream info. This is
9756 		 * inefficent but won't harm the process since the peer will
9757 		 * look at these in sequence and will thus release anything.
9758 		 * It could mean we exceed the PMTU and chop off some that
9759 		 * we could have included.. but this is unlikely (aka 1432/4
9760 		 * would mean 300+ stream seq's would have to be reported in
9761 		 * one FWD-TSN. With a bit of work we can later FIX this to
9762 		 * optimize and pull out duplcates.. but it does add more
9763 		 * overhead. So for now... not!
9764 		 */
9765 		at = TAILQ_FIRST(&asoc->sent_queue);
9766 		for (i = 0; i < cnt_of_skipped; i++) {
9767 			tp1 = TAILQ_NEXT(at, sctp_next);
9768 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9769 				/* We don't report these */
9770 				i--;
9771 				at = tp1;
9772 				continue;
9773 			}
9774 			strseq->stream = ntohs(at->rec.data.stream_number);
9775 			strseq->sequence = ntohs(at->rec.data.stream_seq);
9776 			strseq++;
9777 			at = tp1;
9778 		}
9779 	}
9780 	return;
9781 
9782 }
9783 
9784 void
9785 sctp_send_sack(struct sctp_tcb *stcb)
9786 {
9787 	/*-
9788 	 * Queue up a SACK in the control queue. We must first check to see
9789 	 * if a SACK is somehow on the control queue. If so, we will take
9790 	 * and and remove the old one.
9791 	 */
9792 	struct sctp_association *asoc;
9793 	struct sctp_tmit_chunk *chk, *a_chk;
9794 	struct sctp_sack_chunk *sack;
9795 	struct sctp_gap_ack_block *gap_descriptor;
9796 	struct sack_track *selector;
9797 	int mergeable = 0;
9798 	int offset;
9799 	caddr_t limit;
9800 	uint32_t *dup;
9801 	int limit_reached = 0;
9802 	unsigned int i, jstart, siz, j;
9803 	unsigned int num_gap_blocks = 0, space;
9804 	int num_dups = 0;
9805 	int space_req;
9806 
9807 	a_chk = NULL;
9808 	asoc = &stcb->asoc;
9809 	SCTP_TCB_LOCK_ASSERT(stcb);
9810 	if (asoc->last_data_chunk_from == NULL) {
9811 		/* Hmm we never received anything */
9812 		return;
9813 	}
9814 	sctp_set_rwnd(stcb, asoc);
9815 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9816 		if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
9817 			/* Hmm, found a sack already on queue, remove it */
9818 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
9819 			asoc->ctrl_queue_cnt++;
9820 			a_chk = chk;
9821 			if (a_chk->data) {
9822 				sctp_m_freem(a_chk->data);
9823 				a_chk->data = NULL;
9824 			}
9825 			sctp_free_remote_addr(a_chk->whoTo);
9826 			a_chk->whoTo = NULL;
9827 			break;
9828 		}
9829 	}
9830 	if (a_chk == NULL) {
9831 		sctp_alloc_a_chunk(stcb, a_chk);
9832 		if (a_chk == NULL) {
9833 			/* No memory so we drop the idea, and set a timer */
9834 			if (stcb->asoc.delayed_ack) {
9835 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9836 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
9837 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9838 				    stcb->sctp_ep, stcb, NULL);
9839 			} else {
9840 				stcb->asoc.send_sack = 1;
9841 			}
9842 			return;
9843 		}
9844 		a_chk->copy_by_ref = 0;
9845 		/* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
9846 		a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK;
9847 		a_chk->rec.chunk_id.can_take_data = 1;
9848 	}
9849 	/* Clear our pkt counts */
9850 	asoc->data_pkts_seen = 0;
9851 
9852 	a_chk->asoc = asoc;
9853 	a_chk->snd_count = 0;
9854 	a_chk->send_size = 0;	/* fill in later */
9855 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
9856 	a_chk->whoTo = NULL;
9857 
9858 	if ((asoc->numduptsns) ||
9859 	    (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
9860 	    ) {
9861 		/*-
9862 		 * Ok, we have some duplicates or the destination for the
9863 		 * sack is unreachable, lets see if we can select an
9864 		 * alternate than asoc->last_data_chunk_from
9865 		 */
9866 		if ((!(asoc->last_data_chunk_from->dest_state &
9867 		    SCTP_ADDR_NOT_REACHABLE)) &&
9868 		    (asoc->used_alt_onsack > asoc->numnets)) {
9869 			/* We used an alt last time, don't this time */
9870 			a_chk->whoTo = NULL;
9871 		} else {
9872 			asoc->used_alt_onsack++;
9873 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
9874 		}
9875 		if (a_chk->whoTo == NULL) {
9876 			/* Nope, no alternate */
9877 			a_chk->whoTo = asoc->last_data_chunk_from;
9878 			asoc->used_alt_onsack = 0;
9879 		}
9880 	} else {
9881 		/*
9882 		 * No duplicates so we use the last place we received data
9883 		 * from.
9884 		 */
9885 		asoc->used_alt_onsack = 0;
9886 		a_chk->whoTo = asoc->last_data_chunk_from;
9887 	}
9888 	if (a_chk->whoTo) {
9889 		atomic_add_int(&a_chk->whoTo->ref_count, 1);
9890 	}
9891 	if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
9892 		/* no gaps */
9893 		space_req = sizeof(struct sctp_sack_chunk);
9894 	} else {
9895 		/* gaps get a cluster */
9896 		space_req = MCLBYTES;
9897 	}
9898 	/* Ok now lets formulate a MBUF with our sack */
9899 	a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
9900 	if ((a_chk->data == NULL) ||
9901 	    (a_chk->whoTo == NULL)) {
9902 		/* rats, no mbuf memory */
9903 		if (a_chk->data) {
9904 			/* was a problem with the destination */
9905 			sctp_m_freem(a_chk->data);
9906 			a_chk->data = NULL;
9907 		}
9908 		sctp_free_a_chunk(stcb, a_chk);
9909 		/* sa_ignore NO_NULL_CHK */
9910 		if (stcb->asoc.delayed_ack) {
9911 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9912 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
9913 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9914 			    stcb->sctp_ep, stcb, NULL);
9915 		} else {
9916 			stcb->asoc.send_sack = 1;
9917 		}
9918 		return;
9919 	}
9920 	/* ok, lets go through and fill it in */
9921 	SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
9922 	space = M_TRAILINGSPACE(a_chk->data);
9923 	if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
9924 		space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
9925 	}
9926 	limit = mtod(a_chk->data, caddr_t);
9927 	limit += space;
9928 
9929 	sack = mtod(a_chk->data, struct sctp_sack_chunk *);
9930 	sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
9931 	/* 0x01 is used by nonce for ecn */
9932 	if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
9933 	    (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
9934 	    (asoc->peer_supports_ecn_nonce))
9935 		sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
9936 	else
9937 		sack->ch.chunk_flags = 0;
9938 
9939 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
9940 		/*-
9941 		 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
9942 		 * received, then set high bit to 1, else 0. Reset
9943 		 * pkts_rcvd.
9944 		 */
9945 		sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
9946 		asoc->cmt_dac_pkts_rcvd = 0;
9947 	}
9948 #ifdef SCTP_ASOCLOG_OF_TSNS
9949 	stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
9950 	stcb->asoc.cumack_log_atsnt++;
9951 	if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
9952 		stcb->asoc.cumack_log_atsnt = 0;
9953 	}
9954 #endif
9955 	sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
9956 	sack->sack.a_rwnd = htonl(asoc->my_rwnd);
9957 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
9958 
9959 	/* reset the readers interpretation */
9960 	stcb->freed_by_sorcv_sincelast = 0;
9961 
9962 	gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
9963 
9964 	siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
9965 	if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
9966 		offset = 1;
9967 		/*-
9968 		 * cum-ack behind the mapping array, so we start and use all
9969 		 * entries.
9970 		 */
9971 		jstart = 0;
9972 	} else {
9973 		offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
9974 		/*-
9975 		 * we skip the first one when the cum-ack is at or above the
9976 		 * mapping array base. Note this only works if
9977 		 */
9978 		jstart = 1;
9979 	}
9980 	if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
9981 		/* we have a gap .. maybe */
9982 		for (i = 0; i < siz; i++) {
9983 			selector = &sack_array[asoc->mapping_array[i]];
9984 			if (mergeable && selector->right_edge) {
9985 				/*
9986 				 * Backup, left and right edges were ok to
9987 				 * merge.
9988 				 */
9989 				num_gap_blocks--;
9990 				gap_descriptor--;
9991 			}
9992 			if (selector->num_entries == 0)
9993 				mergeable = 0;
9994 			else {
9995 				for (j = jstart; j < selector->num_entries; j++) {
9996 					if (mergeable && selector->right_edge) {
9997 						/*
9998 						 * do a merge by NOT setting
9999 						 * the left side
10000 						 */
10001 						mergeable = 0;
10002 					} else {
10003 						/*
10004 						 * no merge, set the left
10005 						 * side
10006 						 */
10007 						mergeable = 0;
10008 						gap_descriptor->start = htons((selector->gaps[j].start + offset));
10009 					}
10010 					gap_descriptor->end = htons((selector->gaps[j].end + offset));
10011 					num_gap_blocks++;
10012 					gap_descriptor++;
10013 					if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10014 						/* no more room */
10015 						limit_reached = 1;
10016 						break;
10017 					}
10018 				}
10019 				if (selector->left_edge) {
10020 					mergeable = 1;
10021 				}
10022 			}
10023 			if (limit_reached) {
10024 				/* Reached the limit stop */
10025 				break;
10026 			}
10027 			jstart = 0;
10028 			offset += 8;
10029 		}
10030 		if (num_gap_blocks == 0) {
10031 			/*
10032 			 * slide not yet happened, and somehow we got called
10033 			 * to send a sack. Cumack needs to move up.
10034 			 */
10035 			int abort_flag = 0;
10036 
10037 			asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
10038 			sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10039 			sctp_sack_check(stcb, 0, 0, &abort_flag);
10040 		}
10041 	}
10042 	/* now we must add any dups we are going to report. */
10043 	if ((limit_reached == 0) && (asoc->numduptsns)) {
10044 		dup = (uint32_t *) gap_descriptor;
10045 		for (i = 0; i < asoc->numduptsns; i++) {
10046 			*dup = htonl(asoc->dup_tsns[i]);
10047 			dup++;
10048 			num_dups++;
10049 			if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10050 				/* no more room */
10051 				break;
10052 			}
10053 		}
10054 		asoc->numduptsns = 0;
10055 	}
10056 	/*
10057 	 * now that the chunk is prepared queue it to the control chunk
10058 	 * queue.
10059 	 */
10060 	a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
10061 	    (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
10062 	    (num_dups * sizeof(int32_t)));
10063 	SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10064 	sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10065 	sack->sack.num_dup_tsns = htons(num_dups);
10066 	sack->ch.chunk_length = htons(a_chk->send_size);
10067 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10068 	asoc->ctrl_queue_cnt++;
10069 	asoc->send_sack = 0;
10070 	SCTP_STAT_INCR(sctps_sendsacks);
10071 	return;
10072 }
10073 
10074 
10075 void
10076 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10077 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10078     SCTP_UNUSED
10079 #endif
10080 )
10081 {
10082 	struct mbuf *m_abort;
10083 	struct mbuf *m_out = NULL, *m_end = NULL;
10084 	struct sctp_abort_chunk *abort = NULL;
10085 	int sz;
10086 	uint32_t auth_offset = 0;
10087 	struct sctp_auth_chunk *auth = NULL;
10088 	struct sctphdr *shdr;
10089 
10090 	/*-
10091 	 * Add an AUTH chunk, if chunk requires it and save the offset into
10092 	 * the chain for AUTH
10093 	 */
10094 	if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10095 	    stcb->asoc.peer_auth_chunks)) {
10096 		m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10097 		    stcb, SCTP_ABORT_ASSOCIATION);
10098 	}
10099 	SCTP_TCB_LOCK_ASSERT(stcb);
10100 	m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10101 	if (m_abort == NULL) {
10102 		/* no mbuf's */
10103 		if (m_out)
10104 			sctp_m_freem(m_out);
10105 		return;
10106 	}
10107 	/* link in any error */
10108 	SCTP_BUF_NEXT(m_abort) = operr;
10109 	sz = 0;
10110 	if (operr) {
10111 		struct mbuf *n;
10112 
10113 		n = operr;
10114 		while (n) {
10115 			sz += SCTP_BUF_LEN(n);
10116 			n = SCTP_BUF_NEXT(n);
10117 		}
10118 	}
10119 	SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10120 	if (m_out == NULL) {
10121 		/* NO Auth chunk prepended, so reserve space in front */
10122 		SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10123 		m_out = m_abort;
10124 	} else {
10125 		/* Put AUTH chunk at the front of the chain */
10126 		SCTP_BUF_NEXT(m_end) = m_abort;
10127 	}
10128 
10129 	/* fill in the ABORT chunk */
10130 	abort = mtod(m_abort, struct sctp_abort_chunk *);
10131 	abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10132 	abort->ch.chunk_flags = 0;
10133 	abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10134 
10135 	/* prepend and fill in the SCTP header */
10136 	SCTP_BUF_PREPEND(m_out, sizeof(struct sctphdr), M_DONTWAIT);
10137 	if (m_out == NULL) {
10138 		/* TSNH: no memory */
10139 		return;
10140 	}
10141 	shdr = mtod(m_out, struct sctphdr *);
10142 	shdr->src_port = stcb->sctp_ep->sctp_lport;
10143 	shdr->dest_port = stcb->rport;
10144 	shdr->v_tag = htonl(stcb->asoc.peer_vtag);
10145 	shdr->checksum = 0;
10146 	auth_offset += sizeof(struct sctphdr);
10147 
10148 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
10149 	    stcb->asoc.primary_destination,
10150 	    (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
10151 	    m_out, auth_offset, auth, 1, 0, NULL, 0, stcb->asoc.primary_destination->port, so_locked, NULL);
10152 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10153 }
10154 
10155 void
10156 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10157     struct sctp_nets *net)
10158 {
10159 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10160 	struct mbuf *m_shutdown_comp;
10161 	struct sctp_shutdown_complete_msg *comp_cp;
10162 
10163 	m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_complete_msg), 0, M_DONTWAIT, 1, MT_HEADER);
10164 	if (m_shutdown_comp == NULL) {
10165 		/* no mbuf's */
10166 		return;
10167 	}
10168 	comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
10169 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10170 	comp_cp->shut_cmp.ch.chunk_flags = 0;
10171 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10172 	comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
10173 	comp_cp->sh.dest_port = stcb->rport;
10174 	comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
10175 	comp_cp->sh.checksum = 0;
10176 
10177 	SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_msg);
10178 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10179 	    (struct sockaddr *)&net->ro._l_addr,
10180 	    m_shutdown_comp, 0, NULL, 1, 0, NULL, 0, net->port, SCTP_SO_NOT_LOCKED, NULL);
10181 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10182 	return;
10183 }
10184 
10185 void
10186 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
10187     uint32_t vrf_id, uint16_t port)
10188 {
10189 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10190 	struct mbuf *o_pak;
10191 	struct mbuf *mout;
10192 	struct ip *iph, *iph_out;
10193 	struct udphdr *udp = NULL;
10194 
10195 #ifdef INET6
10196 	struct ip6_hdr *ip6, *ip6_out;
10197 
10198 #endif
10199 	int offset_out, len, mlen;
10200 	struct sctp_shutdown_complete_msg *comp_cp;
10201 
10202 	iph = mtod(m, struct ip *);
10203 	switch (iph->ip_v) {
10204 	case IPVERSION:
10205 		len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10206 		break;
10207 #ifdef INET6
10208 	case IPV6_VERSION >> 4:
10209 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10210 		break;
10211 #endif
10212 	default:
10213 		return;
10214 	}
10215 	if (port) {
10216 		len += sizeof(struct udphdr);
10217 	}
10218 	mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
10219 	if (mout == NULL) {
10220 		return;
10221 	}
10222 	SCTP_BUF_LEN(mout) = len;
10223 	SCTP_BUF_NEXT(mout) = NULL;
10224 	iph_out = NULL;
10225 #ifdef INET6
10226 	ip6_out = NULL;
10227 #endif
10228 	offset_out = 0;
10229 
10230 	switch (iph->ip_v) {
10231 	case IPVERSION:
10232 		iph_out = mtod(mout, struct ip *);
10233 
10234 		/* Fill in the IP header for the ABORT */
10235 		iph_out->ip_v = IPVERSION;
10236 		iph_out->ip_hl = (sizeof(struct ip) / 4);
10237 		iph_out->ip_tos = (u_char)0;
10238 		iph_out->ip_id = 0;
10239 		iph_out->ip_off = 0;
10240 		iph_out->ip_ttl = MAXTTL;
10241 		if (port) {
10242 			iph_out->ip_p = IPPROTO_UDP;
10243 		} else {
10244 			iph_out->ip_p = IPPROTO_SCTP;
10245 		}
10246 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10247 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10248 
10249 		/* let IP layer calculate this */
10250 		iph_out->ip_sum = 0;
10251 		offset_out += sizeof(*iph_out);
10252 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10253 		    (caddr_t)iph_out + offset_out);
10254 		break;
10255 #ifdef INET6
10256 	case IPV6_VERSION >> 4:
10257 		ip6 = (struct ip6_hdr *)iph;
10258 		ip6_out = mtod(mout, struct ip6_hdr *);
10259 
10260 		/* Fill in the IPv6 header for the ABORT */
10261 		ip6_out->ip6_flow = ip6->ip6_flow;
10262 		ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
10263 		if (port) {
10264 			ip6_out->ip6_nxt = IPPROTO_UDP;
10265 		} else {
10266 			ip6_out->ip6_nxt = IPPROTO_SCTP;
10267 		}
10268 		ip6_out->ip6_src = ip6->ip6_dst;
10269 		ip6_out->ip6_dst = ip6->ip6_src;
10270 		/*
10271 		 * ?? The old code had both the iph len + payload, I think
10272 		 * this is wrong and would never have worked
10273 		 */
10274 		ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10275 		offset_out += sizeof(*ip6_out);
10276 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10277 		    (caddr_t)ip6_out + offset_out);
10278 		break;
10279 #endif				/* INET6 */
10280 	default:
10281 		/* Currently not supported. */
10282 		return;
10283 	}
10284 	if (port) {
10285 		udp = (struct udphdr *)comp_cp;
10286 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10287 		udp->uh_dport = port;
10288 		udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
10289 		udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
10290 		offset_out += sizeof(struct udphdr);
10291 		comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
10292 	}
10293 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10294 		/* no mbuf's */
10295 		sctp_m_freem(mout);
10296 		return;
10297 	}
10298 	/* Now copy in and fill in the ABORT tags etc. */
10299 	comp_cp->sh.src_port = sh->dest_port;
10300 	comp_cp->sh.dest_port = sh->src_port;
10301 	comp_cp->sh.checksum = 0;
10302 	comp_cp->sh.v_tag = sh->v_tag;
10303 	comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
10304 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10305 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10306 
10307 	/* add checksum */
10308 	comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
10309 	if (iph_out != NULL) {
10310 		sctp_route_t ro;
10311 		int ret;
10312 		struct sctp_tcb *stcb = NULL;
10313 
10314 		mlen = SCTP_BUF_LEN(mout);
10315 		bzero(&ro, sizeof ro);
10316 		/* set IPv4 length */
10317 		iph_out->ip_len = mlen;
10318 #ifdef  SCTP_PACKET_LOGGING
10319 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10320 			sctp_packet_log(mout, mlen);
10321 #endif
10322 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10323 		if (port) {
10324 			SCTP_ENABLE_UDP_CSUM(o_pak);
10325 		}
10326 		/* out it goes */
10327 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
10328 
10329 		/* Free the route if we got one back */
10330 		if (ro.ro_rt)
10331 			RTFREE(ro.ro_rt);
10332 	}
10333 #ifdef INET6
10334 	if (ip6_out != NULL) {
10335 		struct route_in6 ro;
10336 		int ret;
10337 		struct sctp_tcb *stcb = NULL;
10338 		struct ifnet *ifp = NULL;
10339 
10340 		bzero(&ro, sizeof(ro));
10341 		mlen = SCTP_BUF_LEN(mout);
10342 #ifdef  SCTP_PACKET_LOGGING
10343 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10344 			sctp_packet_log(mout, mlen);
10345 #endif
10346 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10347 		if (port) {
10348 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr),
10349 			    sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr))) == 0) {
10350 				udp->uh_sum = 0xffff;
10351 			}
10352 		}
10353 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
10354 
10355 		/* Free the route if we got one back */
10356 		if (ro.ro_rt)
10357 			RTFREE(ro.ro_rt);
10358 	}
10359 #endif
10360 	SCTP_STAT_INCR(sctps_sendpackets);
10361 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10362 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10363 	return;
10364 
10365 }
10366 
10367 static struct sctp_nets *
10368 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
10369 {
10370 	struct sctp_nets *net, *hnet;
10371 	int ms_goneby, highest_ms, state_overide = 0;
10372 
10373 	(void)SCTP_GETTIME_TIMEVAL(now);
10374 	highest_ms = 0;
10375 	hnet = NULL;
10376 	SCTP_TCB_LOCK_ASSERT(stcb);
10377 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
10378 		if (
10379 		    ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
10380 		    (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
10381 		    ) {
10382 			/*
10383 			 * Skip this guy from consideration if HB is off AND
10384 			 * its confirmed
10385 			 */
10386 			continue;
10387 		}
10388 		if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
10389 			/* skip this dest net from consideration */
10390 			continue;
10391 		}
10392 		if (net->last_sent_time.tv_sec) {
10393 			/* Sent to so we subtract */
10394 			ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
10395 		} else
10396 			/* Never been sent to */
10397 			ms_goneby = 0x7fffffff;
10398 		/*-
10399 		 * When the address state is unconfirmed but still
10400 		 * considered reachable, we HB at a higher rate. Once it
10401 		 * goes confirmed OR reaches the "unreachable" state, thenw
10402 		 * we cut it back to HB at a more normal pace.
10403 		 */
10404 		if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
10405 			state_overide = 1;
10406 		} else {
10407 			state_overide = 0;
10408 		}
10409 
10410 		if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
10411 		    (ms_goneby > highest_ms)) {
10412 			highest_ms = ms_goneby;
10413 			hnet = net;
10414 		}
10415 	}
10416 	if (hnet &&
10417 	    ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
10418 		state_overide = 1;
10419 	} else {
10420 		state_overide = 0;
10421 	}
10422 
10423 	if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
10424 		/*-
10425 		 * Found the one with longest delay bounds OR it is
10426 		 * unconfirmed and still not marked unreachable.
10427 		 */
10428 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
10429 #ifdef SCTP_DEBUG
10430 		if (hnet) {
10431 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
10432 			    (struct sockaddr *)&hnet->ro._l_addr);
10433 		} else {
10434 			SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
10435 		}
10436 #endif
10437 		/* update the timer now */
10438 		hnet->last_sent_time = *now;
10439 		return (hnet);
10440 	}
10441 	/* Nothing to HB */
10442 	return (NULL);
10443 }
10444 
10445 int
10446 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
10447 {
10448 	struct sctp_tmit_chunk *chk;
10449 	struct sctp_nets *net;
10450 	struct sctp_heartbeat_chunk *hb;
10451 	struct timeval now;
10452 	struct sockaddr_in *sin;
10453 	struct sockaddr_in6 *sin6;
10454 
10455 	SCTP_TCB_LOCK_ASSERT(stcb);
10456 	if (user_req == 0) {
10457 		net = sctp_select_hb_destination(stcb, &now);
10458 		if (net == NULL) {
10459 			/*-
10460 			 * All our busy none to send to, just start the
10461 			 * timer again.
10462 			 */
10463 			if (stcb->asoc.state == 0) {
10464 				return (0);
10465 			}
10466 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
10467 			    stcb->sctp_ep,
10468 			    stcb,
10469 			    net);
10470 			return (0);
10471 		}
10472 	} else {
10473 		net = u_net;
10474 		if (net == NULL) {
10475 			return (0);
10476 		}
10477 		(void)SCTP_GETTIME_TIMEVAL(&now);
10478 	}
10479 	sin = (struct sockaddr_in *)&net->ro._l_addr;
10480 	if (sin->sin_family != AF_INET) {
10481 		if (sin->sin_family != AF_INET6) {
10482 			/* huh */
10483 			return (0);
10484 		}
10485 	}
10486 	sctp_alloc_a_chunk(stcb, chk);
10487 	if (chk == NULL) {
10488 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
10489 		return (0);
10490 	}
10491 	chk->copy_by_ref = 0;
10492 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
10493 	chk->rec.chunk_id.can_take_data = 1;
10494 	chk->asoc = &stcb->asoc;
10495 	chk->send_size = sizeof(struct sctp_heartbeat_chunk);
10496 
10497 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10498 	if (chk->data == NULL) {
10499 		sctp_free_a_chunk(stcb, chk);
10500 		return (0);
10501 	}
10502 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10503 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10504 	chk->sent = SCTP_DATAGRAM_UNSENT;
10505 	chk->snd_count = 0;
10506 	chk->whoTo = net;
10507 	atomic_add_int(&chk->whoTo->ref_count, 1);
10508 	/* Now we have a mbuf that we can fill in with the details */
10509 	hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
10510 	memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
10511 	/* fill out chunk header */
10512 	hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
10513 	hb->ch.chunk_flags = 0;
10514 	hb->ch.chunk_length = htons(chk->send_size);
10515 	/* Fill out hb parameter */
10516 	hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
10517 	hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
10518 	hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
10519 	hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
10520 	/* Did our user request this one, put it in */
10521 	hb->heartbeat.hb_info.user_req = user_req;
10522 	hb->heartbeat.hb_info.addr_family = sin->sin_family;
10523 	hb->heartbeat.hb_info.addr_len = sin->sin_len;
10524 	if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
10525 		/*
10526 		 * we only take from the entropy pool if the address is not
10527 		 * confirmed.
10528 		 */
10529 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10530 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10531 	} else {
10532 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
10533 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
10534 	}
10535 	if (sin->sin_family == AF_INET) {
10536 		memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
10537 	} else if (sin->sin_family == AF_INET6) {
10538 		/* We leave the scope the way it is in our lookup table. */
10539 		sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
10540 		memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
10541 	} else {
10542 		/* huh compiler bug */
10543 		return (0);
10544 	}
10545 
10546 	/*
10547 	 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
10548 	 * PF-heartbeats.  Because of this, threshold management is done by
10549 	 * the t3 timer handler, and does not need to be done upon the send
10550 	 * of a PF-heartbeat. If CMT PF is on and the destination to which a
10551 	 * heartbeat is being sent is in PF state, do NOT do threshold
10552 	 * management.
10553 	 */
10554 	if ((SCTP_BASE_SYSCTL(sctp_cmt_pf) == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
10555 		/* ok we have a destination that needs a beat */
10556 		/* lets do the theshold management Qiaobing style */
10557 		if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
10558 		    stcb->asoc.max_send_times)) {
10559 			/*-
10560 			 * we have lost the association, in a way this is
10561 			 * quite bad since we really are one less time since
10562 			 * we really did not send yet. This is the down side
10563 			 * to the Q's style as defined in the RFC and not my
10564 			 * alternate style defined in the RFC.
10565 			 */
10566 			if (chk->data != NULL) {
10567 				sctp_m_freem(chk->data);
10568 				chk->data = NULL;
10569 			}
10570 			/*
10571 			 * Here we do NOT use the macro since the
10572 			 * association is now gone.
10573 			 */
10574 			if (chk->whoTo) {
10575 				sctp_free_remote_addr(chk->whoTo);
10576 				chk->whoTo = NULL;
10577 			}
10578 			sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
10579 			return (-1);
10580 		}
10581 	}
10582 	net->hb_responded = 0;
10583 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10584 	stcb->asoc.ctrl_queue_cnt++;
10585 	SCTP_STAT_INCR(sctps_sendheartbeat);
10586 	/*-
10587 	 * Call directly med level routine to put out the chunk. It will
10588 	 * always tumble out control chunks aka HB but it may even tumble
10589 	 * out data too.
10590 	 */
10591 	return (1);
10592 }
10593 
10594 void
10595 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
10596     uint32_t high_tsn)
10597 {
10598 	struct sctp_association *asoc;
10599 	struct sctp_ecne_chunk *ecne;
10600 	struct sctp_tmit_chunk *chk;
10601 
10602 	asoc = &stcb->asoc;
10603 	SCTP_TCB_LOCK_ASSERT(stcb);
10604 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10605 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
10606 			/* found a previous ECN_ECHO update it if needed */
10607 			ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10608 			ecne->tsn = htonl(high_tsn);
10609 			return;
10610 		}
10611 	}
10612 	/* nope could not find one to update so we must build one */
10613 	sctp_alloc_a_chunk(stcb, chk);
10614 	if (chk == NULL) {
10615 		return;
10616 	}
10617 	chk->copy_by_ref = 0;
10618 	SCTP_STAT_INCR(sctps_sendecne);
10619 	chk->rec.chunk_id.id = SCTP_ECN_ECHO;
10620 	chk->rec.chunk_id.can_take_data = 0;
10621 	chk->asoc = &stcb->asoc;
10622 	chk->send_size = sizeof(struct sctp_ecne_chunk);
10623 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10624 	if (chk->data == NULL) {
10625 		sctp_free_a_chunk(stcb, chk);
10626 		return;
10627 	}
10628 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10629 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10630 	chk->sent = SCTP_DATAGRAM_UNSENT;
10631 	chk->snd_count = 0;
10632 	chk->whoTo = net;
10633 	atomic_add_int(&chk->whoTo->ref_count, 1);
10634 	stcb->asoc.ecn_echo_cnt_onq++;
10635 	ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10636 	ecne->ch.chunk_type = SCTP_ECN_ECHO;
10637 	ecne->ch.chunk_flags = 0;
10638 	ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
10639 	ecne->tsn = htonl(high_tsn);
10640 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10641 	asoc->ctrl_queue_cnt++;
10642 }
10643 
10644 void
10645 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
10646     struct mbuf *m, int iphlen, int bad_crc)
10647 {
10648 	struct sctp_association *asoc;
10649 	struct sctp_pktdrop_chunk *drp;
10650 	struct sctp_tmit_chunk *chk;
10651 	uint8_t *datap;
10652 	int len;
10653 	int was_trunc = 0;
10654 	struct ip *iph;
10655 
10656 #ifdef INET6
10657 	struct ip6_hdr *ip6h;
10658 
10659 #endif
10660 	int fullsz = 0, extra = 0;
10661 	long spc;
10662 	int offset;
10663 	struct sctp_chunkhdr *ch, chunk_buf;
10664 	unsigned int chk_length;
10665 
10666 	if (!stcb) {
10667 		return;
10668 	}
10669 	asoc = &stcb->asoc;
10670 	SCTP_TCB_LOCK_ASSERT(stcb);
10671 	if (asoc->peer_supports_pktdrop == 0) {
10672 		/*-
10673 		 * peer must declare support before I send one.
10674 		 */
10675 		return;
10676 	}
10677 	if (stcb->sctp_socket == NULL) {
10678 		return;
10679 	}
10680 	sctp_alloc_a_chunk(stcb, chk);
10681 	if (chk == NULL) {
10682 		return;
10683 	}
10684 	chk->copy_by_ref = 0;
10685 	iph = mtod(m, struct ip *);
10686 	if (iph == NULL) {
10687 		sctp_free_a_chunk(stcb, chk);
10688 		return;
10689 	}
10690 	switch (iph->ip_v) {
10691 	case IPVERSION:
10692 		/* IPv4 */
10693 		len = chk->send_size = iph->ip_len;
10694 		break;
10695 #ifdef INET6
10696 	case IPV6_VERSION >> 4:
10697 		/* IPv6 */
10698 		ip6h = mtod(m, struct ip6_hdr *);
10699 		len = chk->send_size = htons(ip6h->ip6_plen);
10700 		break;
10701 #endif
10702 	default:
10703 		return;
10704 	}
10705 	/* Validate that we do not have an ABORT in here. */
10706 	offset = iphlen + sizeof(struct sctphdr);
10707 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10708 	    sizeof(*ch), (uint8_t *) & chunk_buf);
10709 	while (ch != NULL) {
10710 		chk_length = ntohs(ch->chunk_length);
10711 		if (chk_length < sizeof(*ch)) {
10712 			/* break to abort land */
10713 			break;
10714 		}
10715 		switch (ch->chunk_type) {
10716 		case SCTP_PACKET_DROPPED:
10717 		case SCTP_ABORT_ASSOCIATION:
10718 			/*-
10719 			 * we don't respond with an PKT-DROP to an ABORT
10720 			 * or PKT-DROP
10721 			 */
10722 			sctp_free_a_chunk(stcb, chk);
10723 			return;
10724 		default:
10725 			break;
10726 		}
10727 		offset += SCTP_SIZE32(chk_length);
10728 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10729 		    sizeof(*ch), (uint8_t *) & chunk_buf);
10730 	}
10731 
10732 	if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
10733 	    min(stcb->asoc.smallest_mtu, MCLBYTES)) {
10734 		/*
10735 		 * only send 1 mtu worth, trim off the excess on the end.
10736 		 */
10737 		fullsz = len - extra;
10738 		len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
10739 		was_trunc = 1;
10740 	}
10741 	chk->asoc = &stcb->asoc;
10742 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10743 	if (chk->data == NULL) {
10744 jump_out:
10745 		sctp_free_a_chunk(stcb, chk);
10746 		return;
10747 	}
10748 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10749 	drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
10750 	if (drp == NULL) {
10751 		sctp_m_freem(chk->data);
10752 		chk->data = NULL;
10753 		goto jump_out;
10754 	}
10755 	chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
10756 	    sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
10757 	chk->book_size_scale = 0;
10758 	if (was_trunc) {
10759 		drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
10760 		drp->trunc_len = htons(fullsz);
10761 		/*
10762 		 * Len is already adjusted to size minus overhead above take
10763 		 * out the pkt_drop chunk itself from it.
10764 		 */
10765 		chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
10766 		len = chk->send_size;
10767 	} else {
10768 		/* no truncation needed */
10769 		drp->ch.chunk_flags = 0;
10770 		drp->trunc_len = htons(0);
10771 	}
10772 	if (bad_crc) {
10773 		drp->ch.chunk_flags |= SCTP_BADCRC;
10774 	}
10775 	chk->send_size += sizeof(struct sctp_pktdrop_chunk);
10776 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10777 	chk->sent = SCTP_DATAGRAM_UNSENT;
10778 	chk->snd_count = 0;
10779 	if (net) {
10780 		/* we should hit here */
10781 		chk->whoTo = net;
10782 	} else {
10783 		chk->whoTo = asoc->primary_destination;
10784 	}
10785 	atomic_add_int(&chk->whoTo->ref_count, 1);
10786 	chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
10787 	chk->rec.chunk_id.can_take_data = 1;
10788 	drp->ch.chunk_type = SCTP_PACKET_DROPPED;
10789 	drp->ch.chunk_length = htons(chk->send_size);
10790 	spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
10791 	if (spc < 0) {
10792 		spc = 0;
10793 	}
10794 	drp->bottle_bw = htonl(spc);
10795 	if (asoc->my_rwnd) {
10796 		drp->current_onq = htonl(asoc->size_on_reasm_queue +
10797 		    asoc->size_on_all_streams +
10798 		    asoc->my_rwnd_control_len +
10799 		    stcb->sctp_socket->so_rcv.sb_cc);
10800 	} else {
10801 		/*-
10802 		 * If my rwnd is 0, possibly from mbuf depletion as well as
10803 		 * space used, tell the peer there is NO space aka onq == bw
10804 		 */
10805 		drp->current_onq = htonl(spc);
10806 	}
10807 	drp->reserved = 0;
10808 	datap = drp->data;
10809 	m_copydata(m, iphlen, len, (caddr_t)datap);
10810 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10811 	asoc->ctrl_queue_cnt++;
10812 }
10813 
10814 void
10815 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
10816 {
10817 	struct sctp_association *asoc;
10818 	struct sctp_cwr_chunk *cwr;
10819 	struct sctp_tmit_chunk *chk;
10820 
10821 	asoc = &stcb->asoc;
10822 	SCTP_TCB_LOCK_ASSERT(stcb);
10823 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10824 		if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
10825 			/* found a previous ECN_CWR update it if needed */
10826 			cwr = mtod(chk->data, struct sctp_cwr_chunk *);
10827 			if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
10828 			    MAX_TSN)) {
10829 				cwr->tsn = htonl(high_tsn);
10830 			}
10831 			return;
10832 		}
10833 	}
10834 	/* nope could not find one to update so we must build one */
10835 	sctp_alloc_a_chunk(stcb, chk);
10836 	if (chk == NULL) {
10837 		return;
10838 	}
10839 	chk->copy_by_ref = 0;
10840 	chk->rec.chunk_id.id = SCTP_ECN_CWR;
10841 	chk->rec.chunk_id.can_take_data = 1;
10842 	chk->asoc = &stcb->asoc;
10843 	chk->send_size = sizeof(struct sctp_cwr_chunk);
10844 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10845 	if (chk->data == NULL) {
10846 		sctp_free_a_chunk(stcb, chk);
10847 		return;
10848 	}
10849 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10850 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10851 	chk->sent = SCTP_DATAGRAM_UNSENT;
10852 	chk->snd_count = 0;
10853 	chk->whoTo = net;
10854 	atomic_add_int(&chk->whoTo->ref_count, 1);
10855 	cwr = mtod(chk->data, struct sctp_cwr_chunk *);
10856 	cwr->ch.chunk_type = SCTP_ECN_CWR;
10857 	cwr->ch.chunk_flags = 0;
10858 	cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
10859 	cwr->tsn = htonl(high_tsn);
10860 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10861 	asoc->ctrl_queue_cnt++;
10862 }
10863 
10864 void
10865 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
10866     int number_entries, uint16_t * list,
10867     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
10868 {
10869 	int len, old_len, i;
10870 	struct sctp_stream_reset_out_request *req_out;
10871 	struct sctp_chunkhdr *ch;
10872 
10873 	ch = mtod(chk->data, struct sctp_chunkhdr *);
10874 
10875 
10876 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10877 
10878 	/* get to new offset for the param. */
10879 	req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
10880 	/* now how long will this param be? */
10881 	len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
10882 	req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
10883 	req_out->ph.param_length = htons(len);
10884 	req_out->request_seq = htonl(seq);
10885 	req_out->response_seq = htonl(resp_seq);
10886 	req_out->send_reset_at_tsn = htonl(last_sent);
10887 	if (number_entries) {
10888 		for (i = 0; i < number_entries; i++) {
10889 			req_out->list_of_streams[i] = htons(list[i]);
10890 		}
10891 	}
10892 	if (SCTP_SIZE32(len) > len) {
10893 		/*-
10894 		 * Need to worry about the pad we may end up adding to the
10895 		 * end. This is easy since the struct is either aligned to 4
10896 		 * bytes or 2 bytes off.
10897 		 */
10898 		req_out->list_of_streams[number_entries] = 0;
10899 	}
10900 	/* now fix the chunk length */
10901 	ch->chunk_length = htons(len + old_len);
10902 	chk->book_size = len + old_len;
10903 	chk->book_size_scale = 0;
10904 	chk->send_size = SCTP_SIZE32(chk->book_size);
10905 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10906 	return;
10907 }
10908 
10909 
10910 void
10911 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
10912     int number_entries, uint16_t * list,
10913     uint32_t seq)
10914 {
10915 	int len, old_len, i;
10916 	struct sctp_stream_reset_in_request *req_in;
10917 	struct sctp_chunkhdr *ch;
10918 
10919 	ch = mtod(chk->data, struct sctp_chunkhdr *);
10920 
10921 
10922 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10923 
10924 	/* get to new offset for the param. */
10925 	req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
10926 	/* now how long will this param be? */
10927 	len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
10928 	req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
10929 	req_in->ph.param_length = htons(len);
10930 	req_in->request_seq = htonl(seq);
10931 	if (number_entries) {
10932 		for (i = 0; i < number_entries; i++) {
10933 			req_in->list_of_streams[i] = htons(list[i]);
10934 		}
10935 	}
10936 	if (SCTP_SIZE32(len) > len) {
10937 		/*-
10938 		 * Need to worry about the pad we may end up adding to the
10939 		 * end. This is easy since the struct is either aligned to 4
10940 		 * bytes or 2 bytes off.
10941 		 */
10942 		req_in->list_of_streams[number_entries] = 0;
10943 	}
10944 	/* now fix the chunk length */
10945 	ch->chunk_length = htons(len + old_len);
10946 	chk->book_size = len + old_len;
10947 	chk->book_size_scale = 0;
10948 	chk->send_size = SCTP_SIZE32(chk->book_size);
10949 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10950 	return;
10951 }
10952 
10953 
10954 void
10955 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
10956     uint32_t seq)
10957 {
10958 	int len, old_len;
10959 	struct sctp_stream_reset_tsn_request *req_tsn;
10960 	struct sctp_chunkhdr *ch;
10961 
10962 	ch = mtod(chk->data, struct sctp_chunkhdr *);
10963 
10964 
10965 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10966 
10967 	/* get to new offset for the param. */
10968 	req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
10969 	/* now how long will this param be? */
10970 	len = sizeof(struct sctp_stream_reset_tsn_request);
10971 	req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
10972 	req_tsn->ph.param_length = htons(len);
10973 	req_tsn->request_seq = htonl(seq);
10974 
10975 	/* now fix the chunk length */
10976 	ch->chunk_length = htons(len + old_len);
10977 	chk->send_size = len + old_len;
10978 	chk->book_size = SCTP_SIZE32(chk->send_size);
10979 	chk->book_size_scale = 0;
10980 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
10981 	return;
10982 }
10983 
10984 void
10985 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
10986     uint32_t resp_seq, uint32_t result)
10987 {
10988 	int len, old_len;
10989 	struct sctp_stream_reset_response *resp;
10990 	struct sctp_chunkhdr *ch;
10991 
10992 	ch = mtod(chk->data, struct sctp_chunkhdr *);
10993 
10994 
10995 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10996 
10997 	/* get to new offset for the param. */
10998 	resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
10999 	/* now how long will this param be? */
11000 	len = sizeof(struct sctp_stream_reset_response);
11001 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11002 	resp->ph.param_length = htons(len);
11003 	resp->response_seq = htonl(resp_seq);
11004 	resp->result = ntohl(result);
11005 
11006 	/* now fix the chunk length */
11007 	ch->chunk_length = htons(len + old_len);
11008 	chk->book_size = len + old_len;
11009 	chk->book_size_scale = 0;
11010 	chk->send_size = SCTP_SIZE32(chk->book_size);
11011 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11012 	return;
11013 
11014 }
11015 
11016 
11017 void
11018 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11019     uint32_t resp_seq, uint32_t result,
11020     uint32_t send_una, uint32_t recv_next)
11021 {
11022 	int len, old_len;
11023 	struct sctp_stream_reset_response_tsn *resp;
11024 	struct sctp_chunkhdr *ch;
11025 
11026 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11027 
11028 
11029 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11030 
11031 	/* get to new offset for the param. */
11032 	resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11033 	/* now how long will this param be? */
11034 	len = sizeof(struct sctp_stream_reset_response_tsn);
11035 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11036 	resp->ph.param_length = htons(len);
11037 	resp->response_seq = htonl(resp_seq);
11038 	resp->result = htonl(result);
11039 	resp->senders_next_tsn = htonl(send_una);
11040 	resp->receivers_next_tsn = htonl(recv_next);
11041 
11042 	/* now fix the chunk length */
11043 	ch->chunk_length = htons(len + old_len);
11044 	chk->book_size = len + old_len;
11045 	chk->send_size = SCTP_SIZE32(chk->book_size);
11046 	chk->book_size_scale = 0;
11047 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11048 	return;
11049 }
11050 
11051 
11052 int
11053 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11054     int number_entries, uint16_t * list,
11055     uint8_t send_out_req, uint32_t resp_seq,
11056     uint8_t send_in_req,
11057     uint8_t send_tsn_req)
11058 {
11059 
11060 	struct sctp_association *asoc;
11061 	struct sctp_tmit_chunk *chk;
11062 	struct sctp_chunkhdr *ch;
11063 	uint32_t seq;
11064 
11065 	asoc = &stcb->asoc;
11066 	if (asoc->stream_reset_outstanding) {
11067 		/*-
11068 		 * Already one pending, must get ACK back to clear the flag.
11069 		 */
11070 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11071 		return (EBUSY);
11072 	}
11073 	if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0)) {
11074 		/* nothing to do */
11075 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11076 		return (EINVAL);
11077 	}
11078 	if (send_tsn_req && (send_out_req || send_in_req)) {
11079 		/* error, can't do that */
11080 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11081 		return (EINVAL);
11082 	}
11083 	sctp_alloc_a_chunk(stcb, chk);
11084 	if (chk == NULL) {
11085 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11086 		return (ENOMEM);
11087 	}
11088 	chk->copy_by_ref = 0;
11089 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11090 	chk->rec.chunk_id.can_take_data = 0;
11091 	chk->asoc = &stcb->asoc;
11092 	chk->book_size = sizeof(struct sctp_chunkhdr);
11093 	chk->send_size = SCTP_SIZE32(chk->book_size);
11094 	chk->book_size_scale = 0;
11095 
11096 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11097 	if (chk->data == NULL) {
11098 		sctp_free_a_chunk(stcb, chk);
11099 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11100 		return (ENOMEM);
11101 	}
11102 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11103 
11104 	/* setup chunk parameters */
11105 	chk->sent = SCTP_DATAGRAM_UNSENT;
11106 	chk->snd_count = 0;
11107 	chk->whoTo = asoc->primary_destination;
11108 	atomic_add_int(&chk->whoTo->ref_count, 1);
11109 
11110 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11111 	ch->chunk_type = SCTP_STREAM_RESET;
11112 	ch->chunk_flags = 0;
11113 	ch->chunk_length = htons(chk->book_size);
11114 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11115 
11116 	seq = stcb->asoc.str_reset_seq_out;
11117 	if (send_out_req) {
11118 		sctp_add_stream_reset_out(chk, number_entries, list,
11119 		    seq, resp_seq, (stcb->asoc.sending_seq - 1));
11120 		asoc->stream_reset_out_is_outstanding = 1;
11121 		seq++;
11122 		asoc->stream_reset_outstanding++;
11123 	}
11124 	if (send_in_req) {
11125 		sctp_add_stream_reset_in(chk, number_entries, list, seq);
11126 		asoc->stream_reset_outstanding++;
11127 	}
11128 	if (send_tsn_req) {
11129 		sctp_add_stream_reset_tsn(chk, seq);
11130 		asoc->stream_reset_outstanding++;
11131 	}
11132 	asoc->str_reset = chk;
11133 
11134 	/* insert the chunk for sending */
11135 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11136 	    chk,
11137 	    sctp_next);
11138 	asoc->ctrl_queue_cnt++;
11139 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11140 	return (0);
11141 }
11142 
11143 void
11144 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11145     struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11146 {
11147 	/*-
11148 	 * Formulate the abort message, and send it back down.
11149 	 */
11150 	struct mbuf *o_pak;
11151 	struct mbuf *mout;
11152 	struct sctp_abort_msg *abm;
11153 	struct ip *iph, *iph_out;
11154 	struct udphdr *udp;
11155 
11156 #ifdef INET6
11157 	struct ip6_hdr *ip6, *ip6_out;
11158 
11159 #endif
11160 	int iphlen_out, len;
11161 
11162 	/* don't respond to ABORT with ABORT */
11163 	if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11164 		if (err_cause)
11165 			sctp_m_freem(err_cause);
11166 		return;
11167 	}
11168 	iph = mtod(m, struct ip *);
11169 	switch (iph->ip_v) {
11170 	case IPVERSION:
11171 		len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11172 		break;
11173 #ifdef INET6
11174 	case IPV6_VERSION >> 4:
11175 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11176 		break;
11177 #endif
11178 	default:
11179 		return;
11180 	}
11181 	if (port) {
11182 		len += sizeof(struct udphdr);
11183 	}
11184 	mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
11185 	if (mout == NULL) {
11186 		if (err_cause)
11187 			sctp_m_freem(err_cause);
11188 		return;
11189 	}
11190 	SCTP_BUF_LEN(mout) = len;
11191 	SCTP_BUF_NEXT(mout) = err_cause;
11192 	iph_out = NULL;
11193 #ifdef INET6
11194 	ip6_out = NULL;
11195 #endif
11196 	switch (iph->ip_v) {
11197 	case IPVERSION:
11198 		iph_out = mtod(mout, struct ip *);
11199 
11200 		/* Fill in the IP header for the ABORT */
11201 		iph_out->ip_v = IPVERSION;
11202 		iph_out->ip_hl = (sizeof(struct ip) / 4);
11203 		iph_out->ip_tos = (u_char)0;
11204 		iph_out->ip_id = 0;
11205 		iph_out->ip_off = 0;
11206 		iph_out->ip_ttl = MAXTTL;
11207 		if (port) {
11208 			iph_out->ip_p = IPPROTO_UDP;
11209 		} else {
11210 			iph_out->ip_p = IPPROTO_SCTP;
11211 		}
11212 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11213 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11214 		/* let IP layer calculate this */
11215 		iph_out->ip_sum = 0;
11216 
11217 		iphlen_out = sizeof(*iph_out);
11218 		abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
11219 		break;
11220 #ifdef INET6
11221 	case IPV6_VERSION >> 4:
11222 		ip6 = (struct ip6_hdr *)iph;
11223 		ip6_out = mtod(mout, struct ip6_hdr *);
11224 
11225 		/* Fill in the IP6 header for the ABORT */
11226 		ip6_out->ip6_flow = ip6->ip6_flow;
11227 		ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
11228 		if (port) {
11229 			ip6_out->ip6_nxt = IPPROTO_UDP;
11230 		} else {
11231 			ip6_out->ip6_nxt = IPPROTO_SCTP;
11232 		}
11233 		ip6_out->ip6_src = ip6->ip6_dst;
11234 		ip6_out->ip6_dst = ip6->ip6_src;
11235 
11236 		iphlen_out = sizeof(*ip6_out);
11237 		abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
11238 		break;
11239 #endif				/* INET6 */
11240 	default:
11241 		/* Currently not supported */
11242 		if (err_cause)
11243 			sctp_m_freem(err_cause);
11244 		sctp_m_freem(mout);
11245 		return;
11246 	}
11247 
11248 	udp = (struct udphdr *)abm;
11249 	if (port) {
11250 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11251 		udp->uh_dport = port;
11252 		/* set udp->uh_ulen later */
11253 		udp->uh_sum = 0;
11254 		iphlen_out += sizeof(struct udphdr);
11255 		abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
11256 	}
11257 	abm->sh.src_port = sh->dest_port;
11258 	abm->sh.dest_port = sh->src_port;
11259 	abm->sh.checksum = 0;
11260 	if (vtag == 0) {
11261 		abm->sh.v_tag = sh->v_tag;
11262 		abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
11263 	} else {
11264 		abm->sh.v_tag = htonl(vtag);
11265 		abm->msg.ch.chunk_flags = 0;
11266 	}
11267 	abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11268 
11269 	if (err_cause) {
11270 		struct mbuf *m_tmp = err_cause;
11271 		int err_len = 0;
11272 
11273 		/* get length of the err_cause chain */
11274 		while (m_tmp != NULL) {
11275 			err_len += SCTP_BUF_LEN(m_tmp);
11276 			m_tmp = SCTP_BUF_NEXT(m_tmp);
11277 		}
11278 		len = SCTP_BUF_LEN(mout) + err_len;
11279 		if (err_len % 4) {
11280 			/* need pad at end of chunk */
11281 			uint32_t cpthis = 0;
11282 			int padlen;
11283 
11284 			padlen = 4 - (len % 4);
11285 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11286 			len += padlen;
11287 		}
11288 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
11289 	} else {
11290 		len = SCTP_BUF_LEN(mout);
11291 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
11292 	}
11293 
11294 	/* add checksum */
11295 	abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
11296 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11297 		/* no mbuf's */
11298 		sctp_m_freem(mout);
11299 		return;
11300 	}
11301 	if (iph_out != NULL) {
11302 		sctp_route_t ro;
11303 		struct sctp_tcb *stcb = NULL;
11304 		int ret;
11305 
11306 		/* zap the stack pointer to the route */
11307 		bzero(&ro, sizeof ro);
11308 		if (port) {
11309 			udp->uh_ulen = htons(len - sizeof(struct ip));
11310 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11311 		}
11312 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
11313 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
11314 		/* set IPv4 length */
11315 		iph_out->ip_len = len;
11316 		/* out it goes */
11317 #ifdef  SCTP_PACKET_LOGGING
11318 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11319 			sctp_packet_log(mout, len);
11320 #endif
11321 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11322 		if (port) {
11323 			SCTP_ENABLE_UDP_CSUM(o_pak);
11324 		}
11325 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
11326 
11327 		/* Free the route if we got one back */
11328 		if (ro.ro_rt)
11329 			RTFREE(ro.ro_rt);
11330 	}
11331 #ifdef INET6
11332 	if (ip6_out != NULL) {
11333 		struct route_in6 ro;
11334 		int ret;
11335 		struct sctp_tcb *stcb = NULL;
11336 		struct ifnet *ifp = NULL;
11337 
11338 		/* zap the stack pointer to the route */
11339 		bzero(&ro, sizeof(ro));
11340 		if (port) {
11341 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11342 		}
11343 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
11344 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
11345 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
11346 #ifdef  SCTP_PACKET_LOGGING
11347 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11348 			sctp_packet_log(mout, len);
11349 #endif
11350 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11351 		if (port) {
11352 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11353 				udp->uh_sum = 0xffff;
11354 			}
11355 		}
11356 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11357 
11358 		/* Free the route if we got one back */
11359 		if (ro.ro_rt)
11360 			RTFREE(ro.ro_rt);
11361 	}
11362 #endif
11363 	SCTP_STAT_INCR(sctps_sendpackets);
11364 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11365 }
11366 
11367 void
11368 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
11369     uint32_t vrf_id, uint16_t port)
11370 {
11371 	struct mbuf *o_pak;
11372 	struct sctphdr *ihdr;
11373 	int retcode;
11374 	struct sctphdr *ohdr;
11375 	struct sctp_chunkhdr *ophdr;
11376 	struct ip *iph;
11377 	struct udphdr *udp = NULL;
11378 	struct mbuf *mout;
11379 
11380 #ifdef INET6
11381 #ifdef SCTP_DEBUG
11382 	struct sockaddr_in6 lsa6, fsa6;
11383 
11384 #endif
11385 #endif
11386 	uint32_t val;
11387 	struct mbuf *at;
11388 	int len;
11389 
11390 	iph = mtod(m, struct ip *);
11391 	ihdr = (struct sctphdr *)((caddr_t)iph + iphlen);
11392 
11393 	SCTP_BUF_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), M_DONTWAIT);
11394 	if (scm == NULL) {
11395 		/* can't send because we can't add a mbuf */
11396 		return;
11397 	}
11398 	ohdr = mtod(scm, struct sctphdr *);
11399 	ohdr->src_port = ihdr->dest_port;
11400 	ohdr->dest_port = ihdr->src_port;
11401 	ohdr->v_tag = vtag;
11402 	ohdr->checksum = 0;
11403 	ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
11404 	ophdr->chunk_type = SCTP_OPERATION_ERROR;
11405 	ophdr->chunk_flags = 0;
11406 	len = 0;
11407 	at = scm;
11408 	while (at) {
11409 		len += SCTP_BUF_LEN(at);
11410 		at = SCTP_BUF_NEXT(at);
11411 	}
11412 	ophdr->chunk_length = htons(len - sizeof(struct sctphdr));
11413 	if (len % 4) {
11414 		/* need padding */
11415 		uint32_t cpthis = 0;
11416 		int padlen;
11417 
11418 		padlen = 4 - (len % 4);
11419 		m_copyback(scm, len, padlen, (caddr_t)&cpthis);
11420 		len += padlen;
11421 	}
11422 	val = sctp_calculate_sum(scm, NULL, 0);
11423 #ifdef INET6
11424 	if (port) {
11425 		mout = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
11426 	} else {
11427 		mout = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
11428 	}
11429 #else
11430 	if (port) {
11431 		mout = sctp_get_mbuf_for_msg(sizeof(struct ip) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
11432 	} else {
11433 		mout = sctp_get_mbuf_for_msg(sizeof(struct ip), 1, M_DONTWAIT, 1, MT_DATA);
11434 	}
11435 #endif
11436 	if (mout == NULL) {
11437 		sctp_m_freem(scm);
11438 		return;
11439 	}
11440 	SCTP_BUF_NEXT(mout) = scm;
11441 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11442 		sctp_m_freem(mout);
11443 		return;
11444 	}
11445 	ohdr->checksum = val;
11446 	switch (iph->ip_v) {
11447 	case IPVERSION:
11448 		{
11449 			/* V4 */
11450 			struct ip *out;
11451 			sctp_route_t ro;
11452 			struct sctp_tcb *stcb = NULL;
11453 
11454 			SCTP_BUF_LEN(mout) = sizeof(struct ip);
11455 			len += sizeof(struct ip);
11456 			if (port) {
11457 				SCTP_BUF_LEN(mout) += sizeof(struct udphdr);
11458 				len += sizeof(struct udphdr);
11459 			}
11460 			bzero(&ro, sizeof ro);
11461 			out = mtod(mout, struct ip *);
11462 			out->ip_v = iph->ip_v;
11463 			out->ip_hl = (sizeof(struct ip) / 4);
11464 			out->ip_tos = iph->ip_tos;
11465 			out->ip_id = iph->ip_id;
11466 			out->ip_off = 0;
11467 			out->ip_ttl = MAXTTL;
11468 			if (port) {
11469 				out->ip_p = IPPROTO_UDP;
11470 			} else {
11471 				out->ip_p = IPPROTO_SCTP;
11472 			}
11473 			out->ip_sum = 0;
11474 			out->ip_src = iph->ip_dst;
11475 			out->ip_dst = iph->ip_src;
11476 			out->ip_len = len;
11477 			if (port) {
11478 				udp = (struct udphdr *)(out + 1);
11479 				udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11480 				udp->uh_dport = port;
11481 				udp->uh_ulen = htons(len - sizeof(struct ip));
11482 				udp->uh_sum = in_pseudo(out->ip_src.s_addr, out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11483 			}
11484 #ifdef  SCTP_PACKET_LOGGING
11485 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11486 				sctp_packet_log(mout, len);
11487 #endif
11488 			SCTP_ATTACH_CHAIN(o_pak, mout, len);
11489 			if (port) {
11490 				SCTP_ENABLE_UDP_CSUM(o_pak);
11491 			}
11492 			SCTP_IP_OUTPUT(retcode, o_pak, &ro, stcb, vrf_id);
11493 
11494 			SCTP_STAT_INCR(sctps_sendpackets);
11495 			SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11496 			/* Free the route if we got one back */
11497 			if (ro.ro_rt)
11498 				RTFREE(ro.ro_rt);
11499 			break;
11500 		}
11501 #ifdef INET6
11502 	case IPV6_VERSION >> 4:
11503 		{
11504 			/* V6 */
11505 			struct route_in6 ro;
11506 			int ret;
11507 			struct sctp_tcb *stcb = NULL;
11508 			struct ifnet *ifp = NULL;
11509 			struct ip6_hdr *out6, *in6;
11510 
11511 			SCTP_BUF_LEN(mout) = sizeof(struct ip6_hdr);
11512 			len += sizeof(struct ip6_hdr);
11513 			bzero(&ro, sizeof ro);
11514 			if (port) {
11515 				SCTP_BUF_LEN(mout) += sizeof(struct udphdr);
11516 				len += sizeof(struct udphdr);
11517 			}
11518 			in6 = mtod(m, struct ip6_hdr *);
11519 			out6 = mtod(mout, struct ip6_hdr *);
11520 			out6->ip6_flow = in6->ip6_flow;
11521 			out6->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
11522 			if (port) {
11523 				out6->ip6_nxt = IPPROTO_UDP;
11524 			} else {
11525 				out6->ip6_nxt = IPPROTO_SCTP;
11526 			}
11527 			out6->ip6_src = in6->ip6_dst;
11528 			out6->ip6_dst = in6->ip6_src;
11529 			out6->ip6_plen = len - sizeof(struct ip6_hdr);
11530 			if (port) {
11531 				udp = (struct udphdr *)(out6 + 1);
11532 				udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11533 				udp->uh_dport = port;
11534 				udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11535 				udp->uh_sum = 0;
11536 			}
11537 #ifdef SCTP_DEBUG
11538 			bzero(&lsa6, sizeof(lsa6));
11539 			lsa6.sin6_len = sizeof(lsa6);
11540 			lsa6.sin6_family = AF_INET6;
11541 			lsa6.sin6_addr = out6->ip6_src;
11542 			bzero(&fsa6, sizeof(fsa6));
11543 			fsa6.sin6_len = sizeof(fsa6);
11544 			fsa6.sin6_family = AF_INET6;
11545 			fsa6.sin6_addr = out6->ip6_dst;
11546 #endif
11547 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_operr_to calling ipv6 output:\n");
11548 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "src: ");
11549 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&lsa6);
11550 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "dst ");
11551 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&fsa6);
11552 
11553 #ifdef  SCTP_PACKET_LOGGING
11554 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11555 				sctp_packet_log(mout, len);
11556 #endif
11557 			SCTP_ATTACH_CHAIN(o_pak, mout, len);
11558 			if (port) {
11559 				if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11560 					udp->uh_sum = 0xffff;
11561 				}
11562 			}
11563 			SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11564 
11565 			SCTP_STAT_INCR(sctps_sendpackets);
11566 			SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11567 			/* Free the route if we got one back */
11568 			if (ro.ro_rt)
11569 				RTFREE(ro.ro_rt);
11570 			break;
11571 		}
11572 #endif				/* INET6 */
11573 	default:
11574 		/* TSNH */
11575 		break;
11576 	}
11577 }
11578 
11579 static struct mbuf *
11580 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
11581     struct uio *uio,
11582     struct sctp_sndrcvinfo *srcv,
11583     int max_send_len,
11584     int user_marks_eor,
11585     int *error,
11586     uint32_t * sndout,
11587     struct mbuf **new_tail)
11588 {
11589 	struct mbuf *m;
11590 
11591 	m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
11592 	    (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
11593 	if (m == NULL) {
11594 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11595 		*error = ENOMEM;
11596 	} else {
11597 		*sndout = m_length(m, NULL);
11598 		*new_tail = m_last(m);
11599 	}
11600 	return (m);
11601 }
11602 
11603 static int
11604 sctp_copy_one(struct sctp_stream_queue_pending *sp,
11605     struct uio *uio,
11606     int resv_upfront)
11607 {
11608 	int left;
11609 
11610 	left = sp->length;
11611 	sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
11612 	    resv_upfront, 0);
11613 	if (sp->data == NULL) {
11614 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11615 		return (ENOMEM);
11616 	}
11617 	sp->tail_mbuf = m_last(sp->data);
11618 	return (0);
11619 }
11620 
11621 
11622 
11623 static struct sctp_stream_queue_pending *
11624 sctp_copy_it_in(struct sctp_tcb *stcb,
11625     struct sctp_association *asoc,
11626     struct sctp_sndrcvinfo *srcv,
11627     struct uio *uio,
11628     struct sctp_nets *net,
11629     int max_send_len,
11630     int user_marks_eor,
11631     int *error,
11632     int non_blocking)
11633 {
11634 	/*-
11635 	 * This routine must be very careful in its work. Protocol
11636 	 * processing is up and running so care must be taken to spl...()
11637 	 * when you need to do something that may effect the stcb/asoc. The
11638 	 * sb is locked however. When data is copied the protocol processing
11639 	 * should be enabled since this is a slower operation...
11640 	 */
11641 	struct sctp_stream_queue_pending *sp = NULL;
11642 	int resv_in_first;
11643 
11644 	*error = 0;
11645 	/* Now can we send this? */
11646 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
11647 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
11648 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
11649 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
11650 		/* got data while shutting down */
11651 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
11652 		*error = ECONNRESET;
11653 		goto out_now;
11654 	}
11655 	sctp_alloc_a_strmoq(stcb, sp);
11656 	if (sp == NULL) {
11657 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11658 		*error = ENOMEM;
11659 		goto out_now;
11660 	}
11661 	sp->act_flags = 0;
11662 	sp->sender_all_done = 0;
11663 	sp->sinfo_flags = srcv->sinfo_flags;
11664 	sp->timetolive = srcv->sinfo_timetolive;
11665 	sp->ppid = srcv->sinfo_ppid;
11666 	sp->context = srcv->sinfo_context;
11667 	sp->strseq = 0;
11668 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
11669 
11670 	sp->stream = srcv->sinfo_stream;
11671 	sp->length = min(uio->uio_resid, max_send_len);
11672 	if ((sp->length == (uint32_t) uio->uio_resid) &&
11673 	    ((user_marks_eor == 0) ||
11674 	    (srcv->sinfo_flags & SCTP_EOF) ||
11675 	    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
11676 		sp->msg_is_complete = 1;
11677 	} else {
11678 		sp->msg_is_complete = 0;
11679 	}
11680 	sp->sender_all_done = 0;
11681 	sp->some_taken = 0;
11682 	sp->put_last_out = 0;
11683 	resv_in_first = sizeof(struct sctp_data_chunk);
11684 	sp->data = sp->tail_mbuf = NULL;
11685 	if (sp->length == 0) {
11686 		*error = 0;
11687 		goto skip_copy;
11688 	}
11689 	*error = sctp_copy_one(sp, uio, resv_in_first);
11690 skip_copy:
11691 	if (*error) {
11692 		sctp_free_a_strmoq(stcb, sp);
11693 		sp = NULL;
11694 	} else {
11695 		if (sp->sinfo_flags & SCTP_ADDR_OVER) {
11696 			sp->net = net;
11697 			sp->addr_over = 1;
11698 		} else {
11699 			sp->net = asoc->primary_destination;
11700 			sp->addr_over = 0;
11701 		}
11702 		atomic_add_int(&sp->net->ref_count, 1);
11703 		sctp_set_prsctp_policy(stcb, sp);
11704 	}
11705 out_now:
11706 	return (sp);
11707 }
11708 
11709 
11710 int
11711 sctp_sosend(struct socket *so,
11712     struct sockaddr *addr,
11713     struct uio *uio,
11714     struct mbuf *top,
11715     struct mbuf *control,
11716     int flags,
11717     struct thread *p
11718 )
11719 {
11720 	struct sctp_inpcb *inp;
11721 	int error, use_rcvinfo = 0;
11722 	struct sctp_sndrcvinfo srcv;
11723 	struct sockaddr *addr_to_use;
11724 
11725 #ifdef INET6
11726 	struct sockaddr_in sin;
11727 
11728 #endif
11729 
11730 	inp = (struct sctp_inpcb *)so->so_pcb;
11731 	if (control) {
11732 		/* process cmsg snd/rcv info (maybe a assoc-id) */
11733 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
11734 		    sizeof(srcv))) {
11735 			/* got one */
11736 			use_rcvinfo = 1;
11737 		}
11738 	}
11739 	addr_to_use = addr;
11740 #if defined(INET6)  && !defined(__Userspace__)	/* TODO port in6_sin6_2_sin */
11741 	if ((addr) && (addr->sa_family == AF_INET6)) {
11742 		struct sockaddr_in6 *sin6;
11743 
11744 		sin6 = (struct sockaddr_in6 *)addr;
11745 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
11746 			in6_sin6_2_sin(&sin, sin6);
11747 			addr_to_use = (struct sockaddr *)&sin;
11748 		}
11749 	}
11750 #endif
11751 	error = sctp_lower_sosend(so, addr_to_use, uio, top,
11752 	    control,
11753 	    flags,
11754 	    use_rcvinfo, &srcv
11755 	    ,p
11756 	    );
11757 	return (error);
11758 }
11759 
11760 
11761 int
11762 sctp_lower_sosend(struct socket *so,
11763     struct sockaddr *addr,
11764     struct uio *uio,
11765     struct mbuf *i_pak,
11766     struct mbuf *control,
11767     int flags,
11768     int use_rcvinfo,
11769     struct sctp_sndrcvinfo *srcv
11770     ,
11771     struct thread *p
11772 )
11773 {
11774 	unsigned int sndlen = 0, max_len;
11775 	int error, len;
11776 	struct mbuf *top = NULL;
11777 	int queue_only = 0, queue_only_for_init = 0;
11778 	int free_cnt_applied = 0;
11779 	int un_sent = 0;
11780 	int now_filled = 0;
11781 	unsigned int inqueue_bytes = 0;
11782 	struct sctp_block_entry be;
11783 	struct sctp_inpcb *inp;
11784 	struct sctp_tcb *stcb = NULL;
11785 	struct timeval now;
11786 	struct sctp_nets *net;
11787 	struct sctp_association *asoc;
11788 	struct sctp_inpcb *t_inp;
11789 	int user_marks_eor;
11790 	int create_lock_applied = 0;
11791 	int nagle_applies = 0;
11792 	int some_on_control = 0;
11793 	int got_all_of_the_send = 0;
11794 	int hold_tcblock = 0;
11795 	int non_blocking = 0;
11796 	int temp_flags = 0;
11797 	uint32_t local_add_more, local_soresv = 0;
11798 
11799 	error = 0;
11800 	net = NULL;
11801 	stcb = NULL;
11802 	asoc = NULL;
11803 
11804 	t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
11805 	if (inp == NULL) {
11806 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11807 		error = EFAULT;
11808 		if (i_pak) {
11809 			SCTP_RELEASE_PKT(i_pak);
11810 		}
11811 		return (error);
11812 	}
11813 	if ((uio == NULL) && (i_pak == NULL)) {
11814 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11815 		return (EINVAL);
11816 	}
11817 	user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
11818 	atomic_add_int(&inp->total_sends, 1);
11819 	if (uio) {
11820 		if (uio->uio_resid < 0) {
11821 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11822 			return (EINVAL);
11823 		}
11824 		sndlen = uio->uio_resid;
11825 	} else {
11826 		top = SCTP_HEADER_TO_CHAIN(i_pak);
11827 		sndlen = SCTP_HEADER_LEN(i_pak);
11828 	}
11829 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
11830 	    addr,
11831 	    sndlen);
11832 	/*-
11833          * Pre-screen address, if one is given the sin-len
11834          * must be set correctly!
11835          */
11836 	if (addr) {
11837 		if ((addr->sa_family == AF_INET) &&
11838 		    (addr->sa_len != sizeof(struct sockaddr_in))) {
11839 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11840 			error = EINVAL;
11841 			goto out_unlocked;
11842 		} else if ((addr->sa_family == AF_INET6) &&
11843 		    (addr->sa_len != sizeof(struct sockaddr_in6))) {
11844 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11845 			error = EINVAL;
11846 			goto out_unlocked;
11847 		}
11848 	}
11849 	hold_tcblock = 0;
11850 
11851 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
11852 	    (inp->sctp_socket->so_qlimit)) {
11853 		/* The listener can NOT send */
11854 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11855 		error = EFAULT;
11856 		goto out_unlocked;
11857 	}
11858 	if ((use_rcvinfo) && srcv) {
11859 		if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
11860 		    PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
11861 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11862 			error = EINVAL;
11863 			goto out_unlocked;
11864 		}
11865 		if (srcv->sinfo_flags)
11866 			SCTP_STAT_INCR(sctps_sends_with_flags);
11867 
11868 		if (srcv->sinfo_flags & SCTP_SENDALL) {
11869 			/* its a sendall */
11870 			error = sctp_sendall(inp, uio, top, srcv);
11871 			top = NULL;
11872 			goto out_unlocked;
11873 		}
11874 	}
11875 	/* now we must find the assoc */
11876 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
11877 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
11878 		SCTP_INP_RLOCK(inp);
11879 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
11880 		if (stcb == NULL) {
11881 			SCTP_INP_RUNLOCK(inp);
11882 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
11883 			error = ENOTCONN;
11884 			goto out_unlocked;
11885 		}
11886 		hold_tcblock = 0;
11887 		SCTP_INP_RUNLOCK(inp);
11888 		if (addr) {
11889 			/* Must locate the net structure if addr given */
11890 			net = sctp_findnet(stcb, addr);
11891 			if (net) {
11892 				/* validate port was 0 or correct */
11893 				struct sockaddr_in *sin;
11894 
11895 				sin = (struct sockaddr_in *)addr;
11896 				if ((sin->sin_port != 0) &&
11897 				    (sin->sin_port != stcb->rport)) {
11898 					net = NULL;
11899 				}
11900 			}
11901 			temp_flags |= SCTP_ADDR_OVER;
11902 		} else
11903 			net = stcb->asoc.primary_destination;
11904 		if (addr && (net == NULL)) {
11905 			/* Could not find address, was it legal */
11906 			if (addr->sa_family == AF_INET) {
11907 				struct sockaddr_in *sin;
11908 
11909 				sin = (struct sockaddr_in *)addr;
11910 				if (sin->sin_addr.s_addr == 0) {
11911 					if ((sin->sin_port == 0) ||
11912 					    (sin->sin_port == stcb->rport)) {
11913 						net = stcb->asoc.primary_destination;
11914 					}
11915 				}
11916 			} else {
11917 				struct sockaddr_in6 *sin6;
11918 
11919 				sin6 = (struct sockaddr_in6 *)addr;
11920 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
11921 					if ((sin6->sin6_port == 0) ||
11922 					    (sin6->sin6_port == stcb->rport)) {
11923 						net = stcb->asoc.primary_destination;
11924 					}
11925 				}
11926 			}
11927 		}
11928 		if (net == NULL) {
11929 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11930 			error = EINVAL;
11931 			goto out_unlocked;
11932 		}
11933 	} else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
11934 		stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
11935 		if (stcb) {
11936 			if (addr)
11937 				/*
11938 				 * Must locate the net structure if addr
11939 				 * given
11940 				 */
11941 				net = sctp_findnet(stcb, addr);
11942 			else
11943 				net = stcb->asoc.primary_destination;
11944 			if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
11945 			    ((net == NULL) || (addr == NULL))) {
11946 				struct sockaddr_in *sin;
11947 
11948 				if (addr == NULL) {
11949 					SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11950 					error = EINVAL;
11951 					goto out_unlocked;
11952 				}
11953 				sin = (struct sockaddr_in *)addr;
11954 				/* Validate port is 0 or correct */
11955 				if ((sin->sin_port != 0) &&
11956 				    (sin->sin_port != stcb->rport)) {
11957 					net = NULL;
11958 				}
11959 			}
11960 		}
11961 		hold_tcblock = 0;
11962 	} else if (addr) {
11963 		/*-
11964 		 * Since we did not use findep we must
11965 		 * increment it, and if we don't find a tcb
11966 		 * decrement it.
11967 		 */
11968 		SCTP_INP_WLOCK(inp);
11969 		SCTP_INP_INCR_REF(inp);
11970 		SCTP_INP_WUNLOCK(inp);
11971 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
11972 		if (stcb == NULL) {
11973 			SCTP_INP_WLOCK(inp);
11974 			SCTP_INP_DECR_REF(inp);
11975 			SCTP_INP_WUNLOCK(inp);
11976 		} else {
11977 			hold_tcblock = 1;
11978 		}
11979 	}
11980 	if ((stcb == NULL) && (addr)) {
11981 		/* Possible implicit send? */
11982 		SCTP_ASOC_CREATE_LOCK(inp);
11983 		create_lock_applied = 1;
11984 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
11985 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
11986 			/* Should I really unlock ? */
11987 			SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11988 			error = EFAULT;
11989 			goto out_unlocked;
11990 
11991 		}
11992 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
11993 		    (addr->sa_family == AF_INET6)) {
11994 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11995 			error = EINVAL;
11996 			goto out_unlocked;
11997 		}
11998 		SCTP_INP_WLOCK(inp);
11999 		SCTP_INP_INCR_REF(inp);
12000 		SCTP_INP_WUNLOCK(inp);
12001 		/* With the lock applied look again */
12002 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12003 		if (stcb == NULL) {
12004 			SCTP_INP_WLOCK(inp);
12005 			SCTP_INP_DECR_REF(inp);
12006 			SCTP_INP_WUNLOCK(inp);
12007 		} else {
12008 			hold_tcblock = 1;
12009 		}
12010 		if (t_inp != inp) {
12011 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12012 			error = ENOTCONN;
12013 			goto out_unlocked;
12014 		}
12015 	}
12016 	if (stcb == NULL) {
12017 		if (addr == NULL) {
12018 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12019 			error = ENOENT;
12020 			goto out_unlocked;
12021 		} else {
12022 			/*
12023 			 * UDP style, we must go ahead and start the INIT
12024 			 * process
12025 			 */
12026 			uint32_t vrf_id;
12027 
12028 			if ((use_rcvinfo) && (srcv) &&
12029 			    ((srcv->sinfo_flags & SCTP_ABORT) ||
12030 			    ((srcv->sinfo_flags & SCTP_EOF) &&
12031 			    (sndlen == 0)))) {
12032 				/*-
12033 				 * User asks to abort a non-existant assoc,
12034 				 * or EOF a non-existant assoc with no data
12035 				 */
12036 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12037 				error = ENOENT;
12038 				goto out_unlocked;
12039 			}
12040 			/* get an asoc/stcb struct */
12041 			vrf_id = inp->def_vrf_id;
12042 #ifdef INVARIANTS
12043 			if (create_lock_applied == 0) {
12044 				panic("Error, should hold create lock and I don't?");
12045 			}
12046 #endif
12047 			stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id,
12048 			    p
12049 			    );
12050 			if (stcb == NULL) {
12051 				/* Error is setup for us in the call */
12052 				goto out_unlocked;
12053 			}
12054 			if (create_lock_applied) {
12055 				SCTP_ASOC_CREATE_UNLOCK(inp);
12056 				create_lock_applied = 0;
12057 			} else {
12058 				SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12059 			}
12060 			/*
12061 			 * Turn on queue only flag to prevent data from
12062 			 * being sent
12063 			 */
12064 			queue_only = 1;
12065 			asoc = &stcb->asoc;
12066 			SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12067 			(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12068 
12069 			/* initialize authentication params for the assoc */
12070 			sctp_initialize_auth_params(inp, stcb);
12071 
12072 			if (control) {
12073 				/*
12074 				 * see if a init structure exists in cmsg
12075 				 * headers
12076 				 */
12077 				struct sctp_initmsg initm;
12078 				int i;
12079 
12080 				if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
12081 				    sizeof(initm))) {
12082 					/*
12083 					 * we have an INIT override of the
12084 					 * default
12085 					 */
12086 					if (initm.sinit_max_attempts)
12087 						asoc->max_init_times = initm.sinit_max_attempts;
12088 					if (initm.sinit_num_ostreams)
12089 						asoc->pre_open_streams = initm.sinit_num_ostreams;
12090 					if (initm.sinit_max_instreams)
12091 						asoc->max_inbound_streams = initm.sinit_max_instreams;
12092 					if (initm.sinit_max_init_timeo)
12093 						asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
12094 					if (asoc->streamoutcnt < asoc->pre_open_streams) {
12095 						/* Default is NOT correct */
12096 						SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
12097 						    asoc->streamoutcnt, asoc->pre_open_streams);
12098 						/*
12099 						 * What happens if this
12100 						 * fails? we panic ...
12101 						 */
12102 						{
12103 							struct sctp_stream_out *tmp_str;
12104 							int had_lock = 0;
12105 
12106 							if (hold_tcblock) {
12107 								had_lock = 1;
12108 								SCTP_TCB_UNLOCK(stcb);
12109 							}
12110 							SCTP_MALLOC(tmp_str,
12111 							    struct sctp_stream_out *,
12112 							    (asoc->pre_open_streams *
12113 							    sizeof(struct sctp_stream_out)),
12114 							    SCTP_M_STRMO);
12115 							if (had_lock) {
12116 								SCTP_TCB_LOCK(stcb);
12117 							}
12118 							if (tmp_str != NULL) {
12119 								SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
12120 								asoc->strmout = tmp_str;
12121 								asoc->streamoutcnt = asoc->pre_open_streams;
12122 							} else {
12123 								asoc->pre_open_streams = asoc->streamoutcnt;
12124 							}
12125 						}
12126 						for (i = 0; i < asoc->streamoutcnt; i++) {
12127 							/*-
12128 						         * inbound side must be set
12129 						         * to 0xffff, also NOTE when
12130 						         * we get the INIT-ACK back
12131 						         * (for INIT sender) we MUST
12132 						         * reduce the count
12133 						         * (streamoutcnt) but first
12134 						         * check if we sent to any
12135 						         * of the upper streams that
12136 						         * were dropped (if some
12137 						         * were). Those that were
12138 						         * dropped must be notified
12139 						         * to the upper layer as
12140 						         * failed to send.
12141 						         */
12142 							asoc->strmout[i].next_sequence_sent = 0x0;
12143 							TAILQ_INIT(&asoc->strmout[i].outqueue);
12144 							asoc->strmout[i].stream_no = i;
12145 							asoc->strmout[i].last_msg_incomplete = 0;
12146 							asoc->strmout[i].next_spoke.tqe_next = 0;
12147 							asoc->strmout[i].next_spoke.tqe_prev = 0;
12148 						}
12149 					}
12150 				}
12151 			}
12152 			hold_tcblock = 1;
12153 			/* out with the INIT */
12154 			queue_only_for_init = 1;
12155 			/*-
12156 		         * we may want to dig in after this call and adjust the MTU
12157 		         * value. It defaulted to 1500 (constant) but the ro
12158 		         * structure may now have an update and thus we may need to
12159 		         * change it BEFORE we append the message.
12160 		         */
12161 			net = stcb->asoc.primary_destination;
12162 			asoc = &stcb->asoc;
12163 		}
12164 	}
12165 	if ((SCTP_SO_IS_NBIO(so)
12166 	    || (flags & MSG_NBIO)
12167 	    )) {
12168 		non_blocking = 1;
12169 	}
12170 	asoc = &stcb->asoc;
12171 
12172 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12173 		if (sndlen > asoc->smallest_mtu) {
12174 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12175 			error = EMSGSIZE;
12176 			goto out_unlocked;
12177 		}
12178 	}
12179 	/* would we block? */
12180 	if (non_blocking) {
12181 		if (hold_tcblock == 0) {
12182 			SCTP_TCB_LOCK(stcb);
12183 			hold_tcblock = 1;
12184 		}
12185 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12186 		if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12187 		    (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12188 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
12189 			if (sndlen > SCTP_SB_LIMIT_SND(so))
12190 				error = EMSGSIZE;
12191 			else
12192 				error = EWOULDBLOCK;
12193 			goto out_unlocked;
12194 		}
12195 		stcb->asoc.sb_send_resv += sndlen;
12196 		SCTP_TCB_UNLOCK(stcb);
12197 		hold_tcblock = 0;
12198 	} else {
12199 		atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
12200 	}
12201 	local_soresv = sndlen;
12202 	/* Keep the stcb from being freed under our feet */
12203 	if (free_cnt_applied) {
12204 #ifdef INVARIANTS
12205 		panic("refcnt already incremented");
12206 #else
12207 		printf("refcnt:1 already incremented?\n");
12208 #endif
12209 	} else {
12210 		atomic_add_int(&stcb->asoc.refcnt, 1);
12211 		free_cnt_applied = 1;
12212 	}
12213 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12214 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12215 		error = ECONNRESET;
12216 		goto out_unlocked;
12217 	}
12218 	if (create_lock_applied) {
12219 		SCTP_ASOC_CREATE_UNLOCK(inp);
12220 		create_lock_applied = 0;
12221 	}
12222 	if (asoc->stream_reset_outstanding) {
12223 		/*
12224 		 * Can't queue any data while stream reset is underway.
12225 		 */
12226 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
12227 		error = EAGAIN;
12228 		goto out_unlocked;
12229 	}
12230 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12231 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12232 		queue_only = 1;
12233 	}
12234 	if ((use_rcvinfo == 0) || (srcv == NULL)) {
12235 		/* Grab the default stuff from the asoc */
12236 		srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
12237 	}
12238 	/* we are now done with all control */
12239 	if (control) {
12240 		sctp_m_freem(control);
12241 		control = NULL;
12242 	}
12243 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12244 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12245 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12246 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12247 		if ((use_rcvinfo) &&
12248 		    (srcv->sinfo_flags & SCTP_ABORT)) {
12249 			;
12250 		} else {
12251 			SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12252 			error = ECONNRESET;
12253 			goto out_unlocked;
12254 		}
12255 	}
12256 	/* Ok, we will attempt a msgsnd :> */
12257 	if (p) {
12258 		p->td_ru.ru_msgsnd++;
12259 	}
12260 	if (stcb) {
12261 		if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
12262 			net = stcb->asoc.primary_destination;
12263 		}
12264 	}
12265 	if (net == NULL) {
12266 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12267 		error = EINVAL;
12268 		goto out_unlocked;
12269 	}
12270 	if ((net->flight_size > net->cwnd) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12271 		/*-
12272 		 * CMT: Added check for CMT above. net above is the primary
12273 		 * dest. If CMT is ON, sender should always attempt to send
12274 		 * with the output routine sctp_fill_outqueue() that loops
12275 		 * through all destination addresses. Therefore, if CMT is
12276 		 * ON, queue_only is NOT set to 1 here, so that
12277 		 * sctp_chunk_output() can be called below.
12278 		 */
12279 		queue_only = 1;
12280 
12281 	} else if (asoc->ifp_had_enobuf) {
12282 		SCTP_STAT_INCR(sctps_ifnomemqueued);
12283 		if (net->flight_size > (net->mtu * 2))
12284 			queue_only = 1;
12285 		asoc->ifp_had_enobuf = 0;
12286 	} else {
12287 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12288 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12289 	}
12290 	/* Are we aborting? */
12291 	if (srcv->sinfo_flags & SCTP_ABORT) {
12292 		struct mbuf *mm;
12293 		int tot_demand, tot_out = 0, max_out;
12294 
12295 		SCTP_STAT_INCR(sctps_sends_with_abort);
12296 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12297 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12298 			/* It has to be up before we abort */
12299 			/* how big is the user initiated abort? */
12300 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12301 			error = EINVAL;
12302 			goto out;
12303 		}
12304 		if (hold_tcblock) {
12305 			SCTP_TCB_UNLOCK(stcb);
12306 			hold_tcblock = 0;
12307 		}
12308 		if (top) {
12309 			struct mbuf *cntm = NULL;
12310 
12311 			mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
12312 			if (sndlen != 0) {
12313 				cntm = top;
12314 				while (cntm) {
12315 					tot_out += SCTP_BUF_LEN(cntm);
12316 					cntm = SCTP_BUF_NEXT(cntm);
12317 				}
12318 			}
12319 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12320 		} else {
12321 			/* Must fit in a MTU */
12322 			tot_out = sndlen;
12323 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12324 			if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12325 				/* To big */
12326 				SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12327 				error = EMSGSIZE;
12328 				goto out;
12329 			}
12330 			mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
12331 		}
12332 		if (mm == NULL) {
12333 			SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12334 			error = ENOMEM;
12335 			goto out;
12336 		}
12337 		max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12338 		max_out -= sizeof(struct sctp_abort_msg);
12339 		if (tot_out > max_out) {
12340 			tot_out = max_out;
12341 		}
12342 		if (mm) {
12343 			struct sctp_paramhdr *ph;
12344 
12345 			/* now move forward the data pointer */
12346 			ph = mtod(mm, struct sctp_paramhdr *);
12347 			ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12348 			ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
12349 			ph++;
12350 			SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
12351 			if (top == NULL) {
12352 				error = uiomove((caddr_t)ph, (int)tot_out, uio);
12353 				if (error) {
12354 					/*-
12355 				         * Here if we can't get his data we
12356 				         * still abort we just don't get to
12357 				         * send the users note :-0
12358 				         */
12359 					sctp_m_freem(mm);
12360 					mm = NULL;
12361 				}
12362 			} else {
12363 				if (sndlen != 0) {
12364 					SCTP_BUF_NEXT(mm) = top;
12365 				}
12366 			}
12367 		}
12368 		if (hold_tcblock == 0) {
12369 			SCTP_TCB_LOCK(stcb);
12370 			hold_tcblock = 1;
12371 		}
12372 		atomic_add_int(&stcb->asoc.refcnt, -1);
12373 		free_cnt_applied = 0;
12374 		/* release this lock, otherwise we hang on ourselves */
12375 		sctp_abort_an_association(stcb->sctp_ep, stcb,
12376 		    SCTP_RESPONSE_TO_USER_REQ,
12377 		    mm, SCTP_SO_LOCKED);
12378 		/* now relock the stcb so everything is sane */
12379 		hold_tcblock = 0;
12380 		stcb = NULL;
12381 		/*
12382 		 * In this case top is already chained to mm avoid double
12383 		 * free, since we free it below if top != NULL and driver
12384 		 * would free it after sending the packet out
12385 		 */
12386 		if (sndlen != 0) {
12387 			top = NULL;
12388 		}
12389 		goto out_unlocked;
12390 	}
12391 	/* Calculate the maximum we can send */
12392 	inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12393 	if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12394 		if (non_blocking) {
12395 			/* we already checked for non-blocking above. */
12396 			max_len = sndlen;
12397 		} else {
12398 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12399 		}
12400 	} else {
12401 		max_len = 0;
12402 	}
12403 	if (hold_tcblock) {
12404 		SCTP_TCB_UNLOCK(stcb);
12405 		hold_tcblock = 0;
12406 	}
12407 	/* Is the stream no. valid? */
12408 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
12409 		/* Invalid stream number */
12410 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12411 		error = EINVAL;
12412 		goto out_unlocked;
12413 	}
12414 	if (asoc->strmout == NULL) {
12415 		/* huh? software error */
12416 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12417 		error = EFAULT;
12418 		goto out_unlocked;
12419 	}
12420 	/* Unless E_EOR mode is on, we must make a send FIT in one call. */
12421 	if ((user_marks_eor == 0) &&
12422 	    (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
12423 		/* It will NEVER fit */
12424 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12425 		error = EMSGSIZE;
12426 		goto out_unlocked;
12427 	}
12428 	if ((uio == NULL) && user_marks_eor) {
12429 		/*-
12430 		 * We do not support eeor mode for
12431 		 * sending with mbuf chains (like sendfile).
12432 		 */
12433 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12434 		error = EINVAL;
12435 		goto out_unlocked;
12436 	}
12437 	if (user_marks_eor) {
12438 		local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
12439 	} else {
12440 		/*-
12441 		 * For non-eeor the whole message must fit in
12442 		 * the socket send buffer.
12443 		 */
12444 		local_add_more = sndlen;
12445 	}
12446 	len = 0;
12447 	if (non_blocking) {
12448 		goto skip_preblock;
12449 	}
12450 	if (((max_len <= local_add_more) &&
12451 	    (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
12452 	    (max_len == 0) ||
12453 	    ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {	/* if */
12454 		/* No room right now ! */
12455 		SOCKBUF_LOCK(&so->so_snd);
12456 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12457 		while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
12458 		    ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) /* while */ ) {
12459 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%d <(inq:%d + %d) || (%d+%d > %d)\n",
12460 			    SCTP_SB_LIMIT_SND(so),
12461 			    inqueue_bytes,
12462 			    local_add_more,
12463 			    stcb->asoc.stream_queue_cnt,
12464 			    stcb->asoc.chunks_on_out_queue,
12465 			    SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
12466 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12467 				sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA,
12468 				    so, asoc, sndlen);
12469 			}
12470 			be.error = 0;
12471 			stcb->block_entry = &be;
12472 			error = sbwait(&so->so_snd);
12473 			stcb->block_entry = NULL;
12474 			if (error || so->so_error || be.error) {
12475 				if (error == 0) {
12476 					if (so->so_error)
12477 						error = so->so_error;
12478 					if (be.error) {
12479 						error = be.error;
12480 					}
12481 				}
12482 				SOCKBUF_UNLOCK(&so->so_snd);
12483 				goto out_unlocked;
12484 			}
12485 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12486 				sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12487 				    so, asoc, stcb->asoc.total_output_queue_size);
12488 			}
12489 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12490 				goto out_unlocked;
12491 			}
12492 			inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12493 		}
12494 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12495 		if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12496 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12497 		} else {
12498 			max_len = 0;
12499 		}
12500 		SOCKBUF_UNLOCK(&so->so_snd);
12501 	}
12502 skip_preblock:
12503 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12504 		goto out_unlocked;
12505 	}
12506 	atomic_add_int(&stcb->total_sends, 1);
12507 	/*
12508 	 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
12509 	 * case NOTE: uio will be null when top/mbuf is passed
12510 	 */
12511 	if (sndlen == 0) {
12512 		if (srcv->sinfo_flags & SCTP_EOF) {
12513 			got_all_of_the_send = 1;
12514 			goto dataless_eof;
12515 		} else {
12516 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12517 			error = EINVAL;
12518 			goto out;
12519 		}
12520 	}
12521 	if (top == NULL) {
12522 		struct sctp_stream_queue_pending *sp;
12523 		struct sctp_stream_out *strm;
12524 		uint32_t sndout, initial_out;
12525 
12526 		initial_out = uio->uio_resid;
12527 
12528 		SCTP_TCB_SEND_LOCK(stcb);
12529 		if ((asoc->stream_locked) &&
12530 		    (asoc->stream_locked_on != srcv->sinfo_stream)) {
12531 			SCTP_TCB_SEND_UNLOCK(stcb);
12532 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12533 			error = EINVAL;
12534 			goto out;
12535 		}
12536 		SCTP_TCB_SEND_UNLOCK(stcb);
12537 
12538 		strm = &stcb->asoc.strmout[srcv->sinfo_stream];
12539 		if (strm->last_msg_incomplete == 0) {
12540 	do_a_copy_in:
12541 			sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
12542 			if ((sp == NULL) || (error)) {
12543 				goto out;
12544 			}
12545 			SCTP_TCB_SEND_LOCK(stcb);
12546 			if (sp->msg_is_complete) {
12547 				strm->last_msg_incomplete = 0;
12548 				asoc->stream_locked = 0;
12549 			} else {
12550 				/*
12551 				 * Just got locked to this guy in case of an
12552 				 * interrupt.
12553 				 */
12554 				strm->last_msg_incomplete = 1;
12555 				asoc->stream_locked = 1;
12556 				asoc->stream_locked_on = srcv->sinfo_stream;
12557 				sp->sender_all_done = 0;
12558 			}
12559 			sctp_snd_sb_alloc(stcb, sp->length);
12560 			atomic_add_int(&asoc->stream_queue_cnt, 1);
12561 			if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
12562 				sp->strseq = strm->next_sequence_sent;
12563 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
12564 					sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
12565 					    (uintptr_t) stcb, sp->length,
12566 					    (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
12567 				}
12568 				strm->next_sequence_sent++;
12569 			} else {
12570 				SCTP_STAT_INCR(sctps_sends_with_unord);
12571 			}
12572 			TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
12573 			if ((strm->next_spoke.tqe_next == NULL) &&
12574 			    (strm->next_spoke.tqe_prev == NULL)) {
12575 				/* Not on wheel, insert */
12576 				sctp_insert_on_wheel(stcb, asoc, strm, 1);
12577 			}
12578 			SCTP_TCB_SEND_UNLOCK(stcb);
12579 		} else {
12580 			SCTP_TCB_SEND_LOCK(stcb);
12581 			sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
12582 			SCTP_TCB_SEND_UNLOCK(stcb);
12583 			if (sp == NULL) {
12584 				/* ???? Huh ??? last msg is gone */
12585 #ifdef INVARIANTS
12586 				panic("Warning: Last msg marked incomplete, yet nothing left?");
12587 #else
12588 				SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
12589 				strm->last_msg_incomplete = 0;
12590 #endif
12591 				goto do_a_copy_in;
12592 
12593 			}
12594 		}
12595 		while (uio->uio_resid > 0) {
12596 			/* How much room do we have? */
12597 			struct mbuf *new_tail, *mm;
12598 
12599 			if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12600 				max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
12601 			else
12602 				max_len = 0;
12603 
12604 			if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
12605 			    (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
12606 			    (uio->uio_resid &&
12607 			    (uio->uio_resid <= (int)max_len))) {
12608 				sndout = 0;
12609 				new_tail = NULL;
12610 				if (hold_tcblock) {
12611 					SCTP_TCB_UNLOCK(stcb);
12612 					hold_tcblock = 0;
12613 				}
12614 				mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
12615 				if ((mm == NULL) || error) {
12616 					if (mm) {
12617 						sctp_m_freem(mm);
12618 					}
12619 					goto out;
12620 				}
12621 				/* Update the mbuf and count */
12622 				SCTP_TCB_SEND_LOCK(stcb);
12623 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12624 					/*
12625 					 * we need to get out. Peer probably
12626 					 * aborted.
12627 					 */
12628 					sctp_m_freem(mm);
12629 					if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
12630 						SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12631 						error = ECONNRESET;
12632 					}
12633 					SCTP_TCB_SEND_UNLOCK(stcb);
12634 					goto out;
12635 				}
12636 				if (sp->tail_mbuf) {
12637 					/* tack it to the end */
12638 					SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
12639 					sp->tail_mbuf = new_tail;
12640 				} else {
12641 					/* A stolen mbuf */
12642 					sp->data = mm;
12643 					sp->tail_mbuf = new_tail;
12644 				}
12645 				sctp_snd_sb_alloc(stcb, sndout);
12646 				atomic_add_int(&sp->length, sndout);
12647 				len += sndout;
12648 
12649 				/* Did we reach EOR? */
12650 				if ((uio->uio_resid == 0) &&
12651 				    ((user_marks_eor == 0) ||
12652 				    (srcv->sinfo_flags & SCTP_EOF) ||
12653 				    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))
12654 				    ) {
12655 					sp->msg_is_complete = 1;
12656 				} else {
12657 					sp->msg_is_complete = 0;
12658 				}
12659 				SCTP_TCB_SEND_UNLOCK(stcb);
12660 			}
12661 			if (uio->uio_resid == 0) {
12662 				/* got it all? */
12663 				continue;
12664 			}
12665 			/* PR-SCTP? */
12666 			if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
12667 				/*
12668 				 * This is ugly but we must assure locking
12669 				 * order
12670 				 */
12671 				if (hold_tcblock == 0) {
12672 					SCTP_TCB_LOCK(stcb);
12673 					hold_tcblock = 1;
12674 				}
12675 				sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
12676 				inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12677 				if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12678 					max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12679 				else
12680 					max_len = 0;
12681 				if (max_len > 0) {
12682 					continue;
12683 				}
12684 				SCTP_TCB_UNLOCK(stcb);
12685 				hold_tcblock = 0;
12686 			}
12687 			/* wait for space now */
12688 			if (non_blocking) {
12689 				/* Non-blocking io in place out */
12690 				goto skip_out_eof;
12691 			}
12692 			if ((net->flight_size > net->cwnd) &&
12693 			    (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12694 				queue_only = 1;
12695 			} else if (asoc->ifp_had_enobuf) {
12696 				SCTP_STAT_INCR(sctps_ifnomemqueued);
12697 				if (net->flight_size > (net->mtu * 2)) {
12698 					queue_only = 1;
12699 				} else {
12700 					queue_only = 0;
12701 				}
12702 				asoc->ifp_had_enobuf = 0;
12703 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12704 				    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12705 			} else {
12706 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12707 				    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12708 				if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
12709 					queue_only = 1;
12710 					SCTP_STAT_INCR(sctps_send_burst_avoid);
12711 				} else if (net->flight_size > net->cwnd) {
12712 					queue_only = 1;
12713 					SCTP_STAT_INCR(sctps_send_cwnd_avoid);
12714 				} else {
12715 					queue_only = 0;
12716 				}
12717 			}
12718 			if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
12719 			    (stcb->asoc.total_flight > 0) &&
12720 			    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
12721 			    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
12722 			    ) {
12723 
12724 				/*-
12725 				 * Ok, Nagle is set on and we have data outstanding.
12726 				 * Don't send anything and let SACKs drive out the
12727 				 * data unless wen have a "full" segment to send.
12728 				 */
12729 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
12730 					sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
12731 				}
12732 				SCTP_STAT_INCR(sctps_naglequeued);
12733 				nagle_applies = 1;
12734 			} else {
12735 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
12736 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
12737 						sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
12738 				}
12739 				SCTP_STAT_INCR(sctps_naglesent);
12740 				nagle_applies = 0;
12741 			}
12742 			/* What about the INIT, send it maybe */
12743 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12744 
12745 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
12746 				    nagle_applies, un_sent);
12747 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
12748 				    stcb->asoc.total_flight,
12749 				    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
12750 			}
12751 			if (queue_only_for_init) {
12752 				if (hold_tcblock == 0) {
12753 					SCTP_TCB_LOCK(stcb);
12754 					hold_tcblock = 1;
12755 				}
12756 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
12757 					/* a collision took us forward? */
12758 					queue_only_for_init = 0;
12759 					queue_only = 0;
12760 				} else {
12761 					sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
12762 					SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12763 					queue_only_for_init = 0;
12764 					queue_only = 1;
12765 				}
12766 			}
12767 			if ((queue_only == 0) && (nagle_applies == 0)
12768 			    ) {
12769 				/*-
12770 				 * need to start chunk output
12771 				 * before blocking.. note that if
12772 				 * a lock is already applied, then
12773 				 * the input via the net is happening
12774 				 * and I don't need to start output :-D
12775 				 */
12776 				if (hold_tcblock == 0) {
12777 					if (SCTP_TCB_TRYLOCK(stcb)) {
12778 						hold_tcblock = 1;
12779 						sctp_chunk_output(inp,
12780 						    stcb,
12781 						    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12782 					}
12783 				} else {
12784 					sctp_chunk_output(inp,
12785 					    stcb,
12786 					    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12787 				}
12788 				if (hold_tcblock == 1) {
12789 					SCTP_TCB_UNLOCK(stcb);
12790 					hold_tcblock = 0;
12791 				}
12792 			}
12793 			SOCKBUF_LOCK(&so->so_snd);
12794 			/*-
12795 		         * This is a bit strange, but I think it will
12796 		         * work. The total_output_queue_size is locked and
12797 		         * protected by the TCB_LOCK, which we just released.
12798 		         * There is a race that can occur between releasing it
12799 		         * above, and me getting the socket lock, where sacks
12800 		         * come in but we have not put the SB_WAIT on the
12801 		         * so_snd buffer to get the wakeup. After the LOCK
12802 		         * is applied the sack_processing will also need to
12803 		         * LOCK the so->so_snd to do the actual sowwakeup(). So
12804 		         * once we have the socket buffer lock if we recheck the
12805 		         * size we KNOW we will get to sleep safely with the
12806 		         * wakeup flag in place.
12807 		         */
12808 			if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
12809 			    min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))
12810 			    ) {
12811 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12812 					sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
12813 					    so, asoc, uio->uio_resid);
12814 				}
12815 				be.error = 0;
12816 				stcb->block_entry = &be;
12817 				error = sbwait(&so->so_snd);
12818 				stcb->block_entry = NULL;
12819 
12820 				if (error || so->so_error || be.error) {
12821 					if (error == 0) {
12822 						if (so->so_error)
12823 							error = so->so_error;
12824 						if (be.error) {
12825 							error = be.error;
12826 						}
12827 					}
12828 					SOCKBUF_UNLOCK(&so->so_snd);
12829 					goto out_unlocked;
12830 				}
12831 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12832 					sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12833 					    so, asoc, stcb->asoc.total_output_queue_size);
12834 				}
12835 			}
12836 			SOCKBUF_UNLOCK(&so->so_snd);
12837 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12838 				goto out_unlocked;
12839 			}
12840 		}
12841 		SCTP_TCB_SEND_LOCK(stcb);
12842 		if (sp) {
12843 			if (sp->msg_is_complete == 0) {
12844 				strm->last_msg_incomplete = 1;
12845 				asoc->stream_locked = 1;
12846 				asoc->stream_locked_on = srcv->sinfo_stream;
12847 			} else {
12848 				sp->sender_all_done = 1;
12849 				strm->last_msg_incomplete = 0;
12850 				asoc->stream_locked = 0;
12851 			}
12852 		} else {
12853 			SCTP_PRINTF("Huh no sp TSNH?\n");
12854 			strm->last_msg_incomplete = 0;
12855 			asoc->stream_locked = 0;
12856 		}
12857 		SCTP_TCB_SEND_UNLOCK(stcb);
12858 		if (uio->uio_resid == 0) {
12859 			got_all_of_the_send = 1;
12860 		}
12861 	} else if (top) {
12862 		/* We send in a 0, since we do NOT have any locks */
12863 		error = sctp_msg_append(stcb, net, top, srcv, 0);
12864 		top = NULL;
12865 		if (srcv->sinfo_flags & SCTP_EOF) {
12866 			/*
12867 			 * This should only happen for Panda for the mbuf
12868 			 * send case, which does NOT yet support EEOR mode.
12869 			 * Thus, we can just set this flag to do the proper
12870 			 * EOF handling.
12871 			 */
12872 			got_all_of_the_send = 1;
12873 		}
12874 	}
12875 	if (error) {
12876 		goto out;
12877 	}
12878 dataless_eof:
12879 	/* EOF thing ? */
12880 	if ((srcv->sinfo_flags & SCTP_EOF) &&
12881 	    (got_all_of_the_send == 1) &&
12882 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
12883 	    ) {
12884 		int cnt;
12885 
12886 		SCTP_STAT_INCR(sctps_sends_with_eof);
12887 		error = 0;
12888 		if (hold_tcblock == 0) {
12889 			SCTP_TCB_LOCK(stcb);
12890 			hold_tcblock = 1;
12891 		}
12892 		cnt = sctp_is_there_unsent_data(stcb);
12893 		if (TAILQ_EMPTY(&asoc->send_queue) &&
12894 		    TAILQ_EMPTY(&asoc->sent_queue) &&
12895 		    (cnt == 0)) {
12896 			if (asoc->locked_on_sending) {
12897 				goto abort_anyway;
12898 			}
12899 			/* there is nothing queued to send, so I'm done... */
12900 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
12901 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
12902 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
12903 				/* only send SHUTDOWN the first time through */
12904 				sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
12905 				if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
12906 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
12907 				}
12908 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
12909 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
12910 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
12911 				    asoc->primary_destination);
12912 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
12913 				    asoc->primary_destination);
12914 			}
12915 		} else {
12916 			/*-
12917 		         * we still got (or just got) data to send, so set
12918 		         * SHUTDOWN_PENDING
12919 		         */
12920 			/*-
12921 		         * XXX sockets draft says that SCTP_EOF should be
12922 		         * sent with no data.  currently, we will allow user
12923 		         * data to be sent first and move to
12924 		         * SHUTDOWN-PENDING
12925 		         */
12926 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
12927 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
12928 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
12929 				if (hold_tcblock == 0) {
12930 					SCTP_TCB_LOCK(stcb);
12931 					hold_tcblock = 1;
12932 				}
12933 				if (asoc->locked_on_sending) {
12934 					/* Locked to send out the data */
12935 					struct sctp_stream_queue_pending *sp;
12936 
12937 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
12938 					if (sp) {
12939 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
12940 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
12941 					}
12942 				}
12943 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
12944 				if (TAILQ_EMPTY(&asoc->send_queue) &&
12945 				    TAILQ_EMPTY(&asoc->sent_queue) &&
12946 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
12947 			abort_anyway:
12948 					if (free_cnt_applied) {
12949 						atomic_add_int(&stcb->asoc.refcnt, -1);
12950 						free_cnt_applied = 0;
12951 					}
12952 					sctp_abort_an_association(stcb->sctp_ep, stcb,
12953 					    SCTP_RESPONSE_TO_USER_REQ,
12954 					    NULL, SCTP_SO_LOCKED);
12955 					/*
12956 					 * now relock the stcb so everything
12957 					 * is sane
12958 					 */
12959 					hold_tcblock = 0;
12960 					stcb = NULL;
12961 					goto out;
12962 				}
12963 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
12964 				    asoc->primary_destination);
12965 				sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
12966 			}
12967 		}
12968 	}
12969 skip_out_eof:
12970 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
12971 		some_on_control = 1;
12972 	}
12973 	if ((net->flight_size > net->cwnd) &&
12974 	    (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12975 		queue_only = 1;
12976 	} else if (asoc->ifp_had_enobuf) {
12977 		SCTP_STAT_INCR(sctps_ifnomemqueued);
12978 		if (net->flight_size > (net->mtu * 2)) {
12979 			queue_only = 1;
12980 		} else {
12981 			queue_only = 0;
12982 		}
12983 		asoc->ifp_had_enobuf = 0;
12984 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12985 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12986 	} else {
12987 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12988 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12989 		if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
12990 			queue_only = 1;
12991 			SCTP_STAT_INCR(sctps_send_burst_avoid);
12992 		} else if (net->flight_size > net->cwnd) {
12993 			queue_only = 1;
12994 			SCTP_STAT_INCR(sctps_send_cwnd_avoid);
12995 		} else {
12996 			queue_only = 0;
12997 		}
12998 	}
12999 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13000 	    (stcb->asoc.total_flight > 0) &&
13001 	    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13002 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
13003 	    ) {
13004 		/*-
13005 		 * Ok, Nagle is set on and we have data outstanding.
13006 		 * Don't send anything and let SACKs drive out the
13007 		 * data unless wen have a "full" segment to send.
13008 		 */
13009 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13010 			sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13011 		}
13012 		SCTP_STAT_INCR(sctps_naglequeued);
13013 		nagle_applies = 1;
13014 	} else {
13015 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13016 			if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13017 				sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13018 		}
13019 		SCTP_STAT_INCR(sctps_naglesent);
13020 		nagle_applies = 0;
13021 	}
13022 	if (queue_only_for_init) {
13023 		if (hold_tcblock == 0) {
13024 			SCTP_TCB_LOCK(stcb);
13025 			hold_tcblock = 1;
13026 		}
13027 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13028 			/* a collision took us forward? */
13029 			queue_only_for_init = 0;
13030 			queue_only = 0;
13031 		} else {
13032 			sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13033 			SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13034 			queue_only_for_init = 0;
13035 			queue_only = 1;
13036 		}
13037 	}
13038 	if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13039 		/* we can attempt to send too. */
13040 		if (hold_tcblock == 0) {
13041 			/*
13042 			 * If there is activity recv'ing sacks no need to
13043 			 * send
13044 			 */
13045 			if (SCTP_TCB_TRYLOCK(stcb)) {
13046 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13047 				hold_tcblock = 1;
13048 			}
13049 		} else {
13050 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13051 		}
13052 	} else if ((queue_only == 0) &&
13053 		    (stcb->asoc.peers_rwnd == 0) &&
13054 	    (stcb->asoc.total_flight == 0)) {
13055 		/* We get to have a probe outstanding */
13056 		if (hold_tcblock == 0) {
13057 			hold_tcblock = 1;
13058 			SCTP_TCB_LOCK(stcb);
13059 		}
13060 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13061 	} else if (some_on_control) {
13062 		int num_out, reason, cwnd_full, frag_point;
13063 
13064 		/* Here we do control only */
13065 		if (hold_tcblock == 0) {
13066 			hold_tcblock = 1;
13067 			SCTP_TCB_LOCK(stcb);
13068 		}
13069 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13070 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13071 		    &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13072 	}
13073 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13074 	    queue_only, stcb->asoc.peers_rwnd, un_sent,
13075 	    stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13076 	    stcb->asoc.total_output_queue_size, error);
13077 
13078 out:
13079 out_unlocked:
13080 
13081 	if (local_soresv && stcb) {
13082 		atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13083 		local_soresv = 0;
13084 	}
13085 	if (create_lock_applied) {
13086 		SCTP_ASOC_CREATE_UNLOCK(inp);
13087 		create_lock_applied = 0;
13088 	}
13089 	if ((stcb) && hold_tcblock) {
13090 		SCTP_TCB_UNLOCK(stcb);
13091 	}
13092 	if (stcb && free_cnt_applied) {
13093 		atomic_add_int(&stcb->asoc.refcnt, -1);
13094 	}
13095 #ifdef INVARIANTS
13096 	if (stcb) {
13097 		if (mtx_owned(&stcb->tcb_mtx)) {
13098 			panic("Leaving with tcb mtx owned?");
13099 		}
13100 		if (mtx_owned(&stcb->tcb_send_mtx)) {
13101 			panic("Leaving with tcb send mtx owned?");
13102 		}
13103 	}
13104 #endif
13105 	if (top) {
13106 		sctp_m_freem(top);
13107 	}
13108 	if (control) {
13109 		sctp_m_freem(control);
13110 	}
13111 	return (error);
13112 }
13113 
13114 
13115 /*
13116  * generate an AUTHentication chunk, if required
13117  */
13118 struct mbuf *
13119 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13120     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13121     struct sctp_tcb *stcb, uint8_t chunk)
13122 {
13123 	struct mbuf *m_auth;
13124 	struct sctp_auth_chunk *auth;
13125 	int chunk_len;
13126 
13127 	if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13128 	    (stcb == NULL))
13129 		return (m);
13130 
13131 	/* sysctl disabled auth? */
13132 	if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13133 		return (m);
13134 
13135 	/* peer doesn't do auth... */
13136 	if (!stcb->asoc.peer_supports_auth) {
13137 		return (m);
13138 	}
13139 	/* does the requested chunk require auth? */
13140 	if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13141 		return (m);
13142 	}
13143 	m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13144 	if (m_auth == NULL) {
13145 		/* no mbuf's */
13146 		return (m);
13147 	}
13148 	/* reserve some space if this will be the first mbuf */
13149 	if (m == NULL)
13150 		SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13151 	/* fill in the AUTH chunk details */
13152 	auth = mtod(m_auth, struct sctp_auth_chunk *);
13153 	bzero(auth, sizeof(*auth));
13154 	auth->ch.chunk_type = SCTP_AUTHENTICATION;
13155 	auth->ch.chunk_flags = 0;
13156 	chunk_len = sizeof(*auth) +
13157 	    sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13158 	auth->ch.chunk_length = htons(chunk_len);
13159 	auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13160 	/* key id and hmac digest will be computed and filled in upon send */
13161 
13162 	/* save the offset where the auth was inserted into the chain */
13163 	if (m != NULL) {
13164 		struct mbuf *cn;
13165 
13166 		*offset = 0;
13167 		cn = m;
13168 		while (cn) {
13169 			*offset += SCTP_BUF_LEN(cn);
13170 			cn = SCTP_BUF_NEXT(cn);
13171 		}
13172 	} else
13173 		*offset = 0;
13174 
13175 	/* update length and return pointer to the auth chunk */
13176 	SCTP_BUF_LEN(m_auth) = chunk_len;
13177 	m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13178 	if (auth_ret != NULL)
13179 		*auth_ret = auth;
13180 
13181 	return (m);
13182 }
13183 
13184 #ifdef INET6
13185 int
13186 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13187 {
13188 	struct nd_prefix *pfx = NULL;
13189 	struct nd_pfxrouter *pfxrtr = NULL;
13190 	struct sockaddr_in6 gw6;
13191 
13192 	if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13193 		return (0);
13194 
13195 	/* get prefix entry of address */
13196 	LIST_FOREACH(pfx, &MODULE_GLOBAL(MOD_INET6, nd_prefix), ndpr_entry) {
13197 		if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13198 			continue;
13199 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13200 		    &src6->sin6_addr, &pfx->ndpr_mask))
13201 			break;
13202 	}
13203 	/* no prefix entry in the prefix list */
13204 	if (pfx == NULL) {
13205 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13206 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13207 		return (0);
13208 	}
13209 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13210 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13211 
13212 	/* search installed gateway from prefix entry */
13213 	for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
13214 	    pfxrtr->pfr_next) {
13215 		memset(&gw6, 0, sizeof(struct sockaddr_in6));
13216 		gw6.sin6_family = AF_INET6;
13217 		gw6.sin6_len = sizeof(struct sockaddr_in6);
13218 		memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13219 		    sizeof(struct in6_addr));
13220 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13221 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13222 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13223 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13224 		if (sctp_cmpaddr((struct sockaddr *)&gw6,
13225 		    ro->ro_rt->rt_gateway)) {
13226 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13227 			return (1);
13228 		}
13229 	}
13230 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13231 	return (0);
13232 }
13233 
13234 #endif
13235 
13236 int
13237 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13238 {
13239 	struct sockaddr_in *sin, *mask;
13240 	struct ifaddr *ifa;
13241 	struct in_addr srcnetaddr, gwnetaddr;
13242 
13243 	if (ro == NULL || ro->ro_rt == NULL ||
13244 	    sifa->address.sa.sa_family != AF_INET) {
13245 		return (0);
13246 	}
13247 	ifa = (struct ifaddr *)sifa->ifa;
13248 	mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13249 	sin = (struct sockaddr_in *)&sifa->address.sin;
13250 	srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13251 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13252 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13253 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13254 
13255 	sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13256 	gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13257 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13258 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13259 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13260 	if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13261 		return (1);
13262 	}
13263 	return (0);
13264 }
13265