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