xref: /freebsd/sys/netinet/sctp_output.c (revision cacdd70cc751fb68dec4b86c5e5b8c969b6e26ef)
1 /*-
2  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $	 */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <netinet/sctp_os.h>
37 #include <sys/proc.h>
38 #include <netinet/sctp_var.h>
39 #include <netinet/sctp_sysctl.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctputil.h>
43 #include <netinet/sctp_output.h>
44 #include <netinet/sctp_uio.h>
45 #include <netinet/sctputil.h>
46 #include <netinet/sctp_auth.h>
47 #include <netinet/sctp_timer.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_indata.h>
50 #include <netinet/sctp_bsd_addr.h>
51 #include <netinet/sctp_input.h>
52 #include <netinet/udp.h>
53 #include <machine/in_cksum.h>
54 
55 
56 
57 #define SCTP_MAX_GAPS_INARRAY 4
58 struct sack_track {
59 	uint8_t right_edge;	/* mergable on the right edge */
60 	uint8_t left_edge;	/* mergable on the left edge */
61 	uint8_t num_entries;
62 	uint8_t spare;
63 	struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
64 };
65 
66 struct sack_track sack_array[256] = {
67 	{0, 0, 0, 0,		/* 0x00 */
68 		{{0, 0},
69 		{0, 0},
70 		{0, 0},
71 		{0, 0}
72 		}
73 	},
74 	{1, 0, 1, 0,		/* 0x01 */
75 		{{0, 0},
76 		{0, 0},
77 		{0, 0},
78 		{0, 0}
79 		}
80 	},
81 	{0, 0, 1, 0,		/* 0x02 */
82 		{{1, 1},
83 		{0, 0},
84 		{0, 0},
85 		{0, 0}
86 		}
87 	},
88 	{1, 0, 1, 0,		/* 0x03 */
89 		{{0, 1},
90 		{0, 0},
91 		{0, 0},
92 		{0, 0}
93 		}
94 	},
95 	{0, 0, 1, 0,		/* 0x04 */
96 		{{2, 2},
97 		{0, 0},
98 		{0, 0},
99 		{0, 0}
100 		}
101 	},
102 	{1, 0, 2, 0,		/* 0x05 */
103 		{{0, 0},
104 		{2, 2},
105 		{0, 0},
106 		{0, 0}
107 		}
108 	},
109 	{0, 0, 1, 0,		/* 0x06 */
110 		{{1, 2},
111 		{0, 0},
112 		{0, 0},
113 		{0, 0}
114 		}
115 	},
116 	{1, 0, 1, 0,		/* 0x07 */
117 		{{0, 2},
118 		{0, 0},
119 		{0, 0},
120 		{0, 0}
121 		}
122 	},
123 	{0, 0, 1, 0,		/* 0x08 */
124 		{{3, 3},
125 		{0, 0},
126 		{0, 0},
127 		{0, 0}
128 		}
129 	},
130 	{1, 0, 2, 0,		/* 0x09 */
131 		{{0, 0},
132 		{3, 3},
133 		{0, 0},
134 		{0, 0}
135 		}
136 	},
137 	{0, 0, 2, 0,		/* 0x0a */
138 		{{1, 1},
139 		{3, 3},
140 		{0, 0},
141 		{0, 0}
142 		}
143 	},
144 	{1, 0, 2, 0,		/* 0x0b */
145 		{{0, 1},
146 		{3, 3},
147 		{0, 0},
148 		{0, 0}
149 		}
150 	},
151 	{0, 0, 1, 0,		/* 0x0c */
152 		{{2, 3},
153 		{0, 0},
154 		{0, 0},
155 		{0, 0}
156 		}
157 	},
158 	{1, 0, 2, 0,		/* 0x0d */
159 		{{0, 0},
160 		{2, 3},
161 		{0, 0},
162 		{0, 0}
163 		}
164 	},
165 	{0, 0, 1, 0,		/* 0x0e */
166 		{{1, 3},
167 		{0, 0},
168 		{0, 0},
169 		{0, 0}
170 		}
171 	},
172 	{1, 0, 1, 0,		/* 0x0f */
173 		{{0, 3},
174 		{0, 0},
175 		{0, 0},
176 		{0, 0}
177 		}
178 	},
179 	{0, 0, 1, 0,		/* 0x10 */
180 		{{4, 4},
181 		{0, 0},
182 		{0, 0},
183 		{0, 0}
184 		}
185 	},
186 	{1, 0, 2, 0,		/* 0x11 */
187 		{{0, 0},
188 		{4, 4},
189 		{0, 0},
190 		{0, 0}
191 		}
192 	},
193 	{0, 0, 2, 0,		/* 0x12 */
194 		{{1, 1},
195 		{4, 4},
196 		{0, 0},
197 		{0, 0}
198 		}
199 	},
200 	{1, 0, 2, 0,		/* 0x13 */
201 		{{0, 1},
202 		{4, 4},
203 		{0, 0},
204 		{0, 0}
205 		}
206 	},
207 	{0, 0, 2, 0,		/* 0x14 */
208 		{{2, 2},
209 		{4, 4},
210 		{0, 0},
211 		{0, 0}
212 		}
213 	},
214 	{1, 0, 3, 0,		/* 0x15 */
215 		{{0, 0},
216 		{2, 2},
217 		{4, 4},
218 		{0, 0}
219 		}
220 	},
221 	{0, 0, 2, 0,		/* 0x16 */
222 		{{1, 2},
223 		{4, 4},
224 		{0, 0},
225 		{0, 0}
226 		}
227 	},
228 	{1, 0, 2, 0,		/* 0x17 */
229 		{{0, 2},
230 		{4, 4},
231 		{0, 0},
232 		{0, 0}
233 		}
234 	},
235 	{0, 0, 1, 0,		/* 0x18 */
236 		{{3, 4},
237 		{0, 0},
238 		{0, 0},
239 		{0, 0}
240 		}
241 	},
242 	{1, 0, 2, 0,		/* 0x19 */
243 		{{0, 0},
244 		{3, 4},
245 		{0, 0},
246 		{0, 0}
247 		}
248 	},
249 	{0, 0, 2, 0,		/* 0x1a */
250 		{{1, 1},
251 		{3, 4},
252 		{0, 0},
253 		{0, 0}
254 		}
255 	},
256 	{1, 0, 2, 0,		/* 0x1b */
257 		{{0, 1},
258 		{3, 4},
259 		{0, 0},
260 		{0, 0}
261 		}
262 	},
263 	{0, 0, 1, 0,		/* 0x1c */
264 		{{2, 4},
265 		{0, 0},
266 		{0, 0},
267 		{0, 0}
268 		}
269 	},
270 	{1, 0, 2, 0,		/* 0x1d */
271 		{{0, 0},
272 		{2, 4},
273 		{0, 0},
274 		{0, 0}
275 		}
276 	},
277 	{0, 0, 1, 0,		/* 0x1e */
278 		{{1, 4},
279 		{0, 0},
280 		{0, 0},
281 		{0, 0}
282 		}
283 	},
284 	{1, 0, 1, 0,		/* 0x1f */
285 		{{0, 4},
286 		{0, 0},
287 		{0, 0},
288 		{0, 0}
289 		}
290 	},
291 	{0, 0, 1, 0,		/* 0x20 */
292 		{{5, 5},
293 		{0, 0},
294 		{0, 0},
295 		{0, 0}
296 		}
297 	},
298 	{1, 0, 2, 0,		/* 0x21 */
299 		{{0, 0},
300 		{5, 5},
301 		{0, 0},
302 		{0, 0}
303 		}
304 	},
305 	{0, 0, 2, 0,		/* 0x22 */
306 		{{1, 1},
307 		{5, 5},
308 		{0, 0},
309 		{0, 0}
310 		}
311 	},
312 	{1, 0, 2, 0,		/* 0x23 */
313 		{{0, 1},
314 		{5, 5},
315 		{0, 0},
316 		{0, 0}
317 		}
318 	},
319 	{0, 0, 2, 0,		/* 0x24 */
320 		{{2, 2},
321 		{5, 5},
322 		{0, 0},
323 		{0, 0}
324 		}
325 	},
326 	{1, 0, 3, 0,		/* 0x25 */
327 		{{0, 0},
328 		{2, 2},
329 		{5, 5},
330 		{0, 0}
331 		}
332 	},
333 	{0, 0, 2, 0,		/* 0x26 */
334 		{{1, 2},
335 		{5, 5},
336 		{0, 0},
337 		{0, 0}
338 		}
339 	},
340 	{1, 0, 2, 0,		/* 0x27 */
341 		{{0, 2},
342 		{5, 5},
343 		{0, 0},
344 		{0, 0}
345 		}
346 	},
347 	{0, 0, 2, 0,		/* 0x28 */
348 		{{3, 3},
349 		{5, 5},
350 		{0, 0},
351 		{0, 0}
352 		}
353 	},
354 	{1, 0, 3, 0,		/* 0x29 */
355 		{{0, 0},
356 		{3, 3},
357 		{5, 5},
358 		{0, 0}
359 		}
360 	},
361 	{0, 0, 3, 0,		/* 0x2a */
362 		{{1, 1},
363 		{3, 3},
364 		{5, 5},
365 		{0, 0}
366 		}
367 	},
368 	{1, 0, 3, 0,		/* 0x2b */
369 		{{0, 1},
370 		{3, 3},
371 		{5, 5},
372 		{0, 0}
373 		}
374 	},
375 	{0, 0, 2, 0,		/* 0x2c */
376 		{{2, 3},
377 		{5, 5},
378 		{0, 0},
379 		{0, 0}
380 		}
381 	},
382 	{1, 0, 3, 0,		/* 0x2d */
383 		{{0, 0},
384 		{2, 3},
385 		{5, 5},
386 		{0, 0}
387 		}
388 	},
389 	{0, 0, 2, 0,		/* 0x2e */
390 		{{1, 3},
391 		{5, 5},
392 		{0, 0},
393 		{0, 0}
394 		}
395 	},
396 	{1, 0, 2, 0,		/* 0x2f */
397 		{{0, 3},
398 		{5, 5},
399 		{0, 0},
400 		{0, 0}
401 		}
402 	},
403 	{0, 0, 1, 0,		/* 0x30 */
404 		{{4, 5},
405 		{0, 0},
406 		{0, 0},
407 		{0, 0}
408 		}
409 	},
410 	{1, 0, 2, 0,		/* 0x31 */
411 		{{0, 0},
412 		{4, 5},
413 		{0, 0},
414 		{0, 0}
415 		}
416 	},
417 	{0, 0, 2, 0,		/* 0x32 */
418 		{{1, 1},
419 		{4, 5},
420 		{0, 0},
421 		{0, 0}
422 		}
423 	},
424 	{1, 0, 2, 0,		/* 0x33 */
425 		{{0, 1},
426 		{4, 5},
427 		{0, 0},
428 		{0, 0}
429 		}
430 	},
431 	{0, 0, 2, 0,		/* 0x34 */
432 		{{2, 2},
433 		{4, 5},
434 		{0, 0},
435 		{0, 0}
436 		}
437 	},
438 	{1, 0, 3, 0,		/* 0x35 */
439 		{{0, 0},
440 		{2, 2},
441 		{4, 5},
442 		{0, 0}
443 		}
444 	},
445 	{0, 0, 2, 0,		/* 0x36 */
446 		{{1, 2},
447 		{4, 5},
448 		{0, 0},
449 		{0, 0}
450 		}
451 	},
452 	{1, 0, 2, 0,		/* 0x37 */
453 		{{0, 2},
454 		{4, 5},
455 		{0, 0},
456 		{0, 0}
457 		}
458 	},
459 	{0, 0, 1, 0,		/* 0x38 */
460 		{{3, 5},
461 		{0, 0},
462 		{0, 0},
463 		{0, 0}
464 		}
465 	},
466 	{1, 0, 2, 0,		/* 0x39 */
467 		{{0, 0},
468 		{3, 5},
469 		{0, 0},
470 		{0, 0}
471 		}
472 	},
473 	{0, 0, 2, 0,		/* 0x3a */
474 		{{1, 1},
475 		{3, 5},
476 		{0, 0},
477 		{0, 0}
478 		}
479 	},
480 	{1, 0, 2, 0,		/* 0x3b */
481 		{{0, 1},
482 		{3, 5},
483 		{0, 0},
484 		{0, 0}
485 		}
486 	},
487 	{0, 0, 1, 0,		/* 0x3c */
488 		{{2, 5},
489 		{0, 0},
490 		{0, 0},
491 		{0, 0}
492 		}
493 	},
494 	{1, 0, 2, 0,		/* 0x3d */
495 		{{0, 0},
496 		{2, 5},
497 		{0, 0},
498 		{0, 0}
499 		}
500 	},
501 	{0, 0, 1, 0,		/* 0x3e */
502 		{{1, 5},
503 		{0, 0},
504 		{0, 0},
505 		{0, 0}
506 		}
507 	},
508 	{1, 0, 1, 0,		/* 0x3f */
509 		{{0, 5},
510 		{0, 0},
511 		{0, 0},
512 		{0, 0}
513 		}
514 	},
515 	{0, 0, 1, 0,		/* 0x40 */
516 		{{6, 6},
517 		{0, 0},
518 		{0, 0},
519 		{0, 0}
520 		}
521 	},
522 	{1, 0, 2, 0,		/* 0x41 */
523 		{{0, 0},
524 		{6, 6},
525 		{0, 0},
526 		{0, 0}
527 		}
528 	},
529 	{0, 0, 2, 0,		/* 0x42 */
530 		{{1, 1},
531 		{6, 6},
532 		{0, 0},
533 		{0, 0}
534 		}
535 	},
536 	{1, 0, 2, 0,		/* 0x43 */
537 		{{0, 1},
538 		{6, 6},
539 		{0, 0},
540 		{0, 0}
541 		}
542 	},
543 	{0, 0, 2, 0,		/* 0x44 */
544 		{{2, 2},
545 		{6, 6},
546 		{0, 0},
547 		{0, 0}
548 		}
549 	},
550 	{1, 0, 3, 0,		/* 0x45 */
551 		{{0, 0},
552 		{2, 2},
553 		{6, 6},
554 		{0, 0}
555 		}
556 	},
557 	{0, 0, 2, 0,		/* 0x46 */
558 		{{1, 2},
559 		{6, 6},
560 		{0, 0},
561 		{0, 0}
562 		}
563 	},
564 	{1, 0, 2, 0,		/* 0x47 */
565 		{{0, 2},
566 		{6, 6},
567 		{0, 0},
568 		{0, 0}
569 		}
570 	},
571 	{0, 0, 2, 0,		/* 0x48 */
572 		{{3, 3},
573 		{6, 6},
574 		{0, 0},
575 		{0, 0}
576 		}
577 	},
578 	{1, 0, 3, 0,		/* 0x49 */
579 		{{0, 0},
580 		{3, 3},
581 		{6, 6},
582 		{0, 0}
583 		}
584 	},
585 	{0, 0, 3, 0,		/* 0x4a */
586 		{{1, 1},
587 		{3, 3},
588 		{6, 6},
589 		{0, 0}
590 		}
591 	},
592 	{1, 0, 3, 0,		/* 0x4b */
593 		{{0, 1},
594 		{3, 3},
595 		{6, 6},
596 		{0, 0}
597 		}
598 	},
599 	{0, 0, 2, 0,		/* 0x4c */
600 		{{2, 3},
601 		{6, 6},
602 		{0, 0},
603 		{0, 0}
604 		}
605 	},
606 	{1, 0, 3, 0,		/* 0x4d */
607 		{{0, 0},
608 		{2, 3},
609 		{6, 6},
610 		{0, 0}
611 		}
612 	},
613 	{0, 0, 2, 0,		/* 0x4e */
614 		{{1, 3},
615 		{6, 6},
616 		{0, 0},
617 		{0, 0}
618 		}
619 	},
620 	{1, 0, 2, 0,		/* 0x4f */
621 		{{0, 3},
622 		{6, 6},
623 		{0, 0},
624 		{0, 0}
625 		}
626 	},
627 	{0, 0, 2, 0,		/* 0x50 */
628 		{{4, 4},
629 		{6, 6},
630 		{0, 0},
631 		{0, 0}
632 		}
633 	},
634 	{1, 0, 3, 0,		/* 0x51 */
635 		{{0, 0},
636 		{4, 4},
637 		{6, 6},
638 		{0, 0}
639 		}
640 	},
641 	{0, 0, 3, 0,		/* 0x52 */
642 		{{1, 1},
643 		{4, 4},
644 		{6, 6},
645 		{0, 0}
646 		}
647 	},
648 	{1, 0, 3, 0,		/* 0x53 */
649 		{{0, 1},
650 		{4, 4},
651 		{6, 6},
652 		{0, 0}
653 		}
654 	},
655 	{0, 0, 3, 0,		/* 0x54 */
656 		{{2, 2},
657 		{4, 4},
658 		{6, 6},
659 		{0, 0}
660 		}
661 	},
662 	{1, 0, 4, 0,		/* 0x55 */
663 		{{0, 0},
664 		{2, 2},
665 		{4, 4},
666 		{6, 6}
667 		}
668 	},
669 	{0, 0, 3, 0,		/* 0x56 */
670 		{{1, 2},
671 		{4, 4},
672 		{6, 6},
673 		{0, 0}
674 		}
675 	},
676 	{1, 0, 3, 0,		/* 0x57 */
677 		{{0, 2},
678 		{4, 4},
679 		{6, 6},
680 		{0, 0}
681 		}
682 	},
683 	{0, 0, 2, 0,		/* 0x58 */
684 		{{3, 4},
685 		{6, 6},
686 		{0, 0},
687 		{0, 0}
688 		}
689 	},
690 	{1, 0, 3, 0,		/* 0x59 */
691 		{{0, 0},
692 		{3, 4},
693 		{6, 6},
694 		{0, 0}
695 		}
696 	},
697 	{0, 0, 3, 0,		/* 0x5a */
698 		{{1, 1},
699 		{3, 4},
700 		{6, 6},
701 		{0, 0}
702 		}
703 	},
704 	{1, 0, 3, 0,		/* 0x5b */
705 		{{0, 1},
706 		{3, 4},
707 		{6, 6},
708 		{0, 0}
709 		}
710 	},
711 	{0, 0, 2, 0,		/* 0x5c */
712 		{{2, 4},
713 		{6, 6},
714 		{0, 0},
715 		{0, 0}
716 		}
717 	},
718 	{1, 0, 3, 0,		/* 0x5d */
719 		{{0, 0},
720 		{2, 4},
721 		{6, 6},
722 		{0, 0}
723 		}
724 	},
725 	{0, 0, 2, 0,		/* 0x5e */
726 		{{1, 4},
727 		{6, 6},
728 		{0, 0},
729 		{0, 0}
730 		}
731 	},
732 	{1, 0, 2, 0,		/* 0x5f */
733 		{{0, 4},
734 		{6, 6},
735 		{0, 0},
736 		{0, 0}
737 		}
738 	},
739 	{0, 0, 1, 0,		/* 0x60 */
740 		{{5, 6},
741 		{0, 0},
742 		{0, 0},
743 		{0, 0}
744 		}
745 	},
746 	{1, 0, 2, 0,		/* 0x61 */
747 		{{0, 0},
748 		{5, 6},
749 		{0, 0},
750 		{0, 0}
751 		}
752 	},
753 	{0, 0, 2, 0,		/* 0x62 */
754 		{{1, 1},
755 		{5, 6},
756 		{0, 0},
757 		{0, 0}
758 		}
759 	},
760 	{1, 0, 2, 0,		/* 0x63 */
761 		{{0, 1},
762 		{5, 6},
763 		{0, 0},
764 		{0, 0}
765 		}
766 	},
767 	{0, 0, 2, 0,		/* 0x64 */
768 		{{2, 2},
769 		{5, 6},
770 		{0, 0},
771 		{0, 0}
772 		}
773 	},
774 	{1, 0, 3, 0,		/* 0x65 */
775 		{{0, 0},
776 		{2, 2},
777 		{5, 6},
778 		{0, 0}
779 		}
780 	},
781 	{0, 0, 2, 0,		/* 0x66 */
782 		{{1, 2},
783 		{5, 6},
784 		{0, 0},
785 		{0, 0}
786 		}
787 	},
788 	{1, 0, 2, 0,		/* 0x67 */
789 		{{0, 2},
790 		{5, 6},
791 		{0, 0},
792 		{0, 0}
793 		}
794 	},
795 	{0, 0, 2, 0,		/* 0x68 */
796 		{{3, 3},
797 		{5, 6},
798 		{0, 0},
799 		{0, 0}
800 		}
801 	},
802 	{1, 0, 3, 0,		/* 0x69 */
803 		{{0, 0},
804 		{3, 3},
805 		{5, 6},
806 		{0, 0}
807 		}
808 	},
809 	{0, 0, 3, 0,		/* 0x6a */
810 		{{1, 1},
811 		{3, 3},
812 		{5, 6},
813 		{0, 0}
814 		}
815 	},
816 	{1, 0, 3, 0,		/* 0x6b */
817 		{{0, 1},
818 		{3, 3},
819 		{5, 6},
820 		{0, 0}
821 		}
822 	},
823 	{0, 0, 2, 0,		/* 0x6c */
824 		{{2, 3},
825 		{5, 6},
826 		{0, 0},
827 		{0, 0}
828 		}
829 	},
830 	{1, 0, 3, 0,		/* 0x6d */
831 		{{0, 0},
832 		{2, 3},
833 		{5, 6},
834 		{0, 0}
835 		}
836 	},
837 	{0, 0, 2, 0,		/* 0x6e */
838 		{{1, 3},
839 		{5, 6},
840 		{0, 0},
841 		{0, 0}
842 		}
843 	},
844 	{1, 0, 2, 0,		/* 0x6f */
845 		{{0, 3},
846 		{5, 6},
847 		{0, 0},
848 		{0, 0}
849 		}
850 	},
851 	{0, 0, 1, 0,		/* 0x70 */
852 		{{4, 6},
853 		{0, 0},
854 		{0, 0},
855 		{0, 0}
856 		}
857 	},
858 	{1, 0, 2, 0,		/* 0x71 */
859 		{{0, 0},
860 		{4, 6},
861 		{0, 0},
862 		{0, 0}
863 		}
864 	},
865 	{0, 0, 2, 0,		/* 0x72 */
866 		{{1, 1},
867 		{4, 6},
868 		{0, 0},
869 		{0, 0}
870 		}
871 	},
872 	{1, 0, 2, 0,		/* 0x73 */
873 		{{0, 1},
874 		{4, 6},
875 		{0, 0},
876 		{0, 0}
877 		}
878 	},
879 	{0, 0, 2, 0,		/* 0x74 */
880 		{{2, 2},
881 		{4, 6},
882 		{0, 0},
883 		{0, 0}
884 		}
885 	},
886 	{1, 0, 3, 0,		/* 0x75 */
887 		{{0, 0},
888 		{2, 2},
889 		{4, 6},
890 		{0, 0}
891 		}
892 	},
893 	{0, 0, 2, 0,		/* 0x76 */
894 		{{1, 2},
895 		{4, 6},
896 		{0, 0},
897 		{0, 0}
898 		}
899 	},
900 	{1, 0, 2, 0,		/* 0x77 */
901 		{{0, 2},
902 		{4, 6},
903 		{0, 0},
904 		{0, 0}
905 		}
906 	},
907 	{0, 0, 1, 0,		/* 0x78 */
908 		{{3, 6},
909 		{0, 0},
910 		{0, 0},
911 		{0, 0}
912 		}
913 	},
914 	{1, 0, 2, 0,		/* 0x79 */
915 		{{0, 0},
916 		{3, 6},
917 		{0, 0},
918 		{0, 0}
919 		}
920 	},
921 	{0, 0, 2, 0,		/* 0x7a */
922 		{{1, 1},
923 		{3, 6},
924 		{0, 0},
925 		{0, 0}
926 		}
927 	},
928 	{1, 0, 2, 0,		/* 0x7b */
929 		{{0, 1},
930 		{3, 6},
931 		{0, 0},
932 		{0, 0}
933 		}
934 	},
935 	{0, 0, 1, 0,		/* 0x7c */
936 		{{2, 6},
937 		{0, 0},
938 		{0, 0},
939 		{0, 0}
940 		}
941 	},
942 	{1, 0, 2, 0,		/* 0x7d */
943 		{{0, 0},
944 		{2, 6},
945 		{0, 0},
946 		{0, 0}
947 		}
948 	},
949 	{0, 0, 1, 0,		/* 0x7e */
950 		{{1, 6},
951 		{0, 0},
952 		{0, 0},
953 		{0, 0}
954 		}
955 	},
956 	{1, 0, 1, 0,		/* 0x7f */
957 		{{0, 6},
958 		{0, 0},
959 		{0, 0},
960 		{0, 0}
961 		}
962 	},
963 	{0, 1, 1, 0,		/* 0x80 */
964 		{{7, 7},
965 		{0, 0},
966 		{0, 0},
967 		{0, 0}
968 		}
969 	},
970 	{1, 1, 2, 0,		/* 0x81 */
971 		{{0, 0},
972 		{7, 7},
973 		{0, 0},
974 		{0, 0}
975 		}
976 	},
977 	{0, 1, 2, 0,		/* 0x82 */
978 		{{1, 1},
979 		{7, 7},
980 		{0, 0},
981 		{0, 0}
982 		}
983 	},
984 	{1, 1, 2, 0,		/* 0x83 */
985 		{{0, 1},
986 		{7, 7},
987 		{0, 0},
988 		{0, 0}
989 		}
990 	},
991 	{0, 1, 2, 0,		/* 0x84 */
992 		{{2, 2},
993 		{7, 7},
994 		{0, 0},
995 		{0, 0}
996 		}
997 	},
998 	{1, 1, 3, 0,		/* 0x85 */
999 		{{0, 0},
1000 		{2, 2},
1001 		{7, 7},
1002 		{0, 0}
1003 		}
1004 	},
1005 	{0, 1, 2, 0,		/* 0x86 */
1006 		{{1, 2},
1007 		{7, 7},
1008 		{0, 0},
1009 		{0, 0}
1010 		}
1011 	},
1012 	{1, 1, 2, 0,		/* 0x87 */
1013 		{{0, 2},
1014 		{7, 7},
1015 		{0, 0},
1016 		{0, 0}
1017 		}
1018 	},
1019 	{0, 1, 2, 0,		/* 0x88 */
1020 		{{3, 3},
1021 		{7, 7},
1022 		{0, 0},
1023 		{0, 0}
1024 		}
1025 	},
1026 	{1, 1, 3, 0,		/* 0x89 */
1027 		{{0, 0},
1028 		{3, 3},
1029 		{7, 7},
1030 		{0, 0}
1031 		}
1032 	},
1033 	{0, 1, 3, 0,		/* 0x8a */
1034 		{{1, 1},
1035 		{3, 3},
1036 		{7, 7},
1037 		{0, 0}
1038 		}
1039 	},
1040 	{1, 1, 3, 0,		/* 0x8b */
1041 		{{0, 1},
1042 		{3, 3},
1043 		{7, 7},
1044 		{0, 0}
1045 		}
1046 	},
1047 	{0, 1, 2, 0,		/* 0x8c */
1048 		{{2, 3},
1049 		{7, 7},
1050 		{0, 0},
1051 		{0, 0}
1052 		}
1053 	},
1054 	{1, 1, 3, 0,		/* 0x8d */
1055 		{{0, 0},
1056 		{2, 3},
1057 		{7, 7},
1058 		{0, 0}
1059 		}
1060 	},
1061 	{0, 1, 2, 0,		/* 0x8e */
1062 		{{1, 3},
1063 		{7, 7},
1064 		{0, 0},
1065 		{0, 0}
1066 		}
1067 	},
1068 	{1, 1, 2, 0,		/* 0x8f */
1069 		{{0, 3},
1070 		{7, 7},
1071 		{0, 0},
1072 		{0, 0}
1073 		}
1074 	},
1075 	{0, 1, 2, 0,		/* 0x90 */
1076 		{{4, 4},
1077 		{7, 7},
1078 		{0, 0},
1079 		{0, 0}
1080 		}
1081 	},
1082 	{1, 1, 3, 0,		/* 0x91 */
1083 		{{0, 0},
1084 		{4, 4},
1085 		{7, 7},
1086 		{0, 0}
1087 		}
1088 	},
1089 	{0, 1, 3, 0,		/* 0x92 */
1090 		{{1, 1},
1091 		{4, 4},
1092 		{7, 7},
1093 		{0, 0}
1094 		}
1095 	},
1096 	{1, 1, 3, 0,		/* 0x93 */
1097 		{{0, 1},
1098 		{4, 4},
1099 		{7, 7},
1100 		{0, 0}
1101 		}
1102 	},
1103 	{0, 1, 3, 0,		/* 0x94 */
1104 		{{2, 2},
1105 		{4, 4},
1106 		{7, 7},
1107 		{0, 0}
1108 		}
1109 	},
1110 	{1, 1, 4, 0,		/* 0x95 */
1111 		{{0, 0},
1112 		{2, 2},
1113 		{4, 4},
1114 		{7, 7}
1115 		}
1116 	},
1117 	{0, 1, 3, 0,		/* 0x96 */
1118 		{{1, 2},
1119 		{4, 4},
1120 		{7, 7},
1121 		{0, 0}
1122 		}
1123 	},
1124 	{1, 1, 3, 0,		/* 0x97 */
1125 		{{0, 2},
1126 		{4, 4},
1127 		{7, 7},
1128 		{0, 0}
1129 		}
1130 	},
1131 	{0, 1, 2, 0,		/* 0x98 */
1132 		{{3, 4},
1133 		{7, 7},
1134 		{0, 0},
1135 		{0, 0}
1136 		}
1137 	},
1138 	{1, 1, 3, 0,		/* 0x99 */
1139 		{{0, 0},
1140 		{3, 4},
1141 		{7, 7},
1142 		{0, 0}
1143 		}
1144 	},
1145 	{0, 1, 3, 0,		/* 0x9a */
1146 		{{1, 1},
1147 		{3, 4},
1148 		{7, 7},
1149 		{0, 0}
1150 		}
1151 	},
1152 	{1, 1, 3, 0,		/* 0x9b */
1153 		{{0, 1},
1154 		{3, 4},
1155 		{7, 7},
1156 		{0, 0}
1157 		}
1158 	},
1159 	{0, 1, 2, 0,		/* 0x9c */
1160 		{{2, 4},
1161 		{7, 7},
1162 		{0, 0},
1163 		{0, 0}
1164 		}
1165 	},
1166 	{1, 1, 3, 0,		/* 0x9d */
1167 		{{0, 0},
1168 		{2, 4},
1169 		{7, 7},
1170 		{0, 0}
1171 		}
1172 	},
1173 	{0, 1, 2, 0,		/* 0x9e */
1174 		{{1, 4},
1175 		{7, 7},
1176 		{0, 0},
1177 		{0, 0}
1178 		}
1179 	},
1180 	{1, 1, 2, 0,		/* 0x9f */
1181 		{{0, 4},
1182 		{7, 7},
1183 		{0, 0},
1184 		{0, 0}
1185 		}
1186 	},
1187 	{0, 1, 2, 0,		/* 0xa0 */
1188 		{{5, 5},
1189 		{7, 7},
1190 		{0, 0},
1191 		{0, 0}
1192 		}
1193 	},
1194 	{1, 1, 3, 0,		/* 0xa1 */
1195 		{{0, 0},
1196 		{5, 5},
1197 		{7, 7},
1198 		{0, 0}
1199 		}
1200 	},
1201 	{0, 1, 3, 0,		/* 0xa2 */
1202 		{{1, 1},
1203 		{5, 5},
1204 		{7, 7},
1205 		{0, 0}
1206 		}
1207 	},
1208 	{1, 1, 3, 0,		/* 0xa3 */
1209 		{{0, 1},
1210 		{5, 5},
1211 		{7, 7},
1212 		{0, 0}
1213 		}
1214 	},
1215 	{0, 1, 3, 0,		/* 0xa4 */
1216 		{{2, 2},
1217 		{5, 5},
1218 		{7, 7},
1219 		{0, 0}
1220 		}
1221 	},
1222 	{1, 1, 4, 0,		/* 0xa5 */
1223 		{{0, 0},
1224 		{2, 2},
1225 		{5, 5},
1226 		{7, 7}
1227 		}
1228 	},
1229 	{0, 1, 3, 0,		/* 0xa6 */
1230 		{{1, 2},
1231 		{5, 5},
1232 		{7, 7},
1233 		{0, 0}
1234 		}
1235 	},
1236 	{1, 1, 3, 0,		/* 0xa7 */
1237 		{{0, 2},
1238 		{5, 5},
1239 		{7, 7},
1240 		{0, 0}
1241 		}
1242 	},
1243 	{0, 1, 3, 0,		/* 0xa8 */
1244 		{{3, 3},
1245 		{5, 5},
1246 		{7, 7},
1247 		{0, 0}
1248 		}
1249 	},
1250 	{1, 1, 4, 0,		/* 0xa9 */
1251 		{{0, 0},
1252 		{3, 3},
1253 		{5, 5},
1254 		{7, 7}
1255 		}
1256 	},
1257 	{0, 1, 4, 0,		/* 0xaa */
1258 		{{1, 1},
1259 		{3, 3},
1260 		{5, 5},
1261 		{7, 7}
1262 		}
1263 	},
1264 	{1, 1, 4, 0,		/* 0xab */
1265 		{{0, 1},
1266 		{3, 3},
1267 		{5, 5},
1268 		{7, 7}
1269 		}
1270 	},
1271 	{0, 1, 3, 0,		/* 0xac */
1272 		{{2, 3},
1273 		{5, 5},
1274 		{7, 7},
1275 		{0, 0}
1276 		}
1277 	},
1278 	{1, 1, 4, 0,		/* 0xad */
1279 		{{0, 0},
1280 		{2, 3},
1281 		{5, 5},
1282 		{7, 7}
1283 		}
1284 	},
1285 	{0, 1, 3, 0,		/* 0xae */
1286 		{{1, 3},
1287 		{5, 5},
1288 		{7, 7},
1289 		{0, 0}
1290 		}
1291 	},
1292 	{1, 1, 3, 0,		/* 0xaf */
1293 		{{0, 3},
1294 		{5, 5},
1295 		{7, 7},
1296 		{0, 0}
1297 		}
1298 	},
1299 	{0, 1, 2, 0,		/* 0xb0 */
1300 		{{4, 5},
1301 		{7, 7},
1302 		{0, 0},
1303 		{0, 0}
1304 		}
1305 	},
1306 	{1, 1, 3, 0,		/* 0xb1 */
1307 		{{0, 0},
1308 		{4, 5},
1309 		{7, 7},
1310 		{0, 0}
1311 		}
1312 	},
1313 	{0, 1, 3, 0,		/* 0xb2 */
1314 		{{1, 1},
1315 		{4, 5},
1316 		{7, 7},
1317 		{0, 0}
1318 		}
1319 	},
1320 	{1, 1, 3, 0,		/* 0xb3 */
1321 		{{0, 1},
1322 		{4, 5},
1323 		{7, 7},
1324 		{0, 0}
1325 		}
1326 	},
1327 	{0, 1, 3, 0,		/* 0xb4 */
1328 		{{2, 2},
1329 		{4, 5},
1330 		{7, 7},
1331 		{0, 0}
1332 		}
1333 	},
1334 	{1, 1, 4, 0,		/* 0xb5 */
1335 		{{0, 0},
1336 		{2, 2},
1337 		{4, 5},
1338 		{7, 7}
1339 		}
1340 	},
1341 	{0, 1, 3, 0,		/* 0xb6 */
1342 		{{1, 2},
1343 		{4, 5},
1344 		{7, 7},
1345 		{0, 0}
1346 		}
1347 	},
1348 	{1, 1, 3, 0,		/* 0xb7 */
1349 		{{0, 2},
1350 		{4, 5},
1351 		{7, 7},
1352 		{0, 0}
1353 		}
1354 	},
1355 	{0, 1, 2, 0,		/* 0xb8 */
1356 		{{3, 5},
1357 		{7, 7},
1358 		{0, 0},
1359 		{0, 0}
1360 		}
1361 	},
1362 	{1, 1, 3, 0,		/* 0xb9 */
1363 		{{0, 0},
1364 		{3, 5},
1365 		{7, 7},
1366 		{0, 0}
1367 		}
1368 	},
1369 	{0, 1, 3, 0,		/* 0xba */
1370 		{{1, 1},
1371 		{3, 5},
1372 		{7, 7},
1373 		{0, 0}
1374 		}
1375 	},
1376 	{1, 1, 3, 0,		/* 0xbb */
1377 		{{0, 1},
1378 		{3, 5},
1379 		{7, 7},
1380 		{0, 0}
1381 		}
1382 	},
1383 	{0, 1, 2, 0,		/* 0xbc */
1384 		{{2, 5},
1385 		{7, 7},
1386 		{0, 0},
1387 		{0, 0}
1388 		}
1389 	},
1390 	{1, 1, 3, 0,		/* 0xbd */
1391 		{{0, 0},
1392 		{2, 5},
1393 		{7, 7},
1394 		{0, 0}
1395 		}
1396 	},
1397 	{0, 1, 2, 0,		/* 0xbe */
1398 		{{1, 5},
1399 		{7, 7},
1400 		{0, 0},
1401 		{0, 0}
1402 		}
1403 	},
1404 	{1, 1, 2, 0,		/* 0xbf */
1405 		{{0, 5},
1406 		{7, 7},
1407 		{0, 0},
1408 		{0, 0}
1409 		}
1410 	},
1411 	{0, 1, 1, 0,		/* 0xc0 */
1412 		{{6, 7},
1413 		{0, 0},
1414 		{0, 0},
1415 		{0, 0}
1416 		}
1417 	},
1418 	{1, 1, 2, 0,		/* 0xc1 */
1419 		{{0, 0},
1420 		{6, 7},
1421 		{0, 0},
1422 		{0, 0}
1423 		}
1424 	},
1425 	{0, 1, 2, 0,		/* 0xc2 */
1426 		{{1, 1},
1427 		{6, 7},
1428 		{0, 0},
1429 		{0, 0}
1430 		}
1431 	},
1432 	{1, 1, 2, 0,		/* 0xc3 */
1433 		{{0, 1},
1434 		{6, 7},
1435 		{0, 0},
1436 		{0, 0}
1437 		}
1438 	},
1439 	{0, 1, 2, 0,		/* 0xc4 */
1440 		{{2, 2},
1441 		{6, 7},
1442 		{0, 0},
1443 		{0, 0}
1444 		}
1445 	},
1446 	{1, 1, 3, 0,		/* 0xc5 */
1447 		{{0, 0},
1448 		{2, 2},
1449 		{6, 7},
1450 		{0, 0}
1451 		}
1452 	},
1453 	{0, 1, 2, 0,		/* 0xc6 */
1454 		{{1, 2},
1455 		{6, 7},
1456 		{0, 0},
1457 		{0, 0}
1458 		}
1459 	},
1460 	{1, 1, 2, 0,		/* 0xc7 */
1461 		{{0, 2},
1462 		{6, 7},
1463 		{0, 0},
1464 		{0, 0}
1465 		}
1466 	},
1467 	{0, 1, 2, 0,		/* 0xc8 */
1468 		{{3, 3},
1469 		{6, 7},
1470 		{0, 0},
1471 		{0, 0}
1472 		}
1473 	},
1474 	{1, 1, 3, 0,		/* 0xc9 */
1475 		{{0, 0},
1476 		{3, 3},
1477 		{6, 7},
1478 		{0, 0}
1479 		}
1480 	},
1481 	{0, 1, 3, 0,		/* 0xca */
1482 		{{1, 1},
1483 		{3, 3},
1484 		{6, 7},
1485 		{0, 0}
1486 		}
1487 	},
1488 	{1, 1, 3, 0,		/* 0xcb */
1489 		{{0, 1},
1490 		{3, 3},
1491 		{6, 7},
1492 		{0, 0}
1493 		}
1494 	},
1495 	{0, 1, 2, 0,		/* 0xcc */
1496 		{{2, 3},
1497 		{6, 7},
1498 		{0, 0},
1499 		{0, 0}
1500 		}
1501 	},
1502 	{1, 1, 3, 0,		/* 0xcd */
1503 		{{0, 0},
1504 		{2, 3},
1505 		{6, 7},
1506 		{0, 0}
1507 		}
1508 	},
1509 	{0, 1, 2, 0,		/* 0xce */
1510 		{{1, 3},
1511 		{6, 7},
1512 		{0, 0},
1513 		{0, 0}
1514 		}
1515 	},
1516 	{1, 1, 2, 0,		/* 0xcf */
1517 		{{0, 3},
1518 		{6, 7},
1519 		{0, 0},
1520 		{0, 0}
1521 		}
1522 	},
1523 	{0, 1, 2, 0,		/* 0xd0 */
1524 		{{4, 4},
1525 		{6, 7},
1526 		{0, 0},
1527 		{0, 0}
1528 		}
1529 	},
1530 	{1, 1, 3, 0,		/* 0xd1 */
1531 		{{0, 0},
1532 		{4, 4},
1533 		{6, 7},
1534 		{0, 0}
1535 		}
1536 	},
1537 	{0, 1, 3, 0,		/* 0xd2 */
1538 		{{1, 1},
1539 		{4, 4},
1540 		{6, 7},
1541 		{0, 0}
1542 		}
1543 	},
1544 	{1, 1, 3, 0,		/* 0xd3 */
1545 		{{0, 1},
1546 		{4, 4},
1547 		{6, 7},
1548 		{0, 0}
1549 		}
1550 	},
1551 	{0, 1, 3, 0,		/* 0xd4 */
1552 		{{2, 2},
1553 		{4, 4},
1554 		{6, 7},
1555 		{0, 0}
1556 		}
1557 	},
1558 	{1, 1, 4, 0,		/* 0xd5 */
1559 		{{0, 0},
1560 		{2, 2},
1561 		{4, 4},
1562 		{6, 7}
1563 		}
1564 	},
1565 	{0, 1, 3, 0,		/* 0xd6 */
1566 		{{1, 2},
1567 		{4, 4},
1568 		{6, 7},
1569 		{0, 0}
1570 		}
1571 	},
1572 	{1, 1, 3, 0,		/* 0xd7 */
1573 		{{0, 2},
1574 		{4, 4},
1575 		{6, 7},
1576 		{0, 0}
1577 		}
1578 	},
1579 	{0, 1, 2, 0,		/* 0xd8 */
1580 		{{3, 4},
1581 		{6, 7},
1582 		{0, 0},
1583 		{0, 0}
1584 		}
1585 	},
1586 	{1, 1, 3, 0,		/* 0xd9 */
1587 		{{0, 0},
1588 		{3, 4},
1589 		{6, 7},
1590 		{0, 0}
1591 		}
1592 	},
1593 	{0, 1, 3, 0,		/* 0xda */
1594 		{{1, 1},
1595 		{3, 4},
1596 		{6, 7},
1597 		{0, 0}
1598 		}
1599 	},
1600 	{1, 1, 3, 0,		/* 0xdb */
1601 		{{0, 1},
1602 		{3, 4},
1603 		{6, 7},
1604 		{0, 0}
1605 		}
1606 	},
1607 	{0, 1, 2, 0,		/* 0xdc */
1608 		{{2, 4},
1609 		{6, 7},
1610 		{0, 0},
1611 		{0, 0}
1612 		}
1613 	},
1614 	{1, 1, 3, 0,		/* 0xdd */
1615 		{{0, 0},
1616 		{2, 4},
1617 		{6, 7},
1618 		{0, 0}
1619 		}
1620 	},
1621 	{0, 1, 2, 0,		/* 0xde */
1622 		{{1, 4},
1623 		{6, 7},
1624 		{0, 0},
1625 		{0, 0}
1626 		}
1627 	},
1628 	{1, 1, 2, 0,		/* 0xdf */
1629 		{{0, 4},
1630 		{6, 7},
1631 		{0, 0},
1632 		{0, 0}
1633 		}
1634 	},
1635 	{0, 1, 1, 0,		/* 0xe0 */
1636 		{{5, 7},
1637 		{0, 0},
1638 		{0, 0},
1639 		{0, 0}
1640 		}
1641 	},
1642 	{1, 1, 2, 0,		/* 0xe1 */
1643 		{{0, 0},
1644 		{5, 7},
1645 		{0, 0},
1646 		{0, 0}
1647 		}
1648 	},
1649 	{0, 1, 2, 0,		/* 0xe2 */
1650 		{{1, 1},
1651 		{5, 7},
1652 		{0, 0},
1653 		{0, 0}
1654 		}
1655 	},
1656 	{1, 1, 2, 0,		/* 0xe3 */
1657 		{{0, 1},
1658 		{5, 7},
1659 		{0, 0},
1660 		{0, 0}
1661 		}
1662 	},
1663 	{0, 1, 2, 0,		/* 0xe4 */
1664 		{{2, 2},
1665 		{5, 7},
1666 		{0, 0},
1667 		{0, 0}
1668 		}
1669 	},
1670 	{1, 1, 3, 0,		/* 0xe5 */
1671 		{{0, 0},
1672 		{2, 2},
1673 		{5, 7},
1674 		{0, 0}
1675 		}
1676 	},
1677 	{0, 1, 2, 0,		/* 0xe6 */
1678 		{{1, 2},
1679 		{5, 7},
1680 		{0, 0},
1681 		{0, 0}
1682 		}
1683 	},
1684 	{1, 1, 2, 0,		/* 0xe7 */
1685 		{{0, 2},
1686 		{5, 7},
1687 		{0, 0},
1688 		{0, 0}
1689 		}
1690 	},
1691 	{0, 1, 2, 0,		/* 0xe8 */
1692 		{{3, 3},
1693 		{5, 7},
1694 		{0, 0},
1695 		{0, 0}
1696 		}
1697 	},
1698 	{1, 1, 3, 0,		/* 0xe9 */
1699 		{{0, 0},
1700 		{3, 3},
1701 		{5, 7},
1702 		{0, 0}
1703 		}
1704 	},
1705 	{0, 1, 3, 0,		/* 0xea */
1706 		{{1, 1},
1707 		{3, 3},
1708 		{5, 7},
1709 		{0, 0}
1710 		}
1711 	},
1712 	{1, 1, 3, 0,		/* 0xeb */
1713 		{{0, 1},
1714 		{3, 3},
1715 		{5, 7},
1716 		{0, 0}
1717 		}
1718 	},
1719 	{0, 1, 2, 0,		/* 0xec */
1720 		{{2, 3},
1721 		{5, 7},
1722 		{0, 0},
1723 		{0, 0}
1724 		}
1725 	},
1726 	{1, 1, 3, 0,		/* 0xed */
1727 		{{0, 0},
1728 		{2, 3},
1729 		{5, 7},
1730 		{0, 0}
1731 		}
1732 	},
1733 	{0, 1, 2, 0,		/* 0xee */
1734 		{{1, 3},
1735 		{5, 7},
1736 		{0, 0},
1737 		{0, 0}
1738 		}
1739 	},
1740 	{1, 1, 2, 0,		/* 0xef */
1741 		{{0, 3},
1742 		{5, 7},
1743 		{0, 0},
1744 		{0, 0}
1745 		}
1746 	},
1747 	{0, 1, 1, 0,		/* 0xf0 */
1748 		{{4, 7},
1749 		{0, 0},
1750 		{0, 0},
1751 		{0, 0}
1752 		}
1753 	},
1754 	{1, 1, 2, 0,		/* 0xf1 */
1755 		{{0, 0},
1756 		{4, 7},
1757 		{0, 0},
1758 		{0, 0}
1759 		}
1760 	},
1761 	{0, 1, 2, 0,		/* 0xf2 */
1762 		{{1, 1},
1763 		{4, 7},
1764 		{0, 0},
1765 		{0, 0}
1766 		}
1767 	},
1768 	{1, 1, 2, 0,		/* 0xf3 */
1769 		{{0, 1},
1770 		{4, 7},
1771 		{0, 0},
1772 		{0, 0}
1773 		}
1774 	},
1775 	{0, 1, 2, 0,		/* 0xf4 */
1776 		{{2, 2},
1777 		{4, 7},
1778 		{0, 0},
1779 		{0, 0}
1780 		}
1781 	},
1782 	{1, 1, 3, 0,		/* 0xf5 */
1783 		{{0, 0},
1784 		{2, 2},
1785 		{4, 7},
1786 		{0, 0}
1787 		}
1788 	},
1789 	{0, 1, 2, 0,		/* 0xf6 */
1790 		{{1, 2},
1791 		{4, 7},
1792 		{0, 0},
1793 		{0, 0}
1794 		}
1795 	},
1796 	{1, 1, 2, 0,		/* 0xf7 */
1797 		{{0, 2},
1798 		{4, 7},
1799 		{0, 0},
1800 		{0, 0}
1801 		}
1802 	},
1803 	{0, 1, 1, 0,		/* 0xf8 */
1804 		{{3, 7},
1805 		{0, 0},
1806 		{0, 0},
1807 		{0, 0}
1808 		}
1809 	},
1810 	{1, 1, 2, 0,		/* 0xf9 */
1811 		{{0, 0},
1812 		{3, 7},
1813 		{0, 0},
1814 		{0, 0}
1815 		}
1816 	},
1817 	{0, 1, 2, 0,		/* 0xfa */
1818 		{{1, 1},
1819 		{3, 7},
1820 		{0, 0},
1821 		{0, 0}
1822 		}
1823 	},
1824 	{1, 1, 2, 0,		/* 0xfb */
1825 		{{0, 1},
1826 		{3, 7},
1827 		{0, 0},
1828 		{0, 0}
1829 		}
1830 	},
1831 	{0, 1, 1, 0,		/* 0xfc */
1832 		{{2, 7},
1833 		{0, 0},
1834 		{0, 0},
1835 		{0, 0}
1836 		}
1837 	},
1838 	{1, 1, 2, 0,		/* 0xfd */
1839 		{{0, 0},
1840 		{2, 7},
1841 		{0, 0},
1842 		{0, 0}
1843 		}
1844 	},
1845 	{0, 1, 1, 0,		/* 0xfe */
1846 		{{1, 7},
1847 		{0, 0},
1848 		{0, 0},
1849 		{0, 0}
1850 		}
1851 	},
1852 	{1, 1, 1, 0,		/* 0xff */
1853 		{{0, 7},
1854 		{0, 0},
1855 		{0, 0},
1856 		{0, 0}
1857 		}
1858 	}
1859 };
1860 
1861 
1862 int
1863 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1864     int ipv4_addr_legal,
1865     int ipv6_addr_legal,
1866     int loopback_scope,
1867     int ipv4_local_scope,
1868     int local_scope,
1869     int site_scope,
1870     int do_update)
1871 {
1872 	if ((loopback_scope == 0) &&
1873 	    (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1874 		/*
1875 		 * skip loopback if not in scope *
1876 		 */
1877 		return (0);
1878 	}
1879 	switch (ifa->address.sa.sa_family) {
1880 	case AF_INET:
1881 		if (ipv4_addr_legal) {
1882 			struct sockaddr_in *sin;
1883 
1884 			sin = (struct sockaddr_in *)&ifa->address.sin;
1885 			if (sin->sin_addr.s_addr == 0) {
1886 				/* not in scope , unspecified */
1887 				return (0);
1888 			}
1889 			if ((ipv4_local_scope == 0) &&
1890 			    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1891 				/* private address not in scope */
1892 				return (0);
1893 			}
1894 		} else {
1895 			return (0);
1896 		}
1897 		break;
1898 #ifdef INET6
1899 	case AF_INET6:
1900 		if (ipv6_addr_legal) {
1901 			struct sockaddr_in6 *sin6;
1902 
1903 			/*
1904 			 * Must update the flags,  bummer, which means any
1905 			 * IFA locks must now be applied HERE <->
1906 			 */
1907 			if (do_update) {
1908 				sctp_gather_internal_ifa_flags(ifa);
1909 			}
1910 			if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1911 				return (0);
1912 			}
1913 			/* ok to use deprecated addresses? */
1914 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1915 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1916 				/* skip unspecifed addresses */
1917 				return (0);
1918 			}
1919 			if (	/* (local_scope == 0) && */
1920 			    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1921 				return (0);
1922 			}
1923 			if ((site_scope == 0) &&
1924 			    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1925 				return (0);
1926 			}
1927 		} else {
1928 			return (0);
1929 		}
1930 		break;
1931 #endif
1932 	default:
1933 		return (0);
1934 	}
1935 	return (1);
1936 }
1937 
1938 static struct mbuf *
1939 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1940 {
1941 	struct sctp_paramhdr *parmh;
1942 	struct mbuf *mret;
1943 	int len;
1944 
1945 	if (ifa->address.sa.sa_family == AF_INET) {
1946 		len = sizeof(struct sctp_ipv4addr_param);
1947 	} else if (ifa->address.sa.sa_family == AF_INET6) {
1948 		len = sizeof(struct sctp_ipv6addr_param);
1949 	} else {
1950 		/* unknown type */
1951 		return (m);
1952 	}
1953 	if (M_TRAILINGSPACE(m) >= len) {
1954 		/* easy side we just drop it on the end */
1955 		parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1956 		mret = m;
1957 	} else {
1958 		/* Need more space */
1959 		mret = m;
1960 		while (SCTP_BUF_NEXT(mret) != NULL) {
1961 			mret = SCTP_BUF_NEXT(mret);
1962 		}
1963 		SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1964 		if (SCTP_BUF_NEXT(mret) == NULL) {
1965 			/* We are hosed, can't add more addresses */
1966 			return (m);
1967 		}
1968 		mret = SCTP_BUF_NEXT(mret);
1969 		parmh = mtod(mret, struct sctp_paramhdr *);
1970 	}
1971 	/* now add the parameter */
1972 	switch (ifa->address.sa.sa_family) {
1973 	case AF_INET:
1974 		{
1975 			struct sctp_ipv4addr_param *ipv4p;
1976 			struct sockaddr_in *sin;
1977 
1978 			sin = (struct sockaddr_in *)&ifa->address.sin;
1979 			ipv4p = (struct sctp_ipv4addr_param *)parmh;
1980 			parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1981 			parmh->param_length = htons(len);
1982 			ipv4p->addr = sin->sin_addr.s_addr;
1983 			SCTP_BUF_LEN(mret) += len;
1984 			break;
1985 		}
1986 #ifdef INET6
1987 	case AF_INET6:
1988 		{
1989 			struct sctp_ipv6addr_param *ipv6p;
1990 			struct sockaddr_in6 *sin6;
1991 
1992 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1993 			ipv6p = (struct sctp_ipv6addr_param *)parmh;
1994 			parmh->param_type = htons(SCTP_IPV6_ADDRESS);
1995 			parmh->param_length = htons(len);
1996 			memcpy(ipv6p->addr, &sin6->sin6_addr,
1997 			    sizeof(ipv6p->addr));
1998 			/* clear embedded scope in the address */
1999 			in6_clearscope((struct in6_addr *)ipv6p->addr);
2000 			SCTP_BUF_LEN(mret) += len;
2001 			break;
2002 		}
2003 #endif
2004 	default:
2005 		return (m);
2006 	}
2007 	return (mret);
2008 }
2009 
2010 
2011 struct mbuf *
2012 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_scoping *scope,
2013     struct mbuf *m_at, int cnt_inits_to)
2014 {
2015 	struct sctp_vrf *vrf = NULL;
2016 	int cnt, limit_out = 0, total_count;
2017 	uint32_t vrf_id;
2018 
2019 	vrf_id = inp->def_vrf_id;
2020 	SCTP_IPI_ADDR_RLOCK();
2021 	vrf = sctp_find_vrf(vrf_id);
2022 	if (vrf == NULL) {
2023 		SCTP_IPI_ADDR_RUNLOCK();
2024 		return (m_at);
2025 	}
2026 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2027 		struct sctp_ifa *sctp_ifap;
2028 		struct sctp_ifn *sctp_ifnp;
2029 
2030 		cnt = cnt_inits_to;
2031 		if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2032 			limit_out = 1;
2033 			cnt = SCTP_ADDRESS_LIMIT;
2034 			goto skip_count;
2035 		}
2036 		LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2037 			if ((scope->loopback_scope == 0) &&
2038 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2039 				/*
2040 				 * Skip loopback devices if loopback_scope
2041 				 * not set
2042 				 */
2043 				continue;
2044 			}
2045 			LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2046 				if (sctp_is_address_in_scope(sctp_ifap,
2047 				    scope->ipv4_addr_legal,
2048 				    scope->ipv6_addr_legal,
2049 				    scope->loopback_scope,
2050 				    scope->ipv4_local_scope,
2051 				    scope->local_scope,
2052 				    scope->site_scope, 1) == 0) {
2053 					continue;
2054 				}
2055 				cnt++;
2056 				if (cnt > SCTP_ADDRESS_LIMIT) {
2057 					break;
2058 				}
2059 			}
2060 			if (cnt > SCTP_ADDRESS_LIMIT) {
2061 				break;
2062 			}
2063 		}
2064 skip_count:
2065 		if (cnt > 1) {
2066 			total_count = 0;
2067 			LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2068 				cnt = 0;
2069 				if ((scope->loopback_scope == 0) &&
2070 				    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2071 					/*
2072 					 * Skip loopback devices if
2073 					 * loopback_scope not set
2074 					 */
2075 					continue;
2076 				}
2077 				LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2078 					if (sctp_is_address_in_scope(sctp_ifap,
2079 					    scope->ipv4_addr_legal,
2080 					    scope->ipv6_addr_legal,
2081 					    scope->loopback_scope,
2082 					    scope->ipv4_local_scope,
2083 					    scope->local_scope,
2084 					    scope->site_scope, 0) == 0) {
2085 						continue;
2086 					}
2087 					m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2088 					if (limit_out) {
2089 						cnt++;
2090 						total_count++;
2091 						if (cnt >= 2) {
2092 							/*
2093 							 * two from each
2094 							 * address
2095 							 */
2096 							break;
2097 						}
2098 						if (total_count > SCTP_ADDRESS_LIMIT) {
2099 							/* No more addresses */
2100 							break;
2101 						}
2102 					}
2103 				}
2104 			}
2105 		}
2106 	} else {
2107 		struct sctp_laddr *laddr;
2108 
2109 		cnt = cnt_inits_to;
2110 		/* First, how many ? */
2111 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2112 			if (laddr->ifa == NULL) {
2113 				continue;
2114 			}
2115 			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2116 				/*
2117 				 * Address being deleted by the system, dont
2118 				 * list.
2119 				 */
2120 				continue;
2121 			if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2122 				/*
2123 				 * Address being deleted on this ep don't
2124 				 * list.
2125 				 */
2126 				continue;
2127 			}
2128 			if (sctp_is_address_in_scope(laddr->ifa,
2129 			    scope->ipv4_addr_legal,
2130 			    scope->ipv6_addr_legal,
2131 			    scope->loopback_scope,
2132 			    scope->ipv4_local_scope,
2133 			    scope->local_scope,
2134 			    scope->site_scope, 1) == 0) {
2135 				continue;
2136 			}
2137 			cnt++;
2138 		}
2139 		if (cnt > SCTP_ADDRESS_LIMIT) {
2140 			limit_out = 1;
2141 		}
2142 		/*
2143 		 * To get through a NAT we only list addresses if we have
2144 		 * more than one. That way if you just bind a single address
2145 		 * we let the source of the init dictate our address.
2146 		 */
2147 		if (cnt > 1) {
2148 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2149 				cnt = 0;
2150 				if (laddr->ifa == NULL) {
2151 					continue;
2152 				}
2153 				if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2154 					continue;
2155 
2156 				if (sctp_is_address_in_scope(laddr->ifa,
2157 				    scope->ipv4_addr_legal,
2158 				    scope->ipv6_addr_legal,
2159 				    scope->loopback_scope,
2160 				    scope->ipv4_local_scope,
2161 				    scope->local_scope,
2162 				    scope->site_scope, 0) == 0) {
2163 					continue;
2164 				}
2165 				m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2166 				cnt++;
2167 				if (cnt >= SCTP_ADDRESS_LIMIT) {
2168 					break;
2169 				}
2170 			}
2171 		}
2172 	}
2173 	SCTP_IPI_ADDR_RUNLOCK();
2174 	return (m_at);
2175 }
2176 
2177 static struct sctp_ifa *
2178 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2179     uint8_t dest_is_loop,
2180     uint8_t dest_is_priv,
2181     sa_family_t fam)
2182 {
2183 	uint8_t dest_is_global = 0;
2184 
2185 	/* dest_is_priv is true if destination is a private address */
2186 	/* dest_is_loop is true if destination is a loopback addresses */
2187 
2188 	/*
2189 	 * Here we determine if its a preferred address. A preferred address
2190 	 * means it is the same scope or higher scope then the destination.
2191 	 * L = loopback, P = private, G = global
2192 	 * ----------------------------------------- src    |  dest | result
2193 	 * ---------------------------------------- L     |    L  |    yes
2194 	 * ----------------------------------------- P     |    L  |
2195 	 * yes-v4 no-v6 ----------------------------------------- G     |
2196 	 * L  |    yes-v4 no-v6 ----------------------------------------- L
2197 	 * |    P  |    no ----------------------------------------- P     |
2198 	 * P  |    yes ----------------------------------------- G     |
2199 	 * P  |    no ----------------------------------------- L     |    G
2200 	 * |    no ----------------------------------------- P     |    G  |
2201 	 * no ----------------------------------------- G     |    G  |
2202 	 * yes -----------------------------------------
2203 	 */
2204 
2205 	if (ifa->address.sa.sa_family != fam) {
2206 		/* forget mis-matched family */
2207 		return (NULL);
2208 	}
2209 	if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2210 		dest_is_global = 1;
2211 	}
2212 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2213 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2214 	/* Ok the address may be ok */
2215 	if (fam == AF_INET6) {
2216 		/* ok to use deprecated addresses? no lets not! */
2217 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2218 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2219 			return (NULL);
2220 		}
2221 		if (ifa->src_is_priv && !ifa->src_is_loop) {
2222 			if (dest_is_loop) {
2223 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2224 				return (NULL);
2225 			}
2226 		}
2227 		if (ifa->src_is_glob) {
2228 			if (dest_is_loop) {
2229 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2230 				return (NULL);
2231 			}
2232 		}
2233 	}
2234 	/*
2235 	 * Now that we know what is what, implement or table this could in
2236 	 * theory be done slicker (it used to be), but this is
2237 	 * straightforward and easier to validate :-)
2238 	 */
2239 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2240 	    ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2241 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2242 	    dest_is_loop, dest_is_priv, dest_is_global);
2243 
2244 	if ((ifa->src_is_loop) && (dest_is_priv)) {
2245 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2246 		return (NULL);
2247 	}
2248 	if ((ifa->src_is_glob) && (dest_is_priv)) {
2249 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2250 		return (NULL);
2251 	}
2252 	if ((ifa->src_is_loop) && (dest_is_global)) {
2253 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2254 		return (NULL);
2255 	}
2256 	if ((ifa->src_is_priv) && (dest_is_global)) {
2257 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2258 		return (NULL);
2259 	}
2260 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2261 	/* its a preferred address */
2262 	return (ifa);
2263 }
2264 
2265 static struct sctp_ifa *
2266 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2267     uint8_t dest_is_loop,
2268     uint8_t dest_is_priv,
2269     sa_family_t fam)
2270 {
2271 	uint8_t dest_is_global = 0;
2272 
2273 
2274 	/*
2275 	 * Here we determine if its a acceptable address. A acceptable
2276 	 * address means it is the same scope or higher scope but we can
2277 	 * allow for NAT which means its ok to have a global dest and a
2278 	 * private src.
2279 	 *
2280 	 * L = loopback, P = private, G = global
2281 	 * ----------------------------------------- src    |  dest | result
2282 	 * ----------------------------------------- L     |   L   |    yes
2283 	 * ----------------------------------------- P     |   L   |
2284 	 * yes-v4 no-v6 ----------------------------------------- G     |
2285 	 * L   |    yes ----------------------------------------- L     |
2286 	 * P   |    no ----------------------------------------- P     |   P
2287 	 * |    yes ----------------------------------------- G     |   P
2288 	 * |    yes - May not work -----------------------------------------
2289 	 * L     |   G   |    no ----------------------------------------- P
2290 	 * |   G   |    yes - May not work
2291 	 * ----------------------------------------- G     |   G   |    yes
2292 	 * -----------------------------------------
2293 	 */
2294 
2295 	if (ifa->address.sa.sa_family != fam) {
2296 		/* forget non matching family */
2297 		return (NULL);
2298 	}
2299 	/* Ok the address may be ok */
2300 	if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2301 		dest_is_global = 1;
2302 	}
2303 	if (fam == AF_INET6) {
2304 		/* ok to use deprecated addresses? */
2305 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2306 			return (NULL);
2307 		}
2308 		if (ifa->src_is_priv) {
2309 			/* Special case, linklocal to loop */
2310 			if (dest_is_loop)
2311 				return (NULL);
2312 		}
2313 	}
2314 	/*
2315 	 * Now that we know what is what, implement our table. This could in
2316 	 * theory be done slicker (it used to be), but this is
2317 	 * straightforward and easier to validate :-)
2318 	 */
2319 	if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2320 		return (NULL);
2321 	}
2322 	if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2323 		return (NULL);
2324 	}
2325 	/* its an acceptable address */
2326 	return (ifa);
2327 }
2328 
2329 int
2330 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2331 {
2332 	struct sctp_laddr *laddr;
2333 
2334 	if (stcb == NULL) {
2335 		/* There are no restrictions, no TCB :-) */
2336 		return (0);
2337 	}
2338 	LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2339 		if (laddr->ifa == NULL) {
2340 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2341 			    __FUNCTION__);
2342 			continue;
2343 		}
2344 		if (laddr->ifa == ifa) {
2345 			/* Yes it is on the list */
2346 			return (1);
2347 		}
2348 	}
2349 	return (0);
2350 }
2351 
2352 
2353 int
2354 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2355 {
2356 	struct sctp_laddr *laddr;
2357 
2358 	if (ifa == NULL)
2359 		return (0);
2360 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2361 		if (laddr->ifa == NULL) {
2362 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2363 			    __FUNCTION__);
2364 			continue;
2365 		}
2366 		if ((laddr->ifa == ifa) && laddr->action == 0)
2367 			/* same pointer */
2368 			return (1);
2369 	}
2370 	return (0);
2371 }
2372 
2373 
2374 
2375 static struct sctp_ifa *
2376 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2377     sctp_route_t * ro,
2378     uint32_t vrf_id,
2379     int non_asoc_addr_ok,
2380     uint8_t dest_is_priv,
2381     uint8_t dest_is_loop,
2382     sa_family_t fam)
2383 {
2384 	struct sctp_laddr *laddr, *starting_point;
2385 	void *ifn;
2386 	int resettotop = 0;
2387 	struct sctp_ifn *sctp_ifn;
2388 	struct sctp_ifa *sctp_ifa, *sifa;
2389 	struct sctp_vrf *vrf;
2390 	uint32_t ifn_index;
2391 
2392 	vrf = sctp_find_vrf(vrf_id);
2393 	if (vrf == NULL)
2394 		return (NULL);
2395 
2396 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2397 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2398 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2399 	/*
2400 	 * first question, is the ifn we will emit on in our list, if so, we
2401 	 * want such an address. Note that we first looked for a preferred
2402 	 * address.
2403 	 */
2404 	if (sctp_ifn) {
2405 		/* is a preferred one on the interface we route out? */
2406 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2407 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2408 			    (non_asoc_addr_ok == 0))
2409 				continue;
2410 			sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2411 			    dest_is_loop,
2412 			    dest_is_priv, fam);
2413 			if (sifa == NULL)
2414 				continue;
2415 			if (sctp_is_addr_in_ep(inp, sifa)) {
2416 				atomic_add_int(&sifa->refcount, 1);
2417 				return (sifa);
2418 			}
2419 		}
2420 	}
2421 	/*
2422 	 * ok, now we now need to find one on the list of the addresses. We
2423 	 * can't get one on the emitting interface so let's find first a
2424 	 * preferred one. If not that an acceptable one otherwise... we
2425 	 * return NULL.
2426 	 */
2427 	starting_point = inp->next_addr_touse;
2428 once_again:
2429 	if (inp->next_addr_touse == NULL) {
2430 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2431 		resettotop = 1;
2432 	}
2433 	for (laddr = inp->next_addr_touse; laddr;
2434 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2435 		if (laddr->ifa == NULL) {
2436 			/* address has been removed */
2437 			continue;
2438 		}
2439 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2440 			/* address is being deleted */
2441 			continue;
2442 		}
2443 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2444 		    dest_is_priv, fam);
2445 		if (sifa == NULL)
2446 			continue;
2447 		atomic_add_int(&sifa->refcount, 1);
2448 		return (sifa);
2449 	}
2450 	if (resettotop == 0) {
2451 		inp->next_addr_touse = NULL;
2452 		goto once_again;
2453 	}
2454 	inp->next_addr_touse = starting_point;
2455 	resettotop = 0;
2456 once_again_too:
2457 	if (inp->next_addr_touse == NULL) {
2458 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2459 		resettotop = 1;
2460 	}
2461 	/* ok, what about an acceptable address in the inp */
2462 	for (laddr = inp->next_addr_touse; laddr;
2463 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2464 		if (laddr->ifa == NULL) {
2465 			/* address has been removed */
2466 			continue;
2467 		}
2468 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2469 			/* address is being deleted */
2470 			continue;
2471 		}
2472 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2473 		    dest_is_priv, fam);
2474 		if (sifa == NULL)
2475 			continue;
2476 		atomic_add_int(&sifa->refcount, 1);
2477 		return (sifa);
2478 	}
2479 	if (resettotop == 0) {
2480 		inp->next_addr_touse = NULL;
2481 		goto once_again_too;
2482 	}
2483 	/*
2484 	 * no address bound can be a source for the destination we are in
2485 	 * trouble
2486 	 */
2487 	return (NULL);
2488 }
2489 
2490 
2491 
2492 static struct sctp_ifa *
2493 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2494     struct sctp_tcb *stcb,
2495     struct sctp_nets *net,
2496     sctp_route_t * ro,
2497     uint32_t vrf_id,
2498     uint8_t dest_is_priv,
2499     uint8_t dest_is_loop,
2500     int non_asoc_addr_ok,
2501     sa_family_t fam)
2502 {
2503 	struct sctp_laddr *laddr, *starting_point;
2504 	void *ifn;
2505 	struct sctp_ifn *sctp_ifn;
2506 	struct sctp_ifa *sctp_ifa, *sifa;
2507 	uint8_t start_at_beginning = 0;
2508 	struct sctp_vrf *vrf;
2509 	uint32_t ifn_index;
2510 
2511 	/*
2512 	 * first question, is the ifn we will emit on in our list, if so, we
2513 	 * want that one.
2514 	 */
2515 	vrf = sctp_find_vrf(vrf_id);
2516 	if (vrf == NULL)
2517 		return (NULL);
2518 
2519 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2520 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2521 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2522 
2523 	/*
2524 	 * first question, is the ifn we will emit on in our list?  If so,
2525 	 * we want that one. First we look for a preferred. Second, we go
2526 	 * for an acceptable.
2527 	 */
2528 	if (sctp_ifn) {
2529 		/* first try for a preferred address on the ep */
2530 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2531 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2532 				continue;
2533 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2534 				sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2535 				if (sifa == NULL)
2536 					continue;
2537 				if (((non_asoc_addr_ok == 0) &&
2538 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2539 				    (non_asoc_addr_ok &&
2540 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2541 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2542 					/* on the no-no list */
2543 					continue;
2544 				}
2545 				atomic_add_int(&sifa->refcount, 1);
2546 				return (sifa);
2547 			}
2548 		}
2549 		/* next try for an acceptable address on the ep */
2550 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2551 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2552 				continue;
2553 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2554 				sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2555 				if (sifa == NULL)
2556 					continue;
2557 				if (((non_asoc_addr_ok == 0) &&
2558 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2559 				    (non_asoc_addr_ok &&
2560 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2561 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2562 					/* on the no-no list */
2563 					continue;
2564 				}
2565 				atomic_add_int(&sifa->refcount, 1);
2566 				return (sifa);
2567 			}
2568 		}
2569 
2570 	}
2571 	/*
2572 	 * if we can't find one like that then we must look at all addresses
2573 	 * bound to pick one at first preferable then secondly acceptable.
2574 	 */
2575 	starting_point = stcb->asoc.last_used_address;
2576 sctp_from_the_top:
2577 	if (stcb->asoc.last_used_address == NULL) {
2578 		start_at_beginning = 1;
2579 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2580 	}
2581 	/* search beginning with the last used address */
2582 	for (laddr = stcb->asoc.last_used_address; laddr;
2583 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2584 		if (laddr->ifa == NULL) {
2585 			/* address has been removed */
2586 			continue;
2587 		}
2588 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2589 			/* address is being deleted */
2590 			continue;
2591 		}
2592 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2593 		if (sifa == NULL)
2594 			continue;
2595 		if (((non_asoc_addr_ok == 0) &&
2596 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2597 		    (non_asoc_addr_ok &&
2598 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2599 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2600 			/* on the no-no list */
2601 			continue;
2602 		}
2603 		stcb->asoc.last_used_address = laddr;
2604 		atomic_add_int(&sifa->refcount, 1);
2605 		return (sifa);
2606 	}
2607 	if (start_at_beginning == 0) {
2608 		stcb->asoc.last_used_address = NULL;
2609 		goto sctp_from_the_top;
2610 	}
2611 	/* now try for any higher scope than the destination */
2612 	stcb->asoc.last_used_address = starting_point;
2613 	start_at_beginning = 0;
2614 sctp_from_the_top2:
2615 	if (stcb->asoc.last_used_address == NULL) {
2616 		start_at_beginning = 1;
2617 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2618 	}
2619 	/* search beginning with the last used address */
2620 	for (laddr = stcb->asoc.last_used_address; laddr;
2621 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2622 		if (laddr->ifa == NULL) {
2623 			/* address has been removed */
2624 			continue;
2625 		}
2626 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2627 			/* address is being deleted */
2628 			continue;
2629 		}
2630 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2631 		    dest_is_priv, fam);
2632 		if (sifa == NULL)
2633 			continue;
2634 		if (((non_asoc_addr_ok == 0) &&
2635 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2636 		    (non_asoc_addr_ok &&
2637 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2638 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2639 			/* on the no-no list */
2640 			continue;
2641 		}
2642 		stcb->asoc.last_used_address = laddr;
2643 		atomic_add_int(&sifa->refcount, 1);
2644 		return (sifa);
2645 	}
2646 	if (start_at_beginning == 0) {
2647 		stcb->asoc.last_used_address = NULL;
2648 		goto sctp_from_the_top2;
2649 	}
2650 	return (NULL);
2651 }
2652 
2653 static struct sctp_ifa *
2654 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2655     struct sctp_tcb *stcb,
2656     int non_asoc_addr_ok,
2657     uint8_t dest_is_loop,
2658     uint8_t dest_is_priv,
2659     int addr_wanted,
2660     sa_family_t fam,
2661     sctp_route_t * ro
2662 )
2663 {
2664 	struct sctp_ifa *ifa, *sifa;
2665 	int num_eligible_addr = 0;
2666 
2667 #ifdef INET6
2668 	struct sockaddr_in6 sin6, lsa6;
2669 
2670 	if (fam == AF_INET6) {
2671 		memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2672 		(void)sa6_recoverscope(&sin6);
2673 	}
2674 #endif				/* INET6 */
2675 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2676 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2677 		    (non_asoc_addr_ok == 0))
2678 			continue;
2679 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2680 		    dest_is_priv, fam);
2681 		if (sifa == NULL)
2682 			continue;
2683 #ifdef INET6
2684 		if (fam == AF_INET6 &&
2685 		    dest_is_loop &&
2686 		    sifa->src_is_loop && sifa->src_is_priv) {
2687 			/*
2688 			 * don't allow fe80::1 to be a src on loop ::1, we
2689 			 * don't list it to the peer so we will get an
2690 			 * abort.
2691 			 */
2692 			continue;
2693 		}
2694 		if (fam == AF_INET6 &&
2695 		    IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2696 		    IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2697 			/*
2698 			 * link-local <-> link-local must belong to the same
2699 			 * scope.
2700 			 */
2701 			memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2702 			(void)sa6_recoverscope(&lsa6);
2703 			if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2704 				continue;
2705 			}
2706 		}
2707 #endif				/* INET6 */
2708 
2709 		/*
2710 		 * Check if the IPv6 address matches to next-hop. In the
2711 		 * mobile case, old IPv6 address may be not deleted from the
2712 		 * interface. Then, the interface has previous and new
2713 		 * addresses.  We should use one corresponding to the
2714 		 * next-hop.  (by micchie)
2715 		 */
2716 #ifdef INET6
2717 		if (stcb && fam == AF_INET6 &&
2718 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2719 			if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2720 			    == 0) {
2721 				continue;
2722 			}
2723 		}
2724 #endif
2725 		/* Avoid topologically incorrect IPv4 address */
2726 		if (stcb && fam == AF_INET &&
2727 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2728 			if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2729 				continue;
2730 			}
2731 		}
2732 		if (stcb) {
2733 			if (((non_asoc_addr_ok == 0) &&
2734 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2735 			    (non_asoc_addr_ok &&
2736 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2737 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2738 				/*
2739 				 * It is restricted for some reason..
2740 				 * probably not yet added.
2741 				 */
2742 				continue;
2743 			}
2744 		}
2745 		if (num_eligible_addr >= addr_wanted) {
2746 			return (sifa);
2747 		}
2748 		num_eligible_addr++;
2749 	}
2750 	return (NULL);
2751 }
2752 
2753 
2754 static int
2755 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2756     struct sctp_tcb *stcb,
2757     int non_asoc_addr_ok,
2758     uint8_t dest_is_loop,
2759     uint8_t dest_is_priv,
2760     sa_family_t fam)
2761 {
2762 	struct sctp_ifa *ifa, *sifa;
2763 	int num_eligible_addr = 0;
2764 
2765 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2766 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2767 		    (non_asoc_addr_ok == 0)) {
2768 			continue;
2769 		}
2770 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2771 		    dest_is_priv, fam);
2772 		if (sifa == NULL) {
2773 			continue;
2774 		}
2775 		if (stcb) {
2776 			if (((non_asoc_addr_ok == 0) &&
2777 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2778 			    (non_asoc_addr_ok &&
2779 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2780 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2781 				/*
2782 				 * It is restricted for some reason..
2783 				 * probably not yet added.
2784 				 */
2785 				continue;
2786 			}
2787 		}
2788 		num_eligible_addr++;
2789 	}
2790 	return (num_eligible_addr);
2791 }
2792 
2793 static struct sctp_ifa *
2794 sctp_choose_boundall(struct sctp_inpcb *inp,
2795     struct sctp_tcb *stcb,
2796     struct sctp_nets *net,
2797     sctp_route_t * ro,
2798     uint32_t vrf_id,
2799     uint8_t dest_is_priv,
2800     uint8_t dest_is_loop,
2801     int non_asoc_addr_ok,
2802     sa_family_t fam)
2803 {
2804 	int cur_addr_num = 0, num_preferred = 0;
2805 	void *ifn;
2806 	struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2807 	struct sctp_ifa *sctp_ifa, *sifa;
2808 	uint32_t ifn_index;
2809 	struct sctp_vrf *vrf;
2810 
2811 	/*-
2812 	 * For boundall we can use any address in the association.
2813 	 * If non_asoc_addr_ok is set we can use any address (at least in
2814 	 * theory). So we look for preferred addresses first. If we find one,
2815 	 * we use it. Otherwise we next try to get an address on the
2816 	 * interface, which we should be able to do (unless non_asoc_addr_ok
2817 	 * is false and we are routed out that way). In these cases where we
2818 	 * can't use the address of the interface we go through all the
2819 	 * ifn's looking for an address we can use and fill that in. Punting
2820 	 * means we send back address 0, which will probably cause problems
2821 	 * actually since then IP will fill in the address of the route ifn,
2822 	 * which means we probably already rejected it.. i.e. here comes an
2823 	 * abort :-<.
2824 	 */
2825 	vrf = sctp_find_vrf(vrf_id);
2826 	if (vrf == NULL)
2827 		return (NULL);
2828 
2829 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2830 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2831 	emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2832 	if (sctp_ifn == NULL) {
2833 		/* ?? We don't have this guy ?? */
2834 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2835 		goto bound_all_plan_b;
2836 	}
2837 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2838 	    ifn_index, sctp_ifn->ifn_name);
2839 
2840 	if (net) {
2841 		cur_addr_num = net->indx_of_eligible_next_to_use;
2842 	}
2843 	num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2844 	    stcb,
2845 	    non_asoc_addr_ok,
2846 	    dest_is_loop,
2847 	    dest_is_priv, fam);
2848 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2849 	    num_preferred, sctp_ifn->ifn_name);
2850 	if (num_preferred == 0) {
2851 		/*
2852 		 * no eligible addresses, we must use some other interface
2853 		 * address if we can find one.
2854 		 */
2855 		goto bound_all_plan_b;
2856 	}
2857 	/*
2858 	 * Ok we have num_eligible_addr set with how many we can use, this
2859 	 * may vary from call to call due to addresses being deprecated
2860 	 * etc..
2861 	 */
2862 	if (cur_addr_num >= num_preferred) {
2863 		cur_addr_num = 0;
2864 	}
2865 	/*
2866 	 * select the nth address from the list (where cur_addr_num is the
2867 	 * nth) and 0 is the first one, 1 is the second one etc...
2868 	 */
2869 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2870 
2871 	sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2872 	    dest_is_priv, cur_addr_num, fam, ro);
2873 
2874 	/* if sctp_ifa is NULL something changed??, fall to plan b. */
2875 	if (sctp_ifa) {
2876 		atomic_add_int(&sctp_ifa->refcount, 1);
2877 		if (net) {
2878 			/* save off where the next one we will want */
2879 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2880 		}
2881 		return (sctp_ifa);
2882 	}
2883 	/*
2884 	 * plan_b: Look at all interfaces and find a preferred address. If
2885 	 * no preferred fall through to plan_c.
2886 	 */
2887 bound_all_plan_b:
2888 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2889 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2890 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2891 		    sctp_ifn->ifn_name);
2892 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2893 			/* wrong base scope */
2894 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2895 			continue;
2896 		}
2897 		if ((sctp_ifn == looked_at) && looked_at) {
2898 			/* already looked at this guy */
2899 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2900 			continue;
2901 		}
2902 		num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2903 		    dest_is_loop, dest_is_priv, fam);
2904 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
2905 		    "Found ifn:%p %d preferred source addresses\n",
2906 		    ifn, num_preferred);
2907 		if (num_preferred == 0) {
2908 			/* None on this interface. */
2909 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2910 			continue;
2911 		}
2912 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
2913 		    "num preferred:%d on interface:%p cur_addr_num:%d\n",
2914 		    num_preferred, sctp_ifn, cur_addr_num);
2915 
2916 		/*
2917 		 * Ok we have num_eligible_addr set with how many we can
2918 		 * use, this may vary from call to call due to addresses
2919 		 * being deprecated etc..
2920 		 */
2921 		if (cur_addr_num >= num_preferred) {
2922 			cur_addr_num = 0;
2923 		}
2924 		sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2925 		    dest_is_priv, cur_addr_num, fam, ro);
2926 		if (sifa == NULL)
2927 			continue;
2928 		if (net) {
2929 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2930 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
2931 			    cur_addr_num);
2932 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
2933 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
2934 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
2935 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
2936 		}
2937 		atomic_add_int(&sifa->refcount, 1);
2938 		return (sifa);
2939 
2940 	}
2941 
2942 	/* plan_c: do we have an acceptable address on the emit interface */
2943 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
2944 	if (emit_ifn == NULL) {
2945 		goto plan_d;
2946 	}
2947 	LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
2948 		if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2949 		    (non_asoc_addr_ok == 0))
2950 			continue;
2951 		sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
2952 		    dest_is_priv, fam);
2953 		if (sifa == NULL)
2954 			continue;
2955 		if (stcb) {
2956 			if (((non_asoc_addr_ok == 0) &&
2957 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2958 			    (non_asoc_addr_ok &&
2959 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2960 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2961 				/*
2962 				 * It is restricted for some reason..
2963 				 * probably not yet added.
2964 				 */
2965 				continue;
2966 			}
2967 		}
2968 		atomic_add_int(&sifa->refcount, 1);
2969 		return (sifa);
2970 	}
2971 plan_d:
2972 	/*
2973 	 * plan_d: We are in trouble. No preferred address on the emit
2974 	 * interface. And not even a preferred address on all interfaces. Go
2975 	 * out and see if we can find an acceptable address somewhere
2976 	 * amongst all interfaces.
2977 	 */
2978 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
2979 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2980 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2981 			/* wrong base scope */
2982 			continue;
2983 		}
2984 		if ((sctp_ifn == looked_at) && looked_at)
2985 			/* already looked at this guy */
2986 			continue;
2987 
2988 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2989 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2990 			    (non_asoc_addr_ok == 0))
2991 				continue;
2992 			sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
2993 			    dest_is_loop,
2994 			    dest_is_priv, fam);
2995 			if (sifa == NULL)
2996 				continue;
2997 			if (stcb) {
2998 				if (((non_asoc_addr_ok == 0) &&
2999 				    (sctp_is_addr_restricted(stcb, sifa))) ||
3000 				    (non_asoc_addr_ok &&
3001 				    (sctp_is_addr_restricted(stcb, sifa)) &&
3002 				    (!sctp_is_addr_pending(stcb, sifa)))) {
3003 					/*
3004 					 * It is restricted for some
3005 					 * reason.. probably not yet added.
3006 					 */
3007 					continue;
3008 				}
3009 			}
3010 			atomic_add_int(&sifa->refcount, 1);
3011 			return (sifa);
3012 		}
3013 	}
3014 	/*
3015 	 * Ok we can find NO address to source from that is not on our
3016 	 * restricted list and non_asoc_address is NOT ok, or it is on our
3017 	 * restricted list. We can't source to it :-(
3018 	 */
3019 	return (NULL);
3020 }
3021 
3022 
3023 
3024 /* tcb may be NULL */
3025 struct sctp_ifa *
3026 sctp_source_address_selection(struct sctp_inpcb *inp,
3027     struct sctp_tcb *stcb,
3028     sctp_route_t * ro,
3029     struct sctp_nets *net,
3030     int non_asoc_addr_ok, uint32_t vrf_id)
3031 {
3032 	struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3033 
3034 #ifdef INET6
3035 	struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3036 
3037 #endif
3038 	struct sctp_ifa *answer;
3039 	uint8_t dest_is_priv, dest_is_loop;
3040 	sa_family_t fam;
3041 
3042 	/*-
3043 	 * Rules: - Find the route if needed, cache if I can. - Look at
3044 	 * interface address in route, Is it in the bound list. If so we
3045 	 * have the best source. - If not we must rotate amongst the
3046 	 * addresses.
3047 	 *
3048 	 * Cavets and issues
3049 	 *
3050 	 * Do we need to pay attention to scope. We can have a private address
3051 	 * or a global address we are sourcing or sending to. So if we draw
3052 	 * it out
3053 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3054 	 * For V4
3055          *------------------------------------------
3056 	 *      source     *      dest  *  result
3057 	 * -----------------------------------------
3058 	 * <a>  Private    *    Global  *  NAT
3059 	 * -----------------------------------------
3060 	 * <b>  Private    *    Private *  No problem
3061 	 * -----------------------------------------
3062          * <c>  Global     *    Private *  Huh, How will this work?
3063 	 * -----------------------------------------
3064          * <d>  Global     *    Global  *  No Problem
3065          *------------------------------------------
3066 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3067 	 * For V6
3068          *------------------------------------------
3069 	 *      source     *      dest  *  result
3070 	 * -----------------------------------------
3071 	 * <a>  Linklocal  *    Global  *
3072 	 * -----------------------------------------
3073 	 * <b>  Linklocal  * Linklocal  *  No problem
3074 	 * -----------------------------------------
3075          * <c>  Global     * Linklocal  *  Huh, How will this work?
3076 	 * -----------------------------------------
3077          * <d>  Global     *    Global  *  No Problem
3078          *------------------------------------------
3079 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3080          *
3081 	 * And then we add to that what happens if there are multiple addresses
3082 	 * assigned to an interface. Remember the ifa on a ifn is a linked
3083 	 * list of addresses. So one interface can have more than one IP
3084 	 * address. What happens if we have both a private and a global
3085 	 * address? Do we then use context of destination to sort out which
3086 	 * one is best? And what about NAT's sending P->G may get you a NAT
3087 	 * translation, or should you select the G thats on the interface in
3088 	 * preference.
3089 	 *
3090 	 * Decisions:
3091 	 *
3092 	 * - count the number of addresses on the interface.
3093          * - if it is one, no problem except case <c>.
3094          *   For <a> we will assume a NAT out there.
3095 	 * - if there are more than one, then we need to worry about scope P
3096 	 *   or G. We should prefer G -> G and P -> P if possible.
3097 	 *   Then as a secondary fall back to mixed types G->P being a last
3098 	 *   ditch one.
3099          * - The above all works for bound all, but bound specific we need to
3100 	 *   use the same concept but instead only consider the bound
3101 	 *   addresses. If the bound set is NOT assigned to the interface then
3102 	 *   we must use rotation amongst the bound addresses..
3103 	 */
3104 	if (ro->ro_rt == NULL) {
3105 		/*
3106 		 * Need a route to cache.
3107 		 */
3108 		SCTP_RTALLOC(ro, vrf_id);
3109 	}
3110 	if (ro->ro_rt == NULL) {
3111 		return (NULL);
3112 	}
3113 	fam = to->sin_family;
3114 	dest_is_priv = dest_is_loop = 0;
3115 	/* Setup our scopes for the destination */
3116 	switch (fam) {
3117 	case AF_INET:
3118 		/* Scope based on outbound address */
3119 		if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3120 			dest_is_loop = 1;
3121 			if (net != NULL) {
3122 				/* mark it as local */
3123 				net->addr_is_local = 1;
3124 			}
3125 		} else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3126 			dest_is_priv = 1;
3127 		}
3128 		break;
3129 #ifdef INET6
3130 	case AF_INET6:
3131 		/* Scope based on outbound address */
3132 		if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3133 		    SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3134 			/*
3135 			 * If the address is a loopback address, which
3136 			 * consists of "::1" OR "fe80::1%lo0", we are
3137 			 * loopback scope. But we don't use dest_is_priv
3138 			 * (link local addresses).
3139 			 */
3140 			dest_is_loop = 1;
3141 			if (net != NULL) {
3142 				/* mark it as local */
3143 				net->addr_is_local = 1;
3144 			}
3145 		} else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3146 			dest_is_priv = 1;
3147 		}
3148 		break;
3149 #endif
3150 	}
3151 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3152 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
3153 	SCTP_IPI_ADDR_RLOCK();
3154 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3155 		/*
3156 		 * Bound all case
3157 		 */
3158 		answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3159 		    dest_is_priv, dest_is_loop,
3160 		    non_asoc_addr_ok, fam);
3161 		SCTP_IPI_ADDR_RUNLOCK();
3162 		return (answer);
3163 	}
3164 	/*
3165 	 * Subset bound case
3166 	 */
3167 	if (stcb) {
3168 		answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
3169 		    vrf_id, dest_is_priv,
3170 		    dest_is_loop,
3171 		    non_asoc_addr_ok, fam);
3172 	} else {
3173 		answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3174 		    non_asoc_addr_ok,
3175 		    dest_is_priv,
3176 		    dest_is_loop, fam);
3177 	}
3178 	SCTP_IPI_ADDR_RUNLOCK();
3179 	return (answer);
3180 }
3181 
3182 static int
3183 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
3184 {
3185 	struct cmsghdr cmh;
3186 	int tlen, at;
3187 
3188 	tlen = SCTP_BUF_LEN(control);
3189 	at = 0;
3190 	/*
3191 	 * Independent of how many mbufs, find the c_type inside the control
3192 	 * structure and copy out the data.
3193 	 */
3194 	while (at < tlen) {
3195 		if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3196 			/* not enough room for one more we are done. */
3197 			return (0);
3198 		}
3199 		m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3200 		if (((int)cmh.cmsg_len + at) > tlen) {
3201 			/*
3202 			 * this is real messed up since there is not enough
3203 			 * data here to cover the cmsg header. We are done.
3204 			 */
3205 			return (0);
3206 		}
3207 		if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3208 		    (c_type == cmh.cmsg_type)) {
3209 			/* found the one we want, copy it out */
3210 			at += CMSG_ALIGN(sizeof(struct cmsghdr));
3211 			if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3212 				/*
3213 				 * space of cmsg_len after header not big
3214 				 * enough
3215 				 */
3216 				return (0);
3217 			}
3218 			m_copydata(control, at, cpsize, data);
3219 			return (1);
3220 		} else {
3221 			at += CMSG_ALIGN(cmh.cmsg_len);
3222 			if (cmh.cmsg_len == 0) {
3223 				break;
3224 			}
3225 		}
3226 	}
3227 	/* not found */
3228 	return (0);
3229 }
3230 
3231 static struct mbuf *
3232 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
3233     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3234 {
3235 	struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3236 	struct sctp_state_cookie *stc;
3237 	struct sctp_paramhdr *ph;
3238 	uint8_t *foo;
3239 	int sig_offset;
3240 	uint16_t cookie_sz;
3241 
3242 	mret = NULL;
3243 	mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3244 	    sizeof(struct sctp_paramhdr)), 0,
3245 	    M_DONTWAIT, 1, MT_DATA);
3246 	if (mret == NULL) {
3247 		return (NULL);
3248 	}
3249 	copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3250 	if (copy_init == NULL) {
3251 		sctp_m_freem(mret);
3252 		return (NULL);
3253 	}
3254 #ifdef SCTP_MBUF_LOGGING
3255 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3256 		struct mbuf *mat;
3257 
3258 		mat = copy_init;
3259 		while (mat) {
3260 			if (SCTP_BUF_IS_EXTENDED(mat)) {
3261 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3262 			}
3263 			mat = SCTP_BUF_NEXT(mat);
3264 		}
3265 	}
3266 #endif
3267 	copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3268 	    M_DONTWAIT);
3269 	if (copy_initack == NULL) {
3270 		sctp_m_freem(mret);
3271 		sctp_m_freem(copy_init);
3272 		return (NULL);
3273 	}
3274 #ifdef SCTP_MBUF_LOGGING
3275 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3276 		struct mbuf *mat;
3277 
3278 		mat = copy_initack;
3279 		while (mat) {
3280 			if (SCTP_BUF_IS_EXTENDED(mat)) {
3281 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3282 			}
3283 			mat = SCTP_BUF_NEXT(mat);
3284 		}
3285 	}
3286 #endif
3287 	/* easy side we just drop it on the end */
3288 	ph = mtod(mret, struct sctp_paramhdr *);
3289 	SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3290 	    sizeof(struct sctp_paramhdr);
3291 	stc = (struct sctp_state_cookie *)((caddr_t)ph +
3292 	    sizeof(struct sctp_paramhdr));
3293 	ph->param_type = htons(SCTP_STATE_COOKIE);
3294 	ph->param_length = 0;	/* fill in at the end */
3295 	/* Fill in the stc cookie data */
3296 	memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3297 
3298 	/* tack the INIT and then the INIT-ACK onto the chain */
3299 	cookie_sz = 0;
3300 	m_at = mret;
3301 	for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3302 		cookie_sz += SCTP_BUF_LEN(m_at);
3303 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3304 			SCTP_BUF_NEXT(m_at) = copy_init;
3305 			break;
3306 		}
3307 	}
3308 
3309 	for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3310 		cookie_sz += SCTP_BUF_LEN(m_at);
3311 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3312 			SCTP_BUF_NEXT(m_at) = copy_initack;
3313 			break;
3314 		}
3315 	}
3316 
3317 	for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3318 		cookie_sz += SCTP_BUF_LEN(m_at);
3319 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3320 			break;
3321 		}
3322 	}
3323 	sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3324 	if (sig == NULL) {
3325 		/* no space, so free the entire chain */
3326 		sctp_m_freem(mret);
3327 		return (NULL);
3328 	}
3329 	SCTP_BUF_LEN(sig) = 0;
3330 	SCTP_BUF_NEXT(m_at) = sig;
3331 	sig_offset = 0;
3332 	foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3333 	memset(foo, 0, SCTP_SIGNATURE_SIZE);
3334 	*signature = foo;
3335 	SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3336 	cookie_sz += SCTP_SIGNATURE_SIZE;
3337 	ph->param_length = htons(cookie_sz);
3338 	return (mret);
3339 }
3340 
3341 
3342 static uint8_t
3343 sctp_get_ect(struct sctp_tcb *stcb,
3344     struct sctp_tmit_chunk *chk)
3345 {
3346 	uint8_t this_random;
3347 
3348 	/* Huh? */
3349 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 0)
3350 		return (0);
3351 
3352 	if (SCTP_BASE_SYSCTL(sctp_ecn_nonce) == 0)
3353 		/* no nonce, always return ECT0 */
3354 		return (SCTP_ECT0_BIT);
3355 
3356 	if (stcb->asoc.peer_supports_ecn_nonce == 0) {
3357 		/* Peer does NOT support it, so we send a ECT0 only */
3358 		return (SCTP_ECT0_BIT);
3359 	}
3360 	if (chk == NULL)
3361 		return (SCTP_ECT0_BIT);
3362 
3363 	if ((stcb->asoc.hb_random_idx > 3) ||
3364 	    ((stcb->asoc.hb_random_idx == 3) &&
3365 	    (stcb->asoc.hb_ect_randombit > 7))) {
3366 		uint32_t rndval;
3367 
3368 warp_drive_sa:
3369 		rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
3370 		memcpy(stcb->asoc.hb_random_values, &rndval,
3371 		    sizeof(stcb->asoc.hb_random_values));
3372 		this_random = stcb->asoc.hb_random_values[0];
3373 		stcb->asoc.hb_random_idx = 0;
3374 		stcb->asoc.hb_ect_randombit = 0;
3375 	} else {
3376 		if (stcb->asoc.hb_ect_randombit > 7) {
3377 			stcb->asoc.hb_ect_randombit = 0;
3378 			stcb->asoc.hb_random_idx++;
3379 			if (stcb->asoc.hb_random_idx > 3) {
3380 				goto warp_drive_sa;
3381 			}
3382 		}
3383 		this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
3384 	}
3385 	if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
3386 		if (chk != NULL)
3387 			/* ECN Nonce stuff */
3388 			chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
3389 		stcb->asoc.hb_ect_randombit++;
3390 		return (SCTP_ECT1_BIT);
3391 	} else {
3392 		stcb->asoc.hb_ect_randombit++;
3393 		return (SCTP_ECT0_BIT);
3394 	}
3395 }
3396 
3397 static int
3398 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3399     struct sctp_tcb *stcb,	/* may be NULL */
3400     struct sctp_nets *net,
3401     struct sockaddr *to,
3402     struct mbuf *m,
3403     uint32_t auth_offset,
3404     struct sctp_auth_chunk *auth,
3405     int nofragment_flag,
3406     int ecn_ok,
3407     struct sctp_tmit_chunk *chk,
3408     int out_of_asoc_ok,
3409     uint16_t port,
3410     int so_locked,
3411 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3412     SCTP_UNUSED
3413 #endif
3414     union sctp_sockstore *over_addr
3415 )
3416 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3417 {
3418 	/*
3419 	 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
3420 	 * header WITH an SCTPHDR but no IP header, endpoint inp and sa
3421 	 * structure: - fill in the HMAC digest of any AUTH chunk in the
3422 	 * packet. - calculate and fill in the SCTP checksum. - prepend an
3423 	 * IP address header. - if boundall use INADDR_ANY. - if
3424 	 * boundspecific do source address selection. - set fragmentation
3425 	 * option for ipV4. - On return from IP output, check/adjust mtu
3426 	 * size of output interface and smallest_mtu size as well.
3427 	 */
3428 	/* Will need ifdefs around this */
3429 	struct mbuf *o_pak;
3430 	struct mbuf *newm;
3431 	struct sctphdr *sctphdr;
3432 	int packet_length;
3433 	uint32_t csum;
3434 	int ret;
3435 	uint32_t vrf_id;
3436 	sctp_route_t *ro = NULL;
3437 	struct udphdr *udp = NULL;
3438 
3439 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3440 	struct socket *so = NULL;
3441 
3442 #endif
3443 
3444 	if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3445 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3446 		sctp_m_freem(m);
3447 		return (EFAULT);
3448 	}
3449 	if (stcb) {
3450 		vrf_id = stcb->asoc.vrf_id;
3451 	} else {
3452 		vrf_id = inp->def_vrf_id;
3453 	}
3454 
3455 	/* fill in the HMAC digest for any AUTH chunk in the packet */
3456 	if ((auth != NULL) && (stcb != NULL)) {
3457 		sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb);
3458 	}
3459 	/* Calculate the csum and fill in the length of the packet */
3460 	sctphdr = mtod(m, struct sctphdr *);
3461 	if (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3462 	    (stcb) &&
3463 	    (to->sa_family == AF_INET) &&
3464 	    (stcb->asoc.loopback_scope)) {
3465 		sctphdr->checksum = 0;
3466 		/*
3467 		 * This can probably now be taken out since my audit shows
3468 		 * no more bad pktlen's coming in. But we will wait a while
3469 		 * yet.
3470 		 */
3471 		packet_length = sctp_calculate_len(m);
3472 	} else {
3473 		sctphdr->checksum = 0;
3474 		csum = sctp_calculate_sum(m, &packet_length, 0);
3475 		sctphdr->checksum = csum;
3476 	}
3477 
3478 	if (to->sa_family == AF_INET) {
3479 		struct ip *ip = NULL;
3480 		sctp_route_t iproute;
3481 		uint8_t tos_value;
3482 
3483 		if (port) {
3484 			newm = sctp_get_mbuf_for_msg(sizeof(struct ip) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
3485 		} else {
3486 			newm = sctp_get_mbuf_for_msg(sizeof(struct ip), 1, M_DONTWAIT, 1, MT_DATA);
3487 		}
3488 		if (newm == NULL) {
3489 			sctp_m_freem(m);
3490 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3491 			return (ENOMEM);
3492 		}
3493 		if (port) {
3494 			SCTP_ALIGN_TO_END(newm, sizeof(struct ip) + sizeof(struct udphdr));
3495 			SCTP_BUF_LEN(newm) = sizeof(struct ip) + sizeof(struct udphdr);
3496 			packet_length += sizeof(struct ip) + sizeof(struct udphdr);
3497 		} else {
3498 			SCTP_ALIGN_TO_END(newm, sizeof(struct ip));
3499 			SCTP_BUF_LEN(newm) = sizeof(struct ip);
3500 			packet_length += sizeof(struct ip);
3501 		}
3502 		SCTP_BUF_NEXT(newm) = m;
3503 		m = newm;
3504 		ip = mtod(m, struct ip *);
3505 		ip->ip_v = IPVERSION;
3506 		ip->ip_hl = (sizeof(struct ip) >> 2);
3507 		if (net) {
3508 			tos_value = net->tos_flowlabel & 0x000000ff;
3509 		} else {
3510 			tos_value = inp->ip_inp.inp.inp_ip_tos;
3511 		}
3512 		if ((nofragment_flag) && (port == 0)) {
3513 #if defined(WITH_CONVERT_IP_OFF) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
3514 			ip->ip_off = IP_DF;
3515 #else
3516 			ip->ip_off = htons(IP_DF);
3517 #endif
3518 		} else
3519 			ip->ip_off = 0;
3520 
3521 		/* FreeBSD has a function for ip_id's */
3522 		ip->ip_id = ip_newid();
3523 
3524 		ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3525 		ip->ip_len = packet_length;
3526 		if (stcb) {
3527 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3528 				/* Enable ECN */
3529 				ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
3530 			} else {
3531 				/* No ECN */
3532 				ip->ip_tos = (u_char)(tos_value & 0xfc);
3533 			}
3534 		} else {
3535 			/* no association at all */
3536 			ip->ip_tos = (tos_value & 0xfc);
3537 		}
3538 		if (port) {
3539 			ip->ip_p = IPPROTO_UDP;
3540 		} else {
3541 			ip->ip_p = IPPROTO_SCTP;
3542 		}
3543 		ip->ip_sum = 0;
3544 		if (net == NULL) {
3545 			ro = &iproute;
3546 			memset(&iproute, 0, sizeof(iproute));
3547 			memcpy(&ro->ro_dst, to, to->sa_len);
3548 		} else {
3549 			ro = (sctp_route_t *) & net->ro;
3550 		}
3551 		/* Now the address selection part */
3552 		ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
3553 
3554 		/* call the routine to select the src address */
3555 		if (net && out_of_asoc_ok == 0) {
3556 			if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3557 				sctp_free_ifa(net->ro._s_addr);
3558 				net->ro._s_addr = NULL;
3559 				net->src_addr_selected = 0;
3560 				if (ro->ro_rt) {
3561 					RTFREE(ro->ro_rt);
3562 					ro->ro_rt = NULL;
3563 				}
3564 			}
3565 			if (net->src_addr_selected == 0) {
3566 				/* Cache the source address */
3567 				net->ro._s_addr = sctp_source_address_selection(inp, stcb,
3568 				    ro, net, 0,
3569 				    vrf_id);
3570 				net->src_addr_selected = 1;
3571 			}
3572 			if (net->ro._s_addr == NULL) {
3573 				/* No route to host */
3574 				net->src_addr_selected = 0;
3575 				goto no_route;
3576 			}
3577 			ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
3578 		} else {
3579 			if (over_addr == NULL) {
3580 				struct sctp_ifa *_lsrc;
3581 
3582 				_lsrc = sctp_source_address_selection(inp, stcb, ro,
3583 				    net,
3584 				    out_of_asoc_ok,
3585 				    vrf_id);
3586 				if (_lsrc == NULL) {
3587 					goto no_route;
3588 				}
3589 				ip->ip_src = _lsrc->address.sin.sin_addr;
3590 				sctp_free_ifa(_lsrc);
3591 			} else {
3592 				ip->ip_src = over_addr->sin.sin_addr;
3593 				SCTP_RTALLOC((&ro->ro_rt), vrf_id);
3594 			}
3595 		}
3596 		if (port) {
3597 			udp = (struct udphdr *)(ip + 1);
3598 			udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3599 			udp->uh_dport = port;
3600 			udp->uh_ulen = htons(packet_length - sizeof(struct ip));
3601 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
3602 		}
3603 		/*
3604 		 * If source address selection fails and we find no route
3605 		 * then the ip_output should fail as well with a
3606 		 * NO_ROUTE_TO_HOST type error. We probably should catch
3607 		 * that somewhere and abort the association right away
3608 		 * (assuming this is an INIT being sent).
3609 		 */
3610 		if ((ro->ro_rt == NULL)) {
3611 			/*
3612 			 * src addr selection failed to find a route (or
3613 			 * valid source addr), so we can't get there from
3614 			 * here (yet)!
3615 			 */
3616 	no_route:
3617 			SCTPDBG(SCTP_DEBUG_OUTPUT1,
3618 			    "%s: dropped packet - no valid source addr\n",
3619 			    __FUNCTION__);
3620 			if (net) {
3621 				SCTPDBG(SCTP_DEBUG_OUTPUT1,
3622 				    "Destination was ");
3623 				SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
3624 				    &net->ro._l_addr.sa);
3625 				if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3626 					if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3627 						SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3628 						sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3629 						    stcb,
3630 						    SCTP_FAILED_THRESHOLD,
3631 						    (void *)net,
3632 						    so_locked);
3633 						net->dest_state &= ~SCTP_ADDR_REACHABLE;
3634 						net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
3635 						/*
3636 						 * JRS 5/14/07 - If a
3637 						 * destination is
3638 						 * unreachable, the PF bit
3639 						 * is turned off.  This
3640 						 * allows an unambiguous use
3641 						 * of the PF bit for
3642 						 * destinations that are
3643 						 * reachable but potentially
3644 						 * failed. If the
3645 						 * destination is set to the
3646 						 * unreachable state, also
3647 						 * set the destination to
3648 						 * the PF state.
3649 						 */
3650 						/*
3651 						 * Add debug message here if
3652 						 * destination is not in PF
3653 						 * state.
3654 						 */
3655 						/*
3656 						 * Stop any running T3
3657 						 * timers here?
3658 						 */
3659 						if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
3660 							net->dest_state &= ~SCTP_ADDR_PF;
3661 							SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
3662 							    net);
3663 						}
3664 					}
3665 				}
3666 				if (stcb) {
3667 					if (net == stcb->asoc.primary_destination) {
3668 						/* need a new primary */
3669 						struct sctp_nets *alt;
3670 
3671 						alt = sctp_find_alternate_net(stcb, net, 0);
3672 						if (alt != net) {
3673 							if (sctp_set_primary_addr(stcb,
3674 							    (struct sockaddr *)NULL,
3675 							    alt) == 0) {
3676 								net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
3677 								if (net->ro._s_addr) {
3678 									sctp_free_ifa(net->ro._s_addr);
3679 									net->ro._s_addr = NULL;
3680 								}
3681 								net->src_addr_selected = 0;
3682 							}
3683 						}
3684 					}
3685 				}
3686 			}
3687 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3688 			sctp_m_freem(m);
3689 			return (EHOSTUNREACH);
3690 		}
3691 		if (ro != &iproute) {
3692 			memcpy(&iproute, ro, sizeof(*ro));
3693 		}
3694 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
3695 		    (uint32_t) (ntohl(ip->ip_src.s_addr)));
3696 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
3697 		    (uint32_t) (ntohl(ip->ip_dst.s_addr)));
3698 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
3699 		    ro->ro_rt);
3700 
3701 		if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3702 			/* failed to prepend data, give up */
3703 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3704 			sctp_m_freem(m);
3705 			return (ENOMEM);
3706 		}
3707 #ifdef  SCTP_PACKET_LOGGING
3708 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
3709 			sctp_packet_log(m, packet_length);
3710 #endif
3711 		SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3712 		if (port) {
3713 			SCTP_ENABLE_UDP_CSUM(o_pak);
3714 		}
3715 		/* send it out.  table id is taken from stcb */
3716 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3717 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3718 			so = SCTP_INP_SO(inp);
3719 			SCTP_SOCKET_UNLOCK(so, 0);
3720 		}
3721 #endif
3722 		SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
3723 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3724 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3725 			atomic_add_int(&stcb->asoc.refcnt, 1);
3726 			SCTP_TCB_UNLOCK(stcb);
3727 			SCTP_SOCKET_LOCK(so, 0);
3728 			SCTP_TCB_LOCK(stcb);
3729 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
3730 		}
3731 #endif
3732 		SCTP_STAT_INCR(sctps_sendpackets);
3733 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3734 		if (ret)
3735 			SCTP_STAT_INCR(sctps_senderrors);
3736 
3737 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
3738 		if (net == NULL) {
3739 			/* free tempy routes */
3740 			if (ro->ro_rt) {
3741 				RTFREE(ro->ro_rt);
3742 				ro->ro_rt = NULL;
3743 			}
3744 		} else {
3745 			/* PMTU check versus smallest asoc MTU goes here */
3746 			if ((ro->ro_rt != NULL) &&
3747 			    (net->ro._s_addr)) {
3748 				uint32_t mtu;
3749 
3750 				mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3751 				if (mtu &&
3752 				    (stcb->asoc.smallest_mtu > mtu)) {
3753 #ifdef SCTP_PRINT_FOR_B_AND_M
3754 					SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n",
3755 					    mtu);
3756 #endif
3757 					sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3758 					net->mtu = mtu;
3759 				}
3760 			} else if (ro->ro_rt == NULL) {
3761 				/* route was freed */
3762 				if (net->ro._s_addr &&
3763 				    net->src_addr_selected) {
3764 					sctp_free_ifa(net->ro._s_addr);
3765 					net->ro._s_addr = NULL;
3766 				}
3767 				net->src_addr_selected = 0;
3768 			}
3769 		}
3770 		return (ret);
3771 	}
3772 #ifdef INET6
3773 	else if (to->sa_family == AF_INET6) {
3774 		uint32_t flowlabel;
3775 		struct ip6_hdr *ip6h;
3776 		struct route_in6 ip6route;
3777 		struct ifnet *ifp;
3778 		u_char flowTop;
3779 		uint16_t flowBottom;
3780 		u_char tosBottom, tosTop;
3781 		struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
3782 		int prev_scope = 0;
3783 		struct sockaddr_in6 lsa6_storage;
3784 		int error;
3785 		u_short prev_port = 0;
3786 
3787 		if (net != NULL) {
3788 			flowlabel = net->tos_flowlabel;
3789 		} else {
3790 			flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
3791 		}
3792 
3793 		if (port) {
3794 			newm = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
3795 		} else {
3796 			newm = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
3797 		}
3798 		if (newm == NULL) {
3799 			sctp_m_freem(m);
3800 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3801 			return (ENOMEM);
3802 		}
3803 		if (port) {
3804 			SCTP_ALIGN_TO_END(newm, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
3805 			SCTP_BUF_LEN(newm) = sizeof(struct ip6_hdr) + sizeof(struct udphdr);
3806 			packet_length += sizeof(struct ip6_hdr) + sizeof(struct udphdr);
3807 		} else {
3808 			SCTP_ALIGN_TO_END(newm, sizeof(struct ip6_hdr));
3809 			SCTP_BUF_LEN(newm) = sizeof(struct ip6_hdr);
3810 			packet_length += sizeof(struct ip6_hdr);
3811 		}
3812 		SCTP_BUF_NEXT(newm) = m;
3813 		m = newm;
3814 
3815 		ip6h = mtod(m, struct ip6_hdr *);
3816 		/*
3817 		 * We assume here that inp_flow is in host byte order within
3818 		 * the TCB!
3819 		 */
3820 		flowBottom = flowlabel & 0x0000ffff;
3821 		flowTop = ((flowlabel & 0x000f0000) >> 16);
3822 		tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
3823 		/* protect *sin6 from overwrite */
3824 		sin6 = (struct sockaddr_in6 *)to;
3825 		tmp = *sin6;
3826 		sin6 = &tmp;
3827 
3828 		/* KAME hack: embed scopeid */
3829 		if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
3830 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3831 			return (EINVAL);
3832 		}
3833 		if (net == NULL) {
3834 			memset(&ip6route, 0, sizeof(ip6route));
3835 			ro = (sctp_route_t *) & ip6route;
3836 			memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
3837 		} else {
3838 			ro = (sctp_route_t *) & net->ro;
3839 		}
3840 		if (stcb != NULL) {
3841 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3842 				/* Enable ECN */
3843 				tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
3844 			} else {
3845 				/* No ECN */
3846 				tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3847 			}
3848 		} else {
3849 			/* we could get no asoc if it is a O-O-T-B packet */
3850 			tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3851 		}
3852 		ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
3853 		if (port) {
3854 			ip6h->ip6_nxt = IPPROTO_UDP;
3855 		} else {
3856 			ip6h->ip6_nxt = IPPROTO_SCTP;
3857 		}
3858 		ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
3859 		ip6h->ip6_dst = sin6->sin6_addr;
3860 
3861 		/*
3862 		 * Add SRC address selection here: we can only reuse to a
3863 		 * limited degree the kame src-addr-sel, since we can try
3864 		 * their selection but it may not be bound.
3865 		 */
3866 		bzero(&lsa6_tmp, sizeof(lsa6_tmp));
3867 		lsa6_tmp.sin6_family = AF_INET6;
3868 		lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
3869 		lsa6 = &lsa6_tmp;
3870 		if (net && out_of_asoc_ok == 0) {
3871 			if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3872 				sctp_free_ifa(net->ro._s_addr);
3873 				net->ro._s_addr = NULL;
3874 				net->src_addr_selected = 0;
3875 				if (ro->ro_rt) {
3876 					RTFREE(ro->ro_rt);
3877 					ro->ro_rt = NULL;
3878 				}
3879 			}
3880 			if (net->src_addr_selected == 0) {
3881 				sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3882 				/* KAME hack: embed scopeid */
3883 				if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
3884 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3885 					return (EINVAL);
3886 				}
3887 				/* Cache the source address */
3888 				net->ro._s_addr = sctp_source_address_selection(inp,
3889 				    stcb,
3890 				    ro,
3891 				    net,
3892 				    0,
3893 				    vrf_id);
3894 				(void)sa6_recoverscope(sin6);
3895 				net->src_addr_selected = 1;
3896 			}
3897 			if (net->ro._s_addr == NULL) {
3898 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
3899 				net->src_addr_selected = 0;
3900 				goto no_route;
3901 			}
3902 			lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
3903 		} else {
3904 			sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
3905 			/* KAME hack: embed scopeid */
3906 			if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
3907 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3908 				return (EINVAL);
3909 			}
3910 			if (over_addr == NULL) {
3911 				struct sctp_ifa *_lsrc;
3912 
3913 				_lsrc = sctp_source_address_selection(inp, stcb, ro,
3914 				    net,
3915 				    out_of_asoc_ok,
3916 				    vrf_id);
3917 				if (_lsrc == NULL) {
3918 					goto no_route;
3919 				}
3920 				lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
3921 				sctp_free_ifa(_lsrc);
3922 			} else {
3923 				lsa6->sin6_addr = over_addr->sin6.sin6_addr;
3924 				SCTP_RTALLOC((&ro->ro_rt), vrf_id);
3925 			}
3926 			(void)sa6_recoverscope(sin6);
3927 		}
3928 		lsa6->sin6_port = inp->sctp_lport;
3929 
3930 		if (ro->ro_rt == NULL) {
3931 			/*
3932 			 * src addr selection failed to find a route (or
3933 			 * valid source addr), so we can't get there from
3934 			 * here!
3935 			 */
3936 			goto no_route;
3937 		}
3938 		/*
3939 		 * XXX: sa6 may not have a valid sin6_scope_id in the
3940 		 * non-SCOPEDROUTING case.
3941 		 */
3942 		bzero(&lsa6_storage, sizeof(lsa6_storage));
3943 		lsa6_storage.sin6_family = AF_INET6;
3944 		lsa6_storage.sin6_len = sizeof(lsa6_storage);
3945 		lsa6_storage.sin6_addr = lsa6->sin6_addr;
3946 		if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
3947 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
3948 			sctp_m_freem(m);
3949 			return (error);
3950 		}
3951 		/* XXX */
3952 		lsa6_storage.sin6_addr = lsa6->sin6_addr;
3953 		lsa6_storage.sin6_port = inp->sctp_lport;
3954 		lsa6 = &lsa6_storage;
3955 		ip6h->ip6_src = lsa6->sin6_addr;
3956 
3957 		if (port) {
3958 			udp = (struct udphdr *)(ip6h + 1);
3959 			udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3960 			udp->uh_dport = port;
3961 			udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
3962 			udp->uh_sum = 0;
3963 		}
3964 		/*
3965 		 * We set the hop limit now since there is a good chance
3966 		 * that our ro pointer is now filled
3967 		 */
3968 		ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
3969 		ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
3970 
3971 #ifdef SCTP_DEBUG
3972 		/* Copy to be sure something bad is not happening */
3973 		sin6->sin6_addr = ip6h->ip6_dst;
3974 		lsa6->sin6_addr = ip6h->ip6_src;
3975 #endif
3976 
3977 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
3978 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
3979 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
3980 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
3981 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
3982 		if (net) {
3983 			sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3984 			/* preserve the port and scope for link local send */
3985 			prev_scope = sin6->sin6_scope_id;
3986 			prev_port = sin6->sin6_port;
3987 		}
3988 		if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3989 			/* failed to prepend data, give up */
3990 			sctp_m_freem(m);
3991 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3992 			return (ENOMEM);
3993 		}
3994 #ifdef  SCTP_PACKET_LOGGING
3995 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
3996 			sctp_packet_log(m, packet_length);
3997 #endif
3998 		SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3999 		if (port) {
4000 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4001 				udp->uh_sum = 0xffff;
4002 			}
4003 		}
4004 		/* send it out. table id is taken from stcb */
4005 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4006 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4007 			so = SCTP_INP_SO(inp);
4008 			SCTP_SOCKET_UNLOCK(so, 0);
4009 		}
4010 #endif
4011 		SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4012 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4013 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4014 			atomic_add_int(&stcb->asoc.refcnt, 1);
4015 			SCTP_TCB_UNLOCK(stcb);
4016 			SCTP_SOCKET_LOCK(so, 0);
4017 			SCTP_TCB_LOCK(stcb);
4018 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
4019 		}
4020 #endif
4021 		if (net) {
4022 			/* for link local this must be done */
4023 			sin6->sin6_scope_id = prev_scope;
4024 			sin6->sin6_port = prev_port;
4025 		}
4026 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4027 		SCTP_STAT_INCR(sctps_sendpackets);
4028 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4029 		if (ret) {
4030 			SCTP_STAT_INCR(sctps_senderrors);
4031 		}
4032 		if (net == NULL) {
4033 			/* Now if we had a temp route free it */
4034 			if (ro->ro_rt) {
4035 				RTFREE(ro->ro_rt);
4036 			}
4037 		} else {
4038 			/* PMTU check versus smallest asoc MTU goes here */
4039 			if (ro->ro_rt == NULL) {
4040 				/* Route was freed */
4041 				if (net->ro._s_addr &&
4042 				    net->src_addr_selected) {
4043 					sctp_free_ifa(net->ro._s_addr);
4044 					net->ro._s_addr = NULL;
4045 				}
4046 				net->src_addr_selected = 0;
4047 			}
4048 			if ((ro->ro_rt != NULL) &&
4049 			    (net->ro._s_addr)) {
4050 				uint32_t mtu;
4051 
4052 				mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4053 				if (mtu &&
4054 				    (stcb->asoc.smallest_mtu > mtu)) {
4055 #ifdef SCTP_PRINT_FOR_B_AND_M
4056 					SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
4057 					    mtu);
4058 #endif
4059 					sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4060 					net->mtu = mtu;
4061 				}
4062 			} else if (ifp) {
4063 				if (ND_IFINFO(ifp)->linkmtu &&
4064 				    (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4065 #ifdef SCTP_PRINT_FOR_B_AND_M
4066 					SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
4067 					    ND_IFINFO(ifp)->linkmtu);
4068 #endif
4069 					sctp_mtu_size_reset(inp,
4070 					    &stcb->asoc,
4071 					    ND_IFINFO(ifp)->linkmtu);
4072 				}
4073 			}
4074 		}
4075 		return (ret);
4076 	}
4077 #endif
4078 	else {
4079 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4080 		    ((struct sockaddr *)to)->sa_family);
4081 		sctp_m_freem(m);
4082 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4083 		return (EFAULT);
4084 	}
4085 }
4086 
4087 
4088 void
4089 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4090 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4091     SCTP_UNUSED
4092 #endif
4093 )
4094 {
4095 	struct mbuf *m, *m_at, *mp_last;
4096 	struct sctp_nets *net;
4097 	struct sctp_init_msg *initm;
4098 	struct sctp_supported_addr_param *sup_addr;
4099 	struct sctp_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, MODULE_GLOBAL(MOD_INET6, 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, MODULE_GLOBAL(MOD_INET6, 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 				/*
5765 				 * TODO sctp_constants.h needs alternative
5766 				 * time macros when _KERNEL is undefined.
5767 				 */
5768 				timevaladd(&sp->ts, &tv);
5769 			}
5770 			break;
5771 		case CHUNK_FLAGS_PR_SCTP_RTX:
5772 			/*
5773 			 * Time to live is a the number or retransmissions
5774 			 * stored in tv_sec.
5775 			 */
5776 			sp->ts.tv_sec = sp->timetolive;
5777 			sp->ts.tv_usec = 0;
5778 			break;
5779 		default:
5780 			SCTPDBG(SCTP_DEBUG_USRREQ1,
5781 			    "Unknown PR_SCTP policy %u.\n",
5782 			    PR_SCTP_POLICY(sp->sinfo_flags));
5783 			break;
5784 		}
5785 	}
5786 }
5787 
5788 static int
5789 sctp_msg_append(struct sctp_tcb *stcb,
5790     struct sctp_nets *net,
5791     struct mbuf *m,
5792     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5793 {
5794 	int error = 0, holds_lock;
5795 	struct mbuf *at;
5796 	struct sctp_stream_queue_pending *sp = NULL;
5797 	struct sctp_stream_out *strm;
5798 
5799 	/*
5800 	 * Given an mbuf chain, put it into the association send queue and
5801 	 * place it on the wheel
5802 	 */
5803 	holds_lock = hold_stcb_lock;
5804 	if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5805 		/* Invalid stream number */
5806 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5807 		error = EINVAL;
5808 		goto out_now;
5809 	}
5810 	if ((stcb->asoc.stream_locked) &&
5811 	    (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5812 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5813 		error = EINVAL;
5814 		goto out_now;
5815 	}
5816 	strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5817 	/* Now can we send this? */
5818 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5819 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5820 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5821 	    (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5822 		/* got data while shutting down */
5823 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
5824 		error = ECONNRESET;
5825 		goto out_now;
5826 	}
5827 	sctp_alloc_a_strmoq(stcb, sp);
5828 	if (sp == NULL) {
5829 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5830 		error = ENOMEM;
5831 		goto out_now;
5832 	}
5833 	sp->sinfo_flags = srcv->sinfo_flags;
5834 	sp->timetolive = srcv->sinfo_timetolive;
5835 	sp->ppid = srcv->sinfo_ppid;
5836 	sp->context = srcv->sinfo_context;
5837 	sp->strseq = 0;
5838 	if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5839 		sp->net = net;
5840 		sp->addr_over = 1;
5841 	} else {
5842 		sp->net = stcb->asoc.primary_destination;
5843 		sp->addr_over = 0;
5844 	}
5845 	atomic_add_int(&sp->net->ref_count, 1);
5846 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5847 	sp->stream = srcv->sinfo_stream;
5848 	sp->msg_is_complete = 1;
5849 	sp->sender_all_done = 1;
5850 	sp->some_taken = 0;
5851 	sp->data = m;
5852 	sp->tail_mbuf = NULL;
5853 	sp->length = 0;
5854 	at = m;
5855 	sctp_set_prsctp_policy(stcb, sp);
5856 	/*
5857 	 * We could in theory (for sendall) sifa the length in, but we would
5858 	 * still have to hunt through the chain since we need to setup the
5859 	 * tail_mbuf
5860 	 */
5861 	while (at) {
5862 		if (SCTP_BUF_NEXT(at) == NULL)
5863 			sp->tail_mbuf = at;
5864 		sp->length += SCTP_BUF_LEN(at);
5865 		at = SCTP_BUF_NEXT(at);
5866 	}
5867 	SCTP_TCB_SEND_LOCK(stcb);
5868 	sctp_snd_sb_alloc(stcb, sp->length);
5869 	atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
5870 	TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
5871 	if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
5872 		sp->strseq = strm->next_sequence_sent;
5873 		strm->next_sequence_sent++;
5874 	}
5875 	if ((strm->next_spoke.tqe_next == NULL) &&
5876 	    (strm->next_spoke.tqe_prev == NULL)) {
5877 		/* Not on wheel, insert */
5878 		sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
5879 	}
5880 	m = NULL;
5881 	SCTP_TCB_SEND_UNLOCK(stcb);
5882 out_now:
5883 	if (m) {
5884 		sctp_m_freem(m);
5885 	}
5886 	return (error);
5887 }
5888 
5889 
5890 static struct mbuf *
5891 sctp_copy_mbufchain(struct mbuf *clonechain,
5892     struct mbuf *outchain,
5893     struct mbuf **endofchain,
5894     int can_take_mbuf,
5895     int sizeofcpy,
5896     uint8_t copy_by_ref)
5897 {
5898 	struct mbuf *m;
5899 	struct mbuf *appendchain;
5900 	caddr_t cp;
5901 	int len;
5902 
5903 	if (endofchain == NULL) {
5904 		/* error */
5905 error_out:
5906 		if (outchain)
5907 			sctp_m_freem(outchain);
5908 		return (NULL);
5909 	}
5910 	if (can_take_mbuf) {
5911 		appendchain = clonechain;
5912 	} else {
5913 		if (!copy_by_ref &&
5914 		    (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
5915 		    ) {
5916 			/* Its not in a cluster */
5917 			if (*endofchain == NULL) {
5918 				/* lets get a mbuf cluster */
5919 				if (outchain == NULL) {
5920 					/* This is the general case */
5921 			new_mbuf:
5922 					outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5923 					if (outchain == NULL) {
5924 						goto error_out;
5925 					}
5926 					SCTP_BUF_LEN(outchain) = 0;
5927 					*endofchain = outchain;
5928 					/* get the prepend space */
5929 					SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
5930 				} else {
5931 					/*
5932 					 * We really should not get a NULL
5933 					 * in endofchain
5934 					 */
5935 					/* find end */
5936 					m = outchain;
5937 					while (m) {
5938 						if (SCTP_BUF_NEXT(m) == NULL) {
5939 							*endofchain = m;
5940 							break;
5941 						}
5942 						m = SCTP_BUF_NEXT(m);
5943 					}
5944 					/* sanity */
5945 					if (*endofchain == NULL) {
5946 						/*
5947 						 * huh, TSNH XXX maybe we
5948 						 * should panic
5949 						 */
5950 						sctp_m_freem(outchain);
5951 						goto new_mbuf;
5952 					}
5953 				}
5954 				/* get the new end of length */
5955 				len = M_TRAILINGSPACE(*endofchain);
5956 			} else {
5957 				/* how much is left at the end? */
5958 				len = M_TRAILINGSPACE(*endofchain);
5959 			}
5960 			/* Find the end of the data, for appending */
5961 			cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
5962 
5963 			/* Now lets copy it out */
5964 			if (len >= sizeofcpy) {
5965 				/* It all fits, copy it in */
5966 				m_copydata(clonechain, 0, sizeofcpy, cp);
5967 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5968 			} else {
5969 				/* fill up the end of the chain */
5970 				if (len > 0) {
5971 					m_copydata(clonechain, 0, len, cp);
5972 					SCTP_BUF_LEN((*endofchain)) += len;
5973 					/* now we need another one */
5974 					sizeofcpy -= len;
5975 				}
5976 				m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5977 				if (m == NULL) {
5978 					/* We failed */
5979 					goto error_out;
5980 				}
5981 				SCTP_BUF_NEXT((*endofchain)) = m;
5982 				*endofchain = m;
5983 				cp = mtod((*endofchain), caddr_t);
5984 				m_copydata(clonechain, len, sizeofcpy, cp);
5985 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5986 			}
5987 			return (outchain);
5988 		} else {
5989 			/* copy the old fashion way */
5990 			appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
5991 #ifdef SCTP_MBUF_LOGGING
5992 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5993 				struct mbuf *mat;
5994 
5995 				mat = appendchain;
5996 				while (mat) {
5997 					if (SCTP_BUF_IS_EXTENDED(mat)) {
5998 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5999 					}
6000 					mat = SCTP_BUF_NEXT(mat);
6001 				}
6002 			}
6003 #endif
6004 		}
6005 	}
6006 	if (appendchain == NULL) {
6007 		/* error */
6008 		if (outchain)
6009 			sctp_m_freem(outchain);
6010 		return (NULL);
6011 	}
6012 	if (outchain) {
6013 		/* tack on to the end */
6014 		if (*endofchain != NULL) {
6015 			SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6016 		} else {
6017 			m = outchain;
6018 			while (m) {
6019 				if (SCTP_BUF_NEXT(m) == NULL) {
6020 					SCTP_BUF_NEXT(m) = appendchain;
6021 					break;
6022 				}
6023 				m = SCTP_BUF_NEXT(m);
6024 			}
6025 		}
6026 		/*
6027 		 * save off the end and update the end-chain postion
6028 		 */
6029 		m = appendchain;
6030 		while (m) {
6031 			if (SCTP_BUF_NEXT(m) == NULL) {
6032 				*endofchain = m;
6033 				break;
6034 			}
6035 			m = SCTP_BUF_NEXT(m);
6036 		}
6037 		return (outchain);
6038 	} else {
6039 		/* save off the end and update the end-chain postion */
6040 		m = appendchain;
6041 		while (m) {
6042 			if (SCTP_BUF_NEXT(m) == NULL) {
6043 				*endofchain = m;
6044 				break;
6045 			}
6046 			m = SCTP_BUF_NEXT(m);
6047 		}
6048 		return (appendchain);
6049 	}
6050 }
6051 
6052 int
6053 sctp_med_chunk_output(struct sctp_inpcb *inp,
6054     struct sctp_tcb *stcb,
6055     struct sctp_association *asoc,
6056     int *num_out,
6057     int *reason_code,
6058     int control_only, int *cwnd_full, int from_where,
6059     struct timeval *now, int *now_filled, int frag_point, int so_locked
6060 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6061     SCTP_UNUSED
6062 #endif
6063 );
6064 
6065 static void
6066 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6067     uint32_t val)
6068 {
6069 	struct sctp_copy_all *ca;
6070 	struct mbuf *m;
6071 	int ret = 0;
6072 	int added_control = 0;
6073 	int un_sent, do_chunk_output = 1;
6074 	struct sctp_association *asoc;
6075 
6076 	ca = (struct sctp_copy_all *)ptr;
6077 	if (ca->m == NULL) {
6078 		return;
6079 	}
6080 	if (ca->inp != inp) {
6081 		/* TSNH */
6082 		return;
6083 	}
6084 	if ((ca->m) && ca->sndlen) {
6085 		m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6086 		if (m == NULL) {
6087 			/* can't copy so we are done */
6088 			ca->cnt_failed++;
6089 			return;
6090 		}
6091 #ifdef SCTP_MBUF_LOGGING
6092 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6093 			struct mbuf *mat;
6094 
6095 			mat = m;
6096 			while (mat) {
6097 				if (SCTP_BUF_IS_EXTENDED(mat)) {
6098 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6099 				}
6100 				mat = SCTP_BUF_NEXT(mat);
6101 			}
6102 		}
6103 #endif
6104 	} else {
6105 		m = NULL;
6106 	}
6107 	SCTP_TCB_LOCK_ASSERT(stcb);
6108 	if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6109 		/* Abort this assoc with m as the user defined reason */
6110 		if (m) {
6111 			struct sctp_paramhdr *ph;
6112 
6113 			SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6114 			if (m) {
6115 				ph = mtod(m, struct sctp_paramhdr *);
6116 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6117 				ph->param_length = htons(ca->sndlen);
6118 			}
6119 			/*
6120 			 * We add one here to keep the assoc from
6121 			 * dis-appearing on us.
6122 			 */
6123 			atomic_add_int(&stcb->asoc.refcnt, 1);
6124 			sctp_abort_an_association(inp, stcb,
6125 			    SCTP_RESPONSE_TO_USER_REQ,
6126 			    m, SCTP_SO_NOT_LOCKED);
6127 			/*
6128 			 * sctp_abort_an_association calls sctp_free_asoc()
6129 			 * free association will NOT free it since we
6130 			 * incremented the refcnt .. we do this to prevent
6131 			 * it being freed and things getting tricky since we
6132 			 * could end up (from free_asoc) calling inpcb_free
6133 			 * which would get a recursive lock call to the
6134 			 * iterator lock.. But as a consequence of that the
6135 			 * stcb will return to us un-locked.. since
6136 			 * free_asoc returns with either no TCB or the TCB
6137 			 * unlocked, we must relock.. to unlock in the
6138 			 * iterator timer :-0
6139 			 */
6140 			SCTP_TCB_LOCK(stcb);
6141 			atomic_add_int(&stcb->asoc.refcnt, -1);
6142 			goto no_chunk_output;
6143 		}
6144 	} else {
6145 		if (m) {
6146 			ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
6147 			    &ca->sndrcv, 1);
6148 		}
6149 		asoc = &stcb->asoc;
6150 		if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6151 			/* shutdown this assoc */
6152 			int cnt;
6153 
6154 			cnt = sctp_is_there_unsent_data(stcb);
6155 
6156 			if (TAILQ_EMPTY(&asoc->send_queue) &&
6157 			    TAILQ_EMPTY(&asoc->sent_queue) &&
6158 			    (cnt == 0)) {
6159 				if (asoc->locked_on_sending) {
6160 					goto abort_anyway;
6161 				}
6162 				/*
6163 				 * there is nothing queued to send, so I'm
6164 				 * done...
6165 				 */
6166 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6167 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6168 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6169 					/*
6170 					 * only send SHUTDOWN the first time
6171 					 * through
6172 					 */
6173 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
6174 					if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6175 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6176 					}
6177 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6178 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6179 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6180 					    asoc->primary_destination);
6181 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6182 					    asoc->primary_destination);
6183 					added_control = 1;
6184 					do_chunk_output = 0;
6185 				}
6186 			} else {
6187 				/*
6188 				 * we still got (or just got) data to send,
6189 				 * so set SHUTDOWN_PENDING
6190 				 */
6191 				/*
6192 				 * XXX sockets draft says that SCTP_EOF
6193 				 * should be sent with no data.  currently,
6194 				 * we will allow user data to be sent first
6195 				 * and move to SHUTDOWN-PENDING
6196 				 */
6197 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6198 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6199 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6200 					if (asoc->locked_on_sending) {
6201 						/*
6202 						 * Locked to send out the
6203 						 * data
6204 						 */
6205 						struct sctp_stream_queue_pending *sp;
6206 
6207 						sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6208 						if (sp) {
6209 							if ((sp->length == 0) && (sp->msg_is_complete == 0))
6210 								asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6211 						}
6212 					}
6213 					asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6214 					if (TAILQ_EMPTY(&asoc->send_queue) &&
6215 					    TAILQ_EMPTY(&asoc->sent_queue) &&
6216 					    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6217 				abort_anyway:
6218 						atomic_add_int(&stcb->asoc.refcnt, 1);
6219 						sctp_abort_an_association(stcb->sctp_ep, stcb,
6220 						    SCTP_RESPONSE_TO_USER_REQ,
6221 						    NULL, SCTP_SO_NOT_LOCKED);
6222 						atomic_add_int(&stcb->asoc.refcnt, -1);
6223 						goto no_chunk_output;
6224 					}
6225 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6226 					    asoc->primary_destination);
6227 				}
6228 			}
6229 
6230 		}
6231 	}
6232 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6233 	    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6234 
6235 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6236 	    (stcb->asoc.total_flight > 0) &&
6237 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6238 	    ) {
6239 		do_chunk_output = 0;
6240 	}
6241 	if (do_chunk_output)
6242 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6243 	else if (added_control) {
6244 		int num_out = 0, reason = 0, cwnd_full = 0, now_filled = 0;
6245 		struct timeval now;
6246 		int frag_point;
6247 
6248 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6249 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6250 		    &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6251 	}
6252 no_chunk_output:
6253 	if (ret) {
6254 		ca->cnt_failed++;
6255 	} else {
6256 		ca->cnt_sent++;
6257 	}
6258 }
6259 
6260 static void
6261 sctp_sendall_completes(void *ptr, uint32_t val)
6262 {
6263 	struct sctp_copy_all *ca;
6264 
6265 	ca = (struct sctp_copy_all *)ptr;
6266 	/*
6267 	 * Do a notify here? Kacheong suggests that the notify be done at
6268 	 * the send time.. so you would push up a notification if any send
6269 	 * failed. Don't know if this is feasable since the only failures we
6270 	 * have is "memory" related and if you cannot get an mbuf to send
6271 	 * the data you surely can't get an mbuf to send up to notify the
6272 	 * user you can't send the data :->
6273 	 */
6274 
6275 	/* now free everything */
6276 	sctp_m_freem(ca->m);
6277 	SCTP_FREE(ca, SCTP_M_COPYAL);
6278 }
6279 
6280 
6281 #define	MC_ALIGN(m, len) do {						\
6282 	SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1));	\
6283 } while (0)
6284 
6285 
6286 
6287 static struct mbuf *
6288 sctp_copy_out_all(struct uio *uio, int len)
6289 {
6290 	struct mbuf *ret, *at;
6291 	int left, willcpy, cancpy, error;
6292 
6293 	ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6294 	if (ret == NULL) {
6295 		/* TSNH */
6296 		return (NULL);
6297 	}
6298 	left = len;
6299 	SCTP_BUF_LEN(ret) = 0;
6300 	/* save space for the data chunk header */
6301 	cancpy = M_TRAILINGSPACE(ret);
6302 	willcpy = min(cancpy, left);
6303 	at = ret;
6304 	while (left > 0) {
6305 		/* Align data to the end */
6306 		error = uiomove(mtod(at, caddr_t), willcpy, uio);
6307 		if (error) {
6308 	err_out_now:
6309 			sctp_m_freem(at);
6310 			return (NULL);
6311 		}
6312 		SCTP_BUF_LEN(at) = willcpy;
6313 		SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6314 		left -= willcpy;
6315 		if (left > 0) {
6316 			SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6317 			if (SCTP_BUF_NEXT(at) == NULL) {
6318 				goto err_out_now;
6319 			}
6320 			at = SCTP_BUF_NEXT(at);
6321 			SCTP_BUF_LEN(at) = 0;
6322 			cancpy = M_TRAILINGSPACE(at);
6323 			willcpy = min(cancpy, left);
6324 		}
6325 	}
6326 	return (ret);
6327 }
6328 
6329 static int
6330 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6331     struct sctp_sndrcvinfo *srcv)
6332 {
6333 	int ret;
6334 	struct sctp_copy_all *ca;
6335 
6336 	SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6337 	    SCTP_M_COPYAL);
6338 	if (ca == NULL) {
6339 		sctp_m_freem(m);
6340 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6341 		return (ENOMEM);
6342 	}
6343 	memset(ca, 0, sizeof(struct sctp_copy_all));
6344 
6345 	ca->inp = inp;
6346 	memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6347 	/*
6348 	 * take off the sendall flag, it would be bad if we failed to do
6349 	 * this :-0
6350 	 */
6351 	ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6352 	/* get length and mbuf chain */
6353 	if (uio) {
6354 		ca->sndlen = uio->uio_resid;
6355 		ca->m = sctp_copy_out_all(uio, ca->sndlen);
6356 		if (ca->m == NULL) {
6357 			SCTP_FREE(ca, SCTP_M_COPYAL);
6358 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6359 			return (ENOMEM);
6360 		}
6361 	} else {
6362 		/* Gather the length of the send */
6363 		struct mbuf *mat;
6364 
6365 		mat = m;
6366 		ca->sndlen = 0;
6367 		while (m) {
6368 			ca->sndlen += SCTP_BUF_LEN(m);
6369 			m = SCTP_BUF_NEXT(m);
6370 		}
6371 		ca->m = mat;
6372 	}
6373 	ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6374 	    SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6375 	    SCTP_ASOC_ANY_STATE,
6376 	    (void *)ca, 0,
6377 	    sctp_sendall_completes, inp, 1);
6378 	if (ret) {
6379 		SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6380 		SCTP_FREE(ca, SCTP_M_COPYAL);
6381 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6382 		return (EFAULT);
6383 	}
6384 	return (0);
6385 }
6386 
6387 
6388 void
6389 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6390 {
6391 	struct sctp_tmit_chunk *chk, *nchk;
6392 
6393 	chk = TAILQ_FIRST(&asoc->control_send_queue);
6394 	while (chk) {
6395 		nchk = TAILQ_NEXT(chk, sctp_next);
6396 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6397 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6398 			if (chk->data) {
6399 				sctp_m_freem(chk->data);
6400 				chk->data = NULL;
6401 			}
6402 			asoc->ctrl_queue_cnt--;
6403 			sctp_free_a_chunk(stcb, chk);
6404 		}
6405 		chk = nchk;
6406 	}
6407 }
6408 
6409 void
6410 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6411 {
6412 	struct sctp_association *asoc;
6413 	struct sctp_tmit_chunk *chk, *chk_tmp;
6414 	struct sctp_asconf_chunk *acp;
6415 
6416 	asoc = &stcb->asoc;
6417 	for (chk = TAILQ_FIRST(&asoc->asconf_send_queue); chk != NULL;
6418 	    chk = chk_tmp) {
6419 		/* get next chk */
6420 		chk_tmp = TAILQ_NEXT(chk, sctp_next);
6421 		/* find SCTP_ASCONF chunk in queue */
6422 		if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6423 			if (chk->data) {
6424 				acp = mtod(chk->data, struct sctp_asconf_chunk *);
6425 				if (compare_with_wrap(ntohl(acp->serial_number), stcb->asoc.asconf_seq_out_acked, MAX_SEQ)) {
6426 					/* Not Acked yet */
6427 					break;
6428 				}
6429 			}
6430 			TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6431 			if (chk->data) {
6432 				sctp_m_freem(chk->data);
6433 				chk->data = NULL;
6434 			}
6435 			asoc->ctrl_queue_cnt--;
6436 			sctp_free_a_chunk(stcb, chk);
6437 		}
6438 	}
6439 }
6440 
6441 
6442 static void
6443 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6444 
6445     struct sctp_association *asoc,
6446     struct sctp_tmit_chunk **data_list,
6447     int bundle_at,
6448     struct sctp_nets *net)
6449 {
6450 	int i;
6451 	struct sctp_tmit_chunk *tp1;
6452 
6453 	for (i = 0; i < bundle_at; i++) {
6454 		/* off of the send queue */
6455 		if (i) {
6456 			/*
6457 			 * Any chunk NOT 0 you zap the time chunk 0 gets
6458 			 * zapped or set based on if a RTO measurment is
6459 			 * needed.
6460 			 */
6461 			data_list[i]->do_rtt = 0;
6462 		}
6463 		/* record time */
6464 		data_list[i]->sent_rcv_time = net->last_sent_time;
6465 		data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6466 		TAILQ_REMOVE(&asoc->send_queue,
6467 		    data_list[i],
6468 		    sctp_next);
6469 		/* on to the sent queue */
6470 		tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6471 		if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
6472 		    data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
6473 			struct sctp_tmit_chunk *tpp;
6474 
6475 			/* need to move back */
6476 	back_up_more:
6477 			tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6478 			if (tpp == NULL) {
6479 				TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6480 				goto all_done;
6481 			}
6482 			tp1 = tpp;
6483 			if (compare_with_wrap(tp1->rec.data.TSN_seq,
6484 			    data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
6485 				goto back_up_more;
6486 			}
6487 			TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6488 		} else {
6489 			TAILQ_INSERT_TAIL(&asoc->sent_queue,
6490 			    data_list[i],
6491 			    sctp_next);
6492 		}
6493 all_done:
6494 		/* This does not lower until the cum-ack passes it */
6495 		asoc->sent_queue_cnt++;
6496 		asoc->send_queue_cnt--;
6497 		if ((asoc->peers_rwnd <= 0) &&
6498 		    (asoc->total_flight == 0) &&
6499 		    (bundle_at == 1)) {
6500 			/* Mark the chunk as being a window probe */
6501 			SCTP_STAT_INCR(sctps_windowprobed);
6502 		}
6503 #ifdef SCTP_AUDITING_ENABLED
6504 		sctp_audit_log(0xC2, 3);
6505 #endif
6506 		data_list[i]->sent = SCTP_DATAGRAM_SENT;
6507 		data_list[i]->snd_count = 1;
6508 		data_list[i]->rec.data.chunk_was_revoked = 0;
6509 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6510 			sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6511 			    data_list[i]->whoTo->flight_size,
6512 			    data_list[i]->book_size,
6513 			    (uintptr_t) data_list[i]->whoTo,
6514 			    data_list[i]->rec.data.TSN_seq);
6515 		}
6516 		sctp_flight_size_increase(data_list[i]);
6517 		sctp_total_flight_increase(stcb, data_list[i]);
6518 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6519 			sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6520 			    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6521 		}
6522 		asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6523 		    (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6524 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6525 			/* SWS sender side engages */
6526 			asoc->peers_rwnd = 0;
6527 		}
6528 	}
6529 }
6530 
6531 static void
6532 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
6533 {
6534 	struct sctp_tmit_chunk *chk, *nchk;
6535 
6536 	for (chk = TAILQ_FIRST(&asoc->control_send_queue);
6537 	    chk; chk = nchk) {
6538 		nchk = TAILQ_NEXT(chk, sctp_next);
6539 		if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6540 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6541 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6542 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6543 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6544 		    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6545 		    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6546 		    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6547 		    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6548 		    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6549 			/* Stray chunks must be cleaned up */
6550 	clean_up_anyway:
6551 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6552 			if (chk->data) {
6553 				sctp_m_freem(chk->data);
6554 				chk->data = NULL;
6555 			}
6556 			asoc->ctrl_queue_cnt--;
6557 			sctp_free_a_chunk(stcb, chk);
6558 		} else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6559 			/* special handling, we must look into the param */
6560 			if (chk != asoc->str_reset) {
6561 				goto clean_up_anyway;
6562 			}
6563 		}
6564 	}
6565 }
6566 
6567 
6568 static int
6569 sctp_can_we_split_this(struct sctp_tcb *stcb,
6570     uint32_t length,
6571     uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6572 {
6573 	/*
6574 	 * Make a decision on if I should split a msg into multiple parts.
6575 	 * This is only asked of incomplete messages.
6576 	 */
6577 	if (eeor_on) {
6578 		/*
6579 		 * If we are doing EEOR we need to always send it if its the
6580 		 * entire thing, since it might be all the guy is putting in
6581 		 * the hopper.
6582 		 */
6583 		if (goal_mtu >= length) {
6584 			/*-
6585 			 * If we have data outstanding,
6586 			 * we get another chance when the sack
6587 			 * arrives to transmit - wait for more data
6588 			 */
6589 			if (stcb->asoc.total_flight == 0) {
6590 				/*
6591 				 * If nothing is in flight, we zero the
6592 				 * packet counter.
6593 				 */
6594 				return (length);
6595 			}
6596 			return (0);
6597 
6598 		} else {
6599 			/* You can fill the rest */
6600 			return (goal_mtu);
6601 		}
6602 	}
6603 	/*-
6604 	 * For those strange folk that make the send buffer
6605 	 * smaller than our fragmentation point, we can't
6606 	 * get a full msg in so we have to allow splitting.
6607 	 */
6608 	if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
6609 		return (length);
6610 	}
6611 	if ((length <= goal_mtu) ||
6612 	    ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
6613 		/* Sub-optimial residual don't split in non-eeor mode. */
6614 		return (0);
6615 	}
6616 	/*
6617 	 * If we reach here length is larger than the goal_mtu. Do we wish
6618 	 * to split it for the sake of packet putting together?
6619 	 */
6620 	if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
6621 		/* Its ok to split it */
6622 		return (min(goal_mtu, frag_point));
6623 	}
6624 	/* Nope, can't split */
6625 	return (0);
6626 
6627 }
6628 
6629 static uint32_t
6630 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
6631     struct sctp_stream_out *strq,
6632     uint32_t goal_mtu,
6633     uint32_t frag_point,
6634     int *locked,
6635     int *giveup,
6636     int eeor_mode,
6637     int *bail)
6638 {
6639 	/* Move from the stream to the send_queue keeping track of the total */
6640 	struct sctp_association *asoc;
6641 	struct sctp_stream_queue_pending *sp;
6642 	struct sctp_tmit_chunk *chk;
6643 	struct sctp_data_chunk *dchkh;
6644 	uint32_t to_move, length;
6645 	uint8_t rcv_flags = 0;
6646 	uint8_t some_taken;
6647 	uint8_t send_lock_up = 0;
6648 
6649 	SCTP_TCB_LOCK_ASSERT(stcb);
6650 	asoc = &stcb->asoc;
6651 one_more_time:
6652 	/* sa_ignore FREED_MEMORY */
6653 	sp = TAILQ_FIRST(&strq->outqueue);
6654 	if (sp == NULL) {
6655 		*locked = 0;
6656 		if (send_lock_up == 0) {
6657 			SCTP_TCB_SEND_LOCK(stcb);
6658 			send_lock_up = 1;
6659 		}
6660 		sp = TAILQ_FIRST(&strq->outqueue);
6661 		if (sp) {
6662 			goto one_more_time;
6663 		}
6664 		if (strq->last_msg_incomplete) {
6665 			SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6666 			    strq->stream_no,
6667 			    strq->last_msg_incomplete);
6668 			strq->last_msg_incomplete = 0;
6669 		}
6670 		to_move = 0;
6671 		if (send_lock_up) {
6672 			SCTP_TCB_SEND_UNLOCK(stcb);
6673 			send_lock_up = 0;
6674 		}
6675 		goto out_of;
6676 	}
6677 	if ((sp->msg_is_complete) && (sp->length == 0)) {
6678 		if (sp->sender_all_done) {
6679 			/*
6680 			 * We are doing differed cleanup. Last time through
6681 			 * when we took all the data the sender_all_done was
6682 			 * not set.
6683 			 */
6684 			if (sp->put_last_out == 0) {
6685 				SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6686 				SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6687 				    sp->sender_all_done,
6688 				    sp->length,
6689 				    sp->msg_is_complete,
6690 				    sp->put_last_out,
6691 				    send_lock_up);
6692 			}
6693 			if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
6694 				SCTP_TCB_SEND_LOCK(stcb);
6695 				send_lock_up = 1;
6696 			}
6697 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6698 			TAILQ_REMOVE(&strq->outqueue, sp, next);
6699 			sctp_free_remote_addr(sp->net);
6700 			if (sp->data) {
6701 				sctp_m_freem(sp->data);
6702 				sp->data = NULL;
6703 			}
6704 			sctp_free_a_strmoq(stcb, sp);
6705 
6706 			/* we can't be locked to it */
6707 			*locked = 0;
6708 			stcb->asoc.locked_on_sending = NULL;
6709 			if (send_lock_up) {
6710 				SCTP_TCB_SEND_UNLOCK(stcb);
6711 				send_lock_up = 0;
6712 			}
6713 			/* back to get the next msg */
6714 			goto one_more_time;
6715 		} else {
6716 			/*
6717 			 * sender just finished this but still holds a
6718 			 * reference
6719 			 */
6720 			*locked = 1;
6721 			*giveup = 1;
6722 			to_move = 0;
6723 			goto out_of;
6724 		}
6725 	} else {
6726 		/* is there some to get */
6727 		if (sp->length == 0) {
6728 			/* no */
6729 			*locked = 1;
6730 			*giveup = 1;
6731 			to_move = 0;
6732 			goto out_of;
6733 		}
6734 	}
6735 	some_taken = sp->some_taken;
6736 	if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6737 		sp->msg_is_complete = 1;
6738 	}
6739 re_look:
6740 	length = sp->length;
6741 	if (sp->msg_is_complete) {
6742 		/* The message is complete */
6743 		to_move = min(length, frag_point);
6744 		if (to_move == length) {
6745 			/* All of it fits in the MTU */
6746 			if (sp->some_taken) {
6747 				rcv_flags |= SCTP_DATA_LAST_FRAG;
6748 				sp->put_last_out = 1;
6749 			} else {
6750 				rcv_flags |= SCTP_DATA_NOT_FRAG;
6751 				sp->put_last_out = 1;
6752 			}
6753 		} else {
6754 			/* Not all of it fits, we fragment */
6755 			if (sp->some_taken == 0) {
6756 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
6757 			}
6758 			sp->some_taken = 1;
6759 		}
6760 	} else {
6761 		to_move = sctp_can_we_split_this(stcb, length, goal_mtu,
6762 		    frag_point, eeor_mode);
6763 		if (to_move) {
6764 			/*-
6765 		         * We use a snapshot of length in case it
6766 		         * is expanding during the compare.
6767 		         */
6768 			uint32_t llen;
6769 
6770 			llen = length;
6771 			if (to_move >= llen) {
6772 				to_move = llen;
6773 				if (send_lock_up == 0) {
6774 					/*-
6775 				         * We are taking all of an incomplete msg
6776 				         * thus we need a send lock.
6777 				         */
6778 					SCTP_TCB_SEND_LOCK(stcb);
6779 					send_lock_up = 1;
6780 					if (sp->msg_is_complete) {
6781 						/*
6782 						 * the sender finished the
6783 						 * msg
6784 						 */
6785 						goto re_look;
6786 					}
6787 				}
6788 			}
6789 			if (sp->some_taken == 0) {
6790 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
6791 				sp->some_taken = 1;
6792 			}
6793 		} else {
6794 			/* Nothing to take. */
6795 			if (sp->some_taken) {
6796 				*locked = 1;
6797 			}
6798 			*giveup = 1;
6799 			to_move = 0;
6800 			goto out_of;
6801 		}
6802 	}
6803 
6804 	/* If we reach here, we can copy out a chunk */
6805 	sctp_alloc_a_chunk(stcb, chk);
6806 	if (chk == NULL) {
6807 		/* No chunk memory */
6808 		*giveup = 1;
6809 		to_move = 0;
6810 		goto out_of;
6811 	}
6812 	/*
6813 	 * Setup for unordered if needed by looking at the user sent info
6814 	 * flags.
6815 	 */
6816 	if (sp->sinfo_flags & SCTP_UNORDERED) {
6817 		rcv_flags |= SCTP_DATA_UNORDERED;
6818 	}
6819 	/* clear out the chunk before setting up */
6820 	memset(chk, 0, sizeof(*chk));
6821 	chk->rec.data.rcv_flags = rcv_flags;
6822 
6823 	if (to_move >= length) {
6824 		/* we think we can steal the whole thing */
6825 		if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
6826 			SCTP_TCB_SEND_LOCK(stcb);
6827 			send_lock_up = 1;
6828 		}
6829 		if (to_move < sp->length) {
6830 			/* bail, it changed */
6831 			goto dont_do_it;
6832 		}
6833 		chk->data = sp->data;
6834 		chk->last_mbuf = sp->tail_mbuf;
6835 		/* register the stealing */
6836 		sp->data = sp->tail_mbuf = NULL;
6837 	} else {
6838 		struct mbuf *m;
6839 
6840 dont_do_it:
6841 		chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
6842 		chk->last_mbuf = NULL;
6843 		if (chk->data == NULL) {
6844 			sp->some_taken = some_taken;
6845 			sctp_free_a_chunk(stcb, chk);
6846 			*bail = 1;
6847 			to_move = 0;
6848 			goto out_of;
6849 		}
6850 #ifdef SCTP_MBUF_LOGGING
6851 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6852 			struct mbuf *mat;
6853 
6854 			mat = chk->data;
6855 			while (mat) {
6856 				if (SCTP_BUF_IS_EXTENDED(mat)) {
6857 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6858 				}
6859 				mat = SCTP_BUF_NEXT(mat);
6860 			}
6861 		}
6862 #endif
6863 		/* Pull off the data */
6864 		m_adj(sp->data, to_move);
6865 		/* Now lets work our way down and compact it */
6866 		m = sp->data;
6867 		while (m && (SCTP_BUF_LEN(m) == 0)) {
6868 			sp->data = SCTP_BUF_NEXT(m);
6869 			SCTP_BUF_NEXT(m) = NULL;
6870 			if (sp->tail_mbuf == m) {
6871 				/*-
6872 				 * Freeing tail? TSNH since
6873 				 * we supposedly were taking less
6874 				 * than the sp->length.
6875 				 */
6876 #ifdef INVARIANTS
6877 				panic("Huh, freing tail? - TSNH");
6878 #else
6879 				SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
6880 				sp->tail_mbuf = sp->data = NULL;
6881 				sp->length = 0;
6882 #endif
6883 
6884 			}
6885 			sctp_m_free(m);
6886 			m = sp->data;
6887 		}
6888 	}
6889 	if (SCTP_BUF_IS_EXTENDED(chk->data)) {
6890 		chk->copy_by_ref = 1;
6891 	} else {
6892 		chk->copy_by_ref = 0;
6893 	}
6894 	/*
6895 	 * get last_mbuf and counts of mb useage This is ugly but hopefully
6896 	 * its only one mbuf.
6897 	 */
6898 	if (chk->last_mbuf == NULL) {
6899 		chk->last_mbuf = chk->data;
6900 		while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
6901 			chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
6902 		}
6903 	}
6904 	if (to_move > length) {
6905 		/*- This should not happen either
6906 		 * since we always lower to_move to the size
6907 		 * of sp->length if its larger.
6908 		 */
6909 #ifdef INVARIANTS
6910 		panic("Huh, how can to_move be larger?");
6911 #else
6912 		SCTP_PRINTF("Huh, how can to_move be larger?\n");
6913 		sp->length = 0;
6914 #endif
6915 	} else {
6916 		atomic_subtract_int(&sp->length, to_move);
6917 	}
6918 	if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
6919 		/* Not enough room for a chunk header, get some */
6920 		struct mbuf *m;
6921 
6922 		m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
6923 		if (m == NULL) {
6924 			/*
6925 			 * we're in trouble here. _PREPEND below will free
6926 			 * all the data if there is no leading space, so we
6927 			 * must put the data back and restore.
6928 			 */
6929 			if (send_lock_up == 0) {
6930 				SCTP_TCB_SEND_LOCK(stcb);
6931 				send_lock_up = 1;
6932 			}
6933 			if (chk->data == NULL) {
6934 				/* unsteal the data */
6935 				sp->data = chk->data;
6936 				sp->tail_mbuf = chk->last_mbuf;
6937 			} else {
6938 				struct mbuf *m_tmp;
6939 
6940 				/* reassemble the data */
6941 				m_tmp = sp->data;
6942 				sp->data = chk->data;
6943 				SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
6944 			}
6945 			sp->some_taken = some_taken;
6946 			atomic_add_int(&sp->length, to_move);
6947 			chk->data = NULL;
6948 			*bail = 1;
6949 			sctp_free_a_chunk(stcb, chk);
6950 			to_move = 0;
6951 			goto out_of;
6952 		} else {
6953 			SCTP_BUF_LEN(m) = 0;
6954 			SCTP_BUF_NEXT(m) = chk->data;
6955 			chk->data = m;
6956 			M_ALIGN(chk->data, 4);
6957 		}
6958 	}
6959 	SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
6960 	if (chk->data == NULL) {
6961 		/* HELP, TSNH since we assured it would not above? */
6962 #ifdef INVARIANTS
6963 		panic("prepend failes HELP?");
6964 #else
6965 		SCTP_PRINTF("prepend fails HELP?\n");
6966 		sctp_free_a_chunk(stcb, chk);
6967 #endif
6968 		*bail = 1;
6969 		to_move = 0;
6970 		goto out_of;
6971 	}
6972 	sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
6973 	chk->book_size = chk->send_size = (to_move +
6974 	    sizeof(struct sctp_data_chunk));
6975 	chk->book_size_scale = 0;
6976 	chk->sent = SCTP_DATAGRAM_UNSENT;
6977 
6978 	chk->flags = 0;
6979 	chk->asoc = &stcb->asoc;
6980 	chk->pad_inplace = 0;
6981 	chk->no_fr_allowed = 0;
6982 	chk->rec.data.stream_seq = sp->strseq;
6983 	chk->rec.data.stream_number = sp->stream;
6984 	chk->rec.data.payloadtype = sp->ppid;
6985 	chk->rec.data.context = sp->context;
6986 	chk->rec.data.doing_fast_retransmit = 0;
6987 	chk->rec.data.ect_nonce = 0;	/* ECN Nonce */
6988 
6989 	chk->rec.data.timetodrop = sp->ts;
6990 	chk->flags = sp->act_flags;
6991 	chk->addr_over = sp->addr_over;
6992 
6993 	chk->whoTo = net;
6994 	atomic_add_int(&chk->whoTo->ref_count, 1);
6995 
6996 	chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
6997 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
6998 		sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
6999 		    (uintptr_t) stcb, sp->length,
7000 		    (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
7001 		    chk->rec.data.TSN_seq);
7002 	}
7003 	dchkh = mtod(chk->data, struct sctp_data_chunk *);
7004 	/*
7005 	 * Put the rest of the things in place now. Size was done earlier in
7006 	 * previous loop prior to padding.
7007 	 */
7008 
7009 #ifdef SCTP_ASOCLOG_OF_TSNS
7010 	SCTP_TCB_LOCK_ASSERT(stcb);
7011 	if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7012 		asoc->tsn_out_at = 0;
7013 		asoc->tsn_out_wrapped = 1;
7014 	}
7015 	asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7016 	asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7017 	asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7018 	asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7019 	asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7020 	asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7021 	asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7022 	asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7023 	asoc->tsn_out_at++;
7024 #endif
7025 
7026 	dchkh->ch.chunk_type = SCTP_DATA;
7027 	dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7028 	dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7029 	dchkh->dp.stream_id = htons(strq->stream_no);
7030 	dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7031 	dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7032 	dchkh->ch.chunk_length = htons(chk->send_size);
7033 	/* Now advance the chk->send_size by the actual pad needed. */
7034 	if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7035 		/* need a pad */
7036 		struct mbuf *lm;
7037 		int pads;
7038 
7039 		pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7040 		if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7041 			chk->pad_inplace = 1;
7042 		}
7043 		if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7044 			/* pad added an mbuf */
7045 			chk->last_mbuf = lm;
7046 		}
7047 		chk->send_size += pads;
7048 	}
7049 	/* We only re-set the policy if it is on */
7050 	if (sp->pr_sctp_on) {
7051 		sctp_set_prsctp_policy(stcb, sp);
7052 		asoc->pr_sctp_cnt++;
7053 		chk->pr_sctp_on = 1;
7054 	} else {
7055 		chk->pr_sctp_on = 0;
7056 	}
7057 	if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7058 		/* All done pull and kill the message */
7059 		atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7060 		if (sp->put_last_out == 0) {
7061 			SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7062 			SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7063 			    sp->sender_all_done,
7064 			    sp->length,
7065 			    sp->msg_is_complete,
7066 			    sp->put_last_out,
7067 			    send_lock_up);
7068 		}
7069 		if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7070 			SCTP_TCB_SEND_LOCK(stcb);
7071 			send_lock_up = 1;
7072 		}
7073 		TAILQ_REMOVE(&strq->outqueue, sp, next);
7074 		sctp_free_remote_addr(sp->net);
7075 		if (sp->data) {
7076 			sctp_m_freem(sp->data);
7077 			sp->data = NULL;
7078 		}
7079 		sctp_free_a_strmoq(stcb, sp);
7080 
7081 		/* we can't be locked to it */
7082 		*locked = 0;
7083 		stcb->asoc.locked_on_sending = NULL;
7084 	} else {
7085 		/* more to go, we are locked */
7086 		*locked = 1;
7087 	}
7088 	asoc->chunks_on_out_queue++;
7089 	TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7090 	asoc->send_queue_cnt++;
7091 out_of:
7092 	if (send_lock_up) {
7093 		SCTP_TCB_SEND_UNLOCK(stcb);
7094 		send_lock_up = 0;
7095 	}
7096 	return (to_move);
7097 }
7098 
7099 
7100 static struct sctp_stream_out *
7101 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
7102 {
7103 	struct sctp_stream_out *strq;
7104 
7105 	/* Find the next stream to use */
7106 	if (asoc->last_out_stream == NULL) {
7107 		strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
7108 		if (asoc->last_out_stream == NULL) {
7109 			/* huh nothing on the wheel, TSNH */
7110 			return (NULL);
7111 		}
7112 		goto done_it;
7113 	}
7114 	strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7115 done_it:
7116 	if (strq == NULL) {
7117 		strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
7118 	}
7119 	/* Save off the last stream */
7120 	asoc->last_out_stream = strq;
7121 	return (strq);
7122 
7123 }
7124 
7125 
7126 static void
7127 sctp_fill_outqueue(struct sctp_tcb *stcb,
7128     struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
7129 {
7130 	struct sctp_association *asoc;
7131 	struct sctp_stream_out *strq, *strqn, *strqt;
7132 	int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7133 	int locked, giveup;
7134 	struct sctp_stream_queue_pending *sp;
7135 
7136 	SCTP_TCB_LOCK_ASSERT(stcb);
7137 	asoc = &stcb->asoc;
7138 #ifdef INET6
7139 	if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
7140 		goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7141 	} else {
7142 		/* ?? not sure what else to do */
7143 		goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7144 	}
7145 #else
7146 	goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7147 #endif
7148 	/* Need an allowance for the data chunk header too */
7149 	goal_mtu -= sizeof(struct sctp_data_chunk);
7150 
7151 	/* must make even word boundary */
7152 	goal_mtu &= 0xfffffffc;
7153 	if (asoc->locked_on_sending) {
7154 		/* We are stuck on one stream until the message completes. */
7155 		strqn = strq = asoc->locked_on_sending;
7156 		locked = 1;
7157 	} else {
7158 		strqn = strq = sctp_select_a_stream(stcb, asoc);
7159 		locked = 0;
7160 	}
7161 
7162 	while ((goal_mtu > 0) && strq) {
7163 		sp = TAILQ_FIRST(&strq->outqueue);
7164 		/*
7165 		 * If CMT is off, we must validate that the stream in
7166 		 * question has the first item pointed towards are network
7167 		 * destionation requested by the caller. Note that if we
7168 		 * turn out to be locked to a stream (assigning TSN's then
7169 		 * we must stop, since we cannot look for another stream
7170 		 * with data to send to that destination). In CMT's case, by
7171 		 * skipping this check, we will send one data packet towards
7172 		 * the requested net.
7173 		 */
7174 		if (sp == NULL) {
7175 			break;
7176 		}
7177 		if ((sp->net != net) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
7178 			/* none for this network */
7179 			if (locked) {
7180 				break;
7181 			} else {
7182 				strq = sctp_select_a_stream(stcb, asoc);
7183 				if (strq == NULL)
7184 					/* none left */
7185 					break;
7186 				if (strqn == strq) {
7187 					/* I have circled */
7188 					break;
7189 				}
7190 				continue;
7191 			}
7192 		}
7193 		giveup = 0;
7194 		bail = 0;
7195 		moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
7196 		    &giveup, eeor_mode, &bail);
7197 		asoc->last_out_stream = strq;
7198 		if (locked) {
7199 			asoc->locked_on_sending = strq;
7200 			if ((moved_how_much == 0) || (giveup) || bail)
7201 				/* no more to move for now */
7202 				break;
7203 		} else {
7204 			asoc->locked_on_sending = NULL;
7205 			strqt = sctp_select_a_stream(stcb, asoc);
7206 			if (TAILQ_FIRST(&strq->outqueue) == NULL) {
7207 				if (strq == strqn) {
7208 					/* Must move start to next one */
7209 					strqn = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7210 					if (strqn == NULL) {
7211 						strqn = TAILQ_FIRST(&asoc->out_wheel);
7212 						if (strqn == NULL) {
7213 							break;
7214 						}
7215 					}
7216 				}
7217 				sctp_remove_from_wheel(stcb, asoc, strq);
7218 			}
7219 			if ((giveup) || bail) {
7220 				break;
7221 			}
7222 			strq = strqt;
7223 			if (strq == NULL) {
7224 				break;
7225 			}
7226 		}
7227 		total_moved += moved_how_much;
7228 		goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7229 		goal_mtu &= 0xfffffffc;
7230 	}
7231 	if (bail)
7232 		*quit_now = 1;
7233 
7234 	if (total_moved == 0) {
7235 		if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) &&
7236 		    (net == stcb->asoc.primary_destination)) {
7237 			/* ran dry for primary network net */
7238 			SCTP_STAT_INCR(sctps_primary_randry);
7239 		} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7240 			/* ran dry with CMT on */
7241 			SCTP_STAT_INCR(sctps_cmt_randry);
7242 		}
7243 	}
7244 }
7245 
7246 void
7247 sctp_fix_ecn_echo(struct sctp_association *asoc)
7248 {
7249 	struct sctp_tmit_chunk *chk;
7250 
7251 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7252 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7253 			chk->sent = SCTP_DATAGRAM_UNSENT;
7254 		}
7255 	}
7256 }
7257 
7258 static void
7259 sctp_move_to_an_alt(struct sctp_tcb *stcb,
7260     struct sctp_association *asoc,
7261     struct sctp_nets *net)
7262 {
7263 	struct sctp_tmit_chunk *chk;
7264 	struct sctp_nets *a_net;
7265 
7266 	SCTP_TCB_LOCK_ASSERT(stcb);
7267 	/*
7268 	 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
7269 	 * destination using the PF algorithm for finding alternate
7270 	 * destinations.
7271 	 */
7272 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
7273 		a_net = sctp_find_alternate_net(stcb, net, 2);
7274 	} else {
7275 		a_net = sctp_find_alternate_net(stcb, net, 0);
7276 	}
7277 	if ((a_net != net) &&
7278 	    ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
7279 		/*
7280 		 * We only proceed if a valid alternate is found that is not
7281 		 * this one and is reachable. Here we must move all chunks
7282 		 * queued in the send queue off of the destination address
7283 		 * to our alternate.
7284 		 */
7285 		TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7286 			if (chk->whoTo == net) {
7287 				/* Move the chunk to our alternate */
7288 				sctp_free_remote_addr(chk->whoTo);
7289 				chk->whoTo = a_net;
7290 				atomic_add_int(&a_net->ref_count, 1);
7291 			}
7292 		}
7293 	}
7294 }
7295 
7296 int
7297 sctp_med_chunk_output(struct sctp_inpcb *inp,
7298     struct sctp_tcb *stcb,
7299     struct sctp_association *asoc,
7300     int *num_out,
7301     int *reason_code,
7302     int control_only, int *cwnd_full, int from_where,
7303     struct timeval *now, int *now_filled, int frag_point, int so_locked
7304 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7305     SCTP_UNUSED
7306 #endif
7307 )
7308 {
7309 	/*
7310 	 * Ok this is the generic chunk service queue. we must do the
7311 	 * following: - Service the stream queue that is next, moving any
7312 	 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7313 	 * LAST to the out queue in one pass) and assigning TSN's - Check to
7314 	 * see if the cwnd/rwnd allows any output, if so we go ahead and
7315 	 * fomulate and send the low level chunks. Making sure to combine
7316 	 * any control in the control chunk queue also.
7317 	 */
7318 	struct sctp_nets *net;
7319 	struct mbuf *outchain, *endoutchain;
7320 	struct sctp_tmit_chunk *chk, *nchk;
7321 	struct sctphdr *shdr;
7322 
7323 	/* temp arrays for unlinking */
7324 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7325 	int no_fragmentflg, error;
7326 	int one_chunk, hbflag, skip_data_for_this_net;
7327 	int asconf, cookie, no_out_cnt;
7328 	int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind, eeor_mode;
7329 	unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7330 	struct sctp_nets *start_at, *old_startat = NULL, *send_start_at;
7331 	int tsns_sent = 0;
7332 	uint32_t auth_offset = 0;
7333 	struct sctp_auth_chunk *auth = NULL;
7334 
7335 	/*
7336 	 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7337 	 * destination.
7338 	 */
7339 	int pf_hbflag = 0;
7340 	int quit_now = 0;
7341 
7342 	*num_out = 0;
7343 	cwnd_full_ind = 0;
7344 
7345 	if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7346 	    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7347 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7348 		eeor_mode = 1;
7349 	} else {
7350 		eeor_mode = 0;
7351 	}
7352 	ctl_cnt = no_out_cnt = asconf = cookie = 0;
7353 	/*
7354 	 * First lets prime the pump. For each destination, if there is room
7355 	 * in the flight size, attempt to pull an MTU's worth out of the
7356 	 * stream queues into the general send_queue
7357 	 */
7358 #ifdef SCTP_AUDITING_ENABLED
7359 	sctp_audit_log(0xC2, 2);
7360 #endif
7361 	SCTP_TCB_LOCK_ASSERT(stcb);
7362 	hbflag = 0;
7363 	if ((control_only) || (asoc->stream_reset_outstanding))
7364 		no_data_chunks = 1;
7365 	else
7366 		no_data_chunks = 0;
7367 
7368 	/* Nothing to possible to send? */
7369 	if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7370 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7371 	    TAILQ_EMPTY(&asoc->send_queue) &&
7372 	    TAILQ_EMPTY(&asoc->out_wheel)) {
7373 		*reason_code = 9;
7374 		return (0);
7375 	}
7376 	if (asoc->peers_rwnd == 0) {
7377 		/* No room in peers rwnd */
7378 		*cwnd_full = 1;
7379 		*reason_code = 1;
7380 		if (asoc->total_flight > 0) {
7381 			/* we are allowed one chunk in flight */
7382 			no_data_chunks = 1;
7383 		}
7384 	}
7385 	if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
7386 		if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7387 			/*
7388 			 * for CMT we start at the next one past the one we
7389 			 * last added data to.
7390 			 */
7391 			if (TAILQ_FIRST(&asoc->send_queue) != NULL) {
7392 				goto skip_the_fill_from_streams;
7393 			}
7394 			if (asoc->last_net_data_came_from) {
7395 				net = TAILQ_NEXT(asoc->last_net_data_came_from, sctp_next);
7396 				if (net == NULL) {
7397 					net = TAILQ_FIRST(&asoc->nets);
7398 				}
7399 			} else {
7400 				/* back to start */
7401 				net = TAILQ_FIRST(&asoc->nets);
7402 			}
7403 
7404 			/*
7405 			 * JRI-TODO: CMT-MPI. Simply set the first
7406 			 * destination (net) to be optimized for the next
7407 			 * message to be pulled out of the outwheel. 1. peek
7408 			 * at outwheel 2. If large message, set net =
7409 			 * highest_cwnd 3. If small message, set net =
7410 			 * lowest rtt
7411 			 */
7412 		} else {
7413 			net = asoc->primary_destination;
7414 			if (net == NULL) {
7415 				/* TSNH */
7416 				net = TAILQ_FIRST(&asoc->nets);
7417 			}
7418 		}
7419 		start_at = net;
7420 
7421 one_more_time:
7422 		for (; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7423 			net->window_probe = 0;
7424 			if (old_startat && (old_startat == net)) {
7425 				break;
7426 			}
7427 			/*
7428 			 * JRI: if dest is unreachable or unconfirmed, do
7429 			 * not send data to it
7430 			 */
7431 			if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
7432 				continue;
7433 			}
7434 			/*
7435 			 * JRI: if dest is in PF state, do not send data to
7436 			 * it
7437 			 */
7438 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
7439 			    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
7440 			    (net->dest_state & SCTP_ADDR_PF)) {
7441 				continue;
7442 			}
7443 			if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
7444 				/* nothing can be in queue for this guy */
7445 				continue;
7446 			}
7447 			if (net->flight_size >= net->cwnd) {
7448 				/* skip this network, no room */
7449 				cwnd_full_ind++;
7450 				continue;
7451 			}
7452 			/*
7453 			 * JRI : this for loop we are in takes in each net,
7454 			 * if its's got space in cwnd and has data sent to
7455 			 * it (when CMT is off) then it calls
7456 			 * sctp_fill_outqueue for the net. This gets data on
7457 			 * the send queue for that network.
7458 			 *
7459 			 * In sctp_fill_outqueue TSN's are assigned and data is
7460 			 * copied out of the stream buffers. Note mostly
7461 			 * copy by reference (we hope).
7462 			 */
7463 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7464 				sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7465 			}
7466 			sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
7467 			if (quit_now) {
7468 				/* memory alloc failure */
7469 				no_data_chunks = 1;
7470 				goto skip_the_fill_from_streams;
7471 			}
7472 		}
7473 		if (start_at != TAILQ_FIRST(&asoc->nets)) {
7474 			/* got to pick up the beginning stuff. */
7475 			old_startat = start_at;
7476 			start_at = net = TAILQ_FIRST(&asoc->nets);
7477 			if (old_startat)
7478 				goto one_more_time;
7479 		}
7480 	}
7481 skip_the_fill_from_streams:
7482 	*cwnd_full = cwnd_full_ind;
7483 
7484 	/* now service each destination and send out what we can for it */
7485 	/* Nothing to send? */
7486 	if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7487 	    (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7488 	    (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
7489 		*reason_code = 8;
7490 		return (0);
7491 	}
7492 	if (no_data_chunks) {
7493 		chk = TAILQ_FIRST(&asoc->asconf_send_queue);
7494 		if (chk == NULL)
7495 			chk = TAILQ_FIRST(&asoc->control_send_queue);
7496 	} else {
7497 		chk = TAILQ_FIRST(&asoc->send_queue);
7498 	}
7499 	if (chk) {
7500 		send_start_at = chk->whoTo;
7501 	} else {
7502 		send_start_at = TAILQ_FIRST(&asoc->nets);
7503 	}
7504 	old_startat = NULL;
7505 again_one_more_time:
7506 	for (net = send_start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7507 		/* how much can we send? */
7508 		/* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7509 		if (old_startat && (old_startat == net)) {
7510 			/* through list ocmpletely. */
7511 			break;
7512 		}
7513 		tsns_sent = 0;
7514 		if (net->ref_count < 2) {
7515 			/*
7516 			 * Ref-count of 1 so we cannot have data or control
7517 			 * queued to this address. Skip it.
7518 			 */
7519 			continue;
7520 		}
7521 		ctl_cnt = bundle_at = 0;
7522 		endoutchain = outchain = NULL;
7523 		no_fragmentflg = 1;
7524 		one_chunk = 0;
7525 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7526 			skip_data_for_this_net = 1;
7527 		} else {
7528 			skip_data_for_this_net = 0;
7529 		}
7530 		if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7531 			/*
7532 			 * if we have a route and an ifp check to see if we
7533 			 * have room to send to this guy
7534 			 */
7535 			struct ifnet *ifp;
7536 
7537 			ifp = net->ro.ro_rt->rt_ifp;
7538 			if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7539 				SCTP_STAT_INCR(sctps_ifnomemqueued);
7540 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7541 					sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7542 				}
7543 				continue;
7544 			}
7545 		}
7546 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7547 		case AF_INET:
7548 			mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7549 			break;
7550 #ifdef INET6
7551 		case AF_INET6:
7552 			mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7553 			break;
7554 #endif
7555 		default:
7556 			/* TSNH */
7557 			mtu = net->mtu;
7558 			break;
7559 		}
7560 		mx_mtu = mtu;
7561 		to_out = 0;
7562 		if (mtu > asoc->peers_rwnd) {
7563 			if (asoc->total_flight > 0) {
7564 				/* We have a packet in flight somewhere */
7565 				r_mtu = asoc->peers_rwnd;
7566 			} else {
7567 				/* We are always allowed to send one MTU out */
7568 				one_chunk = 1;
7569 				r_mtu = mtu;
7570 			}
7571 		} else {
7572 			r_mtu = mtu;
7573 		}
7574 		/************************/
7575 		/* ASCONF transmission */
7576 		/************************/
7577 		/* Now first lets go through the asconf queue */
7578 		for (chk = TAILQ_FIRST(&asoc->asconf_send_queue);
7579 		    chk; chk = nchk) {
7580 			nchk = TAILQ_NEXT(chk, sctp_next);
7581 			if (chk->rec.chunk_id.id != SCTP_ASCONF) {
7582 				continue;
7583 			}
7584 			if (chk->whoTo != net) {
7585 				/*
7586 				 * No, not sent to the network we are
7587 				 * looking at
7588 				 */
7589 				break;
7590 			}
7591 			if (chk->data == NULL) {
7592 				break;
7593 			}
7594 			if (chk->sent != SCTP_DATAGRAM_UNSENT &&
7595 			    chk->sent != SCTP_DATAGRAM_RESEND) {
7596 				break;
7597 			}
7598 			/*
7599 			 * if no AUTH is yet included and this chunk
7600 			 * requires it, make sure to account for it.  We
7601 			 * don't apply the size until the AUTH chunk is
7602 			 * actually added below in case there is no room for
7603 			 * this chunk. NOTE: we overload the use of "omtu"
7604 			 * here
7605 			 */
7606 			if ((auth == NULL) &&
7607 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7608 			    stcb->asoc.peer_auth_chunks)) {
7609 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7610 			} else
7611 				omtu = 0;
7612 			/* Here we do NOT factor the r_mtu */
7613 			if ((chk->send_size < (int)(mtu - omtu)) ||
7614 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7615 				/*
7616 				 * We probably should glom the mbuf chain
7617 				 * from the chk->data for control but the
7618 				 * problem is it becomes yet one more level
7619 				 * of tracking to do if for some reason
7620 				 * output fails. Then I have got to
7621 				 * reconstruct the merged control chain.. el
7622 				 * yucko.. for now we take the easy way and
7623 				 * do the copy
7624 				 */
7625 				/*
7626 				 * Add an AUTH chunk, if chunk requires it
7627 				 * save the offset into the chain for AUTH
7628 				 */
7629 				if ((auth == NULL) &&
7630 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7631 				    stcb->asoc.peer_auth_chunks))) {
7632 					outchain = sctp_add_auth_chunk(outchain,
7633 					    &endoutchain,
7634 					    &auth,
7635 					    &auth_offset,
7636 					    stcb,
7637 					    chk->rec.chunk_id.id);
7638 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7639 				}
7640 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7641 				    (int)chk->rec.chunk_id.can_take_data,
7642 				    chk->send_size, chk->copy_by_ref);
7643 				if (outchain == NULL) {
7644 					*reason_code = 8;
7645 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7646 					return (ENOMEM);
7647 				}
7648 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7649 				/* update our MTU size */
7650 				if (mtu > (chk->send_size + omtu))
7651 					mtu -= (chk->send_size + omtu);
7652 				else
7653 					mtu = 0;
7654 				to_out += (chk->send_size + omtu);
7655 				/* Do clear IP_DF ? */
7656 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7657 					no_fragmentflg = 0;
7658 				}
7659 				if (chk->rec.chunk_id.can_take_data)
7660 					chk->data = NULL;
7661 				/*
7662 				 * set hb flag since we can use these for
7663 				 * RTO
7664 				 */
7665 				hbflag = 1;
7666 				asconf = 1;
7667 				/*
7668 				 * should sysctl this: don't bundle data
7669 				 * with ASCONF since it requires AUTH
7670 				 */
7671 				no_data_chunks = 1;
7672 				chk->sent = SCTP_DATAGRAM_SENT;
7673 				chk->snd_count++;
7674 				if (mtu == 0) {
7675 					/*
7676 					 * Ok we are out of room but we can
7677 					 * output without effecting the
7678 					 * flight size since this little guy
7679 					 * is a control only packet.
7680 					 */
7681 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7682 					/*
7683 					 * do NOT clear the asconf flag as
7684 					 * it is used to do appropriate
7685 					 * source address selection.
7686 					 */
7687 					SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
7688 					if (outchain == NULL) {
7689 						/* no memory */
7690 						SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
7691 						error = ENOBUFS;
7692 						*reason_code = 7;
7693 						continue;
7694 					}
7695 					shdr = mtod(outchain, struct sctphdr *);
7696 					shdr->src_port = inp->sctp_lport;
7697 					shdr->dest_port = stcb->rport;
7698 					shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7699 					shdr->checksum = 0;
7700 					auth_offset += sizeof(struct sctphdr);
7701 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7702 					    (struct sockaddr *)&net->ro._l_addr,
7703 					    outchain, auth_offset, auth,
7704 					    no_fragmentflg, 0, NULL, asconf, net->port, so_locked, NULL))) {
7705 						if (error == ENOBUFS) {
7706 							asoc->ifp_had_enobuf = 1;
7707 							SCTP_STAT_INCR(sctps_lowlevelerr);
7708 						}
7709 						if (from_where == 0) {
7710 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
7711 						}
7712 						if (*now_filled == 0) {
7713 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7714 							*now_filled = 1;
7715 							*now = net->last_sent_time;
7716 						} else {
7717 							net->last_sent_time = *now;
7718 						}
7719 						hbflag = 0;
7720 						/* error, could not output */
7721 						if (error == EHOSTUNREACH) {
7722 							/*
7723 							 * Destination went
7724 							 * unreachable
7725 							 * during this send
7726 							 */
7727 							sctp_move_to_an_alt(stcb, asoc, net);
7728 						}
7729 						*reason_code = 7;
7730 						continue;
7731 					} else
7732 						asoc->ifp_had_enobuf = 0;
7733 					if (*now_filled == 0) {
7734 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7735 						*now_filled = 1;
7736 						*now = net->last_sent_time;
7737 					} else {
7738 						net->last_sent_time = *now;
7739 					}
7740 					hbflag = 0;
7741 					/*
7742 					 * increase the number we sent, if a
7743 					 * cookie is sent we don't tell them
7744 					 * any was sent out.
7745 					 */
7746 					outchain = endoutchain = NULL;
7747 					auth = NULL;
7748 					auth_offset = 0;
7749 					if (!no_out_cnt)
7750 						*num_out += ctl_cnt;
7751 					/* recalc a clean slate and setup */
7752 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7753 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7754 					} else {
7755 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7756 					}
7757 					to_out = 0;
7758 					no_fragmentflg = 1;
7759 				}
7760 			}
7761 		}
7762 		/************************/
7763 		/* Control transmission */
7764 		/************************/
7765 		/* Now first lets go through the control queue */
7766 		for (chk = TAILQ_FIRST(&asoc->control_send_queue);
7767 		    chk; chk = nchk) {
7768 			nchk = TAILQ_NEXT(chk, sctp_next);
7769 			if (chk->whoTo != net) {
7770 				/*
7771 				 * No, not sent to the network we are
7772 				 * looking at
7773 				 */
7774 				continue;
7775 			}
7776 			if (chk->data == NULL) {
7777 				continue;
7778 			}
7779 			if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7780 				/*
7781 				 * It must be unsent. Cookies and ASCONF's
7782 				 * hang around but there timers will force
7783 				 * when marked for resend.
7784 				 */
7785 				continue;
7786 			}
7787 			/*
7788 			 * if no AUTH is yet included and this chunk
7789 			 * requires it, make sure to account for it.  We
7790 			 * don't apply the size until the AUTH chunk is
7791 			 * actually added below in case there is no room for
7792 			 * this chunk. NOTE: we overload the use of "omtu"
7793 			 * here
7794 			 */
7795 			if ((auth == NULL) &&
7796 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7797 			    stcb->asoc.peer_auth_chunks)) {
7798 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7799 			} else
7800 				omtu = 0;
7801 			/* Here we do NOT factor the r_mtu */
7802 			if ((chk->send_size < (int)(mtu - omtu)) ||
7803 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7804 				/*
7805 				 * We probably should glom the mbuf chain
7806 				 * from the chk->data for control but the
7807 				 * problem is it becomes yet one more level
7808 				 * of tracking to do if for some reason
7809 				 * output fails. Then I have got to
7810 				 * reconstruct the merged control chain.. el
7811 				 * yucko.. for now we take the easy way and
7812 				 * do the copy
7813 				 */
7814 				/*
7815 				 * Add an AUTH chunk, if chunk requires it
7816 				 * save the offset into the chain for AUTH
7817 				 */
7818 				if ((auth == NULL) &&
7819 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7820 				    stcb->asoc.peer_auth_chunks))) {
7821 					outchain = sctp_add_auth_chunk(outchain,
7822 					    &endoutchain,
7823 					    &auth,
7824 					    &auth_offset,
7825 					    stcb,
7826 					    chk->rec.chunk_id.id);
7827 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7828 				}
7829 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7830 				    (int)chk->rec.chunk_id.can_take_data,
7831 				    chk->send_size, chk->copy_by_ref);
7832 				if (outchain == NULL) {
7833 					*reason_code = 8;
7834 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7835 					return (ENOMEM);
7836 				}
7837 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7838 				/* update our MTU size */
7839 				if (mtu > (chk->send_size + omtu))
7840 					mtu -= (chk->send_size + omtu);
7841 				else
7842 					mtu = 0;
7843 				to_out += (chk->send_size + omtu);
7844 				/* Do clear IP_DF ? */
7845 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7846 					no_fragmentflg = 0;
7847 				}
7848 				if (chk->rec.chunk_id.can_take_data)
7849 					chk->data = NULL;
7850 				/* Mark things to be removed, if needed */
7851 				if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7852 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7853 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7854 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7855 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7856 				    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7857 				    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7858 				    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7859 				    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7860 				    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7861 
7862 					if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7863 						hbflag = 1;
7864 						/*
7865 						 * JRS 5/14/07 - Set the
7866 						 * flag to say a heartbeat
7867 						 * is being sent.
7868 						 */
7869 						pf_hbflag = 1;
7870 					}
7871 					/* remove these chunks at the end */
7872 					if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
7873 						/* turn off the timer */
7874 						if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7875 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7876 							    inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7877 						}
7878 					}
7879 					ctl_cnt++;
7880 				} else {
7881 					/*
7882 					 * Other chunks, since they have
7883 					 * timers running (i.e. COOKIE) we
7884 					 * just "trust" that it gets sent or
7885 					 * retransmitted.
7886 					 */
7887 					ctl_cnt++;
7888 					if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7889 						cookie = 1;
7890 						no_out_cnt = 1;
7891 					}
7892 					chk->sent = SCTP_DATAGRAM_SENT;
7893 					chk->snd_count++;
7894 				}
7895 				if (mtu == 0) {
7896 					/*
7897 					 * Ok we are out of room but we can
7898 					 * output without effecting the
7899 					 * flight size since this little guy
7900 					 * is a control only packet.
7901 					 */
7902 					if (asconf) {
7903 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7904 						/*
7905 						 * do NOT clear the asconf
7906 						 * flag as it is used to do
7907 						 * appropriate source
7908 						 * address selection.
7909 						 */
7910 					}
7911 					if (cookie) {
7912 						sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7913 						cookie = 0;
7914 					}
7915 					SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
7916 					if (outchain == NULL) {
7917 						/* no memory */
7918 						SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
7919 						error = ENOBUFS;
7920 						goto error_out_again;
7921 					}
7922 					shdr = mtod(outchain, struct sctphdr *);
7923 					shdr->src_port = inp->sctp_lport;
7924 					shdr->dest_port = stcb->rport;
7925 					shdr->v_tag = htonl(stcb->asoc.peer_vtag);
7926 					shdr->checksum = 0;
7927 					auth_offset += sizeof(struct sctphdr);
7928 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7929 					    (struct sockaddr *)&net->ro._l_addr,
7930 					    outchain, auth_offset, auth,
7931 					    no_fragmentflg, 0, NULL, asconf, net->port, so_locked, NULL))) {
7932 						if (error == ENOBUFS) {
7933 							asoc->ifp_had_enobuf = 1;
7934 							SCTP_STAT_INCR(sctps_lowlevelerr);
7935 						}
7936 						if (from_where == 0) {
7937 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
7938 						}
7939 				error_out_again:
7940 						/* error, could not output */
7941 						if (hbflag) {
7942 							if (*now_filled == 0) {
7943 								(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7944 								*now_filled = 1;
7945 								*now = net->last_sent_time;
7946 							} else {
7947 								net->last_sent_time = *now;
7948 							}
7949 							hbflag = 0;
7950 						}
7951 						if (error == EHOSTUNREACH) {
7952 							/*
7953 							 * Destination went
7954 							 * unreachable
7955 							 * during this send
7956 							 */
7957 							sctp_move_to_an_alt(stcb, asoc, net);
7958 						}
7959 						*reason_code = 7;
7960 						continue;
7961 					} else
7962 						asoc->ifp_had_enobuf = 0;
7963 					/* Only HB or ASCONF advances time */
7964 					if (hbflag) {
7965 						if (*now_filled == 0) {
7966 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7967 							*now_filled = 1;
7968 							*now = net->last_sent_time;
7969 						} else {
7970 							net->last_sent_time = *now;
7971 						}
7972 						hbflag = 0;
7973 					}
7974 					/*
7975 					 * increase the number we sent, if a
7976 					 * cookie is sent we don't tell them
7977 					 * any was sent out.
7978 					 */
7979 					outchain = endoutchain = NULL;
7980 					auth = NULL;
7981 					auth_offset = 0;
7982 					if (!no_out_cnt)
7983 						*num_out += ctl_cnt;
7984 					/* recalc a clean slate and setup */
7985 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7986 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7987 					} else {
7988 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7989 					}
7990 					to_out = 0;
7991 					no_fragmentflg = 1;
7992 				}
7993 			}
7994 		}
7995 		/*********************/
7996 		/* Data transmission */
7997 		/*********************/
7998 		/*
7999 		 * if AUTH for DATA is required and no AUTH has been added
8000 		 * yet, account for this in the mtu now... if no data can be
8001 		 * bundled, this adjustment won't matter anyways since the
8002 		 * packet will be going out...
8003 		 */
8004 		if ((auth == NULL) &&
8005 		    sctp_auth_is_required_chunk(SCTP_DATA,
8006 		    stcb->asoc.peer_auth_chunks)) {
8007 			mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8008 		}
8009 		/* now lets add any data within the MTU constraints */
8010 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8011 		case AF_INET:
8012 			if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8013 				omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8014 			else
8015 				omtu = 0;
8016 			break;
8017 #ifdef INET6
8018 		case AF_INET6:
8019 			if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8020 				omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8021 			else
8022 				omtu = 0;
8023 			break;
8024 #endif
8025 		default:
8026 			/* TSNH */
8027 			omtu = 0;
8028 			break;
8029 		}
8030 		if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) && (skip_data_for_this_net == 0)) ||
8031 		    (cookie)) {
8032 			for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
8033 				if (no_data_chunks) {
8034 					/* let only control go out */
8035 					*reason_code = 1;
8036 					break;
8037 				}
8038 				if (net->flight_size >= net->cwnd) {
8039 					/* skip this net, no room for data */
8040 					*reason_code = 2;
8041 					break;
8042 				}
8043 				nchk = TAILQ_NEXT(chk, sctp_next);
8044 				if (chk->whoTo != net) {
8045 					/* No, not sent to this net */
8046 					continue;
8047 				}
8048 				if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8049 					/*-
8050 					 * strange, we have a chunk that is
8051 					 * to big for its destination and
8052 					 * yet no fragment ok flag.
8053 					 * Something went wrong when the
8054 					 * PMTU changed...we did not mark
8055 					 * this chunk for some reason?? I
8056 					 * will fix it here by letting IP
8057 					 * fragment it for now and printing
8058 					 * a warning. This really should not
8059 					 * happen ...
8060 					 */
8061 					SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8062 					    chk->send_size, mtu);
8063 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8064 				}
8065 				if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8066 				    ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8067 					/* ok we will add this one */
8068 
8069 					/*
8070 					 * Add an AUTH chunk, if chunk
8071 					 * requires it, save the offset into
8072 					 * the chain for AUTH
8073 					 */
8074 					if ((auth == NULL) &&
8075 					    (sctp_auth_is_required_chunk(SCTP_DATA,
8076 					    stcb->asoc.peer_auth_chunks))) {
8077 
8078 						outchain = sctp_add_auth_chunk(outchain,
8079 						    &endoutchain,
8080 						    &auth,
8081 						    &auth_offset,
8082 						    stcb,
8083 						    SCTP_DATA);
8084 						SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8085 					}
8086 					outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8087 					    chk->send_size, chk->copy_by_ref);
8088 					if (outchain == NULL) {
8089 						SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8090 						if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8091 							sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8092 						}
8093 						*reason_code = 3;
8094 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8095 						return (ENOMEM);
8096 					}
8097 					/* upate our MTU size */
8098 					/* Do clear IP_DF ? */
8099 					if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8100 						no_fragmentflg = 0;
8101 					}
8102 					/* unsigned subtraction of mtu */
8103 					if (mtu > chk->send_size)
8104 						mtu -= chk->send_size;
8105 					else
8106 						mtu = 0;
8107 					/* unsigned subtraction of r_mtu */
8108 					if (r_mtu > chk->send_size)
8109 						r_mtu -= chk->send_size;
8110 					else
8111 						r_mtu = 0;
8112 
8113 					to_out += chk->send_size;
8114 					if ((to_out > mx_mtu) && no_fragmentflg) {
8115 #ifdef INVARIANTS
8116 						panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8117 #else
8118 						SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8119 						    mx_mtu, to_out);
8120 #endif
8121 					}
8122 					chk->window_probe = 0;
8123 					data_list[bundle_at++] = chk;
8124 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8125 						mtu = 0;
8126 						break;
8127 					}
8128 					if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8129 						if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8130 							SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8131 						} else {
8132 							SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8133 						}
8134 						if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8135 						    ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8136 							/*
8137 							 * Count number of
8138 							 * user msg's that
8139 							 * were fragmented
8140 							 * we do this by
8141 							 * counting when we
8142 							 * see a LAST
8143 							 * fragment only.
8144 							 */
8145 							SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8146 					}
8147 					if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8148 						if (one_chunk) {
8149 							data_list[0]->window_probe = 1;
8150 							net->window_probe = 1;
8151 						}
8152 						break;
8153 					}
8154 				} else {
8155 					/*
8156 					 * Must be sent in order of the
8157 					 * TSN's (on a network)
8158 					 */
8159 					break;
8160 				}
8161 			}	/* for (chunk gather loop for this net) */
8162 		}		/* if asoc.state OPEN */
8163 		/* Is there something to send for this destination? */
8164 		if (outchain) {
8165 			/* We may need to start a control timer or two */
8166 			if (asconf) {
8167 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8168 				    stcb, net);
8169 				/*
8170 				 * do NOT clear the asconf flag as it is
8171 				 * used to do appropriate source address
8172 				 * selection.
8173 				 */
8174 			}
8175 			if (cookie) {
8176 				sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8177 				cookie = 0;
8178 			}
8179 			/* must start a send timer if data is being sent */
8180 			if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8181 				/*
8182 				 * no timer running on this destination
8183 				 * restart it.
8184 				 */
8185 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8186 			} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
8187 				    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
8188 				    pf_hbflag &&
8189 				    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
8190 			    (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8191 				/*
8192 				 * JRS 5/14/07 - If a HB has been sent to a
8193 				 * PF destination and no T3 timer is
8194 				 * currently running, start the T3 timer to
8195 				 * track the HBs that were sent.
8196 				 */
8197 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8198 			}
8199 			/* Now send it, if there is anything to send :> */
8200 			SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
8201 			if (outchain == NULL) {
8202 				/* out of mbufs */
8203 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
8204 				error = ENOBUFS;
8205 				goto errored_send;
8206 			}
8207 			shdr = mtod(outchain, struct sctphdr *);
8208 			shdr->src_port = inp->sctp_lport;
8209 			shdr->dest_port = stcb->rport;
8210 			shdr->v_tag = htonl(stcb->asoc.peer_vtag);
8211 			shdr->checksum = 0;
8212 			auth_offset += sizeof(struct sctphdr);
8213 			if ((error = sctp_lowlevel_chunk_output(inp,
8214 			    stcb,
8215 			    net,
8216 			    (struct sockaddr *)&net->ro._l_addr,
8217 			    outchain,
8218 			    auth_offset,
8219 			    auth,
8220 			    no_fragmentflg,
8221 			    bundle_at,
8222 			    data_list[0],
8223 			    asconf, net->port, so_locked, NULL))) {
8224 				/* error, we could not output */
8225 				if (error == ENOBUFS) {
8226 					SCTP_STAT_INCR(sctps_lowlevelerr);
8227 					asoc->ifp_had_enobuf = 1;
8228 				}
8229 				if (from_where == 0) {
8230 					SCTP_STAT_INCR(sctps_lowlevelerrusr);
8231 				}
8232 		errored_send:
8233 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8234 				if (hbflag) {
8235 					if (*now_filled == 0) {
8236 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8237 						*now_filled = 1;
8238 						*now = net->last_sent_time;
8239 					} else {
8240 						net->last_sent_time = *now;
8241 					}
8242 					hbflag = 0;
8243 				}
8244 				if (error == EHOSTUNREACH) {
8245 					/*
8246 					 * Destination went unreachable
8247 					 * during this send
8248 					 */
8249 					sctp_move_to_an_alt(stcb, asoc, net);
8250 				}
8251 				*reason_code = 6;
8252 				/*-
8253 				 * I add this line to be paranoid. As far as
8254 				 * I can tell the continue, takes us back to
8255 				 * the top of the for, but just to make sure
8256 				 * I will reset these again here.
8257 				 */
8258 				ctl_cnt = bundle_at = 0;
8259 				continue;	/* This takes us back to the
8260 						 * for() for the nets. */
8261 			} else {
8262 				asoc->ifp_had_enobuf = 0;
8263 			}
8264 			outchain = endoutchain = NULL;
8265 			auth = NULL;
8266 			auth_offset = 0;
8267 			if (bundle_at || hbflag) {
8268 				/* For data/asconf and hb set time */
8269 				if (*now_filled == 0) {
8270 					(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8271 					*now_filled = 1;
8272 					*now = net->last_sent_time;
8273 				} else {
8274 					net->last_sent_time = *now;
8275 				}
8276 			}
8277 			if (!no_out_cnt) {
8278 				*num_out += (ctl_cnt + bundle_at);
8279 			}
8280 			if (bundle_at) {
8281 				/* setup for a RTO measurement */
8282 				tsns_sent = data_list[0]->rec.data.TSN_seq;
8283 				/* fill time if not already filled */
8284 				if (*now_filled == 0) {
8285 					(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8286 					*now_filled = 1;
8287 					*now = asoc->time_last_sent;
8288 				} else {
8289 					asoc->time_last_sent = *now;
8290 				}
8291 				data_list[0]->do_rtt = 1;
8292 				SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8293 				sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8294 				if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8295 					if (net->flight_size < net->cwnd) {
8296 						/* start or restart it */
8297 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8298 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8299 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
8300 						}
8301 						SCTP_STAT_INCR(sctps_earlyfrstrout);
8302 						sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
8303 					} else {
8304 						/* stop it if its running */
8305 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8306 							SCTP_STAT_INCR(sctps_earlyfrstpout);
8307 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8308 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
8309 						}
8310 					}
8311 				}
8312 			}
8313 			if (one_chunk) {
8314 				break;
8315 			}
8316 		}
8317 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8318 			sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8319 		}
8320 	}
8321 	if (old_startat == NULL) {
8322 		old_startat = send_start_at;
8323 		send_start_at = TAILQ_FIRST(&asoc->nets);
8324 		if (old_startat)
8325 			goto again_one_more_time;
8326 	}
8327 	/*
8328 	 * At the end there should be no NON timed chunks hanging on this
8329 	 * queue.
8330 	 */
8331 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8332 		sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8333 	}
8334 	if ((*num_out == 0) && (*reason_code == 0)) {
8335 		*reason_code = 4;
8336 	} else {
8337 		*reason_code = 5;
8338 	}
8339 	sctp_clean_up_ctl(stcb, asoc);
8340 	return (0);
8341 }
8342 
8343 void
8344 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8345 {
8346 	/*-
8347 	 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8348 	 * the control chunk queue.
8349 	 */
8350 	struct sctp_chunkhdr *hdr;
8351 	struct sctp_tmit_chunk *chk;
8352 	struct mbuf *mat;
8353 
8354 	SCTP_TCB_LOCK_ASSERT(stcb);
8355 	sctp_alloc_a_chunk(stcb, chk);
8356 	if (chk == NULL) {
8357 		/* no memory */
8358 		sctp_m_freem(op_err);
8359 		return;
8360 	}
8361 	chk->copy_by_ref = 0;
8362 	SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8363 	if (op_err == NULL) {
8364 		sctp_free_a_chunk(stcb, chk);
8365 		return;
8366 	}
8367 	chk->send_size = 0;
8368 	mat = op_err;
8369 	while (mat != NULL) {
8370 		chk->send_size += SCTP_BUF_LEN(mat);
8371 		mat = SCTP_BUF_NEXT(mat);
8372 	}
8373 	chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8374 	chk->rec.chunk_id.can_take_data = 1;
8375 	chk->sent = SCTP_DATAGRAM_UNSENT;
8376 	chk->snd_count = 0;
8377 	chk->flags = 0;
8378 	chk->asoc = &stcb->asoc;
8379 	chk->data = op_err;
8380 	chk->whoTo = chk->asoc->primary_destination;
8381 	atomic_add_int(&chk->whoTo->ref_count, 1);
8382 	hdr = mtod(op_err, struct sctp_chunkhdr *);
8383 	hdr->chunk_type = SCTP_OPERATION_ERROR;
8384 	hdr->chunk_flags = 0;
8385 	hdr->chunk_length = htons(chk->send_size);
8386 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8387 	    chk,
8388 	    sctp_next);
8389 	chk->asoc->ctrl_queue_cnt++;
8390 }
8391 
8392 int
8393 sctp_send_cookie_echo(struct mbuf *m,
8394     int offset,
8395     struct sctp_tcb *stcb,
8396     struct sctp_nets *net)
8397 {
8398 	/*-
8399 	 * pull out the cookie and put it at the front of the control chunk
8400 	 * queue.
8401 	 */
8402 	int at;
8403 	struct mbuf *cookie;
8404 	struct sctp_paramhdr parm, *phdr;
8405 	struct sctp_chunkhdr *hdr;
8406 	struct sctp_tmit_chunk *chk;
8407 	uint16_t ptype, plen;
8408 
8409 	/* First find the cookie in the param area */
8410 	cookie = NULL;
8411 	at = offset + sizeof(struct sctp_init_chunk);
8412 
8413 	SCTP_TCB_LOCK_ASSERT(stcb);
8414 	do {
8415 		phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8416 		if (phdr == NULL) {
8417 			return (-3);
8418 		}
8419 		ptype = ntohs(phdr->param_type);
8420 		plen = ntohs(phdr->param_length);
8421 		if (ptype == SCTP_STATE_COOKIE) {
8422 			int pad;
8423 
8424 			/* found the cookie */
8425 			if ((pad = (plen % 4))) {
8426 				plen += 4 - pad;
8427 			}
8428 			cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8429 			if (cookie == NULL) {
8430 				/* No memory */
8431 				return (-2);
8432 			}
8433 #ifdef SCTP_MBUF_LOGGING
8434 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8435 				struct mbuf *mat;
8436 
8437 				mat = cookie;
8438 				while (mat) {
8439 					if (SCTP_BUF_IS_EXTENDED(mat)) {
8440 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8441 					}
8442 					mat = SCTP_BUF_NEXT(mat);
8443 				}
8444 			}
8445 #endif
8446 			break;
8447 		}
8448 		at += SCTP_SIZE32(plen);
8449 	} while (phdr);
8450 	if (cookie == NULL) {
8451 		/* Did not find the cookie */
8452 		return (-3);
8453 	}
8454 	/* ok, we got the cookie lets change it into a cookie echo chunk */
8455 
8456 	/* first the change from param to cookie */
8457 	hdr = mtod(cookie, struct sctp_chunkhdr *);
8458 	hdr->chunk_type = SCTP_COOKIE_ECHO;
8459 	hdr->chunk_flags = 0;
8460 	/* get the chunk stuff now and place it in the FRONT of the queue */
8461 	sctp_alloc_a_chunk(stcb, chk);
8462 	if (chk == NULL) {
8463 		/* no memory */
8464 		sctp_m_freem(cookie);
8465 		return (-5);
8466 	}
8467 	chk->copy_by_ref = 0;
8468 	chk->send_size = plen;
8469 	chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8470 	chk->rec.chunk_id.can_take_data = 0;
8471 	chk->sent = SCTP_DATAGRAM_UNSENT;
8472 	chk->snd_count = 0;
8473 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8474 	chk->asoc = &stcb->asoc;
8475 	chk->data = cookie;
8476 	chk->whoTo = chk->asoc->primary_destination;
8477 	atomic_add_int(&chk->whoTo->ref_count, 1);
8478 	TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8479 	chk->asoc->ctrl_queue_cnt++;
8480 	return (0);
8481 }
8482 
8483 void
8484 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8485     struct mbuf *m,
8486     int offset,
8487     int chk_length,
8488     struct sctp_nets *net)
8489 {
8490 	/*
8491 	 * take a HB request and make it into a HB ack and send it.
8492 	 */
8493 	struct mbuf *outchain;
8494 	struct sctp_chunkhdr *chdr;
8495 	struct sctp_tmit_chunk *chk;
8496 
8497 
8498 	if (net == NULL)
8499 		/* must have a net pointer */
8500 		return;
8501 
8502 	outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
8503 	if (outchain == NULL) {
8504 		/* gak out of memory */
8505 		return;
8506 	}
8507 #ifdef SCTP_MBUF_LOGGING
8508 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8509 		struct mbuf *mat;
8510 
8511 		mat = outchain;
8512 		while (mat) {
8513 			if (SCTP_BUF_IS_EXTENDED(mat)) {
8514 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8515 			}
8516 			mat = SCTP_BUF_NEXT(mat);
8517 		}
8518 	}
8519 #endif
8520 	chdr = mtod(outchain, struct sctp_chunkhdr *);
8521 	chdr->chunk_type = SCTP_HEARTBEAT_ACK;
8522 	chdr->chunk_flags = 0;
8523 	if (chk_length % 4) {
8524 		/* need pad */
8525 		uint32_t cpthis = 0;
8526 		int padlen;
8527 
8528 		padlen = 4 - (chk_length % 4);
8529 		m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
8530 	}
8531 	sctp_alloc_a_chunk(stcb, chk);
8532 	if (chk == NULL) {
8533 		/* no memory */
8534 		sctp_m_freem(outchain);
8535 		return;
8536 	}
8537 	chk->copy_by_ref = 0;
8538 	chk->send_size = chk_length;
8539 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
8540 	chk->rec.chunk_id.can_take_data = 1;
8541 	chk->sent = SCTP_DATAGRAM_UNSENT;
8542 	chk->snd_count = 0;
8543 	chk->flags = 0;
8544 	chk->asoc = &stcb->asoc;
8545 	chk->data = outchain;
8546 	chk->whoTo = net;
8547 	atomic_add_int(&chk->whoTo->ref_count, 1);
8548 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8549 	chk->asoc->ctrl_queue_cnt++;
8550 }
8551 
8552 void
8553 sctp_send_cookie_ack(struct sctp_tcb *stcb)
8554 {
8555 	/* formulate and queue a cookie-ack back to sender */
8556 	struct mbuf *cookie_ack;
8557 	struct sctp_chunkhdr *hdr;
8558 	struct sctp_tmit_chunk *chk;
8559 
8560 	cookie_ack = NULL;
8561 	SCTP_TCB_LOCK_ASSERT(stcb);
8562 
8563 	cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
8564 	if (cookie_ack == NULL) {
8565 		/* no mbuf's */
8566 		return;
8567 	}
8568 	SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
8569 	sctp_alloc_a_chunk(stcb, chk);
8570 	if (chk == NULL) {
8571 		/* no memory */
8572 		sctp_m_freem(cookie_ack);
8573 		return;
8574 	}
8575 	chk->copy_by_ref = 0;
8576 	chk->send_size = sizeof(struct sctp_chunkhdr);
8577 	chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
8578 	chk->rec.chunk_id.can_take_data = 1;
8579 	chk->sent = SCTP_DATAGRAM_UNSENT;
8580 	chk->snd_count = 0;
8581 	chk->flags = 0;
8582 	chk->asoc = &stcb->asoc;
8583 	chk->data = cookie_ack;
8584 	if (chk->asoc->last_control_chunk_from != NULL) {
8585 		chk->whoTo = chk->asoc->last_control_chunk_from;
8586 	} else {
8587 		chk->whoTo = chk->asoc->primary_destination;
8588 	}
8589 	atomic_add_int(&chk->whoTo->ref_count, 1);
8590 	hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
8591 	hdr->chunk_type = SCTP_COOKIE_ACK;
8592 	hdr->chunk_flags = 0;
8593 	hdr->chunk_length = htons(chk->send_size);
8594 	SCTP_BUF_LEN(cookie_ack) = chk->send_size;
8595 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8596 	chk->asoc->ctrl_queue_cnt++;
8597 	return;
8598 }
8599 
8600 
8601 void
8602 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
8603 {
8604 	/* formulate and queue a SHUTDOWN-ACK back to the sender */
8605 	struct mbuf *m_shutdown_ack;
8606 	struct sctp_shutdown_ack_chunk *ack_cp;
8607 	struct sctp_tmit_chunk *chk;
8608 
8609 	m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8610 	if (m_shutdown_ack == NULL) {
8611 		/* no mbuf's */
8612 		return;
8613 	}
8614 	SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
8615 	sctp_alloc_a_chunk(stcb, chk);
8616 	if (chk == NULL) {
8617 		/* no memory */
8618 		sctp_m_freem(m_shutdown_ack);
8619 		return;
8620 	}
8621 	chk->copy_by_ref = 0;
8622 	chk->send_size = sizeof(struct sctp_chunkhdr);
8623 	chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
8624 	chk->rec.chunk_id.can_take_data = 1;
8625 	chk->sent = SCTP_DATAGRAM_UNSENT;
8626 	chk->snd_count = 0;
8627 	chk->flags = 0;
8628 	chk->asoc = &stcb->asoc;
8629 	chk->data = m_shutdown_ack;
8630 	chk->whoTo = net;
8631 	atomic_add_int(&net->ref_count, 1);
8632 
8633 	ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
8634 	ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
8635 	ack_cp->ch.chunk_flags = 0;
8636 	ack_cp->ch.chunk_length = htons(chk->send_size);
8637 	SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
8638 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8639 	chk->asoc->ctrl_queue_cnt++;
8640 	return;
8641 }
8642 
8643 void
8644 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
8645 {
8646 	/* formulate and queue a SHUTDOWN to the sender */
8647 	struct mbuf *m_shutdown;
8648 	struct sctp_shutdown_chunk *shutdown_cp;
8649 	struct sctp_tmit_chunk *chk;
8650 
8651 	m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8652 	if (m_shutdown == NULL) {
8653 		/* no mbuf's */
8654 		return;
8655 	}
8656 	SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
8657 	sctp_alloc_a_chunk(stcb, chk);
8658 	if (chk == NULL) {
8659 		/* no memory */
8660 		sctp_m_freem(m_shutdown);
8661 		return;
8662 	}
8663 	chk->copy_by_ref = 0;
8664 	chk->send_size = sizeof(struct sctp_shutdown_chunk);
8665 	chk->rec.chunk_id.id = SCTP_SHUTDOWN;
8666 	chk->rec.chunk_id.can_take_data = 1;
8667 	chk->sent = SCTP_DATAGRAM_UNSENT;
8668 	chk->snd_count = 0;
8669 	chk->flags = 0;
8670 	chk->asoc = &stcb->asoc;
8671 	chk->data = m_shutdown;
8672 	chk->whoTo = net;
8673 	atomic_add_int(&net->ref_count, 1);
8674 
8675 	shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
8676 	shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
8677 	shutdown_cp->ch.chunk_flags = 0;
8678 	shutdown_cp->ch.chunk_length = htons(chk->send_size);
8679 	shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
8680 	SCTP_BUF_LEN(m_shutdown) = chk->send_size;
8681 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8682 	chk->asoc->ctrl_queue_cnt++;
8683 	return;
8684 }
8685 
8686 void
8687 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
8688 {
8689 	/*
8690 	 * formulate and queue an ASCONF to the peer. ASCONF parameters
8691 	 * should be queued on the assoc queue.
8692 	 */
8693 	struct sctp_tmit_chunk *chk;
8694 	struct mbuf *m_asconf;
8695 	int len;
8696 
8697 	SCTP_TCB_LOCK_ASSERT(stcb);
8698 
8699 	if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
8700 	    (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
8701 		/* can't send a new one if there is one in flight already */
8702 		return;
8703 	}
8704 	/* compose an ASCONF chunk, maximum length is PMTU */
8705 	m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
8706 	if (m_asconf == NULL) {
8707 		return;
8708 	}
8709 	sctp_alloc_a_chunk(stcb, chk);
8710 	if (chk == NULL) {
8711 		/* no memory */
8712 		sctp_m_freem(m_asconf);
8713 		return;
8714 	}
8715 	chk->copy_by_ref = 0;
8716 	chk->data = m_asconf;
8717 	chk->send_size = len;
8718 	chk->rec.chunk_id.id = SCTP_ASCONF;
8719 	chk->rec.chunk_id.can_take_data = 0;
8720 	chk->sent = SCTP_DATAGRAM_UNSENT;
8721 	chk->snd_count = 0;
8722 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8723 	chk->asoc = &stcb->asoc;
8724 	chk->whoTo = net;
8725 	atomic_add_int(&chk->whoTo->ref_count, 1);
8726 	TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
8727 	chk->asoc->ctrl_queue_cnt++;
8728 	return;
8729 }
8730 
8731 void
8732 sctp_send_asconf_ack(struct sctp_tcb *stcb)
8733 {
8734 	/*
8735 	 * formulate and queue a asconf-ack back to sender. the asconf-ack
8736 	 * must be stored in the tcb.
8737 	 */
8738 	struct sctp_tmit_chunk *chk;
8739 	struct sctp_asconf_ack *ack, *latest_ack;
8740 	struct mbuf *m_ack, *m;
8741 	struct sctp_nets *net = NULL;
8742 
8743 	SCTP_TCB_LOCK_ASSERT(stcb);
8744 	/* Get the latest ASCONF-ACK */
8745 	latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
8746 	if (latest_ack == NULL) {
8747 		return;
8748 	}
8749 	if (latest_ack->last_sent_to != NULL &&
8750 	    latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
8751 		/* we're doing a retransmission */
8752 		net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
8753 		if (net == NULL) {
8754 			/* no alternate */
8755 			if (stcb->asoc.last_control_chunk_from == NULL)
8756 				net = stcb->asoc.primary_destination;
8757 			else
8758 				net = stcb->asoc.last_control_chunk_from;
8759 		}
8760 	} else {
8761 		/* normal case */
8762 		if (stcb->asoc.last_control_chunk_from == NULL)
8763 			net = stcb->asoc.primary_destination;
8764 		else
8765 			net = stcb->asoc.last_control_chunk_from;
8766 	}
8767 	latest_ack->last_sent_to = net;
8768 
8769 	TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
8770 		if (ack->data == NULL) {
8771 			continue;
8772 		}
8773 		/* copy the asconf_ack */
8774 		m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
8775 		if (m_ack == NULL) {
8776 			/* couldn't copy it */
8777 			return;
8778 		}
8779 #ifdef SCTP_MBUF_LOGGING
8780 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8781 			struct mbuf *mat;
8782 
8783 			mat = m_ack;
8784 			while (mat) {
8785 				if (SCTP_BUF_IS_EXTENDED(mat)) {
8786 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8787 				}
8788 				mat = SCTP_BUF_NEXT(mat);
8789 			}
8790 		}
8791 #endif
8792 
8793 		sctp_alloc_a_chunk(stcb, chk);
8794 		if (chk == NULL) {
8795 			/* no memory */
8796 			if (m_ack)
8797 				sctp_m_freem(m_ack);
8798 			return;
8799 		}
8800 		chk->copy_by_ref = 0;
8801 
8802 		chk->whoTo = net;
8803 		chk->data = m_ack;
8804 		chk->send_size = 0;
8805 		/* Get size */
8806 		m = m_ack;
8807 		chk->send_size = ack->len;
8808 		chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8809 		chk->rec.chunk_id.can_take_data = 1;
8810 		chk->sent = SCTP_DATAGRAM_UNSENT;
8811 		chk->snd_count = 0;
8812 		chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;	/* XXX */
8813 		chk->asoc = &stcb->asoc;
8814 		atomic_add_int(&chk->whoTo->ref_count, 1);
8815 
8816 		TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8817 		chk->asoc->ctrl_queue_cnt++;
8818 	}
8819 	return;
8820 }
8821 
8822 
8823 static int
8824 sctp_chunk_retransmission(struct sctp_inpcb *inp,
8825     struct sctp_tcb *stcb,
8826     struct sctp_association *asoc,
8827     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
8828 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8829     SCTP_UNUSED
8830 #endif
8831 )
8832 {
8833 	/*-
8834 	 * send out one MTU of retransmission. If fast_retransmit is
8835 	 * happening we ignore the cwnd. Otherwise we obey the cwnd and
8836 	 * rwnd. For a Cookie or Asconf in the control chunk queue we
8837 	 * retransmit them by themselves.
8838 	 *
8839 	 * For data chunks we will pick out the lowest TSN's in the sent_queue
8840 	 * marked for resend and bundle them all together (up to a MTU of
8841 	 * destination). The address to send to should have been
8842 	 * selected/changed where the retransmission was marked (i.e. in FR
8843 	 * or t3-timeout routines).
8844 	 */
8845 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8846 	struct sctp_tmit_chunk *chk, *fwd;
8847 	struct mbuf *m, *endofchain;
8848 	struct sctphdr *shdr;
8849 	struct sctp_nets *net = NULL;
8850 	uint32_t tsns_sent = 0;
8851 	int no_fragmentflg, bundle_at, cnt_thru;
8852 	unsigned int mtu;
8853 	int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
8854 	struct sctp_auth_chunk *auth = NULL;
8855 	uint32_t auth_offset = 0;
8856 	uint32_t dmtu = 0;
8857 
8858 	SCTP_TCB_LOCK_ASSERT(stcb);
8859 	tmr_started = ctl_cnt = bundle_at = error = 0;
8860 	no_fragmentflg = 1;
8861 	fwd_tsn = 0;
8862 	*cnt_out = 0;
8863 	fwd = NULL;
8864 	endofchain = m = NULL;
8865 #ifdef SCTP_AUDITING_ENABLED
8866 	sctp_audit_log(0xC3, 1);
8867 #endif
8868 	if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
8869 	    (TAILQ_EMPTY(&asoc->control_send_queue))) {
8870 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
8871 		    asoc->sent_queue_retran_cnt);
8872 		asoc->sent_queue_cnt = 0;
8873 		asoc->sent_queue_cnt_removeable = 0;
8874 		/* send back 0/0 so we enter normal transmission */
8875 		*cnt_out = 0;
8876 		return (0);
8877 	}
8878 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8879 		if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
8880 		    (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
8881 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
8882 			if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8883 				if (chk != asoc->str_reset) {
8884 					/*
8885 					 * not eligible for retran if its
8886 					 * not ours
8887 					 */
8888 					continue;
8889 				}
8890 			}
8891 			ctl_cnt++;
8892 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8893 				fwd_tsn = 1;
8894 				fwd = chk;
8895 			}
8896 			/*
8897 			 * Add an AUTH chunk, if chunk requires it save the
8898 			 * offset into the chain for AUTH
8899 			 */
8900 			if ((auth == NULL) &&
8901 			    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8902 			    stcb->asoc.peer_auth_chunks))) {
8903 				m = sctp_add_auth_chunk(m, &endofchain,
8904 				    &auth, &auth_offset,
8905 				    stcb,
8906 				    chk->rec.chunk_id.id);
8907 			}
8908 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8909 			break;
8910 		}
8911 	}
8912 	one_chunk = 0;
8913 	cnt_thru = 0;
8914 	/* do we have control chunks to retransmit? */
8915 	if (m != NULL) {
8916 		/* Start a timer no matter if we suceed or fail */
8917 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8918 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
8919 		} else if (chk->rec.chunk_id.id == SCTP_ASCONF)
8920 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
8921 
8922 		SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
8923 		if (m == NULL) {
8924 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
8925 			return (ENOBUFS);
8926 		}
8927 		shdr = mtod(m, struct sctphdr *);
8928 		shdr->src_port = inp->sctp_lport;
8929 		shdr->dest_port = stcb->rport;
8930 		shdr->v_tag = htonl(stcb->asoc.peer_vtag);
8931 		shdr->checksum = 0;
8932 		auth_offset += sizeof(struct sctphdr);
8933 		chk->snd_count++;	/* update our count */
8934 
8935 		if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
8936 		    (struct sockaddr *)&chk->whoTo->ro._l_addr, m, auth_offset,
8937 		    auth, no_fragmentflg, 0, NULL, 0, chk->whoTo->port, so_locked, NULL))) {
8938 			SCTP_STAT_INCR(sctps_lowlevelerr);
8939 			return (error);
8940 		}
8941 		m = endofchain = NULL;
8942 		auth = NULL;
8943 		auth_offset = 0;
8944 		/*
8945 		 * We don't want to mark the net->sent time here since this
8946 		 * we use this for HB and retrans cannot measure RTT
8947 		 */
8948 		/* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
8949 		*cnt_out += 1;
8950 		chk->sent = SCTP_DATAGRAM_SENT;
8951 		sctp_ucount_decr(asoc->sent_queue_retran_cnt);
8952 		if (fwd_tsn == 0) {
8953 			return (0);
8954 		} else {
8955 			/* Clean up the fwd-tsn list */
8956 			sctp_clean_up_ctl(stcb, asoc);
8957 			return (0);
8958 		}
8959 	}
8960 	/*
8961 	 * Ok, it is just data retransmission we need to do or that and a
8962 	 * fwd-tsn with it all.
8963 	 */
8964 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
8965 		return (SCTP_RETRAN_DONE);
8966 	}
8967 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
8968 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
8969 		/* not yet open, resend the cookie and that is it */
8970 		return (1);
8971 	}
8972 #ifdef SCTP_AUDITING_ENABLED
8973 	sctp_auditing(20, inp, stcb, NULL);
8974 #endif
8975 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
8976 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
8977 			/* No, not sent to this net or not ready for rtx */
8978 			continue;
8979 		}
8980 		if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
8981 		    (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
8982 			/* Gak, we have exceeded max unlucky retran, abort! */
8983 			SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
8984 			    chk->snd_count,
8985 			    SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
8986 			atomic_add_int(&stcb->asoc.refcnt, 1);
8987 			sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
8988 			SCTP_TCB_LOCK(stcb);
8989 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
8990 			return (SCTP_RETRAN_EXIT);
8991 		}
8992 		/* pick up the net */
8993 		net = chk->whoTo;
8994 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8995 			mtu = (net->mtu - SCTP_MIN_OVERHEAD);
8996 		} else {
8997 			mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8998 		}
8999 
9000 		if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9001 			/* No room in peers rwnd */
9002 			uint32_t tsn;
9003 
9004 			tsn = asoc->last_acked_seq + 1;
9005 			if (tsn == chk->rec.data.TSN_seq) {
9006 				/*
9007 				 * we make a special exception for this
9008 				 * case. The peer has no rwnd but is missing
9009 				 * the lowest chunk.. which is probably what
9010 				 * is holding up the rwnd.
9011 				 */
9012 				goto one_chunk_around;
9013 			}
9014 			return (1);
9015 		}
9016 one_chunk_around:
9017 		if (asoc->peers_rwnd < mtu) {
9018 			one_chunk = 1;
9019 			if ((asoc->peers_rwnd == 0) &&
9020 			    (asoc->total_flight == 0)) {
9021 				chk->window_probe = 1;
9022 				chk->whoTo->window_probe = 1;
9023 			}
9024 		}
9025 #ifdef SCTP_AUDITING_ENABLED
9026 		sctp_audit_log(0xC3, 2);
9027 #endif
9028 		bundle_at = 0;
9029 		m = NULL;
9030 		net->fast_retran_ip = 0;
9031 		if (chk->rec.data.doing_fast_retransmit == 0) {
9032 			/*
9033 			 * if no FR in progress skip destination that have
9034 			 * flight_size > cwnd.
9035 			 */
9036 			if (net->flight_size >= net->cwnd) {
9037 				continue;
9038 			}
9039 		} else {
9040 			/*
9041 			 * Mark the destination net to have FR recovery
9042 			 * limits put on it.
9043 			 */
9044 			*fr_done = 1;
9045 			net->fast_retran_ip = 1;
9046 		}
9047 
9048 		/*
9049 		 * if no AUTH is yet included and this chunk requires it,
9050 		 * make sure to account for it.  We don't apply the size
9051 		 * until the AUTH chunk is actually added below in case
9052 		 * there is no room for this chunk.
9053 		 */
9054 		if ((auth == NULL) &&
9055 		    sctp_auth_is_required_chunk(SCTP_DATA,
9056 		    stcb->asoc.peer_auth_chunks)) {
9057 			dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9058 		} else
9059 			dmtu = 0;
9060 
9061 		if ((chk->send_size <= (mtu - dmtu)) ||
9062 		    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9063 			/* ok we will add this one */
9064 			if ((auth == NULL) &&
9065 			    (sctp_auth_is_required_chunk(SCTP_DATA,
9066 			    stcb->asoc.peer_auth_chunks))) {
9067 				m = sctp_add_auth_chunk(m, &endofchain,
9068 				    &auth, &auth_offset,
9069 				    stcb, SCTP_DATA);
9070 			}
9071 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9072 			if (m == NULL) {
9073 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9074 				return (ENOMEM);
9075 			}
9076 			/* Do clear IP_DF ? */
9077 			if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9078 				no_fragmentflg = 0;
9079 			}
9080 			/* upate our MTU size */
9081 			if (mtu > (chk->send_size + dmtu))
9082 				mtu -= (chk->send_size + dmtu);
9083 			else
9084 				mtu = 0;
9085 			data_list[bundle_at++] = chk;
9086 			if (one_chunk && (asoc->total_flight <= 0)) {
9087 				SCTP_STAT_INCR(sctps_windowprobed);
9088 			}
9089 		}
9090 		if (one_chunk == 0) {
9091 			/*
9092 			 * now are there anymore forward from chk to pick
9093 			 * up?
9094 			 */
9095 			fwd = TAILQ_NEXT(chk, sctp_next);
9096 			while (fwd) {
9097 				if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9098 					/* Nope, not for retran */
9099 					fwd = TAILQ_NEXT(fwd, sctp_next);
9100 					continue;
9101 				}
9102 				if (fwd->whoTo != net) {
9103 					/* Nope, not the net in question */
9104 					fwd = TAILQ_NEXT(fwd, sctp_next);
9105 					continue;
9106 				}
9107 				if ((auth == NULL) &&
9108 				    sctp_auth_is_required_chunk(SCTP_DATA,
9109 				    stcb->asoc.peer_auth_chunks)) {
9110 					dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9111 				} else
9112 					dmtu = 0;
9113 				if (fwd->send_size <= (mtu - dmtu)) {
9114 					if ((auth == NULL) &&
9115 					    (sctp_auth_is_required_chunk(SCTP_DATA,
9116 					    stcb->asoc.peer_auth_chunks))) {
9117 						m = sctp_add_auth_chunk(m,
9118 						    &endofchain,
9119 						    &auth, &auth_offset,
9120 						    stcb,
9121 						    SCTP_DATA);
9122 					}
9123 					m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9124 					if (m == NULL) {
9125 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9126 						return (ENOMEM);
9127 					}
9128 					/* Do clear IP_DF ? */
9129 					if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9130 						no_fragmentflg = 0;
9131 					}
9132 					/* upate our MTU size */
9133 					if (mtu > (fwd->send_size + dmtu))
9134 						mtu -= (fwd->send_size + dmtu);
9135 					else
9136 						mtu = 0;
9137 					data_list[bundle_at++] = fwd;
9138 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9139 						break;
9140 					}
9141 					fwd = TAILQ_NEXT(fwd, sctp_next);
9142 				} else {
9143 					/* can't fit so we are done */
9144 					break;
9145 				}
9146 			}
9147 		}
9148 		/* Is there something to send for this destination? */
9149 		if (m) {
9150 			/*
9151 			 * No matter if we fail/or suceed we should start a
9152 			 * timer. A failure is like a lost IP packet :-)
9153 			 */
9154 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9155 				/*
9156 				 * no timer running on this destination
9157 				 * restart it.
9158 				 */
9159 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9160 				tmr_started = 1;
9161 			}
9162 			SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
9163 			if (m == NULL) {
9164 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
9165 				return (ENOBUFS);
9166 			}
9167 			shdr = mtod(m, struct sctphdr *);
9168 			shdr->src_port = inp->sctp_lport;
9169 			shdr->dest_port = stcb->rport;
9170 			shdr->v_tag = htonl(stcb->asoc.peer_vtag);
9171 			shdr->checksum = 0;
9172 			auth_offset += sizeof(struct sctphdr);
9173 			/* Now lets send it, if there is anything to send :> */
9174 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9175 			    (struct sockaddr *)&net->ro._l_addr, m, auth_offset,
9176 			    auth, no_fragmentflg, 0, NULL, 0, net->port, so_locked, NULL))) {
9177 				/* error, we could not output */
9178 				SCTP_STAT_INCR(sctps_lowlevelerr);
9179 				return (error);
9180 			}
9181 			m = endofchain = NULL;
9182 			auth = NULL;
9183 			auth_offset = 0;
9184 			/* For HB's */
9185 			/*
9186 			 * We don't want to mark the net->sent time here
9187 			 * since this we use this for HB and retrans cannot
9188 			 * measure RTT
9189 			 */
9190 			/* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9191 
9192 			/* For auto-close */
9193 			cnt_thru++;
9194 			if (*now_filled == 0) {
9195 				(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9196 				*now = asoc->time_last_sent;
9197 				*now_filled = 1;
9198 			} else {
9199 				asoc->time_last_sent = *now;
9200 			}
9201 			*cnt_out += bundle_at;
9202 #ifdef SCTP_AUDITING_ENABLED
9203 			sctp_audit_log(0xC4, bundle_at);
9204 #endif
9205 			if (bundle_at) {
9206 				tsns_sent = data_list[0]->rec.data.TSN_seq;
9207 			}
9208 			for (i = 0; i < bundle_at; i++) {
9209 				SCTP_STAT_INCR(sctps_sendretransdata);
9210 				data_list[i]->sent = SCTP_DATAGRAM_SENT;
9211 				/*
9212 				 * When we have a revoked data, and we
9213 				 * retransmit it, then we clear the revoked
9214 				 * flag since this flag dictates if we
9215 				 * subtracted from the fs
9216 				 */
9217 				if (data_list[i]->rec.data.chunk_was_revoked) {
9218 					/* Deflate the cwnd */
9219 					data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9220 					data_list[i]->rec.data.chunk_was_revoked = 0;
9221 				}
9222 				data_list[i]->snd_count++;
9223 				sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9224 				/* record the time */
9225 				data_list[i]->sent_rcv_time = asoc->time_last_sent;
9226 				if (data_list[i]->book_size_scale) {
9227 					/*
9228 					 * need to double the book size on
9229 					 * this one
9230 					 */
9231 					data_list[i]->book_size_scale = 0;
9232 					/*
9233 					 * Since we double the booksize, we
9234 					 * must also double the output queue
9235 					 * size, since this get shrunk when
9236 					 * we free by this amount.
9237 					 */
9238 					atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9239 					data_list[i]->book_size *= 2;
9240 
9241 
9242 				} else {
9243 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9244 						sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9245 						    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9246 					}
9247 					asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9248 					    (uint32_t) (data_list[i]->send_size +
9249 					    SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9250 				}
9251 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9252 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9253 					    data_list[i]->whoTo->flight_size,
9254 					    data_list[i]->book_size,
9255 					    (uintptr_t) data_list[i]->whoTo,
9256 					    data_list[i]->rec.data.TSN_seq);
9257 				}
9258 				sctp_flight_size_increase(data_list[i]);
9259 				sctp_total_flight_increase(stcb, data_list[i]);
9260 				if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9261 					/* SWS sender side engages */
9262 					asoc->peers_rwnd = 0;
9263 				}
9264 				if ((i == 0) &&
9265 				    (data_list[i]->rec.data.doing_fast_retransmit)) {
9266 					SCTP_STAT_INCR(sctps_sendfastretrans);
9267 					if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9268 					    (tmr_started == 0)) {
9269 						/*-
9270 						 * ok we just fast-retrans'd
9271 						 * the lowest TSN, i.e the
9272 						 * first on the list. In
9273 						 * this case we want to give
9274 						 * some more time to get a
9275 						 * SACK back without a
9276 						 * t3-expiring.
9277 						 */
9278 						sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9279 						    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9280 						sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9281 					}
9282 				}
9283 			}
9284 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9285 				sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9286 			}
9287 #ifdef SCTP_AUDITING_ENABLED
9288 			sctp_auditing(21, inp, stcb, NULL);
9289 #endif
9290 		} else {
9291 			/* None will fit */
9292 			return (1);
9293 		}
9294 		if (asoc->sent_queue_retran_cnt <= 0) {
9295 			/* all done we have no more to retran */
9296 			asoc->sent_queue_retran_cnt = 0;
9297 			break;
9298 		}
9299 		if (one_chunk) {
9300 			/* No more room in rwnd */
9301 			return (1);
9302 		}
9303 		/* stop the for loop here. we sent out a packet */
9304 		break;
9305 	}
9306 	return (0);
9307 }
9308 
9309 
9310 static int
9311 sctp_timer_validation(struct sctp_inpcb *inp,
9312     struct sctp_tcb *stcb,
9313     struct sctp_association *asoc,
9314     int ret)
9315 {
9316 	struct sctp_nets *net;
9317 
9318 	/* Validate that a timer is running somewhere */
9319 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9320 		if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9321 			/* Here is a timer */
9322 			return (ret);
9323 		}
9324 	}
9325 	SCTP_TCB_LOCK_ASSERT(stcb);
9326 	/* Gak, we did not have a timer somewhere */
9327 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9328 	sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9329 	return (ret);
9330 }
9331 
9332 void
9333 sctp_chunk_output(struct sctp_inpcb *inp,
9334     struct sctp_tcb *stcb,
9335     int from_where,
9336     int so_locked
9337 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9338     SCTP_UNUSED
9339 #endif
9340 )
9341 {
9342 	/*-
9343 	 * Ok this is the generic chunk service queue. we must do the
9344 	 * following:
9345 	 * - See if there are retransmits pending, if so we must
9346 	 *   do these first.
9347 	 * - Service the stream queue that is next, moving any
9348 	 *   message (note I must get a complete message i.e.
9349 	 *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9350 	 *   TSN's
9351 	 * - Check to see if the cwnd/rwnd allows any output, if so we
9352 	 *   go ahead and fomulate and send the low level chunks. Making sure
9353 	 *   to combine any control in the control chunk queue also.
9354 	 */
9355 	struct sctp_association *asoc;
9356 	struct sctp_nets *net;
9357 	int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
9358 	    burst_cnt = 0, burst_limit = 0;
9359 	struct timeval now;
9360 	int now_filled = 0;
9361 	int cwnd_full = 0;
9362 	int nagle_on = 0;
9363 	int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9364 	int un_sent = 0;
9365 	int fr_done, tot_frs = 0;
9366 
9367 	asoc = &stcb->asoc;
9368 	if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9369 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9370 			nagle_on = 0;
9371 		} else {
9372 			nagle_on = 1;
9373 		}
9374 	}
9375 	SCTP_TCB_LOCK_ASSERT(stcb);
9376 
9377 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9378 
9379 	if ((un_sent <= 0) &&
9380 	    (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9381 	    (asoc->sent_queue_retran_cnt == 0)) {
9382 		/* Nothing to do unless there is something to be sent left */
9383 		return;
9384 	}
9385 	/*
9386 	 * Do we have something to send, data or control AND a sack timer
9387 	 * running, if so piggy-back the sack.
9388 	 */
9389 	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9390 		sctp_send_sack(stcb);
9391 		(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9392 	}
9393 	while (asoc->sent_queue_retran_cnt) {
9394 		/*-
9395 		 * Ok, it is retransmission time only, we send out only ONE
9396 		 * packet with a single call off to the retran code.
9397 		 */
9398 		if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9399 			/*-
9400 			 * Special hook for handling cookiess discarded
9401 			 * by peer that carried data. Send cookie-ack only
9402 			 * and then the next call with get the retran's.
9403 			 */
9404 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9405 			    &cwnd_full, from_where,
9406 			    &now, &now_filled, frag_point, so_locked);
9407 			return;
9408 		} else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9409 			/* if its not from a HB then do it */
9410 			fr_done = 0;
9411 			ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9412 			if (fr_done) {
9413 				tot_frs++;
9414 			}
9415 		} else {
9416 			/*
9417 			 * its from any other place, we don't allow retran
9418 			 * output (only control)
9419 			 */
9420 			ret = 1;
9421 		}
9422 		if (ret > 0) {
9423 			/* Can't send anymore */
9424 			/*-
9425 			 * now lets push out control by calling med-level
9426 			 * output once. this assures that we WILL send HB's
9427 			 * if queued too.
9428 			 */
9429 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9430 			    &cwnd_full, from_where,
9431 			    &now, &now_filled, frag_point, so_locked);
9432 #ifdef SCTP_AUDITING_ENABLED
9433 			sctp_auditing(8, inp, stcb, NULL);
9434 #endif
9435 			(void)sctp_timer_validation(inp, stcb, asoc, ret);
9436 			return;
9437 		}
9438 		if (ret < 0) {
9439 			/*-
9440 			 * The count was off.. retran is not happening so do
9441 			 * the normal retransmission.
9442 			 */
9443 #ifdef SCTP_AUDITING_ENABLED
9444 			sctp_auditing(9, inp, stcb, NULL);
9445 #endif
9446 			if (ret == SCTP_RETRAN_EXIT) {
9447 				return;
9448 			}
9449 			break;
9450 		}
9451 		if (from_where == SCTP_OUTPUT_FROM_T3) {
9452 			/* Only one transmission allowed out of a timeout */
9453 #ifdef SCTP_AUDITING_ENABLED
9454 			sctp_auditing(10, inp, stcb, NULL);
9455 #endif
9456 			/* Push out any control */
9457 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
9458 			    &now, &now_filled, frag_point, so_locked);
9459 			return;
9460 		}
9461 		if (tot_frs > asoc->max_burst) {
9462 			/* Hit FR burst limit */
9463 			return;
9464 		}
9465 		if ((num_out == 0) && (ret == 0)) {
9466 
9467 			/* No more retrans to send */
9468 			break;
9469 		}
9470 	}
9471 #ifdef SCTP_AUDITING_ENABLED
9472 	sctp_auditing(12, inp, stcb, NULL);
9473 #endif
9474 	/* Check for bad destinations, if they exist move chunks around. */
9475 	burst_limit = asoc->max_burst;
9476 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9477 		if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
9478 		    SCTP_ADDR_NOT_REACHABLE) {
9479 			/*-
9480 			 * if possible move things off of this address we
9481 			 * still may send below due to the dormant state but
9482 			 * we try to find an alternate address to send to
9483 			 * and if we have one we move all queued data on the
9484 			 * out wheel to this alternate address.
9485 			 */
9486 			if (net->ref_count > 1)
9487 				sctp_move_to_an_alt(stcb, asoc, net);
9488 		} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
9489 			    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
9490 		    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
9491 			/*
9492 			 * JRS 5/14/07 - If CMT PF is on and the current
9493 			 * destination is in PF state, move all queued data
9494 			 * to an alternate desination.
9495 			 */
9496 			if (net->ref_count > 1)
9497 				sctp_move_to_an_alt(stcb, asoc, net);
9498 		} else {
9499 			/*-
9500 			 * if ((asoc->sat_network) || (net->addr_is_local))
9501 			 * { burst_limit = asoc->max_burst *
9502 			 * SCTP_SAT_NETWORK_BURST_INCR; }
9503 			 */
9504 			if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
9505 				if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
9506 					/*
9507 					 * JRS - Use the congestion control
9508 					 * given in the congestion control
9509 					 * module
9510 					 */
9511 					asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
9512 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9513 						sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
9514 					}
9515 					SCTP_STAT_INCR(sctps_maxburstqueued);
9516 				}
9517 				net->fast_retran_ip = 0;
9518 			} else {
9519 				if (net->flight_size == 0) {
9520 					/* Should be decaying the cwnd here */
9521 					;
9522 				}
9523 			}
9524 		}
9525 
9526 	}
9527 	burst_cnt = 0;
9528 	cwnd_full = 0;
9529 	do {
9530 		error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
9531 		    &reason_code, 0, &cwnd_full, from_where,
9532 		    &now, &now_filled, frag_point, so_locked);
9533 		if (error) {
9534 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
9535 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9536 				sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
9537 			}
9538 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9539 				sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
9540 				sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
9541 			}
9542 			break;
9543 		}
9544 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
9545 
9546 		tot_out += num_out;
9547 		burst_cnt++;
9548 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9549 			sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
9550 			if (num_out == 0) {
9551 				sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
9552 			}
9553 		}
9554 		if (nagle_on) {
9555 			/*-
9556 			 * When nagle is on, we look at how much is un_sent, then
9557 			 * if its smaller than an MTU and we have data in
9558 			 * flight we stop.
9559 			 */
9560 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9561 			    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
9562 			if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
9563 			    (stcb->asoc.total_flight > 0)) {
9564 				break;
9565 			}
9566 		}
9567 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9568 		    TAILQ_EMPTY(&asoc->send_queue) &&
9569 		    TAILQ_EMPTY(&asoc->out_wheel)) {
9570 			/* Nothing left to send */
9571 			break;
9572 		}
9573 		if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
9574 			/* Nothing left to send */
9575 			break;
9576 		}
9577 	} while (num_out && (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
9578 	    (burst_cnt < burst_limit)));
9579 
9580 	if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
9581 		if (burst_cnt >= burst_limit) {
9582 			SCTP_STAT_INCR(sctps_maxburstqueued);
9583 			asoc->burst_limit_applied = 1;
9584 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9585 				sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
9586 			}
9587 		} else {
9588 			asoc->burst_limit_applied = 0;
9589 		}
9590 	}
9591 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9592 		sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
9593 	}
9594 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
9595 	    tot_out);
9596 
9597 	/*-
9598 	 * Now we need to clean up the control chunk chain if a ECNE is on
9599 	 * it. It must be marked as UNSENT again so next call will continue
9600 	 * to send it until such time that we get a CWR, to remove it.
9601 	 */
9602 	if (stcb->asoc.ecn_echo_cnt_onq)
9603 		sctp_fix_ecn_echo(asoc);
9604 	return;
9605 }
9606 
9607 
9608 int
9609 sctp_output(inp, m, addr, control, p, flags)
9610 	struct sctp_inpcb *inp;
9611 	struct mbuf *m;
9612 	struct sockaddr *addr;
9613 	struct mbuf *control;
9614 	struct thread *p;
9615 	int flags;
9616 {
9617 	if (inp == NULL) {
9618 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9619 		return (EINVAL);
9620 	}
9621 	if (inp->sctp_socket == NULL) {
9622 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9623 		return (EINVAL);
9624 	}
9625 	return (sctp_sosend(inp->sctp_socket,
9626 	    addr,
9627 	    (struct uio *)NULL,
9628 	    m,
9629 	    control,
9630 	    flags, p
9631 	    ));
9632 }
9633 
9634 void
9635 send_forward_tsn(struct sctp_tcb *stcb,
9636     struct sctp_association *asoc)
9637 {
9638 	struct sctp_tmit_chunk *chk;
9639 	struct sctp_forward_tsn_chunk *fwdtsn;
9640 
9641 	SCTP_TCB_LOCK_ASSERT(stcb);
9642 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9643 		if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9644 			/* mark it to unsent */
9645 			chk->sent = SCTP_DATAGRAM_UNSENT;
9646 			chk->snd_count = 0;
9647 			/* Do we correct its output location? */
9648 			if (chk->whoTo != asoc->primary_destination) {
9649 				sctp_free_remote_addr(chk->whoTo);
9650 				chk->whoTo = asoc->primary_destination;
9651 				atomic_add_int(&chk->whoTo->ref_count, 1);
9652 			}
9653 			goto sctp_fill_in_rest;
9654 		}
9655 	}
9656 	/* Ok if we reach here we must build one */
9657 	sctp_alloc_a_chunk(stcb, chk);
9658 	if (chk == NULL) {
9659 		return;
9660 	}
9661 	chk->copy_by_ref = 0;
9662 	chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
9663 	chk->rec.chunk_id.can_take_data = 0;
9664 	chk->asoc = asoc;
9665 	chk->whoTo = NULL;
9666 
9667 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9668 	if (chk->data == NULL) {
9669 		sctp_free_a_chunk(stcb, chk);
9670 		return;
9671 	}
9672 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9673 	chk->sent = SCTP_DATAGRAM_UNSENT;
9674 	chk->snd_count = 0;
9675 	chk->whoTo = asoc->primary_destination;
9676 	atomic_add_int(&chk->whoTo->ref_count, 1);
9677 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
9678 	asoc->ctrl_queue_cnt++;
9679 sctp_fill_in_rest:
9680 	/*-
9681 	 * Here we go through and fill out the part that deals with
9682 	 * stream/seq of the ones we skip.
9683 	 */
9684 	SCTP_BUF_LEN(chk->data) = 0;
9685 	{
9686 		struct sctp_tmit_chunk *at, *tp1, *last;
9687 		struct sctp_strseq *strseq;
9688 		unsigned int cnt_of_space, i, ovh;
9689 		unsigned int space_needed;
9690 		unsigned int cnt_of_skipped = 0;
9691 
9692 		TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
9693 			if (at->sent != SCTP_FORWARD_TSN_SKIP) {
9694 				/* no more to look at */
9695 				break;
9696 			}
9697 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9698 				/* We don't report these */
9699 				continue;
9700 			}
9701 			cnt_of_skipped++;
9702 		}
9703 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
9704 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
9705 
9706 		cnt_of_space = M_TRAILINGSPACE(chk->data);
9707 
9708 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9709 			ovh = SCTP_MIN_OVERHEAD;
9710 		} else {
9711 			ovh = SCTP_MIN_V4_OVERHEAD;
9712 		}
9713 		if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
9714 			/* trim to a mtu size */
9715 			cnt_of_space = asoc->smallest_mtu - ovh;
9716 		}
9717 		if (cnt_of_space < space_needed) {
9718 			/*-
9719 			 * ok we must trim down the chunk by lowering the
9720 			 * advance peer ack point.
9721 			 */
9722 			cnt_of_skipped = (cnt_of_space -
9723 			    ((sizeof(struct sctp_forward_tsn_chunk)) /
9724 			    sizeof(struct sctp_strseq)));
9725 			/*-
9726 			 * Go through and find the TSN that will be the one
9727 			 * we report.
9728 			 */
9729 			at = TAILQ_FIRST(&asoc->sent_queue);
9730 			for (i = 0; i < cnt_of_skipped; i++) {
9731 				tp1 = TAILQ_NEXT(at, sctp_next);
9732 				at = tp1;
9733 			}
9734 			last = at;
9735 			/*-
9736 			 * last now points to last one I can report, update
9737 			 * peer ack point
9738 			 */
9739 			asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
9740 			space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
9741 		}
9742 		chk->send_size = space_needed;
9743 		/* Setup the chunk */
9744 		fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
9745 		fwdtsn->ch.chunk_length = htons(chk->send_size);
9746 		fwdtsn->ch.chunk_flags = 0;
9747 		fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
9748 		fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
9749 		chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
9750 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
9751 		SCTP_BUF_LEN(chk->data) = chk->send_size;
9752 		fwdtsn++;
9753 		/*-
9754 		 * Move pointer to after the fwdtsn and transfer to the
9755 		 * strseq pointer.
9756 		 */
9757 		strseq = (struct sctp_strseq *)fwdtsn;
9758 		/*-
9759 		 * Now populate the strseq list. This is done blindly
9760 		 * without pulling out duplicate stream info. This is
9761 		 * inefficent but won't harm the process since the peer will
9762 		 * look at these in sequence and will thus release anything.
9763 		 * It could mean we exceed the PMTU and chop off some that
9764 		 * we could have included.. but this is unlikely (aka 1432/4
9765 		 * would mean 300+ stream seq's would have to be reported in
9766 		 * one FWD-TSN. With a bit of work we can later FIX this to
9767 		 * optimize and pull out duplcates.. but it does add more
9768 		 * overhead. So for now... not!
9769 		 */
9770 		at = TAILQ_FIRST(&asoc->sent_queue);
9771 		for (i = 0; i < cnt_of_skipped; i++) {
9772 			tp1 = TAILQ_NEXT(at, sctp_next);
9773 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9774 				/* We don't report these */
9775 				i--;
9776 				at = tp1;
9777 				continue;
9778 			}
9779 			strseq->stream = ntohs(at->rec.data.stream_number);
9780 			strseq->sequence = ntohs(at->rec.data.stream_seq);
9781 			strseq++;
9782 			at = tp1;
9783 		}
9784 	}
9785 	return;
9786 
9787 }
9788 
9789 void
9790 sctp_send_sack(struct sctp_tcb *stcb)
9791 {
9792 	/*-
9793 	 * Queue up a SACK in the control queue. We must first check to see
9794 	 * if a SACK is somehow on the control queue. If so, we will take
9795 	 * and and remove the old one.
9796 	 */
9797 	struct sctp_association *asoc;
9798 	struct sctp_tmit_chunk *chk, *a_chk;
9799 	struct sctp_sack_chunk *sack;
9800 	struct sctp_gap_ack_block *gap_descriptor;
9801 	struct sack_track *selector;
9802 	int mergeable = 0;
9803 	int offset;
9804 	caddr_t limit;
9805 	uint32_t *dup;
9806 	int limit_reached = 0;
9807 	unsigned int i, jstart, siz, j;
9808 	unsigned int num_gap_blocks = 0, space;
9809 	int num_dups = 0;
9810 	int space_req;
9811 
9812 	a_chk = NULL;
9813 	asoc = &stcb->asoc;
9814 	SCTP_TCB_LOCK_ASSERT(stcb);
9815 	if (asoc->last_data_chunk_from == NULL) {
9816 		/* Hmm we never received anything */
9817 		return;
9818 	}
9819 	sctp_set_rwnd(stcb, asoc);
9820 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9821 		if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
9822 			/* Hmm, found a sack already on queue, remove it */
9823 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
9824 			asoc->ctrl_queue_cnt++;
9825 			a_chk = chk;
9826 			if (a_chk->data) {
9827 				sctp_m_freem(a_chk->data);
9828 				a_chk->data = NULL;
9829 			}
9830 			sctp_free_remote_addr(a_chk->whoTo);
9831 			a_chk->whoTo = NULL;
9832 			break;
9833 		}
9834 	}
9835 	if (a_chk == NULL) {
9836 		sctp_alloc_a_chunk(stcb, a_chk);
9837 		if (a_chk == NULL) {
9838 			/* No memory so we drop the idea, and set a timer */
9839 			if (stcb->asoc.delayed_ack) {
9840 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9841 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
9842 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9843 				    stcb->sctp_ep, stcb, NULL);
9844 			} else {
9845 				stcb->asoc.send_sack = 1;
9846 			}
9847 			return;
9848 		}
9849 		a_chk->copy_by_ref = 0;
9850 		/* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
9851 		a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK;
9852 		a_chk->rec.chunk_id.can_take_data = 1;
9853 	}
9854 	/* Clear our pkt counts */
9855 	asoc->data_pkts_seen = 0;
9856 
9857 	a_chk->asoc = asoc;
9858 	a_chk->snd_count = 0;
9859 	a_chk->send_size = 0;	/* fill in later */
9860 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
9861 	a_chk->whoTo = NULL;
9862 
9863 	if ((asoc->numduptsns) ||
9864 	    (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
9865 	    ) {
9866 		/*-
9867 		 * Ok, we have some duplicates or the destination for the
9868 		 * sack is unreachable, lets see if we can select an
9869 		 * alternate than asoc->last_data_chunk_from
9870 		 */
9871 		if ((!(asoc->last_data_chunk_from->dest_state &
9872 		    SCTP_ADDR_NOT_REACHABLE)) &&
9873 		    (asoc->used_alt_onsack > asoc->numnets)) {
9874 			/* We used an alt last time, don't this time */
9875 			a_chk->whoTo = NULL;
9876 		} else {
9877 			asoc->used_alt_onsack++;
9878 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
9879 		}
9880 		if (a_chk->whoTo == NULL) {
9881 			/* Nope, no alternate */
9882 			a_chk->whoTo = asoc->last_data_chunk_from;
9883 			asoc->used_alt_onsack = 0;
9884 		}
9885 	} else {
9886 		/*
9887 		 * No duplicates so we use the last place we received data
9888 		 * from.
9889 		 */
9890 		asoc->used_alt_onsack = 0;
9891 		a_chk->whoTo = asoc->last_data_chunk_from;
9892 	}
9893 	if (a_chk->whoTo) {
9894 		atomic_add_int(&a_chk->whoTo->ref_count, 1);
9895 	}
9896 	if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
9897 		/* no gaps */
9898 		space_req = sizeof(struct sctp_sack_chunk);
9899 	} else {
9900 		/* gaps get a cluster */
9901 		space_req = MCLBYTES;
9902 	}
9903 	/* Ok now lets formulate a MBUF with our sack */
9904 	a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
9905 	if ((a_chk->data == NULL) ||
9906 	    (a_chk->whoTo == NULL)) {
9907 		/* rats, no mbuf memory */
9908 		if (a_chk->data) {
9909 			/* was a problem with the destination */
9910 			sctp_m_freem(a_chk->data);
9911 			a_chk->data = NULL;
9912 		}
9913 		sctp_free_a_chunk(stcb, a_chk);
9914 		/* sa_ignore NO_NULL_CHK */
9915 		if (stcb->asoc.delayed_ack) {
9916 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9917 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
9918 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9919 			    stcb->sctp_ep, stcb, NULL);
9920 		} else {
9921 			stcb->asoc.send_sack = 1;
9922 		}
9923 		return;
9924 	}
9925 	/* ok, lets go through and fill it in */
9926 	SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
9927 	space = M_TRAILINGSPACE(a_chk->data);
9928 	if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
9929 		space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
9930 	}
9931 	limit = mtod(a_chk->data, caddr_t);
9932 	limit += space;
9933 
9934 	sack = mtod(a_chk->data, struct sctp_sack_chunk *);
9935 	sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
9936 	/* 0x01 is used by nonce for ecn */
9937 	if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
9938 	    (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
9939 	    (asoc->peer_supports_ecn_nonce))
9940 		sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
9941 	else
9942 		sack->ch.chunk_flags = 0;
9943 
9944 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
9945 		/*-
9946 		 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
9947 		 * received, then set high bit to 1, else 0. Reset
9948 		 * pkts_rcvd.
9949 		 */
9950 		sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
9951 		asoc->cmt_dac_pkts_rcvd = 0;
9952 	}
9953 #ifdef SCTP_ASOCLOG_OF_TSNS
9954 	stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
9955 	stcb->asoc.cumack_log_atsnt++;
9956 	if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
9957 		stcb->asoc.cumack_log_atsnt = 0;
9958 	}
9959 #endif
9960 	sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
9961 	sack->sack.a_rwnd = htonl(asoc->my_rwnd);
9962 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
9963 
9964 	/* reset the readers interpretation */
9965 	stcb->freed_by_sorcv_sincelast = 0;
9966 
9967 	gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
9968 
9969 	siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
9970 	if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
9971 		offset = 1;
9972 		/*-
9973 		 * cum-ack behind the mapping array, so we start and use all
9974 		 * entries.
9975 		 */
9976 		jstart = 0;
9977 	} else {
9978 		offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
9979 		/*-
9980 		 * we skip the first one when the cum-ack is at or above the
9981 		 * mapping array base. Note this only works if
9982 		 */
9983 		jstart = 1;
9984 	}
9985 	if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
9986 		/* we have a gap .. maybe */
9987 		for (i = 0; i < siz; i++) {
9988 			selector = &sack_array[asoc->mapping_array[i]];
9989 			if (mergeable && selector->right_edge) {
9990 				/*
9991 				 * Backup, left and right edges were ok to
9992 				 * merge.
9993 				 */
9994 				num_gap_blocks--;
9995 				gap_descriptor--;
9996 			}
9997 			if (selector->num_entries == 0)
9998 				mergeable = 0;
9999 			else {
10000 				for (j = jstart; j < selector->num_entries; j++) {
10001 					if (mergeable && selector->right_edge) {
10002 						/*
10003 						 * do a merge by NOT setting
10004 						 * the left side
10005 						 */
10006 						mergeable = 0;
10007 					} else {
10008 						/*
10009 						 * no merge, set the left
10010 						 * side
10011 						 */
10012 						mergeable = 0;
10013 						gap_descriptor->start = htons((selector->gaps[j].start + offset));
10014 					}
10015 					gap_descriptor->end = htons((selector->gaps[j].end + offset));
10016 					num_gap_blocks++;
10017 					gap_descriptor++;
10018 					if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10019 						/* no more room */
10020 						limit_reached = 1;
10021 						break;
10022 					}
10023 				}
10024 				if (selector->left_edge) {
10025 					mergeable = 1;
10026 				}
10027 			}
10028 			if (limit_reached) {
10029 				/* Reached the limit stop */
10030 				break;
10031 			}
10032 			jstart = 0;
10033 			offset += 8;
10034 		}
10035 		if (num_gap_blocks == 0) {
10036 			/*
10037 			 * slide not yet happened, and somehow we got called
10038 			 * to send a sack. Cumack needs to move up.
10039 			 */
10040 			int abort_flag = 0;
10041 
10042 			asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
10043 			sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10044 			sctp_sack_check(stcb, 0, 0, &abort_flag);
10045 		}
10046 	}
10047 	/* now we must add any dups we are going to report. */
10048 	if ((limit_reached == 0) && (asoc->numduptsns)) {
10049 		dup = (uint32_t *) gap_descriptor;
10050 		for (i = 0; i < asoc->numduptsns; i++) {
10051 			*dup = htonl(asoc->dup_tsns[i]);
10052 			dup++;
10053 			num_dups++;
10054 			if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10055 				/* no more room */
10056 				break;
10057 			}
10058 		}
10059 		asoc->numduptsns = 0;
10060 	}
10061 	/*
10062 	 * now that the chunk is prepared queue it to the control chunk
10063 	 * queue.
10064 	 */
10065 	a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
10066 	    (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
10067 	    (num_dups * sizeof(int32_t)));
10068 	SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10069 	sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10070 	sack->sack.num_dup_tsns = htons(num_dups);
10071 	sack->ch.chunk_length = htons(a_chk->send_size);
10072 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10073 	asoc->ctrl_queue_cnt++;
10074 	asoc->send_sack = 0;
10075 	SCTP_STAT_INCR(sctps_sendsacks);
10076 	return;
10077 }
10078 
10079 
10080 void
10081 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10082 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10083     SCTP_UNUSED
10084 #endif
10085 )
10086 {
10087 	struct mbuf *m_abort;
10088 	struct mbuf *m_out = NULL, *m_end = NULL;
10089 	struct sctp_abort_chunk *abort = NULL;
10090 	int sz;
10091 	uint32_t auth_offset = 0;
10092 	struct sctp_auth_chunk *auth = NULL;
10093 	struct sctphdr *shdr;
10094 
10095 	/*-
10096 	 * Add an AUTH chunk, if chunk requires it and save the offset into
10097 	 * the chain for AUTH
10098 	 */
10099 	if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10100 	    stcb->asoc.peer_auth_chunks)) {
10101 		m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10102 		    stcb, SCTP_ABORT_ASSOCIATION);
10103 	}
10104 	SCTP_TCB_LOCK_ASSERT(stcb);
10105 	m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10106 	if (m_abort == NULL) {
10107 		/* no mbuf's */
10108 		if (m_out)
10109 			sctp_m_freem(m_out);
10110 		return;
10111 	}
10112 	/* link in any error */
10113 	SCTP_BUF_NEXT(m_abort) = operr;
10114 	sz = 0;
10115 	if (operr) {
10116 		struct mbuf *n;
10117 
10118 		n = operr;
10119 		while (n) {
10120 			sz += SCTP_BUF_LEN(n);
10121 			n = SCTP_BUF_NEXT(n);
10122 		}
10123 	}
10124 	SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10125 	if (m_out == NULL) {
10126 		/* NO Auth chunk prepended, so reserve space in front */
10127 		SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10128 		m_out = m_abort;
10129 	} else {
10130 		/* Put AUTH chunk at the front of the chain */
10131 		SCTP_BUF_NEXT(m_end) = m_abort;
10132 	}
10133 
10134 	/* fill in the ABORT chunk */
10135 	abort = mtod(m_abort, struct sctp_abort_chunk *);
10136 	abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10137 	abort->ch.chunk_flags = 0;
10138 	abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10139 
10140 	/* prepend and fill in the SCTP header */
10141 	SCTP_BUF_PREPEND(m_out, sizeof(struct sctphdr), M_DONTWAIT);
10142 	if (m_out == NULL) {
10143 		/* TSNH: no memory */
10144 		return;
10145 	}
10146 	shdr = mtod(m_out, struct sctphdr *);
10147 	shdr->src_port = stcb->sctp_ep->sctp_lport;
10148 	shdr->dest_port = stcb->rport;
10149 	shdr->v_tag = htonl(stcb->asoc.peer_vtag);
10150 	shdr->checksum = 0;
10151 	auth_offset += sizeof(struct sctphdr);
10152 
10153 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
10154 	    stcb->asoc.primary_destination,
10155 	    (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
10156 	    m_out, auth_offset, auth, 1, 0, NULL, 0, stcb->asoc.primary_destination->port, so_locked, NULL);
10157 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10158 }
10159 
10160 void
10161 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10162     struct sctp_nets *net)
10163 {
10164 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10165 	struct mbuf *m_shutdown_comp;
10166 	struct sctp_shutdown_complete_msg *comp_cp;
10167 
10168 	m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_complete_msg), 0, M_DONTWAIT, 1, MT_HEADER);
10169 	if (m_shutdown_comp == NULL) {
10170 		/* no mbuf's */
10171 		return;
10172 	}
10173 	comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
10174 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10175 	comp_cp->shut_cmp.ch.chunk_flags = 0;
10176 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10177 	comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
10178 	comp_cp->sh.dest_port = stcb->rport;
10179 	comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
10180 	comp_cp->sh.checksum = 0;
10181 
10182 	SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_msg);
10183 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10184 	    (struct sockaddr *)&net->ro._l_addr,
10185 	    m_shutdown_comp, 0, NULL, 1, 0, NULL, 0, net->port, SCTP_SO_NOT_LOCKED, NULL);
10186 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10187 	return;
10188 }
10189 
10190 void
10191 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
10192     uint32_t vrf_id, uint16_t port)
10193 {
10194 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10195 	struct mbuf *o_pak;
10196 	struct mbuf *mout;
10197 	struct ip *iph, *iph_out;
10198 	struct udphdr *udp = NULL;
10199 
10200 #ifdef INET6
10201 	struct ip6_hdr *ip6, *ip6_out;
10202 
10203 #endif
10204 	int offset_out, len, mlen;
10205 	struct sctp_shutdown_complete_msg *comp_cp;
10206 
10207 	iph = mtod(m, struct ip *);
10208 	switch (iph->ip_v) {
10209 	case IPVERSION:
10210 		len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10211 		break;
10212 #ifdef INET6
10213 	case IPV6_VERSION >> 4:
10214 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10215 		break;
10216 #endif
10217 	default:
10218 		return;
10219 	}
10220 	if (port) {
10221 		len += sizeof(struct udphdr);
10222 	}
10223 	mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
10224 	if (mout == NULL) {
10225 		return;
10226 	}
10227 	SCTP_BUF_LEN(mout) = len;
10228 	SCTP_BUF_NEXT(mout) = NULL;
10229 	iph_out = NULL;
10230 #ifdef INET6
10231 	ip6_out = NULL;
10232 #endif
10233 	offset_out = 0;
10234 
10235 	switch (iph->ip_v) {
10236 	case IPVERSION:
10237 		iph_out = mtod(mout, struct ip *);
10238 
10239 		/* Fill in the IP header for the ABORT */
10240 		iph_out->ip_v = IPVERSION;
10241 		iph_out->ip_hl = (sizeof(struct ip) / 4);
10242 		iph_out->ip_tos = (u_char)0;
10243 		iph_out->ip_id = 0;
10244 		iph_out->ip_off = 0;
10245 		iph_out->ip_ttl = MAXTTL;
10246 		if (port) {
10247 			iph_out->ip_p = IPPROTO_UDP;
10248 		} else {
10249 			iph_out->ip_p = IPPROTO_SCTP;
10250 		}
10251 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10252 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10253 
10254 		/* let IP layer calculate this */
10255 		iph_out->ip_sum = 0;
10256 		offset_out += sizeof(*iph_out);
10257 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10258 		    (caddr_t)iph_out + offset_out);
10259 		break;
10260 #ifdef INET6
10261 	case IPV6_VERSION >> 4:
10262 		ip6 = (struct ip6_hdr *)iph;
10263 		ip6_out = mtod(mout, struct ip6_hdr *);
10264 
10265 		/* Fill in the IPv6 header for the ABORT */
10266 		ip6_out->ip6_flow = ip6->ip6_flow;
10267 		ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
10268 		if (port) {
10269 			ip6_out->ip6_nxt = IPPROTO_UDP;
10270 		} else {
10271 			ip6_out->ip6_nxt = IPPROTO_SCTP;
10272 		}
10273 		ip6_out->ip6_src = ip6->ip6_dst;
10274 		ip6_out->ip6_dst = ip6->ip6_src;
10275 		/*
10276 		 * ?? The old code had both the iph len + payload, I think
10277 		 * this is wrong and would never have worked
10278 		 */
10279 		ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10280 		offset_out += sizeof(*ip6_out);
10281 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10282 		    (caddr_t)ip6_out + offset_out);
10283 		break;
10284 #endif				/* INET6 */
10285 	default:
10286 		/* Currently not supported. */
10287 		return;
10288 	}
10289 	if (port) {
10290 		udp = (struct udphdr *)comp_cp;
10291 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10292 		udp->uh_dport = port;
10293 		udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
10294 		udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
10295 		offset_out += sizeof(struct udphdr);
10296 		comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
10297 	}
10298 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10299 		/* no mbuf's */
10300 		sctp_m_freem(mout);
10301 		return;
10302 	}
10303 	/* Now copy in and fill in the ABORT tags etc. */
10304 	comp_cp->sh.src_port = sh->dest_port;
10305 	comp_cp->sh.dest_port = sh->src_port;
10306 	comp_cp->sh.checksum = 0;
10307 	comp_cp->sh.v_tag = sh->v_tag;
10308 	comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
10309 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10310 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10311 
10312 	/* add checksum */
10313 	comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
10314 	if (iph_out != NULL) {
10315 		sctp_route_t ro;
10316 		int ret;
10317 		struct sctp_tcb *stcb = NULL;
10318 
10319 		mlen = SCTP_BUF_LEN(mout);
10320 		bzero(&ro, sizeof ro);
10321 		/* set IPv4 length */
10322 		iph_out->ip_len = mlen;
10323 #ifdef  SCTP_PACKET_LOGGING
10324 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10325 			sctp_packet_log(mout, mlen);
10326 #endif
10327 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10328 		if (port) {
10329 			SCTP_ENABLE_UDP_CSUM(o_pak);
10330 		}
10331 		/* out it goes */
10332 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
10333 
10334 		/* Free the route if we got one back */
10335 		if (ro.ro_rt)
10336 			RTFREE(ro.ro_rt);
10337 	}
10338 #ifdef INET6
10339 	if (ip6_out != NULL) {
10340 		struct route_in6 ro;
10341 		int ret;
10342 		struct sctp_tcb *stcb = NULL;
10343 		struct ifnet *ifp = NULL;
10344 
10345 		bzero(&ro, sizeof(ro));
10346 		mlen = SCTP_BUF_LEN(mout);
10347 #ifdef  SCTP_PACKET_LOGGING
10348 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10349 			sctp_packet_log(mout, mlen);
10350 #endif
10351 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10352 		if (port) {
10353 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr),
10354 			    sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr))) == 0) {
10355 				udp->uh_sum = 0xffff;
10356 			}
10357 		}
10358 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
10359 
10360 		/* Free the route if we got one back */
10361 		if (ro.ro_rt)
10362 			RTFREE(ro.ro_rt);
10363 	}
10364 #endif
10365 	SCTP_STAT_INCR(sctps_sendpackets);
10366 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10367 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10368 	return;
10369 
10370 }
10371 
10372 static struct sctp_nets *
10373 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
10374 {
10375 	struct sctp_nets *net, *hnet;
10376 	int ms_goneby, highest_ms, state_overide = 0;
10377 
10378 	(void)SCTP_GETTIME_TIMEVAL(now);
10379 	highest_ms = 0;
10380 	hnet = NULL;
10381 	SCTP_TCB_LOCK_ASSERT(stcb);
10382 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
10383 		if (
10384 		    ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
10385 		    (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
10386 		    ) {
10387 			/*
10388 			 * Skip this guy from consideration if HB is off AND
10389 			 * its confirmed
10390 			 */
10391 			continue;
10392 		}
10393 		if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
10394 			/* skip this dest net from consideration */
10395 			continue;
10396 		}
10397 		if (net->last_sent_time.tv_sec) {
10398 			/* Sent to so we subtract */
10399 			ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
10400 		} else
10401 			/* Never been sent to */
10402 			ms_goneby = 0x7fffffff;
10403 		/*-
10404 		 * When the address state is unconfirmed but still
10405 		 * considered reachable, we HB at a higher rate. Once it
10406 		 * goes confirmed OR reaches the "unreachable" state, thenw
10407 		 * we cut it back to HB at a more normal pace.
10408 		 */
10409 		if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
10410 			state_overide = 1;
10411 		} else {
10412 			state_overide = 0;
10413 		}
10414 
10415 		if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
10416 		    (ms_goneby > highest_ms)) {
10417 			highest_ms = ms_goneby;
10418 			hnet = net;
10419 		}
10420 	}
10421 	if (hnet &&
10422 	    ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
10423 		state_overide = 1;
10424 	} else {
10425 		state_overide = 0;
10426 	}
10427 
10428 	if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
10429 		/*-
10430 		 * Found the one with longest delay bounds OR it is
10431 		 * unconfirmed and still not marked unreachable.
10432 		 */
10433 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
10434 #ifdef SCTP_DEBUG
10435 		if (hnet) {
10436 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
10437 			    (struct sockaddr *)&hnet->ro._l_addr);
10438 		} else {
10439 			SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
10440 		}
10441 #endif
10442 		/* update the timer now */
10443 		hnet->last_sent_time = *now;
10444 		return (hnet);
10445 	}
10446 	/* Nothing to HB */
10447 	return (NULL);
10448 }
10449 
10450 int
10451 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
10452 {
10453 	struct sctp_tmit_chunk *chk;
10454 	struct sctp_nets *net;
10455 	struct sctp_heartbeat_chunk *hb;
10456 	struct timeval now;
10457 	struct sockaddr_in *sin;
10458 	struct sockaddr_in6 *sin6;
10459 
10460 	SCTP_TCB_LOCK_ASSERT(stcb);
10461 	if (user_req == 0) {
10462 		net = sctp_select_hb_destination(stcb, &now);
10463 		if (net == NULL) {
10464 			/*-
10465 			 * All our busy none to send to, just start the
10466 			 * timer again.
10467 			 */
10468 			if (stcb->asoc.state == 0) {
10469 				return (0);
10470 			}
10471 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
10472 			    stcb->sctp_ep,
10473 			    stcb,
10474 			    net);
10475 			return (0);
10476 		}
10477 	} else {
10478 		net = u_net;
10479 		if (net == NULL) {
10480 			return (0);
10481 		}
10482 		(void)SCTP_GETTIME_TIMEVAL(&now);
10483 	}
10484 	sin = (struct sockaddr_in *)&net->ro._l_addr;
10485 	if (sin->sin_family != AF_INET) {
10486 		if (sin->sin_family != AF_INET6) {
10487 			/* huh */
10488 			return (0);
10489 		}
10490 	}
10491 	sctp_alloc_a_chunk(stcb, chk);
10492 	if (chk == NULL) {
10493 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
10494 		return (0);
10495 	}
10496 	chk->copy_by_ref = 0;
10497 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
10498 	chk->rec.chunk_id.can_take_data = 1;
10499 	chk->asoc = &stcb->asoc;
10500 	chk->send_size = sizeof(struct sctp_heartbeat_chunk);
10501 
10502 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10503 	if (chk->data == NULL) {
10504 		sctp_free_a_chunk(stcb, chk);
10505 		return (0);
10506 	}
10507 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10508 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10509 	chk->sent = SCTP_DATAGRAM_UNSENT;
10510 	chk->snd_count = 0;
10511 	chk->whoTo = net;
10512 	atomic_add_int(&chk->whoTo->ref_count, 1);
10513 	/* Now we have a mbuf that we can fill in with the details */
10514 	hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
10515 	memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
10516 	/* fill out chunk header */
10517 	hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
10518 	hb->ch.chunk_flags = 0;
10519 	hb->ch.chunk_length = htons(chk->send_size);
10520 	/* Fill out hb parameter */
10521 	hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
10522 	hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
10523 	hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
10524 	hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
10525 	/* Did our user request this one, put it in */
10526 	hb->heartbeat.hb_info.user_req = user_req;
10527 	hb->heartbeat.hb_info.addr_family = sin->sin_family;
10528 	hb->heartbeat.hb_info.addr_len = sin->sin_len;
10529 	if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
10530 		/*
10531 		 * we only take from the entropy pool if the address is not
10532 		 * confirmed.
10533 		 */
10534 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10535 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10536 	} else {
10537 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
10538 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
10539 	}
10540 	if (sin->sin_family == AF_INET) {
10541 		memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
10542 	} else if (sin->sin_family == AF_INET6) {
10543 		/* We leave the scope the way it is in our lookup table. */
10544 		sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
10545 		memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
10546 	} else {
10547 		/* huh compiler bug */
10548 		return (0);
10549 	}
10550 
10551 	/*
10552 	 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
10553 	 * PF-heartbeats.  Because of this, threshold management is done by
10554 	 * the t3 timer handler, and does not need to be done upon the send
10555 	 * of a PF-heartbeat. If CMT PF is on and the destination to which a
10556 	 * heartbeat is being sent is in PF state, do NOT do threshold
10557 	 * management.
10558 	 */
10559 	if ((SCTP_BASE_SYSCTL(sctp_cmt_pf) == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
10560 		/* ok we have a destination that needs a beat */
10561 		/* lets do the theshold management Qiaobing style */
10562 		if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
10563 		    stcb->asoc.max_send_times)) {
10564 			/*-
10565 			 * we have lost the association, in a way this is
10566 			 * quite bad since we really are one less time since
10567 			 * we really did not send yet. This is the down side
10568 			 * to the Q's style as defined in the RFC and not my
10569 			 * alternate style defined in the RFC.
10570 			 */
10571 			if (chk->data != NULL) {
10572 				sctp_m_freem(chk->data);
10573 				chk->data = NULL;
10574 			}
10575 			/*
10576 			 * Here we do NOT use the macro since the
10577 			 * association is now gone.
10578 			 */
10579 			if (chk->whoTo) {
10580 				sctp_free_remote_addr(chk->whoTo);
10581 				chk->whoTo = NULL;
10582 			}
10583 			sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
10584 			return (-1);
10585 		}
10586 	}
10587 	net->hb_responded = 0;
10588 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10589 	stcb->asoc.ctrl_queue_cnt++;
10590 	SCTP_STAT_INCR(sctps_sendheartbeat);
10591 	/*-
10592 	 * Call directly med level routine to put out the chunk. It will
10593 	 * always tumble out control chunks aka HB but it may even tumble
10594 	 * out data too.
10595 	 */
10596 	return (1);
10597 }
10598 
10599 void
10600 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
10601     uint32_t high_tsn)
10602 {
10603 	struct sctp_association *asoc;
10604 	struct sctp_ecne_chunk *ecne;
10605 	struct sctp_tmit_chunk *chk;
10606 
10607 	asoc = &stcb->asoc;
10608 	SCTP_TCB_LOCK_ASSERT(stcb);
10609 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10610 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
10611 			/* found a previous ECN_ECHO update it if needed */
10612 			ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10613 			ecne->tsn = htonl(high_tsn);
10614 			return;
10615 		}
10616 	}
10617 	/* nope could not find one to update so we must build one */
10618 	sctp_alloc_a_chunk(stcb, chk);
10619 	if (chk == NULL) {
10620 		return;
10621 	}
10622 	chk->copy_by_ref = 0;
10623 	SCTP_STAT_INCR(sctps_sendecne);
10624 	chk->rec.chunk_id.id = SCTP_ECN_ECHO;
10625 	chk->rec.chunk_id.can_take_data = 0;
10626 	chk->asoc = &stcb->asoc;
10627 	chk->send_size = sizeof(struct sctp_ecne_chunk);
10628 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10629 	if (chk->data == NULL) {
10630 		sctp_free_a_chunk(stcb, chk);
10631 		return;
10632 	}
10633 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10634 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10635 	chk->sent = SCTP_DATAGRAM_UNSENT;
10636 	chk->snd_count = 0;
10637 	chk->whoTo = net;
10638 	atomic_add_int(&chk->whoTo->ref_count, 1);
10639 	stcb->asoc.ecn_echo_cnt_onq++;
10640 	ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10641 	ecne->ch.chunk_type = SCTP_ECN_ECHO;
10642 	ecne->ch.chunk_flags = 0;
10643 	ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
10644 	ecne->tsn = htonl(high_tsn);
10645 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10646 	asoc->ctrl_queue_cnt++;
10647 }
10648 
10649 void
10650 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
10651     struct mbuf *m, int iphlen, int bad_crc)
10652 {
10653 	struct sctp_association *asoc;
10654 	struct sctp_pktdrop_chunk *drp;
10655 	struct sctp_tmit_chunk *chk;
10656 	uint8_t *datap;
10657 	int len;
10658 	int was_trunc = 0;
10659 	struct ip *iph;
10660 
10661 #ifdef INET6
10662 	struct ip6_hdr *ip6h;
10663 
10664 #endif
10665 	int fullsz = 0, extra = 0;
10666 	long spc;
10667 	int offset;
10668 	struct sctp_chunkhdr *ch, chunk_buf;
10669 	unsigned int chk_length;
10670 
10671 	if (!stcb) {
10672 		return;
10673 	}
10674 	asoc = &stcb->asoc;
10675 	SCTP_TCB_LOCK_ASSERT(stcb);
10676 	if (asoc->peer_supports_pktdrop == 0) {
10677 		/*-
10678 		 * peer must declare support before I send one.
10679 		 */
10680 		return;
10681 	}
10682 	if (stcb->sctp_socket == NULL) {
10683 		return;
10684 	}
10685 	sctp_alloc_a_chunk(stcb, chk);
10686 	if (chk == NULL) {
10687 		return;
10688 	}
10689 	chk->copy_by_ref = 0;
10690 	iph = mtod(m, struct ip *);
10691 	if (iph == NULL) {
10692 		sctp_free_a_chunk(stcb, chk);
10693 		return;
10694 	}
10695 	switch (iph->ip_v) {
10696 	case IPVERSION:
10697 		/* IPv4 */
10698 		len = chk->send_size = iph->ip_len;
10699 		break;
10700 #ifdef INET6
10701 	case IPV6_VERSION >> 4:
10702 		/* IPv6 */
10703 		ip6h = mtod(m, struct ip6_hdr *);
10704 		len = chk->send_size = htons(ip6h->ip6_plen);
10705 		break;
10706 #endif
10707 	default:
10708 		return;
10709 	}
10710 	/* Validate that we do not have an ABORT in here. */
10711 	offset = iphlen + sizeof(struct sctphdr);
10712 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10713 	    sizeof(*ch), (uint8_t *) & chunk_buf);
10714 	while (ch != NULL) {
10715 		chk_length = ntohs(ch->chunk_length);
10716 		if (chk_length < sizeof(*ch)) {
10717 			/* break to abort land */
10718 			break;
10719 		}
10720 		switch (ch->chunk_type) {
10721 		case SCTP_PACKET_DROPPED:
10722 		case SCTP_ABORT_ASSOCIATION:
10723 			/*-
10724 			 * we don't respond with an PKT-DROP to an ABORT
10725 			 * or PKT-DROP
10726 			 */
10727 			sctp_free_a_chunk(stcb, chk);
10728 			return;
10729 		default:
10730 			break;
10731 		}
10732 		offset += SCTP_SIZE32(chk_length);
10733 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10734 		    sizeof(*ch), (uint8_t *) & chunk_buf);
10735 	}
10736 
10737 	if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
10738 	    min(stcb->asoc.smallest_mtu, MCLBYTES)) {
10739 		/*
10740 		 * only send 1 mtu worth, trim off the excess on the end.
10741 		 */
10742 		fullsz = len - extra;
10743 		len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
10744 		was_trunc = 1;
10745 	}
10746 	chk->asoc = &stcb->asoc;
10747 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10748 	if (chk->data == NULL) {
10749 jump_out:
10750 		sctp_free_a_chunk(stcb, chk);
10751 		return;
10752 	}
10753 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10754 	drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
10755 	if (drp == NULL) {
10756 		sctp_m_freem(chk->data);
10757 		chk->data = NULL;
10758 		goto jump_out;
10759 	}
10760 	chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
10761 	    sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
10762 	chk->book_size_scale = 0;
10763 	if (was_trunc) {
10764 		drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
10765 		drp->trunc_len = htons(fullsz);
10766 		/*
10767 		 * Len is already adjusted to size minus overhead above take
10768 		 * out the pkt_drop chunk itself from it.
10769 		 */
10770 		chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
10771 		len = chk->send_size;
10772 	} else {
10773 		/* no truncation needed */
10774 		drp->ch.chunk_flags = 0;
10775 		drp->trunc_len = htons(0);
10776 	}
10777 	if (bad_crc) {
10778 		drp->ch.chunk_flags |= SCTP_BADCRC;
10779 	}
10780 	chk->send_size += sizeof(struct sctp_pktdrop_chunk);
10781 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10782 	chk->sent = SCTP_DATAGRAM_UNSENT;
10783 	chk->snd_count = 0;
10784 	if (net) {
10785 		/* we should hit here */
10786 		chk->whoTo = net;
10787 	} else {
10788 		chk->whoTo = asoc->primary_destination;
10789 	}
10790 	atomic_add_int(&chk->whoTo->ref_count, 1);
10791 	chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
10792 	chk->rec.chunk_id.can_take_data = 1;
10793 	drp->ch.chunk_type = SCTP_PACKET_DROPPED;
10794 	drp->ch.chunk_length = htons(chk->send_size);
10795 	spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
10796 	if (spc < 0) {
10797 		spc = 0;
10798 	}
10799 	drp->bottle_bw = htonl(spc);
10800 	if (asoc->my_rwnd) {
10801 		drp->current_onq = htonl(asoc->size_on_reasm_queue +
10802 		    asoc->size_on_all_streams +
10803 		    asoc->my_rwnd_control_len +
10804 		    stcb->sctp_socket->so_rcv.sb_cc);
10805 	} else {
10806 		/*-
10807 		 * If my rwnd is 0, possibly from mbuf depletion as well as
10808 		 * space used, tell the peer there is NO space aka onq == bw
10809 		 */
10810 		drp->current_onq = htonl(spc);
10811 	}
10812 	drp->reserved = 0;
10813 	datap = drp->data;
10814 	m_copydata(m, iphlen, len, (caddr_t)datap);
10815 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10816 	asoc->ctrl_queue_cnt++;
10817 }
10818 
10819 void
10820 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
10821 {
10822 	struct sctp_association *asoc;
10823 	struct sctp_cwr_chunk *cwr;
10824 	struct sctp_tmit_chunk *chk;
10825 
10826 	asoc = &stcb->asoc;
10827 	SCTP_TCB_LOCK_ASSERT(stcb);
10828 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10829 		if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
10830 			/* found a previous ECN_CWR update it if needed */
10831 			cwr = mtod(chk->data, struct sctp_cwr_chunk *);
10832 			if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
10833 			    MAX_TSN)) {
10834 				cwr->tsn = htonl(high_tsn);
10835 			}
10836 			return;
10837 		}
10838 	}
10839 	/* nope could not find one to update so we must build one */
10840 	sctp_alloc_a_chunk(stcb, chk);
10841 	if (chk == NULL) {
10842 		return;
10843 	}
10844 	chk->copy_by_ref = 0;
10845 	chk->rec.chunk_id.id = SCTP_ECN_CWR;
10846 	chk->rec.chunk_id.can_take_data = 1;
10847 	chk->asoc = &stcb->asoc;
10848 	chk->send_size = sizeof(struct sctp_cwr_chunk);
10849 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10850 	if (chk->data == NULL) {
10851 		sctp_free_a_chunk(stcb, chk);
10852 		return;
10853 	}
10854 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10855 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10856 	chk->sent = SCTP_DATAGRAM_UNSENT;
10857 	chk->snd_count = 0;
10858 	chk->whoTo = net;
10859 	atomic_add_int(&chk->whoTo->ref_count, 1);
10860 	cwr = mtod(chk->data, struct sctp_cwr_chunk *);
10861 	cwr->ch.chunk_type = SCTP_ECN_CWR;
10862 	cwr->ch.chunk_flags = 0;
10863 	cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
10864 	cwr->tsn = htonl(high_tsn);
10865 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10866 	asoc->ctrl_queue_cnt++;
10867 }
10868 
10869 void
10870 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
10871     int number_entries, uint16_t * list,
10872     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
10873 {
10874 	int len, old_len, i;
10875 	struct sctp_stream_reset_out_request *req_out;
10876 	struct sctp_chunkhdr *ch;
10877 
10878 	ch = mtod(chk->data, struct sctp_chunkhdr *);
10879 
10880 
10881 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10882 
10883 	/* get to new offset for the param. */
10884 	req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
10885 	/* now how long will this param be? */
10886 	len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
10887 	req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
10888 	req_out->ph.param_length = htons(len);
10889 	req_out->request_seq = htonl(seq);
10890 	req_out->response_seq = htonl(resp_seq);
10891 	req_out->send_reset_at_tsn = htonl(last_sent);
10892 	if (number_entries) {
10893 		for (i = 0; i < number_entries; i++) {
10894 			req_out->list_of_streams[i] = htons(list[i]);
10895 		}
10896 	}
10897 	if (SCTP_SIZE32(len) > len) {
10898 		/*-
10899 		 * Need to worry about the pad we may end up adding to the
10900 		 * end. This is easy since the struct is either aligned to 4
10901 		 * bytes or 2 bytes off.
10902 		 */
10903 		req_out->list_of_streams[number_entries] = 0;
10904 	}
10905 	/* now fix the chunk length */
10906 	ch->chunk_length = htons(len + old_len);
10907 	chk->book_size = len + old_len;
10908 	chk->book_size_scale = 0;
10909 	chk->send_size = SCTP_SIZE32(chk->book_size);
10910 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10911 	return;
10912 }
10913 
10914 
10915 void
10916 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
10917     int number_entries, uint16_t * list,
10918     uint32_t seq)
10919 {
10920 	int len, old_len, i;
10921 	struct sctp_stream_reset_in_request *req_in;
10922 	struct sctp_chunkhdr *ch;
10923 
10924 	ch = mtod(chk->data, struct sctp_chunkhdr *);
10925 
10926 
10927 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10928 
10929 	/* get to new offset for the param. */
10930 	req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
10931 	/* now how long will this param be? */
10932 	len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
10933 	req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
10934 	req_in->ph.param_length = htons(len);
10935 	req_in->request_seq = htonl(seq);
10936 	if (number_entries) {
10937 		for (i = 0; i < number_entries; i++) {
10938 			req_in->list_of_streams[i] = htons(list[i]);
10939 		}
10940 	}
10941 	if (SCTP_SIZE32(len) > len) {
10942 		/*-
10943 		 * Need to worry about the pad we may end up adding to the
10944 		 * end. This is easy since the struct is either aligned to 4
10945 		 * bytes or 2 bytes off.
10946 		 */
10947 		req_in->list_of_streams[number_entries] = 0;
10948 	}
10949 	/* now fix the chunk length */
10950 	ch->chunk_length = htons(len + old_len);
10951 	chk->book_size = len + old_len;
10952 	chk->book_size_scale = 0;
10953 	chk->send_size = SCTP_SIZE32(chk->book_size);
10954 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10955 	return;
10956 }
10957 
10958 
10959 void
10960 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
10961     uint32_t seq)
10962 {
10963 	int len, old_len;
10964 	struct sctp_stream_reset_tsn_request *req_tsn;
10965 	struct sctp_chunkhdr *ch;
10966 
10967 	ch = mtod(chk->data, struct sctp_chunkhdr *);
10968 
10969 
10970 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
10971 
10972 	/* get to new offset for the param. */
10973 	req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
10974 	/* now how long will this param be? */
10975 	len = sizeof(struct sctp_stream_reset_tsn_request);
10976 	req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
10977 	req_tsn->ph.param_length = htons(len);
10978 	req_tsn->request_seq = htonl(seq);
10979 
10980 	/* now fix the chunk length */
10981 	ch->chunk_length = htons(len + old_len);
10982 	chk->send_size = len + old_len;
10983 	chk->book_size = SCTP_SIZE32(chk->send_size);
10984 	chk->book_size_scale = 0;
10985 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
10986 	return;
10987 }
10988 
10989 void
10990 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
10991     uint32_t resp_seq, uint32_t result)
10992 {
10993 	int len, old_len;
10994 	struct sctp_stream_reset_response *resp;
10995 	struct sctp_chunkhdr *ch;
10996 
10997 	ch = mtod(chk->data, struct sctp_chunkhdr *);
10998 
10999 
11000 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11001 
11002 	/* get to new offset for the param. */
11003 	resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11004 	/* now how long will this param be? */
11005 	len = sizeof(struct sctp_stream_reset_response);
11006 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11007 	resp->ph.param_length = htons(len);
11008 	resp->response_seq = htonl(resp_seq);
11009 	resp->result = ntohl(result);
11010 
11011 	/* now fix the chunk length */
11012 	ch->chunk_length = htons(len + old_len);
11013 	chk->book_size = len + old_len;
11014 	chk->book_size_scale = 0;
11015 	chk->send_size = SCTP_SIZE32(chk->book_size);
11016 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11017 	return;
11018 
11019 }
11020 
11021 
11022 void
11023 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11024     uint32_t resp_seq, uint32_t result,
11025     uint32_t send_una, uint32_t recv_next)
11026 {
11027 	int len, old_len;
11028 	struct sctp_stream_reset_response_tsn *resp;
11029 	struct sctp_chunkhdr *ch;
11030 
11031 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11032 
11033 
11034 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11035 
11036 	/* get to new offset for the param. */
11037 	resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11038 	/* now how long will this param be? */
11039 	len = sizeof(struct sctp_stream_reset_response_tsn);
11040 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11041 	resp->ph.param_length = htons(len);
11042 	resp->response_seq = htonl(resp_seq);
11043 	resp->result = htonl(result);
11044 	resp->senders_next_tsn = htonl(send_una);
11045 	resp->receivers_next_tsn = htonl(recv_next);
11046 
11047 	/* now fix the chunk length */
11048 	ch->chunk_length = htons(len + old_len);
11049 	chk->book_size = len + old_len;
11050 	chk->send_size = SCTP_SIZE32(chk->book_size);
11051 	chk->book_size_scale = 0;
11052 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11053 	return;
11054 }
11055 
11056 
11057 int
11058 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11059     int number_entries, uint16_t * list,
11060     uint8_t send_out_req, uint32_t resp_seq,
11061     uint8_t send_in_req,
11062     uint8_t send_tsn_req)
11063 {
11064 
11065 	struct sctp_association *asoc;
11066 	struct sctp_tmit_chunk *chk;
11067 	struct sctp_chunkhdr *ch;
11068 	uint32_t seq;
11069 
11070 	asoc = &stcb->asoc;
11071 	if (asoc->stream_reset_outstanding) {
11072 		/*-
11073 		 * Already one pending, must get ACK back to clear the flag.
11074 		 */
11075 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11076 		return (EBUSY);
11077 	}
11078 	if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0)) {
11079 		/* nothing to do */
11080 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11081 		return (EINVAL);
11082 	}
11083 	if (send_tsn_req && (send_out_req || send_in_req)) {
11084 		/* error, can't do that */
11085 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11086 		return (EINVAL);
11087 	}
11088 	sctp_alloc_a_chunk(stcb, chk);
11089 	if (chk == NULL) {
11090 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11091 		return (ENOMEM);
11092 	}
11093 	chk->copy_by_ref = 0;
11094 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11095 	chk->rec.chunk_id.can_take_data = 0;
11096 	chk->asoc = &stcb->asoc;
11097 	chk->book_size = sizeof(struct sctp_chunkhdr);
11098 	chk->send_size = SCTP_SIZE32(chk->book_size);
11099 	chk->book_size_scale = 0;
11100 
11101 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11102 	if (chk->data == NULL) {
11103 		sctp_free_a_chunk(stcb, chk);
11104 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11105 		return (ENOMEM);
11106 	}
11107 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11108 
11109 	/* setup chunk parameters */
11110 	chk->sent = SCTP_DATAGRAM_UNSENT;
11111 	chk->snd_count = 0;
11112 	chk->whoTo = asoc->primary_destination;
11113 	atomic_add_int(&chk->whoTo->ref_count, 1);
11114 
11115 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11116 	ch->chunk_type = SCTP_STREAM_RESET;
11117 	ch->chunk_flags = 0;
11118 	ch->chunk_length = htons(chk->book_size);
11119 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11120 
11121 	seq = stcb->asoc.str_reset_seq_out;
11122 	if (send_out_req) {
11123 		sctp_add_stream_reset_out(chk, number_entries, list,
11124 		    seq, resp_seq, (stcb->asoc.sending_seq - 1));
11125 		asoc->stream_reset_out_is_outstanding = 1;
11126 		seq++;
11127 		asoc->stream_reset_outstanding++;
11128 	}
11129 	if (send_in_req) {
11130 		sctp_add_stream_reset_in(chk, number_entries, list, seq);
11131 		asoc->stream_reset_outstanding++;
11132 	}
11133 	if (send_tsn_req) {
11134 		sctp_add_stream_reset_tsn(chk, seq);
11135 		asoc->stream_reset_outstanding++;
11136 	}
11137 	asoc->str_reset = chk;
11138 
11139 	/* insert the chunk for sending */
11140 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11141 	    chk,
11142 	    sctp_next);
11143 	asoc->ctrl_queue_cnt++;
11144 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11145 	return (0);
11146 }
11147 
11148 void
11149 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11150     struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11151 {
11152 	/*-
11153 	 * Formulate the abort message, and send it back down.
11154 	 */
11155 	struct mbuf *o_pak;
11156 	struct mbuf *mout;
11157 	struct sctp_abort_msg *abm;
11158 	struct ip *iph, *iph_out;
11159 	struct udphdr *udp;
11160 
11161 #ifdef INET6
11162 	struct ip6_hdr *ip6, *ip6_out;
11163 
11164 #endif
11165 	int iphlen_out, len;
11166 
11167 	/* don't respond to ABORT with ABORT */
11168 	if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11169 		if (err_cause)
11170 			sctp_m_freem(err_cause);
11171 		return;
11172 	}
11173 	iph = mtod(m, struct ip *);
11174 	switch (iph->ip_v) {
11175 	case IPVERSION:
11176 		len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11177 		break;
11178 #ifdef INET6
11179 	case IPV6_VERSION >> 4:
11180 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11181 		break;
11182 #endif
11183 	default:
11184 		return;
11185 	}
11186 	if (port) {
11187 		len += sizeof(struct udphdr);
11188 	}
11189 	mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
11190 	if (mout == NULL) {
11191 		if (err_cause)
11192 			sctp_m_freem(err_cause);
11193 		return;
11194 	}
11195 	SCTP_BUF_LEN(mout) = len;
11196 	SCTP_BUF_NEXT(mout) = err_cause;
11197 	iph_out = NULL;
11198 #ifdef INET6
11199 	ip6_out = NULL;
11200 #endif
11201 	switch (iph->ip_v) {
11202 	case IPVERSION:
11203 		iph_out = mtod(mout, struct ip *);
11204 
11205 		/* Fill in the IP header for the ABORT */
11206 		iph_out->ip_v = IPVERSION;
11207 		iph_out->ip_hl = (sizeof(struct ip) / 4);
11208 		iph_out->ip_tos = (u_char)0;
11209 		iph_out->ip_id = 0;
11210 		iph_out->ip_off = 0;
11211 		iph_out->ip_ttl = MAXTTL;
11212 		if (port) {
11213 			iph_out->ip_p = IPPROTO_UDP;
11214 		} else {
11215 			iph_out->ip_p = IPPROTO_SCTP;
11216 		}
11217 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11218 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11219 		/* let IP layer calculate this */
11220 		iph_out->ip_sum = 0;
11221 
11222 		iphlen_out = sizeof(*iph_out);
11223 		abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
11224 		break;
11225 #ifdef INET6
11226 	case IPV6_VERSION >> 4:
11227 		ip6 = (struct ip6_hdr *)iph;
11228 		ip6_out = mtod(mout, struct ip6_hdr *);
11229 
11230 		/* Fill in the IP6 header for the ABORT */
11231 		ip6_out->ip6_flow = ip6->ip6_flow;
11232 		ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
11233 		if (port) {
11234 			ip6_out->ip6_nxt = IPPROTO_UDP;
11235 		} else {
11236 			ip6_out->ip6_nxt = IPPROTO_SCTP;
11237 		}
11238 		ip6_out->ip6_src = ip6->ip6_dst;
11239 		ip6_out->ip6_dst = ip6->ip6_src;
11240 
11241 		iphlen_out = sizeof(*ip6_out);
11242 		abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
11243 		break;
11244 #endif				/* INET6 */
11245 	default:
11246 		/* Currently not supported */
11247 		if (err_cause)
11248 			sctp_m_freem(err_cause);
11249 		sctp_m_freem(mout);
11250 		return;
11251 	}
11252 
11253 	udp = (struct udphdr *)abm;
11254 	if (port) {
11255 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11256 		udp->uh_dport = port;
11257 		/* set udp->uh_ulen later */
11258 		udp->uh_sum = 0;
11259 		iphlen_out += sizeof(struct udphdr);
11260 		abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
11261 	}
11262 	abm->sh.src_port = sh->dest_port;
11263 	abm->sh.dest_port = sh->src_port;
11264 	abm->sh.checksum = 0;
11265 	if (vtag == 0) {
11266 		abm->sh.v_tag = sh->v_tag;
11267 		abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
11268 	} else {
11269 		abm->sh.v_tag = htonl(vtag);
11270 		abm->msg.ch.chunk_flags = 0;
11271 	}
11272 	abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11273 
11274 	if (err_cause) {
11275 		struct mbuf *m_tmp = err_cause;
11276 		int err_len = 0;
11277 
11278 		/* get length of the err_cause chain */
11279 		while (m_tmp != NULL) {
11280 			err_len += SCTP_BUF_LEN(m_tmp);
11281 			m_tmp = SCTP_BUF_NEXT(m_tmp);
11282 		}
11283 		len = SCTP_BUF_LEN(mout) + err_len;
11284 		if (err_len % 4) {
11285 			/* need pad at end of chunk */
11286 			uint32_t cpthis = 0;
11287 			int padlen;
11288 
11289 			padlen = 4 - (len % 4);
11290 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11291 			len += padlen;
11292 		}
11293 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
11294 	} else {
11295 		len = SCTP_BUF_LEN(mout);
11296 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
11297 	}
11298 
11299 	/* add checksum */
11300 	abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
11301 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11302 		/* no mbuf's */
11303 		sctp_m_freem(mout);
11304 		return;
11305 	}
11306 	if (iph_out != NULL) {
11307 		sctp_route_t ro;
11308 		struct sctp_tcb *stcb = NULL;
11309 		int ret;
11310 
11311 		/* zap the stack pointer to the route */
11312 		bzero(&ro, sizeof ro);
11313 		if (port) {
11314 			udp->uh_ulen = htons(len - sizeof(struct ip));
11315 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11316 		}
11317 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
11318 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
11319 		/* set IPv4 length */
11320 		iph_out->ip_len = len;
11321 		/* out it goes */
11322 #ifdef  SCTP_PACKET_LOGGING
11323 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11324 			sctp_packet_log(mout, len);
11325 #endif
11326 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11327 		if (port) {
11328 			SCTP_ENABLE_UDP_CSUM(o_pak);
11329 		}
11330 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
11331 
11332 		/* Free the route if we got one back */
11333 		if (ro.ro_rt)
11334 			RTFREE(ro.ro_rt);
11335 	}
11336 #ifdef INET6
11337 	if (ip6_out != NULL) {
11338 		struct route_in6 ro;
11339 		int ret;
11340 		struct sctp_tcb *stcb = NULL;
11341 		struct ifnet *ifp = NULL;
11342 
11343 		/* zap the stack pointer to the route */
11344 		bzero(&ro, sizeof(ro));
11345 		if (port) {
11346 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11347 		}
11348 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
11349 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
11350 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
11351 #ifdef  SCTP_PACKET_LOGGING
11352 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11353 			sctp_packet_log(mout, len);
11354 #endif
11355 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11356 		if (port) {
11357 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11358 				udp->uh_sum = 0xffff;
11359 			}
11360 		}
11361 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11362 
11363 		/* Free the route if we got one back */
11364 		if (ro.ro_rt)
11365 			RTFREE(ro.ro_rt);
11366 	}
11367 #endif
11368 	SCTP_STAT_INCR(sctps_sendpackets);
11369 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11370 }
11371 
11372 void
11373 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
11374     uint32_t vrf_id, uint16_t port)
11375 {
11376 	struct mbuf *o_pak;
11377 	struct sctphdr *ihdr;
11378 	int retcode;
11379 	struct sctphdr *ohdr;
11380 	struct sctp_chunkhdr *ophdr;
11381 	struct ip *iph;
11382 	struct udphdr *udp = NULL;
11383 	struct mbuf *mout;
11384 
11385 #ifdef SCTP_DEBUG
11386 	struct sockaddr_in6 lsa6, fsa6;
11387 
11388 #endif
11389 	uint32_t val;
11390 	struct mbuf *at;
11391 	int len;
11392 
11393 	iph = mtod(m, struct ip *);
11394 	ihdr = (struct sctphdr *)((caddr_t)iph + iphlen);
11395 
11396 	SCTP_BUF_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), M_DONTWAIT);
11397 	if (scm == NULL) {
11398 		/* can't send because we can't add a mbuf */
11399 		return;
11400 	}
11401 	ohdr = mtod(scm, struct sctphdr *);
11402 	ohdr->src_port = ihdr->dest_port;
11403 	ohdr->dest_port = ihdr->src_port;
11404 	ohdr->v_tag = vtag;
11405 	ohdr->checksum = 0;
11406 	ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
11407 	ophdr->chunk_type = SCTP_OPERATION_ERROR;
11408 	ophdr->chunk_flags = 0;
11409 	len = 0;
11410 	at = scm;
11411 	while (at) {
11412 		len += SCTP_BUF_LEN(at);
11413 		at = SCTP_BUF_NEXT(at);
11414 	}
11415 	ophdr->chunk_length = htons(len - sizeof(struct sctphdr));
11416 	if (len % 4) {
11417 		/* need padding */
11418 		uint32_t cpthis = 0;
11419 		int padlen;
11420 
11421 		padlen = 4 - (len % 4);
11422 		m_copyback(scm, len, padlen, (caddr_t)&cpthis);
11423 		len += padlen;
11424 	}
11425 	val = sctp_calculate_sum(scm, NULL, 0);
11426 #ifdef INET6
11427 	if (port) {
11428 		mout = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
11429 	} else {
11430 		mout = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
11431 	}
11432 #else
11433 	if (port) {
11434 		mout = sctp_get_mbuf_for_msg(sizeof(struct ip) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
11435 	} else {
11436 		mout = sctp_get_mbuf_for_msg(sizeof(struct ip), 1, M_DONTWAIT, 1, MT_DATA);
11437 	}
11438 #endif
11439 	if (mout == NULL) {
11440 		sctp_m_freem(scm);
11441 		return;
11442 	}
11443 	SCTP_BUF_NEXT(mout) = scm;
11444 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11445 		sctp_m_freem(mout);
11446 		return;
11447 	}
11448 	ohdr->checksum = val;
11449 	switch (iph->ip_v) {
11450 	case IPVERSION:
11451 		{
11452 			/* V4 */
11453 			struct ip *out;
11454 			sctp_route_t ro;
11455 			struct sctp_tcb *stcb = NULL;
11456 
11457 			SCTP_BUF_LEN(mout) = sizeof(struct ip);
11458 			len += sizeof(struct ip);
11459 			if (port) {
11460 				SCTP_BUF_LEN(mout) += sizeof(struct udphdr);
11461 				len += sizeof(struct udphdr);
11462 			}
11463 			bzero(&ro, sizeof ro);
11464 			out = mtod(mout, struct ip *);
11465 			out->ip_v = iph->ip_v;
11466 			out->ip_hl = (sizeof(struct ip) / 4);
11467 			out->ip_tos = iph->ip_tos;
11468 			out->ip_id = iph->ip_id;
11469 			out->ip_off = 0;
11470 			out->ip_ttl = MAXTTL;
11471 			if (port) {
11472 				out->ip_p = IPPROTO_UDP;
11473 			} else {
11474 				out->ip_p = IPPROTO_SCTP;
11475 			}
11476 			out->ip_sum = 0;
11477 			out->ip_src = iph->ip_dst;
11478 			out->ip_dst = iph->ip_src;
11479 			out->ip_len = len;
11480 			if (port) {
11481 				udp = (struct udphdr *)(out + 1);
11482 				udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11483 				udp->uh_dport = port;
11484 				udp->uh_ulen = htons(len - sizeof(struct ip));
11485 				udp->uh_sum = in_pseudo(out->ip_src.s_addr, out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11486 			}
11487 #ifdef  SCTP_PACKET_LOGGING
11488 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11489 				sctp_packet_log(mout, len);
11490 #endif
11491 			SCTP_ATTACH_CHAIN(o_pak, mout, len);
11492 			if (port) {
11493 				SCTP_ENABLE_UDP_CSUM(o_pak);
11494 			}
11495 			SCTP_IP_OUTPUT(retcode, o_pak, &ro, stcb, vrf_id);
11496 
11497 			SCTP_STAT_INCR(sctps_sendpackets);
11498 			SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11499 			/* Free the route if we got one back */
11500 			if (ro.ro_rt)
11501 				RTFREE(ro.ro_rt);
11502 			break;
11503 		}
11504 #ifdef INET6
11505 	case IPV6_VERSION >> 4:
11506 		{
11507 			/* V6 */
11508 			struct route_in6 ro;
11509 			int ret;
11510 			struct sctp_tcb *stcb = NULL;
11511 			struct ifnet *ifp = NULL;
11512 			struct ip6_hdr *out6, *in6;
11513 
11514 			SCTP_BUF_LEN(mout) = sizeof(struct ip6_hdr);
11515 			len += sizeof(struct ip6_hdr);
11516 			bzero(&ro, sizeof ro);
11517 			if (port) {
11518 				SCTP_BUF_LEN(mout) += sizeof(struct udphdr);
11519 				len += sizeof(struct udphdr);
11520 			}
11521 			in6 = mtod(m, struct ip6_hdr *);
11522 			out6 = mtod(mout, struct ip6_hdr *);
11523 			out6->ip6_flow = in6->ip6_flow;
11524 			out6->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
11525 			if (port) {
11526 				out6->ip6_nxt = IPPROTO_UDP;
11527 			} else {
11528 				out6->ip6_nxt = IPPROTO_SCTP;
11529 			}
11530 			out6->ip6_src = in6->ip6_dst;
11531 			out6->ip6_dst = in6->ip6_src;
11532 			out6->ip6_plen = len - sizeof(struct ip6_hdr);
11533 			if (port) {
11534 				udp = (struct udphdr *)(out6 + 1);
11535 				udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11536 				udp->uh_dport = port;
11537 				udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11538 				udp->uh_sum = 0;
11539 			}
11540 #ifdef SCTP_DEBUG
11541 			bzero(&lsa6, sizeof(lsa6));
11542 			lsa6.sin6_len = sizeof(lsa6);
11543 			lsa6.sin6_family = AF_INET6;
11544 			lsa6.sin6_addr = out6->ip6_src;
11545 			bzero(&fsa6, sizeof(fsa6));
11546 			fsa6.sin6_len = sizeof(fsa6);
11547 			fsa6.sin6_family = AF_INET6;
11548 			fsa6.sin6_addr = out6->ip6_dst;
11549 #endif
11550 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_operr_to calling ipv6 output:\n");
11551 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "src: ");
11552 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&lsa6);
11553 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "dst ");
11554 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&fsa6);
11555 
11556 #ifdef  SCTP_PACKET_LOGGING
11557 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11558 				sctp_packet_log(mout, len);
11559 #endif
11560 			SCTP_ATTACH_CHAIN(o_pak, mout, len);
11561 			if (port) {
11562 				if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11563 					udp->uh_sum = 0xffff;
11564 				}
11565 			}
11566 			SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11567 
11568 			SCTP_STAT_INCR(sctps_sendpackets);
11569 			SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11570 			/* Free the route if we got one back */
11571 			if (ro.ro_rt)
11572 				RTFREE(ro.ro_rt);
11573 			break;
11574 		}
11575 #endif				/* INET6 */
11576 	default:
11577 		/* TSNH */
11578 		break;
11579 	}
11580 }
11581 
11582 static struct mbuf *
11583 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
11584     struct uio *uio,
11585     struct sctp_sndrcvinfo *srcv,
11586     int max_send_len,
11587     int user_marks_eor,
11588     int *error,
11589     uint32_t * sndout,
11590     struct mbuf **new_tail)
11591 {
11592 	struct mbuf *m;
11593 
11594 	m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
11595 	    (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
11596 	if (m == NULL) {
11597 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11598 		*error = ENOMEM;
11599 	} else {
11600 		*sndout = m_length(m, NULL);
11601 		*new_tail = m_last(m);
11602 	}
11603 	return (m);
11604 }
11605 
11606 static int
11607 sctp_copy_one(struct sctp_stream_queue_pending *sp,
11608     struct uio *uio,
11609     int resv_upfront)
11610 {
11611 	int left;
11612 
11613 	left = sp->length;
11614 	sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
11615 	    resv_upfront, 0);
11616 	if (sp->data == NULL) {
11617 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11618 		return (ENOMEM);
11619 	}
11620 	sp->tail_mbuf = m_last(sp->data);
11621 	return (0);
11622 }
11623 
11624 
11625 
11626 static struct sctp_stream_queue_pending *
11627 sctp_copy_it_in(struct sctp_tcb *stcb,
11628     struct sctp_association *asoc,
11629     struct sctp_sndrcvinfo *srcv,
11630     struct uio *uio,
11631     struct sctp_nets *net,
11632     int max_send_len,
11633     int user_marks_eor,
11634     int *error,
11635     int non_blocking)
11636 {
11637 	/*-
11638 	 * This routine must be very careful in its work. Protocol
11639 	 * processing is up and running so care must be taken to spl...()
11640 	 * when you need to do something that may effect the stcb/asoc. The
11641 	 * sb is locked however. When data is copied the protocol processing
11642 	 * should be enabled since this is a slower operation...
11643 	 */
11644 	struct sctp_stream_queue_pending *sp = NULL;
11645 	int resv_in_first;
11646 
11647 	*error = 0;
11648 	/* Now can we send this? */
11649 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
11650 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
11651 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
11652 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
11653 		/* got data while shutting down */
11654 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
11655 		*error = ECONNRESET;
11656 		goto out_now;
11657 	}
11658 	sctp_alloc_a_strmoq(stcb, sp);
11659 	if (sp == NULL) {
11660 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11661 		*error = ENOMEM;
11662 		goto out_now;
11663 	}
11664 	sp->act_flags = 0;
11665 	sp->sender_all_done = 0;
11666 	sp->sinfo_flags = srcv->sinfo_flags;
11667 	sp->timetolive = srcv->sinfo_timetolive;
11668 	sp->ppid = srcv->sinfo_ppid;
11669 	sp->context = srcv->sinfo_context;
11670 	sp->strseq = 0;
11671 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
11672 
11673 	sp->stream = srcv->sinfo_stream;
11674 	sp->length = min(uio->uio_resid, max_send_len);
11675 	if ((sp->length == (uint32_t) uio->uio_resid) &&
11676 	    ((user_marks_eor == 0) ||
11677 	    (srcv->sinfo_flags & SCTP_EOF) ||
11678 	    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
11679 		sp->msg_is_complete = 1;
11680 	} else {
11681 		sp->msg_is_complete = 0;
11682 	}
11683 	sp->sender_all_done = 0;
11684 	sp->some_taken = 0;
11685 	sp->put_last_out = 0;
11686 	resv_in_first = sizeof(struct sctp_data_chunk);
11687 	sp->data = sp->tail_mbuf = NULL;
11688 	*error = sctp_copy_one(sp, uio, resv_in_first);
11689 	if (*error) {
11690 		sctp_free_a_strmoq(stcb, sp);
11691 		sp = NULL;
11692 	} else {
11693 		if (sp->sinfo_flags & SCTP_ADDR_OVER) {
11694 			sp->net = net;
11695 			sp->addr_over = 1;
11696 		} else {
11697 			sp->net = asoc->primary_destination;
11698 			sp->addr_over = 0;
11699 		}
11700 		atomic_add_int(&sp->net->ref_count, 1);
11701 		sctp_set_prsctp_policy(stcb, sp);
11702 	}
11703 out_now:
11704 	return (sp);
11705 }
11706 
11707 
11708 int
11709 sctp_sosend(struct socket *so,
11710     struct sockaddr *addr,
11711     struct uio *uio,
11712     struct mbuf *top,
11713     struct mbuf *control,
11714     int flags,
11715     struct thread *p
11716 )
11717 {
11718 	struct sctp_inpcb *inp;
11719 	int error, use_rcvinfo = 0;
11720 	struct sctp_sndrcvinfo srcv;
11721 	struct sockaddr *addr_to_use;
11722 
11723 #ifdef INET6
11724 	struct sockaddr_in sin;
11725 
11726 #endif
11727 
11728 	inp = (struct sctp_inpcb *)so->so_pcb;
11729 	if (control) {
11730 		/* process cmsg snd/rcv info (maybe a assoc-id) */
11731 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
11732 		    sizeof(srcv))) {
11733 			/* got one */
11734 			use_rcvinfo = 1;
11735 		}
11736 	}
11737 	addr_to_use = addr;
11738 #ifdef INET6
11739 	if ((addr) && (addr->sa_family == AF_INET6)) {
11740 		struct sockaddr_in6 *sin6;
11741 
11742 		sin6 = (struct sockaddr_in6 *)addr;
11743 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
11744 			in6_sin6_2_sin(&sin, sin6);
11745 			addr_to_use = (struct sockaddr *)&sin;
11746 		}
11747 	}
11748 #endif
11749 	error = sctp_lower_sosend(so, addr_to_use, uio, top,
11750 	    control,
11751 	    flags,
11752 	    use_rcvinfo, &srcv
11753 	    ,p
11754 	    );
11755 	return (error);
11756 }
11757 
11758 
11759 int
11760 sctp_lower_sosend(struct socket *so,
11761     struct sockaddr *addr,
11762     struct uio *uio,
11763     struct mbuf *i_pak,
11764     struct mbuf *control,
11765     int flags,
11766     int use_rcvinfo,
11767     struct sctp_sndrcvinfo *srcv
11768     ,
11769     struct thread *p
11770 )
11771 {
11772 	unsigned int sndlen = 0, max_len;
11773 	int error, len;
11774 	struct mbuf *top = NULL;
11775 	int queue_only = 0, queue_only_for_init = 0;
11776 	int free_cnt_applied = 0;
11777 	int un_sent = 0;
11778 	int now_filled = 0;
11779 	unsigned int inqueue_bytes = 0;
11780 	struct sctp_block_entry be;
11781 	struct sctp_inpcb *inp;
11782 	struct sctp_tcb *stcb = NULL;
11783 	struct timeval now;
11784 	struct sctp_nets *net;
11785 	struct sctp_association *asoc;
11786 	struct sctp_inpcb *t_inp;
11787 	int user_marks_eor;
11788 	int create_lock_applied = 0;
11789 	int nagle_applies = 0;
11790 	int some_on_control = 0;
11791 	int got_all_of_the_send = 0;
11792 	int hold_tcblock = 0;
11793 	int non_blocking = 0;
11794 	int temp_flags = 0;
11795 	uint32_t local_add_more, local_soresv = 0;
11796 
11797 	error = 0;
11798 	net = NULL;
11799 	stcb = NULL;
11800 	asoc = NULL;
11801 
11802 	t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
11803 	if (inp == NULL) {
11804 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11805 		error = EFAULT;
11806 		if (i_pak) {
11807 			SCTP_RELEASE_PKT(i_pak);
11808 		}
11809 		return (error);
11810 	}
11811 	if ((uio == NULL) && (i_pak == NULL)) {
11812 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11813 		return (EINVAL);
11814 	}
11815 	user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
11816 	atomic_add_int(&inp->total_sends, 1);
11817 	if (uio) {
11818 		if (uio->uio_resid < 0) {
11819 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11820 			return (EINVAL);
11821 		}
11822 		sndlen = uio->uio_resid;
11823 	} else {
11824 		top = SCTP_HEADER_TO_CHAIN(i_pak);
11825 		sndlen = SCTP_HEADER_LEN(i_pak);
11826 	}
11827 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
11828 	    addr,
11829 	    sndlen);
11830 	/*-
11831          * Pre-screen address, if one is given the sin-len
11832          * must be set correctly!
11833          */
11834 	if (addr) {
11835 		if ((addr->sa_family == AF_INET) &&
11836 		    (addr->sa_len != sizeof(struct sockaddr_in))) {
11837 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11838 			error = EINVAL;
11839 			goto out_unlocked;
11840 		} else if ((addr->sa_family == AF_INET6) &&
11841 		    (addr->sa_len != sizeof(struct sockaddr_in6))) {
11842 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11843 			error = EINVAL;
11844 			goto out_unlocked;
11845 		}
11846 	}
11847 	hold_tcblock = 0;
11848 
11849 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
11850 	    (inp->sctp_socket->so_qlimit)) {
11851 		/* The listener can NOT send */
11852 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11853 		error = EFAULT;
11854 		goto out_unlocked;
11855 	}
11856 	if ((use_rcvinfo) && srcv) {
11857 		if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
11858 		    PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
11859 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11860 			error = EINVAL;
11861 			goto out_unlocked;
11862 		}
11863 		if (srcv->sinfo_flags)
11864 			SCTP_STAT_INCR(sctps_sends_with_flags);
11865 
11866 		if (srcv->sinfo_flags & SCTP_SENDALL) {
11867 			/* its a sendall */
11868 			error = sctp_sendall(inp, uio, top, srcv);
11869 			top = NULL;
11870 			goto out_unlocked;
11871 		}
11872 	}
11873 	/* now we must find the assoc */
11874 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
11875 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
11876 		SCTP_INP_RLOCK(inp);
11877 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
11878 		if (stcb == NULL) {
11879 			SCTP_INP_RUNLOCK(inp);
11880 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
11881 			error = ENOTCONN;
11882 			goto out_unlocked;
11883 		}
11884 		hold_tcblock = 0;
11885 		SCTP_INP_RUNLOCK(inp);
11886 		if (addr) {
11887 			/* Must locate the net structure if addr given */
11888 			net = sctp_findnet(stcb, addr);
11889 			if (net) {
11890 				/* validate port was 0 or correct */
11891 				struct sockaddr_in *sin;
11892 
11893 				sin = (struct sockaddr_in *)addr;
11894 				if ((sin->sin_port != 0) &&
11895 				    (sin->sin_port != stcb->rport)) {
11896 					net = NULL;
11897 				}
11898 			}
11899 			temp_flags |= SCTP_ADDR_OVER;
11900 		} else
11901 			net = stcb->asoc.primary_destination;
11902 		if (addr && (net == NULL)) {
11903 			/* Could not find address, was it legal */
11904 			if (addr->sa_family == AF_INET) {
11905 				struct sockaddr_in *sin;
11906 
11907 				sin = (struct sockaddr_in *)addr;
11908 				if (sin->sin_addr.s_addr == 0) {
11909 					if ((sin->sin_port == 0) ||
11910 					    (sin->sin_port == stcb->rport)) {
11911 						net = stcb->asoc.primary_destination;
11912 					}
11913 				}
11914 			} else {
11915 				struct sockaddr_in6 *sin6;
11916 
11917 				sin6 = (struct sockaddr_in6 *)addr;
11918 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
11919 					if ((sin6->sin6_port == 0) ||
11920 					    (sin6->sin6_port == stcb->rport)) {
11921 						net = stcb->asoc.primary_destination;
11922 					}
11923 				}
11924 			}
11925 		}
11926 		if (net == NULL) {
11927 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11928 			error = EINVAL;
11929 			goto out_unlocked;
11930 		}
11931 	} else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
11932 		stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
11933 		if (stcb) {
11934 			if (addr)
11935 				/*
11936 				 * Must locate the net structure if addr
11937 				 * given
11938 				 */
11939 				net = sctp_findnet(stcb, addr);
11940 			else
11941 				net = stcb->asoc.primary_destination;
11942 			if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
11943 			    ((net == NULL) || (addr == NULL))) {
11944 				struct sockaddr_in *sin;
11945 
11946 				if (addr == NULL) {
11947 					SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11948 					error = EINVAL;
11949 					goto out_unlocked;
11950 				}
11951 				sin = (struct sockaddr_in *)addr;
11952 				/* Validate port is 0 or correct */
11953 				if ((sin->sin_port != 0) &&
11954 				    (sin->sin_port != stcb->rport)) {
11955 					net = NULL;
11956 				}
11957 			}
11958 		}
11959 		hold_tcblock = 0;
11960 	} else if (addr) {
11961 		/*-
11962 		 * Since we did not use findep we must
11963 		 * increment it, and if we don't find a tcb
11964 		 * decrement it.
11965 		 */
11966 		SCTP_INP_WLOCK(inp);
11967 		SCTP_INP_INCR_REF(inp);
11968 		SCTP_INP_WUNLOCK(inp);
11969 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
11970 		if (stcb == NULL) {
11971 			SCTP_INP_WLOCK(inp);
11972 			SCTP_INP_DECR_REF(inp);
11973 			SCTP_INP_WUNLOCK(inp);
11974 		} else {
11975 			hold_tcblock = 1;
11976 		}
11977 	}
11978 	if ((stcb == NULL) && (addr)) {
11979 		/* Possible implicit send? */
11980 		SCTP_ASOC_CREATE_LOCK(inp);
11981 		create_lock_applied = 1;
11982 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
11983 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
11984 			/* Should I really unlock ? */
11985 			SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11986 			error = EFAULT;
11987 			goto out_unlocked;
11988 
11989 		}
11990 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
11991 		    (addr->sa_family == AF_INET6)) {
11992 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11993 			error = EINVAL;
11994 			goto out_unlocked;
11995 		}
11996 		SCTP_INP_WLOCK(inp);
11997 		SCTP_INP_INCR_REF(inp);
11998 		SCTP_INP_WUNLOCK(inp);
11999 		/* With the lock applied look again */
12000 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12001 		if (stcb == NULL) {
12002 			SCTP_INP_WLOCK(inp);
12003 			SCTP_INP_DECR_REF(inp);
12004 			SCTP_INP_WUNLOCK(inp);
12005 		} else {
12006 			hold_tcblock = 1;
12007 		}
12008 		if (t_inp != inp) {
12009 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12010 			error = ENOTCONN;
12011 			goto out_unlocked;
12012 		}
12013 	}
12014 	if (stcb == NULL) {
12015 		if (addr == NULL) {
12016 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12017 			error = ENOENT;
12018 			goto out_unlocked;
12019 		} else {
12020 			/*
12021 			 * UDP style, we must go ahead and start the INIT
12022 			 * process
12023 			 */
12024 			uint32_t vrf_id;
12025 
12026 			if ((use_rcvinfo) && (srcv) &&
12027 			    ((srcv->sinfo_flags & SCTP_ABORT) ||
12028 			    ((srcv->sinfo_flags & SCTP_EOF) &&
12029 			    (sndlen == 0)))) {
12030 				/*-
12031 				 * User asks to abort a non-existant assoc,
12032 				 * or EOF a non-existant assoc with no data
12033 				 */
12034 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12035 				error = ENOENT;
12036 				goto out_unlocked;
12037 			}
12038 			/* get an asoc/stcb struct */
12039 			vrf_id = inp->def_vrf_id;
12040 #ifdef INVARIANTS
12041 			if (create_lock_applied == 0) {
12042 				panic("Error, should hold create lock and I don't?");
12043 			}
12044 #endif
12045 			stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id,
12046 			    p
12047 			    );
12048 			if (stcb == NULL) {
12049 				/* Error is setup for us in the call */
12050 				goto out_unlocked;
12051 			}
12052 			if (create_lock_applied) {
12053 				SCTP_ASOC_CREATE_UNLOCK(inp);
12054 				create_lock_applied = 0;
12055 			} else {
12056 				SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12057 			}
12058 			/*
12059 			 * Turn on queue only flag to prevent data from
12060 			 * being sent
12061 			 */
12062 			queue_only = 1;
12063 			asoc = &stcb->asoc;
12064 			SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12065 			(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12066 
12067 			/* initialize authentication params for the assoc */
12068 			sctp_initialize_auth_params(inp, stcb);
12069 
12070 			if (control) {
12071 				/*
12072 				 * see if a init structure exists in cmsg
12073 				 * headers
12074 				 */
12075 				struct sctp_initmsg initm;
12076 				int i;
12077 
12078 				if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
12079 				    sizeof(initm))) {
12080 					/*
12081 					 * we have an INIT override of the
12082 					 * default
12083 					 */
12084 					if (initm.sinit_max_attempts)
12085 						asoc->max_init_times = initm.sinit_max_attempts;
12086 					if (initm.sinit_num_ostreams)
12087 						asoc->pre_open_streams = initm.sinit_num_ostreams;
12088 					if (initm.sinit_max_instreams)
12089 						asoc->max_inbound_streams = initm.sinit_max_instreams;
12090 					if (initm.sinit_max_init_timeo)
12091 						asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
12092 					if (asoc->streamoutcnt < asoc->pre_open_streams) {
12093 						/* Default is NOT correct */
12094 						SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
12095 						    asoc->streamoutcnt, asoc->pre_open_streams);
12096 						/*
12097 						 * What happens if this
12098 						 * fails? we panic ...
12099 						 */
12100 						{
12101 							struct sctp_stream_out *tmp_str;
12102 							int had_lock = 0;
12103 
12104 							if (hold_tcblock) {
12105 								had_lock = 1;
12106 								SCTP_TCB_UNLOCK(stcb);
12107 							}
12108 							SCTP_MALLOC(tmp_str,
12109 							    struct sctp_stream_out *,
12110 							    (asoc->pre_open_streams *
12111 							    sizeof(struct sctp_stream_out)),
12112 							    SCTP_M_STRMO);
12113 							if (had_lock) {
12114 								SCTP_TCB_LOCK(stcb);
12115 							}
12116 							if (tmp_str != NULL) {
12117 								SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
12118 								asoc->strmout = tmp_str;
12119 								asoc->streamoutcnt = asoc->pre_open_streams;
12120 							} else {
12121 								asoc->pre_open_streams = asoc->streamoutcnt;
12122 							}
12123 						}
12124 						for (i = 0; i < asoc->streamoutcnt; i++) {
12125 							/*-
12126 						         * inbound side must be set
12127 						         * to 0xffff, also NOTE when
12128 						         * we get the INIT-ACK back
12129 						         * (for INIT sender) we MUST
12130 						         * reduce the count
12131 						         * (streamoutcnt) but first
12132 						         * check if we sent to any
12133 						         * of the upper streams that
12134 						         * were dropped (if some
12135 						         * were). Those that were
12136 						         * dropped must be notified
12137 						         * to the upper layer as
12138 						         * failed to send.
12139 						         */
12140 							asoc->strmout[i].next_sequence_sent = 0x0;
12141 							TAILQ_INIT(&asoc->strmout[i].outqueue);
12142 							asoc->strmout[i].stream_no = i;
12143 							asoc->strmout[i].last_msg_incomplete = 0;
12144 							asoc->strmout[i].next_spoke.tqe_next = 0;
12145 							asoc->strmout[i].next_spoke.tqe_prev = 0;
12146 						}
12147 					}
12148 				}
12149 			}
12150 			hold_tcblock = 1;
12151 			/* out with the INIT */
12152 			queue_only_for_init = 1;
12153 			/*-
12154 		         * we may want to dig in after this call and adjust the MTU
12155 		         * value. It defaulted to 1500 (constant) but the ro
12156 		         * structure may now have an update and thus we may need to
12157 		         * change it BEFORE we append the message.
12158 		         */
12159 			net = stcb->asoc.primary_destination;
12160 			asoc = &stcb->asoc;
12161 		}
12162 	}
12163 	if ((SCTP_SO_IS_NBIO(so)
12164 	    || (flags & MSG_NBIO)
12165 	    )) {
12166 		non_blocking = 1;
12167 	}
12168 	asoc = &stcb->asoc;
12169 
12170 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12171 		if (sndlen > asoc->smallest_mtu) {
12172 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12173 			error = EMSGSIZE;
12174 			goto out_unlocked;
12175 		}
12176 	}
12177 	/* would we block? */
12178 	if (non_blocking) {
12179 		if (hold_tcblock == 0) {
12180 			SCTP_TCB_LOCK(stcb);
12181 			hold_tcblock = 1;
12182 		}
12183 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12184 		if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12185 		    (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12186 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
12187 			if (sndlen > SCTP_SB_LIMIT_SND(so))
12188 				error = EMSGSIZE;
12189 			else
12190 				error = EWOULDBLOCK;
12191 			goto out_unlocked;
12192 		}
12193 		stcb->asoc.sb_send_resv += sndlen;
12194 		SCTP_TCB_UNLOCK(stcb);
12195 		hold_tcblock = 0;
12196 	} else {
12197 		atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
12198 	}
12199 	local_soresv = sndlen;
12200 	/* Keep the stcb from being freed under our feet */
12201 	if (free_cnt_applied) {
12202 #ifdef INVARIANTS
12203 		panic("refcnt already incremented");
12204 #else
12205 		printf("refcnt:1 already incremented?\n");
12206 #endif
12207 	} else {
12208 		atomic_add_int(&stcb->asoc.refcnt, 1);
12209 		free_cnt_applied = 1;
12210 	}
12211 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12212 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12213 		error = ECONNRESET;
12214 		goto out_unlocked;
12215 	}
12216 	if (create_lock_applied) {
12217 		SCTP_ASOC_CREATE_UNLOCK(inp);
12218 		create_lock_applied = 0;
12219 	}
12220 	if (asoc->stream_reset_outstanding) {
12221 		/*
12222 		 * Can't queue any data while stream reset is underway.
12223 		 */
12224 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
12225 		error = EAGAIN;
12226 		goto out_unlocked;
12227 	}
12228 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12229 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12230 		queue_only = 1;
12231 	}
12232 	if ((use_rcvinfo == 0) || (srcv == NULL)) {
12233 		/* Grab the default stuff from the asoc */
12234 		srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
12235 	}
12236 	/* we are now done with all control */
12237 	if (control) {
12238 		sctp_m_freem(control);
12239 		control = NULL;
12240 	}
12241 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12242 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12243 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12244 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12245 		if ((use_rcvinfo) &&
12246 		    (srcv->sinfo_flags & SCTP_ABORT)) {
12247 			;
12248 		} else {
12249 			SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12250 			error = ECONNRESET;
12251 			goto out_unlocked;
12252 		}
12253 	}
12254 	/* Ok, we will attempt a msgsnd :> */
12255 	if (p) {
12256 		p->td_ru.ru_msgsnd++;
12257 	}
12258 	if (stcb) {
12259 		if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
12260 			net = stcb->asoc.primary_destination;
12261 		}
12262 	}
12263 	if (net == NULL) {
12264 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12265 		error = EINVAL;
12266 		goto out_unlocked;
12267 	}
12268 	if ((net->flight_size > net->cwnd) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12269 		/*-
12270 		 * CMT: Added check for CMT above. net above is the primary
12271 		 * dest. If CMT is ON, sender should always attempt to send
12272 		 * with the output routine sctp_fill_outqueue() that loops
12273 		 * through all destination addresses. Therefore, if CMT is
12274 		 * ON, queue_only is NOT set to 1 here, so that
12275 		 * sctp_chunk_output() can be called below.
12276 		 */
12277 		queue_only = 1;
12278 
12279 	} else if (asoc->ifp_had_enobuf) {
12280 		SCTP_STAT_INCR(sctps_ifnomemqueued);
12281 		if (net->flight_size > (net->mtu * 2))
12282 			queue_only = 1;
12283 		asoc->ifp_had_enobuf = 0;
12284 	} else {
12285 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12286 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12287 	}
12288 	/* Are we aborting? */
12289 	if (srcv->sinfo_flags & SCTP_ABORT) {
12290 		struct mbuf *mm;
12291 		int tot_demand, tot_out = 0, max_out;
12292 
12293 		SCTP_STAT_INCR(sctps_sends_with_abort);
12294 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12295 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12296 			/* It has to be up before we abort */
12297 			/* how big is the user initiated abort? */
12298 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12299 			error = EINVAL;
12300 			goto out;
12301 		}
12302 		if (hold_tcblock) {
12303 			SCTP_TCB_UNLOCK(stcb);
12304 			hold_tcblock = 0;
12305 		}
12306 		if (top) {
12307 			struct mbuf *cntm = NULL;
12308 
12309 			mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
12310 			if (sndlen != 0) {
12311 				cntm = top;
12312 				while (cntm) {
12313 					tot_out += SCTP_BUF_LEN(cntm);
12314 					cntm = SCTP_BUF_NEXT(cntm);
12315 				}
12316 			}
12317 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12318 		} else {
12319 			/* Must fit in a MTU */
12320 			tot_out = sndlen;
12321 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12322 			if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12323 				/* To big */
12324 				SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12325 				error = EMSGSIZE;
12326 				goto out;
12327 			}
12328 			mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
12329 		}
12330 		if (mm == NULL) {
12331 			SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12332 			error = ENOMEM;
12333 			goto out;
12334 		}
12335 		max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12336 		max_out -= sizeof(struct sctp_abort_msg);
12337 		if (tot_out > max_out) {
12338 			tot_out = max_out;
12339 		}
12340 		if (mm) {
12341 			struct sctp_paramhdr *ph;
12342 
12343 			/* now move forward the data pointer */
12344 			ph = mtod(mm, struct sctp_paramhdr *);
12345 			ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12346 			ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
12347 			ph++;
12348 			SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
12349 			if (top == NULL) {
12350 				error = uiomove((caddr_t)ph, (int)tot_out, uio);
12351 				if (error) {
12352 					/*-
12353 				         * Here if we can't get his data we
12354 				         * still abort we just don't get to
12355 				         * send the users note :-0
12356 				         */
12357 					sctp_m_freem(mm);
12358 					mm = NULL;
12359 				}
12360 			} else {
12361 				if (sndlen != 0) {
12362 					SCTP_BUF_NEXT(mm) = top;
12363 				}
12364 			}
12365 		}
12366 		if (hold_tcblock == 0) {
12367 			SCTP_TCB_LOCK(stcb);
12368 			hold_tcblock = 1;
12369 		}
12370 		atomic_add_int(&stcb->asoc.refcnt, -1);
12371 		free_cnt_applied = 0;
12372 		/* release this lock, otherwise we hang on ourselves */
12373 		sctp_abort_an_association(stcb->sctp_ep, stcb,
12374 		    SCTP_RESPONSE_TO_USER_REQ,
12375 		    mm, SCTP_SO_LOCKED);
12376 		/* now relock the stcb so everything is sane */
12377 		hold_tcblock = 0;
12378 		stcb = NULL;
12379 		/*
12380 		 * In this case top is already chained to mm avoid double
12381 		 * free, since we free it below if top != NULL and driver
12382 		 * would free it after sending the packet out
12383 		 */
12384 		if (sndlen != 0) {
12385 			top = NULL;
12386 		}
12387 		goto out_unlocked;
12388 	}
12389 	/* Calculate the maximum we can send */
12390 	inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12391 	if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12392 		if (non_blocking) {
12393 			/* we already checked for non-blocking above. */
12394 			max_len = sndlen;
12395 		} else {
12396 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12397 		}
12398 	} else {
12399 		max_len = 0;
12400 	}
12401 	if (hold_tcblock) {
12402 		SCTP_TCB_UNLOCK(stcb);
12403 		hold_tcblock = 0;
12404 	}
12405 	/* Is the stream no. valid? */
12406 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
12407 		/* Invalid stream number */
12408 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12409 		error = EINVAL;
12410 		goto out_unlocked;
12411 	}
12412 	if (asoc->strmout == NULL) {
12413 		/* huh? software error */
12414 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12415 		error = EFAULT;
12416 		goto out_unlocked;
12417 	}
12418 	/* Unless E_EOR mode is on, we must make a send FIT in one call. */
12419 	if ((user_marks_eor == 0) &&
12420 	    (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
12421 		/* It will NEVER fit */
12422 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12423 		error = EMSGSIZE;
12424 		goto out_unlocked;
12425 	}
12426 	if ((uio == NULL) && user_marks_eor) {
12427 		/*-
12428 		 * We do not support eeor mode for
12429 		 * sending with mbuf chains (like sendfile).
12430 		 */
12431 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12432 		error = EINVAL;
12433 		goto out_unlocked;
12434 	}
12435 	if (user_marks_eor) {
12436 		local_add_more = SCTP_BASE_SYSCTL(sctp_add_more_threshold);
12437 	} else {
12438 		/*-
12439 		 * For non-eeor the whole message must fit in
12440 		 * the socket send buffer.
12441 		 */
12442 		local_add_more = sndlen;
12443 	}
12444 	len = 0;
12445 	if (non_blocking) {
12446 		goto skip_preblock;
12447 	}
12448 	if (((max_len <= local_add_more) &&
12449 	    (SCTP_SB_LIMIT_SND(so) > local_add_more)) ||
12450 	    ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {	/* if */
12451 		/* No room right now ! */
12452 		SOCKBUF_LOCK(&so->so_snd);
12453 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12454 		while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
12455 		    ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue) /* while */ )) {
12456 
12457 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12458 				sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA,
12459 				    so, asoc, sndlen);
12460 			}
12461 			be.error = 0;
12462 			stcb->block_entry = &be;
12463 			error = sbwait(&so->so_snd);
12464 			stcb->block_entry = NULL;
12465 			if (error || so->so_error || be.error) {
12466 				if (error == 0) {
12467 					if (so->so_error)
12468 						error = so->so_error;
12469 					if (be.error) {
12470 						error = be.error;
12471 					}
12472 				}
12473 				SOCKBUF_UNLOCK(&so->so_snd);
12474 				goto out_unlocked;
12475 			}
12476 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12477 				sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12478 				    so, asoc, stcb->asoc.total_output_queue_size);
12479 			}
12480 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12481 				goto out_unlocked;
12482 			}
12483 			inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12484 		}
12485 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12486 		if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12487 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12488 		} else {
12489 			max_len = 0;
12490 		}
12491 		SOCKBUF_UNLOCK(&so->so_snd);
12492 	}
12493 skip_preblock:
12494 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12495 		goto out_unlocked;
12496 	}
12497 	atomic_add_int(&stcb->total_sends, 1);
12498 	/*
12499 	 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
12500 	 * case NOTE: uio will be null when top/mbuf is passed
12501 	 */
12502 	if (sndlen == 0) {
12503 		if (srcv->sinfo_flags & SCTP_EOF) {
12504 			got_all_of_the_send = 1;
12505 			goto dataless_eof;
12506 		} else {
12507 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12508 			error = EINVAL;
12509 			goto out;
12510 		}
12511 	}
12512 	if (top == NULL) {
12513 		struct sctp_stream_queue_pending *sp;
12514 		struct sctp_stream_out *strm;
12515 		uint32_t sndout, initial_out;
12516 
12517 		initial_out = uio->uio_resid;
12518 
12519 		SCTP_TCB_SEND_LOCK(stcb);
12520 		if ((asoc->stream_locked) &&
12521 		    (asoc->stream_locked_on != srcv->sinfo_stream)) {
12522 			SCTP_TCB_SEND_UNLOCK(stcb);
12523 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12524 			error = EINVAL;
12525 			goto out;
12526 		}
12527 		SCTP_TCB_SEND_UNLOCK(stcb);
12528 
12529 		strm = &stcb->asoc.strmout[srcv->sinfo_stream];
12530 		if (strm->last_msg_incomplete == 0) {
12531 	do_a_copy_in:
12532 			sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
12533 			if ((sp == NULL) || (error)) {
12534 				goto out;
12535 			}
12536 			SCTP_TCB_SEND_LOCK(stcb);
12537 			if (sp->msg_is_complete) {
12538 				strm->last_msg_incomplete = 0;
12539 				asoc->stream_locked = 0;
12540 			} else {
12541 				/*
12542 				 * Just got locked to this guy in case of an
12543 				 * interrupt.
12544 				 */
12545 				strm->last_msg_incomplete = 1;
12546 				asoc->stream_locked = 1;
12547 				asoc->stream_locked_on = srcv->sinfo_stream;
12548 				sp->sender_all_done = 0;
12549 			}
12550 			sctp_snd_sb_alloc(stcb, sp->length);
12551 			atomic_add_int(&asoc->stream_queue_cnt, 1);
12552 			if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
12553 				sp->strseq = strm->next_sequence_sent;
12554 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
12555 					sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
12556 					    (uintptr_t) stcb, sp->length,
12557 					    (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
12558 				}
12559 				strm->next_sequence_sent++;
12560 			} else {
12561 				SCTP_STAT_INCR(sctps_sends_with_unord);
12562 			}
12563 			TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
12564 			if ((strm->next_spoke.tqe_next == NULL) &&
12565 			    (strm->next_spoke.tqe_prev == NULL)) {
12566 				/* Not on wheel, insert */
12567 				sctp_insert_on_wheel(stcb, asoc, strm, 1);
12568 			}
12569 			SCTP_TCB_SEND_UNLOCK(stcb);
12570 		} else {
12571 			SCTP_TCB_SEND_LOCK(stcb);
12572 			sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
12573 			SCTP_TCB_SEND_UNLOCK(stcb);
12574 			if (sp == NULL) {
12575 				/* ???? Huh ??? last msg is gone */
12576 #ifdef INVARIANTS
12577 				panic("Warning: Last msg marked incomplete, yet nothing left?");
12578 #else
12579 				SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
12580 				strm->last_msg_incomplete = 0;
12581 #endif
12582 				goto do_a_copy_in;
12583 
12584 			}
12585 		}
12586 		while (uio->uio_resid > 0) {
12587 			/* How much room do we have? */
12588 			struct mbuf *new_tail, *mm;
12589 
12590 			if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12591 				max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
12592 			else
12593 				max_len = 0;
12594 
12595 			if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
12596 			    (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
12597 			    (uio->uio_resid &&
12598 			    (uio->uio_resid <= (int)max_len))) {
12599 				sndout = 0;
12600 				new_tail = NULL;
12601 				if (hold_tcblock) {
12602 					SCTP_TCB_UNLOCK(stcb);
12603 					hold_tcblock = 0;
12604 				}
12605 				mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
12606 				if ((mm == NULL) || error) {
12607 					if (mm) {
12608 						sctp_m_freem(mm);
12609 					}
12610 					goto out;
12611 				}
12612 				/* Update the mbuf and count */
12613 				SCTP_TCB_SEND_LOCK(stcb);
12614 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12615 					/*
12616 					 * we need to get out. Peer probably
12617 					 * aborted.
12618 					 */
12619 					sctp_m_freem(mm);
12620 					if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
12621 						SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12622 						error = ECONNRESET;
12623 					}
12624 					SCTP_TCB_SEND_UNLOCK(stcb);
12625 					goto out;
12626 				}
12627 				if (sp->tail_mbuf) {
12628 					/* tack it to the end */
12629 					SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
12630 					sp->tail_mbuf = new_tail;
12631 				} else {
12632 					/* A stolen mbuf */
12633 					sp->data = mm;
12634 					sp->tail_mbuf = new_tail;
12635 				}
12636 				sctp_snd_sb_alloc(stcb, sndout);
12637 				atomic_add_int(&sp->length, sndout);
12638 				len += sndout;
12639 
12640 				/* Did we reach EOR? */
12641 				if ((uio->uio_resid == 0) &&
12642 				    ((user_marks_eor == 0) ||
12643 				    (srcv->sinfo_flags & SCTP_EOF) ||
12644 				    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))
12645 				    ) {
12646 					sp->msg_is_complete = 1;
12647 				} else {
12648 					sp->msg_is_complete = 0;
12649 				}
12650 				SCTP_TCB_SEND_UNLOCK(stcb);
12651 			}
12652 			if (uio->uio_resid == 0) {
12653 				/* got it all? */
12654 				continue;
12655 			}
12656 			/* PR-SCTP? */
12657 			if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
12658 				/*
12659 				 * This is ugly but we must assure locking
12660 				 * order
12661 				 */
12662 				if (hold_tcblock == 0) {
12663 					SCTP_TCB_LOCK(stcb);
12664 					hold_tcblock = 1;
12665 				}
12666 				sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
12667 				inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12668 				if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12669 					max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12670 				else
12671 					max_len = 0;
12672 				if (max_len > 0) {
12673 					continue;
12674 				}
12675 				SCTP_TCB_UNLOCK(stcb);
12676 				hold_tcblock = 0;
12677 			}
12678 			/* wait for space now */
12679 			if (non_blocking) {
12680 				/* Non-blocking io in place out */
12681 				goto skip_out_eof;
12682 			}
12683 			if ((net->flight_size > net->cwnd) &&
12684 			    (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12685 				queue_only = 1;
12686 			} else if (asoc->ifp_had_enobuf) {
12687 				SCTP_STAT_INCR(sctps_ifnomemqueued);
12688 				if (net->flight_size > (net->mtu * 2)) {
12689 					queue_only = 1;
12690 				} else {
12691 					queue_only = 0;
12692 				}
12693 				asoc->ifp_had_enobuf = 0;
12694 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12695 				    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12696 			} else {
12697 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12698 				    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12699 				if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
12700 					queue_only = 1;
12701 					SCTP_STAT_INCR(sctps_send_burst_avoid);
12702 				} else if (net->flight_size > net->cwnd) {
12703 					queue_only = 1;
12704 					SCTP_STAT_INCR(sctps_send_cwnd_avoid);
12705 				} else {
12706 					queue_only = 0;
12707 				}
12708 			}
12709 			if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
12710 			    (stcb->asoc.total_flight > 0) &&
12711 			    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
12712 			    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
12713 			    ) {
12714 
12715 				/*-
12716 				 * Ok, Nagle is set on and we have data outstanding.
12717 				 * Don't send anything and let SACKs drive out the
12718 				 * data unless wen have a "full" segment to send.
12719 				 */
12720 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
12721 					sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
12722 				}
12723 				SCTP_STAT_INCR(sctps_naglequeued);
12724 				nagle_applies = 1;
12725 			} else {
12726 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
12727 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
12728 						sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
12729 				}
12730 				SCTP_STAT_INCR(sctps_naglesent);
12731 				nagle_applies = 0;
12732 			}
12733 			/* What about the INIT, send it maybe */
12734 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12735 
12736 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
12737 				    nagle_applies, un_sent);
12738 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
12739 				    stcb->asoc.total_flight,
12740 				    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
12741 			}
12742 			if (queue_only_for_init) {
12743 				if (hold_tcblock == 0) {
12744 					SCTP_TCB_LOCK(stcb);
12745 					hold_tcblock = 1;
12746 				}
12747 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
12748 					/* a collision took us forward? */
12749 					queue_only_for_init = 0;
12750 					queue_only = 0;
12751 				} else {
12752 					sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
12753 					SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12754 					queue_only_for_init = 0;
12755 					queue_only = 1;
12756 				}
12757 			}
12758 			if ((queue_only == 0) && (nagle_applies == 0)
12759 			    ) {
12760 				/*-
12761 				 * need to start chunk output
12762 				 * before blocking.. note that if
12763 				 * a lock is already applied, then
12764 				 * the input via the net is happening
12765 				 * and I don't need to start output :-D
12766 				 */
12767 				if (hold_tcblock == 0) {
12768 					if (SCTP_TCB_TRYLOCK(stcb)) {
12769 						hold_tcblock = 1;
12770 						sctp_chunk_output(inp,
12771 						    stcb,
12772 						    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12773 					}
12774 				} else {
12775 					sctp_chunk_output(inp,
12776 					    stcb,
12777 					    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12778 				}
12779 				if (hold_tcblock == 1) {
12780 					SCTP_TCB_UNLOCK(stcb);
12781 					hold_tcblock = 0;
12782 				}
12783 			}
12784 			SOCKBUF_LOCK(&so->so_snd);
12785 			/*-
12786 		         * This is a bit strange, but I think it will
12787 		         * work. The total_output_queue_size is locked and
12788 		         * protected by the TCB_LOCK, which we just released.
12789 		         * There is a race that can occur between releasing it
12790 		         * above, and me getting the socket lock, where sacks
12791 		         * come in but we have not put the SB_WAIT on the
12792 		         * so_snd buffer to get the wakeup. After the LOCK
12793 		         * is applied the sack_processing will also need to
12794 		         * LOCK the so->so_snd to do the actual sowwakeup(). So
12795 		         * once we have the socket buffer lock if we recheck the
12796 		         * size we KNOW we will get to sleep safely with the
12797 		         * wakeup flag in place.
12798 		         */
12799 			if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
12800 			    min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))
12801 			    ) {
12802 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12803 					sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
12804 					    so, asoc, uio->uio_resid);
12805 				}
12806 				be.error = 0;
12807 				stcb->block_entry = &be;
12808 				error = sbwait(&so->so_snd);
12809 				stcb->block_entry = NULL;
12810 
12811 				if (error || so->so_error || be.error) {
12812 					if (error == 0) {
12813 						if (so->so_error)
12814 							error = so->so_error;
12815 						if (be.error) {
12816 							error = be.error;
12817 						}
12818 					}
12819 					SOCKBUF_UNLOCK(&so->so_snd);
12820 					goto out_unlocked;
12821 				}
12822 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12823 					sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12824 					    so, asoc, stcb->asoc.total_output_queue_size);
12825 				}
12826 			}
12827 			SOCKBUF_UNLOCK(&so->so_snd);
12828 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12829 				goto out_unlocked;
12830 			}
12831 		}
12832 		SCTP_TCB_SEND_LOCK(stcb);
12833 		if (sp) {
12834 			if (sp->msg_is_complete == 0) {
12835 				strm->last_msg_incomplete = 1;
12836 				asoc->stream_locked = 1;
12837 				asoc->stream_locked_on = srcv->sinfo_stream;
12838 			} else {
12839 				sp->sender_all_done = 1;
12840 				strm->last_msg_incomplete = 0;
12841 				asoc->stream_locked = 0;
12842 			}
12843 		} else {
12844 			SCTP_PRINTF("Huh no sp TSNH?\n");
12845 			strm->last_msg_incomplete = 0;
12846 			asoc->stream_locked = 0;
12847 		}
12848 		SCTP_TCB_SEND_UNLOCK(stcb);
12849 		if (uio->uio_resid == 0) {
12850 			got_all_of_the_send = 1;
12851 		}
12852 	} else if (top) {
12853 		/* We send in a 0, since we do NOT have any locks */
12854 		error = sctp_msg_append(stcb, net, top, srcv, 0);
12855 		top = NULL;
12856 		if (srcv->sinfo_flags & SCTP_EOF) {
12857 			/*
12858 			 * This should only happen for Panda for the mbuf
12859 			 * send case, which does NOT yet support EEOR mode.
12860 			 * Thus, we can just set this flag to do the proper
12861 			 * EOF handling.
12862 			 */
12863 			got_all_of_the_send = 1;
12864 		}
12865 	}
12866 	if (error) {
12867 		goto out;
12868 	}
12869 dataless_eof:
12870 	/* EOF thing ? */
12871 	if ((srcv->sinfo_flags & SCTP_EOF) &&
12872 	    (got_all_of_the_send == 1) &&
12873 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
12874 	    ) {
12875 		int cnt;
12876 
12877 		SCTP_STAT_INCR(sctps_sends_with_eof);
12878 		error = 0;
12879 		if (hold_tcblock == 0) {
12880 			SCTP_TCB_LOCK(stcb);
12881 			hold_tcblock = 1;
12882 		}
12883 		cnt = sctp_is_there_unsent_data(stcb);
12884 		if (TAILQ_EMPTY(&asoc->send_queue) &&
12885 		    TAILQ_EMPTY(&asoc->sent_queue) &&
12886 		    (cnt == 0)) {
12887 			if (asoc->locked_on_sending) {
12888 				goto abort_anyway;
12889 			}
12890 			/* there is nothing queued to send, so I'm done... */
12891 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
12892 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
12893 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
12894 				/* only send SHUTDOWN the first time through */
12895 				sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
12896 				if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
12897 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
12898 				}
12899 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
12900 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
12901 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
12902 				    asoc->primary_destination);
12903 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
12904 				    asoc->primary_destination);
12905 			}
12906 		} else {
12907 			/*-
12908 		         * we still got (or just got) data to send, so set
12909 		         * SHUTDOWN_PENDING
12910 		         */
12911 			/*-
12912 		         * XXX sockets draft says that SCTP_EOF should be
12913 		         * sent with no data.  currently, we will allow user
12914 		         * data to be sent first and move to
12915 		         * SHUTDOWN-PENDING
12916 		         */
12917 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
12918 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
12919 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
12920 				if (hold_tcblock == 0) {
12921 					SCTP_TCB_LOCK(stcb);
12922 					hold_tcblock = 1;
12923 				}
12924 				if (asoc->locked_on_sending) {
12925 					/* Locked to send out the data */
12926 					struct sctp_stream_queue_pending *sp;
12927 
12928 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
12929 					if (sp) {
12930 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
12931 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
12932 					}
12933 				}
12934 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
12935 				if (TAILQ_EMPTY(&asoc->send_queue) &&
12936 				    TAILQ_EMPTY(&asoc->sent_queue) &&
12937 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
12938 			abort_anyway:
12939 					if (free_cnt_applied) {
12940 						atomic_add_int(&stcb->asoc.refcnt, -1);
12941 						free_cnt_applied = 0;
12942 					}
12943 					sctp_abort_an_association(stcb->sctp_ep, stcb,
12944 					    SCTP_RESPONSE_TO_USER_REQ,
12945 					    NULL, SCTP_SO_LOCKED);
12946 					/*
12947 					 * now relock the stcb so everything
12948 					 * is sane
12949 					 */
12950 					hold_tcblock = 0;
12951 					stcb = NULL;
12952 					goto out;
12953 				}
12954 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
12955 				    asoc->primary_destination);
12956 				sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
12957 			}
12958 		}
12959 	}
12960 skip_out_eof:
12961 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
12962 		some_on_control = 1;
12963 	}
12964 	if ((net->flight_size > net->cwnd) &&
12965 	    (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12966 		queue_only = 1;
12967 	} else if (asoc->ifp_had_enobuf) {
12968 		SCTP_STAT_INCR(sctps_ifnomemqueued);
12969 		if (net->flight_size > (net->mtu * 2)) {
12970 			queue_only = 1;
12971 		} else {
12972 			queue_only = 0;
12973 		}
12974 		asoc->ifp_had_enobuf = 0;
12975 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12976 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12977 	} else {
12978 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12979 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12980 		if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
12981 			queue_only = 1;
12982 			SCTP_STAT_INCR(sctps_send_burst_avoid);
12983 		} else if (net->flight_size > net->cwnd) {
12984 			queue_only = 1;
12985 			SCTP_STAT_INCR(sctps_send_cwnd_avoid);
12986 		} else {
12987 			queue_only = 0;
12988 		}
12989 	}
12990 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
12991 	    (stcb->asoc.total_flight > 0) &&
12992 	    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
12993 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
12994 	    ) {
12995 		/*-
12996 		 * Ok, Nagle is set on and we have data outstanding.
12997 		 * Don't send anything and let SACKs drive out the
12998 		 * data unless wen have a "full" segment to send.
12999 		 */
13000 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13001 			sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13002 		}
13003 		SCTP_STAT_INCR(sctps_naglequeued);
13004 		nagle_applies = 1;
13005 	} else {
13006 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13007 			if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13008 				sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13009 		}
13010 		SCTP_STAT_INCR(sctps_naglesent);
13011 		nagle_applies = 0;
13012 	}
13013 	if (queue_only_for_init) {
13014 		if (hold_tcblock == 0) {
13015 			SCTP_TCB_LOCK(stcb);
13016 			hold_tcblock = 1;
13017 		}
13018 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13019 			/* a collision took us forward? */
13020 			queue_only_for_init = 0;
13021 			queue_only = 0;
13022 		} else {
13023 			sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13024 			SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13025 			queue_only_for_init = 0;
13026 			queue_only = 1;
13027 		}
13028 	}
13029 	if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13030 		/* we can attempt to send too. */
13031 		if (hold_tcblock == 0) {
13032 			/*
13033 			 * If there is activity recv'ing sacks no need to
13034 			 * send
13035 			 */
13036 			if (SCTP_TCB_TRYLOCK(stcb)) {
13037 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13038 				hold_tcblock = 1;
13039 			}
13040 		} else {
13041 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13042 		}
13043 	} else if ((queue_only == 0) &&
13044 		    (stcb->asoc.peers_rwnd == 0) &&
13045 	    (stcb->asoc.total_flight == 0)) {
13046 		/* We get to have a probe outstanding */
13047 		if (hold_tcblock == 0) {
13048 			hold_tcblock = 1;
13049 			SCTP_TCB_LOCK(stcb);
13050 		}
13051 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13052 	} else if (some_on_control) {
13053 		int num_out, reason, cwnd_full, frag_point;
13054 
13055 		/* Here we do control only */
13056 		if (hold_tcblock == 0) {
13057 			hold_tcblock = 1;
13058 			SCTP_TCB_LOCK(stcb);
13059 		}
13060 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13061 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13062 		    &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13063 	}
13064 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d",
13065 	    queue_only, stcb->asoc.peers_rwnd, un_sent,
13066 	    stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13067 	    stcb->asoc.total_output_queue_size, error);
13068 
13069 out:
13070 out_unlocked:
13071 
13072 	if (local_soresv && stcb) {
13073 		atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13074 		local_soresv = 0;
13075 	}
13076 	if (create_lock_applied) {
13077 		SCTP_ASOC_CREATE_UNLOCK(inp);
13078 		create_lock_applied = 0;
13079 	}
13080 	if ((stcb) && hold_tcblock) {
13081 		SCTP_TCB_UNLOCK(stcb);
13082 	}
13083 	if (stcb && free_cnt_applied) {
13084 		atomic_add_int(&stcb->asoc.refcnt, -1);
13085 	}
13086 #ifdef INVARIANTS
13087 	if (stcb) {
13088 		if (mtx_owned(&stcb->tcb_mtx)) {
13089 			panic("Leaving with tcb mtx owned?");
13090 		}
13091 		if (mtx_owned(&stcb->tcb_send_mtx)) {
13092 			panic("Leaving with tcb send mtx owned?");
13093 		}
13094 	}
13095 #endif
13096 	if (top) {
13097 		sctp_m_freem(top);
13098 	}
13099 	if (control) {
13100 		sctp_m_freem(control);
13101 	}
13102 	return (error);
13103 }
13104 
13105 
13106 /*
13107  * generate an AUTHentication chunk, if required
13108  */
13109 struct mbuf *
13110 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13111     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13112     struct sctp_tcb *stcb, uint8_t chunk)
13113 {
13114 	struct mbuf *m_auth;
13115 	struct sctp_auth_chunk *auth;
13116 	int chunk_len;
13117 
13118 	if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13119 	    (stcb == NULL))
13120 		return (m);
13121 
13122 	/* sysctl disabled auth? */
13123 	if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13124 		return (m);
13125 
13126 	/* peer doesn't do auth... */
13127 	if (!stcb->asoc.peer_supports_auth) {
13128 		return (m);
13129 	}
13130 	/* does the requested chunk require auth? */
13131 	if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13132 		return (m);
13133 	}
13134 	m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13135 	if (m_auth == NULL) {
13136 		/* no mbuf's */
13137 		return (m);
13138 	}
13139 	/* reserve some space if this will be the first mbuf */
13140 	if (m == NULL)
13141 		SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13142 	/* fill in the AUTH chunk details */
13143 	auth = mtod(m_auth, struct sctp_auth_chunk *);
13144 	bzero(auth, sizeof(*auth));
13145 	auth->ch.chunk_type = SCTP_AUTHENTICATION;
13146 	auth->ch.chunk_flags = 0;
13147 	chunk_len = sizeof(*auth) +
13148 	    sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13149 	auth->ch.chunk_length = htons(chunk_len);
13150 	auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13151 	/* key id and hmac digest will be computed and filled in upon send */
13152 
13153 	/* save the offset where the auth was inserted into the chain */
13154 	if (m != NULL) {
13155 		struct mbuf *cn;
13156 
13157 		*offset = 0;
13158 		cn = m;
13159 		while (cn) {
13160 			*offset += SCTP_BUF_LEN(cn);
13161 			cn = SCTP_BUF_NEXT(cn);
13162 		}
13163 	} else
13164 		*offset = 0;
13165 
13166 	/* update length and return pointer to the auth chunk */
13167 	SCTP_BUF_LEN(m_auth) = chunk_len;
13168 	m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13169 	if (auth_ret != NULL)
13170 		*auth_ret = auth;
13171 
13172 	return (m);
13173 }
13174 
13175 #ifdef INET6
13176 int
13177 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13178 {
13179 	struct nd_prefix *pfx = NULL;
13180 	struct nd_pfxrouter *pfxrtr = NULL;
13181 	struct sockaddr_in6 gw6;
13182 
13183 	if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13184 		return (0);
13185 
13186 	/* get prefix entry of address */
13187 	LIST_FOREACH(pfx, &MODULE_GLOBAL(MOD_INET6, nd_prefix), ndpr_entry) {
13188 		if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13189 			continue;
13190 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13191 		    &src6->sin6_addr, &pfx->ndpr_mask))
13192 			break;
13193 	}
13194 	/* no prefix entry in the prefix list */
13195 	if (pfx == NULL) {
13196 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13197 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13198 		return (0);
13199 	}
13200 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13201 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13202 
13203 	/* search installed gateway from prefix entry */
13204 	for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
13205 	    pfxrtr->pfr_next) {
13206 		memset(&gw6, 0, sizeof(struct sockaddr_in6));
13207 		gw6.sin6_family = AF_INET6;
13208 		gw6.sin6_len = sizeof(struct sockaddr_in6);
13209 		memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13210 		    sizeof(struct in6_addr));
13211 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13212 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13213 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13214 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13215 		if (sctp_cmpaddr((struct sockaddr *)&gw6,
13216 		    ro->ro_rt->rt_gateway)) {
13217 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13218 			return (1);
13219 		}
13220 	}
13221 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13222 	return (0);
13223 }
13224 
13225 #endif
13226 
13227 int
13228 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13229 {
13230 	struct sockaddr_in *sin, *mask;
13231 	struct ifaddr *ifa;
13232 	struct in_addr srcnetaddr, gwnetaddr;
13233 
13234 	if (ro == NULL || ro->ro_rt == NULL ||
13235 	    sifa->address.sa.sa_family != AF_INET) {
13236 		return (0);
13237 	}
13238 	ifa = (struct ifaddr *)sifa->ifa;
13239 	mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13240 	sin = (struct sockaddr_in *)&sifa->address.sin;
13241 	srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13242 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13243 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13244 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13245 
13246 	sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13247 	gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13248 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13249 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13250 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13251 	if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13252 		return (1);
13253 	}
13254 	return (0);
13255 }
13256