xref: /freebsd/sys/netinet/sctp_output.c (revision b2db760808f74bb53c232900091c9da801ebbfcc)
1 /*-
2  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $	 */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <netinet/sctp_os.h>
37 #include <sys/proc.h>
38 #include <netinet/sctp_var.h>
39 #include <netinet/sctp_sysctl.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctputil.h>
43 #include <netinet/sctp_output.h>
44 #include <netinet/sctp_uio.h>
45 #include <netinet/sctputil.h>
46 #include <netinet/sctp_auth.h>
47 #include <netinet/sctp_timer.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_indata.h>
50 #include <netinet/sctp_bsd_addr.h>
51 #include <netinet/sctp_input.h>
52 #include <netinet/sctp_crc32.h>
53 #include <netinet/udp.h>
54 #include <machine/in_cksum.h>
55 
56 
57 
58 #define SCTP_MAX_GAPS_INARRAY 4
59 struct sack_track {
60 	uint8_t right_edge;	/* mergable on the right edge */
61 	uint8_t left_edge;	/* mergable on the left edge */
62 	uint8_t num_entries;
63 	uint8_t spare;
64 	struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
65 };
66 
67 struct sack_track sack_array[256] = {
68 	{0, 0, 0, 0,		/* 0x00 */
69 		{{0, 0},
70 		{0, 0},
71 		{0, 0},
72 		{0, 0}
73 		}
74 	},
75 	{1, 0, 1, 0,		/* 0x01 */
76 		{{0, 0},
77 		{0, 0},
78 		{0, 0},
79 		{0, 0}
80 		}
81 	},
82 	{0, 0, 1, 0,		/* 0x02 */
83 		{{1, 1},
84 		{0, 0},
85 		{0, 0},
86 		{0, 0}
87 		}
88 	},
89 	{1, 0, 1, 0,		/* 0x03 */
90 		{{0, 1},
91 		{0, 0},
92 		{0, 0},
93 		{0, 0}
94 		}
95 	},
96 	{0, 0, 1, 0,		/* 0x04 */
97 		{{2, 2},
98 		{0, 0},
99 		{0, 0},
100 		{0, 0}
101 		}
102 	},
103 	{1, 0, 2, 0,		/* 0x05 */
104 		{{0, 0},
105 		{2, 2},
106 		{0, 0},
107 		{0, 0}
108 		}
109 	},
110 	{0, 0, 1, 0,		/* 0x06 */
111 		{{1, 2},
112 		{0, 0},
113 		{0, 0},
114 		{0, 0}
115 		}
116 	},
117 	{1, 0, 1, 0,		/* 0x07 */
118 		{{0, 2},
119 		{0, 0},
120 		{0, 0},
121 		{0, 0}
122 		}
123 	},
124 	{0, 0, 1, 0,		/* 0x08 */
125 		{{3, 3},
126 		{0, 0},
127 		{0, 0},
128 		{0, 0}
129 		}
130 	},
131 	{1, 0, 2, 0,		/* 0x09 */
132 		{{0, 0},
133 		{3, 3},
134 		{0, 0},
135 		{0, 0}
136 		}
137 	},
138 	{0, 0, 2, 0,		/* 0x0a */
139 		{{1, 1},
140 		{3, 3},
141 		{0, 0},
142 		{0, 0}
143 		}
144 	},
145 	{1, 0, 2, 0,		/* 0x0b */
146 		{{0, 1},
147 		{3, 3},
148 		{0, 0},
149 		{0, 0}
150 		}
151 	},
152 	{0, 0, 1, 0,		/* 0x0c */
153 		{{2, 3},
154 		{0, 0},
155 		{0, 0},
156 		{0, 0}
157 		}
158 	},
159 	{1, 0, 2, 0,		/* 0x0d */
160 		{{0, 0},
161 		{2, 3},
162 		{0, 0},
163 		{0, 0}
164 		}
165 	},
166 	{0, 0, 1, 0,		/* 0x0e */
167 		{{1, 3},
168 		{0, 0},
169 		{0, 0},
170 		{0, 0}
171 		}
172 	},
173 	{1, 0, 1, 0,		/* 0x0f */
174 		{{0, 3},
175 		{0, 0},
176 		{0, 0},
177 		{0, 0}
178 		}
179 	},
180 	{0, 0, 1, 0,		/* 0x10 */
181 		{{4, 4},
182 		{0, 0},
183 		{0, 0},
184 		{0, 0}
185 		}
186 	},
187 	{1, 0, 2, 0,		/* 0x11 */
188 		{{0, 0},
189 		{4, 4},
190 		{0, 0},
191 		{0, 0}
192 		}
193 	},
194 	{0, 0, 2, 0,		/* 0x12 */
195 		{{1, 1},
196 		{4, 4},
197 		{0, 0},
198 		{0, 0}
199 		}
200 	},
201 	{1, 0, 2, 0,		/* 0x13 */
202 		{{0, 1},
203 		{4, 4},
204 		{0, 0},
205 		{0, 0}
206 		}
207 	},
208 	{0, 0, 2, 0,		/* 0x14 */
209 		{{2, 2},
210 		{4, 4},
211 		{0, 0},
212 		{0, 0}
213 		}
214 	},
215 	{1, 0, 3, 0,		/* 0x15 */
216 		{{0, 0},
217 		{2, 2},
218 		{4, 4},
219 		{0, 0}
220 		}
221 	},
222 	{0, 0, 2, 0,		/* 0x16 */
223 		{{1, 2},
224 		{4, 4},
225 		{0, 0},
226 		{0, 0}
227 		}
228 	},
229 	{1, 0, 2, 0,		/* 0x17 */
230 		{{0, 2},
231 		{4, 4},
232 		{0, 0},
233 		{0, 0}
234 		}
235 	},
236 	{0, 0, 1, 0,		/* 0x18 */
237 		{{3, 4},
238 		{0, 0},
239 		{0, 0},
240 		{0, 0}
241 		}
242 	},
243 	{1, 0, 2, 0,		/* 0x19 */
244 		{{0, 0},
245 		{3, 4},
246 		{0, 0},
247 		{0, 0}
248 		}
249 	},
250 	{0, 0, 2, 0,		/* 0x1a */
251 		{{1, 1},
252 		{3, 4},
253 		{0, 0},
254 		{0, 0}
255 		}
256 	},
257 	{1, 0, 2, 0,		/* 0x1b */
258 		{{0, 1},
259 		{3, 4},
260 		{0, 0},
261 		{0, 0}
262 		}
263 	},
264 	{0, 0, 1, 0,		/* 0x1c */
265 		{{2, 4},
266 		{0, 0},
267 		{0, 0},
268 		{0, 0}
269 		}
270 	},
271 	{1, 0, 2, 0,		/* 0x1d */
272 		{{0, 0},
273 		{2, 4},
274 		{0, 0},
275 		{0, 0}
276 		}
277 	},
278 	{0, 0, 1, 0,		/* 0x1e */
279 		{{1, 4},
280 		{0, 0},
281 		{0, 0},
282 		{0, 0}
283 		}
284 	},
285 	{1, 0, 1, 0,		/* 0x1f */
286 		{{0, 4},
287 		{0, 0},
288 		{0, 0},
289 		{0, 0}
290 		}
291 	},
292 	{0, 0, 1, 0,		/* 0x20 */
293 		{{5, 5},
294 		{0, 0},
295 		{0, 0},
296 		{0, 0}
297 		}
298 	},
299 	{1, 0, 2, 0,		/* 0x21 */
300 		{{0, 0},
301 		{5, 5},
302 		{0, 0},
303 		{0, 0}
304 		}
305 	},
306 	{0, 0, 2, 0,		/* 0x22 */
307 		{{1, 1},
308 		{5, 5},
309 		{0, 0},
310 		{0, 0}
311 		}
312 	},
313 	{1, 0, 2, 0,		/* 0x23 */
314 		{{0, 1},
315 		{5, 5},
316 		{0, 0},
317 		{0, 0}
318 		}
319 	},
320 	{0, 0, 2, 0,		/* 0x24 */
321 		{{2, 2},
322 		{5, 5},
323 		{0, 0},
324 		{0, 0}
325 		}
326 	},
327 	{1, 0, 3, 0,		/* 0x25 */
328 		{{0, 0},
329 		{2, 2},
330 		{5, 5},
331 		{0, 0}
332 		}
333 	},
334 	{0, 0, 2, 0,		/* 0x26 */
335 		{{1, 2},
336 		{5, 5},
337 		{0, 0},
338 		{0, 0}
339 		}
340 	},
341 	{1, 0, 2, 0,		/* 0x27 */
342 		{{0, 2},
343 		{5, 5},
344 		{0, 0},
345 		{0, 0}
346 		}
347 	},
348 	{0, 0, 2, 0,		/* 0x28 */
349 		{{3, 3},
350 		{5, 5},
351 		{0, 0},
352 		{0, 0}
353 		}
354 	},
355 	{1, 0, 3, 0,		/* 0x29 */
356 		{{0, 0},
357 		{3, 3},
358 		{5, 5},
359 		{0, 0}
360 		}
361 	},
362 	{0, 0, 3, 0,		/* 0x2a */
363 		{{1, 1},
364 		{3, 3},
365 		{5, 5},
366 		{0, 0}
367 		}
368 	},
369 	{1, 0, 3, 0,		/* 0x2b */
370 		{{0, 1},
371 		{3, 3},
372 		{5, 5},
373 		{0, 0}
374 		}
375 	},
376 	{0, 0, 2, 0,		/* 0x2c */
377 		{{2, 3},
378 		{5, 5},
379 		{0, 0},
380 		{0, 0}
381 		}
382 	},
383 	{1, 0, 3, 0,		/* 0x2d */
384 		{{0, 0},
385 		{2, 3},
386 		{5, 5},
387 		{0, 0}
388 		}
389 	},
390 	{0, 0, 2, 0,		/* 0x2e */
391 		{{1, 3},
392 		{5, 5},
393 		{0, 0},
394 		{0, 0}
395 		}
396 	},
397 	{1, 0, 2, 0,		/* 0x2f */
398 		{{0, 3},
399 		{5, 5},
400 		{0, 0},
401 		{0, 0}
402 		}
403 	},
404 	{0, 0, 1, 0,		/* 0x30 */
405 		{{4, 5},
406 		{0, 0},
407 		{0, 0},
408 		{0, 0}
409 		}
410 	},
411 	{1, 0, 2, 0,		/* 0x31 */
412 		{{0, 0},
413 		{4, 5},
414 		{0, 0},
415 		{0, 0}
416 		}
417 	},
418 	{0, 0, 2, 0,		/* 0x32 */
419 		{{1, 1},
420 		{4, 5},
421 		{0, 0},
422 		{0, 0}
423 		}
424 	},
425 	{1, 0, 2, 0,		/* 0x33 */
426 		{{0, 1},
427 		{4, 5},
428 		{0, 0},
429 		{0, 0}
430 		}
431 	},
432 	{0, 0, 2, 0,		/* 0x34 */
433 		{{2, 2},
434 		{4, 5},
435 		{0, 0},
436 		{0, 0}
437 		}
438 	},
439 	{1, 0, 3, 0,		/* 0x35 */
440 		{{0, 0},
441 		{2, 2},
442 		{4, 5},
443 		{0, 0}
444 		}
445 	},
446 	{0, 0, 2, 0,		/* 0x36 */
447 		{{1, 2},
448 		{4, 5},
449 		{0, 0},
450 		{0, 0}
451 		}
452 	},
453 	{1, 0, 2, 0,		/* 0x37 */
454 		{{0, 2},
455 		{4, 5},
456 		{0, 0},
457 		{0, 0}
458 		}
459 	},
460 	{0, 0, 1, 0,		/* 0x38 */
461 		{{3, 5},
462 		{0, 0},
463 		{0, 0},
464 		{0, 0}
465 		}
466 	},
467 	{1, 0, 2, 0,		/* 0x39 */
468 		{{0, 0},
469 		{3, 5},
470 		{0, 0},
471 		{0, 0}
472 		}
473 	},
474 	{0, 0, 2, 0,		/* 0x3a */
475 		{{1, 1},
476 		{3, 5},
477 		{0, 0},
478 		{0, 0}
479 		}
480 	},
481 	{1, 0, 2, 0,		/* 0x3b */
482 		{{0, 1},
483 		{3, 5},
484 		{0, 0},
485 		{0, 0}
486 		}
487 	},
488 	{0, 0, 1, 0,		/* 0x3c */
489 		{{2, 5},
490 		{0, 0},
491 		{0, 0},
492 		{0, 0}
493 		}
494 	},
495 	{1, 0, 2, 0,		/* 0x3d */
496 		{{0, 0},
497 		{2, 5},
498 		{0, 0},
499 		{0, 0}
500 		}
501 	},
502 	{0, 0, 1, 0,		/* 0x3e */
503 		{{1, 5},
504 		{0, 0},
505 		{0, 0},
506 		{0, 0}
507 		}
508 	},
509 	{1, 0, 1, 0,		/* 0x3f */
510 		{{0, 5},
511 		{0, 0},
512 		{0, 0},
513 		{0, 0}
514 		}
515 	},
516 	{0, 0, 1, 0,		/* 0x40 */
517 		{{6, 6},
518 		{0, 0},
519 		{0, 0},
520 		{0, 0}
521 		}
522 	},
523 	{1, 0, 2, 0,		/* 0x41 */
524 		{{0, 0},
525 		{6, 6},
526 		{0, 0},
527 		{0, 0}
528 		}
529 	},
530 	{0, 0, 2, 0,		/* 0x42 */
531 		{{1, 1},
532 		{6, 6},
533 		{0, 0},
534 		{0, 0}
535 		}
536 	},
537 	{1, 0, 2, 0,		/* 0x43 */
538 		{{0, 1},
539 		{6, 6},
540 		{0, 0},
541 		{0, 0}
542 		}
543 	},
544 	{0, 0, 2, 0,		/* 0x44 */
545 		{{2, 2},
546 		{6, 6},
547 		{0, 0},
548 		{0, 0}
549 		}
550 	},
551 	{1, 0, 3, 0,		/* 0x45 */
552 		{{0, 0},
553 		{2, 2},
554 		{6, 6},
555 		{0, 0}
556 		}
557 	},
558 	{0, 0, 2, 0,		/* 0x46 */
559 		{{1, 2},
560 		{6, 6},
561 		{0, 0},
562 		{0, 0}
563 		}
564 	},
565 	{1, 0, 2, 0,		/* 0x47 */
566 		{{0, 2},
567 		{6, 6},
568 		{0, 0},
569 		{0, 0}
570 		}
571 	},
572 	{0, 0, 2, 0,		/* 0x48 */
573 		{{3, 3},
574 		{6, 6},
575 		{0, 0},
576 		{0, 0}
577 		}
578 	},
579 	{1, 0, 3, 0,		/* 0x49 */
580 		{{0, 0},
581 		{3, 3},
582 		{6, 6},
583 		{0, 0}
584 		}
585 	},
586 	{0, 0, 3, 0,		/* 0x4a */
587 		{{1, 1},
588 		{3, 3},
589 		{6, 6},
590 		{0, 0}
591 		}
592 	},
593 	{1, 0, 3, 0,		/* 0x4b */
594 		{{0, 1},
595 		{3, 3},
596 		{6, 6},
597 		{0, 0}
598 		}
599 	},
600 	{0, 0, 2, 0,		/* 0x4c */
601 		{{2, 3},
602 		{6, 6},
603 		{0, 0},
604 		{0, 0}
605 		}
606 	},
607 	{1, 0, 3, 0,		/* 0x4d */
608 		{{0, 0},
609 		{2, 3},
610 		{6, 6},
611 		{0, 0}
612 		}
613 	},
614 	{0, 0, 2, 0,		/* 0x4e */
615 		{{1, 3},
616 		{6, 6},
617 		{0, 0},
618 		{0, 0}
619 		}
620 	},
621 	{1, 0, 2, 0,		/* 0x4f */
622 		{{0, 3},
623 		{6, 6},
624 		{0, 0},
625 		{0, 0}
626 		}
627 	},
628 	{0, 0, 2, 0,		/* 0x50 */
629 		{{4, 4},
630 		{6, 6},
631 		{0, 0},
632 		{0, 0}
633 		}
634 	},
635 	{1, 0, 3, 0,		/* 0x51 */
636 		{{0, 0},
637 		{4, 4},
638 		{6, 6},
639 		{0, 0}
640 		}
641 	},
642 	{0, 0, 3, 0,		/* 0x52 */
643 		{{1, 1},
644 		{4, 4},
645 		{6, 6},
646 		{0, 0}
647 		}
648 	},
649 	{1, 0, 3, 0,		/* 0x53 */
650 		{{0, 1},
651 		{4, 4},
652 		{6, 6},
653 		{0, 0}
654 		}
655 	},
656 	{0, 0, 3, 0,		/* 0x54 */
657 		{{2, 2},
658 		{4, 4},
659 		{6, 6},
660 		{0, 0}
661 		}
662 	},
663 	{1, 0, 4, 0,		/* 0x55 */
664 		{{0, 0},
665 		{2, 2},
666 		{4, 4},
667 		{6, 6}
668 		}
669 	},
670 	{0, 0, 3, 0,		/* 0x56 */
671 		{{1, 2},
672 		{4, 4},
673 		{6, 6},
674 		{0, 0}
675 		}
676 	},
677 	{1, 0, 3, 0,		/* 0x57 */
678 		{{0, 2},
679 		{4, 4},
680 		{6, 6},
681 		{0, 0}
682 		}
683 	},
684 	{0, 0, 2, 0,		/* 0x58 */
685 		{{3, 4},
686 		{6, 6},
687 		{0, 0},
688 		{0, 0}
689 		}
690 	},
691 	{1, 0, 3, 0,		/* 0x59 */
692 		{{0, 0},
693 		{3, 4},
694 		{6, 6},
695 		{0, 0}
696 		}
697 	},
698 	{0, 0, 3, 0,		/* 0x5a */
699 		{{1, 1},
700 		{3, 4},
701 		{6, 6},
702 		{0, 0}
703 		}
704 	},
705 	{1, 0, 3, 0,		/* 0x5b */
706 		{{0, 1},
707 		{3, 4},
708 		{6, 6},
709 		{0, 0}
710 		}
711 	},
712 	{0, 0, 2, 0,		/* 0x5c */
713 		{{2, 4},
714 		{6, 6},
715 		{0, 0},
716 		{0, 0}
717 		}
718 	},
719 	{1, 0, 3, 0,		/* 0x5d */
720 		{{0, 0},
721 		{2, 4},
722 		{6, 6},
723 		{0, 0}
724 		}
725 	},
726 	{0, 0, 2, 0,		/* 0x5e */
727 		{{1, 4},
728 		{6, 6},
729 		{0, 0},
730 		{0, 0}
731 		}
732 	},
733 	{1, 0, 2, 0,		/* 0x5f */
734 		{{0, 4},
735 		{6, 6},
736 		{0, 0},
737 		{0, 0}
738 		}
739 	},
740 	{0, 0, 1, 0,		/* 0x60 */
741 		{{5, 6},
742 		{0, 0},
743 		{0, 0},
744 		{0, 0}
745 		}
746 	},
747 	{1, 0, 2, 0,		/* 0x61 */
748 		{{0, 0},
749 		{5, 6},
750 		{0, 0},
751 		{0, 0}
752 		}
753 	},
754 	{0, 0, 2, 0,		/* 0x62 */
755 		{{1, 1},
756 		{5, 6},
757 		{0, 0},
758 		{0, 0}
759 		}
760 	},
761 	{1, 0, 2, 0,		/* 0x63 */
762 		{{0, 1},
763 		{5, 6},
764 		{0, 0},
765 		{0, 0}
766 		}
767 	},
768 	{0, 0, 2, 0,		/* 0x64 */
769 		{{2, 2},
770 		{5, 6},
771 		{0, 0},
772 		{0, 0}
773 		}
774 	},
775 	{1, 0, 3, 0,		/* 0x65 */
776 		{{0, 0},
777 		{2, 2},
778 		{5, 6},
779 		{0, 0}
780 		}
781 	},
782 	{0, 0, 2, 0,		/* 0x66 */
783 		{{1, 2},
784 		{5, 6},
785 		{0, 0},
786 		{0, 0}
787 		}
788 	},
789 	{1, 0, 2, 0,		/* 0x67 */
790 		{{0, 2},
791 		{5, 6},
792 		{0, 0},
793 		{0, 0}
794 		}
795 	},
796 	{0, 0, 2, 0,		/* 0x68 */
797 		{{3, 3},
798 		{5, 6},
799 		{0, 0},
800 		{0, 0}
801 		}
802 	},
803 	{1, 0, 3, 0,		/* 0x69 */
804 		{{0, 0},
805 		{3, 3},
806 		{5, 6},
807 		{0, 0}
808 		}
809 	},
810 	{0, 0, 3, 0,		/* 0x6a */
811 		{{1, 1},
812 		{3, 3},
813 		{5, 6},
814 		{0, 0}
815 		}
816 	},
817 	{1, 0, 3, 0,		/* 0x6b */
818 		{{0, 1},
819 		{3, 3},
820 		{5, 6},
821 		{0, 0}
822 		}
823 	},
824 	{0, 0, 2, 0,		/* 0x6c */
825 		{{2, 3},
826 		{5, 6},
827 		{0, 0},
828 		{0, 0}
829 		}
830 	},
831 	{1, 0, 3, 0,		/* 0x6d */
832 		{{0, 0},
833 		{2, 3},
834 		{5, 6},
835 		{0, 0}
836 		}
837 	},
838 	{0, 0, 2, 0,		/* 0x6e */
839 		{{1, 3},
840 		{5, 6},
841 		{0, 0},
842 		{0, 0}
843 		}
844 	},
845 	{1, 0, 2, 0,		/* 0x6f */
846 		{{0, 3},
847 		{5, 6},
848 		{0, 0},
849 		{0, 0}
850 		}
851 	},
852 	{0, 0, 1, 0,		/* 0x70 */
853 		{{4, 6},
854 		{0, 0},
855 		{0, 0},
856 		{0, 0}
857 		}
858 	},
859 	{1, 0, 2, 0,		/* 0x71 */
860 		{{0, 0},
861 		{4, 6},
862 		{0, 0},
863 		{0, 0}
864 		}
865 	},
866 	{0, 0, 2, 0,		/* 0x72 */
867 		{{1, 1},
868 		{4, 6},
869 		{0, 0},
870 		{0, 0}
871 		}
872 	},
873 	{1, 0, 2, 0,		/* 0x73 */
874 		{{0, 1},
875 		{4, 6},
876 		{0, 0},
877 		{0, 0}
878 		}
879 	},
880 	{0, 0, 2, 0,		/* 0x74 */
881 		{{2, 2},
882 		{4, 6},
883 		{0, 0},
884 		{0, 0}
885 		}
886 	},
887 	{1, 0, 3, 0,		/* 0x75 */
888 		{{0, 0},
889 		{2, 2},
890 		{4, 6},
891 		{0, 0}
892 		}
893 	},
894 	{0, 0, 2, 0,		/* 0x76 */
895 		{{1, 2},
896 		{4, 6},
897 		{0, 0},
898 		{0, 0}
899 		}
900 	},
901 	{1, 0, 2, 0,		/* 0x77 */
902 		{{0, 2},
903 		{4, 6},
904 		{0, 0},
905 		{0, 0}
906 		}
907 	},
908 	{0, 0, 1, 0,		/* 0x78 */
909 		{{3, 6},
910 		{0, 0},
911 		{0, 0},
912 		{0, 0}
913 		}
914 	},
915 	{1, 0, 2, 0,		/* 0x79 */
916 		{{0, 0},
917 		{3, 6},
918 		{0, 0},
919 		{0, 0}
920 		}
921 	},
922 	{0, 0, 2, 0,		/* 0x7a */
923 		{{1, 1},
924 		{3, 6},
925 		{0, 0},
926 		{0, 0}
927 		}
928 	},
929 	{1, 0, 2, 0,		/* 0x7b */
930 		{{0, 1},
931 		{3, 6},
932 		{0, 0},
933 		{0, 0}
934 		}
935 	},
936 	{0, 0, 1, 0,		/* 0x7c */
937 		{{2, 6},
938 		{0, 0},
939 		{0, 0},
940 		{0, 0}
941 		}
942 	},
943 	{1, 0, 2, 0,		/* 0x7d */
944 		{{0, 0},
945 		{2, 6},
946 		{0, 0},
947 		{0, 0}
948 		}
949 	},
950 	{0, 0, 1, 0,		/* 0x7e */
951 		{{1, 6},
952 		{0, 0},
953 		{0, 0},
954 		{0, 0}
955 		}
956 	},
957 	{1, 0, 1, 0,		/* 0x7f */
958 		{{0, 6},
959 		{0, 0},
960 		{0, 0},
961 		{0, 0}
962 		}
963 	},
964 	{0, 1, 1, 0,		/* 0x80 */
965 		{{7, 7},
966 		{0, 0},
967 		{0, 0},
968 		{0, 0}
969 		}
970 	},
971 	{1, 1, 2, 0,		/* 0x81 */
972 		{{0, 0},
973 		{7, 7},
974 		{0, 0},
975 		{0, 0}
976 		}
977 	},
978 	{0, 1, 2, 0,		/* 0x82 */
979 		{{1, 1},
980 		{7, 7},
981 		{0, 0},
982 		{0, 0}
983 		}
984 	},
985 	{1, 1, 2, 0,		/* 0x83 */
986 		{{0, 1},
987 		{7, 7},
988 		{0, 0},
989 		{0, 0}
990 		}
991 	},
992 	{0, 1, 2, 0,		/* 0x84 */
993 		{{2, 2},
994 		{7, 7},
995 		{0, 0},
996 		{0, 0}
997 		}
998 	},
999 	{1, 1, 3, 0,		/* 0x85 */
1000 		{{0, 0},
1001 		{2, 2},
1002 		{7, 7},
1003 		{0, 0}
1004 		}
1005 	},
1006 	{0, 1, 2, 0,		/* 0x86 */
1007 		{{1, 2},
1008 		{7, 7},
1009 		{0, 0},
1010 		{0, 0}
1011 		}
1012 	},
1013 	{1, 1, 2, 0,		/* 0x87 */
1014 		{{0, 2},
1015 		{7, 7},
1016 		{0, 0},
1017 		{0, 0}
1018 		}
1019 	},
1020 	{0, 1, 2, 0,		/* 0x88 */
1021 		{{3, 3},
1022 		{7, 7},
1023 		{0, 0},
1024 		{0, 0}
1025 		}
1026 	},
1027 	{1, 1, 3, 0,		/* 0x89 */
1028 		{{0, 0},
1029 		{3, 3},
1030 		{7, 7},
1031 		{0, 0}
1032 		}
1033 	},
1034 	{0, 1, 3, 0,		/* 0x8a */
1035 		{{1, 1},
1036 		{3, 3},
1037 		{7, 7},
1038 		{0, 0}
1039 		}
1040 	},
1041 	{1, 1, 3, 0,		/* 0x8b */
1042 		{{0, 1},
1043 		{3, 3},
1044 		{7, 7},
1045 		{0, 0}
1046 		}
1047 	},
1048 	{0, 1, 2, 0,		/* 0x8c */
1049 		{{2, 3},
1050 		{7, 7},
1051 		{0, 0},
1052 		{0, 0}
1053 		}
1054 	},
1055 	{1, 1, 3, 0,		/* 0x8d */
1056 		{{0, 0},
1057 		{2, 3},
1058 		{7, 7},
1059 		{0, 0}
1060 		}
1061 	},
1062 	{0, 1, 2, 0,		/* 0x8e */
1063 		{{1, 3},
1064 		{7, 7},
1065 		{0, 0},
1066 		{0, 0}
1067 		}
1068 	},
1069 	{1, 1, 2, 0,		/* 0x8f */
1070 		{{0, 3},
1071 		{7, 7},
1072 		{0, 0},
1073 		{0, 0}
1074 		}
1075 	},
1076 	{0, 1, 2, 0,		/* 0x90 */
1077 		{{4, 4},
1078 		{7, 7},
1079 		{0, 0},
1080 		{0, 0}
1081 		}
1082 	},
1083 	{1, 1, 3, 0,		/* 0x91 */
1084 		{{0, 0},
1085 		{4, 4},
1086 		{7, 7},
1087 		{0, 0}
1088 		}
1089 	},
1090 	{0, 1, 3, 0,		/* 0x92 */
1091 		{{1, 1},
1092 		{4, 4},
1093 		{7, 7},
1094 		{0, 0}
1095 		}
1096 	},
1097 	{1, 1, 3, 0,		/* 0x93 */
1098 		{{0, 1},
1099 		{4, 4},
1100 		{7, 7},
1101 		{0, 0}
1102 		}
1103 	},
1104 	{0, 1, 3, 0,		/* 0x94 */
1105 		{{2, 2},
1106 		{4, 4},
1107 		{7, 7},
1108 		{0, 0}
1109 		}
1110 	},
1111 	{1, 1, 4, 0,		/* 0x95 */
1112 		{{0, 0},
1113 		{2, 2},
1114 		{4, 4},
1115 		{7, 7}
1116 		}
1117 	},
1118 	{0, 1, 3, 0,		/* 0x96 */
1119 		{{1, 2},
1120 		{4, 4},
1121 		{7, 7},
1122 		{0, 0}
1123 		}
1124 	},
1125 	{1, 1, 3, 0,		/* 0x97 */
1126 		{{0, 2},
1127 		{4, 4},
1128 		{7, 7},
1129 		{0, 0}
1130 		}
1131 	},
1132 	{0, 1, 2, 0,		/* 0x98 */
1133 		{{3, 4},
1134 		{7, 7},
1135 		{0, 0},
1136 		{0, 0}
1137 		}
1138 	},
1139 	{1, 1, 3, 0,		/* 0x99 */
1140 		{{0, 0},
1141 		{3, 4},
1142 		{7, 7},
1143 		{0, 0}
1144 		}
1145 	},
1146 	{0, 1, 3, 0,		/* 0x9a */
1147 		{{1, 1},
1148 		{3, 4},
1149 		{7, 7},
1150 		{0, 0}
1151 		}
1152 	},
1153 	{1, 1, 3, 0,		/* 0x9b */
1154 		{{0, 1},
1155 		{3, 4},
1156 		{7, 7},
1157 		{0, 0}
1158 		}
1159 	},
1160 	{0, 1, 2, 0,		/* 0x9c */
1161 		{{2, 4},
1162 		{7, 7},
1163 		{0, 0},
1164 		{0, 0}
1165 		}
1166 	},
1167 	{1, 1, 3, 0,		/* 0x9d */
1168 		{{0, 0},
1169 		{2, 4},
1170 		{7, 7},
1171 		{0, 0}
1172 		}
1173 	},
1174 	{0, 1, 2, 0,		/* 0x9e */
1175 		{{1, 4},
1176 		{7, 7},
1177 		{0, 0},
1178 		{0, 0}
1179 		}
1180 	},
1181 	{1, 1, 2, 0,		/* 0x9f */
1182 		{{0, 4},
1183 		{7, 7},
1184 		{0, 0},
1185 		{0, 0}
1186 		}
1187 	},
1188 	{0, 1, 2, 0,		/* 0xa0 */
1189 		{{5, 5},
1190 		{7, 7},
1191 		{0, 0},
1192 		{0, 0}
1193 		}
1194 	},
1195 	{1, 1, 3, 0,		/* 0xa1 */
1196 		{{0, 0},
1197 		{5, 5},
1198 		{7, 7},
1199 		{0, 0}
1200 		}
1201 	},
1202 	{0, 1, 3, 0,		/* 0xa2 */
1203 		{{1, 1},
1204 		{5, 5},
1205 		{7, 7},
1206 		{0, 0}
1207 		}
1208 	},
1209 	{1, 1, 3, 0,		/* 0xa3 */
1210 		{{0, 1},
1211 		{5, 5},
1212 		{7, 7},
1213 		{0, 0}
1214 		}
1215 	},
1216 	{0, 1, 3, 0,		/* 0xa4 */
1217 		{{2, 2},
1218 		{5, 5},
1219 		{7, 7},
1220 		{0, 0}
1221 		}
1222 	},
1223 	{1, 1, 4, 0,		/* 0xa5 */
1224 		{{0, 0},
1225 		{2, 2},
1226 		{5, 5},
1227 		{7, 7}
1228 		}
1229 	},
1230 	{0, 1, 3, 0,		/* 0xa6 */
1231 		{{1, 2},
1232 		{5, 5},
1233 		{7, 7},
1234 		{0, 0}
1235 		}
1236 	},
1237 	{1, 1, 3, 0,		/* 0xa7 */
1238 		{{0, 2},
1239 		{5, 5},
1240 		{7, 7},
1241 		{0, 0}
1242 		}
1243 	},
1244 	{0, 1, 3, 0,		/* 0xa8 */
1245 		{{3, 3},
1246 		{5, 5},
1247 		{7, 7},
1248 		{0, 0}
1249 		}
1250 	},
1251 	{1, 1, 4, 0,		/* 0xa9 */
1252 		{{0, 0},
1253 		{3, 3},
1254 		{5, 5},
1255 		{7, 7}
1256 		}
1257 	},
1258 	{0, 1, 4, 0,		/* 0xaa */
1259 		{{1, 1},
1260 		{3, 3},
1261 		{5, 5},
1262 		{7, 7}
1263 		}
1264 	},
1265 	{1, 1, 4, 0,		/* 0xab */
1266 		{{0, 1},
1267 		{3, 3},
1268 		{5, 5},
1269 		{7, 7}
1270 		}
1271 	},
1272 	{0, 1, 3, 0,		/* 0xac */
1273 		{{2, 3},
1274 		{5, 5},
1275 		{7, 7},
1276 		{0, 0}
1277 		}
1278 	},
1279 	{1, 1, 4, 0,		/* 0xad */
1280 		{{0, 0},
1281 		{2, 3},
1282 		{5, 5},
1283 		{7, 7}
1284 		}
1285 	},
1286 	{0, 1, 3, 0,		/* 0xae */
1287 		{{1, 3},
1288 		{5, 5},
1289 		{7, 7},
1290 		{0, 0}
1291 		}
1292 	},
1293 	{1, 1, 3, 0,		/* 0xaf */
1294 		{{0, 3},
1295 		{5, 5},
1296 		{7, 7},
1297 		{0, 0}
1298 		}
1299 	},
1300 	{0, 1, 2, 0,		/* 0xb0 */
1301 		{{4, 5},
1302 		{7, 7},
1303 		{0, 0},
1304 		{0, 0}
1305 		}
1306 	},
1307 	{1, 1, 3, 0,		/* 0xb1 */
1308 		{{0, 0},
1309 		{4, 5},
1310 		{7, 7},
1311 		{0, 0}
1312 		}
1313 	},
1314 	{0, 1, 3, 0,		/* 0xb2 */
1315 		{{1, 1},
1316 		{4, 5},
1317 		{7, 7},
1318 		{0, 0}
1319 		}
1320 	},
1321 	{1, 1, 3, 0,		/* 0xb3 */
1322 		{{0, 1},
1323 		{4, 5},
1324 		{7, 7},
1325 		{0, 0}
1326 		}
1327 	},
1328 	{0, 1, 3, 0,		/* 0xb4 */
1329 		{{2, 2},
1330 		{4, 5},
1331 		{7, 7},
1332 		{0, 0}
1333 		}
1334 	},
1335 	{1, 1, 4, 0,		/* 0xb5 */
1336 		{{0, 0},
1337 		{2, 2},
1338 		{4, 5},
1339 		{7, 7}
1340 		}
1341 	},
1342 	{0, 1, 3, 0,		/* 0xb6 */
1343 		{{1, 2},
1344 		{4, 5},
1345 		{7, 7},
1346 		{0, 0}
1347 		}
1348 	},
1349 	{1, 1, 3, 0,		/* 0xb7 */
1350 		{{0, 2},
1351 		{4, 5},
1352 		{7, 7},
1353 		{0, 0}
1354 		}
1355 	},
1356 	{0, 1, 2, 0,		/* 0xb8 */
1357 		{{3, 5},
1358 		{7, 7},
1359 		{0, 0},
1360 		{0, 0}
1361 		}
1362 	},
1363 	{1, 1, 3, 0,		/* 0xb9 */
1364 		{{0, 0},
1365 		{3, 5},
1366 		{7, 7},
1367 		{0, 0}
1368 		}
1369 	},
1370 	{0, 1, 3, 0,		/* 0xba */
1371 		{{1, 1},
1372 		{3, 5},
1373 		{7, 7},
1374 		{0, 0}
1375 		}
1376 	},
1377 	{1, 1, 3, 0,		/* 0xbb */
1378 		{{0, 1},
1379 		{3, 5},
1380 		{7, 7},
1381 		{0, 0}
1382 		}
1383 	},
1384 	{0, 1, 2, 0,		/* 0xbc */
1385 		{{2, 5},
1386 		{7, 7},
1387 		{0, 0},
1388 		{0, 0}
1389 		}
1390 	},
1391 	{1, 1, 3, 0,		/* 0xbd */
1392 		{{0, 0},
1393 		{2, 5},
1394 		{7, 7},
1395 		{0, 0}
1396 		}
1397 	},
1398 	{0, 1, 2, 0,		/* 0xbe */
1399 		{{1, 5},
1400 		{7, 7},
1401 		{0, 0},
1402 		{0, 0}
1403 		}
1404 	},
1405 	{1, 1, 2, 0,		/* 0xbf */
1406 		{{0, 5},
1407 		{7, 7},
1408 		{0, 0},
1409 		{0, 0}
1410 		}
1411 	},
1412 	{0, 1, 1, 0,		/* 0xc0 */
1413 		{{6, 7},
1414 		{0, 0},
1415 		{0, 0},
1416 		{0, 0}
1417 		}
1418 	},
1419 	{1, 1, 2, 0,		/* 0xc1 */
1420 		{{0, 0},
1421 		{6, 7},
1422 		{0, 0},
1423 		{0, 0}
1424 		}
1425 	},
1426 	{0, 1, 2, 0,		/* 0xc2 */
1427 		{{1, 1},
1428 		{6, 7},
1429 		{0, 0},
1430 		{0, 0}
1431 		}
1432 	},
1433 	{1, 1, 2, 0,		/* 0xc3 */
1434 		{{0, 1},
1435 		{6, 7},
1436 		{0, 0},
1437 		{0, 0}
1438 		}
1439 	},
1440 	{0, 1, 2, 0,		/* 0xc4 */
1441 		{{2, 2},
1442 		{6, 7},
1443 		{0, 0},
1444 		{0, 0}
1445 		}
1446 	},
1447 	{1, 1, 3, 0,		/* 0xc5 */
1448 		{{0, 0},
1449 		{2, 2},
1450 		{6, 7},
1451 		{0, 0}
1452 		}
1453 	},
1454 	{0, 1, 2, 0,		/* 0xc6 */
1455 		{{1, 2},
1456 		{6, 7},
1457 		{0, 0},
1458 		{0, 0}
1459 		}
1460 	},
1461 	{1, 1, 2, 0,		/* 0xc7 */
1462 		{{0, 2},
1463 		{6, 7},
1464 		{0, 0},
1465 		{0, 0}
1466 		}
1467 	},
1468 	{0, 1, 2, 0,		/* 0xc8 */
1469 		{{3, 3},
1470 		{6, 7},
1471 		{0, 0},
1472 		{0, 0}
1473 		}
1474 	},
1475 	{1, 1, 3, 0,		/* 0xc9 */
1476 		{{0, 0},
1477 		{3, 3},
1478 		{6, 7},
1479 		{0, 0}
1480 		}
1481 	},
1482 	{0, 1, 3, 0,		/* 0xca */
1483 		{{1, 1},
1484 		{3, 3},
1485 		{6, 7},
1486 		{0, 0}
1487 		}
1488 	},
1489 	{1, 1, 3, 0,		/* 0xcb */
1490 		{{0, 1},
1491 		{3, 3},
1492 		{6, 7},
1493 		{0, 0}
1494 		}
1495 	},
1496 	{0, 1, 2, 0,		/* 0xcc */
1497 		{{2, 3},
1498 		{6, 7},
1499 		{0, 0},
1500 		{0, 0}
1501 		}
1502 	},
1503 	{1, 1, 3, 0,		/* 0xcd */
1504 		{{0, 0},
1505 		{2, 3},
1506 		{6, 7},
1507 		{0, 0}
1508 		}
1509 	},
1510 	{0, 1, 2, 0,		/* 0xce */
1511 		{{1, 3},
1512 		{6, 7},
1513 		{0, 0},
1514 		{0, 0}
1515 		}
1516 	},
1517 	{1, 1, 2, 0,		/* 0xcf */
1518 		{{0, 3},
1519 		{6, 7},
1520 		{0, 0},
1521 		{0, 0}
1522 		}
1523 	},
1524 	{0, 1, 2, 0,		/* 0xd0 */
1525 		{{4, 4},
1526 		{6, 7},
1527 		{0, 0},
1528 		{0, 0}
1529 		}
1530 	},
1531 	{1, 1, 3, 0,		/* 0xd1 */
1532 		{{0, 0},
1533 		{4, 4},
1534 		{6, 7},
1535 		{0, 0}
1536 		}
1537 	},
1538 	{0, 1, 3, 0,		/* 0xd2 */
1539 		{{1, 1},
1540 		{4, 4},
1541 		{6, 7},
1542 		{0, 0}
1543 		}
1544 	},
1545 	{1, 1, 3, 0,		/* 0xd3 */
1546 		{{0, 1},
1547 		{4, 4},
1548 		{6, 7},
1549 		{0, 0}
1550 		}
1551 	},
1552 	{0, 1, 3, 0,		/* 0xd4 */
1553 		{{2, 2},
1554 		{4, 4},
1555 		{6, 7},
1556 		{0, 0}
1557 		}
1558 	},
1559 	{1, 1, 4, 0,		/* 0xd5 */
1560 		{{0, 0},
1561 		{2, 2},
1562 		{4, 4},
1563 		{6, 7}
1564 		}
1565 	},
1566 	{0, 1, 3, 0,		/* 0xd6 */
1567 		{{1, 2},
1568 		{4, 4},
1569 		{6, 7},
1570 		{0, 0}
1571 		}
1572 	},
1573 	{1, 1, 3, 0,		/* 0xd7 */
1574 		{{0, 2},
1575 		{4, 4},
1576 		{6, 7},
1577 		{0, 0}
1578 		}
1579 	},
1580 	{0, 1, 2, 0,		/* 0xd8 */
1581 		{{3, 4},
1582 		{6, 7},
1583 		{0, 0},
1584 		{0, 0}
1585 		}
1586 	},
1587 	{1, 1, 3, 0,		/* 0xd9 */
1588 		{{0, 0},
1589 		{3, 4},
1590 		{6, 7},
1591 		{0, 0}
1592 		}
1593 	},
1594 	{0, 1, 3, 0,		/* 0xda */
1595 		{{1, 1},
1596 		{3, 4},
1597 		{6, 7},
1598 		{0, 0}
1599 		}
1600 	},
1601 	{1, 1, 3, 0,		/* 0xdb */
1602 		{{0, 1},
1603 		{3, 4},
1604 		{6, 7},
1605 		{0, 0}
1606 		}
1607 	},
1608 	{0, 1, 2, 0,		/* 0xdc */
1609 		{{2, 4},
1610 		{6, 7},
1611 		{0, 0},
1612 		{0, 0}
1613 		}
1614 	},
1615 	{1, 1, 3, 0,		/* 0xdd */
1616 		{{0, 0},
1617 		{2, 4},
1618 		{6, 7},
1619 		{0, 0}
1620 		}
1621 	},
1622 	{0, 1, 2, 0,		/* 0xde */
1623 		{{1, 4},
1624 		{6, 7},
1625 		{0, 0},
1626 		{0, 0}
1627 		}
1628 	},
1629 	{1, 1, 2, 0,		/* 0xdf */
1630 		{{0, 4},
1631 		{6, 7},
1632 		{0, 0},
1633 		{0, 0}
1634 		}
1635 	},
1636 	{0, 1, 1, 0,		/* 0xe0 */
1637 		{{5, 7},
1638 		{0, 0},
1639 		{0, 0},
1640 		{0, 0}
1641 		}
1642 	},
1643 	{1, 1, 2, 0,		/* 0xe1 */
1644 		{{0, 0},
1645 		{5, 7},
1646 		{0, 0},
1647 		{0, 0}
1648 		}
1649 	},
1650 	{0, 1, 2, 0,		/* 0xe2 */
1651 		{{1, 1},
1652 		{5, 7},
1653 		{0, 0},
1654 		{0, 0}
1655 		}
1656 	},
1657 	{1, 1, 2, 0,		/* 0xe3 */
1658 		{{0, 1},
1659 		{5, 7},
1660 		{0, 0},
1661 		{0, 0}
1662 		}
1663 	},
1664 	{0, 1, 2, 0,		/* 0xe4 */
1665 		{{2, 2},
1666 		{5, 7},
1667 		{0, 0},
1668 		{0, 0}
1669 		}
1670 	},
1671 	{1, 1, 3, 0,		/* 0xe5 */
1672 		{{0, 0},
1673 		{2, 2},
1674 		{5, 7},
1675 		{0, 0}
1676 		}
1677 	},
1678 	{0, 1, 2, 0,		/* 0xe6 */
1679 		{{1, 2},
1680 		{5, 7},
1681 		{0, 0},
1682 		{0, 0}
1683 		}
1684 	},
1685 	{1, 1, 2, 0,		/* 0xe7 */
1686 		{{0, 2},
1687 		{5, 7},
1688 		{0, 0},
1689 		{0, 0}
1690 		}
1691 	},
1692 	{0, 1, 2, 0,		/* 0xe8 */
1693 		{{3, 3},
1694 		{5, 7},
1695 		{0, 0},
1696 		{0, 0}
1697 		}
1698 	},
1699 	{1, 1, 3, 0,		/* 0xe9 */
1700 		{{0, 0},
1701 		{3, 3},
1702 		{5, 7},
1703 		{0, 0}
1704 		}
1705 	},
1706 	{0, 1, 3, 0,		/* 0xea */
1707 		{{1, 1},
1708 		{3, 3},
1709 		{5, 7},
1710 		{0, 0}
1711 		}
1712 	},
1713 	{1, 1, 3, 0,		/* 0xeb */
1714 		{{0, 1},
1715 		{3, 3},
1716 		{5, 7},
1717 		{0, 0}
1718 		}
1719 	},
1720 	{0, 1, 2, 0,		/* 0xec */
1721 		{{2, 3},
1722 		{5, 7},
1723 		{0, 0},
1724 		{0, 0}
1725 		}
1726 	},
1727 	{1, 1, 3, 0,		/* 0xed */
1728 		{{0, 0},
1729 		{2, 3},
1730 		{5, 7},
1731 		{0, 0}
1732 		}
1733 	},
1734 	{0, 1, 2, 0,		/* 0xee */
1735 		{{1, 3},
1736 		{5, 7},
1737 		{0, 0},
1738 		{0, 0}
1739 		}
1740 	},
1741 	{1, 1, 2, 0,		/* 0xef */
1742 		{{0, 3},
1743 		{5, 7},
1744 		{0, 0},
1745 		{0, 0}
1746 		}
1747 	},
1748 	{0, 1, 1, 0,		/* 0xf0 */
1749 		{{4, 7},
1750 		{0, 0},
1751 		{0, 0},
1752 		{0, 0}
1753 		}
1754 	},
1755 	{1, 1, 2, 0,		/* 0xf1 */
1756 		{{0, 0},
1757 		{4, 7},
1758 		{0, 0},
1759 		{0, 0}
1760 		}
1761 	},
1762 	{0, 1, 2, 0,		/* 0xf2 */
1763 		{{1, 1},
1764 		{4, 7},
1765 		{0, 0},
1766 		{0, 0}
1767 		}
1768 	},
1769 	{1, 1, 2, 0,		/* 0xf3 */
1770 		{{0, 1},
1771 		{4, 7},
1772 		{0, 0},
1773 		{0, 0}
1774 		}
1775 	},
1776 	{0, 1, 2, 0,		/* 0xf4 */
1777 		{{2, 2},
1778 		{4, 7},
1779 		{0, 0},
1780 		{0, 0}
1781 		}
1782 	},
1783 	{1, 1, 3, 0,		/* 0xf5 */
1784 		{{0, 0},
1785 		{2, 2},
1786 		{4, 7},
1787 		{0, 0}
1788 		}
1789 	},
1790 	{0, 1, 2, 0,		/* 0xf6 */
1791 		{{1, 2},
1792 		{4, 7},
1793 		{0, 0},
1794 		{0, 0}
1795 		}
1796 	},
1797 	{1, 1, 2, 0,		/* 0xf7 */
1798 		{{0, 2},
1799 		{4, 7},
1800 		{0, 0},
1801 		{0, 0}
1802 		}
1803 	},
1804 	{0, 1, 1, 0,		/* 0xf8 */
1805 		{{3, 7},
1806 		{0, 0},
1807 		{0, 0},
1808 		{0, 0}
1809 		}
1810 	},
1811 	{1, 1, 2, 0,		/* 0xf9 */
1812 		{{0, 0},
1813 		{3, 7},
1814 		{0, 0},
1815 		{0, 0}
1816 		}
1817 	},
1818 	{0, 1, 2, 0,		/* 0xfa */
1819 		{{1, 1},
1820 		{3, 7},
1821 		{0, 0},
1822 		{0, 0}
1823 		}
1824 	},
1825 	{1, 1, 2, 0,		/* 0xfb */
1826 		{{0, 1},
1827 		{3, 7},
1828 		{0, 0},
1829 		{0, 0}
1830 		}
1831 	},
1832 	{0, 1, 1, 0,		/* 0xfc */
1833 		{{2, 7},
1834 		{0, 0},
1835 		{0, 0},
1836 		{0, 0}
1837 		}
1838 	},
1839 	{1, 1, 2, 0,		/* 0xfd */
1840 		{{0, 0},
1841 		{2, 7},
1842 		{0, 0},
1843 		{0, 0}
1844 		}
1845 	},
1846 	{0, 1, 1, 0,		/* 0xfe */
1847 		{{1, 7},
1848 		{0, 0},
1849 		{0, 0},
1850 		{0, 0}
1851 		}
1852 	},
1853 	{1, 1, 1, 0,		/* 0xff */
1854 		{{0, 7},
1855 		{0, 0},
1856 		{0, 0},
1857 		{0, 0}
1858 		}
1859 	}
1860 };
1861 
1862 
1863 int
1864 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1865     int ipv4_addr_legal,
1866     int ipv6_addr_legal,
1867     int loopback_scope,
1868     int ipv4_local_scope,
1869     int local_scope,
1870     int site_scope,
1871     int do_update)
1872 {
1873 	if ((loopback_scope == 0) &&
1874 	    (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1875 		/*
1876 		 * skip loopback if not in scope *
1877 		 */
1878 		return (0);
1879 	}
1880 	switch (ifa->address.sa.sa_family) {
1881 	case AF_INET:
1882 		if (ipv4_addr_legal) {
1883 			struct sockaddr_in *sin;
1884 
1885 			sin = (struct sockaddr_in *)&ifa->address.sin;
1886 			if (sin->sin_addr.s_addr == 0) {
1887 				/* not in scope , unspecified */
1888 				return (0);
1889 			}
1890 			if ((ipv4_local_scope == 0) &&
1891 			    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1892 				/* private address not in scope */
1893 				return (0);
1894 			}
1895 		} else {
1896 			return (0);
1897 		}
1898 		break;
1899 #ifdef INET6
1900 	case AF_INET6:
1901 		if (ipv6_addr_legal) {
1902 			struct sockaddr_in6 *sin6;
1903 
1904 			/*
1905 			 * Must update the flags,  bummer, which means any
1906 			 * IFA locks must now be applied HERE <->
1907 			 */
1908 			if (do_update) {
1909 				sctp_gather_internal_ifa_flags(ifa);
1910 			}
1911 			if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1912 				return (0);
1913 			}
1914 			/* ok to use deprecated addresses? */
1915 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1916 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1917 				/* skip unspecifed addresses */
1918 				return (0);
1919 			}
1920 			if (	/* (local_scope == 0) && */
1921 			    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1922 				return (0);
1923 			}
1924 			if ((site_scope == 0) &&
1925 			    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1926 				return (0);
1927 			}
1928 		} else {
1929 			return (0);
1930 		}
1931 		break;
1932 #endif
1933 	default:
1934 		return (0);
1935 	}
1936 	return (1);
1937 }
1938 
1939 static struct mbuf *
1940 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1941 {
1942 	struct sctp_paramhdr *parmh;
1943 	struct mbuf *mret;
1944 	int len;
1945 
1946 	if (ifa->address.sa.sa_family == AF_INET) {
1947 		len = sizeof(struct sctp_ipv4addr_param);
1948 	} else if (ifa->address.sa.sa_family == AF_INET6) {
1949 		len = sizeof(struct sctp_ipv6addr_param);
1950 	} else {
1951 		/* unknown type */
1952 		return (m);
1953 	}
1954 	if (M_TRAILINGSPACE(m) >= len) {
1955 		/* easy side we just drop it on the end */
1956 		parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1957 		mret = m;
1958 	} else {
1959 		/* Need more space */
1960 		mret = m;
1961 		while (SCTP_BUF_NEXT(mret) != NULL) {
1962 			mret = SCTP_BUF_NEXT(mret);
1963 		}
1964 		SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1965 		if (SCTP_BUF_NEXT(mret) == NULL) {
1966 			/* We are hosed, can't add more addresses */
1967 			return (m);
1968 		}
1969 		mret = SCTP_BUF_NEXT(mret);
1970 		parmh = mtod(mret, struct sctp_paramhdr *);
1971 	}
1972 	/* now add the parameter */
1973 	switch (ifa->address.sa.sa_family) {
1974 	case AF_INET:
1975 		{
1976 			struct sctp_ipv4addr_param *ipv4p;
1977 			struct sockaddr_in *sin;
1978 
1979 			sin = (struct sockaddr_in *)&ifa->address.sin;
1980 			ipv4p = (struct sctp_ipv4addr_param *)parmh;
1981 			parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1982 			parmh->param_length = htons(len);
1983 			ipv4p->addr = sin->sin_addr.s_addr;
1984 			SCTP_BUF_LEN(mret) += len;
1985 			break;
1986 		}
1987 #ifdef INET6
1988 	case AF_INET6:
1989 		{
1990 			struct sctp_ipv6addr_param *ipv6p;
1991 			struct sockaddr_in6 *sin6;
1992 
1993 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1994 			ipv6p = (struct sctp_ipv6addr_param *)parmh;
1995 			parmh->param_type = htons(SCTP_IPV6_ADDRESS);
1996 			parmh->param_length = htons(len);
1997 			memcpy(ipv6p->addr, &sin6->sin6_addr,
1998 			    sizeof(ipv6p->addr));
1999 			/* clear embedded scope in the address */
2000 			in6_clearscope((struct in6_addr *)ipv6p->addr);
2001 			SCTP_BUF_LEN(mret) += len;
2002 			break;
2003 		}
2004 #endif
2005 	default:
2006 		return (m);
2007 	}
2008 	return (mret);
2009 }
2010 
2011 
2012 struct mbuf *
2013 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_scoping *scope,
2014     struct mbuf *m_at, int cnt_inits_to)
2015 {
2016 	struct sctp_vrf *vrf = NULL;
2017 	int cnt, limit_out = 0, total_count;
2018 	uint32_t vrf_id;
2019 
2020 	vrf_id = inp->def_vrf_id;
2021 	SCTP_IPI_ADDR_RLOCK();
2022 	vrf = sctp_find_vrf(vrf_id);
2023 	if (vrf == NULL) {
2024 		SCTP_IPI_ADDR_RUNLOCK();
2025 		return (m_at);
2026 	}
2027 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2028 		struct sctp_ifa *sctp_ifap;
2029 		struct sctp_ifn *sctp_ifnp;
2030 
2031 		cnt = cnt_inits_to;
2032 		if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2033 			limit_out = 1;
2034 			cnt = SCTP_ADDRESS_LIMIT;
2035 			goto skip_count;
2036 		}
2037 		LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2038 			if ((scope->loopback_scope == 0) &&
2039 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2040 				/*
2041 				 * Skip loopback devices if loopback_scope
2042 				 * not set
2043 				 */
2044 				continue;
2045 			}
2046 			LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2047 				if (sctp_is_address_in_scope(sctp_ifap,
2048 				    scope->ipv4_addr_legal,
2049 				    scope->ipv6_addr_legal,
2050 				    scope->loopback_scope,
2051 				    scope->ipv4_local_scope,
2052 				    scope->local_scope,
2053 				    scope->site_scope, 1) == 0) {
2054 					continue;
2055 				}
2056 				cnt++;
2057 				if (cnt > SCTP_ADDRESS_LIMIT) {
2058 					break;
2059 				}
2060 			}
2061 			if (cnt > SCTP_ADDRESS_LIMIT) {
2062 				break;
2063 			}
2064 		}
2065 skip_count:
2066 		if (cnt > 1) {
2067 			total_count = 0;
2068 			LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2069 				cnt = 0;
2070 				if ((scope->loopback_scope == 0) &&
2071 				    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2072 					/*
2073 					 * Skip loopback devices if
2074 					 * loopback_scope not set
2075 					 */
2076 					continue;
2077 				}
2078 				LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2079 					if (sctp_is_address_in_scope(sctp_ifap,
2080 					    scope->ipv4_addr_legal,
2081 					    scope->ipv6_addr_legal,
2082 					    scope->loopback_scope,
2083 					    scope->ipv4_local_scope,
2084 					    scope->local_scope,
2085 					    scope->site_scope, 0) == 0) {
2086 						continue;
2087 					}
2088 					m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2089 					if (limit_out) {
2090 						cnt++;
2091 						total_count++;
2092 						if (cnt >= 2) {
2093 							/*
2094 							 * two from each
2095 							 * address
2096 							 */
2097 							break;
2098 						}
2099 						if (total_count > SCTP_ADDRESS_LIMIT) {
2100 							/* No more addresses */
2101 							break;
2102 						}
2103 					}
2104 				}
2105 			}
2106 		}
2107 	} else {
2108 		struct sctp_laddr *laddr;
2109 
2110 		cnt = cnt_inits_to;
2111 		/* First, how many ? */
2112 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2113 			if (laddr->ifa == NULL) {
2114 				continue;
2115 			}
2116 			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2117 				/*
2118 				 * Address being deleted by the system, dont
2119 				 * list.
2120 				 */
2121 				continue;
2122 			if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2123 				/*
2124 				 * Address being deleted on this ep don't
2125 				 * list.
2126 				 */
2127 				continue;
2128 			}
2129 			if (sctp_is_address_in_scope(laddr->ifa,
2130 			    scope->ipv4_addr_legal,
2131 			    scope->ipv6_addr_legal,
2132 			    scope->loopback_scope,
2133 			    scope->ipv4_local_scope,
2134 			    scope->local_scope,
2135 			    scope->site_scope, 1) == 0) {
2136 				continue;
2137 			}
2138 			cnt++;
2139 		}
2140 		if (cnt > SCTP_ADDRESS_LIMIT) {
2141 			limit_out = 1;
2142 		}
2143 		/*
2144 		 * To get through a NAT we only list addresses if we have
2145 		 * more than one. That way if you just bind a single address
2146 		 * we let the source of the init dictate our address.
2147 		 */
2148 		if (cnt > 1) {
2149 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2150 				cnt = 0;
2151 				if (laddr->ifa == NULL) {
2152 					continue;
2153 				}
2154 				if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2155 					continue;
2156 
2157 				if (sctp_is_address_in_scope(laddr->ifa,
2158 				    scope->ipv4_addr_legal,
2159 				    scope->ipv6_addr_legal,
2160 				    scope->loopback_scope,
2161 				    scope->ipv4_local_scope,
2162 				    scope->local_scope,
2163 				    scope->site_scope, 0) == 0) {
2164 					continue;
2165 				}
2166 				m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2167 				cnt++;
2168 				if (cnt >= SCTP_ADDRESS_LIMIT) {
2169 					break;
2170 				}
2171 			}
2172 		}
2173 	}
2174 	SCTP_IPI_ADDR_RUNLOCK();
2175 	return (m_at);
2176 }
2177 
2178 static struct sctp_ifa *
2179 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2180     uint8_t dest_is_loop,
2181     uint8_t dest_is_priv,
2182     sa_family_t fam)
2183 {
2184 	uint8_t dest_is_global = 0;
2185 
2186 	/* dest_is_priv is true if destination is a private address */
2187 	/* dest_is_loop is true if destination is a loopback addresses */
2188 
2189 	/**
2190 	 * Here we determine if its a preferred address. A preferred address
2191 	 * means it is the same scope or higher scope then the destination.
2192 	 * L = loopback, P = private, G = global
2193 	 * -----------------------------------------
2194          *    src    |  dest | result
2195          *  ----------------------------------------
2196          *     L     |    L  |    yes
2197          *  -----------------------------------------
2198          *     P     |    L  |    yes-v4 no-v6
2199          *  -----------------------------------------
2200          *     G     |    L  |    yes-v4 no-v6
2201          *  -----------------------------------------
2202          *     L     |    P  |    no
2203          *  -----------------------------------------
2204          *     P     |    P  |    yes
2205          *  -----------------------------------------
2206          *     G     |    P  |    no
2207          *   -----------------------------------------
2208          *     L     |    G  |    no
2209          *   -----------------------------------------
2210          *     P     |    G  |    no
2211          *    -----------------------------------------
2212          *     G     |    G  |    yes
2213          *    -----------------------------------------
2214 	 */
2215 
2216 	if (ifa->address.sa.sa_family != fam) {
2217 		/* forget mis-matched family */
2218 		return (NULL);
2219 	}
2220 	if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2221 		dest_is_global = 1;
2222 	}
2223 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2224 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2225 	/* Ok the address may be ok */
2226 	if (fam == AF_INET6) {
2227 		/* ok to use deprecated addresses? no lets not! */
2228 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2229 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2230 			return (NULL);
2231 		}
2232 		if (ifa->src_is_priv && !ifa->src_is_loop) {
2233 			if (dest_is_loop) {
2234 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2235 				return (NULL);
2236 			}
2237 		}
2238 		if (ifa->src_is_glob) {
2239 			if (dest_is_loop) {
2240 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2241 				return (NULL);
2242 			}
2243 		}
2244 	}
2245 	/*
2246 	 * Now that we know what is what, implement or table this could in
2247 	 * theory be done slicker (it used to be), but this is
2248 	 * straightforward and easier to validate :-)
2249 	 */
2250 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2251 	    ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2252 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2253 	    dest_is_loop, dest_is_priv, dest_is_global);
2254 
2255 	if ((ifa->src_is_loop) && (dest_is_priv)) {
2256 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2257 		return (NULL);
2258 	}
2259 	if ((ifa->src_is_glob) && (dest_is_priv)) {
2260 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2261 		return (NULL);
2262 	}
2263 	if ((ifa->src_is_loop) && (dest_is_global)) {
2264 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2265 		return (NULL);
2266 	}
2267 	if ((ifa->src_is_priv) && (dest_is_global)) {
2268 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2269 		return (NULL);
2270 	}
2271 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2272 	/* its a preferred address */
2273 	return (ifa);
2274 }
2275 
2276 static struct sctp_ifa *
2277 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2278     uint8_t dest_is_loop,
2279     uint8_t dest_is_priv,
2280     sa_family_t fam)
2281 {
2282 	uint8_t dest_is_global = 0;
2283 
2284 	/*
2285 	 * Here we determine if its a acceptable address. A acceptable
2286 	 * address means it is the same scope or higher scope but we can
2287 	 * allow for NAT which means its ok to have a global dest and a
2288 	 * private src.
2289 	 *
2290 	 * L = loopback, P = private, G = global
2291 	 * ----------------------------------------- src    |  dest | result
2292 	 * ----------------------------------------- L     |   L   |    yes
2293 	 * ----------------------------------------- P     |   L   |
2294 	 * yes-v4 no-v6 ----------------------------------------- G     |
2295 	 * L   |    yes ----------------------------------------- L     |
2296 	 * P   |    no ----------------------------------------- P     |   P
2297 	 * |    yes ----------------------------------------- G     |   P
2298 	 * |    yes - May not work -----------------------------------------
2299 	 * L     |   G   |    no ----------------------------------------- P
2300 	 * |   G   |    yes - May not work
2301 	 * ----------------------------------------- G     |   G   |    yes
2302 	 * -----------------------------------------
2303 	 */
2304 
2305 	if (ifa->address.sa.sa_family != fam) {
2306 		/* forget non matching family */
2307 		return (NULL);
2308 	}
2309 	/* Ok the address may be ok */
2310 	if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2311 		dest_is_global = 1;
2312 	}
2313 	if (fam == AF_INET6) {
2314 		/* ok to use deprecated addresses? */
2315 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2316 			return (NULL);
2317 		}
2318 		if (ifa->src_is_priv) {
2319 			/* Special case, linklocal to loop */
2320 			if (dest_is_loop)
2321 				return (NULL);
2322 		}
2323 	}
2324 	/*
2325 	 * Now that we know what is what, implement our table. This could in
2326 	 * theory be done slicker (it used to be), but this is
2327 	 * straightforward and easier to validate :-)
2328 	 */
2329 	if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2330 		return (NULL);
2331 	}
2332 	if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2333 		return (NULL);
2334 	}
2335 	/* its an acceptable address */
2336 	return (ifa);
2337 }
2338 
2339 int
2340 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2341 {
2342 	struct sctp_laddr *laddr;
2343 
2344 	if (stcb == NULL) {
2345 		/* There are no restrictions, no TCB :-) */
2346 		return (0);
2347 	}
2348 	LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2349 		if (laddr->ifa == NULL) {
2350 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2351 			    __FUNCTION__);
2352 			continue;
2353 		}
2354 		if (laddr->ifa == ifa) {
2355 			/* Yes it is on the list */
2356 			return (1);
2357 		}
2358 	}
2359 	return (0);
2360 }
2361 
2362 
2363 int
2364 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2365 {
2366 	struct sctp_laddr *laddr;
2367 
2368 	if (ifa == NULL)
2369 		return (0);
2370 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2371 		if (laddr->ifa == NULL) {
2372 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2373 			    __FUNCTION__);
2374 			continue;
2375 		}
2376 		if ((laddr->ifa == ifa) && laddr->action == 0)
2377 			/* same pointer */
2378 			return (1);
2379 	}
2380 	return (0);
2381 }
2382 
2383 
2384 
2385 static struct sctp_ifa *
2386 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2387     sctp_route_t * ro,
2388     uint32_t vrf_id,
2389     int non_asoc_addr_ok,
2390     uint8_t dest_is_priv,
2391     uint8_t dest_is_loop,
2392     sa_family_t fam)
2393 {
2394 	struct sctp_laddr *laddr, *starting_point;
2395 	void *ifn;
2396 	int resettotop = 0;
2397 	struct sctp_ifn *sctp_ifn;
2398 	struct sctp_ifa *sctp_ifa, *sifa;
2399 	struct sctp_vrf *vrf;
2400 	uint32_t ifn_index;
2401 
2402 	vrf = sctp_find_vrf(vrf_id);
2403 	if (vrf == NULL)
2404 		return (NULL);
2405 
2406 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2407 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2408 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2409 	/*
2410 	 * first question, is the ifn we will emit on in our list, if so, we
2411 	 * want such an address. Note that we first looked for a preferred
2412 	 * address.
2413 	 */
2414 	if (sctp_ifn) {
2415 		/* is a preferred one on the interface we route out? */
2416 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2417 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2418 			    (non_asoc_addr_ok == 0))
2419 				continue;
2420 			sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2421 			    dest_is_loop,
2422 			    dest_is_priv, fam);
2423 			if (sifa == NULL)
2424 				continue;
2425 			if (sctp_is_addr_in_ep(inp, sifa)) {
2426 				atomic_add_int(&sifa->refcount, 1);
2427 				return (sifa);
2428 			}
2429 		}
2430 	}
2431 	/*
2432 	 * ok, now we now need to find one on the list of the addresses. We
2433 	 * can't get one on the emitting interface so let's find first a
2434 	 * preferred one. If not that an acceptable one otherwise... we
2435 	 * return NULL.
2436 	 */
2437 	starting_point = inp->next_addr_touse;
2438 once_again:
2439 	if (inp->next_addr_touse == NULL) {
2440 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2441 		resettotop = 1;
2442 	}
2443 	for (laddr = inp->next_addr_touse; laddr;
2444 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2445 		if (laddr->ifa == NULL) {
2446 			/* address has been removed */
2447 			continue;
2448 		}
2449 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2450 			/* address is being deleted */
2451 			continue;
2452 		}
2453 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2454 		    dest_is_priv, fam);
2455 		if (sifa == NULL)
2456 			continue;
2457 		atomic_add_int(&sifa->refcount, 1);
2458 		return (sifa);
2459 	}
2460 	if (resettotop == 0) {
2461 		inp->next_addr_touse = NULL;
2462 		goto once_again;
2463 	}
2464 	inp->next_addr_touse = starting_point;
2465 	resettotop = 0;
2466 once_again_too:
2467 	if (inp->next_addr_touse == NULL) {
2468 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2469 		resettotop = 1;
2470 	}
2471 	/* ok, what about an acceptable address in the inp */
2472 	for (laddr = inp->next_addr_touse; laddr;
2473 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2474 		if (laddr->ifa == NULL) {
2475 			/* address has been removed */
2476 			continue;
2477 		}
2478 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2479 			/* address is being deleted */
2480 			continue;
2481 		}
2482 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2483 		    dest_is_priv, fam);
2484 		if (sifa == NULL)
2485 			continue;
2486 		atomic_add_int(&sifa->refcount, 1);
2487 		return (sifa);
2488 	}
2489 	if (resettotop == 0) {
2490 		inp->next_addr_touse = NULL;
2491 		goto once_again_too;
2492 	}
2493 	/*
2494 	 * no address bound can be a source for the destination we are in
2495 	 * trouble
2496 	 */
2497 	return (NULL);
2498 }
2499 
2500 
2501 
2502 static struct sctp_ifa *
2503 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2504     struct sctp_tcb *stcb,
2505     struct sctp_nets *net,
2506     sctp_route_t * ro,
2507     uint32_t vrf_id,
2508     uint8_t dest_is_priv,
2509     uint8_t dest_is_loop,
2510     int non_asoc_addr_ok,
2511     sa_family_t fam)
2512 {
2513 	struct sctp_laddr *laddr, *starting_point;
2514 	void *ifn;
2515 	struct sctp_ifn *sctp_ifn;
2516 	struct sctp_ifa *sctp_ifa, *sifa;
2517 	uint8_t start_at_beginning = 0;
2518 	struct sctp_vrf *vrf;
2519 	uint32_t ifn_index;
2520 
2521 	/*
2522 	 * first question, is the ifn we will emit on in our list, if so, we
2523 	 * want that one.
2524 	 */
2525 	vrf = sctp_find_vrf(vrf_id);
2526 	if (vrf == NULL)
2527 		return (NULL);
2528 
2529 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2530 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2531 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2532 
2533 	/*
2534 	 * first question, is the ifn we will emit on in our list?  If so,
2535 	 * we want that one. First we look for a preferred. Second, we go
2536 	 * for an acceptable.
2537 	 */
2538 	if (sctp_ifn) {
2539 		/* first try for a preferred address on the ep */
2540 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2541 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2542 				continue;
2543 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2544 				sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2545 				if (sifa == NULL)
2546 					continue;
2547 				if (((non_asoc_addr_ok == 0) &&
2548 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2549 				    (non_asoc_addr_ok &&
2550 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2551 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2552 					/* on the no-no list */
2553 					continue;
2554 				}
2555 				atomic_add_int(&sifa->refcount, 1);
2556 				return (sifa);
2557 			}
2558 		}
2559 		/* next try for an acceptable address on the ep */
2560 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2561 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2562 				continue;
2563 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2564 				sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2565 				if (sifa == NULL)
2566 					continue;
2567 				if (((non_asoc_addr_ok == 0) &&
2568 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2569 				    (non_asoc_addr_ok &&
2570 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2571 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2572 					/* on the no-no list */
2573 					continue;
2574 				}
2575 				atomic_add_int(&sifa->refcount, 1);
2576 				return (sifa);
2577 			}
2578 		}
2579 
2580 	}
2581 	/*
2582 	 * if we can't find one like that then we must look at all addresses
2583 	 * bound to pick one at first preferable then secondly acceptable.
2584 	 */
2585 	starting_point = stcb->asoc.last_used_address;
2586 sctp_from_the_top:
2587 	if (stcb->asoc.last_used_address == NULL) {
2588 		start_at_beginning = 1;
2589 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2590 	}
2591 	/* search beginning with the last used address */
2592 	for (laddr = stcb->asoc.last_used_address; laddr;
2593 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2594 		if (laddr->ifa == NULL) {
2595 			/* address has been removed */
2596 			continue;
2597 		}
2598 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2599 			/* address is being deleted */
2600 			continue;
2601 		}
2602 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2603 		if (sifa == NULL)
2604 			continue;
2605 		if (((non_asoc_addr_ok == 0) &&
2606 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2607 		    (non_asoc_addr_ok &&
2608 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2609 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2610 			/* on the no-no list */
2611 			continue;
2612 		}
2613 		stcb->asoc.last_used_address = laddr;
2614 		atomic_add_int(&sifa->refcount, 1);
2615 		return (sifa);
2616 	}
2617 	if (start_at_beginning == 0) {
2618 		stcb->asoc.last_used_address = NULL;
2619 		goto sctp_from_the_top;
2620 	}
2621 	/* now try for any higher scope than the destination */
2622 	stcb->asoc.last_used_address = starting_point;
2623 	start_at_beginning = 0;
2624 sctp_from_the_top2:
2625 	if (stcb->asoc.last_used_address == NULL) {
2626 		start_at_beginning = 1;
2627 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2628 	}
2629 	/* search beginning with the last used address */
2630 	for (laddr = stcb->asoc.last_used_address; laddr;
2631 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2632 		if (laddr->ifa == NULL) {
2633 			/* address has been removed */
2634 			continue;
2635 		}
2636 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2637 			/* address is being deleted */
2638 			continue;
2639 		}
2640 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2641 		    dest_is_priv, fam);
2642 		if (sifa == NULL)
2643 			continue;
2644 		if (((non_asoc_addr_ok == 0) &&
2645 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2646 		    (non_asoc_addr_ok &&
2647 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2648 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2649 			/* on the no-no list */
2650 			continue;
2651 		}
2652 		stcb->asoc.last_used_address = laddr;
2653 		atomic_add_int(&sifa->refcount, 1);
2654 		return (sifa);
2655 	}
2656 	if (start_at_beginning == 0) {
2657 		stcb->asoc.last_used_address = NULL;
2658 		goto sctp_from_the_top2;
2659 	}
2660 	return (NULL);
2661 }
2662 
2663 static struct sctp_ifa *
2664 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2665     struct sctp_tcb *stcb,
2666     int non_asoc_addr_ok,
2667     uint8_t dest_is_loop,
2668     uint8_t dest_is_priv,
2669     int addr_wanted,
2670     sa_family_t fam,
2671     sctp_route_t * ro
2672 )
2673 {
2674 	struct sctp_ifa *ifa, *sifa;
2675 	int num_eligible_addr = 0;
2676 
2677 #ifdef INET6
2678 	struct sockaddr_in6 sin6, lsa6;
2679 
2680 	if (fam == AF_INET6) {
2681 		memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2682 		(void)sa6_recoverscope(&sin6);
2683 	}
2684 #endif				/* INET6 */
2685 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2686 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2687 		    (non_asoc_addr_ok == 0))
2688 			continue;
2689 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2690 		    dest_is_priv, fam);
2691 		if (sifa == NULL)
2692 			continue;
2693 #ifdef INET6
2694 		if (fam == AF_INET6 &&
2695 		    dest_is_loop &&
2696 		    sifa->src_is_loop && sifa->src_is_priv) {
2697 			/*
2698 			 * don't allow fe80::1 to be a src on loop ::1, we
2699 			 * don't list it to the peer so we will get an
2700 			 * abort.
2701 			 */
2702 			continue;
2703 		}
2704 		if (fam == AF_INET6 &&
2705 		    IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2706 		    IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2707 			/*
2708 			 * link-local <-> link-local must belong to the same
2709 			 * scope.
2710 			 */
2711 			memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2712 			(void)sa6_recoverscope(&lsa6);
2713 			if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2714 				continue;
2715 			}
2716 		}
2717 #endif				/* INET6 */
2718 
2719 		/*
2720 		 * Check if the IPv6 address matches to next-hop. In the
2721 		 * mobile case, old IPv6 address may be not deleted from the
2722 		 * interface. Then, the interface has previous and new
2723 		 * addresses.  We should use one corresponding to the
2724 		 * next-hop.  (by micchie)
2725 		 */
2726 #ifdef INET6
2727 		if (stcb && fam == AF_INET6 &&
2728 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2729 			if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2730 			    == 0) {
2731 				continue;
2732 			}
2733 		}
2734 #endif
2735 		/* Avoid topologically incorrect IPv4 address */
2736 		if (stcb && fam == AF_INET &&
2737 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2738 			if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2739 				continue;
2740 			}
2741 		}
2742 		if (stcb) {
2743 			if (sctp_is_address_in_scope(ifa,
2744 			    stcb->asoc.ipv4_addr_legal,
2745 			    stcb->asoc.ipv6_addr_legal,
2746 			    stcb->asoc.loopback_scope,
2747 			    stcb->asoc.ipv4_local_scope,
2748 			    stcb->asoc.local_scope,
2749 			    stcb->asoc.site_scope, 0) == 0) {
2750 				continue;
2751 			}
2752 			if (((non_asoc_addr_ok == 0) &&
2753 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2754 			    (non_asoc_addr_ok &&
2755 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2756 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2757 				/*
2758 				 * It is restricted for some reason..
2759 				 * probably not yet added.
2760 				 */
2761 				continue;
2762 			}
2763 		}
2764 		if (num_eligible_addr >= addr_wanted) {
2765 			return (sifa);
2766 		}
2767 		num_eligible_addr++;
2768 	}
2769 	return (NULL);
2770 }
2771 
2772 
2773 static int
2774 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2775     struct sctp_tcb *stcb,
2776     int non_asoc_addr_ok,
2777     uint8_t dest_is_loop,
2778     uint8_t dest_is_priv,
2779     sa_family_t fam)
2780 {
2781 	struct sctp_ifa *ifa, *sifa;
2782 	int num_eligible_addr = 0;
2783 
2784 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2785 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2786 		    (non_asoc_addr_ok == 0)) {
2787 			continue;
2788 		}
2789 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2790 		    dest_is_priv, fam);
2791 		if (sifa == NULL) {
2792 			continue;
2793 		}
2794 		if (stcb) {
2795 			if (sctp_is_address_in_scope(ifa,
2796 			    stcb->asoc.ipv4_addr_legal,
2797 			    stcb->asoc.ipv6_addr_legal,
2798 			    stcb->asoc.loopback_scope,
2799 			    stcb->asoc.ipv4_local_scope,
2800 			    stcb->asoc.local_scope,
2801 			    stcb->asoc.site_scope, 0) == 0) {
2802 				continue;
2803 			}
2804 			if (((non_asoc_addr_ok == 0) &&
2805 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2806 			    (non_asoc_addr_ok &&
2807 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2808 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2809 				/*
2810 				 * It is restricted for some reason..
2811 				 * probably not yet added.
2812 				 */
2813 				continue;
2814 			}
2815 		}
2816 		num_eligible_addr++;
2817 	}
2818 	return (num_eligible_addr);
2819 }
2820 
2821 static struct sctp_ifa *
2822 sctp_choose_boundall(struct sctp_inpcb *inp,
2823     struct sctp_tcb *stcb,
2824     struct sctp_nets *net,
2825     sctp_route_t * ro,
2826     uint32_t vrf_id,
2827     uint8_t dest_is_priv,
2828     uint8_t dest_is_loop,
2829     int non_asoc_addr_ok,
2830     sa_family_t fam)
2831 {
2832 	int cur_addr_num = 0, num_preferred = 0;
2833 	void *ifn;
2834 	struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2835 	struct sctp_ifa *sctp_ifa, *sifa;
2836 	uint32_t ifn_index;
2837 	struct sctp_vrf *vrf;
2838 
2839 	/*-
2840 	 * For boundall we can use any address in the association.
2841 	 * If non_asoc_addr_ok is set we can use any address (at least in
2842 	 * theory). So we look for preferred addresses first. If we find one,
2843 	 * we use it. Otherwise we next try to get an address on the
2844 	 * interface, which we should be able to do (unless non_asoc_addr_ok
2845 	 * is false and we are routed out that way). In these cases where we
2846 	 * can't use the address of the interface we go through all the
2847 	 * ifn's looking for an address we can use and fill that in. Punting
2848 	 * means we send back address 0, which will probably cause problems
2849 	 * actually since then IP will fill in the address of the route ifn,
2850 	 * which means we probably already rejected it.. i.e. here comes an
2851 	 * abort :-<.
2852 	 */
2853 	vrf = sctp_find_vrf(vrf_id);
2854 	if (vrf == NULL)
2855 		return (NULL);
2856 
2857 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2858 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2859 	emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2860 	if (sctp_ifn == NULL) {
2861 		/* ?? We don't have this guy ?? */
2862 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2863 		goto bound_all_plan_b;
2864 	}
2865 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2866 	    ifn_index, sctp_ifn->ifn_name);
2867 
2868 	if (net) {
2869 		cur_addr_num = net->indx_of_eligible_next_to_use;
2870 	}
2871 	num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2872 	    stcb,
2873 	    non_asoc_addr_ok,
2874 	    dest_is_loop,
2875 	    dest_is_priv, fam);
2876 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2877 	    num_preferred, sctp_ifn->ifn_name);
2878 	if (num_preferred == 0) {
2879 		/*
2880 		 * no eligible addresses, we must use some other interface
2881 		 * address if we can find one.
2882 		 */
2883 		goto bound_all_plan_b;
2884 	}
2885 	/*
2886 	 * Ok we have num_eligible_addr set with how many we can use, this
2887 	 * may vary from call to call due to addresses being deprecated
2888 	 * etc..
2889 	 */
2890 	if (cur_addr_num >= num_preferred) {
2891 		cur_addr_num = 0;
2892 	}
2893 	/*
2894 	 * select the nth address from the list (where cur_addr_num is the
2895 	 * nth) and 0 is the first one, 1 is the second one etc...
2896 	 */
2897 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2898 
2899 	sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2900 	    dest_is_priv, cur_addr_num, fam, ro);
2901 
2902 	/* if sctp_ifa is NULL something changed??, fall to plan b. */
2903 	if (sctp_ifa) {
2904 		atomic_add_int(&sctp_ifa->refcount, 1);
2905 		if (net) {
2906 			/* save off where the next one we will want */
2907 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2908 		}
2909 		return (sctp_ifa);
2910 	}
2911 	/*
2912 	 * plan_b: Look at all interfaces and find a preferred address. If
2913 	 * no preferred fall through to plan_c.
2914 	 */
2915 bound_all_plan_b:
2916 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2917 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2918 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2919 		    sctp_ifn->ifn_name);
2920 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2921 			/* wrong base scope */
2922 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2923 			continue;
2924 		}
2925 		if ((sctp_ifn == looked_at) && looked_at) {
2926 			/* already looked at this guy */
2927 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2928 			continue;
2929 		}
2930 		num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2931 		    dest_is_loop, dest_is_priv, fam);
2932 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
2933 		    "Found ifn:%p %d preferred source addresses\n",
2934 		    ifn, num_preferred);
2935 		if (num_preferred == 0) {
2936 			/* None on this interface. */
2937 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2938 			continue;
2939 		}
2940 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
2941 		    "num preferred:%d on interface:%p cur_addr_num:%d\n",
2942 		    num_preferred, sctp_ifn, cur_addr_num);
2943 
2944 		/*
2945 		 * Ok we have num_eligible_addr set with how many we can
2946 		 * use, this may vary from call to call due to addresses
2947 		 * being deprecated etc..
2948 		 */
2949 		if (cur_addr_num >= num_preferred) {
2950 			cur_addr_num = 0;
2951 		}
2952 		sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2953 		    dest_is_priv, cur_addr_num, fam, ro);
2954 		if (sifa == NULL)
2955 			continue;
2956 		if (net) {
2957 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2958 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
2959 			    cur_addr_num);
2960 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
2961 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
2962 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
2963 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
2964 		}
2965 		atomic_add_int(&sifa->refcount, 1);
2966 		return (sifa);
2967 
2968 	}
2969 
2970 	/* plan_c: do we have an acceptable address on the emit interface */
2971 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
2972 	if (emit_ifn == NULL) {
2973 		goto plan_d;
2974 	}
2975 	LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
2976 		if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2977 		    (non_asoc_addr_ok == 0))
2978 			continue;
2979 		sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
2980 		    dest_is_priv, fam);
2981 		if (sifa == NULL)
2982 			continue;
2983 		if (stcb) {
2984 			if (sctp_is_address_in_scope(sifa,
2985 			    stcb->asoc.ipv4_addr_legal,
2986 			    stcb->asoc.ipv6_addr_legal,
2987 			    stcb->asoc.loopback_scope,
2988 			    stcb->asoc.ipv4_local_scope,
2989 			    stcb->asoc.local_scope,
2990 			    stcb->asoc.site_scope, 0) == 0) {
2991 				continue;
2992 			}
2993 			if (((non_asoc_addr_ok == 0) &&
2994 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2995 			    (non_asoc_addr_ok &&
2996 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2997 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2998 				/*
2999 				 * It is restricted for some reason..
3000 				 * probably not yet added.
3001 				 */
3002 				continue;
3003 			}
3004 		}
3005 		atomic_add_int(&sifa->refcount, 1);
3006 		return (sifa);
3007 	}
3008 plan_d:
3009 	/*
3010 	 * plan_d: We are in trouble. No preferred address on the emit
3011 	 * interface. And not even a preferred address on all interfaces. Go
3012 	 * out and see if we can find an acceptable address somewhere
3013 	 * amongst all interfaces.
3014 	 */
3015 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
3016 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3017 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3018 			/* wrong base scope */
3019 			continue;
3020 		}
3021 		if ((sctp_ifn == looked_at) && looked_at)
3022 			/* already looked at this guy */
3023 			continue;
3024 
3025 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3026 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3027 			    (non_asoc_addr_ok == 0))
3028 				continue;
3029 			sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3030 			    dest_is_loop,
3031 			    dest_is_priv, fam);
3032 			if (sifa == NULL)
3033 				continue;
3034 			if (stcb) {
3035 				if (sctp_is_address_in_scope(sifa,
3036 				    stcb->asoc.ipv4_addr_legal,
3037 				    stcb->asoc.ipv6_addr_legal,
3038 				    stcb->asoc.loopback_scope,
3039 				    stcb->asoc.ipv4_local_scope,
3040 				    stcb->asoc.local_scope,
3041 				    stcb->asoc.site_scope, 0) == 0) {
3042 					continue;
3043 				}
3044 				if (((non_asoc_addr_ok == 0) &&
3045 				    (sctp_is_addr_restricted(stcb, sifa))) ||
3046 				    (non_asoc_addr_ok &&
3047 				    (sctp_is_addr_restricted(stcb, sifa)) &&
3048 				    (!sctp_is_addr_pending(stcb, sifa)))) {
3049 					/*
3050 					 * It is restricted for some
3051 					 * reason.. probably not yet added.
3052 					 */
3053 					continue;
3054 				}
3055 			}
3056 			atomic_add_int(&sifa->refcount, 1);
3057 			return (sifa);
3058 		}
3059 	}
3060 	/*
3061 	 * Ok we can find NO address to source from that is not on our
3062 	 * restricted list and non_asoc_address is NOT ok, or it is on our
3063 	 * restricted list. We can't source to it :-(
3064 	 */
3065 	return (NULL);
3066 }
3067 
3068 
3069 
3070 /* tcb may be NULL */
3071 struct sctp_ifa *
3072 sctp_source_address_selection(struct sctp_inpcb *inp,
3073     struct sctp_tcb *stcb,
3074     sctp_route_t * ro,
3075     struct sctp_nets *net,
3076     int non_asoc_addr_ok, uint32_t vrf_id)
3077 {
3078 	struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3079 
3080 #ifdef INET6
3081 	struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3082 
3083 #endif
3084 	struct sctp_ifa *answer;
3085 	uint8_t dest_is_priv, dest_is_loop;
3086 	sa_family_t fam;
3087 
3088 	/*-
3089 	 * Rules: - Find the route if needed, cache if I can. - Look at
3090 	 * interface address in route, Is it in the bound list. If so we
3091 	 * have the best source. - If not we must rotate amongst the
3092 	 * addresses.
3093 	 *
3094 	 * Cavets and issues
3095 	 *
3096 	 * Do we need to pay attention to scope. We can have a private address
3097 	 * or a global address we are sourcing or sending to. So if we draw
3098 	 * it out
3099 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3100 	 * For V4
3101 	 * ------------------------------------------
3102 	 *      source     *      dest  *  result
3103 	 * -----------------------------------------
3104 	 * <a>  Private    *    Global  *  NAT
3105 	 * -----------------------------------------
3106 	 * <b>  Private    *    Private *  No problem
3107 	 * -----------------------------------------
3108 	 * <c>  Global     *    Private *  Huh, How will this work?
3109 	 * -----------------------------------------
3110 	 * <d>  Global     *    Global  *  No Problem
3111 	 *------------------------------------------
3112 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3113 	 * For V6
3114 	 *------------------------------------------
3115 	 *      source     *      dest  *  result
3116 	 * -----------------------------------------
3117 	 * <a>  Linklocal  *    Global  *
3118 	 * -----------------------------------------
3119 	 * <b>  Linklocal  * Linklocal  *  No problem
3120 	 * -----------------------------------------
3121 	 * <c>  Global     * Linklocal  *  Huh, How will this work?
3122 	 * -----------------------------------------
3123 	 * <d>  Global     *    Global  *  No Problem
3124 	 *------------------------------------------
3125 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3126 	 *
3127 	 * And then we add to that what happens if there are multiple addresses
3128 	 * assigned to an interface. Remember the ifa on a ifn is a linked
3129 	 * list of addresses. So one interface can have more than one IP
3130 	 * address. What happens if we have both a private and a global
3131 	 * address? Do we then use context of destination to sort out which
3132 	 * one is best? And what about NAT's sending P->G may get you a NAT
3133 	 * translation, or should you select the G thats on the interface in
3134 	 * preference.
3135 	 *
3136 	 * Decisions:
3137 	 *
3138 	 * - count the number of addresses on the interface.
3139 	 * - if it is one, no problem except case <c>.
3140 	 *   For <a> we will assume a NAT out there.
3141 	 * - if there are more than one, then we need to worry about scope P
3142 	 *   or G. We should prefer G -> G and P -> P if possible.
3143 	 *   Then as a secondary fall back to mixed types G->P being a last
3144 	 *   ditch one.
3145 	 * - The above all works for bound all, but bound specific we need to
3146 	 *   use the same concept but instead only consider the bound
3147 	 *   addresses. If the bound set is NOT assigned to the interface then
3148 	 *   we must use rotation amongst the bound addresses..
3149 	 */
3150 	if (ro->ro_rt == NULL) {
3151 		/*
3152 		 * Need a route to cache.
3153 		 */
3154 		SCTP_RTALLOC(ro, vrf_id);
3155 	}
3156 	if (ro->ro_rt == NULL) {
3157 		return (NULL);
3158 	}
3159 	fam = to->sin_family;
3160 	dest_is_priv = dest_is_loop = 0;
3161 	/* Setup our scopes for the destination */
3162 	switch (fam) {
3163 	case AF_INET:
3164 		/* Scope based on outbound address */
3165 		if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3166 			dest_is_loop = 1;
3167 			if (net != NULL) {
3168 				/* mark it as local */
3169 				net->addr_is_local = 1;
3170 			}
3171 		} else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3172 			dest_is_priv = 1;
3173 		}
3174 		break;
3175 #ifdef INET6
3176 	case AF_INET6:
3177 		/* Scope based on outbound address */
3178 		if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3179 		    SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3180 			/*
3181 			 * If the address is a loopback address, which
3182 			 * consists of "::1" OR "fe80::1%lo0", we are
3183 			 * loopback scope. But we don't use dest_is_priv
3184 			 * (link local addresses).
3185 			 */
3186 			dest_is_loop = 1;
3187 			if (net != NULL) {
3188 				/* mark it as local */
3189 				net->addr_is_local = 1;
3190 			}
3191 		} else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3192 			dest_is_priv = 1;
3193 		}
3194 		break;
3195 #endif
3196 	}
3197 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3198 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
3199 	SCTP_IPI_ADDR_RLOCK();
3200 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3201 		/*
3202 		 * Bound all case
3203 		 */
3204 		answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3205 		    dest_is_priv, dest_is_loop,
3206 		    non_asoc_addr_ok, fam);
3207 		SCTP_IPI_ADDR_RUNLOCK();
3208 		return (answer);
3209 	}
3210 	/*
3211 	 * Subset bound case
3212 	 */
3213 	if (stcb) {
3214 		answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
3215 		    vrf_id, dest_is_priv,
3216 		    dest_is_loop,
3217 		    non_asoc_addr_ok, fam);
3218 	} else {
3219 		answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3220 		    non_asoc_addr_ok,
3221 		    dest_is_priv,
3222 		    dest_is_loop, fam);
3223 	}
3224 	SCTP_IPI_ADDR_RUNLOCK();
3225 	return (answer);
3226 }
3227 
3228 static int
3229 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
3230 {
3231 	struct cmsghdr cmh;
3232 	int tlen, at;
3233 
3234 	tlen = SCTP_BUF_LEN(control);
3235 	at = 0;
3236 	/*
3237 	 * Independent of how many mbufs, find the c_type inside the control
3238 	 * structure and copy out the data.
3239 	 */
3240 	while (at < tlen) {
3241 		if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3242 			/* not enough room for one more we are done. */
3243 			return (0);
3244 		}
3245 		m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3246 		if (((int)cmh.cmsg_len + at) > tlen) {
3247 			/*
3248 			 * this is real messed up since there is not enough
3249 			 * data here to cover the cmsg header. We are done.
3250 			 */
3251 			return (0);
3252 		}
3253 		if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3254 		    (c_type == cmh.cmsg_type)) {
3255 			/* found the one we want, copy it out */
3256 			at += CMSG_ALIGN(sizeof(struct cmsghdr));
3257 			if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3258 				/*
3259 				 * space of cmsg_len after header not big
3260 				 * enough
3261 				 */
3262 				return (0);
3263 			}
3264 			m_copydata(control, at, cpsize, data);
3265 			return (1);
3266 		} else {
3267 			at += CMSG_ALIGN(cmh.cmsg_len);
3268 			if (cmh.cmsg_len == 0) {
3269 				break;
3270 			}
3271 		}
3272 	}
3273 	/* not found */
3274 	return (0);
3275 }
3276 
3277 static struct mbuf *
3278 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
3279     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3280 {
3281 	struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3282 	struct sctp_state_cookie *stc;
3283 	struct sctp_paramhdr *ph;
3284 	uint8_t *foo;
3285 	int sig_offset;
3286 	uint16_t cookie_sz;
3287 
3288 	mret = NULL;
3289 	mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3290 	    sizeof(struct sctp_paramhdr)), 0,
3291 	    M_DONTWAIT, 1, MT_DATA);
3292 	if (mret == NULL) {
3293 		return (NULL);
3294 	}
3295 	copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3296 	if (copy_init == NULL) {
3297 		sctp_m_freem(mret);
3298 		return (NULL);
3299 	}
3300 #ifdef SCTP_MBUF_LOGGING
3301 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3302 		struct mbuf *mat;
3303 
3304 		mat = copy_init;
3305 		while (mat) {
3306 			if (SCTP_BUF_IS_EXTENDED(mat)) {
3307 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3308 			}
3309 			mat = SCTP_BUF_NEXT(mat);
3310 		}
3311 	}
3312 #endif
3313 	copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3314 	    M_DONTWAIT);
3315 	if (copy_initack == NULL) {
3316 		sctp_m_freem(mret);
3317 		sctp_m_freem(copy_init);
3318 		return (NULL);
3319 	}
3320 #ifdef SCTP_MBUF_LOGGING
3321 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3322 		struct mbuf *mat;
3323 
3324 		mat = copy_initack;
3325 		while (mat) {
3326 			if (SCTP_BUF_IS_EXTENDED(mat)) {
3327 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3328 			}
3329 			mat = SCTP_BUF_NEXT(mat);
3330 		}
3331 	}
3332 #endif
3333 	/* easy side we just drop it on the end */
3334 	ph = mtod(mret, struct sctp_paramhdr *);
3335 	SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3336 	    sizeof(struct sctp_paramhdr);
3337 	stc = (struct sctp_state_cookie *)((caddr_t)ph +
3338 	    sizeof(struct sctp_paramhdr));
3339 	ph->param_type = htons(SCTP_STATE_COOKIE);
3340 	ph->param_length = 0;	/* fill in at the end */
3341 	/* Fill in the stc cookie data */
3342 	memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3343 
3344 	/* tack the INIT and then the INIT-ACK onto the chain */
3345 	cookie_sz = 0;
3346 	m_at = mret;
3347 	for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3348 		cookie_sz += SCTP_BUF_LEN(m_at);
3349 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3350 			SCTP_BUF_NEXT(m_at) = copy_init;
3351 			break;
3352 		}
3353 	}
3354 
3355 	for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3356 		cookie_sz += SCTP_BUF_LEN(m_at);
3357 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3358 			SCTP_BUF_NEXT(m_at) = copy_initack;
3359 			break;
3360 		}
3361 	}
3362 
3363 	for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3364 		cookie_sz += SCTP_BUF_LEN(m_at);
3365 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3366 			break;
3367 		}
3368 	}
3369 	sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3370 	if (sig == NULL) {
3371 		/* no space, so free the entire chain */
3372 		sctp_m_freem(mret);
3373 		return (NULL);
3374 	}
3375 	SCTP_BUF_LEN(sig) = 0;
3376 	SCTP_BUF_NEXT(m_at) = sig;
3377 	sig_offset = 0;
3378 	foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3379 	memset(foo, 0, SCTP_SIGNATURE_SIZE);
3380 	*signature = foo;
3381 	SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3382 	cookie_sz += SCTP_SIGNATURE_SIZE;
3383 	ph->param_length = htons(cookie_sz);
3384 	return (mret);
3385 }
3386 
3387 
3388 static uint8_t
3389 sctp_get_ect(struct sctp_tcb *stcb,
3390     struct sctp_tmit_chunk *chk)
3391 {
3392 	uint8_t this_random;
3393 
3394 	/* Huh? */
3395 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 0)
3396 		return (0);
3397 
3398 	if (SCTP_BASE_SYSCTL(sctp_ecn_nonce) == 0)
3399 		/* no nonce, always return ECT0 */
3400 		return (SCTP_ECT0_BIT);
3401 
3402 	if (stcb->asoc.peer_supports_ecn_nonce == 0) {
3403 		/* Peer does NOT support it, so we send a ECT0 only */
3404 		return (SCTP_ECT0_BIT);
3405 	}
3406 	if (chk == NULL)
3407 		return (SCTP_ECT0_BIT);
3408 
3409 	if ((stcb->asoc.hb_random_idx > 3) ||
3410 	    ((stcb->asoc.hb_random_idx == 3) &&
3411 	    (stcb->asoc.hb_ect_randombit > 7))) {
3412 		uint32_t rndval;
3413 
3414 warp_drive_sa:
3415 		rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
3416 		memcpy(stcb->asoc.hb_random_values, &rndval,
3417 		    sizeof(stcb->asoc.hb_random_values));
3418 		this_random = stcb->asoc.hb_random_values[0];
3419 		stcb->asoc.hb_random_idx = 0;
3420 		stcb->asoc.hb_ect_randombit = 0;
3421 	} else {
3422 		if (stcb->asoc.hb_ect_randombit > 7) {
3423 			stcb->asoc.hb_ect_randombit = 0;
3424 			stcb->asoc.hb_random_idx++;
3425 			if (stcb->asoc.hb_random_idx > 3) {
3426 				goto warp_drive_sa;
3427 			}
3428 		}
3429 		this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
3430 	}
3431 	if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
3432 		if (chk != NULL)
3433 			/* ECN Nonce stuff */
3434 			chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
3435 		stcb->asoc.hb_ect_randombit++;
3436 		return (SCTP_ECT1_BIT);
3437 	} else {
3438 		stcb->asoc.hb_ect_randombit++;
3439 		return (SCTP_ECT0_BIT);
3440 	}
3441 }
3442 
3443 static int
3444 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3445     struct sctp_tcb *stcb,	/* may be NULL */
3446     struct sctp_nets *net,
3447     struct sockaddr *to,
3448     struct mbuf *m,
3449     uint32_t auth_offset,
3450     struct sctp_auth_chunk *auth,
3451     uint16_t auth_keyid,
3452     int nofragment_flag,
3453     int ecn_ok,
3454     struct sctp_tmit_chunk *chk,
3455     int out_of_asoc_ok,
3456     uint16_t src_port,
3457     uint16_t dest_port,
3458     uint32_t v_tag,
3459     uint16_t port,
3460     int so_locked,
3461 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3462     SCTP_UNUSED
3463 #endif
3464     union sctp_sockstore *over_addr
3465 )
3466 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3467 {
3468 	/*
3469 	 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
3470 	 * header WITH an SCTPHDR but no IP header, endpoint inp and sa
3471 	 * structure: - fill in the HMAC digest of any AUTH chunk in the
3472 	 * packet. - calculate and fill in the SCTP checksum. - prepend an
3473 	 * IP address header. - if boundall use INADDR_ANY. - if
3474 	 * boundspecific do source address selection. - set fragmentation
3475 	 * option for ipV4. - On return from IP output, check/adjust mtu
3476 	 * size of output interface and smallest_mtu size as well.
3477 	 */
3478 	/* Will need ifdefs around this */
3479 	struct mbuf *o_pak;
3480 	struct mbuf *newm;
3481 	struct sctphdr *sctphdr;
3482 	int packet_length;
3483 	int ret;
3484 	uint32_t vrf_id;
3485 	sctp_route_t *ro = NULL;
3486 	struct udphdr *udp = NULL;
3487 
3488 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3489 	struct socket *so = NULL;
3490 
3491 #endif
3492 
3493 	if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3494 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3495 		sctp_m_freem(m);
3496 		return (EFAULT);
3497 	}
3498 	if (stcb) {
3499 		vrf_id = stcb->asoc.vrf_id;
3500 	} else {
3501 		vrf_id = inp->def_vrf_id;
3502 	}
3503 
3504 	/* fill in the HMAC digest for any AUTH chunk in the packet */
3505 	if ((auth != NULL) && (stcb != NULL)) {
3506 		sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
3507 	}
3508 	if (to->sa_family == AF_INET) {
3509 		struct ip *ip = NULL;
3510 		sctp_route_t iproute;
3511 		uint8_t tos_value;
3512 		int len;
3513 
3514 		len = sizeof(struct ip) + sizeof(struct sctphdr);
3515 		if (port) {
3516 			len += sizeof(struct udphdr);
3517 		}
3518 		newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3519 		if (newm == NULL) {
3520 			sctp_m_freem(m);
3521 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3522 			return (ENOMEM);
3523 		}
3524 		SCTP_ALIGN_TO_END(newm, len);
3525 		SCTP_BUF_LEN(newm) = len;
3526 		SCTP_BUF_NEXT(newm) = m;
3527 		m = newm;
3528 		packet_length = sctp_calculate_len(m);
3529 		ip = mtod(m, struct ip *);
3530 		ip->ip_v = IPVERSION;
3531 		ip->ip_hl = (sizeof(struct ip) >> 2);
3532 		if (net) {
3533 			tos_value = net->tos_flowlabel & 0x000000ff;
3534 		} else {
3535 			tos_value = inp->ip_inp.inp.inp_ip_tos;
3536 		}
3537 		if ((nofragment_flag) && (port == 0)) {
3538 			ip->ip_off = IP_DF;
3539 		} else
3540 			ip->ip_off = 0;
3541 
3542 		/* FreeBSD has a function for ip_id's */
3543 		ip->ip_id = ip_newid();
3544 
3545 		ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3546 		ip->ip_len = packet_length;
3547 		if (stcb) {
3548 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3549 				/* Enable ECN */
3550 				ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
3551 			} else {
3552 				/* No ECN */
3553 				ip->ip_tos = (u_char)(tos_value & 0xfc);
3554 			}
3555 		} else {
3556 			/* no association at all */
3557 			ip->ip_tos = (tos_value & 0xfc);
3558 		}
3559 		if (port) {
3560 			ip->ip_p = IPPROTO_UDP;
3561 		} else {
3562 			ip->ip_p = IPPROTO_SCTP;
3563 		}
3564 		ip->ip_sum = 0;
3565 		if (net == NULL) {
3566 			ro = &iproute;
3567 			memset(&iproute, 0, sizeof(iproute));
3568 			memcpy(&ro->ro_dst, to, to->sa_len);
3569 		} else {
3570 			ro = (sctp_route_t *) & net->ro;
3571 		}
3572 		/* Now the address selection part */
3573 		ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
3574 
3575 		/* call the routine to select the src address */
3576 		if (net && out_of_asoc_ok == 0) {
3577 			if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3578 				sctp_free_ifa(net->ro._s_addr);
3579 				net->ro._s_addr = NULL;
3580 				net->src_addr_selected = 0;
3581 				if (ro->ro_rt) {
3582 					RTFREE(ro->ro_rt);
3583 					ro->ro_rt = NULL;
3584 				}
3585 			}
3586 			if (net->src_addr_selected == 0) {
3587 				/* Cache the source address */
3588 				net->ro._s_addr = sctp_source_address_selection(inp, stcb,
3589 				    ro, net, 0,
3590 				    vrf_id);
3591 				net->src_addr_selected = 1;
3592 			}
3593 			if (net->ro._s_addr == NULL) {
3594 				/* No route to host */
3595 				net->src_addr_selected = 0;
3596 				goto no_route;
3597 			}
3598 			ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
3599 		} else {
3600 			if (over_addr == NULL) {
3601 				struct sctp_ifa *_lsrc;
3602 
3603 				_lsrc = sctp_source_address_selection(inp, stcb, ro,
3604 				    net,
3605 				    out_of_asoc_ok,
3606 				    vrf_id);
3607 				if (_lsrc == NULL) {
3608 					goto no_route;
3609 				}
3610 				ip->ip_src = _lsrc->address.sin.sin_addr;
3611 				sctp_free_ifa(_lsrc);
3612 			} else {
3613 				ip->ip_src = over_addr->sin.sin_addr;
3614 				SCTP_RTALLOC(ro, vrf_id);
3615 			}
3616 		}
3617 		if (port) {
3618 			udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
3619 			udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3620 			udp->uh_dport = port;
3621 			udp->uh_ulen = htons(packet_length - sizeof(struct ip));
3622 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
3623 			sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
3624 		} else {
3625 			sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
3626 		}
3627 
3628 		sctphdr->src_port = src_port;
3629 		sctphdr->dest_port = dest_port;
3630 		sctphdr->v_tag = v_tag;
3631 		sctphdr->checksum = 0;
3632 
3633 		/*
3634 		 * If source address selection fails and we find no route
3635 		 * then the ip_output should fail as well with a
3636 		 * NO_ROUTE_TO_HOST type error. We probably should catch
3637 		 * that somewhere and abort the association right away
3638 		 * (assuming this is an INIT being sent).
3639 		 */
3640 		if ((ro->ro_rt == NULL)) {
3641 			/*
3642 			 * src addr selection failed to find a route (or
3643 			 * valid source addr), so we can't get there from
3644 			 * here (yet)!
3645 			 */
3646 	no_route:
3647 			SCTPDBG(SCTP_DEBUG_OUTPUT1,
3648 			    "%s: dropped packet - no valid source addr\n",
3649 			    __FUNCTION__);
3650 			if (net) {
3651 				SCTPDBG(SCTP_DEBUG_OUTPUT1,
3652 				    "Destination was ");
3653 				SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
3654 				    &net->ro._l_addr.sa);
3655 				if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3656 					if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3657 						SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3658 						sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3659 						    stcb,
3660 						    SCTP_FAILED_THRESHOLD,
3661 						    (void *)net,
3662 						    so_locked);
3663 						net->dest_state &= ~SCTP_ADDR_REACHABLE;
3664 						net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
3665 						/*
3666 						 * JRS 5/14/07 - If a
3667 						 * destination is
3668 						 * unreachable, the PF bit
3669 						 * is turned off.  This
3670 						 * allows an unambiguous use
3671 						 * of the PF bit for
3672 						 * destinations that are
3673 						 * reachable but potentially
3674 						 * failed. If the
3675 						 * destination is set to the
3676 						 * unreachable state, also
3677 						 * set the destination to
3678 						 * the PF state.
3679 						 */
3680 						/*
3681 						 * Add debug message here if
3682 						 * destination is not in PF
3683 						 * state.
3684 						 */
3685 						/*
3686 						 * Stop any running T3
3687 						 * timers here?
3688 						 */
3689 						if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
3690 							net->dest_state &= ~SCTP_ADDR_PF;
3691 							SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
3692 							    net);
3693 						}
3694 					}
3695 				}
3696 				if (stcb) {
3697 					if (net == stcb->asoc.primary_destination) {
3698 						/* need a new primary */
3699 						struct sctp_nets *alt;
3700 
3701 						alt = sctp_find_alternate_net(stcb, net, 0);
3702 						if (alt != net) {
3703 							if (sctp_set_primary_addr(stcb,
3704 							    (struct sockaddr *)NULL,
3705 							    alt) == 0) {
3706 								net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
3707 								if (net->ro._s_addr) {
3708 									sctp_free_ifa(net->ro._s_addr);
3709 									net->ro._s_addr = NULL;
3710 								}
3711 								net->src_addr_selected = 0;
3712 							}
3713 						}
3714 					}
3715 				}
3716 			}
3717 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3718 			sctp_m_freem(m);
3719 			return (EHOSTUNREACH);
3720 		}
3721 		if (ro != &iproute) {
3722 			memcpy(&iproute, ro, sizeof(*ro));
3723 		}
3724 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
3725 		    (uint32_t) (ntohl(ip->ip_src.s_addr)));
3726 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
3727 		    (uint32_t) (ntohl(ip->ip_dst.s_addr)));
3728 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
3729 		    ro->ro_rt);
3730 
3731 		if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3732 			/* failed to prepend data, give up */
3733 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3734 			sctp_m_freem(m);
3735 			return (ENOMEM);
3736 		}
3737 #ifdef  SCTP_PACKET_LOGGING
3738 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
3739 			sctp_packet_log(m, packet_length);
3740 #endif
3741 		SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3742 		if (port) {
3743 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3744 			    (stcb) &&
3745 			    (stcb->asoc.loopback_scope))) {
3746 				sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
3747 				SCTP_STAT_INCR(sctps_sendswcrc);
3748 			} else {
3749 				SCTP_STAT_INCR(sctps_sendnocrc);
3750 			}
3751 			SCTP_ENABLE_UDP_CSUM(o_pak);
3752 		} else {
3753 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3754 			    (stcb) &&
3755 			    (stcb->asoc.loopback_scope))) {
3756 				m->m_pkthdr.csum_flags = CSUM_SCTP;
3757 				m->m_pkthdr.csum_data = 0;
3758 				SCTP_STAT_INCR(sctps_sendhwcrc);
3759 			} else {
3760 				SCTP_STAT_INCR(sctps_sendnocrc);
3761 			}
3762 		}
3763 		/* send it out.  table id is taken from stcb */
3764 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3765 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3766 			so = SCTP_INP_SO(inp);
3767 			SCTP_SOCKET_UNLOCK(so, 0);
3768 		}
3769 #endif
3770 		SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
3771 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3772 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3773 			atomic_add_int(&stcb->asoc.refcnt, 1);
3774 			SCTP_TCB_UNLOCK(stcb);
3775 			SCTP_SOCKET_LOCK(so, 0);
3776 			SCTP_TCB_LOCK(stcb);
3777 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
3778 		}
3779 #endif
3780 		SCTP_STAT_INCR(sctps_sendpackets);
3781 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3782 		if (ret)
3783 			SCTP_STAT_INCR(sctps_senderrors);
3784 
3785 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
3786 		if (net == NULL) {
3787 			/* free tempy routes */
3788 			if (ro->ro_rt) {
3789 				RTFREE(ro->ro_rt);
3790 				ro->ro_rt = NULL;
3791 			}
3792 		} else {
3793 			/* PMTU check versus smallest asoc MTU goes here */
3794 			if ((ro->ro_rt != NULL) &&
3795 			    (net->ro._s_addr)) {
3796 				uint32_t mtu;
3797 
3798 				mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3799 				if (net->port) {
3800 					mtu -= sizeof(struct udphdr);
3801 				}
3802 				if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
3803 #ifdef SCTP_PRINT_FOR_B_AND_M
3804 					SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n", mtu);
3805 #endif
3806 					sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3807 					net->mtu = mtu;
3808 				}
3809 			} else if (ro->ro_rt == NULL) {
3810 				/* route was freed */
3811 				if (net->ro._s_addr &&
3812 				    net->src_addr_selected) {
3813 					sctp_free_ifa(net->ro._s_addr);
3814 					net->ro._s_addr = NULL;
3815 				}
3816 				net->src_addr_selected = 0;
3817 			}
3818 		}
3819 		return (ret);
3820 	}
3821 #ifdef INET6
3822 	else if (to->sa_family == AF_INET6) {
3823 		uint32_t flowlabel;
3824 		struct ip6_hdr *ip6h;
3825 		struct route_in6 ip6route;
3826 		struct ifnet *ifp;
3827 		u_char flowTop;
3828 		uint16_t flowBottom;
3829 		u_char tosBottom, tosTop;
3830 		struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
3831 		int prev_scope = 0;
3832 		struct sockaddr_in6 lsa6_storage;
3833 		int error;
3834 		u_short prev_port = 0;
3835 		int len;
3836 
3837 		if (net != NULL) {
3838 			flowlabel = net->tos_flowlabel;
3839 		} else {
3840 			flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
3841 		}
3842 
3843 		len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr);
3844 		if (port) {
3845 			len += sizeof(struct udphdr);
3846 		}
3847 		newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3848 		if (newm == NULL) {
3849 			sctp_m_freem(m);
3850 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3851 			return (ENOMEM);
3852 		}
3853 		SCTP_ALIGN_TO_END(newm, len);
3854 		SCTP_BUF_LEN(newm) = len;
3855 		SCTP_BUF_NEXT(newm) = m;
3856 		m = newm;
3857 		packet_length = sctp_calculate_len(m);
3858 
3859 		ip6h = mtod(m, struct ip6_hdr *);
3860 		/*
3861 		 * We assume here that inp_flow is in host byte order within
3862 		 * the TCB!
3863 		 */
3864 		flowBottom = flowlabel & 0x0000ffff;
3865 		flowTop = ((flowlabel & 0x000f0000) >> 16);
3866 		tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
3867 		/* protect *sin6 from overwrite */
3868 		sin6 = (struct sockaddr_in6 *)to;
3869 		tmp = *sin6;
3870 		sin6 = &tmp;
3871 
3872 		/* KAME hack: embed scopeid */
3873 		if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3874 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3875 			return (EINVAL);
3876 		}
3877 		if (net == NULL) {
3878 			memset(&ip6route, 0, sizeof(ip6route));
3879 			ro = (sctp_route_t *) & ip6route;
3880 			memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
3881 		} else {
3882 			ro = (sctp_route_t *) & net->ro;
3883 		}
3884 		if (stcb != NULL) {
3885 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3886 				/* Enable ECN */
3887 				tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
3888 			} else {
3889 				/* No ECN */
3890 				tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3891 			}
3892 		} else {
3893 			/* we could get no asoc if it is a O-O-T-B packet */
3894 			tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3895 		}
3896 		ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
3897 		if (port) {
3898 			ip6h->ip6_nxt = IPPROTO_UDP;
3899 		} else {
3900 			ip6h->ip6_nxt = IPPROTO_SCTP;
3901 		}
3902 		ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
3903 		ip6h->ip6_dst = sin6->sin6_addr;
3904 
3905 		/*
3906 		 * Add SRC address selection here: we can only reuse to a
3907 		 * limited degree the kame src-addr-sel, since we can try
3908 		 * their selection but it may not be bound.
3909 		 */
3910 		bzero(&lsa6_tmp, sizeof(lsa6_tmp));
3911 		lsa6_tmp.sin6_family = AF_INET6;
3912 		lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
3913 		lsa6 = &lsa6_tmp;
3914 		if (net && out_of_asoc_ok == 0) {
3915 			if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3916 				sctp_free_ifa(net->ro._s_addr);
3917 				net->ro._s_addr = NULL;
3918 				net->src_addr_selected = 0;
3919 				if (ro->ro_rt) {
3920 					RTFREE(ro->ro_rt);
3921 					ro->ro_rt = NULL;
3922 				}
3923 			}
3924 			if (net->src_addr_selected == 0) {
3925 				sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3926 				/* KAME hack: embed scopeid */
3927 				if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3928 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3929 					return (EINVAL);
3930 				}
3931 				/* Cache the source address */
3932 				net->ro._s_addr = sctp_source_address_selection(inp,
3933 				    stcb,
3934 				    ro,
3935 				    net,
3936 				    0,
3937 				    vrf_id);
3938 				(void)sa6_recoverscope(sin6);
3939 				net->src_addr_selected = 1;
3940 			}
3941 			if (net->ro._s_addr == NULL) {
3942 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
3943 				net->src_addr_selected = 0;
3944 				goto no_route;
3945 			}
3946 			lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
3947 		} else {
3948 			sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
3949 			/* KAME hack: embed scopeid */
3950 			if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3951 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3952 				return (EINVAL);
3953 			}
3954 			if (over_addr == NULL) {
3955 				struct sctp_ifa *_lsrc;
3956 
3957 				_lsrc = sctp_source_address_selection(inp, stcb, ro,
3958 				    net,
3959 				    out_of_asoc_ok,
3960 				    vrf_id);
3961 				if (_lsrc == NULL) {
3962 					goto no_route;
3963 				}
3964 				lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
3965 				sctp_free_ifa(_lsrc);
3966 			} else {
3967 				lsa6->sin6_addr = over_addr->sin6.sin6_addr;
3968 				SCTP_RTALLOC(ro, vrf_id);
3969 			}
3970 			(void)sa6_recoverscope(sin6);
3971 		}
3972 		lsa6->sin6_port = inp->sctp_lport;
3973 
3974 		if (ro->ro_rt == NULL) {
3975 			/*
3976 			 * src addr selection failed to find a route (or
3977 			 * valid source addr), so we can't get there from
3978 			 * here!
3979 			 */
3980 			goto no_route;
3981 		}
3982 		/*
3983 		 * XXX: sa6 may not have a valid sin6_scope_id in the
3984 		 * non-SCOPEDROUTING case.
3985 		 */
3986 		bzero(&lsa6_storage, sizeof(lsa6_storage));
3987 		lsa6_storage.sin6_family = AF_INET6;
3988 		lsa6_storage.sin6_len = sizeof(lsa6_storage);
3989 		lsa6_storage.sin6_addr = lsa6->sin6_addr;
3990 		if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
3991 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
3992 			sctp_m_freem(m);
3993 			return (error);
3994 		}
3995 		/* XXX */
3996 		lsa6_storage.sin6_addr = lsa6->sin6_addr;
3997 		lsa6_storage.sin6_port = inp->sctp_lport;
3998 		lsa6 = &lsa6_storage;
3999 		ip6h->ip6_src = lsa6->sin6_addr;
4000 
4001 		if (port) {
4002 			udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4003 			udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4004 			udp->uh_dport = port;
4005 			udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
4006 			udp->uh_sum = 0;
4007 			sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4008 		} else {
4009 			sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4010 		}
4011 
4012 		sctphdr->src_port = src_port;
4013 		sctphdr->dest_port = dest_port;
4014 		sctphdr->v_tag = v_tag;
4015 		sctphdr->checksum = 0;
4016 
4017 		/*
4018 		 * We set the hop limit now since there is a good chance
4019 		 * that our ro pointer is now filled
4020 		 */
4021 		ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
4022 		ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4023 
4024 #ifdef SCTP_DEBUG
4025 		/* Copy to be sure something bad is not happening */
4026 		sin6->sin6_addr = ip6h->ip6_dst;
4027 		lsa6->sin6_addr = ip6h->ip6_src;
4028 #endif
4029 
4030 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
4031 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
4032 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
4033 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
4034 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
4035 		if (net) {
4036 			sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4037 			/* preserve the port and scope for link local send */
4038 			prev_scope = sin6->sin6_scope_id;
4039 			prev_port = sin6->sin6_port;
4040 		}
4041 		if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4042 			/* failed to prepend data, give up */
4043 			sctp_m_freem(m);
4044 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4045 			return (ENOMEM);
4046 		}
4047 #ifdef  SCTP_PACKET_LOGGING
4048 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4049 			sctp_packet_log(m, packet_length);
4050 #endif
4051 		SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4052 		if (port) {
4053 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4054 			    (stcb) &&
4055 			    (stcb->asoc.loopback_scope))) {
4056 				sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4057 				SCTP_STAT_INCR(sctps_sendswcrc);
4058 			} else {
4059 				SCTP_STAT_INCR(sctps_sendnocrc);
4060 			}
4061 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4062 				udp->uh_sum = 0xffff;
4063 			}
4064 		} else {
4065 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4066 			    (stcb) &&
4067 			    (stcb->asoc.loopback_scope))) {
4068 				m->m_pkthdr.csum_flags = CSUM_SCTP;
4069 				m->m_pkthdr.csum_data = 0;
4070 				SCTP_STAT_INCR(sctps_sendhwcrc);
4071 			} else {
4072 				SCTP_STAT_INCR(sctps_sendnocrc);
4073 			}
4074 		}
4075 		/* send it out. table id is taken from stcb */
4076 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4077 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4078 			so = SCTP_INP_SO(inp);
4079 			SCTP_SOCKET_UNLOCK(so, 0);
4080 		}
4081 #endif
4082 		SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4083 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4084 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4085 			atomic_add_int(&stcb->asoc.refcnt, 1);
4086 			SCTP_TCB_UNLOCK(stcb);
4087 			SCTP_SOCKET_LOCK(so, 0);
4088 			SCTP_TCB_LOCK(stcb);
4089 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
4090 		}
4091 #endif
4092 		if (net) {
4093 			/* for link local this must be done */
4094 			sin6->sin6_scope_id = prev_scope;
4095 			sin6->sin6_port = prev_port;
4096 		}
4097 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4098 		SCTP_STAT_INCR(sctps_sendpackets);
4099 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4100 		if (ret) {
4101 			SCTP_STAT_INCR(sctps_senderrors);
4102 		}
4103 		if (net == NULL) {
4104 			/* Now if we had a temp route free it */
4105 			if (ro->ro_rt) {
4106 				RTFREE(ro->ro_rt);
4107 			}
4108 		} else {
4109 			/* PMTU check versus smallest asoc MTU goes here */
4110 			if (ro->ro_rt == NULL) {
4111 				/* Route was freed */
4112 				if (net->ro._s_addr &&
4113 				    net->src_addr_selected) {
4114 					sctp_free_ifa(net->ro._s_addr);
4115 					net->ro._s_addr = NULL;
4116 				}
4117 				net->src_addr_selected = 0;
4118 			}
4119 			if ((ro->ro_rt != NULL) &&
4120 			    (net->ro._s_addr)) {
4121 				uint32_t mtu;
4122 
4123 				mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4124 				if (mtu &&
4125 				    (stcb->asoc.smallest_mtu > mtu)) {
4126 #ifdef SCTP_PRINT_FOR_B_AND_M
4127 					SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
4128 					    mtu);
4129 #endif
4130 					sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4131 					net->mtu = mtu;
4132 					if (net->port) {
4133 						net->mtu -= sizeof(struct udphdr);
4134 					}
4135 				}
4136 			} else if (ifp) {
4137 				if (ND_IFINFO(ifp)->linkmtu &&
4138 				    (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4139 #ifdef SCTP_PRINT_FOR_B_AND_M
4140 					SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
4141 					    ND_IFINFO(ifp)->linkmtu);
4142 #endif
4143 					sctp_mtu_size_reset(inp,
4144 					    &stcb->asoc,
4145 					    ND_IFINFO(ifp)->linkmtu);
4146 				}
4147 			}
4148 		}
4149 		return (ret);
4150 	}
4151 #endif
4152 	else {
4153 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4154 		    ((struct sockaddr *)to)->sa_family);
4155 		sctp_m_freem(m);
4156 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4157 		return (EFAULT);
4158 	}
4159 }
4160 
4161 
4162 void
4163 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4164 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4165     SCTP_UNUSED
4166 #endif
4167 )
4168 {
4169 	struct mbuf *m, *m_at, *mp_last;
4170 	struct sctp_nets *net;
4171 	struct sctp_init_chunk *init;
4172 	struct sctp_supported_addr_param *sup_addr;
4173 	struct sctp_adaptation_layer_indication *ali;
4174 	struct sctp_ecn_supported_param *ecn;
4175 	struct sctp_prsctp_supported_param *prsctp;
4176 	struct sctp_ecn_nonce_supported_param *ecn_nonce;
4177 	struct sctp_supported_chunk_types_param *pr_supported;
4178 	int cnt_inits_to = 0;
4179 	int padval, ret;
4180 	int num_ext;
4181 	int p_len;
4182 
4183 	/* INIT's always go to the primary (and usually ONLY address) */
4184 	mp_last = NULL;
4185 	net = stcb->asoc.primary_destination;
4186 	if (net == NULL) {
4187 		net = TAILQ_FIRST(&stcb->asoc.nets);
4188 		if (net == NULL) {
4189 			/* TSNH */
4190 			return;
4191 		}
4192 		/* we confirm any address we send an INIT to */
4193 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4194 		(void)sctp_set_primary_addr(stcb, NULL, net);
4195 	} else {
4196 		/* we confirm any address we send an INIT to */
4197 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4198 	}
4199 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4200 #ifdef INET6
4201 	if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4202 		/*
4203 		 * special hook, if we are sending to link local it will not
4204 		 * show up in our private address count.
4205 		 */
4206 		struct sockaddr_in6 *sin6l;
4207 
4208 		sin6l = &net->ro._l_addr.sin6;
4209 		if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4210 			cnt_inits_to = 1;
4211 	}
4212 #endif
4213 	if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4214 		/* This case should not happen */
4215 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4216 		return;
4217 	}
4218 	/* start the INIT timer */
4219 	sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4220 
4221 	m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4222 	if (m == NULL) {
4223 		/* No memory, INIT timer will re-attempt. */
4224 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4225 		return;
4226 	}
4227 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4228 	/*
4229 	 * assume peer supports asconf in order to be able to queue local
4230 	 * address changes while an INIT is in flight and before the assoc
4231 	 * is established.
4232 	 */
4233 	stcb->asoc.peer_supports_asconf = 1;
4234 	/* Now lets put the SCTP header in place */
4235 	init = mtod(m, struct sctp_init_chunk *);
4236 	/* now the chunk header */
4237 	init->ch.chunk_type = SCTP_INITIATION;
4238 	init->ch.chunk_flags = 0;
4239 	/* fill in later from mbuf we build */
4240 	init->ch.chunk_length = 0;
4241 	/* place in my tag */
4242 	init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4243 	/* set up some of the credits. */
4244 	init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4245 	    SCTP_MINIMAL_RWND));
4246 
4247 	init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4248 	init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4249 	init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4250 	/* now the address restriction */
4251 	sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init +
4252 	    sizeof(*init));
4253 	sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4254 #ifdef INET6
4255 	/* we support 2 types: IPv6/IPv4 */
4256 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
4257 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4258 	sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4259 #else
4260 	/* we support 1 type: IPv4 */
4261 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
4262 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4263 	sup_addr->addr_type[1] = htons(0);	/* this is the padding */
4264 #endif
4265 	SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
4266 	/* adaptation layer indication parameter */
4267 	ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
4268 	ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4269 	ali->ph.param_length = htons(sizeof(*ali));
4270 	ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4271 	SCTP_BUF_LEN(m) += sizeof(*ali);
4272 	ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
4273 
4274 	if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4275 		/* Add NAT friendly parameter */
4276 		struct sctp_paramhdr *ph;
4277 
4278 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4279 		ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4280 		ph->param_length = htons(sizeof(struct sctp_paramhdr));
4281 		SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
4282 		ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph));
4283 	}
4284 	/* now any cookie time extensions */
4285 	if (stcb->asoc.cookie_preserve_req) {
4286 		struct sctp_cookie_perserve_param *cookie_preserve;
4287 
4288 		cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4289 		cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4290 		cookie_preserve->ph.param_length = htons(
4291 		    sizeof(*cookie_preserve));
4292 		cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4293 		SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4294 		ecn = (struct sctp_ecn_supported_param *)(
4295 		    (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4296 		stcb->asoc.cookie_preserve_req = 0;
4297 	}
4298 	/* ECN parameter */
4299 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
4300 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4301 		ecn->ph.param_length = htons(sizeof(*ecn));
4302 		SCTP_BUF_LEN(m) += sizeof(*ecn);
4303 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4304 		    sizeof(*ecn));
4305 	} else {
4306 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4307 	}
4308 	/* And now tell the peer we do pr-sctp */
4309 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4310 	prsctp->ph.param_length = htons(sizeof(*prsctp));
4311 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
4312 
4313 	/* And now tell the peer we do all the extensions */
4314 	pr_supported = (struct sctp_supported_chunk_types_param *)
4315 	    ((caddr_t)prsctp + sizeof(*prsctp));
4316 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4317 	num_ext = 0;
4318 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4319 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4320 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4321 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4322 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4323 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4324 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4325 	}
4326 	/*
4327 	 * EY  if the initiator supports nr_sacks, need to report that to
4328 	 * responder in INIT chunk
4329 	 */
4330 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
4331 		pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4332 	}
4333 	p_len = sizeof(*pr_supported) + num_ext;
4334 	pr_supported->ph.param_length = htons(p_len);
4335 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4336 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4337 
4338 
4339 	/* ECN nonce: And now tell the peer we support ECN nonce */
4340 	if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
4341 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
4342 		    ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
4343 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
4344 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
4345 		SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
4346 	}
4347 	/* add authentication parameters */
4348 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4349 		struct sctp_auth_random *randp;
4350 		struct sctp_auth_hmac_algo *hmacs;
4351 		struct sctp_auth_chunk_list *chunks;
4352 
4353 		/* attach RANDOM parameter, if available */
4354 		if (stcb->asoc.authinfo.random != NULL) {
4355 			randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4356 			p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4357 			/* random key already contains the header */
4358 			bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4359 			/* zero out any padding required */
4360 			bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4361 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4362 		}
4363 		/* add HMAC_ALGO parameter */
4364 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4365 		p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4366 		    (uint8_t *) hmacs->hmac_ids);
4367 		if (p_len > 0) {
4368 			p_len += sizeof(*hmacs);
4369 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4370 			hmacs->ph.param_length = htons(p_len);
4371 			/* zero out any padding required */
4372 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4373 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4374 		}
4375 		/* add CHUNKS parameter */
4376 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4377 		p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4378 		    chunks->chunk_types);
4379 		if (p_len > 0) {
4380 			p_len += sizeof(*chunks);
4381 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4382 			chunks->ph.param_length = htons(p_len);
4383 			/* zero out any padding required */
4384 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4385 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4386 		}
4387 	}
4388 	m_at = m;
4389 	/* now the addresses */
4390 	{
4391 		struct sctp_scoping scp;
4392 
4393 		/*
4394 		 * To optimize this we could put the scoping stuff into a
4395 		 * structure and remove the individual uint8's from the
4396 		 * assoc structure. Then we could just sifa in the address
4397 		 * within the stcb.. but for now this is a quick hack to get
4398 		 * the address stuff teased apart.
4399 		 */
4400 		scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4401 		scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4402 		scp.loopback_scope = stcb->asoc.loopback_scope;
4403 		scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4404 		scp.local_scope = stcb->asoc.local_scope;
4405 		scp.site_scope = stcb->asoc.site_scope;
4406 
4407 		m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
4408 	}
4409 
4410 	/* calulate the size and update pkt header and chunk header */
4411 	p_len = 0;
4412 	for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4413 		if (SCTP_BUF_NEXT(m_at) == NULL)
4414 			mp_last = m_at;
4415 		p_len += SCTP_BUF_LEN(m_at);
4416 	}
4417 	init->ch.chunk_length = htons(p_len);
4418 	/*
4419 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4420 	 * here since the timer will drive a retranmission.
4421 	 */
4422 
4423 	/* I don't expect this to execute but we will be safe here */
4424 	padval = p_len % 4;
4425 	if ((padval) && (mp_last)) {
4426 		/*
4427 		 * The compiler worries that mp_last may not be set even
4428 		 * though I think it is impossible :-> however we add
4429 		 * mp_last here just in case.
4430 		 */
4431 		ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4432 		if (ret) {
4433 			/* Houston we have a problem, no space */
4434 			sctp_m_freem(m);
4435 			return;
4436 		}
4437 		p_len += padval;
4438 	}
4439 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4440 	ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4441 	    (struct sockaddr *)&net->ro._l_addr,
4442 	    m, 0, NULL, 0, 0, 0, NULL, 0,
4443 	    inp->sctp_lport, stcb->rport, htonl(0),
4444 	    net->port, so_locked, NULL);
4445 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4446 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4447 	(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4448 }
4449 
4450 struct mbuf *
4451 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4452     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
4453 {
4454 	/*
4455 	 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4456 	 * being equal to the beginning of the params i.e. (iphlen +
4457 	 * sizeof(struct sctp_init_msg) parse through the parameters to the
4458 	 * end of the mbuf verifying that all parameters are known.
4459 	 *
4460 	 * For unknown parameters build and return a mbuf with
4461 	 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4462 	 * processing this chunk stop, and set *abort_processing to 1.
4463 	 *
4464 	 * By having param_offset be pre-set to where parameters begin it is
4465 	 * hoped that this routine may be reused in the future by new
4466 	 * features.
4467 	 */
4468 	struct sctp_paramhdr *phdr, params;
4469 
4470 	struct mbuf *mat, *op_err;
4471 	char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4472 	int at, limit, pad_needed;
4473 	uint16_t ptype, plen, padded_size;
4474 	int err_at;
4475 
4476 	*abort_processing = 0;
4477 	mat = in_initpkt;
4478 	err_at = 0;
4479 	limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4480 	at = param_offset;
4481 	op_err = NULL;
4482 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4483 	phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4484 	while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4485 		ptype = ntohs(phdr->param_type);
4486 		plen = ntohs(phdr->param_length);
4487 		if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4488 			/* wacked parameter */
4489 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4490 			goto invalid_size;
4491 		}
4492 		limit -= SCTP_SIZE32(plen);
4493 		/*-
4494 		 * All parameters for all chunks that we know/understand are
4495 		 * listed here. We process them other places and make
4496 		 * appropriate stop actions per the upper bits. However this
4497 		 * is the generic routine processor's can call to get back
4498 		 * an operr.. to either incorporate (init-ack) or send.
4499 		 */
4500 		padded_size = SCTP_SIZE32(plen);
4501 		switch (ptype) {
4502 			/* Param's with variable size */
4503 		case SCTP_HEARTBEAT_INFO:
4504 		case SCTP_STATE_COOKIE:
4505 		case SCTP_UNRECOG_PARAM:
4506 		case SCTP_ERROR_CAUSE_IND:
4507 			/* ok skip fwd */
4508 			at += padded_size;
4509 			break;
4510 			/* Param's with variable size within a range */
4511 		case SCTP_CHUNK_LIST:
4512 		case SCTP_SUPPORTED_CHUNK_EXT:
4513 			if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4514 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4515 				goto invalid_size;
4516 			}
4517 			at += padded_size;
4518 			break;
4519 		case SCTP_SUPPORTED_ADDRTYPE:
4520 			if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4521 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4522 				goto invalid_size;
4523 			}
4524 			at += padded_size;
4525 			break;
4526 		case SCTP_RANDOM:
4527 			if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4528 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4529 				goto invalid_size;
4530 			}
4531 			at += padded_size;
4532 			break;
4533 		case SCTP_SET_PRIM_ADDR:
4534 		case SCTP_DEL_IP_ADDRESS:
4535 		case SCTP_ADD_IP_ADDRESS:
4536 			if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4537 			    (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4538 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4539 				goto invalid_size;
4540 			}
4541 			at += padded_size;
4542 			break;
4543 			/* Param's with a fixed size */
4544 		case SCTP_IPV4_ADDRESS:
4545 			if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4546 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4547 				goto invalid_size;
4548 			}
4549 			at += padded_size;
4550 			break;
4551 		case SCTP_IPV6_ADDRESS:
4552 			if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4553 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4554 				goto invalid_size;
4555 			}
4556 			at += padded_size;
4557 			break;
4558 		case SCTP_COOKIE_PRESERVE:
4559 			if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4560 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4561 				goto invalid_size;
4562 			}
4563 			at += padded_size;
4564 			break;
4565 		case SCTP_HAS_NAT_SUPPORT:
4566 			*nat_friendly = 1;
4567 			/* fall through */
4568 		case SCTP_ECN_NONCE_SUPPORTED:
4569 		case SCTP_PRSCTP_SUPPORTED:
4570 
4571 			if (padded_size != sizeof(struct sctp_paramhdr)) {
4572 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp/nat support %d\n", plen);
4573 				goto invalid_size;
4574 			}
4575 			at += padded_size;
4576 			break;
4577 		case SCTP_ECN_CAPABLE:
4578 			if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4579 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4580 				goto invalid_size;
4581 			}
4582 			at += padded_size;
4583 			break;
4584 		case SCTP_ULP_ADAPTATION:
4585 			if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4586 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4587 				goto invalid_size;
4588 			}
4589 			at += padded_size;
4590 			break;
4591 		case SCTP_SUCCESS_REPORT:
4592 			if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
4593 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
4594 				goto invalid_size;
4595 			}
4596 			at += padded_size;
4597 			break;
4598 		case SCTP_HOSTNAME_ADDRESS:
4599 			{
4600 				/* We can NOT handle HOST NAME addresses!! */
4601 				int l_len;
4602 
4603 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
4604 				*abort_processing = 1;
4605 				if (op_err == NULL) {
4606 					/* Ok need to try to get a mbuf */
4607 #ifdef INET6
4608 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4609 #else
4610 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4611 #endif
4612 					l_len += plen;
4613 					l_len += sizeof(struct sctp_paramhdr);
4614 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4615 					if (op_err) {
4616 						SCTP_BUF_LEN(op_err) = 0;
4617 						/*
4618 						 * pre-reserve space for ip
4619 						 * and sctp header  and
4620 						 * chunk hdr
4621 						 */
4622 #ifdef INET6
4623 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4624 #else
4625 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4626 #endif
4627 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4628 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4629 					}
4630 				}
4631 				if (op_err) {
4632 					/* If we have space */
4633 					struct sctp_paramhdr s;
4634 
4635 					if (err_at % 4) {
4636 						uint32_t cpthis = 0;
4637 
4638 						pad_needed = 4 - (err_at % 4);
4639 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4640 						err_at += pad_needed;
4641 					}
4642 					s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
4643 					s.param_length = htons(sizeof(s) + plen);
4644 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4645 					err_at += sizeof(s);
4646 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4647 					if (phdr == NULL) {
4648 						sctp_m_freem(op_err);
4649 						/*
4650 						 * we are out of memory but
4651 						 * we still need to have a
4652 						 * look at what to do (the
4653 						 * system is in trouble
4654 						 * though).
4655 						 */
4656 						return (NULL);
4657 					}
4658 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4659 					err_at += plen;
4660 				}
4661 				return (op_err);
4662 				break;
4663 			}
4664 		default:
4665 			/*
4666 			 * we do not recognize the parameter figure out what
4667 			 * we do.
4668 			 */
4669 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
4670 			if ((ptype & 0x4000) == 0x4000) {
4671 				/* Report bit is set?? */
4672 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
4673 				if (op_err == NULL) {
4674 					int l_len;
4675 
4676 					/* Ok need to try to get an mbuf */
4677 #ifdef INET6
4678 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4679 #else
4680 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4681 #endif
4682 					l_len += plen;
4683 					l_len += sizeof(struct sctp_paramhdr);
4684 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4685 					if (op_err) {
4686 						SCTP_BUF_LEN(op_err) = 0;
4687 #ifdef INET6
4688 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4689 #else
4690 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4691 #endif
4692 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4693 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4694 					}
4695 				}
4696 				if (op_err) {
4697 					/* If we have space */
4698 					struct sctp_paramhdr s;
4699 
4700 					if (err_at % 4) {
4701 						uint32_t cpthis = 0;
4702 
4703 						pad_needed = 4 - (err_at % 4);
4704 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4705 						err_at += pad_needed;
4706 					}
4707 					s.param_type = htons(SCTP_UNRECOG_PARAM);
4708 					s.param_length = htons(sizeof(s) + plen);
4709 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4710 					err_at += sizeof(s);
4711 					if (plen > sizeof(tempbuf)) {
4712 						plen = sizeof(tempbuf);
4713 					}
4714 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4715 					if (phdr == NULL) {
4716 						sctp_m_freem(op_err);
4717 						/*
4718 						 * we are out of memory but
4719 						 * we still need to have a
4720 						 * look at what to do (the
4721 						 * system is in trouble
4722 						 * though).
4723 						 */
4724 						op_err = NULL;
4725 						goto more_processing;
4726 					}
4727 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4728 					err_at += plen;
4729 				}
4730 			}
4731 	more_processing:
4732 			if ((ptype & 0x8000) == 0x0000) {
4733 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
4734 				return (op_err);
4735 			} else {
4736 				/* skip this chunk and continue processing */
4737 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
4738 				at += SCTP_SIZE32(plen);
4739 			}
4740 			break;
4741 
4742 		}
4743 		phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4744 	}
4745 	return (op_err);
4746 invalid_size:
4747 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
4748 	*abort_processing = 1;
4749 	if ((op_err == NULL) && phdr) {
4750 		int l_len;
4751 
4752 #ifdef INET6
4753 		l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4754 #else
4755 		l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4756 #endif
4757 		l_len += (2 * sizeof(struct sctp_paramhdr));
4758 		op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4759 		if (op_err) {
4760 			SCTP_BUF_LEN(op_err) = 0;
4761 #ifdef INET6
4762 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4763 #else
4764 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4765 #endif
4766 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4767 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4768 		}
4769 	}
4770 	if ((op_err) && phdr) {
4771 		struct sctp_paramhdr s;
4772 
4773 		if (err_at % 4) {
4774 			uint32_t cpthis = 0;
4775 
4776 			pad_needed = 4 - (err_at % 4);
4777 			m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4778 			err_at += pad_needed;
4779 		}
4780 		s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4781 		s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
4782 		m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4783 		err_at += sizeof(s);
4784 		/* Only copy back the p-hdr that caused the issue */
4785 		m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
4786 	}
4787 	return (op_err);
4788 }
4789 
4790 static int
4791 sctp_are_there_new_addresses(struct sctp_association *asoc,
4792     struct mbuf *in_initpkt, int iphlen, int offset)
4793 {
4794 	/*
4795 	 * Given a INIT packet, look through the packet to verify that there
4796 	 * are NO new addresses. As we go through the parameters add reports
4797 	 * of any un-understood parameters that require an error.  Also we
4798 	 * must return (1) to drop the packet if we see a un-understood
4799 	 * parameter that tells us to drop the chunk.
4800 	 */
4801 	struct sockaddr_in sin4, *sa4;
4802 
4803 #ifdef INET6
4804 	struct sockaddr_in6 sin6, *sa6;
4805 
4806 #endif
4807 	struct sockaddr *sa_touse;
4808 	struct sockaddr *sa;
4809 	struct sctp_paramhdr *phdr, params;
4810 	struct ip *iph;
4811 
4812 #ifdef INET6
4813 	struct ip6_hdr *ip6h;
4814 
4815 #endif
4816 	struct mbuf *mat;
4817 	uint16_t ptype, plen;
4818 	int err_at;
4819 	uint8_t fnd;
4820 	struct sctp_nets *net;
4821 
4822 	memset(&sin4, 0, sizeof(sin4));
4823 #ifdef INET6
4824 	memset(&sin6, 0, sizeof(sin6));
4825 #endif
4826 	sin4.sin_family = AF_INET;
4827 	sin4.sin_len = sizeof(sin4);
4828 #ifdef INET6
4829 	sin6.sin6_family = AF_INET6;
4830 	sin6.sin6_len = sizeof(sin6);
4831 #endif
4832 	sa_touse = NULL;
4833 	/* First what about the src address of the pkt ? */
4834 	iph = mtod(in_initpkt, struct ip *);
4835 	switch (iph->ip_v) {
4836 	case IPVERSION:
4837 		/* source addr is IPv4 */
4838 		sin4.sin_addr = iph->ip_src;
4839 		sa_touse = (struct sockaddr *)&sin4;
4840 		break;
4841 #ifdef INET6
4842 	case IPV6_VERSION >> 4:
4843 		/* source addr is IPv6 */
4844 		ip6h = mtod(in_initpkt, struct ip6_hdr *);
4845 		sin6.sin6_addr = ip6h->ip6_src;
4846 		sa_touse = (struct sockaddr *)&sin6;
4847 		break;
4848 #endif
4849 	default:
4850 		return (1);
4851 	}
4852 
4853 	fnd = 0;
4854 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4855 		sa = (struct sockaddr *)&net->ro._l_addr;
4856 		if (sa->sa_family == sa_touse->sa_family) {
4857 			if (sa->sa_family == AF_INET) {
4858 				sa4 = (struct sockaddr_in *)sa;
4859 				if (sa4->sin_addr.s_addr ==
4860 				    sin4.sin_addr.s_addr) {
4861 					fnd = 1;
4862 					break;
4863 				}
4864 			}
4865 #ifdef INET6
4866 			if (sa->sa_family == AF_INET6) {
4867 				sa6 = (struct sockaddr_in6 *)sa;
4868 				if (SCTP6_ARE_ADDR_EQUAL(sa6,
4869 				    &sin6)) {
4870 					fnd = 1;
4871 					break;
4872 				}
4873 			}
4874 #endif
4875 		}
4876 	}
4877 	if (fnd == 0) {
4878 		/* New address added! no need to look futher. */
4879 		return (1);
4880 	}
4881 	/* Ok so far lets munge through the rest of the packet */
4882 	mat = in_initpkt;
4883 	err_at = 0;
4884 	sa_touse = NULL;
4885 	offset += sizeof(struct sctp_init_chunk);
4886 	phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
4887 	while (phdr) {
4888 		ptype = ntohs(phdr->param_type);
4889 		plen = ntohs(phdr->param_length);
4890 		if (ptype == SCTP_IPV4_ADDRESS) {
4891 			struct sctp_ipv4addr_param *p4, p4_buf;
4892 
4893 			phdr = sctp_get_next_param(mat, offset,
4894 			    (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4895 			if (plen != sizeof(struct sctp_ipv4addr_param) ||
4896 			    phdr == NULL) {
4897 				return (1);
4898 			}
4899 			p4 = (struct sctp_ipv4addr_param *)phdr;
4900 			sin4.sin_addr.s_addr = p4->addr;
4901 			sa_touse = (struct sockaddr *)&sin4;
4902 		} else if (ptype == SCTP_IPV6_ADDRESS) {
4903 			struct sctp_ipv6addr_param *p6, p6_buf;
4904 
4905 			phdr = sctp_get_next_param(mat, offset,
4906 			    (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4907 			if (plen != sizeof(struct sctp_ipv6addr_param) ||
4908 			    phdr == NULL) {
4909 				return (1);
4910 			}
4911 			p6 = (struct sctp_ipv6addr_param *)phdr;
4912 #ifdef INET6
4913 			memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4914 			    sizeof(p6->addr));
4915 #endif
4916 			sa_touse = (struct sockaddr *)&sin4;
4917 		}
4918 		if (sa_touse) {
4919 			/* ok, sa_touse points to one to check */
4920 			fnd = 0;
4921 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4922 				sa = (struct sockaddr *)&net->ro._l_addr;
4923 				if (sa->sa_family != sa_touse->sa_family) {
4924 					continue;
4925 				}
4926 				if (sa->sa_family == AF_INET) {
4927 					sa4 = (struct sockaddr_in *)sa;
4928 					if (sa4->sin_addr.s_addr ==
4929 					    sin4.sin_addr.s_addr) {
4930 						fnd = 1;
4931 						break;
4932 					}
4933 				}
4934 #ifdef INET6
4935 				if (sa->sa_family == AF_INET6) {
4936 					sa6 = (struct sockaddr_in6 *)sa;
4937 					if (SCTP6_ARE_ADDR_EQUAL(
4938 					    sa6, &sin6)) {
4939 						fnd = 1;
4940 						break;
4941 					}
4942 				}
4943 #endif
4944 			}
4945 			if (!fnd) {
4946 				/* New addr added! no need to look further */
4947 				return (1);
4948 			}
4949 		}
4950 		offset += SCTP_SIZE32(plen);
4951 		phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
4952 	}
4953 	return (0);
4954 }
4955 
4956 /*
4957  * Given a MBUF chain that was sent into us containing an INIT. Build a
4958  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
4959  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
4960  * message (i.e. the struct sctp_init_msg).
4961  */
4962 void
4963 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4964     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
4965     struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
4966 {
4967 	struct sctp_association *asoc;
4968 	struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
4969 	struct sctp_init_ack_chunk *initack;
4970 	struct sctp_adaptation_layer_indication *ali;
4971 	struct sctp_ecn_supported_param *ecn;
4972 	struct sctp_prsctp_supported_param *prsctp;
4973 	struct sctp_ecn_nonce_supported_param *ecn_nonce;
4974 	struct sctp_supported_chunk_types_param *pr_supported;
4975 	union sctp_sockstore store, store1, *over_addr;
4976 	struct sockaddr_in *sin, *to_sin;
4977 
4978 #ifdef INET6
4979 	struct sockaddr_in6 *sin6, *to_sin6;
4980 
4981 #endif
4982 	struct ip *iph;
4983 
4984 #ifdef INET6
4985 	struct ip6_hdr *ip6;
4986 
4987 #endif
4988 	struct sockaddr *to;
4989 	struct sctp_state_cookie stc;
4990 	struct sctp_nets *net = NULL;
4991 	uint8_t *signature = NULL;
4992 	int cnt_inits_to = 0;
4993 	uint16_t his_limit, i_want;
4994 	int abort_flag, padval;
4995 	int num_ext;
4996 	int p_len;
4997 	int nat_friendly = 0;
4998 	struct socket *so;
4999 
5000 	if (stcb)
5001 		asoc = &stcb->asoc;
5002 	else
5003 		asoc = NULL;
5004 	mp_last = NULL;
5005 	if ((asoc != NULL) &&
5006 	    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
5007 	    (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
5008 		/* new addresses, out of here in non-cookie-wait states */
5009 		/*
5010 		 * Send a ABORT, we don't add the new address error clause
5011 		 * though we even set the T bit and copy in the 0 tag.. this
5012 		 * looks no different than if no listener was present.
5013 		 */
5014 		sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
5015 		return;
5016 	}
5017 	abort_flag = 0;
5018 	op_err = sctp_arethere_unrecognized_parameters(init_pkt,
5019 	    (offset + sizeof(struct sctp_init_chunk)),
5020 	    &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
5021 	if (abort_flag) {
5022 do_a_abort:
5023 		sctp_send_abort(init_pkt, iphlen, sh,
5024 		    init_chk->init.initiate_tag, op_err, vrf_id, port);
5025 		return;
5026 	}
5027 	m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
5028 	if (m == NULL) {
5029 		/* No memory, INIT timer will re-attempt. */
5030 		if (op_err)
5031 			sctp_m_freem(op_err);
5032 		return;
5033 	}
5034 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
5035 
5036 	/* the time I built cookie */
5037 	(void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
5038 
5039 	/* populate any tie tags */
5040 	if (asoc != NULL) {
5041 		/* unlock before tag selections */
5042 		stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5043 		stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5044 		stc.cookie_life = asoc->cookie_life;
5045 		net = asoc->primary_destination;
5046 	} else {
5047 		stc.tie_tag_my_vtag = 0;
5048 		stc.tie_tag_peer_vtag = 0;
5049 		/* life I will award this cookie */
5050 		stc.cookie_life = inp->sctp_ep.def_cookie_life;
5051 	}
5052 
5053 	/* copy in the ports for later check */
5054 	stc.myport = sh->dest_port;
5055 	stc.peerport = sh->src_port;
5056 
5057 	/*
5058 	 * If we wanted to honor cookie life extentions, we would add to
5059 	 * stc.cookie_life. For now we should NOT honor any extension
5060 	 */
5061 	stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5062 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5063 		struct inpcb *in_inp;
5064 
5065 		/* Its a V6 socket */
5066 		in_inp = (struct inpcb *)inp;
5067 		stc.ipv6_addr_legal = 1;
5068 		/* Now look at the binding flag to see if V4 will be legal */
5069 		if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
5070 			stc.ipv4_addr_legal = 1;
5071 		} else {
5072 			/* V4 addresses are NOT legal on the association */
5073 			stc.ipv4_addr_legal = 0;
5074 		}
5075 	} else {
5076 		/* Its a V4 socket, no - V6 */
5077 		stc.ipv4_addr_legal = 1;
5078 		stc.ipv6_addr_legal = 0;
5079 	}
5080 
5081 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
5082 	stc.ipv4_scope = 1;
5083 #else
5084 	stc.ipv4_scope = 0;
5085 #endif
5086 	/* now for scope setup */
5087 	memset((caddr_t)&store, 0, sizeof(store));
5088 	memset((caddr_t)&store1, 0, sizeof(store1));
5089 	sin = &store.sin;
5090 	to_sin = &store1.sin;
5091 #ifdef INET6
5092 	sin6 = &store.sin6;
5093 	to_sin6 = &store1.sin6;
5094 #endif
5095 	iph = mtod(init_pkt, struct ip *);
5096 	/* establish the to_addr's */
5097 	switch (iph->ip_v) {
5098 	case IPVERSION:
5099 		to_sin->sin_port = sh->dest_port;
5100 		to_sin->sin_family = AF_INET;
5101 		to_sin->sin_len = sizeof(struct sockaddr_in);
5102 		to_sin->sin_addr = iph->ip_dst;
5103 		break;
5104 #ifdef INET6
5105 	case IPV6_VERSION >> 4:
5106 		ip6 = mtod(init_pkt, struct ip6_hdr *);
5107 		to_sin6->sin6_addr = ip6->ip6_dst;
5108 		to_sin6->sin6_scope_id = 0;
5109 		to_sin6->sin6_port = sh->dest_port;
5110 		to_sin6->sin6_family = AF_INET6;
5111 		to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5112 		break;
5113 #endif
5114 	default:
5115 		goto do_a_abort;
5116 		break;
5117 	};
5118 
5119 	if (net == NULL) {
5120 		to = (struct sockaddr *)&store;
5121 		switch (iph->ip_v) {
5122 		case IPVERSION:
5123 			{
5124 				sin->sin_family = AF_INET;
5125 				sin->sin_len = sizeof(struct sockaddr_in);
5126 				sin->sin_port = sh->src_port;
5127 				sin->sin_addr = iph->ip_src;
5128 				/* lookup address */
5129 				stc.address[0] = sin->sin_addr.s_addr;
5130 				stc.address[1] = 0;
5131 				stc.address[2] = 0;
5132 				stc.address[3] = 0;
5133 				stc.addr_type = SCTP_IPV4_ADDRESS;
5134 				/* local from address */
5135 				stc.laddress[0] = to_sin->sin_addr.s_addr;
5136 				stc.laddress[1] = 0;
5137 				stc.laddress[2] = 0;
5138 				stc.laddress[3] = 0;
5139 				stc.laddr_type = SCTP_IPV4_ADDRESS;
5140 				/* scope_id is only for v6 */
5141 				stc.scope_id = 0;
5142 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5143 				if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5144 					stc.ipv4_scope = 1;
5145 				}
5146 #else
5147 				stc.ipv4_scope = 1;
5148 #endif				/* SCTP_DONT_DO_PRIVADDR_SCOPE */
5149 				/* Must use the address in this case */
5150 				if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5151 					stc.loopback_scope = 1;
5152 					stc.ipv4_scope = 1;
5153 					stc.site_scope = 1;
5154 					stc.local_scope = 0;
5155 				}
5156 				break;
5157 			}
5158 #ifdef INET6
5159 		case IPV6_VERSION >> 4:
5160 			{
5161 				ip6 = mtod(init_pkt, struct ip6_hdr *);
5162 				sin6->sin6_family = AF_INET6;
5163 				sin6->sin6_len = sizeof(struct sockaddr_in6);
5164 				sin6->sin6_port = sh->src_port;
5165 				sin6->sin6_addr = ip6->ip6_src;
5166 				/* lookup address */
5167 				memcpy(&stc.address, &sin6->sin6_addr,
5168 				    sizeof(struct in6_addr));
5169 				sin6->sin6_scope_id = 0;
5170 				stc.addr_type = SCTP_IPV6_ADDRESS;
5171 				stc.scope_id = 0;
5172 				if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5173 					/*
5174 					 * FIX ME: does this have scope from
5175 					 * rcvif?
5176 					 */
5177 					(void)sa6_recoverscope(sin6);
5178 					stc.scope_id = sin6->sin6_scope_id;
5179 					sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5180 					stc.loopback_scope = 1;
5181 					stc.local_scope = 0;
5182 					stc.site_scope = 1;
5183 					stc.ipv4_scope = 1;
5184 				} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5185 					/*
5186 					 * If the new destination is a
5187 					 * LINK_LOCAL we must have common
5188 					 * both site and local scope. Don't
5189 					 * set local scope though since we
5190 					 * must depend on the source to be
5191 					 * added implicitly. We cannot
5192 					 * assure just because we share one
5193 					 * link that all links are common.
5194 					 */
5195 					stc.local_scope = 0;
5196 					stc.site_scope = 1;
5197 					stc.ipv4_scope = 1;
5198 					/*
5199 					 * we start counting for the private
5200 					 * address stuff at 1. since the
5201 					 * link local we source from won't
5202 					 * show up in our scoped count.
5203 					 */
5204 					cnt_inits_to = 1;
5205 					/*
5206 					 * pull out the scope_id from
5207 					 * incoming pkt
5208 					 */
5209 					/*
5210 					 * FIX ME: does this have scope from
5211 					 * rcvif?
5212 					 */
5213 					(void)sa6_recoverscope(sin6);
5214 					stc.scope_id = sin6->sin6_scope_id;
5215 					sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5216 				} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5217 					/*
5218 					 * If the new destination is
5219 					 * SITE_LOCAL then we must have site
5220 					 * scope in common.
5221 					 */
5222 					stc.site_scope = 1;
5223 				}
5224 				memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5225 				stc.laddr_type = SCTP_IPV6_ADDRESS;
5226 				break;
5227 			}
5228 #endif
5229 		default:
5230 			/* TSNH */
5231 			goto do_a_abort;
5232 			break;
5233 		}
5234 	} else {
5235 		/* set the scope per the existing tcb */
5236 
5237 #ifdef INET6
5238 		struct sctp_nets *lnet;
5239 
5240 #endif
5241 
5242 		stc.loopback_scope = asoc->loopback_scope;
5243 		stc.ipv4_scope = asoc->ipv4_local_scope;
5244 		stc.site_scope = asoc->site_scope;
5245 		stc.local_scope = asoc->local_scope;
5246 #ifdef INET6
5247 		/* Why do we not consider IPv4 LL addresses? */
5248 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5249 			if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5250 				if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5251 					/*
5252 					 * if we have a LL address, start
5253 					 * counting at 1.
5254 					 */
5255 					cnt_inits_to = 1;
5256 				}
5257 			}
5258 		}
5259 #endif
5260 		/* use the net pointer */
5261 		to = (struct sockaddr *)&net->ro._l_addr;
5262 		switch (to->sa_family) {
5263 		case AF_INET:
5264 			sin = (struct sockaddr_in *)to;
5265 			stc.address[0] = sin->sin_addr.s_addr;
5266 			stc.address[1] = 0;
5267 			stc.address[2] = 0;
5268 			stc.address[3] = 0;
5269 			stc.addr_type = SCTP_IPV4_ADDRESS;
5270 			if (net->src_addr_selected == 0) {
5271 				/*
5272 				 * strange case here, the INIT should have
5273 				 * did the selection.
5274 				 */
5275 				net->ro._s_addr = sctp_source_address_selection(inp,
5276 				    stcb, (sctp_route_t *) & net->ro,
5277 				    net, 0, vrf_id);
5278 				if (net->ro._s_addr == NULL)
5279 					return;
5280 
5281 				net->src_addr_selected = 1;
5282 
5283 			}
5284 			stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5285 			stc.laddress[1] = 0;
5286 			stc.laddress[2] = 0;
5287 			stc.laddress[3] = 0;
5288 			stc.laddr_type = SCTP_IPV4_ADDRESS;
5289 			break;
5290 #ifdef INET6
5291 		case AF_INET6:
5292 			sin6 = (struct sockaddr_in6 *)to;
5293 			memcpy(&stc.address, &sin6->sin6_addr,
5294 			    sizeof(struct in6_addr));
5295 			stc.addr_type = SCTP_IPV6_ADDRESS;
5296 			if (net->src_addr_selected == 0) {
5297 				/*
5298 				 * strange case here, the INIT should have
5299 				 * did the selection.
5300 				 */
5301 				net->ro._s_addr = sctp_source_address_selection(inp,
5302 				    stcb, (sctp_route_t *) & net->ro,
5303 				    net, 0, vrf_id);
5304 				if (net->ro._s_addr == NULL)
5305 					return;
5306 
5307 				net->src_addr_selected = 1;
5308 			}
5309 			memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5310 			    sizeof(struct in6_addr));
5311 			stc.laddr_type = SCTP_IPV6_ADDRESS;
5312 			break;
5313 #endif
5314 		}
5315 	}
5316 	/* Now lets put the SCTP header in place */
5317 	initack = mtod(m, struct sctp_init_ack_chunk *);
5318 	/* Save it off for quick ref */
5319 	stc.peers_vtag = init_chk->init.initiate_tag;
5320 	/* who are we */
5321 	memcpy(stc.identification, SCTP_VERSION_STRING,
5322 	    min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5323 	/* now the chunk header */
5324 	initack->ch.chunk_type = SCTP_INITIATION_ACK;
5325 	initack->ch.chunk_flags = 0;
5326 	/* fill in later from mbuf we build */
5327 	initack->ch.chunk_length = 0;
5328 	/* place in my tag */
5329 	if ((asoc != NULL) &&
5330 	    ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5331 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5332 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5333 		/* re-use the v-tags and init-seq here */
5334 		initack->init.initiate_tag = htonl(asoc->my_vtag);
5335 		initack->init.initial_tsn = htonl(asoc->init_seq_number);
5336 	} else {
5337 		uint32_t vtag, itsn;
5338 
5339 		if (hold_inp_lock) {
5340 			SCTP_INP_INCR_REF(inp);
5341 			SCTP_INP_RUNLOCK(inp);
5342 		}
5343 		if (asoc) {
5344 			atomic_add_int(&asoc->refcnt, 1);
5345 			SCTP_TCB_UNLOCK(stcb);
5346 	new_tag:
5347 			vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5348 			if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5349 				/*
5350 				 * Got a duplicate vtag on some guy behind a
5351 				 * nat make sure we don't use it.
5352 				 */
5353 				goto new_tag;
5354 			}
5355 			initack->init.initiate_tag = htonl(vtag);
5356 			/* get a TSN to use too */
5357 			itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5358 			initack->init.initial_tsn = htonl(itsn);
5359 			SCTP_TCB_LOCK(stcb);
5360 			atomic_add_int(&asoc->refcnt, -1);
5361 		} else {
5362 			vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5363 			initack->init.initiate_tag = htonl(vtag);
5364 			/* get a TSN to use too */
5365 			initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5366 		}
5367 		if (hold_inp_lock) {
5368 			SCTP_INP_RLOCK(inp);
5369 			SCTP_INP_DECR_REF(inp);
5370 		}
5371 	}
5372 	/* save away my tag to */
5373 	stc.my_vtag = initack->init.initiate_tag;
5374 
5375 	/* set up some of the credits. */
5376 	so = inp->sctp_socket;
5377 	if (so == NULL) {
5378 		/* memory problem */
5379 		sctp_m_freem(m);
5380 		return;
5381 	} else {
5382 		initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5383 	}
5384 	/* set what I want */
5385 	his_limit = ntohs(init_chk->init.num_inbound_streams);
5386 	/* choose what I want */
5387 	if (asoc != NULL) {
5388 		if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5389 			i_want = asoc->streamoutcnt;
5390 		} else {
5391 			i_want = inp->sctp_ep.pre_open_stream_count;
5392 		}
5393 	} else {
5394 		i_want = inp->sctp_ep.pre_open_stream_count;
5395 	}
5396 	if (his_limit < i_want) {
5397 		/* I Want more :< */
5398 		initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5399 	} else {
5400 		/* I can have what I want :> */
5401 		initack->init.num_outbound_streams = htons(i_want);
5402 	}
5403 	/* tell him his limt. */
5404 	initack->init.num_inbound_streams =
5405 	    htons(inp->sctp_ep.max_open_streams_intome);
5406 
5407 	/* adaptation layer indication parameter */
5408 	ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack));
5409 	ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5410 	ali->ph.param_length = htons(sizeof(*ali));
5411 	ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5412 	SCTP_BUF_LEN(m) += sizeof(*ali);
5413 	ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
5414 
5415 	/* ECN parameter */
5416 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
5417 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5418 		ecn->ph.param_length = htons(sizeof(*ecn));
5419 		SCTP_BUF_LEN(m) += sizeof(*ecn);
5420 
5421 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5422 		    sizeof(*ecn));
5423 	} else {
5424 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5425 	}
5426 	/* And now tell the peer we do  pr-sctp */
5427 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5428 	prsctp->ph.param_length = htons(sizeof(*prsctp));
5429 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
5430 	if (nat_friendly) {
5431 		/* Add NAT friendly parameter */
5432 		struct sctp_paramhdr *ph;
5433 
5434 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5435 		ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5436 		ph->param_length = htons(sizeof(struct sctp_paramhdr));
5437 		SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
5438 	}
5439 	/* And now tell the peer we do all the extensions */
5440 	pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5441 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5442 	num_ext = 0;
5443 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5444 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5445 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5446 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5447 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5448 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5449 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5450 	/*
5451 	 * EY  if the sysctl variable is set, tell the assoc. initiator that
5452 	 * we do nr_sack
5453 	 */
5454 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
5455 		pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5456 	p_len = sizeof(*pr_supported) + num_ext;
5457 	pr_supported->ph.param_length = htons(p_len);
5458 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5459 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5460 
5461 	/* ECN nonce: And now tell the peer we support ECN nonce */
5462 	if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
5463 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
5464 		    ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
5465 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
5466 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
5467 		SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
5468 	}
5469 	/* add authentication parameters */
5470 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5471 		struct sctp_auth_random *randp;
5472 		struct sctp_auth_hmac_algo *hmacs;
5473 		struct sctp_auth_chunk_list *chunks;
5474 		uint16_t random_len;
5475 
5476 		/* generate and add RANDOM parameter */
5477 		random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5478 		randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5479 		randp->ph.param_type = htons(SCTP_RANDOM);
5480 		p_len = sizeof(*randp) + random_len;
5481 		randp->ph.param_length = htons(p_len);
5482 		SCTP_READ_RANDOM(randp->random_data, random_len);
5483 		/* zero out any padding required */
5484 		bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5485 		SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5486 
5487 		/* add HMAC_ALGO parameter */
5488 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5489 		p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5490 		    (uint8_t *) hmacs->hmac_ids);
5491 		if (p_len > 0) {
5492 			p_len += sizeof(*hmacs);
5493 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5494 			hmacs->ph.param_length = htons(p_len);
5495 			/* zero out any padding required */
5496 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5497 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5498 		}
5499 		/* add CHUNKS parameter */
5500 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5501 		p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5502 		    chunks->chunk_types);
5503 		if (p_len > 0) {
5504 			p_len += sizeof(*chunks);
5505 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5506 			chunks->ph.param_length = htons(p_len);
5507 			/* zero out any padding required */
5508 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5509 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5510 		}
5511 	}
5512 	m_at = m;
5513 	/* now the addresses */
5514 	{
5515 		struct sctp_scoping scp;
5516 
5517 		/*
5518 		 * To optimize this we could put the scoping stuff into a
5519 		 * structure and remove the individual uint8's from the stc
5520 		 * structure. Then we could just sifa in the address within
5521 		 * the stc.. but for now this is a quick hack to get the
5522 		 * address stuff teased apart.
5523 		 */
5524 		scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5525 		scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5526 		scp.loopback_scope = stc.loopback_scope;
5527 		scp.ipv4_local_scope = stc.ipv4_scope;
5528 		scp.local_scope = stc.local_scope;
5529 		scp.site_scope = stc.site_scope;
5530 		m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
5531 	}
5532 
5533 	/* tack on the operational error if present */
5534 	if (op_err) {
5535 		struct mbuf *ol;
5536 		int llen;
5537 
5538 		llen = 0;
5539 		ol = op_err;
5540 		while (ol) {
5541 			llen += SCTP_BUF_LEN(ol);
5542 			ol = SCTP_BUF_NEXT(ol);
5543 		}
5544 		if (llen % 4) {
5545 			/* must add a pad to the param */
5546 			uint32_t cpthis = 0;
5547 			int padlen;
5548 
5549 			padlen = 4 - (llen % 4);
5550 			m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5551 		}
5552 		while (SCTP_BUF_NEXT(m_at) != NULL) {
5553 			m_at = SCTP_BUF_NEXT(m_at);
5554 		}
5555 		SCTP_BUF_NEXT(m_at) = op_err;
5556 		while (SCTP_BUF_NEXT(m_at) != NULL) {
5557 			m_at = SCTP_BUF_NEXT(m_at);
5558 		}
5559 	}
5560 	/* pre-calulate the size and update pkt header and chunk header */
5561 	p_len = 0;
5562 	for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5563 		p_len += SCTP_BUF_LEN(m_tmp);
5564 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5565 			/* m_tmp should now point to last one */
5566 			break;
5567 		}
5568 	}
5569 
5570 	/* Now we must build a cookie */
5571 	m_cookie = sctp_add_cookie(inp, init_pkt, offset, m, 0, &stc, &signature);
5572 	if (m_cookie == NULL) {
5573 		/* memory problem */
5574 		sctp_m_freem(m);
5575 		return;
5576 	}
5577 	/* Now append the cookie to the end and update the space/size */
5578 	SCTP_BUF_NEXT(m_tmp) = m_cookie;
5579 
5580 	for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5581 		p_len += SCTP_BUF_LEN(m_tmp);
5582 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5583 			/* m_tmp should now point to last one */
5584 			mp_last = m_tmp;
5585 			break;
5586 		}
5587 	}
5588 	/*
5589 	 * Place in the size, but we don't include the last pad (if any) in
5590 	 * the INIT-ACK.
5591 	 */
5592 	initack->ch.chunk_length = htons(p_len);
5593 
5594 	/*
5595 	 * Time to sign the cookie, we don't sign over the cookie signature
5596 	 * though thus we set trailer.
5597 	 */
5598 	(void)sctp_hmac_m(SCTP_HMAC,
5599 	    (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
5600 	    SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
5601 	    (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
5602 	/*
5603 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
5604 	 * here since the timer will drive a retranmission.
5605 	 */
5606 	padval = p_len % 4;
5607 	if ((padval) && (mp_last)) {
5608 		/* see my previous comments on mp_last */
5609 		int ret;
5610 
5611 		ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
5612 		if (ret) {
5613 			/* Houston we have a problem, no space */
5614 			sctp_m_freem(m);
5615 			return;
5616 		}
5617 		p_len += padval;
5618 	}
5619 	if (stc.loopback_scope) {
5620 		over_addr = &store1;
5621 	} else {
5622 		over_addr = NULL;
5623 	}
5624 
5625 	(void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
5626 	    0, NULL, 0,
5627 	    inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
5628 	    port, SCTP_SO_NOT_LOCKED, over_addr);
5629 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5630 }
5631 
5632 
5633 void
5634 sctp_insert_on_wheel(struct sctp_tcb *stcb,
5635     struct sctp_association *asoc,
5636     struct sctp_stream_out *strq, int holds_lock)
5637 {
5638 	if (holds_lock == 0) {
5639 		SCTP_TCB_SEND_LOCK(stcb);
5640 	}
5641 	if ((strq->next_spoke.tqe_next == NULL) &&
5642 	    (strq->next_spoke.tqe_prev == NULL)) {
5643 		TAILQ_INSERT_TAIL(&asoc->out_wheel, strq, next_spoke);
5644 	}
5645 	if (holds_lock == 0) {
5646 		SCTP_TCB_SEND_UNLOCK(stcb);
5647 	}
5648 }
5649 
5650 void
5651 sctp_remove_from_wheel(struct sctp_tcb *stcb,
5652     struct sctp_association *asoc,
5653     struct sctp_stream_out *strq,
5654     int holds_lock)
5655 {
5656 	/* take off and then setup so we know it is not on the wheel */
5657 	if (holds_lock == 0) {
5658 		SCTP_TCB_SEND_LOCK(stcb);
5659 	}
5660 	if (TAILQ_EMPTY(&strq->outqueue)) {
5661 		if (asoc->last_out_stream == strq) {
5662 			asoc->last_out_stream = TAILQ_PREV(asoc->last_out_stream, sctpwheel_listhead, next_spoke);
5663 			if (asoc->last_out_stream == NULL) {
5664 				asoc->last_out_stream = TAILQ_LAST(&asoc->out_wheel, sctpwheel_listhead);
5665 			}
5666 			if (asoc->last_out_stream == strq) {
5667 				asoc->last_out_stream = NULL;
5668 			}
5669 		}
5670 		TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
5671 		strq->next_spoke.tqe_next = NULL;
5672 		strq->next_spoke.tqe_prev = NULL;
5673 	}
5674 	if (holds_lock == 0) {
5675 		SCTP_TCB_SEND_UNLOCK(stcb);
5676 	}
5677 }
5678 
5679 static void
5680 sctp_prune_prsctp(struct sctp_tcb *stcb,
5681     struct sctp_association *asoc,
5682     struct sctp_sndrcvinfo *srcv,
5683     int dataout)
5684 {
5685 	int freed_spc = 0;
5686 	struct sctp_tmit_chunk *chk, *nchk;
5687 
5688 	SCTP_TCB_LOCK_ASSERT(stcb);
5689 	if ((asoc->peer_supports_prsctp) &&
5690 	    (asoc->sent_queue_cnt_removeable > 0)) {
5691 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5692 			/*
5693 			 * Look for chunks marked with the PR_SCTP flag AND
5694 			 * the buffer space flag. If the one being sent is
5695 			 * equal or greater priority then purge the old one
5696 			 * and free some space.
5697 			 */
5698 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5699 				/*
5700 				 * This one is PR-SCTP AND buffer space
5701 				 * limited type
5702 				 */
5703 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5704 					/*
5705 					 * Lower numbers equates to higher
5706 					 * priority so if the one we are
5707 					 * looking at has a larger or equal
5708 					 * priority we want to drop the data
5709 					 * and NOT retransmit it.
5710 					 */
5711 					if (chk->data) {
5712 						/*
5713 						 * We release the book_size
5714 						 * if the mbuf is here
5715 						 */
5716 						int ret_spc;
5717 						int cause;
5718 
5719 						if (chk->sent > SCTP_DATAGRAM_UNSENT)
5720 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
5721 						else
5722 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
5723 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5724 						    cause,
5725 						    SCTP_SO_LOCKED);
5726 						freed_spc += ret_spc;
5727 						if (freed_spc >= dataout) {
5728 							return;
5729 						}
5730 					}	/* if chunk was present */
5731 				}	/* if of sufficent priority */
5732 			}	/* if chunk has enabled */
5733 		}		/* tailqforeach */
5734 
5735 		chk = TAILQ_FIRST(&asoc->send_queue);
5736 		while (chk) {
5737 			nchk = TAILQ_NEXT(chk, sctp_next);
5738 			/* Here we must move to the sent queue and mark */
5739 			if (PR_SCTP_TTL_ENABLED(chk->flags)) {
5740 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5741 					if (chk->data) {
5742 						/*
5743 						 * We release the book_size
5744 						 * if the mbuf is here
5745 						 */
5746 						int ret_spc;
5747 
5748 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5749 						    SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
5750 						    SCTP_SO_LOCKED);
5751 
5752 						freed_spc += ret_spc;
5753 						if (freed_spc >= dataout) {
5754 							return;
5755 						}
5756 					}	/* end if chk->data */
5757 				}	/* end if right class */
5758 			}	/* end if chk pr-sctp */
5759 			chk = nchk;
5760 		}		/* end while (chk) */
5761 	}			/* if enabled in asoc */
5762 }
5763 
5764 int
5765 sctp_get_frag_point(struct sctp_tcb *stcb,
5766     struct sctp_association *asoc)
5767 {
5768 	int siz, ovh;
5769 
5770 	/*
5771 	 * For endpoints that have both v6 and v4 addresses we must reserve
5772 	 * room for the ipv6 header, for those that are only dealing with V4
5773 	 * we use a larger frag point.
5774 	 */
5775 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5776 		ovh = SCTP_MED_OVERHEAD;
5777 	} else {
5778 		ovh = SCTP_MED_V4_OVERHEAD;
5779 	}
5780 
5781 	if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
5782 		siz = asoc->smallest_mtu - ovh;
5783 	else
5784 		siz = (stcb->asoc.sctp_frag_point - ovh);
5785 	/*
5786 	 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
5787 	 */
5788 	/* A data chunk MUST fit in a cluster */
5789 	/* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
5790 	/* } */
5791 
5792 	/* adjust for an AUTH chunk if DATA requires auth */
5793 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
5794 		siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5795 
5796 	if (siz % 4) {
5797 		/* make it an even word boundary please */
5798 		siz -= (siz % 4);
5799 	}
5800 	return (siz);
5801 }
5802 
5803 static void
5804 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
5805 {
5806 	sp->pr_sctp_on = 0;
5807 	/*
5808 	 * We assume that the user wants PR_SCTP_TTL if the user provides a
5809 	 * positive lifetime but does not specify any PR_SCTP policy. This
5810 	 * is a BAD assumption and causes problems at least with the
5811 	 * U-Vancovers MPI folks. I will change this to be no policy means
5812 	 * NO PR-SCTP.
5813 	 */
5814 	if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
5815 		sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
5816 		sp->pr_sctp_on = 1;
5817 	} else {
5818 		return;
5819 	}
5820 	switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
5821 	case CHUNK_FLAGS_PR_SCTP_BUF:
5822 		/*
5823 		 * Time to live is a priority stored in tv_sec when doing
5824 		 * the buffer drop thing.
5825 		 */
5826 		sp->ts.tv_sec = sp->timetolive;
5827 		sp->ts.tv_usec = 0;
5828 		break;
5829 	case CHUNK_FLAGS_PR_SCTP_TTL:
5830 		{
5831 			struct timeval tv;
5832 
5833 			(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5834 			tv.tv_sec = sp->timetolive / 1000;
5835 			tv.tv_usec = (sp->timetolive * 1000) % 1000000;
5836 			/*
5837 			 * TODO sctp_constants.h needs alternative time
5838 			 * macros when _KERNEL is undefined.
5839 			 */
5840 			timevaladd(&sp->ts, &tv);
5841 		}
5842 		break;
5843 	case CHUNK_FLAGS_PR_SCTP_RTX:
5844 		/*
5845 		 * Time to live is a the number or retransmissions stored in
5846 		 * tv_sec.
5847 		 */
5848 		sp->ts.tv_sec = sp->timetolive;
5849 		sp->ts.tv_usec = 0;
5850 		break;
5851 	default:
5852 		SCTPDBG(SCTP_DEBUG_USRREQ1,
5853 		    "Unknown PR_SCTP policy %u.\n",
5854 		    PR_SCTP_POLICY(sp->sinfo_flags));
5855 		break;
5856 	}
5857 }
5858 
5859 static int
5860 sctp_msg_append(struct sctp_tcb *stcb,
5861     struct sctp_nets *net,
5862     struct mbuf *m,
5863     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5864 {
5865 	int error = 0, holds_lock;
5866 	struct mbuf *at;
5867 	struct sctp_stream_queue_pending *sp = NULL;
5868 	struct sctp_stream_out *strm;
5869 
5870 	/*
5871 	 * Given an mbuf chain, put it into the association send queue and
5872 	 * place it on the wheel
5873 	 */
5874 	holds_lock = hold_stcb_lock;
5875 	if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5876 		/* Invalid stream number */
5877 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5878 		error = EINVAL;
5879 		goto out_now;
5880 	}
5881 	if ((stcb->asoc.stream_locked) &&
5882 	    (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5883 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5884 		error = EINVAL;
5885 		goto out_now;
5886 	}
5887 	strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5888 	/* Now can we send this? */
5889 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5890 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5891 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5892 	    (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5893 		/* got data while shutting down */
5894 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
5895 		error = ECONNRESET;
5896 		goto out_now;
5897 	}
5898 	sctp_alloc_a_strmoq(stcb, sp);
5899 	if (sp == NULL) {
5900 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5901 		error = ENOMEM;
5902 		goto out_now;
5903 	}
5904 	sp->sinfo_flags = srcv->sinfo_flags;
5905 	sp->timetolive = srcv->sinfo_timetolive;
5906 	sp->ppid = srcv->sinfo_ppid;
5907 	sp->context = srcv->sinfo_context;
5908 	sp->strseq = 0;
5909 	if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5910 		sp->net = net;
5911 	} else {
5912 		sp->net = stcb->asoc.primary_destination;
5913 	}
5914 	atomic_add_int(&sp->net->ref_count, 1);
5915 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5916 	sp->stream = srcv->sinfo_stream;
5917 	sp->msg_is_complete = 1;
5918 	sp->sender_all_done = 1;
5919 	sp->some_taken = 0;
5920 	sp->data = m;
5921 	sp->tail_mbuf = NULL;
5922 	sp->length = 0;
5923 	at = m;
5924 	sctp_set_prsctp_policy(sp);
5925 	/*
5926 	 * We could in theory (for sendall) sifa the length in, but we would
5927 	 * still have to hunt through the chain since we need to setup the
5928 	 * tail_mbuf
5929 	 */
5930 	while (at) {
5931 		if (SCTP_BUF_NEXT(at) == NULL)
5932 			sp->tail_mbuf = at;
5933 		sp->length += SCTP_BUF_LEN(at);
5934 		at = SCTP_BUF_NEXT(at);
5935 	}
5936 	SCTP_TCB_SEND_LOCK(stcb);
5937 	sctp_snd_sb_alloc(stcb, sp->length);
5938 	atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
5939 	TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
5940 	if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
5941 		sp->strseq = strm->next_sequence_sent;
5942 		strm->next_sequence_sent++;
5943 	}
5944 	if ((strm->next_spoke.tqe_next == NULL) &&
5945 	    (strm->next_spoke.tqe_prev == NULL)) {
5946 		/* Not on wheel, insert */
5947 		sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
5948 	}
5949 	m = NULL;
5950 	SCTP_TCB_SEND_UNLOCK(stcb);
5951 out_now:
5952 	if (m) {
5953 		sctp_m_freem(m);
5954 	}
5955 	return (error);
5956 }
5957 
5958 
5959 static struct mbuf *
5960 sctp_copy_mbufchain(struct mbuf *clonechain,
5961     struct mbuf *outchain,
5962     struct mbuf **endofchain,
5963     int can_take_mbuf,
5964     int sizeofcpy,
5965     uint8_t copy_by_ref)
5966 {
5967 	struct mbuf *m;
5968 	struct mbuf *appendchain;
5969 	caddr_t cp;
5970 	int len;
5971 
5972 	if (endofchain == NULL) {
5973 		/* error */
5974 error_out:
5975 		if (outchain)
5976 			sctp_m_freem(outchain);
5977 		return (NULL);
5978 	}
5979 	if (can_take_mbuf) {
5980 		appendchain = clonechain;
5981 	} else {
5982 		if (!copy_by_ref &&
5983 		    (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
5984 		    ) {
5985 			/* Its not in a cluster */
5986 			if (*endofchain == NULL) {
5987 				/* lets get a mbuf cluster */
5988 				if (outchain == NULL) {
5989 					/* This is the general case */
5990 			new_mbuf:
5991 					outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5992 					if (outchain == NULL) {
5993 						goto error_out;
5994 					}
5995 					SCTP_BUF_LEN(outchain) = 0;
5996 					*endofchain = outchain;
5997 					/* get the prepend space */
5998 					SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
5999 				} else {
6000 					/*
6001 					 * We really should not get a NULL
6002 					 * in endofchain
6003 					 */
6004 					/* find end */
6005 					m = outchain;
6006 					while (m) {
6007 						if (SCTP_BUF_NEXT(m) == NULL) {
6008 							*endofchain = m;
6009 							break;
6010 						}
6011 						m = SCTP_BUF_NEXT(m);
6012 					}
6013 					/* sanity */
6014 					if (*endofchain == NULL) {
6015 						/*
6016 						 * huh, TSNH XXX maybe we
6017 						 * should panic
6018 						 */
6019 						sctp_m_freem(outchain);
6020 						goto new_mbuf;
6021 					}
6022 				}
6023 				/* get the new end of length */
6024 				len = M_TRAILINGSPACE(*endofchain);
6025 			} else {
6026 				/* how much is left at the end? */
6027 				len = M_TRAILINGSPACE(*endofchain);
6028 			}
6029 			/* Find the end of the data, for appending */
6030 			cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
6031 
6032 			/* Now lets copy it out */
6033 			if (len >= sizeofcpy) {
6034 				/* It all fits, copy it in */
6035 				m_copydata(clonechain, 0, sizeofcpy, cp);
6036 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6037 			} else {
6038 				/* fill up the end of the chain */
6039 				if (len > 0) {
6040 					m_copydata(clonechain, 0, len, cp);
6041 					SCTP_BUF_LEN((*endofchain)) += len;
6042 					/* now we need another one */
6043 					sizeofcpy -= len;
6044 				}
6045 				m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6046 				if (m == NULL) {
6047 					/* We failed */
6048 					goto error_out;
6049 				}
6050 				SCTP_BUF_NEXT((*endofchain)) = m;
6051 				*endofchain = m;
6052 				cp = mtod((*endofchain), caddr_t);
6053 				m_copydata(clonechain, len, sizeofcpy, cp);
6054 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6055 			}
6056 			return (outchain);
6057 		} else {
6058 			/* copy the old fashion way */
6059 			appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
6060 #ifdef SCTP_MBUF_LOGGING
6061 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6062 				struct mbuf *mat;
6063 
6064 				mat = appendchain;
6065 				while (mat) {
6066 					if (SCTP_BUF_IS_EXTENDED(mat)) {
6067 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6068 					}
6069 					mat = SCTP_BUF_NEXT(mat);
6070 				}
6071 			}
6072 #endif
6073 		}
6074 	}
6075 	if (appendchain == NULL) {
6076 		/* error */
6077 		if (outchain)
6078 			sctp_m_freem(outchain);
6079 		return (NULL);
6080 	}
6081 	if (outchain) {
6082 		/* tack on to the end */
6083 		if (*endofchain != NULL) {
6084 			SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6085 		} else {
6086 			m = outchain;
6087 			while (m) {
6088 				if (SCTP_BUF_NEXT(m) == NULL) {
6089 					SCTP_BUF_NEXT(m) = appendchain;
6090 					break;
6091 				}
6092 				m = SCTP_BUF_NEXT(m);
6093 			}
6094 		}
6095 		/*
6096 		 * save off the end and update the end-chain postion
6097 		 */
6098 		m = appendchain;
6099 		while (m) {
6100 			if (SCTP_BUF_NEXT(m) == NULL) {
6101 				*endofchain = m;
6102 				break;
6103 			}
6104 			m = SCTP_BUF_NEXT(m);
6105 		}
6106 		return (outchain);
6107 	} else {
6108 		/* save off the end and update the end-chain postion */
6109 		m = appendchain;
6110 		while (m) {
6111 			if (SCTP_BUF_NEXT(m) == NULL) {
6112 				*endofchain = m;
6113 				break;
6114 			}
6115 			m = SCTP_BUF_NEXT(m);
6116 		}
6117 		return (appendchain);
6118 	}
6119 }
6120 
6121 int
6122 sctp_med_chunk_output(struct sctp_inpcb *inp,
6123     struct sctp_tcb *stcb,
6124     struct sctp_association *asoc,
6125     int *num_out,
6126     int *reason_code,
6127     int control_only, int from_where,
6128     struct timeval *now, int *now_filled, int frag_point, int so_locked
6129 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6130     SCTP_UNUSED
6131 #endif
6132 );
6133 
6134 static void
6135 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6136     uint32_t val)
6137 {
6138 	struct sctp_copy_all *ca;
6139 	struct mbuf *m;
6140 	int ret = 0;
6141 	int added_control = 0;
6142 	int un_sent, do_chunk_output = 1;
6143 	struct sctp_association *asoc;
6144 
6145 	ca = (struct sctp_copy_all *)ptr;
6146 	if (ca->m == NULL) {
6147 		return;
6148 	}
6149 	if (ca->inp != inp) {
6150 		/* TSNH */
6151 		return;
6152 	}
6153 	if ((ca->m) && ca->sndlen) {
6154 		m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6155 		if (m == NULL) {
6156 			/* can't copy so we are done */
6157 			ca->cnt_failed++;
6158 			return;
6159 		}
6160 #ifdef SCTP_MBUF_LOGGING
6161 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6162 			struct mbuf *mat;
6163 
6164 			mat = m;
6165 			while (mat) {
6166 				if (SCTP_BUF_IS_EXTENDED(mat)) {
6167 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6168 				}
6169 				mat = SCTP_BUF_NEXT(mat);
6170 			}
6171 		}
6172 #endif
6173 	} else {
6174 		m = NULL;
6175 	}
6176 	SCTP_TCB_LOCK_ASSERT(stcb);
6177 	if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6178 		/* Abort this assoc with m as the user defined reason */
6179 		if (m) {
6180 			struct sctp_paramhdr *ph;
6181 
6182 			SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6183 			if (m) {
6184 				ph = mtod(m, struct sctp_paramhdr *);
6185 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6186 				ph->param_length = htons(ca->sndlen);
6187 			}
6188 			/*
6189 			 * We add one here to keep the assoc from
6190 			 * dis-appearing on us.
6191 			 */
6192 			atomic_add_int(&stcb->asoc.refcnt, 1);
6193 			sctp_abort_an_association(inp, stcb,
6194 			    SCTP_RESPONSE_TO_USER_REQ,
6195 			    m, SCTP_SO_NOT_LOCKED);
6196 			/*
6197 			 * sctp_abort_an_association calls sctp_free_asoc()
6198 			 * free association will NOT free it since we
6199 			 * incremented the refcnt .. we do this to prevent
6200 			 * it being freed and things getting tricky since we
6201 			 * could end up (from free_asoc) calling inpcb_free
6202 			 * which would get a recursive lock call to the
6203 			 * iterator lock.. But as a consequence of that the
6204 			 * stcb will return to us un-locked.. since
6205 			 * free_asoc returns with either no TCB or the TCB
6206 			 * unlocked, we must relock.. to unlock in the
6207 			 * iterator timer :-0
6208 			 */
6209 			SCTP_TCB_LOCK(stcb);
6210 			atomic_add_int(&stcb->asoc.refcnt, -1);
6211 			goto no_chunk_output;
6212 		}
6213 	} else {
6214 		if (m) {
6215 			ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
6216 			    &ca->sndrcv, 1);
6217 		}
6218 		asoc = &stcb->asoc;
6219 		if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6220 			/* shutdown this assoc */
6221 			int cnt;
6222 
6223 			cnt = sctp_is_there_unsent_data(stcb);
6224 
6225 			if (TAILQ_EMPTY(&asoc->send_queue) &&
6226 			    TAILQ_EMPTY(&asoc->sent_queue) &&
6227 			    (cnt == 0)) {
6228 				if (asoc->locked_on_sending) {
6229 					goto abort_anyway;
6230 				}
6231 				/*
6232 				 * there is nothing queued to send, so I'm
6233 				 * done...
6234 				 */
6235 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6236 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6237 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6238 					/*
6239 					 * only send SHUTDOWN the first time
6240 					 * through
6241 					 */
6242 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
6243 					if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6244 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6245 					}
6246 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6247 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6248 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6249 					    asoc->primary_destination);
6250 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6251 					    asoc->primary_destination);
6252 					added_control = 1;
6253 					do_chunk_output = 0;
6254 				}
6255 			} else {
6256 				/*
6257 				 * we still got (or just got) data to send,
6258 				 * so set SHUTDOWN_PENDING
6259 				 */
6260 				/*
6261 				 * XXX sockets draft says that SCTP_EOF
6262 				 * should be sent with no data.  currently,
6263 				 * we will allow user data to be sent first
6264 				 * and move to SHUTDOWN-PENDING
6265 				 */
6266 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6267 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6268 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6269 					if (asoc->locked_on_sending) {
6270 						/*
6271 						 * Locked to send out the
6272 						 * data
6273 						 */
6274 						struct sctp_stream_queue_pending *sp;
6275 
6276 						sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6277 						if (sp) {
6278 							if ((sp->length == 0) && (sp->msg_is_complete == 0))
6279 								asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6280 						}
6281 					}
6282 					asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6283 					if (TAILQ_EMPTY(&asoc->send_queue) &&
6284 					    TAILQ_EMPTY(&asoc->sent_queue) &&
6285 					    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6286 				abort_anyway:
6287 						atomic_add_int(&stcb->asoc.refcnt, 1);
6288 						sctp_abort_an_association(stcb->sctp_ep, stcb,
6289 						    SCTP_RESPONSE_TO_USER_REQ,
6290 						    NULL, SCTP_SO_NOT_LOCKED);
6291 						atomic_add_int(&stcb->asoc.refcnt, -1);
6292 						goto no_chunk_output;
6293 					}
6294 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6295 					    asoc->primary_destination);
6296 				}
6297 			}
6298 
6299 		}
6300 	}
6301 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6302 	    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6303 
6304 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6305 	    (stcb->asoc.total_flight > 0) &&
6306 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6307 	    ) {
6308 		do_chunk_output = 0;
6309 	}
6310 	if (do_chunk_output)
6311 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6312 	else if (added_control) {
6313 		int num_out = 0, reason = 0, now_filled = 0;
6314 		struct timeval now;
6315 		int frag_point;
6316 
6317 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6318 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6319 		    &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6320 	}
6321 no_chunk_output:
6322 	if (ret) {
6323 		ca->cnt_failed++;
6324 	} else {
6325 		ca->cnt_sent++;
6326 	}
6327 }
6328 
6329 static void
6330 sctp_sendall_completes(void *ptr, uint32_t val)
6331 {
6332 	struct sctp_copy_all *ca;
6333 
6334 	ca = (struct sctp_copy_all *)ptr;
6335 	/*
6336 	 * Do a notify here? Kacheong suggests that the notify be done at
6337 	 * the send time.. so you would push up a notification if any send
6338 	 * failed. Don't know if this is feasable since the only failures we
6339 	 * have is "memory" related and if you cannot get an mbuf to send
6340 	 * the data you surely can't get an mbuf to send up to notify the
6341 	 * user you can't send the data :->
6342 	 */
6343 
6344 	/* now free everything */
6345 	sctp_m_freem(ca->m);
6346 	SCTP_FREE(ca, SCTP_M_COPYAL);
6347 }
6348 
6349 
6350 #define	MC_ALIGN(m, len) do {						\
6351 	SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1));	\
6352 } while (0)
6353 
6354 
6355 
6356 static struct mbuf *
6357 sctp_copy_out_all(struct uio *uio, int len)
6358 {
6359 	struct mbuf *ret, *at;
6360 	int left, willcpy, cancpy, error;
6361 
6362 	ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6363 	if (ret == NULL) {
6364 		/* TSNH */
6365 		return (NULL);
6366 	}
6367 	left = len;
6368 	SCTP_BUF_LEN(ret) = 0;
6369 	/* save space for the data chunk header */
6370 	cancpy = M_TRAILINGSPACE(ret);
6371 	willcpy = min(cancpy, left);
6372 	at = ret;
6373 	while (left > 0) {
6374 		/* Align data to the end */
6375 		error = uiomove(mtod(at, caddr_t), willcpy, uio);
6376 		if (error) {
6377 	err_out_now:
6378 			sctp_m_freem(at);
6379 			return (NULL);
6380 		}
6381 		SCTP_BUF_LEN(at) = willcpy;
6382 		SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6383 		left -= willcpy;
6384 		if (left > 0) {
6385 			SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6386 			if (SCTP_BUF_NEXT(at) == NULL) {
6387 				goto err_out_now;
6388 			}
6389 			at = SCTP_BUF_NEXT(at);
6390 			SCTP_BUF_LEN(at) = 0;
6391 			cancpy = M_TRAILINGSPACE(at);
6392 			willcpy = min(cancpy, left);
6393 		}
6394 	}
6395 	return (ret);
6396 }
6397 
6398 static int
6399 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6400     struct sctp_sndrcvinfo *srcv)
6401 {
6402 	int ret;
6403 	struct sctp_copy_all *ca;
6404 
6405 	SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6406 	    SCTP_M_COPYAL);
6407 	if (ca == NULL) {
6408 		sctp_m_freem(m);
6409 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6410 		return (ENOMEM);
6411 	}
6412 	memset(ca, 0, sizeof(struct sctp_copy_all));
6413 
6414 	ca->inp = inp;
6415 	memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6416 	/*
6417 	 * take off the sendall flag, it would be bad if we failed to do
6418 	 * this :-0
6419 	 */
6420 	ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6421 	/* get length and mbuf chain */
6422 	if (uio) {
6423 		ca->sndlen = uio->uio_resid;
6424 		ca->m = sctp_copy_out_all(uio, ca->sndlen);
6425 		if (ca->m == NULL) {
6426 			SCTP_FREE(ca, SCTP_M_COPYAL);
6427 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6428 			return (ENOMEM);
6429 		}
6430 	} else {
6431 		/* Gather the length of the send */
6432 		struct mbuf *mat;
6433 
6434 		mat = m;
6435 		ca->sndlen = 0;
6436 		while (m) {
6437 			ca->sndlen += SCTP_BUF_LEN(m);
6438 			m = SCTP_BUF_NEXT(m);
6439 		}
6440 		ca->m = mat;
6441 	}
6442 	ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6443 	    SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6444 	    SCTP_ASOC_ANY_STATE,
6445 	    (void *)ca, 0,
6446 	    sctp_sendall_completes, inp, 1);
6447 	if (ret) {
6448 		SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6449 		SCTP_FREE(ca, SCTP_M_COPYAL);
6450 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6451 		return (EFAULT);
6452 	}
6453 	return (0);
6454 }
6455 
6456 
6457 void
6458 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6459 {
6460 	struct sctp_tmit_chunk *chk, *nchk;
6461 
6462 	chk = TAILQ_FIRST(&asoc->control_send_queue);
6463 	while (chk) {
6464 		nchk = TAILQ_NEXT(chk, sctp_next);
6465 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6466 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6467 			if (chk->data) {
6468 				sctp_m_freem(chk->data);
6469 				chk->data = NULL;
6470 			}
6471 			asoc->ctrl_queue_cnt--;
6472 			sctp_free_a_chunk(stcb, chk);
6473 		}
6474 		chk = nchk;
6475 	}
6476 }
6477 
6478 void
6479 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6480 {
6481 	struct sctp_association *asoc;
6482 	struct sctp_tmit_chunk *chk, *chk_tmp;
6483 	struct sctp_asconf_chunk *acp;
6484 
6485 	asoc = &stcb->asoc;
6486 	for (chk = TAILQ_FIRST(&asoc->asconf_send_queue); chk != NULL;
6487 	    chk = chk_tmp) {
6488 		/* get next chk */
6489 		chk_tmp = TAILQ_NEXT(chk, sctp_next);
6490 		/* find SCTP_ASCONF chunk in queue */
6491 		if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6492 			if (chk->data) {
6493 				acp = mtod(chk->data, struct sctp_asconf_chunk *);
6494 				if (compare_with_wrap(ntohl(acp->serial_number), stcb->asoc.asconf_seq_out_acked, MAX_SEQ)) {
6495 					/* Not Acked yet */
6496 					break;
6497 				}
6498 			}
6499 			TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6500 			if (chk->data) {
6501 				sctp_m_freem(chk->data);
6502 				chk->data = NULL;
6503 			}
6504 			asoc->ctrl_queue_cnt--;
6505 			sctp_free_a_chunk(stcb, chk);
6506 		}
6507 	}
6508 }
6509 
6510 
6511 static void
6512 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6513 
6514     struct sctp_association *asoc,
6515     struct sctp_tmit_chunk **data_list,
6516     int bundle_at,
6517     struct sctp_nets *net)
6518 {
6519 	int i;
6520 	struct sctp_tmit_chunk *tp1;
6521 
6522 	for (i = 0; i < bundle_at; i++) {
6523 		/* off of the send queue */
6524 		if (i) {
6525 			/*
6526 			 * Any chunk NOT 0 you zap the time chunk 0 gets
6527 			 * zapped or set based on if a RTO measurment is
6528 			 * needed.
6529 			 */
6530 			data_list[i]->do_rtt = 0;
6531 		}
6532 		/* record time */
6533 		data_list[i]->sent_rcv_time = net->last_sent_time;
6534 		data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6535 		TAILQ_REMOVE(&asoc->send_queue,
6536 		    data_list[i],
6537 		    sctp_next);
6538 		/* on to the sent queue */
6539 		tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6540 		if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
6541 		    data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
6542 			struct sctp_tmit_chunk *tpp;
6543 
6544 			/* need to move back */
6545 	back_up_more:
6546 			tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6547 			if (tpp == NULL) {
6548 				TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6549 				goto all_done;
6550 			}
6551 			tp1 = tpp;
6552 			if (compare_with_wrap(tp1->rec.data.TSN_seq,
6553 			    data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
6554 				goto back_up_more;
6555 			}
6556 			TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6557 		} else {
6558 			TAILQ_INSERT_TAIL(&asoc->sent_queue,
6559 			    data_list[i],
6560 			    sctp_next);
6561 		}
6562 all_done:
6563 		/* This does not lower until the cum-ack passes it */
6564 		asoc->sent_queue_cnt++;
6565 		asoc->send_queue_cnt--;
6566 		if ((asoc->peers_rwnd <= 0) &&
6567 		    (asoc->total_flight == 0) &&
6568 		    (bundle_at == 1)) {
6569 			/* Mark the chunk as being a window probe */
6570 			SCTP_STAT_INCR(sctps_windowprobed);
6571 		}
6572 #ifdef SCTP_AUDITING_ENABLED
6573 		sctp_audit_log(0xC2, 3);
6574 #endif
6575 		data_list[i]->sent = SCTP_DATAGRAM_SENT;
6576 		data_list[i]->snd_count = 1;
6577 		data_list[i]->rec.data.chunk_was_revoked = 0;
6578 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6579 			sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6580 			    data_list[i]->whoTo->flight_size,
6581 			    data_list[i]->book_size,
6582 			    (uintptr_t) data_list[i]->whoTo,
6583 			    data_list[i]->rec.data.TSN_seq);
6584 		}
6585 		sctp_flight_size_increase(data_list[i]);
6586 		sctp_total_flight_increase(stcb, data_list[i]);
6587 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6588 			sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6589 			    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6590 		}
6591 		asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6592 		    (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6593 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6594 			/* SWS sender side engages */
6595 			asoc->peers_rwnd = 0;
6596 		}
6597 	}
6598 }
6599 
6600 static void
6601 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
6602 {
6603 	struct sctp_tmit_chunk *chk, *nchk;
6604 
6605 	for (chk = TAILQ_FIRST(&asoc->control_send_queue);
6606 	    chk; chk = nchk) {
6607 		nchk = TAILQ_NEXT(chk, sctp_next);
6608 		if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6609 		    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
6610 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6611 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6612 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
6613 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6614 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6615 		    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6616 		    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6617 		    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6618 		    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6619 		    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6620 			/* Stray chunks must be cleaned up */
6621 	clean_up_anyway:
6622 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6623 			if (chk->data) {
6624 				sctp_m_freem(chk->data);
6625 				chk->data = NULL;
6626 			}
6627 			asoc->ctrl_queue_cnt--;
6628 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)
6629 				asoc->fwd_tsn_cnt--;
6630 			sctp_free_a_chunk(stcb, chk);
6631 		} else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6632 			/* special handling, we must look into the param */
6633 			if (chk != asoc->str_reset) {
6634 				goto clean_up_anyway;
6635 			}
6636 		}
6637 	}
6638 }
6639 
6640 
6641 static int
6642 sctp_can_we_split_this(struct sctp_tcb *stcb,
6643     uint32_t length,
6644     uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6645 {
6646 	/*
6647 	 * Make a decision on if I should split a msg into multiple parts.
6648 	 * This is only asked of incomplete messages.
6649 	 */
6650 	if (eeor_on) {
6651 		/*
6652 		 * If we are doing EEOR we need to always send it if its the
6653 		 * entire thing, since it might be all the guy is putting in
6654 		 * the hopper.
6655 		 */
6656 		if (goal_mtu >= length) {
6657 			/*-
6658 			 * If we have data outstanding,
6659 			 * we get another chance when the sack
6660 			 * arrives to transmit - wait for more data
6661 			 */
6662 			if (stcb->asoc.total_flight == 0) {
6663 				/*
6664 				 * If nothing is in flight, we zero the
6665 				 * packet counter.
6666 				 */
6667 				return (length);
6668 			}
6669 			return (0);
6670 
6671 		} else {
6672 			/* You can fill the rest */
6673 			return (goal_mtu);
6674 		}
6675 	}
6676 	/*-
6677 	 * For those strange folk that make the send buffer
6678 	 * smaller than our fragmentation point, we can't
6679 	 * get a full msg in so we have to allow splitting.
6680 	 */
6681 	if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
6682 		return (length);
6683 	}
6684 	if ((length <= goal_mtu) ||
6685 	    ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
6686 		/* Sub-optimial residual don't split in non-eeor mode. */
6687 		return (0);
6688 	}
6689 	/*
6690 	 * If we reach here length is larger than the goal_mtu. Do we wish
6691 	 * to split it for the sake of packet putting together?
6692 	 */
6693 	if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
6694 		/* Its ok to split it */
6695 		return (min(goal_mtu, frag_point));
6696 	}
6697 	/* Nope, can't split */
6698 	return (0);
6699 
6700 }
6701 
6702 static uint32_t
6703 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
6704     struct sctp_stream_out *strq,
6705     uint32_t goal_mtu,
6706     uint32_t frag_point,
6707     int *locked,
6708     int *giveup,
6709     int eeor_mode,
6710     int *bail)
6711 {
6712 	/* Move from the stream to the send_queue keeping track of the total */
6713 	struct sctp_association *asoc;
6714 	struct sctp_stream_queue_pending *sp;
6715 	struct sctp_tmit_chunk *chk;
6716 	struct sctp_data_chunk *dchkh;
6717 	uint32_t to_move, length;
6718 	uint8_t rcv_flags = 0;
6719 	uint8_t some_taken;
6720 	uint8_t send_lock_up = 0;
6721 
6722 	SCTP_TCB_LOCK_ASSERT(stcb);
6723 	asoc = &stcb->asoc;
6724 one_more_time:
6725 	/* sa_ignore FREED_MEMORY */
6726 	sp = TAILQ_FIRST(&strq->outqueue);
6727 	if (sp == NULL) {
6728 		*locked = 0;
6729 		if (send_lock_up == 0) {
6730 			SCTP_TCB_SEND_LOCK(stcb);
6731 			send_lock_up = 1;
6732 		}
6733 		sp = TAILQ_FIRST(&strq->outqueue);
6734 		if (sp) {
6735 			goto one_more_time;
6736 		}
6737 		if (strq->last_msg_incomplete) {
6738 			SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6739 			    strq->stream_no,
6740 			    strq->last_msg_incomplete);
6741 			strq->last_msg_incomplete = 0;
6742 		}
6743 		to_move = 0;
6744 		if (send_lock_up) {
6745 			SCTP_TCB_SEND_UNLOCK(stcb);
6746 			send_lock_up = 0;
6747 		}
6748 		goto out_of;
6749 	}
6750 	if ((sp->msg_is_complete) && (sp->length == 0)) {
6751 		if (sp->sender_all_done) {
6752 			/*
6753 			 * We are doing differed cleanup. Last time through
6754 			 * when we took all the data the sender_all_done was
6755 			 * not set.
6756 			 */
6757 			if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
6758 				SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6759 				SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6760 				    sp->sender_all_done,
6761 				    sp->length,
6762 				    sp->msg_is_complete,
6763 				    sp->put_last_out,
6764 				    send_lock_up);
6765 			}
6766 			if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
6767 				SCTP_TCB_SEND_LOCK(stcb);
6768 				send_lock_up = 1;
6769 			}
6770 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6771 			TAILQ_REMOVE(&strq->outqueue, sp, next);
6772 			sctp_free_remote_addr(sp->net);
6773 			if (sp->data) {
6774 				sctp_m_freem(sp->data);
6775 				sp->data = NULL;
6776 			}
6777 			sctp_free_a_strmoq(stcb, sp);
6778 			/* we can't be locked to it */
6779 			*locked = 0;
6780 			stcb->asoc.locked_on_sending = NULL;
6781 			if (send_lock_up) {
6782 				SCTP_TCB_SEND_UNLOCK(stcb);
6783 				send_lock_up = 0;
6784 			}
6785 			/* back to get the next msg */
6786 			goto one_more_time;
6787 		} else {
6788 			/*
6789 			 * sender just finished this but still holds a
6790 			 * reference
6791 			 */
6792 			*locked = 1;
6793 			*giveup = 1;
6794 			to_move = 0;
6795 			goto out_of;
6796 		}
6797 	} else {
6798 		/* is there some to get */
6799 		if (sp->length == 0) {
6800 			/* no */
6801 			*locked = 1;
6802 			*giveup = 1;
6803 			to_move = 0;
6804 			goto out_of;
6805 		} else if (sp->discard_rest) {
6806 			if (send_lock_up == 0) {
6807 				SCTP_TCB_SEND_LOCK(stcb);
6808 				send_lock_up = 1;
6809 			}
6810 			/* Whack down the size */
6811 			atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
6812 			if ((stcb->sctp_socket != NULL) && \
6813 			    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6814 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
6815 				atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
6816 			}
6817 			if (sp->data) {
6818 				sctp_m_freem(sp->data);
6819 				sp->data = NULL;
6820 				sp->tail_mbuf = NULL;
6821 			}
6822 			sp->length = 0;
6823 			sp->some_taken = 1;
6824 			*locked = 1;
6825 			*giveup = 1;
6826 			to_move = 0;
6827 			goto out_of;
6828 		}
6829 	}
6830 	some_taken = sp->some_taken;
6831 	if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6832 		sp->msg_is_complete = 1;
6833 	}
6834 re_look:
6835 	length = sp->length;
6836 	if (sp->msg_is_complete) {
6837 		/* The message is complete */
6838 		to_move = min(length, frag_point);
6839 		if (to_move == length) {
6840 			/* All of it fits in the MTU */
6841 			if (sp->some_taken) {
6842 				rcv_flags |= SCTP_DATA_LAST_FRAG;
6843 				sp->put_last_out = 1;
6844 			} else {
6845 				rcv_flags |= SCTP_DATA_NOT_FRAG;
6846 				sp->put_last_out = 1;
6847 			}
6848 		} else {
6849 			/* Not all of it fits, we fragment */
6850 			if (sp->some_taken == 0) {
6851 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
6852 			}
6853 			sp->some_taken = 1;
6854 		}
6855 	} else {
6856 		to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
6857 		if (to_move) {
6858 			/*-
6859 			 * We use a snapshot of length in case it
6860 			 * is expanding during the compare.
6861 			 */
6862 			uint32_t llen;
6863 
6864 			llen = length;
6865 			if (to_move >= llen) {
6866 				to_move = llen;
6867 				if (send_lock_up == 0) {
6868 					/*-
6869 					 * We are taking all of an incomplete msg
6870 					 * thus we need a send lock.
6871 					 */
6872 					SCTP_TCB_SEND_LOCK(stcb);
6873 					send_lock_up = 1;
6874 					if (sp->msg_is_complete) {
6875 						/*
6876 						 * the sender finished the
6877 						 * msg
6878 						 */
6879 						goto re_look;
6880 					}
6881 				}
6882 			}
6883 			if (sp->some_taken == 0) {
6884 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
6885 				sp->some_taken = 1;
6886 			}
6887 		} else {
6888 			/* Nothing to take. */
6889 			if (sp->some_taken) {
6890 				*locked = 1;
6891 			}
6892 			*giveup = 1;
6893 			to_move = 0;
6894 			goto out_of;
6895 		}
6896 	}
6897 
6898 	/* If we reach here, we can copy out a chunk */
6899 	sctp_alloc_a_chunk(stcb, chk);
6900 	if (chk == NULL) {
6901 		/* No chunk memory */
6902 		*giveup = 1;
6903 		to_move = 0;
6904 		goto out_of;
6905 	}
6906 	/*
6907 	 * Setup for unordered if needed by looking at the user sent info
6908 	 * flags.
6909 	 */
6910 	if (sp->sinfo_flags & SCTP_UNORDERED) {
6911 		rcv_flags |= SCTP_DATA_UNORDERED;
6912 	}
6913 	if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
6914 	    ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
6915 		rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
6916 	}
6917 	/* clear out the chunk before setting up */
6918 	memset(chk, 0, sizeof(*chk));
6919 	chk->rec.data.rcv_flags = rcv_flags;
6920 
6921 	if (to_move >= length) {
6922 		/* we think we can steal the whole thing */
6923 		if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
6924 			SCTP_TCB_SEND_LOCK(stcb);
6925 			send_lock_up = 1;
6926 		}
6927 		if (to_move < sp->length) {
6928 			/* bail, it changed */
6929 			goto dont_do_it;
6930 		}
6931 		chk->data = sp->data;
6932 		chk->last_mbuf = sp->tail_mbuf;
6933 		/* register the stealing */
6934 		sp->data = sp->tail_mbuf = NULL;
6935 	} else {
6936 		struct mbuf *m;
6937 
6938 dont_do_it:
6939 		chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
6940 		chk->last_mbuf = NULL;
6941 		if (chk->data == NULL) {
6942 			sp->some_taken = some_taken;
6943 			sctp_free_a_chunk(stcb, chk);
6944 			*bail = 1;
6945 			to_move = 0;
6946 			goto out_of;
6947 		}
6948 #ifdef SCTP_MBUF_LOGGING
6949 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6950 			struct mbuf *mat;
6951 
6952 			mat = chk->data;
6953 			while (mat) {
6954 				if (SCTP_BUF_IS_EXTENDED(mat)) {
6955 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6956 				}
6957 				mat = SCTP_BUF_NEXT(mat);
6958 			}
6959 		}
6960 #endif
6961 		/* Pull off the data */
6962 		m_adj(sp->data, to_move);
6963 		/* Now lets work our way down and compact it */
6964 		m = sp->data;
6965 		while (m && (SCTP_BUF_LEN(m) == 0)) {
6966 			sp->data = SCTP_BUF_NEXT(m);
6967 			SCTP_BUF_NEXT(m) = NULL;
6968 			if (sp->tail_mbuf == m) {
6969 				/*-
6970 				 * Freeing tail? TSNH since
6971 				 * we supposedly were taking less
6972 				 * than the sp->length.
6973 				 */
6974 #ifdef INVARIANTS
6975 				panic("Huh, freing tail? - TSNH");
6976 #else
6977 				SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
6978 				sp->tail_mbuf = sp->data = NULL;
6979 				sp->length = 0;
6980 #endif
6981 
6982 			}
6983 			sctp_m_free(m);
6984 			m = sp->data;
6985 		}
6986 	}
6987 	if (SCTP_BUF_IS_EXTENDED(chk->data)) {
6988 		chk->copy_by_ref = 1;
6989 	} else {
6990 		chk->copy_by_ref = 0;
6991 	}
6992 	/*
6993 	 * get last_mbuf and counts of mb useage This is ugly but hopefully
6994 	 * its only one mbuf.
6995 	 */
6996 	if (chk->last_mbuf == NULL) {
6997 		chk->last_mbuf = chk->data;
6998 		while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
6999 			chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
7000 		}
7001 	}
7002 	if (to_move > length) {
7003 		/*- This should not happen either
7004 		 * since we always lower to_move to the size
7005 		 * of sp->length if its larger.
7006 		 */
7007 #ifdef INVARIANTS
7008 		panic("Huh, how can to_move be larger?");
7009 #else
7010 		SCTP_PRINTF("Huh, how can to_move be larger?\n");
7011 		sp->length = 0;
7012 #endif
7013 	} else {
7014 		atomic_subtract_int(&sp->length, to_move);
7015 	}
7016 	if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
7017 		/* Not enough room for a chunk header, get some */
7018 		struct mbuf *m;
7019 
7020 		m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
7021 		if (m == NULL) {
7022 			/*
7023 			 * we're in trouble here. _PREPEND below will free
7024 			 * all the data if there is no leading space, so we
7025 			 * must put the data back and restore.
7026 			 */
7027 			if (send_lock_up == 0) {
7028 				SCTP_TCB_SEND_LOCK(stcb);
7029 				send_lock_up = 1;
7030 			}
7031 			if (chk->data == NULL) {
7032 				/* unsteal the data */
7033 				sp->data = chk->data;
7034 				sp->tail_mbuf = chk->last_mbuf;
7035 			} else {
7036 				struct mbuf *m_tmp;
7037 
7038 				/* reassemble the data */
7039 				m_tmp = sp->data;
7040 				sp->data = chk->data;
7041 				SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7042 			}
7043 			sp->some_taken = some_taken;
7044 			atomic_add_int(&sp->length, to_move);
7045 			chk->data = NULL;
7046 			*bail = 1;
7047 			sctp_free_a_chunk(stcb, chk);
7048 			to_move = 0;
7049 			goto out_of;
7050 		} else {
7051 			SCTP_BUF_LEN(m) = 0;
7052 			SCTP_BUF_NEXT(m) = chk->data;
7053 			chk->data = m;
7054 			M_ALIGN(chk->data, 4);
7055 		}
7056 	}
7057 	SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
7058 	if (chk->data == NULL) {
7059 		/* HELP, TSNH since we assured it would not above? */
7060 #ifdef INVARIANTS
7061 		panic("prepend failes HELP?");
7062 #else
7063 		SCTP_PRINTF("prepend fails HELP?\n");
7064 		sctp_free_a_chunk(stcb, chk);
7065 #endif
7066 		*bail = 1;
7067 		to_move = 0;
7068 		goto out_of;
7069 	}
7070 	sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
7071 	chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
7072 	chk->book_size_scale = 0;
7073 	chk->sent = SCTP_DATAGRAM_UNSENT;
7074 
7075 	chk->flags = 0;
7076 	chk->asoc = &stcb->asoc;
7077 	chk->pad_inplace = 0;
7078 	chk->no_fr_allowed = 0;
7079 	chk->rec.data.stream_seq = sp->strseq;
7080 	chk->rec.data.stream_number = sp->stream;
7081 	chk->rec.data.payloadtype = sp->ppid;
7082 	chk->rec.data.context = sp->context;
7083 	chk->rec.data.doing_fast_retransmit = 0;
7084 	chk->rec.data.ect_nonce = 0;	/* ECN Nonce */
7085 
7086 	chk->rec.data.timetodrop = sp->ts;
7087 	chk->flags = sp->act_flags;
7088 
7089 	chk->whoTo = net;
7090 	atomic_add_int(&chk->whoTo->ref_count, 1);
7091 
7092 	if (sp->holds_key_ref) {
7093 		chk->auth_keyid = sp->auth_keyid;
7094 		sctp_auth_key_acquire(stcb, chk->auth_keyid);
7095 		chk->holds_key_ref = 1;
7096 	}
7097 	chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
7098 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7099 		sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7100 		    (uintptr_t) stcb, sp->length,
7101 		    (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
7102 		    chk->rec.data.TSN_seq);
7103 	}
7104 	dchkh = mtod(chk->data, struct sctp_data_chunk *);
7105 	/*
7106 	 * Put the rest of the things in place now. Size was done earlier in
7107 	 * previous loop prior to padding.
7108 	 */
7109 
7110 #ifdef SCTP_ASOCLOG_OF_TSNS
7111 	SCTP_TCB_LOCK_ASSERT(stcb);
7112 	if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7113 		asoc->tsn_out_at = 0;
7114 		asoc->tsn_out_wrapped = 1;
7115 	}
7116 	asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7117 	asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7118 	asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7119 	asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7120 	asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7121 	asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7122 	asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7123 	asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7124 	asoc->tsn_out_at++;
7125 #endif
7126 
7127 	dchkh->ch.chunk_type = SCTP_DATA;
7128 	dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7129 	dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7130 	dchkh->dp.stream_id = htons(strq->stream_no);
7131 	dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7132 	dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7133 	dchkh->ch.chunk_length = htons(chk->send_size);
7134 	/* Now advance the chk->send_size by the actual pad needed. */
7135 	if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7136 		/* need a pad */
7137 		struct mbuf *lm;
7138 		int pads;
7139 
7140 		pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7141 		if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7142 			chk->pad_inplace = 1;
7143 		}
7144 		if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7145 			/* pad added an mbuf */
7146 			chk->last_mbuf = lm;
7147 		}
7148 		chk->send_size += pads;
7149 	}
7150 	/* We only re-set the policy if it is on */
7151 	if (sp->pr_sctp_on) {
7152 		sctp_set_prsctp_policy(sp);
7153 		asoc->pr_sctp_cnt++;
7154 		chk->pr_sctp_on = 1;
7155 	} else {
7156 		chk->pr_sctp_on = 0;
7157 	}
7158 	if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7159 		/* All done pull and kill the message */
7160 		atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7161 		if (sp->put_last_out == 0) {
7162 			SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7163 			SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7164 			    sp->sender_all_done,
7165 			    sp->length,
7166 			    sp->msg_is_complete,
7167 			    sp->put_last_out,
7168 			    send_lock_up);
7169 		}
7170 		if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7171 			SCTP_TCB_SEND_LOCK(stcb);
7172 			send_lock_up = 1;
7173 		}
7174 		TAILQ_REMOVE(&strq->outqueue, sp, next);
7175 		sctp_free_remote_addr(sp->net);
7176 		if (sp->data) {
7177 			sctp_m_freem(sp->data);
7178 			sp->data = NULL;
7179 		}
7180 		sctp_free_a_strmoq(stcb, sp);
7181 
7182 		/* we can't be locked to it */
7183 		*locked = 0;
7184 		stcb->asoc.locked_on_sending = NULL;
7185 	} else {
7186 		/* more to go, we are locked */
7187 		*locked = 1;
7188 	}
7189 	asoc->chunks_on_out_queue++;
7190 	TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7191 	asoc->send_queue_cnt++;
7192 out_of:
7193 	if (send_lock_up) {
7194 		SCTP_TCB_SEND_UNLOCK(stcb);
7195 		send_lock_up = 0;
7196 	}
7197 	return (to_move);
7198 }
7199 
7200 
7201 static struct sctp_stream_out *
7202 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
7203 {
7204 	struct sctp_stream_out *strq;
7205 
7206 	/* Find the next stream to use */
7207 	if (asoc->last_out_stream == NULL) {
7208 		strq = TAILQ_FIRST(&asoc->out_wheel);
7209 	} else {
7210 		strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7211 		if (strq == NULL) {
7212 			strq = TAILQ_FIRST(&asoc->out_wheel);
7213 		}
7214 	}
7215 	return (strq);
7216 }
7217 
7218 
7219 static void
7220 sctp_fill_outqueue(struct sctp_tcb *stcb,
7221     struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
7222 {
7223 	struct sctp_association *asoc;
7224 	struct sctp_stream_out *strq, *strqn;
7225 	int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7226 	int locked, giveup;
7227 	struct sctp_stream_queue_pending *sp;
7228 
7229 	SCTP_TCB_LOCK_ASSERT(stcb);
7230 	asoc = &stcb->asoc;
7231 #ifdef INET6
7232 	if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
7233 		goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7234 	} else {
7235 		/* ?? not sure what else to do */
7236 		goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7237 	}
7238 #else
7239 	goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7240 #endif
7241 	/* Need an allowance for the data chunk header too */
7242 	goal_mtu -= sizeof(struct sctp_data_chunk);
7243 
7244 	/* must make even word boundary */
7245 	goal_mtu &= 0xfffffffc;
7246 	if (asoc->locked_on_sending) {
7247 		/* We are stuck on one stream until the message completes. */
7248 		strqn = strq = asoc->locked_on_sending;
7249 		locked = 1;
7250 	} else {
7251 		strqn = strq = sctp_select_a_stream(stcb, asoc);
7252 		locked = 0;
7253 	}
7254 
7255 	while ((goal_mtu > 0) && strq) {
7256 		sp = TAILQ_FIRST(&strq->outqueue);
7257 		/*
7258 		 * If CMT is off, we must validate that the stream in
7259 		 * question has the first item pointed towards are network
7260 		 * destionation requested by the caller. Note that if we
7261 		 * turn out to be locked to a stream (assigning TSN's then
7262 		 * we must stop, since we cannot look for another stream
7263 		 * with data to send to that destination). In CMT's case, by
7264 		 * skipping this check, we will send one data packet towards
7265 		 * the requested net.
7266 		 */
7267 		if (sp == NULL) {
7268 			break;
7269 		}
7270 		if ((sp->net != net) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
7271 			/* none for this network */
7272 			if (locked) {
7273 				break;
7274 			} else {
7275 				strq = sctp_select_a_stream(stcb, asoc);
7276 				if (strq == NULL)
7277 					/* none left */
7278 					break;
7279 				if (strqn == strq) {
7280 					/* I have circled */
7281 					break;
7282 				}
7283 				continue;
7284 			}
7285 		}
7286 		giveup = 0;
7287 		bail = 0;
7288 		moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
7289 		    &giveup, eeor_mode, &bail);
7290 		if (moved_how_much)
7291 			asoc->last_out_stream = strq;
7292 
7293 		if (locked) {
7294 			asoc->locked_on_sending = strq;
7295 			if ((moved_how_much == 0) || (giveup) || bail)
7296 				/* no more to move for now */
7297 				break;
7298 		} else {
7299 			asoc->locked_on_sending = NULL;
7300 			if (TAILQ_EMPTY(&strq->outqueue)) {
7301 				if (strq == strqn) {
7302 					/* Must move start to next one */
7303 					strqn = TAILQ_NEXT(strq, next_spoke);
7304 					if (strqn == NULL) {
7305 						strqn = TAILQ_FIRST(&asoc->out_wheel);
7306 						if (strqn == NULL) {
7307 							break;
7308 						}
7309 					}
7310 				}
7311 				sctp_remove_from_wheel(stcb, asoc, strq, 0);
7312 			}
7313 			if ((giveup) || bail) {
7314 				break;
7315 			}
7316 			strq = sctp_select_a_stream(stcb, asoc);
7317 			if (strq == NULL) {
7318 				break;
7319 			}
7320 		}
7321 		total_moved += moved_how_much;
7322 		goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7323 		goal_mtu &= 0xfffffffc;
7324 	}
7325 	if (bail)
7326 		*quit_now = 1;
7327 
7328 	if (total_moved == 0) {
7329 		if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) &&
7330 		    (net == stcb->asoc.primary_destination)) {
7331 			/* ran dry for primary network net */
7332 			SCTP_STAT_INCR(sctps_primary_randry);
7333 		} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7334 			/* ran dry with CMT on */
7335 			SCTP_STAT_INCR(sctps_cmt_randry);
7336 		}
7337 	}
7338 }
7339 
7340 void
7341 sctp_fix_ecn_echo(struct sctp_association *asoc)
7342 {
7343 	struct sctp_tmit_chunk *chk;
7344 
7345 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7346 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7347 			chk->sent = SCTP_DATAGRAM_UNSENT;
7348 		}
7349 	}
7350 }
7351 
7352 static void
7353 sctp_move_to_an_alt(struct sctp_tcb *stcb,
7354     struct sctp_association *asoc,
7355     struct sctp_nets *net)
7356 {
7357 	struct sctp_tmit_chunk *chk;
7358 	struct sctp_nets *a_net;
7359 
7360 	SCTP_TCB_LOCK_ASSERT(stcb);
7361 	/*
7362 	 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
7363 	 * destination using the PF algorithm for finding alternate
7364 	 * destinations.
7365 	 */
7366 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
7367 		a_net = sctp_find_alternate_net(stcb, net, 2);
7368 	} else {
7369 		a_net = sctp_find_alternate_net(stcb, net, 0);
7370 	}
7371 	if ((a_net != net) &&
7372 	    ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
7373 		/*
7374 		 * We only proceed if a valid alternate is found that is not
7375 		 * this one and is reachable. Here we must move all chunks
7376 		 * queued in the send queue off of the destination address
7377 		 * to our alternate.
7378 		 */
7379 		TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7380 			if (chk->whoTo == net) {
7381 				/* Move the chunk to our alternate */
7382 				sctp_free_remote_addr(chk->whoTo);
7383 				chk->whoTo = a_net;
7384 				atomic_add_int(&a_net->ref_count, 1);
7385 			}
7386 		}
7387 	}
7388 }
7389 
7390 int
7391 sctp_med_chunk_output(struct sctp_inpcb *inp,
7392     struct sctp_tcb *stcb,
7393     struct sctp_association *asoc,
7394     int *num_out,
7395     int *reason_code,
7396     int control_only, int from_where,
7397     struct timeval *now, int *now_filled, int frag_point, int so_locked
7398 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7399     SCTP_UNUSED
7400 #endif
7401 )
7402 {
7403 	/*
7404 	 * Ok this is the generic chunk service queue. we must do the
7405 	 * following: - Service the stream queue that is next, moving any
7406 	 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7407 	 * LAST to the out queue in one pass) and assigning TSN's - Check to
7408 	 * see if the cwnd/rwnd allows any output, if so we go ahead and
7409 	 * fomulate and send the low level chunks. Making sure to combine
7410 	 * any control in the control chunk queue also.
7411 	 */
7412 	struct sctp_nets *net, *start_at, *old_start_at = NULL;
7413 	struct mbuf *outchain, *endoutchain;
7414 	struct sctp_tmit_chunk *chk, *nchk;
7415 
7416 	/* temp arrays for unlinking */
7417 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7418 	int no_fragmentflg, error;
7419 	unsigned int max_rwnd_per_dest;
7420 	int one_chunk, hbflag, skip_data_for_this_net;
7421 	int asconf, cookie, no_out_cnt;
7422 	int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7423 	unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7424 	int tsns_sent = 0;
7425 	uint32_t auth_offset = 0;
7426 	struct sctp_auth_chunk *auth = NULL;
7427 	uint16_t auth_keyid;
7428 	int override_ok = 1;
7429 	int data_auth_reqd = 0;
7430 
7431 	/*
7432 	 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7433 	 * destination.
7434 	 */
7435 	int pf_hbflag = 0;
7436 	int quit_now = 0;
7437 
7438 	*num_out = 0;
7439 	auth_keyid = stcb->asoc.authinfo.active_keyid;
7440 
7441 	if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7442 	    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7443 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7444 		eeor_mode = 1;
7445 	} else {
7446 		eeor_mode = 0;
7447 	}
7448 	ctl_cnt = no_out_cnt = asconf = cookie = 0;
7449 	/*
7450 	 * First lets prime the pump. For each destination, if there is room
7451 	 * in the flight size, attempt to pull an MTU's worth out of the
7452 	 * stream queues into the general send_queue
7453 	 */
7454 #ifdef SCTP_AUDITING_ENABLED
7455 	sctp_audit_log(0xC2, 2);
7456 #endif
7457 	SCTP_TCB_LOCK_ASSERT(stcb);
7458 	hbflag = 0;
7459 	if ((control_only) || (asoc->stream_reset_outstanding))
7460 		no_data_chunks = 1;
7461 	else
7462 		no_data_chunks = 0;
7463 
7464 	/* Nothing to possible to send? */
7465 	if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7466 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7467 	    TAILQ_EMPTY(&asoc->send_queue) &&
7468 	    TAILQ_EMPTY(&asoc->out_wheel)) {
7469 		*reason_code = 9;
7470 		return (0);
7471 	}
7472 	if (asoc->peers_rwnd == 0) {
7473 		/* No room in peers rwnd */
7474 		*reason_code = 1;
7475 		if (asoc->total_flight > 0) {
7476 			/* we are allowed one chunk in flight */
7477 			no_data_chunks = 1;
7478 		}
7479 	}
7480 	max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7481 	if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
7482 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7483 			/*
7484 			 * This for loop we are in takes in each net, if
7485 			 * its's got space in cwnd and has data sent to it
7486 			 * (when CMT is off) then it calls
7487 			 * sctp_fill_outqueue for the net. This gets data on
7488 			 * the send queue for that network.
7489 			 *
7490 			 * In sctp_fill_outqueue TSN's are assigned and data is
7491 			 * copied out of the stream buffers. Note mostly
7492 			 * copy by reference (we hope).
7493 			 */
7494 			net->window_probe = 0;
7495 			if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
7496 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7497 					sctp_log_cwnd(stcb, net, 1,
7498 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7499 				}
7500 				continue;
7501 			}
7502 			if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
7503 				/* nothing can be in queue for this guy */
7504 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7505 					sctp_log_cwnd(stcb, net, 2,
7506 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7507 				}
7508 				continue;
7509 			}
7510 			if (net->flight_size >= net->cwnd) {
7511 				/* skip this network, no room - can't fill */
7512 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7513 					sctp_log_cwnd(stcb, net, 3,
7514 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7515 				}
7516 				continue;
7517 			}
7518 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7519 				sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7520 			}
7521 			sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
7522 			if (quit_now) {
7523 				/* memory alloc failure */
7524 				no_data_chunks = 1;
7525 				break;
7526 			}
7527 		}
7528 	}
7529 	/* now service each destination and send out what we can for it */
7530 	/* Nothing to send? */
7531 	if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7532 	    (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7533 	    (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
7534 		*reason_code = 8;
7535 		return (0);
7536 	}
7537 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7538 		/* get the last start point */
7539 		start_at = asoc->last_net_cmt_send_started;
7540 		if (start_at == NULL) {
7541 			/* null so to beginning */
7542 			start_at = TAILQ_FIRST(&asoc->nets);
7543 		} else {
7544 			start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7545 			if (start_at == NULL) {
7546 				start_at = TAILQ_FIRST(&asoc->nets);
7547 			}
7548 		}
7549 		asoc->last_net_cmt_send_started = start_at;
7550 	} else {
7551 		start_at = TAILQ_FIRST(&asoc->nets);
7552 	}
7553 	old_start_at = NULL;
7554 again_one_more_time:
7555 	for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7556 		/* how much can we send? */
7557 		/* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7558 		if (old_start_at && (old_start_at == net)) {
7559 			/* through list ocmpletely. */
7560 			break;
7561 		}
7562 		tsns_sent = 0xa;
7563 		if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
7564 			/*
7565 			 * Ref-count of 1 so we cannot have data or control
7566 			 * queued to this address. Skip it (non-CMT).
7567 			 */
7568 			continue;
7569 		}
7570 		if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7571 		    (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7572 		    (net->flight_size >= net->cwnd)) {
7573 			/*
7574 			 * Nothing on control or asconf and flight is full,
7575 			 * we can skip even in the CMT case.
7576 			 */
7577 			continue;
7578 		}
7579 		ctl_cnt = bundle_at = 0;
7580 		endoutchain = outchain = NULL;
7581 		no_fragmentflg = 1;
7582 		one_chunk = 0;
7583 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7584 			skip_data_for_this_net = 1;
7585 		} else {
7586 			skip_data_for_this_net = 0;
7587 		}
7588 		if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7589 			/*
7590 			 * if we have a route and an ifp check to see if we
7591 			 * have room to send to this guy
7592 			 */
7593 			struct ifnet *ifp;
7594 
7595 			ifp = net->ro.ro_rt->rt_ifp;
7596 			if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7597 				SCTP_STAT_INCR(sctps_ifnomemqueued);
7598 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7599 					sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7600 				}
7601 				continue;
7602 			}
7603 		}
7604 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7605 		case AF_INET:
7606 			mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7607 			break;
7608 #ifdef INET6
7609 		case AF_INET6:
7610 			mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7611 			break;
7612 #endif
7613 		default:
7614 			/* TSNH */
7615 			mtu = net->mtu;
7616 			break;
7617 		}
7618 		mx_mtu = mtu;
7619 		to_out = 0;
7620 		if (mtu > asoc->peers_rwnd) {
7621 			if (asoc->total_flight > 0) {
7622 				/* We have a packet in flight somewhere */
7623 				r_mtu = asoc->peers_rwnd;
7624 			} else {
7625 				/* We are always allowed to send one MTU out */
7626 				one_chunk = 1;
7627 				r_mtu = mtu;
7628 			}
7629 		} else {
7630 			r_mtu = mtu;
7631 		}
7632 		/************************/
7633 		/* ASCONF transmission */
7634 		/************************/
7635 		/* Now first lets go through the asconf queue */
7636 		for (chk = TAILQ_FIRST(&asoc->asconf_send_queue);
7637 		    chk; chk = nchk) {
7638 			nchk = TAILQ_NEXT(chk, sctp_next);
7639 			if (chk->rec.chunk_id.id != SCTP_ASCONF) {
7640 				continue;
7641 			}
7642 			if (chk->whoTo != net) {
7643 				/*
7644 				 * No, not sent to the network we are
7645 				 * looking at
7646 				 */
7647 				break;
7648 			}
7649 			if (chk->data == NULL) {
7650 				break;
7651 			}
7652 			if (chk->sent != SCTP_DATAGRAM_UNSENT &&
7653 			    chk->sent != SCTP_DATAGRAM_RESEND) {
7654 				break;
7655 			}
7656 			/*
7657 			 * if no AUTH is yet included and this chunk
7658 			 * requires it, make sure to account for it.  We
7659 			 * don't apply the size until the AUTH chunk is
7660 			 * actually added below in case there is no room for
7661 			 * this chunk. NOTE: we overload the use of "omtu"
7662 			 * here
7663 			 */
7664 			if ((auth == NULL) &&
7665 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7666 			    stcb->asoc.peer_auth_chunks)) {
7667 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7668 			} else
7669 				omtu = 0;
7670 			/* Here we do NOT factor the r_mtu */
7671 			if ((chk->send_size < (int)(mtu - omtu)) ||
7672 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7673 				/*
7674 				 * We probably should glom the mbuf chain
7675 				 * from the chk->data for control but the
7676 				 * problem is it becomes yet one more level
7677 				 * of tracking to do if for some reason
7678 				 * output fails. Then I have got to
7679 				 * reconstruct the merged control chain.. el
7680 				 * yucko.. for now we take the easy way and
7681 				 * do the copy
7682 				 */
7683 				/*
7684 				 * Add an AUTH chunk, if chunk requires it
7685 				 * save the offset into the chain for AUTH
7686 				 */
7687 				if ((auth == NULL) &&
7688 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7689 				    stcb->asoc.peer_auth_chunks))) {
7690 					outchain = sctp_add_auth_chunk(outchain,
7691 					    &endoutchain,
7692 					    &auth,
7693 					    &auth_offset,
7694 					    stcb,
7695 					    chk->rec.chunk_id.id);
7696 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7697 				}
7698 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7699 				    (int)chk->rec.chunk_id.can_take_data,
7700 				    chk->send_size, chk->copy_by_ref);
7701 				if (outchain == NULL) {
7702 					*reason_code = 8;
7703 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7704 					return (ENOMEM);
7705 				}
7706 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7707 				/* update our MTU size */
7708 				if (mtu > (chk->send_size + omtu))
7709 					mtu -= (chk->send_size + omtu);
7710 				else
7711 					mtu = 0;
7712 				to_out += (chk->send_size + omtu);
7713 				/* Do clear IP_DF ? */
7714 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7715 					no_fragmentflg = 0;
7716 				}
7717 				if (chk->rec.chunk_id.can_take_data)
7718 					chk->data = NULL;
7719 				/*
7720 				 * set hb flag since we can use these for
7721 				 * RTO
7722 				 */
7723 				hbflag = 1;
7724 				asconf = 1;
7725 				/*
7726 				 * should sysctl this: don't bundle data
7727 				 * with ASCONF since it requires AUTH
7728 				 */
7729 				no_data_chunks = 1;
7730 				chk->sent = SCTP_DATAGRAM_SENT;
7731 				chk->snd_count++;
7732 				if (mtu == 0) {
7733 					/*
7734 					 * Ok we are out of room but we can
7735 					 * output without effecting the
7736 					 * flight size since this little guy
7737 					 * is a control only packet.
7738 					 */
7739 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7740 					/*
7741 					 * do NOT clear the asconf flag as
7742 					 * it is used to do appropriate
7743 					 * source address selection.
7744 					 */
7745 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7746 					    (struct sockaddr *)&net->ro._l_addr,
7747 					    outchain, auth_offset, auth,
7748 					    stcb->asoc.authinfo.active_keyid,
7749 					    no_fragmentflg, 0, NULL, asconf,
7750 					    inp->sctp_lport, stcb->rport,
7751 					    htonl(stcb->asoc.peer_vtag),
7752 					    net->port, so_locked, NULL))) {
7753 						if (error == ENOBUFS) {
7754 							asoc->ifp_had_enobuf = 1;
7755 							SCTP_STAT_INCR(sctps_lowlevelerr);
7756 						}
7757 						if (from_where == 0) {
7758 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
7759 						}
7760 						if (*now_filled == 0) {
7761 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7762 							*now_filled = 1;
7763 							*now = net->last_sent_time;
7764 						} else {
7765 							net->last_sent_time = *now;
7766 						}
7767 						hbflag = 0;
7768 						/* error, could not output */
7769 						if (error == EHOSTUNREACH) {
7770 							/*
7771 							 * Destination went
7772 							 * unreachable
7773 							 * during this send
7774 							 */
7775 							sctp_move_to_an_alt(stcb, asoc, net);
7776 						}
7777 						*reason_code = 7;
7778 						continue;
7779 					} else
7780 						asoc->ifp_had_enobuf = 0;
7781 					if (*now_filled == 0) {
7782 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7783 						*now_filled = 1;
7784 						*now = net->last_sent_time;
7785 					} else {
7786 						net->last_sent_time = *now;
7787 					}
7788 					hbflag = 0;
7789 					/*
7790 					 * increase the number we sent, if a
7791 					 * cookie is sent we don't tell them
7792 					 * any was sent out.
7793 					 */
7794 					outchain = endoutchain = NULL;
7795 					auth = NULL;
7796 					auth_offset = 0;
7797 					if (!no_out_cnt)
7798 						*num_out += ctl_cnt;
7799 					/* recalc a clean slate and setup */
7800 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7801 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7802 					} else {
7803 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7804 					}
7805 					to_out = 0;
7806 					no_fragmentflg = 1;
7807 				}
7808 			}
7809 		}
7810 		/************************/
7811 		/* Control transmission */
7812 		/************************/
7813 		/* Now first lets go through the control queue */
7814 		for (chk = TAILQ_FIRST(&asoc->control_send_queue);
7815 		    chk; chk = nchk) {
7816 			nchk = TAILQ_NEXT(chk, sctp_next);
7817 			if (chk->whoTo != net) {
7818 				/*
7819 				 * No, not sent to the network we are
7820 				 * looking at
7821 				 */
7822 				continue;
7823 			}
7824 			if (chk->data == NULL) {
7825 				continue;
7826 			}
7827 			if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7828 				/*
7829 				 * It must be unsent. Cookies and ASCONF's
7830 				 * hang around but there timers will force
7831 				 * when marked for resend.
7832 				 */
7833 				continue;
7834 			}
7835 			/*
7836 			 * if no AUTH is yet included and this chunk
7837 			 * requires it, make sure to account for it.  We
7838 			 * don't apply the size until the AUTH chunk is
7839 			 * actually added below in case there is no room for
7840 			 * this chunk. NOTE: we overload the use of "omtu"
7841 			 * here
7842 			 */
7843 			if ((auth == NULL) &&
7844 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7845 			    stcb->asoc.peer_auth_chunks)) {
7846 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7847 			} else
7848 				omtu = 0;
7849 			/* Here we do NOT factor the r_mtu */
7850 			if ((chk->send_size <= (int)(mtu - omtu)) ||
7851 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7852 				/*
7853 				 * We probably should glom the mbuf chain
7854 				 * from the chk->data for control but the
7855 				 * problem is it becomes yet one more level
7856 				 * of tracking to do if for some reason
7857 				 * output fails. Then I have got to
7858 				 * reconstruct the merged control chain.. el
7859 				 * yucko.. for now we take the easy way and
7860 				 * do the copy
7861 				 */
7862 				/*
7863 				 * Add an AUTH chunk, if chunk requires it
7864 				 * save the offset into the chain for AUTH
7865 				 */
7866 				if ((auth == NULL) &&
7867 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7868 				    stcb->asoc.peer_auth_chunks))) {
7869 					outchain = sctp_add_auth_chunk(outchain,
7870 					    &endoutchain,
7871 					    &auth,
7872 					    &auth_offset,
7873 					    stcb,
7874 					    chk->rec.chunk_id.id);
7875 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7876 				}
7877 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7878 				    (int)chk->rec.chunk_id.can_take_data,
7879 				    chk->send_size, chk->copy_by_ref);
7880 				if (outchain == NULL) {
7881 					*reason_code = 8;
7882 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7883 					return (ENOMEM);
7884 				}
7885 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7886 				/* update our MTU size */
7887 				if (mtu > (chk->send_size + omtu))
7888 					mtu -= (chk->send_size + omtu);
7889 				else
7890 					mtu = 0;
7891 				to_out += (chk->send_size + omtu);
7892 				/* Do clear IP_DF ? */
7893 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7894 					no_fragmentflg = 0;
7895 				}
7896 				if (chk->rec.chunk_id.can_take_data)
7897 					chk->data = NULL;
7898 				/* Mark things to be removed, if needed */
7899 				if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7900 				    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
7901 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7902 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7903 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7904 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7905 				    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7906 				    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7907 				    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7908 				    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7909 				    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7910 
7911 					if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7912 						hbflag = 1;
7913 						/*
7914 						 * JRS 5/14/07 - Set the
7915 						 * flag to say a heartbeat
7916 						 * is being sent.
7917 						 */
7918 						pf_hbflag = 1;
7919 					}
7920 					/* remove these chunks at the end */
7921 					if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7922 					    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7923 						/* turn off the timer */
7924 						if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7925 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7926 							    inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7927 						}
7928 					}
7929 					ctl_cnt++;
7930 				} else {
7931 					/*
7932 					 * Other chunks, since they have
7933 					 * timers running (i.e. COOKIE) we
7934 					 * just "trust" that it gets sent or
7935 					 * retransmitted.
7936 					 */
7937 					ctl_cnt++;
7938 					if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7939 						cookie = 1;
7940 						no_out_cnt = 1;
7941 					}
7942 					chk->sent = SCTP_DATAGRAM_SENT;
7943 					chk->snd_count++;
7944 				}
7945 				if (mtu == 0) {
7946 					/*
7947 					 * Ok we are out of room but we can
7948 					 * output without effecting the
7949 					 * flight size since this little guy
7950 					 * is a control only packet.
7951 					 */
7952 					if (asconf) {
7953 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7954 						/*
7955 						 * do NOT clear the asconf
7956 						 * flag as it is used to do
7957 						 * appropriate source
7958 						 * address selection.
7959 						 */
7960 					}
7961 					if (cookie) {
7962 						sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7963 						cookie = 0;
7964 					}
7965 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7966 					    (struct sockaddr *)&net->ro._l_addr,
7967 					    outchain,
7968 					    auth_offset, auth,
7969 					    stcb->asoc.authinfo.active_keyid,
7970 					    no_fragmentflg, 0, NULL, asconf,
7971 					    inp->sctp_lport, stcb->rport,
7972 					    htonl(stcb->asoc.peer_vtag),
7973 					    net->port, so_locked, NULL))) {
7974 						if (error == ENOBUFS) {
7975 							asoc->ifp_had_enobuf = 1;
7976 							SCTP_STAT_INCR(sctps_lowlevelerr);
7977 						}
7978 						if (from_where == 0) {
7979 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
7980 						}
7981 						/* error, could not output */
7982 						if (hbflag) {
7983 							if (*now_filled == 0) {
7984 								(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7985 								*now_filled = 1;
7986 								*now = net->last_sent_time;
7987 							} else {
7988 								net->last_sent_time = *now;
7989 							}
7990 							hbflag = 0;
7991 						}
7992 						if (error == EHOSTUNREACH) {
7993 							/*
7994 							 * Destination went
7995 							 * unreachable
7996 							 * during this send
7997 							 */
7998 							sctp_move_to_an_alt(stcb, asoc, net);
7999 						}
8000 						*reason_code = 7;
8001 						continue;
8002 					} else
8003 						asoc->ifp_had_enobuf = 0;
8004 					/* Only HB or ASCONF advances time */
8005 					if (hbflag) {
8006 						if (*now_filled == 0) {
8007 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8008 							*now_filled = 1;
8009 							*now = net->last_sent_time;
8010 						} else {
8011 							net->last_sent_time = *now;
8012 						}
8013 						hbflag = 0;
8014 					}
8015 					/*
8016 					 * increase the number we sent, if a
8017 					 * cookie is sent we don't tell them
8018 					 * any was sent out.
8019 					 */
8020 					outchain = endoutchain = NULL;
8021 					auth = NULL;
8022 					auth_offset = 0;
8023 					if (!no_out_cnt)
8024 						*num_out += ctl_cnt;
8025 					/* recalc a clean slate and setup */
8026 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8027 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
8028 					} else {
8029 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
8030 					}
8031 					to_out = 0;
8032 					no_fragmentflg = 1;
8033 				}
8034 			}
8035 		}
8036 		/* JRI: if dest is in PF state, do not send data to it */
8037 		if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
8038 		    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
8039 		    (net->dest_state & SCTP_ADDR_PF)) {
8040 			goto no_data_fill;
8041 		}
8042 		if (net->flight_size >= net->cwnd) {
8043 			goto no_data_fill;
8044 		}
8045 		if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off)) &&
8046 		    (net->flight_size > max_rwnd_per_dest)) {
8047 			goto no_data_fill;
8048 		}
8049 		/*********************/
8050 		/* Data transmission */
8051 		/*********************/
8052 		/*
8053 		 * if AUTH for DATA is required and no AUTH has been added
8054 		 * yet, account for this in the mtu now... if no data can be
8055 		 * bundled, this adjustment won't matter anyways since the
8056 		 * packet will be going out...
8057 		 */
8058 		data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8059 		    stcb->asoc.peer_auth_chunks);
8060 		if (data_auth_reqd && (auth == NULL)) {
8061 			mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8062 		}
8063 		/* now lets add any data within the MTU constraints */
8064 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8065 		case AF_INET:
8066 			if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8067 				omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8068 			else
8069 				omtu = 0;
8070 			break;
8071 #ifdef INET6
8072 		case AF_INET6:
8073 			if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8074 				omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8075 			else
8076 				omtu = 0;
8077 			break;
8078 #endif
8079 		default:
8080 			/* TSNH */
8081 			omtu = 0;
8082 			break;
8083 		}
8084 		if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) &&
8085 		    (skip_data_for_this_net == 0)) ||
8086 		    (cookie)) {
8087 			for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
8088 				if (no_data_chunks) {
8089 					/* let only control go out */
8090 					*reason_code = 1;
8091 					break;
8092 				}
8093 				if (net->flight_size >= net->cwnd) {
8094 					/* skip this net, no room for data */
8095 					*reason_code = 2;
8096 					break;
8097 				}
8098 				nchk = TAILQ_NEXT(chk, sctp_next);
8099 				if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
8100 					if (chk->whoTo != net) {
8101 						/*
8102 						 * For CMT, steal the data
8103 						 * to this network if its
8104 						 * not set here.
8105 						 */
8106 						sctp_free_remote_addr(chk->whoTo);
8107 						chk->whoTo = net;
8108 						atomic_add_int(&chk->whoTo->ref_count, 1);
8109 					}
8110 				} else if (chk->whoTo != net) {
8111 					/* No, not sent to this net */
8112 					continue;
8113 				}
8114 				if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8115 					/*-
8116 					 * strange, we have a chunk that is
8117 					 * to big for its destination and
8118 					 * yet no fragment ok flag.
8119 					 * Something went wrong when the
8120 					 * PMTU changed...we did not mark
8121 					 * this chunk for some reason?? I
8122 					 * will fix it here by letting IP
8123 					 * fragment it for now and printing
8124 					 * a warning. This really should not
8125 					 * happen ...
8126 					 */
8127 					SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8128 					    chk->send_size, mtu);
8129 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8130 				}
8131 				if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8132 				    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
8133 					struct sctp_data_chunk *dchkh;
8134 
8135 					dchkh = mtod(chk->data, struct sctp_data_chunk *);
8136 					dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8137 				}
8138 				if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8139 				    ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8140 					/* ok we will add this one */
8141 
8142 					/*
8143 					 * Add an AUTH chunk, if chunk
8144 					 * requires it, save the offset into
8145 					 * the chain for AUTH
8146 					 */
8147 					if (data_auth_reqd) {
8148 						if (auth == NULL) {
8149 							outchain = sctp_add_auth_chunk(outchain,
8150 							    &endoutchain,
8151 							    &auth,
8152 							    &auth_offset,
8153 							    stcb,
8154 							    SCTP_DATA);
8155 							auth_keyid = chk->auth_keyid;
8156 							override_ok = 0;
8157 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8158 						} else if (override_ok) {
8159 							/*
8160 							 * use this data's
8161 							 * keyid
8162 							 */
8163 							auth_keyid = chk->auth_keyid;
8164 							override_ok = 0;
8165 						} else if (auth_keyid != chk->auth_keyid) {
8166 							/*
8167 							 * different keyid,
8168 							 * so done bundling
8169 							 */
8170 							break;
8171 						}
8172 					}
8173 					outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8174 					    chk->send_size, chk->copy_by_ref);
8175 					if (outchain == NULL) {
8176 						SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8177 						if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8178 							sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8179 						}
8180 						*reason_code = 3;
8181 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8182 						return (ENOMEM);
8183 					}
8184 					/* upate our MTU size */
8185 					/* Do clear IP_DF ? */
8186 					if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8187 						no_fragmentflg = 0;
8188 					}
8189 					/* unsigned subtraction of mtu */
8190 					if (mtu > chk->send_size)
8191 						mtu -= chk->send_size;
8192 					else
8193 						mtu = 0;
8194 					/* unsigned subtraction of r_mtu */
8195 					if (r_mtu > chk->send_size)
8196 						r_mtu -= chk->send_size;
8197 					else
8198 						r_mtu = 0;
8199 
8200 					to_out += chk->send_size;
8201 					if ((to_out > mx_mtu) && no_fragmentflg) {
8202 #ifdef INVARIANTS
8203 						panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8204 #else
8205 						SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8206 						    mx_mtu, to_out);
8207 #endif
8208 					}
8209 					chk->window_probe = 0;
8210 					data_list[bundle_at++] = chk;
8211 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8212 						mtu = 0;
8213 						break;
8214 					}
8215 					if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8216 						if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8217 							SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8218 						} else {
8219 							SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8220 						}
8221 						if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8222 						    ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8223 							/*
8224 							 * Count number of
8225 							 * user msg's that
8226 							 * were fragmented
8227 							 * we do this by
8228 							 * counting when we
8229 							 * see a LAST
8230 							 * fragment only.
8231 							 */
8232 							SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8233 					}
8234 					if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8235 						if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8236 							data_list[0]->window_probe = 1;
8237 							net->window_probe = 1;
8238 						}
8239 						break;
8240 					}
8241 				} else {
8242 					/*
8243 					 * Must be sent in order of the
8244 					 * TSN's (on a network)
8245 					 */
8246 					break;
8247 				}
8248 			}	/* for (chunk gather loop for this net) */
8249 		}		/* if asoc.state OPEN */
8250 no_data_fill:
8251 		/* Is there something to send for this destination? */
8252 		if (outchain) {
8253 			/* We may need to start a control timer or two */
8254 			if (asconf) {
8255 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8256 				    stcb, net);
8257 				/*
8258 				 * do NOT clear the asconf flag as it is
8259 				 * used to do appropriate source address
8260 				 * selection.
8261 				 */
8262 			}
8263 			if (cookie) {
8264 				sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8265 				cookie = 0;
8266 			}
8267 			/* must start a send timer if data is being sent */
8268 			if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8269 				/*
8270 				 * no timer running on this destination
8271 				 * restart it.
8272 				 */
8273 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8274 			} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
8275 				    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
8276 				    pf_hbflag &&
8277 				    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
8278 			    (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8279 				/*
8280 				 * JRS 5/14/07 - If a HB has been sent to a
8281 				 * PF destination and no T3 timer is
8282 				 * currently running, start the T3 timer to
8283 				 * track the HBs that were sent.
8284 				 */
8285 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8286 			}
8287 			/* Now send it, if there is anything to send :> */
8288 			if ((error = sctp_lowlevel_chunk_output(inp,
8289 			    stcb,
8290 			    net,
8291 			    (struct sockaddr *)&net->ro._l_addr,
8292 			    outchain,
8293 			    auth_offset,
8294 			    auth,
8295 			    auth_keyid,
8296 			    no_fragmentflg,
8297 			    bundle_at,
8298 			    data_list[0],
8299 			    asconf,
8300 			    inp->sctp_lport, stcb->rport,
8301 			    htonl(stcb->asoc.peer_vtag),
8302 			    net->port, so_locked, NULL))) {
8303 				/* error, we could not output */
8304 				if (error == ENOBUFS) {
8305 					SCTP_STAT_INCR(sctps_lowlevelerr);
8306 					asoc->ifp_had_enobuf = 1;
8307 				}
8308 				if (from_where == 0) {
8309 					SCTP_STAT_INCR(sctps_lowlevelerrusr);
8310 				}
8311 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8312 				if (hbflag) {
8313 					if (*now_filled == 0) {
8314 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8315 						*now_filled = 1;
8316 						*now = net->last_sent_time;
8317 					} else {
8318 						net->last_sent_time = *now;
8319 					}
8320 					hbflag = 0;
8321 				}
8322 				if (error == EHOSTUNREACH) {
8323 					/*
8324 					 * Destination went unreachable
8325 					 * during this send
8326 					 */
8327 					sctp_move_to_an_alt(stcb, asoc, net);
8328 				}
8329 				*reason_code = 6;
8330 				/*-
8331 				 * I add this line to be paranoid. As far as
8332 				 * I can tell the continue, takes us back to
8333 				 * the top of the for, but just to make sure
8334 				 * I will reset these again here.
8335 				 */
8336 				ctl_cnt = bundle_at = 0;
8337 				continue;	/* This takes us back to the
8338 						 * for() for the nets. */
8339 			} else {
8340 				asoc->ifp_had_enobuf = 0;
8341 			}
8342 			outchain = endoutchain = NULL;
8343 			auth = NULL;
8344 			auth_offset = 0;
8345 			if (bundle_at || hbflag) {
8346 				/* For data/asconf and hb set time */
8347 				if (*now_filled == 0) {
8348 					(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8349 					*now_filled = 1;
8350 					*now = net->last_sent_time;
8351 				} else {
8352 					net->last_sent_time = *now;
8353 				}
8354 			}
8355 			if (!no_out_cnt) {
8356 				*num_out += (ctl_cnt + bundle_at);
8357 			}
8358 			if (bundle_at) {
8359 				/* setup for a RTO measurement */
8360 				tsns_sent = data_list[0]->rec.data.TSN_seq;
8361 				/* fill time if not already filled */
8362 				if (*now_filled == 0) {
8363 					(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8364 					*now_filled = 1;
8365 					*now = asoc->time_last_sent;
8366 				} else {
8367 					asoc->time_last_sent = *now;
8368 				}
8369 				data_list[0]->do_rtt = 1;
8370 				SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8371 				sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8372 				if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8373 					if (net->flight_size < net->cwnd) {
8374 						/* start or restart it */
8375 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8376 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8377 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
8378 						}
8379 						SCTP_STAT_INCR(sctps_earlyfrstrout);
8380 						sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
8381 					} else {
8382 						/* stop it if its running */
8383 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8384 							SCTP_STAT_INCR(sctps_earlyfrstpout);
8385 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8386 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
8387 						}
8388 					}
8389 				}
8390 			}
8391 			if (one_chunk) {
8392 				break;
8393 			}
8394 		}
8395 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8396 			sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8397 		}
8398 	}
8399 	if (old_start_at == NULL) {
8400 		old_start_at = start_at;
8401 		start_at = TAILQ_FIRST(&asoc->nets);
8402 		if (old_start_at)
8403 			goto again_one_more_time;
8404 	}
8405 	/*
8406 	 * At the end there should be no NON timed chunks hanging on this
8407 	 * queue.
8408 	 */
8409 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8410 		sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8411 	}
8412 	if ((*num_out == 0) && (*reason_code == 0)) {
8413 		*reason_code = 4;
8414 	} else {
8415 		*reason_code = 5;
8416 	}
8417 	sctp_clean_up_ctl(stcb, asoc);
8418 	return (0);
8419 }
8420 
8421 void
8422 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8423 {
8424 	/*-
8425 	 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8426 	 * the control chunk queue.
8427 	 */
8428 	struct sctp_chunkhdr *hdr;
8429 	struct sctp_tmit_chunk *chk;
8430 	struct mbuf *mat;
8431 
8432 	SCTP_TCB_LOCK_ASSERT(stcb);
8433 	sctp_alloc_a_chunk(stcb, chk);
8434 	if (chk == NULL) {
8435 		/* no memory */
8436 		sctp_m_freem(op_err);
8437 		return;
8438 	}
8439 	chk->copy_by_ref = 0;
8440 	SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8441 	if (op_err == NULL) {
8442 		sctp_free_a_chunk(stcb, chk);
8443 		return;
8444 	}
8445 	chk->send_size = 0;
8446 	mat = op_err;
8447 	while (mat != NULL) {
8448 		chk->send_size += SCTP_BUF_LEN(mat);
8449 		mat = SCTP_BUF_NEXT(mat);
8450 	}
8451 	chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8452 	chk->rec.chunk_id.can_take_data = 1;
8453 	chk->sent = SCTP_DATAGRAM_UNSENT;
8454 	chk->snd_count = 0;
8455 	chk->flags = 0;
8456 	chk->asoc = &stcb->asoc;
8457 	chk->data = op_err;
8458 	chk->whoTo = chk->asoc->primary_destination;
8459 	atomic_add_int(&chk->whoTo->ref_count, 1);
8460 	hdr = mtod(op_err, struct sctp_chunkhdr *);
8461 	hdr->chunk_type = SCTP_OPERATION_ERROR;
8462 	hdr->chunk_flags = 0;
8463 	hdr->chunk_length = htons(chk->send_size);
8464 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8465 	    chk,
8466 	    sctp_next);
8467 	chk->asoc->ctrl_queue_cnt++;
8468 }
8469 
8470 int
8471 sctp_send_cookie_echo(struct mbuf *m,
8472     int offset,
8473     struct sctp_tcb *stcb,
8474     struct sctp_nets *net)
8475 {
8476 	/*-
8477 	 * pull out the cookie and put it at the front of the control chunk
8478 	 * queue.
8479 	 */
8480 	int at;
8481 	struct mbuf *cookie;
8482 	struct sctp_paramhdr parm, *phdr;
8483 	struct sctp_chunkhdr *hdr;
8484 	struct sctp_tmit_chunk *chk;
8485 	uint16_t ptype, plen;
8486 
8487 	/* First find the cookie in the param area */
8488 	cookie = NULL;
8489 	at = offset + sizeof(struct sctp_init_chunk);
8490 
8491 	SCTP_TCB_LOCK_ASSERT(stcb);
8492 	do {
8493 		phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8494 		if (phdr == NULL) {
8495 			return (-3);
8496 		}
8497 		ptype = ntohs(phdr->param_type);
8498 		plen = ntohs(phdr->param_length);
8499 		if (ptype == SCTP_STATE_COOKIE) {
8500 			int pad;
8501 
8502 			/* found the cookie */
8503 			if ((pad = (plen % 4))) {
8504 				plen += 4 - pad;
8505 			}
8506 			cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8507 			if (cookie == NULL) {
8508 				/* No memory */
8509 				return (-2);
8510 			}
8511 #ifdef SCTP_MBUF_LOGGING
8512 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8513 				struct mbuf *mat;
8514 
8515 				mat = cookie;
8516 				while (mat) {
8517 					if (SCTP_BUF_IS_EXTENDED(mat)) {
8518 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8519 					}
8520 					mat = SCTP_BUF_NEXT(mat);
8521 				}
8522 			}
8523 #endif
8524 			break;
8525 		}
8526 		at += SCTP_SIZE32(plen);
8527 	} while (phdr);
8528 	if (cookie == NULL) {
8529 		/* Did not find the cookie */
8530 		return (-3);
8531 	}
8532 	/* ok, we got the cookie lets change it into a cookie echo chunk */
8533 
8534 	/* first the change from param to cookie */
8535 	hdr = mtod(cookie, struct sctp_chunkhdr *);
8536 	hdr->chunk_type = SCTP_COOKIE_ECHO;
8537 	hdr->chunk_flags = 0;
8538 	/* get the chunk stuff now and place it in the FRONT of the queue */
8539 	sctp_alloc_a_chunk(stcb, chk);
8540 	if (chk == NULL) {
8541 		/* no memory */
8542 		sctp_m_freem(cookie);
8543 		return (-5);
8544 	}
8545 	chk->copy_by_ref = 0;
8546 	chk->send_size = plen;
8547 	chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8548 	chk->rec.chunk_id.can_take_data = 0;
8549 	chk->sent = SCTP_DATAGRAM_UNSENT;
8550 	chk->snd_count = 0;
8551 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8552 	chk->asoc = &stcb->asoc;
8553 	chk->data = cookie;
8554 	chk->whoTo = chk->asoc->primary_destination;
8555 	atomic_add_int(&chk->whoTo->ref_count, 1);
8556 	TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8557 	chk->asoc->ctrl_queue_cnt++;
8558 	return (0);
8559 }
8560 
8561 void
8562 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8563     struct mbuf *m,
8564     int offset,
8565     int chk_length,
8566     struct sctp_nets *net)
8567 {
8568 	/*
8569 	 * take a HB request and make it into a HB ack and send it.
8570 	 */
8571 	struct mbuf *outchain;
8572 	struct sctp_chunkhdr *chdr;
8573 	struct sctp_tmit_chunk *chk;
8574 
8575 
8576 	if (net == NULL)
8577 		/* must have a net pointer */
8578 		return;
8579 
8580 	outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
8581 	if (outchain == NULL) {
8582 		/* gak out of memory */
8583 		return;
8584 	}
8585 #ifdef SCTP_MBUF_LOGGING
8586 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8587 		struct mbuf *mat;
8588 
8589 		mat = outchain;
8590 		while (mat) {
8591 			if (SCTP_BUF_IS_EXTENDED(mat)) {
8592 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8593 			}
8594 			mat = SCTP_BUF_NEXT(mat);
8595 		}
8596 	}
8597 #endif
8598 	chdr = mtod(outchain, struct sctp_chunkhdr *);
8599 	chdr->chunk_type = SCTP_HEARTBEAT_ACK;
8600 	chdr->chunk_flags = 0;
8601 	if (chk_length % 4) {
8602 		/* need pad */
8603 		uint32_t cpthis = 0;
8604 		int padlen;
8605 
8606 		padlen = 4 - (chk_length % 4);
8607 		m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
8608 	}
8609 	sctp_alloc_a_chunk(stcb, chk);
8610 	if (chk == NULL) {
8611 		/* no memory */
8612 		sctp_m_freem(outchain);
8613 		return;
8614 	}
8615 	chk->copy_by_ref = 0;
8616 	chk->send_size = chk_length;
8617 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
8618 	chk->rec.chunk_id.can_take_data = 1;
8619 	chk->sent = SCTP_DATAGRAM_UNSENT;
8620 	chk->snd_count = 0;
8621 	chk->flags = 0;
8622 	chk->asoc = &stcb->asoc;
8623 	chk->data = outchain;
8624 	chk->whoTo = net;
8625 	atomic_add_int(&chk->whoTo->ref_count, 1);
8626 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8627 	chk->asoc->ctrl_queue_cnt++;
8628 }
8629 
8630 void
8631 sctp_send_cookie_ack(struct sctp_tcb *stcb)
8632 {
8633 	/* formulate and queue a cookie-ack back to sender */
8634 	struct mbuf *cookie_ack;
8635 	struct sctp_chunkhdr *hdr;
8636 	struct sctp_tmit_chunk *chk;
8637 
8638 	cookie_ack = NULL;
8639 	SCTP_TCB_LOCK_ASSERT(stcb);
8640 
8641 	cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
8642 	if (cookie_ack == NULL) {
8643 		/* no mbuf's */
8644 		return;
8645 	}
8646 	SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
8647 	sctp_alloc_a_chunk(stcb, chk);
8648 	if (chk == NULL) {
8649 		/* no memory */
8650 		sctp_m_freem(cookie_ack);
8651 		return;
8652 	}
8653 	chk->copy_by_ref = 0;
8654 	chk->send_size = sizeof(struct sctp_chunkhdr);
8655 	chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
8656 	chk->rec.chunk_id.can_take_data = 1;
8657 	chk->sent = SCTP_DATAGRAM_UNSENT;
8658 	chk->snd_count = 0;
8659 	chk->flags = 0;
8660 	chk->asoc = &stcb->asoc;
8661 	chk->data = cookie_ack;
8662 	if (chk->asoc->last_control_chunk_from != NULL) {
8663 		chk->whoTo = chk->asoc->last_control_chunk_from;
8664 	} else {
8665 		chk->whoTo = chk->asoc->primary_destination;
8666 	}
8667 	atomic_add_int(&chk->whoTo->ref_count, 1);
8668 	hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
8669 	hdr->chunk_type = SCTP_COOKIE_ACK;
8670 	hdr->chunk_flags = 0;
8671 	hdr->chunk_length = htons(chk->send_size);
8672 	SCTP_BUF_LEN(cookie_ack) = chk->send_size;
8673 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8674 	chk->asoc->ctrl_queue_cnt++;
8675 	return;
8676 }
8677 
8678 
8679 void
8680 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
8681 {
8682 	/* formulate and queue a SHUTDOWN-ACK back to the sender */
8683 	struct mbuf *m_shutdown_ack;
8684 	struct sctp_shutdown_ack_chunk *ack_cp;
8685 	struct sctp_tmit_chunk *chk;
8686 
8687 	m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8688 	if (m_shutdown_ack == NULL) {
8689 		/* no mbuf's */
8690 		return;
8691 	}
8692 	SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
8693 	sctp_alloc_a_chunk(stcb, chk);
8694 	if (chk == NULL) {
8695 		/* no memory */
8696 		sctp_m_freem(m_shutdown_ack);
8697 		return;
8698 	}
8699 	chk->copy_by_ref = 0;
8700 	chk->send_size = sizeof(struct sctp_chunkhdr);
8701 	chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
8702 	chk->rec.chunk_id.can_take_data = 1;
8703 	chk->sent = SCTP_DATAGRAM_UNSENT;
8704 	chk->snd_count = 0;
8705 	chk->flags = 0;
8706 	chk->asoc = &stcb->asoc;
8707 	chk->data = m_shutdown_ack;
8708 	chk->whoTo = net;
8709 	atomic_add_int(&net->ref_count, 1);
8710 
8711 	ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
8712 	ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
8713 	ack_cp->ch.chunk_flags = 0;
8714 	ack_cp->ch.chunk_length = htons(chk->send_size);
8715 	SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
8716 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8717 	chk->asoc->ctrl_queue_cnt++;
8718 	return;
8719 }
8720 
8721 void
8722 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
8723 {
8724 	/* formulate and queue a SHUTDOWN to the sender */
8725 	struct mbuf *m_shutdown;
8726 	struct sctp_shutdown_chunk *shutdown_cp;
8727 	struct sctp_tmit_chunk *chk;
8728 
8729 	m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8730 	if (m_shutdown == NULL) {
8731 		/* no mbuf's */
8732 		return;
8733 	}
8734 	SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
8735 	sctp_alloc_a_chunk(stcb, chk);
8736 	if (chk == NULL) {
8737 		/* no memory */
8738 		sctp_m_freem(m_shutdown);
8739 		return;
8740 	}
8741 	chk->copy_by_ref = 0;
8742 	chk->send_size = sizeof(struct sctp_shutdown_chunk);
8743 	chk->rec.chunk_id.id = SCTP_SHUTDOWN;
8744 	chk->rec.chunk_id.can_take_data = 1;
8745 	chk->sent = SCTP_DATAGRAM_UNSENT;
8746 	chk->snd_count = 0;
8747 	chk->flags = 0;
8748 	chk->asoc = &stcb->asoc;
8749 	chk->data = m_shutdown;
8750 	chk->whoTo = net;
8751 	atomic_add_int(&net->ref_count, 1);
8752 
8753 	shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
8754 	shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
8755 	shutdown_cp->ch.chunk_flags = 0;
8756 	shutdown_cp->ch.chunk_length = htons(chk->send_size);
8757 	shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
8758 	SCTP_BUF_LEN(m_shutdown) = chk->send_size;
8759 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8760 	chk->asoc->ctrl_queue_cnt++;
8761 	return;
8762 }
8763 
8764 void
8765 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
8766 {
8767 	/*
8768 	 * formulate and queue an ASCONF to the peer. ASCONF parameters
8769 	 * should be queued on the assoc queue.
8770 	 */
8771 	struct sctp_tmit_chunk *chk;
8772 	struct mbuf *m_asconf;
8773 	int len;
8774 
8775 	SCTP_TCB_LOCK_ASSERT(stcb);
8776 
8777 	if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
8778 	    (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
8779 		/* can't send a new one if there is one in flight already */
8780 		return;
8781 	}
8782 	/* compose an ASCONF chunk, maximum length is PMTU */
8783 	m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
8784 	if (m_asconf == NULL) {
8785 		return;
8786 	}
8787 	sctp_alloc_a_chunk(stcb, chk);
8788 	if (chk == NULL) {
8789 		/* no memory */
8790 		sctp_m_freem(m_asconf);
8791 		return;
8792 	}
8793 	chk->copy_by_ref = 0;
8794 	chk->data = m_asconf;
8795 	chk->send_size = len;
8796 	chk->rec.chunk_id.id = SCTP_ASCONF;
8797 	chk->rec.chunk_id.can_take_data = 0;
8798 	chk->sent = SCTP_DATAGRAM_UNSENT;
8799 	chk->snd_count = 0;
8800 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8801 	chk->asoc = &stcb->asoc;
8802 	chk->whoTo = net;
8803 	atomic_add_int(&chk->whoTo->ref_count, 1);
8804 	TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
8805 	chk->asoc->ctrl_queue_cnt++;
8806 	return;
8807 }
8808 
8809 void
8810 sctp_send_asconf_ack(struct sctp_tcb *stcb)
8811 {
8812 	/*
8813 	 * formulate and queue a asconf-ack back to sender. the asconf-ack
8814 	 * must be stored in the tcb.
8815 	 */
8816 	struct sctp_tmit_chunk *chk;
8817 	struct sctp_asconf_ack *ack, *latest_ack;
8818 	struct mbuf *m_ack, *m;
8819 	struct sctp_nets *net = NULL;
8820 
8821 	SCTP_TCB_LOCK_ASSERT(stcb);
8822 	/* Get the latest ASCONF-ACK */
8823 	latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
8824 	if (latest_ack == NULL) {
8825 		return;
8826 	}
8827 	if (latest_ack->last_sent_to != NULL &&
8828 	    latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
8829 		/* we're doing a retransmission */
8830 		net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
8831 		if (net == NULL) {
8832 			/* no alternate */
8833 			if (stcb->asoc.last_control_chunk_from == NULL)
8834 				net = stcb->asoc.primary_destination;
8835 			else
8836 				net = stcb->asoc.last_control_chunk_from;
8837 		}
8838 	} else {
8839 		/* normal case */
8840 		if (stcb->asoc.last_control_chunk_from == NULL)
8841 			net = stcb->asoc.primary_destination;
8842 		else
8843 			net = stcb->asoc.last_control_chunk_from;
8844 	}
8845 	latest_ack->last_sent_to = net;
8846 
8847 	TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
8848 		if (ack->data == NULL) {
8849 			continue;
8850 		}
8851 		/* copy the asconf_ack */
8852 		m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
8853 		if (m_ack == NULL) {
8854 			/* couldn't copy it */
8855 			return;
8856 		}
8857 #ifdef SCTP_MBUF_LOGGING
8858 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8859 			struct mbuf *mat;
8860 
8861 			mat = m_ack;
8862 			while (mat) {
8863 				if (SCTP_BUF_IS_EXTENDED(mat)) {
8864 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8865 				}
8866 				mat = SCTP_BUF_NEXT(mat);
8867 			}
8868 		}
8869 #endif
8870 
8871 		sctp_alloc_a_chunk(stcb, chk);
8872 		if (chk == NULL) {
8873 			/* no memory */
8874 			if (m_ack)
8875 				sctp_m_freem(m_ack);
8876 			return;
8877 		}
8878 		chk->copy_by_ref = 0;
8879 
8880 		chk->whoTo = net;
8881 		chk->data = m_ack;
8882 		chk->send_size = 0;
8883 		/* Get size */
8884 		m = m_ack;
8885 		chk->send_size = ack->len;
8886 		chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8887 		chk->rec.chunk_id.can_take_data = 1;
8888 		chk->sent = SCTP_DATAGRAM_UNSENT;
8889 		chk->snd_count = 0;
8890 		chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;	/* XXX */
8891 		chk->asoc = &stcb->asoc;
8892 		atomic_add_int(&chk->whoTo->ref_count, 1);
8893 
8894 		TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8895 		chk->asoc->ctrl_queue_cnt++;
8896 	}
8897 	return;
8898 }
8899 
8900 
8901 static int
8902 sctp_chunk_retransmission(struct sctp_inpcb *inp,
8903     struct sctp_tcb *stcb,
8904     struct sctp_association *asoc,
8905     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
8906 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8907     SCTP_UNUSED
8908 #endif
8909 )
8910 {
8911 	/*-
8912 	 * send out one MTU of retransmission. If fast_retransmit is
8913 	 * happening we ignore the cwnd. Otherwise we obey the cwnd and
8914 	 * rwnd. For a Cookie or Asconf in the control chunk queue we
8915 	 * retransmit them by themselves.
8916 	 *
8917 	 * For data chunks we will pick out the lowest TSN's in the sent_queue
8918 	 * marked for resend and bundle them all together (up to a MTU of
8919 	 * destination). The address to send to should have been
8920 	 * selected/changed where the retransmission was marked (i.e. in FR
8921 	 * or t3-timeout routines).
8922 	 */
8923 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8924 	struct sctp_tmit_chunk *chk, *fwd;
8925 	struct mbuf *m, *endofchain;
8926 	struct sctp_nets *net = NULL;
8927 	uint32_t tsns_sent = 0;
8928 	int no_fragmentflg, bundle_at, cnt_thru;
8929 	unsigned int mtu;
8930 	int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
8931 	struct sctp_auth_chunk *auth = NULL;
8932 	uint32_t auth_offset = 0;
8933 	uint16_t auth_keyid;
8934 	int override_ok = 1;
8935 	int data_auth_reqd = 0;
8936 	uint32_t dmtu = 0;
8937 
8938 	SCTP_TCB_LOCK_ASSERT(stcb);
8939 	tmr_started = ctl_cnt = bundle_at = error = 0;
8940 	no_fragmentflg = 1;
8941 	fwd_tsn = 0;
8942 	*cnt_out = 0;
8943 	fwd = NULL;
8944 	endofchain = m = NULL;
8945 	auth_keyid = stcb->asoc.authinfo.active_keyid;
8946 #ifdef SCTP_AUDITING_ENABLED
8947 	sctp_audit_log(0xC3, 1);
8948 #endif
8949 	if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
8950 	    (TAILQ_EMPTY(&asoc->control_send_queue))) {
8951 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
8952 		    asoc->sent_queue_retran_cnt);
8953 		asoc->sent_queue_cnt = 0;
8954 		asoc->sent_queue_cnt_removeable = 0;
8955 		/* send back 0/0 so we enter normal transmission */
8956 		*cnt_out = 0;
8957 		return (0);
8958 	}
8959 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8960 		if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
8961 		    (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
8962 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
8963 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
8964 				continue;
8965 			}
8966 			if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8967 				if (chk != asoc->str_reset) {
8968 					/*
8969 					 * not eligible for retran if its
8970 					 * not ours
8971 					 */
8972 					continue;
8973 				}
8974 			}
8975 			ctl_cnt++;
8976 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8977 				fwd_tsn = 1;
8978 				fwd = chk;
8979 			}
8980 			/*
8981 			 * Add an AUTH chunk, if chunk requires it save the
8982 			 * offset into the chain for AUTH
8983 			 */
8984 			if ((auth == NULL) &&
8985 			    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8986 			    stcb->asoc.peer_auth_chunks))) {
8987 				m = sctp_add_auth_chunk(m, &endofchain,
8988 				    &auth, &auth_offset,
8989 				    stcb,
8990 				    chk->rec.chunk_id.id);
8991 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8992 			}
8993 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8994 			break;
8995 		}
8996 	}
8997 	one_chunk = 0;
8998 	cnt_thru = 0;
8999 	/* do we have control chunks to retransmit? */
9000 	if (m != NULL) {
9001 		/* Start a timer no matter if we suceed or fail */
9002 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9003 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
9004 		} else if (chk->rec.chunk_id.id == SCTP_ASCONF)
9005 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
9006 		chk->snd_count++;	/* update our count */
9007 		if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
9008 		    (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
9009 		    auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9010 		    no_fragmentflg, 0, NULL, 0,
9011 		    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9012 		    chk->whoTo->port, so_locked, NULL))) {
9013 			SCTP_STAT_INCR(sctps_lowlevelerr);
9014 			return (error);
9015 		}
9016 		m = endofchain = NULL;
9017 		auth = NULL;
9018 		auth_offset = 0;
9019 		/*
9020 		 * We don't want to mark the net->sent time here since this
9021 		 * we use this for HB and retrans cannot measure RTT
9022 		 */
9023 		/* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
9024 		*cnt_out += 1;
9025 		chk->sent = SCTP_DATAGRAM_SENT;
9026 		sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
9027 		if (fwd_tsn == 0) {
9028 			return (0);
9029 		} else {
9030 			/* Clean up the fwd-tsn list */
9031 			sctp_clean_up_ctl(stcb, asoc);
9032 			return (0);
9033 		}
9034 	}
9035 	/*
9036 	 * Ok, it is just data retransmission we need to do or that and a
9037 	 * fwd-tsn with it all.
9038 	 */
9039 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
9040 		return (SCTP_RETRAN_DONE);
9041 	}
9042 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
9043 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
9044 		/* not yet open, resend the cookie and that is it */
9045 		return (1);
9046 	}
9047 #ifdef SCTP_AUDITING_ENABLED
9048 	sctp_auditing(20, inp, stcb, NULL);
9049 #endif
9050 	data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9051 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9052 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
9053 			/* No, not sent to this net or not ready for rtx */
9054 			continue;
9055 		}
9056 		if (chk->data == NULL) {
9057 			printf("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
9058 			    chk->rec.data.TSN_seq, chk->snd_count, chk->sent);
9059 			continue;
9060 		}
9061 		if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9062 		    (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9063 			/* Gak, we have exceeded max unlucky retran, abort! */
9064 			SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
9065 			    chk->snd_count,
9066 			    SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
9067 			atomic_add_int(&stcb->asoc.refcnt, 1);
9068 			sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
9069 			SCTP_TCB_LOCK(stcb);
9070 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
9071 			return (SCTP_RETRAN_EXIT);
9072 		}
9073 		/* pick up the net */
9074 		net = chk->whoTo;
9075 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9076 			mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9077 		} else {
9078 			mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9079 		}
9080 
9081 		if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9082 			/* No room in peers rwnd */
9083 			uint32_t tsn;
9084 
9085 			tsn = asoc->last_acked_seq + 1;
9086 			if (tsn == chk->rec.data.TSN_seq) {
9087 				/*
9088 				 * we make a special exception for this
9089 				 * case. The peer has no rwnd but is missing
9090 				 * the lowest chunk.. which is probably what
9091 				 * is holding up the rwnd.
9092 				 */
9093 				goto one_chunk_around;
9094 			}
9095 			return (1);
9096 		}
9097 one_chunk_around:
9098 		if (asoc->peers_rwnd < mtu) {
9099 			one_chunk = 1;
9100 			if ((asoc->peers_rwnd == 0) &&
9101 			    (asoc->total_flight == 0)) {
9102 				chk->window_probe = 1;
9103 				chk->whoTo->window_probe = 1;
9104 			}
9105 		}
9106 #ifdef SCTP_AUDITING_ENABLED
9107 		sctp_audit_log(0xC3, 2);
9108 #endif
9109 		bundle_at = 0;
9110 		m = NULL;
9111 		net->fast_retran_ip = 0;
9112 		if (chk->rec.data.doing_fast_retransmit == 0) {
9113 			/*
9114 			 * if no FR in progress skip destination that have
9115 			 * flight_size > cwnd.
9116 			 */
9117 			if (net->flight_size >= net->cwnd) {
9118 				continue;
9119 			}
9120 		} else {
9121 			/*
9122 			 * Mark the destination net to have FR recovery
9123 			 * limits put on it.
9124 			 */
9125 			*fr_done = 1;
9126 			net->fast_retran_ip = 1;
9127 		}
9128 
9129 		/*
9130 		 * if no AUTH is yet included and this chunk requires it,
9131 		 * make sure to account for it.  We don't apply the size
9132 		 * until the AUTH chunk is actually added below in case
9133 		 * there is no room for this chunk.
9134 		 */
9135 		if (data_auth_reqd && (auth == NULL)) {
9136 			dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9137 		} else
9138 			dmtu = 0;
9139 
9140 		if ((chk->send_size <= (mtu - dmtu)) ||
9141 		    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9142 			/* ok we will add this one */
9143 			if (data_auth_reqd) {
9144 				if (auth == NULL) {
9145 					m = sctp_add_auth_chunk(m,
9146 					    &endofchain,
9147 					    &auth,
9148 					    &auth_offset,
9149 					    stcb,
9150 					    SCTP_DATA);
9151 					auth_keyid = chk->auth_keyid;
9152 					override_ok = 0;
9153 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9154 				} else if (override_ok) {
9155 					auth_keyid = chk->auth_keyid;
9156 					override_ok = 0;
9157 				} else if (chk->auth_keyid != auth_keyid) {
9158 					/* different keyid, so done bundling */
9159 					break;
9160 				}
9161 			}
9162 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9163 			if (m == NULL) {
9164 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9165 				return (ENOMEM);
9166 			}
9167 			/* Do clear IP_DF ? */
9168 			if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9169 				no_fragmentflg = 0;
9170 			}
9171 			/* upate our MTU size */
9172 			if (mtu > (chk->send_size + dmtu))
9173 				mtu -= (chk->send_size + dmtu);
9174 			else
9175 				mtu = 0;
9176 			data_list[bundle_at++] = chk;
9177 			if (one_chunk && (asoc->total_flight <= 0)) {
9178 				SCTP_STAT_INCR(sctps_windowprobed);
9179 			}
9180 		}
9181 		if (one_chunk == 0) {
9182 			/*
9183 			 * now are there anymore forward from chk to pick
9184 			 * up?
9185 			 */
9186 			fwd = TAILQ_NEXT(chk, sctp_next);
9187 			while (fwd) {
9188 				if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9189 					/* Nope, not for retran */
9190 					fwd = TAILQ_NEXT(fwd, sctp_next);
9191 					continue;
9192 				}
9193 				if (fwd->whoTo != net) {
9194 					/* Nope, not the net in question */
9195 					fwd = TAILQ_NEXT(fwd, sctp_next);
9196 					continue;
9197 				}
9198 				if (data_auth_reqd && (auth == NULL)) {
9199 					dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9200 				} else
9201 					dmtu = 0;
9202 				if (fwd->send_size <= (mtu - dmtu)) {
9203 					if (data_auth_reqd) {
9204 						if (auth == NULL) {
9205 							m = sctp_add_auth_chunk(m,
9206 							    &endofchain,
9207 							    &auth,
9208 							    &auth_offset,
9209 							    stcb,
9210 							    SCTP_DATA);
9211 							auth_keyid = fwd->auth_keyid;
9212 							override_ok = 0;
9213 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9214 						} else if (override_ok) {
9215 							auth_keyid = fwd->auth_keyid;
9216 							override_ok = 0;
9217 						} else if (fwd->auth_keyid != auth_keyid) {
9218 							/*
9219 							 * different keyid,
9220 							 * so done bundling
9221 							 */
9222 							break;
9223 						}
9224 					}
9225 					m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9226 					if (m == NULL) {
9227 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9228 						return (ENOMEM);
9229 					}
9230 					/* Do clear IP_DF ? */
9231 					if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9232 						no_fragmentflg = 0;
9233 					}
9234 					/* upate our MTU size */
9235 					if (mtu > (fwd->send_size + dmtu))
9236 						mtu -= (fwd->send_size + dmtu);
9237 					else
9238 						mtu = 0;
9239 					data_list[bundle_at++] = fwd;
9240 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9241 						break;
9242 					}
9243 					fwd = TAILQ_NEXT(fwd, sctp_next);
9244 				} else {
9245 					/* can't fit so we are done */
9246 					break;
9247 				}
9248 			}
9249 		}
9250 		/* Is there something to send for this destination? */
9251 		if (m) {
9252 			/*
9253 			 * No matter if we fail/or suceed we should start a
9254 			 * timer. A failure is like a lost IP packet :-)
9255 			 */
9256 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9257 				/*
9258 				 * no timer running on this destination
9259 				 * restart it.
9260 				 */
9261 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9262 				tmr_started = 1;
9263 			}
9264 			/* Now lets send it, if there is anything to send :> */
9265 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9266 			    (struct sockaddr *)&net->ro._l_addr, m,
9267 			    auth_offset, auth, auth_keyid,
9268 			    no_fragmentflg, 0, NULL, 0,
9269 			    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9270 			    net->port, so_locked, NULL))) {
9271 				/* error, we could not output */
9272 				SCTP_STAT_INCR(sctps_lowlevelerr);
9273 				return (error);
9274 			}
9275 			m = endofchain = NULL;
9276 			auth = NULL;
9277 			auth_offset = 0;
9278 			/* For HB's */
9279 			/*
9280 			 * We don't want to mark the net->sent time here
9281 			 * since this we use this for HB and retrans cannot
9282 			 * measure RTT
9283 			 */
9284 			/* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9285 
9286 			/* For auto-close */
9287 			cnt_thru++;
9288 			if (*now_filled == 0) {
9289 				(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9290 				*now = asoc->time_last_sent;
9291 				*now_filled = 1;
9292 			} else {
9293 				asoc->time_last_sent = *now;
9294 			}
9295 			*cnt_out += bundle_at;
9296 #ifdef SCTP_AUDITING_ENABLED
9297 			sctp_audit_log(0xC4, bundle_at);
9298 #endif
9299 			if (bundle_at) {
9300 				tsns_sent = data_list[0]->rec.data.TSN_seq;
9301 			}
9302 			for (i = 0; i < bundle_at; i++) {
9303 				SCTP_STAT_INCR(sctps_sendretransdata);
9304 				data_list[i]->sent = SCTP_DATAGRAM_SENT;
9305 				/*
9306 				 * When we have a revoked data, and we
9307 				 * retransmit it, then we clear the revoked
9308 				 * flag since this flag dictates if we
9309 				 * subtracted from the fs
9310 				 */
9311 				if (data_list[i]->rec.data.chunk_was_revoked) {
9312 					/* Deflate the cwnd */
9313 					data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9314 					data_list[i]->rec.data.chunk_was_revoked = 0;
9315 				}
9316 				data_list[i]->snd_count++;
9317 				sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9318 				/* record the time */
9319 				data_list[i]->sent_rcv_time = asoc->time_last_sent;
9320 				if (data_list[i]->book_size_scale) {
9321 					/*
9322 					 * need to double the book size on
9323 					 * this one
9324 					 */
9325 					data_list[i]->book_size_scale = 0;
9326 					/*
9327 					 * Since we double the booksize, we
9328 					 * must also double the output queue
9329 					 * size, since this get shrunk when
9330 					 * we free by this amount.
9331 					 */
9332 					atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9333 					data_list[i]->book_size *= 2;
9334 
9335 
9336 				} else {
9337 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9338 						sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9339 						    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9340 					}
9341 					asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9342 					    (uint32_t) (data_list[i]->send_size +
9343 					    SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9344 				}
9345 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9346 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9347 					    data_list[i]->whoTo->flight_size,
9348 					    data_list[i]->book_size,
9349 					    (uintptr_t) data_list[i]->whoTo,
9350 					    data_list[i]->rec.data.TSN_seq);
9351 				}
9352 				sctp_flight_size_increase(data_list[i]);
9353 				sctp_total_flight_increase(stcb, data_list[i]);
9354 				if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9355 					/* SWS sender side engages */
9356 					asoc->peers_rwnd = 0;
9357 				}
9358 				if ((i == 0) &&
9359 				    (data_list[i]->rec.data.doing_fast_retransmit)) {
9360 					SCTP_STAT_INCR(sctps_sendfastretrans);
9361 					if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9362 					    (tmr_started == 0)) {
9363 						/*-
9364 						 * ok we just fast-retrans'd
9365 						 * the lowest TSN, i.e the
9366 						 * first on the list. In
9367 						 * this case we want to give
9368 						 * some more time to get a
9369 						 * SACK back without a
9370 						 * t3-expiring.
9371 						 */
9372 						sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9373 						    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9374 						sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9375 					}
9376 				}
9377 			}
9378 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9379 				sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9380 			}
9381 #ifdef SCTP_AUDITING_ENABLED
9382 			sctp_auditing(21, inp, stcb, NULL);
9383 #endif
9384 		} else {
9385 			/* None will fit */
9386 			return (1);
9387 		}
9388 		if (asoc->sent_queue_retran_cnt <= 0) {
9389 			/* all done we have no more to retran */
9390 			asoc->sent_queue_retran_cnt = 0;
9391 			break;
9392 		}
9393 		if (one_chunk) {
9394 			/* No more room in rwnd */
9395 			return (1);
9396 		}
9397 		/* stop the for loop here. we sent out a packet */
9398 		break;
9399 	}
9400 	return (0);
9401 }
9402 
9403 
9404 static int
9405 sctp_timer_validation(struct sctp_inpcb *inp,
9406     struct sctp_tcb *stcb,
9407     struct sctp_association *asoc,
9408     int ret)
9409 {
9410 	struct sctp_nets *net;
9411 
9412 	/* Validate that a timer is running somewhere */
9413 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9414 		if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9415 			/* Here is a timer */
9416 			return (ret);
9417 		}
9418 	}
9419 	SCTP_TCB_LOCK_ASSERT(stcb);
9420 	/* Gak, we did not have a timer somewhere */
9421 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9422 	sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9423 	return (ret);
9424 }
9425 
9426 void
9427 sctp_chunk_output(struct sctp_inpcb *inp,
9428     struct sctp_tcb *stcb,
9429     int from_where,
9430     int so_locked
9431 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9432     SCTP_UNUSED
9433 #endif
9434 )
9435 {
9436 	/*-
9437 	 * Ok this is the generic chunk service queue. we must do the
9438 	 * following:
9439 	 * - See if there are retransmits pending, if so we must
9440 	 *   do these first.
9441 	 * - Service the stream queue that is next, moving any
9442 	 *   message (note I must get a complete message i.e.
9443 	 *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9444 	 *   TSN's
9445 	 * - Check to see if the cwnd/rwnd allows any output, if so we
9446 	 *   go ahead and fomulate and send the low level chunks. Making sure
9447 	 *   to combine any control in the control chunk queue also.
9448 	 */
9449 	struct sctp_association *asoc;
9450 	struct sctp_nets *net;
9451 	int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
9452 	    burst_cnt = 0, burst_limit = 0;
9453 	struct timeval now;
9454 	int now_filled = 0;
9455 	int nagle_on = 0;
9456 	int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9457 	int un_sent = 0;
9458 	int fr_done, tot_frs = 0;
9459 
9460 	asoc = &stcb->asoc;
9461 	if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9462 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9463 			nagle_on = 0;
9464 		} else {
9465 			nagle_on = 1;
9466 		}
9467 	}
9468 	SCTP_TCB_LOCK_ASSERT(stcb);
9469 
9470 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9471 
9472 	if ((un_sent <= 0) &&
9473 	    (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9474 	    (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
9475 	    (asoc->sent_queue_retran_cnt == 0)) {
9476 		/* Nothing to do unless there is something to be sent left */
9477 		return;
9478 	}
9479 	/*
9480 	 * Do we have something to send, data or control AND a sack timer
9481 	 * running, if so piggy-back the sack.
9482 	 */
9483 	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9484 		sctp_send_sack(stcb);
9485 		(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9486 	}
9487 	while (asoc->sent_queue_retran_cnt) {
9488 		/*-
9489 		 * Ok, it is retransmission time only, we send out only ONE
9490 		 * packet with a single call off to the retran code.
9491 		 */
9492 		if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9493 			/*-
9494 			 * Special hook for handling cookiess discarded
9495 			 * by peer that carried data. Send cookie-ack only
9496 			 * and then the next call with get the retran's.
9497 			 */
9498 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9499 			    from_where,
9500 			    &now, &now_filled, frag_point, so_locked);
9501 			return;
9502 		} else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9503 			/* if its not from a HB then do it */
9504 			fr_done = 0;
9505 			ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9506 			if (fr_done) {
9507 				tot_frs++;
9508 			}
9509 		} else {
9510 			/*
9511 			 * its from any other place, we don't allow retran
9512 			 * output (only control)
9513 			 */
9514 			ret = 1;
9515 		}
9516 		if (ret > 0) {
9517 			/* Can't send anymore */
9518 			/*-
9519 			 * now lets push out control by calling med-level
9520 			 * output once. this assures that we WILL send HB's
9521 			 * if queued too.
9522 			 */
9523 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9524 			    from_where,
9525 			    &now, &now_filled, frag_point, so_locked);
9526 #ifdef SCTP_AUDITING_ENABLED
9527 			sctp_auditing(8, inp, stcb, NULL);
9528 #endif
9529 			(void)sctp_timer_validation(inp, stcb, asoc, ret);
9530 			return;
9531 		}
9532 		if (ret < 0) {
9533 			/*-
9534 			 * The count was off.. retran is not happening so do
9535 			 * the normal retransmission.
9536 			 */
9537 #ifdef SCTP_AUDITING_ENABLED
9538 			sctp_auditing(9, inp, stcb, NULL);
9539 #endif
9540 			if (ret == SCTP_RETRAN_EXIT) {
9541 				return;
9542 			}
9543 			break;
9544 		}
9545 		if (from_where == SCTP_OUTPUT_FROM_T3) {
9546 			/* Only one transmission allowed out of a timeout */
9547 #ifdef SCTP_AUDITING_ENABLED
9548 			sctp_auditing(10, inp, stcb, NULL);
9549 #endif
9550 			/* Push out any control */
9551 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
9552 			    &now, &now_filled, frag_point, so_locked);
9553 			return;
9554 		}
9555 		if (tot_frs > asoc->max_burst) {
9556 			/* Hit FR burst limit */
9557 			return;
9558 		}
9559 		if ((num_out == 0) && (ret == 0)) {
9560 
9561 			/* No more retrans to send */
9562 			break;
9563 		}
9564 	}
9565 #ifdef SCTP_AUDITING_ENABLED
9566 	sctp_auditing(12, inp, stcb, NULL);
9567 #endif
9568 	/* Check for bad destinations, if they exist move chunks around. */
9569 	burst_limit = asoc->max_burst;
9570 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9571 		if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
9572 		    SCTP_ADDR_NOT_REACHABLE) {
9573 			/*-
9574 			 * if possible move things off of this address we
9575 			 * still may send below due to the dormant state but
9576 			 * we try to find an alternate address to send to
9577 			 * and if we have one we move all queued data on the
9578 			 * out wheel to this alternate address.
9579 			 */
9580 			if (net->ref_count > 1)
9581 				sctp_move_to_an_alt(stcb, asoc, net);
9582 		} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
9583 			    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
9584 		    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
9585 			/*
9586 			 * JRS 5/14/07 - If CMT PF is on and the current
9587 			 * destination is in PF state, move all queued data
9588 			 * to an alternate desination.
9589 			 */
9590 			if (net->ref_count > 1)
9591 				sctp_move_to_an_alt(stcb, asoc, net);
9592 		} else {
9593 			/*-
9594 			 * if ((asoc->sat_network) || (net->addr_is_local))
9595 			 * { burst_limit = asoc->max_burst *
9596 			 * SCTP_SAT_NETWORK_BURST_INCR; }
9597 			 */
9598 			if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
9599 				if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
9600 					/*
9601 					 * JRS - Use the congestion control
9602 					 * given in the congestion control
9603 					 * module
9604 					 */
9605 					asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
9606 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9607 						sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
9608 					}
9609 					SCTP_STAT_INCR(sctps_maxburstqueued);
9610 				}
9611 				net->fast_retran_ip = 0;
9612 			} else {
9613 				if (net->flight_size == 0) {
9614 					/* Should be decaying the cwnd here */
9615 					;
9616 				}
9617 			}
9618 		}
9619 
9620 	}
9621 	burst_cnt = 0;
9622 	do {
9623 		error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
9624 		    &reason_code, 0, from_where,
9625 		    &now, &now_filled, frag_point, so_locked);
9626 		if (error) {
9627 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
9628 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9629 				sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
9630 			}
9631 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9632 				sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
9633 				sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
9634 			}
9635 			break;
9636 		}
9637 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
9638 
9639 		tot_out += num_out;
9640 		burst_cnt++;
9641 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9642 			sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
9643 			if (num_out == 0) {
9644 				sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
9645 			}
9646 		}
9647 		if (nagle_on) {
9648 			/*-
9649 			 * When nagle is on, we look at how much is un_sent, then
9650 			 * if its smaller than an MTU and we have data in
9651 			 * flight we stop.
9652 			 */
9653 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9654 			    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
9655 			if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
9656 			    (stcb->asoc.total_flight > 0)) {
9657 				break;
9658 			}
9659 		}
9660 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9661 		    TAILQ_EMPTY(&asoc->send_queue) &&
9662 		    TAILQ_EMPTY(&asoc->out_wheel)) {
9663 			/* Nothing left to send */
9664 			break;
9665 		}
9666 		if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
9667 			/* Nothing left to send */
9668 			break;
9669 		}
9670 	} while (num_out && (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
9671 	    (burst_cnt < burst_limit)));
9672 
9673 	if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
9674 		if (burst_cnt >= burst_limit) {
9675 			SCTP_STAT_INCR(sctps_maxburstqueued);
9676 			asoc->burst_limit_applied = 1;
9677 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9678 				sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
9679 			}
9680 		} else {
9681 			asoc->burst_limit_applied = 0;
9682 		}
9683 	}
9684 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9685 		sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
9686 	}
9687 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
9688 	    tot_out);
9689 
9690 	/*-
9691 	 * Now we need to clean up the control chunk chain if a ECNE is on
9692 	 * it. It must be marked as UNSENT again so next call will continue
9693 	 * to send it until such time that we get a CWR, to remove it.
9694 	 */
9695 	if (stcb->asoc.ecn_echo_cnt_onq)
9696 		sctp_fix_ecn_echo(asoc);
9697 	return;
9698 }
9699 
9700 
9701 int
9702 sctp_output(inp, m, addr, control, p, flags)
9703 	struct sctp_inpcb *inp;
9704 	struct mbuf *m;
9705 	struct sockaddr *addr;
9706 	struct mbuf *control;
9707 	struct thread *p;
9708 	int flags;
9709 {
9710 	if (inp == NULL) {
9711 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9712 		return (EINVAL);
9713 	}
9714 	if (inp->sctp_socket == NULL) {
9715 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9716 		return (EINVAL);
9717 	}
9718 	return (sctp_sosend(inp->sctp_socket,
9719 	    addr,
9720 	    (struct uio *)NULL,
9721 	    m,
9722 	    control,
9723 	    flags, p
9724 	    ));
9725 }
9726 
9727 void
9728 send_forward_tsn(struct sctp_tcb *stcb,
9729     struct sctp_association *asoc)
9730 {
9731 	struct sctp_tmit_chunk *chk;
9732 	struct sctp_forward_tsn_chunk *fwdtsn;
9733 	uint32_t advance_peer_ack_point;
9734 
9735 	SCTP_TCB_LOCK_ASSERT(stcb);
9736 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9737 		if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9738 			/* mark it to unsent */
9739 			chk->sent = SCTP_DATAGRAM_UNSENT;
9740 			chk->snd_count = 0;
9741 			/* Do we correct its output location? */
9742 			if (chk->whoTo != asoc->primary_destination) {
9743 				sctp_free_remote_addr(chk->whoTo);
9744 				chk->whoTo = asoc->primary_destination;
9745 				atomic_add_int(&chk->whoTo->ref_count, 1);
9746 			}
9747 			goto sctp_fill_in_rest;
9748 		}
9749 	}
9750 	/* Ok if we reach here we must build one */
9751 	sctp_alloc_a_chunk(stcb, chk);
9752 	if (chk == NULL) {
9753 		return;
9754 	}
9755 	asoc->fwd_tsn_cnt++;
9756 	chk->copy_by_ref = 0;
9757 	chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
9758 	chk->rec.chunk_id.can_take_data = 0;
9759 	chk->asoc = asoc;
9760 	chk->whoTo = NULL;
9761 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9762 	if (chk->data == NULL) {
9763 		sctp_free_a_chunk(stcb, chk);
9764 		return;
9765 	}
9766 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9767 	chk->sent = SCTP_DATAGRAM_UNSENT;
9768 	chk->snd_count = 0;
9769 	chk->whoTo = asoc->primary_destination;
9770 	atomic_add_int(&chk->whoTo->ref_count, 1);
9771 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
9772 	asoc->ctrl_queue_cnt++;
9773 sctp_fill_in_rest:
9774 	/*-
9775 	 * Here we go through and fill out the part that deals with
9776 	 * stream/seq of the ones we skip.
9777 	 */
9778 	SCTP_BUF_LEN(chk->data) = 0;
9779 	{
9780 		struct sctp_tmit_chunk *at, *tp1, *last;
9781 		struct sctp_strseq *strseq;
9782 		unsigned int cnt_of_space, i, ovh;
9783 		unsigned int space_needed;
9784 		unsigned int cnt_of_skipped = 0;
9785 
9786 		TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
9787 			if (at->sent != SCTP_FORWARD_TSN_SKIP) {
9788 				/* no more to look at */
9789 				break;
9790 			}
9791 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9792 				/* We don't report these */
9793 				continue;
9794 			}
9795 			cnt_of_skipped++;
9796 		}
9797 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
9798 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
9799 
9800 		cnt_of_space = M_TRAILINGSPACE(chk->data);
9801 
9802 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9803 			ovh = SCTP_MIN_OVERHEAD;
9804 		} else {
9805 			ovh = SCTP_MIN_V4_OVERHEAD;
9806 		}
9807 		if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
9808 			/* trim to a mtu size */
9809 			cnt_of_space = asoc->smallest_mtu - ovh;
9810 		}
9811 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9812 			sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9813 			    0xff, 0, cnt_of_skipped,
9814 			    asoc->advanced_peer_ack_point);
9815 
9816 		}
9817 		advance_peer_ack_point = asoc->advanced_peer_ack_point;
9818 		if (cnt_of_space < space_needed) {
9819 			/*-
9820 			 * ok we must trim down the chunk by lowering the
9821 			 * advance peer ack point.
9822 			 */
9823 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9824 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9825 				    0xff, 0xff, cnt_of_space,
9826 				    space_needed);
9827 			}
9828 			cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
9829 			cnt_of_skipped /= sizeof(struct sctp_strseq);
9830 			/*-
9831 			 * Go through and find the TSN that will be the one
9832 			 * we report.
9833 			 */
9834 			at = TAILQ_FIRST(&asoc->sent_queue);
9835 			for (i = 0; i < cnt_of_skipped; i++) {
9836 				tp1 = TAILQ_NEXT(at, sctp_next);
9837 				at = tp1;
9838 			}
9839 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9840 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9841 				    0xff, cnt_of_skipped, at->rec.data.TSN_seq,
9842 				    asoc->advanced_peer_ack_point);
9843 			}
9844 			last = at;
9845 			/*-
9846 			 * last now points to last one I can report, update
9847 			 * peer ack point
9848 			 */
9849 			advance_peer_ack_point = last->rec.data.TSN_seq;
9850 			space_needed = sizeof(struct sctp_forward_tsn_chunk) +
9851 			    cnt_of_skipped * sizeof(struct sctp_strseq);
9852 		}
9853 		chk->send_size = space_needed;
9854 		/* Setup the chunk */
9855 		fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
9856 		fwdtsn->ch.chunk_length = htons(chk->send_size);
9857 		fwdtsn->ch.chunk_flags = 0;
9858 		fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
9859 		fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
9860 		SCTP_BUF_LEN(chk->data) = chk->send_size;
9861 		fwdtsn++;
9862 		/*-
9863 		 * Move pointer to after the fwdtsn and transfer to the
9864 		 * strseq pointer.
9865 		 */
9866 		strseq = (struct sctp_strseq *)fwdtsn;
9867 		/*-
9868 		 * Now populate the strseq list. This is done blindly
9869 		 * without pulling out duplicate stream info. This is
9870 		 * inefficent but won't harm the process since the peer will
9871 		 * look at these in sequence and will thus release anything.
9872 		 * It could mean we exceed the PMTU and chop off some that
9873 		 * we could have included.. but this is unlikely (aka 1432/4
9874 		 * would mean 300+ stream seq's would have to be reported in
9875 		 * one FWD-TSN. With a bit of work we can later FIX this to
9876 		 * optimize and pull out duplcates.. but it does add more
9877 		 * overhead. So for now... not!
9878 		 */
9879 		at = TAILQ_FIRST(&asoc->sent_queue);
9880 		for (i = 0; i < cnt_of_skipped; i++) {
9881 			tp1 = TAILQ_NEXT(at, sctp_next);
9882 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9883 				/* We don't report these */
9884 				i--;
9885 				at = tp1;
9886 				continue;
9887 			}
9888 			if (at->rec.data.TSN_seq == advance_peer_ack_point) {
9889 				at->rec.data.fwd_tsn_cnt = 0;
9890 			}
9891 			strseq->stream = ntohs(at->rec.data.stream_number);
9892 			strseq->sequence = ntohs(at->rec.data.stream_seq);
9893 			strseq++;
9894 			at = tp1;
9895 		}
9896 	}
9897 	return;
9898 
9899 }
9900 
9901 void
9902 sctp_send_sack(struct sctp_tcb *stcb)
9903 {
9904 	/*-
9905 	 * Queue up a SACK or NR-SACK in the control queue.
9906 	 * We must first check to see if a SACK or NR-SACK is
9907 	 * somehow on the control queue.
9908 	 * If so, we will take and and remove the old one.
9909 	 */
9910 	struct sctp_association *asoc;
9911 	struct sctp_tmit_chunk *chk, *a_chk;
9912 	struct sctp_sack_chunk *sack;
9913 	struct sctp_nr_sack_chunk *nr_sack;
9914 	struct sctp_gap_ack_block *gap_descriptor;
9915 	struct sack_track *selector;
9916 	int mergeable = 0;
9917 	int offset;
9918 	caddr_t limit;
9919 	uint32_t *dup;
9920 	int limit_reached = 0;
9921 	unsigned int i, sel_start, siz, j, starting_index;
9922 	unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
9923 	int num_dups = 0;
9924 	int space_req;
9925 	uint32_t highest_tsn;
9926 	uint8_t flags;
9927 	uint8_t type;
9928 
9929 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) &&
9930 	    stcb->asoc.peer_supports_nr_sack) {
9931 		type = SCTP_NR_SELECTIVE_ACK;
9932 	} else {
9933 		type = SCTP_SELECTIVE_ACK;
9934 	}
9935 	a_chk = NULL;
9936 	asoc = &stcb->asoc;
9937 	SCTP_TCB_LOCK_ASSERT(stcb);
9938 	if (asoc->last_data_chunk_from == NULL) {
9939 		/* Hmm we never received anything */
9940 		return;
9941 	}
9942 	sctp_slide_mapping_arrays(stcb);
9943 	sctp_set_rwnd(stcb, asoc);
9944 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9945 		if (chk->rec.chunk_id.id == type) {
9946 			/* Hmm, found a sack already on queue, remove it */
9947 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
9948 			asoc->ctrl_queue_cnt++;
9949 			a_chk = chk;
9950 			if (a_chk->data) {
9951 				sctp_m_freem(a_chk->data);
9952 				a_chk->data = NULL;
9953 			}
9954 			sctp_free_remote_addr(a_chk->whoTo);
9955 			a_chk->whoTo = NULL;
9956 			break;
9957 		}
9958 	}
9959 	if (a_chk == NULL) {
9960 		sctp_alloc_a_chunk(stcb, a_chk);
9961 		if (a_chk == NULL) {
9962 			/* No memory so we drop the idea, and set a timer */
9963 			if (stcb->asoc.delayed_ack) {
9964 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9965 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
9966 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9967 				    stcb->sctp_ep, stcb, NULL);
9968 			} else {
9969 				stcb->asoc.send_sack = 1;
9970 			}
9971 			return;
9972 		}
9973 		a_chk->copy_by_ref = 0;
9974 		a_chk->rec.chunk_id.id = type;
9975 		a_chk->rec.chunk_id.can_take_data = 1;
9976 	}
9977 	/* Clear our pkt counts */
9978 	asoc->data_pkts_seen = 0;
9979 
9980 	a_chk->asoc = asoc;
9981 	a_chk->snd_count = 0;
9982 	a_chk->send_size = 0;	/* fill in later */
9983 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
9984 	a_chk->whoTo = NULL;
9985 
9986 	if ((asoc->numduptsns) ||
9987 	    (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
9988 	    ) {
9989 		/*-
9990 		 * Ok, we have some duplicates or the destination for the
9991 		 * sack is unreachable, lets see if we can select an
9992 		 * alternate than asoc->last_data_chunk_from
9993 		 */
9994 		if ((!(asoc->last_data_chunk_from->dest_state &
9995 		    SCTP_ADDR_NOT_REACHABLE)) &&
9996 		    (asoc->used_alt_onsack > asoc->numnets)) {
9997 			/* We used an alt last time, don't this time */
9998 			a_chk->whoTo = NULL;
9999 		} else {
10000 			asoc->used_alt_onsack++;
10001 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
10002 		}
10003 		if (a_chk->whoTo == NULL) {
10004 			/* Nope, no alternate */
10005 			a_chk->whoTo = asoc->last_data_chunk_from;
10006 			asoc->used_alt_onsack = 0;
10007 		}
10008 	} else {
10009 		/*
10010 		 * No duplicates so we use the last place we received data
10011 		 * from.
10012 		 */
10013 		asoc->used_alt_onsack = 0;
10014 		a_chk->whoTo = asoc->last_data_chunk_from;
10015 	}
10016 	if (a_chk->whoTo) {
10017 		atomic_add_int(&a_chk->whoTo->ref_count, 1);
10018 	}
10019 	if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
10020 		highest_tsn = asoc->highest_tsn_inside_map;
10021 	} else {
10022 		highest_tsn = asoc->highest_tsn_inside_nr_map;
10023 	}
10024 	if (highest_tsn == asoc->cumulative_tsn) {
10025 		/* no gaps */
10026 		if (type == SCTP_SELECTIVE_ACK) {
10027 			space_req = sizeof(struct sctp_sack_chunk);
10028 		} else {
10029 			space_req = sizeof(struct sctp_nr_sack_chunk);
10030 		}
10031 	} else {
10032 		/* gaps get a cluster */
10033 		space_req = MCLBYTES;
10034 	}
10035 	/* Ok now lets formulate a MBUF with our sack */
10036 	a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
10037 	if ((a_chk->data == NULL) ||
10038 	    (a_chk->whoTo == NULL)) {
10039 		/* rats, no mbuf memory */
10040 		if (a_chk->data) {
10041 			/* was a problem with the destination */
10042 			sctp_m_freem(a_chk->data);
10043 			a_chk->data = NULL;
10044 		}
10045 		sctp_free_a_chunk(stcb, a_chk);
10046 		/* sa_ignore NO_NULL_CHK */
10047 		if (stcb->asoc.delayed_ack) {
10048 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10049 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
10050 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10051 			    stcb->sctp_ep, stcb, NULL);
10052 		} else {
10053 			stcb->asoc.send_sack = 1;
10054 		}
10055 		return;
10056 	}
10057 	/* ok, lets go through and fill it in */
10058 	SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10059 	space = M_TRAILINGSPACE(a_chk->data);
10060 	if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10061 		space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10062 	}
10063 	limit = mtod(a_chk->data, caddr_t);
10064 	limit += space;
10065 
10066 	/* 0x01 is used by nonce for ecn */
10067 	if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
10068 	    (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
10069 	    (asoc->peer_supports_ecn_nonce))
10070 		flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
10071 	else
10072 		flags = 0;
10073 
10074 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10075 		/*-
10076 		 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10077 		 * received, then set high bit to 1, else 0. Reset
10078 		 * pkts_rcvd.
10079 		 */
10080 		flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10081 		asoc->cmt_dac_pkts_rcvd = 0;
10082 	}
10083 #ifdef SCTP_ASOCLOG_OF_TSNS
10084 	stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10085 	stcb->asoc.cumack_log_atsnt++;
10086 	if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10087 		stcb->asoc.cumack_log_atsnt = 0;
10088 	}
10089 #endif
10090 	/* reset the readers interpretation */
10091 	stcb->freed_by_sorcv_sincelast = 0;
10092 
10093 	if (type == SCTP_SELECTIVE_ACK) {
10094 		sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10095 		nr_sack = NULL;
10096 		gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10097 		if (highest_tsn > asoc->mapping_array_base_tsn) {
10098 			siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10099 		} else {
10100 			siz = (((MAX_TSN - highest_tsn) + 1) + highest_tsn + 7) / 8;
10101 		}
10102 	} else {
10103 		sack = NULL;
10104 		nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10105 		gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10106 		if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
10107 			siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10108 		} else {
10109 			siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10110 		}
10111 	}
10112 
10113 	if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10114 		offset = 1;
10115 		/*-
10116 		 * The base TSN is intialized to be the first TSN the peer
10117 		 * will send us. If the cum-ack is behind this then when they
10118 		 * send us the next in sequence it will mark the base_tsn bit.
10119 		 * Thus we need to use the very first selector and the offset
10120 		 * is 1. Our table is built for this case.
10121 		 */
10122 		starting_index = 0;
10123 		sel_start = 0;
10124 	} else {
10125 		/*-
10126 		 * we skip the first selector  when the cum-ack is at or above the
10127 		 * mapping array base. This is because the bits at the base or above
10128 		 * are turned on and our first selector in the table assumes they are
10129 		 * off. We thus will use the second selector (first is 0). We use
10130 		 * the reverse of our macro to fix the offset, in bits, that our
10131 		 * table is at. Note that this method assumes that the cum-tsn is
10132 		 * within the first bit, i.e. its value is 0-7 which means the
10133 		 * result to our offset will be either a 0 - -7. If the cumack
10134 		 * is NOT in the first byte (0) (which it should be since we did
10135 		 * a mapping array slide above) then we need to calculate the starting
10136 		 * index i.e. which byte of the mapping array we should start at. We
10137 		 * do this by dividing by 8 and pushing the remainder (mod) into offset.
10138 		 * then we multiply the offset to be negative, since we need a negative
10139 		 * offset into the selector table.
10140 		 */
10141 		SCTP_CALC_TSN_TO_GAP(offset, asoc->cumulative_tsn, asoc->mapping_array_base_tsn);
10142 		if (offset > 7) {
10143 			starting_index = offset / 8;
10144 			offset = offset % 8;
10145 			printf("Strange starting index is %d offset:%d (not 0/x)\n",
10146 			    starting_index, offset);
10147 		} else {
10148 			starting_index = 0;
10149 		}
10150 		/* We need a negative offset in our table */
10151 		offset *= -1;
10152 		sel_start = 1;
10153 	}
10154 	if (((type == SCTP_SELECTIVE_ACK) &&
10155 	    compare_with_wrap(highest_tsn, asoc->cumulative_tsn, MAX_TSN)) ||
10156 	    ((type == SCTP_NR_SELECTIVE_ACK) &&
10157 	    compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN))) {
10158 		/* we have a gap .. maybe */
10159 		for (i = starting_index; i < siz; i++) {
10160 			if (type == SCTP_SELECTIVE_ACK) {
10161 				selector = &sack_array[asoc->mapping_array[i] | asoc->nr_mapping_array[i]];
10162 			} else {
10163 				selector = &sack_array[asoc->mapping_array[i]];
10164 			}
10165 			if (mergeable && selector->right_edge) {
10166 				/*
10167 				 * Backup, left and right edges were ok to
10168 				 * merge.
10169 				 */
10170 				num_gap_blocks--;
10171 				gap_descriptor--;
10172 			}
10173 			if (selector->num_entries == 0)
10174 				mergeable = 0;
10175 			else {
10176 				for (j = sel_start; j < selector->num_entries; j++) {
10177 					if (mergeable && selector->right_edge) {
10178 						/*
10179 						 * do a merge by NOT setting
10180 						 * the left side
10181 						 */
10182 						mergeable = 0;
10183 					} else {
10184 						/*
10185 						 * no merge, set the left
10186 						 * side
10187 						 */
10188 						mergeable = 0;
10189 						gap_descriptor->start = htons((selector->gaps[j].start + offset));
10190 					}
10191 					gap_descriptor->end = htons((selector->gaps[j].end + offset));
10192 					num_gap_blocks++;
10193 					gap_descriptor++;
10194 					if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10195 						/* no more room */
10196 						limit_reached = 1;
10197 						break;
10198 					}
10199 				}
10200 				if (selector->left_edge) {
10201 					mergeable = 1;
10202 				}
10203 			}
10204 			if (limit_reached) {
10205 				/* Reached the limit stop */
10206 				break;
10207 			}
10208 			sel_start = 0;
10209 			offset += 8;
10210 		}
10211 	}
10212 	if ((type == SCTP_NR_SELECTIVE_ACK) &&
10213 	    (limit_reached == 0)) {
10214 
10215 		mergeable = 0;
10216 
10217 		if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn)
10218 			siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10219 		else
10220 			siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10221 
10222 		if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10223 			offset = 1;
10224 			/*-
10225 			* cum-ack behind the mapping array, so we start and use all
10226 			* entries.
10227 			*/
10228 			sel_start = 0;
10229 		} else {
10230 			offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10231 			/*-
10232 			* we skip the first one when the cum-ack is at or above the
10233 			* mapping array base. Note this only works if
10234 			*/
10235 			sel_start = 1;
10236 		}
10237 		if (compare_with_wrap(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn, MAX_TSN)) {
10238 			/* we have a gap .. maybe */
10239 			for (i = 0; i < siz; i++) {
10240 				selector = &sack_array[asoc->nr_mapping_array[i]];
10241 				if (mergeable && selector->right_edge) {
10242 					/*
10243 					 * Backup, left and right edges were
10244 					 * ok to merge.
10245 					 */
10246 					num_nr_gap_blocks--;
10247 					gap_descriptor--;
10248 				}
10249 				if (selector->num_entries == 0)
10250 					mergeable = 0;
10251 				else {
10252 					for (j = sel_start; j < selector->num_entries; j++) {
10253 						if (mergeable && selector->right_edge) {
10254 							/*
10255 							 * do a merge by NOT
10256 							 * setting the left
10257 							 * side
10258 							 */
10259 							mergeable = 0;
10260 						} else {
10261 							/*
10262 							 * no merge, set the
10263 							 * left side
10264 							 */
10265 							mergeable = 0;
10266 							gap_descriptor->start = htons((selector->gaps[j].start + offset));
10267 						}
10268 						gap_descriptor->end = htons((selector->gaps[j].end + offset));
10269 						num_nr_gap_blocks++;
10270 						gap_descriptor++;
10271 						if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10272 							/* no more room */
10273 							limit_reached = 1;
10274 							break;
10275 						}
10276 					}
10277 					if (selector->left_edge) {
10278 						mergeable = 1;
10279 					}
10280 				}
10281 				if (limit_reached) {
10282 					/* Reached the limit stop */
10283 					break;
10284 				}
10285 				sel_start = 0;
10286 				offset += 8;
10287 			}
10288 		}
10289 	}
10290 	/* now we must add any dups we are going to report. */
10291 	if ((limit_reached == 0) && (asoc->numduptsns)) {
10292 		dup = (uint32_t *) gap_descriptor;
10293 		for (i = 0; i < asoc->numduptsns; i++) {
10294 			*dup = htonl(asoc->dup_tsns[i]);
10295 			dup++;
10296 			num_dups++;
10297 			if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10298 				/* no more room */
10299 				break;
10300 			}
10301 		}
10302 		asoc->numduptsns = 0;
10303 	}
10304 	/*
10305 	 * now that the chunk is prepared queue it to the control chunk
10306 	 * queue.
10307 	 */
10308 	if (type == SCTP_SELECTIVE_ACK) {
10309 		a_chk->send_size = sizeof(struct sctp_sack_chunk) +
10310 		    (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10311 		    num_dups * sizeof(int32_t);
10312 		SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10313 		sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10314 		sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10315 		sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10316 		sack->sack.num_dup_tsns = htons(num_dups);
10317 		sack->ch.chunk_type = type;
10318 		sack->ch.chunk_flags = flags;
10319 		sack->ch.chunk_length = htons(a_chk->send_size);
10320 	} else {
10321 		a_chk->send_size = sizeof(struct sctp_nr_sack_chunk) +
10322 		    (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10323 		    num_dups * sizeof(int32_t);
10324 		SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10325 		nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10326 		nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10327 		nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10328 		nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10329 		nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10330 		nr_sack->nr_sack.reserved = 0;
10331 		nr_sack->ch.chunk_type = type;
10332 		nr_sack->ch.chunk_flags = flags;
10333 		nr_sack->ch.chunk_length = htons(a_chk->send_size);
10334 	}
10335 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10336 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
10337 	asoc->ctrl_queue_cnt++;
10338 	asoc->send_sack = 0;
10339 	SCTP_STAT_INCR(sctps_sendsacks);
10340 	return;
10341 }
10342 
10343 void
10344 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10345 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10346     SCTP_UNUSED
10347 #endif
10348 )
10349 {
10350 	struct mbuf *m_abort;
10351 	struct mbuf *m_out = NULL, *m_end = NULL;
10352 	struct sctp_abort_chunk *abort = NULL;
10353 	int sz;
10354 	uint32_t auth_offset = 0;
10355 	struct sctp_auth_chunk *auth = NULL;
10356 
10357 	/*-
10358 	 * Add an AUTH chunk, if chunk requires it and save the offset into
10359 	 * the chain for AUTH
10360 	 */
10361 	if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10362 	    stcb->asoc.peer_auth_chunks)) {
10363 		m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10364 		    stcb, SCTP_ABORT_ASSOCIATION);
10365 		SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10366 	}
10367 	SCTP_TCB_LOCK_ASSERT(stcb);
10368 	m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10369 	if (m_abort == NULL) {
10370 		/* no mbuf's */
10371 		if (m_out)
10372 			sctp_m_freem(m_out);
10373 		return;
10374 	}
10375 	/* link in any error */
10376 	SCTP_BUF_NEXT(m_abort) = operr;
10377 	sz = 0;
10378 	if (operr) {
10379 		struct mbuf *n;
10380 
10381 		n = operr;
10382 		while (n) {
10383 			sz += SCTP_BUF_LEN(n);
10384 			n = SCTP_BUF_NEXT(n);
10385 		}
10386 	}
10387 	SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10388 	if (m_out == NULL) {
10389 		/* NO Auth chunk prepended, so reserve space in front */
10390 		SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10391 		m_out = m_abort;
10392 	} else {
10393 		/* Put AUTH chunk at the front of the chain */
10394 		SCTP_BUF_NEXT(m_end) = m_abort;
10395 	}
10396 
10397 	/* fill in the ABORT chunk */
10398 	abort = mtod(m_abort, struct sctp_abort_chunk *);
10399 	abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10400 	abort->ch.chunk_flags = 0;
10401 	abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10402 
10403 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
10404 	    stcb->asoc.primary_destination,
10405 	    (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
10406 	    m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, NULL, 0,
10407 	    stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10408 	    stcb->asoc.primary_destination->port, so_locked, NULL);
10409 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10410 }
10411 
10412 void
10413 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10414     struct sctp_nets *net,
10415     int reflect_vtag)
10416 {
10417 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10418 	struct mbuf *m_shutdown_comp;
10419 	struct sctp_shutdown_complete_chunk *shutdown_complete;
10420 	uint32_t vtag;
10421 	uint8_t flags;
10422 
10423 	m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10424 	if (m_shutdown_comp == NULL) {
10425 		/* no mbuf's */
10426 		return;
10427 	}
10428 	if (reflect_vtag) {
10429 		flags = SCTP_HAD_NO_TCB;
10430 		vtag = stcb->asoc.my_vtag;
10431 	} else {
10432 		flags = 0;
10433 		vtag = stcb->asoc.peer_vtag;
10434 	}
10435 	shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10436 	shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10437 	shutdown_complete->ch.chunk_flags = flags;
10438 	shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10439 	SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10440 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10441 	    (struct sockaddr *)&net->ro._l_addr,
10442 	    m_shutdown_comp, 0, NULL, 0, 1, 0, NULL, 0,
10443 	    stcb->sctp_ep->sctp_lport, stcb->rport,
10444 	    htonl(vtag),
10445 	    net->port, SCTP_SO_NOT_LOCKED, NULL);
10446 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10447 	return;
10448 }
10449 
10450 void
10451 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
10452     uint32_t vrf_id, uint16_t port)
10453 {
10454 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10455 	struct mbuf *o_pak;
10456 	struct mbuf *mout;
10457 	struct ip *iph, *iph_out;
10458 	struct udphdr *udp = NULL;
10459 
10460 #ifdef INET6
10461 	struct ip6_hdr *ip6, *ip6_out;
10462 
10463 #endif
10464 	int offset_out, len, mlen;
10465 	struct sctp_shutdown_complete_msg *comp_cp;
10466 
10467 	iph = mtod(m, struct ip *);
10468 	switch (iph->ip_v) {
10469 	case IPVERSION:
10470 		len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10471 		break;
10472 #ifdef INET6
10473 	case IPV6_VERSION >> 4:
10474 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10475 		break;
10476 #endif
10477 	default:
10478 		return;
10479 	}
10480 	if (port) {
10481 		len += sizeof(struct udphdr);
10482 	}
10483 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
10484 	if (mout == NULL) {
10485 		return;
10486 	}
10487 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
10488 	SCTP_BUF_LEN(mout) = len;
10489 	SCTP_BUF_NEXT(mout) = NULL;
10490 	iph_out = NULL;
10491 #ifdef INET6
10492 	ip6_out = NULL;
10493 #endif
10494 	offset_out = 0;
10495 
10496 	switch (iph->ip_v) {
10497 	case IPVERSION:
10498 		iph_out = mtod(mout, struct ip *);
10499 
10500 		/* Fill in the IP header for the ABORT */
10501 		iph_out->ip_v = IPVERSION;
10502 		iph_out->ip_hl = (sizeof(struct ip) / 4);
10503 		iph_out->ip_tos = (u_char)0;
10504 		iph_out->ip_id = 0;
10505 		iph_out->ip_off = 0;
10506 		iph_out->ip_ttl = MAXTTL;
10507 		if (port) {
10508 			iph_out->ip_p = IPPROTO_UDP;
10509 		} else {
10510 			iph_out->ip_p = IPPROTO_SCTP;
10511 		}
10512 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10513 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10514 
10515 		/* let IP layer calculate this */
10516 		iph_out->ip_sum = 0;
10517 		offset_out += sizeof(*iph_out);
10518 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10519 		    (caddr_t)iph_out + offset_out);
10520 		break;
10521 #ifdef INET6
10522 	case IPV6_VERSION >> 4:
10523 		ip6 = (struct ip6_hdr *)iph;
10524 		ip6_out = mtod(mout, struct ip6_hdr *);
10525 
10526 		/* Fill in the IPv6 header for the ABORT */
10527 		ip6_out->ip6_flow = ip6->ip6_flow;
10528 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
10529 		if (port) {
10530 			ip6_out->ip6_nxt = IPPROTO_UDP;
10531 		} else {
10532 			ip6_out->ip6_nxt = IPPROTO_SCTP;
10533 		}
10534 		ip6_out->ip6_src = ip6->ip6_dst;
10535 		ip6_out->ip6_dst = ip6->ip6_src;
10536 		/*
10537 		 * ?? The old code had both the iph len + payload, I think
10538 		 * this is wrong and would never have worked
10539 		 */
10540 		ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10541 		offset_out += sizeof(*ip6_out);
10542 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10543 		    (caddr_t)ip6_out + offset_out);
10544 		break;
10545 #endif				/* INET6 */
10546 	default:
10547 		/* Currently not supported. */
10548 		sctp_m_freem(mout);
10549 		return;
10550 	}
10551 	if (port) {
10552 		udp = (struct udphdr *)comp_cp;
10553 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10554 		udp->uh_dport = port;
10555 		udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
10556 		udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
10557 		offset_out += sizeof(struct udphdr);
10558 		comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
10559 	}
10560 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10561 		/* no mbuf's */
10562 		sctp_m_freem(mout);
10563 		return;
10564 	}
10565 	/* Now copy in and fill in the ABORT tags etc. */
10566 	comp_cp->sh.src_port = sh->dest_port;
10567 	comp_cp->sh.dest_port = sh->src_port;
10568 	comp_cp->sh.checksum = 0;
10569 	comp_cp->sh.v_tag = sh->v_tag;
10570 	comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
10571 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10572 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10573 
10574 	if (iph_out != NULL) {
10575 		sctp_route_t ro;
10576 		int ret;
10577 		struct sctp_tcb *stcb = NULL;
10578 
10579 		mlen = SCTP_BUF_LEN(mout);
10580 		bzero(&ro, sizeof ro);
10581 		/* set IPv4 length */
10582 		iph_out->ip_len = mlen;
10583 #ifdef  SCTP_PACKET_LOGGING
10584 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10585 			sctp_packet_log(mout, mlen);
10586 #endif
10587 		if (port) {
10588 			comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
10589 			SCTP_STAT_INCR(sctps_sendswcrc);
10590 			SCTP_ENABLE_UDP_CSUM(mout);
10591 		} else {
10592 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
10593 			mout->m_pkthdr.csum_data = 0;
10594 			SCTP_STAT_INCR(sctps_sendhwcrc);
10595 		}
10596 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10597 		/* out it goes */
10598 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
10599 
10600 		/* Free the route if we got one back */
10601 		if (ro.ro_rt)
10602 			RTFREE(ro.ro_rt);
10603 	}
10604 #ifdef INET6
10605 	if (ip6_out != NULL) {
10606 		struct route_in6 ro;
10607 		int ret;
10608 		struct sctp_tcb *stcb = NULL;
10609 		struct ifnet *ifp = NULL;
10610 
10611 		bzero(&ro, sizeof(ro));
10612 		mlen = SCTP_BUF_LEN(mout);
10613 #ifdef  SCTP_PACKET_LOGGING
10614 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10615 			sctp_packet_log(mout, mlen);
10616 #endif
10617 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10618 		if (port) {
10619 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
10620 			    (stcb) &&
10621 			    (stcb->asoc.loopback_scope))) {
10622 				comp_cp->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
10623 				SCTP_STAT_INCR(sctps_sendswcrc);
10624 			} else {
10625 				SCTP_STAT_INCR(sctps_sendnocrc);
10626 			}
10627 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), mlen - sizeof(struct ip6_hdr))) == 0) {
10628 				udp->uh_sum = 0xffff;
10629 			}
10630 		} else {
10631 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
10632 			    (stcb) &&
10633 			    (stcb->asoc.loopback_scope))) {
10634 				mout->m_pkthdr.csum_flags = CSUM_SCTP;
10635 				mout->m_pkthdr.csum_data = 0;
10636 				SCTP_STAT_INCR(sctps_sendhwcrc);
10637 			} else {
10638 				SCTP_STAT_INCR(sctps_sendnocrc);
10639 			}
10640 		}
10641 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
10642 
10643 		/* Free the route if we got one back */
10644 		if (ro.ro_rt)
10645 			RTFREE(ro.ro_rt);
10646 	}
10647 #endif
10648 	SCTP_STAT_INCR(sctps_sendpackets);
10649 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10650 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10651 	return;
10652 
10653 }
10654 
10655 static struct sctp_nets *
10656 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
10657 {
10658 	struct sctp_nets *net, *hnet;
10659 	int ms_goneby, highest_ms, state_overide = 0;
10660 
10661 	(void)SCTP_GETTIME_TIMEVAL(now);
10662 	highest_ms = 0;
10663 	hnet = NULL;
10664 	SCTP_TCB_LOCK_ASSERT(stcb);
10665 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
10666 		if (
10667 		    ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
10668 		    (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
10669 		    ) {
10670 			/*
10671 			 * Skip this guy from consideration if HB is off AND
10672 			 * its confirmed
10673 			 */
10674 			continue;
10675 		}
10676 		if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
10677 			/* skip this dest net from consideration */
10678 			continue;
10679 		}
10680 		if (net->last_sent_time.tv_sec) {
10681 			/* Sent to so we subtract */
10682 			ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
10683 		} else
10684 			/* Never been sent to */
10685 			ms_goneby = 0x7fffffff;
10686 		/*-
10687 		 * When the address state is unconfirmed but still
10688 		 * considered reachable, we HB at a higher rate. Once it
10689 		 * goes confirmed OR reaches the "unreachable" state, thenw
10690 		 * we cut it back to HB at a more normal pace.
10691 		 */
10692 		if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
10693 			state_overide = 1;
10694 		} else {
10695 			state_overide = 0;
10696 		}
10697 
10698 		if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
10699 		    (ms_goneby > highest_ms)) {
10700 			highest_ms = ms_goneby;
10701 			hnet = net;
10702 		}
10703 	}
10704 	if (hnet &&
10705 	    ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
10706 		state_overide = 1;
10707 	} else {
10708 		state_overide = 0;
10709 	}
10710 
10711 	if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
10712 		/*-
10713 		 * Found the one with longest delay bounds OR it is
10714 		 * unconfirmed and still not marked unreachable.
10715 		 */
10716 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
10717 #ifdef SCTP_DEBUG
10718 		if (hnet) {
10719 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
10720 			    (struct sockaddr *)&hnet->ro._l_addr);
10721 		} else {
10722 			SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
10723 		}
10724 #endif
10725 		/* update the timer now */
10726 		hnet->last_sent_time = *now;
10727 		return (hnet);
10728 	}
10729 	/* Nothing to HB */
10730 	return (NULL);
10731 }
10732 
10733 int
10734 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
10735 {
10736 	struct sctp_tmit_chunk *chk;
10737 	struct sctp_nets *net;
10738 	struct sctp_heartbeat_chunk *hb;
10739 	struct timeval now;
10740 	struct sockaddr_in *sin;
10741 	struct sockaddr_in6 *sin6;
10742 
10743 	SCTP_TCB_LOCK_ASSERT(stcb);
10744 	if (user_req == 0) {
10745 		net = sctp_select_hb_destination(stcb, &now);
10746 		if (net == NULL) {
10747 			/*-
10748 			 * All our busy none to send to, just start the
10749 			 * timer again.
10750 			 */
10751 			if (stcb->asoc.state == 0) {
10752 				return (0);
10753 			}
10754 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
10755 			    stcb->sctp_ep,
10756 			    stcb,
10757 			    net);
10758 			return (0);
10759 		}
10760 	} else {
10761 		net = u_net;
10762 		if (net == NULL) {
10763 			return (0);
10764 		}
10765 		(void)SCTP_GETTIME_TIMEVAL(&now);
10766 	}
10767 	sin = (struct sockaddr_in *)&net->ro._l_addr;
10768 	if (sin->sin_family != AF_INET) {
10769 		if (sin->sin_family != AF_INET6) {
10770 			/* huh */
10771 			return (0);
10772 		}
10773 	}
10774 	sctp_alloc_a_chunk(stcb, chk);
10775 	if (chk == NULL) {
10776 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
10777 		return (0);
10778 	}
10779 	chk->copy_by_ref = 0;
10780 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
10781 	chk->rec.chunk_id.can_take_data = 1;
10782 	chk->asoc = &stcb->asoc;
10783 	chk->send_size = sizeof(struct sctp_heartbeat_chunk);
10784 
10785 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10786 	if (chk->data == NULL) {
10787 		sctp_free_a_chunk(stcb, chk);
10788 		return (0);
10789 	}
10790 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10791 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10792 	chk->sent = SCTP_DATAGRAM_UNSENT;
10793 	chk->snd_count = 0;
10794 	chk->whoTo = net;
10795 	atomic_add_int(&chk->whoTo->ref_count, 1);
10796 	/* Now we have a mbuf that we can fill in with the details */
10797 	hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
10798 	memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
10799 	/* fill out chunk header */
10800 	hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
10801 	hb->ch.chunk_flags = 0;
10802 	hb->ch.chunk_length = htons(chk->send_size);
10803 	/* Fill out hb parameter */
10804 	hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
10805 	hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
10806 	hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
10807 	hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
10808 	/* Did our user request this one, put it in */
10809 	hb->heartbeat.hb_info.user_req = user_req;
10810 	hb->heartbeat.hb_info.addr_family = sin->sin_family;
10811 	hb->heartbeat.hb_info.addr_len = sin->sin_len;
10812 	if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
10813 		/*
10814 		 * we only take from the entropy pool if the address is not
10815 		 * confirmed.
10816 		 */
10817 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10818 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10819 	} else {
10820 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
10821 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
10822 	}
10823 	if (sin->sin_family == AF_INET) {
10824 		memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
10825 	} else if (sin->sin_family == AF_INET6) {
10826 		/* We leave the scope the way it is in our lookup table. */
10827 		sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
10828 		memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
10829 	} else {
10830 		/* huh compiler bug */
10831 		return (0);
10832 	}
10833 
10834 	/*
10835 	 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
10836 	 * PF-heartbeats.  Because of this, threshold management is done by
10837 	 * the t3 timer handler, and does not need to be done upon the send
10838 	 * of a PF-heartbeat. If CMT PF is on and the destination to which a
10839 	 * heartbeat is being sent is in PF state, do NOT do threshold
10840 	 * management.
10841 	 */
10842 	if ((SCTP_BASE_SYSCTL(sctp_cmt_pf) == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
10843 		/* ok we have a destination that needs a beat */
10844 		/* lets do the theshold management Qiaobing style */
10845 		if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
10846 		    stcb->asoc.max_send_times)) {
10847 			/*-
10848 			 * we have lost the association, in a way this is
10849 			 * quite bad since we really are one less time since
10850 			 * we really did not send yet. This is the down side
10851 			 * to the Q's style as defined in the RFC and not my
10852 			 * alternate style defined in the RFC.
10853 			 */
10854 			if (chk->data != NULL) {
10855 				sctp_m_freem(chk->data);
10856 				chk->data = NULL;
10857 			}
10858 			/*
10859 			 * Here we do NOT use the macro since the
10860 			 * association is now gone.
10861 			 */
10862 			if (chk->whoTo) {
10863 				sctp_free_remote_addr(chk->whoTo);
10864 				chk->whoTo = NULL;
10865 			}
10866 			sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
10867 			return (-1);
10868 		}
10869 	}
10870 	net->hb_responded = 0;
10871 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10872 	stcb->asoc.ctrl_queue_cnt++;
10873 	SCTP_STAT_INCR(sctps_sendheartbeat);
10874 	/*-
10875 	 * Call directly med level routine to put out the chunk. It will
10876 	 * always tumble out control chunks aka HB but it may even tumble
10877 	 * out data too.
10878 	 */
10879 	return (1);
10880 }
10881 
10882 void
10883 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
10884     uint32_t high_tsn)
10885 {
10886 	struct sctp_association *asoc;
10887 	struct sctp_ecne_chunk *ecne;
10888 	struct sctp_tmit_chunk *chk;
10889 
10890 	asoc = &stcb->asoc;
10891 	SCTP_TCB_LOCK_ASSERT(stcb);
10892 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10893 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
10894 			/* found a previous ECN_ECHO update it if needed */
10895 			ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10896 			ecne->tsn = htonl(high_tsn);
10897 			return;
10898 		}
10899 	}
10900 	/* nope could not find one to update so we must build one */
10901 	sctp_alloc_a_chunk(stcb, chk);
10902 	if (chk == NULL) {
10903 		return;
10904 	}
10905 	chk->copy_by_ref = 0;
10906 	SCTP_STAT_INCR(sctps_sendecne);
10907 	chk->rec.chunk_id.id = SCTP_ECN_ECHO;
10908 	chk->rec.chunk_id.can_take_data = 0;
10909 	chk->asoc = &stcb->asoc;
10910 	chk->send_size = sizeof(struct sctp_ecne_chunk);
10911 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10912 	if (chk->data == NULL) {
10913 		sctp_free_a_chunk(stcb, chk);
10914 		return;
10915 	}
10916 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10917 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10918 	chk->sent = SCTP_DATAGRAM_UNSENT;
10919 	chk->snd_count = 0;
10920 	chk->whoTo = net;
10921 	atomic_add_int(&chk->whoTo->ref_count, 1);
10922 	stcb->asoc.ecn_echo_cnt_onq++;
10923 	ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10924 	ecne->ch.chunk_type = SCTP_ECN_ECHO;
10925 	ecne->ch.chunk_flags = 0;
10926 	ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
10927 	ecne->tsn = htonl(high_tsn);
10928 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10929 	asoc->ctrl_queue_cnt++;
10930 }
10931 
10932 void
10933 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
10934     struct mbuf *m, int iphlen, int bad_crc)
10935 {
10936 	struct sctp_association *asoc;
10937 	struct sctp_pktdrop_chunk *drp;
10938 	struct sctp_tmit_chunk *chk;
10939 	uint8_t *datap;
10940 	int len;
10941 	int was_trunc = 0;
10942 	struct ip *iph;
10943 
10944 #ifdef INET6
10945 	struct ip6_hdr *ip6h;
10946 
10947 #endif
10948 	int fullsz = 0, extra = 0;
10949 	long spc;
10950 	int offset;
10951 	struct sctp_chunkhdr *ch, chunk_buf;
10952 	unsigned int chk_length;
10953 
10954 	if (!stcb) {
10955 		return;
10956 	}
10957 	asoc = &stcb->asoc;
10958 	SCTP_TCB_LOCK_ASSERT(stcb);
10959 	if (asoc->peer_supports_pktdrop == 0) {
10960 		/*-
10961 		 * peer must declare support before I send one.
10962 		 */
10963 		return;
10964 	}
10965 	if (stcb->sctp_socket == NULL) {
10966 		return;
10967 	}
10968 	sctp_alloc_a_chunk(stcb, chk);
10969 	if (chk == NULL) {
10970 		return;
10971 	}
10972 	chk->copy_by_ref = 0;
10973 	iph = mtod(m, struct ip *);
10974 	if (iph == NULL) {
10975 		sctp_free_a_chunk(stcb, chk);
10976 		return;
10977 	}
10978 	switch (iph->ip_v) {
10979 	case IPVERSION:
10980 		/* IPv4 */
10981 		len = chk->send_size = iph->ip_len;
10982 		break;
10983 #ifdef INET6
10984 	case IPV6_VERSION >> 4:
10985 		/* IPv6 */
10986 		ip6h = mtod(m, struct ip6_hdr *);
10987 		len = chk->send_size = htons(ip6h->ip6_plen);
10988 		break;
10989 #endif
10990 	default:
10991 		return;
10992 	}
10993 	/* Validate that we do not have an ABORT in here. */
10994 	offset = iphlen + sizeof(struct sctphdr);
10995 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10996 	    sizeof(*ch), (uint8_t *) & chunk_buf);
10997 	while (ch != NULL) {
10998 		chk_length = ntohs(ch->chunk_length);
10999 		if (chk_length < sizeof(*ch)) {
11000 			/* break to abort land */
11001 			break;
11002 		}
11003 		switch (ch->chunk_type) {
11004 		case SCTP_PACKET_DROPPED:
11005 		case SCTP_ABORT_ASSOCIATION:
11006 			/*-
11007 			 * we don't respond with an PKT-DROP to an ABORT
11008 			 * or PKT-DROP
11009 			 */
11010 			sctp_free_a_chunk(stcb, chk);
11011 			return;
11012 		default:
11013 			break;
11014 		}
11015 		offset += SCTP_SIZE32(chk_length);
11016 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11017 		    sizeof(*ch), (uint8_t *) & chunk_buf);
11018 	}
11019 
11020 	if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11021 	    min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11022 		/*
11023 		 * only send 1 mtu worth, trim off the excess on the end.
11024 		 */
11025 		fullsz = len - extra;
11026 		len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11027 		was_trunc = 1;
11028 	}
11029 	chk->asoc = &stcb->asoc;
11030 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11031 	if (chk->data == NULL) {
11032 jump_out:
11033 		sctp_free_a_chunk(stcb, chk);
11034 		return;
11035 	}
11036 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11037 	drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11038 	if (drp == NULL) {
11039 		sctp_m_freem(chk->data);
11040 		chk->data = NULL;
11041 		goto jump_out;
11042 	}
11043 	chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11044 	    sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11045 	chk->book_size_scale = 0;
11046 	if (was_trunc) {
11047 		drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11048 		drp->trunc_len = htons(fullsz);
11049 		/*
11050 		 * Len is already adjusted to size minus overhead above take
11051 		 * out the pkt_drop chunk itself from it.
11052 		 */
11053 		chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
11054 		len = chk->send_size;
11055 	} else {
11056 		/* no truncation needed */
11057 		drp->ch.chunk_flags = 0;
11058 		drp->trunc_len = htons(0);
11059 	}
11060 	if (bad_crc) {
11061 		drp->ch.chunk_flags |= SCTP_BADCRC;
11062 	}
11063 	chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11064 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11065 	chk->sent = SCTP_DATAGRAM_UNSENT;
11066 	chk->snd_count = 0;
11067 	if (net) {
11068 		/* we should hit here */
11069 		chk->whoTo = net;
11070 	} else {
11071 		chk->whoTo = asoc->primary_destination;
11072 	}
11073 	atomic_add_int(&chk->whoTo->ref_count, 1);
11074 	chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11075 	chk->rec.chunk_id.can_take_data = 1;
11076 	drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11077 	drp->ch.chunk_length = htons(chk->send_size);
11078 	spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11079 	if (spc < 0) {
11080 		spc = 0;
11081 	}
11082 	drp->bottle_bw = htonl(spc);
11083 	if (asoc->my_rwnd) {
11084 		drp->current_onq = htonl(asoc->size_on_reasm_queue +
11085 		    asoc->size_on_all_streams +
11086 		    asoc->my_rwnd_control_len +
11087 		    stcb->sctp_socket->so_rcv.sb_cc);
11088 	} else {
11089 		/*-
11090 		 * If my rwnd is 0, possibly from mbuf depletion as well as
11091 		 * space used, tell the peer there is NO space aka onq == bw
11092 		 */
11093 		drp->current_onq = htonl(spc);
11094 	}
11095 	drp->reserved = 0;
11096 	datap = drp->data;
11097 	m_copydata(m, iphlen, len, (caddr_t)datap);
11098 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11099 	asoc->ctrl_queue_cnt++;
11100 }
11101 
11102 void
11103 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
11104 {
11105 	struct sctp_association *asoc;
11106 	struct sctp_cwr_chunk *cwr;
11107 	struct sctp_tmit_chunk *chk;
11108 
11109 	asoc = &stcb->asoc;
11110 	SCTP_TCB_LOCK_ASSERT(stcb);
11111 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11112 		if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
11113 			/* found a previous ECN_CWR update it if needed */
11114 			cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11115 			if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
11116 			    MAX_TSN)) {
11117 				cwr->tsn = htonl(high_tsn);
11118 			}
11119 			return;
11120 		}
11121 	}
11122 	/* nope could not find one to update so we must build one */
11123 	sctp_alloc_a_chunk(stcb, chk);
11124 	if (chk == NULL) {
11125 		return;
11126 	}
11127 	chk->copy_by_ref = 0;
11128 	chk->rec.chunk_id.id = SCTP_ECN_CWR;
11129 	chk->rec.chunk_id.can_take_data = 1;
11130 	chk->asoc = &stcb->asoc;
11131 	chk->send_size = sizeof(struct sctp_cwr_chunk);
11132 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11133 	if (chk->data == NULL) {
11134 		sctp_free_a_chunk(stcb, chk);
11135 		return;
11136 	}
11137 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11138 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11139 	chk->sent = SCTP_DATAGRAM_UNSENT;
11140 	chk->snd_count = 0;
11141 	chk->whoTo = net;
11142 	atomic_add_int(&chk->whoTo->ref_count, 1);
11143 	cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11144 	cwr->ch.chunk_type = SCTP_ECN_CWR;
11145 	cwr->ch.chunk_flags = 0;
11146 	cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11147 	cwr->tsn = htonl(high_tsn);
11148 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11149 	asoc->ctrl_queue_cnt++;
11150 }
11151 
11152 void
11153 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
11154     int number_entries, uint16_t * list,
11155     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11156 {
11157 	int len, old_len, i;
11158 	struct sctp_stream_reset_out_request *req_out;
11159 	struct sctp_chunkhdr *ch;
11160 
11161 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11162 
11163 
11164 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11165 
11166 	/* get to new offset for the param. */
11167 	req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11168 	/* now how long will this param be? */
11169 	len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11170 	req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11171 	req_out->ph.param_length = htons(len);
11172 	req_out->request_seq = htonl(seq);
11173 	req_out->response_seq = htonl(resp_seq);
11174 	req_out->send_reset_at_tsn = htonl(last_sent);
11175 	if (number_entries) {
11176 		for (i = 0; i < number_entries; i++) {
11177 			req_out->list_of_streams[i] = htons(list[i]);
11178 		}
11179 	}
11180 	if (SCTP_SIZE32(len) > len) {
11181 		/*-
11182 		 * Need to worry about the pad we may end up adding to the
11183 		 * end. This is easy since the struct is either aligned to 4
11184 		 * bytes or 2 bytes off.
11185 		 */
11186 		req_out->list_of_streams[number_entries] = 0;
11187 	}
11188 	/* now fix the chunk length */
11189 	ch->chunk_length = htons(len + old_len);
11190 	chk->book_size = len + old_len;
11191 	chk->book_size_scale = 0;
11192 	chk->send_size = SCTP_SIZE32(chk->book_size);
11193 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11194 	return;
11195 }
11196 
11197 
11198 void
11199 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11200     int number_entries, uint16_t * list,
11201     uint32_t seq)
11202 {
11203 	int len, old_len, i;
11204 	struct sctp_stream_reset_in_request *req_in;
11205 	struct sctp_chunkhdr *ch;
11206 
11207 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11208 
11209 
11210 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11211 
11212 	/* get to new offset for the param. */
11213 	req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11214 	/* now how long will this param be? */
11215 	len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11216 	req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11217 	req_in->ph.param_length = htons(len);
11218 	req_in->request_seq = htonl(seq);
11219 	if (number_entries) {
11220 		for (i = 0; i < number_entries; i++) {
11221 			req_in->list_of_streams[i] = htons(list[i]);
11222 		}
11223 	}
11224 	if (SCTP_SIZE32(len) > len) {
11225 		/*-
11226 		 * Need to worry about the pad we may end up adding to the
11227 		 * end. This is easy since the struct is either aligned to 4
11228 		 * bytes or 2 bytes off.
11229 		 */
11230 		req_in->list_of_streams[number_entries] = 0;
11231 	}
11232 	/* now fix the chunk length */
11233 	ch->chunk_length = htons(len + old_len);
11234 	chk->book_size = len + old_len;
11235 	chk->book_size_scale = 0;
11236 	chk->send_size = SCTP_SIZE32(chk->book_size);
11237 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11238 	return;
11239 }
11240 
11241 
11242 void
11243 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11244     uint32_t seq)
11245 {
11246 	int len, old_len;
11247 	struct sctp_stream_reset_tsn_request *req_tsn;
11248 	struct sctp_chunkhdr *ch;
11249 
11250 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11251 
11252 
11253 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11254 
11255 	/* get to new offset for the param. */
11256 	req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11257 	/* now how long will this param be? */
11258 	len = sizeof(struct sctp_stream_reset_tsn_request);
11259 	req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11260 	req_tsn->ph.param_length = htons(len);
11261 	req_tsn->request_seq = htonl(seq);
11262 
11263 	/* now fix the chunk length */
11264 	ch->chunk_length = htons(len + old_len);
11265 	chk->send_size = len + old_len;
11266 	chk->book_size = SCTP_SIZE32(chk->send_size);
11267 	chk->book_size_scale = 0;
11268 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11269 	return;
11270 }
11271 
11272 void
11273 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11274     uint32_t resp_seq, uint32_t result)
11275 {
11276 	int len, old_len;
11277 	struct sctp_stream_reset_response *resp;
11278 	struct sctp_chunkhdr *ch;
11279 
11280 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11281 
11282 
11283 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11284 
11285 	/* get to new offset for the param. */
11286 	resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11287 	/* now how long will this param be? */
11288 	len = sizeof(struct sctp_stream_reset_response);
11289 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11290 	resp->ph.param_length = htons(len);
11291 	resp->response_seq = htonl(resp_seq);
11292 	resp->result = ntohl(result);
11293 
11294 	/* now fix the chunk length */
11295 	ch->chunk_length = htons(len + old_len);
11296 	chk->book_size = len + old_len;
11297 	chk->book_size_scale = 0;
11298 	chk->send_size = SCTP_SIZE32(chk->book_size);
11299 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11300 	return;
11301 
11302 }
11303 
11304 
11305 void
11306 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11307     uint32_t resp_seq, uint32_t result,
11308     uint32_t send_una, uint32_t recv_next)
11309 {
11310 	int len, old_len;
11311 	struct sctp_stream_reset_response_tsn *resp;
11312 	struct sctp_chunkhdr *ch;
11313 
11314 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11315 
11316 
11317 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11318 
11319 	/* get to new offset for the param. */
11320 	resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11321 	/* now how long will this param be? */
11322 	len = sizeof(struct sctp_stream_reset_response_tsn);
11323 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11324 	resp->ph.param_length = htons(len);
11325 	resp->response_seq = htonl(resp_seq);
11326 	resp->result = htonl(result);
11327 	resp->senders_next_tsn = htonl(send_una);
11328 	resp->receivers_next_tsn = htonl(recv_next);
11329 
11330 	/* now fix the chunk length */
11331 	ch->chunk_length = htons(len + old_len);
11332 	chk->book_size = len + old_len;
11333 	chk->send_size = SCTP_SIZE32(chk->book_size);
11334 	chk->book_size_scale = 0;
11335 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11336 	return;
11337 }
11338 
11339 static void
11340 sctp_add_a_stream(struct sctp_tmit_chunk *chk,
11341     uint32_t seq,
11342     uint16_t adding)
11343 {
11344 	int len, old_len;
11345 	struct sctp_chunkhdr *ch;
11346 	struct sctp_stream_reset_add_strm *addstr;
11347 
11348 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11349 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11350 
11351 	/* get to new offset for the param. */
11352 	addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11353 	/* now how long will this param be? */
11354 	len = sizeof(struct sctp_stream_reset_add_strm);
11355 
11356 	/* Fill it out. */
11357 	addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_STREAMS);
11358 	addstr->ph.param_length = htons(len);
11359 	addstr->request_seq = htonl(seq);
11360 	addstr->number_of_streams = htons(adding);
11361 	addstr->reserved = 0;
11362 
11363 	/* now fix the chunk length */
11364 	ch->chunk_length = htons(len + old_len);
11365 	chk->send_size = len + old_len;
11366 	chk->book_size = SCTP_SIZE32(chk->send_size);
11367 	chk->book_size_scale = 0;
11368 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11369 	return;
11370 }
11371 
11372 int
11373 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11374     int number_entries, uint16_t * list,
11375     uint8_t send_out_req,
11376     uint32_t resp_seq,
11377     uint8_t send_in_req,
11378     uint8_t send_tsn_req,
11379     uint8_t add_stream,
11380     uint16_t adding
11381 )
11382 {
11383 
11384 	struct sctp_association *asoc;
11385 	struct sctp_tmit_chunk *chk;
11386 	struct sctp_chunkhdr *ch;
11387 	uint32_t seq;
11388 
11389 	asoc = &stcb->asoc;
11390 	if (asoc->stream_reset_outstanding) {
11391 		/*-
11392 		 * Already one pending, must get ACK back to clear the flag.
11393 		 */
11394 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11395 		return (EBUSY);
11396 	}
11397 	if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
11398 	    (add_stream == 0)) {
11399 		/* nothing to do */
11400 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11401 		return (EINVAL);
11402 	}
11403 	if (send_tsn_req && (send_out_req || send_in_req)) {
11404 		/* error, can't do that */
11405 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11406 		return (EINVAL);
11407 	}
11408 	sctp_alloc_a_chunk(stcb, chk);
11409 	if (chk == NULL) {
11410 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11411 		return (ENOMEM);
11412 	}
11413 	chk->copy_by_ref = 0;
11414 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11415 	chk->rec.chunk_id.can_take_data = 0;
11416 	chk->asoc = &stcb->asoc;
11417 	chk->book_size = sizeof(struct sctp_chunkhdr);
11418 	chk->send_size = SCTP_SIZE32(chk->book_size);
11419 	chk->book_size_scale = 0;
11420 
11421 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11422 	if (chk->data == NULL) {
11423 		sctp_free_a_chunk(stcb, chk);
11424 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11425 		return (ENOMEM);
11426 	}
11427 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11428 
11429 	/* setup chunk parameters */
11430 	chk->sent = SCTP_DATAGRAM_UNSENT;
11431 	chk->snd_count = 0;
11432 	chk->whoTo = asoc->primary_destination;
11433 	atomic_add_int(&chk->whoTo->ref_count, 1);
11434 
11435 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11436 	ch->chunk_type = SCTP_STREAM_RESET;
11437 	ch->chunk_flags = 0;
11438 	ch->chunk_length = htons(chk->book_size);
11439 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11440 
11441 	seq = stcb->asoc.str_reset_seq_out;
11442 	if (send_out_req) {
11443 		sctp_add_stream_reset_out(chk, number_entries, list,
11444 		    seq, resp_seq, (stcb->asoc.sending_seq - 1));
11445 		asoc->stream_reset_out_is_outstanding = 1;
11446 		seq++;
11447 		asoc->stream_reset_outstanding++;
11448 	}
11449 	if (add_stream) {
11450 		sctp_add_a_stream(chk, seq, adding);
11451 		seq++;
11452 		asoc->stream_reset_outstanding++;
11453 	}
11454 	if (send_in_req) {
11455 		sctp_add_stream_reset_in(chk, number_entries, list, seq);
11456 		asoc->stream_reset_outstanding++;
11457 	}
11458 	if (send_tsn_req) {
11459 		sctp_add_stream_reset_tsn(chk, seq);
11460 		asoc->stream_reset_outstanding++;
11461 	}
11462 	asoc->str_reset = chk;
11463 
11464 	/* insert the chunk for sending */
11465 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11466 	    chk,
11467 	    sctp_next);
11468 	asoc->ctrl_queue_cnt++;
11469 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11470 	return (0);
11471 }
11472 
11473 void
11474 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11475     struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11476 {
11477 	/*-
11478 	 * Formulate the abort message, and send it back down.
11479 	 */
11480 	struct mbuf *o_pak;
11481 	struct mbuf *mout;
11482 	struct sctp_abort_msg *abm;
11483 	struct ip *iph, *iph_out;
11484 	struct udphdr *udp;
11485 
11486 #ifdef INET6
11487 	struct ip6_hdr *ip6, *ip6_out;
11488 
11489 #endif
11490 	int iphlen_out, len;
11491 
11492 	/* don't respond to ABORT with ABORT */
11493 	if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11494 		if (err_cause)
11495 			sctp_m_freem(err_cause);
11496 		return;
11497 	}
11498 	iph = mtod(m, struct ip *);
11499 	switch (iph->ip_v) {
11500 	case IPVERSION:
11501 		len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11502 		break;
11503 #ifdef INET6
11504 	case IPV6_VERSION >> 4:
11505 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11506 		break;
11507 #endif
11508 	default:
11509 		if (err_cause) {
11510 			sctp_m_freem(err_cause);
11511 		}
11512 		return;
11513 	}
11514 	if (port) {
11515 		len += sizeof(struct udphdr);
11516 	}
11517 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11518 	if (mout == NULL) {
11519 		if (err_cause) {
11520 			sctp_m_freem(err_cause);
11521 		}
11522 		return;
11523 	}
11524 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
11525 	SCTP_BUF_LEN(mout) = len;
11526 	SCTP_BUF_NEXT(mout) = err_cause;
11527 	iph_out = NULL;
11528 #ifdef INET6
11529 	ip6_out = NULL;
11530 #endif
11531 	switch (iph->ip_v) {
11532 	case IPVERSION:
11533 		iph_out = mtod(mout, struct ip *);
11534 
11535 		/* Fill in the IP header for the ABORT */
11536 		iph_out->ip_v = IPVERSION;
11537 		iph_out->ip_hl = (sizeof(struct ip) / 4);
11538 		iph_out->ip_tos = (u_char)0;
11539 		iph_out->ip_id = 0;
11540 		iph_out->ip_off = 0;
11541 		iph_out->ip_ttl = MAXTTL;
11542 		if (port) {
11543 			iph_out->ip_p = IPPROTO_UDP;
11544 		} else {
11545 			iph_out->ip_p = IPPROTO_SCTP;
11546 		}
11547 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11548 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11549 		/* let IP layer calculate this */
11550 		iph_out->ip_sum = 0;
11551 
11552 		iphlen_out = sizeof(*iph_out);
11553 		abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
11554 		break;
11555 #ifdef INET6
11556 	case IPV6_VERSION >> 4:
11557 		ip6 = (struct ip6_hdr *)iph;
11558 		ip6_out = mtod(mout, struct ip6_hdr *);
11559 
11560 		/* Fill in the IP6 header for the ABORT */
11561 		ip6_out->ip6_flow = ip6->ip6_flow;
11562 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11563 		if (port) {
11564 			ip6_out->ip6_nxt = IPPROTO_UDP;
11565 		} else {
11566 			ip6_out->ip6_nxt = IPPROTO_SCTP;
11567 		}
11568 		ip6_out->ip6_src = ip6->ip6_dst;
11569 		ip6_out->ip6_dst = ip6->ip6_src;
11570 
11571 		iphlen_out = sizeof(*ip6_out);
11572 		abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
11573 		break;
11574 #endif				/* INET6 */
11575 	default:
11576 		/* Currently not supported */
11577 		sctp_m_freem(mout);
11578 		return;
11579 	}
11580 
11581 	udp = (struct udphdr *)abm;
11582 	if (port) {
11583 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11584 		udp->uh_dport = port;
11585 		/* set udp->uh_ulen later */
11586 		udp->uh_sum = 0;
11587 		iphlen_out += sizeof(struct udphdr);
11588 		abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
11589 	}
11590 	abm->sh.src_port = sh->dest_port;
11591 	abm->sh.dest_port = sh->src_port;
11592 	abm->sh.checksum = 0;
11593 	if (vtag == 0) {
11594 		abm->sh.v_tag = sh->v_tag;
11595 		abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
11596 	} else {
11597 		abm->sh.v_tag = htonl(vtag);
11598 		abm->msg.ch.chunk_flags = 0;
11599 	}
11600 	abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11601 
11602 	if (err_cause) {
11603 		struct mbuf *m_tmp = err_cause;
11604 		int err_len = 0;
11605 
11606 		/* get length of the err_cause chain */
11607 		while (m_tmp != NULL) {
11608 			err_len += SCTP_BUF_LEN(m_tmp);
11609 			m_tmp = SCTP_BUF_NEXT(m_tmp);
11610 		}
11611 		len = SCTP_BUF_LEN(mout) + err_len;
11612 		if (err_len % 4) {
11613 			/* need pad at end of chunk */
11614 			uint32_t cpthis = 0;
11615 			int padlen;
11616 
11617 			padlen = 4 - (len % 4);
11618 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11619 			len += padlen;
11620 		}
11621 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
11622 	} else {
11623 		len = SCTP_BUF_LEN(mout);
11624 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
11625 	}
11626 
11627 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11628 		/* no mbuf's */
11629 		sctp_m_freem(mout);
11630 		return;
11631 	}
11632 	if (iph_out != NULL) {
11633 		sctp_route_t ro;
11634 		struct sctp_tcb *stcb = NULL;
11635 		int ret;
11636 
11637 		/* zap the stack pointer to the route */
11638 		bzero(&ro, sizeof ro);
11639 		if (port) {
11640 			udp->uh_ulen = htons(len - sizeof(struct ip));
11641 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11642 		}
11643 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
11644 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
11645 		/* set IPv4 length */
11646 		iph_out->ip_len = len;
11647 		/* out it goes */
11648 #ifdef  SCTP_PACKET_LOGGING
11649 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11650 			sctp_packet_log(mout, len);
11651 #endif
11652 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11653 		if (port) {
11654 			abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
11655 			SCTP_STAT_INCR(sctps_sendswcrc);
11656 			SCTP_ENABLE_UDP_CSUM(o_pak);
11657 		} else {
11658 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
11659 			mout->m_pkthdr.csum_data = 0;
11660 			SCTP_STAT_INCR(sctps_sendhwcrc);
11661 		}
11662 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
11663 
11664 		/* Free the route if we got one back */
11665 		if (ro.ro_rt)
11666 			RTFREE(ro.ro_rt);
11667 	}
11668 #ifdef INET6
11669 	if (ip6_out != NULL) {
11670 		struct route_in6 ro;
11671 		int ret;
11672 		struct sctp_tcb *stcb = NULL;
11673 		struct ifnet *ifp = NULL;
11674 
11675 		/* zap the stack pointer to the route */
11676 		bzero(&ro, sizeof(ro));
11677 		if (port) {
11678 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11679 		}
11680 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
11681 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
11682 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
11683 #ifdef  SCTP_PACKET_LOGGING
11684 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11685 			sctp_packet_log(mout, len);
11686 #endif
11687 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11688 		if (port) {
11689 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
11690 			    (stcb) &&
11691 			    (stcb->asoc.loopback_scope))) {
11692 				abm->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11693 				SCTP_STAT_INCR(sctps_sendswcrc);
11694 			} else {
11695 				SCTP_STAT_INCR(sctps_sendnocrc);
11696 			}
11697 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11698 				udp->uh_sum = 0xffff;
11699 			}
11700 		} else {
11701 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
11702 			    (stcb) &&
11703 			    (stcb->asoc.loopback_scope))) {
11704 				mout->m_pkthdr.csum_flags = CSUM_SCTP;
11705 				mout->m_pkthdr.csum_data = 0;
11706 				SCTP_STAT_INCR(sctps_sendhwcrc);
11707 			} else {
11708 				SCTP_STAT_INCR(sctps_sendnocrc);
11709 			}
11710 		}
11711 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11712 
11713 		/* Free the route if we got one back */
11714 		if (ro.ro_rt)
11715 			RTFREE(ro.ro_rt);
11716 	}
11717 #endif
11718 	SCTP_STAT_INCR(sctps_sendpackets);
11719 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11720 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11721 }
11722 
11723 void
11724 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
11725     uint32_t vrf_id, uint16_t port)
11726 {
11727 	struct mbuf *o_pak;
11728 	struct sctphdr *sh, *sh_out;
11729 	struct sctp_chunkhdr *ch;
11730 	struct ip *iph, *iph_out;
11731 	struct udphdr *udp = NULL;
11732 	struct mbuf *mout;
11733 
11734 #ifdef INET6
11735 	struct ip6_hdr *ip6, *ip6_out;
11736 
11737 #endif
11738 	int iphlen_out, len;
11739 
11740 	iph = mtod(m, struct ip *);
11741 	sh = (struct sctphdr *)((caddr_t)iph + iphlen);
11742 	switch (iph->ip_v) {
11743 	case IPVERSION:
11744 		len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11745 		break;
11746 #ifdef INET6
11747 	case IPV6_VERSION >> 4:
11748 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11749 		break;
11750 #endif
11751 	default:
11752 		if (scm) {
11753 			sctp_m_freem(scm);
11754 		}
11755 		return;
11756 	}
11757 	if (port) {
11758 		len += sizeof(struct udphdr);
11759 	}
11760 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11761 	if (mout == NULL) {
11762 		if (scm) {
11763 			sctp_m_freem(scm);
11764 		}
11765 		return;
11766 	}
11767 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
11768 	SCTP_BUF_LEN(mout) = len;
11769 	SCTP_BUF_NEXT(mout) = scm;
11770 	iph_out = NULL;
11771 #ifdef INET6
11772 	ip6_out = NULL;
11773 #endif
11774 	switch (iph->ip_v) {
11775 	case IPVERSION:
11776 		iph_out = mtod(mout, struct ip *);
11777 
11778 		/* Fill in the IP header for the ABORT */
11779 		iph_out->ip_v = IPVERSION;
11780 		iph_out->ip_hl = (sizeof(struct ip) / 4);
11781 		iph_out->ip_tos = (u_char)0;
11782 		iph_out->ip_id = 0;
11783 		iph_out->ip_off = 0;
11784 		iph_out->ip_ttl = MAXTTL;
11785 		if (port) {
11786 			iph_out->ip_p = IPPROTO_UDP;
11787 		} else {
11788 			iph_out->ip_p = IPPROTO_SCTP;
11789 		}
11790 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11791 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11792 		/* let IP layer calculate this */
11793 		iph_out->ip_sum = 0;
11794 
11795 		iphlen_out = sizeof(struct ip);
11796 		sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
11797 		break;
11798 #ifdef INET6
11799 	case IPV6_VERSION >> 4:
11800 		ip6 = (struct ip6_hdr *)iph;
11801 		ip6_out = mtod(mout, struct ip6_hdr *);
11802 
11803 		/* Fill in the IP6 header for the ABORT */
11804 		ip6_out->ip6_flow = ip6->ip6_flow;
11805 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11806 		if (port) {
11807 			ip6_out->ip6_nxt = IPPROTO_UDP;
11808 		} else {
11809 			ip6_out->ip6_nxt = IPPROTO_SCTP;
11810 		}
11811 		ip6_out->ip6_src = ip6->ip6_dst;
11812 		ip6_out->ip6_dst = ip6->ip6_src;
11813 
11814 		iphlen_out = sizeof(struct ip6_hdr);
11815 		sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
11816 		break;
11817 #endif				/* INET6 */
11818 	default:
11819 		/* Currently not supported */
11820 		sctp_m_freem(mout);
11821 		return;
11822 	}
11823 
11824 	udp = (struct udphdr *)sh_out;
11825 	if (port) {
11826 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11827 		udp->uh_dport = port;
11828 		/* set udp->uh_ulen later */
11829 		udp->uh_sum = 0;
11830 		iphlen_out += sizeof(struct udphdr);
11831 		sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
11832 	}
11833 	sh_out->src_port = sh->dest_port;
11834 	sh_out->dest_port = sh->src_port;
11835 	sh_out->v_tag = vtag;
11836 	sh_out->checksum = 0;
11837 
11838 	ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
11839 	ch->chunk_type = SCTP_OPERATION_ERROR;
11840 	ch->chunk_flags = 0;
11841 
11842 	if (scm) {
11843 		struct mbuf *m_tmp = scm;
11844 		int cause_len = 0;
11845 
11846 		/* get length of the err_cause chain */
11847 		while (m_tmp != NULL) {
11848 			cause_len += SCTP_BUF_LEN(m_tmp);
11849 			m_tmp = SCTP_BUF_NEXT(m_tmp);
11850 		}
11851 		len = SCTP_BUF_LEN(mout) + cause_len;
11852 		if (cause_len % 4) {
11853 			/* need pad at end of chunk */
11854 			uint32_t cpthis = 0;
11855 			int padlen;
11856 
11857 			padlen = 4 - (len % 4);
11858 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11859 			len += padlen;
11860 		}
11861 		ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
11862 	} else {
11863 		len = SCTP_BUF_LEN(mout);
11864 		ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
11865 	}
11866 
11867 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11868 		/* no mbuf's */
11869 		sctp_m_freem(mout);
11870 		return;
11871 	}
11872 	if (iph_out != NULL) {
11873 		sctp_route_t ro;
11874 		struct sctp_tcb *stcb = NULL;
11875 		int ret;
11876 
11877 		/* zap the stack pointer to the route */
11878 		bzero(&ro, sizeof ro);
11879 		if (port) {
11880 			udp->uh_ulen = htons(len - sizeof(struct ip));
11881 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11882 		}
11883 		/* set IPv4 length */
11884 		iph_out->ip_len = len;
11885 		/* out it goes */
11886 #ifdef  SCTP_PACKET_LOGGING
11887 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11888 			sctp_packet_log(mout, len);
11889 #endif
11890 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11891 		if (port) {
11892 			sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
11893 			SCTP_STAT_INCR(sctps_sendswcrc);
11894 			SCTP_ENABLE_UDP_CSUM(o_pak);
11895 		} else {
11896 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
11897 			mout->m_pkthdr.csum_data = 0;
11898 			SCTP_STAT_INCR(sctps_sendhwcrc);
11899 		}
11900 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
11901 
11902 		/* Free the route if we got one back */
11903 		if (ro.ro_rt)
11904 			RTFREE(ro.ro_rt);
11905 	}
11906 #ifdef INET6
11907 	if (ip6_out != NULL) {
11908 		struct route_in6 ro;
11909 		int ret;
11910 		struct sctp_tcb *stcb = NULL;
11911 		struct ifnet *ifp = NULL;
11912 
11913 		/* zap the stack pointer to the route */
11914 		bzero(&ro, sizeof(ro));
11915 		if (port) {
11916 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11917 		}
11918 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
11919 #ifdef  SCTP_PACKET_LOGGING
11920 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11921 			sctp_packet_log(mout, len);
11922 #endif
11923 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11924 		if (port) {
11925 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
11926 			    (stcb) &&
11927 			    (stcb->asoc.loopback_scope))) {
11928 				sh_out->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11929 				SCTP_STAT_INCR(sctps_sendswcrc);
11930 			} else {
11931 				SCTP_STAT_INCR(sctps_sendnocrc);
11932 			}
11933 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11934 				udp->uh_sum = 0xffff;
11935 			}
11936 		} else {
11937 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
11938 			    (stcb) &&
11939 			    (stcb->asoc.loopback_scope))) {
11940 				mout->m_pkthdr.csum_flags = CSUM_SCTP;
11941 				mout->m_pkthdr.csum_data = 0;
11942 				SCTP_STAT_INCR(sctps_sendhwcrc);
11943 			} else {
11944 				SCTP_STAT_INCR(sctps_sendnocrc);
11945 			}
11946 		}
11947 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11948 
11949 		/* Free the route if we got one back */
11950 		if (ro.ro_rt)
11951 			RTFREE(ro.ro_rt);
11952 	}
11953 #endif
11954 	SCTP_STAT_INCR(sctps_sendpackets);
11955 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11956 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11957 }
11958 
11959 static struct mbuf *
11960 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
11961     struct uio *uio,
11962     struct sctp_sndrcvinfo *srcv,
11963     int max_send_len,
11964     int user_marks_eor,
11965     int *error,
11966     uint32_t * sndout,
11967     struct mbuf **new_tail)
11968 {
11969 	struct mbuf *m;
11970 
11971 	m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
11972 	    (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
11973 	if (m == NULL) {
11974 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11975 		*error = ENOMEM;
11976 	} else {
11977 		*sndout = m_length(m, NULL);
11978 		*new_tail = m_last(m);
11979 	}
11980 	return (m);
11981 }
11982 
11983 static int
11984 sctp_copy_one(struct sctp_stream_queue_pending *sp,
11985     struct uio *uio,
11986     int resv_upfront)
11987 {
11988 	int left;
11989 
11990 	left = sp->length;
11991 	sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
11992 	    resv_upfront, 0);
11993 	if (sp->data == NULL) {
11994 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11995 		return (ENOMEM);
11996 	}
11997 	sp->tail_mbuf = m_last(sp->data);
11998 	return (0);
11999 }
12000 
12001 
12002 
12003 static struct sctp_stream_queue_pending *
12004 sctp_copy_it_in(struct sctp_tcb *stcb,
12005     struct sctp_association *asoc,
12006     struct sctp_sndrcvinfo *srcv,
12007     struct uio *uio,
12008     struct sctp_nets *net,
12009     int max_send_len,
12010     int user_marks_eor,
12011     int *error,
12012     int non_blocking)
12013 {
12014 	/*-
12015 	 * This routine must be very careful in its work. Protocol
12016 	 * processing is up and running so care must be taken to spl...()
12017 	 * when you need to do something that may effect the stcb/asoc. The
12018 	 * sb is locked however. When data is copied the protocol processing
12019 	 * should be enabled since this is a slower operation...
12020 	 */
12021 	struct sctp_stream_queue_pending *sp = NULL;
12022 	int resv_in_first;
12023 
12024 	*error = 0;
12025 	/* Now can we send this? */
12026 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12027 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12028 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12029 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12030 		/* got data while shutting down */
12031 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12032 		*error = ECONNRESET;
12033 		goto out_now;
12034 	}
12035 	sctp_alloc_a_strmoq(stcb, sp);
12036 	if (sp == NULL) {
12037 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12038 		*error = ENOMEM;
12039 		goto out_now;
12040 	}
12041 	sp->act_flags = 0;
12042 	sp->sender_all_done = 0;
12043 	sp->sinfo_flags = srcv->sinfo_flags;
12044 	sp->timetolive = srcv->sinfo_timetolive;
12045 	sp->ppid = srcv->sinfo_ppid;
12046 	sp->context = srcv->sinfo_context;
12047 	sp->strseq = 0;
12048 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12049 
12050 	sp->stream = srcv->sinfo_stream;
12051 	sp->length = min(uio->uio_resid, max_send_len);
12052 	if ((sp->length == (uint32_t) uio->uio_resid) &&
12053 	    ((user_marks_eor == 0) ||
12054 	    (srcv->sinfo_flags & SCTP_EOF) ||
12055 	    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12056 		sp->msg_is_complete = 1;
12057 	} else {
12058 		sp->msg_is_complete = 0;
12059 	}
12060 	sp->sender_all_done = 0;
12061 	sp->some_taken = 0;
12062 	sp->put_last_out = 0;
12063 	resv_in_first = sizeof(struct sctp_data_chunk);
12064 	sp->data = sp->tail_mbuf = NULL;
12065 	if (sp->length == 0) {
12066 		*error = 0;
12067 		goto skip_copy;
12068 	}
12069 	sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12070 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12071 		sctp_auth_key_acquire(stcb, stcb->asoc.authinfo.active_keyid);
12072 		sp->holds_key_ref = 1;
12073 	}
12074 	*error = sctp_copy_one(sp, uio, resv_in_first);
12075 skip_copy:
12076 	if (*error) {
12077 		sctp_free_a_strmoq(stcb, sp);
12078 		sp = NULL;
12079 	} else {
12080 		if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12081 			sp->net = net;
12082 		} else {
12083 			sp->net = asoc->primary_destination;
12084 		}
12085 		atomic_add_int(&sp->net->ref_count, 1);
12086 		sctp_set_prsctp_policy(sp);
12087 	}
12088 out_now:
12089 	return (sp);
12090 }
12091 
12092 
12093 int
12094 sctp_sosend(struct socket *so,
12095     struct sockaddr *addr,
12096     struct uio *uio,
12097     struct mbuf *top,
12098     struct mbuf *control,
12099     int flags,
12100     struct thread *p
12101 )
12102 {
12103 	struct sctp_inpcb *inp;
12104 	int error, use_rcvinfo = 0;
12105 	struct sctp_sndrcvinfo srcv;
12106 	struct sockaddr *addr_to_use;
12107 
12108 #ifdef INET6
12109 	struct sockaddr_in sin;
12110 
12111 #endif
12112 
12113 	inp = (struct sctp_inpcb *)so->so_pcb;
12114 	if (control) {
12115 		/* process cmsg snd/rcv info (maybe a assoc-id) */
12116 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
12117 		    sizeof(srcv))) {
12118 			/* got one */
12119 			use_rcvinfo = 1;
12120 		}
12121 	}
12122 	addr_to_use = addr;
12123 #if defined(INET6)  && !defined(__Userspace__)	/* TODO port in6_sin6_2_sin */
12124 	if ((addr) && (addr->sa_family == AF_INET6)) {
12125 		struct sockaddr_in6 *sin6;
12126 
12127 		sin6 = (struct sockaddr_in6 *)addr;
12128 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12129 			in6_sin6_2_sin(&sin, sin6);
12130 			addr_to_use = (struct sockaddr *)&sin;
12131 		}
12132 	}
12133 #endif
12134 	error = sctp_lower_sosend(so, addr_to_use, uio, top,
12135 	    control,
12136 	    flags,
12137 	    use_rcvinfo, &srcv
12138 	    ,p
12139 	    );
12140 	return (error);
12141 }
12142 
12143 
12144 int
12145 sctp_lower_sosend(struct socket *so,
12146     struct sockaddr *addr,
12147     struct uio *uio,
12148     struct mbuf *i_pak,
12149     struct mbuf *control,
12150     int flags,
12151     int use_rcvinfo,
12152     struct sctp_sndrcvinfo *srcv
12153     ,
12154     struct thread *p
12155 )
12156 {
12157 	unsigned int sndlen = 0, max_len;
12158 	int error, len;
12159 	struct mbuf *top = NULL;
12160 	int queue_only = 0, queue_only_for_init = 0;
12161 	int free_cnt_applied = 0;
12162 	int un_sent = 0;
12163 	int now_filled = 0;
12164 	unsigned int inqueue_bytes = 0;
12165 	struct sctp_block_entry be;
12166 	struct sctp_inpcb *inp;
12167 	struct sctp_tcb *stcb = NULL;
12168 	struct timeval now;
12169 	struct sctp_nets *net;
12170 	struct sctp_association *asoc;
12171 	struct sctp_inpcb *t_inp;
12172 	int user_marks_eor;
12173 	int create_lock_applied = 0;
12174 	int nagle_applies = 0;
12175 	int some_on_control = 0;
12176 	int got_all_of_the_send = 0;
12177 	int hold_tcblock = 0;
12178 	int non_blocking = 0;
12179 	int temp_flags = 0;
12180 	uint32_t local_add_more, local_soresv = 0;
12181 
12182 	error = 0;
12183 	net = NULL;
12184 	stcb = NULL;
12185 	asoc = NULL;
12186 
12187 	t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12188 	if (inp == NULL) {
12189 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12190 		error = EINVAL;
12191 		if (i_pak) {
12192 			SCTP_RELEASE_PKT(i_pak);
12193 		}
12194 		return (error);
12195 	}
12196 	if ((uio == NULL) && (i_pak == NULL)) {
12197 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12198 		return (EINVAL);
12199 	}
12200 	user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12201 	atomic_add_int(&inp->total_sends, 1);
12202 	if (uio) {
12203 		if (uio->uio_resid < 0) {
12204 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12205 			return (EINVAL);
12206 		}
12207 		sndlen = uio->uio_resid;
12208 	} else {
12209 		top = SCTP_HEADER_TO_CHAIN(i_pak);
12210 		sndlen = SCTP_HEADER_LEN(i_pak);
12211 	}
12212 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
12213 	    addr,
12214 	    sndlen);
12215 	/*-
12216 	 * Pre-screen address, if one is given the sin-len
12217 	 * must be set correctly!
12218 	 */
12219 	if (addr) {
12220 		if ((addr->sa_family == AF_INET) &&
12221 		    (addr->sa_len != sizeof(struct sockaddr_in))) {
12222 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12223 			error = EINVAL;
12224 			goto out_unlocked;
12225 		} else if ((addr->sa_family == AF_INET6) &&
12226 		    (addr->sa_len != sizeof(struct sockaddr_in6))) {
12227 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12228 			error = EINVAL;
12229 			goto out_unlocked;
12230 		}
12231 	}
12232 	hold_tcblock = 0;
12233 
12234 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12235 	    (inp->sctp_socket->so_qlimit)) {
12236 		/* The listener can NOT send */
12237 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12238 		error = ENOTCONN;
12239 		goto out_unlocked;
12240 	}
12241 	if ((use_rcvinfo) && srcv) {
12242 		if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
12243 		    PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
12244 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12245 			error = EINVAL;
12246 			goto out_unlocked;
12247 		}
12248 		if (srcv->sinfo_flags)
12249 			SCTP_STAT_INCR(sctps_sends_with_flags);
12250 
12251 		if (srcv->sinfo_flags & SCTP_SENDALL) {
12252 			/* its a sendall */
12253 			error = sctp_sendall(inp, uio, top, srcv);
12254 			top = NULL;
12255 			goto out_unlocked;
12256 		}
12257 	}
12258 	/* now we must find the assoc */
12259 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12260 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12261 		SCTP_INP_RLOCK(inp);
12262 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
12263 		if (stcb == NULL) {
12264 			SCTP_INP_RUNLOCK(inp);
12265 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12266 			error = ENOTCONN;
12267 			goto out_unlocked;
12268 		}
12269 		SCTP_TCB_LOCK(stcb);
12270 		hold_tcblock = 1;
12271 		SCTP_INP_RUNLOCK(inp);
12272 		if (addr) {
12273 			/* Must locate the net structure if addr given */
12274 			net = sctp_findnet(stcb, addr);
12275 			if (net) {
12276 				/* validate port was 0 or correct */
12277 				struct sockaddr_in *sin;
12278 
12279 				sin = (struct sockaddr_in *)addr;
12280 				if ((sin->sin_port != 0) &&
12281 				    (sin->sin_port != stcb->rport)) {
12282 					net = NULL;
12283 				}
12284 			}
12285 			temp_flags |= SCTP_ADDR_OVER;
12286 		} else
12287 			net = stcb->asoc.primary_destination;
12288 		if (addr && (net == NULL)) {
12289 			/* Could not find address, was it legal */
12290 			if (addr->sa_family == AF_INET) {
12291 				struct sockaddr_in *sin;
12292 
12293 				sin = (struct sockaddr_in *)addr;
12294 				if (sin->sin_addr.s_addr == 0) {
12295 					if ((sin->sin_port == 0) ||
12296 					    (sin->sin_port == stcb->rport)) {
12297 						net = stcb->asoc.primary_destination;
12298 					}
12299 				}
12300 			} else {
12301 				struct sockaddr_in6 *sin6;
12302 
12303 				sin6 = (struct sockaddr_in6 *)addr;
12304 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
12305 					if ((sin6->sin6_port == 0) ||
12306 					    (sin6->sin6_port == stcb->rport)) {
12307 						net = stcb->asoc.primary_destination;
12308 					}
12309 				}
12310 			}
12311 		}
12312 		if (net == NULL) {
12313 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12314 			error = EINVAL;
12315 			goto out_unlocked;
12316 		}
12317 	} else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
12318 		stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
12319 		if (stcb) {
12320 			if (addr)
12321 				/*
12322 				 * Must locate the net structure if addr
12323 				 * given
12324 				 */
12325 				net = sctp_findnet(stcb, addr);
12326 			else
12327 				net = stcb->asoc.primary_destination;
12328 			if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
12329 			    ((net == NULL) || (addr == NULL))) {
12330 				struct sockaddr_in *sin;
12331 
12332 				if (addr == NULL) {
12333 					SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12334 					error = EINVAL;
12335 					goto out_unlocked;
12336 				}
12337 				sin = (struct sockaddr_in *)addr;
12338 				/* Validate port is 0 or correct */
12339 				if ((sin->sin_port != 0) &&
12340 				    (sin->sin_port != stcb->rport)) {
12341 					net = NULL;
12342 				}
12343 			}
12344 		}
12345 		hold_tcblock = 0;
12346 	} else if (addr) {
12347 		/*-
12348 		 * Since we did not use findep we must
12349 		 * increment it, and if we don't find a tcb
12350 		 * decrement it.
12351 		 */
12352 		SCTP_INP_WLOCK(inp);
12353 		SCTP_INP_INCR_REF(inp);
12354 		SCTP_INP_WUNLOCK(inp);
12355 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12356 		if (stcb == NULL) {
12357 			SCTP_INP_WLOCK(inp);
12358 			SCTP_INP_DECR_REF(inp);
12359 			SCTP_INP_WUNLOCK(inp);
12360 		} else {
12361 			hold_tcblock = 1;
12362 		}
12363 	}
12364 	if ((stcb == NULL) && (addr)) {
12365 		/* Possible implicit send? */
12366 		SCTP_ASOC_CREATE_LOCK(inp);
12367 		create_lock_applied = 1;
12368 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12369 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12370 			/* Should I really unlock ? */
12371 			SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12372 			error = EINVAL;
12373 			goto out_unlocked;
12374 
12375 		}
12376 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12377 		    (addr->sa_family == AF_INET6)) {
12378 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12379 			error = EINVAL;
12380 			goto out_unlocked;
12381 		}
12382 		SCTP_INP_WLOCK(inp);
12383 		SCTP_INP_INCR_REF(inp);
12384 		SCTP_INP_WUNLOCK(inp);
12385 		/* With the lock applied look again */
12386 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12387 		if (stcb == NULL) {
12388 			SCTP_INP_WLOCK(inp);
12389 			SCTP_INP_DECR_REF(inp);
12390 			SCTP_INP_WUNLOCK(inp);
12391 		} else {
12392 			hold_tcblock = 1;
12393 		}
12394 		if (t_inp != inp) {
12395 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12396 			error = ENOTCONN;
12397 			goto out_unlocked;
12398 		}
12399 	}
12400 	if (stcb == NULL) {
12401 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
12402 		    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12403 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12404 			error = ENOTCONN;
12405 			goto out_unlocked;
12406 		}
12407 		if (addr == NULL) {
12408 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12409 			error = ENOENT;
12410 			goto out_unlocked;
12411 		} else {
12412 			/*
12413 			 * UDP style, we must go ahead and start the INIT
12414 			 * process
12415 			 */
12416 			uint32_t vrf_id;
12417 
12418 			if ((use_rcvinfo) && (srcv) &&
12419 			    ((srcv->sinfo_flags & SCTP_ABORT) ||
12420 			    ((srcv->sinfo_flags & SCTP_EOF) &&
12421 			    (sndlen == 0)))) {
12422 				/*-
12423 				 * User asks to abort a non-existant assoc,
12424 				 * or EOF a non-existant assoc with no data
12425 				 */
12426 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12427 				error = ENOENT;
12428 				goto out_unlocked;
12429 			}
12430 			/* get an asoc/stcb struct */
12431 			vrf_id = inp->def_vrf_id;
12432 #ifdef INVARIANTS
12433 			if (create_lock_applied == 0) {
12434 				panic("Error, should hold create lock and I don't?");
12435 			}
12436 #endif
12437 			stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
12438 			    p
12439 			    );
12440 			if (stcb == NULL) {
12441 				/* Error is setup for us in the call */
12442 				goto out_unlocked;
12443 			}
12444 			if (create_lock_applied) {
12445 				SCTP_ASOC_CREATE_UNLOCK(inp);
12446 				create_lock_applied = 0;
12447 			} else {
12448 				SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12449 			}
12450 			/*
12451 			 * Turn on queue only flag to prevent data from
12452 			 * being sent
12453 			 */
12454 			queue_only = 1;
12455 			asoc = &stcb->asoc;
12456 			SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12457 			(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12458 
12459 			/* initialize authentication params for the assoc */
12460 			sctp_initialize_auth_params(inp, stcb);
12461 
12462 			if (control) {
12463 				/*
12464 				 * see if a init structure exists in cmsg
12465 				 * headers
12466 				 */
12467 				struct sctp_initmsg initm;
12468 				int i;
12469 
12470 				if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
12471 				    sizeof(initm))) {
12472 					/*
12473 					 * we have an INIT override of the
12474 					 * default
12475 					 */
12476 					if (initm.sinit_max_attempts)
12477 						asoc->max_init_times = initm.sinit_max_attempts;
12478 					if (initm.sinit_num_ostreams)
12479 						asoc->pre_open_streams = initm.sinit_num_ostreams;
12480 					if (initm.sinit_max_instreams)
12481 						asoc->max_inbound_streams = initm.sinit_max_instreams;
12482 					if (initm.sinit_max_init_timeo)
12483 						asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
12484 					if (asoc->streamoutcnt < asoc->pre_open_streams) {
12485 						struct sctp_stream_out *tmp_str;
12486 						int had_lock = 0;
12487 
12488 						/* Default is NOT correct */
12489 						SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
12490 						    asoc->streamoutcnt, asoc->pre_open_streams);
12491 						/*
12492 						 * What happens if this
12493 						 * fails? we panic ...
12494 						 */
12495 
12496 						if (hold_tcblock) {
12497 							had_lock = 1;
12498 							SCTP_TCB_UNLOCK(stcb);
12499 						}
12500 						SCTP_MALLOC(tmp_str,
12501 						    struct sctp_stream_out *,
12502 						    (asoc->pre_open_streams *
12503 						    sizeof(struct sctp_stream_out)),
12504 						    SCTP_M_STRMO);
12505 						if (had_lock) {
12506 							SCTP_TCB_LOCK(stcb);
12507 						}
12508 						if (tmp_str != NULL) {
12509 							SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
12510 							asoc->strmout = tmp_str;
12511 							asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
12512 						} else {
12513 							asoc->pre_open_streams = asoc->streamoutcnt;
12514 						}
12515 						for (i = 0; i < asoc->streamoutcnt; i++) {
12516 							/*-
12517 							 * inbound side must be set
12518 							 * to 0xffff, also NOTE when
12519 							 * we get the INIT-ACK back
12520 							 * (for INIT sender) we MUST
12521 							 * reduce the count
12522 							 * (streamoutcnt) but first
12523 							 * check if we sent to any
12524 							 * of the upper streams that
12525 							 * were dropped (if some
12526 							 * were). Those that were
12527 							 * dropped must be notified
12528 							 * to the upper layer as
12529 							 * failed to send.
12530 							 */
12531 							asoc->strmout[i].next_sequence_sent = 0x0;
12532 							TAILQ_INIT(&asoc->strmout[i].outqueue);
12533 							asoc->strmout[i].stream_no = i;
12534 							asoc->strmout[i].last_msg_incomplete = 0;
12535 							asoc->strmout[i].next_spoke.tqe_next = 0;
12536 							asoc->strmout[i].next_spoke.tqe_prev = 0;
12537 						}
12538 					}
12539 				}
12540 			}
12541 			hold_tcblock = 1;
12542 			/* out with the INIT */
12543 			queue_only_for_init = 1;
12544 			/*-
12545 			 * we may want to dig in after this call and adjust the MTU
12546 			 * value. It defaulted to 1500 (constant) but the ro
12547 			 * structure may now have an update and thus we may need to
12548 			 * change it BEFORE we append the message.
12549 			 */
12550 			net = stcb->asoc.primary_destination;
12551 			asoc = &stcb->asoc;
12552 		}
12553 	}
12554 	if ((SCTP_SO_IS_NBIO(so)
12555 	    || (flags & MSG_NBIO)
12556 	    )) {
12557 		non_blocking = 1;
12558 	}
12559 	asoc = &stcb->asoc;
12560 	atomic_add_int(&stcb->total_sends, 1);
12561 
12562 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12563 		if (sndlen > asoc->smallest_mtu) {
12564 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12565 			error = EMSGSIZE;
12566 			goto out_unlocked;
12567 		}
12568 	}
12569 	/* would we block? */
12570 	if (non_blocking) {
12571 		if (hold_tcblock == 0) {
12572 			SCTP_TCB_LOCK(stcb);
12573 			hold_tcblock = 1;
12574 		}
12575 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12576 		if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12577 		    (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12578 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
12579 			if (sndlen > SCTP_SB_LIMIT_SND(so))
12580 				error = EMSGSIZE;
12581 			else
12582 				error = EWOULDBLOCK;
12583 			goto out_unlocked;
12584 		}
12585 		stcb->asoc.sb_send_resv += sndlen;
12586 		SCTP_TCB_UNLOCK(stcb);
12587 		hold_tcblock = 0;
12588 	} else {
12589 		atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
12590 	}
12591 	local_soresv = sndlen;
12592 	/* Keep the stcb from being freed under our feet */
12593 	if (free_cnt_applied) {
12594 #ifdef INVARIANTS
12595 		panic("refcnt already incremented");
12596 #else
12597 		printf("refcnt:1 already incremented?\n");
12598 #endif
12599 	} else {
12600 		atomic_add_int(&stcb->asoc.refcnt, 1);
12601 		free_cnt_applied = 1;
12602 	}
12603 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12604 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12605 		error = ECONNRESET;
12606 		goto out_unlocked;
12607 	}
12608 	if (create_lock_applied) {
12609 		SCTP_ASOC_CREATE_UNLOCK(inp);
12610 		create_lock_applied = 0;
12611 	}
12612 	if (asoc->stream_reset_outstanding) {
12613 		/*
12614 		 * Can't queue any data while stream reset is underway.
12615 		 */
12616 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
12617 		error = EAGAIN;
12618 		goto out_unlocked;
12619 	}
12620 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12621 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12622 		queue_only = 1;
12623 	}
12624 	if ((use_rcvinfo == 0) || (srcv == NULL)) {
12625 		/* Grab the default stuff from the asoc */
12626 		srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
12627 	}
12628 	/* we are now done with all control */
12629 	if (control) {
12630 		sctp_m_freem(control);
12631 		control = NULL;
12632 	}
12633 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12634 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12635 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12636 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12637 		if ((use_rcvinfo) &&
12638 		    (srcv->sinfo_flags & SCTP_ABORT)) {
12639 			;
12640 		} else {
12641 			SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12642 			error = ECONNRESET;
12643 			goto out_unlocked;
12644 		}
12645 	}
12646 	/* Ok, we will attempt a msgsnd :> */
12647 	if (p) {
12648 		p->td_ru.ru_msgsnd++;
12649 	}
12650 	if (stcb) {
12651 		if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
12652 			net = stcb->asoc.primary_destination;
12653 		}
12654 	}
12655 	if (net == NULL) {
12656 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12657 		error = EINVAL;
12658 		goto out_unlocked;
12659 	}
12660 	if ((net->flight_size > net->cwnd) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12661 		/*-
12662 		 * CMT: Added check for CMT above. net above is the primary
12663 		 * dest. If CMT is ON, sender should always attempt to send
12664 		 * with the output routine sctp_fill_outqueue() that loops
12665 		 * through all destination addresses. Therefore, if CMT is
12666 		 * ON, queue_only is NOT set to 1 here, so that
12667 		 * sctp_chunk_output() can be called below.
12668 		 */
12669 		queue_only = 1;
12670 	} else if (asoc->ifp_had_enobuf) {
12671 		SCTP_STAT_INCR(sctps_ifnomemqueued);
12672 		if (net->flight_size > (net->mtu * 2))
12673 			queue_only = 1;
12674 		asoc->ifp_had_enobuf = 0;
12675 	} else {
12676 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12677 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12678 	}
12679 	/* Are we aborting? */
12680 	if (srcv->sinfo_flags & SCTP_ABORT) {
12681 		struct mbuf *mm;
12682 		int tot_demand, tot_out = 0, max_out;
12683 
12684 		SCTP_STAT_INCR(sctps_sends_with_abort);
12685 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12686 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12687 			/* It has to be up before we abort */
12688 			/* how big is the user initiated abort? */
12689 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12690 			error = EINVAL;
12691 			goto out;
12692 		}
12693 		if (hold_tcblock) {
12694 			SCTP_TCB_UNLOCK(stcb);
12695 			hold_tcblock = 0;
12696 		}
12697 		if (top) {
12698 			struct mbuf *cntm = NULL;
12699 
12700 			mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
12701 			if (sndlen != 0) {
12702 				cntm = top;
12703 				while (cntm) {
12704 					tot_out += SCTP_BUF_LEN(cntm);
12705 					cntm = SCTP_BUF_NEXT(cntm);
12706 				}
12707 			}
12708 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12709 		} else {
12710 			/* Must fit in a MTU */
12711 			tot_out = sndlen;
12712 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12713 			if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12714 				/* To big */
12715 				SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12716 				error = EMSGSIZE;
12717 				goto out;
12718 			}
12719 			mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
12720 		}
12721 		if (mm == NULL) {
12722 			SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12723 			error = ENOMEM;
12724 			goto out;
12725 		}
12726 		max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12727 		max_out -= sizeof(struct sctp_abort_msg);
12728 		if (tot_out > max_out) {
12729 			tot_out = max_out;
12730 		}
12731 		if (mm) {
12732 			struct sctp_paramhdr *ph;
12733 
12734 			/* now move forward the data pointer */
12735 			ph = mtod(mm, struct sctp_paramhdr *);
12736 			ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12737 			ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
12738 			ph++;
12739 			SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
12740 			if (top == NULL) {
12741 				error = uiomove((caddr_t)ph, (int)tot_out, uio);
12742 				if (error) {
12743 					/*-
12744 					 * Here if we can't get his data we
12745 					 * still abort we just don't get to
12746 					 * send the users note :-0
12747 					 */
12748 					sctp_m_freem(mm);
12749 					mm = NULL;
12750 				}
12751 			} else {
12752 				if (sndlen != 0) {
12753 					SCTP_BUF_NEXT(mm) = top;
12754 				}
12755 			}
12756 		}
12757 		if (hold_tcblock == 0) {
12758 			SCTP_TCB_LOCK(stcb);
12759 			hold_tcblock = 1;
12760 		}
12761 		atomic_add_int(&stcb->asoc.refcnt, -1);
12762 		free_cnt_applied = 0;
12763 		/* release this lock, otherwise we hang on ourselves */
12764 		sctp_abort_an_association(stcb->sctp_ep, stcb,
12765 		    SCTP_RESPONSE_TO_USER_REQ,
12766 		    mm, SCTP_SO_LOCKED);
12767 		/* now relock the stcb so everything is sane */
12768 		hold_tcblock = 0;
12769 		stcb = NULL;
12770 		/*
12771 		 * In this case top is already chained to mm avoid double
12772 		 * free, since we free it below if top != NULL and driver
12773 		 * would free it after sending the packet out
12774 		 */
12775 		if (sndlen != 0) {
12776 			top = NULL;
12777 		}
12778 		goto out_unlocked;
12779 	}
12780 	/* Calculate the maximum we can send */
12781 	inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12782 	if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12783 		if (non_blocking) {
12784 			/* we already checked for non-blocking above. */
12785 			max_len = sndlen;
12786 		} else {
12787 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12788 		}
12789 	} else {
12790 		max_len = 0;
12791 	}
12792 	if (hold_tcblock) {
12793 		SCTP_TCB_UNLOCK(stcb);
12794 		hold_tcblock = 0;
12795 	}
12796 	/* Is the stream no. valid? */
12797 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
12798 		/* Invalid stream number */
12799 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12800 		error = EINVAL;
12801 		goto out_unlocked;
12802 	}
12803 	if (asoc->strmout == NULL) {
12804 		/* huh? software error */
12805 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12806 		error = EFAULT;
12807 		goto out_unlocked;
12808 	}
12809 	/* Unless E_EOR mode is on, we must make a send FIT in one call. */
12810 	if ((user_marks_eor == 0) &&
12811 	    (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
12812 		/* It will NEVER fit */
12813 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12814 		error = EMSGSIZE;
12815 		goto out_unlocked;
12816 	}
12817 	if ((uio == NULL) && user_marks_eor) {
12818 		/*-
12819 		 * We do not support eeor mode for
12820 		 * sending with mbuf chains (like sendfile).
12821 		 */
12822 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12823 		error = EINVAL;
12824 		goto out_unlocked;
12825 	}
12826 	if (user_marks_eor) {
12827 		local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
12828 	} else {
12829 		/*-
12830 		 * For non-eeor the whole message must fit in
12831 		 * the socket send buffer.
12832 		 */
12833 		local_add_more = sndlen;
12834 	}
12835 	len = 0;
12836 	if (non_blocking) {
12837 		goto skip_preblock;
12838 	}
12839 	if (((max_len <= local_add_more) &&
12840 	    (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
12841 	    (max_len == 0) ||
12842 	    ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12843 		/* No room right now ! */
12844 		SOCKBUF_LOCK(&so->so_snd);
12845 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12846 		while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
12847 		    ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12848 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
12849 			    (unsigned int)SCTP_SB_LIMIT_SND(so),
12850 			    inqueue_bytes,
12851 			    local_add_more,
12852 			    stcb->asoc.stream_queue_cnt,
12853 			    stcb->asoc.chunks_on_out_queue,
12854 			    SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
12855 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12856 				sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, so, asoc, sndlen);
12857 			}
12858 			be.error = 0;
12859 			stcb->block_entry = &be;
12860 			error = sbwait(&so->so_snd);
12861 			stcb->block_entry = NULL;
12862 			if (error || so->so_error || be.error) {
12863 				if (error == 0) {
12864 					if (so->so_error)
12865 						error = so->so_error;
12866 					if (be.error) {
12867 						error = be.error;
12868 					}
12869 				}
12870 				SOCKBUF_UNLOCK(&so->so_snd);
12871 				goto out_unlocked;
12872 			}
12873 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12874 				sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12875 				    so, asoc, stcb->asoc.total_output_queue_size);
12876 			}
12877 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12878 				goto out_unlocked;
12879 			}
12880 			inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12881 		}
12882 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12883 		if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12884 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12885 		} else {
12886 			max_len = 0;
12887 		}
12888 		SOCKBUF_UNLOCK(&so->so_snd);
12889 	}
12890 skip_preblock:
12891 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12892 		goto out_unlocked;
12893 	}
12894 	/*
12895 	 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
12896 	 * case NOTE: uio will be null when top/mbuf is passed
12897 	 */
12898 	if (sndlen == 0) {
12899 		if (srcv->sinfo_flags & SCTP_EOF) {
12900 			got_all_of_the_send = 1;
12901 			goto dataless_eof;
12902 		} else {
12903 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12904 			error = EINVAL;
12905 			goto out;
12906 		}
12907 	}
12908 	if (top == NULL) {
12909 		struct sctp_stream_queue_pending *sp;
12910 		struct sctp_stream_out *strm;
12911 		uint32_t sndout, initial_out;
12912 
12913 		initial_out = uio->uio_resid;
12914 
12915 		SCTP_TCB_SEND_LOCK(stcb);
12916 		if ((asoc->stream_locked) &&
12917 		    (asoc->stream_locked_on != srcv->sinfo_stream)) {
12918 			SCTP_TCB_SEND_UNLOCK(stcb);
12919 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12920 			error = EINVAL;
12921 			goto out;
12922 		}
12923 		SCTP_TCB_SEND_UNLOCK(stcb);
12924 
12925 		strm = &stcb->asoc.strmout[srcv->sinfo_stream];
12926 		if (strm->last_msg_incomplete == 0) {
12927 	do_a_copy_in:
12928 			sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
12929 			if ((sp == NULL) || (error)) {
12930 				goto out;
12931 			}
12932 			SCTP_TCB_SEND_LOCK(stcb);
12933 			if (sp->msg_is_complete) {
12934 				strm->last_msg_incomplete = 0;
12935 				asoc->stream_locked = 0;
12936 			} else {
12937 				/*
12938 				 * Just got locked to this guy in case of an
12939 				 * interrupt.
12940 				 */
12941 				strm->last_msg_incomplete = 1;
12942 				asoc->stream_locked = 1;
12943 				asoc->stream_locked_on = srcv->sinfo_stream;
12944 				sp->sender_all_done = 0;
12945 			}
12946 			sctp_snd_sb_alloc(stcb, sp->length);
12947 			atomic_add_int(&asoc->stream_queue_cnt, 1);
12948 			if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
12949 				sp->strseq = strm->next_sequence_sent;
12950 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
12951 					sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
12952 					    (uintptr_t) stcb, sp->length,
12953 					    (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
12954 				}
12955 				strm->next_sequence_sent++;
12956 			} else {
12957 				SCTP_STAT_INCR(sctps_sends_with_unord);
12958 			}
12959 			TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
12960 			if ((strm->next_spoke.tqe_next == NULL) &&
12961 			    (strm->next_spoke.tqe_prev == NULL)) {
12962 				/* Not on wheel, insert */
12963 				sctp_insert_on_wheel(stcb, asoc, strm, 1);
12964 			}
12965 			SCTP_TCB_SEND_UNLOCK(stcb);
12966 		} else {
12967 			SCTP_TCB_SEND_LOCK(stcb);
12968 			sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
12969 			SCTP_TCB_SEND_UNLOCK(stcb);
12970 			if (sp == NULL) {
12971 				/* ???? Huh ??? last msg is gone */
12972 #ifdef INVARIANTS
12973 				panic("Warning: Last msg marked incomplete, yet nothing left?");
12974 #else
12975 				SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
12976 				strm->last_msg_incomplete = 0;
12977 #endif
12978 				goto do_a_copy_in;
12979 
12980 			}
12981 		}
12982 		while (uio->uio_resid > 0) {
12983 			/* How much room do we have? */
12984 			struct mbuf *new_tail, *mm;
12985 
12986 			if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12987 				max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
12988 			else
12989 				max_len = 0;
12990 
12991 			if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
12992 			    (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
12993 			    (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
12994 				sndout = 0;
12995 				new_tail = NULL;
12996 				if (hold_tcblock) {
12997 					SCTP_TCB_UNLOCK(stcb);
12998 					hold_tcblock = 0;
12999 				}
13000 				mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
13001 				if ((mm == NULL) || error) {
13002 					if (mm) {
13003 						sctp_m_freem(mm);
13004 					}
13005 					goto out;
13006 				}
13007 				/* Update the mbuf and count */
13008 				SCTP_TCB_SEND_LOCK(stcb);
13009 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13010 					/*
13011 					 * we need to get out. Peer probably
13012 					 * aborted.
13013 					 */
13014 					sctp_m_freem(mm);
13015 					if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
13016 						SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13017 						error = ECONNRESET;
13018 					}
13019 					SCTP_TCB_SEND_UNLOCK(stcb);
13020 					goto out;
13021 				}
13022 				if (sp->tail_mbuf) {
13023 					/* tack it to the end */
13024 					SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13025 					sp->tail_mbuf = new_tail;
13026 				} else {
13027 					/* A stolen mbuf */
13028 					sp->data = mm;
13029 					sp->tail_mbuf = new_tail;
13030 				}
13031 				sctp_snd_sb_alloc(stcb, sndout);
13032 				atomic_add_int(&sp->length, sndout);
13033 				len += sndout;
13034 
13035 				/* Did we reach EOR? */
13036 				if ((uio->uio_resid == 0) &&
13037 				    ((user_marks_eor == 0) ||
13038 				    (srcv->sinfo_flags & SCTP_EOF) ||
13039 				    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
13040 					sp->msg_is_complete = 1;
13041 				} else {
13042 					sp->msg_is_complete = 0;
13043 				}
13044 				SCTP_TCB_SEND_UNLOCK(stcb);
13045 			}
13046 			if (uio->uio_resid == 0) {
13047 				/* got it all? */
13048 				continue;
13049 			}
13050 			/* PR-SCTP? */
13051 			if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
13052 				/*
13053 				 * This is ugly but we must assure locking
13054 				 * order
13055 				 */
13056 				if (hold_tcblock == 0) {
13057 					SCTP_TCB_LOCK(stcb);
13058 					hold_tcblock = 1;
13059 				}
13060 				sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
13061 				inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13062 				if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13063 					max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13064 				else
13065 					max_len = 0;
13066 				if (max_len > 0) {
13067 					continue;
13068 				}
13069 				SCTP_TCB_UNLOCK(stcb);
13070 				hold_tcblock = 0;
13071 			}
13072 			/* wait for space now */
13073 			if (non_blocking) {
13074 				/* Non-blocking io in place out */
13075 				goto skip_out_eof;
13076 			}
13077 			if ((net->flight_size > net->cwnd) &&
13078 			    (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
13079 				queue_only = 1;
13080 			} else if (asoc->ifp_had_enobuf) {
13081 				SCTP_STAT_INCR(sctps_ifnomemqueued);
13082 				if (net->flight_size > (net->mtu * 2)) {
13083 					queue_only = 1;
13084 				} else {
13085 					queue_only = 0;
13086 				}
13087 				asoc->ifp_had_enobuf = 0;
13088 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13089 				    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13090 			} else {
13091 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13092 				    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13093 				if (net->flight_size > net->cwnd) {
13094 					queue_only = 1;
13095 					SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13096 				} else {
13097 					queue_only = 0;
13098 				}
13099 			}
13100 			if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13101 			    (stcb->asoc.total_flight > 0) &&
13102 			    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13103 			    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13104 
13105 				/*-
13106 				 * Ok, Nagle is set on and we have data outstanding.
13107 				 * Don't send anything and let SACKs drive out the
13108 				 * data unless wen have a "full" segment to send.
13109 				 */
13110 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13111 					sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13112 				}
13113 				SCTP_STAT_INCR(sctps_naglequeued);
13114 				nagle_applies = 1;
13115 			} else {
13116 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13117 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13118 						sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13119 				}
13120 				SCTP_STAT_INCR(sctps_naglesent);
13121 				nagle_applies = 0;
13122 			}
13123 			/* What about the INIT, send it maybe */
13124 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13125 
13126 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13127 				    nagle_applies, un_sent);
13128 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13129 				    stcb->asoc.total_flight,
13130 				    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13131 			}
13132 			if (queue_only_for_init) {
13133 				if (hold_tcblock == 0) {
13134 					SCTP_TCB_LOCK(stcb);
13135 					hold_tcblock = 1;
13136 				}
13137 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13138 					/* a collision took us forward? */
13139 					queue_only_for_init = 0;
13140 					queue_only = 0;
13141 				} else {
13142 					sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13143 					SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
13144 					queue_only_for_init = 0;
13145 					queue_only = 1;
13146 				}
13147 			}
13148 			if ((queue_only == 0) && (nagle_applies == 0)) {
13149 				/*-
13150 				 * need to start chunk output
13151 				 * before blocking.. note that if
13152 				 * a lock is already applied, then
13153 				 * the input via the net is happening
13154 				 * and I don't need to start output :-D
13155 				 */
13156 				if (hold_tcblock == 0) {
13157 					if (SCTP_TCB_TRYLOCK(stcb)) {
13158 						hold_tcblock = 1;
13159 						sctp_chunk_output(inp,
13160 						    stcb,
13161 						    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13162 					}
13163 				} else {
13164 					sctp_chunk_output(inp,
13165 					    stcb,
13166 					    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13167 				}
13168 				if (hold_tcblock == 1) {
13169 					SCTP_TCB_UNLOCK(stcb);
13170 					hold_tcblock = 0;
13171 				}
13172 			}
13173 			SOCKBUF_LOCK(&so->so_snd);
13174 			/*-
13175 			 * This is a bit strange, but I think it will
13176 			 * work. The total_output_queue_size is locked and
13177 			 * protected by the TCB_LOCK, which we just released.
13178 			 * There is a race that can occur between releasing it
13179 			 * above, and me getting the socket lock, where sacks
13180 			 * come in but we have not put the SB_WAIT on the
13181 			 * so_snd buffer to get the wakeup. After the LOCK
13182 			 * is applied the sack_processing will also need to
13183 			 * LOCK the so->so_snd to do the actual sowwakeup(). So
13184 			 * once we have the socket buffer lock if we recheck the
13185 			 * size we KNOW we will get to sleep safely with the
13186 			 * wakeup flag in place.
13187 			 */
13188 			if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
13189 			    min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13190 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13191 					sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13192 					    so, asoc, uio->uio_resid);
13193 				}
13194 				be.error = 0;
13195 				stcb->block_entry = &be;
13196 				error = sbwait(&so->so_snd);
13197 				stcb->block_entry = NULL;
13198 
13199 				if (error || so->so_error || be.error) {
13200 					if (error == 0) {
13201 						if (so->so_error)
13202 							error = so->so_error;
13203 						if (be.error) {
13204 							error = be.error;
13205 						}
13206 					}
13207 					SOCKBUF_UNLOCK(&so->so_snd);
13208 					goto out_unlocked;
13209 				}
13210 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13211 					sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13212 					    so, asoc, stcb->asoc.total_output_queue_size);
13213 				}
13214 			}
13215 			SOCKBUF_UNLOCK(&so->so_snd);
13216 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13217 				goto out_unlocked;
13218 			}
13219 		}
13220 		SCTP_TCB_SEND_LOCK(stcb);
13221 		if (sp) {
13222 			if (sp->msg_is_complete == 0) {
13223 				strm->last_msg_incomplete = 1;
13224 				asoc->stream_locked = 1;
13225 				asoc->stream_locked_on = srcv->sinfo_stream;
13226 			} else {
13227 				sp->sender_all_done = 1;
13228 				strm->last_msg_incomplete = 0;
13229 				asoc->stream_locked = 0;
13230 			}
13231 		} else {
13232 			SCTP_PRINTF("Huh no sp TSNH?\n");
13233 			strm->last_msg_incomplete = 0;
13234 			asoc->stream_locked = 0;
13235 		}
13236 		SCTP_TCB_SEND_UNLOCK(stcb);
13237 		if (uio->uio_resid == 0) {
13238 			got_all_of_the_send = 1;
13239 		}
13240 	} else if (top) {
13241 		/* We send in a 0, since we do NOT have any locks */
13242 		error = sctp_msg_append(stcb, net, top, srcv, 0);
13243 		top = NULL;
13244 		if (srcv->sinfo_flags & SCTP_EOF) {
13245 			/*
13246 			 * This should only happen for Panda for the mbuf
13247 			 * send case, which does NOT yet support EEOR mode.
13248 			 * Thus, we can just set this flag to do the proper
13249 			 * EOF handling.
13250 			 */
13251 			got_all_of_the_send = 1;
13252 		}
13253 	}
13254 	if (error) {
13255 		goto out;
13256 	}
13257 dataless_eof:
13258 	/* EOF thing ? */
13259 	if ((srcv->sinfo_flags & SCTP_EOF) &&
13260 	    (got_all_of_the_send == 1) &&
13261 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
13262 		int cnt;
13263 
13264 		SCTP_STAT_INCR(sctps_sends_with_eof);
13265 		error = 0;
13266 		if (hold_tcblock == 0) {
13267 			SCTP_TCB_LOCK(stcb);
13268 			hold_tcblock = 1;
13269 		}
13270 		cnt = sctp_is_there_unsent_data(stcb);
13271 		if (TAILQ_EMPTY(&asoc->send_queue) &&
13272 		    TAILQ_EMPTY(&asoc->sent_queue) &&
13273 		    (cnt == 0)) {
13274 			if (asoc->locked_on_sending) {
13275 				goto abort_anyway;
13276 			}
13277 			/* there is nothing queued to send, so I'm done... */
13278 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13279 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13280 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13281 				/* only send SHUTDOWN the first time through */
13282 				sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
13283 				if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
13284 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13285 				}
13286 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
13287 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
13288 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13289 				    asoc->primary_destination);
13290 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13291 				    asoc->primary_destination);
13292 			}
13293 		} else {
13294 			/*-
13295 			 * we still got (or just got) data to send, so set
13296 			 * SHUTDOWN_PENDING
13297 			 */
13298 			/*-
13299 			 * XXX sockets draft says that SCTP_EOF should be
13300 			 * sent with no data.  currently, we will allow user
13301 			 * data to be sent first and move to
13302 			 * SHUTDOWN-PENDING
13303 			 */
13304 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13305 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13306 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13307 				if (hold_tcblock == 0) {
13308 					SCTP_TCB_LOCK(stcb);
13309 					hold_tcblock = 1;
13310 				}
13311 				if (asoc->locked_on_sending) {
13312 					/* Locked to send out the data */
13313 					struct sctp_stream_queue_pending *sp;
13314 
13315 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
13316 					if (sp) {
13317 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
13318 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
13319 					}
13320 				}
13321 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
13322 				if (TAILQ_EMPTY(&asoc->send_queue) &&
13323 				    TAILQ_EMPTY(&asoc->sent_queue) &&
13324 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13325 			abort_anyway:
13326 					if (free_cnt_applied) {
13327 						atomic_add_int(&stcb->asoc.refcnt, -1);
13328 						free_cnt_applied = 0;
13329 					}
13330 					sctp_abort_an_association(stcb->sctp_ep, stcb,
13331 					    SCTP_RESPONSE_TO_USER_REQ,
13332 					    NULL, SCTP_SO_LOCKED);
13333 					/*
13334 					 * now relock the stcb so everything
13335 					 * is sane
13336 					 */
13337 					hold_tcblock = 0;
13338 					stcb = NULL;
13339 					goto out;
13340 				}
13341 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13342 				    asoc->primary_destination);
13343 				sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13344 			}
13345 		}
13346 	}
13347 skip_out_eof:
13348 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
13349 		some_on_control = 1;
13350 	}
13351 	if ((net->flight_size > net->cwnd) &&
13352 	    (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
13353 		queue_only = 1;
13354 	} else if (asoc->ifp_had_enobuf) {
13355 		SCTP_STAT_INCR(sctps_ifnomemqueued);
13356 		if (net->flight_size > (net->mtu * 2)) {
13357 			queue_only = 1;
13358 		} else {
13359 			queue_only = 0;
13360 		}
13361 		asoc->ifp_had_enobuf = 0;
13362 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13363 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13364 	} else {
13365 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13366 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13367 		if (net->flight_size > net->cwnd) {
13368 			queue_only = 1;
13369 			SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13370 		} else {
13371 			queue_only = 0;
13372 		}
13373 	}
13374 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13375 	    (stcb->asoc.total_flight > 0) &&
13376 	    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13377 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13378 		/*-
13379 		 * Ok, Nagle is set on and we have data outstanding.
13380 		 * Don't send anything and let SACKs drive out the
13381 		 * data unless wen have a "full" segment to send.
13382 		 */
13383 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13384 			sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13385 		}
13386 		SCTP_STAT_INCR(sctps_naglequeued);
13387 		nagle_applies = 1;
13388 	} else {
13389 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13390 			if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13391 				sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13392 		}
13393 		SCTP_STAT_INCR(sctps_naglesent);
13394 		nagle_applies = 0;
13395 	}
13396 	if (queue_only_for_init) {
13397 		if (hold_tcblock == 0) {
13398 			SCTP_TCB_LOCK(stcb);
13399 			hold_tcblock = 1;
13400 		}
13401 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13402 			/* a collision took us forward? */
13403 			queue_only_for_init = 0;
13404 			queue_only = 0;
13405 		} else {
13406 			sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13407 			SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13408 			queue_only_for_init = 0;
13409 			queue_only = 1;
13410 		}
13411 	}
13412 	if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13413 		/* we can attempt to send too. */
13414 		if (hold_tcblock == 0) {
13415 			/*
13416 			 * If there is activity recv'ing sacks no need to
13417 			 * send
13418 			 */
13419 			if (SCTP_TCB_TRYLOCK(stcb)) {
13420 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13421 				hold_tcblock = 1;
13422 			}
13423 		} else {
13424 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13425 		}
13426 	} else if ((queue_only == 0) &&
13427 		    (stcb->asoc.peers_rwnd == 0) &&
13428 	    (stcb->asoc.total_flight == 0)) {
13429 		/* We get to have a probe outstanding */
13430 		if (hold_tcblock == 0) {
13431 			hold_tcblock = 1;
13432 			SCTP_TCB_LOCK(stcb);
13433 		}
13434 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13435 	} else if (some_on_control) {
13436 		int num_out, reason, frag_point;
13437 
13438 		/* Here we do control only */
13439 		if (hold_tcblock == 0) {
13440 			hold_tcblock = 1;
13441 			SCTP_TCB_LOCK(stcb);
13442 		}
13443 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13444 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13445 		    &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13446 	}
13447 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13448 	    queue_only, stcb->asoc.peers_rwnd, un_sent,
13449 	    stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13450 	    stcb->asoc.total_output_queue_size, error);
13451 
13452 out:
13453 out_unlocked:
13454 
13455 	if (local_soresv && stcb) {
13456 		atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13457 		local_soresv = 0;
13458 	}
13459 	if (create_lock_applied) {
13460 		SCTP_ASOC_CREATE_UNLOCK(inp);
13461 		create_lock_applied = 0;
13462 	}
13463 	if ((stcb) && hold_tcblock) {
13464 		SCTP_TCB_UNLOCK(stcb);
13465 	}
13466 	if (stcb && free_cnt_applied) {
13467 		atomic_add_int(&stcb->asoc.refcnt, -1);
13468 	}
13469 #ifdef INVARIANTS
13470 	if (stcb) {
13471 		if (mtx_owned(&stcb->tcb_mtx)) {
13472 			panic("Leaving with tcb mtx owned?");
13473 		}
13474 		if (mtx_owned(&stcb->tcb_send_mtx)) {
13475 			panic("Leaving with tcb send mtx owned?");
13476 		}
13477 	}
13478 #endif
13479 #ifdef INVARIANTS
13480 	if (inp) {
13481 		sctp_validate_no_locks(inp);
13482 	} else {
13483 		printf("Warning - inp is NULL so cant validate locks\n");
13484 	}
13485 #endif
13486 	if (top) {
13487 		sctp_m_freem(top);
13488 	}
13489 	if (control) {
13490 		sctp_m_freem(control);
13491 	}
13492 	return (error);
13493 }
13494 
13495 
13496 /*
13497  * generate an AUTHentication chunk, if required
13498  */
13499 struct mbuf *
13500 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13501     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13502     struct sctp_tcb *stcb, uint8_t chunk)
13503 {
13504 	struct mbuf *m_auth;
13505 	struct sctp_auth_chunk *auth;
13506 	int chunk_len;
13507 
13508 	if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13509 	    (stcb == NULL))
13510 		return (m);
13511 
13512 	/* sysctl disabled auth? */
13513 	if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13514 		return (m);
13515 
13516 	/* peer doesn't do auth... */
13517 	if (!stcb->asoc.peer_supports_auth) {
13518 		return (m);
13519 	}
13520 	/* does the requested chunk require auth? */
13521 	if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13522 		return (m);
13523 	}
13524 	m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13525 	if (m_auth == NULL) {
13526 		/* no mbuf's */
13527 		return (m);
13528 	}
13529 	/* reserve some space if this will be the first mbuf */
13530 	if (m == NULL)
13531 		SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13532 	/* fill in the AUTH chunk details */
13533 	auth = mtod(m_auth, struct sctp_auth_chunk *);
13534 	bzero(auth, sizeof(*auth));
13535 	auth->ch.chunk_type = SCTP_AUTHENTICATION;
13536 	auth->ch.chunk_flags = 0;
13537 	chunk_len = sizeof(*auth) +
13538 	    sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13539 	auth->ch.chunk_length = htons(chunk_len);
13540 	auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13541 	/* key id and hmac digest will be computed and filled in upon send */
13542 
13543 	/* save the offset where the auth was inserted into the chain */
13544 	if (m != NULL) {
13545 		struct mbuf *cn;
13546 
13547 		*offset = 0;
13548 		cn = m;
13549 		while (cn) {
13550 			*offset += SCTP_BUF_LEN(cn);
13551 			cn = SCTP_BUF_NEXT(cn);
13552 		}
13553 	} else
13554 		*offset = 0;
13555 
13556 	/* update length and return pointer to the auth chunk */
13557 	SCTP_BUF_LEN(m_auth) = chunk_len;
13558 	m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13559 	if (auth_ret != NULL)
13560 		*auth_ret = auth;
13561 
13562 	return (m);
13563 }
13564 
13565 #ifdef INET6
13566 int
13567 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13568 {
13569 	struct nd_prefix *pfx = NULL;
13570 	struct nd_pfxrouter *pfxrtr = NULL;
13571 	struct sockaddr_in6 gw6;
13572 
13573 	if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13574 		return (0);
13575 
13576 	/* get prefix entry of address */
13577 	LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13578 		if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13579 			continue;
13580 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13581 		    &src6->sin6_addr, &pfx->ndpr_mask))
13582 			break;
13583 	}
13584 	/* no prefix entry in the prefix list */
13585 	if (pfx == NULL) {
13586 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13587 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13588 		return (0);
13589 	}
13590 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13591 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13592 
13593 	/* search installed gateway from prefix entry */
13594 	for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
13595 	    pfxrtr->pfr_next) {
13596 		memset(&gw6, 0, sizeof(struct sockaddr_in6));
13597 		gw6.sin6_family = AF_INET6;
13598 		gw6.sin6_len = sizeof(struct sockaddr_in6);
13599 		memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13600 		    sizeof(struct in6_addr));
13601 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13602 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13603 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13604 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13605 		if (sctp_cmpaddr((struct sockaddr *)&gw6,
13606 		    ro->ro_rt->rt_gateway)) {
13607 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13608 			return (1);
13609 		}
13610 	}
13611 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13612 	return (0);
13613 }
13614 
13615 #endif
13616 
13617 int
13618 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13619 {
13620 	struct sockaddr_in *sin, *mask;
13621 	struct ifaddr *ifa;
13622 	struct in_addr srcnetaddr, gwnetaddr;
13623 
13624 	if (ro == NULL || ro->ro_rt == NULL ||
13625 	    sifa->address.sa.sa_family != AF_INET) {
13626 		return (0);
13627 	}
13628 	ifa = (struct ifaddr *)sifa->ifa;
13629 	mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13630 	sin = (struct sockaddr_in *)&sifa->address.sin;
13631 	srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13632 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13633 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13634 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13635 
13636 	sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13637 	gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13638 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13639 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13640 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13641 	if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13642 		return (1);
13643 	}
13644 	return (0);
13645 }
13646