xref: /freebsd-src/sys/netinet/sctp_output.c (revision 1670a1c2a47d10ecccd001970b859caf93cd3b6e)
1 /*-
2  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $	 */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <netinet/sctp_os.h>
37 #include <sys/proc.h>
38 #include <netinet/sctp_var.h>
39 #include <netinet/sctp_sysctl.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctputil.h>
43 #include <netinet/sctp_output.h>
44 #include <netinet/sctp_uio.h>
45 #include <netinet/sctputil.h>
46 #include <netinet/sctp_auth.h>
47 #include <netinet/sctp_timer.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_indata.h>
50 #include <netinet/sctp_bsd_addr.h>
51 #include <netinet/sctp_input.h>
52 #include <netinet/sctp_crc32.h>
53 #include <netinet/udp.h>
54 #include <machine/in_cksum.h>
55 
56 
57 
58 #define SCTP_MAX_GAPS_INARRAY 4
59 struct sack_track {
60 	uint8_t right_edge;	/* mergable on the right edge */
61 	uint8_t left_edge;	/* mergable on the left edge */
62 	uint8_t num_entries;
63 	uint8_t spare;
64 	struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
65 };
66 
67 struct sack_track sack_array[256] = {
68 	{0, 0, 0, 0,		/* 0x00 */
69 		{{0, 0},
70 		{0, 0},
71 		{0, 0},
72 		{0, 0}
73 		}
74 	},
75 	{1, 0, 1, 0,		/* 0x01 */
76 		{{0, 0},
77 		{0, 0},
78 		{0, 0},
79 		{0, 0}
80 		}
81 	},
82 	{0, 0, 1, 0,		/* 0x02 */
83 		{{1, 1},
84 		{0, 0},
85 		{0, 0},
86 		{0, 0}
87 		}
88 	},
89 	{1, 0, 1, 0,		/* 0x03 */
90 		{{0, 1},
91 		{0, 0},
92 		{0, 0},
93 		{0, 0}
94 		}
95 	},
96 	{0, 0, 1, 0,		/* 0x04 */
97 		{{2, 2},
98 		{0, 0},
99 		{0, 0},
100 		{0, 0}
101 		}
102 	},
103 	{1, 0, 2, 0,		/* 0x05 */
104 		{{0, 0},
105 		{2, 2},
106 		{0, 0},
107 		{0, 0}
108 		}
109 	},
110 	{0, 0, 1, 0,		/* 0x06 */
111 		{{1, 2},
112 		{0, 0},
113 		{0, 0},
114 		{0, 0}
115 		}
116 	},
117 	{1, 0, 1, 0,		/* 0x07 */
118 		{{0, 2},
119 		{0, 0},
120 		{0, 0},
121 		{0, 0}
122 		}
123 	},
124 	{0, 0, 1, 0,		/* 0x08 */
125 		{{3, 3},
126 		{0, 0},
127 		{0, 0},
128 		{0, 0}
129 		}
130 	},
131 	{1, 0, 2, 0,		/* 0x09 */
132 		{{0, 0},
133 		{3, 3},
134 		{0, 0},
135 		{0, 0}
136 		}
137 	},
138 	{0, 0, 2, 0,		/* 0x0a */
139 		{{1, 1},
140 		{3, 3},
141 		{0, 0},
142 		{0, 0}
143 		}
144 	},
145 	{1, 0, 2, 0,		/* 0x0b */
146 		{{0, 1},
147 		{3, 3},
148 		{0, 0},
149 		{0, 0}
150 		}
151 	},
152 	{0, 0, 1, 0,		/* 0x0c */
153 		{{2, 3},
154 		{0, 0},
155 		{0, 0},
156 		{0, 0}
157 		}
158 	},
159 	{1, 0, 2, 0,		/* 0x0d */
160 		{{0, 0},
161 		{2, 3},
162 		{0, 0},
163 		{0, 0}
164 		}
165 	},
166 	{0, 0, 1, 0,		/* 0x0e */
167 		{{1, 3},
168 		{0, 0},
169 		{0, 0},
170 		{0, 0}
171 		}
172 	},
173 	{1, 0, 1, 0,		/* 0x0f */
174 		{{0, 3},
175 		{0, 0},
176 		{0, 0},
177 		{0, 0}
178 		}
179 	},
180 	{0, 0, 1, 0,		/* 0x10 */
181 		{{4, 4},
182 		{0, 0},
183 		{0, 0},
184 		{0, 0}
185 		}
186 	},
187 	{1, 0, 2, 0,		/* 0x11 */
188 		{{0, 0},
189 		{4, 4},
190 		{0, 0},
191 		{0, 0}
192 		}
193 	},
194 	{0, 0, 2, 0,		/* 0x12 */
195 		{{1, 1},
196 		{4, 4},
197 		{0, 0},
198 		{0, 0}
199 		}
200 	},
201 	{1, 0, 2, 0,		/* 0x13 */
202 		{{0, 1},
203 		{4, 4},
204 		{0, 0},
205 		{0, 0}
206 		}
207 	},
208 	{0, 0, 2, 0,		/* 0x14 */
209 		{{2, 2},
210 		{4, 4},
211 		{0, 0},
212 		{0, 0}
213 		}
214 	},
215 	{1, 0, 3, 0,		/* 0x15 */
216 		{{0, 0},
217 		{2, 2},
218 		{4, 4},
219 		{0, 0}
220 		}
221 	},
222 	{0, 0, 2, 0,		/* 0x16 */
223 		{{1, 2},
224 		{4, 4},
225 		{0, 0},
226 		{0, 0}
227 		}
228 	},
229 	{1, 0, 2, 0,		/* 0x17 */
230 		{{0, 2},
231 		{4, 4},
232 		{0, 0},
233 		{0, 0}
234 		}
235 	},
236 	{0, 0, 1, 0,		/* 0x18 */
237 		{{3, 4},
238 		{0, 0},
239 		{0, 0},
240 		{0, 0}
241 		}
242 	},
243 	{1, 0, 2, 0,		/* 0x19 */
244 		{{0, 0},
245 		{3, 4},
246 		{0, 0},
247 		{0, 0}
248 		}
249 	},
250 	{0, 0, 2, 0,		/* 0x1a */
251 		{{1, 1},
252 		{3, 4},
253 		{0, 0},
254 		{0, 0}
255 		}
256 	},
257 	{1, 0, 2, 0,		/* 0x1b */
258 		{{0, 1},
259 		{3, 4},
260 		{0, 0},
261 		{0, 0}
262 		}
263 	},
264 	{0, 0, 1, 0,		/* 0x1c */
265 		{{2, 4},
266 		{0, 0},
267 		{0, 0},
268 		{0, 0}
269 		}
270 	},
271 	{1, 0, 2, 0,		/* 0x1d */
272 		{{0, 0},
273 		{2, 4},
274 		{0, 0},
275 		{0, 0}
276 		}
277 	},
278 	{0, 0, 1, 0,		/* 0x1e */
279 		{{1, 4},
280 		{0, 0},
281 		{0, 0},
282 		{0, 0}
283 		}
284 	},
285 	{1, 0, 1, 0,		/* 0x1f */
286 		{{0, 4},
287 		{0, 0},
288 		{0, 0},
289 		{0, 0}
290 		}
291 	},
292 	{0, 0, 1, 0,		/* 0x20 */
293 		{{5, 5},
294 		{0, 0},
295 		{0, 0},
296 		{0, 0}
297 		}
298 	},
299 	{1, 0, 2, 0,		/* 0x21 */
300 		{{0, 0},
301 		{5, 5},
302 		{0, 0},
303 		{0, 0}
304 		}
305 	},
306 	{0, 0, 2, 0,		/* 0x22 */
307 		{{1, 1},
308 		{5, 5},
309 		{0, 0},
310 		{0, 0}
311 		}
312 	},
313 	{1, 0, 2, 0,		/* 0x23 */
314 		{{0, 1},
315 		{5, 5},
316 		{0, 0},
317 		{0, 0}
318 		}
319 	},
320 	{0, 0, 2, 0,		/* 0x24 */
321 		{{2, 2},
322 		{5, 5},
323 		{0, 0},
324 		{0, 0}
325 		}
326 	},
327 	{1, 0, 3, 0,		/* 0x25 */
328 		{{0, 0},
329 		{2, 2},
330 		{5, 5},
331 		{0, 0}
332 		}
333 	},
334 	{0, 0, 2, 0,		/* 0x26 */
335 		{{1, 2},
336 		{5, 5},
337 		{0, 0},
338 		{0, 0}
339 		}
340 	},
341 	{1, 0, 2, 0,		/* 0x27 */
342 		{{0, 2},
343 		{5, 5},
344 		{0, 0},
345 		{0, 0}
346 		}
347 	},
348 	{0, 0, 2, 0,		/* 0x28 */
349 		{{3, 3},
350 		{5, 5},
351 		{0, 0},
352 		{0, 0}
353 		}
354 	},
355 	{1, 0, 3, 0,		/* 0x29 */
356 		{{0, 0},
357 		{3, 3},
358 		{5, 5},
359 		{0, 0}
360 		}
361 	},
362 	{0, 0, 3, 0,		/* 0x2a */
363 		{{1, 1},
364 		{3, 3},
365 		{5, 5},
366 		{0, 0}
367 		}
368 	},
369 	{1, 0, 3, 0,		/* 0x2b */
370 		{{0, 1},
371 		{3, 3},
372 		{5, 5},
373 		{0, 0}
374 		}
375 	},
376 	{0, 0, 2, 0,		/* 0x2c */
377 		{{2, 3},
378 		{5, 5},
379 		{0, 0},
380 		{0, 0}
381 		}
382 	},
383 	{1, 0, 3, 0,		/* 0x2d */
384 		{{0, 0},
385 		{2, 3},
386 		{5, 5},
387 		{0, 0}
388 		}
389 	},
390 	{0, 0, 2, 0,		/* 0x2e */
391 		{{1, 3},
392 		{5, 5},
393 		{0, 0},
394 		{0, 0}
395 		}
396 	},
397 	{1, 0, 2, 0,		/* 0x2f */
398 		{{0, 3},
399 		{5, 5},
400 		{0, 0},
401 		{0, 0}
402 		}
403 	},
404 	{0, 0, 1, 0,		/* 0x30 */
405 		{{4, 5},
406 		{0, 0},
407 		{0, 0},
408 		{0, 0}
409 		}
410 	},
411 	{1, 0, 2, 0,		/* 0x31 */
412 		{{0, 0},
413 		{4, 5},
414 		{0, 0},
415 		{0, 0}
416 		}
417 	},
418 	{0, 0, 2, 0,		/* 0x32 */
419 		{{1, 1},
420 		{4, 5},
421 		{0, 0},
422 		{0, 0}
423 		}
424 	},
425 	{1, 0, 2, 0,		/* 0x33 */
426 		{{0, 1},
427 		{4, 5},
428 		{0, 0},
429 		{0, 0}
430 		}
431 	},
432 	{0, 0, 2, 0,		/* 0x34 */
433 		{{2, 2},
434 		{4, 5},
435 		{0, 0},
436 		{0, 0}
437 		}
438 	},
439 	{1, 0, 3, 0,		/* 0x35 */
440 		{{0, 0},
441 		{2, 2},
442 		{4, 5},
443 		{0, 0}
444 		}
445 	},
446 	{0, 0, 2, 0,		/* 0x36 */
447 		{{1, 2},
448 		{4, 5},
449 		{0, 0},
450 		{0, 0}
451 		}
452 	},
453 	{1, 0, 2, 0,		/* 0x37 */
454 		{{0, 2},
455 		{4, 5},
456 		{0, 0},
457 		{0, 0}
458 		}
459 	},
460 	{0, 0, 1, 0,		/* 0x38 */
461 		{{3, 5},
462 		{0, 0},
463 		{0, 0},
464 		{0, 0}
465 		}
466 	},
467 	{1, 0, 2, 0,		/* 0x39 */
468 		{{0, 0},
469 		{3, 5},
470 		{0, 0},
471 		{0, 0}
472 		}
473 	},
474 	{0, 0, 2, 0,		/* 0x3a */
475 		{{1, 1},
476 		{3, 5},
477 		{0, 0},
478 		{0, 0}
479 		}
480 	},
481 	{1, 0, 2, 0,		/* 0x3b */
482 		{{0, 1},
483 		{3, 5},
484 		{0, 0},
485 		{0, 0}
486 		}
487 	},
488 	{0, 0, 1, 0,		/* 0x3c */
489 		{{2, 5},
490 		{0, 0},
491 		{0, 0},
492 		{0, 0}
493 		}
494 	},
495 	{1, 0, 2, 0,		/* 0x3d */
496 		{{0, 0},
497 		{2, 5},
498 		{0, 0},
499 		{0, 0}
500 		}
501 	},
502 	{0, 0, 1, 0,		/* 0x3e */
503 		{{1, 5},
504 		{0, 0},
505 		{0, 0},
506 		{0, 0}
507 		}
508 	},
509 	{1, 0, 1, 0,		/* 0x3f */
510 		{{0, 5},
511 		{0, 0},
512 		{0, 0},
513 		{0, 0}
514 		}
515 	},
516 	{0, 0, 1, 0,		/* 0x40 */
517 		{{6, 6},
518 		{0, 0},
519 		{0, 0},
520 		{0, 0}
521 		}
522 	},
523 	{1, 0, 2, 0,		/* 0x41 */
524 		{{0, 0},
525 		{6, 6},
526 		{0, 0},
527 		{0, 0}
528 		}
529 	},
530 	{0, 0, 2, 0,		/* 0x42 */
531 		{{1, 1},
532 		{6, 6},
533 		{0, 0},
534 		{0, 0}
535 		}
536 	},
537 	{1, 0, 2, 0,		/* 0x43 */
538 		{{0, 1},
539 		{6, 6},
540 		{0, 0},
541 		{0, 0}
542 		}
543 	},
544 	{0, 0, 2, 0,		/* 0x44 */
545 		{{2, 2},
546 		{6, 6},
547 		{0, 0},
548 		{0, 0}
549 		}
550 	},
551 	{1, 0, 3, 0,		/* 0x45 */
552 		{{0, 0},
553 		{2, 2},
554 		{6, 6},
555 		{0, 0}
556 		}
557 	},
558 	{0, 0, 2, 0,		/* 0x46 */
559 		{{1, 2},
560 		{6, 6},
561 		{0, 0},
562 		{0, 0}
563 		}
564 	},
565 	{1, 0, 2, 0,		/* 0x47 */
566 		{{0, 2},
567 		{6, 6},
568 		{0, 0},
569 		{0, 0}
570 		}
571 	},
572 	{0, 0, 2, 0,		/* 0x48 */
573 		{{3, 3},
574 		{6, 6},
575 		{0, 0},
576 		{0, 0}
577 		}
578 	},
579 	{1, 0, 3, 0,		/* 0x49 */
580 		{{0, 0},
581 		{3, 3},
582 		{6, 6},
583 		{0, 0}
584 		}
585 	},
586 	{0, 0, 3, 0,		/* 0x4a */
587 		{{1, 1},
588 		{3, 3},
589 		{6, 6},
590 		{0, 0}
591 		}
592 	},
593 	{1, 0, 3, 0,		/* 0x4b */
594 		{{0, 1},
595 		{3, 3},
596 		{6, 6},
597 		{0, 0}
598 		}
599 	},
600 	{0, 0, 2, 0,		/* 0x4c */
601 		{{2, 3},
602 		{6, 6},
603 		{0, 0},
604 		{0, 0}
605 		}
606 	},
607 	{1, 0, 3, 0,		/* 0x4d */
608 		{{0, 0},
609 		{2, 3},
610 		{6, 6},
611 		{0, 0}
612 		}
613 	},
614 	{0, 0, 2, 0,		/* 0x4e */
615 		{{1, 3},
616 		{6, 6},
617 		{0, 0},
618 		{0, 0}
619 		}
620 	},
621 	{1, 0, 2, 0,		/* 0x4f */
622 		{{0, 3},
623 		{6, 6},
624 		{0, 0},
625 		{0, 0}
626 		}
627 	},
628 	{0, 0, 2, 0,		/* 0x50 */
629 		{{4, 4},
630 		{6, 6},
631 		{0, 0},
632 		{0, 0}
633 		}
634 	},
635 	{1, 0, 3, 0,		/* 0x51 */
636 		{{0, 0},
637 		{4, 4},
638 		{6, 6},
639 		{0, 0}
640 		}
641 	},
642 	{0, 0, 3, 0,		/* 0x52 */
643 		{{1, 1},
644 		{4, 4},
645 		{6, 6},
646 		{0, 0}
647 		}
648 	},
649 	{1, 0, 3, 0,		/* 0x53 */
650 		{{0, 1},
651 		{4, 4},
652 		{6, 6},
653 		{0, 0}
654 		}
655 	},
656 	{0, 0, 3, 0,		/* 0x54 */
657 		{{2, 2},
658 		{4, 4},
659 		{6, 6},
660 		{0, 0}
661 		}
662 	},
663 	{1, 0, 4, 0,		/* 0x55 */
664 		{{0, 0},
665 		{2, 2},
666 		{4, 4},
667 		{6, 6}
668 		}
669 	},
670 	{0, 0, 3, 0,		/* 0x56 */
671 		{{1, 2},
672 		{4, 4},
673 		{6, 6},
674 		{0, 0}
675 		}
676 	},
677 	{1, 0, 3, 0,		/* 0x57 */
678 		{{0, 2},
679 		{4, 4},
680 		{6, 6},
681 		{0, 0}
682 		}
683 	},
684 	{0, 0, 2, 0,		/* 0x58 */
685 		{{3, 4},
686 		{6, 6},
687 		{0, 0},
688 		{0, 0}
689 		}
690 	},
691 	{1, 0, 3, 0,		/* 0x59 */
692 		{{0, 0},
693 		{3, 4},
694 		{6, 6},
695 		{0, 0}
696 		}
697 	},
698 	{0, 0, 3, 0,		/* 0x5a */
699 		{{1, 1},
700 		{3, 4},
701 		{6, 6},
702 		{0, 0}
703 		}
704 	},
705 	{1, 0, 3, 0,		/* 0x5b */
706 		{{0, 1},
707 		{3, 4},
708 		{6, 6},
709 		{0, 0}
710 		}
711 	},
712 	{0, 0, 2, 0,		/* 0x5c */
713 		{{2, 4},
714 		{6, 6},
715 		{0, 0},
716 		{0, 0}
717 		}
718 	},
719 	{1, 0, 3, 0,		/* 0x5d */
720 		{{0, 0},
721 		{2, 4},
722 		{6, 6},
723 		{0, 0}
724 		}
725 	},
726 	{0, 0, 2, 0,		/* 0x5e */
727 		{{1, 4},
728 		{6, 6},
729 		{0, 0},
730 		{0, 0}
731 		}
732 	},
733 	{1, 0, 2, 0,		/* 0x5f */
734 		{{0, 4},
735 		{6, 6},
736 		{0, 0},
737 		{0, 0}
738 		}
739 	},
740 	{0, 0, 1, 0,		/* 0x60 */
741 		{{5, 6},
742 		{0, 0},
743 		{0, 0},
744 		{0, 0}
745 		}
746 	},
747 	{1, 0, 2, 0,		/* 0x61 */
748 		{{0, 0},
749 		{5, 6},
750 		{0, 0},
751 		{0, 0}
752 		}
753 	},
754 	{0, 0, 2, 0,		/* 0x62 */
755 		{{1, 1},
756 		{5, 6},
757 		{0, 0},
758 		{0, 0}
759 		}
760 	},
761 	{1, 0, 2, 0,		/* 0x63 */
762 		{{0, 1},
763 		{5, 6},
764 		{0, 0},
765 		{0, 0}
766 		}
767 	},
768 	{0, 0, 2, 0,		/* 0x64 */
769 		{{2, 2},
770 		{5, 6},
771 		{0, 0},
772 		{0, 0}
773 		}
774 	},
775 	{1, 0, 3, 0,		/* 0x65 */
776 		{{0, 0},
777 		{2, 2},
778 		{5, 6},
779 		{0, 0}
780 		}
781 	},
782 	{0, 0, 2, 0,		/* 0x66 */
783 		{{1, 2},
784 		{5, 6},
785 		{0, 0},
786 		{0, 0}
787 		}
788 	},
789 	{1, 0, 2, 0,		/* 0x67 */
790 		{{0, 2},
791 		{5, 6},
792 		{0, 0},
793 		{0, 0}
794 		}
795 	},
796 	{0, 0, 2, 0,		/* 0x68 */
797 		{{3, 3},
798 		{5, 6},
799 		{0, 0},
800 		{0, 0}
801 		}
802 	},
803 	{1, 0, 3, 0,		/* 0x69 */
804 		{{0, 0},
805 		{3, 3},
806 		{5, 6},
807 		{0, 0}
808 		}
809 	},
810 	{0, 0, 3, 0,		/* 0x6a */
811 		{{1, 1},
812 		{3, 3},
813 		{5, 6},
814 		{0, 0}
815 		}
816 	},
817 	{1, 0, 3, 0,		/* 0x6b */
818 		{{0, 1},
819 		{3, 3},
820 		{5, 6},
821 		{0, 0}
822 		}
823 	},
824 	{0, 0, 2, 0,		/* 0x6c */
825 		{{2, 3},
826 		{5, 6},
827 		{0, 0},
828 		{0, 0}
829 		}
830 	},
831 	{1, 0, 3, 0,		/* 0x6d */
832 		{{0, 0},
833 		{2, 3},
834 		{5, 6},
835 		{0, 0}
836 		}
837 	},
838 	{0, 0, 2, 0,		/* 0x6e */
839 		{{1, 3},
840 		{5, 6},
841 		{0, 0},
842 		{0, 0}
843 		}
844 	},
845 	{1, 0, 2, 0,		/* 0x6f */
846 		{{0, 3},
847 		{5, 6},
848 		{0, 0},
849 		{0, 0}
850 		}
851 	},
852 	{0, 0, 1, 0,		/* 0x70 */
853 		{{4, 6},
854 		{0, 0},
855 		{0, 0},
856 		{0, 0}
857 		}
858 	},
859 	{1, 0, 2, 0,		/* 0x71 */
860 		{{0, 0},
861 		{4, 6},
862 		{0, 0},
863 		{0, 0}
864 		}
865 	},
866 	{0, 0, 2, 0,		/* 0x72 */
867 		{{1, 1},
868 		{4, 6},
869 		{0, 0},
870 		{0, 0}
871 		}
872 	},
873 	{1, 0, 2, 0,		/* 0x73 */
874 		{{0, 1},
875 		{4, 6},
876 		{0, 0},
877 		{0, 0}
878 		}
879 	},
880 	{0, 0, 2, 0,		/* 0x74 */
881 		{{2, 2},
882 		{4, 6},
883 		{0, 0},
884 		{0, 0}
885 		}
886 	},
887 	{1, 0, 3, 0,		/* 0x75 */
888 		{{0, 0},
889 		{2, 2},
890 		{4, 6},
891 		{0, 0}
892 		}
893 	},
894 	{0, 0, 2, 0,		/* 0x76 */
895 		{{1, 2},
896 		{4, 6},
897 		{0, 0},
898 		{0, 0}
899 		}
900 	},
901 	{1, 0, 2, 0,		/* 0x77 */
902 		{{0, 2},
903 		{4, 6},
904 		{0, 0},
905 		{0, 0}
906 		}
907 	},
908 	{0, 0, 1, 0,		/* 0x78 */
909 		{{3, 6},
910 		{0, 0},
911 		{0, 0},
912 		{0, 0}
913 		}
914 	},
915 	{1, 0, 2, 0,		/* 0x79 */
916 		{{0, 0},
917 		{3, 6},
918 		{0, 0},
919 		{0, 0}
920 		}
921 	},
922 	{0, 0, 2, 0,		/* 0x7a */
923 		{{1, 1},
924 		{3, 6},
925 		{0, 0},
926 		{0, 0}
927 		}
928 	},
929 	{1, 0, 2, 0,		/* 0x7b */
930 		{{0, 1},
931 		{3, 6},
932 		{0, 0},
933 		{0, 0}
934 		}
935 	},
936 	{0, 0, 1, 0,		/* 0x7c */
937 		{{2, 6},
938 		{0, 0},
939 		{0, 0},
940 		{0, 0}
941 		}
942 	},
943 	{1, 0, 2, 0,		/* 0x7d */
944 		{{0, 0},
945 		{2, 6},
946 		{0, 0},
947 		{0, 0}
948 		}
949 	},
950 	{0, 0, 1, 0,		/* 0x7e */
951 		{{1, 6},
952 		{0, 0},
953 		{0, 0},
954 		{0, 0}
955 		}
956 	},
957 	{1, 0, 1, 0,		/* 0x7f */
958 		{{0, 6},
959 		{0, 0},
960 		{0, 0},
961 		{0, 0}
962 		}
963 	},
964 	{0, 1, 1, 0,		/* 0x80 */
965 		{{7, 7},
966 		{0, 0},
967 		{0, 0},
968 		{0, 0}
969 		}
970 	},
971 	{1, 1, 2, 0,		/* 0x81 */
972 		{{0, 0},
973 		{7, 7},
974 		{0, 0},
975 		{0, 0}
976 		}
977 	},
978 	{0, 1, 2, 0,		/* 0x82 */
979 		{{1, 1},
980 		{7, 7},
981 		{0, 0},
982 		{0, 0}
983 		}
984 	},
985 	{1, 1, 2, 0,		/* 0x83 */
986 		{{0, 1},
987 		{7, 7},
988 		{0, 0},
989 		{0, 0}
990 		}
991 	},
992 	{0, 1, 2, 0,		/* 0x84 */
993 		{{2, 2},
994 		{7, 7},
995 		{0, 0},
996 		{0, 0}
997 		}
998 	},
999 	{1, 1, 3, 0,		/* 0x85 */
1000 		{{0, 0},
1001 		{2, 2},
1002 		{7, 7},
1003 		{0, 0}
1004 		}
1005 	},
1006 	{0, 1, 2, 0,		/* 0x86 */
1007 		{{1, 2},
1008 		{7, 7},
1009 		{0, 0},
1010 		{0, 0}
1011 		}
1012 	},
1013 	{1, 1, 2, 0,		/* 0x87 */
1014 		{{0, 2},
1015 		{7, 7},
1016 		{0, 0},
1017 		{0, 0}
1018 		}
1019 	},
1020 	{0, 1, 2, 0,		/* 0x88 */
1021 		{{3, 3},
1022 		{7, 7},
1023 		{0, 0},
1024 		{0, 0}
1025 		}
1026 	},
1027 	{1, 1, 3, 0,		/* 0x89 */
1028 		{{0, 0},
1029 		{3, 3},
1030 		{7, 7},
1031 		{0, 0}
1032 		}
1033 	},
1034 	{0, 1, 3, 0,		/* 0x8a */
1035 		{{1, 1},
1036 		{3, 3},
1037 		{7, 7},
1038 		{0, 0}
1039 		}
1040 	},
1041 	{1, 1, 3, 0,		/* 0x8b */
1042 		{{0, 1},
1043 		{3, 3},
1044 		{7, 7},
1045 		{0, 0}
1046 		}
1047 	},
1048 	{0, 1, 2, 0,		/* 0x8c */
1049 		{{2, 3},
1050 		{7, 7},
1051 		{0, 0},
1052 		{0, 0}
1053 		}
1054 	},
1055 	{1, 1, 3, 0,		/* 0x8d */
1056 		{{0, 0},
1057 		{2, 3},
1058 		{7, 7},
1059 		{0, 0}
1060 		}
1061 	},
1062 	{0, 1, 2, 0,		/* 0x8e */
1063 		{{1, 3},
1064 		{7, 7},
1065 		{0, 0},
1066 		{0, 0}
1067 		}
1068 	},
1069 	{1, 1, 2, 0,		/* 0x8f */
1070 		{{0, 3},
1071 		{7, 7},
1072 		{0, 0},
1073 		{0, 0}
1074 		}
1075 	},
1076 	{0, 1, 2, 0,		/* 0x90 */
1077 		{{4, 4},
1078 		{7, 7},
1079 		{0, 0},
1080 		{0, 0}
1081 		}
1082 	},
1083 	{1, 1, 3, 0,		/* 0x91 */
1084 		{{0, 0},
1085 		{4, 4},
1086 		{7, 7},
1087 		{0, 0}
1088 		}
1089 	},
1090 	{0, 1, 3, 0,		/* 0x92 */
1091 		{{1, 1},
1092 		{4, 4},
1093 		{7, 7},
1094 		{0, 0}
1095 		}
1096 	},
1097 	{1, 1, 3, 0,		/* 0x93 */
1098 		{{0, 1},
1099 		{4, 4},
1100 		{7, 7},
1101 		{0, 0}
1102 		}
1103 	},
1104 	{0, 1, 3, 0,		/* 0x94 */
1105 		{{2, 2},
1106 		{4, 4},
1107 		{7, 7},
1108 		{0, 0}
1109 		}
1110 	},
1111 	{1, 1, 4, 0,		/* 0x95 */
1112 		{{0, 0},
1113 		{2, 2},
1114 		{4, 4},
1115 		{7, 7}
1116 		}
1117 	},
1118 	{0, 1, 3, 0,		/* 0x96 */
1119 		{{1, 2},
1120 		{4, 4},
1121 		{7, 7},
1122 		{0, 0}
1123 		}
1124 	},
1125 	{1, 1, 3, 0,		/* 0x97 */
1126 		{{0, 2},
1127 		{4, 4},
1128 		{7, 7},
1129 		{0, 0}
1130 		}
1131 	},
1132 	{0, 1, 2, 0,		/* 0x98 */
1133 		{{3, 4},
1134 		{7, 7},
1135 		{0, 0},
1136 		{0, 0}
1137 		}
1138 	},
1139 	{1, 1, 3, 0,		/* 0x99 */
1140 		{{0, 0},
1141 		{3, 4},
1142 		{7, 7},
1143 		{0, 0}
1144 		}
1145 	},
1146 	{0, 1, 3, 0,		/* 0x9a */
1147 		{{1, 1},
1148 		{3, 4},
1149 		{7, 7},
1150 		{0, 0}
1151 		}
1152 	},
1153 	{1, 1, 3, 0,		/* 0x9b */
1154 		{{0, 1},
1155 		{3, 4},
1156 		{7, 7},
1157 		{0, 0}
1158 		}
1159 	},
1160 	{0, 1, 2, 0,		/* 0x9c */
1161 		{{2, 4},
1162 		{7, 7},
1163 		{0, 0},
1164 		{0, 0}
1165 		}
1166 	},
1167 	{1, 1, 3, 0,		/* 0x9d */
1168 		{{0, 0},
1169 		{2, 4},
1170 		{7, 7},
1171 		{0, 0}
1172 		}
1173 	},
1174 	{0, 1, 2, 0,		/* 0x9e */
1175 		{{1, 4},
1176 		{7, 7},
1177 		{0, 0},
1178 		{0, 0}
1179 		}
1180 	},
1181 	{1, 1, 2, 0,		/* 0x9f */
1182 		{{0, 4},
1183 		{7, 7},
1184 		{0, 0},
1185 		{0, 0}
1186 		}
1187 	},
1188 	{0, 1, 2, 0,		/* 0xa0 */
1189 		{{5, 5},
1190 		{7, 7},
1191 		{0, 0},
1192 		{0, 0}
1193 		}
1194 	},
1195 	{1, 1, 3, 0,		/* 0xa1 */
1196 		{{0, 0},
1197 		{5, 5},
1198 		{7, 7},
1199 		{0, 0}
1200 		}
1201 	},
1202 	{0, 1, 3, 0,		/* 0xa2 */
1203 		{{1, 1},
1204 		{5, 5},
1205 		{7, 7},
1206 		{0, 0}
1207 		}
1208 	},
1209 	{1, 1, 3, 0,		/* 0xa3 */
1210 		{{0, 1},
1211 		{5, 5},
1212 		{7, 7},
1213 		{0, 0}
1214 		}
1215 	},
1216 	{0, 1, 3, 0,		/* 0xa4 */
1217 		{{2, 2},
1218 		{5, 5},
1219 		{7, 7},
1220 		{0, 0}
1221 		}
1222 	},
1223 	{1, 1, 4, 0,		/* 0xa5 */
1224 		{{0, 0},
1225 		{2, 2},
1226 		{5, 5},
1227 		{7, 7}
1228 		}
1229 	},
1230 	{0, 1, 3, 0,		/* 0xa6 */
1231 		{{1, 2},
1232 		{5, 5},
1233 		{7, 7},
1234 		{0, 0}
1235 		}
1236 	},
1237 	{1, 1, 3, 0,		/* 0xa7 */
1238 		{{0, 2},
1239 		{5, 5},
1240 		{7, 7},
1241 		{0, 0}
1242 		}
1243 	},
1244 	{0, 1, 3, 0,		/* 0xa8 */
1245 		{{3, 3},
1246 		{5, 5},
1247 		{7, 7},
1248 		{0, 0}
1249 		}
1250 	},
1251 	{1, 1, 4, 0,		/* 0xa9 */
1252 		{{0, 0},
1253 		{3, 3},
1254 		{5, 5},
1255 		{7, 7}
1256 		}
1257 	},
1258 	{0, 1, 4, 0,		/* 0xaa */
1259 		{{1, 1},
1260 		{3, 3},
1261 		{5, 5},
1262 		{7, 7}
1263 		}
1264 	},
1265 	{1, 1, 4, 0,		/* 0xab */
1266 		{{0, 1},
1267 		{3, 3},
1268 		{5, 5},
1269 		{7, 7}
1270 		}
1271 	},
1272 	{0, 1, 3, 0,		/* 0xac */
1273 		{{2, 3},
1274 		{5, 5},
1275 		{7, 7},
1276 		{0, 0}
1277 		}
1278 	},
1279 	{1, 1, 4, 0,		/* 0xad */
1280 		{{0, 0},
1281 		{2, 3},
1282 		{5, 5},
1283 		{7, 7}
1284 		}
1285 	},
1286 	{0, 1, 3, 0,		/* 0xae */
1287 		{{1, 3},
1288 		{5, 5},
1289 		{7, 7},
1290 		{0, 0}
1291 		}
1292 	},
1293 	{1, 1, 3, 0,		/* 0xaf */
1294 		{{0, 3},
1295 		{5, 5},
1296 		{7, 7},
1297 		{0, 0}
1298 		}
1299 	},
1300 	{0, 1, 2, 0,		/* 0xb0 */
1301 		{{4, 5},
1302 		{7, 7},
1303 		{0, 0},
1304 		{0, 0}
1305 		}
1306 	},
1307 	{1, 1, 3, 0,		/* 0xb1 */
1308 		{{0, 0},
1309 		{4, 5},
1310 		{7, 7},
1311 		{0, 0}
1312 		}
1313 	},
1314 	{0, 1, 3, 0,		/* 0xb2 */
1315 		{{1, 1},
1316 		{4, 5},
1317 		{7, 7},
1318 		{0, 0}
1319 		}
1320 	},
1321 	{1, 1, 3, 0,		/* 0xb3 */
1322 		{{0, 1},
1323 		{4, 5},
1324 		{7, 7},
1325 		{0, 0}
1326 		}
1327 	},
1328 	{0, 1, 3, 0,		/* 0xb4 */
1329 		{{2, 2},
1330 		{4, 5},
1331 		{7, 7},
1332 		{0, 0}
1333 		}
1334 	},
1335 	{1, 1, 4, 0,		/* 0xb5 */
1336 		{{0, 0},
1337 		{2, 2},
1338 		{4, 5},
1339 		{7, 7}
1340 		}
1341 	},
1342 	{0, 1, 3, 0,		/* 0xb6 */
1343 		{{1, 2},
1344 		{4, 5},
1345 		{7, 7},
1346 		{0, 0}
1347 		}
1348 	},
1349 	{1, 1, 3, 0,		/* 0xb7 */
1350 		{{0, 2},
1351 		{4, 5},
1352 		{7, 7},
1353 		{0, 0}
1354 		}
1355 	},
1356 	{0, 1, 2, 0,		/* 0xb8 */
1357 		{{3, 5},
1358 		{7, 7},
1359 		{0, 0},
1360 		{0, 0}
1361 		}
1362 	},
1363 	{1, 1, 3, 0,		/* 0xb9 */
1364 		{{0, 0},
1365 		{3, 5},
1366 		{7, 7},
1367 		{0, 0}
1368 		}
1369 	},
1370 	{0, 1, 3, 0,		/* 0xba */
1371 		{{1, 1},
1372 		{3, 5},
1373 		{7, 7},
1374 		{0, 0}
1375 		}
1376 	},
1377 	{1, 1, 3, 0,		/* 0xbb */
1378 		{{0, 1},
1379 		{3, 5},
1380 		{7, 7},
1381 		{0, 0}
1382 		}
1383 	},
1384 	{0, 1, 2, 0,		/* 0xbc */
1385 		{{2, 5},
1386 		{7, 7},
1387 		{0, 0},
1388 		{0, 0}
1389 		}
1390 	},
1391 	{1, 1, 3, 0,		/* 0xbd */
1392 		{{0, 0},
1393 		{2, 5},
1394 		{7, 7},
1395 		{0, 0}
1396 		}
1397 	},
1398 	{0, 1, 2, 0,		/* 0xbe */
1399 		{{1, 5},
1400 		{7, 7},
1401 		{0, 0},
1402 		{0, 0}
1403 		}
1404 	},
1405 	{1, 1, 2, 0,		/* 0xbf */
1406 		{{0, 5},
1407 		{7, 7},
1408 		{0, 0},
1409 		{0, 0}
1410 		}
1411 	},
1412 	{0, 1, 1, 0,		/* 0xc0 */
1413 		{{6, 7},
1414 		{0, 0},
1415 		{0, 0},
1416 		{0, 0}
1417 		}
1418 	},
1419 	{1, 1, 2, 0,		/* 0xc1 */
1420 		{{0, 0},
1421 		{6, 7},
1422 		{0, 0},
1423 		{0, 0}
1424 		}
1425 	},
1426 	{0, 1, 2, 0,		/* 0xc2 */
1427 		{{1, 1},
1428 		{6, 7},
1429 		{0, 0},
1430 		{0, 0}
1431 		}
1432 	},
1433 	{1, 1, 2, 0,		/* 0xc3 */
1434 		{{0, 1},
1435 		{6, 7},
1436 		{0, 0},
1437 		{0, 0}
1438 		}
1439 	},
1440 	{0, 1, 2, 0,		/* 0xc4 */
1441 		{{2, 2},
1442 		{6, 7},
1443 		{0, 0},
1444 		{0, 0}
1445 		}
1446 	},
1447 	{1, 1, 3, 0,		/* 0xc5 */
1448 		{{0, 0},
1449 		{2, 2},
1450 		{6, 7},
1451 		{0, 0}
1452 		}
1453 	},
1454 	{0, 1, 2, 0,		/* 0xc6 */
1455 		{{1, 2},
1456 		{6, 7},
1457 		{0, 0},
1458 		{0, 0}
1459 		}
1460 	},
1461 	{1, 1, 2, 0,		/* 0xc7 */
1462 		{{0, 2},
1463 		{6, 7},
1464 		{0, 0},
1465 		{0, 0}
1466 		}
1467 	},
1468 	{0, 1, 2, 0,		/* 0xc8 */
1469 		{{3, 3},
1470 		{6, 7},
1471 		{0, 0},
1472 		{0, 0}
1473 		}
1474 	},
1475 	{1, 1, 3, 0,		/* 0xc9 */
1476 		{{0, 0},
1477 		{3, 3},
1478 		{6, 7},
1479 		{0, 0}
1480 		}
1481 	},
1482 	{0, 1, 3, 0,		/* 0xca */
1483 		{{1, 1},
1484 		{3, 3},
1485 		{6, 7},
1486 		{0, 0}
1487 		}
1488 	},
1489 	{1, 1, 3, 0,		/* 0xcb */
1490 		{{0, 1},
1491 		{3, 3},
1492 		{6, 7},
1493 		{0, 0}
1494 		}
1495 	},
1496 	{0, 1, 2, 0,		/* 0xcc */
1497 		{{2, 3},
1498 		{6, 7},
1499 		{0, 0},
1500 		{0, 0}
1501 		}
1502 	},
1503 	{1, 1, 3, 0,		/* 0xcd */
1504 		{{0, 0},
1505 		{2, 3},
1506 		{6, 7},
1507 		{0, 0}
1508 		}
1509 	},
1510 	{0, 1, 2, 0,		/* 0xce */
1511 		{{1, 3},
1512 		{6, 7},
1513 		{0, 0},
1514 		{0, 0}
1515 		}
1516 	},
1517 	{1, 1, 2, 0,		/* 0xcf */
1518 		{{0, 3},
1519 		{6, 7},
1520 		{0, 0},
1521 		{0, 0}
1522 		}
1523 	},
1524 	{0, 1, 2, 0,		/* 0xd0 */
1525 		{{4, 4},
1526 		{6, 7},
1527 		{0, 0},
1528 		{0, 0}
1529 		}
1530 	},
1531 	{1, 1, 3, 0,		/* 0xd1 */
1532 		{{0, 0},
1533 		{4, 4},
1534 		{6, 7},
1535 		{0, 0}
1536 		}
1537 	},
1538 	{0, 1, 3, 0,		/* 0xd2 */
1539 		{{1, 1},
1540 		{4, 4},
1541 		{6, 7},
1542 		{0, 0}
1543 		}
1544 	},
1545 	{1, 1, 3, 0,		/* 0xd3 */
1546 		{{0, 1},
1547 		{4, 4},
1548 		{6, 7},
1549 		{0, 0}
1550 		}
1551 	},
1552 	{0, 1, 3, 0,		/* 0xd4 */
1553 		{{2, 2},
1554 		{4, 4},
1555 		{6, 7},
1556 		{0, 0}
1557 		}
1558 	},
1559 	{1, 1, 4, 0,		/* 0xd5 */
1560 		{{0, 0},
1561 		{2, 2},
1562 		{4, 4},
1563 		{6, 7}
1564 		}
1565 	},
1566 	{0, 1, 3, 0,		/* 0xd6 */
1567 		{{1, 2},
1568 		{4, 4},
1569 		{6, 7},
1570 		{0, 0}
1571 		}
1572 	},
1573 	{1, 1, 3, 0,		/* 0xd7 */
1574 		{{0, 2},
1575 		{4, 4},
1576 		{6, 7},
1577 		{0, 0}
1578 		}
1579 	},
1580 	{0, 1, 2, 0,		/* 0xd8 */
1581 		{{3, 4},
1582 		{6, 7},
1583 		{0, 0},
1584 		{0, 0}
1585 		}
1586 	},
1587 	{1, 1, 3, 0,		/* 0xd9 */
1588 		{{0, 0},
1589 		{3, 4},
1590 		{6, 7},
1591 		{0, 0}
1592 		}
1593 	},
1594 	{0, 1, 3, 0,		/* 0xda */
1595 		{{1, 1},
1596 		{3, 4},
1597 		{6, 7},
1598 		{0, 0}
1599 		}
1600 	},
1601 	{1, 1, 3, 0,		/* 0xdb */
1602 		{{0, 1},
1603 		{3, 4},
1604 		{6, 7},
1605 		{0, 0}
1606 		}
1607 	},
1608 	{0, 1, 2, 0,		/* 0xdc */
1609 		{{2, 4},
1610 		{6, 7},
1611 		{0, 0},
1612 		{0, 0}
1613 		}
1614 	},
1615 	{1, 1, 3, 0,		/* 0xdd */
1616 		{{0, 0},
1617 		{2, 4},
1618 		{6, 7},
1619 		{0, 0}
1620 		}
1621 	},
1622 	{0, 1, 2, 0,		/* 0xde */
1623 		{{1, 4},
1624 		{6, 7},
1625 		{0, 0},
1626 		{0, 0}
1627 		}
1628 	},
1629 	{1, 1, 2, 0,		/* 0xdf */
1630 		{{0, 4},
1631 		{6, 7},
1632 		{0, 0},
1633 		{0, 0}
1634 		}
1635 	},
1636 	{0, 1, 1, 0,		/* 0xe0 */
1637 		{{5, 7},
1638 		{0, 0},
1639 		{0, 0},
1640 		{0, 0}
1641 		}
1642 	},
1643 	{1, 1, 2, 0,		/* 0xe1 */
1644 		{{0, 0},
1645 		{5, 7},
1646 		{0, 0},
1647 		{0, 0}
1648 		}
1649 	},
1650 	{0, 1, 2, 0,		/* 0xe2 */
1651 		{{1, 1},
1652 		{5, 7},
1653 		{0, 0},
1654 		{0, 0}
1655 		}
1656 	},
1657 	{1, 1, 2, 0,		/* 0xe3 */
1658 		{{0, 1},
1659 		{5, 7},
1660 		{0, 0},
1661 		{0, 0}
1662 		}
1663 	},
1664 	{0, 1, 2, 0,		/* 0xe4 */
1665 		{{2, 2},
1666 		{5, 7},
1667 		{0, 0},
1668 		{0, 0}
1669 		}
1670 	},
1671 	{1, 1, 3, 0,		/* 0xe5 */
1672 		{{0, 0},
1673 		{2, 2},
1674 		{5, 7},
1675 		{0, 0}
1676 		}
1677 	},
1678 	{0, 1, 2, 0,		/* 0xe6 */
1679 		{{1, 2},
1680 		{5, 7},
1681 		{0, 0},
1682 		{0, 0}
1683 		}
1684 	},
1685 	{1, 1, 2, 0,		/* 0xe7 */
1686 		{{0, 2},
1687 		{5, 7},
1688 		{0, 0},
1689 		{0, 0}
1690 		}
1691 	},
1692 	{0, 1, 2, 0,		/* 0xe8 */
1693 		{{3, 3},
1694 		{5, 7},
1695 		{0, 0},
1696 		{0, 0}
1697 		}
1698 	},
1699 	{1, 1, 3, 0,		/* 0xe9 */
1700 		{{0, 0},
1701 		{3, 3},
1702 		{5, 7},
1703 		{0, 0}
1704 		}
1705 	},
1706 	{0, 1, 3, 0,		/* 0xea */
1707 		{{1, 1},
1708 		{3, 3},
1709 		{5, 7},
1710 		{0, 0}
1711 		}
1712 	},
1713 	{1, 1, 3, 0,		/* 0xeb */
1714 		{{0, 1},
1715 		{3, 3},
1716 		{5, 7},
1717 		{0, 0}
1718 		}
1719 	},
1720 	{0, 1, 2, 0,		/* 0xec */
1721 		{{2, 3},
1722 		{5, 7},
1723 		{0, 0},
1724 		{0, 0}
1725 		}
1726 	},
1727 	{1, 1, 3, 0,		/* 0xed */
1728 		{{0, 0},
1729 		{2, 3},
1730 		{5, 7},
1731 		{0, 0}
1732 		}
1733 	},
1734 	{0, 1, 2, 0,		/* 0xee */
1735 		{{1, 3},
1736 		{5, 7},
1737 		{0, 0},
1738 		{0, 0}
1739 		}
1740 	},
1741 	{1, 1, 2, 0,		/* 0xef */
1742 		{{0, 3},
1743 		{5, 7},
1744 		{0, 0},
1745 		{0, 0}
1746 		}
1747 	},
1748 	{0, 1, 1, 0,		/* 0xf0 */
1749 		{{4, 7},
1750 		{0, 0},
1751 		{0, 0},
1752 		{0, 0}
1753 		}
1754 	},
1755 	{1, 1, 2, 0,		/* 0xf1 */
1756 		{{0, 0},
1757 		{4, 7},
1758 		{0, 0},
1759 		{0, 0}
1760 		}
1761 	},
1762 	{0, 1, 2, 0,		/* 0xf2 */
1763 		{{1, 1},
1764 		{4, 7},
1765 		{0, 0},
1766 		{0, 0}
1767 		}
1768 	},
1769 	{1, 1, 2, 0,		/* 0xf3 */
1770 		{{0, 1},
1771 		{4, 7},
1772 		{0, 0},
1773 		{0, 0}
1774 		}
1775 	},
1776 	{0, 1, 2, 0,		/* 0xf4 */
1777 		{{2, 2},
1778 		{4, 7},
1779 		{0, 0},
1780 		{0, 0}
1781 		}
1782 	},
1783 	{1, 1, 3, 0,		/* 0xf5 */
1784 		{{0, 0},
1785 		{2, 2},
1786 		{4, 7},
1787 		{0, 0}
1788 		}
1789 	},
1790 	{0, 1, 2, 0,		/* 0xf6 */
1791 		{{1, 2},
1792 		{4, 7},
1793 		{0, 0},
1794 		{0, 0}
1795 		}
1796 	},
1797 	{1, 1, 2, 0,		/* 0xf7 */
1798 		{{0, 2},
1799 		{4, 7},
1800 		{0, 0},
1801 		{0, 0}
1802 		}
1803 	},
1804 	{0, 1, 1, 0,		/* 0xf8 */
1805 		{{3, 7},
1806 		{0, 0},
1807 		{0, 0},
1808 		{0, 0}
1809 		}
1810 	},
1811 	{1, 1, 2, 0,		/* 0xf9 */
1812 		{{0, 0},
1813 		{3, 7},
1814 		{0, 0},
1815 		{0, 0}
1816 		}
1817 	},
1818 	{0, 1, 2, 0,		/* 0xfa */
1819 		{{1, 1},
1820 		{3, 7},
1821 		{0, 0},
1822 		{0, 0}
1823 		}
1824 	},
1825 	{1, 1, 2, 0,		/* 0xfb */
1826 		{{0, 1},
1827 		{3, 7},
1828 		{0, 0},
1829 		{0, 0}
1830 		}
1831 	},
1832 	{0, 1, 1, 0,		/* 0xfc */
1833 		{{2, 7},
1834 		{0, 0},
1835 		{0, 0},
1836 		{0, 0}
1837 		}
1838 	},
1839 	{1, 1, 2, 0,		/* 0xfd */
1840 		{{0, 0},
1841 		{2, 7},
1842 		{0, 0},
1843 		{0, 0}
1844 		}
1845 	},
1846 	{0, 1, 1, 0,		/* 0xfe */
1847 		{{1, 7},
1848 		{0, 0},
1849 		{0, 0},
1850 		{0, 0}
1851 		}
1852 	},
1853 	{1, 1, 1, 0,		/* 0xff */
1854 		{{0, 7},
1855 		{0, 0},
1856 		{0, 0},
1857 		{0, 0}
1858 		}
1859 	}
1860 };
1861 
1862 
1863 int
1864 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1865     int ipv4_addr_legal,
1866     int ipv6_addr_legal,
1867     int loopback_scope,
1868     int ipv4_local_scope,
1869     int local_scope,
1870     int site_scope,
1871     int do_update)
1872 {
1873 	if ((loopback_scope == 0) &&
1874 	    (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1875 		/*
1876 		 * skip loopback if not in scope *
1877 		 */
1878 		return (0);
1879 	}
1880 	switch (ifa->address.sa.sa_family) {
1881 	case AF_INET:
1882 		if (ipv4_addr_legal) {
1883 			struct sockaddr_in *sin;
1884 
1885 			sin = (struct sockaddr_in *)&ifa->address.sin;
1886 			if (sin->sin_addr.s_addr == 0) {
1887 				/* not in scope , unspecified */
1888 				return (0);
1889 			}
1890 			if ((ipv4_local_scope == 0) &&
1891 			    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1892 				/* private address not in scope */
1893 				return (0);
1894 			}
1895 		} else {
1896 			return (0);
1897 		}
1898 		break;
1899 #ifdef INET6
1900 	case AF_INET6:
1901 		if (ipv6_addr_legal) {
1902 			struct sockaddr_in6 *sin6;
1903 
1904 			/*
1905 			 * Must update the flags,  bummer, which means any
1906 			 * IFA locks must now be applied HERE <->
1907 			 */
1908 			if (do_update) {
1909 				sctp_gather_internal_ifa_flags(ifa);
1910 			}
1911 			if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1912 				return (0);
1913 			}
1914 			/* ok to use deprecated addresses? */
1915 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1916 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1917 				/* skip unspecifed addresses */
1918 				return (0);
1919 			}
1920 			if (	/* (local_scope == 0) && */
1921 			    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1922 				return (0);
1923 			}
1924 			if ((site_scope == 0) &&
1925 			    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1926 				return (0);
1927 			}
1928 		} else {
1929 			return (0);
1930 		}
1931 		break;
1932 #endif
1933 	default:
1934 		return (0);
1935 	}
1936 	return (1);
1937 }
1938 
1939 static struct mbuf *
1940 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1941 {
1942 	struct sctp_paramhdr *parmh;
1943 	struct mbuf *mret;
1944 	int len;
1945 
1946 	if (ifa->address.sa.sa_family == AF_INET) {
1947 		len = sizeof(struct sctp_ipv4addr_param);
1948 	} else if (ifa->address.sa.sa_family == AF_INET6) {
1949 		len = sizeof(struct sctp_ipv6addr_param);
1950 	} else {
1951 		/* unknown type */
1952 		return (m);
1953 	}
1954 	if (M_TRAILINGSPACE(m) >= len) {
1955 		/* easy side we just drop it on the end */
1956 		parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1957 		mret = m;
1958 	} else {
1959 		/* Need more space */
1960 		mret = m;
1961 		while (SCTP_BUF_NEXT(mret) != NULL) {
1962 			mret = SCTP_BUF_NEXT(mret);
1963 		}
1964 		SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1965 		if (SCTP_BUF_NEXT(mret) == NULL) {
1966 			/* We are hosed, can't add more addresses */
1967 			return (m);
1968 		}
1969 		mret = SCTP_BUF_NEXT(mret);
1970 		parmh = mtod(mret, struct sctp_paramhdr *);
1971 	}
1972 	/* now add the parameter */
1973 	switch (ifa->address.sa.sa_family) {
1974 	case AF_INET:
1975 		{
1976 			struct sctp_ipv4addr_param *ipv4p;
1977 			struct sockaddr_in *sin;
1978 
1979 			sin = (struct sockaddr_in *)&ifa->address.sin;
1980 			ipv4p = (struct sctp_ipv4addr_param *)parmh;
1981 			parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1982 			parmh->param_length = htons(len);
1983 			ipv4p->addr = sin->sin_addr.s_addr;
1984 			SCTP_BUF_LEN(mret) += len;
1985 			break;
1986 		}
1987 #ifdef INET6
1988 	case AF_INET6:
1989 		{
1990 			struct sctp_ipv6addr_param *ipv6p;
1991 			struct sockaddr_in6 *sin6;
1992 
1993 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1994 			ipv6p = (struct sctp_ipv6addr_param *)parmh;
1995 			parmh->param_type = htons(SCTP_IPV6_ADDRESS);
1996 			parmh->param_length = htons(len);
1997 			memcpy(ipv6p->addr, &sin6->sin6_addr,
1998 			    sizeof(ipv6p->addr));
1999 			/* clear embedded scope in the address */
2000 			in6_clearscope((struct in6_addr *)ipv6p->addr);
2001 			SCTP_BUF_LEN(mret) += len;
2002 			break;
2003 		}
2004 #endif
2005 	default:
2006 		return (m);
2007 	}
2008 	return (mret);
2009 }
2010 
2011 
2012 struct mbuf *
2013 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_scoping *scope,
2014     struct mbuf *m_at, int cnt_inits_to)
2015 {
2016 	struct sctp_vrf *vrf = NULL;
2017 	int cnt, limit_out = 0, total_count;
2018 	uint32_t vrf_id;
2019 
2020 	vrf_id = inp->def_vrf_id;
2021 	SCTP_IPI_ADDR_RLOCK();
2022 	vrf = sctp_find_vrf(vrf_id);
2023 	if (vrf == NULL) {
2024 		SCTP_IPI_ADDR_RUNLOCK();
2025 		return (m_at);
2026 	}
2027 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2028 		struct sctp_ifa *sctp_ifap;
2029 		struct sctp_ifn *sctp_ifnp;
2030 
2031 		cnt = cnt_inits_to;
2032 		if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2033 			limit_out = 1;
2034 			cnt = SCTP_ADDRESS_LIMIT;
2035 			goto skip_count;
2036 		}
2037 		LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2038 			if ((scope->loopback_scope == 0) &&
2039 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2040 				/*
2041 				 * Skip loopback devices if loopback_scope
2042 				 * not set
2043 				 */
2044 				continue;
2045 			}
2046 			LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2047 				if (sctp_is_address_in_scope(sctp_ifap,
2048 				    scope->ipv4_addr_legal,
2049 				    scope->ipv6_addr_legal,
2050 				    scope->loopback_scope,
2051 				    scope->ipv4_local_scope,
2052 				    scope->local_scope,
2053 				    scope->site_scope, 1) == 0) {
2054 					continue;
2055 				}
2056 				cnt++;
2057 				if (cnt > SCTP_ADDRESS_LIMIT) {
2058 					break;
2059 				}
2060 			}
2061 			if (cnt > SCTP_ADDRESS_LIMIT) {
2062 				break;
2063 			}
2064 		}
2065 skip_count:
2066 		if (cnt > 1) {
2067 			total_count = 0;
2068 			LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2069 				cnt = 0;
2070 				if ((scope->loopback_scope == 0) &&
2071 				    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2072 					/*
2073 					 * Skip loopback devices if
2074 					 * loopback_scope not set
2075 					 */
2076 					continue;
2077 				}
2078 				LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2079 					if (sctp_is_address_in_scope(sctp_ifap,
2080 					    scope->ipv4_addr_legal,
2081 					    scope->ipv6_addr_legal,
2082 					    scope->loopback_scope,
2083 					    scope->ipv4_local_scope,
2084 					    scope->local_scope,
2085 					    scope->site_scope, 0) == 0) {
2086 						continue;
2087 					}
2088 					m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2089 					if (limit_out) {
2090 						cnt++;
2091 						total_count++;
2092 						if (cnt >= 2) {
2093 							/*
2094 							 * two from each
2095 							 * address
2096 							 */
2097 							break;
2098 						}
2099 						if (total_count > SCTP_ADDRESS_LIMIT) {
2100 							/* No more addresses */
2101 							break;
2102 						}
2103 					}
2104 				}
2105 			}
2106 		}
2107 	} else {
2108 		struct sctp_laddr *laddr;
2109 
2110 		cnt = cnt_inits_to;
2111 		/* First, how many ? */
2112 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2113 			if (laddr->ifa == NULL) {
2114 				continue;
2115 			}
2116 			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2117 				/*
2118 				 * Address being deleted by the system, dont
2119 				 * list.
2120 				 */
2121 				continue;
2122 			if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2123 				/*
2124 				 * Address being deleted on this ep don't
2125 				 * list.
2126 				 */
2127 				continue;
2128 			}
2129 			if (sctp_is_address_in_scope(laddr->ifa,
2130 			    scope->ipv4_addr_legal,
2131 			    scope->ipv6_addr_legal,
2132 			    scope->loopback_scope,
2133 			    scope->ipv4_local_scope,
2134 			    scope->local_scope,
2135 			    scope->site_scope, 1) == 0) {
2136 				continue;
2137 			}
2138 			cnt++;
2139 		}
2140 		if (cnt > SCTP_ADDRESS_LIMIT) {
2141 			limit_out = 1;
2142 		}
2143 		/*
2144 		 * To get through a NAT we only list addresses if we have
2145 		 * more than one. That way if you just bind a single address
2146 		 * we let the source of the init dictate our address.
2147 		 */
2148 		if (cnt > 1) {
2149 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2150 				cnt = 0;
2151 				if (laddr->ifa == NULL) {
2152 					continue;
2153 				}
2154 				if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2155 					continue;
2156 
2157 				if (sctp_is_address_in_scope(laddr->ifa,
2158 				    scope->ipv4_addr_legal,
2159 				    scope->ipv6_addr_legal,
2160 				    scope->loopback_scope,
2161 				    scope->ipv4_local_scope,
2162 				    scope->local_scope,
2163 				    scope->site_scope, 0) == 0) {
2164 					continue;
2165 				}
2166 				m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2167 				cnt++;
2168 				if (cnt >= SCTP_ADDRESS_LIMIT) {
2169 					break;
2170 				}
2171 			}
2172 		}
2173 	}
2174 	SCTP_IPI_ADDR_RUNLOCK();
2175 	return (m_at);
2176 }
2177 
2178 static struct sctp_ifa *
2179 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2180     uint8_t dest_is_loop,
2181     uint8_t dest_is_priv,
2182     sa_family_t fam)
2183 {
2184 	uint8_t dest_is_global = 0;
2185 
2186 	/* dest_is_priv is true if destination is a private address */
2187 	/* dest_is_loop is true if destination is a loopback addresses */
2188 
2189 	/**
2190 	 * Here we determine if its a preferred address. A preferred address
2191 	 * means it is the same scope or higher scope then the destination.
2192 	 * L = loopback, P = private, G = global
2193 	 * -----------------------------------------
2194          *    src    |  dest | result
2195          *  ----------------------------------------
2196          *     L     |    L  |    yes
2197          *  -----------------------------------------
2198          *     P     |    L  |    yes-v4 no-v6
2199          *  -----------------------------------------
2200          *     G     |    L  |    yes-v4 no-v6
2201          *  -----------------------------------------
2202          *     L     |    P  |    no
2203          *  -----------------------------------------
2204          *     P     |    P  |    yes
2205          *  -----------------------------------------
2206          *     G     |    P  |    no
2207          *   -----------------------------------------
2208          *     L     |    G  |    no
2209          *   -----------------------------------------
2210          *     P     |    G  |    no
2211          *    -----------------------------------------
2212          *     G     |    G  |    yes
2213          *    -----------------------------------------
2214 	 */
2215 
2216 	if (ifa->address.sa.sa_family != fam) {
2217 		/* forget mis-matched family */
2218 		return (NULL);
2219 	}
2220 	if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2221 		dest_is_global = 1;
2222 	}
2223 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2224 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2225 	/* Ok the address may be ok */
2226 	if (fam == AF_INET6) {
2227 		/* ok to use deprecated addresses? no lets not! */
2228 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2229 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2230 			return (NULL);
2231 		}
2232 		if (ifa->src_is_priv && !ifa->src_is_loop) {
2233 			if (dest_is_loop) {
2234 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2235 				return (NULL);
2236 			}
2237 		}
2238 		if (ifa->src_is_glob) {
2239 			if (dest_is_loop) {
2240 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2241 				return (NULL);
2242 			}
2243 		}
2244 	}
2245 	/*
2246 	 * Now that we know what is what, implement or table this could in
2247 	 * theory be done slicker (it used to be), but this is
2248 	 * straightforward and easier to validate :-)
2249 	 */
2250 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2251 	    ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2252 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2253 	    dest_is_loop, dest_is_priv, dest_is_global);
2254 
2255 	if ((ifa->src_is_loop) && (dest_is_priv)) {
2256 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2257 		return (NULL);
2258 	}
2259 	if ((ifa->src_is_glob) && (dest_is_priv)) {
2260 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2261 		return (NULL);
2262 	}
2263 	if ((ifa->src_is_loop) && (dest_is_global)) {
2264 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2265 		return (NULL);
2266 	}
2267 	if ((ifa->src_is_priv) && (dest_is_global)) {
2268 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2269 		return (NULL);
2270 	}
2271 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2272 	/* its a preferred address */
2273 	return (ifa);
2274 }
2275 
2276 static struct sctp_ifa *
2277 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2278     uint8_t dest_is_loop,
2279     uint8_t dest_is_priv,
2280     sa_family_t fam)
2281 {
2282 	uint8_t dest_is_global = 0;
2283 
2284 	/*
2285 	 * Here we determine if its a acceptable address. A acceptable
2286 	 * address means it is the same scope or higher scope but we can
2287 	 * allow for NAT which means its ok to have a global dest and a
2288 	 * private src.
2289 	 *
2290 	 * L = loopback, P = private, G = global
2291 	 * ----------------------------------------- src    |  dest | result
2292 	 * ----------------------------------------- L     |   L   |    yes
2293 	 * ----------------------------------------- P     |   L   |
2294 	 * yes-v4 no-v6 ----------------------------------------- G     |
2295 	 * L   |    yes ----------------------------------------- L     |
2296 	 * P   |    no ----------------------------------------- P     |   P
2297 	 * |    yes ----------------------------------------- G     |   P
2298 	 * |    yes - May not work -----------------------------------------
2299 	 * L     |   G   |    no ----------------------------------------- P
2300 	 * |   G   |    yes - May not work
2301 	 * ----------------------------------------- G     |   G   |    yes
2302 	 * -----------------------------------------
2303 	 */
2304 
2305 	if (ifa->address.sa.sa_family != fam) {
2306 		/* forget non matching family */
2307 		return (NULL);
2308 	}
2309 	/* Ok the address may be ok */
2310 	if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2311 		dest_is_global = 1;
2312 	}
2313 	if (fam == AF_INET6) {
2314 		/* ok to use deprecated addresses? */
2315 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2316 			return (NULL);
2317 		}
2318 		if (ifa->src_is_priv) {
2319 			/* Special case, linklocal to loop */
2320 			if (dest_is_loop)
2321 				return (NULL);
2322 		}
2323 	}
2324 	/*
2325 	 * Now that we know what is what, implement our table. This could in
2326 	 * theory be done slicker (it used to be), but this is
2327 	 * straightforward and easier to validate :-)
2328 	 */
2329 	if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2330 		return (NULL);
2331 	}
2332 	if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2333 		return (NULL);
2334 	}
2335 	/* its an acceptable address */
2336 	return (ifa);
2337 }
2338 
2339 int
2340 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2341 {
2342 	struct sctp_laddr *laddr;
2343 
2344 	if (stcb == NULL) {
2345 		/* There are no restrictions, no TCB :-) */
2346 		return (0);
2347 	}
2348 	LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2349 		if (laddr->ifa == NULL) {
2350 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2351 			    __FUNCTION__);
2352 			continue;
2353 		}
2354 		if (laddr->ifa == ifa) {
2355 			/* Yes it is on the list */
2356 			return (1);
2357 		}
2358 	}
2359 	return (0);
2360 }
2361 
2362 
2363 int
2364 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2365 {
2366 	struct sctp_laddr *laddr;
2367 
2368 	if (ifa == NULL)
2369 		return (0);
2370 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2371 		if (laddr->ifa == NULL) {
2372 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2373 			    __FUNCTION__);
2374 			continue;
2375 		}
2376 		if ((laddr->ifa == ifa) && laddr->action == 0)
2377 			/* same pointer */
2378 			return (1);
2379 	}
2380 	return (0);
2381 }
2382 
2383 
2384 
2385 static struct sctp_ifa *
2386 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2387     sctp_route_t * ro,
2388     uint32_t vrf_id,
2389     int non_asoc_addr_ok,
2390     uint8_t dest_is_priv,
2391     uint8_t dest_is_loop,
2392     sa_family_t fam)
2393 {
2394 	struct sctp_laddr *laddr, *starting_point;
2395 	void *ifn;
2396 	int resettotop = 0;
2397 	struct sctp_ifn *sctp_ifn;
2398 	struct sctp_ifa *sctp_ifa, *sifa;
2399 	struct sctp_vrf *vrf;
2400 	uint32_t ifn_index;
2401 
2402 	vrf = sctp_find_vrf(vrf_id);
2403 	if (vrf == NULL)
2404 		return (NULL);
2405 
2406 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2407 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2408 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2409 	/*
2410 	 * first question, is the ifn we will emit on in our list, if so, we
2411 	 * want such an address. Note that we first looked for a preferred
2412 	 * address.
2413 	 */
2414 	if (sctp_ifn) {
2415 		/* is a preferred one on the interface we route out? */
2416 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2417 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2418 			    (non_asoc_addr_ok == 0))
2419 				continue;
2420 			sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2421 			    dest_is_loop,
2422 			    dest_is_priv, fam);
2423 			if (sifa == NULL)
2424 				continue;
2425 			if (sctp_is_addr_in_ep(inp, sifa)) {
2426 				atomic_add_int(&sifa->refcount, 1);
2427 				return (sifa);
2428 			}
2429 		}
2430 	}
2431 	/*
2432 	 * ok, now we now need to find one on the list of the addresses. We
2433 	 * can't get one on the emitting interface so let's find first a
2434 	 * preferred one. If not that an acceptable one otherwise... we
2435 	 * return NULL.
2436 	 */
2437 	starting_point = inp->next_addr_touse;
2438 once_again:
2439 	if (inp->next_addr_touse == NULL) {
2440 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2441 		resettotop = 1;
2442 	}
2443 	for (laddr = inp->next_addr_touse; laddr;
2444 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2445 		if (laddr->ifa == NULL) {
2446 			/* address has been removed */
2447 			continue;
2448 		}
2449 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2450 			/* address is being deleted */
2451 			continue;
2452 		}
2453 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2454 		    dest_is_priv, fam);
2455 		if (sifa == NULL)
2456 			continue;
2457 		atomic_add_int(&sifa->refcount, 1);
2458 		return (sifa);
2459 	}
2460 	if (resettotop == 0) {
2461 		inp->next_addr_touse = NULL;
2462 		goto once_again;
2463 	}
2464 	inp->next_addr_touse = starting_point;
2465 	resettotop = 0;
2466 once_again_too:
2467 	if (inp->next_addr_touse == NULL) {
2468 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2469 		resettotop = 1;
2470 	}
2471 	/* ok, what about an acceptable address in the inp */
2472 	for (laddr = inp->next_addr_touse; laddr;
2473 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2474 		if (laddr->ifa == NULL) {
2475 			/* address has been removed */
2476 			continue;
2477 		}
2478 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2479 			/* address is being deleted */
2480 			continue;
2481 		}
2482 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2483 		    dest_is_priv, fam);
2484 		if (sifa == NULL)
2485 			continue;
2486 		atomic_add_int(&sifa->refcount, 1);
2487 		return (sifa);
2488 	}
2489 	if (resettotop == 0) {
2490 		inp->next_addr_touse = NULL;
2491 		goto once_again_too;
2492 	}
2493 	/*
2494 	 * no address bound can be a source for the destination we are in
2495 	 * trouble
2496 	 */
2497 	return (NULL);
2498 }
2499 
2500 
2501 
2502 static struct sctp_ifa *
2503 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2504     struct sctp_tcb *stcb,
2505     struct sctp_nets *net,
2506     sctp_route_t * ro,
2507     uint32_t vrf_id,
2508     uint8_t dest_is_priv,
2509     uint8_t dest_is_loop,
2510     int non_asoc_addr_ok,
2511     sa_family_t fam)
2512 {
2513 	struct sctp_laddr *laddr, *starting_point;
2514 	void *ifn;
2515 	struct sctp_ifn *sctp_ifn;
2516 	struct sctp_ifa *sctp_ifa, *sifa;
2517 	uint8_t start_at_beginning = 0;
2518 	struct sctp_vrf *vrf;
2519 	uint32_t ifn_index;
2520 
2521 	/*
2522 	 * first question, is the ifn we will emit on in our list, if so, we
2523 	 * want that one.
2524 	 */
2525 	vrf = sctp_find_vrf(vrf_id);
2526 	if (vrf == NULL)
2527 		return (NULL);
2528 
2529 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2530 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2531 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2532 
2533 	/*
2534 	 * first question, is the ifn we will emit on in our list?  If so,
2535 	 * we want that one. First we look for a preferred. Second, we go
2536 	 * for an acceptable.
2537 	 */
2538 	if (sctp_ifn) {
2539 		/* first try for a preferred address on the ep */
2540 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2541 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2542 				continue;
2543 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2544 				sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2545 				if (sifa == NULL)
2546 					continue;
2547 				if (((non_asoc_addr_ok == 0) &&
2548 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2549 				    (non_asoc_addr_ok &&
2550 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2551 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2552 					/* on the no-no list */
2553 					continue;
2554 				}
2555 				atomic_add_int(&sifa->refcount, 1);
2556 				return (sifa);
2557 			}
2558 		}
2559 		/* next try for an acceptable address on the ep */
2560 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2561 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2562 				continue;
2563 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2564 				sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2565 				if (sifa == NULL)
2566 					continue;
2567 				if (((non_asoc_addr_ok == 0) &&
2568 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2569 				    (non_asoc_addr_ok &&
2570 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2571 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2572 					/* on the no-no list */
2573 					continue;
2574 				}
2575 				atomic_add_int(&sifa->refcount, 1);
2576 				return (sifa);
2577 			}
2578 		}
2579 
2580 	}
2581 	/*
2582 	 * if we can't find one like that then we must look at all addresses
2583 	 * bound to pick one at first preferable then secondly acceptable.
2584 	 */
2585 	starting_point = stcb->asoc.last_used_address;
2586 sctp_from_the_top:
2587 	if (stcb->asoc.last_used_address == NULL) {
2588 		start_at_beginning = 1;
2589 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2590 	}
2591 	/* search beginning with the last used address */
2592 	for (laddr = stcb->asoc.last_used_address; laddr;
2593 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2594 		if (laddr->ifa == NULL) {
2595 			/* address has been removed */
2596 			continue;
2597 		}
2598 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2599 			/* address is being deleted */
2600 			continue;
2601 		}
2602 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2603 		if (sifa == NULL)
2604 			continue;
2605 		if (((non_asoc_addr_ok == 0) &&
2606 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2607 		    (non_asoc_addr_ok &&
2608 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2609 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2610 			/* on the no-no list */
2611 			continue;
2612 		}
2613 		stcb->asoc.last_used_address = laddr;
2614 		atomic_add_int(&sifa->refcount, 1);
2615 		return (sifa);
2616 	}
2617 	if (start_at_beginning == 0) {
2618 		stcb->asoc.last_used_address = NULL;
2619 		goto sctp_from_the_top;
2620 	}
2621 	/* now try for any higher scope than the destination */
2622 	stcb->asoc.last_used_address = starting_point;
2623 	start_at_beginning = 0;
2624 sctp_from_the_top2:
2625 	if (stcb->asoc.last_used_address == NULL) {
2626 		start_at_beginning = 1;
2627 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2628 	}
2629 	/* search beginning with the last used address */
2630 	for (laddr = stcb->asoc.last_used_address; laddr;
2631 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2632 		if (laddr->ifa == NULL) {
2633 			/* address has been removed */
2634 			continue;
2635 		}
2636 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2637 			/* address is being deleted */
2638 			continue;
2639 		}
2640 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2641 		    dest_is_priv, fam);
2642 		if (sifa == NULL)
2643 			continue;
2644 		if (((non_asoc_addr_ok == 0) &&
2645 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2646 		    (non_asoc_addr_ok &&
2647 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2648 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2649 			/* on the no-no list */
2650 			continue;
2651 		}
2652 		stcb->asoc.last_used_address = laddr;
2653 		atomic_add_int(&sifa->refcount, 1);
2654 		return (sifa);
2655 	}
2656 	if (start_at_beginning == 0) {
2657 		stcb->asoc.last_used_address = NULL;
2658 		goto sctp_from_the_top2;
2659 	}
2660 	return (NULL);
2661 }
2662 
2663 static struct sctp_ifa *
2664 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2665     struct sctp_tcb *stcb,
2666     int non_asoc_addr_ok,
2667     uint8_t dest_is_loop,
2668     uint8_t dest_is_priv,
2669     int addr_wanted,
2670     sa_family_t fam,
2671     sctp_route_t * ro
2672 )
2673 {
2674 	struct sctp_ifa *ifa, *sifa;
2675 	int num_eligible_addr = 0;
2676 
2677 #ifdef INET6
2678 	struct sockaddr_in6 sin6, lsa6;
2679 
2680 	if (fam == AF_INET6) {
2681 		memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2682 		(void)sa6_recoverscope(&sin6);
2683 	}
2684 #endif				/* INET6 */
2685 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2686 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2687 		    (non_asoc_addr_ok == 0))
2688 			continue;
2689 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2690 		    dest_is_priv, fam);
2691 		if (sifa == NULL)
2692 			continue;
2693 #ifdef INET6
2694 		if (fam == AF_INET6 &&
2695 		    dest_is_loop &&
2696 		    sifa->src_is_loop && sifa->src_is_priv) {
2697 			/*
2698 			 * don't allow fe80::1 to be a src on loop ::1, we
2699 			 * don't list it to the peer so we will get an
2700 			 * abort.
2701 			 */
2702 			continue;
2703 		}
2704 		if (fam == AF_INET6 &&
2705 		    IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2706 		    IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2707 			/*
2708 			 * link-local <-> link-local must belong to the same
2709 			 * scope.
2710 			 */
2711 			memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2712 			(void)sa6_recoverscope(&lsa6);
2713 			if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2714 				continue;
2715 			}
2716 		}
2717 #endif				/* INET6 */
2718 
2719 		/*
2720 		 * Check if the IPv6 address matches to next-hop. In the
2721 		 * mobile case, old IPv6 address may be not deleted from the
2722 		 * interface. Then, the interface has previous and new
2723 		 * addresses.  We should use one corresponding to the
2724 		 * next-hop.  (by micchie)
2725 		 */
2726 #ifdef INET6
2727 		if (stcb && fam == AF_INET6 &&
2728 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2729 			if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2730 			    == 0) {
2731 				continue;
2732 			}
2733 		}
2734 #endif
2735 		/* Avoid topologically incorrect IPv4 address */
2736 		if (stcb && fam == AF_INET &&
2737 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2738 			if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2739 				continue;
2740 			}
2741 		}
2742 		if (stcb) {
2743 			if (((non_asoc_addr_ok == 0) &&
2744 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2745 			    (non_asoc_addr_ok &&
2746 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2747 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2748 				/*
2749 				 * It is restricted for some reason..
2750 				 * probably not yet added.
2751 				 */
2752 				continue;
2753 			}
2754 		}
2755 		if (num_eligible_addr >= addr_wanted) {
2756 			return (sifa);
2757 		}
2758 		num_eligible_addr++;
2759 	}
2760 	return (NULL);
2761 }
2762 
2763 
2764 static int
2765 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2766     struct sctp_tcb *stcb,
2767     int non_asoc_addr_ok,
2768     uint8_t dest_is_loop,
2769     uint8_t dest_is_priv,
2770     sa_family_t fam)
2771 {
2772 	struct sctp_ifa *ifa, *sifa;
2773 	int num_eligible_addr = 0;
2774 
2775 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2776 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2777 		    (non_asoc_addr_ok == 0)) {
2778 			continue;
2779 		}
2780 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2781 		    dest_is_priv, fam);
2782 		if (sifa == NULL) {
2783 			continue;
2784 		}
2785 		if (stcb) {
2786 			if (((non_asoc_addr_ok == 0) &&
2787 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2788 			    (non_asoc_addr_ok &&
2789 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2790 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2791 				/*
2792 				 * It is restricted for some reason..
2793 				 * probably not yet added.
2794 				 */
2795 				continue;
2796 			}
2797 		}
2798 		num_eligible_addr++;
2799 	}
2800 	return (num_eligible_addr);
2801 }
2802 
2803 static struct sctp_ifa *
2804 sctp_choose_boundall(struct sctp_inpcb *inp,
2805     struct sctp_tcb *stcb,
2806     struct sctp_nets *net,
2807     sctp_route_t * ro,
2808     uint32_t vrf_id,
2809     uint8_t dest_is_priv,
2810     uint8_t dest_is_loop,
2811     int non_asoc_addr_ok,
2812     sa_family_t fam)
2813 {
2814 	int cur_addr_num = 0, num_preferred = 0;
2815 	void *ifn;
2816 	struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2817 	struct sctp_ifa *sctp_ifa, *sifa;
2818 	uint32_t ifn_index;
2819 	struct sctp_vrf *vrf;
2820 
2821 	/*-
2822 	 * For boundall we can use any address in the association.
2823 	 * If non_asoc_addr_ok is set we can use any address (at least in
2824 	 * theory). So we look for preferred addresses first. If we find one,
2825 	 * we use it. Otherwise we next try to get an address on the
2826 	 * interface, which we should be able to do (unless non_asoc_addr_ok
2827 	 * is false and we are routed out that way). In these cases where we
2828 	 * can't use the address of the interface we go through all the
2829 	 * ifn's looking for an address we can use and fill that in. Punting
2830 	 * means we send back address 0, which will probably cause problems
2831 	 * actually since then IP will fill in the address of the route ifn,
2832 	 * which means we probably already rejected it.. i.e. here comes an
2833 	 * abort :-<.
2834 	 */
2835 	vrf = sctp_find_vrf(vrf_id);
2836 	if (vrf == NULL)
2837 		return (NULL);
2838 
2839 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2840 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2841 	emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2842 	if (sctp_ifn == NULL) {
2843 		/* ?? We don't have this guy ?? */
2844 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2845 		goto bound_all_plan_b;
2846 	}
2847 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2848 	    ifn_index, sctp_ifn->ifn_name);
2849 
2850 	if (net) {
2851 		cur_addr_num = net->indx_of_eligible_next_to_use;
2852 	}
2853 	num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2854 	    stcb,
2855 	    non_asoc_addr_ok,
2856 	    dest_is_loop,
2857 	    dest_is_priv, fam);
2858 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2859 	    num_preferred, sctp_ifn->ifn_name);
2860 	if (num_preferred == 0) {
2861 		/*
2862 		 * no eligible addresses, we must use some other interface
2863 		 * address if we can find one.
2864 		 */
2865 		goto bound_all_plan_b;
2866 	}
2867 	/*
2868 	 * Ok we have num_eligible_addr set with how many we can use, this
2869 	 * may vary from call to call due to addresses being deprecated
2870 	 * etc..
2871 	 */
2872 	if (cur_addr_num >= num_preferred) {
2873 		cur_addr_num = 0;
2874 	}
2875 	/*
2876 	 * select the nth address from the list (where cur_addr_num is the
2877 	 * nth) and 0 is the first one, 1 is the second one etc...
2878 	 */
2879 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2880 
2881 	sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2882 	    dest_is_priv, cur_addr_num, fam, ro);
2883 
2884 	/* if sctp_ifa is NULL something changed??, fall to plan b. */
2885 	if (sctp_ifa) {
2886 		atomic_add_int(&sctp_ifa->refcount, 1);
2887 		if (net) {
2888 			/* save off where the next one we will want */
2889 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2890 		}
2891 		return (sctp_ifa);
2892 	}
2893 	/*
2894 	 * plan_b: Look at all interfaces and find a preferred address. If
2895 	 * no preferred fall through to plan_c.
2896 	 */
2897 bound_all_plan_b:
2898 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2899 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2900 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2901 		    sctp_ifn->ifn_name);
2902 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2903 			/* wrong base scope */
2904 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2905 			continue;
2906 		}
2907 		if ((sctp_ifn == looked_at) && looked_at) {
2908 			/* already looked at this guy */
2909 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2910 			continue;
2911 		}
2912 		num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2913 		    dest_is_loop, dest_is_priv, fam);
2914 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
2915 		    "Found ifn:%p %d preferred source addresses\n",
2916 		    ifn, num_preferred);
2917 		if (num_preferred == 0) {
2918 			/* None on this interface. */
2919 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2920 			continue;
2921 		}
2922 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
2923 		    "num preferred:%d on interface:%p cur_addr_num:%d\n",
2924 		    num_preferred, sctp_ifn, cur_addr_num);
2925 
2926 		/*
2927 		 * Ok we have num_eligible_addr set with how many we can
2928 		 * use, this may vary from call to call due to addresses
2929 		 * being deprecated etc..
2930 		 */
2931 		if (cur_addr_num >= num_preferred) {
2932 			cur_addr_num = 0;
2933 		}
2934 		sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2935 		    dest_is_priv, cur_addr_num, fam, ro);
2936 		if (sifa == NULL)
2937 			continue;
2938 		if (net) {
2939 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2940 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
2941 			    cur_addr_num);
2942 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
2943 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
2944 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
2945 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
2946 		}
2947 		atomic_add_int(&sifa->refcount, 1);
2948 		return (sifa);
2949 
2950 	}
2951 
2952 	/* plan_c: do we have an acceptable address on the emit interface */
2953 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
2954 	if (emit_ifn == NULL) {
2955 		goto plan_d;
2956 	}
2957 	LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
2958 		if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2959 		    (non_asoc_addr_ok == 0))
2960 			continue;
2961 		sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
2962 		    dest_is_priv, fam);
2963 		if (sifa == NULL)
2964 			continue;
2965 		if (stcb) {
2966 			if (((non_asoc_addr_ok == 0) &&
2967 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2968 			    (non_asoc_addr_ok &&
2969 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2970 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2971 				/*
2972 				 * It is restricted for some reason..
2973 				 * probably not yet added.
2974 				 */
2975 				continue;
2976 			}
2977 		}
2978 		atomic_add_int(&sifa->refcount, 1);
2979 		return (sifa);
2980 	}
2981 plan_d:
2982 	/*
2983 	 * plan_d: We are in trouble. No preferred address on the emit
2984 	 * interface. And not even a preferred address on all interfaces. Go
2985 	 * out and see if we can find an acceptable address somewhere
2986 	 * amongst all interfaces.
2987 	 */
2988 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
2989 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2990 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2991 			/* wrong base scope */
2992 			continue;
2993 		}
2994 		if ((sctp_ifn == looked_at) && looked_at)
2995 			/* already looked at this guy */
2996 			continue;
2997 
2998 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2999 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3000 			    (non_asoc_addr_ok == 0))
3001 				continue;
3002 			sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3003 			    dest_is_loop,
3004 			    dest_is_priv, fam);
3005 			if (sifa == NULL)
3006 				continue;
3007 			if (stcb) {
3008 				if (((non_asoc_addr_ok == 0) &&
3009 				    (sctp_is_addr_restricted(stcb, sifa))) ||
3010 				    (non_asoc_addr_ok &&
3011 				    (sctp_is_addr_restricted(stcb, sifa)) &&
3012 				    (!sctp_is_addr_pending(stcb, sifa)))) {
3013 					/*
3014 					 * It is restricted for some
3015 					 * reason.. probably not yet added.
3016 					 */
3017 					continue;
3018 				}
3019 			}
3020 			atomic_add_int(&sifa->refcount, 1);
3021 			return (sifa);
3022 		}
3023 	}
3024 	/*
3025 	 * Ok we can find NO address to source from that is not on our
3026 	 * restricted list and non_asoc_address is NOT ok, or it is on our
3027 	 * restricted list. We can't source to it :-(
3028 	 */
3029 	return (NULL);
3030 }
3031 
3032 
3033 
3034 /* tcb may be NULL */
3035 struct sctp_ifa *
3036 sctp_source_address_selection(struct sctp_inpcb *inp,
3037     struct sctp_tcb *stcb,
3038     sctp_route_t * ro,
3039     struct sctp_nets *net,
3040     int non_asoc_addr_ok, uint32_t vrf_id)
3041 {
3042 	struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3043 
3044 #ifdef INET6
3045 	struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3046 
3047 #endif
3048 	struct sctp_ifa *answer;
3049 	uint8_t dest_is_priv, dest_is_loop;
3050 	sa_family_t fam;
3051 
3052 	/*-
3053 	 * Rules: - Find the route if needed, cache if I can. - Look at
3054 	 * interface address in route, Is it in the bound list. If so we
3055 	 * have the best source. - If not we must rotate amongst the
3056 	 * addresses.
3057 	 *
3058 	 * Cavets and issues
3059 	 *
3060 	 * Do we need to pay attention to scope. We can have a private address
3061 	 * or a global address we are sourcing or sending to. So if we draw
3062 	 * it out
3063 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3064 	 * For V4
3065 	 * ------------------------------------------
3066 	 *      source     *      dest  *  result
3067 	 * -----------------------------------------
3068 	 * <a>  Private    *    Global  *  NAT
3069 	 * -----------------------------------------
3070 	 * <b>  Private    *    Private *  No problem
3071 	 * -----------------------------------------
3072 	 * <c>  Global     *    Private *  Huh, How will this work?
3073 	 * -----------------------------------------
3074 	 * <d>  Global     *    Global  *  No Problem
3075 	 *------------------------------------------
3076 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3077 	 * For V6
3078 	 *------------------------------------------
3079 	 *      source     *      dest  *  result
3080 	 * -----------------------------------------
3081 	 * <a>  Linklocal  *    Global  *
3082 	 * -----------------------------------------
3083 	 * <b>  Linklocal  * Linklocal  *  No problem
3084 	 * -----------------------------------------
3085 	 * <c>  Global     * Linklocal  *  Huh, How will this work?
3086 	 * -----------------------------------------
3087 	 * <d>  Global     *    Global  *  No Problem
3088 	 *------------------------------------------
3089 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3090 	 *
3091 	 * And then we add to that what happens if there are multiple addresses
3092 	 * assigned to an interface. Remember the ifa on a ifn is a linked
3093 	 * list of addresses. So one interface can have more than one IP
3094 	 * address. What happens if we have both a private and a global
3095 	 * address? Do we then use context of destination to sort out which
3096 	 * one is best? And what about NAT's sending P->G may get you a NAT
3097 	 * translation, or should you select the G thats on the interface in
3098 	 * preference.
3099 	 *
3100 	 * Decisions:
3101 	 *
3102 	 * - count the number of addresses on the interface.
3103 	 * - if it is one, no problem except case <c>.
3104 	 *   For <a> we will assume a NAT out there.
3105 	 * - if there are more than one, then we need to worry about scope P
3106 	 *   or G. We should prefer G -> G and P -> P if possible.
3107 	 *   Then as a secondary fall back to mixed types G->P being a last
3108 	 *   ditch one.
3109 	 * - The above all works for bound all, but bound specific we need to
3110 	 *   use the same concept but instead only consider the bound
3111 	 *   addresses. If the bound set is NOT assigned to the interface then
3112 	 *   we must use rotation amongst the bound addresses..
3113 	 */
3114 	if (ro->ro_rt == NULL) {
3115 		/*
3116 		 * Need a route to cache.
3117 		 */
3118 		SCTP_RTALLOC(ro, vrf_id);
3119 	}
3120 	if (ro->ro_rt == NULL) {
3121 		return (NULL);
3122 	}
3123 	fam = to->sin_family;
3124 	dest_is_priv = dest_is_loop = 0;
3125 	/* Setup our scopes for the destination */
3126 	switch (fam) {
3127 	case AF_INET:
3128 		/* Scope based on outbound address */
3129 		if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3130 			dest_is_loop = 1;
3131 			if (net != NULL) {
3132 				/* mark it as local */
3133 				net->addr_is_local = 1;
3134 			}
3135 		} else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3136 			dest_is_priv = 1;
3137 		}
3138 		break;
3139 #ifdef INET6
3140 	case AF_INET6:
3141 		/* Scope based on outbound address */
3142 		if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3143 		    SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3144 			/*
3145 			 * If the address is a loopback address, which
3146 			 * consists of "::1" OR "fe80::1%lo0", we are
3147 			 * loopback scope. But we don't use dest_is_priv
3148 			 * (link local addresses).
3149 			 */
3150 			dest_is_loop = 1;
3151 			if (net != NULL) {
3152 				/* mark it as local */
3153 				net->addr_is_local = 1;
3154 			}
3155 		} else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3156 			dest_is_priv = 1;
3157 		}
3158 		break;
3159 #endif
3160 	}
3161 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3162 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
3163 	SCTP_IPI_ADDR_RLOCK();
3164 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3165 		/*
3166 		 * Bound all case
3167 		 */
3168 		answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3169 		    dest_is_priv, dest_is_loop,
3170 		    non_asoc_addr_ok, fam);
3171 		SCTP_IPI_ADDR_RUNLOCK();
3172 		return (answer);
3173 	}
3174 	/*
3175 	 * Subset bound case
3176 	 */
3177 	if (stcb) {
3178 		answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
3179 		    vrf_id, dest_is_priv,
3180 		    dest_is_loop,
3181 		    non_asoc_addr_ok, fam);
3182 	} else {
3183 		answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3184 		    non_asoc_addr_ok,
3185 		    dest_is_priv,
3186 		    dest_is_loop, fam);
3187 	}
3188 	SCTP_IPI_ADDR_RUNLOCK();
3189 	return (answer);
3190 }
3191 
3192 static int
3193 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
3194 {
3195 	struct cmsghdr cmh;
3196 	int tlen, at;
3197 
3198 	tlen = SCTP_BUF_LEN(control);
3199 	at = 0;
3200 	/*
3201 	 * Independent of how many mbufs, find the c_type inside the control
3202 	 * structure and copy out the data.
3203 	 */
3204 	while (at < tlen) {
3205 		if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3206 			/* not enough room for one more we are done. */
3207 			return (0);
3208 		}
3209 		m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3210 		if (((int)cmh.cmsg_len + at) > tlen) {
3211 			/*
3212 			 * this is real messed up since there is not enough
3213 			 * data here to cover the cmsg header. We are done.
3214 			 */
3215 			return (0);
3216 		}
3217 		if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3218 		    (c_type == cmh.cmsg_type)) {
3219 			/* found the one we want, copy it out */
3220 			at += CMSG_ALIGN(sizeof(struct cmsghdr));
3221 			if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3222 				/*
3223 				 * space of cmsg_len after header not big
3224 				 * enough
3225 				 */
3226 				return (0);
3227 			}
3228 			m_copydata(control, at, cpsize, data);
3229 			return (1);
3230 		} else {
3231 			at += CMSG_ALIGN(cmh.cmsg_len);
3232 			if (cmh.cmsg_len == 0) {
3233 				break;
3234 			}
3235 		}
3236 	}
3237 	/* not found */
3238 	return (0);
3239 }
3240 
3241 static struct mbuf *
3242 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
3243     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3244 {
3245 	struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3246 	struct sctp_state_cookie *stc;
3247 	struct sctp_paramhdr *ph;
3248 	uint8_t *foo;
3249 	int sig_offset;
3250 	uint16_t cookie_sz;
3251 
3252 	mret = NULL;
3253 	mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3254 	    sizeof(struct sctp_paramhdr)), 0,
3255 	    M_DONTWAIT, 1, MT_DATA);
3256 	if (mret == NULL) {
3257 		return (NULL);
3258 	}
3259 	copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3260 	if (copy_init == NULL) {
3261 		sctp_m_freem(mret);
3262 		return (NULL);
3263 	}
3264 #ifdef SCTP_MBUF_LOGGING
3265 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3266 		struct mbuf *mat;
3267 
3268 		mat = copy_init;
3269 		while (mat) {
3270 			if (SCTP_BUF_IS_EXTENDED(mat)) {
3271 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3272 			}
3273 			mat = SCTP_BUF_NEXT(mat);
3274 		}
3275 	}
3276 #endif
3277 	copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3278 	    M_DONTWAIT);
3279 	if (copy_initack == NULL) {
3280 		sctp_m_freem(mret);
3281 		sctp_m_freem(copy_init);
3282 		return (NULL);
3283 	}
3284 #ifdef SCTP_MBUF_LOGGING
3285 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3286 		struct mbuf *mat;
3287 
3288 		mat = copy_initack;
3289 		while (mat) {
3290 			if (SCTP_BUF_IS_EXTENDED(mat)) {
3291 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3292 			}
3293 			mat = SCTP_BUF_NEXT(mat);
3294 		}
3295 	}
3296 #endif
3297 	/* easy side we just drop it on the end */
3298 	ph = mtod(mret, struct sctp_paramhdr *);
3299 	SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3300 	    sizeof(struct sctp_paramhdr);
3301 	stc = (struct sctp_state_cookie *)((caddr_t)ph +
3302 	    sizeof(struct sctp_paramhdr));
3303 	ph->param_type = htons(SCTP_STATE_COOKIE);
3304 	ph->param_length = 0;	/* fill in at the end */
3305 	/* Fill in the stc cookie data */
3306 	memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3307 
3308 	/* tack the INIT and then the INIT-ACK onto the chain */
3309 	cookie_sz = 0;
3310 	m_at = mret;
3311 	for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3312 		cookie_sz += SCTP_BUF_LEN(m_at);
3313 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3314 			SCTP_BUF_NEXT(m_at) = copy_init;
3315 			break;
3316 		}
3317 	}
3318 
3319 	for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3320 		cookie_sz += SCTP_BUF_LEN(m_at);
3321 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3322 			SCTP_BUF_NEXT(m_at) = copy_initack;
3323 			break;
3324 		}
3325 	}
3326 
3327 	for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3328 		cookie_sz += SCTP_BUF_LEN(m_at);
3329 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3330 			break;
3331 		}
3332 	}
3333 	sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3334 	if (sig == NULL) {
3335 		/* no space, so free the entire chain */
3336 		sctp_m_freem(mret);
3337 		return (NULL);
3338 	}
3339 	SCTP_BUF_LEN(sig) = 0;
3340 	SCTP_BUF_NEXT(m_at) = sig;
3341 	sig_offset = 0;
3342 	foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3343 	memset(foo, 0, SCTP_SIGNATURE_SIZE);
3344 	*signature = foo;
3345 	SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3346 	cookie_sz += SCTP_SIGNATURE_SIZE;
3347 	ph->param_length = htons(cookie_sz);
3348 	return (mret);
3349 }
3350 
3351 
3352 static uint8_t
3353 sctp_get_ect(struct sctp_tcb *stcb,
3354     struct sctp_tmit_chunk *chk)
3355 {
3356 	uint8_t this_random;
3357 
3358 	/* Huh? */
3359 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 0)
3360 		return (0);
3361 
3362 	if (SCTP_BASE_SYSCTL(sctp_ecn_nonce) == 0)
3363 		/* no nonce, always return ECT0 */
3364 		return (SCTP_ECT0_BIT);
3365 
3366 	if (stcb->asoc.peer_supports_ecn_nonce == 0) {
3367 		/* Peer does NOT support it, so we send a ECT0 only */
3368 		return (SCTP_ECT0_BIT);
3369 	}
3370 	if (chk == NULL)
3371 		return (SCTP_ECT0_BIT);
3372 
3373 	if ((stcb->asoc.hb_random_idx > 3) ||
3374 	    ((stcb->asoc.hb_random_idx == 3) &&
3375 	    (stcb->asoc.hb_ect_randombit > 7))) {
3376 		uint32_t rndval;
3377 
3378 warp_drive_sa:
3379 		rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
3380 		memcpy(stcb->asoc.hb_random_values, &rndval,
3381 		    sizeof(stcb->asoc.hb_random_values));
3382 		this_random = stcb->asoc.hb_random_values[0];
3383 		stcb->asoc.hb_random_idx = 0;
3384 		stcb->asoc.hb_ect_randombit = 0;
3385 	} else {
3386 		if (stcb->asoc.hb_ect_randombit > 7) {
3387 			stcb->asoc.hb_ect_randombit = 0;
3388 			stcb->asoc.hb_random_idx++;
3389 			if (stcb->asoc.hb_random_idx > 3) {
3390 				goto warp_drive_sa;
3391 			}
3392 		}
3393 		this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
3394 	}
3395 	if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
3396 		if (chk != NULL)
3397 			/* ECN Nonce stuff */
3398 			chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
3399 		stcb->asoc.hb_ect_randombit++;
3400 		return (SCTP_ECT1_BIT);
3401 	} else {
3402 		stcb->asoc.hb_ect_randombit++;
3403 		return (SCTP_ECT0_BIT);
3404 	}
3405 }
3406 
3407 static int
3408 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3409     struct sctp_tcb *stcb,	/* may be NULL */
3410     struct sctp_nets *net,
3411     struct sockaddr *to,
3412     struct mbuf *m,
3413     uint32_t auth_offset,
3414     struct sctp_auth_chunk *auth,
3415     uint16_t auth_keyid,
3416     int nofragment_flag,
3417     int ecn_ok,
3418     struct sctp_tmit_chunk *chk,
3419     int out_of_asoc_ok,
3420     uint16_t src_port,
3421     uint16_t dest_port,
3422     uint32_t v_tag,
3423     uint16_t port,
3424     int so_locked,
3425 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3426     SCTP_UNUSED
3427 #endif
3428     union sctp_sockstore *over_addr
3429 )
3430 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3431 {
3432 	/*
3433 	 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
3434 	 * header WITH an SCTPHDR but no IP header, endpoint inp and sa
3435 	 * structure: - fill in the HMAC digest of any AUTH chunk in the
3436 	 * packet. - calculate and fill in the SCTP checksum. - prepend an
3437 	 * IP address header. - if boundall use INADDR_ANY. - if
3438 	 * boundspecific do source address selection. - set fragmentation
3439 	 * option for ipV4. - On return from IP output, check/adjust mtu
3440 	 * size of output interface and smallest_mtu size as well.
3441 	 */
3442 	/* Will need ifdefs around this */
3443 	struct mbuf *o_pak;
3444 	struct mbuf *newm;
3445 	struct sctphdr *sctphdr;
3446 	int packet_length;
3447 	int ret;
3448 	uint32_t vrf_id;
3449 	sctp_route_t *ro = NULL;
3450 	struct udphdr *udp = NULL;
3451 
3452 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3453 	struct socket *so = NULL;
3454 
3455 #endif
3456 
3457 	if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3458 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3459 		sctp_m_freem(m);
3460 		return (EFAULT);
3461 	}
3462 	if (stcb) {
3463 		vrf_id = stcb->asoc.vrf_id;
3464 	} else {
3465 		vrf_id = inp->def_vrf_id;
3466 	}
3467 
3468 	/* fill in the HMAC digest for any AUTH chunk in the packet */
3469 	if ((auth != NULL) && (stcb != NULL)) {
3470 		sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
3471 	}
3472 	if (to->sa_family == AF_INET) {
3473 		struct ip *ip = NULL;
3474 		sctp_route_t iproute;
3475 		uint8_t tos_value;
3476 		int len;
3477 
3478 		len = sizeof(struct ip) + sizeof(struct sctphdr);
3479 		if (port) {
3480 			len += sizeof(struct udphdr);
3481 		}
3482 		newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3483 		if (newm == NULL) {
3484 			sctp_m_freem(m);
3485 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3486 			return (ENOMEM);
3487 		}
3488 		SCTP_ALIGN_TO_END(newm, len);
3489 		SCTP_BUF_LEN(newm) = len;
3490 		SCTP_BUF_NEXT(newm) = m;
3491 		m = newm;
3492 		packet_length = sctp_calculate_len(m);
3493 		ip = mtod(m, struct ip *);
3494 		ip->ip_v = IPVERSION;
3495 		ip->ip_hl = (sizeof(struct ip) >> 2);
3496 		if (net) {
3497 			tos_value = net->tos_flowlabel & 0x000000ff;
3498 		} else {
3499 			tos_value = inp->ip_inp.inp.inp_ip_tos;
3500 		}
3501 		if ((nofragment_flag) && (port == 0)) {
3502 			ip->ip_off = IP_DF;
3503 		} else
3504 			ip->ip_off = 0;
3505 
3506 		/* FreeBSD has a function for ip_id's */
3507 		ip->ip_id = ip_newid();
3508 
3509 		ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3510 		ip->ip_len = packet_length;
3511 		if (stcb) {
3512 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3513 				/* Enable ECN */
3514 				ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
3515 			} else {
3516 				/* No ECN */
3517 				ip->ip_tos = (u_char)(tos_value & 0xfc);
3518 			}
3519 		} else {
3520 			/* no association at all */
3521 			ip->ip_tos = (tos_value & 0xfc);
3522 		}
3523 		if (port) {
3524 			ip->ip_p = IPPROTO_UDP;
3525 		} else {
3526 			ip->ip_p = IPPROTO_SCTP;
3527 		}
3528 		ip->ip_sum = 0;
3529 		if (net == NULL) {
3530 			ro = &iproute;
3531 			memset(&iproute, 0, sizeof(iproute));
3532 			memcpy(&ro->ro_dst, to, to->sa_len);
3533 		} else {
3534 			ro = (sctp_route_t *) & net->ro;
3535 		}
3536 		/* Now the address selection part */
3537 		ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
3538 
3539 		/* call the routine to select the src address */
3540 		if (net && out_of_asoc_ok == 0) {
3541 			if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3542 				sctp_free_ifa(net->ro._s_addr);
3543 				net->ro._s_addr = NULL;
3544 				net->src_addr_selected = 0;
3545 				if (ro->ro_rt) {
3546 					RTFREE(ro->ro_rt);
3547 					ro->ro_rt = NULL;
3548 				}
3549 			}
3550 			if (net->src_addr_selected == 0) {
3551 				/* Cache the source address */
3552 				net->ro._s_addr = sctp_source_address_selection(inp, stcb,
3553 				    ro, net, 0,
3554 				    vrf_id);
3555 				net->src_addr_selected = 1;
3556 			}
3557 			if (net->ro._s_addr == NULL) {
3558 				/* No route to host */
3559 				net->src_addr_selected = 0;
3560 				goto no_route;
3561 			}
3562 			ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
3563 		} else {
3564 			if (over_addr == NULL) {
3565 				struct sctp_ifa *_lsrc;
3566 
3567 				_lsrc = sctp_source_address_selection(inp, stcb, ro,
3568 				    net,
3569 				    out_of_asoc_ok,
3570 				    vrf_id);
3571 				if (_lsrc == NULL) {
3572 					goto no_route;
3573 				}
3574 				ip->ip_src = _lsrc->address.sin.sin_addr;
3575 				sctp_free_ifa(_lsrc);
3576 			} else {
3577 				ip->ip_src = over_addr->sin.sin_addr;
3578 				SCTP_RTALLOC(ro, vrf_id);
3579 			}
3580 		}
3581 		if (port) {
3582 			udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
3583 			udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3584 			udp->uh_dport = port;
3585 			udp->uh_ulen = htons(packet_length - sizeof(struct ip));
3586 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
3587 			sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
3588 		} else {
3589 			sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
3590 		}
3591 
3592 		sctphdr->src_port = src_port;
3593 		sctphdr->dest_port = dest_port;
3594 		sctphdr->v_tag = v_tag;
3595 		sctphdr->checksum = 0;
3596 
3597 		/*
3598 		 * If source address selection fails and we find no route
3599 		 * then the ip_output should fail as well with a
3600 		 * NO_ROUTE_TO_HOST type error. We probably should catch
3601 		 * that somewhere and abort the association right away
3602 		 * (assuming this is an INIT being sent).
3603 		 */
3604 		if ((ro->ro_rt == NULL)) {
3605 			/*
3606 			 * src addr selection failed to find a route (or
3607 			 * valid source addr), so we can't get there from
3608 			 * here (yet)!
3609 			 */
3610 	no_route:
3611 			SCTPDBG(SCTP_DEBUG_OUTPUT1,
3612 			    "%s: dropped packet - no valid source addr\n",
3613 			    __FUNCTION__);
3614 			if (net) {
3615 				SCTPDBG(SCTP_DEBUG_OUTPUT1,
3616 				    "Destination was ");
3617 				SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
3618 				    &net->ro._l_addr.sa);
3619 				if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3620 					if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3621 						SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3622 						sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3623 						    stcb,
3624 						    SCTP_FAILED_THRESHOLD,
3625 						    (void *)net,
3626 						    so_locked);
3627 						net->dest_state &= ~SCTP_ADDR_REACHABLE;
3628 						net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
3629 						/*
3630 						 * JRS 5/14/07 - If a
3631 						 * destination is
3632 						 * unreachable, the PF bit
3633 						 * is turned off.  This
3634 						 * allows an unambiguous use
3635 						 * of the PF bit for
3636 						 * destinations that are
3637 						 * reachable but potentially
3638 						 * failed. If the
3639 						 * destination is set to the
3640 						 * unreachable state, also
3641 						 * set the destination to
3642 						 * the PF state.
3643 						 */
3644 						/*
3645 						 * Add debug message here if
3646 						 * destination is not in PF
3647 						 * state.
3648 						 */
3649 						/*
3650 						 * Stop any running T3
3651 						 * timers here?
3652 						 */
3653 						if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
3654 							net->dest_state &= ~SCTP_ADDR_PF;
3655 							SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
3656 							    net);
3657 						}
3658 					}
3659 				}
3660 				if (stcb) {
3661 					if (net == stcb->asoc.primary_destination) {
3662 						/* need a new primary */
3663 						struct sctp_nets *alt;
3664 
3665 						alt = sctp_find_alternate_net(stcb, net, 0);
3666 						if (alt != net) {
3667 							if (sctp_set_primary_addr(stcb,
3668 							    (struct sockaddr *)NULL,
3669 							    alt) == 0) {
3670 								net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
3671 								if (net->ro._s_addr) {
3672 									sctp_free_ifa(net->ro._s_addr);
3673 									net->ro._s_addr = NULL;
3674 								}
3675 								net->src_addr_selected = 0;
3676 							}
3677 						}
3678 					}
3679 				}
3680 			}
3681 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3682 			sctp_m_freem(m);
3683 			return (EHOSTUNREACH);
3684 		}
3685 		if (ro != &iproute) {
3686 			memcpy(&iproute, ro, sizeof(*ro));
3687 		}
3688 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
3689 		    (uint32_t) (ntohl(ip->ip_src.s_addr)));
3690 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
3691 		    (uint32_t) (ntohl(ip->ip_dst.s_addr)));
3692 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
3693 		    ro->ro_rt);
3694 
3695 		if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3696 			/* failed to prepend data, give up */
3697 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3698 			sctp_m_freem(m);
3699 			return (ENOMEM);
3700 		}
3701 #ifdef  SCTP_PACKET_LOGGING
3702 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
3703 			sctp_packet_log(m, packet_length);
3704 #endif
3705 		SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3706 		if (port) {
3707 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3708 			    (stcb) &&
3709 			    (stcb->asoc.loopback_scope))) {
3710 				sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
3711 				SCTP_STAT_INCR(sctps_sendswcrc);
3712 			} else {
3713 				SCTP_STAT_INCR(sctps_sendnocrc);
3714 			}
3715 			SCTP_ENABLE_UDP_CSUM(o_pak);
3716 		} else {
3717 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3718 			    (stcb) &&
3719 			    (stcb->asoc.loopback_scope))) {
3720 				m->m_pkthdr.csum_flags = CSUM_SCTP;
3721 				m->m_pkthdr.csum_data = 0;
3722 				SCTP_STAT_INCR(sctps_sendhwcrc);
3723 			} else {
3724 				SCTP_STAT_INCR(sctps_sendnocrc);
3725 			}
3726 		}
3727 		/* send it out.  table id is taken from stcb */
3728 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3729 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3730 			so = SCTP_INP_SO(inp);
3731 			SCTP_SOCKET_UNLOCK(so, 0);
3732 		}
3733 #endif
3734 		SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
3735 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3736 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3737 			atomic_add_int(&stcb->asoc.refcnt, 1);
3738 			SCTP_TCB_UNLOCK(stcb);
3739 			SCTP_SOCKET_LOCK(so, 0);
3740 			SCTP_TCB_LOCK(stcb);
3741 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
3742 		}
3743 #endif
3744 		SCTP_STAT_INCR(sctps_sendpackets);
3745 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3746 		if (ret)
3747 			SCTP_STAT_INCR(sctps_senderrors);
3748 
3749 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
3750 		if (net == NULL) {
3751 			/* free tempy routes */
3752 			if (ro->ro_rt) {
3753 				RTFREE(ro->ro_rt);
3754 				ro->ro_rt = NULL;
3755 			}
3756 		} else {
3757 			/* PMTU check versus smallest asoc MTU goes here */
3758 			if ((ro->ro_rt != NULL) &&
3759 			    (net->ro._s_addr)) {
3760 				uint32_t mtu;
3761 
3762 				mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3763 				if (net->port) {
3764 					mtu -= sizeof(struct udphdr);
3765 				}
3766 				if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
3767 #ifdef SCTP_PRINT_FOR_B_AND_M
3768 					SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n", mtu);
3769 #endif
3770 					sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3771 					net->mtu = mtu;
3772 				}
3773 			} else if (ro->ro_rt == NULL) {
3774 				/* route was freed */
3775 				if (net->ro._s_addr &&
3776 				    net->src_addr_selected) {
3777 					sctp_free_ifa(net->ro._s_addr);
3778 					net->ro._s_addr = NULL;
3779 				}
3780 				net->src_addr_selected = 0;
3781 			}
3782 		}
3783 		return (ret);
3784 	}
3785 #ifdef INET6
3786 	else if (to->sa_family == AF_INET6) {
3787 		uint32_t flowlabel;
3788 		struct ip6_hdr *ip6h;
3789 		struct route_in6 ip6route;
3790 		struct ifnet *ifp;
3791 		u_char flowTop;
3792 		uint16_t flowBottom;
3793 		u_char tosBottom, tosTop;
3794 		struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
3795 		int prev_scope = 0;
3796 		struct sockaddr_in6 lsa6_storage;
3797 		int error;
3798 		u_short prev_port = 0;
3799 		int len;
3800 
3801 		if (net != NULL) {
3802 			flowlabel = net->tos_flowlabel;
3803 		} else {
3804 			flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
3805 		}
3806 
3807 		len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr);
3808 		if (port) {
3809 			len += sizeof(struct udphdr);
3810 		}
3811 		newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3812 		if (newm == NULL) {
3813 			sctp_m_freem(m);
3814 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3815 			return (ENOMEM);
3816 		}
3817 		SCTP_ALIGN_TO_END(newm, len);
3818 		SCTP_BUF_LEN(newm) = len;
3819 		SCTP_BUF_NEXT(newm) = m;
3820 		m = newm;
3821 		packet_length = sctp_calculate_len(m);
3822 
3823 		ip6h = mtod(m, struct ip6_hdr *);
3824 		/*
3825 		 * We assume here that inp_flow is in host byte order within
3826 		 * the TCB!
3827 		 */
3828 		flowBottom = flowlabel & 0x0000ffff;
3829 		flowTop = ((flowlabel & 0x000f0000) >> 16);
3830 		tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
3831 		/* protect *sin6 from overwrite */
3832 		sin6 = (struct sockaddr_in6 *)to;
3833 		tmp = *sin6;
3834 		sin6 = &tmp;
3835 
3836 		/* KAME hack: embed scopeid */
3837 		if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3838 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3839 			return (EINVAL);
3840 		}
3841 		if (net == NULL) {
3842 			memset(&ip6route, 0, sizeof(ip6route));
3843 			ro = (sctp_route_t *) & ip6route;
3844 			memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
3845 		} else {
3846 			ro = (sctp_route_t *) & net->ro;
3847 		}
3848 		if (stcb != NULL) {
3849 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3850 				/* Enable ECN */
3851 				tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
3852 			} else {
3853 				/* No ECN */
3854 				tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3855 			}
3856 		} else {
3857 			/* we could get no asoc if it is a O-O-T-B packet */
3858 			tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3859 		}
3860 		ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
3861 		if (port) {
3862 			ip6h->ip6_nxt = IPPROTO_UDP;
3863 		} else {
3864 			ip6h->ip6_nxt = IPPROTO_SCTP;
3865 		}
3866 		ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
3867 		ip6h->ip6_dst = sin6->sin6_addr;
3868 
3869 		/*
3870 		 * Add SRC address selection here: we can only reuse to a
3871 		 * limited degree the kame src-addr-sel, since we can try
3872 		 * their selection but it may not be bound.
3873 		 */
3874 		bzero(&lsa6_tmp, sizeof(lsa6_tmp));
3875 		lsa6_tmp.sin6_family = AF_INET6;
3876 		lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
3877 		lsa6 = &lsa6_tmp;
3878 		if (net && out_of_asoc_ok == 0) {
3879 			if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3880 				sctp_free_ifa(net->ro._s_addr);
3881 				net->ro._s_addr = NULL;
3882 				net->src_addr_selected = 0;
3883 				if (ro->ro_rt) {
3884 					RTFREE(ro->ro_rt);
3885 					ro->ro_rt = NULL;
3886 				}
3887 			}
3888 			if (net->src_addr_selected == 0) {
3889 				sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3890 				/* KAME hack: embed scopeid */
3891 				if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3892 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3893 					return (EINVAL);
3894 				}
3895 				/* Cache the source address */
3896 				net->ro._s_addr = sctp_source_address_selection(inp,
3897 				    stcb,
3898 				    ro,
3899 				    net,
3900 				    0,
3901 				    vrf_id);
3902 				(void)sa6_recoverscope(sin6);
3903 				net->src_addr_selected = 1;
3904 			}
3905 			if (net->ro._s_addr == NULL) {
3906 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
3907 				net->src_addr_selected = 0;
3908 				goto no_route;
3909 			}
3910 			lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
3911 		} else {
3912 			sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
3913 			/* KAME hack: embed scopeid */
3914 			if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3915 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3916 				return (EINVAL);
3917 			}
3918 			if (over_addr == NULL) {
3919 				struct sctp_ifa *_lsrc;
3920 
3921 				_lsrc = sctp_source_address_selection(inp, stcb, ro,
3922 				    net,
3923 				    out_of_asoc_ok,
3924 				    vrf_id);
3925 				if (_lsrc == NULL) {
3926 					goto no_route;
3927 				}
3928 				lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
3929 				sctp_free_ifa(_lsrc);
3930 			} else {
3931 				lsa6->sin6_addr = over_addr->sin6.sin6_addr;
3932 				SCTP_RTALLOC(ro, vrf_id);
3933 			}
3934 			(void)sa6_recoverscope(sin6);
3935 		}
3936 		lsa6->sin6_port = inp->sctp_lport;
3937 
3938 		if (ro->ro_rt == NULL) {
3939 			/*
3940 			 * src addr selection failed to find a route (or
3941 			 * valid source addr), so we can't get there from
3942 			 * here!
3943 			 */
3944 			goto no_route;
3945 		}
3946 		/*
3947 		 * XXX: sa6 may not have a valid sin6_scope_id in the
3948 		 * non-SCOPEDROUTING case.
3949 		 */
3950 		bzero(&lsa6_storage, sizeof(lsa6_storage));
3951 		lsa6_storage.sin6_family = AF_INET6;
3952 		lsa6_storage.sin6_len = sizeof(lsa6_storage);
3953 		lsa6_storage.sin6_addr = lsa6->sin6_addr;
3954 		if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
3955 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
3956 			sctp_m_freem(m);
3957 			return (error);
3958 		}
3959 		/* XXX */
3960 		lsa6_storage.sin6_addr = lsa6->sin6_addr;
3961 		lsa6_storage.sin6_port = inp->sctp_lport;
3962 		lsa6 = &lsa6_storage;
3963 		ip6h->ip6_src = lsa6->sin6_addr;
3964 
3965 		if (port) {
3966 			udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
3967 			udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3968 			udp->uh_dport = port;
3969 			udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
3970 			udp->uh_sum = 0;
3971 			sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
3972 		} else {
3973 			sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
3974 		}
3975 
3976 		sctphdr->src_port = src_port;
3977 		sctphdr->dest_port = dest_port;
3978 		sctphdr->v_tag = v_tag;
3979 		sctphdr->checksum = 0;
3980 
3981 		/*
3982 		 * We set the hop limit now since there is a good chance
3983 		 * that our ro pointer is now filled
3984 		 */
3985 		ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
3986 		ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
3987 
3988 #ifdef SCTP_DEBUG
3989 		/* Copy to be sure something bad is not happening */
3990 		sin6->sin6_addr = ip6h->ip6_dst;
3991 		lsa6->sin6_addr = ip6h->ip6_src;
3992 #endif
3993 
3994 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
3995 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
3996 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
3997 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
3998 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
3999 		if (net) {
4000 			sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4001 			/* preserve the port and scope for link local send */
4002 			prev_scope = sin6->sin6_scope_id;
4003 			prev_port = sin6->sin6_port;
4004 		}
4005 		if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4006 			/* failed to prepend data, give up */
4007 			sctp_m_freem(m);
4008 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4009 			return (ENOMEM);
4010 		}
4011 #ifdef  SCTP_PACKET_LOGGING
4012 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4013 			sctp_packet_log(m, packet_length);
4014 #endif
4015 		SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4016 		if (port) {
4017 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4018 			    (stcb) &&
4019 			    (stcb->asoc.loopback_scope))) {
4020 				sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4021 				SCTP_STAT_INCR(sctps_sendswcrc);
4022 			} else {
4023 				SCTP_STAT_INCR(sctps_sendnocrc);
4024 			}
4025 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4026 				udp->uh_sum = 0xffff;
4027 			}
4028 		} else {
4029 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4030 			    (stcb) &&
4031 			    (stcb->asoc.loopback_scope))) {
4032 				m->m_pkthdr.csum_flags = CSUM_SCTP;
4033 				m->m_pkthdr.csum_data = 0;
4034 				SCTP_STAT_INCR(sctps_sendhwcrc);
4035 			} else {
4036 				SCTP_STAT_INCR(sctps_sendnocrc);
4037 			}
4038 		}
4039 		/* send it out. table id is taken from stcb */
4040 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4041 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4042 			so = SCTP_INP_SO(inp);
4043 			SCTP_SOCKET_UNLOCK(so, 0);
4044 		}
4045 #endif
4046 		SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4047 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4048 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4049 			atomic_add_int(&stcb->asoc.refcnt, 1);
4050 			SCTP_TCB_UNLOCK(stcb);
4051 			SCTP_SOCKET_LOCK(so, 0);
4052 			SCTP_TCB_LOCK(stcb);
4053 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
4054 		}
4055 #endif
4056 		if (net) {
4057 			/* for link local this must be done */
4058 			sin6->sin6_scope_id = prev_scope;
4059 			sin6->sin6_port = prev_port;
4060 		}
4061 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4062 		SCTP_STAT_INCR(sctps_sendpackets);
4063 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4064 		if (ret) {
4065 			SCTP_STAT_INCR(sctps_senderrors);
4066 		}
4067 		if (net == NULL) {
4068 			/* Now if we had a temp route free it */
4069 			if (ro->ro_rt) {
4070 				RTFREE(ro->ro_rt);
4071 			}
4072 		} else {
4073 			/* PMTU check versus smallest asoc MTU goes here */
4074 			if (ro->ro_rt == NULL) {
4075 				/* Route was freed */
4076 				if (net->ro._s_addr &&
4077 				    net->src_addr_selected) {
4078 					sctp_free_ifa(net->ro._s_addr);
4079 					net->ro._s_addr = NULL;
4080 				}
4081 				net->src_addr_selected = 0;
4082 			}
4083 			if ((ro->ro_rt != NULL) &&
4084 			    (net->ro._s_addr)) {
4085 				uint32_t mtu;
4086 
4087 				mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4088 				if (mtu &&
4089 				    (stcb->asoc.smallest_mtu > mtu)) {
4090 #ifdef SCTP_PRINT_FOR_B_AND_M
4091 					SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
4092 					    mtu);
4093 #endif
4094 					sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4095 					net->mtu = mtu;
4096 					if (net->port) {
4097 						net->mtu -= sizeof(struct udphdr);
4098 					}
4099 				}
4100 			} else if (ifp) {
4101 				if (ND_IFINFO(ifp)->linkmtu &&
4102 				    (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4103 #ifdef SCTP_PRINT_FOR_B_AND_M
4104 					SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
4105 					    ND_IFINFO(ifp)->linkmtu);
4106 #endif
4107 					sctp_mtu_size_reset(inp,
4108 					    &stcb->asoc,
4109 					    ND_IFINFO(ifp)->linkmtu);
4110 				}
4111 			}
4112 		}
4113 		return (ret);
4114 	}
4115 #endif
4116 	else {
4117 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4118 		    ((struct sockaddr *)to)->sa_family);
4119 		sctp_m_freem(m);
4120 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4121 		return (EFAULT);
4122 	}
4123 }
4124 
4125 
4126 void
4127 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4128 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4129     SCTP_UNUSED
4130 #endif
4131 )
4132 {
4133 	struct mbuf *m, *m_at, *mp_last;
4134 	struct sctp_nets *net;
4135 	struct sctp_init_chunk *init;
4136 	struct sctp_supported_addr_param *sup_addr;
4137 	struct sctp_adaptation_layer_indication *ali;
4138 	struct sctp_ecn_supported_param *ecn;
4139 	struct sctp_prsctp_supported_param *prsctp;
4140 	struct sctp_ecn_nonce_supported_param *ecn_nonce;
4141 	struct sctp_supported_chunk_types_param *pr_supported;
4142 	int cnt_inits_to = 0;
4143 	int padval, ret;
4144 	int num_ext;
4145 	int p_len;
4146 
4147 	/* INIT's always go to the primary (and usually ONLY address) */
4148 	mp_last = NULL;
4149 	net = stcb->asoc.primary_destination;
4150 	if (net == NULL) {
4151 		net = TAILQ_FIRST(&stcb->asoc.nets);
4152 		if (net == NULL) {
4153 			/* TSNH */
4154 			return;
4155 		}
4156 		/* we confirm any address we send an INIT to */
4157 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4158 		(void)sctp_set_primary_addr(stcb, NULL, net);
4159 	} else {
4160 		/* we confirm any address we send an INIT to */
4161 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4162 	}
4163 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4164 #ifdef INET6
4165 	if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4166 		/*
4167 		 * special hook, if we are sending to link local it will not
4168 		 * show up in our private address count.
4169 		 */
4170 		struct sockaddr_in6 *sin6l;
4171 
4172 		sin6l = &net->ro._l_addr.sin6;
4173 		if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4174 			cnt_inits_to = 1;
4175 	}
4176 #endif
4177 	if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4178 		/* This case should not happen */
4179 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4180 		return;
4181 	}
4182 	/* start the INIT timer */
4183 	sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4184 
4185 	m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4186 	if (m == NULL) {
4187 		/* No memory, INIT timer will re-attempt. */
4188 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4189 		return;
4190 	}
4191 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4192 	/*
4193 	 * assume peer supports asconf in order to be able to queue local
4194 	 * address changes while an INIT is in flight and before the assoc
4195 	 * is established.
4196 	 */
4197 	stcb->asoc.peer_supports_asconf = 1;
4198 	/* Now lets put the SCTP header in place */
4199 	init = mtod(m, struct sctp_init_chunk *);
4200 	/* now the chunk header */
4201 	init->ch.chunk_type = SCTP_INITIATION;
4202 	init->ch.chunk_flags = 0;
4203 	/* fill in later from mbuf we build */
4204 	init->ch.chunk_length = 0;
4205 	/* place in my tag */
4206 	init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4207 	/* set up some of the credits. */
4208 	init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4209 	    SCTP_MINIMAL_RWND));
4210 
4211 	init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4212 	init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4213 	init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4214 	/* now the address restriction */
4215 	sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init +
4216 	    sizeof(*init));
4217 	sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4218 #ifdef INET6
4219 	/* we support 2 types: IPv6/IPv4 */
4220 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
4221 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4222 	sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4223 #else
4224 	/* we support 1 type: IPv4 */
4225 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
4226 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4227 	sup_addr->addr_type[1] = htons(0);	/* this is the padding */
4228 #endif
4229 	SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
4230 	/* adaptation layer indication parameter */
4231 	ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
4232 	ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4233 	ali->ph.param_length = htons(sizeof(*ali));
4234 	ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4235 	SCTP_BUF_LEN(m) += sizeof(*ali);
4236 	ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
4237 
4238 	if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4239 		/* Add NAT friendly parameter */
4240 		struct sctp_paramhdr *ph;
4241 
4242 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4243 		ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4244 		ph->param_length = htons(sizeof(struct sctp_paramhdr));
4245 		SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
4246 		ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph));
4247 	}
4248 	/* now any cookie time extensions */
4249 	if (stcb->asoc.cookie_preserve_req) {
4250 		struct sctp_cookie_perserve_param *cookie_preserve;
4251 
4252 		cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4253 		cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4254 		cookie_preserve->ph.param_length = htons(
4255 		    sizeof(*cookie_preserve));
4256 		cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4257 		SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4258 		ecn = (struct sctp_ecn_supported_param *)(
4259 		    (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4260 		stcb->asoc.cookie_preserve_req = 0;
4261 	}
4262 	/* ECN parameter */
4263 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
4264 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4265 		ecn->ph.param_length = htons(sizeof(*ecn));
4266 		SCTP_BUF_LEN(m) += sizeof(*ecn);
4267 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4268 		    sizeof(*ecn));
4269 	} else {
4270 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4271 	}
4272 	/* And now tell the peer we do pr-sctp */
4273 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4274 	prsctp->ph.param_length = htons(sizeof(*prsctp));
4275 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
4276 
4277 	/* And now tell the peer we do all the extensions */
4278 	pr_supported = (struct sctp_supported_chunk_types_param *)
4279 	    ((caddr_t)prsctp + sizeof(*prsctp));
4280 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4281 	num_ext = 0;
4282 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4283 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4284 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4285 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4286 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4287 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4288 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4289 	}
4290 	/*
4291 	 * EY  if the initiator supports nr_sacks, need to report that to
4292 	 * responder in INIT chunk
4293 	 */
4294 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
4295 		pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4296 	}
4297 	p_len = sizeof(*pr_supported) + num_ext;
4298 	pr_supported->ph.param_length = htons(p_len);
4299 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4300 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4301 
4302 
4303 	/* ECN nonce: And now tell the peer we support ECN nonce */
4304 	if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
4305 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
4306 		    ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
4307 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
4308 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
4309 		SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
4310 	}
4311 	/* add authentication parameters */
4312 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4313 		struct sctp_auth_random *randp;
4314 		struct sctp_auth_hmac_algo *hmacs;
4315 		struct sctp_auth_chunk_list *chunks;
4316 
4317 		/* attach RANDOM parameter, if available */
4318 		if (stcb->asoc.authinfo.random != NULL) {
4319 			randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4320 			p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4321 			/* random key already contains the header */
4322 			bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4323 			/* zero out any padding required */
4324 			bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4325 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4326 		}
4327 		/* add HMAC_ALGO parameter */
4328 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4329 		p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4330 		    (uint8_t *) hmacs->hmac_ids);
4331 		if (p_len > 0) {
4332 			p_len += sizeof(*hmacs);
4333 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4334 			hmacs->ph.param_length = htons(p_len);
4335 			/* zero out any padding required */
4336 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4337 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4338 		}
4339 		/* add CHUNKS parameter */
4340 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4341 		p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4342 		    chunks->chunk_types);
4343 		if (p_len > 0) {
4344 			p_len += sizeof(*chunks);
4345 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4346 			chunks->ph.param_length = htons(p_len);
4347 			/* zero out any padding required */
4348 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4349 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4350 		}
4351 	}
4352 	m_at = m;
4353 	/* now the addresses */
4354 	{
4355 		struct sctp_scoping scp;
4356 
4357 		/*
4358 		 * To optimize this we could put the scoping stuff into a
4359 		 * structure and remove the individual uint8's from the
4360 		 * assoc structure. Then we could just sifa in the address
4361 		 * within the stcb.. but for now this is a quick hack to get
4362 		 * the address stuff teased apart.
4363 		 */
4364 		scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4365 		scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4366 		scp.loopback_scope = stcb->asoc.loopback_scope;
4367 		scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4368 		scp.local_scope = stcb->asoc.local_scope;
4369 		scp.site_scope = stcb->asoc.site_scope;
4370 
4371 		m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
4372 	}
4373 
4374 	/* calulate the size and update pkt header and chunk header */
4375 	p_len = 0;
4376 	for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4377 		if (SCTP_BUF_NEXT(m_at) == NULL)
4378 			mp_last = m_at;
4379 		p_len += SCTP_BUF_LEN(m_at);
4380 	}
4381 	init->ch.chunk_length = htons(p_len);
4382 	/*
4383 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4384 	 * here since the timer will drive a retranmission.
4385 	 */
4386 
4387 	/* I don't expect this to execute but we will be safe here */
4388 	padval = p_len % 4;
4389 	if ((padval) && (mp_last)) {
4390 		/*
4391 		 * The compiler worries that mp_last may not be set even
4392 		 * though I think it is impossible :-> however we add
4393 		 * mp_last here just in case.
4394 		 */
4395 		ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4396 		if (ret) {
4397 			/* Houston we have a problem, no space */
4398 			sctp_m_freem(m);
4399 			return;
4400 		}
4401 		p_len += padval;
4402 	}
4403 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4404 	ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4405 	    (struct sockaddr *)&net->ro._l_addr,
4406 	    m, 0, NULL, 0, 0, 0, NULL, 0,
4407 	    inp->sctp_lport, stcb->rport, htonl(0),
4408 	    net->port, so_locked, NULL);
4409 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4410 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4411 	(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4412 }
4413 
4414 struct mbuf *
4415 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4416     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
4417 {
4418 	/*
4419 	 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4420 	 * being equal to the beginning of the params i.e. (iphlen +
4421 	 * sizeof(struct sctp_init_msg) parse through the parameters to the
4422 	 * end of the mbuf verifying that all parameters are known.
4423 	 *
4424 	 * For unknown parameters build and return a mbuf with
4425 	 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4426 	 * processing this chunk stop, and set *abort_processing to 1.
4427 	 *
4428 	 * By having param_offset be pre-set to where parameters begin it is
4429 	 * hoped that this routine may be reused in the future by new
4430 	 * features.
4431 	 */
4432 	struct sctp_paramhdr *phdr, params;
4433 
4434 	struct mbuf *mat, *op_err;
4435 	char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4436 	int at, limit, pad_needed;
4437 	uint16_t ptype, plen, padded_size;
4438 	int err_at;
4439 
4440 	*abort_processing = 0;
4441 	mat = in_initpkt;
4442 	err_at = 0;
4443 	limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4444 	at = param_offset;
4445 	op_err = NULL;
4446 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4447 	phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4448 	while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4449 		ptype = ntohs(phdr->param_type);
4450 		plen = ntohs(phdr->param_length);
4451 		if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4452 			/* wacked parameter */
4453 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4454 			goto invalid_size;
4455 		}
4456 		limit -= SCTP_SIZE32(plen);
4457 		/*-
4458 		 * All parameters for all chunks that we know/understand are
4459 		 * listed here. We process them other places and make
4460 		 * appropriate stop actions per the upper bits. However this
4461 		 * is the generic routine processor's can call to get back
4462 		 * an operr.. to either incorporate (init-ack) or send.
4463 		 */
4464 		padded_size = SCTP_SIZE32(plen);
4465 		switch (ptype) {
4466 			/* Param's with variable size */
4467 		case SCTP_HEARTBEAT_INFO:
4468 		case SCTP_STATE_COOKIE:
4469 		case SCTP_UNRECOG_PARAM:
4470 		case SCTP_ERROR_CAUSE_IND:
4471 			/* ok skip fwd */
4472 			at += padded_size;
4473 			break;
4474 			/* Param's with variable size within a range */
4475 		case SCTP_CHUNK_LIST:
4476 		case SCTP_SUPPORTED_CHUNK_EXT:
4477 			if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4478 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4479 				goto invalid_size;
4480 			}
4481 			at += padded_size;
4482 			break;
4483 		case SCTP_SUPPORTED_ADDRTYPE:
4484 			if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4485 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4486 				goto invalid_size;
4487 			}
4488 			at += padded_size;
4489 			break;
4490 		case SCTP_RANDOM:
4491 			if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4492 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4493 				goto invalid_size;
4494 			}
4495 			at += padded_size;
4496 			break;
4497 		case SCTP_SET_PRIM_ADDR:
4498 		case SCTP_DEL_IP_ADDRESS:
4499 		case SCTP_ADD_IP_ADDRESS:
4500 			if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4501 			    (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4502 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4503 				goto invalid_size;
4504 			}
4505 			at += padded_size;
4506 			break;
4507 			/* Param's with a fixed size */
4508 		case SCTP_IPV4_ADDRESS:
4509 			if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4510 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4511 				goto invalid_size;
4512 			}
4513 			at += padded_size;
4514 			break;
4515 		case SCTP_IPV6_ADDRESS:
4516 			if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4517 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4518 				goto invalid_size;
4519 			}
4520 			at += padded_size;
4521 			break;
4522 		case SCTP_COOKIE_PRESERVE:
4523 			if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4524 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4525 				goto invalid_size;
4526 			}
4527 			at += padded_size;
4528 			break;
4529 		case SCTP_HAS_NAT_SUPPORT:
4530 			*nat_friendly = 1;
4531 			/* fall through */
4532 		case SCTP_ECN_NONCE_SUPPORTED:
4533 		case SCTP_PRSCTP_SUPPORTED:
4534 
4535 			if (padded_size != sizeof(struct sctp_paramhdr)) {
4536 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp/nat support %d\n", plen);
4537 				goto invalid_size;
4538 			}
4539 			at += padded_size;
4540 			break;
4541 		case SCTP_ECN_CAPABLE:
4542 			if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4543 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4544 				goto invalid_size;
4545 			}
4546 			at += padded_size;
4547 			break;
4548 		case SCTP_ULP_ADAPTATION:
4549 			if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4550 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4551 				goto invalid_size;
4552 			}
4553 			at += padded_size;
4554 			break;
4555 		case SCTP_SUCCESS_REPORT:
4556 			if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
4557 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
4558 				goto invalid_size;
4559 			}
4560 			at += padded_size;
4561 			break;
4562 		case SCTP_HOSTNAME_ADDRESS:
4563 			{
4564 				/* We can NOT handle HOST NAME addresses!! */
4565 				int l_len;
4566 
4567 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
4568 				*abort_processing = 1;
4569 				if (op_err == NULL) {
4570 					/* Ok need to try to get a mbuf */
4571 #ifdef INET6
4572 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4573 #else
4574 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4575 #endif
4576 					l_len += plen;
4577 					l_len += sizeof(struct sctp_paramhdr);
4578 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4579 					if (op_err) {
4580 						SCTP_BUF_LEN(op_err) = 0;
4581 						/*
4582 						 * pre-reserve space for ip
4583 						 * and sctp header  and
4584 						 * chunk hdr
4585 						 */
4586 #ifdef INET6
4587 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4588 #else
4589 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4590 #endif
4591 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4592 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4593 					}
4594 				}
4595 				if (op_err) {
4596 					/* If we have space */
4597 					struct sctp_paramhdr s;
4598 
4599 					if (err_at % 4) {
4600 						uint32_t cpthis = 0;
4601 
4602 						pad_needed = 4 - (err_at % 4);
4603 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4604 						err_at += pad_needed;
4605 					}
4606 					s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
4607 					s.param_length = htons(sizeof(s) + plen);
4608 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4609 					err_at += sizeof(s);
4610 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4611 					if (phdr == NULL) {
4612 						sctp_m_freem(op_err);
4613 						/*
4614 						 * we are out of memory but
4615 						 * we still need to have a
4616 						 * look at what to do (the
4617 						 * system is in trouble
4618 						 * though).
4619 						 */
4620 						return (NULL);
4621 					}
4622 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4623 					err_at += plen;
4624 				}
4625 				return (op_err);
4626 				break;
4627 			}
4628 		default:
4629 			/*
4630 			 * we do not recognize the parameter figure out what
4631 			 * we do.
4632 			 */
4633 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
4634 			if ((ptype & 0x4000) == 0x4000) {
4635 				/* Report bit is set?? */
4636 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
4637 				if (op_err == NULL) {
4638 					int l_len;
4639 
4640 					/* Ok need to try to get an mbuf */
4641 #ifdef INET6
4642 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4643 #else
4644 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4645 #endif
4646 					l_len += plen;
4647 					l_len += sizeof(struct sctp_paramhdr);
4648 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4649 					if (op_err) {
4650 						SCTP_BUF_LEN(op_err) = 0;
4651 #ifdef INET6
4652 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4653 #else
4654 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4655 #endif
4656 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4657 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4658 					}
4659 				}
4660 				if (op_err) {
4661 					/* If we have space */
4662 					struct sctp_paramhdr s;
4663 
4664 					if (err_at % 4) {
4665 						uint32_t cpthis = 0;
4666 
4667 						pad_needed = 4 - (err_at % 4);
4668 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4669 						err_at += pad_needed;
4670 					}
4671 					s.param_type = htons(SCTP_UNRECOG_PARAM);
4672 					s.param_length = htons(sizeof(s) + plen);
4673 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4674 					err_at += sizeof(s);
4675 					if (plen > sizeof(tempbuf)) {
4676 						plen = sizeof(tempbuf);
4677 					}
4678 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4679 					if (phdr == NULL) {
4680 						sctp_m_freem(op_err);
4681 						/*
4682 						 * we are out of memory but
4683 						 * we still need to have a
4684 						 * look at what to do (the
4685 						 * system is in trouble
4686 						 * though).
4687 						 */
4688 						op_err = NULL;
4689 						goto more_processing;
4690 					}
4691 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4692 					err_at += plen;
4693 				}
4694 			}
4695 	more_processing:
4696 			if ((ptype & 0x8000) == 0x0000) {
4697 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
4698 				return (op_err);
4699 			} else {
4700 				/* skip this chunk and continue processing */
4701 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
4702 				at += SCTP_SIZE32(plen);
4703 			}
4704 			break;
4705 
4706 		}
4707 		phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4708 	}
4709 	return (op_err);
4710 invalid_size:
4711 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
4712 	*abort_processing = 1;
4713 	if ((op_err == NULL) && phdr) {
4714 		int l_len;
4715 
4716 #ifdef INET6
4717 		l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4718 #else
4719 		l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4720 #endif
4721 		l_len += (2 * sizeof(struct sctp_paramhdr));
4722 		op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4723 		if (op_err) {
4724 			SCTP_BUF_LEN(op_err) = 0;
4725 #ifdef INET6
4726 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4727 #else
4728 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4729 #endif
4730 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4731 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4732 		}
4733 	}
4734 	if ((op_err) && phdr) {
4735 		struct sctp_paramhdr s;
4736 
4737 		if (err_at % 4) {
4738 			uint32_t cpthis = 0;
4739 
4740 			pad_needed = 4 - (err_at % 4);
4741 			m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4742 			err_at += pad_needed;
4743 		}
4744 		s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4745 		s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
4746 		m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4747 		err_at += sizeof(s);
4748 		/* Only copy back the p-hdr that caused the issue */
4749 		m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
4750 	}
4751 	return (op_err);
4752 }
4753 
4754 static int
4755 sctp_are_there_new_addresses(struct sctp_association *asoc,
4756     struct mbuf *in_initpkt, int iphlen, int offset)
4757 {
4758 	/*
4759 	 * Given a INIT packet, look through the packet to verify that there
4760 	 * are NO new addresses. As we go through the parameters add reports
4761 	 * of any un-understood parameters that require an error.  Also we
4762 	 * must return (1) to drop the packet if we see a un-understood
4763 	 * parameter that tells us to drop the chunk.
4764 	 */
4765 	struct sockaddr_in sin4, *sa4;
4766 
4767 #ifdef INET6
4768 	struct sockaddr_in6 sin6, *sa6;
4769 
4770 #endif
4771 	struct sockaddr *sa_touse;
4772 	struct sockaddr *sa;
4773 	struct sctp_paramhdr *phdr, params;
4774 	struct ip *iph;
4775 
4776 #ifdef INET6
4777 	struct ip6_hdr *ip6h;
4778 
4779 #endif
4780 	struct mbuf *mat;
4781 	uint16_t ptype, plen;
4782 	int err_at;
4783 	uint8_t fnd;
4784 	struct sctp_nets *net;
4785 
4786 	memset(&sin4, 0, sizeof(sin4));
4787 #ifdef INET6
4788 	memset(&sin6, 0, sizeof(sin6));
4789 #endif
4790 	sin4.sin_family = AF_INET;
4791 	sin4.sin_len = sizeof(sin4);
4792 #ifdef INET6
4793 	sin6.sin6_family = AF_INET6;
4794 	sin6.sin6_len = sizeof(sin6);
4795 #endif
4796 	sa_touse = NULL;
4797 	/* First what about the src address of the pkt ? */
4798 	iph = mtod(in_initpkt, struct ip *);
4799 	switch (iph->ip_v) {
4800 	case IPVERSION:
4801 		/* source addr is IPv4 */
4802 		sin4.sin_addr = iph->ip_src;
4803 		sa_touse = (struct sockaddr *)&sin4;
4804 		break;
4805 #ifdef INET6
4806 	case IPV6_VERSION >> 4:
4807 		/* source addr is IPv6 */
4808 		ip6h = mtod(in_initpkt, struct ip6_hdr *);
4809 		sin6.sin6_addr = ip6h->ip6_src;
4810 		sa_touse = (struct sockaddr *)&sin6;
4811 		break;
4812 #endif
4813 	default:
4814 		return (1);
4815 	}
4816 
4817 	fnd = 0;
4818 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4819 		sa = (struct sockaddr *)&net->ro._l_addr;
4820 		if (sa->sa_family == sa_touse->sa_family) {
4821 			if (sa->sa_family == AF_INET) {
4822 				sa4 = (struct sockaddr_in *)sa;
4823 				if (sa4->sin_addr.s_addr ==
4824 				    sin4.sin_addr.s_addr) {
4825 					fnd = 1;
4826 					break;
4827 				}
4828 			}
4829 #ifdef INET6
4830 			if (sa->sa_family == AF_INET6) {
4831 				sa6 = (struct sockaddr_in6 *)sa;
4832 				if (SCTP6_ARE_ADDR_EQUAL(sa6,
4833 				    &sin6)) {
4834 					fnd = 1;
4835 					break;
4836 				}
4837 			}
4838 #endif
4839 		}
4840 	}
4841 	if (fnd == 0) {
4842 		/* New address added! no need to look futher. */
4843 		return (1);
4844 	}
4845 	/* Ok so far lets munge through the rest of the packet */
4846 	mat = in_initpkt;
4847 	err_at = 0;
4848 	sa_touse = NULL;
4849 	offset += sizeof(struct sctp_init_chunk);
4850 	phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
4851 	while (phdr) {
4852 		ptype = ntohs(phdr->param_type);
4853 		plen = ntohs(phdr->param_length);
4854 		if (ptype == SCTP_IPV4_ADDRESS) {
4855 			struct sctp_ipv4addr_param *p4, p4_buf;
4856 
4857 			phdr = sctp_get_next_param(mat, offset,
4858 			    (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4859 			if (plen != sizeof(struct sctp_ipv4addr_param) ||
4860 			    phdr == NULL) {
4861 				return (1);
4862 			}
4863 			p4 = (struct sctp_ipv4addr_param *)phdr;
4864 			sin4.sin_addr.s_addr = p4->addr;
4865 			sa_touse = (struct sockaddr *)&sin4;
4866 		} else if (ptype == SCTP_IPV6_ADDRESS) {
4867 			struct sctp_ipv6addr_param *p6, p6_buf;
4868 
4869 			phdr = sctp_get_next_param(mat, offset,
4870 			    (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4871 			if (plen != sizeof(struct sctp_ipv6addr_param) ||
4872 			    phdr == NULL) {
4873 				return (1);
4874 			}
4875 			p6 = (struct sctp_ipv6addr_param *)phdr;
4876 #ifdef INET6
4877 			memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4878 			    sizeof(p6->addr));
4879 #endif
4880 			sa_touse = (struct sockaddr *)&sin4;
4881 		}
4882 		if (sa_touse) {
4883 			/* ok, sa_touse points to one to check */
4884 			fnd = 0;
4885 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4886 				sa = (struct sockaddr *)&net->ro._l_addr;
4887 				if (sa->sa_family != sa_touse->sa_family) {
4888 					continue;
4889 				}
4890 				if (sa->sa_family == AF_INET) {
4891 					sa4 = (struct sockaddr_in *)sa;
4892 					if (sa4->sin_addr.s_addr ==
4893 					    sin4.sin_addr.s_addr) {
4894 						fnd = 1;
4895 						break;
4896 					}
4897 				}
4898 #ifdef INET6
4899 				if (sa->sa_family == AF_INET6) {
4900 					sa6 = (struct sockaddr_in6 *)sa;
4901 					if (SCTP6_ARE_ADDR_EQUAL(
4902 					    sa6, &sin6)) {
4903 						fnd = 1;
4904 						break;
4905 					}
4906 				}
4907 #endif
4908 			}
4909 			if (!fnd) {
4910 				/* New addr added! no need to look further */
4911 				return (1);
4912 			}
4913 		}
4914 		offset += SCTP_SIZE32(plen);
4915 		phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
4916 	}
4917 	return (0);
4918 }
4919 
4920 /*
4921  * Given a MBUF chain that was sent into us containing an INIT. Build a
4922  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
4923  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
4924  * message (i.e. the struct sctp_init_msg).
4925  */
4926 void
4927 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4928     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
4929     struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
4930 {
4931 	struct sctp_association *asoc;
4932 	struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
4933 	struct sctp_init_ack_chunk *initack;
4934 	struct sctp_adaptation_layer_indication *ali;
4935 	struct sctp_ecn_supported_param *ecn;
4936 	struct sctp_prsctp_supported_param *prsctp;
4937 	struct sctp_ecn_nonce_supported_param *ecn_nonce;
4938 	struct sctp_supported_chunk_types_param *pr_supported;
4939 	union sctp_sockstore store, store1, *over_addr;
4940 	struct sockaddr_in *sin, *to_sin;
4941 
4942 #ifdef INET6
4943 	struct sockaddr_in6 *sin6, *to_sin6;
4944 
4945 #endif
4946 	struct ip *iph;
4947 
4948 #ifdef INET6
4949 	struct ip6_hdr *ip6;
4950 
4951 #endif
4952 	struct sockaddr *to;
4953 	struct sctp_state_cookie stc;
4954 	struct sctp_nets *net = NULL;
4955 	uint8_t *signature = NULL;
4956 	int cnt_inits_to = 0;
4957 	uint16_t his_limit, i_want;
4958 	int abort_flag, padval;
4959 	int num_ext;
4960 	int p_len;
4961 	int nat_friendly = 0;
4962 	struct socket *so;
4963 
4964 	if (stcb)
4965 		asoc = &stcb->asoc;
4966 	else
4967 		asoc = NULL;
4968 	mp_last = NULL;
4969 	if ((asoc != NULL) &&
4970 	    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4971 	    (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
4972 		/* new addresses, out of here in non-cookie-wait states */
4973 		/*
4974 		 * Send a ABORT, we don't add the new address error clause
4975 		 * though we even set the T bit and copy in the 0 tag.. this
4976 		 * looks no different than if no listener was present.
4977 		 */
4978 		sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
4979 		return;
4980 	}
4981 	abort_flag = 0;
4982 	op_err = sctp_arethere_unrecognized_parameters(init_pkt,
4983 	    (offset + sizeof(struct sctp_init_chunk)),
4984 	    &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
4985 	if (abort_flag) {
4986 do_a_abort:
4987 		sctp_send_abort(init_pkt, iphlen, sh,
4988 		    init_chk->init.initiate_tag, op_err, vrf_id, port);
4989 		return;
4990 	}
4991 	m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4992 	if (m == NULL) {
4993 		/* No memory, INIT timer will re-attempt. */
4994 		if (op_err)
4995 			sctp_m_freem(op_err);
4996 		return;
4997 	}
4998 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4999 
5000 	/* the time I built cookie */
5001 	(void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
5002 
5003 	/* populate any tie tags */
5004 	if (asoc != NULL) {
5005 		/* unlock before tag selections */
5006 		stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5007 		stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5008 		stc.cookie_life = asoc->cookie_life;
5009 		net = asoc->primary_destination;
5010 	} else {
5011 		stc.tie_tag_my_vtag = 0;
5012 		stc.tie_tag_peer_vtag = 0;
5013 		/* life I will award this cookie */
5014 		stc.cookie_life = inp->sctp_ep.def_cookie_life;
5015 	}
5016 
5017 	/* copy in the ports for later check */
5018 	stc.myport = sh->dest_port;
5019 	stc.peerport = sh->src_port;
5020 
5021 	/*
5022 	 * If we wanted to honor cookie life extentions, we would add to
5023 	 * stc.cookie_life. For now we should NOT honor any extension
5024 	 */
5025 	stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5026 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5027 		struct inpcb *in_inp;
5028 
5029 		/* Its a V6 socket */
5030 		in_inp = (struct inpcb *)inp;
5031 		stc.ipv6_addr_legal = 1;
5032 		/* Now look at the binding flag to see if V4 will be legal */
5033 		if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
5034 			stc.ipv4_addr_legal = 1;
5035 		} else {
5036 			/* V4 addresses are NOT legal on the association */
5037 			stc.ipv4_addr_legal = 0;
5038 		}
5039 	} else {
5040 		/* Its a V4 socket, no - V6 */
5041 		stc.ipv4_addr_legal = 1;
5042 		stc.ipv6_addr_legal = 0;
5043 	}
5044 
5045 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
5046 	stc.ipv4_scope = 1;
5047 #else
5048 	stc.ipv4_scope = 0;
5049 #endif
5050 	/* now for scope setup */
5051 	memset((caddr_t)&store, 0, sizeof(store));
5052 	memset((caddr_t)&store1, 0, sizeof(store1));
5053 	sin = &store.sin;
5054 	to_sin = &store1.sin;
5055 #ifdef INET6
5056 	sin6 = &store.sin6;
5057 	to_sin6 = &store1.sin6;
5058 #endif
5059 	iph = mtod(init_pkt, struct ip *);
5060 	/* establish the to_addr's */
5061 	switch (iph->ip_v) {
5062 	case IPVERSION:
5063 		to_sin->sin_port = sh->dest_port;
5064 		to_sin->sin_family = AF_INET;
5065 		to_sin->sin_len = sizeof(struct sockaddr_in);
5066 		to_sin->sin_addr = iph->ip_dst;
5067 		break;
5068 #ifdef INET6
5069 	case IPV6_VERSION >> 4:
5070 		ip6 = mtod(init_pkt, struct ip6_hdr *);
5071 		to_sin6->sin6_addr = ip6->ip6_dst;
5072 		to_sin6->sin6_scope_id = 0;
5073 		to_sin6->sin6_port = sh->dest_port;
5074 		to_sin6->sin6_family = AF_INET6;
5075 		to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5076 		break;
5077 #endif
5078 	default:
5079 		goto do_a_abort;
5080 		break;
5081 	};
5082 
5083 	if (net == NULL) {
5084 		to = (struct sockaddr *)&store;
5085 		switch (iph->ip_v) {
5086 		case IPVERSION:
5087 			{
5088 				sin->sin_family = AF_INET;
5089 				sin->sin_len = sizeof(struct sockaddr_in);
5090 				sin->sin_port = sh->src_port;
5091 				sin->sin_addr = iph->ip_src;
5092 				/* lookup address */
5093 				stc.address[0] = sin->sin_addr.s_addr;
5094 				stc.address[1] = 0;
5095 				stc.address[2] = 0;
5096 				stc.address[3] = 0;
5097 				stc.addr_type = SCTP_IPV4_ADDRESS;
5098 				/* local from address */
5099 				stc.laddress[0] = to_sin->sin_addr.s_addr;
5100 				stc.laddress[1] = 0;
5101 				stc.laddress[2] = 0;
5102 				stc.laddress[3] = 0;
5103 				stc.laddr_type = SCTP_IPV4_ADDRESS;
5104 				/* scope_id is only for v6 */
5105 				stc.scope_id = 0;
5106 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5107 				if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5108 					stc.ipv4_scope = 1;
5109 				}
5110 #else
5111 				stc.ipv4_scope = 1;
5112 #endif				/* SCTP_DONT_DO_PRIVADDR_SCOPE */
5113 				/* Must use the address in this case */
5114 				if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5115 					stc.loopback_scope = 1;
5116 					stc.ipv4_scope = 1;
5117 					stc.site_scope = 1;
5118 					stc.local_scope = 0;
5119 				}
5120 				break;
5121 			}
5122 #ifdef INET6
5123 		case IPV6_VERSION >> 4:
5124 			{
5125 				ip6 = mtod(init_pkt, struct ip6_hdr *);
5126 				sin6->sin6_family = AF_INET6;
5127 				sin6->sin6_len = sizeof(struct sockaddr_in6);
5128 				sin6->sin6_port = sh->src_port;
5129 				sin6->sin6_addr = ip6->ip6_src;
5130 				/* lookup address */
5131 				memcpy(&stc.address, &sin6->sin6_addr,
5132 				    sizeof(struct in6_addr));
5133 				sin6->sin6_scope_id = 0;
5134 				stc.addr_type = SCTP_IPV6_ADDRESS;
5135 				stc.scope_id = 0;
5136 				if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5137 					/*
5138 					 * FIX ME: does this have scope from
5139 					 * rcvif?
5140 					 */
5141 					(void)sa6_recoverscope(sin6);
5142 					stc.scope_id = sin6->sin6_scope_id;
5143 					sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5144 					stc.loopback_scope = 1;
5145 					stc.local_scope = 0;
5146 					stc.site_scope = 1;
5147 					stc.ipv4_scope = 1;
5148 				} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5149 					/*
5150 					 * If the new destination is a
5151 					 * LINK_LOCAL we must have common
5152 					 * both site and local scope. Don't
5153 					 * set local scope though since we
5154 					 * must depend on the source to be
5155 					 * added implicitly. We cannot
5156 					 * assure just because we share one
5157 					 * link that all links are common.
5158 					 */
5159 					stc.local_scope = 0;
5160 					stc.site_scope = 1;
5161 					stc.ipv4_scope = 1;
5162 					/*
5163 					 * we start counting for the private
5164 					 * address stuff at 1. since the
5165 					 * link local we source from won't
5166 					 * show up in our scoped count.
5167 					 */
5168 					cnt_inits_to = 1;
5169 					/*
5170 					 * pull out the scope_id from
5171 					 * incoming pkt
5172 					 */
5173 					/*
5174 					 * FIX ME: does this have scope from
5175 					 * rcvif?
5176 					 */
5177 					(void)sa6_recoverscope(sin6);
5178 					stc.scope_id = sin6->sin6_scope_id;
5179 					sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5180 				} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5181 					/*
5182 					 * If the new destination is
5183 					 * SITE_LOCAL then we must have site
5184 					 * scope in common.
5185 					 */
5186 					stc.site_scope = 1;
5187 				}
5188 				memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5189 				stc.laddr_type = SCTP_IPV6_ADDRESS;
5190 				break;
5191 			}
5192 #endif
5193 		default:
5194 			/* TSNH */
5195 			goto do_a_abort;
5196 			break;
5197 		}
5198 	} else {
5199 		/* set the scope per the existing tcb */
5200 
5201 #ifdef INET6
5202 		struct sctp_nets *lnet;
5203 
5204 #endif
5205 
5206 		stc.loopback_scope = asoc->loopback_scope;
5207 		stc.ipv4_scope = asoc->ipv4_local_scope;
5208 		stc.site_scope = asoc->site_scope;
5209 		stc.local_scope = asoc->local_scope;
5210 #ifdef INET6
5211 		/* Why do we not consider IPv4 LL addresses? */
5212 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5213 			if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5214 				if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5215 					/*
5216 					 * if we have a LL address, start
5217 					 * counting at 1.
5218 					 */
5219 					cnt_inits_to = 1;
5220 				}
5221 			}
5222 		}
5223 #endif
5224 		/* use the net pointer */
5225 		to = (struct sockaddr *)&net->ro._l_addr;
5226 		switch (to->sa_family) {
5227 		case AF_INET:
5228 			sin = (struct sockaddr_in *)to;
5229 			stc.address[0] = sin->sin_addr.s_addr;
5230 			stc.address[1] = 0;
5231 			stc.address[2] = 0;
5232 			stc.address[3] = 0;
5233 			stc.addr_type = SCTP_IPV4_ADDRESS;
5234 			if (net->src_addr_selected == 0) {
5235 				/*
5236 				 * strange case here, the INIT should have
5237 				 * did the selection.
5238 				 */
5239 				net->ro._s_addr = sctp_source_address_selection(inp,
5240 				    stcb, (sctp_route_t *) & net->ro,
5241 				    net, 0, vrf_id);
5242 				if (net->ro._s_addr == NULL)
5243 					return;
5244 
5245 				net->src_addr_selected = 1;
5246 
5247 			}
5248 			stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5249 			stc.laddress[1] = 0;
5250 			stc.laddress[2] = 0;
5251 			stc.laddress[3] = 0;
5252 			stc.laddr_type = SCTP_IPV4_ADDRESS;
5253 			break;
5254 #ifdef INET6
5255 		case AF_INET6:
5256 			sin6 = (struct sockaddr_in6 *)to;
5257 			memcpy(&stc.address, &sin6->sin6_addr,
5258 			    sizeof(struct in6_addr));
5259 			stc.addr_type = SCTP_IPV6_ADDRESS;
5260 			if (net->src_addr_selected == 0) {
5261 				/*
5262 				 * strange case here, the INIT should have
5263 				 * did the selection.
5264 				 */
5265 				net->ro._s_addr = sctp_source_address_selection(inp,
5266 				    stcb, (sctp_route_t *) & net->ro,
5267 				    net, 0, vrf_id);
5268 				if (net->ro._s_addr == NULL)
5269 					return;
5270 
5271 				net->src_addr_selected = 1;
5272 			}
5273 			memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5274 			    sizeof(struct in6_addr));
5275 			stc.laddr_type = SCTP_IPV6_ADDRESS;
5276 			break;
5277 #endif
5278 		}
5279 	}
5280 	/* Now lets put the SCTP header in place */
5281 	initack = mtod(m, struct sctp_init_ack_chunk *);
5282 	/* Save it off for quick ref */
5283 	stc.peers_vtag = init_chk->init.initiate_tag;
5284 	/* who are we */
5285 	memcpy(stc.identification, SCTP_VERSION_STRING,
5286 	    min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5287 	/* now the chunk header */
5288 	initack->ch.chunk_type = SCTP_INITIATION_ACK;
5289 	initack->ch.chunk_flags = 0;
5290 	/* fill in later from mbuf we build */
5291 	initack->ch.chunk_length = 0;
5292 	/* place in my tag */
5293 	if ((asoc != NULL) &&
5294 	    ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5295 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5296 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5297 		/* re-use the v-tags and init-seq here */
5298 		initack->init.initiate_tag = htonl(asoc->my_vtag);
5299 		initack->init.initial_tsn = htonl(asoc->init_seq_number);
5300 	} else {
5301 		uint32_t vtag, itsn;
5302 
5303 		if (hold_inp_lock) {
5304 			SCTP_INP_INCR_REF(inp);
5305 			SCTP_INP_RUNLOCK(inp);
5306 		}
5307 		if (asoc) {
5308 			atomic_add_int(&asoc->refcnt, 1);
5309 			SCTP_TCB_UNLOCK(stcb);
5310 	new_tag:
5311 			vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5312 			if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5313 				/*
5314 				 * Got a duplicate vtag on some guy behind a
5315 				 * nat make sure we don't use it.
5316 				 */
5317 				goto new_tag;
5318 			}
5319 			initack->init.initiate_tag = htonl(vtag);
5320 			/* get a TSN to use too */
5321 			itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5322 			initack->init.initial_tsn = htonl(itsn);
5323 			SCTP_TCB_LOCK(stcb);
5324 			atomic_add_int(&asoc->refcnt, -1);
5325 		} else {
5326 			vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5327 			initack->init.initiate_tag = htonl(vtag);
5328 			/* get a TSN to use too */
5329 			initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5330 		}
5331 		if (hold_inp_lock) {
5332 			SCTP_INP_RLOCK(inp);
5333 			SCTP_INP_DECR_REF(inp);
5334 		}
5335 	}
5336 	/* save away my tag to */
5337 	stc.my_vtag = initack->init.initiate_tag;
5338 
5339 	/* set up some of the credits. */
5340 	so = inp->sctp_socket;
5341 	if (so == NULL) {
5342 		/* memory problem */
5343 		sctp_m_freem(m);
5344 		return;
5345 	} else {
5346 		initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5347 	}
5348 	/* set what I want */
5349 	his_limit = ntohs(init_chk->init.num_inbound_streams);
5350 	/* choose what I want */
5351 	if (asoc != NULL) {
5352 		if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5353 			i_want = asoc->streamoutcnt;
5354 		} else {
5355 			i_want = inp->sctp_ep.pre_open_stream_count;
5356 		}
5357 	} else {
5358 		i_want = inp->sctp_ep.pre_open_stream_count;
5359 	}
5360 	if (his_limit < i_want) {
5361 		/* I Want more :< */
5362 		initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5363 	} else {
5364 		/* I can have what I want :> */
5365 		initack->init.num_outbound_streams = htons(i_want);
5366 	}
5367 	/* tell him his limt. */
5368 	initack->init.num_inbound_streams =
5369 	    htons(inp->sctp_ep.max_open_streams_intome);
5370 
5371 	/* adaptation layer indication parameter */
5372 	ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack));
5373 	ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5374 	ali->ph.param_length = htons(sizeof(*ali));
5375 	ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5376 	SCTP_BUF_LEN(m) += sizeof(*ali);
5377 	ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
5378 
5379 	/* ECN parameter */
5380 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
5381 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5382 		ecn->ph.param_length = htons(sizeof(*ecn));
5383 		SCTP_BUF_LEN(m) += sizeof(*ecn);
5384 
5385 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5386 		    sizeof(*ecn));
5387 	} else {
5388 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5389 	}
5390 	/* And now tell the peer we do  pr-sctp */
5391 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5392 	prsctp->ph.param_length = htons(sizeof(*prsctp));
5393 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
5394 	if (nat_friendly) {
5395 		/* Add NAT friendly parameter */
5396 		struct sctp_paramhdr *ph;
5397 
5398 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5399 		ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5400 		ph->param_length = htons(sizeof(struct sctp_paramhdr));
5401 		SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
5402 	}
5403 	/* And now tell the peer we do all the extensions */
5404 	pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5405 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5406 	num_ext = 0;
5407 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5408 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5409 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5410 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5411 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5412 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5413 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5414 	/*
5415 	 * EY  if the sysctl variable is set, tell the assoc. initiator that
5416 	 * we do nr_sack
5417 	 */
5418 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
5419 		pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5420 	p_len = sizeof(*pr_supported) + num_ext;
5421 	pr_supported->ph.param_length = htons(p_len);
5422 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5423 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5424 
5425 	/* ECN nonce: And now tell the peer we support ECN nonce */
5426 	if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
5427 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
5428 		    ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
5429 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
5430 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
5431 		SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
5432 	}
5433 	/* add authentication parameters */
5434 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5435 		struct sctp_auth_random *randp;
5436 		struct sctp_auth_hmac_algo *hmacs;
5437 		struct sctp_auth_chunk_list *chunks;
5438 		uint16_t random_len;
5439 
5440 		/* generate and add RANDOM parameter */
5441 		random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5442 		randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5443 		randp->ph.param_type = htons(SCTP_RANDOM);
5444 		p_len = sizeof(*randp) + random_len;
5445 		randp->ph.param_length = htons(p_len);
5446 		SCTP_READ_RANDOM(randp->random_data, random_len);
5447 		/* zero out any padding required */
5448 		bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5449 		SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5450 
5451 		/* add HMAC_ALGO parameter */
5452 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5453 		p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5454 		    (uint8_t *) hmacs->hmac_ids);
5455 		if (p_len > 0) {
5456 			p_len += sizeof(*hmacs);
5457 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5458 			hmacs->ph.param_length = htons(p_len);
5459 			/* zero out any padding required */
5460 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5461 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5462 		}
5463 		/* add CHUNKS parameter */
5464 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5465 		p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5466 		    chunks->chunk_types);
5467 		if (p_len > 0) {
5468 			p_len += sizeof(*chunks);
5469 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5470 			chunks->ph.param_length = htons(p_len);
5471 			/* zero out any padding required */
5472 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5473 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5474 		}
5475 	}
5476 	m_at = m;
5477 	/* now the addresses */
5478 	{
5479 		struct sctp_scoping scp;
5480 
5481 		/*
5482 		 * To optimize this we could put the scoping stuff into a
5483 		 * structure and remove the individual uint8's from the stc
5484 		 * structure. Then we could just sifa in the address within
5485 		 * the stc.. but for now this is a quick hack to get the
5486 		 * address stuff teased apart.
5487 		 */
5488 		scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5489 		scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5490 		scp.loopback_scope = stc.loopback_scope;
5491 		scp.ipv4_local_scope = stc.ipv4_scope;
5492 		scp.local_scope = stc.local_scope;
5493 		scp.site_scope = stc.site_scope;
5494 		m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
5495 	}
5496 
5497 	/* tack on the operational error if present */
5498 	if (op_err) {
5499 		struct mbuf *ol;
5500 		int llen;
5501 
5502 		llen = 0;
5503 		ol = op_err;
5504 		while (ol) {
5505 			llen += SCTP_BUF_LEN(ol);
5506 			ol = SCTP_BUF_NEXT(ol);
5507 		}
5508 		if (llen % 4) {
5509 			/* must add a pad to the param */
5510 			uint32_t cpthis = 0;
5511 			int padlen;
5512 
5513 			padlen = 4 - (llen % 4);
5514 			m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5515 		}
5516 		while (SCTP_BUF_NEXT(m_at) != NULL) {
5517 			m_at = SCTP_BUF_NEXT(m_at);
5518 		}
5519 		SCTP_BUF_NEXT(m_at) = op_err;
5520 		while (SCTP_BUF_NEXT(m_at) != NULL) {
5521 			m_at = SCTP_BUF_NEXT(m_at);
5522 		}
5523 	}
5524 	/* pre-calulate the size and update pkt header and chunk header */
5525 	p_len = 0;
5526 	for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5527 		p_len += SCTP_BUF_LEN(m_tmp);
5528 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5529 			/* m_tmp should now point to last one */
5530 			break;
5531 		}
5532 	}
5533 
5534 	/* Now we must build a cookie */
5535 	m_cookie = sctp_add_cookie(inp, init_pkt, offset, m, 0, &stc, &signature);
5536 	if (m_cookie == NULL) {
5537 		/* memory problem */
5538 		sctp_m_freem(m);
5539 		return;
5540 	}
5541 	/* Now append the cookie to the end and update the space/size */
5542 	SCTP_BUF_NEXT(m_tmp) = m_cookie;
5543 
5544 	for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5545 		p_len += SCTP_BUF_LEN(m_tmp);
5546 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5547 			/* m_tmp should now point to last one */
5548 			mp_last = m_tmp;
5549 			break;
5550 		}
5551 	}
5552 	/*
5553 	 * Place in the size, but we don't include the last pad (if any) in
5554 	 * the INIT-ACK.
5555 	 */
5556 	initack->ch.chunk_length = htons(p_len);
5557 
5558 	/*
5559 	 * Time to sign the cookie, we don't sign over the cookie signature
5560 	 * though thus we set trailer.
5561 	 */
5562 	(void)sctp_hmac_m(SCTP_HMAC,
5563 	    (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
5564 	    SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
5565 	    (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
5566 	/*
5567 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
5568 	 * here since the timer will drive a retranmission.
5569 	 */
5570 	padval = p_len % 4;
5571 	if ((padval) && (mp_last)) {
5572 		/* see my previous comments on mp_last */
5573 		int ret;
5574 
5575 		ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
5576 		if (ret) {
5577 			/* Houston we have a problem, no space */
5578 			sctp_m_freem(m);
5579 			return;
5580 		}
5581 		p_len += padval;
5582 	}
5583 	if (stc.loopback_scope) {
5584 		over_addr = &store1;
5585 	} else {
5586 		over_addr = NULL;
5587 	}
5588 
5589 	(void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
5590 	    0, NULL, 0,
5591 	    inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
5592 	    port, SCTP_SO_NOT_LOCKED, over_addr);
5593 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5594 }
5595 
5596 
5597 void
5598 sctp_insert_on_wheel(struct sctp_tcb *stcb,
5599     struct sctp_association *asoc,
5600     struct sctp_stream_out *strq, int holds_lock)
5601 {
5602 	if (holds_lock == 0) {
5603 		SCTP_TCB_SEND_LOCK(stcb);
5604 	}
5605 	if ((strq->next_spoke.tqe_next == NULL) &&
5606 	    (strq->next_spoke.tqe_prev == NULL)) {
5607 		TAILQ_INSERT_TAIL(&asoc->out_wheel, strq, next_spoke);
5608 	}
5609 	if (holds_lock == 0) {
5610 		SCTP_TCB_SEND_UNLOCK(stcb);
5611 	}
5612 }
5613 
5614 void
5615 sctp_remove_from_wheel(struct sctp_tcb *stcb,
5616     struct sctp_association *asoc,
5617     struct sctp_stream_out *strq,
5618     int holds_lock)
5619 {
5620 	/* take off and then setup so we know it is not on the wheel */
5621 	if (holds_lock == 0) {
5622 		SCTP_TCB_SEND_LOCK(stcb);
5623 	}
5624 	if (TAILQ_EMPTY(&strq->outqueue)) {
5625 		if (asoc->last_out_stream == strq) {
5626 			asoc->last_out_stream = TAILQ_PREV(asoc->last_out_stream, sctpwheel_listhead, next_spoke);
5627 			if (asoc->last_out_stream == NULL) {
5628 				asoc->last_out_stream = TAILQ_LAST(&asoc->out_wheel, sctpwheel_listhead);
5629 			}
5630 			if (asoc->last_out_stream == strq) {
5631 				asoc->last_out_stream = NULL;
5632 			}
5633 		}
5634 		TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
5635 		strq->next_spoke.tqe_next = NULL;
5636 		strq->next_spoke.tqe_prev = NULL;
5637 	}
5638 	if (holds_lock == 0) {
5639 		SCTP_TCB_SEND_UNLOCK(stcb);
5640 	}
5641 }
5642 
5643 static void
5644 sctp_prune_prsctp(struct sctp_tcb *stcb,
5645     struct sctp_association *asoc,
5646     struct sctp_sndrcvinfo *srcv,
5647     int dataout)
5648 {
5649 	int freed_spc = 0;
5650 	struct sctp_tmit_chunk *chk, *nchk;
5651 
5652 	SCTP_TCB_LOCK_ASSERT(stcb);
5653 	if ((asoc->peer_supports_prsctp) &&
5654 	    (asoc->sent_queue_cnt_removeable > 0)) {
5655 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5656 			/*
5657 			 * Look for chunks marked with the PR_SCTP flag AND
5658 			 * the buffer space flag. If the one being sent is
5659 			 * equal or greater priority then purge the old one
5660 			 * and free some space.
5661 			 */
5662 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5663 				/*
5664 				 * This one is PR-SCTP AND buffer space
5665 				 * limited type
5666 				 */
5667 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5668 					/*
5669 					 * Lower numbers equates to higher
5670 					 * priority so if the one we are
5671 					 * looking at has a larger or equal
5672 					 * priority we want to drop the data
5673 					 * and NOT retransmit it.
5674 					 */
5675 					if (chk->data) {
5676 						/*
5677 						 * We release the book_size
5678 						 * if the mbuf is here
5679 						 */
5680 						int ret_spc;
5681 						int cause;
5682 
5683 						if (chk->sent > SCTP_DATAGRAM_UNSENT)
5684 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
5685 						else
5686 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
5687 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5688 						    cause,
5689 						    SCTP_SO_LOCKED);
5690 						freed_spc += ret_spc;
5691 						if (freed_spc >= dataout) {
5692 							return;
5693 						}
5694 					}	/* if chunk was present */
5695 				}	/* if of sufficent priority */
5696 			}	/* if chunk has enabled */
5697 		}		/* tailqforeach */
5698 
5699 		chk = TAILQ_FIRST(&asoc->send_queue);
5700 		while (chk) {
5701 			nchk = TAILQ_NEXT(chk, sctp_next);
5702 			/* Here we must move to the sent queue and mark */
5703 			if (PR_SCTP_TTL_ENABLED(chk->flags)) {
5704 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5705 					if (chk->data) {
5706 						/*
5707 						 * We release the book_size
5708 						 * if the mbuf is here
5709 						 */
5710 						int ret_spc;
5711 
5712 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5713 						    SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
5714 						    SCTP_SO_LOCKED);
5715 
5716 						freed_spc += ret_spc;
5717 						if (freed_spc >= dataout) {
5718 							return;
5719 						}
5720 					}	/* end if chk->data */
5721 				}	/* end if right class */
5722 			}	/* end if chk pr-sctp */
5723 			chk = nchk;
5724 		}		/* end while (chk) */
5725 	}			/* if enabled in asoc */
5726 }
5727 
5728 int
5729 sctp_get_frag_point(struct sctp_tcb *stcb,
5730     struct sctp_association *asoc)
5731 {
5732 	int siz, ovh;
5733 
5734 	/*
5735 	 * For endpoints that have both v6 and v4 addresses we must reserve
5736 	 * room for the ipv6 header, for those that are only dealing with V4
5737 	 * we use a larger frag point.
5738 	 */
5739 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5740 		ovh = SCTP_MED_OVERHEAD;
5741 	} else {
5742 		ovh = SCTP_MED_V4_OVERHEAD;
5743 	}
5744 
5745 	if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
5746 		siz = asoc->smallest_mtu - ovh;
5747 	else
5748 		siz = (stcb->asoc.sctp_frag_point - ovh);
5749 	/*
5750 	 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
5751 	 */
5752 	/* A data chunk MUST fit in a cluster */
5753 	/* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
5754 	/* } */
5755 
5756 	/* adjust for an AUTH chunk if DATA requires auth */
5757 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
5758 		siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5759 
5760 	if (siz % 4) {
5761 		/* make it an even word boundary please */
5762 		siz -= (siz % 4);
5763 	}
5764 	return (siz);
5765 }
5766 
5767 static void
5768 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
5769 {
5770 	sp->pr_sctp_on = 0;
5771 	/*
5772 	 * We assume that the user wants PR_SCTP_TTL if the user provides a
5773 	 * positive lifetime but does not specify any PR_SCTP policy. This
5774 	 * is a BAD assumption and causes problems at least with the
5775 	 * U-Vancovers MPI folks. I will change this to be no policy means
5776 	 * NO PR-SCTP.
5777 	 */
5778 	if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
5779 		sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
5780 		sp->pr_sctp_on = 1;
5781 	} else {
5782 		return;
5783 	}
5784 	switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
5785 	case CHUNK_FLAGS_PR_SCTP_BUF:
5786 		/*
5787 		 * Time to live is a priority stored in tv_sec when doing
5788 		 * the buffer drop thing.
5789 		 */
5790 		sp->ts.tv_sec = sp->timetolive;
5791 		sp->ts.tv_usec = 0;
5792 		break;
5793 	case CHUNK_FLAGS_PR_SCTP_TTL:
5794 		{
5795 			struct timeval tv;
5796 
5797 			(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5798 			tv.tv_sec = sp->timetolive / 1000;
5799 			tv.tv_usec = (sp->timetolive * 1000) % 1000000;
5800 			/*
5801 			 * TODO sctp_constants.h needs alternative time
5802 			 * macros when _KERNEL is undefined.
5803 			 */
5804 			timevaladd(&sp->ts, &tv);
5805 		}
5806 		break;
5807 	case CHUNK_FLAGS_PR_SCTP_RTX:
5808 		/*
5809 		 * Time to live is a the number or retransmissions stored in
5810 		 * tv_sec.
5811 		 */
5812 		sp->ts.tv_sec = sp->timetolive;
5813 		sp->ts.tv_usec = 0;
5814 		break;
5815 	default:
5816 		SCTPDBG(SCTP_DEBUG_USRREQ1,
5817 		    "Unknown PR_SCTP policy %u.\n",
5818 		    PR_SCTP_POLICY(sp->sinfo_flags));
5819 		break;
5820 	}
5821 }
5822 
5823 static int
5824 sctp_msg_append(struct sctp_tcb *stcb,
5825     struct sctp_nets *net,
5826     struct mbuf *m,
5827     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5828 {
5829 	int error = 0, holds_lock;
5830 	struct mbuf *at;
5831 	struct sctp_stream_queue_pending *sp = NULL;
5832 	struct sctp_stream_out *strm;
5833 
5834 	/*
5835 	 * Given an mbuf chain, put it into the association send queue and
5836 	 * place it on the wheel
5837 	 */
5838 	holds_lock = hold_stcb_lock;
5839 	if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5840 		/* Invalid stream number */
5841 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5842 		error = EINVAL;
5843 		goto out_now;
5844 	}
5845 	if ((stcb->asoc.stream_locked) &&
5846 	    (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5847 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5848 		error = EINVAL;
5849 		goto out_now;
5850 	}
5851 	strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5852 	/* Now can we send this? */
5853 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5854 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5855 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5856 	    (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5857 		/* got data while shutting down */
5858 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
5859 		error = ECONNRESET;
5860 		goto out_now;
5861 	}
5862 	sctp_alloc_a_strmoq(stcb, sp);
5863 	if (sp == NULL) {
5864 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5865 		error = ENOMEM;
5866 		goto out_now;
5867 	}
5868 	sp->sinfo_flags = srcv->sinfo_flags;
5869 	sp->timetolive = srcv->sinfo_timetolive;
5870 	sp->ppid = srcv->sinfo_ppid;
5871 	sp->context = srcv->sinfo_context;
5872 	sp->strseq = 0;
5873 	if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5874 		sp->net = net;
5875 	} else {
5876 		sp->net = stcb->asoc.primary_destination;
5877 	}
5878 	atomic_add_int(&sp->net->ref_count, 1);
5879 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5880 	sp->stream = srcv->sinfo_stream;
5881 	sp->msg_is_complete = 1;
5882 	sp->sender_all_done = 1;
5883 	sp->some_taken = 0;
5884 	sp->data = m;
5885 	sp->tail_mbuf = NULL;
5886 	sp->length = 0;
5887 	at = m;
5888 	sctp_set_prsctp_policy(sp);
5889 	/*
5890 	 * We could in theory (for sendall) sifa the length in, but we would
5891 	 * still have to hunt through the chain since we need to setup the
5892 	 * tail_mbuf
5893 	 */
5894 	while (at) {
5895 		if (SCTP_BUF_NEXT(at) == NULL)
5896 			sp->tail_mbuf = at;
5897 		sp->length += SCTP_BUF_LEN(at);
5898 		at = SCTP_BUF_NEXT(at);
5899 	}
5900 	SCTP_TCB_SEND_LOCK(stcb);
5901 	sctp_snd_sb_alloc(stcb, sp->length);
5902 	atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
5903 	TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
5904 	if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
5905 		sp->strseq = strm->next_sequence_sent;
5906 		strm->next_sequence_sent++;
5907 	}
5908 	if ((strm->next_spoke.tqe_next == NULL) &&
5909 	    (strm->next_spoke.tqe_prev == NULL)) {
5910 		/* Not on wheel, insert */
5911 		sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
5912 	}
5913 	m = NULL;
5914 	SCTP_TCB_SEND_UNLOCK(stcb);
5915 out_now:
5916 	if (m) {
5917 		sctp_m_freem(m);
5918 	}
5919 	return (error);
5920 }
5921 
5922 
5923 static struct mbuf *
5924 sctp_copy_mbufchain(struct mbuf *clonechain,
5925     struct mbuf *outchain,
5926     struct mbuf **endofchain,
5927     int can_take_mbuf,
5928     int sizeofcpy,
5929     uint8_t copy_by_ref)
5930 {
5931 	struct mbuf *m;
5932 	struct mbuf *appendchain;
5933 	caddr_t cp;
5934 	int len;
5935 
5936 	if (endofchain == NULL) {
5937 		/* error */
5938 error_out:
5939 		if (outchain)
5940 			sctp_m_freem(outchain);
5941 		return (NULL);
5942 	}
5943 	if (can_take_mbuf) {
5944 		appendchain = clonechain;
5945 	} else {
5946 		if (!copy_by_ref &&
5947 		    (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
5948 		    ) {
5949 			/* Its not in a cluster */
5950 			if (*endofchain == NULL) {
5951 				/* lets get a mbuf cluster */
5952 				if (outchain == NULL) {
5953 					/* This is the general case */
5954 			new_mbuf:
5955 					outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5956 					if (outchain == NULL) {
5957 						goto error_out;
5958 					}
5959 					SCTP_BUF_LEN(outchain) = 0;
5960 					*endofchain = outchain;
5961 					/* get the prepend space */
5962 					SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
5963 				} else {
5964 					/*
5965 					 * We really should not get a NULL
5966 					 * in endofchain
5967 					 */
5968 					/* find end */
5969 					m = outchain;
5970 					while (m) {
5971 						if (SCTP_BUF_NEXT(m) == NULL) {
5972 							*endofchain = m;
5973 							break;
5974 						}
5975 						m = SCTP_BUF_NEXT(m);
5976 					}
5977 					/* sanity */
5978 					if (*endofchain == NULL) {
5979 						/*
5980 						 * huh, TSNH XXX maybe we
5981 						 * should panic
5982 						 */
5983 						sctp_m_freem(outchain);
5984 						goto new_mbuf;
5985 					}
5986 				}
5987 				/* get the new end of length */
5988 				len = M_TRAILINGSPACE(*endofchain);
5989 			} else {
5990 				/* how much is left at the end? */
5991 				len = M_TRAILINGSPACE(*endofchain);
5992 			}
5993 			/* Find the end of the data, for appending */
5994 			cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
5995 
5996 			/* Now lets copy it out */
5997 			if (len >= sizeofcpy) {
5998 				/* It all fits, copy it in */
5999 				m_copydata(clonechain, 0, sizeofcpy, cp);
6000 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6001 			} else {
6002 				/* fill up the end of the chain */
6003 				if (len > 0) {
6004 					m_copydata(clonechain, 0, len, cp);
6005 					SCTP_BUF_LEN((*endofchain)) += len;
6006 					/* now we need another one */
6007 					sizeofcpy -= len;
6008 				}
6009 				m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6010 				if (m == NULL) {
6011 					/* We failed */
6012 					goto error_out;
6013 				}
6014 				SCTP_BUF_NEXT((*endofchain)) = m;
6015 				*endofchain = m;
6016 				cp = mtod((*endofchain), caddr_t);
6017 				m_copydata(clonechain, len, sizeofcpy, cp);
6018 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6019 			}
6020 			return (outchain);
6021 		} else {
6022 			/* copy the old fashion way */
6023 			appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
6024 #ifdef SCTP_MBUF_LOGGING
6025 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6026 				struct mbuf *mat;
6027 
6028 				mat = appendchain;
6029 				while (mat) {
6030 					if (SCTP_BUF_IS_EXTENDED(mat)) {
6031 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6032 					}
6033 					mat = SCTP_BUF_NEXT(mat);
6034 				}
6035 			}
6036 #endif
6037 		}
6038 	}
6039 	if (appendchain == NULL) {
6040 		/* error */
6041 		if (outchain)
6042 			sctp_m_freem(outchain);
6043 		return (NULL);
6044 	}
6045 	if (outchain) {
6046 		/* tack on to the end */
6047 		if (*endofchain != NULL) {
6048 			SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6049 		} else {
6050 			m = outchain;
6051 			while (m) {
6052 				if (SCTP_BUF_NEXT(m) == NULL) {
6053 					SCTP_BUF_NEXT(m) = appendchain;
6054 					break;
6055 				}
6056 				m = SCTP_BUF_NEXT(m);
6057 			}
6058 		}
6059 		/*
6060 		 * save off the end and update the end-chain postion
6061 		 */
6062 		m = appendchain;
6063 		while (m) {
6064 			if (SCTP_BUF_NEXT(m) == NULL) {
6065 				*endofchain = m;
6066 				break;
6067 			}
6068 			m = SCTP_BUF_NEXT(m);
6069 		}
6070 		return (outchain);
6071 	} else {
6072 		/* save off the end and update the end-chain postion */
6073 		m = appendchain;
6074 		while (m) {
6075 			if (SCTP_BUF_NEXT(m) == NULL) {
6076 				*endofchain = m;
6077 				break;
6078 			}
6079 			m = SCTP_BUF_NEXT(m);
6080 		}
6081 		return (appendchain);
6082 	}
6083 }
6084 
6085 int
6086 sctp_med_chunk_output(struct sctp_inpcb *inp,
6087     struct sctp_tcb *stcb,
6088     struct sctp_association *asoc,
6089     int *num_out,
6090     int *reason_code,
6091     int control_only, int from_where,
6092     struct timeval *now, int *now_filled, int frag_point, int so_locked
6093 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6094     SCTP_UNUSED
6095 #endif
6096 );
6097 
6098 static void
6099 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6100     uint32_t val)
6101 {
6102 	struct sctp_copy_all *ca;
6103 	struct mbuf *m;
6104 	int ret = 0;
6105 	int added_control = 0;
6106 	int un_sent, do_chunk_output = 1;
6107 	struct sctp_association *asoc;
6108 
6109 	ca = (struct sctp_copy_all *)ptr;
6110 	if (ca->m == NULL) {
6111 		return;
6112 	}
6113 	if (ca->inp != inp) {
6114 		/* TSNH */
6115 		return;
6116 	}
6117 	if ((ca->m) && ca->sndlen) {
6118 		m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6119 		if (m == NULL) {
6120 			/* can't copy so we are done */
6121 			ca->cnt_failed++;
6122 			return;
6123 		}
6124 #ifdef SCTP_MBUF_LOGGING
6125 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6126 			struct mbuf *mat;
6127 
6128 			mat = m;
6129 			while (mat) {
6130 				if (SCTP_BUF_IS_EXTENDED(mat)) {
6131 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6132 				}
6133 				mat = SCTP_BUF_NEXT(mat);
6134 			}
6135 		}
6136 #endif
6137 	} else {
6138 		m = NULL;
6139 	}
6140 	SCTP_TCB_LOCK_ASSERT(stcb);
6141 	if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6142 		/* Abort this assoc with m as the user defined reason */
6143 		if (m) {
6144 			struct sctp_paramhdr *ph;
6145 
6146 			SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6147 			if (m) {
6148 				ph = mtod(m, struct sctp_paramhdr *);
6149 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6150 				ph->param_length = htons(ca->sndlen);
6151 			}
6152 			/*
6153 			 * We add one here to keep the assoc from
6154 			 * dis-appearing on us.
6155 			 */
6156 			atomic_add_int(&stcb->asoc.refcnt, 1);
6157 			sctp_abort_an_association(inp, stcb,
6158 			    SCTP_RESPONSE_TO_USER_REQ,
6159 			    m, SCTP_SO_NOT_LOCKED);
6160 			/*
6161 			 * sctp_abort_an_association calls sctp_free_asoc()
6162 			 * free association will NOT free it since we
6163 			 * incremented the refcnt .. we do this to prevent
6164 			 * it being freed and things getting tricky since we
6165 			 * could end up (from free_asoc) calling inpcb_free
6166 			 * which would get a recursive lock call to the
6167 			 * iterator lock.. But as a consequence of that the
6168 			 * stcb will return to us un-locked.. since
6169 			 * free_asoc returns with either no TCB or the TCB
6170 			 * unlocked, we must relock.. to unlock in the
6171 			 * iterator timer :-0
6172 			 */
6173 			SCTP_TCB_LOCK(stcb);
6174 			atomic_add_int(&stcb->asoc.refcnt, -1);
6175 			goto no_chunk_output;
6176 		}
6177 	} else {
6178 		if (m) {
6179 			ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
6180 			    &ca->sndrcv, 1);
6181 		}
6182 		asoc = &stcb->asoc;
6183 		if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6184 			/* shutdown this assoc */
6185 			int cnt;
6186 
6187 			cnt = sctp_is_there_unsent_data(stcb);
6188 
6189 			if (TAILQ_EMPTY(&asoc->send_queue) &&
6190 			    TAILQ_EMPTY(&asoc->sent_queue) &&
6191 			    (cnt == 0)) {
6192 				if (asoc->locked_on_sending) {
6193 					goto abort_anyway;
6194 				}
6195 				/*
6196 				 * there is nothing queued to send, so I'm
6197 				 * done...
6198 				 */
6199 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6200 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6201 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6202 					/*
6203 					 * only send SHUTDOWN the first time
6204 					 * through
6205 					 */
6206 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
6207 					if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6208 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6209 					}
6210 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6211 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6212 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6213 					    asoc->primary_destination);
6214 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6215 					    asoc->primary_destination);
6216 					added_control = 1;
6217 					do_chunk_output = 0;
6218 				}
6219 			} else {
6220 				/*
6221 				 * we still got (or just got) data to send,
6222 				 * so set SHUTDOWN_PENDING
6223 				 */
6224 				/*
6225 				 * XXX sockets draft says that SCTP_EOF
6226 				 * should be sent with no data.  currently,
6227 				 * we will allow user data to be sent first
6228 				 * and move to SHUTDOWN-PENDING
6229 				 */
6230 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6231 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6232 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6233 					if (asoc->locked_on_sending) {
6234 						/*
6235 						 * Locked to send out the
6236 						 * data
6237 						 */
6238 						struct sctp_stream_queue_pending *sp;
6239 
6240 						sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6241 						if (sp) {
6242 							if ((sp->length == 0) && (sp->msg_is_complete == 0))
6243 								asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6244 						}
6245 					}
6246 					asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6247 					if (TAILQ_EMPTY(&asoc->send_queue) &&
6248 					    TAILQ_EMPTY(&asoc->sent_queue) &&
6249 					    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6250 				abort_anyway:
6251 						atomic_add_int(&stcb->asoc.refcnt, 1);
6252 						sctp_abort_an_association(stcb->sctp_ep, stcb,
6253 						    SCTP_RESPONSE_TO_USER_REQ,
6254 						    NULL, SCTP_SO_NOT_LOCKED);
6255 						atomic_add_int(&stcb->asoc.refcnt, -1);
6256 						goto no_chunk_output;
6257 					}
6258 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6259 					    asoc->primary_destination);
6260 				}
6261 			}
6262 
6263 		}
6264 	}
6265 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6266 	    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6267 
6268 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6269 	    (stcb->asoc.total_flight > 0) &&
6270 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6271 	    ) {
6272 		do_chunk_output = 0;
6273 	}
6274 	if (do_chunk_output)
6275 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6276 	else if (added_control) {
6277 		int num_out = 0, reason = 0, now_filled = 0;
6278 		struct timeval now;
6279 		int frag_point;
6280 
6281 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6282 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6283 		    &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6284 	}
6285 no_chunk_output:
6286 	if (ret) {
6287 		ca->cnt_failed++;
6288 	} else {
6289 		ca->cnt_sent++;
6290 	}
6291 }
6292 
6293 static void
6294 sctp_sendall_completes(void *ptr, uint32_t val)
6295 {
6296 	struct sctp_copy_all *ca;
6297 
6298 	ca = (struct sctp_copy_all *)ptr;
6299 	/*
6300 	 * Do a notify here? Kacheong suggests that the notify be done at
6301 	 * the send time.. so you would push up a notification if any send
6302 	 * failed. Don't know if this is feasable since the only failures we
6303 	 * have is "memory" related and if you cannot get an mbuf to send
6304 	 * the data you surely can't get an mbuf to send up to notify the
6305 	 * user you can't send the data :->
6306 	 */
6307 
6308 	/* now free everything */
6309 	sctp_m_freem(ca->m);
6310 	SCTP_FREE(ca, SCTP_M_COPYAL);
6311 }
6312 
6313 
6314 #define	MC_ALIGN(m, len) do {						\
6315 	SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1));	\
6316 } while (0)
6317 
6318 
6319 
6320 static struct mbuf *
6321 sctp_copy_out_all(struct uio *uio, int len)
6322 {
6323 	struct mbuf *ret, *at;
6324 	int left, willcpy, cancpy, error;
6325 
6326 	ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6327 	if (ret == NULL) {
6328 		/* TSNH */
6329 		return (NULL);
6330 	}
6331 	left = len;
6332 	SCTP_BUF_LEN(ret) = 0;
6333 	/* save space for the data chunk header */
6334 	cancpy = M_TRAILINGSPACE(ret);
6335 	willcpy = min(cancpy, left);
6336 	at = ret;
6337 	while (left > 0) {
6338 		/* Align data to the end */
6339 		error = uiomove(mtod(at, caddr_t), willcpy, uio);
6340 		if (error) {
6341 	err_out_now:
6342 			sctp_m_freem(at);
6343 			return (NULL);
6344 		}
6345 		SCTP_BUF_LEN(at) = willcpy;
6346 		SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6347 		left -= willcpy;
6348 		if (left > 0) {
6349 			SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6350 			if (SCTP_BUF_NEXT(at) == NULL) {
6351 				goto err_out_now;
6352 			}
6353 			at = SCTP_BUF_NEXT(at);
6354 			SCTP_BUF_LEN(at) = 0;
6355 			cancpy = M_TRAILINGSPACE(at);
6356 			willcpy = min(cancpy, left);
6357 		}
6358 	}
6359 	return (ret);
6360 }
6361 
6362 static int
6363 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6364     struct sctp_sndrcvinfo *srcv)
6365 {
6366 	int ret;
6367 	struct sctp_copy_all *ca;
6368 
6369 	SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6370 	    SCTP_M_COPYAL);
6371 	if (ca == NULL) {
6372 		sctp_m_freem(m);
6373 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6374 		return (ENOMEM);
6375 	}
6376 	memset(ca, 0, sizeof(struct sctp_copy_all));
6377 
6378 	ca->inp = inp;
6379 	memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6380 	/*
6381 	 * take off the sendall flag, it would be bad if we failed to do
6382 	 * this :-0
6383 	 */
6384 	ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6385 	/* get length and mbuf chain */
6386 	if (uio) {
6387 		ca->sndlen = uio->uio_resid;
6388 		ca->m = sctp_copy_out_all(uio, ca->sndlen);
6389 		if (ca->m == NULL) {
6390 			SCTP_FREE(ca, SCTP_M_COPYAL);
6391 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6392 			return (ENOMEM);
6393 		}
6394 	} else {
6395 		/* Gather the length of the send */
6396 		struct mbuf *mat;
6397 
6398 		mat = m;
6399 		ca->sndlen = 0;
6400 		while (m) {
6401 			ca->sndlen += SCTP_BUF_LEN(m);
6402 			m = SCTP_BUF_NEXT(m);
6403 		}
6404 		ca->m = mat;
6405 	}
6406 	ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6407 	    SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6408 	    SCTP_ASOC_ANY_STATE,
6409 	    (void *)ca, 0,
6410 	    sctp_sendall_completes, inp, 1);
6411 	if (ret) {
6412 		SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6413 		SCTP_FREE(ca, SCTP_M_COPYAL);
6414 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6415 		return (EFAULT);
6416 	}
6417 	return (0);
6418 }
6419 
6420 
6421 void
6422 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6423 {
6424 	struct sctp_tmit_chunk *chk, *nchk;
6425 
6426 	chk = TAILQ_FIRST(&asoc->control_send_queue);
6427 	while (chk) {
6428 		nchk = TAILQ_NEXT(chk, sctp_next);
6429 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6430 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6431 			if (chk->data) {
6432 				sctp_m_freem(chk->data);
6433 				chk->data = NULL;
6434 			}
6435 			asoc->ctrl_queue_cnt--;
6436 			sctp_free_a_chunk(stcb, chk);
6437 		}
6438 		chk = nchk;
6439 	}
6440 }
6441 
6442 void
6443 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6444 {
6445 	struct sctp_association *asoc;
6446 	struct sctp_tmit_chunk *chk, *chk_tmp;
6447 	struct sctp_asconf_chunk *acp;
6448 
6449 	asoc = &stcb->asoc;
6450 	for (chk = TAILQ_FIRST(&asoc->asconf_send_queue); chk != NULL;
6451 	    chk = chk_tmp) {
6452 		/* get next chk */
6453 		chk_tmp = TAILQ_NEXT(chk, sctp_next);
6454 		/* find SCTP_ASCONF chunk in queue */
6455 		if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6456 			if (chk->data) {
6457 				acp = mtod(chk->data, struct sctp_asconf_chunk *);
6458 				if (compare_with_wrap(ntohl(acp->serial_number), stcb->asoc.asconf_seq_out_acked, MAX_SEQ)) {
6459 					/* Not Acked yet */
6460 					break;
6461 				}
6462 			}
6463 			TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6464 			if (chk->data) {
6465 				sctp_m_freem(chk->data);
6466 				chk->data = NULL;
6467 			}
6468 			asoc->ctrl_queue_cnt--;
6469 			sctp_free_a_chunk(stcb, chk);
6470 		}
6471 	}
6472 }
6473 
6474 
6475 static void
6476 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6477 
6478     struct sctp_association *asoc,
6479     struct sctp_tmit_chunk **data_list,
6480     int bundle_at,
6481     struct sctp_nets *net)
6482 {
6483 	int i;
6484 	struct sctp_tmit_chunk *tp1;
6485 
6486 	for (i = 0; i < bundle_at; i++) {
6487 		/* off of the send queue */
6488 		if (i) {
6489 			/*
6490 			 * Any chunk NOT 0 you zap the time chunk 0 gets
6491 			 * zapped or set based on if a RTO measurment is
6492 			 * needed.
6493 			 */
6494 			data_list[i]->do_rtt = 0;
6495 		}
6496 		/* record time */
6497 		data_list[i]->sent_rcv_time = net->last_sent_time;
6498 		data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6499 		TAILQ_REMOVE(&asoc->send_queue,
6500 		    data_list[i],
6501 		    sctp_next);
6502 		/* on to the sent queue */
6503 		tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6504 		if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
6505 		    data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
6506 			struct sctp_tmit_chunk *tpp;
6507 
6508 			/* need to move back */
6509 	back_up_more:
6510 			tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6511 			if (tpp == NULL) {
6512 				TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6513 				goto all_done;
6514 			}
6515 			tp1 = tpp;
6516 			if (compare_with_wrap(tp1->rec.data.TSN_seq,
6517 			    data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
6518 				goto back_up_more;
6519 			}
6520 			TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6521 		} else {
6522 			TAILQ_INSERT_TAIL(&asoc->sent_queue,
6523 			    data_list[i],
6524 			    sctp_next);
6525 		}
6526 all_done:
6527 		/* This does not lower until the cum-ack passes it */
6528 		asoc->sent_queue_cnt++;
6529 		asoc->send_queue_cnt--;
6530 		if ((asoc->peers_rwnd <= 0) &&
6531 		    (asoc->total_flight == 0) &&
6532 		    (bundle_at == 1)) {
6533 			/* Mark the chunk as being a window probe */
6534 			SCTP_STAT_INCR(sctps_windowprobed);
6535 		}
6536 #ifdef SCTP_AUDITING_ENABLED
6537 		sctp_audit_log(0xC2, 3);
6538 #endif
6539 		data_list[i]->sent = SCTP_DATAGRAM_SENT;
6540 		data_list[i]->snd_count = 1;
6541 		data_list[i]->rec.data.chunk_was_revoked = 0;
6542 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6543 			sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6544 			    data_list[i]->whoTo->flight_size,
6545 			    data_list[i]->book_size,
6546 			    (uintptr_t) data_list[i]->whoTo,
6547 			    data_list[i]->rec.data.TSN_seq);
6548 		}
6549 		sctp_flight_size_increase(data_list[i]);
6550 		sctp_total_flight_increase(stcb, data_list[i]);
6551 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6552 			sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6553 			    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6554 		}
6555 		asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6556 		    (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6557 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6558 			/* SWS sender side engages */
6559 			asoc->peers_rwnd = 0;
6560 		}
6561 	}
6562 }
6563 
6564 static void
6565 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
6566 {
6567 	struct sctp_tmit_chunk *chk, *nchk;
6568 
6569 	for (chk = TAILQ_FIRST(&asoc->control_send_queue);
6570 	    chk; chk = nchk) {
6571 		nchk = TAILQ_NEXT(chk, sctp_next);
6572 		if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6573 		    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
6574 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6575 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6576 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
6577 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6578 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6579 		    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6580 		    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6581 		    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6582 		    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6583 		    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6584 			/* Stray chunks must be cleaned up */
6585 	clean_up_anyway:
6586 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6587 			if (chk->data) {
6588 				sctp_m_freem(chk->data);
6589 				chk->data = NULL;
6590 			}
6591 			asoc->ctrl_queue_cnt--;
6592 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)
6593 				asoc->fwd_tsn_cnt--;
6594 			sctp_free_a_chunk(stcb, chk);
6595 		} else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6596 			/* special handling, we must look into the param */
6597 			if (chk != asoc->str_reset) {
6598 				goto clean_up_anyway;
6599 			}
6600 		}
6601 	}
6602 }
6603 
6604 
6605 static int
6606 sctp_can_we_split_this(struct sctp_tcb *stcb,
6607     uint32_t length,
6608     uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6609 {
6610 	/*
6611 	 * Make a decision on if I should split a msg into multiple parts.
6612 	 * This is only asked of incomplete messages.
6613 	 */
6614 	if (eeor_on) {
6615 		/*
6616 		 * If we are doing EEOR we need to always send it if its the
6617 		 * entire thing, since it might be all the guy is putting in
6618 		 * the hopper.
6619 		 */
6620 		if (goal_mtu >= length) {
6621 			/*-
6622 			 * If we have data outstanding,
6623 			 * we get another chance when the sack
6624 			 * arrives to transmit - wait for more data
6625 			 */
6626 			if (stcb->asoc.total_flight == 0) {
6627 				/*
6628 				 * If nothing is in flight, we zero the
6629 				 * packet counter.
6630 				 */
6631 				return (length);
6632 			}
6633 			return (0);
6634 
6635 		} else {
6636 			/* You can fill the rest */
6637 			return (goal_mtu);
6638 		}
6639 	}
6640 	/*-
6641 	 * For those strange folk that make the send buffer
6642 	 * smaller than our fragmentation point, we can't
6643 	 * get a full msg in so we have to allow splitting.
6644 	 */
6645 	if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
6646 		return (length);
6647 	}
6648 	if ((length <= goal_mtu) ||
6649 	    ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
6650 		/* Sub-optimial residual don't split in non-eeor mode. */
6651 		return (0);
6652 	}
6653 	/*
6654 	 * If we reach here length is larger than the goal_mtu. Do we wish
6655 	 * to split it for the sake of packet putting together?
6656 	 */
6657 	if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
6658 		/* Its ok to split it */
6659 		return (min(goal_mtu, frag_point));
6660 	}
6661 	/* Nope, can't split */
6662 	return (0);
6663 
6664 }
6665 
6666 static uint32_t
6667 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
6668     struct sctp_stream_out *strq,
6669     uint32_t goal_mtu,
6670     uint32_t frag_point,
6671     int *locked,
6672     int *giveup,
6673     int eeor_mode,
6674     int *bail)
6675 {
6676 	/* Move from the stream to the send_queue keeping track of the total */
6677 	struct sctp_association *asoc;
6678 	struct sctp_stream_queue_pending *sp;
6679 	struct sctp_tmit_chunk *chk;
6680 	struct sctp_data_chunk *dchkh;
6681 	uint32_t to_move, length;
6682 	uint8_t rcv_flags = 0;
6683 	uint8_t some_taken;
6684 	uint8_t send_lock_up = 0;
6685 
6686 	SCTP_TCB_LOCK_ASSERT(stcb);
6687 	asoc = &stcb->asoc;
6688 one_more_time:
6689 	/* sa_ignore FREED_MEMORY */
6690 	sp = TAILQ_FIRST(&strq->outqueue);
6691 	if (sp == NULL) {
6692 		*locked = 0;
6693 		if (send_lock_up == 0) {
6694 			SCTP_TCB_SEND_LOCK(stcb);
6695 			send_lock_up = 1;
6696 		}
6697 		sp = TAILQ_FIRST(&strq->outqueue);
6698 		if (sp) {
6699 			goto one_more_time;
6700 		}
6701 		if (strq->last_msg_incomplete) {
6702 			SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6703 			    strq->stream_no,
6704 			    strq->last_msg_incomplete);
6705 			strq->last_msg_incomplete = 0;
6706 		}
6707 		to_move = 0;
6708 		if (send_lock_up) {
6709 			SCTP_TCB_SEND_UNLOCK(stcb);
6710 			send_lock_up = 0;
6711 		}
6712 		goto out_of;
6713 	}
6714 	if ((sp->msg_is_complete) && (sp->length == 0)) {
6715 		if (sp->sender_all_done) {
6716 			/*
6717 			 * We are doing differed cleanup. Last time through
6718 			 * when we took all the data the sender_all_done was
6719 			 * not set.
6720 			 */
6721 			if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
6722 				SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6723 				SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6724 				    sp->sender_all_done,
6725 				    sp->length,
6726 				    sp->msg_is_complete,
6727 				    sp->put_last_out,
6728 				    send_lock_up);
6729 			}
6730 			if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
6731 				SCTP_TCB_SEND_LOCK(stcb);
6732 				send_lock_up = 1;
6733 			}
6734 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6735 			TAILQ_REMOVE(&strq->outqueue, sp, next);
6736 			sctp_free_remote_addr(sp->net);
6737 			if (sp->data) {
6738 				sctp_m_freem(sp->data);
6739 				sp->data = NULL;
6740 			}
6741 			sctp_free_a_strmoq(stcb, sp);
6742 			/* we can't be locked to it */
6743 			*locked = 0;
6744 			stcb->asoc.locked_on_sending = NULL;
6745 			if (send_lock_up) {
6746 				SCTP_TCB_SEND_UNLOCK(stcb);
6747 				send_lock_up = 0;
6748 			}
6749 			/* back to get the next msg */
6750 			goto one_more_time;
6751 		} else {
6752 			/*
6753 			 * sender just finished this but still holds a
6754 			 * reference
6755 			 */
6756 			*locked = 1;
6757 			*giveup = 1;
6758 			to_move = 0;
6759 			goto out_of;
6760 		}
6761 	} else {
6762 		/* is there some to get */
6763 		if (sp->length == 0) {
6764 			/* no */
6765 			*locked = 1;
6766 			*giveup = 1;
6767 			to_move = 0;
6768 			goto out_of;
6769 		} else if (sp->discard_rest) {
6770 			if (send_lock_up == 0) {
6771 				SCTP_TCB_SEND_LOCK(stcb);
6772 				send_lock_up = 1;
6773 			}
6774 			/* Whack down the size */
6775 			atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
6776 			if ((stcb->sctp_socket != NULL) && \
6777 			    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6778 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
6779 				atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
6780 			}
6781 			if (sp->data) {
6782 				sctp_m_freem(sp->data);
6783 				sp->data = NULL;
6784 				sp->tail_mbuf = NULL;
6785 			}
6786 			sp->length = 0;
6787 			sp->some_taken = 1;
6788 			*locked = 1;
6789 			*giveup = 1;
6790 			to_move = 0;
6791 			goto out_of;
6792 		}
6793 	}
6794 	some_taken = sp->some_taken;
6795 	if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6796 		sp->msg_is_complete = 1;
6797 	}
6798 re_look:
6799 	length = sp->length;
6800 	if (sp->msg_is_complete) {
6801 		/* The message is complete */
6802 		to_move = min(length, frag_point);
6803 		if (to_move == length) {
6804 			/* All of it fits in the MTU */
6805 			if (sp->some_taken) {
6806 				rcv_flags |= SCTP_DATA_LAST_FRAG;
6807 				sp->put_last_out = 1;
6808 			} else {
6809 				rcv_flags |= SCTP_DATA_NOT_FRAG;
6810 				sp->put_last_out = 1;
6811 			}
6812 		} else {
6813 			/* Not all of it fits, we fragment */
6814 			if (sp->some_taken == 0) {
6815 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
6816 			}
6817 			sp->some_taken = 1;
6818 		}
6819 	} else {
6820 		to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
6821 		if (to_move) {
6822 			/*-
6823 			 * We use a snapshot of length in case it
6824 			 * is expanding during the compare.
6825 			 */
6826 			uint32_t llen;
6827 
6828 			llen = length;
6829 			if (to_move >= llen) {
6830 				to_move = llen;
6831 				if (send_lock_up == 0) {
6832 					/*-
6833 					 * We are taking all of an incomplete msg
6834 					 * thus we need a send lock.
6835 					 */
6836 					SCTP_TCB_SEND_LOCK(stcb);
6837 					send_lock_up = 1;
6838 					if (sp->msg_is_complete) {
6839 						/*
6840 						 * the sender finished the
6841 						 * msg
6842 						 */
6843 						goto re_look;
6844 					}
6845 				}
6846 			}
6847 			if (sp->some_taken == 0) {
6848 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
6849 				sp->some_taken = 1;
6850 			}
6851 		} else {
6852 			/* Nothing to take. */
6853 			if (sp->some_taken) {
6854 				*locked = 1;
6855 			}
6856 			*giveup = 1;
6857 			to_move = 0;
6858 			goto out_of;
6859 		}
6860 	}
6861 
6862 	/* If we reach here, we can copy out a chunk */
6863 	sctp_alloc_a_chunk(stcb, chk);
6864 	if (chk == NULL) {
6865 		/* No chunk memory */
6866 		*giveup = 1;
6867 		to_move = 0;
6868 		goto out_of;
6869 	}
6870 	/*
6871 	 * Setup for unordered if needed by looking at the user sent info
6872 	 * flags.
6873 	 */
6874 	if (sp->sinfo_flags & SCTP_UNORDERED) {
6875 		rcv_flags |= SCTP_DATA_UNORDERED;
6876 	}
6877 	if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
6878 	    ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
6879 		rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
6880 	}
6881 	/* clear out the chunk before setting up */
6882 	memset(chk, 0, sizeof(*chk));
6883 	chk->rec.data.rcv_flags = rcv_flags;
6884 
6885 	if (to_move >= length) {
6886 		/* we think we can steal the whole thing */
6887 		if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
6888 			SCTP_TCB_SEND_LOCK(stcb);
6889 			send_lock_up = 1;
6890 		}
6891 		if (to_move < sp->length) {
6892 			/* bail, it changed */
6893 			goto dont_do_it;
6894 		}
6895 		chk->data = sp->data;
6896 		chk->last_mbuf = sp->tail_mbuf;
6897 		/* register the stealing */
6898 		sp->data = sp->tail_mbuf = NULL;
6899 	} else {
6900 		struct mbuf *m;
6901 
6902 dont_do_it:
6903 		chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
6904 		chk->last_mbuf = NULL;
6905 		if (chk->data == NULL) {
6906 			sp->some_taken = some_taken;
6907 			sctp_free_a_chunk(stcb, chk);
6908 			*bail = 1;
6909 			to_move = 0;
6910 			goto out_of;
6911 		}
6912 #ifdef SCTP_MBUF_LOGGING
6913 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6914 			struct mbuf *mat;
6915 
6916 			mat = chk->data;
6917 			while (mat) {
6918 				if (SCTP_BUF_IS_EXTENDED(mat)) {
6919 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6920 				}
6921 				mat = SCTP_BUF_NEXT(mat);
6922 			}
6923 		}
6924 #endif
6925 		/* Pull off the data */
6926 		m_adj(sp->data, to_move);
6927 		/* Now lets work our way down and compact it */
6928 		m = sp->data;
6929 		while (m && (SCTP_BUF_LEN(m) == 0)) {
6930 			sp->data = SCTP_BUF_NEXT(m);
6931 			SCTP_BUF_NEXT(m) = NULL;
6932 			if (sp->tail_mbuf == m) {
6933 				/*-
6934 				 * Freeing tail? TSNH since
6935 				 * we supposedly were taking less
6936 				 * than the sp->length.
6937 				 */
6938 #ifdef INVARIANTS
6939 				panic("Huh, freing tail? - TSNH");
6940 #else
6941 				SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
6942 				sp->tail_mbuf = sp->data = NULL;
6943 				sp->length = 0;
6944 #endif
6945 
6946 			}
6947 			sctp_m_free(m);
6948 			m = sp->data;
6949 		}
6950 	}
6951 	if (SCTP_BUF_IS_EXTENDED(chk->data)) {
6952 		chk->copy_by_ref = 1;
6953 	} else {
6954 		chk->copy_by_ref = 0;
6955 	}
6956 	/*
6957 	 * get last_mbuf and counts of mb useage This is ugly but hopefully
6958 	 * its only one mbuf.
6959 	 */
6960 	if (chk->last_mbuf == NULL) {
6961 		chk->last_mbuf = chk->data;
6962 		while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
6963 			chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
6964 		}
6965 	}
6966 	if (to_move > length) {
6967 		/*- This should not happen either
6968 		 * since we always lower to_move to the size
6969 		 * of sp->length if its larger.
6970 		 */
6971 #ifdef INVARIANTS
6972 		panic("Huh, how can to_move be larger?");
6973 #else
6974 		SCTP_PRINTF("Huh, how can to_move be larger?\n");
6975 		sp->length = 0;
6976 #endif
6977 	} else {
6978 		atomic_subtract_int(&sp->length, to_move);
6979 	}
6980 	if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
6981 		/* Not enough room for a chunk header, get some */
6982 		struct mbuf *m;
6983 
6984 		m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
6985 		if (m == NULL) {
6986 			/*
6987 			 * we're in trouble here. _PREPEND below will free
6988 			 * all the data if there is no leading space, so we
6989 			 * must put the data back and restore.
6990 			 */
6991 			if (send_lock_up == 0) {
6992 				SCTP_TCB_SEND_LOCK(stcb);
6993 				send_lock_up = 1;
6994 			}
6995 			if (chk->data == NULL) {
6996 				/* unsteal the data */
6997 				sp->data = chk->data;
6998 				sp->tail_mbuf = chk->last_mbuf;
6999 			} else {
7000 				struct mbuf *m_tmp;
7001 
7002 				/* reassemble the data */
7003 				m_tmp = sp->data;
7004 				sp->data = chk->data;
7005 				SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7006 			}
7007 			sp->some_taken = some_taken;
7008 			atomic_add_int(&sp->length, to_move);
7009 			chk->data = NULL;
7010 			*bail = 1;
7011 			sctp_free_a_chunk(stcb, chk);
7012 			to_move = 0;
7013 			goto out_of;
7014 		} else {
7015 			SCTP_BUF_LEN(m) = 0;
7016 			SCTP_BUF_NEXT(m) = chk->data;
7017 			chk->data = m;
7018 			M_ALIGN(chk->data, 4);
7019 		}
7020 	}
7021 	SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
7022 	if (chk->data == NULL) {
7023 		/* HELP, TSNH since we assured it would not above? */
7024 #ifdef INVARIANTS
7025 		panic("prepend failes HELP?");
7026 #else
7027 		SCTP_PRINTF("prepend fails HELP?\n");
7028 		sctp_free_a_chunk(stcb, chk);
7029 #endif
7030 		*bail = 1;
7031 		to_move = 0;
7032 		goto out_of;
7033 	}
7034 	sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
7035 	chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
7036 	chk->book_size_scale = 0;
7037 	chk->sent = SCTP_DATAGRAM_UNSENT;
7038 
7039 	chk->flags = 0;
7040 	chk->asoc = &stcb->asoc;
7041 	chk->pad_inplace = 0;
7042 	chk->no_fr_allowed = 0;
7043 	chk->rec.data.stream_seq = sp->strseq;
7044 	chk->rec.data.stream_number = sp->stream;
7045 	chk->rec.data.payloadtype = sp->ppid;
7046 	chk->rec.data.context = sp->context;
7047 	chk->rec.data.doing_fast_retransmit = 0;
7048 	chk->rec.data.ect_nonce = 0;	/* ECN Nonce */
7049 
7050 	chk->rec.data.timetodrop = sp->ts;
7051 	chk->flags = sp->act_flags;
7052 
7053 	chk->whoTo = net;
7054 	atomic_add_int(&chk->whoTo->ref_count, 1);
7055 
7056 	if (sp->holds_key_ref) {
7057 		chk->auth_keyid = sp->auth_keyid;
7058 		sctp_auth_key_acquire(stcb, chk->auth_keyid);
7059 		chk->holds_key_ref = 1;
7060 	}
7061 	chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
7062 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7063 		sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7064 		    (uintptr_t) stcb, sp->length,
7065 		    (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
7066 		    chk->rec.data.TSN_seq);
7067 	}
7068 	dchkh = mtod(chk->data, struct sctp_data_chunk *);
7069 	/*
7070 	 * Put the rest of the things in place now. Size was done earlier in
7071 	 * previous loop prior to padding.
7072 	 */
7073 
7074 #ifdef SCTP_ASOCLOG_OF_TSNS
7075 	SCTP_TCB_LOCK_ASSERT(stcb);
7076 	if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7077 		asoc->tsn_out_at = 0;
7078 		asoc->tsn_out_wrapped = 1;
7079 	}
7080 	asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7081 	asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7082 	asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7083 	asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7084 	asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7085 	asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7086 	asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7087 	asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7088 	asoc->tsn_out_at++;
7089 #endif
7090 
7091 	dchkh->ch.chunk_type = SCTP_DATA;
7092 	dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7093 	dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7094 	dchkh->dp.stream_id = htons(strq->stream_no);
7095 	dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7096 	dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7097 	dchkh->ch.chunk_length = htons(chk->send_size);
7098 	/* Now advance the chk->send_size by the actual pad needed. */
7099 	if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7100 		/* need a pad */
7101 		struct mbuf *lm;
7102 		int pads;
7103 
7104 		pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7105 		if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7106 			chk->pad_inplace = 1;
7107 		}
7108 		if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7109 			/* pad added an mbuf */
7110 			chk->last_mbuf = lm;
7111 		}
7112 		chk->send_size += pads;
7113 	}
7114 	/* We only re-set the policy if it is on */
7115 	if (sp->pr_sctp_on) {
7116 		sctp_set_prsctp_policy(sp);
7117 		asoc->pr_sctp_cnt++;
7118 		chk->pr_sctp_on = 1;
7119 	} else {
7120 		chk->pr_sctp_on = 0;
7121 	}
7122 	if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7123 		/* All done pull and kill the message */
7124 		atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7125 		if (sp->put_last_out == 0) {
7126 			SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7127 			SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7128 			    sp->sender_all_done,
7129 			    sp->length,
7130 			    sp->msg_is_complete,
7131 			    sp->put_last_out,
7132 			    send_lock_up);
7133 		}
7134 		if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7135 			SCTP_TCB_SEND_LOCK(stcb);
7136 			send_lock_up = 1;
7137 		}
7138 		TAILQ_REMOVE(&strq->outqueue, sp, next);
7139 		sctp_free_remote_addr(sp->net);
7140 		if (sp->data) {
7141 			sctp_m_freem(sp->data);
7142 			sp->data = NULL;
7143 		}
7144 		sctp_free_a_strmoq(stcb, sp);
7145 
7146 		/* we can't be locked to it */
7147 		*locked = 0;
7148 		stcb->asoc.locked_on_sending = NULL;
7149 	} else {
7150 		/* more to go, we are locked */
7151 		*locked = 1;
7152 	}
7153 	asoc->chunks_on_out_queue++;
7154 	TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7155 	asoc->send_queue_cnt++;
7156 out_of:
7157 	if (send_lock_up) {
7158 		SCTP_TCB_SEND_UNLOCK(stcb);
7159 		send_lock_up = 0;
7160 	}
7161 	return (to_move);
7162 }
7163 
7164 
7165 static struct sctp_stream_out *
7166 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
7167 {
7168 	struct sctp_stream_out *strq;
7169 
7170 	/* Find the next stream to use */
7171 	if (asoc->last_out_stream == NULL) {
7172 		strq = TAILQ_FIRST(&asoc->out_wheel);
7173 	} else {
7174 		strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7175 		if (strq == NULL) {
7176 			strq = TAILQ_FIRST(&asoc->out_wheel);
7177 		}
7178 	}
7179 	return (strq);
7180 }
7181 
7182 
7183 static void
7184 sctp_fill_outqueue(struct sctp_tcb *stcb,
7185     struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
7186 {
7187 	struct sctp_association *asoc;
7188 	struct sctp_stream_out *strq, *strqn;
7189 	int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7190 	int locked, giveup;
7191 	struct sctp_stream_queue_pending *sp;
7192 
7193 	SCTP_TCB_LOCK_ASSERT(stcb);
7194 	asoc = &stcb->asoc;
7195 #ifdef INET6
7196 	if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
7197 		goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7198 	} else {
7199 		/* ?? not sure what else to do */
7200 		goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7201 	}
7202 #else
7203 	goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7204 #endif
7205 	/* Need an allowance for the data chunk header too */
7206 	goal_mtu -= sizeof(struct sctp_data_chunk);
7207 
7208 	/* must make even word boundary */
7209 	goal_mtu &= 0xfffffffc;
7210 	if (asoc->locked_on_sending) {
7211 		/* We are stuck on one stream until the message completes. */
7212 		strqn = strq = asoc->locked_on_sending;
7213 		locked = 1;
7214 	} else {
7215 		strqn = strq = sctp_select_a_stream(stcb, asoc);
7216 		locked = 0;
7217 	}
7218 
7219 	while ((goal_mtu > 0) && strq) {
7220 		sp = TAILQ_FIRST(&strq->outqueue);
7221 		/*
7222 		 * If CMT is off, we must validate that the stream in
7223 		 * question has the first item pointed towards are network
7224 		 * destionation requested by the caller. Note that if we
7225 		 * turn out to be locked to a stream (assigning TSN's then
7226 		 * we must stop, since we cannot look for another stream
7227 		 * with data to send to that destination). In CMT's case, by
7228 		 * skipping this check, we will send one data packet towards
7229 		 * the requested net.
7230 		 */
7231 		if (sp == NULL) {
7232 			break;
7233 		}
7234 		if ((sp->net != net) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
7235 			/* none for this network */
7236 			if (locked) {
7237 				break;
7238 			} else {
7239 				strq = sctp_select_a_stream(stcb, asoc);
7240 				if (strq == NULL)
7241 					/* none left */
7242 					break;
7243 				if (strqn == strq) {
7244 					/* I have circled */
7245 					break;
7246 				}
7247 				continue;
7248 			}
7249 		}
7250 		giveup = 0;
7251 		bail = 0;
7252 		moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
7253 		    &giveup, eeor_mode, &bail);
7254 		if (moved_how_much)
7255 			asoc->last_out_stream = strq;
7256 
7257 		if (locked) {
7258 			asoc->locked_on_sending = strq;
7259 			if ((moved_how_much == 0) || (giveup) || bail)
7260 				/* no more to move for now */
7261 				break;
7262 		} else {
7263 			asoc->locked_on_sending = NULL;
7264 			if (TAILQ_EMPTY(&strq->outqueue)) {
7265 				if (strq == strqn) {
7266 					/* Must move start to next one */
7267 					strqn = TAILQ_NEXT(strq, next_spoke);
7268 					if (strqn == NULL) {
7269 						strqn = TAILQ_FIRST(&asoc->out_wheel);
7270 						if (strqn == NULL) {
7271 							break;
7272 						}
7273 					}
7274 				}
7275 				sctp_remove_from_wheel(stcb, asoc, strq, 0);
7276 			}
7277 			if ((giveup) || bail) {
7278 				break;
7279 			}
7280 			strq = sctp_select_a_stream(stcb, asoc);
7281 			if (strq == NULL) {
7282 				break;
7283 			}
7284 		}
7285 		total_moved += moved_how_much;
7286 		goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7287 		goal_mtu &= 0xfffffffc;
7288 	}
7289 	if (bail)
7290 		*quit_now = 1;
7291 
7292 	if (total_moved == 0) {
7293 		if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) &&
7294 		    (net == stcb->asoc.primary_destination)) {
7295 			/* ran dry for primary network net */
7296 			SCTP_STAT_INCR(sctps_primary_randry);
7297 		} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7298 			/* ran dry with CMT on */
7299 			SCTP_STAT_INCR(sctps_cmt_randry);
7300 		}
7301 	}
7302 }
7303 
7304 void
7305 sctp_fix_ecn_echo(struct sctp_association *asoc)
7306 {
7307 	struct sctp_tmit_chunk *chk;
7308 
7309 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7310 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7311 			chk->sent = SCTP_DATAGRAM_UNSENT;
7312 		}
7313 	}
7314 }
7315 
7316 static void
7317 sctp_move_to_an_alt(struct sctp_tcb *stcb,
7318     struct sctp_association *asoc,
7319     struct sctp_nets *net)
7320 {
7321 	struct sctp_tmit_chunk *chk;
7322 	struct sctp_nets *a_net;
7323 
7324 	SCTP_TCB_LOCK_ASSERT(stcb);
7325 	/*
7326 	 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
7327 	 * destination using the PF algorithm for finding alternate
7328 	 * destinations.
7329 	 */
7330 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
7331 		a_net = sctp_find_alternate_net(stcb, net, 2);
7332 	} else {
7333 		a_net = sctp_find_alternate_net(stcb, net, 0);
7334 	}
7335 	if ((a_net != net) &&
7336 	    ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
7337 		/*
7338 		 * We only proceed if a valid alternate is found that is not
7339 		 * this one and is reachable. Here we must move all chunks
7340 		 * queued in the send queue off of the destination address
7341 		 * to our alternate.
7342 		 */
7343 		TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7344 			if (chk->whoTo == net) {
7345 				/* Move the chunk to our alternate */
7346 				sctp_free_remote_addr(chk->whoTo);
7347 				chk->whoTo = a_net;
7348 				atomic_add_int(&a_net->ref_count, 1);
7349 			}
7350 		}
7351 	}
7352 }
7353 
7354 int
7355 sctp_med_chunk_output(struct sctp_inpcb *inp,
7356     struct sctp_tcb *stcb,
7357     struct sctp_association *asoc,
7358     int *num_out,
7359     int *reason_code,
7360     int control_only, int from_where,
7361     struct timeval *now, int *now_filled, int frag_point, int so_locked
7362 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7363     SCTP_UNUSED
7364 #endif
7365 )
7366 {
7367 	/*
7368 	 * Ok this is the generic chunk service queue. we must do the
7369 	 * following: - Service the stream queue that is next, moving any
7370 	 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7371 	 * LAST to the out queue in one pass) and assigning TSN's - Check to
7372 	 * see if the cwnd/rwnd allows any output, if so we go ahead and
7373 	 * fomulate and send the low level chunks. Making sure to combine
7374 	 * any control in the control chunk queue also.
7375 	 */
7376 	struct sctp_nets *net, *start_at, *old_start_at = NULL;
7377 	struct mbuf *outchain, *endoutchain;
7378 	struct sctp_tmit_chunk *chk, *nchk;
7379 
7380 	/* temp arrays for unlinking */
7381 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7382 	int no_fragmentflg, error;
7383 	unsigned int max_rwnd_per_dest;
7384 	int one_chunk, hbflag, skip_data_for_this_net;
7385 	int asconf, cookie, no_out_cnt;
7386 	int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7387 	unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7388 	int tsns_sent = 0;
7389 	uint32_t auth_offset = 0;
7390 	struct sctp_auth_chunk *auth = NULL;
7391 	uint16_t auth_keyid;
7392 	int override_ok = 1;
7393 	int data_auth_reqd = 0;
7394 
7395 	/*
7396 	 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7397 	 * destination.
7398 	 */
7399 	int pf_hbflag = 0;
7400 	int quit_now = 0;
7401 
7402 	*num_out = 0;
7403 	auth_keyid = stcb->asoc.authinfo.active_keyid;
7404 
7405 	if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7406 	    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7407 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7408 		eeor_mode = 1;
7409 	} else {
7410 		eeor_mode = 0;
7411 	}
7412 	ctl_cnt = no_out_cnt = asconf = cookie = 0;
7413 	/*
7414 	 * First lets prime the pump. For each destination, if there is room
7415 	 * in the flight size, attempt to pull an MTU's worth out of the
7416 	 * stream queues into the general send_queue
7417 	 */
7418 #ifdef SCTP_AUDITING_ENABLED
7419 	sctp_audit_log(0xC2, 2);
7420 #endif
7421 	SCTP_TCB_LOCK_ASSERT(stcb);
7422 	hbflag = 0;
7423 	if ((control_only) || (asoc->stream_reset_outstanding))
7424 		no_data_chunks = 1;
7425 	else
7426 		no_data_chunks = 0;
7427 
7428 	/* Nothing to possible to send? */
7429 	if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7430 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7431 	    TAILQ_EMPTY(&asoc->send_queue) &&
7432 	    TAILQ_EMPTY(&asoc->out_wheel)) {
7433 		*reason_code = 9;
7434 		return (0);
7435 	}
7436 	if (asoc->peers_rwnd == 0) {
7437 		/* No room in peers rwnd */
7438 		*reason_code = 1;
7439 		if (asoc->total_flight > 0) {
7440 			/* we are allowed one chunk in flight */
7441 			no_data_chunks = 1;
7442 		}
7443 	}
7444 	max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7445 	if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
7446 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7447 			/*
7448 			 * This for loop we are in takes in each net, if
7449 			 * its's got space in cwnd and has data sent to it
7450 			 * (when CMT is off) then it calls
7451 			 * sctp_fill_outqueue for the net. This gets data on
7452 			 * the send queue for that network.
7453 			 *
7454 			 * In sctp_fill_outqueue TSN's are assigned and data is
7455 			 * copied out of the stream buffers. Note mostly
7456 			 * copy by reference (we hope).
7457 			 */
7458 			net->window_probe = 0;
7459 			if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
7460 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7461 					sctp_log_cwnd(stcb, net, 1,
7462 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7463 				}
7464 				continue;
7465 			}
7466 			if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
7467 				/* nothing can be in queue for this guy */
7468 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7469 					sctp_log_cwnd(stcb, net, 2,
7470 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7471 				}
7472 				continue;
7473 			}
7474 			if (net->flight_size >= net->cwnd) {
7475 				/* skip this network, no room - can't fill */
7476 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7477 					sctp_log_cwnd(stcb, net, 3,
7478 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7479 				}
7480 				continue;
7481 			}
7482 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7483 				sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7484 			}
7485 			sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
7486 			if (quit_now) {
7487 				/* memory alloc failure */
7488 				no_data_chunks = 1;
7489 				break;
7490 			}
7491 		}
7492 	}
7493 	/* now service each destination and send out what we can for it */
7494 	/* Nothing to send? */
7495 	if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7496 	    (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7497 	    (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
7498 		*reason_code = 8;
7499 		return (0);
7500 	}
7501 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7502 		/* get the last start point */
7503 		start_at = asoc->last_net_cmt_send_started;
7504 		if (start_at == NULL) {
7505 			/* null so to beginning */
7506 			start_at = TAILQ_FIRST(&asoc->nets);
7507 		} else {
7508 			start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7509 			if (start_at == NULL) {
7510 				start_at = TAILQ_FIRST(&asoc->nets);
7511 			}
7512 		}
7513 		asoc->last_net_cmt_send_started = start_at;
7514 	} else {
7515 		start_at = TAILQ_FIRST(&asoc->nets);
7516 	}
7517 	old_start_at = NULL;
7518 again_one_more_time:
7519 	for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7520 		/* how much can we send? */
7521 		/* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7522 		if (old_start_at && (old_start_at == net)) {
7523 			/* through list ocmpletely. */
7524 			break;
7525 		}
7526 		tsns_sent = 0xa;
7527 		if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
7528 			/*
7529 			 * Ref-count of 1 so we cannot have data or control
7530 			 * queued to this address. Skip it (non-CMT).
7531 			 */
7532 			continue;
7533 		}
7534 		if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7535 		    (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7536 		    (net->flight_size >= net->cwnd)) {
7537 			/*
7538 			 * Nothing on control or asconf and flight is full,
7539 			 * we can skip even in the CMT case.
7540 			 */
7541 			continue;
7542 		}
7543 		ctl_cnt = bundle_at = 0;
7544 		endoutchain = outchain = NULL;
7545 		no_fragmentflg = 1;
7546 		one_chunk = 0;
7547 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7548 			skip_data_for_this_net = 1;
7549 		} else {
7550 			skip_data_for_this_net = 0;
7551 		}
7552 		if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7553 			/*
7554 			 * if we have a route and an ifp check to see if we
7555 			 * have room to send to this guy
7556 			 */
7557 			struct ifnet *ifp;
7558 
7559 			ifp = net->ro.ro_rt->rt_ifp;
7560 			if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7561 				SCTP_STAT_INCR(sctps_ifnomemqueued);
7562 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7563 					sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7564 				}
7565 				continue;
7566 			}
7567 		}
7568 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7569 		case AF_INET:
7570 			mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7571 			break;
7572 #ifdef INET6
7573 		case AF_INET6:
7574 			mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7575 			break;
7576 #endif
7577 		default:
7578 			/* TSNH */
7579 			mtu = net->mtu;
7580 			break;
7581 		}
7582 		mx_mtu = mtu;
7583 		to_out = 0;
7584 		if (mtu > asoc->peers_rwnd) {
7585 			if (asoc->total_flight > 0) {
7586 				/* We have a packet in flight somewhere */
7587 				r_mtu = asoc->peers_rwnd;
7588 			} else {
7589 				/* We are always allowed to send one MTU out */
7590 				one_chunk = 1;
7591 				r_mtu = mtu;
7592 			}
7593 		} else {
7594 			r_mtu = mtu;
7595 		}
7596 		/************************/
7597 		/* ASCONF transmission */
7598 		/************************/
7599 		/* Now first lets go through the asconf queue */
7600 		for (chk = TAILQ_FIRST(&asoc->asconf_send_queue);
7601 		    chk; chk = nchk) {
7602 			nchk = TAILQ_NEXT(chk, sctp_next);
7603 			if (chk->rec.chunk_id.id != SCTP_ASCONF) {
7604 				continue;
7605 			}
7606 			if (chk->whoTo != net) {
7607 				/*
7608 				 * No, not sent to the network we are
7609 				 * looking at
7610 				 */
7611 				break;
7612 			}
7613 			if (chk->data == NULL) {
7614 				break;
7615 			}
7616 			if (chk->sent != SCTP_DATAGRAM_UNSENT &&
7617 			    chk->sent != SCTP_DATAGRAM_RESEND) {
7618 				break;
7619 			}
7620 			/*
7621 			 * if no AUTH is yet included and this chunk
7622 			 * requires it, make sure to account for it.  We
7623 			 * don't apply the size until the AUTH chunk is
7624 			 * actually added below in case there is no room for
7625 			 * this chunk. NOTE: we overload the use of "omtu"
7626 			 * here
7627 			 */
7628 			if ((auth == NULL) &&
7629 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7630 			    stcb->asoc.peer_auth_chunks)) {
7631 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7632 			} else
7633 				omtu = 0;
7634 			/* Here we do NOT factor the r_mtu */
7635 			if ((chk->send_size < (int)(mtu - omtu)) ||
7636 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7637 				/*
7638 				 * We probably should glom the mbuf chain
7639 				 * from the chk->data for control but the
7640 				 * problem is it becomes yet one more level
7641 				 * of tracking to do if for some reason
7642 				 * output fails. Then I have got to
7643 				 * reconstruct the merged control chain.. el
7644 				 * yucko.. for now we take the easy way and
7645 				 * do the copy
7646 				 */
7647 				/*
7648 				 * Add an AUTH chunk, if chunk requires it
7649 				 * save the offset into the chain for AUTH
7650 				 */
7651 				if ((auth == NULL) &&
7652 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7653 				    stcb->asoc.peer_auth_chunks))) {
7654 					outchain = sctp_add_auth_chunk(outchain,
7655 					    &endoutchain,
7656 					    &auth,
7657 					    &auth_offset,
7658 					    stcb,
7659 					    chk->rec.chunk_id.id);
7660 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7661 				}
7662 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7663 				    (int)chk->rec.chunk_id.can_take_data,
7664 				    chk->send_size, chk->copy_by_ref);
7665 				if (outchain == NULL) {
7666 					*reason_code = 8;
7667 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7668 					return (ENOMEM);
7669 				}
7670 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7671 				/* update our MTU size */
7672 				if (mtu > (chk->send_size + omtu))
7673 					mtu -= (chk->send_size + omtu);
7674 				else
7675 					mtu = 0;
7676 				to_out += (chk->send_size + omtu);
7677 				/* Do clear IP_DF ? */
7678 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7679 					no_fragmentflg = 0;
7680 				}
7681 				if (chk->rec.chunk_id.can_take_data)
7682 					chk->data = NULL;
7683 				/*
7684 				 * set hb flag since we can use these for
7685 				 * RTO
7686 				 */
7687 				hbflag = 1;
7688 				asconf = 1;
7689 				/*
7690 				 * should sysctl this: don't bundle data
7691 				 * with ASCONF since it requires AUTH
7692 				 */
7693 				no_data_chunks = 1;
7694 				chk->sent = SCTP_DATAGRAM_SENT;
7695 				chk->snd_count++;
7696 				if (mtu == 0) {
7697 					/*
7698 					 * Ok we are out of room but we can
7699 					 * output without effecting the
7700 					 * flight size since this little guy
7701 					 * is a control only packet.
7702 					 */
7703 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7704 					/*
7705 					 * do NOT clear the asconf flag as
7706 					 * it is used to do appropriate
7707 					 * source address selection.
7708 					 */
7709 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7710 					    (struct sockaddr *)&net->ro._l_addr,
7711 					    outchain, auth_offset, auth,
7712 					    stcb->asoc.authinfo.active_keyid,
7713 					    no_fragmentflg, 0, NULL, asconf,
7714 					    inp->sctp_lport, stcb->rport,
7715 					    htonl(stcb->asoc.peer_vtag),
7716 					    net->port, so_locked, NULL))) {
7717 						if (error == ENOBUFS) {
7718 							asoc->ifp_had_enobuf = 1;
7719 							SCTP_STAT_INCR(sctps_lowlevelerr);
7720 						}
7721 						if (from_where == 0) {
7722 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
7723 						}
7724 						if (*now_filled == 0) {
7725 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7726 							*now_filled = 1;
7727 							*now = net->last_sent_time;
7728 						} else {
7729 							net->last_sent_time = *now;
7730 						}
7731 						hbflag = 0;
7732 						/* error, could not output */
7733 						if (error == EHOSTUNREACH) {
7734 							/*
7735 							 * Destination went
7736 							 * unreachable
7737 							 * during this send
7738 							 */
7739 							sctp_move_to_an_alt(stcb, asoc, net);
7740 						}
7741 						*reason_code = 7;
7742 						continue;
7743 					} else
7744 						asoc->ifp_had_enobuf = 0;
7745 					if (*now_filled == 0) {
7746 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7747 						*now_filled = 1;
7748 						*now = net->last_sent_time;
7749 					} else {
7750 						net->last_sent_time = *now;
7751 					}
7752 					hbflag = 0;
7753 					/*
7754 					 * increase the number we sent, if a
7755 					 * cookie is sent we don't tell them
7756 					 * any was sent out.
7757 					 */
7758 					outchain = endoutchain = NULL;
7759 					auth = NULL;
7760 					auth_offset = 0;
7761 					if (!no_out_cnt)
7762 						*num_out += ctl_cnt;
7763 					/* recalc a clean slate and setup */
7764 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7765 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7766 					} else {
7767 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7768 					}
7769 					to_out = 0;
7770 					no_fragmentflg = 1;
7771 				}
7772 			}
7773 		}
7774 		/************************/
7775 		/* Control transmission */
7776 		/************************/
7777 		/* Now first lets go through the control queue */
7778 		for (chk = TAILQ_FIRST(&asoc->control_send_queue);
7779 		    chk; chk = nchk) {
7780 			nchk = TAILQ_NEXT(chk, sctp_next);
7781 			if (chk->whoTo != net) {
7782 				/*
7783 				 * No, not sent to the network we are
7784 				 * looking at
7785 				 */
7786 				continue;
7787 			}
7788 			if (chk->data == NULL) {
7789 				continue;
7790 			}
7791 			if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7792 				/*
7793 				 * It must be unsent. Cookies and ASCONF's
7794 				 * hang around but there timers will force
7795 				 * when marked for resend.
7796 				 */
7797 				continue;
7798 			}
7799 			/*
7800 			 * if no AUTH is yet included and this chunk
7801 			 * requires it, make sure to account for it.  We
7802 			 * don't apply the size until the AUTH chunk is
7803 			 * actually added below in case there is no room for
7804 			 * this chunk. NOTE: we overload the use of "omtu"
7805 			 * here
7806 			 */
7807 			if ((auth == NULL) &&
7808 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7809 			    stcb->asoc.peer_auth_chunks)) {
7810 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7811 			} else
7812 				omtu = 0;
7813 			/* Here we do NOT factor the r_mtu */
7814 			if ((chk->send_size <= (int)(mtu - omtu)) ||
7815 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7816 				/*
7817 				 * We probably should glom the mbuf chain
7818 				 * from the chk->data for control but the
7819 				 * problem is it becomes yet one more level
7820 				 * of tracking to do if for some reason
7821 				 * output fails. Then I have got to
7822 				 * reconstruct the merged control chain.. el
7823 				 * yucko.. for now we take the easy way and
7824 				 * do the copy
7825 				 */
7826 				/*
7827 				 * Add an AUTH chunk, if chunk requires it
7828 				 * save the offset into the chain for AUTH
7829 				 */
7830 				if ((auth == NULL) &&
7831 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7832 				    stcb->asoc.peer_auth_chunks))) {
7833 					outchain = sctp_add_auth_chunk(outchain,
7834 					    &endoutchain,
7835 					    &auth,
7836 					    &auth_offset,
7837 					    stcb,
7838 					    chk->rec.chunk_id.id);
7839 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7840 				}
7841 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7842 				    (int)chk->rec.chunk_id.can_take_data,
7843 				    chk->send_size, chk->copy_by_ref);
7844 				if (outchain == NULL) {
7845 					*reason_code = 8;
7846 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7847 					return (ENOMEM);
7848 				}
7849 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7850 				/* update our MTU size */
7851 				if (mtu > (chk->send_size + omtu))
7852 					mtu -= (chk->send_size + omtu);
7853 				else
7854 					mtu = 0;
7855 				to_out += (chk->send_size + omtu);
7856 				/* Do clear IP_DF ? */
7857 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7858 					no_fragmentflg = 0;
7859 				}
7860 				if (chk->rec.chunk_id.can_take_data)
7861 					chk->data = NULL;
7862 				/* Mark things to be removed, if needed */
7863 				if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7864 				    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
7865 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7866 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7867 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7868 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7869 				    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7870 				    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7871 				    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7872 				    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7873 				    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7874 
7875 					if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7876 						hbflag = 1;
7877 						/*
7878 						 * JRS 5/14/07 - Set the
7879 						 * flag to say a heartbeat
7880 						 * is being sent.
7881 						 */
7882 						pf_hbflag = 1;
7883 					}
7884 					/* remove these chunks at the end */
7885 					if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7886 					    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7887 						/* turn off the timer */
7888 						if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7889 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7890 							    inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7891 						}
7892 					}
7893 					ctl_cnt++;
7894 				} else {
7895 					/*
7896 					 * Other chunks, since they have
7897 					 * timers running (i.e. COOKIE) we
7898 					 * just "trust" that it gets sent or
7899 					 * retransmitted.
7900 					 */
7901 					ctl_cnt++;
7902 					if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7903 						cookie = 1;
7904 						no_out_cnt = 1;
7905 					}
7906 					chk->sent = SCTP_DATAGRAM_SENT;
7907 					chk->snd_count++;
7908 				}
7909 				if (mtu == 0) {
7910 					/*
7911 					 * Ok we are out of room but we can
7912 					 * output without effecting the
7913 					 * flight size since this little guy
7914 					 * is a control only packet.
7915 					 */
7916 					if (asconf) {
7917 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7918 						/*
7919 						 * do NOT clear the asconf
7920 						 * flag as it is used to do
7921 						 * appropriate source
7922 						 * address selection.
7923 						 */
7924 					}
7925 					if (cookie) {
7926 						sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7927 						cookie = 0;
7928 					}
7929 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7930 					    (struct sockaddr *)&net->ro._l_addr,
7931 					    outchain,
7932 					    auth_offset, auth,
7933 					    stcb->asoc.authinfo.active_keyid,
7934 					    no_fragmentflg, 0, NULL, asconf,
7935 					    inp->sctp_lport, stcb->rport,
7936 					    htonl(stcb->asoc.peer_vtag),
7937 					    net->port, so_locked, NULL))) {
7938 						if (error == ENOBUFS) {
7939 							asoc->ifp_had_enobuf = 1;
7940 							SCTP_STAT_INCR(sctps_lowlevelerr);
7941 						}
7942 						if (from_where == 0) {
7943 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
7944 						}
7945 						/* error, could not output */
7946 						if (hbflag) {
7947 							if (*now_filled == 0) {
7948 								(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7949 								*now_filled = 1;
7950 								*now = net->last_sent_time;
7951 							} else {
7952 								net->last_sent_time = *now;
7953 							}
7954 							hbflag = 0;
7955 						}
7956 						if (error == EHOSTUNREACH) {
7957 							/*
7958 							 * Destination went
7959 							 * unreachable
7960 							 * during this send
7961 							 */
7962 							sctp_move_to_an_alt(stcb, asoc, net);
7963 						}
7964 						*reason_code = 7;
7965 						continue;
7966 					} else
7967 						asoc->ifp_had_enobuf = 0;
7968 					/* Only HB or ASCONF advances time */
7969 					if (hbflag) {
7970 						if (*now_filled == 0) {
7971 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7972 							*now_filled = 1;
7973 							*now = net->last_sent_time;
7974 						} else {
7975 							net->last_sent_time = *now;
7976 						}
7977 						hbflag = 0;
7978 					}
7979 					/*
7980 					 * increase the number we sent, if a
7981 					 * cookie is sent we don't tell them
7982 					 * any was sent out.
7983 					 */
7984 					outchain = endoutchain = NULL;
7985 					auth = NULL;
7986 					auth_offset = 0;
7987 					if (!no_out_cnt)
7988 						*num_out += ctl_cnt;
7989 					/* recalc a clean slate and setup */
7990 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7991 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7992 					} else {
7993 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7994 					}
7995 					to_out = 0;
7996 					no_fragmentflg = 1;
7997 				}
7998 			}
7999 		}
8000 		/* JRI: if dest is in PF state, do not send data to it */
8001 		if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
8002 		    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
8003 		    (net->dest_state & SCTP_ADDR_PF)) {
8004 			goto no_data_fill;
8005 		}
8006 		if (net->flight_size >= net->cwnd) {
8007 			goto no_data_fill;
8008 		}
8009 		if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off)) &&
8010 		    (net->flight_size > max_rwnd_per_dest)) {
8011 			goto no_data_fill;
8012 		}
8013 		/*********************/
8014 		/* Data transmission */
8015 		/*********************/
8016 		/*
8017 		 * if AUTH for DATA is required and no AUTH has been added
8018 		 * yet, account for this in the mtu now... if no data can be
8019 		 * bundled, this adjustment won't matter anyways since the
8020 		 * packet will be going out...
8021 		 */
8022 		data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8023 		    stcb->asoc.peer_auth_chunks);
8024 		if (data_auth_reqd && (auth == NULL)) {
8025 			mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8026 		}
8027 		/* now lets add any data within the MTU constraints */
8028 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8029 		case AF_INET:
8030 			if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8031 				omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8032 			else
8033 				omtu = 0;
8034 			break;
8035 #ifdef INET6
8036 		case AF_INET6:
8037 			if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8038 				omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8039 			else
8040 				omtu = 0;
8041 			break;
8042 #endif
8043 		default:
8044 			/* TSNH */
8045 			omtu = 0;
8046 			break;
8047 		}
8048 		if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) &&
8049 		    (skip_data_for_this_net == 0)) ||
8050 		    (cookie)) {
8051 			for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
8052 				if (no_data_chunks) {
8053 					/* let only control go out */
8054 					*reason_code = 1;
8055 					break;
8056 				}
8057 				if (net->flight_size >= net->cwnd) {
8058 					/* skip this net, no room for data */
8059 					*reason_code = 2;
8060 					break;
8061 				}
8062 				nchk = TAILQ_NEXT(chk, sctp_next);
8063 				if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
8064 					if (chk->whoTo != net) {
8065 						/*
8066 						 * For CMT, steal the data
8067 						 * to this network if its
8068 						 * not set here.
8069 						 */
8070 						sctp_free_remote_addr(chk->whoTo);
8071 						chk->whoTo = net;
8072 						atomic_add_int(&chk->whoTo->ref_count, 1);
8073 					}
8074 				} else if (chk->whoTo != net) {
8075 					/* No, not sent to this net */
8076 					continue;
8077 				}
8078 				if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8079 					/*-
8080 					 * strange, we have a chunk that is
8081 					 * to big for its destination and
8082 					 * yet no fragment ok flag.
8083 					 * Something went wrong when the
8084 					 * PMTU changed...we did not mark
8085 					 * this chunk for some reason?? I
8086 					 * will fix it here by letting IP
8087 					 * fragment it for now and printing
8088 					 * a warning. This really should not
8089 					 * happen ...
8090 					 */
8091 					SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8092 					    chk->send_size, mtu);
8093 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8094 				}
8095 				if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8096 				    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
8097 					struct sctp_data_chunk *dchkh;
8098 
8099 					dchkh = mtod(chk->data, struct sctp_data_chunk *);
8100 					dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8101 				}
8102 				if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8103 				    ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8104 					/* ok we will add this one */
8105 
8106 					/*
8107 					 * Add an AUTH chunk, if chunk
8108 					 * requires it, save the offset into
8109 					 * the chain for AUTH
8110 					 */
8111 					if (data_auth_reqd) {
8112 						if (auth == NULL) {
8113 							outchain = sctp_add_auth_chunk(outchain,
8114 							    &endoutchain,
8115 							    &auth,
8116 							    &auth_offset,
8117 							    stcb,
8118 							    SCTP_DATA);
8119 							auth_keyid = chk->auth_keyid;
8120 							override_ok = 0;
8121 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8122 						} else if (override_ok) {
8123 							/*
8124 							 * use this data's
8125 							 * keyid
8126 							 */
8127 							auth_keyid = chk->auth_keyid;
8128 							override_ok = 0;
8129 						} else if (auth_keyid != chk->auth_keyid) {
8130 							/*
8131 							 * different keyid,
8132 							 * so done bundling
8133 							 */
8134 							break;
8135 						}
8136 					}
8137 					outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8138 					    chk->send_size, chk->copy_by_ref);
8139 					if (outchain == NULL) {
8140 						SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8141 						if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8142 							sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8143 						}
8144 						*reason_code = 3;
8145 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8146 						return (ENOMEM);
8147 					}
8148 					/* upate our MTU size */
8149 					/* Do clear IP_DF ? */
8150 					if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8151 						no_fragmentflg = 0;
8152 					}
8153 					/* unsigned subtraction of mtu */
8154 					if (mtu > chk->send_size)
8155 						mtu -= chk->send_size;
8156 					else
8157 						mtu = 0;
8158 					/* unsigned subtraction of r_mtu */
8159 					if (r_mtu > chk->send_size)
8160 						r_mtu -= chk->send_size;
8161 					else
8162 						r_mtu = 0;
8163 
8164 					to_out += chk->send_size;
8165 					if ((to_out > mx_mtu) && no_fragmentflg) {
8166 #ifdef INVARIANTS
8167 						panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8168 #else
8169 						SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8170 						    mx_mtu, to_out);
8171 #endif
8172 					}
8173 					chk->window_probe = 0;
8174 					data_list[bundle_at++] = chk;
8175 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8176 						mtu = 0;
8177 						break;
8178 					}
8179 					if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8180 						if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8181 							SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8182 						} else {
8183 							SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8184 						}
8185 						if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8186 						    ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8187 							/*
8188 							 * Count number of
8189 							 * user msg's that
8190 							 * were fragmented
8191 							 * we do this by
8192 							 * counting when we
8193 							 * see a LAST
8194 							 * fragment only.
8195 							 */
8196 							SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8197 					}
8198 					if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8199 						if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8200 							data_list[0]->window_probe = 1;
8201 							net->window_probe = 1;
8202 						}
8203 						break;
8204 					}
8205 				} else {
8206 					/*
8207 					 * Must be sent in order of the
8208 					 * TSN's (on a network)
8209 					 */
8210 					break;
8211 				}
8212 			}	/* for (chunk gather loop for this net) */
8213 		}		/* if asoc.state OPEN */
8214 no_data_fill:
8215 		/* Is there something to send for this destination? */
8216 		if (outchain) {
8217 			/* We may need to start a control timer or two */
8218 			if (asconf) {
8219 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8220 				    stcb, net);
8221 				/*
8222 				 * do NOT clear the asconf flag as it is
8223 				 * used to do appropriate source address
8224 				 * selection.
8225 				 */
8226 			}
8227 			if (cookie) {
8228 				sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8229 				cookie = 0;
8230 			}
8231 			/* must start a send timer if data is being sent */
8232 			if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8233 				/*
8234 				 * no timer running on this destination
8235 				 * restart it.
8236 				 */
8237 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8238 			} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
8239 				    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
8240 				    pf_hbflag &&
8241 				    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
8242 			    (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8243 				/*
8244 				 * JRS 5/14/07 - If a HB has been sent to a
8245 				 * PF destination and no T3 timer is
8246 				 * currently running, start the T3 timer to
8247 				 * track the HBs that were sent.
8248 				 */
8249 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8250 			}
8251 			/* Now send it, if there is anything to send :> */
8252 			if ((error = sctp_lowlevel_chunk_output(inp,
8253 			    stcb,
8254 			    net,
8255 			    (struct sockaddr *)&net->ro._l_addr,
8256 			    outchain,
8257 			    auth_offset,
8258 			    auth,
8259 			    auth_keyid,
8260 			    no_fragmentflg,
8261 			    bundle_at,
8262 			    data_list[0],
8263 			    asconf,
8264 			    inp->sctp_lport, stcb->rport,
8265 			    htonl(stcb->asoc.peer_vtag),
8266 			    net->port, so_locked, NULL))) {
8267 				/* error, we could not output */
8268 				if (error == ENOBUFS) {
8269 					SCTP_STAT_INCR(sctps_lowlevelerr);
8270 					asoc->ifp_had_enobuf = 1;
8271 				}
8272 				if (from_where == 0) {
8273 					SCTP_STAT_INCR(sctps_lowlevelerrusr);
8274 				}
8275 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8276 				if (hbflag) {
8277 					if (*now_filled == 0) {
8278 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8279 						*now_filled = 1;
8280 						*now = net->last_sent_time;
8281 					} else {
8282 						net->last_sent_time = *now;
8283 					}
8284 					hbflag = 0;
8285 				}
8286 				if (error == EHOSTUNREACH) {
8287 					/*
8288 					 * Destination went unreachable
8289 					 * during this send
8290 					 */
8291 					sctp_move_to_an_alt(stcb, asoc, net);
8292 				}
8293 				*reason_code = 6;
8294 				/*-
8295 				 * I add this line to be paranoid. As far as
8296 				 * I can tell the continue, takes us back to
8297 				 * the top of the for, but just to make sure
8298 				 * I will reset these again here.
8299 				 */
8300 				ctl_cnt = bundle_at = 0;
8301 				continue;	/* This takes us back to the
8302 						 * for() for the nets. */
8303 			} else {
8304 				asoc->ifp_had_enobuf = 0;
8305 			}
8306 			outchain = endoutchain = NULL;
8307 			auth = NULL;
8308 			auth_offset = 0;
8309 			if (bundle_at || hbflag) {
8310 				/* For data/asconf and hb set time */
8311 				if (*now_filled == 0) {
8312 					(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8313 					*now_filled = 1;
8314 					*now = net->last_sent_time;
8315 				} else {
8316 					net->last_sent_time = *now;
8317 				}
8318 			}
8319 			if (!no_out_cnt) {
8320 				*num_out += (ctl_cnt + bundle_at);
8321 			}
8322 			if (bundle_at) {
8323 				/* setup for a RTO measurement */
8324 				tsns_sent = data_list[0]->rec.data.TSN_seq;
8325 				/* fill time if not already filled */
8326 				if (*now_filled == 0) {
8327 					(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8328 					*now_filled = 1;
8329 					*now = asoc->time_last_sent;
8330 				} else {
8331 					asoc->time_last_sent = *now;
8332 				}
8333 				data_list[0]->do_rtt = 1;
8334 				SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8335 				sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8336 				if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8337 					if (net->flight_size < net->cwnd) {
8338 						/* start or restart it */
8339 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8340 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8341 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
8342 						}
8343 						SCTP_STAT_INCR(sctps_earlyfrstrout);
8344 						sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
8345 					} else {
8346 						/* stop it if its running */
8347 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8348 							SCTP_STAT_INCR(sctps_earlyfrstpout);
8349 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8350 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
8351 						}
8352 					}
8353 				}
8354 			}
8355 			if (one_chunk) {
8356 				break;
8357 			}
8358 		}
8359 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8360 			sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8361 		}
8362 	}
8363 	if (old_start_at == NULL) {
8364 		old_start_at = start_at;
8365 		start_at = TAILQ_FIRST(&asoc->nets);
8366 		if (old_start_at)
8367 			goto again_one_more_time;
8368 	}
8369 	/*
8370 	 * At the end there should be no NON timed chunks hanging on this
8371 	 * queue.
8372 	 */
8373 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8374 		sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8375 	}
8376 	if ((*num_out == 0) && (*reason_code == 0)) {
8377 		*reason_code = 4;
8378 	} else {
8379 		*reason_code = 5;
8380 	}
8381 	sctp_clean_up_ctl(stcb, asoc);
8382 	return (0);
8383 }
8384 
8385 void
8386 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8387 {
8388 	/*-
8389 	 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8390 	 * the control chunk queue.
8391 	 */
8392 	struct sctp_chunkhdr *hdr;
8393 	struct sctp_tmit_chunk *chk;
8394 	struct mbuf *mat;
8395 
8396 	SCTP_TCB_LOCK_ASSERT(stcb);
8397 	sctp_alloc_a_chunk(stcb, chk);
8398 	if (chk == NULL) {
8399 		/* no memory */
8400 		sctp_m_freem(op_err);
8401 		return;
8402 	}
8403 	chk->copy_by_ref = 0;
8404 	SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8405 	if (op_err == NULL) {
8406 		sctp_free_a_chunk(stcb, chk);
8407 		return;
8408 	}
8409 	chk->send_size = 0;
8410 	mat = op_err;
8411 	while (mat != NULL) {
8412 		chk->send_size += SCTP_BUF_LEN(mat);
8413 		mat = SCTP_BUF_NEXT(mat);
8414 	}
8415 	chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8416 	chk->rec.chunk_id.can_take_data = 1;
8417 	chk->sent = SCTP_DATAGRAM_UNSENT;
8418 	chk->snd_count = 0;
8419 	chk->flags = 0;
8420 	chk->asoc = &stcb->asoc;
8421 	chk->data = op_err;
8422 	chk->whoTo = chk->asoc->primary_destination;
8423 	atomic_add_int(&chk->whoTo->ref_count, 1);
8424 	hdr = mtod(op_err, struct sctp_chunkhdr *);
8425 	hdr->chunk_type = SCTP_OPERATION_ERROR;
8426 	hdr->chunk_flags = 0;
8427 	hdr->chunk_length = htons(chk->send_size);
8428 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8429 	    chk,
8430 	    sctp_next);
8431 	chk->asoc->ctrl_queue_cnt++;
8432 }
8433 
8434 int
8435 sctp_send_cookie_echo(struct mbuf *m,
8436     int offset,
8437     struct sctp_tcb *stcb,
8438     struct sctp_nets *net)
8439 {
8440 	/*-
8441 	 * pull out the cookie and put it at the front of the control chunk
8442 	 * queue.
8443 	 */
8444 	int at;
8445 	struct mbuf *cookie;
8446 	struct sctp_paramhdr parm, *phdr;
8447 	struct sctp_chunkhdr *hdr;
8448 	struct sctp_tmit_chunk *chk;
8449 	uint16_t ptype, plen;
8450 
8451 	/* First find the cookie in the param area */
8452 	cookie = NULL;
8453 	at = offset + sizeof(struct sctp_init_chunk);
8454 
8455 	SCTP_TCB_LOCK_ASSERT(stcb);
8456 	do {
8457 		phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8458 		if (phdr == NULL) {
8459 			return (-3);
8460 		}
8461 		ptype = ntohs(phdr->param_type);
8462 		plen = ntohs(phdr->param_length);
8463 		if (ptype == SCTP_STATE_COOKIE) {
8464 			int pad;
8465 
8466 			/* found the cookie */
8467 			if ((pad = (plen % 4))) {
8468 				plen += 4 - pad;
8469 			}
8470 			cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8471 			if (cookie == NULL) {
8472 				/* No memory */
8473 				return (-2);
8474 			}
8475 #ifdef SCTP_MBUF_LOGGING
8476 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8477 				struct mbuf *mat;
8478 
8479 				mat = cookie;
8480 				while (mat) {
8481 					if (SCTP_BUF_IS_EXTENDED(mat)) {
8482 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8483 					}
8484 					mat = SCTP_BUF_NEXT(mat);
8485 				}
8486 			}
8487 #endif
8488 			break;
8489 		}
8490 		at += SCTP_SIZE32(plen);
8491 	} while (phdr);
8492 	if (cookie == NULL) {
8493 		/* Did not find the cookie */
8494 		return (-3);
8495 	}
8496 	/* ok, we got the cookie lets change it into a cookie echo chunk */
8497 
8498 	/* first the change from param to cookie */
8499 	hdr = mtod(cookie, struct sctp_chunkhdr *);
8500 	hdr->chunk_type = SCTP_COOKIE_ECHO;
8501 	hdr->chunk_flags = 0;
8502 	/* get the chunk stuff now and place it in the FRONT of the queue */
8503 	sctp_alloc_a_chunk(stcb, chk);
8504 	if (chk == NULL) {
8505 		/* no memory */
8506 		sctp_m_freem(cookie);
8507 		return (-5);
8508 	}
8509 	chk->copy_by_ref = 0;
8510 	chk->send_size = plen;
8511 	chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8512 	chk->rec.chunk_id.can_take_data = 0;
8513 	chk->sent = SCTP_DATAGRAM_UNSENT;
8514 	chk->snd_count = 0;
8515 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8516 	chk->asoc = &stcb->asoc;
8517 	chk->data = cookie;
8518 	chk->whoTo = chk->asoc->primary_destination;
8519 	atomic_add_int(&chk->whoTo->ref_count, 1);
8520 	TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8521 	chk->asoc->ctrl_queue_cnt++;
8522 	return (0);
8523 }
8524 
8525 void
8526 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8527     struct mbuf *m,
8528     int offset,
8529     int chk_length,
8530     struct sctp_nets *net)
8531 {
8532 	/*
8533 	 * take a HB request and make it into a HB ack and send it.
8534 	 */
8535 	struct mbuf *outchain;
8536 	struct sctp_chunkhdr *chdr;
8537 	struct sctp_tmit_chunk *chk;
8538 
8539 
8540 	if (net == NULL)
8541 		/* must have a net pointer */
8542 		return;
8543 
8544 	outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
8545 	if (outchain == NULL) {
8546 		/* gak out of memory */
8547 		return;
8548 	}
8549 #ifdef SCTP_MBUF_LOGGING
8550 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8551 		struct mbuf *mat;
8552 
8553 		mat = outchain;
8554 		while (mat) {
8555 			if (SCTP_BUF_IS_EXTENDED(mat)) {
8556 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8557 			}
8558 			mat = SCTP_BUF_NEXT(mat);
8559 		}
8560 	}
8561 #endif
8562 	chdr = mtod(outchain, struct sctp_chunkhdr *);
8563 	chdr->chunk_type = SCTP_HEARTBEAT_ACK;
8564 	chdr->chunk_flags = 0;
8565 	if (chk_length % 4) {
8566 		/* need pad */
8567 		uint32_t cpthis = 0;
8568 		int padlen;
8569 
8570 		padlen = 4 - (chk_length % 4);
8571 		m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
8572 	}
8573 	sctp_alloc_a_chunk(stcb, chk);
8574 	if (chk == NULL) {
8575 		/* no memory */
8576 		sctp_m_freem(outchain);
8577 		return;
8578 	}
8579 	chk->copy_by_ref = 0;
8580 	chk->send_size = chk_length;
8581 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
8582 	chk->rec.chunk_id.can_take_data = 1;
8583 	chk->sent = SCTP_DATAGRAM_UNSENT;
8584 	chk->snd_count = 0;
8585 	chk->flags = 0;
8586 	chk->asoc = &stcb->asoc;
8587 	chk->data = outchain;
8588 	chk->whoTo = net;
8589 	atomic_add_int(&chk->whoTo->ref_count, 1);
8590 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8591 	chk->asoc->ctrl_queue_cnt++;
8592 }
8593 
8594 void
8595 sctp_send_cookie_ack(struct sctp_tcb *stcb)
8596 {
8597 	/* formulate and queue a cookie-ack back to sender */
8598 	struct mbuf *cookie_ack;
8599 	struct sctp_chunkhdr *hdr;
8600 	struct sctp_tmit_chunk *chk;
8601 
8602 	cookie_ack = NULL;
8603 	SCTP_TCB_LOCK_ASSERT(stcb);
8604 
8605 	cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
8606 	if (cookie_ack == NULL) {
8607 		/* no mbuf's */
8608 		return;
8609 	}
8610 	SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
8611 	sctp_alloc_a_chunk(stcb, chk);
8612 	if (chk == NULL) {
8613 		/* no memory */
8614 		sctp_m_freem(cookie_ack);
8615 		return;
8616 	}
8617 	chk->copy_by_ref = 0;
8618 	chk->send_size = sizeof(struct sctp_chunkhdr);
8619 	chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
8620 	chk->rec.chunk_id.can_take_data = 1;
8621 	chk->sent = SCTP_DATAGRAM_UNSENT;
8622 	chk->snd_count = 0;
8623 	chk->flags = 0;
8624 	chk->asoc = &stcb->asoc;
8625 	chk->data = cookie_ack;
8626 	if (chk->asoc->last_control_chunk_from != NULL) {
8627 		chk->whoTo = chk->asoc->last_control_chunk_from;
8628 	} else {
8629 		chk->whoTo = chk->asoc->primary_destination;
8630 	}
8631 	atomic_add_int(&chk->whoTo->ref_count, 1);
8632 	hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
8633 	hdr->chunk_type = SCTP_COOKIE_ACK;
8634 	hdr->chunk_flags = 0;
8635 	hdr->chunk_length = htons(chk->send_size);
8636 	SCTP_BUF_LEN(cookie_ack) = chk->send_size;
8637 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8638 	chk->asoc->ctrl_queue_cnt++;
8639 	return;
8640 }
8641 
8642 
8643 void
8644 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
8645 {
8646 	/* formulate and queue a SHUTDOWN-ACK back to the sender */
8647 	struct mbuf *m_shutdown_ack;
8648 	struct sctp_shutdown_ack_chunk *ack_cp;
8649 	struct sctp_tmit_chunk *chk;
8650 
8651 	m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8652 	if (m_shutdown_ack == NULL) {
8653 		/* no mbuf's */
8654 		return;
8655 	}
8656 	SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
8657 	sctp_alloc_a_chunk(stcb, chk);
8658 	if (chk == NULL) {
8659 		/* no memory */
8660 		sctp_m_freem(m_shutdown_ack);
8661 		return;
8662 	}
8663 	chk->copy_by_ref = 0;
8664 	chk->send_size = sizeof(struct sctp_chunkhdr);
8665 	chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
8666 	chk->rec.chunk_id.can_take_data = 1;
8667 	chk->sent = SCTP_DATAGRAM_UNSENT;
8668 	chk->snd_count = 0;
8669 	chk->flags = 0;
8670 	chk->asoc = &stcb->asoc;
8671 	chk->data = m_shutdown_ack;
8672 	chk->whoTo = net;
8673 	atomic_add_int(&net->ref_count, 1);
8674 
8675 	ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
8676 	ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
8677 	ack_cp->ch.chunk_flags = 0;
8678 	ack_cp->ch.chunk_length = htons(chk->send_size);
8679 	SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
8680 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8681 	chk->asoc->ctrl_queue_cnt++;
8682 	return;
8683 }
8684 
8685 void
8686 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
8687 {
8688 	/* formulate and queue a SHUTDOWN to the sender */
8689 	struct mbuf *m_shutdown;
8690 	struct sctp_shutdown_chunk *shutdown_cp;
8691 	struct sctp_tmit_chunk *chk;
8692 
8693 	m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8694 	if (m_shutdown == NULL) {
8695 		/* no mbuf's */
8696 		return;
8697 	}
8698 	SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
8699 	sctp_alloc_a_chunk(stcb, chk);
8700 	if (chk == NULL) {
8701 		/* no memory */
8702 		sctp_m_freem(m_shutdown);
8703 		return;
8704 	}
8705 	chk->copy_by_ref = 0;
8706 	chk->send_size = sizeof(struct sctp_shutdown_chunk);
8707 	chk->rec.chunk_id.id = SCTP_SHUTDOWN;
8708 	chk->rec.chunk_id.can_take_data = 1;
8709 	chk->sent = SCTP_DATAGRAM_UNSENT;
8710 	chk->snd_count = 0;
8711 	chk->flags = 0;
8712 	chk->asoc = &stcb->asoc;
8713 	chk->data = m_shutdown;
8714 	chk->whoTo = net;
8715 	atomic_add_int(&net->ref_count, 1);
8716 
8717 	shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
8718 	shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
8719 	shutdown_cp->ch.chunk_flags = 0;
8720 	shutdown_cp->ch.chunk_length = htons(chk->send_size);
8721 	shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
8722 	SCTP_BUF_LEN(m_shutdown) = chk->send_size;
8723 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8724 	chk->asoc->ctrl_queue_cnt++;
8725 	return;
8726 }
8727 
8728 void
8729 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
8730 {
8731 	/*
8732 	 * formulate and queue an ASCONF to the peer. ASCONF parameters
8733 	 * should be queued on the assoc queue.
8734 	 */
8735 	struct sctp_tmit_chunk *chk;
8736 	struct mbuf *m_asconf;
8737 	int len;
8738 
8739 	SCTP_TCB_LOCK_ASSERT(stcb);
8740 
8741 	if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
8742 	    (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
8743 		/* can't send a new one if there is one in flight already */
8744 		return;
8745 	}
8746 	/* compose an ASCONF chunk, maximum length is PMTU */
8747 	m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
8748 	if (m_asconf == NULL) {
8749 		return;
8750 	}
8751 	sctp_alloc_a_chunk(stcb, chk);
8752 	if (chk == NULL) {
8753 		/* no memory */
8754 		sctp_m_freem(m_asconf);
8755 		return;
8756 	}
8757 	chk->copy_by_ref = 0;
8758 	chk->data = m_asconf;
8759 	chk->send_size = len;
8760 	chk->rec.chunk_id.id = SCTP_ASCONF;
8761 	chk->rec.chunk_id.can_take_data = 0;
8762 	chk->sent = SCTP_DATAGRAM_UNSENT;
8763 	chk->snd_count = 0;
8764 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8765 	chk->asoc = &stcb->asoc;
8766 	chk->whoTo = net;
8767 	atomic_add_int(&chk->whoTo->ref_count, 1);
8768 	TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
8769 	chk->asoc->ctrl_queue_cnt++;
8770 	return;
8771 }
8772 
8773 void
8774 sctp_send_asconf_ack(struct sctp_tcb *stcb)
8775 {
8776 	/*
8777 	 * formulate and queue a asconf-ack back to sender. the asconf-ack
8778 	 * must be stored in the tcb.
8779 	 */
8780 	struct sctp_tmit_chunk *chk;
8781 	struct sctp_asconf_ack *ack, *latest_ack;
8782 	struct mbuf *m_ack, *m;
8783 	struct sctp_nets *net = NULL;
8784 
8785 	SCTP_TCB_LOCK_ASSERT(stcb);
8786 	/* Get the latest ASCONF-ACK */
8787 	latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
8788 	if (latest_ack == NULL) {
8789 		return;
8790 	}
8791 	if (latest_ack->last_sent_to != NULL &&
8792 	    latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
8793 		/* we're doing a retransmission */
8794 		net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
8795 		if (net == NULL) {
8796 			/* no alternate */
8797 			if (stcb->asoc.last_control_chunk_from == NULL)
8798 				net = stcb->asoc.primary_destination;
8799 			else
8800 				net = stcb->asoc.last_control_chunk_from;
8801 		}
8802 	} else {
8803 		/* normal case */
8804 		if (stcb->asoc.last_control_chunk_from == NULL)
8805 			net = stcb->asoc.primary_destination;
8806 		else
8807 			net = stcb->asoc.last_control_chunk_from;
8808 	}
8809 	latest_ack->last_sent_to = net;
8810 
8811 	TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
8812 		if (ack->data == NULL) {
8813 			continue;
8814 		}
8815 		/* copy the asconf_ack */
8816 		m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
8817 		if (m_ack == NULL) {
8818 			/* couldn't copy it */
8819 			return;
8820 		}
8821 #ifdef SCTP_MBUF_LOGGING
8822 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8823 			struct mbuf *mat;
8824 
8825 			mat = m_ack;
8826 			while (mat) {
8827 				if (SCTP_BUF_IS_EXTENDED(mat)) {
8828 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8829 				}
8830 				mat = SCTP_BUF_NEXT(mat);
8831 			}
8832 		}
8833 #endif
8834 
8835 		sctp_alloc_a_chunk(stcb, chk);
8836 		if (chk == NULL) {
8837 			/* no memory */
8838 			if (m_ack)
8839 				sctp_m_freem(m_ack);
8840 			return;
8841 		}
8842 		chk->copy_by_ref = 0;
8843 
8844 		chk->whoTo = net;
8845 		chk->data = m_ack;
8846 		chk->send_size = 0;
8847 		/* Get size */
8848 		m = m_ack;
8849 		chk->send_size = ack->len;
8850 		chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8851 		chk->rec.chunk_id.can_take_data = 1;
8852 		chk->sent = SCTP_DATAGRAM_UNSENT;
8853 		chk->snd_count = 0;
8854 		chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;	/* XXX */
8855 		chk->asoc = &stcb->asoc;
8856 		atomic_add_int(&chk->whoTo->ref_count, 1);
8857 
8858 		TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8859 		chk->asoc->ctrl_queue_cnt++;
8860 	}
8861 	return;
8862 }
8863 
8864 
8865 static int
8866 sctp_chunk_retransmission(struct sctp_inpcb *inp,
8867     struct sctp_tcb *stcb,
8868     struct sctp_association *asoc,
8869     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
8870 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8871     SCTP_UNUSED
8872 #endif
8873 )
8874 {
8875 	/*-
8876 	 * send out one MTU of retransmission. If fast_retransmit is
8877 	 * happening we ignore the cwnd. Otherwise we obey the cwnd and
8878 	 * rwnd. For a Cookie or Asconf in the control chunk queue we
8879 	 * retransmit them by themselves.
8880 	 *
8881 	 * For data chunks we will pick out the lowest TSN's in the sent_queue
8882 	 * marked for resend and bundle them all together (up to a MTU of
8883 	 * destination). The address to send to should have been
8884 	 * selected/changed where the retransmission was marked (i.e. in FR
8885 	 * or t3-timeout routines).
8886 	 */
8887 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8888 	struct sctp_tmit_chunk *chk, *fwd;
8889 	struct mbuf *m, *endofchain;
8890 	struct sctp_nets *net = NULL;
8891 	uint32_t tsns_sent = 0;
8892 	int no_fragmentflg, bundle_at, cnt_thru;
8893 	unsigned int mtu;
8894 	int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
8895 	struct sctp_auth_chunk *auth = NULL;
8896 	uint32_t auth_offset = 0;
8897 	uint16_t auth_keyid;
8898 	int override_ok = 1;
8899 	int data_auth_reqd = 0;
8900 	uint32_t dmtu = 0;
8901 
8902 	SCTP_TCB_LOCK_ASSERT(stcb);
8903 	tmr_started = ctl_cnt = bundle_at = error = 0;
8904 	no_fragmentflg = 1;
8905 	fwd_tsn = 0;
8906 	*cnt_out = 0;
8907 	fwd = NULL;
8908 	endofchain = m = NULL;
8909 	auth_keyid = stcb->asoc.authinfo.active_keyid;
8910 #ifdef SCTP_AUDITING_ENABLED
8911 	sctp_audit_log(0xC3, 1);
8912 #endif
8913 	if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
8914 	    (TAILQ_EMPTY(&asoc->control_send_queue))) {
8915 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
8916 		    asoc->sent_queue_retran_cnt);
8917 		asoc->sent_queue_cnt = 0;
8918 		asoc->sent_queue_cnt_removeable = 0;
8919 		/* send back 0/0 so we enter normal transmission */
8920 		*cnt_out = 0;
8921 		return (0);
8922 	}
8923 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8924 		if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
8925 		    (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
8926 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
8927 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
8928 				continue;
8929 			}
8930 			if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8931 				if (chk != asoc->str_reset) {
8932 					/*
8933 					 * not eligible for retran if its
8934 					 * not ours
8935 					 */
8936 					continue;
8937 				}
8938 			}
8939 			ctl_cnt++;
8940 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8941 				fwd_tsn = 1;
8942 				fwd = chk;
8943 			}
8944 			/*
8945 			 * Add an AUTH chunk, if chunk requires it save the
8946 			 * offset into the chain for AUTH
8947 			 */
8948 			if ((auth == NULL) &&
8949 			    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8950 			    stcb->asoc.peer_auth_chunks))) {
8951 				m = sctp_add_auth_chunk(m, &endofchain,
8952 				    &auth, &auth_offset,
8953 				    stcb,
8954 				    chk->rec.chunk_id.id);
8955 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8956 			}
8957 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8958 			break;
8959 		}
8960 	}
8961 	one_chunk = 0;
8962 	cnt_thru = 0;
8963 	/* do we have control chunks to retransmit? */
8964 	if (m != NULL) {
8965 		/* Start a timer no matter if we suceed or fail */
8966 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8967 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
8968 		} else if (chk->rec.chunk_id.id == SCTP_ASCONF)
8969 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
8970 		chk->snd_count++;	/* update our count */
8971 		if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
8972 		    (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
8973 		    auth_offset, auth, stcb->asoc.authinfo.active_keyid,
8974 		    no_fragmentflg, 0, NULL, 0,
8975 		    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
8976 		    chk->whoTo->port, so_locked, NULL))) {
8977 			SCTP_STAT_INCR(sctps_lowlevelerr);
8978 			return (error);
8979 		}
8980 		m = endofchain = NULL;
8981 		auth = NULL;
8982 		auth_offset = 0;
8983 		/*
8984 		 * We don't want to mark the net->sent time here since this
8985 		 * we use this for HB and retrans cannot measure RTT
8986 		 */
8987 		/* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
8988 		*cnt_out += 1;
8989 		chk->sent = SCTP_DATAGRAM_SENT;
8990 		sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
8991 		if (fwd_tsn == 0) {
8992 			return (0);
8993 		} else {
8994 			/* Clean up the fwd-tsn list */
8995 			sctp_clean_up_ctl(stcb, asoc);
8996 			return (0);
8997 		}
8998 	}
8999 	/*
9000 	 * Ok, it is just data retransmission we need to do or that and a
9001 	 * fwd-tsn with it all.
9002 	 */
9003 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
9004 		return (SCTP_RETRAN_DONE);
9005 	}
9006 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
9007 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
9008 		/* not yet open, resend the cookie and that is it */
9009 		return (1);
9010 	}
9011 #ifdef SCTP_AUDITING_ENABLED
9012 	sctp_auditing(20, inp, stcb, NULL);
9013 #endif
9014 	data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9015 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9016 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
9017 			/* No, not sent to this net or not ready for rtx */
9018 			continue;
9019 		}
9020 		if (chk->data == NULL) {
9021 			printf("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
9022 			    chk->rec.data.TSN_seq, chk->snd_count, chk->sent);
9023 			continue;
9024 		}
9025 		if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9026 		    (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9027 			/* Gak, we have exceeded max unlucky retran, abort! */
9028 			SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
9029 			    chk->snd_count,
9030 			    SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
9031 			atomic_add_int(&stcb->asoc.refcnt, 1);
9032 			sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
9033 			SCTP_TCB_LOCK(stcb);
9034 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
9035 			return (SCTP_RETRAN_EXIT);
9036 		}
9037 		/* pick up the net */
9038 		net = chk->whoTo;
9039 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9040 			mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9041 		} else {
9042 			mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9043 		}
9044 
9045 		if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9046 			/* No room in peers rwnd */
9047 			uint32_t tsn;
9048 
9049 			tsn = asoc->last_acked_seq + 1;
9050 			if (tsn == chk->rec.data.TSN_seq) {
9051 				/*
9052 				 * we make a special exception for this
9053 				 * case. The peer has no rwnd but is missing
9054 				 * the lowest chunk.. which is probably what
9055 				 * is holding up the rwnd.
9056 				 */
9057 				goto one_chunk_around;
9058 			}
9059 			return (1);
9060 		}
9061 one_chunk_around:
9062 		if (asoc->peers_rwnd < mtu) {
9063 			one_chunk = 1;
9064 			if ((asoc->peers_rwnd == 0) &&
9065 			    (asoc->total_flight == 0)) {
9066 				chk->window_probe = 1;
9067 				chk->whoTo->window_probe = 1;
9068 			}
9069 		}
9070 #ifdef SCTP_AUDITING_ENABLED
9071 		sctp_audit_log(0xC3, 2);
9072 #endif
9073 		bundle_at = 0;
9074 		m = NULL;
9075 		net->fast_retran_ip = 0;
9076 		if (chk->rec.data.doing_fast_retransmit == 0) {
9077 			/*
9078 			 * if no FR in progress skip destination that have
9079 			 * flight_size > cwnd.
9080 			 */
9081 			if (net->flight_size >= net->cwnd) {
9082 				continue;
9083 			}
9084 		} else {
9085 			/*
9086 			 * Mark the destination net to have FR recovery
9087 			 * limits put on it.
9088 			 */
9089 			*fr_done = 1;
9090 			net->fast_retran_ip = 1;
9091 		}
9092 
9093 		/*
9094 		 * if no AUTH is yet included and this chunk requires it,
9095 		 * make sure to account for it.  We don't apply the size
9096 		 * until the AUTH chunk is actually added below in case
9097 		 * there is no room for this chunk.
9098 		 */
9099 		if (data_auth_reqd && (auth == NULL)) {
9100 			dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9101 		} else
9102 			dmtu = 0;
9103 
9104 		if ((chk->send_size <= (mtu - dmtu)) ||
9105 		    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9106 			/* ok we will add this one */
9107 			if (data_auth_reqd) {
9108 				if (auth == NULL) {
9109 					m = sctp_add_auth_chunk(m,
9110 					    &endofchain,
9111 					    &auth,
9112 					    &auth_offset,
9113 					    stcb,
9114 					    SCTP_DATA);
9115 					auth_keyid = chk->auth_keyid;
9116 					override_ok = 0;
9117 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9118 				} else if (override_ok) {
9119 					auth_keyid = chk->auth_keyid;
9120 					override_ok = 0;
9121 				} else if (chk->auth_keyid != auth_keyid) {
9122 					/* different keyid, so done bundling */
9123 					break;
9124 				}
9125 			}
9126 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9127 			if (m == NULL) {
9128 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9129 				return (ENOMEM);
9130 			}
9131 			/* Do clear IP_DF ? */
9132 			if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9133 				no_fragmentflg = 0;
9134 			}
9135 			/* upate our MTU size */
9136 			if (mtu > (chk->send_size + dmtu))
9137 				mtu -= (chk->send_size + dmtu);
9138 			else
9139 				mtu = 0;
9140 			data_list[bundle_at++] = chk;
9141 			if (one_chunk && (asoc->total_flight <= 0)) {
9142 				SCTP_STAT_INCR(sctps_windowprobed);
9143 			}
9144 		}
9145 		if (one_chunk == 0) {
9146 			/*
9147 			 * now are there anymore forward from chk to pick
9148 			 * up?
9149 			 */
9150 			fwd = TAILQ_NEXT(chk, sctp_next);
9151 			while (fwd) {
9152 				if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9153 					/* Nope, not for retran */
9154 					fwd = TAILQ_NEXT(fwd, sctp_next);
9155 					continue;
9156 				}
9157 				if (fwd->whoTo != net) {
9158 					/* Nope, not the net in question */
9159 					fwd = TAILQ_NEXT(fwd, sctp_next);
9160 					continue;
9161 				}
9162 				if (data_auth_reqd && (auth == NULL)) {
9163 					dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9164 				} else
9165 					dmtu = 0;
9166 				if (fwd->send_size <= (mtu - dmtu)) {
9167 					if (data_auth_reqd) {
9168 						if (auth == NULL) {
9169 							m = sctp_add_auth_chunk(m,
9170 							    &endofchain,
9171 							    &auth,
9172 							    &auth_offset,
9173 							    stcb,
9174 							    SCTP_DATA);
9175 							auth_keyid = fwd->auth_keyid;
9176 							override_ok = 0;
9177 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9178 						} else if (override_ok) {
9179 							auth_keyid = fwd->auth_keyid;
9180 							override_ok = 0;
9181 						} else if (fwd->auth_keyid != auth_keyid) {
9182 							/*
9183 							 * different keyid,
9184 							 * so done bundling
9185 							 */
9186 							break;
9187 						}
9188 					}
9189 					m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9190 					if (m == NULL) {
9191 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9192 						return (ENOMEM);
9193 					}
9194 					/* Do clear IP_DF ? */
9195 					if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9196 						no_fragmentflg = 0;
9197 					}
9198 					/* upate our MTU size */
9199 					if (mtu > (fwd->send_size + dmtu))
9200 						mtu -= (fwd->send_size + dmtu);
9201 					else
9202 						mtu = 0;
9203 					data_list[bundle_at++] = fwd;
9204 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9205 						break;
9206 					}
9207 					fwd = TAILQ_NEXT(fwd, sctp_next);
9208 				} else {
9209 					/* can't fit so we are done */
9210 					break;
9211 				}
9212 			}
9213 		}
9214 		/* Is there something to send for this destination? */
9215 		if (m) {
9216 			/*
9217 			 * No matter if we fail/or suceed we should start a
9218 			 * timer. A failure is like a lost IP packet :-)
9219 			 */
9220 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9221 				/*
9222 				 * no timer running on this destination
9223 				 * restart it.
9224 				 */
9225 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9226 				tmr_started = 1;
9227 			}
9228 			/* Now lets send it, if there is anything to send :> */
9229 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9230 			    (struct sockaddr *)&net->ro._l_addr, m,
9231 			    auth_offset, auth, auth_keyid,
9232 			    no_fragmentflg, 0, NULL, 0,
9233 			    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9234 			    net->port, so_locked, NULL))) {
9235 				/* error, we could not output */
9236 				SCTP_STAT_INCR(sctps_lowlevelerr);
9237 				return (error);
9238 			}
9239 			m = endofchain = NULL;
9240 			auth = NULL;
9241 			auth_offset = 0;
9242 			/* For HB's */
9243 			/*
9244 			 * We don't want to mark the net->sent time here
9245 			 * since this we use this for HB and retrans cannot
9246 			 * measure RTT
9247 			 */
9248 			/* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9249 
9250 			/* For auto-close */
9251 			cnt_thru++;
9252 			if (*now_filled == 0) {
9253 				(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9254 				*now = asoc->time_last_sent;
9255 				*now_filled = 1;
9256 			} else {
9257 				asoc->time_last_sent = *now;
9258 			}
9259 			*cnt_out += bundle_at;
9260 #ifdef SCTP_AUDITING_ENABLED
9261 			sctp_audit_log(0xC4, bundle_at);
9262 #endif
9263 			if (bundle_at) {
9264 				tsns_sent = data_list[0]->rec.data.TSN_seq;
9265 			}
9266 			for (i = 0; i < bundle_at; i++) {
9267 				SCTP_STAT_INCR(sctps_sendretransdata);
9268 				data_list[i]->sent = SCTP_DATAGRAM_SENT;
9269 				/*
9270 				 * When we have a revoked data, and we
9271 				 * retransmit it, then we clear the revoked
9272 				 * flag since this flag dictates if we
9273 				 * subtracted from the fs
9274 				 */
9275 				if (data_list[i]->rec.data.chunk_was_revoked) {
9276 					/* Deflate the cwnd */
9277 					data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9278 					data_list[i]->rec.data.chunk_was_revoked = 0;
9279 				}
9280 				data_list[i]->snd_count++;
9281 				sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9282 				/* record the time */
9283 				data_list[i]->sent_rcv_time = asoc->time_last_sent;
9284 				if (data_list[i]->book_size_scale) {
9285 					/*
9286 					 * need to double the book size on
9287 					 * this one
9288 					 */
9289 					data_list[i]->book_size_scale = 0;
9290 					/*
9291 					 * Since we double the booksize, we
9292 					 * must also double the output queue
9293 					 * size, since this get shrunk when
9294 					 * we free by this amount.
9295 					 */
9296 					atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9297 					data_list[i]->book_size *= 2;
9298 
9299 
9300 				} else {
9301 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9302 						sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9303 						    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9304 					}
9305 					asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9306 					    (uint32_t) (data_list[i]->send_size +
9307 					    SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9308 				}
9309 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9310 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9311 					    data_list[i]->whoTo->flight_size,
9312 					    data_list[i]->book_size,
9313 					    (uintptr_t) data_list[i]->whoTo,
9314 					    data_list[i]->rec.data.TSN_seq);
9315 				}
9316 				sctp_flight_size_increase(data_list[i]);
9317 				sctp_total_flight_increase(stcb, data_list[i]);
9318 				if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9319 					/* SWS sender side engages */
9320 					asoc->peers_rwnd = 0;
9321 				}
9322 				if ((i == 0) &&
9323 				    (data_list[i]->rec.data.doing_fast_retransmit)) {
9324 					SCTP_STAT_INCR(sctps_sendfastretrans);
9325 					if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9326 					    (tmr_started == 0)) {
9327 						/*-
9328 						 * ok we just fast-retrans'd
9329 						 * the lowest TSN, i.e the
9330 						 * first on the list. In
9331 						 * this case we want to give
9332 						 * some more time to get a
9333 						 * SACK back without a
9334 						 * t3-expiring.
9335 						 */
9336 						sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9337 						    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9338 						sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9339 					}
9340 				}
9341 			}
9342 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9343 				sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9344 			}
9345 #ifdef SCTP_AUDITING_ENABLED
9346 			sctp_auditing(21, inp, stcb, NULL);
9347 #endif
9348 		} else {
9349 			/* None will fit */
9350 			return (1);
9351 		}
9352 		if (asoc->sent_queue_retran_cnt <= 0) {
9353 			/* all done we have no more to retran */
9354 			asoc->sent_queue_retran_cnt = 0;
9355 			break;
9356 		}
9357 		if (one_chunk) {
9358 			/* No more room in rwnd */
9359 			return (1);
9360 		}
9361 		/* stop the for loop here. we sent out a packet */
9362 		break;
9363 	}
9364 	return (0);
9365 }
9366 
9367 
9368 static int
9369 sctp_timer_validation(struct sctp_inpcb *inp,
9370     struct sctp_tcb *stcb,
9371     struct sctp_association *asoc,
9372     int ret)
9373 {
9374 	struct sctp_nets *net;
9375 
9376 	/* Validate that a timer is running somewhere */
9377 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9378 		if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9379 			/* Here is a timer */
9380 			return (ret);
9381 		}
9382 	}
9383 	SCTP_TCB_LOCK_ASSERT(stcb);
9384 	/* Gak, we did not have a timer somewhere */
9385 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9386 	sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9387 	return (ret);
9388 }
9389 
9390 void
9391 sctp_chunk_output(struct sctp_inpcb *inp,
9392     struct sctp_tcb *stcb,
9393     int from_where,
9394     int so_locked
9395 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9396     SCTP_UNUSED
9397 #endif
9398 )
9399 {
9400 	/*-
9401 	 * Ok this is the generic chunk service queue. we must do the
9402 	 * following:
9403 	 * - See if there are retransmits pending, if so we must
9404 	 *   do these first.
9405 	 * - Service the stream queue that is next, moving any
9406 	 *   message (note I must get a complete message i.e.
9407 	 *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9408 	 *   TSN's
9409 	 * - Check to see if the cwnd/rwnd allows any output, if so we
9410 	 *   go ahead and fomulate and send the low level chunks. Making sure
9411 	 *   to combine any control in the control chunk queue also.
9412 	 */
9413 	struct sctp_association *asoc;
9414 	struct sctp_nets *net;
9415 	int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
9416 	    burst_cnt = 0, burst_limit = 0;
9417 	struct timeval now;
9418 	int now_filled = 0;
9419 	int nagle_on = 0;
9420 	int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9421 	int un_sent = 0;
9422 	int fr_done, tot_frs = 0;
9423 
9424 	asoc = &stcb->asoc;
9425 	if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9426 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9427 			nagle_on = 0;
9428 		} else {
9429 			nagle_on = 1;
9430 		}
9431 	}
9432 	SCTP_TCB_LOCK_ASSERT(stcb);
9433 
9434 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9435 
9436 	if ((un_sent <= 0) &&
9437 	    (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9438 	    (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
9439 	    (asoc->sent_queue_retran_cnt == 0)) {
9440 		/* Nothing to do unless there is something to be sent left */
9441 		return;
9442 	}
9443 	/*
9444 	 * Do we have something to send, data or control AND a sack timer
9445 	 * running, if so piggy-back the sack.
9446 	 */
9447 	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9448 		sctp_send_sack(stcb);
9449 		(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9450 	}
9451 	while (asoc->sent_queue_retran_cnt) {
9452 		/*-
9453 		 * Ok, it is retransmission time only, we send out only ONE
9454 		 * packet with a single call off to the retran code.
9455 		 */
9456 		if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9457 			/*-
9458 			 * Special hook for handling cookiess discarded
9459 			 * by peer that carried data. Send cookie-ack only
9460 			 * and then the next call with get the retran's.
9461 			 */
9462 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9463 			    from_where,
9464 			    &now, &now_filled, frag_point, so_locked);
9465 			return;
9466 		} else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9467 			/* if its not from a HB then do it */
9468 			fr_done = 0;
9469 			ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9470 			if (fr_done) {
9471 				tot_frs++;
9472 			}
9473 		} else {
9474 			/*
9475 			 * its from any other place, we don't allow retran
9476 			 * output (only control)
9477 			 */
9478 			ret = 1;
9479 		}
9480 		if (ret > 0) {
9481 			/* Can't send anymore */
9482 			/*-
9483 			 * now lets push out control by calling med-level
9484 			 * output once. this assures that we WILL send HB's
9485 			 * if queued too.
9486 			 */
9487 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9488 			    from_where,
9489 			    &now, &now_filled, frag_point, so_locked);
9490 #ifdef SCTP_AUDITING_ENABLED
9491 			sctp_auditing(8, inp, stcb, NULL);
9492 #endif
9493 			(void)sctp_timer_validation(inp, stcb, asoc, ret);
9494 			return;
9495 		}
9496 		if (ret < 0) {
9497 			/*-
9498 			 * The count was off.. retran is not happening so do
9499 			 * the normal retransmission.
9500 			 */
9501 #ifdef SCTP_AUDITING_ENABLED
9502 			sctp_auditing(9, inp, stcb, NULL);
9503 #endif
9504 			if (ret == SCTP_RETRAN_EXIT) {
9505 				return;
9506 			}
9507 			break;
9508 		}
9509 		if (from_where == SCTP_OUTPUT_FROM_T3) {
9510 			/* Only one transmission allowed out of a timeout */
9511 #ifdef SCTP_AUDITING_ENABLED
9512 			sctp_auditing(10, inp, stcb, NULL);
9513 #endif
9514 			/* Push out any control */
9515 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
9516 			    &now, &now_filled, frag_point, so_locked);
9517 			return;
9518 		}
9519 		if (tot_frs > asoc->max_burst) {
9520 			/* Hit FR burst limit */
9521 			return;
9522 		}
9523 		if ((num_out == 0) && (ret == 0)) {
9524 
9525 			/* No more retrans to send */
9526 			break;
9527 		}
9528 	}
9529 #ifdef SCTP_AUDITING_ENABLED
9530 	sctp_auditing(12, inp, stcb, NULL);
9531 #endif
9532 	/* Check for bad destinations, if they exist move chunks around. */
9533 	burst_limit = asoc->max_burst;
9534 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9535 		if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
9536 		    SCTP_ADDR_NOT_REACHABLE) {
9537 			/*-
9538 			 * if possible move things off of this address we
9539 			 * still may send below due to the dormant state but
9540 			 * we try to find an alternate address to send to
9541 			 * and if we have one we move all queued data on the
9542 			 * out wheel to this alternate address.
9543 			 */
9544 			if (net->ref_count > 1)
9545 				sctp_move_to_an_alt(stcb, asoc, net);
9546 		} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
9547 			    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
9548 		    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
9549 			/*
9550 			 * JRS 5/14/07 - If CMT PF is on and the current
9551 			 * destination is in PF state, move all queued data
9552 			 * to an alternate desination.
9553 			 */
9554 			if (net->ref_count > 1)
9555 				sctp_move_to_an_alt(stcb, asoc, net);
9556 		} else {
9557 			/*-
9558 			 * if ((asoc->sat_network) || (net->addr_is_local))
9559 			 * { burst_limit = asoc->max_burst *
9560 			 * SCTP_SAT_NETWORK_BURST_INCR; }
9561 			 */
9562 			if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
9563 				if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
9564 					/*
9565 					 * JRS - Use the congestion control
9566 					 * given in the congestion control
9567 					 * module
9568 					 */
9569 					asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
9570 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9571 						sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
9572 					}
9573 					SCTP_STAT_INCR(sctps_maxburstqueued);
9574 				}
9575 				net->fast_retran_ip = 0;
9576 			} else {
9577 				if (net->flight_size == 0) {
9578 					/* Should be decaying the cwnd here */
9579 					;
9580 				}
9581 			}
9582 		}
9583 
9584 	}
9585 	burst_cnt = 0;
9586 	do {
9587 		error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
9588 		    &reason_code, 0, from_where,
9589 		    &now, &now_filled, frag_point, so_locked);
9590 		if (error) {
9591 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
9592 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9593 				sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
9594 			}
9595 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9596 				sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
9597 				sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
9598 			}
9599 			break;
9600 		}
9601 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
9602 
9603 		tot_out += num_out;
9604 		burst_cnt++;
9605 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9606 			sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
9607 			if (num_out == 0) {
9608 				sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
9609 			}
9610 		}
9611 		if (nagle_on) {
9612 			/*-
9613 			 * When nagle is on, we look at how much is un_sent, then
9614 			 * if its smaller than an MTU and we have data in
9615 			 * flight we stop.
9616 			 */
9617 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9618 			    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
9619 			if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
9620 			    (stcb->asoc.total_flight > 0)) {
9621 				break;
9622 			}
9623 		}
9624 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9625 		    TAILQ_EMPTY(&asoc->send_queue) &&
9626 		    TAILQ_EMPTY(&asoc->out_wheel)) {
9627 			/* Nothing left to send */
9628 			break;
9629 		}
9630 		if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
9631 			/* Nothing left to send */
9632 			break;
9633 		}
9634 	} while (num_out && (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
9635 	    (burst_cnt < burst_limit)));
9636 
9637 	if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
9638 		if (burst_cnt >= burst_limit) {
9639 			SCTP_STAT_INCR(sctps_maxburstqueued);
9640 			asoc->burst_limit_applied = 1;
9641 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9642 				sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
9643 			}
9644 		} else {
9645 			asoc->burst_limit_applied = 0;
9646 		}
9647 	}
9648 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9649 		sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
9650 	}
9651 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
9652 	    tot_out);
9653 
9654 	/*-
9655 	 * Now we need to clean up the control chunk chain if a ECNE is on
9656 	 * it. It must be marked as UNSENT again so next call will continue
9657 	 * to send it until such time that we get a CWR, to remove it.
9658 	 */
9659 	if (stcb->asoc.ecn_echo_cnt_onq)
9660 		sctp_fix_ecn_echo(asoc);
9661 	return;
9662 }
9663 
9664 
9665 int
9666 sctp_output(inp, m, addr, control, p, flags)
9667 	struct sctp_inpcb *inp;
9668 	struct mbuf *m;
9669 	struct sockaddr *addr;
9670 	struct mbuf *control;
9671 	struct thread *p;
9672 	int flags;
9673 {
9674 	if (inp == NULL) {
9675 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9676 		return (EINVAL);
9677 	}
9678 	if (inp->sctp_socket == NULL) {
9679 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9680 		return (EINVAL);
9681 	}
9682 	return (sctp_sosend(inp->sctp_socket,
9683 	    addr,
9684 	    (struct uio *)NULL,
9685 	    m,
9686 	    control,
9687 	    flags, p
9688 	    ));
9689 }
9690 
9691 void
9692 send_forward_tsn(struct sctp_tcb *stcb,
9693     struct sctp_association *asoc)
9694 {
9695 	struct sctp_tmit_chunk *chk;
9696 	struct sctp_forward_tsn_chunk *fwdtsn;
9697 	uint32_t advance_peer_ack_point;
9698 
9699 	SCTP_TCB_LOCK_ASSERT(stcb);
9700 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9701 		if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9702 			/* mark it to unsent */
9703 			chk->sent = SCTP_DATAGRAM_UNSENT;
9704 			chk->snd_count = 0;
9705 			/* Do we correct its output location? */
9706 			if (chk->whoTo != asoc->primary_destination) {
9707 				sctp_free_remote_addr(chk->whoTo);
9708 				chk->whoTo = asoc->primary_destination;
9709 				atomic_add_int(&chk->whoTo->ref_count, 1);
9710 			}
9711 			goto sctp_fill_in_rest;
9712 		}
9713 	}
9714 	/* Ok if we reach here we must build one */
9715 	sctp_alloc_a_chunk(stcb, chk);
9716 	if (chk == NULL) {
9717 		return;
9718 	}
9719 	asoc->fwd_tsn_cnt++;
9720 	chk->copy_by_ref = 0;
9721 	chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
9722 	chk->rec.chunk_id.can_take_data = 0;
9723 	chk->asoc = asoc;
9724 	chk->whoTo = NULL;
9725 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9726 	if (chk->data == NULL) {
9727 		sctp_free_a_chunk(stcb, chk);
9728 		return;
9729 	}
9730 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9731 	chk->sent = SCTP_DATAGRAM_UNSENT;
9732 	chk->snd_count = 0;
9733 	chk->whoTo = asoc->primary_destination;
9734 	atomic_add_int(&chk->whoTo->ref_count, 1);
9735 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
9736 	asoc->ctrl_queue_cnt++;
9737 sctp_fill_in_rest:
9738 	/*-
9739 	 * Here we go through and fill out the part that deals with
9740 	 * stream/seq of the ones we skip.
9741 	 */
9742 	SCTP_BUF_LEN(chk->data) = 0;
9743 	{
9744 		struct sctp_tmit_chunk *at, *tp1, *last;
9745 		struct sctp_strseq *strseq;
9746 		unsigned int cnt_of_space, i, ovh;
9747 		unsigned int space_needed;
9748 		unsigned int cnt_of_skipped = 0;
9749 
9750 		TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
9751 			if (at->sent != SCTP_FORWARD_TSN_SKIP) {
9752 				/* no more to look at */
9753 				break;
9754 			}
9755 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9756 				/* We don't report these */
9757 				continue;
9758 			}
9759 			cnt_of_skipped++;
9760 		}
9761 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
9762 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
9763 
9764 		cnt_of_space = M_TRAILINGSPACE(chk->data);
9765 
9766 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9767 			ovh = SCTP_MIN_OVERHEAD;
9768 		} else {
9769 			ovh = SCTP_MIN_V4_OVERHEAD;
9770 		}
9771 		if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
9772 			/* trim to a mtu size */
9773 			cnt_of_space = asoc->smallest_mtu - ovh;
9774 		}
9775 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9776 			sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9777 			    0xff, 0, cnt_of_skipped,
9778 			    asoc->advanced_peer_ack_point);
9779 
9780 		}
9781 		advance_peer_ack_point = asoc->advanced_peer_ack_point;
9782 		if (cnt_of_space < space_needed) {
9783 			/*-
9784 			 * ok we must trim down the chunk by lowering the
9785 			 * advance peer ack point.
9786 			 */
9787 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9788 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9789 				    0xff, 0xff, cnt_of_space,
9790 				    space_needed);
9791 			}
9792 			cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
9793 			cnt_of_skipped /= sizeof(struct sctp_strseq);
9794 			/*-
9795 			 * Go through and find the TSN that will be the one
9796 			 * we report.
9797 			 */
9798 			at = TAILQ_FIRST(&asoc->sent_queue);
9799 			for (i = 0; i < cnt_of_skipped; i++) {
9800 				tp1 = TAILQ_NEXT(at, sctp_next);
9801 				at = tp1;
9802 			}
9803 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9804 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9805 				    0xff, cnt_of_skipped, at->rec.data.TSN_seq,
9806 				    asoc->advanced_peer_ack_point);
9807 			}
9808 			last = at;
9809 			/*-
9810 			 * last now points to last one I can report, update
9811 			 * peer ack point
9812 			 */
9813 			advance_peer_ack_point = last->rec.data.TSN_seq;
9814 			space_needed = sizeof(struct sctp_forward_tsn_chunk) +
9815 			    cnt_of_skipped * sizeof(struct sctp_strseq);
9816 		}
9817 		chk->send_size = space_needed;
9818 		/* Setup the chunk */
9819 		fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
9820 		fwdtsn->ch.chunk_length = htons(chk->send_size);
9821 		fwdtsn->ch.chunk_flags = 0;
9822 		fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
9823 		fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
9824 		SCTP_BUF_LEN(chk->data) = chk->send_size;
9825 		fwdtsn++;
9826 		/*-
9827 		 * Move pointer to after the fwdtsn and transfer to the
9828 		 * strseq pointer.
9829 		 */
9830 		strseq = (struct sctp_strseq *)fwdtsn;
9831 		/*-
9832 		 * Now populate the strseq list. This is done blindly
9833 		 * without pulling out duplicate stream info. This is
9834 		 * inefficent but won't harm the process since the peer will
9835 		 * look at these in sequence and will thus release anything.
9836 		 * It could mean we exceed the PMTU and chop off some that
9837 		 * we could have included.. but this is unlikely (aka 1432/4
9838 		 * would mean 300+ stream seq's would have to be reported in
9839 		 * one FWD-TSN. With a bit of work we can later FIX this to
9840 		 * optimize and pull out duplcates.. but it does add more
9841 		 * overhead. So for now... not!
9842 		 */
9843 		at = TAILQ_FIRST(&asoc->sent_queue);
9844 		for (i = 0; i < cnt_of_skipped; i++) {
9845 			tp1 = TAILQ_NEXT(at, sctp_next);
9846 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9847 				/* We don't report these */
9848 				i--;
9849 				at = tp1;
9850 				continue;
9851 			}
9852 			if (at->rec.data.TSN_seq == advance_peer_ack_point) {
9853 				at->rec.data.fwd_tsn_cnt = 0;
9854 			}
9855 			strseq->stream = ntohs(at->rec.data.stream_number);
9856 			strseq->sequence = ntohs(at->rec.data.stream_seq);
9857 			strseq++;
9858 			at = tp1;
9859 		}
9860 	}
9861 	return;
9862 
9863 }
9864 
9865 void
9866 sctp_send_sack(struct sctp_tcb *stcb)
9867 {
9868 	/*-
9869 	 * Queue up a SACK or NR-SACK in the control queue.
9870 	 * We must first check to see if a SACK or NR-SACK is
9871 	 * somehow on the control queue.
9872 	 * If so, we will take and and remove the old one.
9873 	 */
9874 	struct sctp_association *asoc;
9875 	struct sctp_tmit_chunk *chk, *a_chk;
9876 	struct sctp_sack_chunk *sack;
9877 	struct sctp_nr_sack_chunk *nr_sack;
9878 	struct sctp_gap_ack_block *gap_descriptor;
9879 	struct sack_track *selector;
9880 	int mergeable = 0;
9881 	int offset;
9882 	caddr_t limit;
9883 	uint32_t *dup;
9884 	int limit_reached = 0;
9885 	unsigned int i, sel_start, siz, j, starting_index;
9886 	unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
9887 	int num_dups = 0;
9888 	int space_req;
9889 	uint32_t highest_tsn;
9890 	uint8_t flags;
9891 	uint8_t type;
9892 
9893 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) &&
9894 	    stcb->asoc.peer_supports_nr_sack) {
9895 		type = SCTP_NR_SELECTIVE_ACK;
9896 	} else {
9897 		type = SCTP_SELECTIVE_ACK;
9898 	}
9899 	a_chk = NULL;
9900 	asoc = &stcb->asoc;
9901 	SCTP_TCB_LOCK_ASSERT(stcb);
9902 	if (asoc->last_data_chunk_from == NULL) {
9903 		/* Hmm we never received anything */
9904 		return;
9905 	}
9906 	sctp_slide_mapping_arrays(stcb);
9907 	sctp_set_rwnd(stcb, asoc);
9908 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9909 		if (chk->rec.chunk_id.id == type) {
9910 			/* Hmm, found a sack already on queue, remove it */
9911 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
9912 			asoc->ctrl_queue_cnt++;
9913 			a_chk = chk;
9914 			if (a_chk->data) {
9915 				sctp_m_freem(a_chk->data);
9916 				a_chk->data = NULL;
9917 			}
9918 			sctp_free_remote_addr(a_chk->whoTo);
9919 			a_chk->whoTo = NULL;
9920 			break;
9921 		}
9922 	}
9923 	if (a_chk == NULL) {
9924 		sctp_alloc_a_chunk(stcb, a_chk);
9925 		if (a_chk == NULL) {
9926 			/* No memory so we drop the idea, and set a timer */
9927 			if (stcb->asoc.delayed_ack) {
9928 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9929 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
9930 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9931 				    stcb->sctp_ep, stcb, NULL);
9932 			} else {
9933 				stcb->asoc.send_sack = 1;
9934 			}
9935 			return;
9936 		}
9937 		a_chk->copy_by_ref = 0;
9938 		a_chk->rec.chunk_id.id = type;
9939 		a_chk->rec.chunk_id.can_take_data = 1;
9940 	}
9941 	/* Clear our pkt counts */
9942 	asoc->data_pkts_seen = 0;
9943 
9944 	a_chk->asoc = asoc;
9945 	a_chk->snd_count = 0;
9946 	a_chk->send_size = 0;	/* fill in later */
9947 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
9948 	a_chk->whoTo = NULL;
9949 
9950 	if ((asoc->numduptsns) ||
9951 	    (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
9952 	    ) {
9953 		/*-
9954 		 * Ok, we have some duplicates or the destination for the
9955 		 * sack is unreachable, lets see if we can select an
9956 		 * alternate than asoc->last_data_chunk_from
9957 		 */
9958 		if ((!(asoc->last_data_chunk_from->dest_state &
9959 		    SCTP_ADDR_NOT_REACHABLE)) &&
9960 		    (asoc->used_alt_onsack > asoc->numnets)) {
9961 			/* We used an alt last time, don't this time */
9962 			a_chk->whoTo = NULL;
9963 		} else {
9964 			asoc->used_alt_onsack++;
9965 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
9966 		}
9967 		if (a_chk->whoTo == NULL) {
9968 			/* Nope, no alternate */
9969 			a_chk->whoTo = asoc->last_data_chunk_from;
9970 			asoc->used_alt_onsack = 0;
9971 		}
9972 	} else {
9973 		/*
9974 		 * No duplicates so we use the last place we received data
9975 		 * from.
9976 		 */
9977 		asoc->used_alt_onsack = 0;
9978 		a_chk->whoTo = asoc->last_data_chunk_from;
9979 	}
9980 	if (a_chk->whoTo) {
9981 		atomic_add_int(&a_chk->whoTo->ref_count, 1);
9982 	}
9983 	if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
9984 		highest_tsn = asoc->highest_tsn_inside_map;
9985 	} else {
9986 		highest_tsn = asoc->highest_tsn_inside_nr_map;
9987 	}
9988 	if (highest_tsn == asoc->cumulative_tsn) {
9989 		/* no gaps */
9990 		if (type == SCTP_SELECTIVE_ACK) {
9991 			space_req = sizeof(struct sctp_sack_chunk);
9992 		} else {
9993 			space_req = sizeof(struct sctp_nr_sack_chunk);
9994 		}
9995 	} else {
9996 		/* gaps get a cluster */
9997 		space_req = MCLBYTES;
9998 	}
9999 	/* Ok now lets formulate a MBUF with our sack */
10000 	a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
10001 	if ((a_chk->data == NULL) ||
10002 	    (a_chk->whoTo == NULL)) {
10003 		/* rats, no mbuf memory */
10004 		if (a_chk->data) {
10005 			/* was a problem with the destination */
10006 			sctp_m_freem(a_chk->data);
10007 			a_chk->data = NULL;
10008 		}
10009 		sctp_free_a_chunk(stcb, a_chk);
10010 		/* sa_ignore NO_NULL_CHK */
10011 		if (stcb->asoc.delayed_ack) {
10012 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10013 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
10014 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10015 			    stcb->sctp_ep, stcb, NULL);
10016 		} else {
10017 			stcb->asoc.send_sack = 1;
10018 		}
10019 		return;
10020 	}
10021 	/* ok, lets go through and fill it in */
10022 	SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10023 	space = M_TRAILINGSPACE(a_chk->data);
10024 	if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10025 		space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10026 	}
10027 	limit = mtod(a_chk->data, caddr_t);
10028 	limit += space;
10029 
10030 	/* 0x01 is used by nonce for ecn */
10031 	if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
10032 	    (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
10033 	    (asoc->peer_supports_ecn_nonce))
10034 		flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
10035 	else
10036 		flags = 0;
10037 
10038 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10039 		/*-
10040 		 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10041 		 * received, then set high bit to 1, else 0. Reset
10042 		 * pkts_rcvd.
10043 		 */
10044 		flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10045 		asoc->cmt_dac_pkts_rcvd = 0;
10046 	}
10047 #ifdef SCTP_ASOCLOG_OF_TSNS
10048 	stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10049 	stcb->asoc.cumack_log_atsnt++;
10050 	if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10051 		stcb->asoc.cumack_log_atsnt = 0;
10052 	}
10053 #endif
10054 	/* reset the readers interpretation */
10055 	stcb->freed_by_sorcv_sincelast = 0;
10056 
10057 	if (type == SCTP_SELECTIVE_ACK) {
10058 		sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10059 		nr_sack = NULL;
10060 		gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10061 		if (highest_tsn > asoc->mapping_array_base_tsn) {
10062 			siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10063 		} else {
10064 			siz = (((MAX_TSN - highest_tsn) + 1) + highest_tsn + 7) / 8;
10065 		}
10066 	} else {
10067 		sack = NULL;
10068 		nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10069 		gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10070 		if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
10071 			siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10072 		} else {
10073 			siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10074 		}
10075 	}
10076 
10077 	if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10078 		offset = 1;
10079 		/*-
10080 		 * The base TSN is intialized to be the first TSN the peer
10081 		 * will send us. If the cum-ack is behind this then when they
10082 		 * send us the next in sequence it will mark the base_tsn bit.
10083 		 * Thus we need to use the very first selector and the offset
10084 		 * is 1. Our table is built for this case.
10085 		 */
10086 		starting_index = 0;
10087 		sel_start = 0;
10088 	} else {
10089 		/*-
10090 		 * we skip the first selector  when the cum-ack is at or above the
10091 		 * mapping array base. This is because the bits at the base or above
10092 		 * are turned on and our first selector in the table assumes they are
10093 		 * off. We thus will use the second selector (first is 0). We use
10094 		 * the reverse of our macro to fix the offset, in bits, that our
10095 		 * table is at. Note that this method assumes that the cum-tsn is
10096 		 * within the first bit, i.e. its value is 0-7 which means the
10097 		 * result to our offset will be either a 0 - -7. If the cumack
10098 		 * is NOT in the first byte (0) (which it should be since we did
10099 		 * a mapping array slide above) then we need to calculate the starting
10100 		 * index i.e. which byte of the mapping array we should start at. We
10101 		 * do this by dividing by 8 and pushing the remainder (mod) into offset.
10102 		 * then we multiply the offset to be negative, since we need a negative
10103 		 * offset into the selector table.
10104 		 */
10105 		SCTP_CALC_TSN_TO_GAP(offset, asoc->cumulative_tsn, asoc->mapping_array_base_tsn);
10106 		if (offset > 7) {
10107 			starting_index = offset / 8;
10108 			offset = offset % 8;
10109 			printf("Strange starting index is %d offset:%d (not 0/x)\n",
10110 			    starting_index, offset);
10111 		} else {
10112 			starting_index = 0;
10113 		}
10114 		/* We need a negative offset in our table */
10115 		offset *= -1;
10116 		sel_start = 1;
10117 	}
10118 	if (((type == SCTP_SELECTIVE_ACK) &&
10119 	    compare_with_wrap(highest_tsn, asoc->cumulative_tsn, MAX_TSN)) ||
10120 	    ((type == SCTP_NR_SELECTIVE_ACK) &&
10121 	    compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN))) {
10122 		/* we have a gap .. maybe */
10123 		for (i = starting_index; i < siz; i++) {
10124 			if (type == SCTP_SELECTIVE_ACK) {
10125 				selector = &sack_array[asoc->mapping_array[i] | asoc->nr_mapping_array[i]];
10126 			} else {
10127 				selector = &sack_array[asoc->mapping_array[i]];
10128 			}
10129 			if (mergeable && selector->right_edge) {
10130 				/*
10131 				 * Backup, left and right edges were ok to
10132 				 * merge.
10133 				 */
10134 				num_gap_blocks--;
10135 				gap_descriptor--;
10136 			}
10137 			if (selector->num_entries == 0)
10138 				mergeable = 0;
10139 			else {
10140 				for (j = sel_start; j < selector->num_entries; j++) {
10141 					if (mergeable && selector->right_edge) {
10142 						/*
10143 						 * do a merge by NOT setting
10144 						 * the left side
10145 						 */
10146 						mergeable = 0;
10147 					} else {
10148 						/*
10149 						 * no merge, set the left
10150 						 * side
10151 						 */
10152 						mergeable = 0;
10153 						gap_descriptor->start = htons((selector->gaps[j].start + offset));
10154 					}
10155 					gap_descriptor->end = htons((selector->gaps[j].end + offset));
10156 					num_gap_blocks++;
10157 					gap_descriptor++;
10158 					if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10159 						/* no more room */
10160 						limit_reached = 1;
10161 						break;
10162 					}
10163 				}
10164 				if (selector->left_edge) {
10165 					mergeable = 1;
10166 				}
10167 			}
10168 			if (limit_reached) {
10169 				/* Reached the limit stop */
10170 				break;
10171 			}
10172 			sel_start = 0;
10173 			offset += 8;
10174 		}
10175 	}
10176 	if ((type == SCTP_NR_SELECTIVE_ACK) &&
10177 	    (limit_reached == 0)) {
10178 
10179 		mergeable = 0;
10180 
10181 		if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn)
10182 			siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10183 		else
10184 			siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10185 
10186 		if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10187 			offset = 1;
10188 			/*-
10189 			* cum-ack behind the mapping array, so we start and use all
10190 			* entries.
10191 			*/
10192 			sel_start = 0;
10193 		} else {
10194 			offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10195 			/*-
10196 			* we skip the first one when the cum-ack is at or above the
10197 			* mapping array base. Note this only works if
10198 			*/
10199 			sel_start = 1;
10200 		}
10201 		if (compare_with_wrap(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn, MAX_TSN)) {
10202 			/* we have a gap .. maybe */
10203 			for (i = 0; i < siz; i++) {
10204 				selector = &sack_array[asoc->nr_mapping_array[i]];
10205 				if (mergeable && selector->right_edge) {
10206 					/*
10207 					 * Backup, left and right edges were
10208 					 * ok to merge.
10209 					 */
10210 					num_nr_gap_blocks--;
10211 					gap_descriptor--;
10212 				}
10213 				if (selector->num_entries == 0)
10214 					mergeable = 0;
10215 				else {
10216 					for (j = sel_start; j < selector->num_entries; j++) {
10217 						if (mergeable && selector->right_edge) {
10218 							/*
10219 							 * do a merge by NOT
10220 							 * setting the left
10221 							 * side
10222 							 */
10223 							mergeable = 0;
10224 						} else {
10225 							/*
10226 							 * no merge, set the
10227 							 * left side
10228 							 */
10229 							mergeable = 0;
10230 							gap_descriptor->start = htons((selector->gaps[j].start + offset));
10231 						}
10232 						gap_descriptor->end = htons((selector->gaps[j].end + offset));
10233 						num_nr_gap_blocks++;
10234 						gap_descriptor++;
10235 						if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10236 							/* no more room */
10237 							limit_reached = 1;
10238 							break;
10239 						}
10240 					}
10241 					if (selector->left_edge) {
10242 						mergeable = 1;
10243 					}
10244 				}
10245 				if (limit_reached) {
10246 					/* Reached the limit stop */
10247 					break;
10248 				}
10249 				sel_start = 0;
10250 				offset += 8;
10251 			}
10252 		}
10253 	}
10254 	/* now we must add any dups we are going to report. */
10255 	if ((limit_reached == 0) && (asoc->numduptsns)) {
10256 		dup = (uint32_t *) gap_descriptor;
10257 		for (i = 0; i < asoc->numduptsns; i++) {
10258 			*dup = htonl(asoc->dup_tsns[i]);
10259 			dup++;
10260 			num_dups++;
10261 			if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10262 				/* no more room */
10263 				break;
10264 			}
10265 		}
10266 		asoc->numduptsns = 0;
10267 	}
10268 	/*
10269 	 * now that the chunk is prepared queue it to the control chunk
10270 	 * queue.
10271 	 */
10272 	if (type == SCTP_SELECTIVE_ACK) {
10273 		a_chk->send_size = sizeof(struct sctp_sack_chunk) +
10274 		    (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10275 		    num_dups * sizeof(int32_t);
10276 		SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10277 		sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10278 		sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10279 		sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10280 		sack->sack.num_dup_tsns = htons(num_dups);
10281 		sack->ch.chunk_type = type;
10282 		sack->ch.chunk_flags = flags;
10283 		sack->ch.chunk_length = htons(a_chk->send_size);
10284 	} else {
10285 		a_chk->send_size = sizeof(struct sctp_nr_sack_chunk) +
10286 		    (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10287 		    num_dups * sizeof(int32_t);
10288 		SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10289 		nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10290 		nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10291 		nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10292 		nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10293 		nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10294 		nr_sack->nr_sack.reserved = 0;
10295 		nr_sack->ch.chunk_type = type;
10296 		nr_sack->ch.chunk_flags = flags;
10297 		nr_sack->ch.chunk_length = htons(a_chk->send_size);
10298 	}
10299 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10300 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
10301 	asoc->ctrl_queue_cnt++;
10302 	asoc->send_sack = 0;
10303 	SCTP_STAT_INCR(sctps_sendsacks);
10304 	return;
10305 }
10306 
10307 void
10308 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10309 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10310     SCTP_UNUSED
10311 #endif
10312 )
10313 {
10314 	struct mbuf *m_abort;
10315 	struct mbuf *m_out = NULL, *m_end = NULL;
10316 	struct sctp_abort_chunk *abort = NULL;
10317 	int sz;
10318 	uint32_t auth_offset = 0;
10319 	struct sctp_auth_chunk *auth = NULL;
10320 
10321 	/*-
10322 	 * Add an AUTH chunk, if chunk requires it and save the offset into
10323 	 * the chain for AUTH
10324 	 */
10325 	if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10326 	    stcb->asoc.peer_auth_chunks)) {
10327 		m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10328 		    stcb, SCTP_ABORT_ASSOCIATION);
10329 		SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10330 	}
10331 	SCTP_TCB_LOCK_ASSERT(stcb);
10332 	m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10333 	if (m_abort == NULL) {
10334 		/* no mbuf's */
10335 		if (m_out)
10336 			sctp_m_freem(m_out);
10337 		return;
10338 	}
10339 	/* link in any error */
10340 	SCTP_BUF_NEXT(m_abort) = operr;
10341 	sz = 0;
10342 	if (operr) {
10343 		struct mbuf *n;
10344 
10345 		n = operr;
10346 		while (n) {
10347 			sz += SCTP_BUF_LEN(n);
10348 			n = SCTP_BUF_NEXT(n);
10349 		}
10350 	}
10351 	SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10352 	if (m_out == NULL) {
10353 		/* NO Auth chunk prepended, so reserve space in front */
10354 		SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10355 		m_out = m_abort;
10356 	} else {
10357 		/* Put AUTH chunk at the front of the chain */
10358 		SCTP_BUF_NEXT(m_end) = m_abort;
10359 	}
10360 
10361 	/* fill in the ABORT chunk */
10362 	abort = mtod(m_abort, struct sctp_abort_chunk *);
10363 	abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10364 	abort->ch.chunk_flags = 0;
10365 	abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10366 
10367 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
10368 	    stcb->asoc.primary_destination,
10369 	    (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
10370 	    m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, NULL, 0,
10371 	    stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10372 	    stcb->asoc.primary_destination->port, so_locked, NULL);
10373 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10374 }
10375 
10376 void
10377 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10378     struct sctp_nets *net,
10379     int reflect_vtag)
10380 {
10381 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10382 	struct mbuf *m_shutdown_comp;
10383 	struct sctp_shutdown_complete_chunk *shutdown_complete;
10384 	uint32_t vtag;
10385 	uint8_t flags;
10386 
10387 	m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10388 	if (m_shutdown_comp == NULL) {
10389 		/* no mbuf's */
10390 		return;
10391 	}
10392 	if (reflect_vtag) {
10393 		flags = SCTP_HAD_NO_TCB;
10394 		vtag = stcb->asoc.my_vtag;
10395 	} else {
10396 		flags = 0;
10397 		vtag = stcb->asoc.peer_vtag;
10398 	}
10399 	shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10400 	shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10401 	shutdown_complete->ch.chunk_flags = flags;
10402 	shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10403 	SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10404 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10405 	    (struct sockaddr *)&net->ro._l_addr,
10406 	    m_shutdown_comp, 0, NULL, 0, 1, 0, NULL, 0,
10407 	    stcb->sctp_ep->sctp_lport, stcb->rport,
10408 	    htonl(vtag),
10409 	    net->port, SCTP_SO_NOT_LOCKED, NULL);
10410 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10411 	return;
10412 }
10413 
10414 void
10415 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
10416     uint32_t vrf_id, uint16_t port)
10417 {
10418 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10419 	struct mbuf *o_pak;
10420 	struct mbuf *mout;
10421 	struct ip *iph, *iph_out;
10422 	struct udphdr *udp = NULL;
10423 
10424 #ifdef INET6
10425 	struct ip6_hdr *ip6, *ip6_out;
10426 
10427 #endif
10428 	int offset_out, len, mlen;
10429 	struct sctp_shutdown_complete_msg *comp_cp;
10430 
10431 	iph = mtod(m, struct ip *);
10432 	switch (iph->ip_v) {
10433 	case IPVERSION:
10434 		len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10435 		break;
10436 #ifdef INET6
10437 	case IPV6_VERSION >> 4:
10438 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10439 		break;
10440 #endif
10441 	default:
10442 		return;
10443 	}
10444 	if (port) {
10445 		len += sizeof(struct udphdr);
10446 	}
10447 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
10448 	if (mout == NULL) {
10449 		return;
10450 	}
10451 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
10452 	SCTP_BUF_LEN(mout) = len;
10453 	SCTP_BUF_NEXT(mout) = NULL;
10454 	iph_out = NULL;
10455 #ifdef INET6
10456 	ip6_out = NULL;
10457 #endif
10458 	offset_out = 0;
10459 
10460 	switch (iph->ip_v) {
10461 	case IPVERSION:
10462 		iph_out = mtod(mout, struct ip *);
10463 
10464 		/* Fill in the IP header for the ABORT */
10465 		iph_out->ip_v = IPVERSION;
10466 		iph_out->ip_hl = (sizeof(struct ip) / 4);
10467 		iph_out->ip_tos = (u_char)0;
10468 		iph_out->ip_id = 0;
10469 		iph_out->ip_off = 0;
10470 		iph_out->ip_ttl = MAXTTL;
10471 		if (port) {
10472 			iph_out->ip_p = IPPROTO_UDP;
10473 		} else {
10474 			iph_out->ip_p = IPPROTO_SCTP;
10475 		}
10476 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10477 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10478 
10479 		/* let IP layer calculate this */
10480 		iph_out->ip_sum = 0;
10481 		offset_out += sizeof(*iph_out);
10482 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10483 		    (caddr_t)iph_out + offset_out);
10484 		break;
10485 #ifdef INET6
10486 	case IPV6_VERSION >> 4:
10487 		ip6 = (struct ip6_hdr *)iph;
10488 		ip6_out = mtod(mout, struct ip6_hdr *);
10489 
10490 		/* Fill in the IPv6 header for the ABORT */
10491 		ip6_out->ip6_flow = ip6->ip6_flow;
10492 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
10493 		if (port) {
10494 			ip6_out->ip6_nxt = IPPROTO_UDP;
10495 		} else {
10496 			ip6_out->ip6_nxt = IPPROTO_SCTP;
10497 		}
10498 		ip6_out->ip6_src = ip6->ip6_dst;
10499 		ip6_out->ip6_dst = ip6->ip6_src;
10500 		/*
10501 		 * ?? The old code had both the iph len + payload, I think
10502 		 * this is wrong and would never have worked
10503 		 */
10504 		ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10505 		offset_out += sizeof(*ip6_out);
10506 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10507 		    (caddr_t)ip6_out + offset_out);
10508 		break;
10509 #endif				/* INET6 */
10510 	default:
10511 		/* Currently not supported. */
10512 		sctp_m_freem(mout);
10513 		return;
10514 	}
10515 	if (port) {
10516 		udp = (struct udphdr *)comp_cp;
10517 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10518 		udp->uh_dport = port;
10519 		udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
10520 		udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
10521 		offset_out += sizeof(struct udphdr);
10522 		comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
10523 	}
10524 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10525 		/* no mbuf's */
10526 		sctp_m_freem(mout);
10527 		return;
10528 	}
10529 	/* Now copy in and fill in the ABORT tags etc. */
10530 	comp_cp->sh.src_port = sh->dest_port;
10531 	comp_cp->sh.dest_port = sh->src_port;
10532 	comp_cp->sh.checksum = 0;
10533 	comp_cp->sh.v_tag = sh->v_tag;
10534 	comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
10535 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10536 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10537 
10538 	if (iph_out != NULL) {
10539 		sctp_route_t ro;
10540 		int ret;
10541 		struct sctp_tcb *stcb = NULL;
10542 
10543 		mlen = SCTP_BUF_LEN(mout);
10544 		bzero(&ro, sizeof ro);
10545 		/* set IPv4 length */
10546 		iph_out->ip_len = mlen;
10547 #ifdef  SCTP_PACKET_LOGGING
10548 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10549 			sctp_packet_log(mout, mlen);
10550 #endif
10551 		if (port) {
10552 			comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
10553 			SCTP_STAT_INCR(sctps_sendswcrc);
10554 			SCTP_ENABLE_UDP_CSUM(mout);
10555 		} else {
10556 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
10557 			mout->m_pkthdr.csum_data = 0;
10558 			SCTP_STAT_INCR(sctps_sendhwcrc);
10559 		}
10560 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10561 		/* out it goes */
10562 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
10563 
10564 		/* Free the route if we got one back */
10565 		if (ro.ro_rt)
10566 			RTFREE(ro.ro_rt);
10567 	}
10568 #ifdef INET6
10569 	if (ip6_out != NULL) {
10570 		struct route_in6 ro;
10571 		int ret;
10572 		struct sctp_tcb *stcb = NULL;
10573 		struct ifnet *ifp = NULL;
10574 
10575 		bzero(&ro, sizeof(ro));
10576 		mlen = SCTP_BUF_LEN(mout);
10577 #ifdef  SCTP_PACKET_LOGGING
10578 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10579 			sctp_packet_log(mout, mlen);
10580 #endif
10581 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10582 		if (port) {
10583 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
10584 			    (stcb) &&
10585 			    (stcb->asoc.loopback_scope))) {
10586 				comp_cp->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
10587 				SCTP_STAT_INCR(sctps_sendswcrc);
10588 			} else {
10589 				SCTP_STAT_INCR(sctps_sendnocrc);
10590 			}
10591 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), mlen - sizeof(struct ip6_hdr))) == 0) {
10592 				udp->uh_sum = 0xffff;
10593 			}
10594 		} else {
10595 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
10596 			    (stcb) &&
10597 			    (stcb->asoc.loopback_scope))) {
10598 				mout->m_pkthdr.csum_flags = CSUM_SCTP;
10599 				mout->m_pkthdr.csum_data = 0;
10600 				SCTP_STAT_INCR(sctps_sendhwcrc);
10601 			} else {
10602 				SCTP_STAT_INCR(sctps_sendnocrc);
10603 			}
10604 		}
10605 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
10606 
10607 		/* Free the route if we got one back */
10608 		if (ro.ro_rt)
10609 			RTFREE(ro.ro_rt);
10610 	}
10611 #endif
10612 	SCTP_STAT_INCR(sctps_sendpackets);
10613 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10614 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10615 	return;
10616 
10617 }
10618 
10619 static struct sctp_nets *
10620 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
10621 {
10622 	struct sctp_nets *net, *hnet;
10623 	int ms_goneby, highest_ms, state_overide = 0;
10624 
10625 	(void)SCTP_GETTIME_TIMEVAL(now);
10626 	highest_ms = 0;
10627 	hnet = NULL;
10628 	SCTP_TCB_LOCK_ASSERT(stcb);
10629 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
10630 		if (
10631 		    ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
10632 		    (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
10633 		    ) {
10634 			/*
10635 			 * Skip this guy from consideration if HB is off AND
10636 			 * its confirmed
10637 			 */
10638 			continue;
10639 		}
10640 		if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
10641 			/* skip this dest net from consideration */
10642 			continue;
10643 		}
10644 		if (net->last_sent_time.tv_sec) {
10645 			/* Sent to so we subtract */
10646 			ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
10647 		} else
10648 			/* Never been sent to */
10649 			ms_goneby = 0x7fffffff;
10650 		/*-
10651 		 * When the address state is unconfirmed but still
10652 		 * considered reachable, we HB at a higher rate. Once it
10653 		 * goes confirmed OR reaches the "unreachable" state, thenw
10654 		 * we cut it back to HB at a more normal pace.
10655 		 */
10656 		if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
10657 			state_overide = 1;
10658 		} else {
10659 			state_overide = 0;
10660 		}
10661 
10662 		if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
10663 		    (ms_goneby > highest_ms)) {
10664 			highest_ms = ms_goneby;
10665 			hnet = net;
10666 		}
10667 	}
10668 	if (hnet &&
10669 	    ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
10670 		state_overide = 1;
10671 	} else {
10672 		state_overide = 0;
10673 	}
10674 
10675 	if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
10676 		/*-
10677 		 * Found the one with longest delay bounds OR it is
10678 		 * unconfirmed and still not marked unreachable.
10679 		 */
10680 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
10681 #ifdef SCTP_DEBUG
10682 		if (hnet) {
10683 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
10684 			    (struct sockaddr *)&hnet->ro._l_addr);
10685 		} else {
10686 			SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
10687 		}
10688 #endif
10689 		/* update the timer now */
10690 		hnet->last_sent_time = *now;
10691 		return (hnet);
10692 	}
10693 	/* Nothing to HB */
10694 	return (NULL);
10695 }
10696 
10697 int
10698 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
10699 {
10700 	struct sctp_tmit_chunk *chk;
10701 	struct sctp_nets *net;
10702 	struct sctp_heartbeat_chunk *hb;
10703 	struct timeval now;
10704 	struct sockaddr_in *sin;
10705 	struct sockaddr_in6 *sin6;
10706 
10707 	SCTP_TCB_LOCK_ASSERT(stcb);
10708 	if (user_req == 0) {
10709 		net = sctp_select_hb_destination(stcb, &now);
10710 		if (net == NULL) {
10711 			/*-
10712 			 * All our busy none to send to, just start the
10713 			 * timer again.
10714 			 */
10715 			if (stcb->asoc.state == 0) {
10716 				return (0);
10717 			}
10718 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
10719 			    stcb->sctp_ep,
10720 			    stcb,
10721 			    net);
10722 			return (0);
10723 		}
10724 	} else {
10725 		net = u_net;
10726 		if (net == NULL) {
10727 			return (0);
10728 		}
10729 		(void)SCTP_GETTIME_TIMEVAL(&now);
10730 	}
10731 	sin = (struct sockaddr_in *)&net->ro._l_addr;
10732 	if (sin->sin_family != AF_INET) {
10733 		if (sin->sin_family != AF_INET6) {
10734 			/* huh */
10735 			return (0);
10736 		}
10737 	}
10738 	sctp_alloc_a_chunk(stcb, chk);
10739 	if (chk == NULL) {
10740 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
10741 		return (0);
10742 	}
10743 	chk->copy_by_ref = 0;
10744 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
10745 	chk->rec.chunk_id.can_take_data = 1;
10746 	chk->asoc = &stcb->asoc;
10747 	chk->send_size = sizeof(struct sctp_heartbeat_chunk);
10748 
10749 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10750 	if (chk->data == NULL) {
10751 		sctp_free_a_chunk(stcb, chk);
10752 		return (0);
10753 	}
10754 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10755 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10756 	chk->sent = SCTP_DATAGRAM_UNSENT;
10757 	chk->snd_count = 0;
10758 	chk->whoTo = net;
10759 	atomic_add_int(&chk->whoTo->ref_count, 1);
10760 	/* Now we have a mbuf that we can fill in with the details */
10761 	hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
10762 	memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
10763 	/* fill out chunk header */
10764 	hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
10765 	hb->ch.chunk_flags = 0;
10766 	hb->ch.chunk_length = htons(chk->send_size);
10767 	/* Fill out hb parameter */
10768 	hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
10769 	hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
10770 	hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
10771 	hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
10772 	/* Did our user request this one, put it in */
10773 	hb->heartbeat.hb_info.user_req = user_req;
10774 	hb->heartbeat.hb_info.addr_family = sin->sin_family;
10775 	hb->heartbeat.hb_info.addr_len = sin->sin_len;
10776 	if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
10777 		/*
10778 		 * we only take from the entropy pool if the address is not
10779 		 * confirmed.
10780 		 */
10781 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10782 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10783 	} else {
10784 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
10785 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
10786 	}
10787 	if (sin->sin_family == AF_INET) {
10788 		memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
10789 	} else if (sin->sin_family == AF_INET6) {
10790 		/* We leave the scope the way it is in our lookup table. */
10791 		sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
10792 		memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
10793 	} else {
10794 		/* huh compiler bug */
10795 		return (0);
10796 	}
10797 
10798 	/*
10799 	 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
10800 	 * PF-heartbeats.  Because of this, threshold management is done by
10801 	 * the t3 timer handler, and does not need to be done upon the send
10802 	 * of a PF-heartbeat. If CMT PF is on and the destination to which a
10803 	 * heartbeat is being sent is in PF state, do NOT do threshold
10804 	 * management.
10805 	 */
10806 	if ((SCTP_BASE_SYSCTL(sctp_cmt_pf) == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
10807 		/* ok we have a destination that needs a beat */
10808 		/* lets do the theshold management Qiaobing style */
10809 		if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
10810 		    stcb->asoc.max_send_times)) {
10811 			/*-
10812 			 * we have lost the association, in a way this is
10813 			 * quite bad since we really are one less time since
10814 			 * we really did not send yet. This is the down side
10815 			 * to the Q's style as defined in the RFC and not my
10816 			 * alternate style defined in the RFC.
10817 			 */
10818 			if (chk->data != NULL) {
10819 				sctp_m_freem(chk->data);
10820 				chk->data = NULL;
10821 			}
10822 			/*
10823 			 * Here we do NOT use the macro since the
10824 			 * association is now gone.
10825 			 */
10826 			if (chk->whoTo) {
10827 				sctp_free_remote_addr(chk->whoTo);
10828 				chk->whoTo = NULL;
10829 			}
10830 			sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
10831 			return (-1);
10832 		}
10833 	}
10834 	net->hb_responded = 0;
10835 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10836 	stcb->asoc.ctrl_queue_cnt++;
10837 	SCTP_STAT_INCR(sctps_sendheartbeat);
10838 	/*-
10839 	 * Call directly med level routine to put out the chunk. It will
10840 	 * always tumble out control chunks aka HB but it may even tumble
10841 	 * out data too.
10842 	 */
10843 	return (1);
10844 }
10845 
10846 void
10847 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
10848     uint32_t high_tsn)
10849 {
10850 	struct sctp_association *asoc;
10851 	struct sctp_ecne_chunk *ecne;
10852 	struct sctp_tmit_chunk *chk;
10853 
10854 	asoc = &stcb->asoc;
10855 	SCTP_TCB_LOCK_ASSERT(stcb);
10856 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10857 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
10858 			/* found a previous ECN_ECHO update it if needed */
10859 			ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10860 			ecne->tsn = htonl(high_tsn);
10861 			return;
10862 		}
10863 	}
10864 	/* nope could not find one to update so we must build one */
10865 	sctp_alloc_a_chunk(stcb, chk);
10866 	if (chk == NULL) {
10867 		return;
10868 	}
10869 	chk->copy_by_ref = 0;
10870 	SCTP_STAT_INCR(sctps_sendecne);
10871 	chk->rec.chunk_id.id = SCTP_ECN_ECHO;
10872 	chk->rec.chunk_id.can_take_data = 0;
10873 	chk->asoc = &stcb->asoc;
10874 	chk->send_size = sizeof(struct sctp_ecne_chunk);
10875 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10876 	if (chk->data == NULL) {
10877 		sctp_free_a_chunk(stcb, chk);
10878 		return;
10879 	}
10880 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10881 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10882 	chk->sent = SCTP_DATAGRAM_UNSENT;
10883 	chk->snd_count = 0;
10884 	chk->whoTo = net;
10885 	atomic_add_int(&chk->whoTo->ref_count, 1);
10886 	stcb->asoc.ecn_echo_cnt_onq++;
10887 	ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10888 	ecne->ch.chunk_type = SCTP_ECN_ECHO;
10889 	ecne->ch.chunk_flags = 0;
10890 	ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
10891 	ecne->tsn = htonl(high_tsn);
10892 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10893 	asoc->ctrl_queue_cnt++;
10894 }
10895 
10896 void
10897 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
10898     struct mbuf *m, int iphlen, int bad_crc)
10899 {
10900 	struct sctp_association *asoc;
10901 	struct sctp_pktdrop_chunk *drp;
10902 	struct sctp_tmit_chunk *chk;
10903 	uint8_t *datap;
10904 	int len;
10905 	int was_trunc = 0;
10906 	struct ip *iph;
10907 
10908 #ifdef INET6
10909 	struct ip6_hdr *ip6h;
10910 
10911 #endif
10912 	int fullsz = 0, extra = 0;
10913 	long spc;
10914 	int offset;
10915 	struct sctp_chunkhdr *ch, chunk_buf;
10916 	unsigned int chk_length;
10917 
10918 	if (!stcb) {
10919 		return;
10920 	}
10921 	asoc = &stcb->asoc;
10922 	SCTP_TCB_LOCK_ASSERT(stcb);
10923 	if (asoc->peer_supports_pktdrop == 0) {
10924 		/*-
10925 		 * peer must declare support before I send one.
10926 		 */
10927 		return;
10928 	}
10929 	if (stcb->sctp_socket == NULL) {
10930 		return;
10931 	}
10932 	sctp_alloc_a_chunk(stcb, chk);
10933 	if (chk == NULL) {
10934 		return;
10935 	}
10936 	chk->copy_by_ref = 0;
10937 	iph = mtod(m, struct ip *);
10938 	if (iph == NULL) {
10939 		sctp_free_a_chunk(stcb, chk);
10940 		return;
10941 	}
10942 	switch (iph->ip_v) {
10943 	case IPVERSION:
10944 		/* IPv4 */
10945 		len = chk->send_size = iph->ip_len;
10946 		break;
10947 #ifdef INET6
10948 	case IPV6_VERSION >> 4:
10949 		/* IPv6 */
10950 		ip6h = mtod(m, struct ip6_hdr *);
10951 		len = chk->send_size = htons(ip6h->ip6_plen);
10952 		break;
10953 #endif
10954 	default:
10955 		return;
10956 	}
10957 	/* Validate that we do not have an ABORT in here. */
10958 	offset = iphlen + sizeof(struct sctphdr);
10959 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10960 	    sizeof(*ch), (uint8_t *) & chunk_buf);
10961 	while (ch != NULL) {
10962 		chk_length = ntohs(ch->chunk_length);
10963 		if (chk_length < sizeof(*ch)) {
10964 			/* break to abort land */
10965 			break;
10966 		}
10967 		switch (ch->chunk_type) {
10968 		case SCTP_PACKET_DROPPED:
10969 		case SCTP_ABORT_ASSOCIATION:
10970 			/*-
10971 			 * we don't respond with an PKT-DROP to an ABORT
10972 			 * or PKT-DROP
10973 			 */
10974 			sctp_free_a_chunk(stcb, chk);
10975 			return;
10976 		default:
10977 			break;
10978 		}
10979 		offset += SCTP_SIZE32(chk_length);
10980 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10981 		    sizeof(*ch), (uint8_t *) & chunk_buf);
10982 	}
10983 
10984 	if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
10985 	    min(stcb->asoc.smallest_mtu, MCLBYTES)) {
10986 		/*
10987 		 * only send 1 mtu worth, trim off the excess on the end.
10988 		 */
10989 		fullsz = len - extra;
10990 		len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
10991 		was_trunc = 1;
10992 	}
10993 	chk->asoc = &stcb->asoc;
10994 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10995 	if (chk->data == NULL) {
10996 jump_out:
10997 		sctp_free_a_chunk(stcb, chk);
10998 		return;
10999 	}
11000 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11001 	drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11002 	if (drp == NULL) {
11003 		sctp_m_freem(chk->data);
11004 		chk->data = NULL;
11005 		goto jump_out;
11006 	}
11007 	chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11008 	    sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11009 	chk->book_size_scale = 0;
11010 	if (was_trunc) {
11011 		drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11012 		drp->trunc_len = htons(fullsz);
11013 		/*
11014 		 * Len is already adjusted to size minus overhead above take
11015 		 * out the pkt_drop chunk itself from it.
11016 		 */
11017 		chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
11018 		len = chk->send_size;
11019 	} else {
11020 		/* no truncation needed */
11021 		drp->ch.chunk_flags = 0;
11022 		drp->trunc_len = htons(0);
11023 	}
11024 	if (bad_crc) {
11025 		drp->ch.chunk_flags |= SCTP_BADCRC;
11026 	}
11027 	chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11028 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11029 	chk->sent = SCTP_DATAGRAM_UNSENT;
11030 	chk->snd_count = 0;
11031 	if (net) {
11032 		/* we should hit here */
11033 		chk->whoTo = net;
11034 	} else {
11035 		chk->whoTo = asoc->primary_destination;
11036 	}
11037 	atomic_add_int(&chk->whoTo->ref_count, 1);
11038 	chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11039 	chk->rec.chunk_id.can_take_data = 1;
11040 	drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11041 	drp->ch.chunk_length = htons(chk->send_size);
11042 	spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11043 	if (spc < 0) {
11044 		spc = 0;
11045 	}
11046 	drp->bottle_bw = htonl(spc);
11047 	if (asoc->my_rwnd) {
11048 		drp->current_onq = htonl(asoc->size_on_reasm_queue +
11049 		    asoc->size_on_all_streams +
11050 		    asoc->my_rwnd_control_len +
11051 		    stcb->sctp_socket->so_rcv.sb_cc);
11052 	} else {
11053 		/*-
11054 		 * If my rwnd is 0, possibly from mbuf depletion as well as
11055 		 * space used, tell the peer there is NO space aka onq == bw
11056 		 */
11057 		drp->current_onq = htonl(spc);
11058 	}
11059 	drp->reserved = 0;
11060 	datap = drp->data;
11061 	m_copydata(m, iphlen, len, (caddr_t)datap);
11062 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11063 	asoc->ctrl_queue_cnt++;
11064 }
11065 
11066 void
11067 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
11068 {
11069 	struct sctp_association *asoc;
11070 	struct sctp_cwr_chunk *cwr;
11071 	struct sctp_tmit_chunk *chk;
11072 
11073 	asoc = &stcb->asoc;
11074 	SCTP_TCB_LOCK_ASSERT(stcb);
11075 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11076 		if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
11077 			/* found a previous ECN_CWR update it if needed */
11078 			cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11079 			if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
11080 			    MAX_TSN)) {
11081 				cwr->tsn = htonl(high_tsn);
11082 			}
11083 			return;
11084 		}
11085 	}
11086 	/* nope could not find one to update so we must build one */
11087 	sctp_alloc_a_chunk(stcb, chk);
11088 	if (chk == NULL) {
11089 		return;
11090 	}
11091 	chk->copy_by_ref = 0;
11092 	chk->rec.chunk_id.id = SCTP_ECN_CWR;
11093 	chk->rec.chunk_id.can_take_data = 1;
11094 	chk->asoc = &stcb->asoc;
11095 	chk->send_size = sizeof(struct sctp_cwr_chunk);
11096 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11097 	if (chk->data == NULL) {
11098 		sctp_free_a_chunk(stcb, chk);
11099 		return;
11100 	}
11101 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11102 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11103 	chk->sent = SCTP_DATAGRAM_UNSENT;
11104 	chk->snd_count = 0;
11105 	chk->whoTo = net;
11106 	atomic_add_int(&chk->whoTo->ref_count, 1);
11107 	cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11108 	cwr->ch.chunk_type = SCTP_ECN_CWR;
11109 	cwr->ch.chunk_flags = 0;
11110 	cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11111 	cwr->tsn = htonl(high_tsn);
11112 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11113 	asoc->ctrl_queue_cnt++;
11114 }
11115 
11116 void
11117 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
11118     int number_entries, uint16_t * list,
11119     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11120 {
11121 	int len, old_len, i;
11122 	struct sctp_stream_reset_out_request *req_out;
11123 	struct sctp_chunkhdr *ch;
11124 
11125 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11126 
11127 
11128 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11129 
11130 	/* get to new offset for the param. */
11131 	req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11132 	/* now how long will this param be? */
11133 	len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11134 	req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11135 	req_out->ph.param_length = htons(len);
11136 	req_out->request_seq = htonl(seq);
11137 	req_out->response_seq = htonl(resp_seq);
11138 	req_out->send_reset_at_tsn = htonl(last_sent);
11139 	if (number_entries) {
11140 		for (i = 0; i < number_entries; i++) {
11141 			req_out->list_of_streams[i] = htons(list[i]);
11142 		}
11143 	}
11144 	if (SCTP_SIZE32(len) > len) {
11145 		/*-
11146 		 * Need to worry about the pad we may end up adding to the
11147 		 * end. This is easy since the struct is either aligned to 4
11148 		 * bytes or 2 bytes off.
11149 		 */
11150 		req_out->list_of_streams[number_entries] = 0;
11151 	}
11152 	/* now fix the chunk length */
11153 	ch->chunk_length = htons(len + old_len);
11154 	chk->book_size = len + old_len;
11155 	chk->book_size_scale = 0;
11156 	chk->send_size = SCTP_SIZE32(chk->book_size);
11157 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11158 	return;
11159 }
11160 
11161 
11162 void
11163 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11164     int number_entries, uint16_t * list,
11165     uint32_t seq)
11166 {
11167 	int len, old_len, i;
11168 	struct sctp_stream_reset_in_request *req_in;
11169 	struct sctp_chunkhdr *ch;
11170 
11171 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11172 
11173 
11174 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11175 
11176 	/* get to new offset for the param. */
11177 	req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11178 	/* now how long will this param be? */
11179 	len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11180 	req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11181 	req_in->ph.param_length = htons(len);
11182 	req_in->request_seq = htonl(seq);
11183 	if (number_entries) {
11184 		for (i = 0; i < number_entries; i++) {
11185 			req_in->list_of_streams[i] = htons(list[i]);
11186 		}
11187 	}
11188 	if (SCTP_SIZE32(len) > len) {
11189 		/*-
11190 		 * Need to worry about the pad we may end up adding to the
11191 		 * end. This is easy since the struct is either aligned to 4
11192 		 * bytes or 2 bytes off.
11193 		 */
11194 		req_in->list_of_streams[number_entries] = 0;
11195 	}
11196 	/* now fix the chunk length */
11197 	ch->chunk_length = htons(len + old_len);
11198 	chk->book_size = len + old_len;
11199 	chk->book_size_scale = 0;
11200 	chk->send_size = SCTP_SIZE32(chk->book_size);
11201 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11202 	return;
11203 }
11204 
11205 
11206 void
11207 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11208     uint32_t seq)
11209 {
11210 	int len, old_len;
11211 	struct sctp_stream_reset_tsn_request *req_tsn;
11212 	struct sctp_chunkhdr *ch;
11213 
11214 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11215 
11216 
11217 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11218 
11219 	/* get to new offset for the param. */
11220 	req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11221 	/* now how long will this param be? */
11222 	len = sizeof(struct sctp_stream_reset_tsn_request);
11223 	req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11224 	req_tsn->ph.param_length = htons(len);
11225 	req_tsn->request_seq = htonl(seq);
11226 
11227 	/* now fix the chunk length */
11228 	ch->chunk_length = htons(len + old_len);
11229 	chk->send_size = len + old_len;
11230 	chk->book_size = SCTP_SIZE32(chk->send_size);
11231 	chk->book_size_scale = 0;
11232 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11233 	return;
11234 }
11235 
11236 void
11237 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11238     uint32_t resp_seq, uint32_t result)
11239 {
11240 	int len, old_len;
11241 	struct sctp_stream_reset_response *resp;
11242 	struct sctp_chunkhdr *ch;
11243 
11244 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11245 
11246 
11247 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11248 
11249 	/* get to new offset for the param. */
11250 	resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11251 	/* now how long will this param be? */
11252 	len = sizeof(struct sctp_stream_reset_response);
11253 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11254 	resp->ph.param_length = htons(len);
11255 	resp->response_seq = htonl(resp_seq);
11256 	resp->result = ntohl(result);
11257 
11258 	/* now fix the chunk length */
11259 	ch->chunk_length = htons(len + old_len);
11260 	chk->book_size = len + old_len;
11261 	chk->book_size_scale = 0;
11262 	chk->send_size = SCTP_SIZE32(chk->book_size);
11263 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11264 	return;
11265 
11266 }
11267 
11268 
11269 void
11270 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11271     uint32_t resp_seq, uint32_t result,
11272     uint32_t send_una, uint32_t recv_next)
11273 {
11274 	int len, old_len;
11275 	struct sctp_stream_reset_response_tsn *resp;
11276 	struct sctp_chunkhdr *ch;
11277 
11278 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11279 
11280 
11281 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11282 
11283 	/* get to new offset for the param. */
11284 	resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11285 	/* now how long will this param be? */
11286 	len = sizeof(struct sctp_stream_reset_response_tsn);
11287 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11288 	resp->ph.param_length = htons(len);
11289 	resp->response_seq = htonl(resp_seq);
11290 	resp->result = htonl(result);
11291 	resp->senders_next_tsn = htonl(send_una);
11292 	resp->receivers_next_tsn = htonl(recv_next);
11293 
11294 	/* now fix the chunk length */
11295 	ch->chunk_length = htons(len + old_len);
11296 	chk->book_size = len + old_len;
11297 	chk->send_size = SCTP_SIZE32(chk->book_size);
11298 	chk->book_size_scale = 0;
11299 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11300 	return;
11301 }
11302 
11303 static void
11304 sctp_add_a_stream(struct sctp_tmit_chunk *chk,
11305     uint32_t seq,
11306     uint16_t adding)
11307 {
11308 	int len, old_len;
11309 	struct sctp_chunkhdr *ch;
11310 	struct sctp_stream_reset_add_strm *addstr;
11311 
11312 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11313 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11314 
11315 	/* get to new offset for the param. */
11316 	addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11317 	/* now how long will this param be? */
11318 	len = sizeof(struct sctp_stream_reset_add_strm);
11319 
11320 	/* Fill it out. */
11321 	addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_STREAMS);
11322 	addstr->ph.param_length = htons(len);
11323 	addstr->request_seq = htonl(seq);
11324 	addstr->number_of_streams = htons(adding);
11325 	addstr->reserved = 0;
11326 
11327 	/* now fix the chunk length */
11328 	ch->chunk_length = htons(len + old_len);
11329 	chk->send_size = len + old_len;
11330 	chk->book_size = SCTP_SIZE32(chk->send_size);
11331 	chk->book_size_scale = 0;
11332 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11333 	return;
11334 }
11335 
11336 int
11337 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11338     int number_entries, uint16_t * list,
11339     uint8_t send_out_req,
11340     uint32_t resp_seq,
11341     uint8_t send_in_req,
11342     uint8_t send_tsn_req,
11343     uint8_t add_stream,
11344     uint16_t adding
11345 )
11346 {
11347 
11348 	struct sctp_association *asoc;
11349 	struct sctp_tmit_chunk *chk;
11350 	struct sctp_chunkhdr *ch;
11351 	uint32_t seq;
11352 
11353 	asoc = &stcb->asoc;
11354 	if (asoc->stream_reset_outstanding) {
11355 		/*-
11356 		 * Already one pending, must get ACK back to clear the flag.
11357 		 */
11358 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11359 		return (EBUSY);
11360 	}
11361 	if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
11362 	    (add_stream == 0)) {
11363 		/* nothing to do */
11364 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11365 		return (EINVAL);
11366 	}
11367 	if (send_tsn_req && (send_out_req || send_in_req)) {
11368 		/* error, can't do that */
11369 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11370 		return (EINVAL);
11371 	}
11372 	sctp_alloc_a_chunk(stcb, chk);
11373 	if (chk == NULL) {
11374 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11375 		return (ENOMEM);
11376 	}
11377 	chk->copy_by_ref = 0;
11378 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11379 	chk->rec.chunk_id.can_take_data = 0;
11380 	chk->asoc = &stcb->asoc;
11381 	chk->book_size = sizeof(struct sctp_chunkhdr);
11382 	chk->send_size = SCTP_SIZE32(chk->book_size);
11383 	chk->book_size_scale = 0;
11384 
11385 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11386 	if (chk->data == NULL) {
11387 		sctp_free_a_chunk(stcb, chk);
11388 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11389 		return (ENOMEM);
11390 	}
11391 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11392 
11393 	/* setup chunk parameters */
11394 	chk->sent = SCTP_DATAGRAM_UNSENT;
11395 	chk->snd_count = 0;
11396 	chk->whoTo = asoc->primary_destination;
11397 	atomic_add_int(&chk->whoTo->ref_count, 1);
11398 
11399 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11400 	ch->chunk_type = SCTP_STREAM_RESET;
11401 	ch->chunk_flags = 0;
11402 	ch->chunk_length = htons(chk->book_size);
11403 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11404 
11405 	seq = stcb->asoc.str_reset_seq_out;
11406 	if (send_out_req) {
11407 		sctp_add_stream_reset_out(chk, number_entries, list,
11408 		    seq, resp_seq, (stcb->asoc.sending_seq - 1));
11409 		asoc->stream_reset_out_is_outstanding = 1;
11410 		seq++;
11411 		asoc->stream_reset_outstanding++;
11412 	}
11413 	if (add_stream) {
11414 		sctp_add_a_stream(chk, seq, adding);
11415 		seq++;
11416 		asoc->stream_reset_outstanding++;
11417 	}
11418 	if (send_in_req) {
11419 		sctp_add_stream_reset_in(chk, number_entries, list, seq);
11420 		asoc->stream_reset_outstanding++;
11421 	}
11422 	if (send_tsn_req) {
11423 		sctp_add_stream_reset_tsn(chk, seq);
11424 		asoc->stream_reset_outstanding++;
11425 	}
11426 	asoc->str_reset = chk;
11427 
11428 	/* insert the chunk for sending */
11429 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11430 	    chk,
11431 	    sctp_next);
11432 	asoc->ctrl_queue_cnt++;
11433 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11434 	return (0);
11435 }
11436 
11437 void
11438 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11439     struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11440 {
11441 	/*-
11442 	 * Formulate the abort message, and send it back down.
11443 	 */
11444 	struct mbuf *o_pak;
11445 	struct mbuf *mout;
11446 	struct sctp_abort_msg *abm;
11447 	struct ip *iph, *iph_out;
11448 	struct udphdr *udp;
11449 
11450 #ifdef INET6
11451 	struct ip6_hdr *ip6, *ip6_out;
11452 
11453 #endif
11454 	int iphlen_out, len;
11455 
11456 	/* don't respond to ABORT with ABORT */
11457 	if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11458 		if (err_cause)
11459 			sctp_m_freem(err_cause);
11460 		return;
11461 	}
11462 	iph = mtod(m, struct ip *);
11463 	switch (iph->ip_v) {
11464 	case IPVERSION:
11465 		len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11466 		break;
11467 #ifdef INET6
11468 	case IPV6_VERSION >> 4:
11469 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11470 		break;
11471 #endif
11472 	default:
11473 		if (err_cause) {
11474 			sctp_m_freem(err_cause);
11475 		}
11476 		return;
11477 	}
11478 	if (port) {
11479 		len += sizeof(struct udphdr);
11480 	}
11481 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11482 	if (mout == NULL) {
11483 		if (err_cause) {
11484 			sctp_m_freem(err_cause);
11485 		}
11486 		return;
11487 	}
11488 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
11489 	SCTP_BUF_LEN(mout) = len;
11490 	SCTP_BUF_NEXT(mout) = err_cause;
11491 	iph_out = NULL;
11492 #ifdef INET6
11493 	ip6_out = NULL;
11494 #endif
11495 	switch (iph->ip_v) {
11496 	case IPVERSION:
11497 		iph_out = mtod(mout, struct ip *);
11498 
11499 		/* Fill in the IP header for the ABORT */
11500 		iph_out->ip_v = IPVERSION;
11501 		iph_out->ip_hl = (sizeof(struct ip) / 4);
11502 		iph_out->ip_tos = (u_char)0;
11503 		iph_out->ip_id = 0;
11504 		iph_out->ip_off = 0;
11505 		iph_out->ip_ttl = MAXTTL;
11506 		if (port) {
11507 			iph_out->ip_p = IPPROTO_UDP;
11508 		} else {
11509 			iph_out->ip_p = IPPROTO_SCTP;
11510 		}
11511 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11512 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11513 		/* let IP layer calculate this */
11514 		iph_out->ip_sum = 0;
11515 
11516 		iphlen_out = sizeof(*iph_out);
11517 		abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
11518 		break;
11519 #ifdef INET6
11520 	case IPV6_VERSION >> 4:
11521 		ip6 = (struct ip6_hdr *)iph;
11522 		ip6_out = mtod(mout, struct ip6_hdr *);
11523 
11524 		/* Fill in the IP6 header for the ABORT */
11525 		ip6_out->ip6_flow = ip6->ip6_flow;
11526 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11527 		if (port) {
11528 			ip6_out->ip6_nxt = IPPROTO_UDP;
11529 		} else {
11530 			ip6_out->ip6_nxt = IPPROTO_SCTP;
11531 		}
11532 		ip6_out->ip6_src = ip6->ip6_dst;
11533 		ip6_out->ip6_dst = ip6->ip6_src;
11534 
11535 		iphlen_out = sizeof(*ip6_out);
11536 		abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
11537 		break;
11538 #endif				/* INET6 */
11539 	default:
11540 		/* Currently not supported */
11541 		sctp_m_freem(mout);
11542 		return;
11543 	}
11544 
11545 	udp = (struct udphdr *)abm;
11546 	if (port) {
11547 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11548 		udp->uh_dport = port;
11549 		/* set udp->uh_ulen later */
11550 		udp->uh_sum = 0;
11551 		iphlen_out += sizeof(struct udphdr);
11552 		abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
11553 	}
11554 	abm->sh.src_port = sh->dest_port;
11555 	abm->sh.dest_port = sh->src_port;
11556 	abm->sh.checksum = 0;
11557 	if (vtag == 0) {
11558 		abm->sh.v_tag = sh->v_tag;
11559 		abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
11560 	} else {
11561 		abm->sh.v_tag = htonl(vtag);
11562 		abm->msg.ch.chunk_flags = 0;
11563 	}
11564 	abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11565 
11566 	if (err_cause) {
11567 		struct mbuf *m_tmp = err_cause;
11568 		int err_len = 0;
11569 
11570 		/* get length of the err_cause chain */
11571 		while (m_tmp != NULL) {
11572 			err_len += SCTP_BUF_LEN(m_tmp);
11573 			m_tmp = SCTP_BUF_NEXT(m_tmp);
11574 		}
11575 		len = SCTP_BUF_LEN(mout) + err_len;
11576 		if (err_len % 4) {
11577 			/* need pad at end of chunk */
11578 			uint32_t cpthis = 0;
11579 			int padlen;
11580 
11581 			padlen = 4 - (len % 4);
11582 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11583 			len += padlen;
11584 		}
11585 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
11586 	} else {
11587 		len = SCTP_BUF_LEN(mout);
11588 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
11589 	}
11590 
11591 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11592 		/* no mbuf's */
11593 		sctp_m_freem(mout);
11594 		return;
11595 	}
11596 	if (iph_out != NULL) {
11597 		sctp_route_t ro;
11598 		struct sctp_tcb *stcb = NULL;
11599 		int ret;
11600 
11601 		/* zap the stack pointer to the route */
11602 		bzero(&ro, sizeof ro);
11603 		if (port) {
11604 			udp->uh_ulen = htons(len - sizeof(struct ip));
11605 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11606 		}
11607 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
11608 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
11609 		/* set IPv4 length */
11610 		iph_out->ip_len = len;
11611 		/* out it goes */
11612 #ifdef  SCTP_PACKET_LOGGING
11613 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11614 			sctp_packet_log(mout, len);
11615 #endif
11616 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11617 		if (port) {
11618 			abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
11619 			SCTP_STAT_INCR(sctps_sendswcrc);
11620 			SCTP_ENABLE_UDP_CSUM(o_pak);
11621 		} else {
11622 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
11623 			mout->m_pkthdr.csum_data = 0;
11624 			SCTP_STAT_INCR(sctps_sendhwcrc);
11625 		}
11626 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
11627 
11628 		/* Free the route if we got one back */
11629 		if (ro.ro_rt)
11630 			RTFREE(ro.ro_rt);
11631 	}
11632 #ifdef INET6
11633 	if (ip6_out != NULL) {
11634 		struct route_in6 ro;
11635 		int ret;
11636 		struct sctp_tcb *stcb = NULL;
11637 		struct ifnet *ifp = NULL;
11638 
11639 		/* zap the stack pointer to the route */
11640 		bzero(&ro, sizeof(ro));
11641 		if (port) {
11642 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11643 		}
11644 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
11645 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
11646 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
11647 #ifdef  SCTP_PACKET_LOGGING
11648 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11649 			sctp_packet_log(mout, len);
11650 #endif
11651 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11652 		if (port) {
11653 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
11654 			    (stcb) &&
11655 			    (stcb->asoc.loopback_scope))) {
11656 				abm->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11657 				SCTP_STAT_INCR(sctps_sendswcrc);
11658 			} else {
11659 				SCTP_STAT_INCR(sctps_sendnocrc);
11660 			}
11661 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11662 				udp->uh_sum = 0xffff;
11663 			}
11664 		} else {
11665 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
11666 			    (stcb) &&
11667 			    (stcb->asoc.loopback_scope))) {
11668 				mout->m_pkthdr.csum_flags = CSUM_SCTP;
11669 				mout->m_pkthdr.csum_data = 0;
11670 				SCTP_STAT_INCR(sctps_sendhwcrc);
11671 			} else {
11672 				SCTP_STAT_INCR(sctps_sendnocrc);
11673 			}
11674 		}
11675 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11676 
11677 		/* Free the route if we got one back */
11678 		if (ro.ro_rt)
11679 			RTFREE(ro.ro_rt);
11680 	}
11681 #endif
11682 	SCTP_STAT_INCR(sctps_sendpackets);
11683 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11684 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11685 }
11686 
11687 void
11688 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
11689     uint32_t vrf_id, uint16_t port)
11690 {
11691 	struct mbuf *o_pak;
11692 	struct sctphdr *sh, *sh_out;
11693 	struct sctp_chunkhdr *ch;
11694 	struct ip *iph, *iph_out;
11695 	struct udphdr *udp = NULL;
11696 	struct mbuf *mout;
11697 
11698 #ifdef INET6
11699 	struct ip6_hdr *ip6, *ip6_out;
11700 
11701 #endif
11702 	int iphlen_out, len;
11703 
11704 	iph = mtod(m, struct ip *);
11705 	sh = (struct sctphdr *)((caddr_t)iph + iphlen);
11706 	switch (iph->ip_v) {
11707 	case IPVERSION:
11708 		len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11709 		break;
11710 #ifdef INET6
11711 	case IPV6_VERSION >> 4:
11712 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11713 		break;
11714 #endif
11715 	default:
11716 		if (scm) {
11717 			sctp_m_freem(scm);
11718 		}
11719 		return;
11720 	}
11721 	if (port) {
11722 		len += sizeof(struct udphdr);
11723 	}
11724 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11725 	if (mout == NULL) {
11726 		if (scm) {
11727 			sctp_m_freem(scm);
11728 		}
11729 		return;
11730 	}
11731 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
11732 	SCTP_BUF_LEN(mout) = len;
11733 	SCTP_BUF_NEXT(mout) = scm;
11734 	iph_out = NULL;
11735 #ifdef INET6
11736 	ip6_out = NULL;
11737 #endif
11738 	switch (iph->ip_v) {
11739 	case IPVERSION:
11740 		iph_out = mtod(mout, struct ip *);
11741 
11742 		/* Fill in the IP header for the ABORT */
11743 		iph_out->ip_v = IPVERSION;
11744 		iph_out->ip_hl = (sizeof(struct ip) / 4);
11745 		iph_out->ip_tos = (u_char)0;
11746 		iph_out->ip_id = 0;
11747 		iph_out->ip_off = 0;
11748 		iph_out->ip_ttl = MAXTTL;
11749 		if (port) {
11750 			iph_out->ip_p = IPPROTO_UDP;
11751 		} else {
11752 			iph_out->ip_p = IPPROTO_SCTP;
11753 		}
11754 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11755 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11756 		/* let IP layer calculate this */
11757 		iph_out->ip_sum = 0;
11758 
11759 		iphlen_out = sizeof(struct ip);
11760 		sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
11761 		break;
11762 #ifdef INET6
11763 	case IPV6_VERSION >> 4:
11764 		ip6 = (struct ip6_hdr *)iph;
11765 		ip6_out = mtod(mout, struct ip6_hdr *);
11766 
11767 		/* Fill in the IP6 header for the ABORT */
11768 		ip6_out->ip6_flow = ip6->ip6_flow;
11769 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11770 		if (port) {
11771 			ip6_out->ip6_nxt = IPPROTO_UDP;
11772 		} else {
11773 			ip6_out->ip6_nxt = IPPROTO_SCTP;
11774 		}
11775 		ip6_out->ip6_src = ip6->ip6_dst;
11776 		ip6_out->ip6_dst = ip6->ip6_src;
11777 
11778 		iphlen_out = sizeof(struct ip6_hdr);
11779 		sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
11780 		break;
11781 #endif				/* INET6 */
11782 	default:
11783 		/* Currently not supported */
11784 		sctp_m_freem(mout);
11785 		return;
11786 	}
11787 
11788 	udp = (struct udphdr *)sh_out;
11789 	if (port) {
11790 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11791 		udp->uh_dport = port;
11792 		/* set udp->uh_ulen later */
11793 		udp->uh_sum = 0;
11794 		iphlen_out += sizeof(struct udphdr);
11795 		sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
11796 	}
11797 	sh_out->src_port = sh->dest_port;
11798 	sh_out->dest_port = sh->src_port;
11799 	sh_out->v_tag = vtag;
11800 	sh_out->checksum = 0;
11801 
11802 	ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
11803 	ch->chunk_type = SCTP_OPERATION_ERROR;
11804 	ch->chunk_flags = 0;
11805 
11806 	if (scm) {
11807 		struct mbuf *m_tmp = scm;
11808 		int cause_len = 0;
11809 
11810 		/* get length of the err_cause chain */
11811 		while (m_tmp != NULL) {
11812 			cause_len += SCTP_BUF_LEN(m_tmp);
11813 			m_tmp = SCTP_BUF_NEXT(m_tmp);
11814 		}
11815 		len = SCTP_BUF_LEN(mout) + cause_len;
11816 		if (cause_len % 4) {
11817 			/* need pad at end of chunk */
11818 			uint32_t cpthis = 0;
11819 			int padlen;
11820 
11821 			padlen = 4 - (len % 4);
11822 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11823 			len += padlen;
11824 		}
11825 		ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
11826 	} else {
11827 		len = SCTP_BUF_LEN(mout);
11828 		ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
11829 	}
11830 
11831 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11832 		/* no mbuf's */
11833 		sctp_m_freem(mout);
11834 		return;
11835 	}
11836 	if (iph_out != NULL) {
11837 		sctp_route_t ro;
11838 		struct sctp_tcb *stcb = NULL;
11839 		int ret;
11840 
11841 		/* zap the stack pointer to the route */
11842 		bzero(&ro, sizeof ro);
11843 		if (port) {
11844 			udp->uh_ulen = htons(len - sizeof(struct ip));
11845 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11846 		}
11847 		/* set IPv4 length */
11848 		iph_out->ip_len = len;
11849 		/* out it goes */
11850 #ifdef  SCTP_PACKET_LOGGING
11851 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11852 			sctp_packet_log(mout, len);
11853 #endif
11854 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11855 		if (port) {
11856 			sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
11857 			SCTP_STAT_INCR(sctps_sendswcrc);
11858 			SCTP_ENABLE_UDP_CSUM(o_pak);
11859 		} else {
11860 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
11861 			mout->m_pkthdr.csum_data = 0;
11862 			SCTP_STAT_INCR(sctps_sendhwcrc);
11863 		}
11864 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
11865 
11866 		/* Free the route if we got one back */
11867 		if (ro.ro_rt)
11868 			RTFREE(ro.ro_rt);
11869 	}
11870 #ifdef INET6
11871 	if (ip6_out != NULL) {
11872 		struct route_in6 ro;
11873 		int ret;
11874 		struct sctp_tcb *stcb = NULL;
11875 		struct ifnet *ifp = NULL;
11876 
11877 		/* zap the stack pointer to the route */
11878 		bzero(&ro, sizeof(ro));
11879 		if (port) {
11880 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11881 		}
11882 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
11883 #ifdef  SCTP_PACKET_LOGGING
11884 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11885 			sctp_packet_log(mout, len);
11886 #endif
11887 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11888 		if (port) {
11889 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
11890 			    (stcb) &&
11891 			    (stcb->asoc.loopback_scope))) {
11892 				sh_out->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11893 				SCTP_STAT_INCR(sctps_sendswcrc);
11894 			} else {
11895 				SCTP_STAT_INCR(sctps_sendnocrc);
11896 			}
11897 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11898 				udp->uh_sum = 0xffff;
11899 			}
11900 		} else {
11901 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
11902 			    (stcb) &&
11903 			    (stcb->asoc.loopback_scope))) {
11904 				mout->m_pkthdr.csum_flags = CSUM_SCTP;
11905 				mout->m_pkthdr.csum_data = 0;
11906 				SCTP_STAT_INCR(sctps_sendhwcrc);
11907 			} else {
11908 				SCTP_STAT_INCR(sctps_sendnocrc);
11909 			}
11910 		}
11911 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11912 
11913 		/* Free the route if we got one back */
11914 		if (ro.ro_rt)
11915 			RTFREE(ro.ro_rt);
11916 	}
11917 #endif
11918 	SCTP_STAT_INCR(sctps_sendpackets);
11919 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11920 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11921 }
11922 
11923 static struct mbuf *
11924 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
11925     struct uio *uio,
11926     struct sctp_sndrcvinfo *srcv,
11927     int max_send_len,
11928     int user_marks_eor,
11929     int *error,
11930     uint32_t * sndout,
11931     struct mbuf **new_tail)
11932 {
11933 	struct mbuf *m;
11934 
11935 	m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
11936 	    (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
11937 	if (m == NULL) {
11938 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11939 		*error = ENOMEM;
11940 	} else {
11941 		*sndout = m_length(m, NULL);
11942 		*new_tail = m_last(m);
11943 	}
11944 	return (m);
11945 }
11946 
11947 static int
11948 sctp_copy_one(struct sctp_stream_queue_pending *sp,
11949     struct uio *uio,
11950     int resv_upfront)
11951 {
11952 	int left;
11953 
11954 	left = sp->length;
11955 	sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
11956 	    resv_upfront, 0);
11957 	if (sp->data == NULL) {
11958 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11959 		return (ENOMEM);
11960 	}
11961 	sp->tail_mbuf = m_last(sp->data);
11962 	return (0);
11963 }
11964 
11965 
11966 
11967 static struct sctp_stream_queue_pending *
11968 sctp_copy_it_in(struct sctp_tcb *stcb,
11969     struct sctp_association *asoc,
11970     struct sctp_sndrcvinfo *srcv,
11971     struct uio *uio,
11972     struct sctp_nets *net,
11973     int max_send_len,
11974     int user_marks_eor,
11975     int *error,
11976     int non_blocking)
11977 {
11978 	/*-
11979 	 * This routine must be very careful in its work. Protocol
11980 	 * processing is up and running so care must be taken to spl...()
11981 	 * when you need to do something that may effect the stcb/asoc. The
11982 	 * sb is locked however. When data is copied the protocol processing
11983 	 * should be enabled since this is a slower operation...
11984 	 */
11985 	struct sctp_stream_queue_pending *sp = NULL;
11986 	int resv_in_first;
11987 
11988 	*error = 0;
11989 	/* Now can we send this? */
11990 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
11991 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
11992 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
11993 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
11994 		/* got data while shutting down */
11995 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
11996 		*error = ECONNRESET;
11997 		goto out_now;
11998 	}
11999 	sctp_alloc_a_strmoq(stcb, sp);
12000 	if (sp == NULL) {
12001 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12002 		*error = ENOMEM;
12003 		goto out_now;
12004 	}
12005 	sp->act_flags = 0;
12006 	sp->sender_all_done = 0;
12007 	sp->sinfo_flags = srcv->sinfo_flags;
12008 	sp->timetolive = srcv->sinfo_timetolive;
12009 	sp->ppid = srcv->sinfo_ppid;
12010 	sp->context = srcv->sinfo_context;
12011 	sp->strseq = 0;
12012 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12013 
12014 	sp->stream = srcv->sinfo_stream;
12015 	sp->length = min(uio->uio_resid, max_send_len);
12016 	if ((sp->length == (uint32_t) uio->uio_resid) &&
12017 	    ((user_marks_eor == 0) ||
12018 	    (srcv->sinfo_flags & SCTP_EOF) ||
12019 	    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12020 		sp->msg_is_complete = 1;
12021 	} else {
12022 		sp->msg_is_complete = 0;
12023 	}
12024 	sp->sender_all_done = 0;
12025 	sp->some_taken = 0;
12026 	sp->put_last_out = 0;
12027 	resv_in_first = sizeof(struct sctp_data_chunk);
12028 	sp->data = sp->tail_mbuf = NULL;
12029 	if (sp->length == 0) {
12030 		*error = 0;
12031 		goto skip_copy;
12032 	}
12033 	sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12034 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12035 		sctp_auth_key_acquire(stcb, stcb->asoc.authinfo.active_keyid);
12036 		sp->holds_key_ref = 1;
12037 	}
12038 	*error = sctp_copy_one(sp, uio, resv_in_first);
12039 skip_copy:
12040 	if (*error) {
12041 		sctp_free_a_strmoq(stcb, sp);
12042 		sp = NULL;
12043 	} else {
12044 		if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12045 			sp->net = net;
12046 		} else {
12047 			sp->net = asoc->primary_destination;
12048 		}
12049 		atomic_add_int(&sp->net->ref_count, 1);
12050 		sctp_set_prsctp_policy(sp);
12051 	}
12052 out_now:
12053 	return (sp);
12054 }
12055 
12056 
12057 int
12058 sctp_sosend(struct socket *so,
12059     struct sockaddr *addr,
12060     struct uio *uio,
12061     struct mbuf *top,
12062     struct mbuf *control,
12063     int flags,
12064     struct thread *p
12065 )
12066 {
12067 	struct sctp_inpcb *inp;
12068 	int error, use_rcvinfo = 0;
12069 	struct sctp_sndrcvinfo srcv;
12070 	struct sockaddr *addr_to_use;
12071 
12072 #ifdef INET6
12073 	struct sockaddr_in sin;
12074 
12075 #endif
12076 
12077 	inp = (struct sctp_inpcb *)so->so_pcb;
12078 	if (control) {
12079 		/* process cmsg snd/rcv info (maybe a assoc-id) */
12080 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
12081 		    sizeof(srcv))) {
12082 			/* got one */
12083 			use_rcvinfo = 1;
12084 		}
12085 	}
12086 	addr_to_use = addr;
12087 #if defined(INET6)  && !defined(__Userspace__)	/* TODO port in6_sin6_2_sin */
12088 	if ((addr) && (addr->sa_family == AF_INET6)) {
12089 		struct sockaddr_in6 *sin6;
12090 
12091 		sin6 = (struct sockaddr_in6 *)addr;
12092 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12093 			in6_sin6_2_sin(&sin, sin6);
12094 			addr_to_use = (struct sockaddr *)&sin;
12095 		}
12096 	}
12097 #endif
12098 	error = sctp_lower_sosend(so, addr_to_use, uio, top,
12099 	    control,
12100 	    flags,
12101 	    use_rcvinfo, &srcv
12102 	    ,p
12103 	    );
12104 	return (error);
12105 }
12106 
12107 
12108 int
12109 sctp_lower_sosend(struct socket *so,
12110     struct sockaddr *addr,
12111     struct uio *uio,
12112     struct mbuf *i_pak,
12113     struct mbuf *control,
12114     int flags,
12115     int use_rcvinfo,
12116     struct sctp_sndrcvinfo *srcv
12117     ,
12118     struct thread *p
12119 )
12120 {
12121 	unsigned int sndlen = 0, max_len;
12122 	int error, len;
12123 	struct mbuf *top = NULL;
12124 	int queue_only = 0, queue_only_for_init = 0;
12125 	int free_cnt_applied = 0;
12126 	int un_sent = 0;
12127 	int now_filled = 0;
12128 	unsigned int inqueue_bytes = 0;
12129 	struct sctp_block_entry be;
12130 	struct sctp_inpcb *inp;
12131 	struct sctp_tcb *stcb = NULL;
12132 	struct timeval now;
12133 	struct sctp_nets *net;
12134 	struct sctp_association *asoc;
12135 	struct sctp_inpcb *t_inp;
12136 	int user_marks_eor;
12137 	int create_lock_applied = 0;
12138 	int nagle_applies = 0;
12139 	int some_on_control = 0;
12140 	int got_all_of_the_send = 0;
12141 	int hold_tcblock = 0;
12142 	int non_blocking = 0;
12143 	int temp_flags = 0;
12144 	uint32_t local_add_more, local_soresv = 0;
12145 
12146 	error = 0;
12147 	net = NULL;
12148 	stcb = NULL;
12149 	asoc = NULL;
12150 
12151 	t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12152 	if (inp == NULL) {
12153 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12154 		error = EINVAL;
12155 		if (i_pak) {
12156 			SCTP_RELEASE_PKT(i_pak);
12157 		}
12158 		return (error);
12159 	}
12160 	if ((uio == NULL) && (i_pak == NULL)) {
12161 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12162 		return (EINVAL);
12163 	}
12164 	user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12165 	atomic_add_int(&inp->total_sends, 1);
12166 	if (uio) {
12167 		if (uio->uio_resid < 0) {
12168 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12169 			return (EINVAL);
12170 		}
12171 		sndlen = uio->uio_resid;
12172 	} else {
12173 		top = SCTP_HEADER_TO_CHAIN(i_pak);
12174 		sndlen = SCTP_HEADER_LEN(i_pak);
12175 	}
12176 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
12177 	    addr,
12178 	    sndlen);
12179 	/*-
12180 	 * Pre-screen address, if one is given the sin-len
12181 	 * must be set correctly!
12182 	 */
12183 	if (addr) {
12184 		if ((addr->sa_family == AF_INET) &&
12185 		    (addr->sa_len != sizeof(struct sockaddr_in))) {
12186 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12187 			error = EINVAL;
12188 			goto out_unlocked;
12189 		} else if ((addr->sa_family == AF_INET6) &&
12190 		    (addr->sa_len != sizeof(struct sockaddr_in6))) {
12191 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12192 			error = EINVAL;
12193 			goto out_unlocked;
12194 		}
12195 	}
12196 	hold_tcblock = 0;
12197 
12198 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12199 	    (inp->sctp_socket->so_qlimit)) {
12200 		/* The listener can NOT send */
12201 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12202 		error = ENOTCONN;
12203 		goto out_unlocked;
12204 	}
12205 	if ((use_rcvinfo) && srcv) {
12206 		if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
12207 		    PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
12208 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12209 			error = EINVAL;
12210 			goto out_unlocked;
12211 		}
12212 		if (srcv->sinfo_flags)
12213 			SCTP_STAT_INCR(sctps_sends_with_flags);
12214 
12215 		if (srcv->sinfo_flags & SCTP_SENDALL) {
12216 			/* its a sendall */
12217 			error = sctp_sendall(inp, uio, top, srcv);
12218 			top = NULL;
12219 			goto out_unlocked;
12220 		}
12221 	}
12222 	/* now we must find the assoc */
12223 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12224 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12225 		SCTP_INP_RLOCK(inp);
12226 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
12227 		if (stcb == NULL) {
12228 			SCTP_INP_RUNLOCK(inp);
12229 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12230 			error = ENOTCONN;
12231 			goto out_unlocked;
12232 		}
12233 		SCTP_TCB_LOCK(stcb);
12234 		hold_tcblock = 1;
12235 		SCTP_INP_RUNLOCK(inp);
12236 		if (addr) {
12237 			/* Must locate the net structure if addr given */
12238 			net = sctp_findnet(stcb, addr);
12239 			if (net) {
12240 				/* validate port was 0 or correct */
12241 				struct sockaddr_in *sin;
12242 
12243 				sin = (struct sockaddr_in *)addr;
12244 				if ((sin->sin_port != 0) &&
12245 				    (sin->sin_port != stcb->rport)) {
12246 					net = NULL;
12247 				}
12248 			}
12249 			temp_flags |= SCTP_ADDR_OVER;
12250 		} else
12251 			net = stcb->asoc.primary_destination;
12252 		if (addr && (net == NULL)) {
12253 			/* Could not find address, was it legal */
12254 			if (addr->sa_family == AF_INET) {
12255 				struct sockaddr_in *sin;
12256 
12257 				sin = (struct sockaddr_in *)addr;
12258 				if (sin->sin_addr.s_addr == 0) {
12259 					if ((sin->sin_port == 0) ||
12260 					    (sin->sin_port == stcb->rport)) {
12261 						net = stcb->asoc.primary_destination;
12262 					}
12263 				}
12264 			} else {
12265 				struct sockaddr_in6 *sin6;
12266 
12267 				sin6 = (struct sockaddr_in6 *)addr;
12268 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
12269 					if ((sin6->sin6_port == 0) ||
12270 					    (sin6->sin6_port == stcb->rport)) {
12271 						net = stcb->asoc.primary_destination;
12272 					}
12273 				}
12274 			}
12275 		}
12276 		if (net == NULL) {
12277 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12278 			error = EINVAL;
12279 			goto out_unlocked;
12280 		}
12281 	} else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
12282 		stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
12283 		if (stcb) {
12284 			if (addr)
12285 				/*
12286 				 * Must locate the net structure if addr
12287 				 * given
12288 				 */
12289 				net = sctp_findnet(stcb, addr);
12290 			else
12291 				net = stcb->asoc.primary_destination;
12292 			if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
12293 			    ((net == NULL) || (addr == NULL))) {
12294 				struct sockaddr_in *sin;
12295 
12296 				if (addr == NULL) {
12297 					SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12298 					error = EINVAL;
12299 					goto out_unlocked;
12300 				}
12301 				sin = (struct sockaddr_in *)addr;
12302 				/* Validate port is 0 or correct */
12303 				if ((sin->sin_port != 0) &&
12304 				    (sin->sin_port != stcb->rport)) {
12305 					net = NULL;
12306 				}
12307 			}
12308 		}
12309 		hold_tcblock = 0;
12310 	} else if (addr) {
12311 		/*-
12312 		 * Since we did not use findep we must
12313 		 * increment it, and if we don't find a tcb
12314 		 * decrement it.
12315 		 */
12316 		SCTP_INP_WLOCK(inp);
12317 		SCTP_INP_INCR_REF(inp);
12318 		SCTP_INP_WUNLOCK(inp);
12319 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12320 		if (stcb == NULL) {
12321 			SCTP_INP_WLOCK(inp);
12322 			SCTP_INP_DECR_REF(inp);
12323 			SCTP_INP_WUNLOCK(inp);
12324 		} else {
12325 			hold_tcblock = 1;
12326 		}
12327 	}
12328 	if ((stcb == NULL) && (addr)) {
12329 		/* Possible implicit send? */
12330 		SCTP_ASOC_CREATE_LOCK(inp);
12331 		create_lock_applied = 1;
12332 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12333 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12334 			/* Should I really unlock ? */
12335 			SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12336 			error = EINVAL;
12337 			goto out_unlocked;
12338 
12339 		}
12340 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12341 		    (addr->sa_family == AF_INET6)) {
12342 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12343 			error = EINVAL;
12344 			goto out_unlocked;
12345 		}
12346 		SCTP_INP_WLOCK(inp);
12347 		SCTP_INP_INCR_REF(inp);
12348 		SCTP_INP_WUNLOCK(inp);
12349 		/* With the lock applied look again */
12350 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12351 		if (stcb == NULL) {
12352 			SCTP_INP_WLOCK(inp);
12353 			SCTP_INP_DECR_REF(inp);
12354 			SCTP_INP_WUNLOCK(inp);
12355 		} else {
12356 			hold_tcblock = 1;
12357 		}
12358 		if (t_inp != inp) {
12359 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12360 			error = ENOTCONN;
12361 			goto out_unlocked;
12362 		}
12363 	}
12364 	if (stcb == NULL) {
12365 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
12366 		    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12367 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12368 			error = ENOTCONN;
12369 			goto out_unlocked;
12370 		}
12371 		if (addr == NULL) {
12372 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12373 			error = ENOENT;
12374 			goto out_unlocked;
12375 		} else {
12376 			/*
12377 			 * UDP style, we must go ahead and start the INIT
12378 			 * process
12379 			 */
12380 			uint32_t vrf_id;
12381 
12382 			if ((use_rcvinfo) && (srcv) &&
12383 			    ((srcv->sinfo_flags & SCTP_ABORT) ||
12384 			    ((srcv->sinfo_flags & SCTP_EOF) &&
12385 			    (sndlen == 0)))) {
12386 				/*-
12387 				 * User asks to abort a non-existant assoc,
12388 				 * or EOF a non-existant assoc with no data
12389 				 */
12390 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12391 				error = ENOENT;
12392 				goto out_unlocked;
12393 			}
12394 			/* get an asoc/stcb struct */
12395 			vrf_id = inp->def_vrf_id;
12396 #ifdef INVARIANTS
12397 			if (create_lock_applied == 0) {
12398 				panic("Error, should hold create lock and I don't?");
12399 			}
12400 #endif
12401 			stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
12402 			    p
12403 			    );
12404 			if (stcb == NULL) {
12405 				/* Error is setup for us in the call */
12406 				goto out_unlocked;
12407 			}
12408 			if (create_lock_applied) {
12409 				SCTP_ASOC_CREATE_UNLOCK(inp);
12410 				create_lock_applied = 0;
12411 			} else {
12412 				SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12413 			}
12414 			/*
12415 			 * Turn on queue only flag to prevent data from
12416 			 * being sent
12417 			 */
12418 			queue_only = 1;
12419 			asoc = &stcb->asoc;
12420 			SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12421 			(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12422 
12423 			/* initialize authentication params for the assoc */
12424 			sctp_initialize_auth_params(inp, stcb);
12425 
12426 			if (control) {
12427 				/*
12428 				 * see if a init structure exists in cmsg
12429 				 * headers
12430 				 */
12431 				struct sctp_initmsg initm;
12432 				int i;
12433 
12434 				if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
12435 				    sizeof(initm))) {
12436 					/*
12437 					 * we have an INIT override of the
12438 					 * default
12439 					 */
12440 					if (initm.sinit_max_attempts)
12441 						asoc->max_init_times = initm.sinit_max_attempts;
12442 					if (initm.sinit_num_ostreams)
12443 						asoc->pre_open_streams = initm.sinit_num_ostreams;
12444 					if (initm.sinit_max_instreams)
12445 						asoc->max_inbound_streams = initm.sinit_max_instreams;
12446 					if (initm.sinit_max_init_timeo)
12447 						asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
12448 					if (asoc->streamoutcnt < asoc->pre_open_streams) {
12449 						struct sctp_stream_out *tmp_str;
12450 						int had_lock = 0;
12451 
12452 						/* Default is NOT correct */
12453 						SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
12454 						    asoc->streamoutcnt, asoc->pre_open_streams);
12455 						/*
12456 						 * What happens if this
12457 						 * fails? we panic ...
12458 						 */
12459 
12460 						if (hold_tcblock) {
12461 							had_lock = 1;
12462 							SCTP_TCB_UNLOCK(stcb);
12463 						}
12464 						SCTP_MALLOC(tmp_str,
12465 						    struct sctp_stream_out *,
12466 						    (asoc->pre_open_streams *
12467 						    sizeof(struct sctp_stream_out)),
12468 						    SCTP_M_STRMO);
12469 						if (had_lock) {
12470 							SCTP_TCB_LOCK(stcb);
12471 						}
12472 						if (tmp_str != NULL) {
12473 							SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
12474 							asoc->strmout = tmp_str;
12475 							asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
12476 						} else {
12477 							asoc->pre_open_streams = asoc->streamoutcnt;
12478 						}
12479 						for (i = 0; i < asoc->streamoutcnt; i++) {
12480 							/*-
12481 							 * inbound side must be set
12482 							 * to 0xffff, also NOTE when
12483 							 * we get the INIT-ACK back
12484 							 * (for INIT sender) we MUST
12485 							 * reduce the count
12486 							 * (streamoutcnt) but first
12487 							 * check if we sent to any
12488 							 * of the upper streams that
12489 							 * were dropped (if some
12490 							 * were). Those that were
12491 							 * dropped must be notified
12492 							 * to the upper layer as
12493 							 * failed to send.
12494 							 */
12495 							asoc->strmout[i].next_sequence_sent = 0x0;
12496 							TAILQ_INIT(&asoc->strmout[i].outqueue);
12497 							asoc->strmout[i].stream_no = i;
12498 							asoc->strmout[i].last_msg_incomplete = 0;
12499 							asoc->strmout[i].next_spoke.tqe_next = 0;
12500 							asoc->strmout[i].next_spoke.tqe_prev = 0;
12501 						}
12502 					}
12503 				}
12504 			}
12505 			hold_tcblock = 1;
12506 			/* out with the INIT */
12507 			queue_only_for_init = 1;
12508 			/*-
12509 			 * we may want to dig in after this call and adjust the MTU
12510 			 * value. It defaulted to 1500 (constant) but the ro
12511 			 * structure may now have an update and thus we may need to
12512 			 * change it BEFORE we append the message.
12513 			 */
12514 			net = stcb->asoc.primary_destination;
12515 			asoc = &stcb->asoc;
12516 		}
12517 	}
12518 	if ((SCTP_SO_IS_NBIO(so)
12519 	    || (flags & MSG_NBIO)
12520 	    )) {
12521 		non_blocking = 1;
12522 	}
12523 	asoc = &stcb->asoc;
12524 	atomic_add_int(&stcb->total_sends, 1);
12525 
12526 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12527 		if (sndlen > asoc->smallest_mtu) {
12528 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12529 			error = EMSGSIZE;
12530 			goto out_unlocked;
12531 		}
12532 	}
12533 	/* would we block? */
12534 	if (non_blocking) {
12535 		if (hold_tcblock == 0) {
12536 			SCTP_TCB_LOCK(stcb);
12537 			hold_tcblock = 1;
12538 		}
12539 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12540 		if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12541 		    (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12542 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
12543 			if (sndlen > SCTP_SB_LIMIT_SND(so))
12544 				error = EMSGSIZE;
12545 			else
12546 				error = EWOULDBLOCK;
12547 			goto out_unlocked;
12548 		}
12549 		stcb->asoc.sb_send_resv += sndlen;
12550 		SCTP_TCB_UNLOCK(stcb);
12551 		hold_tcblock = 0;
12552 	} else {
12553 		atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
12554 	}
12555 	local_soresv = sndlen;
12556 	/* Keep the stcb from being freed under our feet */
12557 	if (free_cnt_applied) {
12558 #ifdef INVARIANTS
12559 		panic("refcnt already incremented");
12560 #else
12561 		printf("refcnt:1 already incremented?\n");
12562 #endif
12563 	} else {
12564 		atomic_add_int(&stcb->asoc.refcnt, 1);
12565 		free_cnt_applied = 1;
12566 	}
12567 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12568 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12569 		error = ECONNRESET;
12570 		goto out_unlocked;
12571 	}
12572 	if (create_lock_applied) {
12573 		SCTP_ASOC_CREATE_UNLOCK(inp);
12574 		create_lock_applied = 0;
12575 	}
12576 	if (asoc->stream_reset_outstanding) {
12577 		/*
12578 		 * Can't queue any data while stream reset is underway.
12579 		 */
12580 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
12581 		error = EAGAIN;
12582 		goto out_unlocked;
12583 	}
12584 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12585 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12586 		queue_only = 1;
12587 	}
12588 	if ((use_rcvinfo == 0) || (srcv == NULL)) {
12589 		/* Grab the default stuff from the asoc */
12590 		srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
12591 	}
12592 	/* we are now done with all control */
12593 	if (control) {
12594 		sctp_m_freem(control);
12595 		control = NULL;
12596 	}
12597 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12598 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12599 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12600 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12601 		if ((use_rcvinfo) &&
12602 		    (srcv->sinfo_flags & SCTP_ABORT)) {
12603 			;
12604 		} else {
12605 			SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12606 			error = ECONNRESET;
12607 			goto out_unlocked;
12608 		}
12609 	}
12610 	/* Ok, we will attempt a msgsnd :> */
12611 	if (p) {
12612 		p->td_ru.ru_msgsnd++;
12613 	}
12614 	if (stcb) {
12615 		if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
12616 			net = stcb->asoc.primary_destination;
12617 		}
12618 	}
12619 	if (net == NULL) {
12620 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12621 		error = EINVAL;
12622 		goto out_unlocked;
12623 	}
12624 	if ((net->flight_size > net->cwnd) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
12625 		/*-
12626 		 * CMT: Added check for CMT above. net above is the primary
12627 		 * dest. If CMT is ON, sender should always attempt to send
12628 		 * with the output routine sctp_fill_outqueue() that loops
12629 		 * through all destination addresses. Therefore, if CMT is
12630 		 * ON, queue_only is NOT set to 1 here, so that
12631 		 * sctp_chunk_output() can be called below.
12632 		 */
12633 		queue_only = 1;
12634 	} else if (asoc->ifp_had_enobuf) {
12635 		SCTP_STAT_INCR(sctps_ifnomemqueued);
12636 		if (net->flight_size > (net->mtu * 2))
12637 			queue_only = 1;
12638 		asoc->ifp_had_enobuf = 0;
12639 	} else {
12640 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12641 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12642 	}
12643 	/* Are we aborting? */
12644 	if (srcv->sinfo_flags & SCTP_ABORT) {
12645 		struct mbuf *mm;
12646 		int tot_demand, tot_out = 0, max_out;
12647 
12648 		SCTP_STAT_INCR(sctps_sends_with_abort);
12649 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12650 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12651 			/* It has to be up before we abort */
12652 			/* how big is the user initiated abort? */
12653 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12654 			error = EINVAL;
12655 			goto out;
12656 		}
12657 		if (hold_tcblock) {
12658 			SCTP_TCB_UNLOCK(stcb);
12659 			hold_tcblock = 0;
12660 		}
12661 		if (top) {
12662 			struct mbuf *cntm = NULL;
12663 
12664 			mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
12665 			if (sndlen != 0) {
12666 				cntm = top;
12667 				while (cntm) {
12668 					tot_out += SCTP_BUF_LEN(cntm);
12669 					cntm = SCTP_BUF_NEXT(cntm);
12670 				}
12671 			}
12672 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12673 		} else {
12674 			/* Must fit in a MTU */
12675 			tot_out = sndlen;
12676 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12677 			if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12678 				/* To big */
12679 				SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12680 				error = EMSGSIZE;
12681 				goto out;
12682 			}
12683 			mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
12684 		}
12685 		if (mm == NULL) {
12686 			SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12687 			error = ENOMEM;
12688 			goto out;
12689 		}
12690 		max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12691 		max_out -= sizeof(struct sctp_abort_msg);
12692 		if (tot_out > max_out) {
12693 			tot_out = max_out;
12694 		}
12695 		if (mm) {
12696 			struct sctp_paramhdr *ph;
12697 
12698 			/* now move forward the data pointer */
12699 			ph = mtod(mm, struct sctp_paramhdr *);
12700 			ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12701 			ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
12702 			ph++;
12703 			SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
12704 			if (top == NULL) {
12705 				error = uiomove((caddr_t)ph, (int)tot_out, uio);
12706 				if (error) {
12707 					/*-
12708 					 * Here if we can't get his data we
12709 					 * still abort we just don't get to
12710 					 * send the users note :-0
12711 					 */
12712 					sctp_m_freem(mm);
12713 					mm = NULL;
12714 				}
12715 			} else {
12716 				if (sndlen != 0) {
12717 					SCTP_BUF_NEXT(mm) = top;
12718 				}
12719 			}
12720 		}
12721 		if (hold_tcblock == 0) {
12722 			SCTP_TCB_LOCK(stcb);
12723 			hold_tcblock = 1;
12724 		}
12725 		atomic_add_int(&stcb->asoc.refcnt, -1);
12726 		free_cnt_applied = 0;
12727 		/* release this lock, otherwise we hang on ourselves */
12728 		sctp_abort_an_association(stcb->sctp_ep, stcb,
12729 		    SCTP_RESPONSE_TO_USER_REQ,
12730 		    mm, SCTP_SO_LOCKED);
12731 		/* now relock the stcb so everything is sane */
12732 		hold_tcblock = 0;
12733 		stcb = NULL;
12734 		/*
12735 		 * In this case top is already chained to mm avoid double
12736 		 * free, since we free it below if top != NULL and driver
12737 		 * would free it after sending the packet out
12738 		 */
12739 		if (sndlen != 0) {
12740 			top = NULL;
12741 		}
12742 		goto out_unlocked;
12743 	}
12744 	/* Calculate the maximum we can send */
12745 	inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12746 	if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12747 		if (non_blocking) {
12748 			/* we already checked for non-blocking above. */
12749 			max_len = sndlen;
12750 		} else {
12751 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12752 		}
12753 	} else {
12754 		max_len = 0;
12755 	}
12756 	if (hold_tcblock) {
12757 		SCTP_TCB_UNLOCK(stcb);
12758 		hold_tcblock = 0;
12759 	}
12760 	/* Is the stream no. valid? */
12761 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
12762 		/* Invalid stream number */
12763 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12764 		error = EINVAL;
12765 		goto out_unlocked;
12766 	}
12767 	if (asoc->strmout == NULL) {
12768 		/* huh? software error */
12769 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12770 		error = EFAULT;
12771 		goto out_unlocked;
12772 	}
12773 	/* Unless E_EOR mode is on, we must make a send FIT in one call. */
12774 	if ((user_marks_eor == 0) &&
12775 	    (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
12776 		/* It will NEVER fit */
12777 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12778 		error = EMSGSIZE;
12779 		goto out_unlocked;
12780 	}
12781 	if ((uio == NULL) && user_marks_eor) {
12782 		/*-
12783 		 * We do not support eeor mode for
12784 		 * sending with mbuf chains (like sendfile).
12785 		 */
12786 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12787 		error = EINVAL;
12788 		goto out_unlocked;
12789 	}
12790 	if (user_marks_eor) {
12791 		local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
12792 	} else {
12793 		/*-
12794 		 * For non-eeor the whole message must fit in
12795 		 * the socket send buffer.
12796 		 */
12797 		local_add_more = sndlen;
12798 	}
12799 	len = 0;
12800 	if (non_blocking) {
12801 		goto skip_preblock;
12802 	}
12803 	if (((max_len <= local_add_more) &&
12804 	    (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
12805 	    (max_len == 0) ||
12806 	    ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12807 		/* No room right now ! */
12808 		SOCKBUF_LOCK(&so->so_snd);
12809 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12810 		while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
12811 		    ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12812 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
12813 			    (unsigned int)SCTP_SB_LIMIT_SND(so),
12814 			    inqueue_bytes,
12815 			    local_add_more,
12816 			    stcb->asoc.stream_queue_cnt,
12817 			    stcb->asoc.chunks_on_out_queue,
12818 			    SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
12819 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12820 				sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, so, asoc, sndlen);
12821 			}
12822 			be.error = 0;
12823 			stcb->block_entry = &be;
12824 			error = sbwait(&so->so_snd);
12825 			stcb->block_entry = NULL;
12826 			if (error || so->so_error || be.error) {
12827 				if (error == 0) {
12828 					if (so->so_error)
12829 						error = so->so_error;
12830 					if (be.error) {
12831 						error = be.error;
12832 					}
12833 				}
12834 				SOCKBUF_UNLOCK(&so->so_snd);
12835 				goto out_unlocked;
12836 			}
12837 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12838 				sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12839 				    so, asoc, stcb->asoc.total_output_queue_size);
12840 			}
12841 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12842 				goto out_unlocked;
12843 			}
12844 			inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12845 		}
12846 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12847 		if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12848 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12849 		} else {
12850 			max_len = 0;
12851 		}
12852 		SOCKBUF_UNLOCK(&so->so_snd);
12853 	}
12854 skip_preblock:
12855 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12856 		goto out_unlocked;
12857 	}
12858 	/*
12859 	 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
12860 	 * case NOTE: uio will be null when top/mbuf is passed
12861 	 */
12862 	if (sndlen == 0) {
12863 		if (srcv->sinfo_flags & SCTP_EOF) {
12864 			got_all_of_the_send = 1;
12865 			goto dataless_eof;
12866 		} else {
12867 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12868 			error = EINVAL;
12869 			goto out;
12870 		}
12871 	}
12872 	if (top == NULL) {
12873 		struct sctp_stream_queue_pending *sp;
12874 		struct sctp_stream_out *strm;
12875 		uint32_t sndout, initial_out;
12876 
12877 		initial_out = uio->uio_resid;
12878 
12879 		SCTP_TCB_SEND_LOCK(stcb);
12880 		if ((asoc->stream_locked) &&
12881 		    (asoc->stream_locked_on != srcv->sinfo_stream)) {
12882 			SCTP_TCB_SEND_UNLOCK(stcb);
12883 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12884 			error = EINVAL;
12885 			goto out;
12886 		}
12887 		SCTP_TCB_SEND_UNLOCK(stcb);
12888 
12889 		strm = &stcb->asoc.strmout[srcv->sinfo_stream];
12890 		if (strm->last_msg_incomplete == 0) {
12891 	do_a_copy_in:
12892 			sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
12893 			if ((sp == NULL) || (error)) {
12894 				goto out;
12895 			}
12896 			SCTP_TCB_SEND_LOCK(stcb);
12897 			if (sp->msg_is_complete) {
12898 				strm->last_msg_incomplete = 0;
12899 				asoc->stream_locked = 0;
12900 			} else {
12901 				/*
12902 				 * Just got locked to this guy in case of an
12903 				 * interrupt.
12904 				 */
12905 				strm->last_msg_incomplete = 1;
12906 				asoc->stream_locked = 1;
12907 				asoc->stream_locked_on = srcv->sinfo_stream;
12908 				sp->sender_all_done = 0;
12909 			}
12910 			sctp_snd_sb_alloc(stcb, sp->length);
12911 			atomic_add_int(&asoc->stream_queue_cnt, 1);
12912 			if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
12913 				sp->strseq = strm->next_sequence_sent;
12914 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
12915 					sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
12916 					    (uintptr_t) stcb, sp->length,
12917 					    (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
12918 				}
12919 				strm->next_sequence_sent++;
12920 			} else {
12921 				SCTP_STAT_INCR(sctps_sends_with_unord);
12922 			}
12923 			TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
12924 			if ((strm->next_spoke.tqe_next == NULL) &&
12925 			    (strm->next_spoke.tqe_prev == NULL)) {
12926 				/* Not on wheel, insert */
12927 				sctp_insert_on_wheel(stcb, asoc, strm, 1);
12928 			}
12929 			SCTP_TCB_SEND_UNLOCK(stcb);
12930 		} else {
12931 			SCTP_TCB_SEND_LOCK(stcb);
12932 			sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
12933 			SCTP_TCB_SEND_UNLOCK(stcb);
12934 			if (sp == NULL) {
12935 				/* ???? Huh ??? last msg is gone */
12936 #ifdef INVARIANTS
12937 				panic("Warning: Last msg marked incomplete, yet nothing left?");
12938 #else
12939 				SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
12940 				strm->last_msg_incomplete = 0;
12941 #endif
12942 				goto do_a_copy_in;
12943 
12944 			}
12945 		}
12946 		while (uio->uio_resid > 0) {
12947 			/* How much room do we have? */
12948 			struct mbuf *new_tail, *mm;
12949 
12950 			if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12951 				max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
12952 			else
12953 				max_len = 0;
12954 
12955 			if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
12956 			    (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
12957 			    (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
12958 				sndout = 0;
12959 				new_tail = NULL;
12960 				if (hold_tcblock) {
12961 					SCTP_TCB_UNLOCK(stcb);
12962 					hold_tcblock = 0;
12963 				}
12964 				mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
12965 				if ((mm == NULL) || error) {
12966 					if (mm) {
12967 						sctp_m_freem(mm);
12968 					}
12969 					goto out;
12970 				}
12971 				/* Update the mbuf and count */
12972 				SCTP_TCB_SEND_LOCK(stcb);
12973 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12974 					/*
12975 					 * we need to get out. Peer probably
12976 					 * aborted.
12977 					 */
12978 					sctp_m_freem(mm);
12979 					if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
12980 						SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12981 						error = ECONNRESET;
12982 					}
12983 					SCTP_TCB_SEND_UNLOCK(stcb);
12984 					goto out;
12985 				}
12986 				if (sp->tail_mbuf) {
12987 					/* tack it to the end */
12988 					SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
12989 					sp->tail_mbuf = new_tail;
12990 				} else {
12991 					/* A stolen mbuf */
12992 					sp->data = mm;
12993 					sp->tail_mbuf = new_tail;
12994 				}
12995 				sctp_snd_sb_alloc(stcb, sndout);
12996 				atomic_add_int(&sp->length, sndout);
12997 				len += sndout;
12998 
12999 				/* Did we reach EOR? */
13000 				if ((uio->uio_resid == 0) &&
13001 				    ((user_marks_eor == 0) ||
13002 				    (srcv->sinfo_flags & SCTP_EOF) ||
13003 				    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
13004 					sp->msg_is_complete = 1;
13005 				} else {
13006 					sp->msg_is_complete = 0;
13007 				}
13008 				SCTP_TCB_SEND_UNLOCK(stcb);
13009 			}
13010 			if (uio->uio_resid == 0) {
13011 				/* got it all? */
13012 				continue;
13013 			}
13014 			/* PR-SCTP? */
13015 			if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
13016 				/*
13017 				 * This is ugly but we must assure locking
13018 				 * order
13019 				 */
13020 				if (hold_tcblock == 0) {
13021 					SCTP_TCB_LOCK(stcb);
13022 					hold_tcblock = 1;
13023 				}
13024 				sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
13025 				inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13026 				if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13027 					max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13028 				else
13029 					max_len = 0;
13030 				if (max_len > 0) {
13031 					continue;
13032 				}
13033 				SCTP_TCB_UNLOCK(stcb);
13034 				hold_tcblock = 0;
13035 			}
13036 			/* wait for space now */
13037 			if (non_blocking) {
13038 				/* Non-blocking io in place out */
13039 				goto skip_out_eof;
13040 			}
13041 			if ((net->flight_size > net->cwnd) &&
13042 			    (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
13043 				queue_only = 1;
13044 			} else if (asoc->ifp_had_enobuf) {
13045 				SCTP_STAT_INCR(sctps_ifnomemqueued);
13046 				if (net->flight_size > (net->mtu * 2)) {
13047 					queue_only = 1;
13048 				} else {
13049 					queue_only = 0;
13050 				}
13051 				asoc->ifp_had_enobuf = 0;
13052 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13053 				    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13054 			} else {
13055 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13056 				    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13057 				if (net->flight_size > net->cwnd) {
13058 					queue_only = 1;
13059 					SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13060 				} else {
13061 					queue_only = 0;
13062 				}
13063 			}
13064 			if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13065 			    (stcb->asoc.total_flight > 0) &&
13066 			    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13067 			    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13068 
13069 				/*-
13070 				 * Ok, Nagle is set on and we have data outstanding.
13071 				 * Don't send anything and let SACKs drive out the
13072 				 * data unless wen have a "full" segment to send.
13073 				 */
13074 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13075 					sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13076 				}
13077 				SCTP_STAT_INCR(sctps_naglequeued);
13078 				nagle_applies = 1;
13079 			} else {
13080 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13081 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13082 						sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13083 				}
13084 				SCTP_STAT_INCR(sctps_naglesent);
13085 				nagle_applies = 0;
13086 			}
13087 			/* What about the INIT, send it maybe */
13088 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13089 
13090 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13091 				    nagle_applies, un_sent);
13092 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13093 				    stcb->asoc.total_flight,
13094 				    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13095 			}
13096 			if (queue_only_for_init) {
13097 				if (hold_tcblock == 0) {
13098 					SCTP_TCB_LOCK(stcb);
13099 					hold_tcblock = 1;
13100 				}
13101 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13102 					/* a collision took us forward? */
13103 					queue_only_for_init = 0;
13104 					queue_only = 0;
13105 				} else {
13106 					sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13107 					SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
13108 					queue_only_for_init = 0;
13109 					queue_only = 1;
13110 				}
13111 			}
13112 			if ((queue_only == 0) && (nagle_applies == 0)) {
13113 				/*-
13114 				 * need to start chunk output
13115 				 * before blocking.. note that if
13116 				 * a lock is already applied, then
13117 				 * the input via the net is happening
13118 				 * and I don't need to start output :-D
13119 				 */
13120 				if (hold_tcblock == 0) {
13121 					if (SCTP_TCB_TRYLOCK(stcb)) {
13122 						hold_tcblock = 1;
13123 						sctp_chunk_output(inp,
13124 						    stcb,
13125 						    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13126 					}
13127 				} else {
13128 					sctp_chunk_output(inp,
13129 					    stcb,
13130 					    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13131 				}
13132 				if (hold_tcblock == 1) {
13133 					SCTP_TCB_UNLOCK(stcb);
13134 					hold_tcblock = 0;
13135 				}
13136 			}
13137 			SOCKBUF_LOCK(&so->so_snd);
13138 			/*-
13139 			 * This is a bit strange, but I think it will
13140 			 * work. The total_output_queue_size is locked and
13141 			 * protected by the TCB_LOCK, which we just released.
13142 			 * There is a race that can occur between releasing it
13143 			 * above, and me getting the socket lock, where sacks
13144 			 * come in but we have not put the SB_WAIT on the
13145 			 * so_snd buffer to get the wakeup. After the LOCK
13146 			 * is applied the sack_processing will also need to
13147 			 * LOCK the so->so_snd to do the actual sowwakeup(). So
13148 			 * once we have the socket buffer lock if we recheck the
13149 			 * size we KNOW we will get to sleep safely with the
13150 			 * wakeup flag in place.
13151 			 */
13152 			if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
13153 			    min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13154 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13155 					sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13156 					    so, asoc, uio->uio_resid);
13157 				}
13158 				be.error = 0;
13159 				stcb->block_entry = &be;
13160 				error = sbwait(&so->so_snd);
13161 				stcb->block_entry = NULL;
13162 
13163 				if (error || so->so_error || be.error) {
13164 					if (error == 0) {
13165 						if (so->so_error)
13166 							error = so->so_error;
13167 						if (be.error) {
13168 							error = be.error;
13169 						}
13170 					}
13171 					SOCKBUF_UNLOCK(&so->so_snd);
13172 					goto out_unlocked;
13173 				}
13174 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13175 					sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13176 					    so, asoc, stcb->asoc.total_output_queue_size);
13177 				}
13178 			}
13179 			SOCKBUF_UNLOCK(&so->so_snd);
13180 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13181 				goto out_unlocked;
13182 			}
13183 		}
13184 		SCTP_TCB_SEND_LOCK(stcb);
13185 		if (sp) {
13186 			if (sp->msg_is_complete == 0) {
13187 				strm->last_msg_incomplete = 1;
13188 				asoc->stream_locked = 1;
13189 				asoc->stream_locked_on = srcv->sinfo_stream;
13190 			} else {
13191 				sp->sender_all_done = 1;
13192 				strm->last_msg_incomplete = 0;
13193 				asoc->stream_locked = 0;
13194 			}
13195 		} else {
13196 			SCTP_PRINTF("Huh no sp TSNH?\n");
13197 			strm->last_msg_incomplete = 0;
13198 			asoc->stream_locked = 0;
13199 		}
13200 		SCTP_TCB_SEND_UNLOCK(stcb);
13201 		if (uio->uio_resid == 0) {
13202 			got_all_of_the_send = 1;
13203 		}
13204 	} else if (top) {
13205 		/* We send in a 0, since we do NOT have any locks */
13206 		error = sctp_msg_append(stcb, net, top, srcv, 0);
13207 		top = NULL;
13208 		if (srcv->sinfo_flags & SCTP_EOF) {
13209 			/*
13210 			 * This should only happen for Panda for the mbuf
13211 			 * send case, which does NOT yet support EEOR mode.
13212 			 * Thus, we can just set this flag to do the proper
13213 			 * EOF handling.
13214 			 */
13215 			got_all_of_the_send = 1;
13216 		}
13217 	}
13218 	if (error) {
13219 		goto out;
13220 	}
13221 dataless_eof:
13222 	/* EOF thing ? */
13223 	if ((srcv->sinfo_flags & SCTP_EOF) &&
13224 	    (got_all_of_the_send == 1) &&
13225 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
13226 		int cnt;
13227 
13228 		SCTP_STAT_INCR(sctps_sends_with_eof);
13229 		error = 0;
13230 		if (hold_tcblock == 0) {
13231 			SCTP_TCB_LOCK(stcb);
13232 			hold_tcblock = 1;
13233 		}
13234 		cnt = sctp_is_there_unsent_data(stcb);
13235 		if (TAILQ_EMPTY(&asoc->send_queue) &&
13236 		    TAILQ_EMPTY(&asoc->sent_queue) &&
13237 		    (cnt == 0)) {
13238 			if (asoc->locked_on_sending) {
13239 				goto abort_anyway;
13240 			}
13241 			/* there is nothing queued to send, so I'm done... */
13242 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13243 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13244 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13245 				/* only send SHUTDOWN the first time through */
13246 				sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
13247 				if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
13248 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13249 				}
13250 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
13251 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
13252 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13253 				    asoc->primary_destination);
13254 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13255 				    asoc->primary_destination);
13256 			}
13257 		} else {
13258 			/*-
13259 			 * we still got (or just got) data to send, so set
13260 			 * SHUTDOWN_PENDING
13261 			 */
13262 			/*-
13263 			 * XXX sockets draft says that SCTP_EOF should be
13264 			 * sent with no data.  currently, we will allow user
13265 			 * data to be sent first and move to
13266 			 * SHUTDOWN-PENDING
13267 			 */
13268 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13269 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13270 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13271 				if (hold_tcblock == 0) {
13272 					SCTP_TCB_LOCK(stcb);
13273 					hold_tcblock = 1;
13274 				}
13275 				if (asoc->locked_on_sending) {
13276 					/* Locked to send out the data */
13277 					struct sctp_stream_queue_pending *sp;
13278 
13279 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
13280 					if (sp) {
13281 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
13282 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
13283 					}
13284 				}
13285 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
13286 				if (TAILQ_EMPTY(&asoc->send_queue) &&
13287 				    TAILQ_EMPTY(&asoc->sent_queue) &&
13288 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13289 			abort_anyway:
13290 					if (free_cnt_applied) {
13291 						atomic_add_int(&stcb->asoc.refcnt, -1);
13292 						free_cnt_applied = 0;
13293 					}
13294 					sctp_abort_an_association(stcb->sctp_ep, stcb,
13295 					    SCTP_RESPONSE_TO_USER_REQ,
13296 					    NULL, SCTP_SO_LOCKED);
13297 					/*
13298 					 * now relock the stcb so everything
13299 					 * is sane
13300 					 */
13301 					hold_tcblock = 0;
13302 					stcb = NULL;
13303 					goto out;
13304 				}
13305 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13306 				    asoc->primary_destination);
13307 				sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13308 			}
13309 		}
13310 	}
13311 skip_out_eof:
13312 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
13313 		some_on_control = 1;
13314 	}
13315 	if ((net->flight_size > net->cwnd) &&
13316 	    (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
13317 		queue_only = 1;
13318 	} else if (asoc->ifp_had_enobuf) {
13319 		SCTP_STAT_INCR(sctps_ifnomemqueued);
13320 		if (net->flight_size > (net->mtu * 2)) {
13321 			queue_only = 1;
13322 		} else {
13323 			queue_only = 0;
13324 		}
13325 		asoc->ifp_had_enobuf = 0;
13326 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13327 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13328 	} else {
13329 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13330 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13331 		if (net->flight_size > net->cwnd) {
13332 			queue_only = 1;
13333 			SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13334 		} else {
13335 			queue_only = 0;
13336 		}
13337 	}
13338 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13339 	    (stcb->asoc.total_flight > 0) &&
13340 	    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13341 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13342 		/*-
13343 		 * Ok, Nagle is set on and we have data outstanding.
13344 		 * Don't send anything and let SACKs drive out the
13345 		 * data unless wen have a "full" segment to send.
13346 		 */
13347 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13348 			sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13349 		}
13350 		SCTP_STAT_INCR(sctps_naglequeued);
13351 		nagle_applies = 1;
13352 	} else {
13353 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13354 			if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13355 				sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13356 		}
13357 		SCTP_STAT_INCR(sctps_naglesent);
13358 		nagle_applies = 0;
13359 	}
13360 	if (queue_only_for_init) {
13361 		if (hold_tcblock == 0) {
13362 			SCTP_TCB_LOCK(stcb);
13363 			hold_tcblock = 1;
13364 		}
13365 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13366 			/* a collision took us forward? */
13367 			queue_only_for_init = 0;
13368 			queue_only = 0;
13369 		} else {
13370 			sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13371 			SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13372 			queue_only_for_init = 0;
13373 			queue_only = 1;
13374 		}
13375 	}
13376 	if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13377 		/* we can attempt to send too. */
13378 		if (hold_tcblock == 0) {
13379 			/*
13380 			 * If there is activity recv'ing sacks no need to
13381 			 * send
13382 			 */
13383 			if (SCTP_TCB_TRYLOCK(stcb)) {
13384 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13385 				hold_tcblock = 1;
13386 			}
13387 		} else {
13388 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13389 		}
13390 	} else if ((queue_only == 0) &&
13391 		    (stcb->asoc.peers_rwnd == 0) &&
13392 	    (stcb->asoc.total_flight == 0)) {
13393 		/* We get to have a probe outstanding */
13394 		if (hold_tcblock == 0) {
13395 			hold_tcblock = 1;
13396 			SCTP_TCB_LOCK(stcb);
13397 		}
13398 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13399 	} else if (some_on_control) {
13400 		int num_out, reason, frag_point;
13401 
13402 		/* Here we do control only */
13403 		if (hold_tcblock == 0) {
13404 			hold_tcblock = 1;
13405 			SCTP_TCB_LOCK(stcb);
13406 		}
13407 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13408 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13409 		    &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13410 	}
13411 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13412 	    queue_only, stcb->asoc.peers_rwnd, un_sent,
13413 	    stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13414 	    stcb->asoc.total_output_queue_size, error);
13415 
13416 out:
13417 out_unlocked:
13418 
13419 	if (local_soresv && stcb) {
13420 		atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13421 		local_soresv = 0;
13422 	}
13423 	if (create_lock_applied) {
13424 		SCTP_ASOC_CREATE_UNLOCK(inp);
13425 		create_lock_applied = 0;
13426 	}
13427 	if ((stcb) && hold_tcblock) {
13428 		SCTP_TCB_UNLOCK(stcb);
13429 	}
13430 	if (stcb && free_cnt_applied) {
13431 		atomic_add_int(&stcb->asoc.refcnt, -1);
13432 	}
13433 #ifdef INVARIANTS
13434 	if (stcb) {
13435 		if (mtx_owned(&stcb->tcb_mtx)) {
13436 			panic("Leaving with tcb mtx owned?");
13437 		}
13438 		if (mtx_owned(&stcb->tcb_send_mtx)) {
13439 			panic("Leaving with tcb send mtx owned?");
13440 		}
13441 	}
13442 #endif
13443 #ifdef INVARIANTS
13444 	if (inp) {
13445 		sctp_validate_no_locks(inp);
13446 	} else {
13447 		printf("Warning - inp is NULL so cant validate locks\n");
13448 	}
13449 #endif
13450 	if (top) {
13451 		sctp_m_freem(top);
13452 	}
13453 	if (control) {
13454 		sctp_m_freem(control);
13455 	}
13456 	return (error);
13457 }
13458 
13459 
13460 /*
13461  * generate an AUTHentication chunk, if required
13462  */
13463 struct mbuf *
13464 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13465     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13466     struct sctp_tcb *stcb, uint8_t chunk)
13467 {
13468 	struct mbuf *m_auth;
13469 	struct sctp_auth_chunk *auth;
13470 	int chunk_len;
13471 
13472 	if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13473 	    (stcb == NULL))
13474 		return (m);
13475 
13476 	/* sysctl disabled auth? */
13477 	if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13478 		return (m);
13479 
13480 	/* peer doesn't do auth... */
13481 	if (!stcb->asoc.peer_supports_auth) {
13482 		return (m);
13483 	}
13484 	/* does the requested chunk require auth? */
13485 	if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13486 		return (m);
13487 	}
13488 	m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13489 	if (m_auth == NULL) {
13490 		/* no mbuf's */
13491 		return (m);
13492 	}
13493 	/* reserve some space if this will be the first mbuf */
13494 	if (m == NULL)
13495 		SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13496 	/* fill in the AUTH chunk details */
13497 	auth = mtod(m_auth, struct sctp_auth_chunk *);
13498 	bzero(auth, sizeof(*auth));
13499 	auth->ch.chunk_type = SCTP_AUTHENTICATION;
13500 	auth->ch.chunk_flags = 0;
13501 	chunk_len = sizeof(*auth) +
13502 	    sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13503 	auth->ch.chunk_length = htons(chunk_len);
13504 	auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13505 	/* key id and hmac digest will be computed and filled in upon send */
13506 
13507 	/* save the offset where the auth was inserted into the chain */
13508 	if (m != NULL) {
13509 		struct mbuf *cn;
13510 
13511 		*offset = 0;
13512 		cn = m;
13513 		while (cn) {
13514 			*offset += SCTP_BUF_LEN(cn);
13515 			cn = SCTP_BUF_NEXT(cn);
13516 		}
13517 	} else
13518 		*offset = 0;
13519 
13520 	/* update length and return pointer to the auth chunk */
13521 	SCTP_BUF_LEN(m_auth) = chunk_len;
13522 	m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13523 	if (auth_ret != NULL)
13524 		*auth_ret = auth;
13525 
13526 	return (m);
13527 }
13528 
13529 #ifdef INET6
13530 int
13531 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13532 {
13533 	struct nd_prefix *pfx = NULL;
13534 	struct nd_pfxrouter *pfxrtr = NULL;
13535 	struct sockaddr_in6 gw6;
13536 
13537 	if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13538 		return (0);
13539 
13540 	/* get prefix entry of address */
13541 	LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13542 		if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13543 			continue;
13544 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13545 		    &src6->sin6_addr, &pfx->ndpr_mask))
13546 			break;
13547 	}
13548 	/* no prefix entry in the prefix list */
13549 	if (pfx == NULL) {
13550 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13551 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13552 		return (0);
13553 	}
13554 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13555 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13556 
13557 	/* search installed gateway from prefix entry */
13558 	for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
13559 	    pfxrtr->pfr_next) {
13560 		memset(&gw6, 0, sizeof(struct sockaddr_in6));
13561 		gw6.sin6_family = AF_INET6;
13562 		gw6.sin6_len = sizeof(struct sockaddr_in6);
13563 		memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13564 		    sizeof(struct in6_addr));
13565 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13566 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13567 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13568 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13569 		if (sctp_cmpaddr((struct sockaddr *)&gw6,
13570 		    ro->ro_rt->rt_gateway)) {
13571 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13572 			return (1);
13573 		}
13574 	}
13575 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13576 	return (0);
13577 }
13578 
13579 #endif
13580 
13581 int
13582 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13583 {
13584 	struct sockaddr_in *sin, *mask;
13585 	struct ifaddr *ifa;
13586 	struct in_addr srcnetaddr, gwnetaddr;
13587 
13588 	if (ro == NULL || ro->ro_rt == NULL ||
13589 	    sifa->address.sa.sa_family != AF_INET) {
13590 		return (0);
13591 	}
13592 	ifa = (struct ifaddr *)sifa->ifa;
13593 	mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13594 	sin = (struct sockaddr_in *)&sifa->address.sin;
13595 	srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13596 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13597 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13598 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13599 
13600 	sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13601 	gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13602 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13603 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13604 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13605 	if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13606 		return (1);
13607 	}
13608 	return (0);
13609 }
13610