xref: /freebsd/sys/netinet/sctp_output.c (revision 3ef51c5fb9163f2aafb1c14729e06a8bf0c4d113)
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 <netinet/udp_var.h>
57 #include <machine/in_cksum.h>
58 
59 
60 
61 #define SCTP_MAX_GAPS_INARRAY 4
62 struct sack_track {
63 	uint8_t right_edge;	/* mergable on the right edge */
64 	uint8_t left_edge;	/* mergable on the left edge */
65 	uint8_t num_entries;
66 	uint8_t spare;
67 	struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
68 };
69 
70 struct sack_track sack_array[256] = {
71 	{0, 0, 0, 0,		/* 0x00 */
72 		{{0, 0},
73 		{0, 0},
74 		{0, 0},
75 		{0, 0}
76 		}
77 	},
78 	{1, 0, 1, 0,		/* 0x01 */
79 		{{0, 0},
80 		{0, 0},
81 		{0, 0},
82 		{0, 0}
83 		}
84 	},
85 	{0, 0, 1, 0,		/* 0x02 */
86 		{{1, 1},
87 		{0, 0},
88 		{0, 0},
89 		{0, 0}
90 		}
91 	},
92 	{1, 0, 1, 0,		/* 0x03 */
93 		{{0, 1},
94 		{0, 0},
95 		{0, 0},
96 		{0, 0}
97 		}
98 	},
99 	{0, 0, 1, 0,		/* 0x04 */
100 		{{2, 2},
101 		{0, 0},
102 		{0, 0},
103 		{0, 0}
104 		}
105 	},
106 	{1, 0, 2, 0,		/* 0x05 */
107 		{{0, 0},
108 		{2, 2},
109 		{0, 0},
110 		{0, 0}
111 		}
112 	},
113 	{0, 0, 1, 0,		/* 0x06 */
114 		{{1, 2},
115 		{0, 0},
116 		{0, 0},
117 		{0, 0}
118 		}
119 	},
120 	{1, 0, 1, 0,		/* 0x07 */
121 		{{0, 2},
122 		{0, 0},
123 		{0, 0},
124 		{0, 0}
125 		}
126 	},
127 	{0, 0, 1, 0,		/* 0x08 */
128 		{{3, 3},
129 		{0, 0},
130 		{0, 0},
131 		{0, 0}
132 		}
133 	},
134 	{1, 0, 2, 0,		/* 0x09 */
135 		{{0, 0},
136 		{3, 3},
137 		{0, 0},
138 		{0, 0}
139 		}
140 	},
141 	{0, 0, 2, 0,		/* 0x0a */
142 		{{1, 1},
143 		{3, 3},
144 		{0, 0},
145 		{0, 0}
146 		}
147 	},
148 	{1, 0, 2, 0,		/* 0x0b */
149 		{{0, 1},
150 		{3, 3},
151 		{0, 0},
152 		{0, 0}
153 		}
154 	},
155 	{0, 0, 1, 0,		/* 0x0c */
156 		{{2, 3},
157 		{0, 0},
158 		{0, 0},
159 		{0, 0}
160 		}
161 	},
162 	{1, 0, 2, 0,		/* 0x0d */
163 		{{0, 0},
164 		{2, 3},
165 		{0, 0},
166 		{0, 0}
167 		}
168 	},
169 	{0, 0, 1, 0,		/* 0x0e */
170 		{{1, 3},
171 		{0, 0},
172 		{0, 0},
173 		{0, 0}
174 		}
175 	},
176 	{1, 0, 1, 0,		/* 0x0f */
177 		{{0, 3},
178 		{0, 0},
179 		{0, 0},
180 		{0, 0}
181 		}
182 	},
183 	{0, 0, 1, 0,		/* 0x10 */
184 		{{4, 4},
185 		{0, 0},
186 		{0, 0},
187 		{0, 0}
188 		}
189 	},
190 	{1, 0, 2, 0,		/* 0x11 */
191 		{{0, 0},
192 		{4, 4},
193 		{0, 0},
194 		{0, 0}
195 		}
196 	},
197 	{0, 0, 2, 0,		/* 0x12 */
198 		{{1, 1},
199 		{4, 4},
200 		{0, 0},
201 		{0, 0}
202 		}
203 	},
204 	{1, 0, 2, 0,		/* 0x13 */
205 		{{0, 1},
206 		{4, 4},
207 		{0, 0},
208 		{0, 0}
209 		}
210 	},
211 	{0, 0, 2, 0,		/* 0x14 */
212 		{{2, 2},
213 		{4, 4},
214 		{0, 0},
215 		{0, 0}
216 		}
217 	},
218 	{1, 0, 3, 0,		/* 0x15 */
219 		{{0, 0},
220 		{2, 2},
221 		{4, 4},
222 		{0, 0}
223 		}
224 	},
225 	{0, 0, 2, 0,		/* 0x16 */
226 		{{1, 2},
227 		{4, 4},
228 		{0, 0},
229 		{0, 0}
230 		}
231 	},
232 	{1, 0, 2, 0,		/* 0x17 */
233 		{{0, 2},
234 		{4, 4},
235 		{0, 0},
236 		{0, 0}
237 		}
238 	},
239 	{0, 0, 1, 0,		/* 0x18 */
240 		{{3, 4},
241 		{0, 0},
242 		{0, 0},
243 		{0, 0}
244 		}
245 	},
246 	{1, 0, 2, 0,		/* 0x19 */
247 		{{0, 0},
248 		{3, 4},
249 		{0, 0},
250 		{0, 0}
251 		}
252 	},
253 	{0, 0, 2, 0,		/* 0x1a */
254 		{{1, 1},
255 		{3, 4},
256 		{0, 0},
257 		{0, 0}
258 		}
259 	},
260 	{1, 0, 2, 0,		/* 0x1b */
261 		{{0, 1},
262 		{3, 4},
263 		{0, 0},
264 		{0, 0}
265 		}
266 	},
267 	{0, 0, 1, 0,		/* 0x1c */
268 		{{2, 4},
269 		{0, 0},
270 		{0, 0},
271 		{0, 0}
272 		}
273 	},
274 	{1, 0, 2, 0,		/* 0x1d */
275 		{{0, 0},
276 		{2, 4},
277 		{0, 0},
278 		{0, 0}
279 		}
280 	},
281 	{0, 0, 1, 0,		/* 0x1e */
282 		{{1, 4},
283 		{0, 0},
284 		{0, 0},
285 		{0, 0}
286 		}
287 	},
288 	{1, 0, 1, 0,		/* 0x1f */
289 		{{0, 4},
290 		{0, 0},
291 		{0, 0},
292 		{0, 0}
293 		}
294 	},
295 	{0, 0, 1, 0,		/* 0x20 */
296 		{{5, 5},
297 		{0, 0},
298 		{0, 0},
299 		{0, 0}
300 		}
301 	},
302 	{1, 0, 2, 0,		/* 0x21 */
303 		{{0, 0},
304 		{5, 5},
305 		{0, 0},
306 		{0, 0}
307 		}
308 	},
309 	{0, 0, 2, 0,		/* 0x22 */
310 		{{1, 1},
311 		{5, 5},
312 		{0, 0},
313 		{0, 0}
314 		}
315 	},
316 	{1, 0, 2, 0,		/* 0x23 */
317 		{{0, 1},
318 		{5, 5},
319 		{0, 0},
320 		{0, 0}
321 		}
322 	},
323 	{0, 0, 2, 0,		/* 0x24 */
324 		{{2, 2},
325 		{5, 5},
326 		{0, 0},
327 		{0, 0}
328 		}
329 	},
330 	{1, 0, 3, 0,		/* 0x25 */
331 		{{0, 0},
332 		{2, 2},
333 		{5, 5},
334 		{0, 0}
335 		}
336 	},
337 	{0, 0, 2, 0,		/* 0x26 */
338 		{{1, 2},
339 		{5, 5},
340 		{0, 0},
341 		{0, 0}
342 		}
343 	},
344 	{1, 0, 2, 0,		/* 0x27 */
345 		{{0, 2},
346 		{5, 5},
347 		{0, 0},
348 		{0, 0}
349 		}
350 	},
351 	{0, 0, 2, 0,		/* 0x28 */
352 		{{3, 3},
353 		{5, 5},
354 		{0, 0},
355 		{0, 0}
356 		}
357 	},
358 	{1, 0, 3, 0,		/* 0x29 */
359 		{{0, 0},
360 		{3, 3},
361 		{5, 5},
362 		{0, 0}
363 		}
364 	},
365 	{0, 0, 3, 0,		/* 0x2a */
366 		{{1, 1},
367 		{3, 3},
368 		{5, 5},
369 		{0, 0}
370 		}
371 	},
372 	{1, 0, 3, 0,		/* 0x2b */
373 		{{0, 1},
374 		{3, 3},
375 		{5, 5},
376 		{0, 0}
377 		}
378 	},
379 	{0, 0, 2, 0,		/* 0x2c */
380 		{{2, 3},
381 		{5, 5},
382 		{0, 0},
383 		{0, 0}
384 		}
385 	},
386 	{1, 0, 3, 0,		/* 0x2d */
387 		{{0, 0},
388 		{2, 3},
389 		{5, 5},
390 		{0, 0}
391 		}
392 	},
393 	{0, 0, 2, 0,		/* 0x2e */
394 		{{1, 3},
395 		{5, 5},
396 		{0, 0},
397 		{0, 0}
398 		}
399 	},
400 	{1, 0, 2, 0,		/* 0x2f */
401 		{{0, 3},
402 		{5, 5},
403 		{0, 0},
404 		{0, 0}
405 		}
406 	},
407 	{0, 0, 1, 0,		/* 0x30 */
408 		{{4, 5},
409 		{0, 0},
410 		{0, 0},
411 		{0, 0}
412 		}
413 	},
414 	{1, 0, 2, 0,		/* 0x31 */
415 		{{0, 0},
416 		{4, 5},
417 		{0, 0},
418 		{0, 0}
419 		}
420 	},
421 	{0, 0, 2, 0,		/* 0x32 */
422 		{{1, 1},
423 		{4, 5},
424 		{0, 0},
425 		{0, 0}
426 		}
427 	},
428 	{1, 0, 2, 0,		/* 0x33 */
429 		{{0, 1},
430 		{4, 5},
431 		{0, 0},
432 		{0, 0}
433 		}
434 	},
435 	{0, 0, 2, 0,		/* 0x34 */
436 		{{2, 2},
437 		{4, 5},
438 		{0, 0},
439 		{0, 0}
440 		}
441 	},
442 	{1, 0, 3, 0,		/* 0x35 */
443 		{{0, 0},
444 		{2, 2},
445 		{4, 5},
446 		{0, 0}
447 		}
448 	},
449 	{0, 0, 2, 0,		/* 0x36 */
450 		{{1, 2},
451 		{4, 5},
452 		{0, 0},
453 		{0, 0}
454 		}
455 	},
456 	{1, 0, 2, 0,		/* 0x37 */
457 		{{0, 2},
458 		{4, 5},
459 		{0, 0},
460 		{0, 0}
461 		}
462 	},
463 	{0, 0, 1, 0,		/* 0x38 */
464 		{{3, 5},
465 		{0, 0},
466 		{0, 0},
467 		{0, 0}
468 		}
469 	},
470 	{1, 0, 2, 0,		/* 0x39 */
471 		{{0, 0},
472 		{3, 5},
473 		{0, 0},
474 		{0, 0}
475 		}
476 	},
477 	{0, 0, 2, 0,		/* 0x3a */
478 		{{1, 1},
479 		{3, 5},
480 		{0, 0},
481 		{0, 0}
482 		}
483 	},
484 	{1, 0, 2, 0,		/* 0x3b */
485 		{{0, 1},
486 		{3, 5},
487 		{0, 0},
488 		{0, 0}
489 		}
490 	},
491 	{0, 0, 1, 0,		/* 0x3c */
492 		{{2, 5},
493 		{0, 0},
494 		{0, 0},
495 		{0, 0}
496 		}
497 	},
498 	{1, 0, 2, 0,		/* 0x3d */
499 		{{0, 0},
500 		{2, 5},
501 		{0, 0},
502 		{0, 0}
503 		}
504 	},
505 	{0, 0, 1, 0,		/* 0x3e */
506 		{{1, 5},
507 		{0, 0},
508 		{0, 0},
509 		{0, 0}
510 		}
511 	},
512 	{1, 0, 1, 0,		/* 0x3f */
513 		{{0, 5},
514 		{0, 0},
515 		{0, 0},
516 		{0, 0}
517 		}
518 	},
519 	{0, 0, 1, 0,		/* 0x40 */
520 		{{6, 6},
521 		{0, 0},
522 		{0, 0},
523 		{0, 0}
524 		}
525 	},
526 	{1, 0, 2, 0,		/* 0x41 */
527 		{{0, 0},
528 		{6, 6},
529 		{0, 0},
530 		{0, 0}
531 		}
532 	},
533 	{0, 0, 2, 0,		/* 0x42 */
534 		{{1, 1},
535 		{6, 6},
536 		{0, 0},
537 		{0, 0}
538 		}
539 	},
540 	{1, 0, 2, 0,		/* 0x43 */
541 		{{0, 1},
542 		{6, 6},
543 		{0, 0},
544 		{0, 0}
545 		}
546 	},
547 	{0, 0, 2, 0,		/* 0x44 */
548 		{{2, 2},
549 		{6, 6},
550 		{0, 0},
551 		{0, 0}
552 		}
553 	},
554 	{1, 0, 3, 0,		/* 0x45 */
555 		{{0, 0},
556 		{2, 2},
557 		{6, 6},
558 		{0, 0}
559 		}
560 	},
561 	{0, 0, 2, 0,		/* 0x46 */
562 		{{1, 2},
563 		{6, 6},
564 		{0, 0},
565 		{0, 0}
566 		}
567 	},
568 	{1, 0, 2, 0,		/* 0x47 */
569 		{{0, 2},
570 		{6, 6},
571 		{0, 0},
572 		{0, 0}
573 		}
574 	},
575 	{0, 0, 2, 0,		/* 0x48 */
576 		{{3, 3},
577 		{6, 6},
578 		{0, 0},
579 		{0, 0}
580 		}
581 	},
582 	{1, 0, 3, 0,		/* 0x49 */
583 		{{0, 0},
584 		{3, 3},
585 		{6, 6},
586 		{0, 0}
587 		}
588 	},
589 	{0, 0, 3, 0,		/* 0x4a */
590 		{{1, 1},
591 		{3, 3},
592 		{6, 6},
593 		{0, 0}
594 		}
595 	},
596 	{1, 0, 3, 0,		/* 0x4b */
597 		{{0, 1},
598 		{3, 3},
599 		{6, 6},
600 		{0, 0}
601 		}
602 	},
603 	{0, 0, 2, 0,		/* 0x4c */
604 		{{2, 3},
605 		{6, 6},
606 		{0, 0},
607 		{0, 0}
608 		}
609 	},
610 	{1, 0, 3, 0,		/* 0x4d */
611 		{{0, 0},
612 		{2, 3},
613 		{6, 6},
614 		{0, 0}
615 		}
616 	},
617 	{0, 0, 2, 0,		/* 0x4e */
618 		{{1, 3},
619 		{6, 6},
620 		{0, 0},
621 		{0, 0}
622 		}
623 	},
624 	{1, 0, 2, 0,		/* 0x4f */
625 		{{0, 3},
626 		{6, 6},
627 		{0, 0},
628 		{0, 0}
629 		}
630 	},
631 	{0, 0, 2, 0,		/* 0x50 */
632 		{{4, 4},
633 		{6, 6},
634 		{0, 0},
635 		{0, 0}
636 		}
637 	},
638 	{1, 0, 3, 0,		/* 0x51 */
639 		{{0, 0},
640 		{4, 4},
641 		{6, 6},
642 		{0, 0}
643 		}
644 	},
645 	{0, 0, 3, 0,		/* 0x52 */
646 		{{1, 1},
647 		{4, 4},
648 		{6, 6},
649 		{0, 0}
650 		}
651 	},
652 	{1, 0, 3, 0,		/* 0x53 */
653 		{{0, 1},
654 		{4, 4},
655 		{6, 6},
656 		{0, 0}
657 		}
658 	},
659 	{0, 0, 3, 0,		/* 0x54 */
660 		{{2, 2},
661 		{4, 4},
662 		{6, 6},
663 		{0, 0}
664 		}
665 	},
666 	{1, 0, 4, 0,		/* 0x55 */
667 		{{0, 0},
668 		{2, 2},
669 		{4, 4},
670 		{6, 6}
671 		}
672 	},
673 	{0, 0, 3, 0,		/* 0x56 */
674 		{{1, 2},
675 		{4, 4},
676 		{6, 6},
677 		{0, 0}
678 		}
679 	},
680 	{1, 0, 3, 0,		/* 0x57 */
681 		{{0, 2},
682 		{4, 4},
683 		{6, 6},
684 		{0, 0}
685 		}
686 	},
687 	{0, 0, 2, 0,		/* 0x58 */
688 		{{3, 4},
689 		{6, 6},
690 		{0, 0},
691 		{0, 0}
692 		}
693 	},
694 	{1, 0, 3, 0,		/* 0x59 */
695 		{{0, 0},
696 		{3, 4},
697 		{6, 6},
698 		{0, 0}
699 		}
700 	},
701 	{0, 0, 3, 0,		/* 0x5a */
702 		{{1, 1},
703 		{3, 4},
704 		{6, 6},
705 		{0, 0}
706 		}
707 	},
708 	{1, 0, 3, 0,		/* 0x5b */
709 		{{0, 1},
710 		{3, 4},
711 		{6, 6},
712 		{0, 0}
713 		}
714 	},
715 	{0, 0, 2, 0,		/* 0x5c */
716 		{{2, 4},
717 		{6, 6},
718 		{0, 0},
719 		{0, 0}
720 		}
721 	},
722 	{1, 0, 3, 0,		/* 0x5d */
723 		{{0, 0},
724 		{2, 4},
725 		{6, 6},
726 		{0, 0}
727 		}
728 	},
729 	{0, 0, 2, 0,		/* 0x5e */
730 		{{1, 4},
731 		{6, 6},
732 		{0, 0},
733 		{0, 0}
734 		}
735 	},
736 	{1, 0, 2, 0,		/* 0x5f */
737 		{{0, 4},
738 		{6, 6},
739 		{0, 0},
740 		{0, 0}
741 		}
742 	},
743 	{0, 0, 1, 0,		/* 0x60 */
744 		{{5, 6},
745 		{0, 0},
746 		{0, 0},
747 		{0, 0}
748 		}
749 	},
750 	{1, 0, 2, 0,		/* 0x61 */
751 		{{0, 0},
752 		{5, 6},
753 		{0, 0},
754 		{0, 0}
755 		}
756 	},
757 	{0, 0, 2, 0,		/* 0x62 */
758 		{{1, 1},
759 		{5, 6},
760 		{0, 0},
761 		{0, 0}
762 		}
763 	},
764 	{1, 0, 2, 0,		/* 0x63 */
765 		{{0, 1},
766 		{5, 6},
767 		{0, 0},
768 		{0, 0}
769 		}
770 	},
771 	{0, 0, 2, 0,		/* 0x64 */
772 		{{2, 2},
773 		{5, 6},
774 		{0, 0},
775 		{0, 0}
776 		}
777 	},
778 	{1, 0, 3, 0,		/* 0x65 */
779 		{{0, 0},
780 		{2, 2},
781 		{5, 6},
782 		{0, 0}
783 		}
784 	},
785 	{0, 0, 2, 0,		/* 0x66 */
786 		{{1, 2},
787 		{5, 6},
788 		{0, 0},
789 		{0, 0}
790 		}
791 	},
792 	{1, 0, 2, 0,		/* 0x67 */
793 		{{0, 2},
794 		{5, 6},
795 		{0, 0},
796 		{0, 0}
797 		}
798 	},
799 	{0, 0, 2, 0,		/* 0x68 */
800 		{{3, 3},
801 		{5, 6},
802 		{0, 0},
803 		{0, 0}
804 		}
805 	},
806 	{1, 0, 3, 0,		/* 0x69 */
807 		{{0, 0},
808 		{3, 3},
809 		{5, 6},
810 		{0, 0}
811 		}
812 	},
813 	{0, 0, 3, 0,		/* 0x6a */
814 		{{1, 1},
815 		{3, 3},
816 		{5, 6},
817 		{0, 0}
818 		}
819 	},
820 	{1, 0, 3, 0,		/* 0x6b */
821 		{{0, 1},
822 		{3, 3},
823 		{5, 6},
824 		{0, 0}
825 		}
826 	},
827 	{0, 0, 2, 0,		/* 0x6c */
828 		{{2, 3},
829 		{5, 6},
830 		{0, 0},
831 		{0, 0}
832 		}
833 	},
834 	{1, 0, 3, 0,		/* 0x6d */
835 		{{0, 0},
836 		{2, 3},
837 		{5, 6},
838 		{0, 0}
839 		}
840 	},
841 	{0, 0, 2, 0,		/* 0x6e */
842 		{{1, 3},
843 		{5, 6},
844 		{0, 0},
845 		{0, 0}
846 		}
847 	},
848 	{1, 0, 2, 0,		/* 0x6f */
849 		{{0, 3},
850 		{5, 6},
851 		{0, 0},
852 		{0, 0}
853 		}
854 	},
855 	{0, 0, 1, 0,		/* 0x70 */
856 		{{4, 6},
857 		{0, 0},
858 		{0, 0},
859 		{0, 0}
860 		}
861 	},
862 	{1, 0, 2, 0,		/* 0x71 */
863 		{{0, 0},
864 		{4, 6},
865 		{0, 0},
866 		{0, 0}
867 		}
868 	},
869 	{0, 0, 2, 0,		/* 0x72 */
870 		{{1, 1},
871 		{4, 6},
872 		{0, 0},
873 		{0, 0}
874 		}
875 	},
876 	{1, 0, 2, 0,		/* 0x73 */
877 		{{0, 1},
878 		{4, 6},
879 		{0, 0},
880 		{0, 0}
881 		}
882 	},
883 	{0, 0, 2, 0,		/* 0x74 */
884 		{{2, 2},
885 		{4, 6},
886 		{0, 0},
887 		{0, 0}
888 		}
889 	},
890 	{1, 0, 3, 0,		/* 0x75 */
891 		{{0, 0},
892 		{2, 2},
893 		{4, 6},
894 		{0, 0}
895 		}
896 	},
897 	{0, 0, 2, 0,		/* 0x76 */
898 		{{1, 2},
899 		{4, 6},
900 		{0, 0},
901 		{0, 0}
902 		}
903 	},
904 	{1, 0, 2, 0,		/* 0x77 */
905 		{{0, 2},
906 		{4, 6},
907 		{0, 0},
908 		{0, 0}
909 		}
910 	},
911 	{0, 0, 1, 0,		/* 0x78 */
912 		{{3, 6},
913 		{0, 0},
914 		{0, 0},
915 		{0, 0}
916 		}
917 	},
918 	{1, 0, 2, 0,		/* 0x79 */
919 		{{0, 0},
920 		{3, 6},
921 		{0, 0},
922 		{0, 0}
923 		}
924 	},
925 	{0, 0, 2, 0,		/* 0x7a */
926 		{{1, 1},
927 		{3, 6},
928 		{0, 0},
929 		{0, 0}
930 		}
931 	},
932 	{1, 0, 2, 0,		/* 0x7b */
933 		{{0, 1},
934 		{3, 6},
935 		{0, 0},
936 		{0, 0}
937 		}
938 	},
939 	{0, 0, 1, 0,		/* 0x7c */
940 		{{2, 6},
941 		{0, 0},
942 		{0, 0},
943 		{0, 0}
944 		}
945 	},
946 	{1, 0, 2, 0,		/* 0x7d */
947 		{{0, 0},
948 		{2, 6},
949 		{0, 0},
950 		{0, 0}
951 		}
952 	},
953 	{0, 0, 1, 0,		/* 0x7e */
954 		{{1, 6},
955 		{0, 0},
956 		{0, 0},
957 		{0, 0}
958 		}
959 	},
960 	{1, 0, 1, 0,		/* 0x7f */
961 		{{0, 6},
962 		{0, 0},
963 		{0, 0},
964 		{0, 0}
965 		}
966 	},
967 	{0, 1, 1, 0,		/* 0x80 */
968 		{{7, 7},
969 		{0, 0},
970 		{0, 0},
971 		{0, 0}
972 		}
973 	},
974 	{1, 1, 2, 0,		/* 0x81 */
975 		{{0, 0},
976 		{7, 7},
977 		{0, 0},
978 		{0, 0}
979 		}
980 	},
981 	{0, 1, 2, 0,		/* 0x82 */
982 		{{1, 1},
983 		{7, 7},
984 		{0, 0},
985 		{0, 0}
986 		}
987 	},
988 	{1, 1, 2, 0,		/* 0x83 */
989 		{{0, 1},
990 		{7, 7},
991 		{0, 0},
992 		{0, 0}
993 		}
994 	},
995 	{0, 1, 2, 0,		/* 0x84 */
996 		{{2, 2},
997 		{7, 7},
998 		{0, 0},
999 		{0, 0}
1000 		}
1001 	},
1002 	{1, 1, 3, 0,		/* 0x85 */
1003 		{{0, 0},
1004 		{2, 2},
1005 		{7, 7},
1006 		{0, 0}
1007 		}
1008 	},
1009 	{0, 1, 2, 0,		/* 0x86 */
1010 		{{1, 2},
1011 		{7, 7},
1012 		{0, 0},
1013 		{0, 0}
1014 		}
1015 	},
1016 	{1, 1, 2, 0,		/* 0x87 */
1017 		{{0, 2},
1018 		{7, 7},
1019 		{0, 0},
1020 		{0, 0}
1021 		}
1022 	},
1023 	{0, 1, 2, 0,		/* 0x88 */
1024 		{{3, 3},
1025 		{7, 7},
1026 		{0, 0},
1027 		{0, 0}
1028 		}
1029 	},
1030 	{1, 1, 3, 0,		/* 0x89 */
1031 		{{0, 0},
1032 		{3, 3},
1033 		{7, 7},
1034 		{0, 0}
1035 		}
1036 	},
1037 	{0, 1, 3, 0,		/* 0x8a */
1038 		{{1, 1},
1039 		{3, 3},
1040 		{7, 7},
1041 		{0, 0}
1042 		}
1043 	},
1044 	{1, 1, 3, 0,		/* 0x8b */
1045 		{{0, 1},
1046 		{3, 3},
1047 		{7, 7},
1048 		{0, 0}
1049 		}
1050 	},
1051 	{0, 1, 2, 0,		/* 0x8c */
1052 		{{2, 3},
1053 		{7, 7},
1054 		{0, 0},
1055 		{0, 0}
1056 		}
1057 	},
1058 	{1, 1, 3, 0,		/* 0x8d */
1059 		{{0, 0},
1060 		{2, 3},
1061 		{7, 7},
1062 		{0, 0}
1063 		}
1064 	},
1065 	{0, 1, 2, 0,		/* 0x8e */
1066 		{{1, 3},
1067 		{7, 7},
1068 		{0, 0},
1069 		{0, 0}
1070 		}
1071 	},
1072 	{1, 1, 2, 0,		/* 0x8f */
1073 		{{0, 3},
1074 		{7, 7},
1075 		{0, 0},
1076 		{0, 0}
1077 		}
1078 	},
1079 	{0, 1, 2, 0,		/* 0x90 */
1080 		{{4, 4},
1081 		{7, 7},
1082 		{0, 0},
1083 		{0, 0}
1084 		}
1085 	},
1086 	{1, 1, 3, 0,		/* 0x91 */
1087 		{{0, 0},
1088 		{4, 4},
1089 		{7, 7},
1090 		{0, 0}
1091 		}
1092 	},
1093 	{0, 1, 3, 0,		/* 0x92 */
1094 		{{1, 1},
1095 		{4, 4},
1096 		{7, 7},
1097 		{0, 0}
1098 		}
1099 	},
1100 	{1, 1, 3, 0,		/* 0x93 */
1101 		{{0, 1},
1102 		{4, 4},
1103 		{7, 7},
1104 		{0, 0}
1105 		}
1106 	},
1107 	{0, 1, 3, 0,		/* 0x94 */
1108 		{{2, 2},
1109 		{4, 4},
1110 		{7, 7},
1111 		{0, 0}
1112 		}
1113 	},
1114 	{1, 1, 4, 0,		/* 0x95 */
1115 		{{0, 0},
1116 		{2, 2},
1117 		{4, 4},
1118 		{7, 7}
1119 		}
1120 	},
1121 	{0, 1, 3, 0,		/* 0x96 */
1122 		{{1, 2},
1123 		{4, 4},
1124 		{7, 7},
1125 		{0, 0}
1126 		}
1127 	},
1128 	{1, 1, 3, 0,		/* 0x97 */
1129 		{{0, 2},
1130 		{4, 4},
1131 		{7, 7},
1132 		{0, 0}
1133 		}
1134 	},
1135 	{0, 1, 2, 0,		/* 0x98 */
1136 		{{3, 4},
1137 		{7, 7},
1138 		{0, 0},
1139 		{0, 0}
1140 		}
1141 	},
1142 	{1, 1, 3, 0,		/* 0x99 */
1143 		{{0, 0},
1144 		{3, 4},
1145 		{7, 7},
1146 		{0, 0}
1147 		}
1148 	},
1149 	{0, 1, 3, 0,		/* 0x9a */
1150 		{{1, 1},
1151 		{3, 4},
1152 		{7, 7},
1153 		{0, 0}
1154 		}
1155 	},
1156 	{1, 1, 3, 0,		/* 0x9b */
1157 		{{0, 1},
1158 		{3, 4},
1159 		{7, 7},
1160 		{0, 0}
1161 		}
1162 	},
1163 	{0, 1, 2, 0,		/* 0x9c */
1164 		{{2, 4},
1165 		{7, 7},
1166 		{0, 0},
1167 		{0, 0}
1168 		}
1169 	},
1170 	{1, 1, 3, 0,		/* 0x9d */
1171 		{{0, 0},
1172 		{2, 4},
1173 		{7, 7},
1174 		{0, 0}
1175 		}
1176 	},
1177 	{0, 1, 2, 0,		/* 0x9e */
1178 		{{1, 4},
1179 		{7, 7},
1180 		{0, 0},
1181 		{0, 0}
1182 		}
1183 	},
1184 	{1, 1, 2, 0,		/* 0x9f */
1185 		{{0, 4},
1186 		{7, 7},
1187 		{0, 0},
1188 		{0, 0}
1189 		}
1190 	},
1191 	{0, 1, 2, 0,		/* 0xa0 */
1192 		{{5, 5},
1193 		{7, 7},
1194 		{0, 0},
1195 		{0, 0}
1196 		}
1197 	},
1198 	{1, 1, 3, 0,		/* 0xa1 */
1199 		{{0, 0},
1200 		{5, 5},
1201 		{7, 7},
1202 		{0, 0}
1203 		}
1204 	},
1205 	{0, 1, 3, 0,		/* 0xa2 */
1206 		{{1, 1},
1207 		{5, 5},
1208 		{7, 7},
1209 		{0, 0}
1210 		}
1211 	},
1212 	{1, 1, 3, 0,		/* 0xa3 */
1213 		{{0, 1},
1214 		{5, 5},
1215 		{7, 7},
1216 		{0, 0}
1217 		}
1218 	},
1219 	{0, 1, 3, 0,		/* 0xa4 */
1220 		{{2, 2},
1221 		{5, 5},
1222 		{7, 7},
1223 		{0, 0}
1224 		}
1225 	},
1226 	{1, 1, 4, 0,		/* 0xa5 */
1227 		{{0, 0},
1228 		{2, 2},
1229 		{5, 5},
1230 		{7, 7}
1231 		}
1232 	},
1233 	{0, 1, 3, 0,		/* 0xa6 */
1234 		{{1, 2},
1235 		{5, 5},
1236 		{7, 7},
1237 		{0, 0}
1238 		}
1239 	},
1240 	{1, 1, 3, 0,		/* 0xa7 */
1241 		{{0, 2},
1242 		{5, 5},
1243 		{7, 7},
1244 		{0, 0}
1245 		}
1246 	},
1247 	{0, 1, 3, 0,		/* 0xa8 */
1248 		{{3, 3},
1249 		{5, 5},
1250 		{7, 7},
1251 		{0, 0}
1252 		}
1253 	},
1254 	{1, 1, 4, 0,		/* 0xa9 */
1255 		{{0, 0},
1256 		{3, 3},
1257 		{5, 5},
1258 		{7, 7}
1259 		}
1260 	},
1261 	{0, 1, 4, 0,		/* 0xaa */
1262 		{{1, 1},
1263 		{3, 3},
1264 		{5, 5},
1265 		{7, 7}
1266 		}
1267 	},
1268 	{1, 1, 4, 0,		/* 0xab */
1269 		{{0, 1},
1270 		{3, 3},
1271 		{5, 5},
1272 		{7, 7}
1273 		}
1274 	},
1275 	{0, 1, 3, 0,		/* 0xac */
1276 		{{2, 3},
1277 		{5, 5},
1278 		{7, 7},
1279 		{0, 0}
1280 		}
1281 	},
1282 	{1, 1, 4, 0,		/* 0xad */
1283 		{{0, 0},
1284 		{2, 3},
1285 		{5, 5},
1286 		{7, 7}
1287 		}
1288 	},
1289 	{0, 1, 3, 0,		/* 0xae */
1290 		{{1, 3},
1291 		{5, 5},
1292 		{7, 7},
1293 		{0, 0}
1294 		}
1295 	},
1296 	{1, 1, 3, 0,		/* 0xaf */
1297 		{{0, 3},
1298 		{5, 5},
1299 		{7, 7},
1300 		{0, 0}
1301 		}
1302 	},
1303 	{0, 1, 2, 0,		/* 0xb0 */
1304 		{{4, 5},
1305 		{7, 7},
1306 		{0, 0},
1307 		{0, 0}
1308 		}
1309 	},
1310 	{1, 1, 3, 0,		/* 0xb1 */
1311 		{{0, 0},
1312 		{4, 5},
1313 		{7, 7},
1314 		{0, 0}
1315 		}
1316 	},
1317 	{0, 1, 3, 0,		/* 0xb2 */
1318 		{{1, 1},
1319 		{4, 5},
1320 		{7, 7},
1321 		{0, 0}
1322 		}
1323 	},
1324 	{1, 1, 3, 0,		/* 0xb3 */
1325 		{{0, 1},
1326 		{4, 5},
1327 		{7, 7},
1328 		{0, 0}
1329 		}
1330 	},
1331 	{0, 1, 3, 0,		/* 0xb4 */
1332 		{{2, 2},
1333 		{4, 5},
1334 		{7, 7},
1335 		{0, 0}
1336 		}
1337 	},
1338 	{1, 1, 4, 0,		/* 0xb5 */
1339 		{{0, 0},
1340 		{2, 2},
1341 		{4, 5},
1342 		{7, 7}
1343 		}
1344 	},
1345 	{0, 1, 3, 0,		/* 0xb6 */
1346 		{{1, 2},
1347 		{4, 5},
1348 		{7, 7},
1349 		{0, 0}
1350 		}
1351 	},
1352 	{1, 1, 3, 0,		/* 0xb7 */
1353 		{{0, 2},
1354 		{4, 5},
1355 		{7, 7},
1356 		{0, 0}
1357 		}
1358 	},
1359 	{0, 1, 2, 0,		/* 0xb8 */
1360 		{{3, 5},
1361 		{7, 7},
1362 		{0, 0},
1363 		{0, 0}
1364 		}
1365 	},
1366 	{1, 1, 3, 0,		/* 0xb9 */
1367 		{{0, 0},
1368 		{3, 5},
1369 		{7, 7},
1370 		{0, 0}
1371 		}
1372 	},
1373 	{0, 1, 3, 0,		/* 0xba */
1374 		{{1, 1},
1375 		{3, 5},
1376 		{7, 7},
1377 		{0, 0}
1378 		}
1379 	},
1380 	{1, 1, 3, 0,		/* 0xbb */
1381 		{{0, 1},
1382 		{3, 5},
1383 		{7, 7},
1384 		{0, 0}
1385 		}
1386 	},
1387 	{0, 1, 2, 0,		/* 0xbc */
1388 		{{2, 5},
1389 		{7, 7},
1390 		{0, 0},
1391 		{0, 0}
1392 		}
1393 	},
1394 	{1, 1, 3, 0,		/* 0xbd */
1395 		{{0, 0},
1396 		{2, 5},
1397 		{7, 7},
1398 		{0, 0}
1399 		}
1400 	},
1401 	{0, 1, 2, 0,		/* 0xbe */
1402 		{{1, 5},
1403 		{7, 7},
1404 		{0, 0},
1405 		{0, 0}
1406 		}
1407 	},
1408 	{1, 1, 2, 0,		/* 0xbf */
1409 		{{0, 5},
1410 		{7, 7},
1411 		{0, 0},
1412 		{0, 0}
1413 		}
1414 	},
1415 	{0, 1, 1, 0,		/* 0xc0 */
1416 		{{6, 7},
1417 		{0, 0},
1418 		{0, 0},
1419 		{0, 0}
1420 		}
1421 	},
1422 	{1, 1, 2, 0,		/* 0xc1 */
1423 		{{0, 0},
1424 		{6, 7},
1425 		{0, 0},
1426 		{0, 0}
1427 		}
1428 	},
1429 	{0, 1, 2, 0,		/* 0xc2 */
1430 		{{1, 1},
1431 		{6, 7},
1432 		{0, 0},
1433 		{0, 0}
1434 		}
1435 	},
1436 	{1, 1, 2, 0,		/* 0xc3 */
1437 		{{0, 1},
1438 		{6, 7},
1439 		{0, 0},
1440 		{0, 0}
1441 		}
1442 	},
1443 	{0, 1, 2, 0,		/* 0xc4 */
1444 		{{2, 2},
1445 		{6, 7},
1446 		{0, 0},
1447 		{0, 0}
1448 		}
1449 	},
1450 	{1, 1, 3, 0,		/* 0xc5 */
1451 		{{0, 0},
1452 		{2, 2},
1453 		{6, 7},
1454 		{0, 0}
1455 		}
1456 	},
1457 	{0, 1, 2, 0,		/* 0xc6 */
1458 		{{1, 2},
1459 		{6, 7},
1460 		{0, 0},
1461 		{0, 0}
1462 		}
1463 	},
1464 	{1, 1, 2, 0,		/* 0xc7 */
1465 		{{0, 2},
1466 		{6, 7},
1467 		{0, 0},
1468 		{0, 0}
1469 		}
1470 	},
1471 	{0, 1, 2, 0,		/* 0xc8 */
1472 		{{3, 3},
1473 		{6, 7},
1474 		{0, 0},
1475 		{0, 0}
1476 		}
1477 	},
1478 	{1, 1, 3, 0,		/* 0xc9 */
1479 		{{0, 0},
1480 		{3, 3},
1481 		{6, 7},
1482 		{0, 0}
1483 		}
1484 	},
1485 	{0, 1, 3, 0,		/* 0xca */
1486 		{{1, 1},
1487 		{3, 3},
1488 		{6, 7},
1489 		{0, 0}
1490 		}
1491 	},
1492 	{1, 1, 3, 0,		/* 0xcb */
1493 		{{0, 1},
1494 		{3, 3},
1495 		{6, 7},
1496 		{0, 0}
1497 		}
1498 	},
1499 	{0, 1, 2, 0,		/* 0xcc */
1500 		{{2, 3},
1501 		{6, 7},
1502 		{0, 0},
1503 		{0, 0}
1504 		}
1505 	},
1506 	{1, 1, 3, 0,		/* 0xcd */
1507 		{{0, 0},
1508 		{2, 3},
1509 		{6, 7},
1510 		{0, 0}
1511 		}
1512 	},
1513 	{0, 1, 2, 0,		/* 0xce */
1514 		{{1, 3},
1515 		{6, 7},
1516 		{0, 0},
1517 		{0, 0}
1518 		}
1519 	},
1520 	{1, 1, 2, 0,		/* 0xcf */
1521 		{{0, 3},
1522 		{6, 7},
1523 		{0, 0},
1524 		{0, 0}
1525 		}
1526 	},
1527 	{0, 1, 2, 0,		/* 0xd0 */
1528 		{{4, 4},
1529 		{6, 7},
1530 		{0, 0},
1531 		{0, 0}
1532 		}
1533 	},
1534 	{1, 1, 3, 0,		/* 0xd1 */
1535 		{{0, 0},
1536 		{4, 4},
1537 		{6, 7},
1538 		{0, 0}
1539 		}
1540 	},
1541 	{0, 1, 3, 0,		/* 0xd2 */
1542 		{{1, 1},
1543 		{4, 4},
1544 		{6, 7},
1545 		{0, 0}
1546 		}
1547 	},
1548 	{1, 1, 3, 0,		/* 0xd3 */
1549 		{{0, 1},
1550 		{4, 4},
1551 		{6, 7},
1552 		{0, 0}
1553 		}
1554 	},
1555 	{0, 1, 3, 0,		/* 0xd4 */
1556 		{{2, 2},
1557 		{4, 4},
1558 		{6, 7},
1559 		{0, 0}
1560 		}
1561 	},
1562 	{1, 1, 4, 0,		/* 0xd5 */
1563 		{{0, 0},
1564 		{2, 2},
1565 		{4, 4},
1566 		{6, 7}
1567 		}
1568 	},
1569 	{0, 1, 3, 0,		/* 0xd6 */
1570 		{{1, 2},
1571 		{4, 4},
1572 		{6, 7},
1573 		{0, 0}
1574 		}
1575 	},
1576 	{1, 1, 3, 0,		/* 0xd7 */
1577 		{{0, 2},
1578 		{4, 4},
1579 		{6, 7},
1580 		{0, 0}
1581 		}
1582 	},
1583 	{0, 1, 2, 0,		/* 0xd8 */
1584 		{{3, 4},
1585 		{6, 7},
1586 		{0, 0},
1587 		{0, 0}
1588 		}
1589 	},
1590 	{1, 1, 3, 0,		/* 0xd9 */
1591 		{{0, 0},
1592 		{3, 4},
1593 		{6, 7},
1594 		{0, 0}
1595 		}
1596 	},
1597 	{0, 1, 3, 0,		/* 0xda */
1598 		{{1, 1},
1599 		{3, 4},
1600 		{6, 7},
1601 		{0, 0}
1602 		}
1603 	},
1604 	{1, 1, 3, 0,		/* 0xdb */
1605 		{{0, 1},
1606 		{3, 4},
1607 		{6, 7},
1608 		{0, 0}
1609 		}
1610 	},
1611 	{0, 1, 2, 0,		/* 0xdc */
1612 		{{2, 4},
1613 		{6, 7},
1614 		{0, 0},
1615 		{0, 0}
1616 		}
1617 	},
1618 	{1, 1, 3, 0,		/* 0xdd */
1619 		{{0, 0},
1620 		{2, 4},
1621 		{6, 7},
1622 		{0, 0}
1623 		}
1624 	},
1625 	{0, 1, 2, 0,		/* 0xde */
1626 		{{1, 4},
1627 		{6, 7},
1628 		{0, 0},
1629 		{0, 0}
1630 		}
1631 	},
1632 	{1, 1, 2, 0,		/* 0xdf */
1633 		{{0, 4},
1634 		{6, 7},
1635 		{0, 0},
1636 		{0, 0}
1637 		}
1638 	},
1639 	{0, 1, 1, 0,		/* 0xe0 */
1640 		{{5, 7},
1641 		{0, 0},
1642 		{0, 0},
1643 		{0, 0}
1644 		}
1645 	},
1646 	{1, 1, 2, 0,		/* 0xe1 */
1647 		{{0, 0},
1648 		{5, 7},
1649 		{0, 0},
1650 		{0, 0}
1651 		}
1652 	},
1653 	{0, 1, 2, 0,		/* 0xe2 */
1654 		{{1, 1},
1655 		{5, 7},
1656 		{0, 0},
1657 		{0, 0}
1658 		}
1659 	},
1660 	{1, 1, 2, 0,		/* 0xe3 */
1661 		{{0, 1},
1662 		{5, 7},
1663 		{0, 0},
1664 		{0, 0}
1665 		}
1666 	},
1667 	{0, 1, 2, 0,		/* 0xe4 */
1668 		{{2, 2},
1669 		{5, 7},
1670 		{0, 0},
1671 		{0, 0}
1672 		}
1673 	},
1674 	{1, 1, 3, 0,		/* 0xe5 */
1675 		{{0, 0},
1676 		{2, 2},
1677 		{5, 7},
1678 		{0, 0}
1679 		}
1680 	},
1681 	{0, 1, 2, 0,		/* 0xe6 */
1682 		{{1, 2},
1683 		{5, 7},
1684 		{0, 0},
1685 		{0, 0}
1686 		}
1687 	},
1688 	{1, 1, 2, 0,		/* 0xe7 */
1689 		{{0, 2},
1690 		{5, 7},
1691 		{0, 0},
1692 		{0, 0}
1693 		}
1694 	},
1695 	{0, 1, 2, 0,		/* 0xe8 */
1696 		{{3, 3},
1697 		{5, 7},
1698 		{0, 0},
1699 		{0, 0}
1700 		}
1701 	},
1702 	{1, 1, 3, 0,		/* 0xe9 */
1703 		{{0, 0},
1704 		{3, 3},
1705 		{5, 7},
1706 		{0, 0}
1707 		}
1708 	},
1709 	{0, 1, 3, 0,		/* 0xea */
1710 		{{1, 1},
1711 		{3, 3},
1712 		{5, 7},
1713 		{0, 0}
1714 		}
1715 	},
1716 	{1, 1, 3, 0,		/* 0xeb */
1717 		{{0, 1},
1718 		{3, 3},
1719 		{5, 7},
1720 		{0, 0}
1721 		}
1722 	},
1723 	{0, 1, 2, 0,		/* 0xec */
1724 		{{2, 3},
1725 		{5, 7},
1726 		{0, 0},
1727 		{0, 0}
1728 		}
1729 	},
1730 	{1, 1, 3, 0,		/* 0xed */
1731 		{{0, 0},
1732 		{2, 3},
1733 		{5, 7},
1734 		{0, 0}
1735 		}
1736 	},
1737 	{0, 1, 2, 0,		/* 0xee */
1738 		{{1, 3},
1739 		{5, 7},
1740 		{0, 0},
1741 		{0, 0}
1742 		}
1743 	},
1744 	{1, 1, 2, 0,		/* 0xef */
1745 		{{0, 3},
1746 		{5, 7},
1747 		{0, 0},
1748 		{0, 0}
1749 		}
1750 	},
1751 	{0, 1, 1, 0,		/* 0xf0 */
1752 		{{4, 7},
1753 		{0, 0},
1754 		{0, 0},
1755 		{0, 0}
1756 		}
1757 	},
1758 	{1, 1, 2, 0,		/* 0xf1 */
1759 		{{0, 0},
1760 		{4, 7},
1761 		{0, 0},
1762 		{0, 0}
1763 		}
1764 	},
1765 	{0, 1, 2, 0,		/* 0xf2 */
1766 		{{1, 1},
1767 		{4, 7},
1768 		{0, 0},
1769 		{0, 0}
1770 		}
1771 	},
1772 	{1, 1, 2, 0,		/* 0xf3 */
1773 		{{0, 1},
1774 		{4, 7},
1775 		{0, 0},
1776 		{0, 0}
1777 		}
1778 	},
1779 	{0, 1, 2, 0,		/* 0xf4 */
1780 		{{2, 2},
1781 		{4, 7},
1782 		{0, 0},
1783 		{0, 0}
1784 		}
1785 	},
1786 	{1, 1, 3, 0,		/* 0xf5 */
1787 		{{0, 0},
1788 		{2, 2},
1789 		{4, 7},
1790 		{0, 0}
1791 		}
1792 	},
1793 	{0, 1, 2, 0,		/* 0xf6 */
1794 		{{1, 2},
1795 		{4, 7},
1796 		{0, 0},
1797 		{0, 0}
1798 		}
1799 	},
1800 	{1, 1, 2, 0,		/* 0xf7 */
1801 		{{0, 2},
1802 		{4, 7},
1803 		{0, 0},
1804 		{0, 0}
1805 		}
1806 	},
1807 	{0, 1, 1, 0,		/* 0xf8 */
1808 		{{3, 7},
1809 		{0, 0},
1810 		{0, 0},
1811 		{0, 0}
1812 		}
1813 	},
1814 	{1, 1, 2, 0,		/* 0xf9 */
1815 		{{0, 0},
1816 		{3, 7},
1817 		{0, 0},
1818 		{0, 0}
1819 		}
1820 	},
1821 	{0, 1, 2, 0,		/* 0xfa */
1822 		{{1, 1},
1823 		{3, 7},
1824 		{0, 0},
1825 		{0, 0}
1826 		}
1827 	},
1828 	{1, 1, 2, 0,		/* 0xfb */
1829 		{{0, 1},
1830 		{3, 7},
1831 		{0, 0},
1832 		{0, 0}
1833 		}
1834 	},
1835 	{0, 1, 1, 0,		/* 0xfc */
1836 		{{2, 7},
1837 		{0, 0},
1838 		{0, 0},
1839 		{0, 0}
1840 		}
1841 	},
1842 	{1, 1, 2, 0,		/* 0xfd */
1843 		{{0, 0},
1844 		{2, 7},
1845 		{0, 0},
1846 		{0, 0}
1847 		}
1848 	},
1849 	{0, 1, 1, 0,		/* 0xfe */
1850 		{{1, 7},
1851 		{0, 0},
1852 		{0, 0},
1853 		{0, 0}
1854 		}
1855 	},
1856 	{1, 1, 1, 0,		/* 0xff */
1857 		{{0, 7},
1858 		{0, 0},
1859 		{0, 0},
1860 		{0, 0}
1861 		}
1862 	}
1863 };
1864 
1865 
1866 int
1867 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1868     int ipv4_addr_legal,
1869     int ipv6_addr_legal,
1870     int loopback_scope,
1871     int ipv4_local_scope,
1872     int local_scope SCTP_UNUSED,/* XXX */
1873     int site_scope,
1874     int do_update)
1875 {
1876 	if ((loopback_scope == 0) &&
1877 	    (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1878 		/*
1879 		 * skip loopback if not in scope *
1880 		 */
1881 		return (0);
1882 	}
1883 	switch (ifa->address.sa.sa_family) {
1884 #ifdef INET
1885 	case AF_INET:
1886 		if (ipv4_addr_legal) {
1887 			struct sockaddr_in *sin;
1888 
1889 			sin = (struct sockaddr_in *)&ifa->address.sin;
1890 			if (sin->sin_addr.s_addr == 0) {
1891 				/* not in scope , unspecified */
1892 				return (0);
1893 			}
1894 			if ((ipv4_local_scope == 0) &&
1895 			    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1896 				/* private address not in scope */
1897 				return (0);
1898 			}
1899 		} else {
1900 			return (0);
1901 		}
1902 		break;
1903 #endif
1904 #ifdef INET6
1905 	case AF_INET6:
1906 		if (ipv6_addr_legal) {
1907 			struct sockaddr_in6 *sin6;
1908 
1909 			/*
1910 			 * Must update the flags,  bummer, which means any
1911 			 * IFA locks must now be applied HERE <->
1912 			 */
1913 			if (do_update) {
1914 				sctp_gather_internal_ifa_flags(ifa);
1915 			}
1916 			if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1917 				return (0);
1918 			}
1919 			/* ok to use deprecated addresses? */
1920 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1921 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1922 				/* skip unspecifed addresses */
1923 				return (0);
1924 			}
1925 			if (	/* (local_scope == 0) && */
1926 			    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1927 				return (0);
1928 			}
1929 			if ((site_scope == 0) &&
1930 			    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1931 				return (0);
1932 			}
1933 		} else {
1934 			return (0);
1935 		}
1936 		break;
1937 #endif
1938 	default:
1939 		return (0);
1940 	}
1941 	return (1);
1942 }
1943 
1944 static struct mbuf *
1945 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1946 {
1947 	struct sctp_paramhdr *parmh;
1948 	struct mbuf *mret;
1949 	int len;
1950 
1951 	switch (ifa->address.sa.sa_family) {
1952 #ifdef INET
1953 	case AF_INET:
1954 		len = sizeof(struct sctp_ipv4addr_param);
1955 		break;
1956 #endif
1957 #ifdef INET6
1958 	case AF_INET6:
1959 		len = sizeof(struct sctp_ipv6addr_param);
1960 		break;
1961 #endif
1962 	default:
1963 		return (m);
1964 	}
1965 	if (M_TRAILINGSPACE(m) >= len) {
1966 		/* easy side we just drop it on the end */
1967 		parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1968 		mret = m;
1969 	} else {
1970 		/* Need more space */
1971 		mret = m;
1972 		while (SCTP_BUF_NEXT(mret) != NULL) {
1973 			mret = SCTP_BUF_NEXT(mret);
1974 		}
1975 		SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1976 		if (SCTP_BUF_NEXT(mret) == NULL) {
1977 			/* We are hosed, can't add more addresses */
1978 			return (m);
1979 		}
1980 		mret = SCTP_BUF_NEXT(mret);
1981 		parmh = mtod(mret, struct sctp_paramhdr *);
1982 	}
1983 	/* now add the parameter */
1984 	switch (ifa->address.sa.sa_family) {
1985 #ifdef INET
1986 	case AF_INET:
1987 		{
1988 			struct sctp_ipv4addr_param *ipv4p;
1989 			struct sockaddr_in *sin;
1990 
1991 			sin = (struct sockaddr_in *)&ifa->address.sin;
1992 			ipv4p = (struct sctp_ipv4addr_param *)parmh;
1993 			parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1994 			parmh->param_length = htons(len);
1995 			ipv4p->addr = sin->sin_addr.s_addr;
1996 			SCTP_BUF_LEN(mret) += len;
1997 			break;
1998 		}
1999 #endif
2000 #ifdef INET6
2001 	case AF_INET6:
2002 		{
2003 			struct sctp_ipv6addr_param *ipv6p;
2004 			struct sockaddr_in6 *sin6;
2005 
2006 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
2007 			ipv6p = (struct sctp_ipv6addr_param *)parmh;
2008 			parmh->param_type = htons(SCTP_IPV6_ADDRESS);
2009 			parmh->param_length = htons(len);
2010 			memcpy(ipv6p->addr, &sin6->sin6_addr,
2011 			    sizeof(ipv6p->addr));
2012 			/* clear embedded scope in the address */
2013 			in6_clearscope((struct in6_addr *)ipv6p->addr);
2014 			SCTP_BUF_LEN(mret) += len;
2015 			break;
2016 		}
2017 #endif
2018 	default:
2019 		return (m);
2020 	}
2021 	return (mret);
2022 }
2023 
2024 
2025 struct mbuf *
2026 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2027     struct sctp_scoping *scope,
2028     struct mbuf *m_at, int cnt_inits_to)
2029 {
2030 	struct sctp_vrf *vrf = NULL;
2031 	int cnt, limit_out = 0, total_count;
2032 	uint32_t vrf_id;
2033 
2034 	vrf_id = inp->def_vrf_id;
2035 	SCTP_IPI_ADDR_RLOCK();
2036 	vrf = sctp_find_vrf(vrf_id);
2037 	if (vrf == NULL) {
2038 		SCTP_IPI_ADDR_RUNLOCK();
2039 		return (m_at);
2040 	}
2041 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2042 		struct sctp_ifa *sctp_ifap;
2043 		struct sctp_ifn *sctp_ifnp;
2044 
2045 		cnt = cnt_inits_to;
2046 		if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2047 			limit_out = 1;
2048 			cnt = SCTP_ADDRESS_LIMIT;
2049 			goto skip_count;
2050 		}
2051 		LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2052 			if ((scope->loopback_scope == 0) &&
2053 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2054 				/*
2055 				 * Skip loopback devices if loopback_scope
2056 				 * not set
2057 				 */
2058 				continue;
2059 			}
2060 			LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2061 				if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2062 					continue;
2063 				}
2064 				if (sctp_is_address_in_scope(sctp_ifap,
2065 				    scope->ipv4_addr_legal,
2066 				    scope->ipv6_addr_legal,
2067 				    scope->loopback_scope,
2068 				    scope->ipv4_local_scope,
2069 				    scope->local_scope,
2070 				    scope->site_scope, 1) == 0) {
2071 					continue;
2072 				}
2073 				cnt++;
2074 				if (cnt > SCTP_ADDRESS_LIMIT) {
2075 					break;
2076 				}
2077 			}
2078 			if (cnt > SCTP_ADDRESS_LIMIT) {
2079 				break;
2080 			}
2081 		}
2082 skip_count:
2083 		if (cnt > 1) {
2084 			total_count = 0;
2085 			LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2086 				cnt = 0;
2087 				if ((scope->loopback_scope == 0) &&
2088 				    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2089 					/*
2090 					 * Skip loopback devices if
2091 					 * loopback_scope not set
2092 					 */
2093 					continue;
2094 				}
2095 				LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2096 					if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2097 						continue;
2098 					}
2099 					if (sctp_is_address_in_scope(sctp_ifap,
2100 					    scope->ipv4_addr_legal,
2101 					    scope->ipv6_addr_legal,
2102 					    scope->loopback_scope,
2103 					    scope->ipv4_local_scope,
2104 					    scope->local_scope,
2105 					    scope->site_scope, 0) == 0) {
2106 						continue;
2107 					}
2108 					m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2109 					if (limit_out) {
2110 						cnt++;
2111 						total_count++;
2112 						if (cnt >= 2) {
2113 							/*
2114 							 * two from each
2115 							 * address
2116 							 */
2117 							break;
2118 						}
2119 						if (total_count > SCTP_ADDRESS_LIMIT) {
2120 							/* No more addresses */
2121 							break;
2122 						}
2123 					}
2124 				}
2125 			}
2126 		}
2127 	} else {
2128 		struct sctp_laddr *laddr;
2129 
2130 		cnt = cnt_inits_to;
2131 		/* First, how many ? */
2132 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2133 			if (laddr->ifa == NULL) {
2134 				continue;
2135 			}
2136 			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2137 				/*
2138 				 * Address being deleted by the system, dont
2139 				 * list.
2140 				 */
2141 				continue;
2142 			if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2143 				/*
2144 				 * Address being deleted on this ep don't
2145 				 * list.
2146 				 */
2147 				continue;
2148 			}
2149 			if (sctp_is_address_in_scope(laddr->ifa,
2150 			    scope->ipv4_addr_legal,
2151 			    scope->ipv6_addr_legal,
2152 			    scope->loopback_scope,
2153 			    scope->ipv4_local_scope,
2154 			    scope->local_scope,
2155 			    scope->site_scope, 1) == 0) {
2156 				continue;
2157 			}
2158 			cnt++;
2159 		}
2160 		/*
2161 		 * To get through a NAT we only list addresses if we have
2162 		 * more than one. That way if you just bind a single address
2163 		 * we let the source of the init dictate our address.
2164 		 */
2165 		if (cnt > 1) {
2166 			cnt = cnt_inits_to;
2167 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2168 				if (laddr->ifa == NULL) {
2169 					continue;
2170 				}
2171 				if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
2172 					continue;
2173 				}
2174 				if (sctp_is_address_in_scope(laddr->ifa,
2175 				    scope->ipv4_addr_legal,
2176 				    scope->ipv6_addr_legal,
2177 				    scope->loopback_scope,
2178 				    scope->ipv4_local_scope,
2179 				    scope->local_scope,
2180 				    scope->site_scope, 0) == 0) {
2181 					continue;
2182 				}
2183 				m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2184 				cnt++;
2185 				if (cnt >= SCTP_ADDRESS_LIMIT) {
2186 					break;
2187 				}
2188 			}
2189 		}
2190 	}
2191 	SCTP_IPI_ADDR_RUNLOCK();
2192 	return (m_at);
2193 }
2194 
2195 static struct sctp_ifa *
2196 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2197     uint8_t dest_is_loop,
2198     uint8_t dest_is_priv,
2199     sa_family_t fam)
2200 {
2201 	uint8_t dest_is_global = 0;
2202 
2203 	/* dest_is_priv is true if destination is a private address */
2204 	/* dest_is_loop is true if destination is a loopback addresses */
2205 
2206 	/**
2207 	 * Here we determine if its a preferred address. A preferred address
2208 	 * means it is the same scope or higher scope then the destination.
2209 	 * L = loopback, P = private, G = global
2210 	 * -----------------------------------------
2211 	 *    src    |  dest | result
2212 	 *  ----------------------------------------
2213 	 *     L     |    L  |    yes
2214 	 *  -----------------------------------------
2215 	 *     P     |    L  |    yes-v4 no-v6
2216 	 *  -----------------------------------------
2217 	 *     G     |    L  |    yes-v4 no-v6
2218 	 *  -----------------------------------------
2219 	 *     L     |    P  |    no
2220 	 *  -----------------------------------------
2221 	 *     P     |    P  |    yes
2222 	 *  -----------------------------------------
2223 	 *     G     |    P  |    no
2224 	 *   -----------------------------------------
2225 	 *     L     |    G  |    no
2226 	 *   -----------------------------------------
2227 	 *     P     |    G  |    no
2228 	 *    -----------------------------------------
2229 	 *     G     |    G  |    yes
2230 	 *    -----------------------------------------
2231 	 */
2232 
2233 	if (ifa->address.sa.sa_family != fam) {
2234 		/* forget mis-matched family */
2235 		return (NULL);
2236 	}
2237 	if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2238 		dest_is_global = 1;
2239 	}
2240 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2241 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2242 	/* Ok the address may be ok */
2243 #ifdef INET6
2244 	if (fam == AF_INET6) {
2245 		/* ok to use deprecated addresses? no lets not! */
2246 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2247 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2248 			return (NULL);
2249 		}
2250 		if (ifa->src_is_priv && !ifa->src_is_loop) {
2251 			if (dest_is_loop) {
2252 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2253 				return (NULL);
2254 			}
2255 		}
2256 		if (ifa->src_is_glob) {
2257 			if (dest_is_loop) {
2258 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2259 				return (NULL);
2260 			}
2261 		}
2262 	}
2263 #endif
2264 	/*
2265 	 * Now that we know what is what, implement or table this could in
2266 	 * theory be done slicker (it used to be), but this is
2267 	 * straightforward and easier to validate :-)
2268 	 */
2269 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2270 	    ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2271 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2272 	    dest_is_loop, dest_is_priv, dest_is_global);
2273 
2274 	if ((ifa->src_is_loop) && (dest_is_priv)) {
2275 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2276 		return (NULL);
2277 	}
2278 	if ((ifa->src_is_glob) && (dest_is_priv)) {
2279 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2280 		return (NULL);
2281 	}
2282 	if ((ifa->src_is_loop) && (dest_is_global)) {
2283 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2284 		return (NULL);
2285 	}
2286 	if ((ifa->src_is_priv) && (dest_is_global)) {
2287 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2288 		return (NULL);
2289 	}
2290 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2291 	/* its a preferred address */
2292 	return (ifa);
2293 }
2294 
2295 static struct sctp_ifa *
2296 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2297     uint8_t dest_is_loop,
2298     uint8_t dest_is_priv,
2299     sa_family_t fam)
2300 {
2301 	uint8_t dest_is_global = 0;
2302 
2303 	/**
2304 	 * Here we determine if its a acceptable address. A acceptable
2305 	 * address means it is the same scope or higher scope but we can
2306 	 * allow for NAT which means its ok to have a global dest and a
2307 	 * private src.
2308 	 *
2309 	 * L = loopback, P = private, G = global
2310 	 * -----------------------------------------
2311 	 *  src    |  dest | result
2312 	 * -----------------------------------------
2313 	 *   L     |   L   |    yes
2314 	 *  -----------------------------------------
2315 	 *   P     |   L   |    yes-v4 no-v6
2316 	 *  -----------------------------------------
2317 	 *   G     |   L   |    yes
2318 	 * -----------------------------------------
2319 	 *   L     |   P   |    no
2320 	 * -----------------------------------------
2321 	 *   P     |   P   |    yes
2322 	 * -----------------------------------------
2323 	 *   G     |   P   |    yes - May not work
2324 	 * -----------------------------------------
2325 	 *   L     |   G   |    no
2326 	 * -----------------------------------------
2327 	 *   P     |   G   |    yes - May not work
2328 	 * -----------------------------------------
2329 	 *   G     |   G   |    yes
2330 	 * -----------------------------------------
2331 	 */
2332 
2333 	if (ifa->address.sa.sa_family != fam) {
2334 		/* forget non matching family */
2335 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n",
2336 		    ifa->address.sa.sa_family, fam);
2337 		return (NULL);
2338 	}
2339 	/* Ok the address may be ok */
2340 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa);
2341 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n",
2342 	    dest_is_loop, dest_is_priv);
2343 	if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2344 		dest_is_global = 1;
2345 	}
2346 #ifdef INET6
2347 	if (fam == AF_INET6) {
2348 		/* ok to use deprecated addresses? */
2349 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2350 			return (NULL);
2351 		}
2352 		if (ifa->src_is_priv) {
2353 			/* Special case, linklocal to loop */
2354 			if (dest_is_loop)
2355 				return (NULL);
2356 		}
2357 	}
2358 #endif
2359 	/*
2360 	 * Now that we know what is what, implement our table. This could in
2361 	 * theory be done slicker (it used to be), but this is
2362 	 * straightforward and easier to validate :-)
2363 	 */
2364 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n",
2365 	    ifa->src_is_loop,
2366 	    dest_is_priv);
2367 	if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2368 		return (NULL);
2369 	}
2370 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n",
2371 	    ifa->src_is_loop,
2372 	    dest_is_global);
2373 	if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2374 		return (NULL);
2375 	}
2376 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n");
2377 	/* its an acceptable address */
2378 	return (ifa);
2379 }
2380 
2381 int
2382 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2383 {
2384 	struct sctp_laddr *laddr;
2385 
2386 	if (stcb == NULL) {
2387 		/* There are no restrictions, no TCB :-) */
2388 		return (0);
2389 	}
2390 	LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2391 		if (laddr->ifa == NULL) {
2392 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2393 			    __FUNCTION__);
2394 			continue;
2395 		}
2396 		if (laddr->ifa == ifa) {
2397 			/* Yes it is on the list */
2398 			return (1);
2399 		}
2400 	}
2401 	return (0);
2402 }
2403 
2404 
2405 int
2406 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2407 {
2408 	struct sctp_laddr *laddr;
2409 
2410 	if (ifa == NULL)
2411 		return (0);
2412 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2413 		if (laddr->ifa == NULL) {
2414 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2415 			    __FUNCTION__);
2416 			continue;
2417 		}
2418 		if ((laddr->ifa == ifa) && laddr->action == 0)
2419 			/* same pointer */
2420 			return (1);
2421 	}
2422 	return (0);
2423 }
2424 
2425 
2426 
2427 static struct sctp_ifa *
2428 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2429     sctp_route_t * ro,
2430     uint32_t vrf_id,
2431     int non_asoc_addr_ok,
2432     uint8_t dest_is_priv,
2433     uint8_t dest_is_loop,
2434     sa_family_t fam)
2435 {
2436 	struct sctp_laddr *laddr, *starting_point;
2437 	void *ifn;
2438 	int resettotop = 0;
2439 	struct sctp_ifn *sctp_ifn;
2440 	struct sctp_ifa *sctp_ifa, *sifa;
2441 	struct sctp_vrf *vrf;
2442 	uint32_t ifn_index;
2443 
2444 	vrf = sctp_find_vrf(vrf_id);
2445 	if (vrf == NULL)
2446 		return (NULL);
2447 
2448 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2449 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2450 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2451 	/*
2452 	 * first question, is the ifn we will emit on in our list, if so, we
2453 	 * want such an address. Note that we first looked for a preferred
2454 	 * address.
2455 	 */
2456 	if (sctp_ifn) {
2457 		/* is a preferred one on the interface we route out? */
2458 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2459 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2460 			    (non_asoc_addr_ok == 0))
2461 				continue;
2462 			sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2463 			    dest_is_loop,
2464 			    dest_is_priv, fam);
2465 			if (sifa == NULL)
2466 				continue;
2467 			if (sctp_is_addr_in_ep(inp, sifa)) {
2468 				atomic_add_int(&sifa->refcount, 1);
2469 				return (sifa);
2470 			}
2471 		}
2472 	}
2473 	/*
2474 	 * ok, now we now need to find one on the list of the addresses. We
2475 	 * can't get one on the emitting interface so let's find first a
2476 	 * preferred one. If not that an acceptable one otherwise... we
2477 	 * return NULL.
2478 	 */
2479 	starting_point = inp->next_addr_touse;
2480 once_again:
2481 	if (inp->next_addr_touse == NULL) {
2482 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2483 		resettotop = 1;
2484 	}
2485 	for (laddr = inp->next_addr_touse; laddr;
2486 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2487 		if (laddr->ifa == NULL) {
2488 			/* address has been removed */
2489 			continue;
2490 		}
2491 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2492 			/* address is being deleted */
2493 			continue;
2494 		}
2495 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2496 		    dest_is_priv, fam);
2497 		if (sifa == NULL)
2498 			continue;
2499 		atomic_add_int(&sifa->refcount, 1);
2500 		return (sifa);
2501 	}
2502 	if (resettotop == 0) {
2503 		inp->next_addr_touse = NULL;
2504 		goto once_again;
2505 	}
2506 	inp->next_addr_touse = starting_point;
2507 	resettotop = 0;
2508 once_again_too:
2509 	if (inp->next_addr_touse == NULL) {
2510 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2511 		resettotop = 1;
2512 	}
2513 	/* ok, what about an acceptable address in the inp */
2514 	for (laddr = inp->next_addr_touse; laddr;
2515 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2516 		if (laddr->ifa == NULL) {
2517 			/* address has been removed */
2518 			continue;
2519 		}
2520 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2521 			/* address is being deleted */
2522 			continue;
2523 		}
2524 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2525 		    dest_is_priv, fam);
2526 		if (sifa == NULL)
2527 			continue;
2528 		atomic_add_int(&sifa->refcount, 1);
2529 		return (sifa);
2530 	}
2531 	if (resettotop == 0) {
2532 		inp->next_addr_touse = NULL;
2533 		goto once_again_too;
2534 	}
2535 	/*
2536 	 * no address bound can be a source for the destination we are in
2537 	 * trouble
2538 	 */
2539 	return (NULL);
2540 }
2541 
2542 
2543 
2544 static struct sctp_ifa *
2545 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2546     struct sctp_tcb *stcb,
2547     sctp_route_t * ro,
2548     uint32_t vrf_id,
2549     uint8_t dest_is_priv,
2550     uint8_t dest_is_loop,
2551     int non_asoc_addr_ok,
2552     sa_family_t fam)
2553 {
2554 	struct sctp_laddr *laddr, *starting_point;
2555 	void *ifn;
2556 	struct sctp_ifn *sctp_ifn;
2557 	struct sctp_ifa *sctp_ifa, *sifa;
2558 	uint8_t start_at_beginning = 0;
2559 	struct sctp_vrf *vrf;
2560 	uint32_t ifn_index;
2561 
2562 	/*
2563 	 * first question, is the ifn we will emit on in our list, if so, we
2564 	 * want that one.
2565 	 */
2566 	vrf = sctp_find_vrf(vrf_id);
2567 	if (vrf == NULL)
2568 		return (NULL);
2569 
2570 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2571 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2572 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2573 
2574 	/*
2575 	 * first question, is the ifn we will emit on in our list?  If so,
2576 	 * we want that one. First we look for a preferred. Second, we go
2577 	 * for an acceptable.
2578 	 */
2579 	if (sctp_ifn) {
2580 		/* first try for a preferred address on the ep */
2581 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2582 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2583 				continue;
2584 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2585 				sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2586 				if (sifa == NULL)
2587 					continue;
2588 				if (((non_asoc_addr_ok == 0) &&
2589 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2590 				    (non_asoc_addr_ok &&
2591 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2592 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2593 					/* on the no-no list */
2594 					continue;
2595 				}
2596 				atomic_add_int(&sifa->refcount, 1);
2597 				return (sifa);
2598 			}
2599 		}
2600 		/* next try for an acceptable address on the ep */
2601 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2602 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2603 				continue;
2604 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2605 				sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2606 				if (sifa == NULL)
2607 					continue;
2608 				if (((non_asoc_addr_ok == 0) &&
2609 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2610 				    (non_asoc_addr_ok &&
2611 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2612 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2613 					/* on the no-no list */
2614 					continue;
2615 				}
2616 				atomic_add_int(&sifa->refcount, 1);
2617 				return (sifa);
2618 			}
2619 		}
2620 
2621 	}
2622 	/*
2623 	 * if we can't find one like that then we must look at all addresses
2624 	 * bound to pick one at first preferable then secondly acceptable.
2625 	 */
2626 	starting_point = stcb->asoc.last_used_address;
2627 sctp_from_the_top:
2628 	if (stcb->asoc.last_used_address == NULL) {
2629 		start_at_beginning = 1;
2630 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2631 	}
2632 	/* search beginning with the last used address */
2633 	for (laddr = stcb->asoc.last_used_address; laddr;
2634 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2635 		if (laddr->ifa == NULL) {
2636 			/* address has been removed */
2637 			continue;
2638 		}
2639 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2640 			/* address is being deleted */
2641 			continue;
2642 		}
2643 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2644 		if (sifa == NULL)
2645 			continue;
2646 		if (((non_asoc_addr_ok == 0) &&
2647 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2648 		    (non_asoc_addr_ok &&
2649 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2650 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2651 			/* on the no-no list */
2652 			continue;
2653 		}
2654 		stcb->asoc.last_used_address = laddr;
2655 		atomic_add_int(&sifa->refcount, 1);
2656 		return (sifa);
2657 	}
2658 	if (start_at_beginning == 0) {
2659 		stcb->asoc.last_used_address = NULL;
2660 		goto sctp_from_the_top;
2661 	}
2662 	/* now try for any higher scope than the destination */
2663 	stcb->asoc.last_used_address = starting_point;
2664 	start_at_beginning = 0;
2665 sctp_from_the_top2:
2666 	if (stcb->asoc.last_used_address == NULL) {
2667 		start_at_beginning = 1;
2668 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2669 	}
2670 	/* search beginning with the last used address */
2671 	for (laddr = stcb->asoc.last_used_address; laddr;
2672 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2673 		if (laddr->ifa == NULL) {
2674 			/* address has been removed */
2675 			continue;
2676 		}
2677 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2678 			/* address is being deleted */
2679 			continue;
2680 		}
2681 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2682 		    dest_is_priv, fam);
2683 		if (sifa == NULL)
2684 			continue;
2685 		if (((non_asoc_addr_ok == 0) &&
2686 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2687 		    (non_asoc_addr_ok &&
2688 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2689 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2690 			/* on the no-no list */
2691 			continue;
2692 		}
2693 		stcb->asoc.last_used_address = laddr;
2694 		atomic_add_int(&sifa->refcount, 1);
2695 		return (sifa);
2696 	}
2697 	if (start_at_beginning == 0) {
2698 		stcb->asoc.last_used_address = NULL;
2699 		goto sctp_from_the_top2;
2700 	}
2701 	return (NULL);
2702 }
2703 
2704 static struct sctp_ifa *
2705 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2706     struct sctp_tcb *stcb,
2707     int non_asoc_addr_ok,
2708     uint8_t dest_is_loop,
2709     uint8_t dest_is_priv,
2710     int addr_wanted,
2711     sa_family_t fam,
2712     sctp_route_t * ro
2713 )
2714 {
2715 	struct sctp_ifa *ifa, *sifa;
2716 	int num_eligible_addr = 0;
2717 
2718 #ifdef INET6
2719 	struct sockaddr_in6 sin6, lsa6;
2720 
2721 	if (fam == AF_INET6) {
2722 		memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2723 		(void)sa6_recoverscope(&sin6);
2724 	}
2725 #endif				/* INET6 */
2726 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2727 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2728 		    (non_asoc_addr_ok == 0))
2729 			continue;
2730 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2731 		    dest_is_priv, fam);
2732 		if (sifa == NULL)
2733 			continue;
2734 #ifdef INET6
2735 		if (fam == AF_INET6 &&
2736 		    dest_is_loop &&
2737 		    sifa->src_is_loop && sifa->src_is_priv) {
2738 			/*
2739 			 * don't allow fe80::1 to be a src on loop ::1, we
2740 			 * don't list it to the peer so we will get an
2741 			 * abort.
2742 			 */
2743 			continue;
2744 		}
2745 		if (fam == AF_INET6 &&
2746 		    IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2747 		    IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2748 			/*
2749 			 * link-local <-> link-local must belong to the same
2750 			 * scope.
2751 			 */
2752 			memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2753 			(void)sa6_recoverscope(&lsa6);
2754 			if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2755 				continue;
2756 			}
2757 		}
2758 #endif				/* INET6 */
2759 
2760 		/*
2761 		 * Check if the IPv6 address matches to next-hop. In the
2762 		 * mobile case, old IPv6 address may be not deleted from the
2763 		 * interface. Then, the interface has previous and new
2764 		 * addresses.  We should use one corresponding to the
2765 		 * next-hop.  (by micchie)
2766 		 */
2767 #ifdef INET6
2768 		if (stcb && fam == AF_INET6 &&
2769 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2770 			if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2771 			    == 0) {
2772 				continue;
2773 			}
2774 		}
2775 #endif
2776 #ifdef INET
2777 		/* Avoid topologically incorrect IPv4 address */
2778 		if (stcb && fam == AF_INET &&
2779 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2780 			if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2781 				continue;
2782 			}
2783 		}
2784 #endif
2785 		if (stcb) {
2786 			if (sctp_is_address_in_scope(ifa,
2787 			    stcb->asoc.ipv4_addr_legal,
2788 			    stcb->asoc.ipv6_addr_legal,
2789 			    stcb->asoc.loopback_scope,
2790 			    stcb->asoc.ipv4_local_scope,
2791 			    stcb->asoc.local_scope,
2792 			    stcb->asoc.site_scope, 0) == 0) {
2793 				continue;
2794 			}
2795 			if (((non_asoc_addr_ok == 0) &&
2796 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2797 			    (non_asoc_addr_ok &&
2798 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2799 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2800 				/*
2801 				 * It is restricted for some reason..
2802 				 * probably not yet added.
2803 				 */
2804 				continue;
2805 			}
2806 		}
2807 		if (num_eligible_addr >= addr_wanted) {
2808 			return (sifa);
2809 		}
2810 		num_eligible_addr++;
2811 	}
2812 	return (NULL);
2813 }
2814 
2815 
2816 static int
2817 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2818     struct sctp_tcb *stcb,
2819     int non_asoc_addr_ok,
2820     uint8_t dest_is_loop,
2821     uint8_t dest_is_priv,
2822     sa_family_t fam)
2823 {
2824 	struct sctp_ifa *ifa, *sifa;
2825 	int num_eligible_addr = 0;
2826 
2827 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2828 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2829 		    (non_asoc_addr_ok == 0)) {
2830 			continue;
2831 		}
2832 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2833 		    dest_is_priv, fam);
2834 		if (sifa == NULL) {
2835 			continue;
2836 		}
2837 		if (stcb) {
2838 			if (sctp_is_address_in_scope(ifa,
2839 			    stcb->asoc.ipv4_addr_legal,
2840 			    stcb->asoc.ipv6_addr_legal,
2841 			    stcb->asoc.loopback_scope,
2842 			    stcb->asoc.ipv4_local_scope,
2843 			    stcb->asoc.local_scope,
2844 			    stcb->asoc.site_scope, 0) == 0) {
2845 				continue;
2846 			}
2847 			if (((non_asoc_addr_ok == 0) &&
2848 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2849 			    (non_asoc_addr_ok &&
2850 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2851 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2852 				/*
2853 				 * It is restricted for some reason..
2854 				 * probably not yet added.
2855 				 */
2856 				continue;
2857 			}
2858 		}
2859 		num_eligible_addr++;
2860 	}
2861 	return (num_eligible_addr);
2862 }
2863 
2864 static struct sctp_ifa *
2865 sctp_choose_boundall(struct sctp_tcb *stcb,
2866     struct sctp_nets *net,
2867     sctp_route_t * ro,
2868     uint32_t vrf_id,
2869     uint8_t dest_is_priv,
2870     uint8_t dest_is_loop,
2871     int non_asoc_addr_ok,
2872     sa_family_t fam)
2873 {
2874 	int cur_addr_num = 0, num_preferred = 0;
2875 	void *ifn;
2876 	struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2877 	struct sctp_ifa *sctp_ifa, *sifa;
2878 	uint32_t ifn_index;
2879 	struct sctp_vrf *vrf;
2880 
2881 #ifdef INET
2882 	int retried = 0;
2883 
2884 #endif
2885 
2886 	/*-
2887 	 * For boundall we can use any address in the association.
2888 	 * If non_asoc_addr_ok is set we can use any address (at least in
2889 	 * theory). So we look for preferred addresses first. If we find one,
2890 	 * we use it. Otherwise we next try to get an address on the
2891 	 * interface, which we should be able to do (unless non_asoc_addr_ok
2892 	 * is false and we are routed out that way). In these cases where we
2893 	 * can't use the address of the interface we go through all the
2894 	 * ifn's looking for an address we can use and fill that in. Punting
2895 	 * means we send back address 0, which will probably cause problems
2896 	 * actually since then IP will fill in the address of the route ifn,
2897 	 * which means we probably already rejected it.. i.e. here comes an
2898 	 * abort :-<.
2899 	 */
2900 	vrf = sctp_find_vrf(vrf_id);
2901 	if (vrf == NULL)
2902 		return (NULL);
2903 
2904 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2905 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2906 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn from route:%p ifn_index:%d\n", ifn, ifn_index);
2907 	emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2908 	if (sctp_ifn == NULL) {
2909 		/* ?? We don't have this guy ?? */
2910 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2911 		goto bound_all_plan_b;
2912 	}
2913 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2914 	    ifn_index, sctp_ifn->ifn_name);
2915 
2916 	if (net) {
2917 		cur_addr_num = net->indx_of_eligible_next_to_use;
2918 	}
2919 	num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2920 	    stcb,
2921 	    non_asoc_addr_ok,
2922 	    dest_is_loop,
2923 	    dest_is_priv, fam);
2924 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2925 	    num_preferred, sctp_ifn->ifn_name);
2926 	if (num_preferred == 0) {
2927 		/*
2928 		 * no eligible addresses, we must use some other interface
2929 		 * address if we can find one.
2930 		 */
2931 		goto bound_all_plan_b;
2932 	}
2933 	/*
2934 	 * Ok we have num_eligible_addr set with how many we can use, this
2935 	 * may vary from call to call due to addresses being deprecated
2936 	 * etc..
2937 	 */
2938 	if (cur_addr_num >= num_preferred) {
2939 		cur_addr_num = 0;
2940 	}
2941 	/*
2942 	 * select the nth address from the list (where cur_addr_num is the
2943 	 * nth) and 0 is the first one, 1 is the second one etc...
2944 	 */
2945 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2946 
2947 	sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2948 	    dest_is_priv, cur_addr_num, fam, ro);
2949 
2950 	/* if sctp_ifa is NULL something changed??, fall to plan b. */
2951 	if (sctp_ifa) {
2952 		atomic_add_int(&sctp_ifa->refcount, 1);
2953 		if (net) {
2954 			/* save off where the next one we will want */
2955 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2956 		}
2957 		return (sctp_ifa);
2958 	}
2959 	/*
2960 	 * plan_b: Look at all interfaces and find a preferred address. If
2961 	 * no preferred fall through to plan_c.
2962 	 */
2963 bound_all_plan_b:
2964 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2965 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2966 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2967 		    sctp_ifn->ifn_name);
2968 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2969 			/* wrong base scope */
2970 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2971 			continue;
2972 		}
2973 		if ((sctp_ifn == looked_at) && looked_at) {
2974 			/* already looked at this guy */
2975 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2976 			continue;
2977 		}
2978 		num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2979 		    dest_is_loop, dest_is_priv, fam);
2980 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
2981 		    "Found ifn:%p %d preferred source addresses\n",
2982 		    ifn, num_preferred);
2983 		if (num_preferred == 0) {
2984 			/* None on this interface. */
2985 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2986 			continue;
2987 		}
2988 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
2989 		    "num preferred:%d on interface:%p cur_addr_num:%d\n",
2990 		    num_preferred, sctp_ifn, cur_addr_num);
2991 
2992 		/*
2993 		 * Ok we have num_eligible_addr set with how many we can
2994 		 * use, this may vary from call to call due to addresses
2995 		 * being deprecated etc..
2996 		 */
2997 		if (cur_addr_num >= num_preferred) {
2998 			cur_addr_num = 0;
2999 		}
3000 		sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
3001 		    dest_is_priv, cur_addr_num, fam, ro);
3002 		if (sifa == NULL)
3003 			continue;
3004 		if (net) {
3005 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3006 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
3007 			    cur_addr_num);
3008 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
3009 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
3010 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
3011 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
3012 		}
3013 		atomic_add_int(&sifa->refcount, 1);
3014 		return (sifa);
3015 	}
3016 #ifdef INET
3017 again_with_private_addresses_allowed:
3018 #endif
3019 	/* plan_c: do we have an acceptable address on the emit interface */
3020 	sifa = NULL;
3021 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
3022 	if (emit_ifn == NULL) {
3023 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jump to Plan D - no emit_ifn\n");
3024 		goto plan_d;
3025 	}
3026 	LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
3027 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", sctp_ifa);
3028 		if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3029 		    (non_asoc_addr_ok == 0)) {
3030 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Defer\n");
3031 			continue;
3032 		}
3033 		sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
3034 		    dest_is_priv, fam);
3035 		if (sifa == NULL) {
3036 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n");
3037 			continue;
3038 		}
3039 		if (stcb) {
3040 			if (sctp_is_address_in_scope(sifa,
3041 			    stcb->asoc.ipv4_addr_legal,
3042 			    stcb->asoc.ipv6_addr_legal,
3043 			    stcb->asoc.loopback_scope,
3044 			    stcb->asoc.ipv4_local_scope,
3045 			    stcb->asoc.local_scope,
3046 			    stcb->asoc.site_scope, 0) == 0) {
3047 				SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n");
3048 				sifa = NULL;
3049 				continue;
3050 			}
3051 			if (((non_asoc_addr_ok == 0) &&
3052 			    (sctp_is_addr_restricted(stcb, sifa))) ||
3053 			    (non_asoc_addr_ok &&
3054 			    (sctp_is_addr_restricted(stcb, sifa)) &&
3055 			    (!sctp_is_addr_pending(stcb, sifa)))) {
3056 				/*
3057 				 * It is restricted for some reason..
3058 				 * probably not yet added.
3059 				 */
3060 				SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its resticted\n");
3061 				sifa = NULL;
3062 				continue;
3063 			}
3064 		} else {
3065 			printf("Stcb is null - no print\n");
3066 		}
3067 		atomic_add_int(&sifa->refcount, 1);
3068 		goto out;
3069 	}
3070 plan_d:
3071 	/*
3072 	 * plan_d: We are in trouble. No preferred address on the emit
3073 	 * interface. And not even a preferred address on all interfaces. Go
3074 	 * out and see if we can find an acceptable address somewhere
3075 	 * amongst all interfaces.
3076 	 */
3077 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", looked_at);
3078 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3079 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3080 			/* wrong base scope */
3081 			continue;
3082 		}
3083 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3084 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3085 			    (non_asoc_addr_ok == 0))
3086 				continue;
3087 			sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3088 			    dest_is_loop,
3089 			    dest_is_priv, fam);
3090 			if (sifa == NULL)
3091 				continue;
3092 			if (stcb) {
3093 				if (sctp_is_address_in_scope(sifa,
3094 				    stcb->asoc.ipv4_addr_legal,
3095 				    stcb->asoc.ipv6_addr_legal,
3096 				    stcb->asoc.loopback_scope,
3097 				    stcb->asoc.ipv4_local_scope,
3098 				    stcb->asoc.local_scope,
3099 				    stcb->asoc.site_scope, 0) == 0) {
3100 					sifa = NULL;
3101 					continue;
3102 				}
3103 				if (((non_asoc_addr_ok == 0) &&
3104 				    (sctp_is_addr_restricted(stcb, sifa))) ||
3105 				    (non_asoc_addr_ok &&
3106 				    (sctp_is_addr_restricted(stcb, sifa)) &&
3107 				    (!sctp_is_addr_pending(stcb, sifa)))) {
3108 					/*
3109 					 * It is restricted for some
3110 					 * reason.. probably not yet added.
3111 					 */
3112 					sifa = NULL;
3113 					continue;
3114 				}
3115 			}
3116 			goto out;
3117 		}
3118 	}
3119 #ifdef INET
3120 	if ((retried == 0) && (stcb->asoc.ipv4_local_scope == 0)) {
3121 		stcb->asoc.ipv4_local_scope = 1;
3122 		retried = 1;
3123 		goto again_with_private_addresses_allowed;
3124 	} else if (retried == 1) {
3125 		stcb->asoc.ipv4_local_scope = 0;
3126 	}
3127 #endif
3128 out:
3129 #ifdef INET
3130 	if (sifa) {
3131 		if (retried == 1) {
3132 			LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3133 				if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3134 					/* wrong base scope */
3135 					continue;
3136 				}
3137 				LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3138 					struct sctp_ifa *tmp_sifa;
3139 
3140 					if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3141 					    (non_asoc_addr_ok == 0))
3142 						continue;
3143 					tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3144 					    dest_is_loop,
3145 					    dest_is_priv, fam);
3146 					if (tmp_sifa == NULL) {
3147 						continue;
3148 					}
3149 					if (tmp_sifa == sifa) {
3150 						continue;
3151 					}
3152 					if (stcb) {
3153 						if (sctp_is_address_in_scope(tmp_sifa,
3154 						    stcb->asoc.ipv4_addr_legal,
3155 						    stcb->asoc.ipv6_addr_legal,
3156 						    stcb->asoc.loopback_scope,
3157 						    stcb->asoc.ipv4_local_scope,
3158 						    stcb->asoc.local_scope,
3159 						    stcb->asoc.site_scope, 0) == 0) {
3160 							continue;
3161 						}
3162 						if (((non_asoc_addr_ok == 0) &&
3163 						    (sctp_is_addr_restricted(stcb, tmp_sifa))) ||
3164 						    (non_asoc_addr_ok &&
3165 						    (sctp_is_addr_restricted(stcb, tmp_sifa)) &&
3166 						    (!sctp_is_addr_pending(stcb, tmp_sifa)))) {
3167 							/*
3168 							 * It is restricted
3169 							 * for some reason..
3170 							 * probably not yet
3171 							 * added.
3172 							 */
3173 							continue;
3174 						}
3175 					}
3176 					if ((tmp_sifa->address.sin.sin_family == AF_INET) &&
3177 					    (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) {
3178 						sctp_add_local_addr_restricted(stcb, tmp_sifa);
3179 					}
3180 				}
3181 			}
3182 		}
3183 		atomic_add_int(&sifa->refcount, 1);
3184 	}
3185 #endif
3186 	return (sifa);
3187 }
3188 
3189 
3190 
3191 /* tcb may be NULL */
3192 struct sctp_ifa *
3193 sctp_source_address_selection(struct sctp_inpcb *inp,
3194     struct sctp_tcb *stcb,
3195     sctp_route_t * ro,
3196     struct sctp_nets *net,
3197     int non_asoc_addr_ok, uint32_t vrf_id)
3198 {
3199 	struct sctp_ifa *answer;
3200 	uint8_t dest_is_priv, dest_is_loop;
3201 	sa_family_t fam;
3202 
3203 #ifdef INET
3204 	struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3205 
3206 #endif
3207 #ifdef INET6
3208 	struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3209 
3210 #endif
3211 
3212 	/**
3213 	 * Rules: - Find the route if needed, cache if I can. - Look at
3214 	 * interface address in route, Is it in the bound list. If so we
3215 	 * have the best source. - If not we must rotate amongst the
3216 	 * addresses.
3217 	 *
3218 	 * Cavets and issues
3219 	 *
3220 	 * Do we need to pay attention to scope. We can have a private address
3221 	 * or a global address we are sourcing or sending to. So if we draw
3222 	 * it out
3223 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3224 	 * For V4
3225 	 * ------------------------------------------
3226 	 *      source     *      dest  *  result
3227 	 * -----------------------------------------
3228 	 * <a>  Private    *    Global  *  NAT
3229 	 * -----------------------------------------
3230 	 * <b>  Private    *    Private *  No problem
3231 	 * -----------------------------------------
3232 	 * <c>  Global     *    Private *  Huh, How will this work?
3233 	 * -----------------------------------------
3234 	 * <d>  Global     *    Global  *  No Problem
3235 	 *------------------------------------------
3236 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3237 	 * For V6
3238 	 *------------------------------------------
3239 	 *      source     *      dest  *  result
3240 	 * -----------------------------------------
3241 	 * <a>  Linklocal  *    Global  *
3242 	 * -----------------------------------------
3243 	 * <b>  Linklocal  * Linklocal  *  No problem
3244 	 * -----------------------------------------
3245 	 * <c>  Global     * Linklocal  *  Huh, How will this work?
3246 	 * -----------------------------------------
3247 	 * <d>  Global     *    Global  *  No Problem
3248 	 *------------------------------------------
3249 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3250 	 *
3251 	 * And then we add to that what happens if there are multiple addresses
3252 	 * assigned to an interface. Remember the ifa on a ifn is a linked
3253 	 * list of addresses. So one interface can have more than one IP
3254 	 * address. What happens if we have both a private and a global
3255 	 * address? Do we then use context of destination to sort out which
3256 	 * one is best? And what about NAT's sending P->G may get you a NAT
3257 	 * translation, or should you select the G thats on the interface in
3258 	 * preference.
3259 	 *
3260 	 * Decisions:
3261 	 *
3262 	 * - count the number of addresses on the interface.
3263 	 * - if it is one, no problem except case <c>.
3264 	 *   For <a> we will assume a NAT out there.
3265 	 * - if there are more than one, then we need to worry about scope P
3266 	 *   or G. We should prefer G -> G and P -> P if possible.
3267 	 *   Then as a secondary fall back to mixed types G->P being a last
3268 	 *   ditch one.
3269 	 * - The above all works for bound all, but bound specific we need to
3270 	 *   use the same concept but instead only consider the bound
3271 	 *   addresses. If the bound set is NOT assigned to the interface then
3272 	 *   we must use rotation amongst the bound addresses..
3273 	 */
3274 	if (ro->ro_rt == NULL) {
3275 		/*
3276 		 * Need a route to cache.
3277 		 */
3278 		SCTP_RTALLOC(ro, vrf_id);
3279 	}
3280 	if (ro->ro_rt == NULL) {
3281 		return (NULL);
3282 	}
3283 	fam = ro->ro_dst.sa_family;
3284 	dest_is_priv = dest_is_loop = 0;
3285 	/* Setup our scopes for the destination */
3286 	switch (fam) {
3287 #ifdef INET
3288 	case AF_INET:
3289 		/* Scope based on outbound address */
3290 		if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3291 			dest_is_loop = 1;
3292 			if (net != NULL) {
3293 				/* mark it as local */
3294 				net->addr_is_local = 1;
3295 			}
3296 		} else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3297 			dest_is_priv = 1;
3298 		}
3299 		break;
3300 #endif
3301 #ifdef INET6
3302 	case AF_INET6:
3303 		/* Scope based on outbound address */
3304 		if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3305 		    SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3306 			/*
3307 			 * If the address is a loopback address, which
3308 			 * consists of "::1" OR "fe80::1%lo0", we are
3309 			 * loopback scope. But we don't use dest_is_priv
3310 			 * (link local addresses).
3311 			 */
3312 			dest_is_loop = 1;
3313 			if (net != NULL) {
3314 				/* mark it as local */
3315 				net->addr_is_local = 1;
3316 			}
3317 		} else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3318 			dest_is_priv = 1;
3319 		}
3320 		break;
3321 #endif
3322 	}
3323 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3324 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst);
3325 	SCTP_IPI_ADDR_RLOCK();
3326 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3327 		/*
3328 		 * Bound all case
3329 		 */
3330 		answer = sctp_choose_boundall(stcb, net, ro, vrf_id,
3331 		    dest_is_priv, dest_is_loop,
3332 		    non_asoc_addr_ok, fam);
3333 		SCTP_IPI_ADDR_RUNLOCK();
3334 		return (answer);
3335 	}
3336 	/*
3337 	 * Subset bound case
3338 	 */
3339 	if (stcb) {
3340 		answer = sctp_choose_boundspecific_stcb(inp, stcb, ro,
3341 		    vrf_id, dest_is_priv,
3342 		    dest_is_loop,
3343 		    non_asoc_addr_ok, fam);
3344 	} else {
3345 		answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3346 		    non_asoc_addr_ok,
3347 		    dest_is_priv,
3348 		    dest_is_loop, fam);
3349 	}
3350 	SCTP_IPI_ADDR_RUNLOCK();
3351 	return (answer);
3352 }
3353 
3354 static int
3355 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, size_t cpsize)
3356 {
3357 	struct cmsghdr cmh;
3358 	int tlen, at, found;
3359 	struct sctp_sndinfo sndinfo;
3360 	struct sctp_prinfo prinfo;
3361 	struct sctp_authinfo authinfo;
3362 
3363 	tlen = SCTP_BUF_LEN(control);
3364 	at = 0;
3365 	found = 0;
3366 	/*
3367 	 * Independent of how many mbufs, find the c_type inside the control
3368 	 * structure and copy out the data.
3369 	 */
3370 	while (at < tlen) {
3371 		if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3372 			/* There is not enough room for one more. */
3373 			return (found);
3374 		}
3375 		m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3376 		if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) {
3377 			/* We dont't have a complete CMSG header. */
3378 			return (found);
3379 		}
3380 		if (((int)cmh.cmsg_len + at) > tlen) {
3381 			/* We don't have the complete CMSG. */
3382 			return (found);
3383 		}
3384 		if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3385 		    ((c_type == cmh.cmsg_type) ||
3386 		    ((c_type == SCTP_SNDRCV) &&
3387 		    ((cmh.cmsg_type == SCTP_SNDINFO) ||
3388 		    (cmh.cmsg_type == SCTP_PRINFO) ||
3389 		    (cmh.cmsg_type == SCTP_AUTHINFO))))) {
3390 			if (c_type == cmh.cmsg_type) {
3391 				if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3392 					return (found);
3393 				}
3394 				/* It is exactly what we want. Copy it out. */
3395 				m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), cpsize, (caddr_t)data);
3396 				return (1);
3397 			} else {
3398 				struct sctp_sndrcvinfo *sndrcvinfo;
3399 
3400 				sndrcvinfo = (struct sctp_sndrcvinfo *)data;
3401 				if (found == 0) {
3402 					if (cpsize < sizeof(struct sctp_sndrcvinfo)) {
3403 						return (found);
3404 					}
3405 					memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo));
3406 				}
3407 				switch (cmh.cmsg_type) {
3408 				case SCTP_SNDINFO:
3409 					if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_sndinfo)) {
3410 						return (found);
3411 					}
3412 					m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_sndinfo), (caddr_t)&sndinfo);
3413 					sndrcvinfo->sinfo_stream = sndinfo.snd_sid;
3414 					sndrcvinfo->sinfo_flags = sndinfo.snd_flags;
3415 					sndrcvinfo->sinfo_ppid = sndinfo.snd_ppid;
3416 					sndrcvinfo->sinfo_context = sndinfo.snd_context;
3417 					sndrcvinfo->sinfo_assoc_id = sndinfo.snd_assoc_id;
3418 					break;
3419 				case SCTP_PRINFO:
3420 					if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_prinfo)) {
3421 						return (found);
3422 					}
3423 					m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_prinfo), (caddr_t)&prinfo);
3424 					sndrcvinfo->sinfo_timetolive = prinfo.pr_value;
3425 					sndrcvinfo->sinfo_flags |= prinfo.pr_policy;
3426 					break;
3427 				case SCTP_AUTHINFO:
3428 					if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_authinfo)) {
3429 						return (found);
3430 					}
3431 					m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_authinfo), (caddr_t)&authinfo);
3432 					sndrcvinfo->sinfo_keynumber_valid = 1;
3433 					sndrcvinfo->sinfo_keynumber = authinfo.auth_keyid;
3434 					break;
3435 				default:
3436 					return (found);
3437 				}
3438 				found = 1;
3439 			}
3440 		}
3441 		at += CMSG_ALIGN(cmh.cmsg_len);
3442 	}
3443 	return (found);
3444 }
3445 
3446 static int
3447 sctp_process_cmsgs_for_init(struct sctp_tcb *stcb, struct mbuf *control, int *error)
3448 {
3449 	struct cmsghdr cmh;
3450 	int tlen, at;
3451 	struct sctp_initmsg initmsg;
3452 
3453 #ifdef INET
3454 	struct sockaddr_in sin;
3455 
3456 #endif
3457 #ifdef INET6
3458 	struct sockaddr_in6 sin6;
3459 
3460 #endif
3461 
3462 	tlen = SCTP_BUF_LEN(control);
3463 	at = 0;
3464 	while (at < tlen) {
3465 		if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3466 			/* There is not enough room for one more. */
3467 			*error = EINVAL;
3468 			return (1);
3469 		}
3470 		m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3471 		if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) {
3472 			/* We dont't have a complete CMSG header. */
3473 			*error = EINVAL;
3474 			return (1);
3475 		}
3476 		if (((int)cmh.cmsg_len + at) > tlen) {
3477 			/* We don't have the complete CMSG. */
3478 			*error = EINVAL;
3479 			return (1);
3480 		}
3481 		if (cmh.cmsg_level == IPPROTO_SCTP) {
3482 			switch (cmh.cmsg_type) {
3483 			case SCTP_INIT:
3484 				if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_initmsg)) {
3485 					*error = EINVAL;
3486 					return (1);
3487 				}
3488 				m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_initmsg), (caddr_t)&initmsg);
3489 				if (initmsg.sinit_max_attempts)
3490 					stcb->asoc.max_init_times = initmsg.sinit_max_attempts;
3491 				if (initmsg.sinit_num_ostreams)
3492 					stcb->asoc.pre_open_streams = initmsg.sinit_num_ostreams;
3493 				if (initmsg.sinit_max_instreams)
3494 					stcb->asoc.max_inbound_streams = initmsg.sinit_max_instreams;
3495 				if (initmsg.sinit_max_init_timeo)
3496 					stcb->asoc.initial_init_rto_max = initmsg.sinit_max_init_timeo;
3497 				if (stcb->asoc.streamoutcnt < stcb->asoc.pre_open_streams) {
3498 					struct sctp_stream_out *tmp_str;
3499 					unsigned int i;
3500 
3501 					/* Default is NOT correct */
3502 					SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, default:%d pre_open:%d\n",
3503 					    stcb->asoc.streamoutcnt, stcb->asoc.pre_open_streams);
3504 					SCTP_TCB_UNLOCK(stcb);
3505 					SCTP_MALLOC(tmp_str,
3506 					    struct sctp_stream_out *,
3507 					    (stcb->asoc.pre_open_streams * sizeof(struct sctp_stream_out)),
3508 					    SCTP_M_STRMO);
3509 					SCTP_TCB_LOCK(stcb);
3510 					if (tmp_str != NULL) {
3511 						SCTP_FREE(stcb->asoc.strmout, SCTP_M_STRMO);
3512 						stcb->asoc.strmout = tmp_str;
3513 						stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt = stcb->asoc.pre_open_streams;
3514 					} else {
3515 						stcb->asoc.pre_open_streams = stcb->asoc.streamoutcnt;
3516 					}
3517 					for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3518 						stcb->asoc.strmout[i].next_sequence_sent = 0;
3519 						TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3520 						stcb->asoc.strmout[i].stream_no = i;
3521 						stcb->asoc.strmout[i].last_msg_incomplete = 0;
3522 						stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], NULL);
3523 					}
3524 				}
3525 				break;
3526 #ifdef INET
3527 			case SCTP_DSTADDRV4:
3528 				if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in_addr)) {
3529 					*error = EINVAL;
3530 					return (1);
3531 				}
3532 				memset(&sin, 0, sizeof(struct sockaddr_in));
3533 				sin.sin_family = AF_INET;
3534 				sin.sin_len = sizeof(struct sockaddr_in);
3535 				sin.sin_port = stcb->rport;
3536 				m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3537 				if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3538 				    (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3539 				    IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3540 					*error = EINVAL;
3541 					return (1);
3542 				}
3543 				if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL,
3544 				    SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3545 					*error = ENOBUFS;
3546 					return (1);
3547 				}
3548 				break;
3549 #endif
3550 #ifdef INET6
3551 			case SCTP_DSTADDRV6:
3552 				if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in6_addr)) {
3553 					*error = EINVAL;
3554 					return (1);
3555 				}
3556 				memset(&sin6, 0, sizeof(struct sockaddr_in6));
3557 				sin6.sin6_family = AF_INET6;
3558 				sin6.sin6_len = sizeof(struct sockaddr_in6);
3559 				sin6.sin6_port = stcb->rport;
3560 				m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3561 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) ||
3562 				    IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
3563 					*error = EINVAL;
3564 					return (1);
3565 				}
3566 #ifdef INET
3567 				if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3568 					in6_sin6_2_sin(&sin, &sin6);
3569 					if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3570 					    (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3571 					    IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3572 						*error = EINVAL;
3573 						return (1);
3574 					}
3575 					if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL,
3576 					    SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3577 						*error = ENOBUFS;
3578 						return (1);
3579 					}
3580 				} else
3581 #endif
3582 					if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin6, NULL,
3583 				    SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3584 					*error = ENOBUFS;
3585 					return (1);
3586 				}
3587 				break;
3588 #endif
3589 			default:
3590 				break;
3591 			}
3592 		}
3593 		at += CMSG_ALIGN(cmh.cmsg_len);
3594 	}
3595 	return (0);
3596 }
3597 
3598 static struct sctp_tcb *
3599 sctp_findassociation_cmsgs(struct sctp_inpcb **inp_p,
3600     in_port_t port,
3601     struct mbuf *control,
3602     struct sctp_nets **net_p,
3603     int *error)
3604 {
3605 	struct cmsghdr cmh;
3606 	int tlen, at;
3607 	struct sctp_tcb *stcb;
3608 	struct sockaddr *addr;
3609 
3610 #ifdef INET
3611 	struct sockaddr_in sin;
3612 
3613 #endif
3614 #ifdef INET6
3615 	struct sockaddr_in6 sin6;
3616 
3617 #endif
3618 
3619 	tlen = SCTP_BUF_LEN(control);
3620 	at = 0;
3621 	while (at < tlen) {
3622 		if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3623 			/* There is not enough room for one more. */
3624 			*error = EINVAL;
3625 			return (NULL);
3626 		}
3627 		m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3628 		if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) {
3629 			/* We dont't have a complete CMSG header. */
3630 			*error = EINVAL;
3631 			return (NULL);
3632 		}
3633 		if (((int)cmh.cmsg_len + at) > tlen) {
3634 			/* We don't have the complete CMSG. */
3635 			*error = EINVAL;
3636 			return (NULL);
3637 		}
3638 		if (cmh.cmsg_level == IPPROTO_SCTP) {
3639 			switch (cmh.cmsg_type) {
3640 #ifdef INET
3641 			case SCTP_DSTADDRV4:
3642 				if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in_addr)) {
3643 					*error = EINVAL;
3644 					return (NULL);
3645 				}
3646 				memset(&sin, 0, sizeof(struct sockaddr_in));
3647 				sin.sin_family = AF_INET;
3648 				sin.sin_len = sizeof(struct sockaddr_in);
3649 				sin.sin_port = port;
3650 				m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3651 				addr = (struct sockaddr *)&sin;
3652 				break;
3653 #endif
3654 #ifdef INET6
3655 			case SCTP_DSTADDRV6:
3656 				if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in6_addr)) {
3657 					*error = EINVAL;
3658 					return (NULL);
3659 				}
3660 				memset(&sin6, 0, sizeof(struct sockaddr_in6));
3661 				sin6.sin6_family = AF_INET6;
3662 				sin6.sin6_len = sizeof(struct sockaddr_in6);
3663 				sin6.sin6_port = port;
3664 				m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3665 #ifdef INET
3666 				if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3667 					in6_sin6_2_sin(&sin, &sin6);
3668 					addr = (struct sockaddr *)&sin;
3669 				} else
3670 #endif
3671 					addr = (struct sockaddr *)&sin6;
3672 				break;
3673 #endif
3674 			default:
3675 				addr = NULL;
3676 				break;
3677 			}
3678 			if (addr) {
3679 				stcb = sctp_findassociation_ep_addr(inp_p, addr, net_p, NULL, NULL);
3680 				if (stcb != NULL) {
3681 					return (stcb);
3682 				}
3683 			}
3684 		}
3685 		at += CMSG_ALIGN(cmh.cmsg_len);
3686 	}
3687 	return (NULL);
3688 }
3689 
3690 static struct mbuf *
3691 sctp_add_cookie(struct mbuf *init, int init_offset,
3692     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3693 {
3694 	struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3695 	struct sctp_state_cookie *stc;
3696 	struct sctp_paramhdr *ph;
3697 	uint8_t *foo;
3698 	int sig_offset;
3699 	uint16_t cookie_sz;
3700 
3701 	mret = NULL;
3702 	mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3703 	    sizeof(struct sctp_paramhdr)), 0,
3704 	    M_DONTWAIT, 1, MT_DATA);
3705 	if (mret == NULL) {
3706 		return (NULL);
3707 	}
3708 	copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3709 	if (copy_init == NULL) {
3710 		sctp_m_freem(mret);
3711 		return (NULL);
3712 	}
3713 #ifdef SCTP_MBUF_LOGGING
3714 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3715 		struct mbuf *mat;
3716 
3717 		mat = copy_init;
3718 		while (mat) {
3719 			if (SCTP_BUF_IS_EXTENDED(mat)) {
3720 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3721 			}
3722 			mat = SCTP_BUF_NEXT(mat);
3723 		}
3724 	}
3725 #endif
3726 	copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3727 	    M_DONTWAIT);
3728 	if (copy_initack == NULL) {
3729 		sctp_m_freem(mret);
3730 		sctp_m_freem(copy_init);
3731 		return (NULL);
3732 	}
3733 #ifdef SCTP_MBUF_LOGGING
3734 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3735 		struct mbuf *mat;
3736 
3737 		mat = copy_initack;
3738 		while (mat) {
3739 			if (SCTP_BUF_IS_EXTENDED(mat)) {
3740 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3741 			}
3742 			mat = SCTP_BUF_NEXT(mat);
3743 		}
3744 	}
3745 #endif
3746 	/* easy side we just drop it on the end */
3747 	ph = mtod(mret, struct sctp_paramhdr *);
3748 	SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3749 	    sizeof(struct sctp_paramhdr);
3750 	stc = (struct sctp_state_cookie *)((caddr_t)ph +
3751 	    sizeof(struct sctp_paramhdr));
3752 	ph->param_type = htons(SCTP_STATE_COOKIE);
3753 	ph->param_length = 0;	/* fill in at the end */
3754 	/* Fill in the stc cookie data */
3755 	memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3756 
3757 	/* tack the INIT and then the INIT-ACK onto the chain */
3758 	cookie_sz = 0;
3759 	for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3760 		cookie_sz += SCTP_BUF_LEN(m_at);
3761 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3762 			SCTP_BUF_NEXT(m_at) = copy_init;
3763 			break;
3764 		}
3765 	}
3766 	for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3767 		cookie_sz += SCTP_BUF_LEN(m_at);
3768 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3769 			SCTP_BUF_NEXT(m_at) = copy_initack;
3770 			break;
3771 		}
3772 	}
3773 	for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3774 		cookie_sz += SCTP_BUF_LEN(m_at);
3775 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3776 			break;
3777 		}
3778 	}
3779 	sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3780 	if (sig == NULL) {
3781 		/* no space, so free the entire chain */
3782 		sctp_m_freem(mret);
3783 		return (NULL);
3784 	}
3785 	SCTP_BUF_LEN(sig) = 0;
3786 	SCTP_BUF_NEXT(m_at) = sig;
3787 	sig_offset = 0;
3788 	foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3789 	memset(foo, 0, SCTP_SIGNATURE_SIZE);
3790 	*signature = foo;
3791 	SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3792 	cookie_sz += SCTP_SIGNATURE_SIZE;
3793 	ph->param_length = htons(cookie_sz);
3794 	return (mret);
3795 }
3796 
3797 
3798 static uint8_t
3799 sctp_get_ect(struct sctp_tcb *stcb)
3800 {
3801 	if ((stcb != NULL) && (stcb->asoc.ecn_allowed == 1)) {
3802 		return (SCTP_ECT0_BIT);
3803 	} else {
3804 		return (0);
3805 	}
3806 }
3807 
3808 static void
3809 sctp_handle_no_route(struct sctp_tcb *stcb,
3810     struct sctp_nets *net,
3811     int so_locked)
3812 {
3813 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n");
3814 
3815 	if (net) {
3816 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was ");
3817 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa);
3818 		if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3819 			if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3820 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3821 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3822 				    stcb,
3823 				    SCTP_FAILED_THRESHOLD,
3824 				    (void *)net,
3825 				    so_locked);
3826 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
3827 				net->dest_state &= ~SCTP_ADDR_PF;
3828 			}
3829 		}
3830 		if (stcb) {
3831 			if (net == stcb->asoc.primary_destination) {
3832 				/* need a new primary */
3833 				struct sctp_nets *alt;
3834 
3835 				alt = sctp_find_alternate_net(stcb, net, 0);
3836 				if (alt != net) {
3837 					if (stcb->asoc.alternate) {
3838 						sctp_free_remote_addr(stcb->asoc.alternate);
3839 					}
3840 					stcb->asoc.alternate = alt;
3841 					atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
3842 					if (net->ro._s_addr) {
3843 						sctp_free_ifa(net->ro._s_addr);
3844 						net->ro._s_addr = NULL;
3845 					}
3846 					net->src_addr_selected = 0;
3847 				}
3848 			}
3849 		}
3850 	}
3851 }
3852 
3853 static int
3854 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3855     struct sctp_tcb *stcb,	/* may be NULL */
3856     struct sctp_nets *net,
3857     struct sockaddr *to,
3858     struct mbuf *m,
3859     uint32_t auth_offset,
3860     struct sctp_auth_chunk *auth,
3861     uint16_t auth_keyid,
3862     int nofragment_flag,
3863     int ecn_ok,
3864     int out_of_asoc_ok,
3865     uint16_t src_port,
3866     uint16_t dest_port,
3867     uint32_t v_tag,
3868     uint16_t port,
3869 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3870     int so_locked SCTP_UNUSED,
3871 #else
3872     int so_locked,
3873 #endif
3874     union sctp_sockstore *over_addr,
3875     struct mbuf *init
3876 )
3877 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3878 {
3879 	/**
3880 	 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet header
3881 	 * WITH an SCTPHDR but no IP header, endpoint inp and sa structure:
3882 	 * - fill in the HMAC digest of any AUTH chunk in the packet.
3883 	 * - calculate and fill in the SCTP checksum.
3884 	 * - prepend an IP address header.
3885 	 * - if boundall use INADDR_ANY.
3886 	 * - if boundspecific do source address selection.
3887 	 * - set fragmentation option for ipV4.
3888 	 * - On return from IP output, check/adjust mtu size of output
3889 	 *   interface and smallest_mtu size as well.
3890 	 */
3891 	/* Will need ifdefs around this */
3892 	struct mbuf *o_pak;
3893 	struct mbuf *newm;
3894 	struct sctphdr *sctphdr;
3895 	int packet_length;
3896 	int ret;
3897 	uint32_t vrf_id;
3898 	sctp_route_t *ro = NULL;
3899 	struct udphdr *udp = NULL;
3900 	uint8_t tos_value;
3901 
3902 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3903 	struct socket *so = NULL;
3904 
3905 #endif
3906 
3907 	if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3908 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3909 		sctp_m_freem(m);
3910 		return (EFAULT);
3911 	}
3912 	if (stcb) {
3913 		vrf_id = stcb->asoc.vrf_id;
3914 	} else {
3915 		vrf_id = inp->def_vrf_id;
3916 	}
3917 
3918 	/* fill in the HMAC digest for any AUTH chunk in the packet */
3919 	if ((auth != NULL) && (stcb != NULL)) {
3920 		sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
3921 	}
3922 	if (net) {
3923 		tos_value = net->dscp;
3924 	} else if (stcb) {
3925 		tos_value = stcb->asoc.default_dscp;
3926 	} else {
3927 		tos_value = inp->sctp_ep.default_dscp;
3928 	}
3929 
3930 	switch (to->sa_family) {
3931 #ifdef INET
3932 	case AF_INET:
3933 		{
3934 			struct ip *ip = NULL;
3935 			sctp_route_t iproute;
3936 			int len;
3937 
3938 			len = sizeof(struct ip) + sizeof(struct sctphdr);
3939 			if (port) {
3940 				len += sizeof(struct udphdr);
3941 			}
3942 			newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3943 			if (newm == NULL) {
3944 				sctp_m_freem(m);
3945 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3946 				return (ENOMEM);
3947 			}
3948 			SCTP_ALIGN_TO_END(newm, len);
3949 			SCTP_BUF_LEN(newm) = len;
3950 			SCTP_BUF_NEXT(newm) = m;
3951 			m = newm;
3952 			if (net != NULL) {
3953 #ifdef INVARIANTS
3954 				if (net->flowidset == 0) {
3955 					panic("Flow ID not set");
3956 				}
3957 #endif
3958 				m->m_pkthdr.flowid = net->flowid;
3959 				m->m_flags |= M_FLOWID;
3960 			} else {
3961 				if ((init != NULL) && (init->m_flags & M_FLOWID)) {
3962 					m->m_pkthdr.flowid = init->m_pkthdr.flowid;
3963 					m->m_flags |= M_FLOWID;
3964 				}
3965 			}
3966 			packet_length = sctp_calculate_len(m);
3967 			ip = mtod(m, struct ip *);
3968 			ip->ip_v = IPVERSION;
3969 			ip->ip_hl = (sizeof(struct ip) >> 2);
3970 			if (tos_value == 0) {
3971 				/*
3972 				 * This means especially, that it is not set
3973 				 * at the SCTP layer. So use the value from
3974 				 * the IP layer.
3975 				 */
3976 				tos_value = inp->ip_inp.inp.inp_ip_tos;
3977 			}
3978 			tos_value &= 0xfc;
3979 			if (ecn_ok) {
3980 				tos_value |= sctp_get_ect(stcb);
3981 			}
3982 			if ((nofragment_flag) && (port == 0)) {
3983 				ip->ip_off = IP_DF;
3984 			} else
3985 				ip->ip_off = 0;
3986 
3987 			/* FreeBSD has a function for ip_id's */
3988 			ip->ip_id = ip_newid();
3989 
3990 			ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3991 			ip->ip_len = packet_length;
3992 			ip->ip_tos = tos_value;
3993 			if (port) {
3994 				ip->ip_p = IPPROTO_UDP;
3995 			} else {
3996 				ip->ip_p = IPPROTO_SCTP;
3997 			}
3998 			ip->ip_sum = 0;
3999 			if (net == NULL) {
4000 				ro = &iproute;
4001 				memset(&iproute, 0, sizeof(iproute));
4002 				memcpy(&ro->ro_dst, to, to->sa_len);
4003 			} else {
4004 				ro = (sctp_route_t *) & net->ro;
4005 			}
4006 			/* Now the address selection part */
4007 			ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
4008 
4009 			/* call the routine to select the src address */
4010 			if (net && out_of_asoc_ok == 0) {
4011 				if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4012 					sctp_free_ifa(net->ro._s_addr);
4013 					net->ro._s_addr = NULL;
4014 					net->src_addr_selected = 0;
4015 					if (ro->ro_rt) {
4016 						RTFREE(ro->ro_rt);
4017 						ro->ro_rt = NULL;
4018 					}
4019 				}
4020 				if (net->src_addr_selected == 0) {
4021 					/* Cache the source address */
4022 					net->ro._s_addr = sctp_source_address_selection(inp, stcb,
4023 					    ro, net, 0,
4024 					    vrf_id);
4025 					net->src_addr_selected = 1;
4026 				}
4027 				if (net->ro._s_addr == NULL) {
4028 					/* No route to host */
4029 					net->src_addr_selected = 0;
4030 					sctp_handle_no_route(stcb, net, so_locked);
4031 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4032 					sctp_m_freem(m);
4033 					return (EHOSTUNREACH);
4034 				}
4035 				ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
4036 			} else {
4037 				if (over_addr == NULL) {
4038 					struct sctp_ifa *_lsrc;
4039 
4040 					_lsrc = sctp_source_address_selection(inp, stcb, ro,
4041 					    net,
4042 					    out_of_asoc_ok,
4043 					    vrf_id);
4044 					if (_lsrc == NULL) {
4045 						sctp_handle_no_route(stcb, net, so_locked);
4046 						SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4047 						sctp_m_freem(m);
4048 						return (EHOSTUNREACH);
4049 					}
4050 					ip->ip_src = _lsrc->address.sin.sin_addr;
4051 					sctp_free_ifa(_lsrc);
4052 				} else {
4053 					ip->ip_src = over_addr->sin.sin_addr;
4054 					SCTP_RTALLOC(ro, vrf_id);
4055 				}
4056 			}
4057 			if (port) {
4058 				if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4059 					sctp_handle_no_route(stcb, net, so_locked);
4060 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4061 					sctp_m_freem(m);
4062 					return (EHOSTUNREACH);
4063 				}
4064 				udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
4065 				udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4066 				udp->uh_dport = port;
4067 				udp->uh_ulen = htons(packet_length - sizeof(struct ip));
4068 				if (V_udp_cksum) {
4069 					udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
4070 				} else {
4071 					udp->uh_sum = 0;
4072 				}
4073 				sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4074 			} else {
4075 				sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
4076 			}
4077 
4078 			sctphdr->src_port = src_port;
4079 			sctphdr->dest_port = dest_port;
4080 			sctphdr->v_tag = v_tag;
4081 			sctphdr->checksum = 0;
4082 
4083 			/*
4084 			 * If source address selection fails and we find no
4085 			 * route then the ip_output should fail as well with
4086 			 * a NO_ROUTE_TO_HOST type error. We probably should
4087 			 * catch that somewhere and abort the association
4088 			 * right away (assuming this is an INIT being sent).
4089 			 */
4090 			if (ro->ro_rt == NULL) {
4091 				/*
4092 				 * src addr selection failed to find a route
4093 				 * (or valid source addr), so we can't get
4094 				 * there from here (yet)!
4095 				 */
4096 				sctp_handle_no_route(stcb, net, so_locked);
4097 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4098 				sctp_m_freem(m);
4099 				return (EHOSTUNREACH);
4100 			}
4101 			if (ro != &iproute) {
4102 				memcpy(&iproute, ro, sizeof(*ro));
4103 			}
4104 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
4105 			    (uint32_t) (ntohl(ip->ip_src.s_addr)));
4106 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
4107 			    (uint32_t) (ntohl(ip->ip_dst.s_addr)));
4108 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
4109 			    ro->ro_rt);
4110 
4111 			if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4112 				/* failed to prepend data, give up */
4113 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4114 				sctp_m_freem(m);
4115 				return (ENOMEM);
4116 			}
4117 #ifdef  SCTP_PACKET_LOGGING
4118 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4119 				sctp_packet_log(m, packet_length);
4120 #endif
4121 			SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4122 			if (port) {
4123 #if defined(SCTP_WITH_NO_CSUM)
4124 				SCTP_STAT_INCR(sctps_sendnocrc);
4125 #else
4126 				if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4127 				    (stcb) &&
4128 				    (stcb->asoc.loopback_scope))) {
4129 					sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
4130 					SCTP_STAT_INCR(sctps_sendswcrc);
4131 				} else {
4132 					SCTP_STAT_INCR(sctps_sendnocrc);
4133 				}
4134 #endif
4135 				if (V_udp_cksum) {
4136 					SCTP_ENABLE_UDP_CSUM(o_pak);
4137 				}
4138 			} else {
4139 #if defined(SCTP_WITH_NO_CSUM)
4140 				SCTP_STAT_INCR(sctps_sendnocrc);
4141 #else
4142 				m->m_pkthdr.csum_flags = CSUM_SCTP;
4143 				m->m_pkthdr.csum_data = 0;
4144 				SCTP_STAT_INCR(sctps_sendhwcrc);
4145 #endif
4146 			}
4147 			/* send it out.  table id is taken from stcb */
4148 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4149 			if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4150 				so = SCTP_INP_SO(inp);
4151 				SCTP_SOCKET_UNLOCK(so, 0);
4152 			}
4153 #endif
4154 			SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
4155 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4156 			if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4157 				atomic_add_int(&stcb->asoc.refcnt, 1);
4158 				SCTP_TCB_UNLOCK(stcb);
4159 				SCTP_SOCKET_LOCK(so, 0);
4160 				SCTP_TCB_LOCK(stcb);
4161 				atomic_subtract_int(&stcb->asoc.refcnt, 1);
4162 			}
4163 #endif
4164 			SCTP_STAT_INCR(sctps_sendpackets);
4165 			SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4166 			if (ret)
4167 				SCTP_STAT_INCR(sctps_senderrors);
4168 
4169 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
4170 			if (net == NULL) {
4171 				/* free tempy routes */
4172 				if (ro->ro_rt) {
4173 					RTFREE(ro->ro_rt);
4174 					ro->ro_rt = NULL;
4175 				}
4176 			} else {
4177 				/*
4178 				 * PMTU check versus smallest asoc MTU goes
4179 				 * here
4180 				 */
4181 				if ((ro->ro_rt != NULL) &&
4182 				    (net->ro._s_addr)) {
4183 					uint32_t mtu;
4184 
4185 					mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4186 					if (net->port) {
4187 						mtu -= sizeof(struct udphdr);
4188 					}
4189 					if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
4190 						sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4191 						net->mtu = mtu;
4192 					}
4193 				} else if (ro->ro_rt == NULL) {
4194 					/* route was freed */
4195 					if (net->ro._s_addr &&
4196 					    net->src_addr_selected) {
4197 						sctp_free_ifa(net->ro._s_addr);
4198 						net->ro._s_addr = NULL;
4199 					}
4200 					net->src_addr_selected = 0;
4201 				}
4202 			}
4203 			return (ret);
4204 		}
4205 #endif
4206 #ifdef INET6
4207 	case AF_INET6:
4208 		{
4209 			uint32_t flowlabel, flowinfo;
4210 			struct ip6_hdr *ip6h;
4211 			struct route_in6 ip6route;
4212 			struct ifnet *ifp;
4213 			struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
4214 			int prev_scope = 0;
4215 			struct sockaddr_in6 lsa6_storage;
4216 			int error;
4217 			u_short prev_port = 0;
4218 			int len;
4219 
4220 			if (net) {
4221 				flowlabel = net->flowlabel;
4222 			} else if (stcb) {
4223 				flowlabel = stcb->asoc.default_flowlabel;
4224 			} else {
4225 				flowlabel = inp->sctp_ep.default_flowlabel;
4226 			}
4227 			if (flowlabel == 0) {
4228 				/*
4229 				 * This means especially, that it is not set
4230 				 * at the SCTP layer. So use the value from
4231 				 * the IP layer.
4232 				 */
4233 				flowlabel = ntohl(((struct in6pcb *)inp)->in6p_flowinfo);
4234 			}
4235 			flowlabel &= 0x000fffff;
4236 			len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr);
4237 			if (port) {
4238 				len += sizeof(struct udphdr);
4239 			}
4240 			newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
4241 			if (newm == NULL) {
4242 				sctp_m_freem(m);
4243 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4244 				return (ENOMEM);
4245 			}
4246 			SCTP_ALIGN_TO_END(newm, len);
4247 			SCTP_BUF_LEN(newm) = len;
4248 			SCTP_BUF_NEXT(newm) = m;
4249 			m = newm;
4250 			if (net != NULL) {
4251 #ifdef INVARIANTS
4252 				if (net->flowidset == 0) {
4253 					panic("Flow ID not set");
4254 				}
4255 #endif
4256 				m->m_pkthdr.flowid = net->flowid;
4257 				m->m_flags |= M_FLOWID;
4258 			} else {
4259 				if ((init != NULL) && (init->m_flags & M_FLOWID)) {
4260 					m->m_pkthdr.flowid = init->m_pkthdr.flowid;
4261 					m->m_flags |= M_FLOWID;
4262 				}
4263 			}
4264 			packet_length = sctp_calculate_len(m);
4265 
4266 			ip6h = mtod(m, struct ip6_hdr *);
4267 			/* protect *sin6 from overwrite */
4268 			sin6 = (struct sockaddr_in6 *)to;
4269 			tmp = *sin6;
4270 			sin6 = &tmp;
4271 
4272 			/* KAME hack: embed scopeid */
4273 			if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4274 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4275 				return (EINVAL);
4276 			}
4277 			if (net == NULL) {
4278 				memset(&ip6route, 0, sizeof(ip6route));
4279 				ro = (sctp_route_t *) & ip6route;
4280 				memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
4281 			} else {
4282 				ro = (sctp_route_t *) & net->ro;
4283 			}
4284 			/*
4285 			 * We assume here that inp_flow is in host byte
4286 			 * order within the TCB!
4287 			 */
4288 			if (tos_value == 0) {
4289 				/*
4290 				 * This means especially, that it is not set
4291 				 * at the SCTP layer. So use the value from
4292 				 * the IP layer.
4293 				 */
4294 				tos_value = (ntohl(((struct in6pcb *)inp)->in6p_flowinfo) >> 20) & 0xff;
4295 			}
4296 			tos_value &= 0xfc;
4297 			if (ecn_ok) {
4298 				tos_value |= sctp_get_ect(stcb);
4299 			}
4300 			flowinfo = 0x06;
4301 			flowinfo <<= 8;
4302 			flowinfo |= tos_value;
4303 			flowinfo <<= 20;
4304 			flowinfo |= flowlabel;
4305 			ip6h->ip6_flow = htonl(flowinfo);
4306 			if (port) {
4307 				ip6h->ip6_nxt = IPPROTO_UDP;
4308 			} else {
4309 				ip6h->ip6_nxt = IPPROTO_SCTP;
4310 			}
4311 			ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
4312 			ip6h->ip6_dst = sin6->sin6_addr;
4313 
4314 			/*
4315 			 * Add SRC address selection here: we can only reuse
4316 			 * to a limited degree the kame src-addr-sel, since
4317 			 * we can try their selection but it may not be
4318 			 * bound.
4319 			 */
4320 			bzero(&lsa6_tmp, sizeof(lsa6_tmp));
4321 			lsa6_tmp.sin6_family = AF_INET6;
4322 			lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
4323 			lsa6 = &lsa6_tmp;
4324 			if (net && out_of_asoc_ok == 0) {
4325 				if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4326 					sctp_free_ifa(net->ro._s_addr);
4327 					net->ro._s_addr = NULL;
4328 					net->src_addr_selected = 0;
4329 					if (ro->ro_rt) {
4330 						RTFREE(ro->ro_rt);
4331 						ro->ro_rt = NULL;
4332 					}
4333 				}
4334 				if (net->src_addr_selected == 0) {
4335 					sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4336 					/* KAME hack: embed scopeid */
4337 					if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4338 						SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4339 						return (EINVAL);
4340 					}
4341 					/* Cache the source address */
4342 					net->ro._s_addr = sctp_source_address_selection(inp,
4343 					    stcb,
4344 					    ro,
4345 					    net,
4346 					    0,
4347 					    vrf_id);
4348 					(void)sa6_recoverscope(sin6);
4349 					net->src_addr_selected = 1;
4350 				}
4351 				if (net->ro._s_addr == NULL) {
4352 					SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
4353 					net->src_addr_selected = 0;
4354 					sctp_handle_no_route(stcb, net, so_locked);
4355 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4356 					sctp_m_freem(m);
4357 					return (EHOSTUNREACH);
4358 				}
4359 				lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
4360 			} else {
4361 				sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
4362 				/* KAME hack: embed scopeid */
4363 				if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4364 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4365 					return (EINVAL);
4366 				}
4367 				if (over_addr == NULL) {
4368 					struct sctp_ifa *_lsrc;
4369 
4370 					_lsrc = sctp_source_address_selection(inp, stcb, ro,
4371 					    net,
4372 					    out_of_asoc_ok,
4373 					    vrf_id);
4374 					if (_lsrc == NULL) {
4375 						sctp_handle_no_route(stcb, net, so_locked);
4376 						SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4377 						sctp_m_freem(m);
4378 						return (EHOSTUNREACH);
4379 					}
4380 					lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
4381 					sctp_free_ifa(_lsrc);
4382 				} else {
4383 					lsa6->sin6_addr = over_addr->sin6.sin6_addr;
4384 					SCTP_RTALLOC(ro, vrf_id);
4385 				}
4386 				(void)sa6_recoverscope(sin6);
4387 			}
4388 			lsa6->sin6_port = inp->sctp_lport;
4389 
4390 			if (ro->ro_rt == NULL) {
4391 				/*
4392 				 * src addr selection failed to find a route
4393 				 * (or valid source addr), so we can't get
4394 				 * there from here!
4395 				 */
4396 				sctp_handle_no_route(stcb, net, so_locked);
4397 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4398 				sctp_m_freem(m);
4399 				return (EHOSTUNREACH);
4400 			}
4401 			/*
4402 			 * XXX: sa6 may not have a valid sin6_scope_id in
4403 			 * the non-SCOPEDROUTING case.
4404 			 */
4405 			bzero(&lsa6_storage, sizeof(lsa6_storage));
4406 			lsa6_storage.sin6_family = AF_INET6;
4407 			lsa6_storage.sin6_len = sizeof(lsa6_storage);
4408 			lsa6_storage.sin6_addr = lsa6->sin6_addr;
4409 			if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
4410 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
4411 				sctp_m_freem(m);
4412 				return (error);
4413 			}
4414 			/* XXX */
4415 			lsa6_storage.sin6_addr = lsa6->sin6_addr;
4416 			lsa6_storage.sin6_port = inp->sctp_lport;
4417 			lsa6 = &lsa6_storage;
4418 			ip6h->ip6_src = lsa6->sin6_addr;
4419 
4420 			if (port) {
4421 				if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4422 					sctp_handle_no_route(stcb, net, so_locked);
4423 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4424 					sctp_m_freem(m);
4425 					return (EHOSTUNREACH);
4426 				}
4427 				udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4428 				udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4429 				udp->uh_dport = port;
4430 				udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
4431 				udp->uh_sum = 0;
4432 				sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4433 			} else {
4434 				sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4435 			}
4436 
4437 			sctphdr->src_port = src_port;
4438 			sctphdr->dest_port = dest_port;
4439 			sctphdr->v_tag = v_tag;
4440 			sctphdr->checksum = 0;
4441 
4442 			/*
4443 			 * We set the hop limit now since there is a good
4444 			 * chance that our ro pointer is now filled
4445 			 */
4446 			ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
4447 			ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4448 
4449 #ifdef SCTP_DEBUG
4450 			/* Copy to be sure something bad is not happening */
4451 			sin6->sin6_addr = ip6h->ip6_dst;
4452 			lsa6->sin6_addr = ip6h->ip6_src;
4453 #endif
4454 
4455 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
4456 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
4457 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
4458 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
4459 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
4460 			if (net) {
4461 				sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4462 				/*
4463 				 * preserve the port and scope for link
4464 				 * local send
4465 				 */
4466 				prev_scope = sin6->sin6_scope_id;
4467 				prev_port = sin6->sin6_port;
4468 			}
4469 			if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4470 				/* failed to prepend data, give up */
4471 				sctp_m_freem(m);
4472 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4473 				return (ENOMEM);
4474 			}
4475 #ifdef  SCTP_PACKET_LOGGING
4476 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4477 				sctp_packet_log(m, packet_length);
4478 #endif
4479 			SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4480 			if (port) {
4481 #if defined(SCTP_WITH_NO_CSUM)
4482 				SCTP_STAT_INCR(sctps_sendnocrc);
4483 #else
4484 				if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4485 				    (stcb) &&
4486 				    (stcb->asoc.loopback_scope))) {
4487 					sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4488 					SCTP_STAT_INCR(sctps_sendswcrc);
4489 				} else {
4490 					SCTP_STAT_INCR(sctps_sendnocrc);
4491 				}
4492 #endif
4493 				if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4494 					udp->uh_sum = 0xffff;
4495 				}
4496 			} else {
4497 #if defined(SCTP_WITH_NO_CSUM)
4498 				SCTP_STAT_INCR(sctps_sendnocrc);
4499 #else
4500 				m->m_pkthdr.csum_flags = CSUM_SCTP;
4501 				m->m_pkthdr.csum_data = 0;
4502 				SCTP_STAT_INCR(sctps_sendhwcrc);
4503 #endif
4504 			}
4505 			/* send it out. table id is taken from stcb */
4506 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4507 			if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4508 				so = SCTP_INP_SO(inp);
4509 				SCTP_SOCKET_UNLOCK(so, 0);
4510 			}
4511 #endif
4512 			SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4513 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4514 			if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4515 				atomic_add_int(&stcb->asoc.refcnt, 1);
4516 				SCTP_TCB_UNLOCK(stcb);
4517 				SCTP_SOCKET_LOCK(so, 0);
4518 				SCTP_TCB_LOCK(stcb);
4519 				atomic_subtract_int(&stcb->asoc.refcnt, 1);
4520 			}
4521 #endif
4522 			if (net) {
4523 				/* for link local this must be done */
4524 				sin6->sin6_scope_id = prev_scope;
4525 				sin6->sin6_port = prev_port;
4526 			}
4527 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4528 			SCTP_STAT_INCR(sctps_sendpackets);
4529 			SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4530 			if (ret) {
4531 				SCTP_STAT_INCR(sctps_senderrors);
4532 			}
4533 			if (net == NULL) {
4534 				/* Now if we had a temp route free it */
4535 				if (ro->ro_rt) {
4536 					RTFREE(ro->ro_rt);
4537 				}
4538 			} else {
4539 				/*
4540 				 * PMTU check versus smallest asoc MTU goes
4541 				 * here
4542 				 */
4543 				if (ro->ro_rt == NULL) {
4544 					/* Route was freed */
4545 					if (net->ro._s_addr &&
4546 					    net->src_addr_selected) {
4547 						sctp_free_ifa(net->ro._s_addr);
4548 						net->ro._s_addr = NULL;
4549 					}
4550 					net->src_addr_selected = 0;
4551 				}
4552 				if ((ro->ro_rt != NULL) &&
4553 				    (net->ro._s_addr)) {
4554 					uint32_t mtu;
4555 
4556 					mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4557 					if (mtu &&
4558 					    (stcb->asoc.smallest_mtu > mtu)) {
4559 						sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4560 						net->mtu = mtu;
4561 						if (net->port) {
4562 							net->mtu -= sizeof(struct udphdr);
4563 						}
4564 					}
4565 				} else if (ifp) {
4566 					if (ND_IFINFO(ifp)->linkmtu &&
4567 					    (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4568 						sctp_mtu_size_reset(inp,
4569 						    &stcb->asoc,
4570 						    ND_IFINFO(ifp)->linkmtu);
4571 					}
4572 				}
4573 			}
4574 			return (ret);
4575 		}
4576 #endif
4577 	default:
4578 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4579 		    ((struct sockaddr *)to)->sa_family);
4580 		sctp_m_freem(m);
4581 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4582 		return (EFAULT);
4583 	}
4584 }
4585 
4586 
4587 void
4588 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4589 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4590     SCTP_UNUSED
4591 #endif
4592 )
4593 {
4594 	struct mbuf *m, *m_at, *mp_last;
4595 	struct sctp_nets *net;
4596 	struct sctp_init_chunk *init;
4597 	struct sctp_supported_addr_param *sup_addr;
4598 	struct sctp_adaptation_layer_indication *ali;
4599 	struct sctp_ecn_supported_param *ecn;
4600 	struct sctp_prsctp_supported_param *prsctp;
4601 	struct sctp_supported_chunk_types_param *pr_supported;
4602 	int cnt_inits_to = 0;
4603 	int padval, ret;
4604 	int num_ext;
4605 	int p_len;
4606 
4607 	/* INIT's always go to the primary (and usually ONLY address) */
4608 	mp_last = NULL;
4609 	net = stcb->asoc.primary_destination;
4610 	if (net == NULL) {
4611 		net = TAILQ_FIRST(&stcb->asoc.nets);
4612 		if (net == NULL) {
4613 			/* TSNH */
4614 			return;
4615 		}
4616 		/* we confirm any address we send an INIT to */
4617 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4618 		(void)sctp_set_primary_addr(stcb, NULL, net);
4619 	} else {
4620 		/* we confirm any address we send an INIT to */
4621 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4622 	}
4623 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4624 #ifdef INET6
4625 	if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4626 		/*
4627 		 * special hook, if we are sending to link local it will not
4628 		 * show up in our private address count.
4629 		 */
4630 		struct sockaddr_in6 *sin6l;
4631 
4632 		sin6l = &net->ro._l_addr.sin6;
4633 		if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4634 			cnt_inits_to = 1;
4635 	}
4636 #endif
4637 	if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4638 		/* This case should not happen */
4639 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4640 		return;
4641 	}
4642 	/* start the INIT timer */
4643 	sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4644 
4645 	m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4646 	if (m == NULL) {
4647 		/* No memory, INIT timer will re-attempt. */
4648 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4649 		return;
4650 	}
4651 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4652 	/*
4653 	 * assume peer supports asconf in order to be able to queue local
4654 	 * address changes while an INIT is in flight and before the assoc
4655 	 * is established.
4656 	 */
4657 	stcb->asoc.peer_supports_asconf = 1;
4658 	/* Now lets put the SCTP header in place */
4659 	init = mtod(m, struct sctp_init_chunk *);
4660 	/* now the chunk header */
4661 	init->ch.chunk_type = SCTP_INITIATION;
4662 	init->ch.chunk_flags = 0;
4663 	/* fill in later from mbuf we build */
4664 	init->ch.chunk_length = 0;
4665 	/* place in my tag */
4666 	init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4667 	/* set up some of the credits. */
4668 	init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4669 	    SCTP_MINIMAL_RWND));
4670 
4671 	init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4672 	init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4673 	init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4674 	/* now the address restriction */
4675 	/* XXX Should we take the address family of the socket into account? */
4676 	sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init +
4677 	    sizeof(*init));
4678 	sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4679 #ifdef INET6
4680 #ifdef INET
4681 	/* we support 2 types: IPv4/IPv6 */
4682 	sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + 2 * sizeof(uint16_t));
4683 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4684 	sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4685 #else
4686 	/* we support 1 type: IPv6 */
4687 	sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + sizeof(uint16_t));
4688 	sup_addr->addr_type[0] = htons(SCTP_IPV6_ADDRESS);
4689 	sup_addr->addr_type[1] = htons(0);	/* this is the padding */
4690 #endif
4691 #else
4692 	/* we support 1 type: IPv4 */
4693 	sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + sizeof(uint16_t));
4694 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4695 	sup_addr->addr_type[1] = htons(0);	/* this is the padding */
4696 #endif
4697 	SCTP_BUF_LEN(m) += sizeof(struct sctp_supported_addr_param);
4698 	/* adaptation layer indication parameter */
4699 	ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(struct sctp_supported_addr_param));
4700 	ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4701 	ali->ph.param_length = htons(sizeof(*ali));
4702 	ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4703 	SCTP_BUF_LEN(m) += sizeof(*ali);
4704 	ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
4705 
4706 	if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4707 		/* Add NAT friendly parameter */
4708 		struct sctp_paramhdr *ph;
4709 
4710 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4711 		ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4712 		ph->param_length = htons(sizeof(struct sctp_paramhdr));
4713 		SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
4714 		ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph));
4715 	}
4716 	/* now any cookie time extensions */
4717 	if (stcb->asoc.cookie_preserve_req) {
4718 		struct sctp_cookie_perserve_param *cookie_preserve;
4719 
4720 		cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4721 		cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4722 		cookie_preserve->ph.param_length = htons(
4723 		    sizeof(*cookie_preserve));
4724 		cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4725 		SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4726 		ecn = (struct sctp_ecn_supported_param *)(
4727 		    (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4728 		stcb->asoc.cookie_preserve_req = 0;
4729 	}
4730 	/* ECN parameter */
4731 	if (stcb->asoc.ecn_allowed == 1) {
4732 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4733 		ecn->ph.param_length = htons(sizeof(*ecn));
4734 		SCTP_BUF_LEN(m) += sizeof(*ecn);
4735 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4736 		    sizeof(*ecn));
4737 	} else {
4738 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4739 	}
4740 	/* And now tell the peer we do pr-sctp */
4741 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4742 	prsctp->ph.param_length = htons(sizeof(*prsctp));
4743 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
4744 
4745 	/* And now tell the peer we do all the extensions */
4746 	pr_supported = (struct sctp_supported_chunk_types_param *)
4747 	    ((caddr_t)prsctp + sizeof(*prsctp));
4748 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4749 	num_ext = 0;
4750 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4751 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4752 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4753 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4754 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4755 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4756 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4757 	}
4758 	if (stcb->asoc.sctp_nr_sack_on_off == 1) {
4759 		pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4760 	}
4761 	p_len = sizeof(*pr_supported) + num_ext;
4762 	pr_supported->ph.param_length = htons(p_len);
4763 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4764 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4765 
4766 
4767 	/* add authentication parameters */
4768 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4769 		struct sctp_auth_random *randp;
4770 		struct sctp_auth_hmac_algo *hmacs;
4771 		struct sctp_auth_chunk_list *chunks;
4772 
4773 		/* attach RANDOM parameter, if available */
4774 		if (stcb->asoc.authinfo.random != NULL) {
4775 			randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4776 			p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4777 			/* random key already contains the header */
4778 			bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4779 			/* zero out any padding required */
4780 			bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4781 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4782 		}
4783 		/* add HMAC_ALGO parameter */
4784 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4785 		p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4786 		    (uint8_t *) hmacs->hmac_ids);
4787 		if (p_len > 0) {
4788 			p_len += sizeof(*hmacs);
4789 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4790 			hmacs->ph.param_length = htons(p_len);
4791 			/* zero out any padding required */
4792 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4793 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4794 		}
4795 		/* add CHUNKS parameter */
4796 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4797 		p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4798 		    chunks->chunk_types);
4799 		if (p_len > 0) {
4800 			p_len += sizeof(*chunks);
4801 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4802 			chunks->ph.param_length = htons(p_len);
4803 			/* zero out any padding required */
4804 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4805 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4806 		}
4807 	}
4808 	/* now the addresses */
4809 	{
4810 		struct sctp_scoping scp;
4811 
4812 		/*
4813 		 * To optimize this we could put the scoping stuff into a
4814 		 * structure and remove the individual uint8's from the
4815 		 * assoc structure. Then we could just sifa in the address
4816 		 * within the stcb. But for now this is a quick hack to get
4817 		 * the address stuff teased apart.
4818 		 */
4819 
4820 		scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4821 		scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4822 		scp.loopback_scope = stcb->asoc.loopback_scope;
4823 		scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4824 		scp.local_scope = stcb->asoc.local_scope;
4825 		scp.site_scope = stcb->asoc.site_scope;
4826 
4827 		sctp_add_addresses_to_i_ia(inp, stcb, &scp, m, cnt_inits_to);
4828 	}
4829 
4830 	/* calulate the size and update pkt header and chunk header */
4831 	p_len = 0;
4832 	for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4833 		if (SCTP_BUF_NEXT(m_at) == NULL)
4834 			mp_last = m_at;
4835 		p_len += SCTP_BUF_LEN(m_at);
4836 	}
4837 	init->ch.chunk_length = htons(p_len);
4838 	/*
4839 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4840 	 * here since the timer will drive a retranmission.
4841 	 */
4842 
4843 	/* I don't expect this to execute but we will be safe here */
4844 	padval = p_len % 4;
4845 	if ((padval) && (mp_last)) {
4846 		/*
4847 		 * The compiler worries that mp_last may not be set even
4848 		 * though I think it is impossible :-> however we add
4849 		 * mp_last here just in case.
4850 		 */
4851 		ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4852 		if (ret) {
4853 			/* Houston we have a problem, no space */
4854 			sctp_m_freem(m);
4855 			return;
4856 		}
4857 	}
4858 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4859 	ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4860 	    (struct sockaddr *)&net->ro._l_addr,
4861 	    m, 0, NULL, 0, 0, 0, 0,
4862 	    inp->sctp_lport, stcb->rport, htonl(0),
4863 	    net->port, so_locked, NULL, NULL);
4864 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4865 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4866 	(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4867 }
4868 
4869 struct mbuf *
4870 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4871     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
4872 {
4873 	/*
4874 	 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4875 	 * being equal to the beginning of the params i.e. (iphlen +
4876 	 * sizeof(struct sctp_init_msg) parse through the parameters to the
4877 	 * end of the mbuf verifying that all parameters are known.
4878 	 *
4879 	 * For unknown parameters build and return a mbuf with
4880 	 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4881 	 * processing this chunk stop, and set *abort_processing to 1.
4882 	 *
4883 	 * By having param_offset be pre-set to where parameters begin it is
4884 	 * hoped that this routine may be reused in the future by new
4885 	 * features.
4886 	 */
4887 	struct sctp_paramhdr *phdr, params;
4888 
4889 	struct mbuf *mat, *op_err;
4890 	char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4891 	int at, limit, pad_needed;
4892 	uint16_t ptype, plen, padded_size;
4893 	int err_at;
4894 
4895 	*abort_processing = 0;
4896 	mat = in_initpkt;
4897 	err_at = 0;
4898 	limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4899 	at = param_offset;
4900 	op_err = NULL;
4901 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4902 	phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4903 	while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4904 		ptype = ntohs(phdr->param_type);
4905 		plen = ntohs(phdr->param_length);
4906 		if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4907 			/* wacked parameter */
4908 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4909 			goto invalid_size;
4910 		}
4911 		limit -= SCTP_SIZE32(plen);
4912 		/*-
4913 		 * All parameters for all chunks that we know/understand are
4914 		 * listed here. We process them other places and make
4915 		 * appropriate stop actions per the upper bits. However this
4916 		 * is the generic routine processor's can call to get back
4917 		 * an operr.. to either incorporate (init-ack) or send.
4918 		 */
4919 		padded_size = SCTP_SIZE32(plen);
4920 		switch (ptype) {
4921 			/* Param's with variable size */
4922 		case SCTP_HEARTBEAT_INFO:
4923 		case SCTP_STATE_COOKIE:
4924 		case SCTP_UNRECOG_PARAM:
4925 		case SCTP_ERROR_CAUSE_IND:
4926 			/* ok skip fwd */
4927 			at += padded_size;
4928 			break;
4929 			/* Param's with variable size within a range */
4930 		case SCTP_CHUNK_LIST:
4931 		case SCTP_SUPPORTED_CHUNK_EXT:
4932 			if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4933 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4934 				goto invalid_size;
4935 			}
4936 			at += padded_size;
4937 			break;
4938 		case SCTP_SUPPORTED_ADDRTYPE:
4939 			if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4940 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4941 				goto invalid_size;
4942 			}
4943 			at += padded_size;
4944 			break;
4945 		case SCTP_RANDOM:
4946 			if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4947 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4948 				goto invalid_size;
4949 			}
4950 			at += padded_size;
4951 			break;
4952 		case SCTP_SET_PRIM_ADDR:
4953 		case SCTP_DEL_IP_ADDRESS:
4954 		case SCTP_ADD_IP_ADDRESS:
4955 			if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4956 			    (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4957 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4958 				goto invalid_size;
4959 			}
4960 			at += padded_size;
4961 			break;
4962 			/* Param's with a fixed size */
4963 		case SCTP_IPV4_ADDRESS:
4964 			if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4965 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4966 				goto invalid_size;
4967 			}
4968 			at += padded_size;
4969 			break;
4970 		case SCTP_IPV6_ADDRESS:
4971 			if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4972 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4973 				goto invalid_size;
4974 			}
4975 			at += padded_size;
4976 			break;
4977 		case SCTP_COOKIE_PRESERVE:
4978 			if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4979 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4980 				goto invalid_size;
4981 			}
4982 			at += padded_size;
4983 			break;
4984 		case SCTP_HAS_NAT_SUPPORT:
4985 			*nat_friendly = 1;
4986 			/* fall through */
4987 		case SCTP_PRSCTP_SUPPORTED:
4988 
4989 			if (padded_size != sizeof(struct sctp_paramhdr)) {
4990 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen);
4991 				goto invalid_size;
4992 			}
4993 			at += padded_size;
4994 			break;
4995 		case SCTP_ECN_CAPABLE:
4996 			if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4997 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4998 				goto invalid_size;
4999 			}
5000 			at += padded_size;
5001 			break;
5002 		case SCTP_ULP_ADAPTATION:
5003 			if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
5004 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
5005 				goto invalid_size;
5006 			}
5007 			at += padded_size;
5008 			break;
5009 		case SCTP_SUCCESS_REPORT:
5010 			if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
5011 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
5012 				goto invalid_size;
5013 			}
5014 			at += padded_size;
5015 			break;
5016 		case SCTP_HOSTNAME_ADDRESS:
5017 			{
5018 				/* We can NOT handle HOST NAME addresses!! */
5019 				int l_len;
5020 
5021 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
5022 				*abort_processing = 1;
5023 				if (op_err == NULL) {
5024 					/* Ok need to try to get a mbuf */
5025 #ifdef INET6
5026 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5027 #else
5028 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5029 #endif
5030 					l_len += plen;
5031 					l_len += sizeof(struct sctp_paramhdr);
5032 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
5033 					if (op_err) {
5034 						SCTP_BUF_LEN(op_err) = 0;
5035 						/*
5036 						 * pre-reserve space for ip
5037 						 * and sctp header  and
5038 						 * chunk hdr
5039 						 */
5040 #ifdef INET6
5041 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5042 #else
5043 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5044 #endif
5045 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5046 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5047 					}
5048 				}
5049 				if (op_err) {
5050 					/* If we have space */
5051 					struct sctp_paramhdr s;
5052 
5053 					if (err_at % 4) {
5054 						uint32_t cpthis = 0;
5055 
5056 						pad_needed = 4 - (err_at % 4);
5057 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
5058 						err_at += pad_needed;
5059 					}
5060 					s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
5061 					s.param_length = htons(sizeof(s) + plen);
5062 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
5063 					err_at += sizeof(s);
5064 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
5065 					if (phdr == NULL) {
5066 						sctp_m_freem(op_err);
5067 						/*
5068 						 * we are out of memory but
5069 						 * we still need to have a
5070 						 * look at what to do (the
5071 						 * system is in trouble
5072 						 * though).
5073 						 */
5074 						return (NULL);
5075 					}
5076 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
5077 				}
5078 				return (op_err);
5079 				break;
5080 			}
5081 		default:
5082 			/*
5083 			 * we do not recognize the parameter figure out what
5084 			 * we do.
5085 			 */
5086 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
5087 			if ((ptype & 0x4000) == 0x4000) {
5088 				/* Report bit is set?? */
5089 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
5090 				if (op_err == NULL) {
5091 					int l_len;
5092 
5093 					/* Ok need to try to get an mbuf */
5094 #ifdef INET6
5095 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5096 #else
5097 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5098 #endif
5099 					l_len += plen;
5100 					l_len += sizeof(struct sctp_paramhdr);
5101 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
5102 					if (op_err) {
5103 						SCTP_BUF_LEN(op_err) = 0;
5104 #ifdef INET6
5105 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5106 #else
5107 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5108 #endif
5109 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5110 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5111 					}
5112 				}
5113 				if (op_err) {
5114 					/* If we have space */
5115 					struct sctp_paramhdr s;
5116 
5117 					if (err_at % 4) {
5118 						uint32_t cpthis = 0;
5119 
5120 						pad_needed = 4 - (err_at % 4);
5121 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
5122 						err_at += pad_needed;
5123 					}
5124 					s.param_type = htons(SCTP_UNRECOG_PARAM);
5125 					s.param_length = htons(sizeof(s) + plen);
5126 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
5127 					err_at += sizeof(s);
5128 					if (plen > sizeof(tempbuf)) {
5129 						plen = sizeof(tempbuf);
5130 					}
5131 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
5132 					if (phdr == NULL) {
5133 						sctp_m_freem(op_err);
5134 						/*
5135 						 * we are out of memory but
5136 						 * we still need to have a
5137 						 * look at what to do (the
5138 						 * system is in trouble
5139 						 * though).
5140 						 */
5141 						op_err = NULL;
5142 						goto more_processing;
5143 					}
5144 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
5145 					err_at += plen;
5146 				}
5147 			}
5148 	more_processing:
5149 			if ((ptype & 0x8000) == 0x0000) {
5150 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
5151 				return (op_err);
5152 			} else {
5153 				/* skip this chunk and continue processing */
5154 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
5155 				at += SCTP_SIZE32(plen);
5156 			}
5157 			break;
5158 
5159 		}
5160 		phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
5161 	}
5162 	return (op_err);
5163 invalid_size:
5164 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
5165 	*abort_processing = 1;
5166 	if ((op_err == NULL) && phdr) {
5167 		int l_len;
5168 
5169 #ifdef INET6
5170 		l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5171 #else
5172 		l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5173 #endif
5174 		l_len += (2 * sizeof(struct sctp_paramhdr));
5175 		op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
5176 		if (op_err) {
5177 			SCTP_BUF_LEN(op_err) = 0;
5178 #ifdef INET6
5179 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5180 #else
5181 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5182 #endif
5183 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5184 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5185 		}
5186 	}
5187 	if ((op_err) && phdr) {
5188 		struct sctp_paramhdr s;
5189 
5190 		if (err_at % 4) {
5191 			uint32_t cpthis = 0;
5192 
5193 			pad_needed = 4 - (err_at % 4);
5194 			m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
5195 			err_at += pad_needed;
5196 		}
5197 		s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5198 		s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
5199 		m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
5200 		err_at += sizeof(s);
5201 		/* Only copy back the p-hdr that caused the issue */
5202 		m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
5203 	}
5204 	return (op_err);
5205 }
5206 
5207 static int
5208 sctp_are_there_new_addresses(struct sctp_association *asoc,
5209     struct mbuf *in_initpkt, int offset)
5210 {
5211 	/*
5212 	 * Given a INIT packet, look through the packet to verify that there
5213 	 * are NO new addresses. As we go through the parameters add reports
5214 	 * of any un-understood parameters that require an error.  Also we
5215 	 * must return (1) to drop the packet if we see a un-understood
5216 	 * parameter that tells us to drop the chunk.
5217 	 */
5218 	struct sockaddr *sa_touse;
5219 	struct sockaddr *sa;
5220 	struct sctp_paramhdr *phdr, params;
5221 	uint16_t ptype, plen;
5222 	uint8_t fnd;
5223 	struct sctp_nets *net;
5224 	struct ip *iph;
5225 
5226 #ifdef INET
5227 	struct sockaddr_in sin4, *sa4;
5228 
5229 #endif
5230 #ifdef INET6
5231 	struct sockaddr_in6 sin6, *sa6;
5232 	struct ip6_hdr *ip6h;
5233 
5234 #endif
5235 
5236 #ifdef INET
5237 	memset(&sin4, 0, sizeof(sin4));
5238 	sin4.sin_family = AF_INET;
5239 	sin4.sin_len = sizeof(sin4);
5240 #endif
5241 #ifdef INET6
5242 	memset(&sin6, 0, sizeof(sin6));
5243 	sin6.sin6_family = AF_INET6;
5244 	sin6.sin6_len = sizeof(sin6);
5245 #endif
5246 	sa_touse = NULL;
5247 	/* First what about the src address of the pkt ? */
5248 	iph = mtod(in_initpkt, struct ip *);
5249 	switch (iph->ip_v) {
5250 #ifdef INET
5251 	case IPVERSION:
5252 		/* source addr is IPv4 */
5253 		sin4.sin_addr = iph->ip_src;
5254 		sa_touse = (struct sockaddr *)&sin4;
5255 		break;
5256 #endif
5257 #ifdef INET6
5258 	case IPV6_VERSION >> 4:
5259 		/* source addr is IPv6 */
5260 		ip6h = mtod(in_initpkt, struct ip6_hdr *);
5261 		sin6.sin6_addr = ip6h->ip6_src;
5262 		sa_touse = (struct sockaddr *)&sin6;
5263 		break;
5264 #endif
5265 	default:
5266 		return (1);
5267 	}
5268 
5269 	fnd = 0;
5270 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5271 		sa = (struct sockaddr *)&net->ro._l_addr;
5272 		if (sa->sa_family == sa_touse->sa_family) {
5273 #ifdef INET
5274 			if (sa->sa_family == AF_INET) {
5275 				sa4 = (struct sockaddr_in *)sa;
5276 				if (sa4->sin_addr.s_addr == sin4.sin_addr.s_addr) {
5277 					fnd = 1;
5278 					break;
5279 				}
5280 			}
5281 #endif
5282 #ifdef INET6
5283 			if (sa->sa_family == AF_INET6) {
5284 				sa6 = (struct sockaddr_in6 *)sa;
5285 				if (SCTP6_ARE_ADDR_EQUAL(sa6, &sin6)) {
5286 					fnd = 1;
5287 					break;
5288 				}
5289 			}
5290 #endif
5291 		}
5292 	}
5293 	if (fnd == 0) {
5294 		/* New address added! no need to look futher. */
5295 		return (1);
5296 	}
5297 	/* Ok so far lets munge through the rest of the packet */
5298 	offset += sizeof(struct sctp_init_chunk);
5299 	phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
5300 	while (phdr) {
5301 		sa_touse = NULL;
5302 		ptype = ntohs(phdr->param_type);
5303 		plen = ntohs(phdr->param_length);
5304 		switch (ptype) {
5305 #ifdef INET
5306 		case SCTP_IPV4_ADDRESS:
5307 			{
5308 				struct sctp_ipv4addr_param *p4, p4_buf;
5309 
5310 				phdr = sctp_get_next_param(in_initpkt, offset,
5311 				    (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
5312 				if (plen != sizeof(struct sctp_ipv4addr_param) ||
5313 				    phdr == NULL) {
5314 					return (1);
5315 				}
5316 				p4 = (struct sctp_ipv4addr_param *)phdr;
5317 				sin4.sin_addr.s_addr = p4->addr;
5318 				sa_touse = (struct sockaddr *)&sin4;
5319 				break;
5320 			}
5321 #endif
5322 #ifdef INET6
5323 		case SCTP_IPV6_ADDRESS:
5324 			{
5325 				struct sctp_ipv6addr_param *p6, p6_buf;
5326 
5327 				phdr = sctp_get_next_param(in_initpkt, offset,
5328 				    (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
5329 				if (plen != sizeof(struct sctp_ipv6addr_param) ||
5330 				    phdr == NULL) {
5331 					return (1);
5332 				}
5333 				p6 = (struct sctp_ipv6addr_param *)phdr;
5334 				memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
5335 				    sizeof(p6->addr));
5336 				sa_touse = (struct sockaddr *)&sin6;
5337 				break;
5338 			}
5339 #endif
5340 		default:
5341 			sa_touse = NULL;
5342 			break;
5343 		}
5344 		if (sa_touse) {
5345 			/* ok, sa_touse points to one to check */
5346 			fnd = 0;
5347 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5348 				sa = (struct sockaddr *)&net->ro._l_addr;
5349 				if (sa->sa_family != sa_touse->sa_family) {
5350 					continue;
5351 				}
5352 #ifdef INET
5353 				if (sa->sa_family == AF_INET) {
5354 					sa4 = (struct sockaddr_in *)sa;
5355 					if (sa4->sin_addr.s_addr ==
5356 					    sin4.sin_addr.s_addr) {
5357 						fnd = 1;
5358 						break;
5359 					}
5360 				}
5361 #endif
5362 #ifdef INET6
5363 				if (sa->sa_family == AF_INET6) {
5364 					sa6 = (struct sockaddr_in6 *)sa;
5365 					if (SCTP6_ARE_ADDR_EQUAL(
5366 					    sa6, &sin6)) {
5367 						fnd = 1;
5368 						break;
5369 					}
5370 				}
5371 #endif
5372 			}
5373 			if (!fnd) {
5374 				/* New addr added! no need to look further */
5375 				return (1);
5376 			}
5377 		}
5378 		offset += SCTP_SIZE32(plen);
5379 		phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
5380 	}
5381 	return (0);
5382 }
5383 
5384 /*
5385  * Given a MBUF chain that was sent into us containing an INIT. Build a
5386  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
5387  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
5388  * message (i.e. the struct sctp_init_msg).
5389  */
5390 void
5391 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
5392     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
5393     struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
5394 {
5395 	struct sctp_association *asoc;
5396 	struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
5397 	struct sctp_init_ack_chunk *initack;
5398 	struct sctp_adaptation_layer_indication *ali;
5399 	struct sctp_ecn_supported_param *ecn;
5400 	struct sctp_prsctp_supported_param *prsctp;
5401 	struct sctp_supported_chunk_types_param *pr_supported;
5402 	union sctp_sockstore store, store1, *over_addr;
5403 
5404 #ifdef INET
5405 	struct sockaddr_in *sin, *to_sin;
5406 
5407 #endif
5408 #ifdef INET6
5409 	struct sockaddr_in6 *sin6, *to_sin6;
5410 
5411 #endif
5412 	struct ip *iph;
5413 
5414 #ifdef INET6
5415 	struct ip6_hdr *ip6;
5416 
5417 #endif
5418 	struct sockaddr *to;
5419 	struct sctp_state_cookie stc;
5420 	struct sctp_nets *net = NULL;
5421 	uint8_t *signature = NULL;
5422 	int cnt_inits_to = 0;
5423 	uint16_t his_limit, i_want;
5424 	int abort_flag, padval;
5425 	int num_ext;
5426 	int p_len;
5427 	int nat_friendly = 0;
5428 	struct socket *so;
5429 
5430 	if (stcb)
5431 		asoc = &stcb->asoc;
5432 	else
5433 		asoc = NULL;
5434 	mp_last = NULL;
5435 	if ((asoc != NULL) &&
5436 	    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
5437 	    (sctp_are_there_new_addresses(asoc, init_pkt, offset))) {
5438 		/* new addresses, out of here in non-cookie-wait states */
5439 		/*
5440 		 * Send a ABORT, we don't add the new address error clause
5441 		 * though we even set the T bit and copy in the 0 tag.. this
5442 		 * looks no different than if no listener was present.
5443 		 */
5444 		sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
5445 		return;
5446 	}
5447 	abort_flag = 0;
5448 	op_err = sctp_arethere_unrecognized_parameters(init_pkt,
5449 	    (offset + sizeof(struct sctp_init_chunk)),
5450 	    &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
5451 	if (abort_flag) {
5452 do_a_abort:
5453 		sctp_send_abort(init_pkt, iphlen, sh,
5454 		    init_chk->init.initiate_tag, op_err, vrf_id, port);
5455 		return;
5456 	}
5457 	m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
5458 	if (m == NULL) {
5459 		/* No memory, INIT timer will re-attempt. */
5460 		if (op_err)
5461 			sctp_m_freem(op_err);
5462 		return;
5463 	}
5464 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
5465 
5466 	/* the time I built cookie */
5467 	(void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
5468 
5469 	/* populate any tie tags */
5470 	if (asoc != NULL) {
5471 		/* unlock before tag selections */
5472 		stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5473 		stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5474 		stc.cookie_life = asoc->cookie_life;
5475 		net = asoc->primary_destination;
5476 	} else {
5477 		stc.tie_tag_my_vtag = 0;
5478 		stc.tie_tag_peer_vtag = 0;
5479 		/* life I will award this cookie */
5480 		stc.cookie_life = inp->sctp_ep.def_cookie_life;
5481 	}
5482 
5483 	/* copy in the ports for later check */
5484 	stc.myport = sh->dest_port;
5485 	stc.peerport = sh->src_port;
5486 
5487 	/*
5488 	 * If we wanted to honor cookie life extentions, we would add to
5489 	 * stc.cookie_life. For now we should NOT honor any extension
5490 	 */
5491 	stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5492 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5493 		struct inpcb *in_inp;
5494 
5495 		/* Its a V6 socket */
5496 		in_inp = (struct inpcb *)inp;
5497 		stc.ipv6_addr_legal = 1;
5498 		/* Now look at the binding flag to see if V4 will be legal */
5499 		if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
5500 			stc.ipv4_addr_legal = 1;
5501 		} else {
5502 			/* V4 addresses are NOT legal on the association */
5503 			stc.ipv4_addr_legal = 0;
5504 		}
5505 	} else {
5506 		/* Its a V4 socket, no - V6 */
5507 		stc.ipv4_addr_legal = 1;
5508 		stc.ipv6_addr_legal = 0;
5509 	}
5510 
5511 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
5512 	stc.ipv4_scope = 1;
5513 #else
5514 	stc.ipv4_scope = 0;
5515 #endif
5516 	/* now for scope setup */
5517 	memset((caddr_t)&store, 0, sizeof(store));
5518 	memset((caddr_t)&store1, 0, sizeof(store1));
5519 #ifdef INET
5520 	sin = &store.sin;
5521 	to_sin = &store1.sin;
5522 #endif
5523 #ifdef INET6
5524 	sin6 = &store.sin6;
5525 	to_sin6 = &store1.sin6;
5526 #endif
5527 	iph = mtod(init_pkt, struct ip *);
5528 	/* establish the to_addr's */
5529 	switch (iph->ip_v) {
5530 #ifdef INET
5531 	case IPVERSION:
5532 		to_sin->sin_port = sh->dest_port;
5533 		to_sin->sin_family = AF_INET;
5534 		to_sin->sin_len = sizeof(struct sockaddr_in);
5535 		to_sin->sin_addr = iph->ip_dst;
5536 		break;
5537 #endif
5538 #ifdef INET6
5539 	case IPV6_VERSION >> 4:
5540 		ip6 = mtod(init_pkt, struct ip6_hdr *);
5541 		to_sin6->sin6_addr = ip6->ip6_dst;
5542 		to_sin6->sin6_scope_id = 0;
5543 		to_sin6->sin6_port = sh->dest_port;
5544 		to_sin6->sin6_family = AF_INET6;
5545 		to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5546 		break;
5547 #endif
5548 	default:
5549 		goto do_a_abort;
5550 		break;
5551 	}
5552 
5553 	if (net == NULL) {
5554 		to = (struct sockaddr *)&store;
5555 		switch (iph->ip_v) {
5556 #ifdef INET
5557 		case IPVERSION:
5558 			{
5559 				sin->sin_family = AF_INET;
5560 				sin->sin_len = sizeof(struct sockaddr_in);
5561 				sin->sin_port = sh->src_port;
5562 				sin->sin_addr = iph->ip_src;
5563 				/* lookup address */
5564 				stc.address[0] = sin->sin_addr.s_addr;
5565 				stc.address[1] = 0;
5566 				stc.address[2] = 0;
5567 				stc.address[3] = 0;
5568 				stc.addr_type = SCTP_IPV4_ADDRESS;
5569 				/* local from address */
5570 				stc.laddress[0] = to_sin->sin_addr.s_addr;
5571 				stc.laddress[1] = 0;
5572 				stc.laddress[2] = 0;
5573 				stc.laddress[3] = 0;
5574 				stc.laddr_type = SCTP_IPV4_ADDRESS;
5575 				/* scope_id is only for v6 */
5576 				stc.scope_id = 0;
5577 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5578 				if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5579 					stc.ipv4_scope = 1;
5580 				}
5581 #else
5582 				stc.ipv4_scope = 1;
5583 #endif				/* SCTP_DONT_DO_PRIVADDR_SCOPE */
5584 				/* Must use the address in this case */
5585 				if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5586 					stc.loopback_scope = 1;
5587 					stc.ipv4_scope = 1;
5588 					stc.site_scope = 1;
5589 					stc.local_scope = 0;
5590 				}
5591 				break;
5592 			}
5593 #endif
5594 #ifdef INET6
5595 		case IPV6_VERSION >> 4:
5596 			{
5597 				ip6 = mtod(init_pkt, struct ip6_hdr *);
5598 				sin6->sin6_family = AF_INET6;
5599 				sin6->sin6_len = sizeof(struct sockaddr_in6);
5600 				sin6->sin6_port = sh->src_port;
5601 				sin6->sin6_addr = ip6->ip6_src;
5602 				/* lookup address */
5603 				memcpy(&stc.address, &sin6->sin6_addr,
5604 				    sizeof(struct in6_addr));
5605 				sin6->sin6_scope_id = 0;
5606 				stc.addr_type = SCTP_IPV6_ADDRESS;
5607 				stc.scope_id = 0;
5608 				if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5609 					/*
5610 					 * FIX ME: does this have scope from
5611 					 * rcvif?
5612 					 */
5613 					(void)sa6_recoverscope(sin6);
5614 					stc.scope_id = sin6->sin6_scope_id;
5615 					sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5616 					stc.loopback_scope = 1;
5617 					stc.local_scope = 0;
5618 					stc.site_scope = 1;
5619 					stc.ipv4_scope = 1;
5620 				} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5621 					/*
5622 					 * If the new destination is a
5623 					 * LINK_LOCAL we must have common
5624 					 * both site and local scope. Don't
5625 					 * set local scope though since we
5626 					 * must depend on the source to be
5627 					 * added implicitly. We cannot
5628 					 * assure just because we share one
5629 					 * link that all links are common.
5630 					 */
5631 					stc.local_scope = 0;
5632 					stc.site_scope = 1;
5633 					stc.ipv4_scope = 1;
5634 					/*
5635 					 * we start counting for the private
5636 					 * address stuff at 1. since the
5637 					 * link local we source from won't
5638 					 * show up in our scoped count.
5639 					 */
5640 					cnt_inits_to = 1;
5641 					/*
5642 					 * pull out the scope_id from
5643 					 * incoming pkt
5644 					 */
5645 					/*
5646 					 * FIX ME: does this have scope from
5647 					 * rcvif?
5648 					 */
5649 					(void)sa6_recoverscope(sin6);
5650 					stc.scope_id = sin6->sin6_scope_id;
5651 					sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5652 				} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5653 					/*
5654 					 * If the new destination is
5655 					 * SITE_LOCAL then we must have site
5656 					 * scope in common.
5657 					 */
5658 					stc.site_scope = 1;
5659 				}
5660 				memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5661 				stc.laddr_type = SCTP_IPV6_ADDRESS;
5662 				break;
5663 			}
5664 #endif
5665 		default:
5666 			/* TSNH */
5667 			goto do_a_abort;
5668 			break;
5669 		}
5670 	} else {
5671 		/* set the scope per the existing tcb */
5672 
5673 #ifdef INET6
5674 		struct sctp_nets *lnet;
5675 
5676 #endif
5677 
5678 		stc.loopback_scope = asoc->loopback_scope;
5679 		stc.ipv4_scope = asoc->ipv4_local_scope;
5680 		stc.site_scope = asoc->site_scope;
5681 		stc.local_scope = asoc->local_scope;
5682 #ifdef INET6
5683 		/* Why do we not consider IPv4 LL addresses? */
5684 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5685 			if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5686 				if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5687 					/*
5688 					 * if we have a LL address, start
5689 					 * counting at 1.
5690 					 */
5691 					cnt_inits_to = 1;
5692 				}
5693 			}
5694 		}
5695 #endif
5696 		/* use the net pointer */
5697 		to = (struct sockaddr *)&net->ro._l_addr;
5698 		switch (to->sa_family) {
5699 #ifdef INET
5700 		case AF_INET:
5701 			sin = (struct sockaddr_in *)to;
5702 			stc.address[0] = sin->sin_addr.s_addr;
5703 			stc.address[1] = 0;
5704 			stc.address[2] = 0;
5705 			stc.address[3] = 0;
5706 			stc.addr_type = SCTP_IPV4_ADDRESS;
5707 			if (net->src_addr_selected == 0) {
5708 				/*
5709 				 * strange case here, the INIT should have
5710 				 * did the selection.
5711 				 */
5712 				net->ro._s_addr = sctp_source_address_selection(inp,
5713 				    stcb, (sctp_route_t *) & net->ro,
5714 				    net, 0, vrf_id);
5715 				if (net->ro._s_addr == NULL)
5716 					return;
5717 
5718 				net->src_addr_selected = 1;
5719 
5720 			}
5721 			stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5722 			stc.laddress[1] = 0;
5723 			stc.laddress[2] = 0;
5724 			stc.laddress[3] = 0;
5725 			stc.laddr_type = SCTP_IPV4_ADDRESS;
5726 			/* scope_id is only for v6 */
5727 			stc.scope_id = 0;
5728 			break;
5729 #endif
5730 #ifdef INET6
5731 		case AF_INET6:
5732 			sin6 = (struct sockaddr_in6 *)to;
5733 			memcpy(&stc.address, &sin6->sin6_addr,
5734 			    sizeof(struct in6_addr));
5735 			stc.addr_type = SCTP_IPV6_ADDRESS;
5736 			stc.scope_id = sin6->sin6_scope_id;
5737 			if (net->src_addr_selected == 0) {
5738 				/*
5739 				 * strange case here, the INIT should have
5740 				 * did the selection.
5741 				 */
5742 				net->ro._s_addr = sctp_source_address_selection(inp,
5743 				    stcb, (sctp_route_t *) & net->ro,
5744 				    net, 0, vrf_id);
5745 				if (net->ro._s_addr == NULL)
5746 					return;
5747 
5748 				net->src_addr_selected = 1;
5749 			}
5750 			memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5751 			    sizeof(struct in6_addr));
5752 			stc.laddr_type = SCTP_IPV6_ADDRESS;
5753 			break;
5754 #endif
5755 		}
5756 	}
5757 	/* Now lets put the SCTP header in place */
5758 	initack = mtod(m, struct sctp_init_ack_chunk *);
5759 	/* Save it off for quick ref */
5760 	stc.peers_vtag = init_chk->init.initiate_tag;
5761 	/* who are we */
5762 	memcpy(stc.identification, SCTP_VERSION_STRING,
5763 	    min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5764 	memset(stc.reserved, 0, SCTP_RESERVE_SPACE);
5765 	/* now the chunk header */
5766 	initack->ch.chunk_type = SCTP_INITIATION_ACK;
5767 	initack->ch.chunk_flags = 0;
5768 	/* fill in later from mbuf we build */
5769 	initack->ch.chunk_length = 0;
5770 	/* place in my tag */
5771 	if ((asoc != NULL) &&
5772 	    ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5773 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5774 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5775 		/* re-use the v-tags and init-seq here */
5776 		initack->init.initiate_tag = htonl(asoc->my_vtag);
5777 		initack->init.initial_tsn = htonl(asoc->init_seq_number);
5778 	} else {
5779 		uint32_t vtag, itsn;
5780 
5781 		if (hold_inp_lock) {
5782 			SCTP_INP_INCR_REF(inp);
5783 			SCTP_INP_RUNLOCK(inp);
5784 		}
5785 		if (asoc) {
5786 			atomic_add_int(&asoc->refcnt, 1);
5787 			SCTP_TCB_UNLOCK(stcb);
5788 	new_tag:
5789 			vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5790 			if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5791 				/*
5792 				 * Got a duplicate vtag on some guy behind a
5793 				 * nat make sure we don't use it.
5794 				 */
5795 				goto new_tag;
5796 			}
5797 			initack->init.initiate_tag = htonl(vtag);
5798 			/* get a TSN to use too */
5799 			itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5800 			initack->init.initial_tsn = htonl(itsn);
5801 			SCTP_TCB_LOCK(stcb);
5802 			atomic_add_int(&asoc->refcnt, -1);
5803 		} else {
5804 			vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5805 			initack->init.initiate_tag = htonl(vtag);
5806 			/* get a TSN to use too */
5807 			initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5808 		}
5809 		if (hold_inp_lock) {
5810 			SCTP_INP_RLOCK(inp);
5811 			SCTP_INP_DECR_REF(inp);
5812 		}
5813 	}
5814 	/* save away my tag to */
5815 	stc.my_vtag = initack->init.initiate_tag;
5816 
5817 	/* set up some of the credits. */
5818 	so = inp->sctp_socket;
5819 	if (so == NULL) {
5820 		/* memory problem */
5821 		sctp_m_freem(m);
5822 		return;
5823 	} else {
5824 		initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5825 	}
5826 	/* set what I want */
5827 	his_limit = ntohs(init_chk->init.num_inbound_streams);
5828 	/* choose what I want */
5829 	if (asoc != NULL) {
5830 		if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5831 			i_want = asoc->streamoutcnt;
5832 		} else {
5833 			i_want = inp->sctp_ep.pre_open_stream_count;
5834 		}
5835 	} else {
5836 		i_want = inp->sctp_ep.pre_open_stream_count;
5837 	}
5838 	if (his_limit < i_want) {
5839 		/* I Want more :< */
5840 		initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5841 	} else {
5842 		/* I can have what I want :> */
5843 		initack->init.num_outbound_streams = htons(i_want);
5844 	}
5845 	/* tell him his limt. */
5846 	initack->init.num_inbound_streams =
5847 	    htons(inp->sctp_ep.max_open_streams_intome);
5848 
5849 	/* adaptation layer indication parameter */
5850 	ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack));
5851 	ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5852 	ali->ph.param_length = htons(sizeof(*ali));
5853 	ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5854 	SCTP_BUF_LEN(m) += sizeof(*ali);
5855 	ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
5856 
5857 	/* ECN parameter */
5858 	if (((asoc != NULL) && (asoc->ecn_allowed == 1)) ||
5859 	    (inp->sctp_ecn_enable == 1)) {
5860 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5861 		ecn->ph.param_length = htons(sizeof(*ecn));
5862 		SCTP_BUF_LEN(m) += sizeof(*ecn);
5863 
5864 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5865 		    sizeof(*ecn));
5866 	} else {
5867 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5868 	}
5869 	/* And now tell the peer we do  pr-sctp */
5870 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5871 	prsctp->ph.param_length = htons(sizeof(*prsctp));
5872 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
5873 	if (nat_friendly) {
5874 		/* Add NAT friendly parameter */
5875 		struct sctp_paramhdr *ph;
5876 
5877 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5878 		ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5879 		ph->param_length = htons(sizeof(struct sctp_paramhdr));
5880 		SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
5881 	}
5882 	/* And now tell the peer we do all the extensions */
5883 	pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5884 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5885 	num_ext = 0;
5886 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5887 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5888 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5889 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5890 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5891 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5892 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5893 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
5894 		pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5895 	p_len = sizeof(*pr_supported) + num_ext;
5896 	pr_supported->ph.param_length = htons(p_len);
5897 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5898 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5899 
5900 	/* add authentication parameters */
5901 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5902 		struct sctp_auth_random *randp;
5903 		struct sctp_auth_hmac_algo *hmacs;
5904 		struct sctp_auth_chunk_list *chunks;
5905 		uint16_t random_len;
5906 
5907 		/* generate and add RANDOM parameter */
5908 		random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5909 		randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5910 		randp->ph.param_type = htons(SCTP_RANDOM);
5911 		p_len = sizeof(*randp) + random_len;
5912 		randp->ph.param_length = htons(p_len);
5913 		SCTP_READ_RANDOM(randp->random_data, random_len);
5914 		/* zero out any padding required */
5915 		bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5916 		SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5917 
5918 		/* add HMAC_ALGO parameter */
5919 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5920 		p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5921 		    (uint8_t *) hmacs->hmac_ids);
5922 		if (p_len > 0) {
5923 			p_len += sizeof(*hmacs);
5924 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5925 			hmacs->ph.param_length = htons(p_len);
5926 			/* zero out any padding required */
5927 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5928 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5929 		}
5930 		/* add CHUNKS parameter */
5931 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5932 		p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5933 		    chunks->chunk_types);
5934 		if (p_len > 0) {
5935 			p_len += sizeof(*chunks);
5936 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5937 			chunks->ph.param_length = htons(p_len);
5938 			/* zero out any padding required */
5939 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5940 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5941 		}
5942 	}
5943 	m_at = m;
5944 	/* now the addresses */
5945 	{
5946 		struct sctp_scoping scp;
5947 
5948 		/*
5949 		 * To optimize this we could put the scoping stuff into a
5950 		 * structure and remove the individual uint8's from the stc
5951 		 * structure. Then we could just sifa in the address within
5952 		 * the stc.. but for now this is a quick hack to get the
5953 		 * address stuff teased apart.
5954 		 */
5955 		scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5956 		scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5957 		scp.loopback_scope = stc.loopback_scope;
5958 		scp.ipv4_local_scope = stc.ipv4_scope;
5959 		scp.local_scope = stc.local_scope;
5960 		scp.site_scope = stc.site_scope;
5961 		m_at = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_at, cnt_inits_to);
5962 	}
5963 
5964 	/* tack on the operational error if present */
5965 	if (op_err) {
5966 		struct mbuf *ol;
5967 		int llen;
5968 
5969 		llen = 0;
5970 		ol = op_err;
5971 
5972 		while (ol) {
5973 			llen += SCTP_BUF_LEN(ol);
5974 			ol = SCTP_BUF_NEXT(ol);
5975 		}
5976 		if (llen % 4) {
5977 			/* must add a pad to the param */
5978 			uint32_t cpthis = 0;
5979 			int padlen;
5980 
5981 			padlen = 4 - (llen % 4);
5982 			m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5983 		}
5984 		while (SCTP_BUF_NEXT(m_at) != NULL) {
5985 			m_at = SCTP_BUF_NEXT(m_at);
5986 		}
5987 		SCTP_BUF_NEXT(m_at) = op_err;
5988 		while (SCTP_BUF_NEXT(m_at) != NULL) {
5989 			m_at = SCTP_BUF_NEXT(m_at);
5990 		}
5991 	}
5992 	/* pre-calulate the size and update pkt header and chunk header */
5993 	p_len = 0;
5994 	for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5995 		p_len += SCTP_BUF_LEN(m_tmp);
5996 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5997 			/* m_tmp should now point to last one */
5998 			break;
5999 		}
6000 	}
6001 
6002 	/* Now we must build a cookie */
6003 	m_cookie = sctp_add_cookie(init_pkt, offset, m, 0, &stc, &signature);
6004 	if (m_cookie == NULL) {
6005 		/* memory problem */
6006 		sctp_m_freem(m);
6007 		return;
6008 	}
6009 	/* Now append the cookie to the end and update the space/size */
6010 	SCTP_BUF_NEXT(m_tmp) = m_cookie;
6011 
6012 	for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
6013 		p_len += SCTP_BUF_LEN(m_tmp);
6014 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
6015 			/* m_tmp should now point to last one */
6016 			mp_last = m_tmp;
6017 			break;
6018 		}
6019 	}
6020 	/*
6021 	 * Place in the size, but we don't include the last pad (if any) in
6022 	 * the INIT-ACK.
6023 	 */
6024 	initack->ch.chunk_length = htons(p_len);
6025 
6026 	/*
6027 	 * Time to sign the cookie, we don't sign over the cookie signature
6028 	 * though thus we set trailer.
6029 	 */
6030 	(void)sctp_hmac_m(SCTP_HMAC,
6031 	    (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
6032 	    SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
6033 	    (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
6034 	/*
6035 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
6036 	 * here since the timer will drive a retranmission.
6037 	 */
6038 	padval = p_len % 4;
6039 	if ((padval) && (mp_last)) {
6040 		/* see my previous comments on mp_last */
6041 		if (sctp_add_pad_tombuf(mp_last, (4 - padval))) {
6042 			/* Houston we have a problem, no space */
6043 			sctp_m_freem(m);
6044 			return;
6045 		}
6046 	}
6047 	if (stc.loopback_scope) {
6048 		over_addr = &store1;
6049 	} else {
6050 		over_addr = NULL;
6051 	}
6052 
6053 	(void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
6054 	    0, 0,
6055 	    inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
6056 	    port, SCTP_SO_NOT_LOCKED, over_addr, init_pkt);
6057 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
6058 }
6059 
6060 
6061 static void
6062 sctp_prune_prsctp(struct sctp_tcb *stcb,
6063     struct sctp_association *asoc,
6064     struct sctp_sndrcvinfo *srcv,
6065     int dataout)
6066 {
6067 	int freed_spc = 0;
6068 	struct sctp_tmit_chunk *chk, *nchk;
6069 
6070 	SCTP_TCB_LOCK_ASSERT(stcb);
6071 	if ((asoc->peer_supports_prsctp) &&
6072 	    (asoc->sent_queue_cnt_removeable > 0)) {
6073 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6074 			/*
6075 			 * Look for chunks marked with the PR_SCTP flag AND
6076 			 * the buffer space flag. If the one being sent is
6077 			 * equal or greater priority then purge the old one
6078 			 * and free some space.
6079 			 */
6080 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6081 				/*
6082 				 * This one is PR-SCTP AND buffer space
6083 				 * limited type
6084 				 */
6085 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
6086 					/*
6087 					 * Lower numbers equates to higher
6088 					 * priority so if the one we are
6089 					 * looking at has a larger or equal
6090 					 * priority we want to drop the data
6091 					 * and NOT retransmit it.
6092 					 */
6093 					if (chk->data) {
6094 						/*
6095 						 * We release the book_size
6096 						 * if the mbuf is here
6097 						 */
6098 						int ret_spc;
6099 						int cause;
6100 
6101 						if (chk->sent > SCTP_DATAGRAM_UNSENT)
6102 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
6103 						else
6104 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
6105 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6106 						    cause,
6107 						    SCTP_SO_LOCKED);
6108 						freed_spc += ret_spc;
6109 						if (freed_spc >= dataout) {
6110 							return;
6111 						}
6112 					}	/* if chunk was present */
6113 				}	/* if of sufficent priority */
6114 			}	/* if chunk has enabled */
6115 		}		/* tailqforeach */
6116 
6117 		TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
6118 			/* Here we must move to the sent queue and mark */
6119 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6120 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
6121 					if (chk->data) {
6122 						/*
6123 						 * We release the book_size
6124 						 * if the mbuf is here
6125 						 */
6126 						int ret_spc;
6127 
6128 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6129 						    SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
6130 						    SCTP_SO_LOCKED);
6131 
6132 						freed_spc += ret_spc;
6133 						if (freed_spc >= dataout) {
6134 							return;
6135 						}
6136 					}	/* end if chk->data */
6137 				}	/* end if right class */
6138 			}	/* end if chk pr-sctp */
6139 		}		/* tailqforeachsafe (chk) */
6140 	}			/* if enabled in asoc */
6141 }
6142 
6143 int
6144 sctp_get_frag_point(struct sctp_tcb *stcb,
6145     struct sctp_association *asoc)
6146 {
6147 	int siz, ovh;
6148 
6149 	/*
6150 	 * For endpoints that have both v6 and v4 addresses we must reserve
6151 	 * room for the ipv6 header, for those that are only dealing with V4
6152 	 * we use a larger frag point.
6153 	 */
6154 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6155 		ovh = SCTP_MED_OVERHEAD;
6156 	} else {
6157 		ovh = SCTP_MED_V4_OVERHEAD;
6158 	}
6159 
6160 	if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
6161 		siz = asoc->smallest_mtu - ovh;
6162 	else
6163 		siz = (stcb->asoc.sctp_frag_point - ovh);
6164 	/*
6165 	 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
6166 	 */
6167 	/* A data chunk MUST fit in a cluster */
6168 	/* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
6169 	/* } */
6170 
6171 	/* adjust for an AUTH chunk if DATA requires auth */
6172 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
6173 		siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
6174 
6175 	if (siz % 4) {
6176 		/* make it an even word boundary please */
6177 		siz -= (siz % 4);
6178 	}
6179 	return (siz);
6180 }
6181 
6182 static void
6183 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
6184 {
6185 	sp->pr_sctp_on = 0;
6186 	/*
6187 	 * We assume that the user wants PR_SCTP_TTL if the user provides a
6188 	 * positive lifetime but does not specify any PR_SCTP policy. This
6189 	 * is a BAD assumption and causes problems at least with the
6190 	 * U-Vancovers MPI folks. I will change this to be no policy means
6191 	 * NO PR-SCTP.
6192 	 */
6193 	if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
6194 		sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
6195 		sp->pr_sctp_on = 1;
6196 	} else {
6197 		return;
6198 	}
6199 	switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
6200 	case CHUNK_FLAGS_PR_SCTP_BUF:
6201 		/*
6202 		 * Time to live is a priority stored in tv_sec when doing
6203 		 * the buffer drop thing.
6204 		 */
6205 		sp->ts.tv_sec = sp->timetolive;
6206 		sp->ts.tv_usec = 0;
6207 		break;
6208 	case CHUNK_FLAGS_PR_SCTP_TTL:
6209 		{
6210 			struct timeval tv;
6211 
6212 			(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6213 			tv.tv_sec = sp->timetolive / 1000;
6214 			tv.tv_usec = (sp->timetolive * 1000) % 1000000;
6215 			/*
6216 			 * TODO sctp_constants.h needs alternative time
6217 			 * macros when _KERNEL is undefined.
6218 			 */
6219 			timevaladd(&sp->ts, &tv);
6220 		}
6221 		break;
6222 	case CHUNK_FLAGS_PR_SCTP_RTX:
6223 		/*
6224 		 * Time to live is a the number or retransmissions stored in
6225 		 * tv_sec.
6226 		 */
6227 		sp->ts.tv_sec = sp->timetolive;
6228 		sp->ts.tv_usec = 0;
6229 		break;
6230 	default:
6231 		SCTPDBG(SCTP_DEBUG_USRREQ1,
6232 		    "Unknown PR_SCTP policy %u.\n",
6233 		    PR_SCTP_POLICY(sp->sinfo_flags));
6234 		break;
6235 	}
6236 }
6237 
6238 static int
6239 sctp_msg_append(struct sctp_tcb *stcb,
6240     struct sctp_nets *net,
6241     struct mbuf *m,
6242     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
6243 {
6244 	int error = 0;
6245 	struct mbuf *at;
6246 	struct sctp_stream_queue_pending *sp = NULL;
6247 	struct sctp_stream_out *strm;
6248 
6249 	/*
6250 	 * Given an mbuf chain, put it into the association send queue and
6251 	 * place it on the wheel
6252 	 */
6253 	if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
6254 		/* Invalid stream number */
6255 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6256 		error = EINVAL;
6257 		goto out_now;
6258 	}
6259 	if ((stcb->asoc.stream_locked) &&
6260 	    (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
6261 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6262 		error = EINVAL;
6263 		goto out_now;
6264 	}
6265 	strm = &stcb->asoc.strmout[srcv->sinfo_stream];
6266 	/* Now can we send this? */
6267 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
6268 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
6269 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
6270 	    (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
6271 		/* got data while shutting down */
6272 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
6273 		error = ECONNRESET;
6274 		goto out_now;
6275 	}
6276 	sctp_alloc_a_strmoq(stcb, sp);
6277 	if (sp == NULL) {
6278 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6279 		error = ENOMEM;
6280 		goto out_now;
6281 	}
6282 	sp->sinfo_flags = srcv->sinfo_flags;
6283 	sp->timetolive = srcv->sinfo_timetolive;
6284 	sp->ppid = srcv->sinfo_ppid;
6285 	sp->context = srcv->sinfo_context;
6286 	sp->strseq = 0;
6287 	if (sp->sinfo_flags & SCTP_ADDR_OVER) {
6288 		sp->net = net;
6289 		atomic_add_int(&sp->net->ref_count, 1);
6290 	} else {
6291 		sp->net = NULL;
6292 	}
6293 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6294 	sp->stream = srcv->sinfo_stream;
6295 	sp->msg_is_complete = 1;
6296 	sp->sender_all_done = 1;
6297 	sp->some_taken = 0;
6298 	sp->data = m;
6299 	sp->tail_mbuf = NULL;
6300 	sctp_set_prsctp_policy(sp);
6301 	/*
6302 	 * We could in theory (for sendall) sifa the length in, but we would
6303 	 * still have to hunt through the chain since we need to setup the
6304 	 * tail_mbuf
6305 	 */
6306 	sp->length = 0;
6307 	for (at = m; at; at = SCTP_BUF_NEXT(at)) {
6308 		if (SCTP_BUF_NEXT(at) == NULL)
6309 			sp->tail_mbuf = at;
6310 		sp->length += SCTP_BUF_LEN(at);
6311 	}
6312 	if (srcv->sinfo_keynumber_valid) {
6313 		sp->auth_keyid = srcv->sinfo_keynumber;
6314 	} else {
6315 		sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
6316 	}
6317 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
6318 		sctp_auth_key_acquire(stcb, sp->auth_keyid);
6319 		sp->holds_key_ref = 1;
6320 	}
6321 	if (hold_stcb_lock == 0) {
6322 		SCTP_TCB_SEND_LOCK(stcb);
6323 	}
6324 	sctp_snd_sb_alloc(stcb, sp->length);
6325 	atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
6326 	TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
6327 	if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
6328 		sp->strseq = strm->next_sequence_sent;
6329 		strm->next_sequence_sent++;
6330 	}
6331 	stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp, 1);
6332 	m = NULL;
6333 	if (hold_stcb_lock == 0) {
6334 		SCTP_TCB_SEND_UNLOCK(stcb);
6335 	}
6336 out_now:
6337 	if (m) {
6338 		sctp_m_freem(m);
6339 	}
6340 	return (error);
6341 }
6342 
6343 
6344 static struct mbuf *
6345 sctp_copy_mbufchain(struct mbuf *clonechain,
6346     struct mbuf *outchain,
6347     struct mbuf **endofchain,
6348     int can_take_mbuf,
6349     int sizeofcpy,
6350     uint8_t copy_by_ref)
6351 {
6352 	struct mbuf *m;
6353 	struct mbuf *appendchain;
6354 	caddr_t cp;
6355 	int len;
6356 
6357 	if (endofchain == NULL) {
6358 		/* error */
6359 error_out:
6360 		if (outchain)
6361 			sctp_m_freem(outchain);
6362 		return (NULL);
6363 	}
6364 	if (can_take_mbuf) {
6365 		appendchain = clonechain;
6366 	} else {
6367 		if (!copy_by_ref &&
6368 		    (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
6369 		    ) {
6370 			/* Its not in a cluster */
6371 			if (*endofchain == NULL) {
6372 				/* lets get a mbuf cluster */
6373 				if (outchain == NULL) {
6374 					/* This is the general case */
6375 			new_mbuf:
6376 					outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6377 					if (outchain == NULL) {
6378 						goto error_out;
6379 					}
6380 					SCTP_BUF_LEN(outchain) = 0;
6381 					*endofchain = outchain;
6382 					/* get the prepend space */
6383 					SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
6384 				} else {
6385 					/*
6386 					 * We really should not get a NULL
6387 					 * in endofchain
6388 					 */
6389 					/* find end */
6390 					m = outchain;
6391 					while (m) {
6392 						if (SCTP_BUF_NEXT(m) == NULL) {
6393 							*endofchain = m;
6394 							break;
6395 						}
6396 						m = SCTP_BUF_NEXT(m);
6397 					}
6398 					/* sanity */
6399 					if (*endofchain == NULL) {
6400 						/*
6401 						 * huh, TSNH XXX maybe we
6402 						 * should panic
6403 						 */
6404 						sctp_m_freem(outchain);
6405 						goto new_mbuf;
6406 					}
6407 				}
6408 				/* get the new end of length */
6409 				len = M_TRAILINGSPACE(*endofchain);
6410 			} else {
6411 				/* how much is left at the end? */
6412 				len = M_TRAILINGSPACE(*endofchain);
6413 			}
6414 			/* Find the end of the data, for appending */
6415 			cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
6416 
6417 			/* Now lets copy it out */
6418 			if (len >= sizeofcpy) {
6419 				/* It all fits, copy it in */
6420 				m_copydata(clonechain, 0, sizeofcpy, cp);
6421 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6422 			} else {
6423 				/* fill up the end of the chain */
6424 				if (len > 0) {
6425 					m_copydata(clonechain, 0, len, cp);
6426 					SCTP_BUF_LEN((*endofchain)) += len;
6427 					/* now we need another one */
6428 					sizeofcpy -= len;
6429 				}
6430 				m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6431 				if (m == NULL) {
6432 					/* We failed */
6433 					goto error_out;
6434 				}
6435 				SCTP_BUF_NEXT((*endofchain)) = m;
6436 				*endofchain = m;
6437 				cp = mtod((*endofchain), caddr_t);
6438 				m_copydata(clonechain, len, sizeofcpy, cp);
6439 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6440 			}
6441 			return (outchain);
6442 		} else {
6443 			/* copy the old fashion way */
6444 			appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
6445 #ifdef SCTP_MBUF_LOGGING
6446 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6447 				struct mbuf *mat;
6448 
6449 				mat = appendchain;
6450 				while (mat) {
6451 					if (SCTP_BUF_IS_EXTENDED(mat)) {
6452 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6453 					}
6454 					mat = SCTP_BUF_NEXT(mat);
6455 				}
6456 			}
6457 #endif
6458 		}
6459 	}
6460 	if (appendchain == NULL) {
6461 		/* error */
6462 		if (outchain)
6463 			sctp_m_freem(outchain);
6464 		return (NULL);
6465 	}
6466 	if (outchain) {
6467 		/* tack on to the end */
6468 		if (*endofchain != NULL) {
6469 			SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6470 		} else {
6471 			m = outchain;
6472 			while (m) {
6473 				if (SCTP_BUF_NEXT(m) == NULL) {
6474 					SCTP_BUF_NEXT(m) = appendchain;
6475 					break;
6476 				}
6477 				m = SCTP_BUF_NEXT(m);
6478 			}
6479 		}
6480 		/*
6481 		 * save off the end and update the end-chain postion
6482 		 */
6483 		m = appendchain;
6484 		while (m) {
6485 			if (SCTP_BUF_NEXT(m) == NULL) {
6486 				*endofchain = m;
6487 				break;
6488 			}
6489 			m = SCTP_BUF_NEXT(m);
6490 		}
6491 		return (outchain);
6492 	} else {
6493 		/* save off the end and update the end-chain postion */
6494 		m = appendchain;
6495 		while (m) {
6496 			if (SCTP_BUF_NEXT(m) == NULL) {
6497 				*endofchain = m;
6498 				break;
6499 			}
6500 			m = SCTP_BUF_NEXT(m);
6501 		}
6502 		return (appendchain);
6503 	}
6504 }
6505 
6506 int
6507 sctp_med_chunk_output(struct sctp_inpcb *inp,
6508     struct sctp_tcb *stcb,
6509     struct sctp_association *asoc,
6510     int *num_out,
6511     int *reason_code,
6512     int control_only, int from_where,
6513     struct timeval *now, int *now_filled, int frag_point, int so_locked
6514 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6515     SCTP_UNUSED
6516 #endif
6517 );
6518 
6519 static void
6520 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6521     uint32_t val SCTP_UNUSED)
6522 {
6523 	struct sctp_copy_all *ca;
6524 	struct mbuf *m;
6525 	int ret = 0;
6526 	int added_control = 0;
6527 	int un_sent, do_chunk_output = 1;
6528 	struct sctp_association *asoc;
6529 	struct sctp_nets *net;
6530 
6531 	ca = (struct sctp_copy_all *)ptr;
6532 	if (ca->m == NULL) {
6533 		return;
6534 	}
6535 	if (ca->inp != inp) {
6536 		/* TSNH */
6537 		return;
6538 	}
6539 	if ((ca->m) && ca->sndlen) {
6540 		m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6541 		if (m == NULL) {
6542 			/* can't copy so we are done */
6543 			ca->cnt_failed++;
6544 			return;
6545 		}
6546 #ifdef SCTP_MBUF_LOGGING
6547 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6548 			struct mbuf *mat;
6549 
6550 			mat = m;
6551 			while (mat) {
6552 				if (SCTP_BUF_IS_EXTENDED(mat)) {
6553 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6554 				}
6555 				mat = SCTP_BUF_NEXT(mat);
6556 			}
6557 		}
6558 #endif
6559 	} else {
6560 		m = NULL;
6561 	}
6562 	SCTP_TCB_LOCK_ASSERT(stcb);
6563 	if (stcb->asoc.alternate) {
6564 		net = stcb->asoc.alternate;
6565 	} else {
6566 		net = stcb->asoc.primary_destination;
6567 	}
6568 	if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6569 		/* Abort this assoc with m as the user defined reason */
6570 		if (m) {
6571 			struct sctp_paramhdr *ph;
6572 
6573 			SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6574 			if (m) {
6575 				ph = mtod(m, struct sctp_paramhdr *);
6576 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6577 				ph->param_length = htons(ca->sndlen);
6578 			}
6579 			/*
6580 			 * We add one here to keep the assoc from
6581 			 * dis-appearing on us.
6582 			 */
6583 			atomic_add_int(&stcb->asoc.refcnt, 1);
6584 			sctp_abort_an_association(inp, stcb,
6585 			    SCTP_RESPONSE_TO_USER_REQ,
6586 			    m, SCTP_SO_NOT_LOCKED);
6587 			/*
6588 			 * sctp_abort_an_association calls sctp_free_asoc()
6589 			 * free association will NOT free it since we
6590 			 * incremented the refcnt .. we do this to prevent
6591 			 * it being freed and things getting tricky since we
6592 			 * could end up (from free_asoc) calling inpcb_free
6593 			 * which would get a recursive lock call to the
6594 			 * iterator lock.. But as a consequence of that the
6595 			 * stcb will return to us un-locked.. since
6596 			 * free_asoc returns with either no TCB or the TCB
6597 			 * unlocked, we must relock.. to unlock in the
6598 			 * iterator timer :-0
6599 			 */
6600 			SCTP_TCB_LOCK(stcb);
6601 			atomic_add_int(&stcb->asoc.refcnt, -1);
6602 			goto no_chunk_output;
6603 		}
6604 	} else {
6605 		if (m) {
6606 			ret = sctp_msg_append(stcb, net, m,
6607 			    &ca->sndrcv, 1);
6608 		}
6609 		asoc = &stcb->asoc;
6610 		if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6611 			/* shutdown this assoc */
6612 			int cnt;
6613 
6614 			cnt = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
6615 
6616 			if (TAILQ_EMPTY(&asoc->send_queue) &&
6617 			    TAILQ_EMPTY(&asoc->sent_queue) &&
6618 			    (cnt == 0)) {
6619 				if (asoc->locked_on_sending) {
6620 					goto abort_anyway;
6621 				}
6622 				/*
6623 				 * there is nothing queued to send, so I'm
6624 				 * done...
6625 				 */
6626 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6627 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6628 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6629 					/*
6630 					 * only send SHUTDOWN the first time
6631 					 * through
6632 					 */
6633 					sctp_send_shutdown(stcb, net);
6634 					if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6635 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6636 					}
6637 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6638 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6639 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6640 					    net);
6641 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6642 					    asoc->primary_destination);
6643 					added_control = 1;
6644 					do_chunk_output = 0;
6645 				}
6646 			} else {
6647 				/*
6648 				 * we still got (or just got) data to send,
6649 				 * so set SHUTDOWN_PENDING
6650 				 */
6651 				/*
6652 				 * XXX sockets draft says that SCTP_EOF
6653 				 * should be sent with no data.  currently,
6654 				 * we will allow user data to be sent first
6655 				 * and move to SHUTDOWN-PENDING
6656 				 */
6657 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6658 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6659 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6660 					if (asoc->locked_on_sending) {
6661 						/*
6662 						 * Locked to send out the
6663 						 * data
6664 						 */
6665 						struct sctp_stream_queue_pending *sp;
6666 
6667 						sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6668 						if (sp) {
6669 							if ((sp->length == 0) && (sp->msg_is_complete == 0))
6670 								asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6671 						}
6672 					}
6673 					asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6674 					if (TAILQ_EMPTY(&asoc->send_queue) &&
6675 					    TAILQ_EMPTY(&asoc->sent_queue) &&
6676 					    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6677 				abort_anyway:
6678 						atomic_add_int(&stcb->asoc.refcnt, 1);
6679 						sctp_abort_an_association(stcb->sctp_ep, stcb,
6680 						    SCTP_RESPONSE_TO_USER_REQ,
6681 						    NULL, SCTP_SO_NOT_LOCKED);
6682 						atomic_add_int(&stcb->asoc.refcnt, -1);
6683 						goto no_chunk_output;
6684 					}
6685 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6686 					    asoc->primary_destination);
6687 				}
6688 			}
6689 
6690 		}
6691 	}
6692 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6693 	    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6694 
6695 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6696 	    (stcb->asoc.total_flight > 0) &&
6697 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6698 	    ) {
6699 		do_chunk_output = 0;
6700 	}
6701 	if (do_chunk_output)
6702 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6703 	else if (added_control) {
6704 		int num_out = 0, reason = 0, now_filled = 0;
6705 		struct timeval now;
6706 		int frag_point;
6707 
6708 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6709 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6710 		    &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6711 	}
6712 no_chunk_output:
6713 	if (ret) {
6714 		ca->cnt_failed++;
6715 	} else {
6716 		ca->cnt_sent++;
6717 	}
6718 }
6719 
6720 static void
6721 sctp_sendall_completes(void *ptr, uint32_t val SCTP_UNUSED)
6722 {
6723 	struct sctp_copy_all *ca;
6724 
6725 	ca = (struct sctp_copy_all *)ptr;
6726 	/*
6727 	 * Do a notify here? Kacheong suggests that the notify be done at
6728 	 * the send time.. so you would push up a notification if any send
6729 	 * failed. Don't know if this is feasable since the only failures we
6730 	 * have is "memory" related and if you cannot get an mbuf to send
6731 	 * the data you surely can't get an mbuf to send up to notify the
6732 	 * user you can't send the data :->
6733 	 */
6734 
6735 	/* now free everything */
6736 	sctp_m_freem(ca->m);
6737 	SCTP_FREE(ca, SCTP_M_COPYAL);
6738 }
6739 
6740 
6741 #define	MC_ALIGN(m, len) do {						\
6742 	SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1));	\
6743 } while (0)
6744 
6745 
6746 
6747 static struct mbuf *
6748 sctp_copy_out_all(struct uio *uio, int len)
6749 {
6750 	struct mbuf *ret, *at;
6751 	int left, willcpy, cancpy, error;
6752 
6753 	ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6754 	if (ret == NULL) {
6755 		/* TSNH */
6756 		return (NULL);
6757 	}
6758 	left = len;
6759 	SCTP_BUF_LEN(ret) = 0;
6760 	/* save space for the data chunk header */
6761 	cancpy = M_TRAILINGSPACE(ret);
6762 	willcpy = min(cancpy, left);
6763 	at = ret;
6764 	while (left > 0) {
6765 		/* Align data to the end */
6766 		error = uiomove(mtod(at, caddr_t), willcpy, uio);
6767 		if (error) {
6768 	err_out_now:
6769 			sctp_m_freem(at);
6770 			return (NULL);
6771 		}
6772 		SCTP_BUF_LEN(at) = willcpy;
6773 		SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6774 		left -= willcpy;
6775 		if (left > 0) {
6776 			SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6777 			if (SCTP_BUF_NEXT(at) == NULL) {
6778 				goto err_out_now;
6779 			}
6780 			at = SCTP_BUF_NEXT(at);
6781 			SCTP_BUF_LEN(at) = 0;
6782 			cancpy = M_TRAILINGSPACE(at);
6783 			willcpy = min(cancpy, left);
6784 		}
6785 	}
6786 	return (ret);
6787 }
6788 
6789 static int
6790 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6791     struct sctp_sndrcvinfo *srcv)
6792 {
6793 	int ret;
6794 	struct sctp_copy_all *ca;
6795 
6796 	SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6797 	    SCTP_M_COPYAL);
6798 	if (ca == NULL) {
6799 		sctp_m_freem(m);
6800 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6801 		return (ENOMEM);
6802 	}
6803 	memset(ca, 0, sizeof(struct sctp_copy_all));
6804 
6805 	ca->inp = inp;
6806 	if (srcv) {
6807 		memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6808 	}
6809 	/*
6810 	 * take off the sendall flag, it would be bad if we failed to do
6811 	 * this :-0
6812 	 */
6813 	ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6814 	/* get length and mbuf chain */
6815 	if (uio) {
6816 		ca->sndlen = uio->uio_resid;
6817 		ca->m = sctp_copy_out_all(uio, ca->sndlen);
6818 		if (ca->m == NULL) {
6819 			SCTP_FREE(ca, SCTP_M_COPYAL);
6820 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6821 			return (ENOMEM);
6822 		}
6823 	} else {
6824 		/* Gather the length of the send */
6825 		struct mbuf *mat;
6826 
6827 		mat = m;
6828 		ca->sndlen = 0;
6829 		while (m) {
6830 			ca->sndlen += SCTP_BUF_LEN(m);
6831 			m = SCTP_BUF_NEXT(m);
6832 		}
6833 		ca->m = mat;
6834 	}
6835 	ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6836 	    SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6837 	    SCTP_ASOC_ANY_STATE,
6838 	    (void *)ca, 0,
6839 	    sctp_sendall_completes, inp, 1);
6840 	if (ret) {
6841 		SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6842 		SCTP_FREE(ca, SCTP_M_COPYAL);
6843 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6844 		return (EFAULT);
6845 	}
6846 	return (0);
6847 }
6848 
6849 
6850 void
6851 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6852 {
6853 	struct sctp_tmit_chunk *chk, *nchk;
6854 
6855 	TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
6856 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6857 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6858 			if (chk->data) {
6859 				sctp_m_freem(chk->data);
6860 				chk->data = NULL;
6861 			}
6862 			asoc->ctrl_queue_cnt--;
6863 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6864 		}
6865 	}
6866 }
6867 
6868 void
6869 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6870 {
6871 	struct sctp_association *asoc;
6872 	struct sctp_tmit_chunk *chk, *nchk;
6873 	struct sctp_asconf_chunk *acp;
6874 
6875 	asoc = &stcb->asoc;
6876 	TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
6877 		/* find SCTP_ASCONF chunk in queue */
6878 		if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6879 			if (chk->data) {
6880 				acp = mtod(chk->data, struct sctp_asconf_chunk *);
6881 				if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) {
6882 					/* Not Acked yet */
6883 					break;
6884 				}
6885 			}
6886 			TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6887 			if (chk->data) {
6888 				sctp_m_freem(chk->data);
6889 				chk->data = NULL;
6890 			}
6891 			asoc->ctrl_queue_cnt--;
6892 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6893 		}
6894 	}
6895 }
6896 
6897 
6898 static void
6899 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6900     struct sctp_association *asoc,
6901     struct sctp_tmit_chunk **data_list,
6902     int bundle_at,
6903     struct sctp_nets *net)
6904 {
6905 	int i;
6906 	struct sctp_tmit_chunk *tp1;
6907 
6908 	for (i = 0; i < bundle_at; i++) {
6909 		/* off of the send queue */
6910 		TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next);
6911 		asoc->send_queue_cnt--;
6912 		if (i > 0) {
6913 			/*
6914 			 * Any chunk NOT 0 you zap the time chunk 0 gets
6915 			 * zapped or set based on if a RTO measurment is
6916 			 * needed.
6917 			 */
6918 			data_list[i]->do_rtt = 0;
6919 		}
6920 		/* record time */
6921 		data_list[i]->sent_rcv_time = net->last_sent_time;
6922 		data_list[i]->rec.data.cwnd_at_send = net->cwnd;
6923 		data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6924 		if (data_list[i]->whoTo == NULL) {
6925 			data_list[i]->whoTo = net;
6926 			atomic_add_int(&net->ref_count, 1);
6927 		}
6928 		/* on to the sent queue */
6929 		tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6930 		if ((tp1) && SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) {
6931 			struct sctp_tmit_chunk *tpp;
6932 
6933 			/* need to move back */
6934 	back_up_more:
6935 			tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6936 			if (tpp == NULL) {
6937 				TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6938 				goto all_done;
6939 			}
6940 			tp1 = tpp;
6941 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) {
6942 				goto back_up_more;
6943 			}
6944 			TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6945 		} else {
6946 			TAILQ_INSERT_TAIL(&asoc->sent_queue,
6947 			    data_list[i],
6948 			    sctp_next);
6949 		}
6950 all_done:
6951 		/* This does not lower until the cum-ack passes it */
6952 		asoc->sent_queue_cnt++;
6953 		if ((asoc->peers_rwnd <= 0) &&
6954 		    (asoc->total_flight == 0) &&
6955 		    (bundle_at == 1)) {
6956 			/* Mark the chunk as being a window probe */
6957 			SCTP_STAT_INCR(sctps_windowprobed);
6958 		}
6959 #ifdef SCTP_AUDITING_ENABLED
6960 		sctp_audit_log(0xC2, 3);
6961 #endif
6962 		data_list[i]->sent = SCTP_DATAGRAM_SENT;
6963 		data_list[i]->snd_count = 1;
6964 		data_list[i]->rec.data.chunk_was_revoked = 0;
6965 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6966 			sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6967 			    data_list[i]->whoTo->flight_size,
6968 			    data_list[i]->book_size,
6969 			    (uintptr_t) data_list[i]->whoTo,
6970 			    data_list[i]->rec.data.TSN_seq);
6971 		}
6972 		sctp_flight_size_increase(data_list[i]);
6973 		sctp_total_flight_increase(stcb, data_list[i]);
6974 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6975 			sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6976 			    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6977 		}
6978 		asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6979 		    (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6980 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6981 			/* SWS sender side engages */
6982 			asoc->peers_rwnd = 0;
6983 		}
6984 	}
6985 	if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) {
6986 		(*asoc->cc_functions.sctp_cwnd_update_packet_transmitted) (stcb, net);
6987 	}
6988 }
6989 
6990 static void
6991 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked
6992 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6993     SCTP_UNUSED
6994 #endif
6995 )
6996 {
6997 	struct sctp_tmit_chunk *chk, *nchk;
6998 
6999 	TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
7000 		if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7001 		    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
7002 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7003 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7004 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
7005 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7006 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7007 		    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7008 		    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7009 		    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7010 		    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7011 		    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7012 			/* Stray chunks must be cleaned up */
7013 	clean_up_anyway:
7014 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7015 			if (chk->data) {
7016 				sctp_m_freem(chk->data);
7017 				chk->data = NULL;
7018 			}
7019 			asoc->ctrl_queue_cnt--;
7020 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)
7021 				asoc->fwd_tsn_cnt--;
7022 			sctp_free_a_chunk(stcb, chk, so_locked);
7023 		} else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
7024 			/* special handling, we must look into the param */
7025 			if (chk != asoc->str_reset) {
7026 				goto clean_up_anyway;
7027 			}
7028 		}
7029 	}
7030 }
7031 
7032 
7033 static int
7034 sctp_can_we_split_this(struct sctp_tcb *stcb,
7035     uint32_t length,
7036     uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
7037 {
7038 	/*
7039 	 * Make a decision on if I should split a msg into multiple parts.
7040 	 * This is only asked of incomplete messages.
7041 	 */
7042 	if (eeor_on) {
7043 		/*
7044 		 * If we are doing EEOR we need to always send it if its the
7045 		 * entire thing, since it might be all the guy is putting in
7046 		 * the hopper.
7047 		 */
7048 		if (goal_mtu >= length) {
7049 			/*-
7050 			 * If we have data outstanding,
7051 			 * we get another chance when the sack
7052 			 * arrives to transmit - wait for more data
7053 			 */
7054 			if (stcb->asoc.total_flight == 0) {
7055 				/*
7056 				 * If nothing is in flight, we zero the
7057 				 * packet counter.
7058 				 */
7059 				return (length);
7060 			}
7061 			return (0);
7062 
7063 		} else {
7064 			/* You can fill the rest */
7065 			return (goal_mtu);
7066 		}
7067 	}
7068 	/*-
7069 	 * For those strange folk that make the send buffer
7070 	 * smaller than our fragmentation point, we can't
7071 	 * get a full msg in so we have to allow splitting.
7072 	 */
7073 	if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
7074 		return (length);
7075 	}
7076 	if ((length <= goal_mtu) ||
7077 	    ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
7078 		/* Sub-optimial residual don't split in non-eeor mode. */
7079 		return (0);
7080 	}
7081 	/*
7082 	 * If we reach here length is larger than the goal_mtu. Do we wish
7083 	 * to split it for the sake of packet putting together?
7084 	 */
7085 	if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
7086 		/* Its ok to split it */
7087 		return (min(goal_mtu, frag_point));
7088 	}
7089 	/* Nope, can't split */
7090 	return (0);
7091 
7092 }
7093 
7094 static uint32_t
7095 sctp_move_to_outqueue(struct sctp_tcb *stcb,
7096     struct sctp_stream_out *strq,
7097     uint32_t goal_mtu,
7098     uint32_t frag_point,
7099     int *locked,
7100     int *giveup,
7101     int eeor_mode,
7102     int *bail,
7103     int so_locked
7104 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7105     SCTP_UNUSED
7106 #endif
7107 )
7108 {
7109 	/* Move from the stream to the send_queue keeping track of the total */
7110 	struct sctp_association *asoc;
7111 	struct sctp_stream_queue_pending *sp;
7112 	struct sctp_tmit_chunk *chk;
7113 	struct sctp_data_chunk *dchkh;
7114 	uint32_t to_move, length;
7115 	uint8_t rcv_flags = 0;
7116 	uint8_t some_taken;
7117 	uint8_t send_lock_up = 0;
7118 
7119 	SCTP_TCB_LOCK_ASSERT(stcb);
7120 	asoc = &stcb->asoc;
7121 one_more_time:
7122 	/* sa_ignore FREED_MEMORY */
7123 	sp = TAILQ_FIRST(&strq->outqueue);
7124 	if (sp == NULL) {
7125 		*locked = 0;
7126 		if (send_lock_up == 0) {
7127 			SCTP_TCB_SEND_LOCK(stcb);
7128 			send_lock_up = 1;
7129 		}
7130 		sp = TAILQ_FIRST(&strq->outqueue);
7131 		if (sp) {
7132 			goto one_more_time;
7133 		}
7134 		if (strq->last_msg_incomplete) {
7135 			SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
7136 			    strq->stream_no,
7137 			    strq->last_msg_incomplete);
7138 			strq->last_msg_incomplete = 0;
7139 		}
7140 		to_move = 0;
7141 		if (send_lock_up) {
7142 			SCTP_TCB_SEND_UNLOCK(stcb);
7143 			send_lock_up = 0;
7144 		}
7145 		goto out_of;
7146 	}
7147 	if ((sp->msg_is_complete) && (sp->length == 0)) {
7148 		if (sp->sender_all_done) {
7149 			/*
7150 			 * We are doing differed cleanup. Last time through
7151 			 * when we took all the data the sender_all_done was
7152 			 * not set.
7153 			 */
7154 			if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
7155 				SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
7156 				SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7157 				    sp->sender_all_done,
7158 				    sp->length,
7159 				    sp->msg_is_complete,
7160 				    sp->put_last_out,
7161 				    send_lock_up);
7162 			}
7163 			if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
7164 				SCTP_TCB_SEND_LOCK(stcb);
7165 				send_lock_up = 1;
7166 			}
7167 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7168 			TAILQ_REMOVE(&strq->outqueue, sp, next);
7169 			stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up);
7170 			if (sp->net) {
7171 				sctp_free_remote_addr(sp->net);
7172 				sp->net = NULL;
7173 			}
7174 			if (sp->data) {
7175 				sctp_m_freem(sp->data);
7176 				sp->data = NULL;
7177 			}
7178 			sctp_free_a_strmoq(stcb, sp, so_locked);
7179 			/* we can't be locked to it */
7180 			*locked = 0;
7181 			stcb->asoc.locked_on_sending = NULL;
7182 			if (send_lock_up) {
7183 				SCTP_TCB_SEND_UNLOCK(stcb);
7184 				send_lock_up = 0;
7185 			}
7186 			/* back to get the next msg */
7187 			goto one_more_time;
7188 		} else {
7189 			/*
7190 			 * sender just finished this but still holds a
7191 			 * reference
7192 			 */
7193 			*locked = 1;
7194 			*giveup = 1;
7195 			to_move = 0;
7196 			goto out_of;
7197 		}
7198 	} else {
7199 		/* is there some to get */
7200 		if (sp->length == 0) {
7201 			/* no */
7202 			*locked = 1;
7203 			*giveup = 1;
7204 			to_move = 0;
7205 			goto out_of;
7206 		} else if (sp->discard_rest) {
7207 			if (send_lock_up == 0) {
7208 				SCTP_TCB_SEND_LOCK(stcb);
7209 				send_lock_up = 1;
7210 			}
7211 			/* Whack down the size */
7212 			atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
7213 			if ((stcb->sctp_socket != NULL) && \
7214 			    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7215 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
7216 				atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
7217 			}
7218 			if (sp->data) {
7219 				sctp_m_freem(sp->data);
7220 				sp->data = NULL;
7221 				sp->tail_mbuf = NULL;
7222 			}
7223 			sp->length = 0;
7224 			sp->some_taken = 1;
7225 			*locked = 1;
7226 			*giveup = 1;
7227 			to_move = 0;
7228 			goto out_of;
7229 		}
7230 	}
7231 	some_taken = sp->some_taken;
7232 	if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
7233 		sp->msg_is_complete = 1;
7234 	}
7235 re_look:
7236 	length = sp->length;
7237 	if (sp->msg_is_complete) {
7238 		/* The message is complete */
7239 		to_move = min(length, frag_point);
7240 		if (to_move == length) {
7241 			/* All of it fits in the MTU */
7242 			if (sp->some_taken) {
7243 				rcv_flags |= SCTP_DATA_LAST_FRAG;
7244 				sp->put_last_out = 1;
7245 			} else {
7246 				rcv_flags |= SCTP_DATA_NOT_FRAG;
7247 				sp->put_last_out = 1;
7248 			}
7249 		} else {
7250 			/* Not all of it fits, we fragment */
7251 			if (sp->some_taken == 0) {
7252 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
7253 			}
7254 			sp->some_taken = 1;
7255 		}
7256 	} else {
7257 		to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
7258 		if (to_move) {
7259 			/*-
7260 			 * We use a snapshot of length in case it
7261 			 * is expanding during the compare.
7262 			 */
7263 			uint32_t llen;
7264 
7265 			llen = length;
7266 			if (to_move >= llen) {
7267 				to_move = llen;
7268 				if (send_lock_up == 0) {
7269 					/*-
7270 					 * We are taking all of an incomplete msg
7271 					 * thus we need a send lock.
7272 					 */
7273 					SCTP_TCB_SEND_LOCK(stcb);
7274 					send_lock_up = 1;
7275 					if (sp->msg_is_complete) {
7276 						/*
7277 						 * the sender finished the
7278 						 * msg
7279 						 */
7280 						goto re_look;
7281 					}
7282 				}
7283 			}
7284 			if (sp->some_taken == 0) {
7285 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
7286 				sp->some_taken = 1;
7287 			}
7288 		} else {
7289 			/* Nothing to take. */
7290 			if (sp->some_taken) {
7291 				*locked = 1;
7292 			}
7293 			*giveup = 1;
7294 			to_move = 0;
7295 			goto out_of;
7296 		}
7297 	}
7298 
7299 	/* If we reach here, we can copy out a chunk */
7300 	sctp_alloc_a_chunk(stcb, chk);
7301 	if (chk == NULL) {
7302 		/* No chunk memory */
7303 		*giveup = 1;
7304 		to_move = 0;
7305 		goto out_of;
7306 	}
7307 	/*
7308 	 * Setup for unordered if needed by looking at the user sent info
7309 	 * flags.
7310 	 */
7311 	if (sp->sinfo_flags & SCTP_UNORDERED) {
7312 		rcv_flags |= SCTP_DATA_UNORDERED;
7313 	}
7314 	if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
7315 	    ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
7316 		rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
7317 	}
7318 	/* clear out the chunk before setting up */
7319 	memset(chk, 0, sizeof(*chk));
7320 	chk->rec.data.rcv_flags = rcv_flags;
7321 
7322 	if (to_move >= length) {
7323 		/* we think we can steal the whole thing */
7324 		if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
7325 			SCTP_TCB_SEND_LOCK(stcb);
7326 			send_lock_up = 1;
7327 		}
7328 		if (to_move < sp->length) {
7329 			/* bail, it changed */
7330 			goto dont_do_it;
7331 		}
7332 		chk->data = sp->data;
7333 		chk->last_mbuf = sp->tail_mbuf;
7334 		/* register the stealing */
7335 		sp->data = sp->tail_mbuf = NULL;
7336 	} else {
7337 		struct mbuf *m;
7338 
7339 dont_do_it:
7340 		chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
7341 		chk->last_mbuf = NULL;
7342 		if (chk->data == NULL) {
7343 			sp->some_taken = some_taken;
7344 			sctp_free_a_chunk(stcb, chk, so_locked);
7345 			*bail = 1;
7346 			to_move = 0;
7347 			goto out_of;
7348 		}
7349 #ifdef SCTP_MBUF_LOGGING
7350 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7351 			struct mbuf *mat;
7352 
7353 			mat = chk->data;
7354 			while (mat) {
7355 				if (SCTP_BUF_IS_EXTENDED(mat)) {
7356 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
7357 				}
7358 				mat = SCTP_BUF_NEXT(mat);
7359 			}
7360 		}
7361 #endif
7362 		/* Pull off the data */
7363 		m_adj(sp->data, to_move);
7364 		/* Now lets work our way down and compact it */
7365 		m = sp->data;
7366 		while (m && (SCTP_BUF_LEN(m) == 0)) {
7367 			sp->data = SCTP_BUF_NEXT(m);
7368 			SCTP_BUF_NEXT(m) = NULL;
7369 			if (sp->tail_mbuf == m) {
7370 				/*-
7371 				 * Freeing tail? TSNH since
7372 				 * we supposedly were taking less
7373 				 * than the sp->length.
7374 				 */
7375 #ifdef INVARIANTS
7376 				panic("Huh, freing tail? - TSNH");
7377 #else
7378 				SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
7379 				sp->tail_mbuf = sp->data = NULL;
7380 				sp->length = 0;
7381 #endif
7382 
7383 			}
7384 			sctp_m_free(m);
7385 			m = sp->data;
7386 		}
7387 	}
7388 	if (SCTP_BUF_IS_EXTENDED(chk->data)) {
7389 		chk->copy_by_ref = 1;
7390 	} else {
7391 		chk->copy_by_ref = 0;
7392 	}
7393 	/*
7394 	 * get last_mbuf and counts of mb useage This is ugly but hopefully
7395 	 * its only one mbuf.
7396 	 */
7397 	if (chk->last_mbuf == NULL) {
7398 		chk->last_mbuf = chk->data;
7399 		while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
7400 			chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
7401 		}
7402 	}
7403 	if (to_move > length) {
7404 		/*- This should not happen either
7405 		 * since we always lower to_move to the size
7406 		 * of sp->length if its larger.
7407 		 */
7408 #ifdef INVARIANTS
7409 		panic("Huh, how can to_move be larger?");
7410 #else
7411 		SCTP_PRINTF("Huh, how can to_move be larger?\n");
7412 		sp->length = 0;
7413 #endif
7414 	} else {
7415 		atomic_subtract_int(&sp->length, to_move);
7416 	}
7417 	if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
7418 		/* Not enough room for a chunk header, get some */
7419 		struct mbuf *m;
7420 
7421 		m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
7422 		if (m == NULL) {
7423 			/*
7424 			 * we're in trouble here. _PREPEND below will free
7425 			 * all the data if there is no leading space, so we
7426 			 * must put the data back and restore.
7427 			 */
7428 			if (send_lock_up == 0) {
7429 				SCTP_TCB_SEND_LOCK(stcb);
7430 				send_lock_up = 1;
7431 			}
7432 			if (chk->data == NULL) {
7433 				/* unsteal the data */
7434 				sp->data = chk->data;
7435 				sp->tail_mbuf = chk->last_mbuf;
7436 			} else {
7437 				struct mbuf *m_tmp;
7438 
7439 				/* reassemble the data */
7440 				m_tmp = sp->data;
7441 				sp->data = chk->data;
7442 				SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7443 			}
7444 			sp->some_taken = some_taken;
7445 			atomic_add_int(&sp->length, to_move);
7446 			chk->data = NULL;
7447 			*bail = 1;
7448 			sctp_free_a_chunk(stcb, chk, so_locked);
7449 			to_move = 0;
7450 			goto out_of;
7451 		} else {
7452 			SCTP_BUF_LEN(m) = 0;
7453 			SCTP_BUF_NEXT(m) = chk->data;
7454 			chk->data = m;
7455 			M_ALIGN(chk->data, 4);
7456 		}
7457 	}
7458 	SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
7459 	if (chk->data == NULL) {
7460 		/* HELP, TSNH since we assured it would not above? */
7461 #ifdef INVARIANTS
7462 		panic("prepend failes HELP?");
7463 #else
7464 		SCTP_PRINTF("prepend fails HELP?\n");
7465 		sctp_free_a_chunk(stcb, chk, so_locked);
7466 #endif
7467 		*bail = 1;
7468 		to_move = 0;
7469 		goto out_of;
7470 	}
7471 	sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
7472 	chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
7473 	chk->book_size_scale = 0;
7474 	chk->sent = SCTP_DATAGRAM_UNSENT;
7475 
7476 	chk->flags = 0;
7477 	chk->asoc = &stcb->asoc;
7478 	chk->pad_inplace = 0;
7479 	chk->no_fr_allowed = 0;
7480 	chk->rec.data.stream_seq = sp->strseq;
7481 	chk->rec.data.stream_number = sp->stream;
7482 	chk->rec.data.payloadtype = sp->ppid;
7483 	chk->rec.data.context = sp->context;
7484 	chk->rec.data.doing_fast_retransmit = 0;
7485 
7486 	chk->rec.data.timetodrop = sp->ts;
7487 	chk->flags = sp->act_flags;
7488 
7489 	if (sp->net) {
7490 		chk->whoTo = sp->net;
7491 		atomic_add_int(&chk->whoTo->ref_count, 1);
7492 	} else
7493 		chk->whoTo = NULL;
7494 
7495 	if (sp->holds_key_ref) {
7496 		chk->auth_keyid = sp->auth_keyid;
7497 		sctp_auth_key_acquire(stcb, chk->auth_keyid);
7498 		chk->holds_key_ref = 1;
7499 	}
7500 	chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
7501 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7502 		sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7503 		    (uintptr_t) stcb, sp->length,
7504 		    (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
7505 		    chk->rec.data.TSN_seq);
7506 	}
7507 	dchkh = mtod(chk->data, struct sctp_data_chunk *);
7508 	/*
7509 	 * Put the rest of the things in place now. Size was done earlier in
7510 	 * previous loop prior to padding.
7511 	 */
7512 
7513 #ifdef SCTP_ASOCLOG_OF_TSNS
7514 	SCTP_TCB_LOCK_ASSERT(stcb);
7515 	if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7516 		asoc->tsn_out_at = 0;
7517 		asoc->tsn_out_wrapped = 1;
7518 	}
7519 	asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7520 	asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7521 	asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7522 	asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7523 	asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7524 	asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7525 	asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7526 	asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7527 	asoc->tsn_out_at++;
7528 #endif
7529 
7530 	dchkh->ch.chunk_type = SCTP_DATA;
7531 	dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7532 	dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7533 	dchkh->dp.stream_id = htons(strq->stream_no);
7534 	dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7535 	dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7536 	dchkh->ch.chunk_length = htons(chk->send_size);
7537 	/* Now advance the chk->send_size by the actual pad needed. */
7538 	if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7539 		/* need a pad */
7540 		struct mbuf *lm;
7541 		int pads;
7542 
7543 		pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7544 		if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7545 			chk->pad_inplace = 1;
7546 		}
7547 		if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7548 			/* pad added an mbuf */
7549 			chk->last_mbuf = lm;
7550 		}
7551 		chk->send_size += pads;
7552 	}
7553 	/* We only re-set the policy if it is on */
7554 	if (sp->pr_sctp_on) {
7555 		sctp_set_prsctp_policy(sp);
7556 		asoc->pr_sctp_cnt++;
7557 		chk->pr_sctp_on = 1;
7558 	} else {
7559 		chk->pr_sctp_on = 0;
7560 	}
7561 	if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7562 		/* All done pull and kill the message */
7563 		atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7564 		if (sp->put_last_out == 0) {
7565 			SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7566 			SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7567 			    sp->sender_all_done,
7568 			    sp->length,
7569 			    sp->msg_is_complete,
7570 			    sp->put_last_out,
7571 			    send_lock_up);
7572 		}
7573 		if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7574 			SCTP_TCB_SEND_LOCK(stcb);
7575 			send_lock_up = 1;
7576 		}
7577 		TAILQ_REMOVE(&strq->outqueue, sp, next);
7578 		stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up);
7579 		if (sp->net) {
7580 			sctp_free_remote_addr(sp->net);
7581 			sp->net = NULL;
7582 		}
7583 		if (sp->data) {
7584 			sctp_m_freem(sp->data);
7585 			sp->data = NULL;
7586 		}
7587 		sctp_free_a_strmoq(stcb, sp, so_locked);
7588 
7589 		/* we can't be locked to it */
7590 		*locked = 0;
7591 		stcb->asoc.locked_on_sending = NULL;
7592 	} else {
7593 		/* more to go, we are locked */
7594 		*locked = 1;
7595 	}
7596 	asoc->chunks_on_out_queue++;
7597 	TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7598 	asoc->send_queue_cnt++;
7599 out_of:
7600 	if (send_lock_up) {
7601 		SCTP_TCB_SEND_UNLOCK(stcb);
7602 	}
7603 	return (to_move);
7604 }
7605 
7606 
7607 static void
7608 sctp_fill_outqueue(struct sctp_tcb *stcb,
7609     struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now, int so_locked
7610 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7611     SCTP_UNUSED
7612 #endif
7613 )
7614 {
7615 	struct sctp_association *asoc;
7616 	struct sctp_stream_out *strq;
7617 	int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7618 	int locked, giveup;
7619 
7620 	SCTP_TCB_LOCK_ASSERT(stcb);
7621 	asoc = &stcb->asoc;
7622 	switch (net->ro._l_addr.sa.sa_family) {
7623 #ifdef INET
7624 	case AF_INET:
7625 		goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7626 		break;
7627 #endif
7628 #ifdef INET6
7629 	case AF_INET6:
7630 		goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7631 		break;
7632 #endif
7633 	default:
7634 		/* TSNH */
7635 		goal_mtu = net->mtu;
7636 		break;
7637 	}
7638 	/* Need an allowance for the data chunk header too */
7639 	goal_mtu -= sizeof(struct sctp_data_chunk);
7640 
7641 	/* must make even word boundary */
7642 	goal_mtu &= 0xfffffffc;
7643 	if (asoc->locked_on_sending) {
7644 		/* We are stuck on one stream until the message completes. */
7645 		strq = asoc->locked_on_sending;
7646 		locked = 1;
7647 	} else {
7648 		strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7649 		locked = 0;
7650 	}
7651 	while ((goal_mtu > 0) && strq) {
7652 		giveup = 0;
7653 		bail = 0;
7654 		moved_how_much = sctp_move_to_outqueue(stcb, strq, goal_mtu, frag_point, &locked,
7655 		    &giveup, eeor_mode, &bail, so_locked);
7656 		if (moved_how_much)
7657 			stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, moved_how_much);
7658 
7659 		if (locked) {
7660 			asoc->locked_on_sending = strq;
7661 			if ((moved_how_much == 0) || (giveup) || bail)
7662 				/* no more to move for now */
7663 				break;
7664 		} else {
7665 			asoc->locked_on_sending = NULL;
7666 			if ((giveup) || bail) {
7667 				break;
7668 			}
7669 			strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7670 			if (strq == NULL) {
7671 				break;
7672 			}
7673 		}
7674 		total_moved += moved_how_much;
7675 		goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7676 		goal_mtu &= 0xfffffffc;
7677 	}
7678 	if (bail)
7679 		*quit_now = 1;
7680 
7681 	stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc);
7682 
7683 	if (total_moved == 0) {
7684 		if ((stcb->asoc.sctp_cmt_on_off == 0) &&
7685 		    (net == stcb->asoc.primary_destination)) {
7686 			/* ran dry for primary network net */
7687 			SCTP_STAT_INCR(sctps_primary_randry);
7688 		} else if (stcb->asoc.sctp_cmt_on_off > 0) {
7689 			/* ran dry with CMT on */
7690 			SCTP_STAT_INCR(sctps_cmt_randry);
7691 		}
7692 	}
7693 }
7694 
7695 void
7696 sctp_fix_ecn_echo(struct sctp_association *asoc)
7697 {
7698 	struct sctp_tmit_chunk *chk;
7699 
7700 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7701 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7702 			chk->sent = SCTP_DATAGRAM_UNSENT;
7703 		}
7704 	}
7705 }
7706 
7707 void
7708 sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net)
7709 {
7710 	struct sctp_association *asoc;
7711 	struct sctp_tmit_chunk *chk;
7712 	struct sctp_stream_queue_pending *sp;
7713 	unsigned int i;
7714 
7715 	if (net == NULL) {
7716 		return;
7717 	}
7718 	asoc = &stcb->asoc;
7719 	for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
7720 		TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
7721 			if (sp->net == net) {
7722 				sctp_free_remote_addr(sp->net);
7723 				sp->net = NULL;
7724 			}
7725 		}
7726 	}
7727 	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7728 		if (chk->whoTo == net) {
7729 			sctp_free_remote_addr(chk->whoTo);
7730 			chk->whoTo = NULL;
7731 		}
7732 	}
7733 }
7734 
7735 int
7736 sctp_med_chunk_output(struct sctp_inpcb *inp,
7737     struct sctp_tcb *stcb,
7738     struct sctp_association *asoc,
7739     int *num_out,
7740     int *reason_code,
7741     int control_only, int from_where,
7742     struct timeval *now, int *now_filled, int frag_point, int so_locked
7743 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7744     SCTP_UNUSED
7745 #endif
7746 )
7747 {
7748 	/*
7749 	 * Ok this is the generic chunk service queue. we must do the
7750 	 * following: - Service the stream queue that is next, moving any
7751 	 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7752 	 * LAST to the out queue in one pass) and assigning TSN's - Check to
7753 	 * see if the cwnd/rwnd allows any output, if so we go ahead and
7754 	 * fomulate and send the low level chunks. Making sure to combine
7755 	 * any control in the control chunk queue also.
7756 	 */
7757 	struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL;
7758 	struct mbuf *outchain, *endoutchain;
7759 	struct sctp_tmit_chunk *chk, *nchk;
7760 
7761 	/* temp arrays for unlinking */
7762 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7763 	int no_fragmentflg, error;
7764 	unsigned int max_rwnd_per_dest, max_send_per_dest;
7765 	int one_chunk, hbflag, skip_data_for_this_net;
7766 	int asconf, cookie, no_out_cnt;
7767 	int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7768 	unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7769 	int tsns_sent = 0;
7770 	uint32_t auth_offset = 0;
7771 	struct sctp_auth_chunk *auth = NULL;
7772 	uint16_t auth_keyid;
7773 	int override_ok = 1;
7774 	int skip_fill_up = 0;
7775 	int data_auth_reqd = 0;
7776 
7777 	/*
7778 	 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7779 	 * destination.
7780 	 */
7781 	int quit_now = 0;
7782 
7783 	*num_out = 0;
7784 	auth_keyid = stcb->asoc.authinfo.active_keyid;
7785 
7786 	if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7787 	    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7788 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7789 		eeor_mode = 1;
7790 	} else {
7791 		eeor_mode = 0;
7792 	}
7793 	ctl_cnt = no_out_cnt = asconf = cookie = 0;
7794 	/*
7795 	 * First lets prime the pump. For each destination, if there is room
7796 	 * in the flight size, attempt to pull an MTU's worth out of the
7797 	 * stream queues into the general send_queue
7798 	 */
7799 #ifdef SCTP_AUDITING_ENABLED
7800 	sctp_audit_log(0xC2, 2);
7801 #endif
7802 	SCTP_TCB_LOCK_ASSERT(stcb);
7803 	hbflag = 0;
7804 	if ((control_only) || (asoc->stream_reset_outstanding))
7805 		no_data_chunks = 1;
7806 	else
7807 		no_data_chunks = 0;
7808 
7809 	/* Nothing to possible to send? */
7810 	if ((TAILQ_EMPTY(&asoc->control_send_queue) ||
7811 	    (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) &&
7812 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7813 	    TAILQ_EMPTY(&asoc->send_queue) &&
7814 	    stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
7815 nothing_to_send:
7816 		*reason_code = 9;
7817 		return (0);
7818 	}
7819 	if (asoc->peers_rwnd == 0) {
7820 		/* No room in peers rwnd */
7821 		*reason_code = 1;
7822 		if (asoc->total_flight > 0) {
7823 			/* we are allowed one chunk in flight */
7824 			no_data_chunks = 1;
7825 		}
7826 	}
7827 	if (stcb->asoc.ecn_echo_cnt_onq) {
7828 		/* Record where a sack goes, if any */
7829 		if (no_data_chunks &&
7830 		    (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) {
7831 			/* Nothing but ECNe to send - we don't do that */
7832 			goto nothing_to_send;
7833 		}
7834 		TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7835 			if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7836 			    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7837 				sack_goes_to = chk->whoTo;
7838 				break;
7839 			}
7840 		}
7841 	}
7842 	max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7843 	if (stcb->sctp_socket)
7844 		max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets;
7845 	else
7846 		max_send_per_dest = 0;
7847 	if (no_data_chunks == 0) {
7848 		/* How many non-directed chunks are there? */
7849 		TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7850 			if (chk->whoTo == NULL) {
7851 				/*
7852 				 * We already have non-directed chunks on
7853 				 * the queue, no need to do a fill-up.
7854 				 */
7855 				skip_fill_up = 1;
7856 				break;
7857 			}
7858 		}
7859 
7860 	}
7861 	if ((no_data_chunks == 0) &&
7862 	    (skip_fill_up == 0) &&
7863 	    (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) {
7864 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7865 			/*
7866 			 * This for loop we are in takes in each net, if
7867 			 * its's got space in cwnd and has data sent to it
7868 			 * (when CMT is off) then it calls
7869 			 * sctp_fill_outqueue for the net. This gets data on
7870 			 * the send queue for that network.
7871 			 *
7872 			 * In sctp_fill_outqueue TSN's are assigned and data is
7873 			 * copied out of the stream buffers. Note mostly
7874 			 * copy by reference (we hope).
7875 			 */
7876 			net->window_probe = 0;
7877 			if ((net != stcb->asoc.alternate) &&
7878 			    ((net->dest_state & SCTP_ADDR_PF) ||
7879 			    (!(net->dest_state & SCTP_ADDR_REACHABLE)) ||
7880 			    (net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
7881 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7882 					sctp_log_cwnd(stcb, net, 1,
7883 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7884 				}
7885 				continue;
7886 			}
7887 			if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
7888 			    (net->flight_size == 0)) {
7889 				(*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
7890 			}
7891 			if (net->flight_size >= net->cwnd) {
7892 				/* skip this network, no room - can't fill */
7893 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7894 					sctp_log_cwnd(stcb, net, 3,
7895 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7896 				}
7897 				continue;
7898 			}
7899 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7900 				sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7901 			}
7902 			sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked);
7903 			if (quit_now) {
7904 				/* memory alloc failure */
7905 				no_data_chunks = 1;
7906 				break;
7907 			}
7908 		}
7909 	}
7910 	/* now service each destination and send out what we can for it */
7911 	/* Nothing to send? */
7912 	if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7913 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7914 	    TAILQ_EMPTY(&asoc->send_queue)) {
7915 		*reason_code = 8;
7916 		return (0);
7917 	}
7918 	if (asoc->sctp_cmt_on_off > 0) {
7919 		/* get the last start point */
7920 		start_at = asoc->last_net_cmt_send_started;
7921 		if (start_at == NULL) {
7922 			/* null so to beginning */
7923 			start_at = TAILQ_FIRST(&asoc->nets);
7924 		} else {
7925 			start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7926 			if (start_at == NULL) {
7927 				start_at = TAILQ_FIRST(&asoc->nets);
7928 			}
7929 		}
7930 		asoc->last_net_cmt_send_started = start_at;
7931 	} else {
7932 		start_at = TAILQ_FIRST(&asoc->nets);
7933 	}
7934 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7935 		if (chk->whoTo == NULL) {
7936 			if (asoc->alternate) {
7937 				chk->whoTo = asoc->alternate;
7938 			} else {
7939 				chk->whoTo = asoc->primary_destination;
7940 			}
7941 			atomic_add_int(&chk->whoTo->ref_count, 1);
7942 		}
7943 	}
7944 	old_start_at = NULL;
7945 again_one_more_time:
7946 	for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7947 		/* how much can we send? */
7948 		/* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7949 		if (old_start_at && (old_start_at == net)) {
7950 			/* through list ocmpletely. */
7951 			break;
7952 		}
7953 		tsns_sent = 0xa;
7954 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7955 		    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7956 		    (net->flight_size >= net->cwnd)) {
7957 			/*
7958 			 * Nothing on control or asconf and flight is full,
7959 			 * we can skip even in the CMT case.
7960 			 */
7961 			continue;
7962 		}
7963 		bundle_at = 0;
7964 		endoutchain = outchain = NULL;
7965 		no_fragmentflg = 1;
7966 		one_chunk = 0;
7967 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7968 			skip_data_for_this_net = 1;
7969 		} else {
7970 			skip_data_for_this_net = 0;
7971 		}
7972 		if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7973 			/*
7974 			 * if we have a route and an ifp check to see if we
7975 			 * have room to send to this guy
7976 			 */
7977 			struct ifnet *ifp;
7978 
7979 			ifp = net->ro.ro_rt->rt_ifp;
7980 			if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7981 				SCTP_STAT_INCR(sctps_ifnomemqueued);
7982 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7983 					sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7984 				}
7985 				continue;
7986 			}
7987 		}
7988 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7989 #ifdef INET
7990 		case AF_INET:
7991 			mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7992 			break;
7993 #endif
7994 #ifdef INET6
7995 		case AF_INET6:
7996 			mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7997 			break;
7998 #endif
7999 		default:
8000 			/* TSNH */
8001 			mtu = net->mtu;
8002 			break;
8003 		}
8004 		mx_mtu = mtu;
8005 		to_out = 0;
8006 		if (mtu > asoc->peers_rwnd) {
8007 			if (asoc->total_flight > 0) {
8008 				/* We have a packet in flight somewhere */
8009 				r_mtu = asoc->peers_rwnd;
8010 			} else {
8011 				/* We are always allowed to send one MTU out */
8012 				one_chunk = 1;
8013 				r_mtu = mtu;
8014 			}
8015 		} else {
8016 			r_mtu = mtu;
8017 		}
8018 		/************************/
8019 		/* ASCONF transmission */
8020 		/************************/
8021 		/* Now first lets go through the asconf queue */
8022 		TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
8023 			if (chk->rec.chunk_id.id != SCTP_ASCONF) {
8024 				continue;
8025 			}
8026 			if (chk->whoTo == NULL) {
8027 				if (asoc->alternate == NULL) {
8028 					if (asoc->primary_destination != net) {
8029 						break;
8030 					}
8031 				} else {
8032 					if (asoc->alternate != net) {
8033 						break;
8034 					}
8035 				}
8036 			} else {
8037 				if (chk->whoTo != net) {
8038 					break;
8039 				}
8040 			}
8041 			if (chk->data == NULL) {
8042 				break;
8043 			}
8044 			if (chk->sent != SCTP_DATAGRAM_UNSENT &&
8045 			    chk->sent != SCTP_DATAGRAM_RESEND) {
8046 				break;
8047 			}
8048 			/*
8049 			 * if no AUTH is yet included and this chunk
8050 			 * requires it, make sure to account for it.  We
8051 			 * don't apply the size until the AUTH chunk is
8052 			 * actually added below in case there is no room for
8053 			 * this chunk. NOTE: we overload the use of "omtu"
8054 			 * here
8055 			 */
8056 			if ((auth == NULL) &&
8057 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8058 			    stcb->asoc.peer_auth_chunks)) {
8059 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8060 			} else
8061 				omtu = 0;
8062 			/* Here we do NOT factor the r_mtu */
8063 			if ((chk->send_size < (int)(mtu - omtu)) ||
8064 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8065 				/*
8066 				 * We probably should glom the mbuf chain
8067 				 * from the chk->data for control but the
8068 				 * problem is it becomes yet one more level
8069 				 * of tracking to do if for some reason
8070 				 * output fails. Then I have got to
8071 				 * reconstruct the merged control chain.. el
8072 				 * yucko.. for now we take the easy way and
8073 				 * do the copy
8074 				 */
8075 				/*
8076 				 * Add an AUTH chunk, if chunk requires it
8077 				 * save the offset into the chain for AUTH
8078 				 */
8079 				if ((auth == NULL) &&
8080 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8081 				    stcb->asoc.peer_auth_chunks))) {
8082 					outchain = sctp_add_auth_chunk(outchain,
8083 					    &endoutchain,
8084 					    &auth,
8085 					    &auth_offset,
8086 					    stcb,
8087 					    chk->rec.chunk_id.id);
8088 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8089 				}
8090 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8091 				    (int)chk->rec.chunk_id.can_take_data,
8092 				    chk->send_size, chk->copy_by_ref);
8093 				if (outchain == NULL) {
8094 					*reason_code = 8;
8095 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8096 					return (ENOMEM);
8097 				}
8098 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8099 				/* update our MTU size */
8100 				if (mtu > (chk->send_size + omtu))
8101 					mtu -= (chk->send_size + omtu);
8102 				else
8103 					mtu = 0;
8104 				to_out += (chk->send_size + omtu);
8105 				/* Do clear IP_DF ? */
8106 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8107 					no_fragmentflg = 0;
8108 				}
8109 				if (chk->rec.chunk_id.can_take_data)
8110 					chk->data = NULL;
8111 				/*
8112 				 * set hb flag since we can use these for
8113 				 * RTO
8114 				 */
8115 				hbflag = 1;
8116 				asconf = 1;
8117 				/*
8118 				 * should sysctl this: don't bundle data
8119 				 * with ASCONF since it requires AUTH
8120 				 */
8121 				no_data_chunks = 1;
8122 				chk->sent = SCTP_DATAGRAM_SENT;
8123 				if (chk->whoTo == NULL) {
8124 					chk->whoTo = net;
8125 					atomic_add_int(&net->ref_count, 1);
8126 				}
8127 				chk->snd_count++;
8128 				if (mtu == 0) {
8129 					/*
8130 					 * Ok we are out of room but we can
8131 					 * output without effecting the
8132 					 * flight size since this little guy
8133 					 * is a control only packet.
8134 					 */
8135 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8136 					/*
8137 					 * do NOT clear the asconf flag as
8138 					 * it is used to do appropriate
8139 					 * source address selection.
8140 					 */
8141 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8142 					    (struct sockaddr *)&net->ro._l_addr,
8143 					    outchain, auth_offset, auth,
8144 					    stcb->asoc.authinfo.active_keyid,
8145 					    no_fragmentflg, 0, asconf,
8146 					    inp->sctp_lport, stcb->rport,
8147 					    htonl(stcb->asoc.peer_vtag),
8148 					    net->port, so_locked, NULL, NULL))) {
8149 						if (error == ENOBUFS) {
8150 							asoc->ifp_had_enobuf = 1;
8151 							SCTP_STAT_INCR(sctps_lowlevelerr);
8152 						}
8153 						if (from_where == 0) {
8154 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
8155 						}
8156 						if (*now_filled == 0) {
8157 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8158 							*now_filled = 1;
8159 							*now = net->last_sent_time;
8160 						} else {
8161 							net->last_sent_time = *now;
8162 						}
8163 						hbflag = 0;
8164 						/* error, could not output */
8165 						if (error == EHOSTUNREACH) {
8166 							/*
8167 							 * Destination went
8168 							 * unreachable
8169 							 * during this send
8170 							 */
8171 							sctp_move_chunks_from_net(stcb, net);
8172 						}
8173 						*reason_code = 7;
8174 						continue;
8175 					} else
8176 						asoc->ifp_had_enobuf = 0;
8177 					if (*now_filled == 0) {
8178 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8179 						*now_filled = 1;
8180 						*now = net->last_sent_time;
8181 					} else {
8182 						net->last_sent_time = *now;
8183 					}
8184 					hbflag = 0;
8185 					/*
8186 					 * increase the number we sent, if a
8187 					 * cookie is sent we don't tell them
8188 					 * any was sent out.
8189 					 */
8190 					outchain = endoutchain = NULL;
8191 					auth = NULL;
8192 					auth_offset = 0;
8193 					if (!no_out_cnt)
8194 						*num_out += ctl_cnt;
8195 					/* recalc a clean slate and setup */
8196 					switch (net->ro._l_addr.sa.sa_family) {
8197 #ifdef INET
8198 					case AF_INET:
8199 						mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8200 						break;
8201 #endif
8202 #ifdef INET6
8203 					case AF_INET6:
8204 						mtu = net->mtu - SCTP_MIN_OVERHEAD;
8205 						break;
8206 #endif
8207 					default:
8208 						/* TSNH */
8209 						mtu = net->mtu;
8210 						break;
8211 					}
8212 					to_out = 0;
8213 					no_fragmentflg = 1;
8214 				}
8215 			}
8216 		}
8217 		/************************/
8218 		/* Control transmission */
8219 		/************************/
8220 		/* Now first lets go through the control queue */
8221 		TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
8222 			if ((sack_goes_to) &&
8223 			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO) &&
8224 			    (chk->whoTo != sack_goes_to)) {
8225 				/*
8226 				 * if we have a sack in queue, and we are
8227 				 * looking at an ecn echo that is NOT queued
8228 				 * to where the sack is going..
8229 				 */
8230 				if (chk->whoTo == net) {
8231 					/*
8232 					 * Don't transmit it to where its
8233 					 * going (current net)
8234 					 */
8235 					continue;
8236 				} else if (sack_goes_to == net) {
8237 					/*
8238 					 * But do transmit it to this
8239 					 * address
8240 					 */
8241 					goto skip_net_check;
8242 				}
8243 			}
8244 			if (chk->whoTo == NULL) {
8245 				if (asoc->alternate == NULL) {
8246 					if (asoc->primary_destination != net) {
8247 						continue;
8248 					}
8249 				} else {
8250 					if (asoc->alternate != net) {
8251 						continue;
8252 					}
8253 				}
8254 			} else {
8255 				if (chk->whoTo != net) {
8256 					continue;
8257 				}
8258 			}
8259 	skip_net_check:
8260 			if (chk->data == NULL) {
8261 				continue;
8262 			}
8263 			if (chk->sent != SCTP_DATAGRAM_UNSENT) {
8264 				/*
8265 				 * It must be unsent. Cookies and ASCONF's
8266 				 * hang around but there timers will force
8267 				 * when marked for resend.
8268 				 */
8269 				continue;
8270 			}
8271 			/*
8272 			 * if no AUTH is yet included and this chunk
8273 			 * requires it, make sure to account for it.  We
8274 			 * don't apply the size until the AUTH chunk is
8275 			 * actually added below in case there is no room for
8276 			 * this chunk. NOTE: we overload the use of "omtu"
8277 			 * here
8278 			 */
8279 			if ((auth == NULL) &&
8280 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8281 			    stcb->asoc.peer_auth_chunks)) {
8282 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8283 			} else
8284 				omtu = 0;
8285 			/* Here we do NOT factor the r_mtu */
8286 			if ((chk->send_size <= (int)(mtu - omtu)) ||
8287 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8288 				/*
8289 				 * We probably should glom the mbuf chain
8290 				 * from the chk->data for control but the
8291 				 * problem is it becomes yet one more level
8292 				 * of tracking to do if for some reason
8293 				 * output fails. Then I have got to
8294 				 * reconstruct the merged control chain.. el
8295 				 * yucko.. for now we take the easy way and
8296 				 * do the copy
8297 				 */
8298 				/*
8299 				 * Add an AUTH chunk, if chunk requires it
8300 				 * save the offset into the chain for AUTH
8301 				 */
8302 				if ((auth == NULL) &&
8303 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8304 				    stcb->asoc.peer_auth_chunks))) {
8305 					outchain = sctp_add_auth_chunk(outchain,
8306 					    &endoutchain,
8307 					    &auth,
8308 					    &auth_offset,
8309 					    stcb,
8310 					    chk->rec.chunk_id.id);
8311 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8312 				}
8313 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8314 				    (int)chk->rec.chunk_id.can_take_data,
8315 				    chk->send_size, chk->copy_by_ref);
8316 				if (outchain == NULL) {
8317 					*reason_code = 8;
8318 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8319 					return (ENOMEM);
8320 				}
8321 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8322 				/* update our MTU size */
8323 				if (mtu > (chk->send_size + omtu))
8324 					mtu -= (chk->send_size + omtu);
8325 				else
8326 					mtu = 0;
8327 				to_out += (chk->send_size + omtu);
8328 				/* Do clear IP_DF ? */
8329 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8330 					no_fragmentflg = 0;
8331 				}
8332 				if (chk->rec.chunk_id.can_take_data)
8333 					chk->data = NULL;
8334 				/* Mark things to be removed, if needed */
8335 				if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8336 				    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
8337 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
8338 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
8339 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
8340 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
8341 				    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
8342 				    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
8343 				    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
8344 				    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
8345 				    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
8346 					if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
8347 						hbflag = 1;
8348 					}
8349 					/* remove these chunks at the end */
8350 					if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8351 					    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
8352 						/* turn off the timer */
8353 						if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
8354 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
8355 							    inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
8356 						}
8357 					}
8358 					ctl_cnt++;
8359 				} else {
8360 					/*
8361 					 * Other chunks, since they have
8362 					 * timers running (i.e. COOKIE) we
8363 					 * just "trust" that it gets sent or
8364 					 * retransmitted.
8365 					 */
8366 					ctl_cnt++;
8367 					if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8368 						cookie = 1;
8369 						no_out_cnt = 1;
8370 					} else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
8371 						/*
8372 						 * Increment ecne send count
8373 						 * here this means we may be
8374 						 * over-zealous in our
8375 						 * counting if the send
8376 						 * fails, but its the best
8377 						 * place to do it (we used
8378 						 * to do it in the queue of
8379 						 * the chunk, but that did
8380 						 * not tell how many times
8381 						 * it was sent.
8382 						 */
8383 						SCTP_STAT_INCR(sctps_sendecne);
8384 					}
8385 					chk->sent = SCTP_DATAGRAM_SENT;
8386 					if (chk->whoTo == NULL) {
8387 						chk->whoTo = net;
8388 						atomic_add_int(&net->ref_count, 1);
8389 					}
8390 					chk->snd_count++;
8391 				}
8392 				if (mtu == 0) {
8393 					/*
8394 					 * Ok we are out of room but we can
8395 					 * output without effecting the
8396 					 * flight size since this little guy
8397 					 * is a control only packet.
8398 					 */
8399 					if (asconf) {
8400 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8401 						/*
8402 						 * do NOT clear the asconf
8403 						 * flag as it is used to do
8404 						 * appropriate source
8405 						 * address selection.
8406 						 */
8407 					}
8408 					if (cookie) {
8409 						sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8410 						cookie = 0;
8411 					}
8412 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8413 					    (struct sockaddr *)&net->ro._l_addr,
8414 					    outchain,
8415 					    auth_offset, auth,
8416 					    stcb->asoc.authinfo.active_keyid,
8417 					    no_fragmentflg, 0, asconf,
8418 					    inp->sctp_lport, stcb->rport,
8419 					    htonl(stcb->asoc.peer_vtag),
8420 					    net->port, so_locked, NULL, NULL))) {
8421 						if (error == ENOBUFS) {
8422 							asoc->ifp_had_enobuf = 1;
8423 							SCTP_STAT_INCR(sctps_lowlevelerr);
8424 						}
8425 						if (from_where == 0) {
8426 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
8427 						}
8428 						/* error, could not output */
8429 						if (hbflag) {
8430 							if (*now_filled == 0) {
8431 								(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8432 								*now_filled = 1;
8433 								*now = net->last_sent_time;
8434 							} else {
8435 								net->last_sent_time = *now;
8436 							}
8437 							hbflag = 0;
8438 						}
8439 						if (error == EHOSTUNREACH) {
8440 							/*
8441 							 * Destination went
8442 							 * unreachable
8443 							 * during this send
8444 							 */
8445 							sctp_move_chunks_from_net(stcb, net);
8446 						}
8447 						*reason_code = 7;
8448 						continue;
8449 					} else
8450 						asoc->ifp_had_enobuf = 0;
8451 					/* Only HB or ASCONF advances time */
8452 					if (hbflag) {
8453 						if (*now_filled == 0) {
8454 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8455 							*now_filled = 1;
8456 							*now = net->last_sent_time;
8457 						} else {
8458 							net->last_sent_time = *now;
8459 						}
8460 						hbflag = 0;
8461 					}
8462 					/*
8463 					 * increase the number we sent, if a
8464 					 * cookie is sent we don't tell them
8465 					 * any was sent out.
8466 					 */
8467 					outchain = endoutchain = NULL;
8468 					auth = NULL;
8469 					auth_offset = 0;
8470 					if (!no_out_cnt)
8471 						*num_out += ctl_cnt;
8472 					/* recalc a clean slate and setup */
8473 					switch (net->ro._l_addr.sa.sa_family) {
8474 #ifdef INET
8475 					case AF_INET:
8476 						mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8477 						break;
8478 #endif
8479 #ifdef INET6
8480 					case AF_INET6:
8481 						mtu = net->mtu - SCTP_MIN_OVERHEAD;
8482 						break;
8483 #endif
8484 					default:
8485 						/* TSNH */
8486 						mtu = net->mtu;
8487 						break;
8488 					}
8489 					to_out = 0;
8490 					no_fragmentflg = 1;
8491 				}
8492 			}
8493 		}
8494 		/* JRI: if dest is in PF state, do not send data to it */
8495 		if ((asoc->sctp_cmt_on_off > 0) &&
8496 		    (net != stcb->asoc.alternate) &&
8497 		    (net->dest_state & SCTP_ADDR_PF)) {
8498 			goto no_data_fill;
8499 		}
8500 		if (net->flight_size >= net->cwnd) {
8501 			goto no_data_fill;
8502 		}
8503 		if ((asoc->sctp_cmt_on_off > 0) &&
8504 		    (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) &&
8505 		    (net->flight_size > max_rwnd_per_dest)) {
8506 			goto no_data_fill;
8507 		}
8508 		/*
8509 		 * We need a specific accounting for the usage of the send
8510 		 * buffer. We also need to check the number of messages per
8511 		 * net. For now, this is better than nothing and it disabled
8512 		 * by default...
8513 		 */
8514 		if ((asoc->sctp_cmt_on_off > 0) &&
8515 		    (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) &&
8516 		    (max_send_per_dest > 0) &&
8517 		    (net->flight_size > max_send_per_dest)) {
8518 			goto no_data_fill;
8519 		}
8520 		/*********************/
8521 		/* Data transmission */
8522 		/*********************/
8523 		/*
8524 		 * if AUTH for DATA is required and no AUTH has been added
8525 		 * yet, account for this in the mtu now... if no data can be
8526 		 * bundled, this adjustment won't matter anyways since the
8527 		 * packet will be going out...
8528 		 */
8529 		data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8530 		    stcb->asoc.peer_auth_chunks);
8531 		if (data_auth_reqd && (auth == NULL)) {
8532 			mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8533 		}
8534 		/* now lets add any data within the MTU constraints */
8535 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8536 		case AF_INET:
8537 			if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8538 				omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8539 			else
8540 				omtu = 0;
8541 			break;
8542 #ifdef INET6
8543 		case AF_INET6:
8544 			if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8545 				omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8546 			else
8547 				omtu = 0;
8548 			break;
8549 #endif
8550 		default:
8551 			/* TSNH */
8552 			omtu = 0;
8553 			break;
8554 		}
8555 		if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) &&
8556 		    (skip_data_for_this_net == 0)) ||
8557 		    (cookie)) {
8558 			TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
8559 				if (no_data_chunks) {
8560 					/* let only control go out */
8561 					*reason_code = 1;
8562 					break;
8563 				}
8564 				if (net->flight_size >= net->cwnd) {
8565 					/* skip this net, no room for data */
8566 					*reason_code = 2;
8567 					break;
8568 				}
8569 				if ((chk->whoTo != NULL) &&
8570 				    (chk->whoTo != net)) {
8571 					/* Don't send the chunk on this net */
8572 					continue;
8573 				}
8574 				if (asoc->sctp_cmt_on_off == 0) {
8575 					if ((asoc->alternate) &&
8576 					    (asoc->alternate != net) &&
8577 					    (chk->whoTo == NULL)) {
8578 						continue;
8579 					} else if ((net != asoc->primary_destination) &&
8580 						    (asoc->alternate == NULL) &&
8581 					    (chk->whoTo == NULL)) {
8582 						continue;
8583 					}
8584 				}
8585 				if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8586 					/*-
8587 					 * strange, we have a chunk that is
8588 					 * to big for its destination and
8589 					 * yet no fragment ok flag.
8590 					 * Something went wrong when the
8591 					 * PMTU changed...we did not mark
8592 					 * this chunk for some reason?? I
8593 					 * will fix it here by letting IP
8594 					 * fragment it for now and printing
8595 					 * a warning. This really should not
8596 					 * happen ...
8597 					 */
8598 					SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8599 					    chk->send_size, mtu);
8600 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8601 				}
8602 				if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8603 				    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
8604 					struct sctp_data_chunk *dchkh;
8605 
8606 					dchkh = mtod(chk->data, struct sctp_data_chunk *);
8607 					dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8608 				}
8609 				if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8610 				    ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8611 					/* ok we will add this one */
8612 
8613 					/*
8614 					 * Add an AUTH chunk, if chunk
8615 					 * requires it, save the offset into
8616 					 * the chain for AUTH
8617 					 */
8618 					if (data_auth_reqd) {
8619 						if (auth == NULL) {
8620 							outchain = sctp_add_auth_chunk(outchain,
8621 							    &endoutchain,
8622 							    &auth,
8623 							    &auth_offset,
8624 							    stcb,
8625 							    SCTP_DATA);
8626 							auth_keyid = chk->auth_keyid;
8627 							override_ok = 0;
8628 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8629 						} else if (override_ok) {
8630 							/*
8631 							 * use this data's
8632 							 * keyid
8633 							 */
8634 							auth_keyid = chk->auth_keyid;
8635 							override_ok = 0;
8636 						} else if (auth_keyid != chk->auth_keyid) {
8637 							/*
8638 							 * different keyid,
8639 							 * so done bundling
8640 							 */
8641 							break;
8642 						}
8643 					}
8644 					outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8645 					    chk->send_size, chk->copy_by_ref);
8646 					if (outchain == NULL) {
8647 						SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8648 						if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8649 							sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8650 						}
8651 						*reason_code = 3;
8652 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8653 						return (ENOMEM);
8654 					}
8655 					/* upate our MTU size */
8656 					/* Do clear IP_DF ? */
8657 					if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8658 						no_fragmentflg = 0;
8659 					}
8660 					/* unsigned subtraction of mtu */
8661 					if (mtu > chk->send_size)
8662 						mtu -= chk->send_size;
8663 					else
8664 						mtu = 0;
8665 					/* unsigned subtraction of r_mtu */
8666 					if (r_mtu > chk->send_size)
8667 						r_mtu -= chk->send_size;
8668 					else
8669 						r_mtu = 0;
8670 
8671 					to_out += chk->send_size;
8672 					if ((to_out > mx_mtu) && no_fragmentflg) {
8673 #ifdef INVARIANTS
8674 						panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8675 #else
8676 						SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8677 						    mx_mtu, to_out);
8678 #endif
8679 					}
8680 					chk->window_probe = 0;
8681 					data_list[bundle_at++] = chk;
8682 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8683 						break;
8684 					}
8685 					if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8686 						if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8687 							SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8688 						} else {
8689 							SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8690 						}
8691 						if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8692 						    ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8693 							/*
8694 							 * Count number of
8695 							 * user msg's that
8696 							 * were fragmented
8697 							 * we do this by
8698 							 * counting when we
8699 							 * see a LAST
8700 							 * fragment only.
8701 							 */
8702 							SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8703 					}
8704 					if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8705 						if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8706 							data_list[0]->window_probe = 1;
8707 							net->window_probe = 1;
8708 						}
8709 						break;
8710 					}
8711 				} else {
8712 					/*
8713 					 * Must be sent in order of the
8714 					 * TSN's (on a network)
8715 					 */
8716 					break;
8717 				}
8718 			}	/* for (chunk gather loop for this net) */
8719 		}		/* if asoc.state OPEN */
8720 no_data_fill:
8721 		/* Is there something to send for this destination? */
8722 		if (outchain) {
8723 			/* We may need to start a control timer or two */
8724 			if (asconf) {
8725 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8726 				    stcb, net);
8727 				/*
8728 				 * do NOT clear the asconf flag as it is
8729 				 * used to do appropriate source address
8730 				 * selection.
8731 				 */
8732 			}
8733 			if (cookie) {
8734 				sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8735 				cookie = 0;
8736 			}
8737 			/* must start a send timer if data is being sent */
8738 			if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8739 				/*
8740 				 * no timer running on this destination
8741 				 * restart it.
8742 				 */
8743 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8744 			}
8745 			/* Now send it, if there is anything to send :> */
8746 			if ((error = sctp_lowlevel_chunk_output(inp,
8747 			    stcb,
8748 			    net,
8749 			    (struct sockaddr *)&net->ro._l_addr,
8750 			    outchain,
8751 			    auth_offset,
8752 			    auth,
8753 			    auth_keyid,
8754 			    no_fragmentflg,
8755 			    bundle_at,
8756 			    asconf,
8757 			    inp->sctp_lport, stcb->rport,
8758 			    htonl(stcb->asoc.peer_vtag),
8759 			    net->port, so_locked, NULL, NULL))) {
8760 				/* error, we could not output */
8761 				if (error == ENOBUFS) {
8762 					SCTP_STAT_INCR(sctps_lowlevelerr);
8763 					asoc->ifp_had_enobuf = 1;
8764 				}
8765 				if (from_where == 0) {
8766 					SCTP_STAT_INCR(sctps_lowlevelerrusr);
8767 				}
8768 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8769 				if (hbflag) {
8770 					if (*now_filled == 0) {
8771 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8772 						*now_filled = 1;
8773 						*now = net->last_sent_time;
8774 					} else {
8775 						net->last_sent_time = *now;
8776 					}
8777 					hbflag = 0;
8778 				}
8779 				if (error == EHOSTUNREACH) {
8780 					/*
8781 					 * Destination went unreachable
8782 					 * during this send
8783 					 */
8784 					sctp_move_chunks_from_net(stcb, net);
8785 				}
8786 				*reason_code = 6;
8787 				/*-
8788 				 * I add this line to be paranoid. As far as
8789 				 * I can tell the continue, takes us back to
8790 				 * the top of the for, but just to make sure
8791 				 * I will reset these again here.
8792 				 */
8793 				ctl_cnt = bundle_at = 0;
8794 				continue;	/* This takes us back to the
8795 						 * for() for the nets. */
8796 			} else {
8797 				asoc->ifp_had_enobuf = 0;
8798 			}
8799 			endoutchain = NULL;
8800 			auth = NULL;
8801 			auth_offset = 0;
8802 			if (bundle_at || hbflag) {
8803 				/* For data/asconf and hb set time */
8804 				if (*now_filled == 0) {
8805 					(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8806 					*now_filled = 1;
8807 					*now = net->last_sent_time;
8808 				} else {
8809 					net->last_sent_time = *now;
8810 				}
8811 			}
8812 			if (!no_out_cnt) {
8813 				*num_out += (ctl_cnt + bundle_at);
8814 			}
8815 			if (bundle_at) {
8816 				/* setup for a RTO measurement */
8817 				tsns_sent = data_list[0]->rec.data.TSN_seq;
8818 				/* fill time if not already filled */
8819 				if (*now_filled == 0) {
8820 					(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8821 					*now_filled = 1;
8822 					*now = asoc->time_last_sent;
8823 				} else {
8824 					asoc->time_last_sent = *now;
8825 				}
8826 				if (net->rto_needed) {
8827 					data_list[0]->do_rtt = 1;
8828 					net->rto_needed = 0;
8829 				}
8830 				SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8831 				sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8832 			}
8833 			if (one_chunk) {
8834 				break;
8835 			}
8836 		}
8837 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8838 			sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8839 		}
8840 	}
8841 	if (old_start_at == NULL) {
8842 		old_start_at = start_at;
8843 		start_at = TAILQ_FIRST(&asoc->nets);
8844 		if (old_start_at)
8845 			goto again_one_more_time;
8846 	}
8847 	/*
8848 	 * At the end there should be no NON timed chunks hanging on this
8849 	 * queue.
8850 	 */
8851 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8852 		sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8853 	}
8854 	if ((*num_out == 0) && (*reason_code == 0)) {
8855 		*reason_code = 4;
8856 	} else {
8857 		*reason_code = 5;
8858 	}
8859 	sctp_clean_up_ctl(stcb, asoc, so_locked);
8860 	return (0);
8861 }
8862 
8863 void
8864 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8865 {
8866 	/*-
8867 	 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8868 	 * the control chunk queue.
8869 	 */
8870 	struct sctp_chunkhdr *hdr;
8871 	struct sctp_tmit_chunk *chk;
8872 	struct mbuf *mat;
8873 
8874 	SCTP_TCB_LOCK_ASSERT(stcb);
8875 	sctp_alloc_a_chunk(stcb, chk);
8876 	if (chk == NULL) {
8877 		/* no memory */
8878 		sctp_m_freem(op_err);
8879 		return;
8880 	}
8881 	chk->copy_by_ref = 0;
8882 	SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8883 	if (op_err == NULL) {
8884 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
8885 		return;
8886 	}
8887 	chk->send_size = 0;
8888 	mat = op_err;
8889 	while (mat != NULL) {
8890 		chk->send_size += SCTP_BUF_LEN(mat);
8891 		mat = SCTP_BUF_NEXT(mat);
8892 	}
8893 	chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8894 	chk->rec.chunk_id.can_take_data = 1;
8895 	chk->sent = SCTP_DATAGRAM_UNSENT;
8896 	chk->snd_count = 0;
8897 	chk->flags = 0;
8898 	chk->asoc = &stcb->asoc;
8899 	chk->data = op_err;
8900 	chk->whoTo = NULL;
8901 	hdr = mtod(op_err, struct sctp_chunkhdr *);
8902 	hdr->chunk_type = SCTP_OPERATION_ERROR;
8903 	hdr->chunk_flags = 0;
8904 	hdr->chunk_length = htons(chk->send_size);
8905 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8906 	    chk,
8907 	    sctp_next);
8908 	chk->asoc->ctrl_queue_cnt++;
8909 }
8910 
8911 int
8912 sctp_send_cookie_echo(struct mbuf *m,
8913     int offset,
8914     struct sctp_tcb *stcb,
8915     struct sctp_nets *net)
8916 {
8917 	/*-
8918 	 * pull out the cookie and put it at the front of the control chunk
8919 	 * queue.
8920 	 */
8921 	int at;
8922 	struct mbuf *cookie;
8923 	struct sctp_paramhdr parm, *phdr;
8924 	struct sctp_chunkhdr *hdr;
8925 	struct sctp_tmit_chunk *chk;
8926 	uint16_t ptype, plen;
8927 
8928 	/* First find the cookie in the param area */
8929 	cookie = NULL;
8930 	at = offset + sizeof(struct sctp_init_chunk);
8931 
8932 	SCTP_TCB_LOCK_ASSERT(stcb);
8933 	do {
8934 		phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8935 		if (phdr == NULL) {
8936 			return (-3);
8937 		}
8938 		ptype = ntohs(phdr->param_type);
8939 		plen = ntohs(phdr->param_length);
8940 		if (ptype == SCTP_STATE_COOKIE) {
8941 			int pad;
8942 
8943 			/* found the cookie */
8944 			if ((pad = (plen % 4))) {
8945 				plen += 4 - pad;
8946 			}
8947 			cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8948 			if (cookie == NULL) {
8949 				/* No memory */
8950 				return (-2);
8951 			}
8952 #ifdef SCTP_MBUF_LOGGING
8953 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8954 				struct mbuf *mat;
8955 
8956 				mat = cookie;
8957 				while (mat) {
8958 					if (SCTP_BUF_IS_EXTENDED(mat)) {
8959 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8960 					}
8961 					mat = SCTP_BUF_NEXT(mat);
8962 				}
8963 			}
8964 #endif
8965 			break;
8966 		}
8967 		at += SCTP_SIZE32(plen);
8968 	} while (phdr);
8969 	if (cookie == NULL) {
8970 		/* Did not find the cookie */
8971 		return (-3);
8972 	}
8973 	/* ok, we got the cookie lets change it into a cookie echo chunk */
8974 
8975 	/* first the change from param to cookie */
8976 	hdr = mtod(cookie, struct sctp_chunkhdr *);
8977 	hdr->chunk_type = SCTP_COOKIE_ECHO;
8978 	hdr->chunk_flags = 0;
8979 	/* get the chunk stuff now and place it in the FRONT of the queue */
8980 	sctp_alloc_a_chunk(stcb, chk);
8981 	if (chk == NULL) {
8982 		/* no memory */
8983 		sctp_m_freem(cookie);
8984 		return (-5);
8985 	}
8986 	chk->copy_by_ref = 0;
8987 	chk->send_size = plen;
8988 	chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8989 	chk->rec.chunk_id.can_take_data = 0;
8990 	chk->sent = SCTP_DATAGRAM_UNSENT;
8991 	chk->snd_count = 0;
8992 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8993 	chk->asoc = &stcb->asoc;
8994 	chk->data = cookie;
8995 	chk->whoTo = net;
8996 	atomic_add_int(&chk->whoTo->ref_count, 1);
8997 	TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8998 	chk->asoc->ctrl_queue_cnt++;
8999 	return (0);
9000 }
9001 
9002 void
9003 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
9004     struct mbuf *m,
9005     int offset,
9006     int chk_length,
9007     struct sctp_nets *net)
9008 {
9009 	/*
9010 	 * take a HB request and make it into a HB ack and send it.
9011 	 */
9012 	struct mbuf *outchain;
9013 	struct sctp_chunkhdr *chdr;
9014 	struct sctp_tmit_chunk *chk;
9015 
9016 
9017 	if (net == NULL)
9018 		/* must have a net pointer */
9019 		return;
9020 
9021 	outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
9022 	if (outchain == NULL) {
9023 		/* gak out of memory */
9024 		return;
9025 	}
9026 #ifdef SCTP_MBUF_LOGGING
9027 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9028 		struct mbuf *mat;
9029 
9030 		mat = outchain;
9031 		while (mat) {
9032 			if (SCTP_BUF_IS_EXTENDED(mat)) {
9033 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
9034 			}
9035 			mat = SCTP_BUF_NEXT(mat);
9036 		}
9037 	}
9038 #endif
9039 	chdr = mtod(outchain, struct sctp_chunkhdr *);
9040 	chdr->chunk_type = SCTP_HEARTBEAT_ACK;
9041 	chdr->chunk_flags = 0;
9042 	if (chk_length % 4) {
9043 		/* need pad */
9044 		uint32_t cpthis = 0;
9045 		int padlen;
9046 
9047 		padlen = 4 - (chk_length % 4);
9048 		m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
9049 	}
9050 	sctp_alloc_a_chunk(stcb, chk);
9051 	if (chk == NULL) {
9052 		/* no memory */
9053 		sctp_m_freem(outchain);
9054 		return;
9055 	}
9056 	chk->copy_by_ref = 0;
9057 	chk->send_size = chk_length;
9058 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
9059 	chk->rec.chunk_id.can_take_data = 1;
9060 	chk->sent = SCTP_DATAGRAM_UNSENT;
9061 	chk->snd_count = 0;
9062 	chk->flags = 0;
9063 	chk->asoc = &stcb->asoc;
9064 	chk->data = outchain;
9065 	chk->whoTo = net;
9066 	atomic_add_int(&chk->whoTo->ref_count, 1);
9067 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9068 	chk->asoc->ctrl_queue_cnt++;
9069 }
9070 
9071 void
9072 sctp_send_cookie_ack(struct sctp_tcb *stcb)
9073 {
9074 	/* formulate and queue a cookie-ack back to sender */
9075 	struct mbuf *cookie_ack;
9076 	struct sctp_chunkhdr *hdr;
9077 	struct sctp_tmit_chunk *chk;
9078 
9079 	cookie_ack = NULL;
9080 	SCTP_TCB_LOCK_ASSERT(stcb);
9081 
9082 	cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
9083 	if (cookie_ack == NULL) {
9084 		/* no mbuf's */
9085 		return;
9086 	}
9087 	SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
9088 	sctp_alloc_a_chunk(stcb, chk);
9089 	if (chk == NULL) {
9090 		/* no memory */
9091 		sctp_m_freem(cookie_ack);
9092 		return;
9093 	}
9094 	chk->copy_by_ref = 0;
9095 	chk->send_size = sizeof(struct sctp_chunkhdr);
9096 	chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
9097 	chk->rec.chunk_id.can_take_data = 1;
9098 	chk->sent = SCTP_DATAGRAM_UNSENT;
9099 	chk->snd_count = 0;
9100 	chk->flags = 0;
9101 	chk->asoc = &stcb->asoc;
9102 	chk->data = cookie_ack;
9103 	if (chk->asoc->last_control_chunk_from != NULL) {
9104 		chk->whoTo = chk->asoc->last_control_chunk_from;
9105 		atomic_add_int(&chk->whoTo->ref_count, 1);
9106 	} else {
9107 		chk->whoTo = NULL;
9108 	}
9109 	hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
9110 	hdr->chunk_type = SCTP_COOKIE_ACK;
9111 	hdr->chunk_flags = 0;
9112 	hdr->chunk_length = htons(chk->send_size);
9113 	SCTP_BUF_LEN(cookie_ack) = chk->send_size;
9114 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9115 	chk->asoc->ctrl_queue_cnt++;
9116 	return;
9117 }
9118 
9119 
9120 void
9121 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
9122 {
9123 	/* formulate and queue a SHUTDOWN-ACK back to the sender */
9124 	struct mbuf *m_shutdown_ack;
9125 	struct sctp_shutdown_ack_chunk *ack_cp;
9126 	struct sctp_tmit_chunk *chk;
9127 
9128 	m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
9129 	if (m_shutdown_ack == NULL) {
9130 		/* no mbuf's */
9131 		return;
9132 	}
9133 	SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
9134 	sctp_alloc_a_chunk(stcb, chk);
9135 	if (chk == NULL) {
9136 		/* no memory */
9137 		sctp_m_freem(m_shutdown_ack);
9138 		return;
9139 	}
9140 	chk->copy_by_ref = 0;
9141 	chk->send_size = sizeof(struct sctp_chunkhdr);
9142 	chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
9143 	chk->rec.chunk_id.can_take_data = 1;
9144 	chk->sent = SCTP_DATAGRAM_UNSENT;
9145 	chk->snd_count = 0;
9146 	chk->flags = 0;
9147 	chk->asoc = &stcb->asoc;
9148 	chk->data = m_shutdown_ack;
9149 	chk->whoTo = net;
9150 	if (chk->whoTo) {
9151 		atomic_add_int(&chk->whoTo->ref_count, 1);
9152 	}
9153 	ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
9154 	ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
9155 	ack_cp->ch.chunk_flags = 0;
9156 	ack_cp->ch.chunk_length = htons(chk->send_size);
9157 	SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
9158 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9159 	chk->asoc->ctrl_queue_cnt++;
9160 	return;
9161 }
9162 
9163 void
9164 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
9165 {
9166 	/* formulate and queue a SHUTDOWN to the sender */
9167 	struct mbuf *m_shutdown;
9168 	struct sctp_shutdown_chunk *shutdown_cp;
9169 	struct sctp_tmit_chunk *chk;
9170 
9171 	m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
9172 	if (m_shutdown == NULL) {
9173 		/* no mbuf's */
9174 		return;
9175 	}
9176 	SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
9177 	sctp_alloc_a_chunk(stcb, chk);
9178 	if (chk == NULL) {
9179 		/* no memory */
9180 		sctp_m_freem(m_shutdown);
9181 		return;
9182 	}
9183 	chk->copy_by_ref = 0;
9184 	chk->send_size = sizeof(struct sctp_shutdown_chunk);
9185 	chk->rec.chunk_id.id = SCTP_SHUTDOWN;
9186 	chk->rec.chunk_id.can_take_data = 1;
9187 	chk->sent = SCTP_DATAGRAM_UNSENT;
9188 	chk->snd_count = 0;
9189 	chk->flags = 0;
9190 	chk->asoc = &stcb->asoc;
9191 	chk->data = m_shutdown;
9192 	chk->whoTo = net;
9193 	if (chk->whoTo) {
9194 		atomic_add_int(&chk->whoTo->ref_count, 1);
9195 	}
9196 	shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
9197 	shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
9198 	shutdown_cp->ch.chunk_flags = 0;
9199 	shutdown_cp->ch.chunk_length = htons(chk->send_size);
9200 	shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
9201 	SCTP_BUF_LEN(m_shutdown) = chk->send_size;
9202 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9203 	chk->asoc->ctrl_queue_cnt++;
9204 	return;
9205 }
9206 
9207 void
9208 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
9209 {
9210 	/*
9211 	 * formulate and queue an ASCONF to the peer. ASCONF parameters
9212 	 * should be queued on the assoc queue.
9213 	 */
9214 	struct sctp_tmit_chunk *chk;
9215 	struct mbuf *m_asconf;
9216 	int len;
9217 
9218 	SCTP_TCB_LOCK_ASSERT(stcb);
9219 
9220 	if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
9221 	    (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
9222 		/* can't send a new one if there is one in flight already */
9223 		return;
9224 	}
9225 	/* compose an ASCONF chunk, maximum length is PMTU */
9226 	m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
9227 	if (m_asconf == NULL) {
9228 		return;
9229 	}
9230 	sctp_alloc_a_chunk(stcb, chk);
9231 	if (chk == NULL) {
9232 		/* no memory */
9233 		sctp_m_freem(m_asconf);
9234 		return;
9235 	}
9236 	chk->copy_by_ref = 0;
9237 	chk->data = m_asconf;
9238 	chk->send_size = len;
9239 	chk->rec.chunk_id.id = SCTP_ASCONF;
9240 	chk->rec.chunk_id.can_take_data = 0;
9241 	chk->sent = SCTP_DATAGRAM_UNSENT;
9242 	chk->snd_count = 0;
9243 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9244 	chk->asoc = &stcb->asoc;
9245 	chk->whoTo = net;
9246 	if (chk->whoTo) {
9247 		atomic_add_int(&chk->whoTo->ref_count, 1);
9248 	}
9249 	TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
9250 	chk->asoc->ctrl_queue_cnt++;
9251 	return;
9252 }
9253 
9254 void
9255 sctp_send_asconf_ack(struct sctp_tcb *stcb)
9256 {
9257 	/*
9258 	 * formulate and queue a asconf-ack back to sender. the asconf-ack
9259 	 * must be stored in the tcb.
9260 	 */
9261 	struct sctp_tmit_chunk *chk;
9262 	struct sctp_asconf_ack *ack, *latest_ack;
9263 	struct mbuf *m_ack;
9264 	struct sctp_nets *net = NULL;
9265 
9266 	SCTP_TCB_LOCK_ASSERT(stcb);
9267 	/* Get the latest ASCONF-ACK */
9268 	latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
9269 	if (latest_ack == NULL) {
9270 		return;
9271 	}
9272 	if (latest_ack->last_sent_to != NULL &&
9273 	    latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
9274 		/* we're doing a retransmission */
9275 		net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
9276 		if (net == NULL) {
9277 			/* no alternate */
9278 			if (stcb->asoc.last_control_chunk_from == NULL) {
9279 				if (stcb->asoc.alternate) {
9280 					net = stcb->asoc.alternate;
9281 				} else {
9282 					net = stcb->asoc.primary_destination;
9283 				}
9284 			} else {
9285 				net = stcb->asoc.last_control_chunk_from;
9286 			}
9287 		}
9288 	} else {
9289 		/* normal case */
9290 		if (stcb->asoc.last_control_chunk_from == NULL) {
9291 			if (stcb->asoc.alternate) {
9292 				net = stcb->asoc.alternate;
9293 			} else {
9294 				net = stcb->asoc.primary_destination;
9295 			}
9296 		} else {
9297 			net = stcb->asoc.last_control_chunk_from;
9298 		}
9299 	}
9300 	latest_ack->last_sent_to = net;
9301 
9302 	TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
9303 		if (ack->data == NULL) {
9304 			continue;
9305 		}
9306 		/* copy the asconf_ack */
9307 		m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
9308 		if (m_ack == NULL) {
9309 			/* couldn't copy it */
9310 			return;
9311 		}
9312 #ifdef SCTP_MBUF_LOGGING
9313 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9314 			struct mbuf *mat;
9315 
9316 			mat = m_ack;
9317 			while (mat) {
9318 				if (SCTP_BUF_IS_EXTENDED(mat)) {
9319 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
9320 				}
9321 				mat = SCTP_BUF_NEXT(mat);
9322 			}
9323 		}
9324 #endif
9325 
9326 		sctp_alloc_a_chunk(stcb, chk);
9327 		if (chk == NULL) {
9328 			/* no memory */
9329 			if (m_ack)
9330 				sctp_m_freem(m_ack);
9331 			return;
9332 		}
9333 		chk->copy_by_ref = 0;
9334 
9335 		chk->whoTo = net;
9336 		if (chk->whoTo) {
9337 			atomic_add_int(&chk->whoTo->ref_count, 1);
9338 		}
9339 		chk->data = m_ack;
9340 		chk->send_size = 0;
9341 		/* Get size */
9342 		chk->send_size = ack->len;
9343 		chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
9344 		chk->rec.chunk_id.can_take_data = 1;
9345 		chk->sent = SCTP_DATAGRAM_UNSENT;
9346 		chk->snd_count = 0;
9347 		chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;	/* XXX */
9348 		chk->asoc = &stcb->asoc;
9349 
9350 		TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9351 		chk->asoc->ctrl_queue_cnt++;
9352 	}
9353 	return;
9354 }
9355 
9356 
9357 static int
9358 sctp_chunk_retransmission(struct sctp_inpcb *inp,
9359     struct sctp_tcb *stcb,
9360     struct sctp_association *asoc,
9361     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
9362 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9363     SCTP_UNUSED
9364 #endif
9365 )
9366 {
9367 	/*-
9368 	 * send out one MTU of retransmission. If fast_retransmit is
9369 	 * happening we ignore the cwnd. Otherwise we obey the cwnd and
9370 	 * rwnd. For a Cookie or Asconf in the control chunk queue we
9371 	 * retransmit them by themselves.
9372 	 *
9373 	 * For data chunks we will pick out the lowest TSN's in the sent_queue
9374 	 * marked for resend and bundle them all together (up to a MTU of
9375 	 * destination). The address to send to should have been
9376 	 * selected/changed where the retransmission was marked (i.e. in FR
9377 	 * or t3-timeout routines).
9378 	 */
9379 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
9380 	struct sctp_tmit_chunk *chk, *fwd;
9381 	struct mbuf *m, *endofchain;
9382 	struct sctp_nets *net = NULL;
9383 	uint32_t tsns_sent = 0;
9384 	int no_fragmentflg, bundle_at, cnt_thru;
9385 	unsigned int mtu;
9386 	int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
9387 	struct sctp_auth_chunk *auth = NULL;
9388 	uint32_t auth_offset = 0;
9389 	uint16_t auth_keyid;
9390 	int override_ok = 1;
9391 	int data_auth_reqd = 0;
9392 	uint32_t dmtu = 0;
9393 
9394 	SCTP_TCB_LOCK_ASSERT(stcb);
9395 	tmr_started = ctl_cnt = bundle_at = error = 0;
9396 	no_fragmentflg = 1;
9397 	fwd_tsn = 0;
9398 	*cnt_out = 0;
9399 	fwd = NULL;
9400 	endofchain = m = NULL;
9401 	auth_keyid = stcb->asoc.authinfo.active_keyid;
9402 #ifdef SCTP_AUDITING_ENABLED
9403 	sctp_audit_log(0xC3, 1);
9404 #endif
9405 	if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
9406 	    (TAILQ_EMPTY(&asoc->control_send_queue))) {
9407 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
9408 		    asoc->sent_queue_retran_cnt);
9409 		asoc->sent_queue_cnt = 0;
9410 		asoc->sent_queue_cnt_removeable = 0;
9411 		/* send back 0/0 so we enter normal transmission */
9412 		*cnt_out = 0;
9413 		return (0);
9414 	}
9415 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9416 		if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
9417 		    (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
9418 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
9419 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
9420 				continue;
9421 			}
9422 			if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
9423 				if (chk != asoc->str_reset) {
9424 					/*
9425 					 * not eligible for retran if its
9426 					 * not ours
9427 					 */
9428 					continue;
9429 				}
9430 			}
9431 			ctl_cnt++;
9432 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9433 				fwd_tsn = 1;
9434 			}
9435 			/*
9436 			 * Add an AUTH chunk, if chunk requires it save the
9437 			 * offset into the chain for AUTH
9438 			 */
9439 			if ((auth == NULL) &&
9440 			    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9441 			    stcb->asoc.peer_auth_chunks))) {
9442 				m = sctp_add_auth_chunk(m, &endofchain,
9443 				    &auth, &auth_offset,
9444 				    stcb,
9445 				    chk->rec.chunk_id.id);
9446 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9447 			}
9448 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9449 			break;
9450 		}
9451 	}
9452 	one_chunk = 0;
9453 	cnt_thru = 0;
9454 	/* do we have control chunks to retransmit? */
9455 	if (m != NULL) {
9456 		/* Start a timer no matter if we suceed or fail */
9457 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9458 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
9459 		} else if (chk->rec.chunk_id.id == SCTP_ASCONF)
9460 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
9461 		chk->snd_count++;	/* update our count */
9462 		if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
9463 		    (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
9464 		    auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9465 		    no_fragmentflg, 0, 0,
9466 		    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9467 		    chk->whoTo->port, so_locked, NULL, NULL))) {
9468 			SCTP_STAT_INCR(sctps_lowlevelerr);
9469 			return (error);
9470 		}
9471 		endofchain = NULL;
9472 		auth = NULL;
9473 		auth_offset = 0;
9474 		/*
9475 		 * We don't want to mark the net->sent time here since this
9476 		 * we use this for HB and retrans cannot measure RTT
9477 		 */
9478 		/* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
9479 		*cnt_out += 1;
9480 		chk->sent = SCTP_DATAGRAM_SENT;
9481 		sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
9482 		if (fwd_tsn == 0) {
9483 			return (0);
9484 		} else {
9485 			/* Clean up the fwd-tsn list */
9486 			sctp_clean_up_ctl(stcb, asoc, so_locked);
9487 			return (0);
9488 		}
9489 	}
9490 	/*
9491 	 * Ok, it is just data retransmission we need to do or that and a
9492 	 * fwd-tsn with it all.
9493 	 */
9494 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
9495 		return (SCTP_RETRAN_DONE);
9496 	}
9497 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
9498 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
9499 		/* not yet open, resend the cookie and that is it */
9500 		return (1);
9501 	}
9502 #ifdef SCTP_AUDITING_ENABLED
9503 	sctp_auditing(20, inp, stcb, NULL);
9504 #endif
9505 	data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9506 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9507 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
9508 			/* No, not sent to this net or not ready for rtx */
9509 			continue;
9510 		}
9511 		if (chk->data == NULL) {
9512 			printf("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
9513 			    chk->rec.data.TSN_seq, chk->snd_count, chk->sent);
9514 			continue;
9515 		}
9516 		if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9517 		    (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9518 			/* Gak, we have exceeded max unlucky retran, abort! */
9519 			SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
9520 			    chk->snd_count,
9521 			    SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
9522 			atomic_add_int(&stcb->asoc.refcnt, 1);
9523 			sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
9524 			SCTP_TCB_LOCK(stcb);
9525 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
9526 			return (SCTP_RETRAN_EXIT);
9527 		}
9528 		/* pick up the net */
9529 		net = chk->whoTo;
9530 		switch (net->ro._l_addr.sa.sa_family) {
9531 #ifdef INET
9532 		case AF_INET:
9533 			mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9534 			break;
9535 #endif
9536 #ifdef INET6
9537 		case AF_INET6:
9538 			mtu = net->mtu - SCTP_MIN_OVERHEAD;
9539 			break;
9540 #endif
9541 		default:
9542 			/* TSNH */
9543 			mtu = net->mtu;
9544 			break;
9545 		}
9546 
9547 		if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9548 			/* No room in peers rwnd */
9549 			uint32_t tsn;
9550 
9551 			tsn = asoc->last_acked_seq + 1;
9552 			if (tsn == chk->rec.data.TSN_seq) {
9553 				/*
9554 				 * we make a special exception for this
9555 				 * case. The peer has no rwnd but is missing
9556 				 * the lowest chunk.. which is probably what
9557 				 * is holding up the rwnd.
9558 				 */
9559 				goto one_chunk_around;
9560 			}
9561 			return (1);
9562 		}
9563 one_chunk_around:
9564 		if (asoc->peers_rwnd < mtu) {
9565 			one_chunk = 1;
9566 			if ((asoc->peers_rwnd == 0) &&
9567 			    (asoc->total_flight == 0)) {
9568 				chk->window_probe = 1;
9569 				chk->whoTo->window_probe = 1;
9570 			}
9571 		}
9572 #ifdef SCTP_AUDITING_ENABLED
9573 		sctp_audit_log(0xC3, 2);
9574 #endif
9575 		bundle_at = 0;
9576 		m = NULL;
9577 		net->fast_retran_ip = 0;
9578 		if (chk->rec.data.doing_fast_retransmit == 0) {
9579 			/*
9580 			 * if no FR in progress skip destination that have
9581 			 * flight_size > cwnd.
9582 			 */
9583 			if (net->flight_size >= net->cwnd) {
9584 				continue;
9585 			}
9586 		} else {
9587 			/*
9588 			 * Mark the destination net to have FR recovery
9589 			 * limits put on it.
9590 			 */
9591 			*fr_done = 1;
9592 			net->fast_retran_ip = 1;
9593 		}
9594 
9595 		/*
9596 		 * if no AUTH is yet included and this chunk requires it,
9597 		 * make sure to account for it.  We don't apply the size
9598 		 * until the AUTH chunk is actually added below in case
9599 		 * there is no room for this chunk.
9600 		 */
9601 		if (data_auth_reqd && (auth == NULL)) {
9602 			dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9603 		} else
9604 			dmtu = 0;
9605 
9606 		if ((chk->send_size <= (mtu - dmtu)) ||
9607 		    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9608 			/* ok we will add this one */
9609 			if (data_auth_reqd) {
9610 				if (auth == NULL) {
9611 					m = sctp_add_auth_chunk(m,
9612 					    &endofchain,
9613 					    &auth,
9614 					    &auth_offset,
9615 					    stcb,
9616 					    SCTP_DATA);
9617 					auth_keyid = chk->auth_keyid;
9618 					override_ok = 0;
9619 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9620 				} else if (override_ok) {
9621 					auth_keyid = chk->auth_keyid;
9622 					override_ok = 0;
9623 				} else if (chk->auth_keyid != auth_keyid) {
9624 					/* different keyid, so done bundling */
9625 					break;
9626 				}
9627 			}
9628 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9629 			if (m == NULL) {
9630 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9631 				return (ENOMEM);
9632 			}
9633 			/* Do clear IP_DF ? */
9634 			if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9635 				no_fragmentflg = 0;
9636 			}
9637 			/* upate our MTU size */
9638 			if (mtu > (chk->send_size + dmtu))
9639 				mtu -= (chk->send_size + dmtu);
9640 			else
9641 				mtu = 0;
9642 			data_list[bundle_at++] = chk;
9643 			if (one_chunk && (asoc->total_flight <= 0)) {
9644 				SCTP_STAT_INCR(sctps_windowprobed);
9645 			}
9646 		}
9647 		if (one_chunk == 0) {
9648 			/*
9649 			 * now are there anymore forward from chk to pick
9650 			 * up?
9651 			 */
9652 			for (fwd = TAILQ_NEXT(chk, sctp_next); fwd != NULL; fwd = TAILQ_NEXT(fwd, sctp_next)) {
9653 				if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9654 					/* Nope, not for retran */
9655 					continue;
9656 				}
9657 				if (fwd->whoTo != net) {
9658 					/* Nope, not the net in question */
9659 					continue;
9660 				}
9661 				if (data_auth_reqd && (auth == NULL)) {
9662 					dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9663 				} else
9664 					dmtu = 0;
9665 				if (fwd->send_size <= (mtu - dmtu)) {
9666 					if (data_auth_reqd) {
9667 						if (auth == NULL) {
9668 							m = sctp_add_auth_chunk(m,
9669 							    &endofchain,
9670 							    &auth,
9671 							    &auth_offset,
9672 							    stcb,
9673 							    SCTP_DATA);
9674 							auth_keyid = fwd->auth_keyid;
9675 							override_ok = 0;
9676 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9677 						} else if (override_ok) {
9678 							auth_keyid = fwd->auth_keyid;
9679 							override_ok = 0;
9680 						} else if (fwd->auth_keyid != auth_keyid) {
9681 							/*
9682 							 * different keyid,
9683 							 * so done bundling
9684 							 */
9685 							break;
9686 						}
9687 					}
9688 					m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9689 					if (m == NULL) {
9690 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9691 						return (ENOMEM);
9692 					}
9693 					/* Do clear IP_DF ? */
9694 					if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9695 						no_fragmentflg = 0;
9696 					}
9697 					/* upate our MTU size */
9698 					if (mtu > (fwd->send_size + dmtu))
9699 						mtu -= (fwd->send_size + dmtu);
9700 					else
9701 						mtu = 0;
9702 					data_list[bundle_at++] = fwd;
9703 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9704 						break;
9705 					}
9706 				} else {
9707 					/* can't fit so we are done */
9708 					break;
9709 				}
9710 			}
9711 		}
9712 		/* Is there something to send for this destination? */
9713 		if (m) {
9714 			/*
9715 			 * No matter if we fail/or suceed we should start a
9716 			 * timer. A failure is like a lost IP packet :-)
9717 			 */
9718 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9719 				/*
9720 				 * no timer running on this destination
9721 				 * restart it.
9722 				 */
9723 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9724 				tmr_started = 1;
9725 			}
9726 			/* Now lets send it, if there is anything to send :> */
9727 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9728 			    (struct sockaddr *)&net->ro._l_addr, m,
9729 			    auth_offset, auth, auth_keyid,
9730 			    no_fragmentflg, 0, 0,
9731 			    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9732 			    net->port, so_locked, NULL, NULL))) {
9733 				/* error, we could not output */
9734 				SCTP_STAT_INCR(sctps_lowlevelerr);
9735 				return (error);
9736 			}
9737 			endofchain = NULL;
9738 			auth = NULL;
9739 			auth_offset = 0;
9740 			/* For HB's */
9741 			/*
9742 			 * We don't want to mark the net->sent time here
9743 			 * since this we use this for HB and retrans cannot
9744 			 * measure RTT
9745 			 */
9746 			/* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9747 
9748 			/* For auto-close */
9749 			cnt_thru++;
9750 			if (*now_filled == 0) {
9751 				(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9752 				*now = asoc->time_last_sent;
9753 				*now_filled = 1;
9754 			} else {
9755 				asoc->time_last_sent = *now;
9756 			}
9757 			*cnt_out += bundle_at;
9758 #ifdef SCTP_AUDITING_ENABLED
9759 			sctp_audit_log(0xC4, bundle_at);
9760 #endif
9761 			if (bundle_at) {
9762 				tsns_sent = data_list[0]->rec.data.TSN_seq;
9763 			}
9764 			for (i = 0; i < bundle_at; i++) {
9765 				SCTP_STAT_INCR(sctps_sendretransdata);
9766 				data_list[i]->sent = SCTP_DATAGRAM_SENT;
9767 				/*
9768 				 * When we have a revoked data, and we
9769 				 * retransmit it, then we clear the revoked
9770 				 * flag since this flag dictates if we
9771 				 * subtracted from the fs
9772 				 */
9773 				if (data_list[i]->rec.data.chunk_was_revoked) {
9774 					/* Deflate the cwnd */
9775 					data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9776 					data_list[i]->rec.data.chunk_was_revoked = 0;
9777 				}
9778 				data_list[i]->snd_count++;
9779 				sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9780 				/* record the time */
9781 				data_list[i]->sent_rcv_time = asoc->time_last_sent;
9782 				if (data_list[i]->book_size_scale) {
9783 					/*
9784 					 * need to double the book size on
9785 					 * this one
9786 					 */
9787 					data_list[i]->book_size_scale = 0;
9788 					/*
9789 					 * Since we double the booksize, we
9790 					 * must also double the output queue
9791 					 * size, since this get shrunk when
9792 					 * we free by this amount.
9793 					 */
9794 					atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9795 					data_list[i]->book_size *= 2;
9796 
9797 
9798 				} else {
9799 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9800 						sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9801 						    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9802 					}
9803 					asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9804 					    (uint32_t) (data_list[i]->send_size +
9805 					    SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9806 				}
9807 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9808 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9809 					    data_list[i]->whoTo->flight_size,
9810 					    data_list[i]->book_size,
9811 					    (uintptr_t) data_list[i]->whoTo,
9812 					    data_list[i]->rec.data.TSN_seq);
9813 				}
9814 				sctp_flight_size_increase(data_list[i]);
9815 				sctp_total_flight_increase(stcb, data_list[i]);
9816 				if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9817 					/* SWS sender side engages */
9818 					asoc->peers_rwnd = 0;
9819 				}
9820 				if ((i == 0) &&
9821 				    (data_list[i]->rec.data.doing_fast_retransmit)) {
9822 					SCTP_STAT_INCR(sctps_sendfastretrans);
9823 					if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9824 					    (tmr_started == 0)) {
9825 						/*-
9826 						 * ok we just fast-retrans'd
9827 						 * the lowest TSN, i.e the
9828 						 * first on the list. In
9829 						 * this case we want to give
9830 						 * some more time to get a
9831 						 * SACK back without a
9832 						 * t3-expiring.
9833 						 */
9834 						sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9835 						    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9836 						sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9837 					}
9838 				}
9839 			}
9840 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9841 				sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9842 			}
9843 #ifdef SCTP_AUDITING_ENABLED
9844 			sctp_auditing(21, inp, stcb, NULL);
9845 #endif
9846 		} else {
9847 			/* None will fit */
9848 			return (1);
9849 		}
9850 		if (asoc->sent_queue_retran_cnt <= 0) {
9851 			/* all done we have no more to retran */
9852 			asoc->sent_queue_retran_cnt = 0;
9853 			break;
9854 		}
9855 		if (one_chunk) {
9856 			/* No more room in rwnd */
9857 			return (1);
9858 		}
9859 		/* stop the for loop here. we sent out a packet */
9860 		break;
9861 	}
9862 	return (0);
9863 }
9864 
9865 static void
9866 sctp_timer_validation(struct sctp_inpcb *inp,
9867     struct sctp_tcb *stcb,
9868     struct sctp_association *asoc)
9869 {
9870 	struct sctp_nets *net;
9871 
9872 	/* Validate that a timer is running somewhere */
9873 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9874 		if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9875 			/* Here is a timer */
9876 			return;
9877 		}
9878 	}
9879 	SCTP_TCB_LOCK_ASSERT(stcb);
9880 	/* Gak, we did not have a timer somewhere */
9881 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9882 	if (asoc->alternate) {
9883 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->alternate);
9884 	} else {
9885 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9886 	}
9887 	return;
9888 }
9889 
9890 void
9891 sctp_chunk_output(struct sctp_inpcb *inp,
9892     struct sctp_tcb *stcb,
9893     int from_where,
9894     int so_locked
9895 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9896     SCTP_UNUSED
9897 #endif
9898 )
9899 {
9900 	/*-
9901 	 * Ok this is the generic chunk service queue. we must do the
9902 	 * following:
9903 	 * - See if there are retransmits pending, if so we must
9904 	 *   do these first.
9905 	 * - Service the stream queue that is next, moving any
9906 	 *   message (note I must get a complete message i.e.
9907 	 *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9908 	 *   TSN's
9909 	 * - Check to see if the cwnd/rwnd allows any output, if so we
9910 	 *   go ahead and fomulate and send the low level chunks. Making sure
9911 	 *   to combine any control in the control chunk queue also.
9912 	 */
9913 	struct sctp_association *asoc;
9914 	struct sctp_nets *net;
9915 	int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0;
9916 	unsigned int burst_cnt = 0;
9917 	struct timeval now;
9918 	int now_filled = 0;
9919 	int nagle_on;
9920 	int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9921 	int un_sent = 0;
9922 	int fr_done;
9923 	unsigned int tot_frs = 0;
9924 
9925 	asoc = &stcb->asoc;
9926 	/* The Nagle algorithm is only applied when handling a send call. */
9927 	if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9928 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9929 			nagle_on = 0;
9930 		} else {
9931 			nagle_on = 1;
9932 		}
9933 	} else {
9934 		nagle_on = 0;
9935 	}
9936 	SCTP_TCB_LOCK_ASSERT(stcb);
9937 
9938 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9939 
9940 	if ((un_sent <= 0) &&
9941 	    (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9942 	    (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
9943 	    (asoc->sent_queue_retran_cnt == 0)) {
9944 		/* Nothing to do unless there is something to be sent left */
9945 		return;
9946 	}
9947 	/*
9948 	 * Do we have something to send, data or control AND a sack timer
9949 	 * running, if so piggy-back the sack.
9950 	 */
9951 	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9952 		sctp_send_sack(stcb, so_locked);
9953 		(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9954 	}
9955 	while (asoc->sent_queue_retran_cnt) {
9956 		/*-
9957 		 * Ok, it is retransmission time only, we send out only ONE
9958 		 * packet with a single call off to the retran code.
9959 		 */
9960 		if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9961 			/*-
9962 			 * Special hook for handling cookiess discarded
9963 			 * by peer that carried data. Send cookie-ack only
9964 			 * and then the next call with get the retran's.
9965 			 */
9966 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9967 			    from_where,
9968 			    &now, &now_filled, frag_point, so_locked);
9969 			return;
9970 		} else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9971 			/* if its not from a HB then do it */
9972 			fr_done = 0;
9973 			ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9974 			if (fr_done) {
9975 				tot_frs++;
9976 			}
9977 		} else {
9978 			/*
9979 			 * its from any other place, we don't allow retran
9980 			 * output (only control)
9981 			 */
9982 			ret = 1;
9983 		}
9984 		if (ret > 0) {
9985 			/* Can't send anymore */
9986 			/*-
9987 			 * now lets push out control by calling med-level
9988 			 * output once. this assures that we WILL send HB's
9989 			 * if queued too.
9990 			 */
9991 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9992 			    from_where,
9993 			    &now, &now_filled, frag_point, so_locked);
9994 #ifdef SCTP_AUDITING_ENABLED
9995 			sctp_auditing(8, inp, stcb, NULL);
9996 #endif
9997 			sctp_timer_validation(inp, stcb, asoc);
9998 			return;
9999 		}
10000 		if (ret < 0) {
10001 			/*-
10002 			 * The count was off.. retran is not happening so do
10003 			 * the normal retransmission.
10004 			 */
10005 #ifdef SCTP_AUDITING_ENABLED
10006 			sctp_auditing(9, inp, stcb, NULL);
10007 #endif
10008 			if (ret == SCTP_RETRAN_EXIT) {
10009 				return;
10010 			}
10011 			break;
10012 		}
10013 		if (from_where == SCTP_OUTPUT_FROM_T3) {
10014 			/* Only one transmission allowed out of a timeout */
10015 #ifdef SCTP_AUDITING_ENABLED
10016 			sctp_auditing(10, inp, stcb, NULL);
10017 #endif
10018 			/* Push out any control */
10019 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
10020 			    &now, &now_filled, frag_point, so_locked);
10021 			return;
10022 		}
10023 		if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) {
10024 			/* Hit FR burst limit */
10025 			return;
10026 		}
10027 		if ((num_out == 0) && (ret == 0)) {
10028 			/* No more retrans to send */
10029 			break;
10030 		}
10031 	}
10032 #ifdef SCTP_AUDITING_ENABLED
10033 	sctp_auditing(12, inp, stcb, NULL);
10034 #endif
10035 	/* Check for bad destinations, if they exist move chunks around. */
10036 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
10037 		if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
10038 			/*-
10039 			 * if possible move things off of this address we
10040 			 * still may send below due to the dormant state but
10041 			 * we try to find an alternate address to send to
10042 			 * and if we have one we move all queued data on the
10043 			 * out wheel to this alternate address.
10044 			 */
10045 			if (net->ref_count > 1)
10046 				sctp_move_chunks_from_net(stcb, net);
10047 		} else {
10048 			/*-
10049 			 * if ((asoc->sat_network) || (net->addr_is_local))
10050 			 * { burst_limit = asoc->max_burst *
10051 			 * SCTP_SAT_NETWORK_BURST_INCR; }
10052 			 */
10053 			if (asoc->max_burst > 0) {
10054 				if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
10055 					if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) {
10056 						/*
10057 						 * JRS - Use the congestion
10058 						 * control given in the
10059 						 * congestion control module
10060 						 */
10061 						asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst);
10062 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10063 							sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED);
10064 						}
10065 						SCTP_STAT_INCR(sctps_maxburstqueued);
10066 					}
10067 					net->fast_retran_ip = 0;
10068 				} else {
10069 					if (net->flight_size == 0) {
10070 						/*
10071 						 * Should be decaying the
10072 						 * cwnd here
10073 						 */
10074 						;
10075 					}
10076 				}
10077 			}
10078 		}
10079 
10080 	}
10081 	burst_cnt = 0;
10082 	do {
10083 		error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
10084 		    &reason_code, 0, from_where,
10085 		    &now, &now_filled, frag_point, so_locked);
10086 		if (error) {
10087 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
10088 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10089 				sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
10090 			}
10091 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10092 				sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
10093 				sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
10094 			}
10095 			break;
10096 		}
10097 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
10098 
10099 		tot_out += num_out;
10100 		burst_cnt++;
10101 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10102 			sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
10103 			if (num_out == 0) {
10104 				sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
10105 			}
10106 		}
10107 		if (nagle_on) {
10108 			/*
10109 			 * When the Nagle algorithm is used, look at how
10110 			 * much is unsent, then if its smaller than an MTU
10111 			 * and we have data in flight we stop, except if we
10112 			 * are handling a fragmented user message.
10113 			 */
10114 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
10115 			    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
10116 			if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
10117 			    (stcb->asoc.total_flight > 0) &&
10118 			    ((stcb->asoc.locked_on_sending == NULL) ||
10119 			    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
10120 				break;
10121 			}
10122 		}
10123 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
10124 		    TAILQ_EMPTY(&asoc->send_queue) &&
10125 		    stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
10126 			/* Nothing left to send */
10127 			break;
10128 		}
10129 		if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
10130 			/* Nothing left to send */
10131 			break;
10132 		}
10133 	} while (num_out &&
10134 	    ((asoc->max_burst == 0) ||
10135 	    SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
10136 	    (burst_cnt < asoc->max_burst)));
10137 
10138 	if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
10139 		if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) {
10140 			SCTP_STAT_INCR(sctps_maxburstqueued);
10141 			asoc->burst_limit_applied = 1;
10142 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10143 				sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
10144 			}
10145 		} else {
10146 			asoc->burst_limit_applied = 0;
10147 		}
10148 	}
10149 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10150 		sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
10151 	}
10152 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
10153 	    tot_out);
10154 
10155 	/*-
10156 	 * Now we need to clean up the control chunk chain if a ECNE is on
10157 	 * it. It must be marked as UNSENT again so next call will continue
10158 	 * to send it until such time that we get a CWR, to remove it.
10159 	 */
10160 	if (stcb->asoc.ecn_echo_cnt_onq)
10161 		sctp_fix_ecn_echo(asoc);
10162 	return;
10163 }
10164 
10165 
10166 int
10167 sctp_output(
10168     struct sctp_inpcb *inp,
10169     struct mbuf *m,
10170     struct sockaddr *addr,
10171     struct mbuf *control,
10172     struct thread *p,
10173     int flags)
10174 {
10175 	if (inp == NULL) {
10176 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10177 		return (EINVAL);
10178 	}
10179 	if (inp->sctp_socket == NULL) {
10180 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10181 		return (EINVAL);
10182 	}
10183 	return (sctp_sosend(inp->sctp_socket,
10184 	    addr,
10185 	    (struct uio *)NULL,
10186 	    m,
10187 	    control,
10188 	    flags, p
10189 	    ));
10190 }
10191 
10192 void
10193 send_forward_tsn(struct sctp_tcb *stcb,
10194     struct sctp_association *asoc)
10195 {
10196 	struct sctp_tmit_chunk *chk;
10197 	struct sctp_forward_tsn_chunk *fwdtsn;
10198 	uint32_t advance_peer_ack_point;
10199 
10200 	SCTP_TCB_LOCK_ASSERT(stcb);
10201 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10202 		if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
10203 			/* mark it to unsent */
10204 			chk->sent = SCTP_DATAGRAM_UNSENT;
10205 			chk->snd_count = 0;
10206 			/* Do we correct its output location? */
10207 			if (chk->whoTo) {
10208 				sctp_free_remote_addr(chk->whoTo);
10209 				chk->whoTo = NULL;
10210 			}
10211 			goto sctp_fill_in_rest;
10212 		}
10213 	}
10214 	/* Ok if we reach here we must build one */
10215 	sctp_alloc_a_chunk(stcb, chk);
10216 	if (chk == NULL) {
10217 		return;
10218 	}
10219 	asoc->fwd_tsn_cnt++;
10220 	chk->copy_by_ref = 0;
10221 	chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
10222 	chk->rec.chunk_id.can_take_data = 0;
10223 	chk->asoc = asoc;
10224 	chk->whoTo = NULL;
10225 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10226 	if (chk->data == NULL) {
10227 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
10228 		return;
10229 	}
10230 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10231 	chk->sent = SCTP_DATAGRAM_UNSENT;
10232 	chk->snd_count = 0;
10233 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
10234 	asoc->ctrl_queue_cnt++;
10235 sctp_fill_in_rest:
10236 	/*-
10237 	 * Here we go through and fill out the part that deals with
10238 	 * stream/seq of the ones we skip.
10239 	 */
10240 	SCTP_BUF_LEN(chk->data) = 0;
10241 	{
10242 		struct sctp_tmit_chunk *at, *tp1, *last;
10243 		struct sctp_strseq *strseq;
10244 		unsigned int cnt_of_space, i, ovh;
10245 		unsigned int space_needed;
10246 		unsigned int cnt_of_skipped = 0;
10247 
10248 		TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
10249 			if (at->sent != SCTP_FORWARD_TSN_SKIP) {
10250 				/* no more to look at */
10251 				break;
10252 			}
10253 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
10254 				/* We don't report these */
10255 				continue;
10256 			}
10257 			cnt_of_skipped++;
10258 		}
10259 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
10260 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
10261 
10262 		cnt_of_space = M_TRAILINGSPACE(chk->data);
10263 
10264 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
10265 			ovh = SCTP_MIN_OVERHEAD;
10266 		} else {
10267 			ovh = SCTP_MIN_V4_OVERHEAD;
10268 		}
10269 		if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
10270 			/* trim to a mtu size */
10271 			cnt_of_space = asoc->smallest_mtu - ovh;
10272 		}
10273 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10274 			sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10275 			    0xff, 0, cnt_of_skipped,
10276 			    asoc->advanced_peer_ack_point);
10277 
10278 		}
10279 		advance_peer_ack_point = asoc->advanced_peer_ack_point;
10280 		if (cnt_of_space < space_needed) {
10281 			/*-
10282 			 * ok we must trim down the chunk by lowering the
10283 			 * advance peer ack point.
10284 			 */
10285 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10286 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10287 				    0xff, 0xff, cnt_of_space,
10288 				    space_needed);
10289 			}
10290 			cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
10291 			cnt_of_skipped /= sizeof(struct sctp_strseq);
10292 			/*-
10293 			 * Go through and find the TSN that will be the one
10294 			 * we report.
10295 			 */
10296 			at = TAILQ_FIRST(&asoc->sent_queue);
10297 			if (at != NULL) {
10298 				for (i = 0; i < cnt_of_skipped; i++) {
10299 					tp1 = TAILQ_NEXT(at, sctp_next);
10300 					if (tp1 == NULL) {
10301 						break;
10302 					}
10303 					at = tp1;
10304 				}
10305 			}
10306 			if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10307 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10308 				    0xff, cnt_of_skipped, at->rec.data.TSN_seq,
10309 				    asoc->advanced_peer_ack_point);
10310 			}
10311 			last = at;
10312 			/*-
10313 			 * last now points to last one I can report, update
10314 			 * peer ack point
10315 			 */
10316 			if (last)
10317 				advance_peer_ack_point = last->rec.data.TSN_seq;
10318 			space_needed = sizeof(struct sctp_forward_tsn_chunk) +
10319 			    cnt_of_skipped * sizeof(struct sctp_strseq);
10320 		}
10321 		chk->send_size = space_needed;
10322 		/* Setup the chunk */
10323 		fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
10324 		fwdtsn->ch.chunk_length = htons(chk->send_size);
10325 		fwdtsn->ch.chunk_flags = 0;
10326 		fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
10327 		fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
10328 		SCTP_BUF_LEN(chk->data) = chk->send_size;
10329 		fwdtsn++;
10330 		/*-
10331 		 * Move pointer to after the fwdtsn and transfer to the
10332 		 * strseq pointer.
10333 		 */
10334 		strseq = (struct sctp_strseq *)fwdtsn;
10335 		/*-
10336 		 * Now populate the strseq list. This is done blindly
10337 		 * without pulling out duplicate stream info. This is
10338 		 * inefficent but won't harm the process since the peer will
10339 		 * look at these in sequence and will thus release anything.
10340 		 * It could mean we exceed the PMTU and chop off some that
10341 		 * we could have included.. but this is unlikely (aka 1432/4
10342 		 * would mean 300+ stream seq's would have to be reported in
10343 		 * one FWD-TSN. With a bit of work we can later FIX this to
10344 		 * optimize and pull out duplcates.. but it does add more
10345 		 * overhead. So for now... not!
10346 		 */
10347 		at = TAILQ_FIRST(&asoc->sent_queue);
10348 		for (i = 0; i < cnt_of_skipped; i++) {
10349 			tp1 = TAILQ_NEXT(at, sctp_next);
10350 			if (tp1 == NULL)
10351 				break;
10352 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
10353 				/* We don't report these */
10354 				i--;
10355 				at = tp1;
10356 				continue;
10357 			}
10358 			if (at->rec.data.TSN_seq == advance_peer_ack_point) {
10359 				at->rec.data.fwd_tsn_cnt = 0;
10360 			}
10361 			strseq->stream = ntohs(at->rec.data.stream_number);
10362 			strseq->sequence = ntohs(at->rec.data.stream_seq);
10363 			strseq++;
10364 			at = tp1;
10365 		}
10366 	}
10367 	return;
10368 }
10369 
10370 void
10371 sctp_send_sack(struct sctp_tcb *stcb, int so_locked
10372 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10373     SCTP_UNUSED
10374 #endif
10375 )
10376 {
10377 	/*-
10378 	 * Queue up a SACK or NR-SACK in the control queue.
10379 	 * We must first check to see if a SACK or NR-SACK is
10380 	 * somehow on the control queue.
10381 	 * If so, we will take and and remove the old one.
10382 	 */
10383 	struct sctp_association *asoc;
10384 	struct sctp_tmit_chunk *chk, *a_chk;
10385 	struct sctp_sack_chunk *sack;
10386 	struct sctp_nr_sack_chunk *nr_sack;
10387 	struct sctp_gap_ack_block *gap_descriptor;
10388 	struct sack_track *selector;
10389 	int mergeable = 0;
10390 	int offset;
10391 	caddr_t limit;
10392 	uint32_t *dup;
10393 	int limit_reached = 0;
10394 	unsigned int i, siz, j;
10395 	unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
10396 	int num_dups = 0;
10397 	int space_req;
10398 	uint32_t highest_tsn;
10399 	uint8_t flags;
10400 	uint8_t type;
10401 	uint8_t tsn_map;
10402 
10403 	if ((stcb->asoc.sctp_nr_sack_on_off == 1) &&
10404 	    (stcb->asoc.peer_supports_nr_sack == 1)) {
10405 		type = SCTP_NR_SELECTIVE_ACK;
10406 	} else {
10407 		type = SCTP_SELECTIVE_ACK;
10408 	}
10409 	a_chk = NULL;
10410 	asoc = &stcb->asoc;
10411 	SCTP_TCB_LOCK_ASSERT(stcb);
10412 	if (asoc->last_data_chunk_from == NULL) {
10413 		/* Hmm we never received anything */
10414 		return;
10415 	}
10416 	sctp_slide_mapping_arrays(stcb);
10417 	sctp_set_rwnd(stcb, asoc);
10418 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10419 		if (chk->rec.chunk_id.id == type) {
10420 			/* Hmm, found a sack already on queue, remove it */
10421 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
10422 			asoc->ctrl_queue_cnt--;
10423 			a_chk = chk;
10424 			if (a_chk->data) {
10425 				sctp_m_freem(a_chk->data);
10426 				a_chk->data = NULL;
10427 			}
10428 			if (a_chk->whoTo) {
10429 				sctp_free_remote_addr(a_chk->whoTo);
10430 				a_chk->whoTo = NULL;
10431 			}
10432 			break;
10433 		}
10434 	}
10435 	if (a_chk == NULL) {
10436 		sctp_alloc_a_chunk(stcb, a_chk);
10437 		if (a_chk == NULL) {
10438 			/* No memory so we drop the idea, and set a timer */
10439 			if (stcb->asoc.delayed_ack) {
10440 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10441 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
10442 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10443 				    stcb->sctp_ep, stcb, NULL);
10444 			} else {
10445 				stcb->asoc.send_sack = 1;
10446 			}
10447 			return;
10448 		}
10449 		a_chk->copy_by_ref = 0;
10450 		a_chk->rec.chunk_id.id = type;
10451 		a_chk->rec.chunk_id.can_take_data = 1;
10452 	}
10453 	/* Clear our pkt counts */
10454 	asoc->data_pkts_seen = 0;
10455 
10456 	a_chk->asoc = asoc;
10457 	a_chk->snd_count = 0;
10458 	a_chk->send_size = 0;	/* fill in later */
10459 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
10460 	a_chk->whoTo = NULL;
10461 
10462 	if ((asoc->numduptsns) ||
10463 	    (!(asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE))) {
10464 		/*-
10465 		 * Ok, we have some duplicates or the destination for the
10466 		 * sack is unreachable, lets see if we can select an
10467 		 * alternate than asoc->last_data_chunk_from
10468 		 */
10469 		if ((asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE) &&
10470 		    (asoc->used_alt_onsack > asoc->numnets)) {
10471 			/* We used an alt last time, don't this time */
10472 			a_chk->whoTo = NULL;
10473 		} else {
10474 			asoc->used_alt_onsack++;
10475 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
10476 		}
10477 		if (a_chk->whoTo == NULL) {
10478 			/* Nope, no alternate */
10479 			a_chk->whoTo = asoc->last_data_chunk_from;
10480 			asoc->used_alt_onsack = 0;
10481 		}
10482 	} else {
10483 		/*
10484 		 * No duplicates so we use the last place we received data
10485 		 * from.
10486 		 */
10487 		asoc->used_alt_onsack = 0;
10488 		a_chk->whoTo = asoc->last_data_chunk_from;
10489 	}
10490 	if (a_chk->whoTo) {
10491 		atomic_add_int(&a_chk->whoTo->ref_count, 1);
10492 	}
10493 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) {
10494 		highest_tsn = asoc->highest_tsn_inside_map;
10495 	} else {
10496 		highest_tsn = asoc->highest_tsn_inside_nr_map;
10497 	}
10498 	if (highest_tsn == asoc->cumulative_tsn) {
10499 		/* no gaps */
10500 		if (type == SCTP_SELECTIVE_ACK) {
10501 			space_req = sizeof(struct sctp_sack_chunk);
10502 		} else {
10503 			space_req = sizeof(struct sctp_nr_sack_chunk);
10504 		}
10505 	} else {
10506 		/* gaps get a cluster */
10507 		space_req = MCLBYTES;
10508 	}
10509 	/* Ok now lets formulate a MBUF with our sack */
10510 	a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
10511 	if ((a_chk->data == NULL) ||
10512 	    (a_chk->whoTo == NULL)) {
10513 		/* rats, no mbuf memory */
10514 		if (a_chk->data) {
10515 			/* was a problem with the destination */
10516 			sctp_m_freem(a_chk->data);
10517 			a_chk->data = NULL;
10518 		}
10519 		sctp_free_a_chunk(stcb, a_chk, so_locked);
10520 		/* sa_ignore NO_NULL_CHK */
10521 		if (stcb->asoc.delayed_ack) {
10522 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10523 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
10524 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10525 			    stcb->sctp_ep, stcb, NULL);
10526 		} else {
10527 			stcb->asoc.send_sack = 1;
10528 		}
10529 		return;
10530 	}
10531 	/* ok, lets go through and fill it in */
10532 	SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10533 	space = M_TRAILINGSPACE(a_chk->data);
10534 	if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10535 		space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10536 	}
10537 	limit = mtod(a_chk->data, caddr_t);
10538 	limit += space;
10539 
10540 	flags = 0;
10541 
10542 	if ((asoc->sctp_cmt_on_off > 0) &&
10543 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10544 		/*-
10545 		 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10546 		 * received, then set high bit to 1, else 0. Reset
10547 		 * pkts_rcvd.
10548 		 */
10549 		flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10550 		asoc->cmt_dac_pkts_rcvd = 0;
10551 	}
10552 #ifdef SCTP_ASOCLOG_OF_TSNS
10553 	stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10554 	stcb->asoc.cumack_log_atsnt++;
10555 	if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10556 		stcb->asoc.cumack_log_atsnt = 0;
10557 	}
10558 #endif
10559 	/* reset the readers interpretation */
10560 	stcb->freed_by_sorcv_sincelast = 0;
10561 
10562 	if (type == SCTP_SELECTIVE_ACK) {
10563 		sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10564 		nr_sack = NULL;
10565 		gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10566 		if (highest_tsn > asoc->mapping_array_base_tsn) {
10567 			siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10568 		} else {
10569 			siz = (((MAX_TSN - highest_tsn) + 1) + highest_tsn + 7) / 8;
10570 		}
10571 	} else {
10572 		sack = NULL;
10573 		nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10574 		gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10575 		if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
10576 			siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10577 		} else {
10578 			siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10579 		}
10580 	}
10581 
10582 	if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10583 		offset = 1;
10584 	} else {
10585 		offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10586 	}
10587 	if (((type == SCTP_SELECTIVE_ACK) &&
10588 	    SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) ||
10589 	    ((type == SCTP_NR_SELECTIVE_ACK) &&
10590 	    SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) {
10591 		/* we have a gap .. maybe */
10592 		for (i = 0; i < siz; i++) {
10593 			tsn_map = asoc->mapping_array[i];
10594 			if (type == SCTP_SELECTIVE_ACK) {
10595 				tsn_map |= asoc->nr_mapping_array[i];
10596 			}
10597 			if (i == 0) {
10598 				/*
10599 				 * Clear all bits corresponding to TSNs
10600 				 * smaller or equal to the cumulative TSN.
10601 				 */
10602 				tsn_map &= (~0 << (1 - offset));
10603 			}
10604 			selector = &sack_array[tsn_map];
10605 			if (mergeable && selector->right_edge) {
10606 				/*
10607 				 * Backup, left and right edges were ok to
10608 				 * merge.
10609 				 */
10610 				num_gap_blocks--;
10611 				gap_descriptor--;
10612 			}
10613 			if (selector->num_entries == 0)
10614 				mergeable = 0;
10615 			else {
10616 				for (j = 0; j < selector->num_entries; j++) {
10617 					if (mergeable && selector->right_edge) {
10618 						/*
10619 						 * do a merge by NOT setting
10620 						 * the left side
10621 						 */
10622 						mergeable = 0;
10623 					} else {
10624 						/*
10625 						 * no merge, set the left
10626 						 * side
10627 						 */
10628 						mergeable = 0;
10629 						gap_descriptor->start = htons((selector->gaps[j].start + offset));
10630 					}
10631 					gap_descriptor->end = htons((selector->gaps[j].end + offset));
10632 					num_gap_blocks++;
10633 					gap_descriptor++;
10634 					if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10635 						/* no more room */
10636 						limit_reached = 1;
10637 						break;
10638 					}
10639 				}
10640 				if (selector->left_edge) {
10641 					mergeable = 1;
10642 				}
10643 			}
10644 			if (limit_reached) {
10645 				/* Reached the limit stop */
10646 				break;
10647 			}
10648 			offset += 8;
10649 		}
10650 	}
10651 	if ((type == SCTP_NR_SELECTIVE_ACK) &&
10652 	    (limit_reached == 0)) {
10653 
10654 		mergeable = 0;
10655 
10656 		if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) {
10657 			siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10658 		} else {
10659 			siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10660 		}
10661 
10662 		if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10663 			offset = 1;
10664 		} else {
10665 			offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10666 		}
10667 		if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) {
10668 			/* we have a gap .. maybe */
10669 			for (i = 0; i < siz; i++) {
10670 				tsn_map = asoc->nr_mapping_array[i];
10671 				if (i == 0) {
10672 					/*
10673 					 * Clear all bits corresponding to
10674 					 * TSNs smaller or equal to the
10675 					 * cumulative TSN.
10676 					 */
10677 					tsn_map &= (~0 << (1 - offset));
10678 				}
10679 				selector = &sack_array[tsn_map];
10680 				if (mergeable && selector->right_edge) {
10681 					/*
10682 					 * Backup, left and right edges were
10683 					 * ok to merge.
10684 					 */
10685 					num_nr_gap_blocks--;
10686 					gap_descriptor--;
10687 				}
10688 				if (selector->num_entries == 0)
10689 					mergeable = 0;
10690 				else {
10691 					for (j = 0; j < selector->num_entries; j++) {
10692 						if (mergeable && selector->right_edge) {
10693 							/*
10694 							 * do a merge by NOT
10695 							 * setting the left
10696 							 * side
10697 							 */
10698 							mergeable = 0;
10699 						} else {
10700 							/*
10701 							 * no merge, set the
10702 							 * left side
10703 							 */
10704 							mergeable = 0;
10705 							gap_descriptor->start = htons((selector->gaps[j].start + offset));
10706 						}
10707 						gap_descriptor->end = htons((selector->gaps[j].end + offset));
10708 						num_nr_gap_blocks++;
10709 						gap_descriptor++;
10710 						if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10711 							/* no more room */
10712 							limit_reached = 1;
10713 							break;
10714 						}
10715 					}
10716 					if (selector->left_edge) {
10717 						mergeable = 1;
10718 					}
10719 				}
10720 				if (limit_reached) {
10721 					/* Reached the limit stop */
10722 					break;
10723 				}
10724 				offset += 8;
10725 			}
10726 		}
10727 	}
10728 	/* now we must add any dups we are going to report. */
10729 	if ((limit_reached == 0) && (asoc->numduptsns)) {
10730 		dup = (uint32_t *) gap_descriptor;
10731 		for (i = 0; i < asoc->numduptsns; i++) {
10732 			*dup = htonl(asoc->dup_tsns[i]);
10733 			dup++;
10734 			num_dups++;
10735 			if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10736 				/* no more room */
10737 				break;
10738 			}
10739 		}
10740 		asoc->numduptsns = 0;
10741 	}
10742 	/*
10743 	 * now that the chunk is prepared queue it to the control chunk
10744 	 * queue.
10745 	 */
10746 	if (type == SCTP_SELECTIVE_ACK) {
10747 		a_chk->send_size = sizeof(struct sctp_sack_chunk) +
10748 		    (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10749 		    num_dups * sizeof(int32_t);
10750 		SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10751 		sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10752 		sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10753 		sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10754 		sack->sack.num_dup_tsns = htons(num_dups);
10755 		sack->ch.chunk_type = type;
10756 		sack->ch.chunk_flags = flags;
10757 		sack->ch.chunk_length = htons(a_chk->send_size);
10758 	} else {
10759 		a_chk->send_size = sizeof(struct sctp_nr_sack_chunk) +
10760 		    (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10761 		    num_dups * sizeof(int32_t);
10762 		SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10763 		nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10764 		nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10765 		nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10766 		nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10767 		nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10768 		nr_sack->nr_sack.reserved = 0;
10769 		nr_sack->ch.chunk_type = type;
10770 		nr_sack->ch.chunk_flags = flags;
10771 		nr_sack->ch.chunk_length = htons(a_chk->send_size);
10772 	}
10773 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10774 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
10775 	asoc->ctrl_queue_cnt++;
10776 	asoc->send_sack = 0;
10777 	SCTP_STAT_INCR(sctps_sendsacks);
10778 	return;
10779 }
10780 
10781 void
10782 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10783 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10784     SCTP_UNUSED
10785 #endif
10786 )
10787 {
10788 	struct mbuf *m_abort;
10789 	struct mbuf *m_out = NULL, *m_end = NULL;
10790 	struct sctp_abort_chunk *abort = NULL;
10791 	int sz;
10792 	uint32_t auth_offset = 0;
10793 	struct sctp_auth_chunk *auth = NULL;
10794 	struct sctp_nets *net;
10795 
10796 	/*-
10797 	 * Add an AUTH chunk, if chunk requires it and save the offset into
10798 	 * the chain for AUTH
10799 	 */
10800 	if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10801 	    stcb->asoc.peer_auth_chunks)) {
10802 		m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10803 		    stcb, SCTP_ABORT_ASSOCIATION);
10804 		SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10805 	}
10806 	SCTP_TCB_LOCK_ASSERT(stcb);
10807 	m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10808 	if (m_abort == NULL) {
10809 		/* no mbuf's */
10810 		if (m_out)
10811 			sctp_m_freem(m_out);
10812 		return;
10813 	}
10814 	/* link in any error */
10815 	SCTP_BUF_NEXT(m_abort) = operr;
10816 	sz = 0;
10817 	if (operr) {
10818 		struct mbuf *n;
10819 
10820 		n = operr;
10821 		while (n) {
10822 			sz += SCTP_BUF_LEN(n);
10823 			n = SCTP_BUF_NEXT(n);
10824 		}
10825 	}
10826 	SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10827 	if (m_out == NULL) {
10828 		/* NO Auth chunk prepended, so reserve space in front */
10829 		SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10830 		m_out = m_abort;
10831 	} else {
10832 		/* Put AUTH chunk at the front of the chain */
10833 		SCTP_BUF_NEXT(m_end) = m_abort;
10834 	}
10835 	if (stcb->asoc.alternate) {
10836 		net = stcb->asoc.alternate;
10837 	} else {
10838 		net = stcb->asoc.primary_destination;
10839 	}
10840 	/* fill in the ABORT chunk */
10841 	abort = mtod(m_abort, struct sctp_abort_chunk *);
10842 	abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10843 	abort->ch.chunk_flags = 0;
10844 	abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10845 
10846 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10847 	    (struct sockaddr *)&net->ro._l_addr,
10848 	    m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, 0,
10849 	    stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10850 	    stcb->asoc.primary_destination->port, so_locked, NULL, NULL);
10851 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10852 }
10853 
10854 void
10855 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10856     struct sctp_nets *net,
10857     int reflect_vtag)
10858 {
10859 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10860 	struct mbuf *m_shutdown_comp;
10861 	struct sctp_shutdown_complete_chunk *shutdown_complete;
10862 	uint32_t vtag;
10863 	uint8_t flags;
10864 
10865 	m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10866 	if (m_shutdown_comp == NULL) {
10867 		/* no mbuf's */
10868 		return;
10869 	}
10870 	if (reflect_vtag) {
10871 		flags = SCTP_HAD_NO_TCB;
10872 		vtag = stcb->asoc.my_vtag;
10873 	} else {
10874 		flags = 0;
10875 		vtag = stcb->asoc.peer_vtag;
10876 	}
10877 	shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10878 	shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10879 	shutdown_complete->ch.chunk_flags = flags;
10880 	shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10881 	SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10882 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10883 	    (struct sockaddr *)&net->ro._l_addr,
10884 	    m_shutdown_comp, 0, NULL, 0, 1, 0, 0,
10885 	    stcb->sctp_ep->sctp_lport, stcb->rport,
10886 	    htonl(vtag),
10887 	    net->port, SCTP_SO_NOT_LOCKED, NULL, NULL);
10888 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10889 	return;
10890 }
10891 
10892 void
10893 sctp_send_shutdown_complete2(struct mbuf *m, struct sctphdr *sh,
10894     uint32_t vrf_id, uint16_t port)
10895 {
10896 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10897 	struct mbuf *o_pak;
10898 	struct mbuf *mout;
10899 	struct ip *iph;
10900 	struct udphdr *udp = NULL;
10901 	int offset_out, len, mlen;
10902 	struct sctp_shutdown_complete_msg *comp_cp;
10903 
10904 #ifdef INET
10905 	struct ip *iph_out;
10906 
10907 #endif
10908 #ifdef INET6
10909 	struct ip6_hdr *ip6, *ip6_out;
10910 
10911 #endif
10912 
10913 	iph = mtod(m, struct ip *);
10914 	switch (iph->ip_v) {
10915 #ifdef INET
10916 	case IPVERSION:
10917 		len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10918 		break;
10919 #endif
10920 #ifdef INET6
10921 	case IPV6_VERSION >> 4:
10922 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10923 		break;
10924 #endif
10925 	default:
10926 		return;
10927 	}
10928 	if (port) {
10929 		len += sizeof(struct udphdr);
10930 	}
10931 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
10932 	if (mout == NULL) {
10933 		return;
10934 	}
10935 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
10936 	SCTP_BUF_LEN(mout) = len;
10937 	SCTP_BUF_NEXT(mout) = NULL;
10938 	if (m->m_flags & M_FLOWID) {
10939 		mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
10940 		mout->m_flags |= M_FLOWID;
10941 	}
10942 #ifdef INET
10943 	iph_out = NULL;
10944 #endif
10945 #ifdef INET6
10946 	ip6_out = NULL;
10947 #endif
10948 	offset_out = 0;
10949 
10950 	switch (iph->ip_v) {
10951 #ifdef INET
10952 	case IPVERSION:
10953 		iph_out = mtod(mout, struct ip *);
10954 
10955 		/* Fill in the IP header for the ABORT */
10956 		iph_out->ip_v = IPVERSION;
10957 		iph_out->ip_hl = (sizeof(struct ip) / 4);
10958 		iph_out->ip_tos = (u_char)0;
10959 		iph_out->ip_id = 0;
10960 		iph_out->ip_off = 0;
10961 		iph_out->ip_ttl = MAXTTL;
10962 		if (port) {
10963 			iph_out->ip_p = IPPROTO_UDP;
10964 		} else {
10965 			iph_out->ip_p = IPPROTO_SCTP;
10966 		}
10967 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10968 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10969 
10970 		/* let IP layer calculate this */
10971 		iph_out->ip_sum = 0;
10972 		offset_out += sizeof(*iph_out);
10973 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10974 		    (caddr_t)iph_out + offset_out);
10975 		break;
10976 #endif
10977 #ifdef INET6
10978 	case IPV6_VERSION >> 4:
10979 		ip6 = (struct ip6_hdr *)iph;
10980 		ip6_out = mtod(mout, struct ip6_hdr *);
10981 
10982 		/* Fill in the IPv6 header for the ABORT */
10983 		ip6_out->ip6_flow = ip6->ip6_flow;
10984 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
10985 		if (port) {
10986 			ip6_out->ip6_nxt = IPPROTO_UDP;
10987 		} else {
10988 			ip6_out->ip6_nxt = IPPROTO_SCTP;
10989 		}
10990 		ip6_out->ip6_src = ip6->ip6_dst;
10991 		ip6_out->ip6_dst = ip6->ip6_src;
10992 		/*
10993 		 * ?? The old code had both the iph len + payload, I think
10994 		 * this is wrong and would never have worked
10995 		 */
10996 		ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10997 		offset_out += sizeof(*ip6_out);
10998 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10999 		    (caddr_t)ip6_out + offset_out);
11000 		break;
11001 #endif				/* INET6 */
11002 	default:
11003 		/* Currently not supported. */
11004 		sctp_m_freem(mout);
11005 		return;
11006 	}
11007 	if (port) {
11008 		if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
11009 			sctp_m_freem(mout);
11010 			return;
11011 		}
11012 		udp = (struct udphdr *)comp_cp;
11013 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11014 		udp->uh_dport = port;
11015 		udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
11016 #ifdef INET
11017 		if (iph_out) {
11018 			if (V_udp_cksum) {
11019 				udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11020 			} else {
11021 				udp->uh_sum = 0;
11022 			}
11023 		}
11024 #endif
11025 		offset_out += sizeof(struct udphdr);
11026 		comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
11027 	}
11028 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11029 		/* no mbuf's */
11030 		sctp_m_freem(mout);
11031 		return;
11032 	}
11033 	/* Now copy in and fill in the ABORT tags etc. */
11034 	comp_cp->sh.src_port = sh->dest_port;
11035 	comp_cp->sh.dest_port = sh->src_port;
11036 	comp_cp->sh.checksum = 0;
11037 	comp_cp->sh.v_tag = sh->v_tag;
11038 	comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
11039 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
11040 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
11041 
11042 #ifdef INET
11043 	if (iph_out != NULL) {
11044 		sctp_route_t ro;
11045 		int ret;
11046 
11047 		mlen = SCTP_BUF_LEN(mout);
11048 		bzero(&ro, sizeof ro);
11049 		/* set IPv4 length */
11050 		iph_out->ip_len = mlen;
11051 #ifdef  SCTP_PACKET_LOGGING
11052 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11053 			sctp_packet_log(mout, mlen);
11054 #endif
11055 		if (port) {
11056 #if defined(SCTP_WITH_NO_CSUM)
11057 			SCTP_STAT_INCR(sctps_sendnocrc);
11058 #else
11059 			comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
11060 			SCTP_STAT_INCR(sctps_sendswcrc);
11061 #endif
11062 			if (V_udp_cksum) {
11063 				SCTP_ENABLE_UDP_CSUM(mout);
11064 			}
11065 		} else {
11066 #if defined(SCTP_WITH_NO_CSUM)
11067 			SCTP_STAT_INCR(sctps_sendnocrc);
11068 #else
11069 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
11070 			mout->m_pkthdr.csum_data = 0;
11071 			SCTP_STAT_INCR(sctps_sendhwcrc);
11072 #endif
11073 		}
11074 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
11075 		/* out it goes */
11076 		SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
11077 
11078 		/* Free the route if we got one back */
11079 		if (ro.ro_rt)
11080 			RTFREE(ro.ro_rt);
11081 	}
11082 #endif
11083 #ifdef INET6
11084 	if (ip6_out != NULL) {
11085 		struct route_in6 ro;
11086 		int ret;
11087 		struct ifnet *ifp = NULL;
11088 
11089 		bzero(&ro, sizeof(ro));
11090 		mlen = SCTP_BUF_LEN(mout);
11091 #ifdef  SCTP_PACKET_LOGGING
11092 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11093 			sctp_packet_log(mout, mlen);
11094 #endif
11095 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
11096 		if (port) {
11097 #if defined(SCTP_WITH_NO_CSUM)
11098 			SCTP_STAT_INCR(sctps_sendnocrc);
11099 #else
11100 			comp_cp->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11101 			SCTP_STAT_INCR(sctps_sendswcrc);
11102 #endif
11103 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), mlen - sizeof(struct ip6_hdr))) == 0) {
11104 				udp->uh_sum = 0xffff;
11105 			}
11106 		} else {
11107 #if defined(SCTP_WITH_NO_CSUM)
11108 			SCTP_STAT_INCR(sctps_sendnocrc);
11109 #else
11110 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
11111 			mout->m_pkthdr.csum_data = 0;
11112 			SCTP_STAT_INCR(sctps_sendhwcrc);
11113 #endif
11114 		}
11115 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
11116 
11117 		/* Free the route if we got one back */
11118 		if (ro.ro_rt)
11119 			RTFREE(ro.ro_rt);
11120 	}
11121 #endif
11122 	SCTP_STAT_INCR(sctps_sendpackets);
11123 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11124 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11125 	return;
11126 
11127 }
11128 
11129 void
11130 sctp_send_hb(struct sctp_tcb *stcb, struct sctp_nets *net, int so_locked
11131 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
11132     SCTP_UNUSED
11133 #endif
11134 )
11135 {
11136 	struct sctp_tmit_chunk *chk;
11137 	struct sctp_heartbeat_chunk *hb;
11138 	struct timeval now;
11139 
11140 	SCTP_TCB_LOCK_ASSERT(stcb);
11141 	if (net == NULL) {
11142 		return;
11143 	}
11144 	(void)SCTP_GETTIME_TIMEVAL(&now);
11145 	switch (net->ro._l_addr.sa.sa_family) {
11146 #ifdef INET
11147 	case AF_INET:
11148 		break;
11149 #endif
11150 #ifdef INET6
11151 	case AF_INET6:
11152 		break;
11153 #endif
11154 	default:
11155 		return;
11156 	}
11157 	sctp_alloc_a_chunk(stcb, chk);
11158 	if (chk == NULL) {
11159 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
11160 		return;
11161 	}
11162 	chk->copy_by_ref = 0;
11163 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
11164 	chk->rec.chunk_id.can_take_data = 1;
11165 	chk->asoc = &stcb->asoc;
11166 	chk->send_size = sizeof(struct sctp_heartbeat_chunk);
11167 
11168 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11169 	if (chk->data == NULL) {
11170 		sctp_free_a_chunk(stcb, chk, so_locked);
11171 		return;
11172 	}
11173 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11174 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11175 	chk->sent = SCTP_DATAGRAM_UNSENT;
11176 	chk->snd_count = 0;
11177 	chk->whoTo = net;
11178 	atomic_add_int(&chk->whoTo->ref_count, 1);
11179 	/* Now we have a mbuf that we can fill in with the details */
11180 	hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
11181 	memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
11182 	/* fill out chunk header */
11183 	hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
11184 	hb->ch.chunk_flags = 0;
11185 	hb->ch.chunk_length = htons(chk->send_size);
11186 	/* Fill out hb parameter */
11187 	hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
11188 	hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
11189 	hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
11190 	hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
11191 	/* Did our user request this one, put it in */
11192 	hb->heartbeat.hb_info.addr_family = net->ro._l_addr.sa.sa_family;
11193 	hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len;
11194 	if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
11195 		/*
11196 		 * we only take from the entropy pool if the address is not
11197 		 * confirmed.
11198 		 */
11199 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11200 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11201 	} else {
11202 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
11203 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
11204 	}
11205 	switch (net->ro._l_addr.sa.sa_family) {
11206 #ifdef INET
11207 	case AF_INET:
11208 		memcpy(hb->heartbeat.hb_info.address,
11209 		    &net->ro._l_addr.sin.sin_addr,
11210 		    sizeof(net->ro._l_addr.sin.sin_addr));
11211 		break;
11212 #endif
11213 #ifdef INET6
11214 	case AF_INET6:
11215 		memcpy(hb->heartbeat.hb_info.address,
11216 		    &net->ro._l_addr.sin6.sin6_addr,
11217 		    sizeof(net->ro._l_addr.sin6.sin6_addr));
11218 		break;
11219 #endif
11220 	default:
11221 		return;
11222 		break;
11223 	}
11224 	net->hb_responded = 0;
11225 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11226 	stcb->asoc.ctrl_queue_cnt++;
11227 	SCTP_STAT_INCR(sctps_sendheartbeat);
11228 	return;
11229 }
11230 
11231 void
11232 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
11233     uint32_t high_tsn)
11234 {
11235 	struct sctp_association *asoc;
11236 	struct sctp_ecne_chunk *ecne;
11237 	struct sctp_tmit_chunk *chk;
11238 
11239 	if (net == NULL) {
11240 		return;
11241 	}
11242 	asoc = &stcb->asoc;
11243 	SCTP_TCB_LOCK_ASSERT(stcb);
11244 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11245 		if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) {
11246 			/* found a previous ECN_ECHO update it if needed */
11247 			uint32_t cnt, ctsn;
11248 
11249 			ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11250 			ctsn = ntohl(ecne->tsn);
11251 			if (SCTP_TSN_GT(high_tsn, ctsn)) {
11252 				ecne->tsn = htonl(high_tsn);
11253 				SCTP_STAT_INCR(sctps_queue_upd_ecne);
11254 			}
11255 			cnt = ntohl(ecne->num_pkts_since_cwr);
11256 			cnt++;
11257 			ecne->num_pkts_since_cwr = htonl(cnt);
11258 			return;
11259 		}
11260 	}
11261 	/* nope could not find one to update so we must build one */
11262 	sctp_alloc_a_chunk(stcb, chk);
11263 	if (chk == NULL) {
11264 		return;
11265 	}
11266 	chk->copy_by_ref = 0;
11267 	SCTP_STAT_INCR(sctps_queue_upd_ecne);
11268 	chk->rec.chunk_id.id = SCTP_ECN_ECHO;
11269 	chk->rec.chunk_id.can_take_data = 0;
11270 	chk->asoc = &stcb->asoc;
11271 	chk->send_size = sizeof(struct sctp_ecne_chunk);
11272 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11273 	if (chk->data == NULL) {
11274 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11275 		return;
11276 	}
11277 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11278 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11279 	chk->sent = SCTP_DATAGRAM_UNSENT;
11280 	chk->snd_count = 0;
11281 	chk->whoTo = net;
11282 	atomic_add_int(&chk->whoTo->ref_count, 1);
11283 
11284 	stcb->asoc.ecn_echo_cnt_onq++;
11285 	ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11286 	ecne->ch.chunk_type = SCTP_ECN_ECHO;
11287 	ecne->ch.chunk_flags = 0;
11288 	ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
11289 	ecne->tsn = htonl(high_tsn);
11290 	ecne->num_pkts_since_cwr = htonl(1);
11291 	TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next);
11292 	asoc->ctrl_queue_cnt++;
11293 }
11294 
11295 void
11296 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
11297     struct mbuf *m, int iphlen, int bad_crc)
11298 {
11299 	struct sctp_association *asoc;
11300 	struct sctp_pktdrop_chunk *drp;
11301 	struct sctp_tmit_chunk *chk;
11302 	uint8_t *datap;
11303 	int len;
11304 	int was_trunc = 0;
11305 	struct ip *iph;
11306 
11307 #ifdef INET6
11308 	struct ip6_hdr *ip6h;
11309 
11310 #endif
11311 	int fullsz = 0, extra = 0;
11312 	long spc;
11313 	int offset;
11314 	struct sctp_chunkhdr *ch, chunk_buf;
11315 	unsigned int chk_length;
11316 
11317 	if (!stcb) {
11318 		return;
11319 	}
11320 	asoc = &stcb->asoc;
11321 	SCTP_TCB_LOCK_ASSERT(stcb);
11322 	if (asoc->peer_supports_pktdrop == 0) {
11323 		/*-
11324 		 * peer must declare support before I send one.
11325 		 */
11326 		return;
11327 	}
11328 	if (stcb->sctp_socket == NULL) {
11329 		return;
11330 	}
11331 	sctp_alloc_a_chunk(stcb, chk);
11332 	if (chk == NULL) {
11333 		return;
11334 	}
11335 	chk->copy_by_ref = 0;
11336 	iph = mtod(m, struct ip *);
11337 	if (iph == NULL) {
11338 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11339 		return;
11340 	}
11341 	switch (iph->ip_v) {
11342 #ifdef INET
11343 	case IPVERSION:
11344 		/* IPv4 */
11345 		len = chk->send_size = iph->ip_len;
11346 		break;
11347 #endif
11348 #ifdef INET6
11349 	case IPV6_VERSION >> 4:
11350 		/* IPv6 */
11351 		ip6h = mtod(m, struct ip6_hdr *);
11352 		len = chk->send_size = htons(ip6h->ip6_plen);
11353 		break;
11354 #endif
11355 	default:
11356 		return;
11357 	}
11358 	/* Validate that we do not have an ABORT in here. */
11359 	offset = iphlen + sizeof(struct sctphdr);
11360 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11361 	    sizeof(*ch), (uint8_t *) & chunk_buf);
11362 	while (ch != NULL) {
11363 		chk_length = ntohs(ch->chunk_length);
11364 		if (chk_length < sizeof(*ch)) {
11365 			/* break to abort land */
11366 			break;
11367 		}
11368 		switch (ch->chunk_type) {
11369 		case SCTP_PACKET_DROPPED:
11370 		case SCTP_ABORT_ASSOCIATION:
11371 		case SCTP_INITIATION_ACK:
11372 			/**
11373 			 * We don't respond with an PKT-DROP to an ABORT
11374 			 * or PKT-DROP. We also do not respond to an
11375 			 * INIT-ACK, because we can't know if the initiation
11376 			 * tag is correct or not.
11377 			 */
11378 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11379 			return;
11380 		default:
11381 			break;
11382 		}
11383 		offset += SCTP_SIZE32(chk_length);
11384 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11385 		    sizeof(*ch), (uint8_t *) & chunk_buf);
11386 	}
11387 
11388 	if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11389 	    min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11390 		/*
11391 		 * only send 1 mtu worth, trim off the excess on the end.
11392 		 */
11393 		fullsz = len - extra;
11394 		len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11395 		was_trunc = 1;
11396 	}
11397 	chk->asoc = &stcb->asoc;
11398 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11399 	if (chk->data == NULL) {
11400 jump_out:
11401 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11402 		return;
11403 	}
11404 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11405 	drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11406 	if (drp == NULL) {
11407 		sctp_m_freem(chk->data);
11408 		chk->data = NULL;
11409 		goto jump_out;
11410 	}
11411 	chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11412 	    sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11413 	chk->book_size_scale = 0;
11414 	if (was_trunc) {
11415 		drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11416 		drp->trunc_len = htons(fullsz);
11417 		/*
11418 		 * Len is already adjusted to size minus overhead above take
11419 		 * out the pkt_drop chunk itself from it.
11420 		 */
11421 		chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
11422 		len = chk->send_size;
11423 	} else {
11424 		/* no truncation needed */
11425 		drp->ch.chunk_flags = 0;
11426 		drp->trunc_len = htons(0);
11427 	}
11428 	if (bad_crc) {
11429 		drp->ch.chunk_flags |= SCTP_BADCRC;
11430 	}
11431 	chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11432 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11433 	chk->sent = SCTP_DATAGRAM_UNSENT;
11434 	chk->snd_count = 0;
11435 	if (net) {
11436 		/* we should hit here */
11437 		chk->whoTo = net;
11438 		atomic_add_int(&chk->whoTo->ref_count, 1);
11439 	} else {
11440 		chk->whoTo = NULL;
11441 	}
11442 	chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11443 	chk->rec.chunk_id.can_take_data = 1;
11444 	drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11445 	drp->ch.chunk_length = htons(chk->send_size);
11446 	spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11447 	if (spc < 0) {
11448 		spc = 0;
11449 	}
11450 	drp->bottle_bw = htonl(spc);
11451 	if (asoc->my_rwnd) {
11452 		drp->current_onq = htonl(asoc->size_on_reasm_queue +
11453 		    asoc->size_on_all_streams +
11454 		    asoc->my_rwnd_control_len +
11455 		    stcb->sctp_socket->so_rcv.sb_cc);
11456 	} else {
11457 		/*-
11458 		 * If my rwnd is 0, possibly from mbuf depletion as well as
11459 		 * space used, tell the peer there is NO space aka onq == bw
11460 		 */
11461 		drp->current_onq = htonl(spc);
11462 	}
11463 	drp->reserved = 0;
11464 	datap = drp->data;
11465 	m_copydata(m, iphlen, len, (caddr_t)datap);
11466 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11467 	asoc->ctrl_queue_cnt++;
11468 }
11469 
11470 void
11471 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override)
11472 {
11473 	struct sctp_association *asoc;
11474 	struct sctp_cwr_chunk *cwr;
11475 	struct sctp_tmit_chunk *chk;
11476 
11477 	asoc = &stcb->asoc;
11478 	SCTP_TCB_LOCK_ASSERT(stcb);
11479 	if (net == NULL) {
11480 		return;
11481 	}
11482 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11483 		if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) {
11484 			/*
11485 			 * found a previous CWR queued to same destination
11486 			 * update it if needed
11487 			 */
11488 			uint32_t ctsn;
11489 
11490 			cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11491 			ctsn = ntohl(cwr->tsn);
11492 			if (SCTP_TSN_GT(high_tsn, ctsn)) {
11493 				cwr->tsn = htonl(high_tsn);
11494 			}
11495 			if (override & SCTP_CWR_REDUCE_OVERRIDE) {
11496 				/* Make sure override is carried */
11497 				cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE;
11498 			}
11499 			return;
11500 		}
11501 	}
11502 	sctp_alloc_a_chunk(stcb, chk);
11503 	if (chk == NULL) {
11504 		return;
11505 	}
11506 	chk->copy_by_ref = 0;
11507 	chk->rec.chunk_id.id = SCTP_ECN_CWR;
11508 	chk->rec.chunk_id.can_take_data = 1;
11509 	chk->asoc = &stcb->asoc;
11510 	chk->send_size = sizeof(struct sctp_cwr_chunk);
11511 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11512 	if (chk->data == NULL) {
11513 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11514 		return;
11515 	}
11516 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11517 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11518 	chk->sent = SCTP_DATAGRAM_UNSENT;
11519 	chk->snd_count = 0;
11520 	chk->whoTo = net;
11521 	atomic_add_int(&chk->whoTo->ref_count, 1);
11522 	cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11523 	cwr->ch.chunk_type = SCTP_ECN_CWR;
11524 	cwr->ch.chunk_flags = override;
11525 	cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11526 	cwr->tsn = htonl(high_tsn);
11527 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11528 	asoc->ctrl_queue_cnt++;
11529 }
11530 
11531 void
11532 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
11533     int number_entries, uint16_t * list,
11534     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11535 {
11536 	int len, old_len, i;
11537 	struct sctp_stream_reset_out_request *req_out;
11538 	struct sctp_chunkhdr *ch;
11539 
11540 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11541 
11542 
11543 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11544 
11545 	/* get to new offset for the param. */
11546 	req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11547 	/* now how long will this param be? */
11548 	len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11549 	req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11550 	req_out->ph.param_length = htons(len);
11551 	req_out->request_seq = htonl(seq);
11552 	req_out->response_seq = htonl(resp_seq);
11553 	req_out->send_reset_at_tsn = htonl(last_sent);
11554 	if (number_entries) {
11555 		for (i = 0; i < number_entries; i++) {
11556 			req_out->list_of_streams[i] = htons(list[i]);
11557 		}
11558 	}
11559 	if (SCTP_SIZE32(len) > len) {
11560 		/*-
11561 		 * Need to worry about the pad we may end up adding to the
11562 		 * end. This is easy since the struct is either aligned to 4
11563 		 * bytes or 2 bytes off.
11564 		 */
11565 		req_out->list_of_streams[number_entries] = 0;
11566 	}
11567 	/* now fix the chunk length */
11568 	ch->chunk_length = htons(len + old_len);
11569 	chk->book_size = len + old_len;
11570 	chk->book_size_scale = 0;
11571 	chk->send_size = SCTP_SIZE32(chk->book_size);
11572 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11573 	return;
11574 }
11575 
11576 
11577 void
11578 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11579     int number_entries, uint16_t * list,
11580     uint32_t seq)
11581 {
11582 	int len, old_len, i;
11583 	struct sctp_stream_reset_in_request *req_in;
11584 	struct sctp_chunkhdr *ch;
11585 
11586 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11587 
11588 
11589 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11590 
11591 	/* get to new offset for the param. */
11592 	req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11593 	/* now how long will this param be? */
11594 	len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11595 	req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11596 	req_in->ph.param_length = htons(len);
11597 	req_in->request_seq = htonl(seq);
11598 	if (number_entries) {
11599 		for (i = 0; i < number_entries; i++) {
11600 			req_in->list_of_streams[i] = htons(list[i]);
11601 		}
11602 	}
11603 	if (SCTP_SIZE32(len) > len) {
11604 		/*-
11605 		 * Need to worry about the pad we may end up adding to the
11606 		 * end. This is easy since the struct is either aligned to 4
11607 		 * bytes or 2 bytes off.
11608 		 */
11609 		req_in->list_of_streams[number_entries] = 0;
11610 	}
11611 	/* now fix the chunk length */
11612 	ch->chunk_length = htons(len + old_len);
11613 	chk->book_size = len + old_len;
11614 	chk->book_size_scale = 0;
11615 	chk->send_size = SCTP_SIZE32(chk->book_size);
11616 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11617 	return;
11618 }
11619 
11620 
11621 void
11622 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11623     uint32_t seq)
11624 {
11625 	int len, old_len;
11626 	struct sctp_stream_reset_tsn_request *req_tsn;
11627 	struct sctp_chunkhdr *ch;
11628 
11629 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11630 
11631 
11632 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11633 
11634 	/* get to new offset for the param. */
11635 	req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11636 	/* now how long will this param be? */
11637 	len = sizeof(struct sctp_stream_reset_tsn_request);
11638 	req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11639 	req_tsn->ph.param_length = htons(len);
11640 	req_tsn->request_seq = htonl(seq);
11641 
11642 	/* now fix the chunk length */
11643 	ch->chunk_length = htons(len + old_len);
11644 	chk->send_size = len + old_len;
11645 	chk->book_size = SCTP_SIZE32(chk->send_size);
11646 	chk->book_size_scale = 0;
11647 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11648 	return;
11649 }
11650 
11651 void
11652 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11653     uint32_t resp_seq, uint32_t result)
11654 {
11655 	int len, old_len;
11656 	struct sctp_stream_reset_response *resp;
11657 	struct sctp_chunkhdr *ch;
11658 
11659 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11660 
11661 
11662 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11663 
11664 	/* get to new offset for the param. */
11665 	resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11666 	/* now how long will this param be? */
11667 	len = sizeof(struct sctp_stream_reset_response);
11668 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11669 	resp->ph.param_length = htons(len);
11670 	resp->response_seq = htonl(resp_seq);
11671 	resp->result = ntohl(result);
11672 
11673 	/* now fix the chunk length */
11674 	ch->chunk_length = htons(len + old_len);
11675 	chk->book_size = len + old_len;
11676 	chk->book_size_scale = 0;
11677 	chk->send_size = SCTP_SIZE32(chk->book_size);
11678 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11679 	return;
11680 
11681 }
11682 
11683 
11684 void
11685 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11686     uint32_t resp_seq, uint32_t result,
11687     uint32_t send_una, uint32_t recv_next)
11688 {
11689 	int len, old_len;
11690 	struct sctp_stream_reset_response_tsn *resp;
11691 	struct sctp_chunkhdr *ch;
11692 
11693 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11694 
11695 
11696 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11697 
11698 	/* get to new offset for the param. */
11699 	resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11700 	/* now how long will this param be? */
11701 	len = sizeof(struct sctp_stream_reset_response_tsn);
11702 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11703 	resp->ph.param_length = htons(len);
11704 	resp->response_seq = htonl(resp_seq);
11705 	resp->result = htonl(result);
11706 	resp->senders_next_tsn = htonl(send_una);
11707 	resp->receivers_next_tsn = htonl(recv_next);
11708 
11709 	/* now fix the chunk length */
11710 	ch->chunk_length = htons(len + old_len);
11711 	chk->book_size = len + old_len;
11712 	chk->send_size = SCTP_SIZE32(chk->book_size);
11713 	chk->book_size_scale = 0;
11714 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11715 	return;
11716 }
11717 
11718 static void
11719 sctp_add_an_out_stream(struct sctp_tmit_chunk *chk,
11720     uint32_t seq,
11721     uint16_t adding)
11722 {
11723 	int len, old_len;
11724 	struct sctp_chunkhdr *ch;
11725 	struct sctp_stream_reset_add_strm *addstr;
11726 
11727 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11728 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11729 
11730 	/* get to new offset for the param. */
11731 	addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11732 	/* now how long will this param be? */
11733 	len = sizeof(struct sctp_stream_reset_add_strm);
11734 
11735 	/* Fill it out. */
11736 	addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_OUT_STREAMS);
11737 	addstr->ph.param_length = htons(len);
11738 	addstr->request_seq = htonl(seq);
11739 	addstr->number_of_streams = htons(adding);
11740 	addstr->reserved = 0;
11741 
11742 	/* now fix the chunk length */
11743 	ch->chunk_length = htons(len + old_len);
11744 	chk->send_size = len + old_len;
11745 	chk->book_size = SCTP_SIZE32(chk->send_size);
11746 	chk->book_size_scale = 0;
11747 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11748 	return;
11749 }
11750 
11751 static void
11752 sctp_add_an_in_stream(struct sctp_tmit_chunk *chk,
11753     uint32_t seq,
11754     uint16_t adding)
11755 {
11756 	int len, old_len;
11757 	struct sctp_chunkhdr *ch;
11758 	struct sctp_stream_reset_add_strm *addstr;
11759 
11760 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11761 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11762 
11763 	/* get to new offset for the param. */
11764 	addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11765 	/* now how long will this param be? */
11766 	len = sizeof(struct sctp_stream_reset_add_strm);
11767 	/* Fill it out. */
11768 	addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_IN_STREAMS);
11769 	addstr->ph.param_length = htons(len);
11770 	addstr->request_seq = htonl(seq);
11771 	addstr->number_of_streams = htons(adding);
11772 	addstr->reserved = 0;
11773 
11774 	/* now fix the chunk length */
11775 	ch->chunk_length = htons(len + old_len);
11776 	chk->send_size = len + old_len;
11777 	chk->book_size = SCTP_SIZE32(chk->send_size);
11778 	chk->book_size_scale = 0;
11779 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11780 	return;
11781 }
11782 
11783 
11784 
11785 int
11786 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11787     int number_entries, uint16_t * list,
11788     uint8_t send_out_req,
11789     uint8_t send_in_req,
11790     uint8_t send_tsn_req,
11791     uint8_t add_stream,
11792     uint16_t adding_o,
11793     uint16_t adding_i, uint8_t peer_asked
11794 )
11795 {
11796 
11797 	struct sctp_association *asoc;
11798 	struct sctp_tmit_chunk *chk;
11799 	struct sctp_chunkhdr *ch;
11800 	uint32_t seq;
11801 
11802 	asoc = &stcb->asoc;
11803 	if (asoc->stream_reset_outstanding) {
11804 		/*-
11805 		 * Already one pending, must get ACK back to clear the flag.
11806 		 */
11807 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11808 		return (EBUSY);
11809 	}
11810 	if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
11811 	    (add_stream == 0)) {
11812 		/* nothing to do */
11813 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11814 		return (EINVAL);
11815 	}
11816 	if (send_tsn_req && (send_out_req || send_in_req)) {
11817 		/* error, can't do that */
11818 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11819 		return (EINVAL);
11820 	}
11821 	sctp_alloc_a_chunk(stcb, chk);
11822 	if (chk == NULL) {
11823 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11824 		return (ENOMEM);
11825 	}
11826 	chk->copy_by_ref = 0;
11827 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11828 	chk->rec.chunk_id.can_take_data = 0;
11829 	chk->asoc = &stcb->asoc;
11830 	chk->book_size = sizeof(struct sctp_chunkhdr);
11831 	chk->send_size = SCTP_SIZE32(chk->book_size);
11832 	chk->book_size_scale = 0;
11833 
11834 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11835 	if (chk->data == NULL) {
11836 		sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
11837 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11838 		return (ENOMEM);
11839 	}
11840 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11841 
11842 	/* setup chunk parameters */
11843 	chk->sent = SCTP_DATAGRAM_UNSENT;
11844 	chk->snd_count = 0;
11845 	if (stcb->asoc.alternate) {
11846 		chk->whoTo = stcb->asoc.alternate;
11847 	} else {
11848 		chk->whoTo = stcb->asoc.primary_destination;
11849 	}
11850 	atomic_add_int(&chk->whoTo->ref_count, 1);
11851 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11852 	ch->chunk_type = SCTP_STREAM_RESET;
11853 	ch->chunk_flags = 0;
11854 	ch->chunk_length = htons(chk->book_size);
11855 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11856 
11857 	seq = stcb->asoc.str_reset_seq_out;
11858 	if (send_out_req) {
11859 		sctp_add_stream_reset_out(chk, number_entries, list,
11860 		    seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1));
11861 		asoc->stream_reset_out_is_outstanding = 1;
11862 		seq++;
11863 		asoc->stream_reset_outstanding++;
11864 	}
11865 	if ((add_stream & 1) &&
11866 	    ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < adding_o)) {
11867 		/* Need to allocate more */
11868 		struct sctp_stream_out *oldstream;
11869 		struct sctp_stream_queue_pending *sp, *nsp;
11870 		int i;
11871 
11872 		oldstream = stcb->asoc.strmout;
11873 		/* get some more */
11874 		SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
11875 		    ((stcb->asoc.streamoutcnt + adding_o) * sizeof(struct sctp_stream_out)),
11876 		    SCTP_M_STRMO);
11877 		if (stcb->asoc.strmout == NULL) {
11878 			uint8_t x;
11879 
11880 			stcb->asoc.strmout = oldstream;
11881 			/* Turn off the bit */
11882 			x = add_stream & 0xfe;
11883 			add_stream = x;
11884 			goto skip_stuff;
11885 		}
11886 		/*
11887 		 * Ok now we proceed with copying the old out stuff and
11888 		 * initializing the new stuff.
11889 		 */
11890 		SCTP_TCB_SEND_LOCK(stcb);
11891 		stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 0, 1);
11892 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11893 			TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
11894 			stcb->asoc.strmout[i].next_sequence_sent = oldstream[i].next_sequence_sent;
11895 			stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
11896 			stcb->asoc.strmout[i].stream_no = i;
11897 			stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], &oldstream[i]);
11898 			/* now anything on those queues? */
11899 			TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) {
11900 				TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
11901 				TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
11902 			}
11903 			/* Now move assoc pointers too */
11904 			if (stcb->asoc.last_out_stream == &oldstream[i]) {
11905 				stcb->asoc.last_out_stream = &stcb->asoc.strmout[i];
11906 			}
11907 			if (stcb->asoc.locked_on_sending == &oldstream[i]) {
11908 				stcb->asoc.locked_on_sending = &stcb->asoc.strmout[i];
11909 			}
11910 		}
11911 		/* now the new streams */
11912 		stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
11913 		for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + adding_o); i++) {
11914 			stcb->asoc.strmout[i].next_sequence_sent = 0x0;
11915 			TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
11916 			stcb->asoc.strmout[i].stream_no = i;
11917 			stcb->asoc.strmout[i].last_msg_incomplete = 0;
11918 			stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], NULL);
11919 		}
11920 		stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + adding_o;
11921 		SCTP_FREE(oldstream, SCTP_M_STRMO);
11922 		SCTP_TCB_SEND_UNLOCK(stcb);
11923 	}
11924 skip_stuff:
11925 	if ((add_stream & 1) && (adding_o > 0)) {
11926 		asoc->strm_pending_add_size = adding_o;
11927 		asoc->peer_req_out = peer_asked;
11928 		sctp_add_an_out_stream(chk, seq, adding_o);
11929 		seq++;
11930 		asoc->stream_reset_outstanding++;
11931 	}
11932 	if ((add_stream & 2) && (adding_i > 0)) {
11933 		sctp_add_an_in_stream(chk, seq, adding_i);
11934 		seq++;
11935 		asoc->stream_reset_outstanding++;
11936 	}
11937 	if (send_in_req) {
11938 		sctp_add_stream_reset_in(chk, number_entries, list, seq);
11939 		seq++;
11940 		asoc->stream_reset_outstanding++;
11941 	}
11942 	if (send_tsn_req) {
11943 		sctp_add_stream_reset_tsn(chk, seq);
11944 		asoc->stream_reset_outstanding++;
11945 	}
11946 	asoc->str_reset = chk;
11947 	/* insert the chunk for sending */
11948 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11949 	    chk,
11950 	    sctp_next);
11951 	asoc->ctrl_queue_cnt++;
11952 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11953 	return (0);
11954 }
11955 
11956 void
11957 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11958     struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11959 {
11960 	/*-
11961 	 * Formulate the abort message, and send it back down.
11962 	 */
11963 	struct mbuf *o_pak;
11964 	struct mbuf *mout;
11965 	struct sctp_abort_msg *abm;
11966 	struct ip *iph;
11967 	struct udphdr *udp;
11968 	int iphlen_out, len;
11969 
11970 #ifdef INET
11971 	struct ip *iph_out;
11972 
11973 #endif
11974 #ifdef INET6
11975 	struct ip6_hdr *ip6, *ip6_out;
11976 
11977 #endif
11978 
11979 	/* don't respond to ABORT with ABORT */
11980 	if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11981 		if (err_cause)
11982 			sctp_m_freem(err_cause);
11983 		return;
11984 	}
11985 	iph = mtod(m, struct ip *);
11986 	switch (iph->ip_v) {
11987 #ifdef INET
11988 	case IPVERSION:
11989 		len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11990 		break;
11991 #endif
11992 #ifdef INET6
11993 	case IPV6_VERSION >> 4:
11994 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11995 		break;
11996 #endif
11997 	default:
11998 		if (err_cause) {
11999 			sctp_m_freem(err_cause);
12000 		}
12001 		return;
12002 	}
12003 	if (port) {
12004 		len += sizeof(struct udphdr);
12005 	}
12006 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
12007 	if (mout == NULL) {
12008 		if (err_cause) {
12009 			sctp_m_freem(err_cause);
12010 		}
12011 		return;
12012 	}
12013 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
12014 	SCTP_BUF_LEN(mout) = len;
12015 	SCTP_BUF_NEXT(mout) = err_cause;
12016 	if (m->m_flags & M_FLOWID) {
12017 		mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
12018 		mout->m_flags |= M_FLOWID;
12019 	}
12020 #ifdef INET
12021 	iph_out = NULL;
12022 #endif
12023 #ifdef INET6
12024 	ip6_out = NULL;
12025 #endif
12026 	switch (iph->ip_v) {
12027 #ifdef INET
12028 	case IPVERSION:
12029 		iph_out = mtod(mout, struct ip *);
12030 
12031 		/* Fill in the IP header for the ABORT */
12032 		iph_out->ip_v = IPVERSION;
12033 		iph_out->ip_hl = (sizeof(struct ip) / 4);
12034 		iph_out->ip_tos = (u_char)0;
12035 		iph_out->ip_id = 0;
12036 		iph_out->ip_off = 0;
12037 		iph_out->ip_ttl = MAXTTL;
12038 		if (port) {
12039 			iph_out->ip_p = IPPROTO_UDP;
12040 		} else {
12041 			iph_out->ip_p = IPPROTO_SCTP;
12042 		}
12043 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
12044 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
12045 		/* let IP layer calculate this */
12046 		iph_out->ip_sum = 0;
12047 
12048 		iphlen_out = sizeof(*iph_out);
12049 		abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
12050 		break;
12051 #endif
12052 #ifdef INET6
12053 	case IPV6_VERSION >> 4:
12054 		ip6 = (struct ip6_hdr *)iph;
12055 		ip6_out = mtod(mout, struct ip6_hdr *);
12056 
12057 		/* Fill in the IP6 header for the ABORT */
12058 		ip6_out->ip6_flow = ip6->ip6_flow;
12059 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
12060 		if (port) {
12061 			ip6_out->ip6_nxt = IPPROTO_UDP;
12062 		} else {
12063 			ip6_out->ip6_nxt = IPPROTO_SCTP;
12064 		}
12065 		ip6_out->ip6_src = ip6->ip6_dst;
12066 		ip6_out->ip6_dst = ip6->ip6_src;
12067 
12068 		iphlen_out = sizeof(*ip6_out);
12069 		abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
12070 		break;
12071 #endif				/* INET6 */
12072 	default:
12073 		/* Currently not supported */
12074 		sctp_m_freem(mout);
12075 		return;
12076 	}
12077 
12078 	udp = (struct udphdr *)abm;
12079 	if (port) {
12080 		if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
12081 			sctp_m_freem(mout);
12082 			return;
12083 		}
12084 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
12085 		udp->uh_dport = port;
12086 		/* set udp->uh_ulen later */
12087 		udp->uh_sum = 0;
12088 		iphlen_out += sizeof(struct udphdr);
12089 		abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
12090 	}
12091 	abm->sh.src_port = sh->dest_port;
12092 	abm->sh.dest_port = sh->src_port;
12093 	abm->sh.checksum = 0;
12094 	if (vtag == 0) {
12095 		abm->sh.v_tag = sh->v_tag;
12096 		abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
12097 	} else {
12098 		abm->sh.v_tag = htonl(vtag);
12099 		abm->msg.ch.chunk_flags = 0;
12100 	}
12101 	abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
12102 
12103 	if (err_cause) {
12104 		struct mbuf *m_tmp = err_cause;
12105 		int err_len = 0;
12106 
12107 		/* get length of the err_cause chain */
12108 		while (m_tmp != NULL) {
12109 			err_len += SCTP_BUF_LEN(m_tmp);
12110 			m_tmp = SCTP_BUF_NEXT(m_tmp);
12111 		}
12112 		len = SCTP_BUF_LEN(mout) + err_len;
12113 		if (err_len % 4) {
12114 			/* need pad at end of chunk */
12115 			uint32_t cpthis = 0;
12116 			int padlen;
12117 
12118 			padlen = 4 - (len % 4);
12119 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
12120 			len += padlen;
12121 		}
12122 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
12123 	} else {
12124 		len = SCTP_BUF_LEN(mout);
12125 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
12126 	}
12127 
12128 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12129 		/* no mbuf's */
12130 		sctp_m_freem(mout);
12131 		return;
12132 	}
12133 #ifdef INET
12134 	if (iph_out != NULL) {
12135 		sctp_route_t ro;
12136 		int ret;
12137 
12138 		/* zap the stack pointer to the route */
12139 		bzero(&ro, sizeof ro);
12140 		if (port) {
12141 			udp->uh_ulen = htons(len - sizeof(struct ip));
12142 			if (V_udp_cksum) {
12143 				udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12144 			} else {
12145 				udp->uh_sum = 0;
12146 			}
12147 		}
12148 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
12149 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
12150 		/* set IPv4 length */
12151 		iph_out->ip_len = len;
12152 		/* out it goes */
12153 #ifdef  SCTP_PACKET_LOGGING
12154 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12155 			sctp_packet_log(mout, len);
12156 #endif
12157 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
12158 		if (port) {
12159 #if defined(SCTP_WITH_NO_CSUM)
12160 			SCTP_STAT_INCR(sctps_sendnocrc);
12161 #else
12162 			abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
12163 			SCTP_STAT_INCR(sctps_sendswcrc);
12164 #endif
12165 			if (V_udp_cksum) {
12166 				SCTP_ENABLE_UDP_CSUM(o_pak);
12167 			}
12168 		} else {
12169 #if defined(SCTP_WITH_NO_CSUM)
12170 			SCTP_STAT_INCR(sctps_sendnocrc);
12171 #else
12172 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
12173 			mout->m_pkthdr.csum_data = 0;
12174 			SCTP_STAT_INCR(sctps_sendhwcrc);
12175 #endif
12176 		}
12177 		SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
12178 
12179 		/* Free the route if we got one back */
12180 		if (ro.ro_rt)
12181 			RTFREE(ro.ro_rt);
12182 	}
12183 #endif
12184 #ifdef INET6
12185 	if (ip6_out != NULL) {
12186 		struct route_in6 ro;
12187 		int ret;
12188 		struct ifnet *ifp = NULL;
12189 
12190 		/* zap the stack pointer to the route */
12191 		bzero(&ro, sizeof(ro));
12192 		if (port) {
12193 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
12194 		}
12195 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
12196 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
12197 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
12198 #ifdef  SCTP_PACKET_LOGGING
12199 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12200 			sctp_packet_log(mout, len);
12201 #endif
12202 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
12203 		if (port) {
12204 #if defined(SCTP_WITH_NO_CSUM)
12205 			SCTP_STAT_INCR(sctps_sendnocrc);
12206 #else
12207 			abm->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
12208 			SCTP_STAT_INCR(sctps_sendswcrc);
12209 #endif
12210 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
12211 				udp->uh_sum = 0xffff;
12212 			}
12213 		} else {
12214 #if defined(SCTP_WITH_NO_CSUM)
12215 			SCTP_STAT_INCR(sctps_sendnocrc);
12216 #else
12217 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
12218 			mout->m_pkthdr.csum_data = 0;
12219 			SCTP_STAT_INCR(sctps_sendhwcrc);
12220 #endif
12221 		}
12222 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
12223 
12224 		/* Free the route if we got one back */
12225 		if (ro.ro_rt)
12226 			RTFREE(ro.ro_rt);
12227 	}
12228 #endif
12229 	SCTP_STAT_INCR(sctps_sendpackets);
12230 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12231 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12232 }
12233 
12234 void
12235 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
12236     uint32_t vrf_id, uint16_t port)
12237 {
12238 	struct mbuf *o_pak;
12239 	struct sctphdr *sh, *sh_out;
12240 	struct sctp_chunkhdr *ch;
12241 	struct ip *iph;
12242 	struct udphdr *udp = NULL;
12243 	struct mbuf *mout;
12244 	int iphlen_out, len;
12245 
12246 #ifdef INET
12247 	struct ip *iph_out;
12248 
12249 #endif
12250 #ifdef INET6
12251 	struct ip6_hdr *ip6, *ip6_out;
12252 
12253 #endif
12254 
12255 	iph = mtod(m, struct ip *);
12256 	sh = (struct sctphdr *)((caddr_t)iph + iphlen);
12257 	switch (iph->ip_v) {
12258 #ifdef INET
12259 	case IPVERSION:
12260 		len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
12261 		break;
12262 #endif
12263 #ifdef INET6
12264 	case IPV6_VERSION >> 4:
12265 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
12266 		break;
12267 #endif
12268 	default:
12269 		if (scm) {
12270 			sctp_m_freem(scm);
12271 		}
12272 		return;
12273 	}
12274 	if (port) {
12275 		len += sizeof(struct udphdr);
12276 	}
12277 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
12278 	if (mout == NULL) {
12279 		if (scm) {
12280 			sctp_m_freem(scm);
12281 		}
12282 		return;
12283 	}
12284 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
12285 	SCTP_BUF_LEN(mout) = len;
12286 	SCTP_BUF_NEXT(mout) = scm;
12287 	if (m->m_flags & M_FLOWID) {
12288 		mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
12289 		mout->m_flags |= M_FLOWID;
12290 	}
12291 #ifdef INET
12292 	iph_out = NULL;
12293 #endif
12294 #ifdef INET6
12295 	ip6_out = NULL;
12296 #endif
12297 	switch (iph->ip_v) {
12298 #ifdef INET
12299 	case IPVERSION:
12300 		iph_out = mtod(mout, struct ip *);
12301 
12302 		/* Fill in the IP header for the ABORT */
12303 		iph_out->ip_v = IPVERSION;
12304 		iph_out->ip_hl = (sizeof(struct ip) / 4);
12305 		iph_out->ip_tos = (u_char)0;
12306 		iph_out->ip_id = 0;
12307 		iph_out->ip_off = 0;
12308 		iph_out->ip_ttl = MAXTTL;
12309 		if (port) {
12310 			iph_out->ip_p = IPPROTO_UDP;
12311 		} else {
12312 			iph_out->ip_p = IPPROTO_SCTP;
12313 		}
12314 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
12315 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
12316 		/* let IP layer calculate this */
12317 		iph_out->ip_sum = 0;
12318 
12319 		iphlen_out = sizeof(struct ip);
12320 		sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
12321 		break;
12322 #endif
12323 #ifdef INET6
12324 	case IPV6_VERSION >> 4:
12325 		ip6 = (struct ip6_hdr *)iph;
12326 		ip6_out = mtod(mout, struct ip6_hdr *);
12327 
12328 		/* Fill in the IP6 header for the ABORT */
12329 		ip6_out->ip6_flow = ip6->ip6_flow;
12330 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
12331 		if (port) {
12332 			ip6_out->ip6_nxt = IPPROTO_UDP;
12333 		} else {
12334 			ip6_out->ip6_nxt = IPPROTO_SCTP;
12335 		}
12336 		ip6_out->ip6_src = ip6->ip6_dst;
12337 		ip6_out->ip6_dst = ip6->ip6_src;
12338 
12339 		iphlen_out = sizeof(struct ip6_hdr);
12340 		sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
12341 		break;
12342 #endif				/* INET6 */
12343 	default:
12344 		/* Currently not supported */
12345 		sctp_m_freem(mout);
12346 		return;
12347 	}
12348 
12349 	udp = (struct udphdr *)sh_out;
12350 	if (port) {
12351 		if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
12352 			sctp_m_freem(mout);
12353 			return;
12354 		}
12355 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
12356 		udp->uh_dport = port;
12357 		/* set udp->uh_ulen later */
12358 		udp->uh_sum = 0;
12359 		iphlen_out += sizeof(struct udphdr);
12360 		sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
12361 	}
12362 	sh_out->src_port = sh->dest_port;
12363 	sh_out->dest_port = sh->src_port;
12364 	sh_out->v_tag = vtag;
12365 	sh_out->checksum = 0;
12366 
12367 	ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
12368 	ch->chunk_type = SCTP_OPERATION_ERROR;
12369 	ch->chunk_flags = 0;
12370 
12371 	if (scm) {
12372 		struct mbuf *m_tmp = scm;
12373 		int cause_len = 0;
12374 
12375 		/* get length of the err_cause chain */
12376 		while (m_tmp != NULL) {
12377 			cause_len += SCTP_BUF_LEN(m_tmp);
12378 			m_tmp = SCTP_BUF_NEXT(m_tmp);
12379 		}
12380 		len = SCTP_BUF_LEN(mout) + cause_len;
12381 		if (cause_len % 4) {
12382 			/* need pad at end of chunk */
12383 			uint32_t cpthis = 0;
12384 			int padlen;
12385 
12386 			padlen = 4 - (len % 4);
12387 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
12388 			len += padlen;
12389 		}
12390 		ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
12391 	} else {
12392 		len = SCTP_BUF_LEN(mout);
12393 		ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
12394 	}
12395 
12396 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12397 		/* no mbuf's */
12398 		sctp_m_freem(mout);
12399 		return;
12400 	}
12401 #ifdef INET
12402 	if (iph_out != NULL) {
12403 		sctp_route_t ro;
12404 		int ret;
12405 
12406 		/* zap the stack pointer to the route */
12407 		bzero(&ro, sizeof ro);
12408 		if (port) {
12409 			udp->uh_ulen = htons(len - sizeof(struct ip));
12410 			if (V_udp_cksum) {
12411 				udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12412 			} else {
12413 				udp->uh_sum = 0;
12414 			}
12415 		}
12416 		/* set IPv4 length */
12417 		iph_out->ip_len = len;
12418 		/* out it goes */
12419 #ifdef  SCTP_PACKET_LOGGING
12420 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12421 			sctp_packet_log(mout, len);
12422 #endif
12423 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
12424 		if (port) {
12425 #if defined(SCTP_WITH_NO_CSUM)
12426 			SCTP_STAT_INCR(sctps_sendnocrc);
12427 #else
12428 			sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
12429 			SCTP_STAT_INCR(sctps_sendswcrc);
12430 #endif
12431 			if (V_udp_cksum) {
12432 				SCTP_ENABLE_UDP_CSUM(o_pak);
12433 			}
12434 		} else {
12435 #if defined(SCTP_WITH_NO_CSUM)
12436 			SCTP_STAT_INCR(sctps_sendnocrc);
12437 #else
12438 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
12439 			mout->m_pkthdr.csum_data = 0;
12440 			SCTP_STAT_INCR(sctps_sendhwcrc);
12441 #endif
12442 		}
12443 		SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
12444 
12445 		/* Free the route if we got one back */
12446 		if (ro.ro_rt)
12447 			RTFREE(ro.ro_rt);
12448 	}
12449 #endif
12450 #ifdef INET6
12451 	if (ip6_out != NULL) {
12452 		struct route_in6 ro;
12453 		int ret;
12454 		struct ifnet *ifp = NULL;
12455 
12456 		/* zap the stack pointer to the route */
12457 		bzero(&ro, sizeof(ro));
12458 		if (port) {
12459 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
12460 		}
12461 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
12462 #ifdef  SCTP_PACKET_LOGGING
12463 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12464 			sctp_packet_log(mout, len);
12465 #endif
12466 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
12467 		if (port) {
12468 #if defined(SCTP_WITH_NO_CSUM)
12469 			SCTP_STAT_INCR(sctps_sendnocrc);
12470 #else
12471 			sh_out->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
12472 			SCTP_STAT_INCR(sctps_sendswcrc);
12473 #endif
12474 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
12475 				udp->uh_sum = 0xffff;
12476 			}
12477 		} else {
12478 #if defined(SCTP_WITH_NO_CSUM)
12479 			SCTP_STAT_INCR(sctps_sendnocrc);
12480 #else
12481 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
12482 			mout->m_pkthdr.csum_data = 0;
12483 			SCTP_STAT_INCR(sctps_sendhwcrc);
12484 #endif
12485 		}
12486 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
12487 
12488 		/* Free the route if we got one back */
12489 		if (ro.ro_rt)
12490 			RTFREE(ro.ro_rt);
12491 	}
12492 #endif
12493 	SCTP_STAT_INCR(sctps_sendpackets);
12494 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12495 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12496 }
12497 
12498 static struct mbuf *
12499 sctp_copy_resume(struct uio *uio,
12500     int max_send_len,
12501     int user_marks_eor,
12502     int *error,
12503     uint32_t * sndout,
12504     struct mbuf **new_tail)
12505 {
12506 	struct mbuf *m;
12507 
12508 	m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
12509 	    (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
12510 	if (m == NULL) {
12511 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12512 		*error = ENOMEM;
12513 	} else {
12514 		*sndout = m_length(m, NULL);
12515 		*new_tail = m_last(m);
12516 	}
12517 	return (m);
12518 }
12519 
12520 static int
12521 sctp_copy_one(struct sctp_stream_queue_pending *sp,
12522     struct uio *uio,
12523     int resv_upfront)
12524 {
12525 	int left;
12526 
12527 	left = sp->length;
12528 	sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
12529 	    resv_upfront, 0);
12530 	if (sp->data == NULL) {
12531 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12532 		return (ENOMEM);
12533 	}
12534 	sp->tail_mbuf = m_last(sp->data);
12535 	return (0);
12536 }
12537 
12538 
12539 
12540 static struct sctp_stream_queue_pending *
12541 sctp_copy_it_in(struct sctp_tcb *stcb,
12542     struct sctp_association *asoc,
12543     struct sctp_sndrcvinfo *srcv,
12544     struct uio *uio,
12545     struct sctp_nets *net,
12546     int max_send_len,
12547     int user_marks_eor,
12548     int *error)
12549 {
12550 	/*-
12551 	 * This routine must be very careful in its work. Protocol
12552 	 * processing is up and running so care must be taken to spl...()
12553 	 * when you need to do something that may effect the stcb/asoc. The
12554 	 * sb is locked however. When data is copied the protocol processing
12555 	 * should be enabled since this is a slower operation...
12556 	 */
12557 	struct sctp_stream_queue_pending *sp = NULL;
12558 	int resv_in_first;
12559 
12560 	*error = 0;
12561 	/* Now can we send this? */
12562 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12563 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12564 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12565 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12566 		/* got data while shutting down */
12567 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12568 		*error = ECONNRESET;
12569 		goto out_now;
12570 	}
12571 	sctp_alloc_a_strmoq(stcb, sp);
12572 	if (sp == NULL) {
12573 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12574 		*error = ENOMEM;
12575 		goto out_now;
12576 	}
12577 	sp->act_flags = 0;
12578 	sp->sender_all_done = 0;
12579 	sp->sinfo_flags = srcv->sinfo_flags;
12580 	sp->timetolive = srcv->sinfo_timetolive;
12581 	sp->ppid = srcv->sinfo_ppid;
12582 	sp->context = srcv->sinfo_context;
12583 	sp->strseq = 0;
12584 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12585 
12586 	sp->stream = srcv->sinfo_stream;
12587 	sp->length = min(uio->uio_resid, max_send_len);
12588 	if ((sp->length == (uint32_t) uio->uio_resid) &&
12589 	    ((user_marks_eor == 0) ||
12590 	    (srcv->sinfo_flags & SCTP_EOF) ||
12591 	    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12592 		sp->msg_is_complete = 1;
12593 	} else {
12594 		sp->msg_is_complete = 0;
12595 	}
12596 	sp->sender_all_done = 0;
12597 	sp->some_taken = 0;
12598 	sp->put_last_out = 0;
12599 	resv_in_first = sizeof(struct sctp_data_chunk);
12600 	sp->data = sp->tail_mbuf = NULL;
12601 	if (sp->length == 0) {
12602 		*error = 0;
12603 		goto skip_copy;
12604 	}
12605 	if (srcv->sinfo_keynumber_valid) {
12606 		sp->auth_keyid = srcv->sinfo_keynumber;
12607 	} else {
12608 		sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12609 	}
12610 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12611 		sctp_auth_key_acquire(stcb, sp->auth_keyid);
12612 		sp->holds_key_ref = 1;
12613 	}
12614 	*error = sctp_copy_one(sp, uio, resv_in_first);
12615 skip_copy:
12616 	if (*error) {
12617 		sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
12618 		sp = NULL;
12619 	} else {
12620 		if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12621 			sp->net = net;
12622 			atomic_add_int(&sp->net->ref_count, 1);
12623 		} else {
12624 			sp->net = NULL;
12625 		}
12626 		sctp_set_prsctp_policy(sp);
12627 	}
12628 out_now:
12629 	return (sp);
12630 }
12631 
12632 
12633 int
12634 sctp_sosend(struct socket *so,
12635     struct sockaddr *addr,
12636     struct uio *uio,
12637     struct mbuf *top,
12638     struct mbuf *control,
12639     int flags,
12640     struct thread *p
12641 )
12642 {
12643 	int error, use_sndinfo = 0;
12644 	struct sctp_sndrcvinfo sndrcvninfo;
12645 	struct sockaddr *addr_to_use;
12646 
12647 #if defined(INET) && defined(INET6)
12648 	struct sockaddr_in sin;
12649 
12650 #endif
12651 
12652 	if (control) {
12653 		/* process cmsg snd/rcv info (maybe a assoc-id) */
12654 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&sndrcvninfo, control,
12655 		    sizeof(sndrcvninfo))) {
12656 			/* got one */
12657 			use_sndinfo = 1;
12658 		}
12659 	}
12660 	addr_to_use = addr;
12661 #if defined(INET) && defined(INET6)
12662 	if ((addr) && (addr->sa_family == AF_INET6)) {
12663 		struct sockaddr_in6 *sin6;
12664 
12665 		sin6 = (struct sockaddr_in6 *)addr;
12666 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12667 			in6_sin6_2_sin(&sin, sin6);
12668 			addr_to_use = (struct sockaddr *)&sin;
12669 		}
12670 	}
12671 #endif
12672 	error = sctp_lower_sosend(so, addr_to_use, uio, top,
12673 	    control,
12674 	    flags,
12675 	    use_sndinfo ? &sndrcvninfo : NULL
12676 	    ,p
12677 	    );
12678 	return (error);
12679 }
12680 
12681 
12682 int
12683 sctp_lower_sosend(struct socket *so,
12684     struct sockaddr *addr,
12685     struct uio *uio,
12686     struct mbuf *i_pak,
12687     struct mbuf *control,
12688     int flags,
12689     struct sctp_sndrcvinfo *srcv
12690     ,
12691     struct thread *p
12692 )
12693 {
12694 	unsigned int sndlen = 0, max_len;
12695 	int error, len;
12696 	struct mbuf *top = NULL;
12697 	int queue_only = 0, queue_only_for_init = 0;
12698 	int free_cnt_applied = 0;
12699 	int un_sent;
12700 	int now_filled = 0;
12701 	unsigned int inqueue_bytes = 0;
12702 	struct sctp_block_entry be;
12703 	struct sctp_inpcb *inp;
12704 	struct sctp_tcb *stcb = NULL;
12705 	struct timeval now;
12706 	struct sctp_nets *net;
12707 	struct sctp_association *asoc;
12708 	struct sctp_inpcb *t_inp;
12709 	int user_marks_eor;
12710 	int create_lock_applied = 0;
12711 	int nagle_applies = 0;
12712 	int some_on_control = 0;
12713 	int got_all_of_the_send = 0;
12714 	int hold_tcblock = 0;
12715 	int non_blocking = 0;
12716 	uint32_t local_add_more, local_soresv = 0;
12717 	uint16_t port;
12718 	uint16_t sinfo_flags;
12719 	sctp_assoc_t sinfo_assoc_id;
12720 
12721 	error = 0;
12722 	net = NULL;
12723 	stcb = NULL;
12724 	asoc = NULL;
12725 
12726 	t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12727 	if (inp == NULL) {
12728 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12729 		error = EINVAL;
12730 		if (i_pak) {
12731 			SCTP_RELEASE_PKT(i_pak);
12732 		}
12733 		return (error);
12734 	}
12735 	if ((uio == NULL) && (i_pak == NULL)) {
12736 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12737 		return (EINVAL);
12738 	}
12739 	user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12740 	atomic_add_int(&inp->total_sends, 1);
12741 	if (uio) {
12742 		if (uio->uio_resid < 0) {
12743 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12744 			return (EINVAL);
12745 		}
12746 		sndlen = uio->uio_resid;
12747 	} else {
12748 		top = SCTP_HEADER_TO_CHAIN(i_pak);
12749 		sndlen = SCTP_HEADER_LEN(i_pak);
12750 	}
12751 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
12752 	    addr,
12753 	    sndlen);
12754 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12755 	    (inp->sctp_socket->so_qlimit)) {
12756 		/* The listener can NOT send */
12757 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12758 		error = ENOTCONN;
12759 		goto out_unlocked;
12760 	}
12761 	/**
12762 	 * Pre-screen address, if one is given the sin-len
12763 	 * must be set correctly!
12764 	 */
12765 	if (addr) {
12766 		union sctp_sockstore *raddr = (union sctp_sockstore *)addr;
12767 
12768 		switch (raddr->sa.sa_family) {
12769 #if defined(INET)
12770 		case AF_INET:
12771 			if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) {
12772 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12773 				error = EINVAL;
12774 				goto out_unlocked;
12775 			}
12776 			port = raddr->sin.sin_port;
12777 			break;
12778 #endif
12779 #if defined(INET6)
12780 		case AF_INET6:
12781 			if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) {
12782 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12783 				error = EINVAL;
12784 				goto out_unlocked;
12785 			}
12786 			port = raddr->sin6.sin6_port;
12787 			break;
12788 #endif
12789 		default:
12790 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAFNOSUPPORT);
12791 			error = EAFNOSUPPORT;
12792 			goto out_unlocked;
12793 		}
12794 	} else
12795 		port = 0;
12796 
12797 	if (srcv) {
12798 		sinfo_flags = srcv->sinfo_flags;
12799 		sinfo_assoc_id = srcv->sinfo_assoc_id;
12800 		if (INVALID_SINFO_FLAG(sinfo_flags) ||
12801 		    PR_SCTP_INVALID_POLICY(sinfo_flags)) {
12802 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12803 			error = EINVAL;
12804 			goto out_unlocked;
12805 		}
12806 		if (srcv->sinfo_flags)
12807 			SCTP_STAT_INCR(sctps_sends_with_flags);
12808 	} else {
12809 		sinfo_flags = inp->def_send.sinfo_flags;
12810 		sinfo_assoc_id = inp->def_send.sinfo_assoc_id;
12811 	}
12812 	if (sinfo_flags & SCTP_SENDALL) {
12813 		/* its a sendall */
12814 		error = sctp_sendall(inp, uio, top, srcv);
12815 		top = NULL;
12816 		goto out_unlocked;
12817 	}
12818 	if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) {
12819 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12820 		error = EINVAL;
12821 		goto out_unlocked;
12822 	}
12823 	/* now we must find the assoc */
12824 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12825 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12826 		SCTP_INP_RLOCK(inp);
12827 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
12828 		if (stcb) {
12829 			SCTP_TCB_LOCK(stcb);
12830 			hold_tcblock = 1;
12831 		}
12832 		SCTP_INP_RUNLOCK(inp);
12833 	} else if (sinfo_assoc_id) {
12834 		stcb = sctp_findassociation_ep_asocid(inp, sinfo_assoc_id, 0);
12835 	} else if (addr) {
12836 		/*-
12837 		 * Since we did not use findep we must
12838 		 * increment it, and if we don't find a tcb
12839 		 * decrement it.
12840 		 */
12841 		SCTP_INP_WLOCK(inp);
12842 		SCTP_INP_INCR_REF(inp);
12843 		SCTP_INP_WUNLOCK(inp);
12844 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12845 		if (stcb == NULL) {
12846 			SCTP_INP_WLOCK(inp);
12847 			SCTP_INP_DECR_REF(inp);
12848 			SCTP_INP_WUNLOCK(inp);
12849 		} else {
12850 			hold_tcblock = 1;
12851 		}
12852 	}
12853 	if ((stcb == NULL) && (addr)) {
12854 		/* Possible implicit send? */
12855 		SCTP_ASOC_CREATE_LOCK(inp);
12856 		create_lock_applied = 1;
12857 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12858 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12859 			/* Should I really unlock ? */
12860 			SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12861 			error = EINVAL;
12862 			goto out_unlocked;
12863 
12864 		}
12865 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12866 		    (addr->sa_family == AF_INET6)) {
12867 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12868 			error = EINVAL;
12869 			goto out_unlocked;
12870 		}
12871 		SCTP_INP_WLOCK(inp);
12872 		SCTP_INP_INCR_REF(inp);
12873 		SCTP_INP_WUNLOCK(inp);
12874 		/* With the lock applied look again */
12875 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12876 		if ((stcb == NULL) && (control != NULL) && (port > 0)) {
12877 			stcb = sctp_findassociation_cmsgs(&t_inp, port, control, &net, &error);
12878 		}
12879 		if (stcb == NULL) {
12880 			SCTP_INP_WLOCK(inp);
12881 			SCTP_INP_DECR_REF(inp);
12882 			SCTP_INP_WUNLOCK(inp);
12883 		} else {
12884 			hold_tcblock = 1;
12885 		}
12886 		if (error) {
12887 			goto out_unlocked;
12888 		}
12889 		if (t_inp != inp) {
12890 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12891 			error = ENOTCONN;
12892 			goto out_unlocked;
12893 		}
12894 	}
12895 	if (stcb == NULL) {
12896 		if (addr == NULL) {
12897 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12898 			error = ENOENT;
12899 			goto out_unlocked;
12900 		} else {
12901 			/* We must go ahead and start the INIT process */
12902 			uint32_t vrf_id;
12903 
12904 			if ((sinfo_flags & SCTP_ABORT) ||
12905 			    ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) {
12906 				/*-
12907 				 * User asks to abort a non-existant assoc,
12908 				 * or EOF a non-existant assoc with no data
12909 				 */
12910 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12911 				error = ENOENT;
12912 				goto out_unlocked;
12913 			}
12914 			/* get an asoc/stcb struct */
12915 			vrf_id = inp->def_vrf_id;
12916 #ifdef INVARIANTS
12917 			if (create_lock_applied == 0) {
12918 				panic("Error, should hold create lock and I don't?");
12919 			}
12920 #endif
12921 			stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
12922 			    p
12923 			    );
12924 			if (stcb == NULL) {
12925 				/* Error is setup for us in the call */
12926 				goto out_unlocked;
12927 			}
12928 			if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
12929 				stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
12930 				/*
12931 				 * Set the connected flag so we can queue
12932 				 * data
12933 				 */
12934 				soisconnecting(so);
12935 			}
12936 			hold_tcblock = 1;
12937 			if (create_lock_applied) {
12938 				SCTP_ASOC_CREATE_UNLOCK(inp);
12939 				create_lock_applied = 0;
12940 			} else {
12941 				SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12942 			}
12943 			/*
12944 			 * Turn on queue only flag to prevent data from
12945 			 * being sent
12946 			 */
12947 			queue_only = 1;
12948 			asoc = &stcb->asoc;
12949 			SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12950 			(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12951 
12952 			/* initialize authentication params for the assoc */
12953 			sctp_initialize_auth_params(inp, stcb);
12954 
12955 			if (control) {
12956 				if (sctp_process_cmsgs_for_init(stcb, control, &error)) {
12957 					sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_7);
12958 					hold_tcblock = 0;
12959 					stcb = NULL;
12960 					goto out_unlocked;
12961 				}
12962 			}
12963 			/* out with the INIT */
12964 			queue_only_for_init = 1;
12965 			/*-
12966 			 * we may want to dig in after this call and adjust the MTU
12967 			 * value. It defaulted to 1500 (constant) but the ro
12968 			 * structure may now have an update and thus we may need to
12969 			 * change it BEFORE we append the message.
12970 			 */
12971 		}
12972 	} else
12973 		asoc = &stcb->asoc;
12974 	if (srcv == NULL)
12975 		srcv = (struct sctp_sndrcvinfo *)&asoc->def_send;
12976 	if (srcv->sinfo_flags & SCTP_ADDR_OVER) {
12977 		if (addr)
12978 			net = sctp_findnet(stcb, addr);
12979 		else
12980 			net = NULL;
12981 		if ((net == NULL) ||
12982 		    ((port != 0) && (port != stcb->rport))) {
12983 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12984 			error = EINVAL;
12985 			goto out_unlocked;
12986 		}
12987 	} else {
12988 		if (stcb->asoc.alternate) {
12989 			net = stcb->asoc.alternate;
12990 		} else {
12991 			net = stcb->asoc.primary_destination;
12992 		}
12993 	}
12994 	atomic_add_int(&stcb->total_sends, 1);
12995 	/* Keep the stcb from being freed under our feet */
12996 	atomic_add_int(&asoc->refcnt, 1);
12997 	free_cnt_applied = 1;
12998 
12999 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
13000 		if (sndlen > asoc->smallest_mtu) {
13001 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
13002 			error = EMSGSIZE;
13003 			goto out_unlocked;
13004 		}
13005 	}
13006 	if (SCTP_SO_IS_NBIO(so)
13007 	    || (flags & MSG_NBIO)
13008 	    ) {
13009 		non_blocking = 1;
13010 	}
13011 	/* would we block? */
13012 	if (non_blocking) {
13013 		if (hold_tcblock == 0) {
13014 			SCTP_TCB_LOCK(stcb);
13015 			hold_tcblock = 1;
13016 		}
13017 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13018 		if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
13019 		    (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13020 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
13021 			if (sndlen > SCTP_SB_LIMIT_SND(so))
13022 				error = EMSGSIZE;
13023 			else
13024 				error = EWOULDBLOCK;
13025 			goto out_unlocked;
13026 		}
13027 		stcb->asoc.sb_send_resv += sndlen;
13028 		SCTP_TCB_UNLOCK(stcb);
13029 		hold_tcblock = 0;
13030 	} else {
13031 		atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
13032 	}
13033 	local_soresv = sndlen;
13034 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13035 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13036 		error = ECONNRESET;
13037 		goto out_unlocked;
13038 	}
13039 	if (create_lock_applied) {
13040 		SCTP_ASOC_CREATE_UNLOCK(inp);
13041 		create_lock_applied = 0;
13042 	}
13043 	if (asoc->stream_reset_outstanding) {
13044 		/*
13045 		 * Can't queue any data while stream reset is underway.
13046 		 */
13047 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
13048 		error = EAGAIN;
13049 		goto out_unlocked;
13050 	}
13051 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
13052 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
13053 		queue_only = 1;
13054 	}
13055 	/* we are now done with all control */
13056 	if (control) {
13057 		sctp_m_freem(control);
13058 		control = NULL;
13059 	}
13060 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
13061 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
13062 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
13063 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
13064 		if (srcv->sinfo_flags & SCTP_ABORT) {
13065 			;
13066 		} else {
13067 			SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13068 			error = ECONNRESET;
13069 			goto out_unlocked;
13070 		}
13071 	}
13072 	/* Ok, we will attempt a msgsnd :> */
13073 	if (p) {
13074 		p->td_ru.ru_msgsnd++;
13075 	}
13076 	/* Are we aborting? */
13077 	if (srcv->sinfo_flags & SCTP_ABORT) {
13078 		struct mbuf *mm;
13079 		int tot_demand, tot_out = 0, max_out;
13080 
13081 		SCTP_STAT_INCR(sctps_sends_with_abort);
13082 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
13083 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
13084 			/* It has to be up before we abort */
13085 			/* how big is the user initiated abort? */
13086 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13087 			error = EINVAL;
13088 			goto out;
13089 		}
13090 		if (hold_tcblock) {
13091 			SCTP_TCB_UNLOCK(stcb);
13092 			hold_tcblock = 0;
13093 		}
13094 		if (top) {
13095 			struct mbuf *cntm = NULL;
13096 
13097 			mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_WAIT, 1, MT_DATA);
13098 			if (sndlen != 0) {
13099 				for (cntm = top; cntm; cntm = SCTP_BUF_NEXT(cntm)) {
13100 					tot_out += SCTP_BUF_LEN(cntm);
13101 				}
13102 			}
13103 		} else {
13104 			/* Must fit in a MTU */
13105 			tot_out = sndlen;
13106 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
13107 			if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
13108 				/* To big */
13109 				SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
13110 				error = EMSGSIZE;
13111 				goto out;
13112 			}
13113 			mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
13114 		}
13115 		if (mm == NULL) {
13116 			SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13117 			error = ENOMEM;
13118 			goto out;
13119 		}
13120 		max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
13121 		max_out -= sizeof(struct sctp_abort_msg);
13122 		if (tot_out > max_out) {
13123 			tot_out = max_out;
13124 		}
13125 		if (mm) {
13126 			struct sctp_paramhdr *ph;
13127 
13128 			/* now move forward the data pointer */
13129 			ph = mtod(mm, struct sctp_paramhdr *);
13130 			ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
13131 			ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
13132 			ph++;
13133 			SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
13134 			if (top == NULL) {
13135 				error = uiomove((caddr_t)ph, (int)tot_out, uio);
13136 				if (error) {
13137 					/*-
13138 					 * Here if we can't get his data we
13139 					 * still abort we just don't get to
13140 					 * send the users note :-0
13141 					 */
13142 					sctp_m_freem(mm);
13143 					mm = NULL;
13144 				}
13145 			} else {
13146 				if (sndlen != 0) {
13147 					SCTP_BUF_NEXT(mm) = top;
13148 				}
13149 			}
13150 		}
13151 		if (hold_tcblock == 0) {
13152 			SCTP_TCB_LOCK(stcb);
13153 		}
13154 		atomic_add_int(&stcb->asoc.refcnt, -1);
13155 		free_cnt_applied = 0;
13156 		/* release this lock, otherwise we hang on ourselves */
13157 		sctp_abort_an_association(stcb->sctp_ep, stcb,
13158 		    SCTP_RESPONSE_TO_USER_REQ,
13159 		    mm, SCTP_SO_LOCKED);
13160 		/* now relock the stcb so everything is sane */
13161 		hold_tcblock = 0;
13162 		stcb = NULL;
13163 		/*
13164 		 * In this case top is already chained to mm avoid double
13165 		 * free, since we free it below if top != NULL and driver
13166 		 * would free it after sending the packet out
13167 		 */
13168 		if (sndlen != 0) {
13169 			top = NULL;
13170 		}
13171 		goto out_unlocked;
13172 	}
13173 	/* Calculate the maximum we can send */
13174 	inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13175 	if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13176 		if (non_blocking) {
13177 			/* we already checked for non-blocking above. */
13178 			max_len = sndlen;
13179 		} else {
13180 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13181 		}
13182 	} else {
13183 		max_len = 0;
13184 	}
13185 	if (hold_tcblock) {
13186 		SCTP_TCB_UNLOCK(stcb);
13187 		hold_tcblock = 0;
13188 	}
13189 	/* Is the stream no. valid? */
13190 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
13191 		/* Invalid stream number */
13192 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13193 		error = EINVAL;
13194 		goto out_unlocked;
13195 	}
13196 	if (asoc->strmout == NULL) {
13197 		/* huh? software error */
13198 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
13199 		error = EFAULT;
13200 		goto out_unlocked;
13201 	}
13202 	/* Unless E_EOR mode is on, we must make a send FIT in one call. */
13203 	if ((user_marks_eor == 0) &&
13204 	    (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
13205 		/* It will NEVER fit */
13206 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
13207 		error = EMSGSIZE;
13208 		goto out_unlocked;
13209 	}
13210 	if ((uio == NULL) && user_marks_eor) {
13211 		/*-
13212 		 * We do not support eeor mode for
13213 		 * sending with mbuf chains (like sendfile).
13214 		 */
13215 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13216 		error = EINVAL;
13217 		goto out_unlocked;
13218 	}
13219 	if (user_marks_eor) {
13220 		local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
13221 	} else {
13222 		/*-
13223 		 * For non-eeor the whole message must fit in
13224 		 * the socket send buffer.
13225 		 */
13226 		local_add_more = sndlen;
13227 	}
13228 	len = 0;
13229 	if (non_blocking) {
13230 		goto skip_preblock;
13231 	}
13232 	if (((max_len <= local_add_more) &&
13233 	    (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
13234 	    (max_len == 0) ||
13235 	    ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13236 		/* No room right now ! */
13237 		SOCKBUF_LOCK(&so->so_snd);
13238 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13239 		while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
13240 		    ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13241 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
13242 			    (unsigned int)SCTP_SB_LIMIT_SND(so),
13243 			    inqueue_bytes,
13244 			    local_add_more,
13245 			    stcb->asoc.stream_queue_cnt,
13246 			    stcb->asoc.chunks_on_out_queue,
13247 			    SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
13248 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13249 				sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, asoc, sndlen);
13250 			}
13251 			be.error = 0;
13252 			stcb->block_entry = &be;
13253 			error = sbwait(&so->so_snd);
13254 			stcb->block_entry = NULL;
13255 			if (error || so->so_error || be.error) {
13256 				if (error == 0) {
13257 					if (so->so_error)
13258 						error = so->so_error;
13259 					if (be.error) {
13260 						error = be.error;
13261 					}
13262 				}
13263 				SOCKBUF_UNLOCK(&so->so_snd);
13264 				goto out_unlocked;
13265 			}
13266 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13267 				sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13268 				    asoc, stcb->asoc.total_output_queue_size);
13269 			}
13270 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13271 				goto out_unlocked;
13272 			}
13273 			inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13274 		}
13275 		if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13276 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13277 		} else {
13278 			max_len = 0;
13279 		}
13280 		SOCKBUF_UNLOCK(&so->so_snd);
13281 	}
13282 skip_preblock:
13283 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13284 		goto out_unlocked;
13285 	}
13286 	/*
13287 	 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
13288 	 * case NOTE: uio will be null when top/mbuf is passed
13289 	 */
13290 	if (sndlen == 0) {
13291 		if (srcv->sinfo_flags & SCTP_EOF) {
13292 			got_all_of_the_send = 1;
13293 			goto dataless_eof;
13294 		} else {
13295 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13296 			error = EINVAL;
13297 			goto out;
13298 		}
13299 	}
13300 	if (top == NULL) {
13301 		struct sctp_stream_queue_pending *sp;
13302 		struct sctp_stream_out *strm;
13303 		uint32_t sndout;
13304 
13305 		SCTP_TCB_SEND_LOCK(stcb);
13306 		if ((asoc->stream_locked) &&
13307 		    (asoc->stream_locked_on != srcv->sinfo_stream)) {
13308 			SCTP_TCB_SEND_UNLOCK(stcb);
13309 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13310 			error = EINVAL;
13311 			goto out;
13312 		}
13313 		SCTP_TCB_SEND_UNLOCK(stcb);
13314 
13315 		strm = &stcb->asoc.strmout[srcv->sinfo_stream];
13316 		if (strm->last_msg_incomplete == 0) {
13317 	do_a_copy_in:
13318 			sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error);
13319 			if ((sp == NULL) || (error)) {
13320 				goto out;
13321 			}
13322 			SCTP_TCB_SEND_LOCK(stcb);
13323 			if (sp->msg_is_complete) {
13324 				strm->last_msg_incomplete = 0;
13325 				asoc->stream_locked = 0;
13326 			} else {
13327 				/*
13328 				 * Just got locked to this guy in case of an
13329 				 * interrupt.
13330 				 */
13331 				strm->last_msg_incomplete = 1;
13332 				asoc->stream_locked = 1;
13333 				asoc->stream_locked_on = srcv->sinfo_stream;
13334 				sp->sender_all_done = 0;
13335 			}
13336 			sctp_snd_sb_alloc(stcb, sp->length);
13337 			atomic_add_int(&asoc->stream_queue_cnt, 1);
13338 			if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
13339 				sp->strseq = strm->next_sequence_sent;
13340 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
13341 					sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
13342 					    (uintptr_t) stcb, sp->length,
13343 					    (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
13344 				}
13345 				strm->next_sequence_sent++;
13346 			} else {
13347 				SCTP_STAT_INCR(sctps_sends_with_unord);
13348 			}
13349 			TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
13350 			stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp, 1);
13351 			SCTP_TCB_SEND_UNLOCK(stcb);
13352 		} else {
13353 			SCTP_TCB_SEND_LOCK(stcb);
13354 			sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
13355 			SCTP_TCB_SEND_UNLOCK(stcb);
13356 			if (sp == NULL) {
13357 				/* ???? Huh ??? last msg is gone */
13358 #ifdef INVARIANTS
13359 				panic("Warning: Last msg marked incomplete, yet nothing left?");
13360 #else
13361 				SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
13362 				strm->last_msg_incomplete = 0;
13363 #endif
13364 				goto do_a_copy_in;
13365 
13366 			}
13367 		}
13368 		while (uio->uio_resid > 0) {
13369 			/* How much room do we have? */
13370 			struct mbuf *new_tail, *mm;
13371 
13372 			if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13373 				max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
13374 			else
13375 				max_len = 0;
13376 
13377 			if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
13378 			    (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
13379 			    (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
13380 				sndout = 0;
13381 				new_tail = NULL;
13382 				if (hold_tcblock) {
13383 					SCTP_TCB_UNLOCK(stcb);
13384 					hold_tcblock = 0;
13385 				}
13386 				mm = sctp_copy_resume(uio, max_len, user_marks_eor, &error, &sndout, &new_tail);
13387 				if ((mm == NULL) || error) {
13388 					if (mm) {
13389 						sctp_m_freem(mm);
13390 					}
13391 					goto out;
13392 				}
13393 				/* Update the mbuf and count */
13394 				SCTP_TCB_SEND_LOCK(stcb);
13395 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13396 					/*
13397 					 * we need to get out. Peer probably
13398 					 * aborted.
13399 					 */
13400 					sctp_m_freem(mm);
13401 					if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
13402 						SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13403 						error = ECONNRESET;
13404 					}
13405 					SCTP_TCB_SEND_UNLOCK(stcb);
13406 					goto out;
13407 				}
13408 				if (sp->tail_mbuf) {
13409 					/* tack it to the end */
13410 					SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13411 					sp->tail_mbuf = new_tail;
13412 				} else {
13413 					/* A stolen mbuf */
13414 					sp->data = mm;
13415 					sp->tail_mbuf = new_tail;
13416 				}
13417 				sctp_snd_sb_alloc(stcb, sndout);
13418 				atomic_add_int(&sp->length, sndout);
13419 				len += sndout;
13420 
13421 				/* Did we reach EOR? */
13422 				if ((uio->uio_resid == 0) &&
13423 				    ((user_marks_eor == 0) ||
13424 				    (srcv->sinfo_flags & SCTP_EOF) ||
13425 				    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
13426 					sp->msg_is_complete = 1;
13427 				} else {
13428 					sp->msg_is_complete = 0;
13429 				}
13430 				SCTP_TCB_SEND_UNLOCK(stcb);
13431 			}
13432 			if (uio->uio_resid == 0) {
13433 				/* got it all? */
13434 				continue;
13435 			}
13436 			/* PR-SCTP? */
13437 			if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
13438 				/*
13439 				 * This is ugly but we must assure locking
13440 				 * order
13441 				 */
13442 				if (hold_tcblock == 0) {
13443 					SCTP_TCB_LOCK(stcb);
13444 					hold_tcblock = 1;
13445 				}
13446 				sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
13447 				inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13448 				if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13449 					max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13450 				else
13451 					max_len = 0;
13452 				if (max_len > 0) {
13453 					continue;
13454 				}
13455 				SCTP_TCB_UNLOCK(stcb);
13456 				hold_tcblock = 0;
13457 			}
13458 			/* wait for space now */
13459 			if (non_blocking) {
13460 				/* Non-blocking io in place out */
13461 				goto skip_out_eof;
13462 			}
13463 			/* What about the INIT, send it maybe */
13464 			if (queue_only_for_init) {
13465 				if (hold_tcblock == 0) {
13466 					SCTP_TCB_LOCK(stcb);
13467 					hold_tcblock = 1;
13468 				}
13469 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13470 					/* a collision took us forward? */
13471 					queue_only = 0;
13472 				} else {
13473 					sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13474 					SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
13475 					queue_only = 1;
13476 				}
13477 			}
13478 			if ((net->flight_size > net->cwnd) &&
13479 			    (asoc->sctp_cmt_on_off == 0)) {
13480 				SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13481 				queue_only = 1;
13482 			} else if (asoc->ifp_had_enobuf) {
13483 				SCTP_STAT_INCR(sctps_ifnomemqueued);
13484 				if (net->flight_size > (2 * net->mtu)) {
13485 					queue_only = 1;
13486 				}
13487 				asoc->ifp_had_enobuf = 0;
13488 			}
13489 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13490 			    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13491 			if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13492 			    (stcb->asoc.total_flight > 0) &&
13493 			    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13494 			    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13495 
13496 				/*-
13497 				 * Ok, Nagle is set on and we have data outstanding.
13498 				 * Don't send anything and let SACKs drive out the
13499 				 * data unless wen have a "full" segment to send.
13500 				 */
13501 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13502 					sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13503 				}
13504 				SCTP_STAT_INCR(sctps_naglequeued);
13505 				nagle_applies = 1;
13506 			} else {
13507 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13508 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13509 						sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13510 				}
13511 				SCTP_STAT_INCR(sctps_naglesent);
13512 				nagle_applies = 0;
13513 			}
13514 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13515 
13516 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13517 				    nagle_applies, un_sent);
13518 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13519 				    stcb->asoc.total_flight,
13520 				    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13521 			}
13522 			if (queue_only_for_init)
13523 				queue_only_for_init = 0;
13524 			if ((queue_only == 0) && (nagle_applies == 0)) {
13525 				/*-
13526 				 * need to start chunk output
13527 				 * before blocking.. note that if
13528 				 * a lock is already applied, then
13529 				 * the input via the net is happening
13530 				 * and I don't need to start output :-D
13531 				 */
13532 				if (hold_tcblock == 0) {
13533 					if (SCTP_TCB_TRYLOCK(stcb)) {
13534 						hold_tcblock = 1;
13535 						sctp_chunk_output(inp,
13536 						    stcb,
13537 						    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13538 					}
13539 				} else {
13540 					sctp_chunk_output(inp,
13541 					    stcb,
13542 					    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13543 				}
13544 				if (hold_tcblock == 1) {
13545 					SCTP_TCB_UNLOCK(stcb);
13546 					hold_tcblock = 0;
13547 				}
13548 			}
13549 			SOCKBUF_LOCK(&so->so_snd);
13550 			/*-
13551 			 * This is a bit strange, but I think it will
13552 			 * work. The total_output_queue_size is locked and
13553 			 * protected by the TCB_LOCK, which we just released.
13554 			 * There is a race that can occur between releasing it
13555 			 * above, and me getting the socket lock, where sacks
13556 			 * come in but we have not put the SB_WAIT on the
13557 			 * so_snd buffer to get the wakeup. After the LOCK
13558 			 * is applied the sack_processing will also need to
13559 			 * LOCK the so->so_snd to do the actual sowwakeup(). So
13560 			 * once we have the socket buffer lock if we recheck the
13561 			 * size we KNOW we will get to sleep safely with the
13562 			 * wakeup flag in place.
13563 			 */
13564 			if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
13565 			    min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13566 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13567 					sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13568 					    asoc, uio->uio_resid);
13569 				}
13570 				be.error = 0;
13571 				stcb->block_entry = &be;
13572 				error = sbwait(&so->so_snd);
13573 				stcb->block_entry = NULL;
13574 
13575 				if (error || so->so_error || be.error) {
13576 					if (error == 0) {
13577 						if (so->so_error)
13578 							error = so->so_error;
13579 						if (be.error) {
13580 							error = be.error;
13581 						}
13582 					}
13583 					SOCKBUF_UNLOCK(&so->so_snd);
13584 					goto out_unlocked;
13585 				}
13586 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13587 					sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13588 					    asoc, stcb->asoc.total_output_queue_size);
13589 				}
13590 			}
13591 			SOCKBUF_UNLOCK(&so->so_snd);
13592 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13593 				goto out_unlocked;
13594 			}
13595 		}
13596 		SCTP_TCB_SEND_LOCK(stcb);
13597 		if (sp) {
13598 			if (sp->msg_is_complete == 0) {
13599 				strm->last_msg_incomplete = 1;
13600 				asoc->stream_locked = 1;
13601 				asoc->stream_locked_on = srcv->sinfo_stream;
13602 			} else {
13603 				sp->sender_all_done = 1;
13604 				strm->last_msg_incomplete = 0;
13605 				asoc->stream_locked = 0;
13606 			}
13607 		} else {
13608 			SCTP_PRINTF("Huh no sp TSNH?\n");
13609 			strm->last_msg_incomplete = 0;
13610 			asoc->stream_locked = 0;
13611 		}
13612 		SCTP_TCB_SEND_UNLOCK(stcb);
13613 		if (uio->uio_resid == 0) {
13614 			got_all_of_the_send = 1;
13615 		}
13616 	} else {
13617 		/* We send in a 0, since we do NOT have any locks */
13618 		error = sctp_msg_append(stcb, net, top, srcv, 0);
13619 		top = NULL;
13620 		if (srcv->sinfo_flags & SCTP_EOF) {
13621 			/*
13622 			 * This should only happen for Panda for the mbuf
13623 			 * send case, which does NOT yet support EEOR mode.
13624 			 * Thus, we can just set this flag to do the proper
13625 			 * EOF handling.
13626 			 */
13627 			got_all_of_the_send = 1;
13628 		}
13629 	}
13630 	if (error) {
13631 		goto out;
13632 	}
13633 dataless_eof:
13634 	/* EOF thing ? */
13635 	if ((srcv->sinfo_flags & SCTP_EOF) &&
13636 	    (got_all_of_the_send == 1) &&
13637 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
13638 		int cnt;
13639 
13640 		SCTP_STAT_INCR(sctps_sends_with_eof);
13641 		error = 0;
13642 		if (hold_tcblock == 0) {
13643 			SCTP_TCB_LOCK(stcb);
13644 			hold_tcblock = 1;
13645 		}
13646 		cnt = sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED);
13647 		if (TAILQ_EMPTY(&asoc->send_queue) &&
13648 		    TAILQ_EMPTY(&asoc->sent_queue) &&
13649 		    (cnt == 0)) {
13650 			if (asoc->locked_on_sending) {
13651 				goto abort_anyway;
13652 			}
13653 			/* there is nothing queued to send, so I'm done... */
13654 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13655 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13656 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13657 				struct sctp_nets *netp;
13658 
13659 				if (stcb->asoc.alternate) {
13660 					netp = stcb->asoc.alternate;
13661 				} else {
13662 					netp = stcb->asoc.primary_destination;
13663 				}
13664 				/* only send SHUTDOWN the first time through */
13665 				sctp_send_shutdown(stcb, netp);
13666 				if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
13667 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13668 				}
13669 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
13670 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
13671 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13672 				    netp);
13673 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13674 				    asoc->primary_destination);
13675 			}
13676 		} else {
13677 			/*-
13678 			 * we still got (or just got) data to send, so set
13679 			 * SHUTDOWN_PENDING
13680 			 */
13681 			/*-
13682 			 * XXX sockets draft says that SCTP_EOF should be
13683 			 * sent with no data.  currently, we will allow user
13684 			 * data to be sent first and move to
13685 			 * SHUTDOWN-PENDING
13686 			 */
13687 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13688 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13689 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13690 				if (hold_tcblock == 0) {
13691 					SCTP_TCB_LOCK(stcb);
13692 					hold_tcblock = 1;
13693 				}
13694 				if (asoc->locked_on_sending) {
13695 					/* Locked to send out the data */
13696 					struct sctp_stream_queue_pending *sp;
13697 
13698 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
13699 					if (sp) {
13700 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
13701 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
13702 					}
13703 				}
13704 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
13705 				if (TAILQ_EMPTY(&asoc->send_queue) &&
13706 				    TAILQ_EMPTY(&asoc->sent_queue) &&
13707 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13708 			abort_anyway:
13709 					if (free_cnt_applied) {
13710 						atomic_add_int(&stcb->asoc.refcnt, -1);
13711 						free_cnt_applied = 0;
13712 					}
13713 					sctp_abort_an_association(stcb->sctp_ep, stcb,
13714 					    SCTP_RESPONSE_TO_USER_REQ,
13715 					    NULL, SCTP_SO_LOCKED);
13716 					/*
13717 					 * now relock the stcb so everything
13718 					 * is sane
13719 					 */
13720 					hold_tcblock = 0;
13721 					stcb = NULL;
13722 					goto out;
13723 				}
13724 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13725 				    asoc->primary_destination);
13726 				sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13727 			}
13728 		}
13729 	}
13730 skip_out_eof:
13731 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
13732 		some_on_control = 1;
13733 	}
13734 	if (queue_only_for_init) {
13735 		if (hold_tcblock == 0) {
13736 			SCTP_TCB_LOCK(stcb);
13737 			hold_tcblock = 1;
13738 		}
13739 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13740 			/* a collision took us forward? */
13741 			queue_only = 0;
13742 		} else {
13743 			sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13744 			SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13745 			queue_only = 1;
13746 		}
13747 	}
13748 	if ((net->flight_size > net->cwnd) &&
13749 	    (stcb->asoc.sctp_cmt_on_off == 0)) {
13750 		SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13751 		queue_only = 1;
13752 	} else if (asoc->ifp_had_enobuf) {
13753 		SCTP_STAT_INCR(sctps_ifnomemqueued);
13754 		if (net->flight_size > (2 * net->mtu)) {
13755 			queue_only = 1;
13756 		}
13757 		asoc->ifp_had_enobuf = 0;
13758 	}
13759 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13760 	    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13761 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13762 	    (stcb->asoc.total_flight > 0) &&
13763 	    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13764 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13765 		/*-
13766 		 * Ok, Nagle is set on and we have data outstanding.
13767 		 * Don't send anything and let SACKs drive out the
13768 		 * data unless wen have a "full" segment to send.
13769 		 */
13770 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13771 			sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13772 		}
13773 		SCTP_STAT_INCR(sctps_naglequeued);
13774 		nagle_applies = 1;
13775 	} else {
13776 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13777 			if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13778 				sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13779 		}
13780 		SCTP_STAT_INCR(sctps_naglesent);
13781 		nagle_applies = 0;
13782 	}
13783 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13784 		sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13785 		    nagle_applies, un_sent);
13786 		sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13787 		    stcb->asoc.total_flight,
13788 		    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13789 	}
13790 	if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13791 		/* we can attempt to send too. */
13792 		if (hold_tcblock == 0) {
13793 			/*
13794 			 * If there is activity recv'ing sacks no need to
13795 			 * send
13796 			 */
13797 			if (SCTP_TCB_TRYLOCK(stcb)) {
13798 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13799 				hold_tcblock = 1;
13800 			}
13801 		} else {
13802 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13803 		}
13804 	} else if ((queue_only == 0) &&
13805 		    (stcb->asoc.peers_rwnd == 0) &&
13806 	    (stcb->asoc.total_flight == 0)) {
13807 		/* We get to have a probe outstanding */
13808 		if (hold_tcblock == 0) {
13809 			hold_tcblock = 1;
13810 			SCTP_TCB_LOCK(stcb);
13811 		}
13812 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13813 	} else if (some_on_control) {
13814 		int num_out, reason, frag_point;
13815 
13816 		/* Here we do control only */
13817 		if (hold_tcblock == 0) {
13818 			hold_tcblock = 1;
13819 			SCTP_TCB_LOCK(stcb);
13820 		}
13821 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13822 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13823 		    &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13824 	}
13825 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13826 	    queue_only, stcb->asoc.peers_rwnd, un_sent,
13827 	    stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13828 	    stcb->asoc.total_output_queue_size, error);
13829 
13830 out:
13831 out_unlocked:
13832 
13833 	if (local_soresv && stcb) {
13834 		atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13835 	}
13836 	if (create_lock_applied) {
13837 		SCTP_ASOC_CREATE_UNLOCK(inp);
13838 	}
13839 	if ((stcb) && hold_tcblock) {
13840 		SCTP_TCB_UNLOCK(stcb);
13841 	}
13842 	if (stcb && free_cnt_applied) {
13843 		atomic_add_int(&stcb->asoc.refcnt, -1);
13844 	}
13845 #ifdef INVARIANTS
13846 	if (stcb) {
13847 		if (mtx_owned(&stcb->tcb_mtx)) {
13848 			panic("Leaving with tcb mtx owned?");
13849 		}
13850 		if (mtx_owned(&stcb->tcb_send_mtx)) {
13851 			panic("Leaving with tcb send mtx owned?");
13852 		}
13853 	}
13854 #endif
13855 #ifdef INVARIANTS
13856 	if (inp) {
13857 		sctp_validate_no_locks(inp);
13858 	} else {
13859 		printf("Warning - inp is NULL so cant validate locks\n");
13860 	}
13861 #endif
13862 	if (top) {
13863 		sctp_m_freem(top);
13864 	}
13865 	if (control) {
13866 		sctp_m_freem(control);
13867 	}
13868 	return (error);
13869 }
13870 
13871 
13872 /*
13873  * generate an AUTHentication chunk, if required
13874  */
13875 struct mbuf *
13876 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13877     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13878     struct sctp_tcb *stcb, uint8_t chunk)
13879 {
13880 	struct mbuf *m_auth;
13881 	struct sctp_auth_chunk *auth;
13882 	int chunk_len;
13883 	struct mbuf *cn;
13884 
13885 	if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13886 	    (stcb == NULL))
13887 		return (m);
13888 
13889 	/* sysctl disabled auth? */
13890 	if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13891 		return (m);
13892 
13893 	/* peer doesn't do auth... */
13894 	if (!stcb->asoc.peer_supports_auth) {
13895 		return (m);
13896 	}
13897 	/* does the requested chunk require auth? */
13898 	if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13899 		return (m);
13900 	}
13901 	m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13902 	if (m_auth == NULL) {
13903 		/* no mbuf's */
13904 		return (m);
13905 	}
13906 	/* reserve some space if this will be the first mbuf */
13907 	if (m == NULL)
13908 		SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13909 	/* fill in the AUTH chunk details */
13910 	auth = mtod(m_auth, struct sctp_auth_chunk *);
13911 	bzero(auth, sizeof(*auth));
13912 	auth->ch.chunk_type = SCTP_AUTHENTICATION;
13913 	auth->ch.chunk_flags = 0;
13914 	chunk_len = sizeof(*auth) +
13915 	    sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13916 	auth->ch.chunk_length = htons(chunk_len);
13917 	auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13918 	/* key id and hmac digest will be computed and filled in upon send */
13919 
13920 	/* save the offset where the auth was inserted into the chain */
13921 	*offset = 0;
13922 	for (cn = m; cn; cn = SCTP_BUF_NEXT(cn)) {
13923 		*offset += SCTP_BUF_LEN(cn);
13924 	}
13925 
13926 	/* update length and return pointer to the auth chunk */
13927 	SCTP_BUF_LEN(m_auth) = chunk_len;
13928 	m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13929 	if (auth_ret != NULL)
13930 		*auth_ret = auth;
13931 
13932 	return (m);
13933 }
13934 
13935 #ifdef INET6
13936 int
13937 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13938 {
13939 	struct nd_prefix *pfx = NULL;
13940 	struct nd_pfxrouter *pfxrtr = NULL;
13941 	struct sockaddr_in6 gw6;
13942 
13943 	if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13944 		return (0);
13945 
13946 	/* get prefix entry of address */
13947 	LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13948 		if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13949 			continue;
13950 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13951 		    &src6->sin6_addr, &pfx->ndpr_mask))
13952 			break;
13953 	}
13954 	/* no prefix entry in the prefix list */
13955 	if (pfx == NULL) {
13956 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13957 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13958 		return (0);
13959 	}
13960 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13961 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13962 
13963 	/* search installed gateway from prefix entry */
13964 	LIST_FOREACH(pfxrtr, &pfx->ndpr_advrtrs, pfr_entry) {
13965 		memset(&gw6, 0, sizeof(struct sockaddr_in6));
13966 		gw6.sin6_family = AF_INET6;
13967 		gw6.sin6_len = sizeof(struct sockaddr_in6);
13968 		memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13969 		    sizeof(struct in6_addr));
13970 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13971 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13972 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13973 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13974 		if (sctp_cmpaddr((struct sockaddr *)&gw6,
13975 		    ro->ro_rt->rt_gateway)) {
13976 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13977 			return (1);
13978 		}
13979 	}
13980 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13981 	return (0);
13982 }
13983 
13984 #endif
13985 
13986 int
13987 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13988 {
13989 #ifdef INET
13990 	struct sockaddr_in *sin, *mask;
13991 	struct ifaddr *ifa;
13992 	struct in_addr srcnetaddr, gwnetaddr;
13993 
13994 	if (ro == NULL || ro->ro_rt == NULL ||
13995 	    sifa->address.sa.sa_family != AF_INET) {
13996 		return (0);
13997 	}
13998 	ifa = (struct ifaddr *)sifa->ifa;
13999 	mask = (struct sockaddr_in *)(ifa->ifa_netmask);
14000 	sin = (struct sockaddr_in *)&sifa->address.sin;
14001 	srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
14002 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
14003 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
14004 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
14005 
14006 	sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
14007 	gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
14008 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
14009 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
14010 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
14011 	if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
14012 		return (1);
14013 	}
14014 #endif
14015 	return (0);
14016 }
14017