xref: /freebsd/sys/netinet/sctp_output.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*-
2  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4  * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * a) Redistributions of source code must retain the above copyright notice,
10  *   this list of conditions and the following disclaimer.
11  *
12  * b) Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *   the documentation and/or other materials provided with the distribution.
15  *
16  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $	 */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <netinet/sctp_os.h>
39 #include <sys/proc.h>
40 #include <netinet/sctp_var.h>
41 #include <netinet/sctp_sysctl.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctp_pcb.h>
44 #include <netinet/sctputil.h>
45 #include <netinet/sctp_output.h>
46 #include <netinet/sctp_uio.h>
47 #include <netinet/sctputil.h>
48 #include <netinet/sctp_auth.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_asconf.h>
51 #include <netinet/sctp_indata.h>
52 #include <netinet/sctp_bsd_addr.h>
53 #include <netinet/sctp_input.h>
54 #include <netinet/sctp_crc32.h>
55 #include <netinet/udp.h>
56 #include <machine/in_cksum.h>
57 
58 
59 
60 #define SCTP_MAX_GAPS_INARRAY 4
61 struct sack_track {
62 	uint8_t right_edge;	/* mergable on the right edge */
63 	uint8_t left_edge;	/* mergable on the left edge */
64 	uint8_t num_entries;
65 	uint8_t spare;
66 	struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
67 };
68 
69 struct sack_track sack_array[256] = {
70 	{0, 0, 0, 0,		/* 0x00 */
71 		{{0, 0},
72 		{0, 0},
73 		{0, 0},
74 		{0, 0}
75 		}
76 	},
77 	{1, 0, 1, 0,		/* 0x01 */
78 		{{0, 0},
79 		{0, 0},
80 		{0, 0},
81 		{0, 0}
82 		}
83 	},
84 	{0, 0, 1, 0,		/* 0x02 */
85 		{{1, 1},
86 		{0, 0},
87 		{0, 0},
88 		{0, 0}
89 		}
90 	},
91 	{1, 0, 1, 0,		/* 0x03 */
92 		{{0, 1},
93 		{0, 0},
94 		{0, 0},
95 		{0, 0}
96 		}
97 	},
98 	{0, 0, 1, 0,		/* 0x04 */
99 		{{2, 2},
100 		{0, 0},
101 		{0, 0},
102 		{0, 0}
103 		}
104 	},
105 	{1, 0, 2, 0,		/* 0x05 */
106 		{{0, 0},
107 		{2, 2},
108 		{0, 0},
109 		{0, 0}
110 		}
111 	},
112 	{0, 0, 1, 0,		/* 0x06 */
113 		{{1, 2},
114 		{0, 0},
115 		{0, 0},
116 		{0, 0}
117 		}
118 	},
119 	{1, 0, 1, 0,		/* 0x07 */
120 		{{0, 2},
121 		{0, 0},
122 		{0, 0},
123 		{0, 0}
124 		}
125 	},
126 	{0, 0, 1, 0,		/* 0x08 */
127 		{{3, 3},
128 		{0, 0},
129 		{0, 0},
130 		{0, 0}
131 		}
132 	},
133 	{1, 0, 2, 0,		/* 0x09 */
134 		{{0, 0},
135 		{3, 3},
136 		{0, 0},
137 		{0, 0}
138 		}
139 	},
140 	{0, 0, 2, 0,		/* 0x0a */
141 		{{1, 1},
142 		{3, 3},
143 		{0, 0},
144 		{0, 0}
145 		}
146 	},
147 	{1, 0, 2, 0,		/* 0x0b */
148 		{{0, 1},
149 		{3, 3},
150 		{0, 0},
151 		{0, 0}
152 		}
153 	},
154 	{0, 0, 1, 0,		/* 0x0c */
155 		{{2, 3},
156 		{0, 0},
157 		{0, 0},
158 		{0, 0}
159 		}
160 	},
161 	{1, 0, 2, 0,		/* 0x0d */
162 		{{0, 0},
163 		{2, 3},
164 		{0, 0},
165 		{0, 0}
166 		}
167 	},
168 	{0, 0, 1, 0,		/* 0x0e */
169 		{{1, 3},
170 		{0, 0},
171 		{0, 0},
172 		{0, 0}
173 		}
174 	},
175 	{1, 0, 1, 0,		/* 0x0f */
176 		{{0, 3},
177 		{0, 0},
178 		{0, 0},
179 		{0, 0}
180 		}
181 	},
182 	{0, 0, 1, 0,		/* 0x10 */
183 		{{4, 4},
184 		{0, 0},
185 		{0, 0},
186 		{0, 0}
187 		}
188 	},
189 	{1, 0, 2, 0,		/* 0x11 */
190 		{{0, 0},
191 		{4, 4},
192 		{0, 0},
193 		{0, 0}
194 		}
195 	},
196 	{0, 0, 2, 0,		/* 0x12 */
197 		{{1, 1},
198 		{4, 4},
199 		{0, 0},
200 		{0, 0}
201 		}
202 	},
203 	{1, 0, 2, 0,		/* 0x13 */
204 		{{0, 1},
205 		{4, 4},
206 		{0, 0},
207 		{0, 0}
208 		}
209 	},
210 	{0, 0, 2, 0,		/* 0x14 */
211 		{{2, 2},
212 		{4, 4},
213 		{0, 0},
214 		{0, 0}
215 		}
216 	},
217 	{1, 0, 3, 0,		/* 0x15 */
218 		{{0, 0},
219 		{2, 2},
220 		{4, 4},
221 		{0, 0}
222 		}
223 	},
224 	{0, 0, 2, 0,		/* 0x16 */
225 		{{1, 2},
226 		{4, 4},
227 		{0, 0},
228 		{0, 0}
229 		}
230 	},
231 	{1, 0, 2, 0,		/* 0x17 */
232 		{{0, 2},
233 		{4, 4},
234 		{0, 0},
235 		{0, 0}
236 		}
237 	},
238 	{0, 0, 1, 0,		/* 0x18 */
239 		{{3, 4},
240 		{0, 0},
241 		{0, 0},
242 		{0, 0}
243 		}
244 	},
245 	{1, 0, 2, 0,		/* 0x19 */
246 		{{0, 0},
247 		{3, 4},
248 		{0, 0},
249 		{0, 0}
250 		}
251 	},
252 	{0, 0, 2, 0,		/* 0x1a */
253 		{{1, 1},
254 		{3, 4},
255 		{0, 0},
256 		{0, 0}
257 		}
258 	},
259 	{1, 0, 2, 0,		/* 0x1b */
260 		{{0, 1},
261 		{3, 4},
262 		{0, 0},
263 		{0, 0}
264 		}
265 	},
266 	{0, 0, 1, 0,		/* 0x1c */
267 		{{2, 4},
268 		{0, 0},
269 		{0, 0},
270 		{0, 0}
271 		}
272 	},
273 	{1, 0, 2, 0,		/* 0x1d */
274 		{{0, 0},
275 		{2, 4},
276 		{0, 0},
277 		{0, 0}
278 		}
279 	},
280 	{0, 0, 1, 0,		/* 0x1e */
281 		{{1, 4},
282 		{0, 0},
283 		{0, 0},
284 		{0, 0}
285 		}
286 	},
287 	{1, 0, 1, 0,		/* 0x1f */
288 		{{0, 4},
289 		{0, 0},
290 		{0, 0},
291 		{0, 0}
292 		}
293 	},
294 	{0, 0, 1, 0,		/* 0x20 */
295 		{{5, 5},
296 		{0, 0},
297 		{0, 0},
298 		{0, 0}
299 		}
300 	},
301 	{1, 0, 2, 0,		/* 0x21 */
302 		{{0, 0},
303 		{5, 5},
304 		{0, 0},
305 		{0, 0}
306 		}
307 	},
308 	{0, 0, 2, 0,		/* 0x22 */
309 		{{1, 1},
310 		{5, 5},
311 		{0, 0},
312 		{0, 0}
313 		}
314 	},
315 	{1, 0, 2, 0,		/* 0x23 */
316 		{{0, 1},
317 		{5, 5},
318 		{0, 0},
319 		{0, 0}
320 		}
321 	},
322 	{0, 0, 2, 0,		/* 0x24 */
323 		{{2, 2},
324 		{5, 5},
325 		{0, 0},
326 		{0, 0}
327 		}
328 	},
329 	{1, 0, 3, 0,		/* 0x25 */
330 		{{0, 0},
331 		{2, 2},
332 		{5, 5},
333 		{0, 0}
334 		}
335 	},
336 	{0, 0, 2, 0,		/* 0x26 */
337 		{{1, 2},
338 		{5, 5},
339 		{0, 0},
340 		{0, 0}
341 		}
342 	},
343 	{1, 0, 2, 0,		/* 0x27 */
344 		{{0, 2},
345 		{5, 5},
346 		{0, 0},
347 		{0, 0}
348 		}
349 	},
350 	{0, 0, 2, 0,		/* 0x28 */
351 		{{3, 3},
352 		{5, 5},
353 		{0, 0},
354 		{0, 0}
355 		}
356 	},
357 	{1, 0, 3, 0,		/* 0x29 */
358 		{{0, 0},
359 		{3, 3},
360 		{5, 5},
361 		{0, 0}
362 		}
363 	},
364 	{0, 0, 3, 0,		/* 0x2a */
365 		{{1, 1},
366 		{3, 3},
367 		{5, 5},
368 		{0, 0}
369 		}
370 	},
371 	{1, 0, 3, 0,		/* 0x2b */
372 		{{0, 1},
373 		{3, 3},
374 		{5, 5},
375 		{0, 0}
376 		}
377 	},
378 	{0, 0, 2, 0,		/* 0x2c */
379 		{{2, 3},
380 		{5, 5},
381 		{0, 0},
382 		{0, 0}
383 		}
384 	},
385 	{1, 0, 3, 0,		/* 0x2d */
386 		{{0, 0},
387 		{2, 3},
388 		{5, 5},
389 		{0, 0}
390 		}
391 	},
392 	{0, 0, 2, 0,		/* 0x2e */
393 		{{1, 3},
394 		{5, 5},
395 		{0, 0},
396 		{0, 0}
397 		}
398 	},
399 	{1, 0, 2, 0,		/* 0x2f */
400 		{{0, 3},
401 		{5, 5},
402 		{0, 0},
403 		{0, 0}
404 		}
405 	},
406 	{0, 0, 1, 0,		/* 0x30 */
407 		{{4, 5},
408 		{0, 0},
409 		{0, 0},
410 		{0, 0}
411 		}
412 	},
413 	{1, 0, 2, 0,		/* 0x31 */
414 		{{0, 0},
415 		{4, 5},
416 		{0, 0},
417 		{0, 0}
418 		}
419 	},
420 	{0, 0, 2, 0,		/* 0x32 */
421 		{{1, 1},
422 		{4, 5},
423 		{0, 0},
424 		{0, 0}
425 		}
426 	},
427 	{1, 0, 2, 0,		/* 0x33 */
428 		{{0, 1},
429 		{4, 5},
430 		{0, 0},
431 		{0, 0}
432 		}
433 	},
434 	{0, 0, 2, 0,		/* 0x34 */
435 		{{2, 2},
436 		{4, 5},
437 		{0, 0},
438 		{0, 0}
439 		}
440 	},
441 	{1, 0, 3, 0,		/* 0x35 */
442 		{{0, 0},
443 		{2, 2},
444 		{4, 5},
445 		{0, 0}
446 		}
447 	},
448 	{0, 0, 2, 0,		/* 0x36 */
449 		{{1, 2},
450 		{4, 5},
451 		{0, 0},
452 		{0, 0}
453 		}
454 	},
455 	{1, 0, 2, 0,		/* 0x37 */
456 		{{0, 2},
457 		{4, 5},
458 		{0, 0},
459 		{0, 0}
460 		}
461 	},
462 	{0, 0, 1, 0,		/* 0x38 */
463 		{{3, 5},
464 		{0, 0},
465 		{0, 0},
466 		{0, 0}
467 		}
468 	},
469 	{1, 0, 2, 0,		/* 0x39 */
470 		{{0, 0},
471 		{3, 5},
472 		{0, 0},
473 		{0, 0}
474 		}
475 	},
476 	{0, 0, 2, 0,		/* 0x3a */
477 		{{1, 1},
478 		{3, 5},
479 		{0, 0},
480 		{0, 0}
481 		}
482 	},
483 	{1, 0, 2, 0,		/* 0x3b */
484 		{{0, 1},
485 		{3, 5},
486 		{0, 0},
487 		{0, 0}
488 		}
489 	},
490 	{0, 0, 1, 0,		/* 0x3c */
491 		{{2, 5},
492 		{0, 0},
493 		{0, 0},
494 		{0, 0}
495 		}
496 	},
497 	{1, 0, 2, 0,		/* 0x3d */
498 		{{0, 0},
499 		{2, 5},
500 		{0, 0},
501 		{0, 0}
502 		}
503 	},
504 	{0, 0, 1, 0,		/* 0x3e */
505 		{{1, 5},
506 		{0, 0},
507 		{0, 0},
508 		{0, 0}
509 		}
510 	},
511 	{1, 0, 1, 0,		/* 0x3f */
512 		{{0, 5},
513 		{0, 0},
514 		{0, 0},
515 		{0, 0}
516 		}
517 	},
518 	{0, 0, 1, 0,		/* 0x40 */
519 		{{6, 6},
520 		{0, 0},
521 		{0, 0},
522 		{0, 0}
523 		}
524 	},
525 	{1, 0, 2, 0,		/* 0x41 */
526 		{{0, 0},
527 		{6, 6},
528 		{0, 0},
529 		{0, 0}
530 		}
531 	},
532 	{0, 0, 2, 0,		/* 0x42 */
533 		{{1, 1},
534 		{6, 6},
535 		{0, 0},
536 		{0, 0}
537 		}
538 	},
539 	{1, 0, 2, 0,		/* 0x43 */
540 		{{0, 1},
541 		{6, 6},
542 		{0, 0},
543 		{0, 0}
544 		}
545 	},
546 	{0, 0, 2, 0,		/* 0x44 */
547 		{{2, 2},
548 		{6, 6},
549 		{0, 0},
550 		{0, 0}
551 		}
552 	},
553 	{1, 0, 3, 0,		/* 0x45 */
554 		{{0, 0},
555 		{2, 2},
556 		{6, 6},
557 		{0, 0}
558 		}
559 	},
560 	{0, 0, 2, 0,		/* 0x46 */
561 		{{1, 2},
562 		{6, 6},
563 		{0, 0},
564 		{0, 0}
565 		}
566 	},
567 	{1, 0, 2, 0,		/* 0x47 */
568 		{{0, 2},
569 		{6, 6},
570 		{0, 0},
571 		{0, 0}
572 		}
573 	},
574 	{0, 0, 2, 0,		/* 0x48 */
575 		{{3, 3},
576 		{6, 6},
577 		{0, 0},
578 		{0, 0}
579 		}
580 	},
581 	{1, 0, 3, 0,		/* 0x49 */
582 		{{0, 0},
583 		{3, 3},
584 		{6, 6},
585 		{0, 0}
586 		}
587 	},
588 	{0, 0, 3, 0,		/* 0x4a */
589 		{{1, 1},
590 		{3, 3},
591 		{6, 6},
592 		{0, 0}
593 		}
594 	},
595 	{1, 0, 3, 0,		/* 0x4b */
596 		{{0, 1},
597 		{3, 3},
598 		{6, 6},
599 		{0, 0}
600 		}
601 	},
602 	{0, 0, 2, 0,		/* 0x4c */
603 		{{2, 3},
604 		{6, 6},
605 		{0, 0},
606 		{0, 0}
607 		}
608 	},
609 	{1, 0, 3, 0,		/* 0x4d */
610 		{{0, 0},
611 		{2, 3},
612 		{6, 6},
613 		{0, 0}
614 		}
615 	},
616 	{0, 0, 2, 0,		/* 0x4e */
617 		{{1, 3},
618 		{6, 6},
619 		{0, 0},
620 		{0, 0}
621 		}
622 	},
623 	{1, 0, 2, 0,		/* 0x4f */
624 		{{0, 3},
625 		{6, 6},
626 		{0, 0},
627 		{0, 0}
628 		}
629 	},
630 	{0, 0, 2, 0,		/* 0x50 */
631 		{{4, 4},
632 		{6, 6},
633 		{0, 0},
634 		{0, 0}
635 		}
636 	},
637 	{1, 0, 3, 0,		/* 0x51 */
638 		{{0, 0},
639 		{4, 4},
640 		{6, 6},
641 		{0, 0}
642 		}
643 	},
644 	{0, 0, 3, 0,		/* 0x52 */
645 		{{1, 1},
646 		{4, 4},
647 		{6, 6},
648 		{0, 0}
649 		}
650 	},
651 	{1, 0, 3, 0,		/* 0x53 */
652 		{{0, 1},
653 		{4, 4},
654 		{6, 6},
655 		{0, 0}
656 		}
657 	},
658 	{0, 0, 3, 0,		/* 0x54 */
659 		{{2, 2},
660 		{4, 4},
661 		{6, 6},
662 		{0, 0}
663 		}
664 	},
665 	{1, 0, 4, 0,		/* 0x55 */
666 		{{0, 0},
667 		{2, 2},
668 		{4, 4},
669 		{6, 6}
670 		}
671 	},
672 	{0, 0, 3, 0,		/* 0x56 */
673 		{{1, 2},
674 		{4, 4},
675 		{6, 6},
676 		{0, 0}
677 		}
678 	},
679 	{1, 0, 3, 0,		/* 0x57 */
680 		{{0, 2},
681 		{4, 4},
682 		{6, 6},
683 		{0, 0}
684 		}
685 	},
686 	{0, 0, 2, 0,		/* 0x58 */
687 		{{3, 4},
688 		{6, 6},
689 		{0, 0},
690 		{0, 0}
691 		}
692 	},
693 	{1, 0, 3, 0,		/* 0x59 */
694 		{{0, 0},
695 		{3, 4},
696 		{6, 6},
697 		{0, 0}
698 		}
699 	},
700 	{0, 0, 3, 0,		/* 0x5a */
701 		{{1, 1},
702 		{3, 4},
703 		{6, 6},
704 		{0, 0}
705 		}
706 	},
707 	{1, 0, 3, 0,		/* 0x5b */
708 		{{0, 1},
709 		{3, 4},
710 		{6, 6},
711 		{0, 0}
712 		}
713 	},
714 	{0, 0, 2, 0,		/* 0x5c */
715 		{{2, 4},
716 		{6, 6},
717 		{0, 0},
718 		{0, 0}
719 		}
720 	},
721 	{1, 0, 3, 0,		/* 0x5d */
722 		{{0, 0},
723 		{2, 4},
724 		{6, 6},
725 		{0, 0}
726 		}
727 	},
728 	{0, 0, 2, 0,		/* 0x5e */
729 		{{1, 4},
730 		{6, 6},
731 		{0, 0},
732 		{0, 0}
733 		}
734 	},
735 	{1, 0, 2, 0,		/* 0x5f */
736 		{{0, 4},
737 		{6, 6},
738 		{0, 0},
739 		{0, 0}
740 		}
741 	},
742 	{0, 0, 1, 0,		/* 0x60 */
743 		{{5, 6},
744 		{0, 0},
745 		{0, 0},
746 		{0, 0}
747 		}
748 	},
749 	{1, 0, 2, 0,		/* 0x61 */
750 		{{0, 0},
751 		{5, 6},
752 		{0, 0},
753 		{0, 0}
754 		}
755 	},
756 	{0, 0, 2, 0,		/* 0x62 */
757 		{{1, 1},
758 		{5, 6},
759 		{0, 0},
760 		{0, 0}
761 		}
762 	},
763 	{1, 0, 2, 0,		/* 0x63 */
764 		{{0, 1},
765 		{5, 6},
766 		{0, 0},
767 		{0, 0}
768 		}
769 	},
770 	{0, 0, 2, 0,		/* 0x64 */
771 		{{2, 2},
772 		{5, 6},
773 		{0, 0},
774 		{0, 0}
775 		}
776 	},
777 	{1, 0, 3, 0,		/* 0x65 */
778 		{{0, 0},
779 		{2, 2},
780 		{5, 6},
781 		{0, 0}
782 		}
783 	},
784 	{0, 0, 2, 0,		/* 0x66 */
785 		{{1, 2},
786 		{5, 6},
787 		{0, 0},
788 		{0, 0}
789 		}
790 	},
791 	{1, 0, 2, 0,		/* 0x67 */
792 		{{0, 2},
793 		{5, 6},
794 		{0, 0},
795 		{0, 0}
796 		}
797 	},
798 	{0, 0, 2, 0,		/* 0x68 */
799 		{{3, 3},
800 		{5, 6},
801 		{0, 0},
802 		{0, 0}
803 		}
804 	},
805 	{1, 0, 3, 0,		/* 0x69 */
806 		{{0, 0},
807 		{3, 3},
808 		{5, 6},
809 		{0, 0}
810 		}
811 	},
812 	{0, 0, 3, 0,		/* 0x6a */
813 		{{1, 1},
814 		{3, 3},
815 		{5, 6},
816 		{0, 0}
817 		}
818 	},
819 	{1, 0, 3, 0,		/* 0x6b */
820 		{{0, 1},
821 		{3, 3},
822 		{5, 6},
823 		{0, 0}
824 		}
825 	},
826 	{0, 0, 2, 0,		/* 0x6c */
827 		{{2, 3},
828 		{5, 6},
829 		{0, 0},
830 		{0, 0}
831 		}
832 	},
833 	{1, 0, 3, 0,		/* 0x6d */
834 		{{0, 0},
835 		{2, 3},
836 		{5, 6},
837 		{0, 0}
838 		}
839 	},
840 	{0, 0, 2, 0,		/* 0x6e */
841 		{{1, 3},
842 		{5, 6},
843 		{0, 0},
844 		{0, 0}
845 		}
846 	},
847 	{1, 0, 2, 0,		/* 0x6f */
848 		{{0, 3},
849 		{5, 6},
850 		{0, 0},
851 		{0, 0}
852 		}
853 	},
854 	{0, 0, 1, 0,		/* 0x70 */
855 		{{4, 6},
856 		{0, 0},
857 		{0, 0},
858 		{0, 0}
859 		}
860 	},
861 	{1, 0, 2, 0,		/* 0x71 */
862 		{{0, 0},
863 		{4, 6},
864 		{0, 0},
865 		{0, 0}
866 		}
867 	},
868 	{0, 0, 2, 0,		/* 0x72 */
869 		{{1, 1},
870 		{4, 6},
871 		{0, 0},
872 		{0, 0}
873 		}
874 	},
875 	{1, 0, 2, 0,		/* 0x73 */
876 		{{0, 1},
877 		{4, 6},
878 		{0, 0},
879 		{0, 0}
880 		}
881 	},
882 	{0, 0, 2, 0,		/* 0x74 */
883 		{{2, 2},
884 		{4, 6},
885 		{0, 0},
886 		{0, 0}
887 		}
888 	},
889 	{1, 0, 3, 0,		/* 0x75 */
890 		{{0, 0},
891 		{2, 2},
892 		{4, 6},
893 		{0, 0}
894 		}
895 	},
896 	{0, 0, 2, 0,		/* 0x76 */
897 		{{1, 2},
898 		{4, 6},
899 		{0, 0},
900 		{0, 0}
901 		}
902 	},
903 	{1, 0, 2, 0,		/* 0x77 */
904 		{{0, 2},
905 		{4, 6},
906 		{0, 0},
907 		{0, 0}
908 		}
909 	},
910 	{0, 0, 1, 0,		/* 0x78 */
911 		{{3, 6},
912 		{0, 0},
913 		{0, 0},
914 		{0, 0}
915 		}
916 	},
917 	{1, 0, 2, 0,		/* 0x79 */
918 		{{0, 0},
919 		{3, 6},
920 		{0, 0},
921 		{0, 0}
922 		}
923 	},
924 	{0, 0, 2, 0,		/* 0x7a */
925 		{{1, 1},
926 		{3, 6},
927 		{0, 0},
928 		{0, 0}
929 		}
930 	},
931 	{1, 0, 2, 0,		/* 0x7b */
932 		{{0, 1},
933 		{3, 6},
934 		{0, 0},
935 		{0, 0}
936 		}
937 	},
938 	{0, 0, 1, 0,		/* 0x7c */
939 		{{2, 6},
940 		{0, 0},
941 		{0, 0},
942 		{0, 0}
943 		}
944 	},
945 	{1, 0, 2, 0,		/* 0x7d */
946 		{{0, 0},
947 		{2, 6},
948 		{0, 0},
949 		{0, 0}
950 		}
951 	},
952 	{0, 0, 1, 0,		/* 0x7e */
953 		{{1, 6},
954 		{0, 0},
955 		{0, 0},
956 		{0, 0}
957 		}
958 	},
959 	{1, 0, 1, 0,		/* 0x7f */
960 		{{0, 6},
961 		{0, 0},
962 		{0, 0},
963 		{0, 0}
964 		}
965 	},
966 	{0, 1, 1, 0,		/* 0x80 */
967 		{{7, 7},
968 		{0, 0},
969 		{0, 0},
970 		{0, 0}
971 		}
972 	},
973 	{1, 1, 2, 0,		/* 0x81 */
974 		{{0, 0},
975 		{7, 7},
976 		{0, 0},
977 		{0, 0}
978 		}
979 	},
980 	{0, 1, 2, 0,		/* 0x82 */
981 		{{1, 1},
982 		{7, 7},
983 		{0, 0},
984 		{0, 0}
985 		}
986 	},
987 	{1, 1, 2, 0,		/* 0x83 */
988 		{{0, 1},
989 		{7, 7},
990 		{0, 0},
991 		{0, 0}
992 		}
993 	},
994 	{0, 1, 2, 0,		/* 0x84 */
995 		{{2, 2},
996 		{7, 7},
997 		{0, 0},
998 		{0, 0}
999 		}
1000 	},
1001 	{1, 1, 3, 0,		/* 0x85 */
1002 		{{0, 0},
1003 		{2, 2},
1004 		{7, 7},
1005 		{0, 0}
1006 		}
1007 	},
1008 	{0, 1, 2, 0,		/* 0x86 */
1009 		{{1, 2},
1010 		{7, 7},
1011 		{0, 0},
1012 		{0, 0}
1013 		}
1014 	},
1015 	{1, 1, 2, 0,		/* 0x87 */
1016 		{{0, 2},
1017 		{7, 7},
1018 		{0, 0},
1019 		{0, 0}
1020 		}
1021 	},
1022 	{0, 1, 2, 0,		/* 0x88 */
1023 		{{3, 3},
1024 		{7, 7},
1025 		{0, 0},
1026 		{0, 0}
1027 		}
1028 	},
1029 	{1, 1, 3, 0,		/* 0x89 */
1030 		{{0, 0},
1031 		{3, 3},
1032 		{7, 7},
1033 		{0, 0}
1034 		}
1035 	},
1036 	{0, 1, 3, 0,		/* 0x8a */
1037 		{{1, 1},
1038 		{3, 3},
1039 		{7, 7},
1040 		{0, 0}
1041 		}
1042 	},
1043 	{1, 1, 3, 0,		/* 0x8b */
1044 		{{0, 1},
1045 		{3, 3},
1046 		{7, 7},
1047 		{0, 0}
1048 		}
1049 	},
1050 	{0, 1, 2, 0,		/* 0x8c */
1051 		{{2, 3},
1052 		{7, 7},
1053 		{0, 0},
1054 		{0, 0}
1055 		}
1056 	},
1057 	{1, 1, 3, 0,		/* 0x8d */
1058 		{{0, 0},
1059 		{2, 3},
1060 		{7, 7},
1061 		{0, 0}
1062 		}
1063 	},
1064 	{0, 1, 2, 0,		/* 0x8e */
1065 		{{1, 3},
1066 		{7, 7},
1067 		{0, 0},
1068 		{0, 0}
1069 		}
1070 	},
1071 	{1, 1, 2, 0,		/* 0x8f */
1072 		{{0, 3},
1073 		{7, 7},
1074 		{0, 0},
1075 		{0, 0}
1076 		}
1077 	},
1078 	{0, 1, 2, 0,		/* 0x90 */
1079 		{{4, 4},
1080 		{7, 7},
1081 		{0, 0},
1082 		{0, 0}
1083 		}
1084 	},
1085 	{1, 1, 3, 0,		/* 0x91 */
1086 		{{0, 0},
1087 		{4, 4},
1088 		{7, 7},
1089 		{0, 0}
1090 		}
1091 	},
1092 	{0, 1, 3, 0,		/* 0x92 */
1093 		{{1, 1},
1094 		{4, 4},
1095 		{7, 7},
1096 		{0, 0}
1097 		}
1098 	},
1099 	{1, 1, 3, 0,		/* 0x93 */
1100 		{{0, 1},
1101 		{4, 4},
1102 		{7, 7},
1103 		{0, 0}
1104 		}
1105 	},
1106 	{0, 1, 3, 0,		/* 0x94 */
1107 		{{2, 2},
1108 		{4, 4},
1109 		{7, 7},
1110 		{0, 0}
1111 		}
1112 	},
1113 	{1, 1, 4, 0,		/* 0x95 */
1114 		{{0, 0},
1115 		{2, 2},
1116 		{4, 4},
1117 		{7, 7}
1118 		}
1119 	},
1120 	{0, 1, 3, 0,		/* 0x96 */
1121 		{{1, 2},
1122 		{4, 4},
1123 		{7, 7},
1124 		{0, 0}
1125 		}
1126 	},
1127 	{1, 1, 3, 0,		/* 0x97 */
1128 		{{0, 2},
1129 		{4, 4},
1130 		{7, 7},
1131 		{0, 0}
1132 		}
1133 	},
1134 	{0, 1, 2, 0,		/* 0x98 */
1135 		{{3, 4},
1136 		{7, 7},
1137 		{0, 0},
1138 		{0, 0}
1139 		}
1140 	},
1141 	{1, 1, 3, 0,		/* 0x99 */
1142 		{{0, 0},
1143 		{3, 4},
1144 		{7, 7},
1145 		{0, 0}
1146 		}
1147 	},
1148 	{0, 1, 3, 0,		/* 0x9a */
1149 		{{1, 1},
1150 		{3, 4},
1151 		{7, 7},
1152 		{0, 0}
1153 		}
1154 	},
1155 	{1, 1, 3, 0,		/* 0x9b */
1156 		{{0, 1},
1157 		{3, 4},
1158 		{7, 7},
1159 		{0, 0}
1160 		}
1161 	},
1162 	{0, 1, 2, 0,		/* 0x9c */
1163 		{{2, 4},
1164 		{7, 7},
1165 		{0, 0},
1166 		{0, 0}
1167 		}
1168 	},
1169 	{1, 1, 3, 0,		/* 0x9d */
1170 		{{0, 0},
1171 		{2, 4},
1172 		{7, 7},
1173 		{0, 0}
1174 		}
1175 	},
1176 	{0, 1, 2, 0,		/* 0x9e */
1177 		{{1, 4},
1178 		{7, 7},
1179 		{0, 0},
1180 		{0, 0}
1181 		}
1182 	},
1183 	{1, 1, 2, 0,		/* 0x9f */
1184 		{{0, 4},
1185 		{7, 7},
1186 		{0, 0},
1187 		{0, 0}
1188 		}
1189 	},
1190 	{0, 1, 2, 0,		/* 0xa0 */
1191 		{{5, 5},
1192 		{7, 7},
1193 		{0, 0},
1194 		{0, 0}
1195 		}
1196 	},
1197 	{1, 1, 3, 0,		/* 0xa1 */
1198 		{{0, 0},
1199 		{5, 5},
1200 		{7, 7},
1201 		{0, 0}
1202 		}
1203 	},
1204 	{0, 1, 3, 0,		/* 0xa2 */
1205 		{{1, 1},
1206 		{5, 5},
1207 		{7, 7},
1208 		{0, 0}
1209 		}
1210 	},
1211 	{1, 1, 3, 0,		/* 0xa3 */
1212 		{{0, 1},
1213 		{5, 5},
1214 		{7, 7},
1215 		{0, 0}
1216 		}
1217 	},
1218 	{0, 1, 3, 0,		/* 0xa4 */
1219 		{{2, 2},
1220 		{5, 5},
1221 		{7, 7},
1222 		{0, 0}
1223 		}
1224 	},
1225 	{1, 1, 4, 0,		/* 0xa5 */
1226 		{{0, 0},
1227 		{2, 2},
1228 		{5, 5},
1229 		{7, 7}
1230 		}
1231 	},
1232 	{0, 1, 3, 0,		/* 0xa6 */
1233 		{{1, 2},
1234 		{5, 5},
1235 		{7, 7},
1236 		{0, 0}
1237 		}
1238 	},
1239 	{1, 1, 3, 0,		/* 0xa7 */
1240 		{{0, 2},
1241 		{5, 5},
1242 		{7, 7},
1243 		{0, 0}
1244 		}
1245 	},
1246 	{0, 1, 3, 0,		/* 0xa8 */
1247 		{{3, 3},
1248 		{5, 5},
1249 		{7, 7},
1250 		{0, 0}
1251 		}
1252 	},
1253 	{1, 1, 4, 0,		/* 0xa9 */
1254 		{{0, 0},
1255 		{3, 3},
1256 		{5, 5},
1257 		{7, 7}
1258 		}
1259 	},
1260 	{0, 1, 4, 0,		/* 0xaa */
1261 		{{1, 1},
1262 		{3, 3},
1263 		{5, 5},
1264 		{7, 7}
1265 		}
1266 	},
1267 	{1, 1, 4, 0,		/* 0xab */
1268 		{{0, 1},
1269 		{3, 3},
1270 		{5, 5},
1271 		{7, 7}
1272 		}
1273 	},
1274 	{0, 1, 3, 0,		/* 0xac */
1275 		{{2, 3},
1276 		{5, 5},
1277 		{7, 7},
1278 		{0, 0}
1279 		}
1280 	},
1281 	{1, 1, 4, 0,		/* 0xad */
1282 		{{0, 0},
1283 		{2, 3},
1284 		{5, 5},
1285 		{7, 7}
1286 		}
1287 	},
1288 	{0, 1, 3, 0,		/* 0xae */
1289 		{{1, 3},
1290 		{5, 5},
1291 		{7, 7},
1292 		{0, 0}
1293 		}
1294 	},
1295 	{1, 1, 3, 0,		/* 0xaf */
1296 		{{0, 3},
1297 		{5, 5},
1298 		{7, 7},
1299 		{0, 0}
1300 		}
1301 	},
1302 	{0, 1, 2, 0,		/* 0xb0 */
1303 		{{4, 5},
1304 		{7, 7},
1305 		{0, 0},
1306 		{0, 0}
1307 		}
1308 	},
1309 	{1, 1, 3, 0,		/* 0xb1 */
1310 		{{0, 0},
1311 		{4, 5},
1312 		{7, 7},
1313 		{0, 0}
1314 		}
1315 	},
1316 	{0, 1, 3, 0,		/* 0xb2 */
1317 		{{1, 1},
1318 		{4, 5},
1319 		{7, 7},
1320 		{0, 0}
1321 		}
1322 	},
1323 	{1, 1, 3, 0,		/* 0xb3 */
1324 		{{0, 1},
1325 		{4, 5},
1326 		{7, 7},
1327 		{0, 0}
1328 		}
1329 	},
1330 	{0, 1, 3, 0,		/* 0xb4 */
1331 		{{2, 2},
1332 		{4, 5},
1333 		{7, 7},
1334 		{0, 0}
1335 		}
1336 	},
1337 	{1, 1, 4, 0,		/* 0xb5 */
1338 		{{0, 0},
1339 		{2, 2},
1340 		{4, 5},
1341 		{7, 7}
1342 		}
1343 	},
1344 	{0, 1, 3, 0,		/* 0xb6 */
1345 		{{1, 2},
1346 		{4, 5},
1347 		{7, 7},
1348 		{0, 0}
1349 		}
1350 	},
1351 	{1, 1, 3, 0,		/* 0xb7 */
1352 		{{0, 2},
1353 		{4, 5},
1354 		{7, 7},
1355 		{0, 0}
1356 		}
1357 	},
1358 	{0, 1, 2, 0,		/* 0xb8 */
1359 		{{3, 5},
1360 		{7, 7},
1361 		{0, 0},
1362 		{0, 0}
1363 		}
1364 	},
1365 	{1, 1, 3, 0,		/* 0xb9 */
1366 		{{0, 0},
1367 		{3, 5},
1368 		{7, 7},
1369 		{0, 0}
1370 		}
1371 	},
1372 	{0, 1, 3, 0,		/* 0xba */
1373 		{{1, 1},
1374 		{3, 5},
1375 		{7, 7},
1376 		{0, 0}
1377 		}
1378 	},
1379 	{1, 1, 3, 0,		/* 0xbb */
1380 		{{0, 1},
1381 		{3, 5},
1382 		{7, 7},
1383 		{0, 0}
1384 		}
1385 	},
1386 	{0, 1, 2, 0,		/* 0xbc */
1387 		{{2, 5},
1388 		{7, 7},
1389 		{0, 0},
1390 		{0, 0}
1391 		}
1392 	},
1393 	{1, 1, 3, 0,		/* 0xbd */
1394 		{{0, 0},
1395 		{2, 5},
1396 		{7, 7},
1397 		{0, 0}
1398 		}
1399 	},
1400 	{0, 1, 2, 0,		/* 0xbe */
1401 		{{1, 5},
1402 		{7, 7},
1403 		{0, 0},
1404 		{0, 0}
1405 		}
1406 	},
1407 	{1, 1, 2, 0,		/* 0xbf */
1408 		{{0, 5},
1409 		{7, 7},
1410 		{0, 0},
1411 		{0, 0}
1412 		}
1413 	},
1414 	{0, 1, 1, 0,		/* 0xc0 */
1415 		{{6, 7},
1416 		{0, 0},
1417 		{0, 0},
1418 		{0, 0}
1419 		}
1420 	},
1421 	{1, 1, 2, 0,		/* 0xc1 */
1422 		{{0, 0},
1423 		{6, 7},
1424 		{0, 0},
1425 		{0, 0}
1426 		}
1427 	},
1428 	{0, 1, 2, 0,		/* 0xc2 */
1429 		{{1, 1},
1430 		{6, 7},
1431 		{0, 0},
1432 		{0, 0}
1433 		}
1434 	},
1435 	{1, 1, 2, 0,		/* 0xc3 */
1436 		{{0, 1},
1437 		{6, 7},
1438 		{0, 0},
1439 		{0, 0}
1440 		}
1441 	},
1442 	{0, 1, 2, 0,		/* 0xc4 */
1443 		{{2, 2},
1444 		{6, 7},
1445 		{0, 0},
1446 		{0, 0}
1447 		}
1448 	},
1449 	{1, 1, 3, 0,		/* 0xc5 */
1450 		{{0, 0},
1451 		{2, 2},
1452 		{6, 7},
1453 		{0, 0}
1454 		}
1455 	},
1456 	{0, 1, 2, 0,		/* 0xc6 */
1457 		{{1, 2},
1458 		{6, 7},
1459 		{0, 0},
1460 		{0, 0}
1461 		}
1462 	},
1463 	{1, 1, 2, 0,		/* 0xc7 */
1464 		{{0, 2},
1465 		{6, 7},
1466 		{0, 0},
1467 		{0, 0}
1468 		}
1469 	},
1470 	{0, 1, 2, 0,		/* 0xc8 */
1471 		{{3, 3},
1472 		{6, 7},
1473 		{0, 0},
1474 		{0, 0}
1475 		}
1476 	},
1477 	{1, 1, 3, 0,		/* 0xc9 */
1478 		{{0, 0},
1479 		{3, 3},
1480 		{6, 7},
1481 		{0, 0}
1482 		}
1483 	},
1484 	{0, 1, 3, 0,		/* 0xca */
1485 		{{1, 1},
1486 		{3, 3},
1487 		{6, 7},
1488 		{0, 0}
1489 		}
1490 	},
1491 	{1, 1, 3, 0,		/* 0xcb */
1492 		{{0, 1},
1493 		{3, 3},
1494 		{6, 7},
1495 		{0, 0}
1496 		}
1497 	},
1498 	{0, 1, 2, 0,		/* 0xcc */
1499 		{{2, 3},
1500 		{6, 7},
1501 		{0, 0},
1502 		{0, 0}
1503 		}
1504 	},
1505 	{1, 1, 3, 0,		/* 0xcd */
1506 		{{0, 0},
1507 		{2, 3},
1508 		{6, 7},
1509 		{0, 0}
1510 		}
1511 	},
1512 	{0, 1, 2, 0,		/* 0xce */
1513 		{{1, 3},
1514 		{6, 7},
1515 		{0, 0},
1516 		{0, 0}
1517 		}
1518 	},
1519 	{1, 1, 2, 0,		/* 0xcf */
1520 		{{0, 3},
1521 		{6, 7},
1522 		{0, 0},
1523 		{0, 0}
1524 		}
1525 	},
1526 	{0, 1, 2, 0,		/* 0xd0 */
1527 		{{4, 4},
1528 		{6, 7},
1529 		{0, 0},
1530 		{0, 0}
1531 		}
1532 	},
1533 	{1, 1, 3, 0,		/* 0xd1 */
1534 		{{0, 0},
1535 		{4, 4},
1536 		{6, 7},
1537 		{0, 0}
1538 		}
1539 	},
1540 	{0, 1, 3, 0,		/* 0xd2 */
1541 		{{1, 1},
1542 		{4, 4},
1543 		{6, 7},
1544 		{0, 0}
1545 		}
1546 	},
1547 	{1, 1, 3, 0,		/* 0xd3 */
1548 		{{0, 1},
1549 		{4, 4},
1550 		{6, 7},
1551 		{0, 0}
1552 		}
1553 	},
1554 	{0, 1, 3, 0,		/* 0xd4 */
1555 		{{2, 2},
1556 		{4, 4},
1557 		{6, 7},
1558 		{0, 0}
1559 		}
1560 	},
1561 	{1, 1, 4, 0,		/* 0xd5 */
1562 		{{0, 0},
1563 		{2, 2},
1564 		{4, 4},
1565 		{6, 7}
1566 		}
1567 	},
1568 	{0, 1, 3, 0,		/* 0xd6 */
1569 		{{1, 2},
1570 		{4, 4},
1571 		{6, 7},
1572 		{0, 0}
1573 		}
1574 	},
1575 	{1, 1, 3, 0,		/* 0xd7 */
1576 		{{0, 2},
1577 		{4, 4},
1578 		{6, 7},
1579 		{0, 0}
1580 		}
1581 	},
1582 	{0, 1, 2, 0,		/* 0xd8 */
1583 		{{3, 4},
1584 		{6, 7},
1585 		{0, 0},
1586 		{0, 0}
1587 		}
1588 	},
1589 	{1, 1, 3, 0,		/* 0xd9 */
1590 		{{0, 0},
1591 		{3, 4},
1592 		{6, 7},
1593 		{0, 0}
1594 		}
1595 	},
1596 	{0, 1, 3, 0,		/* 0xda */
1597 		{{1, 1},
1598 		{3, 4},
1599 		{6, 7},
1600 		{0, 0}
1601 		}
1602 	},
1603 	{1, 1, 3, 0,		/* 0xdb */
1604 		{{0, 1},
1605 		{3, 4},
1606 		{6, 7},
1607 		{0, 0}
1608 		}
1609 	},
1610 	{0, 1, 2, 0,		/* 0xdc */
1611 		{{2, 4},
1612 		{6, 7},
1613 		{0, 0},
1614 		{0, 0}
1615 		}
1616 	},
1617 	{1, 1, 3, 0,		/* 0xdd */
1618 		{{0, 0},
1619 		{2, 4},
1620 		{6, 7},
1621 		{0, 0}
1622 		}
1623 	},
1624 	{0, 1, 2, 0,		/* 0xde */
1625 		{{1, 4},
1626 		{6, 7},
1627 		{0, 0},
1628 		{0, 0}
1629 		}
1630 	},
1631 	{1, 1, 2, 0,		/* 0xdf */
1632 		{{0, 4},
1633 		{6, 7},
1634 		{0, 0},
1635 		{0, 0}
1636 		}
1637 	},
1638 	{0, 1, 1, 0,		/* 0xe0 */
1639 		{{5, 7},
1640 		{0, 0},
1641 		{0, 0},
1642 		{0, 0}
1643 		}
1644 	},
1645 	{1, 1, 2, 0,		/* 0xe1 */
1646 		{{0, 0},
1647 		{5, 7},
1648 		{0, 0},
1649 		{0, 0}
1650 		}
1651 	},
1652 	{0, 1, 2, 0,		/* 0xe2 */
1653 		{{1, 1},
1654 		{5, 7},
1655 		{0, 0},
1656 		{0, 0}
1657 		}
1658 	},
1659 	{1, 1, 2, 0,		/* 0xe3 */
1660 		{{0, 1},
1661 		{5, 7},
1662 		{0, 0},
1663 		{0, 0}
1664 		}
1665 	},
1666 	{0, 1, 2, 0,		/* 0xe4 */
1667 		{{2, 2},
1668 		{5, 7},
1669 		{0, 0},
1670 		{0, 0}
1671 		}
1672 	},
1673 	{1, 1, 3, 0,		/* 0xe5 */
1674 		{{0, 0},
1675 		{2, 2},
1676 		{5, 7},
1677 		{0, 0}
1678 		}
1679 	},
1680 	{0, 1, 2, 0,		/* 0xe6 */
1681 		{{1, 2},
1682 		{5, 7},
1683 		{0, 0},
1684 		{0, 0}
1685 		}
1686 	},
1687 	{1, 1, 2, 0,		/* 0xe7 */
1688 		{{0, 2},
1689 		{5, 7},
1690 		{0, 0},
1691 		{0, 0}
1692 		}
1693 	},
1694 	{0, 1, 2, 0,		/* 0xe8 */
1695 		{{3, 3},
1696 		{5, 7},
1697 		{0, 0},
1698 		{0, 0}
1699 		}
1700 	},
1701 	{1, 1, 3, 0,		/* 0xe9 */
1702 		{{0, 0},
1703 		{3, 3},
1704 		{5, 7},
1705 		{0, 0}
1706 		}
1707 	},
1708 	{0, 1, 3, 0,		/* 0xea */
1709 		{{1, 1},
1710 		{3, 3},
1711 		{5, 7},
1712 		{0, 0}
1713 		}
1714 	},
1715 	{1, 1, 3, 0,		/* 0xeb */
1716 		{{0, 1},
1717 		{3, 3},
1718 		{5, 7},
1719 		{0, 0}
1720 		}
1721 	},
1722 	{0, 1, 2, 0,		/* 0xec */
1723 		{{2, 3},
1724 		{5, 7},
1725 		{0, 0},
1726 		{0, 0}
1727 		}
1728 	},
1729 	{1, 1, 3, 0,		/* 0xed */
1730 		{{0, 0},
1731 		{2, 3},
1732 		{5, 7},
1733 		{0, 0}
1734 		}
1735 	},
1736 	{0, 1, 2, 0,		/* 0xee */
1737 		{{1, 3},
1738 		{5, 7},
1739 		{0, 0},
1740 		{0, 0}
1741 		}
1742 	},
1743 	{1, 1, 2, 0,		/* 0xef */
1744 		{{0, 3},
1745 		{5, 7},
1746 		{0, 0},
1747 		{0, 0}
1748 		}
1749 	},
1750 	{0, 1, 1, 0,		/* 0xf0 */
1751 		{{4, 7},
1752 		{0, 0},
1753 		{0, 0},
1754 		{0, 0}
1755 		}
1756 	},
1757 	{1, 1, 2, 0,		/* 0xf1 */
1758 		{{0, 0},
1759 		{4, 7},
1760 		{0, 0},
1761 		{0, 0}
1762 		}
1763 	},
1764 	{0, 1, 2, 0,		/* 0xf2 */
1765 		{{1, 1},
1766 		{4, 7},
1767 		{0, 0},
1768 		{0, 0}
1769 		}
1770 	},
1771 	{1, 1, 2, 0,		/* 0xf3 */
1772 		{{0, 1},
1773 		{4, 7},
1774 		{0, 0},
1775 		{0, 0}
1776 		}
1777 	},
1778 	{0, 1, 2, 0,		/* 0xf4 */
1779 		{{2, 2},
1780 		{4, 7},
1781 		{0, 0},
1782 		{0, 0}
1783 		}
1784 	},
1785 	{1, 1, 3, 0,		/* 0xf5 */
1786 		{{0, 0},
1787 		{2, 2},
1788 		{4, 7},
1789 		{0, 0}
1790 		}
1791 	},
1792 	{0, 1, 2, 0,		/* 0xf6 */
1793 		{{1, 2},
1794 		{4, 7},
1795 		{0, 0},
1796 		{0, 0}
1797 		}
1798 	},
1799 	{1, 1, 2, 0,		/* 0xf7 */
1800 		{{0, 2},
1801 		{4, 7},
1802 		{0, 0},
1803 		{0, 0}
1804 		}
1805 	},
1806 	{0, 1, 1, 0,		/* 0xf8 */
1807 		{{3, 7},
1808 		{0, 0},
1809 		{0, 0},
1810 		{0, 0}
1811 		}
1812 	},
1813 	{1, 1, 2, 0,		/* 0xf9 */
1814 		{{0, 0},
1815 		{3, 7},
1816 		{0, 0},
1817 		{0, 0}
1818 		}
1819 	},
1820 	{0, 1, 2, 0,		/* 0xfa */
1821 		{{1, 1},
1822 		{3, 7},
1823 		{0, 0},
1824 		{0, 0}
1825 		}
1826 	},
1827 	{1, 1, 2, 0,		/* 0xfb */
1828 		{{0, 1},
1829 		{3, 7},
1830 		{0, 0},
1831 		{0, 0}
1832 		}
1833 	},
1834 	{0, 1, 1, 0,		/* 0xfc */
1835 		{{2, 7},
1836 		{0, 0},
1837 		{0, 0},
1838 		{0, 0}
1839 		}
1840 	},
1841 	{1, 1, 2, 0,		/* 0xfd */
1842 		{{0, 0},
1843 		{2, 7},
1844 		{0, 0},
1845 		{0, 0}
1846 		}
1847 	},
1848 	{0, 1, 1, 0,		/* 0xfe */
1849 		{{1, 7},
1850 		{0, 0},
1851 		{0, 0},
1852 		{0, 0}
1853 		}
1854 	},
1855 	{1, 1, 1, 0,		/* 0xff */
1856 		{{0, 7},
1857 		{0, 0},
1858 		{0, 0},
1859 		{0, 0}
1860 		}
1861 	}
1862 };
1863 
1864 
1865 int
1866 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1867     int ipv4_addr_legal,
1868     int ipv6_addr_legal,
1869     int loopback_scope,
1870     int ipv4_local_scope,
1871     int local_scope,
1872     int site_scope,
1873     int do_update)
1874 {
1875 	if ((loopback_scope == 0) &&
1876 	    (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1877 		/*
1878 		 * skip loopback if not in scope *
1879 		 */
1880 		return (0);
1881 	}
1882 	switch (ifa->address.sa.sa_family) {
1883 #ifdef INET
1884 	case AF_INET:
1885 		if (ipv4_addr_legal) {
1886 			struct sockaddr_in *sin;
1887 
1888 			sin = (struct sockaddr_in *)&ifa->address.sin;
1889 			if (sin->sin_addr.s_addr == 0) {
1890 				/* not in scope , unspecified */
1891 				return (0);
1892 			}
1893 			if ((ipv4_local_scope == 0) &&
1894 			    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1895 				/* private address not in scope */
1896 				return (0);
1897 			}
1898 		} else {
1899 			return (0);
1900 		}
1901 		break;
1902 #endif
1903 #ifdef INET6
1904 	case AF_INET6:
1905 		if (ipv6_addr_legal) {
1906 			struct sockaddr_in6 *sin6;
1907 
1908 			/*
1909 			 * Must update the flags,  bummer, which means any
1910 			 * IFA locks must now be applied HERE <->
1911 			 */
1912 			if (do_update) {
1913 				sctp_gather_internal_ifa_flags(ifa);
1914 			}
1915 			if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1916 				return (0);
1917 			}
1918 			/* ok to use deprecated addresses? */
1919 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1920 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1921 				/* skip unspecifed addresses */
1922 				return (0);
1923 			}
1924 			if (	/* (local_scope == 0) && */
1925 			    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1926 				return (0);
1927 			}
1928 			if ((site_scope == 0) &&
1929 			    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1930 				return (0);
1931 			}
1932 		} else {
1933 			return (0);
1934 		}
1935 		break;
1936 #endif
1937 	default:
1938 		return (0);
1939 	}
1940 	return (1);
1941 }
1942 
1943 static struct mbuf *
1944 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1945 {
1946 	struct sctp_paramhdr *parmh;
1947 	struct mbuf *mret;
1948 	int len;
1949 
1950 	switch (ifa->address.sa.sa_family) {
1951 #ifdef INET
1952 	case AF_INET:
1953 		len = sizeof(struct sctp_ipv4addr_param);
1954 		break;
1955 #endif
1956 #ifdef INET6
1957 	case AF_INET6:
1958 		len = sizeof(struct sctp_ipv6addr_param);
1959 		break;
1960 #endif
1961 	default:
1962 		return (m);
1963 	}
1964 	if (M_TRAILINGSPACE(m) >= len) {
1965 		/* easy side we just drop it on the end */
1966 		parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1967 		mret = m;
1968 	} else {
1969 		/* Need more space */
1970 		mret = m;
1971 		while (SCTP_BUF_NEXT(mret) != NULL) {
1972 			mret = SCTP_BUF_NEXT(mret);
1973 		}
1974 		SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1975 		if (SCTP_BUF_NEXT(mret) == NULL) {
1976 			/* We are hosed, can't add more addresses */
1977 			return (m);
1978 		}
1979 		mret = SCTP_BUF_NEXT(mret);
1980 		parmh = mtod(mret, struct sctp_paramhdr *);
1981 	}
1982 	/* now add the parameter */
1983 	switch (ifa->address.sa.sa_family) {
1984 #ifdef INET
1985 	case AF_INET:
1986 		{
1987 			struct sctp_ipv4addr_param *ipv4p;
1988 			struct sockaddr_in *sin;
1989 
1990 			sin = (struct sockaddr_in *)&ifa->address.sin;
1991 			ipv4p = (struct sctp_ipv4addr_param *)parmh;
1992 			parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1993 			parmh->param_length = htons(len);
1994 			ipv4p->addr = sin->sin_addr.s_addr;
1995 			SCTP_BUF_LEN(mret) += len;
1996 			break;
1997 		}
1998 #endif
1999 #ifdef INET6
2000 	case AF_INET6:
2001 		{
2002 			struct sctp_ipv6addr_param *ipv6p;
2003 			struct sockaddr_in6 *sin6;
2004 
2005 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
2006 			ipv6p = (struct sctp_ipv6addr_param *)parmh;
2007 			parmh->param_type = htons(SCTP_IPV6_ADDRESS);
2008 			parmh->param_length = htons(len);
2009 			memcpy(ipv6p->addr, &sin6->sin6_addr,
2010 			    sizeof(ipv6p->addr));
2011 			/* clear embedded scope in the address */
2012 			in6_clearscope((struct in6_addr *)ipv6p->addr);
2013 			SCTP_BUF_LEN(mret) += len;
2014 			break;
2015 		}
2016 #endif
2017 	default:
2018 		return (m);
2019 	}
2020 	return (mret);
2021 }
2022 
2023 
2024 struct mbuf *
2025 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2026     struct sctp_scoping *scope,
2027     struct mbuf *m_at, int cnt_inits_to)
2028 {
2029 	struct sctp_vrf *vrf = NULL;
2030 	int cnt, limit_out = 0, total_count;
2031 	uint32_t vrf_id;
2032 
2033 	vrf_id = inp->def_vrf_id;
2034 	SCTP_IPI_ADDR_RLOCK();
2035 	vrf = sctp_find_vrf(vrf_id);
2036 	if (vrf == NULL) {
2037 		SCTP_IPI_ADDR_RUNLOCK();
2038 		return (m_at);
2039 	}
2040 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2041 		struct sctp_ifa *sctp_ifap;
2042 		struct sctp_ifn *sctp_ifnp;
2043 
2044 		cnt = cnt_inits_to;
2045 		if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2046 			limit_out = 1;
2047 			cnt = SCTP_ADDRESS_LIMIT;
2048 			goto skip_count;
2049 		}
2050 		LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2051 			if ((scope->loopback_scope == 0) &&
2052 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2053 				/*
2054 				 * Skip loopback devices if loopback_scope
2055 				 * not set
2056 				 */
2057 				continue;
2058 			}
2059 			LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2060 				if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2061 					continue;
2062 				}
2063 				if (sctp_is_address_in_scope(sctp_ifap,
2064 				    scope->ipv4_addr_legal,
2065 				    scope->ipv6_addr_legal,
2066 				    scope->loopback_scope,
2067 				    scope->ipv4_local_scope,
2068 				    scope->local_scope,
2069 				    scope->site_scope, 1) == 0) {
2070 					continue;
2071 				}
2072 				cnt++;
2073 				if (cnt > SCTP_ADDRESS_LIMIT) {
2074 					break;
2075 				}
2076 			}
2077 			if (cnt > SCTP_ADDRESS_LIMIT) {
2078 				break;
2079 			}
2080 		}
2081 skip_count:
2082 		if (cnt > 1) {
2083 			total_count = 0;
2084 			LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2085 				cnt = 0;
2086 				if ((scope->loopback_scope == 0) &&
2087 				    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2088 					/*
2089 					 * Skip loopback devices if
2090 					 * loopback_scope not set
2091 					 */
2092 					continue;
2093 				}
2094 				LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2095 					if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2096 						continue;
2097 					}
2098 					if (sctp_is_address_in_scope(sctp_ifap,
2099 					    scope->ipv4_addr_legal,
2100 					    scope->ipv6_addr_legal,
2101 					    scope->loopback_scope,
2102 					    scope->ipv4_local_scope,
2103 					    scope->local_scope,
2104 					    scope->site_scope, 0) == 0) {
2105 						continue;
2106 					}
2107 					m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2108 					if (limit_out) {
2109 						cnt++;
2110 						total_count++;
2111 						if (cnt >= 2) {
2112 							/*
2113 							 * two from each
2114 							 * address
2115 							 */
2116 							break;
2117 						}
2118 						if (total_count > SCTP_ADDRESS_LIMIT) {
2119 							/* No more addresses */
2120 							break;
2121 						}
2122 					}
2123 				}
2124 			}
2125 		}
2126 	} else {
2127 		struct sctp_laddr *laddr;
2128 
2129 		cnt = cnt_inits_to;
2130 		/* First, how many ? */
2131 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2132 			if (laddr->ifa == NULL) {
2133 				continue;
2134 			}
2135 			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2136 				/*
2137 				 * Address being deleted by the system, dont
2138 				 * list.
2139 				 */
2140 				continue;
2141 			if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2142 				/*
2143 				 * Address being deleted on this ep don't
2144 				 * list.
2145 				 */
2146 				continue;
2147 			}
2148 			if (sctp_is_address_in_scope(laddr->ifa,
2149 			    scope->ipv4_addr_legal,
2150 			    scope->ipv6_addr_legal,
2151 			    scope->loopback_scope,
2152 			    scope->ipv4_local_scope,
2153 			    scope->local_scope,
2154 			    scope->site_scope, 1) == 0) {
2155 				continue;
2156 			}
2157 			cnt++;
2158 		}
2159 		if (cnt > SCTP_ADDRESS_LIMIT) {
2160 			limit_out = 1;
2161 		}
2162 		/*
2163 		 * To get through a NAT we only list addresses if we have
2164 		 * more than one. That way if you just bind a single address
2165 		 * we let the source of the init dictate our address.
2166 		 */
2167 		if (cnt > 1) {
2168 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2169 				cnt = 0;
2170 				if (laddr->ifa == NULL) {
2171 					continue;
2172 				}
2173 				if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2174 					continue;
2175 
2176 				if (sctp_is_address_in_scope(laddr->ifa,
2177 				    scope->ipv4_addr_legal,
2178 				    scope->ipv6_addr_legal,
2179 				    scope->loopback_scope,
2180 				    scope->ipv4_local_scope,
2181 				    scope->local_scope,
2182 				    scope->site_scope, 0) == 0) {
2183 					continue;
2184 				}
2185 				m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2186 				cnt++;
2187 				if (cnt >= SCTP_ADDRESS_LIMIT) {
2188 					break;
2189 				}
2190 			}
2191 		}
2192 	}
2193 	SCTP_IPI_ADDR_RUNLOCK();
2194 	return (m_at);
2195 }
2196 
2197 static struct sctp_ifa *
2198 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2199     uint8_t dest_is_loop,
2200     uint8_t dest_is_priv,
2201     sa_family_t fam)
2202 {
2203 	uint8_t dest_is_global = 0;
2204 
2205 	/* dest_is_priv is true if destination is a private address */
2206 	/* dest_is_loop is true if destination is a loopback addresses */
2207 
2208 	/**
2209 	 * Here we determine if its a preferred address. A preferred address
2210 	 * means it is the same scope or higher scope then the destination.
2211 	 * L = loopback, P = private, G = global
2212 	 * -----------------------------------------
2213 	 *    src    |  dest | result
2214 	 *  ----------------------------------------
2215 	 *     L     |    L  |    yes
2216 	 *  -----------------------------------------
2217 	 *     P     |    L  |    yes-v4 no-v6
2218 	 *  -----------------------------------------
2219 	 *     G     |    L  |    yes-v4 no-v6
2220 	 *  -----------------------------------------
2221 	 *     L     |    P  |    no
2222 	 *  -----------------------------------------
2223 	 *     P     |    P  |    yes
2224 	 *  -----------------------------------------
2225 	 *     G     |    P  |    no
2226 	 *   -----------------------------------------
2227 	 *     L     |    G  |    no
2228 	 *   -----------------------------------------
2229 	 *     P     |    G  |    no
2230 	 *    -----------------------------------------
2231 	 *     G     |    G  |    yes
2232 	 *    -----------------------------------------
2233 	 */
2234 
2235 	if (ifa->address.sa.sa_family != fam) {
2236 		/* forget mis-matched family */
2237 		return (NULL);
2238 	}
2239 	if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2240 		dest_is_global = 1;
2241 	}
2242 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2243 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2244 	/* Ok the address may be ok */
2245 #ifdef INET6
2246 	if (fam == AF_INET6) {
2247 		/* ok to use deprecated addresses? no lets not! */
2248 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2249 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2250 			return (NULL);
2251 		}
2252 		if (ifa->src_is_priv && !ifa->src_is_loop) {
2253 			if (dest_is_loop) {
2254 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2255 				return (NULL);
2256 			}
2257 		}
2258 		if (ifa->src_is_glob) {
2259 			if (dest_is_loop) {
2260 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2261 				return (NULL);
2262 			}
2263 		}
2264 	}
2265 #endif
2266 	/*
2267 	 * Now that we know what is what, implement or table this could in
2268 	 * theory be done slicker (it used to be), but this is
2269 	 * straightforward and easier to validate :-)
2270 	 */
2271 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2272 	    ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2273 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2274 	    dest_is_loop, dest_is_priv, dest_is_global);
2275 
2276 	if ((ifa->src_is_loop) && (dest_is_priv)) {
2277 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2278 		return (NULL);
2279 	}
2280 	if ((ifa->src_is_glob) && (dest_is_priv)) {
2281 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2282 		return (NULL);
2283 	}
2284 	if ((ifa->src_is_loop) && (dest_is_global)) {
2285 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2286 		return (NULL);
2287 	}
2288 	if ((ifa->src_is_priv) && (dest_is_global)) {
2289 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2290 		return (NULL);
2291 	}
2292 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2293 	/* its a preferred address */
2294 	return (ifa);
2295 }
2296 
2297 static struct sctp_ifa *
2298 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2299     uint8_t dest_is_loop,
2300     uint8_t dest_is_priv,
2301     sa_family_t fam)
2302 {
2303 	uint8_t dest_is_global = 0;
2304 
2305 	/**
2306 	 * Here we determine if its a acceptable address. A acceptable
2307 	 * address means it is the same scope or higher scope but we can
2308 	 * allow for NAT which means its ok to have a global dest and a
2309 	 * private src.
2310 	 *
2311 	 * L = loopback, P = private, G = global
2312 	 * -----------------------------------------
2313 	 *  src    |  dest | result
2314 	 * -----------------------------------------
2315 	 *   L     |   L   |    yes
2316 	 *  -----------------------------------------
2317 	 *   P     |   L   |    yes-v4 no-v6
2318 	 *  -----------------------------------------
2319 	 *   G     |   L   |    yes
2320 	 * -----------------------------------------
2321 	 *   L     |   P   |    no
2322 	 * -----------------------------------------
2323 	 *   P     |   P   |    yes
2324 	 * -----------------------------------------
2325 	 *   G     |   P   |    yes - May not work
2326 	 * -----------------------------------------
2327 	 *   L     |   G   |    no
2328 	 * -----------------------------------------
2329 	 *   P     |   G   |    yes - May not work
2330 	 * -----------------------------------------
2331 	 *   G     |   G   |    yes
2332 	 * -----------------------------------------
2333 	 */
2334 
2335 	if (ifa->address.sa.sa_family != fam) {
2336 		/* forget non matching family */
2337 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n",
2338 		    ifa->address.sa.sa_family, fam);
2339 		return (NULL);
2340 	}
2341 	/* Ok the address may be ok */
2342 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa);
2343 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n",
2344 	    dest_is_loop, dest_is_priv);
2345 	if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2346 		dest_is_global = 1;
2347 	}
2348 #ifdef INET6
2349 	if (fam == AF_INET6) {
2350 		/* ok to use deprecated addresses? */
2351 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2352 			return (NULL);
2353 		}
2354 		if (ifa->src_is_priv) {
2355 			/* Special case, linklocal to loop */
2356 			if (dest_is_loop)
2357 				return (NULL);
2358 		}
2359 	}
2360 #endif
2361 	/*
2362 	 * Now that we know what is what, implement our table. This could in
2363 	 * theory be done slicker (it used to be), but this is
2364 	 * straightforward and easier to validate :-)
2365 	 */
2366 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n",
2367 	    ifa->src_is_loop,
2368 	    dest_is_priv);
2369 	if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2370 		return (NULL);
2371 	}
2372 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n",
2373 	    ifa->src_is_loop,
2374 	    dest_is_global);
2375 	if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2376 		return (NULL);
2377 	}
2378 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n");
2379 	/* its an acceptable address */
2380 	return (ifa);
2381 }
2382 
2383 int
2384 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2385 {
2386 	struct sctp_laddr *laddr;
2387 
2388 	if (stcb == NULL) {
2389 		/* There are no restrictions, no TCB :-) */
2390 		return (0);
2391 	}
2392 	LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2393 		if (laddr->ifa == NULL) {
2394 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2395 			    __FUNCTION__);
2396 			continue;
2397 		}
2398 		if (laddr->ifa == ifa) {
2399 			/* Yes it is on the list */
2400 			return (1);
2401 		}
2402 	}
2403 	return (0);
2404 }
2405 
2406 
2407 int
2408 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2409 {
2410 	struct sctp_laddr *laddr;
2411 
2412 	if (ifa == NULL)
2413 		return (0);
2414 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2415 		if (laddr->ifa == NULL) {
2416 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2417 			    __FUNCTION__);
2418 			continue;
2419 		}
2420 		if ((laddr->ifa == ifa) && laddr->action == 0)
2421 			/* same pointer */
2422 			return (1);
2423 	}
2424 	return (0);
2425 }
2426 
2427 
2428 
2429 static struct sctp_ifa *
2430 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2431     sctp_route_t * ro,
2432     uint32_t vrf_id,
2433     int non_asoc_addr_ok,
2434     uint8_t dest_is_priv,
2435     uint8_t dest_is_loop,
2436     sa_family_t fam)
2437 {
2438 	struct sctp_laddr *laddr, *starting_point;
2439 	void *ifn;
2440 	int resettotop = 0;
2441 	struct sctp_ifn *sctp_ifn;
2442 	struct sctp_ifa *sctp_ifa, *sifa;
2443 	struct sctp_vrf *vrf;
2444 	uint32_t ifn_index;
2445 
2446 	vrf = sctp_find_vrf(vrf_id);
2447 	if (vrf == NULL)
2448 		return (NULL);
2449 
2450 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2451 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2452 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2453 	/*
2454 	 * first question, is the ifn we will emit on in our list, if so, we
2455 	 * want such an address. Note that we first looked for a preferred
2456 	 * address.
2457 	 */
2458 	if (sctp_ifn) {
2459 		/* is a preferred one on the interface we route out? */
2460 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2461 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2462 			    (non_asoc_addr_ok == 0))
2463 				continue;
2464 			sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2465 			    dest_is_loop,
2466 			    dest_is_priv, fam);
2467 			if (sifa == NULL)
2468 				continue;
2469 			if (sctp_is_addr_in_ep(inp, sifa)) {
2470 				atomic_add_int(&sifa->refcount, 1);
2471 				return (sifa);
2472 			}
2473 		}
2474 	}
2475 	/*
2476 	 * ok, now we now need to find one on the list of the addresses. We
2477 	 * can't get one on the emitting interface so let's find first a
2478 	 * preferred one. If not that an acceptable one otherwise... we
2479 	 * return NULL.
2480 	 */
2481 	starting_point = inp->next_addr_touse;
2482 once_again:
2483 	if (inp->next_addr_touse == NULL) {
2484 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2485 		resettotop = 1;
2486 	}
2487 	for (laddr = inp->next_addr_touse; laddr;
2488 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2489 		if (laddr->ifa == NULL) {
2490 			/* address has been removed */
2491 			continue;
2492 		}
2493 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2494 			/* address is being deleted */
2495 			continue;
2496 		}
2497 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2498 		    dest_is_priv, fam);
2499 		if (sifa == NULL)
2500 			continue;
2501 		atomic_add_int(&sifa->refcount, 1);
2502 		return (sifa);
2503 	}
2504 	if (resettotop == 0) {
2505 		inp->next_addr_touse = NULL;
2506 		goto once_again;
2507 	}
2508 	inp->next_addr_touse = starting_point;
2509 	resettotop = 0;
2510 once_again_too:
2511 	if (inp->next_addr_touse == NULL) {
2512 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2513 		resettotop = 1;
2514 	}
2515 	/* ok, what about an acceptable address in the inp */
2516 	for (laddr = inp->next_addr_touse; laddr;
2517 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2518 		if (laddr->ifa == NULL) {
2519 			/* address has been removed */
2520 			continue;
2521 		}
2522 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2523 			/* address is being deleted */
2524 			continue;
2525 		}
2526 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2527 		    dest_is_priv, fam);
2528 		if (sifa == NULL)
2529 			continue;
2530 		atomic_add_int(&sifa->refcount, 1);
2531 		return (sifa);
2532 	}
2533 	if (resettotop == 0) {
2534 		inp->next_addr_touse = NULL;
2535 		goto once_again_too;
2536 	}
2537 	/*
2538 	 * no address bound can be a source for the destination we are in
2539 	 * trouble
2540 	 */
2541 	return (NULL);
2542 }
2543 
2544 
2545 
2546 static struct sctp_ifa *
2547 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2548     struct sctp_tcb *stcb,
2549     struct sctp_nets *net,
2550     sctp_route_t * ro,
2551     uint32_t vrf_id,
2552     uint8_t dest_is_priv,
2553     uint8_t dest_is_loop,
2554     int non_asoc_addr_ok,
2555     sa_family_t fam)
2556 {
2557 	struct sctp_laddr *laddr, *starting_point;
2558 	void *ifn;
2559 	struct sctp_ifn *sctp_ifn;
2560 	struct sctp_ifa *sctp_ifa, *sifa;
2561 	uint8_t start_at_beginning = 0;
2562 	struct sctp_vrf *vrf;
2563 	uint32_t ifn_index;
2564 
2565 	/*
2566 	 * first question, is the ifn we will emit on in our list, if so, we
2567 	 * want that one.
2568 	 */
2569 	vrf = sctp_find_vrf(vrf_id);
2570 	if (vrf == NULL)
2571 		return (NULL);
2572 
2573 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2574 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2575 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2576 
2577 	/*
2578 	 * first question, is the ifn we will emit on in our list?  If so,
2579 	 * we want that one. First we look for a preferred. Second, we go
2580 	 * for an acceptable.
2581 	 */
2582 	if (sctp_ifn) {
2583 		/* first try for a preferred address on the ep */
2584 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2585 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2586 				continue;
2587 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2588 				sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2589 				if (sifa == NULL)
2590 					continue;
2591 				if (((non_asoc_addr_ok == 0) &&
2592 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2593 				    (non_asoc_addr_ok &&
2594 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2595 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2596 					/* on the no-no list */
2597 					continue;
2598 				}
2599 				atomic_add_int(&sifa->refcount, 1);
2600 				return (sifa);
2601 			}
2602 		}
2603 		/* next try for an acceptable address on the ep */
2604 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2605 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2606 				continue;
2607 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2608 				sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2609 				if (sifa == NULL)
2610 					continue;
2611 				if (((non_asoc_addr_ok == 0) &&
2612 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2613 				    (non_asoc_addr_ok &&
2614 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2615 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2616 					/* on the no-no list */
2617 					continue;
2618 				}
2619 				atomic_add_int(&sifa->refcount, 1);
2620 				return (sifa);
2621 			}
2622 		}
2623 
2624 	}
2625 	/*
2626 	 * if we can't find one like that then we must look at all addresses
2627 	 * bound to pick one at first preferable then secondly acceptable.
2628 	 */
2629 	starting_point = stcb->asoc.last_used_address;
2630 sctp_from_the_top:
2631 	if (stcb->asoc.last_used_address == NULL) {
2632 		start_at_beginning = 1;
2633 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2634 	}
2635 	/* search beginning with the last used address */
2636 	for (laddr = stcb->asoc.last_used_address; laddr;
2637 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2638 		if (laddr->ifa == NULL) {
2639 			/* address has been removed */
2640 			continue;
2641 		}
2642 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2643 			/* address is being deleted */
2644 			continue;
2645 		}
2646 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2647 		if (sifa == NULL)
2648 			continue;
2649 		if (((non_asoc_addr_ok == 0) &&
2650 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2651 		    (non_asoc_addr_ok &&
2652 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2653 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2654 			/* on the no-no list */
2655 			continue;
2656 		}
2657 		stcb->asoc.last_used_address = laddr;
2658 		atomic_add_int(&sifa->refcount, 1);
2659 		return (sifa);
2660 	}
2661 	if (start_at_beginning == 0) {
2662 		stcb->asoc.last_used_address = NULL;
2663 		goto sctp_from_the_top;
2664 	}
2665 	/* now try for any higher scope than the destination */
2666 	stcb->asoc.last_used_address = starting_point;
2667 	start_at_beginning = 0;
2668 sctp_from_the_top2:
2669 	if (stcb->asoc.last_used_address == NULL) {
2670 		start_at_beginning = 1;
2671 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2672 	}
2673 	/* search beginning with the last used address */
2674 	for (laddr = stcb->asoc.last_used_address; laddr;
2675 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2676 		if (laddr->ifa == NULL) {
2677 			/* address has been removed */
2678 			continue;
2679 		}
2680 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2681 			/* address is being deleted */
2682 			continue;
2683 		}
2684 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2685 		    dest_is_priv, fam);
2686 		if (sifa == NULL)
2687 			continue;
2688 		if (((non_asoc_addr_ok == 0) &&
2689 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2690 		    (non_asoc_addr_ok &&
2691 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2692 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2693 			/* on the no-no list */
2694 			continue;
2695 		}
2696 		stcb->asoc.last_used_address = laddr;
2697 		atomic_add_int(&sifa->refcount, 1);
2698 		return (sifa);
2699 	}
2700 	if (start_at_beginning == 0) {
2701 		stcb->asoc.last_used_address = NULL;
2702 		goto sctp_from_the_top2;
2703 	}
2704 	return (NULL);
2705 }
2706 
2707 static struct sctp_ifa *
2708 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2709     struct sctp_tcb *stcb,
2710     int non_asoc_addr_ok,
2711     uint8_t dest_is_loop,
2712     uint8_t dest_is_priv,
2713     int addr_wanted,
2714     sa_family_t fam,
2715     sctp_route_t * ro
2716 )
2717 {
2718 	struct sctp_ifa *ifa, *sifa;
2719 	int num_eligible_addr = 0;
2720 
2721 #ifdef INET6
2722 	struct sockaddr_in6 sin6, lsa6;
2723 
2724 	if (fam == AF_INET6) {
2725 		memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2726 		(void)sa6_recoverscope(&sin6);
2727 	}
2728 #endif				/* INET6 */
2729 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2730 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2731 		    (non_asoc_addr_ok == 0))
2732 			continue;
2733 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2734 		    dest_is_priv, fam);
2735 		if (sifa == NULL)
2736 			continue;
2737 #ifdef INET6
2738 		if (fam == AF_INET6 &&
2739 		    dest_is_loop &&
2740 		    sifa->src_is_loop && sifa->src_is_priv) {
2741 			/*
2742 			 * don't allow fe80::1 to be a src on loop ::1, we
2743 			 * don't list it to the peer so we will get an
2744 			 * abort.
2745 			 */
2746 			continue;
2747 		}
2748 		if (fam == AF_INET6 &&
2749 		    IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2750 		    IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2751 			/*
2752 			 * link-local <-> link-local must belong to the same
2753 			 * scope.
2754 			 */
2755 			memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2756 			(void)sa6_recoverscope(&lsa6);
2757 			if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2758 				continue;
2759 			}
2760 		}
2761 #endif				/* INET6 */
2762 
2763 		/*
2764 		 * Check if the IPv6 address matches to next-hop. In the
2765 		 * mobile case, old IPv6 address may be not deleted from the
2766 		 * interface. Then, the interface has previous and new
2767 		 * addresses.  We should use one corresponding to the
2768 		 * next-hop.  (by micchie)
2769 		 */
2770 #ifdef INET6
2771 		if (stcb && fam == AF_INET6 &&
2772 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2773 			if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2774 			    == 0) {
2775 				continue;
2776 			}
2777 		}
2778 #endif
2779 #ifdef INET
2780 		/* Avoid topologically incorrect IPv4 address */
2781 		if (stcb && fam == AF_INET &&
2782 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2783 			if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2784 				continue;
2785 			}
2786 		}
2787 #endif
2788 		if (stcb) {
2789 			if (sctp_is_address_in_scope(ifa,
2790 			    stcb->asoc.ipv4_addr_legal,
2791 			    stcb->asoc.ipv6_addr_legal,
2792 			    stcb->asoc.loopback_scope,
2793 			    stcb->asoc.ipv4_local_scope,
2794 			    stcb->asoc.local_scope,
2795 			    stcb->asoc.site_scope, 0) == 0) {
2796 				continue;
2797 			}
2798 			if (((non_asoc_addr_ok == 0) &&
2799 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2800 			    (non_asoc_addr_ok &&
2801 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2802 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2803 				/*
2804 				 * It is restricted for some reason..
2805 				 * probably not yet added.
2806 				 */
2807 				continue;
2808 			}
2809 		}
2810 		if (num_eligible_addr >= addr_wanted) {
2811 			return (sifa);
2812 		}
2813 		num_eligible_addr++;
2814 	}
2815 	return (NULL);
2816 }
2817 
2818 
2819 static int
2820 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2821     struct sctp_tcb *stcb,
2822     int non_asoc_addr_ok,
2823     uint8_t dest_is_loop,
2824     uint8_t dest_is_priv,
2825     sa_family_t fam)
2826 {
2827 	struct sctp_ifa *ifa, *sifa;
2828 	int num_eligible_addr = 0;
2829 
2830 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2831 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2832 		    (non_asoc_addr_ok == 0)) {
2833 			continue;
2834 		}
2835 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2836 		    dest_is_priv, fam);
2837 		if (sifa == NULL) {
2838 			continue;
2839 		}
2840 		if (stcb) {
2841 			if (sctp_is_address_in_scope(ifa,
2842 			    stcb->asoc.ipv4_addr_legal,
2843 			    stcb->asoc.ipv6_addr_legal,
2844 			    stcb->asoc.loopback_scope,
2845 			    stcb->asoc.ipv4_local_scope,
2846 			    stcb->asoc.local_scope,
2847 			    stcb->asoc.site_scope, 0) == 0) {
2848 				continue;
2849 			}
2850 			if (((non_asoc_addr_ok == 0) &&
2851 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2852 			    (non_asoc_addr_ok &&
2853 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2854 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2855 				/*
2856 				 * It is restricted for some reason..
2857 				 * probably not yet added.
2858 				 */
2859 				continue;
2860 			}
2861 		}
2862 		num_eligible_addr++;
2863 	}
2864 	return (num_eligible_addr);
2865 }
2866 
2867 static struct sctp_ifa *
2868 sctp_choose_boundall(struct sctp_inpcb *inp,
2869     struct sctp_tcb *stcb,
2870     struct sctp_nets *net,
2871     sctp_route_t * ro,
2872     uint32_t vrf_id,
2873     uint8_t dest_is_priv,
2874     uint8_t dest_is_loop,
2875     int non_asoc_addr_ok,
2876     sa_family_t fam)
2877 {
2878 	int cur_addr_num = 0, num_preferred = 0;
2879 	void *ifn;
2880 	struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2881 	struct sctp_ifa *sctp_ifa, *sifa;
2882 	uint32_t ifn_index;
2883 	struct sctp_vrf *vrf;
2884 
2885 #ifdef INET
2886 	int retried = 0;
2887 
2888 #endif
2889 	/*-
2890 	 * For boundall we can use any address in the association.
2891 	 * If non_asoc_addr_ok is set we can use any address (at least in
2892 	 * theory). So we look for preferred addresses first. If we find one,
2893 	 * we use it. Otherwise we next try to get an address on the
2894 	 * interface, which we should be able to do (unless non_asoc_addr_ok
2895 	 * is false and we are routed out that way). In these cases where we
2896 	 * can't use the address of the interface we go through all the
2897 	 * ifn's looking for an address we can use and fill that in. Punting
2898 	 * means we send back address 0, which will probably cause problems
2899 	 * actually since then IP will fill in the address of the route ifn,
2900 	 * which means we probably already rejected it.. i.e. here comes an
2901 	 * abort :-<.
2902 	 */
2903 	vrf = sctp_find_vrf(vrf_id);
2904 	if (vrf == NULL)
2905 		return (NULL);
2906 
2907 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2908 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2909 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn from route:%p ifn_index:%d\n", ifn, ifn_index);
2910 	emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2911 	if (sctp_ifn == NULL) {
2912 		/* ?? We don't have this guy ?? */
2913 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2914 		goto bound_all_plan_b;
2915 	}
2916 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2917 	    ifn_index, sctp_ifn->ifn_name);
2918 
2919 	if (net) {
2920 		cur_addr_num = net->indx_of_eligible_next_to_use;
2921 	}
2922 	num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2923 	    stcb,
2924 	    non_asoc_addr_ok,
2925 	    dest_is_loop,
2926 	    dest_is_priv, fam);
2927 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2928 	    num_preferred, sctp_ifn->ifn_name);
2929 	if (num_preferred == 0) {
2930 		/*
2931 		 * no eligible addresses, we must use some other interface
2932 		 * address if we can find one.
2933 		 */
2934 		goto bound_all_plan_b;
2935 	}
2936 	/*
2937 	 * Ok we have num_eligible_addr set with how many we can use, this
2938 	 * may vary from call to call due to addresses being deprecated
2939 	 * etc..
2940 	 */
2941 	if (cur_addr_num >= num_preferred) {
2942 		cur_addr_num = 0;
2943 	}
2944 	/*
2945 	 * select the nth address from the list (where cur_addr_num is the
2946 	 * nth) and 0 is the first one, 1 is the second one etc...
2947 	 */
2948 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2949 
2950 	sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2951 	    dest_is_priv, cur_addr_num, fam, ro);
2952 
2953 	/* if sctp_ifa is NULL something changed??, fall to plan b. */
2954 	if (sctp_ifa) {
2955 		atomic_add_int(&sctp_ifa->refcount, 1);
2956 		if (net) {
2957 			/* save off where the next one we will want */
2958 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2959 		}
2960 		return (sctp_ifa);
2961 	}
2962 	/*
2963 	 * plan_b: Look at all interfaces and find a preferred address. If
2964 	 * no preferred fall through to plan_c.
2965 	 */
2966 bound_all_plan_b:
2967 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2968 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2969 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2970 		    sctp_ifn->ifn_name);
2971 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2972 			/* wrong base scope */
2973 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2974 			continue;
2975 		}
2976 		if ((sctp_ifn == looked_at) && looked_at) {
2977 			/* already looked at this guy */
2978 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2979 			continue;
2980 		}
2981 		num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2982 		    dest_is_loop, dest_is_priv, fam);
2983 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
2984 		    "Found ifn:%p %d preferred source addresses\n",
2985 		    ifn, num_preferred);
2986 		if (num_preferred == 0) {
2987 			/* None on this interface. */
2988 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2989 			continue;
2990 		}
2991 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
2992 		    "num preferred:%d on interface:%p cur_addr_num:%d\n",
2993 		    num_preferred, sctp_ifn, cur_addr_num);
2994 
2995 		/*
2996 		 * Ok we have num_eligible_addr set with how many we can
2997 		 * use, this may vary from call to call due to addresses
2998 		 * being deprecated etc..
2999 		 */
3000 		if (cur_addr_num >= num_preferred) {
3001 			cur_addr_num = 0;
3002 		}
3003 		sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
3004 		    dest_is_priv, cur_addr_num, fam, ro);
3005 		if (sifa == NULL)
3006 			continue;
3007 		if (net) {
3008 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3009 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
3010 			    cur_addr_num);
3011 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
3012 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
3013 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
3014 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
3015 		}
3016 		atomic_add_int(&sifa->refcount, 1);
3017 		return (sifa);
3018 	}
3019 #ifdef INET
3020 again_with_private_addresses_allowed:
3021 #endif
3022 	/* plan_c: do we have an acceptable address on the emit interface */
3023 	sifa = NULL;
3024 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
3025 	if (emit_ifn == NULL) {
3026 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jump to Plan D - no emit_ifn\n");
3027 		goto plan_d;
3028 	}
3029 	LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
3030 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", sctp_ifa);
3031 		if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3032 		    (non_asoc_addr_ok == 0)) {
3033 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Defer\n");
3034 			continue;
3035 		}
3036 		sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
3037 		    dest_is_priv, fam);
3038 		if (sifa == NULL) {
3039 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n");
3040 			continue;
3041 		}
3042 		if (stcb) {
3043 			if (sctp_is_address_in_scope(sifa,
3044 			    stcb->asoc.ipv4_addr_legal,
3045 			    stcb->asoc.ipv6_addr_legal,
3046 			    stcb->asoc.loopback_scope,
3047 			    stcb->asoc.ipv4_local_scope,
3048 			    stcb->asoc.local_scope,
3049 			    stcb->asoc.site_scope, 0) == 0) {
3050 				SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n");
3051 				sifa = NULL;
3052 				continue;
3053 			}
3054 			if (((non_asoc_addr_ok == 0) &&
3055 			    (sctp_is_addr_restricted(stcb, sifa))) ||
3056 			    (non_asoc_addr_ok &&
3057 			    (sctp_is_addr_restricted(stcb, sifa)) &&
3058 			    (!sctp_is_addr_pending(stcb, sifa)))) {
3059 				/*
3060 				 * It is restricted for some reason..
3061 				 * probably not yet added.
3062 				 */
3063 				SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its resticted\n");
3064 				sifa = NULL;
3065 				continue;
3066 			}
3067 		} else {
3068 			printf("Stcb is null - no print\n");
3069 		}
3070 		atomic_add_int(&sifa->refcount, 1);
3071 		goto out;
3072 	}
3073 plan_d:
3074 	/*
3075 	 * plan_d: We are in trouble. No preferred address on the emit
3076 	 * interface. And not even a preferred address on all interfaces. Go
3077 	 * out and see if we can find an acceptable address somewhere
3078 	 * amongst all interfaces.
3079 	 */
3080 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", looked_at);
3081 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3082 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3083 			/* wrong base scope */
3084 			continue;
3085 		}
3086 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3087 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3088 			    (non_asoc_addr_ok == 0))
3089 				continue;
3090 			sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3091 			    dest_is_loop,
3092 			    dest_is_priv, fam);
3093 			if (sifa == NULL)
3094 				continue;
3095 			if (stcb) {
3096 				if (sctp_is_address_in_scope(sifa,
3097 				    stcb->asoc.ipv4_addr_legal,
3098 				    stcb->asoc.ipv6_addr_legal,
3099 				    stcb->asoc.loopback_scope,
3100 				    stcb->asoc.ipv4_local_scope,
3101 				    stcb->asoc.local_scope,
3102 				    stcb->asoc.site_scope, 0) == 0) {
3103 					sifa = NULL;
3104 					continue;
3105 				}
3106 				if (((non_asoc_addr_ok == 0) &&
3107 				    (sctp_is_addr_restricted(stcb, sifa))) ||
3108 				    (non_asoc_addr_ok &&
3109 				    (sctp_is_addr_restricted(stcb, sifa)) &&
3110 				    (!sctp_is_addr_pending(stcb, sifa)))) {
3111 					/*
3112 					 * It is restricted for some
3113 					 * reason.. probably not yet added.
3114 					 */
3115 					sifa = NULL;
3116 					continue;
3117 				}
3118 			}
3119 			goto out;
3120 		}
3121 	}
3122 #ifdef INET
3123 	if ((retried == 0) && (stcb->asoc.ipv4_local_scope == 0)) {
3124 		stcb->asoc.ipv4_local_scope = 1;
3125 		retried = 1;
3126 		goto again_with_private_addresses_allowed;
3127 	} else if (retried == 1) {
3128 		stcb->asoc.ipv4_local_scope = 0;
3129 	}
3130 #endif
3131 out:
3132 #ifdef INET
3133 	if (sifa) {
3134 		if (retried == 1) {
3135 			LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3136 				if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3137 					/* wrong base scope */
3138 					continue;
3139 				}
3140 				LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3141 					struct sctp_ifa *tmp_sifa;
3142 
3143 					if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3144 					    (non_asoc_addr_ok == 0))
3145 						continue;
3146 					tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3147 					    dest_is_loop,
3148 					    dest_is_priv, fam);
3149 					if (tmp_sifa == NULL) {
3150 						continue;
3151 					}
3152 					if (tmp_sifa == sifa) {
3153 						continue;
3154 					}
3155 					if (stcb) {
3156 						if (sctp_is_address_in_scope(tmp_sifa,
3157 						    stcb->asoc.ipv4_addr_legal,
3158 						    stcb->asoc.ipv6_addr_legal,
3159 						    stcb->asoc.loopback_scope,
3160 						    stcb->asoc.ipv4_local_scope,
3161 						    stcb->asoc.local_scope,
3162 						    stcb->asoc.site_scope, 0) == 0) {
3163 							continue;
3164 						}
3165 						if (((non_asoc_addr_ok == 0) &&
3166 						    (sctp_is_addr_restricted(stcb, tmp_sifa))) ||
3167 						    (non_asoc_addr_ok &&
3168 						    (sctp_is_addr_restricted(stcb, tmp_sifa)) &&
3169 						    (!sctp_is_addr_pending(stcb, tmp_sifa)))) {
3170 							/*
3171 							 * It is restricted
3172 							 * for some reason..
3173 							 * probably not yet
3174 							 * added.
3175 							 */
3176 							continue;
3177 						}
3178 					}
3179 					if ((tmp_sifa->address.sin.sin_family == AF_INET) &&
3180 					    (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) {
3181 						sctp_add_local_addr_restricted(stcb, tmp_sifa);
3182 					}
3183 				}
3184 			}
3185 		}
3186 		atomic_add_int(&sifa->refcount, 1);
3187 	}
3188 #endif
3189 	return (sifa);
3190 }
3191 
3192 
3193 
3194 /* tcb may be NULL */
3195 struct sctp_ifa *
3196 sctp_source_address_selection(struct sctp_inpcb *inp,
3197     struct sctp_tcb *stcb,
3198     sctp_route_t * ro,
3199     struct sctp_nets *net,
3200     int non_asoc_addr_ok, uint32_t vrf_id)
3201 {
3202 	struct sctp_ifa *answer;
3203 	uint8_t dest_is_priv, dest_is_loop;
3204 	sa_family_t fam;
3205 
3206 #ifdef INET
3207 	struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3208 
3209 #endif
3210 #ifdef INET6
3211 	struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3212 
3213 #endif
3214 
3215 	/**
3216 	 * Rules: - Find the route if needed, cache if I can. - Look at
3217 	 * interface address in route, Is it in the bound list. If so we
3218 	 * have the best source. - If not we must rotate amongst the
3219 	 * addresses.
3220 	 *
3221 	 * Cavets and issues
3222 	 *
3223 	 * Do we need to pay attention to scope. We can have a private address
3224 	 * or a global address we are sourcing or sending to. So if we draw
3225 	 * it out
3226 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3227 	 * For V4
3228 	 * ------------------------------------------
3229 	 *      source     *      dest  *  result
3230 	 * -----------------------------------------
3231 	 * <a>  Private    *    Global  *  NAT
3232 	 * -----------------------------------------
3233 	 * <b>  Private    *    Private *  No problem
3234 	 * -----------------------------------------
3235 	 * <c>  Global     *    Private *  Huh, How will this work?
3236 	 * -----------------------------------------
3237 	 * <d>  Global     *    Global  *  No Problem
3238 	 *------------------------------------------
3239 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3240 	 * For V6
3241 	 *------------------------------------------
3242 	 *      source     *      dest  *  result
3243 	 * -----------------------------------------
3244 	 * <a>  Linklocal  *    Global  *
3245 	 * -----------------------------------------
3246 	 * <b>  Linklocal  * Linklocal  *  No problem
3247 	 * -----------------------------------------
3248 	 * <c>  Global     * Linklocal  *  Huh, How will this work?
3249 	 * -----------------------------------------
3250 	 * <d>  Global     *    Global  *  No Problem
3251 	 *------------------------------------------
3252 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3253 	 *
3254 	 * And then we add to that what happens if there are multiple addresses
3255 	 * assigned to an interface. Remember the ifa on a ifn is a linked
3256 	 * list of addresses. So one interface can have more than one IP
3257 	 * address. What happens if we have both a private and a global
3258 	 * address? Do we then use context of destination to sort out which
3259 	 * one is best? And what about NAT's sending P->G may get you a NAT
3260 	 * translation, or should you select the G thats on the interface in
3261 	 * preference.
3262 	 *
3263 	 * Decisions:
3264 	 *
3265 	 * - count the number of addresses on the interface.
3266 	 * - if it is one, no problem except case <c>.
3267 	 *   For <a> we will assume a NAT out there.
3268 	 * - if there are more than one, then we need to worry about scope P
3269 	 *   or G. We should prefer G -> G and P -> P if possible.
3270 	 *   Then as a secondary fall back to mixed types G->P being a last
3271 	 *   ditch one.
3272 	 * - The above all works for bound all, but bound specific we need to
3273 	 *   use the same concept but instead only consider the bound
3274 	 *   addresses. If the bound set is NOT assigned to the interface then
3275 	 *   we must use rotation amongst the bound addresses..
3276 	 */
3277 	if (ro->ro_rt == NULL) {
3278 		/*
3279 		 * Need a route to cache.
3280 		 */
3281 		SCTP_RTALLOC(ro, vrf_id);
3282 	}
3283 	if (ro->ro_rt == NULL) {
3284 		return (NULL);
3285 	}
3286 	fam = ro->ro_dst.sa_family;
3287 	dest_is_priv = dest_is_loop = 0;
3288 	/* Setup our scopes for the destination */
3289 	switch (fam) {
3290 #ifdef INET
3291 	case AF_INET:
3292 		/* Scope based on outbound address */
3293 		if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3294 			dest_is_loop = 1;
3295 			if (net != NULL) {
3296 				/* mark it as local */
3297 				net->addr_is_local = 1;
3298 			}
3299 		} else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3300 			dest_is_priv = 1;
3301 		}
3302 		break;
3303 #endif
3304 #ifdef INET6
3305 	case AF_INET6:
3306 		/* Scope based on outbound address */
3307 		if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3308 		    SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3309 			/*
3310 			 * If the address is a loopback address, which
3311 			 * consists of "::1" OR "fe80::1%lo0", we are
3312 			 * loopback scope. But we don't use dest_is_priv
3313 			 * (link local addresses).
3314 			 */
3315 			dest_is_loop = 1;
3316 			if (net != NULL) {
3317 				/* mark it as local */
3318 				net->addr_is_local = 1;
3319 			}
3320 		} else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3321 			dest_is_priv = 1;
3322 		}
3323 		break;
3324 #endif
3325 	}
3326 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3327 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst);
3328 	SCTP_IPI_ADDR_RLOCK();
3329 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3330 		/*
3331 		 * Bound all case
3332 		 */
3333 		answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3334 		    dest_is_priv, dest_is_loop,
3335 		    non_asoc_addr_ok, fam);
3336 		SCTP_IPI_ADDR_RUNLOCK();
3337 		return (answer);
3338 	}
3339 	/*
3340 	 * Subset bound case
3341 	 */
3342 	if (stcb) {
3343 		answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
3344 		    vrf_id, dest_is_priv,
3345 		    dest_is_loop,
3346 		    non_asoc_addr_ok, fam);
3347 	} else {
3348 		answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3349 		    non_asoc_addr_ok,
3350 		    dest_is_priv,
3351 		    dest_is_loop, fam);
3352 	}
3353 	SCTP_IPI_ADDR_RUNLOCK();
3354 	return (answer);
3355 }
3356 
3357 static int
3358 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
3359 {
3360 	struct cmsghdr cmh;
3361 	int tlen, at;
3362 
3363 	tlen = SCTP_BUF_LEN(control);
3364 	at = 0;
3365 	/*
3366 	 * Independent of how many mbufs, find the c_type inside the control
3367 	 * structure and copy out the data.
3368 	 */
3369 	while (at < tlen) {
3370 		if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3371 			/* not enough room for one more we are done. */
3372 			return (0);
3373 		}
3374 		m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3375 		if (((int)cmh.cmsg_len + at) > tlen) {
3376 			/*
3377 			 * this is real messed up since there is not enough
3378 			 * data here to cover the cmsg header. We are done.
3379 			 */
3380 			return (0);
3381 		}
3382 		if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3383 		    (c_type == cmh.cmsg_type)) {
3384 			/* found the one we want, copy it out */
3385 			at += CMSG_ALIGN(sizeof(struct cmsghdr));
3386 			if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3387 				/*
3388 				 * space of cmsg_len after header not big
3389 				 * enough
3390 				 */
3391 				return (0);
3392 			}
3393 			m_copydata(control, at, cpsize, data);
3394 			return (1);
3395 		} else {
3396 			at += CMSG_ALIGN(cmh.cmsg_len);
3397 			if (cmh.cmsg_len == 0) {
3398 				break;
3399 			}
3400 		}
3401 	}
3402 	/* not found */
3403 	return (0);
3404 }
3405 
3406 static struct mbuf *
3407 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
3408     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3409 {
3410 	struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3411 	struct sctp_state_cookie *stc;
3412 	struct sctp_paramhdr *ph;
3413 	uint8_t *foo;
3414 	int sig_offset;
3415 	uint16_t cookie_sz;
3416 
3417 	mret = NULL;
3418 	mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3419 	    sizeof(struct sctp_paramhdr)), 0,
3420 	    M_DONTWAIT, 1, MT_DATA);
3421 	if (mret == NULL) {
3422 		return (NULL);
3423 	}
3424 	copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3425 	if (copy_init == NULL) {
3426 		sctp_m_freem(mret);
3427 		return (NULL);
3428 	}
3429 #ifdef SCTP_MBUF_LOGGING
3430 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3431 		struct mbuf *mat;
3432 
3433 		mat = copy_init;
3434 		while (mat) {
3435 			if (SCTP_BUF_IS_EXTENDED(mat)) {
3436 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3437 			}
3438 			mat = SCTP_BUF_NEXT(mat);
3439 		}
3440 	}
3441 #endif
3442 	copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3443 	    M_DONTWAIT);
3444 	if (copy_initack == NULL) {
3445 		sctp_m_freem(mret);
3446 		sctp_m_freem(copy_init);
3447 		return (NULL);
3448 	}
3449 #ifdef SCTP_MBUF_LOGGING
3450 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3451 		struct mbuf *mat;
3452 
3453 		mat = copy_initack;
3454 		while (mat) {
3455 			if (SCTP_BUF_IS_EXTENDED(mat)) {
3456 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3457 			}
3458 			mat = SCTP_BUF_NEXT(mat);
3459 		}
3460 	}
3461 #endif
3462 	/* easy side we just drop it on the end */
3463 	ph = mtod(mret, struct sctp_paramhdr *);
3464 	SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3465 	    sizeof(struct sctp_paramhdr);
3466 	stc = (struct sctp_state_cookie *)((caddr_t)ph +
3467 	    sizeof(struct sctp_paramhdr));
3468 	ph->param_type = htons(SCTP_STATE_COOKIE);
3469 	ph->param_length = 0;	/* fill in at the end */
3470 	/* Fill in the stc cookie data */
3471 	memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3472 
3473 	/* tack the INIT and then the INIT-ACK onto the chain */
3474 	cookie_sz = 0;
3475 	m_at = mret;
3476 	for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3477 		cookie_sz += SCTP_BUF_LEN(m_at);
3478 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3479 			SCTP_BUF_NEXT(m_at) = copy_init;
3480 			break;
3481 		}
3482 	}
3483 
3484 	for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3485 		cookie_sz += SCTP_BUF_LEN(m_at);
3486 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3487 			SCTP_BUF_NEXT(m_at) = copy_initack;
3488 			break;
3489 		}
3490 	}
3491 
3492 	for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3493 		cookie_sz += SCTP_BUF_LEN(m_at);
3494 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3495 			break;
3496 		}
3497 	}
3498 	sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3499 	if (sig == NULL) {
3500 		/* no space, so free the entire chain */
3501 		sctp_m_freem(mret);
3502 		return (NULL);
3503 	}
3504 	SCTP_BUF_LEN(sig) = 0;
3505 	SCTP_BUF_NEXT(m_at) = sig;
3506 	sig_offset = 0;
3507 	foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3508 	memset(foo, 0, SCTP_SIGNATURE_SIZE);
3509 	*signature = foo;
3510 	SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3511 	cookie_sz += SCTP_SIGNATURE_SIZE;
3512 	ph->param_length = htons(cookie_sz);
3513 	return (mret);
3514 }
3515 
3516 
3517 static uint8_t
3518 sctp_get_ect(struct sctp_tcb *stcb,
3519     struct sctp_tmit_chunk *chk)
3520 {
3521 	if ((stcb != NULL) && (stcb->asoc.ecn_allowed == 1)) {
3522 		return (SCTP_ECT0_BIT);
3523 	} else {
3524 		return (0);
3525 	}
3526 }
3527 
3528 static void
3529 sctp_handle_no_route(struct sctp_tcb *stcb,
3530     struct sctp_nets *net,
3531     int so_locked)
3532 {
3533 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n");
3534 
3535 	if (net) {
3536 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was ");
3537 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa);
3538 		if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3539 			if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3540 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3541 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3542 				    stcb,
3543 				    SCTP_FAILED_THRESHOLD,
3544 				    (void *)net,
3545 				    so_locked);
3546 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
3547 				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
3548 				/*
3549 				 * JRS 5/14/07 - If a destination is
3550 				 * unreachable, the PF bit is turned off.
3551 				 * This allows an unambiguous use of the PF
3552 				 * bit for destinations that are reachable
3553 				 * but potentially failed. If the
3554 				 * destination is set to the unreachable
3555 				 * state, also set the destination to the PF
3556 				 * state.
3557 				 */
3558 				/*
3559 				 * Add debug message here if destination is
3560 				 * not in PF state.
3561 				 */
3562 				/* Stop any running T3 timers here? */
3563 				if ((stcb->asoc.sctp_cmt_on_off > 0) &&
3564 				    (stcb->asoc.sctp_cmt_pf > 0)) {
3565 					net->dest_state &= ~SCTP_ADDR_PF;
3566 					SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
3567 					    net);
3568 				}
3569 			}
3570 		}
3571 		if (stcb) {
3572 			if (net == stcb->asoc.primary_destination) {
3573 				/* need a new primary */
3574 				struct sctp_nets *alt;
3575 
3576 				alt = sctp_find_alternate_net(stcb, net, 0);
3577 				if (alt != net) {
3578 					if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, alt) == 0) {
3579 						net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
3580 						if (net->ro._s_addr) {
3581 							sctp_free_ifa(net->ro._s_addr);
3582 							net->ro._s_addr = NULL;
3583 						}
3584 						net->src_addr_selected = 0;
3585 					}
3586 				}
3587 			}
3588 		}
3589 	}
3590 }
3591 
3592 static int
3593 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3594     struct sctp_tcb *stcb,	/* may be NULL */
3595     struct sctp_nets *net,
3596     struct sockaddr *to,
3597     struct mbuf *m,
3598     uint32_t auth_offset,
3599     struct sctp_auth_chunk *auth,
3600     uint16_t auth_keyid,
3601     int nofragment_flag,
3602     int ecn_ok,
3603     struct sctp_tmit_chunk *chk,
3604     int out_of_asoc_ok,
3605     uint16_t src_port,
3606     uint16_t dest_port,
3607     uint32_t v_tag,
3608     uint16_t port,
3609     int so_locked,
3610 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3611     SCTP_UNUSED
3612 #endif
3613     union sctp_sockstore *over_addr,
3614     struct mbuf *init
3615 )
3616 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3617 {
3618 	/**
3619 	 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet header
3620 	 * WITH an SCTPHDR but no IP header, endpoint inp and sa structure:
3621 	 * - fill in the HMAC digest of any AUTH chunk in the packet.
3622 	 * - calculate and fill in the SCTP checksum.
3623 	 * - prepend an IP address header.
3624 	 * - if boundall use INADDR_ANY.
3625 	 * - if boundspecific do source address selection.
3626 	 * - set fragmentation option for ipV4.
3627 	 * - On return from IP output, check/adjust mtu size of output
3628 	 *   interface and smallest_mtu size as well.
3629 	 */
3630 	/* Will need ifdefs around this */
3631 	struct mbuf *o_pak;
3632 	struct mbuf *newm;
3633 	struct sctphdr *sctphdr;
3634 	int packet_length;
3635 	int ret;
3636 	uint32_t vrf_id;
3637 	sctp_route_t *ro = NULL;
3638 	struct udphdr *udp = NULL;
3639 
3640 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3641 	struct socket *so = NULL;
3642 
3643 #endif
3644 
3645 	if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3646 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3647 		sctp_m_freem(m);
3648 		return (EFAULT);
3649 	}
3650 	if (stcb) {
3651 		vrf_id = stcb->asoc.vrf_id;
3652 	} else {
3653 		vrf_id = inp->def_vrf_id;
3654 	}
3655 
3656 	/* fill in the HMAC digest for any AUTH chunk in the packet */
3657 	if ((auth != NULL) && (stcb != NULL)) {
3658 		sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
3659 	}
3660 	switch (to->sa_family) {
3661 #ifdef INET
3662 	case AF_INET:
3663 		{
3664 			struct ip *ip = NULL;
3665 			sctp_route_t iproute;
3666 			uint8_t tos_value;
3667 			int len;
3668 
3669 			len = sizeof(struct ip) + sizeof(struct sctphdr);
3670 			if (port) {
3671 				len += sizeof(struct udphdr);
3672 			}
3673 			newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3674 			if (newm == NULL) {
3675 				sctp_m_freem(m);
3676 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3677 				return (ENOMEM);
3678 			}
3679 			SCTP_ALIGN_TO_END(newm, len);
3680 			SCTP_BUF_LEN(newm) = len;
3681 			SCTP_BUF_NEXT(newm) = m;
3682 			m = newm;
3683 			if (net != NULL) {
3684 #ifdef INVARIANTS
3685 				if (net->flowidset == 0) {
3686 					panic("Flow ID not set");
3687 				}
3688 #endif
3689 				m->m_pkthdr.flowid = net->flowid;
3690 				m->m_flags |= M_FLOWID;
3691 			} else {
3692 				if ((init != NULL) && (init->m_flags & M_FLOWID)) {
3693 					m->m_pkthdr.flowid = init->m_pkthdr.flowid;
3694 					m->m_flags |= M_FLOWID;
3695 				}
3696 			}
3697 			packet_length = sctp_calculate_len(m);
3698 			ip = mtod(m, struct ip *);
3699 			ip->ip_v = IPVERSION;
3700 			ip->ip_hl = (sizeof(struct ip) >> 2);
3701 			if (net) {
3702 				tos_value = net->tos_flowlabel & 0x000000ff;
3703 			} else {
3704 				tos_value = inp->ip_inp.inp.inp_ip_tos;
3705 			}
3706 			if ((nofragment_flag) && (port == 0)) {
3707 				ip->ip_off = IP_DF;
3708 			} else
3709 				ip->ip_off = 0;
3710 
3711 			/* FreeBSD has a function for ip_id's */
3712 			ip->ip_id = ip_newid();
3713 
3714 			ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3715 			ip->ip_len = packet_length;
3716 			ip->ip_tos = tos_value & 0xfc;
3717 			if (ecn_ok) {
3718 				ip->ip_tos |= sctp_get_ect(stcb, chk);
3719 			}
3720 			if (port) {
3721 				ip->ip_p = IPPROTO_UDP;
3722 			} else {
3723 				ip->ip_p = IPPROTO_SCTP;
3724 			}
3725 			ip->ip_sum = 0;
3726 			if (net == NULL) {
3727 				ro = &iproute;
3728 				memset(&iproute, 0, sizeof(iproute));
3729 				memcpy(&ro->ro_dst, to, to->sa_len);
3730 			} else {
3731 				ro = (sctp_route_t *) & net->ro;
3732 			}
3733 			/* Now the address selection part */
3734 			ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
3735 
3736 			/* call the routine to select the src address */
3737 			if (net && out_of_asoc_ok == 0) {
3738 				if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3739 					sctp_free_ifa(net->ro._s_addr);
3740 					net->ro._s_addr = NULL;
3741 					net->src_addr_selected = 0;
3742 					if (ro->ro_rt) {
3743 						RTFREE(ro->ro_rt);
3744 						ro->ro_rt = NULL;
3745 					}
3746 				}
3747 				if (net->src_addr_selected == 0) {
3748 					/* Cache the source address */
3749 					net->ro._s_addr = sctp_source_address_selection(inp, stcb,
3750 					    ro, net, 0,
3751 					    vrf_id);
3752 					net->src_addr_selected = 1;
3753 				}
3754 				if (net->ro._s_addr == NULL) {
3755 					/* No route to host */
3756 					net->src_addr_selected = 0;
3757 					sctp_handle_no_route(stcb, net, so_locked);
3758 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3759 					sctp_m_freem(m);
3760 					return (EHOSTUNREACH);
3761 				}
3762 				ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
3763 			} else {
3764 				if (over_addr == NULL) {
3765 					struct sctp_ifa *_lsrc;
3766 
3767 					_lsrc = sctp_source_address_selection(inp, stcb, ro,
3768 					    net,
3769 					    out_of_asoc_ok,
3770 					    vrf_id);
3771 					if (_lsrc == NULL) {
3772 						sctp_handle_no_route(stcb, net, so_locked);
3773 						SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3774 						sctp_m_freem(m);
3775 						return (EHOSTUNREACH);
3776 					}
3777 					ip->ip_src = _lsrc->address.sin.sin_addr;
3778 					sctp_free_ifa(_lsrc);
3779 				} else {
3780 					ip->ip_src = over_addr->sin.sin_addr;
3781 					SCTP_RTALLOC(ro, vrf_id);
3782 				}
3783 			}
3784 			if (port) {
3785 				udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
3786 				udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3787 				udp->uh_dport = port;
3788 				udp->uh_ulen = htons(packet_length - sizeof(struct ip));
3789 				udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
3790 				sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
3791 			} else {
3792 				sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
3793 			}
3794 
3795 			sctphdr->src_port = src_port;
3796 			sctphdr->dest_port = dest_port;
3797 			sctphdr->v_tag = v_tag;
3798 			sctphdr->checksum = 0;
3799 
3800 			/*
3801 			 * If source address selection fails and we find no
3802 			 * route then the ip_output should fail as well with
3803 			 * a NO_ROUTE_TO_HOST type error. We probably should
3804 			 * catch that somewhere and abort the association
3805 			 * right away (assuming this is an INIT being sent).
3806 			 */
3807 			if (ro->ro_rt == NULL) {
3808 				/*
3809 				 * src addr selection failed to find a route
3810 				 * (or valid source addr), so we can't get
3811 				 * there from here (yet)!
3812 				 */
3813 				sctp_handle_no_route(stcb, net, so_locked);
3814 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3815 				sctp_m_freem(m);
3816 				return (EHOSTUNREACH);
3817 			}
3818 			if (ro != &iproute) {
3819 				memcpy(&iproute, ro, sizeof(*ro));
3820 			}
3821 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
3822 			    (uint32_t) (ntohl(ip->ip_src.s_addr)));
3823 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
3824 			    (uint32_t) (ntohl(ip->ip_dst.s_addr)));
3825 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
3826 			    ro->ro_rt);
3827 
3828 			if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3829 				/* failed to prepend data, give up */
3830 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3831 				sctp_m_freem(m);
3832 				return (ENOMEM);
3833 			}
3834 #ifdef  SCTP_PACKET_LOGGING
3835 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
3836 				sctp_packet_log(m, packet_length);
3837 #endif
3838 			SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3839 			if (port) {
3840 #if defined(SCTP_WITH_NO_CSUM)
3841 				SCTP_STAT_INCR(sctps_sendnocrc);
3842 #else
3843 				if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3844 				    (stcb) &&
3845 				    (stcb->asoc.loopback_scope))) {
3846 					sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
3847 					SCTP_STAT_INCR(sctps_sendswcrc);
3848 				} else {
3849 					SCTP_STAT_INCR(sctps_sendnocrc);
3850 				}
3851 #endif
3852 				SCTP_ENABLE_UDP_CSUM(o_pak);
3853 			} else {
3854 #if defined(SCTP_WITH_NO_CSUM)
3855 				SCTP_STAT_INCR(sctps_sendnocrc);
3856 #else
3857 				m->m_pkthdr.csum_flags = CSUM_SCTP;
3858 				m->m_pkthdr.csum_data = 0;
3859 				SCTP_STAT_INCR(sctps_sendhwcrc);
3860 #endif
3861 			}
3862 			/* send it out.  table id is taken from stcb */
3863 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3864 			if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3865 				so = SCTP_INP_SO(inp);
3866 				SCTP_SOCKET_UNLOCK(so, 0);
3867 			}
3868 #endif
3869 			SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
3870 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3871 			if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3872 				atomic_add_int(&stcb->asoc.refcnt, 1);
3873 				SCTP_TCB_UNLOCK(stcb);
3874 				SCTP_SOCKET_LOCK(so, 0);
3875 				SCTP_TCB_LOCK(stcb);
3876 				atomic_subtract_int(&stcb->asoc.refcnt, 1);
3877 			}
3878 #endif
3879 			SCTP_STAT_INCR(sctps_sendpackets);
3880 			SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3881 			if (ret)
3882 				SCTP_STAT_INCR(sctps_senderrors);
3883 
3884 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
3885 			if (net == NULL) {
3886 				/* free tempy routes */
3887 				if (ro->ro_rt) {
3888 					RTFREE(ro->ro_rt);
3889 					ro->ro_rt = NULL;
3890 				}
3891 			} else {
3892 				/*
3893 				 * PMTU check versus smallest asoc MTU goes
3894 				 * here
3895 				 */
3896 				if ((ro->ro_rt != NULL) &&
3897 				    (net->ro._s_addr)) {
3898 					uint32_t mtu;
3899 
3900 					mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3901 					if (net->port) {
3902 						mtu -= sizeof(struct udphdr);
3903 					}
3904 					if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
3905 						sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3906 						net->mtu = mtu;
3907 					}
3908 				} else if (ro->ro_rt == NULL) {
3909 					/* route was freed */
3910 					if (net->ro._s_addr &&
3911 					    net->src_addr_selected) {
3912 						sctp_free_ifa(net->ro._s_addr);
3913 						net->ro._s_addr = NULL;
3914 					}
3915 					net->src_addr_selected = 0;
3916 				}
3917 			}
3918 			return (ret);
3919 		}
3920 #endif
3921 #ifdef INET6
3922 	case AF_INET6:
3923 		{
3924 			uint32_t flowlabel;
3925 			struct ip6_hdr *ip6h;
3926 			struct route_in6 ip6route;
3927 			struct ifnet *ifp;
3928 			u_char flowTop;
3929 			uint16_t flowBottom;
3930 			u_char tosBottom, tosTop;
3931 			struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
3932 			int prev_scope = 0;
3933 			struct sockaddr_in6 lsa6_storage;
3934 			int error;
3935 			u_short prev_port = 0;
3936 			int len;
3937 
3938 			if (net != NULL) {
3939 				flowlabel = net->tos_flowlabel;
3940 			} else {
3941 				flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
3942 			}
3943 
3944 			len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr);
3945 			if (port) {
3946 				len += sizeof(struct udphdr);
3947 			}
3948 			newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3949 			if (newm == NULL) {
3950 				sctp_m_freem(m);
3951 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3952 				return (ENOMEM);
3953 			}
3954 			SCTP_ALIGN_TO_END(newm, len);
3955 			SCTP_BUF_LEN(newm) = len;
3956 			SCTP_BUF_NEXT(newm) = m;
3957 			m = newm;
3958 			if (net != NULL) {
3959 #ifdef INVARIANTS
3960 				if (net->flowidset == 0) {
3961 					panic("Flow ID not set");
3962 				}
3963 #endif
3964 				m->m_pkthdr.flowid = net->flowid;
3965 				m->m_flags |= M_FLOWID;
3966 			} else {
3967 				if ((init != NULL) && (init->m_flags & M_FLOWID)) {
3968 					m->m_pkthdr.flowid = init->m_pkthdr.flowid;
3969 					m->m_flags |= M_FLOWID;
3970 				}
3971 			}
3972 			packet_length = sctp_calculate_len(m);
3973 
3974 			ip6h = mtod(m, struct ip6_hdr *);
3975 			/*
3976 			 * We assume here that inp_flow is in host byte
3977 			 * order within the TCB!
3978 			 */
3979 			flowBottom = flowlabel & 0x0000ffff;
3980 			flowTop = ((flowlabel & 0x000f0000) >> 16);
3981 			tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
3982 			/* protect *sin6 from overwrite */
3983 			sin6 = (struct sockaddr_in6 *)to;
3984 			tmp = *sin6;
3985 			sin6 = &tmp;
3986 
3987 			/* KAME hack: embed scopeid */
3988 			if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3989 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3990 				return (EINVAL);
3991 			}
3992 			if (net == NULL) {
3993 				memset(&ip6route, 0, sizeof(ip6route));
3994 				ro = (sctp_route_t *) & ip6route;
3995 				memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
3996 			} else {
3997 				ro = (sctp_route_t *) & net->ro;
3998 			}
3999 			tosBottom = (((struct in6pcb *)inp)->in6p_flowinfo & 0x0c);
4000 			if (ecn_ok) {
4001 				tosBottom |= sctp_get_ect(stcb, chk);
4002 			}
4003 			tosBottom <<= 4;
4004 			ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
4005 			if (port) {
4006 				ip6h->ip6_nxt = IPPROTO_UDP;
4007 			} else {
4008 				ip6h->ip6_nxt = IPPROTO_SCTP;
4009 			}
4010 			ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
4011 			ip6h->ip6_dst = sin6->sin6_addr;
4012 
4013 			/*
4014 			 * Add SRC address selection here: we can only reuse
4015 			 * to a limited degree the kame src-addr-sel, since
4016 			 * we can try their selection but it may not be
4017 			 * bound.
4018 			 */
4019 			bzero(&lsa6_tmp, sizeof(lsa6_tmp));
4020 			lsa6_tmp.sin6_family = AF_INET6;
4021 			lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
4022 			lsa6 = &lsa6_tmp;
4023 			if (net && out_of_asoc_ok == 0) {
4024 				if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4025 					sctp_free_ifa(net->ro._s_addr);
4026 					net->ro._s_addr = NULL;
4027 					net->src_addr_selected = 0;
4028 					if (ro->ro_rt) {
4029 						RTFREE(ro->ro_rt);
4030 						ro->ro_rt = NULL;
4031 					}
4032 				}
4033 				if (net->src_addr_selected == 0) {
4034 					sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4035 					/* KAME hack: embed scopeid */
4036 					if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4037 						SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4038 						return (EINVAL);
4039 					}
4040 					/* Cache the source address */
4041 					net->ro._s_addr = sctp_source_address_selection(inp,
4042 					    stcb,
4043 					    ro,
4044 					    net,
4045 					    0,
4046 					    vrf_id);
4047 					(void)sa6_recoverscope(sin6);
4048 					net->src_addr_selected = 1;
4049 				}
4050 				if (net->ro._s_addr == NULL) {
4051 					SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
4052 					net->src_addr_selected = 0;
4053 					sctp_handle_no_route(stcb, net, so_locked);
4054 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4055 					sctp_m_freem(m);
4056 					return (EHOSTUNREACH);
4057 				}
4058 				lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
4059 			} else {
4060 				sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
4061 				/* KAME hack: embed scopeid */
4062 				if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4063 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4064 					return (EINVAL);
4065 				}
4066 				if (over_addr == NULL) {
4067 					struct sctp_ifa *_lsrc;
4068 
4069 					_lsrc = sctp_source_address_selection(inp, stcb, ro,
4070 					    net,
4071 					    out_of_asoc_ok,
4072 					    vrf_id);
4073 					if (_lsrc == NULL) {
4074 						sctp_handle_no_route(stcb, net, so_locked);
4075 						SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4076 						sctp_m_freem(m);
4077 						return (EHOSTUNREACH);
4078 					}
4079 					lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
4080 					sctp_free_ifa(_lsrc);
4081 				} else {
4082 					lsa6->sin6_addr = over_addr->sin6.sin6_addr;
4083 					SCTP_RTALLOC(ro, vrf_id);
4084 				}
4085 				(void)sa6_recoverscope(sin6);
4086 			}
4087 			lsa6->sin6_port = inp->sctp_lport;
4088 
4089 			if (ro->ro_rt == NULL) {
4090 				/*
4091 				 * src addr selection failed to find a route
4092 				 * (or valid source addr), so we can't get
4093 				 * there from here!
4094 				 */
4095 				sctp_handle_no_route(stcb, net, so_locked);
4096 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4097 				sctp_m_freem(m);
4098 				return (EHOSTUNREACH);
4099 			}
4100 			/*
4101 			 * XXX: sa6 may not have a valid sin6_scope_id in
4102 			 * the non-SCOPEDROUTING case.
4103 			 */
4104 			bzero(&lsa6_storage, sizeof(lsa6_storage));
4105 			lsa6_storage.sin6_family = AF_INET6;
4106 			lsa6_storage.sin6_len = sizeof(lsa6_storage);
4107 			lsa6_storage.sin6_addr = lsa6->sin6_addr;
4108 			if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
4109 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
4110 				sctp_m_freem(m);
4111 				return (error);
4112 			}
4113 			/* XXX */
4114 			lsa6_storage.sin6_addr = lsa6->sin6_addr;
4115 			lsa6_storage.sin6_port = inp->sctp_lport;
4116 			lsa6 = &lsa6_storage;
4117 			ip6h->ip6_src = lsa6->sin6_addr;
4118 
4119 			if (port) {
4120 				udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4121 				udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4122 				udp->uh_dport = port;
4123 				udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
4124 				udp->uh_sum = 0;
4125 				sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4126 			} else {
4127 				sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4128 			}
4129 
4130 			sctphdr->src_port = src_port;
4131 			sctphdr->dest_port = dest_port;
4132 			sctphdr->v_tag = v_tag;
4133 			sctphdr->checksum = 0;
4134 
4135 			/*
4136 			 * We set the hop limit now since there is a good
4137 			 * chance that our ro pointer is now filled
4138 			 */
4139 			ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
4140 			ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4141 
4142 #ifdef SCTP_DEBUG
4143 			/* Copy to be sure something bad is not happening */
4144 			sin6->sin6_addr = ip6h->ip6_dst;
4145 			lsa6->sin6_addr = ip6h->ip6_src;
4146 #endif
4147 
4148 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
4149 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
4150 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
4151 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
4152 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
4153 			if (net) {
4154 				sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4155 				/*
4156 				 * preserve the port and scope for link
4157 				 * local send
4158 				 */
4159 				prev_scope = sin6->sin6_scope_id;
4160 				prev_port = sin6->sin6_port;
4161 			}
4162 			if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4163 				/* failed to prepend data, give up */
4164 				sctp_m_freem(m);
4165 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4166 				return (ENOMEM);
4167 			}
4168 #ifdef  SCTP_PACKET_LOGGING
4169 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4170 				sctp_packet_log(m, packet_length);
4171 #endif
4172 			SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4173 			if (port) {
4174 #if defined(SCTP_WITH_NO_CSUM)
4175 				SCTP_STAT_INCR(sctps_sendnocrc);
4176 #else
4177 				if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4178 				    (stcb) &&
4179 				    (stcb->asoc.loopback_scope))) {
4180 					sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4181 					SCTP_STAT_INCR(sctps_sendswcrc);
4182 				} else {
4183 					SCTP_STAT_INCR(sctps_sendnocrc);
4184 				}
4185 #endif
4186 				if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4187 					udp->uh_sum = 0xffff;
4188 				}
4189 			} else {
4190 #if defined(SCTP_WITH_NO_CSUM)
4191 				SCTP_STAT_INCR(sctps_sendnocrc);
4192 #else
4193 				m->m_pkthdr.csum_flags = CSUM_SCTP;
4194 				m->m_pkthdr.csum_data = 0;
4195 				SCTP_STAT_INCR(sctps_sendhwcrc);
4196 #endif
4197 			}
4198 			/* send it out. table id is taken from stcb */
4199 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4200 			if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4201 				so = SCTP_INP_SO(inp);
4202 				SCTP_SOCKET_UNLOCK(so, 0);
4203 			}
4204 #endif
4205 			SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4206 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4207 			if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4208 				atomic_add_int(&stcb->asoc.refcnt, 1);
4209 				SCTP_TCB_UNLOCK(stcb);
4210 				SCTP_SOCKET_LOCK(so, 0);
4211 				SCTP_TCB_LOCK(stcb);
4212 				atomic_subtract_int(&stcb->asoc.refcnt, 1);
4213 			}
4214 #endif
4215 			if (net) {
4216 				/* for link local this must be done */
4217 				sin6->sin6_scope_id = prev_scope;
4218 				sin6->sin6_port = prev_port;
4219 			}
4220 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4221 			SCTP_STAT_INCR(sctps_sendpackets);
4222 			SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4223 			if (ret) {
4224 				SCTP_STAT_INCR(sctps_senderrors);
4225 			}
4226 			if (net == NULL) {
4227 				/* Now if we had a temp route free it */
4228 				if (ro->ro_rt) {
4229 					RTFREE(ro->ro_rt);
4230 				}
4231 			} else {
4232 				/*
4233 				 * PMTU check versus smallest asoc MTU goes
4234 				 * here
4235 				 */
4236 				if (ro->ro_rt == NULL) {
4237 					/* Route was freed */
4238 					if (net->ro._s_addr &&
4239 					    net->src_addr_selected) {
4240 						sctp_free_ifa(net->ro._s_addr);
4241 						net->ro._s_addr = NULL;
4242 					}
4243 					net->src_addr_selected = 0;
4244 				}
4245 				if ((ro->ro_rt != NULL) &&
4246 				    (net->ro._s_addr)) {
4247 					uint32_t mtu;
4248 
4249 					mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4250 					if (mtu &&
4251 					    (stcb->asoc.smallest_mtu > mtu)) {
4252 						sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4253 						net->mtu = mtu;
4254 						if (net->port) {
4255 							net->mtu -= sizeof(struct udphdr);
4256 						}
4257 					}
4258 				} else if (ifp) {
4259 					if (ND_IFINFO(ifp)->linkmtu &&
4260 					    (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4261 						sctp_mtu_size_reset(inp,
4262 						    &stcb->asoc,
4263 						    ND_IFINFO(ifp)->linkmtu);
4264 					}
4265 				}
4266 			}
4267 			return (ret);
4268 		}
4269 #endif
4270 	default:
4271 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4272 		    ((struct sockaddr *)to)->sa_family);
4273 		sctp_m_freem(m);
4274 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4275 		return (EFAULT);
4276 	}
4277 }
4278 
4279 
4280 void
4281 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4282 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4283     SCTP_UNUSED
4284 #endif
4285 )
4286 {
4287 	struct mbuf *m, *m_at, *mp_last;
4288 	struct sctp_nets *net;
4289 	struct sctp_init_chunk *init;
4290 	struct sctp_supported_addr_param *sup_addr;
4291 	struct sctp_adaptation_layer_indication *ali;
4292 	struct sctp_ecn_supported_param *ecn;
4293 	struct sctp_prsctp_supported_param *prsctp;
4294 	struct sctp_supported_chunk_types_param *pr_supported;
4295 	int cnt_inits_to = 0;
4296 	int padval, ret;
4297 	int num_ext;
4298 	int p_len;
4299 
4300 	/* INIT's always go to the primary (and usually ONLY address) */
4301 	mp_last = NULL;
4302 	net = stcb->asoc.primary_destination;
4303 	if (net == NULL) {
4304 		net = TAILQ_FIRST(&stcb->asoc.nets);
4305 		if (net == NULL) {
4306 			/* TSNH */
4307 			return;
4308 		}
4309 		/* we confirm any address we send an INIT to */
4310 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4311 		(void)sctp_set_primary_addr(stcb, NULL, net);
4312 	} else {
4313 		/* we confirm any address we send an INIT to */
4314 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4315 	}
4316 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4317 #ifdef INET6
4318 	if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4319 		/*
4320 		 * special hook, if we are sending to link local it will not
4321 		 * show up in our private address count.
4322 		 */
4323 		struct sockaddr_in6 *sin6l;
4324 
4325 		sin6l = &net->ro._l_addr.sin6;
4326 		if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4327 			cnt_inits_to = 1;
4328 	}
4329 #endif
4330 	if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4331 		/* This case should not happen */
4332 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4333 		return;
4334 	}
4335 	/* start the INIT timer */
4336 	sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4337 
4338 	m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4339 	if (m == NULL) {
4340 		/* No memory, INIT timer will re-attempt. */
4341 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4342 		return;
4343 	}
4344 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4345 	/*
4346 	 * assume peer supports asconf in order to be able to queue local
4347 	 * address changes while an INIT is in flight and before the assoc
4348 	 * is established.
4349 	 */
4350 	stcb->asoc.peer_supports_asconf = 1;
4351 	/* Now lets put the SCTP header in place */
4352 	init = mtod(m, struct sctp_init_chunk *);
4353 	/* now the chunk header */
4354 	init->ch.chunk_type = SCTP_INITIATION;
4355 	init->ch.chunk_flags = 0;
4356 	/* fill in later from mbuf we build */
4357 	init->ch.chunk_length = 0;
4358 	/* place in my tag */
4359 	init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4360 	/* set up some of the credits. */
4361 	init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4362 	    SCTP_MINIMAL_RWND));
4363 
4364 	init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4365 	init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4366 	init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4367 	/* now the address restriction */
4368 	/* XXX Should we take the address family of the socket into account? */
4369 	sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init +
4370 	    sizeof(*init));
4371 	sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4372 #ifdef INET6
4373 #ifdef INET
4374 	/* we support 2 types: IPv4/IPv6 */
4375 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
4376 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4377 	sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4378 #else
4379 	/* we support 1 type: IPv6 */
4380 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
4381 	sup_addr->addr_type[0] = htons(SCTP_IPV6_ADDRESS);
4382 	sup_addr->addr_type[1] = htons(0);	/* this is the padding */
4383 #endif
4384 #else
4385 	/* we support 1 type: IPv4 */
4386 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
4387 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4388 	sup_addr->addr_type[1] = htons(0);	/* this is the padding */
4389 #endif
4390 	SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
4391 	/* adaptation layer indication parameter */
4392 	ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
4393 	ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4394 	ali->ph.param_length = htons(sizeof(*ali));
4395 	ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4396 	SCTP_BUF_LEN(m) += sizeof(*ali);
4397 	ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
4398 
4399 	if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4400 		/* Add NAT friendly parameter */
4401 		struct sctp_paramhdr *ph;
4402 
4403 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4404 		ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4405 		ph->param_length = htons(sizeof(struct sctp_paramhdr));
4406 		SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
4407 		ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph));
4408 	}
4409 	/* now any cookie time extensions */
4410 	if (stcb->asoc.cookie_preserve_req) {
4411 		struct sctp_cookie_perserve_param *cookie_preserve;
4412 
4413 		cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4414 		cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4415 		cookie_preserve->ph.param_length = htons(
4416 		    sizeof(*cookie_preserve));
4417 		cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4418 		SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4419 		ecn = (struct sctp_ecn_supported_param *)(
4420 		    (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4421 		stcb->asoc.cookie_preserve_req = 0;
4422 	}
4423 	/* ECN parameter */
4424 	if (stcb->asoc.ecn_allowed == 1) {
4425 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4426 		ecn->ph.param_length = htons(sizeof(*ecn));
4427 		SCTP_BUF_LEN(m) += sizeof(*ecn);
4428 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4429 		    sizeof(*ecn));
4430 	} else {
4431 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4432 	}
4433 	/* And now tell the peer we do pr-sctp */
4434 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4435 	prsctp->ph.param_length = htons(sizeof(*prsctp));
4436 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
4437 
4438 	/* And now tell the peer we do all the extensions */
4439 	pr_supported = (struct sctp_supported_chunk_types_param *)
4440 	    ((caddr_t)prsctp + sizeof(*prsctp));
4441 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4442 	num_ext = 0;
4443 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4444 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4445 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4446 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4447 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4448 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4449 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4450 	}
4451 	if (stcb->asoc.sctp_nr_sack_on_off == 1) {
4452 		pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4453 	}
4454 	p_len = sizeof(*pr_supported) + num_ext;
4455 	pr_supported->ph.param_length = htons(p_len);
4456 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4457 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4458 
4459 
4460 	/* add authentication parameters */
4461 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4462 		struct sctp_auth_random *randp;
4463 		struct sctp_auth_hmac_algo *hmacs;
4464 		struct sctp_auth_chunk_list *chunks;
4465 
4466 		/* attach RANDOM parameter, if available */
4467 		if (stcb->asoc.authinfo.random != NULL) {
4468 			randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4469 			p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4470 			/* random key already contains the header */
4471 			bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4472 			/* zero out any padding required */
4473 			bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4474 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4475 		}
4476 		/* add HMAC_ALGO parameter */
4477 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4478 		p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4479 		    (uint8_t *) hmacs->hmac_ids);
4480 		if (p_len > 0) {
4481 			p_len += sizeof(*hmacs);
4482 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4483 			hmacs->ph.param_length = htons(p_len);
4484 			/* zero out any padding required */
4485 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4486 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4487 		}
4488 		/* add CHUNKS parameter */
4489 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4490 		p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4491 		    chunks->chunk_types);
4492 		if (p_len > 0) {
4493 			p_len += sizeof(*chunks);
4494 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4495 			chunks->ph.param_length = htons(p_len);
4496 			/* zero out any padding required */
4497 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4498 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4499 		}
4500 	}
4501 	m_at = m;
4502 	/* now the addresses */
4503 	{
4504 		struct sctp_scoping scp;
4505 
4506 		/*
4507 		 * To optimize this we could put the scoping stuff into a
4508 		 * structure and remove the individual uint8's from the
4509 		 * assoc structure. Then we could just sifa in the address
4510 		 * within the stcb.. but for now this is a quick hack to get
4511 		 * the address stuff teased apart.
4512 		 */
4513 		scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4514 		scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4515 		scp.loopback_scope = stcb->asoc.loopback_scope;
4516 		scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4517 		scp.local_scope = stcb->asoc.local_scope;
4518 		scp.site_scope = stcb->asoc.site_scope;
4519 
4520 		m_at = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_at, cnt_inits_to);
4521 	}
4522 
4523 	/* calulate the size and update pkt header and chunk header */
4524 	p_len = 0;
4525 	for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4526 		if (SCTP_BUF_NEXT(m_at) == NULL)
4527 			mp_last = m_at;
4528 		p_len += SCTP_BUF_LEN(m_at);
4529 	}
4530 	init->ch.chunk_length = htons(p_len);
4531 	/*
4532 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4533 	 * here since the timer will drive a retranmission.
4534 	 */
4535 
4536 	/* I don't expect this to execute but we will be safe here */
4537 	padval = p_len % 4;
4538 	if ((padval) && (mp_last)) {
4539 		/*
4540 		 * The compiler worries that mp_last may not be set even
4541 		 * though I think it is impossible :-> however we add
4542 		 * mp_last here just in case.
4543 		 */
4544 		ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4545 		if (ret) {
4546 			/* Houston we have a problem, no space */
4547 			sctp_m_freem(m);
4548 			return;
4549 		}
4550 		p_len += padval;
4551 	}
4552 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4553 	ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4554 	    (struct sockaddr *)&net->ro._l_addr,
4555 	    m, 0, NULL, 0, 0, 0, NULL, 0,
4556 	    inp->sctp_lport, stcb->rport, htonl(0),
4557 	    net->port, so_locked, NULL, NULL);
4558 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4559 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4560 	(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4561 }
4562 
4563 struct mbuf *
4564 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4565     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
4566 {
4567 	/*
4568 	 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4569 	 * being equal to the beginning of the params i.e. (iphlen +
4570 	 * sizeof(struct sctp_init_msg) parse through the parameters to the
4571 	 * end of the mbuf verifying that all parameters are known.
4572 	 *
4573 	 * For unknown parameters build and return a mbuf with
4574 	 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4575 	 * processing this chunk stop, and set *abort_processing to 1.
4576 	 *
4577 	 * By having param_offset be pre-set to where parameters begin it is
4578 	 * hoped that this routine may be reused in the future by new
4579 	 * features.
4580 	 */
4581 	struct sctp_paramhdr *phdr, params;
4582 
4583 	struct mbuf *mat, *op_err;
4584 	char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4585 	int at, limit, pad_needed;
4586 	uint16_t ptype, plen, padded_size;
4587 	int err_at;
4588 
4589 	*abort_processing = 0;
4590 	mat = in_initpkt;
4591 	err_at = 0;
4592 	limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4593 	at = param_offset;
4594 	op_err = NULL;
4595 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4596 	phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4597 	while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4598 		ptype = ntohs(phdr->param_type);
4599 		plen = ntohs(phdr->param_length);
4600 		if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4601 			/* wacked parameter */
4602 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4603 			goto invalid_size;
4604 		}
4605 		limit -= SCTP_SIZE32(plen);
4606 		/*-
4607 		 * All parameters for all chunks that we know/understand are
4608 		 * listed here. We process them other places and make
4609 		 * appropriate stop actions per the upper bits. However this
4610 		 * is the generic routine processor's can call to get back
4611 		 * an operr.. to either incorporate (init-ack) or send.
4612 		 */
4613 		padded_size = SCTP_SIZE32(plen);
4614 		switch (ptype) {
4615 			/* Param's with variable size */
4616 		case SCTP_HEARTBEAT_INFO:
4617 		case SCTP_STATE_COOKIE:
4618 		case SCTP_UNRECOG_PARAM:
4619 		case SCTP_ERROR_CAUSE_IND:
4620 			/* ok skip fwd */
4621 			at += padded_size;
4622 			break;
4623 			/* Param's with variable size within a range */
4624 		case SCTP_CHUNK_LIST:
4625 		case SCTP_SUPPORTED_CHUNK_EXT:
4626 			if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4627 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4628 				goto invalid_size;
4629 			}
4630 			at += padded_size;
4631 			break;
4632 		case SCTP_SUPPORTED_ADDRTYPE:
4633 			if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4634 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4635 				goto invalid_size;
4636 			}
4637 			at += padded_size;
4638 			break;
4639 		case SCTP_RANDOM:
4640 			if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4641 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4642 				goto invalid_size;
4643 			}
4644 			at += padded_size;
4645 			break;
4646 		case SCTP_SET_PRIM_ADDR:
4647 		case SCTP_DEL_IP_ADDRESS:
4648 		case SCTP_ADD_IP_ADDRESS:
4649 			if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4650 			    (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4651 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4652 				goto invalid_size;
4653 			}
4654 			at += padded_size;
4655 			break;
4656 			/* Param's with a fixed size */
4657 		case SCTP_IPV4_ADDRESS:
4658 			if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4659 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4660 				goto invalid_size;
4661 			}
4662 			at += padded_size;
4663 			break;
4664 		case SCTP_IPV6_ADDRESS:
4665 			if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4666 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4667 				goto invalid_size;
4668 			}
4669 			at += padded_size;
4670 			break;
4671 		case SCTP_COOKIE_PRESERVE:
4672 			if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4673 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4674 				goto invalid_size;
4675 			}
4676 			at += padded_size;
4677 			break;
4678 		case SCTP_HAS_NAT_SUPPORT:
4679 			*nat_friendly = 1;
4680 			/* fall through */
4681 		case SCTP_PRSCTP_SUPPORTED:
4682 
4683 			if (padded_size != sizeof(struct sctp_paramhdr)) {
4684 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen);
4685 				goto invalid_size;
4686 			}
4687 			at += padded_size;
4688 			break;
4689 		case SCTP_ECN_CAPABLE:
4690 			if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4691 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4692 				goto invalid_size;
4693 			}
4694 			at += padded_size;
4695 			break;
4696 		case SCTP_ULP_ADAPTATION:
4697 			if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4698 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4699 				goto invalid_size;
4700 			}
4701 			at += padded_size;
4702 			break;
4703 		case SCTP_SUCCESS_REPORT:
4704 			if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
4705 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
4706 				goto invalid_size;
4707 			}
4708 			at += padded_size;
4709 			break;
4710 		case SCTP_HOSTNAME_ADDRESS:
4711 			{
4712 				/* We can NOT handle HOST NAME addresses!! */
4713 				int l_len;
4714 
4715 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
4716 				*abort_processing = 1;
4717 				if (op_err == NULL) {
4718 					/* Ok need to try to get a mbuf */
4719 #ifdef INET6
4720 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4721 #else
4722 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4723 #endif
4724 					l_len += plen;
4725 					l_len += sizeof(struct sctp_paramhdr);
4726 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4727 					if (op_err) {
4728 						SCTP_BUF_LEN(op_err) = 0;
4729 						/*
4730 						 * pre-reserve space for ip
4731 						 * and sctp header  and
4732 						 * chunk hdr
4733 						 */
4734 #ifdef INET6
4735 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4736 #else
4737 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4738 #endif
4739 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4740 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4741 					}
4742 				}
4743 				if (op_err) {
4744 					/* If we have space */
4745 					struct sctp_paramhdr s;
4746 
4747 					if (err_at % 4) {
4748 						uint32_t cpthis = 0;
4749 
4750 						pad_needed = 4 - (err_at % 4);
4751 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4752 						err_at += pad_needed;
4753 					}
4754 					s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
4755 					s.param_length = htons(sizeof(s) + plen);
4756 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4757 					err_at += sizeof(s);
4758 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4759 					if (phdr == NULL) {
4760 						sctp_m_freem(op_err);
4761 						/*
4762 						 * we are out of memory but
4763 						 * we still need to have a
4764 						 * look at what to do (the
4765 						 * system is in trouble
4766 						 * though).
4767 						 */
4768 						return (NULL);
4769 					}
4770 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4771 					err_at += plen;
4772 				}
4773 				return (op_err);
4774 				break;
4775 			}
4776 		default:
4777 			/*
4778 			 * we do not recognize the parameter figure out what
4779 			 * we do.
4780 			 */
4781 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
4782 			if ((ptype & 0x4000) == 0x4000) {
4783 				/* Report bit is set?? */
4784 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
4785 				if (op_err == NULL) {
4786 					int l_len;
4787 
4788 					/* Ok need to try to get an mbuf */
4789 #ifdef INET6
4790 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4791 #else
4792 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4793 #endif
4794 					l_len += plen;
4795 					l_len += sizeof(struct sctp_paramhdr);
4796 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4797 					if (op_err) {
4798 						SCTP_BUF_LEN(op_err) = 0;
4799 #ifdef INET6
4800 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4801 #else
4802 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4803 #endif
4804 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4805 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4806 					}
4807 				}
4808 				if (op_err) {
4809 					/* If we have space */
4810 					struct sctp_paramhdr s;
4811 
4812 					if (err_at % 4) {
4813 						uint32_t cpthis = 0;
4814 
4815 						pad_needed = 4 - (err_at % 4);
4816 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4817 						err_at += pad_needed;
4818 					}
4819 					s.param_type = htons(SCTP_UNRECOG_PARAM);
4820 					s.param_length = htons(sizeof(s) + plen);
4821 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4822 					err_at += sizeof(s);
4823 					if (plen > sizeof(tempbuf)) {
4824 						plen = sizeof(tempbuf);
4825 					}
4826 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4827 					if (phdr == NULL) {
4828 						sctp_m_freem(op_err);
4829 						/*
4830 						 * we are out of memory but
4831 						 * we still need to have a
4832 						 * look at what to do (the
4833 						 * system is in trouble
4834 						 * though).
4835 						 */
4836 						op_err = NULL;
4837 						goto more_processing;
4838 					}
4839 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4840 					err_at += plen;
4841 				}
4842 			}
4843 	more_processing:
4844 			if ((ptype & 0x8000) == 0x0000) {
4845 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
4846 				return (op_err);
4847 			} else {
4848 				/* skip this chunk and continue processing */
4849 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
4850 				at += SCTP_SIZE32(plen);
4851 			}
4852 			break;
4853 
4854 		}
4855 		phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4856 	}
4857 	return (op_err);
4858 invalid_size:
4859 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
4860 	*abort_processing = 1;
4861 	if ((op_err == NULL) && phdr) {
4862 		int l_len;
4863 
4864 #ifdef INET6
4865 		l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4866 #else
4867 		l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4868 #endif
4869 		l_len += (2 * sizeof(struct sctp_paramhdr));
4870 		op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4871 		if (op_err) {
4872 			SCTP_BUF_LEN(op_err) = 0;
4873 #ifdef INET6
4874 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4875 #else
4876 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4877 #endif
4878 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4879 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4880 		}
4881 	}
4882 	if ((op_err) && phdr) {
4883 		struct sctp_paramhdr s;
4884 
4885 		if (err_at % 4) {
4886 			uint32_t cpthis = 0;
4887 
4888 			pad_needed = 4 - (err_at % 4);
4889 			m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4890 			err_at += pad_needed;
4891 		}
4892 		s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4893 		s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
4894 		m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4895 		err_at += sizeof(s);
4896 		/* Only copy back the p-hdr that caused the issue */
4897 		m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
4898 	}
4899 	return (op_err);
4900 }
4901 
4902 static int
4903 sctp_are_there_new_addresses(struct sctp_association *asoc,
4904     struct mbuf *in_initpkt, int iphlen, int offset)
4905 {
4906 	/*
4907 	 * Given a INIT packet, look through the packet to verify that there
4908 	 * are NO new addresses. As we go through the parameters add reports
4909 	 * of any un-understood parameters that require an error.  Also we
4910 	 * must return (1) to drop the packet if we see a un-understood
4911 	 * parameter that tells us to drop the chunk.
4912 	 */
4913 	struct sockaddr *sa_touse;
4914 	struct sockaddr *sa;
4915 	struct sctp_paramhdr *phdr, params;
4916 	uint16_t ptype, plen;
4917 	uint8_t fnd;
4918 	struct sctp_nets *net;
4919 	struct ip *iph;
4920 
4921 #ifdef INET
4922 	struct sockaddr_in sin4, *sa4;
4923 
4924 #endif
4925 #ifdef INET6
4926 	struct sockaddr_in6 sin6, *sa6;
4927 	struct ip6_hdr *ip6h;
4928 
4929 #endif
4930 
4931 #ifdef INET
4932 	memset(&sin4, 0, sizeof(sin4));
4933 	sin4.sin_family = AF_INET;
4934 	sin4.sin_len = sizeof(sin4);
4935 #endif
4936 #ifdef INET6
4937 	memset(&sin6, 0, sizeof(sin6));
4938 	sin6.sin6_family = AF_INET6;
4939 	sin6.sin6_len = sizeof(sin6);
4940 #endif
4941 	sa_touse = NULL;
4942 	/* First what about the src address of the pkt ? */
4943 	iph = mtod(in_initpkt, struct ip *);
4944 	switch (iph->ip_v) {
4945 #ifdef INET
4946 	case IPVERSION:
4947 		/* source addr is IPv4 */
4948 		sin4.sin_addr = iph->ip_src;
4949 		sa_touse = (struct sockaddr *)&sin4;
4950 		break;
4951 #endif
4952 #ifdef INET6
4953 	case IPV6_VERSION >> 4:
4954 		/* source addr is IPv6 */
4955 		ip6h = mtod(in_initpkt, struct ip6_hdr *);
4956 		sin6.sin6_addr = ip6h->ip6_src;
4957 		sa_touse = (struct sockaddr *)&sin6;
4958 		break;
4959 #endif
4960 	default:
4961 		return (1);
4962 	}
4963 
4964 	fnd = 0;
4965 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4966 		sa = (struct sockaddr *)&net->ro._l_addr;
4967 		if (sa->sa_family == sa_touse->sa_family) {
4968 #ifdef INET
4969 			if (sa->sa_family == AF_INET) {
4970 				sa4 = (struct sockaddr_in *)sa;
4971 				if (sa4->sin_addr.s_addr == sin4.sin_addr.s_addr) {
4972 					fnd = 1;
4973 					break;
4974 				}
4975 			}
4976 #endif
4977 #ifdef INET6
4978 			if (sa->sa_family == AF_INET6) {
4979 				sa6 = (struct sockaddr_in6 *)sa;
4980 				if (SCTP6_ARE_ADDR_EQUAL(sa6, &sin6)) {
4981 					fnd = 1;
4982 					break;
4983 				}
4984 			}
4985 #endif
4986 		}
4987 	}
4988 	if (fnd == 0) {
4989 		/* New address added! no need to look futher. */
4990 		return (1);
4991 	}
4992 	/* Ok so far lets munge through the rest of the packet */
4993 	offset += sizeof(struct sctp_init_chunk);
4994 	phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
4995 	while (phdr) {
4996 		sa_touse = NULL;
4997 		ptype = ntohs(phdr->param_type);
4998 		plen = ntohs(phdr->param_length);
4999 		switch (ptype) {
5000 #ifdef INET
5001 		case SCTP_IPV4_ADDRESS:
5002 			{
5003 				struct sctp_ipv4addr_param *p4, p4_buf;
5004 
5005 				phdr = sctp_get_next_param(in_initpkt, offset,
5006 				    (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
5007 				if (plen != sizeof(struct sctp_ipv4addr_param) ||
5008 				    phdr == NULL) {
5009 					return (1);
5010 				}
5011 				p4 = (struct sctp_ipv4addr_param *)phdr;
5012 				sin4.sin_addr.s_addr = p4->addr;
5013 				sa_touse = (struct sockaddr *)&sin4;
5014 			}
5015 #endif
5016 #ifdef INET6
5017 		case SCTP_IPV6_ADDRESS:
5018 			{
5019 				struct sctp_ipv6addr_param *p6, p6_buf;
5020 
5021 				phdr = sctp_get_next_param(in_initpkt, offset,
5022 				    (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
5023 				if (plen != sizeof(struct sctp_ipv6addr_param) ||
5024 				    phdr == NULL) {
5025 					return (1);
5026 				}
5027 				p6 = (struct sctp_ipv6addr_param *)phdr;
5028 				memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
5029 				    sizeof(p6->addr));
5030 				sa_touse = (struct sockaddr *)&sin6;
5031 			}
5032 #endif
5033 		default:
5034 			sa_touse = NULL;
5035 		}
5036 		if (sa_touse) {
5037 			/* ok, sa_touse points to one to check */
5038 			fnd = 0;
5039 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5040 				sa = (struct sockaddr *)&net->ro._l_addr;
5041 				if (sa->sa_family != sa_touse->sa_family) {
5042 					continue;
5043 				}
5044 #ifdef INET
5045 				if (sa->sa_family == AF_INET) {
5046 					sa4 = (struct sockaddr_in *)sa;
5047 					if (sa4->sin_addr.s_addr ==
5048 					    sin4.sin_addr.s_addr) {
5049 						fnd = 1;
5050 						break;
5051 					}
5052 				}
5053 #endif
5054 #ifdef INET6
5055 				if (sa->sa_family == AF_INET6) {
5056 					sa6 = (struct sockaddr_in6 *)sa;
5057 					if (SCTP6_ARE_ADDR_EQUAL(
5058 					    sa6, &sin6)) {
5059 						fnd = 1;
5060 						break;
5061 					}
5062 				}
5063 #endif
5064 			}
5065 			if (!fnd) {
5066 				/* New addr added! no need to look further */
5067 				return (1);
5068 			}
5069 		}
5070 		offset += SCTP_SIZE32(plen);
5071 		phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
5072 	}
5073 	return (0);
5074 }
5075 
5076 /*
5077  * Given a MBUF chain that was sent into us containing an INIT. Build a
5078  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
5079  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
5080  * message (i.e. the struct sctp_init_msg).
5081  */
5082 void
5083 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
5084     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
5085     struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
5086 {
5087 	struct sctp_association *asoc;
5088 	struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
5089 	struct sctp_init_ack_chunk *initack;
5090 	struct sctp_adaptation_layer_indication *ali;
5091 	struct sctp_ecn_supported_param *ecn;
5092 	struct sctp_prsctp_supported_param *prsctp;
5093 	struct sctp_supported_chunk_types_param *pr_supported;
5094 	union sctp_sockstore store, store1, *over_addr;
5095 
5096 #ifdef INET
5097 	struct sockaddr_in *sin, *to_sin;
5098 
5099 #endif
5100 #ifdef INET6
5101 	struct sockaddr_in6 *sin6, *to_sin6;
5102 
5103 #endif
5104 	struct ip *iph;
5105 
5106 #ifdef INET6
5107 	struct ip6_hdr *ip6;
5108 
5109 #endif
5110 	struct sockaddr *to;
5111 	struct sctp_state_cookie stc;
5112 	struct sctp_nets *net = NULL;
5113 	uint8_t *signature = NULL;
5114 	int cnt_inits_to = 0;
5115 	uint16_t his_limit, i_want;
5116 	int abort_flag, padval;
5117 	int num_ext;
5118 	int p_len;
5119 	int nat_friendly = 0;
5120 	struct socket *so;
5121 
5122 	if (stcb)
5123 		asoc = &stcb->asoc;
5124 	else
5125 		asoc = NULL;
5126 	mp_last = NULL;
5127 	if ((asoc != NULL) &&
5128 	    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
5129 	    (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
5130 		/* new addresses, out of here in non-cookie-wait states */
5131 		/*
5132 		 * Send a ABORT, we don't add the new address error clause
5133 		 * though we even set the T bit and copy in the 0 tag.. this
5134 		 * looks no different than if no listener was present.
5135 		 */
5136 		sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
5137 		return;
5138 	}
5139 	abort_flag = 0;
5140 	op_err = sctp_arethere_unrecognized_parameters(init_pkt,
5141 	    (offset + sizeof(struct sctp_init_chunk)),
5142 	    &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
5143 	if (abort_flag) {
5144 do_a_abort:
5145 		sctp_send_abort(init_pkt, iphlen, sh,
5146 		    init_chk->init.initiate_tag, op_err, vrf_id, port);
5147 		return;
5148 	}
5149 	m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
5150 	if (m == NULL) {
5151 		/* No memory, INIT timer will re-attempt. */
5152 		if (op_err)
5153 			sctp_m_freem(op_err);
5154 		return;
5155 	}
5156 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
5157 
5158 	/* the time I built cookie */
5159 	(void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
5160 
5161 	/* populate any tie tags */
5162 	if (asoc != NULL) {
5163 		/* unlock before tag selections */
5164 		stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5165 		stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5166 		stc.cookie_life = asoc->cookie_life;
5167 		net = asoc->primary_destination;
5168 	} else {
5169 		stc.tie_tag_my_vtag = 0;
5170 		stc.tie_tag_peer_vtag = 0;
5171 		/* life I will award this cookie */
5172 		stc.cookie_life = inp->sctp_ep.def_cookie_life;
5173 	}
5174 
5175 	/* copy in the ports for later check */
5176 	stc.myport = sh->dest_port;
5177 	stc.peerport = sh->src_port;
5178 
5179 	/*
5180 	 * If we wanted to honor cookie life extentions, we would add to
5181 	 * stc.cookie_life. For now we should NOT honor any extension
5182 	 */
5183 	stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5184 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5185 		struct inpcb *in_inp;
5186 
5187 		/* Its a V6 socket */
5188 		in_inp = (struct inpcb *)inp;
5189 		stc.ipv6_addr_legal = 1;
5190 		/* Now look at the binding flag to see if V4 will be legal */
5191 		if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
5192 			stc.ipv4_addr_legal = 1;
5193 		} else {
5194 			/* V4 addresses are NOT legal on the association */
5195 			stc.ipv4_addr_legal = 0;
5196 		}
5197 	} else {
5198 		/* Its a V4 socket, no - V6 */
5199 		stc.ipv4_addr_legal = 1;
5200 		stc.ipv6_addr_legal = 0;
5201 	}
5202 
5203 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
5204 	stc.ipv4_scope = 1;
5205 #else
5206 	stc.ipv4_scope = 0;
5207 #endif
5208 	/* now for scope setup */
5209 	memset((caddr_t)&store, 0, sizeof(store));
5210 	memset((caddr_t)&store1, 0, sizeof(store1));
5211 #ifdef INET
5212 	sin = &store.sin;
5213 	to_sin = &store1.sin;
5214 #endif
5215 #ifdef INET6
5216 	sin6 = &store.sin6;
5217 	to_sin6 = &store1.sin6;
5218 #endif
5219 	iph = mtod(init_pkt, struct ip *);
5220 	/* establish the to_addr's */
5221 	switch (iph->ip_v) {
5222 #ifdef INET
5223 	case IPVERSION:
5224 		to_sin->sin_port = sh->dest_port;
5225 		to_sin->sin_family = AF_INET;
5226 		to_sin->sin_len = sizeof(struct sockaddr_in);
5227 		to_sin->sin_addr = iph->ip_dst;
5228 		break;
5229 #endif
5230 #ifdef INET6
5231 	case IPV6_VERSION >> 4:
5232 		ip6 = mtod(init_pkt, struct ip6_hdr *);
5233 		to_sin6->sin6_addr = ip6->ip6_dst;
5234 		to_sin6->sin6_scope_id = 0;
5235 		to_sin6->sin6_port = sh->dest_port;
5236 		to_sin6->sin6_family = AF_INET6;
5237 		to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5238 		break;
5239 #endif
5240 	default:
5241 		goto do_a_abort;
5242 		break;
5243 	};
5244 
5245 	if (net == NULL) {
5246 		to = (struct sockaddr *)&store;
5247 		switch (iph->ip_v) {
5248 #ifdef INET
5249 		case IPVERSION:
5250 			{
5251 				sin->sin_family = AF_INET;
5252 				sin->sin_len = sizeof(struct sockaddr_in);
5253 				sin->sin_port = sh->src_port;
5254 				sin->sin_addr = iph->ip_src;
5255 				/* lookup address */
5256 				stc.address[0] = sin->sin_addr.s_addr;
5257 				stc.address[1] = 0;
5258 				stc.address[2] = 0;
5259 				stc.address[3] = 0;
5260 				stc.addr_type = SCTP_IPV4_ADDRESS;
5261 				/* local from address */
5262 				stc.laddress[0] = to_sin->sin_addr.s_addr;
5263 				stc.laddress[1] = 0;
5264 				stc.laddress[2] = 0;
5265 				stc.laddress[3] = 0;
5266 				stc.laddr_type = SCTP_IPV4_ADDRESS;
5267 				/* scope_id is only for v6 */
5268 				stc.scope_id = 0;
5269 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5270 				if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5271 					stc.ipv4_scope = 1;
5272 				}
5273 #else
5274 				stc.ipv4_scope = 1;
5275 #endif				/* SCTP_DONT_DO_PRIVADDR_SCOPE */
5276 				/* Must use the address in this case */
5277 				if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5278 					stc.loopback_scope = 1;
5279 					stc.ipv4_scope = 1;
5280 					stc.site_scope = 1;
5281 					stc.local_scope = 0;
5282 				}
5283 				break;
5284 			}
5285 #endif
5286 #ifdef INET6
5287 		case IPV6_VERSION >> 4:
5288 			{
5289 				ip6 = mtod(init_pkt, struct ip6_hdr *);
5290 				sin6->sin6_family = AF_INET6;
5291 				sin6->sin6_len = sizeof(struct sockaddr_in6);
5292 				sin6->sin6_port = sh->src_port;
5293 				sin6->sin6_addr = ip6->ip6_src;
5294 				/* lookup address */
5295 				memcpy(&stc.address, &sin6->sin6_addr,
5296 				    sizeof(struct in6_addr));
5297 				sin6->sin6_scope_id = 0;
5298 				stc.addr_type = SCTP_IPV6_ADDRESS;
5299 				stc.scope_id = 0;
5300 				if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5301 					/*
5302 					 * FIX ME: does this have scope from
5303 					 * rcvif?
5304 					 */
5305 					(void)sa6_recoverscope(sin6);
5306 					stc.scope_id = sin6->sin6_scope_id;
5307 					sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5308 					stc.loopback_scope = 1;
5309 					stc.local_scope = 0;
5310 					stc.site_scope = 1;
5311 					stc.ipv4_scope = 1;
5312 				} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5313 					/*
5314 					 * If the new destination is a
5315 					 * LINK_LOCAL we must have common
5316 					 * both site and local scope. Don't
5317 					 * set local scope though since we
5318 					 * must depend on the source to be
5319 					 * added implicitly. We cannot
5320 					 * assure just because we share one
5321 					 * link that all links are common.
5322 					 */
5323 					stc.local_scope = 0;
5324 					stc.site_scope = 1;
5325 					stc.ipv4_scope = 1;
5326 					/*
5327 					 * we start counting for the private
5328 					 * address stuff at 1. since the
5329 					 * link local we source from won't
5330 					 * show up in our scoped count.
5331 					 */
5332 					cnt_inits_to = 1;
5333 					/*
5334 					 * pull out the scope_id from
5335 					 * incoming pkt
5336 					 */
5337 					/*
5338 					 * FIX ME: does this have scope from
5339 					 * rcvif?
5340 					 */
5341 					(void)sa6_recoverscope(sin6);
5342 					stc.scope_id = sin6->sin6_scope_id;
5343 					sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5344 				} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5345 					/*
5346 					 * If the new destination is
5347 					 * SITE_LOCAL then we must have site
5348 					 * scope in common.
5349 					 */
5350 					stc.site_scope = 1;
5351 				}
5352 				memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5353 				stc.laddr_type = SCTP_IPV6_ADDRESS;
5354 				break;
5355 			}
5356 #endif
5357 		default:
5358 			/* TSNH */
5359 			goto do_a_abort;
5360 			break;
5361 		}
5362 	} else {
5363 		/* set the scope per the existing tcb */
5364 
5365 #ifdef INET6
5366 		struct sctp_nets *lnet;
5367 
5368 #endif
5369 
5370 		stc.loopback_scope = asoc->loopback_scope;
5371 		stc.ipv4_scope = asoc->ipv4_local_scope;
5372 		stc.site_scope = asoc->site_scope;
5373 		stc.local_scope = asoc->local_scope;
5374 #ifdef INET6
5375 		/* Why do we not consider IPv4 LL addresses? */
5376 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5377 			if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5378 				if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5379 					/*
5380 					 * if we have a LL address, start
5381 					 * counting at 1.
5382 					 */
5383 					cnt_inits_to = 1;
5384 				}
5385 			}
5386 		}
5387 #endif
5388 		/* use the net pointer */
5389 		to = (struct sockaddr *)&net->ro._l_addr;
5390 		switch (to->sa_family) {
5391 #ifdef INET
5392 		case AF_INET:
5393 			sin = (struct sockaddr_in *)to;
5394 			stc.address[0] = sin->sin_addr.s_addr;
5395 			stc.address[1] = 0;
5396 			stc.address[2] = 0;
5397 			stc.address[3] = 0;
5398 			stc.addr_type = SCTP_IPV4_ADDRESS;
5399 			if (net->src_addr_selected == 0) {
5400 				/*
5401 				 * strange case here, the INIT should have
5402 				 * did the selection.
5403 				 */
5404 				net->ro._s_addr = sctp_source_address_selection(inp,
5405 				    stcb, (sctp_route_t *) & net->ro,
5406 				    net, 0, vrf_id);
5407 				if (net->ro._s_addr == NULL)
5408 					return;
5409 
5410 				net->src_addr_selected = 1;
5411 
5412 			}
5413 			stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5414 			stc.laddress[1] = 0;
5415 			stc.laddress[2] = 0;
5416 			stc.laddress[3] = 0;
5417 			stc.laddr_type = SCTP_IPV4_ADDRESS;
5418 			break;
5419 #endif
5420 #ifdef INET6
5421 		case AF_INET6:
5422 			sin6 = (struct sockaddr_in6 *)to;
5423 			memcpy(&stc.address, &sin6->sin6_addr,
5424 			    sizeof(struct in6_addr));
5425 			stc.addr_type = SCTP_IPV6_ADDRESS;
5426 			if (net->src_addr_selected == 0) {
5427 				/*
5428 				 * strange case here, the INIT should have
5429 				 * did the selection.
5430 				 */
5431 				net->ro._s_addr = sctp_source_address_selection(inp,
5432 				    stcb, (sctp_route_t *) & net->ro,
5433 				    net, 0, vrf_id);
5434 				if (net->ro._s_addr == NULL)
5435 					return;
5436 
5437 				net->src_addr_selected = 1;
5438 			}
5439 			memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5440 			    sizeof(struct in6_addr));
5441 			stc.laddr_type = SCTP_IPV6_ADDRESS;
5442 			break;
5443 #endif
5444 		}
5445 	}
5446 	/* Now lets put the SCTP header in place */
5447 	initack = mtod(m, struct sctp_init_ack_chunk *);
5448 	/* Save it off for quick ref */
5449 	stc.peers_vtag = init_chk->init.initiate_tag;
5450 	/* who are we */
5451 	memcpy(stc.identification, SCTP_VERSION_STRING,
5452 	    min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5453 	/* now the chunk header */
5454 	initack->ch.chunk_type = SCTP_INITIATION_ACK;
5455 	initack->ch.chunk_flags = 0;
5456 	/* fill in later from mbuf we build */
5457 	initack->ch.chunk_length = 0;
5458 	/* place in my tag */
5459 	if ((asoc != NULL) &&
5460 	    ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5461 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5462 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5463 		/* re-use the v-tags and init-seq here */
5464 		initack->init.initiate_tag = htonl(asoc->my_vtag);
5465 		initack->init.initial_tsn = htonl(asoc->init_seq_number);
5466 	} else {
5467 		uint32_t vtag, itsn;
5468 
5469 		if (hold_inp_lock) {
5470 			SCTP_INP_INCR_REF(inp);
5471 			SCTP_INP_RUNLOCK(inp);
5472 		}
5473 		if (asoc) {
5474 			atomic_add_int(&asoc->refcnt, 1);
5475 			SCTP_TCB_UNLOCK(stcb);
5476 	new_tag:
5477 			vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5478 			if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5479 				/*
5480 				 * Got a duplicate vtag on some guy behind a
5481 				 * nat make sure we don't use it.
5482 				 */
5483 				goto new_tag;
5484 			}
5485 			initack->init.initiate_tag = htonl(vtag);
5486 			/* get a TSN to use too */
5487 			itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5488 			initack->init.initial_tsn = htonl(itsn);
5489 			SCTP_TCB_LOCK(stcb);
5490 			atomic_add_int(&asoc->refcnt, -1);
5491 		} else {
5492 			vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5493 			initack->init.initiate_tag = htonl(vtag);
5494 			/* get a TSN to use too */
5495 			initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5496 		}
5497 		if (hold_inp_lock) {
5498 			SCTP_INP_RLOCK(inp);
5499 			SCTP_INP_DECR_REF(inp);
5500 		}
5501 	}
5502 	/* save away my tag to */
5503 	stc.my_vtag = initack->init.initiate_tag;
5504 
5505 	/* set up some of the credits. */
5506 	so = inp->sctp_socket;
5507 	if (so == NULL) {
5508 		/* memory problem */
5509 		sctp_m_freem(m);
5510 		return;
5511 	} else {
5512 		initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5513 	}
5514 	/* set what I want */
5515 	his_limit = ntohs(init_chk->init.num_inbound_streams);
5516 	/* choose what I want */
5517 	if (asoc != NULL) {
5518 		if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5519 			i_want = asoc->streamoutcnt;
5520 		} else {
5521 			i_want = inp->sctp_ep.pre_open_stream_count;
5522 		}
5523 	} else {
5524 		i_want = inp->sctp_ep.pre_open_stream_count;
5525 	}
5526 	if (his_limit < i_want) {
5527 		/* I Want more :< */
5528 		initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5529 	} else {
5530 		/* I can have what I want :> */
5531 		initack->init.num_outbound_streams = htons(i_want);
5532 	}
5533 	/* tell him his limt. */
5534 	initack->init.num_inbound_streams =
5535 	    htons(inp->sctp_ep.max_open_streams_intome);
5536 
5537 	/* adaptation layer indication parameter */
5538 	ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack));
5539 	ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5540 	ali->ph.param_length = htons(sizeof(*ali));
5541 	ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5542 	SCTP_BUF_LEN(m) += sizeof(*ali);
5543 	ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
5544 
5545 	/* ECN parameter */
5546 	if (((asoc != NULL) && (asoc->ecn_allowed == 1)) ||
5547 	    (inp->sctp_ecn_enable == 1)) {
5548 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5549 		ecn->ph.param_length = htons(sizeof(*ecn));
5550 		SCTP_BUF_LEN(m) += sizeof(*ecn);
5551 
5552 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5553 		    sizeof(*ecn));
5554 	} else {
5555 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5556 	}
5557 	/* And now tell the peer we do  pr-sctp */
5558 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5559 	prsctp->ph.param_length = htons(sizeof(*prsctp));
5560 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
5561 	if (nat_friendly) {
5562 		/* Add NAT friendly parameter */
5563 		struct sctp_paramhdr *ph;
5564 
5565 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5566 		ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5567 		ph->param_length = htons(sizeof(struct sctp_paramhdr));
5568 		SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
5569 	}
5570 	/* And now tell the peer we do all the extensions */
5571 	pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5572 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5573 	num_ext = 0;
5574 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5575 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5576 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5577 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5578 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5579 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5580 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5581 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
5582 		pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5583 	p_len = sizeof(*pr_supported) + num_ext;
5584 	pr_supported->ph.param_length = htons(p_len);
5585 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5586 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5587 
5588 	/* add authentication parameters */
5589 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5590 		struct sctp_auth_random *randp;
5591 		struct sctp_auth_hmac_algo *hmacs;
5592 		struct sctp_auth_chunk_list *chunks;
5593 		uint16_t random_len;
5594 
5595 		/* generate and add RANDOM parameter */
5596 		random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5597 		randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5598 		randp->ph.param_type = htons(SCTP_RANDOM);
5599 		p_len = sizeof(*randp) + random_len;
5600 		randp->ph.param_length = htons(p_len);
5601 		SCTP_READ_RANDOM(randp->random_data, random_len);
5602 		/* zero out any padding required */
5603 		bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5604 		SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5605 
5606 		/* add HMAC_ALGO parameter */
5607 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5608 		p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5609 		    (uint8_t *) hmacs->hmac_ids);
5610 		if (p_len > 0) {
5611 			p_len += sizeof(*hmacs);
5612 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5613 			hmacs->ph.param_length = htons(p_len);
5614 			/* zero out any padding required */
5615 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5616 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5617 		}
5618 		/* add CHUNKS parameter */
5619 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5620 		p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5621 		    chunks->chunk_types);
5622 		if (p_len > 0) {
5623 			p_len += sizeof(*chunks);
5624 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5625 			chunks->ph.param_length = htons(p_len);
5626 			/* zero out any padding required */
5627 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5628 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5629 		}
5630 	}
5631 	m_at = m;
5632 	/* now the addresses */
5633 	{
5634 		struct sctp_scoping scp;
5635 
5636 		/*
5637 		 * To optimize this we could put the scoping stuff into a
5638 		 * structure and remove the individual uint8's from the stc
5639 		 * structure. Then we could just sifa in the address within
5640 		 * the stc.. but for now this is a quick hack to get the
5641 		 * address stuff teased apart.
5642 		 */
5643 		scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5644 		scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5645 		scp.loopback_scope = stc.loopback_scope;
5646 		scp.ipv4_local_scope = stc.ipv4_scope;
5647 		scp.local_scope = stc.local_scope;
5648 		scp.site_scope = stc.site_scope;
5649 		m_at = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_at, cnt_inits_to);
5650 	}
5651 
5652 	/* tack on the operational error if present */
5653 	if (op_err) {
5654 		struct mbuf *ol;
5655 		int llen;
5656 
5657 		llen = 0;
5658 		ol = op_err;
5659 		while (ol) {
5660 			llen += SCTP_BUF_LEN(ol);
5661 			ol = SCTP_BUF_NEXT(ol);
5662 		}
5663 		if (llen % 4) {
5664 			/* must add a pad to the param */
5665 			uint32_t cpthis = 0;
5666 			int padlen;
5667 
5668 			padlen = 4 - (llen % 4);
5669 			m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5670 		}
5671 		while (SCTP_BUF_NEXT(m_at) != NULL) {
5672 			m_at = SCTP_BUF_NEXT(m_at);
5673 		}
5674 		SCTP_BUF_NEXT(m_at) = op_err;
5675 		while (SCTP_BUF_NEXT(m_at) != NULL) {
5676 			m_at = SCTP_BUF_NEXT(m_at);
5677 		}
5678 	}
5679 	/* pre-calulate the size and update pkt header and chunk header */
5680 	p_len = 0;
5681 	for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5682 		p_len += SCTP_BUF_LEN(m_tmp);
5683 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5684 			/* m_tmp should now point to last one */
5685 			break;
5686 		}
5687 	}
5688 
5689 	/* Now we must build a cookie */
5690 	m_cookie = sctp_add_cookie(inp, init_pkt, offset, m, 0, &stc, &signature);
5691 	if (m_cookie == NULL) {
5692 		/* memory problem */
5693 		sctp_m_freem(m);
5694 		return;
5695 	}
5696 	/* Now append the cookie to the end and update the space/size */
5697 	SCTP_BUF_NEXT(m_tmp) = m_cookie;
5698 
5699 	for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5700 		p_len += SCTP_BUF_LEN(m_tmp);
5701 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5702 			/* m_tmp should now point to last one */
5703 			mp_last = m_tmp;
5704 			break;
5705 		}
5706 	}
5707 	/*
5708 	 * Place in the size, but we don't include the last pad (if any) in
5709 	 * the INIT-ACK.
5710 	 */
5711 	initack->ch.chunk_length = htons(p_len);
5712 
5713 	/*
5714 	 * Time to sign the cookie, we don't sign over the cookie signature
5715 	 * though thus we set trailer.
5716 	 */
5717 	(void)sctp_hmac_m(SCTP_HMAC,
5718 	    (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
5719 	    SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
5720 	    (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
5721 	/*
5722 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
5723 	 * here since the timer will drive a retranmission.
5724 	 */
5725 	padval = p_len % 4;
5726 	if ((padval) && (mp_last)) {
5727 		/* see my previous comments on mp_last */
5728 		int ret;
5729 
5730 		ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
5731 		if (ret) {
5732 			/* Houston we have a problem, no space */
5733 			sctp_m_freem(m);
5734 			return;
5735 		}
5736 		p_len += padval;
5737 	}
5738 	if (stc.loopback_scope) {
5739 		over_addr = &store1;
5740 	} else {
5741 		over_addr = NULL;
5742 	}
5743 
5744 	(void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
5745 	    0, NULL, 0,
5746 	    inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
5747 	    port, SCTP_SO_NOT_LOCKED, over_addr, init_pkt);
5748 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5749 }
5750 
5751 
5752 static void
5753 sctp_prune_prsctp(struct sctp_tcb *stcb,
5754     struct sctp_association *asoc,
5755     struct sctp_sndrcvinfo *srcv,
5756     int dataout)
5757 {
5758 	int freed_spc = 0;
5759 	struct sctp_tmit_chunk *chk, *nchk;
5760 
5761 	SCTP_TCB_LOCK_ASSERT(stcb);
5762 	if ((asoc->peer_supports_prsctp) &&
5763 	    (asoc->sent_queue_cnt_removeable > 0)) {
5764 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5765 			/*
5766 			 * Look for chunks marked with the PR_SCTP flag AND
5767 			 * the buffer space flag. If the one being sent is
5768 			 * equal or greater priority then purge the old one
5769 			 * and free some space.
5770 			 */
5771 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5772 				/*
5773 				 * This one is PR-SCTP AND buffer space
5774 				 * limited type
5775 				 */
5776 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5777 					/*
5778 					 * Lower numbers equates to higher
5779 					 * priority so if the one we are
5780 					 * looking at has a larger or equal
5781 					 * priority we want to drop the data
5782 					 * and NOT retransmit it.
5783 					 */
5784 					if (chk->data) {
5785 						/*
5786 						 * We release the book_size
5787 						 * if the mbuf is here
5788 						 */
5789 						int ret_spc;
5790 						int cause;
5791 
5792 						if (chk->sent > SCTP_DATAGRAM_UNSENT)
5793 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
5794 						else
5795 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
5796 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5797 						    cause,
5798 						    SCTP_SO_LOCKED);
5799 						freed_spc += ret_spc;
5800 						if (freed_spc >= dataout) {
5801 							return;
5802 						}
5803 					}	/* if chunk was present */
5804 				}	/* if of sufficent priority */
5805 			}	/* if chunk has enabled */
5806 		}		/* tailqforeach */
5807 
5808 		TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
5809 			/* Here we must move to the sent queue and mark */
5810 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5811 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5812 					if (chk->data) {
5813 						/*
5814 						 * We release the book_size
5815 						 * if the mbuf is here
5816 						 */
5817 						int ret_spc;
5818 
5819 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5820 						    SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
5821 						    SCTP_SO_LOCKED);
5822 
5823 						freed_spc += ret_spc;
5824 						if (freed_spc >= dataout) {
5825 							return;
5826 						}
5827 					}	/* end if chk->data */
5828 				}	/* end if right class */
5829 			}	/* end if chk pr-sctp */
5830 		}		/* tailqforeachsafe (chk) */
5831 	}			/* if enabled in asoc */
5832 }
5833 
5834 int
5835 sctp_get_frag_point(struct sctp_tcb *stcb,
5836     struct sctp_association *asoc)
5837 {
5838 	int siz, ovh;
5839 
5840 	/*
5841 	 * For endpoints that have both v6 and v4 addresses we must reserve
5842 	 * room for the ipv6 header, for those that are only dealing with V4
5843 	 * we use a larger frag point.
5844 	 */
5845 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5846 		ovh = SCTP_MED_OVERHEAD;
5847 	} else {
5848 		ovh = SCTP_MED_V4_OVERHEAD;
5849 	}
5850 
5851 	if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
5852 		siz = asoc->smallest_mtu - ovh;
5853 	else
5854 		siz = (stcb->asoc.sctp_frag_point - ovh);
5855 	/*
5856 	 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
5857 	 */
5858 	/* A data chunk MUST fit in a cluster */
5859 	/* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
5860 	/* } */
5861 
5862 	/* adjust for an AUTH chunk if DATA requires auth */
5863 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
5864 		siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5865 
5866 	if (siz % 4) {
5867 		/* make it an even word boundary please */
5868 		siz -= (siz % 4);
5869 	}
5870 	return (siz);
5871 }
5872 
5873 static void
5874 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
5875 {
5876 	sp->pr_sctp_on = 0;
5877 	/*
5878 	 * We assume that the user wants PR_SCTP_TTL if the user provides a
5879 	 * positive lifetime but does not specify any PR_SCTP policy. This
5880 	 * is a BAD assumption and causes problems at least with the
5881 	 * U-Vancovers MPI folks. I will change this to be no policy means
5882 	 * NO PR-SCTP.
5883 	 */
5884 	if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
5885 		sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
5886 		sp->pr_sctp_on = 1;
5887 	} else {
5888 		return;
5889 	}
5890 	switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
5891 	case CHUNK_FLAGS_PR_SCTP_BUF:
5892 		/*
5893 		 * Time to live is a priority stored in tv_sec when doing
5894 		 * the buffer drop thing.
5895 		 */
5896 		sp->ts.tv_sec = sp->timetolive;
5897 		sp->ts.tv_usec = 0;
5898 		break;
5899 	case CHUNK_FLAGS_PR_SCTP_TTL:
5900 		{
5901 			struct timeval tv;
5902 
5903 			(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5904 			tv.tv_sec = sp->timetolive / 1000;
5905 			tv.tv_usec = (sp->timetolive * 1000) % 1000000;
5906 			/*
5907 			 * TODO sctp_constants.h needs alternative time
5908 			 * macros when _KERNEL is undefined.
5909 			 */
5910 			timevaladd(&sp->ts, &tv);
5911 		}
5912 		break;
5913 	case CHUNK_FLAGS_PR_SCTP_RTX:
5914 		/*
5915 		 * Time to live is a the number or retransmissions stored in
5916 		 * tv_sec.
5917 		 */
5918 		sp->ts.tv_sec = sp->timetolive;
5919 		sp->ts.tv_usec = 0;
5920 		break;
5921 	default:
5922 		SCTPDBG(SCTP_DEBUG_USRREQ1,
5923 		    "Unknown PR_SCTP policy %u.\n",
5924 		    PR_SCTP_POLICY(sp->sinfo_flags));
5925 		break;
5926 	}
5927 }
5928 
5929 static int
5930 sctp_msg_append(struct sctp_tcb *stcb,
5931     struct sctp_nets *net,
5932     struct mbuf *m,
5933     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5934 {
5935 	int error = 0, holds_lock;
5936 	struct mbuf *at;
5937 	struct sctp_stream_queue_pending *sp = NULL;
5938 	struct sctp_stream_out *strm;
5939 
5940 	/*
5941 	 * Given an mbuf chain, put it into the association send queue and
5942 	 * place it on the wheel
5943 	 */
5944 	holds_lock = hold_stcb_lock;
5945 	if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5946 		/* Invalid stream number */
5947 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5948 		error = EINVAL;
5949 		goto out_now;
5950 	}
5951 	if ((stcb->asoc.stream_locked) &&
5952 	    (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5953 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5954 		error = EINVAL;
5955 		goto out_now;
5956 	}
5957 	strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5958 	/* Now can we send this? */
5959 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5960 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5961 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5962 	    (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5963 		/* got data while shutting down */
5964 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
5965 		error = ECONNRESET;
5966 		goto out_now;
5967 	}
5968 	sctp_alloc_a_strmoq(stcb, sp);
5969 	if (sp == NULL) {
5970 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5971 		error = ENOMEM;
5972 		goto out_now;
5973 	}
5974 	sp->sinfo_flags = srcv->sinfo_flags;
5975 	sp->timetolive = srcv->sinfo_timetolive;
5976 	sp->ppid = srcv->sinfo_ppid;
5977 	sp->context = srcv->sinfo_context;
5978 	sp->strseq = 0;
5979 	if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5980 		sp->net = net;
5981 		atomic_add_int(&sp->net->ref_count, 1);
5982 	} else {
5983 		sp->net = NULL;
5984 	}
5985 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5986 	sp->stream = srcv->sinfo_stream;
5987 	sp->msg_is_complete = 1;
5988 	sp->sender_all_done = 1;
5989 	sp->some_taken = 0;
5990 	sp->data = m;
5991 	sp->tail_mbuf = NULL;
5992 	sp->length = 0;
5993 	at = m;
5994 	sctp_set_prsctp_policy(sp);
5995 	/*
5996 	 * We could in theory (for sendall) sifa the length in, but we would
5997 	 * still have to hunt through the chain since we need to setup the
5998 	 * tail_mbuf
5999 	 */
6000 	while (at) {
6001 		if (SCTP_BUF_NEXT(at) == NULL)
6002 			sp->tail_mbuf = at;
6003 		sp->length += SCTP_BUF_LEN(at);
6004 		at = SCTP_BUF_NEXT(at);
6005 	}
6006 	SCTP_TCB_SEND_LOCK(stcb);
6007 	sctp_snd_sb_alloc(stcb, sp->length);
6008 	atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
6009 	TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
6010 	if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
6011 		sp->strseq = strm->next_sequence_sent;
6012 		strm->next_sequence_sent++;
6013 	}
6014 	stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp, 1);
6015 	m = NULL;
6016 	SCTP_TCB_SEND_UNLOCK(stcb);
6017 out_now:
6018 	if (m) {
6019 		sctp_m_freem(m);
6020 	}
6021 	return (error);
6022 }
6023 
6024 
6025 static struct mbuf *
6026 sctp_copy_mbufchain(struct mbuf *clonechain,
6027     struct mbuf *outchain,
6028     struct mbuf **endofchain,
6029     int can_take_mbuf,
6030     int sizeofcpy,
6031     uint8_t copy_by_ref)
6032 {
6033 	struct mbuf *m;
6034 	struct mbuf *appendchain;
6035 	caddr_t cp;
6036 	int len;
6037 
6038 	if (endofchain == NULL) {
6039 		/* error */
6040 error_out:
6041 		if (outchain)
6042 			sctp_m_freem(outchain);
6043 		return (NULL);
6044 	}
6045 	if (can_take_mbuf) {
6046 		appendchain = clonechain;
6047 	} else {
6048 		if (!copy_by_ref &&
6049 		    (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
6050 		    ) {
6051 			/* Its not in a cluster */
6052 			if (*endofchain == NULL) {
6053 				/* lets get a mbuf cluster */
6054 				if (outchain == NULL) {
6055 					/* This is the general case */
6056 			new_mbuf:
6057 					outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6058 					if (outchain == NULL) {
6059 						goto error_out;
6060 					}
6061 					SCTP_BUF_LEN(outchain) = 0;
6062 					*endofchain = outchain;
6063 					/* get the prepend space */
6064 					SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
6065 				} else {
6066 					/*
6067 					 * We really should not get a NULL
6068 					 * in endofchain
6069 					 */
6070 					/* find end */
6071 					m = outchain;
6072 					while (m) {
6073 						if (SCTP_BUF_NEXT(m) == NULL) {
6074 							*endofchain = m;
6075 							break;
6076 						}
6077 						m = SCTP_BUF_NEXT(m);
6078 					}
6079 					/* sanity */
6080 					if (*endofchain == NULL) {
6081 						/*
6082 						 * huh, TSNH XXX maybe we
6083 						 * should panic
6084 						 */
6085 						sctp_m_freem(outchain);
6086 						goto new_mbuf;
6087 					}
6088 				}
6089 				/* get the new end of length */
6090 				len = M_TRAILINGSPACE(*endofchain);
6091 			} else {
6092 				/* how much is left at the end? */
6093 				len = M_TRAILINGSPACE(*endofchain);
6094 			}
6095 			/* Find the end of the data, for appending */
6096 			cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
6097 
6098 			/* Now lets copy it out */
6099 			if (len >= sizeofcpy) {
6100 				/* It all fits, copy it in */
6101 				m_copydata(clonechain, 0, sizeofcpy, cp);
6102 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6103 			} else {
6104 				/* fill up the end of the chain */
6105 				if (len > 0) {
6106 					m_copydata(clonechain, 0, len, cp);
6107 					SCTP_BUF_LEN((*endofchain)) += len;
6108 					/* now we need another one */
6109 					sizeofcpy -= len;
6110 				}
6111 				m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6112 				if (m == NULL) {
6113 					/* We failed */
6114 					goto error_out;
6115 				}
6116 				SCTP_BUF_NEXT((*endofchain)) = m;
6117 				*endofchain = m;
6118 				cp = mtod((*endofchain), caddr_t);
6119 				m_copydata(clonechain, len, sizeofcpy, cp);
6120 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6121 			}
6122 			return (outchain);
6123 		} else {
6124 			/* copy the old fashion way */
6125 			appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
6126 #ifdef SCTP_MBUF_LOGGING
6127 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6128 				struct mbuf *mat;
6129 
6130 				mat = appendchain;
6131 				while (mat) {
6132 					if (SCTP_BUF_IS_EXTENDED(mat)) {
6133 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6134 					}
6135 					mat = SCTP_BUF_NEXT(mat);
6136 				}
6137 			}
6138 #endif
6139 		}
6140 	}
6141 	if (appendchain == NULL) {
6142 		/* error */
6143 		if (outchain)
6144 			sctp_m_freem(outchain);
6145 		return (NULL);
6146 	}
6147 	if (outchain) {
6148 		/* tack on to the end */
6149 		if (*endofchain != NULL) {
6150 			SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6151 		} else {
6152 			m = outchain;
6153 			while (m) {
6154 				if (SCTP_BUF_NEXT(m) == NULL) {
6155 					SCTP_BUF_NEXT(m) = appendchain;
6156 					break;
6157 				}
6158 				m = SCTP_BUF_NEXT(m);
6159 			}
6160 		}
6161 		/*
6162 		 * save off the end and update the end-chain postion
6163 		 */
6164 		m = appendchain;
6165 		while (m) {
6166 			if (SCTP_BUF_NEXT(m) == NULL) {
6167 				*endofchain = m;
6168 				break;
6169 			}
6170 			m = SCTP_BUF_NEXT(m);
6171 		}
6172 		return (outchain);
6173 	} else {
6174 		/* save off the end and update the end-chain postion */
6175 		m = appendchain;
6176 		while (m) {
6177 			if (SCTP_BUF_NEXT(m) == NULL) {
6178 				*endofchain = m;
6179 				break;
6180 			}
6181 			m = SCTP_BUF_NEXT(m);
6182 		}
6183 		return (appendchain);
6184 	}
6185 }
6186 
6187 int
6188 sctp_med_chunk_output(struct sctp_inpcb *inp,
6189     struct sctp_tcb *stcb,
6190     struct sctp_association *asoc,
6191     int *num_out,
6192     int *reason_code,
6193     int control_only, int from_where,
6194     struct timeval *now, int *now_filled, int frag_point, int so_locked
6195 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6196     SCTP_UNUSED
6197 #endif
6198 );
6199 
6200 static void
6201 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6202     uint32_t val)
6203 {
6204 	struct sctp_copy_all *ca;
6205 	struct mbuf *m;
6206 	int ret = 0;
6207 	int added_control = 0;
6208 	int un_sent, do_chunk_output = 1;
6209 	struct sctp_association *asoc;
6210 
6211 	ca = (struct sctp_copy_all *)ptr;
6212 	if (ca->m == NULL) {
6213 		return;
6214 	}
6215 	if (ca->inp != inp) {
6216 		/* TSNH */
6217 		return;
6218 	}
6219 	if ((ca->m) && ca->sndlen) {
6220 		m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6221 		if (m == NULL) {
6222 			/* can't copy so we are done */
6223 			ca->cnt_failed++;
6224 			return;
6225 		}
6226 #ifdef SCTP_MBUF_LOGGING
6227 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6228 			struct mbuf *mat;
6229 
6230 			mat = m;
6231 			while (mat) {
6232 				if (SCTP_BUF_IS_EXTENDED(mat)) {
6233 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6234 				}
6235 				mat = SCTP_BUF_NEXT(mat);
6236 			}
6237 		}
6238 #endif
6239 	} else {
6240 		m = NULL;
6241 	}
6242 	SCTP_TCB_LOCK_ASSERT(stcb);
6243 	if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6244 		/* Abort this assoc with m as the user defined reason */
6245 		if (m) {
6246 			struct sctp_paramhdr *ph;
6247 
6248 			SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6249 			if (m) {
6250 				ph = mtod(m, struct sctp_paramhdr *);
6251 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6252 				ph->param_length = htons(ca->sndlen);
6253 			}
6254 			/*
6255 			 * We add one here to keep the assoc from
6256 			 * dis-appearing on us.
6257 			 */
6258 			atomic_add_int(&stcb->asoc.refcnt, 1);
6259 			sctp_abort_an_association(inp, stcb,
6260 			    SCTP_RESPONSE_TO_USER_REQ,
6261 			    m, SCTP_SO_NOT_LOCKED);
6262 			/*
6263 			 * sctp_abort_an_association calls sctp_free_asoc()
6264 			 * free association will NOT free it since we
6265 			 * incremented the refcnt .. we do this to prevent
6266 			 * it being freed and things getting tricky since we
6267 			 * could end up (from free_asoc) calling inpcb_free
6268 			 * which would get a recursive lock call to the
6269 			 * iterator lock.. But as a consequence of that the
6270 			 * stcb will return to us un-locked.. since
6271 			 * free_asoc returns with either no TCB or the TCB
6272 			 * unlocked, we must relock.. to unlock in the
6273 			 * iterator timer :-0
6274 			 */
6275 			SCTP_TCB_LOCK(stcb);
6276 			atomic_add_int(&stcb->asoc.refcnt, -1);
6277 			goto no_chunk_output;
6278 		}
6279 	} else {
6280 		if (m) {
6281 			ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
6282 			    &ca->sndrcv, 1);
6283 		}
6284 		asoc = &stcb->asoc;
6285 		if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6286 			/* shutdown this assoc */
6287 			int cnt;
6288 
6289 			cnt = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
6290 
6291 			if (TAILQ_EMPTY(&asoc->send_queue) &&
6292 			    TAILQ_EMPTY(&asoc->sent_queue) &&
6293 			    (cnt == 0)) {
6294 				if (asoc->locked_on_sending) {
6295 					goto abort_anyway;
6296 				}
6297 				/*
6298 				 * there is nothing queued to send, so I'm
6299 				 * done...
6300 				 */
6301 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6302 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6303 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6304 					/*
6305 					 * only send SHUTDOWN the first time
6306 					 * through
6307 					 */
6308 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
6309 					if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6310 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6311 					}
6312 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6313 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6314 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6315 					    asoc->primary_destination);
6316 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6317 					    asoc->primary_destination);
6318 					added_control = 1;
6319 					do_chunk_output = 0;
6320 				}
6321 			} else {
6322 				/*
6323 				 * we still got (or just got) data to send,
6324 				 * so set SHUTDOWN_PENDING
6325 				 */
6326 				/*
6327 				 * XXX sockets draft says that SCTP_EOF
6328 				 * should be sent with no data.  currently,
6329 				 * we will allow user data to be sent first
6330 				 * and move to SHUTDOWN-PENDING
6331 				 */
6332 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6333 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6334 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6335 					if (asoc->locked_on_sending) {
6336 						/*
6337 						 * Locked to send out the
6338 						 * data
6339 						 */
6340 						struct sctp_stream_queue_pending *sp;
6341 
6342 						sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6343 						if (sp) {
6344 							if ((sp->length == 0) && (sp->msg_is_complete == 0))
6345 								asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6346 						}
6347 					}
6348 					asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6349 					if (TAILQ_EMPTY(&asoc->send_queue) &&
6350 					    TAILQ_EMPTY(&asoc->sent_queue) &&
6351 					    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6352 				abort_anyway:
6353 						atomic_add_int(&stcb->asoc.refcnt, 1);
6354 						sctp_abort_an_association(stcb->sctp_ep, stcb,
6355 						    SCTP_RESPONSE_TO_USER_REQ,
6356 						    NULL, SCTP_SO_NOT_LOCKED);
6357 						atomic_add_int(&stcb->asoc.refcnt, -1);
6358 						goto no_chunk_output;
6359 					}
6360 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6361 					    asoc->primary_destination);
6362 				}
6363 			}
6364 
6365 		}
6366 	}
6367 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6368 	    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6369 
6370 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6371 	    (stcb->asoc.total_flight > 0) &&
6372 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6373 	    ) {
6374 		do_chunk_output = 0;
6375 	}
6376 	if (do_chunk_output)
6377 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6378 	else if (added_control) {
6379 		int num_out = 0, reason = 0, now_filled = 0;
6380 		struct timeval now;
6381 		int frag_point;
6382 
6383 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6384 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6385 		    &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6386 	}
6387 no_chunk_output:
6388 	if (ret) {
6389 		ca->cnt_failed++;
6390 	} else {
6391 		ca->cnt_sent++;
6392 	}
6393 }
6394 
6395 static void
6396 sctp_sendall_completes(void *ptr, uint32_t val)
6397 {
6398 	struct sctp_copy_all *ca;
6399 
6400 	ca = (struct sctp_copy_all *)ptr;
6401 	/*
6402 	 * Do a notify here? Kacheong suggests that the notify be done at
6403 	 * the send time.. so you would push up a notification if any send
6404 	 * failed. Don't know if this is feasable since the only failures we
6405 	 * have is "memory" related and if you cannot get an mbuf to send
6406 	 * the data you surely can't get an mbuf to send up to notify the
6407 	 * user you can't send the data :->
6408 	 */
6409 
6410 	/* now free everything */
6411 	sctp_m_freem(ca->m);
6412 	SCTP_FREE(ca, SCTP_M_COPYAL);
6413 }
6414 
6415 
6416 #define	MC_ALIGN(m, len) do {						\
6417 	SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1));	\
6418 } while (0)
6419 
6420 
6421 
6422 static struct mbuf *
6423 sctp_copy_out_all(struct uio *uio, int len)
6424 {
6425 	struct mbuf *ret, *at;
6426 	int left, willcpy, cancpy, error;
6427 
6428 	ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6429 	if (ret == NULL) {
6430 		/* TSNH */
6431 		return (NULL);
6432 	}
6433 	left = len;
6434 	SCTP_BUF_LEN(ret) = 0;
6435 	/* save space for the data chunk header */
6436 	cancpy = M_TRAILINGSPACE(ret);
6437 	willcpy = min(cancpy, left);
6438 	at = ret;
6439 	while (left > 0) {
6440 		/* Align data to the end */
6441 		error = uiomove(mtod(at, caddr_t), willcpy, uio);
6442 		if (error) {
6443 	err_out_now:
6444 			sctp_m_freem(at);
6445 			return (NULL);
6446 		}
6447 		SCTP_BUF_LEN(at) = willcpy;
6448 		SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6449 		left -= willcpy;
6450 		if (left > 0) {
6451 			SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6452 			if (SCTP_BUF_NEXT(at) == NULL) {
6453 				goto err_out_now;
6454 			}
6455 			at = SCTP_BUF_NEXT(at);
6456 			SCTP_BUF_LEN(at) = 0;
6457 			cancpy = M_TRAILINGSPACE(at);
6458 			willcpy = min(cancpy, left);
6459 		}
6460 	}
6461 	return (ret);
6462 }
6463 
6464 static int
6465 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6466     struct sctp_sndrcvinfo *srcv)
6467 {
6468 	int ret;
6469 	struct sctp_copy_all *ca;
6470 
6471 	SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6472 	    SCTP_M_COPYAL);
6473 	if (ca == NULL) {
6474 		sctp_m_freem(m);
6475 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6476 		return (ENOMEM);
6477 	}
6478 	memset(ca, 0, sizeof(struct sctp_copy_all));
6479 
6480 	ca->inp = inp;
6481 	memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6482 	/*
6483 	 * take off the sendall flag, it would be bad if we failed to do
6484 	 * this :-0
6485 	 */
6486 	ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6487 	/* get length and mbuf chain */
6488 	if (uio) {
6489 		ca->sndlen = uio->uio_resid;
6490 		ca->m = sctp_copy_out_all(uio, ca->sndlen);
6491 		if (ca->m == NULL) {
6492 			SCTP_FREE(ca, SCTP_M_COPYAL);
6493 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6494 			return (ENOMEM);
6495 		}
6496 	} else {
6497 		/* Gather the length of the send */
6498 		struct mbuf *mat;
6499 
6500 		mat = m;
6501 		ca->sndlen = 0;
6502 		while (m) {
6503 			ca->sndlen += SCTP_BUF_LEN(m);
6504 			m = SCTP_BUF_NEXT(m);
6505 		}
6506 		ca->m = mat;
6507 	}
6508 	ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6509 	    SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6510 	    SCTP_ASOC_ANY_STATE,
6511 	    (void *)ca, 0,
6512 	    sctp_sendall_completes, inp, 1);
6513 	if (ret) {
6514 		SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6515 		SCTP_FREE(ca, SCTP_M_COPYAL);
6516 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6517 		return (EFAULT);
6518 	}
6519 	return (0);
6520 }
6521 
6522 
6523 void
6524 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6525 {
6526 	struct sctp_tmit_chunk *chk, *nchk;
6527 
6528 	TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
6529 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6530 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6531 			if (chk->data) {
6532 				sctp_m_freem(chk->data);
6533 				chk->data = NULL;
6534 			}
6535 			asoc->ctrl_queue_cnt--;
6536 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6537 		}
6538 	}
6539 }
6540 
6541 void
6542 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6543 {
6544 	struct sctp_association *asoc;
6545 	struct sctp_tmit_chunk *chk, *nchk;
6546 	struct sctp_asconf_chunk *acp;
6547 
6548 	asoc = &stcb->asoc;
6549 	TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
6550 		/* find SCTP_ASCONF chunk in queue */
6551 		if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6552 			if (chk->data) {
6553 				acp = mtod(chk->data, struct sctp_asconf_chunk *);
6554 				if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) {
6555 					/* Not Acked yet */
6556 					break;
6557 				}
6558 			}
6559 			TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6560 			if (chk->data) {
6561 				sctp_m_freem(chk->data);
6562 				chk->data = NULL;
6563 			}
6564 			asoc->ctrl_queue_cnt--;
6565 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6566 		}
6567 	}
6568 }
6569 
6570 
6571 static void
6572 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6573     struct sctp_association *asoc,
6574     struct sctp_tmit_chunk **data_list,
6575     int bundle_at,
6576     struct sctp_nets *net)
6577 {
6578 	int i;
6579 	struct sctp_tmit_chunk *tp1;
6580 
6581 	for (i = 0; i < bundle_at; i++) {
6582 		/* off of the send queue */
6583 		TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next);
6584 		asoc->send_queue_cnt--;
6585 		if (i > 0) {
6586 			/*
6587 			 * Any chunk NOT 0 you zap the time chunk 0 gets
6588 			 * zapped or set based on if a RTO measurment is
6589 			 * needed.
6590 			 */
6591 			data_list[i]->do_rtt = 0;
6592 		}
6593 		/* record time */
6594 		data_list[i]->sent_rcv_time = net->last_sent_time;
6595 		data_list[i]->rec.data.cwnd_at_send = net->cwnd;
6596 		data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6597 		if (data_list[i]->whoTo == NULL) {
6598 			data_list[i]->whoTo = net;
6599 			atomic_add_int(&net->ref_count, 1);
6600 		}
6601 		/* on to the sent queue */
6602 		tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6603 		if ((tp1) && SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) {
6604 			struct sctp_tmit_chunk *tpp;
6605 
6606 			/* need to move back */
6607 	back_up_more:
6608 			tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6609 			if (tpp == NULL) {
6610 				TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6611 				goto all_done;
6612 			}
6613 			tp1 = tpp;
6614 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) {
6615 				goto back_up_more;
6616 			}
6617 			TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6618 		} else {
6619 			TAILQ_INSERT_TAIL(&asoc->sent_queue,
6620 			    data_list[i],
6621 			    sctp_next);
6622 		}
6623 all_done:
6624 		/* This does not lower until the cum-ack passes it */
6625 		asoc->sent_queue_cnt++;
6626 		if ((asoc->peers_rwnd <= 0) &&
6627 		    (asoc->total_flight == 0) &&
6628 		    (bundle_at == 1)) {
6629 			/* Mark the chunk as being a window probe */
6630 			SCTP_STAT_INCR(sctps_windowprobed);
6631 		}
6632 #ifdef SCTP_AUDITING_ENABLED
6633 		sctp_audit_log(0xC2, 3);
6634 #endif
6635 		data_list[i]->sent = SCTP_DATAGRAM_SENT;
6636 		data_list[i]->snd_count = 1;
6637 		data_list[i]->rec.data.chunk_was_revoked = 0;
6638 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6639 			sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6640 			    data_list[i]->whoTo->flight_size,
6641 			    data_list[i]->book_size,
6642 			    (uintptr_t) data_list[i]->whoTo,
6643 			    data_list[i]->rec.data.TSN_seq);
6644 		}
6645 		sctp_flight_size_increase(data_list[i]);
6646 		sctp_total_flight_increase(stcb, data_list[i]);
6647 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6648 			sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6649 			    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6650 		}
6651 		asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6652 		    (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6653 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6654 			/* SWS sender side engages */
6655 			asoc->peers_rwnd = 0;
6656 		}
6657 	}
6658 	if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) {
6659 		(*asoc->cc_functions.sctp_cwnd_update_packet_transmitted) (stcb, net);
6660 	}
6661 }
6662 
6663 static void
6664 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked
6665 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6666     SCTP_UNUSED
6667 #endif
6668 )
6669 {
6670 	struct sctp_tmit_chunk *chk, *nchk;
6671 
6672 	TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
6673 		if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6674 		    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
6675 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6676 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6677 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
6678 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6679 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6680 		    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6681 		    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6682 		    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6683 		    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6684 		    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6685 			/* Stray chunks must be cleaned up */
6686 	clean_up_anyway:
6687 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6688 			if (chk->data) {
6689 				sctp_m_freem(chk->data);
6690 				chk->data = NULL;
6691 			}
6692 			asoc->ctrl_queue_cnt--;
6693 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)
6694 				asoc->fwd_tsn_cnt--;
6695 			sctp_free_a_chunk(stcb, chk, so_locked);
6696 		} else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6697 			/* special handling, we must look into the param */
6698 			if (chk != asoc->str_reset) {
6699 				goto clean_up_anyway;
6700 			}
6701 		}
6702 	}
6703 }
6704 
6705 
6706 static int
6707 sctp_can_we_split_this(struct sctp_tcb *stcb,
6708     uint32_t length,
6709     uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6710 {
6711 	/*
6712 	 * Make a decision on if I should split a msg into multiple parts.
6713 	 * This is only asked of incomplete messages.
6714 	 */
6715 	if (eeor_on) {
6716 		/*
6717 		 * If we are doing EEOR we need to always send it if its the
6718 		 * entire thing, since it might be all the guy is putting in
6719 		 * the hopper.
6720 		 */
6721 		if (goal_mtu >= length) {
6722 			/*-
6723 			 * If we have data outstanding,
6724 			 * we get another chance when the sack
6725 			 * arrives to transmit - wait for more data
6726 			 */
6727 			if (stcb->asoc.total_flight == 0) {
6728 				/*
6729 				 * If nothing is in flight, we zero the
6730 				 * packet counter.
6731 				 */
6732 				return (length);
6733 			}
6734 			return (0);
6735 
6736 		} else {
6737 			/* You can fill the rest */
6738 			return (goal_mtu);
6739 		}
6740 	}
6741 	/*-
6742 	 * For those strange folk that make the send buffer
6743 	 * smaller than our fragmentation point, we can't
6744 	 * get a full msg in so we have to allow splitting.
6745 	 */
6746 	if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
6747 		return (length);
6748 	}
6749 	if ((length <= goal_mtu) ||
6750 	    ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
6751 		/* Sub-optimial residual don't split in non-eeor mode. */
6752 		return (0);
6753 	}
6754 	/*
6755 	 * If we reach here length is larger than the goal_mtu. Do we wish
6756 	 * to split it for the sake of packet putting together?
6757 	 */
6758 	if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
6759 		/* Its ok to split it */
6760 		return (min(goal_mtu, frag_point));
6761 	}
6762 	/* Nope, can't split */
6763 	return (0);
6764 
6765 }
6766 
6767 static uint32_t
6768 sctp_move_to_outqueue(struct sctp_tcb *stcb,
6769     struct sctp_stream_out *strq,
6770     uint32_t goal_mtu,
6771     uint32_t frag_point,
6772     int *locked,
6773     int *giveup,
6774     int eeor_mode,
6775     int *bail,
6776     int so_locked
6777 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6778     SCTP_UNUSED
6779 #endif
6780 )
6781 {
6782 	/* Move from the stream to the send_queue keeping track of the total */
6783 	struct sctp_association *asoc;
6784 	struct sctp_stream_queue_pending *sp;
6785 	struct sctp_tmit_chunk *chk;
6786 	struct sctp_data_chunk *dchkh;
6787 	uint32_t to_move, length;
6788 	uint8_t rcv_flags = 0;
6789 	uint8_t some_taken;
6790 	uint8_t send_lock_up = 0;
6791 
6792 	SCTP_TCB_LOCK_ASSERT(stcb);
6793 	asoc = &stcb->asoc;
6794 one_more_time:
6795 	/* sa_ignore FREED_MEMORY */
6796 	sp = TAILQ_FIRST(&strq->outqueue);
6797 	if (sp == NULL) {
6798 		*locked = 0;
6799 		if (send_lock_up == 0) {
6800 			SCTP_TCB_SEND_LOCK(stcb);
6801 			send_lock_up = 1;
6802 		}
6803 		sp = TAILQ_FIRST(&strq->outqueue);
6804 		if (sp) {
6805 			goto one_more_time;
6806 		}
6807 		if (strq->last_msg_incomplete) {
6808 			SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6809 			    strq->stream_no,
6810 			    strq->last_msg_incomplete);
6811 			strq->last_msg_incomplete = 0;
6812 		}
6813 		to_move = 0;
6814 		if (send_lock_up) {
6815 			SCTP_TCB_SEND_UNLOCK(stcb);
6816 			send_lock_up = 0;
6817 		}
6818 		goto out_of;
6819 	}
6820 	if ((sp->msg_is_complete) && (sp->length == 0)) {
6821 		if (sp->sender_all_done) {
6822 			/*
6823 			 * We are doing differed cleanup. Last time through
6824 			 * when we took all the data the sender_all_done was
6825 			 * not set.
6826 			 */
6827 			if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
6828 				SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6829 				SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6830 				    sp->sender_all_done,
6831 				    sp->length,
6832 				    sp->msg_is_complete,
6833 				    sp->put_last_out,
6834 				    send_lock_up);
6835 			}
6836 			if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
6837 				SCTP_TCB_SEND_LOCK(stcb);
6838 				send_lock_up = 1;
6839 			}
6840 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6841 			TAILQ_REMOVE(&strq->outqueue, sp, next);
6842 			stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up);
6843 			if (sp->net) {
6844 				sctp_free_remote_addr(sp->net);
6845 				sp->net = NULL;
6846 			}
6847 			if (sp->data) {
6848 				sctp_m_freem(sp->data);
6849 				sp->data = NULL;
6850 			}
6851 			sctp_free_a_strmoq(stcb, sp, so_locked);
6852 			/* we can't be locked to it */
6853 			*locked = 0;
6854 			stcb->asoc.locked_on_sending = NULL;
6855 			if (send_lock_up) {
6856 				SCTP_TCB_SEND_UNLOCK(stcb);
6857 				send_lock_up = 0;
6858 			}
6859 			/* back to get the next msg */
6860 			goto one_more_time;
6861 		} else {
6862 			/*
6863 			 * sender just finished this but still holds a
6864 			 * reference
6865 			 */
6866 			*locked = 1;
6867 			*giveup = 1;
6868 			to_move = 0;
6869 			goto out_of;
6870 		}
6871 	} else {
6872 		/* is there some to get */
6873 		if (sp->length == 0) {
6874 			/* no */
6875 			*locked = 1;
6876 			*giveup = 1;
6877 			to_move = 0;
6878 			goto out_of;
6879 		} else if (sp->discard_rest) {
6880 			if (send_lock_up == 0) {
6881 				SCTP_TCB_SEND_LOCK(stcb);
6882 				send_lock_up = 1;
6883 			}
6884 			/* Whack down the size */
6885 			atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
6886 			if ((stcb->sctp_socket != NULL) && \
6887 			    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6888 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
6889 				atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
6890 			}
6891 			if (sp->data) {
6892 				sctp_m_freem(sp->data);
6893 				sp->data = NULL;
6894 				sp->tail_mbuf = NULL;
6895 			}
6896 			sp->length = 0;
6897 			sp->some_taken = 1;
6898 			*locked = 1;
6899 			*giveup = 1;
6900 			to_move = 0;
6901 			goto out_of;
6902 		}
6903 	}
6904 	some_taken = sp->some_taken;
6905 	if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6906 		sp->msg_is_complete = 1;
6907 	}
6908 re_look:
6909 	length = sp->length;
6910 	if (sp->msg_is_complete) {
6911 		/* The message is complete */
6912 		to_move = min(length, frag_point);
6913 		if (to_move == length) {
6914 			/* All of it fits in the MTU */
6915 			if (sp->some_taken) {
6916 				rcv_flags |= SCTP_DATA_LAST_FRAG;
6917 				sp->put_last_out = 1;
6918 			} else {
6919 				rcv_flags |= SCTP_DATA_NOT_FRAG;
6920 				sp->put_last_out = 1;
6921 			}
6922 		} else {
6923 			/* Not all of it fits, we fragment */
6924 			if (sp->some_taken == 0) {
6925 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
6926 			}
6927 			sp->some_taken = 1;
6928 		}
6929 	} else {
6930 		to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
6931 		if (to_move) {
6932 			/*-
6933 			 * We use a snapshot of length in case it
6934 			 * is expanding during the compare.
6935 			 */
6936 			uint32_t llen;
6937 
6938 			llen = length;
6939 			if (to_move >= llen) {
6940 				to_move = llen;
6941 				if (send_lock_up == 0) {
6942 					/*-
6943 					 * We are taking all of an incomplete msg
6944 					 * thus we need a send lock.
6945 					 */
6946 					SCTP_TCB_SEND_LOCK(stcb);
6947 					send_lock_up = 1;
6948 					if (sp->msg_is_complete) {
6949 						/*
6950 						 * the sender finished the
6951 						 * msg
6952 						 */
6953 						goto re_look;
6954 					}
6955 				}
6956 			}
6957 			if (sp->some_taken == 0) {
6958 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
6959 				sp->some_taken = 1;
6960 			}
6961 		} else {
6962 			/* Nothing to take. */
6963 			if (sp->some_taken) {
6964 				*locked = 1;
6965 			}
6966 			*giveup = 1;
6967 			to_move = 0;
6968 			goto out_of;
6969 		}
6970 	}
6971 
6972 	/* If we reach here, we can copy out a chunk */
6973 	sctp_alloc_a_chunk(stcb, chk);
6974 	if (chk == NULL) {
6975 		/* No chunk memory */
6976 		*giveup = 1;
6977 		to_move = 0;
6978 		goto out_of;
6979 	}
6980 	/*
6981 	 * Setup for unordered if needed by looking at the user sent info
6982 	 * flags.
6983 	 */
6984 	if (sp->sinfo_flags & SCTP_UNORDERED) {
6985 		rcv_flags |= SCTP_DATA_UNORDERED;
6986 	}
6987 	if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
6988 	    ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
6989 		rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
6990 	}
6991 	/* clear out the chunk before setting up */
6992 	memset(chk, 0, sizeof(*chk));
6993 	chk->rec.data.rcv_flags = rcv_flags;
6994 
6995 	if (to_move >= length) {
6996 		/* we think we can steal the whole thing */
6997 		if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
6998 			SCTP_TCB_SEND_LOCK(stcb);
6999 			send_lock_up = 1;
7000 		}
7001 		if (to_move < sp->length) {
7002 			/* bail, it changed */
7003 			goto dont_do_it;
7004 		}
7005 		chk->data = sp->data;
7006 		chk->last_mbuf = sp->tail_mbuf;
7007 		/* register the stealing */
7008 		sp->data = sp->tail_mbuf = NULL;
7009 	} else {
7010 		struct mbuf *m;
7011 
7012 dont_do_it:
7013 		chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
7014 		chk->last_mbuf = NULL;
7015 		if (chk->data == NULL) {
7016 			sp->some_taken = some_taken;
7017 			sctp_free_a_chunk(stcb, chk, so_locked);
7018 			*bail = 1;
7019 			to_move = 0;
7020 			goto out_of;
7021 		}
7022 #ifdef SCTP_MBUF_LOGGING
7023 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7024 			struct mbuf *mat;
7025 
7026 			mat = chk->data;
7027 			while (mat) {
7028 				if (SCTP_BUF_IS_EXTENDED(mat)) {
7029 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
7030 				}
7031 				mat = SCTP_BUF_NEXT(mat);
7032 			}
7033 		}
7034 #endif
7035 		/* Pull off the data */
7036 		m_adj(sp->data, to_move);
7037 		/* Now lets work our way down and compact it */
7038 		m = sp->data;
7039 		while (m && (SCTP_BUF_LEN(m) == 0)) {
7040 			sp->data = SCTP_BUF_NEXT(m);
7041 			SCTP_BUF_NEXT(m) = NULL;
7042 			if (sp->tail_mbuf == m) {
7043 				/*-
7044 				 * Freeing tail? TSNH since
7045 				 * we supposedly were taking less
7046 				 * than the sp->length.
7047 				 */
7048 #ifdef INVARIANTS
7049 				panic("Huh, freing tail? - TSNH");
7050 #else
7051 				SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
7052 				sp->tail_mbuf = sp->data = NULL;
7053 				sp->length = 0;
7054 #endif
7055 
7056 			}
7057 			sctp_m_free(m);
7058 			m = sp->data;
7059 		}
7060 	}
7061 	if (SCTP_BUF_IS_EXTENDED(chk->data)) {
7062 		chk->copy_by_ref = 1;
7063 	} else {
7064 		chk->copy_by_ref = 0;
7065 	}
7066 	/*
7067 	 * get last_mbuf and counts of mb useage This is ugly but hopefully
7068 	 * its only one mbuf.
7069 	 */
7070 	if (chk->last_mbuf == NULL) {
7071 		chk->last_mbuf = chk->data;
7072 		while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
7073 			chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
7074 		}
7075 	}
7076 	if (to_move > length) {
7077 		/*- This should not happen either
7078 		 * since we always lower to_move to the size
7079 		 * of sp->length if its larger.
7080 		 */
7081 #ifdef INVARIANTS
7082 		panic("Huh, how can to_move be larger?");
7083 #else
7084 		SCTP_PRINTF("Huh, how can to_move be larger?\n");
7085 		sp->length = 0;
7086 #endif
7087 	} else {
7088 		atomic_subtract_int(&sp->length, to_move);
7089 	}
7090 	if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
7091 		/* Not enough room for a chunk header, get some */
7092 		struct mbuf *m;
7093 
7094 		m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
7095 		if (m == NULL) {
7096 			/*
7097 			 * we're in trouble here. _PREPEND below will free
7098 			 * all the data if there is no leading space, so we
7099 			 * must put the data back and restore.
7100 			 */
7101 			if (send_lock_up == 0) {
7102 				SCTP_TCB_SEND_LOCK(stcb);
7103 				send_lock_up = 1;
7104 			}
7105 			if (chk->data == NULL) {
7106 				/* unsteal the data */
7107 				sp->data = chk->data;
7108 				sp->tail_mbuf = chk->last_mbuf;
7109 			} else {
7110 				struct mbuf *m_tmp;
7111 
7112 				/* reassemble the data */
7113 				m_tmp = sp->data;
7114 				sp->data = chk->data;
7115 				SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7116 			}
7117 			sp->some_taken = some_taken;
7118 			atomic_add_int(&sp->length, to_move);
7119 			chk->data = NULL;
7120 			*bail = 1;
7121 			sctp_free_a_chunk(stcb, chk, so_locked);
7122 			to_move = 0;
7123 			goto out_of;
7124 		} else {
7125 			SCTP_BUF_LEN(m) = 0;
7126 			SCTP_BUF_NEXT(m) = chk->data;
7127 			chk->data = m;
7128 			M_ALIGN(chk->data, 4);
7129 		}
7130 	}
7131 	SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
7132 	if (chk->data == NULL) {
7133 		/* HELP, TSNH since we assured it would not above? */
7134 #ifdef INVARIANTS
7135 		panic("prepend failes HELP?");
7136 #else
7137 		SCTP_PRINTF("prepend fails HELP?\n");
7138 		sctp_free_a_chunk(stcb, chk, so_locked);
7139 #endif
7140 		*bail = 1;
7141 		to_move = 0;
7142 		goto out_of;
7143 	}
7144 	sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
7145 	chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
7146 	chk->book_size_scale = 0;
7147 	chk->sent = SCTP_DATAGRAM_UNSENT;
7148 
7149 	chk->flags = 0;
7150 	chk->asoc = &stcb->asoc;
7151 	chk->pad_inplace = 0;
7152 	chk->no_fr_allowed = 0;
7153 	chk->rec.data.stream_seq = sp->strseq;
7154 	chk->rec.data.stream_number = sp->stream;
7155 	chk->rec.data.payloadtype = sp->ppid;
7156 	chk->rec.data.context = sp->context;
7157 	chk->rec.data.doing_fast_retransmit = 0;
7158 
7159 	chk->rec.data.timetodrop = sp->ts;
7160 	chk->flags = sp->act_flags;
7161 
7162 	if (sp->net) {
7163 		chk->whoTo = sp->net;
7164 		atomic_add_int(&chk->whoTo->ref_count, 1);
7165 	} else
7166 		chk->whoTo = NULL;
7167 
7168 	if (sp->holds_key_ref) {
7169 		chk->auth_keyid = sp->auth_keyid;
7170 		sctp_auth_key_acquire(stcb, chk->auth_keyid);
7171 		chk->holds_key_ref = 1;
7172 	}
7173 	chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
7174 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7175 		sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7176 		    (uintptr_t) stcb, sp->length,
7177 		    (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
7178 		    chk->rec.data.TSN_seq);
7179 	}
7180 	dchkh = mtod(chk->data, struct sctp_data_chunk *);
7181 	/*
7182 	 * Put the rest of the things in place now. Size was done earlier in
7183 	 * previous loop prior to padding.
7184 	 */
7185 
7186 #ifdef SCTP_ASOCLOG_OF_TSNS
7187 	SCTP_TCB_LOCK_ASSERT(stcb);
7188 	if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7189 		asoc->tsn_out_at = 0;
7190 		asoc->tsn_out_wrapped = 1;
7191 	}
7192 	asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7193 	asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7194 	asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7195 	asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7196 	asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7197 	asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7198 	asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7199 	asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7200 	asoc->tsn_out_at++;
7201 #endif
7202 
7203 	dchkh->ch.chunk_type = SCTP_DATA;
7204 	dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7205 	dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7206 	dchkh->dp.stream_id = htons(strq->stream_no);
7207 	dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7208 	dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7209 	dchkh->ch.chunk_length = htons(chk->send_size);
7210 	/* Now advance the chk->send_size by the actual pad needed. */
7211 	if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7212 		/* need a pad */
7213 		struct mbuf *lm;
7214 		int pads;
7215 
7216 		pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7217 		if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7218 			chk->pad_inplace = 1;
7219 		}
7220 		if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7221 			/* pad added an mbuf */
7222 			chk->last_mbuf = lm;
7223 		}
7224 		chk->send_size += pads;
7225 	}
7226 	/* We only re-set the policy if it is on */
7227 	if (sp->pr_sctp_on) {
7228 		sctp_set_prsctp_policy(sp);
7229 		asoc->pr_sctp_cnt++;
7230 		chk->pr_sctp_on = 1;
7231 	} else {
7232 		chk->pr_sctp_on = 0;
7233 	}
7234 	if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7235 		/* All done pull and kill the message */
7236 		atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7237 		if (sp->put_last_out == 0) {
7238 			SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7239 			SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7240 			    sp->sender_all_done,
7241 			    sp->length,
7242 			    sp->msg_is_complete,
7243 			    sp->put_last_out,
7244 			    send_lock_up);
7245 		}
7246 		if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7247 			SCTP_TCB_SEND_LOCK(stcb);
7248 			send_lock_up = 1;
7249 		}
7250 		TAILQ_REMOVE(&strq->outqueue, sp, next);
7251 		stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up);
7252 		if (sp->net) {
7253 			sctp_free_remote_addr(sp->net);
7254 			sp->net = NULL;
7255 		}
7256 		if (sp->data) {
7257 			sctp_m_freem(sp->data);
7258 			sp->data = NULL;
7259 		}
7260 		sctp_free_a_strmoq(stcb, sp, so_locked);
7261 
7262 		/* we can't be locked to it */
7263 		*locked = 0;
7264 		stcb->asoc.locked_on_sending = NULL;
7265 	} else {
7266 		/* more to go, we are locked */
7267 		*locked = 1;
7268 	}
7269 	asoc->chunks_on_out_queue++;
7270 	TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7271 	asoc->send_queue_cnt++;
7272 out_of:
7273 	if (send_lock_up) {
7274 		SCTP_TCB_SEND_UNLOCK(stcb);
7275 		send_lock_up = 0;
7276 	}
7277 	return (to_move);
7278 }
7279 
7280 
7281 static void
7282 sctp_fill_outqueue(struct sctp_tcb *stcb,
7283     struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now, int so_locked
7284 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7285     SCTP_UNUSED
7286 #endif
7287 )
7288 {
7289 	struct sctp_association *asoc;
7290 	struct sctp_stream_out *strq, *strqn;
7291 	int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7292 	int locked, giveup;
7293 
7294 	SCTP_TCB_LOCK_ASSERT(stcb);
7295 	asoc = &stcb->asoc;
7296 #ifdef INET6
7297 	if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
7298 		goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7299 	} else {
7300 		/* ?? not sure what else to do */
7301 		goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7302 	}
7303 #else
7304 	goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7305 #endif
7306 	/* Need an allowance for the data chunk header too */
7307 	goal_mtu -= sizeof(struct sctp_data_chunk);
7308 
7309 	/* must make even word boundary */
7310 	goal_mtu &= 0xfffffffc;
7311 	if (asoc->locked_on_sending) {
7312 		/* We are stuck on one stream until the message completes. */
7313 		strq = asoc->locked_on_sending;
7314 		locked = 1;
7315 	} else {
7316 		strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7317 		locked = 0;
7318 	}
7319 	strqn = strq;
7320 	while ((goal_mtu > 0) && strq) {
7321 		giveup = 0;
7322 		bail = 0;
7323 		moved_how_much = sctp_move_to_outqueue(stcb, strq, goal_mtu, frag_point, &locked,
7324 		    &giveup, eeor_mode, &bail, so_locked);
7325 		if (moved_how_much)
7326 			stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, moved_how_much);
7327 
7328 		if (locked) {
7329 			asoc->locked_on_sending = strq;
7330 			if ((moved_how_much == 0) || (giveup) || bail)
7331 				/* no more to move for now */
7332 				break;
7333 		} else {
7334 			asoc->locked_on_sending = NULL;
7335 			if ((giveup) || bail) {
7336 				break;
7337 			}
7338 			strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7339 			if (strq == NULL) {
7340 				break;
7341 			}
7342 		}
7343 		total_moved += moved_how_much;
7344 		goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7345 		goal_mtu &= 0xfffffffc;
7346 	}
7347 	if (bail)
7348 		*quit_now = 1;
7349 
7350 	stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc);
7351 
7352 	if (total_moved == 0) {
7353 		if ((stcb->asoc.sctp_cmt_on_off == 0) &&
7354 		    (net == stcb->asoc.primary_destination)) {
7355 			/* ran dry for primary network net */
7356 			SCTP_STAT_INCR(sctps_primary_randry);
7357 		} else if (stcb->asoc.sctp_cmt_on_off > 0) {
7358 			/* ran dry with CMT on */
7359 			SCTP_STAT_INCR(sctps_cmt_randry);
7360 		}
7361 	}
7362 }
7363 
7364 void
7365 sctp_fix_ecn_echo(struct sctp_association *asoc)
7366 {
7367 	struct sctp_tmit_chunk *chk;
7368 
7369 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7370 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7371 			chk->sent = SCTP_DATAGRAM_UNSENT;
7372 		}
7373 	}
7374 }
7375 
7376 void
7377 sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net)
7378 {
7379 	struct sctp_association *asoc;
7380 	struct sctp_tmit_chunk *chk;
7381 	struct sctp_stream_queue_pending *sp;
7382 	unsigned int i;
7383 
7384 	if (net == NULL) {
7385 		return;
7386 	}
7387 	asoc = &stcb->asoc;
7388 	for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
7389 		TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
7390 			if (sp->net == net) {
7391 				sctp_free_remote_addr(sp->net);
7392 				sp->net = NULL;
7393 			}
7394 		}
7395 	}
7396 	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7397 		if (chk->whoTo == net) {
7398 			sctp_free_remote_addr(chk->whoTo);
7399 			chk->whoTo = NULL;
7400 		}
7401 	}
7402 }
7403 
7404 int
7405 sctp_med_chunk_output(struct sctp_inpcb *inp,
7406     struct sctp_tcb *stcb,
7407     struct sctp_association *asoc,
7408     int *num_out,
7409     int *reason_code,
7410     int control_only, int from_where,
7411     struct timeval *now, int *now_filled, int frag_point, int so_locked
7412 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7413     SCTP_UNUSED
7414 #endif
7415 )
7416 {
7417 	/*
7418 	 * Ok this is the generic chunk service queue. we must do the
7419 	 * following: - Service the stream queue that is next, moving any
7420 	 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7421 	 * LAST to the out queue in one pass) and assigning TSN's - Check to
7422 	 * see if the cwnd/rwnd allows any output, if so we go ahead and
7423 	 * fomulate and send the low level chunks. Making sure to combine
7424 	 * any control in the control chunk queue also.
7425 	 */
7426 	struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL;
7427 	struct mbuf *outchain, *endoutchain;
7428 	struct sctp_tmit_chunk *chk, *nchk;
7429 
7430 	/* temp arrays for unlinking */
7431 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7432 	int no_fragmentflg, error;
7433 	unsigned int max_rwnd_per_dest, max_send_per_dest;
7434 	int one_chunk, hbflag, skip_data_for_this_net;
7435 	int asconf, cookie, no_out_cnt;
7436 	int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7437 	unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7438 	int tsns_sent = 0;
7439 	uint32_t auth_offset = 0;
7440 	struct sctp_auth_chunk *auth = NULL;
7441 	uint16_t auth_keyid;
7442 	int override_ok = 1;
7443 	int data_auth_reqd = 0;
7444 
7445 	/*
7446 	 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7447 	 * destination.
7448 	 */
7449 	int pf_hbflag = 0;
7450 	int quit_now = 0;
7451 
7452 	*num_out = 0;
7453 	auth_keyid = stcb->asoc.authinfo.active_keyid;
7454 
7455 	if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7456 	    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7457 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7458 		eeor_mode = 1;
7459 	} else {
7460 		eeor_mode = 0;
7461 	}
7462 	ctl_cnt = no_out_cnt = asconf = cookie = 0;
7463 	/*
7464 	 * First lets prime the pump. For each destination, if there is room
7465 	 * in the flight size, attempt to pull an MTU's worth out of the
7466 	 * stream queues into the general send_queue
7467 	 */
7468 #ifdef SCTP_AUDITING_ENABLED
7469 	sctp_audit_log(0xC2, 2);
7470 #endif
7471 	SCTP_TCB_LOCK_ASSERT(stcb);
7472 	hbflag = 0;
7473 	if ((control_only) || (asoc->stream_reset_outstanding))
7474 		no_data_chunks = 1;
7475 	else
7476 		no_data_chunks = 0;
7477 
7478 	/* Nothing to possible to send? */
7479 	if ((TAILQ_EMPTY(&asoc->control_send_queue) ||
7480 	    (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) &&
7481 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7482 	    TAILQ_EMPTY(&asoc->send_queue) &&
7483 	    stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
7484 nothing_to_send:
7485 		*reason_code = 9;
7486 		return (0);
7487 	}
7488 	if (asoc->peers_rwnd == 0) {
7489 		/* No room in peers rwnd */
7490 		*reason_code = 1;
7491 		if (asoc->total_flight > 0) {
7492 			/* we are allowed one chunk in flight */
7493 			no_data_chunks = 1;
7494 		}
7495 	}
7496 	if (stcb->asoc.ecn_echo_cnt_onq) {
7497 		/* Record where a sack goes, if any */
7498 		if (no_data_chunks &&
7499 		    (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) {
7500 			/* Nothing but ECNe to send - we don't do that */
7501 			goto nothing_to_send;
7502 		}
7503 		TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7504 			if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7505 			    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7506 				sack_goes_to = chk->whoTo;
7507 				break;
7508 			}
7509 		}
7510 	}
7511 	max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7512 	if (stcb->sctp_socket)
7513 		max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets;
7514 	else
7515 		max_send_per_dest = 0;
7516 	if ((no_data_chunks == 0) &&
7517 	    (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) {
7518 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7519 			/*
7520 			 * This for loop we are in takes in each net, if
7521 			 * its's got space in cwnd and has data sent to it
7522 			 * (when CMT is off) then it calls
7523 			 * sctp_fill_outqueue for the net. This gets data on
7524 			 * the send queue for that network.
7525 			 *
7526 			 * In sctp_fill_outqueue TSN's are assigned and data is
7527 			 * copied out of the stream buffers. Note mostly
7528 			 * copy by reference (we hope).
7529 			 */
7530 			net->window_probe = 0;
7531 			if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ||
7532 			    (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
7533 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7534 					sctp_log_cwnd(stcb, net, 1,
7535 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7536 				}
7537 				continue;
7538 			}
7539 			if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
7540 			    (net->flight_size == 0)) {
7541 				(*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
7542 			}
7543 			if ((asoc->sctp_cmt_on_off == 0) &&
7544 			    (asoc->primary_destination != net) &&
7545 			    (net->ref_count < 2)) {
7546 				/* nothing can be in queue for this guy */
7547 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7548 					sctp_log_cwnd(stcb, net, 2,
7549 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7550 				}
7551 				continue;
7552 			}
7553 			if (net->flight_size >= net->cwnd) {
7554 				/* skip this network, no room - can't fill */
7555 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7556 					sctp_log_cwnd(stcb, net, 3,
7557 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7558 				}
7559 				continue;
7560 			}
7561 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7562 				sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7563 			}
7564 			sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked);
7565 			if (quit_now) {
7566 				/* memory alloc failure */
7567 				no_data_chunks = 1;
7568 				break;
7569 			}
7570 		}
7571 	}
7572 	/* now service each destination and send out what we can for it */
7573 	/* Nothing to send? */
7574 	if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7575 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7576 	    TAILQ_EMPTY(&asoc->send_queue)) {
7577 		*reason_code = 8;
7578 		return (0);
7579 	}
7580 	if (asoc->sctp_cmt_on_off > 0) {
7581 		/* get the last start point */
7582 		start_at = asoc->last_net_cmt_send_started;
7583 		if (start_at == NULL) {
7584 			/* null so to beginning */
7585 			start_at = TAILQ_FIRST(&asoc->nets);
7586 		} else {
7587 			start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7588 			if (start_at == NULL) {
7589 				start_at = TAILQ_FIRST(&asoc->nets);
7590 			}
7591 		}
7592 		asoc->last_net_cmt_send_started = start_at;
7593 	} else {
7594 		start_at = TAILQ_FIRST(&asoc->nets);
7595 	}
7596 	old_start_at = NULL;
7597 again_one_more_time:
7598 	for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7599 		/* how much can we send? */
7600 		/* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7601 		if (old_start_at && (old_start_at == net)) {
7602 			/* through list ocmpletely. */
7603 			break;
7604 		}
7605 		tsns_sent = 0xa;
7606 		if ((asoc->sctp_cmt_on_off == 0) &&
7607 		    (asoc->primary_destination != net) &&
7608 		    (net->ref_count < 2)) {
7609 			/*
7610 			 * Ref-count of 1 so we cannot have data or control
7611 			 * queued to this address. Skip it (non-CMT).
7612 			 */
7613 			continue;
7614 		}
7615 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7616 		    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7617 		    (net->flight_size >= net->cwnd)) {
7618 			/*
7619 			 * Nothing on control or asconf and flight is full,
7620 			 * we can skip even in the CMT case.
7621 			 */
7622 			continue;
7623 		}
7624 		ctl_cnt = bundle_at = 0;
7625 		endoutchain = outchain = NULL;
7626 		no_fragmentflg = 1;
7627 		one_chunk = 0;
7628 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7629 			skip_data_for_this_net = 1;
7630 		} else {
7631 			skip_data_for_this_net = 0;
7632 		}
7633 		if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7634 			/*
7635 			 * if we have a route and an ifp check to see if we
7636 			 * have room to send to this guy
7637 			 */
7638 			struct ifnet *ifp;
7639 
7640 			ifp = net->ro.ro_rt->rt_ifp;
7641 			if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7642 				SCTP_STAT_INCR(sctps_ifnomemqueued);
7643 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7644 					sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7645 				}
7646 				continue;
7647 			}
7648 		}
7649 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7650 #ifdef INET
7651 		case AF_INET:
7652 			mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7653 			break;
7654 #endif
7655 #ifdef INET6
7656 		case AF_INET6:
7657 			mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7658 			break;
7659 #endif
7660 		default:
7661 			/* TSNH */
7662 			mtu = net->mtu;
7663 			break;
7664 		}
7665 		mx_mtu = mtu;
7666 		to_out = 0;
7667 		if (mtu > asoc->peers_rwnd) {
7668 			if (asoc->total_flight > 0) {
7669 				/* We have a packet in flight somewhere */
7670 				r_mtu = asoc->peers_rwnd;
7671 			} else {
7672 				/* We are always allowed to send one MTU out */
7673 				one_chunk = 1;
7674 				r_mtu = mtu;
7675 			}
7676 		} else {
7677 			r_mtu = mtu;
7678 		}
7679 		/************************/
7680 		/* ASCONF transmission */
7681 		/************************/
7682 		/* Now first lets go through the asconf queue */
7683 		TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
7684 			if (chk->rec.chunk_id.id != SCTP_ASCONF) {
7685 				continue;
7686 			}
7687 			if (chk->whoTo != net) {
7688 				/*
7689 				 * No, not sent to the network we are
7690 				 * looking at
7691 				 */
7692 				break;
7693 			}
7694 			if (chk->data == NULL) {
7695 				break;
7696 			}
7697 			if (chk->sent != SCTP_DATAGRAM_UNSENT &&
7698 			    chk->sent != SCTP_DATAGRAM_RESEND) {
7699 				break;
7700 			}
7701 			/*
7702 			 * if no AUTH is yet included and this chunk
7703 			 * requires it, make sure to account for it.  We
7704 			 * don't apply the size until the AUTH chunk is
7705 			 * actually added below in case there is no room for
7706 			 * this chunk. NOTE: we overload the use of "omtu"
7707 			 * here
7708 			 */
7709 			if ((auth == NULL) &&
7710 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7711 			    stcb->asoc.peer_auth_chunks)) {
7712 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7713 			} else
7714 				omtu = 0;
7715 			/* Here we do NOT factor the r_mtu */
7716 			if ((chk->send_size < (int)(mtu - omtu)) ||
7717 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7718 				/*
7719 				 * We probably should glom the mbuf chain
7720 				 * from the chk->data for control but the
7721 				 * problem is it becomes yet one more level
7722 				 * of tracking to do if for some reason
7723 				 * output fails. Then I have got to
7724 				 * reconstruct the merged control chain.. el
7725 				 * yucko.. for now we take the easy way and
7726 				 * do the copy
7727 				 */
7728 				/*
7729 				 * Add an AUTH chunk, if chunk requires it
7730 				 * save the offset into the chain for AUTH
7731 				 */
7732 				if ((auth == NULL) &&
7733 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7734 				    stcb->asoc.peer_auth_chunks))) {
7735 					outchain = sctp_add_auth_chunk(outchain,
7736 					    &endoutchain,
7737 					    &auth,
7738 					    &auth_offset,
7739 					    stcb,
7740 					    chk->rec.chunk_id.id);
7741 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7742 				}
7743 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7744 				    (int)chk->rec.chunk_id.can_take_data,
7745 				    chk->send_size, chk->copy_by_ref);
7746 				if (outchain == NULL) {
7747 					*reason_code = 8;
7748 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7749 					return (ENOMEM);
7750 				}
7751 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7752 				/* update our MTU size */
7753 				if (mtu > (chk->send_size + omtu))
7754 					mtu -= (chk->send_size + omtu);
7755 				else
7756 					mtu = 0;
7757 				to_out += (chk->send_size + omtu);
7758 				/* Do clear IP_DF ? */
7759 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7760 					no_fragmentflg = 0;
7761 				}
7762 				if (chk->rec.chunk_id.can_take_data)
7763 					chk->data = NULL;
7764 				/*
7765 				 * set hb flag since we can use these for
7766 				 * RTO
7767 				 */
7768 				hbflag = 1;
7769 				asconf = 1;
7770 				/*
7771 				 * should sysctl this: don't bundle data
7772 				 * with ASCONF since it requires AUTH
7773 				 */
7774 				no_data_chunks = 1;
7775 				chk->sent = SCTP_DATAGRAM_SENT;
7776 				chk->snd_count++;
7777 				if (mtu == 0) {
7778 					/*
7779 					 * Ok we are out of room but we can
7780 					 * output without effecting the
7781 					 * flight size since this little guy
7782 					 * is a control only packet.
7783 					 */
7784 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7785 					/*
7786 					 * do NOT clear the asconf flag as
7787 					 * it is used to do appropriate
7788 					 * source address selection.
7789 					 */
7790 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7791 					    (struct sockaddr *)&net->ro._l_addr,
7792 					    outchain, auth_offset, auth,
7793 					    stcb->asoc.authinfo.active_keyid,
7794 					    no_fragmentflg, 0, NULL, asconf,
7795 					    inp->sctp_lport, stcb->rport,
7796 					    htonl(stcb->asoc.peer_vtag),
7797 					    net->port, so_locked, NULL, NULL))) {
7798 						if (error == ENOBUFS) {
7799 							asoc->ifp_had_enobuf = 1;
7800 							SCTP_STAT_INCR(sctps_lowlevelerr);
7801 						}
7802 						if (from_where == 0) {
7803 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
7804 						}
7805 						if (*now_filled == 0) {
7806 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7807 							*now_filled = 1;
7808 							*now = net->last_sent_time;
7809 						} else {
7810 							net->last_sent_time = *now;
7811 						}
7812 						hbflag = 0;
7813 						/* error, could not output */
7814 						if (error == EHOSTUNREACH) {
7815 							/*
7816 							 * Destination went
7817 							 * unreachable
7818 							 * during this send
7819 							 */
7820 							sctp_move_chunks_from_net(stcb, net);
7821 						}
7822 						*reason_code = 7;
7823 						continue;
7824 					} else
7825 						asoc->ifp_had_enobuf = 0;
7826 					if (*now_filled == 0) {
7827 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7828 						*now_filled = 1;
7829 						*now = net->last_sent_time;
7830 					} else {
7831 						net->last_sent_time = *now;
7832 					}
7833 					hbflag = 0;
7834 					/*
7835 					 * increase the number we sent, if a
7836 					 * cookie is sent we don't tell them
7837 					 * any was sent out.
7838 					 */
7839 					outchain = endoutchain = NULL;
7840 					auth = NULL;
7841 					auth_offset = 0;
7842 					if (!no_out_cnt)
7843 						*num_out += ctl_cnt;
7844 					/* recalc a clean slate and setup */
7845 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7846 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7847 					} else {
7848 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7849 					}
7850 					to_out = 0;
7851 					no_fragmentflg = 1;
7852 				}
7853 			}
7854 		}
7855 		/************************/
7856 		/* Control transmission */
7857 		/************************/
7858 		/* Now first lets go through the control queue */
7859 		TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
7860 			if ((sack_goes_to) &&
7861 			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO) &&
7862 			    (chk->whoTo != sack_goes_to)) {
7863 				/*
7864 				 * if we have a sack in queue, and we are
7865 				 * looking at an ecn echo that is NOT queued
7866 				 * to where the sack is going..
7867 				 */
7868 				if (chk->whoTo == net) {
7869 					/*
7870 					 * Don't transmit it to where its
7871 					 * going (current net)
7872 					 */
7873 					continue;
7874 				} else if (sack_goes_to == net) {
7875 					/*
7876 					 * But do transmit it to this
7877 					 * address
7878 					 */
7879 					goto skip_net_check;
7880 				}
7881 			}
7882 			if (chk->whoTo != net) {
7883 				/*
7884 				 * No, not sent to the network we are
7885 				 * looking at
7886 				 */
7887 				continue;
7888 			}
7889 	skip_net_check:
7890 			if (chk->data == NULL) {
7891 				continue;
7892 			}
7893 			if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7894 				/*
7895 				 * It must be unsent. Cookies and ASCONF's
7896 				 * hang around but there timers will force
7897 				 * when marked for resend.
7898 				 */
7899 				continue;
7900 			}
7901 			/*
7902 			 * if no AUTH is yet included and this chunk
7903 			 * requires it, make sure to account for it.  We
7904 			 * don't apply the size until the AUTH chunk is
7905 			 * actually added below in case there is no room for
7906 			 * this chunk. NOTE: we overload the use of "omtu"
7907 			 * here
7908 			 */
7909 			if ((auth == NULL) &&
7910 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7911 			    stcb->asoc.peer_auth_chunks)) {
7912 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7913 			} else
7914 				omtu = 0;
7915 			/* Here we do NOT factor the r_mtu */
7916 			if ((chk->send_size <= (int)(mtu - omtu)) ||
7917 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7918 				/*
7919 				 * We probably should glom the mbuf chain
7920 				 * from the chk->data for control but the
7921 				 * problem is it becomes yet one more level
7922 				 * of tracking to do if for some reason
7923 				 * output fails. Then I have got to
7924 				 * reconstruct the merged control chain.. el
7925 				 * yucko.. for now we take the easy way and
7926 				 * do the copy
7927 				 */
7928 				/*
7929 				 * Add an AUTH chunk, if chunk requires it
7930 				 * save the offset into the chain for AUTH
7931 				 */
7932 				if ((auth == NULL) &&
7933 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7934 				    stcb->asoc.peer_auth_chunks))) {
7935 					outchain = sctp_add_auth_chunk(outchain,
7936 					    &endoutchain,
7937 					    &auth,
7938 					    &auth_offset,
7939 					    stcb,
7940 					    chk->rec.chunk_id.id);
7941 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7942 				}
7943 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7944 				    (int)chk->rec.chunk_id.can_take_data,
7945 				    chk->send_size, chk->copy_by_ref);
7946 				if (outchain == NULL) {
7947 					*reason_code = 8;
7948 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7949 					return (ENOMEM);
7950 				}
7951 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7952 				/* update our MTU size */
7953 				if (mtu > (chk->send_size + omtu))
7954 					mtu -= (chk->send_size + omtu);
7955 				else
7956 					mtu = 0;
7957 				to_out += (chk->send_size + omtu);
7958 				/* Do clear IP_DF ? */
7959 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7960 					no_fragmentflg = 0;
7961 				}
7962 				if (chk->rec.chunk_id.can_take_data)
7963 					chk->data = NULL;
7964 				/* Mark things to be removed, if needed */
7965 				if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7966 				    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
7967 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7968 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7969 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7970 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7971 				    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7972 				    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7973 				    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7974 				    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7975 				    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7976 
7977 					if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7978 						hbflag = 1;
7979 						/*
7980 						 * JRS 5/14/07 - Set the
7981 						 * flag to say a heartbeat
7982 						 * is being sent.
7983 						 */
7984 						pf_hbflag = 1;
7985 					}
7986 					/* remove these chunks at the end */
7987 					if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7988 					    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7989 						/* turn off the timer */
7990 						if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7991 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7992 							    inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7993 						}
7994 					}
7995 					ctl_cnt++;
7996 				} else {
7997 					/*
7998 					 * Other chunks, since they have
7999 					 * timers running (i.e. COOKIE) we
8000 					 * just "trust" that it gets sent or
8001 					 * retransmitted.
8002 					 */
8003 					ctl_cnt++;
8004 					if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8005 						cookie = 1;
8006 						no_out_cnt = 1;
8007 					} else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
8008 						/*
8009 						 * Increment ecne send count
8010 						 * here this means we may be
8011 						 * over-zealous in our
8012 						 * counting if the send
8013 						 * fails, but its the best
8014 						 * place to do it (we used
8015 						 * to do it in the queue of
8016 						 * the chunk, but that did
8017 						 * not tell how many times
8018 						 * it was sent.
8019 						 */
8020 						SCTP_STAT_INCR(sctps_sendecne);
8021 					}
8022 					chk->sent = SCTP_DATAGRAM_SENT;
8023 					chk->snd_count++;
8024 				}
8025 				if (mtu == 0) {
8026 					/*
8027 					 * Ok we are out of room but we can
8028 					 * output without effecting the
8029 					 * flight size since this little guy
8030 					 * is a control only packet.
8031 					 */
8032 					if (asconf) {
8033 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8034 						/*
8035 						 * do NOT clear the asconf
8036 						 * flag as it is used to do
8037 						 * appropriate source
8038 						 * address selection.
8039 						 */
8040 					}
8041 					if (cookie) {
8042 						sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8043 						cookie = 0;
8044 					}
8045 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8046 					    (struct sockaddr *)&net->ro._l_addr,
8047 					    outchain,
8048 					    auth_offset, auth,
8049 					    stcb->asoc.authinfo.active_keyid,
8050 					    no_fragmentflg, 0, NULL, asconf,
8051 					    inp->sctp_lport, stcb->rport,
8052 					    htonl(stcb->asoc.peer_vtag),
8053 					    net->port, so_locked, NULL, NULL))) {
8054 						if (error == ENOBUFS) {
8055 							asoc->ifp_had_enobuf = 1;
8056 							SCTP_STAT_INCR(sctps_lowlevelerr);
8057 						}
8058 						if (from_where == 0) {
8059 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
8060 						}
8061 						/* error, could not output */
8062 						if (hbflag) {
8063 							if (*now_filled == 0) {
8064 								(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8065 								*now_filled = 1;
8066 								*now = net->last_sent_time;
8067 							} else {
8068 								net->last_sent_time = *now;
8069 							}
8070 							hbflag = 0;
8071 						}
8072 						if (error == EHOSTUNREACH) {
8073 							/*
8074 							 * Destination went
8075 							 * unreachable
8076 							 * during this send
8077 							 */
8078 							sctp_move_chunks_from_net(stcb, net);
8079 						}
8080 						*reason_code = 7;
8081 						continue;
8082 					} else
8083 						asoc->ifp_had_enobuf = 0;
8084 					/* Only HB or ASCONF advances time */
8085 					if (hbflag) {
8086 						if (*now_filled == 0) {
8087 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8088 							*now_filled = 1;
8089 							*now = net->last_sent_time;
8090 						} else {
8091 							net->last_sent_time = *now;
8092 						}
8093 						hbflag = 0;
8094 					}
8095 					/*
8096 					 * increase the number we sent, if a
8097 					 * cookie is sent we don't tell them
8098 					 * any was sent out.
8099 					 */
8100 					outchain = endoutchain = NULL;
8101 					auth = NULL;
8102 					auth_offset = 0;
8103 					if (!no_out_cnt)
8104 						*num_out += ctl_cnt;
8105 					/* recalc a clean slate and setup */
8106 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8107 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
8108 					} else {
8109 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
8110 					}
8111 					to_out = 0;
8112 					no_fragmentflg = 1;
8113 				}
8114 			}
8115 		}
8116 		/* JRI: if dest is in PF state, do not send data to it */
8117 		if ((asoc->sctp_cmt_on_off > 0) &&
8118 		    (asoc->sctp_cmt_pf > 0) &&
8119 		    (net->dest_state & SCTP_ADDR_PF)) {
8120 			goto no_data_fill;
8121 		}
8122 		if (net->flight_size >= net->cwnd) {
8123 			goto no_data_fill;
8124 		}
8125 		if ((asoc->sctp_cmt_on_off > 0) &&
8126 		    (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) &&
8127 		    (net->flight_size > max_rwnd_per_dest)) {
8128 			goto no_data_fill;
8129 		}
8130 		/*
8131 		 * We need a specific accounting for the usage of the send
8132 		 * buffer. We also need to check the number of messages per
8133 		 * net. For now, this is better than nothing and it disabled
8134 		 * by default...
8135 		 */
8136 		if ((asoc->sctp_cmt_on_off > 0) &&
8137 		    (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) &&
8138 		    (max_send_per_dest > 0) &&
8139 		    (net->flight_size > max_send_per_dest)) {
8140 			goto no_data_fill;
8141 		}
8142 		/*********************/
8143 		/* Data transmission */
8144 		/*********************/
8145 		/*
8146 		 * if AUTH for DATA is required and no AUTH has been added
8147 		 * yet, account for this in the mtu now... if no data can be
8148 		 * bundled, this adjustment won't matter anyways since the
8149 		 * packet will be going out...
8150 		 */
8151 		data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8152 		    stcb->asoc.peer_auth_chunks);
8153 		if (data_auth_reqd && (auth == NULL)) {
8154 			mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8155 		}
8156 		/* now lets add any data within the MTU constraints */
8157 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8158 		case AF_INET:
8159 			if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8160 				omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8161 			else
8162 				omtu = 0;
8163 			break;
8164 #ifdef INET6
8165 		case AF_INET6:
8166 			if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8167 				omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8168 			else
8169 				omtu = 0;
8170 			break;
8171 #endif
8172 		default:
8173 			/* TSNH */
8174 			omtu = 0;
8175 			break;
8176 		}
8177 		if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) &&
8178 		    (skip_data_for_this_net == 0)) ||
8179 		    (cookie)) {
8180 			TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
8181 				if (no_data_chunks) {
8182 					/* let only control go out */
8183 					*reason_code = 1;
8184 					break;
8185 				}
8186 				if (net->flight_size >= net->cwnd) {
8187 					/* skip this net, no room for data */
8188 					*reason_code = 2;
8189 					break;
8190 				}
8191 				if ((chk->whoTo != NULL) &&
8192 				    (chk->whoTo != net)) {
8193 					/* Don't send the chunk on this net */
8194 					continue;
8195 				}
8196 				if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8197 					/*-
8198 					 * strange, we have a chunk that is
8199 					 * to big for its destination and
8200 					 * yet no fragment ok flag.
8201 					 * Something went wrong when the
8202 					 * PMTU changed...we did not mark
8203 					 * this chunk for some reason?? I
8204 					 * will fix it here by letting IP
8205 					 * fragment it for now and printing
8206 					 * a warning. This really should not
8207 					 * happen ...
8208 					 */
8209 					SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8210 					    chk->send_size, mtu);
8211 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8212 				}
8213 				if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8214 				    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
8215 					struct sctp_data_chunk *dchkh;
8216 
8217 					dchkh = mtod(chk->data, struct sctp_data_chunk *);
8218 					dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8219 				}
8220 				if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8221 				    ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8222 					/* ok we will add this one */
8223 
8224 					/*
8225 					 * Add an AUTH chunk, if chunk
8226 					 * requires it, save the offset into
8227 					 * the chain for AUTH
8228 					 */
8229 					if (data_auth_reqd) {
8230 						if (auth == NULL) {
8231 							outchain = sctp_add_auth_chunk(outchain,
8232 							    &endoutchain,
8233 							    &auth,
8234 							    &auth_offset,
8235 							    stcb,
8236 							    SCTP_DATA);
8237 							auth_keyid = chk->auth_keyid;
8238 							override_ok = 0;
8239 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8240 						} else if (override_ok) {
8241 							/*
8242 							 * use this data's
8243 							 * keyid
8244 							 */
8245 							auth_keyid = chk->auth_keyid;
8246 							override_ok = 0;
8247 						} else if (auth_keyid != chk->auth_keyid) {
8248 							/*
8249 							 * different keyid,
8250 							 * so done bundling
8251 							 */
8252 							break;
8253 						}
8254 					}
8255 					outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8256 					    chk->send_size, chk->copy_by_ref);
8257 					if (outchain == NULL) {
8258 						SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8259 						if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8260 							sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8261 						}
8262 						*reason_code = 3;
8263 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8264 						return (ENOMEM);
8265 					}
8266 					/* upate our MTU size */
8267 					/* Do clear IP_DF ? */
8268 					if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8269 						no_fragmentflg = 0;
8270 					}
8271 					/* unsigned subtraction of mtu */
8272 					if (mtu > chk->send_size)
8273 						mtu -= chk->send_size;
8274 					else
8275 						mtu = 0;
8276 					/* unsigned subtraction of r_mtu */
8277 					if (r_mtu > chk->send_size)
8278 						r_mtu -= chk->send_size;
8279 					else
8280 						r_mtu = 0;
8281 
8282 					to_out += chk->send_size;
8283 					if ((to_out > mx_mtu) && no_fragmentflg) {
8284 #ifdef INVARIANTS
8285 						panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8286 #else
8287 						SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8288 						    mx_mtu, to_out);
8289 #endif
8290 					}
8291 					chk->window_probe = 0;
8292 					data_list[bundle_at++] = chk;
8293 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8294 						mtu = 0;
8295 						break;
8296 					}
8297 					if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8298 						if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8299 							SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8300 						} else {
8301 							SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8302 						}
8303 						if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8304 						    ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8305 							/*
8306 							 * Count number of
8307 							 * user msg's that
8308 							 * were fragmented
8309 							 * we do this by
8310 							 * counting when we
8311 							 * see a LAST
8312 							 * fragment only.
8313 							 */
8314 							SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8315 					}
8316 					if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8317 						if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8318 							data_list[0]->window_probe = 1;
8319 							net->window_probe = 1;
8320 						}
8321 						break;
8322 					}
8323 				} else {
8324 					/*
8325 					 * Must be sent in order of the
8326 					 * TSN's (on a network)
8327 					 */
8328 					break;
8329 				}
8330 			}	/* for (chunk gather loop for this net) */
8331 		}		/* if asoc.state OPEN */
8332 no_data_fill:
8333 		/* Is there something to send for this destination? */
8334 		if (outchain) {
8335 			/* We may need to start a control timer or two */
8336 			if (asconf) {
8337 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8338 				    stcb, net);
8339 				/*
8340 				 * do NOT clear the asconf flag as it is
8341 				 * used to do appropriate source address
8342 				 * selection.
8343 				 */
8344 			}
8345 			if (cookie) {
8346 				sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8347 				cookie = 0;
8348 			}
8349 			/* must start a send timer if data is being sent */
8350 			if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8351 				/*
8352 				 * no timer running on this destination
8353 				 * restart it.
8354 				 */
8355 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8356 			} else if ((asoc->sctp_cmt_on_off > 0) &&
8357 				    (asoc->sctp_cmt_pf > 0) &&
8358 				    pf_hbflag &&
8359 				    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
8360 			    (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8361 				/*
8362 				 * JRS 5/14/07 - If a HB has been sent to a
8363 				 * PF destination and no T3 timer is
8364 				 * currently running, start the T3 timer to
8365 				 * track the HBs that were sent.
8366 				 */
8367 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8368 			}
8369 			/* Now send it, if there is anything to send :> */
8370 			if ((error = sctp_lowlevel_chunk_output(inp,
8371 			    stcb,
8372 			    net,
8373 			    (struct sockaddr *)&net->ro._l_addr,
8374 			    outchain,
8375 			    auth_offset,
8376 			    auth,
8377 			    auth_keyid,
8378 			    no_fragmentflg,
8379 			    bundle_at,
8380 			    data_list[0],
8381 			    asconf,
8382 			    inp->sctp_lport, stcb->rport,
8383 			    htonl(stcb->asoc.peer_vtag),
8384 			    net->port, so_locked, NULL, NULL))) {
8385 				/* error, we could not output */
8386 				if (error == ENOBUFS) {
8387 					SCTP_STAT_INCR(sctps_lowlevelerr);
8388 					asoc->ifp_had_enobuf = 1;
8389 				}
8390 				if (from_where == 0) {
8391 					SCTP_STAT_INCR(sctps_lowlevelerrusr);
8392 				}
8393 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8394 				if (hbflag) {
8395 					if (*now_filled == 0) {
8396 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8397 						*now_filled = 1;
8398 						*now = net->last_sent_time;
8399 					} else {
8400 						net->last_sent_time = *now;
8401 					}
8402 					hbflag = 0;
8403 				}
8404 				if (error == EHOSTUNREACH) {
8405 					/*
8406 					 * Destination went unreachable
8407 					 * during this send
8408 					 */
8409 					sctp_move_chunks_from_net(stcb, net);
8410 				}
8411 				*reason_code = 6;
8412 				/*-
8413 				 * I add this line to be paranoid. As far as
8414 				 * I can tell the continue, takes us back to
8415 				 * the top of the for, but just to make sure
8416 				 * I will reset these again here.
8417 				 */
8418 				ctl_cnt = bundle_at = 0;
8419 				continue;	/* This takes us back to the
8420 						 * for() for the nets. */
8421 			} else {
8422 				asoc->ifp_had_enobuf = 0;
8423 			}
8424 			outchain = endoutchain = NULL;
8425 			auth = NULL;
8426 			auth_offset = 0;
8427 			if (bundle_at || hbflag) {
8428 				/* For data/asconf and hb set time */
8429 				if (*now_filled == 0) {
8430 					(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8431 					*now_filled = 1;
8432 					*now = net->last_sent_time;
8433 				} else {
8434 					net->last_sent_time = *now;
8435 				}
8436 			}
8437 			if (!no_out_cnt) {
8438 				*num_out += (ctl_cnt + bundle_at);
8439 			}
8440 			if (bundle_at) {
8441 				/* setup for a RTO measurement */
8442 				tsns_sent = data_list[0]->rec.data.TSN_seq;
8443 				/* fill time if not already filled */
8444 				if (*now_filled == 0) {
8445 					(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8446 					*now_filled = 1;
8447 					*now = asoc->time_last_sent;
8448 				} else {
8449 					asoc->time_last_sent = *now;
8450 				}
8451 				if (net->rto_needed) {
8452 					data_list[0]->do_rtt = 1;
8453 					net->rto_needed = 0;
8454 				}
8455 				SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8456 				sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8457 				if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8458 					if (net->flight_size < net->cwnd) {
8459 						/* start or restart it */
8460 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8461 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8462 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
8463 						}
8464 						SCTP_STAT_INCR(sctps_earlyfrstrout);
8465 						sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
8466 					} else {
8467 						/* stop it if its running */
8468 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8469 							SCTP_STAT_INCR(sctps_earlyfrstpout);
8470 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8471 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
8472 						}
8473 					}
8474 				}
8475 			}
8476 			if (one_chunk) {
8477 				break;
8478 			}
8479 		}
8480 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8481 			sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8482 		}
8483 	}
8484 	if (old_start_at == NULL) {
8485 		old_start_at = start_at;
8486 		start_at = TAILQ_FIRST(&asoc->nets);
8487 		if (old_start_at)
8488 			goto again_one_more_time;
8489 	}
8490 	/*
8491 	 * At the end there should be no NON timed chunks hanging on this
8492 	 * queue.
8493 	 */
8494 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8495 		sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8496 	}
8497 	if ((*num_out == 0) && (*reason_code == 0)) {
8498 		*reason_code = 4;
8499 	} else {
8500 		*reason_code = 5;
8501 	}
8502 	sctp_clean_up_ctl(stcb, asoc, so_locked);
8503 	return (0);
8504 }
8505 
8506 void
8507 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8508 {
8509 	/*-
8510 	 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8511 	 * the control chunk queue.
8512 	 */
8513 	struct sctp_chunkhdr *hdr;
8514 	struct sctp_tmit_chunk *chk;
8515 	struct mbuf *mat;
8516 
8517 	SCTP_TCB_LOCK_ASSERT(stcb);
8518 	sctp_alloc_a_chunk(stcb, chk);
8519 	if (chk == NULL) {
8520 		/* no memory */
8521 		sctp_m_freem(op_err);
8522 		return;
8523 	}
8524 	chk->copy_by_ref = 0;
8525 	SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8526 	if (op_err == NULL) {
8527 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
8528 		return;
8529 	}
8530 	chk->send_size = 0;
8531 	mat = op_err;
8532 	while (mat != NULL) {
8533 		chk->send_size += SCTP_BUF_LEN(mat);
8534 		mat = SCTP_BUF_NEXT(mat);
8535 	}
8536 	chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8537 	chk->rec.chunk_id.can_take_data = 1;
8538 	chk->sent = SCTP_DATAGRAM_UNSENT;
8539 	chk->snd_count = 0;
8540 	chk->flags = 0;
8541 	chk->asoc = &stcb->asoc;
8542 	chk->data = op_err;
8543 	chk->whoTo = chk->asoc->primary_destination;
8544 	atomic_add_int(&chk->whoTo->ref_count, 1);
8545 	hdr = mtod(op_err, struct sctp_chunkhdr *);
8546 	hdr->chunk_type = SCTP_OPERATION_ERROR;
8547 	hdr->chunk_flags = 0;
8548 	hdr->chunk_length = htons(chk->send_size);
8549 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8550 	    chk,
8551 	    sctp_next);
8552 	chk->asoc->ctrl_queue_cnt++;
8553 }
8554 
8555 int
8556 sctp_send_cookie_echo(struct mbuf *m,
8557     int offset,
8558     struct sctp_tcb *stcb,
8559     struct sctp_nets *net)
8560 {
8561 	/*-
8562 	 * pull out the cookie and put it at the front of the control chunk
8563 	 * queue.
8564 	 */
8565 	int at;
8566 	struct mbuf *cookie;
8567 	struct sctp_paramhdr parm, *phdr;
8568 	struct sctp_chunkhdr *hdr;
8569 	struct sctp_tmit_chunk *chk;
8570 	uint16_t ptype, plen;
8571 
8572 	/* First find the cookie in the param area */
8573 	cookie = NULL;
8574 	at = offset + sizeof(struct sctp_init_chunk);
8575 
8576 	SCTP_TCB_LOCK_ASSERT(stcb);
8577 	do {
8578 		phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8579 		if (phdr == NULL) {
8580 			return (-3);
8581 		}
8582 		ptype = ntohs(phdr->param_type);
8583 		plen = ntohs(phdr->param_length);
8584 		if (ptype == SCTP_STATE_COOKIE) {
8585 			int pad;
8586 
8587 			/* found the cookie */
8588 			if ((pad = (plen % 4))) {
8589 				plen += 4 - pad;
8590 			}
8591 			cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8592 			if (cookie == NULL) {
8593 				/* No memory */
8594 				return (-2);
8595 			}
8596 #ifdef SCTP_MBUF_LOGGING
8597 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8598 				struct mbuf *mat;
8599 
8600 				mat = cookie;
8601 				while (mat) {
8602 					if (SCTP_BUF_IS_EXTENDED(mat)) {
8603 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8604 					}
8605 					mat = SCTP_BUF_NEXT(mat);
8606 				}
8607 			}
8608 #endif
8609 			break;
8610 		}
8611 		at += SCTP_SIZE32(plen);
8612 	} while (phdr);
8613 	if (cookie == NULL) {
8614 		/* Did not find the cookie */
8615 		return (-3);
8616 	}
8617 	/* ok, we got the cookie lets change it into a cookie echo chunk */
8618 
8619 	/* first the change from param to cookie */
8620 	hdr = mtod(cookie, struct sctp_chunkhdr *);
8621 	hdr->chunk_type = SCTP_COOKIE_ECHO;
8622 	hdr->chunk_flags = 0;
8623 	/* get the chunk stuff now and place it in the FRONT of the queue */
8624 	sctp_alloc_a_chunk(stcb, chk);
8625 	if (chk == NULL) {
8626 		/* no memory */
8627 		sctp_m_freem(cookie);
8628 		return (-5);
8629 	}
8630 	chk->copy_by_ref = 0;
8631 	chk->send_size = plen;
8632 	chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8633 	chk->rec.chunk_id.can_take_data = 0;
8634 	chk->sent = SCTP_DATAGRAM_UNSENT;
8635 	chk->snd_count = 0;
8636 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8637 	chk->asoc = &stcb->asoc;
8638 	chk->data = cookie;
8639 	chk->whoTo = chk->asoc->primary_destination;
8640 	atomic_add_int(&chk->whoTo->ref_count, 1);
8641 	TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8642 	chk->asoc->ctrl_queue_cnt++;
8643 	return (0);
8644 }
8645 
8646 void
8647 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8648     struct mbuf *m,
8649     int offset,
8650     int chk_length,
8651     struct sctp_nets *net)
8652 {
8653 	/*
8654 	 * take a HB request and make it into a HB ack and send it.
8655 	 */
8656 	struct mbuf *outchain;
8657 	struct sctp_chunkhdr *chdr;
8658 	struct sctp_tmit_chunk *chk;
8659 
8660 
8661 	if (net == NULL)
8662 		/* must have a net pointer */
8663 		return;
8664 
8665 	outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
8666 	if (outchain == NULL) {
8667 		/* gak out of memory */
8668 		return;
8669 	}
8670 #ifdef SCTP_MBUF_LOGGING
8671 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8672 		struct mbuf *mat;
8673 
8674 		mat = outchain;
8675 		while (mat) {
8676 			if (SCTP_BUF_IS_EXTENDED(mat)) {
8677 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8678 			}
8679 			mat = SCTP_BUF_NEXT(mat);
8680 		}
8681 	}
8682 #endif
8683 	chdr = mtod(outchain, struct sctp_chunkhdr *);
8684 	chdr->chunk_type = SCTP_HEARTBEAT_ACK;
8685 	chdr->chunk_flags = 0;
8686 	if (chk_length % 4) {
8687 		/* need pad */
8688 		uint32_t cpthis = 0;
8689 		int padlen;
8690 
8691 		padlen = 4 - (chk_length % 4);
8692 		m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
8693 	}
8694 	sctp_alloc_a_chunk(stcb, chk);
8695 	if (chk == NULL) {
8696 		/* no memory */
8697 		sctp_m_freem(outchain);
8698 		return;
8699 	}
8700 	chk->copy_by_ref = 0;
8701 	chk->send_size = chk_length;
8702 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
8703 	chk->rec.chunk_id.can_take_data = 1;
8704 	chk->sent = SCTP_DATAGRAM_UNSENT;
8705 	chk->snd_count = 0;
8706 	chk->flags = 0;
8707 	chk->asoc = &stcb->asoc;
8708 	chk->data = outchain;
8709 	chk->whoTo = net;
8710 	atomic_add_int(&chk->whoTo->ref_count, 1);
8711 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8712 	chk->asoc->ctrl_queue_cnt++;
8713 }
8714 
8715 void
8716 sctp_send_cookie_ack(struct sctp_tcb *stcb)
8717 {
8718 	/* formulate and queue a cookie-ack back to sender */
8719 	struct mbuf *cookie_ack;
8720 	struct sctp_chunkhdr *hdr;
8721 	struct sctp_tmit_chunk *chk;
8722 
8723 	cookie_ack = NULL;
8724 	SCTP_TCB_LOCK_ASSERT(stcb);
8725 
8726 	cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
8727 	if (cookie_ack == NULL) {
8728 		/* no mbuf's */
8729 		return;
8730 	}
8731 	SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
8732 	sctp_alloc_a_chunk(stcb, chk);
8733 	if (chk == NULL) {
8734 		/* no memory */
8735 		sctp_m_freem(cookie_ack);
8736 		return;
8737 	}
8738 	chk->copy_by_ref = 0;
8739 	chk->send_size = sizeof(struct sctp_chunkhdr);
8740 	chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
8741 	chk->rec.chunk_id.can_take_data = 1;
8742 	chk->sent = SCTP_DATAGRAM_UNSENT;
8743 	chk->snd_count = 0;
8744 	chk->flags = 0;
8745 	chk->asoc = &stcb->asoc;
8746 	chk->data = cookie_ack;
8747 	if (chk->asoc->last_control_chunk_from != NULL) {
8748 		chk->whoTo = chk->asoc->last_control_chunk_from;
8749 	} else {
8750 		chk->whoTo = chk->asoc->primary_destination;
8751 	}
8752 	atomic_add_int(&chk->whoTo->ref_count, 1);
8753 	hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
8754 	hdr->chunk_type = SCTP_COOKIE_ACK;
8755 	hdr->chunk_flags = 0;
8756 	hdr->chunk_length = htons(chk->send_size);
8757 	SCTP_BUF_LEN(cookie_ack) = chk->send_size;
8758 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8759 	chk->asoc->ctrl_queue_cnt++;
8760 	return;
8761 }
8762 
8763 
8764 void
8765 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
8766 {
8767 	/* formulate and queue a SHUTDOWN-ACK back to the sender */
8768 	struct mbuf *m_shutdown_ack;
8769 	struct sctp_shutdown_ack_chunk *ack_cp;
8770 	struct sctp_tmit_chunk *chk;
8771 
8772 	m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8773 	if (m_shutdown_ack == NULL) {
8774 		/* no mbuf's */
8775 		return;
8776 	}
8777 	SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
8778 	sctp_alloc_a_chunk(stcb, chk);
8779 	if (chk == NULL) {
8780 		/* no memory */
8781 		sctp_m_freem(m_shutdown_ack);
8782 		return;
8783 	}
8784 	chk->copy_by_ref = 0;
8785 	chk->send_size = sizeof(struct sctp_chunkhdr);
8786 	chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
8787 	chk->rec.chunk_id.can_take_data = 1;
8788 	chk->sent = SCTP_DATAGRAM_UNSENT;
8789 	chk->snd_count = 0;
8790 	chk->flags = 0;
8791 	chk->asoc = &stcb->asoc;
8792 	chk->data = m_shutdown_ack;
8793 	chk->whoTo = net;
8794 	atomic_add_int(&net->ref_count, 1);
8795 
8796 	ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
8797 	ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
8798 	ack_cp->ch.chunk_flags = 0;
8799 	ack_cp->ch.chunk_length = htons(chk->send_size);
8800 	SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
8801 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8802 	chk->asoc->ctrl_queue_cnt++;
8803 	return;
8804 }
8805 
8806 void
8807 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
8808 {
8809 	/* formulate and queue a SHUTDOWN to the sender */
8810 	struct mbuf *m_shutdown;
8811 	struct sctp_shutdown_chunk *shutdown_cp;
8812 	struct sctp_tmit_chunk *chk;
8813 
8814 	m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8815 	if (m_shutdown == NULL) {
8816 		/* no mbuf's */
8817 		return;
8818 	}
8819 	SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
8820 	sctp_alloc_a_chunk(stcb, chk);
8821 	if (chk == NULL) {
8822 		/* no memory */
8823 		sctp_m_freem(m_shutdown);
8824 		return;
8825 	}
8826 	chk->copy_by_ref = 0;
8827 	chk->send_size = sizeof(struct sctp_shutdown_chunk);
8828 	chk->rec.chunk_id.id = SCTP_SHUTDOWN;
8829 	chk->rec.chunk_id.can_take_data = 1;
8830 	chk->sent = SCTP_DATAGRAM_UNSENT;
8831 	chk->snd_count = 0;
8832 	chk->flags = 0;
8833 	chk->asoc = &stcb->asoc;
8834 	chk->data = m_shutdown;
8835 	chk->whoTo = net;
8836 	atomic_add_int(&net->ref_count, 1);
8837 
8838 	shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
8839 	shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
8840 	shutdown_cp->ch.chunk_flags = 0;
8841 	shutdown_cp->ch.chunk_length = htons(chk->send_size);
8842 	shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
8843 	SCTP_BUF_LEN(m_shutdown) = chk->send_size;
8844 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8845 	chk->asoc->ctrl_queue_cnt++;
8846 	return;
8847 }
8848 
8849 void
8850 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
8851 {
8852 	/*
8853 	 * formulate and queue an ASCONF to the peer. ASCONF parameters
8854 	 * should be queued on the assoc queue.
8855 	 */
8856 	struct sctp_tmit_chunk *chk;
8857 	struct mbuf *m_asconf;
8858 	int len;
8859 
8860 	SCTP_TCB_LOCK_ASSERT(stcb);
8861 
8862 	if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
8863 	    (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
8864 		/* can't send a new one if there is one in flight already */
8865 		return;
8866 	}
8867 	/* compose an ASCONF chunk, maximum length is PMTU */
8868 	m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
8869 	if (m_asconf == NULL) {
8870 		return;
8871 	}
8872 	sctp_alloc_a_chunk(stcb, chk);
8873 	if (chk == NULL) {
8874 		/* no memory */
8875 		sctp_m_freem(m_asconf);
8876 		return;
8877 	}
8878 	chk->copy_by_ref = 0;
8879 	chk->data = m_asconf;
8880 	chk->send_size = len;
8881 	chk->rec.chunk_id.id = SCTP_ASCONF;
8882 	chk->rec.chunk_id.can_take_data = 0;
8883 	chk->sent = SCTP_DATAGRAM_UNSENT;
8884 	chk->snd_count = 0;
8885 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8886 	chk->asoc = &stcb->asoc;
8887 	chk->whoTo = net;
8888 	atomic_add_int(&chk->whoTo->ref_count, 1);
8889 	TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
8890 	chk->asoc->ctrl_queue_cnt++;
8891 	return;
8892 }
8893 
8894 void
8895 sctp_send_asconf_ack(struct sctp_tcb *stcb)
8896 {
8897 	/*
8898 	 * formulate and queue a asconf-ack back to sender. the asconf-ack
8899 	 * must be stored in the tcb.
8900 	 */
8901 	struct sctp_tmit_chunk *chk;
8902 	struct sctp_asconf_ack *ack, *latest_ack;
8903 	struct mbuf *m_ack, *m;
8904 	struct sctp_nets *net = NULL;
8905 
8906 	SCTP_TCB_LOCK_ASSERT(stcb);
8907 	/* Get the latest ASCONF-ACK */
8908 	latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
8909 	if (latest_ack == NULL) {
8910 		return;
8911 	}
8912 	if (latest_ack->last_sent_to != NULL &&
8913 	    latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
8914 		/* we're doing a retransmission */
8915 		net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
8916 		if (net == NULL) {
8917 			/* no alternate */
8918 			if (stcb->asoc.last_control_chunk_from == NULL)
8919 				net = stcb->asoc.primary_destination;
8920 			else
8921 				net = stcb->asoc.last_control_chunk_from;
8922 		}
8923 	} else {
8924 		/* normal case */
8925 		if (stcb->asoc.last_control_chunk_from == NULL)
8926 			net = stcb->asoc.primary_destination;
8927 		else
8928 			net = stcb->asoc.last_control_chunk_from;
8929 	}
8930 	latest_ack->last_sent_to = net;
8931 
8932 	TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
8933 		if (ack->data == NULL) {
8934 			continue;
8935 		}
8936 		/* copy the asconf_ack */
8937 		m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
8938 		if (m_ack == NULL) {
8939 			/* couldn't copy it */
8940 			return;
8941 		}
8942 #ifdef SCTP_MBUF_LOGGING
8943 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8944 			struct mbuf *mat;
8945 
8946 			mat = m_ack;
8947 			while (mat) {
8948 				if (SCTP_BUF_IS_EXTENDED(mat)) {
8949 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8950 				}
8951 				mat = SCTP_BUF_NEXT(mat);
8952 			}
8953 		}
8954 #endif
8955 
8956 		sctp_alloc_a_chunk(stcb, chk);
8957 		if (chk == NULL) {
8958 			/* no memory */
8959 			if (m_ack)
8960 				sctp_m_freem(m_ack);
8961 			return;
8962 		}
8963 		chk->copy_by_ref = 0;
8964 
8965 		chk->whoTo = net;
8966 		chk->data = m_ack;
8967 		chk->send_size = 0;
8968 		/* Get size */
8969 		m = m_ack;
8970 		chk->send_size = ack->len;
8971 		chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8972 		chk->rec.chunk_id.can_take_data = 1;
8973 		chk->sent = SCTP_DATAGRAM_UNSENT;
8974 		chk->snd_count = 0;
8975 		chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;	/* XXX */
8976 		chk->asoc = &stcb->asoc;
8977 		atomic_add_int(&chk->whoTo->ref_count, 1);
8978 
8979 		TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8980 		chk->asoc->ctrl_queue_cnt++;
8981 	}
8982 	return;
8983 }
8984 
8985 
8986 static int
8987 sctp_chunk_retransmission(struct sctp_inpcb *inp,
8988     struct sctp_tcb *stcb,
8989     struct sctp_association *asoc,
8990     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
8991 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8992     SCTP_UNUSED
8993 #endif
8994 )
8995 {
8996 	/*-
8997 	 * send out one MTU of retransmission. If fast_retransmit is
8998 	 * happening we ignore the cwnd. Otherwise we obey the cwnd and
8999 	 * rwnd. For a Cookie or Asconf in the control chunk queue we
9000 	 * retransmit them by themselves.
9001 	 *
9002 	 * For data chunks we will pick out the lowest TSN's in the sent_queue
9003 	 * marked for resend and bundle them all together (up to a MTU of
9004 	 * destination). The address to send to should have been
9005 	 * selected/changed where the retransmission was marked (i.e. in FR
9006 	 * or t3-timeout routines).
9007 	 */
9008 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
9009 	struct sctp_tmit_chunk *chk, *fwd;
9010 	struct mbuf *m, *endofchain;
9011 	struct sctp_nets *net = NULL;
9012 	uint32_t tsns_sent = 0;
9013 	int no_fragmentflg, bundle_at, cnt_thru;
9014 	unsigned int mtu;
9015 	int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
9016 	struct sctp_auth_chunk *auth = NULL;
9017 	uint32_t auth_offset = 0;
9018 	uint16_t auth_keyid;
9019 	int override_ok = 1;
9020 	int data_auth_reqd = 0;
9021 	uint32_t dmtu = 0;
9022 
9023 	SCTP_TCB_LOCK_ASSERT(stcb);
9024 	tmr_started = ctl_cnt = bundle_at = error = 0;
9025 	no_fragmentflg = 1;
9026 	fwd_tsn = 0;
9027 	*cnt_out = 0;
9028 	fwd = NULL;
9029 	endofchain = m = NULL;
9030 	auth_keyid = stcb->asoc.authinfo.active_keyid;
9031 #ifdef SCTP_AUDITING_ENABLED
9032 	sctp_audit_log(0xC3, 1);
9033 #endif
9034 	if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
9035 	    (TAILQ_EMPTY(&asoc->control_send_queue))) {
9036 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
9037 		    asoc->sent_queue_retran_cnt);
9038 		asoc->sent_queue_cnt = 0;
9039 		asoc->sent_queue_cnt_removeable = 0;
9040 		/* send back 0/0 so we enter normal transmission */
9041 		*cnt_out = 0;
9042 		return (0);
9043 	}
9044 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9045 		if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
9046 		    (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
9047 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
9048 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
9049 				continue;
9050 			}
9051 			if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
9052 				if (chk != asoc->str_reset) {
9053 					/*
9054 					 * not eligible for retran if its
9055 					 * not ours
9056 					 */
9057 					continue;
9058 				}
9059 			}
9060 			ctl_cnt++;
9061 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9062 				fwd_tsn = 1;
9063 				fwd = chk;
9064 			}
9065 			/*
9066 			 * Add an AUTH chunk, if chunk requires it save the
9067 			 * offset into the chain for AUTH
9068 			 */
9069 			if ((auth == NULL) &&
9070 			    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9071 			    stcb->asoc.peer_auth_chunks))) {
9072 				m = sctp_add_auth_chunk(m, &endofchain,
9073 				    &auth, &auth_offset,
9074 				    stcb,
9075 				    chk->rec.chunk_id.id);
9076 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9077 			}
9078 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9079 			break;
9080 		}
9081 	}
9082 	one_chunk = 0;
9083 	cnt_thru = 0;
9084 	/* do we have control chunks to retransmit? */
9085 	if (m != NULL) {
9086 		/* Start a timer no matter if we suceed or fail */
9087 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9088 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
9089 		} else if (chk->rec.chunk_id.id == SCTP_ASCONF)
9090 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
9091 		chk->snd_count++;	/* update our count */
9092 		if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
9093 		    (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
9094 		    auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9095 		    no_fragmentflg, 0, NULL, 0,
9096 		    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9097 		    chk->whoTo->port, so_locked, NULL, NULL))) {
9098 			SCTP_STAT_INCR(sctps_lowlevelerr);
9099 			return (error);
9100 		}
9101 		m = endofchain = NULL;
9102 		auth = NULL;
9103 		auth_offset = 0;
9104 		/*
9105 		 * We don't want to mark the net->sent time here since this
9106 		 * we use this for HB and retrans cannot measure RTT
9107 		 */
9108 		/* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
9109 		*cnt_out += 1;
9110 		chk->sent = SCTP_DATAGRAM_SENT;
9111 		sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
9112 		if (fwd_tsn == 0) {
9113 			return (0);
9114 		} else {
9115 			/* Clean up the fwd-tsn list */
9116 			sctp_clean_up_ctl(stcb, asoc, so_locked);
9117 			return (0);
9118 		}
9119 	}
9120 	/*
9121 	 * Ok, it is just data retransmission we need to do or that and a
9122 	 * fwd-tsn with it all.
9123 	 */
9124 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
9125 		return (SCTP_RETRAN_DONE);
9126 	}
9127 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
9128 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
9129 		/* not yet open, resend the cookie and that is it */
9130 		return (1);
9131 	}
9132 #ifdef SCTP_AUDITING_ENABLED
9133 	sctp_auditing(20, inp, stcb, NULL);
9134 #endif
9135 	data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9136 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9137 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
9138 			/* No, not sent to this net or not ready for rtx */
9139 			continue;
9140 		}
9141 		if (chk->data == NULL) {
9142 			printf("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
9143 			    chk->rec.data.TSN_seq, chk->snd_count, chk->sent);
9144 			continue;
9145 		}
9146 		if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9147 		    (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9148 			/* Gak, we have exceeded max unlucky retran, abort! */
9149 			SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
9150 			    chk->snd_count,
9151 			    SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
9152 			atomic_add_int(&stcb->asoc.refcnt, 1);
9153 			sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
9154 			SCTP_TCB_LOCK(stcb);
9155 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
9156 			return (SCTP_RETRAN_EXIT);
9157 		}
9158 		/* pick up the net */
9159 		net = chk->whoTo;
9160 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9161 			mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9162 		} else {
9163 			mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9164 		}
9165 
9166 		if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9167 			/* No room in peers rwnd */
9168 			uint32_t tsn;
9169 
9170 			tsn = asoc->last_acked_seq + 1;
9171 			if (tsn == chk->rec.data.TSN_seq) {
9172 				/*
9173 				 * we make a special exception for this
9174 				 * case. The peer has no rwnd but is missing
9175 				 * the lowest chunk.. which is probably what
9176 				 * is holding up the rwnd.
9177 				 */
9178 				goto one_chunk_around;
9179 			}
9180 			return (1);
9181 		}
9182 one_chunk_around:
9183 		if (asoc->peers_rwnd < mtu) {
9184 			one_chunk = 1;
9185 			if ((asoc->peers_rwnd == 0) &&
9186 			    (asoc->total_flight == 0)) {
9187 				chk->window_probe = 1;
9188 				chk->whoTo->window_probe = 1;
9189 			}
9190 		}
9191 #ifdef SCTP_AUDITING_ENABLED
9192 		sctp_audit_log(0xC3, 2);
9193 #endif
9194 		bundle_at = 0;
9195 		m = NULL;
9196 		net->fast_retran_ip = 0;
9197 		if (chk->rec.data.doing_fast_retransmit == 0) {
9198 			/*
9199 			 * if no FR in progress skip destination that have
9200 			 * flight_size > cwnd.
9201 			 */
9202 			if (net->flight_size >= net->cwnd) {
9203 				continue;
9204 			}
9205 		} else {
9206 			/*
9207 			 * Mark the destination net to have FR recovery
9208 			 * limits put on it.
9209 			 */
9210 			*fr_done = 1;
9211 			net->fast_retran_ip = 1;
9212 		}
9213 
9214 		/*
9215 		 * if no AUTH is yet included and this chunk requires it,
9216 		 * make sure to account for it.  We don't apply the size
9217 		 * until the AUTH chunk is actually added below in case
9218 		 * there is no room for this chunk.
9219 		 */
9220 		if (data_auth_reqd && (auth == NULL)) {
9221 			dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9222 		} else
9223 			dmtu = 0;
9224 
9225 		if ((chk->send_size <= (mtu - dmtu)) ||
9226 		    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9227 			/* ok we will add this one */
9228 			if (data_auth_reqd) {
9229 				if (auth == NULL) {
9230 					m = sctp_add_auth_chunk(m,
9231 					    &endofchain,
9232 					    &auth,
9233 					    &auth_offset,
9234 					    stcb,
9235 					    SCTP_DATA);
9236 					auth_keyid = chk->auth_keyid;
9237 					override_ok = 0;
9238 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9239 				} else if (override_ok) {
9240 					auth_keyid = chk->auth_keyid;
9241 					override_ok = 0;
9242 				} else if (chk->auth_keyid != auth_keyid) {
9243 					/* different keyid, so done bundling */
9244 					break;
9245 				}
9246 			}
9247 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9248 			if (m == NULL) {
9249 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9250 				return (ENOMEM);
9251 			}
9252 			/* Do clear IP_DF ? */
9253 			if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9254 				no_fragmentflg = 0;
9255 			}
9256 			/* upate our MTU size */
9257 			if (mtu > (chk->send_size + dmtu))
9258 				mtu -= (chk->send_size + dmtu);
9259 			else
9260 				mtu = 0;
9261 			data_list[bundle_at++] = chk;
9262 			if (one_chunk && (asoc->total_flight <= 0)) {
9263 				SCTP_STAT_INCR(sctps_windowprobed);
9264 			}
9265 		}
9266 		if (one_chunk == 0) {
9267 			/*
9268 			 * now are there anymore forward from chk to pick
9269 			 * up?
9270 			 */
9271 			fwd = TAILQ_NEXT(chk, sctp_next);
9272 			while (fwd) {
9273 				if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9274 					/* Nope, not for retran */
9275 					fwd = TAILQ_NEXT(fwd, sctp_next);
9276 					continue;
9277 				}
9278 				if (fwd->whoTo != net) {
9279 					/* Nope, not the net in question */
9280 					fwd = TAILQ_NEXT(fwd, sctp_next);
9281 					continue;
9282 				}
9283 				if (data_auth_reqd && (auth == NULL)) {
9284 					dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9285 				} else
9286 					dmtu = 0;
9287 				if (fwd->send_size <= (mtu - dmtu)) {
9288 					if (data_auth_reqd) {
9289 						if (auth == NULL) {
9290 							m = sctp_add_auth_chunk(m,
9291 							    &endofchain,
9292 							    &auth,
9293 							    &auth_offset,
9294 							    stcb,
9295 							    SCTP_DATA);
9296 							auth_keyid = fwd->auth_keyid;
9297 							override_ok = 0;
9298 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9299 						} else if (override_ok) {
9300 							auth_keyid = fwd->auth_keyid;
9301 							override_ok = 0;
9302 						} else if (fwd->auth_keyid != auth_keyid) {
9303 							/*
9304 							 * different keyid,
9305 							 * so done bundling
9306 							 */
9307 							break;
9308 						}
9309 					}
9310 					m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9311 					if (m == NULL) {
9312 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9313 						return (ENOMEM);
9314 					}
9315 					/* Do clear IP_DF ? */
9316 					if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9317 						no_fragmentflg = 0;
9318 					}
9319 					/* upate our MTU size */
9320 					if (mtu > (fwd->send_size + dmtu))
9321 						mtu -= (fwd->send_size + dmtu);
9322 					else
9323 						mtu = 0;
9324 					data_list[bundle_at++] = fwd;
9325 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9326 						break;
9327 					}
9328 					fwd = TAILQ_NEXT(fwd, sctp_next);
9329 				} else {
9330 					/* can't fit so we are done */
9331 					break;
9332 				}
9333 			}
9334 		}
9335 		/* Is there something to send for this destination? */
9336 		if (m) {
9337 			/*
9338 			 * No matter if we fail/or suceed we should start a
9339 			 * timer. A failure is like a lost IP packet :-)
9340 			 */
9341 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9342 				/*
9343 				 * no timer running on this destination
9344 				 * restart it.
9345 				 */
9346 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9347 				tmr_started = 1;
9348 			}
9349 			/* Now lets send it, if there is anything to send :> */
9350 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9351 			    (struct sockaddr *)&net->ro._l_addr, m,
9352 			    auth_offset, auth, auth_keyid,
9353 			    no_fragmentflg, 0, NULL, 0,
9354 			    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9355 			    net->port, so_locked, NULL, NULL))) {
9356 				/* error, we could not output */
9357 				SCTP_STAT_INCR(sctps_lowlevelerr);
9358 				return (error);
9359 			}
9360 			m = endofchain = NULL;
9361 			auth = NULL;
9362 			auth_offset = 0;
9363 			/* For HB's */
9364 			/*
9365 			 * We don't want to mark the net->sent time here
9366 			 * since this we use this for HB and retrans cannot
9367 			 * measure RTT
9368 			 */
9369 			/* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9370 
9371 			/* For auto-close */
9372 			cnt_thru++;
9373 			if (*now_filled == 0) {
9374 				(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9375 				*now = asoc->time_last_sent;
9376 				*now_filled = 1;
9377 			} else {
9378 				asoc->time_last_sent = *now;
9379 			}
9380 			*cnt_out += bundle_at;
9381 #ifdef SCTP_AUDITING_ENABLED
9382 			sctp_audit_log(0xC4, bundle_at);
9383 #endif
9384 			if (bundle_at) {
9385 				tsns_sent = data_list[0]->rec.data.TSN_seq;
9386 			}
9387 			for (i = 0; i < bundle_at; i++) {
9388 				SCTP_STAT_INCR(sctps_sendretransdata);
9389 				data_list[i]->sent = SCTP_DATAGRAM_SENT;
9390 				/*
9391 				 * When we have a revoked data, and we
9392 				 * retransmit it, then we clear the revoked
9393 				 * flag since this flag dictates if we
9394 				 * subtracted from the fs
9395 				 */
9396 				if (data_list[i]->rec.data.chunk_was_revoked) {
9397 					/* Deflate the cwnd */
9398 					data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9399 					data_list[i]->rec.data.chunk_was_revoked = 0;
9400 				}
9401 				data_list[i]->snd_count++;
9402 				sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9403 				/* record the time */
9404 				data_list[i]->sent_rcv_time = asoc->time_last_sent;
9405 				if (data_list[i]->book_size_scale) {
9406 					/*
9407 					 * need to double the book size on
9408 					 * this one
9409 					 */
9410 					data_list[i]->book_size_scale = 0;
9411 					/*
9412 					 * Since we double the booksize, we
9413 					 * must also double the output queue
9414 					 * size, since this get shrunk when
9415 					 * we free by this amount.
9416 					 */
9417 					atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9418 					data_list[i]->book_size *= 2;
9419 
9420 
9421 				} else {
9422 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9423 						sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9424 						    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9425 					}
9426 					asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9427 					    (uint32_t) (data_list[i]->send_size +
9428 					    SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9429 				}
9430 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9431 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9432 					    data_list[i]->whoTo->flight_size,
9433 					    data_list[i]->book_size,
9434 					    (uintptr_t) data_list[i]->whoTo,
9435 					    data_list[i]->rec.data.TSN_seq);
9436 				}
9437 				sctp_flight_size_increase(data_list[i]);
9438 				sctp_total_flight_increase(stcb, data_list[i]);
9439 				if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9440 					/* SWS sender side engages */
9441 					asoc->peers_rwnd = 0;
9442 				}
9443 				if ((i == 0) &&
9444 				    (data_list[i]->rec.data.doing_fast_retransmit)) {
9445 					SCTP_STAT_INCR(sctps_sendfastretrans);
9446 					if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9447 					    (tmr_started == 0)) {
9448 						/*-
9449 						 * ok we just fast-retrans'd
9450 						 * the lowest TSN, i.e the
9451 						 * first on the list. In
9452 						 * this case we want to give
9453 						 * some more time to get a
9454 						 * SACK back without a
9455 						 * t3-expiring.
9456 						 */
9457 						sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9458 						    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9459 						sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9460 					}
9461 				}
9462 			}
9463 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9464 				sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9465 			}
9466 #ifdef SCTP_AUDITING_ENABLED
9467 			sctp_auditing(21, inp, stcb, NULL);
9468 #endif
9469 		} else {
9470 			/* None will fit */
9471 			return (1);
9472 		}
9473 		if (asoc->sent_queue_retran_cnt <= 0) {
9474 			/* all done we have no more to retran */
9475 			asoc->sent_queue_retran_cnt = 0;
9476 			break;
9477 		}
9478 		if (one_chunk) {
9479 			/* No more room in rwnd */
9480 			return (1);
9481 		}
9482 		/* stop the for loop here. we sent out a packet */
9483 		break;
9484 	}
9485 	return (0);
9486 }
9487 
9488 
9489 static int
9490 sctp_timer_validation(struct sctp_inpcb *inp,
9491     struct sctp_tcb *stcb,
9492     struct sctp_association *asoc,
9493     int ret)
9494 {
9495 	struct sctp_nets *net;
9496 
9497 	/* Validate that a timer is running somewhere */
9498 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9499 		if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9500 			/* Here is a timer */
9501 			return (ret);
9502 		}
9503 	}
9504 	SCTP_TCB_LOCK_ASSERT(stcb);
9505 	/* Gak, we did not have a timer somewhere */
9506 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9507 	sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9508 	return (ret);
9509 }
9510 
9511 void
9512 sctp_chunk_output(struct sctp_inpcb *inp,
9513     struct sctp_tcb *stcb,
9514     int from_where,
9515     int so_locked
9516 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9517     SCTP_UNUSED
9518 #endif
9519 )
9520 {
9521 	/*-
9522 	 * Ok this is the generic chunk service queue. we must do the
9523 	 * following:
9524 	 * - See if there are retransmits pending, if so we must
9525 	 *   do these first.
9526 	 * - Service the stream queue that is next, moving any
9527 	 *   message (note I must get a complete message i.e.
9528 	 *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9529 	 *   TSN's
9530 	 * - Check to see if the cwnd/rwnd allows any output, if so we
9531 	 *   go ahead and fomulate and send the low level chunks. Making sure
9532 	 *   to combine any control in the control chunk queue also.
9533 	 */
9534 	struct sctp_association *asoc;
9535 	struct sctp_nets *net;
9536 	int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0;
9537 	unsigned int burst_cnt = 0;
9538 	struct timeval now;
9539 	int now_filled = 0;
9540 	int nagle_on = 0;
9541 	int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9542 	int un_sent = 0;
9543 	int fr_done;
9544 	unsigned int tot_frs = 0;
9545 
9546 	asoc = &stcb->asoc;
9547 	if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9548 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9549 			nagle_on = 0;
9550 		} else {
9551 			nagle_on = 1;
9552 		}
9553 	}
9554 	SCTP_TCB_LOCK_ASSERT(stcb);
9555 
9556 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9557 
9558 	if ((un_sent <= 0) &&
9559 	    (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9560 	    (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
9561 	    (asoc->sent_queue_retran_cnt == 0)) {
9562 		/* Nothing to do unless there is something to be sent left */
9563 		return;
9564 	}
9565 	/*
9566 	 * Do we have something to send, data or control AND a sack timer
9567 	 * running, if so piggy-back the sack.
9568 	 */
9569 	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9570 		sctp_send_sack(stcb, so_locked);
9571 		(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9572 	}
9573 	while (asoc->sent_queue_retran_cnt) {
9574 		/*-
9575 		 * Ok, it is retransmission time only, we send out only ONE
9576 		 * packet with a single call off to the retran code.
9577 		 */
9578 		if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9579 			/*-
9580 			 * Special hook for handling cookiess discarded
9581 			 * by peer that carried data. Send cookie-ack only
9582 			 * and then the next call with get the retran's.
9583 			 */
9584 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9585 			    from_where,
9586 			    &now, &now_filled, frag_point, so_locked);
9587 			return;
9588 		} else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9589 			/* if its not from a HB then do it */
9590 			fr_done = 0;
9591 			ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9592 			if (fr_done) {
9593 				tot_frs++;
9594 			}
9595 		} else {
9596 			/*
9597 			 * its from any other place, we don't allow retran
9598 			 * output (only control)
9599 			 */
9600 			ret = 1;
9601 		}
9602 		if (ret > 0) {
9603 			/* Can't send anymore */
9604 			/*-
9605 			 * now lets push out control by calling med-level
9606 			 * output once. this assures that we WILL send HB's
9607 			 * if queued too.
9608 			 */
9609 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9610 			    from_where,
9611 			    &now, &now_filled, frag_point, so_locked);
9612 #ifdef SCTP_AUDITING_ENABLED
9613 			sctp_auditing(8, inp, stcb, NULL);
9614 #endif
9615 			(void)sctp_timer_validation(inp, stcb, asoc, ret);
9616 			return;
9617 		}
9618 		if (ret < 0) {
9619 			/*-
9620 			 * The count was off.. retran is not happening so do
9621 			 * the normal retransmission.
9622 			 */
9623 #ifdef SCTP_AUDITING_ENABLED
9624 			sctp_auditing(9, inp, stcb, NULL);
9625 #endif
9626 			if (ret == SCTP_RETRAN_EXIT) {
9627 				return;
9628 			}
9629 			break;
9630 		}
9631 		if (from_where == SCTP_OUTPUT_FROM_T3) {
9632 			/* Only one transmission allowed out of a timeout */
9633 #ifdef SCTP_AUDITING_ENABLED
9634 			sctp_auditing(10, inp, stcb, NULL);
9635 #endif
9636 			/* Push out any control */
9637 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
9638 			    &now, &now_filled, frag_point, so_locked);
9639 			return;
9640 		}
9641 		if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) {
9642 			/* Hit FR burst limit */
9643 			return;
9644 		}
9645 		if ((num_out == 0) && (ret == 0)) {
9646 			/* No more retrans to send */
9647 			break;
9648 		}
9649 	}
9650 #ifdef SCTP_AUDITING_ENABLED
9651 	sctp_auditing(12, inp, stcb, NULL);
9652 #endif
9653 	/* Check for bad destinations, if they exist move chunks around. */
9654 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9655 		if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
9656 		    SCTP_ADDR_NOT_REACHABLE) {
9657 			/*-
9658 			 * if possible move things off of this address we
9659 			 * still may send below due to the dormant state but
9660 			 * we try to find an alternate address to send to
9661 			 * and if we have one we move all queued data on the
9662 			 * out wheel to this alternate address.
9663 			 */
9664 			if (net->ref_count > 1)
9665 				sctp_move_chunks_from_net(stcb, net);
9666 		} else if ((asoc->sctp_cmt_on_off > 0) &&
9667 			    (asoc->sctp_cmt_pf > 0) &&
9668 		    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
9669 			/*
9670 			 * JRS 5/14/07 - If CMT PF is on and the current
9671 			 * destination is in PF state, move all queued data
9672 			 * to an alternate desination.
9673 			 */
9674 			if (net->ref_count > 1)
9675 				sctp_move_chunks_from_net(stcb, net);
9676 		} else {
9677 			/*-
9678 			 * if ((asoc->sat_network) || (net->addr_is_local))
9679 			 * { burst_limit = asoc->max_burst *
9680 			 * SCTP_SAT_NETWORK_BURST_INCR; }
9681 			 */
9682 			if (asoc->max_burst > 0) {
9683 				if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
9684 					if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) {
9685 						/*
9686 						 * JRS - Use the congestion
9687 						 * control given in the
9688 						 * congestion control module
9689 						 */
9690 						asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst);
9691 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9692 							sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED);
9693 						}
9694 						SCTP_STAT_INCR(sctps_maxburstqueued);
9695 					}
9696 					net->fast_retran_ip = 0;
9697 				} else {
9698 					if (net->flight_size == 0) {
9699 						/*
9700 						 * Should be decaying the
9701 						 * cwnd here
9702 						 */
9703 						;
9704 					}
9705 				}
9706 			}
9707 		}
9708 
9709 	}
9710 	burst_cnt = 0;
9711 	do {
9712 		error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
9713 		    &reason_code, 0, from_where,
9714 		    &now, &now_filled, frag_point, so_locked);
9715 		if (error) {
9716 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
9717 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9718 				sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
9719 			}
9720 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9721 				sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
9722 				sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
9723 			}
9724 			break;
9725 		}
9726 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
9727 
9728 		tot_out += num_out;
9729 		burst_cnt++;
9730 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9731 			sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
9732 			if (num_out == 0) {
9733 				sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
9734 			}
9735 		}
9736 		if (nagle_on) {
9737 			/*-
9738 			 * When nagle is on, we look at how much is un_sent, then
9739 			 * if its smaller than an MTU and we have data in
9740 			 * flight we stop.
9741 			 */
9742 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9743 			    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
9744 			if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
9745 			    (stcb->asoc.total_flight > 0)) {
9746 				break;
9747 			}
9748 		}
9749 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9750 		    TAILQ_EMPTY(&asoc->send_queue) &&
9751 		    stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
9752 			/* Nothing left to send */
9753 			break;
9754 		}
9755 		if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
9756 			/* Nothing left to send */
9757 			break;
9758 		}
9759 	} while (num_out &&
9760 	    ((asoc->max_burst == 0) ||
9761 	    SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
9762 	    (burst_cnt < asoc->max_burst)));
9763 
9764 	if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
9765 		if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) {
9766 			SCTP_STAT_INCR(sctps_maxburstqueued);
9767 			asoc->burst_limit_applied = 1;
9768 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9769 				sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
9770 			}
9771 		} else {
9772 			asoc->burst_limit_applied = 0;
9773 		}
9774 	}
9775 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9776 		sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
9777 	}
9778 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
9779 	    tot_out);
9780 
9781 	/*-
9782 	 * Now we need to clean up the control chunk chain if a ECNE is on
9783 	 * it. It must be marked as UNSENT again so next call will continue
9784 	 * to send it until such time that we get a CWR, to remove it.
9785 	 */
9786 	if (stcb->asoc.ecn_echo_cnt_onq)
9787 		sctp_fix_ecn_echo(asoc);
9788 	return;
9789 }
9790 
9791 
9792 int
9793 sctp_output(inp, m, addr, control, p, flags)
9794 	struct sctp_inpcb *inp;
9795 	struct mbuf *m;
9796 	struct sockaddr *addr;
9797 	struct mbuf *control;
9798 	struct thread *p;
9799 	int flags;
9800 {
9801 	if (inp == NULL) {
9802 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9803 		return (EINVAL);
9804 	}
9805 	if (inp->sctp_socket == NULL) {
9806 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9807 		return (EINVAL);
9808 	}
9809 	return (sctp_sosend(inp->sctp_socket,
9810 	    addr,
9811 	    (struct uio *)NULL,
9812 	    m,
9813 	    control,
9814 	    flags, p
9815 	    ));
9816 }
9817 
9818 void
9819 send_forward_tsn(struct sctp_tcb *stcb,
9820     struct sctp_association *asoc)
9821 {
9822 	struct sctp_tmit_chunk *chk;
9823 	struct sctp_forward_tsn_chunk *fwdtsn;
9824 	uint32_t advance_peer_ack_point;
9825 
9826 	SCTP_TCB_LOCK_ASSERT(stcb);
9827 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9828 		if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9829 			/* mark it to unsent */
9830 			chk->sent = SCTP_DATAGRAM_UNSENT;
9831 			chk->snd_count = 0;
9832 			/* Do we correct its output location? */
9833 			if (chk->whoTo != asoc->primary_destination) {
9834 				sctp_free_remote_addr(chk->whoTo);
9835 				chk->whoTo = asoc->primary_destination;
9836 				atomic_add_int(&chk->whoTo->ref_count, 1);
9837 			}
9838 			goto sctp_fill_in_rest;
9839 		}
9840 	}
9841 	/* Ok if we reach here we must build one */
9842 	sctp_alloc_a_chunk(stcb, chk);
9843 	if (chk == NULL) {
9844 		return;
9845 	}
9846 	asoc->fwd_tsn_cnt++;
9847 	chk->copy_by_ref = 0;
9848 	chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
9849 	chk->rec.chunk_id.can_take_data = 0;
9850 	chk->asoc = asoc;
9851 	chk->whoTo = NULL;
9852 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9853 	if (chk->data == NULL) {
9854 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
9855 		return;
9856 	}
9857 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9858 	chk->sent = SCTP_DATAGRAM_UNSENT;
9859 	chk->snd_count = 0;
9860 	chk->whoTo = asoc->primary_destination;
9861 	atomic_add_int(&chk->whoTo->ref_count, 1);
9862 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
9863 	asoc->ctrl_queue_cnt++;
9864 sctp_fill_in_rest:
9865 	/*-
9866 	 * Here we go through and fill out the part that deals with
9867 	 * stream/seq of the ones we skip.
9868 	 */
9869 	SCTP_BUF_LEN(chk->data) = 0;
9870 	{
9871 		struct sctp_tmit_chunk *at, *tp1, *last;
9872 		struct sctp_strseq *strseq;
9873 		unsigned int cnt_of_space, i, ovh;
9874 		unsigned int space_needed;
9875 		unsigned int cnt_of_skipped = 0;
9876 
9877 		TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
9878 			if (at->sent != SCTP_FORWARD_TSN_SKIP) {
9879 				/* no more to look at */
9880 				break;
9881 			}
9882 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9883 				/* We don't report these */
9884 				continue;
9885 			}
9886 			cnt_of_skipped++;
9887 		}
9888 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
9889 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
9890 
9891 		cnt_of_space = M_TRAILINGSPACE(chk->data);
9892 
9893 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9894 			ovh = SCTP_MIN_OVERHEAD;
9895 		} else {
9896 			ovh = SCTP_MIN_V4_OVERHEAD;
9897 		}
9898 		if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
9899 			/* trim to a mtu size */
9900 			cnt_of_space = asoc->smallest_mtu - ovh;
9901 		}
9902 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9903 			sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9904 			    0xff, 0, cnt_of_skipped,
9905 			    asoc->advanced_peer_ack_point);
9906 
9907 		}
9908 		advance_peer_ack_point = asoc->advanced_peer_ack_point;
9909 		if (cnt_of_space < space_needed) {
9910 			/*-
9911 			 * ok we must trim down the chunk by lowering the
9912 			 * advance peer ack point.
9913 			 */
9914 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9915 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9916 				    0xff, 0xff, cnt_of_space,
9917 				    space_needed);
9918 			}
9919 			cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
9920 			cnt_of_skipped /= sizeof(struct sctp_strseq);
9921 			/*-
9922 			 * Go through and find the TSN that will be the one
9923 			 * we report.
9924 			 */
9925 			at = TAILQ_FIRST(&asoc->sent_queue);
9926 			for (i = 0; i < cnt_of_skipped; i++) {
9927 				tp1 = TAILQ_NEXT(at, sctp_next);
9928 				if (tp1 == NULL) {
9929 					break;
9930 				}
9931 				at = tp1;
9932 			}
9933 			if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9934 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9935 				    0xff, cnt_of_skipped, at->rec.data.TSN_seq,
9936 				    asoc->advanced_peer_ack_point);
9937 			}
9938 			last = at;
9939 			/*-
9940 			 * last now points to last one I can report, update
9941 			 * peer ack point
9942 			 */
9943 			if (last)
9944 				advance_peer_ack_point = last->rec.data.TSN_seq;
9945 			space_needed = sizeof(struct sctp_forward_tsn_chunk) +
9946 			    cnt_of_skipped * sizeof(struct sctp_strseq);
9947 		}
9948 		chk->send_size = space_needed;
9949 		/* Setup the chunk */
9950 		fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
9951 		fwdtsn->ch.chunk_length = htons(chk->send_size);
9952 		fwdtsn->ch.chunk_flags = 0;
9953 		fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
9954 		fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
9955 		SCTP_BUF_LEN(chk->data) = chk->send_size;
9956 		fwdtsn++;
9957 		/*-
9958 		 * Move pointer to after the fwdtsn and transfer to the
9959 		 * strseq pointer.
9960 		 */
9961 		strseq = (struct sctp_strseq *)fwdtsn;
9962 		/*-
9963 		 * Now populate the strseq list. This is done blindly
9964 		 * without pulling out duplicate stream info. This is
9965 		 * inefficent but won't harm the process since the peer will
9966 		 * look at these in sequence and will thus release anything.
9967 		 * It could mean we exceed the PMTU and chop off some that
9968 		 * we could have included.. but this is unlikely (aka 1432/4
9969 		 * would mean 300+ stream seq's would have to be reported in
9970 		 * one FWD-TSN. With a bit of work we can later FIX this to
9971 		 * optimize and pull out duplcates.. but it does add more
9972 		 * overhead. So for now... not!
9973 		 */
9974 		at = TAILQ_FIRST(&asoc->sent_queue);
9975 		for (i = 0; i < cnt_of_skipped; i++) {
9976 			tp1 = TAILQ_NEXT(at, sctp_next);
9977 			if (tp1 == NULL)
9978 				break;
9979 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9980 				/* We don't report these */
9981 				i--;
9982 				at = tp1;
9983 				continue;
9984 			}
9985 			if (at->rec.data.TSN_seq == advance_peer_ack_point) {
9986 				at->rec.data.fwd_tsn_cnt = 0;
9987 			}
9988 			strseq->stream = ntohs(at->rec.data.stream_number);
9989 			strseq->sequence = ntohs(at->rec.data.stream_seq);
9990 			strseq++;
9991 			at = tp1;
9992 		}
9993 	}
9994 	return;
9995 
9996 }
9997 
9998 void
9999 sctp_send_sack(struct sctp_tcb *stcb, int so_locked
10000 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10001     SCTP_UNUSED
10002 #endif
10003 )
10004 {
10005 	/*-
10006 	 * Queue up a SACK or NR-SACK in the control queue.
10007 	 * We must first check to see if a SACK or NR-SACK is
10008 	 * somehow on the control queue.
10009 	 * If so, we will take and and remove the old one.
10010 	 */
10011 	struct sctp_association *asoc;
10012 	struct sctp_tmit_chunk *chk, *a_chk;
10013 	struct sctp_sack_chunk *sack;
10014 	struct sctp_nr_sack_chunk *nr_sack;
10015 	struct sctp_gap_ack_block *gap_descriptor;
10016 	struct sack_track *selector;
10017 	int mergeable = 0;
10018 	int offset;
10019 	caddr_t limit;
10020 	uint32_t *dup;
10021 	int limit_reached = 0;
10022 	unsigned int i, siz, j;
10023 	unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
10024 	int num_dups = 0;
10025 	int space_req;
10026 	uint32_t highest_tsn;
10027 	uint8_t flags;
10028 	uint8_t type;
10029 	uint8_t tsn_map;
10030 
10031 	if ((stcb->asoc.sctp_nr_sack_on_off == 1) &&
10032 	    (stcb->asoc.peer_supports_nr_sack == 1)) {
10033 		type = SCTP_NR_SELECTIVE_ACK;
10034 	} else {
10035 		type = SCTP_SELECTIVE_ACK;
10036 	}
10037 	a_chk = NULL;
10038 	asoc = &stcb->asoc;
10039 	SCTP_TCB_LOCK_ASSERT(stcb);
10040 	if (asoc->last_data_chunk_from == NULL) {
10041 		/* Hmm we never received anything */
10042 		return;
10043 	}
10044 	sctp_slide_mapping_arrays(stcb);
10045 	sctp_set_rwnd(stcb, asoc);
10046 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10047 		if (chk->rec.chunk_id.id == type) {
10048 			/* Hmm, found a sack already on queue, remove it */
10049 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
10050 			asoc->ctrl_queue_cnt--;
10051 			a_chk = chk;
10052 			if (a_chk->data) {
10053 				sctp_m_freem(a_chk->data);
10054 				a_chk->data = NULL;
10055 			}
10056 			sctp_free_remote_addr(a_chk->whoTo);
10057 			a_chk->whoTo = NULL;
10058 			break;
10059 		}
10060 	}
10061 	if (a_chk == NULL) {
10062 		sctp_alloc_a_chunk(stcb, a_chk);
10063 		if (a_chk == NULL) {
10064 			/* No memory so we drop the idea, and set a timer */
10065 			if (stcb->asoc.delayed_ack) {
10066 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10067 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
10068 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10069 				    stcb->sctp_ep, stcb, NULL);
10070 			} else {
10071 				stcb->asoc.send_sack = 1;
10072 			}
10073 			return;
10074 		}
10075 		a_chk->copy_by_ref = 0;
10076 		a_chk->rec.chunk_id.id = type;
10077 		a_chk->rec.chunk_id.can_take_data = 1;
10078 	}
10079 	/* Clear our pkt counts */
10080 	asoc->data_pkts_seen = 0;
10081 
10082 	a_chk->asoc = asoc;
10083 	a_chk->snd_count = 0;
10084 	a_chk->send_size = 0;	/* fill in later */
10085 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
10086 	a_chk->whoTo = NULL;
10087 
10088 	if ((asoc->numduptsns) ||
10089 	    (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)) {
10090 		/*-
10091 		 * Ok, we have some duplicates or the destination for the
10092 		 * sack is unreachable, lets see if we can select an
10093 		 * alternate than asoc->last_data_chunk_from
10094 		 */
10095 		if ((!(asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)) &&
10096 		    (asoc->used_alt_onsack > asoc->numnets)) {
10097 			/* We used an alt last time, don't this time */
10098 			a_chk->whoTo = NULL;
10099 		} else {
10100 			asoc->used_alt_onsack++;
10101 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
10102 		}
10103 		if (a_chk->whoTo == NULL) {
10104 			/* Nope, no alternate */
10105 			a_chk->whoTo = asoc->last_data_chunk_from;
10106 			asoc->used_alt_onsack = 0;
10107 		}
10108 	} else {
10109 		/*
10110 		 * No duplicates so we use the last place we received data
10111 		 * from.
10112 		 */
10113 		asoc->used_alt_onsack = 0;
10114 		a_chk->whoTo = asoc->last_data_chunk_from;
10115 	}
10116 	if (a_chk->whoTo) {
10117 		atomic_add_int(&a_chk->whoTo->ref_count, 1);
10118 	}
10119 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) {
10120 		highest_tsn = asoc->highest_tsn_inside_map;
10121 	} else {
10122 		highest_tsn = asoc->highest_tsn_inside_nr_map;
10123 	}
10124 	if (highest_tsn == asoc->cumulative_tsn) {
10125 		/* no gaps */
10126 		if (type == SCTP_SELECTIVE_ACK) {
10127 			space_req = sizeof(struct sctp_sack_chunk);
10128 		} else {
10129 			space_req = sizeof(struct sctp_nr_sack_chunk);
10130 		}
10131 	} else {
10132 		/* gaps get a cluster */
10133 		space_req = MCLBYTES;
10134 	}
10135 	/* Ok now lets formulate a MBUF with our sack */
10136 	a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
10137 	if ((a_chk->data == NULL) ||
10138 	    (a_chk->whoTo == NULL)) {
10139 		/* rats, no mbuf memory */
10140 		if (a_chk->data) {
10141 			/* was a problem with the destination */
10142 			sctp_m_freem(a_chk->data);
10143 			a_chk->data = NULL;
10144 		}
10145 		sctp_free_a_chunk(stcb, a_chk, so_locked);
10146 		/* sa_ignore NO_NULL_CHK */
10147 		if (stcb->asoc.delayed_ack) {
10148 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10149 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
10150 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10151 			    stcb->sctp_ep, stcb, NULL);
10152 		} else {
10153 			stcb->asoc.send_sack = 1;
10154 		}
10155 		return;
10156 	}
10157 	/* ok, lets go through and fill it in */
10158 	SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10159 	space = M_TRAILINGSPACE(a_chk->data);
10160 	if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10161 		space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10162 	}
10163 	limit = mtod(a_chk->data, caddr_t);
10164 	limit += space;
10165 
10166 	flags = 0;
10167 
10168 	if ((asoc->sctp_cmt_on_off > 0) &&
10169 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10170 		/*-
10171 		 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10172 		 * received, then set high bit to 1, else 0. Reset
10173 		 * pkts_rcvd.
10174 		 */
10175 		flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10176 		asoc->cmt_dac_pkts_rcvd = 0;
10177 	}
10178 #ifdef SCTP_ASOCLOG_OF_TSNS
10179 	stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10180 	stcb->asoc.cumack_log_atsnt++;
10181 	if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10182 		stcb->asoc.cumack_log_atsnt = 0;
10183 	}
10184 #endif
10185 	/* reset the readers interpretation */
10186 	stcb->freed_by_sorcv_sincelast = 0;
10187 
10188 	if (type == SCTP_SELECTIVE_ACK) {
10189 		sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10190 		nr_sack = NULL;
10191 		gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10192 		if (highest_tsn > asoc->mapping_array_base_tsn) {
10193 			siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10194 		} else {
10195 			siz = (((MAX_TSN - highest_tsn) + 1) + highest_tsn + 7) / 8;
10196 		}
10197 	} else {
10198 		sack = NULL;
10199 		nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10200 		gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10201 		if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
10202 			siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10203 		} else {
10204 			siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10205 		}
10206 	}
10207 
10208 	if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10209 		offset = 1;
10210 	} else {
10211 		offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10212 	}
10213 	if (((type == SCTP_SELECTIVE_ACK) &&
10214 	    SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) ||
10215 	    ((type == SCTP_NR_SELECTIVE_ACK) &&
10216 	    SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) {
10217 		/* we have a gap .. maybe */
10218 		for (i = 0; i < siz; i++) {
10219 			tsn_map = asoc->mapping_array[i];
10220 			if (type == SCTP_SELECTIVE_ACK) {
10221 				tsn_map |= asoc->nr_mapping_array[i];
10222 			}
10223 			if (i == 0) {
10224 				/*
10225 				 * Clear all bits corresponding to TSNs
10226 				 * smaller or equal to the cumulative TSN.
10227 				 */
10228 				tsn_map &= (~0 << (1 - offset));
10229 			}
10230 			selector = &sack_array[tsn_map];
10231 			if (mergeable && selector->right_edge) {
10232 				/*
10233 				 * Backup, left and right edges were ok to
10234 				 * merge.
10235 				 */
10236 				num_gap_blocks--;
10237 				gap_descriptor--;
10238 			}
10239 			if (selector->num_entries == 0)
10240 				mergeable = 0;
10241 			else {
10242 				for (j = 0; j < selector->num_entries; j++) {
10243 					if (mergeable && selector->right_edge) {
10244 						/*
10245 						 * do a merge by NOT setting
10246 						 * the left side
10247 						 */
10248 						mergeable = 0;
10249 					} else {
10250 						/*
10251 						 * no merge, set the left
10252 						 * side
10253 						 */
10254 						mergeable = 0;
10255 						gap_descriptor->start = htons((selector->gaps[j].start + offset));
10256 					}
10257 					gap_descriptor->end = htons((selector->gaps[j].end + offset));
10258 					num_gap_blocks++;
10259 					gap_descriptor++;
10260 					if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10261 						/* no more room */
10262 						limit_reached = 1;
10263 						break;
10264 					}
10265 				}
10266 				if (selector->left_edge) {
10267 					mergeable = 1;
10268 				}
10269 			}
10270 			if (limit_reached) {
10271 				/* Reached the limit stop */
10272 				break;
10273 			}
10274 			offset += 8;
10275 		}
10276 	}
10277 	if ((type == SCTP_NR_SELECTIVE_ACK) &&
10278 	    (limit_reached == 0)) {
10279 
10280 		mergeable = 0;
10281 
10282 		if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) {
10283 			siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10284 		} else {
10285 			siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10286 		}
10287 
10288 		if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10289 			offset = 1;
10290 		} else {
10291 			offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10292 		}
10293 		if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) {
10294 			/* we have a gap .. maybe */
10295 			for (i = 0; i < siz; i++) {
10296 				tsn_map = asoc->nr_mapping_array[i];
10297 				if (i == 0) {
10298 					/*
10299 					 * Clear all bits corresponding to
10300 					 * TSNs smaller or equal to the
10301 					 * cumulative TSN.
10302 					 */
10303 					tsn_map &= (~0 << (1 - offset));
10304 				}
10305 				selector = &sack_array[tsn_map];
10306 				if (mergeable && selector->right_edge) {
10307 					/*
10308 					 * Backup, left and right edges were
10309 					 * ok to merge.
10310 					 */
10311 					num_nr_gap_blocks--;
10312 					gap_descriptor--;
10313 				}
10314 				if (selector->num_entries == 0)
10315 					mergeable = 0;
10316 				else {
10317 					for (j = 0; j < selector->num_entries; j++) {
10318 						if (mergeable && selector->right_edge) {
10319 							/*
10320 							 * do a merge by NOT
10321 							 * setting the left
10322 							 * side
10323 							 */
10324 							mergeable = 0;
10325 						} else {
10326 							/*
10327 							 * no merge, set the
10328 							 * left side
10329 							 */
10330 							mergeable = 0;
10331 							gap_descriptor->start = htons((selector->gaps[j].start + offset));
10332 						}
10333 						gap_descriptor->end = htons((selector->gaps[j].end + offset));
10334 						num_nr_gap_blocks++;
10335 						gap_descriptor++;
10336 						if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10337 							/* no more room */
10338 							limit_reached = 1;
10339 							break;
10340 						}
10341 					}
10342 					if (selector->left_edge) {
10343 						mergeable = 1;
10344 					}
10345 				}
10346 				if (limit_reached) {
10347 					/* Reached the limit stop */
10348 					break;
10349 				}
10350 				offset += 8;
10351 			}
10352 		}
10353 	}
10354 	/* now we must add any dups we are going to report. */
10355 	if ((limit_reached == 0) && (asoc->numduptsns)) {
10356 		dup = (uint32_t *) gap_descriptor;
10357 		for (i = 0; i < asoc->numduptsns; i++) {
10358 			*dup = htonl(asoc->dup_tsns[i]);
10359 			dup++;
10360 			num_dups++;
10361 			if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10362 				/* no more room */
10363 				break;
10364 			}
10365 		}
10366 		asoc->numduptsns = 0;
10367 	}
10368 	/*
10369 	 * now that the chunk is prepared queue it to the control chunk
10370 	 * queue.
10371 	 */
10372 	if (type == SCTP_SELECTIVE_ACK) {
10373 		a_chk->send_size = sizeof(struct sctp_sack_chunk) +
10374 		    (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10375 		    num_dups * sizeof(int32_t);
10376 		SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10377 		sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10378 		sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10379 		sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10380 		sack->sack.num_dup_tsns = htons(num_dups);
10381 		sack->ch.chunk_type = type;
10382 		sack->ch.chunk_flags = flags;
10383 		sack->ch.chunk_length = htons(a_chk->send_size);
10384 	} else {
10385 		a_chk->send_size = sizeof(struct sctp_nr_sack_chunk) +
10386 		    (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10387 		    num_dups * sizeof(int32_t);
10388 		SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10389 		nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10390 		nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10391 		nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10392 		nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10393 		nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10394 		nr_sack->nr_sack.reserved = 0;
10395 		nr_sack->ch.chunk_type = type;
10396 		nr_sack->ch.chunk_flags = flags;
10397 		nr_sack->ch.chunk_length = htons(a_chk->send_size);
10398 	}
10399 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10400 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
10401 	asoc->ctrl_queue_cnt++;
10402 	asoc->send_sack = 0;
10403 	SCTP_STAT_INCR(sctps_sendsacks);
10404 	return;
10405 }
10406 
10407 void
10408 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10409 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10410     SCTP_UNUSED
10411 #endif
10412 )
10413 {
10414 	struct mbuf *m_abort;
10415 	struct mbuf *m_out = NULL, *m_end = NULL;
10416 	struct sctp_abort_chunk *abort = NULL;
10417 	int sz;
10418 	uint32_t auth_offset = 0;
10419 	struct sctp_auth_chunk *auth = NULL;
10420 
10421 	/*-
10422 	 * Add an AUTH chunk, if chunk requires it and save the offset into
10423 	 * the chain for AUTH
10424 	 */
10425 	if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10426 	    stcb->asoc.peer_auth_chunks)) {
10427 		m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10428 		    stcb, SCTP_ABORT_ASSOCIATION);
10429 		SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10430 	}
10431 	SCTP_TCB_LOCK_ASSERT(stcb);
10432 	m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10433 	if (m_abort == NULL) {
10434 		/* no mbuf's */
10435 		if (m_out)
10436 			sctp_m_freem(m_out);
10437 		return;
10438 	}
10439 	/* link in any error */
10440 	SCTP_BUF_NEXT(m_abort) = operr;
10441 	sz = 0;
10442 	if (operr) {
10443 		struct mbuf *n;
10444 
10445 		n = operr;
10446 		while (n) {
10447 			sz += SCTP_BUF_LEN(n);
10448 			n = SCTP_BUF_NEXT(n);
10449 		}
10450 	}
10451 	SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10452 	if (m_out == NULL) {
10453 		/* NO Auth chunk prepended, so reserve space in front */
10454 		SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10455 		m_out = m_abort;
10456 	} else {
10457 		/* Put AUTH chunk at the front of the chain */
10458 		SCTP_BUF_NEXT(m_end) = m_abort;
10459 	}
10460 
10461 	/* fill in the ABORT chunk */
10462 	abort = mtod(m_abort, struct sctp_abort_chunk *);
10463 	abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10464 	abort->ch.chunk_flags = 0;
10465 	abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10466 
10467 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
10468 	    stcb->asoc.primary_destination,
10469 	    (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
10470 	    m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, NULL, 0,
10471 	    stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10472 	    stcb->asoc.primary_destination->port, so_locked, NULL, NULL);
10473 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10474 }
10475 
10476 void
10477 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10478     struct sctp_nets *net,
10479     int reflect_vtag)
10480 {
10481 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10482 	struct mbuf *m_shutdown_comp;
10483 	struct sctp_shutdown_complete_chunk *shutdown_complete;
10484 	uint32_t vtag;
10485 	uint8_t flags;
10486 
10487 	m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10488 	if (m_shutdown_comp == NULL) {
10489 		/* no mbuf's */
10490 		return;
10491 	}
10492 	if (reflect_vtag) {
10493 		flags = SCTP_HAD_NO_TCB;
10494 		vtag = stcb->asoc.my_vtag;
10495 	} else {
10496 		flags = 0;
10497 		vtag = stcb->asoc.peer_vtag;
10498 	}
10499 	shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10500 	shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10501 	shutdown_complete->ch.chunk_flags = flags;
10502 	shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10503 	SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10504 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10505 	    (struct sockaddr *)&net->ro._l_addr,
10506 	    m_shutdown_comp, 0, NULL, 0, 1, 0, NULL, 0,
10507 	    stcb->sctp_ep->sctp_lport, stcb->rport,
10508 	    htonl(vtag),
10509 	    net->port, SCTP_SO_NOT_LOCKED, NULL, NULL);
10510 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10511 	return;
10512 }
10513 
10514 void
10515 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
10516     uint32_t vrf_id, uint16_t port)
10517 {
10518 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10519 	struct mbuf *o_pak;
10520 	struct mbuf *mout;
10521 	struct ip *iph;
10522 	struct udphdr *udp = NULL;
10523 	int offset_out, len, mlen;
10524 	struct sctp_shutdown_complete_msg *comp_cp;
10525 
10526 #ifdef INET
10527 	struct ip *iph_out;
10528 
10529 #endif
10530 #ifdef INET6
10531 	struct ip6_hdr *ip6, *ip6_out;
10532 
10533 #endif
10534 
10535 	iph = mtod(m, struct ip *);
10536 	switch (iph->ip_v) {
10537 #ifdef INET
10538 	case IPVERSION:
10539 		len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10540 		break;
10541 #endif
10542 #ifdef INET6
10543 	case IPV6_VERSION >> 4:
10544 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10545 		break;
10546 #endif
10547 	default:
10548 		return;
10549 	}
10550 	if (port) {
10551 		len += sizeof(struct udphdr);
10552 	}
10553 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
10554 	if (mout == NULL) {
10555 		return;
10556 	}
10557 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
10558 	SCTP_BUF_LEN(mout) = len;
10559 	SCTP_BUF_NEXT(mout) = NULL;
10560 	if (m->m_flags & M_FLOWID) {
10561 		mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
10562 		mout->m_flags |= M_FLOWID;
10563 	}
10564 #ifdef INET
10565 	iph_out = NULL;
10566 #endif
10567 #ifdef INET6
10568 	ip6_out = NULL;
10569 #endif
10570 	offset_out = 0;
10571 
10572 	switch (iph->ip_v) {
10573 #ifdef INET
10574 	case IPVERSION:
10575 		iph_out = mtod(mout, struct ip *);
10576 
10577 		/* Fill in the IP header for the ABORT */
10578 		iph_out->ip_v = IPVERSION;
10579 		iph_out->ip_hl = (sizeof(struct ip) / 4);
10580 		iph_out->ip_tos = (u_char)0;
10581 		iph_out->ip_id = 0;
10582 		iph_out->ip_off = 0;
10583 		iph_out->ip_ttl = MAXTTL;
10584 		if (port) {
10585 			iph_out->ip_p = IPPROTO_UDP;
10586 		} else {
10587 			iph_out->ip_p = IPPROTO_SCTP;
10588 		}
10589 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10590 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10591 
10592 		/* let IP layer calculate this */
10593 		iph_out->ip_sum = 0;
10594 		offset_out += sizeof(*iph_out);
10595 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10596 		    (caddr_t)iph_out + offset_out);
10597 		break;
10598 #endif
10599 #ifdef INET6
10600 	case IPV6_VERSION >> 4:
10601 		ip6 = (struct ip6_hdr *)iph;
10602 		ip6_out = mtod(mout, struct ip6_hdr *);
10603 
10604 		/* Fill in the IPv6 header for the ABORT */
10605 		ip6_out->ip6_flow = ip6->ip6_flow;
10606 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
10607 		if (port) {
10608 			ip6_out->ip6_nxt = IPPROTO_UDP;
10609 		} else {
10610 			ip6_out->ip6_nxt = IPPROTO_SCTP;
10611 		}
10612 		ip6_out->ip6_src = ip6->ip6_dst;
10613 		ip6_out->ip6_dst = ip6->ip6_src;
10614 		/*
10615 		 * ?? The old code had both the iph len + payload, I think
10616 		 * this is wrong and would never have worked
10617 		 */
10618 		ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10619 		offset_out += sizeof(*ip6_out);
10620 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10621 		    (caddr_t)ip6_out + offset_out);
10622 		break;
10623 #endif				/* INET6 */
10624 	default:
10625 		/* Currently not supported. */
10626 		sctp_m_freem(mout);
10627 		return;
10628 	}
10629 	if (port) {
10630 		udp = (struct udphdr *)comp_cp;
10631 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10632 		udp->uh_dport = port;
10633 		udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
10634 #ifdef INET
10635 		if (iph_out)
10636 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
10637 #endif
10638 		offset_out += sizeof(struct udphdr);
10639 		comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
10640 	}
10641 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10642 		/* no mbuf's */
10643 		sctp_m_freem(mout);
10644 		return;
10645 	}
10646 	/* Now copy in and fill in the ABORT tags etc. */
10647 	comp_cp->sh.src_port = sh->dest_port;
10648 	comp_cp->sh.dest_port = sh->src_port;
10649 	comp_cp->sh.checksum = 0;
10650 	comp_cp->sh.v_tag = sh->v_tag;
10651 	comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
10652 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10653 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10654 
10655 #ifdef INET
10656 	if (iph_out != NULL) {
10657 		sctp_route_t ro;
10658 		int ret;
10659 
10660 		mlen = SCTP_BUF_LEN(mout);
10661 		bzero(&ro, sizeof ro);
10662 		/* set IPv4 length */
10663 		iph_out->ip_len = mlen;
10664 #ifdef  SCTP_PACKET_LOGGING
10665 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10666 			sctp_packet_log(mout, mlen);
10667 #endif
10668 		if (port) {
10669 #if defined(SCTP_WITH_NO_CSUM)
10670 			SCTP_STAT_INCR(sctps_sendnocrc);
10671 #else
10672 			comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
10673 			SCTP_STAT_INCR(sctps_sendswcrc);
10674 #endif
10675 			SCTP_ENABLE_UDP_CSUM(mout);
10676 		} else {
10677 #if defined(SCTP_WITH_NO_CSUM)
10678 			SCTP_STAT_INCR(sctps_sendnocrc);
10679 #else
10680 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
10681 			mout->m_pkthdr.csum_data = 0;
10682 			SCTP_STAT_INCR(sctps_sendhwcrc);
10683 #endif
10684 		}
10685 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10686 		/* out it goes */
10687 		SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
10688 
10689 		/* Free the route if we got one back */
10690 		if (ro.ro_rt)
10691 			RTFREE(ro.ro_rt);
10692 	}
10693 #endif
10694 #ifdef INET6
10695 	if (ip6_out != NULL) {
10696 		struct route_in6 ro;
10697 		int ret;
10698 		struct ifnet *ifp = NULL;
10699 
10700 		bzero(&ro, sizeof(ro));
10701 		mlen = SCTP_BUF_LEN(mout);
10702 #ifdef  SCTP_PACKET_LOGGING
10703 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10704 			sctp_packet_log(mout, mlen);
10705 #endif
10706 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10707 		if (port) {
10708 #if defined(SCTP_WITH_NO_CSUM)
10709 			SCTP_STAT_INCR(sctps_sendnocrc);
10710 #else
10711 			comp_cp->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
10712 			SCTP_STAT_INCR(sctps_sendswcrc);
10713 #endif
10714 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), mlen - sizeof(struct ip6_hdr))) == 0) {
10715 				udp->uh_sum = 0xffff;
10716 			}
10717 		} else {
10718 #if defined(SCTP_WITH_NO_CSUM)
10719 			SCTP_STAT_INCR(sctps_sendnocrc);
10720 #else
10721 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
10722 			mout->m_pkthdr.csum_data = 0;
10723 			SCTP_STAT_INCR(sctps_sendhwcrc);
10724 #endif
10725 		}
10726 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
10727 
10728 		/* Free the route if we got one back */
10729 		if (ro.ro_rt)
10730 			RTFREE(ro.ro_rt);
10731 	}
10732 #endif
10733 	SCTP_STAT_INCR(sctps_sendpackets);
10734 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10735 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10736 	return;
10737 
10738 }
10739 
10740 static struct sctp_nets *
10741 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
10742 {
10743 	struct sctp_nets *net, *hnet;
10744 	int ms_goneby, highest_ms, state_overide = 0;
10745 
10746 	(void)SCTP_GETTIME_TIMEVAL(now);
10747 	highest_ms = 0;
10748 	hnet = NULL;
10749 	SCTP_TCB_LOCK_ASSERT(stcb);
10750 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
10751 		if (
10752 		    ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
10753 		    (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
10754 		    ) {
10755 			/*
10756 			 * Skip this guy from consideration if HB is off AND
10757 			 * its confirmed
10758 			 */
10759 			continue;
10760 		}
10761 		if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
10762 			/* skip this dest net from consideration */
10763 			continue;
10764 		}
10765 		if (net->last_sent_time.tv_sec) {
10766 			/* Sent to so we subtract */
10767 			ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
10768 		} else
10769 			/* Never been sent to */
10770 			ms_goneby = 0x7fffffff;
10771 		/*-
10772 		 * When the address state is unconfirmed but still
10773 		 * considered reachable, we HB at a higher rate. Once it
10774 		 * goes confirmed OR reaches the "unreachable" state, thenw
10775 		 * we cut it back to HB at a more normal pace.
10776 		 */
10777 		if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
10778 			state_overide = 1;
10779 		} else {
10780 			state_overide = 0;
10781 		}
10782 
10783 		if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
10784 		    (ms_goneby > highest_ms)) {
10785 			highest_ms = ms_goneby;
10786 			hnet = net;
10787 		}
10788 	}
10789 	if (hnet &&
10790 	    ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
10791 		state_overide = 1;
10792 	} else {
10793 		state_overide = 0;
10794 	}
10795 
10796 	if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
10797 		/*-
10798 		 * Found the one with longest delay bounds OR it is
10799 		 * unconfirmed and still not marked unreachable.
10800 		 */
10801 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
10802 #ifdef SCTP_DEBUG
10803 		if (hnet) {
10804 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
10805 			    (struct sockaddr *)&hnet->ro._l_addr);
10806 		} else {
10807 			SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
10808 		}
10809 #endif
10810 		/* update the timer now */
10811 		hnet->last_sent_time = *now;
10812 		return (hnet);
10813 	}
10814 	/* Nothing to HB */
10815 	return (NULL);
10816 }
10817 
10818 int
10819 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net, int so_locked
10820 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10821     SCTP_UNUSED
10822 #endif
10823 )
10824 {
10825 	struct sctp_tmit_chunk *chk;
10826 	struct sctp_nets *net;
10827 	struct sctp_heartbeat_chunk *hb;
10828 	struct timeval now;
10829 
10830 	SCTP_TCB_LOCK_ASSERT(stcb);
10831 	if (user_req == 0) {
10832 		net = sctp_select_hb_destination(stcb, &now);
10833 		if (net == NULL) {
10834 			/*-
10835 			 * All our busy none to send to, just start the
10836 			 * timer again.
10837 			 */
10838 			if (stcb->asoc.state == 0) {
10839 				return (0);
10840 			}
10841 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
10842 			    stcb->sctp_ep,
10843 			    stcb,
10844 			    net);
10845 			return (0);
10846 		}
10847 	} else {
10848 		net = u_net;
10849 		if (net == NULL) {
10850 			return (0);
10851 		}
10852 		(void)SCTP_GETTIME_TIMEVAL(&now);
10853 	}
10854 	switch (net->ro._l_addr.sa.sa_family) {
10855 #ifdef INET
10856 	case AF_INET:
10857 		break;
10858 #endif
10859 #ifdef INET6
10860 	case AF_INET6:
10861 		break;
10862 #endif
10863 	default:
10864 		return (0);
10865 	}
10866 	sctp_alloc_a_chunk(stcb, chk);
10867 	if (chk == NULL) {
10868 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
10869 		return (0);
10870 	}
10871 	chk->copy_by_ref = 0;
10872 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
10873 	chk->rec.chunk_id.can_take_data = 1;
10874 	chk->asoc = &stcb->asoc;
10875 	chk->send_size = sizeof(struct sctp_heartbeat_chunk);
10876 
10877 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10878 	if (chk->data == NULL) {
10879 		sctp_free_a_chunk(stcb, chk, so_locked);
10880 		return (0);
10881 	}
10882 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10883 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10884 	chk->sent = SCTP_DATAGRAM_UNSENT;
10885 	chk->snd_count = 0;
10886 	chk->whoTo = net;
10887 	atomic_add_int(&chk->whoTo->ref_count, 1);
10888 	/* Now we have a mbuf that we can fill in with the details */
10889 	hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
10890 	memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
10891 	/* fill out chunk header */
10892 	hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
10893 	hb->ch.chunk_flags = 0;
10894 	hb->ch.chunk_length = htons(chk->send_size);
10895 	/* Fill out hb parameter */
10896 	hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
10897 	hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
10898 	hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
10899 	hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
10900 	/* Did our user request this one, put it in */
10901 	hb->heartbeat.hb_info.user_req = user_req;
10902 	hb->heartbeat.hb_info.addr_family = net->ro._l_addr.sa.sa_family;
10903 	hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len;
10904 	if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
10905 		/*
10906 		 * we only take from the entropy pool if the address is not
10907 		 * confirmed.
10908 		 */
10909 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10910 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10911 	} else {
10912 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
10913 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
10914 	}
10915 	switch (net->ro._l_addr.sa.sa_family) {
10916 #ifdef INET
10917 	case AF_INET:
10918 		memcpy(hb->heartbeat.hb_info.address,
10919 		    &net->ro._l_addr.sin.sin_addr,
10920 		    sizeof(net->ro._l_addr.sin.sin_addr));
10921 		break;
10922 #endif
10923 #ifdef INET6
10924 	case AF_INET6:
10925 		memcpy(hb->heartbeat.hb_info.address,
10926 		    &net->ro._l_addr.sin6.sin6_addr,
10927 		    sizeof(net->ro._l_addr.sin6.sin6_addr));
10928 		break;
10929 #endif
10930 	default:
10931 		return (0);
10932 		break;
10933 	}
10934 
10935 	/*
10936 	 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
10937 	 * PF-heartbeats.  Because of this, threshold management is done by
10938 	 * the t3 timer handler, and does not need to be done upon the send
10939 	 * of a PF-heartbeat. If CMT PF is on and the destination to which a
10940 	 * heartbeat is being sent is in PF state, do NOT do threshold
10941 	 * management.
10942 	 */
10943 	if ((stcb->asoc.sctp_cmt_pf == 0) ||
10944 	    ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
10945 		/* ok we have a destination that needs a beat */
10946 		/* lets do the theshold management Qiaobing style */
10947 		if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
10948 		    stcb->asoc.max_send_times)) {
10949 			/*-
10950 			 * we have lost the association, in a way this is
10951 			 * quite bad since we really are one less time since
10952 			 * we really did not send yet. This is the down side
10953 			 * to the Q's style as defined in the RFC and not my
10954 			 * alternate style defined in the RFC.
10955 			 */
10956 			if (chk->data != NULL) {
10957 				sctp_m_freem(chk->data);
10958 				chk->data = NULL;
10959 			}
10960 			/*
10961 			 * Here we do NOT use the macro since the
10962 			 * association is now gone.
10963 			 */
10964 			if (chk->whoTo) {
10965 				sctp_free_remote_addr(chk->whoTo);
10966 				chk->whoTo = NULL;
10967 			}
10968 			sctp_free_a_chunk((struct sctp_tcb *)NULL, chk, so_locked);
10969 			return (-1);
10970 		}
10971 	}
10972 	net->hb_responded = 0;
10973 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10974 	stcb->asoc.ctrl_queue_cnt++;
10975 	SCTP_STAT_INCR(sctps_sendheartbeat);
10976 	/*-
10977 	 * Call directly med level routine to put out the chunk. It will
10978 	 * always tumble out control chunks aka HB but it may even tumble
10979 	 * out data too.
10980 	 */
10981 	return (1);
10982 }
10983 
10984 void
10985 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
10986     uint32_t high_tsn)
10987 {
10988 	struct sctp_association *asoc;
10989 	struct sctp_ecne_chunk *ecne;
10990 	struct sctp_tmit_chunk *chk;
10991 
10992 	asoc = &stcb->asoc;
10993 	SCTP_TCB_LOCK_ASSERT(stcb);
10994 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10995 		if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) {
10996 			/* found a previous ECN_ECHO update it if needed */
10997 			uint32_t cnt, ctsn;
10998 
10999 			ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11000 			ctsn = ntohl(ecne->tsn);
11001 			if (SCTP_TSN_GT(high_tsn, ctsn)) {
11002 				ecne->tsn = htonl(high_tsn);
11003 				SCTP_STAT_INCR(sctps_queue_upd_ecne);
11004 			}
11005 			cnt = ntohl(ecne->num_pkts_since_cwr);
11006 			cnt++;
11007 			ecne->num_pkts_since_cwr = htonl(cnt);
11008 			return;
11009 		}
11010 	}
11011 	/* nope could not find one to update so we must build one */
11012 	sctp_alloc_a_chunk(stcb, chk);
11013 	if (chk == NULL) {
11014 		return;
11015 	}
11016 	chk->copy_by_ref = 0;
11017 	SCTP_STAT_INCR(sctps_queue_upd_ecne);
11018 	chk->rec.chunk_id.id = SCTP_ECN_ECHO;
11019 	chk->rec.chunk_id.can_take_data = 0;
11020 	chk->asoc = &stcb->asoc;
11021 	chk->send_size = sizeof(struct sctp_ecne_chunk);
11022 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11023 	if (chk->data == NULL) {
11024 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11025 		return;
11026 	}
11027 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11028 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11029 	chk->sent = SCTP_DATAGRAM_UNSENT;
11030 	chk->snd_count = 0;
11031 	chk->whoTo = net;
11032 	atomic_add_int(&chk->whoTo->ref_count, 1);
11033 	stcb->asoc.ecn_echo_cnt_onq++;
11034 	ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11035 	ecne->ch.chunk_type = SCTP_ECN_ECHO;
11036 	ecne->ch.chunk_flags = 0;
11037 	ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
11038 	ecne->tsn = htonl(high_tsn);
11039 	ecne->num_pkts_since_cwr = htonl(1);
11040 	TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next);
11041 	asoc->ctrl_queue_cnt++;
11042 }
11043 
11044 void
11045 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
11046     struct mbuf *m, int iphlen, int bad_crc)
11047 {
11048 	struct sctp_association *asoc;
11049 	struct sctp_pktdrop_chunk *drp;
11050 	struct sctp_tmit_chunk *chk;
11051 	uint8_t *datap;
11052 	int len;
11053 	int was_trunc = 0;
11054 	struct ip *iph;
11055 
11056 #ifdef INET6
11057 	struct ip6_hdr *ip6h;
11058 
11059 #endif
11060 	int fullsz = 0, extra = 0;
11061 	long spc;
11062 	int offset;
11063 	struct sctp_chunkhdr *ch, chunk_buf;
11064 	unsigned int chk_length;
11065 
11066 	if (!stcb) {
11067 		return;
11068 	}
11069 	asoc = &stcb->asoc;
11070 	SCTP_TCB_LOCK_ASSERT(stcb);
11071 	if (asoc->peer_supports_pktdrop == 0) {
11072 		/*-
11073 		 * peer must declare support before I send one.
11074 		 */
11075 		return;
11076 	}
11077 	if (stcb->sctp_socket == NULL) {
11078 		return;
11079 	}
11080 	sctp_alloc_a_chunk(stcb, chk);
11081 	if (chk == NULL) {
11082 		return;
11083 	}
11084 	chk->copy_by_ref = 0;
11085 	iph = mtod(m, struct ip *);
11086 	if (iph == NULL) {
11087 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11088 		return;
11089 	}
11090 	switch (iph->ip_v) {
11091 #ifdef INET
11092 	case IPVERSION:
11093 		/* IPv4 */
11094 		len = chk->send_size = iph->ip_len;
11095 		break;
11096 #endif
11097 #ifdef INET6
11098 	case IPV6_VERSION >> 4:
11099 		/* IPv6 */
11100 		ip6h = mtod(m, struct ip6_hdr *);
11101 		len = chk->send_size = htons(ip6h->ip6_plen);
11102 		break;
11103 #endif
11104 	default:
11105 		return;
11106 	}
11107 	/* Validate that we do not have an ABORT in here. */
11108 	offset = iphlen + sizeof(struct sctphdr);
11109 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11110 	    sizeof(*ch), (uint8_t *) & chunk_buf);
11111 	while (ch != NULL) {
11112 		chk_length = ntohs(ch->chunk_length);
11113 		if (chk_length < sizeof(*ch)) {
11114 			/* break to abort land */
11115 			break;
11116 		}
11117 		switch (ch->chunk_type) {
11118 		case SCTP_PACKET_DROPPED:
11119 		case SCTP_ABORT_ASSOCIATION:
11120 		case SCTP_INITIATION_ACK:
11121 			/**
11122 			 * We don't respond with an PKT-DROP to an ABORT
11123 			 * or PKT-DROP. We also do not respond to an
11124 			 * INIT-ACK, because we can't know if the initiation
11125 			 * tag is correct or not.
11126 			 */
11127 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11128 			return;
11129 		default:
11130 			break;
11131 		}
11132 		offset += SCTP_SIZE32(chk_length);
11133 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11134 		    sizeof(*ch), (uint8_t *) & chunk_buf);
11135 	}
11136 
11137 	if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11138 	    min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11139 		/*
11140 		 * only send 1 mtu worth, trim off the excess on the end.
11141 		 */
11142 		fullsz = len - extra;
11143 		len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11144 		was_trunc = 1;
11145 	}
11146 	chk->asoc = &stcb->asoc;
11147 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11148 	if (chk->data == NULL) {
11149 jump_out:
11150 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11151 		return;
11152 	}
11153 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11154 	drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11155 	if (drp == NULL) {
11156 		sctp_m_freem(chk->data);
11157 		chk->data = NULL;
11158 		goto jump_out;
11159 	}
11160 	chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11161 	    sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11162 	chk->book_size_scale = 0;
11163 	if (was_trunc) {
11164 		drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11165 		drp->trunc_len = htons(fullsz);
11166 		/*
11167 		 * Len is already adjusted to size minus overhead above take
11168 		 * out the pkt_drop chunk itself from it.
11169 		 */
11170 		chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
11171 		len = chk->send_size;
11172 	} else {
11173 		/* no truncation needed */
11174 		drp->ch.chunk_flags = 0;
11175 		drp->trunc_len = htons(0);
11176 	}
11177 	if (bad_crc) {
11178 		drp->ch.chunk_flags |= SCTP_BADCRC;
11179 	}
11180 	chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11181 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11182 	chk->sent = SCTP_DATAGRAM_UNSENT;
11183 	chk->snd_count = 0;
11184 	if (net) {
11185 		/* we should hit here */
11186 		chk->whoTo = net;
11187 	} else {
11188 		chk->whoTo = asoc->primary_destination;
11189 	}
11190 	atomic_add_int(&chk->whoTo->ref_count, 1);
11191 	chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11192 	chk->rec.chunk_id.can_take_data = 1;
11193 	drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11194 	drp->ch.chunk_length = htons(chk->send_size);
11195 	spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11196 	if (spc < 0) {
11197 		spc = 0;
11198 	}
11199 	drp->bottle_bw = htonl(spc);
11200 	if (asoc->my_rwnd) {
11201 		drp->current_onq = htonl(asoc->size_on_reasm_queue +
11202 		    asoc->size_on_all_streams +
11203 		    asoc->my_rwnd_control_len +
11204 		    stcb->sctp_socket->so_rcv.sb_cc);
11205 	} else {
11206 		/*-
11207 		 * If my rwnd is 0, possibly from mbuf depletion as well as
11208 		 * space used, tell the peer there is NO space aka onq == bw
11209 		 */
11210 		drp->current_onq = htonl(spc);
11211 	}
11212 	drp->reserved = 0;
11213 	datap = drp->data;
11214 	m_copydata(m, iphlen, len, (caddr_t)datap);
11215 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11216 	asoc->ctrl_queue_cnt++;
11217 }
11218 
11219 void
11220 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override)
11221 {
11222 	struct sctp_association *asoc;
11223 	struct sctp_cwr_chunk *cwr;
11224 	struct sctp_tmit_chunk *chk;
11225 
11226 	asoc = &stcb->asoc;
11227 	SCTP_TCB_LOCK_ASSERT(stcb);
11228 
11229 
11230 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11231 		if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) {
11232 			/*
11233 			 * found a previous CWR queued to same destination
11234 			 * update it if needed
11235 			 */
11236 			uint32_t ctsn;
11237 
11238 			cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11239 			ctsn = ntohl(cwr->tsn);
11240 			if (SCTP_TSN_GT(high_tsn, ctsn)) {
11241 				cwr->tsn = htonl(high_tsn);
11242 			}
11243 			if (override & SCTP_CWR_REDUCE_OVERRIDE) {
11244 				/* Make sure override is carried */
11245 				cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE;
11246 			}
11247 			return;
11248 		}
11249 	}
11250 	sctp_alloc_a_chunk(stcb, chk);
11251 	if (chk == NULL) {
11252 		return;
11253 	}
11254 	chk->copy_by_ref = 0;
11255 	chk->rec.chunk_id.id = SCTP_ECN_CWR;
11256 	chk->rec.chunk_id.can_take_data = 1;
11257 	chk->asoc = &stcb->asoc;
11258 	chk->send_size = sizeof(struct sctp_cwr_chunk);
11259 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11260 	if (chk->data == NULL) {
11261 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11262 		return;
11263 	}
11264 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11265 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11266 	chk->sent = SCTP_DATAGRAM_UNSENT;
11267 	chk->snd_count = 0;
11268 	chk->whoTo = net;
11269 	atomic_add_int(&chk->whoTo->ref_count, 1);
11270 	cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11271 	cwr->ch.chunk_type = SCTP_ECN_CWR;
11272 	cwr->ch.chunk_flags = override;
11273 	cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11274 	cwr->tsn = htonl(high_tsn);
11275 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11276 	asoc->ctrl_queue_cnt++;
11277 }
11278 
11279 void
11280 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
11281     int number_entries, uint16_t * list,
11282     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11283 {
11284 	int len, old_len, i;
11285 	struct sctp_stream_reset_out_request *req_out;
11286 	struct sctp_chunkhdr *ch;
11287 
11288 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11289 
11290 
11291 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11292 
11293 	/* get to new offset for the param. */
11294 	req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11295 	/* now how long will this param be? */
11296 	len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11297 	req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11298 	req_out->ph.param_length = htons(len);
11299 	req_out->request_seq = htonl(seq);
11300 	req_out->response_seq = htonl(resp_seq);
11301 	req_out->send_reset_at_tsn = htonl(last_sent);
11302 	if (number_entries) {
11303 		for (i = 0; i < number_entries; i++) {
11304 			req_out->list_of_streams[i] = htons(list[i]);
11305 		}
11306 	}
11307 	if (SCTP_SIZE32(len) > len) {
11308 		/*-
11309 		 * Need to worry about the pad we may end up adding to the
11310 		 * end. This is easy since the struct is either aligned to 4
11311 		 * bytes or 2 bytes off.
11312 		 */
11313 		req_out->list_of_streams[number_entries] = 0;
11314 	}
11315 	/* now fix the chunk length */
11316 	ch->chunk_length = htons(len + old_len);
11317 	chk->book_size = len + old_len;
11318 	chk->book_size_scale = 0;
11319 	chk->send_size = SCTP_SIZE32(chk->book_size);
11320 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11321 	return;
11322 }
11323 
11324 
11325 void
11326 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11327     int number_entries, uint16_t * list,
11328     uint32_t seq)
11329 {
11330 	int len, old_len, i;
11331 	struct sctp_stream_reset_in_request *req_in;
11332 	struct sctp_chunkhdr *ch;
11333 
11334 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11335 
11336 
11337 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11338 
11339 	/* get to new offset for the param. */
11340 	req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11341 	/* now how long will this param be? */
11342 	len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11343 	req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11344 	req_in->ph.param_length = htons(len);
11345 	req_in->request_seq = htonl(seq);
11346 	if (number_entries) {
11347 		for (i = 0; i < number_entries; i++) {
11348 			req_in->list_of_streams[i] = htons(list[i]);
11349 		}
11350 	}
11351 	if (SCTP_SIZE32(len) > len) {
11352 		/*-
11353 		 * Need to worry about the pad we may end up adding to the
11354 		 * end. This is easy since the struct is either aligned to 4
11355 		 * bytes or 2 bytes off.
11356 		 */
11357 		req_in->list_of_streams[number_entries] = 0;
11358 	}
11359 	/* now fix the chunk length */
11360 	ch->chunk_length = htons(len + old_len);
11361 	chk->book_size = len + old_len;
11362 	chk->book_size_scale = 0;
11363 	chk->send_size = SCTP_SIZE32(chk->book_size);
11364 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11365 	return;
11366 }
11367 
11368 
11369 void
11370 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11371     uint32_t seq)
11372 {
11373 	int len, old_len;
11374 	struct sctp_stream_reset_tsn_request *req_tsn;
11375 	struct sctp_chunkhdr *ch;
11376 
11377 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11378 
11379 
11380 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11381 
11382 	/* get to new offset for the param. */
11383 	req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11384 	/* now how long will this param be? */
11385 	len = sizeof(struct sctp_stream_reset_tsn_request);
11386 	req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11387 	req_tsn->ph.param_length = htons(len);
11388 	req_tsn->request_seq = htonl(seq);
11389 
11390 	/* now fix the chunk length */
11391 	ch->chunk_length = htons(len + old_len);
11392 	chk->send_size = len + old_len;
11393 	chk->book_size = SCTP_SIZE32(chk->send_size);
11394 	chk->book_size_scale = 0;
11395 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11396 	return;
11397 }
11398 
11399 void
11400 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11401     uint32_t resp_seq, uint32_t result)
11402 {
11403 	int len, old_len;
11404 	struct sctp_stream_reset_response *resp;
11405 	struct sctp_chunkhdr *ch;
11406 
11407 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11408 
11409 
11410 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11411 
11412 	/* get to new offset for the param. */
11413 	resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11414 	/* now how long will this param be? */
11415 	len = sizeof(struct sctp_stream_reset_response);
11416 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11417 	resp->ph.param_length = htons(len);
11418 	resp->response_seq = htonl(resp_seq);
11419 	resp->result = ntohl(result);
11420 
11421 	/* now fix the chunk length */
11422 	ch->chunk_length = htons(len + old_len);
11423 	chk->book_size = len + old_len;
11424 	chk->book_size_scale = 0;
11425 	chk->send_size = SCTP_SIZE32(chk->book_size);
11426 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11427 	return;
11428 
11429 }
11430 
11431 
11432 void
11433 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11434     uint32_t resp_seq, uint32_t result,
11435     uint32_t send_una, uint32_t recv_next)
11436 {
11437 	int len, old_len;
11438 	struct sctp_stream_reset_response_tsn *resp;
11439 	struct sctp_chunkhdr *ch;
11440 
11441 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11442 
11443 
11444 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11445 
11446 	/* get to new offset for the param. */
11447 	resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11448 	/* now how long will this param be? */
11449 	len = sizeof(struct sctp_stream_reset_response_tsn);
11450 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11451 	resp->ph.param_length = htons(len);
11452 	resp->response_seq = htonl(resp_seq);
11453 	resp->result = htonl(result);
11454 	resp->senders_next_tsn = htonl(send_una);
11455 	resp->receivers_next_tsn = htonl(recv_next);
11456 
11457 	/* now fix the chunk length */
11458 	ch->chunk_length = htons(len + old_len);
11459 	chk->book_size = len + old_len;
11460 	chk->send_size = SCTP_SIZE32(chk->book_size);
11461 	chk->book_size_scale = 0;
11462 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11463 	return;
11464 }
11465 
11466 static void
11467 sctp_add_a_stream(struct sctp_tmit_chunk *chk,
11468     uint32_t seq,
11469     uint16_t adding)
11470 {
11471 	int len, old_len;
11472 	struct sctp_chunkhdr *ch;
11473 	struct sctp_stream_reset_add_strm *addstr;
11474 
11475 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11476 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11477 
11478 	/* get to new offset for the param. */
11479 	addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11480 	/* now how long will this param be? */
11481 	len = sizeof(struct sctp_stream_reset_add_strm);
11482 
11483 	/* Fill it out. */
11484 	addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_STREAMS);
11485 	addstr->ph.param_length = htons(len);
11486 	addstr->request_seq = htonl(seq);
11487 	addstr->number_of_streams = htons(adding);
11488 	addstr->reserved = 0;
11489 
11490 	/* now fix the chunk length */
11491 	ch->chunk_length = htons(len + old_len);
11492 	chk->send_size = len + old_len;
11493 	chk->book_size = SCTP_SIZE32(chk->send_size);
11494 	chk->book_size_scale = 0;
11495 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11496 	return;
11497 }
11498 
11499 int
11500 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11501     int number_entries, uint16_t * list,
11502     uint8_t send_out_req,
11503     uint32_t resp_seq,
11504     uint8_t send_in_req,
11505     uint8_t send_tsn_req,
11506     uint8_t add_stream,
11507     uint16_t adding
11508 )
11509 {
11510 
11511 	struct sctp_association *asoc;
11512 	struct sctp_tmit_chunk *chk;
11513 	struct sctp_chunkhdr *ch;
11514 	uint32_t seq;
11515 
11516 	asoc = &stcb->asoc;
11517 	if (asoc->stream_reset_outstanding) {
11518 		/*-
11519 		 * Already one pending, must get ACK back to clear the flag.
11520 		 */
11521 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11522 		return (EBUSY);
11523 	}
11524 	if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
11525 	    (add_stream == 0)) {
11526 		/* nothing to do */
11527 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11528 		return (EINVAL);
11529 	}
11530 	if (send_tsn_req && (send_out_req || send_in_req)) {
11531 		/* error, can't do that */
11532 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11533 		return (EINVAL);
11534 	}
11535 	sctp_alloc_a_chunk(stcb, chk);
11536 	if (chk == NULL) {
11537 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11538 		return (ENOMEM);
11539 	}
11540 	chk->copy_by_ref = 0;
11541 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11542 	chk->rec.chunk_id.can_take_data = 0;
11543 	chk->asoc = &stcb->asoc;
11544 	chk->book_size = sizeof(struct sctp_chunkhdr);
11545 	chk->send_size = SCTP_SIZE32(chk->book_size);
11546 	chk->book_size_scale = 0;
11547 
11548 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11549 	if (chk->data == NULL) {
11550 		sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
11551 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11552 		return (ENOMEM);
11553 	}
11554 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11555 
11556 	/* setup chunk parameters */
11557 	chk->sent = SCTP_DATAGRAM_UNSENT;
11558 	chk->snd_count = 0;
11559 	chk->whoTo = asoc->primary_destination;
11560 	atomic_add_int(&chk->whoTo->ref_count, 1);
11561 
11562 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11563 	ch->chunk_type = SCTP_STREAM_RESET;
11564 	ch->chunk_flags = 0;
11565 	ch->chunk_length = htons(chk->book_size);
11566 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11567 
11568 	seq = stcb->asoc.str_reset_seq_out;
11569 	if (send_out_req) {
11570 		sctp_add_stream_reset_out(chk, number_entries, list,
11571 		    seq, resp_seq, (stcb->asoc.sending_seq - 1));
11572 		asoc->stream_reset_out_is_outstanding = 1;
11573 		seq++;
11574 		asoc->stream_reset_outstanding++;
11575 	}
11576 	if (add_stream) {
11577 		sctp_add_a_stream(chk, seq, adding);
11578 		seq++;
11579 		asoc->stream_reset_outstanding++;
11580 	}
11581 	if (send_in_req) {
11582 		sctp_add_stream_reset_in(chk, number_entries, list, seq);
11583 		asoc->stream_reset_outstanding++;
11584 	}
11585 	if (send_tsn_req) {
11586 		sctp_add_stream_reset_tsn(chk, seq);
11587 		asoc->stream_reset_outstanding++;
11588 	}
11589 	asoc->str_reset = chk;
11590 
11591 	/* insert the chunk for sending */
11592 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11593 	    chk,
11594 	    sctp_next);
11595 	asoc->ctrl_queue_cnt++;
11596 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11597 	return (0);
11598 }
11599 
11600 void
11601 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11602     struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11603 {
11604 	/*-
11605 	 * Formulate the abort message, and send it back down.
11606 	 */
11607 	struct mbuf *o_pak;
11608 	struct mbuf *mout;
11609 	struct sctp_abort_msg *abm;
11610 	struct ip *iph;
11611 	struct udphdr *udp;
11612 	int iphlen_out, len;
11613 
11614 #ifdef INET
11615 	struct ip *iph_out;
11616 
11617 #endif
11618 #ifdef INET6
11619 	struct ip6_hdr *ip6, *ip6_out;
11620 
11621 #endif
11622 
11623 	/* don't respond to ABORT with ABORT */
11624 	if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11625 		if (err_cause)
11626 			sctp_m_freem(err_cause);
11627 		return;
11628 	}
11629 	iph = mtod(m, struct ip *);
11630 	switch (iph->ip_v) {
11631 #ifdef INET
11632 	case IPVERSION:
11633 		len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11634 		break;
11635 #endif
11636 #ifdef INET6
11637 	case IPV6_VERSION >> 4:
11638 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11639 		break;
11640 #endif
11641 	default:
11642 		if (err_cause) {
11643 			sctp_m_freem(err_cause);
11644 		}
11645 		return;
11646 	}
11647 	if (port) {
11648 		len += sizeof(struct udphdr);
11649 	}
11650 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11651 	if (mout == NULL) {
11652 		if (err_cause) {
11653 			sctp_m_freem(err_cause);
11654 		}
11655 		return;
11656 	}
11657 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
11658 	SCTP_BUF_LEN(mout) = len;
11659 	SCTP_BUF_NEXT(mout) = err_cause;
11660 	if (m->m_flags & M_FLOWID) {
11661 		mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
11662 		mout->m_flags |= M_FLOWID;
11663 	}
11664 #ifdef INET
11665 	iph_out = NULL;
11666 #endif
11667 #ifdef INET6
11668 	ip6_out = NULL;
11669 #endif
11670 	switch (iph->ip_v) {
11671 #ifdef INET
11672 	case IPVERSION:
11673 		iph_out = mtod(mout, struct ip *);
11674 
11675 		/* Fill in the IP header for the ABORT */
11676 		iph_out->ip_v = IPVERSION;
11677 		iph_out->ip_hl = (sizeof(struct ip) / 4);
11678 		iph_out->ip_tos = (u_char)0;
11679 		iph_out->ip_id = 0;
11680 		iph_out->ip_off = 0;
11681 		iph_out->ip_ttl = MAXTTL;
11682 		if (port) {
11683 			iph_out->ip_p = IPPROTO_UDP;
11684 		} else {
11685 			iph_out->ip_p = IPPROTO_SCTP;
11686 		}
11687 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11688 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11689 		/* let IP layer calculate this */
11690 		iph_out->ip_sum = 0;
11691 
11692 		iphlen_out = sizeof(*iph_out);
11693 		abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
11694 		break;
11695 #endif
11696 #ifdef INET6
11697 	case IPV6_VERSION >> 4:
11698 		ip6 = (struct ip6_hdr *)iph;
11699 		ip6_out = mtod(mout, struct ip6_hdr *);
11700 
11701 		/* Fill in the IP6 header for the ABORT */
11702 		ip6_out->ip6_flow = ip6->ip6_flow;
11703 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11704 		if (port) {
11705 			ip6_out->ip6_nxt = IPPROTO_UDP;
11706 		} else {
11707 			ip6_out->ip6_nxt = IPPROTO_SCTP;
11708 		}
11709 		ip6_out->ip6_src = ip6->ip6_dst;
11710 		ip6_out->ip6_dst = ip6->ip6_src;
11711 
11712 		iphlen_out = sizeof(*ip6_out);
11713 		abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
11714 		break;
11715 #endif				/* INET6 */
11716 	default:
11717 		/* Currently not supported */
11718 		sctp_m_freem(mout);
11719 		return;
11720 	}
11721 
11722 	udp = (struct udphdr *)abm;
11723 	if (port) {
11724 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11725 		udp->uh_dport = port;
11726 		/* set udp->uh_ulen later */
11727 		udp->uh_sum = 0;
11728 		iphlen_out += sizeof(struct udphdr);
11729 		abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
11730 	}
11731 	abm->sh.src_port = sh->dest_port;
11732 	abm->sh.dest_port = sh->src_port;
11733 	abm->sh.checksum = 0;
11734 	if (vtag == 0) {
11735 		abm->sh.v_tag = sh->v_tag;
11736 		abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
11737 	} else {
11738 		abm->sh.v_tag = htonl(vtag);
11739 		abm->msg.ch.chunk_flags = 0;
11740 	}
11741 	abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11742 
11743 	if (err_cause) {
11744 		struct mbuf *m_tmp = err_cause;
11745 		int err_len = 0;
11746 
11747 		/* get length of the err_cause chain */
11748 		while (m_tmp != NULL) {
11749 			err_len += SCTP_BUF_LEN(m_tmp);
11750 			m_tmp = SCTP_BUF_NEXT(m_tmp);
11751 		}
11752 		len = SCTP_BUF_LEN(mout) + err_len;
11753 		if (err_len % 4) {
11754 			/* need pad at end of chunk */
11755 			uint32_t cpthis = 0;
11756 			int padlen;
11757 
11758 			padlen = 4 - (len % 4);
11759 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11760 			len += padlen;
11761 		}
11762 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
11763 	} else {
11764 		len = SCTP_BUF_LEN(mout);
11765 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
11766 	}
11767 
11768 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11769 		/* no mbuf's */
11770 		sctp_m_freem(mout);
11771 		return;
11772 	}
11773 #ifdef INET
11774 	if (iph_out != NULL) {
11775 		sctp_route_t ro;
11776 		int ret;
11777 
11778 		/* zap the stack pointer to the route */
11779 		bzero(&ro, sizeof ro);
11780 		if (port) {
11781 			udp->uh_ulen = htons(len - sizeof(struct ip));
11782 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11783 		}
11784 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
11785 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
11786 		/* set IPv4 length */
11787 		iph_out->ip_len = len;
11788 		/* out it goes */
11789 #ifdef  SCTP_PACKET_LOGGING
11790 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11791 			sctp_packet_log(mout, len);
11792 #endif
11793 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11794 		if (port) {
11795 #if defined(SCTP_WITH_NO_CSUM)
11796 			SCTP_STAT_INCR(sctps_sendnocrc);
11797 #else
11798 			abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
11799 			SCTP_STAT_INCR(sctps_sendswcrc);
11800 #endif
11801 			SCTP_ENABLE_UDP_CSUM(o_pak);
11802 		} else {
11803 #if defined(SCTP_WITH_NO_CSUM)
11804 			SCTP_STAT_INCR(sctps_sendnocrc);
11805 #else
11806 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
11807 			mout->m_pkthdr.csum_data = 0;
11808 			SCTP_STAT_INCR(sctps_sendhwcrc);
11809 #endif
11810 		}
11811 		SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
11812 
11813 		/* Free the route if we got one back */
11814 		if (ro.ro_rt)
11815 			RTFREE(ro.ro_rt);
11816 	}
11817 #endif
11818 #ifdef INET6
11819 	if (ip6_out != NULL) {
11820 		struct route_in6 ro;
11821 		int ret;
11822 		struct ifnet *ifp = NULL;
11823 
11824 		/* zap the stack pointer to the route */
11825 		bzero(&ro, sizeof(ro));
11826 		if (port) {
11827 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11828 		}
11829 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
11830 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
11831 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
11832 #ifdef  SCTP_PACKET_LOGGING
11833 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11834 			sctp_packet_log(mout, len);
11835 #endif
11836 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11837 		if (port) {
11838 #if defined(SCTP_WITH_NO_CSUM)
11839 			SCTP_STAT_INCR(sctps_sendnocrc);
11840 #else
11841 			abm->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11842 			SCTP_STAT_INCR(sctps_sendswcrc);
11843 #endif
11844 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11845 				udp->uh_sum = 0xffff;
11846 			}
11847 		} else {
11848 #if defined(SCTP_WITH_NO_CSUM)
11849 			SCTP_STAT_INCR(sctps_sendnocrc);
11850 #else
11851 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
11852 			mout->m_pkthdr.csum_data = 0;
11853 			SCTP_STAT_INCR(sctps_sendhwcrc);
11854 #endif
11855 		}
11856 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
11857 
11858 		/* Free the route if we got one back */
11859 		if (ro.ro_rt)
11860 			RTFREE(ro.ro_rt);
11861 	}
11862 #endif
11863 	SCTP_STAT_INCR(sctps_sendpackets);
11864 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11865 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11866 }
11867 
11868 void
11869 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
11870     uint32_t vrf_id, uint16_t port)
11871 {
11872 	struct mbuf *o_pak;
11873 	struct sctphdr *sh, *sh_out;
11874 	struct sctp_chunkhdr *ch;
11875 	struct ip *iph;
11876 	struct udphdr *udp = NULL;
11877 	struct mbuf *mout;
11878 	int iphlen_out, len;
11879 
11880 #ifdef INET
11881 	struct ip *iph_out;
11882 
11883 #endif
11884 #ifdef INET6
11885 	struct ip6_hdr *ip6, *ip6_out;
11886 
11887 #endif
11888 
11889 	iph = mtod(m, struct ip *);
11890 	sh = (struct sctphdr *)((caddr_t)iph + iphlen);
11891 	switch (iph->ip_v) {
11892 #ifdef INET
11893 	case IPVERSION:
11894 		len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11895 		break;
11896 #endif
11897 #ifdef INET6
11898 	case IPV6_VERSION >> 4:
11899 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11900 		break;
11901 #endif
11902 	default:
11903 		if (scm) {
11904 			sctp_m_freem(scm);
11905 		}
11906 		return;
11907 	}
11908 	if (port) {
11909 		len += sizeof(struct udphdr);
11910 	}
11911 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11912 	if (mout == NULL) {
11913 		if (scm) {
11914 			sctp_m_freem(scm);
11915 		}
11916 		return;
11917 	}
11918 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
11919 	SCTP_BUF_LEN(mout) = len;
11920 	SCTP_BUF_NEXT(mout) = scm;
11921 	if (m->m_flags & M_FLOWID) {
11922 		mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
11923 		mout->m_flags |= M_FLOWID;
11924 	}
11925 #ifdef INET
11926 	iph_out = NULL;
11927 #endif
11928 #ifdef INET6
11929 	ip6_out = NULL;
11930 #endif
11931 	switch (iph->ip_v) {
11932 #ifdef INET
11933 	case IPVERSION:
11934 		iph_out = mtod(mout, struct ip *);
11935 
11936 		/* Fill in the IP header for the ABORT */
11937 		iph_out->ip_v = IPVERSION;
11938 		iph_out->ip_hl = (sizeof(struct ip) / 4);
11939 		iph_out->ip_tos = (u_char)0;
11940 		iph_out->ip_id = 0;
11941 		iph_out->ip_off = 0;
11942 		iph_out->ip_ttl = MAXTTL;
11943 		if (port) {
11944 			iph_out->ip_p = IPPROTO_UDP;
11945 		} else {
11946 			iph_out->ip_p = IPPROTO_SCTP;
11947 		}
11948 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11949 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11950 		/* let IP layer calculate this */
11951 		iph_out->ip_sum = 0;
11952 
11953 		iphlen_out = sizeof(struct ip);
11954 		sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
11955 		break;
11956 #endif
11957 #ifdef INET6
11958 	case IPV6_VERSION >> 4:
11959 		ip6 = (struct ip6_hdr *)iph;
11960 		ip6_out = mtod(mout, struct ip6_hdr *);
11961 
11962 		/* Fill in the IP6 header for the ABORT */
11963 		ip6_out->ip6_flow = ip6->ip6_flow;
11964 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11965 		if (port) {
11966 			ip6_out->ip6_nxt = IPPROTO_UDP;
11967 		} else {
11968 			ip6_out->ip6_nxt = IPPROTO_SCTP;
11969 		}
11970 		ip6_out->ip6_src = ip6->ip6_dst;
11971 		ip6_out->ip6_dst = ip6->ip6_src;
11972 
11973 		iphlen_out = sizeof(struct ip6_hdr);
11974 		sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
11975 		break;
11976 #endif				/* INET6 */
11977 	default:
11978 		/* Currently not supported */
11979 		sctp_m_freem(mout);
11980 		return;
11981 	}
11982 
11983 	udp = (struct udphdr *)sh_out;
11984 	if (port) {
11985 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11986 		udp->uh_dport = port;
11987 		/* set udp->uh_ulen later */
11988 		udp->uh_sum = 0;
11989 		iphlen_out += sizeof(struct udphdr);
11990 		sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
11991 	}
11992 	sh_out->src_port = sh->dest_port;
11993 	sh_out->dest_port = sh->src_port;
11994 	sh_out->v_tag = vtag;
11995 	sh_out->checksum = 0;
11996 
11997 	ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
11998 	ch->chunk_type = SCTP_OPERATION_ERROR;
11999 	ch->chunk_flags = 0;
12000 
12001 	if (scm) {
12002 		struct mbuf *m_tmp = scm;
12003 		int cause_len = 0;
12004 
12005 		/* get length of the err_cause chain */
12006 		while (m_tmp != NULL) {
12007 			cause_len += SCTP_BUF_LEN(m_tmp);
12008 			m_tmp = SCTP_BUF_NEXT(m_tmp);
12009 		}
12010 		len = SCTP_BUF_LEN(mout) + cause_len;
12011 		if (cause_len % 4) {
12012 			/* need pad at end of chunk */
12013 			uint32_t cpthis = 0;
12014 			int padlen;
12015 
12016 			padlen = 4 - (len % 4);
12017 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
12018 			len += padlen;
12019 		}
12020 		ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
12021 	} else {
12022 		len = SCTP_BUF_LEN(mout);
12023 		ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
12024 	}
12025 
12026 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12027 		/* no mbuf's */
12028 		sctp_m_freem(mout);
12029 		return;
12030 	}
12031 #ifdef INET
12032 	if (iph_out != NULL) {
12033 		sctp_route_t ro;
12034 		int ret;
12035 
12036 		/* zap the stack pointer to the route */
12037 		bzero(&ro, sizeof ro);
12038 		if (port) {
12039 			udp->uh_ulen = htons(len - sizeof(struct ip));
12040 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12041 		}
12042 		/* set IPv4 length */
12043 		iph_out->ip_len = len;
12044 		/* out it goes */
12045 #ifdef  SCTP_PACKET_LOGGING
12046 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12047 			sctp_packet_log(mout, len);
12048 #endif
12049 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
12050 		if (port) {
12051 #if defined(SCTP_WITH_NO_CSUM)
12052 			SCTP_STAT_INCR(sctps_sendnocrc);
12053 #else
12054 			sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
12055 			SCTP_STAT_INCR(sctps_sendswcrc);
12056 #endif
12057 			SCTP_ENABLE_UDP_CSUM(o_pak);
12058 		} else {
12059 #if defined(SCTP_WITH_NO_CSUM)
12060 			SCTP_STAT_INCR(sctps_sendnocrc);
12061 #else
12062 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
12063 			mout->m_pkthdr.csum_data = 0;
12064 			SCTP_STAT_INCR(sctps_sendhwcrc);
12065 #endif
12066 		}
12067 		SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
12068 
12069 		/* Free the route if we got one back */
12070 		if (ro.ro_rt)
12071 			RTFREE(ro.ro_rt);
12072 	}
12073 #endif
12074 #ifdef INET6
12075 	if (ip6_out != NULL) {
12076 		struct route_in6 ro;
12077 		int ret;
12078 		struct ifnet *ifp = NULL;
12079 
12080 		/* zap the stack pointer to the route */
12081 		bzero(&ro, sizeof(ro));
12082 		if (port) {
12083 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
12084 		}
12085 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
12086 #ifdef  SCTP_PACKET_LOGGING
12087 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12088 			sctp_packet_log(mout, len);
12089 #endif
12090 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
12091 		if (port) {
12092 #if defined(SCTP_WITH_NO_CSUM)
12093 			SCTP_STAT_INCR(sctps_sendnocrc);
12094 #else
12095 			sh_out->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
12096 			SCTP_STAT_INCR(sctps_sendswcrc);
12097 #endif
12098 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
12099 				udp->uh_sum = 0xffff;
12100 			}
12101 		} else {
12102 #if defined(SCTP_WITH_NO_CSUM)
12103 			SCTP_STAT_INCR(sctps_sendnocrc);
12104 #else
12105 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
12106 			mout->m_pkthdr.csum_data = 0;
12107 			SCTP_STAT_INCR(sctps_sendhwcrc);
12108 #endif
12109 		}
12110 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
12111 
12112 		/* Free the route if we got one back */
12113 		if (ro.ro_rt)
12114 			RTFREE(ro.ro_rt);
12115 	}
12116 #endif
12117 	SCTP_STAT_INCR(sctps_sendpackets);
12118 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12119 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12120 }
12121 
12122 static struct mbuf *
12123 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
12124     struct uio *uio,
12125     struct sctp_sndrcvinfo *srcv,
12126     int max_send_len,
12127     int user_marks_eor,
12128     int *error,
12129     uint32_t * sndout,
12130     struct mbuf **new_tail)
12131 {
12132 	struct mbuf *m;
12133 
12134 	m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
12135 	    (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
12136 	if (m == NULL) {
12137 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12138 		*error = ENOMEM;
12139 	} else {
12140 		*sndout = m_length(m, NULL);
12141 		*new_tail = m_last(m);
12142 	}
12143 	return (m);
12144 }
12145 
12146 static int
12147 sctp_copy_one(struct sctp_stream_queue_pending *sp,
12148     struct uio *uio,
12149     int resv_upfront)
12150 {
12151 	int left;
12152 
12153 	left = sp->length;
12154 	sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
12155 	    resv_upfront, 0);
12156 	if (sp->data == NULL) {
12157 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12158 		return (ENOMEM);
12159 	}
12160 	sp->tail_mbuf = m_last(sp->data);
12161 	return (0);
12162 }
12163 
12164 
12165 
12166 static struct sctp_stream_queue_pending *
12167 sctp_copy_it_in(struct sctp_tcb *stcb,
12168     struct sctp_association *asoc,
12169     struct sctp_sndrcvinfo *srcv,
12170     struct uio *uio,
12171     struct sctp_nets *net,
12172     int max_send_len,
12173     int user_marks_eor,
12174     int *error,
12175     int non_blocking)
12176 {
12177 	/*-
12178 	 * This routine must be very careful in its work. Protocol
12179 	 * processing is up and running so care must be taken to spl...()
12180 	 * when you need to do something that may effect the stcb/asoc. The
12181 	 * sb is locked however. When data is copied the protocol processing
12182 	 * should be enabled since this is a slower operation...
12183 	 */
12184 	struct sctp_stream_queue_pending *sp = NULL;
12185 	int resv_in_first;
12186 
12187 	*error = 0;
12188 	/* Now can we send this? */
12189 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12190 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12191 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12192 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12193 		/* got data while shutting down */
12194 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12195 		*error = ECONNRESET;
12196 		goto out_now;
12197 	}
12198 	sctp_alloc_a_strmoq(stcb, sp);
12199 	if (sp == NULL) {
12200 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12201 		*error = ENOMEM;
12202 		goto out_now;
12203 	}
12204 	sp->act_flags = 0;
12205 	sp->sender_all_done = 0;
12206 	sp->sinfo_flags = srcv->sinfo_flags;
12207 	sp->timetolive = srcv->sinfo_timetolive;
12208 	sp->ppid = srcv->sinfo_ppid;
12209 	sp->context = srcv->sinfo_context;
12210 	sp->strseq = 0;
12211 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12212 
12213 	sp->stream = srcv->sinfo_stream;
12214 	sp->length = min(uio->uio_resid, max_send_len);
12215 	if ((sp->length == (uint32_t) uio->uio_resid) &&
12216 	    ((user_marks_eor == 0) ||
12217 	    (srcv->sinfo_flags & SCTP_EOF) ||
12218 	    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12219 		sp->msg_is_complete = 1;
12220 	} else {
12221 		sp->msg_is_complete = 0;
12222 	}
12223 	sp->sender_all_done = 0;
12224 	sp->some_taken = 0;
12225 	sp->put_last_out = 0;
12226 	resv_in_first = sizeof(struct sctp_data_chunk);
12227 	sp->data = sp->tail_mbuf = NULL;
12228 	if (sp->length == 0) {
12229 		*error = 0;
12230 		goto skip_copy;
12231 	}
12232 	sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12233 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12234 		sctp_auth_key_acquire(stcb, stcb->asoc.authinfo.active_keyid);
12235 		sp->holds_key_ref = 1;
12236 	}
12237 	*error = sctp_copy_one(sp, uio, resv_in_first);
12238 skip_copy:
12239 	if (*error) {
12240 		sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
12241 		sp = NULL;
12242 	} else {
12243 		if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12244 			sp->net = net;
12245 			atomic_add_int(&sp->net->ref_count, 1);
12246 		} else {
12247 			sp->net = NULL;
12248 		}
12249 		sctp_set_prsctp_policy(sp);
12250 	}
12251 out_now:
12252 	return (sp);
12253 }
12254 
12255 
12256 int
12257 sctp_sosend(struct socket *so,
12258     struct sockaddr *addr,
12259     struct uio *uio,
12260     struct mbuf *top,
12261     struct mbuf *control,
12262     int flags,
12263     struct thread *p
12264 )
12265 {
12266 	int error, use_rcvinfo = 0;
12267 	struct sctp_sndrcvinfo srcv;
12268 	struct sockaddr *addr_to_use;
12269 
12270 #if defined(INET) && defined(INET6)
12271 	struct sockaddr_in sin;
12272 
12273 #endif
12274 
12275 	if (control) {
12276 		/* process cmsg snd/rcv info (maybe a assoc-id) */
12277 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
12278 		    sizeof(srcv))) {
12279 			/* got one */
12280 			use_rcvinfo = 1;
12281 		}
12282 	}
12283 	addr_to_use = addr;
12284 #if defined(INET) && defined(INET6)
12285 	if ((addr) && (addr->sa_family == AF_INET6)) {
12286 		struct sockaddr_in6 *sin6;
12287 
12288 		sin6 = (struct sockaddr_in6 *)addr;
12289 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12290 			in6_sin6_2_sin(&sin, sin6);
12291 			addr_to_use = (struct sockaddr *)&sin;
12292 		}
12293 	}
12294 #endif
12295 	error = sctp_lower_sosend(so, addr_to_use, uio, top,
12296 	    control,
12297 	    flags,
12298 	    use_rcvinfo ? &srcv : NULL
12299 	    ,p
12300 	    );
12301 	return (error);
12302 }
12303 
12304 
12305 int
12306 sctp_lower_sosend(struct socket *so,
12307     struct sockaddr *addr,
12308     struct uio *uio,
12309     struct mbuf *i_pak,
12310     struct mbuf *control,
12311     int flags,
12312     struct sctp_sndrcvinfo *srcv
12313     ,
12314     struct thread *p
12315 )
12316 {
12317 	unsigned int sndlen = 0, max_len;
12318 	int error, len;
12319 	struct mbuf *top = NULL;
12320 	int queue_only = 0, queue_only_for_init = 0;
12321 	int free_cnt_applied = 0;
12322 	int un_sent;
12323 	int now_filled = 0;
12324 	unsigned int inqueue_bytes = 0;
12325 	struct sctp_block_entry be;
12326 	struct sctp_inpcb *inp;
12327 	struct sctp_tcb *stcb = NULL;
12328 	struct timeval now;
12329 	struct sctp_nets *net;
12330 	struct sctp_association *asoc;
12331 	struct sctp_inpcb *t_inp;
12332 	int user_marks_eor;
12333 	int create_lock_applied = 0;
12334 	int nagle_applies = 0;
12335 	int some_on_control = 0;
12336 	int got_all_of_the_send = 0;
12337 	int hold_tcblock = 0;
12338 	int non_blocking = 0;
12339 	uint32_t local_add_more, local_soresv = 0;
12340 	uint16_t port;
12341 	uint16_t sinfo_flags;
12342 	sctp_assoc_t sinfo_assoc_id;
12343 
12344 	error = 0;
12345 	net = NULL;
12346 	stcb = NULL;
12347 	asoc = NULL;
12348 
12349 	t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12350 	if (inp == NULL) {
12351 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12352 		error = EINVAL;
12353 		if (i_pak) {
12354 			SCTP_RELEASE_PKT(i_pak);
12355 		}
12356 		return (error);
12357 	}
12358 	if ((uio == NULL) && (i_pak == NULL)) {
12359 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12360 		return (EINVAL);
12361 	}
12362 	user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12363 	atomic_add_int(&inp->total_sends, 1);
12364 	if (uio) {
12365 		if (uio->uio_resid < 0) {
12366 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12367 			return (EINVAL);
12368 		}
12369 		sndlen = uio->uio_resid;
12370 	} else {
12371 		top = SCTP_HEADER_TO_CHAIN(i_pak);
12372 		sndlen = SCTP_HEADER_LEN(i_pak);
12373 	}
12374 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
12375 	    addr,
12376 	    sndlen);
12377 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12378 	    (inp->sctp_socket->so_qlimit)) {
12379 		/* The listener can NOT send */
12380 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12381 		error = ENOTCONN;
12382 		goto out_unlocked;
12383 	}
12384 	/**
12385 	 * Pre-screen address, if one is given the sin-len
12386 	 * must be set correctly!
12387 	 */
12388 	if (addr) {
12389 		union sctp_sockstore *raddr = (union sctp_sockstore *)addr;
12390 
12391 		switch (raddr->sa.sa_family) {
12392 #if defined(INET)
12393 		case AF_INET:
12394 			if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) {
12395 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12396 				error = EINVAL;
12397 				goto out_unlocked;
12398 			}
12399 			port = raddr->sin.sin_port;
12400 			break;
12401 #endif
12402 #if defined(INET6)
12403 		case AF_INET6:
12404 			if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) {
12405 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12406 				error = EINVAL;
12407 				goto out_unlocked;
12408 			}
12409 			port = raddr->sin6.sin6_port;
12410 			break;
12411 #endif
12412 		default:
12413 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAFNOSUPPORT);
12414 			error = EAFNOSUPPORT;
12415 			goto out_unlocked;
12416 		}
12417 	} else
12418 		port = 0;
12419 
12420 	if (srcv) {
12421 		sinfo_flags = srcv->sinfo_flags;
12422 		sinfo_assoc_id = srcv->sinfo_assoc_id;
12423 		if (INVALID_SINFO_FLAG(sinfo_flags) ||
12424 		    PR_SCTP_INVALID_POLICY(sinfo_flags)) {
12425 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12426 			error = EINVAL;
12427 			goto out_unlocked;
12428 		}
12429 		if (srcv->sinfo_flags)
12430 			SCTP_STAT_INCR(sctps_sends_with_flags);
12431 	} else {
12432 		sinfo_flags = inp->def_send.sinfo_flags;
12433 		sinfo_assoc_id = inp->def_send.sinfo_assoc_id;
12434 	}
12435 	if (sinfo_flags & SCTP_SENDALL) {
12436 		/* its a sendall */
12437 		error = sctp_sendall(inp, uio, top, srcv);
12438 		top = NULL;
12439 		goto out_unlocked;
12440 	}
12441 	if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) {
12442 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12443 		error = EINVAL;
12444 		goto out_unlocked;
12445 	}
12446 	/* now we must find the assoc */
12447 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12448 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12449 		SCTP_INP_RLOCK(inp);
12450 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
12451 		if (stcb == NULL) {
12452 			SCTP_INP_RUNLOCK(inp);
12453 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12454 			error = ENOTCONN;
12455 			goto out_unlocked;
12456 		}
12457 		SCTP_TCB_LOCK(stcb);
12458 		hold_tcblock = 1;
12459 		SCTP_INP_RUNLOCK(inp);
12460 	} else if (sinfo_assoc_id) {
12461 		stcb = sctp_findassociation_ep_asocid(inp, sinfo_assoc_id, 0);
12462 	} else if (addr) {
12463 		/*-
12464 		 * Since we did not use findep we must
12465 		 * increment it, and if we don't find a tcb
12466 		 * decrement it.
12467 		 */
12468 		SCTP_INP_WLOCK(inp);
12469 		SCTP_INP_INCR_REF(inp);
12470 		SCTP_INP_WUNLOCK(inp);
12471 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12472 		if (stcb == NULL) {
12473 			SCTP_INP_WLOCK(inp);
12474 			SCTP_INP_DECR_REF(inp);
12475 			SCTP_INP_WUNLOCK(inp);
12476 		} else {
12477 			hold_tcblock = 1;
12478 		}
12479 	}
12480 	if ((stcb == NULL) && (addr)) {
12481 		/* Possible implicit send? */
12482 		SCTP_ASOC_CREATE_LOCK(inp);
12483 		create_lock_applied = 1;
12484 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12485 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12486 			/* Should I really unlock ? */
12487 			SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12488 			error = EINVAL;
12489 			goto out_unlocked;
12490 
12491 		}
12492 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12493 		    (addr->sa_family == AF_INET6)) {
12494 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12495 			error = EINVAL;
12496 			goto out_unlocked;
12497 		}
12498 		SCTP_INP_WLOCK(inp);
12499 		SCTP_INP_INCR_REF(inp);
12500 		SCTP_INP_WUNLOCK(inp);
12501 		/* With the lock applied look again */
12502 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12503 		if (stcb == NULL) {
12504 			SCTP_INP_WLOCK(inp);
12505 			SCTP_INP_DECR_REF(inp);
12506 			SCTP_INP_WUNLOCK(inp);
12507 		} else {
12508 			hold_tcblock = 1;
12509 		}
12510 		if (t_inp != inp) {
12511 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12512 			error = ENOTCONN;
12513 			goto out_unlocked;
12514 		}
12515 	}
12516 	if (stcb == NULL) {
12517 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
12518 		    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12519 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12520 			error = ENOTCONN;
12521 			goto out_unlocked;
12522 		}
12523 		if (addr == NULL) {
12524 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12525 			error = ENOENT;
12526 			goto out_unlocked;
12527 		} else {
12528 			/*
12529 			 * UDP style, we must go ahead and start the INIT
12530 			 * process
12531 			 */
12532 			uint32_t vrf_id;
12533 
12534 			if ((sinfo_flags & SCTP_ABORT) ||
12535 			    ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) {
12536 				/*-
12537 				 * User asks to abort a non-existant assoc,
12538 				 * or EOF a non-existant assoc with no data
12539 				 */
12540 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12541 				error = ENOENT;
12542 				goto out_unlocked;
12543 			}
12544 			/* get an asoc/stcb struct */
12545 			vrf_id = inp->def_vrf_id;
12546 #ifdef INVARIANTS
12547 			if (create_lock_applied == 0) {
12548 				panic("Error, should hold create lock and I don't?");
12549 			}
12550 #endif
12551 			stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
12552 			    p
12553 			    );
12554 			if (stcb == NULL) {
12555 				/* Error is setup for us in the call */
12556 				goto out_unlocked;
12557 			}
12558 			if (create_lock_applied) {
12559 				SCTP_ASOC_CREATE_UNLOCK(inp);
12560 				create_lock_applied = 0;
12561 			} else {
12562 				SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12563 			}
12564 			/*
12565 			 * Turn on queue only flag to prevent data from
12566 			 * being sent
12567 			 */
12568 			queue_only = 1;
12569 			asoc = &stcb->asoc;
12570 			SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12571 			(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12572 
12573 			/* initialize authentication params for the assoc */
12574 			sctp_initialize_auth_params(inp, stcb);
12575 
12576 			if (control) {
12577 				/*
12578 				 * see if a init structure exists in cmsg
12579 				 * headers
12580 				 */
12581 				struct sctp_initmsg initm;
12582 				int i;
12583 
12584 				if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
12585 				    sizeof(initm))) {
12586 					/*
12587 					 * we have an INIT override of the
12588 					 * default
12589 					 */
12590 					if (initm.sinit_max_attempts)
12591 						asoc->max_init_times = initm.sinit_max_attempts;
12592 					if (initm.sinit_num_ostreams)
12593 						asoc->pre_open_streams = initm.sinit_num_ostreams;
12594 					if (initm.sinit_max_instreams)
12595 						asoc->max_inbound_streams = initm.sinit_max_instreams;
12596 					if (initm.sinit_max_init_timeo)
12597 						asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
12598 					if (asoc->streamoutcnt < asoc->pre_open_streams) {
12599 						struct sctp_stream_out *tmp_str;
12600 						int had_lock = 0;
12601 
12602 						/* Default is NOT correct */
12603 						SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
12604 						    asoc->streamoutcnt, asoc->pre_open_streams);
12605 						/*
12606 						 * What happens if this
12607 						 * fails? we panic ...
12608 						 */
12609 
12610 						if (hold_tcblock) {
12611 							had_lock = 1;
12612 							SCTP_TCB_UNLOCK(stcb);
12613 						}
12614 						SCTP_MALLOC(tmp_str,
12615 						    struct sctp_stream_out *,
12616 						    (asoc->pre_open_streams *
12617 						    sizeof(struct sctp_stream_out)),
12618 						    SCTP_M_STRMO);
12619 						if (had_lock) {
12620 							SCTP_TCB_LOCK(stcb);
12621 						}
12622 						if (tmp_str != NULL) {
12623 							SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
12624 							asoc->strmout = tmp_str;
12625 							asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
12626 						} else {
12627 							asoc->pre_open_streams = asoc->streamoutcnt;
12628 						}
12629 						for (i = 0; i < asoc->streamoutcnt; i++) {
12630 							/*-
12631 							 * inbound side must be set
12632 							 * to 0xffff, also NOTE when
12633 							 * we get the INIT-ACK back
12634 							 * (for INIT sender) we MUST
12635 							 * reduce the count
12636 							 * (streamoutcnt) but first
12637 							 * check if we sent to any
12638 							 * of the upper streams that
12639 							 * were dropped (if some
12640 							 * were). Those that were
12641 							 * dropped must be notified
12642 							 * to the upper layer as
12643 							 * failed to send.
12644 							 */
12645 							asoc->strmout[i].next_sequence_sent = 0x0;
12646 							TAILQ_INIT(&asoc->strmout[i].outqueue);
12647 							asoc->strmout[i].stream_no = i;
12648 							asoc->strmout[i].last_msg_incomplete = 0;
12649 							asoc->ss_functions.sctp_ss_init_stream(&asoc->strmout[i], NULL);
12650 						}
12651 					}
12652 				}
12653 			}
12654 			hold_tcblock = 1;
12655 			/* out with the INIT */
12656 			queue_only_for_init = 1;
12657 			/*-
12658 			 * we may want to dig in after this call and adjust the MTU
12659 			 * value. It defaulted to 1500 (constant) but the ro
12660 			 * structure may now have an update and thus we may need to
12661 			 * change it BEFORE we append the message.
12662 			 */
12663 		}
12664 	} else
12665 		asoc = &stcb->asoc;
12666 	if (srcv == NULL)
12667 		srcv = (struct sctp_sndrcvinfo *)&asoc->def_send;
12668 	if (srcv->sinfo_flags & SCTP_ADDR_OVER) {
12669 		if (addr)
12670 			net = sctp_findnet(stcb, addr);
12671 		else
12672 			net = NULL;
12673 		if ((net == NULL) ||
12674 		    ((port != 0) && (port != stcb->rport))) {
12675 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12676 			error = EINVAL;
12677 			goto out_unlocked;
12678 		}
12679 	} else {
12680 		net = stcb->asoc.primary_destination;
12681 	}
12682 	atomic_add_int(&stcb->total_sends, 1);
12683 	/* Keep the stcb from being freed under our feet */
12684 	atomic_add_int(&asoc->refcnt, 1);
12685 	free_cnt_applied = 1;
12686 
12687 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12688 		if (sndlen > asoc->smallest_mtu) {
12689 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12690 			error = EMSGSIZE;
12691 			goto out_unlocked;
12692 		}
12693 	}
12694 	if ((SCTP_SO_IS_NBIO(so)
12695 	    || (flags & MSG_NBIO)
12696 	    )) {
12697 		non_blocking = 1;
12698 	}
12699 	/* would we block? */
12700 	if (non_blocking) {
12701 		if (hold_tcblock == 0) {
12702 			SCTP_TCB_LOCK(stcb);
12703 			hold_tcblock = 1;
12704 		}
12705 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12706 		if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12707 		    (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12708 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
12709 			if (sndlen > SCTP_SB_LIMIT_SND(so))
12710 				error = EMSGSIZE;
12711 			else
12712 				error = EWOULDBLOCK;
12713 			goto out_unlocked;
12714 		}
12715 		stcb->asoc.sb_send_resv += sndlen;
12716 		SCTP_TCB_UNLOCK(stcb);
12717 		hold_tcblock = 0;
12718 	} else {
12719 		atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
12720 	}
12721 	local_soresv = sndlen;
12722 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12723 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12724 		error = ECONNRESET;
12725 		goto out_unlocked;
12726 	}
12727 	if (create_lock_applied) {
12728 		SCTP_ASOC_CREATE_UNLOCK(inp);
12729 		create_lock_applied = 0;
12730 	}
12731 	if (asoc->stream_reset_outstanding) {
12732 		/*
12733 		 * Can't queue any data while stream reset is underway.
12734 		 */
12735 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
12736 		error = EAGAIN;
12737 		goto out_unlocked;
12738 	}
12739 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12740 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12741 		queue_only = 1;
12742 	}
12743 	/* we are now done with all control */
12744 	if (control) {
12745 		sctp_m_freem(control);
12746 		control = NULL;
12747 	}
12748 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12749 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12750 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12751 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12752 		if (srcv->sinfo_flags & SCTP_ABORT) {
12753 			;
12754 		} else {
12755 			SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12756 			error = ECONNRESET;
12757 			goto out_unlocked;
12758 		}
12759 	}
12760 	/* Ok, we will attempt a msgsnd :> */
12761 	if (p) {
12762 		p->td_ru.ru_msgsnd++;
12763 	}
12764 	/* Are we aborting? */
12765 	if (srcv->sinfo_flags & SCTP_ABORT) {
12766 		struct mbuf *mm;
12767 		int tot_demand, tot_out = 0, max_out;
12768 
12769 		SCTP_STAT_INCR(sctps_sends_with_abort);
12770 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12771 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12772 			/* It has to be up before we abort */
12773 			/* how big is the user initiated abort? */
12774 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12775 			error = EINVAL;
12776 			goto out;
12777 		}
12778 		if (hold_tcblock) {
12779 			SCTP_TCB_UNLOCK(stcb);
12780 			hold_tcblock = 0;
12781 		}
12782 		if (top) {
12783 			struct mbuf *cntm = NULL;
12784 
12785 			mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
12786 			if (sndlen != 0) {
12787 				cntm = top;
12788 				while (cntm) {
12789 					tot_out += SCTP_BUF_LEN(cntm);
12790 					cntm = SCTP_BUF_NEXT(cntm);
12791 				}
12792 			}
12793 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12794 		} else {
12795 			/* Must fit in a MTU */
12796 			tot_out = sndlen;
12797 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12798 			if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12799 				/* To big */
12800 				SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12801 				error = EMSGSIZE;
12802 				goto out;
12803 			}
12804 			mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
12805 		}
12806 		if (mm == NULL) {
12807 			SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12808 			error = ENOMEM;
12809 			goto out;
12810 		}
12811 		max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12812 		max_out -= sizeof(struct sctp_abort_msg);
12813 		if (tot_out > max_out) {
12814 			tot_out = max_out;
12815 		}
12816 		if (mm) {
12817 			struct sctp_paramhdr *ph;
12818 
12819 			/* now move forward the data pointer */
12820 			ph = mtod(mm, struct sctp_paramhdr *);
12821 			ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12822 			ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
12823 			ph++;
12824 			SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
12825 			if (top == NULL) {
12826 				error = uiomove((caddr_t)ph, (int)tot_out, uio);
12827 				if (error) {
12828 					/*-
12829 					 * Here if we can't get his data we
12830 					 * still abort we just don't get to
12831 					 * send the users note :-0
12832 					 */
12833 					sctp_m_freem(mm);
12834 					mm = NULL;
12835 				}
12836 			} else {
12837 				if (sndlen != 0) {
12838 					SCTP_BUF_NEXT(mm) = top;
12839 				}
12840 			}
12841 		}
12842 		if (hold_tcblock == 0) {
12843 			SCTP_TCB_LOCK(stcb);
12844 			hold_tcblock = 1;
12845 		}
12846 		atomic_add_int(&stcb->asoc.refcnt, -1);
12847 		free_cnt_applied = 0;
12848 		/* release this lock, otherwise we hang on ourselves */
12849 		sctp_abort_an_association(stcb->sctp_ep, stcb,
12850 		    SCTP_RESPONSE_TO_USER_REQ,
12851 		    mm, SCTP_SO_LOCKED);
12852 		/* now relock the stcb so everything is sane */
12853 		hold_tcblock = 0;
12854 		stcb = NULL;
12855 		/*
12856 		 * In this case top is already chained to mm avoid double
12857 		 * free, since we free it below if top != NULL and driver
12858 		 * would free it after sending the packet out
12859 		 */
12860 		if (sndlen != 0) {
12861 			top = NULL;
12862 		}
12863 		goto out_unlocked;
12864 	}
12865 	/* Calculate the maximum we can send */
12866 	inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12867 	if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12868 		if (non_blocking) {
12869 			/* we already checked for non-blocking above. */
12870 			max_len = sndlen;
12871 		} else {
12872 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12873 		}
12874 	} else {
12875 		max_len = 0;
12876 	}
12877 	if (hold_tcblock) {
12878 		SCTP_TCB_UNLOCK(stcb);
12879 		hold_tcblock = 0;
12880 	}
12881 	/* Is the stream no. valid? */
12882 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
12883 		/* Invalid stream number */
12884 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12885 		error = EINVAL;
12886 		goto out_unlocked;
12887 	}
12888 	if (asoc->strmout == NULL) {
12889 		/* huh? software error */
12890 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12891 		error = EFAULT;
12892 		goto out_unlocked;
12893 	}
12894 	/* Unless E_EOR mode is on, we must make a send FIT in one call. */
12895 	if ((user_marks_eor == 0) &&
12896 	    (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
12897 		/* It will NEVER fit */
12898 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12899 		error = EMSGSIZE;
12900 		goto out_unlocked;
12901 	}
12902 	if ((uio == NULL) && user_marks_eor) {
12903 		/*-
12904 		 * We do not support eeor mode for
12905 		 * sending with mbuf chains (like sendfile).
12906 		 */
12907 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12908 		error = EINVAL;
12909 		goto out_unlocked;
12910 	}
12911 	if (user_marks_eor) {
12912 		local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
12913 	} else {
12914 		/*-
12915 		 * For non-eeor the whole message must fit in
12916 		 * the socket send buffer.
12917 		 */
12918 		local_add_more = sndlen;
12919 	}
12920 	len = 0;
12921 	if (non_blocking) {
12922 		goto skip_preblock;
12923 	}
12924 	if (((max_len <= local_add_more) &&
12925 	    (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
12926 	    (max_len == 0) ||
12927 	    ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12928 		/* No room right now ! */
12929 		SOCKBUF_LOCK(&so->so_snd);
12930 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12931 		while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
12932 		    ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12933 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
12934 			    (unsigned int)SCTP_SB_LIMIT_SND(so),
12935 			    inqueue_bytes,
12936 			    local_add_more,
12937 			    stcb->asoc.stream_queue_cnt,
12938 			    stcb->asoc.chunks_on_out_queue,
12939 			    SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
12940 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12941 				sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, so, asoc, sndlen);
12942 			}
12943 			be.error = 0;
12944 			stcb->block_entry = &be;
12945 			error = sbwait(&so->so_snd);
12946 			stcb->block_entry = NULL;
12947 			if (error || so->so_error || be.error) {
12948 				if (error == 0) {
12949 					if (so->so_error)
12950 						error = so->so_error;
12951 					if (be.error) {
12952 						error = be.error;
12953 					}
12954 				}
12955 				SOCKBUF_UNLOCK(&so->so_snd);
12956 				goto out_unlocked;
12957 			}
12958 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12959 				sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12960 				    so, asoc, stcb->asoc.total_output_queue_size);
12961 			}
12962 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12963 				goto out_unlocked;
12964 			}
12965 			inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12966 		}
12967 		if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12968 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12969 		} else {
12970 			max_len = 0;
12971 		}
12972 		SOCKBUF_UNLOCK(&so->so_snd);
12973 	}
12974 skip_preblock:
12975 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12976 		goto out_unlocked;
12977 	}
12978 	/*
12979 	 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
12980 	 * case NOTE: uio will be null when top/mbuf is passed
12981 	 */
12982 	if (sndlen == 0) {
12983 		if (srcv->sinfo_flags & SCTP_EOF) {
12984 			got_all_of_the_send = 1;
12985 			goto dataless_eof;
12986 		} else {
12987 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12988 			error = EINVAL;
12989 			goto out;
12990 		}
12991 	}
12992 	if (top == NULL) {
12993 		struct sctp_stream_queue_pending *sp;
12994 		struct sctp_stream_out *strm;
12995 		uint32_t sndout;
12996 
12997 		SCTP_TCB_SEND_LOCK(stcb);
12998 		if ((asoc->stream_locked) &&
12999 		    (asoc->stream_locked_on != srcv->sinfo_stream)) {
13000 			SCTP_TCB_SEND_UNLOCK(stcb);
13001 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13002 			error = EINVAL;
13003 			goto out;
13004 		}
13005 		SCTP_TCB_SEND_UNLOCK(stcb);
13006 
13007 		strm = &stcb->asoc.strmout[srcv->sinfo_stream];
13008 		if (strm->last_msg_incomplete == 0) {
13009 	do_a_copy_in:
13010 			sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
13011 			if ((sp == NULL) || (error)) {
13012 				goto out;
13013 			}
13014 			SCTP_TCB_SEND_LOCK(stcb);
13015 			if (sp->msg_is_complete) {
13016 				strm->last_msg_incomplete = 0;
13017 				asoc->stream_locked = 0;
13018 			} else {
13019 				/*
13020 				 * Just got locked to this guy in case of an
13021 				 * interrupt.
13022 				 */
13023 				strm->last_msg_incomplete = 1;
13024 				asoc->stream_locked = 1;
13025 				asoc->stream_locked_on = srcv->sinfo_stream;
13026 				sp->sender_all_done = 0;
13027 			}
13028 			sctp_snd_sb_alloc(stcb, sp->length);
13029 			atomic_add_int(&asoc->stream_queue_cnt, 1);
13030 			if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
13031 				sp->strseq = strm->next_sequence_sent;
13032 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
13033 					sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
13034 					    (uintptr_t) stcb, sp->length,
13035 					    (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
13036 				}
13037 				strm->next_sequence_sent++;
13038 			} else {
13039 				SCTP_STAT_INCR(sctps_sends_with_unord);
13040 			}
13041 			TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
13042 			stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp, 1);
13043 			SCTP_TCB_SEND_UNLOCK(stcb);
13044 		} else {
13045 			SCTP_TCB_SEND_LOCK(stcb);
13046 			sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
13047 			SCTP_TCB_SEND_UNLOCK(stcb);
13048 			if (sp == NULL) {
13049 				/* ???? Huh ??? last msg is gone */
13050 #ifdef INVARIANTS
13051 				panic("Warning: Last msg marked incomplete, yet nothing left?");
13052 #else
13053 				SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
13054 				strm->last_msg_incomplete = 0;
13055 #endif
13056 				goto do_a_copy_in;
13057 
13058 			}
13059 		}
13060 		while (uio->uio_resid > 0) {
13061 			/* How much room do we have? */
13062 			struct mbuf *new_tail, *mm;
13063 
13064 			if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13065 				max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
13066 			else
13067 				max_len = 0;
13068 
13069 			if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
13070 			    (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
13071 			    (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
13072 				sndout = 0;
13073 				new_tail = NULL;
13074 				if (hold_tcblock) {
13075 					SCTP_TCB_UNLOCK(stcb);
13076 					hold_tcblock = 0;
13077 				}
13078 				mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
13079 				if ((mm == NULL) || error) {
13080 					if (mm) {
13081 						sctp_m_freem(mm);
13082 					}
13083 					goto out;
13084 				}
13085 				/* Update the mbuf and count */
13086 				SCTP_TCB_SEND_LOCK(stcb);
13087 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13088 					/*
13089 					 * we need to get out. Peer probably
13090 					 * aborted.
13091 					 */
13092 					sctp_m_freem(mm);
13093 					if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
13094 						SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13095 						error = ECONNRESET;
13096 					}
13097 					SCTP_TCB_SEND_UNLOCK(stcb);
13098 					goto out;
13099 				}
13100 				if (sp->tail_mbuf) {
13101 					/* tack it to the end */
13102 					SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13103 					sp->tail_mbuf = new_tail;
13104 				} else {
13105 					/* A stolen mbuf */
13106 					sp->data = mm;
13107 					sp->tail_mbuf = new_tail;
13108 				}
13109 				sctp_snd_sb_alloc(stcb, sndout);
13110 				atomic_add_int(&sp->length, sndout);
13111 				len += sndout;
13112 
13113 				/* Did we reach EOR? */
13114 				if ((uio->uio_resid == 0) &&
13115 				    ((user_marks_eor == 0) ||
13116 				    (srcv->sinfo_flags & SCTP_EOF) ||
13117 				    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
13118 					sp->msg_is_complete = 1;
13119 				} else {
13120 					sp->msg_is_complete = 0;
13121 				}
13122 				SCTP_TCB_SEND_UNLOCK(stcb);
13123 			}
13124 			if (uio->uio_resid == 0) {
13125 				/* got it all? */
13126 				continue;
13127 			}
13128 			/* PR-SCTP? */
13129 			if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
13130 				/*
13131 				 * This is ugly but we must assure locking
13132 				 * order
13133 				 */
13134 				if (hold_tcblock == 0) {
13135 					SCTP_TCB_LOCK(stcb);
13136 					hold_tcblock = 1;
13137 				}
13138 				sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
13139 				inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13140 				if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13141 					max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13142 				else
13143 					max_len = 0;
13144 				if (max_len > 0) {
13145 					continue;
13146 				}
13147 				SCTP_TCB_UNLOCK(stcb);
13148 				hold_tcblock = 0;
13149 			}
13150 			/* wait for space now */
13151 			if (non_blocking) {
13152 				/* Non-blocking io in place out */
13153 				goto skip_out_eof;
13154 			}
13155 			/* What about the INIT, send it maybe */
13156 			if (queue_only_for_init) {
13157 				if (hold_tcblock == 0) {
13158 					SCTP_TCB_LOCK(stcb);
13159 					hold_tcblock = 1;
13160 				}
13161 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13162 					/* a collision took us forward? */
13163 					queue_only = 0;
13164 				} else {
13165 					sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13166 					SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
13167 					queue_only = 1;
13168 				}
13169 			}
13170 			if ((net->flight_size > net->cwnd) &&
13171 			    (asoc->sctp_cmt_on_off == 0)) {
13172 				SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13173 				queue_only = 1;
13174 			} else if (asoc->ifp_had_enobuf) {
13175 				SCTP_STAT_INCR(sctps_ifnomemqueued);
13176 				if (net->flight_size > (2 * net->mtu)) {
13177 					queue_only = 1;
13178 				}
13179 				asoc->ifp_had_enobuf = 0;
13180 			}
13181 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13182 			    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13183 			if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13184 			    (stcb->asoc.total_flight > 0) &&
13185 			    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13186 			    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13187 
13188 				/*-
13189 				 * Ok, Nagle is set on and we have data outstanding.
13190 				 * Don't send anything and let SACKs drive out the
13191 				 * data unless wen have a "full" segment to send.
13192 				 */
13193 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13194 					sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13195 				}
13196 				SCTP_STAT_INCR(sctps_naglequeued);
13197 				nagle_applies = 1;
13198 			} else {
13199 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13200 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13201 						sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13202 				}
13203 				SCTP_STAT_INCR(sctps_naglesent);
13204 				nagle_applies = 0;
13205 			}
13206 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13207 
13208 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13209 				    nagle_applies, un_sent);
13210 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13211 				    stcb->asoc.total_flight,
13212 				    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13213 			}
13214 			if (queue_only_for_init)
13215 				queue_only_for_init = 0;
13216 			if ((queue_only == 0) && (nagle_applies == 0)) {
13217 				/*-
13218 				 * need to start chunk output
13219 				 * before blocking.. note that if
13220 				 * a lock is already applied, then
13221 				 * the input via the net is happening
13222 				 * and I don't need to start output :-D
13223 				 */
13224 				if (hold_tcblock == 0) {
13225 					if (SCTP_TCB_TRYLOCK(stcb)) {
13226 						hold_tcblock = 1;
13227 						sctp_chunk_output(inp,
13228 						    stcb,
13229 						    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13230 					}
13231 				} else {
13232 					sctp_chunk_output(inp,
13233 					    stcb,
13234 					    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13235 				}
13236 				if (hold_tcblock == 1) {
13237 					SCTP_TCB_UNLOCK(stcb);
13238 					hold_tcblock = 0;
13239 				}
13240 			}
13241 			SOCKBUF_LOCK(&so->so_snd);
13242 			/*-
13243 			 * This is a bit strange, but I think it will
13244 			 * work. The total_output_queue_size is locked and
13245 			 * protected by the TCB_LOCK, which we just released.
13246 			 * There is a race that can occur between releasing it
13247 			 * above, and me getting the socket lock, where sacks
13248 			 * come in but we have not put the SB_WAIT on the
13249 			 * so_snd buffer to get the wakeup. After the LOCK
13250 			 * is applied the sack_processing will also need to
13251 			 * LOCK the so->so_snd to do the actual sowwakeup(). So
13252 			 * once we have the socket buffer lock if we recheck the
13253 			 * size we KNOW we will get to sleep safely with the
13254 			 * wakeup flag in place.
13255 			 */
13256 			if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
13257 			    min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13258 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13259 					sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13260 					    so, asoc, uio->uio_resid);
13261 				}
13262 				be.error = 0;
13263 				stcb->block_entry = &be;
13264 				error = sbwait(&so->so_snd);
13265 				stcb->block_entry = NULL;
13266 
13267 				if (error || so->so_error || be.error) {
13268 					if (error == 0) {
13269 						if (so->so_error)
13270 							error = so->so_error;
13271 						if (be.error) {
13272 							error = be.error;
13273 						}
13274 					}
13275 					SOCKBUF_UNLOCK(&so->so_snd);
13276 					goto out_unlocked;
13277 				}
13278 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13279 					sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13280 					    so, asoc, stcb->asoc.total_output_queue_size);
13281 				}
13282 			}
13283 			SOCKBUF_UNLOCK(&so->so_snd);
13284 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13285 				goto out_unlocked;
13286 			}
13287 		}
13288 		SCTP_TCB_SEND_LOCK(stcb);
13289 		if (sp) {
13290 			if (sp->msg_is_complete == 0) {
13291 				strm->last_msg_incomplete = 1;
13292 				asoc->stream_locked = 1;
13293 				asoc->stream_locked_on = srcv->sinfo_stream;
13294 			} else {
13295 				sp->sender_all_done = 1;
13296 				strm->last_msg_incomplete = 0;
13297 				asoc->stream_locked = 0;
13298 			}
13299 		} else {
13300 			SCTP_PRINTF("Huh no sp TSNH?\n");
13301 			strm->last_msg_incomplete = 0;
13302 			asoc->stream_locked = 0;
13303 		}
13304 		SCTP_TCB_SEND_UNLOCK(stcb);
13305 		if (uio->uio_resid == 0) {
13306 			got_all_of_the_send = 1;
13307 		}
13308 	} else {
13309 		/* We send in a 0, since we do NOT have any locks */
13310 		error = sctp_msg_append(stcb, net, top, srcv, 0);
13311 		top = NULL;
13312 		if (srcv->sinfo_flags & SCTP_EOF) {
13313 			/*
13314 			 * This should only happen for Panda for the mbuf
13315 			 * send case, which does NOT yet support EEOR mode.
13316 			 * Thus, we can just set this flag to do the proper
13317 			 * EOF handling.
13318 			 */
13319 			got_all_of_the_send = 1;
13320 		}
13321 	}
13322 	if (error) {
13323 		goto out;
13324 	}
13325 dataless_eof:
13326 	/* EOF thing ? */
13327 	if ((srcv->sinfo_flags & SCTP_EOF) &&
13328 	    (got_all_of_the_send == 1) &&
13329 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
13330 		int cnt;
13331 
13332 		SCTP_STAT_INCR(sctps_sends_with_eof);
13333 		error = 0;
13334 		if (hold_tcblock == 0) {
13335 			SCTP_TCB_LOCK(stcb);
13336 			hold_tcblock = 1;
13337 		}
13338 		cnt = sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED);
13339 		if (TAILQ_EMPTY(&asoc->send_queue) &&
13340 		    TAILQ_EMPTY(&asoc->sent_queue) &&
13341 		    (cnt == 0)) {
13342 			if (asoc->locked_on_sending) {
13343 				goto abort_anyway;
13344 			}
13345 			/* there is nothing queued to send, so I'm done... */
13346 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13347 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13348 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13349 				/* only send SHUTDOWN the first time through */
13350 				sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
13351 				if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
13352 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13353 				}
13354 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
13355 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
13356 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13357 				    asoc->primary_destination);
13358 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13359 				    asoc->primary_destination);
13360 			}
13361 		} else {
13362 			/*-
13363 			 * we still got (or just got) data to send, so set
13364 			 * SHUTDOWN_PENDING
13365 			 */
13366 			/*-
13367 			 * XXX sockets draft says that SCTP_EOF should be
13368 			 * sent with no data.  currently, we will allow user
13369 			 * data to be sent first and move to
13370 			 * SHUTDOWN-PENDING
13371 			 */
13372 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13373 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13374 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13375 				if (hold_tcblock == 0) {
13376 					SCTP_TCB_LOCK(stcb);
13377 					hold_tcblock = 1;
13378 				}
13379 				if (asoc->locked_on_sending) {
13380 					/* Locked to send out the data */
13381 					struct sctp_stream_queue_pending *sp;
13382 
13383 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
13384 					if (sp) {
13385 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
13386 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
13387 					}
13388 				}
13389 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
13390 				if (TAILQ_EMPTY(&asoc->send_queue) &&
13391 				    TAILQ_EMPTY(&asoc->sent_queue) &&
13392 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13393 			abort_anyway:
13394 					if (free_cnt_applied) {
13395 						atomic_add_int(&stcb->asoc.refcnt, -1);
13396 						free_cnt_applied = 0;
13397 					}
13398 					sctp_abort_an_association(stcb->sctp_ep, stcb,
13399 					    SCTP_RESPONSE_TO_USER_REQ,
13400 					    NULL, SCTP_SO_LOCKED);
13401 					/*
13402 					 * now relock the stcb so everything
13403 					 * is sane
13404 					 */
13405 					hold_tcblock = 0;
13406 					stcb = NULL;
13407 					goto out;
13408 				}
13409 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13410 				    asoc->primary_destination);
13411 				sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13412 			}
13413 		}
13414 	}
13415 skip_out_eof:
13416 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
13417 		some_on_control = 1;
13418 	}
13419 	if (queue_only_for_init) {
13420 		if (hold_tcblock == 0) {
13421 			SCTP_TCB_LOCK(stcb);
13422 			hold_tcblock = 1;
13423 		}
13424 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13425 			/* a collision took us forward? */
13426 			queue_only = 0;
13427 		} else {
13428 			sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13429 			SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13430 			queue_only = 1;
13431 		}
13432 	}
13433 	if ((net->flight_size > net->cwnd) &&
13434 	    (stcb->asoc.sctp_cmt_on_off == 0)) {
13435 		SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13436 		queue_only = 1;
13437 	} else if (asoc->ifp_had_enobuf) {
13438 		SCTP_STAT_INCR(sctps_ifnomemqueued);
13439 		if (net->flight_size > (2 * net->mtu)) {
13440 			queue_only = 1;
13441 		}
13442 		asoc->ifp_had_enobuf = 0;
13443 	}
13444 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13445 	    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13446 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13447 	    (stcb->asoc.total_flight > 0) &&
13448 	    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13449 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13450 		/*-
13451 		 * Ok, Nagle is set on and we have data outstanding.
13452 		 * Don't send anything and let SACKs drive out the
13453 		 * data unless wen have a "full" segment to send.
13454 		 */
13455 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13456 			sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13457 		}
13458 		SCTP_STAT_INCR(sctps_naglequeued);
13459 		nagle_applies = 1;
13460 	} else {
13461 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13462 			if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13463 				sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13464 		}
13465 		SCTP_STAT_INCR(sctps_naglesent);
13466 		nagle_applies = 0;
13467 	}
13468 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13469 		sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13470 		    nagle_applies, un_sent);
13471 		sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13472 		    stcb->asoc.total_flight,
13473 		    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13474 	}
13475 	if (queue_only_for_init)
13476 		queue_only_for_init = 0;
13477 	if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13478 		/* we can attempt to send too. */
13479 		if (hold_tcblock == 0) {
13480 			/*
13481 			 * If there is activity recv'ing sacks no need to
13482 			 * send
13483 			 */
13484 			if (SCTP_TCB_TRYLOCK(stcb)) {
13485 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13486 				hold_tcblock = 1;
13487 			}
13488 		} else {
13489 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13490 		}
13491 	} else if ((queue_only == 0) &&
13492 		    (stcb->asoc.peers_rwnd == 0) &&
13493 	    (stcb->asoc.total_flight == 0)) {
13494 		/* We get to have a probe outstanding */
13495 		if (hold_tcblock == 0) {
13496 			hold_tcblock = 1;
13497 			SCTP_TCB_LOCK(stcb);
13498 		}
13499 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13500 	} else if (some_on_control) {
13501 		int num_out, reason, frag_point;
13502 
13503 		/* Here we do control only */
13504 		if (hold_tcblock == 0) {
13505 			hold_tcblock = 1;
13506 			SCTP_TCB_LOCK(stcb);
13507 		}
13508 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13509 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13510 		    &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13511 	}
13512 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13513 	    queue_only, stcb->asoc.peers_rwnd, un_sent,
13514 	    stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13515 	    stcb->asoc.total_output_queue_size, error);
13516 
13517 out:
13518 out_unlocked:
13519 
13520 	if (local_soresv && stcb) {
13521 		atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13522 		local_soresv = 0;
13523 	}
13524 	if (create_lock_applied) {
13525 		SCTP_ASOC_CREATE_UNLOCK(inp);
13526 		create_lock_applied = 0;
13527 	}
13528 	if ((stcb) && hold_tcblock) {
13529 		SCTP_TCB_UNLOCK(stcb);
13530 	}
13531 	if (stcb && free_cnt_applied) {
13532 		atomic_add_int(&stcb->asoc.refcnt, -1);
13533 	}
13534 #ifdef INVARIANTS
13535 	if (stcb) {
13536 		if (mtx_owned(&stcb->tcb_mtx)) {
13537 			panic("Leaving with tcb mtx owned?");
13538 		}
13539 		if (mtx_owned(&stcb->tcb_send_mtx)) {
13540 			panic("Leaving with tcb send mtx owned?");
13541 		}
13542 	}
13543 #endif
13544 #ifdef INVARIANTS
13545 	if (inp) {
13546 		sctp_validate_no_locks(inp);
13547 	} else {
13548 		printf("Warning - inp is NULL so cant validate locks\n");
13549 	}
13550 #endif
13551 	if (top) {
13552 		sctp_m_freem(top);
13553 	}
13554 	if (control) {
13555 		sctp_m_freem(control);
13556 	}
13557 	return (error);
13558 }
13559 
13560 
13561 /*
13562  * generate an AUTHentication chunk, if required
13563  */
13564 struct mbuf *
13565 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13566     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13567     struct sctp_tcb *stcb, uint8_t chunk)
13568 {
13569 	struct mbuf *m_auth;
13570 	struct sctp_auth_chunk *auth;
13571 	int chunk_len;
13572 
13573 	if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13574 	    (stcb == NULL))
13575 		return (m);
13576 
13577 	/* sysctl disabled auth? */
13578 	if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13579 		return (m);
13580 
13581 	/* peer doesn't do auth... */
13582 	if (!stcb->asoc.peer_supports_auth) {
13583 		return (m);
13584 	}
13585 	/* does the requested chunk require auth? */
13586 	if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13587 		return (m);
13588 	}
13589 	m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13590 	if (m_auth == NULL) {
13591 		/* no mbuf's */
13592 		return (m);
13593 	}
13594 	/* reserve some space if this will be the first mbuf */
13595 	if (m == NULL)
13596 		SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13597 	/* fill in the AUTH chunk details */
13598 	auth = mtod(m_auth, struct sctp_auth_chunk *);
13599 	bzero(auth, sizeof(*auth));
13600 	auth->ch.chunk_type = SCTP_AUTHENTICATION;
13601 	auth->ch.chunk_flags = 0;
13602 	chunk_len = sizeof(*auth) +
13603 	    sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13604 	auth->ch.chunk_length = htons(chunk_len);
13605 	auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13606 	/* key id and hmac digest will be computed and filled in upon send */
13607 
13608 	/* save the offset where the auth was inserted into the chain */
13609 	if (m != NULL) {
13610 		struct mbuf *cn;
13611 
13612 		*offset = 0;
13613 		cn = m;
13614 		while (cn) {
13615 			*offset += SCTP_BUF_LEN(cn);
13616 			cn = SCTP_BUF_NEXT(cn);
13617 		}
13618 	} else
13619 		*offset = 0;
13620 
13621 	/* update length and return pointer to the auth chunk */
13622 	SCTP_BUF_LEN(m_auth) = chunk_len;
13623 	m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13624 	if (auth_ret != NULL)
13625 		*auth_ret = auth;
13626 
13627 	return (m);
13628 }
13629 
13630 #ifdef INET6
13631 int
13632 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13633 {
13634 	struct nd_prefix *pfx = NULL;
13635 	struct nd_pfxrouter *pfxrtr = NULL;
13636 	struct sockaddr_in6 gw6;
13637 
13638 	if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13639 		return (0);
13640 
13641 	/* get prefix entry of address */
13642 	LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13643 		if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13644 			continue;
13645 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13646 		    &src6->sin6_addr, &pfx->ndpr_mask))
13647 			break;
13648 	}
13649 	/* no prefix entry in the prefix list */
13650 	if (pfx == NULL) {
13651 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13652 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13653 		return (0);
13654 	}
13655 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13656 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13657 
13658 	/* search installed gateway from prefix entry */
13659 	for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
13660 	    pfxrtr->pfr_next) {
13661 		memset(&gw6, 0, sizeof(struct sockaddr_in6));
13662 		gw6.sin6_family = AF_INET6;
13663 		gw6.sin6_len = sizeof(struct sockaddr_in6);
13664 		memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13665 		    sizeof(struct in6_addr));
13666 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13667 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13668 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13669 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13670 		if (sctp_cmpaddr((struct sockaddr *)&gw6,
13671 		    ro->ro_rt->rt_gateway)) {
13672 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13673 			return (1);
13674 		}
13675 	}
13676 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13677 	return (0);
13678 }
13679 
13680 #endif
13681 
13682 int
13683 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13684 {
13685 #ifdef INET
13686 	struct sockaddr_in *sin, *mask;
13687 	struct ifaddr *ifa;
13688 	struct in_addr srcnetaddr, gwnetaddr;
13689 
13690 	if (ro == NULL || ro->ro_rt == NULL ||
13691 	    sifa->address.sa.sa_family != AF_INET) {
13692 		return (0);
13693 	}
13694 	ifa = (struct ifaddr *)sifa->ifa;
13695 	mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13696 	sin = (struct sockaddr_in *)&sifa->address.sin;
13697 	srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13698 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13699 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13700 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13701 
13702 	sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13703 	gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13704 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13705 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13706 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13707 	if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13708 		return (1);
13709 	}
13710 #endif
13711 	return (0);
13712 }
13713