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