xref: /freebsd/sys/netinet/sctp_output.c (revision 721351876cd4d3a8a700f62d2061331fa951a488)
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__)
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, 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, 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, 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_ecn_supported_param *ecn;
4100 	struct sctp_prsctp_supported_param *prsctp;
4101 	struct sctp_ecn_nonce_supported_param *ecn_nonce;
4102 	struct sctp_supported_chunk_types_param *pr_supported;
4103 	int cnt_inits_to = 0;
4104 	int padval, ret;
4105 	int num_ext;
4106 	int p_len;
4107 
4108 	/* INIT's always go to the primary (and usually ONLY address) */
4109 	mp_last = NULL;
4110 	net = stcb->asoc.primary_destination;
4111 	if (net == NULL) {
4112 		net = TAILQ_FIRST(&stcb->asoc.nets);
4113 		if (net == NULL) {
4114 			/* TSNH */
4115 			return;
4116 		}
4117 		/* we confirm any address we send an INIT to */
4118 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4119 		(void)sctp_set_primary_addr(stcb, NULL, net);
4120 	} else {
4121 		/* we confirm any address we send an INIT to */
4122 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4123 	}
4124 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4125 #ifdef INET6
4126 	if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4127 		/*
4128 		 * special hook, if we are sending to link local it will not
4129 		 * show up in our private address count.
4130 		 */
4131 		struct sockaddr_in6 *sin6l;
4132 
4133 		sin6l = &net->ro._l_addr.sin6;
4134 		if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4135 			cnt_inits_to = 1;
4136 	}
4137 #endif
4138 	if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4139 		/* This case should not happen */
4140 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4141 		return;
4142 	}
4143 	/* start the INIT timer */
4144 	sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4145 
4146 	m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4147 	if (m == NULL) {
4148 		/* No memory, INIT timer will re-attempt. */
4149 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4150 		return;
4151 	}
4152 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
4153 	/*
4154 	 * assume peer supports asconf in order to be able to queue local
4155 	 * address changes while an INIT is in flight and before the assoc
4156 	 * is established.
4157 	 */
4158 	stcb->asoc.peer_supports_asconf = 1;
4159 	/* Now lets put the SCTP header in place */
4160 	initm = mtod(m, struct sctp_init_msg *);
4161 	initm->sh.src_port = inp->sctp_lport;
4162 	initm->sh.dest_port = stcb->rport;
4163 	initm->sh.v_tag = 0;
4164 	initm->sh.checksum = 0;	/* calculate later */
4165 	/* now the chunk header */
4166 	initm->msg.ch.chunk_type = SCTP_INITIATION;
4167 	initm->msg.ch.chunk_flags = 0;
4168 	/* fill in later from mbuf we build */
4169 	initm->msg.ch.chunk_length = 0;
4170 	/* place in my tag */
4171 	initm->msg.init.initiate_tag = htonl(stcb->asoc.my_vtag);
4172 	/* set up some of the credits. */
4173 	initm->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(inp->sctp_socket),
4174 	    SCTP_MINIMAL_RWND));
4175 
4176 	initm->msg.init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4177 	initm->msg.init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4178 	initm->msg.init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4179 	/* now the address restriction */
4180 	sup_addr = (struct sctp_supported_addr_param *)((caddr_t)initm +
4181 	    sizeof(*initm));
4182 	sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4183 #ifdef INET6
4184 	/* we support 2 types: IPv6/IPv4 */
4185 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
4186 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4187 	sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4188 #else
4189 	/* we support 1 type: IPv4 */
4190 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
4191 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4192 	sup_addr->addr_type[1] = htons(0);	/* this is the padding */
4193 #endif
4194 	SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
4195 
4196 	if (inp->sctp_ep.adaptation_layer_indicator) {
4197 		struct sctp_adaptation_layer_indication *ali;
4198 
4199 		ali = (struct sctp_adaptation_layer_indication *)(
4200 		    (caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
4201 		ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4202 		ali->ph.param_length = htons(sizeof(*ali));
4203 		ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4204 		SCTP_BUF_LEN(m) += sizeof(*ali);
4205 		ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
4206 		    sizeof(*ali));
4207 	} else {
4208 		ecn = (struct sctp_ecn_supported_param *)((caddr_t)sup_addr +
4209 		    sizeof(*sup_addr) + sizeof(uint16_t));
4210 	}
4211 
4212 	/* now any cookie time extensions */
4213 	if (stcb->asoc.cookie_preserve_req) {
4214 		struct sctp_cookie_perserve_param *cookie_preserve;
4215 
4216 		cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4217 		cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4218 		cookie_preserve->ph.param_length = htons(
4219 		    sizeof(*cookie_preserve));
4220 		cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4221 		SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4222 		ecn = (struct sctp_ecn_supported_param *)(
4223 		    (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4224 		stcb->asoc.cookie_preserve_req = 0;
4225 	}
4226 	/* ECN parameter */
4227 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
4228 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4229 		ecn->ph.param_length = htons(sizeof(*ecn));
4230 		SCTP_BUF_LEN(m) += sizeof(*ecn);
4231 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4232 		    sizeof(*ecn));
4233 	} else {
4234 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4235 	}
4236 	/* And now tell the peer we do pr-sctp */
4237 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4238 	prsctp->ph.param_length = htons(sizeof(*prsctp));
4239 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
4240 
4241 	/* And now tell the peer we do all the extensions */
4242 	pr_supported = (struct sctp_supported_chunk_types_param *)
4243 	    ((caddr_t)prsctp + sizeof(*prsctp));
4244 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4245 	num_ext = 0;
4246 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4247 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4248 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4249 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4250 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4251 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
4252 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4253 	p_len = sizeof(*pr_supported) + num_ext;
4254 	pr_supported->ph.param_length = htons(p_len);
4255 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4256 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4257 
4258 	/* ECN nonce: And now tell the peer we support ECN nonce */
4259 	if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
4260 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
4261 		    ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
4262 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
4263 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
4264 		SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
4265 	}
4266 	/* add authentication parameters */
4267 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4268 		struct sctp_auth_random *randp;
4269 		struct sctp_auth_hmac_algo *hmacs;
4270 		struct sctp_auth_chunk_list *chunks;
4271 
4272 		/* attach RANDOM parameter, if available */
4273 		if (stcb->asoc.authinfo.random != NULL) {
4274 			randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4275 			p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4276 #ifdef SCTP_AUTH_DRAFT_04
4277 			randp->ph.param_type = htons(SCTP_RANDOM);
4278 			randp->ph.param_length = htons(p_len);
4279 			bcopy(stcb->asoc.authinfo.random->key,
4280 			    randp->random_data,
4281 			    stcb->asoc.authinfo.random_len);
4282 #else
4283 			/* random key already contains the header */
4284 			bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4285 #endif
4286 			/* zero out any padding required */
4287 			bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4288 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4289 		}
4290 		/* add HMAC_ALGO parameter */
4291 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4292 		p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4293 		    (uint8_t *) hmacs->hmac_ids);
4294 		if (p_len > 0) {
4295 			p_len += sizeof(*hmacs);
4296 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4297 			hmacs->ph.param_length = htons(p_len);
4298 			/* zero out any padding required */
4299 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4300 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4301 		}
4302 		/* add CHUNKS parameter */
4303 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4304 		p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4305 		    chunks->chunk_types);
4306 		if (p_len > 0) {
4307 			p_len += sizeof(*chunks);
4308 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4309 			chunks->ph.param_length = htons(p_len);
4310 			/* zero out any padding required */
4311 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4312 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4313 		}
4314 	}
4315 	m_at = m;
4316 	/* now the addresses */
4317 	{
4318 		struct sctp_scoping scp;
4319 
4320 		/*
4321 		 * To optimize this we could put the scoping stuff into a
4322 		 * structure and remove the individual uint8's from the
4323 		 * assoc structure. Then we could just sifa in the address
4324 		 * within the stcb.. but for now this is a quick hack to get
4325 		 * the address stuff teased apart.
4326 		 */
4327 		scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4328 		scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4329 		scp.loopback_scope = stcb->asoc.loopback_scope;
4330 		scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4331 		scp.local_scope = stcb->asoc.local_scope;
4332 		scp.site_scope = stcb->asoc.site_scope;
4333 
4334 		m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
4335 	}
4336 
4337 	/* calulate the size and update pkt header and chunk header */
4338 	p_len = 0;
4339 	for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4340 		if (SCTP_BUF_NEXT(m_at) == NULL)
4341 			mp_last = m_at;
4342 		p_len += SCTP_BUF_LEN(m_at);
4343 	}
4344 	initm->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
4345 	/*
4346 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4347 	 * here since the timer will drive a retranmission.
4348 	 */
4349 
4350 	/* I don't expect this to execute but we will be safe here */
4351 	padval = p_len % 4;
4352 	if ((padval) && (mp_last)) {
4353 		/*
4354 		 * The compiler worries that mp_last may not be set even
4355 		 * though I think it is impossible :-> however we add
4356 		 * mp_last here just in case.
4357 		 */
4358 		ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4359 		if (ret) {
4360 			/* Houston we have a problem, no space */
4361 			sctp_m_freem(m);
4362 			return;
4363 		}
4364 		p_len += padval;
4365 	}
4366 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4367 	ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4368 	    (struct sockaddr *)&net->ro._l_addr,
4369 	    m, 0, NULL, 0, 0, NULL, 0, net->port, so_locked, NULL);
4370 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4371 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4372 	sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4373 	(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4374 }
4375 
4376 struct mbuf *
4377 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4378     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp)
4379 {
4380 	/*
4381 	 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4382 	 * being equal to the beginning of the params i.e. (iphlen +
4383 	 * sizeof(struct sctp_init_msg) parse through the parameters to the
4384 	 * end of the mbuf verifying that all parameters are known.
4385 	 *
4386 	 * For unknown parameters build and return a mbuf with
4387 	 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4388 	 * processing this chunk stop, and set *abort_processing to 1.
4389 	 *
4390 	 * By having param_offset be pre-set to where parameters begin it is
4391 	 * hoped that this routine may be reused in the future by new
4392 	 * features.
4393 	 */
4394 	struct sctp_paramhdr *phdr, params;
4395 
4396 	struct mbuf *mat, *op_err;
4397 	char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4398 	int at, limit, pad_needed;
4399 	uint16_t ptype, plen, padded_size;
4400 	int err_at;
4401 
4402 	*abort_processing = 0;
4403 	mat = in_initpkt;
4404 	err_at = 0;
4405 	limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4406 	at = param_offset;
4407 	op_err = NULL;
4408 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4409 	phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4410 	while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4411 		ptype = ntohs(phdr->param_type);
4412 		plen = ntohs(phdr->param_length);
4413 		if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4414 			/* wacked parameter */
4415 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4416 			goto invalid_size;
4417 		}
4418 		limit -= SCTP_SIZE32(plen);
4419 		/*-
4420 		 * All parameters for all chunks that we know/understand are
4421 		 * listed here. We process them other places and make
4422 		 * appropriate stop actions per the upper bits. However this
4423 		 * is the generic routine processor's can call to get back
4424 		 * an operr.. to either incorporate (init-ack) or send.
4425 		 */
4426 		padded_size = SCTP_SIZE32(plen);
4427 		switch (ptype) {
4428 			/* Param's with variable size */
4429 		case SCTP_HEARTBEAT_INFO:
4430 		case SCTP_STATE_COOKIE:
4431 		case SCTP_UNRECOG_PARAM:
4432 		case SCTP_ERROR_CAUSE_IND:
4433 			/* ok skip fwd */
4434 			at += padded_size;
4435 			break;
4436 			/* Param's with variable size within a range */
4437 		case SCTP_CHUNK_LIST:
4438 		case SCTP_SUPPORTED_CHUNK_EXT:
4439 			if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4440 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4441 				goto invalid_size;
4442 			}
4443 			at += padded_size;
4444 			break;
4445 		case SCTP_SUPPORTED_ADDRTYPE:
4446 			if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4447 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4448 				goto invalid_size;
4449 			}
4450 			at += padded_size;
4451 			break;
4452 		case SCTP_RANDOM:
4453 			if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4454 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4455 				goto invalid_size;
4456 			}
4457 			at += padded_size;
4458 			break;
4459 		case SCTP_SET_PRIM_ADDR:
4460 		case SCTP_DEL_IP_ADDRESS:
4461 		case SCTP_ADD_IP_ADDRESS:
4462 			if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4463 			    (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4464 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4465 				goto invalid_size;
4466 			}
4467 			at += padded_size;
4468 			break;
4469 			/* Param's with a fixed size */
4470 		case SCTP_IPV4_ADDRESS:
4471 			if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4472 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4473 				goto invalid_size;
4474 			}
4475 			at += padded_size;
4476 			break;
4477 		case SCTP_IPV6_ADDRESS:
4478 			if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4479 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4480 				goto invalid_size;
4481 			}
4482 			at += padded_size;
4483 			break;
4484 		case SCTP_COOKIE_PRESERVE:
4485 			if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4486 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4487 				goto invalid_size;
4488 			}
4489 			at += padded_size;
4490 			break;
4491 		case SCTP_ECN_NONCE_SUPPORTED:
4492 		case SCTP_PRSCTP_SUPPORTED:
4493 			if (padded_size != sizeof(struct sctp_paramhdr)) {
4494 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp %d\n", plen);
4495 				goto invalid_size;
4496 			}
4497 			at += padded_size;
4498 			break;
4499 		case SCTP_ECN_CAPABLE:
4500 			if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4501 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4502 				goto invalid_size;
4503 			}
4504 			at += padded_size;
4505 			break;
4506 		case SCTP_ULP_ADAPTATION:
4507 			if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4508 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4509 				goto invalid_size;
4510 			}
4511 			at += padded_size;
4512 			break;
4513 		case SCTP_SUCCESS_REPORT:
4514 			if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
4515 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
4516 				goto invalid_size;
4517 			}
4518 			at += padded_size;
4519 			break;
4520 		case SCTP_HOSTNAME_ADDRESS:
4521 			{
4522 				/* We can NOT handle HOST NAME addresses!! */
4523 				int l_len;
4524 
4525 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
4526 				*abort_processing = 1;
4527 				if (op_err == NULL) {
4528 					/* Ok need to try to get a mbuf */
4529 #ifdef INET6
4530 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4531 #else
4532 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4533 #endif
4534 					l_len += plen;
4535 					l_len += sizeof(struct sctp_paramhdr);
4536 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4537 					if (op_err) {
4538 						SCTP_BUF_LEN(op_err) = 0;
4539 						/*
4540 						 * pre-reserve space for ip
4541 						 * and sctp header  and
4542 						 * chunk hdr
4543 						 */
4544 #ifdef INET6
4545 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4546 #else
4547 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4548 #endif
4549 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4550 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4551 					}
4552 				}
4553 				if (op_err) {
4554 					/* If we have space */
4555 					struct sctp_paramhdr s;
4556 
4557 					if (err_at % 4) {
4558 						uint32_t cpthis = 0;
4559 
4560 						pad_needed = 4 - (err_at % 4);
4561 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4562 						err_at += pad_needed;
4563 					}
4564 					s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
4565 					s.param_length = htons(sizeof(s) + plen);
4566 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4567 					err_at += sizeof(s);
4568 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4569 					if (phdr == NULL) {
4570 						sctp_m_freem(op_err);
4571 						/*
4572 						 * we are out of memory but
4573 						 * we still need to have a
4574 						 * look at what to do (the
4575 						 * system is in trouble
4576 						 * though).
4577 						 */
4578 						return (NULL);
4579 					}
4580 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4581 					err_at += plen;
4582 				}
4583 				return (op_err);
4584 				break;
4585 			}
4586 		default:
4587 			/*
4588 			 * we do not recognize the parameter figure out what
4589 			 * we do.
4590 			 */
4591 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
4592 			if ((ptype & 0x4000) == 0x4000) {
4593 				/* Report bit is set?? */
4594 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
4595 				if (op_err == NULL) {
4596 					int l_len;
4597 
4598 					/* Ok need to try to get an mbuf */
4599 #ifdef INET6
4600 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4601 #else
4602 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4603 #endif
4604 					l_len += plen;
4605 					l_len += sizeof(struct sctp_paramhdr);
4606 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4607 					if (op_err) {
4608 						SCTP_BUF_LEN(op_err) = 0;
4609 #ifdef INET6
4610 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4611 #else
4612 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4613 #endif
4614 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4615 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4616 					}
4617 				}
4618 				if (op_err) {
4619 					/* If we have space */
4620 					struct sctp_paramhdr s;
4621 
4622 					if (err_at % 4) {
4623 						uint32_t cpthis = 0;
4624 
4625 						pad_needed = 4 - (err_at % 4);
4626 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4627 						err_at += pad_needed;
4628 					}
4629 					s.param_type = htons(SCTP_UNRECOG_PARAM);
4630 					s.param_length = htons(sizeof(s) + plen);
4631 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4632 					err_at += sizeof(s);
4633 					if (plen > sizeof(tempbuf)) {
4634 						plen = sizeof(tempbuf);
4635 					}
4636 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4637 					if (phdr == NULL) {
4638 						sctp_m_freem(op_err);
4639 						/*
4640 						 * we are out of memory but
4641 						 * we still need to have a
4642 						 * look at what to do (the
4643 						 * system is in trouble
4644 						 * though).
4645 						 */
4646 						op_err = NULL;
4647 						goto more_processing;
4648 					}
4649 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4650 					err_at += plen;
4651 				}
4652 			}
4653 	more_processing:
4654 			if ((ptype & 0x8000) == 0x0000) {
4655 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
4656 				return (op_err);
4657 			} else {
4658 				/* skip this chunk and continue processing */
4659 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
4660 				at += SCTP_SIZE32(plen);
4661 			}
4662 			break;
4663 
4664 		}
4665 		phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4666 	}
4667 	return (op_err);
4668 invalid_size:
4669 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
4670 	*abort_processing = 1;
4671 	if ((op_err == NULL) && phdr) {
4672 		int l_len;
4673 
4674 #ifdef INET6
4675 		l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4676 #else
4677 		l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4678 #endif
4679 		l_len += (2 * sizeof(struct sctp_paramhdr));
4680 		op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4681 		if (op_err) {
4682 			SCTP_BUF_LEN(op_err) = 0;
4683 #ifdef INET6
4684 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4685 #else
4686 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4687 #endif
4688 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4689 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4690 		}
4691 	}
4692 	if ((op_err) && phdr) {
4693 		struct sctp_paramhdr s;
4694 
4695 		if (err_at % 4) {
4696 			uint32_t cpthis = 0;
4697 
4698 			pad_needed = 4 - (err_at % 4);
4699 			m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4700 			err_at += pad_needed;
4701 		}
4702 		s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4703 		s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
4704 		m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4705 		err_at += sizeof(s);
4706 		/* Only copy back the p-hdr that caused the issue */
4707 		m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
4708 	}
4709 	return (op_err);
4710 }
4711 
4712 static int
4713 sctp_are_there_new_addresses(struct sctp_association *asoc,
4714     struct mbuf *in_initpkt, int iphlen, int offset)
4715 {
4716 	/*
4717 	 * Given a INIT packet, look through the packet to verify that there
4718 	 * are NO new addresses. As we go through the parameters add reports
4719 	 * of any un-understood parameters that require an error.  Also we
4720 	 * must return (1) to drop the packet if we see a un-understood
4721 	 * parameter that tells us to drop the chunk.
4722 	 */
4723 	struct sockaddr_in sin4, *sa4;
4724 
4725 #ifdef INET6
4726 	struct sockaddr_in6 sin6, *sa6;
4727 
4728 #endif
4729 	struct sockaddr *sa_touse;
4730 	struct sockaddr *sa;
4731 	struct sctp_paramhdr *phdr, params;
4732 	struct ip *iph;
4733 
4734 #ifdef INET6
4735 	struct ip6_hdr *ip6h;
4736 
4737 #endif
4738 	struct mbuf *mat;
4739 	uint16_t ptype, plen;
4740 	int err_at;
4741 	uint8_t fnd;
4742 	struct sctp_nets *net;
4743 
4744 	memset(&sin4, 0, sizeof(sin4));
4745 #ifdef INET6
4746 	memset(&sin6, 0, sizeof(sin6));
4747 #endif
4748 	sin4.sin_family = AF_INET;
4749 	sin4.sin_len = sizeof(sin4);
4750 #ifdef INET6
4751 	sin6.sin6_family = AF_INET6;
4752 	sin6.sin6_len = sizeof(sin6);
4753 #endif
4754 	sa_touse = NULL;
4755 	/* First what about the src address of the pkt ? */
4756 	iph = mtod(in_initpkt, struct ip *);
4757 	switch (iph->ip_v) {
4758 	case IPVERSION:
4759 		/* source addr is IPv4 */
4760 		sin4.sin_addr = iph->ip_src;
4761 		sa_touse = (struct sockaddr *)&sin4;
4762 		break;
4763 #ifdef INET6
4764 	case IPV6_VERSION >> 4:
4765 		/* source addr is IPv6 */
4766 		ip6h = mtod(in_initpkt, struct ip6_hdr *);
4767 		sin6.sin6_addr = ip6h->ip6_src;
4768 		sa_touse = (struct sockaddr *)&sin6;
4769 		break;
4770 #endif
4771 	default:
4772 		return (1);
4773 	}
4774 
4775 	fnd = 0;
4776 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4777 		sa = (struct sockaddr *)&net->ro._l_addr;
4778 		if (sa->sa_family == sa_touse->sa_family) {
4779 			if (sa->sa_family == AF_INET) {
4780 				sa4 = (struct sockaddr_in *)sa;
4781 				if (sa4->sin_addr.s_addr ==
4782 				    sin4.sin_addr.s_addr) {
4783 					fnd = 1;
4784 					break;
4785 				}
4786 			}
4787 #ifdef INET6
4788 			if (sa->sa_family == AF_INET6) {
4789 				sa6 = (struct sockaddr_in6 *)sa;
4790 				if (SCTP6_ARE_ADDR_EQUAL(sa6,
4791 				    &sin6)) {
4792 					fnd = 1;
4793 					break;
4794 				}
4795 			}
4796 #endif
4797 		}
4798 	}
4799 	if (fnd == 0) {
4800 		/* New address added! no need to look futher. */
4801 		return (1);
4802 	}
4803 	/* Ok so far lets munge through the rest of the packet */
4804 	mat = in_initpkt;
4805 	err_at = 0;
4806 	sa_touse = NULL;
4807 	offset += sizeof(struct sctp_init_chunk);
4808 	phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
4809 	while (phdr) {
4810 		ptype = ntohs(phdr->param_type);
4811 		plen = ntohs(phdr->param_length);
4812 		if (ptype == SCTP_IPV4_ADDRESS) {
4813 			struct sctp_ipv4addr_param *p4, p4_buf;
4814 
4815 			phdr = sctp_get_next_param(mat, offset,
4816 			    (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4817 			if (plen != sizeof(struct sctp_ipv4addr_param) ||
4818 			    phdr == NULL) {
4819 				return (1);
4820 			}
4821 			p4 = (struct sctp_ipv4addr_param *)phdr;
4822 			sin4.sin_addr.s_addr = p4->addr;
4823 			sa_touse = (struct sockaddr *)&sin4;
4824 		} else if (ptype == SCTP_IPV6_ADDRESS) {
4825 			struct sctp_ipv6addr_param *p6, p6_buf;
4826 
4827 			phdr = sctp_get_next_param(mat, offset,
4828 			    (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4829 			if (plen != sizeof(struct sctp_ipv6addr_param) ||
4830 			    phdr == NULL) {
4831 				return (1);
4832 			}
4833 			p6 = (struct sctp_ipv6addr_param *)phdr;
4834 #ifdef INET6
4835 			memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4836 			    sizeof(p6->addr));
4837 #endif
4838 			sa_touse = (struct sockaddr *)&sin4;
4839 		}
4840 		if (sa_touse) {
4841 			/* ok, sa_touse points to one to check */
4842 			fnd = 0;
4843 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4844 				sa = (struct sockaddr *)&net->ro._l_addr;
4845 				if (sa->sa_family != sa_touse->sa_family) {
4846 					continue;
4847 				}
4848 				if (sa->sa_family == AF_INET) {
4849 					sa4 = (struct sockaddr_in *)sa;
4850 					if (sa4->sin_addr.s_addr ==
4851 					    sin4.sin_addr.s_addr) {
4852 						fnd = 1;
4853 						break;
4854 					}
4855 				}
4856 #ifdef INET6
4857 				if (sa->sa_family == AF_INET6) {
4858 					sa6 = (struct sockaddr_in6 *)sa;
4859 					if (SCTP6_ARE_ADDR_EQUAL(
4860 					    sa6, &sin6)) {
4861 						fnd = 1;
4862 						break;
4863 					}
4864 				}
4865 #endif
4866 			}
4867 			if (!fnd) {
4868 				/* New addr added! no need to look further */
4869 				return (1);
4870 			}
4871 		}
4872 		offset += SCTP_SIZE32(plen);
4873 		phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
4874 	}
4875 	return (0);
4876 }
4877 
4878 /*
4879  * Given a MBUF chain that was sent into us containing an INIT. Build a
4880  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
4881  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
4882  * message (i.e. the struct sctp_init_msg).
4883  */
4884 void
4885 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4886     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
4887     struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
4888 {
4889 	struct sctp_association *asoc;
4890 	struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
4891 	struct sctp_init_msg *initackm_out;
4892 	struct sctp_ecn_supported_param *ecn;
4893 	struct sctp_prsctp_supported_param *prsctp;
4894 	struct sctp_ecn_nonce_supported_param *ecn_nonce;
4895 	struct sctp_supported_chunk_types_param *pr_supported;
4896 	union sctp_sockstore store, store1, *over_addr;
4897 	struct sockaddr_in *sin, *to_sin;
4898 
4899 #ifdef INET6
4900 	struct sockaddr_in6 *sin6, *to_sin6;
4901 
4902 #endif
4903 	struct ip *iph;
4904 
4905 #ifdef INET6
4906 	struct ip6_hdr *ip6;
4907 
4908 #endif
4909 	struct sockaddr *to;
4910 	struct sctp_state_cookie stc;
4911 	struct sctp_nets *net = NULL;
4912 	uint8_t *signature = NULL;
4913 	int cnt_inits_to = 0;
4914 	uint16_t his_limit, i_want;
4915 	int abort_flag, padval;
4916 	int num_ext;
4917 	int p_len;
4918 	struct socket *so;
4919 
4920 	if (stcb)
4921 		asoc = &stcb->asoc;
4922 	else
4923 		asoc = NULL;
4924 	mp_last = NULL;
4925 	if ((asoc != NULL) &&
4926 	    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4927 	    (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
4928 		/* new addresses, out of here in non-cookie-wait states */
4929 		/*
4930 		 * Send a ABORT, we don't add the new address error clause
4931 		 * though we even set the T bit and copy in the 0 tag.. this
4932 		 * looks no different than if no listener was present.
4933 		 */
4934 		sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
4935 		return;
4936 	}
4937 	abort_flag = 0;
4938 	op_err = sctp_arethere_unrecognized_parameters(init_pkt,
4939 	    (offset + sizeof(struct sctp_init_chunk)),
4940 	    &abort_flag, (struct sctp_chunkhdr *)init_chk);
4941 	if (abort_flag) {
4942 do_a_abort:
4943 		sctp_send_abort(init_pkt, iphlen, sh,
4944 		    init_chk->init.initiate_tag, op_err, vrf_id, port);
4945 		return;
4946 	}
4947 	m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4948 	if (m == NULL) {
4949 		/* No memory, INIT timer will re-attempt. */
4950 		if (op_err)
4951 			sctp_m_freem(op_err);
4952 		return;
4953 	}
4954 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
4955 
4956 	/* the time I built cookie */
4957 	(void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
4958 
4959 	/* populate any tie tags */
4960 	if (asoc != NULL) {
4961 		/* unlock before tag selections */
4962 		stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
4963 		stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
4964 		stc.cookie_life = asoc->cookie_life;
4965 		net = asoc->primary_destination;
4966 	} else {
4967 		stc.tie_tag_my_vtag = 0;
4968 		stc.tie_tag_peer_vtag = 0;
4969 		/* life I will award this cookie */
4970 		stc.cookie_life = inp->sctp_ep.def_cookie_life;
4971 	}
4972 
4973 	/* copy in the ports for later check */
4974 	stc.myport = sh->dest_port;
4975 	stc.peerport = sh->src_port;
4976 
4977 	/*
4978 	 * If we wanted to honor cookie life extentions, we would add to
4979 	 * stc.cookie_life. For now we should NOT honor any extension
4980 	 */
4981 	stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
4982 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4983 		struct inpcb *in_inp;
4984 
4985 		/* Its a V6 socket */
4986 		in_inp = (struct inpcb *)inp;
4987 		stc.ipv6_addr_legal = 1;
4988 		/* Now look at the binding flag to see if V4 will be legal */
4989 		if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
4990 			stc.ipv4_addr_legal = 1;
4991 		} else {
4992 			/* V4 addresses are NOT legal on the association */
4993 			stc.ipv4_addr_legal = 0;
4994 		}
4995 	} else {
4996 		/* Its a V4 socket, no - V6 */
4997 		stc.ipv4_addr_legal = 1;
4998 		stc.ipv6_addr_legal = 0;
4999 	}
5000 
5001 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
5002 	stc.ipv4_scope = 1;
5003 #else
5004 	stc.ipv4_scope = 0;
5005 #endif
5006 	/* now for scope setup */
5007 	memset((caddr_t)&store, 0, sizeof(store));
5008 	memset((caddr_t)&store1, 0, sizeof(store1));
5009 	sin = &store.sin;
5010 	to_sin = &store1.sin;
5011 #ifdef INET6
5012 	sin6 = &store.sin6;
5013 	to_sin6 = &store1.sin6;
5014 #endif
5015 	iph = mtod(init_pkt, struct ip *);
5016 	/* establish the to_addr's */
5017 	switch (iph->ip_v) {
5018 	case IPVERSION:
5019 		to_sin->sin_port = sh->dest_port;
5020 		to_sin->sin_family = AF_INET;
5021 		to_sin->sin_len = sizeof(struct sockaddr_in);
5022 		to_sin->sin_addr = iph->ip_dst;
5023 		break;
5024 #ifdef INET6
5025 	case IPV6_VERSION >> 4:
5026 		ip6 = mtod(init_pkt, struct ip6_hdr *);
5027 		to_sin6->sin6_addr = ip6->ip6_dst;
5028 		to_sin6->sin6_scope_id = 0;
5029 		to_sin6->sin6_port = sh->dest_port;
5030 		to_sin6->sin6_family = AF_INET6;
5031 		to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5032 		break;
5033 #endif
5034 	default:
5035 		goto do_a_abort;
5036 		break;
5037 	};
5038 
5039 	if (net == NULL) {
5040 		to = (struct sockaddr *)&store;
5041 		switch (iph->ip_v) {
5042 		case IPVERSION:
5043 			{
5044 				sin->sin_family = AF_INET;
5045 				sin->sin_len = sizeof(struct sockaddr_in);
5046 				sin->sin_port = sh->src_port;
5047 				sin->sin_addr = iph->ip_src;
5048 				/* lookup address */
5049 				stc.address[0] = sin->sin_addr.s_addr;
5050 				stc.address[1] = 0;
5051 				stc.address[2] = 0;
5052 				stc.address[3] = 0;
5053 				stc.addr_type = SCTP_IPV4_ADDRESS;
5054 				/* local from address */
5055 				stc.laddress[0] = to_sin->sin_addr.s_addr;
5056 				stc.laddress[1] = 0;
5057 				stc.laddress[2] = 0;
5058 				stc.laddress[3] = 0;
5059 				stc.laddr_type = SCTP_IPV4_ADDRESS;
5060 				/* scope_id is only for v6 */
5061 				stc.scope_id = 0;
5062 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5063 				if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5064 					stc.ipv4_scope = 1;
5065 				}
5066 #else
5067 				stc.ipv4_scope = 1;
5068 #endif				/* SCTP_DONT_DO_PRIVADDR_SCOPE */
5069 				/* Must use the address in this case */
5070 				if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5071 					stc.loopback_scope = 1;
5072 					stc.ipv4_scope = 1;
5073 					stc.site_scope = 1;
5074 					stc.local_scope = 0;
5075 				}
5076 				break;
5077 			}
5078 #ifdef INET6
5079 		case IPV6_VERSION >> 4:
5080 			{
5081 				ip6 = mtod(init_pkt, struct ip6_hdr *);
5082 				sin6->sin6_family = AF_INET6;
5083 				sin6->sin6_len = sizeof(struct sockaddr_in6);
5084 				sin6->sin6_port = sh->src_port;
5085 				sin6->sin6_addr = ip6->ip6_src;
5086 				/* lookup address */
5087 				memcpy(&stc.address, &sin6->sin6_addr,
5088 				    sizeof(struct in6_addr));
5089 				sin6->sin6_scope_id = 0;
5090 				stc.addr_type = SCTP_IPV6_ADDRESS;
5091 				stc.scope_id = 0;
5092 				if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5093 					/*
5094 					 * FIX ME: does this have scope from
5095 					 * rcvif?
5096 					 */
5097 					(void)sa6_recoverscope(sin6);
5098 					stc.scope_id = sin6->sin6_scope_id;
5099 					sa6_embedscope(sin6, ip6_use_defzone);
5100 					stc.loopback_scope = 1;
5101 					stc.local_scope = 0;
5102 					stc.site_scope = 1;
5103 					stc.ipv4_scope = 1;
5104 				} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5105 					/*
5106 					 * If the new destination is a
5107 					 * LINK_LOCAL we must have common
5108 					 * both site and local scope. Don't
5109 					 * set local scope though since we
5110 					 * must depend on the source to be
5111 					 * added implicitly. We cannot
5112 					 * assure just because we share one
5113 					 * link that all links are common.
5114 					 */
5115 					stc.local_scope = 0;
5116 					stc.site_scope = 1;
5117 					stc.ipv4_scope = 1;
5118 					/*
5119 					 * we start counting for the private
5120 					 * address stuff at 1. since the
5121 					 * link local we source from won't
5122 					 * show up in our scoped count.
5123 					 */
5124 					cnt_inits_to = 1;
5125 					/*
5126 					 * pull out the scope_id from
5127 					 * incoming pkt
5128 					 */
5129 					/*
5130 					 * FIX ME: does this have scope from
5131 					 * rcvif?
5132 					 */
5133 					(void)sa6_recoverscope(sin6);
5134 					stc.scope_id = sin6->sin6_scope_id;
5135 					sa6_embedscope(sin6, ip6_use_defzone);
5136 				} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5137 					/*
5138 					 * If the new destination is
5139 					 * SITE_LOCAL then we must have site
5140 					 * scope in common.
5141 					 */
5142 					stc.site_scope = 1;
5143 				}
5144 				memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5145 				stc.laddr_type = SCTP_IPV6_ADDRESS;
5146 				break;
5147 			}
5148 #endif
5149 		default:
5150 			/* TSNH */
5151 			goto do_a_abort;
5152 			break;
5153 		}
5154 	} else {
5155 		/* set the scope per the existing tcb */
5156 
5157 #ifdef INET6
5158 		struct sctp_nets *lnet;
5159 
5160 #endif
5161 
5162 		stc.loopback_scope = asoc->loopback_scope;
5163 		stc.ipv4_scope = asoc->ipv4_local_scope;
5164 		stc.site_scope = asoc->site_scope;
5165 		stc.local_scope = asoc->local_scope;
5166 #ifdef INET6
5167 		/* Why do we not consider IPv4 LL addresses? */
5168 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5169 			if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5170 				if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5171 					/*
5172 					 * if we have a LL address, start
5173 					 * counting at 1.
5174 					 */
5175 					cnt_inits_to = 1;
5176 				}
5177 			}
5178 		}
5179 #endif
5180 		/* use the net pointer */
5181 		to = (struct sockaddr *)&net->ro._l_addr;
5182 		switch (to->sa_family) {
5183 		case AF_INET:
5184 			sin = (struct sockaddr_in *)to;
5185 			stc.address[0] = sin->sin_addr.s_addr;
5186 			stc.address[1] = 0;
5187 			stc.address[2] = 0;
5188 			stc.address[3] = 0;
5189 			stc.addr_type = SCTP_IPV4_ADDRESS;
5190 			if (net->src_addr_selected == 0) {
5191 				/*
5192 				 * strange case here, the INIT should have
5193 				 * did the selection.
5194 				 */
5195 				net->ro._s_addr = sctp_source_address_selection(inp,
5196 				    stcb, (sctp_route_t *) & net->ro,
5197 				    net, 0, vrf_id);
5198 				if (net->ro._s_addr == NULL)
5199 					return;
5200 
5201 				net->src_addr_selected = 1;
5202 
5203 			}
5204 			stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5205 			stc.laddress[1] = 0;
5206 			stc.laddress[2] = 0;
5207 			stc.laddress[3] = 0;
5208 			stc.laddr_type = SCTP_IPV4_ADDRESS;
5209 			break;
5210 #ifdef INET6
5211 		case AF_INET6:
5212 			sin6 = (struct sockaddr_in6 *)to;
5213 			memcpy(&stc.address, &sin6->sin6_addr,
5214 			    sizeof(struct in6_addr));
5215 			stc.addr_type = SCTP_IPV6_ADDRESS;
5216 			if (net->src_addr_selected == 0) {
5217 				/*
5218 				 * strange case here, the INIT should have
5219 				 * did the selection.
5220 				 */
5221 				net->ro._s_addr = sctp_source_address_selection(inp,
5222 				    stcb, (sctp_route_t *) & net->ro,
5223 				    net, 0, vrf_id);
5224 				if (net->ro._s_addr == NULL)
5225 					return;
5226 
5227 				net->src_addr_selected = 1;
5228 			}
5229 			memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5230 			    sizeof(struct in6_addr));
5231 			stc.laddr_type = SCTP_IPV6_ADDRESS;
5232 			break;
5233 #endif
5234 		}
5235 	}
5236 	/* Now lets put the SCTP header in place */
5237 	initackm_out = mtod(m, struct sctp_init_msg *);
5238 	initackm_out->sh.src_port = inp->sctp_lport;
5239 	initackm_out->sh.dest_port = sh->src_port;
5240 	initackm_out->sh.v_tag = init_chk->init.initiate_tag;
5241 	/* Save it off for quick ref */
5242 	stc.peers_vtag = init_chk->init.initiate_tag;
5243 	initackm_out->sh.checksum = 0;	/* calculate later */
5244 	/* who are we */
5245 	memcpy(stc.identification, SCTP_VERSION_STRING,
5246 	    min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5247 	/* now the chunk header */
5248 	initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
5249 	initackm_out->msg.ch.chunk_flags = 0;
5250 	/* fill in later from mbuf we build */
5251 	initackm_out->msg.ch.chunk_length = 0;
5252 	/* place in my tag */
5253 	if ((asoc != NULL) &&
5254 	    ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5255 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5256 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5257 		/* re-use the v-tags and init-seq here */
5258 		initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
5259 		initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
5260 	} else {
5261 		uint32_t vtag, itsn;
5262 
5263 		if (hold_inp_lock) {
5264 			SCTP_INP_INCR_REF(inp);
5265 			SCTP_INP_RUNLOCK(inp);
5266 		}
5267 		if (asoc) {
5268 			atomic_add_int(&asoc->refcnt, 1);
5269 			SCTP_TCB_UNLOCK(stcb);
5270 			vtag = sctp_select_a_tag(inp, 1);
5271 			initackm_out->msg.init.initiate_tag = htonl(vtag);
5272 			/* get a TSN to use too */
5273 			itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5274 			initackm_out->msg.init.initial_tsn = htonl(itsn);
5275 			SCTP_TCB_LOCK(stcb);
5276 			atomic_add_int(&asoc->refcnt, -1);
5277 		} else {
5278 			vtag = sctp_select_a_tag(inp, 1);
5279 			initackm_out->msg.init.initiate_tag = htonl(vtag);
5280 			/* get a TSN to use too */
5281 			initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5282 		}
5283 		if (hold_inp_lock) {
5284 			SCTP_INP_RLOCK(inp);
5285 			SCTP_INP_DECR_REF(inp);
5286 		}
5287 	}
5288 	/* save away my tag to */
5289 	stc.my_vtag = initackm_out->msg.init.initiate_tag;
5290 
5291 	/* set up some of the credits. */
5292 	so = inp->sctp_socket;
5293 	if (so == NULL) {
5294 		/* memory problem */
5295 		sctp_m_freem(m);
5296 		return;
5297 	} else {
5298 		initackm_out->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5299 	}
5300 	/* set what I want */
5301 	his_limit = ntohs(init_chk->init.num_inbound_streams);
5302 	/* choose what I want */
5303 	if (asoc != NULL) {
5304 		if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5305 			i_want = asoc->streamoutcnt;
5306 		} else {
5307 			i_want = inp->sctp_ep.pre_open_stream_count;
5308 		}
5309 	} else {
5310 		i_want = inp->sctp_ep.pre_open_stream_count;
5311 	}
5312 	if (his_limit < i_want) {
5313 		/* I Want more :< */
5314 		initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
5315 	} else {
5316 		/* I can have what I want :> */
5317 		initackm_out->msg.init.num_outbound_streams = htons(i_want);
5318 	}
5319 	/* tell him his limt. */
5320 	initackm_out->msg.init.num_inbound_streams =
5321 	    htons(inp->sctp_ep.max_open_streams_intome);
5322 	/* setup the ECN pointer */
5323 
5324 	if (inp->sctp_ep.adaptation_layer_indicator) {
5325 		struct sctp_adaptation_layer_indication *ali;
5326 
5327 		ali = (struct sctp_adaptation_layer_indication *)(
5328 		    (caddr_t)initackm_out + sizeof(*initackm_out));
5329 		ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5330 		ali->ph.param_length = htons(sizeof(*ali));
5331 		ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5332 		SCTP_BUF_LEN(m) += sizeof(*ali);
5333 		ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
5334 		    sizeof(*ali));
5335 	} else {
5336 		ecn = (struct sctp_ecn_supported_param *)(
5337 		    (caddr_t)initackm_out + sizeof(*initackm_out));
5338 	}
5339 
5340 	/* ECN parameter */
5341 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
5342 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5343 		ecn->ph.param_length = htons(sizeof(*ecn));
5344 		SCTP_BUF_LEN(m) += sizeof(*ecn);
5345 
5346 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5347 		    sizeof(*ecn));
5348 	} else {
5349 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5350 	}
5351 	/* And now tell the peer we do  pr-sctp */
5352 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5353 	prsctp->ph.param_length = htons(sizeof(*prsctp));
5354 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
5355 
5356 	/* And now tell the peer we do all the extensions */
5357 	pr_supported = (struct sctp_supported_chunk_types_param *)
5358 	    ((caddr_t)prsctp + sizeof(*prsctp));
5359 
5360 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5361 	num_ext = 0;
5362 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5363 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5364 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5365 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5366 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5367 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5368 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5369 	p_len = sizeof(*pr_supported) + num_ext;
5370 	pr_supported->ph.param_length = htons(p_len);
5371 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5372 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5373 
5374 	/* ECN nonce: And now tell the peer we support ECN nonce */
5375 	if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
5376 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
5377 		    ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
5378 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
5379 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
5380 		SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
5381 	}
5382 	/* add authentication parameters */
5383 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5384 		struct sctp_auth_random *randp;
5385 		struct sctp_auth_hmac_algo *hmacs;
5386 		struct sctp_auth_chunk_list *chunks;
5387 		uint16_t random_len;
5388 
5389 		/* generate and add RANDOM parameter */
5390 		random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5391 		randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5392 		randp->ph.param_type = htons(SCTP_RANDOM);
5393 		p_len = sizeof(*randp) + random_len;
5394 		randp->ph.param_length = htons(p_len);
5395 		SCTP_READ_RANDOM(randp->random_data, random_len);
5396 		/* zero out any padding required */
5397 		bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5398 		SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5399 
5400 		/* add HMAC_ALGO parameter */
5401 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5402 		p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5403 		    (uint8_t *) hmacs->hmac_ids);
5404 		if (p_len > 0) {
5405 			p_len += sizeof(*hmacs);
5406 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5407 			hmacs->ph.param_length = htons(p_len);
5408 			/* zero out any padding required */
5409 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5410 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5411 		}
5412 		/* add CHUNKS parameter */
5413 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5414 		p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5415 		    chunks->chunk_types);
5416 		if (p_len > 0) {
5417 			p_len += sizeof(*chunks);
5418 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5419 			chunks->ph.param_length = htons(p_len);
5420 			/* zero out any padding required */
5421 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5422 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5423 		}
5424 	}
5425 	m_at = m;
5426 	/* now the addresses */
5427 	{
5428 		struct sctp_scoping scp;
5429 
5430 		/*
5431 		 * To optimize this we could put the scoping stuff into a
5432 		 * structure and remove the individual uint8's from the stc
5433 		 * structure. Then we could just sifa in the address within
5434 		 * the stc.. but for now this is a quick hack to get the
5435 		 * address stuff teased apart.
5436 		 */
5437 		scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5438 		scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5439 		scp.loopback_scope = stc.loopback_scope;
5440 		scp.ipv4_local_scope = stc.ipv4_scope;
5441 		scp.local_scope = stc.local_scope;
5442 		scp.site_scope = stc.site_scope;
5443 		m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
5444 	}
5445 
5446 	/* tack on the operational error if present */
5447 	if (op_err) {
5448 		struct mbuf *ol;
5449 		int llen;
5450 
5451 		llen = 0;
5452 		ol = op_err;
5453 		while (ol) {
5454 			llen += SCTP_BUF_LEN(ol);
5455 			ol = SCTP_BUF_NEXT(ol);
5456 		}
5457 		if (llen % 4) {
5458 			/* must add a pad to the param */
5459 			uint32_t cpthis = 0;
5460 			int padlen;
5461 
5462 			padlen = 4 - (llen % 4);
5463 			m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5464 		}
5465 		while (SCTP_BUF_NEXT(m_at) != NULL) {
5466 			m_at = SCTP_BUF_NEXT(m_at);
5467 		}
5468 		SCTP_BUF_NEXT(m_at) = op_err;
5469 		while (SCTP_BUF_NEXT(m_at) != NULL) {
5470 			m_at = SCTP_BUF_NEXT(m_at);
5471 		}
5472 	}
5473 	/* pre-calulate the size and update pkt header and chunk header */
5474 	p_len = 0;
5475 	for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5476 		p_len += SCTP_BUF_LEN(m_tmp);
5477 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5478 			/* m_tmp should now point to last one */
5479 			break;
5480 		}
5481 	}
5482 
5483 	/* Now we must build a cookie */
5484 	m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
5485 	    sizeof(struct sctphdr), &stc, &signature);
5486 	if (m_cookie == NULL) {
5487 		/* memory problem */
5488 		sctp_m_freem(m);
5489 		return;
5490 	}
5491 	/* Now append the cookie to the end and update the space/size */
5492 	SCTP_BUF_NEXT(m_tmp) = m_cookie;
5493 
5494 	for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5495 		p_len += SCTP_BUF_LEN(m_tmp);
5496 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5497 			/* m_tmp should now point to last one */
5498 			mp_last = m_tmp;
5499 			break;
5500 		}
5501 	}
5502 	/*
5503 	 * Place in the size, but we don't include the last pad (if any) in
5504 	 * the INIT-ACK.
5505 	 */
5506 	initackm_out->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
5507 
5508 	/*
5509 	 * Time to sign the cookie, we don't sign over the cookie signature
5510 	 * though thus we set trailer.
5511 	 */
5512 	(void)sctp_hmac_m(SCTP_HMAC,
5513 	    (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
5514 	    SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
5515 	    (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
5516 	/*
5517 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
5518 	 * here since the timer will drive a retranmission.
5519 	 */
5520 	padval = p_len % 4;
5521 	if ((padval) && (mp_last)) {
5522 		/* see my previous comments on mp_last */
5523 		int ret;
5524 
5525 		ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
5526 		if (ret) {
5527 			/* Houston we have a problem, no space */
5528 			sctp_m_freem(m);
5529 			return;
5530 		}
5531 		p_len += padval;
5532 	}
5533 	if (stc.loopback_scope) {
5534 		over_addr = &store1;
5535 	} else {
5536 		over_addr = NULL;
5537 
5538 	}
5539 
5540 	(void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
5541 	    NULL, 0, port, SCTP_SO_NOT_LOCKED, over_addr);
5542 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5543 }
5544 
5545 
5546 void
5547 sctp_insert_on_wheel(struct sctp_tcb *stcb,
5548     struct sctp_association *asoc,
5549     struct sctp_stream_out *strq, int holds_lock)
5550 {
5551 	struct sctp_stream_out *stre, *strn;
5552 
5553 	if (holds_lock == 0) {
5554 		SCTP_TCB_SEND_LOCK(stcb);
5555 	}
5556 	if ((strq->next_spoke.tqe_next) ||
5557 	    (strq->next_spoke.tqe_prev)) {
5558 		/* already on wheel */
5559 		goto outof_here;
5560 	}
5561 	stre = TAILQ_FIRST(&asoc->out_wheel);
5562 	if (stre == NULL) {
5563 		/* only one on wheel */
5564 		TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
5565 		goto outof_here;
5566 	}
5567 	for (; stre; stre = strn) {
5568 		strn = TAILQ_NEXT(stre, next_spoke);
5569 		if (stre->stream_no > strq->stream_no) {
5570 			TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
5571 			goto outof_here;
5572 		} else if (stre->stream_no == strq->stream_no) {
5573 			/* huh, should not happen */
5574 			goto outof_here;
5575 		} else if (strn == NULL) {
5576 			/* next one is null */
5577 			TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
5578 			    next_spoke);
5579 		}
5580 	}
5581 outof_here:
5582 	if (holds_lock == 0) {
5583 		SCTP_TCB_SEND_UNLOCK(stcb);
5584 	}
5585 }
5586 
5587 static void
5588 sctp_remove_from_wheel(struct sctp_tcb *stcb,
5589     struct sctp_association *asoc,
5590     struct sctp_stream_out *strq)
5591 {
5592 	/* take off and then setup so we know it is not on the wheel */
5593 	SCTP_TCB_SEND_LOCK(stcb);
5594 	if (TAILQ_FIRST(&strq->outqueue)) {
5595 		/* more was added */
5596 		SCTP_TCB_SEND_UNLOCK(stcb);
5597 		return;
5598 	}
5599 	TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
5600 	strq->next_spoke.tqe_next = NULL;
5601 	strq->next_spoke.tqe_prev = NULL;
5602 	SCTP_TCB_SEND_UNLOCK(stcb);
5603 }
5604 
5605 static void
5606 sctp_prune_prsctp(struct sctp_tcb *stcb,
5607     struct sctp_association *asoc,
5608     struct sctp_sndrcvinfo *srcv,
5609     int dataout)
5610 {
5611 	int freed_spc = 0;
5612 	struct sctp_tmit_chunk *chk, *nchk;
5613 
5614 	SCTP_TCB_LOCK_ASSERT(stcb);
5615 	if ((asoc->peer_supports_prsctp) &&
5616 	    (asoc->sent_queue_cnt_removeable > 0)) {
5617 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5618 			/*
5619 			 * Look for chunks marked with the PR_SCTP flag AND
5620 			 * the buffer space flag. If the one being sent is
5621 			 * equal or greater priority then purge the old one
5622 			 * and free some space.
5623 			 */
5624 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5625 				/*
5626 				 * This one is PR-SCTP AND buffer space
5627 				 * limited type
5628 				 */
5629 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5630 					/*
5631 					 * Lower numbers equates to higher
5632 					 * priority so if the one we are
5633 					 * looking at has a larger or equal
5634 					 * priority we want to drop the data
5635 					 * and NOT retransmit it.
5636 					 */
5637 					if (chk->data) {
5638 						/*
5639 						 * We release the book_size
5640 						 * if the mbuf is here
5641 						 */
5642 						int ret_spc;
5643 						int cause;
5644 
5645 						if (chk->sent > SCTP_DATAGRAM_UNSENT)
5646 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
5647 						else
5648 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
5649 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5650 						    cause,
5651 						    &asoc->sent_queue, SCTP_SO_LOCKED);
5652 						freed_spc += ret_spc;
5653 						if (freed_spc >= dataout) {
5654 							return;
5655 						}
5656 					}	/* if chunk was present */
5657 				}	/* if of sufficent priority */
5658 			}	/* if chunk has enabled */
5659 		}		/* tailqforeach */
5660 
5661 		chk = TAILQ_FIRST(&asoc->send_queue);
5662 		while (chk) {
5663 			nchk = TAILQ_NEXT(chk, sctp_next);
5664 			/* Here we must move to the sent queue and mark */
5665 			if (PR_SCTP_TTL_ENABLED(chk->flags)) {
5666 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5667 					if (chk->data) {
5668 						/*
5669 						 * We release the book_size
5670 						 * if the mbuf is here
5671 						 */
5672 						int ret_spc;
5673 
5674 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5675 						    SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
5676 						    &asoc->send_queue, SCTP_SO_LOCKED);
5677 
5678 						freed_spc += ret_spc;
5679 						if (freed_spc >= dataout) {
5680 							return;
5681 						}
5682 					}	/* end if chk->data */
5683 				}	/* end if right class */
5684 			}	/* end if chk pr-sctp */
5685 			chk = nchk;
5686 		}		/* end while (chk) */
5687 	}			/* if enabled in asoc */
5688 }
5689 
5690 int
5691 sctp_get_frag_point(struct sctp_tcb *stcb,
5692     struct sctp_association *asoc)
5693 {
5694 	int siz, ovh;
5695 
5696 	/*
5697 	 * For endpoints that have both v6 and v4 addresses we must reserve
5698 	 * room for the ipv6 header, for those that are only dealing with V4
5699 	 * we use a larger frag point.
5700 	 */
5701 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5702 		ovh = SCTP_MED_OVERHEAD;
5703 	} else {
5704 		ovh = SCTP_MED_V4_OVERHEAD;
5705 	}
5706 
5707 	if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
5708 		siz = asoc->smallest_mtu - ovh;
5709 	else
5710 		siz = (stcb->asoc.sctp_frag_point - ovh);
5711 	/*
5712 	 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
5713 	 */
5714 	/* A data chunk MUST fit in a cluster */
5715 	/* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
5716 	/* } */
5717 
5718 	/* adjust for an AUTH chunk if DATA requires auth */
5719 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
5720 		siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5721 
5722 	if (siz % 4) {
5723 		/* make it an even word boundary please */
5724 		siz -= (siz % 4);
5725 	}
5726 	return (siz);
5727 }
5728 
5729 static void
5730 sctp_set_prsctp_policy(struct sctp_tcb *stcb,
5731     struct sctp_stream_queue_pending *sp)
5732 {
5733 	sp->pr_sctp_on = 0;
5734 	if (stcb->asoc.peer_supports_prsctp) {
5735 		/*
5736 		 * We assume that the user wants PR_SCTP_TTL if the user
5737 		 * provides a positive lifetime but does not specify any
5738 		 * PR_SCTP policy. This is a BAD assumption and causes
5739 		 * problems at least with the U-Vancovers MPI folks. I will
5740 		 * change this to be no policy means NO PR-SCTP.
5741 		 */
5742 		if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
5743 			sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
5744 			sp->pr_sctp_on = 1;
5745 		} else {
5746 			return;
5747 		}
5748 		switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
5749 		case CHUNK_FLAGS_PR_SCTP_BUF:
5750 			/*
5751 			 * Time to live is a priority stored in tv_sec when
5752 			 * doing the buffer drop thing.
5753 			 */
5754 			sp->ts.tv_sec = sp->timetolive;
5755 			sp->ts.tv_usec = 0;
5756 			break;
5757 		case CHUNK_FLAGS_PR_SCTP_TTL:
5758 			{
5759 				struct timeval tv;
5760 
5761 				(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5762 				tv.tv_sec = sp->timetolive / 1000;
5763 				tv.tv_usec = (sp->timetolive * 1000) % 1000000;
5764 				timevaladd(&sp->ts, &tv);
5765 			}
5766 			break;
5767 		case CHUNK_FLAGS_PR_SCTP_RTX:
5768 			/*
5769 			 * Time to live is a the number or retransmissions
5770 			 * stored in tv_sec.
5771 			 */
5772 			sp->ts.tv_sec = sp->timetolive;
5773 			sp->ts.tv_usec = 0;
5774 			break;
5775 		default:
5776 			SCTPDBG(SCTP_DEBUG_USRREQ1,
5777 			    "Unknown PR_SCTP policy %u.\n",
5778 			    PR_SCTP_POLICY(sp->sinfo_flags));
5779 			break;
5780 		}
5781 	}
5782 }
5783 
5784 static int
5785 sctp_msg_append(struct sctp_tcb *stcb,
5786     struct sctp_nets *net,
5787     struct mbuf *m,
5788     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5789 {
5790 	int error = 0, holds_lock;
5791 	struct mbuf *at;
5792 	struct sctp_stream_queue_pending *sp = NULL;
5793 	struct sctp_stream_out *strm;
5794 
5795 	/*
5796 	 * Given an mbuf chain, put it into the association send queue and
5797 	 * place it on the wheel
5798 	 */
5799 	holds_lock = hold_stcb_lock;
5800 	if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5801 		/* Invalid stream number */
5802 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5803 		error = EINVAL;
5804 		goto out_now;
5805 	}
5806 	if ((stcb->asoc.stream_locked) &&
5807 	    (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5808 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5809 		error = EINVAL;
5810 		goto out_now;
5811 	}
5812 	strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5813 	/* Now can we send this? */
5814 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5815 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5816 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5817 	    (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5818 		/* got data while shutting down */
5819 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
5820 		error = ECONNRESET;
5821 		goto out_now;
5822 	}
5823 	sctp_alloc_a_strmoq(stcb, sp);
5824 	if (sp == NULL) {
5825 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5826 		error = ENOMEM;
5827 		goto out_now;
5828 	}
5829 	sp->sinfo_flags = srcv->sinfo_flags;
5830 	sp->timetolive = srcv->sinfo_timetolive;
5831 	sp->ppid = srcv->sinfo_ppid;
5832 	sp->context = srcv->sinfo_context;
5833 	sp->strseq = 0;
5834 	if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5835 		sp->net = net;
5836 		sp->addr_over = 1;
5837 	} else {
5838 		sp->net = stcb->asoc.primary_destination;
5839 		sp->addr_over = 0;
5840 	}
5841 	atomic_add_int(&sp->net->ref_count, 1);
5842 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5843 	sp->stream = srcv->sinfo_stream;
5844 	sp->msg_is_complete = 1;
5845 	sp->sender_all_done = 1;
5846 	sp->some_taken = 0;
5847 	sp->data = m;
5848 	sp->tail_mbuf = NULL;
5849 	sp->length = 0;
5850 	at = m;
5851 	sctp_set_prsctp_policy(stcb, sp);
5852 	/*
5853 	 * We could in theory (for sendall) sifa the length in, but we would
5854 	 * still have to hunt through the chain since we need to setup the
5855 	 * tail_mbuf
5856 	 */
5857 	while (at) {
5858 		if (SCTP_BUF_NEXT(at) == NULL)
5859 			sp->tail_mbuf = at;
5860 		sp->length += SCTP_BUF_LEN(at);
5861 		at = SCTP_BUF_NEXT(at);
5862 	}
5863 	SCTP_TCB_SEND_LOCK(stcb);
5864 	sctp_snd_sb_alloc(stcb, sp->length);
5865 	atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
5866 	TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
5867 	if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
5868 		sp->strseq = strm->next_sequence_sent;
5869 		strm->next_sequence_sent++;
5870 	}
5871 	if ((strm->next_spoke.tqe_next == NULL) &&
5872 	    (strm->next_spoke.tqe_prev == NULL)) {
5873 		/* Not on wheel, insert */
5874 		sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
5875 	}
5876 	m = NULL;
5877 	SCTP_TCB_SEND_UNLOCK(stcb);
5878 out_now:
5879 	if (m) {
5880 		sctp_m_freem(m);
5881 	}
5882 	return (error);
5883 }
5884 
5885 
5886 static struct mbuf *
5887 sctp_copy_mbufchain(struct mbuf *clonechain,
5888     struct mbuf *outchain,
5889     struct mbuf **endofchain,
5890     int can_take_mbuf,
5891     int sizeofcpy,
5892     uint8_t copy_by_ref)
5893 {
5894 	struct mbuf *m;
5895 	struct mbuf *appendchain;
5896 	caddr_t cp;
5897 	int len;
5898 
5899 	if (endofchain == NULL) {
5900 		/* error */
5901 error_out:
5902 		if (outchain)
5903 			sctp_m_freem(outchain);
5904 		return (NULL);
5905 	}
5906 	if (can_take_mbuf) {
5907 		appendchain = clonechain;
5908 	} else {
5909 		if (!copy_by_ref &&
5910 		    (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
5911 		    ) {
5912 			/* Its not in a cluster */
5913 			if (*endofchain == NULL) {
5914 				/* lets get a mbuf cluster */
5915 				if (outchain == NULL) {
5916 					/* This is the general case */
5917 			new_mbuf:
5918 					outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5919 					if (outchain == NULL) {
5920 						goto error_out;
5921 					}
5922 					SCTP_BUF_LEN(outchain) = 0;
5923 					*endofchain = outchain;
5924 					/* get the prepend space */
5925 					SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
5926 				} else {
5927 					/*
5928 					 * We really should not get a NULL
5929 					 * in endofchain
5930 					 */
5931 					/* find end */
5932 					m = outchain;
5933 					while (m) {
5934 						if (SCTP_BUF_NEXT(m) == NULL) {
5935 							*endofchain = m;
5936 							break;
5937 						}
5938 						m = SCTP_BUF_NEXT(m);
5939 					}
5940 					/* sanity */
5941 					if (*endofchain == NULL) {
5942 						/*
5943 						 * huh, TSNH XXX maybe we
5944 						 * should panic
5945 						 */
5946 						sctp_m_freem(outchain);
5947 						goto new_mbuf;
5948 					}
5949 				}
5950 				/* get the new end of length */
5951 				len = M_TRAILINGSPACE(*endofchain);
5952 			} else {
5953 				/* how much is left at the end? */
5954 				len = M_TRAILINGSPACE(*endofchain);
5955 			}
5956 			/* Find the end of the data, for appending */
5957 			cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
5958 
5959 			/* Now lets copy it out */
5960 			if (len >= sizeofcpy) {
5961 				/* It all fits, copy it in */
5962 				m_copydata(clonechain, 0, sizeofcpy, cp);
5963 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5964 			} else {
5965 				/* fill up the end of the chain */
5966 				if (len > 0) {
5967 					m_copydata(clonechain, 0, len, cp);
5968 					SCTP_BUF_LEN((*endofchain)) += len;
5969 					/* now we need another one */
5970 					sizeofcpy -= len;
5971 				}
5972 				m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5973 				if (m == NULL) {
5974 					/* We failed */
5975 					goto error_out;
5976 				}
5977 				SCTP_BUF_NEXT((*endofchain)) = m;
5978 				*endofchain = m;
5979 				cp = mtod((*endofchain), caddr_t);
5980 				m_copydata(clonechain, len, sizeofcpy, cp);
5981 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5982 			}
5983 			return (outchain);
5984 		} else {
5985 			/* copy the old fashion way */
5986 			appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
5987 #ifdef SCTP_MBUF_LOGGING
5988 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5989 				struct mbuf *mat;
5990 
5991 				mat = appendchain;
5992 				while (mat) {
5993 					if (SCTP_BUF_IS_EXTENDED(mat)) {
5994 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5995 					}
5996 					mat = SCTP_BUF_NEXT(mat);
5997 				}
5998 			}
5999 #endif
6000 		}
6001 	}
6002 	if (appendchain == NULL) {
6003 		/* error */
6004 		if (outchain)
6005 			sctp_m_freem(outchain);
6006 		return (NULL);
6007 	}
6008 	if (outchain) {
6009 		/* tack on to the end */
6010 		if (*endofchain != NULL) {
6011 			SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6012 		} else {
6013 			m = outchain;
6014 			while (m) {
6015 				if (SCTP_BUF_NEXT(m) == NULL) {
6016 					SCTP_BUF_NEXT(m) = appendchain;
6017 					break;
6018 				}
6019 				m = SCTP_BUF_NEXT(m);
6020 			}
6021 		}
6022 		/*
6023 		 * save off the end and update the end-chain postion
6024 		 */
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 (outchain);
6034 	} else {
6035 		/* save off the end and update the end-chain postion */
6036 		m = appendchain;
6037 		while (m) {
6038 			if (SCTP_BUF_NEXT(m) == NULL) {
6039 				*endofchain = m;
6040 				break;
6041 			}
6042 			m = SCTP_BUF_NEXT(m);
6043 		}
6044 		return (appendchain);
6045 	}
6046 }
6047 
6048 int
6049 sctp_med_chunk_output(struct sctp_inpcb *inp,
6050     struct sctp_tcb *stcb,
6051     struct sctp_association *asoc,
6052     int *num_out,
6053     int *reason_code,
6054     int control_only, int *cwnd_full, int from_where,
6055     struct timeval *now, int *now_filled, int frag_point, int so_locked
6056 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6057     SCTP_UNUSED
6058 #endif
6059 );
6060 
6061 static void
6062 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6063     uint32_t val)
6064 {
6065 	struct sctp_copy_all *ca;
6066 	struct mbuf *m;
6067 	int ret = 0;
6068 	int added_control = 0;
6069 	int un_sent, do_chunk_output = 1;
6070 	struct sctp_association *asoc;
6071 
6072 	ca = (struct sctp_copy_all *)ptr;
6073 	if (ca->m == NULL) {
6074 		return;
6075 	}
6076 	if (ca->inp != inp) {
6077 		/* TSNH */
6078 		return;
6079 	}
6080 	if ((ca->m) && ca->sndlen) {
6081 		m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6082 		if (m == NULL) {
6083 			/* can't copy so we are done */
6084 			ca->cnt_failed++;
6085 			return;
6086 		}
6087 #ifdef SCTP_MBUF_LOGGING
6088 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6089 			struct mbuf *mat;
6090 
6091 			mat = m;
6092 			while (mat) {
6093 				if (SCTP_BUF_IS_EXTENDED(mat)) {
6094 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6095 				}
6096 				mat = SCTP_BUF_NEXT(mat);
6097 			}
6098 		}
6099 #endif
6100 	} else {
6101 		m = NULL;
6102 	}
6103 	SCTP_TCB_LOCK_ASSERT(stcb);
6104 	if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6105 		/* Abort this assoc with m as the user defined reason */
6106 		if (m) {
6107 			struct sctp_paramhdr *ph;
6108 
6109 			SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6110 			if (m) {
6111 				ph = mtod(m, struct sctp_paramhdr *);
6112 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6113 				ph->param_length = htons(ca->sndlen);
6114 			}
6115 			/*
6116 			 * We add one here to keep the assoc from
6117 			 * dis-appearing on us.
6118 			 */
6119 			atomic_add_int(&stcb->asoc.refcnt, 1);
6120 			sctp_abort_an_association(inp, stcb,
6121 			    SCTP_RESPONSE_TO_USER_REQ,
6122 			    m, SCTP_SO_NOT_LOCKED);
6123 			/*
6124 			 * sctp_abort_an_association calls sctp_free_asoc()
6125 			 * free association will NOT free it since we
6126 			 * incremented the refcnt .. we do this to prevent
6127 			 * it being freed and things getting tricky since we
6128 			 * could end up (from free_asoc) calling inpcb_free
6129 			 * which would get a recursive lock call to the
6130 			 * iterator lock.. But as a consequence of that the
6131 			 * stcb will return to us un-locked.. since
6132 			 * free_asoc returns with either no TCB or the TCB
6133 			 * unlocked, we must relock.. to unlock in the
6134 			 * iterator timer :-0
6135 			 */
6136 			SCTP_TCB_LOCK(stcb);
6137 			atomic_add_int(&stcb->asoc.refcnt, -1);
6138 			goto no_chunk_output;
6139 		}
6140 	} else {
6141 		if (m) {
6142 			ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
6143 			    &ca->sndrcv, 1);
6144 		}
6145 		asoc = &stcb->asoc;
6146 		if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6147 			/* shutdown this assoc */
6148 			int cnt;
6149 
6150 			cnt = sctp_is_there_unsent_data(stcb);
6151 
6152 			if (TAILQ_EMPTY(&asoc->send_queue) &&
6153 			    TAILQ_EMPTY(&asoc->sent_queue) &&
6154 			    (cnt == 0)) {
6155 				if (asoc->locked_on_sending) {
6156 					goto abort_anyway;
6157 				}
6158 				/*
6159 				 * there is nothing queued to send, so I'm
6160 				 * done...
6161 				 */
6162 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6163 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6164 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6165 					/*
6166 					 * only send SHUTDOWN the first time
6167 					 * through
6168 					 */
6169 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
6170 					if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6171 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6172 					}
6173 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6174 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6175 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6176 					    asoc->primary_destination);
6177 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6178 					    asoc->primary_destination);
6179 					added_control = 1;
6180 					do_chunk_output = 0;
6181 				}
6182 			} else {
6183 				/*
6184 				 * we still got (or just got) data to send,
6185 				 * so set SHUTDOWN_PENDING
6186 				 */
6187 				/*
6188 				 * XXX sockets draft says that SCTP_EOF
6189 				 * should be sent with no data.  currently,
6190 				 * we will allow user data to be sent first
6191 				 * and move to SHUTDOWN-PENDING
6192 				 */
6193 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6194 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6195 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6196 					if (asoc->locked_on_sending) {
6197 						/*
6198 						 * Locked to send out the
6199 						 * data
6200 						 */
6201 						struct sctp_stream_queue_pending *sp;
6202 
6203 						sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6204 						if (sp) {
6205 							if ((sp->length == 0) && (sp->msg_is_complete == 0))
6206 								asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6207 						}
6208 					}
6209 					asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6210 					if (TAILQ_EMPTY(&asoc->send_queue) &&
6211 					    TAILQ_EMPTY(&asoc->sent_queue) &&
6212 					    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6213 				abort_anyway:
6214 						atomic_add_int(&stcb->asoc.refcnt, 1);
6215 						sctp_abort_an_association(stcb->sctp_ep, stcb,
6216 						    SCTP_RESPONSE_TO_USER_REQ,
6217 						    NULL, SCTP_SO_NOT_LOCKED);
6218 						atomic_add_int(&stcb->asoc.refcnt, -1);
6219 						goto no_chunk_output;
6220 					}
6221 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6222 					    asoc->primary_destination);
6223 				}
6224 			}
6225 
6226 		}
6227 	}
6228 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6229 	    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6230 
6231 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6232 	    (stcb->asoc.total_flight > 0) &&
6233 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6234 	    ) {
6235 		do_chunk_output = 0;
6236 	}
6237 	if (do_chunk_output)
6238 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6239 	else if (added_control) {
6240 		int num_out = 0, reason = 0, cwnd_full = 0, now_filled = 0;
6241 		struct timeval now;
6242 		int frag_point;
6243 
6244 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6245 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6246 		    &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6247 	}
6248 no_chunk_output:
6249 	if (ret) {
6250 		ca->cnt_failed++;
6251 	} else {
6252 		ca->cnt_sent++;
6253 	}
6254 }
6255 
6256 static void
6257 sctp_sendall_completes(void *ptr, uint32_t val)
6258 {
6259 	struct sctp_copy_all *ca;
6260 
6261 	ca = (struct sctp_copy_all *)ptr;
6262 	/*
6263 	 * Do a notify here? Kacheong suggests that the notify be done at
6264 	 * the send time.. so you would push up a notification if any send
6265 	 * failed. Don't know if this is feasable since the only failures we
6266 	 * have is "memory" related and if you cannot get an mbuf to send
6267 	 * the data you surely can't get an mbuf to send up to notify the
6268 	 * user you can't send the data :->
6269 	 */
6270 
6271 	/* now free everything */
6272 	sctp_m_freem(ca->m);
6273 	SCTP_FREE(ca, SCTP_M_COPYAL);
6274 }
6275 
6276 
6277 #define	MC_ALIGN(m, len) do {						\
6278 	SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1));	\
6279 } while (0)
6280 
6281 
6282 
6283 static struct mbuf *
6284 sctp_copy_out_all(struct uio *uio, int len)
6285 {
6286 	struct mbuf *ret, *at;
6287 	int left, willcpy, cancpy, error;
6288 
6289 	ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6290 	if (ret == NULL) {
6291 		/* TSNH */
6292 		return (NULL);
6293 	}
6294 	left = len;
6295 	SCTP_BUF_LEN(ret) = 0;
6296 	/* save space for the data chunk header */
6297 	cancpy = M_TRAILINGSPACE(ret);
6298 	willcpy = min(cancpy, left);
6299 	at = ret;
6300 	while (left > 0) {
6301 		/* Align data to the end */
6302 		error = uiomove(mtod(at, caddr_t), willcpy, uio);
6303 		if (error) {
6304 	err_out_now:
6305 			sctp_m_freem(at);
6306 			return (NULL);
6307 		}
6308 		SCTP_BUF_LEN(at) = willcpy;
6309 		SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6310 		left -= willcpy;
6311 		if (left > 0) {
6312 			SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6313 			if (SCTP_BUF_NEXT(at) == NULL) {
6314 				goto err_out_now;
6315 			}
6316 			at = SCTP_BUF_NEXT(at);
6317 			SCTP_BUF_LEN(at) = 0;
6318 			cancpy = M_TRAILINGSPACE(at);
6319 			willcpy = min(cancpy, left);
6320 		}
6321 	}
6322 	return (ret);
6323 }
6324 
6325 static int
6326 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6327     struct sctp_sndrcvinfo *srcv)
6328 {
6329 	int ret;
6330 	struct sctp_copy_all *ca;
6331 
6332 	SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6333 	    SCTP_M_COPYAL);
6334 	if (ca == NULL) {
6335 		sctp_m_freem(m);
6336 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6337 		return (ENOMEM);
6338 	}
6339 	memset(ca, 0, sizeof(struct sctp_copy_all));
6340 
6341 	ca->inp = inp;
6342 	memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6343 	/*
6344 	 * take off the sendall flag, it would be bad if we failed to do
6345 	 * this :-0
6346 	 */
6347 	ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6348 	/* get length and mbuf chain */
6349 	if (uio) {
6350 		ca->sndlen = uio->uio_resid;
6351 		ca->m = sctp_copy_out_all(uio, ca->sndlen);
6352 		if (ca->m == NULL) {
6353 			SCTP_FREE(ca, SCTP_M_COPYAL);
6354 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6355 			return (ENOMEM);
6356 		}
6357 	} else {
6358 		/* Gather the length of the send */
6359 		struct mbuf *mat;
6360 
6361 		mat = m;
6362 		ca->sndlen = 0;
6363 		while (m) {
6364 			ca->sndlen += SCTP_BUF_LEN(m);
6365 			m = SCTP_BUF_NEXT(m);
6366 		}
6367 		ca->m = mat;
6368 	}
6369 	ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6370 	    SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6371 	    SCTP_ASOC_ANY_STATE,
6372 	    (void *)ca, 0,
6373 	    sctp_sendall_completes, inp, 1);
6374 	if (ret) {
6375 		SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6376 		SCTP_FREE(ca, SCTP_M_COPYAL);
6377 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6378 		return (EFAULT);
6379 	}
6380 	return (0);
6381 }
6382 
6383 
6384 void
6385 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6386 {
6387 	struct sctp_tmit_chunk *chk, *nchk;
6388 
6389 	chk = TAILQ_FIRST(&asoc->control_send_queue);
6390 	while (chk) {
6391 		nchk = TAILQ_NEXT(chk, sctp_next);
6392 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6393 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6394 			if (chk->data) {
6395 				sctp_m_freem(chk->data);
6396 				chk->data = NULL;
6397 			}
6398 			asoc->ctrl_queue_cnt--;
6399 			sctp_free_a_chunk(stcb, chk);
6400 		}
6401 		chk = nchk;
6402 	}
6403 }
6404 
6405 void
6406 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6407 {
6408 	struct sctp_association *asoc;
6409 	struct sctp_tmit_chunk *chk, *chk_tmp;
6410 	struct sctp_asconf_chunk *acp;
6411 
6412 	asoc = &stcb->asoc;
6413 	for (chk = TAILQ_FIRST(&asoc->asconf_send_queue); chk != NULL;
6414 	    chk = chk_tmp) {
6415 		/* get next chk */
6416 		chk_tmp = TAILQ_NEXT(chk, sctp_next);
6417 		/* find SCTP_ASCONF chunk in queue */
6418 		if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6419 			if (chk->data) {
6420 				acp = mtod(chk->data, struct sctp_asconf_chunk *);
6421 				if (compare_with_wrap(ntohl(acp->serial_number), stcb->asoc.asconf_seq_out_acked, MAX_SEQ)) {
6422 					/* Not Acked yet */
6423 					break;
6424 				}
6425 			}
6426 			TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6427 			if (chk->data) {
6428 				sctp_m_freem(chk->data);
6429 				chk->data = NULL;
6430 			}
6431 			asoc->ctrl_queue_cnt--;
6432 			sctp_free_a_chunk(stcb, chk);
6433 		}
6434 	}
6435 }
6436 
6437 
6438 static void
6439 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6440 
6441     struct sctp_association *asoc,
6442     struct sctp_tmit_chunk **data_list,
6443     int bundle_at,
6444     struct sctp_nets *net)
6445 {
6446 	int i;
6447 	struct sctp_tmit_chunk *tp1;
6448 
6449 	for (i = 0; i < bundle_at; i++) {
6450 		/* off of the send queue */
6451 		if (i) {
6452 			/*
6453 			 * Any chunk NOT 0 you zap the time chunk 0 gets
6454 			 * zapped or set based on if a RTO measurment is
6455 			 * needed.
6456 			 */
6457 			data_list[i]->do_rtt = 0;
6458 		}
6459 		/* record time */
6460 		data_list[i]->sent_rcv_time = net->last_sent_time;
6461 		data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6462 		TAILQ_REMOVE(&asoc->send_queue,
6463 		    data_list[i],
6464 		    sctp_next);
6465 		/* on to the sent queue */
6466 		tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6467 		if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
6468 		    data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
6469 			struct sctp_tmit_chunk *tpp;
6470 
6471 			/* need to move back */
6472 	back_up_more:
6473 			tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6474 			if (tpp == NULL) {
6475 				TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6476 				goto all_done;
6477 			}
6478 			tp1 = tpp;
6479 			if (compare_with_wrap(tp1->rec.data.TSN_seq,
6480 			    data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
6481 				goto back_up_more;
6482 			}
6483 			TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6484 		} else {
6485 			TAILQ_INSERT_TAIL(&asoc->sent_queue,
6486 			    data_list[i],
6487 			    sctp_next);
6488 		}
6489 all_done:
6490 		/* This does not lower until the cum-ack passes it */
6491 		asoc->sent_queue_cnt++;
6492 		asoc->send_queue_cnt--;
6493 		if ((asoc->peers_rwnd <= 0) &&
6494 		    (asoc->total_flight == 0) &&
6495 		    (bundle_at == 1)) {
6496 			/* Mark the chunk as being a window probe */
6497 			SCTP_STAT_INCR(sctps_windowprobed);
6498 		}
6499 #ifdef SCTP_AUDITING_ENABLED
6500 		sctp_audit_log(0xC2, 3);
6501 #endif
6502 		data_list[i]->sent = SCTP_DATAGRAM_SENT;
6503 		data_list[i]->snd_count = 1;
6504 		data_list[i]->rec.data.chunk_was_revoked = 0;
6505 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6506 			sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6507 			    data_list[i]->whoTo->flight_size,
6508 			    data_list[i]->book_size,
6509 			    (uintptr_t) data_list[i]->whoTo,
6510 			    data_list[i]->rec.data.TSN_seq);
6511 		}
6512 		sctp_flight_size_increase(data_list[i]);
6513 		sctp_total_flight_increase(stcb, data_list[i]);
6514 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6515 			sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6516 			    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6517 		}
6518 		asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6519 		    (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6520 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6521 			/* SWS sender side engages */
6522 			asoc->peers_rwnd = 0;
6523 		}
6524 	}
6525 }
6526 
6527 static void
6528 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
6529 {
6530 	struct sctp_tmit_chunk *chk, *nchk;
6531 
6532 	for (chk = TAILQ_FIRST(&asoc->control_send_queue);
6533 	    chk; chk = nchk) {
6534 		nchk = TAILQ_NEXT(chk, sctp_next);
6535 		if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6536 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6537 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6538 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6539 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6540 		    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6541 		    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6542 		    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6543 		    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6544 		    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6545 			/* Stray chunks must be cleaned up */
6546 	clean_up_anyway:
6547 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6548 			if (chk->data) {
6549 				sctp_m_freem(chk->data);
6550 				chk->data = NULL;
6551 			}
6552 			asoc->ctrl_queue_cnt--;
6553 			sctp_free_a_chunk(stcb, chk);
6554 		} else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6555 			/* special handling, we must look into the param */
6556 			if (chk != asoc->str_reset) {
6557 				goto clean_up_anyway;
6558 			}
6559 		}
6560 	}
6561 }
6562 
6563 
6564 static int
6565 sctp_can_we_split_this(struct sctp_tcb *stcb,
6566     uint32_t length,
6567     uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6568 {
6569 	/*
6570 	 * Make a decision on if I should split a msg into multiple parts.
6571 	 * This is only asked of incomplete messages.
6572 	 */
6573 	if (eeor_on) {
6574 		/*
6575 		 * If we are doing EEOR we need to always send it if its the
6576 		 * entire thing, since it might be all the guy is putting in
6577 		 * the hopper.
6578 		 */
6579 		if (goal_mtu >= length) {
6580 			/*-
6581 			 * If we have data outstanding,
6582 			 * we get another chance when the sack
6583 			 * arrives to transmit - wait for more data
6584 			 */
6585 			if (stcb->asoc.total_flight == 0) {
6586 				/*
6587 				 * If nothing is in flight, we zero the
6588 				 * packet counter.
6589 				 */
6590 				return (length);
6591 			}
6592 			return (0);
6593 
6594 		} else {
6595 			/* You can fill the rest */
6596 			return (goal_mtu);
6597 		}
6598 	}
6599 	/*-
6600 	 * For those strange folk that make the send buffer
6601 	 * smaller than our fragmentation point, we can't
6602 	 * get a full msg in so we have to allow splitting.
6603 	 */
6604 	if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
6605 		return (length);
6606 	}
6607 	if ((length <= goal_mtu) ||
6608 	    ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
6609 		/* Sub-optimial residual don't split in non-eeor mode. */
6610 		return (0);
6611 	}
6612 	/*
6613 	 * If we reach here length is larger than the goal_mtu. Do we wish
6614 	 * to split it for the sake of packet putting together?
6615 	 */
6616 	if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
6617 		/* Its ok to split it */
6618 		return (min(goal_mtu, frag_point));
6619 	}
6620 	/* Nope, can't split */
6621 	return (0);
6622 
6623 }
6624 
6625 static uint32_t
6626 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
6627     struct sctp_stream_out *strq,
6628     uint32_t goal_mtu,
6629     uint32_t frag_point,
6630     int *locked,
6631     int *giveup,
6632     int eeor_mode,
6633     int *bail)
6634 {
6635 	/* Move from the stream to the send_queue keeping track of the total */
6636 	struct sctp_association *asoc;
6637 	struct sctp_stream_queue_pending *sp;
6638 	struct sctp_tmit_chunk *chk;
6639 	struct sctp_data_chunk *dchkh;
6640 	uint32_t to_move, length;
6641 	uint8_t rcv_flags = 0;
6642 	uint8_t some_taken;
6643 	uint8_t send_lock_up = 0;
6644 
6645 	SCTP_TCB_LOCK_ASSERT(stcb);
6646 	asoc = &stcb->asoc;
6647 one_more_time:
6648 	/* sa_ignore FREED_MEMORY */
6649 	sp = TAILQ_FIRST(&strq->outqueue);
6650 	if (sp == NULL) {
6651 		*locked = 0;
6652 		if (send_lock_up == 0) {
6653 			SCTP_TCB_SEND_LOCK(stcb);
6654 			send_lock_up = 1;
6655 		}
6656 		sp = TAILQ_FIRST(&strq->outqueue);
6657 		if (sp) {
6658 			goto one_more_time;
6659 		}
6660 		if (strq->last_msg_incomplete) {
6661 			SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6662 			    strq->stream_no,
6663 			    strq->last_msg_incomplete);
6664 			strq->last_msg_incomplete = 0;
6665 		}
6666 		to_move = 0;
6667 		if (send_lock_up) {
6668 			SCTP_TCB_SEND_UNLOCK(stcb);
6669 			send_lock_up = 0;
6670 		}
6671 		goto out_of;
6672 	}
6673 	if ((sp->msg_is_complete) && (sp->length == 0)) {
6674 		if (sp->sender_all_done) {
6675 			/*
6676 			 * We are doing differed cleanup. Last time through
6677 			 * when we took all the data the sender_all_done was
6678 			 * not set.
6679 			 */
6680 			if (sp->put_last_out == 0) {
6681 				SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6682 				SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6683 				    sp->sender_all_done,
6684 				    sp->length,
6685 				    sp->msg_is_complete,
6686 				    sp->put_last_out,
6687 				    send_lock_up);
6688 			}
6689 			if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
6690 				SCTP_TCB_SEND_LOCK(stcb);
6691 				send_lock_up = 1;
6692 			}
6693 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6694 			TAILQ_REMOVE(&strq->outqueue, sp, next);
6695 			sctp_free_remote_addr(sp->net);
6696 			if (sp->data) {
6697 				sctp_m_freem(sp->data);
6698 				sp->data = NULL;
6699 			}
6700 			sctp_free_a_strmoq(stcb, sp);
6701 
6702 			/* we can't be locked to it */
6703 			*locked = 0;
6704 			stcb->asoc.locked_on_sending = NULL;
6705 			if (send_lock_up) {
6706 				SCTP_TCB_SEND_UNLOCK(stcb);
6707 				send_lock_up = 0;
6708 			}
6709 			/* back to get the next msg */
6710 			goto one_more_time;
6711 		} else {
6712 			/*
6713 			 * sender just finished this but still holds a
6714 			 * reference
6715 			 */
6716 			*locked = 1;
6717 			*giveup = 1;
6718 			to_move = 0;
6719 			goto out_of;
6720 		}
6721 	} else {
6722 		/* is there some to get */
6723 		if (sp->length == 0) {
6724 			/* no */
6725 			*locked = 1;
6726 			*giveup = 1;
6727 			to_move = 0;
6728 			goto out_of;
6729 		}
6730 	}
6731 	some_taken = sp->some_taken;
6732 	if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6733 		sp->msg_is_complete = 1;
6734 	}
6735 re_look:
6736 	length = sp->length;
6737 	if (sp->msg_is_complete) {
6738 		/* The message is complete */
6739 		to_move = min(length, frag_point);
6740 		if (to_move == length) {
6741 			/* All of it fits in the MTU */
6742 			if (sp->some_taken) {
6743 				rcv_flags |= SCTP_DATA_LAST_FRAG;
6744 				sp->put_last_out = 1;
6745 			} else {
6746 				rcv_flags |= SCTP_DATA_NOT_FRAG;
6747 				sp->put_last_out = 1;
6748 			}
6749 		} else {
6750 			/* Not all of it fits, we fragment */
6751 			if (sp->some_taken == 0) {
6752 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
6753 			}
6754 			sp->some_taken = 1;
6755 		}
6756 	} else {
6757 		to_move = sctp_can_we_split_this(stcb, length, goal_mtu,
6758 		    frag_point, eeor_mode);
6759 		if (to_move) {
6760 			/*-
6761 		         * We use a snapshot of length in case it
6762 		         * is expanding during the compare.
6763 		         */
6764 			uint32_t llen;
6765 
6766 			llen = length;
6767 			if (to_move >= llen) {
6768 				to_move = llen;
6769 				if (send_lock_up == 0) {
6770 					/*-
6771 				         * We are taking all of an incomplete msg
6772 				         * thus we need a send lock.
6773 				         */
6774 					SCTP_TCB_SEND_LOCK(stcb);
6775 					send_lock_up = 1;
6776 					if (sp->msg_is_complete) {
6777 						/*
6778 						 * the sender finished the
6779 						 * msg
6780 						 */
6781 						goto re_look;
6782 					}
6783 				}
6784 			}
6785 			if (sp->some_taken == 0) {
6786 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
6787 				sp->some_taken = 1;
6788 			}
6789 		} else {
6790 			/* Nothing to take. */
6791 			if (sp->some_taken) {
6792 				*locked = 1;
6793 			}
6794 			*giveup = 1;
6795 			to_move = 0;
6796 			goto out_of;
6797 		}
6798 	}
6799 
6800 	/* If we reach here, we can copy out a chunk */
6801 	sctp_alloc_a_chunk(stcb, chk);
6802 	if (chk == NULL) {
6803 		/* No chunk memory */
6804 		*giveup = 1;
6805 		to_move = 0;
6806 		goto out_of;
6807 	}
6808 	/*
6809 	 * Setup for unordered if needed by looking at the user sent info
6810 	 * flags.
6811 	 */
6812 	if (sp->sinfo_flags & SCTP_UNORDERED) {
6813 		rcv_flags |= SCTP_DATA_UNORDERED;
6814 	}
6815 	/* clear out the chunk before setting up */
6816 	memset(chk, 0, sizeof(*chk));
6817 	chk->rec.data.rcv_flags = rcv_flags;
6818 
6819 	if (to_move >= length) {
6820 		/* we think we can steal the whole thing */
6821 		if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
6822 			SCTP_TCB_SEND_LOCK(stcb);
6823 			send_lock_up = 1;
6824 		}
6825 		if (to_move < sp->length) {
6826 			/* bail, it changed */
6827 			goto dont_do_it;
6828 		}
6829 		chk->data = sp->data;
6830 		chk->last_mbuf = sp->tail_mbuf;
6831 		/* register the stealing */
6832 		sp->data = sp->tail_mbuf = NULL;
6833 	} else {
6834 		struct mbuf *m;
6835 
6836 dont_do_it:
6837 		chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
6838 		chk->last_mbuf = NULL;
6839 		if (chk->data == NULL) {
6840 			sp->some_taken = some_taken;
6841 			sctp_free_a_chunk(stcb, chk);
6842 			*bail = 1;
6843 			to_move = 0;
6844 			goto out_of;
6845 		}
6846 #ifdef SCTP_MBUF_LOGGING
6847 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6848 			struct mbuf *mat;
6849 
6850 			mat = chk->data;
6851 			while (mat) {
6852 				if (SCTP_BUF_IS_EXTENDED(mat)) {
6853 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6854 				}
6855 				mat = SCTP_BUF_NEXT(mat);
6856 			}
6857 		}
6858 #endif
6859 		/* Pull off the data */
6860 		m_adj(sp->data, to_move);
6861 		/* Now lets work our way down and compact it */
6862 		m = sp->data;
6863 		while (m && (SCTP_BUF_LEN(m) == 0)) {
6864 			sp->data = SCTP_BUF_NEXT(m);
6865 			SCTP_BUF_NEXT(m) = NULL;
6866 			if (sp->tail_mbuf == m) {
6867 				/*-
6868 				 * Freeing tail? TSNH since
6869 				 * we supposedly were taking less
6870 				 * than the sp->length.
6871 				 */
6872 #ifdef INVARIANTS
6873 				panic("Huh, freing tail? - TSNH");
6874 #else
6875 				SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
6876 				sp->tail_mbuf = sp->data = NULL;
6877 				sp->length = 0;
6878 #endif
6879 
6880 			}
6881 			sctp_m_free(m);
6882 			m = sp->data;
6883 		}
6884 	}
6885 	if (SCTP_BUF_IS_EXTENDED(chk->data)) {
6886 		chk->copy_by_ref = 1;
6887 	} else {
6888 		chk->copy_by_ref = 0;
6889 	}
6890 	/*
6891 	 * get last_mbuf and counts of mb useage This is ugly but hopefully
6892 	 * its only one mbuf.
6893 	 */
6894 	if (chk->last_mbuf == NULL) {
6895 		chk->last_mbuf = chk->data;
6896 		while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
6897 			chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
6898 		}
6899 	}
6900 	if (to_move > length) {
6901 		/*- This should not happen either
6902 		 * since we always lower to_move to the size
6903 		 * of sp->length if its larger.
6904 		 */
6905 #ifdef INVARIANTS
6906 		panic("Huh, how can to_move be larger?");
6907 #else
6908 		SCTP_PRINTF("Huh, how can to_move be larger?\n");
6909 		sp->length = 0;
6910 #endif
6911 	} else {
6912 		atomic_subtract_int(&sp->length, to_move);
6913 	}
6914 	if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
6915 		/* Not enough room for a chunk header, get some */
6916 		struct mbuf *m;
6917 
6918 		m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
6919 		if (m == NULL) {
6920 			/*
6921 			 * we're in trouble here. _PREPEND below will free
6922 			 * all the data if there is no leading space, so we
6923 			 * must put the data back and restore.
6924 			 */
6925 			if (send_lock_up == 0) {
6926 				SCTP_TCB_SEND_LOCK(stcb);
6927 				send_lock_up = 1;
6928 			}
6929 			if (chk->data == NULL) {
6930 				/* unsteal the data */
6931 				sp->data = chk->data;
6932 				sp->tail_mbuf = chk->last_mbuf;
6933 			} else {
6934 				struct mbuf *m_tmp;
6935 
6936 				/* reassemble the data */
6937 				m_tmp = sp->data;
6938 				sp->data = chk->data;
6939 				SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
6940 			}
6941 			sp->some_taken = some_taken;
6942 			atomic_add_int(&sp->length, to_move);
6943 			chk->data = NULL;
6944 			*bail = 1;
6945 			sctp_free_a_chunk(stcb, chk);
6946 			to_move = 0;
6947 			goto out_of;
6948 		} else {
6949 			SCTP_BUF_LEN(m) = 0;
6950 			SCTP_BUF_NEXT(m) = chk->data;
6951 			chk->data = m;
6952 			M_ALIGN(chk->data, 4);
6953 		}
6954 	}
6955 	SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
6956 	if (chk->data == NULL) {
6957 		/* HELP, TSNH since we assured it would not above? */
6958 #ifdef INVARIANTS
6959 		panic("prepend failes HELP?");
6960 #else
6961 		SCTP_PRINTF("prepend fails HELP?\n");
6962 		sctp_free_a_chunk(stcb, chk);
6963 #endif
6964 		*bail = 1;
6965 		to_move = 0;
6966 		goto out_of;
6967 	}
6968 	sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
6969 	chk->book_size = chk->send_size = (to_move +
6970 	    sizeof(struct sctp_data_chunk));
6971 	chk->book_size_scale = 0;
6972 	chk->sent = SCTP_DATAGRAM_UNSENT;
6973 
6974 	chk->flags = 0;
6975 	chk->asoc = &stcb->asoc;
6976 	chk->pad_inplace = 0;
6977 	chk->no_fr_allowed = 0;
6978 	chk->rec.data.stream_seq = sp->strseq;
6979 	chk->rec.data.stream_number = sp->stream;
6980 	chk->rec.data.payloadtype = sp->ppid;
6981 	chk->rec.data.context = sp->context;
6982 	chk->rec.data.doing_fast_retransmit = 0;
6983 	chk->rec.data.ect_nonce = 0;	/* ECN Nonce */
6984 
6985 	chk->rec.data.timetodrop = sp->ts;
6986 	chk->flags = sp->act_flags;
6987 	chk->addr_over = sp->addr_over;
6988 
6989 	chk->whoTo = net;
6990 	atomic_add_int(&chk->whoTo->ref_count, 1);
6991 
6992 	chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
6993 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
6994 		sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
6995 		    (uintptr_t) stcb, sp->length,
6996 		    (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
6997 		    chk->rec.data.TSN_seq);
6998 	}
6999 	dchkh = mtod(chk->data, struct sctp_data_chunk *);
7000 	/*
7001 	 * Put the rest of the things in place now. Size was done earlier in
7002 	 * previous loop prior to padding.
7003 	 */
7004 
7005 #ifdef SCTP_ASOCLOG_OF_TSNS
7006 	SCTP_TCB_LOCK_ASSERT(stcb);
7007 	if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7008 		asoc->tsn_out_at = 0;
7009 		asoc->tsn_out_wrapped = 1;
7010 	}
7011 	asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7012 	asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7013 	asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7014 	asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7015 	asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7016 	asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7017 	asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7018 	asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7019 	asoc->tsn_out_at++;
7020 #endif
7021 
7022 	dchkh->ch.chunk_type = SCTP_DATA;
7023 	dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7024 	dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7025 	dchkh->dp.stream_id = htons(strq->stream_no);
7026 	dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7027 	dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7028 	dchkh->ch.chunk_length = htons(chk->send_size);
7029 	/* Now advance the chk->send_size by the actual pad needed. */
7030 	if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7031 		/* need a pad */
7032 		struct mbuf *lm;
7033 		int pads;
7034 
7035 		pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7036 		if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7037 			chk->pad_inplace = 1;
7038 		}
7039 		if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7040 			/* pad added an mbuf */
7041 			chk->last_mbuf = lm;
7042 		}
7043 		chk->send_size += pads;
7044 	}
7045 	/* We only re-set the policy if it is on */
7046 	if (sp->pr_sctp_on) {
7047 		sctp_set_prsctp_policy(stcb, sp);
7048 		asoc->pr_sctp_cnt++;
7049 		chk->pr_sctp_on = 1;
7050 	} else {
7051 		chk->pr_sctp_on = 0;
7052 	}
7053 	if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7054 		/* All done pull and kill the message */
7055 		atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7056 		if (sp->put_last_out == 0) {
7057 			SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7058 			SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7059 			    sp->sender_all_done,
7060 			    sp->length,
7061 			    sp->msg_is_complete,
7062 			    sp->put_last_out,
7063 			    send_lock_up);
7064 		}
7065 		if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7066 			SCTP_TCB_SEND_LOCK(stcb);
7067 			send_lock_up = 1;
7068 		}
7069 		TAILQ_REMOVE(&strq->outqueue, sp, next);
7070 		sctp_free_remote_addr(sp->net);
7071 		if (sp->data) {
7072 			sctp_m_freem(sp->data);
7073 			sp->data = NULL;
7074 		}
7075 		sctp_free_a_strmoq(stcb, sp);
7076 
7077 		/* we can't be locked to it */
7078 		*locked = 0;
7079 		stcb->asoc.locked_on_sending = NULL;
7080 	} else {
7081 		/* more to go, we are locked */
7082 		*locked = 1;
7083 	}
7084 	asoc->chunks_on_out_queue++;
7085 	TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7086 	asoc->send_queue_cnt++;
7087 out_of:
7088 	if (send_lock_up) {
7089 		SCTP_TCB_SEND_UNLOCK(stcb);
7090 		send_lock_up = 0;
7091 	}
7092 	return (to_move);
7093 }
7094 
7095 
7096 static struct sctp_stream_out *
7097 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
7098 {
7099 	struct sctp_stream_out *strq;
7100 
7101 	/* Find the next stream to use */
7102 	if (asoc->last_out_stream == NULL) {
7103 		strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
7104 		if (asoc->last_out_stream == NULL) {
7105 			/* huh nothing on the wheel, TSNH */
7106 			return (NULL);
7107 		}
7108 		goto done_it;
7109 	}
7110 	strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7111 done_it:
7112 	if (strq == NULL) {
7113 		strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
7114 	}
7115 	/* Save off the last stream */
7116 	asoc->last_out_stream = strq;
7117 	return (strq);
7118 
7119 }
7120 
7121 
7122 static void
7123 sctp_fill_outqueue(struct sctp_tcb *stcb,
7124     struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
7125 {
7126 	struct sctp_association *asoc;
7127 	struct sctp_stream_out *strq, *strqn, *strqt;
7128 	int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7129 	int locked, giveup;
7130 	struct sctp_stream_queue_pending *sp;
7131 
7132 	SCTP_TCB_LOCK_ASSERT(stcb);
7133 	asoc = &stcb->asoc;
7134 #ifdef INET6
7135 	if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
7136 		goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7137 	} else {
7138 		/* ?? not sure what else to do */
7139 		goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7140 	}
7141 #else
7142 	goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7143 #endif
7144 	/* Need an allowance for the data chunk header too */
7145 	goal_mtu -= sizeof(struct sctp_data_chunk);
7146 
7147 	/* must make even word boundary */
7148 	goal_mtu &= 0xfffffffc;
7149 	if (asoc->locked_on_sending) {
7150 		/* We are stuck on one stream until the message completes. */
7151 		strqn = strq = asoc->locked_on_sending;
7152 		locked = 1;
7153 	} else {
7154 		strqn = strq = sctp_select_a_stream(stcb, asoc);
7155 		locked = 0;
7156 	}
7157 
7158 	while ((goal_mtu > 0) && strq) {
7159 		sp = TAILQ_FIRST(&strq->outqueue);
7160 		/*
7161 		 * If CMT is off, we must validate that the stream in
7162 		 * question has the first item pointed towards are network
7163 		 * destionation requested by the caller. Note that if we
7164 		 * turn out to be locked to a stream (assigning TSN's then
7165 		 * we must stop, since we cannot look for another stream
7166 		 * with data to send to that destination). In CMT's case, by
7167 		 * skipping this check, we will send one data packet towards
7168 		 * the requested net.
7169 		 */
7170 		if (sp == NULL) {
7171 			break;
7172 		}
7173 		if ((sp->net != net) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
7174 			/* none for this network */
7175 			if (locked) {
7176 				break;
7177 			} else {
7178 				strq = sctp_select_a_stream(stcb, asoc);
7179 				if (strq == NULL)
7180 					/* none left */
7181 					break;
7182 				if (strqn == strq) {
7183 					/* I have circled */
7184 					break;
7185 				}
7186 				continue;
7187 			}
7188 		}
7189 		giveup = 0;
7190 		bail = 0;
7191 		moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
7192 		    &giveup, eeor_mode, &bail);
7193 		asoc->last_out_stream = strq;
7194 		if (locked) {
7195 			asoc->locked_on_sending = strq;
7196 			if ((moved_how_much == 0) || (giveup) || bail)
7197 				/* no more to move for now */
7198 				break;
7199 		} else {
7200 			asoc->locked_on_sending = NULL;
7201 			strqt = sctp_select_a_stream(stcb, asoc);
7202 			if (TAILQ_FIRST(&strq->outqueue) == NULL) {
7203 				if (strq == strqn) {
7204 					/* Must move start to next one */
7205 					strqn = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7206 					if (strqn == NULL) {
7207 						strqn = TAILQ_FIRST(&asoc->out_wheel);
7208 						if (strqn == NULL) {
7209 							break;
7210 						}
7211 					}
7212 				}
7213 				sctp_remove_from_wheel(stcb, asoc, strq);
7214 			}
7215 			if ((giveup) || bail) {
7216 				break;
7217 			}
7218 			strq = strqt;
7219 			if (strq == NULL) {
7220 				break;
7221 			}
7222 		}
7223 		total_moved += moved_how_much;
7224 		goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7225 		goal_mtu &= 0xfffffffc;
7226 	}
7227 	if (bail)
7228 		*quit_now = 1;
7229 
7230 	if (total_moved == 0) {
7231 		if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) &&
7232 		    (net == stcb->asoc.primary_destination)) {
7233 			/* ran dry for primary network net */
7234 			SCTP_STAT_INCR(sctps_primary_randry);
7235 		} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7236 			/* ran dry with CMT on */
7237 			SCTP_STAT_INCR(sctps_cmt_randry);
7238 		}
7239 	}
7240 }
7241 
7242 void
7243 sctp_fix_ecn_echo(struct sctp_association *asoc)
7244 {
7245 	struct sctp_tmit_chunk *chk;
7246 
7247 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7248 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7249 			chk->sent = SCTP_DATAGRAM_UNSENT;
7250 		}
7251 	}
7252 }
7253 
7254 static void
7255 sctp_move_to_an_alt(struct sctp_tcb *stcb,
7256     struct sctp_association *asoc,
7257     struct sctp_nets *net)
7258 {
7259 	struct sctp_tmit_chunk *chk;
7260 	struct sctp_nets *a_net;
7261 
7262 	SCTP_TCB_LOCK_ASSERT(stcb);
7263 	/*
7264 	 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
7265 	 * destination using the PF algorithm for finding alternate
7266 	 * destinations.
7267 	 */
7268 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
7269 		a_net = sctp_find_alternate_net(stcb, net, 2);
7270 	} else {
7271 		a_net = sctp_find_alternate_net(stcb, net, 0);
7272 	}
7273 	if ((a_net != net) &&
7274 	    ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
7275 		/*
7276 		 * We only proceed if a valid alternate is found that is not
7277 		 * this one and is reachable. Here we must move all chunks
7278 		 * queued in the send queue off of the destination address
7279 		 * to our alternate.
7280 		 */
7281 		TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7282 			if (chk->whoTo == net) {
7283 				/* Move the chunk to our alternate */
7284 				sctp_free_remote_addr(chk->whoTo);
7285 				chk->whoTo = a_net;
7286 				atomic_add_int(&a_net->ref_count, 1);
7287 			}
7288 		}
7289 	}
7290 }
7291 
7292 int
7293 sctp_med_chunk_output(struct sctp_inpcb *inp,
7294     struct sctp_tcb *stcb,
7295     struct sctp_association *asoc,
7296     int *num_out,
7297     int *reason_code,
7298     int control_only, int *cwnd_full, int from_where,
7299     struct timeval *now, int *now_filled, int frag_point, int so_locked
7300 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7301     SCTP_UNUSED
7302 #endif
7303 )
7304 {
7305 	/*
7306 	 * Ok this is the generic chunk service queue. we must do the
7307 	 * following: - Service the stream queue that is next, moving any
7308 	 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7309 	 * LAST to the out queue in one pass) and assigning TSN's - Check to
7310 	 * see if the cwnd/rwnd allows any output, if so we go ahead and
7311 	 * fomulate and send the low level chunks. Making sure to combine
7312 	 * any control in the control chunk queue also.
7313 	 */
7314 	struct sctp_nets *net;
7315 	struct mbuf *outchain, *endoutchain;
7316 	struct sctp_tmit_chunk *chk, *nchk;
7317 	struct sctphdr *shdr;
7318 
7319 	/* temp arrays for unlinking */
7320 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7321 	int no_fragmentflg, error;
7322 	int one_chunk, hbflag, skip_data_for_this_net;
7323 	int asconf, cookie, no_out_cnt;
7324 	int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind, eeor_mode;
7325 	unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7326 	struct sctp_nets *start_at, *old_startat = NULL, *send_start_at;
7327 	int tsns_sent = 0;
7328 	uint32_t auth_offset = 0;
7329 	struct sctp_auth_chunk *auth = NULL;
7330 
7331 	/*
7332 	 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7333 	 * destination.
7334 	 */
7335 	int pf_hbflag = 0;
7336 	int quit_now = 0;
7337 
7338 	*num_out = 0;
7339 	cwnd_full_ind = 0;
7340 
7341 	if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7342 	    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7343 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7344 		eeor_mode = 1;
7345 	} else {
7346 		eeor_mode = 0;
7347 	}
7348 	ctl_cnt = no_out_cnt = asconf = cookie = 0;
7349 	/*
7350 	 * First lets prime the pump. For each destination, if there is room
7351 	 * in the flight size, attempt to pull an MTU's worth out of the
7352 	 * stream queues into the general send_queue
7353 	 */
7354 #ifdef SCTP_AUDITING_ENABLED
7355 	sctp_audit_log(0xC2, 2);
7356 #endif
7357 	SCTP_TCB_LOCK_ASSERT(stcb);
7358 	hbflag = 0;
7359 	if ((control_only) || (asoc->stream_reset_outstanding))
7360 		no_data_chunks = 1;
7361 	else
7362 		no_data_chunks = 0;
7363 
7364 	/* Nothing to possible to send? */
7365 	if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7366 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7367 	    TAILQ_EMPTY(&asoc->send_queue) &&
7368 	    TAILQ_EMPTY(&asoc->out_wheel)) {
7369 		*reason_code = 9;
7370 		return (0);
7371 	}
7372 	if (asoc->peers_rwnd == 0) {
7373 		/* No room in peers rwnd */
7374 		*cwnd_full = 1;
7375 		*reason_code = 1;
7376 		if (asoc->total_flight > 0) {
7377 			/* we are allowed one chunk in flight */
7378 			no_data_chunks = 1;
7379 		}
7380 	}
7381 	if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
7382 		if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7383 			/*
7384 			 * for CMT we start at the next one past the one we
7385 			 * last added data to.
7386 			 */
7387 			if (TAILQ_FIRST(&asoc->send_queue) != NULL) {
7388 				goto skip_the_fill_from_streams;
7389 			}
7390 			if (asoc->last_net_data_came_from) {
7391 				net = TAILQ_NEXT(asoc->last_net_data_came_from, sctp_next);
7392 				if (net == NULL) {
7393 					net = TAILQ_FIRST(&asoc->nets);
7394 				}
7395 			} else {
7396 				/* back to start */
7397 				net = TAILQ_FIRST(&asoc->nets);
7398 			}
7399 
7400 			/*
7401 			 * JRI-TODO: CMT-MPI. Simply set the first
7402 			 * destination (net) to be optimized for the next
7403 			 * message to be pulled out of the outwheel. 1. peek
7404 			 * at outwheel 2. If large message, set net =
7405 			 * highest_cwnd 3. If small message, set net =
7406 			 * lowest rtt
7407 			 */
7408 		} else {
7409 			net = asoc->primary_destination;
7410 			if (net == NULL) {
7411 				/* TSNH */
7412 				net = TAILQ_FIRST(&asoc->nets);
7413 			}
7414 		}
7415 		start_at = net;
7416 
7417 one_more_time:
7418 		for (; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7419 			net->window_probe = 0;
7420 			if (old_startat && (old_startat == net)) {
7421 				break;
7422 			}
7423 			/*
7424 			 * JRI: if dest is unreachable or unconfirmed, do
7425 			 * not send data to it
7426 			 */
7427 			if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
7428 				continue;
7429 			}
7430 			/*
7431 			 * JRI: if dest is in PF state, do not send data to
7432 			 * it
7433 			 */
7434 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
7435 			    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
7436 			    (net->dest_state & SCTP_ADDR_PF)) {
7437 				continue;
7438 			}
7439 			if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
7440 				/* nothing can be in queue for this guy */
7441 				continue;
7442 			}
7443 			if (net->flight_size >= net->cwnd) {
7444 				/* skip this network, no room */
7445 				cwnd_full_ind++;
7446 				continue;
7447 			}
7448 			/*
7449 			 * JRI : this for loop we are in takes in each net,
7450 			 * if its's got space in cwnd and has data sent to
7451 			 * it (when CMT is off) then it calls
7452 			 * sctp_fill_outqueue for the net. This gets data on
7453 			 * the send queue for that network.
7454 			 *
7455 			 * In sctp_fill_outqueue TSN's are assigned and data is
7456 			 * copied out of the stream buffers. Note mostly
7457 			 * copy by reference (we hope).
7458 			 */
7459 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7460 				sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7461 			}
7462 			sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
7463 			if (quit_now) {
7464 				/* memory alloc failure */
7465 				no_data_chunks = 1;
7466 				goto skip_the_fill_from_streams;
7467 			}
7468 		}
7469 		if (start_at != TAILQ_FIRST(&asoc->nets)) {
7470 			/* got to pick up the beginning stuff. */
7471 			old_startat = start_at;
7472 			start_at = net = TAILQ_FIRST(&asoc->nets);
7473 			if (old_startat)
7474 				goto one_more_time;
7475 		}
7476 	}
7477 skip_the_fill_from_streams:
7478 	*cwnd_full = cwnd_full_ind;
7479 
7480 	/* now service each destination and send out what we can for it */
7481 	/* Nothing to send? */
7482 	if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7483 	    (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7484 	    (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
7485 		*reason_code = 8;
7486 		return (0);
7487 	}
7488 	if (no_data_chunks) {
7489 		chk = TAILQ_FIRST(&asoc->asconf_send_queue);
7490 		if (chk == NULL)
7491 			chk = TAILQ_FIRST(&asoc->control_send_queue);
7492 	} else {
7493 		chk = TAILQ_FIRST(&asoc->send_queue);
7494 	}
7495 	if (chk) {
7496 		send_start_at = chk->whoTo;
7497 	} else {
7498 		send_start_at = TAILQ_FIRST(&asoc->nets);
7499 	}
7500 	old_startat = NULL;
7501 again_one_more_time:
7502 	for (net = send_start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7503 		/* how much can we send? */
7504 		/* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7505 		if (old_startat && (old_startat == net)) {
7506 			/* through list ocmpletely. */
7507 			break;
7508 		}
7509 		tsns_sent = 0;
7510 		if (net->ref_count < 2) {
7511 			/*
7512 			 * Ref-count of 1 so we cannot have data or control
7513 			 * queued to this address. Skip it.
7514 			 */
7515 			continue;
7516 		}
7517 		ctl_cnt = bundle_at = 0;
7518 		endoutchain = outchain = NULL;
7519 		no_fragmentflg = 1;
7520 		one_chunk = 0;
7521 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7522 			skip_data_for_this_net = 1;
7523 		} else {
7524 			skip_data_for_this_net = 0;
7525 		}
7526 		if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7527 			/*
7528 			 * if we have a route and an ifp check to see if we
7529 			 * have room to send to this guy
7530 			 */
7531 			struct ifnet *ifp;
7532 
7533 			ifp = net->ro.ro_rt->rt_ifp;
7534 			if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7535 				SCTP_STAT_INCR(sctps_ifnomemqueued);
7536 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7537 					sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7538 				}
7539 				continue;
7540 			}
7541 		}
7542 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7543 		case AF_INET:
7544 			mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7545 			break;
7546 #ifdef INET6
7547 		case AF_INET6:
7548 			mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7549 			break;
7550 #endif
7551 		default:
7552 			/* TSNH */
7553 			mtu = net->mtu;
7554 			break;
7555 		}
7556 		mx_mtu = mtu;
7557 		to_out = 0;
7558 		if (mtu > asoc->peers_rwnd) {
7559 			if (asoc->total_flight > 0) {
7560 				/* We have a packet in flight somewhere */
7561 				r_mtu = asoc->peers_rwnd;
7562 			} else {
7563 				/* We are always allowed to send one MTU out */
7564 				one_chunk = 1;
7565 				r_mtu = mtu;
7566 			}
7567 		} else {
7568 			r_mtu = mtu;
7569 		}
7570 		/************************/
7571 		/* ASCONF transmission */
7572 		/************************/
7573 		/* Now first lets go through the asconf queue */
7574 		for (chk = TAILQ_FIRST(&asoc->asconf_send_queue);
7575 		    chk; chk = nchk) {
7576 			nchk = TAILQ_NEXT(chk, sctp_next);
7577 			if (chk->rec.chunk_id.id != SCTP_ASCONF) {
7578 				continue;
7579 			}
7580 			if (chk->whoTo != net) {
7581 				/*
7582 				 * No, not sent to the network we are
7583 				 * looking at
7584 				 */
7585 				break;
7586 			}
7587 			if (chk->data == NULL) {
7588 				break;
7589 			}
7590 			if (chk->sent != SCTP_DATAGRAM_UNSENT &&
7591 			    chk->sent != SCTP_DATAGRAM_RESEND) {
7592 				break;
7593 			}
7594 			/*
7595 			 * if no AUTH is yet included and this chunk
7596 			 * requires it, make sure to account for it.  We
7597 			 * don't apply the size until the AUTH chunk is
7598 			 * actually added below in case there is no room for
7599 			 * this chunk. NOTE: we overload the use of "omtu"
7600 			 * here
7601 			 */
7602 			if ((auth == NULL) &&
7603 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7604 			    stcb->asoc.peer_auth_chunks)) {
7605 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7606 			} else
7607 				omtu = 0;
7608 			/* Here we do NOT factor the r_mtu */
7609 			if ((chk->send_size < (int)(mtu - omtu)) ||
7610 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7611 				/*
7612 				 * We probably should glom the mbuf chain
7613 				 * from the chk->data for control but the
7614 				 * problem is it becomes yet one more level
7615 				 * of tracking to do if for some reason
7616 				 * output fails. Then I have got to
7617 				 * reconstruct the merged control chain.. el
7618 				 * yucko.. for now we take the easy way and
7619 				 * do the copy
7620 				 */
7621 				/*
7622 				 * Add an AUTH chunk, if chunk requires it
7623 				 * save the offset into the chain for AUTH
7624 				 */
7625 				if ((auth == NULL) &&
7626 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7627 				    stcb->asoc.peer_auth_chunks))) {
7628 					outchain = sctp_add_auth_chunk(outchain,
7629 					    &endoutchain,
7630 					    &auth,
7631 					    &auth_offset,
7632 					    stcb,
7633 					    chk->rec.chunk_id.id);
7634 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7635 				}
7636 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7637 				    (int)chk->rec.chunk_id.can_take_data,
7638 				    chk->send_size, chk->copy_by_ref);
7639 				if (outchain == NULL) {
7640 					*reason_code = 8;
7641 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7642 					return (ENOMEM);
7643 				}
7644 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7645 				/* update our MTU size */
7646 				if (mtu > (chk->send_size + omtu))
7647 					mtu -= (chk->send_size + omtu);
7648 				else
7649 					mtu = 0;
7650 				to_out += (chk->send_size + omtu);
7651 				/* Do clear IP_DF ? */
7652 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7653 					no_fragmentflg = 0;
7654 				}
7655 				if (chk->rec.chunk_id.can_take_data)
7656 					chk->data = NULL;
7657 				/*
7658 				 * set hb flag since we can use these for
7659 				 * RTO
7660 				 */
7661 				hbflag = 1;
7662 				asconf = 1;
7663 				/*
7664 				 * should sysctl this: don't bundle data
7665 				 * with ASCONF since it requires AUTH
7666 				 */
7667 				no_data_chunks = 1;
7668 				chk->sent = SCTP_DATAGRAM_SENT;
7669 				chk->snd_count++;
7670 				if (mtu == 0) {
7671 					/*
7672 					 * Ok we are out of room but we can
7673 					 * output without effecting the
7674 					 * flight size since this little guy
7675 					 * is a control only packet.
7676 					 */
7677 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7678 					/*
7679 					 * do NOT clear the asconf flag as
7680 					 * it is used to do appropriate
7681 					 * source address selection.
7682 					 */
7683 					SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
7684 					if (outchain == NULL) {
7685 						/* no memory */
7686 						SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
7687 						error = ENOBUFS;
7688 						*reason_code = 7;
7689 						continue;
7690 					}
7691 					shdr = mtod(outchain, struct sctphdr *);
7692 					shdr->src_port = inp->sctp_lport;
7693 					shdr->dest_port = stcb->rport;
7694 					shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7695 					shdr->checksum = 0;
7696 					auth_offset += sizeof(struct sctphdr);
7697 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7698 					    (struct sockaddr *)&net->ro._l_addr,
7699 					    outchain, auth_offset, auth,
7700 					    no_fragmentflg, 0, NULL, asconf, net->port, so_locked, NULL))) {
7701 						if (error == ENOBUFS) {
7702 							asoc->ifp_had_enobuf = 1;
7703 							SCTP_STAT_INCR(sctps_lowlevelerr);
7704 						}
7705 						if (from_where == 0) {
7706 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
7707 						}
7708 						if (*now_filled == 0) {
7709 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7710 							*now_filled = 1;
7711 							*now = net->last_sent_time;
7712 						} else {
7713 							net->last_sent_time = *now;
7714 						}
7715 						hbflag = 0;
7716 						/* error, could not output */
7717 						if (error == EHOSTUNREACH) {
7718 							/*
7719 							 * Destination went
7720 							 * unreachable
7721 							 * during this send
7722 							 */
7723 							sctp_move_to_an_alt(stcb, asoc, net);
7724 						}
7725 						*reason_code = 7;
7726 						continue;
7727 					} else
7728 						asoc->ifp_had_enobuf = 0;
7729 					if (*now_filled == 0) {
7730 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7731 						*now_filled = 1;
7732 						*now = net->last_sent_time;
7733 					} else {
7734 						net->last_sent_time = *now;
7735 					}
7736 					hbflag = 0;
7737 					/*
7738 					 * increase the number we sent, if a
7739 					 * cookie is sent we don't tell them
7740 					 * any was sent out.
7741 					 */
7742 					outchain = endoutchain = NULL;
7743 					auth = NULL;
7744 					auth_offset = 0;
7745 					if (!no_out_cnt)
7746 						*num_out += ctl_cnt;
7747 					/* recalc a clean slate and setup */
7748 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7749 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7750 					} else {
7751 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7752 					}
7753 					to_out = 0;
7754 					no_fragmentflg = 1;
7755 				}
7756 			}
7757 		}
7758 		/************************/
7759 		/* Control transmission */
7760 		/************************/
7761 		/* Now first lets go through the control queue */
7762 		for (chk = TAILQ_FIRST(&asoc->control_send_queue);
7763 		    chk; chk = nchk) {
7764 			nchk = TAILQ_NEXT(chk, sctp_next);
7765 			if (chk->whoTo != net) {
7766 				/*
7767 				 * No, not sent to the network we are
7768 				 * looking at
7769 				 */
7770 				continue;
7771 			}
7772 			if (chk->data == NULL) {
7773 				continue;
7774 			}
7775 			if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7776 				/*
7777 				 * It must be unsent. Cookies and ASCONF's
7778 				 * hang around but there timers will force
7779 				 * when marked for resend.
7780 				 */
7781 				continue;
7782 			}
7783 			/*
7784 			 * if no AUTH is yet included and this chunk
7785 			 * requires it, make sure to account for it.  We
7786 			 * don't apply the size until the AUTH chunk is
7787 			 * actually added below in case there is no room for
7788 			 * this chunk. NOTE: we overload the use of "omtu"
7789 			 * here
7790 			 */
7791 			if ((auth == NULL) &&
7792 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7793 			    stcb->asoc.peer_auth_chunks)) {
7794 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7795 			} else
7796 				omtu = 0;
7797 			/* Here we do NOT factor the r_mtu */
7798 			if ((chk->send_size < (int)(mtu - omtu)) ||
7799 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7800 				/*
7801 				 * We probably should glom the mbuf chain
7802 				 * from the chk->data for control but the
7803 				 * problem is it becomes yet one more level
7804 				 * of tracking to do if for some reason
7805 				 * output fails. Then I have got to
7806 				 * reconstruct the merged control chain.. el
7807 				 * yucko.. for now we take the easy way and
7808 				 * do the copy
7809 				 */
7810 				/*
7811 				 * Add an AUTH chunk, if chunk requires it
7812 				 * save the offset into the chain for AUTH
7813 				 */
7814 				if ((auth == NULL) &&
7815 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7816 				    stcb->asoc.peer_auth_chunks))) {
7817 					outchain = sctp_add_auth_chunk(outchain,
7818 					    &endoutchain,
7819 					    &auth,
7820 					    &auth_offset,
7821 					    stcb,
7822 					    chk->rec.chunk_id.id);
7823 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7824 				}
7825 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7826 				    (int)chk->rec.chunk_id.can_take_data,
7827 				    chk->send_size, chk->copy_by_ref);
7828 				if (outchain == NULL) {
7829 					*reason_code = 8;
7830 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7831 					return (ENOMEM);
7832 				}
7833 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7834 				/* update our MTU size */
7835 				if (mtu > (chk->send_size + omtu))
7836 					mtu -= (chk->send_size + omtu);
7837 				else
7838 					mtu = 0;
7839 				to_out += (chk->send_size + omtu);
7840 				/* Do clear IP_DF ? */
7841 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7842 					no_fragmentflg = 0;
7843 				}
7844 				if (chk->rec.chunk_id.can_take_data)
7845 					chk->data = NULL;
7846 				/* Mark things to be removed, if needed */
7847 				if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7848 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7849 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7850 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7851 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7852 				    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7853 				    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7854 				    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7855 				    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7856 				    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7857 
7858 					if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7859 						hbflag = 1;
7860 						/*
7861 						 * JRS 5/14/07 - Set the
7862 						 * flag to say a heartbeat
7863 						 * is being sent.
7864 						 */
7865 						pf_hbflag = 1;
7866 					}
7867 					/* remove these chunks at the end */
7868 					if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
7869 						/* turn off the timer */
7870 						if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7871 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7872 							    inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7873 						}
7874 					}
7875 					ctl_cnt++;
7876 				} else {
7877 					/*
7878 					 * Other chunks, since they have
7879 					 * timers running (i.e. COOKIE) we
7880 					 * just "trust" that it gets sent or
7881 					 * retransmitted.
7882 					 */
7883 					ctl_cnt++;
7884 					if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7885 						cookie = 1;
7886 						no_out_cnt = 1;
7887 					}
7888 					chk->sent = SCTP_DATAGRAM_SENT;
7889 					chk->snd_count++;
7890 				}
7891 				if (mtu == 0) {
7892 					/*
7893 					 * Ok we are out of room but we can
7894 					 * output without effecting the
7895 					 * flight size since this little guy
7896 					 * is a control only packet.
7897 					 */
7898 					if (asconf) {
7899 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7900 						/*
7901 						 * do NOT clear the asconf
7902 						 * flag as it is used to do
7903 						 * appropriate source
7904 						 * address selection.
7905 						 */
7906 					}
7907 					if (cookie) {
7908 						sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7909 						cookie = 0;
7910 					}
7911 					SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
7912 					if (outchain == NULL) {
7913 						/* no memory */
7914 						SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
7915 						error = ENOBUFS;
7916 						goto error_out_again;
7917 					}
7918 					shdr = mtod(outchain, struct sctphdr *);
7919 					shdr->src_port = inp->sctp_lport;
7920 					shdr->dest_port = stcb->rport;
7921 					shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7922 					shdr->checksum = 0;
7923 					auth_offset += sizeof(struct sctphdr);
7924 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7925 					    (struct sockaddr *)&net->ro._l_addr,
7926 					    outchain, auth_offset, auth,
7927 					    no_fragmentflg, 0, NULL, asconf, net->port, so_locked, NULL))) {
7928 						if (error == ENOBUFS) {
7929 							asoc->ifp_had_enobuf = 1;
7930 							SCTP_STAT_INCR(sctps_lowlevelerr);
7931 						}
7932 						if (from_where == 0) {
7933 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
7934 						}
7935 				error_out_again:
7936 						/* error, could not output */
7937 						if (hbflag) {
7938 							if (*now_filled == 0) {
7939 								(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7940 								*now_filled = 1;
7941 								*now = net->last_sent_time;
7942 							} else {
7943 								net->last_sent_time = *now;
7944 							}
7945 							hbflag = 0;
7946 						}
7947 						if (error == EHOSTUNREACH) {
7948 							/*
7949 							 * Destination went
7950 							 * unreachable
7951 							 * during this send
7952 							 */
7953 							sctp_move_to_an_alt(stcb, asoc, net);
7954 						}
7955 						*reason_code = 7;
7956 						continue;
7957 					} else
7958 						asoc->ifp_had_enobuf = 0;
7959 					/* Only HB or ASCONF advances time */
7960 					if (hbflag) {
7961 						if (*now_filled == 0) {
7962 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7963 							*now_filled = 1;
7964 							*now = net->last_sent_time;
7965 						} else {
7966 							net->last_sent_time = *now;
7967 						}
7968 						hbflag = 0;
7969 					}
7970 					/*
7971 					 * increase the number we sent, if a
7972 					 * cookie is sent we don't tell them
7973 					 * any was sent out.
7974 					 */
7975 					outchain = endoutchain = NULL;
7976 					auth = NULL;
7977 					auth_offset = 0;
7978 					if (!no_out_cnt)
7979 						*num_out += ctl_cnt;
7980 					/* recalc a clean slate and setup */
7981 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7982 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7983 					} else {
7984 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7985 					}
7986 					to_out = 0;
7987 					no_fragmentflg = 1;
7988 				}
7989 			}
7990 		}
7991 		/*********************/
7992 		/* Data transmission */
7993 		/*********************/
7994 		/*
7995 		 * if AUTH for DATA is required and no AUTH has been added
7996 		 * yet, account for this in the mtu now... if no data can be
7997 		 * bundled, this adjustment won't matter anyways since the
7998 		 * packet will be going out...
7999 		 */
8000 		if ((auth == NULL) &&
8001 		    sctp_auth_is_required_chunk(SCTP_DATA,
8002 		    stcb->asoc.peer_auth_chunks)) {
8003 			mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8004 		}
8005 		/* now lets add any data within the MTU constraints */
8006 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8007 		case AF_INET:
8008 			if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8009 				omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8010 			else
8011 				omtu = 0;
8012 			break;
8013 #ifdef INET6
8014 		case AF_INET6:
8015 			if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8016 				omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8017 			else
8018 				omtu = 0;
8019 			break;
8020 #endif
8021 		default:
8022 			/* TSNH */
8023 			omtu = 0;
8024 			break;
8025 		}
8026 		if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) && (skip_data_for_this_net == 0)) ||
8027 		    (cookie)) {
8028 			for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
8029 				if (no_data_chunks) {
8030 					/* let only control go out */
8031 					*reason_code = 1;
8032 					break;
8033 				}
8034 				if (net->flight_size >= net->cwnd) {
8035 					/* skip this net, no room for data */
8036 					*reason_code = 2;
8037 					break;
8038 				}
8039 				nchk = TAILQ_NEXT(chk, sctp_next);
8040 				if (chk->whoTo != net) {
8041 					/* No, not sent to this net */
8042 					continue;
8043 				}
8044 				if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8045 					/*-
8046 					 * strange, we have a chunk that is
8047 					 * to big for its destination and
8048 					 * yet no fragment ok flag.
8049 					 * Something went wrong when the
8050 					 * PMTU changed...we did not mark
8051 					 * this chunk for some reason?? I
8052 					 * will fix it here by letting IP
8053 					 * fragment it for now and printing
8054 					 * a warning. This really should not
8055 					 * happen ...
8056 					 */
8057 					SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8058 					    chk->send_size, mtu);
8059 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8060 				}
8061 				if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8062 				    ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8063 					/* ok we will add this one */
8064 
8065 					/*
8066 					 * Add an AUTH chunk, if chunk
8067 					 * requires it, save the offset into
8068 					 * the chain for AUTH
8069 					 */
8070 					if ((auth == NULL) &&
8071 					    (sctp_auth_is_required_chunk(SCTP_DATA,
8072 					    stcb->asoc.peer_auth_chunks))) {
8073 
8074 						outchain = sctp_add_auth_chunk(outchain,
8075 						    &endoutchain,
8076 						    &auth,
8077 						    &auth_offset,
8078 						    stcb,
8079 						    SCTP_DATA);
8080 						SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8081 					}
8082 					outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8083 					    chk->send_size, chk->copy_by_ref);
8084 					if (outchain == NULL) {
8085 						SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8086 						if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8087 							sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8088 						}
8089 						*reason_code = 3;
8090 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8091 						return (ENOMEM);
8092 					}
8093 					/* upate our MTU size */
8094 					/* Do clear IP_DF ? */
8095 					if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8096 						no_fragmentflg = 0;
8097 					}
8098 					/* unsigned subtraction of mtu */
8099 					if (mtu > chk->send_size)
8100 						mtu -= chk->send_size;
8101 					else
8102 						mtu = 0;
8103 					/* unsigned subtraction of r_mtu */
8104 					if (r_mtu > chk->send_size)
8105 						r_mtu -= chk->send_size;
8106 					else
8107 						r_mtu = 0;
8108 
8109 					to_out += chk->send_size;
8110 					if ((to_out > mx_mtu) && no_fragmentflg) {
8111 #ifdef INVARIANTS
8112 						panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8113 #else
8114 						SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8115 						    mx_mtu, to_out);
8116 #endif
8117 					}
8118 					chk->window_probe = 0;
8119 					data_list[bundle_at++] = chk;
8120 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8121 						mtu = 0;
8122 						break;
8123 					}
8124 					if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8125 						if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8126 							SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8127 						} else {
8128 							SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8129 						}
8130 						if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8131 						    ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8132 							/*
8133 							 * Count number of
8134 							 * user msg's that
8135 							 * were fragmented
8136 							 * we do this by
8137 							 * counting when we
8138 							 * see a LAST
8139 							 * fragment only.
8140 							 */
8141 							SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8142 					}
8143 					if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8144 						if (one_chunk) {
8145 							data_list[0]->window_probe = 1;
8146 							net->window_probe = 1;
8147 						}
8148 						break;
8149 					}
8150 				} else {
8151 					/*
8152 					 * Must be sent in order of the
8153 					 * TSN's (on a network)
8154 					 */
8155 					break;
8156 				}
8157 			}	/* for (chunk gather loop for this net) */
8158 		}		/* if asoc.state OPEN */
8159 		/* Is there something to send for this destination? */
8160 		if (outchain) {
8161 			/* We may need to start a control timer or two */
8162 			if (asconf) {
8163 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8164 				    stcb, net);
8165 				/*
8166 				 * do NOT clear the asconf flag as it is
8167 				 * used to do appropriate source address
8168 				 * selection.
8169 				 */
8170 			}
8171 			if (cookie) {
8172 				sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8173 				cookie = 0;
8174 			}
8175 			/* must start a send timer if data is being sent */
8176 			if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8177 				/*
8178 				 * no timer running on this destination
8179 				 * restart it.
8180 				 */
8181 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8182 			} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
8183 				    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
8184 				    pf_hbflag &&
8185 				    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
8186 			    (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8187 				/*
8188 				 * JRS 5/14/07 - If a HB has been sent to a
8189 				 * PF destination and no T3 timer is
8190 				 * currently running, start the T3 timer to
8191 				 * track the HBs that were sent.
8192 				 */
8193 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8194 			}
8195 			/* Now send it, if there is anything to send :> */
8196 			SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
8197 			if (outchain == NULL) {
8198 				/* out of mbufs */
8199 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
8200 				error = ENOBUFS;
8201 				goto errored_send;
8202 			}
8203 			shdr = mtod(outchain, struct sctphdr *);
8204 			shdr->src_port = inp->sctp_lport;
8205 			shdr->dest_port = stcb->rport;
8206 			shdr->v_tag = htonl(stcb->asoc.peer_vtag);
8207 			shdr->checksum = 0;
8208 			auth_offset += sizeof(struct sctphdr);
8209 			if ((error = sctp_lowlevel_chunk_output(inp,
8210 			    stcb,
8211 			    net,
8212 			    (struct sockaddr *)&net->ro._l_addr,
8213 			    outchain,
8214 			    auth_offset,
8215 			    auth,
8216 			    no_fragmentflg,
8217 			    bundle_at,
8218 			    data_list[0],
8219 			    asconf, net->port, so_locked, NULL))) {
8220 				/* error, we could not output */
8221 				if (error == ENOBUFS) {
8222 					SCTP_STAT_INCR(sctps_lowlevelerr);
8223 					asoc->ifp_had_enobuf = 1;
8224 				}
8225 				if (from_where == 0) {
8226 					SCTP_STAT_INCR(sctps_lowlevelerrusr);
8227 				}
8228 		errored_send:
8229 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8230 				if (hbflag) {
8231 					if (*now_filled == 0) {
8232 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8233 						*now_filled = 1;
8234 						*now = net->last_sent_time;
8235 					} else {
8236 						net->last_sent_time = *now;
8237 					}
8238 					hbflag = 0;
8239 				}
8240 				if (error == EHOSTUNREACH) {
8241 					/*
8242 					 * Destination went unreachable
8243 					 * during this send
8244 					 */
8245 					sctp_move_to_an_alt(stcb, asoc, net);
8246 				}
8247 				*reason_code = 6;
8248 				/*-
8249 				 * I add this line to be paranoid. As far as
8250 				 * I can tell the continue, takes us back to
8251 				 * the top of the for, but just to make sure
8252 				 * I will reset these again here.
8253 				 */
8254 				ctl_cnt = bundle_at = 0;
8255 				continue;	/* This takes us back to the
8256 						 * for() for the nets. */
8257 			} else {
8258 				asoc->ifp_had_enobuf = 0;
8259 			}
8260 			outchain = endoutchain = NULL;
8261 			auth = NULL;
8262 			auth_offset = 0;
8263 			if (bundle_at || hbflag) {
8264 				/* For data/asconf and hb set time */
8265 				if (*now_filled == 0) {
8266 					(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8267 					*now_filled = 1;
8268 					*now = net->last_sent_time;
8269 				} else {
8270 					net->last_sent_time = *now;
8271 				}
8272 			}
8273 			if (!no_out_cnt) {
8274 				*num_out += (ctl_cnt + bundle_at);
8275 			}
8276 			if (bundle_at) {
8277 				/* setup for a RTO measurement */
8278 				tsns_sent = data_list[0]->rec.data.TSN_seq;
8279 				/* fill time if not already filled */
8280 				if (*now_filled == 0) {
8281 					(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8282 					*now_filled = 1;
8283 					*now = asoc->time_last_sent;
8284 				} else {
8285 					asoc->time_last_sent = *now;
8286 				}
8287 				data_list[0]->do_rtt = 1;
8288 				SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8289 				sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8290 				if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8291 					if (net->flight_size < net->cwnd) {
8292 						/* start or restart it */
8293 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8294 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8295 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
8296 						}
8297 						SCTP_STAT_INCR(sctps_earlyfrstrout);
8298 						sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
8299 					} else {
8300 						/* stop it if its running */
8301 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8302 							SCTP_STAT_INCR(sctps_earlyfrstpout);
8303 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8304 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
8305 						}
8306 					}
8307 				}
8308 			}
8309 			if (one_chunk) {
8310 				break;
8311 			}
8312 		}
8313 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8314 			sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8315 		}
8316 	}
8317 	if (old_startat == NULL) {
8318 		old_startat = send_start_at;
8319 		send_start_at = TAILQ_FIRST(&asoc->nets);
8320 		if (old_startat)
8321 			goto again_one_more_time;
8322 	}
8323 	/*
8324 	 * At the end there should be no NON timed chunks hanging on this
8325 	 * queue.
8326 	 */
8327 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8328 		sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8329 	}
8330 	if ((*num_out == 0) && (*reason_code == 0)) {
8331 		*reason_code = 4;
8332 	} else {
8333 		*reason_code = 5;
8334 	}
8335 	sctp_clean_up_ctl(stcb, asoc);
8336 	return (0);
8337 }
8338 
8339 void
8340 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8341 {
8342 	/*-
8343 	 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8344 	 * the control chunk queue.
8345 	 */
8346 	struct sctp_chunkhdr *hdr;
8347 	struct sctp_tmit_chunk *chk;
8348 	struct mbuf *mat;
8349 
8350 	SCTP_TCB_LOCK_ASSERT(stcb);
8351 	sctp_alloc_a_chunk(stcb, chk);
8352 	if (chk == NULL) {
8353 		/* no memory */
8354 		sctp_m_freem(op_err);
8355 		return;
8356 	}
8357 	chk->copy_by_ref = 0;
8358 	SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8359 	if (op_err == NULL) {
8360 		sctp_free_a_chunk(stcb, chk);
8361 		return;
8362 	}
8363 	chk->send_size = 0;
8364 	mat = op_err;
8365 	while (mat != NULL) {
8366 		chk->send_size += SCTP_BUF_LEN(mat);
8367 		mat = SCTP_BUF_NEXT(mat);
8368 	}
8369 	chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8370 	chk->rec.chunk_id.can_take_data = 1;
8371 	chk->sent = SCTP_DATAGRAM_UNSENT;
8372 	chk->snd_count = 0;
8373 	chk->flags = 0;
8374 	chk->asoc = &stcb->asoc;
8375 	chk->data = op_err;
8376 	chk->whoTo = chk->asoc->primary_destination;
8377 	atomic_add_int(&chk->whoTo->ref_count, 1);
8378 	hdr = mtod(op_err, struct sctp_chunkhdr *);
8379 	hdr->chunk_type = SCTP_OPERATION_ERROR;
8380 	hdr->chunk_flags = 0;
8381 	hdr->chunk_length = htons(chk->send_size);
8382 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8383 	    chk,
8384 	    sctp_next);
8385 	chk->asoc->ctrl_queue_cnt++;
8386 }
8387 
8388 int
8389 sctp_send_cookie_echo(struct mbuf *m,
8390     int offset,
8391     struct sctp_tcb *stcb,
8392     struct sctp_nets *net)
8393 {
8394 	/*-
8395 	 * pull out the cookie and put it at the front of the control chunk
8396 	 * queue.
8397 	 */
8398 	int at;
8399 	struct mbuf *cookie;
8400 	struct sctp_paramhdr parm, *phdr;
8401 	struct sctp_chunkhdr *hdr;
8402 	struct sctp_tmit_chunk *chk;
8403 	uint16_t ptype, plen;
8404 
8405 	/* First find the cookie in the param area */
8406 	cookie = NULL;
8407 	at = offset + sizeof(struct sctp_init_chunk);
8408 
8409 	SCTP_TCB_LOCK_ASSERT(stcb);
8410 	do {
8411 		phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8412 		if (phdr == NULL) {
8413 			return (-3);
8414 		}
8415 		ptype = ntohs(phdr->param_type);
8416 		plen = ntohs(phdr->param_length);
8417 		if (ptype == SCTP_STATE_COOKIE) {
8418 			int pad;
8419 
8420 			/* found the cookie */
8421 			if ((pad = (plen % 4))) {
8422 				plen += 4 - pad;
8423 			}
8424 			cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8425 			if (cookie == NULL) {
8426 				/* No memory */
8427 				return (-2);
8428 			}
8429 #ifdef SCTP_MBUF_LOGGING
8430 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8431 				struct mbuf *mat;
8432 
8433 				mat = cookie;
8434 				while (mat) {
8435 					if (SCTP_BUF_IS_EXTENDED(mat)) {
8436 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8437 					}
8438 					mat = SCTP_BUF_NEXT(mat);
8439 				}
8440 			}
8441 #endif
8442 			break;
8443 		}
8444 		at += SCTP_SIZE32(plen);
8445 	} while (phdr);
8446 	if (cookie == NULL) {
8447 		/* Did not find the cookie */
8448 		return (-3);
8449 	}
8450 	/* ok, we got the cookie lets change it into a cookie echo chunk */
8451 
8452 	/* first the change from param to cookie */
8453 	hdr = mtod(cookie, struct sctp_chunkhdr *);
8454 	hdr->chunk_type = SCTP_COOKIE_ECHO;
8455 	hdr->chunk_flags = 0;
8456 	/* get the chunk stuff now and place it in the FRONT of the queue */
8457 	sctp_alloc_a_chunk(stcb, chk);
8458 	if (chk == NULL) {
8459 		/* no memory */
8460 		sctp_m_freem(cookie);
8461 		return (-5);
8462 	}
8463 	chk->copy_by_ref = 0;
8464 	chk->send_size = plen;
8465 	chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8466 	chk->rec.chunk_id.can_take_data = 0;
8467 	chk->sent = SCTP_DATAGRAM_UNSENT;
8468 	chk->snd_count = 0;
8469 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8470 	chk->asoc = &stcb->asoc;
8471 	chk->data = cookie;
8472 	chk->whoTo = chk->asoc->primary_destination;
8473 	atomic_add_int(&chk->whoTo->ref_count, 1);
8474 	TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8475 	chk->asoc->ctrl_queue_cnt++;
8476 	return (0);
8477 }
8478 
8479 void
8480 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8481     struct mbuf *m,
8482     int offset,
8483     int chk_length,
8484     struct sctp_nets *net)
8485 {
8486 	/*
8487 	 * take a HB request and make it into a HB ack and send it.
8488 	 */
8489 	struct mbuf *outchain;
8490 	struct sctp_chunkhdr *chdr;
8491 	struct sctp_tmit_chunk *chk;
8492 
8493 
8494 	if (net == NULL)
8495 		/* must have a net pointer */
8496 		return;
8497 
8498 	outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
8499 	if (outchain == NULL) {
8500 		/* gak out of memory */
8501 		return;
8502 	}
8503 #ifdef SCTP_MBUF_LOGGING
8504 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8505 		struct mbuf *mat;
8506 
8507 		mat = outchain;
8508 		while (mat) {
8509 			if (SCTP_BUF_IS_EXTENDED(mat)) {
8510 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8511 			}
8512 			mat = SCTP_BUF_NEXT(mat);
8513 		}
8514 	}
8515 #endif
8516 	chdr = mtod(outchain, struct sctp_chunkhdr *);
8517 	chdr->chunk_type = SCTP_HEARTBEAT_ACK;
8518 	chdr->chunk_flags = 0;
8519 	if (chk_length % 4) {
8520 		/* need pad */
8521 		uint32_t cpthis = 0;
8522 		int padlen;
8523 
8524 		padlen = 4 - (chk_length % 4);
8525 		m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
8526 	}
8527 	sctp_alloc_a_chunk(stcb, chk);
8528 	if (chk == NULL) {
8529 		/* no memory */
8530 		sctp_m_freem(outchain);
8531 		return;
8532 	}
8533 	chk->copy_by_ref = 0;
8534 	chk->send_size = chk_length;
8535 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
8536 	chk->rec.chunk_id.can_take_data = 1;
8537 	chk->sent = SCTP_DATAGRAM_UNSENT;
8538 	chk->snd_count = 0;
8539 	chk->flags = 0;
8540 	chk->asoc = &stcb->asoc;
8541 	chk->data = outchain;
8542 	chk->whoTo = net;
8543 	atomic_add_int(&chk->whoTo->ref_count, 1);
8544 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8545 	chk->asoc->ctrl_queue_cnt++;
8546 }
8547 
8548 void
8549 sctp_send_cookie_ack(struct sctp_tcb *stcb)
8550 {
8551 	/* formulate and queue a cookie-ack back to sender */
8552 	struct mbuf *cookie_ack;
8553 	struct sctp_chunkhdr *hdr;
8554 	struct sctp_tmit_chunk *chk;
8555 
8556 	cookie_ack = NULL;
8557 	SCTP_TCB_LOCK_ASSERT(stcb);
8558 
8559 	cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
8560 	if (cookie_ack == NULL) {
8561 		/* no mbuf's */
8562 		return;
8563 	}
8564 	SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
8565 	sctp_alloc_a_chunk(stcb, chk);
8566 	if (chk == NULL) {
8567 		/* no memory */
8568 		sctp_m_freem(cookie_ack);
8569 		return;
8570 	}
8571 	chk->copy_by_ref = 0;
8572 	chk->send_size = sizeof(struct sctp_chunkhdr);
8573 	chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
8574 	chk->rec.chunk_id.can_take_data = 1;
8575 	chk->sent = SCTP_DATAGRAM_UNSENT;
8576 	chk->snd_count = 0;
8577 	chk->flags = 0;
8578 	chk->asoc = &stcb->asoc;
8579 	chk->data = cookie_ack;
8580 	if (chk->asoc->last_control_chunk_from != NULL) {
8581 		chk->whoTo = chk->asoc->last_control_chunk_from;
8582 	} else {
8583 		chk->whoTo = chk->asoc->primary_destination;
8584 	}
8585 	atomic_add_int(&chk->whoTo->ref_count, 1);
8586 	hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
8587 	hdr->chunk_type = SCTP_COOKIE_ACK;
8588 	hdr->chunk_flags = 0;
8589 	hdr->chunk_length = htons(chk->send_size);
8590 	SCTP_BUF_LEN(cookie_ack) = chk->send_size;
8591 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8592 	chk->asoc->ctrl_queue_cnt++;
8593 	return;
8594 }
8595 
8596 
8597 void
8598 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
8599 {
8600 	/* formulate and queue a SHUTDOWN-ACK back to the sender */
8601 	struct mbuf *m_shutdown_ack;
8602 	struct sctp_shutdown_ack_chunk *ack_cp;
8603 	struct sctp_tmit_chunk *chk;
8604 
8605 	m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8606 	if (m_shutdown_ack == NULL) {
8607 		/* no mbuf's */
8608 		return;
8609 	}
8610 	SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
8611 	sctp_alloc_a_chunk(stcb, chk);
8612 	if (chk == NULL) {
8613 		/* no memory */
8614 		sctp_m_freem(m_shutdown_ack);
8615 		return;
8616 	}
8617 	chk->copy_by_ref = 0;
8618 	chk->send_size = sizeof(struct sctp_chunkhdr);
8619 	chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
8620 	chk->rec.chunk_id.can_take_data = 1;
8621 	chk->sent = SCTP_DATAGRAM_UNSENT;
8622 	chk->snd_count = 0;
8623 	chk->flags = 0;
8624 	chk->asoc = &stcb->asoc;
8625 	chk->data = m_shutdown_ack;
8626 	chk->whoTo = net;
8627 	atomic_add_int(&net->ref_count, 1);
8628 
8629 	ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
8630 	ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
8631 	ack_cp->ch.chunk_flags = 0;
8632 	ack_cp->ch.chunk_length = htons(chk->send_size);
8633 	SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
8634 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8635 	chk->asoc->ctrl_queue_cnt++;
8636 	return;
8637 }
8638 
8639 void
8640 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
8641 {
8642 	/* formulate and queue a SHUTDOWN to the sender */
8643 	struct mbuf *m_shutdown;
8644 	struct sctp_shutdown_chunk *shutdown_cp;
8645 	struct sctp_tmit_chunk *chk;
8646 
8647 	m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8648 	if (m_shutdown == NULL) {
8649 		/* no mbuf's */
8650 		return;
8651 	}
8652 	SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
8653 	sctp_alloc_a_chunk(stcb, chk);
8654 	if (chk == NULL) {
8655 		/* no memory */
8656 		sctp_m_freem(m_shutdown);
8657 		return;
8658 	}
8659 	chk->copy_by_ref = 0;
8660 	chk->send_size = sizeof(struct sctp_shutdown_chunk);
8661 	chk->rec.chunk_id.id = SCTP_SHUTDOWN;
8662 	chk->rec.chunk_id.can_take_data = 1;
8663 	chk->sent = SCTP_DATAGRAM_UNSENT;
8664 	chk->snd_count = 0;
8665 	chk->flags = 0;
8666 	chk->asoc = &stcb->asoc;
8667 	chk->data = m_shutdown;
8668 	chk->whoTo = net;
8669 	atomic_add_int(&net->ref_count, 1);
8670 
8671 	shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
8672 	shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
8673 	shutdown_cp->ch.chunk_flags = 0;
8674 	shutdown_cp->ch.chunk_length = htons(chk->send_size);
8675 	shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
8676 	SCTP_BUF_LEN(m_shutdown) = chk->send_size;
8677 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8678 	chk->asoc->ctrl_queue_cnt++;
8679 	return;
8680 }
8681 
8682 void
8683 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
8684 {
8685 	/*
8686 	 * formulate and queue an ASCONF to the peer. ASCONF parameters
8687 	 * should be queued on the assoc queue.
8688 	 */
8689 	struct sctp_tmit_chunk *chk;
8690 	struct mbuf *m_asconf;
8691 	int len;
8692 
8693 	SCTP_TCB_LOCK_ASSERT(stcb);
8694 
8695 	if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
8696 	    (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
8697 		/* can't send a new one if there is one in flight already */
8698 		return;
8699 	}
8700 	/* compose an ASCONF chunk, maximum length is PMTU */
8701 	m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
8702 	if (m_asconf == NULL) {
8703 		return;
8704 	}
8705 	sctp_alloc_a_chunk(stcb, chk);
8706 	if (chk == NULL) {
8707 		/* no memory */
8708 		sctp_m_freem(m_asconf);
8709 		return;
8710 	}
8711 	chk->copy_by_ref = 0;
8712 	chk->data = m_asconf;
8713 	chk->send_size = len;
8714 	chk->rec.chunk_id.id = SCTP_ASCONF;
8715 	chk->rec.chunk_id.can_take_data = 0;
8716 	chk->sent = SCTP_DATAGRAM_UNSENT;
8717 	chk->snd_count = 0;
8718 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8719 	chk->asoc = &stcb->asoc;
8720 	chk->whoTo = net;
8721 	atomic_add_int(&chk->whoTo->ref_count, 1);
8722 	TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
8723 	chk->asoc->ctrl_queue_cnt++;
8724 	return;
8725 }
8726 
8727 void
8728 sctp_send_asconf_ack(struct sctp_tcb *stcb)
8729 {
8730 	/*
8731 	 * formulate and queue a asconf-ack back to sender. the asconf-ack
8732 	 * must be stored in the tcb.
8733 	 */
8734 	struct sctp_tmit_chunk *chk;
8735 	struct sctp_asconf_ack *ack, *latest_ack;
8736 	struct mbuf *m_ack, *m;
8737 	struct sctp_nets *net = NULL;
8738 
8739 	SCTP_TCB_LOCK_ASSERT(stcb);
8740 	/* Get the latest ASCONF-ACK */
8741 	latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
8742 	if (latest_ack == NULL) {
8743 		return;
8744 	}
8745 	if (latest_ack->last_sent_to != NULL &&
8746 	    latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
8747 		/* we're doing a retransmission */
8748 		net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
8749 		if (net == NULL) {
8750 			/* no alternate */
8751 			if (stcb->asoc.last_control_chunk_from == NULL)
8752 				net = stcb->asoc.primary_destination;
8753 			else
8754 				net = stcb->asoc.last_control_chunk_from;
8755 		}
8756 	} else {
8757 		/* normal case */
8758 		if (stcb->asoc.last_control_chunk_from == NULL)
8759 			net = stcb->asoc.primary_destination;
8760 		else
8761 			net = stcb->asoc.last_control_chunk_from;
8762 	}
8763 	latest_ack->last_sent_to = net;
8764 
8765 	TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
8766 		if (ack->data == NULL) {
8767 			continue;
8768 		}
8769 		/* copy the asconf_ack */
8770 		m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
8771 		if (m_ack == NULL) {
8772 			/* couldn't copy it */
8773 			return;
8774 		}
8775 #ifdef SCTP_MBUF_LOGGING
8776 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8777 			struct mbuf *mat;
8778 
8779 			mat = m_ack;
8780 			while (mat) {
8781 				if (SCTP_BUF_IS_EXTENDED(mat)) {
8782 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8783 				}
8784 				mat = SCTP_BUF_NEXT(mat);
8785 			}
8786 		}
8787 #endif
8788 
8789 		sctp_alloc_a_chunk(stcb, chk);
8790 		if (chk == NULL) {
8791 			/* no memory */
8792 			if (m_ack)
8793 				sctp_m_freem(m_ack);
8794 			return;
8795 		}
8796 		chk->copy_by_ref = 0;
8797 
8798 		chk->whoTo = net;
8799 		chk->data = m_ack;
8800 		chk->send_size = 0;
8801 		/* Get size */
8802 		m = m_ack;
8803 		chk->send_size = ack->len;
8804 		chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8805 		chk->rec.chunk_id.can_take_data = 1;
8806 		chk->sent = SCTP_DATAGRAM_UNSENT;
8807 		chk->snd_count = 0;
8808 		chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;	/* XXX */
8809 		chk->asoc = &stcb->asoc;
8810 		atomic_add_int(&chk->whoTo->ref_count, 1);
8811 
8812 		TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8813 		chk->asoc->ctrl_queue_cnt++;
8814 	}
8815 	return;
8816 }
8817 
8818 
8819 static int
8820 sctp_chunk_retransmission(struct sctp_inpcb *inp,
8821     struct sctp_tcb *stcb,
8822     struct sctp_association *asoc,
8823     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
8824 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8825     SCTP_UNUSED
8826 #endif
8827 )
8828 {
8829 	/*-
8830 	 * send out one MTU of retransmission. If fast_retransmit is
8831 	 * happening we ignore the cwnd. Otherwise we obey the cwnd and
8832 	 * rwnd. For a Cookie or Asconf in the control chunk queue we
8833 	 * retransmit them by themselves.
8834 	 *
8835 	 * For data chunks we will pick out the lowest TSN's in the sent_queue
8836 	 * marked for resend and bundle them all together (up to a MTU of
8837 	 * destination). The address to send to should have been
8838 	 * selected/changed where the retransmission was marked (i.e. in FR
8839 	 * or t3-timeout routines).
8840 	 */
8841 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8842 	struct sctp_tmit_chunk *chk, *fwd;
8843 	struct mbuf *m, *endofchain;
8844 	struct sctphdr *shdr;
8845 	struct sctp_nets *net = NULL;
8846 	uint32_t tsns_sent = 0;
8847 	int no_fragmentflg, bundle_at, cnt_thru;
8848 	unsigned int mtu;
8849 	int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
8850 	struct sctp_auth_chunk *auth = NULL;
8851 	uint32_t auth_offset = 0;
8852 	uint32_t dmtu = 0;
8853 
8854 	SCTP_TCB_LOCK_ASSERT(stcb);
8855 	tmr_started = ctl_cnt = bundle_at = error = 0;
8856 	no_fragmentflg = 1;
8857 	fwd_tsn = 0;
8858 	*cnt_out = 0;
8859 	fwd = NULL;
8860 	endofchain = m = NULL;
8861 #ifdef SCTP_AUDITING_ENABLED
8862 	sctp_audit_log(0xC3, 1);
8863 #endif
8864 	if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
8865 	    (TAILQ_EMPTY(&asoc->control_send_queue))) {
8866 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
8867 		    asoc->sent_queue_retran_cnt);
8868 		asoc->sent_queue_cnt = 0;
8869 		asoc->sent_queue_cnt_removeable = 0;
8870 		/* send back 0/0 so we enter normal transmission */
8871 		*cnt_out = 0;
8872 		return (0);
8873 	}
8874 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8875 		if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
8876 		    (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
8877 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
8878 			if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8879 				if (chk != asoc->str_reset) {
8880 					/*
8881 					 * not eligible for retran if its
8882 					 * not ours
8883 					 */
8884 					continue;
8885 				}
8886 			}
8887 			ctl_cnt++;
8888 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8889 				fwd_tsn = 1;
8890 				fwd = chk;
8891 			}
8892 			/*
8893 			 * Add an AUTH chunk, if chunk requires it save the
8894 			 * offset into the chain for AUTH
8895 			 */
8896 			if ((auth == NULL) &&
8897 			    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8898 			    stcb->asoc.peer_auth_chunks))) {
8899 				m = sctp_add_auth_chunk(m, &endofchain,
8900 				    &auth, &auth_offset,
8901 				    stcb,
8902 				    chk->rec.chunk_id.id);
8903 			}
8904 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8905 			break;
8906 		}
8907 	}
8908 	one_chunk = 0;
8909 	cnt_thru = 0;
8910 	/* do we have control chunks to retransmit? */
8911 	if (m != NULL) {
8912 		/* Start a timer no matter if we suceed or fail */
8913 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8914 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
8915 		} else if (chk->rec.chunk_id.id == SCTP_ASCONF)
8916 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
8917 
8918 		SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
8919 		if (m == NULL) {
8920 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
8921 			return (ENOBUFS);
8922 		}
8923 		shdr = mtod(m, struct sctphdr *);
8924 		shdr->src_port = inp->sctp_lport;
8925 		shdr->dest_port = stcb->rport;
8926 		shdr->v_tag = htonl(stcb->asoc.peer_vtag);
8927 		shdr->checksum = 0;
8928 		auth_offset += sizeof(struct sctphdr);
8929 		chk->snd_count++;	/* update our count */
8930 
8931 		if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
8932 		    (struct sockaddr *)&chk->whoTo->ro._l_addr, m, auth_offset,
8933 		    auth, no_fragmentflg, 0, NULL, 0, chk->whoTo->port, so_locked, NULL))) {
8934 			SCTP_STAT_INCR(sctps_lowlevelerr);
8935 			return (error);
8936 		}
8937 		m = endofchain = NULL;
8938 		auth = NULL;
8939 		auth_offset = 0;
8940 		/*
8941 		 * We don't want to mark the net->sent time here since this
8942 		 * we use this for HB and retrans cannot measure RTT
8943 		 */
8944 		/* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
8945 		*cnt_out += 1;
8946 		chk->sent = SCTP_DATAGRAM_SENT;
8947 		sctp_ucount_decr(asoc->sent_queue_retran_cnt);
8948 		if (fwd_tsn == 0) {
8949 			return (0);
8950 		} else {
8951 			/* Clean up the fwd-tsn list */
8952 			sctp_clean_up_ctl(stcb, asoc);
8953 			return (0);
8954 		}
8955 	}
8956 	/*
8957 	 * Ok, it is just data retransmission we need to do or that and a
8958 	 * fwd-tsn with it all.
8959 	 */
8960 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
8961 		return (SCTP_RETRAN_DONE);
8962 	}
8963 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
8964 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
8965 		/* not yet open, resend the cookie and that is it */
8966 		return (1);
8967 	}
8968 #ifdef SCTP_AUDITING_ENABLED
8969 	sctp_auditing(20, inp, stcb, NULL);
8970 #endif
8971 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
8972 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
8973 			/* No, not sent to this net or not ready for rtx */
8974 			continue;
8975 		}
8976 		if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
8977 		    (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
8978 			/* Gak, we have exceeded max unlucky retran, abort! */
8979 			SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
8980 			    chk->snd_count,
8981 			    SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
8982 			atomic_add_int(&stcb->asoc.refcnt, 1);
8983 			sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
8984 			SCTP_TCB_LOCK(stcb);
8985 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
8986 			return (SCTP_RETRAN_EXIT);
8987 		}
8988 		/* pick up the net */
8989 		net = chk->whoTo;
8990 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8991 			mtu = (net->mtu - SCTP_MIN_OVERHEAD);
8992 		} else {
8993 			mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8994 		}
8995 
8996 		if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
8997 			/* No room in peers rwnd */
8998 			uint32_t tsn;
8999 
9000 			tsn = asoc->last_acked_seq + 1;
9001 			if (tsn == chk->rec.data.TSN_seq) {
9002 				/*
9003 				 * we make a special exception for this
9004 				 * case. The peer has no rwnd but is missing
9005 				 * the lowest chunk.. which is probably what
9006 				 * is holding up the rwnd.
9007 				 */
9008 				goto one_chunk_around;
9009 			}
9010 			return (1);
9011 		}
9012 one_chunk_around:
9013 		if (asoc->peers_rwnd < mtu) {
9014 			one_chunk = 1;
9015 			if ((asoc->peers_rwnd == 0) &&
9016 			    (asoc->total_flight == 0)) {
9017 				chk->window_probe = 1;
9018 				chk->whoTo->window_probe = 1;
9019 			}
9020 		}
9021 #ifdef SCTP_AUDITING_ENABLED
9022 		sctp_audit_log(0xC3, 2);
9023 #endif
9024 		bundle_at = 0;
9025 		m = NULL;
9026 		net->fast_retran_ip = 0;
9027 		if (chk->rec.data.doing_fast_retransmit == 0) {
9028 			/*
9029 			 * if no FR in progress skip destination that have
9030 			 * flight_size > cwnd.
9031 			 */
9032 			if (net->flight_size >= net->cwnd) {
9033 				continue;
9034 			}
9035 		} else {
9036 			/*
9037 			 * Mark the destination net to have FR recovery
9038 			 * limits put on it.
9039 			 */
9040 			*fr_done = 1;
9041 			net->fast_retran_ip = 1;
9042 		}
9043 
9044 		/*
9045 		 * if no AUTH is yet included and this chunk requires it,
9046 		 * make sure to account for it.  We don't apply the size
9047 		 * until the AUTH chunk is actually added below in case
9048 		 * there is no room for this chunk.
9049 		 */
9050 		if ((auth == NULL) &&
9051 		    sctp_auth_is_required_chunk(SCTP_DATA,
9052 		    stcb->asoc.peer_auth_chunks)) {
9053 			dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9054 		} else
9055 			dmtu = 0;
9056 
9057 		if ((chk->send_size <= (mtu - dmtu)) ||
9058 		    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9059 			/* ok we will add this one */
9060 			if ((auth == NULL) &&
9061 			    (sctp_auth_is_required_chunk(SCTP_DATA,
9062 			    stcb->asoc.peer_auth_chunks))) {
9063 				m = sctp_add_auth_chunk(m, &endofchain,
9064 				    &auth, &auth_offset,
9065 				    stcb, SCTP_DATA);
9066 			}
9067 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9068 			if (m == NULL) {
9069 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9070 				return (ENOMEM);
9071 			}
9072 			/* Do clear IP_DF ? */
9073 			if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9074 				no_fragmentflg = 0;
9075 			}
9076 			/* upate our MTU size */
9077 			if (mtu > (chk->send_size + dmtu))
9078 				mtu -= (chk->send_size + dmtu);
9079 			else
9080 				mtu = 0;
9081 			data_list[bundle_at++] = chk;
9082 			if (one_chunk && (asoc->total_flight <= 0)) {
9083 				SCTP_STAT_INCR(sctps_windowprobed);
9084 			}
9085 		}
9086 		if (one_chunk == 0) {
9087 			/*
9088 			 * now are there anymore forward from chk to pick
9089 			 * up?
9090 			 */
9091 			fwd = TAILQ_NEXT(chk, sctp_next);
9092 			while (fwd) {
9093 				if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9094 					/* Nope, not for retran */
9095 					fwd = TAILQ_NEXT(fwd, sctp_next);
9096 					continue;
9097 				}
9098 				if (fwd->whoTo != net) {
9099 					/* Nope, not the net in question */
9100 					fwd = TAILQ_NEXT(fwd, sctp_next);
9101 					continue;
9102 				}
9103 				if ((auth == NULL) &&
9104 				    sctp_auth_is_required_chunk(SCTP_DATA,
9105 				    stcb->asoc.peer_auth_chunks)) {
9106 					dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9107 				} else
9108 					dmtu = 0;
9109 				if (fwd->send_size <= (mtu - dmtu)) {
9110 					if ((auth == NULL) &&
9111 					    (sctp_auth_is_required_chunk(SCTP_DATA,
9112 					    stcb->asoc.peer_auth_chunks))) {
9113 						m = sctp_add_auth_chunk(m,
9114 						    &endofchain,
9115 						    &auth, &auth_offset,
9116 						    stcb,
9117 						    SCTP_DATA);
9118 					}
9119 					m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9120 					if (m == NULL) {
9121 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9122 						return (ENOMEM);
9123 					}
9124 					/* Do clear IP_DF ? */
9125 					if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9126 						no_fragmentflg = 0;
9127 					}
9128 					/* upate our MTU size */
9129 					if (mtu > (fwd->send_size + dmtu))
9130 						mtu -= (fwd->send_size + dmtu);
9131 					else
9132 						mtu = 0;
9133 					data_list[bundle_at++] = fwd;
9134 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9135 						break;
9136 					}
9137 					fwd = TAILQ_NEXT(fwd, sctp_next);
9138 				} else {
9139 					/* can't fit so we are done */
9140 					break;
9141 				}
9142 			}
9143 		}
9144 		/* Is there something to send for this destination? */
9145 		if (m) {
9146 			/*
9147 			 * No matter if we fail/or suceed we should start a
9148 			 * timer. A failure is like a lost IP packet :-)
9149 			 */
9150 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9151 				/*
9152 				 * no timer running on this destination
9153 				 * restart it.
9154 				 */
9155 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9156 				tmr_started = 1;
9157 			}
9158 			SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
9159 			if (m == NULL) {
9160 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
9161 				return (ENOBUFS);
9162 			}
9163 			shdr = mtod(m, struct sctphdr *);
9164 			shdr->src_port = inp->sctp_lport;
9165 			shdr->dest_port = stcb->rport;
9166 			shdr->v_tag = htonl(stcb->asoc.peer_vtag);
9167 			shdr->checksum = 0;
9168 			auth_offset += sizeof(struct sctphdr);
9169 			/* Now lets send it, if there is anything to send :> */
9170 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9171 			    (struct sockaddr *)&net->ro._l_addr, m, auth_offset,
9172 			    auth, no_fragmentflg, 0, NULL, 0, net->port, so_locked, NULL))) {
9173 				/* error, we could not output */
9174 				SCTP_STAT_INCR(sctps_lowlevelerr);
9175 				return (error);
9176 			}
9177 			m = endofchain = NULL;
9178 			auth = NULL;
9179 			auth_offset = 0;
9180 			/* For HB's */
9181 			/*
9182 			 * We don't want to mark the net->sent time here
9183 			 * since this we use this for HB and retrans cannot
9184 			 * measure RTT
9185 			 */
9186 			/* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9187 
9188 			/* For auto-close */
9189 			cnt_thru++;
9190 			if (*now_filled == 0) {
9191 				(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9192 				*now = asoc->time_last_sent;
9193 				*now_filled = 1;
9194 			} else {
9195 				asoc->time_last_sent = *now;
9196 			}
9197 			*cnt_out += bundle_at;
9198 #ifdef SCTP_AUDITING_ENABLED
9199 			sctp_audit_log(0xC4, bundle_at);
9200 #endif
9201 			if (bundle_at) {
9202 				tsns_sent = data_list[0]->rec.data.TSN_seq;
9203 			}
9204 			for (i = 0; i < bundle_at; i++) {
9205 				SCTP_STAT_INCR(sctps_sendretransdata);
9206 				data_list[i]->sent = SCTP_DATAGRAM_SENT;
9207 				/*
9208 				 * When we have a revoked data, and we
9209 				 * retransmit it, then we clear the revoked
9210 				 * flag since this flag dictates if we
9211 				 * subtracted from the fs
9212 				 */
9213 				if (data_list[i]->rec.data.chunk_was_revoked) {
9214 					/* Deflate the cwnd */
9215 					data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9216 					data_list[i]->rec.data.chunk_was_revoked = 0;
9217 				}
9218 				data_list[i]->snd_count++;
9219 				sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9220 				/* record the time */
9221 				data_list[i]->sent_rcv_time = asoc->time_last_sent;
9222 				if (data_list[i]->book_size_scale) {
9223 					/*
9224 					 * need to double the book size on
9225 					 * this one
9226 					 */
9227 					data_list[i]->book_size_scale = 0;
9228 					/*
9229 					 * Since we double the booksize, we
9230 					 * must also double the output queue
9231 					 * size, since this get shrunk when
9232 					 * we free by this amount.
9233 					 */
9234 					atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9235 					data_list[i]->book_size *= 2;
9236 
9237 
9238 				} else {
9239 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9240 						sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9241 						    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9242 					}
9243 					asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9244 					    (uint32_t) (data_list[i]->send_size +
9245 					    SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9246 				}
9247 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9248 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9249 					    data_list[i]->whoTo->flight_size,
9250 					    data_list[i]->book_size,
9251 					    (uintptr_t) data_list[i]->whoTo,
9252 					    data_list[i]->rec.data.TSN_seq);
9253 				}
9254 				sctp_flight_size_increase(data_list[i]);
9255 				sctp_total_flight_increase(stcb, data_list[i]);
9256 				if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9257 					/* SWS sender side engages */
9258 					asoc->peers_rwnd = 0;
9259 				}
9260 				if ((i == 0) &&
9261 				    (data_list[i]->rec.data.doing_fast_retransmit)) {
9262 					SCTP_STAT_INCR(sctps_sendfastretrans);
9263 					if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9264 					    (tmr_started == 0)) {
9265 						/*-
9266 						 * ok we just fast-retrans'd
9267 						 * the lowest TSN, i.e the
9268 						 * first on the list. In
9269 						 * this case we want to give
9270 						 * some more time to get a
9271 						 * SACK back without a
9272 						 * t3-expiring.
9273 						 */
9274 						sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9275 						    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9276 						sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9277 					}
9278 				}
9279 			}
9280 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9281 				sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9282 			}
9283 #ifdef SCTP_AUDITING_ENABLED
9284 			sctp_auditing(21, inp, stcb, NULL);
9285 #endif
9286 		} else {
9287 			/* None will fit */
9288 			return (1);
9289 		}
9290 		if (asoc->sent_queue_retran_cnt <= 0) {
9291 			/* all done we have no more to retran */
9292 			asoc->sent_queue_retran_cnt = 0;
9293 			break;
9294 		}
9295 		if (one_chunk) {
9296 			/* No more room in rwnd */
9297 			return (1);
9298 		}
9299 		/* stop the for loop here. we sent out a packet */
9300 		break;
9301 	}
9302 	return (0);
9303 }
9304 
9305 
9306 static int
9307 sctp_timer_validation(struct sctp_inpcb *inp,
9308     struct sctp_tcb *stcb,
9309     struct sctp_association *asoc,
9310     int ret)
9311 {
9312 	struct sctp_nets *net;
9313 
9314 	/* Validate that a timer is running somewhere */
9315 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9316 		if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9317 			/* Here is a timer */
9318 			return (ret);
9319 		}
9320 	}
9321 	SCTP_TCB_LOCK_ASSERT(stcb);
9322 	/* Gak, we did not have a timer somewhere */
9323 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9324 	sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9325 	return (ret);
9326 }
9327 
9328 void
9329 sctp_chunk_output(struct sctp_inpcb *inp,
9330     struct sctp_tcb *stcb,
9331     int from_where,
9332     int so_locked
9333 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9334     SCTP_UNUSED
9335 #endif
9336 )
9337 {
9338 	/*-
9339 	 * Ok this is the generic chunk service queue. we must do the
9340 	 * following:
9341 	 * - See if there are retransmits pending, if so we must
9342 	 *   do these first.
9343 	 * - Service the stream queue that is next, moving any
9344 	 *   message (note I must get a complete message i.e.
9345 	 *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9346 	 *   TSN's
9347 	 * - Check to see if the cwnd/rwnd allows any output, if so we
9348 	 *   go ahead and fomulate and send the low level chunks. Making sure
9349 	 *   to combine any control in the control chunk queue also.
9350 	 */
9351 	struct sctp_association *asoc;
9352 	struct sctp_nets *net;
9353 	int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
9354 	    burst_cnt = 0, burst_limit = 0;
9355 	struct timeval now;
9356 	int now_filled = 0;
9357 	int cwnd_full = 0;
9358 	int nagle_on = 0;
9359 	int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9360 	int un_sent = 0;
9361 	int fr_done, tot_frs = 0;
9362 
9363 	asoc = &stcb->asoc;
9364 	if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9365 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9366 			nagle_on = 0;
9367 		} else {
9368 			nagle_on = 1;
9369 		}
9370 	}
9371 	SCTP_TCB_LOCK_ASSERT(stcb);
9372 
9373 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9374 
9375 	if ((un_sent <= 0) &&
9376 	    (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9377 	    (asoc->sent_queue_retran_cnt == 0)) {
9378 		/* Nothing to do unless there is something to be sent left */
9379 		return;
9380 	}
9381 	/*
9382 	 * Do we have something to send, data or control AND a sack timer
9383 	 * running, if so piggy-back the sack.
9384 	 */
9385 	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9386 		sctp_send_sack(stcb);
9387 		(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9388 	}
9389 	while (asoc->sent_queue_retran_cnt) {
9390 		/*-
9391 		 * Ok, it is retransmission time only, we send out only ONE
9392 		 * packet with a single call off to the retran code.
9393 		 */
9394 		if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9395 			/*-
9396 			 * Special hook for handling cookiess discarded
9397 			 * by peer that carried data. Send cookie-ack only
9398 			 * and then the next call with get the retran's.
9399 			 */
9400 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9401 			    &cwnd_full, from_where,
9402 			    &now, &now_filled, frag_point, so_locked);
9403 			return;
9404 		} else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9405 			/* if its not from a HB then do it */
9406 			fr_done = 0;
9407 			ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9408 			if (fr_done) {
9409 				tot_frs++;
9410 			}
9411 		} else {
9412 			/*
9413 			 * its from any other place, we don't allow retran
9414 			 * output (only control)
9415 			 */
9416 			ret = 1;
9417 		}
9418 		if (ret > 0) {
9419 			/* Can't send anymore */
9420 			/*-
9421 			 * now lets push out control by calling med-level
9422 			 * output once. this assures that we WILL send HB's
9423 			 * if queued too.
9424 			 */
9425 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9426 			    &cwnd_full, from_where,
9427 			    &now, &now_filled, frag_point, so_locked);
9428 #ifdef SCTP_AUDITING_ENABLED
9429 			sctp_auditing(8, inp, stcb, NULL);
9430 #endif
9431 			(void)sctp_timer_validation(inp, stcb, asoc, ret);
9432 			return;
9433 		}
9434 		if (ret < 0) {
9435 			/*-
9436 			 * The count was off.. retran is not happening so do
9437 			 * the normal retransmission.
9438 			 */
9439 #ifdef SCTP_AUDITING_ENABLED
9440 			sctp_auditing(9, inp, stcb, NULL);
9441 #endif
9442 			if (ret == SCTP_RETRAN_EXIT) {
9443 				return;
9444 			}
9445 			break;
9446 		}
9447 		if (from_where == SCTP_OUTPUT_FROM_T3) {
9448 			/* Only one transmission allowed out of a timeout */
9449 #ifdef SCTP_AUDITING_ENABLED
9450 			sctp_auditing(10, inp, stcb, NULL);
9451 #endif
9452 			/* Push out any control */
9453 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
9454 			    &now, &now_filled, frag_point, so_locked);
9455 			return;
9456 		}
9457 		if (tot_frs > asoc->max_burst) {
9458 			/* Hit FR burst limit */
9459 			return;
9460 		}
9461 		if ((num_out == 0) && (ret == 0)) {
9462 
9463 			/* No more retrans to send */
9464 			break;
9465 		}
9466 	}
9467 #ifdef SCTP_AUDITING_ENABLED
9468 	sctp_auditing(12, inp, stcb, NULL);
9469 #endif
9470 	/* Check for bad destinations, if they exist move chunks around. */
9471 	burst_limit = asoc->max_burst;
9472 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9473 		if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
9474 		    SCTP_ADDR_NOT_REACHABLE) {
9475 			/*-
9476 			 * if possible move things off of this address we
9477 			 * still may send below due to the dormant state but
9478 			 * we try to find an alternate address to send to
9479 			 * and if we have one we move all queued data on the
9480 			 * out wheel to this alternate address.
9481 			 */
9482 			if (net->ref_count > 1)
9483 				sctp_move_to_an_alt(stcb, asoc, net);
9484 		} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
9485 			    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
9486 		    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
9487 			/*
9488 			 * JRS 5/14/07 - If CMT PF is on and the current
9489 			 * destination is in PF state, move all queued data
9490 			 * to an alternate desination.
9491 			 */
9492 			if (net->ref_count > 1)
9493 				sctp_move_to_an_alt(stcb, asoc, net);
9494 		} else {
9495 			/*-
9496 			 * if ((asoc->sat_network) || (net->addr_is_local))
9497 			 * { burst_limit = asoc->max_burst *
9498 			 * SCTP_SAT_NETWORK_BURST_INCR; }
9499 			 */
9500 			if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
9501 				if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
9502 					/*
9503 					 * JRS - Use the congestion control
9504 					 * given in the congestion control
9505 					 * module
9506 					 */
9507 					asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
9508 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9509 						sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
9510 					}
9511 					SCTP_STAT_INCR(sctps_maxburstqueued);
9512 				}
9513 				net->fast_retran_ip = 0;
9514 			} else {
9515 				if (net->flight_size == 0) {
9516 					/* Should be decaying the cwnd here */
9517 					;
9518 				}
9519 			}
9520 		}
9521 
9522 	}
9523 	burst_cnt = 0;
9524 	cwnd_full = 0;
9525 	do {
9526 		error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
9527 		    &reason_code, 0, &cwnd_full, from_where,
9528 		    &now, &now_filled, frag_point, so_locked);
9529 		if (error) {
9530 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
9531 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9532 				sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
9533 			}
9534 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9535 				sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
9536 				sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
9537 			}
9538 			break;
9539 		}
9540 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
9541 
9542 		tot_out += num_out;
9543 		burst_cnt++;
9544 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9545 			sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
9546 			if (num_out == 0) {
9547 				sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
9548 			}
9549 		}
9550 		if (nagle_on) {
9551 			/*-
9552 			 * When nagle is on, we look at how much is un_sent, then
9553 			 * if its smaller than an MTU and we have data in
9554 			 * flight we stop.
9555 			 */
9556 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9557 			    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
9558 			if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
9559 			    (stcb->asoc.total_flight > 0)) {
9560 				break;
9561 			}
9562 		}
9563 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9564 		    TAILQ_EMPTY(&asoc->send_queue) &&
9565 		    TAILQ_EMPTY(&asoc->out_wheel)) {
9566 			/* Nothing left to send */
9567 			break;
9568 		}
9569 		if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
9570 			/* Nothing left to send */
9571 			break;
9572 		}
9573 	} while (num_out && (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
9574 	    (burst_cnt < burst_limit)));
9575 
9576 	if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
9577 		if (burst_cnt >= burst_limit) {
9578 			SCTP_STAT_INCR(sctps_maxburstqueued);
9579 			asoc->burst_limit_applied = 1;
9580 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9581 				sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
9582 			}
9583 		} else {
9584 			asoc->burst_limit_applied = 0;
9585 		}
9586 	}
9587 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9588 		sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
9589 	}
9590 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
9591 	    tot_out);
9592 
9593 	/*-
9594 	 * Now we need to clean up the control chunk chain if a ECNE is on
9595 	 * it. It must be marked as UNSENT again so next call will continue
9596 	 * to send it until such time that we get a CWR, to remove it.
9597 	 */
9598 	if (stcb->asoc.ecn_echo_cnt_onq)
9599 		sctp_fix_ecn_echo(asoc);
9600 	return;
9601 }
9602 
9603 
9604 int
9605 sctp_output(inp, m, addr, control, p, flags)
9606 	struct sctp_inpcb *inp;
9607 	struct mbuf *m;
9608 	struct sockaddr *addr;
9609 	struct mbuf *control;
9610 	struct thread *p;
9611 	int flags;
9612 {
9613 	if (inp == NULL) {
9614 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9615 		return (EINVAL);
9616 	}
9617 	if (inp->sctp_socket == NULL) {
9618 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9619 		return (EINVAL);
9620 	}
9621 	return (sctp_sosend(inp->sctp_socket,
9622 	    addr,
9623 	    (struct uio *)NULL,
9624 	    m,
9625 	    control,
9626 	    flags, p
9627 	    ));
9628 }
9629 
9630 void
9631 send_forward_tsn(struct sctp_tcb *stcb,
9632     struct sctp_association *asoc)
9633 {
9634 	struct sctp_tmit_chunk *chk;
9635 	struct sctp_forward_tsn_chunk *fwdtsn;
9636 
9637 	SCTP_TCB_LOCK_ASSERT(stcb);
9638 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9639 		if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9640 			/* mark it to unsent */
9641 			chk->sent = SCTP_DATAGRAM_UNSENT;
9642 			chk->snd_count = 0;
9643 			/* Do we correct its output location? */
9644 			if (chk->whoTo != asoc->primary_destination) {
9645 				sctp_free_remote_addr(chk->whoTo);
9646 				chk->whoTo = asoc->primary_destination;
9647 				atomic_add_int(&chk->whoTo->ref_count, 1);
9648 			}
9649 			goto sctp_fill_in_rest;
9650 		}
9651 	}
9652 	/* Ok if we reach here we must build one */
9653 	sctp_alloc_a_chunk(stcb, chk);
9654 	if (chk == NULL) {
9655 		return;
9656 	}
9657 	chk->copy_by_ref = 0;
9658 	chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
9659 	chk->rec.chunk_id.can_take_data = 0;
9660 	chk->asoc = asoc;
9661 	chk->whoTo = NULL;
9662 
9663 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9664 	if (chk->data == NULL) {
9665 		sctp_free_a_chunk(stcb, chk);
9666 		return;
9667 	}
9668 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9669 	chk->sent = SCTP_DATAGRAM_UNSENT;
9670 	chk->snd_count = 0;
9671 	chk->whoTo = asoc->primary_destination;
9672 	atomic_add_int(&chk->whoTo->ref_count, 1);
9673 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
9674 	asoc->ctrl_queue_cnt++;
9675 sctp_fill_in_rest:
9676 	/*-
9677 	 * Here we go through and fill out the part that deals with
9678 	 * stream/seq of the ones we skip.
9679 	 */
9680 	SCTP_BUF_LEN(chk->data) = 0;
9681 	{
9682 		struct sctp_tmit_chunk *at, *tp1, *last;
9683 		struct sctp_strseq *strseq;
9684 		unsigned int cnt_of_space, i, ovh;
9685 		unsigned int space_needed;
9686 		unsigned int cnt_of_skipped = 0;
9687 
9688 		TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
9689 			if (at->sent != SCTP_FORWARD_TSN_SKIP) {
9690 				/* no more to look at */
9691 				break;
9692 			}
9693 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9694 				/* We don't report these */
9695 				continue;
9696 			}
9697 			cnt_of_skipped++;
9698 		}
9699 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
9700 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
9701 
9702 		cnt_of_space = M_TRAILINGSPACE(chk->data);
9703 
9704 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9705 			ovh = SCTP_MIN_OVERHEAD;
9706 		} else {
9707 			ovh = SCTP_MIN_V4_OVERHEAD;
9708 		}
9709 		if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
9710 			/* trim to a mtu size */
9711 			cnt_of_space = asoc->smallest_mtu - ovh;
9712 		}
9713 		if (cnt_of_space < space_needed) {
9714 			/*-
9715 			 * ok we must trim down the chunk by lowering the
9716 			 * advance peer ack point.
9717 			 */
9718 			cnt_of_skipped = (cnt_of_space -
9719 			    ((sizeof(struct sctp_forward_tsn_chunk)) /
9720 			    sizeof(struct sctp_strseq)));
9721 			/*-
9722 			 * Go through and find the TSN that will be the one
9723 			 * we report.
9724 			 */
9725 			at = TAILQ_FIRST(&asoc->sent_queue);
9726 			for (i = 0; i < cnt_of_skipped; i++) {
9727 				tp1 = TAILQ_NEXT(at, sctp_next);
9728 				at = tp1;
9729 			}
9730 			last = at;
9731 			/*-
9732 			 * last now points to last one I can report, update
9733 			 * peer ack point
9734 			 */
9735 			asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
9736 			space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
9737 		}
9738 		chk->send_size = space_needed;
9739 		/* Setup the chunk */
9740 		fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
9741 		fwdtsn->ch.chunk_length = htons(chk->send_size);
9742 		fwdtsn->ch.chunk_flags = 0;
9743 		fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
9744 		fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
9745 		chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
9746 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
9747 		SCTP_BUF_LEN(chk->data) = chk->send_size;
9748 		fwdtsn++;
9749 		/*-
9750 		 * Move pointer to after the fwdtsn and transfer to the
9751 		 * strseq pointer.
9752 		 */
9753 		strseq = (struct sctp_strseq *)fwdtsn;
9754 		/*-
9755 		 * Now populate the strseq list. This is done blindly
9756 		 * without pulling out duplicate stream info. This is
9757 		 * inefficent but won't harm the process since the peer will
9758 		 * look at these in sequence and will thus release anything.
9759 		 * It could mean we exceed the PMTU and chop off some that
9760 		 * we could have included.. but this is unlikely (aka 1432/4
9761 		 * would mean 300+ stream seq's would have to be reported in
9762 		 * one FWD-TSN. With a bit of work we can later FIX this to
9763 		 * optimize and pull out duplcates.. but it does add more
9764 		 * overhead. So for now... not!
9765 		 */
9766 		at = TAILQ_FIRST(&asoc->sent_queue);
9767 		for (i = 0; i < cnt_of_skipped; i++) {
9768 			tp1 = TAILQ_NEXT(at, sctp_next);
9769 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9770 				/* We don't report these */
9771 				i--;
9772 				at = tp1;
9773 				continue;
9774 			}
9775 			strseq->stream = ntohs(at->rec.data.stream_number);
9776 			strseq->sequence = ntohs(at->rec.data.stream_seq);
9777 			strseq++;
9778 			at = tp1;
9779 		}
9780 	}
9781 	return;
9782 
9783 }
9784 
9785 void
9786 sctp_send_sack(struct sctp_tcb *stcb)
9787 {
9788 	/*-
9789 	 * Queue up a SACK in the control queue. We must first check to see
9790 	 * if a SACK is somehow on the control queue. If so, we will take
9791 	 * and and remove the old one.
9792 	 */
9793 	struct sctp_association *asoc;
9794 	struct sctp_tmit_chunk *chk, *a_chk;
9795 	struct sctp_sack_chunk *sack;
9796 	struct sctp_gap_ack_block *gap_descriptor;
9797 	struct sack_track *selector;
9798 	int mergeable = 0;
9799 	int offset;
9800 	caddr_t limit;
9801 	uint32_t *dup;
9802 	int limit_reached = 0;
9803 	unsigned int i, jstart, siz, j;
9804 	unsigned int num_gap_blocks = 0, space;
9805 	int num_dups = 0;
9806 	int space_req;
9807 
9808 	a_chk = NULL;
9809 	asoc = &stcb->asoc;
9810 	SCTP_TCB_LOCK_ASSERT(stcb);
9811 	if (asoc->last_data_chunk_from == NULL) {
9812 		/* Hmm we never received anything */
9813 		return;
9814 	}
9815 	sctp_set_rwnd(stcb, asoc);
9816 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9817 		if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
9818 			/* Hmm, found a sack already on queue, remove it */
9819 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
9820 			asoc->ctrl_queue_cnt++;
9821 			a_chk = chk;
9822 			if (a_chk->data) {
9823 				sctp_m_freem(a_chk->data);
9824 				a_chk->data = NULL;
9825 			}
9826 			sctp_free_remote_addr(a_chk->whoTo);
9827 			a_chk->whoTo = NULL;
9828 			break;
9829 		}
9830 	}
9831 	if (a_chk == NULL) {
9832 		sctp_alloc_a_chunk(stcb, a_chk);
9833 		if (a_chk == NULL) {
9834 			/* No memory so we drop the idea, and set a timer */
9835 			if (stcb->asoc.delayed_ack) {
9836 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9837 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
9838 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9839 				    stcb->sctp_ep, stcb, NULL);
9840 			} else {
9841 				stcb->asoc.send_sack = 1;
9842 			}
9843 			return;
9844 		}
9845 		a_chk->copy_by_ref = 0;
9846 		/* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
9847 		a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK;
9848 		a_chk->rec.chunk_id.can_take_data = 1;
9849 	}
9850 	/* Clear our pkt counts */
9851 	asoc->data_pkts_seen = 0;
9852 
9853 	a_chk->asoc = asoc;
9854 	a_chk->snd_count = 0;
9855 	a_chk->send_size = 0;	/* fill in later */
9856 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
9857 	a_chk->whoTo = NULL;
9858 
9859 	if ((asoc->numduptsns) ||
9860 	    (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
9861 	    ) {
9862 		/*-
9863 		 * Ok, we have some duplicates or the destination for the
9864 		 * sack is unreachable, lets see if we can select an
9865 		 * alternate than asoc->last_data_chunk_from
9866 		 */
9867 		if ((!(asoc->last_data_chunk_from->dest_state &
9868 		    SCTP_ADDR_NOT_REACHABLE)) &&
9869 		    (asoc->used_alt_onsack > asoc->numnets)) {
9870 			/* We used an alt last time, don't this time */
9871 			a_chk->whoTo = NULL;
9872 		} else {
9873 			asoc->used_alt_onsack++;
9874 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
9875 		}
9876 		if (a_chk->whoTo == NULL) {
9877 			/* Nope, no alternate */
9878 			a_chk->whoTo = asoc->last_data_chunk_from;
9879 			asoc->used_alt_onsack = 0;
9880 		}
9881 	} else {
9882 		/*
9883 		 * No duplicates so we use the last place we received data
9884 		 * from.
9885 		 */
9886 		asoc->used_alt_onsack = 0;
9887 		a_chk->whoTo = asoc->last_data_chunk_from;
9888 	}
9889 	if (a_chk->whoTo) {
9890 		atomic_add_int(&a_chk->whoTo->ref_count, 1);
9891 	}
9892 	if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
9893 		/* no gaps */
9894 		space_req = sizeof(struct sctp_sack_chunk);
9895 	} else {
9896 		/* gaps get a cluster */
9897 		space_req = MCLBYTES;
9898 	}
9899 	/* Ok now lets formulate a MBUF with our sack */
9900 	a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
9901 	if ((a_chk->data == NULL) ||
9902 	    (a_chk->whoTo == NULL)) {
9903 		/* rats, no mbuf memory */
9904 		if (a_chk->data) {
9905 			/* was a problem with the destination */
9906 			sctp_m_freem(a_chk->data);
9907 			a_chk->data = NULL;
9908 		}
9909 		sctp_free_a_chunk(stcb, a_chk);
9910 		/* sa_ignore NO_NULL_CHK */
9911 		if (stcb->asoc.delayed_ack) {
9912 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9913 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
9914 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9915 			    stcb->sctp_ep, stcb, NULL);
9916 		} else {
9917 			stcb->asoc.send_sack = 1;
9918 		}
9919 		return;
9920 	}
9921 	/* ok, lets go through and fill it in */
9922 	SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
9923 	space = M_TRAILINGSPACE(a_chk->data);
9924 	if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
9925 		space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
9926 	}
9927 	limit = mtod(a_chk->data, caddr_t);
9928 	limit += space;
9929 
9930 	sack = mtod(a_chk->data, struct sctp_sack_chunk *);
9931 	sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
9932 	/* 0x01 is used by nonce for ecn */
9933 	if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
9934 	    (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
9935 	    (asoc->peer_supports_ecn_nonce))
9936 		sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
9937 	else
9938 		sack->ch.chunk_flags = 0;
9939 
9940 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
9941 		/*-
9942 		 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
9943 		 * received, then set high bit to 1, else 0. Reset
9944 		 * pkts_rcvd.
9945 		 */
9946 		sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
9947 		asoc->cmt_dac_pkts_rcvd = 0;
9948 	}
9949 #ifdef SCTP_ASOCLOG_OF_TSNS
9950 	stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
9951 	stcb->asoc.cumack_log_atsnt++;
9952 	if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
9953 		stcb->asoc.cumack_log_atsnt = 0;
9954 	}
9955 #endif
9956 	sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
9957 	sack->sack.a_rwnd = htonl(asoc->my_rwnd);
9958 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
9959 
9960 	/* reset the readers interpretation */
9961 	stcb->freed_by_sorcv_sincelast = 0;
9962 
9963 	gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
9964 
9965 	siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
9966 	if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
9967 		offset = 1;
9968 		/*-
9969 		 * cum-ack behind the mapping array, so we start and use all
9970 		 * entries.
9971 		 */
9972 		jstart = 0;
9973 	} else {
9974 		offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
9975 		/*-
9976 		 * we skip the first one when the cum-ack is at or above the
9977 		 * mapping array base. Note this only works if
9978 		 */
9979 		jstart = 1;
9980 	}
9981 	if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
9982 		/* we have a gap .. maybe */
9983 		for (i = 0; i < siz; i++) {
9984 			selector = &sack_array[asoc->mapping_array[i]];
9985 			if (mergeable && selector->right_edge) {
9986 				/*
9987 				 * Backup, left and right edges were ok to
9988 				 * merge.
9989 				 */
9990 				num_gap_blocks--;
9991 				gap_descriptor--;
9992 			}
9993 			if (selector->num_entries == 0)
9994 				mergeable = 0;
9995 			else {
9996 				for (j = jstart; j < selector->num_entries; j++) {
9997 					if (mergeable && selector->right_edge) {
9998 						/*
9999 						 * do a merge by NOT setting
10000 						 * the left side
10001 						 */
10002 						mergeable = 0;
10003 					} else {
10004 						/*
10005 						 * no merge, set the left
10006 						 * side
10007 						 */
10008 						mergeable = 0;
10009 						gap_descriptor->start = htons((selector->gaps[j].start + offset));
10010 					}
10011 					gap_descriptor->end = htons((selector->gaps[j].end + offset));
10012 					num_gap_blocks++;
10013 					gap_descriptor++;
10014 					if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10015 						/* no more room */
10016 						limit_reached = 1;
10017 						break;
10018 					}
10019 				}
10020 				if (selector->left_edge) {
10021 					mergeable = 1;
10022 				}
10023 			}
10024 			if (limit_reached) {
10025 				/* Reached the limit stop */
10026 				break;
10027 			}
10028 			jstart = 0;
10029 			offset += 8;
10030 		}
10031 		if (num_gap_blocks == 0) {
10032 			/*
10033 			 * slide not yet happened, and somehow we got called
10034 			 * to send a sack. Cumack needs to move up.
10035 			 */
10036 			int abort_flag = 0;
10037 
10038 			asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
10039 			sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10040 			sctp_sack_check(stcb, 0, 0, &abort_flag);
10041 		}
10042 	}
10043 	/* now we must add any dups we are going to report. */
10044 	if ((limit_reached == 0) && (asoc->numduptsns)) {
10045 		dup = (uint32_t *) gap_descriptor;
10046 		for (i = 0; i < asoc->numduptsns; i++) {
10047 			*dup = htonl(asoc->dup_tsns[i]);
10048 			dup++;
10049 			num_dups++;
10050 			if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10051 				/* no more room */
10052 				break;
10053 			}
10054 		}
10055 		asoc->numduptsns = 0;
10056 	}
10057 	/*
10058 	 * now that the chunk is prepared queue it to the control chunk
10059 	 * queue.
10060 	 */
10061 	a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
10062 	    (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
10063 	    (num_dups * sizeof(int32_t)));
10064 	SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10065 	sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10066 	sack->sack.num_dup_tsns = htons(num_dups);
10067 	sack->ch.chunk_length = htons(a_chk->send_size);
10068 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10069 	asoc->ctrl_queue_cnt++;
10070 	asoc->send_sack = 0;
10071 	SCTP_STAT_INCR(sctps_sendsacks);
10072 	return;
10073 }
10074 
10075 
10076 void
10077 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10078 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10079     SCTP_UNUSED
10080 #endif
10081 )
10082 {
10083 	struct mbuf *m_abort;
10084 	struct mbuf *m_out = NULL, *m_end = NULL;
10085 	struct sctp_abort_chunk *abort = NULL;
10086 	int sz;
10087 	uint32_t auth_offset = 0;
10088 	struct sctp_auth_chunk *auth = NULL;
10089 	struct sctphdr *shdr;
10090 
10091 	/*-
10092 	 * Add an AUTH chunk, if chunk requires it and save the offset into
10093 	 * the chain for AUTH
10094 	 */
10095 	if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10096 	    stcb->asoc.peer_auth_chunks)) {
10097 		m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10098 		    stcb, SCTP_ABORT_ASSOCIATION);
10099 	}
10100 	SCTP_TCB_LOCK_ASSERT(stcb);
10101 	m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10102 	if (m_abort == NULL) {
10103 		/* no mbuf's */
10104 		if (m_out)
10105 			sctp_m_freem(m_out);
10106 		return;
10107 	}
10108 	/* link in any error */
10109 	SCTP_BUF_NEXT(m_abort) = operr;
10110 	sz = 0;
10111 	if (operr) {
10112 		struct mbuf *n;
10113 
10114 		n = operr;
10115 		while (n) {
10116 			sz += SCTP_BUF_LEN(n);
10117 			n = SCTP_BUF_NEXT(n);
10118 		}
10119 	}
10120 	SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10121 	if (m_out == NULL) {
10122 		/* NO Auth chunk prepended, so reserve space in front */
10123 		SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10124 		m_out = m_abort;
10125 	} else {
10126 		/* Put AUTH chunk at the front of the chain */
10127 		SCTP_BUF_NEXT(m_end) = m_abort;
10128 	}
10129 
10130 	/* fill in the ABORT chunk */
10131 	abort = mtod(m_abort, struct sctp_abort_chunk *);
10132 	abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10133 	abort->ch.chunk_flags = 0;
10134 	abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10135 
10136 	/* prepend and fill in the SCTP header */
10137 	SCTP_BUF_PREPEND(m_out, sizeof(struct sctphdr), M_DONTWAIT);
10138 	if (m_out == NULL) {
10139 		/* TSNH: no memory */
10140 		return;
10141 	}
10142 	shdr = mtod(m_out, struct sctphdr *);
10143 	shdr->src_port = stcb->sctp_ep->sctp_lport;
10144 	shdr->dest_port = stcb->rport;
10145 	shdr->v_tag = htonl(stcb->asoc.peer_vtag);
10146 	shdr->checksum = 0;
10147 	auth_offset += sizeof(struct sctphdr);
10148 
10149 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
10150 	    stcb->asoc.primary_destination,
10151 	    (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
10152 	    m_out, auth_offset, auth, 1, 0, NULL, 0, stcb->asoc.primary_destination->port, so_locked, NULL);
10153 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10154 }
10155 
10156 void
10157 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10158     struct sctp_nets *net)
10159 {
10160 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10161 	struct mbuf *m_shutdown_comp;
10162 	struct sctp_shutdown_complete_msg *comp_cp;
10163 
10164 	m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_complete_msg), 0, M_DONTWAIT, 1, MT_HEADER);
10165 	if (m_shutdown_comp == NULL) {
10166 		/* no mbuf's */
10167 		return;
10168 	}
10169 	comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
10170 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10171 	comp_cp->shut_cmp.ch.chunk_flags = 0;
10172 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10173 	comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
10174 	comp_cp->sh.dest_port = stcb->rport;
10175 	comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
10176 	comp_cp->sh.checksum = 0;
10177 
10178 	SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_msg);
10179 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10180 	    (struct sockaddr *)&net->ro._l_addr,
10181 	    m_shutdown_comp, 0, NULL, 1, 0, NULL, 0, net->port, SCTP_SO_NOT_LOCKED, NULL);
10182 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10183 	return;
10184 }
10185 
10186 void
10187 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
10188     uint32_t vrf_id, uint16_t port)
10189 {
10190 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10191 	struct mbuf *o_pak;
10192 	struct mbuf *mout;
10193 	struct ip *iph, *iph_out;
10194 	struct udphdr *udp = NULL;
10195 
10196 #ifdef INET6
10197 	struct ip6_hdr *ip6, *ip6_out;
10198 
10199 #endif
10200 	int offset_out, len, mlen;
10201 	struct sctp_shutdown_complete_msg *comp_cp;
10202 
10203 	iph = mtod(m, struct ip *);
10204 	switch (iph->ip_v) {
10205 	case IPVERSION:
10206 		len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10207 		break;
10208 #ifdef INET6
10209 	case IPV6_VERSION >> 4:
10210 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10211 		break;
10212 #endif
10213 	default:
10214 		return;
10215 	}
10216 	if (port) {
10217 		len += sizeof(struct udphdr);
10218 	}
10219 	mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
10220 	if (mout == NULL) {
10221 		return;
10222 	}
10223 	SCTP_BUF_LEN(mout) = len;
10224 	SCTP_BUF_NEXT(mout) = NULL;
10225 	iph_out = NULL;
10226 #ifdef INET6
10227 	ip6_out = NULL;
10228 #endif
10229 	offset_out = 0;
10230 
10231 	switch (iph->ip_v) {
10232 	case IPVERSION:
10233 		iph_out = mtod(mout, struct ip *);
10234 
10235 		/* Fill in the IP header for the ABORT */
10236 		iph_out->ip_v = IPVERSION;
10237 		iph_out->ip_hl = (sizeof(struct ip) / 4);
10238 		iph_out->ip_tos = (u_char)0;
10239 		iph_out->ip_id = 0;
10240 		iph_out->ip_off = 0;
10241 		iph_out->ip_ttl = MAXTTL;
10242 		if (port) {
10243 			iph_out->ip_p = IPPROTO_UDP;
10244 		} else {
10245 			iph_out->ip_p = IPPROTO_SCTP;
10246 		}
10247 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10248 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10249 
10250 		/* let IP layer calculate this */
10251 		iph_out->ip_sum = 0;
10252 		offset_out += sizeof(*iph_out);
10253 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10254 		    (caddr_t)iph_out + offset_out);
10255 		break;
10256 #ifdef INET6
10257 	case IPV6_VERSION >> 4:
10258 		ip6 = (struct ip6_hdr *)iph;
10259 		ip6_out = mtod(mout, struct ip6_hdr *);
10260 
10261 		/* Fill in the IPv6 header for the ABORT */
10262 		ip6_out->ip6_flow = ip6->ip6_flow;
10263 		ip6_out->ip6_hlim = ip6_defhlim;
10264 		if (port) {
10265 			ip6_out->ip6_nxt = IPPROTO_UDP;
10266 		} else {
10267 			ip6_out->ip6_nxt = IPPROTO_SCTP;
10268 		}
10269 		ip6_out->ip6_src = ip6->ip6_dst;
10270 		ip6_out->ip6_dst = ip6->ip6_src;
10271 		/*
10272 		 * ?? The old code had both the iph len + payload, I think
10273 		 * this is wrong and would never have worked
10274 		 */
10275 		ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10276 		offset_out += sizeof(*ip6_out);
10277 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10278 		    (caddr_t)ip6_out + offset_out);
10279 		break;
10280 #endif
10281 	default:
10282 		/* Currently not supported. */
10283 		return;
10284 	}
10285 	if (port) {
10286 		udp = (struct udphdr *)comp_cp;
10287 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10288 		udp->uh_dport = port;
10289 		udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
10290 		udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
10291 		offset_out += sizeof(struct udphdr);
10292 		comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
10293 	}
10294 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10295 		/* no mbuf's */
10296 		sctp_m_freem(mout);
10297 		return;
10298 	}
10299 	/* Now copy in and fill in the ABORT tags etc. */
10300 	comp_cp->sh.src_port = sh->dest_port;
10301 	comp_cp->sh.dest_port = sh->src_port;
10302 	comp_cp->sh.checksum = 0;
10303 	comp_cp->sh.v_tag = sh->v_tag;
10304 	comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
10305 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10306 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10307 
10308 	/* add checksum */
10309 	comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
10310 	if (iph_out != NULL) {
10311 		sctp_route_t ro;
10312 		int ret;
10313 		struct sctp_tcb *stcb = NULL;
10314 
10315 		mlen = SCTP_BUF_LEN(mout);
10316 		bzero(&ro, sizeof ro);
10317 		/* set IPv4 length */
10318 		iph_out->ip_len = mlen;
10319 #ifdef  SCTP_PACKET_LOGGING
10320 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10321 			sctp_packet_log(mout, mlen);
10322 #endif
10323 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10324 		if (port) {
10325 			SCTP_ENABLE_UDP_CSUM(o_pak);
10326 		}
10327 		/* out it goes */
10328 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
10329 
10330 		/* Free the route if we got one back */
10331 		if (ro.ro_rt)
10332 			RTFREE(ro.ro_rt);
10333 	}
10334 #ifdef INET6
10335 	if (ip6_out != NULL) {
10336 		struct route_in6 ro;
10337 		int ret;
10338 		struct sctp_tcb *stcb = NULL;
10339 		struct ifnet *ifp = NULL;
10340 
10341 		bzero(&ro, sizeof(ro));
10342 		mlen = SCTP_BUF_LEN(mout);
10343 #ifdef  SCTP_PACKET_LOGGING
10344 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10345 			sctp_packet_log(mout, mlen);
10346 #endif
10347 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10348 		if (port) {
10349 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr),
10350 			    sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr))) == 0) {
10351 				udp->uh_sum = 0xffff;
10352 			}
10353 		}
10354 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
10355 
10356 		/* Free the route if we got one back */
10357 		if (ro.ro_rt)
10358 			RTFREE(ro.ro_rt);
10359 	}
10360 #endif
10361 	SCTP_STAT_INCR(sctps_sendpackets);
10362 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10363 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10364 	return;
10365 
10366 }
10367 
10368 static struct sctp_nets *
10369 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
10370 {
10371 	struct sctp_nets *net, *hnet;
10372 	int ms_goneby, highest_ms, state_overide = 0;
10373 
10374 	(void)SCTP_GETTIME_TIMEVAL(now);
10375 	highest_ms = 0;
10376 	hnet = NULL;
10377 	SCTP_TCB_LOCK_ASSERT(stcb);
10378 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
10379 		if (
10380 		    ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
10381 		    (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
10382 		    ) {
10383 			/*
10384 			 * Skip this guy from consideration if HB is off AND
10385 			 * its confirmed
10386 			 */
10387 			continue;
10388 		}
10389 		if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
10390 			/* skip this dest net from consideration */
10391 			continue;
10392 		}
10393 		if (net->last_sent_time.tv_sec) {
10394 			/* Sent to so we subtract */
10395 			ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
10396 		} else
10397 			/* Never been sent to */
10398 			ms_goneby = 0x7fffffff;
10399 		/*-
10400 		 * When the address state is unconfirmed but still
10401 		 * considered reachable, we HB at a higher rate. Once it
10402 		 * goes confirmed OR reaches the "unreachable" state, thenw
10403 		 * we cut it back to HB at a more normal pace.
10404 		 */
10405 		if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
10406 			state_overide = 1;
10407 		} else {
10408 			state_overide = 0;
10409 		}
10410 
10411 		if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
10412 		    (ms_goneby > highest_ms)) {
10413 			highest_ms = ms_goneby;
10414 			hnet = net;
10415 		}
10416 	}
10417 	if (hnet &&
10418 	    ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
10419 		state_overide = 1;
10420 	} else {
10421 		state_overide = 0;
10422 	}
10423 
10424 	if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
10425 		/*-
10426 		 * Found the one with longest delay bounds OR it is
10427 		 * unconfirmed and still not marked unreachable.
10428 		 */
10429 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
10430 #ifdef SCTP_DEBUG
10431 		if (hnet) {
10432 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
10433 			    (struct sockaddr *)&hnet->ro._l_addr);
10434 		} else {
10435 			SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
10436 		}
10437 #endif
10438 		/* update the timer now */
10439 		hnet->last_sent_time = *now;
10440 		return (hnet);
10441 	}
10442 	/* Nothing to HB */
10443 	return (NULL);
10444 }
10445 
10446 int
10447 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
10448 {
10449 	struct sctp_tmit_chunk *chk;
10450 	struct sctp_nets *net;
10451 	struct sctp_heartbeat_chunk *hb;
10452 	struct timeval now;
10453 	struct sockaddr_in *sin;
10454 	struct sockaddr_in6 *sin6;
10455 
10456 	SCTP_TCB_LOCK_ASSERT(stcb);
10457 	if (user_req == 0) {
10458 		net = sctp_select_hb_destination(stcb, &now);
10459 		if (net == NULL) {
10460 			/*-
10461 			 * All our busy none to send to, just start the
10462 			 * timer again.
10463 			 */
10464 			if (stcb->asoc.state == 0) {
10465 				return (0);
10466 			}
10467 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
10468 			    stcb->sctp_ep,
10469 			    stcb,
10470 			    net);
10471 			return (0);
10472 		}
10473 	} else {
10474 		net = u_net;
10475 		if (net == NULL) {
10476 			return (0);
10477 		}
10478 		(void)SCTP_GETTIME_TIMEVAL(&now);
10479 	}
10480 	sin = (struct sockaddr_in *)&net->ro._l_addr;
10481 	if (sin->sin_family != AF_INET) {
10482 		if (sin->sin_family != AF_INET6) {
10483 			/* huh */
10484 			return (0);
10485 		}
10486 	}
10487 	sctp_alloc_a_chunk(stcb, chk);
10488 	if (chk == NULL) {
10489 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
10490 		return (0);
10491 	}
10492 	chk->copy_by_ref = 0;
10493 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
10494 	chk->rec.chunk_id.can_take_data = 1;
10495 	chk->asoc = &stcb->asoc;
10496 	chk->send_size = sizeof(struct sctp_heartbeat_chunk);
10497 
10498 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10499 	if (chk->data == NULL) {
10500 		sctp_free_a_chunk(stcb, chk);
10501 		return (0);
10502 	}
10503 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10504 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10505 	chk->sent = SCTP_DATAGRAM_UNSENT;
10506 	chk->snd_count = 0;
10507 	chk->whoTo = net;
10508 	atomic_add_int(&chk->whoTo->ref_count, 1);
10509 	/* Now we have a mbuf that we can fill in with the details */
10510 	hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
10511 	memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
10512 	/* fill out chunk header */
10513 	hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
10514 	hb->ch.chunk_flags = 0;
10515 	hb->ch.chunk_length = htons(chk->send_size);
10516 	/* Fill out hb parameter */
10517 	hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
10518 	hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
10519 	hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
10520 	hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
10521 	/* Did our user request this one, put it in */
10522 	hb->heartbeat.hb_info.user_req = user_req;
10523 	hb->heartbeat.hb_info.addr_family = sin->sin_family;
10524 	hb->heartbeat.hb_info.addr_len = sin->sin_len;
10525 	if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
10526 		/*
10527 		 * we only take from the entropy pool if the address is not
10528 		 * confirmed.
10529 		 */
10530 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10531 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10532 	} else {
10533 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
10534 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
10535 	}
10536 	if (sin->sin_family == AF_INET) {
10537 		memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
10538 	} else if (sin->sin_family == AF_INET6) {
10539 		/* We leave the scope the way it is in our lookup table. */
10540 		sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
10541 		memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
10542 	} else {
10543 		/* huh compiler bug */
10544 		return (0);
10545 	}
10546 
10547 	/*
10548 	 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
10549 	 * PF-heartbeats.  Because of this, threshold management is done by
10550 	 * the t3 timer handler, and does not need to be done upon the send
10551 	 * of a PF-heartbeat. If CMT PF is on and the destination to which a
10552 	 * heartbeat is being sent is in PF state, do NOT do threshold
10553 	 * management.
10554 	 */
10555 	if ((SCTP_BASE_SYSCTL(sctp_cmt_pf) == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
10556 		/* ok we have a destination that needs a beat */
10557 		/* lets do the theshold management Qiaobing style */
10558 		if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
10559 		    stcb->asoc.max_send_times)) {
10560 			/*-
10561 			 * we have lost the association, in a way this is
10562 			 * quite bad since we really are one less time since
10563 			 * we really did not send yet. This is the down side
10564 			 * to the Q's style as defined in the RFC and not my
10565 			 * alternate style defined in the RFC.
10566 			 */
10567 			if (chk->data != NULL) {
10568 				sctp_m_freem(chk->data);
10569 				chk->data = NULL;
10570 			}
10571 			/*
10572 			 * Here we do NOT use the macro since the
10573 			 * association is now gone.
10574 			 */
10575 			if (chk->whoTo) {
10576 				sctp_free_remote_addr(chk->whoTo);
10577 				chk->whoTo = NULL;
10578 			}
10579 			sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
10580 			return (-1);
10581 		}
10582 	}
10583 	net->hb_responded = 0;
10584 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10585 	stcb->asoc.ctrl_queue_cnt++;
10586 	SCTP_STAT_INCR(sctps_sendheartbeat);
10587 	/*-
10588 	 * Call directly med level routine to put out the chunk. It will
10589 	 * always tumble out control chunks aka HB but it may even tumble
10590 	 * out data too.
10591 	 */
10592 	return (1);
10593 }
10594 
10595 void
10596 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
10597     uint32_t high_tsn)
10598 {
10599 	struct sctp_association *asoc;
10600 	struct sctp_ecne_chunk *ecne;
10601 	struct sctp_tmit_chunk *chk;
10602 
10603 	asoc = &stcb->asoc;
10604 	SCTP_TCB_LOCK_ASSERT(stcb);
10605 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10606 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
10607 			/* found a previous ECN_ECHO update it if needed */
10608 			ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10609 			ecne->tsn = htonl(high_tsn);
10610 			return;
10611 		}
10612 	}
10613 	/* nope could not find one to update so we must build one */
10614 	sctp_alloc_a_chunk(stcb, chk);
10615 	if (chk == NULL) {
10616 		return;
10617 	}
10618 	chk->copy_by_ref = 0;
10619 	SCTP_STAT_INCR(sctps_sendecne);
10620 	chk->rec.chunk_id.id = SCTP_ECN_ECHO;
10621 	chk->rec.chunk_id.can_take_data = 0;
10622 	chk->asoc = &stcb->asoc;
10623 	chk->send_size = sizeof(struct sctp_ecne_chunk);
10624 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10625 	if (chk->data == NULL) {
10626 		sctp_free_a_chunk(stcb, chk);
10627 		return;
10628 	}
10629 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10630 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10631 	chk->sent = SCTP_DATAGRAM_UNSENT;
10632 	chk->snd_count = 0;
10633 	chk->whoTo = net;
10634 	atomic_add_int(&chk->whoTo->ref_count, 1);
10635 	stcb->asoc.ecn_echo_cnt_onq++;
10636 	ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10637 	ecne->ch.chunk_type = SCTP_ECN_ECHO;
10638 	ecne->ch.chunk_flags = 0;
10639 	ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
10640 	ecne->tsn = htonl(high_tsn);
10641 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10642 	asoc->ctrl_queue_cnt++;
10643 }
10644 
10645 void
10646 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
10647     struct mbuf *m, int iphlen, int bad_crc)
10648 {
10649 	struct sctp_association *asoc;
10650 	struct sctp_pktdrop_chunk *drp;
10651 	struct sctp_tmit_chunk *chk;
10652 	uint8_t *datap;
10653 	int len;
10654 	int was_trunc = 0;
10655 	struct ip *iph;
10656 
10657 #ifdef INET6
10658 	struct ip6_hdr *ip6h;
10659 
10660 #endif
10661 	int fullsz = 0, extra = 0;
10662 	long spc;
10663 	int offset;
10664 	struct sctp_chunkhdr *ch, chunk_buf;
10665 	unsigned int chk_length;
10666 
10667 	if (!stcb) {
10668 		return;
10669 	}
10670 	asoc = &stcb->asoc;
10671 	SCTP_TCB_LOCK_ASSERT(stcb);
10672 	if (asoc->peer_supports_pktdrop == 0) {
10673 		/*-
10674 		 * peer must declare support before I send one.
10675 		 */
10676 		return;
10677 	}
10678 	if (stcb->sctp_socket == NULL) {
10679 		return;
10680 	}
10681 	sctp_alloc_a_chunk(stcb, chk);
10682 	if (chk == NULL) {
10683 		return;
10684 	}
10685 	chk->copy_by_ref = 0;
10686 	iph = mtod(m, struct ip *);
10687 	if (iph == NULL) {
10688 		sctp_free_a_chunk(stcb, chk);
10689 		return;
10690 	}
10691 	switch (iph->ip_v) {
10692 	case IPVERSION:
10693 		/* IPv4 */
10694 		len = chk->send_size = iph->ip_len;
10695 		break;
10696 #ifdef INET6
10697 	case IPV6_VERSION >> 4:
10698 		/* IPv6 */
10699 		ip6h = mtod(m, struct ip6_hdr *);
10700 		len = chk->send_size = htons(ip6h->ip6_plen);
10701 		break;
10702 #endif
10703 	default:
10704 		return;
10705 	}
10706 	/* Validate that we do not have an ABORT in here. */
10707 	offset = iphlen + sizeof(struct sctphdr);
10708 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10709 	    sizeof(*ch), (uint8_t *) & chunk_buf);
10710 	while (ch != NULL) {
10711 		chk_length = ntohs(ch->chunk_length);
10712 		if (chk_length < sizeof(*ch)) {
10713 			/* break to abort land */
10714 			break;
10715 		}
10716 		switch (ch->chunk_type) {
10717 		case SCTP_PACKET_DROPPED:
10718 		case SCTP_ABORT_ASSOCIATION:
10719 			/*-
10720 			 * we don't respond with an PKT-DROP to an ABORT
10721 			 * or PKT-DROP
10722 			 */
10723 			sctp_free_a_chunk(stcb, chk);
10724 			return;
10725 		default:
10726 			break;
10727 		}
10728 		offset += SCTP_SIZE32(chk_length);
10729 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10730 		    sizeof(*ch), (uint8_t *) & chunk_buf);
10731 	}
10732 
10733 	if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
10734 	    min(stcb->asoc.smallest_mtu, MCLBYTES)) {
10735 		/*
10736 		 * only send 1 mtu worth, trim off the excess on the end.
10737 		 */
10738 		fullsz = len - extra;
10739 		len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
10740 		was_trunc = 1;
10741 	}
10742 	chk->asoc = &stcb->asoc;
10743 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10744 	if (chk->data == NULL) {
10745 jump_out:
10746 		sctp_free_a_chunk(stcb, chk);
10747 		return;
10748 	}
10749 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10750 	drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
10751 	if (drp == NULL) {
10752 		sctp_m_freem(chk->data);
10753 		chk->data = NULL;
10754 		goto jump_out;
10755 	}
10756 	chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
10757 	    sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
10758 	chk->book_size_scale = 0;
10759 	if (was_trunc) {
10760 		drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
10761 		drp->trunc_len = htons(fullsz);
10762 		/*
10763 		 * Len is already adjusted to size minus overhead above take
10764 		 * out the pkt_drop chunk itself from it.
10765 		 */
10766 		chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
10767 		len = chk->send_size;
10768 	} else {
10769 		/* no truncation needed */
10770 		drp->ch.chunk_flags = 0;
10771 		drp->trunc_len = htons(0);
10772 	}
10773 	if (bad_crc) {
10774 		drp->ch.chunk_flags |= SCTP_BADCRC;
10775 	}
10776 	chk->send_size += sizeof(struct sctp_pktdrop_chunk);
10777 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10778 	chk->sent = SCTP_DATAGRAM_UNSENT;
10779 	chk->snd_count = 0;
10780 	if (net) {
10781 		/* we should hit here */
10782 		chk->whoTo = net;
10783 	} else {
10784 		chk->whoTo = asoc->primary_destination;
10785 	}
10786 	atomic_add_int(&chk->whoTo->ref_count, 1);
10787 	chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
10788 	chk->rec.chunk_id.can_take_data = 1;
10789 	drp->ch.chunk_type = SCTP_PACKET_DROPPED;
10790 	drp->ch.chunk_length = htons(chk->send_size);
10791 	spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
10792 	if (spc < 0) {
10793 		spc = 0;
10794 	}
10795 	drp->bottle_bw = htonl(spc);
10796 	if (asoc->my_rwnd) {
10797 		drp->current_onq = htonl(asoc->size_on_reasm_queue +
10798 		    asoc->size_on_all_streams +
10799 		    asoc->my_rwnd_control_len +
10800 		    stcb->sctp_socket->so_rcv.sb_cc);
10801 	} else {
10802 		/*-
10803 		 * If my rwnd is 0, possibly from mbuf depletion as well as
10804 		 * space used, tell the peer there is NO space aka onq == bw
10805 		 */
10806 		drp->current_onq = htonl(spc);
10807 	}
10808 	drp->reserved = 0;
10809 	datap = drp->data;
10810 	m_copydata(m, iphlen, len, (caddr_t)datap);
10811 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10812 	asoc->ctrl_queue_cnt++;
10813 }
10814 
10815 void
10816 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
10817 {
10818 	struct sctp_association *asoc;
10819 	struct sctp_cwr_chunk *cwr;
10820 	struct sctp_tmit_chunk *chk;
10821 
10822 	asoc = &stcb->asoc;
10823 	SCTP_TCB_LOCK_ASSERT(stcb);
10824 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10825 		if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
10826 			/* found a previous ECN_CWR update it if needed */
10827 			cwr = mtod(chk->data, struct sctp_cwr_chunk *);
10828 			if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
10829 			    MAX_TSN)) {
10830 				cwr->tsn = htonl(high_tsn);
10831 			}
10832 			return;
10833 		}
10834 	}
10835 	/* nope could not find one to update so we must build one */
10836 	sctp_alloc_a_chunk(stcb, chk);
10837 	if (chk == NULL) {
10838 		return;
10839 	}
10840 	chk->copy_by_ref = 0;
10841 	chk->rec.chunk_id.id = SCTP_ECN_CWR;
10842 	chk->rec.chunk_id.can_take_data = 1;
10843 	chk->asoc = &stcb->asoc;
10844 	chk->send_size = sizeof(struct sctp_cwr_chunk);
10845 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10846 	if (chk->data == NULL) {
10847 		sctp_free_a_chunk(stcb, chk);
10848 		return;
10849 	}
10850 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10851 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10852 	chk->sent = SCTP_DATAGRAM_UNSENT;
10853 	chk->snd_count = 0;
10854 	chk->whoTo = net;
10855 	atomic_add_int(&chk->whoTo->ref_count, 1);
10856 	cwr = mtod(chk->data, struct sctp_cwr_chunk *);
10857 	cwr->ch.chunk_type = SCTP_ECN_CWR;
10858 	cwr->ch.chunk_flags = 0;
10859 	cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
10860 	cwr->tsn = htonl(high_tsn);
10861 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10862 	asoc->ctrl_queue_cnt++;
10863 }
10864 
10865 void
10866 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
10867     int number_entries, uint16_t * list,
10868     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
10869 {
10870 	int len, old_len, i;
10871 	struct sctp_stream_reset_out_request *req_out;
10872 	struct sctp_chunkhdr *ch;
10873 
10874 	ch = mtod(chk->data, struct sctp_chunkhdr *);
10875 
10876 
10877 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10878 
10879 	/* get to new offset for the param. */
10880 	req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
10881 	/* now how long will this param be? */
10882 	len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
10883 	req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
10884 	req_out->ph.param_length = htons(len);
10885 	req_out->request_seq = htonl(seq);
10886 	req_out->response_seq = htonl(resp_seq);
10887 	req_out->send_reset_at_tsn = htonl(last_sent);
10888 	if (number_entries) {
10889 		for (i = 0; i < number_entries; i++) {
10890 			req_out->list_of_streams[i] = htons(list[i]);
10891 		}
10892 	}
10893 	if (SCTP_SIZE32(len) > len) {
10894 		/*-
10895 		 * Need to worry about the pad we may end up adding to the
10896 		 * end. This is easy since the struct is either aligned to 4
10897 		 * bytes or 2 bytes off.
10898 		 */
10899 		req_out->list_of_streams[number_entries] = 0;
10900 	}
10901 	/* now fix the chunk length */
10902 	ch->chunk_length = htons(len + old_len);
10903 	chk->book_size = len + old_len;
10904 	chk->book_size_scale = 0;
10905 	chk->send_size = SCTP_SIZE32(chk->book_size);
10906 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10907 	return;
10908 }
10909 
10910 
10911 void
10912 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
10913     int number_entries, uint16_t * list,
10914     uint32_t seq)
10915 {
10916 	int len, old_len, i;
10917 	struct sctp_stream_reset_in_request *req_in;
10918 	struct sctp_chunkhdr *ch;
10919 
10920 	ch = mtod(chk->data, struct sctp_chunkhdr *);
10921 
10922 
10923 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10924 
10925 	/* get to new offset for the param. */
10926 	req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
10927 	/* now how long will this param be? */
10928 	len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
10929 	req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
10930 	req_in->ph.param_length = htons(len);
10931 	req_in->request_seq = htonl(seq);
10932 	if (number_entries) {
10933 		for (i = 0; i < number_entries; i++) {
10934 			req_in->list_of_streams[i] = htons(list[i]);
10935 		}
10936 	}
10937 	if (SCTP_SIZE32(len) > len) {
10938 		/*-
10939 		 * Need to worry about the pad we may end up adding to the
10940 		 * end. This is easy since the struct is either aligned to 4
10941 		 * bytes or 2 bytes off.
10942 		 */
10943 		req_in->list_of_streams[number_entries] = 0;
10944 	}
10945 	/* now fix the chunk length */
10946 	ch->chunk_length = htons(len + old_len);
10947 	chk->book_size = len + old_len;
10948 	chk->book_size_scale = 0;
10949 	chk->send_size = SCTP_SIZE32(chk->book_size);
10950 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10951 	return;
10952 }
10953 
10954 
10955 void
10956 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
10957     uint32_t seq)
10958 {
10959 	int len, old_len;
10960 	struct sctp_stream_reset_tsn_request *req_tsn;
10961 	struct sctp_chunkhdr *ch;
10962 
10963 	ch = mtod(chk->data, struct sctp_chunkhdr *);
10964 
10965 
10966 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10967 
10968 	/* get to new offset for the param. */
10969 	req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
10970 	/* now how long will this param be? */
10971 	len = sizeof(struct sctp_stream_reset_tsn_request);
10972 	req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
10973 	req_tsn->ph.param_length = htons(len);
10974 	req_tsn->request_seq = htonl(seq);
10975 
10976 	/* now fix the chunk length */
10977 	ch->chunk_length = htons(len + old_len);
10978 	chk->send_size = len + old_len;
10979 	chk->book_size = SCTP_SIZE32(chk->send_size);
10980 	chk->book_size_scale = 0;
10981 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
10982 	return;
10983 }
10984 
10985 void
10986 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
10987     uint32_t resp_seq, uint32_t result)
10988 {
10989 	int len, old_len;
10990 	struct sctp_stream_reset_response *resp;
10991 	struct sctp_chunkhdr *ch;
10992 
10993 	ch = mtod(chk->data, struct sctp_chunkhdr *);
10994 
10995 
10996 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10997 
10998 	/* get to new offset for the param. */
10999 	resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11000 	/* now how long will this param be? */
11001 	len = sizeof(struct sctp_stream_reset_response);
11002 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11003 	resp->ph.param_length = htons(len);
11004 	resp->response_seq = htonl(resp_seq);
11005 	resp->result = ntohl(result);
11006 
11007 	/* now fix the chunk length */
11008 	ch->chunk_length = htons(len + old_len);
11009 	chk->book_size = len + old_len;
11010 	chk->book_size_scale = 0;
11011 	chk->send_size = SCTP_SIZE32(chk->book_size);
11012 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11013 	return;
11014 
11015 }
11016 
11017 
11018 void
11019 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11020     uint32_t resp_seq, uint32_t result,
11021     uint32_t send_una, uint32_t recv_next)
11022 {
11023 	int len, old_len;
11024 	struct sctp_stream_reset_response_tsn *resp;
11025 	struct sctp_chunkhdr *ch;
11026 
11027 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11028 
11029 
11030 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11031 
11032 	/* get to new offset for the param. */
11033 	resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11034 	/* now how long will this param be? */
11035 	len = sizeof(struct sctp_stream_reset_response_tsn);
11036 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11037 	resp->ph.param_length = htons(len);
11038 	resp->response_seq = htonl(resp_seq);
11039 	resp->result = htonl(result);
11040 	resp->senders_next_tsn = htonl(send_una);
11041 	resp->receivers_next_tsn = htonl(recv_next);
11042 
11043 	/* now fix the chunk length */
11044 	ch->chunk_length = htons(len + old_len);
11045 	chk->book_size = len + old_len;
11046 	chk->send_size = SCTP_SIZE32(chk->book_size);
11047 	chk->book_size_scale = 0;
11048 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11049 	return;
11050 }
11051 
11052 
11053 int
11054 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11055     int number_entries, uint16_t * list,
11056     uint8_t send_out_req, uint32_t resp_seq,
11057     uint8_t send_in_req,
11058     uint8_t send_tsn_req)
11059 {
11060 
11061 	struct sctp_association *asoc;
11062 	struct sctp_tmit_chunk *chk;
11063 	struct sctp_chunkhdr *ch;
11064 	uint32_t seq;
11065 
11066 	asoc = &stcb->asoc;
11067 	if (asoc->stream_reset_outstanding) {
11068 		/*-
11069 		 * Already one pending, must get ACK back to clear the flag.
11070 		 */
11071 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11072 		return (EBUSY);
11073 	}
11074 	if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0)) {
11075 		/* nothing to do */
11076 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11077 		return (EINVAL);
11078 	}
11079 	if (send_tsn_req && (send_out_req || send_in_req)) {
11080 		/* error, can't do that */
11081 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11082 		return (EINVAL);
11083 	}
11084 	sctp_alloc_a_chunk(stcb, chk);
11085 	if (chk == NULL) {
11086 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11087 		return (ENOMEM);
11088 	}
11089 	chk->copy_by_ref = 0;
11090 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11091 	chk->rec.chunk_id.can_take_data = 0;
11092 	chk->asoc = &stcb->asoc;
11093 	chk->book_size = sizeof(struct sctp_chunkhdr);
11094 	chk->send_size = SCTP_SIZE32(chk->book_size);
11095 	chk->book_size_scale = 0;
11096 
11097 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11098 	if (chk->data == NULL) {
11099 		sctp_free_a_chunk(stcb, chk);
11100 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11101 		return (ENOMEM);
11102 	}
11103 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11104 
11105 	/* setup chunk parameters */
11106 	chk->sent = SCTP_DATAGRAM_UNSENT;
11107 	chk->snd_count = 0;
11108 	chk->whoTo = asoc->primary_destination;
11109 	atomic_add_int(&chk->whoTo->ref_count, 1);
11110 
11111 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11112 	ch->chunk_type = SCTP_STREAM_RESET;
11113 	ch->chunk_flags = 0;
11114 	ch->chunk_length = htons(chk->book_size);
11115 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11116 
11117 	seq = stcb->asoc.str_reset_seq_out;
11118 	if (send_out_req) {
11119 		sctp_add_stream_reset_out(chk, number_entries, list,
11120 		    seq, resp_seq, (stcb->asoc.sending_seq - 1));
11121 		asoc->stream_reset_out_is_outstanding = 1;
11122 		seq++;
11123 		asoc->stream_reset_outstanding++;
11124 	}
11125 	if (send_in_req) {
11126 		sctp_add_stream_reset_in(chk, number_entries, list, seq);
11127 		asoc->stream_reset_outstanding++;
11128 	}
11129 	if (send_tsn_req) {
11130 		sctp_add_stream_reset_tsn(chk, seq);
11131 		asoc->stream_reset_outstanding++;
11132 	}
11133 	asoc->str_reset = chk;
11134 
11135 	/* insert the chunk for sending */
11136 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11137 	    chk,
11138 	    sctp_next);
11139 	asoc->ctrl_queue_cnt++;
11140 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11141 	return (0);
11142 }
11143 
11144 void
11145 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11146     struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11147 {
11148 	/*-
11149 	 * Formulate the abort message, and send it back down.
11150 	 */
11151 	struct mbuf *o_pak;
11152 	struct mbuf *mout;
11153 	struct sctp_abort_msg *abm;
11154 	struct ip *iph, *iph_out;
11155 	struct udphdr *udp;
11156 
11157 #ifdef INET6
11158 	struct ip6_hdr *ip6, *ip6_out;
11159 
11160 #endif
11161 	int iphlen_out, len;
11162 
11163 	/* don't respond to ABORT with ABORT */
11164 	if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11165 		if (err_cause)
11166 			sctp_m_freem(err_cause);
11167 		return;
11168 	}
11169 	iph = mtod(m, struct ip *);
11170 	switch (iph->ip_v) {
11171 	case IPVERSION:
11172 		len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11173 		break;
11174 #ifdef INET6
11175 	case IPV6_VERSION >> 4:
11176 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11177 		break;
11178 #endif
11179 	default:
11180 		return;
11181 	}
11182 	if (port) {
11183 		len += sizeof(struct udphdr);
11184 	}
11185 	mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
11186 	if (mout == NULL) {
11187 		if (err_cause)
11188 			sctp_m_freem(err_cause);
11189 		return;
11190 	}
11191 	SCTP_BUF_LEN(mout) = len;
11192 	SCTP_BUF_NEXT(mout) = err_cause;
11193 	iph_out = NULL;
11194 #ifdef INET6
11195 	ip6_out = NULL;
11196 #endif
11197 	switch (iph->ip_v) {
11198 	case IPVERSION:
11199 		iph_out = mtod(mout, struct ip *);
11200 
11201 		/* Fill in the IP header for the ABORT */
11202 		iph_out->ip_v = IPVERSION;
11203 		iph_out->ip_hl = (sizeof(struct ip) / 4);
11204 		iph_out->ip_tos = (u_char)0;
11205 		iph_out->ip_id = 0;
11206 		iph_out->ip_off = 0;
11207 		iph_out->ip_ttl = MAXTTL;
11208 		if (port) {
11209 			iph_out->ip_p = IPPROTO_UDP;
11210 		} else {
11211 			iph_out->ip_p = IPPROTO_SCTP;
11212 		}
11213 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11214 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11215 		/* let IP layer calculate this */
11216 		iph_out->ip_sum = 0;
11217 
11218 		iphlen_out = sizeof(*iph_out);
11219 		abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
11220 		break;
11221 #ifdef INET6
11222 	case IPV6_VERSION >> 4:
11223 		ip6 = (struct ip6_hdr *)iph;
11224 		ip6_out = mtod(mout, struct ip6_hdr *);
11225 
11226 		/* Fill in the IP6 header for the ABORT */
11227 		ip6_out->ip6_flow = ip6->ip6_flow;
11228 		ip6_out->ip6_hlim = ip6_defhlim;
11229 		if (port) {
11230 			ip6_out->ip6_nxt = IPPROTO_UDP;
11231 		} else {
11232 			ip6_out->ip6_nxt = IPPROTO_SCTP;
11233 		}
11234 		ip6_out->ip6_src = ip6->ip6_dst;
11235 		ip6_out->ip6_dst = ip6->ip6_src;
11236 
11237 		iphlen_out = sizeof(*ip6_out);
11238 		abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
11239 		break;
11240 #endif
11241 	default:
11242 		/* Currently not supported */
11243 		if (err_cause)
11244 			sctp_m_freem(err_cause);
11245 		sctp_m_freem(mout);
11246 		return;
11247 	}
11248 
11249 	udp = (struct udphdr *)abm;
11250 	if (port) {
11251 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11252 		udp->uh_dport = port;
11253 		/* set udp->uh_ulen later */
11254 		udp->uh_sum = 0;
11255 		iphlen_out += sizeof(struct udphdr);
11256 		abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
11257 	}
11258 	abm->sh.src_port = sh->dest_port;
11259 	abm->sh.dest_port = sh->src_port;
11260 	abm->sh.checksum = 0;
11261 	if (vtag == 0) {
11262 		abm->sh.v_tag = sh->v_tag;
11263 		abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
11264 	} else {
11265 		abm->sh.v_tag = htonl(vtag);
11266 		abm->msg.ch.chunk_flags = 0;
11267 	}
11268 	abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11269 
11270 	if (err_cause) {
11271 		struct mbuf *m_tmp = err_cause;
11272 		int err_len = 0;
11273 
11274 		/* get length of the err_cause chain */
11275 		while (m_tmp != NULL) {
11276 			err_len += SCTP_BUF_LEN(m_tmp);
11277 			m_tmp = SCTP_BUF_NEXT(m_tmp);
11278 		}
11279 		len = SCTP_BUF_LEN(mout) + err_len;
11280 		if (err_len % 4) {
11281 			/* need pad at end of chunk */
11282 			uint32_t cpthis = 0;
11283 			int padlen;
11284 
11285 			padlen = 4 - (len % 4);
11286 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11287 			len += padlen;
11288 		}
11289 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
11290 	} else {
11291 		len = SCTP_BUF_LEN(mout);
11292 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
11293 	}
11294 
11295 	/* add checksum */
11296 	abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
11297 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11298 		/* no mbuf's */
11299 		sctp_m_freem(mout);
11300 		return;
11301 	}
11302 	if (iph_out != NULL) {
11303 		sctp_route_t ro;
11304 		struct sctp_tcb *stcb = NULL;
11305 		int ret;
11306 
11307 		/* zap the stack pointer to the route */
11308 		bzero(&ro, sizeof ro);
11309 		if (port) {
11310 			udp->uh_ulen = htons(len - sizeof(struct ip));
11311 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11312 		}
11313 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
11314 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
11315 		/* set IPv4 length */
11316 		iph_out->ip_len = len;
11317 		/* out it goes */
11318 #ifdef  SCTP_PACKET_LOGGING
11319 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11320 			sctp_packet_log(mout, len);
11321 #endif
11322 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11323 		if (port) {
11324 			SCTP_ENABLE_UDP_CSUM(o_pak);
11325 		}
11326 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
11327 
11328 		/* Free the route if we got one back */
11329 		if (ro.ro_rt)
11330 			RTFREE(ro.ro_rt);
11331 	}
11332 #ifdef INET6
11333 	if (ip6_out != NULL) {
11334 		struct route_in6 ro;
11335 		int ret;
11336 		struct sctp_tcb *stcb = NULL;
11337 		struct ifnet *ifp = NULL;
11338 
11339 		/* zap the stack pointer to the route */
11340 		bzero(&ro, sizeof(ro));
11341 		if (port) {
11342 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11343 		}
11344 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
11345 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
11346 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
11347 #ifdef  SCTP_PACKET_LOGGING
11348 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11349 			sctp_packet_log(mout, len);
11350 #endif
11351 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11352 		if (port) {
11353 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11354 				udp->uh_sum = 0xffff;
11355 			}
11356 		}
11357 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11358 
11359 		/* Free the route if we got one back */
11360 		if (ro.ro_rt)
11361 			RTFREE(ro.ro_rt);
11362 	}
11363 #endif
11364 	SCTP_STAT_INCR(sctps_sendpackets);
11365 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11366 }
11367 
11368 void
11369 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
11370     uint32_t vrf_id, uint16_t port)
11371 {
11372 	struct mbuf *o_pak;
11373 	struct sctphdr *ihdr;
11374 	int retcode;
11375 	struct sctphdr *ohdr;
11376 	struct sctp_chunkhdr *ophdr;
11377 	struct ip *iph;
11378 	struct udphdr *udp = NULL;
11379 	struct mbuf *mout;
11380 
11381 #ifdef SCTP_DEBUG
11382 	struct sockaddr_in6 lsa6, fsa6;
11383 
11384 #endif
11385 	uint32_t val;
11386 	struct mbuf *at;
11387 	int len;
11388 
11389 	iph = mtod(m, struct ip *);
11390 	ihdr = (struct sctphdr *)((caddr_t)iph + iphlen);
11391 
11392 	SCTP_BUF_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), M_DONTWAIT);
11393 	if (scm == NULL) {
11394 		/* can't send because we can't add a mbuf */
11395 		return;
11396 	}
11397 	ohdr = mtod(scm, struct sctphdr *);
11398 	ohdr->src_port = ihdr->dest_port;
11399 	ohdr->dest_port = ihdr->src_port;
11400 	ohdr->v_tag = vtag;
11401 	ohdr->checksum = 0;
11402 	ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
11403 	ophdr->chunk_type = SCTP_OPERATION_ERROR;
11404 	ophdr->chunk_flags = 0;
11405 	len = 0;
11406 	at = scm;
11407 	while (at) {
11408 		len += SCTP_BUF_LEN(at);
11409 		at = SCTP_BUF_NEXT(at);
11410 	}
11411 	ophdr->chunk_length = htons(len - sizeof(struct sctphdr));
11412 	if (len % 4) {
11413 		/* need padding */
11414 		uint32_t cpthis = 0;
11415 		int padlen;
11416 
11417 		padlen = 4 - (len % 4);
11418 		m_copyback(scm, len, padlen, (caddr_t)&cpthis);
11419 		len += padlen;
11420 	}
11421 	val = sctp_calculate_sum(scm, NULL, 0);
11422 #ifdef INET6
11423 	if (port) {
11424 		mout = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
11425 	} else {
11426 		mout = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
11427 	}
11428 #else
11429 	if (port) {
11430 		mout = sctp_get_mbuf_for_msg(sizeof(struct ip) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
11431 	} else {
11432 		mout = sctp_get_mbuf_for_msg(sizeof(struct ip), 1, M_DONTWAIT, 1, MT_DATA);
11433 	}
11434 #endif
11435 	if (mout == NULL) {
11436 		sctp_m_freem(scm);
11437 		return;
11438 	}
11439 	SCTP_BUF_NEXT(mout) = scm;
11440 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11441 		sctp_m_freem(mout);
11442 		return;
11443 	}
11444 	ohdr->checksum = val;
11445 	switch (iph->ip_v) {
11446 	case IPVERSION:
11447 		{
11448 			/* V4 */
11449 			struct ip *out;
11450 			sctp_route_t ro;
11451 			struct sctp_tcb *stcb = NULL;
11452 
11453 			SCTP_BUF_LEN(mout) = sizeof(struct ip);
11454 			len += sizeof(struct ip);
11455 			if (port) {
11456 				SCTP_BUF_LEN(mout) += sizeof(struct udphdr);
11457 				len += sizeof(struct udphdr);
11458 			}
11459 			bzero(&ro, sizeof ro);
11460 			out = mtod(mout, struct ip *);
11461 			out->ip_v = iph->ip_v;
11462 			out->ip_hl = (sizeof(struct ip) / 4);
11463 			out->ip_tos = iph->ip_tos;
11464 			out->ip_id = iph->ip_id;
11465 			out->ip_off = 0;
11466 			out->ip_ttl = MAXTTL;
11467 			if (port) {
11468 				out->ip_p = IPPROTO_UDP;
11469 			} else {
11470 				out->ip_p = IPPROTO_SCTP;
11471 			}
11472 			out->ip_sum = 0;
11473 			out->ip_src = iph->ip_dst;
11474 			out->ip_dst = iph->ip_src;
11475 			out->ip_len = len;
11476 			if (port) {
11477 				udp = (struct udphdr *)(out + 1);
11478 				udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11479 				udp->uh_dport = port;
11480 				udp->uh_ulen = htons(len - sizeof(struct ip));
11481 				udp->uh_sum = in_pseudo(out->ip_src.s_addr, out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11482 			}
11483 #ifdef  SCTP_PACKET_LOGGING
11484 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11485 				sctp_packet_log(mout, len);
11486 #endif
11487 			SCTP_ATTACH_CHAIN(o_pak, mout, len);
11488 			if (port) {
11489 				SCTP_ENABLE_UDP_CSUM(o_pak);
11490 			}
11491 			SCTP_IP_OUTPUT(retcode, o_pak, &ro, stcb, vrf_id);
11492 
11493 			SCTP_STAT_INCR(sctps_sendpackets);
11494 			SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11495 			/* Free the route if we got one back */
11496 			if (ro.ro_rt)
11497 				RTFREE(ro.ro_rt);
11498 			break;
11499 		}
11500 #ifdef INET6
11501 	case IPV6_VERSION >> 4:
11502 		{
11503 			/* V6 */
11504 			struct route_in6 ro;
11505 			int ret;
11506 			struct sctp_tcb *stcb = NULL;
11507 			struct ifnet *ifp = NULL;
11508 			struct ip6_hdr *out6, *in6;
11509 
11510 			SCTP_BUF_LEN(mout) = sizeof(struct ip6_hdr);
11511 			len += sizeof(struct ip6_hdr);
11512 			bzero(&ro, sizeof ro);
11513 			if (port) {
11514 				SCTP_BUF_LEN(mout) += sizeof(struct udphdr);
11515 				len += sizeof(struct udphdr);
11516 			}
11517 			in6 = mtod(m, struct ip6_hdr *);
11518 			out6 = mtod(mout, struct ip6_hdr *);
11519 			out6->ip6_flow = in6->ip6_flow;
11520 			out6->ip6_hlim = ip6_defhlim;
11521 			if (port) {
11522 				out6->ip6_nxt = IPPROTO_UDP;
11523 			} else {
11524 				out6->ip6_nxt = IPPROTO_SCTP;
11525 			}
11526 			out6->ip6_src = in6->ip6_dst;
11527 			out6->ip6_dst = in6->ip6_src;
11528 			out6->ip6_plen = len - sizeof(struct ip6_hdr);
11529 			if (port) {
11530 				udp = (struct udphdr *)(out6 + 1);
11531 				udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11532 				udp->uh_dport = port;
11533 				udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11534 				udp->uh_sum = 0;
11535 			}
11536 #ifdef SCTP_DEBUG
11537 			bzero(&lsa6, sizeof(lsa6));
11538 			lsa6.sin6_len = sizeof(lsa6);
11539 			lsa6.sin6_family = AF_INET6;
11540 			lsa6.sin6_addr = out6->ip6_src;
11541 			bzero(&fsa6, sizeof(fsa6));
11542 			fsa6.sin6_len = sizeof(fsa6);
11543 			fsa6.sin6_family = AF_INET6;
11544 			fsa6.sin6_addr = out6->ip6_dst;
11545 #endif
11546 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_operr_to calling ipv6 output:\n");
11547 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "src: ");
11548 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&lsa6);
11549 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "dst ");
11550 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&fsa6);
11551 
11552 #ifdef  SCTP_PACKET_LOGGING
11553 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11554 				sctp_packet_log(mout, len);
11555 #endif
11556 			SCTP_ATTACH_CHAIN(o_pak, mout, len);
11557 			if (port) {
11558 				if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11559 					udp->uh_sum = 0xffff;
11560 				}
11561 			}
11562 			SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11563 
11564 			SCTP_STAT_INCR(sctps_sendpackets);
11565 			SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11566 			/* Free the route if we got one back */
11567 			if (ro.ro_rt)
11568 				RTFREE(ro.ro_rt);
11569 			break;
11570 		}
11571 #endif
11572 	default:
11573 		/* TSNH */
11574 		break;
11575 	}
11576 }
11577 
11578 static struct mbuf *
11579 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
11580     struct uio *uio,
11581     struct sctp_sndrcvinfo *srcv,
11582     int max_send_len,
11583     int user_marks_eor,
11584     int *error,
11585     uint32_t * sndout,
11586     struct mbuf **new_tail)
11587 {
11588 	struct mbuf *m;
11589 
11590 	m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
11591 	    (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
11592 	if (m == NULL) {
11593 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11594 		*error = ENOMEM;
11595 	} else {
11596 		*sndout = m_length(m, NULL);
11597 		*new_tail = m_last(m);
11598 	}
11599 	return (m);
11600 }
11601 
11602 static int
11603 sctp_copy_one(struct sctp_stream_queue_pending *sp,
11604     struct uio *uio,
11605     int resv_upfront)
11606 {
11607 	int left;
11608 
11609 	left = sp->length;
11610 	sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
11611 	    resv_upfront, 0);
11612 	if (sp->data == NULL) {
11613 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11614 		return (ENOMEM);
11615 	}
11616 	sp->tail_mbuf = m_last(sp->data);
11617 	return (0);
11618 }
11619 
11620 
11621 
11622 static struct sctp_stream_queue_pending *
11623 sctp_copy_it_in(struct sctp_tcb *stcb,
11624     struct sctp_association *asoc,
11625     struct sctp_sndrcvinfo *srcv,
11626     struct uio *uio,
11627     struct sctp_nets *net,
11628     int max_send_len,
11629     int user_marks_eor,
11630     int *error,
11631     int non_blocking)
11632 {
11633 	/*-
11634 	 * This routine must be very careful in its work. Protocol
11635 	 * processing is up and running so care must be taken to spl...()
11636 	 * when you need to do something that may effect the stcb/asoc. The
11637 	 * sb is locked however. When data is copied the protocol processing
11638 	 * should be enabled since this is a slower operation...
11639 	 */
11640 	struct sctp_stream_queue_pending *sp = NULL;
11641 	int resv_in_first;
11642 
11643 	*error = 0;
11644 	/* Now can we send this? */
11645 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
11646 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
11647 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
11648 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
11649 		/* got data while shutting down */
11650 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
11651 		*error = ECONNRESET;
11652 		goto out_now;
11653 	}
11654 	sctp_alloc_a_strmoq(stcb, sp);
11655 	if (sp == NULL) {
11656 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11657 		*error = ENOMEM;
11658 		goto out_now;
11659 	}
11660 	sp->act_flags = 0;
11661 	sp->sender_all_done = 0;
11662 	sp->sinfo_flags = srcv->sinfo_flags;
11663 	sp->timetolive = srcv->sinfo_timetolive;
11664 	sp->ppid = srcv->sinfo_ppid;
11665 	sp->context = srcv->sinfo_context;
11666 	sp->strseq = 0;
11667 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
11668 
11669 	sp->stream = srcv->sinfo_stream;
11670 	sp->length = min(uio->uio_resid, max_send_len);
11671 	if ((sp->length == (uint32_t) uio->uio_resid) &&
11672 	    ((user_marks_eor == 0) ||
11673 	    (srcv->sinfo_flags & SCTP_EOF) ||
11674 	    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
11675 		sp->msg_is_complete = 1;
11676 	} else {
11677 		sp->msg_is_complete = 0;
11678 	}
11679 	sp->sender_all_done = 0;
11680 	sp->some_taken = 0;
11681 	sp->put_last_out = 0;
11682 	resv_in_first = sizeof(struct sctp_data_chunk);
11683 	sp->data = sp->tail_mbuf = NULL;
11684 	*error = sctp_copy_one(sp, uio, resv_in_first);
11685 	if (*error) {
11686 		sctp_free_a_strmoq(stcb, sp);
11687 		sp = NULL;
11688 	} else {
11689 		if (sp->sinfo_flags & SCTP_ADDR_OVER) {
11690 			sp->net = net;
11691 			sp->addr_over = 1;
11692 		} else {
11693 			sp->net = asoc->primary_destination;
11694 			sp->addr_over = 0;
11695 		}
11696 		atomic_add_int(&sp->net->ref_count, 1);
11697 		sctp_set_prsctp_policy(stcb, sp);
11698 	}
11699 out_now:
11700 	return (sp);
11701 }
11702 
11703 
11704 int
11705 sctp_sosend(struct socket *so,
11706     struct sockaddr *addr,
11707     struct uio *uio,
11708     struct mbuf *top,
11709     struct mbuf *control,
11710     int flags,
11711     struct thread *p
11712 )
11713 {
11714 	struct sctp_inpcb *inp;
11715 	int error, use_rcvinfo = 0;
11716 	struct sctp_sndrcvinfo srcv;
11717 
11718 	inp = (struct sctp_inpcb *)so->so_pcb;
11719 	if (control) {
11720 		/* process cmsg snd/rcv info (maybe a assoc-id) */
11721 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
11722 		    sizeof(srcv))) {
11723 			/* got one */
11724 			use_rcvinfo = 1;
11725 		}
11726 	}
11727 	error = sctp_lower_sosend(so, addr, uio, top,
11728 	    control,
11729 	    flags,
11730 	    use_rcvinfo, &srcv
11731 	    ,p
11732 	    );
11733 	return (error);
11734 }
11735 
11736 
11737 int
11738 sctp_lower_sosend(struct socket *so,
11739     struct sockaddr *addr,
11740     struct uio *uio,
11741     struct mbuf *i_pak,
11742     struct mbuf *control,
11743     int flags,
11744     int use_rcvinfo,
11745     struct sctp_sndrcvinfo *srcv
11746     ,
11747     struct thread *p
11748 )
11749 {
11750 	unsigned int sndlen = 0, max_len;
11751 	int error, len;
11752 	struct mbuf *top = NULL;
11753 	int queue_only = 0, queue_only_for_init = 0;
11754 	int free_cnt_applied = 0;
11755 	int un_sent = 0;
11756 	int now_filled = 0;
11757 	unsigned int inqueue_bytes = 0;
11758 	struct sctp_block_entry be;
11759 	struct sctp_inpcb *inp;
11760 	struct sctp_tcb *stcb = NULL;
11761 	struct timeval now;
11762 	struct sctp_nets *net;
11763 	struct sctp_association *asoc;
11764 	struct sctp_inpcb *t_inp;
11765 	int user_marks_eor;
11766 	int create_lock_applied = 0;
11767 	int nagle_applies = 0;
11768 	int some_on_control = 0;
11769 	int got_all_of_the_send = 0;
11770 	int hold_tcblock = 0;
11771 	int non_blocking = 0;
11772 	int temp_flags = 0;
11773 	uint32_t local_add_more, local_soresv = 0;
11774 
11775 	error = 0;
11776 	net = NULL;
11777 	stcb = NULL;
11778 	asoc = NULL;
11779 
11780 	t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
11781 	if (inp == NULL) {
11782 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11783 		error = EFAULT;
11784 		if (i_pak) {
11785 			SCTP_RELEASE_PKT(i_pak);
11786 		}
11787 		return (error);
11788 	}
11789 	if ((uio == NULL) && (i_pak == NULL)) {
11790 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11791 		return (EINVAL);
11792 	}
11793 	user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
11794 	atomic_add_int(&inp->total_sends, 1);
11795 	if (uio) {
11796 		if (uio->uio_resid < 0) {
11797 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11798 			return (EINVAL);
11799 		}
11800 		sndlen = uio->uio_resid;
11801 	} else {
11802 		top = SCTP_HEADER_TO_CHAIN(i_pak);
11803 		sndlen = SCTP_HEADER_LEN(i_pak);
11804 	}
11805 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
11806 	    addr,
11807 	    sndlen);
11808 	/*-
11809          * Pre-screen address, if one is given the sin-len
11810          * must be set correctly!
11811          */
11812 	if (addr) {
11813 		if ((addr->sa_family == AF_INET) &&
11814 		    (addr->sa_len != sizeof(struct sockaddr_in))) {
11815 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11816 			error = EINVAL;
11817 			goto out_unlocked;
11818 		} else if ((addr->sa_family == AF_INET6) &&
11819 		    (addr->sa_len != sizeof(struct sockaddr_in6))) {
11820 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11821 			error = EINVAL;
11822 			goto out_unlocked;
11823 		}
11824 	}
11825 	hold_tcblock = 0;
11826 
11827 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
11828 	    (inp->sctp_socket->so_qlimit)) {
11829 		/* The listener can NOT send */
11830 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11831 		error = EFAULT;
11832 		goto out_unlocked;
11833 	}
11834 	if ((use_rcvinfo) && srcv) {
11835 		if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
11836 		    PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
11837 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11838 			error = EINVAL;
11839 			goto out_unlocked;
11840 		}
11841 		if (srcv->sinfo_flags)
11842 			SCTP_STAT_INCR(sctps_sends_with_flags);
11843 
11844 		if (srcv->sinfo_flags & SCTP_SENDALL) {
11845 			/* its a sendall */
11846 			error = sctp_sendall(inp, uio, top, srcv);
11847 			top = NULL;
11848 			goto out_unlocked;
11849 		}
11850 	}
11851 	/* now we must find the assoc */
11852 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
11853 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
11854 		SCTP_INP_RLOCK(inp);
11855 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
11856 		if (stcb == NULL) {
11857 			SCTP_INP_RUNLOCK(inp);
11858 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
11859 			error = ENOTCONN;
11860 			goto out_unlocked;
11861 		}
11862 		hold_tcblock = 0;
11863 		SCTP_INP_RUNLOCK(inp);
11864 		if (addr) {
11865 			/* Must locate the net structure if addr given */
11866 			net = sctp_findnet(stcb, addr);
11867 			if (net) {
11868 				/* validate port was 0 or correct */
11869 				struct sockaddr_in *sin;
11870 
11871 				sin = (struct sockaddr_in *)addr;
11872 				if ((sin->sin_port != 0) &&
11873 				    (sin->sin_port != stcb->rport)) {
11874 					net = NULL;
11875 				}
11876 			}
11877 			temp_flags |= SCTP_ADDR_OVER;
11878 		} else
11879 			net = stcb->asoc.primary_destination;
11880 		if (addr && (net == NULL)) {
11881 			/* Could not find address, was it legal */
11882 			if (addr->sa_family == AF_INET) {
11883 				struct sockaddr_in *sin;
11884 
11885 				sin = (struct sockaddr_in *)addr;
11886 				if (sin->sin_addr.s_addr == 0) {
11887 					if ((sin->sin_port == 0) ||
11888 					    (sin->sin_port == stcb->rport)) {
11889 						net = stcb->asoc.primary_destination;
11890 					}
11891 				}
11892 			} else {
11893 				struct sockaddr_in6 *sin6;
11894 
11895 				sin6 = (struct sockaddr_in6 *)addr;
11896 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
11897 					if ((sin6->sin6_port == 0) ||
11898 					    (sin6->sin6_port == stcb->rport)) {
11899 						net = stcb->asoc.primary_destination;
11900 					}
11901 				}
11902 			}
11903 		}
11904 		if (net == NULL) {
11905 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11906 			error = EINVAL;
11907 			goto out_unlocked;
11908 		}
11909 	} else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
11910 		stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
11911 		if (stcb) {
11912 			if (addr)
11913 				/*
11914 				 * Must locate the net structure if addr
11915 				 * given
11916 				 */
11917 				net = sctp_findnet(stcb, addr);
11918 			else
11919 				net = stcb->asoc.primary_destination;
11920 			if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
11921 			    ((net == NULL) || (addr == NULL))) {
11922 				struct sockaddr_in *sin;
11923 
11924 				if (addr == NULL) {
11925 					SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11926 					error = EINVAL;
11927 					goto out_unlocked;
11928 				}
11929 				sin = (struct sockaddr_in *)addr;
11930 				/* Validate port is 0 or correct */
11931 				if ((sin->sin_port != 0) &&
11932 				    (sin->sin_port != stcb->rport)) {
11933 					net = NULL;
11934 				}
11935 			}
11936 		}
11937 		hold_tcblock = 0;
11938 	} else if (addr) {
11939 		/*-
11940 		 * Since we did not use findep we must
11941 		 * increment it, and if we don't find a tcb
11942 		 * decrement it.
11943 		 */
11944 		SCTP_INP_WLOCK(inp);
11945 		SCTP_INP_INCR_REF(inp);
11946 		SCTP_INP_WUNLOCK(inp);
11947 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
11948 		if (stcb == NULL) {
11949 			SCTP_INP_WLOCK(inp);
11950 			SCTP_INP_DECR_REF(inp);
11951 			SCTP_INP_WUNLOCK(inp);
11952 		} else {
11953 			hold_tcblock = 1;
11954 		}
11955 	}
11956 	if ((stcb == NULL) && (addr)) {
11957 		/* Possible implicit send? */
11958 		SCTP_ASOC_CREATE_LOCK(inp);
11959 		create_lock_applied = 1;
11960 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
11961 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
11962 			/* Should I really unlock ? */
11963 			SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11964 			error = EFAULT;
11965 			goto out_unlocked;
11966 
11967 		}
11968 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
11969 		    (addr->sa_family == AF_INET6)) {
11970 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11971 			error = EINVAL;
11972 			goto out_unlocked;
11973 		}
11974 		SCTP_INP_WLOCK(inp);
11975 		SCTP_INP_INCR_REF(inp);
11976 		SCTP_INP_WUNLOCK(inp);
11977 		/* With the lock applied look again */
11978 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
11979 		if (stcb == NULL) {
11980 			SCTP_INP_WLOCK(inp);
11981 			SCTP_INP_DECR_REF(inp);
11982 			SCTP_INP_WUNLOCK(inp);
11983 		} else {
11984 			hold_tcblock = 1;
11985 		}
11986 		if (t_inp != inp) {
11987 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
11988 			error = ENOTCONN;
11989 			goto out_unlocked;
11990 		}
11991 	}
11992 	if (stcb == NULL) {
11993 		if (addr == NULL) {
11994 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
11995 			error = ENOENT;
11996 			goto out_unlocked;
11997 		} else {
11998 			/*
11999 			 * UDP style, we must go ahead and start the INIT
12000 			 * process
12001 			 */
12002 			uint32_t vrf_id;
12003 
12004 			if ((use_rcvinfo) && (srcv) &&
12005 			    ((srcv->sinfo_flags & SCTP_ABORT) ||
12006 			    ((srcv->sinfo_flags & SCTP_EOF) &&
12007 			    (sndlen == 0)))) {
12008 				/*-
12009 				 * User asks to abort a non-existant assoc,
12010 				 * or EOF a non-existant assoc with no data
12011 				 */
12012 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12013 				error = ENOENT;
12014 				goto out_unlocked;
12015 			}
12016 			/* get an asoc/stcb struct */
12017 			vrf_id = inp->def_vrf_id;
12018 #ifdef INVARIANTS
12019 			if (create_lock_applied == 0) {
12020 				panic("Error, should hold create lock and I don't?");
12021 			}
12022 #endif
12023 			stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id,
12024 			    p
12025 			    );
12026 			if (stcb == NULL) {
12027 				/* Error is setup for us in the call */
12028 				goto out_unlocked;
12029 			}
12030 			if (create_lock_applied) {
12031 				SCTP_ASOC_CREATE_UNLOCK(inp);
12032 				create_lock_applied = 0;
12033 			} else {
12034 				SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12035 			}
12036 			/*
12037 			 * Turn on queue only flag to prevent data from
12038 			 * being sent
12039 			 */
12040 			queue_only = 1;
12041 			asoc = &stcb->asoc;
12042 			SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12043 			(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12044 
12045 			/* initialize authentication params for the assoc */
12046 			sctp_initialize_auth_params(inp, stcb);
12047 
12048 			if (control) {
12049 				/*
12050 				 * see if a init structure exists in cmsg
12051 				 * headers
12052 				 */
12053 				struct sctp_initmsg initm;
12054 				int i;
12055 
12056 				if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
12057 				    sizeof(initm))) {
12058 					/*
12059 					 * we have an INIT override of the
12060 					 * default
12061 					 */
12062 					if (initm.sinit_max_attempts)
12063 						asoc->max_init_times = initm.sinit_max_attempts;
12064 					if (initm.sinit_num_ostreams)
12065 						asoc->pre_open_streams = initm.sinit_num_ostreams;
12066 					if (initm.sinit_max_instreams)
12067 						asoc->max_inbound_streams = initm.sinit_max_instreams;
12068 					if (initm.sinit_max_init_timeo)
12069 						asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
12070 					if (asoc->streamoutcnt < asoc->pre_open_streams) {
12071 						/* Default is NOT correct */
12072 						SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
12073 						    asoc->streamoutcnt, asoc->pre_open_streams);
12074 						/*
12075 						 * What happens if this
12076 						 * fails? we panic ...
12077 						 */
12078 						{
12079 							struct sctp_stream_out *tmp_str;
12080 							int had_lock = 0;
12081 
12082 							if (hold_tcblock) {
12083 								had_lock = 1;
12084 								SCTP_TCB_UNLOCK(stcb);
12085 							}
12086 							SCTP_MALLOC(tmp_str,
12087 							    struct sctp_stream_out *,
12088 							    (asoc->pre_open_streams *
12089 							    sizeof(struct sctp_stream_out)),
12090 							    SCTP_M_STRMO);
12091 							if (had_lock) {
12092 								SCTP_TCB_LOCK(stcb);
12093 							}
12094 							if (tmp_str != NULL) {
12095 								SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
12096 								asoc->strmout = tmp_str;
12097 								asoc->streamoutcnt = asoc->pre_open_streams;
12098 							} else {
12099 								asoc->pre_open_streams = asoc->streamoutcnt;
12100 							}
12101 						}
12102 						for (i = 0; i < asoc->streamoutcnt; i++) {
12103 							/*-
12104 						         * inbound side must be set
12105 						         * to 0xffff, also NOTE when
12106 						         * we get the INIT-ACK back
12107 						         * (for INIT sender) we MUST
12108 						         * reduce the count
12109 						         * (streamoutcnt) but first
12110 						         * check if we sent to any
12111 						         * of the upper streams that
12112 						         * were dropped (if some
12113 						         * were). Those that were
12114 						         * dropped must be notified
12115 						         * to the upper layer as
12116 						         * failed to send.
12117 						         */
12118 							asoc->strmout[i].next_sequence_sent = 0x0;
12119 							TAILQ_INIT(&asoc->strmout[i].outqueue);
12120 							asoc->strmout[i].stream_no = i;
12121 							asoc->strmout[i].last_msg_incomplete = 0;
12122 							asoc->strmout[i].next_spoke.tqe_next = 0;
12123 							asoc->strmout[i].next_spoke.tqe_prev = 0;
12124 						}
12125 					}
12126 				}
12127 			}
12128 			hold_tcblock = 1;
12129 			/* out with the INIT */
12130 			queue_only_for_init = 1;
12131 			/*-
12132 		         * we may want to dig in after this call and adjust the MTU
12133 		         * value. It defaulted to 1500 (constant) but the ro
12134 		         * structure may now have an update and thus we may need to
12135 		         * change it BEFORE we append the message.
12136 		         */
12137 			net = stcb->asoc.primary_destination;
12138 			asoc = &stcb->asoc;
12139 		}
12140 	}
12141 	if ((SCTP_SO_IS_NBIO(so)
12142 	    || (flags & MSG_NBIO)
12143 	    )) {
12144 		non_blocking = 1;
12145 	}
12146 	asoc = &stcb->asoc;
12147 
12148 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12149 		if (sndlen > asoc->smallest_mtu) {
12150 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12151 			error = EMSGSIZE;
12152 			goto out_unlocked;
12153 		}
12154 	}
12155 	/* would we block? */
12156 	if (non_blocking) {
12157 		if (hold_tcblock == 0) {
12158 			SCTP_TCB_LOCK(stcb);
12159 			hold_tcblock = 1;
12160 		}
12161 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12162 		if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12163 		    (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12164 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
12165 			if (sndlen > SCTP_SB_LIMIT_SND(so))
12166 				error = EMSGSIZE;
12167 			else
12168 				error = EWOULDBLOCK;
12169 			goto out_unlocked;
12170 		}
12171 		stcb->asoc.sb_send_resv += sndlen;
12172 		SCTP_TCB_UNLOCK(stcb);
12173 		hold_tcblock = 0;
12174 	} else {
12175 		atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
12176 	}
12177 	local_soresv = sndlen;
12178 	/* Keep the stcb from being freed under our feet */
12179 	if (free_cnt_applied) {
12180 #ifdef INVARIANTS
12181 		panic("refcnt already incremented");
12182 #else
12183 		printf("refcnt:1 already incremented?\n");
12184 #endif
12185 	} else {
12186 		atomic_add_int(&stcb->asoc.refcnt, 1);
12187 		free_cnt_applied = 1;
12188 	}
12189 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12190 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12191 		error = ECONNRESET;
12192 		goto out_unlocked;
12193 	}
12194 	if (create_lock_applied) {
12195 		SCTP_ASOC_CREATE_UNLOCK(inp);
12196 		create_lock_applied = 0;
12197 	}
12198 	if (asoc->stream_reset_outstanding) {
12199 		/*
12200 		 * Can't queue any data while stream reset is underway.
12201 		 */
12202 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
12203 		error = EAGAIN;
12204 		goto out_unlocked;
12205 	}
12206 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12207 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12208 		queue_only = 1;
12209 	}
12210 	if ((use_rcvinfo == 0) || (srcv == NULL)) {
12211 		/* Grab the default stuff from the asoc */
12212 		srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
12213 	}
12214 	/* we are now done with all control */
12215 	if (control) {
12216 		sctp_m_freem(control);
12217 		control = NULL;
12218 	}
12219 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12220 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12221 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12222 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12223 		if ((use_rcvinfo) &&
12224 		    (srcv->sinfo_flags & SCTP_ABORT)) {
12225 			;
12226 		} else {
12227 			SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12228 			error = ECONNRESET;
12229 			goto out_unlocked;
12230 		}
12231 	}
12232 	/* Ok, we will attempt a msgsnd :> */
12233 	if (p) {
12234 		p->td_ru.ru_msgsnd++;
12235 	}
12236 	if (stcb) {
12237 		if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
12238 			net = stcb->asoc.primary_destination;
12239 		}
12240 	}
12241 	if (net == NULL) {
12242 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12243 		error = EINVAL;
12244 		goto out_unlocked;
12245 	}
12246 	if ((net->flight_size > net->cwnd) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12247 		/*-
12248 		 * CMT: Added check for CMT above. net above is the primary
12249 		 * dest. If CMT is ON, sender should always attempt to send
12250 		 * with the output routine sctp_fill_outqueue() that loops
12251 		 * through all destination addresses. Therefore, if CMT is
12252 		 * ON, queue_only is NOT set to 1 here, so that
12253 		 * sctp_chunk_output() can be called below.
12254 		 */
12255 		queue_only = 1;
12256 
12257 	} else if (asoc->ifp_had_enobuf) {
12258 		SCTP_STAT_INCR(sctps_ifnomemqueued);
12259 		if (net->flight_size > (net->mtu * 2))
12260 			queue_only = 1;
12261 		asoc->ifp_had_enobuf = 0;
12262 	} else {
12263 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12264 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12265 	}
12266 	/* Are we aborting? */
12267 	if (srcv->sinfo_flags & SCTP_ABORT) {
12268 		struct mbuf *mm;
12269 		int tot_demand, tot_out = 0, max_out;
12270 
12271 		SCTP_STAT_INCR(sctps_sends_with_abort);
12272 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12273 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12274 			/* It has to be up before we abort */
12275 			/* how big is the user initiated abort? */
12276 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12277 			error = EINVAL;
12278 			goto out;
12279 		}
12280 		if (hold_tcblock) {
12281 			SCTP_TCB_UNLOCK(stcb);
12282 			hold_tcblock = 0;
12283 		}
12284 		if (top) {
12285 			struct mbuf *cntm = NULL;
12286 
12287 			mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
12288 			if (sndlen != 0) {
12289 				cntm = top;
12290 				while (cntm) {
12291 					tot_out += SCTP_BUF_LEN(cntm);
12292 					cntm = SCTP_BUF_NEXT(cntm);
12293 				}
12294 			}
12295 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12296 		} else {
12297 			/* Must fit in a MTU */
12298 			tot_out = sndlen;
12299 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12300 			if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12301 				/* To big */
12302 				SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12303 				error = EMSGSIZE;
12304 				goto out;
12305 			}
12306 			mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
12307 		}
12308 		if (mm == NULL) {
12309 			SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12310 			error = ENOMEM;
12311 			goto out;
12312 		}
12313 		max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12314 		max_out -= sizeof(struct sctp_abort_msg);
12315 		if (tot_out > max_out) {
12316 			tot_out = max_out;
12317 		}
12318 		if (mm) {
12319 			struct sctp_paramhdr *ph;
12320 
12321 			/* now move forward the data pointer */
12322 			ph = mtod(mm, struct sctp_paramhdr *);
12323 			ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12324 			ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
12325 			ph++;
12326 			SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
12327 			if (top == NULL) {
12328 				error = uiomove((caddr_t)ph, (int)tot_out, uio);
12329 				if (error) {
12330 					/*-
12331 				         * Here if we can't get his data we
12332 				         * still abort we just don't get to
12333 				         * send the users note :-0
12334 				         */
12335 					sctp_m_freem(mm);
12336 					mm = NULL;
12337 				}
12338 			} else {
12339 				if (sndlen != 0) {
12340 					SCTP_BUF_NEXT(mm) = top;
12341 				}
12342 			}
12343 		}
12344 		if (hold_tcblock == 0) {
12345 			SCTP_TCB_LOCK(stcb);
12346 			hold_tcblock = 1;
12347 		}
12348 		atomic_add_int(&stcb->asoc.refcnt, -1);
12349 		free_cnt_applied = 0;
12350 		/* release this lock, otherwise we hang on ourselves */
12351 		sctp_abort_an_association(stcb->sctp_ep, stcb,
12352 		    SCTP_RESPONSE_TO_USER_REQ,
12353 		    mm, SCTP_SO_LOCKED);
12354 		/* now relock the stcb so everything is sane */
12355 		hold_tcblock = 0;
12356 		stcb = NULL;
12357 		/*
12358 		 * In this case top is already chained to mm avoid double
12359 		 * free, since we free it below if top != NULL and driver
12360 		 * would free it after sending the packet out
12361 		 */
12362 		if (sndlen != 0) {
12363 			top = NULL;
12364 		}
12365 		goto out_unlocked;
12366 	}
12367 	/* Calculate the maximum we can send */
12368 	inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12369 	if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12370 		if (non_blocking) {
12371 			/* we already checked for non-blocking above. */
12372 			max_len = sndlen;
12373 		} else {
12374 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12375 		}
12376 	} else {
12377 		max_len = 0;
12378 	}
12379 	if (hold_tcblock) {
12380 		SCTP_TCB_UNLOCK(stcb);
12381 		hold_tcblock = 0;
12382 	}
12383 	/* Is the stream no. valid? */
12384 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
12385 		/* Invalid stream number */
12386 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12387 		error = EINVAL;
12388 		goto out_unlocked;
12389 	}
12390 	if (asoc->strmout == NULL) {
12391 		/* huh? software error */
12392 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12393 		error = EFAULT;
12394 		goto out_unlocked;
12395 	}
12396 	/* Unless E_EOR mode is on, we must make a send FIT in one call. */
12397 	if ((user_marks_eor == 0) &&
12398 	    (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
12399 		/* It will NEVER fit */
12400 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12401 		error = EMSGSIZE;
12402 		goto out_unlocked;
12403 	}
12404 	if ((uio == NULL) && user_marks_eor) {
12405 		/*-
12406 		 * We do not support eeor mode for
12407 		 * sending with mbuf chains (like sendfile).
12408 		 */
12409 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12410 		error = EINVAL;
12411 		goto out_unlocked;
12412 	}
12413 	if (user_marks_eor) {
12414 		local_add_more = SCTP_BASE_SYSCTL(sctp_add_more_threshold);
12415 	} else {
12416 		/*-
12417 		 * For non-eeor the whole message must fit in
12418 		 * the socket send buffer.
12419 		 */
12420 		local_add_more = sndlen;
12421 	}
12422 	len = 0;
12423 	if (non_blocking) {
12424 		goto skip_preblock;
12425 	}
12426 	if (((max_len <= local_add_more) &&
12427 	    (SCTP_SB_LIMIT_SND(so) > local_add_more)) ||
12428 	    ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {	/* if */
12429 		/* No room right now ! */
12430 		SOCKBUF_LOCK(&so->so_snd);
12431 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12432 		while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
12433 		    ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue) /* while */ )) {
12434 
12435 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12436 				sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA,
12437 				    so, asoc, sndlen);
12438 			}
12439 			be.error = 0;
12440 			stcb->block_entry = &be;
12441 			error = sbwait(&so->so_snd);
12442 			stcb->block_entry = NULL;
12443 			if (error || so->so_error || be.error) {
12444 				if (error == 0) {
12445 					if (so->so_error)
12446 						error = so->so_error;
12447 					if (be.error) {
12448 						error = be.error;
12449 					}
12450 				}
12451 				SOCKBUF_UNLOCK(&so->so_snd);
12452 				goto out_unlocked;
12453 			}
12454 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12455 				sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12456 				    so, asoc, stcb->asoc.total_output_queue_size);
12457 			}
12458 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12459 				goto out_unlocked;
12460 			}
12461 			inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12462 		}
12463 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12464 		if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12465 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12466 		} else {
12467 			max_len = 0;
12468 		}
12469 		SOCKBUF_UNLOCK(&so->so_snd);
12470 	}
12471 skip_preblock:
12472 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12473 		goto out_unlocked;
12474 	}
12475 	atomic_add_int(&stcb->total_sends, 1);
12476 	/*
12477 	 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
12478 	 * case NOTE: uio will be null when top/mbuf is passed
12479 	 */
12480 	if (sndlen == 0) {
12481 		if (srcv->sinfo_flags & SCTP_EOF) {
12482 			got_all_of_the_send = 1;
12483 			goto dataless_eof;
12484 		} else {
12485 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12486 			error = EINVAL;
12487 			goto out;
12488 		}
12489 	}
12490 	if (top == NULL) {
12491 		struct sctp_stream_queue_pending *sp;
12492 		struct sctp_stream_out *strm;
12493 		uint32_t sndout, initial_out;
12494 
12495 		initial_out = uio->uio_resid;
12496 
12497 		SCTP_TCB_SEND_LOCK(stcb);
12498 		if ((asoc->stream_locked) &&
12499 		    (asoc->stream_locked_on != srcv->sinfo_stream)) {
12500 			SCTP_TCB_SEND_UNLOCK(stcb);
12501 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12502 			error = EINVAL;
12503 			goto out;
12504 		}
12505 		SCTP_TCB_SEND_UNLOCK(stcb);
12506 
12507 		strm = &stcb->asoc.strmout[srcv->sinfo_stream];
12508 		if (strm->last_msg_incomplete == 0) {
12509 	do_a_copy_in:
12510 			sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
12511 			if ((sp == NULL) || (error)) {
12512 				goto out;
12513 			}
12514 			SCTP_TCB_SEND_LOCK(stcb);
12515 			if (sp->msg_is_complete) {
12516 				strm->last_msg_incomplete = 0;
12517 				asoc->stream_locked = 0;
12518 			} else {
12519 				/*
12520 				 * Just got locked to this guy in case of an
12521 				 * interrupt.
12522 				 */
12523 				strm->last_msg_incomplete = 1;
12524 				asoc->stream_locked = 1;
12525 				asoc->stream_locked_on = srcv->sinfo_stream;
12526 				sp->sender_all_done = 0;
12527 			}
12528 			sctp_snd_sb_alloc(stcb, sp->length);
12529 			atomic_add_int(&asoc->stream_queue_cnt, 1);
12530 			if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
12531 				sp->strseq = strm->next_sequence_sent;
12532 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
12533 					sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
12534 					    (uintptr_t) stcb, sp->length,
12535 					    (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
12536 				}
12537 				strm->next_sequence_sent++;
12538 			} else {
12539 				SCTP_STAT_INCR(sctps_sends_with_unord);
12540 			}
12541 			TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
12542 			if ((strm->next_spoke.tqe_next == NULL) &&
12543 			    (strm->next_spoke.tqe_prev == NULL)) {
12544 				/* Not on wheel, insert */
12545 				sctp_insert_on_wheel(stcb, asoc, strm, 1);
12546 			}
12547 			SCTP_TCB_SEND_UNLOCK(stcb);
12548 		} else {
12549 			SCTP_TCB_SEND_LOCK(stcb);
12550 			sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
12551 			SCTP_TCB_SEND_UNLOCK(stcb);
12552 			if (sp == NULL) {
12553 				/* ???? Huh ??? last msg is gone */
12554 #ifdef INVARIANTS
12555 				panic("Warning: Last msg marked incomplete, yet nothing left?");
12556 #else
12557 				SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
12558 				strm->last_msg_incomplete = 0;
12559 #endif
12560 				goto do_a_copy_in;
12561 
12562 			}
12563 		}
12564 		while (uio->uio_resid > 0) {
12565 			/* How much room do we have? */
12566 			struct mbuf *new_tail, *mm;
12567 
12568 			if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12569 				max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
12570 			else
12571 				max_len = 0;
12572 
12573 			if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
12574 			    (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
12575 			    (uio->uio_resid &&
12576 			    (uio->uio_resid <= (int)max_len))) {
12577 				sndout = 0;
12578 				new_tail = NULL;
12579 				if (hold_tcblock) {
12580 					SCTP_TCB_UNLOCK(stcb);
12581 					hold_tcblock = 0;
12582 				}
12583 				mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
12584 				if ((mm == NULL) || error) {
12585 					if (mm) {
12586 						sctp_m_freem(mm);
12587 					}
12588 					goto out;
12589 				}
12590 				/* Update the mbuf and count */
12591 				SCTP_TCB_SEND_LOCK(stcb);
12592 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12593 					/*
12594 					 * we need to get out. Peer probably
12595 					 * aborted.
12596 					 */
12597 					sctp_m_freem(mm);
12598 					if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
12599 						SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12600 						error = ECONNRESET;
12601 					}
12602 					SCTP_TCB_SEND_UNLOCK(stcb);
12603 					goto out;
12604 				}
12605 				if (sp->tail_mbuf) {
12606 					/* tack it to the end */
12607 					SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
12608 					sp->tail_mbuf = new_tail;
12609 				} else {
12610 					/* A stolen mbuf */
12611 					sp->data = mm;
12612 					sp->tail_mbuf = new_tail;
12613 				}
12614 				sctp_snd_sb_alloc(stcb, sndout);
12615 				atomic_add_int(&sp->length, sndout);
12616 				len += sndout;
12617 
12618 				/* Did we reach EOR? */
12619 				if ((uio->uio_resid == 0) &&
12620 				    ((user_marks_eor == 0) ||
12621 				    (srcv->sinfo_flags & SCTP_EOF) ||
12622 				    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))
12623 				    ) {
12624 					sp->msg_is_complete = 1;
12625 				} else {
12626 					sp->msg_is_complete = 0;
12627 				}
12628 				SCTP_TCB_SEND_UNLOCK(stcb);
12629 			}
12630 			if (uio->uio_resid == 0) {
12631 				/* got it all? */
12632 				continue;
12633 			}
12634 			/* PR-SCTP? */
12635 			if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
12636 				/*
12637 				 * This is ugly but we must assure locking
12638 				 * order
12639 				 */
12640 				if (hold_tcblock == 0) {
12641 					SCTP_TCB_LOCK(stcb);
12642 					hold_tcblock = 1;
12643 				}
12644 				sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
12645 				inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12646 				if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12647 					max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12648 				else
12649 					max_len = 0;
12650 				if (max_len > 0) {
12651 					continue;
12652 				}
12653 				SCTP_TCB_UNLOCK(stcb);
12654 				hold_tcblock = 0;
12655 			}
12656 			/* wait for space now */
12657 			if (non_blocking) {
12658 				/* Non-blocking io in place out */
12659 				goto skip_out_eof;
12660 			}
12661 			if ((net->flight_size > net->cwnd) &&
12662 			    (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12663 				queue_only = 1;
12664 			} else if (asoc->ifp_had_enobuf) {
12665 				SCTP_STAT_INCR(sctps_ifnomemqueued);
12666 				if (net->flight_size > (net->mtu * 2)) {
12667 					queue_only = 1;
12668 				} else {
12669 					queue_only = 0;
12670 				}
12671 				asoc->ifp_had_enobuf = 0;
12672 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12673 				    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12674 			} else {
12675 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12676 				    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12677 				if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
12678 					queue_only = 1;
12679 					SCTP_STAT_INCR(sctps_send_burst_avoid);
12680 				} else if (net->flight_size > net->cwnd) {
12681 					queue_only = 1;
12682 					SCTP_STAT_INCR(sctps_send_cwnd_avoid);
12683 				} else {
12684 					queue_only = 0;
12685 				}
12686 			}
12687 			if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
12688 			    (stcb->asoc.total_flight > 0) &&
12689 			    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
12690 			    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
12691 			    ) {
12692 
12693 				/*-
12694 				 * Ok, Nagle is set on and we have data outstanding.
12695 				 * Don't send anything and let SACKs drive out the
12696 				 * data unless wen have a "full" segment to send.
12697 				 */
12698 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
12699 					sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
12700 				}
12701 				SCTP_STAT_INCR(sctps_naglequeued);
12702 				nagle_applies = 1;
12703 			} else {
12704 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
12705 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
12706 						sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
12707 				}
12708 				SCTP_STAT_INCR(sctps_naglesent);
12709 				nagle_applies = 0;
12710 			}
12711 			/* What about the INIT, send it maybe */
12712 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12713 
12714 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
12715 				    nagle_applies, un_sent);
12716 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
12717 				    stcb->asoc.total_flight,
12718 				    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
12719 			}
12720 			if (queue_only_for_init) {
12721 				if (hold_tcblock == 0) {
12722 					SCTP_TCB_LOCK(stcb);
12723 					hold_tcblock = 1;
12724 				}
12725 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
12726 					/* a collision took us forward? */
12727 					queue_only_for_init = 0;
12728 					queue_only = 0;
12729 				} else {
12730 					sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
12731 					SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12732 					queue_only_for_init = 0;
12733 					queue_only = 1;
12734 				}
12735 			}
12736 			if ((queue_only == 0) && (nagle_applies == 0)
12737 			    ) {
12738 				/*-
12739 				 * need to start chunk output
12740 				 * before blocking.. note that if
12741 				 * a lock is already applied, then
12742 				 * the input via the net is happening
12743 				 * and I don't need to start output :-D
12744 				 */
12745 				if (hold_tcblock == 0) {
12746 					if (SCTP_TCB_TRYLOCK(stcb)) {
12747 						hold_tcblock = 1;
12748 						sctp_chunk_output(inp,
12749 						    stcb,
12750 						    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12751 					}
12752 				} else {
12753 					sctp_chunk_output(inp,
12754 					    stcb,
12755 					    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12756 				}
12757 				if (hold_tcblock == 1) {
12758 					SCTP_TCB_UNLOCK(stcb);
12759 					hold_tcblock = 0;
12760 				}
12761 			}
12762 			SOCKBUF_LOCK(&so->so_snd);
12763 			/*-
12764 		         * This is a bit strange, but I think it will
12765 		         * work. The total_output_queue_size is locked and
12766 		         * protected by the TCB_LOCK, which we just released.
12767 		         * There is a race that can occur between releasing it
12768 		         * above, and me getting the socket lock, where sacks
12769 		         * come in but we have not put the SB_WAIT on the
12770 		         * so_snd buffer to get the wakeup. After the LOCK
12771 		         * is applied the sack_processing will also need to
12772 		         * LOCK the so->so_snd to do the actual sowwakeup(). So
12773 		         * once we have the socket buffer lock if we recheck the
12774 		         * size we KNOW we will get to sleep safely with the
12775 		         * wakeup flag in place.
12776 		         */
12777 			if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
12778 			    min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))
12779 			    ) {
12780 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12781 					sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
12782 					    so, asoc, uio->uio_resid);
12783 				}
12784 				be.error = 0;
12785 				stcb->block_entry = &be;
12786 				error = sbwait(&so->so_snd);
12787 				stcb->block_entry = NULL;
12788 
12789 				if (error || so->so_error || be.error) {
12790 					if (error == 0) {
12791 						if (so->so_error)
12792 							error = so->so_error;
12793 						if (be.error) {
12794 							error = be.error;
12795 						}
12796 					}
12797 					SOCKBUF_UNLOCK(&so->so_snd);
12798 					goto out_unlocked;
12799 				}
12800 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12801 					sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12802 					    so, asoc, stcb->asoc.total_output_queue_size);
12803 				}
12804 			}
12805 			SOCKBUF_UNLOCK(&so->so_snd);
12806 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12807 				goto out_unlocked;
12808 			}
12809 		}
12810 		SCTP_TCB_SEND_LOCK(stcb);
12811 		if (sp) {
12812 			if (sp->msg_is_complete == 0) {
12813 				strm->last_msg_incomplete = 1;
12814 				asoc->stream_locked = 1;
12815 				asoc->stream_locked_on = srcv->sinfo_stream;
12816 			} else {
12817 				sp->sender_all_done = 1;
12818 				strm->last_msg_incomplete = 0;
12819 				asoc->stream_locked = 0;
12820 			}
12821 		} else {
12822 			SCTP_PRINTF("Huh no sp TSNH?\n");
12823 			strm->last_msg_incomplete = 0;
12824 			asoc->stream_locked = 0;
12825 		}
12826 		SCTP_TCB_SEND_UNLOCK(stcb);
12827 		if (uio->uio_resid == 0) {
12828 			got_all_of_the_send = 1;
12829 		}
12830 	} else if (top) {
12831 		/* We send in a 0, since we do NOT have any locks */
12832 		error = sctp_msg_append(stcb, net, top, srcv, 0);
12833 		top = NULL;
12834 		if (srcv->sinfo_flags & SCTP_EOF) {
12835 			/*
12836 			 * This should only happen for Panda for the mbuf
12837 			 * send case, which does NOT yet support EEOR mode.
12838 			 * Thus, we can just set this flag to do the proper
12839 			 * EOF handling.
12840 			 */
12841 			got_all_of_the_send = 1;
12842 		}
12843 	}
12844 	if (error) {
12845 		goto out;
12846 	}
12847 dataless_eof:
12848 	/* EOF thing ? */
12849 	if ((srcv->sinfo_flags & SCTP_EOF) &&
12850 	    (got_all_of_the_send == 1) &&
12851 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
12852 	    ) {
12853 		int cnt;
12854 
12855 		SCTP_STAT_INCR(sctps_sends_with_eof);
12856 		error = 0;
12857 		if (hold_tcblock == 0) {
12858 			SCTP_TCB_LOCK(stcb);
12859 			hold_tcblock = 1;
12860 		}
12861 		cnt = sctp_is_there_unsent_data(stcb);
12862 		if (TAILQ_EMPTY(&asoc->send_queue) &&
12863 		    TAILQ_EMPTY(&asoc->sent_queue) &&
12864 		    (cnt == 0)) {
12865 			if (asoc->locked_on_sending) {
12866 				goto abort_anyway;
12867 			}
12868 			/* there is nothing queued to send, so I'm done... */
12869 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
12870 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
12871 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
12872 				/* only send SHUTDOWN the first time through */
12873 				sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
12874 				if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
12875 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
12876 				}
12877 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
12878 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
12879 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
12880 				    asoc->primary_destination);
12881 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
12882 				    asoc->primary_destination);
12883 			}
12884 		} else {
12885 			/*-
12886 		         * we still got (or just got) data to send, so set
12887 		         * SHUTDOWN_PENDING
12888 		         */
12889 			/*-
12890 		         * XXX sockets draft says that SCTP_EOF should be
12891 		         * sent with no data.  currently, we will allow user
12892 		         * data to be sent first and move to
12893 		         * SHUTDOWN-PENDING
12894 		         */
12895 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
12896 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
12897 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
12898 				if (hold_tcblock == 0) {
12899 					SCTP_TCB_LOCK(stcb);
12900 					hold_tcblock = 1;
12901 				}
12902 				if (asoc->locked_on_sending) {
12903 					/* Locked to send out the data */
12904 					struct sctp_stream_queue_pending *sp;
12905 
12906 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
12907 					if (sp) {
12908 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
12909 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
12910 					}
12911 				}
12912 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
12913 				if (TAILQ_EMPTY(&asoc->send_queue) &&
12914 				    TAILQ_EMPTY(&asoc->sent_queue) &&
12915 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
12916 			abort_anyway:
12917 					if (free_cnt_applied) {
12918 						atomic_add_int(&stcb->asoc.refcnt, -1);
12919 						free_cnt_applied = 0;
12920 					}
12921 					sctp_abort_an_association(stcb->sctp_ep, stcb,
12922 					    SCTP_RESPONSE_TO_USER_REQ,
12923 					    NULL, SCTP_SO_LOCKED);
12924 					/*
12925 					 * now relock the stcb so everything
12926 					 * is sane
12927 					 */
12928 					hold_tcblock = 0;
12929 					stcb = NULL;
12930 					goto out;
12931 				}
12932 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
12933 				    asoc->primary_destination);
12934 				sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
12935 			}
12936 		}
12937 	}
12938 skip_out_eof:
12939 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
12940 		some_on_control = 1;
12941 	}
12942 	if ((net->flight_size > net->cwnd) &&
12943 	    (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12944 		queue_only = 1;
12945 	} else if (asoc->ifp_had_enobuf) {
12946 		SCTP_STAT_INCR(sctps_ifnomemqueued);
12947 		if (net->flight_size > (net->mtu * 2)) {
12948 			queue_only = 1;
12949 		} else {
12950 			queue_only = 0;
12951 		}
12952 		asoc->ifp_had_enobuf = 0;
12953 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12954 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12955 	} else {
12956 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12957 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12958 		if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
12959 			queue_only = 1;
12960 			SCTP_STAT_INCR(sctps_send_burst_avoid);
12961 		} else if (net->flight_size > net->cwnd) {
12962 			queue_only = 1;
12963 			SCTP_STAT_INCR(sctps_send_cwnd_avoid);
12964 		} else {
12965 			queue_only = 0;
12966 		}
12967 	}
12968 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
12969 	    (stcb->asoc.total_flight > 0) &&
12970 	    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
12971 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
12972 	    ) {
12973 		/*-
12974 		 * Ok, Nagle is set on and we have data outstanding.
12975 		 * Don't send anything and let SACKs drive out the
12976 		 * data unless wen have a "full" segment to send.
12977 		 */
12978 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
12979 			sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
12980 		}
12981 		SCTP_STAT_INCR(sctps_naglequeued);
12982 		nagle_applies = 1;
12983 	} else {
12984 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
12985 			if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
12986 				sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
12987 		}
12988 		SCTP_STAT_INCR(sctps_naglesent);
12989 		nagle_applies = 0;
12990 	}
12991 	if (queue_only_for_init) {
12992 		if (hold_tcblock == 0) {
12993 			SCTP_TCB_LOCK(stcb);
12994 			hold_tcblock = 1;
12995 		}
12996 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
12997 			/* a collision took us forward? */
12998 			queue_only_for_init = 0;
12999 			queue_only = 0;
13000 		} else {
13001 			sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13002 			SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13003 			queue_only_for_init = 0;
13004 			queue_only = 1;
13005 		}
13006 	}
13007 	if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13008 		/* we can attempt to send too. */
13009 		if (hold_tcblock == 0) {
13010 			/*
13011 			 * If there is activity recv'ing sacks no need to
13012 			 * send
13013 			 */
13014 			if (SCTP_TCB_TRYLOCK(stcb)) {
13015 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13016 				hold_tcblock = 1;
13017 			}
13018 		} else {
13019 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13020 		}
13021 	} else if ((queue_only == 0) &&
13022 		    (stcb->asoc.peers_rwnd == 0) &&
13023 	    (stcb->asoc.total_flight == 0)) {
13024 		/* We get to have a probe outstanding */
13025 		if (hold_tcblock == 0) {
13026 			hold_tcblock = 1;
13027 			SCTP_TCB_LOCK(stcb);
13028 		}
13029 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13030 	} else if (some_on_control) {
13031 		int num_out, reason, cwnd_full, frag_point;
13032 
13033 		/* Here we do control only */
13034 		if (hold_tcblock == 0) {
13035 			hold_tcblock = 1;
13036 			SCTP_TCB_LOCK(stcb);
13037 		}
13038 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13039 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13040 		    &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13041 	}
13042 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d",
13043 	    queue_only, stcb->asoc.peers_rwnd, un_sent,
13044 	    stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13045 	    stcb->asoc.total_output_queue_size, error);
13046 
13047 out:
13048 out_unlocked:
13049 
13050 	if (local_soresv && stcb) {
13051 		atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13052 		local_soresv = 0;
13053 	}
13054 	if (create_lock_applied) {
13055 		SCTP_ASOC_CREATE_UNLOCK(inp);
13056 		create_lock_applied = 0;
13057 	}
13058 	if ((stcb) && hold_tcblock) {
13059 		SCTP_TCB_UNLOCK(stcb);
13060 	}
13061 	if (stcb && free_cnt_applied) {
13062 		atomic_add_int(&stcb->asoc.refcnt, -1);
13063 	}
13064 #ifdef INVARIANTS
13065 	if (stcb) {
13066 		if (mtx_owned(&stcb->tcb_mtx)) {
13067 			panic("Leaving with tcb mtx owned?");
13068 		}
13069 		if (mtx_owned(&stcb->tcb_send_mtx)) {
13070 			panic("Leaving with tcb send mtx owned?");
13071 		}
13072 	}
13073 #endif
13074 	if (top) {
13075 		sctp_m_freem(top);
13076 	}
13077 	if (control) {
13078 		sctp_m_freem(control);
13079 	}
13080 	return (error);
13081 }
13082 
13083 
13084 /*
13085  * generate an AUTHentication chunk, if required
13086  */
13087 struct mbuf *
13088 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13089     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13090     struct sctp_tcb *stcb, uint8_t chunk)
13091 {
13092 	struct mbuf *m_auth;
13093 	struct sctp_auth_chunk *auth;
13094 	int chunk_len;
13095 
13096 	if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13097 	    (stcb == NULL))
13098 		return (m);
13099 
13100 	/* sysctl disabled auth? */
13101 	if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13102 		return (m);
13103 
13104 	/* peer doesn't do auth... */
13105 	if (!stcb->asoc.peer_supports_auth) {
13106 		return (m);
13107 	}
13108 	/* does the requested chunk require auth? */
13109 	if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13110 		return (m);
13111 	}
13112 	m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13113 	if (m_auth == NULL) {
13114 		/* no mbuf's */
13115 		return (m);
13116 	}
13117 	/* reserve some space if this will be the first mbuf */
13118 	if (m == NULL)
13119 		SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13120 	/* fill in the AUTH chunk details */
13121 	auth = mtod(m_auth, struct sctp_auth_chunk *);
13122 	bzero(auth, sizeof(*auth));
13123 	auth->ch.chunk_type = SCTP_AUTHENTICATION;
13124 	auth->ch.chunk_flags = 0;
13125 	chunk_len = sizeof(*auth) +
13126 	    sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13127 	auth->ch.chunk_length = htons(chunk_len);
13128 	auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13129 	/* key id and hmac digest will be computed and filled in upon send */
13130 
13131 	/* save the offset where the auth was inserted into the chain */
13132 	if (m != NULL) {
13133 		struct mbuf *cn;
13134 
13135 		*offset = 0;
13136 		cn = m;
13137 		while (cn) {
13138 			*offset += SCTP_BUF_LEN(cn);
13139 			cn = SCTP_BUF_NEXT(cn);
13140 		}
13141 	} else
13142 		*offset = 0;
13143 
13144 	/* update length and return pointer to the auth chunk */
13145 	SCTP_BUF_LEN(m_auth) = chunk_len;
13146 	m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13147 	if (auth_ret != NULL)
13148 		*auth_ret = auth;
13149 
13150 	return (m);
13151 }
13152 
13153 #ifdef INET6
13154 int
13155 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13156 {
13157 	struct nd_prefix *pfx = NULL;
13158 	struct nd_pfxrouter *pfxrtr = NULL;
13159 	struct sockaddr_in6 gw6;
13160 
13161 	if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13162 		return (0);
13163 
13164 	/* get prefix entry of address */
13165 	LIST_FOREACH(pfx, &nd_prefix, ndpr_entry) {
13166 		if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13167 			continue;
13168 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13169 		    &src6->sin6_addr, &pfx->ndpr_mask))
13170 			break;
13171 	}
13172 	/* no prefix entry in the prefix list */
13173 	if (pfx == NULL) {
13174 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13175 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13176 		return (0);
13177 	}
13178 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13179 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13180 
13181 	/* search installed gateway from prefix entry */
13182 	for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
13183 	    pfxrtr->pfr_next) {
13184 		memset(&gw6, 0, sizeof(struct sockaddr_in6));
13185 		gw6.sin6_family = AF_INET6;
13186 		gw6.sin6_len = sizeof(struct sockaddr_in6);
13187 		memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13188 		    sizeof(struct in6_addr));
13189 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13190 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13191 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13192 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13193 		if (sctp_cmpaddr((struct sockaddr *)&gw6,
13194 		    ro->ro_rt->rt_gateway)) {
13195 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13196 			return (1);
13197 		}
13198 	}
13199 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13200 	return (0);
13201 }
13202 
13203 #endif
13204 
13205 int
13206 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13207 {
13208 	struct sockaddr_in *sin, *mask;
13209 	struct ifaddr *ifa;
13210 	struct in_addr srcnetaddr, gwnetaddr;
13211 
13212 	if (ro == NULL || ro->ro_rt == NULL ||
13213 	    sifa->address.sa.sa_family != AF_INET) {
13214 		return (0);
13215 	}
13216 	ifa = (struct ifaddr *)sifa->ifa;
13217 	mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13218 	sin = (struct sockaddr_in *)&sifa->address.sin;
13219 	srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13220 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13221 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13222 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13223 
13224 	sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13225 	gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13226 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13227 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13228 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13229 	if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13230 		return (1);
13231 	}
13232 	return (0);
13233 }
13234